ldso.c 40 KB

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