Statistics
| Branch: | Revision:

root / darwin-user / machload.c @ 2d72c572

History | View | Annotate | Download (26 kB)

1 831b7825 ths
/*
2 831b7825 ths
 *  Mach-O object file loading
3 831b7825 ths
 *
4 831b7825 ths
 *  Copyright (c) 2006 Pierre d'Herbemont
5 831b7825 ths
 *
6 831b7825 ths
 * This library is free software; you can redistribute it and/or
7 831b7825 ths
 * modify it under the terms of the GNU Lesser General Public
8 831b7825 ths
 * License as published by the Free Software Foundation; either
9 831b7825 ths
 * version 2 of the License, or (at your option) any later version.
10 831b7825 ths
 *
11 831b7825 ths
 * This library is distributed in the hope that it will be useful,
12 831b7825 ths
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 831b7825 ths
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 831b7825 ths
 * Lesser General Public License for more details.
15 831b7825 ths
 *
16 831b7825 ths
 * You should have received a copy of the GNU Lesser General Public
17 831b7825 ths
 * License along with this library; if not, write to the Free Software
18 fad6cb1a aurel32
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA  02110-1301 USA
19 831b7825 ths
 */
20 831b7825 ths
#include <stdio.h>
21 831b7825 ths
#include <sys/types.h>
22 831b7825 ths
#include <fcntl.h>
23 831b7825 ths
#include <sys/stat.h>
24 831b7825 ths
#include <errno.h>
25 831b7825 ths
#include <unistd.h>
26 831b7825 ths
#include <sys/mman.h>
27 831b7825 ths
#include <stdlib.h>
28 831b7825 ths
#include <string.h>
29 831b7825 ths
30 831b7825 ths
#include "qemu.h"
31 831b7825 ths
#include "disas.h"
32 831b7825 ths
33 831b7825 ths
#include <mach-o/loader.h>
34 831b7825 ths
#include <mach-o/fat.h>
35 831b7825 ths
#include <mach-o/nlist.h>
36 831b7825 ths
#include <mach-o/reloc.h>
37 831b7825 ths
#include <mach-o/ppc/reloc.h>
38 831b7825 ths
39 831b7825 ths
//#define DEBUG_MACHLOAD
40 831b7825 ths
41 831b7825 ths
#ifdef DEBUG_MACHLOAD
42 93fcfe39 aliguori
# define DPRINTF(...) do { qemu_log(__VA_ARGS__); printf(__VA_ARGS__); } while(0)
43 831b7825 ths
#else
44 93fcfe39 aliguori
# define DPRINTF(...) do { qemu_log(__VA_ARGS__); } while(0)
45 831b7825 ths
#endif
46 831b7825 ths
47 831b7825 ths
# define check_mach_header(x) (x.magic == MH_CIGAM)
48 831b7825 ths
49 831b7825 ths
extern const char *interp_prefix;
50 831b7825 ths
51 831b7825 ths
/* we don't have a good implementation for this */
52 831b7825 ths
#define DONT_USE_DYLD_SHARED_MAP
53 831b7825 ths
54 831b7825 ths
/* Pass extra arg to DYLD for debug */
55 831b7825 ths
//#define ACTIVATE_DYLD_TRACE
56 831b7825 ths
57 831b7825 ths
//#define OVERRIDE_DYLINKER
58 831b7825 ths
59 831b7825 ths
#ifdef OVERRIDE_DYLINKER
60 831b7825 ths
# ifdef TARGET_I386
61 831b7825 ths
#  define DYLINKER_NAME "/Users/steg/qemu/tests/i386-darwin-env/usr/lib/dyld"
62 831b7825 ths
# else
63 831b7825 ths
#  define DYLINKER_NAME "/usr/lib/dyld"
64 831b7825 ths
# endif
65 831b7825 ths
#endif
66 831b7825 ths
67 831b7825 ths
/* XXX: in an include */
68 831b7825 ths
struct nlist_extended
69 831b7825 ths
{
70 831b7825 ths
    union {
71 831b7825 ths
        char *n_name;
72 831b7825 ths
        long  n_strx;
73 831b7825 ths
    } n_un;
74 831b7825 ths
    unsigned char n_type;
75 831b7825 ths
    unsigned char n_sect;
76 831b7825 ths
    short st_desc;
77 831b7825 ths
    unsigned long st_value;
78 831b7825 ths
    unsigned long st_size;
79 831b7825 ths
};
80 831b7825 ths
81 831b7825 ths
/* Print symbols in gdb */
82 831b7825 ths
void *macho_text_sect = 0;
83 831b7825 ths
int   macho_offset = 0;
84 831b7825 ths
85 831b7825 ths
int load_object(const char *filename, struct target_pt_regs * regs, void ** mh);
86 831b7825 ths
void qerror(const char *format, ...);
87 831b7825 ths
#ifdef TARGET_I386
88 831b7825 ths
typedef struct mach_i386_thread_state {
89 831b7825 ths
    unsigned int    eax;
90 831b7825 ths
    unsigned int    ebx;
91 831b7825 ths
    unsigned int    ecx;
92 831b7825 ths
    unsigned int    edx;
93 831b7825 ths
    unsigned int    edi;
94 831b7825 ths
    unsigned int    esi;
95 831b7825 ths
    unsigned int    ebp;
96 831b7825 ths
    unsigned int    esp;
97 831b7825 ths
    unsigned int    ss;
98 831b7825 ths
    unsigned int    eflags;
99 831b7825 ths
    unsigned int    eip;
100 831b7825 ths
    unsigned int    cs;
101 831b7825 ths
    unsigned int    ds;
102 831b7825 ths
    unsigned int    es;
103 831b7825 ths
    unsigned int    fs;
104 831b7825 ths
    unsigned int    gs;
105 831b7825 ths
} mach_i386_thread_state_t;
106 831b7825 ths
107 831b7825 ths
void bswap_i386_thread_state(struct mach_i386_thread_state *ts)
108 831b7825 ths
{
109 831b7825 ths
    bswap32s((uint32_t*)&ts->eax);
110 831b7825 ths
    bswap32s((uint32_t*)&ts->ebx);
111 831b7825 ths
    bswap32s((uint32_t*)&ts->ecx);
112 831b7825 ths
    bswap32s((uint32_t*)&ts->edx);
113 831b7825 ths
    bswap32s((uint32_t*)&ts->edi);
114 831b7825 ths
    bswap32s((uint32_t*)&ts->esi);
115 831b7825 ths
    bswap32s((uint32_t*)&ts->ebp);
116 831b7825 ths
    bswap32s((uint32_t*)&ts->esp);
117 831b7825 ths
    bswap32s((uint32_t*)&ts->ss);
118 831b7825 ths
    bswap32s((uint32_t*)&ts->eflags);
119 831b7825 ths
    bswap32s((uint32_t*)&ts->eip);
120 831b7825 ths
    bswap32s((uint32_t*)&ts->cs);
121 831b7825 ths
    bswap32s((uint32_t*)&ts->ds);
122 831b7825 ths
    bswap32s((uint32_t*)&ts->es);
123 831b7825 ths
    bswap32s((uint32_t*)&ts->fs);
124 831b7825 ths
    bswap32s((uint32_t*)&ts->gs);
125 831b7825 ths
}
126 831b7825 ths
#define target_thread_state mach_i386_thread_state
127 831b7825 ths
#define TARGET_CPU_TYPE CPU_TYPE_I386
128 831b7825 ths
#define TARGET_CPU_NAME "i386"
129 831b7825 ths
#endif
130 831b7825 ths
131 831b7825 ths
#ifdef TARGET_PPC
132 831b7825 ths
struct mach_ppc_thread_state {
133 831b7825 ths
    unsigned int srr0;      /* Instruction address register (PC) */
134 831b7825 ths
    unsigned int srr1;    /* Machine state register (supervisor) */
135 831b7825 ths
    unsigned int r0;
136 831b7825 ths
    unsigned int r1;
137 831b7825 ths
    unsigned int r2;
138 831b7825 ths
    unsigned int r3;
139 831b7825 ths
    unsigned int r4;
140 831b7825 ths
    unsigned int r5;
141 831b7825 ths
    unsigned int r6;
142 831b7825 ths
    unsigned int r7;
143 831b7825 ths
    unsigned int r8;
144 831b7825 ths
    unsigned int r9;
145 831b7825 ths
    unsigned int r10;
146 831b7825 ths
    unsigned int r11;
147 831b7825 ths
    unsigned int r12;
148 831b7825 ths
    unsigned int r13;
149 831b7825 ths
    unsigned int r14;
150 831b7825 ths
    unsigned int r15;
151 831b7825 ths
    unsigned int r16;
152 831b7825 ths
    unsigned int r17;
153 831b7825 ths
    unsigned int r18;
154 831b7825 ths
    unsigned int r19;
155 831b7825 ths
    unsigned int r20;
156 831b7825 ths
    unsigned int r21;
157 831b7825 ths
    unsigned int r22;
158 831b7825 ths
    unsigned int r23;
159 831b7825 ths
    unsigned int r24;
160 831b7825 ths
    unsigned int r25;
161 831b7825 ths
    unsigned int r26;
162 831b7825 ths
    unsigned int r27;
163 831b7825 ths
    unsigned int r28;
164 831b7825 ths
    unsigned int r29;
165 831b7825 ths
    unsigned int r30;
166 831b7825 ths
    unsigned int r31;
167 831b7825 ths
168 831b7825 ths
    unsigned int cr;        /* Condition register */
169 831b7825 ths
    unsigned int xer;    /* User's integer exception register */
170 831b7825 ths
    unsigned int lr;    /* Link register */
171 831b7825 ths
    unsigned int ctr;    /* Count register */
172 831b7825 ths
    unsigned int mq;    /* MQ register (601 only) */
173 831b7825 ths
174 831b7825 ths
    unsigned int vrsave;    /* Vector Save Register */
175 831b7825 ths
};
176 831b7825 ths
177 831b7825 ths
void bswap_ppc_thread_state(struct mach_ppc_thread_state *ts)
178 831b7825 ths
{
179 831b7825 ths
    bswap32s((uint32_t*)&ts->srr0);
180 831b7825 ths
    bswap32s((uint32_t*)&ts->srr1);
181 831b7825 ths
    bswap32s((uint32_t*)&ts->r0);
182 831b7825 ths
    bswap32s((uint32_t*)&ts->r1);
183 831b7825 ths
    bswap32s((uint32_t*)&ts->r2);
184 831b7825 ths
    bswap32s((uint32_t*)&ts->r3);
185 831b7825 ths
    bswap32s((uint32_t*)&ts->r4);
186 831b7825 ths
    bswap32s((uint32_t*)&ts->r5);
187 831b7825 ths
    bswap32s((uint32_t*)&ts->r6);
188 831b7825 ths
    bswap32s((uint32_t*)&ts->r7);
189 831b7825 ths
    bswap32s((uint32_t*)&ts->r8);
190 831b7825 ths
    bswap32s((uint32_t*)&ts->r9);
191 831b7825 ths
    bswap32s((uint32_t*)&ts->r10);
192 831b7825 ths
    bswap32s((uint32_t*)&ts->r11);
193 831b7825 ths
    bswap32s((uint32_t*)&ts->r12);
194 831b7825 ths
    bswap32s((uint32_t*)&ts->r13);
195 831b7825 ths
    bswap32s((uint32_t*)&ts->r14);
196 831b7825 ths
    bswap32s((uint32_t*)&ts->r15);
197 831b7825 ths
    bswap32s((uint32_t*)&ts->r16);
198 831b7825 ths
    bswap32s((uint32_t*)&ts->r17);
199 831b7825 ths
    bswap32s((uint32_t*)&ts->r18);
200 831b7825 ths
    bswap32s((uint32_t*)&ts->r19);
201 831b7825 ths
    bswap32s((uint32_t*)&ts->r20);
202 831b7825 ths
    bswap32s((uint32_t*)&ts->r21);
203 831b7825 ths
    bswap32s((uint32_t*)&ts->r22);
204 831b7825 ths
    bswap32s((uint32_t*)&ts->r23);
205 831b7825 ths
    bswap32s((uint32_t*)&ts->r24);
206 831b7825 ths
    bswap32s((uint32_t*)&ts->r25);
207 831b7825 ths
    bswap32s((uint32_t*)&ts->r26);
208 831b7825 ths
    bswap32s((uint32_t*)&ts->r27);
209 831b7825 ths
    bswap32s((uint32_t*)&ts->r28);
210 831b7825 ths
    bswap32s((uint32_t*)&ts->r29);
211 831b7825 ths
    bswap32s((uint32_t*)&ts->r30);
212 831b7825 ths
    bswap32s((uint32_t*)&ts->r31);
213 831b7825 ths
214 831b7825 ths
    bswap32s((uint32_t*)&ts->cr);
215 831b7825 ths
    bswap32s((uint32_t*)&ts->xer);
216 831b7825 ths
    bswap32s((uint32_t*)&ts->lr);
217 831b7825 ths
    bswap32s((uint32_t*)&ts->ctr);
218 831b7825 ths
    bswap32s((uint32_t*)&ts->mq);
219 831b7825 ths
220 831b7825 ths
    bswap32s((uint32_t*)&ts->vrsave);
221 831b7825 ths
}
222 831b7825 ths
223 831b7825 ths
#define target_thread_state mach_ppc_thread_state
224 831b7825 ths
#define TARGET_CPU_TYPE CPU_TYPE_POWERPC
225 831b7825 ths
#define TARGET_CPU_NAME "PowerPC"
226 831b7825 ths
#endif
227 831b7825 ths
228 831b7825 ths
struct target_thread_command {
229 831b7825 ths
    unsigned long    cmd;    /* LC_THREAD or  LC_UNIXTHREAD */
230 831b7825 ths
    unsigned long    cmdsize;    /* total size of this command */
231 831b7825 ths
    unsigned long flavor;    /* flavor of thread state */
232 831b7825 ths
    unsigned long count;        /* count of longs in thread state */
233 831b7825 ths
    struct target_thread_state state;  /* thread state for this flavor */
234 831b7825 ths
};
235 831b7825 ths
236 831b7825 ths
void bswap_tc(struct target_thread_command *tc)
237 831b7825 ths
{
238 831b7825 ths
    bswap32s((uint32_t*)(&tc->flavor));
239 831b7825 ths
    bswap32s((uint32_t*)&tc->count);
240 831b7825 ths
#if defined(TARGET_I386)
241 831b7825 ths
    bswap_i386_thread_state(&tc->state);
242 831b7825 ths
#elif defined(TARGET_PPC)
243 831b7825 ths
    bswap_ppc_thread_state(&tc->state);
244 831b7825 ths
#else
245 831b7825 ths
# error unknown TARGET_CPU_TYPE
246 831b7825 ths
#endif
247 831b7825 ths
}
248 831b7825 ths
249 831b7825 ths
void bswap_mh(struct mach_header *mh)
250 831b7825 ths
{
251 831b7825 ths
    bswap32s((uint32_t*)(&mh->magic));
252 831b7825 ths
    bswap32s((uint32_t*)&mh->cputype);
253 831b7825 ths
    bswap32s((uint32_t*)&mh->cpusubtype);
254 831b7825 ths
    bswap32s((uint32_t*)&mh->filetype);
255 831b7825 ths
    bswap32s((uint32_t*)&mh->ncmds);
256 831b7825 ths
    bswap32s((uint32_t*)&mh->sizeofcmds);
257 831b7825 ths
    bswap32s((uint32_t*)&mh->flags);
258 831b7825 ths
}
259 831b7825 ths
260 831b7825 ths
void bswap_lc(struct load_command *lc)
261 831b7825 ths
{
262 831b7825 ths
    bswap32s((uint32_t*)&lc->cmd);
263 831b7825 ths
    bswap32s((uint32_t*)&lc->cmdsize);
264 831b7825 ths
}
265 831b7825 ths
266 831b7825 ths
267 831b7825 ths
void bswap_fh(struct fat_header *fh)
268 831b7825 ths
{
269 831b7825 ths
    bswap32s((uint32_t*)&fh->magic);
270 831b7825 ths
    bswap32s((uint32_t*)&fh->nfat_arch);
271 831b7825 ths
}
272 831b7825 ths
273 831b7825 ths
void bswap_fa(struct fat_arch *fa)
274 831b7825 ths
{
275 831b7825 ths
    bswap32s((uint32_t*)&fa->cputype);
276 831b7825 ths
    bswap32s((uint32_t*)&fa->cpusubtype);
277 831b7825 ths
    bswap32s((uint32_t*)&fa->offset);
278 831b7825 ths
    bswap32s((uint32_t*)&fa->size);
279 831b7825 ths
    bswap32s((uint32_t*)&fa->align);
280 831b7825 ths
}
281 831b7825 ths
282 831b7825 ths
void bswap_segcmd(struct segment_command *sc)
283 831b7825 ths
{
284 831b7825 ths
    bswap32s((uint32_t*)&sc->vmaddr);
285 831b7825 ths
    bswap32s((uint32_t*)&sc->vmsize);
286 831b7825 ths
    bswap32s((uint32_t*)&sc->fileoff);
287 831b7825 ths
    bswap32s((uint32_t*)&sc->filesize);
288 831b7825 ths
    bswap32s((uint32_t*)&sc->maxprot);
289 831b7825 ths
    bswap32s((uint32_t*)&sc->initprot);
290 831b7825 ths
    bswap32s((uint32_t*)&sc->nsects);
291 831b7825 ths
    bswap32s((uint32_t*)&sc->flags);
292 831b7825 ths
}
293 831b7825 ths
294 831b7825 ths
void bswap_symtabcmd(struct symtab_command *stc)
295 831b7825 ths
{
296 831b7825 ths
    bswap32s((uint32_t*)&stc->cmd);
297 831b7825 ths
    bswap32s((uint32_t*)&stc->cmdsize);
298 831b7825 ths
    bswap32s((uint32_t*)&stc->symoff);
299 831b7825 ths
    bswap32s((uint32_t*)&stc->nsyms);
300 831b7825 ths
    bswap32s((uint32_t*)&stc->stroff);
301 831b7825 ths
    bswap32s((uint32_t*)&stc->strsize);
302 831b7825 ths
}
303 831b7825 ths
304 831b7825 ths
void bswap_sym(struct nlist *n)
305 831b7825 ths
{
306 831b7825 ths
    bswap32s((uint32_t*)&n->n_un.n_strx);
307 831b7825 ths
    bswap16s((uint16_t*)&n->n_desc);
308 831b7825 ths
    bswap32s((uint32_t*)&n->n_value);
309 831b7825 ths
}
310 831b7825 ths
311 831b7825 ths
int load_thread(struct mach_header *mh, struct target_thread_command *tc, struct target_pt_regs * regs, int fd, int mh_pos, int need_bswap)
312 831b7825 ths
{
313 831b7825 ths
    int entry;
314 831b7825 ths
    if(need_bswap)
315 831b7825 ths
        bswap_tc(tc);
316 831b7825 ths
#if defined(TARGET_I386)
317 831b7825 ths
    entry = tc->state.eip;
318 831b7825 ths
    DPRINTF(" eax 0x%.8x\n ebx 0x%.8x\n ecx 0x%.8x\n edx 0x%.8x\n edi 0x%.8x\n esi 0x%.8x\n ebp 0x%.8x\n esp 0x%.8x\n ss 0x%.8x\n eflags 0x%.8x\n eip 0x%.8x\n cs 0x%.8x\n ds 0x%.8x\n es 0x%.8x\n fs 0x%.8x\n gs 0x%.8x\n",
319 831b7825 ths
            tc->state.eax, tc->state.ebx, tc->state.ecx, tc->state.edx, tc->state.edi, tc->state.esi, tc->state.ebp,
320 831b7825 ths
            tc->state.esp, tc->state.ss, tc->state.eflags, tc->state.eip, tc->state.cs, tc->state.ds, tc->state.es,
321 831b7825 ths
            tc->state.fs, tc->state.gs );
322 831b7825 ths
#define reg_copy(reg)   regs->reg = tc->state.reg
323 831b7825 ths
    if(regs)
324 831b7825 ths
    {
325 831b7825 ths
        reg_copy(eax);
326 831b7825 ths
        reg_copy(ebx);
327 831b7825 ths
        reg_copy(ecx);
328 831b7825 ths
        reg_copy(edx);
329 831b7825 ths
330 831b7825 ths
        reg_copy(edi);
331 831b7825 ths
        reg_copy(esi);
332 831b7825 ths
333 831b7825 ths
        reg_copy(ebp);
334 831b7825 ths
        reg_copy(esp);
335 831b7825 ths
336 831b7825 ths
        reg_copy(eflags);
337 831b7825 ths
        reg_copy(eip);
338 831b7825 ths
    /*
339 831b7825 ths
        reg_copy(ss);
340 831b7825 ths
        reg_copy(cs);
341 831b7825 ths
        reg_copy(ds);
342 831b7825 ths
        reg_copy(es);
343 831b7825 ths
        reg_copy(fs);
344 831b7825 ths
        reg_copy(gs);*/
345 831b7825 ths
    }
346 831b7825 ths
#undef reg_copy
347 831b7825 ths
#elif defined(TARGET_PPC)
348 831b7825 ths
    entry =  tc->state.srr0;
349 831b7825 ths
#endif
350 831b7825 ths
    DPRINTF("load_thread: entry 0x%x\n", entry);
351 831b7825 ths
    return entry;
352 831b7825 ths
}
353 831b7825 ths
354 831b7825 ths
int load_dylinker(struct mach_header *mh, struct dylinker_command *dc, int fd, int mh_pos, int need_bswap)
355 831b7825 ths
{
356 831b7825 ths
    int size;
357 831b7825 ths
    char * dylinker_name;
358 831b7825 ths
    size = dc->cmdsize - sizeof(struct dylinker_command);
359 831b7825 ths
360 831b7825 ths
    if(need_bswap)
361 831b7825 ths
        dylinker_name = (char*)(bswap_32(dc->name.offset)+(int)dc);
362 831b7825 ths
    else
363 831b7825 ths
        dylinker_name = (char*)((dc->name.offset)+(int)dc);
364 831b7825 ths
365 831b7825 ths
#ifdef OVERRIDE_DYLINKER
366 831b7825 ths
    dylinker_name = DYLINKER_NAME;
367 831b7825 ths
#else
368 831b7825 ths
    if(asprintf(&dylinker_name, "%s%s", interp_prefix, dylinker_name) == -1)
369 831b7825 ths
        qerror("can't allocate the new dylinker name\n");
370 831b7825 ths
#endif
371 831b7825 ths
372 831b7825 ths
    DPRINTF("dylinker_name %s\n", dylinker_name);
373 831b7825 ths
    return load_object(dylinker_name, NULL, NULL);
374 831b7825 ths
}
375 831b7825 ths
376 831b7825 ths
int load_segment(struct mach_header *mh, struct segment_command *sc, int fd, int mh_pos, int need_bswap, int fixed, int slide)
377 831b7825 ths
{
378 831b7825 ths
    unsigned long addr = sc->vmaddr;
379 831b7825 ths
    unsigned long size = sc->filesize;
380 831b7825 ths
    unsigned long error = 0;
381 831b7825 ths
382 831b7825 ths
    if(need_bswap)
383 831b7825 ths
        bswap_segcmd(sc);
384 831b7825 ths
385 831b7825 ths
    if(sc->vmaddr == 0)
386 831b7825 ths
    {
387 831b7825 ths
        DPRINTF("load_segment: sc->vmaddr == 0 returning\n");
388 831b7825 ths
        return -1;
389 831b7825 ths
    }
390 831b7825 ths
391 831b7825 ths
    if (strcmp(sc->segname, "__PAGEZERO") == 0)
392 831b7825 ths
    {
393 831b7825 ths
        DPRINTF("load_segment: __PAGEZERO returning\n");
394 831b7825 ths
        return -1;
395 831b7825 ths
    }
396 831b7825 ths
397 831b7825 ths
    /* Right now mmap memory */
398 831b7825 ths
    /* XXX: should check to see that the space is free, because MAP_FIXED is dangerous */
399 831b7825 ths
    DPRINTF("load_segment: mmaping %s to 0x%x-(0x%x|0x%x) + 0x%x\n", sc->segname, sc->vmaddr, sc->filesize, sc->vmsize, slide);
400 831b7825 ths
401 831b7825 ths
    if(sc->filesize > 0)
402 831b7825 ths
    {
403 831b7825 ths
        int opt = 0;
404 831b7825 ths
405 831b7825 ths
        if(fixed)
406 831b7825 ths
            opt |= MAP_FIXED;
407 831b7825 ths
408 831b7825 ths
        DPRINTF("sc->vmaddr 0x%x slide 0x%x add 0x%x\n", slide, sc->vmaddr, sc->vmaddr+slide);
409 831b7825 ths
410 831b7825 ths
        addr = target_mmap(sc->vmaddr+slide, sc->filesize,  sc->initprot, opt, fd, mh_pos + sc->fileoff);
411 831b7825 ths
412 831b7825 ths
        if(addr==-1)
413 831b7825 ths
            qerror("load_segment: can't mmap at 0x%x\n", sc->vmaddr+slide);
414 831b7825 ths
415 831b7825 ths
        error = addr-sc->vmaddr;
416 831b7825 ths
    }
417 831b7825 ths
    else
418 831b7825 ths
    {
419 831b7825 ths
        addr = sc->vmaddr+slide;
420 831b7825 ths
        error = slide;
421 831b7825 ths
    }
422 831b7825 ths
423 831b7825 ths
    if(sc->vmsize > sc->filesize)
424 831b7825 ths
    {
425 831b7825 ths
        addr += sc->filesize;
426 831b7825 ths
        size = sc->vmsize-sc->filesize;
427 831b7825 ths
        addr = target_mmap(addr, size, sc->initprot, MAP_ANONYMOUS | MAP_FIXED, -1, 0);
428 831b7825 ths
        if(addr==-1)
429 831b7825 ths
            qerror("load_segment: can't mmap at 0x%x\n", sc->vmaddr+slide);
430 831b7825 ths
    }
431 831b7825 ths
432 831b7825 ths
    return error;
433 831b7825 ths
}
434 831b7825 ths
435 831b7825 ths
void *load_data(int fd, long offset, unsigned int size)
436 831b7825 ths
{
437 831b7825 ths
    char *data;
438 831b7825 ths
439 831b7825 ths
    data = malloc(size);
440 831b7825 ths
    if (!data)
441 831b7825 ths
        return NULL;
442 831b7825 ths
    lseek(fd, offset, SEEK_SET);
443 831b7825 ths
    if (read(fd, data, size) != size) {
444 831b7825 ths
        free(data);
445 831b7825 ths
        return NULL;
446 831b7825 ths
    }
447 831b7825 ths
    return data;
448 831b7825 ths
}
449 831b7825 ths
450 831b7825 ths
/* load a mach-o object file */
451 831b7825 ths
int load_object(const char *filename, struct target_pt_regs * regs, void ** mh)
452 831b7825 ths
{
453 831b7825 ths
    int need_bswap = 0;
454 831b7825 ths
    int entry_point = 0;
455 831b7825 ths
    int dyld_entry_point = 0;
456 831b7825 ths
    int slide, mmapfixed;
457 831b7825 ths
    int fd;
458 831b7825 ths
    struct load_command *lcmds, *lc;
459 831b7825 ths
    int is_fat = 0;
460 831b7825 ths
    unsigned int i, magic;
461 831b7825 ths
    int mach_hdr_pos = 0;
462 831b7825 ths
    struct mach_header mach_hdr;
463 831b7825 ths
464 831b7825 ths
    /* for symbol lookup whith -d flag. */
465 831b7825 ths
    struct symtab_command *    symtabcmd = 0;
466 831b7825 ths
    struct nlist_extended *symtab, *sym;
467 831b7825 ths
    struct nlist     *symtab_std, *syment;
468 831b7825 ths
    char            *strtab;
469 831b7825 ths
470 831b7825 ths
    fd = open(filename, O_RDONLY);
471 831b7825 ths
    if (fd < 0)
472 831b7825 ths
        qerror("can't open file '%s'", filename);
473 831b7825 ths
474 831b7825 ths
    /* Read magic header.  */
475 831b7825 ths
    if (read(fd, &magic, sizeof (magic)) != sizeof (magic))
476 831b7825 ths
        qerror("unable to read Magic of '%s'", filename);
477 831b7825 ths
478 831b7825 ths
    /* Check Mach identification.  */
479 831b7825 ths
    if(magic == MH_MAGIC)
480 831b7825 ths
    {
481 831b7825 ths
        is_fat = 0;
482 831b7825 ths
        need_bswap = 0;
483 831b7825 ths
    } else if (magic == MH_CIGAM)
484 831b7825 ths
    {
485 831b7825 ths
        is_fat = 0;
486 831b7825 ths
        need_bswap = 1;
487 831b7825 ths
    } else if (magic == FAT_MAGIC)
488 831b7825 ths
    {
489 831b7825 ths
        is_fat = 1;
490 831b7825 ths
        need_bswap = 0;
491 831b7825 ths
    } else if (magic == FAT_CIGAM)
492 831b7825 ths
    {
493 831b7825 ths
        is_fat = 1;
494 831b7825 ths
        need_bswap = 1;
495 831b7825 ths
    }
496 831b7825 ths
    else
497 831b7825 ths
        qerror("Not a Mach-O file.", filename);
498 831b7825 ths
499 831b7825 ths
    DPRINTF("loading %s %s...\n", filename, is_fat ? "[FAT]": "[REGULAR]");
500 831b7825 ths
    if(is_fat)
501 831b7825 ths
    {
502 831b7825 ths
        int found = 0;
503 831b7825 ths
        struct fat_header fh;
504 831b7825 ths
        struct fat_arch *fa;
505 831b7825 ths
506 831b7825 ths
        lseek(fd, 0, SEEK_SET);
507 831b7825 ths
508 831b7825 ths
        /* Read Fat header.  */
509 831b7825 ths
        if (read(fd, &fh, sizeof (fh)) != sizeof (fh))
510 831b7825 ths
            qerror("unable to read file header");
511 831b7825 ths
512 831b7825 ths
        if(need_bswap)
513 831b7825 ths
            bswap_fh(&fh);
514 831b7825 ths
515 831b7825 ths
        /* Read Fat Arch.  */
516 831b7825 ths
        fa = malloc(sizeof(struct fat_arch)*fh.nfat_arch);
517 831b7825 ths
518 831b7825 ths
        if (read(fd, fa, sizeof(struct fat_arch)*fh.nfat_arch) != sizeof(struct fat_arch)*fh.nfat_arch)
519 831b7825 ths
            qerror("unable to read file header");
520 831b7825 ths
521 831b7825 ths
        for( i = 0; i < fh.nfat_arch; i++, fa++)
522 831b7825 ths
        {
523 831b7825 ths
            if(need_bswap)
524 831b7825 ths
                bswap_fa(fa);
525 831b7825 ths
            if(fa->cputype == TARGET_CPU_TYPE)
526 831b7825 ths
            {
527 831b7825 ths
                mach_hdr_pos = fa->offset;
528 831b7825 ths
                lseek(fd, mach_hdr_pos, SEEK_SET);
529 831b7825 ths
530 831b7825 ths
                /* Read Mach header.  */
531 831b7825 ths
532 831b7825 ths
                if (read(fd, &mach_hdr, sizeof(struct mach_header)) != sizeof (struct mach_header))
533 831b7825 ths
                    qerror("unable to read file header");
534 831b7825 ths
535 831b7825 ths
                if(mach_hdr.magic == MH_MAGIC)
536 831b7825 ths
                    need_bswap = 0;
537 831b7825 ths
                else if (mach_hdr.magic == MH_CIGAM)
538 831b7825 ths
                    need_bswap = 1;
539 831b7825 ths
                else
540 831b7825 ths
                    qerror("Invalid mach header in Fat Mach-O File");
541 831b7825 ths
                found = 1;
542 831b7825 ths
                break;
543 831b7825 ths
            }
544 831b7825 ths
        }
545 831b7825 ths
        if(!found)
546 831b7825 ths
            qerror("%s: No %s CPU found in FAT Header", filename, TARGET_CPU_NAME);
547 831b7825 ths
    }
548 831b7825 ths
    else
549 831b7825 ths
    {
550 831b7825 ths
        lseek(fd, 0, SEEK_SET);
551 831b7825 ths
        /* Read Mach header */
552 831b7825 ths
        if (read(fd, &mach_hdr, sizeof (mach_hdr)) != sizeof (mach_hdr))
553 831b7825 ths
            qerror("%s: unable to read file header", filename);
554 831b7825 ths
    }
555 831b7825 ths
556 831b7825 ths
    if(need_bswap)
557 831b7825 ths
        bswap_mh(&mach_hdr);
558 831b7825 ths
559 831b7825 ths
    if ((mach_hdr.cputype) != TARGET_CPU_TYPE)
560 831b7825 ths
        qerror("%s: Unsupported CPU 0x%x (only 0x%x(%s) supported)", filename, mach_hdr.cputype, TARGET_CPU_TYPE, TARGET_CPU_NAME);
561 831b7825 ths
562 831b7825 ths
563 831b7825 ths
    switch(mach_hdr.filetype)
564 831b7825 ths
    {
565 831b7825 ths
        case MH_EXECUTE:  break;
566 831b7825 ths
        case MH_FVMLIB:
567 831b7825 ths
        case MH_DYLIB:
568 831b7825 ths
        case MH_DYLINKER: break;
569 831b7825 ths
        default:
570 831b7825 ths
            qerror("%s: Unsupported Mach type (0x%x)", filename, mach_hdr.filetype);
571 831b7825 ths
    }
572 831b7825 ths
573 831b7825 ths
    /* read segment headers */
574 831b7825 ths
    lcmds = malloc(mach_hdr.sizeofcmds);
575 831b7825 ths
576 831b7825 ths
    if(read(fd, lcmds, mach_hdr.sizeofcmds) != mach_hdr.sizeofcmds)
577 831b7825 ths
            qerror("%s: unable to read load_command", filename);
578 831b7825 ths
    slide = 0;
579 831b7825 ths
    mmapfixed = 0;
580 831b7825 ths
    for(i=0, lc = lcmds; i < (mach_hdr.ncmds) ; i++)
581 831b7825 ths
    {
582 831b7825 ths
583 831b7825 ths
        if(need_bswap)
584 831b7825 ths
            bswap_lc(lc);
585 831b7825 ths
        switch(lc->cmd)
586 831b7825 ths
        {
587 831b7825 ths
            case LC_SEGMENT:
588 831b7825 ths
                /* The main_exe can't be relocated */
589 831b7825 ths
                if(mach_hdr.filetype == MH_EXECUTE)
590 831b7825 ths
                    mmapfixed = 1;
591 831b7825 ths
592 831b7825 ths
                slide = load_segment(&mach_hdr, (struct segment_command*)lc, fd, mach_hdr_pos, need_bswap, mmapfixed, slide);
593 831b7825 ths
594 831b7825 ths
                /* other segment must be mapped according to slide exactly, if load_segment did something */
595 831b7825 ths
                if(slide != -1)
596 831b7825 ths
                    mmapfixed = 1;
597 831b7825 ths
                else
598 831b7825 ths
                    slide = 0; /* load_segment didn't map the segment */
599 831b7825 ths
600 831b7825 ths
                if(mach_hdr.filetype == MH_EXECUTE && slide != 0)
601 831b7825 ths
                    qerror("%s: Warning executable can't be mapped at the right address (offset: 0x%x)\n", filename, slide);
602 831b7825 ths
603 831b7825 ths
                if(strcmp(((struct segment_command*)(lc))->segname, "__TEXT") == 0)
604 831b7825 ths
                {
605 831b7825 ths
                    /* Text section */
606 831b7825 ths
                    if(mach_hdr.filetype == MH_EXECUTE)
607 831b7825 ths
                    {
608 831b7825 ths
                        /* return the mach_header */
609 831b7825 ths
                        *mh = (void*)(((struct segment_command*)(lc))->vmaddr + slide);
610 831b7825 ths
                    }
611 831b7825 ths
                    else
612 831b7825 ths
                    {
613 831b7825 ths
                        /* it is dyld save the section for gdb, we will be interested in dyld symbol
614 831b7825 ths
                           while debuging */
615 831b7825 ths
                        macho_text_sect = (void*)(((struct segment_command*)(lc))->vmaddr + slide);
616 831b7825 ths
                        macho_offset = slide;
617 831b7825 ths
                    }
618 831b7825 ths
                }
619 831b7825 ths
                break;
620 831b7825 ths
            case LC_LOAD_DYLINKER:
621 831b7825 ths
                dyld_entry_point = load_dylinker( &mach_hdr, (struct dylinker_command*)lc, fd, mach_hdr_pos, need_bswap );
622 831b7825 ths
                break;
623 831b7825 ths
            case LC_LOAD_DYLIB:
624 831b7825 ths
                /* dyld will do that for us */
625 831b7825 ths
                break;
626 831b7825 ths
            case LC_THREAD:
627 831b7825 ths
            case LC_UNIXTHREAD:
628 831b7825 ths
                {
629 831b7825 ths
                struct target_pt_regs * _regs;
630 831b7825 ths
                if(mach_hdr.filetype == MH_DYLINKER)
631 831b7825 ths
                    _regs = regs;
632 831b7825 ths
                else
633 831b7825 ths
                    _regs = 0;
634 831b7825 ths
                entry_point = load_thread( &mach_hdr, (struct target_thread_command*)lc, _regs, fd, mach_hdr_pos, need_bswap );
635 831b7825 ths
                }
636 831b7825 ths
                break;
637 831b7825 ths
            case LC_SYMTAB:
638 831b7825 ths
                /* Save the symtab and strtab */
639 831b7825 ths
                symtabcmd = (struct symtab_command *)lc;
640 831b7825 ths
                break;
641 831b7825 ths
            case LC_ID_DYLINKER:
642 831b7825 ths
            case LC_ID_DYLIB:
643 831b7825 ths
            case LC_UUID:
644 831b7825 ths
            case LC_DYSYMTAB:
645 831b7825 ths
            case LC_TWOLEVEL_HINTS:
646 831b7825 ths
            case LC_PREBIND_CKSUM:
647 831b7825 ths
            case LC_SUB_LIBRARY:
648 831b7825 ths
                break;
649 831b7825 ths
            default: fprintf(stderr, "warning: unkown command 0x%x in '%s'\n", lc->cmd, filename);
650 831b7825 ths
        }
651 831b7825 ths
        lc = (struct load_command*)((int)(lc)+(lc->cmdsize));
652 831b7825 ths
    }
653 831b7825 ths
654 831b7825 ths
    if(symtabcmd)
655 831b7825 ths
    {
656 831b7825 ths
        if(need_bswap)
657 831b7825 ths
            bswap_symtabcmd(symtabcmd);
658 831b7825 ths
659 831b7825 ths
        symtab_std = load_data(fd, symtabcmd->symoff+mach_hdr_pos, symtabcmd->nsyms * sizeof(struct nlist));
660 831b7825 ths
        strtab = load_data(fd, symtabcmd->stroff+mach_hdr_pos, symtabcmd->strsize);
661 831b7825 ths
662 831b7825 ths
        symtab = malloc(sizeof(struct nlist_extended) * symtabcmd->nsyms);
663 831b7825 ths
664 831b7825 ths
        if(need_bswap)
665 831b7825 ths
        {
666 831b7825 ths
            for(i = 0, syment = symtab_std; i < symtabcmd->nsyms; i++, syment++)
667 831b7825 ths
                bswap_sym(syment);
668 831b7825 ths
        }
669 831b7825 ths
670 831b7825 ths
        for(i = 0, sym = symtab, syment = symtab_std; i < symtabcmd->nsyms; i++, sym++, syment++)
671 831b7825 ths
        {
672 831b7825 ths
            struct nlist *sym_follow, *sym_next = 0;
673 831b7825 ths
            unsigned int j;
674 831b7825 ths
            memset(sym, 0, sizeof(*sym));
675 831b7825 ths
676 831b7825 ths
            sym->n_type = syment->n_type;
677 831b7825 ths
            if ( syment->n_type & N_STAB ) /* Debug symbols are skipped */
678 831b7825 ths
                continue;
679 831b7825 ths
680 831b7825 ths
            memcpy(sym, syment, sizeof(*syment));
681 831b7825 ths
682 831b7825 ths
            /* Find the following symbol in order to get the current symbol size */
683 831b7825 ths
            for(j = 0, sym_follow = symtab_std; j < symtabcmd->nsyms; j++, sym_follow++) {
684 831b7825 ths
                if ( sym_follow->n_type & N_STAB || !(sym_follow->n_value > sym->st_value))
685 831b7825 ths
                    continue;
686 831b7825 ths
                if(!sym_next) {
687 831b7825 ths
                    sym_next = sym_follow;
688 831b7825 ths
                    continue;
689 831b7825 ths
                }
690 831b7825 ths
                if(!(sym_next->n_value > sym_follow->n_value))
691 831b7825 ths
                    continue;
692 831b7825 ths
                sym_next = sym_follow;
693 831b7825 ths
            }
694 831b7825 ths
            if(sym_next)
695 831b7825 ths
                sym->st_size = sym_next->n_value - sym->st_value;
696 831b7825 ths
            else
697 831b7825 ths
                sym->st_size = 10; /* XXX: text_sec_hdr->size + text_sec_hdr->offset - sym->st_value; */
698 831b7825 ths
699 831b7825 ths
            sym->st_value += slide;
700 831b7825 ths
        }
701 831b7825 ths
702 831b7825 ths
        free((void*)symtab_std);
703 831b7825 ths
704 831b7825 ths
        {
705 831b7825 ths
            DPRINTF("saving symtab of %s (%d symbol(s))\n", filename, symtabcmd->nsyms);
706 831b7825 ths
            struct syminfo *s;
707 831b7825 ths
            s = malloc(sizeof(*s));
708 831b7825 ths
            s->disas_symtab = symtab;
709 831b7825 ths
            s->disas_strtab = strtab;
710 831b7825 ths
            s->disas_num_syms = symtabcmd->nsyms;
711 831b7825 ths
            s->next = syminfos;
712 831b7825 ths
            syminfos = s;
713 831b7825 ths
        }
714 831b7825 ths
    }
715 831b7825 ths
    close(fd);
716 831b7825 ths
    if(mach_hdr.filetype == MH_EXECUTE && dyld_entry_point)
717 831b7825 ths
        return dyld_entry_point;
718 831b7825 ths
    else
719 831b7825 ths
        return entry_point+slide;
720 831b7825 ths
}
721 831b7825 ths
722 831b7825 ths
extern unsigned long stack_size;
723 831b7825 ths
724 831b7825 ths
unsigned long setup_arg_pages(void * mh, char ** argv, char ** env)
725 831b7825 ths
{
726 831b7825 ths
    unsigned long stack_base, error, size;
727 831b7825 ths
    int i;
728 831b7825 ths
    int * stack;
729 831b7825 ths
    int argc, envc;
730 831b7825 ths
731 831b7825 ths
    /* Create enough stack to hold everything.  If we don't use
732 831b7825 ths
     * it for args, we'll use it for something else...
733 831b7825 ths
     */
734 831b7825 ths
    size = stack_size;
735 831b7825 ths
736 831b7825 ths
    error = target_mmap(0,
737 831b7825 ths
                        size + qemu_host_page_size,
738 831b7825 ths
                        PROT_READ | PROT_WRITE,
739 831b7825 ths
                        MAP_PRIVATE | MAP_ANONYMOUS,
740 831b7825 ths
                        -1, 0);
741 831b7825 ths
    if (error == -1)
742 831b7825 ths
        qerror("stk mmap");
743 831b7825 ths
744 831b7825 ths
    /* we reserve one extra page at the top of the stack as guard */
745 831b7825 ths
    target_mprotect(error + size, qemu_host_page_size, PROT_NONE);
746 831b7825 ths
747 831b7825 ths
    stack_base = error + size;
748 831b7825 ths
    stack = (void*)stack_base;
749 831b7825 ths
/*
750 831b7825 ths
 *    | STRING AREA |
751 831b7825 ths
 *    +-------------+
752 831b7825 ths
 *    |      0      |
753 831b7825 ths
*    +-------------+
754 831b7825 ths
 *    |  apple[n]   |
755 831b7825 ths
 *    +-------------+
756 831b7825 ths
 *           :
757 831b7825 ths
 *    +-------------+
758 831b7825 ths
 *    |  apple[0]   |
759 831b7825 ths
 *    +-------------+
760 831b7825 ths
 *    |      0      |
761 831b7825 ths
 *    +-------------+
762 831b7825 ths
 *    |    env[n]   |
763 831b7825 ths
 *    +-------------+
764 831b7825 ths
 *           :
765 831b7825 ths
 *           :
766 831b7825 ths
 *    +-------------+
767 831b7825 ths
 *    |    env[0]   |
768 831b7825 ths
 *    +-------------+
769 831b7825 ths
 *    |      0      |
770 831b7825 ths
 *    +-------------+
771 831b7825 ths
 *    | arg[argc-1] |
772 831b7825 ths
 *    +-------------+
773 831b7825 ths
 *           :
774 831b7825 ths
 *           :
775 831b7825 ths
 *    +-------------+
776 831b7825 ths
 *    |    arg[0]   |
777 831b7825 ths
 *    +-------------+
778 831b7825 ths
 *    |     argc    |
779 831b7825 ths
 *    +-------------+
780 831b7825 ths
 * sp->    |      mh     | address of where the a.out's file offset 0 is in memory
781 831b7825 ths
 *    +-------------+
782 831b7825 ths
*/
783 831b7825 ths
    /* Construct the stack Stack grows down */
784 831b7825 ths
    stack--;
785 831b7825 ths
786 831b7825 ths
    /* XXX: string should go up there */
787 831b7825 ths
788 831b7825 ths
    *stack = 0;
789 831b7825 ths
    stack--;
790 831b7825 ths
791 831b7825 ths
    /* Push the absolute path of our executable */
792 831b7825 ths
    DPRINTF("pushing apple %s (0x%x)\n", (char*)argv[0], (int)argv[0]);
793 831b7825 ths
    stl(stack, (int) argv[0]);
794 831b7825 ths
795 831b7825 ths
    stack--;
796 831b7825 ths
797 831b7825 ths
    stl(stack, 0);
798 831b7825 ths
    stack--;
799 831b7825 ths
800 831b7825 ths
    /* Get envc */
801 831b7825 ths
    for(envc = 0; env[envc]; envc++);
802 831b7825 ths
803 831b7825 ths
    for(i = envc-1; i >= 0; i--)
804 831b7825 ths
    {
805 831b7825 ths
        DPRINTF("pushing env %s (0x%x)\n", (char*)env[i], (int)env[i]);
806 831b7825 ths
        stl(stack, (int)env[i]);
807 831b7825 ths
        stack--;
808 831b7825 ths
809 831b7825 ths
        /* XXX: remove that when string will be on top of the stack */
810 831b7825 ths
        page_set_flags((int)env[i], (int)(env[i]+strlen(env[i])), PROT_READ | PAGE_VALID);
811 831b7825 ths
    }
812 831b7825 ths
813 831b7825 ths
    /* Add on the stack the interp_prefix choosen if so */
814 831b7825 ths
    if(interp_prefix[0])
815 831b7825 ths
    {
816 831b7825 ths
        char *dyld_root;
817 831b7825 ths
        asprintf(&dyld_root, "DYLD_ROOT_PATH=%s", interp_prefix);
818 831b7825 ths
        page_set_flags((int)dyld_root, (int)(dyld_root+strlen(interp_prefix)+1), PROT_READ | PAGE_VALID);
819 831b7825 ths
820 831b7825 ths
        stl(stack, (int)dyld_root);
821 831b7825 ths
        stack--;
822 831b7825 ths
    }
823 831b7825 ths
824 831b7825 ths
#ifdef DONT_USE_DYLD_SHARED_MAP
825 831b7825 ths
    {
826 831b7825 ths
        char *shared_map_mode;
827 831b7825 ths
        asprintf(&shared_map_mode, "DYLD_SHARED_REGION=avoid");
828 831b7825 ths
        page_set_flags((int)shared_map_mode, (int)(shared_map_mode+strlen(shared_map_mode)+1), PROT_READ | PAGE_VALID);
829 831b7825 ths
830 831b7825 ths
        stl(stack, (int)shared_map_mode);
831 831b7825 ths
        stack--;
832 831b7825 ths
    }
833 831b7825 ths
#endif
834 831b7825 ths
835 831b7825 ths
#ifdef ACTIVATE_DYLD_TRACE
836 831b7825 ths
    char * extra_env_static[] = {"DYLD_DEBUG_TRACE=yes",
837 831b7825 ths
    "DYLD_PREBIND_DEBUG=3", "DYLD_UNKNOW_TRACE=yes",
838 831b7825 ths
    "DYLD_PRINT_INITIALIZERS=yes",
839 831b7825 ths
    "DYLD_PRINT_SEGMENTS=yes", "DYLD_PRINT_REBASINGS=yes", "DYLD_PRINT_BINDINGS=yes", "DYLD_PRINT_INITIALIZERS=yes", "DYLD_PRINT_WARNINGS=yes" };
840 831b7825 ths
841 831b7825 ths
    char ** extra_env = malloc(sizeof(extra_env_static));
842 831b7825 ths
    bcopy(extra_env_static, extra_env, sizeof(extra_env_static));
843 831b7825 ths
    page_set_flags((int)extra_env, (int)((void*)extra_env+sizeof(extra_env_static)), PROT_READ | PAGE_VALID);
844 831b7825 ths
845 831b7825 ths
    for(i = 0; i<9; i++)
846 831b7825 ths
    {
847 831b7825 ths
        DPRINTF("pushing (extra) env %s (0x%x)\n", (char*)extra_env[i], (int)extra_env[i]);
848 831b7825 ths
        stl(stack, (int) extra_env[i]);
849 831b7825 ths
        stack--;
850 831b7825 ths
    }
851 831b7825 ths
#endif
852 831b7825 ths
853 831b7825 ths
    stl(stack, 0);
854 831b7825 ths
    stack--;
855 831b7825 ths
856 831b7825 ths
    /* Get argc */
857 831b7825 ths
    for(argc = 0; argv[argc]; argc++);
858 831b7825 ths
859 831b7825 ths
    for(i = argc-1; i >= 0; i--)
860 831b7825 ths
    {
861 831b7825 ths
        DPRINTF("pushing arg %s (0x%x)\n", (char*)argv[i], (int)argv[i]);
862 831b7825 ths
        stl(stack, (int) argv[i]);
863 831b7825 ths
        stack--;
864 831b7825 ths
865 831b7825 ths
        /* XXX: remove that when string will be on top of the stack */
866 831b7825 ths
        page_set_flags((int)argv[i], (int)(argv[i]+strlen(argv[i])), PROT_READ | PAGE_VALID);
867 831b7825 ths
    }
868 831b7825 ths
869 831b7825 ths
    DPRINTF("pushing argc %d \n", argc);
870 831b7825 ths
    stl(stack, argc);
871 831b7825 ths
    stack--;
872 831b7825 ths
873 831b7825 ths
    DPRINTF("pushing mh 0x%x \n", (int)mh);
874 831b7825 ths
    stl(stack, (int) mh);
875 831b7825 ths
876 831b7825 ths
    /* Stack points on the mh */
877 831b7825 ths
    return (unsigned long)stack;
878 831b7825 ths
}
879 831b7825 ths
880 831b7825 ths
int mach_exec(const char * filename, char ** argv, char ** envp,
881 831b7825 ths
             struct target_pt_regs * regs)
