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} else if (fhp->magic == FAT_CIGAM)/* 0xbebafeca */
67{
68nfat = OSSwapInt32(fhp->nfat_arch);
69swapped = 1;
70} else {
71return -1;
72}
73
74for (; nfat > 0; nfat--, fap++)
75{
76if (swapped)
77{
78fapcputype = OSSwapInt32(fap->cputype);
79fapoffset = OSSwapInt32(fap->offset);
80fapsize = OSSwapInt32(fap->size);
81} else {
82fapcputype = fap->cputype;
83fapoffset = fap->offset;
84fapsize = fap->size;
85}
86
87if (fapcputype == archCpuType)
88{
89*binary = (void *) ((unsigned long)*binary + fapoffset);
90size = fapsize;
91break;
92}
93}
94
95if (length != 0)
96{
97*length = size;
98}
99
100return 0;
101}
102
103
104//==============================================================================
105
106long DecodeMachO(void *binary, entry_t *rentry, char **raddr, int *rsize)
107{
108struct mach_header *mH;
109unsigned long ncmds, cmdBase, cmd, cmdsize, cmdstart;
110// long headerBase, headerAddr, headerSize;
111unsigned int vmaddr = ~0;
112unsigned int vmend = 0;
113unsigned long cnt;
114long ret = -1;
115unsigned int entry = 0;
116
117gBinaryAddress = (unsigned long)binary;
118
119mH = (struct mach_header *)(gBinaryAddress);
120
121/*#if DEBUG
122DBG("magic: 0x%x\n", (unsigned)mH->magic);
123DBG("cputype: 0x%x\n", (unsigned)mH->cputype);
124DBG("cpusubtype: 0x%x\n", (unsigned)mH->cpusubtype);
125DBG("filetype: 0x%x\n", (unsigned)mH->filetype);
126DBG("ncmds: 0x%x\n", (unsigned)mH->ncmds);
127DBG("sizeofcmds: 0x%x\n", (unsigned)mH->sizeofcmds);
128DBG("flags: 0x%x\n", (unsigned)mH->flags);
129DBG("archCpuType: 0x%x\n", archCpuType);
130//getchar();
131#endif*/
132
133switch (archCpuType)
134{
135case CPU_TYPE_I386:
136
137if (mH->magic != MH_MAGIC)
138{
139error("Mach-O (i386) file has bad magic number\n");
140return -1;
141}
142
143cmdstart = (unsigned long)gBinaryAddress + sizeof(struct mach_header);
144break;
145
146case CPU_TYPE_X86_64:
147/*
148if (mH->magic != MH_MAGIC_64 && mH->magic == MH_MAGIC)
149{
150return -1;
151}
152*/
153if (mH->magic != MH_MAGIC_64)
154{
155error("Mach-O file (x86_64) has bad magic number\n");
156return -1;
157}
158
159cmdstart = (unsigned long)gBinaryAddress + sizeof(struct mach_header_64);
160break;
161
162default:
163
164error("Unknown CPU type\n");
165return -1;
166}
167
168cmdBase = cmdstart;
169ncmds = mH->ncmds;
170
171for (cnt = 0; cnt < ncmds; cnt++)
172{
173cmd = ((long *)cmdBase)[0];
174cmdsize = ((long *)cmdBase)[1];
175unsigned int load_addr;
176unsigned int load_size;
177
178switch (cmd)
179{
180case LC_SEGMENT_64:
181case LC_SEGMENT:
182ret = DecodeSegment(cmdBase, &load_addr, &load_size);
183
184if (ret == 0 && load_size != 0 && load_addr >= KERNEL_ADDR)
185{
186vmaddr = MIN(vmaddr, load_addr);
187vmend = MAX(vmend, load_addr + load_size);
188}
189break;
190
191case LC_UNIXTHREAD:
192ret = DecodeUnixThread(cmdBase, &entry);
193break;
194
195case LC_SYMTAB:
196break;
197
198default:
199#if NOTDEF
200printf("Ignoring cmd type %d.\n", (unsigned)cmd);
201#endif
202break;
203}
204
205
206if (ret != 0)
207{
208return -1;
209}
210
211cmdBase += cmdsize;
212}
213
214*rentry = (entry_t)( (unsigned long) entry & 0x3fffffff );
215*rsize = vmend - vmaddr;
216*raddr = (char *)vmaddr;
217
218cmdBase = cmdstart;
219
220for (cnt = 0; cnt < ncmds; cnt++)
221{
222cmd = ((long *)cmdBase)[0];
223cmdsize = ((long *)cmdBase)[1];
224
225if (cmd == LC_SYMTAB)
226{
227if (DecodeSymbolTable(cmdBase) != 0)
228{
229return -1;
230}
231}
232
233cmdBase += cmdsize;
234}
235
236return ret;
237}
238
239
240//==============================================================================
241// Private function.
242
243
244static long DecodeSegment(long cmdBase, unsigned int *load_addr, unsigned int *load_size)
245{
246char *segname;
247long vmsize, filesize;
248unsigned long vmaddr, fileaddr;
249
250if (((long *)cmdBase)[0] == LC_SEGMENT_64)
251{
252struct segment_command_64 *segCmd;
253segCmd = (struct segment_command_64 *)cmdBase;
254vmaddr = (segCmd->vmaddr & 0x3fffffff);
255vmsize = segCmd->vmsize;
256fileaddr = (gBinaryAddress + segCmd->fileoff);
257filesize = segCmd->filesize;
258segname = segCmd->segname;
259
260#ifdef DEBUG
261 printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",
262 segCmd->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,
263 (unsigned) segCmd->nsects, (unsigned)segCmd->flags);
264 getchar();
265#endif
266} else {
267struct segment_command *segCmd;
268
269segCmd = (struct segment_command *)cmdBase;
270
271vmaddr = (segCmd->vmaddr & 0x3fffffff);
272vmsize = segCmd->vmsize;
273fileaddr = (gBinaryAddress + segCmd->fileoff);
274filesize = segCmd->filesize;
275segname = segCmd->segname;
276
277#ifdef DEBUG
278printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",
279segCmd->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize, (unsigned) segCmd->nsects, (unsigned)segCmd->flags);
280getchar();
281#endif
282}
283
284if (vmsize == 0 || filesize == 0)
285{
286*load_addr = ~0;
287*load_size = 0;
288return 0;
289}
290
291if (! ((vmaddr >= KERNEL_ADDR && (vmaddr + vmsize) <= (KERNEL_ADDR + KERNEL_LEN)) ||
292 (vmaddr >= HIB_ADDR && (vmaddr + vmsize) <= (HIB_ADDR + HIB_LEN)))) {
293stop("Kernel overflows available space");
294}
295
296if (vmsize && ((strcmp(segname, "__PRELINK_INFO") == 0) || (strcmp(segname, "__PRELINK") == 0))) {
297gHaveKernelCache = true;
298}
299
300// Copy from file load area.
301if (vmsize>0 && filesize > 0) {
302bcopy((char *)fileaddr, (char *)vmaddr, vmsize > filesize ? filesize : vmsize);
303}
304
305// Zero space at the end of the segment.
306if (vmsize > filesize) {
307bzero((char *)(vmaddr + filesize), vmsize - filesize);
308}
309
310*load_addr = vmaddr;
311*load_size = vmsize;
312
313return 0;
314}
315
316//==============================================================================
317
318static long DecodeUnixThread(long cmdBase, unsigned int *entry)
319{
320switch (archCpuType) {
321case CPU_TYPE_I386:
322{
323i386_thread_state_t *i386ThreadState;
324i386ThreadState = (i386_thread_state_t *) (cmdBase + sizeof(struct thread_command) + 8);
325
326*entry = i386ThreadState->eip;
327return 0;
328}
329
330case CPU_TYPE_X86_64:
331{
332x86_thread_state64_t *x86_64ThreadState;
333x86_64ThreadState = (x86_thread_state64_t *) (cmdBase + sizeof(struct thread_command) + 8);
334*entry = x86_64ThreadState->rip;
335return 0;
336}
337
338default:
339error("Unknown CPU type\n");
340return -1;
341}
342}
343
344
345//==============================================================================
346
347static long DecodeSymbolTable(long cmdBase)
348{
349long tmpAddr, symsSize, totalSize;
350long gSymbolTableAddr;
351long gSymbolTableSize;
352
353struct symtab_command *symTab, *symTableSave;
354
355symTab = (struct symtab_command *)cmdBase;
356
357#if DEBUG
358
359printf("symoff: %x, nsyms: %x, stroff: %x, strsize: %x\n", symTab->symoff, symTab->nsyms, symTab->stroff, symTab->strsize);
360getchar();
361#endif
362
363symsSize = symTab->stroff - symTab->symoff;
364totalSize = symsSize + symTab->strsize;
365
366gSymbolTableSize = totalSize + sizeof(struct symtab_command);
367gSymbolTableAddr = AllocateKernelMemory(gSymbolTableSize);
368// Add the SymTab to the memory-map.
369AllocateMemoryRange("Kernel-__SYMTAB", gSymbolTableAddr, gSymbolTableSize, -1);
370
371symTableSave = (struct symtab_command *)gSymbolTableAddr;
372tmpAddr = gSymbolTableAddr + sizeof(struct symtab_command);
373
374symTableSave->symoff = tmpAddr;
375symTableSave->nsyms = symTab->nsyms;
376symTableSave->stroff = tmpAddr + symsSize;
377symTableSave->strsize = symTab->strsize;
378
379bcopy((char *)(gBinaryAddress + symTab->symoff), (char *)tmpAddr, totalSize);
380
381return 0;
382}
383

Archive Download this file

Revision: 2476