* elf32-spu.c (spu_elf_relocate_section): Only encode overlay index
[deliverable/binutils-gdb.git] / bfd / elf32-frv.c
index 3a3124347b5d4cdaeaac7f58e179866652529d78..01e15f9604430f54be6e69c80ce34c5558f572e4 100644 (file)
@@ -1,24 +1,26 @@
 /* FRV-specific support for 32-bit ELF.
-   Copyright 2002, 2003, 2004, 2005, 2006  Free Software Foundation, Inc.
+   Copyright 2002, 2003, 2004, 2005, 2006, 2007, 2008
+   Free Software Foundation, Inc.
 
-This file is part of BFD, the Binary File Descriptor library.
+   This file is part of BFD, the Binary File Descriptor library.
 
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA.  */
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+   MA 02110-1301, USA.  */
 
-#include "bfd.h"
 #include "sysdep.h"
+#include "bfd.h"
 #include "libbfd.h"
 #include "elf-bfd.h"
 #include "elf/frv.h"
@@ -1339,26 +1341,9 @@ _frvfdpic_add_rofixup (bfd *output_bfd, asection *rofixup, bfd_vma offset,
 static unsigned
 _frvfdpic_osec_to_segment (bfd *output_bfd, asection *osec)
 {
-  struct elf_segment_map *m;
-  Elf_Internal_Phdr *p;
+  Elf_Internal_Phdr *p = _bfd_elf_find_segment_containing_section (output_bfd, osec);
 
-  /* Find the segment that contains the output_section.  */
-  for (m = elf_tdata (output_bfd)->segment_map,
-        p = elf_tdata (output_bfd)->phdr;
-       m != NULL;
-       m = m->next, p++)
-    {
-      int i;
-
-      for (i = m->count - 1; i >= 0; i--)
-       if (m->sections[i] == osec)
-         break;
-
-      if (i >= 0)
-       break;
-    }
-
-  return p - elf_tdata (output_bfd)->phdr;
+  return (p != NULL) ? p - elf_tdata (output_bfd)->phdr : -1;
 }
 
 inline static bfd_boolean
@@ -1412,7 +1397,8 @@ _frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
        dynindx = entry->d.h->dynindx;
       else
        {
-         if (sec->output_section
+         if (sec
+             && sec->output_section
              && ! bfd_is_abs_section (sec->output_section)
              && ! bfd_is_und_section (sec->output_section))
            dynindx = elf_section_data (sec->output_section)->dynindx;
@@ -1694,8 +1680,9 @@ _frvfdpic_emit_got_relocs_plt_entries (struct frvfdpic_relocs_info *entry,
             of the section.  For a non-local function, it's
             disregarded.  */
          lowword = ad;
-         if (entry->symndx == -1 && entry->d.h->dynindx != -1
-             && entry->d.h->dynindx == idx)
+         if (sec == NULL
+             || (entry->symndx == -1 && entry->d.h->dynindx != -1
+                 && entry->d.h->dynindx == idx))
            highword = 0;
          else
            highword = _frvfdpic_osec_to_segment
@@ -2589,6 +2576,26 @@ frv_reloc_type_lookup (abfd, code)
   return NULL;
 }
 
+static reloc_howto_type *
+frv_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, const char *r_name)
+{
+  unsigned int i;
+
+  for (i = 0;
+       i < sizeof (elf32_frv_howto_table) / sizeof (elf32_frv_howto_table[0]);
+       i++)
+    if (elf32_frv_howto_table[i].name != NULL
+       && strcasecmp (elf32_frv_howto_table[i].name, r_name) == 0)
+      return &elf32_frv_howto_table[i];
+
+  if (strcasecmp (elf32_frv_vtinherit_howto.name, r_name) == 0)
+    return &elf32_frv_vtinherit_howto;
+  if (strcasecmp (elf32_frv_vtentry_howto.name, r_name) == 0)
+    return &elf32_frv_vtentry_howto;
+
+  return NULL;
+}
+
 /* Set the howto pointer for an FRV ELF reloc.  */
 
 static void
@@ -2722,9 +2729,6 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
   int silence_segment_error = !(info->shared || info->pie);
   unsigned long insn;
 
-  if (info->relocatable)
-    return TRUE;
-
   symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (input_bfd);
   relend     = relocs + input_section->reloc_count;
@@ -2776,7 +2780,6 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
          || r_type == R_FRV_GNU_VTENTRY)
        continue;
 
-      /* This is a final link.  */
       r_symndx = ELF32_R_SYM (rel->r_info);
       howto  = elf32_frv_howto_table + ELF32_R_TYPE (rel->r_info);
       h      = NULL;
@@ -2795,50 +2798,38 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
        }
       else
        {
-         h = sym_hashes [r_symndx - symtab_hdr->sh_info];
+         bfd_boolean warned;
+         bfd_boolean unresolved_reloc;
 
-         while (h->root.type == bfd_link_hash_indirect
-                || h->root.type == bfd_link_hash_warning)
-           h = (struct elf_link_hash_entry *) h->root.u.i.link;
+         RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
+                                  r_symndx, symtab_hdr, sym_hashes,
+                                  h, sec, relocation,
+                                  unresolved_reloc, warned);
+         osec = sec;
+       }
 
