ldso.c 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452
  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 ? __RTLD_SECURE : 0,
  518. &rpnt, _dl_progname);
  519. if (!app_tpnt) {
  520. _dl_dprintf(2, "can't load '%s'\n", _dl_progname);
  521. _dl_exit(16);
  522. }
  523. /*
  524. * FIXME: it needs to properly handle a PIE executable
  525. * Usually for a main application, loadaddr is computed as difference
  526. * between auxvt entry points and phdr, so if it is not 0, that it is a
  527. * PIE executable. In this case instead we need to set the loadaddr to 0
  528. * because we are actually mapping the ELF for the main application by
  529. * ourselves. So the PIE case must be checked.
  530. */
  531. app_tpnt->rtld_flags = unlazy | RTLD_GLOBAL;
  532. /*
  533. * This is used by gdb to locate the chain of shared libraries that are
  534. * currently loaded.
  535. */
  536. debug_addr = _dl_zalloc(sizeof(struct r_debug));
  537. ppnt = (ElfW(Phdr) *)app_tpnt->ppnt;
  538. for (i = 0; i < app_tpnt->n_phent; i++, ppnt++) {
  539. if (ppnt->p_type == PT_DYNAMIC) {
  540. dpnt = (ElfW(Dyn) *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  541. _dl_parse_dynamic_info(dpnt, app_tpnt->dynamic_info, debug_addr, app_tpnt->loadaddr);
  542. }
  543. }
  544. _dl_ldsopath_init(tpnt);
  545. } else {
  546. #endif
  547. /* At this point we are now free to examine the user application,
  548. * and figure out which libraries are supposed to be called. Until
  549. * we have this list, we will not be completely ready for dynamic
  550. * linking.
  551. */
  552. /* Find the runtime load address of the main executable. This may be
  553. * different from what the ELF header says for ET_DYN/PIE executables.
  554. */
  555. {
  556. unsigned int idx;
  557. ElfW(Phdr) *phdr = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  558. for (idx = 0; idx < auxvt[AT_PHNUM].a_un.a_val; idx++, phdr++)
  559. if (phdr->p_type == PT_PHDR) {
  560. DL_INIT_LOADADDR_PROG(app_tpnt->loadaddr, auxvt[AT_PHDR].a_un.a_val - phdr->p_vaddr);
  561. break;
  562. }
  563. if (DL_LOADADDR_BASE(app_tpnt->loadaddr))
  564. _dl_debug_early("Position Independent Executable: "
  565. "app_tpnt->loadaddr=%x\n", DL_LOADADDR_BASE(app_tpnt->loadaddr));
  566. }
  567. /*
  568. * This is used by gdb to locate the chain of shared libraries that are
  569. * currently loaded.
  570. */
  571. debug_addr = _dl_zalloc(sizeof(struct r_debug));
  572. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  573. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  574. if (ppnt->p_type == PT_GNU_RELRO) {
  575. relro_addr = ppnt->p_vaddr;
  576. relro_size = ppnt->p_memsz;
  577. }
  578. if (!app_mapaddr && (ppnt->p_type == PT_LOAD)) {
  579. app_mapaddr = DL_RELOC_ADDR (app_tpnt->loadaddr, ppnt->p_vaddr);
  580. }
  581. if (ppnt->p_type == PT_DYNAMIC) {
  582. dpnt = (ElfW(Dyn) *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  583. _dl_parse_dynamic_info(dpnt, app_tpnt->dynamic_info, debug_addr, app_tpnt->loadaddr);
  584. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  585. /* Ugly, ugly. We need to call mprotect to change the
  586. * protection of the text pages so that we can do the
  587. * dynamic linking. We can set the protection back
  588. * again once we are done.
  589. */
  590. _dl_debug_early("calling mprotect on the application program\n");
  591. /* Now cover the application program. */
  592. if (app_tpnt->dynamic_info[DT_TEXTREL]) {
  593. ElfW(Phdr) *ppnt_outer = ppnt;
  594. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  595. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  596. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  597. _dl_mprotect((void *) (DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr) & PAGE_ALIGN),
  598. (DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr) & ADDR_ALIGN) +
  599. (unsigned long) ppnt->p_filesz,
  600. PROT_READ | PROT_WRITE | PROT_EXEC);
  601. }
  602. ppnt = ppnt_outer;
  603. }
  604. #else
  605. if (app_tpnt->dynamic_info[DT_TEXTREL]) {
  606. _dl_dprintf(2, "Can't modify application's text section; use the GCC option -fPIE for position-independent executables.\n");
  607. _dl_exit(1);
  608. }
  609. #endif
  610. #ifndef ALLOW_ZERO_PLTGOT
  611. /* make sure it's really there. */
  612. if (app_tpnt->dynamic_info[DT_PLTGOT] == 0)
  613. continue;
  614. #endif
  615. /* OK, we have what we need - slip this one into the list. */
  616. app_tpnt = _dl_add_elf_hash_table(_dl_progname, app_tpnt->loadaddr,
  617. app_tpnt->dynamic_info,
  618. (unsigned long) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr),
  619. ppnt->p_filesz);
  620. _dl_loaded_modules->libtype = elf_executable;
  621. _dl_loaded_modules->ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_val;
  622. _dl_loaded_modules->n_phent = auxvt[AT_PHNUM].a_un.a_val;
  623. _dl_symbol_tables = rpnt = _dl_zalloc(sizeof(struct dyn_elf));
  624. rpnt->dyn = _dl_loaded_modules;
  625. app_tpnt->mapaddr = app_mapaddr;
  626. app_tpnt->rtld_flags = unlazy | RTLD_GLOBAL;
  627. app_tpnt->usage_count++;
  628. #ifdef __DSBT__
  629. _dl_ldso_dsbt[0] = app_tpnt->dsbt_table;
  630. _dl_memcpy(app_tpnt->dsbt_table, _dl_ldso_dsbt,
  631. app_tpnt->dsbt_size * sizeof(tpnt->dsbt_table[0]));
  632. #endif
  633. lpnt = (unsigned long *) (app_tpnt->dynamic_info[DT_PLTGOT]);
  634. #ifdef ALLOW_ZERO_PLTGOT
  635. if (lpnt)
  636. #endif
  637. INIT_GOT(lpnt, _dl_loaded_modules);
  638. }
  639. /* OK, fill this in - we did not have this before */
  640. if (ppnt->p_type == PT_INTERP) {
  641. tpnt->libname = (char *) DL_RELOC_ADDR(app_tpnt->loadaddr, ppnt->p_vaddr);
  642. _dl_ldsopath_init(tpnt);
  643. }
  644. /* Discover any TLS sections if the target supports them. */
  645. if (ppnt->p_type == PT_TLS) {
  646. #if defined(USE_TLS) && USE_TLS
  647. if (ppnt->p_memsz > 0) {
  648. app_tpnt->l_tls_blocksize = ppnt->p_memsz;
  649. app_tpnt->l_tls_align = ppnt->p_align;
  650. if (ppnt->p_align == 0)
  651. app_tpnt->l_tls_firstbyte_offset = 0;
  652. else
  653. app_tpnt->l_tls_firstbyte_offset =
  654. (ppnt->p_vaddr & (ppnt->p_align - 1));
  655. app_tpnt->l_tls_initimage_size = ppnt->p_filesz;
  656. app_tpnt->l_tls_initimage = (void *) ppnt->p_vaddr;
  657. /* This image gets the ID one. */
  658. _dl_tls_max_dtv_idx = app_tpnt->l_tls_modid = 1;
  659. }
  660. _dl_debug_early("Found TLS header for application program\n");
  661. break;
  662. #else
  663. _dl_dprintf(2, "Program uses unsupported TLS data!\n");
  664. _dl_exit(1);
  665. #endif
  666. }
  667. }
  668. app_tpnt->relro_addr = relro_addr;
  669. app_tpnt->relro_size = relro_size;
  670. #if defined(USE_TLS) && USE_TLS
  671. /*
  672. * Adjust the address of the TLS initialization image in
  673. * case the executable is actually an ET_DYN object.
  674. */
  675. if (app_tpnt->l_tls_initimage != NULL) {
  676. char *tmp attribute_unused =
  677. (char *) app_tpnt->l_tls_initimage;
  678. app_tpnt->l_tls_initimage =
  679. (char *) app_tpnt->l_tls_initimage + app_tpnt->loadaddr;
  680. _dl_debug_early("Relocated TLS initial image from %x to %x (size = %x)\n",
  681. tmp, app_tpnt->l_tls_initimage, app_tpnt->l_tls_initimage_size);
  682. }
  683. #endif
  684. #ifdef __LDSO_STANDALONE_SUPPORT__
  685. } /* ! ldso standalone mode */
  686. #endif
  687. #ifdef __SUPPORT_LD_DEBUG__
  688. _dl_debug = _dl_getenv("LD_DEBUG", envp);
  689. if (_dl_debug) {
  690. if (_dl_strstr(_dl_debug, "all")) {
  691. _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
  692. = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = (void*)1;
  693. } else {
  694. _dl_debug_detail = _dl_strstr(_dl_debug, "detail");
  695. _dl_debug_move = _dl_strstr(_dl_debug, "move");
  696. _dl_debug_symbols = _dl_strstr(_dl_debug, "sym");
  697. _dl_debug_reloc = _dl_strstr(_dl_debug, "reloc");
  698. _dl_debug_nofixups = _dl_strstr(_dl_debug, "nofix");
  699. _dl_debug_bindings = _dl_strstr(_dl_debug, "bind");
  700. }
  701. }
  702. {
  703. const char *dl_debug_output;
  704. dl_debug_output = _dl_getenv("LD_DEBUG_OUTPUT", envp);
  705. if (dl_debug_output) {
  706. char tmp[22], *tmp1, *filename;
  707. int len1, len2;
  708. _dl_memset(tmp, 0, sizeof(tmp));
  709. tmp1 = _dl_simple_ltoa( tmp, (unsigned long)_dl_getpid());
  710. len1 = _dl_strlen(dl_debug_output);
  711. len2 = _dl_strlen(tmp1);
  712. filename = _dl_malloc(len1 + len2 + 2);
  713. if (filename) {
  714. _dl_strcpy (filename, dl_debug_output);
  715. filename[len1] = '.';
  716. _dl_strcpy (&filename[len1+1], tmp1);
  717. _dl_debug_file = _dl_open(filename, O_WRONLY|O_CREAT, 0644);
  718. if (_dl_debug_file < 0) {
  719. _dl_debug_file = 2;
  720. _dl_dprintf(_dl_debug_file, "can't open file: '%s'\n",filename);
  721. }
  722. }
  723. }
  724. }
  725. #endif
  726. #ifdef __LDSO_PRELINK_SUPPORT__
  727. {
  728. char *ld_warn = _dl_getenv ("LD_WARN", envp);
  729. if (ld_warn && *ld_warn == '\0')
  730. _dl_verbose = false;
  731. }
  732. _dl_trace_prelink = _dl_getenv("LD_TRACE_PRELINKING", envp);
  733. #endif
  734. if (_dl_getenv("LD_TRACE_LOADED_OBJECTS", envp) != NULL) {
  735. trace_loaded_objects++;
  736. }
  737. #ifndef __LDSO_LDD_SUPPORT__
  738. if (trace_loaded_objects) {
  739. _dl_dprintf(2, "Use the ldd provided by uClibc\n");
  740. _dl_exit(1);
  741. }
  742. #endif
  743. ldso_mapaddr = (ElfW(Addr)) auxvt[AT_BASE].a_un.a_val;
  744. /*
  745. * OK, fix one more thing - set up debug_addr so it will point
  746. * to our chain. Later we may need to fill in more fields, but this
  747. * should be enough for now.
  748. */
  749. debug_addr->r_map = (struct link_map *) _dl_loaded_modules;
  750. debug_addr->r_version = 1;
  751. debug_addr->r_ldbase = (ElfW(Addr))
  752. DL_LOADADDR_BASE(DL_GET_RUN_ADDR(load_addr, ldso_mapaddr));
  753. debug_addr->r_brk = (unsigned long) &_dl_debug_state;
  754. _dl_debug_addr = debug_addr;
  755. /* Do not notify the debugger until the interpreter is in the list */
  756. /* OK, we now have the application in the list, and we have some
  757. * basic stuff in place. Now search through the list for other shared
  758. * libraries that should be loaded, and insert them on the list in the
  759. * correct order.
  760. */
  761. _dl_map_cache();
  762. #ifdef __LDSO_PRELOAD_ENV_SUPPORT__
  763. if (_dl_preload) {
  764. char c, *str, *str2;
  765. str = _dl_preload;
  766. while (*str == ':' || *str == ' ' || *str == '\t')
  767. str++;
  768. while (*str) {
  769. str2 = str;
  770. while (*str2 && *str2 != ':' && *str2 != ' ' && *str2 != '\t')
  771. str2++;
  772. c = *str2;
  773. *str2 = '\0';
  774. if (!_dl_secure || _dl_strchr(str, '/') == NULL) {
  775. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", str, _dl_progname);
  776. tpnt1 = _dl_load_shared_library(
  777. _dl_secure ? __RTLD_SECURE : 0,
  778. &rpnt, NULL, str, trace_loaded_objects);
  779. if (!tpnt1) {
  780. #ifdef __LDSO_LDD_SUPPORT__
  781. if (trace_loaded_objects || _dl_trace_prelink)
  782. _dl_dprintf(1, "\t%s => not found\n", str);
  783. else
  784. #endif
  785. {
  786. _dl_dprintf(2, "%s: library '%s' "
  787. "from LD_PRELOAD can't be preloaded: ignored.\n",
  788. _dl_progname, str);
  789. }
  790. } else {
  791. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  792. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  793. #ifdef __LDSO_LDD_SUPPORT__
  794. if (trace_loaded_objects && !_dl_trace_prelink &&
  795. tpnt1->usage_count == 1) {
  796. /* This is a real hack to make
  797. * ldd not print the library
  798. * itself when run on a
  799. * library.
  800. */
  801. if (_dl_strcmp(_dl_progname, str) != 0)
  802. _dl_dprintf(1, "\t%s => %s (%x)\n", str, tpnt1->libname,
  803. DL_LOADADDR_BASE(tpnt1->loadaddr));
  804. }
  805. #endif
  806. }
  807. }
  808. *str2 = c;
  809. str = str2;
  810. while (*str == ':' || *str == ' ' || *str == '\t')
  811. str++;
  812. }
  813. }
  814. #endif /* __LDSO_PRELOAD_ENV_SUPPORT__ */
  815. #ifdef __LDSO_PRELOAD_FILE_SUPPORT__
  816. do {
  817. char *preload;
  818. int fd;
  819. char c, *cp, *cp2;
  820. struct stat st;
  821. if (_dl_stat(LDSO_PRELOAD, &st) || st.st_size == 0) {
  822. break;
  823. }
  824. if ((fd = _dl_open(LDSO_PRELOAD, O_RDONLY, 0)) < 0) {
  825. _dl_dprintf(2, "%s: can't open file '%s'\n",
  826. _dl_progname, LDSO_PRELOAD);
  827. break;
  828. }
  829. preload = (caddr_t) _dl_mmap(0, st.st_size + 1,
  830. PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  831. _dl_close(fd);
  832. if (preload == (caddr_t) -1) {
  833. _dl_dprintf(2, "%s:%i: can't map '%s'\n",
  834. _dl_progname, __LINE__, LDSO_PRELOAD);
  835. break;
  836. }
  837. /* convert all separators and comments to spaces */
  838. for (cp = preload; *cp; /*nada */ ) {
  839. if (*cp == ':' || *cp == '\t' || *cp == '\n') {
  840. *cp++ = ' ';
  841. } else if (*cp == '#') {
  842. do {
  843. *cp++ = ' ';
  844. } while (*cp != '\n' && *cp != '\0');
  845. } else {
  846. cp++;
  847. }
  848. }
  849. /* find start of first library */
  850. for (cp = preload; *cp && *cp == ' '; cp++)
  851. /*nada */ ;
  852. while (*cp) {
  853. /* find end of library */
  854. for (cp2 = cp; *cp && *cp != ' '; cp++)
  855. /*nada */ ;
  856. c = *cp;
  857. *cp = '\0';
  858. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", cp2, _dl_progname);
  859. tpnt1 = _dl_load_shared_library(0, &rpnt, NULL, cp2, trace_loaded_objects);
  860. if (!tpnt1) {
  861. # ifdef __LDSO_LDD_SUPPORT__
  862. if (trace_loaded_objects || _dl_trace_prelink)
  863. _dl_dprintf(1, "\t%s => not found\n", cp2);
  864. else
  865. # endif
  866. {
  867. _dl_dprintf(2, "%s: can't load library '%s'\n", _dl_progname, cp2);
  868. _dl_exit(15);
  869. }
  870. } else {
  871. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  872. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  873. # ifdef __LDSO_LDD_SUPPORT__
  874. if (trace_loaded_objects && !_dl_trace_prelink &&
  875. tpnt1->usage_count == 1) {
  876. _dl_dprintf(1, "\t%s => %s (%x)\n",
  877. cp2, tpnt1->libname,
  878. DL_LOADADDR_BASE(tpnt1->loadaddr));
  879. }
  880. # endif
  881. }
  882. /* find start of next library */
  883. *cp = c;
  884. for ( /*nada */ ; *cp && *cp == ' '; cp++)
  885. /*nada */ ;
  886. }
  887. _dl_munmap(preload, st.st_size + 1);
  888. } while (0);
  889. #endif /* __LDSO_PRELOAD_FILE_SUPPORT__ */
  890. nlist = 0;
  891. for (tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next) {
  892. ElfW(Dyn) *this_dpnt;
  893. nlist++;
  894. for (this_dpnt = (ElfW(Dyn) *) tcurr->dynamic_addr; this_dpnt->d_tag; this_dpnt++) {
  895. if (this_dpnt->d_tag == DT_NEEDED) {
  896. char *name;
  897. struct init_fini_list *tmp;
  898. lpntstr = (char*) (tcurr->dynamic_info[DT_STRTAB] + this_dpnt->d_un.d_val);
  899. name = _dl_get_last_path_component(lpntstr);
  900. _dl_if_debug_dprint("\tfile='%s'; needed by '%s'\n", lpntstr, _dl_progname);
  901. if (_dl_strcmp(name, UCLIBC_LDSO) == 0) {
  902. if (!ldso_tpnt) {
  903. /* Insert the ld.so only once */
  904. ldso_tpnt = add_ldso(tpnt, load_addr,
  905. ldso_mapaddr, auxvt, rpnt);
  906. }
  907. ldso_tpnt->usage_count++;
  908. tpnt1 = ldso_tpnt;
  909. } else
  910. tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr, trace_loaded_objects);
  911. if (!tpnt1) {
  912. #ifdef __LDSO_LDD_SUPPORT__
  913. if (trace_loaded_objects || _dl_trace_prelink) {
  914. _dl_dprintf(1, "\t%s => not found\n", lpntstr);
  915. continue;
  916. } else
  917. #endif
  918. {
  919. _dl_dprintf(2, "%s: can't load library '%s'\n", _dl_progname, lpntstr);
  920. _dl_exit(16);
  921. }
  922. }
  923. tmp = alloca(sizeof(struct init_fini_list)); /* Allocates on stack, no need to free this memory */
  924. tmp->tpnt = tpnt1;
  925. tmp->next = tcurr->init_fini;
  926. tcurr->init_fini = tmp;
  927. tpnt1->rtld_flags = unlazy | RTLD_GLOBAL;
  928. _dl_debug_early("Loading: (%x) %s\n", DL_LOADADDR_BASE(tpnt1->loadaddr), tpnt1->libname);
  929. #ifdef __LDSO_LDD_SUPPORT__
  930. if (trace_loaded_objects && !_dl_trace_prelink &&
  931. tpnt1->usage_count == 1) {
  932. _dl_dprintf(1, "\t%s => %s (%x)\n",
  933. lpntstr, tpnt1->libname,
  934. DL_LOADADDR_BASE(tpnt1->loadaddr));
  935. }
  936. #endif
  937. }
  938. }
  939. }
  940. _dl_unmap_cache();
  941. /* Keep track of the number of elements in the global scope */
  942. nscope_elem = nlist;
  943. if (_dl_loaded_modules->libtype == elf_executable) {
  944. --nlist; /* Exclude the application. */
  945. tcurr = _dl_loaded_modules->next;
  946. } else
  947. tcurr = _dl_loaded_modules;
  948. init_fini_list = _dl_malloc(nlist * sizeof(struct elf_resolve *));
  949. i = 0;
  950. for (; tcurr; tcurr = tcurr->next)
  951. init_fini_list[i++] = tcurr;
  952. /* Sort the INIT/FINI list in dependency order. */
  953. for (tcurr = _dl_loaded_modules->next; tcurr; tcurr = tcurr->next) {
  954. unsigned int j, k;
  955. for (j = 0; init_fini_list[j] != tcurr; ++j)
  956. /* Empty */;
  957. for (k = j + 1; k < nlist; ++k) {
  958. struct init_fini_list *runp = init_fini_list[k]->init_fini;
  959. for (; runp; runp = runp->next) {
  960. if (runp->tpnt == tcurr) {
  961. struct elf_resolve *here = init_fini_list[k];
  962. _dl_if_debug_dprint("Move %s from pos %d to %d in INIT/FINI list\n", here->libname, k, j);
  963. for (i = (k - j); i; --i)
  964. init_fini_list[i+j] = init_fini_list[i+j-1];
  965. init_fini_list[j] = here;
  966. ++j;
  967. break;
  968. }
  969. }
  970. }
  971. }
  972. #ifdef __SUPPORT_LD_DEBUG__
  973. if (_dl_debug) {
  974. _dl_dprintf(_dl_debug_file, "\nINIT/FINI order and dependencies:\n");
  975. for (i = 0; i < nlist; i++) {
  976. struct init_fini_list *tmp;
  977. _dl_dprintf(_dl_debug_file, "lib: %s has deps:\n",
  978. init_fini_list[i]->libname);
  979. tmp = init_fini_list[i]->init_fini;
  980. for (; tmp; tmp = tmp->next)
  981. _dl_dprintf(_dl_debug_file, " %s ", tmp->tpnt->libname);
  982. _dl_dprintf(_dl_debug_file, "\n");
  983. }
  984. }
  985. #endif
  986. /*
  987. * If the program interpreter is not in the module chain, add it.
  988. * This will be required for dlopen to be able to access the internal
  989. * functions in the dynamic linker and to relocate the interpreter
  990. * again once all libs are loaded.
  991. */
  992. if (!ldso_tpnt) {
  993. tpnt = add_ldso(tpnt, load_addr, ldso_mapaddr, auxvt, rpnt);
  994. tpnt->usage_count++;
  995. nscope_elem++;
  996. } else
  997. tpnt = ldso_tpnt;
  998. #ifdef RERELOCATE_LDSO
  999. /* Only rerelocate functions for now. */
  1000. tpnt->init_flag = RELOCS_DONE;
  1001. lpnt = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT]);
  1002. # ifdef ALLOW_ZERO_PLTGOT
  1003. if (tpnt->dynamic_info[DT_PLTGOT])
  1004. # endif
  1005. INIT_GOT(lpnt, tpnt);
  1006. #else
  1007. tpnt->init_flag = RELOCS_DONE | JMP_RELOCS_DONE;
  1008. #endif
  1009. tpnt = NULL;
  1010. /*
  1011. * Allocate the global scope array.
  1012. */
  1013. scope_elem_list = (struct elf_resolve **) _dl_malloc(nscope_elem * sizeof(struct elf_resolve *));
  1014. for (i = 0, tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next)
  1015. scope_elem_list[i++] = tcurr;
  1016. _dl_loaded_modules->symbol_scope.r_list = scope_elem_list;
  1017. _dl_loaded_modules->symbol_scope.r_nlist = nscope_elem;
  1018. /*
  1019. * The symbol scope of the application, that is the first entry of the
  1020. * _dl_loaded_modules list, is just the global scope to be used for the
  1021. * symbol lookup.
  1022. */
  1023. global_scope = &_dl_loaded_modules->symbol_scope;
  1024. /* Build the local scope for each loaded modules. */
  1025. local_scope = _dl_malloc(nscope_elem * sizeof(struct elf_resolve *));
  1026. i = 1;
  1027. for (tcurr = _dl_loaded_modules->next; tcurr; tcurr = tcurr->next) {
  1028. unsigned int k;
  1029. cnt = _dl_build_local_scope(local_scope, scope_elem_list[i++]);
  1030. tcurr->symbol_scope.r_list = _dl_malloc(cnt * sizeof(struct elf_resolve *));
  1031. tcurr->symbol_scope.r_nlist = cnt;
  1032. _dl_memcpy (tcurr->symbol_scope.r_list, local_scope, cnt * sizeof (struct elf_resolve *));
  1033. /* Restoring the init_flag.*/
  1034. for (k = 1; k < nscope_elem; k++)
  1035. scope_elem_list[k]->init_flag &= ~DL_RESERVED;
  1036. }
  1037. _dl_free(local_scope);
  1038. #ifdef __LDSO_LDD_SUPPORT__
  1039. /* Exit if LD_TRACE_LOADED_OBJECTS is on. */
  1040. if (trace_loaded_objects && !_dl_trace_prelink)
  1041. _dl_exit(0);
  1042. #endif
  1043. #if defined(USE_TLS) && USE_TLS
  1044. /* We do not initialize any of the TLS functionality unless any of the
  1045. * initial modules uses TLS. This makes dynamic loading of modules with
  1046. * TLS impossible, but to support it requires either eagerly doing setup
  1047. * now or lazily doing it later. Doing it now makes us incompatible with
  1048. * an old kernel that can't perform TLS_INIT_TP, even if no TLS is ever
  1049. * used. Trying to do it lazily is too hairy to try when there could be
  1050. * multiple threads (from a non-TLS-using libpthread). */
  1051. bool was_tls_init_tp_called = tls_init_tp_called;
  1052. if (tcbp == NULL) {
  1053. _dl_debug_early("Calling init_tls()!\n");
  1054. tcbp = init_tls ();
  1055. }
  1056. #endif
  1057. #ifdef __UCLIBC_HAS_SSP__
  1058. _dl_debug_early("Setting up SSP guards\n");
  1059. /* Set up the stack checker's canary. */
  1060. stack_chk_guard = _dl_setup_stack_chk_guard ();
  1061. # ifdef THREAD_SET_STACK_GUARD
  1062. THREAD_SET_STACK_GUARD (stack_chk_guard);
  1063. # else
  1064. __stack_chk_guard = stack_chk_guard;
  1065. # endif
  1066. # ifdef __UCLIBC_HAS_SSP_COMPAT__
  1067. __guard = stack_chk_guard;
  1068. # endif
  1069. #endif
  1070. #ifdef __LDSO_PRELINK_SUPPORT__
  1071. if (_dl_trace_prelink) {
  1072. unsigned int nscope_trace = ldso_tpnt ? nscope_elem : (nscope_elem - 1);
  1073. for (i = 0; i < nscope_trace; i++)
  1074. trace_objects(scope_elem_list[i],
  1075. _dl_get_last_path_component(scope_elem_list[i]->libname));
  1076. if (_dl_verbose)
  1077. /* Warn about undefined symbols. */
  1078. if (_dl_symbol_tables)
  1079. if (_dl_fixup(_dl_symbol_tables, global_scope, unlazy))
  1080. _dl_exit(-1);
  1081. _dl_exit(0);
  1082. }
  1083. if (_dl_loaded_modules->dynamic_info[DT_GNU_LIBLIST_IDX]) {
  1084. ElfW(Lib) *liblist, *liblistend;
  1085. struct elf_resolve **r_list, **r_listend, *l;
  1086. const char *strtab = (const char *)_dl_loaded_modules->dynamic_info[DT_STRTAB];
  1087. _dl_assert (_dl_loaded_modules->dynamic_info[DT_GNU_LIBLISTSZ_IDX] != 0);
  1088. liblist = (ElfW(Lib) *) _dl_loaded_modules->dynamic_info[DT_GNU_LIBLIST_IDX];
  1089. liblistend = (ElfW(Lib) *)
  1090. ((char *) liblist + _dl_loaded_modules->dynamic_info[DT_GNU_LIBLISTSZ_IDX]);
  1091. r_list = _dl_loaded_modules->symbol_scope.r_list;
  1092. r_listend = r_list + nscope_elem;
  1093. for (; r_list < r_listend && liblist < liblistend; r_list++) {
  1094. l = *r_list;
  1095. if (l == _dl_loaded_modules)
  1096. continue;
  1097. /* If the library is not mapped where it should, fail. */
  1098. if (l->loadaddr)
  1099. break;
  1100. /* Next, check if checksum matches. */
  1101. if (l->dynamic_info[DT_CHECKSUM_IDX] == 0 ||
  1102. l->dynamic_info[DT_CHECKSUM_IDX] != liblist->l_checksum)
  1103. break;
  1104. if (l->dynamic_info[DT_GNU_PRELINKED_IDX] == 0 ||
  1105. (l->dynamic_info[DT_GNU_PRELINKED_IDX] != liblist->l_time_stamp))
  1106. break;
  1107. if (_dl_strcmp(strtab + liblist->l_name, _dl_get_last_path_component(l->libname)) != 0)
  1108. break;
  1109. ++liblist;
  1110. }
  1111. if (r_list == r_listend && liblist == liblistend)
  1112. prelinked = true;
  1113. }
  1114. _dl_debug_early ("prelink checking: %s\n", prelinked ? "ok" : "failed");
  1115. if (prelinked) {
  1116. if (_dl_loaded_modules->dynamic_info[DT_GNU_CONFLICT_IDX]) {
  1117. ELF_RELOC *conflict;
  1118. unsigned long conflict_size;
  1119. _dl_assert (_dl_loaded_modules->dynamic_info[DT_GNU_CONFLICTSZ_IDX] != 0);
  1120. conflict = (ELF_RELOC *) _dl_loaded_modules->dynamic_info[DT_GNU_CONFLICT_IDX];
  1121. conflict_size = _dl_loaded_modules->dynamic_info[DT_GNU_CONFLICTSZ_IDX];
  1122. _dl_parse_relocation_information(_dl_symbol_tables, global_scope,
  1123. (unsigned long) conflict, conflict_size);
  1124. }
  1125. /* Mark all the objects so we know they have been already relocated. */
  1126. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1127. tpnt->init_flag |= RELOCS_DONE;
  1128. if (tpnt->relro_size)
  1129. _dl_protect_relro (tpnt);
  1130. }
  1131. } else
  1132. #endif
  1133. {
  1134. _dl_debug_early("Beginning relocation fixups\n");
  1135. #ifdef __mips__
  1136. /*
  1137. * Relocation of the GOT entries for MIPS have to be done
  1138. * after all the libraries have been loaded.
  1139. */
  1140. _dl_perform_mips_global_got_relocations(_dl_loaded_modules, !unlazy);
  1141. #endif
  1142. /*
  1143. * OK, now all of the kids are tucked into bed in their proper
  1144. * addresses. Now we go through and look for REL and RELA records that
  1145. * indicate fixups to the GOT tables. We need to do this in reverse
  1146. * order so that COPY directives work correctly.
  1147. */
  1148. if (_dl_symbol_tables)
  1149. if (_dl_fixup(_dl_symbol_tables, global_scope, unlazy))
  1150. _dl_exit(-1);
  1151. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1152. if (tpnt->relro_size)
  1153. _dl_protect_relro (tpnt);
  1154. }
  1155. } /* not prelinked */
  1156. #if defined(USE_TLS) && USE_TLS
  1157. if (!was_tls_init_tp_called && _dl_tls_max_dtv_idx > 0)
  1158. ++_dl_tls_generation;
  1159. _dl_debug_early("Calling _dl_allocate_tls_init()!\n");
  1160. /* Now that we have completed relocation, the initializer data
  1161. for the TLS blocks has its final values and we can copy them
  1162. into the main thread's TLS area, which we allocated above. */
  1163. _dl_allocate_tls_init (tcbp);
  1164. /* And finally install it for the main thread. If ld.so itself uses
  1165. TLS we know the thread pointer was initialized earlier. */
  1166. if (! tls_init_tp_called) {
  1167. const char *lossage = (char *) TLS_INIT_TP (tcbp, USE___THREAD);
  1168. if (__builtin_expect (lossage != NULL, 0)) {
  1169. _dl_debug_early("cannot set up thread-local storage: %s\n", lossage);
  1170. _dl_exit(30);
  1171. }
  1172. }
  1173. #endif /* USE_TLS */
  1174. /* OK, at this point things are pretty much ready to run. Now we need
  1175. * to touch up a few items that are required, and then we can let the
  1176. * user application have at it. Note that the dynamic linker itself
  1177. * is not guaranteed to be fully dynamicly linked if we are using
  1178. * ld.so.1, so we have to look up each symbol individually.
  1179. */
  1180. _dl_envp = (unsigned long *) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "__environ", global_scope, NULL, 0, NULL);
  1181. if (_dl_envp)
  1182. *_dl_envp = (unsigned long) envp;
  1183. #ifndef __FORCE_SHAREABLE_TEXT_SEGMENTS__
  1184. {
  1185. unsigned int j;
  1186. ElfW(Phdr) *myppnt;
  1187. /* We had to set the protections of all pages to R/W for
  1188. * dynamic linking. Set text pages back to R/O.
  1189. */
  1190. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1191. for (myppnt = tpnt->ppnt, j = 0; j < tpnt->n_phent; j++, myppnt++) {
  1192. if (myppnt->p_type == PT_LOAD && !(myppnt->p_flags & PF_W) && tpnt->dynamic_info[DT_TEXTREL]) {
  1193. _dl_mprotect((void *) (DL_RELOC_ADDR(tpnt->loadaddr, myppnt->p_vaddr) & PAGE_ALIGN),
  1194. (myppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) myppnt->p_filesz, LXFLAGS(myppnt->p_flags));
  1195. }
  1196. }
  1197. }
  1198. }
  1199. #endif
  1200. /* Notify the debugger we have added some objects. */
  1201. _dl_debug_addr->r_state = RT_ADD;
  1202. _dl_debug_state();
  1203. /* Run pre-initialization functions for the executable. */
  1204. _dl_run_array_forward(_dl_loaded_modules->dynamic_info[DT_PREINIT_ARRAY],
  1205. _dl_loaded_modules->dynamic_info[DT_PREINIT_ARRAYSZ],
  1206. _dl_loaded_modules->loadaddr);
  1207. /* Run initialization functions for loaded objects. For the
  1208. main executable, they will be run from __uClibc_main. */
  1209. for (i = nlist; i; --i) {
  1210. tpnt = init_fini_list[i-1];
  1211. tpnt->init_fini = NULL; /* Clear, since alloca was used */
  1212. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  1213. continue;
  1214. tpnt->init_flag |= INIT_FUNCS_CALLED;
  1215. if (tpnt->dynamic_info[DT_INIT]) {
  1216. void (*dl_elf_func) (void);
  1217. dl_elf_func = (void (*)(void)) DL_RELOC_ADDR(tpnt->loadaddr, tpnt->dynamic_info[DT_INIT]);
  1218. _dl_if_debug_dprint("calling INIT: %s\n\n", tpnt->libname);
  1219. DL_CALL_FUNC_AT_ADDR (dl_elf_func, tpnt->loadaddr, (void(*)(void)));
  1220. }
  1221. _dl_run_init_array(tpnt);
  1222. }
  1223. /* Find the real malloc function and make ldso functions use that from now on */
  1224. _dl_malloc_function = (void* (*)(size_t)) (intptr_t) _dl_find_hash(__C_SYMBOL_PREFIX__ "malloc",
  1225. global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1226. #if defined(USE_TLS) && USE_TLS
  1227. /* Find the real functions and make ldso functions use them from now on */
  1228. _dl_calloc_function = (void* (*)(size_t, size_t)) (intptr_t)
  1229. _dl_find_hash(__C_SYMBOL_PREFIX__ "calloc", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1230. _dl_realloc_function = (void* (*)(void *, size_t)) (intptr_t)
  1231. _dl_find_hash(__C_SYMBOL_PREFIX__ "realloc", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1232. _dl_free_function = (void (*)(void *)) (intptr_t)
  1233. _dl_find_hash(__C_SYMBOL_PREFIX__ "free", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1234. _dl_memalign_function = (void* (*)(size_t, size_t)) (intptr_t)
  1235. _dl_find_hash(__C_SYMBOL_PREFIX__ "memalign", global_scope, NULL, ELF_RTYPE_CLASS_PLT, NULL);
  1236. #endif
  1237. /* Notify the debugger that all objects are now mapped in. */
  1238. _dl_debug_addr->r_state = RT_CONSISTENT;
  1239. _dl_debug_state();
  1240. #ifdef __LDSO_STANDALONE_SUPPORT__
  1241. if (_start == (void *) auxvt[AT_ENTRY].a_un.a_val)
  1242. return (void *) app_tpnt->l_entry;
  1243. else
  1244. #endif
  1245. return (void *) auxvt[AT_ENTRY].a_un.a_val;
  1246. }
  1247. #include "dl-hash.c"
  1248. #include "dl-elf.c"