ld-uClibc.c 30 KB

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