-         name = h->root.root.string;
+      if (sec != NULL && elf_discarded_section (sec))
+       {
+         /* For relocs against symbols from removed linkonce sections,
+            or sections discarded by a linker script, we just want the
+            section contents zeroed.  Avoid any special processing.  */
+         _bfd_clear_contents (howto, input_bfd, contents + rel->r_offset);
+         rel->r_info = 0;
+         rel->r_addend = 0;
+         continue;
+       }
 
-         if ((h->root.type == bfd_link_hash_defined
-              || h->root.type == bfd_link_hash_defweak))
-           {
-             if (/* TLSMOFF forces local binding.  */
-                 r_type != R_FRV_TLSMOFF
-                 && ! FRVFDPIC_SYM_LOCAL (info, h))
-               {
-                 sec = NULL;
-                 relocation = 0;
-               }
-             else
-               {
-                 sec = h->root.u.def.section;
-                 relocation = (h->root.u.def.value
-                               + sec->output_section->vma
-                               + sec->output_offset);
-               }
-           }
-         else if (h->root.type == bfd_link_hash_undefweak)
-           {
-             relocation = 0;
-           }
-         else if (info->unresolved_syms_in_objects == RM_IGNORE
-                  && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
-           relocation = 0;
-         else
-           {
-             if (! ((*info->callbacks->undefined_symbol)
-                    (info, h->root.root.string, input_bfd,
-                     input_section, rel->r_offset,
-                     (info->unresolved_syms_in_objects == RM_GENERATE_ERROR
-                      || ELF_ST_VISIBILITY (h->other)))))
-               return FALSE;
-             relocation = 0;
-           }
-         osec = sec;
+      if (info->relocatable)
+       continue;
+
+      if (r_type != R_FRV_TLSMOFF
+         && h != NULL
+         && (h->root.type == bfd_link_hash_defined
+             || h->root.type == bfd_link_hash_defweak)
+         && !FRVFDPIC_SYM_LOCAL (info, h))
+       {
+         osec = sec = NULL;
+         relocation = 0;
        }
 
       switch (r_type)
@@ -3639,6 +3630,8 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
                                                input_section->output_section)
                         & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
                      {
+                       bfd_vma offset;
+
                        if (_frvfdpic_osec_readonly_p (output_bfd,
                                                       input_section
                                                       ->output_section))
@@ -3649,22 +3642,27 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
                               name, input_bfd, input_section, rel->r_offset);
                            return FALSE;
                          }
