Chameleon

Chameleon Svn Source Tree

Root/trunk/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
40static long DecodeSegment(long cmdBase, unsigned int*load_addr, unsigned int *load_size);
41static long DecodeUnixThread(long cmdBase, unsigned int *entry);
42static long DecodeSymbolTable(long cmdBase);
43
44
45static unsigned long gBinaryAddress;
46bool gHaveKernelCache;/* XXX aserebln: uninitialized? and only set to true, never to false */
47cpu_type_t archCpuType = CPU_TYPE_I386;
48
49
50//==============================================================================
51// Public function.
52
53long ThinFatFile(void **binary, unsigned long *length)
54{
55unsigned long nfat, swapped, size = 0;
56struct fat_header *fhp = (struct fat_header *)*binary;
57struct fat_arch *fap = (struct fat_arch *)((unsigned long)*binary + sizeof(struct fat_header));
58cpu_type_t fapcputype;
59uint32_t fapoffset;
60uint32_t fapsize;
61
62if (fhp->magic == FAT_MAGIC)/* 0xcafebabe */
63{
64nfat = fhp->nfat_arch;
65swapped = 0;
66}
67else if (fhp->magic == FAT_CIGAM)/* 0xbebafeca */
68{
69nfat = OSSwapInt32(fhp->nfat_arch);
70swapped = 1;
71}
72else
73{
74return -1;
75}
76
77for (; nfat > 0; nfat--, fap++)
78{
79if (swapped)
80{
81fapcputype = OSSwapInt32(fap->cputype);
82fapoffset = OSSwapInt32(fap->offset);
83fapsize = OSSwapInt32(fap->size);
84}
85else
86{
87fapcputype = fap->cputype;
88fapoffset = fap->offset;
89fapsize = fap->size;
90}
91
92if (fapcputype == archCpuType)
93{
94*binary = (void *) ((unsigned long)*binary + fapoffset);
95size = fapsize;
96break;
97}
98}
99
100if (length != 0)
101{
102*length = size;
103}
104
105return 0;
106}
107
108
109//==============================================================================
110
111long DecodeMachO(void *binary, entry_t *rentry, char **raddr, int *rsize)
112{
113struct mach_header *mH;
114unsigned long ncmds, cmdBase, cmd, cmdsize, cmdstart;
115// long headerBase, headerAddr, headerSize;
116unsigned int vmaddr = ~0;
117unsigned int vmend = 0;
118unsigned long cnt;
119long ret = -1;
120unsigned int entry = 0;
121
122gBinaryAddress = (unsigned long)binary;
123
124mH = (struct mach_header *)(gBinaryAddress);
125
126/*#if DEBUG
127DBG("magic: 0x%x\n", (unsigned)mH->magic);
128DBG("cputype: 0x%x\n", (unsigned)mH->cputype);
129DBG("cpusubtype: 0x%x\n", (unsigned)mH->cpusubtype);
130DBG("filetype: 0x%x\n", (unsigned)mH->filetype);
131DBG("ncmds: 0x%x\n", (unsigned)mH->ncmds);
132DBG("sizeofcmds: 0x%x\n", (unsigned)mH->sizeofcmds);
133DBG("flags: 0x%x\n", (unsigned)mH->flags);
134DBG("archCpuType: 0x%x\n", archCpuType);
135//getchar();
136#endif*/
137
138switch (archCpuType)
139{
140case CPU_TYPE_I386:
141
142if (mH->magic != MH_MAGIC)
143{
144error("Mach-O (i386) file has bad magic number\n");
145return -1;
146}
147
148cmdstart = (unsigned long)gBinaryAddress + sizeof(struct mach_header);
149break;
150
151case CPU_TYPE_X86_64:
152/*
153if (mH->magic != MH_MAGIC_64 && mH->magic == MH_MAGIC)
154{
155return -1;
156}
157*/
158if (mH->magic != MH_MAGIC_64)
159{
160error("Mach-O file (x86_64) has bad magic number\n");
161return -1;
162}
163
164cmdstart = (unsigned long)gBinaryAddress + sizeof(struct mach_header_64);
165break;
166
167default:
168
169error("Unknown CPU type\n");
170return -1;
171}
172
173cmdBase = cmdstart;
174ncmds = mH->ncmds;
175
176for (cnt = 0; cnt < ncmds; cnt++)
177{
178cmd = ((long *)cmdBase)[0];
179cmdsize = ((long *)cmdBase)[1];
180unsigned int load_addr;
181unsigned int load_size;
182
183switch (cmd)
184{
185case LC_SEGMENT:
186case LC_SEGMENT_64:
187ret = DecodeSegment(cmdBase, &load_addr, &load_size);
188
189if (ret == 0 && load_size != 0 && load_addr >= KERNEL_ADDR)
190{
191vmaddr = MIN(vmaddr, load_addr);
192vmend = MAX(vmend, load_addr + load_size);
193}
194break;
195
196case LC_MAIN:/* Mountain Lion's replacement for LC_UNIXTHREAD */
197case LC_UNIXTHREAD:
198ret = DecodeUnixThread(cmdBase, &entry);
199break;
200
201case LC_SYMTAB:
202break;
203
204default:
205#if NOTDEF
206printf("Ignoring cmd type %d.\n", (unsigned)cmd);
207#endif
208break;
209}
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
249
250static long DecodeSegment(long cmdBase, unsigned int *load_addr, unsigned int *load_size)
251{
252char *segname;
253long vmsize, filesize;
254unsigned long vmaddr, fileaddr;
255
256if (((long *)cmdBase)[0] == LC_SEGMENT_64)
257{
258struct segment_command_64 *segCmd;
259segCmd = (struct segment_command_64 *)cmdBase;
260vmaddr = (segCmd->vmaddr & 0x3fffffff);
261vmsize = segCmd->vmsize;
262fileaddr = (gBinaryAddress + segCmd->fileoff);
263filesize = segCmd->filesize;
264segname = segCmd->segname;
265
266#ifdef DEBUG
267printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",
268segCmd->segname, (unsigned)vmaddr, (unsigned)vmsize, (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;
281fileaddr = (gBinaryAddress + segCmd->fileoff);
282filesize = segCmd->filesize;
283segname = segCmd->segname;
284
285#ifdef DEBUG
286printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",
287segCmd->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize, (unsigned) segCmd->nsects, (unsigned)segCmd->flags);
288getchar();
289#endif
290}
291
292//===================================================
293
294if (vmsize == 0 || filesize == 0)
295{
296*load_addr = ~0;
297*load_size = 0;
298return 0;
299}
300
301if (! ((vmaddr >= KERNEL_ADDR && (vmaddr + vmsize) <= (KERNEL_ADDR + KERNEL_LEN)) ||
302 (vmaddr >= HIB_ADDR && (vmaddr + vmsize) <= (HIB_ADDR + HIB_LEN))))
303{
304stop("Kernel overflows available space");
305}
306
307if (vmsize && ((strncmp(segname, "__PRELINK_INFO", sizeof("__PRELINK_INFO")) == 0) || (strncmp(segname, "__PRELINK", sizeof("__PRELINK")) == 0)))
308{
309gHaveKernelCache = true;
310}
311
312// Copy from file load area.
313if (vmsize>0 && filesize > 0)
314{
315bcopy((char *)fileaddr, (char *)vmaddr, vmsize > filesize ? filesize : vmsize);
316}
317
318// Zero space at the end of the segment.
319if (vmsize > filesize)
320{
321bzero((char *)(vmaddr + filesize), vmsize - filesize);
322}
323
324*load_addr = vmaddr;
325*load_size = vmsize;
326
327return 0;
328}
329
330//==============================================================================
331
332static long DecodeUnixThread(long cmdBase, unsigned int *entry)
333{
334switch (archCpuType)
335{
336case CPU_TYPE_I386:
337{
338i386_thread_state_t *i386ThreadState;
339i386ThreadState = (i386_thread_state_t *) (cmdBase + sizeof(struct thread_command) + 8);
340
341*entry = i386ThreadState->eip;
342return 0;
343}
344
345case CPU_TYPE_X86_64:
346{
347x86_thread_state64_t *x86_64ThreadState;
348x86_64ThreadState = (x86_thread_state64_t *) (cmdBase + sizeof(struct thread_command) + 8);
349*entry = x86_64ThreadState->rip;
350return 0;
351}
352
353default:
354error("Unknown CPU type\n");
355return -1;
356}
357}
358
359
360//==============================================================================
361
362static long DecodeSymbolTable(long cmdBase)
363{
364long tmpAddr, symsSize, totalSize;
365long gSymbolTableAddr;
366long gSymbolTableSize;
367
368struct symtab_command *symTab, *symTableSave;
369
370symTab = (struct symtab_command *)cmdBase;
371
372#if DEBUG
373printf("symoff: %x, nsyms: %x, stroff: %x, strsize: %x\n",
374symTab->symoff, symTab->nsyms, symTab->stroff, symTab->strsize);
375getchar();
376#endif
377
378symsSize = symTab->stroff - symTab->symoff;
379totalSize = symsSize + symTab->strsize;
380
381gSymbolTableSize = totalSize + sizeof(struct symtab_command);
382gSymbolTableAddr = AllocateKernelMemory(gSymbolTableSize);
383// Add the SymTab to the memory-map.
384AllocateMemoryRange("Kernel-__SYMTAB", gSymbolTableAddr, gSymbolTableSize, -1);
385
386symTableSave = (struct symtab_command *)gSymbolTableAddr;
387tmpAddr = gSymbolTableAddr + sizeof(struct symtab_command);
388
389symTableSave->symoff = tmpAddr;
390symTableSave->nsyms = symTab->nsyms;
391symTableSave->stroff = tmpAddr + symsSize;
392symTableSave->strsize = symTab->strsize;
393
394bcopy((char *)(gBinaryAddress + symTab->symoff), (char *)tmpAddr, totalSize);
395
396return 0;
397}
398

Archive Download this file

Revision: 2759