ldso.c 45 KB

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