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 | #ifndef DEBUG_MODULES␊ |
12 | #define DEBUG_MODULES 0␊ |
13 | #endif␊ |
14 | ␊ |
15 | #if DEBUG_MODULES␊ |
16 | #define DBG(x...)␉printf(x)␊ |
17 | #else␊ |
18 | #define DBG(x...)␊ |
19 | #endif␊ |
20 | ␊ |
21 | // NOTE: Global so that modules can link with this␊ |
22 | unsigned long long textAddress = 0;␊ |
23 | unsigned long long textSection = 0;␊ |
24 | ␊ |
25 | moduleHook_t* moduleCallbacks = NULL;␊ |
26 | moduleList_t* loadedModules = NULL;␊ |
27 | symbolList_t* moduleSymbols = NULL;␊ |
28 | unsigned int (*lookup_symbol)(const char*) = NULL;␊ |
29 | ␊ |
30 | #if DEBUG_MODULES␊ |
31 | VOID print_hook_list()␊ |
32 | {␊ |
33 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
34 | ␉printf("Hook list: \n");␊ |
35 | ␉while(hooks)␊ |
36 | ␉{␊ |
37 | ␉␉printf("* %s\n", hooks->name);␊ |
38 | ␉␉hooks = hooks->next;␊ |
39 | ␉}␊ |
40 | ␉printf("\n");␊ |
41 | }␊ |
42 | ␊ |
43 | VOID print_symbol_list()␊ |
44 | {␊ |
45 | ␉symbolList_t* symbol = moduleSymbols;␊ |
46 | ␉printf("Symbol list: \n");␊ |
47 | ␉while(symbol)␊ |
48 | ␉{␊ |
49 | ␉␉printf("* %s\n", symbol->symbol);␊ |
50 | ␉␉symbol = symbol->next;␊ |
51 | ␉}␊ |
52 | ␉printf("\n");␊ |
53 | }␊ |
54 | #endif␊ |
55 | ␊ |
56 | ␊ |
57 | /*␊ |
58 | * Initialize the module system by loading the Symbols.dylib module.␊ |
59 | * Once loaded, locate the _lookup_symbol function so that internal␊ |
60 | * symbols can be resolved.␊ |
61 | */␊ |
62 | EFI_STATUS init_module_system()␊ |
63 | {␊ |
64 | ␉msglog("* Attempting to load system module\n");␊ |
65 | ␉␊ |
66 | ␉// Intialize module system␊ |
67 | EFI_STATUS status = load_module(SYMBOLS_MODULE);␊ |
68 | ␉if((status == EFI_SUCCESS) || (status == EFI_ALREADY_STARTED)/*should never happen*/ ) ␊ |
69 | ␉{␊ |
70 | ␉␉lookup_symbol = (void*)lookup_all_symbols(SYMBOL_LOOKUP_SYMBOL);␊ |
71 | ␉␉␊ |
72 | ␉␉if((UInt32)lookup_symbol != 0xFFFFFFFF)␊ |
73 | ␉␉{␊ |
74 | ␉␉␉return status;␊ |
75 | ␉␉}␊ |
76 | ␉␉␊ |
77 | ␉}␊ |
78 | ␉␊ |
79 | ␉return EFI_LOAD_ERROR;␊ |
80 | }␊ |
81 | ␊ |
82 | ␊ |
83 | /*␊ |
84 | * Load all modules in the /Extra/modules/ directory␊ |
85 | * Module depencdies will be loaded first␊ |
86 | * MOdules will only be loaded once. When loaded a module must␊ |
87 | * setup apropriete function calls and hooks as required.␊ |
88 | * NOTE: To ensure a module loads after another you may ␊ |
89 | * link one module with the other. For dyld to allow this, you must␊ |
90 | * reference at least one symbol within the module.␊ |
91 | */␊ |
92 | ␊ |
93 | VOID load_all_modules()␊ |
94 | {␊ |
95 | ␉char* name;␊ |
96 | ␉long flags;␊ |
97 | ␉long time;␊ |
98 | ␉struct dirstuff* moduleDir = opendir("/Extra/modules/");␊ |
99 | ␉while(readdir(moduleDir, (const char**)&name, &flags, &time) >= 0)␊ |
100 | ␉{␊ |
101 | ␉␉if ((strcmp(SYMBOLS_MODULE,name)) == 0) continue; // if we found Symbols.dylib, just skip it␊ |
102 | ␊ |
103 | ␉␉if(strcmp(&name[strlen(name) - sizeof("dylib")], ".dylib") == 0)␊ |
104 | ␉␉{␊ |
105 | ␉␉␉char* tmp = malloc(strlen(name) + 1);␊ |
106 | ␉␉␉strcpy(tmp, name);␊ |
107 | ␉␉␉␊ |
108 | ␉␉␉msglog("* Attempting to load module: %s\n", tmp);␉␉␉␊ |
109 | ␉␉␉if(load_module(tmp) != EFI_SUCCESS)␊ |
110 | ␉␉␉{␊ |
111 | ␉␉␉␉// failed to load or already loaded␊ |
112 | ␉␉␉␉ free(tmp);␊ |
113 | ␉␉␉}␊ |
114 | ␉␉}␊ |
115 | #if DEBUG_MODULES␊ |
116 | ␉␉else ␊ |
117 | ␉␉{␊ |
118 | ␉␉␉DBG("Ignoring %s\n", name);␊ |
119 | ␉␉}␊ |
120 | #endif␊ |
121 | ␊ |
122 | ␉}␊ |
123 | #if DEBUG_MODULES␊ |
124 | ␉print_symbol_list();␊ |
125 | #endif␊ |
126 | }␊ |
127 | ␊ |
128 | /*␊ |
129 | * Load a module file in /Extra/modules␊ |
130 | * TODO: verify version number of module␊ |
131 | */␊ |
132 | EFI_STATUS load_module(char* module)␊ |
133 | {␊ |
134 | ␉void (*module_start)(void) = NULL;␊ |
135 | ␊ |
136 | ␉␊ |
137 | ␉// Check to see if the module has already been loaded␊ |
138 | ␉if(is_module_loaded(module) == EFI_SUCCESS)␊ |
139 | ␉{␊ |
140 | ␉␉msglog("Module %s already registred\n", module);␊ |
141 | ␉␉return EFI_ALREADY_STARTED;␊ |
142 | ␉}␊ |
143 | ␉␊ |
144 | ␉char modString[128];␊ |
145 | ␉int fh = -1;␊ |
146 | ␉sprintf(modString, "/Extra/modules/%s", module);␊ |
147 | ␉fh = open(modString);␊ |
148 | ␉if(fh < 0)␊ |
149 | ␉{␉␉␊ |
150 | #if DEBUG_MODULES␊ |
151 | ␉␉DBG("Unable to locate module %s\n", modString);␊ |
152 | ␉␉getc();␊ |
153 | #else␊ |
154 | ␉␉msglog("Unable to locate module %s\n", modString);␊ |
155 | #endif␊ |
156 | ␉␉␉return EFI_OUT_OF_RESOURCES;␉␉␊ |
157 | ␉}␊ |
158 | ␉EFI_STATUS ret = EFI_SUCCESS;␊ |
159 | ␉int moduleSize = file_size(fh);␊ |
160 | ␉char* module_base = (char*) malloc(moduleSize);␊ |
161 | ␉if (moduleSize && read(fh, module_base, moduleSize) == moduleSize)␊ |
162 | ␉{␊ |
163 | ␊ |
164 | ␉␉DBG("Module %s read in.\n", modString);␊ |
165 | ␊ |
166 | ␉␉// Module loaded into memory, parse it␊ |
167 | ␉␉module_start = parse_mach(module_base, &load_module, &add_symbol);␊ |
168 | ␊ |
169 | ␉␉if(module_start && module_start != (void*)0xFFFFFFFF)␊ |
170 | ␉␉{␊ |
171 | ␉␉␉module_loaded(module/*moduleName, moduleVersion, moduleCompat*/);␊ |
172 | ␉␉␉// Notify the system that it was laoded␊ |
173 | ␉␉␉(*module_start)();␉// Start the module␊ |
174 | ␉␉␉msglog("%s successfully Loaded.\n", module);␊ |
175 | ␉␉}␊ |
176 | ␉␉else {␊ |
177 | ␉␉␉// The module does not have a valid start function␊ |
178 | ␉␉␉printf("Unable to start %s\n", module);␉␊ |
179 | ␉␉␉ret = EFI_NOT_STARTED;␊ |
180 | #if DEBUG_MODULES␉␉␉␊ |
181 | ␉␉␉getc();␊ |
182 | #endif␊ |
183 | ␉␉}␉␉␊ |
184 | ␉}␊ |
185 | ␉else␊ |
186 | ␉{␊ |
187 | ␉␉printf("Unable to read in module %s\n.", module);␊ |
188 | #if DEBUG_MODULES␉␉␊ |
189 | ␉␉getc();␊ |
190 | #endif␊ |
191 | ␉␉ret = EFI_LOAD_ERROR;␊ |
192 | ␉}␊ |
193 | ␉close(fh);␊ |
194 | ␉return ret;␊ |
195 | }␊ |
196 | ␊ |
197 | moduleHook_t* get_callback(const char* name)␊ |
198 | {␊ |
199 | ␉moduleHook_t* hooks = moduleCallbacks;␊ |
200 | ␉␊ |
201 | ␉// look for a hook. If it exists, return the moduleHook_t*,␊ |
202 | ␉// If not, return NULL.␊ |
203 | ␉while(hooks)␊ |
204 | ␉{␊ |
205 | ␉␉if(strcmp(name, hooks->name) == 0)␊ |
206 | ␉␉{␊ |
207 | ␉␉␉//DBG("Located hook %s\n", name);␊ |
208 | ␉␉␉return hooks;␊ |
209 | ␉␉}␊ |
210 | ␉␉hooks = hooks->next;␊ |
211 | ␉}␊ |
212 | ␉return NULL;␊ |
213 | ␉␊ |
214 | }␊ |
215 | ␊ |
216 | /*␊ |
217 | *␉execute_hook( const char* name )␊ |
218 | *␉␉name - Name of the module hook␊ |
219 | *␉␉␉If any callbacks have been registered for this hook␊ |
220 | *␉␉␉they will be executed now in the same order that the␊ |
221 | *␉␉␉hooks were added.␊ |
222 | */␊ |
223 | EFI_STATUS execute_hook(const char* name, void* arg1, void* arg2, void* arg3, void* arg4, void* arg5, void* arg6)␊ |
224 | {␊ |
225 | ␉DBG("Attempting to execute hook '%s'\n", name);␊ |
226 | ␉moduleHook_t* hook = get_callback(name);␊ |
227 | ␉␊ |
228 | ␉if(hook)␊ |
229 | ␉{␊ |
230 | ␉␉// Loop through all callbacks for this module␊ |
231 | ␉␉callbackList_t* callbacks = hook->callbacks;␊ |
232 | ␉␉␊ |
233 | ␉␉while(callbacks)␊ |
234 | ␉␉{␊ |
235 | ␉␉␉// Execute callback␊ |
236 | ␉␉␉callbacks->callback(arg1, arg2, arg3, arg4, arg5, arg6);␊ |
237 | ␉␉␉callbacks = callbacks->next;␊ |
238 | ␉␉}␊ |
239 | ␉␉DBG("Hook '%s' executed.\n", name); ␊ |
240 | ␉␉return EFI_SUCCESS;␊ |
241 | ␉}␊ |
242 | ␉␊ |
243 | ␉// Callback for this hook doesn't exist;␊ |
244 | ␉DBG("No callbacks for '%s' hook.\n", name);␊ |
245 | ␉return EFI_NOT_FOUND;␊ |
246 | ␉␊ |
247 | }␊ |
248 | ␊ |
249 | /*␊ |
250 | *␉register_hook_callback( const char* name, void(*callback)())␊ |
251 | *␉␉name - Name of the module hook to attach to.␊ |
252 | *␉␉callbacks - The funciton pointer that will be called when the␊ |
253 | *␉␉␉hook is executed. When registering a new callback name, the callback is added sorted.␊ |
254 | *␉␉␉NOTE: the hooks take four void* arguments.␊ |
255 | */␊ |
256 | VOID register_hook_callback(const char* name, void(*callback)(void*, void*, void*, void*, void*, void*))␊ |
257 | {␉␊ |
258 | ␉DBG("Adding callback for '%s' hook.\n", name);␊ |
259 | ␉␊ |
260 | ␉moduleHook_t* hook = get_callback(name);␊ |
261 | ␉␊ |
262 | ␉if(hook)␊ |
263 | ␉{␊ |
264 | ␉␉// append␊ |
265 | ␉␉callbackList_t* newCallback = malloc(sizeof(callbackList_t));␊ |
266 | ␉␉newCallback->next = hook->callbacks;␊ |
267 | ␉␉hook->callbacks = newCallback;␊ |
268 | ␉␉newCallback->callback = callback;␊ |
269 | ␉}␊ |
270 | ␉else␊ |
271 | ␉{␊ |
272 | ␉␉// create new hook␊ |
273 | ␉␉moduleHook_t* newHook = malloc(sizeof(moduleHook_t));␉␉␊ |
274 | ␉␉newHook->name = name;␊ |
275 | ␉␉newHook->callbacks = malloc(sizeof(callbackList_t));␊ |
276 | ␉␉newHook->callbacks->callback = callback;␊ |
277 | ␉␉newHook->callbacks->next = NULL;␊ |
278 | ␉␉␊ |
279 | ␉␉newHook->next = moduleCallbacks;␊ |
280 | ␉␉moduleCallbacks = newHook;␊ |
281 | ␉␉␊ |
282 | ␉}␊ |
283 | ␉␊ |
284 | #if DEBUG_MODULES␊ |
285 | ␉print_hook_list();␊ |
286 | ␉getc();␊ |
287 | #endif␊ |
288 | ␉␊ |
289 | }␊ |
290 | ␊ |
291 | #if DEBUG_MODULES␊ |
292 | unsigned long vmaddr;␊ |
293 | long vmsize;␊ |
294 | #endif␊ |
295 | ␊ |
296 | /*␊ |
297 | * Parse through a macho module. The module will be rebased and binded␊ |
298 | * as specified in the macho header. If the module is successfully loaded␊ |
299 | * the module iinit address will be returned.␊ |
300 | * NOTE; all dependecies will be loaded before this module is started␊ |
301 | * NOTE: If the module is unable to load ot completeion, the modules␊ |
302 | * symbols will still be available (TODO: fix this). This should not␊ |
303 | * happen as all dependencies are verified before the sybols are read in.␊ |
304 | */␊ |
305 | void* parse_mach(void* binary, EFI_STATUS(*dylib_loader)(char*), long long(*symbol_handler)(char*, long long, char))␉// TODO: add param to specify valid archs␊ |
306 | {␉␊ |
307 | ␉char is64 = false;␊ |
308 | ␉void (*module_start)(void) = NULL;␊ |
309 | ␉␊ |
310 | ␉// Module info␊ |
311 | ␉/*char* moduleName = NULL;␊ |
312 | ␉UInt32 moduleVersion = 0;␊ |
313 | ␉UInt32 moduleCompat = 0;␊ |
314 | ␉*/␊ |
315 | ␉// TODO convert all of the structs to a union␊ |
316 | ␉struct load_command *loadCommand = NULL;␊ |
317 | ␉struct dylib_command* dylibCommand = NULL;␊ |
318 | ␉struct dyld_info_command* dyldInfoCommand = NULL;␊ |
319 | ␉␊ |
320 | ␉struct symtab_command* symtabCommand = NULL;␊ |
321 | ␉struct segment_command *segCommand = NULL;␊ |
322 | ␉struct segment_command_64 *segCommand64 = NULL;␊ |
323 | ␊ |
324 | ␉//struct dysymtab_command* dysymtabCommand = NULL;␊ |
325 | ␉UInt32 binaryIndex = 0;␊ |
326 | ␉UInt16 cmd = 0;␊ |
327 | ␊ |
328 | ␉// Parse through the load commands␊ |
329 | ␉if(((struct mach_header*)binary)->magic == MH_MAGIC)␊ |
330 | ␉{␊ |
331 | ␉␉is64 = false;␊ |
332 | ␉␉binaryIndex += sizeof(struct mach_header);␊ |
333 | ␉}␊ |
334 | ␉else if(((struct mach_header_64*)binary)->magic == MH_MAGIC_64)␊ |
335 | ␉{␊ |
336 | ␉␉// NOTE: modules cannot be 64bit...␊ |
337 | ␉␉is64 = true;␊ |
338 | ␉␉binaryIndex += sizeof(struct mach_header_64);␊ |
339 | ␉}␊ |
340 | ␉else␊ |
341 | ␉{␊ |
342 | ␉␉printf("Modules: Invalid mach magic\n");␊ |
343 | ␉␉getc();␊ |
344 | ␉␉return NULL;␊ |
345 | ␉}␊ |
346 | ␊ |
347 | ␊ |
348 | ␉␊ |
349 | ␉/*if(((struct mach_header*)binary)->filetype != MH_DYLIB)␊ |
350 | ␉{␊ |
351 | ␉␉printf("Module is not a dylib. Unable to load.\n");␊ |
352 | ␉␉getc();␊ |
353 | ␉␉return NULL; // Module is in the incorrect format␊ |
354 | ␉}*/␊ |
355 | ␉␊ |
356 | ␉while(cmd < ((struct mach_header*)binary)->ncmds)␊ |
357 | ␉{␊ |
358 | ␉␉cmd++;␊ |
359 | ␉␉␊ |
360 | ␉␉loadCommand = binary + binaryIndex;␊ |
361 | ␉␉UInt32 cmdSize = loadCommand->cmdsize;␊ |
362 | ␊ |
363 | ␉␉␊ |
364 | ␉␉switch ((loadCommand->cmd & 0x7FFFFFFF))␊ |
365 | ␉␉{␊ |
366 | ␉␉␉case LC_SYMTAB:␊ |
367 | ␉␉␉␉symtabCommand = binary + binaryIndex;␊ |
368 | ␉␉␉␉break;␊ |
369 | ␉␉␉␉␊ |
370 | ␉␉␉case LC_SEGMENT: // 32bit macho␊ |
371 | ␉␉␉␉segCommand = binary + binaryIndex;␊ |
372 | ␉␉␉␉␊ |
373 | ␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
374 | ␉␉␉␉␊ |
375 | ␉␉␉␉if(strcmp("__TEXT", segCommand->segname) == 0)␊ |
376 | ␉␉␉␉{␊ |
377 | ␉␉␉␉␉UInt32 sectionIndex;␊ |
378 | ␉␉␉␉␉␊ |
379 | #if HARD_DEBUG_MODULES␉␉␉␊ |
380 | ␉␉␉␉␉unsigned long fileaddr;␊ |
381 | ␉␉␉␉␉long filesize;␉␉␉␉␉␊ |
382 | ␉␉␉␉␉vmaddr = (segCommand->vmaddr & 0x3fffffff);␊ |
383 | ␉␉␉␉␉vmsize = segCommand->vmsize;␉ ␊ |
384 | ␉␉␉␉␉fileaddr = ((unsigned long)(binary + binaryIndex) + segCommand->fileoff);␊ |
385 | ␉␉␉␉␉filesize = segCommand->filesize;␊ |
386 | ␉␉␉␉␉␊ |
387 | ␉␉␉␉␉printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",␊ |
388 | ␉␉␉␉␉␉ segCommand->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,␊ |
389 | ␉␉␉␉␉␉ (unsigned) segCommand->nsects, (unsigned)segCommand->flags);␊ |
390 | ␉␉␉␉␉getc();␊ |
391 | #endif␊ |
392 | ␉␉␉␉␉sectionIndex = sizeof(struct segment_command);␊ |
393 | ␉␉␉␉␉␊ |
394 | ␉␉␉␉␉struct section *sect;␊ |
395 | ␉␉␉␉␉␊ |
396 | ␉␉␉␉␉while(sectionIndex < segCommand->cmdsize)␊ |
397 | ␉␉␉␉␉{␊ |
398 | ␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
399 | ␉␉␉␉␉␉␊ |
400 | ␉␉␉␉␉␉sectionIndex += sizeof(struct section);␊ |
401 | ␉␉␉␉␉␉␊ |
402 | ␉␉␉␉␉␉␊ |
403 | ␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
404 | ␉␉␉␉␉␉{␊ |
405 | ␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
406 | ␉␉␉␉␉␉␉textSection = sect->offset;␊ |
407 | ␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
408 | ␉␉␉␉␉␉␉break;␊ |
409 | ␉␉␉␉␉␉}␉␉␉␉␉␊ |
410 | ␉␉␉␉␉}␊ |
411 | ␉␉␉␉}␊ |
412 | ␉␉␉␉break;␊ |
413 | ␉␉␉case LC_SEGMENT_64:␉// 64bit macho's␊ |
414 | ␉␉␉␉segCommand64 = binary + binaryIndex;␊ |
415 | ␉␉␉␉␊ |
416 | ␉␉␉␉//printf("Segment name is %s\n", segCommand->segname);␊ |
417 | ␉␉␉␉␊ |
418 | ␉␉␉␉if(strcmp("__TEXT", segCommand64->segname) == 0)␊ |
419 | ␉␉␉␉{␊ |
420 | ␉␉␉␉␉UInt32 sectionIndex;␊ |
421 | ␉␉␉␉␉␊ |
422 | #if HARD_DEBUG_MODULES␉␉␉␉␉␊ |
423 | ␉␉␉␉␉␊ |
424 | ␉␉␉␉␉unsigned long fileaddr;␊ |
425 | ␉␉␉␉␉long filesize;␉␉␉␉␉␊ |
426 | ␉␉␉␉␉vmaddr = (segCommand64->vmaddr & 0x3fffffff);␊ |
427 | ␉␉␉␉␉vmsize = segCommand64->vmsize;␉ ␊ |
428 | ␉␉␉␉␉fileaddr = ((unsigned long)(binary + binaryIndex) + segCommand64->fileoff);␊ |
429 | ␉␉␉␉␉filesize = segCommand64->filesize;␊ |
430 | ␉␉␉␉␉␊ |
431 | ␉␉␉␉␉printf("segname: %s, vmaddr: %x, vmsize: %x, fileoff: %x, filesize: %x, nsects: %d, flags: %x.\n",␊ |
432 | ␉␉␉␉␉␉ segCommand64->segname, (unsigned)vmaddr, (unsigned)vmsize, (unsigned)fileaddr, (unsigned)filesize,␊ |
433 | ␉␉␉␉␉␉ (unsigned) segCommand64->nsects, (unsigned)segCommand64->flags);␊ |
434 | ␉␉␉␉␉getc();␊ |
435 | #endif␊ |
436 | ␉␉␉␉␉␊ |
437 | ␉␉␉␉␉sectionIndex = sizeof(struct segment_command_64);␊ |
438 | ␉␉␉␉␉␊ |
439 | ␉␉␉␉␉struct section_64 *sect;␊ |
440 | ␉␉␉␉␉␊ |
441 | ␉␉␉␉␉while(sectionIndex < segCommand64->cmdsize)␊ |
442 | ␉␉␉␉␉{␊ |
443 | ␉␉␉␉␉␉sect = binary + binaryIndex + sectionIndex;␊ |
444 | ␉␉␉␉␉␉␊ |
445 | ␉␉␉␉␉␉sectionIndex += sizeof(struct section_64);␊ |
446 | ␉␉␉␉␉␉␊ |
447 | ␉␉␉␉␉␉␊ |
448 | ␉␉␉␉␉␉if(strcmp("__text", sect->sectname) == 0)␊ |
449 | ␉␉␉␉␉␉{␊ |
450 | ␉␉␉␉␉␉␉// __TEXT,__text found, save the offset and address for when looking for the calls.␊ |
451 | ␉␉␉␉␉␉␉textSection = sect->offset;␊ |
452 | ␉␉␉␉␉␉␉textAddress = sect->addr;␊ |
453 | ␉␉␉␉␉␉␉␊ |
454 | ␉␉␉␉␉␉␉break;␊ |
455 | ␉␉␉␉␉␉}␉␉␉␉␉␊ |
456 | ␉␉␉␉␉}␊ |
457 | ␉␉␉␉}␊ |
458 | ␉␉␉␉␊ |
459 | ␉␉␉␉break;␊ |
460 | ␉␉␉␉␊ |
461 | ␉␉␉case LC_DYSYMTAB:␊ |
462 | ␉␉␉␉break;␊ |
463 | ␉␉␉␉␊ |
464 | ␉␉␉case LC_LOAD_DYLIB:␊ |
465 | ␉␉␉case LC_LOAD_WEAK_DYLIB ^ LC_REQ_DYLD:␊ |
466 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
467 | ␉␉␉␉char* module = binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
468 | ␉␉␉␉// TODO: verify version␊ |
469 | ␉␉␉␉// =␉dylibCommand->dylib.current_version;␊ |
470 | ␉␉␉␉// =␉dylibCommand->dylib.compatibility_version;␊ |
471 | ␉␉␉␉char* name = malloc(strlen(module) + strlen(".dylib") + 1);␊ |
472 | ␉␉␉␉sprintf(name, "%s.dylib", module);␉␉␉␉␊ |
473 | ␉␉␉␉if(dylib_loader == EFI_SUCCESS)␊ |
474 | ␉␉␉␉{␊ |
475 | ␉␉␉␉␉EFI_STATUS statue = dylib_loader(name);␊ |
476 | ␉␉␉␉␉␊ |
477 | ␉␉␉␉␉if( statue != EFI_SUCCESS)␊ |
478 | ␉␉␉␉␉{␊ |
479 | ␉␉␉␉␉␉free(name);␊ |
480 | ␉␉␉␉␉␉if (statue != EFI_ALREADY_STARTED) {␊ |
481 | ␉␉␉␉␉␉␉// Unable to load dependancy␊ |
482 | ␉␉␉␉␉␉␉return NULL;␊ |
483 | ␉␉␉␉␉␉}␊ |
484 | ␉␉␉␉␉␉␊ |
485 | ␉␉␉␉␉} ␊ |
486 | ␊ |
487 | ␉␉␉␉}␊ |
488 | ␉␉␉␉break;␊ |
489 | ␉␉␉␉␊ |
490 | ␉␉␉case LC_ID_DYLIB:␊ |
491 | ␉␉␉␉dylibCommand = binary + binaryIndex;␊ |
492 | ␉␉␉␉/*moduleName =␉binary + binaryIndex + ((UInt32)*((UInt32*)&dylibCommand->dylib.name));␊ |
493 | ␉␉␉␉moduleVersion =␉dylibCommand->dylib.current_version;␊ |
494 | ␉␉␉␉moduleCompat =␉dylibCommand->dylib.compatibility_version;␊ |
495 | ␉␉␉␉ */␊ |
496 | ␉␉␉␉break;␊ |
497 | ␊ |
498 | ␉␉␉case LC_DYLD_INFO:␊ |
499 | ␉␉␉␉// Bind and rebase info is stored here␊ |
500 | ␉␉␉␉dyldInfoCommand = binary + binaryIndex;␊ |
501 | ␉␉␉␉break;␊ |
502 | ␉␉␉␉␊ |
503 | ␉␉␉case LC_UUID:␊ |
504 | ␉␉␉␉break;␊ |
505 | ␉␉␉␉␊ |
506 | ␉␉␉case LC_UNIXTHREAD:␊ |
507 | ␉␉␉␉break;␊ |
508 | ␉␉␉␉␊ |
509 | ␉␉␉default:␊ |
510 | ␉␉␉␉DBG("Unhandled loadcommand 0x%X\n", loadCommand->cmd & 0x7FFFFFFF);␊ |
511 | ␉␉␉␉break;␊ |
512 | ␉␉␊ |
513 | ␉␉}␊ |
514 | ␊ |
515 | ␉␉binaryIndex += cmdSize;␊ |
516 | ␉}␊ |
517 | ␉//if(!moduleName) return NULL;␊ |
518 | ␉␉␊ |
519 | ␊ |
520 | ␉// bind_macho uses the symbols.␊ |
521 | ␉module_start = (void*)handle_symtable((UInt32)binary, symtabCommand, symbol_handler, is64);␊ |
522 | ␊ |
523 | ␉// Rebase the module before binding it.␊ |
524 | ␉if(dyldInfoCommand && dyldInfoCommand->rebase_off)␊ |
525 | ␉{␊ |
526 | ␉␉rebase_macho(binary, (char*)dyldInfoCommand->rebase_off, dyldInfoCommand->rebase_size);␊ |
527 | ␉}␊ |
528 | ␉␊ |
529 | ␉if(dyldInfoCommand && dyldInfoCommand->bind_off)␊ |
530 | ␉{␊ |
531 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->bind_off, dyldInfoCommand->bind_size);␊ |
532 | ␉}␊ |
533 | ␉␊ |
534 | ␉if(dyldInfoCommand && dyldInfoCommand->weak_bind_off)␊ |
535 | ␉{␊ |
536 | ␉␉// NOTE: this currently should never happen.␊ |
537 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->weak_bind_off, dyldInfoCommand->weak_bind_size);␊ |
538 | ␉}␊ |
539 | ␉␊ |
540 | ␉if(dyldInfoCommand && dyldInfoCommand->lazy_bind_off)␊ |
541 | ␉{␊ |
542 | ␉␉// NOTE: we are binding the lazy pointers as a module is laoded,␊ |
543 | ␉␉// This should be changed to bind when a symbol is referened at runtime instead.␊ |
544 | ␉␉bind_macho(binary, (char*)dyldInfoCommand->lazy_bind_off, dyldInfoCommand->lazy_bind_size);␊ |
545 | ␉}␊ |
546 | ␊ |
547 | ␉return module_start;␊ |
548 | ␉␊ |
549 | }␊ |
550 | ␊ |
551 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
552 | void rebase_macho(void* base, char* rebase_stream, UInt32 size)␊ |
553 | {␊ |
554 | ␉rebase_stream += (UInt32)base;␊ |
555 | ␉␊ |
556 | ␉UInt8 immediate = 0;␊ |
557 | ␉UInt8 opcode = 0;␊ |
558 | ␉UInt8 type = 0;␊ |
559 | ␉␊ |
560 | ␉UInt32 segmentAddress = 0;␊ |
561 | ␉␊ |
562 | ␉␊ |
563 | ␉␊ |
564 | ␉UInt32 tmp = 0;␊ |
565 | ␉UInt32 tmp2 = 0;␊ |
566 | ␉UInt8 bits = 0;␊ |
567 | ␉UInt32 index = 0;␊ |
568 | ␉␊ |
569 | ␉int done = 0;␊ |
570 | ␉unsigned int i = 0;␊ |
571 | ␉␊ |
572 | ␉while(/*!done &&*/ i < size)␊ |
573 | ␉{␊ |
574 | ␉␉immediate = rebase_stream[i] & REBASE_IMMEDIATE_MASK;␊ |
575 | ␉␉opcode = rebase_stream[i] & REBASE_OPCODE_MASK;␊ |
576 | ␊ |
577 | ␉␉␊ |
578 | ␉␉switch(opcode)␊ |
579 | ␉␉{␊ |
580 | ␉␉␉case REBASE_OPCODE_DONE:␊ |
581 | ␉␉␉␉// Rebase complete.␊ |
582 | ␉␉␉␉done = 1;␊ |
583 | ␉␉␉␉break;␊ |
584 | ␉␉␉␉␊ |
585 | ␉␉␉␉␊ |
586 | ␉␉␉case REBASE_OPCODE_SET_TYPE_IMM:␊ |
587 | ␉␉␉␉// Set rebase type (pointer, absolute32, pcrel32)␊ |
588 | ␉␉␉␉//DBG("Rebase type = 0x%X\n", immediate);␊ |
589 | ␉␉␉␉type = immediate;␊ |
590 | ␉␉␉␉break;␊ |
591 | ␉␉␉␉␊ |
592 | ␉␉␉␉␊ |
593 | ␉␉␉case REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
594 | ␉␉␉␉// Locate address to begin rebasing␊ |
595 | ␉␉␉␉segmentAddress = 0;␊ |
596 | ␊ |
597 | ␉␉␉␉struct segment_command* segCommand = NULL; // NOTE: 32bit only␊ |
598 | ␉␉␉␉␊ |
599 | ␉␉␉␉unsigned int binIndex = 0;␊ |
600 | ␉␉␉␉index = 0;␊ |
601 | ␉␉␉␉do␊ |
602 | ␉␉␉␉{␊ |
603 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
604 | ␉␉␉␉␉␊ |
605 | ␉␉␉␉␉␊ |
606 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
607 | ␉␉␉␉␉index++;␊ |
608 | ␉␉␉␉}␊ |
609 | ␉␉␉␉while(index <= immediate);␊ |
610 | ␊ |
611 | ␊ |
612 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
613 | ␉␉␉␉␊ |
614 | ␉␉␉␉tmp = 0;␊ |
615 | ␉␉␉␉bits = 0;␊ |
616 | ␉␉␉␉do␊ |
617 | ␉␉␉␉{␊ |
618 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
619 | ␉␉␉␉␉bits += 7;␊ |
620 | ␉␉␉␉}␊ |
621 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
622 | ␉␉␉␉␊ |
623 | ␉␉␉␉segmentAddress += tmp;␊ |
624 | ␉␉␉␉break;␊ |
625 | ␉␉␉␉␊ |
626 | ␉␉␉␉␊ |
627 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_ULEB:␊ |
628 | ␉␉␉␉// Add value to rebase address␊ |
629 | ␉␉␉␉tmp = 0;␊ |
630 | ␉␉␉␉bits = 0;␊ |
631 | ␉␉␉␉do␊ |
632 | ␉␉␉␉{␊ |
633 | ␉␉␉␉␉tmp <<= bits;␊ |
634 | ␉␉␉␉␉tmp |= rebase_stream[++i] & 0x7f;␊ |
635 | ␉␉␉␉␉bits += 7;␊ |
636 | ␉␉␉␉}␊ |
637 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
638 | ␉␉␉␉␊ |
639 | ␉␉␉␉segmentAddress +=␉tmp; ␊ |
640 | ␉␉␉␉break;␊ |
641 | ␉␉␉␉␊ |
642 | ␉␉␉case REBASE_OPCODE_ADD_ADDR_IMM_SCALED:␊ |
643 | ␉␉␉␉segmentAddress += immediate * sizeof(void*);␊ |
644 | ␉␉␉␉break;␊ |
645 | ␉␉␉␉␊ |
646 | ␉␉␉␉␊ |
647 | ␉␉␉case REBASE_OPCODE_DO_REBASE_IMM_TIMES:␊ |
648 | ␉␉␉␉index = 0;␊ |
649 | ␉␉␉␉for (index = 0; index < immediate; ++index) {␊ |
650 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
651 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
652 | ␉␉␉␉}␊ |
653 | ␉␉␉␉break;␊ |
654 | ␉␉␉␊ |
655 | ␉␉␉␉␊ |
656 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES:␊ |
657 | ␉␉␉␉tmp = 0;␊ |
658 | ␉␉␉␉bits = 0;␊ |
659 | ␉␉␉␉do␊ |
660 | ␉␉␉␉{␊ |
661 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
662 | ␉␉␉␉␉bits += 7;␊ |
663 | ␉␉␉␉}␊ |
664 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
665 | ␉␉␉␉␊ |
666 | ␉␉␉␉index = 0;␊ |
667 | ␉␉␉␉for (index = 0; index < tmp; ++index) {␊ |
668 | ␉␉␉␉␉//DBG("\tRebasing 0x%X\n", segmentAddress);␊ |
669 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␉␉␉␉␉␊ |
670 | ␉␉␉␉␉segmentAddress += sizeof(void*);␊ |
671 | ␉␉␉␉}␊ |
672 | ␉␉␉␉break;␊ |
673 | ␉␉␉␉␊ |
674 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:␊ |
675 | ␉␉␉␉tmp = 0;␊ |
676 | ␉␉␉␉bits = 0;␊ |
677 | ␉␉␉␉do␊ |
678 | ␉␉␉␉{␊ |
679 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
680 | ␉␉␉␉␉bits += 7;␊ |
681 | ␉␉␉␉}␊ |
682 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
683 | ␉␉␉␉␊ |
684 | ␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
685 | ␉␉␉␉␊ |
686 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
687 | ␉␉␉␉break;␊ |
688 | ␉␉␉␉␊ |
689 | ␉␉␉case REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:␊ |
690 | ␉␉␉␉tmp = 0;␊ |
691 | ␉␉␉␉bits = 0;␊ |
692 | ␉␉␉␉do␊ |
693 | ␉␉␉␉{␊ |
694 | ␉␉␉␉␉tmp |= (rebase_stream[++i] & 0x7f) << bits;␊ |
695 | ␉␉␉␉␉bits += 7;␊ |
696 | ␉␉␉␉}␊ |
697 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
698 | ␉␉␉␉␊ |
699 | ␉␉␉␉␊ |
700 | ␉␉␉␉tmp2 = 0;␊ |
701 | ␉␉␉␉bits = 0;␊ |
702 | ␉␉␉␉do␊ |
703 | ␉␉␉␉{␊ |
704 | ␉␉␉␉␉tmp2 |= (rebase_stream[++i] & 0x7f) << bits;␊ |
705 | ␉␉␉␉␉bits += 7;␊ |
706 | ␉␉␉␉}␊ |
707 | ␉␉␉␉while(rebase_stream[i] & 0x80);␊ |
708 | ␉␉␉␉␊ |
709 | ␉␉␉␉index = 0;␊ |
710 | ␉␉␉␉for (index = 0; index < tmp; ++index) {␊ |
711 | ␊ |
712 | ␉␉␉␉␉rebase_location(base + segmentAddress, (char*)base, type);␊ |
713 | ␉␉␉␉␉␊ |
714 | ␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
715 | ␉␉␉␉}␊ |
716 | ␉␉␉␉break;␊ |
717 | ␉␉␉default:␊ |
718 | ␉␉␉␉break;␊ |
719 | ␉␉}␊ |
720 | ␉␉i++;␊ |
721 | ␉}␊ |
722 | }␊ |
723 | ␊ |
724 | // Based on code from dylibinfo.cpp and ImageLoaderMachOCompressed.cpp␊ |
725 | // NOTE: this uses 32bit values, and not 64bit values. ␊ |
726 | // There is apossibility that this could cause issues,␊ |
727 | // however the macho file is 32 bit, so it shouldn't matter too much␊ |
728 | void bind_macho(void* base, char* bind_stream, UInt32 size)␊ |
729 | {␉␊ |
730 | ␉bind_stream += (UInt32)base;␊ |
731 | ␉␊ |
732 | ␉UInt8 immediate = 0;␊ |
733 | ␉UInt8 opcode = 0;␊ |
734 | ␉UInt8 type = 0;␊ |
735 | ␉␊ |
736 | ␉UInt32 segmentAddress = 0;␊ |
737 | ␉␊ |
738 | ␉UInt32 address = 0;␊ |
739 | ␉␊ |
740 | ␉SInt32 addend = 0;␉␉␉// TODO: handle this␊ |
741 | ␉SInt32 libraryOrdinal = 0;␊ |
742 | ␊ |
743 | ␉const char* symbolName = NULL;␊ |
744 | ␉UInt8 symboFlags = 0;␊ |
745 | ␉UInt32 symbolAddr = 0xFFFFFFFF;␊ |
746 | ␉␊ |
747 | ␉// Temperary variables␊ |
748 | ␉UInt8 bits = 0;␊ |
749 | ␉UInt32 tmp = 0;␊ |
750 | ␉UInt32 tmp2 = 0;␊ |
751 | ␉␊ |
752 | ␉UInt32 index = 0;␊ |
753 | ␉int done = 0;␊ |
754 | ␉unsigned int i = 0;␊ |
755 | ␉␊ |
756 | ␉while(/*!done &&*/ i < size)␊ |
757 | ␉{␊ |
758 | ␉␉immediate = bind_stream[i] & BIND_IMMEDIATE_MASK;␊ |
759 | ␉␉opcode = bind_stream[i] & BIND_OPCODE_MASK;␊ |
760 | ␉␉␊ |
761 | ␉␉␊ |
762 | ␉␉switch(opcode)␊ |
763 | ␉␉{␊ |
764 | ␉␉␉case BIND_OPCODE_DONE:␊ |
765 | ␉␉␉␉done = 1; ␊ |
766 | ␉␉␉␉break;␊ |
767 | ␉␉␉␉␊ |
768 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:␊ |
769 | ␉␉␉␉libraryOrdinal = immediate;␊ |
770 | ␉␉␉␉//DBG("BIND_OPCODE_SET_DYLIB_ORDINAL_IMM: %d\n", libraryOrdinal);␊ |
771 | ␉␉␉␉break;␊ |
772 | ␉␉␉␉␊ |
773 | ␉␉␉case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:␊ |
774 | ␉␉␉␉libraryOrdinal = 0;␊ |
775 | ␉␉␉␉bits = 0;␊ |
776 | ␉␉␉␉do␊ |
777 | ␉␉␉␉{␊ |
778 | ␉␉␉␉␉libraryOrdinal |= (bind_stream[++i] & 0x7f) << bits;␊ |
779 | ␉␉␉␉␉bits += 7;␊ |
780 | ␉␉␉␉}␊ |
781 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
782 | ␉␉␉␉␊ |
783 | ␉␉␉␉//DBG("BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB: %d\n", libraryOrdinal);␊ |
784 | ␊ |
785 | ␉␉␉␉break;␊ |
786 | ␉␉␉␉␊ |
787 | ␉␉␉case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:␊ |
788 | ␉␉␉␉// NOTE: this is wrong, fortunately we don't use it␊ |
789 | ␉␉␉␉libraryOrdinal = -immediate;␊ |
790 | ␉␉␉␉//DBG("BIND_OPCODE_SET_DYLIB_SPECIAL_IMM: %d\n", libraryOrdinal);␊ |
791 | ␊ |
792 | ␉␉␉␉break;␊ |
793 | ␉␉␉␉␊ |
794 | ␉␉␉case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:␊ |
795 | ␉␉␉␉symboFlags = immediate;␊ |
796 | ␉␉␉␉symbolName = (char*)&bind_stream[++i];␊ |
797 | ␉␉␉␉i += strlen((char*)&bind_stream[i]);␊ |
798 | ␉␉␉␉//DBG("BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM: %s, 0x%X\n", symbolName, symboFlags);␊ |
799 | ␊ |
800 | ␉␉␉␉symbolAddr = lookup_all_symbols(symbolName);␊ |
801 | ␊ |
802 | ␉␉␉␉break;␊ |
803 | ␉␉␉␉␊ |
804 | ␉␉␉case BIND_OPCODE_SET_TYPE_IMM:␊ |
805 | ␉␉␉␉// Set bind type (pointer, absolute32, pcrel32)␊ |
806 | ␉␉␉␉type = immediate;␊ |
807 | ␉␉␉␉//DBG("BIND_OPCODE_SET_TYPE_IMM: %d\n", type);␊ |
808 | ␊ |
809 | ␉␉␉␉break;␊ |
810 | ␉␉␉␉␊ |
811 | ␉␉␉case BIND_OPCODE_SET_ADDEND_SLEB:␊ |
812 | ␉␉␉␉addend = 0;␊ |
813 | ␉␉␉␉bits = 0;␊ |
814 | ␉␉␉␉do␊ |
815 | ␉␉␉␉{␊ |
816 | ␉␉␉␉␉addend |= (bind_stream[++i] & 0x7f) << bits;␊ |
817 | ␉␉␉␉␉bits += 7;␊ |
818 | ␉␉␉␉}␊ |
819 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
820 | ␉␉␉␉␊ |
821 | ␉␉␉␉if(!(bind_stream[i-1] & 0x40)) addend *= -1;␊ |
822 | ␉␉␉␉␊ |
823 | ␉␉␉␉//DBG("BIND_OPCODE_SET_ADDEND_SLEB: %d\n", addend);␊ |
824 | ␉␉␉␉break;␊ |
825 | ␉␉␉␉␊ |
826 | ␉␉␉case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:␊ |
827 | ␉␉␉␉segmentAddress = 0;␊ |
828 | ␊ |
829 | ␉␉␉␉// Locate address␊ |
830 | ␉␉␉␉struct segment_command* segCommand = NULL;␉// NOTE: 32bit only␊ |
831 | ␉␉␉␉␊ |
832 | ␉␉␉␉unsigned int binIndex = 0;␊ |
833 | ␉␉␉␉index = 0;␊ |
834 | ␉␉␉␉do␊ |
835 | ␉␉␉␉{␊ |
836 | ␉␉␉␉␉segCommand = base + sizeof(struct mach_header) + binIndex;␊ |
837 | ␉␉␉␉␉binIndex += segCommand->cmdsize;␊ |
838 | ␉␉␉␉␉index++;␊ |
839 | ␉␉␉␉}while(index <= immediate);␊ |
840 | ␉␉␉␉␊ |
841 | ␉␉␉␉segmentAddress = segCommand->fileoff;␊ |
842 | ␉␉␉␉␊ |
843 | ␉␉␉␉// Read in offset␊ |
844 | ␉␉␉␉tmp = 0;␊ |
845 | ␉␉␉␉bits = 0;␊ |
846 | ␉␉␉␉do␊ |
847 | ␉␉␉␉{␊ |
848 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
849 | ␉␉␉␉␉bits += 7;␊ |
850 | ␉␉␉␉}while(bind_stream[i] & 0x80);␊ |
851 | ␉␉␉␉␊ |
852 | ␉␉␉␉segmentAddress += tmp;␊ |
853 | ␉␉␉␉␊ |
854 | ␉␉␉␉//DBG("BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB: 0x%X\n", segmentAddress);␊ |
855 | ␉␉␉␉break;␊ |
856 | ␉␉␉␉␊ |
857 | ␉␉␉case BIND_OPCODE_ADD_ADDR_ULEB:␊ |
858 | ␉␉␉␉// Read in offset␊ |
859 | ␉␉␉␉tmp = 0;␊ |
860 | ␉␉␉␉bits = 0;␊ |
861 | ␉␉␉␉do␊ |
862 | ␉␉␉␉{␊ |
863 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
864 | ␉␉␉␉␉bits += 7;␊ |
865 | ␉␉␉␉}␊ |
866 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
867 | ␉␉␉␉␊ |
868 | ␉␉␉␉segmentAddress += tmp;␊ |
869 | ␉␉␉␉//DBG("BIND_OPCODE_ADD_ADDR_ULEB: 0x%X\n", segmentAddress);␊ |
870 | ␉␉␉␉break;␊ |
871 | ␉␉␉␉␊ |
872 | ␉␉␉case BIND_OPCODE_DO_BIND:␊ |
873 | ␉␉␉␉//DBG("BIND_OPCODE_DO_BIND\n");␊ |
874 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
875 | ␉␉␉␉{␊ |
876 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
877 | ␊ |
878 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
879 | ␉␉␉␉}␊ |
880 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
881 | ␉␉␉␉{␊ |
882 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
883 | ␉␉␉␉}␊ |
884 | ␉␉␉␉␊ |
885 | ␉␉␉␉segmentAddress += sizeof(void*);␊ |
886 | ␉␉␉␉break;␊ |
887 | ␉␉␉␉␊ |
888 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:␊ |
889 | ␉␉␉␉//DBG("BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB\n");␊ |
890 | ␉␉␉␉␊ |
891 | ␉␉␉␉␊ |
892 | ␉␉␉␉// Read in offset␊ |
893 | ␉␉␉␉tmp = 0;␊ |
894 | ␉␉␉␉bits = 0;␊ |
895 | ␉␉␉␉do␊ |
896 | ␉␉␉␉{␊ |
897 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
898 | ␉␉␉␉␉bits += 7;␊ |
899 | ␉␉␉␉}␊ |
900 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
901 | ␉␉␉␉␊ |
902 | ␉␉␉␉␊ |
903 | ␉␉␉␉␊ |
904 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
905 | ␉␉␉␉{␊ |
906 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
907 | ␊ |
908 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
909 | ␉␉␉␉}␊ |
910 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
911 | ␉␉␉␉{␊ |
912 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
913 | ␉␉␉␉}␊ |
914 | ␉␉␉␉segmentAddress += tmp + sizeof(void*);␊ |
915 | ␊ |
916 | ␉␉␉␉␊ |
917 | ␉␉␉␉break;␊ |
918 | ␉␉␉␉␊ |
919 | ␉␉␉case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:␊ |
920 | ␉␉␉␉//DBG("BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED\n");␊ |
921 | ␉␉␉␉␊ |
922 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
923 | ␉␉␉␉{␊ |
924 | ␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
925 | ␊ |
926 | ␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
927 | ␉␉␉␉}␊ |
928 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
929 | ␉␉␉␉{␊ |
930 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
931 | ␉␉␉␉}␊ |
932 | ␉␉␉␉segmentAddress += (immediate * sizeof(void*)) + sizeof(void*);␊ |
933 | ␊ |
934 | ␉␉␉␉␊ |
935 | ␉␉␉␉break;␊ |
936 | ␉␉␉␉␊ |
937 | ␉␉␉case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:␊ |
938 | ␊ |
939 | ␉␉␉␉tmp = 0;␊ |
940 | ␉␉␉␉bits = 0;␊ |
941 | ␉␉␉␉do␊ |
942 | ␉␉␉␉{␊ |
943 | ␉␉␉␉␉tmp |= (bind_stream[++i] & 0x7f) << bits;␊ |
944 | ␉␉␉␉␉bits += 7;␊ |
945 | ␉␉␉␉}␊ |
946 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
947 | ␊ |
948 | ␉␉␉␉␊ |
949 | ␉␉␉␉tmp2 = 0;␊ |
950 | ␉␉␉␉bits = 0;␊ |
951 | ␉␉␉␉do␊ |
952 | ␉␉␉␉{␊ |
953 | ␉␉␉␉␉tmp2 |= (bind_stream[++i] & 0x7f) << bits;␊ |
954 | ␉␉␉␉␉bits += 7;␊ |
955 | ␉␉␉␉}␊ |
956 | ␉␉␉␉while(bind_stream[i] & 0x80);␊ |
957 | ␉␉␉␉␊ |
958 | ␉␉␉␉␊ |
959 | ␉␉␉␉//DBG("BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB 0x%X 0x%X\n", tmp, tmp2);␊ |
960 | ␊ |
961 | ␉␉␉␉␊ |
962 | ␉␉␉␉if(symbolAddr != 0xFFFFFFFF)␊ |
963 | ␉␉␉␉{␊ |
964 | ␉␉␉␉␉for(index = 0; index < tmp; index++)␊ |
965 | ␉␉␉␉␉{␊ |
966 | ␉␉␉␉␉␉␊ |
967 | ␉␉␉␉␉␉address = segmentAddress + (UInt32)base;␊ |
968 | ␊ |
969 | ␉␉␉␉␉␉bind_location((UInt32*)address, (char*)symbolAddr, addend, BIND_TYPE_POINTER);␊ |
970 | ␉␉␉␉␉␉␊ |
971 | ␉␉␉␉␉␉segmentAddress += tmp2 + sizeof(void*);␊ |
972 | ␉␉␉␉␉}␊ |
973 | ␉␉␉␉}␊ |
974 | ␉␉␉␉else if(strcmp(symbolName, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
975 | ␉␉␉␉{␊ |
976 | ␉␉␉␉␉printf("Unable to bind symbol %s\n", symbolName);␊ |
977 | ␉␉␉␉}␊ |
978 | ␉␉␉␉␊ |
979 | ␉␉␉␉␊ |
980 | ␉␉␉␉break;␊ |
981 | ␉␉␉default:␊ |
982 | ␉␉␉␉break;␊ |
983 | ␉␉␉␉␊ |
984 | ␉␉}␊ |
985 | ␉␉i++;␊ |
986 | ␉}␊ |
987 | }␊ |
988 | ␊ |
989 | inline void rebase_location(UInt32* location, char* base, int type)␊ |
990 | {␉␊ |
991 | ␉switch(type)␊ |
992 | ␉{␊ |
993 | ␉␉case REBASE_TYPE_POINTER:␊ |
994 | ␉␉case REBASE_TYPE_TEXT_ABSOLUTE32:␊ |
995 | ␉␉␉*location += (UInt32)base;␊ |
996 | ␉␉␉break;␊ |
997 | ␉␉␉␊ |
998 | ␉␉default:␊ |
999 | ␉␉␉break;␊ |
1000 | ␉}␊ |
1001 | }␊ |
1002 | ␊ |
1003 | inline void bind_location(UInt32* location, char* value, UInt32 addend, int type)␊ |
1004 | {␉␊ |
1005 | ␉// do actual update␊ |
1006 | ␉char* newValue = value + addend;␊ |
1007 | ␊ |
1008 | ␉switch (type) {␊ |
1009 | ␉␉case BIND_TYPE_POINTER:␊ |
1010 | ␉␉case BIND_TYPE_TEXT_ABSOLUTE32:␊ |
1011 | ␉␉␉break;␊ |
1012 | ␊ |
1013 | ␉␉case BIND_TYPE_TEXT_PCREL32:␊ |
1014 | ␉␉␉newValue -= ((UInt32)location + 4);␊ |
1015 | ␊ |
1016 | ␉␉␉break;␊ |
1017 | ␉␉default:␊ |
1018 | ␉␉␉return;␊ |
1019 | ␉}␊ |
1020 | ␉*location = (UInt32)newValue;␊ |
1021 | ␉␊ |
1022 | ␊ |
1023 | }␊ |
1024 | ␊ |
1025 | /*␊ |
1026 | * add_symbol␊ |
1027 | * This function adds a symbol from a module to the list of known symbols ␊ |
1028 | * possibly change to a pointer and add this to the Symbol module so that it can␊ |
1029 | * adjust it's internal symbol list (sort) to optimize locating new symbols␊ |
1030 | * NOTE: returns the address if the symbol is "start", else returns 0xFFFFFFFF␊ |
1031 | */␊ |
1032 | long long add_symbol(char* symbol, long long addr, char is64)␊ |
1033 | {␊ |
1034 | ␉if(is64) return 0xFFFFFFFF; // Fixme␊ |
1035 | ␉␊ |
1036 | ␉// This only can handle 32bit symbols ␊ |
1037 | ␉symbolList_t* new_entry= malloc(sizeof(symbolList_t));␊ |
1038 | ␉DBG("Adding symbol %s at 0x%X\n", symbol, addr);␊ |
1039 | ␉if (new_entry) {␉␊ |
1040 | ␉␉new_entry->next = moduleSymbols;␊ |
1041 | ␉␉␊ |
1042 | ␉␉moduleSymbols = new_entry;␊ |
1043 | ␉␉␊ |
1044 | ␉␉new_entry->addr = (UInt32)addr;␊ |
1045 | ␉␉new_entry->symbol = symbol;␊ |
1046 | ␉␉return addr;␊ |
1047 | ␉}␉␊ |
1048 | ␉␊ |
1049 | ␉return 0xFFFFFFFF;␊ |
1050 | ␉␊ |
1051 | }␊ |
1052 | ␊ |
1053 | /*␊ |
1054 | * print out the information about the loaded module␊ |
1055 | */␊ |
1056 | VOID module_loaded(const char* name/*, UInt32 version, UInt32 compat*/)␊ |
1057 | {␊ |
1058 | ␉moduleList_t* new_entry = malloc(sizeof(moduleList_t));␊ |
1059 | ␉if (new_entry) {␉␊ |
1060 | ␉␉new_entry->next = loadedModules;␊ |
1061 | ␉␊ |
1062 | ␉␉loadedModules = new_entry;␊ |
1063 | ␉␊ |
1064 | ␉␉new_entry->module = (char*)name;␊ |
1065 | ␉␉//␉new_entry->version = version;␊ |
1066 | ␉␉//␉new_entry->compat = compat;␊ |
1067 | ␉}␊ |
1068 | }␊ |
1069 | ␊ |
1070 | EFI_STATUS is_module_loaded(const char* name)␊ |
1071 | {␊ |
1072 | ␉moduleList_t* entry = loadedModules;␊ |
1073 | ␉while(entry)␊ |
1074 | ␉{␊ |
1075 | ␉␉DBG("Comparing %s with %s\n", name, entry->module);␊ |
1076 | ␉␉char fullname[128];␊ |
1077 | ␉␉sprintf(fullname, "%s.dylib",name);␊ |
1078 | ␉␉if((strcmp(entry->module, name) == 0) || (strcmp(entry->module, fullname) == 0))␊ |
1079 | ␉␉{␊ |
1080 | ␉␉␉DBG("Located module %s\n", name);␊ |
1081 | ␉␉␉return EFI_SUCCESS;␊ |
1082 | ␉␉}␊ |
1083 | ␉␉else␊ |
1084 | ␉␉{␊ |
1085 | ␉␉␉entry = entry->next;␊ |
1086 | ␉␉}␊ |
1087 | ␊ |
1088 | ␉}␊ |
1089 | ␉DBG("Module %s not found\n", name);␊ |
1090 | ␊ |
1091 | ␉return EFI_NOT_FOUND;␊ |
1092 | }␊ |
1093 | ␊ |
1094 | // Look for symbols using the Smbols moduel function.␊ |
1095 | // If non are found, look through the list of module symbols␊ |
1096 | unsigned int lookup_all_symbols(const char* name)␊ |
1097 | {␊ |
1098 | ␉unsigned int addr = 0xFFFFFFFF;␊ |
1099 | ␉if(lookup_symbol && (UInt32)lookup_symbol != 0xFFFFFFFF)␊ |
1100 | ␉{␊ |
1101 | ␉␉addr = lookup_symbol(name);␊ |
1102 | ␉␉if(addr != 0xFFFFFFFF)␊ |
1103 | ␉␉{␊ |
1104 | ␉␉␉DBG("Internal symbol %s located at 0x%X\n", name, addr);␊ |
1105 | ␉␉␉return addr;␊ |
1106 | ␉␉}␊ |
1107 | ␉}␊ |
1108 | ␊ |
1109 | ␉␊ |
1110 | ␉symbolList_t* entry = moduleSymbols;␊ |
1111 | ␉while(entry)␊ |
1112 | ␉{␊ |
1113 | ␉␉if(strcmp(entry->symbol, name) == 0)␊ |
1114 | ␉␉{␊ |
1115 | ␉␉␉DBG("External symbol %s located at 0x%X\n", name, entry->addr);␊ |
1116 | ␉␉␉return entry->addr;␊ |
1117 | ␉␉}␊ |
1118 | ␉␉else␊ |
1119 | ␉␉{␊ |
1120 | ␉␉␉entry = entry->next;␊ |
1121 | ␉␉}␊ |
1122 | ␊ |
1123 | ␉}␊ |
1124 | #if DEBUG_MODULES␊ |
1125 | ␉if(strcmp(name, SYMBOL_DYLD_STUB_BINDER) != 0)␊ |
1126 | ␉{␊ |
1127 | ␉␉verbose("Unable to locate symbol %s\n", name);␊ |
1128 | ␉␉getc();␊ |
1129 | ␉}␊ |
1130 | #endif␊ |
1131 | ␉return 0xFFFFFFFF;␊ |
1132 | }␊ |
1133 | ␊ |
1134 | ␊ |
1135 | /*␊ |
1136 | * parse the symbol table␊ |
1137 | * Lookup any undefined symbols␊ |
1138 | */␊ |
1139 | ␊ |
1140 | unsigned int handle_symtable(UInt32 base, struct symtab_command* symtabCommand, long long(*symbol_handler)(char*, long long, char), char is64)␊ |
1141 | {␉␉␊ |
1142 | ␉unsigned int module_start = 0xFFFFFFFF;␊ |
1143 | ␉␊ |
1144 | ␉UInt32 symbolIndex = 0;␊ |
1145 | ␉char* symbolString = base + (char*)symtabCommand->stroff;␊ |
1146 | ␉//char* symbolTable = base + symtabCommand->symoff;␊ |
1147 | ␉if(!is64)␊ |
1148 | ␉{␊ |
1149 | ␉␉struct nlist* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
1150 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
1151 | ␉␉{␉␉␉␉␉␉␊ |
1152 | ␉␉␉if(symbolEntry->n_value)␊ |
1153 | ␉␉␉{␉␉␉␉␊ |
1154 | ␉␉␉␉if(strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0) {␊ |
1155 | ␉␉␉␉␉// Module start located. 'start' is an alias so don't register it␉␉␉␉␊ |
1156 | ␉␉␉␉␉module_start = base + symbolEntry->n_value;␊ |
1157 | ␉␉␉␉␉DBG("n_value %x module_start %x\n", (unsigned)symbolEntry->n_value, (unsigned)module_start);␊ |
1158 | ␉␉␉␉} else {␊ |
1159 | ␉␉␉␉␉symbol_handler(symbolString + symbolEntry->n_un.n_strx, (long long)base + symbolEntry->n_value, is64);␊ |
1160 | ␉␉␉␉}␊ |
1161 | ␉␉␉␉␊ |
1162 | #if HARD_DEBUG_MODULES␉␊ |
1163 | ␉␉␉␉bool isTexT = (((unsigned)symbolEntry->n_value > (unsigned)vmaddr) && ((unsigned)(vmaddr + vmsize) > (unsigned)symbolEntry->n_value ));␊ |
1164 | ␉␉␉␉printf("%s %s\n", isTexT ? "__TEXT :" : "__DATA(OR ANY) :", symbolString + symbolEntry->n_un.n_strx);␊ |
1165 | ␉␉␉␉␊ |
1166 | ␉␉␉␉if(strcmp(symbolString + symbolEntry->n_un.n_strx, "_BootHelp_txt") == 0) {␊ |
1167 | ␉␉␉␉␉long long addr = (long long)base + symbolEntry->n_value;␊ |
1168 | ␉␉␉␉␉unsigned char *BootHelp = NULL;␊ |
1169 | ␉␉␉␉␉BootHelp = (unsigned char*)(UInt32)addr;␉␉␉␉␉␊ |
1170 | ␉␉␉␉␉printf("method 1: __DATA : BootHelp_txt[0] %x\n", BootHelp[0]);␊ |
1171 | ␉␉␉␉␉␊ |
1172 | ␉␉␉␉␉long long addr2 = symbolEntry->n_value;␊ |
1173 | ␉␉␉␉␉unsigned char *BootHelp2 = NULL;␊ |
1174 | ␉␉␉␉␉BootHelp2 = (unsigned char*)(UInt32)addr2;␉␉␉␉␉␊ |
1175 | ␉␉␉␉␉printf("method 2: __DATA : BootHelp_txt[0] %x\n", BootHelp2[0]);␊ |
1176 | ␉␉␉␉}␊ |
1177 | #endif␊ |
1178 | ␉␉␉}␊ |
1179 | ␉␉␉␊ |
1180 | ␉␉␉symbolEntry++;␊ |
1181 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
1182 | ␉␉}␊ |
1183 | ␉}␊ |
1184 | ␉else␊ |
1185 | ␉{␊ |
1186 | ␉␉struct nlist_64* symbolEntry = (void*)base + symtabCommand->symoff;␊ |
1187 | ␉␉// NOTE First entry is *not* correct, but we can ignore it (i'm getting radar:// right now)␉␊ |
1188 | ␉␉while(symbolIndex < symtabCommand->nsyms)␊ |
1189 | ␉␉{␉␊ |
1190 | ␉␉␉␊ |
1191 | ␉␉␉if(strcmp(symbolString + symbolEntry->n_un.n_strx, "start") == 0) {␊ |
1192 | ␉␉␉␉// Module start located. 'start' is an alias so don't register it␉␉␉␉␊ |
1193 | ␉␉␉␉module_start = base + symbolEntry->n_value;␊ |
1194 | ␉␉␉} else {␊ |
1195 | ␉␉␉␉symbol_handler(symbolString + symbolEntry->n_un.n_strx, (long long)base + symbolEntry->n_value, is64);␊ |
1196 | ␉␉␉}␊ |
1197 | ␉␉␉␊ |
1198 | ␉␉␉symbolEntry++;␊ |
1199 | ␉␉␉symbolIndex++;␉// TODO remove␊ |
1200 | ␉␉}␊ |
1201 | ␉}␊ |
1202 | ␉␉␊ |
1203 | ␉return module_start;␊ |
1204 | ␉␊ |
1205 | }␊ |
1206 | ␊ |
1207 | ␊ |
1208 | /*␊ |
1209 | * Locate the symbol for an already loaded function and modify the beginning of␊ |
1210 | * the function to jump directly to the new one␊ |
1211 | * example: replace_function("_HelloWorld_start", &replacement_start);␊ |
1212 | */␊ |
1213 | EFI_STATUS replace_function(const char* symbol, void* newAddress)␊ |
1214 | {␊ |
1215 | ␉UInt32* jumpPointer = malloc(sizeof(UInt32*));␉ ␊ |
1216 | ␉// TODO: look into using the next four bytes of the function instead␊ |
1217 | ␉// Most functions should support this, as they probably will be at ␊ |
1218 | ␉// least 10 bytes long, but you never know, this is sligtly safer as␊ |
1219 | ␉// function can be as small as 6 bytes.␊ |
1220 | ␉UInt32 addr = lookup_all_symbols(symbol);␊ |
1221 | ␉␊ |
1222 | ␉char* binary = (char*)addr;␊ |
1223 | ␉if(addr != 0xFFFFFFFF)␊ |
1224 | ␉{␊ |
1225 | ␉␉*binary++ = 0xFF;␉// Jump␊ |
1226 | ␉␉*binary++ = 0x25;␉// Long Jump␊ |
1227 | ␉␉*((UInt32*)binary) = (UInt32)jumpPointer;␊ |
1228 | ␉␉␊ |
1229 | ␉␉*jumpPointer = (UInt32)newAddress;␊ |
1230 | ␉␉␊ |
1231 | ␉␉return EFI_SUCCESS;␊ |
1232 | ␉}␊ |
1233 | ␉␊ |
1234 | ␉return EFI_NOT_FOUND;␊ |
1235 | ␊ |
1236 | }␊ |
1237 | ␊ |
1238 | ␊ |
1239 | /* Nedded to divide 64bit numbers correctly. TODO: look into why modules need this␊ |
1240 | * And why it isn't needed when compiled into boot2␊ |
1241 | *␊ |
1242 | * In the next versions, this will be surely replaced by the Apple's libcc_kext or the meklort's klibc␊ |
1243 | */␊ |
1244 | ␊ |
1245 | uint64_t __udivdi3(uint64_t numerator, uint64_t denominator)␊ |
1246 | {␊ |
1247 | ␉uint64_t quotient = 0, qbit = 1;␊ |
1248 | ␉␊ |
1249 | ␉if (denominator)␊ |
1250 | ␉{␊ |
1251 | ␉␉while ((int64_t) denominator >= 0)␊ |
1252 | ␉␉{␊ |
1253 | ␉␉␉denominator <<= 1;␊ |
1254 | ␉␉␉qbit <<= 1;␊ |
1255 | ␉␉}␊ |
1256 | ␉␉␊ |
1257 | ␉␉while (denominator)␊ |
1258 | ␉␉{␊ |
1259 | ␉␉␉if (denominator <= numerator)␊ |
1260 | ␉␉␉{␊ |
1261 | ␉␉␉␉numerator -= denominator;␊ |
1262 | ␉␉␉␉quotient += qbit;␊ |
1263 | ␉␉␉}␊ |
1264 | ␉␉␉denominator >>= 1;␊ |
1265 | ␉␉␉qbit >>= 1;␊ |
1266 | ␉␉}␊ |
1267 | ␉␉␊ |
1268 | ␉␉return quotient;␊ |
1269 | ␉}␊ |
1270 | ␉␊ |
1271 | ␉stop("Divide by 0");␊ |
1272 | ␉return 0;␉␊ |
1273 | }␊ |
1274 | |