ldso.c 44 KB

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