ldso.c 43 KB

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