Statistics
| Branch: | Revision:

root / darwin-user / machload.c @ bd023f95

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