libdl.c 31 KB

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