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