libdl.c 22 KB

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