ldso.c 37 KB

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