-                       _frvfdpic_add_rofixup (output_bfd,
-                                              frvfdpic_gotfixup_section
-                                              (info),
-                                              _bfd_elf_section_offset
-                                              (output_bfd, info,
-                                               input_section, rel->r_offset)
-                                              + input_section
-                                              ->output_section->vma
-                                              + input_section->output_offset,
-                                              picrel);
+
+                       offset = _bfd_elf_section_offset
+                         (output_bfd, info,
+                          input_section, rel->r_offset);
+
+                       if (offset != (bfd_vma)-1)
+                         _frvfdpic_add_rofixup (output_bfd,
+                                                frvfdpic_gotfixup_section
+                                                (info),
+                                                offset + input_section
+                                                ->output_section->vma
+                                                + input_section->output_offset,
+                                                picrel);
                      }
                  }
                else if ((bfd_get_section_flags (output_bfd,
                                                 input_section->output_section)
                          & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
                  {
+                   bfd_vma offset;
+
                    if (_frvfdpic_osec_readonly_p (output_bfd,
                                                   input_section
                                                   ->output_section))
@@ -3675,15 +3673,18 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
                           name, input_bfd, input_section, rel->r_offset);
                        return FALSE;
                      }
-                   _frvfdpic_add_dyn_reloc (output_bfd,
-                                            frvfdpic_gotrel_section (info),
-                                            _bfd_elf_section_offset
-                                            (output_bfd, info,
-                                             input_section, rel->r_offset)
-                                            + input_section
-                                            ->output_section->vma
-                                            + input_section->output_offset,
-                                            r_type, dynindx, addend, picrel);
+
+                   offset = _bfd_elf_section_offset
+                     (output_bfd, info,
+                      input_section, rel->r_offset);
+
+                   if (offset != (bfd_vma)-1)
+                     _frvfdpic_add_dyn_reloc (output_bfd,
+                                              frvfdpic_gotrel_section (info),
+                                              offset + input_section
+                                              ->output_section->vma
+                                              + input_section->output_offset,
+                                              r_type, dynindx, addend, picrel);
                  }
                else
                  addend += frvfdpic_got_section (info)->output_section->vma;
@@ -3764,25 +3765,27 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
                      }
                    if (!h || h->root.type != bfd_link_hash_undefweak)
                      {
-                       _frvfdpic_add_rofixup (output_bfd,
-                                              frvfdpic_gotfixup_section
-                                              (info),
-                                              _bfd_elf_section_offset
-                                              (output_bfd, info,
-                                               input_section, rel->r_offset)
-                                              + input_section
-                                              ->output_section->vma
-                                              + input_section->output_offset,
-                                              picrel);
-                       if (r_type == R_FRV_FUNCDESC_VALUE)
-                         _frvfdpic_add_rofixup
-                           (output_bfd,
-                            frvfdpic_gotfixup_section (info),
-                            _bfd_elf_section_offset
-                            (output_bfd, info,
-                             input_section, rel->r_offset)
-                            + input_section->output_section->vma
-                            + input_section->output_offset + 4, picrel);
+                       bfd_vma offset = _bfd_elf_section_offset
+                         (output_bfd, info,
+                          input_section, rel->r_offset);
+
+                       if (offset != (bfd_vma)-1)
+                         {
+                           _frvfdpic_add_rofixup (output_bfd,
+                                                  frvfdpic_gotfixup_section
+                                                  (info),
+                                                  offset + input_section
+                                                  ->output_section->vma
+                                                  + input_section->output_offset,
+                                                  picrel);
+                           if (r_type == R_FRV_FUNCDESC_VALUE)
+                             _frvfdpic_add_rofixup
+                               (output_bfd,
+                                frvfdpic_gotfixup_section (info),
+                                offset
+                                + input_section->output_section->vma
+                                + input_section->output_offset + 4, picrel);
+                         }
                      }
                  }
              }
@@ -3792,6 +3795,8 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
                                            input_section->output_section)
                     & (SEC_ALLOC | SEC_LOAD)) == (SEC_ALLOC | SEC_LOAD))
                  {
+                   bfd_vma offset;
+
                    if (_frvfdpic_osec_readonly_p (output_bfd,
                                                   input_section
                                                   ->output_section))
@@ -3802,15 +3807,18 @@ elf32_frv_relocate_section (output_bfd, info, input_bfd, input_section,
                           name, input_bfd, input_section, rel->r_offset);
                        return FALSE;
                      }
-                   _frvfdpic_add_dyn_reloc (output_bfd,
-                                            frvfdpic_gotrel_section (info),
-                                            _bfd_elf_section_offset
-                                            (output_bfd, info,
-                                             input_section, rel->r_offset)
-                                            + input_section
-                                            ->output_section->vma
-                                            + input_section->output_offset,
-                                            r_type, dynindx, addend, picrel);
+
+                   offset = _bfd_elf_section_offset
+                     (output_bfd, info,
+                      input_section, rel->r_offset);
+
+                   if (offset != (bfd_vma)-1)
+                     _frvfdpic_add_dyn_reloc (output_bfd,
+                                              frvfdpic_gotrel_section (info),
+                                              offset + input_section
+                                              ->output_section->vma
+                                              + input_section->output_offset,
+                                              r_type, dynindx, addend, picrel);
                  }
                else if (osec)
                  addend += osec->output_section->vma;
