libdl.c 18 KB

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