dlib.c 17 KB

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