daily update
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
index 0ed8e9e3f87e3a6067b7f8f209daf8bfcee3d58c..02d2913d3b6dc43b24527bd8e6d1e6b650044145 100644 (file)
@@ -92,9 +92,6 @@ static boolean ppc64_elf_finish_dynamic_sections
   PARAMS ((bfd *, struct bfd_link_info *));
 
 
-/* Branch prediction bit for branch taken relocs.  */
-#define BRANCH_PREDICT_BIT 0x200000
-
 /* Mask to set RA in memory instructions.  */
 #define RA_REGISTER_MASK 0x001f0000
 
@@ -1642,6 +1639,9 @@ struct ppc_link_hash_table
   asection *sdynbss;
   asection *srelbss;
   asection *sglink;
+
+  /* Small local sym to section mapping cache.  */
+  struct sym_sec_cache sym_sec;
 };
 
 /* Get the ppc64 ELF linker hash table from a link_info structure.  */
@@ -1704,6 +1704,7 @@ ppc64_elf_link_hash_table_create (abfd)
   htab->sdynbss = NULL;
   htab->srelbss = NULL;
   htab->sglink = NULL;
+  htab->sym_sec.abfd = NULL;
 
   return &htab->elf.root;
 }
@@ -1794,13 +1795,39 @@ ppc64_elf_copy_indirect_symbol (dir, ind)
   edir = (struct ppc_link_hash_entry *) dir;
   eind = (struct ppc_link_hash_entry *) ind;
 
-  if (edir->dyn_relocs == NULL)
+  if (eind->dyn_relocs != NULL)
     {
+      if (edir->dyn_relocs != NULL)
+       {
+         struct ppc_dyn_relocs **pp;
+         struct ppc_dyn_relocs *p;
+
+         if (ind->root.type == bfd_link_hash_indirect)
+           abort ();
+
+         /* Add reloc counts against the weak sym to the strong sym
+            list.  Merge any entries against the same section.  */
+         for (pp = &eind->dyn_relocs; (p = *pp) != NULL; )
+           {
+             struct ppc_dyn_relocs *q;
+
+             for (q = edir->dyn_relocs; q != NULL; q = q->next)
+               if (q->sec == p->sec)
+                 {
+                   q->pc_count += p->pc_count;
+                   q->count += p->count;
+                   *pp = p->next;
+                   break;
+                 }
+             if (q == NULL)
+               pp = &p->next;
+           }
+         *pp = edir->dyn_relocs;
+       }
+
       edir->dyn_relocs = eind->dyn_relocs;
       eind->dyn_relocs = NULL;
     }
-  else if (eind->dyn_relocs != NULL)
-    abort ();
 
   _bfd_elf_link_hash_copy_indirect (dir, ind);
 }
