Back to index

cell-binutils  2.17cvs20070401
elf32-d10v.c
Go to the documentation of this file.
00001 /* D10V-specific support for 32-bit ELF
00002    Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
00003    2007 Free Software Foundation, Inc.
00004    Contributed by Martin Hunt (hunt@cygnus.com).
00005 
00006    This file is part of BFD, the Binary File Descriptor library.
00007 
00008    This program is free software; you can redistribute it and/or modify
00009    it under the terms of the GNU General Public License as published by
00010    the Free Software Foundation; either version 2 of the License, or
00011    (at your option) any later version.
00012 
00013    This program is distributed in the hope that it will be useful,
00014    but WITHOUT ANY WARRANTY; without even the implied warranty of
00015    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016    GNU General Public License for more details.
00017 
00018    You should have received a copy of the GNU General Public License
00019    along with this program; if not, write to the Free Software
00020    Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
00021    MA 02110-1301, USA.  */
00022 
00023 #include "bfd.h"
00024 #include "sysdep.h"
00025 #include "libbfd.h"
00026 #include "elf-bfd.h"
00027 #include "elf/d10v.h"
00028 
00029 /* Use REL instead of RELA to save space.  */
00030 #define USE_REL      1
00031 
00032 static reloc_howto_type elf_d10v_howto_table[] =
00033 {
00034   /* This reloc does nothing.  */
00035   HOWTO (R_D10V_NONE,              /* Type.  */
00036         0,                  /* Rightshift.  */
00037         2,                  /* Size (0 = byte, 1 = short, 2 = long).  */
00038         32,                 /* Bitsize.  */
00039         FALSE,                     /* PC_relative.  */
00040         0,                  /* Bitpos.  */
00041         complain_overflow_dont,/* Complain_on_overflow.  */
00042         bfd_elf_generic_reloc, /* Special_function.  */
00043         "R_D10V_NONE",             /* Name.  */
00044         FALSE,                     /* Partial_inplace.  */
00045         0,                  /* Src_mask.  */
00046         0,                  /* Dst_mask.  */
00047         FALSE),             /* PCrel_offset.  */
00048 
00049   /* An PC Relative 10-bit relocation, shifted by 2, right container.  */
00050   HOWTO (R_D10V_10_PCREL_R, /* Type.  */
00051         2,                    /* Rightshift.  */
00052         2,                    /* Size (0 = byte, 1 = short, 2 = long).  */
00053         8,                    /* Bitsize.  */
00054         TRUE,               /* PC_relative.  */
00055         0,                    /* Bitpos.  */
00056         complain_overflow_signed, /* Complain_on_overflow.  */
00057         bfd_elf_generic_reloc, /* Special_function.  */
00058         "R_D10V_10_PCREL_R",       /* Name.  */
00059         FALSE,                     /* Partial_inplace.  */
00060         0xff,               /* Src_mask.  */
00061         0xff,               /* Dst_mask.  */
00062         TRUE),                     /* PCrel_offset.  */
00063 
00064   /* An PC Relative 10-bit relocation, shifted by 2, left container.  */
00065   HOWTO (R_D10V_10_PCREL_L, /* Type.  */
00066         2,                    /* Rightshift.  */
00067         2,                    /* Size (0 = byte, 1 = short, 2 = long).  */
00068         8,                    /* Bitsize.  */
00069         TRUE,               /* PC_relative.  */
00070         15,                   /* Bitpos.  */
00071         complain_overflow_signed, /* Complain_on_overflow.  */
00072         bfd_elf_generic_reloc, /* Special_function.  */
00073         "R_D10V_10_PCREL_L",       /* Name.  */
00074         FALSE,                     /* Partial_inplace.  */
00075         0x07f8000,          /* Src_mask.  */
00076         0x07f8000,                 /* Dst_mask.  */
00077         TRUE),                     /* PCrel_offset.  */
00078 
00079   /* A 16 bit absolute relocation.  */
00080   HOWTO (R_D10V_16,         /* Type.  */
00081         0,                  /* Rightshift.  */
00082         1,                  /* Size (0 = byte, 1 = short, 2 = long).  */
00083         16,                 /* Bitsize.  */
00084         FALSE,                     /* PC_relative.  */
00085         0,                  /* Bitpos.  */
00086         complain_overflow_dont,/* Complain_on_overflow.  */
00087         bfd_elf_generic_reloc, /* Special_function.  */
00088         "R_D10V_16",        /* Name.  */
00089         FALSE,                     /* Partial_inplace.  */
00090         0xffff,             /* Src_mask.  */
00091         0xffff,             /* Dst_mask.  */
00092         FALSE),             /* PCrel_offset.  */
00093 
00094   /* An 18 bit absolute relocation, right shifted 2.  */
00095   HOWTO (R_D10V_18,         /* Type.  */
00096         2,                  /* Rightshift.  */
00097         1,                  /* Size (0 = byte, 1 = short, 2 = long).  */
00098         16,                 /* Bitsize.  */
00099         FALSE,                     /* PC_relative.  */
00100         0,                  /* Bitpos.  */
00101         complain_overflow_dont, /* Complain_on_overflow.  */
00102         bfd_elf_generic_reloc, /* Special_function.  */
00103         "R_D10V_18",        /* Name.  */
00104         FALSE,                     /* Partial_inplace.  */
00105         0xffff,             /* Src_mask.  */
00106         0xffff,             /* Dst_mask.  */
00107         FALSE),             /* PCrel_offset.  */
00108 
00109   /* A relative 18 bit relocation, right shifted by 2.  */
00110   HOWTO (R_D10V_18_PCREL,   /* Type.  */
00111         2,                  /* Rightshift.  */
00112         2,                  /* Size (0 = byte, 1 = short, 2 = long).  */
00113         16,                 /* Bitsize.  */
00114         TRUE,               /* PC_relative.  */
00115         0,                  /* Bitpos.  */
00116         complain_overflow_signed, /* Complain_on_overflow.  */
00117         bfd_elf_generic_reloc, /* Special_function.  */
00118         "R_D10V_18_PCREL",  /* Name.  */
00119         FALSE,                     /* Partial_inplace.  */
00120         0xffff,             /* Src_mask.  */
00121         0xffff,             /* Dst_mask.  */
00122         TRUE),                     /* PCrel_offset.  */
00123 
00124   /* A 32 bit absolute relocation.  */
00125   HOWTO (R_D10V_32,         /* Type.  */
00126         0,                  /* Rightshift.  */
00127         2,                  /* Size (0 = byte, 1 = short, 2 = long).  */
00128         32,                 /* Bitsize.  */
00129         FALSE,                     /* PC_relative.  */
00130         0,                  /* Bitpos.  */
00131         complain_overflow_dont,/* Complain_on_overflow.  */
00132         bfd_elf_generic_reloc, /* Special_function.  */
00133         "R_D10V_32",        /* Name.  */
00134         FALSE,                     /* Partial_inplace.  */
00135         0xffffffff,         /* Src_mask.  */
00136         0xffffffff,         /* Dst_mask.  */
00137         FALSE),             /* PCrel_offset.  */
00138 
00139   /* GNU extension to record C++ vtable hierarchy.  */
00140   HOWTO (R_D10V_GNU_VTINHERIT,     /* Type.  */
00141         0,                     /* Rightshift.  */
00142         2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
00143         0,                     /* Bitsize.  */
00144         FALSE,                 /* PC_relative.  */
00145         0,                     /* Bitpos.  */
00146         complain_overflow_dont,/* Complain_on_overflow.  */
00147         NULL,                  /* Special_function.  */
00148         "R_D10V_GNU_VTINHERIT",/* Name.  */
00149         FALSE,                 /* Partial_inplace.  */
00150         0,                     /* Src_mask.  */
00151         0,                     /* Dst_mask.  */
00152         FALSE),                /* PCrel_offset.  */
00153 
00154   /* GNU extension to record C++ vtable member usage.  */
00155   HOWTO (R_D10V_GNU_VTENTRY,    /* Type.  */
00156         0,                     /* Rightshift.  */
00157         2,                     /* Size (0 = byte, 1 = short, 2 = long).  */
00158         0,                     /* Bitsize.  */
00159         FALSE,                 /* PC_relative.  */
00160         0,                     /* Bitpos.  */
00161         complain_overflow_dont,/* Complain_on_overflow.  */
00162         _bfd_elf_rel_vtable_reloc_fn,  /* Special_function.  */
00163         "R_D10V_GNU_VTENTRY",  /* Name.  */
00164         FALSE,                 /* Partial_inplace.  */
00165         0,                     /* Src_mask.  */
00166         0,                     /* Dst_mask.  */
00167         FALSE),                /* PCrel_offset.  */
00168 };
00169 
00170 /* Map BFD reloc types to D10V ELF reloc types.  */
00171 
00172 struct d10v_reloc_map
00173 {
00174   bfd_reloc_code_real_type bfd_reloc_val;
00175   unsigned char elf_reloc_val;
00176 };
00177 
00178 static const struct d10v_reloc_map d10v_reloc_map[] =
00179 {
00180   { BFD_RELOC_NONE, R_D10V_NONE, },
00181   { BFD_RELOC_D10V_10_PCREL_R, R_D10V_10_PCREL_R },
00182   { BFD_RELOC_D10V_10_PCREL_L, R_D10V_10_PCREL_L },
00183   { BFD_RELOC_16, R_D10V_16 },
00184   { BFD_RELOC_D10V_18, R_D10V_18 },
00185   { BFD_RELOC_D10V_18_PCREL, R_D10V_18_PCREL },
00186   { BFD_RELOC_32, R_D10V_32 },
00187   { BFD_RELOC_VTABLE_INHERIT, R_D10V_GNU_VTINHERIT },
00188   { BFD_RELOC_VTABLE_ENTRY, R_D10V_GNU_VTENTRY },
00189 };
00190 
00191 static reloc_howto_type *
00192 bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00193                              bfd_reloc_code_real_type code)
00194 {
00195   unsigned int i;
00196 
00197   for (i = 0;
00198        i < sizeof (d10v_reloc_map) / sizeof (struct d10v_reloc_map);
00199        i++)
00200     if (d10v_reloc_map[i].bfd_reloc_val == code)
00201       return &elf_d10v_howto_table[d10v_reloc_map[i].elf_reloc_val];
00202 
00203   return NULL;
00204 }
00205 
00206 static reloc_howto_type *
00207 bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
00208                              const char *r_name)
00209 {
00210   unsigned int i;
00211 
00212   for (i = 0;
00213        i < sizeof (elf_d10v_howto_table) / sizeof (elf_d10v_howto_table[0]);
00214        i++)
00215     if (elf_d10v_howto_table[i].name != NULL
00216        && strcasecmp (elf_d10v_howto_table[i].name, r_name) == 0)
00217       return &elf_d10v_howto_table[i];
00218 
00219   return NULL;
00220 }
00221 
00222 /* Set the howto pointer for an D10V ELF reloc.  */
00223 
00224 static void
00225 d10v_info_to_howto_rel (bfd *abfd ATTRIBUTE_UNUSED,
00226                      arelent *cache_ptr,
00227                      Elf_Internal_Rela *dst)
00228 {
00229   unsigned int r_type;
00230 
00231   r_type = ELF32_R_TYPE (dst->r_info);
00232   BFD_ASSERT (r_type < (unsigned int) R_D10V_max);
00233   cache_ptr->howto = &elf_d10v_howto_table[r_type];
00234 }
00235 
00236 static asection *
00237 elf32_d10v_gc_mark_hook (asection *sec,
00238                       struct bfd_link_info *info,
00239                       Elf_Internal_Rela *rel,
00240                       struct elf_link_hash_entry *h,
00241                       Elf_Internal_Sym *sym)
00242 {
00243   if (h != NULL)
00244     switch (ELF32_R_TYPE (rel->r_info))
00245       {
00246       case R_D10V_GNU_VTINHERIT:
00247       case R_D10V_GNU_VTENTRY:
00248        return NULL;
00249       }
00250 
00251   return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
00252 }
00253 
00254 /* Look through the relocs for a section during the first phase.
00255    Since we don't do .gots or .plts, we just need to consider the
00256    virtual table relocs for gc.  */
00257 
00258 static bfd_boolean
00259 elf32_d10v_check_relocs (bfd *abfd,
00260                       struct bfd_link_info *info,
00261                       asection *sec,
00262                       const Elf_Internal_Rela *relocs)
00263 {
00264   Elf_Internal_Shdr *symtab_hdr;
00265   struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
00266   const Elf_Internal_Rela *rel;
00267   const Elf_Internal_Rela *rel_end;
00268 
00269   if (info->relocatable)
00270     return TRUE;
00271 
00272   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
00273   sym_hashes = elf_sym_hashes (abfd);
00274   sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof (Elf32_External_Sym);
00275   if (!elf_bad_symtab (abfd))
00276     sym_hashes_end -= symtab_hdr->sh_info;
00277 
00278   rel_end = relocs + sec->reloc_count;
00279   for (rel = relocs; rel < rel_end; rel++)
00280     {
00281       struct elf_link_hash_entry *h;
00282       unsigned long r_symndx;
00283 
00284       r_symndx = ELF32_R_SYM (rel->r_info);
00285       if (r_symndx < symtab_hdr->sh_info)
00286         h = NULL;
00287       else
00288        {
00289          h = sym_hashes[r_symndx - symtab_hdr->sh_info];
00290          while (h->root.type == bfd_link_hash_indirect
00291                || h->root.type == bfd_link_hash_warning)
00292            h = (struct elf_link_hash_entry *) h->root.u.i.link;
00293        }
00294 
00295       switch (ELF32_R_TYPE (rel->r_info))
00296         {
00297         /* This relocation describes the C++ object vtable hierarchy.
00298            Reconstruct it for later use during GC.  */
00299         case R_D10V_GNU_VTINHERIT:
00300           if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
00301             return FALSE;
00302           break;
00303 
00304         /* This relocation describes which C++ vtable entries are actually
00305            used.  Record for later use during GC.  */
00306         case R_D10V_GNU_VTENTRY:
00307           if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_offset))
00308             return FALSE;
00309           break;
00310         }
00311     }
00312 
00313   return TRUE;
00314 }
00315 
00316 static bfd_vma
00317 extract_rel_addend (bfd *abfd,
00318                   bfd_byte *where,
00319                   reloc_howto_type *howto)
00320 {
00321   bfd_vma insn, val;
00322 
00323   switch (howto->size)
00324     {
00325     case 0:
00326       insn = bfd_get_8 (abfd, where);
00327       break;
00328     case 1:
00329       insn = bfd_get_16 (abfd, where);
00330       break;
00331     case 2:
00332       insn = bfd_get_32 (abfd, where);
00333       break;
00334     default:
00335       abort ();
00336     }
00337 
00338   val = (insn & howto->dst_mask) >> howto->bitpos << howto->rightshift;
00339   /* We should really be testing for signed addends here, but we don't
00340      have that info directly in the howto.  */
00341   if (howto->pc_relative)
00342     {
00343       bfd_vma sign;
00344       sign = howto->dst_mask & (~howto->dst_mask >> 1 | ~(-(bfd_vma) 1 >> 1));
00345       sign = sign >> howto->bitpos << howto->rightshift;
00346       val = (val ^ sign) - sign;
00347     }
00348   return val;
00349 }
00350 
00351 static void
00352 insert_rel_addend (bfd *abfd,
00353                  bfd_byte *where,
00354                  reloc_howto_type *howto,
00355                  bfd_vma addend)
00356 {
00357   bfd_vma insn;
00358 
00359   addend = (addend >> howto->rightshift << howto->bitpos) & howto->dst_mask;
00360   insn = ~howto->dst_mask;
00361   switch (howto->size)
00362     {
00363     case 0:
00364       insn &= bfd_get_8 (abfd, where);
00365       insn |= addend;
00366       bfd_put_8 (abfd, insn, where);
00367       break;
00368     case 1:
00369       insn &= bfd_get_16 (abfd, where);
00370       insn |= addend;
00371       bfd_put_16 (abfd, insn, where);
00372       break;
00373     case 2:
00374       insn &= bfd_get_32 (abfd, where);
00375       insn |= addend;
00376       bfd_put_32 (abfd, insn, where);
00377       break;
00378     default:
00379       abort ();
00380     }
00381 }
00382 
00383 /* Relocate a D10V ELF section.  */
00384 
00385 static bfd_boolean
00386 elf32_d10v_relocate_section (bfd *output_bfd,
00387                           struct bfd_link_info *info,
00388                           bfd *input_bfd,
00389                           asection *input_section,
00390                           bfd_byte *contents,
00391                           Elf_Internal_Rela *relocs,
00392                           Elf_Internal_Sym *local_syms,
00393                           asection **local_sections)
00394 {
00395   Elf_Internal_Shdr *symtab_hdr;
00396   struct elf_link_hash_entry **sym_hashes;
00397   Elf_Internal_Rela *rel, *relend;
00398   const char *name;
00399 
00400   symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
00401   sym_hashes = elf_sym_hashes (input_bfd);
00402 
00403   rel = relocs;
00404   relend = relocs + input_section->reloc_count;
00405   for (; rel < relend; rel++)
00406     {
00407       int r_type;
00408       reloc_howto_type *howto;
00409       unsigned long r_symndx;
00410       Elf_Internal_Sym *sym;
00411       asection *sec;
00412       struct elf_link_hash_entry *h;
00413       bfd_vma relocation;
00414       bfd_reloc_status_type r;
00415 
00416       r_symndx = ELF32_R_SYM (rel->r_info);
00417       r_type = ELF32_R_TYPE (rel->r_info);
00418 
00419       if (r_type == R_D10V_GNU_VTENTRY
00420           || r_type == R_D10V_GNU_VTINHERIT)
00421         continue;
00422 
00423       howto = elf_d10v_howto_table + r_type;
00424       h = NULL;
00425       sym = NULL;
00426       sec = NULL;
00427       if (r_symndx < symtab_hdr->sh_info)
00428        {
00429          sym = local_syms + r_symndx;
00430          sec = local_sections[r_symndx];
00431          relocation = (sec->output_section->vma
00432                      + sec->output_offset
00433                      + sym->st_value);
00434          if (ELF_ST_TYPE (sym->st_info) == STT_SECTION
00435              && ((sec->flags & SEC_MERGE) != 0
00436                 || (info->relocatable
00437                     && sec->output_offset != 0)))
00438            {
00439              bfd_vma addend;
00440              bfd_byte *where = contents + rel->r_offset;
00441 
00442              addend = extract_rel_addend (input_bfd, where, howto);
00443 
00444              if (info->relocatable)
00445               addend += sec->output_offset;
00446              else
00447               {
00448                 asection *msec = sec;
00449                 addend = _bfd_elf_rel_local_sym (output_bfd, sym, &msec,
00450                                              addend);
00451                 addend -= relocation;
00452                 addend += msec->output_section->vma + msec->output_offset;
00453               }
00454              insert_rel_addend (input_bfd, where, howto, addend);
00455            }
00456        }
00457       else
00458        {
00459          bfd_boolean unresolved_reloc, warned;
00460 
00461          RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
00462                                r_symndx, symtab_hdr, sym_hashes,
00463                                h, sec, relocation,
00464                                unresolved_reloc, warned);
00465        }
00466 
00467       if (sec != NULL && elf_discarded_section (sec))
00468        {
00469          /* For relocs against symbols from removed linkonce sections,
00470             or sections discarded by a linker script, we just want the
00471             section contents zeroed.  Avoid any special processing.  */
00472          _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
00473          rel->r_info = 0;
00474          rel->r_addend = 0;
00475          continue;
00476        }
00477 
00478       if (info->relocatable)
00479        continue;
00480 
00481       if (h != NULL)
00482        name = h->root.root.string;
00483       else
00484        {
00485          name = (bfd_elf_string_from_elf_section
00486                 (input_bfd, symtab_hdr->sh_link, sym->st_name));
00487          if (name == NULL || *name == '\0')
00488            name = bfd_section_name (input_bfd, sec);
00489        }
00490 
00491       r = _bfd_final_link_relocate (howto, input_bfd, input_section,
00492                                     contents, rel->r_offset,
00493                                     relocation, (bfd_vma) 0);
00494 
00495       if (r != bfd_reloc_ok)
00496        {
00497          const char * msg = (const char *) 0;
00498 
00499          switch (r)
00500            {
00501            case bfd_reloc_overflow:
00502              if (!((*info->callbacks->reloc_overflow)
00503                   (info, (h ? &h->root : NULL), name, howto->name,
00504                    (bfd_vma) 0, input_bfd, input_section,
00505                    rel->r_offset)))
00506               return FALSE;
00507              break;
00508 
00509            case bfd_reloc_undefined:
00510              if (!((*info->callbacks->undefined_symbol)
00511                   (info, name, input_bfd, input_section,
00512                    rel->r_offset, TRUE)))
00513               return FALSE;
00514              break;
00515 
00516            case bfd_reloc_outofrange:
00517              msg = _("internal error: out of range error");
00518              goto common_error;
00519 
00520            case bfd_reloc_notsupported:
00521              msg = _("internal error: unsupported relocation error");
00522              goto common_error;
00523 
00524            case bfd_reloc_dangerous:
00525              msg = _("internal error: dangerous error");
00526              goto common_error;
00527 
00528            default:
00529              msg = _("internal error: unknown error");
00530              /* fall through */
00531 
00532            common_error:
00533              if (!((*info->callbacks->warning)
00534                   (info, msg, name, input_bfd, input_section,
00535                    rel->r_offset)))
00536               return FALSE;
00537              break;
00538            }
00539        }
00540     }
00541 
00542   return TRUE;
00543 }
00544 #define ELF_ARCH            bfd_arch_d10v
00545 #define ELF_MACHINE_CODE    EM_D10V
00546 #define ELF_MACHINE_ALT1    EM_CYGNUS_D10V
00547 #define ELF_MAXPAGESIZE            0x1000
00548 
00549 #define TARGET_BIG_SYM          bfd_elf32_d10v_vec
00550 #define TARGET_BIG_NAME            "elf32-d10v"
00551 
00552 #define elf_info_to_howto                0
00553 #define elf_info_to_howto_rel                   d10v_info_to_howto_rel
00554 #define elf_backend_object_p                    0
00555 #define elf_backend_final_write_processing   0
00556 #define elf_backend_gc_mark_hook             elf32_d10v_gc_mark_hook
00557 #define elf_backend_check_relocs             elf32_d10v_check_relocs
00558 #define elf_backend_relocate_section         elf32_d10v_relocate_section
00559 #define elf_backend_can_gc_sections          1
00560 
00561 #include "elf32-target.h"