ldso.c 42 KB

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