libdl.c 18 KB

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