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 int _dl_fixup(struct elf_resolve *tpnt, int lazy)
  32. __attribute__ ((__weak__, __alias__ ("foobar")));
  33. extern int _dl_copy_fixups(struct dyn_elf * tpnt)
  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. #define _dl_debug_file 2
  51. #endif
  52. #else
  53. #ifdef __SUPPORT_LD_DEBUG__
  54. static char *_dl_debug = 0;
  55. static char *_dl_debug_symbols = 0;
  56. static char *_dl_debug_move = 0;
  57. static char *_dl_debug_reloc = 0;
  58. static char *_dl_debug_detail = 0;
  59. static char *_dl_debug_nofixups = 0;
  60. static char *_dl_debug_bindings = 0;
  61. static int _dl_debug_file = 2;
  62. #elif defined __SUPPORT_LD_DEBUG_EARLY__
  63. #define _dl_debug_file 2
  64. #endif
  65. char *_dl_library_path = 0;
  66. char *_dl_ldsopath = 0;
  67. struct r_debug *_dl_debug_addr = NULL;
  68. static char *_dl_malloc_addr, *_dl_mmap_zero;
  69. #include "../ldso/_dl_progname.h" /* Pull in the name of ld.so */
  70. #include "../ldso/hash.c"
  71. #include "../ldso/readelflib1.c"
  72. void *(*_dl_malloc_function) (size_t size);
  73. int _dl_fixup(struct elf_resolve *tpnt, int lazy);
  74. #endif
  75. static int do_dlclose(void *, int need_fini);
  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, *tcurr;
  113. struct dyn_elf *dyn_chain, *rpnt = NULL;
  114. struct dyn_elf *dpnt;
  115. static int dl_init = 0;
  116. ElfW(Addr) from;
  117. struct elf_resolve *tpnt1;
  118. void (*dl_brk) (void);
  119. /* A bit of sanity checking... */
  120. if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
  121. _dl_error_number = LD_BAD_HANDLE;
  122. return NULL;
  123. }
  124. from = (ElfW(Addr)) __builtin_return_address(0);
  125. /* Have the dynamic linker use the regular malloc function now */
  126. if (!dl_init) {
  127. dl_init++;
  128. _dl_malloc_function = malloc;
  129. }
  130. /* Cover the trivial case first */
  131. if (!libname)
  132. return _dl_symbol_tables;
  133. _dl_map_cache();
  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. /* Try to load the specified library */
  150. #ifdef __SUPPORT_LD_DEBUG__
  151. _dl_dprintf(_dl_debug_file, "Trying to dlopen '%s'\n", (char*)libname);
  152. #endif
  153. if (!(tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname))) {
  154. _dl_unmap_cache();
  155. return NULL;
  156. }
  157. //tpnt->libtype = loaded_file;
  158. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  159. _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
  160. dyn_chain->dyn = tpnt;
  161. dyn_chain->flags = flag;
  162. if (!tpnt->symbol_scope)
  163. tpnt->symbol_scope = dyn_chain;
  164. dyn_chain->next_handle = _dl_handles;
  165. _dl_handles = rpnt = dyn_chain;
  166. #ifdef __SUPPORT_LD_DEBUG__
  167. _dl_dprintf(_dl_debug_file, "Looking for needed libraries\n");
  168. #endif
  169. for (tcurr = tpnt; tcurr; tcurr = tcurr->next)
  170. {
  171. Elf32_Dyn *dpnt;
  172. char *lpntstr;
  173. const char *libname1, *ptr;
  174. for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++) {
  175. if (dpnt->d_tag == DT_NEEDED) {
  176. lpntstr = (char*)tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  177. dpnt->d_un.d_val;
  178. /* Skip over any initial initial './' and '/' stuff to
  179. * get the short form libname with no path garbage */
  180. libname1 = lpntstr;
  181. ptr = _dl_strrchr(libname1, '/');
  182. if (ptr) {
  183. libname1 = ptr + 1;
  184. }
  185. /* Linked with glibc? */
  186. if (_dl_strcmp(libname1, "libc.so.6") == 0) {
  187. _dl_dprintf(2, "\tERROR: %s is linked with GNU libc!\n",
  188. tcurr->libname);
  189. goto oops;
  190. }
  191. #ifdef __SUPPORT_LD_DEBUG__
  192. _dl_dprintf(_dl_debug_file, "Trying to load '%s', needed by '%s'\n",
  193. lpntstr, tcurr->libname);
  194. #endif
  195. if (!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr))) {
  196. goto oops;
  197. }
  198. /* We need global symbol resolution for everything
  199. * in the dependent chain */
  200. dyn_chain->flags |= RTLD_GLOBAL;
  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. _dl_dprintf(_dl_debug_file, "Beginning dlopen relocation fixups\n");
  222. #endif
  223. /*
  224. * OK, now all of the kids are tucked into bed in their proper addresses.
  225. * Now we go through and look for REL and RELA records that indicate fixups
  226. * to the GOT tables. We need to do this in reverse order so that COPY
  227. * directives work correctly */
  228. if (_dl_fixup(dyn_chain->dyn, (dyn_chain->flags & RTLD_LAZY)))
  229. goto oops;
  230. #ifdef __SUPPORT_LD_DEBUG__
  231. _dl_dprintf(_dl_debug_file, "Beginning dlopen copy fixups\n");
  232. #endif
  233. if (_dl_symbol_tables) {
  234. if (_dl_copy_fixups(dyn_chain))
  235. goto oops;
  236. }
  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. _dl_debug_addr->r_state = RT_ADD;
  240. if (_dl_debug_addr) {
  241. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  242. if (dl_brk != NULL) {
  243. _dl_debug_addr->r_state = RT_ADD;
  244. (*dl_brk) ();
  245. _dl_debug_addr->r_state = RT_CONSISTENT;
  246. (*dl_brk) ();
  247. }
  248. }
  249. #ifdef __SUPPORT_LD_DEBUG__
  250. _dlinfo();
  251. #endif
  252. #ifdef __PIC__
  253. /* Find the last library so we can run things in the right order */
  254. for (rpnt = _dl_symbol_tables; rpnt!=NULL&& rpnt->next!=NULL; rpnt=rpnt->next)
  255. ;
  256. /* Run the ctors and set up the dtors */
  257. for (;rpnt!=NULL; rpnt=rpnt->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. tpnt = rpnt->dyn;
  263. if (tpnt->libtype == program_interpreter)
  264. continue;
  265. if (tpnt->libtype == elf_executable)
  266. continue;
  267. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  268. continue;
  269. tpnt->init_flag |= INIT_FUNCS_CALLED;
  270. if (tpnt->dynamic_info[DT_INIT]) {
  271. void (*dl_elf_func) (void);
  272. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  273. if (dl_elf_func && *dl_elf_func != NULL) {
  274. #ifdef __SUPPORT_LD_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. _dl_dprintf(2, "setting up dtors for library %s at '%x'\n", tpnt->libname, dl_elf_func);
  286. #endif
  287. atexit(dl_elf_func);
  288. }
  289. }
  290. }
  291. #endif
  292. return (void *) dyn_chain;
  293. oops:
  294. /* Something went wrong. Clean up and return NULL. */
  295. _dl_unmap_cache();
  296. do_dlclose(dyn_chain, 0);
  297. return NULL;
  298. }
  299. void *_dlsym(void *vhandle, const char *name)
  300. {
  301. struct elf_resolve *tpnt, *tfrom;
  302. struct dyn_elf *handle;
  303. ElfW(Addr) from;
  304. struct dyn_elf *rpnt;
  305. void *ret;
  306. handle = (struct dyn_elf *) vhandle;
  307. /* First of all verify that we have a real handle
  308. of some kind. Return NULL if not a valid handle. */
  309. if (handle == NULL)
  310. handle = _dl_symbol_tables;
  311. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  312. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  313. if (rpnt == handle)
  314. break;
  315. if (!rpnt) {
  316. _dl_error_number = LD_BAD_HANDLE;
  317. return NULL;
  318. }
  319. } else if (handle == RTLD_NEXT) {
  320. /*
  321. * Try and locate the module we were called from - we
  322. * need this so that we know where to start searching
  323. * from. We never pass RTLD_NEXT down into the actual
  324. * dynamic loader itself, as it doesn't know
  325. * how to properly treat it.
  326. */
  327. from = (ElfW(Addr)) __builtin_return_address(0);
  328. tfrom = NULL;
  329. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  330. tpnt = rpnt->dyn;
  331. if (tpnt->loadaddr < from
  332. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  333. tfrom = tpnt;
  334. handle = rpnt->next;
  335. }
  336. }
  337. }
  338. ret = _dl_find_hash((char*)name, handle, NULL, copyrel);
  339. /*
  340. * Nothing found.
  341. */
  342. if (!ret)
  343. _dl_error_number = LD_NO_SYMBOL;
  344. return ret;
  345. }
  346. int _dlclose(void *vhandle)
  347. {
  348. return do_dlclose(vhandle, 1);
  349. }
  350. static int do_dlclose(void *vhandle, int need_fini)
  351. {
  352. struct dyn_elf *rpnt, *rpnt1;
  353. struct dyn_elf *spnt, *spnt1;
  354. ElfW(Phdr) *ppnt;
  355. struct elf_resolve *tpnt;
  356. int (*dl_elf_fini) (void);
  357. void (*dl_brk) (void);
  358. struct dyn_elf *handle;
  359. unsigned int end;
  360. int i = 0;
  361. handle = (struct dyn_elf *) vhandle;
  362. rpnt1 = NULL;
  363. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  364. if (rpnt == handle) {
  365. break;
  366. }
  367. rpnt1 = rpnt;
  368. }
  369. if (!rpnt) {
  370. _dl_error_number = LD_BAD_HANDLE;
  371. return 1;
  372. }
  373. /* OK, this is a valid handle - now close out the file.
  374. * We check if we need to call fini () on the handle. */
  375. spnt = need_fini ? handle : handle->next;
  376. for (; spnt; spnt = spnt1) {
  377. spnt1 = spnt->next;
  378. /* We appended the module list to the end - when we get back here,
  379. quit. The access counts were not adjusted to account for being here. */
  380. if (spnt == _dl_symbol_tables)
  381. break;
  382. if (spnt->dyn->usage_count == 1
  383. && spnt->dyn->libtype == loaded_file) {
  384. tpnt = spnt->dyn;
  385. /* Apparently crt1 for the application is responsible for handling this.
  386. * We only need to run the init/fini for shared libraries
  387. */
  388. if (tpnt->dynamic_info[DT_FINI]) {
  389. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
  390. tpnt->dynamic_info[DT_FINI]);
  391. (*dl_elf_fini) ();
  392. }
  393. }
  394. }
  395. if (rpnt1)
  396. rpnt1->next_handle = rpnt->next_handle;
  397. else
  398. _dl_handles = rpnt->next_handle;
  399. /* OK, this is a valid handle - now close out the file */
  400. for (rpnt = handle; rpnt; rpnt = rpnt1) {
  401. rpnt1 = rpnt->next;
  402. /* We appended the module list to the end - when we get back here,
  403. quit. The access counts were not adjusted to account for being here. */
  404. if (rpnt == _dl_symbol_tables)
  405. break;
  406. rpnt->dyn->usage_count--;
  407. if (rpnt->dyn->usage_count == 0
  408. && rpnt->dyn->libtype == loaded_file) {
  409. tpnt = rpnt->dyn;
  410. /* Apparently crt1 for the application is responsible for handling this.
  411. * We only need to run the init/fini for shared libraries
  412. */
  413. #if 0
  414. /* We have to do this above, before we start closing objects.
  415. * Otherwise when the needed symbols for _fini handling are
  416. * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
  417. if (tpnt->dynamic_info[DT_FINI]) {
  418. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  419. (*dl_elf_fini) ();
  420. }
  421. #endif
  422. end = 0;
  423. for (i = 0, ppnt = rpnt->dyn->ppnt;
  424. i < rpnt->dyn->n_phent; ppnt++, i++) {
  425. if (ppnt->p_type != PT_LOAD)
  426. continue;
  427. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  428. end = ppnt->p_vaddr + ppnt->p_memsz;
  429. }
  430. _dl_munmap((void*)rpnt->dyn->loadaddr, end);
  431. /* Next, remove rpnt->dyn from the loaded_module list */
  432. if (_dl_loaded_modules == rpnt->dyn) {
  433. _dl_loaded_modules = rpnt->dyn->next;
  434. if (_dl_loaded_modules)
  435. _dl_loaded_modules->prev = 0;
  436. } else
  437. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  438. if (tpnt->next == rpnt->dyn) {
  439. tpnt->next = tpnt->next->next;
  440. if (tpnt->next)
  441. tpnt->next->prev = tpnt;
  442. break;
  443. }
  444. free(rpnt->dyn->libname);
  445. free(rpnt->dyn);
  446. }
  447. free(rpnt);
  448. }
  449. if (_dl_debug_addr) {
  450. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  451. if (dl_brk != NULL) {
  452. _dl_debug_addr->r_state = RT_DELETE;
  453. (*dl_brk) ();
  454. _dl_debug_addr->r_state = RT_CONSISTENT;
  455. (*dl_brk) ();
  456. }
  457. }
  458. return 0;
  459. }
  460. const char *_dlerror(void)
  461. {
  462. const char *retval;
  463. if (!_dl_error_number)
  464. return NULL;
  465. retval = dl_error_names[_dl_error_number];
  466. _dl_error_number = 0;
  467. return retval;
  468. }
  469. /*
  470. * Dump information to stderrr about the current loaded modules
  471. */
  472. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  473. void _dlinfo(void)
  474. {
  475. struct elf_resolve *tpnt;
  476. struct dyn_elf *rpnt, *hpnt;
  477. _dl_dprintf(2, "List of loaded modules\n");
  478. /* First start with a complete list of all of the loaded files. */
  479. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  480. _dl_dprintf(2, "\t%x %x %x %s %d %s\n",
  481. (unsigned) tpnt->loadaddr, (unsigned) tpnt,
  482. (unsigned) tpnt->symbol_scope,
  483. type[tpnt->libtype],
  484. tpnt->usage_count, tpnt->libname);
  485. }
  486. /* Next dump the module list for the application itself */
  487. _dl_dprintf(2, "\nModules for application (%x):\n",
  488. (unsigned) _dl_symbol_tables);
  489. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  490. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  491. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  492. _dl_dprintf(2, "Modules for handle %x\n", (unsigned) hpnt);
  493. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  494. _dl_dprintf(2, "\t%x %s\n", (unsigned) rpnt->dyn,
  495. rpnt->dyn->libname);
  496. }
  497. }
  498. int _dladdr(void *__address, Dl_info * __dlip)
  499. {
  500. struct elf_resolve *pelf;
  501. struct elf_resolve *rpnt;
  502. _dl_map_cache();
  503. /*
  504. * Try and locate the module address is in
  505. */
  506. pelf = NULL;
  507. #if 0
  508. _dl_dprintf(2, "dladdr( %x, %x )\n", __address, __dlip);
  509. #endif
  510. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  511. struct elf_resolve *tpnt;
  512. tpnt = rpnt;
  513. #if 0
  514. _dl_dprintf(2, "Module \"%s\" at %x\n",
  515. tpnt->libname, tpnt->loadaddr);
  516. #endif
  517. if (tpnt->loadaddr < (ElfW(Addr)) __address
  518. && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
  519. pelf = tpnt;
  520. }
  521. }
  522. if (!pelf) {
  523. return 0;
  524. }
  525. /*
  526. * Try and locate the symbol of address
  527. */
  528. {
  529. char *strtab;
  530. Elf32_Sym *symtab;
  531. int hn, si;
  532. int sf;
  533. int sn = 0;
  534. ElfW(Addr) sa;
  535. sa = 0;
  536. symtab = (Elf32_Sym *) (pelf->dynamic_info[DT_SYMTAB] + pelf->loadaddr);
  537. strtab = (char *) (pelf->dynamic_info[DT_STRTAB] + pelf->loadaddr);
  538. sf = 0;
  539. for (hn = 0; hn < pelf->nbucket; hn++) {
  540. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  541. ElfW(Addr) symbol_addr;
  542. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  543. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  544. sa = symbol_addr;
  545. sn = si;
  546. sf = 1;
  547. }
  548. #if 0
  549. _dl_dprintf(2, "Symbol \"%s\" at %x\n",
  550. strtab + symtab[si].st_name, symbol_addr);
  551. #endif
  552. }
  553. }
  554. if (sf) {
  555. __dlip->dli_fname = pelf->libname;
  556. __dlip->dli_fbase = (void *)pelf->loadaddr;
  557. __dlip->dli_sname = strtab + symtab[sn].st_name;
  558. __dlip->dli_saddr = (void *)sa;
  559. }
  560. return 1;
  561. }
  562. }