libdl.c 33 KB

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