882 831b7825 ths
{
883 831b7825 ths
    int entrypoint, stack;
884 831b7825 ths
    void * mh; /* the Mach Header that will be  used by dyld */
885 831b7825 ths
886 831b7825 ths
    DPRINTF("mach_exec at 0x%x\n", (int)mach_exec);
887 831b7825 ths
888 831b7825 ths
    entrypoint = load_object(filename, regs, &mh);
889 831b7825 ths
    stack = setup_arg_pages(mh, argv, envp);
890 831b7825 ths
#if defined(TARGET_I386)
891 831b7825 ths
    regs->eip = entrypoint;
892 831b7825 ths
    regs->esp = stack;
893 831b7825 ths
#elif defined(TARGET_PPC)
894 831b7825 ths
    regs->nip = entrypoint;
895 831b7825 ths
    regs->gpr[1] = stack;
896 831b7825 ths
#endif
897 831b7825 ths
    DPRINTF("mach_exec returns eip set to 0x%x esp 0x%x mh 0x%x\n", entrypoint, stack, (int)mh);
898 831b7825 ths
899 831b7825 ths
    if(!entrypoint)
900 831b7825 ths
        qerror("%s: no entry point!\n", filename);
901 831b7825 ths
902 831b7825 ths
    return 0;
903 831b7825 ths
}