libdl.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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 USE_CACHE
  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 *init_list;
  123. struct init_fini_list *tmp;
  124. /* A bit of sanity checking... */
  125. if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
  126. _dl_error_number = LD_BAD_HANDLE;
  127. return NULL;
  128. }
  129. from = (ElfW(Addr)) __builtin_return_address(0);
  130. /* Cover the trivial case first */
  131. if (!libname)
  132. return _dl_symbol_tables;
  133. _dl_map_cache();
  134. /*
  135. * Try and locate the module we were called from - we
  136. * need this so that we get the correct RPATH. Note that
  137. * this is the current behavior under Solaris, but the
  138. * ABI+ specifies that we should only use the RPATH from
  139. * the application. Thus this may go away at some time
  140. * in the future.
  141. */
  142. tfrom = NULL;
  143. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  144. tpnt = dpnt->dyn;
  145. if (tpnt->loadaddr < from
  146. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
  147. tfrom = tpnt;
  148. }
  149. for(rpnt = _dl_symbol_tables; rpnt->next; rpnt=rpnt->next);
  150. /* Try to load the specified library */
  151. #ifdef __SUPPORT_LD_DEBUG__
  152. if(_dl_debug)
  153. fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname);
  154. #endif
  155. tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0);
  156. if (!(tpnt))
  157. tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
  158. else
  159. tpnt->usage_count++;
  160. if (tpnt == NULL) {
  161. _dl_unmap_cache();
  162. return NULL;
  163. }
  164. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  165. _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
  166. dyn_chain->dyn = tpnt;
  167. tpnt->rtld_flags |= RTLD_GLOBAL;
  168. dyn_chain->next_handle = _dl_handles;
  169. _dl_handles = dyn_ptr = dyn_chain;
  170. #ifdef __SUPPORT_LD_DEBUG__
  171. if(_dl_debug)
  172. fprintf(stderr, "Looking for needed libraries\n");
  173. #endif
  174. init_list = NULL;
  175. tmp = malloc(sizeof(struct init_fini_list));
  176. tmp->tpnt = tpnt;
  177. tmp->next = NULL;
  178. tmp->prev = init_list;
  179. init_list = tmp;
  180. dyn_chain->init_fini = init_list;
  181. for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
  182. {
  183. Elf32_Dyn *dpnt;
  184. char *lpntstr;
  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. dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  198. _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
  199. dyn_ptr = dyn_ptr->next;
  200. dyn_ptr->dyn = tpnt1;
  201. if (!tpnt1) {
  202. tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0);
  203. dyn_ptr->dyn = tpnt1;
  204. if (!tpnt1)
  205. goto oops;
  206. tpnt1->rtld_flags |= RTLD_GLOBAL;
  207. } else {
  208. tpnt1->rtld_flags |= RTLD_GLOBAL;
  209. tpnt1->usage_count++;
  210. }
  211. tmp = malloc(sizeof(struct init_fini_list));
  212. tmp->tpnt = tpnt1;
  213. tmp->next = NULL;
  214. tmp->prev = init_list;
  215. init_list->next = tmp;
  216. init_list = init_list->next;;
  217. }
  218. }
  219. }
  220. if (dyn_chain->dyn->init_flag & INIT_FUNCS_CALLED) {
  221. /* If the init and fini stuff has already been run, that means
  222. * the dlopen'd library has already been loaded, and nothing
  223. * further needs to be done. */
  224. return (void *) dyn_chain;
  225. }
  226. #ifdef __mips__
  227. /*
  228. * Relocation of the GOT entries for MIPS have to be done
  229. * after all the libraries have been loaded.
  230. */
  231. _dl_perform_mips_global_got_relocations(tpnt);
  232. #endif
  233. #ifdef __SUPPORT_LD_DEBUG__
  234. if(_dl_debug)
  235. fprintf(stderr, "Beginning dlopen relocation fixups\n");
  236. #endif
  237. /*
  238. * OK, now all of the kids are tucked into bed in their proper addresses.
  239. * Now we go through and look for REL and RELA records that indicate fixups
  240. * to the GOT tables. We need to do this in reverse order so that COPY
  241. * directives work correctly */
  242. now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
  243. if (getenv("LD_BIND_NOW"))
  244. now_flag = RTLD_NOW;
  245. if (_dl_fixup(dyn_chain, now_flag))
  246. goto oops;
  247. /* TODO: Should we set the protections of all pages back to R/O now ? */
  248. /* Notify the debugger we have added some objects. */
  249. if (_dl_debug_addr) {
  250. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  251. if (dl_brk != NULL) {
  252. _dl_debug_addr->r_state = RT_ADD;
  253. (*dl_brk) ();
  254. _dl_debug_addr->r_state = RT_CONSISTENT;
  255. (*dl_brk) ();
  256. }
  257. }
  258. #if defined (__LIBDL_SHARED__)
  259. /* Run the ctors and setup the dtors */
  260. for (; init_list; init_list = init_list->prev) {
  261. /* Apparently crt1 for the application is responsible for handling this.
  262. * We only need to run the init/fini for shared libraries. */
  263. tpnt = init_list->tpnt;
  264. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  265. continue;
  266. tpnt->init_flag |= INIT_FUNCS_CALLED;
  267. if (tpnt->dynamic_info[DT_INIT]) {
  268. void (*dl_elf_func) (void);
  269. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  270. if (dl_elf_func && *dl_elf_func != NULL) {
  271. #ifdef __SUPPORT_LD_DEBUG__
  272. if(_dl_debug)
  273. fprintf(stderr, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
  274. #endif
  275. (*dl_elf_func) ();
  276. }
  277. }
  278. }
  279. #endif
  280. return (void *) dyn_chain;
  281. oops:
  282. /* Something went wrong. Clean up and return NULL. */
  283. _dl_unmap_cache();
  284. do_dlclose(dyn_chain, 0);
  285. return NULL;
  286. }
  287. void *dlsym(void *vhandle, const char *name)
  288. {
  289. struct elf_resolve *tpnt, *tfrom;
  290. struct dyn_elf *handle;
  291. ElfW(Addr) from;
  292. struct dyn_elf *rpnt;
  293. void *ret;
  294. handle = (struct dyn_elf *) vhandle;
  295. /* First of all verify that we have a real handle
  296. of some kind. Return NULL if not a valid handle. */
  297. if (handle == NULL)
  298. handle = _dl_symbol_tables;
  299. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  300. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  301. if (rpnt == handle)
  302. break;
  303. if (!rpnt) {
  304. _dl_error_number = LD_BAD_HANDLE;
  305. return NULL;
  306. }
  307. } else if (handle == RTLD_NEXT) {
  308. /*
  309. * Try and locate the module we were called from - we
  310. * need this so that we know where to start searching
  311. * from. We never pass RTLD_NEXT down into the actual
  312. * dynamic loader itself, as it doesn't know
  313. * how to properly treat it.
  314. */
  315. from = (ElfW(Addr)) __builtin_return_address(0);
  316. tfrom = NULL;
  317. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  318. tpnt = rpnt->dyn;
  319. if (tpnt->loadaddr < from
  320. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  321. tfrom = tpnt;
  322. handle = rpnt->next;
  323. }
  324. }
  325. }
  326. ret = _dl_find_hash((char*)name, handle, 0);
  327. /*
  328. * Nothing found.
  329. */
  330. if (!ret)
  331. _dl_error_number = LD_NO_SYMBOL;
  332. return ret;
  333. }
  334. static int do_dlclose(void *vhandle, int need_fini)
  335. {
  336. struct dyn_elf *rpnt, *rpnt1;
  337. ElfW(Phdr) *ppnt;
  338. struct elf_resolve *tpnt;
  339. int (*dl_elf_fini) (void);
  340. void (*dl_brk) (void);
  341. struct dyn_elf *handle;
  342. unsigned int end;
  343. int i = 0;
  344. struct init_fini_list *fini_list, *tmp;
  345. handle = (struct dyn_elf *) vhandle;
  346. rpnt1 = NULL;
  347. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  348. if (rpnt == handle)
  349. break;
  350. rpnt1 = rpnt;
  351. }
  352. if (!rpnt) {
  353. _dl_error_number = LD_BAD_HANDLE;
  354. return 1;
  355. }
  356. if (rpnt1)
  357. rpnt1->next_handle = rpnt->next_handle;
  358. else
  359. _dl_handles = rpnt->next_handle;
  360. if (need_fini) {
  361. for (fini_list = handle->init_fini; fini_list; ) {
  362. tpnt = fini_list->tpnt;
  363. tmp = NULL;
  364. if (tpnt->dynamic_info[DT_FINI] && tpnt->usage_count == 1 &&
  365. !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
  366. tpnt->init_flag |= FINI_FUNCS_CALLED;
  367. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  368. #ifdef __SUPPORT_LD_DEBUG__
  369. if(_dl_debug)
  370. fprintf(stderr, "running dtors for library %s at '%x'\n", tpnt->libname, dl_elf_fini);
  371. #endif
  372. (*dl_elf_fini) ();
  373. tmp = fini_list;
  374. }
  375. fini_list = fini_list->next;
  376. free(tmp);
  377. }
  378. }
  379. /* OK, this is a valid handle - now close out the file */
  380. for (rpnt = handle; rpnt; rpnt = rpnt->next) {
  381. tpnt = rpnt->dyn;
  382. if (--tpnt->usage_count == 0) {
  383. end = 0;
  384. for (i = 0, ppnt = tpnt->ppnt;
  385. i < tpnt->n_phent; ppnt++, i++) {
  386. if (ppnt->p_type != PT_LOAD)
  387. continue;
  388. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  389. end = ppnt->p_vaddr + ppnt->p_memsz;
  390. }
  391. _dl_munmap((void*)tpnt->loadaddr, end);
  392. /* Next, remove tpnt from the loaded_module list */
  393. if (_dl_loaded_modules == tpnt) {
  394. _dl_loaded_modules = tpnt->next;
  395. if (_dl_loaded_modules)
  396. _dl_loaded_modules->prev = 0;
  397. } else
  398. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  399. if (tpnt->next == rpnt->dyn) {
  400. tpnt->next = tpnt->next->next;
  401. if (tpnt->next)
  402. tpnt->next->prev = tpnt;
  403. break;
  404. }
  405. /* Next, remove tpnt from the global symbol table list */
  406. if (_dl_symbol_tables->dyn == rpnt->dyn) {
  407. _dl_symbol_tables = rpnt->next;
  408. if (_dl_symbol_tables)
  409. _dl_symbol_tables->prev = 0;
  410. } else
  411. for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
  412. if (rpnt1->next->dyn == rpnt->dyn) {
  413. free(rpnt1->next);
  414. rpnt1->next = rpnt1->next->next;
  415. if (rpnt1->next)
  416. rpnt1->next->prev = rpnt1;
  417. break;
  418. }
  419. }
  420. free(rpnt->dyn->libname);
  421. free(rpnt->dyn);
  422. }
  423. free(rpnt);
  424. }
  425. if (_dl_debug_addr) {
  426. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  427. if (dl_brk != NULL) {
  428. _dl_debug_addr->r_state = RT_DELETE;
  429. (*dl_brk) ();
  430. _dl_debug_addr->r_state = RT_CONSISTENT;
  431. (*dl_brk) ();
  432. }
  433. }
  434. return 0;
  435. }
  436. int dlclose(void *vhandle)
  437. {
  438. return do_dlclose(vhandle, 1);
  439. }
  440. const char *dlerror(void)
  441. {
  442. const char *retval;
  443. if (!_dl_error_number)
  444. return NULL;
  445. retval = dl_error_names[_dl_error_number];
  446. _dl_error_number = 0;
  447. return retval;
  448. }
  449. /*
  450. * Dump information to stderrr about the current loaded modules
  451. */
  452. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  453. void dlinfo(void)
  454. {
  455. struct elf_resolve *tpnt;
  456. struct dyn_elf *rpnt, *hpnt;
  457. fprintf(stderr, "List of loaded modules\n");
  458. /* First start with a complete list of all of the loaded files. */
  459. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  460. fprintf(stderr, "\t%x %x %x %s %d %s\n",
  461. (unsigned) tpnt->loadaddr, (unsigned) tpnt,
  462. (unsigned) tpnt->symbol_scope,
  463. type[tpnt->libtype],
  464. tpnt->usage_count, tpnt->libname);
  465. }
  466. /* Next dump the module list for the application itself */
  467. fprintf(stderr, "\nModules for application (%x):\n",
  468. (unsigned) _dl_symbol_tables);
  469. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  470. fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  471. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  472. fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
  473. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  474. fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn,
  475. rpnt->dyn->libname);
  476. }
  477. }
  478. int dladdr(const void *__address, Dl_info * __info)
  479. {
  480. struct elf_resolve *pelf;
  481. struct elf_resolve *rpnt;
  482. _dl_map_cache();
  483. /*
  484. * Try and locate the module address is in
  485. */
  486. pelf = NULL;
  487. #if 0
  488. fprintf(stderr, "dladdr( %x, %x )\n", __address, __info);
  489. #endif
  490. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  491. struct elf_resolve *tpnt;
  492. tpnt = rpnt;
  493. #if 0
  494. fprintf(stderr, "Module \"%s\" at %x\n",
  495. tpnt->libname, tpnt->loadaddr);
  496. #endif
  497. if (tpnt->loadaddr < (ElfW(Addr)) __address
  498. && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
  499. pelf = tpnt;
  500. }
  501. }
  502. if (!pelf) {
  503. return 0;
  504. }
  505. /*
  506. * Try and locate the symbol of address
  507. */
  508. {
  509. char *strtab;
  510. Elf32_Sym *symtab;
  511. int hn, si;
  512. int sf;
  513. int sn = 0;
  514. ElfW(Addr) sa;
  515. sa = 0;
  516. symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
  517. strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
  518. sf = 0;
  519. for (hn = 0; hn < pelf->nbucket; hn++) {
  520. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  521. ElfW(Addr) symbol_addr;
  522. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  523. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  524. sa = symbol_addr;
  525. sn = si;
  526. sf = 1;
  527. }
  528. #if 0
  529. fprintf(stderr, "Symbol \"%s\" at %x\n",
  530. strtab + symtab[si].st_name, symbol_addr);
  531. #endif
  532. }
  533. }
  534. if (sf) {
  535. __info->dli_fname = pelf->libname;
  536. __info->dli_fbase = (void *)pelf->loadaddr;
  537. __info->dli_sname = strtab + symtab[sn].st_name;
  538. __info->dli_saddr = (void *)sa;
  539. }
  540. return 1;
  541. }
  542. }