libdl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. * libdl.c
  3. *
  4. * Functions required for dlopen et. al.
  5. */
  6. #include <ldso.h>
  7. /* The public interfaces */
  8. void *dlopen(const char *, int) __attribute__ ((__weak__, __alias__ ("_dlopen")));
  9. int dlclose(void *) __attribute__ ((__weak__, __alias__ ("_dlclose")));
  10. void *dlsym(void *, const char *) __attribute__ ((__weak__, __alias__ ("_dlsym")));
  11. const char *dlerror(void) __attribute__ ((__weak__, __alias__ ("_dlerror")));
  12. int dladdr(void *, Dl_info *) __attribute__ ((__weak__, __alias__ ("_dladdr")));
  13. #ifdef __PIC__
  14. /* This is a real hack. We need access to the dynamic linker, but we
  15. also need to make it possible to link against this library without any
  16. unresolved externals. We provide these weak symbols to make the link
  17. possible, but at run time the normal symbols are accessed. */
  18. static void __attribute__ ((unused)) foobar(void)
  19. {
  20. const char msg[]="libdl library not correctly linked\n";
  21. _dl_write(2, msg, _dl_strlen(msg));
  22. _dl_exit(1);
  23. }
  24. static int __attribute__ ((unused)) foobar1 = (int) foobar; /* Use as pointer */
  25. extern void _dl_dprintf(int, const char *, ...) __attribute__ ((__weak__, __alias__ ("foobar")));
  26. extern char *_dl_find_hash(const char *, struct dyn_elf *, struct elf_resolve *, enum caller_type)
  27. __attribute__ ((__weak__, __alias__ ("foobar")));
  28. extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **, struct elf_resolve *, char *)
  29. __attribute__ ((__weak__, __alias__ ("foobar")));
  30. extern int _dl_fixup(struct elf_resolve *tpnt, int lazy)
  31. __attribute__ ((__weak__, __alias__ ("foobar")));
  32. extern int _dl_copy_fixups(struct dyn_elf * tpnt)
  33. __attribute__ ((__weak__, __alias__ ("foobar")));
  34. #ifdef __mips__
  35. extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt)
  36. __attribute__ ((__weak__, __alias__ ("foobar")));
  37. #endif
  38. #ifdef USE_CACHE
  39. int _dl_map_cache(void) __attribute__ ((__weak__, __alias__ ("foobar")));
  40. int _dl_unmap_cache(void) __attribute__ ((__weak__, __alias__ ("foobar")));
  41. #endif
  42. extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__, __alias__ ("foobar1")));
  43. extern struct dyn_elf *_dl_handles __attribute__ ((__weak__, __alias__ ("foobar1")));
  44. extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__, __alias__ ("foobar1")));
  45. extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__, __alias__ ("foobar1")));
  46. extern unsigned long _dl_error_number __attribute__ ((__weak__, __alias__ ("foobar1")));
  47. extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__, __alias__ ("foobar1")));
  48. #ifdef __SUPPORT_LD_DEBUG__
  49. #define _dl_debug_file 2
  50. #endif
  51. #else
  52. #ifdef __SUPPORT_LD_DEBUG__
  53. static char *_dl_debug = 0;
  54. static char *_dl_debug_symbols = 0;
  55. static char *_dl_debug_move = 0;
  56. static char *_dl_debug_reloc = 0;
  57. static char *_dl_debug_detail = 0;
  58. static char *_dl_debug_nofixups = 0;
  59. static char *_dl_debug_bindings = 0;
  60. static int _dl_debug_file = 2;
  61. #elif defined __SUPPORT_LD_DEBUG_EARLY__
  62. #define _dl_debug_file 2
  63. #endif
  64. char *_dl_library_path = 0;
  65. char *_dl_ldsopath = 0;
  66. struct r_debug *_dl_debug_addr = NULL;
  67. static char *_dl_malloc_addr, *_dl_mmap_zero;
  68. #include "../ldso/_dl_progname.h" /* Pull in the name of ld.so */
  69. #include "../ldso/hash.c"
  70. #include "../ldso/readelflib1.c"
  71. void *(*_dl_malloc_function) (size_t size);
  72. int _dl_fixup(struct elf_resolve *tpnt, int lazy);
  73. #endif
  74. static int do_dlclose(void *, int need_fini);
  75. static const char *dl_error_names[] = {
  76. "",
  77. "File not found",
  78. "Unable to open /dev/zero",
  79. "Not an ELF file",
  80. #if defined (__i386__)
  81. "Not i386 binary",
  82. #elif defined (__sparc__)
  83. "Not sparc binary",
  84. #elif defined (__mc68000__)
  85. "Not m68k binary",
  86. #else
  87. "Unrecognized binary type",
  88. #endif
  89. "Not an ELF shared library",
  90. "Unable to mmap file",
  91. "No dynamic section",
  92. #ifdef ELF_USES_RELOCA
  93. "Unable to process REL relocs",
  94. #else
  95. "Unable to process RELA relocs",
  96. #endif
  97. "Bad handle",
  98. "Unable to resolve symbol"
  99. };
  100. static void __attribute__ ((destructor)) dl_cleanup(void)
  101. {
  102. struct dyn_elf *d;
  103. for (d = _dl_handles; d; d = d->next_handle)
  104. if (d->dyn->libtype == loaded_file && d->dyn->dynamic_info[DT_FINI]) {
  105. (* ((int (*)(void)) (d->dyn->loadaddr + d->dyn->dynamic_info[DT_FINI]))) ();
  106. d->dyn->dynamic_info[DT_FINI] = 0;
  107. }
  108. }
  109. void *_dlopen(const char *libname, int flag)
  110. {
  111. struct elf_resolve *tpnt, *tfrom;
  112. struct dyn_elf *rpnt = NULL;
  113. struct dyn_elf *dyn_chain;
  114. struct dyn_elf *dpnt;
  115. static int dl_init = 0;
  116. char *from;
  117. void (*dl_brk) (void);
  118. #ifdef __PIC__
  119. int (*dl_elf_init) (void);
  120. #endif
  121. /* A bit of sanity checking... */
  122. if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
  123. _dl_error_number = LD_BAD_HANDLE;
  124. return NULL;
  125. }
  126. from = __builtin_return_address(0);
  127. /* Have the dynamic linker use the regular malloc function now */
  128. if (!dl_init) {
  129. dl_init++;
  130. _dl_malloc_function = malloc;
  131. }
  132. /* Cover the trivial case first */
  133. if (!libname)
  134. return _dl_symbol_tables;
  135. #ifdef USE_CACHE
  136. _dl_map_cache();
  137. #endif
  138. /*
  139. * Try and locate the module we were called from - we
  140. * need this so that we get the correct RPATH. Note that
  141. * this is the current behavior under Solaris, but the
  142. * ABI+ specifies that we should only use the RPATH from
  143. * the application. Thus this may go away at some time
  144. * in the future.
  145. */
  146. tfrom = NULL;
  147. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  148. tpnt = dpnt->dyn;
  149. if (tpnt->loadaddr < from
  150. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
  151. tfrom = tpnt;
  152. }
  153. if (!(tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname))) {
  154. #ifdef USE_CACHE
  155. _dl_unmap_cache();
  156. #endif
  157. return NULL;
  158. }
  159. //tpnt->libtype = loaded_file;
  160. dyn_chain = rpnt = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  161. _dl_memset(rpnt, 0, sizeof(struct dyn_elf));
  162. rpnt->dyn = tpnt;
  163. rpnt->flags = flag;
  164. if (!tpnt->symbol_scope)
  165. tpnt->symbol_scope = dyn_chain;
  166. rpnt->next_handle = _dl_handles;
  167. _dl_handles = rpnt;
  168. /*
  169. * OK, we have the requested file in memory. Now check for
  170. * any other requested files that may also be required.
  171. */
  172. {
  173. struct elf_resolve *tcurr;
  174. struct elf_resolve * tpnt1;
  175. Elf32_Dyn * dpnt;
  176. char * lpnt;
  177. tcurr = tpnt;
  178. do{
  179. for(dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++)
  180. {
  181. if(dpnt->d_tag == DT_NEEDED)
  182. {
  183. lpnt = tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  184. dpnt->d_un.d_val;
  185. if(!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpnt)))
  186. goto oops;
  187. rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  188. _dl_memset (rpnt->next, 0, sizeof (struct dyn_elf));
  189. rpnt = rpnt->next;
  190. if (!tpnt1->symbol_scope) tpnt1->symbol_scope = dyn_chain;
  191. rpnt->dyn = tpnt1;
  192. };
  193. }
  194. tcurr = tcurr->next;
  195. } while(tcurr);
  196. }
  197. /*
  198. * OK, now attach the entire chain at the end
  199. */
  200. rpnt->next = _dl_symbol_tables;
  201. /*
  202. * MIPS is special *sigh*
  203. */
  204. #ifdef __mips__
  205. _dl_perform_mips_global_got_relocations(tpnt);
  206. #endif
  207. if (_dl_fixup(tpnt, (flag & RTLD_LAZY))) {
  208. _dl_error_number = LD_NO_SYMBOL;
  209. goto oops;
  210. }
  211. if (_dl_debug_addr) {
  212. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  213. if (dl_brk != NULL) {
  214. _dl_debug_addr->r_state = RT_ADD;
  215. (*dl_brk) ();
  216. _dl_debug_addr->r_state = RT_CONSISTENT;
  217. (*dl_brk) ();
  218. }
  219. }
  220. #ifdef __PIC__
  221. /* Find the last library */
  222. for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next)
  223. ;
  224. /* Run the ctors and set up the dtors */
  225. for (; tpnt != dyn_chain->dyn->prev; tpnt=tpnt->prev)
  226. {
  227. /* Apparently crt1 for the application is responsible for handling this.
  228. * We only need to run the init/fini for shared libraries
  229. */
  230. if (tpnt->libtype == program_interpreter)
  231. continue;
  232. if (tpnt->libtype == elf_executable)
  233. continue;
  234. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  235. continue;
  236. tpnt->init_flag |= INIT_FUNCS_CALLED;
  237. if (tpnt->dynamic_info[DT_INIT]) {
  238. dl_elf_init = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  239. (*dl_elf_init) ();
  240. }
  241. if (tpnt->dynamic_info[DT_FINI]) {
  242. atexit((void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]));
  243. }
  244. }
  245. #endif
  246. #ifdef USE_CACHE
  247. _dl_unmap_cache();
  248. #endif
  249. return (void *) dyn_chain;
  250. oops:
  251. /* Something went wrong. Clean up and return NULL. */
  252. #ifdef USE_CACHE
  253. _dl_unmap_cache();
  254. #endif
  255. do_dlclose(dyn_chain, 0);
  256. return NULL;
  257. }
  258. void *_dlsym(void *vhandle, const char *name)
  259. {
  260. struct elf_resolve *tpnt, *tfrom;
  261. struct dyn_elf *handle;
  262. char *from;
  263. struct dyn_elf *rpnt;
  264. void *ret;
  265. handle = (struct dyn_elf *) vhandle;
  266. /* First of all verify that we have a real handle
  267. of some kind. Return NULL if not a valid handle. */
  268. if (handle == NULL)
  269. handle = _dl_symbol_tables;
  270. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  271. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  272. if (rpnt == handle)
  273. break;
  274. if (!rpnt) {
  275. _dl_error_number = LD_BAD_HANDLE;
  276. return NULL;
  277. }
  278. } else if (handle == RTLD_NEXT) {
  279. /*
  280. * Try and locate the module we were called from - we
  281. * need this so that we know where to start searching
  282. * from. We never pass RTLD_NEXT down into the actual
  283. * dynamic loader itself, as it doesn't know
  284. * how to properly treat it.
  285. */
  286. from = __builtin_return_address(0);
  287. tfrom = NULL;
  288. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  289. tpnt = rpnt->dyn;
  290. if (tpnt->loadaddr < from
  291. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  292. tfrom = tpnt;
  293. handle = rpnt->next;
  294. }
  295. }
  296. }
  297. ret = _dl_find_hash((char*)name, handle, NULL, copyrel);
  298. /*
  299. * Nothing found.
  300. */
  301. if (!ret)
  302. _dl_error_number = LD_NO_SYMBOL;
  303. return ret;
  304. }
  305. int _dlclose(void *vhandle)
  306. {
  307. return do_dlclose(vhandle, 1);
  308. }
  309. static int do_dlclose(void *vhandle, int need_fini)
  310. {
  311. struct dyn_elf *rpnt, *rpnt1;
  312. struct dyn_elf *spnt, *spnt1;
  313. ElfW(Phdr) *ppnt;
  314. struct elf_resolve *tpnt;
  315. int (*dl_elf_fini) (void);
  316. void (*dl_brk) (void);
  317. struct dyn_elf *handle;
  318. unsigned int end;
  319. int i = 0;
  320. handle = (struct dyn_elf *) vhandle;
  321. rpnt1 = NULL;
  322. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  323. if (rpnt == handle) {
  324. break;
  325. }
  326. rpnt1 = rpnt;
  327. }
  328. if (!rpnt) {
  329. _dl_error_number = LD_BAD_HANDLE;
  330. return 1;
  331. }
  332. /* OK, this is a valid handle - now close out the file.
  333. * We check if we need to call fini () on the handle. */
  334. spnt = need_fini ? handle : handle->next;
  335. for (; spnt; spnt = spnt1) {
  336. spnt1 = spnt->next;
  337. /* We appended the module list to the end - when we get back here,
  338. quit. The access counts were not adjusted to account for being here. */
  339. if (spnt == _dl_symbol_tables)
  340. break;
  341. if (spnt->dyn->usage_count == 1
  342. && spnt->dyn->libtype == loaded_file) {
  343. tpnt = spnt->dyn;
  344. /* Apparently crt1 for the application is responsible for handling this.
  345. * We only need to run the init/fini for shared libraries
  346. */
  347. if (tpnt->dynamic_info[DT_FINI]) {
  348. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
  349. tpnt->dynamic_info[DT_FINI]);
  350. (*dl_elf_fini) ();
  351. }
  352. }
  353. }
  354. if (rpnt1)
  355. rpnt1->next_handle = rpnt->next_handle;
  356. else
  357. _dl_handles = rpnt->next_handle;
  358. /* OK, this is a valid handle - now close out the file */
  359. for (rpnt = handle; rpnt; rpnt = rpnt1) {
  360. rpnt1 = rpnt->next;
  361. /* We appended the module list to the end - when we get back here,
  362. quit. The access counts were not adjusted to account for being here. */
  363. if (rpnt == _dl_symbol_tables)
  364. break;
  365. rpnt->dyn->usage_count--;
  366. if (rpnt->dyn->usage_count == 0
  367. && rpnt->dyn->libtype == loaded_file) {
  368. tpnt = rpnt->dyn;
  369. /* Apparently crt1 for the application is responsible for handling this.
  370. * We only need to run the init/fini for shared libraries
  371. */
  372. #if 0
  373. /* We have to do this above, before we start closing objects.
  374. * Otherwise when the needed symbols for _fini handling are
  375. * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
  376. if (tpnt->dynamic_info[DT_FINI]) {
  377. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  378. (*dl_elf_fini) ();
  379. }
  380. #endif
  381. end = 0;
  382. for (i = 0, ppnt = rpnt->dyn->ppnt;
  383. i < rpnt->dyn->n_phent; ppnt++, i++) {
  384. if (ppnt->p_type != PT_LOAD)
  385. continue;
  386. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  387. end = ppnt->p_vaddr + ppnt->p_memsz;
  388. }
  389. _dl_munmap(rpnt->dyn->loadaddr, end);
  390. /* Next, remove rpnt->dyn from the loaded_module list */
  391. if (_dl_loaded_modules == rpnt->dyn) {
  392. _dl_loaded_modules = rpnt->dyn->next;
  393. if (_dl_loaded_modules)
  394. _dl_loaded_modules->prev = 0;
  395. } else
  396. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  397. if (tpnt->next == rpnt->dyn) {
  398. tpnt->next = tpnt->next->next;
  399. if (tpnt->next)
  400. tpnt->next->prev = tpnt;
  401. break;
  402. }
  403. free(rpnt->dyn->libname);
  404. free(rpnt->dyn);
  405. }
  406. free(rpnt);
  407. }
  408. if (_dl_debug_addr) {
  409. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  410. if (dl_brk != NULL) {
  411. _dl_debug_addr->r_state = RT_DELETE;
  412. (*dl_brk) ();
  413. _dl_debug_addr->r_state = RT_CONSISTENT;
  414. (*dl_brk) ();
  415. }
  416. }
  417. return 0;
  418. }
  419. const char *_dlerror(void)
  420. {
  421. const char *retval;
  422. if (!_dl_error_number)
  423. return NULL;
  424. retval = dl_error_names[_dl_error_number];
  425. _dl_error_number = 0;
  426. return retval;
  427. }
  428. /*
  429. * Dump information to stderrr about the current loaded modules
  430. */
  431. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  432. void _dlinfo(void)
  433. {
  434. struct elf_resolve *tpnt;
  435. struct dyn_elf *rpnt, *hpnt;
  436. _dl_dprintf(2, "List of loaded modules\n");
  437. /* First start with a complete list of all of the loaded files. */
  438. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  439. _dl_dprintf(2, "\t%x %x %x %s %d %s\n",
  440. (unsigned) tpnt->loadaddr, (unsigned) tpnt,
  441. (unsigned) tpnt->symbol_scope,
  442. type[tpnt->libtype],
  443. tpnt->usage_count, tpnt->libname);
  444. }
  445. /* Next dump the module list for the application itself */
  446. _dl_dprintf(2, "\nModules for application (%x):\n",
  447. (unsigned) _dl_symbol_tables);
  448. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  449. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  450. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  451. _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt);
  452. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  453. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn,
  454. rpnt->dyn->libname);
  455. }
  456. }
  457. int _dladdr(void *__address, Dl_info * __dlip)
  458. {
  459. struct elf_resolve *pelf;
  460. struct elf_resolve *rpnt;
  461. #ifdef USE_CACHE
  462. _dl_map_cache();
  463. #endif
  464. /*
  465. * Try and locate the module address is in
  466. */
  467. pelf = NULL;
  468. #if 0
  469. _dl_dprintf(2, "dladdr( 0x%p, 0x%p )\n", __address, __dlip);
  470. #endif
  471. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  472. struct elf_resolve *tpnt;
  473. tpnt = rpnt;
  474. #if 0
  475. _dl_dprintf(2, "Module \"%s\" at 0x%p\n",
  476. tpnt->libname, tpnt->loadaddr);
  477. #endif
  478. if (tpnt->loadaddr < (char *) __address
  479. && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
  480. pelf = tpnt;
  481. }
  482. }
  483. if (!pelf) {
  484. return 0;
  485. }
  486. /*
  487. * Try and locate the symbol of address
  488. */
  489. {
  490. char *strtab;
  491. Elf32_Sym *symtab;
  492. int hn, si;
  493. int sf;
  494. int sn = 0;
  495. void *sa = 0;
  496. symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
  497. strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
  498. sf = 0;
  499. for (hn = 0; hn < pelf->nbucket; hn++) {
  500. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  501. void *symbol_addr;
  502. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  503. if (symbol_addr <= __address && (!sf || sa < symbol_addr)) {
  504. sa = symbol_addr;
  505. sn = si;
  506. sf = 1;
  507. }
  508. #if 0
  509. _dl_dprintf(2, "Symbol \"%s\" at 0x%p\n",
  510. strtab + symtab[si].st_name, symbol_addr);
  511. #endif
  512. }
  513. }
  514. if (sf) {
  515. __dlip->dli_fname = pelf->libname;
  516. __dlip->dli_fbase = pelf->loadaddr;
  517. __dlip->dli_sname = strtab + symtab[sn].st_name;
  518. __dlip->dli_saddr = sa;
  519. }
  520. return 1;
  521. }
  522. }