ldso.c 44 KB

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