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_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) {
  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. if (--tpnt->usage_count == 0) {
  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