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 / readelflib1.c < prev   
Encoding:
C/C++ Source or Header  |  1994-10-20  |  12.1 KB  |  422 lines

  1. /* Load an ELF sharable library into memory.
  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. /* This file contains the helper routines to load an ELF sharable
  22.    library into memory and add the symbol table info to the chain. */
  23.  
  24. #include <fcntl.h>
  25. #include <errno.h>
  26. #include <string.h>
  27. #include <stdlib.h>
  28. #include <sys/mman.h>
  29. #include "hash.h"
  30. #include "linuxelf.h"
  31. #include "elf_target.h"
  32. #ifdef IBCS_COMPATIBLE
  33. #include <ibcs/unistd.h>
  34. #else
  35. #include <linux/unistd.h>
  36. #endif
  37. #include "syscall.h"
  38. #include "string.h"
  39.  
  40. /*
  41.  * Used to return error codes back to dlopen et. al.
  42.  */
  43.  
  44. unsigned int _dl_error_number;
  45. unsigned int _dl_internal_error_number;
  46.  
  47. struct elf_resolve * _dl_load_shared_library(struct dyn_elf * dpnt,
  48.     char * full_libname){
  49.   char * pnt, *pnt1, *pnt2;
  50.   struct elf_resolve * tpnt, *tpnt1;
  51.   char mylibname[1024];
  52.   char * libname;
  53.  
  54.   _dl_internal_error_number = 0;
  55.   pnt = libname = full_libname;
  56.   while (*pnt) {
  57.     if(*pnt == '/') libname = pnt+1;
  58.     pnt++;
  59.   }
  60.  
  61.   for(; dpnt; dpnt = dpnt->next) {
  62.     tpnt = dpnt->dyn;
  63.     pnt1 = (char *) tpnt->dynamic_info[DT_RPATH];
  64.     if(pnt1) {
  65.       pnt1 += (unsigned int) tpnt->loadaddr + tpnt->dynamic_info[DT_STRTAB];
  66.       while(*pnt1){
  67.     pnt2 = mylibname;
  68.     while(*pnt1 && *pnt1 != ':') *pnt2++ = *pnt1++;
  69.     if(pnt2[-1] != '/') *pnt2++ = '/';
  70.     pnt = libname;
  71.     while(*pnt) *pnt2++  = *pnt++;
  72.     *pnt2++ = 0;
  73.     tpnt1 = _dl_load_elf_shared_library(mylibname, 0);
  74.     if(tpnt1) return tpnt1;
  75.     if(*pnt1 == ':') pnt1++;
  76.       }
  77.     }
  78.   }
  79.  
  80.   pnt1 = _dl_library_path;
  81.   if(*pnt1) {
  82.     while(*pnt1){
  83.       pnt2 = mylibname;
  84.       while(*pnt1 && *pnt1 != ':' && *pnt1 != ';') *pnt2++ = *pnt1++;
  85.       if(pnt2[-1] != '/') *pnt2++ = '/';
  86.       pnt = libname;
  87.       while(*pnt) *pnt2++  = *pnt++;
  88.       *pnt2++ = 0;
  89.       tpnt1 = _dl_load_elf_shared_library(mylibname, 0);
  90.       if(tpnt1) return tpnt1;
  91.       if(*pnt1 == ':' || *pnt1 == ';') pnt1++;
  92.     }
  93.   }
  94.  
  95.   /* Still no luck.  Try one last possibility */
  96.  
  97.   /* If the filename has any '/', try it straight and leave it at that */
  98.   if(libname != full_libname)
  99.      return _dl_load_elf_shared_library(full_libname, 0);
  100.  
  101.   pnt1 = "/usr/lib/";
  102.   pnt = mylibname;
  103.   while(*pnt1) *pnt++ = *pnt1++;
  104.   pnt1 = libname;
  105.   while(*pnt1) *pnt++ = *pnt1++;
  106.   *pnt++ = 0;
  107.  
  108.   tpnt1 = _dl_load_elf_shared_library(mylibname, 0);
  109.   if(tpnt1) return tpnt1;
  110.   
  111.   /* Well, we shot our wad on that one.  All we can do now is punt */
  112.   if (_dl_internal_error_number) _dl_error_number = _dl_internal_error_number;
  113.   return NULL;
  114. }
  115.  
  116. /*
  117.  * Read one ELF library into memory, mmap it into the correct locations and
  118.  * add the symbol info to the symbol chain.  Perform any relocations that
  119.  * are required.
  120.  */
  121.  
  122. struct elf_resolve * _dl_load_elf_shared_library(char * libname, int flag){
  123.   struct elfhdr * epnt;
  124.   unsigned int dynamic_addr = 0;
  125.   unsigned int dynamic_size = 0;
  126.   struct dynamic * dpnt;
  127.   struct elf_resolve * tpnt;
  128.   struct elf_phdr * ppnt;
  129.   int piclib;
  130.   char * status;
  131. #ifdef DEBUG
  132.   int lastmapped = 0;
  133. #endif
  134.   int flags;
  135.   char header[4096];
  136.   int dynamic_info[24];
  137.   int * lpnt;
  138.   char  * zfile = "/dev/zero";
  139.   unsigned int libaddr;
  140.  
  141.   int i;
  142.   int infile, zerofile;
  143.  
  144.   /* If this file is already loaded, skip this step */
  145.   tpnt = _dl_check_hashed_files(libname);
  146.   if(tpnt) return tpnt;
  147.  
  148.   libaddr = 0;
  149.   infile = _dl_open(libname, O_RDONLY);
  150.   if(infile < 0) {
  151.     _dl_internal_error_number = DL_ERROR_NOFILE;
  152.     return NULL;
  153.   }
  154.   zerofile = _dl_open(zfile, O_RDONLY);
  155.   if(zerofile < 0) {
  156.     SEND_STDERR("Unable to open /dev/zero.  Please create.\n");
  157.     _dl_internal_error_number = DL_ERROR_NONULL;
  158.     return NULL;
  159.   }; 
  160.  
  161.   _dl_read(infile, header, sizeof(header));
  162.   epnt = (struct elfhdr *) header;
  163.   if (epnt->e_ident[0] != 0x7f ||
  164.       epnt->e_ident[1] != 'E' ||
  165.       epnt->e_ident[2] != 'L' ||
  166.       epnt->e_ident[3] != 'F') {
  167.     SEND_STDERR(libname);
  168.     SEND_STDERR(" is not an ELF file\n");
  169.     _dl_internal_error_number = DL_ERROR_NOTELF;
  170.     _dl_exit(1);
  171.   };
  172.   
  173.   if((epnt->e_type != ET_DYN) || 
  174.      (epnt->e_machine != MAGIC1 
  175. #ifdef MAGIC2
  176.       && epnt->e_machine != MAGIC2
  177. #endif
  178.       )){
  179.     _dl_internal_error_number = (epnt->e_type != ET_DYN ? DL_ERROR_NOTDYN : DL_ERROR_NOTMAGIC);
  180.     SEND_STDERR(libname);
  181.     SEND_STDERR(" is not an ELF executable for " ELF_TARGET "\n");
  182.     _dl_exit(1);
  183.   };
  184.  
  185.   ppnt = (struct elf_phdr *) &header[epnt->e_phoff];
  186.   for(i=0;i < epnt->e_phnum; i++){
  187.  
  188.     if(ppnt->p_type == PT_DYNAMIC) {
  189.       if(dynamic_addr) SEND_STDERR("Error - more than one dynamic section\n");
  190.       dynamic_addr = ppnt->p_vaddr;
  191.       dynamic_size = ppnt->p_filesz;
  192.     };
  193.     ppnt++;
  194.   };
  195.  
  196.   flags = MAP_PRIVATE;
  197.   piclib = 1;
  198.  
  199.     /* Get the memory to store the library */
  200.   ppnt = (struct elf_phdr *) &header[epnt->e_phoff];
  201.   for(i=0;i < epnt->e_phnum; i++){
  202.     if(ppnt->p_type == PT_LOAD) {
  203.  
  204.       /* See if this is a PIC library. */
  205.       if(i == 0 && ppnt->p_vaddr > 0x1000000) {
  206.     piclib = 0;
  207.     flags |= MAP_FIXED;
  208.       }
  209.  
  210.  
  211.       
  212.       if(ppnt->p_flags & PF_W) {
  213.     unsigned int map_size;
  214.     char * cpnt;
  215.     
  216.     status = (char *) _dl_mmap((char *) (libaddr +
  217.                          (ppnt->p_vaddr & 0xfffff000)),
  218.               (ppnt->p_vaddr & 0xfff) + ppnt->p_filesz, 
  219.               LXFLAGS(ppnt->p_flags), 
  220.               flags, infile,
  221.               ppnt->p_offset & 0x7ffff000);
  222.     
  223.     
  224.     if(!piclib) status = 0;
  225.     
  226.     /* Pad the last page with zeroes. */
  227.     cpnt =(char *) (status + (ppnt->p_vaddr & 0xfff) + ppnt->p_filesz);
  228.     while(((unsigned int) cpnt) & 0xfff) *cpnt++ = 0;
  229.     
  230. /* I am not quite sure if this is completely correct to do or not, but
  231.    the basic way that we handle bss segments is that we mmap /dev/zero if
  232.    there are any pages left over that are not mapped as part of the file */
  233.  
  234.     map_size = (ppnt->p_vaddr + ppnt->p_filesz + 0xfff) & 0xfffff000;
  235.     if(map_size < ppnt->p_vaddr + ppnt->p_memsz)
  236.       status = (char *) _dl_mmap((char *) map_size + libaddr, 
  237.                 ppnt->p_vaddr + ppnt->p_memsz - map_size,
  238.                 LXFLAGS(ppnt->p_flags),
  239.                 flags, zerofile, 0);
  240.       } else
  241.     status = (char *) _dl_mmap((char *) (ppnt->p_vaddr & 0xfffff000) + 
  242.                    libaddr, 
  243.               (ppnt->p_vaddr & 0xfff) + ppnt->p_filesz, 
  244.               LXFLAGS(ppnt->p_flags), 
  245.               flags, infile, 
  246.               ppnt->p_offset & 0x7ffff000);
  247.       if(status == (char *) 0xffffffff) {
  248.     SEND_STDERR("mmap failed.\n")
  249.       _dl_internal_error_number = DL_ERROR_MMAP_FAILED;
  250.       _dl_exit(1);
  251.       };
  252.       if(libaddr == 0 && piclib) {
  253.     libaddr = (unsigned int) status;
  254.     flags |= MAP_FIXED;
  255.       };
  256.     };
  257.     ppnt++;
  258.   };
  259.   _dl_close(zerofile);
  260.   _dl_close(infile);
  261.   
  262.   /* For a non-PIC library, the addresses are all absolute */
  263.   if(!piclib) libaddr = 0;
  264.  
  265.   dynamic_addr += (unsigned int) libaddr;
  266.  
  267.  /* 
  268.   * OK, the ELF library is now loaded into VM in the correct locations
  269.   * The next step is to go through and do the dynamic linking (if needed).
  270.   */
  271.   
  272.   /* Start by scanning the dynamic section to get all of the pointers */
  273.   
  274.   if(!dynamic_addr) {
  275.     _dl_internal_error_number = DL_ERROR_NODYNAMIC;
  276.     SEND_STDERR(libname);
  277.     SEND_STDERR(" is missing a DYNAMIC section.\n");
  278.     
  279.     _dl_exit(1);
  280.   }
  281.  
  282.   dpnt = (struct dynamic *) dynamic_addr;
  283.  
  284.   dynamic_size = dynamic_size / sizeof(struct dynamic);
  285.   _dl_memset(dynamic_info, 0, sizeof(dynamic_info));
  286.   for(i=0; i< dynamic_size; i++){
  287.     dynamic_info[dpnt->d_tag] = dpnt->d_un.d_val;
  288.     if(dpnt->d_tag == DT_TEXTREL ||
  289.        SVR4_BUGCOMPAT) dynamic_info[DT_TEXTREL] = 1;
  290.     dpnt++;
  291.   };
  292.  
  293.   /* If the TEXTREL is set, this means that we need to make the pages
  294.      writable before we perform relocations.  Do this now. They get set back
  295.      again later. */
  296.  
  297.   ppnt = (struct elf_phdr *) &header[epnt->e_phoff];
  298.   for(i=0;i < epnt->e_phnum; i++, ppnt++){
  299.     if(ppnt->p_type == PT_LOAD && !(ppnt->p_flags & PF_W))
  300.       _dl_mprotect((void *) (libaddr + (ppnt->p_vaddr & 0xfffff000)),
  301.            (ppnt->p_vaddr & 0xfff) + (unsigned int) ppnt->p_filesz,
  302.            PROT_READ | PROT_WRITE | PROT_EXEC);
  303.   }
  304.  
  305.   tpnt = _dl_add_elf_hash_table(libname, (char *) libaddr, dynamic_info, dynamic_addr, 
  306.                   dynamic_size);
  307.  
  308.   tpnt->ppnt = (struct elf_phdr *) (tpnt->loadaddr + epnt->e_phoff);
  309.   tpnt->n_phent = epnt->e_phnum;
  310.  
  311.   /*
  312.    * OK, the next thing we need to do is to insert the dynamic linker into
  313.    * the proper entry in the GOT so that the PLT symbols can be properly
  314.    * resolved. 
  315.    */
  316.   
  317.   lpnt = (int *) dynamic_info[DT_PLTGOT];
  318.   
  319.   if(lpnt) {
  320.     lpnt = (int *) (dynamic_info[DT_PLTGOT] + ((int) libaddr));
  321.     lpnt[1] = (int) tpnt;
  322.     lpnt[2] = (int) _dl_linux_resolve;
  323.   };
  324.   
  325.   return tpnt;
  326. }
  327.  
  328. /* Ugly, ugly.  Some versions of the SVr4 linker fail to generate COPY
  329.    relocations for global variables that are present both in the image and
  330.    the shared library.  Go through and do it manually.  If the images
  331.    are guaranteed to be generated by a trustworthy linker, then this
  332.    step can be skipped. */
  333.  
  334. int _dl_copy_fixups(struct dyn_elf * rpnt)
  335. {
  336.   int goof = 0;
  337.   struct elf_resolve * tpnt;
  338. #ifdef BROKEN_LINKER
  339.   int hn, hn1, si, si1;
  340.   unsigned int elf_hash_number;
  341.   struct elf_resolve * tpnt1;
  342.   struct dyn_elf * rpnt1;
  343.   char * strtab, *strtab1;
  344.   struct Elf32_Sym * symtab, *symtab1;
  345.   char * pnt, *pnt1;
  346. #endif
  347.  
  348.   if(rpnt->next) goof += _dl_copy_fixups(rpnt->next);
  349.   else return 0;
  350.  
  351.   tpnt = rpnt->dyn;
  352.     
  353.   if (tpnt->init_flag & COPY_RELOCS_DONE) return goof;
  354.   tpnt->init_flag |= COPY_RELOCS_DONE;
  355.   
  356. /* Use BROKEN_LINKER if the SVr4 linker is being used. */
  357. #ifndef BROKEN_LINKER
  358.   goof += _dl_parse_copy_information(rpnt, tpnt->dynamic_info[DT_REL],
  359.                        tpnt->dynamic_info[DT_RELSZ], 0);
  360.  
  361. #else
  362.   /* OK, now scan the symbol table for this module */
  363.  
  364.   symtab = (struct Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + 
  365.                  tpnt->loadaddr);
  366.   strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  367.  
  368.   for(hn = 0; hn < tpnt->nbucket; hn++) {
  369.     if(!tpnt->elf_buckets[hn]) continue;
  370.     for(si = tpnt->elf_buckets[hn]; si; si = tpnt->chains[si]) {
  371.       if(ELF32_ST_TYPE(symtab[si].st_info) != STT_OBJECT) continue;
  372.       if(ELF32_ST_BIND(symtab[si].st_info) != STB_GLOBAL) continue; 
  373.       if(symtab[si].st_value == 0 || symtab[si].st_shndx == 0) continue;
  374.       if(!symtab[si].st_size) continue;
  375.       pnt = strtab + symtab[si].st_name;
  376.  
  377.       elf_hash_number = _dl_elf_hash(pnt);
  378.  
  379.       /* OK, we have a candidate.  Now search for the same symbol in other
  380.      libraries.  The hash number will be the same. */
  381.       for(rpnt1 = rpnt->next; rpnt1; rpnt1 = tpnt1->next)
  382.     {
  383.       tpnt1 = rpnt1->dyn;
  384.       hn1 = elf_hash_number % tpnt1->nbucket;
  385.       if(!tpnt1->elf_buckets[hn1]) continue;
  386.  
  387.       symtab1 = (struct Elf32_Sym *) (tpnt1->dynamic_info[DT_SYMTAB] + 
  388.                      tpnt1->loadaddr);
  389.       strtab1 = (char *) (tpnt1->dynamic_info[DT_STRTAB] + tpnt1->loadaddr);
  390.       for(si1 = tpnt1->elf_buckets[hn1]; si1; si1 = tpnt1->chains[si1]) {
  391.         pnt1 = strtab1 + symtab1[si1].st_name;
  392.         if(ELF32_ST_TYPE(symtab1[si1].st_info) != STT_OBJECT) continue;
  393.         if(ELF32_ST_BIND(symtab1[si1].st_info) != STB_GLOBAL) continue; 
  394.         if(symtab1[si1].st_value == 0 || symtab1[si1].st_shndx == 0) continue;
  395.         if(!symtab1[si1].st_size) continue;
  396.         if(symtab1[si1].st_size != symtab[si].st_size) continue;
  397.         pnt1 = strtab1 + symtab1[si1].st_name;
  398.         if(_dl_strcmp(pnt, pnt1) == 0) {
  399.           char * to, *from;
  400.           int count;
  401.           to = tpnt->loadaddr + symtab[si].st_value;
  402.           from = tpnt1->loadaddr + symtab1[si1].st_value;
  403.           count = symtab[si].st_size;
  404.           while(count--) *to++ = *from++;
  405.           /* Cannot use memcpy - SVr4 assembler complains about dup label*/
  406. #if 0
  407.           SEND_STDERR("Global symbol in ");
  408.           SEND_STDERR(tpnt1->libname);
  409.           SEND_STDERR(" ");
  410.           SEND_STDERR(pnt);
  411.           SEND_STDERR("\n");
  412. #endif
  413.         };
  414.       };
  415.     };
  416.     };
  417.   };
  418. #endif
  419.   return goof;
  420. }
  421.  
  422.