libdl.c 19 KB

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