linker.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822
  1. /* vim: ts=4 sw=4 noexpandtab
  2. * This file is part of ToaruOS and is released under the terms
  3. * of the NCSA / University of Illinois License - see LICENSE.md
  4. * Copyright (C) 2016-2018 Kevin Lange
  5. *
  6. * ELF Dynamic Linker/Loader
  7. *
  8. * Loads ELF executables and links them at runtime to their
  9. * shared library dependencies.
  10. *
  11. * As of writing, this is a simplistic and not-fully-compliant
  12. * implementation of ELF dynamic linking. It suffers from a number
  13. * of issues, including not actually sharing libraries (there
  14. * isn't a sufficient mechanism in the kernel at the moment for
  15. * doing that - we need something with copy-on-write, preferably
  16. * an mmap-file mechanism), as well as not handling symbol
  17. * resolution correctly.
  18. *
  19. * However, it's sufficient for our purposes, and works well enough
  20. * to load Python C modules.
  21. */
  22. #include <stdlib.h>
  23. #include <stdint.h>
  24. #include <alloca.h>
  25. #include <stdio.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <sys/stat.h>
  29. #include <sys/types.h>
  30. #include <sys/sysfunc.h>
  31. #include <kernel/elf.h>
  32. void * (*_malloc)(size_t size) = malloc;
  33. void (*_free)(void * ptr) = free;
  34. #undef malloc
  35. #undef free
  36. #define malloc ld_x_malloc
  37. #define free ld_x_free
  38. uintptr_t _malloc_minimum = 0;
  39. static void * malloc(size_t size) {
  40. return _malloc(size);
  41. }
  42. static void free(void * ptr) {
  43. if ((uintptr_t)ptr < _malloc_minimum) return;
  44. _free(ptr);
  45. }
  46. /*
  47. * When the LD_DEBUG environment variable is set, TRACE_LD messages
  48. * will be printed to stderr
  49. */
  50. #define TRACE_APP_NAME "ld.so"
  51. #define TRACE_LD(...) do { if (__trace_ld) { TRACE(__VA_ARGS__); } } while (0)
  52. static int __trace_ld = 0;
  53. #include <toaru/trace.h>
  54. /*
  55. * This libraries are included in source form to avoid having
  56. * to build separate objects for them and complicate linking,
  57. * since ld is specially built as a static object.
  58. */
  59. #include "../lib/list.c"
  60. #include "../lib/hashmap.c"
  61. typedef int (*entry_point_t)(int, char *[], char**);
  62. /* Global linking state */
  63. static hashmap_t * dumb_symbol_table;
  64. static hashmap_t * glob_dat;
  65. static hashmap_t * objects_map;
  66. /* Used for dlerror */
  67. static char * last_error = NULL;
  68. typedef struct elf_object {
  69. FILE * file;
  70. /* Full copy of the header. */
  71. Elf32_Header header;
  72. char * dyn_string_table;
  73. size_t dyn_string_table_size;
  74. Elf32_Sym * dyn_symbol_table;
  75. size_t dyn_symbol_table_size;
  76. Elf32_Dyn * dynamic;
  77. Elf32_Word * dyn_hash;
  78. void (*init)(void);
  79. void (**init_array)(void);
  80. size_t init_array_size;
  81. uintptr_t base;
  82. list_t * dependencies;
  83. int loaded;
  84. } elf_t;
  85. static elf_t * _main_obj = NULL;
  86. /* Locate library for LD_LIBRARY PATH */
  87. static char * find_lib(const char * file) {
  88. /* If it was an absolute path, there's no need to find it. */
  89. if (strchr(file, '/')) return strdup(file);
  90. /* Collect the environment variable. */
  91. char * path = getenv("LD_LIBRARY_PATH");
  92. if (!path) {
  93. /* Not set - this is the default state. Should probably read from config file? */
  94. path = "/lib:/usr/lib";
  95. }
  96. /* Duplicate so we can tokenize without editing */
  97. char * xpath = strdup(path);
  98. char * p, * last;
  99. for ((p = strtok_r(xpath, ":", &last)); p; p = strtok_r(NULL, ":", &last)) {
  100. /* Go through each LD_LIBRARY_PATH entry */
  101. int r;
  102. struct stat stat_buf;
  103. /* Append the requested file to that path */
  104. char * exe = malloc(strlen(p) + strlen(file) + 2);
  105. *exe = '\0';
  106. strcat(exe, p);
  107. strcat(exe, "/");
  108. strcat(exe, file);
  109. /* See if it exists */
  110. r = stat(exe, &stat_buf);
  111. if (r != 0) {
  112. /* Nope. */
  113. free(exe);
  114. continue;
  115. }
  116. /* It exists, so this is what we want. */
  117. return exe;
  118. }
  119. free(xpath);
  120. /* No match found. */
  121. return NULL;
  122. }
  123. /* Open an object file */
  124. static elf_t * open_object(const char * path) {
  125. /* If no path (eg. dlopen(NULL)), return the main object (the executable). */
  126. if (!path) {
  127. return _main_obj;
  128. }
  129. /* If we've already opened a file with this name, return it - don't load things twice. */
  130. if (hashmap_has(objects_map, (void*)path)) {
  131. elf_t * object = hashmap_get(objects_map, (void*)path);
  132. return object;
  133. }
  134. /* Locate the library */
  135. char * file = find_lib(path);
  136. if (!file) {
  137. last_error = "Could not find library.";
  138. return NULL;
  139. }
  140. /* Open the library. */
  141. FILE * f = fopen(file, "r");
  142. /* Free the expanded path, we don't need it anymore. */
  143. free(file);
  144. /* Failed to open? Unlikely, but could mean permissions problems. */
  145. if (!f) {
  146. last_error = "Could not open library.";
  147. return NULL;
  148. }
  149. /* Initialize a fresh object object. */
  150. elf_t * object = malloc(sizeof(elf_t));
  151. memset(object, 0, sizeof(elf_t));
  152. hashmap_set(objects_map, (void*)path, object);
  153. /* Really unlikely... */
  154. if (!object) {
  155. last_error = "Could not allocate space.";
  156. return NULL;
  157. }
  158. object->file = f;
  159. /* Read the header */
  160. size_t r = fread(&object->header, sizeof(Elf32_Header), 1, object->file);
  161. /* Header failed to read? */
  162. if (!r) {
  163. last_error = "Failed to read object header.";
  164. free(object);
  165. return NULL;
  166. }
  167. /* Is this actually an ELF object? */
  168. if (object->header.e_ident[0] != ELFMAG0 ||
  169. object->header.e_ident[1] != ELFMAG1 ||
  170. object->header.e_ident[2] != ELFMAG2 ||
  171. object->header.e_ident[3] != ELFMAG3) {
  172. last_error = "Not an ELF object.";
  173. free(object);
  174. return NULL;
  175. }
  176. /* Prepare a list for tracking dependencies. */
  177. object->dependencies = list_create();
  178. return object;
  179. }
  180. /* Calculate the size of an object file by examining its phdrs */
  181. static size_t object_calculate_size(elf_t * object) {
  182. uintptr_t base_addr = 0xFFFFFFFF;
  183. uintptr_t end_addr = 0x0;
  184. size_t headers = 0;
  185. while (headers < object->header.e_phnum) {
  186. Elf32_Phdr phdr;
  187. /* Read the phdr */
  188. fseek(object->file, object->header.e_phoff + object->header.e_phentsize * headers, SEEK_SET);
  189. fread(&phdr, object->header.e_phentsize, 1, object->file);
  190. switch (phdr.p_type) {
  191. case PT_LOAD:
  192. {
  193. /* If this loads lower than our current base... */
  194. if (phdr.p_vaddr < base_addr) {
  195. base_addr = phdr.p_vaddr;
  196. }
  197. /* Or higher than our current end address... */
  198. if (phdr.p_memsz + phdr.p_vaddr > end_addr) {
  199. end_addr = phdr.p_memsz + phdr.p_vaddr;
  200. }
  201. }
  202. break;
  203. /* TODO: Do we care about other PHDR types here? */
  204. default:
  205. break;
  206. }
  207. headers++;
  208. }
  209. /* If base_addr is still -1, then no valid phdrs were found, and the object has no loaded size. */
  210. if (base_addr == 0xFFFFFFFF) return 0;
  211. return end_addr - base_addr;
  212. }
  213. /* Load an object into memory */
  214. static uintptr_t object_load(elf_t * object, uintptr_t base) {
  215. uintptr_t end_addr = 0x0;
  216. object->base = base;
  217. size_t headers = 0;
  218. while (headers < object->header.e_phnum) {
  219. Elf32_Phdr phdr;
  220. /* Read the phdr */
  221. fseek(object->file, object->header.e_phoff + object->header.e_phentsize * headers, SEEK_SET);
  222. fread(&phdr, object->header.e_phentsize, 1, object->file);
  223. switch (phdr.p_type) {
  224. case PT_LOAD:
  225. {
  226. /* Request memory to load this PHDR into */
  227. char * args[] = {(char *)(base + phdr.p_vaddr), (char *)phdr.p_memsz};
  228. sysfunc(TOARU_SYS_FUNC_MMAP, args);
  229. /* Copy the code into memory */
  230. fseek(object->file, phdr.p_offset, SEEK_SET);
  231. fread((void *)(base + phdr.p_vaddr), phdr.p_filesz, 1, object->file);
  232. /* Zero the remaining area */
  233. size_t r = phdr.p_filesz;
  234. while (r < phdr.p_memsz) {
  235. *(char *)(phdr.p_vaddr + base + r) = 0;
  236. r++;
  237. }
  238. /* If this expands our end address, be sure to update it */
  239. if (end_addr < phdr.p_vaddr + base + phdr.p_memsz) {
  240. end_addr = phdr.p_vaddr + base + phdr.p_memsz;
  241. }
  242. }
  243. break;
  244. case PT_DYNAMIC:
  245. {
  246. /* Keep a reference to the dynamic section, which is actually loaded by a PT_LOAD normally. */
  247. object->dynamic = (Elf32_Dyn *)(base + phdr.p_vaddr);
  248. }
  249. break;
  250. default:
  251. break;
  252. }
  253. headers++;
  254. }
  255. return end_addr;
  256. }
  257. /* Perform cleanup after loading */
  258. static int object_postload(elf_t * object) {
  259. /* If there is a dynamic table, parse it. */
  260. if (object->dynamic) {
  261. Elf32_Dyn * table;
  262. /* Locate string tables */
  263. table = object->dynamic;
  264. while (table->d_tag) {
  265. switch (table->d_tag) {
  266. case 4:
  267. object->dyn_hash = (Elf32_Word *)(object->base + table->d_un.d_ptr);
  268. object->dyn_symbol_table_size = object->dyn_hash[1];
  269. break;
  270. case 5: /* Dynamic String Table */
  271. object->dyn_string_table = (char *)(object->base + table->d_un.d_ptr);
  272. break;
  273. case 6: /* Dynamic Symbol Table */
  274. object->dyn_symbol_table = (Elf32_Sym *)(object->base + table->d_un.d_ptr);
  275. break;
  276. case 10: /* Size of string table */
  277. object->dyn_string_table_size = table->d_un.d_val;
  278. break;
  279. case 12: /* DT_INIT - initialization function */
  280. object->init = (void (*)(void))(table->d_un.d_ptr + object->base);
  281. break;
  282. case 25: /* DT_INIT_ARRAY - array of constructors */
  283. object->init_array = (void (**)(void))(table->d_un.d_ptr + object->base);
  284. break;
  285. case 27: /* DT_INIT_ARRAYSZ - size of the table of constructors */
  286. object->init_array_size = table->d_un.d_val / sizeof(uintptr_t);
  287. break;
  288. }
  289. table++;
  290. }
  291. /*
  292. * Read through dependencies
  293. * We have to do this separately from the above to make sure
  294. * we have the dynamic string tables loaded first, as they
  295. * are needed for the dependency names.
  296. */
  297. table = object->dynamic;
  298. while (table->d_tag) {
  299. switch (table->d_tag) {
  300. case 1:
  301. list_insert(object->dependencies, object->dyn_string_table + table->d_un.d_val);
  302. break;
  303. }
  304. table++;
  305. }
  306. }
  307. return 0;
  308. }
  309. /* Whether symbol addresses is needed for a relocation type */
  310. static int need_symbol_for_type(unsigned char type) {
  311. switch(type) {
  312. case 1:
  313. case 2:
  314. case 5:
  315. case 6:
  316. case 7:
  317. return 1;
  318. default:
  319. return 0;
  320. }
  321. }
  322. /* Apply ELF relocations */
  323. static int object_relocate(elf_t * object) {
  324. /* If there is a dynamic symbol table, load symbols */
  325. if (object->dyn_symbol_table) {
  326. Elf32_Sym * table = object->dyn_symbol_table;
  327. size_t i = 0;
  328. while (i < object->dyn_symbol_table_size) {
  329. char * symname = (char *)((uintptr_t)object->dyn_string_table + table->st_name);
  330. /* If we haven't added this symbol to our symbol table, do so now. */
  331. if (!hashmap_has(dumb_symbol_table, symname)) {
  332. if (table->st_shndx) {
  333. hashmap_set(dumb_symbol_table, symname, (void*)(table->st_value + object->base));
  334. }
  335. }
  336. table++;
  337. i++;
  338. }
  339. }
  340. /* Find relocation table */
  341. for (uintptr_t x = 0; x < object->header.e_shentsize * object->header.e_shnum; x += object->header.e_shentsize) {
  342. Elf32_Shdr shdr;
  343. /* Load section header */
  344. fseek(object->file, object->header.e_shoff + x, SEEK_SET);
  345. fread(&shdr, object->header.e_shentsize, 1, object->file);
  346. /* Relocation table found */
  347. if (shdr.sh_type == 9) {
  348. Elf32_Rel * table = (Elf32_Rel *)(shdr.sh_addr + object->base);
  349. while ((uintptr_t)table - ((uintptr_t)shdr.sh_addr + object->base) < shdr.sh_size) {
  350. unsigned int symbol = ELF32_R_SYM(table->r_info);
  351. unsigned char type = ELF32_R_TYPE(table->r_info);
  352. Elf32_Sym * sym = &object->dyn_symbol_table[symbol];
  353. /* If we need symbol for this, get it. */
  354. char * symname = NULL;
  355. uintptr_t x = sym->st_value + object->base;
  356. if (need_symbol_for_type(type) || (type == 5)) {
  357. symname = (char *)((uintptr_t)object->dyn_string_table + sym->st_name);
  358. if (symname && hashmap_has(dumb_symbol_table, symname)) {
  359. x = (uintptr_t)hashmap_get(dumb_symbol_table, symname);
  360. } else {
  361. /* This isn't fatal, but do log a message if debugging is enabled. */
  362. TRACE_LD("Symbol not found: %s", symname);
  363. x = 0x0;
  364. }
  365. }
  366. /* Relocations, symbol lookups, etc. */
  367. switch (type) {
  368. case 6: /* GLOB_DAT */
  369. if (symname && hashmap_has(glob_dat, symname)) {
  370. x = (uintptr_t)hashmap_get(glob_dat, symname);
  371. }
  372. case 7: /* JUMP_SLOT */
  373. memcpy((void *)(table->r_offset + object->base), &x, sizeof(uintptr_t));
  374. break;
  375. case 1: /* 32 */
  376. x += *((ssize_t *)(table->r_offset + object->base));
  377. memcpy((void *)(table->r_offset + object->base), &x, sizeof(uintptr_t));
  378. break;
  379. case 2: /* PC32 */
  380. x += *((ssize_t *)(table->r_offset + object->base));
  381. x -= (table->r_offset + object->base);
  382. memcpy((void *)(table->r_offset + object->base), &x, sizeof(uintptr_t));
  383. break;
  384. case 8: /* RELATIVE */
  385. x = object->base;
  386. x += *((ssize_t *)(table->r_offset + object->base));
  387. memcpy((void *)(table->r_offset + object->base), &x, sizeof(uintptr_t));
  388. break;
  389. case 5: /* COPY */
  390. memcpy((void *)(table->r_offset + object->base), (void *)x, sym->st_size);
  391. break;
  392. default:
  393. TRACE_LD("Unknown relocation type: %d", type);
  394. }
  395. table++;
  396. }
  397. }
  398. }
  399. return 0;
  400. }
  401. /* Copy relocations are special and need to be located before other relocations. */
  402. static void object_find_copy_relocations(elf_t * object) {
  403. for (uintptr_t x = 0; x < object->header.e_shentsize * object->header.e_shnum; x += object->header.e_shentsize) {
  404. Elf32_Shdr shdr;
  405. fseek(object->file, object->header.e_shoff + x, SEEK_SET);
  406. fread(&shdr, object->header.e_shentsize, 1, object->file);
  407. /* Relocation table found */
  408. if (shdr.sh_type == 9) {
  409. Elf32_Rel * table = (Elf32_Rel *)(shdr.sh_addr + object->base);
  410. while ((uintptr_t)table - ((uintptr_t)shdr.sh_addr + object->base) < shdr.sh_size) {
  411. unsigned char type = ELF32_R_TYPE(table->r_info);
  412. if (type == 5) {
  413. unsigned int symbol = ELF32_R_SYM(table->r_info);
  414. Elf32_Sym * sym = &object->dyn_symbol_table[symbol];
  415. char * symname = (char *)((uintptr_t)object->dyn_string_table + sym->st_name);
  416. hashmap_set(glob_dat, symname, (void *)table->r_offset);
  417. }
  418. table++;
  419. }
  420. }
  421. }
  422. }
  423. /* Find a symbol in a specific object. */
  424. static void * object_find_symbol(elf_t * object, const char * symbol_name) {
  425. if (!object->dyn_symbol_table) {
  426. last_error = "lib does not have a symbol table";
  427. return NULL;
  428. }
  429. Elf32_Sym * table = object->dyn_symbol_table;
  430. size_t i = 0;
  431. while (i < object->dyn_symbol_table_size) {
  432. if (!strcmp(symbol_name, (char *)((uintptr_t)object->dyn_string_table + table->st_name))) {
  433. return (void *)(table->st_value + object->base);
  434. }
  435. table++;
  436. i++;
  437. }
  438. last_error = "symbol not found in library";
  439. return NULL;
  440. }
  441. /* Fully load an object. */
  442. static void * do_actual_load(const char * filename, elf_t * lib, int flags) {
  443. (void)flags;
  444. if (!lib) {
  445. last_error = "could not open library (not found, or other failure)";
  446. TRACE_LD("could not open library");
  447. return NULL;
  448. }
  449. size_t lib_size = object_calculate_size(lib);
  450. /* Needs to be at least a page. */
  451. if (lib_size < 4096) {
  452. lib_size = 4096;
  453. }
  454. /*
  455. * Allocate space to load the library
  456. * This is where we should really be loading things into COW
  457. * but we don't have the functionality available.
  458. */
  459. uintptr_t load_addr = (uintptr_t)malloc(lib_size);
  460. object_load(lib, load_addr);
  461. /* Perform cleanup steps */
  462. object_postload(lib);
  463. /* Ensure dependencies are available */
  464. node_t * item;
  465. while ((item = list_pop(lib->dependencies))) {
  466. elf_t * _lib = open_object(item->value);
  467. if (!_lib) {
  468. /* Missing dependencies are fatal to this process, but
  469. * not to the entire application. */
  470. free((void *)load_addr);
  471. last_error = "Failed to load a dependency.";
  472. lib->loaded = 0;
  473. TRACE_LD("Failed to load object: %s", item->value);
  474. return NULL;
  475. }
  476. if (!_lib->loaded) {
  477. do_actual_load(item->value, _lib, 0);
  478. TRACE_LD("Loaded %s at 0x%x", item->value, lib->base);
  479. }
  480. }
  481. /* Perform relocations */
  482. TRACE_LD("Relocating %s", filename);
  483. object_relocate(lib);
  484. /* We're done with the file. */
  485. fclose(lib->file);
  486. /* If there was an init_array, call everything in it */
  487. if (lib->init_array) {
  488. for (size_t i = 0; i < lib->init_array_size; i++) {
  489. TRACE_LD(" 0x%x()", lib->init_array[i]);
  490. lib->init_array[i]();
  491. }
  492. }
  493. /* If the library has an init function, call that last. */
  494. if (lib->init) {
  495. lib->init();
  496. }
  497. lib->loaded = 1;
  498. /* And return an object for the loaded library */
  499. return (void *)lib;
  500. }
  501. /* exposed dlopen() method */
  502. static void * dlopen_ld(const char * filename, int flags) {
  503. TRACE_LD("dlopen(%s,0x%x)", filename, flags);
  504. elf_t * lib = open_object(filename);
  505. if (!lib) {
  506. return NULL;
  507. }
  508. if (lib->loaded) {
  509. return lib;
  510. }
  511. void * ret = do_actual_load(filename, lib, flags);
  512. if (!ret) {
  513. /* Dependency load failure, remove us from hash */
  514. TRACE_LD("Dependency load failure");
  515. hashmap_remove(objects_map, (void*)filename);
  516. }
  517. TRACE_LD("Loaded %s at 0x%x", filename, lib->base);
  518. return ret;
  519. }
  520. /* exposed dlclose() method - XXX not fully implemented */
  521. static int dlclose_ld(elf_t * lib) {
  522. /* TODO close dependencies? Make sure nothing references this. */
  523. free((void *)lib->base);
  524. return 0;
  525. }
  526. /* exposed dlerror() method */
  527. static char * dlerror_ld(void) {
  528. char * this_error = last_error;
  529. last_error = NULL;
  530. return this_error;
  531. }
  532. /* Specially used by libc */
  533. static void * _argv_value = NULL;
  534. static char * argv_value(void) {
  535. return _argv_value;
  536. }
  537. /* Exported methods (dlfcn) */
  538. typedef struct {
  539. char * name;
  540. void * symbol;
  541. } ld_exports_t;
  542. ld_exports_t ld_builtin_exports[] = {
  543. {"dlopen", dlopen_ld},
  544. {"dlsym", object_find_symbol},
  545. {"dlclose", dlclose_ld},
  546. {"dlerror", dlerror_ld},
  547. {"__get_argv", argv_value},
  548. {NULL, NULL},
  549. };
  550. int main(int argc, char * argv[]) {
  551. char * file = argv[1];
  552. size_t arg_offset = 1;
  553. if (!strcmp(argv[1], "-e")) {
  554. arg_offset = 3;
  555. file = argv[2];
  556. }
  557. _argv_value = argv+arg_offset;
  558. /* Enable tracing if requested */
  559. char * trace_ld_env = getenv("LD_DEBUG");
  560. if ((trace_ld_env && (!strcmp(trace_ld_env,"1") || !strcmp(trace_ld_env,"yes")))) {
  561. __trace_ld = 1;
  562. }
  563. /* Initialize hashmaps for symbols, GLOB_DATs, and objects */
  564. dumb_symbol_table = hashmap_create(10);
  565. glob_dat = hashmap_create(10);
  566. objects_map = hashmap_create(10);
  567. /* Setup symbols for built-in exports */
  568. ld_exports_t * ex = ld_builtin_exports;
  569. while (ex->name) {
  570. hashmap_set(dumb_symbol_table, ex->name, ex->symbol);
  571. ex++;
  572. }
  573. /* Open the requested main object */
  574. elf_t * main_obj = open_object(file);
  575. _main_obj = main_obj;
  576. if (!main_obj) {
  577. fprintf(stderr, "%s: error: failed to open object '%s'.\n", argv[0], file);
  578. return 1;
  579. }
  580. /* Load the main object */
  581. uintptr_t end_addr = object_load(main_obj, 0x0);
  582. object_postload(main_obj);
  583. object_find_copy_relocations(main_obj);
  584. /* Load library dependencies */
  585. hashmap_t * libs = hashmap_create(10);
  586. while (end_addr & 0xFFF) {
  587. end_addr++;
  588. }
  589. list_t * ctor_libs = list_create();
  590. list_t * init_libs = list_create();
  591. TRACE_LD("Loading dependencies.");
  592. node_t * item;
  593. while ((item = list_pop(main_obj->dependencies))) {
  594. while (end_addr & 0xFFF) {
  595. end_addr++;
  596. }
  597. char * lib_name = item->value;
  598. /* Reject libg.so */
  599. if (!strcmp(lib_name, "libg.so")) goto nope;
  600. elf_t * lib = open_object(lib_name);
  601. if (!lib) {
  602. fprintf(stderr, "Failed to load dependency '%s'.\n", lib_name);
  603. return 1;
  604. }
  605. hashmap_set(libs, lib_name, lib);
  606. TRACE_LD("Loading %s at 0x%x", lib_name, end_addr);
  607. end_addr = object_load(lib, end_addr);
  608. object_postload(lib);
  609. TRACE_LD("Relocating %s", lib_name);
  610. object_relocate(lib);
  611. fclose(lib->file);
  612. /* Store constructors for later execution */
  613. if (lib->init_array) {
  614. list_insert(ctor_libs, lib);
  615. }
  616. if (lib->init) {
  617. list_insert(init_libs, lib);
  618. }
  619. lib->loaded = 1;
  620. nope:
  621. free(item);
  622. }
  623. /* Relocate the main object */
  624. TRACE_LD("Relocating main object");
  625. object_relocate(main_obj);
  626. TRACE_LD("Placing heap at end");
  627. while (end_addr & 0xFFF) {
  628. end_addr++;
  629. }
  630. /* Call constructors for loaded dependencies */
  631. char * ld_no_ctors = getenv("LD_DISABLE_CTORS");
  632. if (ld_no_ctors && (!strcmp(ld_no_ctors,"1") || !strcmp(ld_no_ctors,"yes"))) {
  633. TRACE_LD("skipping ctors because LD_DISABLE_CTORS was set");
  634. } else {
  635. foreach(node, ctor_libs) {
  636. elf_t * lib = node->value;
  637. if (lib->init_array) {
  638. TRACE_LD("Executing init_array...");
  639. for (size_t i = 0; i < lib->init_array_size; i++) {
  640. TRACE_LD(" 0x%x()", lib->init_array[i]);
  641. lib->init_array[i]();
  642. }
  643. }
  644. }
  645. }
  646. foreach(node, init_libs) {
  647. elf_t * lib = node->value;
  648. lib->init();
  649. }
  650. /* If main object had constructors, call them. */
  651. if (main_obj->init_array) {
  652. for (size_t i = 0; i < main_obj->init_array_size; i++) {
  653. TRACE_LD(" 0x%x()", main_obj->init_array[i]);
  654. main_obj->init_array[i]();
  655. }
  656. }
  657. if (main_obj->init) {
  658. main_obj->init();
  659. }
  660. main_obj->loaded = 1;
  661. /* Move heap start (kind of like a weird sbrk) */
  662. {
  663. char * args[] = {(char*)end_addr};
  664. sysfunc(TOARU_SYS_FUNC_SETHEAP, args);
  665. }
  666. /* Set heap functions for later usage */
  667. if (hashmap_has(dumb_symbol_table, "malloc")) _malloc = hashmap_get(dumb_symbol_table, "malloc");
  668. if (hashmap_has(dumb_symbol_table, "free")) _free = hashmap_get(dumb_symbol_table, "free");
  669. _malloc_minimum = 0x40000000;
  670. /* Jump to the entry for the main object */
  671. TRACE_LD("Jumping to entry point");
  672. entry_point_t entry = (entry_point_t)main_obj->header.e_entry;
  673. entry(argc-arg_offset,argv+arg_offset,environ);
  674. return 0;
  675. }