dlib.c 17 KB

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