libdl.c 31 KB

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