libdl.c 18 KB

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