Chameleon

Chameleon Svn Source Tree

Root/branches/ErmaC/Enoch/i386/libsaio/load.c

1/*
2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 2.0 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 *
22 * load.c - Functions for decoding a Mach-o Kernel.
23 *
24 * Copyright (c) 1998-2003 Apple Computer, Inc.
25 *
26 */
27
28#include <mach-o/fat.h>
29#include <mach-o/loader.h>
30#include <mach/machine/thread_status.h>
31
32#include <sl.h>
33
34#if DEBUG
35#define DBG(x...)printf(x)
36#else
37#define DBG(x...)msglog(x)
38#endif
39
40// Private functions.
41static long DecodeSegment(long cmdBase, unsigned int *load_addr, unsigned int *load_size, void *binary, u_int32_t uncompressed_size);
42static long DecodeUnixThread(long cmdBase, unsigned int *entry);
43static long DecodeSymbolTable(long cmdBase);
44
45
46static unsigned long gBinaryAddress;
47bool gHaveKernelCache;/* XXX aserebln: uninitialized? and only set to true, never to false */
48cpu_type_t archCpuType = CPU_TYPE_I386;
49
50
51//==============================================================================
52// Public function.
53
54long ThinFatFile(void **binary, unsigned long *length)
55{
56unsigned long nfat, swapped, size = 0;
57struct fat_header *fhp = (struct fat_header *)*binary;
58struct fat_arch *fap = (struct fat_arch *)((unsigned long)*binary + sizeof(struct fat_header));
59cpu_type_t fapcputype;
60uint32_t fapoffset;
61uint32_t fapsize;
62
63if (fhp->magic == FAT_MAGIC)/* 0xcafebabe */
64{
65nfat = fhp->nfat_arch;
66swapped = 0;
67}
68else if (fhp->magic == FAT_CIGAM)/* 0xbebafeca */
69{
70nfat = OSSwapInt32(fhp->nfat_arch);
71swapped = 1;
72}
73else
74{
75return -1;
76}
77
78for (; nfat > 0; nfat--, fap++)
79{
80if (swapped)
81{
82fapcputype = OSSwapInt32(fap->cputype);
83fapoffset = OSSwapInt32(fap->offset);
84fapsize = OSSwapInt32(fap->size);
85}
86else
87{
88fapcputype = fap->cputype;
89fapoffset = fap->offset;
90fapsize = fap->size;
91}
92
93if (fapcputype == archCpuType)
94{
95*binary = (void *) ((unsigned long)*binary + fapoffset);
96size = fapsize;
97break;
98}
99}
100
101if (length != 0)
102{
103*length = size;
104}
105
106return 0;
107}
108
109
110//==============================================================================
111
112long DecodeMachO(void *binary, u_int32_t uncompressed_size, entry_t *rentry, char **raddr, int *rsize)
113{
114struct mach_header *mH;
115unsigned long ncmds, cmdBase, cmd, cmdsize, cmdstart;
116// long headerBase, headerAddr, headerSize;
117unsigned int vmaddr = ~0;
118unsigned int vmend = 0;
119unsigned long cnt;
120long ret = -1;
121unsigned int entry = 0;
122
123gBinaryAddress = (unsigned long)binary;
124
125mH = (struct mach_header *)(gBinaryAddress);
126
127/*#if DEBUG
128DBG("magic: 0x%x\n", (unsigned)mH->magic);
129DBG("cputype: 0x%x\n", (unsigned)mH->cputype);
130DBG("cpusubtype: 0x%x\n", (unsigned)mH->cpusubtype);
131DBG("filetype: 0x%x\n", (unsigned)mH->filetype);
132DBG("ncmds: 0x%x\n", (unsigned)mH->ncmds);
133DBG("sizeofcmds: 0x%x\n", (unsigned)mH->sizeofcmds);
134DBG("flags: 0x%x\n", (unsigned)mH->flags);
135DBG("archCpuType: 0x%x\n", archCpuType);
136//getchar();
137#endif*/
138
139switch (archCpuType)
140{
141case CPU_TYPE_I386:
142
143if (mH->magic != MH_MAGIC)
144{
145error("Mach-O (i386) file has bad magic number\n");
146return -1;
147}
148
149cmdstart = (unsigned long)gBinaryAddress + sizeof(struct mach_header);
150break;
151
152case CPU_TYPE_X86_64:
153/*
154if (mH->magic != MH_MAGIC_64 && mH->magic == MH_MAGIC)
155{
156return -1;
157}
158*/
159if (mH->magic != MH_MAGIC_64)
160{
161error("Mach-O file (x86_64) has bad magic number\n");
162return -1;
163}
164
165cmdstart = (unsigned long)gBinaryAddress + sizeof(struct mach_header_64);
166break;
167
168default:
169
170error("Unknown CPU type\n");
171return -1;
172}
173
174cmdBase = cmdstart;
175ncmds = mH->ncmds;
176
177for (cnt = 0; cnt < ncmds; cnt++)
178{
179cmd = ((long *)cmdBase)[0];
180cmdsize = ((long *)cmdBase)[1];
181unsigned int load_addr;
182unsigned int load_size;
183
184switch (cmd)
185{
186case LC_SEGMENT:
187case LC_SEGMENT_64:
188ret = DecodeSegment(cmdBase, &load_addr, &load_size, binary, uncompressed_size);
189
190if (ret == 0 && load_size != 0 && load_addr >= KERNEL_ADDR)
191{
192vmaddr = MIN(vmaddr, load_addr);
193vmend = MAX(vmend, load_addr + load_size);
194}
195break;
196
197case LC_MAIN:/* Mountain Lion's replacement for LC_UNIXTHREAD */
198case LC_UNIXTHREAD:
199ret = DecodeUnixThread(cmdBase, &entry);
200break;
201
202case LC_SYMTAB:
203break;
204
205default:
206#if NOTDEF
207printf("Ignoring cmd type %d.\n", (unsigned)cmd);
208#endif
209break;
210}
211
212if (ret != 0)
213{
214return -1;
215}
216
217cmdBase += cmdsize;
218}
219
220*rentry = (entry_t)( (unsigned long) entry & 0x3fffffff );
221*rsize = vmend - vmaddr;
222*raddr = (char *)vmaddr;
223
224cmdBase = cmdstart;
225
226for (cnt = 0; cnt < ncmds; cnt++)
227{
228cmd = ((long *)cmdBase)[0];
229cmdsize = ((long *)cmdBase)[1];
230
231if (cmd == LC_SYMTAB)
232{
233if (DecodeSymbolTable(cmdBase) != 0)
234{
235return -1;
236}
237}
238
239cmdBase += cmdsize;
240}
241
242return ret;
243}
244
245
246//==============================================================================
247// Private function.
248
249static long DecodeSegment(long cmdBase, unsigned int *load_addr, unsigned int *load_size, void *binary, u_int32_t uncompressed_size)
250{
251char *segname;
252long vmsize, filesize;
253unsigned long vmaddr, fileoff, fileaddr;
254
255if (((long *)cmdBase)[0] == LC_SEGMENT_64)
256{
257struct segment_command_64 *segCmd;
258segCmd = (struct segment_command_64 *)cmdBase;
259vmaddr = (segCmd->vmaddr & 0x3fffffff);
260vmsize = segCmd->vmsize;
261fileoff = segCmd->fileoff;
262fileaddr = (gBinaryAddress + segCmd->fileoff);
263filesize = segCmd->filesize;
264segname = (char *)segCmd->segname;
265
266#ifdef DEBUG
267printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, fileaddr: %x, filesize: %x, nsects: %d, flags: %x.\n",
268segCmd->segname, (unsigned)vmaddr, (unsigned)vmsize, segCmd->fileoff, (unsigned)fileaddr, (unsigned)filesize,
269(unsigned) segCmd->nsects, (unsigned)segCmd->flags);
270getchar();
271#endif
272}
273else
274{
275struct segment_command *segCmd;
276
277segCmd = (struct segment_command *)cmdBase;
278
279vmaddr = (segCmd->vmaddr & 0x3fffffff);
280vmsize = segCmd->vmsize;
281fileoff = segCmd->fileoff;
282fileaddr = (gBinaryAddress + segCmd->fileoff);
283filesize = segCmd->filesize;
284segname = (char *)segCmd->segname;
285
286#ifdef DEBUG
287printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, fileaddr: %x, filesize: %x, nsects: %d, flags: %x.\n",
288segCmd->segname, (unsigned)vmaddr, (unsigned)vmsize, segCmd->fileoff, (unsigned)fileaddr, (unsigned)filesize,
289(unsigned) segCmd->nsects, (unsigned)segCmd->flags);
290getchar();
291#endif
292}
293
294//===================================================
295
296if (vmsize == 0 || filesize == 0)
297{
298*load_addr = ~0;
299*load_size = 0;
300return 0;
301}
302
303if (! ((vmaddr >= KERNEL_ADDR && (vmaddr + vmsize) <= (KERNEL_ADDR + KERNEL_LEN)) ||
304 (vmaddr >= HIB_ADDR && (vmaddr + vmsize) <= (HIB_ADDR + HIB_LEN))))
305{
306stop("Kernel overflows available space");
307}
308
309if (vmsize && ((strncmp(segname, "__PRELINK_INFO", sizeof("__PRELINK_INFO")) == 0) || (strncmp(segname, "__PRELINK", sizeof("__PRELINK")) == 0)))
310{
311gHaveKernelCache = true;
312}
313
314// Copy from file load area.
315if (vmsize>0 && filesize > 0)
316{
317bcopy((char *)fileaddr, (char *)vmaddr, vmsize > filesize ? filesize : vmsize);
318}
319
320// Zero space at the end of the segment.
321if (vmsize > filesize)
322{
323bzero((char *)(vmaddr + filesize), vmsize - filesize);
324}
325
326*load_addr = vmaddr;
327*load_size = vmsize;
328
329return 0;
330}
331
332//==============================================================================
333
334static long DecodeUnixThread(long cmdBase, unsigned int *entry)
335{
336switch (archCpuType)
337{
338case CPU_TYPE_I386:
339{
340i386_thread_state_t *i386ThreadState;
341i386ThreadState = (i386_thread_state_t *) (cmdBase + sizeof(struct thread_command) + 8);
342
343*entry = i386ThreadState->eip;
344return 0;
345}
346
347case CPU_TYPE_X86_64:
348{
349x86_thread_state64_t *x86_64ThreadState;
350x86_64ThreadState = (x86_thread_state64_t *) (cmdBase + sizeof(struct thread_command) + 8);
351*entry = x86_64ThreadState->rip;
352return 0;
353}
354
355default:
356error("Unknown CPU type\n");
357return -1;
358}
359}
360
361
362//==============================================================================
363
364static long DecodeSymbolTable(long cmdBase)
365{
366long tmpAddr, symsSize, totalSize;
367long gSymbolTableAddr;
368long gSymbolTableSize;
369
370struct symtab_command *symTab, *symTableSave;
371
372symTab = (struct symtab_command *)cmdBase;
373
374#if DEBUG
375printf("symoff: %x, nsyms: %x, stroff: %x, strsize: %x\n",
376symTab->symoff, symTab->nsyms, symTab->stroff, symTab->strsize);
377getchar();
378#endif
379
380symsSize = symTab->stroff - symTab->symoff;
381totalSize = symsSize + symTab->strsize;
382
383gSymbolTableSize = totalSize + sizeof(struct symtab_command);
384gSymbolTableAddr = AllocateKernelMemory(gSymbolTableSize);
385// Add the SymTab to the memory-map.
386AllocateMemoryRange("Kernel-__SYMTAB", gSymbolTableAddr, gSymbolTableSize, -1);
387
388symTableSave = (struct symtab_command *)gSymbolTableAddr;
389tmpAddr = gSymbolTableAddr + sizeof(struct symtab_command);
390
391symTableSave->symoff = tmpAddr;
392symTableSave->nsyms = symTab->nsyms;
393symTableSave->stroff = tmpAddr + symsSize;
394symTableSave->strsize = symTab->strsize;
395
396bcopy((char *)(gBinaryAddress + symTab->symoff), (char *)tmpAddr, totalSize);
397
398return 0;
399}
400

Archive Download this file

Revision: 2865