boot1.c 35 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189
  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 for
  70. * use as we set things up. We mmap one page of scratch space, and have a
  71. * simple _dl_malloc that uses this memory. This is a good thing, since we do
  72. * not want to use the same memory pool as malloc anyway - esp if the user
  73. * redefines malloc to do something funky.
  74. *
  75. * Our first task is to perform a minimal linking so that we can call other
  76. * portions of the dynamic linker. Once we have done this, we then build
  77. * the list of modules that the application requires, using LD_LIBRARY_PATH
  78. * if this is not a suid program (/usr/lib otherwise). Once this is done,
  79. * we can do the dynamic linking as required (and we must omit the things
  80. * we did to get the dynamic linker up and running in the first place.
  81. * After we have done this, we just have a few housekeeping chores and we
  82. * can transfer control to the user's application.
  83. */
  84. #include <stdarg.h>
  85. #include "sysdep.h" /* before elf.h to get ELF_USES_RELOCA right */
  86. #include <elf.h>
  87. #include "linuxelf.h"
  88. #include "hash.h"
  89. #include "syscall.h"
  90. #include "string.h"
  91. #include "../config.h"
  92. #define ALLOW_ZERO_PLTGOT
  93. /* Some arches may need to override this in boot1_arch.h */
  94. #define ELFMAGIC ELFMAG
  95. /* This is a poor man's malloc, used prior to resolving our internal poor man's malloc */
  96. #define DL_MALLOC(SIZE) ((void *) (malloc_buffer += SIZE, malloc_buffer - SIZE)) ; REALIGN();
  97. /*
  98. * Make sure that the malloc buffer is aligned on 4 byte boundary. For 64 bit
  99. * platforms we may need to increase this to 8, but this is good enough for
  100. * now. This is typically called after DL_MALLOC.
  101. */
  102. #define REALIGN() malloc_buffer = (char *) (((unsigned long) malloc_buffer + 3) & ~(3))
  103. static char *_dl_malloc_addr, *_dl_mmap_zero;
  104. char *_dl_library_path = 0; /* Where we look for libraries */
  105. char *_dl_preload = 0; /* Things to be loaded before the libs. */
  106. #include "ld.so.h" /* Pull in the name of ld.so */
  107. const char *_dl_progname=_dl_static_progname;
  108. static char *_dl_not_lazy = 0;
  109. #ifdef DL_TRACE
  110. static char *_dl_trace_loaded_objects = 0;
  111. #endif
  112. static int (*_dl_elf_main) (int, char **, char **);
  113. static int (*_dl_elf_init) (void);
  114. void *(*_dl_malloc_function) (int size) = NULL;
  115. struct r_debug *_dl_debug_addr = NULL;
  116. unsigned long *_dl_brkp;
  117. unsigned long *_dl_envp;
  118. char *_dl_getenv(char *symbol, char **envp);
  119. void _dl_unsetenv(char *symbol, char **envp);
  120. int _dl_fixup(struct elf_resolve *tpnt);
  121. void _dl_debug_state(void);
  122. char *_dl_get_last_path_component(char *path);
  123. #include "boot1_arch.h"
  124. /* When we enter this piece of code, the program stack looks like this:
  125. argc argument counter (integer)
  126. argv[0] program name (pointer)
  127. argv[1...N] program args (pointers)
  128. argv[argc-1] end of args (integer)
  129. NULL
  130. env[0...N] environment variables (pointers)
  131. NULL
  132. auxv_t[0...N] Auxiliary Vector Table elements (mixed types)
  133. */
  134. DL_BOOT(unsigned long args)
  135. {
  136. unsigned int argc;
  137. char **argv, **envp;
  138. unsigned long load_addr;
  139. unsigned long *got;
  140. unsigned long *aux_dat;
  141. int goof = 0;
  142. elfhdr *header;
  143. struct elf_resolve *tpnt;
  144. struct dyn_elf *rpnt;
  145. struct elf_resolve *app_tpnt;
  146. unsigned long brk_addr;
  147. Elf32_auxv_t auxv_t[AT_EGID + 1];
  148. unsigned char *malloc_buffer, *mmap_zero;
  149. int (*_dl_atexit) (void *);
  150. unsigned long *lpnt;
  151. Elf32_Dyn *dpnt;
  152. unsigned long *hash_addr;
  153. struct r_debug *debug_addr;
  154. unsigned long *chains;
  155. int indx;
  156. int _dl_secure;
  157. int status;
  158. /* WARNING! -- we cannot make _any_ funtion calls until we have
  159. * taken care of fixing up our own relocations. Making static
  160. * lnline calls is ok, but _no_ function calls. Not yet
  161. * anyways. */
  162. /* First obtain the information on the stack that tells us more about
  163. what binary is loaded, where it is loaded, etc, etc */
  164. GET_ARGV(aux_dat,args);
  165. #if defined(__arm__)
  166. aux_dat+=1;
  167. #endif
  168. argc = *(aux_dat - 1);
  169. argv = (char **) aux_dat;
  170. aux_dat += argc; /* Skip over the argv pointers */
  171. aux_dat++; /* Skip over NULL at end of argv */
  172. envp = (char **) aux_dat;
  173. while (*aux_dat)
  174. aux_dat++; /* Skip over the envp pointers */
  175. aux_dat++; /* Skip over NULL at end of envp */
  176. /* Place -1 here as a checkpoint. We later check if it was changed
  177. * when we read in the auxv_t */
  178. auxv_t[AT_UID].a_type = -1;
  179. /* The junk on the stack immediately following the environment is
  180. * the Auxiliary Vector Table. Read out the elements of the auxv_t,
  181. * sort and store them in auxv_t for later use. */
  182. while (*aux_dat)
  183. {
  184. Elf32_auxv_t *auxv_entry = (Elf32_auxv_t*) aux_dat;
  185. if (auxv_entry->a_type <= AT_EGID) {
  186. _dl_memcpy(&(auxv_t[auxv_entry->a_type]),
  187. auxv_entry, sizeof(Elf32_auxv_t));
  188. }
  189. aux_dat += 2;
  190. }
  191. /* locate the ELF header. We need this done as soon as possible
  192. * (esp since SEND_STDERR() needs this on some platforms... */
  193. load_addr = auxv_t[AT_BASE].a_un.a_val;
  194. header = (elfhdr *) auxv_t[AT_BASE].a_un.a_ptr;
  195. /* Check the ELF header to make sure everything looks ok. */
  196. if (! header || header->e_ident[EI_CLASS] != ELFCLASS32 ||
  197. header->e_ident[EI_VERSION] != EV_CURRENT ||
  198. _dl_strncmp((void *)header, ELFMAGIC, SELFMAG) != 0)
  199. {
  200. SEND_STDERR("Invalid ELF header\n");
  201. _dl_exit(0);
  202. }
  203. #ifdef DL_DEBUG
  204. SEND_STDERR("ELF header =");
  205. SEND_ADDRESS_STDERR(load_addr, 1);
  206. #endif
  207. /* Locate the global offset table. Since this code must be PIC
  208. * we can take advantage of the magic offset register, if we
  209. * happen to know what that is for this architecture. If not,
  210. * we can always read stuff out of the ELF file to find it... */
  211. #if defined(__i386__)
  212. __asm__("\tmovl %%ebx,%0\n\t" : "=a" (got));
  213. #elif defined(__m68k__)
  214. __asm__ ("movel %%a5,%0" : "=g" (got))
  215. #elif defined(__sparc__)
  216. __asm__("\tmov %%l7,%0\n\t" : "=r" (got))
  217. #elif defined(__arm__)
  218. __asm__("\tmov %0, r10\n\t" : "=r"(got));
  219. #elif defined(__powerpc__)
  220. __asm__("\tbl _GLOBAL_OFFSET_TABLE_-4@local\n\t" : "=l"(got));
  221. #else
  222. /* Do things the slow way in C */
  223. {
  224. unsigned long tx_reloc;
  225. Elf32_Dyn *dynamic=NULL;
  226. Elf32_Shdr *shdr;
  227. Elf32_Phdr *pt_load;
  228. #ifdef DL_DEBUG
  229. SEND_STDERR("Finding the got using C code to read the ELF file\n");
  230. #endif
  231. /* Find where the dynamic linking information section is hiding */
  232. shdr = (Elf32_Shdr *)(header->e_shoff + (char *)header);
  233. for (indx = header->e_shnum; --indx>=0; ++shdr) {
  234. if (shdr->sh_type == SHT_DYNAMIC) {
  235. goto found_dynamic;
  236. }
  237. }
  238. SEND_STDERR("missing dynamic linking information section \n");
  239. _dl_exit(0);
  240. found_dynamic:
  241. dynamic = (Elf32_Dyn*)(shdr->sh_offset + (char *)header);
  242. /* Find where PT_LOAD is hiding */
  243. pt_load = (Elf32_Phdr *)(header->e_phoff + (char *)header);
  244. for (indx = header->e_phnum; --indx>=0; ++pt_load) {
  245. if (pt_load->p_type == PT_LOAD) {
  246. goto found_pt_load;
  247. }
  248. }
  249. SEND_STDERR("missing loadable program segment\n");
  250. _dl_exit(0);
  251. found_pt_load:
  252. /* Now (finally) find where DT_PLTGOT is hiding */
  253. tx_reloc = pt_load->p_vaddr - pt_load->p_offset;
  254. for (; DT_NULL!=dynamic->d_tag; ++dynamic) {
  255. if (dynamic->d_tag == DT_PLTGOT) {
  256. goto found_got;
  257. }
  258. }
  259. SEND_STDERR("missing global offset table\n");
  260. _dl_exit(0);
  261. found_got:
  262. got = (unsigned long *)(dynamic->d_un.d_val - tx_reloc + (char *)header );
  263. }
  264. #endif
  265. /* Now, finally, fix up the location of the dynamic stuff */
  266. dpnt = (Elf32_Dyn *) (*got + load_addr);
  267. #ifdef DL_DEBUG
  268. SEND_STDERR("First Dynamic section entry=");
  269. SEND_ADDRESS_STDERR(dpnt, 1);
  270. #endif
  271. /* Call mmap to get a page of writable memory that can be used
  272. * for _dl_malloc throughout the shared lib loader. */
  273. mmap_zero = malloc_buffer = _dl_mmap((void *) 0, 4096,
  274. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  275. if (_dl_mmap_check_error(mmap_zero)) {
  276. SEND_STDERR("dl_boot: mmap of a spare page failed!\n");
  277. _dl_exit(13);
  278. }
  279. tpnt = DL_MALLOC(sizeof(struct elf_resolve));
  280. _dl_memset(tpnt, 0, sizeof(*tpnt));
  281. app_tpnt = DL_MALLOC(sizeof(struct elf_resolve));
  282. _dl_memset(app_tpnt, 0, sizeof(*app_tpnt));
  283. /*
  284. * This is used by gdb to locate the chain of shared libraries that are currently loaded.
  285. */
  286. debug_addr = DL_MALLOC(sizeof(struct r_debug));
  287. _dl_memset(debug_addr, 0, sizeof(*debug_addr));
  288. /* OK, that was easy. Next scan the DYNAMIC section of the image.
  289. We are only doing ourself right now - we will have to do the rest later */
  290. while (dpnt->d_tag) {
  291. tpnt->dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  292. if (dpnt->d_tag == DT_TEXTREL || SVR4_BUGCOMPAT)
  293. tpnt->dynamic_info[DT_TEXTREL] = 1;
  294. dpnt++;
  295. }
  296. {
  297. elf_phdr *ppnt;
  298. int i;
  299. ppnt = (elf_phdr *) auxv_t[AT_PHDR].a_un.a_ptr;
  300. for (i = 0; i < auxv_t[AT_PHNUM].a_un.a_val; i++, ppnt++)
  301. if (ppnt->p_type == PT_DYNAMIC) {
  302. dpnt = (Elf32_Dyn *) ppnt->p_vaddr;
  303. while (dpnt->d_tag) {
  304. if (dpnt->d_tag > DT_JMPREL) {
  305. dpnt++;
  306. continue;
  307. }
  308. app_tpnt->dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  309. if (dpnt->d_tag == DT_DEBUG)
  310. dpnt->d_un.d_val = (unsigned long) debug_addr;
  311. if (dpnt->d_tag == DT_TEXTREL || SVR4_BUGCOMPAT)
  312. app_tpnt->dynamic_info[DT_TEXTREL] = 1;
  313. dpnt++;
  314. }
  315. }
  316. }
  317. /* Get some more of the information that we will need to dynamicly link
  318. this module to itself */
  319. hash_addr = (unsigned long *) (tpnt->dynamic_info[DT_HASH] + load_addr);
  320. tpnt->nbucket = *hash_addr++;
  321. tpnt->nchain = *hash_addr++;
  322. tpnt->elf_buckets = hash_addr;
  323. hash_addr += tpnt->nbucket;
  324. chains = hash_addr;
  325. /* Ugly, ugly. We need to call mprotect to change the protection of
  326. the text pages so that we can do the dynamic linking. We can set the
  327. protection back again once we are done */
  328. {
  329. elf_phdr *ppnt;
  330. int i;
  331. /* First cover the shared library/dynamic linker. */
  332. if (tpnt->dynamic_info[DT_TEXTREL]) {
  333. header = (elfhdr *) auxv_t[AT_BASE].a_un.a_ptr;
  334. ppnt = (elf_phdr *) (auxv_t[AT_BASE].a_un.a_ptr + header->e_phoff);
  335. for (i = 0; i < header->e_phnum; i++, ppnt++) {
  336. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  337. _dl_mprotect((void *) (load_addr + (ppnt->p_vaddr & 0xfffff000)),
  338. (ppnt->p_vaddr & 0xfff) + (unsigned long) ppnt->p_filesz,
  339. PROT_READ | PROT_WRITE | PROT_EXEC);
  340. }
  341. }
  342. /* Now cover the application program. */
  343. if (app_tpnt->dynamic_info[DT_TEXTREL]) {
  344. ppnt = (elf_phdr *) auxv_t[AT_PHDR].a_un.a_ptr;
  345. for (i = 0; i < auxv_t[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  346. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  347. _dl_mprotect((void *) (ppnt->p_vaddr & 0xfffff000),
  348. (ppnt->p_vaddr & 0xfff) +
  349. (unsigned long) ppnt->p_filesz,
  350. PROT_READ | PROT_WRITE | PROT_EXEC);
  351. }
  352. }
  353. }
  354. /* OK, now do the relocations. We do not do a lazy binding here, so
  355. that once we are done, we have considerably more flexibility. */
  356. goof = 0;
  357. for (indx = 0; indx < 2; indx++) {
  358. int i;
  359. ELF_RELOC *rpnt;
  360. unsigned long *reloc_addr;
  361. unsigned long symbol_addr;
  362. int symtab_index;
  363. unsigned long rel_addr, rel_size;
  364. #ifdef ELF_USES_RELOCA
  365. rel_addr =
  366. (indx ? tpnt->dynamic_info[DT_JMPREL] : tpnt->
  367. dynamic_info[DT_RELA]);
  368. rel_size =
  369. (indx ? tpnt->dynamic_info[DT_PLTRELSZ] : tpnt->
  370. dynamic_info[DT_RELASZ]);
  371. #else
  372. rel_addr =
  373. (indx ? tpnt->dynamic_info[DT_JMPREL] : tpnt->
  374. dynamic_info[DT_REL]);
  375. rel_size =
  376. (indx ? tpnt->dynamic_info[DT_PLTRELSZ] : tpnt->
  377. dynamic_info[DT_RELSZ]);
  378. #endif
  379. if (!rel_addr)
  380. continue;
  381. /* Now parse the relocation information */
  382. rpnt = (ELF_RELOC *) (rel_addr + load_addr);
  383. for (i = 0; i < rel_size; i += sizeof(ELF_RELOC), rpnt++) {
  384. reloc_addr = (unsigned long *) (load_addr + (unsigned long) rpnt->r_offset);
  385. symtab_index = ELF32_R_SYM(rpnt->r_info);
  386. symbol_addr = 0;
  387. if (symtab_index) {
  388. char *strtab;
  389. Elf32_Sym *symtab;
  390. symtab = (Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + load_addr);
  391. strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + load_addr);
  392. /* We only do a partial dynamic linking right now. The user
  393. is not supposed to redefine any symbols that start with
  394. a '_', so we can do this with confidence. */
  395. if (!_dl_symbol(strtab + symtab[symtab_index].st_name))
  396. continue;
  397. symbol_addr = load_addr + symtab[symtab_index].st_value;
  398. if (!symbol_addr) {
  399. /*
  400. * This will segfault - you cannot call a function until
  401. * we have finished the relocations.
  402. */
  403. SEND_STDERR("ELF dynamic loader - unable to "
  404. "self-bootstrap - symbol ");
  405. SEND_STDERR(strtab + symtab[symtab_index].st_name);
  406. SEND_STDERR(" undefined.\n");
  407. goof++;
  408. }
  409. }
  410. /*
  411. * Use this machine-specific macro to perform the actual relocation.
  412. */
  413. PERFORM_BOOTSTRAP_RELOC(rpnt, reloc_addr, symbol_addr, load_addr);
  414. }
  415. }
  416. if (goof) {
  417. _dl_exit(14);
  418. }
  419. /* OK, at this point we have a crude malloc capability. Start to build
  420. the tables of the modules that are required for this beast to run.
  421. We start with the basic executable, and then go from there. Eventually
  422. we will run across ourself, and we will need to properly deal with that
  423. as well. */
  424. _dl_malloc_addr = malloc_buffer;
  425. _dl_mmap_zero = mmap_zero;
  426. /* Now we have done the mandatory linking of some things. We are now
  427. free to start using global variables, since these things have all been
  428. fixed up by now. Still no function calls outside of this library ,
  429. since the dynamic resolver is not yet ready. */
  430. lpnt = (unsigned long *) (tpnt->dynamic_info[DT_PLTGOT] + load_addr);
  431. INIT_GOT(lpnt, tpnt);
  432. /* OK, this was a big step, now we need to scan all of the user images
  433. and load them properly. */
  434. tpnt->next = 0;
  435. tpnt->libname = 0;
  436. tpnt->libtype = program_interpreter;
  437. {
  438. elfhdr *epnt;
  439. elf_phdr *ppnt;
  440. int i;
  441. epnt = (elfhdr *) auxv_t[AT_BASE].a_un.a_ptr;
  442. tpnt->n_phent = epnt->e_phnum;
  443. tpnt->ppnt = ppnt = (elf_phdr *) (load_addr + epnt->e_phoff);
  444. for (i = 0; i < epnt->e_phnum; i++, ppnt++) {
  445. if (ppnt->p_type == PT_DYNAMIC) {
  446. tpnt->dynamic_addr = ppnt->p_vaddr + load_addr;
  447. tpnt->dynamic_size = ppnt->p_filesz;
  448. }
  449. }
  450. }
  451. tpnt->chains = chains;
  452. tpnt->loadaddr = (char *) load_addr;
  453. brk_addr = 0;
  454. rpnt = NULL;
  455. /* At this point we are now free to examine the user application,
  456. and figure out which libraries are supposed to be called. Until
  457. we have this list, we will not be completely ready for dynamic linking */
  458. {
  459. elf_phdr *ppnt;
  460. int i;
  461. ppnt = (elf_phdr *) auxv_t[AT_PHDR].a_un.a_ptr;
  462. for (i = 0; i < auxv_t[AT_PHNUM].a_un.a_val; i++, ppnt++) {
  463. if (ppnt->p_type == PT_LOAD) {
  464. if (ppnt->p_vaddr + ppnt->p_memsz > brk_addr)
  465. brk_addr = ppnt->p_vaddr + ppnt->p_memsz;
  466. }
  467. if (ppnt->p_type == PT_DYNAMIC) {
  468. #ifndef ALLOW_ZERO_PLTGOT
  469. /* make sure it's really there. */
  470. if (app_tpnt->dynamic_info[DT_PLTGOT] == 0)
  471. continue;
  472. #endif
  473. /* OK, we have what we need - slip this one into the list. */
  474. app_tpnt = _dl_add_elf_hash_table("", 0,
  475. app_tpnt->dynamic_info, ppnt->p_vaddr, ppnt->p_filesz);
  476. _dl_loaded_modules->libtype = elf_executable;
  477. _dl_loaded_modules->ppnt = (elf_phdr *) auxv_t[AT_PHDR].a_un.a_ptr;
  478. _dl_loaded_modules->n_phent = auxv_t[AT_PHNUM].a_un.a_val;
  479. _dl_symbol_tables = rpnt = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  480. _dl_memset(rpnt, 0, sizeof(*rpnt));
  481. rpnt->dyn = _dl_loaded_modules;
  482. app_tpnt->usage_count++;
  483. app_tpnt->symbol_scope = _dl_symbol_tables;
  484. lpnt = (unsigned long *) (app_tpnt->dynamic_info[DT_PLTGOT]);
  485. #ifdef ALLOW_ZERO_PLTGOT
  486. if (lpnt)
  487. #endif
  488. INIT_GOT(lpnt, _dl_loaded_modules);
  489. }
  490. if (ppnt->p_type == PT_INTERP) { /* OK, fill this in - we did not
  491. have this before */
  492. tpnt->libname = _dl_strdup((char *) ppnt->p_offset +
  493. (auxv_t[AT_PHDR].a_un.a_val & 0xfffff000));
  494. }
  495. }
  496. }
  497. if (argv[0]) {
  498. _dl_progname = argv[0];
  499. }
  500. /* Now we need to figure out what kind of options are selected.
  501. Note that for SUID programs we ignore the settings in LD_LIBRARY_PATH */
  502. {
  503. _dl_not_lazy = _dl_getenv("LD_BIND_NOW", envp);
  504. if ((auxv_t[AT_UID].a_un.a_val == -1 && _dl_suid_ok()) ||
  505. (auxv_t[AT_UID].a_un.a_val != -1 &&
  506. auxv_t[AT_UID].a_un.a_val == auxv_t[AT_EUID].a_un.a_val
  507. && auxv_t[AT_GID].a_un.a_val== auxv_t[AT_EGID].a_un.a_val)) {
  508. _dl_secure = 0;
  509. _dl_preload = _dl_getenv("LD_PRELOAD", envp);
  510. _dl_library_path = _dl_getenv("LD_LIBRARY_PATH", envp);
  511. } else {
  512. _dl_secure = 1;
  513. _dl_preload = _dl_getenv("LD_PRELOAD", envp);
  514. _dl_unsetenv("LD_AOUT_PRELOAD", envp);
  515. _dl_unsetenv("LD_LIBRARY_PATH", envp);
  516. _dl_unsetenv("LD_AOUT_LIBRARY_PATH", envp);
  517. _dl_library_path = NULL;
  518. }
  519. }
  520. #ifdef DL_TRACE
  521. _dl_trace_loaded_objects = _dl_getenv("LD_TRACE_LOADED_OBJECTS", envp);
  522. #endif
  523. /* OK, we now have the application in the list, and we have some
  524. basic stuff in place. Now search through the list for other shared
  525. libraries that should be loaded, and insert them on the list in the
  526. correct order. */
  527. #ifdef USE_CACHE
  528. _dl_map_cache();
  529. #endif
  530. {
  531. struct elf_resolve *tcurr;
  532. struct elf_resolve *tpnt1;
  533. char *lpnt;
  534. if (_dl_preload)
  535. {
  536. char c, *str, *str2;
  537. str = _dl_preload;
  538. while (*str == ':' || *str == ' ' || *str == '\t')
  539. str++;
  540. while (*str)
  541. {
  542. str2 = str;
  543. while (*str2 && *str2 != ':' && *str2 != ' ' && *str2 != '\t')
  544. str2++;
  545. c = *str2;
  546. *str2 = '\0';
  547. if (!_dl_secure || _dl_strchr(str, '/') == NULL)
  548. {
  549. tpnt1 = _dl_load_shared_library(_dl_secure, NULL, str);
  550. if (!tpnt1) {
  551. #ifdef DL_TRACE
  552. if (_dl_trace_loaded_objects)
  553. _dl_dprintf(1, "\t%s => not found\n", str);
  554. else {
  555. #endif
  556. _dl_dprintf(2, "%s: can't load "
  557. "library '%s'\n", _dl_progname, str);
  558. _dl_exit(15);
  559. #ifdef DL_TRACE
  560. }
  561. #endif
  562. } else {
  563. #ifdef DL_TRACE
  564. if (_dl_trace_loaded_objects
  565. && !tpnt1->usage_count) {
  566. /* this is a real hack to make ldd not print
  567. * the library itself when run on a library. */
  568. if (_dl_strcmp(_dl_progname, str) != 0)
  569. _dl_dprintf(1, "\t%s => %s (0x%x)\n", str, tpnt1->libname,
  570. (unsigned) tpnt1->loadaddr);
  571. }
  572. #endif
  573. rpnt->next = (struct dyn_elf *)
  574. _dl_malloc(sizeof(struct dyn_elf));
  575. _dl_memset(rpnt->next, 0, sizeof(*(rpnt->next)));
  576. rpnt = rpnt->next;
  577. tpnt1->usage_count++;
  578. tpnt1->symbol_scope = _dl_symbol_tables;
  579. tpnt1->libtype = elf_lib;
  580. rpnt->dyn = tpnt1;
  581. }
  582. }
  583. *str2 = c;
  584. str = str2;
  585. while (*str == ':' || *str == ' ' || *str == '\t')
  586. str++;
  587. }
  588. }
  589. {
  590. int fd;
  591. struct stat st;
  592. char *preload;
  593. if (!_dl_stat(LDSO_PRELOAD, &st) && st.st_size > 0) {
  594. if ((fd = _dl_open(LDSO_PRELOAD, O_RDONLY)) < 0) {
  595. _dl_dprintf(2, "%s: can't open file '%s'\n",
  596. _dl_progname, LDSO_PRELOAD);
  597. } else {
  598. preload = (caddr_t) _dl_mmap(0, st.st_size + 1,
  599. PROT_READ | PROT_WRITE, MAP_PRIVATE, fd, 0);
  600. _dl_close(fd);
  601. if (preload == (caddr_t) - 1) {
  602. _dl_dprintf(2, "%s: can't map file '%s'\n",
  603. _dl_progname, LDSO_PRELOAD);
  604. } else {
  605. char c, *cp, *cp2;
  606. /* convert all separators and comments to spaces */
  607. for (cp = preload; *cp; /*nada */ ) {
  608. if (*cp == ':' || *cp == '\t' || *cp == '\n') {
  609. *cp++ = ' ';
  610. } else if (*cp == '#') {
  611. do
  612. *cp++ = ' ';
  613. while (*cp != '\n' && *cp != '\0');
  614. } else {
  615. cp++;
  616. }
  617. }
  618. /* find start of first library */
  619. for (cp = preload; *cp && *cp == ' '; cp++)
  620. /*nada */ ;
  621. while (*cp) {
  622. /* find end of library */
  623. for (cp2 = cp; *cp && *cp != ' '; cp++)
  624. /*nada */ ;
  625. c = *cp;
  626. *cp = '\0';
  627. tpnt1 = _dl_load_shared_library(0, NULL, cp2);
  628. if (!tpnt1) {
  629. #ifdef DL_TRACE
  630. if (_dl_trace_loaded_objects)
  631. _dl_dprintf(1, "\t%s => not found\n", cp2);
  632. else {
  633. #endif
  634. _dl_dprintf(2, "%s: can't load library '%s'\n",
  635. _dl_progname, cp2);
  636. _dl_exit(15);
  637. #ifdef DL_TRACE
  638. }
  639. #endif
  640. } else {
  641. #ifdef DL_TRACE
  642. if (_dl_trace_loaded_objects
  643. && !tpnt1->usage_count) {
  644. _dl_dprintf(1, "\t%s => %s (0x%x)\n", cp2,
  645. tpnt1->libname, (unsigned) tpnt1->loadaddr);
  646. }
  647. #endif
  648. rpnt->next = (struct dyn_elf *)
  649. _dl_malloc(sizeof(struct dyn_elf));
  650. _dl_memset(rpnt->next, 0,
  651. sizeof(*(rpnt->next)));
  652. rpnt = rpnt->next;
  653. tpnt1->usage_count++;
  654. tpnt1->symbol_scope = _dl_symbol_tables;
  655. tpnt1->libtype = elf_lib;
  656. rpnt->dyn = tpnt1;
  657. }
  658. /* find start of next library */
  659. *cp = c;
  660. for ( /*nada */ ; *cp && *cp == ' '; cp++)
  661. /*nada */ ;
  662. }
  663. _dl_munmap(preload, st.st_size + 1);
  664. }
  665. }
  666. }
  667. }
  668. for (tcurr = _dl_loaded_modules; tcurr; tcurr = tcurr->next) {
  669. for (dpnt = (Elf32_Dyn *) tcurr->dynamic_addr; dpnt->d_tag;
  670. dpnt++) {
  671. if (dpnt->d_tag == DT_NEEDED) {
  672. lpnt = tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] +
  673. dpnt->d_un.d_val;
  674. if (tpnt && _dl_strcmp(lpnt,
  675. _dl_get_last_path_component(tpnt->libname)) == 0) {
  676. struct elf_resolve *ttmp;
  677. #ifdef DL_TRACE
  678. if (_dl_trace_loaded_objects && !tpnt->usage_count) {
  679. _dl_dprintf(1, "\t%s => %s (0x%x)\n",
  680. lpnt, tpnt->libname, (unsigned) tpnt->loadaddr);
  681. }
  682. #endif
  683. ttmp = _dl_loaded_modules;
  684. while (ttmp->next)
  685. ttmp = ttmp->next;
  686. ttmp->next = tpnt;
  687. tpnt->prev = ttmp;
  688. tpnt->next = NULL;
  689. rpnt->next = (struct dyn_elf *)
  690. _dl_malloc(sizeof(struct dyn_elf));
  691. _dl_memset(rpnt->next, 0, sizeof(*(rpnt->next)));
  692. rpnt = rpnt->next;
  693. rpnt->dyn = tpnt;
  694. tpnt->usage_count++;
  695. tpnt->symbol_scope = _dl_symbol_tables;
  696. tpnt = NULL;
  697. continue;
  698. }
  699. if (!(tpnt1 = _dl_load_shared_library(0, tcurr, lpnt))) {
  700. #ifdef DL_TRACE
  701. if (_dl_trace_loaded_objects)
  702. _dl_dprintf(1, "\t%s => not found\n", lpnt);
  703. else {
  704. #endif
  705. _dl_dprintf(2, "%s: can't load library '%s'\n",
  706. _dl_progname, lpnt);
  707. _dl_exit(16);
  708. #ifdef DL_TRACE
  709. }
  710. #endif
  711. } else {
  712. #ifdef DL_TRACE
  713. if (_dl_trace_loaded_objects && !tpnt1->usage_count)
  714. _dl_dprintf(1, "\t%s => %s (0x%x)\n", lpnt, tpnt1->libname,
  715. (unsigned) tpnt1->loadaddr);
  716. #endif
  717. rpnt->next = (struct dyn_elf *)
  718. _dl_malloc(sizeof(struct dyn_elf));
  719. _dl_memset(rpnt->next, 0, sizeof(*(rpnt->next)));
  720. rpnt = rpnt->next;
  721. tpnt1->usage_count++;
  722. tpnt1->symbol_scope = _dl_symbol_tables;
  723. tpnt1->libtype = elf_lib;
  724. rpnt->dyn = tpnt1;
  725. }
  726. }
  727. }
  728. }
  729. }
  730. #ifdef USE_CACHE
  731. _dl_unmap_cache();
  732. #endif
  733. /* ldd uses uses this. I am not sure how you pick up the other flags */
  734. #ifdef DL_TRACE
  735. if (_dl_trace_loaded_objects) {
  736. char *_dl_warn = 0;
  737. _dl_warn = _dl_getenv("LD_WARN", envp);
  738. if (!_dl_warn)
  739. _dl_exit(0);
  740. }
  741. #endif
  742. /*
  743. * If the program interpreter is not in the module chain, add it. This will
  744. * be required for dlopen to be able to access the internal functions in the
  745. * dynamic linker.
  746. */
  747. if (tpnt) {
  748. struct elf_resolve *tcurr;
  749. tcurr = _dl_loaded_modules;
  750. if (tcurr)
  751. while (tcurr->next)
  752. tcurr = tcurr->next;
  753. tpnt->next = NULL;
  754. tpnt->usage_count++;
  755. if (tcurr) {
  756. tcurr->next = tpnt;
  757. tpnt->prev = tcurr;
  758. } else {
  759. _dl_loaded_modules = tpnt;
  760. tpnt->prev = NULL;
  761. }
  762. if (rpnt) {
  763. rpnt->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  764. _dl_memset(rpnt->next, 0, sizeof(*(rpnt->next)));
  765. rpnt = rpnt->next;
  766. } else {
  767. rpnt = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  768. _dl_memset(rpnt, 0, sizeof(*(rpnt->next)));
  769. }
  770. rpnt->dyn = tpnt;
  771. tpnt = NULL;
  772. }
  773. /*
  774. * OK, now all of the kids are tucked into bed in their proper addresses.
  775. * Now we go through and look for REL and RELA records that indicate fixups
  776. * to the GOT tables. We need to do this in reverse order so that COPY
  777. * directives work correctly */
  778. goof = _dl_loaded_modules ? _dl_fixup(_dl_loaded_modules) : 0;
  779. /* Some flavors of SVr4 do not generate the R_*_COPY directive,
  780. and we have to manually search for entries that require fixups.
  781. Solaris gets this one right, from what I understand. */
  782. if (_dl_symbol_tables)
  783. goof += _dl_copy_fixups(_dl_symbol_tables);
  784. #ifdef DL_TRACE
  785. if (goof || _dl_trace_loaded_objects)
  786. _dl_exit(0);
  787. #endif
  788. /* OK, at this point things are pretty much ready to run. Now we
  789. need to touch up a few items that are required, and then
  790. we can let the user application have at it. Note that
  791. the dynamic linker itself is not guaranteed to be fully
  792. dynamicly linked if we are using ld.so.1, so we have to look
  793. up each symbol individually. */
  794. _dl_brkp = (unsigned long *) _dl_find_hash("___brk_addr", NULL, 1, NULL, 0);
  795. if (_dl_brkp)
  796. *_dl_brkp = brk_addr;
  797. _dl_envp =
  798. (unsigned long *) _dl_find_hash("__environ", NULL, 1, NULL, 0);
  799. if (_dl_envp)
  800. *_dl_envp = (unsigned long) envp;
  801. {
  802. int i;
  803. elf_phdr *ppnt;
  804. /* We had to set the protections of all pages to R/W for dynamic linking.
  805. Set text pages back to R/O */
  806. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  807. for (ppnt = tpnt->ppnt, i = 0; i < tpnt->n_phent; i++, ppnt++)
  808. if (ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W) &&
  809. tpnt->dynamic_info[DT_TEXTREL])
  810. _dl_mprotect((void *) (tpnt->loadaddr + (ppnt->p_vaddr & 0xfffff000)),
  811. (ppnt->p_vaddr & 0xfff) + (unsigned long) ppnt->p_filesz,
  812. LXFLAGS(ppnt->p_flags));
  813. }
  814. _dl_atexit = (int (*)(void *)) _dl_find_hash("atexit", NULL, 1, NULL, 0);
  815. /*
  816. * OK, fix one more thing - set up the debug_addr structure to point
  817. * to our chain. Later we may need to fill in more fields, but this
  818. * should be enough for now.
  819. */
  820. debug_addr->r_map = (struct link_map *) _dl_loaded_modules;
  821. debug_addr->r_version = 1;
  822. debug_addr->r_ldbase = load_addr;
  823. debug_addr->r_brk = (unsigned long) &_dl_debug_state;
  824. _dl_debug_addr = debug_addr;
  825. debug_addr->r_state = RT_CONSISTENT;
  826. /* This is written in this funny way to keep gcc from inlining the
  827. function call. */
  828. ((void (*)(void)) debug_addr->r_brk) ();
  829. for (tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next) {
  830. /* Apparently crt1 for the application is responsible for handling this.
  831. * We only need to run the init/fini for shared libraries
  832. */
  833. if (tpnt->libtype == program_interpreter ||
  834. tpnt->libtype == elf_executable)
  835. continue;
  836. if (tpnt->init_flag & INIT_FUNCS_CALLED)
  837. continue;
  838. tpnt->init_flag |= INIT_FUNCS_CALLED;
  839. if (tpnt->dynamic_info[DT_INIT]) {
  840. _dl_elf_init = (int (*)(void)) (tpnt->loadaddr +
  841. tpnt->dynamic_info[DT_INIT]);
  842. (*_dl_elf_init) ();
  843. }
  844. if (_dl_atexit && tpnt->dynamic_info[DT_FINI]) {
  845. (*_dl_atexit) (tpnt->loadaddr + tpnt->dynamic_info[DT_FINI]);
  846. }
  847. #undef DL_DEBUG
  848. #ifdef DL_DEBUG
  849. else {
  850. _dl_dprintf(2, tpnt->libname);
  851. _dl_dprintf(2, ": ");
  852. if (!_dl_atexit)
  853. _dl_dprintf(2, "The address is atexit () is 0x0.");
  854. if (!tpnt->dynamic_info[DT_FINI])
  855. _dl_dprintf(2, "Invalid .fini section.");
  856. _dl_dprintf(2, "\n");
  857. }
  858. #endif
  859. #undef DL_DEBUG
  860. }
  861. /* OK we are done here. Turn out the lights, and lock up. */
  862. _dl_elf_main = (int (*)(int, char **, char **)) auxv_t[AT_ENTRY].a_un.a_fcn;
  863. /*
  864. * Transfer control to the application.
  865. */
  866. status = 0; /* Used on x86, but not on other arches */
  867. START();
  868. }
  869. /*
  870. * This stub function is used by some debuggers. The idea is that they
  871. * can set an internal breakpoint on it, so that we are notified when the
  872. * address mapping is changed in some way.
  873. */
  874. void _dl_debug_state()
  875. {
  876. return;
  877. }
  878. int _dl_fixup(struct elf_resolve *tpnt)
  879. {
  880. int goof = 0;
  881. if (tpnt->next)
  882. goof += _dl_fixup(tpnt->next);
  883. if (tpnt->dynamic_info[DT_REL]) {
  884. #ifdef ELF_USES_RELOCA
  885. _dl_dprintf(2, "%s: can't handle REL relocation records\n",
  886. _dl_progname);
  887. _dl_exit(17);
  888. #else
  889. if (tpnt->init_flag & RELOCS_DONE)
  890. return goof;
  891. tpnt->init_flag |= RELOCS_DONE;
  892. goof += _dl_parse_relocation_information(tpnt,
  893. tpnt->dynamic_info[DT_REL], tpnt->dynamic_info[DT_RELSZ], 0);
  894. #endif
  895. }
  896. if (tpnt->dynamic_info[DT_RELA]) {
  897. #ifdef ELF_USES_RELOCA
  898. if (tpnt->init_flag & RELOCS_DONE)
  899. return goof;
  900. tpnt->init_flag |= RELOCS_DONE;
  901. goof += _dl_parse_relocation_information(tpnt,
  902. tpnt->dynamic_info[DT_RELA], tpnt->dynamic_info[DT_RELASZ], 0);
  903. #else
  904. _dl_dprintf(2, "%s: can't handle RELA relocation records\n",
  905. _dl_progname);
  906. _dl_exit(18);
  907. #endif
  908. }
  909. if (tpnt->dynamic_info[DT_JMPREL]) {
  910. if (tpnt->init_flag & JMP_RELOCS_DONE)
  911. return goof;
  912. tpnt->init_flag |= JMP_RELOCS_DONE;
  913. if (!_dl_not_lazy || *_dl_not_lazy == 0)
  914. _dl_parse_lazy_relocation_information(tpnt,
  915. tpnt->dynamic_info[DT_JMPREL], tpnt->dynamic_info[DT_PLTRELSZ], 0);
  916. else
  917. goof += _dl_parse_relocation_information(tpnt,
  918. tpnt->dynamic_info[DT_JMPREL], tpnt->dynamic_info[DT_PLTRELSZ], 0);
  919. }
  920. return goof;
  921. }
  922. void *_dl_malloc(int size)
  923. {
  924. void *retval;
  925. #ifdef DL_DEBUG
  926. SEND_STDERR("malloc: request for ");
  927. SEND_NUMBER_STDERR(size, 0);
  928. SEND_STDERR(" bytes\n");
  929. #endif
  930. if (_dl_malloc_function)
  931. return (*_dl_malloc_function) (size);
  932. if (_dl_malloc_addr - _dl_mmap_zero + size > 4096) {
  933. #ifdef DL_DEBUG
  934. SEND_STDERR("malloc: mmapping more memory\n");
  935. #endif
  936. _dl_mmap_zero = _dl_malloc_addr = _dl_mmap((void *) 0, size,
  937. PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, 0, 0);
  938. if (_dl_mmap_check_error(_dl_mmap_zero)) {
  939. _dl_dprintf(2, "%s: mmap of a spare page failed!\n", _dl_progname);
  940. _dl_exit(20);
  941. }
  942. }
  943. retval = _dl_malloc_addr;
  944. _dl_malloc_addr += size;
  945. /*
  946. * Align memory to 4 byte boundary. Some platforms require this, others
  947. * simply get better performance.
  948. */
  949. _dl_malloc_addr = (char *) (((unsigned long) _dl_malloc_addr + 3) & ~(3));
  950. return retval;
  951. }
  952. char *_dl_getenv(char *symbol, char **envp)
  953. {
  954. char *pnt;
  955. char *pnt1;
  956. while ((pnt = *envp++)) {
  957. pnt1 = symbol;
  958. while (*pnt && *pnt == *pnt1)
  959. pnt1++, pnt++;
  960. if (!*pnt || *pnt != '=' || *pnt1)
  961. continue;
  962. return pnt + 1;
  963. }
  964. return 0;
  965. }
  966. void _dl_unsetenv(char *symbol, char **envp)
  967. {
  968. char *pnt;
  969. char *pnt1;
  970. char **newenvp = envp;
  971. for (pnt = *envp; pnt; pnt = *++envp) {
  972. pnt1 = symbol;
  973. while (*pnt && *pnt == *pnt1)
  974. pnt1++, pnt++;
  975. if (!*pnt || *pnt != '=' || *pnt1)
  976. *newenvp++ = *envp;
  977. }
  978. *newenvp++ = *envp;
  979. return;
  980. }
  981. char *_dl_strdup(const char *string)
  982. {
  983. char *retval;
  984. int len;
  985. len = _dl_strlen(string);
  986. retval = _dl_malloc(len + 1);
  987. _dl_strcpy(retval, string);
  988. return retval;
  989. }
  990. /* Minimum printf which handles only characters, %d's and %s's */
  991. void _dl_dprintf(int fd, const char *fmt, ...)
  992. {
  993. int num;
  994. va_list args;
  995. char *start, *ptr, *string;
  996. char buf[2048];
  997. start = ptr = buf;
  998. if (!fmt)
  999. return;
  1000. if (_dl_strlen(fmt) >= (sizeof(buf)-1))
  1001. _dl_write(fd, "(overflow)\n", 10);
  1002. _dl_strcpy(buf, fmt);
  1003. va_start(args, fmt);
  1004. while(start)
  1005. {
  1006. while(*ptr != '%' && *ptr) {
  1007. ptr++;
  1008. }
  1009. if(*ptr == '%')
  1010. {
  1011. *ptr++ = '\0';
  1012. _dl_write(fd, start, _dl_strlen(start));
  1013. switch(*ptr++)
  1014. {
  1015. case 's':
  1016. string = va_arg(args, char *);
  1017. if (!string)
  1018. _dl_write(fd, "(null)", 6);
  1019. else
  1020. _dl_write(fd, string, _dl_strlen(string));
  1021. break;
  1022. case 'i':
  1023. case 'd':
  1024. {
  1025. char tmp[13];
  1026. num = va_arg(args, int);
  1027. string = _dl_simple_ltoa(tmp, num);
  1028. _dl_write(fd, string, _dl_strlen(string));
  1029. break;
  1030. }
  1031. case 'x':
  1032. case 'X':
  1033. {
  1034. char tmp[13];
  1035. num = va_arg(args, int);
  1036. string = _dl_simple_ltoahex(tmp, num);
  1037. _dl_write(fd, string, _dl_strlen(string));
  1038. break;
  1039. }
  1040. default:
  1041. _dl_write(fd, "(null)", 6);
  1042. break;
  1043. }
  1044. start = ptr;
  1045. }
  1046. else
  1047. {
  1048. _dl_write(fd, start, _dl_strlen(start));
  1049. start = NULL;
  1050. }
  1051. }
  1052. return;
  1053. }