@@ -5596,6 +5604,119 @@ elf32_frvfdpic_always_size_sections (bfd *output_bfd,
   return TRUE;
 }
 
+/* Check whether any of the relocations was optimized away, and
+   subtract it from the relocation or fixup count.  */
+static bfd_boolean
+_frvfdpic_check_discarded_relocs (bfd *abfd, asection *sec,
+                                 struct bfd_link_info *info,
+                                 
+                                 bfd_boolean *changed)
+{
+  Elf_Internal_Shdr *symtab_hdr;
+  struct elf_link_hash_entry **sym_hashes;
+  Elf_Internal_Rela *rel, *erel;
+
+  if ((sec->flags & SEC_RELOC) == 0
+      || sec->reloc_count == 0)
+    return TRUE;
+
+  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+  sym_hashes = elf_sym_hashes (abfd);
+
+  rel = elf_section_data (sec)->relocs;
+
+  /* Now examine each relocation.  */
+  for (erel = rel + sec->reloc_count; rel < erel; rel++)
+    {
+      struct elf_link_hash_entry *h;
+      unsigned long r_symndx;
+      struct frvfdpic_relocs_info *picrel;
+      struct _frvfdpic_dynamic_got_info *dinfo;
+
+      if (ELF32_R_TYPE (rel->r_info) != R_FRV_32
+         && ELF32_R_TYPE (rel->r_info) != R_FRV_FUNCDESC)
+       continue;
+
+      if (_bfd_elf_section_offset (sec->output_section->owner,
+                                  info, sec, rel->r_offset)
+         != (bfd_vma)-1)
+       continue;
+
+      r_symndx = ELF32_R_SYM (rel->r_info);
+      if (r_symndx < symtab_hdr->sh_info)
+       h = NULL;
+      else
+       {
+         h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+         while (h->root.type == bfd_link_hash_indirect
+                || h->root.type == bfd_link_hash_warning)
+           h = (struct elf_link_hash_entry *)h->root.u.i.link;
+       }
+
+      if (h != NULL)
+       picrel = frvfdpic_relocs_info_for_global (frvfdpic_relocs_info (info),
+                                                 abfd, h,
+                                                 rel->r_addend, NO_INSERT);
+      else
+       picrel = frvfdpic_relocs_info_for_local (frvfdpic_relocs_info (info),
+                                                abfd, r_symndx,
+                                                rel->r_addend, NO_INSERT);
+
+      if (! picrel)
+       return FALSE;
+
+      *changed = TRUE;
+      dinfo = frvfdpic_dynamic_got_plt_info (info);
+
+      _frvfdpic_count_relocs_fixups (picrel, dinfo, TRUE);
+      if (ELF32_R_TYPE (rel->r_info) == R_FRV_32)
+       picrel->relocs32--;
+      else /* we know (ELF32_R_TYPE (rel->r_info) == R_FRV_FUNCDESC) */
+       picrel->relocsfd--;
+      _frvfdpic_count_relocs_fixups (picrel, dinfo, FALSE);
+    }
+
+  return TRUE;
+}
+
+static bfd_boolean
+frvfdpic_elf_discard_info (bfd *ibfd,
+                          struct elf_reloc_cookie *cookie ATTRIBUTE_UNUSED,
+                          struct bfd_link_info *info)
+{
+  bfd_boolean changed = FALSE;
+  asection *s;
+  bfd *obfd = NULL;
+
+  /* Account for relaxation of .eh_frame section.  */
+  for (s = ibfd->sections; s; s = s->next)
+    if (s->sec_info_type == ELF_INFO_TYPE_EH_FRAME)
+      {
+       if (!_frvfdpic_check_discarded_relocs (ibfd, s, info, &changed))
+         return FALSE;
+       obfd = s->output_section->owner;
+      }
+
+  if (changed)
+    {
+      struct _frvfdpic_dynamic_got_plt_info gpinfo;
+
+      memset (&gpinfo, 0, sizeof (gpinfo));
+      memcpy (&gpinfo.g, frvfdpic_dynamic_got_plt_info (info),
+             sizeof (gpinfo.g));
+
+      /* Clear GOT and PLT assignments.  */
+      htab_traverse (frvfdpic_relocs_info (info),
+                    _frvfdpic_reset_got_plt_entries,
+                    NULL);
+
+      if (!_frvfdpic_size_got_plt (obfd, &gpinfo))
+       return FALSE;
+    }
+
+  return TRUE;
+}
+
 /* Look for opportunities to relax TLS relocations.  We can assume
    we're linking the main executable or a static-tls library, since
    otherwise we wouldn't have got here.  */
