libdl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Program to load an ELF binary on a linux system, and run it
  4. * after resolving ELF shared library symbols
  5. *
  6. * Copyright (C) 2000-2004 by Erik Andersen <andersen@codepoet.org>
  7. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  8. * David Engel, Hongjiu Lu and Mitch D'Souza
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <ldso.h>
  32. #include <stdio.h>
  33. #if defined (__LIBDL_SHARED__)
  34. /* When libdl is loaded as a shared library, we need to load in
  35. * and use a pile of symbols from ldso... */
  36. extern char *_dl_find_hash(const char *, struct dyn_elf *, struct elf_resolve *, int)
  37. __attribute__ ((__weak__));
  38. extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
  39. struct elf_resolve *, char *, int) __attribute__ ((__weak__));
  40. extern struct elf_resolve * _dl_check_if_named_library_is_loaded(const char *, int)
  41. __attribute__ ((__weak__));
  42. extern int _dl_fixup(struct dyn_elf *rpnt, int lazy)
  43. __attribute__ ((__weak__));
  44. extern void _dl_protect_relro(struct elf_resolve * tpnt)
  45. __attribute__ ((__weak__));
  46. extern int _dl_errno __attribute__ ((__weak__));
  47. extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__));
  48. extern struct dyn_elf *_dl_handles __attribute__ ((__weak__));
  49. extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__));
  50. extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__));
  51. extern unsigned long _dl_error_number __attribute__ ((__weak__));
  52. extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__));
  53. #ifdef __LDSO_CACHE_SUPPORT__
  54. int _dl_map_cache(void) __attribute__ ((__weak__));
  55. int _dl_unmap_cache(void) __attribute__ ((__weak__));
  56. #endif
  57. #ifdef __mips__
  58. extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy)
  59. __attribute__ ((__weak__));
  60. #endif
  61. #ifdef __SUPPORT_LD_DEBUG__
  62. extern char *_dl_debug __attribute__ ((__weak__));
  63. #endif
  64. #else /* __LIBDL_SHARED__ */
  65. /* When libdl is linked as a static library, we need to replace all
  66. * the symbols that otherwise would have been loaded in from ldso... */
  67. #ifdef __SUPPORT_LD_DEBUG__
  68. char *_dl_debug = 0;
  69. #endif
  70. char *_dl_library_path = 0; /* Where we look for libraries */
  71. char *_dl_ldsopath = 0; /* Location of the shared lib loader */
  72. int _dl_errno = 0; /* We can't use the real errno in ldso */
  73. size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
  74. /* This global variable is also to communicate with debuggers such as gdb. */
  75. struct r_debug *_dl_debug_addr = NULL;
  76. #define _dl_malloc malloc
  77. #include "dl-progname.h"
  78. #include "../ldso/dl-hash.c"
  79. #define _dl_trace_loaded_objects 0
  80. #include "../ldso/dl-elf.c"
  81. #endif
  82. static int do_dlclose(void *, int need_fini);
  83. static const char *dl_error_names[] = {
  84. "",
  85. "File not found",
  86. "Unable to open /dev/zero",
  87. "Not an ELF file",
  88. #if defined (__i386__)
  89. "Not i386 binary",
  90. #elif defined (__sparc__)
  91. "Not sparc binary",
  92. #elif defined (__mc68000__)
  93. "Not m68k binary",
  94. #else
  95. "Unrecognized binary type",
  96. #endif
  97. "Not an ELF shared library",
  98. "Unable to mmap file",
  99. "No dynamic section",
  100. #ifdef ELF_USES_RELOCA
  101. "Unable to process REL relocs",
  102. #else
  103. "Unable to process RELA relocs",
  104. #endif
  105. "Bad handle",
  106. "Unable to resolve symbol"
  107. };
  108. void __attribute__ ((destructor)) dl_cleanup(void)
  109. {
  110. struct dyn_elf *d;
  111. for (d = _dl_handles; d; d = d->next_handle) {
  112. do_dlclose(d, 1);
  113. }
  114. }
  115. void *dlopen(const char *libname, int flag)
  116. {
  117. struct elf_resolve *tpnt, *tfrom, *tcurr;
  118. struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr;
  119. struct dyn_elf *dpnt;
  120. ElfW(Addr) from;
  121. struct elf_resolve *tpnt1;
  122. void (*dl_brk) (void);
  123. int now_flag;
  124. struct init_fini_list *tmp, *runp;
  125. int nlist, i;
  126. struct elf_resolve **init_fini_list;
  127. /* A bit of sanity checking... */
  128. if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
  129. _dl_error_number = LD_BAD_HANDLE;
  130. return NULL;
  131. }
  132. from = (ElfW(Addr)) __builtin_return_address(0);
  133. /* Cover the trivial case first */
  134. if (!libname)
  135. return _dl_symbol_tables;
  136. _dl_map_cache();
  137. /*
  138. * Try and locate the module we were called from - we
  139. * need this so that we get the correct RPATH. Note that
  140. * this is the current behavior under Solaris, but the
  141. * ABI+ specifies that we should only use the RPATH from
  142. * the application. Thus this may go away at some time
  143. * in the future.
  144. */
  145. tfrom = NULL;
  146. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  147. tpnt = dpnt->dyn;
  148. if (tpnt->loadaddr < from
  149. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
  150. tfrom = tpnt;
  151. }
  152. for(rpnt = _dl_symbol_tables; rpnt->next; rpnt=rpnt->next);
  153. relro_ptr = rpnt;
  154. /* Try to load the specified library */
  155. #ifdef __SUPPORT_LD_DEBUG__
  156. if(_dl_debug)
  157. fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname);
  158. #endif
  159. tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0);
  160. if (!(tpnt))
  161. tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
  162. else
  163. tpnt->usage_count++;
  164. if (tpnt == NULL) {
  165. _dl_unmap_cache();
  166. return NULL;
  167. }
  168. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  169. _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
  170. dyn_chain->dyn = tpnt;
  171. tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
  172. dyn_chain->next_handle = _dl_handles;
  173. _dl_handles = dyn_ptr = dyn_chain;
  174. #ifdef __SUPPORT_LD_DEBUG__
  175. if(_dl_debug)
  176. fprintf(stderr, "Looking for needed libraries\n");
  177. #endif
  178. nlist = 0;
  179. for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
  180. {
  181. Elf32_Dyn *dpnt;
  182. char *lpntstr;
  183. nlist++;
  184. tcurr->init_fini = NULL; /* clear any previous dependcies */
  185. for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
  186. if (dpnt->d_tag == DT_NEEDED) {
  187. char *name;
  188. lpntstr = (char*) (tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  189. dpnt->d_un.d_val);
  190. name = _dl_get_last_path_component(lpntstr);
  191. tpnt1 = _dl_check_if_named_library_is_loaded(name, 0);
  192. #ifdef __SUPPORT_LD_DEBUG__
  193. if(_dl_debug)
  194. fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
  195. lpntstr, tcurr->libname);
  196. #endif
  197. if (tpnt1) {
  198. tpnt1->usage_count++;
  199. } else {
  200. tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
  201. if (!tpnt1)
  202. goto oops;
  203. }
  204. tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
  205. dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  206. _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
  207. dyn_ptr = dyn_ptr->next;
  208. dyn_ptr->dyn = tpnt1;
  209. tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
  210. tmp->tpnt = tpnt1;
  211. tmp->next = tcurr->init_fini;
  212. tcurr->init_fini = tmp;
  213. }
  214. }
  215. }
  216. init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
  217. dyn_chain->init_fini.init_fini = init_fini_list;
  218. dyn_chain->init_fini.nlist = nlist;
  219. i = 0;
  220. for (tcurr = tpnt; tcurr; tcurr = tcurr->next) {
  221. init_fini_list[i++] = tcurr;
  222. for(runp = tcurr->init_fini; runp; runp = runp->next){
  223. if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
  224. tmp = malloc(sizeof(struct init_fini_list));
  225. tmp->tpnt = runp->tpnt;
  226. tmp->next = tcurr->rtld_local;
  227. tcurr->rtld_local = tmp;
  228. }
  229. }
  230. }
  231. /* Sort the INIT/FINI list in dependency order. */
  232. for (tcurr = tpnt; tcurr; tcurr = tcurr->next) {
  233. int j, k;
  234. for (j = 0; init_fini_list[j] != tcurr; ++j)
  235. /* Empty */;
  236. for (k = j + 1; k < nlist; ++k) {
  237. struct init_fini_list *runp = init_fini_list[k]->init_fini;
  238. for (; runp; runp = runp->next) {
  239. if (runp->tpnt == tcurr) {
  240. struct elf_resolve *here = init_fini_list[k];
  241. #ifdef __SUPPORT_LD_DEBUG__
  242. if(_dl_debug)
  243. fprintf(stderr, "Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
  244. #endif
  245. for (i = (k - j); i; --i)
  246. init_fini_list[i+j] = init_fini_list[i+j-1];
  247. init_fini_list[j] = here;
  248. ++j;
  249. break;
  250. }
  251. }
  252. }
  253. }
  254. #ifdef __SUPPORT_LD_DEBUG__
  255. if(_dl_debug) {
  256. fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
  257. for (i=0;i < nlist;i++) {
  258. fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
  259. runp = init_fini_list[i]->init_fini;
  260. for ( ;runp; runp = runp->next)
  261. printf(" %s ", runp->tpnt->libname);
  262. printf("\n");
  263. }
  264. }
  265. #endif
  266. if (dyn_chain->dyn->init_flag & INIT_FUNCS_CALLED) {
  267. /* If the init and fini stuff has already been run, that means
  268. * the dlopen'd library has already been loaded, and nothing
  269. * further needs to be done. */
  270. return (void *) dyn_chain;
  271. }
  272. #ifdef __SUPPORT_LD_DEBUG__
  273. if(_dl_debug)
  274. fprintf(stderr, "Beginning dlopen relocation fixups\n");
  275. #endif
  276. /*
  277. * OK, now all of the kids are tucked into bed in their proper addresses.
  278. * Now we go through and look for REL and RELA records that indicate fixups
  279. * to the GOT tables. We need to do this in reverse order so that COPY
  280. * directives work correctly */
  281. now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
  282. if (getenv("LD_BIND_NOW"))
  283. now_flag = RTLD_NOW;
  284. #ifdef __mips__
  285. /*
  286. * Relocation of the GOT entries for MIPS have to be done
  287. * after all the libraries have been loaded.
  288. */
  289. _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
  290. #endif
  291. if (_dl_fixup(dyn_chain, now_flag))
  292. goto oops;
  293. for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
  294. if (rpnt->dyn->relro_size)
  295. _dl_protect_relro(rpnt->dyn);
  296. }
  297. /* TODO: Should we set the protections of all pages back to R/O now ? */
  298. /* Notify the debugger we have added some objects. */
  299. if (_dl_debug_addr) {
  300. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  301. if (dl_brk != NULL) {
  302. _dl_debug_addr->r_state = RT_ADD;
  303. (*dl_brk) ();
  304. _dl_debug_addr->r_state = RT_CONSISTENT;
  305. (*dl_brk) ();
  306. }
  307. }
  308. #if defined (__LIBDL_SHARED__)
  309. /* Run the ctors and setup the dtors */
  310. for (i = nlist; i; --i) {
  311. tpnt = init_fini_list[i-1];
  312. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  313. continue;
  314. tpnt->init_flag |= INIT_FUNCS_CALLED;
  315. if (tpnt->dynamic_info[DT_INIT]) {
  316. void (*dl_elf_func) (void);
  317. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  318. if (dl_elf_func && *dl_elf_func != NULL) {
  319. #ifdef __SUPPORT_LD_DEBUG__
  320. if(_dl_debug)
  321. fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
  322. #endif
  323. (*dl_elf_func) ();
  324. }
  325. }
  326. }
  327. #endif
  328. _dl_unmap_cache();
  329. return (void *) dyn_chain;
  330. oops:
  331. /* Something went wrong. Clean up and return NULL. */
  332. _dl_unmap_cache();
  333. do_dlclose(dyn_chain, 0);
  334. return NULL;
  335. }
  336. void *dlsym(void *vhandle, const char *name)
  337. {
  338. struct elf_resolve *tpnt, *tfrom;
  339. struct dyn_elf *handle;
  340. ElfW(Addr) from;
  341. struct dyn_elf *rpnt;
  342. void *ret;
  343. handle = (struct dyn_elf *) vhandle;
  344. /* First of all verify that we have a real handle
  345. of some kind. Return NULL if not a valid handle. */
  346. if (handle == NULL)
  347. handle = _dl_symbol_tables;
  348. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  349. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  350. if (rpnt == handle)
  351. break;
  352. if (!rpnt) {
  353. _dl_error_number = LD_BAD_HANDLE;
  354. return NULL;
  355. }
  356. } else if (handle == RTLD_NEXT) {
  357. /*
  358. * Try and locate the module we were called from - we
  359. * need this so that we know where to start searching
  360. * from. We never pass RTLD_NEXT down into the actual
  361. * dynamic loader itself, as it doesn't know
  362. * how to properly treat it.
  363. */
  364. from = (ElfW(Addr)) __builtin_return_address(0);
  365. tfrom = NULL;
  366. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  367. tpnt = rpnt->dyn;
  368. if (tpnt->loadaddr < from
  369. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  370. tfrom = tpnt;
  371. handle = rpnt->next;
  372. }
  373. }
  374. }
  375. ret = _dl_find_hash((char*)name, handle, NULL, 0);
  376. /*
  377. * Nothing found.
  378. */
  379. if (!ret)
  380. _dl_error_number = LD_NO_SYMBOL;
  381. return ret;
  382. }
  383. static int do_dlclose(void *vhandle, int need_fini)
  384. {
  385. struct dyn_elf *rpnt, *rpnt1;
  386. struct init_fini_list *runp, *tmp;
  387. ElfW(Phdr) *ppnt;
  388. struct elf_resolve *tpnt;
  389. int (*dl_elf_fini) (void);
  390. void (*dl_brk) (void);
  391. struct dyn_elf *handle;
  392. unsigned int end;
  393. int i = 0;
  394. handle = (struct dyn_elf *) vhandle;
  395. rpnt1 = NULL;
  396. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  397. if (rpnt == handle)
  398. break;
  399. rpnt1 = rpnt;
  400. }
  401. if (!rpnt) {
  402. _dl_error_number = LD_BAD_HANDLE;
  403. return 1;
  404. }
  405. if (rpnt1)
  406. rpnt1->next_handle = rpnt->next_handle;
  407. else
  408. _dl_handles = rpnt->next_handle;
  409. if (need_fini) {
  410. for (i = 0; i < handle->init_fini.nlist; ++i) {
  411. tpnt = handle->init_fini.init_fini[i];
  412. if (tpnt->dynamic_info[DT_FINI] && tpnt->usage_count == 1 &&
  413. !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
  414. tpnt->init_flag |= FINI_FUNCS_CALLED;
  415. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  416. #ifdef __SUPPORT_LD_DEBUG__
  417. if(_dl_debug)
  418. fprintf(stderr, "running dtors for library %s at '%x'\n", tpnt->libname, dl_elf_fini);
  419. #endif
  420. (*dl_elf_fini) ();
  421. }
  422. }
  423. }
  424. if (handle->dyn->usage_count == 1)
  425. free(handle->init_fini.init_fini);
  426. /* OK, this is a valid handle - now close out the file */
  427. for (rpnt = handle; rpnt; rpnt = rpnt->next) {
  428. tpnt = rpnt->dyn;
  429. if (--tpnt->usage_count == 0) {
  430. end = 0;
  431. for (i = 0, ppnt = tpnt->ppnt;
  432. i < tpnt->n_phent; ppnt++, i++) {
  433. if (ppnt->p_type != PT_LOAD)
  434. continue;
  435. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  436. end = ppnt->p_vaddr + ppnt->p_memsz;
  437. }
  438. _dl_munmap((void*)tpnt->loadaddr, end);
  439. /* Free elements in RTLD_LOCAL scope list */
  440. for (runp = tpnt->rtld_local; runp; runp = tmp) {
  441. tmp = runp->next;
  442. free(runp);
  443. }
  444. /* Next, remove tpnt from the loaded_module list */
  445. if (_dl_loaded_modules == tpnt) {
  446. _dl_loaded_modules = tpnt->next;
  447. if (_dl_loaded_modules)
  448. _dl_loaded_modules->prev = 0;
  449. } else
  450. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  451. if (tpnt->next == rpnt->dyn) {
  452. tpnt->next = tpnt->next->next;
  453. if (tpnt->next)
  454. tpnt->next->prev = tpnt;
  455. break;
  456. }
  457. /* Next, remove tpnt from the global symbol table list */
  458. if (_dl_symbol_tables->dyn == rpnt->dyn) {
  459. _dl_symbol_tables = rpnt->next;
  460. if (_dl_symbol_tables)
  461. _dl_symbol_tables->prev = 0;
  462. } else
  463. for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
  464. if (rpnt1->next->dyn == rpnt->dyn) {
  465. free(rpnt1->next);
  466. rpnt1->next = rpnt1->next->next;
  467. if (rpnt1->next)
  468. rpnt1->next->prev = rpnt1;
  469. break;
  470. }
  471. }
  472. free(rpnt->dyn->libname);
  473. free(rpnt->dyn);
  474. }
  475. free(rpnt);
  476. }
  477. if (_dl_debug_addr) {
  478. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  479. if (dl_brk != NULL) {
  480. _dl_debug_addr->r_state = RT_DELETE;
  481. (*dl_brk) ();
  482. _dl_debug_addr->r_state = RT_CONSISTENT;
  483. (*dl_brk) ();
  484. }
  485. }
  486. return 0;
  487. }
  488. int dlclose(void *vhandle)
  489. {
  490. return do_dlclose(vhandle, 1);
  491. }
  492. const char *dlerror(void)
  493. {
  494. const char *retval;
  495. if (!_dl_error_number)
  496. return NULL;
  497. retval = dl_error_names[_dl_error_number];
  498. _dl_error_number = 0;
  499. return retval;
  500. }
  501. /*
  502. * Dump information to stderrr about the current loaded modules
  503. */
  504. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  505. void dlinfo(void)
  506. {
  507. struct elf_resolve *tpnt;
  508. struct dyn_elf *rpnt, *hpnt;
  509. fprintf(stderr, "List of loaded modules\n");
  510. /* First start with a complete list of all of the loaded files. */
  511. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  512. fprintf(stderr, "\t%x %x %x %s %d %s\n",
  513. (unsigned) tpnt->loadaddr, (unsigned) tpnt,
  514. (unsigned) tpnt->symbol_scope,
  515. type[tpnt->libtype],
  516. tpnt->usage_count, tpnt->libname);
  517. }
  518. /* Next dump the module list for the application itself */
  519. fprintf(stderr, "\nModules for application (%x):\n",
  520. (unsigned) _dl_symbol_tables);
  521. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  522. fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  523. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  524. fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
  525. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  526. fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
  527. rpnt->dyn->libname);
  528. }
  529. }
  530. int dladdr(const void *__address, Dl_info * __info)
  531. {
  532. struct elf_resolve *pelf;
  533. struct elf_resolve *rpnt;
  534. _dl_map_cache();
  535. /*
  536. * Try and locate the module address is in
  537. */
  538. pelf = NULL;
  539. #if 0
  540. fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
  541. #endif
  542. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  543. struct elf_resolve *tpnt;
  544. tpnt = rpnt;
  545. #if 0
  546. fprintf(stderr, "Module \"%s\" at %x\n",
  547. tpnt->libname, tpnt->loadaddr);
  548. #endif
  549. if (tpnt->loadaddr < (ElfW(Addr)) __address
  550. && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
  551. pelf = tpnt;
  552. }
  553. }
  554. if (!pelf) {
  555. return 0;
  556. }
  557. /*
  558. * Try and locate the symbol of address
  559. */
  560. {
  561. char *strtab;
  562. Elf32_Sym *symtab;
  563. int hn, si;
  564. int sf;
  565. int sn = 0;
  566. ElfW(Addr) sa;
  567. sa = 0;
  568. symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
  569. strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
  570. sf = 0;
  571. for (hn = 0; hn < pelf->nbucket; hn++) {
  572. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  573. ElfW(Addr) symbol_addr;
  574. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  575. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  576. sa = symbol_addr;
  577. sn = si;
  578. sf = 1;
  579. }
  580. #if 0
  581. fprintf(stderr, "Symbol \"%s\" at %x\n",
  582. strtab + symtab[si].st_name, symbol_addr);
  583. #endif
  584. }
  585. }
  586. if (sf) {
  587. __info->dli_fname = pelf->libname;
  588. __info->dli_fbase = (void *)pelf->loadaddr;
  589. __info->dli_sname = strtab + symtab[sn].st_name;
  590. __info->dli_saddr = (void *)sa;
  591. }
  592. return 1;
  593. }
  594. }