libdl.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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 (tpnt->loadaddr < from
  158. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr))
  159. tfrom = tpnt;
  160. }
  161. }
  162. for(rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt=rpnt->next);
  163. relro_ptr = rpnt;
  164. now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
  165. if (getenv("LD_BIND_NOW"))
  166. now_flag = RTLD_NOW;
  167. /* Try to load the specified library */
  168. _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
  169. (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
  170. tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
  171. if (tpnt == NULL) {
  172. _dl_unmap_cache();
  173. return NULL;
  174. }
  175. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  176. _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
  177. dyn_chain->dyn = tpnt;
  178. tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
  179. dyn_chain->next_handle = _dl_handles;
  180. _dl_handles = dyn_ptr = dyn_chain;
  181. if (tpnt->usage_count > 1) {
  182. _dl_if_debug_print("Lib: %s already opened\n", libname);
  183. /* see if there is a handle from a earlier dlopen */
  184. for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
  185. if (handle->dyn == tpnt) {
  186. dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
  187. dyn_chain->init_fini.nlist = handle->init_fini.nlist;
  188. for(i=0; i < dyn_chain->init_fini.nlist; i++)
  189. dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
  190. dyn_chain->next = handle->next;
  191. break;
  192. }
  193. }
  194. return dyn_chain;
  195. } else {
  196. tpnt->init_flag |= DL_OPENED;
  197. }
  198. _dl_if_debug_print("Looking for needed libraries\n");
  199. nlist = 0;
  200. runp = alloca(sizeof(*runp));
  201. runp->tpnt = tpnt;
  202. runp->next = NULL;
  203. dep_list = runp2 = runp;
  204. for (; runp; runp = runp->next)
  205. {
  206. ElfW(Dyn) *dpnt;
  207. char *lpntstr;
  208. nlist++;
  209. runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
  210. for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
  211. if (dpnt->d_tag == DT_NEEDED) {
  212. lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
  213. dpnt->d_un.d_val);
  214. _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
  215. lpntstr, runp->tpnt->libname);
  216. tpnt1 = _dl_load_shared_library(0, &rpnt, runp->tpnt, lpntstr, 0);
  217. if (!tpnt1)
  218. goto oops;
  219. tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
  220. if (tpnt1->usage_count == 1) {
  221. tpnt1->init_flag |= DL_OPENED;
  222. /* This list is for dlsym() and relocation */
  223. dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  224. _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
  225. dyn_ptr = dyn_ptr->next;
  226. dyn_ptr->dyn = tpnt1;
  227. }
  228. if (tpnt1->init_flag & DL_OPENED) {
  229. /* Used to record RTLD_LOCAL scope */
  230. tmp = alloca(sizeof(struct init_fini_list));
  231. tmp->tpnt = tpnt1;
  232. tmp->next = runp->tpnt->init_fini;
  233. runp->tpnt->init_fini = tmp;
  234. for (tmp=dep_list; tmp; tmp = tmp->next) {
  235. if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
  236. _dl_if_debug_print("Circular dependency, skipping '%s',\n",
  237. tmp->tpnt->libname);
  238. tpnt1->usage_count--;
  239. break;
  240. }
  241. }
  242. if (!tmp) { /* Don't add if circular dependency detected */
  243. runp2->next = alloca(sizeof(*runp));
  244. runp2 = runp2->next;
  245. runp2->tpnt = tpnt1;
  246. runp2->next = NULL;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
  253. dyn_chain->init_fini.init_fini = init_fini_list;
  254. dyn_chain->init_fini.nlist = nlist;
  255. i = 0;
  256. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  257. init_fini_list[i++] = runp2->tpnt;
  258. for(runp = runp2->tpnt->init_fini; runp; runp = runp->next){
  259. if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
  260. tmp = malloc(sizeof(struct init_fini_list));
  261. tmp->tpnt = runp->tpnt;
  262. tmp->next = runp2->tpnt->rtld_local;
  263. runp2->tpnt->rtld_local = tmp;
  264. }
  265. }
  266. }
  267. /* Sort the INIT/FINI list in dependency order. */
  268. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  269. unsigned int j, k;
  270. for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
  271. /* Empty */;
  272. for (k = j + 1; k < nlist; ++k) {
  273. struct init_fini_list *ele = init_fini_list[k]->init_fini;
  274. for (; ele; ele = ele->next) {
  275. if (ele->tpnt == runp2->tpnt) {
  276. struct elf_resolve *here = init_fini_list[k];
  277. _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
  278. for (i = (k - j); i; --i)
  279. init_fini_list[i+j] = init_fini_list[i+j-1];
  280. init_fini_list[j] = here;
  281. ++j;
  282. break;
  283. }
  284. }
  285. }
  286. }
  287. #ifdef __SUPPORT_LD_DEBUG__
  288. if(_dl_debug) {
  289. fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
  290. for (i=0;i < nlist;i++) {
  291. fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
  292. runp = init_fini_list[i]->init_fini;
  293. for (; runp; runp = runp->next)
  294. printf(" %s ", runp->tpnt->libname);
  295. printf("\n");
  296. }
  297. }
  298. #endif
  299. _dl_if_debug_print("Beginning dlopen relocation fixups\n");
  300. /*
  301. * OK, now all of the kids are tucked into bed in their proper addresses.
  302. * Now we go through and look for REL and RELA records that indicate fixups
  303. * to the GOT tables. We need to do this in reverse order so that COPY
  304. * directives work correctly */
  305. #ifdef __mips__
  306. /*
  307. * Relocation of the GOT entries for MIPS have to be done
  308. * after all the libraries have been loaded.
  309. */
  310. _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
  311. #endif
  312. if (_dl_fixup(dyn_chain, now_flag))
  313. goto oops;
  314. if (relro_ptr) {
  315. for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
  316. if (rpnt->dyn->relro_size)
  317. _dl_protect_relro(rpnt->dyn);
  318. }
  319. }
  320. /* TODO: Should we set the protections of all pages back to R/O now ? */
  321. /* Notify the debugger we have added some objects. */
  322. if (_dl_debug_addr) {
  323. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  324. if (dl_brk != NULL) {
  325. _dl_debug_addr->r_state = RT_ADD;
  326. (*dl_brk) ();
  327. _dl_debug_addr->r_state = RT_CONSISTENT;
  328. (*dl_brk) ();
  329. }
  330. }
  331. #ifdef SHARED
  332. /* Run the ctors and setup the dtors */
  333. for (i = nlist; i; --i) {
  334. tpnt = init_fini_list[i-1];
  335. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  336. continue;
  337. tpnt->init_flag |= INIT_FUNCS_CALLED;
  338. if (tpnt->dynamic_info[DT_INIT]) {
  339. void (*dl_elf_func) (void);
  340. dl_elf_func = (void (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  341. if (dl_elf_func && *dl_elf_func != NULL) {
  342. _dl_if_debug_print("running ctors for library %s at '%p'\n",
  343. tpnt->libname, dl_elf_func);
  344. (*dl_elf_func) ();
  345. }
  346. }
  347. _dl_run_init_array(tpnt);
  348. }
  349. #endif /* SHARED */
  350. _dl_unmap_cache();
  351. return (void *) dyn_chain;
  352. oops:
  353. /* Something went wrong. Clean up and return NULL. */
  354. _dl_unmap_cache();
  355. do_dlclose(dyn_chain, 0);
  356. return NULL;
  357. }
  358. void *dlsym(void *vhandle, const char *name)
  359. {
  360. struct elf_resolve *tpnt, *tfrom;
  361. struct dyn_elf *handle;
  362. ElfW(Addr) from;
  363. struct dyn_elf *rpnt;
  364. void *ret;
  365. handle = (struct dyn_elf *) vhandle;
  366. /* First of all verify that we have a real handle
  367. of some kind. Return NULL if not a valid handle. */
  368. if (handle == NULL)
  369. handle = _dl_symbol_tables;
  370. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  371. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  372. if (rpnt == handle)
  373. break;
  374. if (!rpnt) {
  375. _dl_error_number = LD_BAD_HANDLE;
  376. return NULL;
  377. }
  378. } else if (handle == RTLD_NEXT) {
  379. /*
  380. * Try and locate the module we were called from - we
  381. * need this so that we know where to start searching
  382. * from. We never pass RTLD_NEXT down into the actual
  383. * dynamic loader itself, as it doesn't know
  384. * how to properly treat it.
  385. */
  386. from = (ElfW(Addr)) __builtin_return_address(0);
  387. tfrom = NULL;
  388. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  389. tpnt = rpnt->dyn;
  390. if (tpnt->loadaddr < from
  391. && (tfrom == NULL || tfrom->loadaddr < tpnt->loadaddr)) {
  392. tfrom = tpnt;
  393. handle = rpnt->next;
  394. }
  395. }
  396. }
  397. ret = _dl_find_hash((char*)name, handle, NULL, 0);
  398. /*
  399. * Nothing found.
  400. */
  401. if (!ret)
  402. _dl_error_number = LD_NO_SYMBOL;
  403. return ret;
  404. }
  405. #if 0
  406. void *dlvsym(void *vhandle, const char *name, const char *version)
  407. {
  408. return dlsym(vhandle, name);
  409. }
  410. #endif
  411. static int do_dlclose(void *vhandle, int need_fini)
  412. {
  413. struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
  414. struct init_fini_list *runp, *tmp;
  415. ElfW(Phdr) *ppnt;
  416. struct elf_resolve *tpnt, *run_tpnt;
  417. int (*dl_elf_fini) (void);
  418. void (*dl_brk) (void);
  419. struct dyn_elf *handle;
  420. unsigned int end;
  421. unsigned int i, j;
  422. handle = (struct dyn_elf *) vhandle;
  423. if (handle == _dl_symbol_tables)
  424. return 0;
  425. rpnt1 = NULL;
  426. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  427. if (rpnt == handle)
  428. break;
  429. rpnt1 = rpnt;
  430. }
  431. if (!rpnt) {
  432. _dl_error_number = LD_BAD_HANDLE;
  433. return 1;
  434. }
  435. if (rpnt1)
  436. rpnt1->next_handle = rpnt->next_handle;
  437. else
  438. _dl_handles = rpnt->next_handle;
  439. _dl_if_debug_print("%s: usage count: %d\n",
  440. handle->dyn->libname, handle->dyn->usage_count);
  441. if (handle->dyn->usage_count != 1) {
  442. handle->dyn->usage_count--;
  443. free(handle);
  444. return 0;
  445. }
  446. /* OK, this is a valid handle - now close out the file */
  447. for (j = 0; j < handle->init_fini.nlist; ++j) {
  448. tpnt = handle->init_fini.init_fini[j];
  449. if (--tpnt->usage_count == 0) {
  450. if ((tpnt->dynamic_info[DT_FINI]
  451. || tpnt->dynamic_info[DT_FINI_ARRAY])
  452. && need_fini &&
  453. !(tpnt->init_flag & FINI_FUNCS_CALLED)) {
  454. tpnt->init_flag |= FINI_FUNCS_CALLED;
  455. #ifdef SHARED
  456. _dl_run_fini_array(tpnt);
  457. #endif
  458. if (tpnt->dynamic_info[DT_FINI]) {
  459. dl_elf_fini = (int (*)(void)) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  460. _dl_if_debug_print("running dtors for library %s at '%p'\n",
  461. tpnt->libname, dl_elf_fini);
  462. (*dl_elf_fini) ();
  463. }
  464. }
  465. _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
  466. end = 0;
  467. for (i = 0, ppnt = tpnt->ppnt;
  468. i < tpnt->n_phent; ppnt++, i++) {
  469. if (ppnt->p_type != PT_LOAD)
  470. continue;
  471. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  472. end = ppnt->p_vaddr + ppnt->p_memsz;
  473. }
  474. _dl_munmap((void*)tpnt->loadaddr, end);
  475. /* Free elements in RTLD_LOCAL scope list */
  476. for (runp = tpnt->rtld_local; runp; runp = tmp) {
  477. tmp = runp->next;
  478. free(runp);
  479. }
  480. /* Next, remove tpnt from the loaded_module list */
  481. if (_dl_loaded_modules == tpnt) {
  482. _dl_loaded_modules = tpnt->next;
  483. if (_dl_loaded_modules)
  484. _dl_loaded_modules->prev = 0;
  485. } else
  486. for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next)
  487. if (run_tpnt->next == tpnt) {
  488. _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
  489. run_tpnt->next = run_tpnt->next->next;
  490. if (run_tpnt->next)
  491. run_tpnt->next->prev = run_tpnt;
  492. break;
  493. }
  494. /* Next, remove tpnt from the global symbol table list */
  495. if (_dl_symbol_tables) {
  496. if (_dl_symbol_tables->dyn == tpnt) {
  497. _dl_symbol_tables = _dl_symbol_tables->next;
  498. if (_dl_symbol_tables)
  499. _dl_symbol_tables->prev = 0;
  500. } else
  501. for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
  502. if (rpnt1->next->dyn == tpnt) {
  503. _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
  504. rpnt1_tmp = rpnt1->next->next;
  505. free(rpnt1->next);
  506. rpnt1->next = rpnt1_tmp;
  507. if (rpnt1->next)
  508. rpnt1->next->prev = rpnt1;
  509. break;
  510. }
  511. }
  512. }
  513. free(tpnt->libname);
  514. free(tpnt);
  515. }
  516. }
  517. free(handle->init_fini.init_fini);
  518. free(handle);
  519. if (_dl_debug_addr) {
  520. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  521. if (dl_brk != NULL) {
  522. _dl_debug_addr->r_state = RT_DELETE;
  523. (*dl_brk) ();
  524. _dl_debug_addr->r_state = RT_CONSISTENT;
  525. (*dl_brk) ();
  526. }
  527. }
  528. return 0;
  529. }
  530. int dlclose(void *vhandle)
  531. {
  532. return do_dlclose(vhandle, 1);
  533. }
  534. char *dlerror(void)
  535. {
  536. const char *retval;
  537. if (!_dl_error_number)
  538. return NULL;
  539. retval = dl_error_names[_dl_error_number];
  540. _dl_error_number = 0;
  541. return (char *)retval;
  542. }
  543. /*
  544. * Dump information to stderrr about the current loaded modules
  545. */
  546. #ifdef __USE_GNU
  547. static char *type[] = { "Lib", "Exe", "Int", "Mod" };
  548. int dlinfo(void)
  549. {
  550. struct elf_resolve *tpnt;
  551. struct dyn_elf *rpnt, *hpnt;
  552. fprintf(stderr, "List of loaded modules\n");
  553. /* First start with a complete list of all of the loaded files. */
  554. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  555. fprintf(stderr, "\t%p %p %p %s %d %s\n",
  556. tpnt->loadaddr, tpnt, tpnt->symbol_scope,
  557. type[tpnt->libtype],
  558. tpnt->usage_count, tpnt->libname);
  559. }
  560. /* Next dump the module list for the application itself */
  561. fprintf(stderr, "\nModules for application (%p):\n", _dl_symbol_tables);
  562. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  563. fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
  564. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  565. fprintf(stderr, "Modules for handle %p\n", hpnt);
  566. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  567. fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
  568. }
  569. return 0;
  570. }
  571. int dladdr(const void *__address, Dl_info * __info)
  572. {
  573. struct elf_resolve *pelf;
  574. struct elf_resolve *rpnt;
  575. _dl_map_cache();
  576. /*
  577. * Try and locate the module address is in
  578. */
  579. pelf = NULL;
  580. #if 0
  581. fprintf(stderr, "dladdr( %p, %p )\n", __address, __info);
  582. #endif
  583. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  584. struct elf_resolve *tpnt;
  585. tpnt = rpnt;
  586. #if 0
  587. fprintf(stderr, "Module \"%s\" at %p\n",
  588. tpnt->libname, tpnt->loadaddr);
  589. #endif
  590. if (tpnt->loadaddr < (ElfW(Addr)) __address
  591. && (pelf == NULL || pelf->loadaddr < tpnt->loadaddr)) {
  592. pelf = tpnt;
  593. }
  594. }
  595. if (!pelf) {
  596. return 0;
  597. }
  598. /*
  599. * Try and locate the symbol of address
  600. */
  601. {
  602. char *strtab;
  603. ElfW(Sym) *symtab;
  604. unsigned int hn, si, sn, sf;
  605. ElfW(Addr) sa;
  606. sa = 0;
  607. symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
  608. strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
  609. sf = sn = 0;
  610. for (hn = 0; hn < pelf->nbucket; hn++) {
  611. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  612. ElfW(Addr) symbol_addr;
  613. symbol_addr = pelf->loadaddr + symtab[si].st_value;
  614. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  615. sa = symbol_addr;
  616. sn = si;
  617. sf = 1;
  618. }
  619. #if 0
  620. fprintf(stderr, "Symbol \"%s\" at %p\n",
  621. strtab + symtab[si].st_name, symbol_addr);
  622. #endif
  623. }
  624. }
  625. if (sf) {
  626. __info->dli_fname = pelf->libname;
  627. __info->dli_fbase = (void *)pelf->loadaddr;
  628. __info->dli_sname = strtab + symtab[sn].st_name;
  629. __info->dli_saddr = (void *)sa;
  630. }
  631. return 1;
  632. }
  633. }
  634. #endif