@@ -6040,7 +6161,7 @@ elf32_frv_check_relocs (abfd, info, sec, relocs)
      const Elf_Internal_Rela *relocs;
 {
   Elf_Internal_Shdr *symtab_hdr;
-  struct elf_link_hash_entry **sym_hashes, **sym_hashes_end;
+  struct elf_link_hash_entry **sym_hashes;
   const Elf_Internal_Rela *rel;
   const Elf_Internal_Rela *rel_end;
   bfd *dynobj;
@@ -6051,9 +6172,6 @@ elf32_frv_check_relocs (abfd, info, sec, relocs)
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
-  sym_hashes_end = sym_hashes + symtab_hdr->sh_size/sizeof(Elf32_External_Sym);
-  if (!elf_bad_symtab (abfd))
-    sym_hashes_end -= symtab_hdr->sh_info;
 
   dynobj = elf_hash_table (info)->dynobj;
   rel_end = relocs + sec->reloc_count;
@@ -6261,7 +6379,9 @@ elf32_frv_check_relocs (abfd, info, sec, relocs)
         /* This relocation describes which C++ vtable entries are actually
            used.  Record for later use during GC.  */
         case R_FRV_GNU_VTENTRY:
-          if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
+          BFD_ASSERT (h != NULL);
+          if (h != NULL
+              && !bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
             return FALSE;
           break;
 
@@ -6352,6 +6472,10 @@ frv_elf_copy_private_bfd_data (ibfd, obfd)
 
   elf_elfheader (obfd)->e_flags = elf_elfheader (ibfd)->e_flags;
   elf_flags_init (obfd) = TRUE;
+
+  /* Copy object attributes.  */
+  _bfd_elf_copy_obj_attributes (ibfd, obfd);
+
   return TRUE;
 }
 
@@ -6714,7 +6838,7 @@ frv_elf_print_private_bfd_data (abfd, ptr)
   _bfd_elf_print_private_bfd_data (abfd, ptr);
 
   flags = elf_elfheader (abfd)->e_flags;
-  fprintf (file, _("private flags = 0x%lx:"), (long)flags);
+  fprintf (file, _("private flags = 0x%lx:"), (unsigned long) flags);
 
   switch (flags & EF_FRV_CPU_MASK)
     {
@@ -6881,6 +7005,7 @@ elf32_frv_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
 #define elf_backend_rela_normal                        1
 
 #define bfd_elf32_bfd_reloc_type_lookup                frv_reloc_type_lookup
+#define bfd_elf32_bfd_reloc_name_lookup        frv_reloc_name_lookup
 #define bfd_elf32_bfd_set_private_flags                frv_elf_set_private_flags
 #define bfd_elf32_bfd_copy_private_bfd_data    frv_elf_copy_private_bfd_data
 #define bfd_elf32_bfd_merge_private_bfd_data   frv_elf_merge_private_bfd_data
@@ -6946,6 +7071,9 @@ elf32_frv_grok_psinfo (bfd *abfd, Elf_Internal_Note *note)
 #define elf_backend_finish_dynamic_sections \
                elf32_frvfdpic_finish_dynamic_sections
 
+#undef elf_backend_discard_info
+#define elf_backend_discard_info \
+               frvfdpic_elf_discard_info
 #undef elf_backend_can_make_relative_eh_frame
 #define elf_backend_can_make_relative_eh_frame \
                frvfdpic_elf_use_relative_eh_frame
This page took 0.044525 seconds and 4 git commands to generate.