"Fossies" - the Fresh Open Source Software Archive

Member "modutils-2.4.27/obj/obj_sh.c" (26 Sep 2001, 5222 Bytes) of package /linux/misc/old/modutils-2.4.27.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "obj_sh.c" see the Fossies "Dox" file reference documentation.

    1 /* SH specific support for Elf loading and relocation.
    2    Copyright 1999 Linux International.
    3 
    4    Contributed by Kazumoto Kojima <kkojima@rr.iij4u.or.jp>
    5 
    6    This file is part of the Linux modutils.
    7 
    8    This program is free software; you can redistribute it and/or modify it
    9    under the terms of the GNU General Public License as published by the
   10    Free Software Foundation; either version 2 of the License, or (at your
   11    option) any later version.
   12 
   13    This program is distributed in the hope that it will be useful, but
   14    WITHOUT ANY WARRANTY; without even the implied warranty of
   15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   16    General Public License for more details.
   17 
   18    You should have received a copy of the GNU General Public License
   19    along with this program; if not, write to the Free Software Foundation,
   20    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
   21 
   22 #include <string.h>
   23 #include <assert.h>
   24 
   25 #include <module.h>
   26 #include <obj.h>
   27 #include <util.h>
   28 
   29 
   30 /*======================================================================*/
   31 
   32 struct sh_got_entry
   33 {
   34   int offset;
   35   unsigned offset_done : 1;
   36   unsigned reloc_done : 1;
   37 };
   38 
   39 struct sh_file
   40 {
   41   struct obj_file root;
   42   struct obj_section *got;
   43 };
   44 
   45 struct sh_symbol
   46 {
   47   struct obj_symbol root;
   48   struct sh_got_entry gotent;
   49 };
   50 
   51 
   52 /*======================================================================*/
   53 
   54 struct obj_file *
   55 arch_new_file (void)
   56 {
   57   struct sh_file *f;
   58   f = xmalloc(sizeof(*f));
   59   f->got = NULL;
   60   return &f->root;
   61 }
   62 
   63 struct obj_section *
   64 arch_new_section (void)
   65 {
   66   return xmalloc(sizeof(struct obj_section));
   67 }
   68 
   69 struct obj_symbol *
   70 arch_new_symbol (void)
   71 {
   72   struct sh_symbol *sym;
   73   sym = xmalloc(sizeof(*sym));
   74   memset(&sym->gotent, 0, sizeof(sym->gotent));
   75   return &sym->root;
   76 }
   77 
   78 enum obj_reloc
   79 arch_apply_relocation (struct obj_file *f,
   80                struct obj_section *targsec,
   81                struct obj_section *symsec,
   82                struct obj_symbol *sym,
   83                Elf32_Rela *rel,
   84                Elf32_Addr v)
   85 {
   86   struct sh_file *ifile = (struct sh_file *)f;
   87   struct sh_symbol *isym  = (struct sh_symbol *)sym;
   88 
   89   Elf32_Addr *loc = (Elf32_Addr *)(targsec->contents + rel->r_offset);
   90   Elf32_Addr dot = targsec->header.sh_addr + rel->r_offset;
   91   Elf32_Addr got = ifile->got ? ifile->got->header.sh_addr : 0;
   92 
   93   enum obj_reloc ret = obj_reloc_ok;
   94 
   95   switch (ELF32_R_TYPE(rel->r_info))
   96     {
   97     case R_SH_NONE:
   98       break;
   99 
  100     case R_SH_DIR32:
  101       *loc += v;
  102       break;
  103 
  104     case R_SH_REL32:
  105       *loc += v - dot;
  106       break;
  107 
  108     case R_SH_PLT32:
  109       *loc = v - dot;
  110       break;
  111 
  112     case R_SH_GLOB_DAT:
  113     case R_SH_JMP_SLOT:
  114       *loc = v;
  115       break;
  116 
  117     case R_SH_RELATIVE:
  118       *loc = f->baseaddr + rel->r_addend;
  119       break;
  120 
  121     case R_SH_GOTPC:
  122       assert(got != 0);
  123       *loc = got - dot + rel->r_addend;
  124       break;
  125 
  126     case R_SH_GOT32:
  127       assert(isym != NULL);
  128       if (!isym->gotent.reloc_done)
  129     {
  130       isym->gotent.reloc_done = 1;
  131       *(Elf32_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
  132     }
  133       *loc = isym->gotent.offset + rel->r_addend;
  134       break;
  135 
  136     case R_SH_GOTOFF:
  137       assert(got != 0);
  138       *loc = v - got;
  139       break;
  140 
  141     default:
  142       ret = obj_reloc_unhandled;
  143       break;
  144     }
  145 
  146   return ret;
  147 }
  148 
  149 int
  150 arch_create_got (struct obj_file *f)
  151 {
  152   struct sh_file *ifile = (struct sh_file *)f;
  153   int i, n, offset = 0, gotneeded = 0;
  154 
  155   n = ifile->root.header.e_shnum;
  156   for (i = 0; i < n; ++i)
  157     {
  158       struct obj_section *relsec, *symsec, *strsec;
  159       Elf32_Rela *rel, *relend;
  160       Elf32_Sym *symtab;
  161       const char *strtab;
  162 
  163       relsec = ifile->root.sections[i];
  164       if (relsec->header.sh_type != SHT_REL)
  165     continue;
  166 
  167       symsec = ifile->root.sections[relsec->header.sh_link];
  168       strsec = ifile->root.sections[symsec->header.sh_link];
  169 
  170       rel = (Elf32_Rela *)relsec->contents;
  171       relend = rel + (relsec->header.sh_size / sizeof(Elf32_Rela));
  172       symtab = (Elf32_Sym *)symsec->contents;
  173       strtab = (const char *)strsec->contents;
  174 
  175       for (; rel < relend; ++rel)
  176     {
  177       struct sh_symbol *intsym;
  178 
  179       switch (ELF32_R_TYPE(rel->r_info))
  180         {
  181         case R_SH_GOTPC:
  182         case R_SH_GOTOFF:
  183           gotneeded = 1;
  184         default:
  185           continue;
  186 
  187         case R_SH_GOT32:
  188           break;
  189         }
  190 
  191       obj_find_relsym(intsym, f, &ifile->root, rel, symtab, strtab);
  192 
  193       if (!intsym->gotent.offset_done)
  194         {
  195           intsym->gotent.offset_done = 1;
  196           intsym->gotent.offset = offset;
  197           offset += 4;
  198         }
  199     }
  200     }
  201 
  202   if (offset > 0 || gotneeded)
  203     ifile->got = obj_create_alloced_section(&ifile->root, ".got", 4, offset, SHF_WRITE);
  204 
  205   return 1;
  206 }
  207 
  208 int
  209 arch_init_module (struct obj_file *f, struct module *mod)
  210 {
  211   return 1;
  212 }
  213 
  214 int
  215 arch_finalize_section_address(struct obj_file *f, Elf32_Addr base)
  216 {
  217   int  i, n = f->header.e_shnum;
  218 
  219   f->baseaddr = base;
  220   for (i = 0; i < n; ++i)
  221     f->sections[i]->header.sh_addr += base;
  222   return 1;
  223 }
  224 
  225 int
  226 arch_load_proc_section(struct obj_section *sec, int fp)
  227 {
  228     /* Assume it's just a debugging section that we can safely
  229        ignore ...  */
  230     sec->contents = NULL;
  231 
  232     return 0;
  233 }
  234 
  235 int
  236 arch_archdata (struct obj_file *fin, struct obj_section *sec)
  237 {
  238   return 0;
  239 }