libdl.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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 __UCLIBC_HAS_TLS__
  36. #include <tls.h>
  37. #endif
  38. #if USE_TLS
  39. #include <ldsodefs.h>
  40. extern void (*_dl_init_static_tls) (struct link_map *);
  41. extern void _dl_add_to_slotinfo(struct link_map *l);
  42. #endif
  43. #ifdef SHARED
  44. # if USE_TLS
  45. # include <dl-tls.h>
  46. extern struct link_map *_dl_update_slotinfo(unsigned long int req_modid);
  47. # endif
  48. /* When libdl is loaded as a shared library, we need to load in
  49. * and use a pile of symbols from ldso... */
  50. extern struct elf_resolve * _dl_load_shared_library(int, struct dyn_elf **,
  51. struct elf_resolve *, char *, int);
  52. extern int _dl_fixup(struct dyn_elf *rpnt, int lazy);
  53. extern void _dl_protect_relro(struct elf_resolve * tpnt);
  54. extern int _dl_errno;
  55. extern struct dyn_elf *_dl_symbol_tables;
  56. extern struct dyn_elf *_dl_handles;
  57. extern struct elf_resolve *_dl_loaded_modules;
  58. extern void _dl_free (void *__ptr);
  59. extern struct r_debug *_dl_debug_addr;
  60. extern unsigned long _dl_error_number;
  61. extern void *(*_dl_malloc_function)(size_t);
  62. extern void (*_dl_free_function) (void *p);
  63. extern void _dl_run_init_array(struct elf_resolve *);
  64. extern void _dl_run_fini_array(struct elf_resolve *);
  65. #ifdef __LDSO_CACHE_SUPPORT__
  66. int _dl_map_cache(void);
  67. int _dl_unmap_cache(void);
  68. #endif
  69. #ifdef __mips__
  70. extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
  71. #endif
  72. #ifdef __SUPPORT_LD_DEBUG__
  73. extern char *_dl_debug;
  74. #endif
  75. #else /* !SHARED */
  76. #define _dl_malloc malloc
  77. #define _dl_free free
  78. /* When libdl is linked as a static library, we need to replace all
  79. * the symbols that otherwise would have been loaded in from ldso... */
  80. #ifdef __SUPPORT_LD_DEBUG__
  81. char *_dl_debug = NULL;
  82. char *_dl_debug_symbols = NULL;
  83. char *_dl_debug_move = NULL;
  84. char *_dl_debug_reloc = NULL;
  85. char *_dl_debug_detail = NULL;
  86. char *_dl_debug_nofixups = NULL;
  87. char *_dl_debug_bindings = NULL;
  88. int _dl_debug_file = 2;
  89. #endif
  90. const char *_dl_progname = ""; /* Program name */
  91. void *(*_dl_malloc_function)(size_t);
  92. void (*_dl_free_function) (void *p);
  93. char *_dl_library_path = NULL; /* Where we look for libraries */
  94. char *_dl_ldsopath = NULL; /* Location of the shared lib loader */
  95. int _dl_errno = 0; /* We can't use the real errno in ldso */
  96. size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
  97. /* This global variable is also to communicate with debuggers such as gdb. */
  98. struct r_debug *_dl_debug_addr = NULL;
  99. #include "../ldso/dl-array.c"
  100. #include "../ldso/dl-debug.c"
  101. # if USE_TLS
  102. /*
  103. * Giving this initialized value preallocates some surplus bytes in the
  104. * static TLS area, see __libc_setup_tls (libc-tls.c).
  105. */
  106. size_t _dl_tls_static_size = 2048;
  107. # endif
  108. #include LDSO_ELFINTERP
  109. #include "../ldso/dl-hash.c"
  110. #define _dl_trace_loaded_objects 0
  111. #include "../ldso/dl-elf.c"
  112. #endif /* SHARED */
  113. #ifdef __SUPPORT_LD_DEBUG__
  114. # define _dl_if_debug_print(fmt, args...) \
  115. do { \
  116. if (_dl_debug) \
  117. fprintf(stderr, "%s():%i: " fmt, __FUNCTION__, __LINE__, ## args); \
  118. } while (0)
  119. #else
  120. # define _dl_if_debug_print(fmt, args...)
  121. #endif
  122. static int do_dlclose(void *, int need_fini);
  123. static const char *const dl_error_names[] = {
  124. "",
  125. "File not found",
  126. "Unable to open /dev/zero",
  127. "Not an ELF file",
  128. #if defined (__i386__)
  129. "Not i386 binary",
  130. #elif defined (__sparc__)
  131. "Not sparc binary",
  132. #elif defined (__mc68000__)
  133. "Not m68k binary",
  134. #else
  135. "Unrecognized binary type",
  136. #endif
  137. "Not an ELF shared library",
  138. "Unable to mmap file",
  139. "No dynamic section",
  140. "Library contains unsupported TLS",
  141. #ifdef ELF_USES_RELOCA
  142. "Unable to process REL relocs",
  143. #else
  144. "Unable to process RELA relocs",
  145. #endif
  146. "Bad handle",
  147. "Unable to resolve symbol"
  148. };
  149. #if USE_TLS
  150. #ifdef SHARED
  151. /*
  152. * Systems which do not have tls_index also probably have to define
  153. * DONT_USE_TLS_INDEX.
  154. */
  155. # ifndef __TLS_GET_ADDR
  156. # define __TLS_GET_ADDR __tls_get_addr
  157. # endif
  158. /*
  159. * Return the symbol address given the map of the module it is in and
  160. * the symbol record. This is used in dl-sym.c.
  161. */
  162. static void *
  163. internal_function
  164. _dl_tls_symaddr(struct link_map *map, const Elf32_Addr st_value)
  165. {
  166. # ifndef DONT_USE_TLS_INDEX
  167. tls_index tmp =
  168. {
  169. .ti_module = map->l_tls_modid,
  170. .ti_offset = st_value
  171. };
  172. return __TLS_GET_ADDR (&tmp);
  173. # else
  174. return __TLS_GET_ADDR (map->l_tls_modid, st_value);
  175. # endif
  176. }
  177. #endif
  178. /* Returns true we an non-empty was found. */
  179. static bool
  180. remove_slotinfo(size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
  181. bool should_be_there)
  182. {
  183. if(idx - disp >= listp->len)
  184. {
  185. if(listp->next == NULL)
  186. {
  187. /*
  188. * The index is not actually valid in the slotinfo list,
  189. * because this object was closed before it was fully set
  190. * up due to some error.
  191. */
  192. _dl_assert(!should_be_there);
  193. }
  194. else
  195. {
  196. if(remove_slotinfo(idx, listp->next, disp + listp->len,
  197. should_be_there))
  198. return true;
  199. /*
  200. * No non-empty entry. Search from the end of this element's
  201. * slotinfo array.
  202. */
  203. idx = disp + listp->len;
  204. }
  205. }
  206. else
  207. {
  208. struct link_map *old_map = listp->slotinfo[idx - disp].map;
  209. /*
  210. * The entry might still be in its unused state if we are
  211. * closing an object that wasn't fully set up.
  212. */
  213. if(__builtin_expect(old_map != NULL, 1))
  214. {
  215. _dl_assert(old_map->l_tls_modid == idx);
  216. /* Mark the entry as unused. */
  217. listp->slotinfo[idx - disp].gen = _dl_tls_generation + 1;
  218. listp->slotinfo[idx - disp].map = NULL;
  219. }
  220. /*
  221. * If this is not the last currently used entry no need to
  222. * look further.
  223. */
  224. if (idx != _dl_tls_max_dtv_idx)
  225. return true;
  226. }
  227. while(idx - disp > (disp == 0 ? 1 + _dl_tls_static_nelem : 0))
  228. {
  229. --idx;
  230. if(listp->slotinfo[idx - disp].map != NULL)
  231. {
  232. /* Found a new last used index. */
  233. _dl_tls_max_dtv_idx = idx;
  234. return true;
  235. }
  236. }
  237. /* No non-entry in this list element. */
  238. return false;
  239. }
  240. #endif
  241. void dl_cleanup(void) __attribute__ ((destructor));
  242. void dl_cleanup(void)
  243. {
  244. struct dyn_elf *h, *n;
  245. for (h = _dl_handles; h; h = n) {
  246. n = h->next_handle;
  247. do_dlclose(h, 1);
  248. }
  249. }
  250. void *dlopen(const char *libname, int flag)
  251. {
  252. struct elf_resolve *tpnt, *tfrom;
  253. struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
  254. ElfW(Addr) from;
  255. struct elf_resolve *tpnt1;
  256. void (*dl_brk) (void);
  257. int now_flag;
  258. struct init_fini_list *tmp, *runp, *runp2, *dep_list;
  259. unsigned int nlist, i;
  260. struct elf_resolve **init_fini_list;
  261. static bool _dl_init;
  262. #if USE_TLS
  263. bool any_tls = false;
  264. #endif
  265. /* A bit of sanity checking... */
  266. if (!(flag & (RTLD_LAZY|RTLD_NOW))) {
  267. _dl_error_number = LD_BAD_HANDLE;
  268. return NULL;
  269. }
  270. from = (ElfW(Addr)) __builtin_return_address(0);
  271. if (!_dl_init) {
  272. _dl_init = true;
  273. _dl_malloc_function = malloc;
  274. _dl_free_function = free;
  275. }
  276. /* Cover the trivial case first */
  277. if (!libname)
  278. return _dl_symbol_tables;
  279. #ifndef SHARED
  280. # ifdef __SUPPORT_LD_DEBUG__
  281. _dl_debug = getenv("LD_DEBUG");
  282. if (_dl_debug) {
  283. if (_dl_strstr(_dl_debug, "all")) {
  284. _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
  285. = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
  286. } else {
  287. _dl_debug_detail = strstr(_dl_debug, "detail");
  288. _dl_debug_move = strstr(_dl_debug, "move");
  289. _dl_debug_symbols = strstr(_dl_debug, "sym");
  290. _dl_debug_reloc = strstr(_dl_debug, "reloc");
  291. _dl_debug_nofixups = strstr(_dl_debug, "nofix");
  292. _dl_debug_bindings = strstr(_dl_debug, "bind");
  293. }
  294. }
  295. # endif
  296. #endif
  297. _dl_map_cache();
  298. /*
  299. * Try and locate the module we were called from - we
  300. * need this so that we get the correct RPATH/RUNPATH. Note that
  301. * this is the current behavior under Solaris, but the
  302. * ABI+ specifies that we should only use the RPATH from
  303. * the application. Thus this may go away at some time
  304. * in the future.
  305. */
  306. {
  307. struct dyn_elf *dpnt;
  308. tfrom = NULL;
  309. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  310. tpnt = dpnt->dyn;
  311. if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom))
  312. tfrom = tpnt;
  313. }
  314. }
  315. for (rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt = rpnt->next)
  316. continue;
  317. relro_ptr = rpnt;
  318. now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
  319. if (getenv("LD_BIND_NOW"))
  320. now_flag = RTLD_NOW;
  321. #ifndef SHARED
  322. /* When statically linked, the _dl_library_path is not yet initialized */
  323. _dl_library_path = getenv("LD_LIBRARY_PATH");
  324. #endif
  325. /* Try to load the specified library */
  326. _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
  327. (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
  328. tpnt = _dl_load_shared_library(0, &rpnt, tfrom, (char*)libname, 0);
  329. if (tpnt == NULL) {
  330. _dl_unmap_cache();
  331. return NULL;
  332. }
  333. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  334. _dl_memset(dyn_chain, 0, sizeof(struct dyn_elf));
  335. dyn_chain->dyn = tpnt;
  336. tpnt->rtld_flags |= (flag & RTLD_GLOBAL);
  337. dyn_chain->next_handle = _dl_handles;
  338. _dl_handles = dyn_ptr = dyn_chain;
  339. if (tpnt->usage_count > 1) {
  340. _dl_if_debug_print("Lib: %s already opened\n", libname);
  341. /* see if there is a handle from a earlier dlopen */
  342. for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
  343. if (handle->dyn == tpnt) {
  344. dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
  345. dyn_chain->init_fini.nlist = handle->init_fini.nlist;
  346. for (i = 0; i < dyn_chain->init_fini.nlist; i++)
  347. dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & RTLD_GLOBAL);
  348. dyn_chain->next = handle->next;
  349. break;
  350. }
  351. }
  352. return dyn_chain;
  353. }
  354. tpnt->init_flag |= DL_OPENED;
  355. _dl_if_debug_print("Looking for needed libraries\n");
  356. nlist = 0;
  357. runp = alloca(sizeof(*runp));
  358. runp->tpnt = tpnt;
  359. runp->next = NULL;
  360. dep_list = runp2 = runp;
  361. for (; runp; runp = runp->next) {
  362. ElfW(Dyn) *dpnt;
  363. char *lpntstr;
  364. nlist++;
  365. runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
  366. for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
  367. if (dpnt->d_tag == DT_NEEDED) {
  368. lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
  369. dpnt->d_un.d_val);
  370. _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
  371. lpntstr, runp->tpnt->libname);
  372. tpnt1 = _dl_load_shared_library(0, &rpnt, runp->tpnt, lpntstr, 0);
  373. if (!tpnt1)
  374. goto oops;
  375. tpnt1->rtld_flags |= (flag & RTLD_GLOBAL);
  376. /* This list is for dlsym() and relocation */
  377. dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  378. _dl_memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
  379. dyn_ptr = dyn_ptr->next;
  380. dyn_ptr->dyn = tpnt1;
  381. /* Used to record RTLD_LOCAL scope */
  382. tmp = alloca(sizeof(struct init_fini_list));
  383. tmp->tpnt = tpnt1;
  384. tmp->next = runp->tpnt->init_fini;
  385. runp->tpnt->init_fini = tmp;
  386. for (tmp=dep_list; tmp; tmp = tmp->next) {
  387. if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
  388. _dl_if_debug_print("Circular dependency, skipping '%s',\n",
  389. tmp->tpnt->libname);
  390. tpnt1->usage_count--;
  391. break;
  392. }
  393. }
  394. if (!tmp) { /* Don't add if circular dependency detected */
  395. runp2->next = alloca(sizeof(*runp));
  396. runp2 = runp2->next;
  397. runp2->tpnt = tpnt1;
  398. runp2->next = NULL;
  399. }
  400. }
  401. }
  402. }
  403. init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
  404. dyn_chain->init_fini.init_fini = init_fini_list;
  405. dyn_chain->init_fini.nlist = nlist;
  406. i = 0;
  407. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  408. init_fini_list[i++] = runp2->tpnt;
  409. for (runp = runp2->tpnt->init_fini; runp; runp = runp->next) {
  410. if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
  411. tmp = malloc(sizeof(struct init_fini_list));
  412. tmp->tpnt = runp->tpnt;
  413. tmp->next = runp2->tpnt->rtld_local;
  414. runp2->tpnt->rtld_local = tmp;
  415. }
  416. }
  417. }
  418. /* Sort the INIT/FINI list in dependency order. */
  419. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  420. unsigned int j, k;
  421. for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
  422. /* Empty */;
  423. for (k = j + 1; k < nlist; ++k) {
  424. struct init_fini_list *ele = init_fini_list[k]->init_fini;
  425. for (; ele; ele = ele->next) {
  426. if (ele->tpnt == runp2->tpnt) {
  427. struct elf_resolve *here = init_fini_list[k];
  428. _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
  429. for (i = (k - j); i; --i)
  430. init_fini_list[i+j] = init_fini_list[i+j-1];
  431. init_fini_list[j] = here;
  432. ++j;
  433. break;
  434. }
  435. }
  436. }
  437. }
  438. #ifdef __SUPPORT_LD_DEBUG__
  439. if (_dl_debug) {
  440. fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
  441. for (i = 0; i < nlist; i++) {
  442. fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
  443. runp = init_fini_list[i]->init_fini;
  444. for (; runp; runp = runp->next)
  445. fprintf(stderr, " %s ", runp->tpnt->libname);
  446. fprintf(stderr, "\n");
  447. }
  448. }
  449. #endif
  450. _dl_if_debug_print("Beginning dlopen relocation fixups\n");
  451. /*
  452. * OK, now all of the kids are tucked into bed in their proper addresses.
  453. * Now we go through and look for REL and RELA records that indicate fixups
  454. * to the GOT tables. We need to do this in reverse order so that COPY
  455. * directives work correctly */
  456. #ifdef __mips__
  457. /*
  458. * Relocation of the GOT entries for MIPS have to be done
  459. * after all the libraries have been loaded.
  460. */
  461. _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
  462. #endif
  463. if (_dl_fixup(dyn_chain, now_flag))
  464. goto oops;
  465. if (relro_ptr) {
  466. for (rpnt = relro_ptr->next; rpnt; rpnt = rpnt->next) {
  467. if (rpnt->dyn->relro_size)
  468. _dl_protect_relro(rpnt->dyn);
  469. }
  470. }
  471. /* TODO: Should we set the protections of all pages back to R/O now ? */
  472. #if USE_TLS
  473. for (i=0; i < nlist; i++) {
  474. struct elf_resolve *tmp_tpnt = init_fini_list[i];
  475. /* Only add TLS memory if this object is loaded now and
  476. therefore is not yet initialized. */
  477. if (!(tmp_tpnt->init_flag & INIT_FUNCS_CALLED)
  478. /* Only if the module defines thread local data. */
  479. && __builtin_expect (tmp_tpnt->l_tls_blocksize > 0, 0)) {
  480. /* Now that we know the object is loaded successfully add
  481. modules containing TLS data to the slot info table. We
  482. might have to increase its size. */
  483. _dl_add_to_slotinfo ((struct link_map*)tmp_tpnt);
  484. /* It is the case in which we couldn't perform TLS static
  485. initialization at relocation time, and we delayed it until
  486. the relocation has been completed. */
  487. if (tmp_tpnt->l_need_tls_init) {
  488. tmp_tpnt->l_need_tls_init = 0;
  489. # ifdef SHARED
  490. /* Update the slot information data for at least the
  491. generation of the DSO we are allocating data for. */
  492. _dl_update_slotinfo (tmp_tpnt->l_tls_modid);
  493. # endif
  494. _dl_init_static_tls((struct link_map*)tmp_tpnt);
  495. _dl_assert (tmp_tpnt->l_need_tls_init == 0);
  496. }
  497. /* We have to bump the generation counter. */
  498. any_tls = true;
  499. }
  500. }
  501. /* Bump the generation number if necessary. */
  502. if (any_tls && __builtin_expect (++_dl_tls_generation == 0, 0)) {
  503. _dl_debug_early("TLS generation counter wrapped! Please report this.");
  504. _dl_exit(30);
  505. }
  506. #endif
  507. /* Notify the debugger we have added some objects. */
  508. if (_dl_debug_addr) {
  509. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  510. if (dl_brk != NULL) {
  511. _dl_debug_addr->r_state = RT_ADD;
  512. (*dl_brk) ();
  513. _dl_debug_addr->r_state = RT_CONSISTENT;
  514. (*dl_brk) ();
  515. }
  516. }
  517. /* Run the ctors and setup the dtors */
  518. for (i = nlist; i; --i) {
  519. tpnt = init_fini_list[i-1];
  520. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  521. continue;
  522. tpnt->init_flag |= INIT_FUNCS_CALLED;
  523. if (tpnt->dynamic_info[DT_INIT]) {
  524. void (*dl_elf_func) (void);
  525. dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
  526. if (dl_elf_func) {
  527. _dl_if_debug_print("running ctors for library %s at '%p'\n",
  528. tpnt->libname, dl_elf_func);
  529. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  530. }
  531. }
  532. _dl_run_init_array(tpnt);
  533. }
  534. _dl_unmap_cache();
  535. return (void *) dyn_chain;
  536. oops:
  537. /* Something went wrong. Clean up and return NULL. */
  538. _dl_unmap_cache();
  539. do_dlclose(dyn_chain, 0);
  540. return NULL;
  541. }
  542. void *dlsym(void *vhandle, const char *name)
  543. {
  544. struct elf_resolve *tpnt, *tfrom;
  545. struct dyn_elf *handle;
  546. ElfW(Addr) from;
  547. struct dyn_elf *rpnt;
  548. void *ret;
  549. struct elf_resolve *tls_tpnt = NULL;
  550. /* Nastiness to support underscore prefixes. */
  551. #ifdef __UCLIBC_UNDERSCORES__
  552. char tmp_buf[80];
  553. char *name2 = tmp_buf;
  554. size_t nlen = strlen (name) + 1;
  555. if (nlen + 1 > sizeof (tmp_buf))
  556. name2 = malloc (nlen + 1);
  557. if (name2 == 0) {
  558. _dl_error_number = LD_ERROR_MMAP_FAILED;
  559. return 0;
  560. }
  561. name2[0] = '_';
  562. memcpy (name2 + 1, name, nlen);
  563. #else
  564. const char *name2 = name;
  565. #endif
  566. handle = (struct dyn_elf *) vhandle;
  567. /* First of all verify that we have a real handle
  568. of some kind. Return NULL if not a valid handle. */
  569. if (handle == NULL)
  570. handle = _dl_symbol_tables;
  571. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  572. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  573. if (rpnt == handle)
  574. break;
  575. if (!rpnt) {
  576. _dl_error_number = LD_BAD_HANDLE;
  577. ret = NULL;
  578. goto out;
  579. }
  580. } else if (handle == RTLD_NEXT) {
  581. /*
  582. * Try and locate the module we were called from - we
  583. * need this so that we know where to start searching
  584. * from. We never pass RTLD_NEXT down into the actual
  585. * dynamic loader itself, as it doesn't know
  586. * how to properly treat it.
  587. */
  588. from = (ElfW(Addr)) __builtin_return_address(0);
  589. tfrom = NULL;
  590. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  591. tpnt = rpnt->dyn;
  592. if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom)) {
  593. tfrom = tpnt;
  594. handle = rpnt->next;
  595. }
  596. }
  597. }
  598. tpnt = NULL;
  599. if (handle == _dl_symbol_tables)
  600. tpnt = handle->dyn; /* Only search RTLD_GLOBAL objs if global object */
  601. ret = _dl_find_hash(name2, handle, NULL, 0, &tls_tpnt);
  602. #if defined USE_TLS && defined SHARED
  603. if(tls_tpnt) {
  604. /* The found symbol is a thread-local storage variable.
  605. Return the address for to the current thread. */
  606. ret = _dl_tls_symaddr ((struct link_map *)tls_tpnt, (Elf32_Addr)ret);
  607. }
  608. #endif
  609. /*
  610. * Nothing found.
  611. */
  612. if (!ret)
  613. _dl_error_number = LD_NO_SYMBOL;
  614. out:
  615. #ifdef __UCLIBC_UNDERSCORES__
  616. if (name2 != tmp_buf)
  617. free (name2);
  618. #endif
  619. return ret;
  620. }
  621. #if 0
  622. void *dlvsym(void *vhandle, const char *name, const char *version)
  623. {
  624. return dlsym(vhandle, name);
  625. }
  626. #endif
  627. static int do_dlclose(void *vhandle, int need_fini)
  628. {
  629. struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
  630. struct init_fini_list *runp, *tmp;
  631. ElfW(Phdr) *ppnt;
  632. struct elf_resolve *tpnt, *run_tpnt;
  633. int (*dl_elf_fini) (void);
  634. void (*dl_brk) (void);
  635. struct dyn_elf *handle;
  636. unsigned int end;
  637. unsigned int i, j;
  638. #if USE_TLS
  639. bool any_tls = false;
  640. size_t tls_free_start = NO_TLS_OFFSET;
  641. size_t tls_free_end = NO_TLS_OFFSET;
  642. struct link_map *tls_lmap;
  643. #endif
  644. handle = (struct dyn_elf *) vhandle;
  645. if (handle == _dl_symbol_tables)
  646. return 0;
  647. rpnt1 = NULL;
  648. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  649. if (rpnt == handle)
  650. break;
  651. rpnt1 = rpnt;
  652. }
  653. if (!rpnt) {
  654. _dl_error_number = LD_BAD_HANDLE;
  655. return 1;
  656. }
  657. if (rpnt1)
  658. rpnt1->next_handle = rpnt->next_handle;
  659. else
  660. _dl_handles = rpnt->next_handle;
  661. _dl_if_debug_print("%s: usage count: %d\n",
  662. handle->dyn->libname, handle->dyn->usage_count);
  663. if (handle->dyn->usage_count != 1) {
  664. handle->dyn->usage_count--;
  665. free(handle);
  666. return 0;
  667. }
  668. /* OK, this is a valid handle - now close out the file */
  669. for (j = 0; j < handle->init_fini.nlist; ++j) {
  670. tpnt = handle->init_fini.init_fini[j];
  671. if (--tpnt->usage_count == 0) {
  672. if ((tpnt->dynamic_info[DT_FINI]
  673. || tpnt->dynamic_info[DT_FINI_ARRAY])
  674. && need_fini
  675. && !(tpnt->init_flag & FINI_FUNCS_CALLED)
  676. ) {
  677. tpnt->init_flag |= FINI_FUNCS_CALLED;
  678. _dl_run_fini_array(tpnt);
  679. if (tpnt->dynamic_info[DT_FINI]) {
  680. dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
  681. _dl_if_debug_print("running dtors for library %s at '%p'\n",
  682. tpnt->libname, dl_elf_fini);
  683. DL_CALL_FUNC_AT_ADDR (dl_elf_fini, tpnt->loadaddr, (int (*)(void)));
  684. }
  685. }
  686. _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
  687. end = 0;
  688. for (i = 0, ppnt = tpnt->ppnt;
  689. i < tpnt->n_phent; ppnt++, i++) {
  690. if (ppnt->p_type != PT_LOAD)
  691. continue;
  692. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  693. end = ppnt->p_vaddr + ppnt->p_memsz;
  694. }
  695. #if USE_TLS
  696. /* Do the cast to make things easy. */
  697. tls_lmap = (struct link_map *) tpnt;
  698. /* Remove the object from the dtv slotinfo array if it uses TLS. */
  699. if (__builtin_expect (tls_lmap->l_tls_blocksize > 0, 0))
  700. {
  701. any_tls = true;
  702. if (_dl_tls_dtv_slotinfo_list != NULL
  703. && ! remove_slotinfo (tls_lmap->l_tls_modid,
  704. _dl_tls_dtv_slotinfo_list, 0,
  705. (tpnt->init_flag & INIT_FUNCS_CALLED)))
  706. /* All dynamically loaded modules with TLS are unloaded. */
  707. _dl_tls_max_dtv_idx = _dl_tls_static_nelem;
  708. if (tls_lmap->l_tls_offset != NO_TLS_OFFSET)
  709. {
  710. /*
  711. * Collect a contiguous chunk built from the objects in
  712. * this search list, going in either direction. When the
  713. * whole chunk is at the end of the used area then we can
  714. * reclaim it.
  715. */
  716. # if defined(TLS_TCB_AT_TP)
  717. if (tls_free_start == NO_TLS_OFFSET
  718. || (size_t) tls_lmap->l_tls_offset == tls_free_start)
  719. {
  720. /* Extend the contiguous chunk being reclaimed. */
  721. tls_free_start
  722. = tls_lmap->l_tls_offset -
  723. tls_lmap->l_tls_blocksize;
  724. if (tls_free_end == NO_TLS_OFFSET)
  725. tls_free_end = tls_lmap->l_tls_offset;
  726. }
  727. else if (tls_lmap->l_tls_offset - tls_lmap->l_tls_blocksize
  728. == tls_free_end)
  729. /* Extend the chunk backwards. */
  730. tls_free_end = tls_lmap->l_tls_offset;
  731. else
  732. {
  733. /*
  734. * This isn't contiguous with the last chunk freed.
  735. * One of them will be leaked unless we can free
  736. * one block right away.
  737. */
  738. if (tls_free_end == _dl_tls_static_used)
  739. {
  740. _dl_tls_static_used = tls_free_start;
  741. tls_free_end = tls_lmap->l_tls_offset;
  742. tls_free_start
  743. = tls_free_end - tls_lmap->l_tls_blocksize;
  744. }
  745. else if ((size_t) tls_lmap->l_tls_offset
  746. == _dl_tls_static_used)
  747. _dl_tls_static_used = tls_lmap->l_tls_offset -
  748. tls_lmap->l_tls_blocksize;
  749. else if (tls_free_end < (size_t) tls_lmap->l_tls_offset)
  750. {
  751. /*
  752. * We pick the later block. It has a chance
  753. * to be freed.
  754. */
  755. tls_free_end = tls_lmap->l_tls_offset;
  756. tls_free_start = tls_free_end -
  757. tls_lmap->l_tls_blocksize;
  758. }
  759. }
  760. # elif defined(TLS_DTV_AT_TP)
  761. if ((size_t) tls_lmap->l_tls_offset == tls_free_end)
  762. /* Extend the contiguous chunk being reclaimed. */
  763. tls_free_end -= tls_lmap->l_tls_blocksize;
  764. else if (tls_lmap->l_tls_offset + tls_lmap->l_tls_blocksize
  765. == tls_free_start)
  766. /* Extend the chunk backwards. */
  767. tls_free_start = tls_lmap->l_tls_offset;
  768. else
  769. {
  770. /*
  771. * This isn't contiguous with the last chunk
  772. * freed. One of them will be leaked.
  773. */
  774. if (tls_free_end == _dl_tls_static_used)
  775. _dl_tls_static_used = tls_free_start;
  776. tls_free_start = tls_lmap->l_tls_offset;
  777. tls_free_end = tls_free_start +
  778. tls_lmap->l_tls_blocksize;
  779. }
  780. # else
  781. # error "Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined"
  782. # endif
  783. } else {
  784. #define TLS_DTV_UNALLOCATED ((void *) -1l)
  785. dtv_t *dtv = THREAD_DTV ();
  786. _dl_assert(!(dtv[tls_lmap->l_tls_modid].pointer.is_static));
  787. if(dtv[tls_lmap->l_tls_modid].pointer.val != TLS_DTV_UNALLOCATED) {
  788. /* Note that free is called for NULL is well. We
  789. deallocate even if it is this dtv entry we are
  790. supposed to load. The reason is that we call
  791. memalign and not malloc. */
  792. _dl_free (dtv[tls_lmap->l_tls_modid].pointer.val);
  793. dtv[tls_lmap->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
  794. }
  795. }
  796. }
  797. #endif
  798. DL_LIB_UNMAP (tpnt, end);
  799. /* Free elements in RTLD_LOCAL scope list */
  800. for (runp = tpnt->rtld_local; runp; runp = tmp) {
  801. tmp = runp->next;
  802. free(runp);
  803. }
  804. /* Next, remove tpnt from the loaded_module list */
  805. if (_dl_loaded_modules == tpnt) {
  806. _dl_loaded_modules = tpnt->next;
  807. if (_dl_loaded_modules)
  808. _dl_loaded_modules->prev = 0;
  809. } else {
  810. for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next) {
  811. if (run_tpnt->next == tpnt) {
  812. _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
  813. run_tpnt->next = run_tpnt->next->next;
  814. if (run_tpnt->next)
  815. run_tpnt->next->prev = run_tpnt;
  816. break;
  817. }
  818. }
  819. }
  820. /* Next, remove tpnt from the global symbol table list */
  821. if (_dl_symbol_tables) {
  822. if (_dl_symbol_tables->dyn == tpnt) {
  823. _dl_symbol_tables = _dl_symbol_tables->next;
  824. if (_dl_symbol_tables)
  825. _dl_symbol_tables->prev = 0;
  826. } else {
  827. for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
  828. if (rpnt1->next->dyn == tpnt) {
  829. _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
  830. rpnt1_tmp = rpnt1->next->next;
  831. free(rpnt1->next);
  832. rpnt1->next = rpnt1_tmp;
  833. if (rpnt1->next)
  834. rpnt1->next->prev = rpnt1;
  835. break;
  836. }
  837. }
  838. }
  839. }
  840. free(tpnt->libname);
  841. free(tpnt);
  842. }
  843. }
  844. free(handle->init_fini.init_fini);
  845. free(handle);
  846. #if USE_TLS
  847. /* If we removed any object which uses TLS bump the generation counter. */
  848. if (any_tls)
  849. {
  850. if (__builtin_expect (++_dl_tls_generation == 0, 0))
  851. {
  852. _dl_debug_early ("TLS generation counter wrapped! Please report to the uClibc mailing list.\n");
  853. _dl_exit(30);
  854. }
  855. if (tls_free_end == _dl_tls_static_used)
  856. _dl_tls_static_used = tls_free_start;
  857. }
  858. #endif
  859. if (_dl_debug_addr) {
  860. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  861. if (dl_brk != NULL) {
  862. _dl_debug_addr->r_state = RT_DELETE;
  863. (*dl_brk) ();
  864. _dl_debug_addr->r_state = RT_CONSISTENT;
  865. (*dl_brk) ();
  866. }
  867. }
  868. return 0;
  869. }
  870. int dlclose(void *vhandle)
  871. {
  872. return do_dlclose(vhandle, 1);
  873. }
  874. char *dlerror(void)
  875. {
  876. const char *retval;
  877. if (!_dl_error_number)
  878. return NULL;
  879. retval = dl_error_names[_dl_error_number];
  880. _dl_error_number = 0;
  881. return (char *)retval;
  882. }
  883. /*
  884. * Dump information to stderr about the current loaded modules
  885. */
  886. #ifdef __USE_GNU
  887. static const char type[][4] = { "Lib", "Exe", "Int", "Mod" };
  888. int dlinfo(void)
  889. {
  890. struct elf_resolve *tpnt;
  891. struct dyn_elf *rpnt, *hpnt;
  892. fprintf(stderr, "List of loaded modules\n");
  893. /* First start with a complete list of all of the loaded files. */
  894. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  895. fprintf(stderr, "\t%p %p %p %s %d %s\n",
  896. DL_LOADADDR_BASE(tpnt->loadaddr), tpnt, tpnt->symbol_scope,
  897. type[tpnt->libtype],
  898. tpnt->usage_count, tpnt->libname);
  899. }
  900. /* Next dump the module list for the application itself */
  901. fprintf(stderr, "\nModules for application (%p):\n", _dl_symbol_tables);
  902. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next)
  903. fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
  904. for (hpnt = _dl_handles; hpnt; hpnt = hpnt->next_handle) {
  905. fprintf(stderr, "Modules for handle %p\n", hpnt);
  906. for (rpnt = hpnt; rpnt; rpnt = rpnt->next)
  907. fprintf(stderr, "\t%p %s\n", rpnt->dyn, rpnt->dyn->libname);
  908. }
  909. return 0;
  910. }
  911. int dladdr(const void *__address, Dl_info * __info)
  912. {
  913. struct elf_resolve *pelf;
  914. struct elf_resolve *rpnt;
  915. _dl_map_cache();
  916. /*
  917. * Try and locate the module address is in
  918. */
  919. pelf = NULL;
  920. _dl_if_debug_print("__address: %p __info: %p\n", __address, __info);
  921. __address = DL_LOOKUP_ADDRESS (__address);
  922. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  923. struct elf_resolve *tpnt;
  924. tpnt = rpnt;
  925. _dl_if_debug_print("Module \"%s\" at %p\n",
  926. tpnt->libname, DL_LOADADDR_BASE(tpnt->loadaddr));
  927. if (DL_ADDR_IN_LOADADDR((ElfW(Addr)) __address, tpnt, pelf))
  928. pelf = tpnt;
  929. }
  930. if (!pelf) {
  931. return 0;
  932. }
  933. /*
  934. * Try and locate the symbol of address
  935. */
  936. {
  937. char *strtab;
  938. ElfW(Sym) *symtab;
  939. unsigned int hn, si, sn, sf;
  940. ElfW(Addr) sa = 0;
  941. /* Set the info for the object the address lies in */
  942. __info->dli_fname = pelf->libname;
  943. __info->dli_fbase = (void *)pelf->mapaddr;
  944. symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
  945. strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
  946. sf = sn = 0;
  947. #ifdef __LDSO_GNU_HASH_SUPPORT__
  948. if (pelf->l_gnu_bitmask) {
  949. for (hn = 0; hn < pelf->nbucket; hn++) {
  950. si = pelf->l_gnu_buckets[hn];
  951. if (!si)
  952. continue;
  953. const Elf32_Word *hasharr = &pelf->l_gnu_chain_zero[si];
  954. do {
  955. ElfW(Addr) symbol_addr;
  956. symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
  957. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  958. sa = symbol_addr;
  959. sn = si;
  960. sf = 1;
  961. }
  962. _dl_if_debug_print("Symbol \"%s\" at %p\n", strtab + symtab[si].st_name, symbol_addr);
  963. ++si;
  964. } while ((*hasharr++ & 1u) == 0);
  965. }
  966. } else
  967. #endif
  968. for (hn = 0; hn < pelf->nbucket; hn++) {
  969. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  970. ElfW(Addr) symbol_addr;
  971. symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
  972. if (symbol_addr <= (ElfW(Addr))__address && (!sf || sa < symbol_addr)) {
  973. sa = symbol_addr;
  974. sn = si;
  975. sf = 1;
  976. }
  977. _dl_if_debug_print("Symbol \"%s\" at %p\n",
  978. strtab + symtab[si].st_name, symbol_addr);
  979. }
  980. }
  981. if (sf) {
  982. /* A nearest symbol has been found; fill the entries */
  983. __info->dli_sname = strtab + symtab[sn].st_name;
  984. __info->dli_saddr = (void *)sa;
  985. } else {
  986. /* No symbol found, fill entries with NULL value,
  987. only the containing object will be returned. */
  988. __info->dli_sname = NULL;
  989. __info->dli_saddr = NULL;
  990. }
  991. return 1;
  992. }
  993. }
  994. #endif