ldso.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /* vi: set sw=4 ts=4: */
  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) 1994-2000 Eric Youngdale, Peter MacDonald,
  6. * David Engel, Hongjiu Lu and Mitch D'Souza
  7. * Copyright (C) 2001-2002, Erik Andersen
  8. *
  9. * All rights reserved.
  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. // Support a list of library preloads in /etc/ld.so.preload
  33. //#define SUPPORT_LDSO_PRELOAD_FILE
  34. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  35. I ever taken any courses on internals. This program was developed using
  36. information available through the book "UNIX SYSTEM V RELEASE 4,
  37. Programmers guide: Ansi C and Programming Support Tools", which did
  38. a more than adequate job of explaining everything required to get this
  39. working. */
  40. /*
  41. * The main trick with this program is that initially, we ourselves are
  42. * not dynamicly linked. This means that we cannot access any global
  43. * variables or call any functions. No globals initially, since the
  44. * Global Offset Table (GOT) is initialized by the linker assuming a
  45. * virtual address of 0, and no function calls initially since the
  46. * Procedure Linkage Table (PLT) is not yet initialized.
  47. *
  48. * There are additional initial restrictions - we cannot use large
  49. * switch statements, since the compiler generates tables of addresses
  50. * and jumps through them. We can use inline functions, because these
  51. * do not transfer control to a new address, but they must be static so
  52. * that they are not exported from the modules. We cannot use normal
  53. * syscall stubs, because these all reference the errno global variable
  54. * which is not yet initialized. We can use all of the local stack
  55. * variables that we want.
  56. *
  57. * Life is further complicated by the fact that initially we do not
  58. * want to do a complete dynamic linking. We want to allow the user to
  59. * supply new functions to override symbols (i.e. weak symbols and/or
  60. * LD_PRELOAD). So initially, we only perform relocations for
  61. * variables that start with "_dl_" since ANSI specifies that the user
  62. * is not supposed to redefine any of these variables.
  63. *
  64. * Fortunately, the linker itself leaves a few clues lying around, and
  65. * when the kernel starts the image, there are a few further clues.
  66. * First of all, there is Auxiliary Vector Table information sitting on
  67. * which is provided to us by the kernel, and which includes
  68. * information about the load address that the program interpreter was
  69. * loaded at, the number of sections, the address the application was
  70. * loaded at and so forth. Here this information is stored in the
  71. * array auxvt. For details see linux/fs/binfmt_elf.c where it calls
  72. * NEW_AUX_ENT() a bunch of time....
  73. *
  74. * Next, we need to find the GOT. On most arches there is a register
  75. * pointing to the GOT, but just in case (and for new ports) I've added
  76. * some (slow) C code to locate the GOT for you.
  77. *
  78. * This code was originally written for SVr4, and there the kernel
  79. * would load all text pages R/O, so they needed to call mprotect a
  80. * zillion times to mark all text pages as writable so dynamic linking
  81. * would succeed. Then when they were done, they would change the
  82. * protections for all the pages back again. Well, under Linux
  83. * everything is loaded writable (since Linux does copy on write
  84. * anyways) so all the mprotect stuff has been disabled.
  85. *
  86. * Initially, we do not have access to _dl_malloc since we can't yet
  87. * make function calls, so we mmap one page to use as scratch space.
  88. * Later on, when we can call _dl_malloc we reuse this this memory.
  89. * This is also beneficial, since we do not want to use the same memory
  90. * pool as malloc anyway - esp if the user redefines malloc to do
  91. * something funky.
  92. *
  93. * Our first task is to perform a minimal linking so that we can call
  94. * other portions of the dynamic linker. Once we have done this, we
  95. * then build the list of modules that the application requires, using
  96. * LD_LIBRARY_PATH if this is not a suid program (/usr/lib otherwise).
  97. * Once this is done, we can do the dynamic linking as required, and we
  98. * must omit the things we did to get the dynamic linker up and running
  99. * in the first place. After we have done this, we just have a few
  100. * housekeeping chores and we can transfer control to the user's
  101. * application.
  102. */
  103. #include "ldso.h"
  104. #define ALLOW_ZERO_PLTGOT
  105. /* Some arches may need to override this in boot1_arch.h */
  106. #define ELFMAGIC ELFMAG
  107. /* This is a poor man's malloc, used prior to resolving our internal poor man's malloc */
  108. #define LD_MALLOC(SIZE) ((void *) (malloc_buffer += SIZE, malloc_buffer - SIZE)) ; REALIGN();
  109. /*
  110. * Make sure that the malloc buffer is aligned on 4 byte boundary. For 64 bit
  111. * platforms we may need to increase this to 8, but this is good enough for
  112. * now. This is typically called after LD_MALLOC.
  113. */
  114. #define REALIGN() malloc_buffer = (char *) (((unsigned long) malloc_buffer + 3) & ~(3))
  115. char *_dl_library_path = 0; /* Where we look for libraries */
  116. char *_dl_preload = 0; /* Things to be loaded before the libs. */
  117. char *_dl_ldsopath = 0;
  118. static int _dl_be_lazy = 1;
  119. #ifdef __SUPPORT_LD_DEBUG__
  120. static char *_dl_debug = 0;
  121. static char *_dl_debug_symbols = 0;
  122. static char *_dl_debug_move = 0;
  123. static char *_dl_debug_reloc = 0;
  124. static char *_dl_debug_detail = 0;
  125. static char *_dl_debug_nofixups = 0;
  126. static char *_dl_debug_bindings = 0;
  127. static int _dl_debug_file = 2;
  128. #else
  129. #define _dl_debug_file 2
  130. #endif
  131. static char *_dl_malloc_addr, *_dl_mmap_zero;
  132. static char *_dl_trace_loaded_objects = 0;
  133. static int (*_dl_elf_main) (int, char **, char **);
  134. static int (*_dl_elf_init) (void);
  135. struct r_debug *_dl_debug_addr = NULL;
  136. unsigned long *_dl_brkp;
  137. unsigned long *_dl_envp;
  138. int _dl_fixup(struct elf_resolve *tpnt, int lazy);
  139. void _dl_debug_state(void);
  140. char *_dl_get_last_path_component(char *path);
  141. static void _dl_get_ready_to_run(struct elf_resolve *tpnt, struct elf_resolve *app_tpnt,
  142. unsigned long load_addr, unsigned long *hash_addr, Elf32_auxv_t auxvt[AT_EGID + 1],
  143. char **envp, struct r_debug *debug_addr);
  144. #include "boot1_arch.h"
  145. #include "_dl_progname.h" /* Pull in the value of _dl_progname */
  146. /* When we enter this piece of code, the program stack looks like this:
  147. argc argument counter (integer)
  148. argv[0] program name (pointer)
  149. argv[1...N] program args (pointers)
  150. argv[argc-1] end of args (integer)
  151. NULL
  152. env[0...N] environment variables (pointers)
  153. NULL
  154. auxvt[0...N] Auxiliary Vector Table elements (mixed types)
  155. */
  156. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  157. /* Debugging is especially tricky on PowerPC, since string literals
  158. * require relocations. Thus, you can't use _dl_dprintf() for
  159. * anything until the bootstrap relocations are finished. */
  160. static inline void hexprint(unsigned long x)
  161. {
  162. int i;
  163. char c;
  164. for (i = 0; i < 8; i++) {
  165. c = ((x >> 28) + '0');
  166. if (c > '9')
  167. c += 'a' - '9' - 1;
  168. _dl_write(1, &c, 1);
  169. x <<= 4;
  170. }
  171. c = '\n';
  172. _dl_write(1, &c, 1);
  173. }
  174. #endif
  175. LD_BOOT(unsigned long args) __attribute__ ((unused));
  176. LD_BOOT(unsigned long args)
  177. {
  178. unsigned int argc;
  179. char **argv, **envp;
  180. unsigned long load_addr;
  181. unsigned long *got;
  182. unsigned long *aux_dat;
  183. int goof = 0;
  184. ElfW(Ehdr) *header;
  185. struct elf_resolve *tpnt;
  186. struct elf_resolve *app_tpnt;
  187. Elf32_auxv_t auxvt[AT_EGID + 1];
  188. unsigned char *malloc_buffer, *mmap_zero;
  189. Elf32_Dyn *dpnt;
  190. unsigned long *hash_addr;
  191. struct r_debug *debug_addr = NULL;
  192. int indx;
  193. int status;
  194. /* WARNING! -- we cannot make _any_ funtion calls until we have
  195. * taken care of fixing up our own relocations. Making static
  196. * inline calls is ok, but _no_ function calls. Not yet
  197. * anyways. */
  198. /* First obtain the information on the stack that tells us more about
  199. what binary is loaded, where it is loaded, etc, etc */
  200. GET_ARGV(aux_dat, args);
  201. #if defined (__arm__) || defined (__mips__)
  202. aux_dat += 1;
  203. #endif
  204. argc = *(aux_dat - 1);
  205. argv = (char **) aux_dat;
  206. aux_dat += argc; /* Skip over the argv pointers */
  207. aux_dat++; /* Skip over NULL at end of argv */
  208. envp = (char **) aux_dat;
  209. while (*aux_dat)
  210. aux_dat++; /* Skip over the envp pointers */
  211. aux_dat++; /* Skip over NULL at end of envp */
  212. /* Place -1 here as a checkpoint. We later check if it was changed
  213. * when we read in the auxvt */
  214. auxvt[AT_UID].a_type = -1;
  215. /* The junk on the stack immediately following the environment is
  216. * the Auxiliary Vector Table. Read out the elements of the auxvt,
  217. * sort and store them in auxvt for later use. */
  218. while (*aux_dat) {
  219. Elf32_auxv_t *auxv_entry = (Elf32_auxv_t *) aux_dat;
  220. if (auxv_entry->a_type <= AT_EGID) {
  221. _dl_memcpy(&(auxvt[auxv_entry->a_type]), auxv_entry, sizeof(Elf32_auxv_t));
  222. }
  223. aux_dat += 2;
  224. }
  225. /* locate the ELF header. We need this done as soon as possible
  226. * (esp since SEND_STDERR() needs this on some platforms... */
  227. load_addr = auxvt[AT_BASE].a_un.a_val;
  228. header = (ElfW(Ehdr) *) auxvt[AT_BASE].a_un.a_ptr;
  229. /* Check the ELF header to make sure everything looks ok. */
  230. if (!header || header->e_ident[EI_CLASS] != ELFCLASS32 ||
  231. header->e_ident[EI_VERSION] != EV_CURRENT
  232. #if !defined(__powerpc__) && !defined(__mips__) && !defined(__sh__)
  233. || _dl_strncmp((void *) header, ELFMAGIC, SELFMAG) != 0
  234. #else
  235. || header->e_ident[EI_MAG0] != ELFMAG0
  236. || header->e_ident[EI_MAG1] != ELFMAG1
  237. || header->e_ident[EI_MAG2] != ELFMAG2
  238. || header->e_ident[EI_MAG3] != ELFMAG3
  239. #endif
  240. ) {
  241. SEND_STDERR("Invalid ELF header\n");
  242. _dl_exit(0);
  243. }
  244. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  245. SEND_STDERR("ELF header=");
  246. SEND_ADDRESS_STDERR(load_addr, 1);
  247. #endif
  248. /* Locate the global offset table. Since this code must be PIC
  249. * we can take advantage of the magic offset register, if we
  250. * happen to know what that is for this architecture. If not,
  251. * we can always read stuff out of the ELF file to find it... */
  252. #if defined(__i386__)
  253. __asm__("\tmovl %%ebx,%0\n\t":"=a"(got));
  254. #elif defined(__m68k__)
  255. __asm__("movel %%a5,%0":"=g"(got))
  256. #elif defined(__sparc__)
  257. __asm__("\tmov %%l7,%0\n\t":"=r"(got))
  258. #elif defined(__arm__)
  259. __asm__("\tmov %0, r10\n\t":"=r"(got));
  260. #elif defined(__powerpc__)
  261. __asm__("\tbl _GLOBAL_OFFSET_TABLE_-4@local\n\t":"=l"(got));
  262. #elif defined(__mips__)
  263. __asm__("\tmove %0, $28\n\tsubu %0,%0,0x7ff0\n\t":"=r"(got));
  264. #elif defined(__sh__)
  265. __asm__(
  266. " mov.l 1f, %0\n"
  267. " mova 1f, r0\n"
  268. " bra 2f\n"
  269. " add r0, %0\n"
  270. " .balign 4\n"
  271. "1: .long _GLOBAL_OFFSET_TABLE_\n"
  272. "2:" : "=r" (got) : : "r0");
  273. #elif defined(__cris__)
  274. __asm__("\tmove.d $pc,%0\n\tsub.d .:GOTOFF,%0\n\t":"=r"(got));
  275. #else
  276. /* Do things the slow way in C */
  277. {
  278. unsigned long tx_reloc;
  279. Elf32_Dyn *dynamic = NULL;
  280. Elf32_Shdr *shdr;
  281. Elf32_Phdr *pt_load;
  282. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  283. SEND_STDERR("Finding the GOT using C code to read the ELF file\n");
  284. #endif
  285. /* Find where the dynamic linking information section is hiding */
  286. shdr = (Elf32_Shdr *) (header->e_shoff + (char *) header);
  287. for (indx = header->e_shnum; --indx >= 0; ++shdr) {
  288. if (shdr->sh_type == SHT_DYNAMIC) {
  289. goto found_dynamic;
  290. }
  291. }
  292. SEND_STDERR("missing dynamic linking information section \n");
  293. _dl_exit(0);
  294. found_dynamic:
  295. dynamic = (Elf32_Dyn *) (shdr->sh_offset + (char *) header);
  296. /* Find where PT_LOAD is hiding */
  297. pt_load = (Elf32_Phdr *) (header->e_phoff + (char *) header);
  298. for (indx = header->e_phnum; --indx >= 0; ++pt_load) {
  299. if (pt_load->p_type == PT_LOAD) {
  300. goto found_pt_load;
  301. }
  302. }
  303. SEND_STDERR("missing loadable program segment\n");
  304. _dl_exit(0);
  305. found_pt_load:
  306. /* Now (finally) find where DT_PLTGOT is hiding */
  307. tx_reloc = pt_load->p_vaddr - pt_load->p_offset;
  308. for (; DT_NULL != dynamic->d_tag; ++dynamic) {
  309. if (dynamic->d_tag == DT_PLTGOT) {
  310. goto found_got;
  311. }
  312. }
  313. SEND_STDERR("missing global offset table\n");
  314. _dl_exit(0);
  315. found_got:
  316. got = (unsigned long *) (dynamic->d_un.d_val - tx_reloc +
  317. (char *) header);
  318. }
  319. #endif
  320. /* Now, finally, fix up the location of the dynamic stuff */
  321. dpnt = (Elf32_Dyn *) (*got + load_addr);
  322. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  323. SEND_STDERR("First Dynamic section entry=");
  324. SEND_ADDRESS_STDERR(dpnt, 1);
  325. #endif
  326. /* Call mmap to get a page of writable memory that can be used
  327. * for _dl_malloc throughout the shared lib loader. */
  328. mmap_zero = malloc_buffer = _dl_mmap((void *) 0, 4096,
  329. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  330. if (_dl_mmap_check_error(mmap_zero)) {
  331. SEND_STDERR("dl_boot: mmap of a spare page failed!\n");
  332. _dl_exit(13);
  333. }
  334. tpnt = LD_MALLOC(sizeof(struct elf_resolve));
  335. _dl_memset(tpnt, 0, sizeof(struct elf_resolve));
  336. app_tpnt = LD_MALLOC(sizeof(struct elf_resolve));
  337. _dl_memset(app_tpnt, 0, sizeof(struct elf_resolve));
  338. /*
  339. * This is used by gdb to locate the chain of shared libraries that are currently loaded.
  340. */
  341. debug_addr = LD_MALLOC(sizeof(struct r_debug));
  342. _dl_memset(debug_addr, 0, sizeof(struct r_debug));
  343. /* OK, that was easy. Next scan the DYNAMIC section of the image.
  344. We are only doing ourself right now - we will have to do the rest later */
  345. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  346. SEND_STDERR("scanning DYNAMIC section\n");
  347. #endif
  348. while (dpnt->d_tag) {
  349. #if defined(__mips__)
  350. if (dpnt->d_tag == DT_MIPS_GOTSYM)
  351. tpnt->mips_gotsym = (unsigned long) dpnt->d_un.d_val;
  352. if (dpnt->d_tag == DT_MIPS_LOCAL_GOTNO)
  353. tpnt->mips_local_gotno = (unsigned long) dpnt->d_un.d_val;
  354. if (dpnt->d_tag == DT_MIPS_SYMTABNO)
  355. tpnt->mips_symtabno = (unsigned long) dpnt->d_un.d_val;
  356. #endif
  357. if (dpnt->d_tag < 24) {
  358. tpnt->dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  359. if (dpnt->d_tag == DT_TEXTREL) {
  360. tpnt->dynamic_info[DT_TEXTREL] = 1;
  361. }
  362. }
  363. dpnt++;
  364. }
  365. {
  366. ElfW(Phdr) *ppnt;
  367. int i;
  368. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_ptr;
  369. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++)
  370. if (ppnt->p_type == PT_DYNAMIC) {
  371. dpnt = (Elf32_Dyn *) ppnt->p_vaddr;
  372. while (dpnt->d_tag) {
  373. #if defined(__mips__)
  374. if (dpnt->d_tag == DT_MIPS_GOTSYM)
  375. app_tpnt->mips_gotsym =
  376. (unsigned long) dpnt->d_un.d_val;
  377. if (dpnt->d_tag == DT_MIPS_LOCAL_GOTNO)
  378. app_tpnt->mips_local_gotno =
  379. (unsigned long) dpnt->d_un.d_val;
  380. if (dpnt->d_tag == DT_MIPS_SYMTABNO)
  381. app_tpnt->mips_symtabno =
  382. (unsigned long) dpnt->d_un.d_val;
  383. #endif
  384. if (dpnt->d_tag > DT_JMPREL) {
  385. dpnt++;
  386. continue;
  387. }
  388. app_tpnt->dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  389. #if !defined(__mips__)
  390. if (dpnt->d_tag == DT_DEBUG) {
  391. dpnt->d_un.d_val = (unsigned long) debug_addr;
  392. }
  393. #else
  394. #warning "Debugging threads on mips won't work till someone fixes this..."
  395. #endif
  396. if (dpnt->d_tag == DT_TEXTREL)
  397. app_tpnt->dynamic_info[DT_TEXTREL] = 1;
  398. dpnt++;
  399. }
  400. }
  401. }
  402. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  403. SEND_STDERR("done scanning DYNAMIC section\n");
  404. #endif
  405. /* Get some more of the information that we will need to dynamicly link
  406. this module to itself */
  407. hash_addr = (unsigned long *) (tpnt->dynamic_info[DT_HASH] + load_addr);
  408. tpnt->nbucket = *hash_addr++;
  409. tpnt->nchain = *hash_addr++;
  410. tpnt->elf_buckets = hash_addr;
  411. hash_addr += tpnt->nbucket;
  412. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  413. SEND_STDERR("done grabbing link information\n");
  414. #endif
  415. #ifndef FORCE_SHAREABLE_TEXT_SEGMENTS
  416. /* Ugly, ugly. We need to call mprotect to change the protection of
  417. the text pages so that we can do the dynamic linking. We can set the
  418. protection back again once we are done */
  419. {
  420. ElfW(Phdr) *ppnt;
  421. int i;
  422. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  423. SEND_STDERR("calling mprotect on the shared library/dynamic linker\n");
  424. #endif
  425. /* First cover the shared library/dynamic linker. */
  426. if (tpnt->dynamic_info[DT_TEXTREL]) {
  427. header = (ElfW(Ehdr) *) auxvt[AT_BASE].a_un.a_ptr;
  428. ppnt = (ElfW(Phdr) *) ((int)auxvt[AT_BASE].a_un.a_ptr +
  429. header->e_phoff);
  430. for (i = 0; i < header->e_phnum; i++, ppnt++) {
  431. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W)) {
  432. _dl_mprotect((void *) (load_addr + (ppnt->p_vaddr & PAGE_ALIGN)),
  433. (ppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) ppnt->p_filesz,
  434. PROT_READ | PROT_WRITE | PROT_EXEC);
  435. }
  436. }
  437. }
  438. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  439. SEND_STDERR("calling mprotect on the application program\n");
  440. #endif
  441. /* Now cover the application program. */
  442. if (app_tpnt->dynamic_info[DT_TEXTREL]) {
  443. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_ptr;
  444. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  445. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  446. _dl_mprotect((void *) (ppnt->p_vaddr & PAGE_ALIGN),
  447. (ppnt->p_vaddr & ADDR_ALIGN) +
  448. (unsigned long) ppnt->p_filesz,
  449. PROT_READ | PROT_WRITE | PROT_EXEC);
  450. }
  451. }
  452. }
  453. #endif
  454. #if defined(__mips__)
  455. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  456. SEND_STDERR("About to do MIPS specific GOT bootstrap\n");
  457. #endif
  458. /* For MIPS we have to do stuff to the GOT before we do relocations. */
  459. PERFORM_BOOTSTRAP_GOT(got);
  460. #endif
  461. /* OK, now do the relocations. We do not do a lazy binding here, so
  462. that once we are done, we have considerably more flexibility. */
  463. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  464. SEND_STDERR("About to do library loader relocations\n");
  465. #endif
  466. goof = 0;
  467. for (indx = 0; indx < 2; indx++) {
  468. unsigned int i;
  469. ELF_RELOC *rpnt;
  470. unsigned long *reloc_addr;
  471. unsigned long symbol_addr;
  472. int symtab_index;
  473. unsigned long rel_addr, rel_size;
  474. #ifdef ELF_USES_RELOCA
  475. rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] : tpnt->
  476. dynamic_info[DT_RELA]);
  477. rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] : tpnt->
  478. dynamic_info[DT_RELASZ]);
  479. #else
  480. rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] : tpnt->
  481. dynamic_info[DT_REL]);
  482. rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] : tpnt->
  483. dynamic_info[DT_RELSZ]);
  484. #endif
  485. if (!rel_addr)
  486. continue;
  487. /* Now parse the relocation information */
  488. rpnt = (ELF_RELOC *) (rel_addr + load_addr);
  489. for (i = 0; i < rel_size; i += sizeof(ELF_RELOC), rpnt++) {
  490. reloc_addr = (unsigned long *) (load_addr + (unsigned long) rpnt->r_offset);
  491. symtab_index = ELF32_R_SYM(rpnt->r_info);
  492. symbol_addr = 0;
  493. if (symtab_index) {
  494. char *strtab;
  495. Elf32_Sym *symtab;
  496. symtab = (Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + load_addr);
  497. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + load_addr);
  498. /* We only do a partial dynamic linking right now. The user
  499. is not supposed to redefine any symbols that start with
  500. a '_', so we can do this with confidence. */
  501. if (!_dl_symbol(strtab + symtab[symtab_index].st_name))
  502. continue;
  503. symbol_addr = load_addr + symtab[symtab_index].st_value;
  504. if (!symbol_addr) {
  505. /* This will segfault - you cannot call a function until
  506. * we have finished the relocations.
  507. */
  508. SEND_STDERR("ELF dynamic loader - unable to self-bootstrap - symbol ");
  509. SEND_STDERR(strtab + symtab[symtab_index].st_name);
  510. SEND_STDERR(" undefined.\n");
  511. goof++;
  512. }
  513. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  514. SEND_STDERR("About to fixup symbol: ");
  515. SEND_STDERR(strtab + symtab[symtab_index].st_name);
  516. SEND_STDERR("\n");
  517. #endif
  518. }
  519. /*
  520. * Use this machine-specific macro to perform the actual relocation.
  521. */
  522. PERFORM_BOOTSTRAP_RELOC(rpnt, reloc_addr, symbol_addr, load_addr);
  523. }
  524. }
  525. if (goof) {
  526. _dl_exit(14);
  527. }
  528. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  529. /* Wahoo!!! */
  530. _dl_dprintf(_dl_debug_file, "Done relocating library loader, so we can now\n\tuse globals and make function calls!\n");
  531. #endif
  532. if (argv[0]) {
  533. _dl_progname = argv[0];
  534. }
  535. /* Start to build the tables of the modules that are required for
  536. * this beast to run. We start with the basic executable, and then
  537. * go from there. Eventually we will run across ourself, and we
  538. * will need to properly deal with that as well. */
  539. /* Make it so _dl_malloc can use the page of memory we have already
  540. * allocated, so we shouldn't need to grab any more memory */
  541. _dl_malloc_addr = malloc_buffer;
  542. _dl_mmap_zero = mmap_zero;
  543. /* Now we have done the mandatory linking of some things. We are now
  544. free to start using global variables, since these things have all been
  545. fixed up by now. Still no function calls outside of this library ,
  546. since the dynamic resolver is not yet ready. */
  547. _dl_get_ready_to_run(tpnt, app_tpnt, load_addr, hash_addr, auxvt, envp, debug_addr);
  548. /* Notify the debugger that all objects are now mapped in. */
  549. _dl_debug_addr->r_state = RT_CONSISTENT;
  550. _dl_debug_state();
  551. /* OK we are done here. Turn out the lights, and lock up. */
  552. _dl_elf_main = (int (*)(int, char **, char **)) auxvt[AT_ENTRY].a_un.a_fcn;
  553. /*
  554. * Transfer control to the application.
  555. */
  556. status = 0; /* Used on x86, but not on other arches */
  557. #if defined (__SUPPORT_LD_DEBUG__)
  558. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\ntransfering control: %s\n\n", _dl_progname);
  559. #endif
  560. START();
  561. }
  562. #if defined (__SUPPORT_LD_DEBUG__)
  563. static void debug_fini (int status, void *arg)
  564. {
  565. (void)status;
  566. _dl_dprintf(_dl_debug_file,"\ncalling fini: %s\n\n", (const char*)arg);
  567. }
  568. #endif
  569. static void _dl_get_ready_to_run(struct elf_resolve *tpnt, struct elf_resolve *app_tpnt,
  570. unsigned long load_addr, unsigned long *hash_addr, Elf32_auxv_t auxvt[AT_EGID + 1],
  571. char **envp, struct r_debug *debug_addr)
  572. {
  573. ElfW(Phdr) *ppnt;
  574. char *lpntstr;
  575. int i, _dl_secure, goof = 0;
  576. struct dyn_elf *rpnt;
  577. struct elf_resolve *tcurr;
  578. struct elf_resolve *tpnt1;
  579. unsigned long brk_addr, *lpnt;
  580. int (*_dl_atexit) (void *);
  581. #if defined (__SUPPORT_LD_DEBUG__)
  582. int (*_dl_on_exit) (void (*FUNCTION)(int STATUS, void *ARG),void*);
  583. #endif
  584. /* Now we have done the mandatory linking of some things. We are now
  585. free to start using global variables, since these things have all been
  586. fixed up by now. Still no function calls outside of this library ,
  587. since the dynamic resolver is not yet ready. */
  588. lpnt = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT] + load_addr);
  589. tpnt->chains = hash_addr;
  590. tpnt->next = 0;
  591. tpnt->libname = 0;
  592. tpnt->libtype = program_interpreter;
  593. tpnt->loadaddr = (char *) load_addr;
  594. #ifdef ALLOW_ZERO_PLTGOT
  595. if (tpnt->dynamic_info[DT_PLTGOT])
  596. #endif
  597. {
  598. INIT_GOT(lpnt, tpnt);
  599. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  600. _dl_dprintf(_dl_debug_file, "GOT found at %x\n", lpnt);
  601. #endif
  602. }
  603. /* OK, this was a big step, now we need to scan all of the user images
  604. and load them properly. */
  605. {
  606. ElfW(Ehdr) *epnt;
  607. ElfW(Phdr) *myppnt;
  608. int j;
  609. epnt = (ElfW(Ehdr) *) auxvt[AT_BASE].a_un.a_ptr;
  610. tpnt->n_phent = epnt->e_phnum;
  611. tpnt->ppnt = myppnt = (ElfW(Phdr) *) (load_addr + epnt->e_phoff);
  612. for (j = 0; j < epnt->e_phnum; j++, myppnt++) {
  613. if (myppnt->p_type == PT_DYNAMIC) {
  614. tpnt->dynamic_addr = myppnt->p_vaddr + load_addr;
  615. #if defined(__mips__)
  616. {
  617. int k = 1;
  618. Elf32_Dyn *dpnt = (Elf32_Dyn *) tpnt->dynamic_addr;
  619. while(dpnt->d_tag) {
  620. dpnt++;
  621. k++;
  622. }
  623. tpnt->dynamic_size = k * sizeof(Elf32_Dyn);
  624. }
  625. #else
  626. tpnt->dynamic_size = myppnt->p_filesz;
  627. #endif
  628. }
  629. }
  630. }
  631. brk_addr = 0;
  632. rpnt = NULL;
  633. /* At this point we are now free to examine the user application,
  634. and figure out which libraries are supposed to be called. Until
  635. we have this list, we will not be completely ready for dynamic linking */
  636. ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_ptr;
  637. for (i = 0; i < auxvt[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  638. if (ppnt->p_type == PT_LOAD) {
  639. if (ppnt->p_vaddr + ppnt->p_memsz > brk_addr)
  640. brk_addr = ppnt->p_vaddr + ppnt->p_memsz;
  641. }
  642. if (ppnt->p_type == PT_DYNAMIC) {
  643. #ifndef ALLOW_ZERO_PLTGOT
  644. /* make sure it's really there. */
  645. if (app_tpnt->dynamic_info[DT_PLTGOT] == 0)
  646. continue;
  647. #endif
  648. /* OK, we have what we need - slip this one into the list. */
  649. #if defined(__mips__)
  650. {
  651. int i = 1;
  652. Elf32_Dyn *dpnt = (Elf32_Dyn *) tpnt->dynamic_addr;
  653. while(dpnt->d_tag) {
  654. dpnt++;
  655. i++;
  656. }
  657. app_tpnt = _dl_add_elf_hash_table("", 0,
  658. app_tpnt->dynamic_info, ppnt->p_vaddr,
  659. (i * sizeof(Elf32_Dyn)));
  660. }
  661. #else
  662. app_tpnt = _dl_add_elf_hash_table("", 0,
  663. app_tpnt->dynamic_info, ppnt->p_vaddr, ppnt->p_filesz);
  664. #endif
  665. _dl_loaded_modules->libtype = elf_executable;
  666. _dl_loaded_modules->ppnt = (ElfW(Phdr) *) auxvt[AT_PHDR].a_un.a_ptr;
  667. _dl_loaded_modules->n_phent = auxvt[AT_PHNUM].a_un.a_val;
  668. _dl_symbol_tables = rpnt = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  669. _dl_memset(rpnt, 0, sizeof(struct dyn_elf));
  670. rpnt->dyn = _dl_loaded_modules;
  671. app_tpnt->usage_count++;
  672. app_tpnt->symbol_scope = _dl_symbol_tables;
  673. lpnt = (unsigned long *) (app_tpnt->dynamic_info[DT_PLTGOT]);
  674. #ifdef ALLOW_ZERO_PLTGOT
  675. if (lpnt)
  676. #endif
  677. INIT_GOT(lpnt, _dl_loaded_modules);
  678. }
  679. /* OK, fill this in - we did not have this before */
  680. if (ppnt->p_type == PT_INTERP) {
  681. int readsize = 0;
  682. char *pnt, *pnt1, buf[1024];
  683. tpnt->libname = _dl_strdup((char *) ppnt->p_offset +
  684. (auxvt[AT_PHDR].a_un.a_val & PAGE_ALIGN));
  685. /* Determine if the shared lib loader is a symlink */
  686. _dl_memset(buf, 0, sizeof(buf));
  687. readsize = _dl_readlink(tpnt->libname, buf, sizeof(buf));
  688. if (readsize > 0 && readsize < (int)(sizeof(buf)-1)) {
  689. pnt1 = _dl_strrchr(buf, '/');
  690. if (pnt1 && buf != pnt1) {
  691. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  692. _dl_dprintf(_dl_debug_file, "changing tpnt->libname from '%s' to '%s'\n", tpnt->libname, buf);
  693. #endif
  694. tpnt->libname = _dl_strdup(buf);
  695. }
  696. }
  697. /* Store the path where the shared lib loader was found for
  698. * later use */
  699. pnt = _dl_strdup(tpnt->libname);
  700. pnt1 = _dl_strrchr(pnt, '/');
  701. if (pnt != pnt1) {
  702. *pnt1 = '\0';
  703. _dl_ldsopath = pnt;
  704. } else {
  705. _dl_ldsopath = tpnt->libname;
  706. }
  707. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  708. _dl_dprintf(_dl_debug_file, "Lib Loader:\t(%x) %s\n", tpnt->loadaddr, tpnt->libname);
  709. #endif
  710. }
  711. }
  712. /* Now we need to figure out what kind of options are selected.
  713. Note that for SUID programs we ignore the settings in LD_LIBRARY_PATH */
  714. {
  715. if (_dl_getenv("LD_BIND_NOW", envp))
  716. _dl_be_lazy = 0;
  717. if ((auxvt[AT_UID].a_un.a_val == -1 && _dl_suid_ok()) ||
  718. (auxvt[AT_UID].a_un.a_val != -1 &&
  719. auxvt[AT_UID].a_un.a_val == auxvt[AT_EUID].a_un.a_val
  720. && auxvt[AT_GID].a_un.a_val== auxvt[AT_EGID].a_un.a_val)) {
  721. _dl_secure = 0;
  722. _dl_preload = _dl_getenv("LD_PRELOAD", envp);
  723. _dl_library_path = _dl_getenv("LD_LIBRARY_PATH", envp);
  724. } else {
  725. _dl_secure = 1;
  726. _dl_preload = _dl_getenv("LD_PRELOAD", envp);
  727. _dl_unsetenv("LD_AOUT_PRELOAD", envp);
  728. _dl_unsetenv("LD_LIBRARY_PATH", envp);
  729. _dl_unsetenv("LD_AOUT_LIBRARY_PATH", envp);
  730. _dl_library_path = NULL;
  731. }
  732. }
  733. #ifdef __SUPPORT_LD_DEBUG__
  734. _dl_debug = _dl_getenv("LD_DEBUG", envp);
  735. if (_dl_debug)
  736. {
  737. if (_dl_strstr(_dl_debug, "all")) {
  738. _dl_debug_detail = _dl_debug_move = _dl_debug_symbols
  739. = _dl_debug_reloc = _dl_debug_bindings = _dl_debug_nofixups = _dl_strstr(_dl_debug, "all");
  740. }
  741. else {
  742. _dl_debug_detail = _dl_strstr(_dl_debug, "detail");
  743. _dl_debug_move = _dl_strstr(_dl_debug, "move");
  744. _dl_debug_symbols = _dl_strstr(_dl_debug, "sym");
  745. _dl_debug_reloc = _dl_strstr(_dl_debug, "reloc");
  746. _dl_debug_nofixups = _dl_strstr(_dl_debug, "nofix");
  747. _dl_debug_bindings = _dl_strstr(_dl_debug, "bind");
  748. }
  749. }
  750. {
  751. const char *dl_debug_output;
  752. dl_debug_output = _dl_getenv("LD_DEBUG_OUTPUT", envp);
  753. if (dl_debug_output)
  754. {
  755. char tmp[22], *tmp1, *filename;
  756. int len1, len2;
  757. _dl_memset(tmp, 0, sizeof(tmp));
  758. tmp1=_dl_simple_ltoa( tmp, (unsigned long)_dl_getpid());
  759. len1 = _dl_strlen(dl_debug_output);
  760. len2 = _dl_strlen(tmp1);
  761. filename = _dl_malloc(len1+len2+2);
  762. if (filename)
  763. {
  764. _dl_strcpy (filename, dl_debug_output);
  765. filename[len1] = '.';
  766. _dl_strcpy (&filename[len1+1], tmp1);
  767. _dl_debug_file= _dl_open (filename, O_WRONLY|O_CREAT);
  768. if (_dl_debug_file<0)
  769. {
  770. _dl_debug_file = 2;
  771. _dl_dprintf (2, "can't open file: '%s'\n",filename);
  772. }
  773. }
  774. }
  775. }
  776. #endif
  777. _dl_trace_loaded_objects = _dl_getenv("LD_TRACE_LOADED_OBJECTS", envp);
  778. #ifndef __LDSO_LDD_SUPPORT__
  779. if (_dl_trace_loaded_objects) {
  780. _dl_dprintf(_dl_debug_file, "Use the ldd provided by uClibc\n");
  781. _dl_exit(1);
  782. }
  783. #endif
  784. /*
  785. * OK, fix one more thing - set up debug_addr so it will point
  786. * to our chain. Later we may need to fill in more fields, but this
  787. * should be enough for now.
  788. */
  789. debug_addr->r_map = (struct link_map *) _dl_loaded_modules;
  790. debug_addr->r_version = 1;
  791. debug_addr->r_ldbase = load_addr;
  792. debug_addr->r_brk = (unsigned long) &_dl_debug_state;
  793. _dl_debug_addr = debug_addr;
  794. /* Notify the debugger we are in a consistant state */
  795. _dl_debug_addr->r_state = RT_CONSISTENT;
  796. _dl_debug_state();
  797. /* OK, we now have the application in the list, and we have some
  798. basic stuff in place. Now search through the list for other shared
  799. libraries that should be loaded, and insert them on the list in the
  800. correct order. */
  801. _dl_map_cache();
  802. if (_dl_preload)
  803. {
  804. char c, *str, *str2;
  805. str = _dl_preload;
  806. while (*str == ':' || *str == ' ' || *str == '\t')
  807. str++;
  808. while (*str)
  809. {
  810. str2 = str;
  811. while (*str2 && *str2 != ':' && *str2 != ' ' && *str2 != '\t')
  812. str2++;
  813. c = *str2;
  814. *str2 = '\0';
  815. if (!_dl_secure || _dl_strchr(str, '/') == NULL)
  816. {
  817. tpnt1 = _dl_load_shared_library(_dl_secure, &rpnt, NULL, str);
  818. if (!tpnt1) {
  819. #ifdef __LDSO_LDD_SUPPORT__
  820. if (_dl_trace_loaded_objects)
  821. _dl_dprintf(1, "\t%s => not found\n", str);
  822. else
  823. #endif
  824. {
  825. _dl_dprintf(2, "%s: can't load " "library '%s'\n", _dl_progname, str);
  826. _dl_exit(15);
  827. }
  828. } else {
  829. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  830. _dl_dprintf(_dl_debug_file, "Loading:\t(%x) %s\n", tpnt1->loadaddr, tpnt1->libname);
  831. #endif
  832. #ifdef __LDSO_LDD_SUPPORT__
  833. if (_dl_trace_loaded_objects && tpnt1->usage_count==1) {
  834. /* this is a real hack to make ldd not print
  835. * the library itself when run on a library. */
  836. if (_dl_strcmp(_dl_progname, str) != 0)
  837. _dl_dprintf(1, "\t%s => %s (%x)\n", str, tpnt1->libname,
  838. (unsigned) tpnt1->loadaddr);
  839. }
  840. #endif
  841. }
  842. }
  843. *str2 = c;
  844. str = str2;
  845. while (*str == ':' || *str == ' ' || *str == '\t')
  846. str++;
  847. }
  848. }
  849. #ifdef SUPPORT_LDSO_PRELOAD_FILE
  850. {
  851. int fd;
  852. struct stat st;
  853. char *preload;
  854. if (!_dl_stat(LDSO_PRELOAD, &st) && st.st_size > 0) {
  855. if ((fd = _dl_open(LDSO_PRELOAD, O_RDONLY)) < 0) {
  856. _dl_dprintf(2, "%s: can't open file '%s'\n",
  857. _dl_progname, LDSO_PRELOAD);
  858. } else {
  859. preload = (caddr_t) _dl_mmap(0, st.st_size + 1,
  860. PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  861. _dl_close(fd);
  862. if (preload == (caddr_t) - 1) {
  863. _dl_dprintf(2, "%s: can't map file '%s'\n",
  864. _dl_progname, LDSO_PRELOAD);
  865. } else {
  866. char c, *cp, *cp2;
  867. /* convert all separators and comments to spaces */
  868. for (cp = preload; *cp; /*nada */ ) {
  869. if (*cp == ':' || *cp == '\t' || *cp == '\n') {
  870. *cp++ = ' ';
  871. } else if (*cp == '#') {
  872. do
  873. *cp++ = ' ';
  874. while (*cp != '\n' && *cp != '\0');
  875. } else {
  876. cp++;
  877. }
  878. }
  879. /* find start of first library */
  880. for (cp = preload; *cp && *cp == ' '; cp++)
  881. /*nada */ ;
  882. while (*cp) {
  883. /* find end of library */
  884. for (cp2 = cp; *cp && *cp != ' '; cp++)
  885. /*nada */ ;
  886. c = *cp;
  887. *cp = '\0';
  888. tpnt1 = _dl_load_shared_library(0, &rpnt, NULL, cp2);
  889. if (!tpnt1) {
  890. #ifdef __LDSO_LDD_SUPPORT__
  891. if (_dl_trace_loaded_objects)
  892. _dl_dprintf(1, "\t%s => not found\n", cp2);
  893. else
  894. #endif
  895. {
  896. _dl_dprintf(2, "%s: can't load library '%s'\n", _dl_progname, cp2);
  897. _dl_exit(15);
  898. }
  899. } else {
  900. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  901. _dl_dprintf(_dl_debug_file, "Loading:\t(%x) %s\n", tpnt1->loadaddr, tpnt1->libname);
  902. #endif
  903. #ifdef __LDSO_LDD_SUPPORT__
  904. if (_dl_trace_loaded_objects && tpnt1->usage_count==1) {
  905. _dl_dprintf(1, "\t%s => %s (%x)\n", cp2,
  906. tpnt1->libname, (unsigned) tpnt1->loadaddr);
  907. }
  908. #endif
  909. }
  910. /* find start of next library */
  911. *cp = c;
  912. for ( /*nada */ ; *cp && *cp == ' '; cp++)
  913. /*nada */ ;
  914. }
  915. _dl_munmap(preload, st.st_size + 1);
  916. }
  917. }
  918. }
  919. }
  920. #endif
  921. for (tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next) {
  922. Elf32_Dyn *dpnt;
  923. for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag;
  924. dpnt++) {
  925. if (dpnt->d_tag == DT_NEEDED) {
  926. lpntstr = tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  927. dpnt->d_un.d_val;
  928. if (_dl_strcmp(lpntstr, "libc.so.6") == 0) {
  929. char *name, *msg;
  930. name = tcurr->libname;
  931. while(*name == '/')
  932. name++;
  933. if (_dl_trace_loaded_objects) {
  934. msg = "WARNING";
  935. } else {
  936. msg = "ERROR";
  937. }
  938. _dl_dprintf(2, "\t%s: %s is linked with GNU libc!\n", msg, --name);
  939. /* If all we are doing is ldd, then we don't need to freak out... */
  940. if (_dl_trace_loaded_objects) {
  941. continue;
  942. }
  943. /* Time to freak out. Make sure glibc linked libraries are not loaded */
  944. _dl_exit(150);
  945. }
  946. if (tpnt && _dl_strcmp(lpntstr, _dl_get_last_path_component(tpnt->libname)) == 0)
  947. {
  948. struct elf_resolve *ttmp;
  949. #ifdef __LDSO_LDD_SUPPORT__
  950. if (_dl_trace_loaded_objects && tpnt->usage_count==1) {
  951. char *name;
  952. name = tpnt->libname;
  953. while(*name == '/')
  954. name++;
  955. _dl_dprintf(1, "\t%s => %s (%x)\n",
  956. lpntstr, --name, (unsigned) tpnt->loadaddr);
  957. }
  958. #endif
  959. ttmp = _dl_loaded_modules;
  960. while (ttmp->next)
  961. ttmp = ttmp->next;
  962. ttmp->next = tpnt;
  963. tpnt->prev = ttmp;
  964. tpnt->next = NULL;
  965. rpnt->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  966. _dl_memset(rpnt->next, 0, sizeof(struct dyn_elf));
  967. rpnt->next->prev = rpnt;
  968. rpnt = rpnt->next;
  969. rpnt->dyn = tpnt;
  970. tpnt->usage_count++;
  971. tpnt->symbol_scope = _dl_symbol_tables;
  972. tpnt = NULL;
  973. continue;
  974. }
  975. if (!(tpnt1 = _dl_load_shared_library(0, &rpnt, tcurr, lpntstr)))
  976. {
  977. #ifdef __LDSO_LDD_SUPPORT__
  978. if (_dl_trace_loaded_objects)
  979. _dl_dprintf(1, "\t%s => not found\n", lpntstr);
  980. else
  981. #endif
  982. {
  983. _dl_dprintf(2, "%s: can't load library '%s'\n", _dl_progname, lpntstr);
  984. _dl_exit(16);
  985. }
  986. } else {
  987. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  988. _dl_dprintf(_dl_debug_file, "Loading:\t(%x) %s\n", tpnt1->loadaddr, tpnt1->libname);
  989. #endif
  990. #ifdef __LDSO_LDD_SUPPORT__
  991. if (_dl_trace_loaded_objects && tpnt1->usage_count==1) {
  992. char *name;
  993. name = tpnt1->libname;
  994. while(*name == '/')
  995. name++;
  996. _dl_dprintf(1, "\t%s => %s (%x)\n", lpntstr, --name,
  997. (unsigned) tpnt1->loadaddr);
  998. }
  999. #endif
  1000. }
  1001. }
  1002. }
  1003. }
  1004. _dl_unmap_cache();
  1005. /*
  1006. * If the program interpreter is not in the module chain, add it. This will
  1007. * be required for dlopen to be able to access the internal functions in the
  1008. * dynamic linker.
  1009. */
  1010. if (tpnt) {
  1011. tcurr = _dl_loaded_modules;
  1012. if (tcurr)
  1013. while (tcurr->next)
  1014. tcurr = tcurr->next;
  1015. tpnt->next = NULL;
  1016. tpnt->usage_count++;
  1017. if (tcurr) {
  1018. tcurr->next = tpnt;
  1019. tpnt->prev = tcurr;
  1020. } else {
  1021. _dl_loaded_modules = tpnt;
  1022. tpnt->prev = NULL;
  1023. }
  1024. if (rpnt) {
  1025. rpnt->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  1026. _dl_memset(rpnt->next, 0, sizeof(struct dyn_elf));
  1027. rpnt->next->prev = rpnt;
  1028. rpnt = rpnt->next;
  1029. } else {
  1030. rpnt = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  1031. _dl_memset(rpnt, 0, sizeof(struct dyn_elf));
  1032. }
  1033. rpnt->dyn = tpnt;
  1034. tpnt = NULL;
  1035. }
  1036. #ifdef __LDSO_LDD_SUPPORT__
  1037. /* End of the line for ldd.... */
  1038. if (_dl_trace_loaded_objects) {
  1039. _dl_dprintf(1, "\t%s => %s (%x)\n", rpnt->dyn->libname + (_dl_strlen(_dl_ldsopath)) + 1,
  1040. rpnt->dyn->libname, rpnt->dyn->loadaddr);
  1041. _dl_exit(0);
  1042. }
  1043. #endif
  1044. #ifdef __mips__
  1045. /*
  1046. * Relocation of the GOT entries for MIPS have to be done
  1047. * after all the libraries have been loaded.
  1048. */
  1049. _dl_perform_mips_global_got_relocations(_dl_loaded_modules);
  1050. #endif
  1051. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  1052. _dl_dprintf(_dl_debug_file, "Beginning relocation fixups\n");
  1053. #endif
  1054. /*
  1055. * OK, now all of the kids are tucked into bed in their proper addresses.
  1056. * Now we go through and look for REL and RELA records that indicate fixups
  1057. * to the GOT tables. We need to do this in reverse order so that COPY
  1058. * directives work correctly */
  1059. goof = _dl_loaded_modules ? _dl_fixup(_dl_loaded_modules, _dl_be_lazy) : 0;
  1060. /* Some flavors of SVr4 do not generate the R_*_COPY directive,
  1061. and we have to manually search for entries that require fixups.
  1062. Solaris gets this one right, from what I understand. */
  1063. #ifdef __SUPPORT_LD_DEBUG_EARLY__
  1064. _dl_dprintf(_dl_debug_file, "Beginning copy fixups\n");
  1065. #endif
  1066. if (_dl_symbol_tables)
  1067. goof += _dl_copy_fixups(_dl_symbol_tables);
  1068. /* OK, at this point things are pretty much ready to run. Now we
  1069. need to touch up a few items that are required, and then
  1070. we can let the user application have at it. Note that
  1071. the dynamic linker itself is not guaranteed to be fully
  1072. dynamicly linked if we are using ld.so.1, so we have to look
  1073. up each symbol individually. */
  1074. _dl_brkp = (unsigned long *) (intptr_t) _dl_find_hash("___brk_addr", NULL, NULL, symbolrel);
  1075. if (_dl_brkp) {
  1076. *_dl_brkp = brk_addr;
  1077. }
  1078. _dl_envp = (unsigned long *) (intptr_t) _dl_find_hash("__environ", NULL, NULL, symbolrel);
  1079. if (_dl_envp) {
  1080. *_dl_envp = (unsigned long) envp;
  1081. }
  1082. #ifndef FORCE_SHAREABLE_TEXT_SEGMENTS
  1083. {
  1084. unsigned int j;
  1085. ElfW(Phdr) *myppnt;
  1086. /* We had to set the protections of all pages to R/W for dynamic linking.
  1087. Set text pages back to R/O */
  1088. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  1089. for (myppnt = tpnt->ppnt, j = 0; j < tpnt->n_phent; j++, myppnt++) {
  1090. if (myppnt->p_type == PT_LOAD && !(myppnt->p_flags & PF_W) && tpnt->dynamic_info[DT_TEXTREL]) {
  1091. _dl_mprotect((void *) (tpnt->loadaddr + (myppnt->p_vaddr & PAGE_ALIGN)),
  1092. (myppnt->p_vaddr & ADDR_ALIGN) + (unsigned long) myppnt->p_filesz, LXFLAGS(myppnt->p_flags));
  1093. }
  1094. }
  1095. }
  1096. }
  1097. #endif
  1098. _dl_atexit = (int (*)(void *)) (intptr_t) _dl_find_hash("atexit", NULL, NULL, symbolrel);
  1099. #if defined (__SUPPORT_LD_DEBUG__)
  1100. _dl_on_exit = (int (*)(void (*)(int, void *),void*))
  1101. (intptr_t) _dl_find_hash("on_exit", NULL, NULL, symbolrel);
  1102. #endif
  1103. /* Notify the debugger we have added some objects. */
  1104. _dl_debug_addr->r_state = RT_ADD;
  1105. _dl_debug_state();
  1106. for (rpnt = _dl_symbol_tables; rpnt!=NULL&& rpnt->next!=NULL; rpnt=rpnt->next)
  1107. ;
  1108. for (;rpnt!=NULL; rpnt=rpnt->prev)
  1109. {
  1110. tpnt = rpnt->dyn;
  1111. if (tpnt->libtype == program_interpreter)
  1112. continue;
  1113. /* Apparently crt0/1 for the application is responsible for handling this.
  1114. * We only need to run the init/fini for shared libraries
  1115. */
  1116. if (tpnt->libtype == elf_executable)
  1117. break; /* at this point all shared libs are initialized !! */
  1118. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  1119. continue;
  1120. tpnt->init_flag |= INIT_FUNCS_CALLED;
  1121. if (tpnt->dynamic_info[DT_INIT]) {
  1122. _dl_elf_init = (int (*)(void)) (intptr_t) (tpnt->loadaddr + tpnt->dynamic_info[DT_INIT]);
  1123. #if defined (__SUPPORT_LD_DEBUG__)
  1124. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\ncalling init: %s\n\n", tpnt->libname);
  1125. #endif
  1126. (*_dl_elf_init) ();
  1127. }
  1128. if (_dl_atexit && tpnt->dynamic_info[DT_FINI]) {
  1129. (*_dl_atexit) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  1130. #if defined (__SUPPORT_LD_DEBUG__)
  1131. if(_dl_debug && _dl_on_exit)
  1132. {
  1133. (*_dl_on_exit)(debug_fini, tpnt->libname);
  1134. }
  1135. #endif
  1136. }
  1137. #if defined (__SUPPORT_LD_DEBUG__)
  1138. else {
  1139. if (!_dl_atexit)
  1140. _dl_dprintf(_dl_debug_file, "%s: The address of atexit () is 0x0.\n", tpnt->libname);
  1141. #if 0
  1142. if (!tpnt->dynamic_info[DT_FINI])
  1143. _dl_dprintf(_dl_debug_file, "%s: Invalid .fini section.\n", tpnt->libname);
  1144. #endif
  1145. }
  1146. #endif
  1147. }
  1148. }
  1149. /*
  1150. * This stub function is used by some debuggers. The idea is that they
  1151. * can set an internal breakpoint on it, so that we are notified when the
  1152. * address mapping is changed in some way.
  1153. */
  1154. void _dl_debug_state(void)
  1155. {
  1156. }
  1157. int _dl_fixup(struct elf_resolve *tpnt, int flag)
  1158. {
  1159. int goof = 0;
  1160. if (tpnt->next)
  1161. goof += _dl_fixup(tpnt->next, flag);
  1162. #if defined (__SUPPORT_LD_DEBUG__)
  1163. if(_dl_debug) _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
  1164. #endif
  1165. if (tpnt->dynamic_info[DT_REL]) {
  1166. #ifdef ELF_USES_RELOCA
  1167. #if defined (__SUPPORT_LD_DEBUG__)
  1168. if(_dl_debug) _dl_dprintf(2, "%s: can't handle REL relocation records\n", _dl_progname);
  1169. #endif
  1170. goof++;
  1171. return goof;
  1172. #else
  1173. if (tpnt->init_flag & RELOCS_DONE)
  1174. return goof;
  1175. tpnt->init_flag |= RELOCS_DONE;
  1176. goof += _dl_parse_relocation_information(tpnt,
  1177. tpnt->dynamic_info[DT_REL],
  1178. tpnt->dynamic_info[DT_RELSZ], 0);
  1179. #endif
  1180. }
  1181. if (tpnt->dynamic_info[DT_RELA]) {
  1182. #ifndef ELF_USES_RELOCA
  1183. #if defined (__SUPPORT_LD_DEBUG__)
  1184. if(_dl_debug) _dl_dprintf(2, "%s: can't handle RELA relocation records\n", _dl_progname);
  1185. #endif
  1186. goof++;
  1187. return goof;
  1188. #else
  1189. if (tpnt->init_flag & RELOCS_DONE)
  1190. return goof;
  1191. tpnt->init_flag |= RELOCS_DONE;
  1192. goof += _dl_parse_relocation_information(tpnt,
  1193. tpnt->dynamic_info[DT_RELA],
  1194. tpnt->dynamic_info[DT_RELASZ], 0);
  1195. #endif
  1196. }
  1197. if (tpnt->dynamic_info[DT_JMPREL]) {
  1198. if (tpnt->init_flag & JMP_RELOCS_DONE)
  1199. return goof;
  1200. tpnt->init_flag |= JMP_RELOCS_DONE;
  1201. if (flag & RTLD_LAZY) {
  1202. _dl_parse_lazy_relocation_information(tpnt,
  1203. tpnt->dynamic_info[DT_JMPREL],
  1204. tpnt->dynamic_info [DT_PLTRELSZ], 0);
  1205. } else {
  1206. goof += _dl_parse_relocation_information(tpnt,
  1207. tpnt->dynamic_info[DT_JMPREL],
  1208. tpnt->dynamic_info[DT_PLTRELSZ], 0);
  1209. }
  1210. }
  1211. #if defined (__SUPPORT_LD_DEBUG__)
  1212. if(_dl_debug) {
  1213. _dl_dprintf(_dl_debug_file,"\nrelocation processing: %s", tpnt->libname);
  1214. _dl_dprintf(_dl_debug_file,"; finished\n\n");
  1215. }
  1216. #endif
  1217. return goof;
  1218. }
  1219. char *_dl_getenv(const char *symbol, char **envp)
  1220. {
  1221. char *pnt;
  1222. const char *pnt1;
  1223. while ((pnt = *envp++)) {
  1224. pnt1 = symbol;
  1225. while (*pnt && *pnt == *pnt1)
  1226. pnt1++, pnt++;
  1227. if (!*pnt || *pnt != '=' || *pnt1)
  1228. continue;
  1229. return pnt + 1;
  1230. }
  1231. return 0;
  1232. }
  1233. void _dl_unsetenv(const char *symbol, char **envp)
  1234. {
  1235. char *pnt;
  1236. const char *pnt1;
  1237. char **newenvp = envp;
  1238. for (pnt = *envp; pnt; pnt = *++envp) {
  1239. pnt1 = symbol;
  1240. while (*pnt && *pnt == *pnt1)
  1241. pnt1++, pnt++;
  1242. if (!*pnt || *pnt != '=' || *pnt1)
  1243. *newenvp++ = *envp;
  1244. }
  1245. *newenvp++ = *envp;
  1246. return;
  1247. }
  1248. #include "hash.c"
  1249. #include "readelflib1.c"