libdl.c 18 KB

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