ldso.c 43 KB

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