libdl.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214
  1. /*
  2. * Program to load an ELF binary on a linux system, and run it
  3. * after resolving ELF shared library symbols
  4. *
  5. * Copyright (C) 2000-2006 by Erik Andersen <andersen@uclibc.org>
  6. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  7. * David Engel, Hongjiu Lu and Mitch D'Souza
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. The name of the above contributors may not be
  15. * used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  22. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  23. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  24. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  25. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  26. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  27. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. */
  30. /* When libdl is linked in statically into libc.a, we need to replace
  31. * these symbols that otherwise would have been loaded in from ldso.
  32. * This must be before including ldso.h */
  33. #ifndef SHARED
  34. #define _dl_malloc malloc
  35. #define _dl_free free
  36. #endif
  37. #include <ldso.h>
  38. #include <stdio.h>
  39. #include <string.h>
  40. #include <stdbool.h>
  41. #include <bits/uClibc_mutex.h>
  42. #ifdef __UCLIBC_HAS_TLS__
  43. #include <tls.h>
  44. #endif
  45. #if defined(USE_TLS) && USE_TLS
  46. #include <ldsodefs.h>
  47. #include <dl-tls.h>
  48. extern void _dl_add_to_slotinfo(struct link_map *l);
  49. #endif
  50. /* TODO: get rid of global lock and use more finegrained locking, or
  51. * perhaps RCU for the global structures */
  52. __UCLIBC_MUTEX_STATIC(_dl_mutex, PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP);
  53. #ifdef SHARED
  54. # if defined(USE_TLS) && USE_TLS
  55. extern struct link_map *_dl_update_slotinfo(unsigned long int req_modid);
  56. # endif
  57. /* When libdl is loaded as a shared library, we need to load in
  58. * and use a pile of symbols from ldso... */
  59. #include <dl-elf.h>
  60. extern int _dl_errno;
  61. extern struct dyn_elf *_dl_symbol_tables;
  62. extern struct dyn_elf *_dl_handles;
  63. extern struct elf_resolve *_dl_loaded_modules;
  64. extern void _dl_free (void *__ptr);
  65. extern struct r_debug *_dl_debug_addr;
  66. extern unsigned long _dl_error_number;
  67. extern void *(*_dl_malloc_function)(size_t);
  68. extern void (*_dl_free_function) (void *p);
  69. extern void _dl_run_init_array(struct elf_resolve *);
  70. extern void _dl_run_fini_array(struct elf_resolve *);
  71. #ifdef __LDSO_CACHE_SUPPORT__
  72. int _dl_map_cache(void);
  73. int _dl_unmap_cache(void);
  74. #endif
  75. #ifdef __mips__
  76. extern void _dl_perform_mips_global_got_relocations(struct elf_resolve *tpnt, int lazy);
  77. #endif
  78. #ifdef __SUPPORT_LD_DEBUG__
  79. extern char *_dl_debug;
  80. #endif
  81. #else /* !SHARED */
  82. /* When libdl is linked as a static library, we need to replace all
  83. * the symbols that otherwise would have been loaded in from ldso... */
  84. #ifdef __SUPPORT_LD_DEBUG__
  85. char *_dl_debug = NULL;
  86. char *_dl_debug_symbols = NULL;
  87. char *_dl_debug_move = NULL;
  88. char *_dl_debug_reloc = NULL;
  89. char *_dl_debug_detail = NULL;
  90. char *_dl_debug_nofixups = NULL;
  91. char *_dl_debug_bindings = NULL;
  92. int _dl_debug_file = 2;
  93. #endif
  94. const char *_dl_progname = ""; /* Program name */
  95. void *(*_dl_malloc_function)(size_t);
  96. void (*_dl_free_function) (void *p);
  97. #ifdef __LDSO_LD_LIBRARY_PATH__
  98. char *_dl_library_path = NULL; /* Where we look for libraries */
  99. #endif
  100. int _dl_errno = 0; /* We can't use the real errno in ldso */
  101. size_t _dl_pagesize = PAGE_SIZE; /* Store the page size for use later */
  102. /* This global variable is also to communicate with debuggers such as gdb. */
  103. struct r_debug *_dl_debug_addr = NULL;
  104. #include "../ldso/dl-array.c"
  105. #include "../ldso/dl-debug.c"
  106. # if defined(USE_TLS) && USE_TLS
  107. /*
  108. * Giving this initialized value preallocates some surplus bytes in the
  109. * static TLS area, see __libc_setup_tls (libc-tls.c).
  110. */
  111. size_t _dl_tls_static_size = 2048;
  112. # endif
  113. #include LDSO_ELFINTERP
  114. #include "../ldso/dl-hash.c"
  115. #define _dl_trace_loaded_objects 0
  116. #include "../ldso/dl-elf.c"
  117. #endif /* SHARED */
  118. #ifdef __SUPPORT_LD_DEBUG__
  119. # define _dl_if_debug_print(fmt, args...) \
  120. do { \
  121. if (_dl_debug) \
  122. fprintf(stderr, "%s():%i: " fmt, __func__, __LINE__, ## args); \
  123. } while (0)
  124. #else
  125. # define _dl_if_debug_print(fmt, args...)
  126. #endif
  127. static int do_dlclose(void *, int need_fini);
  128. static const char *const dl_error_names[] = {
  129. "",
  130. "File not found",
  131. "Unable to open /dev/zero",
  132. "Not an ELF file",
  133. #if defined (__i386__)
  134. "Not i386 binary",
  135. #elif defined (__sparc__)
  136. "Not sparc binary",
  137. #elif defined (__mc68000__)
  138. "Not m68k binary",
  139. #else
  140. "Unrecognized binary type",
  141. #endif
  142. "Not an ELF shared library",
  143. "Unable to mmap file",
  144. "No dynamic section",
  145. "Library contains unsupported TLS",
  146. #ifdef ELF_USES_RELOCA
  147. "Unable to process REL relocs",
  148. #else
  149. "Unable to process RELA relocs",
  150. #endif
  151. "Bad handle",
  152. "Unable to resolve symbol"
  153. };
  154. #if defined(USE_TLS) && USE_TLS
  155. #ifdef SHARED
  156. /*
  157. * Systems which do not have tls_index also probably have to define
  158. * DONT_USE_TLS_INDEX.
  159. */
  160. # ifndef __TLS_GET_ADDR
  161. # define __TLS_GET_ADDR __tls_get_addr
  162. # endif
  163. /*
  164. * Return the symbol address given the map of the module it is in and
  165. * the symbol record. This is used in dl-sym.c.
  166. */
  167. static void *
  168. internal_function
  169. _dl_tls_symaddr(struct link_map *map, const ElfW(Addr) st_value)
  170. {
  171. # ifndef DONT_USE_TLS_INDEX
  172. tls_index tmp =
  173. {
  174. .ti_module = map->l_tls_modid,
  175. .ti_offset = st_value
  176. };
  177. return __TLS_GET_ADDR (&tmp);
  178. # else
  179. return __TLS_GET_ADDR (map->l_tls_modid, st_value);
  180. # endif
  181. }
  182. #endif
  183. /* Returns true when a non-empty entry was found. */
  184. static bool
  185. remove_slotinfo(size_t idx, struct dtv_slotinfo_list *listp, size_t disp,
  186. bool should_be_there)
  187. {
  188. if (idx - disp >= listp->len) {
  189. if (listp->next == NULL) {
  190. /*
  191. * The index is not actually valid in the slotinfo list,
  192. * because this object was closed before it was fully set
  193. * up due to some error.
  194. */
  195. _dl_assert(!should_be_there);
  196. } else {
  197. if (remove_slotinfo(idx, listp->next, disp + listp->len,
  198. should_be_there))
  199. return true;
  200. /*
  201. * No non-empty entry. Search from the end of this element's
  202. * slotinfo array.
  203. */
  204. idx = disp + listp->len;
  205. }
  206. } else {
  207. struct link_map *old_map = listp->slotinfo[idx - disp].map;
  208. /*
  209. * The entry might still be in its unused state if we are
  210. * closing an object that wasn't fully set up.
  211. */
  212. if (__builtin_expect(old_map != NULL, 1)) {
  213. _dl_assert(old_map->l_tls_modid == idx);
  214. /* Mark the entry as unused. */
  215. listp->slotinfo[idx - disp].gen = _dl_tls_generation + 1;
  216. listp->slotinfo[idx - disp].map = NULL;
  217. }
  218. /*
  219. * If this is not the last currently used entry no need to
  220. * look further.
  221. */
  222. if (idx != _dl_tls_max_dtv_idx)
  223. return true;
  224. }
  225. while (idx - disp > (disp == 0 ? 1 + _dl_tls_static_nelem : 0)) {
  226. --idx;
  227. if (listp->slotinfo[idx - disp].map != NULL) {
  228. /* Found a new last used index. */
  229. _dl_tls_max_dtv_idx = idx;
  230. return true;
  231. }
  232. }
  233. /* No non-entry in this list element. */
  234. return false;
  235. }
  236. #endif
  237. static ptrdiff_t _dl_build_local_scope (struct elf_resolve **list,
  238. struct elf_resolve *map)
  239. {
  240. struct elf_resolve **p = list;
  241. struct init_fini_list *q;
  242. *p++ = map;
  243. map->init_flag |= DL_RESERVED;
  244. if (map->init_fini)
  245. for (q = map->init_fini; q; q = q->next)
  246. if (! (q->tpnt->init_flag & DL_RESERVED))
  247. p += _dl_build_local_scope (p, q->tpnt);
  248. return p - list;
  249. }
  250. static void *do_dlopen(const char *libname, int flag, ElfW(Addr) from)
  251. {
  252. struct elf_resolve *tpnt, *tfrom;
  253. struct dyn_elf *dyn_chain, *rpnt = NULL, *dyn_ptr, *relro_ptr, *handle;
  254. struct elf_resolve *tpnt1;
  255. void (*dl_brk) (void);
  256. int now_flag;
  257. struct init_fini_list *tmp, *runp, *runp2, *dep_list;
  258. unsigned int nlist, i;
  259. struct elf_resolve **init_fini_list;
  260. static bool _dl_init;
  261. struct elf_resolve **local_scope;
  262. #ifdef SHARED
  263. struct r_scope_elem *ls;
  264. #endif
  265. #if defined(USE_TLS) && USE_TLS
  266. bool any_tls = false;
  267. #endif
  268. /* A bit of sanity checking... */
  269. if (!(flag & (RTLD_LAZY|RTLD_NOW|RTLD_NOLOAD))) {
  270. _dl_error_number = LD_BAD_HANDLE;
  271. return NULL;
  272. }
  273. if (!_dl_init) {
  274. _dl_init = true;
  275. _dl_malloc_function = malloc;
  276. _dl_free_function = free;
  277. }
  278. /* Cover the trivial case first */
  279. if (!libname)
  280. return _dl_symbol_tables;
  281. #ifndef SHARED
  282. # ifdef __SUPPORT_LD_DEBUG__
  283. _dl_debug = getenv("LD_DEBUG");
  284. if (_dl_debug) {
  285. if (strstr(_dl_debug, "all")) {
  286. _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
  287. = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
  288. } else {
  289. _dl_debug_detail = strstr(_dl_debug, "detail");
  290. _dl_debug_move = strstr(_dl_debug, "move");
  291. _dl_debug_symbols = strstr(_dl_debug, "sym");
  292. _dl_debug_reloc = strstr(_dl_debug, "reloc");
  293. _dl_debug_nofixups = strstr(_dl_debug, "nofix");
  294. _dl_debug_bindings = strstr(_dl_debug, "bind");
  295. }
  296. }
  297. # endif
  298. #endif
  299. _dl_map_cache();
  300. /*
  301. * Try and locate the module we were called from - we
  302. * need this so that we get the correct RPATH/RUNPATH. Note that
  303. * this is the current behavior under Solaris, but the
  304. * ABI+ specifies that we should only use the RPATH from
  305. * the application. Thus this may go away at some time
  306. * in the future.
  307. */
  308. {
  309. struct dyn_elf *dpnt;
  310. tfrom = NULL;
  311. for (dpnt = _dl_symbol_tables; dpnt; dpnt = dpnt->next) {
  312. tpnt = dpnt->dyn;
  313. if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom))
  314. tfrom = tpnt;
  315. }
  316. }
  317. for (rpnt = _dl_symbol_tables; rpnt && rpnt->next; rpnt = rpnt->next)
  318. continue;
  319. relro_ptr = rpnt;
  320. now_flag = (flag & RTLD_NOW) ? RTLD_NOW : 0;
  321. if (getenv("LD_BIND_NOW"))
  322. now_flag = RTLD_NOW;
  323. #if !defined SHARED && defined __LDSO_LD_LIBRARY_PATH__
  324. /* When statically linked, the _dl_library_path is not yet initialized */
  325. _dl_library_path = getenv("LD_LIBRARY_PATH");
  326. #endif
  327. /* Try to load the specified library */
  328. _dl_if_debug_print("Trying to dlopen '%s', RTLD_GLOBAL:%d RTLD_NOW:%d\n",
  329. (char*)libname, (flag & RTLD_GLOBAL ? 1:0), (now_flag & RTLD_NOW ? 1:0));
  330. tpnt = _dl_load_shared_library(flag & (RTLD_NOLOAD | RTLD_GLOBAL | RTLD_NODELETE),
  331. &rpnt, tfrom, (char*)libname, 0);
  332. if (tpnt == NULL) {
  333. _dl_unmap_cache();
  334. return NULL;
  335. }
  336. dyn_chain = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  337. memset(dyn_chain, 0, sizeof(struct dyn_elf));
  338. dyn_chain->dyn = tpnt;
  339. dyn_chain->next_handle = _dl_handles;
  340. _dl_handles = dyn_ptr = dyn_chain;
  341. if (tpnt->init_flag & DL_OPENED2) {
  342. _dl_if_debug_print("Lib: %s already opened\n", libname);
  343. /* see if there is a handle from a earlier dlopen */
  344. for (handle = _dl_handles->next_handle; handle; handle = handle->next_handle) {
  345. if (handle->dyn == tpnt) {
  346. dyn_chain->init_fini.init_fini = handle->init_fini.init_fini;
  347. dyn_chain->init_fini.nlist = handle->init_fini.nlist;
  348. for (i = 0; i < dyn_chain->init_fini.nlist; i++)
  349. dyn_chain->init_fini.init_fini[i]->rtld_flags |= (flag & (RTLD_GLOBAL|RTLD_NODELETE));
  350. dyn_chain->next = handle->next;
  351. break;
  352. }
  353. }
  354. return dyn_chain;
  355. }
  356. _dl_if_debug_print("Looking for needed libraries\n");
  357. nlist = 0;
  358. runp = alloca(sizeof(*runp));
  359. runp->tpnt = tpnt;
  360. runp->next = NULL;
  361. dep_list = runp2 = runp;
  362. for (; runp; runp = runp->next) {
  363. ElfW(Dyn) *dpnt;
  364. char *lpntstr;
  365. nlist++;
  366. runp->tpnt->init_fini = NULL; /* clear any previous dependcies */
  367. for (dpnt = (ElfW(Dyn) *) runp->tpnt->dynamic_addr; dpnt->d_tag; dpnt++) {
  368. if (dpnt->d_tag == DT_NEEDED) {
  369. lpntstr = (char*) (runp->tpnt->dynamic_info[DT_STRTAB] +
  370. dpnt->d_un.d_val);
  371. _dl_if_debug_print("Trying to load '%s', needed by '%s'\n",
  372. lpntstr, runp->tpnt->libname);
  373. tpnt1 = _dl_load_shared_library(flag & (RTLD_GLOBAL | RTLD_NODELETE),
  374. &rpnt, runp->tpnt, lpntstr, 0);
  375. if (!tpnt1)
  376. goto oops;
  377. /* This list is for dlsym() and relocation */
  378. dyn_ptr->next = (struct dyn_elf *) malloc(sizeof(struct dyn_elf));
  379. memset (dyn_ptr->next, 0, sizeof (struct dyn_elf));
  380. dyn_ptr = dyn_ptr->next;
  381. dyn_ptr->dyn = tpnt1;
  382. /* Used to record RTLD_LOCAL scope */
  383. tmp = alloca(sizeof(struct init_fini_list));
  384. tmp->tpnt = tpnt1;
  385. tmp->next = runp->tpnt->init_fini;
  386. runp->tpnt->init_fini = tmp;
  387. for (tmp=dep_list; tmp; tmp = tmp->next) {
  388. if (tpnt1 == tmp->tpnt) { /* if match => cirular dependency, drop it */
  389. _dl_if_debug_print("Circular dependency, skipping '%s',\n",
  390. tmp->tpnt->libname);
  391. tpnt1->usage_count--;
  392. break;
  393. }
  394. }
  395. if (!tmp) { /* Don't add if circular dependency detected */
  396. runp2->next = alloca(sizeof(*runp));
  397. runp2 = runp2->next;
  398. runp2->tpnt = tpnt1;
  399. runp2->next = NULL;
  400. }
  401. }
  402. }
  403. }
  404. init_fini_list = malloc(nlist * sizeof(struct elf_resolve *));
  405. dyn_chain->init_fini.init_fini = init_fini_list;
  406. dyn_chain->init_fini.nlist = nlist;
  407. i = 0;
  408. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  409. init_fini_list[i++] = runp2->tpnt;
  410. for (runp = runp2->tpnt->init_fini; runp; runp = runp->next) {
  411. if (!(runp->tpnt->rtld_flags & RTLD_GLOBAL)) {
  412. tmp = malloc(sizeof(struct init_fini_list));
  413. tmp->tpnt = runp->tpnt;
  414. tmp->next = runp2->tpnt->rtld_local;
  415. runp2->tpnt->rtld_local = tmp;
  416. }
  417. }
  418. }
  419. /* Build the local scope for the dynamically loaded modules. */
  420. local_scope = _dl_malloc(nlist * sizeof(struct elf_resolve *)); /* Could it allocated on stack? */
  421. for (i = 0; i < nlist; i++)
  422. if (init_fini_list[i]->symbol_scope.r_nlist == 0) {
  423. int k, cnt;
  424. cnt = _dl_build_local_scope(local_scope, init_fini_list[i]);
  425. init_fini_list[i]->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
  426. init_fini_list[i]->symbol_scope.r_nlist = cnt;
  427. _dl_memcpy (init_fini_list[i]->symbol_scope.r_list, local_scope,
  428. cnt * sizeof (struct elf_resolve *));
  429. /* Restoring the init_flag.*/
  430. for (k = 0; k < nlist; k++)
  431. init_fini_list[k]->init_flag &= ~DL_RESERVED;
  432. }
  433. _dl_free(local_scope);
  434. /* Sort the INIT/FINI list in dependency order. */
  435. for (runp2 = dep_list; runp2; runp2 = runp2->next) {
  436. unsigned int j, k;
  437. for (j = 0; init_fini_list[j] != runp2->tpnt; ++j)
  438. /* Empty */;
  439. for (k = j + 1; k < nlist; ++k) {
  440. struct init_fini_list *ele = init_fini_list[k]->init_fini;
  441. for (; ele; ele = ele->next) {
  442. if (ele->tpnt == runp2->tpnt) {
  443. struct elf_resolve *here = init_fini_list[k];
  444. _dl_if_debug_print("Move %s from pos %d to %d in INIT/FINI list.\n", here->libname, k, j);
  445. for (i = (k - j); i; --i)
  446. init_fini_list[i+j] = init_fini_list[i+j-1];
  447. init_fini_list[j] = here;
  448. ++j;
  449. break;
  450. }
  451. }
  452. }
  453. }
  454. #ifdef __SUPPORT_LD_DEBUG__
  455. if (_dl_debug) {
  456. fprintf(stderr, "\nINIT/FINI order and dependencies:\n");
  457. for (i = 0; i < nlist; i++) {
  458. fprintf(stderr, "lib: %s has deps:\n", init_fini_list[i]->libname);
  459. runp = init_fini_list[i]->init_fini;
  460. for (; runp; runp = runp->next)
  461. fprintf(stderr, " %s ", runp->tpnt->libname);
  462. fprintf(stderr, "\n");
  463. }
  464. }
  465. #endif
  466. _dl_if_debug_print("Beginning dlopen relocation fixups\n");
  467. /*
  468. * OK, now all of the kids are tucked into bed in their proper addresses.
  469. * Now we go through and look for REL and RELA records that indicate fixups
  470. * to the GOT tables. We need to do this in reverse order so that COPY
  471. * directives work correctly */
  472. #ifdef SHARED
  473. /*
  474. * Get the tail of the list.
  475. * In the static case doesn't need to extend the global scope, it is
  476. * ready to be used as it is, because _dl_loaded_modules already points
  477. * to the dlopened library.
  478. */
  479. for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next);
  480. /* Extend the global scope by adding the local scope of the dlopened DSO. */
  481. ls->next = &dyn_chain->dyn->symbol_scope;
  482. #endif
  483. #ifdef __mips__
  484. /*
  485. * Relocation of the GOT entries for MIPS have to be done
  486. * after all the libraries have been loaded.
  487. */
  488. _dl_perform_mips_global_got_relocations(tpnt, !now_flag);
  489. #endif
  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 old-style 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. if (tpnt->dynamic_info[DT_INIT_ARRAY]) {
  560. void (*dl_elf_func) (void);
  561. dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT_ARRAY]);
  562. if (dl_elf_func) {
  563. _dl_if_debug_print("running ctors for library %s at '%p'\n",
  564. tpnt->libname, dl_elf_func);
  565. _dl_run_init_array(tpnt);
  566. }
  567. }
  568. }
  569. _dl_unmap_cache();
  570. return (void *) dyn_chain;
  571. oops:
  572. /* Something went wrong. Clean up and return NULL. */
  573. _dl_unmap_cache();
  574. do_dlclose(dyn_chain, 0);
  575. return NULL;
  576. }
  577. void *dlopen(const char *libname, int flag)
  578. {
  579. void *ret;
  580. __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
  581. ret = do_dlopen(libname, flag,
  582. (ElfW(Addr)) __builtin_return_address(0));
  583. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
  584. return ret;
  585. }
  586. static void *do_dlsym(void *vhandle, const char *name, void *caller_address)
  587. {
  588. struct elf_resolve *tpnt, *tfrom;
  589. struct dyn_elf *handle;
  590. ElfW(Addr) from = 0;
  591. struct dyn_elf *rpnt;
  592. void *ret;
  593. struct symbol_ref sym_ref = { NULL, NULL };
  594. /* Nastiness to support underscore prefixes. */
  595. #ifdef __UCLIBC_UNDERSCORES__
  596. char tmp_buf[80];
  597. char *name2 = tmp_buf;
  598. size_t nlen = strlen (name) + 1;
  599. if (nlen + 1 > sizeof (tmp_buf))
  600. name2 = malloc (nlen + 1);
  601. if (name2 == 0) {
  602. _dl_error_number = LD_ERROR_MMAP_FAILED;
  603. return 0;
  604. }
  605. name2[0] = '_';
  606. memcpy (name2 + 1, name, nlen);
  607. #else
  608. const char *name2 = name;
  609. #endif
  610. handle = (struct dyn_elf *) vhandle;
  611. /* First of all verify that we have a real handle
  612. of some kind. Return NULL if not a valid handle. */
  613. if (handle == NULL)
  614. handle = _dl_symbol_tables;
  615. else if (handle != RTLD_NEXT && handle != _dl_symbol_tables) {
  616. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle)
  617. if (rpnt == handle)
  618. break;
  619. if (!rpnt) {
  620. _dl_error_number = LD_BAD_HANDLE;
  621. ret = NULL;
  622. goto out;
  623. }
  624. } else if (handle == RTLD_NEXT) {
  625. /*
  626. * Try and locate the module we were called from - we
  627. * need this so that we know where to start searching
  628. * from. We never pass RTLD_NEXT down into the actual
  629. * dynamic loader itself, as it doesn't know
  630. * how to properly treat it.
  631. */
  632. from = (ElfW(Addr)) caller_address;
  633. tfrom = NULL;
  634. for (rpnt = _dl_symbol_tables; rpnt; rpnt = rpnt->next) {
  635. tpnt = rpnt->dyn;
  636. if (DL_ADDR_IN_LOADADDR(from, tpnt, tfrom)) {
  637. tfrom = tpnt;
  638. handle = rpnt->next;
  639. }
  640. }
  641. }
  642. tpnt = NULL;
  643. if (handle == _dl_symbol_tables)
  644. tpnt = handle->dyn; /* Only search RTLD_GLOBAL objs if global object */
  645. do {
  646. ret = _dl_find_hash(name2, &handle->dyn->symbol_scope, tpnt, ELF_RTYPE_CLASS_DLSYM, &sym_ref);
  647. if (ret != NULL)
  648. break;
  649. handle = handle->next;
  650. } while (from && handle);
  651. #if defined(USE_TLS) && USE_TLS && defined SHARED
  652. if (sym_ref.sym && (ELF_ST_TYPE(sym_ref.sym->st_info) == STT_TLS) && (sym_ref.tpnt)) {
  653. /* The found symbol is a thread-local storage variable.
  654. Return its address for the current thread. */
  655. ret = _dl_tls_symaddr ((struct link_map *)sym_ref.tpnt, (ElfW(Addr))ret);
  656. }
  657. #endif
  658. /*
  659. * Nothing found.
  660. */
  661. if (!ret)
  662. _dl_error_number = LD_NO_SYMBOL;
  663. out:
  664. #ifdef __UCLIBC_UNDERSCORES__
  665. if (name2 != tmp_buf)
  666. free (name2);
  667. #endif
  668. return ret;
  669. }
  670. void *dlsym(void *vhandle, const char *name)
  671. {
  672. void *ret;
  673. __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
  674. ret = do_dlsym(vhandle, name, __builtin_return_address(0));
  675. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
  676. return ret;
  677. }
  678. static int do_dlclose(void *vhandle, int need_fini)
  679. {
  680. struct dyn_elf *rpnt, *rpnt1, *rpnt1_tmp;
  681. struct init_fini_list *runp, *tmp;
  682. ElfW(Phdr) *ppnt;
  683. struct elf_resolve *tpnt, *run_tpnt;
  684. int (*dl_elf_fini) (void);
  685. void (*dl_brk) (void);
  686. struct dyn_elf *handle;
  687. ElfW(Addr) end = 0, start = (ElfW(Addr))(~0ULL);
  688. unsigned int i, j;
  689. struct r_scope_elem *ls, *ls_next = NULL;
  690. struct elf_resolve **handle_rlist;
  691. #if defined(USE_TLS) && USE_TLS
  692. bool any_tls = false;
  693. size_t tls_free_start = NO_TLS_OFFSET;
  694. size_t tls_free_end = NO_TLS_OFFSET;
  695. struct link_map *tls_lmap;
  696. #endif
  697. handle = (struct dyn_elf *) vhandle;
  698. if (handle == _dl_symbol_tables)
  699. return 0;
  700. rpnt1 = NULL;
  701. for (rpnt = _dl_handles; rpnt; rpnt = rpnt->next_handle) {
  702. if (rpnt == handle)
  703. break;
  704. rpnt1 = rpnt;
  705. }
  706. if (!rpnt) {
  707. _dl_error_number = LD_BAD_HANDLE;
  708. return 1;
  709. }
  710. if (rpnt1)
  711. rpnt1->next_handle = rpnt->next_handle;
  712. else
  713. _dl_handles = rpnt->next_handle;
  714. _dl_if_debug_print("%s: usage count: %d\n",
  715. handle->dyn->libname, handle->dyn->usage_count);
  716. if (handle->dyn->usage_count != 1) {
  717. handle->dyn->usage_count--;
  718. free(handle);
  719. return 0;
  720. }
  721. /* Store the handle's local scope array for later removal */
  722. handle_rlist = handle->dyn->symbol_scope.r_list;
  723. /* Store references to the local scope entries for later removal */
  724. for (ls = &_dl_loaded_modules->symbol_scope; ls && ls->next; ls = ls->next)
  725. if (ls->next->r_list[0] == handle->dyn) {
  726. break;
  727. }
  728. /* ls points to the previous local symbol scope */
  729. if(ls && ls->next)
  730. ls_next = ls->next->next;
  731. /* OK, this is a valid handle - now close out the file */
  732. for (j = 0; j < handle->init_fini.nlist; ++j) {
  733. tpnt = handle->init_fini.init_fini[j];
  734. tpnt->usage_count--;
  735. if (tpnt->usage_count == 0) {
  736. if ((tpnt->dynamic_info[DT_FINI]
  737. || tpnt->dynamic_info[DT_FINI_ARRAY])
  738. && need_fini
  739. && !(tpnt->init_flag & FINI_FUNCS_CALLED)
  740. ) {
  741. tpnt->init_flag |= FINI_FUNCS_CALLED;
  742. if (tpnt->dynamic_info[DT_FINI_ARRAY]) {
  743. dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI_ARRAY]);
  744. _dl_if_debug_print("running dtors for library %s at '%p'\n",
  745. tpnt->libname, dl_elf_fini);
  746. _dl_run_fini_array(tpnt);
  747. }
  748. if (tpnt->dynamic_info[DT_FINI]) {
  749. dl_elf_fini = (int (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
  750. _dl_if_debug_print("running old-style dtors for library %s at '%p'\n",
  751. tpnt->libname, dl_elf_fini);
  752. DL_CALL_FUNC_AT_ADDR (dl_elf_fini, tpnt->loadaddr, (int (*)(void)));
  753. }
  754. }
  755. }
  756. }
  757. for (j = 0; j < handle->init_fini.nlist; ++j) {
  758. tpnt = handle->init_fini.init_fini[j];
  759. if (tpnt->usage_count == 0) {
  760. _dl_if_debug_print("unmapping: %s\n", tpnt->libname);
  761. end = 0;
  762. for (i = 0, ppnt = tpnt->ppnt;
  763. i < tpnt->n_phent; ppnt++, i++) {
  764. if (ppnt->p_type != PT_LOAD)
  765. continue;
  766. if (ppnt->p_vaddr < start)
  767. start = ppnt->p_vaddr;
  768. if (end < ppnt->p_vaddr + ppnt->p_memsz)
  769. end = ppnt->p_vaddr + ppnt->p_memsz;
  770. }
  771. #if defined(USE_TLS) && USE_TLS
  772. /* Do the cast to make things easy. */
  773. tls_lmap = (struct link_map *) tpnt;
  774. /* Remove the object from the dtv slotinfo array if it uses TLS. */
  775. if (__builtin_expect (tls_lmap->l_tls_blocksize > 0, 0)) {
  776. any_tls = true;
  777. if (_dl_tls_dtv_slotinfo_list != NULL
  778. && ! remove_slotinfo (tls_lmap->l_tls_modid,
  779. _dl_tls_dtv_slotinfo_list, 0,
  780. (tpnt->init_flag & INIT_FUNCS_CALLED)))
  781. /* All dynamically loaded modules with TLS are unloaded. */
  782. _dl_tls_max_dtv_idx = _dl_tls_static_nelem;
  783. if (tls_lmap->l_tls_offset != NO_TLS_OFFSET) {
  784. /*
  785. * Collect a contiguous chunk built from the objects in
  786. * this search list, going in either direction. When the
  787. * whole chunk is at the end of the used area then we can
  788. * reclaim it.
  789. */
  790. # if defined(TLS_TCB_AT_TP)
  791. if (tls_free_start == NO_TLS_OFFSET
  792. || (size_t) tls_lmap->l_tls_offset == tls_free_start) {
  793. /* Extend the contiguous chunk being reclaimed. */
  794. tls_free_start
  795. = tls_lmap->l_tls_offset -
  796. tls_lmap->l_tls_blocksize;
  797. if (tls_free_end == NO_TLS_OFFSET)
  798. tls_free_end = tls_lmap->l_tls_offset;
  799. } else if (tls_lmap->l_tls_offset - tls_lmap->l_tls_blocksize
  800. == tls_free_end)
  801. /* Extend the chunk backwards. */
  802. tls_free_end = tls_lmap->l_tls_offset;
  803. else {
  804. /*
  805. * This isn't contiguous with the last chunk freed.
  806. * One of them will be leaked unless we can free
  807. * one block right away.
  808. */
  809. if (tls_free_end == _dl_tls_static_used) {
  810. _dl_tls_static_used = tls_free_start;
  811. tls_free_end = tls_lmap->l_tls_offset;
  812. tls_free_start
  813. = tls_free_end - tls_lmap->l_tls_blocksize;
  814. } else if ((size_t) tls_lmap->l_tls_offset
  815. == _dl_tls_static_used)
  816. _dl_tls_static_used = tls_lmap->l_tls_offset -
  817. tls_lmap->l_tls_blocksize;
  818. else if (tls_free_end < (size_t) tls_lmap->l_tls_offset) {
  819. /*
  820. * We pick the later block. It has a chance
  821. * to be freed.
  822. */
  823. tls_free_end = tls_lmap->l_tls_offset;
  824. tls_free_start = tls_free_end -
  825. tls_lmap->l_tls_blocksize;
  826. }
  827. }
  828. # elif defined(TLS_DTV_AT_TP)
  829. if ((size_t) tls_lmap->l_tls_offset == tls_free_end)
  830. /* Extend the contiguous chunk being reclaimed. */
  831. tls_free_end -= tls_lmap->l_tls_blocksize;
  832. else if (tls_lmap->l_tls_offset + tls_lmap->l_tls_blocksize
  833. == tls_free_start)
  834. /* Extend the chunk backwards. */
  835. tls_free_start = tls_lmap->l_tls_offset;
  836. else {
  837. /*
  838. * This isn't contiguous with the last chunk
  839. * freed. One of them will be leaked.
  840. */
  841. if (tls_free_end == _dl_tls_static_used)
  842. _dl_tls_static_used = tls_free_start;
  843. tls_free_start = tls_lmap->l_tls_offset;
  844. tls_free_end = tls_free_start +
  845. tls_lmap->l_tls_blocksize;
  846. }
  847. # else
  848. # error Either TLS_TCB_AT_TP or TLS_DTV_AT_TP must be defined
  849. # endif
  850. } else {
  851. #define TLS_DTV_UNALLOCATED ((void *) -1l)
  852. dtv_t *dtv = THREAD_DTV ();
  853. if (!(dtv[tls_lmap->l_tls_modid].pointer.is_static) &&
  854. dtv[tls_lmap->l_tls_modid].pointer.val != TLS_DTV_UNALLOCATED) {
  855. /* Note that free is called for NULL is well. We
  856. deallocate even if it is this dtv entry we are
  857. supposed to load. The reason is that we call
  858. memalign and not malloc. */
  859. _dl_free (dtv[tls_lmap->l_tls_modid].pointer.val);
  860. dtv[tls_lmap->l_tls_modid].pointer.val = TLS_DTV_UNALLOCATED;
  861. }
  862. }
  863. }
  864. #endif
  865. end = (end + ADDR_ALIGN) & PAGE_ALIGN;
  866. start = start & ~ADDR_ALIGN;
  867. if (end > start) {
  868. _dl_if_debug_print("unmapping: %s at %p with length: '%p' until %p\n", tpnt->libname, (void *)tpnt->mapaddr, (void *)(end - start), (void *)(tpnt->mapaddr + (end - start)));
  869. DL_LIB_UNMAP (tpnt, end - start);
  870. }
  871. else {
  872. _dl_if_debug_print("NOT unmapping: %s at %p. start<end ('%p'<'%p')", tpnt->libname, (void *)tpnt->mapaddr, (void *)start, (void *)end);
  873. }
  874. /* Free elements in RTLD_LOCAL scope list */
  875. for (runp = tpnt->rtld_local; runp; runp = tmp) {
  876. tmp = runp->next;
  877. free(runp);
  878. }
  879. /* Next, remove tpnt from the loaded_module list */
  880. if (_dl_loaded_modules == tpnt) {
  881. _dl_loaded_modules = tpnt->next;
  882. if (_dl_loaded_modules)
  883. _dl_loaded_modules->prev = 0;
  884. } else {
  885. for (run_tpnt = _dl_loaded_modules; run_tpnt; run_tpnt = run_tpnt->next) {
  886. if (run_tpnt->next == tpnt) {
  887. _dl_if_debug_print("removing loaded_modules: %s\n", tpnt->libname);
  888. run_tpnt->next = run_tpnt->next->next;
  889. if (run_tpnt->next)
  890. run_tpnt->next->prev = run_tpnt;
  891. break;
  892. }
  893. }
  894. }
  895. /* Next, remove tpnt from the global symbol table list */
  896. if (_dl_symbol_tables) {
  897. if (_dl_symbol_tables->dyn == tpnt) {
  898. _dl_symbol_tables = _dl_symbol_tables->next;
  899. if (_dl_symbol_tables)
  900. _dl_symbol_tables->prev = 0;
  901. } else {
  902. for (rpnt1 = _dl_symbol_tables; rpnt1->next; rpnt1 = rpnt1->next) {
  903. if (rpnt1->next->dyn == tpnt) {
  904. _dl_if_debug_print("removing symbol_tables: %s\n", tpnt->libname);
  905. rpnt1_tmp = rpnt1->next->next;
  906. free(rpnt1->next);
  907. rpnt1->next = rpnt1_tmp;
  908. if (rpnt1->next)
  909. rpnt1->next->prev = rpnt1;
  910. break;
  911. }
  912. }
  913. }
  914. }
  915. free(tpnt->libname);
  916. if (handle->dyn != tpnt)
  917. free(tpnt->symbol_scope.r_list);
  918. free(tpnt);
  919. }
  920. }
  921. /* Unlink and release the handle's local scope from global one */
  922. if(ls)
  923. ls->next = ls_next;
  924. free(handle_rlist);
  925. for (rpnt1 = handle->next; rpnt1; rpnt1 = rpnt1_tmp) {
  926. rpnt1_tmp = rpnt1->next;
  927. free(rpnt1);
  928. }
  929. free(handle->init_fini.init_fini);
  930. free(handle);
  931. #if defined(USE_TLS) && USE_TLS
  932. /* If we removed any object which uses TLS bump the generation counter. */
  933. if (any_tls) {
  934. if (__builtin_expect(++_dl_tls_generation == 0, 0)) {
  935. _dl_debug_early("TLS generation counter wrapped! Please report to the uClibc mailing list.\n");
  936. _dl_exit(30);
  937. }
  938. if (tls_free_end == _dl_tls_static_used)
  939. _dl_tls_static_used = tls_free_start;
  940. }
  941. #endif
  942. if (_dl_debug_addr) {
  943. dl_brk = (void (*)(void)) _dl_debug_addr->r_brk;
  944. if (dl_brk != NULL) {
  945. _dl_debug_addr->r_state = RT_DELETE;
  946. (*dl_brk) ();
  947. _dl_debug_addr->r_state = RT_CONSISTENT;
  948. (*dl_brk) ();
  949. }
  950. }
  951. return 0;
  952. }
  953. int dlclose(void *vhandle)
  954. {
  955. int ret;
  956. __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
  957. ret = do_dlclose(vhandle, 1);
  958. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
  959. return ret;
  960. }
  961. char *dlerror(void)
  962. {
  963. const char *retval;
  964. if (!_dl_error_number)
  965. return NULL;
  966. retval = dl_error_names[_dl_error_number];
  967. _dl_error_number = 0;
  968. return (char *)retval;
  969. }
  970. #ifdef __USE_GNU
  971. static int do_dladdr(const void *__address, Dl_info * __info)
  972. {
  973. struct elf_resolve *pelf;
  974. struct elf_resolve *rpnt;
  975. _dl_map_cache();
  976. /*
  977. * Try and locate the module address is in
  978. */
  979. pelf = NULL;
  980. _dl_if_debug_print("__address: %p __info: %p\n", __address, __info);
  981. __address = DL_LOOKUP_ADDRESS (__address);
  982. for (rpnt = _dl_loaded_modules; rpnt; rpnt = rpnt->next) {
  983. struct elf_resolve *tpnt;
  984. tpnt = rpnt;
  985. _dl_if_debug_print("Module \"%s\" at %p\n",
  986. tpnt->libname, DL_LOADADDR_BASE(tpnt->loadaddr));
  987. if (DL_ADDR_IN_LOADADDR((ElfW(Addr)) __address, tpnt, pelf))
  988. pelf = tpnt;
  989. }
  990. if (!pelf) {
  991. return 0;
  992. }
  993. /*
  994. * Try and locate the symbol of address
  995. */
  996. {
  997. char *strtab;
  998. ElfW(Sym) *symtab;
  999. unsigned int hn, si, sn, sf;
  1000. ElfW(Addr) sa = 0;
  1001. /* Set the info for the object the address lies in */
  1002. __info->dli_fname = pelf->libname;
  1003. __info->dli_fbase = (void *)pelf->mapaddr;
  1004. symtab = (ElfW(Sym) *) (pelf->dynamic_info[DT_SYMTAB]);
  1005. strtab = (char *) (pelf->dynamic_info[DT_STRTAB]);
  1006. sf = sn = 0;
  1007. #ifdef __LDSO_GNU_HASH_SUPPORT__
  1008. if (pelf->l_gnu_bitmask) {
  1009. for (hn = 0; hn < pelf->nbucket; hn++) {
  1010. si = pelf->l_gnu_buckets[hn];
  1011. if (!si)
  1012. continue;
  1013. const Elf32_Word *hasharr = &pelf->l_gnu_chain_zero[si];
  1014. do {
  1015. ElfW(Addr) symbol_addr;
  1016. symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
  1017. if ((symtab[si].st_shndx != SHN_UNDEF
  1018. || symtab[si].st_value != 0)
  1019. && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
  1020. && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
  1021. (ElfW(Addr)) __address)) {
  1022. sa = symbol_addr;
  1023. sn = si;
  1024. sf = 1;
  1025. }
  1026. _dl_if_debug_print("Symbol \"%s\" at %p\n", strtab + symtab[si].st_name, symbol_addr);
  1027. ++si;
  1028. } while ((*hasharr++ & 1u) == 0);
  1029. }
  1030. } else
  1031. #endif
  1032. for (hn = 0; hn < pelf->nbucket; hn++) {
  1033. for (si = pelf->elf_buckets[hn]; si; si = pelf->chains[si]) {
  1034. ElfW(Addr) symbol_addr;
  1035. symbol_addr = (ElfW(Addr)) DL_RELOC_ADDR(pelf->loadaddr, symtab[si].st_value);
  1036. if ((symtab[si].st_shndx != SHN_UNDEF
  1037. || symtab[si].st_value != 0)
  1038. && ELF_ST_TYPE(symtab[si].st_info) != STT_TLS
  1039. && DL_ADDR_SYM_MATCH(symbol_addr, &symtab[si], sa,
  1040. (ElfW(Addr)) __address)) {
  1041. sa = symbol_addr;
  1042. sn = si;
  1043. sf = 1;
  1044. }
  1045. _dl_if_debug_print("Symbol \"%s\" at %p\n",
  1046. strtab + symtab[si].st_name, symbol_addr);
  1047. }
  1048. }
  1049. if (sf) {
  1050. /* A nearest symbol has been found; fill the entries */
  1051. __info->dli_sname = strtab + symtab[sn].st_name;
  1052. __info->dli_saddr = (void *)sa;
  1053. } else {
  1054. /* No symbol found, fill entries with NULL value,
  1055. only the containing object will be returned. */
  1056. __info->dli_sname = NULL;
  1057. __info->dli_saddr = NULL;
  1058. }
  1059. return 1;
  1060. }
  1061. }
  1062. #endif
  1063. int dladdr(const void *__address, Dl_info * __info)
  1064. {
  1065. int ret;
  1066. __UCLIBC_MUTEX_CONDITIONAL_LOCK(_dl_mutex, 1);
  1067. ret = do_dladdr(__address, __info);
  1068. __UCLIBC_MUTEX_CONDITIONAL_UNLOCK(_dl_mutex, 1);
  1069. return ret;
  1070. }