ldso.c 44 KB

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