ldso.c 43 KB

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