@@ -1842,6 +1869,7 @@ ppc64_elf_check_relocs (abfd, info, sec, relocs)
     {
       unsigned long r_symndx;
       struct elf_link_hash_entry *h;
+      enum elf_ppc_reloc_type r_type;
 
       r_symndx = ELF64_R_SYM (rel->r_info);
       if (r_symndx < symtab_hdr->sh_info)
@@ -1849,7 +1877,8 @@ ppc64_elf_check_relocs (abfd, info, sec, relocs)
       else
        h = sym_hashes[r_symndx - symtab_hdr->sh_info];
 
-      switch (ELF64_R_TYPE (rel->r_info))
+      r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
+      switch (r_type)
        {
          /* GOT16 relocations */
        case R_PPC64_GOT16:
@@ -1997,7 +2026,7 @@ ppc64_elf_check_relocs (abfd, info, sec, relocs)
             symbol.  */
          if ((info->shared
               && (sec->flags & SEC_ALLOC) != 0
-              && (IS_ABSOLUTE_RELOC (ELF64_R_TYPE (rel->r_info))
+              && (IS_ABSOLUTE_RELOC (r_type)
                   || (h != NULL
                       && (! info->symbolic
                           || h->root.type == bfd_link_hash_defweak
@@ -2010,6 +2039,9 @@ ppc64_elf_check_relocs (abfd, info, sec, relocs)
                      || (h->elf_link_hash_flags
                          & ELF_LINK_HASH_DEF_REGULAR) == 0)))
            {
+             struct ppc_dyn_relocs *p;
+             struct ppc_dyn_relocs **head;
+
              /* We must copy these reloc types into the output file.
                 Create a reloc section in dynobj and make room for
                 this reloc.  */
@@ -2060,35 +2092,42 @@ ppc64_elf_check_relocs (abfd, info, sec, relocs)
                 relocations we need for this symbol.  */
              if (h != NULL)
                {
-                 struct ppc_link_hash_entry *eh;
-                 struct ppc_dyn_relocs *p;
-
-                 eh = (struct ppc_link_hash_entry *) h;
-                 p = eh->dyn_relocs;
-
-                 if (p == NULL || p->sec != sec)
-                   {
-                     p = ((struct ppc_dyn_relocs *)
-                          bfd_alloc (htab->elf.dynobj,
-                                     (bfd_size_type) sizeof *p));
-                     if (p == NULL)
-                       return false;
-                     p->next = eh->dyn_relocs;
-                     eh->dyn_relocs = p;
-                     p->sec = sec;
-                     p->count = 0;
-                     p->pc_count = 0;
-                   }
-
-                 p->count += 1;
-                 if (!IS_ABSOLUTE_RELOC (ELF64_R_TYPE (rel->r_info)))
-                   p->pc_count += 1;
+                 head = &((struct ppc_link_hash_entry *) h)->dyn_relocs;
                }
              else
                {
-                 /* Track dynamic relocs needed for local syms too.  */
-                 elf_section_data (sec)->local_dynrel += 1;
+                 /* Track dynamic relocs needed for local syms too.
+                    We really need local syms available to do this
+                    easily.  Oh well.  */
+
+                 asection *s;
+                 s = bfd_section_from_r_symndx (abfd, &htab->sym_sec,
+                                                sec, r_symndx);
+                 if (s == NULL)
+                   return false;
+
+                 head = ((struct ppc_dyn_relocs **)
+                         &elf_section_data (s)->local_dynrel);
                }
+
+             p = *head;
+             if (p == NULL || p->sec != sec)
+               {
+                 p = ((struct ppc_dyn_relocs *)
+                      bfd_alloc (htab->elf.dynobj,
+                                 (bfd_size_type) sizeof *p));
+                 if (p == NULL)
+                   return false;
+                 p->next = *head;
+                 *head = p;
+                 p->sec = sec;
+                 p->count = 0;
+                 p->pc_count = 0;
+               }
+
+             p->count += 1;
+             if (!IS_ABSOLUTE_RELOC (r_type))
+               p->pc_count += 1;
            }
          break;
 
@@ -2112,7 +2151,10 @@ ppc64_elf_gc_mark_hook (abfd, info, rel, h, sym)
 {
   if (h != NULL)
     {
-      switch (ELF64_R_TYPE (rel->r_info))
+      enum elf_ppc_reloc_type r_type;
+
+      r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
+      switch (r_type)
        {
        case R_PPC64_GNU_VTINHERIT:
        case R_PPC64_GNU_VTENTRY:
@@ -2161,8 +2203,8 @@ ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
   struct elf_link_hash_entry **sym_hashes;
   bfd_signed_vma *local_got_refcounts;
   const Elf_Internal_Rela *rel, *relend;
-  unsigned long r_symndx;
-  struct elf_link_hash_entry *h;
+
+  elf_section_data (sec)->local_dynrel = NULL;
 
   symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
   sym_hashes = elf_sym_hashes (abfd);
@@ -2170,116 +2212,119 @@ ppc64_elf_gc_sweep_hook (abfd, info, sec, relocs)
 
   relend = relocs + sec->reloc_count;
   for (rel = relocs; rel < relend; rel++)
-    switch (ELF64_R_TYPE (rel->r_info))
-      {
-      case R_PPC64_GOT16:
-      case R_PPC64_GOT16_DS:
-      case R_PPC64_GOT16_HA:
-      case R_PPC64_GOT16_HI:
-      case R_PPC64_GOT16_LO:
-      case R_PPC64_GOT16_LO_DS:
-       r_symndx = ELF64_R_SYM (rel->r_info);
-       if (r_symndx >= symtab_hdr->sh_info)
-         {
-           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-           if (h->got.refcount > 0)
-             h->got.refcount--;
-         }
-       else
-         {
-           if (local_got_refcounts[r_symndx] > 0)
-             local_got_refcounts[r_symndx]--;
-         }
-        break;
-
-      case R_PPC64_PLT16_HA:
-      case R_PPC64_PLT16_HI:
-      case R_PPC64_PLT16_LO:
-      case R_PPC64_PLT32:
-      case R_PPC64_PLT64:
-       r_symndx = ELF64_R_SYM (rel->r_info);
-       if (r_symndx >= symtab_hdr->sh_info)
-         {
-           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-           if (h->plt.refcount > 0)
-             h->plt.refcount--;
-         }
-       break;
+    {
+      unsigned long r_symndx;
+      enum elf_ppc_reloc_type r_type;
+      struct elf_link_hash_entry *h;
 
-      case R_PPC64_REL14:
-      case R_PPC64_REL14_BRNTAKEN:
-      case R_PPC64_REL14_BRTAKEN:
-      case R_PPC64_REL24:
-      case R_PPC64_REL32:
-      case R_PPC64_REL64:
-       r_symndx = ELF64_R_SYM (rel->r_info);
-       if (r_symndx >= symtab_hdr->sh_info)
-         {
-           struct ppc_link_hash_entry *eh;
-           struct ppc_dyn_relocs **pp;
-           struct ppc_dyn_relocs *p;
+      r_symndx = ELF64_R_SYM (rel->r_info);
+      r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rel->r_info);
+      switch (r_type)
+       {
+       case R_PPC64_GOT16:
+       case R_PPC64_GOT16_DS:
+       case R_PPC64_GOT16_HA:
+       case R_PPC64_GOT16_HI:
+       case R_PPC64_GOT16_LO:
+       case R_PPC64_GOT16_LO_DS:
+         if (r_symndx >= symtab_hdr->sh_info)
+           {
+             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+             if (h->got.refcount > 0)
+               h->got.refcount--;
+           }
+         else
+           {
+             if (local_got_refcounts[r_symndx] > 0)
+               local_got_refcounts[r_symndx]--;
+           }
+         break;
 
-           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-           eh = (struct ppc_link_hash_entry *) h;
+       case R_PPC64_PLT16_HA:
+       case R_PPC64_PLT16_HI:
+       case R_PPC64_PLT16_LO:
+       case R_PPC64_PLT32:
+       case R_PPC64_PLT64:
+         if (r_symndx >= symtab_hdr->sh_info)
+           {
+             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+             if (h->plt.refcount > 0)
+               h->plt.refcount--;
+           }
+         break;
 
-           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
-             if (p->sec == sec)
-               {
-                 p->pc_count -= 1;
-                 p->count -= 1;
-                 if (p->count == 0)
-                   *pp = p->next;
-                 break;
-               }
-         }
-       break;
+       case R_PPC64_REL14:
+       case R_PPC64_REL14_BRNTAKEN:
+       case R_PPC64_REL14_BRTAKEN:
+       case R_PPC64_REL24:
+       case R_PPC64_REL32:
+       case R_PPC64_REL64:
+         if (r_symndx >= symtab_hdr->sh_info)
+           {
+             struct ppc_link_hash_entry *eh;
+             struct ppc_dyn_relocs **pp;
+             struct ppc_dyn_relocs *p;
 
-      case R_PPC64_ADDR14:
-      case R_PPC64_ADDR14_BRNTAKEN:
-      case R_PPC64_ADDR14_BRTAKEN:
-      case R_PPC64_ADDR16:
-      case R_PPC64_ADDR16_DS:
-      case R_PPC64_ADDR16_HA:
-      case R_PPC64_ADDR16_HI:
-      case R_PPC64_ADDR16_HIGHER:
-      case R_PPC64_ADDR16_HIGHERA:
-      case R_PPC64_ADDR16_HIGHEST:
-      case R_PPC64_ADDR16_HIGHESTA:
-      case R_PPC64_ADDR16_LO:
-      case R_PPC64_ADDR16_LO_DS:
-      case R_PPC64_ADDR24:
-      case R_PPC64_ADDR30:
-      case R_PPC64_ADDR32:
-      case R_PPC64_ADDR64:
-      case R_PPC64_UADDR16:
-      case R_PPC64_UADDR32:
-      case R_PPC64_UADDR64:
-      case R_PPC64_TOC:
-       r_symndx = ELF64_R_SYM (rel->r_info);
-       if (r_symndx >= symtab_hdr->sh_info)
-         {
-           struct ppc_link_hash_entry *eh;
-           struct ppc_dyn_relocs **pp;
-           struct ppc_dyn_relocs *p;
+             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+             eh = (struct ppc_link_hash_entry *) h;
 
-           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
-           eh = (struct ppc_link_hash_entry *) h;
+             for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
+               if (p->sec == sec)
+                 {
+                   p->pc_count -= 1;
+                   p->count -= 1;
+                   if (p->count == 0)
+                     *pp = p->next;
+                   break;
+                 }
+           }
+         break;
 
-           for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
-             if (p->sec == sec)
-               {
-                 p->count -= 1;
-                 if (p->count == 0)
-                   *pp = p->next;
-                 break;
-               }
-         }
-       break;
+       case R_PPC64_ADDR14:
+       case R_PPC64_ADDR14_BRNTAKEN:
+       case R_PPC64_ADDR14_BRTAKEN:
+       case R_PPC64_ADDR16:
+       case R_PPC64_ADDR16_DS:
+       case R_PPC64_ADDR16_HA:
+       case R_PPC64_ADDR16_HI:
+       case R_PPC64_ADDR16_HIGHER:
+       case R_PPC64_ADDR16_HIGHERA:
+       case R_PPC64_ADDR16_HIGHEST:
+       case R_PPC64_ADDR16_HIGHESTA:
+       case R_PPC64_ADDR16_LO:
+       case R_PPC64_ADDR16_LO_DS:
+       case R_PPC64_ADDR24:
+       case R_PPC64_ADDR30:
+       case R_PPC64_ADDR32:
+       case R_PPC64_ADDR64:
+       case R_PPC64_UADDR16:
+       case R_PPC64_UADDR32:
+       case R_PPC64_UADDR64:
+       case R_PPC64_TOC:
+         if (r_symndx >= symtab_hdr->sh_info)
+           {
+             struct ppc_link_hash_entry *eh;
+             struct ppc_dyn_relocs **pp;
+             struct ppc_dyn_relocs *p;
 
-      default:
-       break;
-      }
+             h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+             eh = (struct ppc_link_hash_entry *) h;
 
+             for (pp = &eh->dyn_relocs; (p = *pp) != NULL; pp = &p->next)
+               if (p->sec == sec)
+                 {
+                   p->count -= 1;
+                   if (p->count == 0)
+                     *pp = p->next;
+                   break;
+                 }
+           }
+         break;
+
+       default:
+         break;
+       }
+    }
   return true;
 }
 
@@ -2360,6 +2405,8 @@ ppc64_elf_adjust_dynamic_symbol (info, h)
        }
       return true;
     }
+  else
+    h->plt.offset = (bfd_vma) -1;
 
   /* If this is a weak symbol, and there is a real definition, the
      processor independent code will have arranged for us to see the
@@ -2370,6 +2417,7 @@ ppc64_elf_adjust_dynamic_symbol (info, h)
                  || h->weakdef->root.type == bfd_link_hash_defweak);
       h->root.u.def.section = h->weakdef->root.u.def.section;
       h->root.u.def.value = h->weakdef->root.u.def.value;
+      return true;
     }
 
   /* This is a reference to a symbol defined by a dynamic object which
@@ -2628,7 +2676,7 @@ allocate_dynrelocs (h, inf)
          if (h->dynindx == -1
              && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
            {
-             if (! bfd_elf32_link_record_dynamic_symbol (info, h))
+             if (! bfd_elf64_link_record_dynamic_symbol (info, h))
                return false;
            }
 
@@ -2640,7 +2688,7 @@ allocate_dynrelocs (h, inf)
 
       eh->dyn_relocs = NULL;
 
-    keep:
+    keep: ;
     }
 
   /* Finally, allocate space.  */
@@ -2727,12 +2775,26 @@ ppc64_elf_size_dynamic_sections (output_bfd, info)
 
       for (s = ibfd->sections; s != NULL; s = s->next)
        {
-         bfd_size_type count = elf_section_data (s)->local_dynrel;
+         struct ppc_dyn_relocs *p;
 
-         if (count != 0)
+         for (p = *((struct ppc_dyn_relocs **)
+                    &elf_section_data (s)->local_dynrel);
+              p != NULL;
+              p = p->next)
            {
-             srel = elf_section_data (s)->sreloc;
-             srel->_raw_size += count * sizeof (Elf64_External_Rela);
+             if (!bfd_is_abs_section (p->sec)
+                 && bfd_is_abs_section (p->sec->output_section))
+               {
+                 /* Input section has been discarded, either because
+                    it is a copy of a linkonce section or due to
+                    linker script /DISCARD/, so we'll be discarding
+                    the relocs too.  */
+               }
+             else
+               {
+                 srel = elf_section_data (p->sec)->sreloc;
+                 srel->_raw_size += p->count * sizeof (Elf64_External_Rela);
+               }
            }
        }
 
@@ -3078,9 +3140,8 @@ ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          sec = local_sections[r_symndx];
          sym_name = "<local symbol>";
 
-         relocation = (sec->output_section->vma
-                       + sec->output_offset
-                       + sym->st_value);
+         relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
+         addend = rel->r_addend;
        }
       else
        {
@@ -3126,6 +3187,7 @@ ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
        }
 
       /* First handle relocations that tweak non-addend part of insn.  */
+      insn = 0;
       switch (r_type)
        {
        default:
@@ -3134,22 +3196,23 @@ ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          /* Branch taken prediction relocations.  */
        case R_PPC64_ADDR14_BRTAKEN:
        case R_PPC64_REL14_BRTAKEN:
-         insn = bfd_get_32 (output_bfd, contents + offset);
-         if ((relocation - offset) & 0x8000)
-           insn &= ~BRANCH_PREDICT_BIT;
-         else
-           insn |= BRANCH_PREDICT_BIT;
-         bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
-         break;
+         insn = 0x01 << 21; /* Set 't' bit, lowest bit of BO field. */
+         /* Fall thru. */
 
-         /* Branch not taken predicition relocations.  */
+         /* Branch not taken prediction relocations.  */
        case R_PPC64_ADDR14_BRNTAKEN:
        case R_PPC64_REL14_BRNTAKEN:
-         insn = bfd_get_32 (output_bfd, contents + offset);
-         if ((relocation - offset) & 0x8000)
-           insn |= BRANCH_PREDICT_BIT;
+         insn |= bfd_get_32 (output_bfd, contents + offset) & ~(0x01 << 21);
+         /* Set 'a' bit.  This is 0b00010 in BO field for branch on CR(BI)
+            insns (BO == 001at or 011at), and 0b01000 for branch on CTR
+            insns (BO == 1a00t or 1a01t).  */
+         if ((insn & (0x14 << 21)) == (0x04 << 21))
+           insn |= 0x02 << 21;
+         else if ((insn & (0x14 << 21)) == (0x10 << 21))
+           insn |= 0x08 << 21;
          else
-           insn &= ~BRANCH_PREDICT_BIT;
+           break;
+
          bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
          break;
 
@@ -3395,8 +3458,14 @@ ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
        case R_PPC64_UADDR32:
        case R_PPC64_UADDR64:
        case R_PPC64_TOC:
+         /* r_symndx will be zero only for relocs against symbols
+            from removed linkonce sections, or sections discarded by
+            a linker script.  */
+         if (r_symndx == 0
+             || (input_section->flags & SEC_ALLOC) == 0)
+           break;
+
          if ((info->shared
-              && (input_section->flags & SEC_ALLOC) != 0
               && (IS_ABSOLUTE_RELOC (r_type)
                   || (h != NULL
                       && h->dynindx != -1
@@ -3404,7 +3473,6 @@ ppc64_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                           || (h->elf_link_hash_flags
                               & ELF_LINK_HASH_DEF_REGULAR) == 0))))
              || (!info->shared
-                 && (input_section->flags & SEC_ALLOC) != 0
                  && h != NULL
                  && h->dynindx != -1
                  && (h->elf_link_hash_flags & ELF_LINK_NON_GOT_REF) == 0
@@ -3833,7 +3901,10 @@ static enum elf_reloc_type_class
 ppc64_elf_reloc_type_class (rela)
      const Elf_Internal_Rela *rela;
 {
-  switch ((int) ELF64_R_TYPE (rela->r_info))
+  enum elf_ppc_reloc_type r_type;
+
+  r_type = (enum elf_ppc_reloc_type) ELF64_R_TYPE (rela->r_info);
+  switch (r_type)
     {
     case R_PPC64_RELATIVE:
       return reloc_class_relative;
This page took 0.031776 seconds and 4 git commands to generate.