libdl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708
  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-2006 by Erik Andersen <andersen@uclibc.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. #ifdef 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 *, struct elf_resolve *, int);
  37. extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
  38. struct elf_resolve *, char *, int);
  39. extern int _dl_fixup(struct dyn_elf *rpnt, int lazy);
  40. extern void _dl_protect_relro(struct elf_resolve * tpnt);
  41. extern int _dl_errno;
  42. extern struct dyn_elf *_dl_symbol_tables;
  43. extern struct dyn_elf *_dl_handles;
  44. extern struct elf_resolve *_dl_loaded_modules;
  45. extern struct r_debug *_dl_debug_addr;
  46. extern unsigned long _dl_error_number;
  47. extern void *(*_dl_malloc_function)(size_t);
  48. extern void _dl_run_init_array(struct elf_resolve *);
  49. extern void _dl_run_fini_array(struct elf_resolve *);
  50. #ifdef __LDSO_CACHE_SUPPORT__
  51. int _dl_map_cache(void);
  52. int _dl_unmap_cache(void);
  53. #endif
  54. #ifdef __mips__
  55. extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
  56. #endif
  57. #ifdef __SUPPORT_LD_DEBUG__
  58. extern char *_dl_debug;
  59. #endif
  60. #else /* SHARED */
  61. /* When libdl is linked as a static library, we need to replace all
  62. * the symbols that otherwise would have been loaded in from ldso... */
  63. #ifdef __SUPPORT_LD_DEBUG__
  64. char *_dl_debug = 0;
  65. #endif
  66. const char *_dl_progname = ""; /* Program name */
  67. char *_dl_library_path = 0; /* Where we look for libraries */
  68. char *_dl_ldsopath = 0; /* Location of the shared lib loader */
  69. int _dl_errno = 0; /* We can't use the real errno in ldso */
  70. size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
  71. /* This global variable is also to communicate with debuggers such as gdb. */
  72. struct r_debug *_dl_debug_addr = NULL;
  73. #define _dl_malloc malloc
  74. #include "../ldso/dl-debug.c"
  75. #include LDSO_ELFINTERP
  76. #include "../ldso/dl-hash.c"
  77. #define _dl_trace_loaded_objects 0
  78. #include "../ldso/dl-elf.c"
  79. #endif /* SHARED */
  80. #ifdef __SUPPORT_LD_DEBUG__
  81. # define _dl_if_debug_print(fmt, args...) \
  82. do { \
  83. if (_dl_debug) \
  84. fprintf(stderr, "%s():%i: " fmt, __FUNCTION__, __LINE__, ## args); \
  85. } while (0)
  86. #else
  87. # define _dl_if_debug_print(fmt, args...)
  88. #endif
  89. static int do_dlclose(void *, int need_fini);
  90. static const char *dl_error_names[] = {
  91. "",
  92. "File not found",
  93. "Unable to open /dev/zero",
  94. "Not an ELF file",
  95. #if defined (__i386__)
  96. "Not i386 binary",
  97. #elif defined (__sparc__)
  98. "Not sparc binary",
  99. #elif defined (__mc68000__)
  100. "Not m68k binary",
  101. #else
  102. "Unrecognized binary type",
  103. #endif
  104. "Not an ELF shared library",
  105. "Unable to mmap file",
  106. "No dynamic section",
  107. #ifdef ELF_USES_RELOCA
  108. "Unable to process REL relocs",
  109. #else
  110. "Unable to process RELA relocs",
  111. #endif
  112. "Bad handle",
  113. "Unable to resolve symbol"
  114. };
  115. void dl_cleanup(void) __attribute__ ((destructor));
  116. void dl_cleanup(void)
  117. {
  118. struct dyn_elf *d;
  119. for (d = _dl_handles; d; d = d->next_handle) {
  120. do_dlclose(d, 1);
  121. }
  122. }
  123. void *dlopen(const char *libname, int flag)
  124. {
  125. struct elf_resolve *tpnt, *tfrom;
  126. struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
  127. ElfW(Addr) from;
  128. struct elf_resolve *tpnt1;
  129. void (*dl_brk) (void);
  130. int now_flag;
  131. struct init_fini_list *tmp, *runp, *runp2, *dep_list;
  132. unsigned int nlist, i;
  133. struct elf_resolve **init_fini_list;
  134. /* A bit of sanity checking... */
  135. if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
  136. _dl_error_number = LD_BAD_HANDLE;
  137. return NULL;
  138. }
  139. from = (ElfW(Addr)) __builtin_return_address(0);
  140. /* Cover the trivial case first */
  141. if (!libname)
  142. return _dl_symbol_tables;
  143. _dl_map_cache();
  144. /*
  145. * Try and locate the module we were called from - we
  146. * need this so that we get the correct RPATH/RUNPATH. Note that
  147. * this is the current behavior under Solaris, but the
  148. * ABI+ specifies that we should only use the RPATH from
  149. * the application. Thus this may go away at some time
  150. * in the future.
  151. */
  152. {
  153. struct dyn_elf *dpnt;
  154. tfrom = NULL;
  155. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  156. tpnt = dpnt->dyn;
  157. if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom))
  158. tfrom = tpnt;
  159. }
  160. }
  161. for(rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt=rpnt->next);
  162. relro_ptr = rpnt;
  163. now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
  164. if (getenv("LD_BIND_NOW"))
  165. now_flag = RTLD_NOW;
  166. /* Try to load the specified library */
  167. _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
  168. (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
  169. tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
  170. if (tpnt == NULL) {
  171. _dl_unmap_cache();
  172. return NULL;
  173. }
  174. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  175. _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
  176. dyn_chain->dyn = tpnt;
  177. tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
  178. dyn_chain->next_handle = _dl_handles;
  179. _dl_handles = dyn_ptr = dyn_chain;
  180. if (tpnt->usage_count > 1) {
  181. _dl_if_debug_print("Lib: %s already opened\n", libname);
  182. /* see if there is a handle from a earlier dlopen */
  183. for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
  184. if (handle->dyn == tpnt) {
  185. dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
  186. dyn_chain->init_fini.nlist = handle->init_fini.nlist;
  187. for(i=0; i < dyn_chain->init_fini.nlist; i++)
  188. dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
  189. dyn_chain->next = handle->next;
  190. break;
  191. }
  192. }
  193. return dyn_chain;
  194. } else {
  195. tpnt->init_flag |= DL_OPENED;
  196. }
  197. _dl_if_debug_print("Looking for needed libraries\n");
  198. nlist = 0;
  199. runp = alloca(sizeof(*runp));
  200. runp->tpnt = tpnt;
  201. runp->next = NULL;
  202. dep_list = runp2 = runp;
  203. for (; runp; runp = runp->next)
  204. {
  205. ElfW(Dyn) *dpnt;
  206. char *lpntstr;
  207. nlist++;
  208. runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
  209. for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
  210. if (dpnt->d_tag == DT_NEEDED) {
  211. lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
  212. dpnt->d_un.d_val);
  213. _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
  214. lpntstr, runp->tpnt->libname);
  215. tpnt1 = _dl_load_shared_library(0, &rpnt, runp->tpnt, lpntstr, 0);
  216. if (!tpnt1)
  217. goto oops;
  218. tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
  219. /* This list is for dlsym() and relocation */
  220. dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  221. _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
  222. dyn_ptr = dyn_ptr->next;
  223. dyn_ptr->dyn = tpnt1;
  224. /* Used to record RTLD_LOCAL scope */
  225. tmp = alloca(sizeof(struct init_fini_list));
  226. tmp->tpnt = tpnt1;
  227. tmp->next = runp->tpnt->init_fini;
  228. runp->tpnt->init_fini = tmp;
  229. for (tmp=dep_list; tmp; tmp = tmp->next) {
  230. if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
  231. _dl_if_debug_print("Circular dependency, skipping '%s',\n",
  232. tmp->tpnt->libname);
  233. tpnt1->usage_count--;
  234. break;
  235. }
  236. }
  237. if (!tmp) { /* Don't add if circular dependency detected */
  238. runp2->next = alloca(sizeof(*runp));
  239. runp2 = runp2->next;
  240. runp2->tpnt = tpnt1;
  241. runp2->next = NULL;
  242. }
  243. }
  244. }
  245. }
  246. init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
  247. dyn_chain->init_fini.init_fini = init_fini_list;
  248. dyn_chain->init_fini.nlist = nlist;
  249. i = 0;
  250. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  251. init_fini_list[i++] = runp2->tpnt;
  252. for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
  253. if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
  254. tmp = malloc(sizeof(struct init_fini_list));
  255. tmp->tpnt = runp->tpnt;
  256. tmp->next = runp2->tpnt->rtld_local;
  257. runp2->tpnt->rtld_local = tmp;
  258. }
  259. }
  260. }
  261. /* Sort the INIT/FINI list in dependency order. */
  262. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  263. unsigned int j, k;
  264. for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
  265. /* Empty */;
  266. for (k = j + 1; k < nlist; ++k) {
  267. struct init_fini_list *ele = init_fini_list[k]->init_fini;
  268. for (; ele; ele = ele->next) {
  269. if (ele->tpnt == runp2->tpnt) {
  270. struct elf_resolve *here = init_fini_list[k];
  271. _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
  272. for (i = (k - j); i; --i)
  273. init_fini_list[i+j] = init_fini_list[i+j-1];
  274. init_fini_list[j] = here;
  275. ++j;
  276. break;
  277. }
  278. }
  279. }
  280. }
  281. #ifdef __SUPPORT_LD_DEBUG__
  282. if(_dl_debug) {
  283. fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
  284. for (i=0;i < nlist;i++) {
  285. fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
  286. runp = init_fini_list[i]->init_fini;
  287. for (; runp; runp = runp->next)
  288. printf(" %s ", runp->tpnt->libname);
  289. printf("\n");
  290. }
  291. }
  292. #endif
  293. _dl_if_debug_print("Beginning dlopen relocation fixups\n");
  294. /*
  295. * OK, now all of the kids are tucked into bed in their proper addresses.
  296. * Now we go through and look for REL and RELA records that indicate fixups
  297. * to the GOT tables. We need to do this in reverse order so that COPY
  298. * directives work correctly */
  299. #ifdef __mips__
  300. /*
  301. * Relocation of the GOT entries for MIPS have to be done
  302. * after all the libraries have been loaded.
  303. */
  304. _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
  305. #endif
  306. if (_dl_fixup(dyn_chain, now_flag))
  307. goto oops;
  308. if (relro_ptr) {
  309. for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
  310. if (rpnt->dyn->relro_size)
  311. _dl_protect_relro(rpnt->dyn);
  312. }
  313. }
  314. /* TODO: Should we set the protections of all pages back to R/O now ? */
  315. /* Notify the debugger we have added some objects. */
  316. if (_dl_debug_addr) {
  317. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  318. if (dl_brk != NULL) {
  319. _dl_debug_addr->r_state = RT_ADD;
  320. (*dl_brk) ();
  321. _dl_debug_addr->r_state = RT_CONSISTENT;
  322. (*dl_brk) ();
  323. }
  324. }
  325. #ifdef SHARED
  326. /* Run the ctors and setup the dtors */
  327. for (i = nlist; i; --i) {
  328. tpnt = init_fini_list[i-1];
  329. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  330. continue;
  331. tpnt->init_flag |= INIT_FUNCS_CALLED;
  332. if (tpnt->dynamic_info[DT_INIT]) {
  333. void (*dl_elf_func) (void);
  334. dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
  335. if (dl_elf_func) {
  336. _dl_if_debug_print("running ctors for library %s at '%p'\n",
  337. tpnt->libname, dl_elf_func);
  338. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  339. }
  340. }
  341. _dl_run_init_array(tpnt);
  342. }
  343. #endif /* SHARED */
  344. _dl_unmap_cache();
  345. return (void *) dyn_chain;
  346. oops:
  347. /* Something went wrong. Clean up and return NULL. */
  348. _dl_unmap_cache();
  349. do_dlclose(dyn_chain, 0);
  350. return NULL;
  351. }
  352. void *dlsym(void *vhandle, const char *name)
  353. {
  354. struct elf_resolve *tpnt, *tfrom;
  355. struct dyn_elf *handle;
  356. ElfW(Addr) from;
  357. struct dyn_elf *rpnt;
  358. void *ret;
  359. handle = (struct dyn_elf *) vhandle;
  360. /* First of all verify that we have a real handle
  361. of some kind. Return NULL if not a valid handle. */
  362. if (handle == NULL)
  363. handle = _dl_symbol_tables;
  364. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  365. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  366. if (rpnt == handle)
  367. break;
  368. if (!rpnt) {
  369. _dl_error_number = LD_BAD_HANDLE;
  370. return NULL;
  371. }
  372. } else if (handle == RTLD_NEXT) {
  373. /*
  374. * Try and locate the module we were called from - we
  375. * need this so that we know where to start searching
  376. * from. We never pass RTLD_NEXT down into the actual
  377. * dynamic loader itself, as it doesn't know
  378. * how to properly treat it.
  379. */
  380. from = (ElfW(Addr)) __builtin_return_address(0);
  381. tfrom = NULL;
  382. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  383. tpnt = rpnt->dyn;
  384. if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom)) {
  385. tfrom = tpnt;
  386. handle = rpnt->next;
  387. }
  388. }
  389. }
  390. tpnt = NULL;
  391. if (handle == _dl_symbol_tables)
  392. tpnt = handle->dyn; /* Only search RTLD_GLOBAL objs if global object */
  393. ret = _dl_find_hash((char*)name, handle, tpnt, 0);
  394. /*
  395. * Nothing found.
  396. */
  397. if (!ret)
  398. _dl_error_number = LD_NO_SYMBOL;
  399. return ret;
  400. }
  401. #if 0
  402. void *dlvsym(void *vhandle, const char *name, const char *version)
  403. {
  404. return dlsym(vhandle, name);
  405. }
  406. #endif
  407. static int do_dlclose(void *vhandle, int need_fini)
  408. {
  409. struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
  410. struct init_fini_list *runp, *tmp;
  411. ElfW(Phdr) *ppnt;
  412. struct elf_resolve *tpnt, *run_tpnt;
  413. int (*dl_elf_fini) (void);
  414. void (*dl_brk) (void);
  415. struct dyn_elf *handle;
  416. unsigned int end;
  417. unsigned int i, j;
  418. handle = (struct dyn_elf *) vhandle;
  419. if (handle == _dl_symbol_tables)
  420. return 0;
  421. rpnt1 = NULL;
  422. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  423. if (rpnt == handle)
  424. break;
  425. rpnt1 = rpnt;
  426. }
  427. if (!rpnt) {
  428. _dl_error_number = LD_BAD_HANDLE;
  429. return 1;
  430. }
  431. if (rpnt1)
  432. rpnt1->next_handle = rpnt->next_handle;
  433. else
  434. _dl_handles = rpnt->next_handle;
  435. _dl_if_debug_print("%s: usage count: %d\n",
  436. handle->dyn->libname, handle->dyn->usage_count);
  437. if (handle->dyn->usage_count != 1) {
  438. handle->dyn->usage_count--;
  439. free(handle);
  440. return 0;
  441. }
  442. /* OK, this is a valid handle - now close out the file */
  443. for (j = 0; j < handle->init_fini.nlist; ++j) {
  444. tpnt = handle->init_fini.init_fini[j];
  445. if (--tpnt->usage_count == 0) {
  446. if ((tpnt->dynamic_info[DT_FINI]
  447. || tpnt->dynamic_info[DT_FINI_ARRAY])
  448. && need_fini &&
  449. !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
  450. tpnt->init_flag |= FINI_FUNCS_CALLED;
  451. #ifdef SHARED
  452. _dl_run_fini_array(tpnt);
  453. #endif
  454. if (tpnt->dynamic_info[DT_FINI]) {
  455. dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
  456. _dl_if_debug_print("running dtors for library %s at '%p'\n",
  457. tpnt->libname, dl_elf_fini);
  458. DL_CALL_FUNC_AT_ADDR (dl_elf_fini, tpnt->loadaddr, (int (*)(void)));
  459. }
  460. }
  461. _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
  462. end = 0;
  463. for (i = 0, ppnt = tpnt->ppnt;
  464. i < tpnt->n_phent; ppnt++, i++) {
  465. if (ppnt->p_type != PT_LOAD)
  466. continue;
  467. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  468. end = ppnt->p_vaddr + ppnt->p_memsz;
  469. }
  470. _dl_munmap((void*)tpnt->loadaddr, end);
  471. /* Free elements in RTLD_LOCAL scope list */
  472. for (runp = tpnt->rtld_local; runp; runp = tmp) {
  473. tmp = runp->next;
  474. free(runp);
  475. }
  476. /* Next, remove tpnt from the loaded_module list */
  477. if (_dl_loaded_modules == tpnt) {
  478. _dl_loaded_modules = tpnt->next;
  479. if (_dl_loaded_modules)
  480. _dl_loaded_modules->prev = 0;
  481. } else
  482. for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next)
  483. if (run_tpnt->next == tpnt) {
  484. _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
  485. run_tpnt->next = run_tpnt->next->next;
  486. if (run_tpnt->next)
  487. run_tpnt->next->prev = run_tpnt;
  488. break;
  489. }
  490. /* Next, remove tpnt from the global symbol table list */
  491. if (_dl_symbol_tables) {
  492. if (_dl_symbol_tables->dyn == tpnt) {
  493. _dl_symbol_tables = _dl_symbol_tables->next;
  494. if (_dl_symbol_tables)
  495. _dl_symbol_tables->prev = 0;
  496. } else
  497. for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
  498. if (rpnt1->next->dyn == tpnt) {
  499. _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
  500. rpnt1_tmp = rpnt1->next->next;
  501. free(rpnt1->next);
  502. rpnt1->next = rpnt1_tmp;
  503. if (rpnt1->next)
  504. rpnt1->next->prev = rpnt1;
  505. break;
  506. }
  507. }
  508. }
  509. free(tpnt->libname);
  510. free(tpnt);
  511. }
  512. }
  513. free(handle->init_fini.init_fini);
  514. free(handle);
  515. if (_dl_debug_addr) {
  516. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  517. if (dl_brk != NULL) {
  518. _dl_debug_addr->r_state = RT_DELETE;
  519. (*dl_brk) ();
  520. _dl_debug_addr->r_state = RT_CONSISTENT;
  521. (*dl_brk) ();
  522. }
  523. }
  524. return 0;
  525. }
  526. int dlclose(void *vhandle)
  527. {
  528. return do_dlclose(vhandle, 1);
  529. }
  530. char *dlerror(void)
  531. {
  532. const char *retval;
  533. if (!_dl_error_number)
  534. return NULL;
  535. retval = dl_error_names[_dl_error_number];
  536. _dl_error_number = 0;
  537. return (char *)retval;
  538. }
  539. /*
  540. * Dump information to stderr about the current loaded modules
  541. */
  542. #ifdef __USE_GNU
  543. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  544. int dlinfo(void)
  545. {
  546. struct elf_resolve *tpnt;
  547. struct dyn_elf *rpnt, *hpnt;
  548. fprintf(stderr, "List of loaded modules\n");
  549. /* First start with a complete list of all of the loaded files. */
  550. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  551. fprintf(stderr, "\t%p %p %p %s %d %s\n",
  552. DL_LOADADDR_BASE(tpnt->loadaddr), tpnt, tpnt->symbol_scope,
  553. type[tpnt->libtype],
  554. tpnt->usage_count, tpnt->libname);
  555. }
  556. /* Next dump the module list for the application itself */
  557. fprintf(stderr, "\nModules for application (%p):\n", _dl_symbol_tables);
  558. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  559. fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
  560. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  561. fprintf(stderr, "Modules for handle %p\n", hpnt);
  562. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  563. fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
  564. }
  565. return 0;
  566. }
  567. int dladdr(const void *__address, Dl_info * __info)
  568. {
  569. struct elf_resolve *pelf;
  570. struct elf_resolve *rpnt;
  571. _dl_map_cache();
  572. /*
  573. * Try and locate the module address is in
  574. */
  575. pelf = NULL;
  576. _dl_if_debug_print("__address: %p __info: %p\n", __address, __info);
  577. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  578. struct elf_resolve *tpnt;
  579. tpnt = rpnt;
  580. _dl_if_debug_print("Module \"%s\" at %p\n",
  581. tpnt->libname, DL_LOADADDR_BASE(tpnt->loadaddr));
  582. if (DL_ADDR_IN_LOADADDR((ElfW(Addr)) __address, tpnt, pelf))
  583. pelf = tpnt;
  584. }
  585. if (!pelf) {
  586. return 0;
  587. }
  588. /*
  589. * Try and locate the symbol of address
  590. */
  591. {
  592. char *strtab;
  593. ElfW(Sym) *symtab;
  594. unsigned int hn, si, sn, sf;
  595. ElfW(Addr) sa;
  596. sa = 0;
  597. symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
  598. strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
  599. sf = sn = 0;
  600. for (hn = 0; hn < pelf->nbucket; hn++) {
  601. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  602. ElfW(Addr) symbol_addr;
  603. symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
  604. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  605. sa = symbol_addr;
  606. sn = si;
  607. sf = 1;
  608. }
  609. _dl_if_debug_print("Symbol \"%s\" at %p\n",
  610. strtab + symtab[si].st_name, symbol_addr);
  611. }
  612. }
  613. if (sf) {
  614. __info->dli_fname = pelf->libname;
  615. __info->dli_fbase = (void *) DL_LOADADDR_BASE(pelf->loadaddr);
  616. __info->dli_sname = strtab + symtab[sn].st_name;
  617. __info->dli_saddr = (void *)sa;
  618. }
  619. return 1;
  620. }
  621. }
  622. #endif