libdl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  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 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 char *_dl_malloc_addr, *_dl_mmap_zero;
  74. #include "../ldso/_dl_progname.h" /* Pull in the name of ld.so */
  75. #include "../ldso/hash.c"
  76. #define _dl_trace_loaded_objects 0
  77. #include "../ldso/readelflib1.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);
  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))) {
  199. goto oops;
  200. }
  201. #if 1
  202. //FIXME: Enabling this is _so_ wrong....
  203. /* We need global symbol resolution for everything
  204. * in the dependent chain */
  205. dyn_chain->flags |= RTLD_GLOBAL;
  206. #endif
  207. rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  208. _dl_memset (rpnt->next, 0, sizeof (struct dyn_elf));
  209. rpnt = rpnt->next;
  210. if (!tpnt1->symbol_scope) tpnt1->symbol_scope = rpnt;
  211. rpnt->dyn = tpnt1;
  212. }
  213. }
  214. }
  215. /*
  216. * OK, now attach the entire chain at the end
  217. */
  218. rpnt->next = _dl_symbol_tables;
  219. #ifdef __mips__
  220. /*
  221. * Relocation of the GOT entries for MIPS have to be done
  222. * after all the libraries have been loaded.
  223. */
  224. _dl_perform_mips_global_got_relocations(tpnt);
  225. #endif
  226. #ifdef __SUPPORT_LD_DEBUG__
  227. if(_dl_debug)
  228. _dl_dprintf(_dl_debug_file, "Beginning dlopen relocation fixups\n");
  229. #endif
  230. /*
  231. * OK, now all of the kids are tucked into bed in their proper addresses.
  232. * Now we go through and look for REL and RELA records that indicate fixups
  233. * to the GOT tables. We need to do this in reverse order so that COPY
  234. * directives work correctly */
  235. if (_dl_fixup(dyn_chain, dyn_chain->flags))
  236. goto oops;
  237. /* TODO: Should we set the protections of all pages back to R/O now ? */
  238. /* Notify the debugger we have added some objects. */
  239. if (_dl_debug_addr) {
  240. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  241. if (dl_brk != NULL) {
  242. _dl_debug_addr->r_state = RT_ADD;
  243. (*dl_brk) ();
  244. _dl_debug_addr->r_state = RT_CONSISTENT;
  245. (*dl_brk) ();
  246. }
  247. }
  248. #if 0 //def __SUPPORT_LD_DEBUG__
  249. if(_dl_debug)
  250. _dlinfo();
  251. #endif
  252. #ifdef __PIC__
  253. /* Find the last library so we can run things in the right order */
  254. for (tpnt = dyn_chain->dyn; tpnt->next!=NULL; tpnt = tpnt->next)
  255. ;
  256. /* Run the ctors and set up the dtors */
  257. for (; tpnt != dyn_chain->dyn->prev; tpnt=tpnt->prev)
  258. {
  259. /* Apparently crt1 for the application is responsible for handling this.
  260. * We only need to run the init/fini for shared libraries
  261. */
  262. if (tpnt->libtype == program_interpreter)
  263. continue;
  264. if (tpnt->libtype == elf_executable)
  265. continue;
  266. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  267. continue;
  268. tpnt->init_flag |= INIT_FUNCS_CALLED;
  269. if (tpnt->dynamic_info[DT_INIT]) {
  270. void (*dl_elf_func) (void);
  271. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  272. if (dl_elf_func && *dl_elf_func != NULL) {
  273. #ifdef __SUPPORT_LD_DEBUG__
  274. if(_dl_debug)
  275. _dl_dprintf(2, "running ctors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
  276. #endif
  277. (*dl_elf_func) ();
  278. }
  279. }
  280. if (tpnt->dynamic_info[DT_FINI]) {
  281. void (*dl_elf_func) (void);
  282. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  283. if (dl_elf_func && *dl_elf_func != NULL) {
  284. #ifdef __SUPPORT_LD_DEBUG__
  285. if(_dl_debug)
  286. _dl_dprintf(2, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
  287. #endif
  288. atexit(dl_elf_func);
  289. }
  290. }
  291. }
  292. #endif
  293. return (void *) dyn_chain;
  294. oops:
  295. /* Something went wrong. Clean up and return NULL. */
  296. _dl_unmap_cache();
  297. do_dlclose(dyn_chain, 0);
  298. return NULL;
  299. }
  300. void *_dlsym(void *vhandle, const char *name)
  301. {
  302. struct elf_resolve *tpnt, *tfrom;
  303. struct dyn_elf *handle;
  304. ElfW(Addr) from;
  305. struct dyn_elf *rpnt;
  306. void *ret;
  307. handle = (struct dyn_elf *) vhandle;
  308. /* First of all verify that we have a real handle
  309. of some kind. Return NULL if not a valid handle. */
  310. if (handle == NULL)
  311. handle = _dl_symbol_tables;
  312. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  313. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  314. if (rpnt == handle)
  315. break;
  316. if (!rpnt) {
  317. _dl_error_number = LD_BAD_HANDLE;
  318. return NULL;
  319. }
  320. } else if (handle == RTLD_NEXT) {
  321. /*
  322. * Try and locate the module we were called from - we
  323. * need this so that we know where to start searching
  324. * from. We never pass RTLD_NEXT down into the actual
  325. * dynamic loader itself, as it doesn't know
  326. * how to properly treat it.
  327. */
  328. from = (ElfW(Addr)) __builtin_return_address(0);
  329. tfrom = NULL;
  330. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  331. tpnt = rpnt->dyn;
  332. if (tpnt->loadaddr < from
  333. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  334. tfrom = tpnt;
  335. handle = rpnt->next;
  336. }
  337. }
  338. }
  339. ret = _dl_find_hash((char*)name, handle, NULL, copyrel);
  340. /*
  341. * Nothing found.
  342. */
  343. if (!ret)
  344. _dl_error_number = LD_NO_SYMBOL;
  345. return ret;
  346. }
  347. int _dlclose(void *vhandle)
  348. {
  349. return do_dlclose(vhandle, 1);
  350. }
  351. static int do_dlclose(void *vhandle, int need_fini)
  352. {
  353. struct dyn_elf *rpnt, *rpnt1;
  354. struct dyn_elf *spnt, *spnt1;
  355. ElfW(Phdr) *ppnt;
  356. struct elf_resolve *tpnt;
  357. int (*dl_elf_fini) (void);
  358. void (*dl_brk) (void);
  359. struct dyn_elf *handle;
  360. unsigned int end;
  361. int i = 0;
  362. handle = (struct dyn_elf *) vhandle;
  363. rpnt1 = NULL;
  364. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  365. if (rpnt == handle) {
  366. break;
  367. }
  368. rpnt1 = rpnt;
  369. }
  370. if (!rpnt) {
  371. _dl_error_number = LD_BAD_HANDLE;
  372. return 1;
  373. }
  374. /* OK, this is a valid handle - now close out the file.
  375. * We check if we need to call fini () on the handle. */
  376. spnt = need_fini ? handle : handle->next;
  377. for (; spnt; spnt = spnt1) {
  378. spnt1 = spnt->next;
  379. /* We appended the module list to the end - when we get back here,
  380. quit. The access counts were not adjusted to account for being here. */
  381. if (spnt == _dl_symbol_tables)
  382. break;
  383. if (spnt->dyn->usage_count == 1
  384. && spnt->dyn->libtype == loaded_file) {
  385. tpnt = spnt->dyn;
  386. /* Apparently crt1 for the application is responsible for handling this.
  387. * We only need to run the init/fini for shared libraries
  388. */
  389. if (tpnt->dynamic_info[DT_FINI]) {
  390. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
  391. tpnt->dynamic_info[DT_FINI]);
  392. (*dl_elf_fini) ();
  393. }
  394. }
  395. }
  396. if (rpnt1)
  397. rpnt1->next_handle = rpnt->next_handle;
  398. else
  399. _dl_handles = rpnt->next_handle;
  400. /* OK, this is a valid handle - now close out the file */
  401. for (rpnt = handle; rpnt; rpnt = rpnt1) {
  402. rpnt1 = rpnt->next;
  403. /* We appended the module list to the end - when we get back here,
  404. quit. The access counts were not adjusted to account for being here. */
  405. if (rpnt == _dl_symbol_tables)
  406. break;
  407. rpnt->dyn->usage_count--;
  408. if (rpnt->dyn->usage_count == 0
  409. && rpnt->dyn->libtype == loaded_file) {
  410. tpnt = rpnt->dyn;
  411. /* Apparently crt1 for the application is responsible for handling this.
  412. * We only need to run the init/fini for shared libraries
  413. */
  414. #if 0
  415. /* We have to do this above, before we start closing objects.
  416. * Otherwise when the needed symbols for _fini handling are
  417. * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
  418. if (tpnt->dynamic_info[DT_FINI]) {
  419. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  420. (*dl_elf_fini) ();
  421. }
  422. #endif
  423. end = 0;
  424. for (i = 0, ppnt = rpnt->dyn->ppnt;
  425. i < rpnt->dyn->n_phent; ppnt++, i++) {
  426. if (ppnt->p_type != PT_LOAD)
  427. continue;
  428. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  429. end = ppnt->p_vaddr + ppnt->p_memsz;
  430. }
  431. _dl_munmap((void*)rpnt->dyn->loadaddr, end);
  432. /* Next, remove rpnt->dyn from the loaded_module list */
  433. if (_dl_loaded_modules == rpnt->dyn) {
  434. _dl_loaded_modules = rpnt->dyn->next;
  435. if (_dl_loaded_modules)
  436. _dl_loaded_modules->prev = 0;
  437. } else
  438. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  439. if (tpnt->next == rpnt->dyn) {
  440. tpnt->next = tpnt->next->next;
  441. if (tpnt->next)
  442. tpnt->next->prev = tpnt;
  443. break;
  444. }
  445. free(rpnt->dyn->libname);
  446. free(rpnt->dyn);
  447. }
  448. free(rpnt);
  449. }
  450. if (_dl_debug_addr) {
  451. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  452. if (dl_brk != NULL) {
  453. _dl_debug_addr->r_state = RT_DELETE;
  454. (*dl_brk) ();
  455. _dl_debug_addr->r_state = RT_CONSISTENT;
  456. (*dl_brk) ();
  457. }
  458. }
  459. return 0;
  460. }
  461. const char *_dlerror(void)
  462. {
  463. const char *retval;
  464. if (!_dl_error_number)
  465. return NULL;
  466. retval = dl_error_names[_dl_error_number];
  467. _dl_error_number = 0;
  468. return retval;
  469. }
  470. /*
  471. * Dump information to stderrr about the current loaded modules
  472. */
  473. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  474. void _dlinfo(void)
  475. {
  476. struct elf_resolve *tpnt;
  477. struct dyn_elf *rpnt, *hpnt;
  478. _dl_dprintf(2, "List of loaded modules\n");
  479. /* First start with a complete list of all of the loaded files. */
  480. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  481. _dl_dprintf(2, "\t%x %x %x %s %d %s\n",
  482. (unsigned) tpnt->loadaddr, (unsigned) tpnt,
  483. (unsigned) tpnt->symbol_scope,
  484. type[tpnt->libtype],
  485. tpnt->usage_count, tpnt->libname);
  486. }
  487. /* Next dump the module list for the application itself */
  488. _dl_dprintf(2, "\nModules for application (%x):\n",
  489. (unsigned) _dl_symbol_tables);
  490. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  491. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  492. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  493. _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt);
  494. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  495. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn,
  496. rpnt->dyn->libname);
  497. }
  498. }
  499. int _dladdr(void *__address, Dl_info * __dlip)
  500. {
  501. struct elf_resolve *pelf;
  502. struct elf_resolve *rpnt;
  503. _dl_map_cache();
  504. /*
  505. * Try and locate the module address is in
  506. */
  507. pelf = NULL;
  508. #if 0
  509. _dl_dprintf(2, "dladdr( %x, %x )\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 %x\n",
  516. tpnt->libname, tpnt->loadaddr);
  517. #endif
  518. if (tpnt->loadaddr < (ElfW(Addr)) __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. ElfW(Addr) sa;
  536. sa = 0;
  537. symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
  538. strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
  539. sf = 0;
  540. for (hn = 0; hn < pelf->nbucket; hn++) {
  541. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  542. ElfW(Addr) symbol_addr;
  543. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  544. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  545. sa = symbol_addr;
  546. sn = si;
  547. sf = 1;
  548. }
  549. #if 0
  550. _dl_dprintf(2, "Symbol \"%s\" at %x\n",
  551. strtab + symtab[si].st_name, symbol_addr);
  552. #endif
  553. }
  554. }
  555. if (sf) {
  556. __dlip->dli_fname = pelf->libname;
  557. __dlip->dli_fbase = (void *)pelf->loadaddr;
  558. __dlip->dli_sname = strtab + symtab[sn].st_name;
  559. __dlip->dli_saddr = (void *)sa;
  560. }
  561. return 1;
  562. }
  563. }