libdl.c 17 KB

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