ldso.c 39 KB

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