libdl.c 17 KB

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