boot1.c 36 KB

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