ldso.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418
  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) 2005 by Joakim Tjernlund
  7. * Copyright (C) 2000-2006 by Erik Andersen <andersen@codepoet.org>
  8. * Copyright (c) 1994-2000 Eric Youngdale, Peter MacDonald,
  9. * David Engel, Hongjiu Lu and Mitch D'Souza
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. * 1. Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * 2. The name of the above contributors may not be
  17. * used to endorse or promote products derived from this software
  18. * without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE CONTRIBUTORS ``AS IS'' AND
  21. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23. * ARE DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE
  24. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  25. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  26. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  28. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  29. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. */
  32. #include "ldso.h"
  33. #include "unsecvars.h"
  34. /* Pull in common debug code */
  35. #include "dl-debug.c"
  36. #define ALLOW_ZERO_PLTGOT
  37. #if defined(USE_TLS) && USE_TLS
  38. #include "dl-tls.c"
  39. #endif
  40. /* Pull in the value of _dl_progname */
  41. #include LDSO_ELFINTERP
  42. /* Global variables used within the shared library loader */
  43. #ifdef __LDSO_LD_LIBRARY_PATH__
  44. char *_dl_library_path = NULL; /* Where we look for libraries */
  45. #endif
  46. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  47. char *_dl_preload = NULL; /* Things to be loaded before the libs */
  48. #endif
  49. #ifdef __LDSO_SEARCH_INTERP_PATH__
  50. char *_dl_ldsopath = NULL; /* Location of the shared lib loader */
  51. #endif
  52. int _dl_errno = 0; /* We can't use the real errno in ldso */
  53. size_t _dl_pagesize = 0; /* Store the page size for use later */
  54. struct r_debug *_dl_debug_addr = NULL; /* Used to communicate with the gdb debugger */
  55. void *(*_dl_malloc_function) (size_t size) = NULL;
  56. void (*_dl_free_function) (void *p) = NULL;
  57. #ifdef __LDSO_PRELINK_SUPPORT__
  58. char *_dl_trace_prelink = NULL; /* Library for prelinking trace */
  59. struct elf_resolve *_dl_trace_prelink_map = NULL; /* Library module for prelinking trace */
  60. bool _dl_verbose = true; /* On by default */
  61. bool prelinked = false;
  62. #endif
  63. static int _dl_secure = 1; /* Are we dealing with setuid stuff? */
  64. #ifdef __SUPPORT_LD_DEBUG__
  65. char *_dl_debug = NULL;
  66. char *_dl_debug_symbols = NULL;
  67. char *_dl_debug_move = NULL;
  68. char *_dl_debug_reloc = NULL;
  69. char *_dl_debug_detail = NULL;
  70. char *_dl_debug_nofixups = NULL;
  71. char *_dl_debug_bindings = NULL;
  72. int _dl_debug_file = 2;
  73. #endif
  74. #if defined (__LDSO_STANDALONE_SUPPORT__) && defined (__sh__)
  75. /* Not hidden, needed for standalone execution. */
  76. /*
  77. * FIXME: align dl_start for SH to other archs so that we can keep this symbol
  78. * hidden and we don't need to handle in __uClibc_main
  79. */
  80. unsigned long _dl_skip_args = 0;
  81. #else
  82. unsigned long attribute_hidden _dl_skip_args = 0;
  83. #endif
  84. const char *_dl_progname = UCLIBC_LDSO; /* The name of the executable being run */
  85. #include "dl-startup.c"
  86. #include "dl-symbols.c"
  87. #include "dl-array.c"
  88. /*
  89. * This stub function is used by some debuggers. The idea is that they
  90. * can set an internal breakpoint on it, so that we are notified when the
  91. * address mapping is changed in some way.
  92. */
  93. void _dl_debug_state(void);
  94. rtld_hidden_proto(_dl_debug_state, noinline);
  95. void _dl_debug_state(void)
  96. {
  97. /* Make sure GCC doesn't recognize this function as pure, to avoid
  98. * having the calls optimized away.
  99. */
  100. __asm__("");
  101. }
  102. rtld_hidden_def(_dl_debug_state);
  103. static unsigned char *_dl_malloc_addr = NULL; /* Lets _dl_malloc use the already allocated memory page */
  104. static unsigned char *_dl_mmap_zero = NULL; /* Also used by _dl_malloc */
  105. static struct elf_resolve **init_fini_list;
  106. static struct elf_resolve **scope_elem_list;
  107. static unsigned int nlist; /* # items in init_fini_list */
  108. extern void _start(void);
  109. #ifdef __UCLIBC_HAS_SSP__
  110. # include <dl-osinfo.h>
  111. static uintptr_t stack_chk_guard;
  112. # ifndef THREAD_SET_STACK_GUARD
  113. /* Only exported for architectures that don't store the stack guard canary
  114. * in local thread area. */
  115. uintptr_t __stack_chk_guard attribute_relro;
  116. # endif
  117. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  118. uintptr_t __guard attribute_relro;
  119. # endif
  120. #endif
  121. char *_dl_getenv(const char *symbol, char **envp)
  122. {
  123. char *pnt;
  124. const char *pnt1;
  125. while ((pnt = *envp++)) {
  126. pnt1 = symbol;
  127. while (*pnt && *pnt == *pnt1)
  128. pnt1++, pnt++;
  129. if (!*pnt || *pnt != '=' || *pnt1)
  130. continue;
  131. return pnt + 1;
  132. }
  133. return 0;
  134. }
  135. void _dl_unsetenv(const char *symbol, char **envp)
  136. {
  137. char *pnt;
  138. const char *pnt1;
  139. char **newenvp = envp;
  140. for (pnt = *envp; pnt; pnt = *++envp) {
  141. pnt1 = symbol;
  142. while (*pnt && *pnt == *pnt1)
  143. pnt1++, pnt++;
  144. if (!*pnt || *pnt != '=' || *pnt1)
  145. *newenvp++ = *envp;
  146. }
  147. *newenvp++ = *envp;
  148. return;
  149. }
  150. static int _dl_suid_ok(void)
  151. {
  152. __kernel_uid_t uid, euid;
  153. __kernel_gid_t gid, egid;
  154. uid = _dl_getuid();
  155. euid = _dl_geteuid();
  156. gid = _dl_getgid();
  157. egid = _dl_getegid();
  158. if (uid == euid && gid == egid) {
  159. return 1;
  160. }
  161. return 0;
  162. }
  163. void *_dl_malloc(size_t size)
  164. {
  165. void *retval;
  166. #if 0
  167. _dl_debug_early("request for %d bytes\n", size);
  168. #endif
  169. if (_dl_malloc_function)
  170. return (*_dl_malloc_function) (size);
  171. if (_dl_malloc_addr - _dl_mmap_zero + size > _dl_pagesize) {
  172. size_t rounded_size;
  173. /* Since the above assumes we get a full page even if
  174. we request less than that, make sure we request a
  175. full page, since uClinux may give us less than than
  176. a full page. We might round even
  177. larger-than-a-page sizes, but we end up never
  178. reusing _dl_mmap_zero/_dl_malloc_addr in that case,
  179. so we don't do it.
  180. The actual page size doesn't really matter; as long
  181. as we're self-consistent here, we're safe. */
  182. if (size < _dl_pagesize)
  183. rounded_size = (size + ADDR_ALIGN) & _dl_pagesize;
  184. else
  185. rounded_size = size;
  186. _dl_debug_early("mmapping more memory\n");
  187. _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, rounded_size,
  188. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS | MAP_UNINITIALIZE, -1, 0);
  189. if (_dl_mmap_check_error(_dl_mmap_zero)) {
  190. _dl_dprintf(_dl_debug_file, "%s: mmap of a spare page failed!\n", _dl_progname);
  191. _dl_exit(20);
  192. }
  193. }
  194. retval = _dl_malloc_addr;
  195. _dl_malloc_addr += size;
  196. /*
  197. * Align memory to DL_MALLOC_ALIGN byte boundary. Some
  198. * platforms require this, others simply get better
  199. * performance.
  200. */
  201. _dl_malloc_addr = (unsigned char *) (((unsigned long) _dl_malloc_addr + DL_MALLOC_ALIGN - 1) & ~(DL_MALLOC_ALIGN - 1));
  202. return retval;
  203. }
  204. static void *_dl_zalloc(size_t size)
  205. {
  206. void *p = _dl_malloc(size);
  207. if (p)
  208. _dl_memset(p, 0, size);
  209. return p;
  210. }
  211. void _dl_free(void *p)
  212. {
  213. if (_dl_free_function)
  214. (*_dl_free_function) (p);
  215. }
  216. #if defined(USE_TLS) && USE_TLS
  217. void *_dl_memalign(size_t __boundary, size_t __size)
  218. {
  219. void *result;
  220. int i = 0;
  221. size_t delta;
  222. size_t rounded = 0;
  223. if (_dl_memalign_function)
  224. return (*_dl_memalign_function) (__boundary, __size);
  225. while (rounded < __boundary) {
  226. rounded = (1 << i++);
  227. }
  228. delta = (((size_t) _dl_malloc_addr + __size) & (rounded - 1));
  229. if ((result = _dl_malloc(rounded - delta)) == NULL)
  230. return result;
  231. result = _dl_malloc(__size);
  232. return result;
  233. }
  234. #endif
  235. static void __attribute__ ((destructor)) __attribute_used__ _dl_fini(void)
  236. {
  237. unsigned int i;
  238. struct elf_resolve * tpnt;
  239. for (i = 0; i < nlist; ++i) {
  240. tpnt = init_fini_list[i];
  241. if (tpnt->init_flag & FINI_FUNCS_CALLED)
  242. continue;
  243. tpnt->init_flag |= FINI_FUNCS_CALLED;
  244. _dl_run_fini_array(tpnt);
  245. if (tpnt->dynamic_info[DT_FINI]) {
  246. void (*dl_elf_func) (void);
  247. dl_elf_func = (void (*)(void)) (intptr_t) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_FINI]);
  248. _dl_if_debug_dprint("\ncalling FINI: %s\n\n", tpnt->libname);
  249. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  250. }
  251. }
  252. }
  253. #ifdef __LDSO_PRELINK_SUPPORT__
  254. static void trace_objects(struct elf_resolve *tpnt, char *str_name)
  255. {
  256. if (_dl_strcmp(_dl_trace_prelink, tpnt->libname) == 0)
  257. _dl_trace_prelink_map = tpnt;
  258. if (tpnt->libtype == elf_executable) {
  259. /* Main executeble */
  260. _dl_dprintf(1, "\t%s => %s (%x, %x)", tpnt->libname, tpnt->libname,
  261. tpnt->mapaddr, DL_LOADADDR_BASE(tpnt->loadaddr));
  262. } else {
  263. /* Preloaded, Needed or interpreter */
  264. _dl_dprintf(1, "\t%s => %s (%x, %x)", str_name, tpnt->libname,
  265. tpnt->mapaddr, DL_LOADADDR_BASE(tpnt->loadaddr));
  266. }
  267. #if defined USE_TLS && USE_TLS
  268. if ((tpnt->libtype != program_interpreter) && (tpnt->l_tls_modid))
  269. _dl_dprintf (1, " TLS(%x, %x)\n", tpnt->l_tls_modid,
  270. (size_t) tpnt->l_tls_offset);
  271. else
  272. #endif
  273. _dl_dprintf (1, "\n");
  274. }
  275. #endif
  276. static struct elf_resolve * add_ldso(struct elf_resolve *tpnt,
  277. DL_LOADADDR_TYPE load_addr,
  278. ElfW(auxv_t) auxvt[AT_EGID + 1],
  279. struct dyn_elf *rpnt)
  280. {
  281. ElfW(Ehdr) *epnt = (ElfW(Ehdr) *) auxvt[AT_BASE].a_un.a_val;
  282. ElfW(Phdr) *myppnt = (ElfW(Phdr) *)
  283. DL_RELOC_ADDR(load_addr, epnt->e_phoff);
  284. int j;
  285. struct stat st;
  286. tpnt = _dl_add_elf_hash_table(tpnt->libname, tpnt->loadaddr,
  287. tpnt->dynamic_info, (unsigned long)tpnt->dynamic_addr,
  288. 0);
  289. tpnt->mapaddr = load_addr;
  290. if (_dl_stat(tpnt->libname, &st) >= 0) {
  291. tpnt->st_dev = st.st_dev;
  292. tpnt->st_ino = st.st_ino;
  293. }
  294. tpnt->n_phent = epnt->e_phnum;
  295. tpnt->ppnt = myppnt;
  296. for (j = 0; j < epnt->e_phnum; j++, myppnt++) {
  297. if (myppnt->p_type == PT_GNU_RELRO) {
  298. tpnt->relro_addr = myppnt->p_vaddr;
  299. tpnt->relro_size = myppnt->p_memsz;
  300. break;
  301. }
  302. }
  303. tpnt->libtype = program_interpreter;
  304. if (rpnt) {
  305. rpnt->next = _dl_zalloc(sizeof(struct dyn_elf));
  306. rpnt->next->prev = rpnt;
  307. rpnt = rpnt->next;
  308. } else {
  309. rpnt = _dl_zalloc(sizeof(struct dyn_elf));
  310. }
  311. rpnt->dyn = tpnt;
  312. tpnt->rtld_flags = RTLD_NOW | RTLD_GLOBAL; /* Must not be LAZY */
  313. return tpnt;
  314. }
  315. static ptrdiff_t _dl_build_local_scope (struct elf_resolve **list,
  316. struct elf_resolve *map)
  317. {
  318. struct elf_resolve **p = list;
  319. struct init_fini_list *q;
  320. *p++ = map;
  321. map->init_flag |= DL_RESERVED;
  322. if (map->init_fini)
  323. for (q = map->init_fini; q; q = q->next)
  324. if (! (q->tpnt->init_flag & DL_RESERVED))
  325. p += _dl_build_local_scope (p, q->tpnt);
  326. return p - list;
  327. }
  328. void *_dl_get_ready_to_run(struct elf_resolve *tpnt, DL_LOADADDR_TYPE load_addr,
  329. ElfW(auxv_t) auxvt[AT_EGID + 1], char **envp, char **argv
  330. DL_GET_READY_TO_RUN_EXTRA_PARMS)
  331. {
  332. ElfW(Addr) app_mapaddr = 0;
  333. ElfW(Phdr) *ppnt;
  334. ElfW(Dyn) *dpnt;
  335. char *lpntstr;
  336. unsigned int i, cnt, nscope_elem;
  337. int unlazy = 0, trace_loaded_objects = 0;
  338. struct dyn_elf *rpnt;
  339. struct elf_resolve *tcurr;
  340. struct elf_resolve *tpnt1;
  341. struct elf_resolve *ldso_tpnt = NULL;
  342. struct elf_resolve app_tpnt_tmp;
  343. struct elf_resolve *app_tpnt = &app_tpnt_tmp;
  344. struct r_debug *debug_addr;
  345. unsigned long *lpnt;
  346. unsigned long *_dl_envp; /* The environment address */
  347. ElfW(Addr) relro_addr = 0;
  348. size_t relro_size = 0;
  349. struct r_scope_elem *global_scope;
  350. struct elf_resolve **local_scope;
  351. #if defined(USE_TLS) && USE_TLS
  352. void *tcbp = NULL;
  353. #endif
  354. /* Wahoo!!! We managed to make a function call! Get malloc
  355. * setup so we can use _dl_dprintf() to print debug noise
  356. * instead of the SEND_STDERR macros used in dl-startup.c */
  357. _dl_memset(app_tpnt, 0, sizeof(*app_tpnt));
  358. /* Store the page size for later use */
  359. _dl_pagesize = (auxvt[AT_PAGESZ].a_un.a_val) ? (size_t) auxvt[AT_PAGESZ].a_un.a_val : PAGE_SIZE;
  360. /* Make it so _dl_malloc can use the page of memory we have already
  361. * allocated. We shouldn't need to grab any more memory. This must
  362. * be first since things like _dl_dprintf() use _dl_malloc()...
  363. */
  364. _dl_malloc_addr = (unsigned char *)_dl_pagesize;
  365. _dl_mmap_zero = 0;
  366. /* Wahoo!!! */
  367. _dl_debug_early("Cool, ldso survived making function calls\n");
  368. /* Now we have done the mandatory linking of some things. We are now
  369. * free to start using global variables, since these things have all
  370. * been fixed up by now. Still no function calls outside of this
  371. * library, since the dynamic resolver is not yet ready.
  372. */
  373. if (argv[0]) {
  374. _dl_progname = argv[0];
  375. }
  376. #ifndef __LDSO_STANDALONE_SUPPORT__
  377. if (_start == (void *) auxvt[AT_ENTRY].a_un.a_val) {
  378. _dl_dprintf(_dl_debug_file, "Standalone execution is not enabled\n");
  379. _dl_exit(1);
  380. }
  381. #endif
  382. /* Start to build the tables of the modules that are required for
  383. * this beast to run. We start with the basic executable, and then
  384. * go from there. Eventually we will run across ourself, and we
  385. * will need to properly deal with that as well.
  386. */
  387. rpnt = NULL;
  388. if (_dl_getenv("LD_BIND_NOW", envp))
  389. unlazy = RTLD_NOW;
  390. /* Now we need to figure out what kind of options are selected.
  391. * Note that for SUID programs we ignore the settings in
  392. * LD_LIBRARY_PATH.
  393. */
  394. if ((auxvt[AT_UID].a_un.a_val == (size_t)-1 && _dl_suid_ok()) ||
  395. (auxvt[AT_UID].a_un.a_val != (size_t)-1 &&
  396. auxvt[AT_UID].a_un.a_val == auxvt[AT_EUID].a_un.a_val &&
  397. auxvt[AT_GID].a_un.a_val == auxvt[AT_EGID].a_un.a_val)) {
  398. _dl_secure = 0;
  399. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  400. _dl_preload = _dl_getenv("LD_PRELOAD", envp);
  401. #endif
  402. #ifdef __LDSO_LD_LIBRARY_PATH__
  403. _dl_library_path = _dl_getenv("LD_LIBRARY_PATH", envp);
  404. #endif
  405. } else {
  406. static const char unsecure_envvars[] =
  407. #ifdef EXTRA_UNSECURE_ENVVARS
  408. EXTRA_UNSECURE_ENVVARS
  409. #endif
  410. UNSECURE_ENVVARS;
  411. const char *nextp;
  412. _dl_secure = 1;
  413. nextp = unsecure_envvars;
  414. do {
  415. _dl_unsetenv (nextp, envp);
  416. /* We could use rawmemchr but this need not be fast. */
  417. nextp = _dl_strchr(nextp, '\0') + 1;
  418. } while (*nextp != '\0');
  419. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  420. _dl_preload = NULL;
  421. #endif
  422. #ifdef __LDSO_LD_LIBRARY_PATH__
  423. _dl_library_path = NULL;
  424. #endif
  425. /* SUID binaries can be exploited if they do LAZY relocation. */
  426. unlazy = RTLD_NOW;
  427. }
  428. #if defined(USE_TLS) && USE_TLS
  429. _dl_error_catch_tsd = &_dl_initial_error_catch_tsd;
  430. _dl_init_static_tls = &_dl_nothread_init_static_tls;
  431. #endif
  432. #ifdef __LDSO_STANDALONE_SUPPORT__
  433. if (_start == (void *) auxvt[AT_ENTRY].a_un.a_val) {
  434. unsigned int *aux_dat = (unsigned int *) argv;
  435. int argc = aux_dat[-1];
  436. tpnt->libname = argv[0];
  437. while (argc > 1)
  438. if (! _dl_strcmp (argv[1], "--library-path") && argc > 2) {
  439. #ifdef __LDSO_LD_LIBRARY_PATH__
  440. _dl_library_path = argv[2];
  441. #endif
  442. _dl_skip_args += 2;
  443. argc -= 2;
  444. argv += 2;
  445. } else
  446. break;
  447. /*
  448. * If we have no further argument the program was called incorrectly.
  449. * Grant the user some education.
  450. */
  451. if (argc < 2) {
  452. _dl_dprintf(1, "\
  453. Usage: ld.so [OPTION]... EXECUTABLE-FILE [ARGS-FOR-PROGRAM...]\n\
  454. You have invoked `ld.so', the helper program for shared library executables.\n\
  455. This program usually lives in the file `/lib/ld.so', and special directives\n\
  456. in executable files using ELF shared libraries tell the system's program\n\
  457. loader to load the helper program from this file. This helper program loads\n\
  458. the shared libraries needed by the program executable, prepares the program\n\
  459. to run, and runs it. You may invoke this helper program directly from the\n\
  460. command line to load and run an ELF executable file; this is like executing\n\
  461. that file itself, but always uses this helper program from the file you\n\
  462. specified, instead of the helper program file specified in the executable\n\
  463. file you run. This is mostly of use for maintainers to test new versions\n\
  464. of this helper program; chances are you did not intend to run this program.\n\
  465. \n\
  466. --library-path PATH use given PATH instead of content of the environment\n\
  467. variable LD_LIBRARY_PATH\n");
  468. _dl_exit(1);
  469. }
  470. ++_dl_skip_args;
  471. ++argv;
  472. _dl_progname = argv[0];
  473. _dl_symbol_tables = rpnt = _dl_zalloc(sizeof(struct dyn_elf));
  474. /*
  475. * It needs to load the _dl_progname and to map it
  476. * Usually it is the main application launched by means of the ld.so
  477. * but it could be also a shared object (when ld.so used for tracing)
  478. * We keep the misleading app_tpnt name to avoid variable pollution
  479. */
  480. app_tpnt = _dl_load_elf_shared_library(_dl_secure, &rpnt, _dl_progname);
  481. if (!app_tpnt) {
  482. _dl_dprintf(_dl_debug_file, "can't load '%s'\n", _dl_progname);
  483. _dl_exit(16);
  484. }
  485. /*
  486. * FIXME: it needs to properly handle a PIE executable
  487. * Usually for a main application, loadaddr is computed as difference
  488. * between auxvt entry points and phdr, so if it is not 0, that it is a
  489. * PIE executable. In this case instead we need to set the loadaddr to 0
  490. * because we are actually mapping the ELF for the main application by
  491. * ourselves. So the PIE case must be checked.
  492. */
  493. app_tpnt->rtld_flags = unlazy | RTLD_GLOBAL;
  494. /*
  495. * This is used by gdb to locate the chain of shared libraries that are
  496. * currently loaded.
  497. */
  498. debug_addr = _dl_zalloc(sizeof(struct r_debug));
  499. ppnt = (ElfW(Phdr) *)app_tpnt->ppnt;
  500. for (i = 0; i < app_tpnt->n_phent; i++, ppnt++) {
  501. if (ppnt->p_type == PT_DYNAMIC) {
  502. dpnt = (ElfW(Dyn) *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  503. _dl_parse_dynamic_info(dpnt, app_tpnt->dynamic_info, debug_addr, app_tpnt->loadaddr);
  504. }
  505. }
  506. #ifdef __LDSO_SEARCH_INTERP_PATH__
  507. {
  508. char *ptmp;
  509. /* Store the path where the shared lib loader was found
  510. * for later use
  511. */
  512. _dl_ldsopath = _dl_strdup(tpnt->libname);
  513. ptmp = _dl_strrchr(_dl_ldsopath, '/');
  514. if (ptmp != _dl_ldsopath)
  515. *ptmp = '\0';
  516. _dl_debug_early("Lib Loader: (%x) %s\n", (unsigned) DL_LOADADDR_BASE(tpnt->loadaddr), tpnt->libname);
  517. }
  518. #endif
  519. } else {
  520. #endif
  521. /* At this point we are now free to examine the user application,
  522. * and figure out which libraries are supposed to be called. Until
  523. * we have this list, we will not be completely ready for dynamic
  524. * linking.
  525. */
  526. /* Find the runtime load address of the main executable. This may be
  527. * different from what the ELF header says for ET_DYN/PIE executables.
  528. */
  529. {
  530. unsigned int idx;
  531. ElfW(Phdr) *phdr = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  532. for (idx = 0; idx < auxvt[AT_PHNUM].a_un.a_val; idx++, phdr++)
  533. if (phdr->p_type == PT_PHDR) {
  534. DL_INIT_LOADADDR_PROG(app_tpnt->loadaddr, auxvt[AT_PHDR].a_un.a_val - phdr->p_vaddr);
  535. break;
  536. }
  537. if (DL_LOADADDR_BASE(app_tpnt->loadaddr))
  538. _dl_debug_early("Position Independent Executable: "
  539. "app_tpnt->loadaddr=%x\n", DL_LOADADDR_BASE(app_tpnt->loadaddr));
  540. }
  541. /*
  542. * This is used by gdb to locate the chain of shared libraries that are
  543. * currently loaded.
  544. */
  545. debug_addr = _dl_zalloc(sizeof(struct r_debug));
  546. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  547. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  548. if (ppnt->p_type == PT_GNU_RELRO) {
  549. relro_addr = ppnt->p_vaddr;
  550. relro_size = ppnt->p_memsz;
  551. }
  552. if (!app_mapaddr && (ppnt->p_type == PT_LOAD)) {
  553. app_mapaddr = DL_RELOC_ADDR (app_tpnt->loadaddr, ppnt->p_vaddr);
  554. }
  555. if (ppnt->p_type == PT_DYNAMIC) {
  556. dpnt = (ElfW(Dyn) *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  557. _dl_parse_dynamic_info(dpnt, app_tpnt->dynamic_info, debug_addr, app_tpnt->loadaddr);
  558. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  559. /* Ugly, ugly. We need to call mprotect to change the
  560. * protection of the text pages so that we can do the
  561. * dynamic linking. We can set the protection back
  562. * again once we are done.
  563. */
  564. _dl_debug_early("calling mprotect on the application program\n");
  565. /* Now cover the application program. */
  566. if (app_tpnt->dynamic_info[DT_TEXTREL]) {
  567. ElfW(Phdr) *ppnt_outer = ppnt;
  568. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  569. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  570. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  571. _dl_mprotect((void *) (DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr) & PAGE_ALIGN),
  572. (DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr) & ADDR_ALIGN) +
  573. (unsigned long) ppnt->p_filesz,
  574. PROT_READ | PROT_WRITE | PROT_EXEC);
  575. }
  576. ppnt = ppnt_outer;
  577. }
  578. #else
  579. if (app_tpnt->dynamic_info[DT_TEXTREL]) {
  580. _dl_dprintf(_dl_debug_file, "Can't modify application's text section; use the GCC option -fPIE for position-independent executables.\n");
  581. _dl_exit(1);
  582. }
  583. #endif
  584. #ifndef ALLOW_ZERO_PLTGOT
  585. /* make sure it's really there. */
  586. if (app_tpnt->dynamic_info[DT_PLTGOT] == 0)
  587. continue;
  588. #endif
  589. /* OK, we have what we need - slip this one into the list. */
  590. app_tpnt = _dl_add_elf_hash_table(_dl_progname, app_tpnt->loadaddr,
  591. app_tpnt->dynamic_info,
  592. (unsigned long) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr),
  593. ppnt->p_filesz);
  594. _dl_loaded_modules->libtype = elf_executable;
  595. _dl_loaded_modules->ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  596. _dl_loaded_modules->n_phent = auxvt[AT_PHNUM].a_un.a_val;
  597. _dl_symbol_tables = rpnt = _dl_zalloc(sizeof(struct dyn_elf));
  598. rpnt->dyn = _dl_loaded_modules;
  599. app_tpnt->mapaddr = app_mapaddr;
  600. app_tpnt->rtld_flags = unlazy | RTLD_GLOBAL;
  601. app_tpnt->usage_count++;
  602. lpnt = (unsigned long *) (app_tpnt->dynamic_info[DT_PLTGOT]);
  603. #ifdef ALLOW_ZERO_PLTGOT
  604. if (lpnt)
  605. #endif
  606. INIT_GOT(lpnt, _dl_loaded_modules);
  607. }
  608. /* OK, fill this in - we did not have this before */
  609. if (ppnt->p_type == PT_INTERP) {
  610. tpnt->libname = (char *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  611. #ifdef __LDSO_SEARCH_INTERP_PATH__
  612. {
  613. char *ptmp;
  614. /* Store the path where the shared lib loader was found
  615. * for later use
  616. */
  617. _dl_ldsopath = _dl_strdup(tpnt->libname);
  618. ptmp = _dl_strrchr(_dl_ldsopath, '/');
  619. if (ptmp != _dl_ldsopath)
  620. *ptmp = '\0';
  621. }
  622. _dl_debug_early("Lib Loader: (%x) %s\n", (unsigned) DL_LOADADDR_BASE(tpnt->loadaddr), tpnt->libname);
  623. #endif
  624. }
  625. /* Discover any TLS sections if the target supports them. */
  626. if (ppnt->p_type == PT_TLS) {
  627. #if defined(USE_TLS) && USE_TLS
  628. if (ppnt->p_memsz > 0) {
  629. app_tpnt->l_tls_blocksize = ppnt->p_memsz;
  630. app_tpnt->l_tls_align = ppnt->p_align;
  631. if (ppnt->p_align == 0)
  632. app_tpnt->l_tls_firstbyte_offset = 0;
  633. else
  634. app_tpnt->l_tls_firstbyte_offset =
  635. (ppnt->p_vaddr & (ppnt->p_align - 1));
  636. app_tpnt->l_tls_initimage_size = ppnt->p_filesz;
  637. app_tpnt->l_tls_initimage = (void *) ppnt->p_vaddr;
  638. /* This image gets the ID one. */
  639. _dl_tls_max_dtv_idx = app_tpnt->l_tls_modid = 1;
  640. }
  641. _dl_debug_early("Found TLS header for appplication program\n");
  642. break;
  643. #else
  644. _dl_dprintf(_dl_debug_file, "Program uses unsupported TLS data!\n");
  645. _dl_exit(1);
  646. #endif
  647. }
  648. }
  649. app_tpnt->relro_addr = relro_addr;
  650. app_tpnt->relro_size = relro_size;
  651. #if defined(USE_TLS) && USE_TLS
  652. /*
  653. * Adjust the address of the TLS initialization image in
  654. * case the executable is actually an ET_DYN object.
  655. */
  656. if (app_tpnt->l_tls_initimage != NULL) {
  657. unsigned int tmp = (unsigned int) app_tpnt->l_tls_initimage;
  658. app_tpnt->l_tls_initimage =
  659. (char *) app_tpnt->l_tls_initimage + app_tpnt->loadaddr;
  660. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n",
  661. tmp, app_tpnt->l_tls_initimage, app_tpnt->l_tls_initimage_size);
  662. }
  663. #endif
  664. #ifdef __LDSO_STANDALONE_SUPPORT__
  665. } /* ! ldso standalone mode */
  666. #endif
  667. #ifdef __SUPPORT_LD_DEBUG__
  668. _dl_debug = _dl_getenv("LD_DEBUG", envp);
  669. if (_dl_debug) {
  670. if (_dl_strstr(_dl_debug, "all")) {
  671. _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
  672. = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
  673. } else {
  674. _dl_debug_detail = _dl_strstr(_dl_debug, "detail");
  675. _dl_debug_move = _dl_strstr(_dl_debug, "move");
  676. _dl_debug_symbols = _dl_strstr(_dl_debug, "sym");
  677. _dl_debug_reloc = _dl_strstr(_dl_debug, "reloc");
  678. _dl_debug_nofixups = _dl_strstr(_dl_debug, "nofix");
  679. _dl_debug_bindings = _dl_strstr(_dl_debug, "bind");
  680. }
  681. }
  682. {
  683. const char *dl_debug_output;
  684. dl_debug_output = _dl_getenv("LD_DEBUG_OUTPUT", envp);
  685. if (dl_debug_output) {
  686. char tmp[22], *tmp1, *filename;
  687. int len1, len2;
  688. _dl_memset(tmp, 0, sizeof(tmp));
  689. tmp1 = _dl_simple_ltoa( tmp, (unsigned long)_dl_getpid());
  690. len1 = _dl_strlen(dl_debug_output);
  691. len2 = _dl_strlen(tmp1);
  692. filename = _dl_malloc(len1 + len2 + 2);
  693. if (filename) {
  694. _dl_strcpy (filename, dl_debug_output);
  695. filename[len1] = '.';
  696. _dl_strcpy (&filename[len1+1], tmp1);
  697. _dl_debug_file = _dl_open(filename, O_WRONLY|O_CREAT, 0644);
  698. if (_dl_debug_file < 0) {
  699. _dl_debug_file = 2;
  700. _dl_dprintf(_dl_debug_file, "can't open file: '%s'\n",filename);
  701. }
  702. }
  703. }
  704. }
  705. #endif
  706. #ifdef __LDSO_PRELINK_SUPPORT__
  707. {
  708. char *ld_warn = _dl_getenv ("LD_WARN", envp);
  709. if (ld_warn && *ld_warn == '\0')
  710. _dl_verbose = false;
  711. }
  712. _dl_trace_prelink = _dl_getenv("LD_TRACE_PRELINKING", envp);
  713. #endif
  714. if (_dl_getenv("LD_TRACE_LOADED_OBJECTS", envp) != NULL) {
  715. trace_loaded_objects++;
  716. }
  717. #ifndef __LDSO_LDD_SUPPORT__
  718. if (trace_loaded_objects) {
  719. _dl_dprintf(_dl_debug_file, "Use the ldd provided by uClibc\n");
  720. _dl_exit(1);
  721. }
  722. #endif
  723. /*
  724. * OK, fix one more thing - set up debug_addr so it will point
  725. * to our chain. Later we may need to fill in more fields, but this
  726. * should be enough for now.
  727. */
  728. debug_addr->r_map = (struct link_map *) _dl_loaded_modules;
  729. debug_addr->r_version = 1;
  730. debug_addr->r_ldbase = (ElfW(Addr)) DL_LOADADDR_BASE(load_addr);
  731. debug_addr->r_brk = (unsigned long) &_dl_debug_state;
  732. _dl_debug_addr = debug_addr;
  733. /* Do not notify the debugger until the interpreter is in the list */
  734. /* OK, we now have the application in the list, and we have some
  735. * basic stuff in place. Now search through the list for other shared
  736. * libraries that should be loaded, and insert them on the list in the
  737. * correct order.
  738. */
  739. _dl_map_cache();
  740. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  741. if (_dl_preload) {
  742. char c, *str, *str2;
  743. str = _dl_preload;
  744. while (*str == ':' || *str == ' ' || *str == '\t')
  745. str++;
  746. while (*str) {
  747. str2 = str;
  748. while (*str2 && *str2 != ':' && *str2 != ' ' && *str2 != '\t')
  749. str2++;
  750. c = *str2;
  751. *str2 = '\0';
  752. if (!_dl_secure || _dl_strchr(str, '/') == NULL) {
  753. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", str, _dl_progname);
  754. tpnt1 = _dl_load_shared_library(_dl_secure, &rpnt, NULL, str, trace_loaded_objects);
  755. if (!tpnt1) {
  756. #ifdef __LDSO_LDD_SUPPORT__
  757. if (trace_loaded_objects || _dl_trace_prelink)
  758. _dl_dprintf(1, "\t%s => not found\n", str);
  759. else
  760. #endif
  761. {
  762. _dl_dprintf(_dl_debug_file, "%s: can't load " "library '%s'\n", _dl_progname, str);
  763. _dl_exit(15);
  764. }
  765. } else {
  766. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  767. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  768. #ifdef __LDSO_LDD_SUPPORT__
  769. if (trace_loaded_objects && !_dl_trace_prelink &&
  770. tpnt1->usage_count == 1) {
  771. /* This is a real hack to make
  772. * ldd not print the library
  773. * itself when run on a
  774. * library.
  775. */
  776. if (_dl_strcmp(_dl_progname, str) != 0)
  777. _dl_dprintf(1, "\t%s => %s (%x)\n", str, tpnt1->libname,
  778. DL_LOADADDR_BASE(tpnt1->loadaddr));
  779. }
  780. #endif
  781. }
  782. }
  783. *str2 = c;
  784. str = str2;
  785. while (*str == ':' || *str == ' ' || *str == '\t')
  786. str++;
  787. }
  788. }
  789. #endif /* __LDSO_PRELOAD_ENV_SUPPORT__ */
  790. #ifdef __LDSO_PRELOAD_FILE_SUPPORT__
  791. do {
  792. char *preload;
  793. int fd;
  794. char c, *cp, *cp2;
  795. struct stat st;
  796. if (_dl_stat(LDSO_PRELOAD, &st) || st.st_size == 0) {
  797. break;
  798. }
  799. if ((fd = _dl_open(LDSO_PRELOAD, O_RDONLY, 0)) < 0) {
  800. _dl_dprintf(_dl_debug_file, "%s: can't open file '%s'\n",
  801. _dl_progname, LDSO_PRELOAD);
  802. break;
  803. }
  804. preload = (caddr_t) _dl_mmap(0, st.st_size + 1,
  805. PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  806. _dl_close(fd);
  807. if (preload == (caddr_t) -1) {
  808. _dl_dprintf(_dl_debug_file, "%s:%i: can't map '%s'\n",
  809. _dl_progname, __LINE__, LDSO_PRELOAD);
  810. break;
  811. }
  812. /* convert all separators and comments to spaces */
  813. for (cp = preload; *cp; /*nada */ ) {
  814. if (*cp == ':' || *cp == '\t' || *cp == '\n') {
  815. *cp++ = ' ';
  816. } else if (*cp == '#') {
  817. do {
  818. *cp++ = ' ';
  819. } while (*cp != '\n' && *cp != '\0');
  820. } else {
  821. cp++;
  822. }
  823. }
  824. /* find start of first library */
  825. for (cp = preload; *cp && *cp == ' '; cp++)
  826. /*nada */ ;
  827. while (*cp) {
  828. /* find end of library */
  829. for (cp2 = cp; *cp && *cp != ' '; cp++)
  830. /*nada */ ;
  831. c = *cp;
  832. *cp = '\0';
  833. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", cp2, _dl_progname);
  834. tpnt1 = _dl_load_shared_library(0, &rpnt, NULL, cp2, trace_loaded_objects);
  835. if (!tpnt1) {
  836. # ifdef __LDSO_LDD_SUPPORT__
  837. if (trace_loaded_objects || _dl_trace_prelink)
  838. _dl_dprintf(1, "\t%s => not found\n", cp2);
  839. else
  840. # endif
  841. {
  842. _dl_dprintf(_dl_debug_file, "%s: can't load library '%s'\n", _dl_progname, cp2);
  843. _dl_exit(15);
  844. }
  845. } else {
  846. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  847. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  848. # ifdef __LDSO_LDD_SUPPORT__
  849. if (trace_loaded_objects && !_dl_trace_prelink &&
  850. tpnt1->usage_count == 1) {
  851. _dl_dprintf(1, "\t%s => %s (%x)\n",
  852. cp2, tpnt1->libname,
  853. DL_LOADADDR_BASE(tpnt1->loadaddr));
  854. }
  855. # endif
  856. }
  857. /* find start of next library */
  858. *cp = c;
  859. for ( /*nada */ ; *cp && *cp == ' '; cp++)
  860. /*nada */ ;
  861. }
  862. _dl_munmap(preload, st.st_size + 1);
  863. } while (0);
  864. #endif /* __LDSO_PRELOAD_FILE_SUPPORT__ */
  865. nlist = 0;
  866. for (tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next) {
  867. ElfW(Dyn) *this_dpnt;
  868. nlist++;
  869. for (this_dpnt = (ElfW(Dyn) *) tcurr->dynamic_addr; this_dpnt->d_tag; this_dpnt++) {
  870. if (this_dpnt->d_tag == DT_NEEDED) {
  871. char *name;
  872. struct init_fini_list *tmp;
  873. lpntstr = (char*) (tcurr->dynamic_info[DT_STRTAB] + this_dpnt->d_un.d_val);
  874. name = _dl_get_last_path_component(lpntstr);
  875. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", lpntstr, _dl_progname);
  876. if (_dl_strcmp(name, UCLIBC_LDSO) == 0) {
  877. if (!ldso_tpnt) {
  878. /* Insert the ld.so only once */
  879. ldso_tpnt = add_ldso(tpnt, load_addr, auxvt, rpnt);
  880. }
  881. ldso_tpnt->usage_count++;
  882. tpnt1 = ldso_tpnt;
  883. } else
  884. tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, trace_loaded_objects);
  885. if (!tpnt1) {
  886. #ifdef __LDSO_LDD_SUPPORT__
  887. if (trace_loaded_objects || _dl_trace_prelink) {
  888. _dl_dprintf(1, "\t%s => not found\n", lpntstr);
  889. continue;
  890. } else
  891. #endif
  892. {
  893. _dl_dprintf(_dl_debug_file, "%s: can't load library '%s'\n", _dl_progname, lpntstr);
  894. _dl_exit(16);
  895. }
  896. }
  897. tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
  898. tmp->tpnt = tpnt1;
  899. tmp->next = tcurr->init_fini;
  900. tcurr->init_fini = tmp;
  901. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  902. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  903. #ifdef __LDSO_LDD_SUPPORT__
  904. if (trace_loaded_objects && !_dl_trace_prelink &&
  905. tpnt1->usage_count == 1) {
  906. _dl_dprintf(1, "\t%s => %s (%x)\n",
  907. lpntstr, tpnt1->libname,
  908. DL_LOADADDR_BASE(tpnt1->loadaddr));
  909. }
  910. #endif
  911. }
  912. }
  913. }
  914. _dl_unmap_cache();
  915. /* Keep track of the number of elements in the global scope */
  916. nscope_elem = nlist;
  917. if (_dl_loaded_modules->libtype == elf_executable) {
  918. --nlist; /* Exclude the application. */
  919. tcurr = _dl_loaded_modules->next;
  920. } else
  921. tcurr = _dl_loaded_modules;
  922. init_fini_list = _dl_malloc(nlist * sizeof(struct elf_resolve *));
  923. i = 0;
  924. for (; tcurr; tcurr = tcurr->next)
  925. init_fini_list[i++] = tcurr;
  926. /* Sort the INIT/FINI list in dependency order. */
  927. for (tcurr = _dl_loaded_modules->next; tcurr; tcurr = tcurr->next) {
  928. unsigned int j, k;
  929. for (j = 0; init_fini_list[j] != tcurr; ++j)
  930. /* Empty */;
  931. for (k = j + 1; k < nlist; ++k) {
  932. struct init_fini_list *runp = init_fini_list[k]->init_fini;
  933. for (; runp; runp = runp->next) {
  934. if (runp->tpnt == tcurr) {
  935. struct elf_resolve *here = init_fini_list[k];
  936. _dl_if_debug_dprint("Move %s from pos %d to %d in INIT/FINI list\n", here->libname, k, j);
  937. for (i = (k - j); i; --i)
  938. init_fini_list[i+j] = init_fini_list[i+j-1];
  939. init_fini_list[j] = here;
  940. ++j;
  941. break;
  942. }
  943. }
  944. }
  945. }
  946. #ifdef __SUPPORT_LD_DEBUG__
  947. if (_dl_debug) {
  948. _dl_dprintf(_dl_debug_file, "\nINIT/FINI order and dependencies:\n");
  949. for (i = 0; i < nlist; i++) {
  950. struct init_fini_list *tmp;
  951. _dl_dprintf(_dl_debug_file, "lib: %s has deps:\n",
  952. init_fini_list[i]->libname);
  953. tmp = init_fini_list[i]->init_fini;
  954. for (; tmp; tmp = tmp->next)
  955. _dl_dprintf(_dl_debug_file, " %s ", tmp->tpnt->libname);
  956. _dl_dprintf(_dl_debug_file, "\n");
  957. }
  958. }
  959. #endif
  960. /*
  961. * If the program interpreter is not in the module chain, add it.
  962. * This will be required for dlopen to be able to access the internal
  963. * functions in the dynamic linker and to relocate the interpreter
  964. * again once all libs are loaded.
  965. */
  966. if (!ldso_tpnt) {
  967. tpnt = add_ldso(tpnt, load_addr, auxvt, rpnt);
  968. tpnt->usage_count++;
  969. nscope_elem++;
  970. } else
  971. tpnt = ldso_tpnt;
  972. #ifdef RERELOCATE_LDSO
  973. /* Only rerelocate functions for now. */
  974. tpnt->init_flag = RELOCS_DONE;
  975. lpnt = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT]);
  976. # ifdef ALLOW_ZERO_PLTGOT
  977. if (tpnt->dynamic_info[DT_PLTGOT])
  978. # endif
  979. INIT_GOT(lpnt, tpnt);
  980. #else
  981. tpnt->init_flag = RELOCS_DONE | JMP_RELOCS_DONE;
  982. #endif
  983. tpnt = NULL;
  984. /*
  985. * Allocate the global scope array.
  986. */
  987. scope_elem_list = (struct elf_resolve **) _dl_malloc(nscope_elem * sizeof(struct elf_resolve *));
  988. for (i = 0, tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next)
  989. scope_elem_list[i++] = tcurr;
  990. _dl_loaded_modules->symbol_scope.r_list = scope_elem_list;
  991. _dl_loaded_modules->symbol_scope.r_nlist = nscope_elem;
  992. /*
  993. * The symbol scope of the application, that is the first entry of the
  994. * _dl_loaded_modules list, is just the global scope to be used for the
  995. * symbol lookup.
  996. */
  997. global_scope = &_dl_loaded_modules->symbol_scope;
  998. /* Build the local scope for each loaded modules. */
  999. local_scope = _dl_malloc(nscope_elem * sizeof(struct elf_resolve *));
  1000. i = 1;
  1001. for (tcurr = _dl_loaded_modules->next; tcurr; tcurr = tcurr->next) {
  1002. unsigned int k;
  1003. cnt = _dl_build_local_scope(local_scope, scope_elem_list[i++]);
  1004. tcurr->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
  1005. tcurr->symbol_scope.r_nlist = cnt;
  1006. _dl_memcpy (tcurr->symbol_scope.r_list, local_scope, cnt * sizeof (struct elf_resolve *));
  1007. /* Restoring the init_flag.*/
  1008. for (k = 1; k < nscope_elem; k++)
  1009. scope_elem_list[k]->init_flag &= ~DL_RESERVED;
  1010. }
  1011. _dl_free(local_scope);
  1012. #ifdef __LDSO_LDD_SUPPORT__
  1013. /* Exit if LD_TRACE_LOADED_OBJECTS is on. */
  1014. if (trace_loaded_objects && !_dl_trace_prelink)
  1015. _dl_exit(0);
  1016. #endif
  1017. #if defined(USE_TLS) && USE_TLS
  1018. /* We do not initialize any of the TLS functionality unless any of the
  1019. * initial modules uses TLS. This makes dynamic loading of modules with
  1020. * TLS impossible, but to support it requires either eagerly doing setup
  1021. * now or lazily doing it later. Doing it now makes us incompatible with
  1022. * an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
  1023. * used. Trying to do it lazily is too hairy to try when there could be
  1024. * multiple threads (from a non-TLS-using libpthread). */
  1025. bool was_tls_init_tp_called = tls_init_tp_called;
  1026. if (tcbp == NULL) {
  1027. _dl_debug_early("Calling init_tls()!\n");
  1028. tcbp = init_tls ();
  1029. }
  1030. #endif
  1031. #ifdef __UCLIBC_HAS_SSP__
  1032. /* Set up the stack checker's canary. */
  1033. stack_chk_guard = _dl_setup_stack_chk_guard ();
  1034. # ifdef THREAD_SET_STACK_GUARD
  1035. THREAD_SET_STACK_GUARD (stack_chk_guard);
  1036. # else
  1037. __stack_chk_guard = stack_chk_guard;
  1038. # endif
  1039. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  1040. __guard = stack_chk_guard;
  1041. # endif
  1042. #endif
  1043. #ifdef __LDSO_PRELINK_SUPPORT__
  1044. if (_dl_trace_prelink) {
  1045. unsigned int nscope_trace = ldso_tpnt ? nscope_elem : (nscope_elem - 1);
  1046. for (i = 0; i < nscope_trace; i++)
  1047. trace_objects(scope_elem_list[i],
  1048. _dl_get_last_path_component(scope_elem_list[i]->libname));
  1049. if (_dl_verbose)
  1050. /* Warn about undefined symbols. */
  1051. if (_dl_symbol_tables)
  1052. if (_dl_fixup(_dl_symbol_tables, global_scope, unlazy))
  1053. _dl_exit(-1);
  1054. _dl_exit(0);
  1055. }
  1056. if (_dl_loaded_modules->dynamic_info[DT_GNU_LIBLIST_IDX]) {
  1057. ElfW(Lib) *liblist, *liblistend;
  1058. struct elf_resolve **r_list, **r_listend, *l;
  1059. const char *strtab = (const char *)_dl_loaded_modules->dynamic_info[DT_STRTAB];
  1060. _dl_assert (_dl_loaded_modules->dynamic_info[DT_GNU_LIBLISTSZ_IDX] != 0);
  1061. liblist = (ElfW(Lib) *) _dl_loaded_modules->dynamic_info[DT_GNU_LIBLIST_IDX];
  1062. liblistend = (ElfW(Lib) *)
  1063. ((char *) liblist + _dl_loaded_modules->dynamic_info[DT_GNU_LIBLISTSZ_IDX]);
  1064. r_list = _dl_loaded_modules->symbol_scope.r_list;
  1065. r_listend = r_list + nscope_elem;
  1066. for (; r_list < r_listend && liblist < liblistend; r_list++) {
  1067. l = *r_list;
  1068. if (l == _dl_loaded_modules)
  1069. continue;
  1070. /* If the library is not mapped where it should, fail. */
  1071. if (l->loadaddr)
  1072. break;
  1073. /* Next, check if checksum matches. */
  1074. if (l->dynamic_info[DT_CHECKSUM_IDX] == 0 ||
  1075. l->dynamic_info[DT_CHECKSUM_IDX] != liblist->l_checksum)
  1076. break;
  1077. if (l->dynamic_info[DT_GNU_PRELINKED_IDX] == 0 ||
  1078. (l->dynamic_info[DT_GNU_PRELINKED_IDX] != liblist->l_time_stamp))
  1079. break;
  1080. if (_dl_strcmp(strtab + liblist->l_name, _dl_get_last_path_component(l->libname)) != 0)
  1081. break;
  1082. ++liblist;
  1083. }
  1084. if (r_list == r_listend && liblist == liblistend)
  1085. prelinked = true;
  1086. }
  1087. _dl_debug_early ("\nprelink checking: %s\n", prelinked ? "ok" : "failed");
  1088. if (prelinked) {
  1089. if (_dl_loaded_modules->dynamic_info[DT_GNU_CONFLICT_IDX]) {
  1090. ELF_RELOC *conflict;
  1091. unsigned long conflict_size;
  1092. _dl_assert (_dl_loaded_modules->dynamic_info[DT_GNU_CONFLICTSZ_IDX] != 0);
  1093. conflict = (ELF_RELOC *) _dl_loaded_modules->dynamic_info[DT_GNU_CONFLICT_IDX];
  1094. conflict_size = _dl_loaded_modules->dynamic_info[DT_GNU_CONFLICTSZ_IDX];
  1095. _dl_parse_relocation_information(_dl_symbol_tables, global_scope,
  1096. (unsigned long) conflict, conflict_size);
  1097. }
  1098. /* Mark all the objects so we know they have been already relocated. */
  1099. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1100. tpnt->init_flag |= RELOCS_DONE;
  1101. if (tpnt->relro_size)
  1102. _dl_protect_relro (tpnt);
  1103. }
  1104. } else
  1105. #endif
  1106. {
  1107. _dl_debug_early("Beginning relocation fixups\n");
  1108. #ifdef __mips__
  1109. /*
  1110. * Relocation of the GOT entries for MIPS have to be done
  1111. * after all the libraries have been loaded.
  1112. */
  1113. _dl_perform_mips_global_got_relocations(_dl_loaded_modules, !unlazy);
  1114. #endif
  1115. /*
  1116. * OK, now all of the kids are tucked into bed in their proper
  1117. * addresses. Now we go through and look for REL and RELA records that
  1118. * indicate fixups to the GOT tables. We need to do this in reverse
  1119. * order so that COPY directives work correctly.
  1120. */
  1121. if (_dl_symbol_tables)
  1122. if (_dl_fixup(_dl_symbol_tables, global_scope, unlazy))
  1123. _dl_exit(-1);
  1124. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1125. if (tpnt->relro_size)
  1126. _dl_protect_relro (tpnt);
  1127. }
  1128. } /* not prelinked */
  1129. #if defined(USE_TLS) && USE_TLS
  1130. if (!was_tls_init_tp_called && _dl_tls_max_dtv_idx > 0)
  1131. ++_dl_tls_generation;
  1132. _dl_debug_early("Calling _dl_allocate_tls_init()!\n");
  1133. /* Now that we have completed relocation, the initializer data
  1134. for the TLS blocks has its final values and we can copy them
  1135. into the main thread's TLS area, which we allocated above. */
  1136. _dl_allocate_tls_init (tcbp);
  1137. /* And finally install it for the main thread. If ld.so itself uses
  1138. TLS we know the thread pointer was initialized earlier. */
  1139. if (! tls_init_tp_called) {
  1140. const char *lossage = (char *) TLS_INIT_TP (tcbp, USE___THREAD);
  1141. if (__builtin_expect (lossage != NULL, 0)) {
  1142. _dl_debug_early("cannot set up thread-local storage: %s\n", lossage);
  1143. _dl_exit(30);
  1144. }
  1145. }
  1146. #endif /* USE_TLS */
  1147. /* OK, at this point things are pretty much ready to run. Now we need
  1148. * to touch up a few items that are required, and then we can let the
  1149. * user application have at it. Note that the dynamic linker itself
  1150. * is not guaranteed to be fully dynamicly linked if we are using
  1151. * ld.so.1, so we have to look up each symbol individually.
  1152. */
  1153. _dl_envp = (unsigned long *) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "__environ", global_scope, NULL, 0, NULL);
  1154. if (_dl_envp)
  1155. *_dl_envp = (unsigned long) envp;
  1156. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  1157. {
  1158. unsigned int j;
  1159. ElfW(Phdr) *myppnt;
  1160. /* We had to set the protections of all pages to R/W for
  1161. * dynamic linking. Set text pages back to R/O.
  1162. */
  1163. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1164. for (myppnt = tpnt->ppnt, j = 0; j < tpnt->n_phent; j++, myppnt++) {
  1165. if (myppnt->p_type == PT_LOAD && !(myppnt->p_flags & PF_W) && tpnt->dynamic_info[DT_TEXTREL]) {
  1166. _dl_mprotect((void *) (DL_RELOC_ADDR(tpnt->loadaddr, myppnt->p_vaddr) & PAGE_ALIGN),
  1167. (myppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) myppnt->p_filesz, LXFLAGS(myppnt->p_flags));
  1168. }
  1169. }
  1170. }
  1171. }
  1172. #endif
  1173. /* Notify the debugger we have added some objects. */
  1174. _dl_debug_addr->r_state = RT_ADD;
  1175. _dl_debug_state();
  1176. /* Run pre-initialization functions for the executable. */
  1177. _dl_run_array_forward(_dl_loaded_modules->dynamic_info[DT_PREINIT_ARRAY],
  1178. _dl_loaded_modules->dynamic_info[DT_PREINIT_ARRAYSZ],
  1179. _dl_loaded_modules->loadaddr);
  1180. /* Run initialization functions for loaded objects. For the
  1181. main executable, they will be run from __uClibc_main. */
  1182. for (i = nlist; i; --i) {
  1183. tpnt = init_fini_list[i-1];
  1184. tpnt->init_fini = NULL; /* Clear, since alloca was used */
  1185. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  1186. continue;
  1187. tpnt->init_flag |= INIT_FUNCS_CALLED;
  1188. if (tpnt->dynamic_info[DT_INIT]) {
  1189. void (*dl_elf_func) (void);
  1190. dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
  1191. _dl_if_debug_dprint("calling INIT: %s\n\n", tpnt->libname);
  1192. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  1193. }
  1194. _dl_run_init_array(tpnt);
  1195. }
  1196. /* Find the real malloc function and make ldso functions use that from now on */
  1197. _dl_malloc_function = (void* (*)(size_t)) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "malloc",
  1198. global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1199. #if defined(USE_TLS) && USE_TLS
  1200. /* Find the real functions and make ldso functions use them from now on */
  1201. _dl_calloc_function = (void* (*)(size_t, size_t)) (intptr_t)
  1202. _dl_find_hash(__C_SYMBOL_PREFIX__ "calloc", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1203. _dl_realloc_function = (void* (*)(void *, size_t)) (intptr_t)
  1204. _dl_find_hash(__C_SYMBOL_PREFIX__ "realloc", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1205. _dl_free_function = (void (*)(void *)) (intptr_t)
  1206. _dl_find_hash(__C_SYMBOL_PREFIX__ "free", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1207. _dl_memalign_function = (void* (*)(size_t, size_t)) (intptr_t)
  1208. _dl_find_hash(__C_SYMBOL_PREFIX__ "memalign", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1209. #endif
  1210. /* Notify the debugger that all objects are now mapped in. */
  1211. _dl_debug_addr->r_state = RT_CONSISTENT;
  1212. _dl_debug_state();
  1213. #ifdef __LDSO_STANDALONE_SUPPORT__
  1214. if (_start == (void *) auxvt[AT_ENTRY].a_un.a_val)
  1215. return (void *) app_tpnt->l_entry;
  1216. else
  1217. #endif
  1218. return (void *) auxvt[AT_ENTRY].a_un.a_val;
  1219. }
  1220. #include "dl-hash.c"
  1221. #include "dl-elf.c"