ldso.c 43 KB

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