Root/
Source at commit 401 created 13 years 10 months ago. By azimutz, Same as rev 400. | |
---|---|
1 | /*␊ |
2 | * Copyright 2010 Evan Lojewski. All rights reserved.␊ |
3 | *␊ |
4 | */␊ |
5 | ␊ |
6 | #include "boot.h"␊ |
7 | #include "bootstruct.h"␊ |
8 | #include "multiboot.h"␊ |
9 | #include "modules.h"␊ |
10 | ␊ |
11 | ␊ |
12 | moduleList_t* loadedModules = NULL;␊ |
13 | symbolList_t* moduleSymbols = NULL;␊ |
14 | unsigned int (*lookup_symbol)(const char*) = NULL;␊ |
15 | ␊ |
16 | ␊ |
17 | void rebase_macho(void* base, char* rebase_stream, UInt32 size);␊ |
18 | void bind_macho(void* base, char* bind_stream, UInt32 size);␊ |
19 | ␊ |
20 | ␊ |
21 | /*␊ |
22 | * Initialize the module system by loading the Symbols.dylib module.␊ |
23 | * Once laoded, locate the _lookup_symbol function so that internal␊ |
24 | * symbols can be resolved.␊ |
25 | */␊ |
26 | int init_module_system()␊ |
27 | {␊ |
28 | ␉// Intialize module system␊ |
29 | ␉if(load_module(SYMBOLS_MODULE))␊ |
30 | ␉{␊ |
31 | ␉␉lookup_symbol = (void*)lookup_all_symbols("_lookup_symbol");␊ |
32 | ␉}␊ |
33 | ␉␊ |
34 | ␉if((UInt32)lookup_symbol == 0xFFFFFFFF)␊ |
35 | ␉{␊ |
36 | ␉␉return 0;␊ |
37 | ␉}␊ |
38 | ␉else␊ |
39 | ␉{␊ |
40 | ␉␉return 1;␊ |
41 | ␉}␊ |
42 | }␊ |
43 | ␊ |
44 | /*␊ |
45 | * Load all modules in the /Extra/modules/ directory␊ |
46 | * Module depencdies will be loaded first␊ |
47 | * MOdules will only be loaded once. When loaded a module must␊ |
48 | * setup apropriete function calls and hooks as required.␊ |
49 | * NOTE: To ensure a module loads after another you may ␊ |
50 | * link one module with the other. For dyld to allow this, you must␊ |
51 | * reference at least one symbol within the module.␊ |
52 | */␊ |
53 | void load_all_modules()␊ |
54 | {␊ |
55 | ␉char* name;␊ |
56 | ␉long flags;␊ |
57 | ␉long time;␊ |
58 | ␉struct dirstuff* moduleDir = opendir("/Extra/modules/");␊ |
59 | ␉while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)␊ |
60 | ␉{␊ |
61 | ␉␉// TODO: verify ends in .dylib␊ |
62 | ␉␉if(strlen(name) > sizeof(".dylib"))␊ |
63 | ␉␉{␊ |
64 | ␉␉␉name[strlen(name) - sizeof(".dylib")] = 0;␊ |
65 | ␉␉␉load_module(name);␊ |
66 | ␉␉}␊ |
67 | ␉}␊ |
68 | }␊ |
69 | ␊ |
70 | ␊ |
71 | /*␊ |
72 | * Load a module file in /Extra/modules␊ |
73 | * TODO: verify version number of module␊ |
74 | */␊ |
75 | int load_module(const char* module)␊ |
76 | {␊ |
77 | ␉// Check to see if the module has already been loaded␊ |
78 | ␉if(is_module_laoded(module))␊ |
79 | ␉{␊ |
80 | ␉␉return 1;␊ |
81 | ␉}␊ |
82 | ␉␊ |
83 | ␉char modString[128];␊ |
84 | ␉int fh = -1;␊ |
85 | ␉sprintf(modString, "/Extra/modules/%s.dylib", module);␊ |
86 | ␉fh = open(modString, 0);␊ |
87 | ␉if(fh < 0)␊ |
88 | ␉{␊ |
89 | ␉␉printf("Unable to locate module %s\n", modString);␊ |
90 | ␉␉getc();␊ |
91 | ␉␉return 0;␊ |
92 | ␉}␊ |
93 | ␉␊ |
94 | ␉unsigned int moduleSize = file_size(fh);␊ |
95 | ␉char* module_base = (char*) malloc(moduleSize);␊ |
96 | ␉if (read(fh, module_base, moduleSize) == moduleSize)␊ |
97 | ␉{␊ |
98 | ␉␉void (*module_start)(void) = NULL;␊ |
99 | ␊ |
100 | ␉␉printf("Module %s read in.\n", modString);␊ |
101 | ␊ |
102 | ␉␉// Module loaded into memory, parse it␊ |
103 | ␉␉module_start = parse_mach(module_base);␊ |
104 | ␊ |
105 | ␉␉if(module_start)␊ |
106 | ␉␉{␊ |
107 | ␉␉␉(*module_start)();␉// Start the module␊ |
108 | ␉␉␉printf("Module started\n");␊ |
109 | ␉␉}␊ |
110 | ␉␉else {␊ |
111 | ␉␉␉printf("Unabel to locate module start\n");␊ |
112 | ␉␉}␊ |
113 | ␊ |
114 | ␉␉␊ |
115 | ␉␉getc();␊ |
116 | ␉␉␊ |
117 | ␉␉// TODO: Add module to loaded list if loaded successfuly␊ |
118 | ␉␉␊ |
119 | ␉}␊ |
120 | ␉else␊ |
121 | ␉{␊ |
122 | ␉␉printf("Unable to read in module %s.dylib\n.", module);␊ |
123 | ␉␉getc();␊ |
124 | ␉}␊ |
125 | ␉return 1;␊ |
126 | }␊ |
127 | ␊ |
128 | /*␊ |
129 | * Parse through a macho module. The module will be rebased and binded␊ |
130 | * as specified in the macho header. If the module is sucessfuly laoded␊ |
131 | * the module iinit address will be returned.␊ |
132 | * NOTE; all dependecies will be loaded before this module is started␊ |
133 | * NOTE: If the module is unable to load ot completeion, the modules␊ |
134 | * symbols will still be available (TODO: fix this). This should not␊ |
135 | * happen as all dependencies are verified before the sybols are read in.␊ |
136 | */␊ |
137 | void* parse_mach(void* binary)␊ |
138 | {␉␊ |
139 | ␉void (*module_start)(void) = NULL;␊ |
140 | ␊ |
141 | ␉// Module info␊ |
142 | ␉char* moduleName = NULL;␊ |
143 | ␉UInt32 moduleVersion = 0;␊ |
144 | ␉UInt32 moduleCompat = 0;␊ |
145 | ␉␊ |
146 | ␉char* symbolStub = NULL;␊ |
147 | ␉char* nonlazy = NULL;␊ |
148 | ␉//char* nonlazy_variables = NULL;␊ |
149 | ␊ |
150 | ␉// TODO convert all of the structs to a union␊ |
151 | ␉struct load_command *loadCommand = NULL;␊ |
152 | ␉struct dylib_command* dylibCommand = NULL;␊ |
153 | ␉struct dyld_info_command* dyldInfoCommand = NULL;␊ |
154 | ␉struct symtab_command* symtabCommand = NULL;␊ |
155 | ␉struct segment_command* segmentCommand = NULL;␊ |
156 | ␉//struct dysymtab_command* dysymtabCommand = NULL;␊ |
157 | ␉struct section* section = NULL;␊ |
158 | ␉UInt32 binaryIndex = sizeof(struct mach_header);␊ |
159 | ␉UInt16 cmd = 0;␊ |
160 | ␊ |
161 | ␉// Parse through the load commands␊ |
162 | ␉if(((struct mach_header*)binary)->magic != MH_MAGIC)␊ |
163 | ␉{␊ |
164 | ␉␉printf("Module is not 32bit\n");␊ |
165 | ␉␉return NULL;␉// 32bit only␊ |
166 | ␉}␊ |
167 | ␉␊ |
168 | ␉if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
169 | ␉{␊ |
170 | ␉␉printf("Module is not a dylib. Unable to load.\n");␊ |
171 | ␉␉return NULL; // Module is in the incorrect format␊ |
172 | ␉}␊ |
173 | ␉␊ |
174 | ␉while(cmd < ((struct mach_header*)binary)->ncmds)␉// TODO: for loop instead␊ |
175 | ␉{␊ |
176 | ␉␉cmd++;␊ |
177 | ␉␉␊ |
178 | ␉␉loadCommand = binary + binaryIndex;␊ |
179 | ␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
180 | ␊ |
181 | ␉␉␊ |
182 | ␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
183 | ␉␉{␊ |
184 | ␉␉␉case LC_SYMTAB:␊ |
185 | ␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
186 | ␉␉␉␉break;␊ |
187 | ␉␉␉case LC_SEGMENT:␊ |
188 | ␉␉␉␉segmentCommand = binary + binaryIndex;␊ |
189 | ␊ |
190 | ␉␉␉␉UInt32 sections = segmentCommand->nsects;␊ |
191 | ␉␉␉␉section = binary + binaryIndex + sizeof(struct segment_command);␊ |
192 | ␉␉␉␉␊ |
193 | ␉␉␉␉// Loop untill needed variables are found␊ |
194 | ␉␉␉␉while(!(nonlazy && symbolStub) && sections)␉␊ |
195 | ␉␉␉␉{␊ |
196 | ␉␉␉␉␉// Look for some sections and save the addresses␊ |
197 | ␉␉␉␉␉if(strcmp(section->sectname, SECT_NON_LAZY_SYMBOL_PTR) == 0)␊ |
198 | ␉␉␉␉␉{␊ |
199 | ␉␉␉␉␉␉/*printf("\tSection non lazy pointers at 0x%X, %d symbols\n",␊ |
200 | ␉␉␉␉␉␉␉ section->offset, ␊ |
201 | ␉␉␉␉␉␉␉ section->size / 4);␊ |
202 | ␉␉␉␉␉␉*/␊ |
203 | ␉␉␉␉␉␉switch(section->flags)␊ |
204 | ␉␉␉␉␉␉{␊ |
205 | ␉␉␉␉␉␉␉case S_NON_LAZY_SYMBOL_POINTERS:␊ |
206 | ␉␉␉␉␉␉␉␉//printf("%s S_NON_LAZY_SYMBOL_POINTERS section\n", SECT_NON_LAZY_SYMBOL_PTR);␊ |
207 | ␊ |
208 | ␉␉␉␉␉␉␉␉//nonlazy_variables = binary + section->offset;␊ |
209 | ␉␉␉␉␉␉␉␉nonlazy = binary + section->offset;␊ |
210 | ␉␉␉␉␉␉␉␉break;␊ |
211 | ␉␉␉␉␉␉␉␉␊ |
212 | ␉␉␉␉␉␉␉case S_LAZY_SYMBOL_POINTERS:␊ |
213 | ␉␉␉␉␉␉␉␉//nonlazy = binary + section->offset;␊ |
214 | ␉␉␉␉␉␉␉␉//printf("%s S_LAZY_SYMBOL_POINTERS section, 0x%X\n", SECT_NON_LAZY_SYMBOL_PTR, nonlazy);␊ |
215 | ␉␉␉␉␉␉␉␉// Fucntions␊ |
216 | ␉␉␉␉␉␉␉␉break;␊ |
217 | ␉␉␉␉␉␉␉␉␊ |
218 | ␉␉␉␉␉␉␉default:␊ |
219 | ␉␉␉␉␉␉␉␉//printf("Unhandled %s section\n", SECT_NON_LAZY_SYMBOL_PTR);␊ |
220 | ␉␉␉␉␉␉␉␉//getc();␊ |
221 | ␉␉␉␉␉␉␉␉break;␊ |
222 | ␉␉␉␉␉␉}␊ |
223 | ␉␉␉␉␉␉//getc();␊ |
224 | ␉␉␉␉␉}␊ |
225 | ␉␉␉␉␉else if(strcmp(section->sectname, SECT_SYMBOL_STUBS) == 0)␊ |
226 | ␉␉␉␉␉{␊ |
227 | ␉␉␉␉␉␉symbolStub = binary + section->offset;␊ |
228 | ␉␉␉␉␉}␊ |
229 | ␉␉␉␉␉/*␊ |
230 | ␉␉␉␉␉else ␊ |
231 | ␉␉␉␉␉{␊ |
232 | ␉␉␉␉␉␉printf("Unhandled section %s\n", section->sectname);␊ |
233 | ␉␉␉␉␉}␊ |
234 | ␉␉␉␉␉*/␊ |
235 | ␉␉␉␉␉sections--;␊ |
236 | ␉␉␉␉␉section++;␊ |
237 | ␉␉␉␉}␊ |
238 | ␉␉␉␉␉␉ ␊ |
239 | ␉␉␉␉␊ |
240 | ␉␉␉␉break;␊ |
241 | ␉␉␉␉␊ |
242 | ␉␉␉case LC_DYSYMTAB:␊ |
243 | ␉␉␉␉//dysymtabCommand = binary + binaryIndex;␊ |
244 | ␉␉␉␉//printf("Unhandled loadcommand LC_DYSYMTAB\n");␊ |
245 | ␉␉␉␉break;␊ |
246 | ␉␉␉␉␊ |
247 | ␉␉␉case LC_LOAD_DYLIB:␊ |
248 | ␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
249 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
250 | ␉␉␉␉char* module = binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
251 | ␉␉␉␉// TODO: verify version␊ |
252 | ␉␉␉␉// =␉dylibCommand->dylib.current_version;␊ |
253 | ␉␉␉␉// =␉dylibCommand->dylib.compatibility_version;␊ |
254 | ␊ |
255 | ␉␉␉␉if(!load_module(module))␊ |
256 | ␉␉␉␉{␊ |
257 | ␉␉␉␉␉// Unable to load dependancy␊ |
258 | ␉␉␉␉␉return NULL;␊ |
259 | ␉␉␉␉}␊ |
260 | ␉␉␉␉break;␊ |
261 | ␉␉␉␉␊ |
262 | ␉␉␉case LC_ID_DYLIB:␊ |
263 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
264 | ␉␉␉␉moduleName =␉binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
265 | ␉␉␉␉moduleVersion =␉dylibCommand->dylib.current_version;␊ |
266 | ␉␉␉␉moduleCompat =␉dylibCommand->dylib.compatibility_version;␊ |
267 | ␉␉␉␉break;␊ |
268 | ␊ |
269 | ␉␉␉case LC_DYLD_INFO:␊ |
270 | ␉␉␉␉// Bind and rebase info is stored here␊ |
271 | ␉␉␉␉dyldInfoCommand = binary + binaryIndex;␊ |
272 | ␉␉␉␉break;␊ |
273 | ␉␉␉␉␊ |
274 | ␉␉␉case LC_UUID:␊ |
275 | ␉␉␉␉break;␊ |
276 | ␉␉␉␉␊ |
277 | ␉␉␉default:␊ |
278 | ␉␉␉␉printf("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);␊ |
279 | ␉␉␉␉break;␊ |
280 | ␉␉␊ |
281 | ␉␉}␊ |
282 | ␊ |
283 | ␉␉binaryIndex += cmdSize;␊ |
284 | ␉}␊ |
285 | ␉if(!moduleName) return NULL;␊ |
286 | ␉␉␊ |
287 | ␊ |
288 | ␉// bind_macho uses the symbols added in for some reason...␊ |
289 | ␉module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, symbolStub, (UInt32)nonlazy);␊ |
290 | ␊ |
291 | ␉// REbase the module before binding it.␊ |
292 | ␉if(dyldInfoCommand && dyldInfoCommand->rebase_off)␊ |
293 | ␉{␊ |
294 | ␉␉rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);␊ |
295 | ␉}␊ |
296 | ␉␊ |
297 | ␉if(dyldInfoCommand && dyldInfoCommand->bind_off)␊ |
298 | ␉{␊ |
299 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->bind_off, dyldInfoCommand->bind_size);␊ |
300 | ␉}␊ |
301 | ␉␊ |
302 | ␉if(dyldInfoCommand && dyldInfoCommand->weak_bind_off)␊ |
303 | ␉{␊ |
304 | ␉␉// NOTE: this currently should never happen.␊ |
305 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->weak_bind_off, dyldInfoCommand->weak_bind_size);␊ |
306 | ␉}␊ |
307 | ␉␊ |
308 | ␉if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off)␊ |
309 | ␉{␊ |
310 | ␉␉// NOTE: we are binding the lazy pointers as a module is laoded,␊ |
311 | ␉␉// This should be cahnged to bund when a symbol is referened at runtime.␊ |
312 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);␊ |
313 | ␉}␊ |
314 | ␉␊ |
315 | ␊ |
316 | ␉␊ |
317 | ␊ |
318 | ␉// Notify the system that it was laoded␊ |
319 | ␉module_loaded(moduleName, moduleVersion, moduleCompat);␊ |
320 | ␉␊ |
321 | ␉return module_start;␊ |
322 | ␉␊ |
323 | }␊ |
324 | ␊ |
325 | ␊ |
326 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
327 | void rebase_macho(void* base, char* rebase_stream, UInt32 size)␊ |
328 | {␊ |
329 | ␉rebase_stream += (UInt32)base;␊ |
330 | ␉␊ |
331 | ␉UInt8 immediate = 0;␊ |
332 | ␉UInt8 opcode = 0;␊ |
333 | ␉UInt8 type = 0;␊ |
334 | ␉␊ |
335 | ␉UInt32 segmentAddress = 0;␊ |
336 | ␉␊ |
337 | ␉␊ |
338 | ␉␊ |
339 | ␉UInt32 tmp = 0;␊ |
340 | ␉UInt32 tmp2 = 0;␊ |
341 | ␉UInt8 bits = 0;␊ |
342 | ␉int index = 0;␊ |
343 | ␉␊ |
344 | ␉int done = 0;␊ |
345 | ␉unsigned int i = 0;␊ |
346 | ␉␊ |
347 | ␉while(/*!done &&*/ i < size)␊ |
348 | ␉{␊ |
349 | ␉␉immediate = rebase_stream[i] & REBASE_IMMEDIATE_MASK;␊ |
350 | ␉␉opcode = rebase_stream[i] & REBASE_OPCODE_MASK;␊ |
351 | ␊ |
352 | ␉␉␊ |
353 | ␉␉switch(opcode)␊ |
354 | ␉␉{␊ |
355 | ␉␉␉case REBASE_OPCODE_DONE:␊ |
356 | ␉␉␉␉// Rebase complete.␊ |
357 | ␉␉␉␉done = 1;␊ |
358 | ␉␉␉␉break;␊ |
359 | ␉␉␉␉␊ |
360 | ␉␉␉␉␊ |
361 | ␉␉␉case REBASE_OPCODE_SET_TYPE_IMM:␊ |
362 | ␉␉␉␉// Set rebase type (pointer, absolute32, pcrel32)␊ |
363 | ␉␉␉␉//printf("Rebase type = 0x%X\n", immediate);␊ |
364 | ␉␉␉␉// NOTE: This is currently NOT used␊ |
365 | ␉␉␉␉type = immediate;␊ |
366 | ␉␉␉␉break;␊ |
367 | ␉␉␉␉␊ |
368 | ␉␉␉␉␊ |
369 | ␉␉␉case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
370 | ␉␉␉␉// Locate address to begin rebasing␊ |
371 | ␉␉␉␉segmentAddress = 0;␊ |
372 | ␊ |
373 | ␉␉␉␉struct segment_command* segCommand = NULL;␊ |
374 | ␉␉␉␉␊ |
375 | ␉␉␉␉unsigned int binIndex = 0;␊ |
376 | ␉␉␉␉index = 0;␊ |
377 | ␉␉␉␉do␊ |
378 | ␉␉␉␉{␊ |
379 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
380 | ␉␉␉␉␉␊ |
381 | ␉␉␉␉␉␊ |
382 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
383 | ␉␉␉␉␉index++;␊ |
384 | ␉␉␉␉}␊ |
385 | ␉␉␉␉while(index <= immediate);␊ |
386 | ␊ |
387 | ␊ |
388 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
389 | ␉␉␉␉␊ |
390 | ␉␉␉␉tmp = 0;␊ |
391 | ␉␉␉␉bits = 0;␊ |
392 | ␉␉␉␉do␊ |
393 | ␉␉␉␉{␊ |
394 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
395 | ␉␉␉␉␉bits += 7;␊ |
396 | ␉␉␉␉}␊ |
397 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
398 | ␉␉␉␉␊ |
399 | ␉␉␉␉segmentAddress += tmp;␊ |
400 | ␉␉␉␉break;␊ |
401 | ␉␉␉␉␊ |
402 | ␉␉␉␉␊ |
403 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_ULEB:␊ |
404 | ␉␉␉␉// Add value to rebase address␊ |
405 | ␉␉␉␉tmp = 0;␊ |
406 | ␉␉␉␉bits = 0;␊ |
407 | ␉␉␉␉do␊ |
408 | ␉␉␉␉{␊ |
409 | ␉␉␉␉␉tmp <<= bits;␊ |
410 | ␉␉␉␉␉tmp |= rebase_stream[++i] & 0x7f;␊ |
411 | ␉␉␉␉␉bits += 7;␊ |
412 | ␉␉␉␉}␊ |
413 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
414 | ␉␉␉␉␊ |
415 | ␉␉␉␉segmentAddress +=␉tmp; ␊ |
416 | ␉␉␉␉break;␊ |
417 | ␉␉␉␉␊ |
418 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:␊ |
419 | ␉␉␉␉segmentAddress += immediate * sizeof(void*);␊ |
420 | ␉␉␉␉break;␊ |
421 | ␉␉␉␉␊ |
422 | ␉␉␉␉␊ |
423 | ␉␉␉case REBASE_OPCODE_DO_REBASE_IMM_TIMES:␊ |
424 | ␉␉␉␉index = 0;␊ |
425 | ␉␉␉␉for (index = 0; index < immediate; ++index) {␊ |
426 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base);␊ |
427 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
428 | ␉␉␉␉}␊ |
429 | ␉␉␉␉break;␊ |
430 | ␉␉␉␊ |
431 | ␉␉␉␉␊ |
432 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:␊ |
433 | ␉␉␉␉tmp = 0;␊ |
434 | ␉␉␉␉bits = 0;␊ |
435 | ␉␉␉␉do␊ |
436 | ␉␉␉␉{␊ |
437 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
438 | ␉␉␉␉␉bits += 7;␊ |
439 | ␉␉␉␉}␊ |
440 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
441 | ␉␉␉␉␊ |
442 | ␉␉␉␉index = 0;␊ |
443 | ␉␉␉␉for (index = 0; index < tmp; ++index) {␊ |
444 | ␉␉␉␉␉//printf("\tRebasing 0x%X\n", segmentAddress);␊ |
445 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base);␉␉␉␉␉␊ |
446 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
447 | ␉␉␉␉}␊ |
448 | ␉␉␉␉break;␊ |
449 | ␉␉␉␉␊ |
450 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:␊ |
451 | ␉␉␉␉tmp = 0;␊ |
452 | ␉␉␉␉bits = 0;␊ |
453 | ␉␉␉␉do␊ |
454 | ␉␉␉␉{␊ |
455 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
456 | ␉␉␉␉␉bits += 7;␊ |
457 | ␉␉␉␉}␊ |
458 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
459 | ␉␉␉␉␊ |
460 | ␉␉␉␉rebase_location(base + segmentAddress, (char*)base);␊ |
461 | ␉␉␉␉␊ |
462 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
463 | ␉␉␉␉break;␊ |
464 | ␉␉␉␉␊ |
465 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:␊ |
466 | ␉␉␉␉tmp = 0;␊ |
467 | ␉␉␉␉bits = 0;␊ |
468 | ␉␉␉␉do␊ |
469 | ␉␉␉␉{␊ |
470 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
471 | ␉␉␉␉␉bits += 7;␊ |
472 | ␉␉␉␉}␊ |
473 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
474 | ␉␉␉␉␊ |
475 | ␉␉␉␉␊ |
476 | ␉␉␉␉tmp2 = 0;␊ |
477 | ␉␉␉␉bits = 0;␊ |
478 | ␉␉␉␉do␊ |
479 | ␉␉␉␉{␊ |
480 | ␉␉␉␉␉tmp2 |= (rebase_stream[++i] & 0x7f) << bits;␊ |
481 | ␉␉␉␉␉bits += 7;␊ |
482 | ␉␉␉␉}␊ |
483 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
484 | ␉␉␉␉␊ |
485 | ␉␉␉␉index = 0;␊ |
486 | ␉␉␉␉for (index = 0; index < tmp; ++index) {␊ |
487 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base);␊ |
488 | ␉␉␉␉␉␊ |
489 | ␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
490 | ␉␉␉␉}␊ |
491 | ␉␉␉␉break;␊ |
492 | ␉␉}␊ |
493 | ␉␉i++;␊ |
494 | ␉}␊ |
495 | }␊ |
496 | ␊ |
497 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
498 | // NOTE: this uses 32bit values, and not 64bit values. ␊ |
499 | // There is apossibility that this could cause issues,␊ |
500 | // however the macho file is 32 bit, so it shouldn't matter too much␊ |
501 | void bind_macho(void* base, char* bind_stream, UInt32 size)␊ |
502 | {␉␊ |
503 | ␉bind_stream += (UInt32)base;␊ |
504 | ␉␊ |
505 | ␉UInt8 immediate = 0;␊ |
506 | ␉UInt8 opcode = 0;␊ |
507 | ␉UInt8 type = 0;␊ |
508 | ␉␊ |
509 | ␉UInt32 segmentAddress = 0;␊ |
510 | ␉␊ |
511 | ␉UInt32 address = 0;␊ |
512 | ␉␊ |
513 | ␉SInt32 addend = 0;␉␉␉// TODO: handle this␊ |
514 | ␉SInt32 libraryOrdinal = 0;␊ |
515 | ␊ |
516 | ␉const char* symbolName = NULL;␊ |
517 | ␉UInt8 symboFlags = 0;␊ |
518 | ␉UInt32 symbolAddr = 0xFFFFFFFF;␊ |
519 | ␉␊ |
520 | ␉// Temperary variables␊ |
521 | ␉UInt8 bits = 0;␊ |
522 | ␉UInt32 tmp = 0;␊ |
523 | ␉UInt32 tmp2 = 0;␊ |
524 | ␉␊ |
525 | ␉UInt32 index = 0;␊ |
526 | ␉int done = 0;␊ |
527 | ␉unsigned int i = 0;␊ |
528 | ␉␊ |
529 | ␉while(/*!done &&*/ i < size)␊ |
530 | ␉{␊ |
531 | ␉␉immediate = bind_stream[i] & BIND_IMMEDIATE_MASK;␊ |
532 | ␉␉opcode = bind_stream[i] & BIND_OPCODE_MASK;␊ |
533 | ␉␉␊ |
534 | ␉␉␊ |
535 | ␉␉switch(opcode)␊ |
536 | ␉␉{␊ |
537 | ␉␉␉case BIND_OPCODE_DONE:␊ |
538 | ␉␉␉␉done = 1; ␊ |
539 | ␉␉␉␉break;␊ |
540 | ␉␉␉␉␊ |
541 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:␊ |
542 | ␉␉␉␉libraryOrdinal = immediate;␊ |
543 | ␉␉␉␉//printf("BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: %d\n", libraryOrdinal);␊ |
544 | ␉␉␉␉break;␊ |
545 | ␉␉␉␉␊ |
546 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:␊ |
547 | ␉␉␉␉libraryOrdinal = 0;␊ |
548 | ␉␉␉␉bits = 0;␊ |
549 | ␉␉␉␉do␊ |
550 | ␉␉␉␉{␊ |
551 | ␉␉␉␉␉libraryOrdinal |= (bind_stream[++i] & 0x7f) << bits;␊ |
552 | ␉␉␉␉␉bits += 7;␊ |
553 | ␉␉␉␉}␊ |
554 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
555 | ␉␉␉␉␊ |
556 | ␉␉␉␉//printf("BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: %d\n", libraryOrdinal);␊ |
557 | ␊ |
558 | ␉␉␉␉break;␊ |
559 | ␉␉␉␉␊ |
560 | ␉␉␉case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:␊ |
561 | ␉␉␉␉// NOTE: this is wrong, fortunately we don't use it␊ |
562 | ␉␉␉␉libraryOrdinal = -immediate;␊ |
563 | ␉␉␉␉//printf("BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: %d\n", libraryOrdinal);␊ |
564 | ␊ |
565 | ␉␉␉␉break;␊ |
566 | ␉␉␉␉␊ |
567 | ␉␉␉case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:␊ |
568 | ␉␉␉␉symboFlags = immediate;␊ |
569 | ␉␉␉␉symbolName = (char*)&bind_stream[++i];␊ |
570 | ␉␉␉␉i += strlen((char*)&bind_stream[i]);␊ |
571 | ␉␉␉␉//printf("BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: %s, 0x%X\n", symbolName, symboFlags);␊ |
572 | ␊ |
573 | ␉␉␉␉symbolAddr = lookup_all_symbols(symbolName);␊ |
574 | ␊ |
575 | ␉␉␉␉break;␊ |
576 | ␉␉␉␉␊ |
577 | ␉␉␉case BIND_OPCODE_SET_TYPE_IMM:␊ |
578 | ␉␉␉␉// Set bind type (pointer, absolute32, pcrel32)␊ |
579 | ␉␉␉␉type = immediate;␊ |
580 | ␉␉␉␉//printf("BIND_OPCODE_SET_TYPE_IMM: %d\n", type);␊ |
581 | ␊ |
582 | ␉␉␉␉break;␊ |
583 | ␉␉␉␉␊ |
584 | ␉␉␉case BIND_OPCODE_SET_ADDEND_SLEB:␊ |
585 | ␉␉␉␉addend = 0;␊ |
586 | ␉␉␉␉bits = 0;␊ |
587 | ␉␉␉␉do␊ |
588 | ␉␉␉␉{␊ |
589 | ␉␉␉␉␉addend |= (bind_stream[++i] & 0x7f) << bits;␊ |
590 | ␉␉␉␉␉bits += 7;␊ |
591 | ␉␉␉␉}␊ |
592 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
593 | ␉␉␉␉␊ |
594 | ␉␉␉␉if(!(bind_stream[i-1] & 0x40)) addend *= -1;␊ |
595 | ␉␉␉␉␊ |
596 | ␉␉␉␉//printf("BIND_OPCODE_SET_ADDEND_SLEB: %d\n", addend);␊ |
597 | ␉␉␉␉break;␊ |
598 | ␉␉␉␉␊ |
599 | ␉␉␉case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
600 | ␉␉␉␉segmentAddress = 0;␊ |
601 | ␊ |
602 | ␉␉␉␉// Locate address␊ |
603 | ␉␉␉␉struct segment_command* segCommand = NULL;␊ |
604 | ␉␉␉␉␊ |
605 | ␉␉␉␉unsigned int binIndex = 0;␊ |
606 | ␉␉␉␉index = 0;␊ |
607 | ␉␉␉␉do␊ |
608 | ␉␉␉␉{␊ |
609 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
610 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
611 | ␉␉␉␉␉index++;␊ |
612 | ␉␉␉␉}␊ |
613 | ␉␉␉␉while(index <= immediate);␊ |
614 | ␉␉␉␉␊ |
615 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
616 | ␉␉␉␉␊ |
617 | ␉␉␉␉// Read in offset␊ |
618 | ␉␉␉␉tmp = 0;␊ |
619 | ␉␉␉␉bits = 0;␊ |
620 | ␉␉␉␉do␊ |
621 | ␉␉␉␉{␊ |
622 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
623 | ␉␉␉␉␉bits += 7;␊ |
624 | ␉␉␉␉}␊ |
625 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
626 | ␉␉␉␉␊ |
627 | ␉␉␉␉segmentAddress += tmp;␊ |
628 | ␉␉␉␉␊ |
629 | ␉␉␉␉//printf("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: 0x%X\n", segmentAddress);␊ |
630 | ␉␉␉␉break;␊ |
631 | ␉␉␉␉␊ |
632 | ␉␉␉case BIND_OPCODE_ADD_ADDR_ULEB:␊ |
633 | ␉␉␉␉// Read in offset␊ |
634 | ␉␉␉␉tmp = 0;␊ |
635 | ␉␉␉␉bits = 0;␊ |
636 | ␉␉␉␉do␊ |
637 | ␉␉␉␉{␊ |
638 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
639 | ␉␉␉␉␉bits += 7;␊ |
640 | ␉␉␉␉}␊ |
641 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
642 | ␉␉␉␉␊ |
643 | ␉␉␉␉segmentAddress += tmp;␊ |
644 | ␉␉␉␉//printf("BIND_OPCODE_ADD_ADDR_ULEB: 0x%X\n", segmentAddress);␊ |
645 | ␉␉␉␉break;␊ |
646 | ␉␉␉␉␊ |
647 | ␉␉␉case BIND_OPCODE_DO_BIND:␊ |
648 | ␉␉␉␉//printf("BIND_OPCODE_DO_BIND\n");␊ |
649 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
650 | ␉␉␉␉{␊ |
651 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
652 | ␊ |
653 | ␉␉␉␉␉((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;␊ |
654 | ␉␉␉␉␉((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;␊ |
655 | ␉␉␉␉␉((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;␊ |
656 | ␉␉␉␉␉((char*)address)[3] = (symbolAddr & 0xFF000000) >> 24;␊ |
657 | ␉␉␉␉}␊ |
658 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
659 | ␉␉␉␉{␊ |
660 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
661 | ␉␉␉␉}␊ |
662 | ␉␉␉␉␊ |
663 | ␉␉␉␉segmentAddress += sizeof(void*);␊ |
664 | ␉␉␉␉break;␊ |
665 | ␉␉␉␉␊ |
666 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:␊ |
667 | ␉␉␉␉//printf("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB\n");␊ |
668 | ␉␉␉␉␊ |
669 | ␉␉␉␉␊ |
670 | ␉␉␉␉// Read in offset␊ |
671 | ␉␉␉␉tmp = 0;␊ |
672 | ␉␉␉␉bits = 0;␊ |
673 | ␉␉␉␉do␊ |
674 | ␉␉␉␉{␊ |
675 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
676 | ␉␉␉␉␉bits += 7;␊ |
677 | ␉␉␉␉}␊ |
678 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
679 | ␉␉␉␉␊ |
680 | ␉␉␉␉␊ |
681 | ␉␉␉␉␊ |
682 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
683 | ␉␉␉␉{␊ |
684 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
685 | ␉␉␉␉␉␊ |
686 | ␉␉␉␉␉((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;␊ |
687 | ␉␉␉␉␉((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;␊ |
688 | ␉␉␉␉␉((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;␊ |
689 | ␉␉␉␉␉((char*)address)[3] = (symbolAddr & 0xFF000000) >> 24;␊ |
690 | ␉␉␉␉}␊ |
691 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
692 | ␉␉␉␉{␊ |
693 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
694 | ␉␉␉␉}␊ |
695 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
696 | ␊ |
697 | ␉␉␉␉␊ |
698 | ␉␉␉␉break;␊ |
699 | ␉␉␉␉␊ |
700 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:␊ |
701 | ␉␉␉␉//printf("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED\n");␊ |
702 | ␉␉␉␉␊ |
703 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
704 | ␉␉␉␉{␊ |
705 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
706 | ␉␉␉␉␉␊ |
707 | ␉␉␉␉␉((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;␊ |
708 | ␉␉␉␉␉((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;␊ |
709 | ␉␉␉␉␉((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;␊ |
710 | ␉␉␉␉␉((char*)address)[3] = (symbolAddr & 0xFF000000) >> 24;␊ |
711 | ␉␉␉␉}␊ |
712 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
713 | ␉␉␉␉{␊ |
714 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
715 | ␉␉␉␉}␊ |
716 | ␉␉␉␉segmentAddress += (immediate * sizeof(void*)) + sizeof(void*);␊ |
717 | ␊ |
718 | ␉␉␉␉␊ |
719 | ␉␉␉␉break;␊ |
720 | ␉␉␉␉␊ |
721 | ␉␉␉case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:␊ |
722 | ␊ |
723 | ␉␉␉␉tmp = 0;␊ |
724 | ␉␉␉␉bits = 0;␊ |
725 | ␉␉␉␉do␊ |
726 | ␉␉␉␉{␊ |
727 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
728 | ␉␉␉␉␉bits += 7;␊ |
729 | ␉␉␉␉}␊ |
730 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
731 | ␊ |
732 | ␉␉␉␉␊ |
733 | ␉␉␉␉tmp2 = 0;␊ |
734 | ␉␉␉␉bits = 0;␊ |
735 | ␉␉␉␉do␊ |
736 | ␉␉␉␉{␊ |
737 | ␉␉␉␉␉tmp2 |= (bind_stream[++i] & 0x7f) << bits;␊ |
738 | ␉␉␉␉␉bits += 7;␊ |
739 | ␉␉␉␉}␊ |
740 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
741 | ␉␉␉␉␊ |
742 | ␉␉␉␉␊ |
743 | ␉␉␉␉//printf("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0x%X 0x%X\n", tmp, tmp2);␊ |
744 | ␊ |
745 | ␉␉␉␉␊ |
746 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
747 | ␉␉␉␉{␊ |
748 | ␉␉␉␉␉for(index = 0; index < tmp; index++)␊ |
749 | ␉␉␉␉␉{␊ |
750 | ␉␉␉␉␉␉␊ |
751 | ␉␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
752 | ␉␉␉␉␉␉␊ |
753 | ␉␉␉␉␉␉((char*)address)[0] = (symbolAddr & 0x000000FF) >> 0;␊ |
754 | ␉␉␉␉␉␉((char*)address)[1] = (symbolAddr & 0x0000FF00) >> 8;␊ |
755 | ␉␉␉␉␉␉((char*)address)[2] = (symbolAddr & 0x00FF0000) >> 16;␊ |
756 | ␉␉␉␉␉␉((char*)address)[3] = (symbolAddr & 0xFF000000) >> 24;␊ |
757 | ␉␉␉␉␉␉␊ |
758 | ␉␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
759 | ␉␉␉␉␉}␊ |
760 | ␉␉␉␉}␊ |
761 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
762 | ␉␉␉␉{␊ |
763 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
764 | ␉␉␉␉}␊ |
765 | ␉␉␉␉␊ |
766 | ␉␉␉␉␊ |
767 | ␉␉␉␉break;␊ |
768 | ␉␉␉␉␊ |
769 | ␉␉}␊ |
770 | ␉␉i++;␊ |
771 | ␉}␊ |
772 | }␊ |
773 | ␊ |
774 | inline void rebase_location(UInt32* location, char* base)␊ |
775 | {␊ |
776 | ␉*location += (UInt32)base;␊ |
777 | }␊ |
778 | ␊ |
779 | /*␊ |
780 | * add_symbol␊ |
781 | * This function adds a symbol from a module to the list of known symbols ␊ |
782 | * possibly change to a pointer and add this to the Symbol module so that it can␊ |
783 | * adjust it's internal symbol list (sort) to optimize locating new symbols␊ |
784 | */␊ |
785 | void add_symbol(char* symbol, void* addr)␊ |
786 | {␊ |
787 | ␉symbolList_t* entry;␊ |
788 | ␉//printf("Adding symbol %s at 0x%X\n", symbol, addr);␊ |
789 | ␉␊ |
790 | ␉if(!moduleSymbols)␊ |
791 | ␉{␊ |
792 | ␉␉moduleSymbols = entry = malloc(sizeof(symbolList_t));␊ |
793 | ␊ |
794 | ␉}␊ |
795 | ␉else␊ |
796 | ␉{␊ |
797 | ␉␉entry = moduleSymbols;␊ |
798 | ␉␉while(entry->next)␊ |
799 | ␉␉{␊ |
800 | ␉␉␉entry = entry->next;␊ |
801 | ␉␉}␊ |
802 | ␉␉␊ |
803 | ␉␉entry->next = malloc(sizeof(symbolList_t));␊ |
804 | ␉␉entry = entry->next;␊ |
805 | ␉}␊ |
806 | ␊ |
807 | ␉entry->next = NULL;␊ |
808 | ␉entry->addr = (unsigned int)addr;␊ |
809 | ␉entry->symbol = symbol;␊ |
810 | }␊ |
811 | ␊ |
812 | ␊ |
813 | /*␊ |
814 | * print out the information about the loaded module␊ |
815 | ␊ |
816 | */␊ |
817 | void module_loaded(char* name, UInt32 version, UInt32 compat)␊ |
818 | {␊ |
819 | ␉moduleList_t* entry;␊ |
820 | ␉/*␊ |
821 | ␉printf("\%s.dylib Version %d.%d.%d loaded\n"␊ |
822 | ␉␉ "\tCompatibility Version: %d.%d.%d\n",␊ |
823 | ␉␉ name,␊ |
824 | ␉␉ (version >> 16) & 0xFFFF,␊ |
825 | ␉␉ (version >> 8) & 0x00FF,␊ |
826 | ␉␉ (version >> 0) & 0x00FF,␊ |
827 | ␉␉ (compat >> 16) & 0xFFFF,␊ |
828 | ␉␉ (compat >> 8) & 0x00FF,␊ |
829 | ␉␉ (compat >> 0) & 0x00FF);␉␊ |
830 | ␉*/␊ |
831 | ␉if(loadedModules == NULL)␊ |
832 | ␉{␊ |
833 | ␉␉loadedModules = entry = malloc(sizeof(moduleList_t));␊ |
834 | ␉}␊ |
835 | ␉else␊ |
836 | ␉{␊ |
837 | ␉␉entry = loadedModules;␊ |
838 | ␉␉while(entry->next)␊ |
839 | ␉␉{␊ |
840 | ␉␉␉entry = entry->next;␊ |
841 | ␉␉}␊ |
842 | ␉␉entry->next = malloc(sizeof(moduleList_t));␊ |
843 | ␉␉entry = entry->next;␊ |
844 | ␉}␊ |
845 | ␉␊ |
846 | ␉entry->next = NULL;␊ |
847 | ␉entry->module = name;␊ |
848 | ␉entry->version = version;␊ |
849 | ␉entry->compat = compat;␊ |
850 | ␉␊ |
851 | ␉␊ |
852 | }␊ |
853 | ␊ |
854 | int is_module_laoded(const char* name)␊ |
855 | {␊ |
856 | ␉moduleList_t* entry = loadedModules;␊ |
857 | ␉while(entry)␊ |
858 | ␉{␊ |
859 | ␉␉if(strcmp(entry->module, name) == 0)␊ |
860 | ␉␉{␊ |
861 | ␉␉␉return 1;␊ |
862 | ␉␉}␊ |
863 | ␉␉else␊ |
864 | ␉␉{␊ |
865 | ␉␉␉entry = entry->next;␊ |
866 | ␉␉}␊ |
867 | ␊ |
868 | ␉}␊ |
869 | ␉return 0;␊ |
870 | }␊ |
871 | ␊ |
872 | // Look for symbols using the Smbols moduel function.␊ |
873 | // If non are found, look through the list of module symbols␊ |
874 | unsigned int lookup_all_symbols(const char* name)␊ |
875 | {␊ |
876 | ␉unsigned int addr = 0xFFFFFFFF;␊ |
877 | ␉if(lookup_symbol && (UInt32)lookup_symbol != 0xFFFFFFFF)␊ |
878 | ␉{␊ |
879 | ␉␉addr = lookup_symbol(name);␊ |
880 | ␉␉if(addr != 0xFFFFFFFF)␊ |
881 | ␉␉{␊ |
882 | ␉␉␉//printf("Internal symbol %s located at 0x%X\n", name, addr);␊ |
883 | ␉␉␉return addr;␊ |
884 | ␉␉}␊ |
885 | ␉}␊ |
886 | ␊ |
887 | ␉␊ |
888 | ␉symbolList_t* entry = moduleSymbols;␊ |
889 | ␉while(entry)␊ |
890 | ␉{␊ |
891 | ␉␉if(strcmp(entry->symbol, name) == 0)␊ |
892 | ␉␉{␊ |
893 | ␉␉␉//printf("External symbol %s located at 0x%X\n", name, entry->addr);␊ |
894 | ␉␉␉return entry->addr;␊ |
895 | ␉␉}␊ |
896 | ␉␉else␊ |
897 | ␉␉{␊ |
898 | ␉␉␉entry = entry->next;␊ |
899 | ␉␉}␊ |
900 | ␊ |
901 | ␉}␊ |
902 | ␉if(strcmp(name, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
903 | ␉{␊ |
904 | ␉␉printf("Unable to locate symbol %s\n", name);␊ |
905 | ␉}␊ |
906 | ␉return 0xFFFFFFFF;␊ |
907 | }␊ |
908 | ␊ |
909 | ␊ |
910 | /*␊ |
911 | * parse the symbol table␊ |
912 | * Lookup any undefined symbols␊ |
913 | */␊ |
914 | ␊ |
915 | unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, char* symbolStub, UInt32 nonlazy)␊ |
916 | {␊ |
917 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
918 | ␉␊ |
919 | ␉UInt32 symbolIndex = 0;␊ |
920 | ␉char* symbolString = base + (char*)symtabCommand->stroff;␊ |
921 | ␉//char* symbolTable = base + symtabCommand->symoff;␊ |
922 | ␉␊ |
923 | ␉while(symbolIndex < symtabCommand->nsyms)␊ |
924 | ␉{␊ |
925 | ␉␉␊ |
926 | ␉␉struct nlist* symbolEntry = (void*)base + symtabCommand->symoff + (symbolIndex * sizeof(struct nlist));␊ |
927 | ␉␉␊ |
928 | ␉␉// If the symbol is exported by this module␊ |
929 | ␉␉if(symbolEntry->n_value)␊ |
930 | ␉␉{␊ |
931 | ␉␉␉if(strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0)␊ |
932 | ␉␉␉{␊ |
933 | ␉␉␉␉// Module start located. Start is an alias so don't register it␊ |
934 | ␉␉␉␉module_start = base + symbolEntry->n_value;␊ |
935 | ␉␉␉}␊ |
936 | ␉␉␉else␊ |
937 | ␉␉␉{␊ |
938 | ␉␉␉␉add_symbol(symbolString + symbolEntry->n_un.n_strx, (void*)base + symbolEntry->n_value); ␊ |
939 | ␉␉␉}␊ |
940 | ␉␉}␊ |
941 | ␉␉␊ |
942 | ␉␉symbolEntry+= sizeof(struct nlist);␊ |
943 | ␉␉symbolIndex++;␉// TODO remove␊ |
944 | ␉}␊ |
945 | ␉␊ |
946 | ␉return module_start;␊ |
947 | ␉␊ |
948 | }␊ |
949 | ␊ |
950 | ␊ |
951 | /*␊ |
952 | * Locate the symbol for an already loaded function and modify the beginning of␊ |
953 | * the function to jump directly to the new one␊ |
954 | * example: replace_function("_HelloWorld_start", &replacement_start);␊ |
955 | */␊ |
956 | int replace_function(const char* symbol, void* newAddress)␊ |
957 | {␊ |
958 | ␉UInt32* jumpPointer = malloc(sizeof(UInt32*));␉ ␊ |
959 | ␉// TODO: look into using the next four bytes of the function instead␊ |
960 | ␉// Most functions should support this, as they probably will be at ␊ |
961 | ␉// least 10 bytes long, but you never know, this is sligtly saver as␊ |
962 | ␉// function can be as small as 6 bytes.␊ |
963 | ␉UInt32 addr = lookup_all_symbols(symbol);␊ |
964 | ␉␊ |
965 | ␉char* binary = (char*)addr;␊ |
966 | ␉if(addr != 0xFFFFFFFF)␊ |
967 | ␉{␊ |
968 | ␉␉*binary++ = 0xFF;␉// Jump␊ |
969 | ␉␉*binary++ = 0x25;␉// Long Jump␊ |
970 | ␉␉*((UInt32*)binary) = (UInt32)jumpPointer;␊ |
971 | ␉␉␊ |
972 | ␉␉*jumpPointer = (UInt32)newAddress;␊ |
973 | ␉␉␊ |
974 | ␉␉return 1;␊ |
975 | ␉}␊ |
976 | ␉else ␊ |
977 | ␉{␊ |
978 | ␉␉return 0;␊ |
979 | ␉}␊ |
980 | ␊ |
981 | } |