home *** CD-ROM | disk | FTP | other *** search
/ Serving the Web / ServingTheWeb1995.disc1of1.iso / linux / slacksrce / d / libc / libc-4.6 / libc-4 / libc-linux / elf / d-link.old / i386 / boot1.c next >
Encoding:
C/C++ Source or Header  |  1994-10-20  |  24.1 KB  |  744 lines

  1. /* Run an ELF binary on a linux system.
  2.  
  3.    Copyright (C) 1993, Eric Youngdale.
  4.  
  5.    This program is free software; you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation; either version 2, or (at your option)
  8.    any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program; if not, write to the Free Software
  17.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  18.  
  19.  
  20.  
  21. /* Program to load an ELF binary on a linux system, and run it.
  22.  * References to symbols in sharable libraries can be resolved by
  23.  * an ELF sharable library. */
  24.  
  25. /* Disclaimer:  I have never seen any AT&T source code for SVr4, nor have
  26.    I ever taken any courses on internals.  This program was developed using
  27.    information available through the book "UNIX SYSTEM V RELEASE 4,
  28.    Programmers guide: Ansi C and Programming Support Tools", which did
  29.    a more than adequate job of explaining everything required to get this
  30.    working. */
  31.  
  32. /*
  33.  * The main trick with this program is that initially, we ourselves are not
  34.  * dynamicly linked.  This means that we cannot access any global variables
  35.  * since the GOT is initialized by the linker assuming a virtual address of 0,
  36.  * and we cannot call any functions since the PLT is not initialized at all
  37.  * (it will tend to want to call the dynamic linker
  38.  *
  39.  * There are further restrictions - we cannot use large switch statements,
  40.  * since the compiler generates tables of addresses and jumps through them.
  41.  * We can use inline functions, because these do not transfer control to
  42.  * a new address, but they must be static so that they are not exported
  43.  * from the modules.  We cannot use normal syscall stubs, because these
  44.  * all reference the errno global variable which is not yet initialized.
  45.  * We can use all of the local stack variables that we want, since these
  46.  * are all referenced to %ebp or %esp.
  47.  *
  48.  * Life is further complicated by the fact that initially we do not want
  49.  * to do a complete dynamic linking.  We want to allow the user to supply
  50.  * new functions replacing some of the library versions, and until we have
  51.  * the list of modules that we should search set up, we do not want to do
  52.  * any of this.  Thus I have chosen to only perform the relocations for
  53.  * variables that start with "_dl_" since ANSI specifies that the user is
  54.  * not supposed to redefine any of these variables.
  55.  *
  56.  * Fortunately, the linker itself leaves a few clues lying around, and
  57.  * when the kernel starts the image, there are a few further clues.
  58.  * First of all, there is information buried on the stack that the kernel
  59.  * leaves, which includes information about the load address that the
  60.  * program interpreter was loaded at, the number of sections, the address
  61.  * the application was loaded at and so forth.  Here this information
  62.  * is stored in the array dl_info, and the indicies are taken from the
  63.  * file /usr/include/sys/auxv.h on any SVr4 system.
  64.  *
  65.  * The linker itself leaves a pointer to the .dynamic section in the first
  66.  * slot of the GOT, and as it turns out, %ebx points to ghe GOT when
  67.  * you are using PIC code, so we just dereference this to get the address
  68.  * of the dynamic sections.
  69.  *
  70.  * Typically you must load all text pages as writable so that dynamic linking
  71.  * can succeed.  The kernel under SVr4 loads these as R/O, so we must call
  72.  * mprotect to change the protections.  Once we are done, we should set these
  73.  * back again, so the desired behavior is achieved.  Under linux there is
  74.  * currently no mprotect function in the distribution kernel (although
  75.  * someone has alpha patches), so for now everything is loaded writable.
  76.  *
  77.  * We do not have access to malloc and friends at the initial stages of dynamic
  78.  * linking, and it would be handy to have some scratchpad memory available
  79.  * for use as we set things up.  It is a bit of a kluge, but we mmap /dev/zero
  80.  * to get one page of scratchpad.  A simpleminded _dl_malloc is provided so
  81.  * that we have some memory that can be used for this purpose.  Typically
  82.  * we would not want to use the same memory pool as malloc anyway - the user
  83.  * might want to redefine malloc for example.
  84.  *
  85.  * Our first task is to perform a minimal linking so that we can call other
  86.  * portions of the dynamic linker.  Once we have done this, we then build
  87.  * the list of modules that the application requires, using LD_LIBRARY_PATH
  88.  * if this is not a suid program (/usr/lib otherwise).  Once this is done,
  89.  * we can do the dynamic linking as required (and we must omit the things
  90.  * we did to get the dynamic linker up and running in the first place.
  91.  * After we have done this, we just have a few housekeeping chores and we
  92.  * can transfer control to the user's application.
  93.  */
  94.  
  95. #include <stdarg.h>
  96. #ifdef IBCS_COMPATIBLE
  97. #include <ibcs/unistd.h>
  98. #else
  99. #include <linux/unistd.h>
  100. #endif
  101. #include <sys/mman.h>
  102. #include "hash.h"
  103. #include "linuxelf.h"
  104. #include "syscall.h"
  105. #include "string.h"
  106.  
  107. static char * _dl_malloc_addr;
  108. char * _dl_library_path = 0; /* Where we look for libraries */
  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.  
  114. static int (*_dl_elf_init)(void);
  115.  
  116. unsigned int * _dl_brkp; 
  117.  
  118. unsigned int * _dl_envp;
  119.  
  120. #define DL_MALLOC(SIZE) ((void *) (malloc_buffer += SIZE, malloc_buffer - SIZE))
  121.  
  122.  
  123.  
  124. #define ELF_HASH(RESULT,NAME) { \
  125.   unsigned long hash = 0; \
  126.     unsigned long tmp;  \
  127.   char * name = NAME; \
  128.   while (*name){  \
  129.     hash = (hash << 4) + *name++; \
  130.     if((tmp = hash & 0xf0000000)) hash ^= tmp >> 24; \
  131.     hash &= ~tmp; \
  132.   }; \
  133.   RESULT = hash; \
  134. }
  135. extern _dl_linux_resolve(void);
  136. extern int _dl_interpreter_exit(int);
  137. extern char * _dl_strdup(const char *);
  138. extern char * _dl_getenv(char * symbol, char ** envp);
  139. extern int _dl_fixup(struct elf_resolve * tpnt);
  140.  
  141. void _dl_boot(int args);
  142.  
  143. void _dl_boot(int args){
  144.   unsigned int argc;
  145.   char ** argv, ** envp;
  146.   int status;
  147.  
  148.   unsigned int load_addr;
  149.   unsigned int * got;
  150.   unsigned int * aux_dat;
  151.   int goof = 0;
  152.   struct elf_resolve * tpnt;
  153.   struct dyn_elf * rpnt;
  154.   struct elf_resolve * app_tpnt;
  155.   unsigned int brk_addr;
  156.   unsigned int dl_data[10];
  157.   unsigned char * malloc_buffer;
  158.   int (*_dl_atexit)(void *);
  159.   int * lpnt;
  160.   struct dynamic * dpnt;
  161.   unsigned int *hash_addr;
  162.   unsigned int *chains;
  163.   int indx;
  164.  
  165.   /* First obtain the information on the stack that tells us more about
  166.      what binary is loaded, where it is loaded, etc, etc */
  167.  
  168.   aux_dat = (unsigned int *) &args;
  169.   argc = *(aux_dat - 1);
  170.   argv = (char **) aux_dat;
  171.   while(*aux_dat) aux_dat++;  /* Skip over the argv pointers */
  172.   aux_dat++;  /* Skip over NULL at end of argv */
  173.   envp = (char **) aux_dat;
  174.   while(*aux_dat) aux_dat++;  /* Skip over the envp pointers */
  175.   aux_dat++;  /* Skip over NULL at end of envp */
  176.   while(*aux_dat)
  177.     {
  178.       unsigned int * ad1;
  179.       ad1 = aux_dat + 1;
  180.       dl_data[*aux_dat] = *ad1;
  181.       aux_dat += 2;
  182.     }
  183.  
  184.   /* Next, locate the GOT */
  185.  
  186.   load_addr = dl_data[7];
  187.  
  188.   __asm__("\tmovl %%ebx,%0\n\t" : "=a" (got));
  189.   dpnt = (struct dynamic *) (*got + load_addr);
  190.  
  191.   /* OK, time for another hack.  Now call mmap to get a page of writable
  192.      memory that can be used for a temporary malloc.  We do not know brk
  193.      yet, so we cannot use real malloc. */
  194.  
  195.   {
  196.     int zfileno;
  197.     char  * zfile = "/dev/zero";
  198.     zfileno = _dl_open(zfile, 0);
  199.     malloc_buffer = (unsigned char *) _dl_mmap((void*) 0, 4096, PROT_READ | PROT_WRITE, 
  200.                  MAP_PRIVATE, zfileno, 0);
  201.     _dl_close(zfileno);
  202.   };
  203.  
  204.   tpnt = DL_MALLOC(sizeof(struct elf_resolve));
  205.   app_tpnt = DL_MALLOC(sizeof(struct elf_resolve));
  206.  
  207.   /* OK, that was easy.  Next scan the DYNAMIC section of the image.
  208.      We are only doing ourself right now - we will have to do the rest later */
  209.  
  210.   while(dpnt->d_tag)
  211.     {
  212.       tpnt->dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  213.       if(dpnt->d_tag == DT_TEXTREL ||
  214.      SVR4_BUGCOMPAT) tpnt->dynamic_info[DT_TEXTREL] = 1;
  215.       dpnt++;
  216.     }
  217.  
  218.   {
  219.     struct elf_phdr * ppnt;
  220.     int i;
  221.     
  222.     ppnt = (struct elf_phdr *) dl_data[3];
  223.     for(i=0; i<dl_data[5]; i++, ppnt++)
  224.       if(ppnt->p_type == PT_DYNAMIC) {
  225.     dpnt = (struct dynamic *) ppnt->p_vaddr;
  226.     while(dpnt->d_tag)
  227.       {
  228.         app_tpnt->dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  229.         if(dpnt->d_tag == DT_TEXTREL ||
  230.            SVR4_BUGCOMPAT) app_tpnt->dynamic_info[DT_TEXTREL] = 1;
  231.         dpnt++;
  232.       }
  233.       }
  234.   }
  235.  
  236.   /* Get some more of the information that we will need to dynamicly link
  237.      this module to itself */
  238.  
  239.   hash_addr = (unsigned int *) (tpnt->dynamic_info[DT_HASH]+load_addr);
  240.   tpnt->nbucket = *hash_addr++;
  241.   tpnt->nchain = *hash_addr++;
  242.   tpnt->elf_buckets = hash_addr;
  243.   hash_addr += tpnt->nbucket;
  244.   chains = hash_addr;
  245.  
  246.   /* Ugly, ugly.  We need to call mprotect to change the protection of
  247.      the text pages so that we can do the dynamic linking.  We can set the
  248.      protection back again once we are done */
  249.  
  250.   {
  251.     char * foo;
  252.     struct elf_phdr * ppnt;
  253.     int i
  254. ;
  255.     foo = (char*) &_dl_not_lazy;
  256.  
  257.     /* First cover the shared library/dynamic linker */
  258.     if(tpnt->dynamic_info[DT_TEXTREL]) 
  259.       _dl_mprotect((void *) load_addr, (unsigned int) foo,
  260.            PROT_READ | PROT_WRITE | PROT_EXEC);
  261.  
  262.     /* Now cover the application program. */
  263.     if(app_tpnt->dynamic_info[DT_TEXTREL]) {
  264.       ppnt = (struct elf_phdr *) dl_data[3];
  265.       for(i=0; i<dl_data[5]; i++, ppnt++) {
  266.     if(ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  267.       _dl_mprotect((void *) (ppnt->p_vaddr & 0xfffff000),
  268.                (ppnt->p_vaddr & 0xfff) + (unsigned int) ppnt->p_filesz,
  269.                PROT_READ | PROT_WRITE | PROT_EXEC);
  270.       }
  271.     };
  272.   }
  273.  
  274.   /* OK, now do the relocations.  We do not do a lazy binding here, so
  275.    that once we are done, we have considerably more flexibility. */
  276.  
  277.   for(indx=0; indx < 2; indx++)
  278.     {
  279.       int i;
  280.       int reloc_type;
  281.       struct Elf32_Rel * rpnt;
  282.       unsigned int * reloc_addr;
  283.       unsigned int symbol_addr;
  284.       int symtab_index;
  285.       unsigned int rel_addr, rel_size;
  286.  
  287.   
  288.       rel_addr = (indx ? tpnt->dynamic_info[DT_JMPREL] : tpnt->dynamic_info[DT_REL]);
  289.       rel_size = (indx ? tpnt->dynamic_info[DT_PLTRELSZ] : tpnt->dynamic_info[DT_RELSZ]);
  290.  
  291.       if(!rel_addr) continue;
  292.  
  293.       /* Now parse the relocation information */
  294.       rpnt = (struct Elf32_Rel *) (rel_addr + load_addr);
  295.       rel_size = rel_size / sizeof(struct Elf32_Rel);
  296.       
  297.       for(i=0; i< rel_size; i++, rpnt++){
  298.     reloc_addr = (int *) (load_addr + (int)rpnt->offset);
  299.     reloc_type = ELF32_R_TYPE(rpnt->info);
  300.     symtab_index = ELF32_R_SYM(rpnt->info);
  301.     symbol_addr = 0;
  302.     if(symtab_index) {
  303.       int si;
  304.       char * pnt;
  305.       char * strtab;
  306.       struct Elf32_Sym * symtab;
  307.       unsigned int elf_hash_number, hn;
  308.       unsigned int weak_result;
  309.       
  310.       weak_result = 0;
  311.       symtab = (struct Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB]+load_addr);
  312.       strtab = (char *) (tpnt->dynamic_info[DT_STRTAB]+load_addr);
  313.  
  314.       /* We only do a partial dynamic linking right now.  The user
  315.          is not supposed to redefine any symbols that start with
  316.          a '_', so we can do this with confidence. */
  317.  
  318.       if (!_dl_symbol(strtab + symtab[symtab_index].st_name)) continue;
  319.  
  320.       ELF_HASH(elf_hash_number, strtab + symtab[symtab_index].st_name);
  321.       
  322.       hn = elf_hash_number % tpnt->nbucket;
  323.       for(si = tpnt->elf_buckets[hn]; si; si = chains[si]){
  324.         pnt = strtab + symtab[si].st_name;
  325.         if(_dl_strcmp(pnt, strtab + symtab[symtab_index].st_name) == 0 && 
  326.            (ELF32_ST_TYPE(symtab[si].st_info) == STT_FUNC ||
  327.         ELF32_ST_TYPE(symtab[si].st_info) == STT_OBJECT) &&
  328.            (reloc_addr && 
  329.         symtab[si].st_value != (unsigned int) reloc_addr) &&
  330.            symtab[si].st_value != 0 && symtab[si].st_shndx != 0)
  331.           switch(ELF32_ST_BIND(symtab[si].st_info)){
  332.           case STB_GLOBAL:
  333.         symbol_addr = load_addr + symtab[si].st_value;
  334.         break;
  335.           case STB_WEAK:
  336.         if (!weak_result) weak_result = load_addr + 
  337.           symtab[si].st_value;
  338.         break;
  339.           default:  /* Do local symbols need to be examined? */
  340.         break;
  341.           };
  342.         if(symbol_addr) break;
  343.       };
  344.       if(!symbol_addr) symbol_addr = weak_result;
  345.       if(!symbol_addr)
  346.         continue;
  347.     };
  348.     switch(reloc_type){
  349.     case R_386_32:
  350.       *reloc_addr += symbol_addr;
  351.       break;
  352.     case R_386_PC32:
  353.       *reloc_addr += symbol_addr - (unsigned int) reloc_addr;
  354.       break;
  355.     case R_386_GLOB_DAT:
  356.     case R_386_JMP_SLOT:
  357.       *reloc_addr = symbol_addr;
  358.       break;
  359.     case R_386_RELATIVE:
  360.       *reloc_addr += (unsigned int) load_addr;
  361.       break;
  362.     default:
  363. #ifdef IBCS_COMPATIBLE
  364.       __asm__("pushl $13\n\tpushl $0\n\tmovl $1,%%eax\n\tlcall $7,$0" : : );
  365. #else
  366.       __asm__("pushl $13\n\tmovl $1,%%eax\n\tint $0x80" : : );
  367. #endif
  368.     };
  369.       };
  370.     }; 
  371.  
  372.   /* OK, at this point we have a crude malloc capability.  Start to build
  373.      the tables of the modules that are required for this beast to run.
  374.      We start with the basic executable, and then go from there.  Eventually
  375.      we will run across ourself, and we will need to properly deal with that
  376.      as well. */
  377.  
  378.   _dl_malloc_addr = malloc_buffer;
  379. /*  tpnt = _dl_malloc(sizeof(struct elf_resolve)); */
  380.  
  381. /* Now we have done the mandatory linking of some things.  We are now
  382.    free to start using global variables, since these things have all been
  383.    fixed up by now.  Still no function calls outside of this library ,
  384.    since the dynamic resolver is not yet ready. */
  385.  
  386.   lpnt = (int *) (tpnt->dynamic_info[DT_PLTGOT] + load_addr);
  387.   lpnt[2] = (int) _dl_linux_resolve;
  388.   lpnt[1] = (int) tpnt;
  389.  
  390.   /* OK, this was a big step, now we need to scan all of the user images
  391.      and load them properly. */
  392.  
  393.   tpnt->next = 0;
  394.   tpnt->libname = 0;
  395.   tpnt->libtype = program_interpreter;
  396.  
  397.   { struct elfhdr * epnt;
  398.     struct elf_phdr * ppnt;
  399.     int i;
  400.  
  401.     epnt = (struct elfhdr *) dl_data[7];
  402.     tpnt->n_phent = epnt->e_phnum;
  403.     tpnt->ppnt = ppnt = (struct elf_phdr *) (load_addr + epnt->e_phoff);
  404.     for(i=0;i < epnt->e_phnum; i++, ppnt++){
  405.       if(ppnt->p_type == PT_DYNAMIC) {
  406.     tpnt->dynamic_addr = ppnt->p_vaddr + load_addr;
  407.     tpnt->dynamic_size = ppnt->p_filesz;
  408.       }
  409.     }
  410.   }
  411.  
  412.   tpnt->chains = chains;
  413.   tpnt->loadaddr = (char *) load_addr;
  414.  
  415.   brk_addr = 0;
  416.   rpnt = NULL;
  417.  
  418.   /* At this point we are now free to examine the user application,
  419.      and figure out which libraries are supposed to be called.  Until
  420.      we have this list, we will not be completely ready for dynamic linking */
  421.  
  422.   {
  423.     struct elf_phdr * ppnt;
  424.     int i;
  425.  
  426.     ppnt = (struct elf_phdr *) dl_data[3];
  427.     for(i=0; i<dl_data[5]; i++, ppnt++) {
  428.       if(ppnt->p_type == PT_LOAD) {
  429.     if(ppnt->p_vaddr + ppnt->p_memsz > brk_addr) 
  430.       brk_addr = ppnt->p_vaddr + ppnt->p_memsz;
  431.       };
  432.       if(ppnt->p_type == PT_DYNAMIC) {
  433.     /* OK, we have what we need - slip this one into the list. */
  434.     _dl_add_elf_hash_table("", 0, 
  435.                 app_tpnt->dynamic_info, ppnt->p_vaddr, ppnt->p_filesz);
  436.     _dl_loaded_modules->libtype = elf_executable;
  437.     _dl_loaded_modules->ppnt = (struct elf_phdr *) dl_data[3];
  438.     _dl_loaded_modules->n_phent = dl_data[5];
  439.     _dl_symbol_tables = rpnt = 
  440.        (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  441.     rpnt->dyn = _dl_loaded_modules;
  442.     app_tpnt->usage_count++;
  443.     app_tpnt->symbol_scope = _dl_symbol_tables;
  444.     rpnt->next = NULL;
  445.     lpnt = (int *) (app_tpnt->dynamic_info[DT_PLTGOT]);
  446.     lpnt[2] = (int) _dl_linux_resolve;
  447.     lpnt[1] = (int) _dl_loaded_modules;
  448.       }
  449.       if(ppnt->p_type == PT_INTERP) { /* OK, fill this in - we did not have
  450.                      this before */
  451.     tpnt->libname =  _dl_strdup((char *) ppnt->p_offset +(dl_data[3] & 0xfffff000));
  452.       }
  453.     }
  454.   }
  455.  
  456.   /* Now we need to figure out what kind of options are selected.
  457.    Note that for SUID programs we ignore the settings in LD_LIBRARY_PATH */
  458.   {
  459.     unsigned int uid, euid, gid, egid;
  460.     euid = egid = 0; /* So compiler does not warn us */
  461.     _dl_not_lazy = _dl_getenv("LD_BIND_NOW",envp);
  462.  
  463. #ifdef IBCS_COMPATIBLE
  464.     __asm__("movl %2,%%eax\n\tlcall $7,$0" :"=a" (uid), "=d" (euid) :"a" (__IBCS_getuid));
  465.     __asm__("movl %2,%%eax\n\tlcall $7,$0" :"=a" (gid), "=d" (egid) :"a" (__IBCS_getgid));
  466. #else
  467.     __asm__ volatile ("int $0x80" : "=a" (uid) : "0" (__NR_getuid));
  468.     __asm__ volatile ("int $0x80" : "=a" (euid) : "0" (__NR_geteuid));
  469.     __asm__ volatile ("int $0x80" : "=a" (gid) : "0" (__NR_getgid));
  470.     __asm__ volatile ("int $0x80" : "=a" (egid) : "0" (__NR_getegid));
  471. #endif
  472.  
  473.     if(uid == euid && gid == egid) {
  474. #ifndef IBCS_COMPATIBLE
  475.       _dl_library_path = _dl_getenv("ELF_LD_LIBRARY_PATH",envp);
  476.       if (!_dl_library_path)
  477. #endif
  478.         _dl_library_path = _dl_getenv("LD_LIBRARY_PATH",envp);
  479.     }
  480.     else
  481.       _dl_library_path = 0;
  482.   }
  483.  
  484.   /* OK, we now have the application in the list, and we have some
  485.      basic stuff in place.  Now search through the list for other shared
  486.      libraries that should be loaded, and insert them on the list in the
  487.      correct order. */
  488.  
  489.   {
  490.     struct elf_resolve *tcurr;
  491.     struct elf_resolve * tpnt1;
  492.     char * lpnt;
  493.  
  494.     tcurr = _dl_loaded_modules;
  495.     do{
  496.       for(dpnt = (struct dynamic *) tcurr->dynamic_addr; dpnt->d_tag; dpnt++)
  497.     {
  498.       
  499.       if(dpnt->d_tag == DT_NEEDED)
  500.         {
  501.           lpnt = tcurr->loadaddr + tcurr->dynamic_info[DT_STRTAB] + 
  502.         dpnt->d_un.d_val;
  503.           if(tpnt && _dl_strcmp(lpnt, tpnt->libname) == 0) {
  504.         struct elf_resolve * ttmp;
  505.         ttmp = _dl_loaded_modules;
  506.         while(ttmp->next) ttmp = ttmp->next;
  507.         ttmp->next = tpnt;
  508.         tpnt->next = NULL;
  509.         rpnt->next = 
  510.            (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  511.         rpnt = rpnt->next;
  512.         rpnt->dyn = tpnt;
  513.         rpnt->next = NULL;
  514.         tpnt->usage_count++;
  515.         tpnt->symbol_scope = _dl_symbol_tables;
  516.         tpnt = NULL;
  517.         continue;
  518.           };
  519.           if(!(tpnt1 = _dl_load_shared_library(_dl_symbol_tables, lpnt))) {
  520.         SEND_STDERR("Unable to load shared library ");
  521.         SEND_STDERR(tcurr->loadaddr+tcurr->dynamic_info[DT_STRTAB] + 
  522.                 dpnt->d_un.d_val);
  523.         SEND_STDERR("\n");
  524.         _dl_exit(12);
  525.           };
  526.           rpnt->next = 
  527.              (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  528.           rpnt = rpnt->next;
  529.           tpnt1->usage_count++;
  530.           tpnt1->symbol_scope = _dl_symbol_tables;
  531.           rpnt->dyn = tpnt1;
  532.           rpnt->next = NULL;
  533.         };
  534.     }
  535.       
  536.       tcurr = tcurr->next;
  537.     } while(tcurr);
  538.   }
  539.  
  540.   /*
  541.    * Now we need to create the dyn_elf entries that the dynamic linker
  542.    * will use.  We build this from the _dl_loaded_modules linked list.
  543.    */
  544.    
  545.   for(tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  546.   {
  547.     if (!rpnt)
  548.       _dl_symbol_tables = rpnt = 
  549.           (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  550.     else {
  551.       rpnt->next = (struct dyn_elf *) _dl_malloc(sizeof(struct dyn_elf));
  552.       rpnt = rpnt->next;
  553.     }
  554.     rpnt->dyn = tpnt;
  555.     rpnt->next = NULL;
  556.     tpnt->usage_count++;
  557.     tpnt->symbol_scope = _dl_symbol_tables;
  558.   }  
  559.  
  560.   _dl_trace_loaded_objects = _dl_getenv("LD_TRACE_LOADED_OBJECTS",envp);
  561.  
  562.     /* ldd uses uses this.  I am not sure how you pick up the other flags */ 
  563.   if(_dl_trace_loaded_objects)
  564.     { struct elf_resolve * ttmp;
  565.       int shared = 0;
  566.       for(ttmp = _dl_loaded_modules->next; ttmp; ttmp = ttmp->next)
  567.     {
  568.       shared++;
  569.       SEND_STDERR("Linux ELF dynamic linker: ");
  570.       SEND_STDERR(argv[0]);
  571.       SEND_STDERR(": file loaded: ");
  572.       SEND_STDERR(ttmp->libname);
  573.       SEND_STDERR("\n");
  574.     };
  575.       if (!shared)
  576.       {
  577.       SEND_STDERR(argv[0]);
  578.       SEND_STDERR(": statically linked\n");
  579.       }
  580.       _dl_warn = _dl_getenv("LD_WARN",envp);
  581.       if(!_dl_warn) _dl_exit(0);
  582.     };
  583.   
  584.   /*
  585.    * If the program interpreter is not in the module chain, add it.  This will
  586.    * be required for dlopen to be able to access the internal functions in the 
  587.    * dynamic linker.
  588.    */
  589.   if(tpnt) {
  590.     struct elf_resolve * tcurr;
  591.  
  592.     tcurr = _dl_loaded_modules;
  593.     while(tcurr->next) tcurr = tcurr->next;
  594.     tpnt->next = NULL;
  595.     tcurr->next = tpnt;
  596.     tpnt = NULL;
  597.   }
  598.  
  599.   /*
  600.    * OK, now all of the kids are tucked into bed in their proper addresses.
  601.    * Now we go through and look for REL and RELA records that indicate fixups
  602.    * to the GOT tables.  We need to do this in reverse order so that COPY
  603.    * directives work correctly */
  604.   goof = _dl_fixup(_dl_loaded_modules);
  605.  
  606.   /* Some flavors of SVr4 do not generate the R_386_COPY directive,
  607.    and we have to manually search for entries that require fixups. 
  608.    Solaris gets this one right, from what I understand.  */
  609.  
  610.   goof +=  _dl_copy_fixups(_dl_symbol_tables);
  611.   if(goof || _dl_trace_loaded_objects) _dl_exit(0);
  612.  
  613.   /* OK, at this point things are pretty much ready to run.  Now we
  614.      need to touch up a few items that are required, and then
  615.      we can let the user application have at it.  Note that
  616.      the dynamic linker itself is not guaranteed to be fully
  617.      dynamicly linked if we are using ld.so.1, so we have to look
  618.      up each symbol individually. */
  619.  
  620.  
  621.   _dl_brkp = (unsigned int *) _dl_find_hash("___brk_addr", NULL, 1, NULL);
  622.   if (_dl_brkp) *_dl_brkp = brk_addr;
  623.  
  624. #ifdef IBCS_COMPATIBLE
  625.   _dl_envp = (unsigned int *) _dl_find_hash("_environ", NULL, 1, NULL);
  626. #else
  627.   _dl_envp = (unsigned int *) _dl_find_hash("__environ", NULL, 1, NULL);
  628. #endif
  629.  
  630.   if (_dl_envp) *_dl_envp = (unsigned int) envp;
  631.  
  632.   {
  633.     int i;
  634.     struct elf_phdr * ppnt;
  635.  
  636.   /* We had to set the protections of all pages to R/W for dynamic linking.
  637.      Set text pages back to R/O */
  638.   for(tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  639.     for(ppnt = tpnt->ppnt, i=0; i < tpnt->n_phent; i++, ppnt++)
  640.       if(ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W) &&
  641.      tpnt->dynamic_info[DT_TEXTREL])
  642.     _dl_mprotect((void *) (tpnt->loadaddr + (ppnt->p_vaddr & 0xfffff000)),
  643.              (ppnt->p_vaddr & 0xfff) + (unsigned int) ppnt->p_filesz,
  644.              LXFLAGS(ppnt->p_flags));
  645.  
  646.   }
  647.  
  648.   _dl_atexit = (int (*)(void *)) _dl_find_hash("atexit", NULL, 1, NULL);
  649.  
  650.   for(tpnt = _dl_loaded_modules; tpnt; tpnt = tpnt->next)
  651.     {
  652.       /* Apparently crt1 for the application is responsible for handling this.
  653.        * We only need to run the init/fini for shared libraries
  654.        */
  655.       if (tpnt->libtype == elf_executable) continue;
  656.       if(tpnt->dynamic_info[DT_INIT]) {
  657.     _dl_elf_init = (int (*)(void)) (tpnt->loadaddr + 
  658.                     tpnt->dynamic_info[DT_INIT]);
  659.     (*_dl_elf_init)();
  660.       }
  661.       if(_dl_atexit && tpnt->dynamic_info[DT_FINI]) (*_dl_atexit)(tpnt->loadaddr + 
  662.                          tpnt->dynamic_info[DT_FINI]);
  663.    }
  664.  
  665.   /* OK we are done here.  Turn out the lights, and lock up. */
  666.   _dl_elf_main = (int (*)(int, char**, char**)) dl_data[9];
  667.  
  668.   __asm__ volatile ("leave\n\t" \
  669.             "jmp *%%eax\n\t"
  670.             : "=a" (status) :
  671.             "d" (_dl_interpreter_exit), "a" (_dl_elf_main));
  672. }
  673.  
  674. int _dl_fixup(struct elf_resolve * tpnt)
  675. {
  676.   int goof = 0;
  677.   if(tpnt->next) goof += _dl_fixup(tpnt->next);
  678.  
  679.   if(tpnt->dynamic_info[DT_REL]) {
  680.     if (tpnt->init_flag & RELOCS_DONE) return goof;
  681.     tpnt->init_flag |= RELOCS_DONE;
  682.    
  683.     goof += _dl_parse_relocation_information(tpnt, tpnt->dynamic_info[DT_REL],
  684.                          tpnt->dynamic_info[DT_RELSZ], 0);
  685.   }
  686.   if(tpnt->dynamic_info[DT_JMPREL])
  687.     {
  688.       if (tpnt->init_flag & JMP_RELOCS_DONE) return goof;
  689.       tpnt->init_flag |= JMP_RELOCS_DONE;
  690.       
  691.       if(! _dl_not_lazy || *_dl_not_lazy == 0)
  692.     _dl_parse_lazy_relocation_information(tpnt, tpnt->dynamic_info[DT_JMPREL],
  693.                           tpnt->dynamic_info[DT_PLTRELSZ], 0);
  694.       else
  695.     goof +=  _dl_parse_relocation_information(tpnt,
  696.                           tpnt->dynamic_info[DT_JMPREL],
  697.                           tpnt->dynamic_info[DT_PLTRELSZ], 0);
  698.     };
  699.   if(tpnt->dynamic_info[DT_RELA]) {
  700.     SEND_STDERR("Unable to handle RELA relocation records\n");
  701.     _dl_exit(1);
  702.   };
  703.   return goof;
  704. }
  705.  
  706. void * _dl_malloc(int size) {
  707.   void * retval;
  708.   retval = _dl_malloc_addr;
  709.   _dl_malloc_addr += size;
  710.   return retval;
  711. }
  712.  
  713. char * _dl_getenv(char * symbol, char ** envp)
  714. {
  715.   char * pnt;
  716.   char * pnt1;
  717.   while ((pnt = *envp++)) {
  718.     pnt1 = symbol;
  719.     while(*pnt && *pnt1 && *pnt == *pnt1) {pnt1++; pnt++;};
  720.     if(!*pnt || *pnt != '=' || *pnt1) continue;
  721.     return pnt+1;
  722.   }
  723.   return 0;
  724. }
  725.  
  726. char * _dl_strdup(const char * string){
  727.   void * retval;
  728.   char * pnt;
  729.  
  730.   pnt = retval = _dl_malloc_addr;
  731.   while(*string)
  732.     *pnt++ = *string++;
  733.   *pnt++ = 0;
  734.   _dl_malloc_addr = pnt;
  735.   return retval;
  736. }
  737.  
  738. /* In principle we could do the .fini stuff here, but we already
  739.    registered this stuff with atexit */
  740. int _dl_interpreter_exit(int exitcode){
  741. /*  SEND_STDERR("Hey, look where I am!\n"); */
  742.   return 0;
  743. }
  744.