libdl.c 35 KB

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