libdl.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Program to load an ELF binary on a linux system, and run it
  4. * after resolving ELF shared library symbols
  5. *
  6. * Copyright (C) 2000-2004 by Erik Andersen <andersen@codpoet.org>
  7. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  8. * David Engel, Hongjiu Lu and Mitch D'Souza
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions
  12. * are met:
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. * 2. The name of the above contributors may not be
  16. * used to endorse or promote products derived from this software
  17. * without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  21. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  22. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  23. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  24. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  25. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  26. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  27. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  28. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. */
  31. #include <ldso.h>
  32. #include <stdio.h>
  33. #if defined (__LIBDL_SHARED__)
  34. /* When libdl is loaded as a shared library, we need to load in
  35. * and use a pile of symbols from ldso... */
  36. extern char *_dl_find_hash(const char *, struct dyn_elf *, int)
  37. __attribute__ ((__weak__));
  38. extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
  39. struct elf_resolve *, char *, int) __attribute__ ((__weak__));
  40. extern struct elf_resolve * _dl_check_if_named_library_is_loaded(const char *, int)
  41. __attribute__ ((__weak__));
  42. extern int _dl_fixup(struct dyn_elf *rpnt, int lazy)
  43. __attribute__ ((__weak__));
  44. extern int _dl_errno __attribute__ ((__weak__));
  45. extern struct dyn_elf *_dl_symbol_tables __attribute__ ((__weak__));
  46. extern struct dyn_elf *_dl_handles __attribute__ ((__weak__));
  47. extern struct elf_resolve *_dl_loaded_modules __attribute__ ((__weak__));
  48. extern struct r_debug *_dl_debug_addr __attribute__ ((__weak__));
  49. extern unsigned long _dl_error_number __attribute__ ((__weak__));
  50. extern void *(*_dl_malloc_function)(size_t) __attribute__ ((__weak__));
  51. #ifdef USE_CACHE
  52. int _dl_map_cache(void) __attribute__ ((__weak__));
  53. int _dl_unmap_cache(void) __attribute__ ((__weak__));
  54. #endif
  55. #ifdef __mips__
  56. extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt)
  57. __attribute__ ((__weak__));
  58. #endif
  59. #ifdef __SUPPORT_LD_DEBUG__
  60. extern char *_dl_debug __attribute__ ((__weak__));
  61. #endif
  62. #else /* __LIBDL_SHARED__ */
  63. /* When libdl is linked as a static library, we need to replace all
  64. * the symbols that otherwise would have been loaded in from ldso... */
  65. #ifdef __SUPPORT_LD_DEBUG__
  66. char *_dl_debug = 0;
  67. #endif
  68. char *_dl_library_path = 0; /* Where we look for libraries */
  69. char *_dl_ldsopath = 0; /* Location of the shared lib loader */
  70. int _dl_errno = 0; /* We can't use the real errno in ldso */
  71. size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
  72. /* This global variable is also to communicate with debuggers such as gdb. */
  73. struct r_debug *_dl_debug_addr = NULL;
  74. #define _dl_malloc malloc
  75. #include "dl-progname.h"
  76. #include "../ldso/dl-hash.c"
  77. #define _dl_trace_loaded_objects 0
  78. #include "../ldso/dl-elf.c"
  79. #endif
  80. static int do_dlclose(void *, int need_fini);
  81. static const char *dl_error_names[] = {
  82. "",
  83. "File not found",
  84. "Unable to open /dev/zero",
  85. "Not an ELF file",
  86. #if defined (__i386__)
  87. "Not i386 binary",
  88. #elif defined (__sparc__)
  89. "Not sparc binary",
  90. #elif defined (__mc68000__)
  91. "Not m68k binary",
  92. #else
  93. "Unrecognized binary type",
  94. #endif
  95. "Not an ELF shared library",
  96. "Unable to mmap file",
  97. "No dynamic section",
  98. #ifdef ELF_USES_RELOCA
  99. "Unable to process REL relocs",
  100. #else
  101. "Unable to process RELA relocs",
  102. #endif
  103. "Bad handle",
  104. "Unable to resolve symbol"
  105. };
  106. static void __attribute__ ((destructor)) dl_cleanup(void)
  107. {
  108. struct dyn_elf *d;
  109. for (d = _dl_handles; d; d = d->next_handle)
  110. if (d->dyn->libtype == loaded_file && d->dyn->dynamic_info[DT_FINI]) {
  111. (* ((int (*)(void)) (d->dyn->loadaddr + d->dyn->dynamic_info[DT_FINI]))) ();
  112. d->dyn->dynamic_info[DT_FINI] = 0;
  113. }
  114. }
  115. void *dlopen(const char *libname, int flag)
  116. {
  117. struct elf_resolve *tpnt, *tfrom, *tcurr;
  118. struct dyn_elf *dyn_chain, *rpnt = NULL;
  119. struct dyn_elf *dpnt;
  120. static int dl_init = 0;
  121. ElfW(Addr) from;
  122. struct elf_resolve *tpnt1;
  123. void (*dl_brk) (void);
  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 = (ElfW(Addr)) __builtin_return_address(0);
  130. /* Have the dynamic linker use the regular malloc function now */
  131. if (!dl_init) {
  132. dl_init++;
  133. #if defined (__LIBDL_SHARED__)
  134. _dl_malloc_function = malloc;
  135. #endif
  136. }
  137. /* Cover the trivial case first */
  138. if (!libname)
  139. return _dl_symbol_tables;
  140. _dl_map_cache();
  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. /* Try to load the specified library */
  157. #ifdef __SUPPORT_LD_DEBUG__
  158. if(_dl_debug)
  159. fprintf(stderr, "Trying to dlopen '%s'\n", (char*)libname);
  160. #endif
  161. if (!(tpnt = _dl_check_if_named_library_is_loaded((char *)libname, 0)))
  162. tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
  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. fprintf(stderr, "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. if ((tpnt1 = _dl_check_if_named_library_is_loaded(name, 0)))
  196. continue;
  197. #ifdef __SUPPORT_LD_DEBUG__
  198. if(_dl_debug)
  199. fprintf(stderr, "Trying to load '%s', needed by '%s'\n",
  200. lpntstr, tcurr->libname);
  201. #endif
  202. if (!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, 0))) {
  203. goto oops;
  204. }
  205. rpnt->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  206. _dl_memset (rpnt->next, 0, sizeof (struct dyn_elf));
  207. rpnt = rpnt->next;
  208. if (!tpnt1->symbol_scope) tpnt1->symbol_scope = rpnt;
  209. rpnt->dyn = tpnt1;
  210. }
  211. }
  212. }
  213. /*
  214. * OK, now attach the entire chain at the end
  215. */
  216. rpnt->next = _dl_symbol_tables;
  217. #ifdef __mips__
  218. /*
  219. * Relocation of the GOT entries for MIPS have to be done
  220. * after all the libraries have been loaded.
  221. */
  222. _dl_perform_mips_global_got_relocations(tpnt);
  223. #endif
  224. #ifdef __SUPPORT_LD_DEBUG__
  225. if(_dl_debug)
  226. fprintf(stderr, "Beginning dlopen relocation fixups\n");
  227. #endif
  228. /*
  229. * OK, now all of the kids are tucked into bed in their proper addresses.
  230. * Now we go through and look for REL and RELA records that indicate fixups
  231. * to the GOT tables. We need to do this in reverse order so that COPY
  232. * directives work correctly */
  233. if (_dl_fixup(dyn_chain, dyn_chain->flags))
  234. goto oops;
  235. /* TODO: Should we set the protections of all pages back to R/O now ? */
  236. /* Notify the debugger we have added some objects. */
  237. if (_dl_debug_addr) {
  238. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  239. if (dl_brk != NULL) {
  240. _dl_debug_addr->r_state = RT_ADD;
  241. (*dl_brk) ();
  242. _dl_debug_addr->r_state = RT_CONSISTENT;
  243. (*dl_brk) ();
  244. }
  245. }
  246. #if defined (__LIBDL_SHARED__)
  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. fprintf(stderr, "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. fprintf(stderr, "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, 0);
  334. /*
  335. * Nothing found.
  336. */
  337. if (!ret)
  338. _dl_error_number = LD_NO_SYMBOL;
  339. return ret;
  340. }
  341. static int do_dlclose(void *vhandle, int need_fini)
  342. {
  343. struct dyn_elf *rpnt, *rpnt1;
  344. struct dyn_elf *spnt, *spnt1;
  345. ElfW(Phdr) *ppnt;
  346. struct elf_resolve *tpnt;
  347. int (*dl_elf_fini) (void);
  348. void (*dl_brk) (void);
  349. struct dyn_elf *handle;
  350. unsigned int end;
  351. int i = 0;
  352. handle = (struct dyn_elf *) vhandle;
  353. rpnt1 = NULL;
  354. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  355. if (rpnt == handle) {
  356. break;
  357. }
  358. rpnt1 = rpnt;
  359. }
  360. if (!rpnt) {
  361. _dl_error_number = LD_BAD_HANDLE;
  362. return 1;
  363. }
  364. /* OK, this is a valid handle - now close out the file.
  365. * We check if we need to call fini () on the handle. */
  366. spnt = need_fini ? handle : handle->next;
  367. for (; spnt; spnt = spnt1) {
  368. spnt1 = spnt->next;
  369. /* We appended the module list to the end - when we get back here,
  370. quit. The access counts were not adjusted to account for being here. */
  371. if (spnt == _dl_symbol_tables)
  372. break;
  373. if (spnt->dyn->usage_count == 1
  374. && spnt->dyn->libtype == loaded_file) {
  375. tpnt = spnt->dyn;
  376. /* Apparently crt1 for the application is responsible for handling this.
  377. * We only need to run the init/fini for shared libraries
  378. */
  379. if (tpnt->dynamic_info[DT_FINI]) {
  380. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr +
  381. tpnt->dynamic_info[DT_FINI]);
  382. (*dl_elf_fini) ();
  383. }
  384. }
  385. }
  386. if (rpnt1)
  387. rpnt1->next_handle = rpnt->next_handle;
  388. else
  389. _dl_handles = rpnt->next_handle;
  390. /* OK, this is a valid handle - now close out the file */
  391. for (rpnt = handle; rpnt; rpnt = rpnt1) {
  392. rpnt1 = rpnt->next;
  393. /* We appended the module list to the end - when we get back here,
  394. quit. The access counts were not adjusted to account for being here. */
  395. if (rpnt == _dl_symbol_tables)
  396. break;
  397. rpnt->dyn->usage_count--;
  398. if (rpnt->dyn->usage_count == 0
  399. && rpnt->dyn->libtype == loaded_file) {
  400. tpnt = rpnt->dyn;
  401. /* Apparently crt1 for the application is responsible for handling this.
  402. * We only need to run the init/fini for shared libraries
  403. */
  404. #if 0
  405. /* We have to do this above, before we start closing objects.
  406. * Otherwise when the needed symbols for _fini handling are
  407. * resolved a coredump would occur. Rob Ryan (robr@cmu.edu)*/
  408. if (tpnt->dynamic_info[DT_FINI]) {
  409. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  410. (*dl_elf_fini) ();
  411. }
  412. #endif
  413. end = 0;
  414. for (i = 0, ppnt = rpnt->dyn->ppnt;
  415. i < rpnt->dyn->n_phent; ppnt++, i++) {
  416. if (ppnt->p_type != PT_LOAD)
  417. continue;
  418. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  419. end = ppnt->p_vaddr + ppnt->p_memsz;
  420. }
  421. _dl_munmap((void*)rpnt->dyn->loadaddr, end);
  422. /* Next, remove rpnt->dyn from the loaded_module list */
  423. if (_dl_loaded_modules == rpnt->dyn) {
  424. _dl_loaded_modules = rpnt->dyn->next;
  425. if (_dl_loaded_modules)
  426. _dl_loaded_modules->prev = 0;
  427. } else
  428. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  429. if (tpnt->next == rpnt->dyn) {
  430. tpnt->next = tpnt->next->next;
  431. if (tpnt->next)
  432. tpnt->next->prev = tpnt;
  433. break;
  434. }
  435. free(rpnt->dyn->libname);
  436. free(rpnt->dyn);
  437. }
  438. free(rpnt);
  439. }
  440. if (_dl_debug_addr) {
  441. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  442. if (dl_brk != NULL) {
  443. _dl_debug_addr->r_state = RT_DELETE;
  444. (*dl_brk) ();
  445. _dl_debug_addr->r_state = RT_CONSISTENT;
  446. (*dl_brk) ();
  447. }
  448. }
  449. return 0;
  450. }
  451. int dlclose(void *vhandle)
  452. {
  453. return do_dlclose(vhandle, 1);
  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. fprintf(stderr, "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. fprintf(stderr, "\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. fprintf(stderr, "\nModules for application (%x):\n",
  483. (unsigned) _dl_symbol_tables);
  484. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  485. fprintf(stderr, "\t%x %s\n", (unsigned) rpnt->dyn, rpnt->dyn->libname);
  486. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  487. fprintf(stderr, "Modules for handle %x\n", (unsigned) hpnt);
  488. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  489. fprintf(stderr, "\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. fprintf(stderr, "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. fprintf(stderr, "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. fprintf(stderr, "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. }