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 / hash.c next >
Encoding:
C/C++ Source or Header  |  1994-12-08  |  6.0 KB  |  210 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. /* Various symbol table handling functions, including symbol lookup */
  22.  
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #ifdef IBCS_COMPATIBLE
  27. #include <ibcs/unistd.h>
  28. #else
  29. #include <linux/unistd.h>
  30. #endif
  31. #include "hash.h"
  32. #include "linuxelf.h"
  33. #include "syscall.h"
  34. #include "string.h"
  35.  
  36.  
  37. /*
  38.  * This is the start of the linked list that describes all of the files present
  39.  * in the system with pointers to all of the symbol, string, and hash tables, 
  40.  * as well as all of the other good stuff in the binary.
  41.  */
  42.  
  43. struct elf_resolve * _dl_loaded_modules = NULL;
  44.  
  45. /*
  46.  * This is the list of modules that are loaded when the image is first
  47.  * started.  As we add more via dlopen, they get added into other
  48.  * chains.
  49.  */
  50. struct dyn_elf * _dl_symbol_tables = NULL;
  51.  
  52. /*
  53.  * This is the hash function that is used by the ELF linker to generate
  54.  * the hash table that each executable and library is required to
  55.  * have.  We need it to decode the hash table.
  56.  */
  57.  
  58. unsigned long _dl_elf_hash(const char * name){
  59.   unsigned long hash = 0;
  60.   unsigned long tmp;
  61.  
  62.   while (*name){
  63.     hash = (hash << 4) + *name++;
  64.     if((tmp = hash & 0xf0000000)) hash ^= tmp >> 24;
  65.     hash &= ~tmp;
  66.   };
  67.   return hash;
  68. }
  69.  
  70. /*
  71.  * Check to see if a library has already been added to the hash chain.
  72.  */
  73. struct elf_resolve * _dl_check_hashed_files(char * libname){
  74.   struct elf_resolve * tpnt;
  75.   tpnt = _dl_loaded_modules;
  76.   while(tpnt){
  77.     if(_dl_strcmp(tpnt->libname, libname) == 0) return tpnt;
  78.     tpnt = tpnt->next;
  79.   };
  80.   return NULL;
  81. }
  82.  
  83. /*
  84.  * We call this function when we have just read an ELF library or executable.
  85.  * We add the relevant info to the symbol chain, so that we can resolve all
  86.  * externals properly.
  87.  */
  88.  
  89. struct elf_resolve * _dl_add_elf_hash_table(char * libname,
  90.                     char * loadaddr,
  91.                     unsigned int * dynamic_info,
  92.                     unsigned int dynamic_addr,
  93.                     unsigned int dynamic_size){
  94.   unsigned int *  hash_addr;
  95.   struct elf_resolve * tpnt;
  96.   int i;
  97.  
  98.   if (!_dl_loaded_modules) {
  99.     tpnt = _dl_loaded_modules = 
  100.       (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  101.     _dl_memset (tpnt, 0, sizeof (*tpnt));
  102.   }
  103.   else {
  104.     tpnt = _dl_loaded_modules;
  105.     while(tpnt->next) tpnt = tpnt->next;
  106.     tpnt->next = (struct elf_resolve *) _dl_malloc(sizeof(struct elf_resolve));
  107.     _dl_memset (tpnt->next, 0, sizeof (*(tpnt->next)));
  108.     tpnt->next->prev = tpnt;
  109.     tpnt = tpnt->next;
  110.   };
  111.   
  112.   hash_addr = (unsigned int *) (dynamic_info[DT_HASH] + loadaddr);
  113.   tpnt->next = NULL;
  114.   tpnt->init_flag = 0;
  115.   tpnt->nbucket = *hash_addr++;
  116.   tpnt->nchain = *hash_addr++;
  117.   tpnt->libname = _dl_strdup(libname);
  118.   tpnt->elf_buckets = hash_addr;
  119.   hash_addr += tpnt->nbucket;
  120.   tpnt->dynamic_addr = dynamic_addr;
  121.   tpnt->dynamic_size = dynamic_size;
  122.   tpnt->libtype = loaded_file;
  123.  
  124.   tpnt->chains = hash_addr;
  125.   tpnt->loadaddr = loadaddr;
  126.   for(i=0; i<24; i++) tpnt->dynamic_info[i] = dynamic_info[i];
  127.   return tpnt;
  128. }
  129.  
  130.  
  131. /*
  132.  * This function resolves externals, and this is either called when we process
  133.  * relocations or when we call an entry in the PLT table for the first time.
  134.  */
  135.  
  136. char * _dl_find_hash(char * name, struct dyn_elf * rpnt1, 
  137.              unsigned int instr_addr, struct elf_resolve * f_tpnt, int copyrel){
  138.   struct elf_resolve * tpnt;
  139.   int si;
  140.   char * pnt;
  141.   char * strtab;
  142.   struct Elf32_Sym * symtab;
  143.   unsigned int elf_hash_number, hn;
  144.   char * weak_result;
  145.   struct elf_resolve * first_def;
  146.   struct dyn_elf * rpnt, first;
  147.  
  148.   weak_result = 0;
  149.   elf_hash_number = _dl_elf_hash(name);
  150.  
  151.   /* A quick little hack to make sure that any symbol in the executable
  152.   will be preferred to one in a shared library.  This is necessary so
  153.   that any shared library data symbols referenced in the executable
  154.   will be seen at the same address by the executable, shared libraries
  155.   and dynamically loaded code. -Rob Ryan (robr@cmu.edu) */
  156.   if(!copyrel && rpnt1) {
  157.       first=(*_dl_symbol_tables);
  158.       first.next=rpnt1;
  159.       rpnt1=(&first);
  160.   }
  161.   
  162.   for(rpnt = (rpnt1 ? rpnt1 : _dl_symbol_tables); 
  163.       rpnt; rpnt = rpnt->next) {
  164.     tpnt = rpnt->dyn;
  165.     hn = elf_hash_number % tpnt->nbucket;
  166.     symtab = (struct Elf32_Sym *) (tpnt->dynamic_info[DT_SYMTAB] + 
  167.                    tpnt->loadaddr);
  168.     strtab = (char *) (tpnt->dynamic_info[DT_STRTAB] + tpnt->loadaddr);
  169.     /*
  170.      * This crap is required because the first instance of a symbol on the
  171.      * chain will be used for all symbol references.  Thus this instance
  172.      * must be resolved to an address that contains the actual function,
  173.      */
  174.  
  175.     first_def = NULL;
  176.  
  177.     for(si = tpnt->elf_buckets[hn]; si; si = tpnt->chains[si]){
  178.       pnt = strtab + symtab[si].st_name;
  179.  
  180.       if(_dl_strcmp(pnt, name) == 0 && 
  181.      (ELF32_ST_TYPE(symtab[si].st_info) == STT_FUNC ||
  182.       ELF32_ST_TYPE(symtab[si].st_info) == STT_NOTYPE ||
  183.       ELF32_ST_TYPE(symtab[si].st_info) == STT_OBJECT) &&
  184.      symtab[si].st_value != 0) {
  185.  
  186.     /* Here we make sure that we find a module where the symbol is
  187.      * actually defined.
  188.      */
  189.  
  190.     if(!first_def) first_def = tpnt;
  191.     if(f_tpnt && first_def == f_tpnt && symtab[si].st_shndx == 0)
  192.       continue;
  193.  
  194.     switch(ELF32_ST_BIND(symtab[si].st_info)){
  195.     case STB_GLOBAL:
  196.       return tpnt->loadaddr + symtab[si].st_value;
  197.     case STB_WEAK:
  198.       if (!weak_result) weak_result = tpnt->loadaddr + symtab[si].st_value;
  199.         break;
  200.     default:  /* Do local symbols need to be examined? */
  201.       break;
  202.     }
  203.       }
  204.     }
  205.   }
  206.   return weak_result;
  207. }
  208.  
  209.  
  210.