boot1.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. /* Run an ELF binary on a linux system.
  2. Copyright (C) 1993-1996, Eric Youngdale.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2, or (at your option)
  6. any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
  14. /* Program to load an ELF binary on a linux system, and run it.
  15. * References to symbols in sharable libraries can be resolved by
  16. * an ELF sharable library. */
  17. /* Disclaimer: I have never seen any AT&T source code for SVr4, nor have
  18. I ever taken any courses on internals. This program was developed using
  19. information available through the book "UNIX SYSTEM V RELEASE 4,
  20. Programmers guide: Ansi C and Programming Support Tools", which did
  21. a more than adequate job of explaining everything required to get this
  22. working. */
  23. /*
  24. * The main trick with this program is that initially, we ourselves are not
  25. * dynamicly linked. This means that we cannot access any global variables
  26. * since the GOT is initialized by the linker assuming a virtual address of 0,
  27. * and we cannot call any functions since the PLT is not initialized at all
  28. * (it will tend to want to call the dynamic linker
  29. *
  30. * There are further restrictions - we cannot use large switch statements,
  31. * since the compiler generates tables of addresses and jumps through them.
  32. * We can use inline functions, because these do not transfer control to
  33. * a new address, but they must be static so that they are not exported
  34. * from the modules. We cannot use normal syscall stubs, because these
  35. * all reference the errno global variable which is not yet initialized.
  36. * We can use all of the local stack variables that we want, since these
  37. * are all referenced to %ebp or %esp.
  38. *
  39. * Life is further complicated by the fact that initially we do not want
  40. * to do a complete dynamic linking. We want to allow the user to supply
  41. * new functions replacing some of the library versions, and until we have
  42. * the list of modules that we should search set up, we do not want to do
  43. * any of this. Thus I have chosen to only perform the relocations for
  44. * variables that start with "_dl_" since ANSI specifies that the user is
  45. * not supposed to redefine any of these variables.
  46. *
  47. * Fortunately, the linker itself leaves a few clues lying around, and
  48. * when the kernel starts the image, there are a few further clues.
  49. * First of all, there is information buried on the stack that the kernel
  50. * leaves, which includes information about the load address that the
  51. * program interpreter was loaded at, the number of sections, the address
  52. * the application was loaded at and so forth. Here this information
  53. * is stored in the array dl_info, and the indicies are taken from the
  54. * file /usr/include/sys/auxv.h on any SVr4 system.
  55. *
  56. * The linker itself leaves a pointer to the .dynamic section in the first
  57. * slot of the GOT, and as it turns out, %ebx points to ghe GOT when
  58. * you are using PIC code, so we just dereference this to get the address
  59. * of the dynamic sections.
  60. *
  61. * Typically you must load all text pages as writable so that dynamic linking
  62. * can succeed. The kernel under SVr4 loads these as R/O, so we must call
  63. * mprotect to change the protections. Once we are done, we should set these
  64. * back again, so the desired behavior is achieved. Under linux there is
  65. * currently no mprotect function in the distribution kernel (although
  66. * someone has alpha patches), so for now everything is loaded writable.
  67. *
  68. * We do not have access to malloc and friends at the initial stages of dynamic
  69. * linking, and it would be handy to have some scratchpad memory available
  70. * for use as we set things up. It is a bit of a kluge, but we mmap /dev/zero
  71. * to get one page of scratchpad. A simpleminded _dl_malloc is provided so
  72. * that we have some memory that can be used for this purpose. Typically
  73. * we would not want to use the same memory pool as malloc anyway - the user
  74. * might want to redefine malloc for example.
  75. *
  76. * Our first task is to perform a minimal linking so that we can call other
  77. * portions of the dynamic linker. Once we have done this, we then build
  78. * the list of modules that the application requires, using LD_LIBRARY_PATH
  79. * if this is not a suid program (/usr/lib otherwise). Once this is done,
  80. * we can do the dynamic linking as required (and we must omit the things
  81. * we did to get the dynamic linker up and running in the first place.
  82. * After we have done this, we just have a few housekeeping chores and we
  83. * can transfer control to the user's application.
  84. */
  85. #include <stdarg.h>
  86. #include <linux/types.h>
  87. #include <linux/fcntl.h>
  88. #include <linux/unistd.h>
  89. #include <linux/elf.h>
  90. #include <linux/mman.h>
  91. #include "link.h"
  92. #include "sysdep.h"
  93. #include "hash.h"
  94. #include "linuxelf.h"
  95. #include "syscall.h"
  96. #include "string.h"
  97. #include "../config.h"
  98. #define ALLOW_ZERO_PLTGOT
  99. static char * _dl_malloc_addr, *_dl_mmap_zero;
  100. char * _dl_library_path = 0; /* Where we look for libraries */
  101. char *_dl_preload = 0; /* Things to be loaded before the libs. */
  102. char *_dl_progname = "/lib/ld-linux-uclibc.so.1";
  103. static char * _dl_not_lazy = 0;
  104. static char * _dl_warn = 0; /* Used by ldd */
  105. static char * _dl_trace_loaded_objects = 0;
  106. static int (*_dl_elf_main)(int, char **, char**);
  107. static int (*_dl_elf_init)(void);
  108. void * (*_dl_malloc_function)(int size) = NULL;
  109. struct r_debug * _dl_debug_addr = NULL;
  110. unsigned int * _dl_brkp;
  111. unsigned int * _dl_envp;
  112. #define DL_MALLOC(SIZE) ((void *) (malloc_buffer += SIZE, malloc_buffer - SIZE))
  113. /*
  114. * Make sure that the malloc buffer is aligned on 4 byte boundary. For 64 bit
  115. * platforms we may need to increase this to 8, but this is good enough for
  116. * now. This is typically called after DL_MALLOC.
  117. */
  118. #define REALIGN() malloc_buffer = (char *) (((unsigned int) malloc_buffer + 3) & ~(3))
  119. #define ELF_HASH(RESULT,NAME) { \
  120. unsigned long hash = 0; \
  121. unsigned long tmp; \
  122. char * name = NAME; \
  123. while (*name){ \
  124. hash = (hash << 4) + *name++; \
  125. if((tmp = hash & 0xf0000000)) hash ^= tmp >> 24; \
  126. hash &= ~tmp; \
  127. } \
  128. RESULT = hash; \
  129. }
  130. extern int _dl_linux_resolve(void);
  131. extern int _dl_interpreter_exit(int);
  132. extern char * _dl_strdup(const char *);
  133. extern char * _dl_getenv(char * symbol, char ** envp);
  134. extern void _dl_unsetenv(char * symbol, char ** envp);
  135. extern int _dl_fixup(struct elf_resolve * tpnt);
  136. /*
  137. * Datatype of a relocation on this platform
  138. */
  139. #ifdef ELF_USES_RELOCA
  140. typedef struct elf32_rela ELF_RELOC;
  141. #else
  142. typedef struct elf32_rel ELF_RELOC;
  143. #endif
  144. /*
  145. * This stub function is used by some debuggers. The idea is that they
  146. * can set an internal breakpoint on it, so that we are notified when the
  147. * address mapping is changed in some way.
  148. */
  149. void _dl_debug_state()
  150. {
  151. return;
  152. }
  153. void _dl_boot(int args);
  154. void _dl_boot(int args){
  155. unsigned int argc;
  156. char ** argv, ** envp;
  157. int status;
  158. unsigned int load_addr;
  159. unsigned int * got;
  160. unsigned int * aux_dat;
  161. int goof = 0;
  162. struct elfhdr * header;
  163. struct elf_resolve * tpnt;
  164. struct dyn_elf * rpnt;
  165. struct elf_resolve * app_tpnt;
  166. unsigned int brk_addr;
  167. unsigned int dl_data[AT_EGID+1];
  168. unsigned char * malloc_buffer, *mmap_zero;
  169. int (*_dl_atexit)(void *);
  170. int * lpnt;
  171. struct dynamic * dpnt;
  172. unsigned int *hash_addr;
  173. struct r_debug * debug_addr;
  174. unsigned int *chains;
  175. int indx;
  176. int _dl_secure;
  177. /* First obtain the information on the stack that tells us more about
  178. what binary is loaded, where it is loaded, etc, etc */
  179. GET_ARGV(aux_dat, args);
  180. argc = *(aux_dat - 1);
  181. argv = (char **) aux_dat;
  182. aux_dat += argc; /* Skip over the argv pointers */
  183. aux_dat++; /* Skip over NULL at end of argv */
  184. envp = (char **) aux_dat;
  185. while(*aux_dat) aux_dat++; /* Skip over the envp pointers */
  186. aux_dat++; /* Skip over NULL at end of envp */
  187. dl_data[AT_UID] = -1; /* check later to see if it is changed */
  188. while(*aux_dat)
  189. {
  190. unsigned int * ad1;
  191. ad1 = aux_dat + 1;
  192. if( *aux_dat <= AT_EGID ) dl_data[*aux_dat] = *ad1;
  193. aux_dat += 2;
  194. }
  195. /* Next, locate the GOT */
  196. load_addr = dl_data[AT_BASE];
  197. GET_GOT(got);
  198. dpnt = (struct dynamic *) (*got + load_addr);
  199. /* OK, time for another hack. Now call mmap to get a page of writable
  200. memory that can be used for a temporary malloc. We do not know brk
  201. yet, so we cannot use real malloc. */
  202. {
  203. /* This hack is to work around a suspected asm bug in gcc-2.7.0 */
  204. int zfileno;
  205. #define ZFILENO ((-1 & (~zfileno)) | zfileno)
  206. /*#define ZFILENO -1*/
  207. #ifndef MAP_ANONYMOUS
  208. #ifdef __sparc__
  209. #define MAP_ANONYMOUS 0x20
  210. #else
  211. #error MAP_ANONYMOUS not defined and suplementary value not known
  212. #endif
  213. #endif
  214. /* See if we need to relocate this address */
  215. mmap_zero = malloc_buffer = (unsigned char *) _dl_mmap((void*) 0, 4096,
  216. PROT_READ | PROT_WRITE,
  217. MAP_PRIVATE | MAP_ANONYMOUS, ZFILENO, 0);
  218. if(_dl_mmap_check_error(mmap_zero)) {
  219. SEND_STDERR("dl_boot: mmap of /dev/zero failed!\n");
  220. _dl_exit(13);
  221. }
  222. }
  223. tpnt = DL_MALLOC(sizeof(struct elf_resolve));
  224. REALIGN();
  225. _dl_memset (tpnt, 0, sizeof (*tpnt));
  226. app_tpnt = DL_MALLOC(sizeof(struct elf_resolve));
  227. REALIGN();
  228. _dl_memset (app_tpnt, 0, sizeof (*app_tpnt));
  229. /*
  230. * This is used by gdb to locate the chain of shared libraries that are currently loaded.
  231. */
  232. debug_addr = DL_MALLOC(sizeof(struct r_debug));
  233. REALIGN();
  234. _dl_memset (debug_addr, 0, sizeof (*debug_addr));
  235. /* OK, that was easy. Next scan the DYNAMIC section of the image.
  236. We are only doing ourself right now - we will have to do the rest later */
  237. while(dpnt->d_tag)
  238. {
  239. tpnt->dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  240. if(dpnt->d_tag == DT_TEXTREL ||
  241. SVR4_BUGCOMPAT) tpnt->dynamic_info[DT_TEXTREL] = 1;
  242. dpnt++;
  243. }
  244. {
  245. struct elf_phdr * ppnt;
  246. int i;
  247. ppnt = (struct elf_phdr *) dl_data[AT_PHDR];
  248. for(i=0; i<dl_data[AT_PHNUM]; i++, ppnt++)
  249. if(ppnt->p_type == PT_DYNAMIC) {
  250. dpnt = (struct dynamic *) ppnt->p_vaddr;
  251. while(dpnt->d_tag)
  252. {
  253. if(dpnt->d_tag > DT_JMPREL) {dpnt++; continue; }
  254. app_tpnt->dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  255. if(dpnt->d_tag == DT_DEBUG) dpnt->d_un.d_val = (int) debug_addr;
  256. if(dpnt->d_tag == DT_TEXTREL ||
  257. SVR4_BUGCOMPAT) app_tpnt->dynamic_info[DT_TEXTREL] = 1;
  258. dpnt++;
  259. }
  260. }
  261. }
  262. /* Get some more of the information that we will need to dynamicly link
  263. this module to itself */
  264. hash_addr = (unsigned int *) (tpnt->dynamic_info[DT_HASH]+load_addr);
  265. tpnt->nbucket = *hash_addr++;
  266. tpnt->nchain = *hash_addr++;
  267. tpnt->elf_buckets = hash_addr;
  268. hash_addr += tpnt->nbucket;
  269. chains = hash_addr;
  270. /* Ugly, ugly. We need to call mprotect to change the protection of
  271. the text pages so that we can do the dynamic linking. We can set the
  272. protection back again once we are done */
  273. {
  274. struct elf_phdr * ppnt;
  275. int i;
  276. /* First cover the shared library/dynamic linker. */
  277. if(tpnt->dynamic_info[DT_TEXTREL]) {
  278. header = (struct elfhdr *) dl_data[AT_BASE];
  279. ppnt = (struct elf_phdr *) (dl_data[AT_BASE] + header->e_phoff);
  280. for(i=0; i<header->e_phnum ; i++, ppnt++) {
  281. if(ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  282. _dl_mprotect((void *) (load_addr + (ppnt->p_vaddr & 0xfffff000)),
  283. (ppnt->p_vaddr & 0xfff) + (unsigned int) ppnt->p_filesz,
  284. PROT_READ | PROT_WRITE | PROT_EXEC);
  285. }
  286. }
  287. /* Now cover the application program. */
  288. if(app_tpnt->dynamic_info[DT_TEXTREL]) {
  289. ppnt = (struct elf_phdr *) dl_data[AT_PHDR];
  290. for(i=0; i<dl_data[AT_PHNUM]; i++, ppnt++) {
  291. if(ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  292. _dl_mprotect((void *) (ppnt->p_vaddr & 0xfffff000),
  293. (ppnt->p_vaddr & 0xfff) + (unsigned int) ppnt->p_filesz,
  294. PROT_READ | PROT_WRITE | PROT_EXEC);
  295. }
  296. }
  297. }
  298. /* OK, now do the relocations. We do not do a lazy binding here, so
  299. that once we are done, we have considerably more flexibility. */
  300. goof = 0;
  301. for(indx=0; indx < 2; indx++)
  302. {
  303. int i;
  304. ELF_RELOC * rpnt;
  305. unsigned int * reloc_addr;
  306. unsigned int symbol_addr;
  307. int symtab_index;
  308. unsigned int rel_addr, rel_size;
  309. #ifdef ELF_USES_RELOCA
  310. rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] : tpnt->dynamic_info[DT_RELA]);
  311. rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] : tpnt->dynamic_info[DT_RELASZ]);
  312. #else
  313. rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] : tpnt->dynamic_info[DT_REL]);
  314. rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] : tpnt->dynamic_info[DT_RELSZ]);
  315. #endif
  316. if(!rel_addr) continue;
  317. /* Now parse the relocation information */
  318. rpnt = (ELF_RELOC *) (rel_addr + load_addr);
  319. for(i=0; i< rel_size; i+=sizeof(ELF_RELOC), rpnt++){
  320. reloc_addr = (int *) (load_addr + (int)rpnt->r_offset);
  321. symtab_index = ELF32_R_SYM(rpnt->r_info);
  322. symbol_addr = 0;
  323. if(symtab_index) {
  324. char * strtab;
  325. struct elf32_sym * symtab;
  326. symtab = (struct elf32_sym *) (tpnt->dynamic_info[DT_SYMTAB]+load_addr);
  327. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]+load_addr);
  328. /* We only do a partial dynamic linking right now. The user
  329. is not supposed to redefine any symbols that start with
  330. a '_', so we can do this with confidence. */
  331. if (!_dl_symbol(strtab + symtab[symtab_index].st_name)) continue;
  332. symbol_addr = load_addr + symtab[symtab_index].st_value;
  333. if(!symbol_addr) {
  334. /*
  335. * This will segfault - you cannot call a function until
  336. * we have finished the relocations.
  337. */
  338. SEND_STDERR("ELF dynamic loader - unable to self-bootstrap - symbol ");
  339. SEND_STDERR(strtab + symtab[symtab_index].st_name);
  340. SEND_STDERR(" undefined.\n");
  341. goof++;
  342. }
  343. }
  344. /*
  345. * Use this machine-specific macro to perform the actual relocation.
  346. */
  347. PERFORM_BOOTSTRAP_RELOC(rpnt, reloc_addr, symbol_addr, load_addr);
  348. }
  349. }
  350. if (goof) _dl_exit(14);
  351. /* OK, at this point we have a crude malloc capability. Start to build
  352. the tables of the modules that are required for this beast to run.
  353. We start with the basic executable, and then go from there. Eventually
  354. we will run across ourself, and we will need to properly deal with that
  355. as well. */
  356. _dl_malloc_addr = malloc_buffer;
  357. _dl_mmap_zero = mmap_zero;
  358. /* tpnt = _dl_malloc(sizeof(struct elf_resolve)); */
  359. /* Now we have done the mandatory linking of some things. We are now
  360. free to start using global variables, since these things have all been
  361. fixed up by now. Still no function calls outside of this library ,
  362. since the dynamic resolver is not yet ready. */
  363. lpnt = (int *) (tpnt->dynamic_info[DT_PLTGOT] + load_addr);
  364. INIT_GOT(lpnt, tpnt);
  365. /* OK, this was a big step, now we need to scan all of the user images
  366. and load them properly. */
  367. tpnt->next = 0;
  368. tpnt->libname = 0;
  369. tpnt->libtype = program_interpreter;
  370. { struct elfhdr * epnt;
  371. struct elf_phdr * ppnt;
  372. int i;
  373. epnt = (struct elfhdr *) dl_data[AT_BASE];
  374. tpnt->n_phent = epnt->e_phnum;
  375. tpnt->ppnt = ppnt = (struct elf_phdr *) (load_addr + epnt->e_phoff);
  376. for(i=0;i < epnt->e_phnum; i++, ppnt++){
  377. if(ppnt->p_type == PT_DYNAMIC) {
  378. tpnt->dynamic_addr = ppnt->p_vaddr + load_addr;
  379. tpnt->dynamic_size = ppnt->p_filesz;
  380. }
  381. }
  382. }
  383. tpnt->chains = chains;
  384. tpnt->loadaddr = (char *) load_addr;
  385. brk_addr = 0;
  386. rpnt = NULL;
  387. /* At this point we are now free to examine the user application,
  388. and figure out which libraries are supposed to be called. Until
  389. we have this list, we will not be completely ready for dynamic linking */
  390. {
  391. struct elf_phdr * ppnt;
  392. int i;
  393. ppnt = (struct elf_phdr *) dl_data[AT_PHDR];
  394. for(i=0; i<dl_data[AT_PHNUM]; i++, ppnt++) {
  395. if(ppnt->p_type == PT_LOAD) {
  396. if(ppnt->p_vaddr + ppnt->p_memsz > brk_addr)
  397. brk_addr = ppnt->p_vaddr + ppnt->p_memsz;
  398. }
  399. if(ppnt->p_type == PT_DYNAMIC) {
  400. #ifndef ALLOW_ZERO_PLTGOT
  401. /* make sure it's really there. */
  402. if (app_tpnt->dynamic_info[DT_PLTGOT] == 0) continue;
  403. #endif
  404. /* OK, we have what we need - slip this one into the list. */
  405. app_tpnt = _dl_add_elf_hash_table("", 0,
  406. app_tpnt->dynamic_info, ppnt->p_vaddr, ppnt->p_filesz);
  407. _dl_loaded_modules->libtype = elf_executable;
  408. _dl_loaded_modules->ppnt = (struct elf_phdr *) dl_data[AT_PHDR];
  409. _dl_loaded_modules->n_phent = dl_data[AT_PHNUM];
  410. _dl_symbol_tables = rpnt =
  411. (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  412. _dl_memset (rpnt, 0, sizeof (*rpnt));
  413. rpnt->dyn = _dl_loaded_modules;
  414. app_tpnt->usage_count++;
  415. app_tpnt->symbol_scope = _dl_symbol_tables;
  416. lpnt = (int *) (app_tpnt->dynamic_info[DT_PLTGOT]);
  417. #ifdef ALLOW_ZERO_PLTGOT
  418. if (lpnt)
  419. #endif
  420. INIT_GOT(lpnt, _dl_loaded_modules);
  421. }
  422. if(ppnt->p_type == PT_INTERP) { /* OK, fill this in - we did not have
  423. this before */
  424. tpnt->libname = _dl_strdup((char *) ppnt->p_offset +(dl_data[AT_PHDR] & 0xfffff000));
  425. }
  426. }
  427. }
  428. if (argv[0])
  429. _dl_progname = argv[0];
  430. /* Now we need to figure out what kind of options are selected.
  431. Note that for SUID programs we ignore the settings in LD_LIBRARY_PATH */
  432. {
  433. _dl_not_lazy = _dl_getenv("LD_BIND_NOW",envp);
  434. if ( (dl_data[AT_UID] == -1 && _dl_suid_ok()) ||
  435. (dl_data[AT_UID] != -1 && dl_data[AT_UID] == dl_data[AT_EUID] &&
  436. dl_data[AT_GID] == dl_data[AT_EGID]))
  437. {
  438. _dl_secure = 0;
  439. _dl_preload = _dl_getenv("LD_PRELOAD", envp);
  440. _dl_library_path = _dl_getenv("LD_LIBRARY_PATH",envp);
  441. }
  442. else
  443. {
  444. _dl_secure = 1;
  445. _dl_preload = _dl_getenv("LD_PRELOAD", envp);
  446. _dl_unsetenv("LD_AOUT_PRELOAD", envp);
  447. _dl_unsetenv("LD_LIBRARY_PATH", envp);
  448. _dl_unsetenv("LD_AOUT_LIBRARY_PATH", envp);
  449. _dl_library_path = NULL;
  450. }
  451. }
  452. _dl_trace_loaded_objects = _dl_getenv("LD_TRACE_LOADED_OBJECTS", envp);
  453. /* OK, we now have the application in the list, and we have some
  454. basic stuff in place. Now search through the list for other shared
  455. libraries that should be loaded, and insert them on the list in the
  456. correct order. */
  457. #ifdef USE_CACHE
  458. _dl_map_cache();
  459. #endif
  460. {
  461. struct elf_resolve *tcurr;
  462. struct elf_resolve *tpnt1;
  463. char *lpnt;
  464. if (_dl_preload) {
  465. char c, *str, *str2;
  466. str = _dl_preload;
  467. while (*str == ':' || *str == ' ' || *str == '\t')
  468. str++;
  469. while (*str) {
  470. str2 = str;
  471. while (*str2 && *str2 != ':' && *str2 != ' ' && *str2 != '\t')
  472. str2++;
  473. c = *str2;
  474. *str2 = '\0';
  475. if (!_dl_secure || _dl_strchr(str, '/') == NULL) {
  476. tpnt1 = _dl_load_shared_library(_dl_secure, NULL, str);
  477. if (!tpnt1) {
  478. if (_dl_trace_loaded_objects)
  479. _dl_fdprintf(1, "\t%s => not found\n", str);
  480. else {
  481. _dl_fdprintf(2, "%s: can't load library '%s'\n",
  482. _dl_progname, str);
  483. _dl_exit(15);
  484. }
  485. } else {
  486. if (_dl_trace_loaded_objects && !tpnt1->usage_count) {
  487. /* this is a real hack to make ldd not print the
  488. library itself when run on a library. */
  489. if (_dl_strcmp(_dl_progname, str) != 0)
  490. _dl_fdprintf(1, "\t%s => %s (0x%x)\n", str, tpnt1->libname,
  491. (unsigned)tpnt1->loadaddr);
  492. }
  493. rpnt->next =
  494. (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  495. _dl_memset (rpnt->next, 0, sizeof (*(rpnt->next)));
  496. rpnt = rpnt->next;
  497. tpnt1->usage_count++;
  498. tpnt1->symbol_scope = _dl_symbol_tables;
  499. tpnt1->libtype = elf_lib;
  500. rpnt->dyn = tpnt1;
  501. }
  502. }
  503. *str2 = c;
  504. str = str2;
  505. while (*str == ':' || *str == ' ' || *str == '\t')
  506. str++;
  507. }
  508. }
  509. {
  510. int fd;
  511. struct kernel_stat st;
  512. char *preload;
  513. if (!_dl_stat(LDSO_PRELOAD, &st)) {
  514. if ((fd = _dl_open(LDSO_PRELOAD, O_RDONLY)) < 0) {
  515. _dl_fdprintf(2, "%s: can't open file '%s'\n", _dl_progname,
  516. LDSO_PRELOAD);
  517. } else {
  518. preload = (caddr_t)_dl_mmap(0, st.st_size+1, PROT_READ|PROT_WRITE,
  519. MAP_PRIVATE, fd, 0);
  520. _dl_close (fd);
  521. if (preload == (caddr_t)-1) {
  522. _dl_fdprintf(2, "%s: can't map file '%s'\n", _dl_progname,
  523. LDSO_PRELOAD);
  524. } else {
  525. char c, *cp, *cp2;
  526. /* convert all separators and comments to spaces */
  527. for (cp = preload; *cp; /*nada*/) {
  528. if (*cp == ':' || *cp == '\t' || *cp == '\n') {
  529. *cp++ = ' ';
  530. } else if (*cp == '#') {
  531. do
  532. *cp++ = ' ';
  533. while (*cp != '\n' && *cp != '\0');
  534. } else {
  535. cp++;
  536. }
  537. }
  538. /* find start of first library */
  539. for (cp = preload; *cp && *cp == ' '; cp++)
  540. /*nada*/;
  541. while (*cp) {
  542. /* find end of library */
  543. for (cp2 = cp; *cp && *cp != ' '; cp++)
  544. /*nada*/;
  545. c = *cp;
  546. *cp = '\0';
  547. tpnt1 = _dl_load_shared_library(0, NULL, cp2);
  548. if (!tpnt1) {
  549. if (_dl_trace_loaded_objects)
  550. _dl_fdprintf(1, "\t%s => not found\n", cp2);
  551. else {
  552. _dl_fdprintf(2, "%s: can't load library '%s'\n",
  553. _dl_progname, cp2);
  554. _dl_exit(15);
  555. }
  556. } else {
  557. if (_dl_trace_loaded_objects && !tpnt1->usage_count)
  558. _dl_fdprintf(1, "\t%s => %s (0x%x)\n", cp2, tpnt1->libname,
  559. (unsigned)tpnt1->loadaddr);
  560. rpnt->next =
  561. (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  562. _dl_memset (rpnt->next, 0, sizeof (*(rpnt->next)));
  563. rpnt = rpnt->next;
  564. tpnt1->usage_count++;
  565. tpnt1->symbol_scope = _dl_symbol_tables;
  566. tpnt1->libtype = elf_lib;
  567. rpnt->dyn = tpnt1;
  568. }
  569. /* find start of next library */
  570. *cp = c;
  571. for (/*nada*/; *cp && *cp == ' '; cp++)
  572. /*nada*/;
  573. }
  574. _dl_munmap(preload, st.st_size+1);
  575. }
  576. }
  577. }
  578. }
  579. for (tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next)
  580. {
  581. for (dpnt = (struct dynamic *)tcurr->dynamic_addr; dpnt->d_tag; dpnt++)
  582. {
  583. if(dpnt->d_tag == DT_NEEDED)
  584. {
  585. lpnt = tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  586. dpnt->d_un.d_val;
  587. if (tpnt && _dl_strcmp(lpnt, tpnt->libname) == 0)
  588. {
  589. struct elf_resolve * ttmp;
  590. ttmp = _dl_loaded_modules;
  591. while (ttmp->next)
  592. ttmp = ttmp->next;
  593. ttmp->next = tpnt;
  594. tpnt->prev = ttmp;
  595. tpnt->next = NULL;
  596. rpnt->next =
  597. (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  598. _dl_memset (rpnt->next, 0, sizeof (*(rpnt->next)));
  599. rpnt = rpnt->next;
  600. rpnt->dyn = tpnt;
  601. tpnt->usage_count++;
  602. tpnt->symbol_scope = _dl_symbol_tables;
  603. tpnt = NULL;
  604. continue;
  605. }
  606. if (!(tpnt1 = _dl_load_shared_library(0, tcurr, lpnt)))
  607. {
  608. if (_dl_trace_loaded_objects)
  609. _dl_fdprintf(1, "\t%s => not found\n", lpnt);
  610. else
  611. {
  612. _dl_fdprintf(2, "%s: can't load library '%s'\n",
  613. _dl_progname, lpnt);
  614. _dl_exit(16);
  615. }
  616. }
  617. else
  618. {
  619. if (_dl_trace_loaded_objects && !tpnt1->usage_count)
  620. _dl_fdprintf(1, "\t%s => %s (0x%x)\n", lpnt, tpnt1->libname,
  621. (unsigned)tpnt1->loadaddr);
  622. rpnt->next =
  623. (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  624. _dl_memset (rpnt->next, 0, sizeof (*(rpnt->next)));
  625. rpnt = rpnt->next;
  626. tpnt1->usage_count++;
  627. tpnt1->symbol_scope = _dl_symbol_tables;
  628. tpnt1->libtype = elf_lib;
  629. rpnt->dyn = tpnt1;
  630. }
  631. }
  632. }
  633. }
  634. }
  635. #ifdef USE_CACHE
  636. _dl_unmap_cache();
  637. #endif
  638. /* ldd uses uses this. I am not sure how you pick up the other flags */
  639. if(_dl_trace_loaded_objects)
  640. {
  641. _dl_warn = _dl_getenv("LD_WARN", envp);
  642. if (!_dl_warn) _dl_exit(0);
  643. }
  644. /*
  645. * If the program interpreter is not in the module chain, add it. This will
  646. * be required for dlopen to be able to access the internal functions in the
  647. * dynamic linker.
  648. */
  649. if(tpnt) {
  650. struct elf_resolve * tcurr;
  651. tcurr = _dl_loaded_modules;
  652. if (tcurr)
  653. while(tcurr->next) tcurr = tcurr->next;
  654. tpnt->next = NULL;
  655. tpnt->usage_count++;
  656. if (tcurr) {
  657. tcurr->next = tpnt;
  658. tpnt->prev = tcurr;
  659. }
  660. else {
  661. _dl_loaded_modules = tpnt;
  662. tpnt->prev = NULL;
  663. }
  664. if (rpnt) {
  665. rpnt->next =
  666. (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  667. _dl_memset (rpnt->next, 0, sizeof (*(rpnt->next)));
  668. rpnt = rpnt->next;
  669. } else {
  670. rpnt = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  671. _dl_memset (rpnt, 0, sizeof (*(rpnt->next)));
  672. }
  673. rpnt->dyn = tpnt;
  674. tpnt = NULL;
  675. }
  676. /*
  677. * OK, now all of the kids are tucked into bed in their proper addresses.
  678. * Now we go through and look for REL and RELA records that indicate fixups
  679. * to the GOT tables. We need to do this in reverse order so that COPY
  680. * directives work correctly */
  681. goof = _dl_loaded_modules ? _dl_fixup(_dl_loaded_modules) : 0;
  682. /* Some flavors of SVr4 do not generate the R_*_COPY directive,
  683. and we have to manually search for entries that require fixups.
  684. Solaris gets this one right, from what I understand. */
  685. if (_dl_symbol_tables)
  686. goof += _dl_copy_fixups(_dl_symbol_tables);
  687. if(goof || _dl_trace_loaded_objects) _dl_exit(0);
  688. /* OK, at this point things are pretty much ready to run. Now we
  689. need to touch up a few items that are required, and then
  690. we can let the user application have at it. Note that
  691. the dynamic linker itself is not guaranteed to be fully
  692. dynamicly linked if we are using ld.so.1, so we have to look
  693. up each symbol individually. */
  694. _dl_brkp = (unsigned int *) _dl_find_hash("___brk_addr", NULL, 1, NULL, 0);
  695. if (_dl_brkp) *_dl_brkp = brk_addr;
  696. _dl_envp = (unsigned int *) _dl_find_hash("__environ", NULL, 1, NULL, 0);
  697. if (_dl_envp) *_dl_envp = (unsigned int) envp;
  698. {
  699. int i;
  700. struct elf_phdr * ppnt;
  701. /* We had to set the protections of all pages to R/W for dynamic linking.
  702. Set text pages back to R/O */
  703. for(tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  704. for(ppnt = tpnt->ppnt, i=0; i < tpnt->n_phent; i++, ppnt++)
  705. if(ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W) &&
  706. tpnt->dynamic_info[DT_TEXTREL])
  707. _dl_mprotect((void *) (tpnt->loadaddr + (ppnt->p_vaddr & 0xfffff000)),
  708. (ppnt->p_vaddr & 0xfff) + (unsigned int) ppnt->p_filesz,
  709. LXFLAGS(ppnt->p_flags));
  710. }
  711. _dl_atexit = (int (*)(void *)) _dl_find_hash("atexit", NULL, 1, NULL, 0);
  712. /*
  713. * OK, fix one more thing - set up the debug_addr structure to point
  714. * to our chain. Later we may need to fill in more fields, but this
  715. * should be enough for now.
  716. */
  717. debug_addr->r_map = (struct link_map *) _dl_loaded_modules;
  718. debug_addr->r_version = 1;
  719. debug_addr->r_ldbase = load_addr;
  720. debug_addr->r_brk = (unsigned long) &_dl_debug_state;
  721. _dl_debug_addr = debug_addr;
  722. debug_addr->r_state = RT_CONSISTENT;
  723. /* This is written in this funny way to keep gcc from inlining the
  724. function call. */
  725. ((void (*)(void))debug_addr->r_brk)();
  726. for(tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  727. {
  728. /* Apparently crt1 for the application is responsible for handling this.
  729. * We only need to run the init/fini for shared libraries
  730. */
  731. if (tpnt->libtype == program_interpreter ||
  732. tpnt->libtype == elf_executable) continue;
  733. if (tpnt->init_flag & INIT_FUNCS_CALLED) continue;
  734. tpnt->init_flag |= INIT_FUNCS_CALLED;
  735. if(tpnt->dynamic_info[DT_INIT]) {
  736. _dl_elf_init = (int (*)(void)) (tpnt->loadaddr +
  737. tpnt->dynamic_info[DT_INIT]);
  738. (*_dl_elf_init)();
  739. }
  740. if(_dl_atexit && tpnt->dynamic_info[DT_FINI])
  741. {
  742. (*_dl_atexit)(tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  743. }
  744. #undef DL_DEBUG
  745. #ifdef DL_DEBUG
  746. else
  747. {
  748. _dl_fdprintf(2, tpnt->libname);
  749. _dl_fdprintf(2, ": ");
  750. if (!_dl_atexit)
  751. _dl_fdprintf(2, "The address is atexit () is 0x0.");
  752. if (!tpnt->dynamic_info[DT_FINI])
  753. _dl_fdprintf(2, "Invalid .fini section.");
  754. _dl_fdprintf(2, "\n");
  755. }
  756. #endif
  757. #undef DL_DEBUG
  758. }
  759. /* OK we are done here. Turn out the lights, and lock up. */
  760. _dl_elf_main = (int (*)(int, char**, char**)) dl_data[AT_ENTRY];
  761. /*
  762. * Transfer control to the application.
  763. */
  764. START();
  765. }
  766. int _dl_fixup(struct elf_resolve * tpnt)
  767. {
  768. int goof = 0;
  769. if(tpnt->next) goof += _dl_fixup(tpnt->next);
  770. if(tpnt->dynamic_info[DT_REL]) {
  771. #ifdef ELF_USES_RELOCA
  772. _dl_fdprintf(2, "%s: can't handle REL relocation records\n", _dl_progname);
  773. _dl_exit(17);
  774. #else
  775. if (tpnt->init_flag & RELOCS_DONE) return goof;
  776. tpnt->init_flag |= RELOCS_DONE;
  777. goof += _dl_parse_relocation_information(tpnt, tpnt->dynamic_info[DT_REL],
  778. tpnt->dynamic_info[DT_RELSZ], 0);
  779. #endif
  780. }
  781. if(tpnt->dynamic_info[DT_RELA]) {
  782. #ifdef ELF_USES_RELOCA
  783. if (tpnt->init_flag & RELOCS_DONE) return goof;
  784. tpnt->init_flag |= RELOCS_DONE;
  785. goof += _dl_parse_relocation_information(tpnt, tpnt->dynamic_info[DT_RELA],
  786. tpnt->dynamic_info[DT_RELASZ], 0);
  787. #else
  788. _dl_fdprintf(2, "%s: can't handle RELA relocation records\n", _dl_progname);
  789. _dl_exit(18);
  790. #endif
  791. }
  792. if(tpnt->dynamic_info[DT_JMPREL])
  793. {
  794. if (tpnt->init_flag & JMP_RELOCS_DONE) return goof;
  795. tpnt->init_flag |= JMP_RELOCS_DONE;
  796. if(! _dl_not_lazy || *_dl_not_lazy == 0)
  797. _dl_parse_lazy_relocation_information(tpnt, tpnt->dynamic_info[DT_JMPREL],
  798. tpnt->dynamic_info[DT_PLTRELSZ], 0);
  799. else
  800. goof += _dl_parse_relocation_information(tpnt,
  801. tpnt->dynamic_info[DT_JMPREL],
  802. tpnt->dynamic_info[DT_PLTRELSZ], 0);
  803. }
  804. return goof;
  805. }
  806. void * _dl_malloc(int size) {
  807. void * retval;
  808. if(_dl_malloc_function)
  809. return (*_dl_malloc_function)(size);
  810. if(_dl_malloc_addr-_dl_mmap_zero+size>4096) {
  811. _dl_mmap_zero = _dl_malloc_addr = (unsigned char *) _dl_mmap((void*) 0, size,
  812. PROT_READ | PROT_WRITE,
  813. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  814. if(_dl_mmap_check_error(_dl_mmap_zero)) {
  815. _dl_fdprintf(2, "%s: can't map '/dev/zero'\n", _dl_progname);
  816. _dl_exit(20);
  817. }
  818. }
  819. retval = _dl_malloc_addr;
  820. _dl_malloc_addr += size;
  821. /*
  822. * Align memory to 4 byte boundary. Some platforms require this, others
  823. * simply get better performance.
  824. */
  825. _dl_malloc_addr = (char *) (((unsigned int) _dl_malloc_addr + 3) & ~(3));
  826. return retval;
  827. }
  828. char * _dl_getenv(char *symbol, char **envp)
  829. {
  830. char *pnt;
  831. char *pnt1;
  832. while ((pnt = *envp++)) {
  833. pnt1 = symbol;
  834. while (*pnt && *pnt == *pnt1)
  835. pnt1++, pnt++;
  836. if (!*pnt || *pnt != '=' || *pnt1)
  837. continue;
  838. return pnt+1;
  839. }
  840. return 0;
  841. }
  842. void _dl_unsetenv(char *symbol, char **envp)
  843. {
  844. char *pnt;
  845. char *pnt1;
  846. char **newenvp = envp;
  847. for (pnt = *envp; pnt; pnt = *++envp) {
  848. pnt1 = symbol;
  849. while (*pnt && *pnt == *pnt1)
  850. pnt1++, pnt++;
  851. if(!*pnt || *pnt != '=' || *pnt1)
  852. *newenvp++ = *envp;
  853. }
  854. *newenvp++ = *envp;
  855. return;
  856. }
  857. char * _dl_strdup(const char * string){
  858. char * retval;
  859. int len;
  860. len = _dl_strlen(string);
  861. retval = _dl_malloc(len + 1);
  862. _dl_strcpy(retval, string);
  863. return retval;
  864. }
  865. /* In principle we could do the .fini stuff here, but we already
  866. registered this stuff with atexit */
  867. int _dl_interpreter_exit(int exitcode){
  868. /* _dl_fdprintf(2, "Hey, look where I am!\n"); */
  869. return 0;
  870. }