2004-08-04 Michael Chastain <mec.gnu@mindspring.com>
[deliverable/binutils-gdb.git] / bfd / elf32-ppc.c
index a2eec938b9e318b1c554e3ad6de24318cde5fea9..e53c47d4a241f99b2a93d0892b899ccd8d0b6f58 100644 (file)
@@ -1,6 +1,6 @@
 /* PowerPC-specific support for 32-bit ELF
-   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
-   Free Software Foundation, Inc.
+   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
+   2004 Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Cygnus Support.
 
    This file is part of BFD, the Binary File Descriptor library.
@@ -205,9 +205,6 @@ struct ppc_elf_link_hash_table
   elf_linker_section_t *sdata2;
   asection *sbss;
 
-  /* Short-cut to first output tls section.  */
-  asection *tls_sec;
-
   /* Shortcut to .__tls_get_addr.  */
   struct elf_link_hash_entry *tls_get_addr;
 
@@ -285,7 +282,7 @@ ppc_elf_link_hash_table_create (bfd *abfd)
 /* Copy the extra info we tack onto an elf_link_hash_entry.  */
 
 static void
-ppc_elf_copy_indirect_symbol (struct elf_backend_data *bed,
+ppc_elf_copy_indirect_symbol (const struct elf_backend_data *bed,
                              struct elf_link_hash_entry *dir,
                              struct elf_link_hash_entry *ind)
 {
@@ -339,7 +336,8 @@ ppc_elf_copy_indirect_symbol (struct elf_backend_data *bed,
     dir->elf_link_hash_flags |=
       (ind->elf_link_hash_flags & (ELF_LINK_HASH_REF_DYNAMIC
                                   | ELF_LINK_HASH_REF_REGULAR
-                                  | ELF_LINK_HASH_REF_REGULAR_NONWEAK));
+                                  | ELF_LINK_HASH_REF_REGULAR_NONWEAK
+                                  | ELF_LINK_HASH_NEEDS_PLT));
   else
     _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
 }
@@ -1534,6 +1532,35 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0xffff,                /* dst_mask */
         FALSE),                /* pcrel_offset */
 
+  /* Phony relocs to handle branch stubs.  */
+  HOWTO (R_PPC_RELAX32,                /* type */
+        0,                     /* rightshift */
+        0,                     /* size */
+        0,                     /* bitsize */
+        FALSE,                 /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont, /* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_PPC_RELAX32",       /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0,                     /* dst_mask */
+        FALSE),                /* pcrel_offset */
+
+  HOWTO (R_PPC_RELAX32PC,      /* type */
+        0,                     /* rightshift */
+        0,                     /* size */
+        0,                     /* bitsize */
+        FALSE,                 /* pc_relative */
+        0,                     /* bitpos */
+        complain_overflow_dont, /* complain_on_overflow */
+        bfd_elf_generic_reloc, /* special_function */
+        "R_PPC_RELAX32PC",     /* name */
+        FALSE,                 /* partial_inplace */
+        0,                     /* src_mask */
+        0,                     /* dst_mask */
+        FALSE),                /* pcrel_offset */
+
   /* GNU extension to record C++ vtable hierarchy.  */
   HOWTO (R_PPC_GNU_VTINHERIT,  /* type */
         0,                     /* rightshift */
@@ -1599,39 +1626,28 @@ ppc_elf_howto_init (void)
     }
 }
 \f
-/* This function handles relaxing for the PPC with option --mpc860c0[=<n>].
-
-   The MPC860, revision C0 or earlier contains a bug in the die.
-   If all of the following conditions are true, the next instruction
-   to be executed *may* be treated as a no-op.
-   1/ A forward branch is executed.
-   2/ The branch is predicted as not taken.
-   3/ The branch is taken.
-   4/ The branch is located in the last 5 words of a page.
-      (The EOP limit is 5 by default but may be specified as any value
-      from 1-10.)
-
-   Our software solution is to detect these problematic branches in a
-   linker pass and modify them as follows:
-   1/ Unconditional branches - Since these are always predicted taken,
-      there is no problem and no action is required.
-   2/ Conditional backward branches - No problem, no action required.
-   3/ Conditional forward branches - Ensure that the "inverse prediction
-      bit" is set (ensure it is predicted taken).
-   4/ Conditional register branches - Ensure that the "y bit" is set
-      (ensure it is predicted taken).  */
-
-/* Sort sections by address.  */
+#define ARRAY_SIZE(a) (sizeof (a) / sizeof ((a)[0]))
 
-static int
-ppc_elf_sort_rela (const void *arg1, const void *arg2)
-{
-  const Elf_Internal_Rela * const *rela1 = arg1;
-  const Elf_Internal_Rela * const *rela2 = arg2;
+static const int shared_stub_entry[] =
+  {
+    0x7c0802a6, /* mflr 0 */
+    0x429f0005, /* bcl 20, 31, .Lxxx */
+    0x7d6802a6, /* mflr 11 */
+    0x3d6b0000, /* addis 11, 11, (xxx-.Lxxx)@ha */
+    0x396b0018, /* addi 11, 11, (xxx-.Lxxx)@l */
+    0x7c0803a6, /* mtlr 0 */
+    0x7d6903a6, /* mtctr 11 */
+    0x4e800420, /* bctr */
+  };
+
+static const int stub_entry[] =
+  {
+    0x3d600000, /* lis 11,xxx@ha */
+    0x396b0000, /* addi 11,11,xxx@l */
+    0x7d6903a6, /* mtctr 11 */
+    0x4e800420, /* bctr */
+  };
 
-  /* Sort by offset.  */
-  return ((*rela1)->r_offset - (*rela2)->r_offset);
-}
 
 static bfd_boolean
 ppc_elf_relax_section (bfd *abfd,
@@ -1639,246 +1655,377 @@ ppc_elf_relax_section (bfd *abfd,
                       struct bfd_link_info *link_info,
                       bfd_boolean *again)
 {
-#define PAGESIZE 0x1000
+  struct one_fixup
+  {
+    struct one_fixup *next;
+    asection *tsec;
+    bfd_vma toff;
+    bfd_vma trampoff;
+  };
 
+  Elf_Internal_Shdr *symtab_hdr;
   bfd_byte *contents = NULL;
-  bfd_byte *free_contents = NULL;
+  Elf_Internal_Sym *isymbuf = NULL;
   Elf_Internal_Rela *internal_relocs = NULL;
-  Elf_Internal_Rela *free_relocs = NULL;
-  Elf_Internal_Rela **rela_comb = NULL;
-  int comb_curr, comb_count;
+  Elf_Internal_Rela *irel, *irelend;
+  struct one_fixup *fixups = NULL;
+  bfd_boolean changed;
+  struct ppc_elf_link_hash_table *ppc_info;
+  bfd_size_type trampoff;
 
-  /* We never have to do this more than once per input section.  */
   *again = FALSE;
 
-  /* If needed, initialize this section's cooked size.  */
-  if (isec->_cooked_size == 0)
-    isec->_cooked_size = isec->_raw_size;
+  /* Nothing to do if there are no relocations.  */
+  if ((isec->flags & SEC_RELOC) == 0 || isec->reloc_count == 0)
+    return TRUE;
+
+  trampoff = (isec->size + 3) & (bfd_vma) -4;
+  /* Space for a branch around any trampolines.  */
+  trampoff += 4;
 
-  /* We're only interested in text sections which overlap the
-     troublesome area at the end of a page.  */
-  if (link_info->mpc860c0 && (isec->flags & SEC_CODE) && isec->_cooked_size)
+  symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
+
+  /* Get a copy of the native relocations.  */
+  internal_relocs = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
+                                              link_info->keep_memory);
+  if (internal_relocs == NULL)
+    goto error_return;
+
+  ppc_info = ppc_elf_hash_table (link_info);
+  irelend = internal_relocs + isec->reloc_count;
+
+  for (irel = internal_relocs; irel < irelend; irel++)
     {
-      bfd_vma dot, end_page, end_section;
-      bfd_boolean section_modified;
+      unsigned long r_type = ELF32_R_TYPE (irel->r_info);
+      bfd_vma symaddr, reladdr, toff, roff;
+      asection *tsec;
+      struct one_fixup *f;
+      size_t insn_offset = 0;
+      bfd_vma max_branch_offset, val;
+      bfd_byte *hit_addr;
+      unsigned long t0;
+      unsigned char sym_type;
 
-      /* Get the section contents.  */
-      /* Get cached copy if it exists.  */
-      if (elf_section_data (isec)->this_hdr.contents != NULL)
-       contents = elf_section_data (isec)->this_hdr.contents;
-      else
+      switch (r_type)
        {
-         /* Go get them off disk.  */
-         contents = bfd_malloc (isec->_raw_size);
-         if (contents == NULL)
-           goto error_return;
-         free_contents = contents;
-
-         if (! bfd_get_section_contents (abfd, isec, contents,
-                                         (file_ptr) 0, isec->_raw_size))
-           goto error_return;
+       case R_PPC_REL24:
+       case R_PPC_LOCAL24PC:
+       case R_PPC_PLTREL24:
+         max_branch_offset = 1 << 25;
+         break;
+
+       case R_PPC_REL14:
+       case R_PPC_REL14_BRTAKEN:
+       case R_PPC_REL14_BRNTAKEN:
+         max_branch_offset = 1 << 15;
+         break;
+
+       default:
+         continue;
        }
 
-      comb_curr = 0;
-      comb_count = 0;
-      if (isec->reloc_count)
+      /* Get the value of the symbol referred to by the reloc.  */
+      if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
        {
-         unsigned n;
-         bfd_size_type amt;
-
-         /* Get a copy of the native relocations.  */
-         internal_relocs
-           = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL,
-                                        link_info->keep_memory);
-         if (internal_relocs == NULL)
-           goto error_return;
-         if (! link_info->keep_memory)
-           free_relocs = internal_relocs;
-
-         /* Setup a faster access method for the reloc info we need.  */
-         amt = isec->reloc_count;
-         amt *= sizeof (Elf_Internal_Rela*);
-         rela_comb = bfd_malloc (amt);
-         if (rela_comb == NULL)
-           goto error_return;
-         for (n = 0; n < isec->reloc_count; ++n)
-           {
-             enum elf_ppc_reloc_type r_type;
+         /* A local symbol.  */
+         Elf_Internal_Sym *isym;
 
-             r_type = ELF32_R_TYPE (internal_relocs[n].r_info);
-             if (r_type >= R_PPC_max)
+         /* Read this BFD's local symbols.  */
+         if (isymbuf == NULL)
+           {
+             isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
+             if (isymbuf == NULL)
+               isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
+                                               symtab_hdr->sh_info, 0,
+                                               NULL, NULL, NULL);
+             if (isymbuf == 0)
                goto error_return;
+           }
+         isym = isymbuf + ELF32_R_SYM (irel->r_info);
+         if (isym->st_shndx == SHN_UNDEF)
+           continue;   /* We can't do anything with undefined symbols.  */
+         else if (isym->st_shndx == SHN_ABS)
+           tsec = bfd_abs_section_ptr;
+         else if (isym->st_shndx == SHN_COMMON)
+           tsec = bfd_com_section_ptr;
+         else
+           tsec = bfd_section_from_elf_index (abfd, isym->st_shndx);
 
-             /* Prologue constants are sometimes present in the ".text"
-                sections and they can be identified by their associated
-                relocation.  We don't want to process those words and
-                some others which can also be identified by their
-                relocations.  However, not all conditional branches will
-                have a relocation so we will only ignore words that
-                1) have a reloc, and 2) the reloc is not applicable to a
-                conditional branch.  The array rela_comb is built here
-                for use in the EOP scan loop.  */
-             switch (r_type)
-               {
-               case R_PPC_ADDR14_BRNTAKEN:
-               case R_PPC_REL14:
-               case R_PPC_REL14_BRNTAKEN:
-                 /* We should check the instruction.  */
-                 break;
-               default:
-                 /* The word is not a conditional branch - ignore it.  */
-                 rela_comb[comb_count++] = &internal_relocs[n];
-                 break;
-               }
+         toff = isym->st_value;
+         sym_type = ELF_ST_TYPE (isym->st_info);
+       }
+      else
+       {
+         /* Global symbol handling.  */
+         unsigned long indx;
+         struct elf_link_hash_entry *h;
+
+         indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
+         h = elf_sym_hashes (abfd)[indx];
+
+         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 (r_type == R_PPC_PLTREL24
+             && ppc_info->plt != NULL
+             && h->plt.offset != (bfd_vma) -1)
+           {
+             tsec = ppc_info->plt;
+             toff = h->plt.offset;
+           }
+         else if (h->root.type == bfd_link_hash_defined
+                  || h->root.type == bfd_link_hash_defweak)
+           {
+             tsec = h->root.u.def.section;
+             toff = h->root.u.def.value;
            }
-         if (comb_count > 1)
-           qsort (rela_comb, (size_t) comb_count, sizeof (int),
-                  ppc_elf_sort_rela);
+         else
+           continue;
+
+         sym_type = h->type;
+       }
+
+      /* If the branch and target are in the same section, you have
+        no hope of adding stubs.  We'll error out later should the
+        branch overflow.  */
+      if (tsec == isec)
+       continue;
+
+      /* There probably isn't any reason to handle symbols in
+        SEC_MERGE sections;  SEC_MERGE doesn't seem a likely
+        attribute for a code section, and we are only looking at
+        branches.  However, implement it correctly here as a
+        reference for other target relax_section functions.  */
+      if (0 && tsec->sec_info_type == ELF_INFO_TYPE_MERGE)
+       {
+         /* At this stage in linking, no SEC_MERGE symbol has been
+            adjusted, so all references to such symbols need to be
+            passed through _bfd_merged_section_offset.  (Later, in
+            relocate_section, all SEC_MERGE symbols *except* for
+            section symbols have been adjusted.)
+
+            gas may reduce relocations against symbols in SEC_MERGE
+            sections to a relocation against the section symbol when
+            the original addend was zero.  When the reloc is against
+            a section symbol we should include the addend in the
+            offset passed to _bfd_merged_section_offset, since the
+            location of interest is the original symbol.  On the
+            other hand, an access to "sym+addend" where "sym" is not
+            a section symbol should not include the addend;  Such an
+            access is presumed to be an offset from "sym";  The
+            location of interest is just "sym".  */
+         if (sym_type == STT_SECTION)
+           toff += irel->r_addend;
+
+         toff = _bfd_merged_section_offset (abfd, &tsec,
+                                            elf_section_data (tsec)->sec_info,
+                                            toff);
+
+         if (sym_type != STT_SECTION)
+           toff += irel->r_addend;
        }
+      else
+       toff += irel->r_addend;
+
+      symaddr = tsec->output_section->vma + tsec->output_offset + toff;
+
+      roff = irel->r_offset;
+      reladdr = isec->output_section->vma + isec->output_offset + roff;
+
+      /* If the branch is in range, no need to do anything.  */
+      if (symaddr - reladdr + max_branch_offset < 2 * max_branch_offset)
+       continue;
 
-      /* Enumerate each EOP region that overlaps this section.  */
-      end_section = isec->vma + isec->_cooked_size;
-      dot = end_page = (isec->vma | (PAGESIZE - 1)) + 1;
-      dot -= link_info->mpc860c0;
-      section_modified = FALSE;
-      /* Increment the start position if this section begins in the
-        middle of its first EOP region.  */
-      if (dot < isec->vma)
-       dot = isec->vma;
-      for (;
-          dot < end_section;
-          dot += PAGESIZE, end_page += PAGESIZE)
+      /* Look for an existing fixup to this address.  */
+      for (f = fixups; f ; f = f->next)
+       if (f->tsec == tsec && f->toff == toff)
+         break;
+
+      if (f == NULL)
        {
-         /* Check each word in this EOP region.  */
-         for (; dot < end_page; dot += 4)
+         size_t size;
+         unsigned long stub_rtype;
+
+         val = trampoff - roff;
+         if (val >= max_branch_offset)
+           /* Oh dear, we can't reach a trampoline.  Don't try to add
+              one.  We'll report an error later.  */
+           continue;
+
+         if (link_info->shared)
            {
-             bfd_vma isec_offset;
-             unsigned long insn;
-             bfd_boolean skip, modified;
-
-             /* Don't process this word if there is a relocation for it
-                and the relocation indicates the word is not a
-                conditional branch.  */
-             skip = FALSE;
-             isec_offset = dot - isec->vma;
-             for (; comb_curr<comb_count; ++comb_curr)
-               {
-                 bfd_vma r_offset;
+             size = 4 * ARRAY_SIZE (shared_stub_entry);
+             insn_offset = 12;
+             stub_rtype = R_PPC_RELAX32PC;
+           }
+         else
+           {
+             size = 4 * ARRAY_SIZE (stub_entry);
+             insn_offset = 0;
+             stub_rtype = R_PPC_RELAX32;
+           }
 
-                 r_offset = rela_comb[comb_curr]->r_offset;
-                 if (r_offset >= isec_offset)
-                   {
-                     if (r_offset == isec_offset) skip = TRUE;
-                     break;
-                   }
-               }
-             if (skip) continue;
-
-             /* Check the current word for a problematic conditional
-                branch.  */
-#define BO0(insn) ((insn) & 0x02000000)
-#define BO2(insn) ((insn) & 0x00800000)
-#define BO4(insn) ((insn) & 0x00200000)
-             insn = (unsigned long) bfd_get_32 (abfd, contents + isec_offset);
-             modified = FALSE;
-             if ((insn & 0xFc000000) == 0x40000000)
-               {
-                 /* Instruction is BCx */
-                 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
-                   {
-                     bfd_vma target;
-
-                     /* This branch is predicted as "normal".
-                        If this is a forward branch, it is problematic.  */
-                     target = insn & 0x0000Fffc;
-                     target = (target ^ 0x8000) - 0x8000;
-                     if ((insn & 0x00000002) == 0)
-                       /* Convert to abs.  */
-                       target += dot;
-                     if (target > dot)
-                       {
-                         /* Set the prediction bit.  */
-                         insn |= 0x00200000;
-                         modified = TRUE;
-                       }
-                   }
-               }
-             else if ((insn & 0xFc00Fffe) == 0x4c000420)
-               {
-                 /* Instruction is BCCTRx.  */
-                 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
-                   {
-                     /* This branch is predicted as not-taken.
-                        If this is a forward branch, it is problematic.
-                        Since we can't tell statically if it will branch
-                        forward, always set the prediction bit.  */
-                     insn |= 0x00200000;
-                     modified = TRUE;
-                   }
-               }
-             else if ((insn & 0xFc00Fffe) == 0x4c000020)
-               {
-                 /* Instruction is BCLRx */
-                 if ((!BO0(insn) || !BO2(insn)) && !BO4(insn))
-                   {
-                     /* This branch is predicted as not-taken.
-                        If this is a forward branch, it is problematic.
-                        Since we can't tell statically if it will branch
-                        forward, always set the prediction bit.  */
-                     insn |= 0x00200000;
-                     modified = TRUE;
-                   }
-               }
-#undef BO0
-#undef BO2
-#undef BO4
-             if (modified)
-               {
-                 bfd_put_32 (abfd, insn, contents + isec_offset);
-                 section_modified = TRUE;
-               }
+         /* Hijack the old relocation.  Since we need two
+            relocations for this use a "composite" reloc.  */
+         irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
+                                      stub_rtype);
+         irel->r_offset = trampoff + insn_offset;
+
+         /* Record the fixup so we don't do it again this section.  */
+         f = bfd_malloc (sizeof (*f));
+         f->next = fixups;
+         f->tsec = tsec;
+         f->toff = toff;
+         f->trampoff = trampoff;
+         fixups = f;
+
+         trampoff += size;
+       }
+      else
+       {
+         val = f->trampoff - roff;
+         if (val >= max_branch_offset)
+           continue;
+
+         /* Nop out the reloc, since we're finalizing things here.  */
+         irel->r_info = ELF32_R_INFO (0, R_PPC_NONE);
+       }
+
+      /* Get the section contents.  */
+      if (contents == NULL)
+       {
+         /* Get cached copy if it exists.  */
+         if (elf_section_data (isec)->this_hdr.contents != NULL)
+           contents = elf_section_data (isec)->this_hdr.contents;
+         else
+           {
+             /* Go get them off disk.  */
+             if (!bfd_malloc_and_get_section (abfd, isec, &contents))
+               goto error_return;
            }
        }
-      if (section_modified)
+
+      /* Fix up the existing branch to hit the trampoline.  */
+      hit_addr = contents + roff;
+      switch (r_type)
        {
-         elf_section_data (isec)->this_hdr.contents = contents;
-         free_contents = NULL;
+       case R_PPC_REL24:
+       case R_PPC_LOCAL24PC:
+       case R_PPC_PLTREL24:
+         t0 = bfd_get_32 (abfd, hit_addr);
+         t0 &= ~0x3fffffc;
+         t0 |= val & 0x3fffffc;
+         bfd_put_32 (abfd, t0, hit_addr);
+         break;
+
+       case R_PPC_REL14:
+       case R_PPC_REL14_BRTAKEN:
+       case R_PPC_REL14_BRNTAKEN:
+         t0 = bfd_get_32 (abfd, hit_addr);
+         t0 &= ~0xfffc;
+         t0 |= val & 0xfffc;
+         bfd_put_32 (abfd, t0, hit_addr);
+         break;
        }
     }
 
-  if (rela_comb != NULL)
+  /* Write out the trampolines.  */
+  changed = fixups != NULL;
+  if (fixups != NULL)
     {
-      free (rela_comb);
-      rela_comb = NULL;
+      const int *stub;
+      bfd_byte *dest;
+      bfd_vma val;
+      int i, size;
+
+      do
+       {
+         struct one_fixup *f = fixups;
+         fixups = fixups->next;
+         free (f);
+       }
+      while (fixups);
+
+      contents = bfd_realloc (contents, trampoff);
+      if (contents == NULL)
+       goto error_return;
+
+      isec->size = (isec->size + 3) & (bfd_vma) -4;
+      /* Branch around the trampolines.  */
+      val = trampoff - isec->size + 0x48000000;
+      dest = contents + isec->size;
+      isec->size = trampoff;
+      bfd_put_32 (abfd, val, dest);
+      dest += 4;
+
+      if (link_info->shared)
+       {
+         stub = shared_stub_entry;
+         size = ARRAY_SIZE (shared_stub_entry);
+       }
+      else
+       {
+         stub = stub_entry;
+         size = ARRAY_SIZE (stub_entry);
+       }
+
+      i = 0;
+      while (dest < contents + trampoff)
+       {
+         bfd_put_32 (abfd, stub[i], dest);
+         i++;
+         if (i == size)
+           i = 0;
+         dest += 4;
+       }
+      BFD_ASSERT (i == 0);
     }
 
-  if (free_relocs != NULL)
+  if (isymbuf != NULL
+      && symtab_hdr->contents != (unsigned char *) isymbuf)
     {
-      free (free_relocs);
-      free_relocs = NULL;
+      if (! link_info->keep_memory)
+       free (isymbuf);
+      else
+       {
+         /* Cache the symbols for elf_link_input_bfd.  */
+         symtab_hdr->contents = (unsigned char *) isymbuf;
+       }
     }
 
-  if (free_contents != NULL)
+  if (contents != NULL
+      && elf_section_data (isec)->this_hdr.contents != contents)
     {
-      if (! link_info->keep_memory)
-       free (free_contents);
+      if (!changed && !link_info->keep_memory)
+       free (contents);
       else
        {
          /* Cache the section contents for elf_link_input_bfd.  */
          elf_section_data (isec)->this_hdr.contents = contents;
        }
-      free_contents = NULL;
     }
 
+  if (elf_section_data (isec)->relocs != internal_relocs)
+    {
+      if (!changed)
+       free (internal_relocs);
+      else
+       elf_section_data (isec)->relocs = internal_relocs;
+    }
+
+  *again = changed;
   return TRUE;
 
  error_return:
-  if (rela_comb != NULL)
-    free (rela_comb);
-  if (free_relocs != NULL)
-    free (free_relocs);
-  if (free_contents != NULL)
-    free (free_contents);
+  if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
+    free (isymbuf);
+  if (contents != NULL
+      && elf_section_data (isec)->this_hdr.contents != contents)
+    free (contents);
+  if (internal_relocs != NULL
+      && elf_section_data (isec)->relocs != internal_relocs)
+    free (internal_relocs);
   return FALSE;
 }
 \f
@@ -2017,7 +2164,7 @@ ppc_elf_addr16_ha_reloc (bfd *abfd ATTRIBUTE_UNUSED,
       return bfd_reloc_ok;
     }
 
-  if (reloc_entry->address > input_section->_cooked_size)
+  if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
     return bfd_reloc_outofrange;
 
   if (bfd_is_com_section (symbol->section))
@@ -2274,12 +2421,12 @@ elf_create_pointer_linker_section (bfd *abfd,
       /* Make sure this symbol is output as a dynamic symbol.  */
       if (h->dynindx == -1)
        {
-         if (! _bfd_elf_link_record_dynamic_symbol (info, h))
+         if (! bfd_elf_link_record_dynamic_symbol (info, h))
            return FALSE;
        }
 
       if (lsect->rel_section)
-       lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
+       lsect->rel_section->size += sizeof (Elf32_External_Rela);
     }
   else
     {
@@ -2315,7 +2462,7 @@ elf_create_pointer_linker_section (bfd *abfd,
             output a R_<xxx>_RELATIVE reloc so that the
             dynamic linker can adjust this GOT entry.  */
          BFD_ASSERT (lsect->rel_section != NULL);
-         lsect->rel_section->_raw_size += sizeof (Elf32_External_Rela);
+         lsect->rel_section->size += sizeof (Elf32_External_Rela);
        }
     }
 
@@ -2334,14 +2481,14 @@ elf_create_pointer_linker_section (bfd *abfd,
   linker_section_ptr->written_address_p = FALSE;
   *ptr_linker_section_ptr = linker_section_ptr;
 
-  linker_section_ptr->offset = lsect->section->_raw_size;
-  lsect->section->_raw_size += 4;
+  linker_section_ptr->offset = lsect->section->size;
+  lsect->section->size += 4;
 
 #ifdef DEBUG
   fprintf (stderr,
           "Create pointer in linker section %s, offset = %ld, section size = %ld\n",
           lsect->name, (long) linker_section_ptr->offset,
-          (long) lsect->section->_raw_size);
+          (long) lsect->section->size);
 #endif
 
   return TRUE;
@@ -2429,11 +2576,12 @@ elf_finish_pointer_linker_section (bfd *output_bfd,
              asection *srel = lsect->rel_section;
              Elf_Internal_Rela outrel[MAX_INT_RELS_PER_EXT_REL];
              bfd_byte *erel;
-             struct elf_backend_data *bed = get_elf_backend_data (output_bfd);
+             const struct elf_backend_data *bed;
              unsigned int i;
 
              BFD_ASSERT (srel != NULL);
 
+             bed = get_elf_backend_data (output_bfd);
              for (i = 0; i < bed->s->int_rels_per_ext_rel; i++)
                {
                  outrel[i].r_offset = (lsect->section->output_section->vma
@@ -2474,7 +2622,7 @@ ppc_elf_create_linker_section (bfd *abfd,
 {
   elf_linker_section_t *lsect;
   struct ppc_elf_link_hash_table *htab = ppc_elf_hash_table (info);
-  asection *s;
+  asection *s, *sym_sec;
   bfd_size_type amt;
   flagword flags;
   const char *name;
@@ -2523,13 +2671,15 @@ ppc_elf_create_linker_section (bfd *abfd,
   lsect->sym_offset = sym_offset;
 
   /* See if the sections already exist.  */
-  s = bfd_get_section_by_name (htab->elf.dynobj, name);
+  sym_sec = s = bfd_get_section_by_name (htab->elf.dynobj, name);
   if (s == NULL || (s->flags & flags) != flags)
     {
       s = bfd_make_section_anyway (htab->elf.dynobj, name);
       if (s == NULL
          || !bfd_set_section_flags (htab->elf.dynobj, s, flags))
        return NULL;
+      if (sym_sec == NULL)
+       sym_sec = s;
     }
   lsect->section = s;
 
@@ -2537,11 +2687,11 @@ ppc_elf_create_linker_section (bfd *abfd,
       && !bfd_set_section_alignment (htab->elf.dynobj, s, 2))
     return NULL;
 
-  s->_raw_size = align_power (s->_raw_size, 2);
+  s->size = align_power (s->size, 2);
 
 #ifdef DEBUG
   fprintf (stderr, "Creating section %s, current size = %ld\n",
-          name, (long) s->_raw_size);
+          name, (long) s->size);
 #endif
 
   if (sym_name)
@@ -2557,7 +2707,7 @@ ppc_elf_create_linker_section (bfd *abfd,
 
       if ((bh == NULL || bh->type == bfd_link_hash_undefined)
          && !(_bfd_generic_link_add_one_symbol
-              (info, abfd, sym_name, BSF_GLOBAL, s, sym_offset, NULL,
+              (info, abfd, sym_name, BSF_GLOBAL, sym_sec, sym_offset, NULL,
                FALSE, get_elf_backend_data (abfd)->collect, &bh)))
        return NULL;
       h = (struct elf_link_hash_entry *) bh;
@@ -2566,7 +2716,7 @@ ppc_elf_create_linker_section (bfd *abfd,
       lsect->sym_hash = h;
 
       if (info->shared
-         && ! _bfd_elf_link_record_dynamic_symbol (info, h))
+         && ! bfd_elf_link_record_dynamic_symbol (info, h))
        return NULL;
     }
 
@@ -2601,11 +2751,11 @@ ppc_elf_additional_program_headers (bfd *abfd)
     ++ret;
 
   s = bfd_get_section_by_name (abfd, ".sbss2");
-  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
+  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size > 0)
     ++ret;
 
   s = bfd_get_section_by_name (abfd, ".PPC.EMB.sbss0");
-  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->_raw_size > 0)
+  if (s != NULL && (s->flags & SEC_LOAD) != 0 && s->size > 0)
     ++ret;
 
   return ret;
@@ -2614,7 +2764,8 @@ ppc_elf_additional_program_headers (bfd *abfd)
 /* Modify the segment map if needed.  */
 
 static bfd_boolean
-ppc_elf_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED)
+ppc_elf_modify_segment_map (bfd *abfd ATTRIBUTE_UNUSED,
+                           struct bfd_link_info *info ATTRIBUTE_UNUSED)
 {
   return TRUE;
 }
@@ -2844,7 +2995,7 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
       else
        srel = htab->relbss;
       BFD_ASSERT (srel != NULL);
-      srel->_raw_size += sizeof (Elf32_External_Rela);
+      srel->size += sizeof (Elf32_External_Rela);
       h->elf_link_hash_flags |= ELF_LINK_HASH_NEEDS_COPY;
     }
 
@@ -2855,8 +3006,7 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
     power_of_two = 4;
 
   /* Apply the required alignment.  */
-  s->_raw_size = BFD_ALIGN (s->_raw_size,
-                           (bfd_size_type) (1 << power_of_two));
+  s->size = BFD_ALIGN (s->size, (bfd_size_type) (1 << power_of_two));
   if (power_of_two > bfd_get_section_alignment (htab->elf.dynobj, s))
     {
       if (! bfd_set_section_alignment (htab->elf.dynobj, s, power_of_two))
@@ -2865,25 +3015,14 @@ ppc_elf_adjust_dynamic_symbol (struct bfd_link_info *info,
 
   /* Define the symbol as being at this point in the section.  */
   h->root.u.def.section = s;
-  h->root.u.def.value = s->_raw_size;
+  h->root.u.def.value = s->size;
 
   /* Increment the section size to make room for the symbol.  */
-  s->_raw_size += h->size;
+  s->size += h->size;
 
   return TRUE;
 }
 \f
-/* This is the condition under which finish_dynamic_symbol will be
-   called from elflink.h.  If elflink.h doesn't call our
-   finish_dynamic_symbol routine, we'll need to do something about
-   initializing any .plt and .got entries in relocate_section.  */
-#define WILL_CALL_FINISH_DYNAMIC_SYMBOL(DYN, SHARED, H) \
-  ((DYN)                                                               \
-   && ((SHARED)                                                                \
-       || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)     \
-   && ((H)->dynindx != -1                                              \
-       || ((H)->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) != 0))
-
 /* Of those relocs that might be copied as dynamic relocs, this macro
    selects those that must be copied when linking a shared library,
    even when the symbol is local.  */
@@ -2922,7 +3061,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *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_elf_link_record_dynamic_symbol (info, h))
            return FALSE;
        }
 
@@ -2933,15 +3072,15 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 
          /* If this is the first .plt entry, make room for the special
             first entry.  */
-         if (s->_raw_size == 0)
-           s->_raw_size += PLT_INITIAL_ENTRY_SIZE;
+         if (s->size == 0)
+           s->size += PLT_INITIAL_ENTRY_SIZE;
 
          /* The PowerPC PLT is actually composed of two parts, the
             first part is 2 words (for a load and a jump), and then
             there is a remaining word available at the end.  */
          h->plt.offset = (PLT_INITIAL_ENTRY_SIZE
                           + (PLT_SLOT_SIZE
-                             * ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE)
+                             * ((s->size - PLT_INITIAL_ENTRY_SIZE)
                                 / PLT_ENTRY_SIZE)));
 
          /* If this symbol is not defined in a regular file, and we
@@ -2958,13 +3097,13 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
 
          /* Make room for this entry.  After the 8192nd entry, room
             for two entries is allocated.  */
-         s->_raw_size += PLT_ENTRY_SIZE;
-         if ((s->_raw_size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
+         s->size += PLT_ENTRY_SIZE;
+         if ((s->size - PLT_INITIAL_ENTRY_SIZE) / PLT_ENTRY_SIZE
              > PLT_NUM_SINGLE_ENTRIES)
-           s->_raw_size += PLT_ENTRY_SIZE;
+           s->size += PLT_ENTRY_SIZE;
 
          /* We also need to make an entry in the .rela.plt section.  */
-         htab->relplt->_raw_size += sizeof (Elf32_External_Rela);
+         htab->relplt->size += sizeof (Elf32_External_Rela);
        }
       else
        {
@@ -2985,7 +3124,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       if (eh->elf.dynindx == -1
          && (eh->elf.elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
        {
-         if (!bfd_elf32_link_record_dynamic_symbol (info, &eh->elf))
+         if (!bfd_elf_link_record_dynamic_symbol (info, &eh->elf))
            return FALSE;
        }
 
@@ -2996,20 +3135,20 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       else
        {
          bfd_boolean dyn;
-         eh->elf.got.offset = htab->got->_raw_size;
+         eh->elf.got.offset = htab->got->size;
          if ((eh->tls_mask & TLS_TLS) != 0)
            {
              if ((eh->tls_mask & TLS_LD) != 0)
-               htab->got->_raw_size += 8;
+               htab->got->size += 8;
              if ((eh->tls_mask & TLS_GD) != 0)
-               htab->got->_raw_size += 8;
+               htab->got->size += 8;
              if ((eh->tls_mask & (TLS_TPREL | TLS_TPRELGD)) != 0)
-               htab->got->_raw_size += 4;
+               htab->got->size += 4;
              if ((eh->tls_mask & TLS_DTPREL) != 0)
-               htab->got->_raw_size += 4;
+               htab->got->size += 4;
            }
          else
-           htab->got->_raw_size += 4;
+           htab->got->size += 4;
          dyn = htab->elf.dynamic_sections_created;
          if ((info->shared
               || WILL_CALL_FINISH_DYNAMIC_SYMBOL (dyn, 0, &eh->elf))
@@ -3017,12 +3156,12 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
                  || eh->elf.root.type != bfd_link_hash_undefweak))
            {
              /* All the entries we allocated need relocs.  */
-             htab->relgot->_raw_size
-               += ((htab->got->_raw_size - eh->elf.got.offset) / 4
+             htab->relgot->size
+               += ((htab->got->size - eh->elf.got.offset) / 4
                    * sizeof (Elf32_External_Rela));
              /* Except LD only needs one.  */
              if ((eh->tls_mask & TLS_LD) != 0)
-               htab->relgot->_raw_size -= sizeof (Elf32_External_Rela);
+               htab->relgot->size -= sizeof (Elf32_External_Rela);
            }
        }
     }
@@ -3066,6 +3205,18 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
       if (ELF_ST_VISIBILITY (h->other) != STV_DEFAULT
          && h->root.type == bfd_link_hash_undefweak)
        eh->dyn_relocs = NULL;
+
+      /* Make sure undefined weak symbols are output as a dynamic symbol
+        in PIEs.  */
+      if (info->pie
+         && eh->dyn_relocs != NULL
+         && h->dynindx == -1
+         && h->root.type == bfd_link_hash_undefweak
+         && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
+       {
+         if (! bfd_elf_link_record_dynamic_symbol (info, h))
+           return FALSE;
+       }
     }
   else if (ELIMINATE_COPY_RELOCS)
     {
@@ -3082,7 +3233,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
          if (h->dynindx == -1
              && (h->elf_link_hash_flags & ELF_LINK_FORCED_LOCAL) == 0)
            {
-             if (! bfd_elf64_link_record_dynamic_symbol (info, h))
+             if (! bfd_elf_link_record_dynamic_symbol (info, h))
                return FALSE;
            }
 
@@ -3101,7 +3252,7 @@ allocate_dynrelocs (struct elf_link_hash_entry *h, void *inf)
   for (p = eh->dyn_relocs; p != NULL; p = p->next)
     {
       asection *sreloc = elf_section_data (p->sec)->sreloc;
-      sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
+      sreloc->size += p->count * sizeof (Elf32_External_Rela);
     }
 
   return TRUE;
@@ -3162,17 +3313,17 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
        {
          s = bfd_get_section_by_name (htab->elf.dynobj, ".interp");
          BFD_ASSERT (s != NULL);
-         s->_raw_size = sizeof ELF_DYNAMIC_INTERPRETER;
+         s->size = sizeof ELF_DYNAMIC_INTERPRETER;
          s->contents = (unsigned char *) ELF_DYNAMIC_INTERPRETER;
        }
     }
 
   if (htab->tlsld_got.refcount > 0)
     {
-      htab->tlsld_got.offset = htab->got->_raw_size;
-      htab->got->_raw_size += 8;
+      htab->tlsld_got.offset = htab->got->size;
+      htab->got->size += 8;
       if (info->shared)
-       htab->relgot->_raw_size += sizeof (Elf32_External_Rela);
+       htab->relgot->size += sizeof (Elf32_External_Rela);
     }
   else
     htab->tlsld_got.offset = (bfd_vma) -1;
@@ -3210,7 +3361,7 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
                }
              else if (p->count != 0)
                {
-                 elf_section_data (p->sec)->sreloc->_raw_size
+                 elf_section_data (p->sec)->sreloc->size
                    += p->count * sizeof (Elf32_External_Rela);
                  if ((p->sec->output_section->flags
                       & (SEC_READONLY | SEC_ALLOC))
@@ -3239,30 +3390,30 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
                   htab->tlsld_got.offset.  */
                if (htab->tlsld_got.offset == (bfd_vma) -1)
                  {
-                   htab->tlsld_got.offset = s->_raw_size;
-                   s->_raw_size += 8;
+                   htab->tlsld_got.offset = s->size;
+                   s->size += 8;
                    if (info->shared)
-                     srel->_raw_size += sizeof (Elf32_External_Rela);
+                     srel->size += sizeof (Elf32_External_Rela);
                  }
                *local_got = (bfd_vma) -1;
              }
            else
              {
-               *local_got = s->_raw_size;
+               *local_got = s->size;
                if ((*lgot_masks & TLS_TLS) != 0)
                  {
                    if ((*lgot_masks & TLS_GD) != 0)
-                     s->_raw_size += 8;
+                     s->size += 8;
                    if ((*lgot_masks & (TLS_TPREL | TLS_TPRELGD)) != 0)
-                     s->_raw_size += 4;
+                     s->size += 4;
                    if ((*lgot_masks & TLS_DTPREL) != 0)
-                     s->_raw_size += 4;
+                     s->size += 4;
                  }
                else
-                 s->_raw_size += 4;
+                 s->size += 4;
                if (info->shared)
-                 srel->_raw_size += ((s->_raw_size - *local_got) / 4
-                                     * sizeof (Elf32_External_Rela));
+                 srel->size += ((s->size - *local_got) / 4
+                                * sizeof (Elf32_External_Rela));
              }
          }
        else
@@ -3290,7 +3441,7 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
        }
       else if (strncmp (bfd_get_section_name (dynobj, s), ".rela", 5) == 0)
        {
-         if (s->_raw_size == 0)
+         if (s->size == 0)
            {
              /* If we don't need this section, strip it from the
                 output file.  This is mostly to handle .rela.bss and
@@ -3318,14 +3469,14 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
          continue;
        }
 
-      if (s->_raw_size == 0)
+      if (s->size == 0)
        {
          _bfd_strip_section_from_output (info, s);
          continue;
        }
 
       /* Allocate memory for the section contents.  */
-      s->contents = bfd_zalloc (htab->elf.dynobj, s->_raw_size);
+      s->contents = bfd_zalloc (htab->elf.dynobj, s->size);
       if (s->contents == NULL)
        return FALSE;
     }
@@ -3338,7 +3489,7 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
         the .dynamic section.  The DT_DEBUG entry is filled in by the
         dynamic linker and used by the debugger.  */
 #define add_dynamic_entry(TAG, VAL) \
-  bfd_elf32_add_dynamic_entry (info, (TAG), (VAL))
+  _bfd_elf_add_dynamic_entry (info, TAG, VAL)
 
       if (info->executable)
        {
@@ -3346,7 +3497,7 @@ ppc_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
            return FALSE;
        }
 
-      if (htab->plt != NULL && htab->plt->_raw_size != 0)
+      if (htab->plt != NULL && htab->plt->size != 0)
        {
          if (!add_dynamic_entry (DT_PLTGOT, 0)
              || !add_dynamic_entry (DT_PLTRELSZ, 0)
@@ -3674,14 +3825,14 @@ ppc_elf_check_relocs (bfd *abfd,
          /* This relocation describes the C++ object vtable hierarchy.
             Reconstruct it for later use during GC.  */
        case R_PPC_GNU_VTINHERIT:
-         if (!_bfd_elf32_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
+         if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
            return FALSE;
          break;
 
          /* This relocation describes which C++ vtable entries are actually
             used.  Record for later use during GC.  */
        case R_PPC_GNU_VTENTRY:
-         if (!_bfd_elf32_gc_record_vtentry (abfd, sec, h, rel->r_addend))
+         if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
            return FALSE;
          break;
 
@@ -4031,25 +4182,18 @@ ppc_elf_gc_sweep_hook (bfd *abfd,
   return TRUE;
 }
 
-/* Set htab->tls_sec and htab->tls_get_addr.  */
+/* Set htab->tls_get_addr and call the generic ELF tls_setup function.  */
 
-bfd_boolean
+asection *
 ppc_elf_tls_setup (bfd *obfd, struct bfd_link_info *info)
 {
-  asection *tls;
   struct ppc_elf_link_hash_table *htab;
 
   htab = ppc_elf_hash_table (info);
   htab->tls_get_addr = elf_link_hash_lookup (&htab->elf, "__tls_get_addr",
                                             FALSE, FALSE, TRUE);
 
-  for (tls = obfd->sections; tls != NULL; tls = tls->next)
-    if ((tls->flags & (SEC_THREAD_LOCAL | SEC_LOAD))
-       == (SEC_THREAD_LOCAL | SEC_LOAD))
-      break;
-  htab->tls_sec = tls;
-
-  return tls != NULL;
+  return _bfd_elf_tls_setup (obfd, info);
 }
 
 /* Run through all the TLS relocs looking for optimization
@@ -4251,7 +4395,7 @@ ppc_elf_tls_optimize (bfd *obfd ATTRIBUTE_UNUSED,
 static bfd_boolean
 ppc_elf_add_symbol_hook (bfd *abfd,
                         struct bfd_link_info *info,
-                        const Elf_Internal_Sym *sym,
+                        Elf_Internal_Sym *sym,
                         const char **namep ATTRIBUTE_UNUSED,
                         flagword *flagsp ATTRIBUTE_UNUSED,
                         asection **secp,
@@ -4417,7 +4561,7 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
       BFD_ASSERT (htab->plt != NULL && sdyn != NULL);
 
       dyncon = (Elf32_External_Dyn *) sdyn->contents;
-      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->_raw_size);
+      dynconend = (Elf32_External_Dyn *) (sdyn->contents + sdyn->size);
       for (; dyncon < dynconend; dyncon++)
        {
          Elf_Internal_Dyn dyn;
@@ -4433,7 +4577,7 @@ ppc_elf_finish_dynamic_sections (bfd *output_bfd,
              break;
 
            case DT_PLTRELSZ:
-             dyn.d_un.d_val = htab->relplt->_raw_size;
+             dyn.d_un.d_val = htab->relplt->size;
              break;
 
            case DT_JMPREL:
@@ -4565,54 +4709,23 @@ ppc_elf_relocate_section (bfd *output_bfd,
       unresolved_reloc = FALSE;
       warned = FALSE;
       r_symndx = ELF32_R_SYM (rel->r_info);
+
       if (r_symndx < symtab_hdr->sh_info)
        {
          sym = local_syms + r_symndx;
          sec = local_sections[r_symndx];
          sym_name = bfd_elf_local_sym_name (input_bfd, sym);
 
-         relocation = _bfd_elf_rela_local_sym (output_bfd, sym, sec, rel);
+         relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
        }
       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;
-         sym_name = h->root.root.string;
+         RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
+                                  r_symndx, symtab_hdr, sym_hashes,
+                                  h, sec, relocation,
+                                  unresolved_reloc, warned);
 
-         relocation = 0;
-         if (h->root.type == bfd_link_hash_defined
-             || h->root.type == bfd_link_hash_defweak)
-           {
-             sec = h->root.u.def.section;
-             /* Set a flag that will be cleared later if we find a
-                relocation value for this symbol.  output_section
-                is typically NULL for symbols satisfied by a shared
-                library.  */
-             if (sec->output_section == NULL)
-               unresolved_reloc = TRUE;
-             else
-               relocation = (h->root.u.def.value
-                             + sec->output_section->vma
-                             + sec->output_offset);
-           }
-         else if (h->root.type == bfd_link_hash_undefweak)
-           ;
-         else if (!info->executable
-                  && !info->no_undefined
-                  && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
-           ;
-         else
-           {
-             if (! ((*info->callbacks->undefined_symbol)
-                    (info, h->root.root.string, input_bfd, input_section,
-                     rel->r_offset, (!info->shared
-                                     || info->no_undefined
-                                     || ELF_ST_VISIBILITY (h->other)))))
-               return FALSE;
-             warned = TRUE;
-           }
+         sym_name = h->root.root.string;
        }
 
       /* TLS optimizations.  Replace instruction sequences and relocs
@@ -4795,8 +4908,8 @@ ppc_elf_relocate_section (bfd *output_bfd,
                        {
                          /* Was an LD reloc.  */
                          r_symndx = 0;
-                         rel->r_addend = htab->tls_sec->vma + DTP_OFFSET;
-                         rel[1].r_addend = htab->tls_sec->vma + DTP_OFFSET;
+                         rel->r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
+                         rel[1].r_addend = htab->elf.tls_sec->vma + DTP_OFFSET;
                        }
                      r_type = R_PPC_TPREL16_HA;
                      rel->r_info = ELF32_R_INFO (r_symndx, r_type);
@@ -4831,7 +4944,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
          branch_bit = BRANCH_PREDICT_BIT;
          /* Fall thru */
 
-         /* Branch not taken predicition relocations.  */
+         /* Branch not taken prediction relocations.  */
        case R_PPC_ADDR14_BRNTAKEN:
        case R_PPC_REL14_BRNTAKEN:
          insn = bfd_get_32 (output_bfd, contents + rel->r_offset);
@@ -5034,7 +5147,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                          {
                            outrel.r_addend += relocation;
                            if (tls_ty & (TLS_GD | TLS_DTPREL | TLS_TPREL))
-                             outrel.r_addend -= htab->tls_sec->vma;
+                             outrel.r_addend -= htab->elf.tls_sec->vma;
                          }
                        loc = htab->relgot->contents;
                        loc += (htab->relgot->reloc_count++
@@ -5052,7 +5165,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                          value = 1;
                        else if (tls_ty != 0)
                          {
-                           value -= htab->tls_sec->vma + DTP_OFFSET;
+                           value -= htab->elf.tls_sec->vma + DTP_OFFSET;
                            if (tls_ty == (TLS_TLS | TLS_TPREL))
                              value += DTP_OFFSET - TP_OFFSET;
 
@@ -5140,7 +5253,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
        case R_PPC_DTPREL16_LO:
        case R_PPC_DTPREL16_HI:
        case R_PPC_DTPREL16_HA:
-         addend -= htab->tls_sec->vma + DTP_OFFSET;
+         addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
          break;
 
          /* Relocations that may need to be propagated if this is a shared
@@ -5149,18 +5262,18 @@ ppc_elf_relocate_section (bfd *output_bfd,
        case R_PPC_TPREL16_LO:
        case R_PPC_TPREL16_HI:
        case R_PPC_TPREL16_HA:
-         addend -= htab->tls_sec->vma + TP_OFFSET;
+         addend -= htab->elf.tls_sec->vma + TP_OFFSET;
          /* The TPREL16 relocs shouldn't really be used in shared
             libs as they will result in DT_TEXTREL being set, but
             support them anyway.  */
          goto dodyn;
 
        case R_PPC_TPREL32:
-         addend -= htab->tls_sec->vma + TP_OFFSET;
+         addend -= htab->elf.tls_sec->vma + TP_OFFSET;
          goto dodyn;
 
        case R_PPC_DTPREL32:
-         addend -= htab->tls_sec->vma + DTP_OFFSET;
+         addend -= htab->elf.tls_sec->vma + DTP_OFFSET;
          goto dodyn;
 
        case R_PPC_DTPMOD32:
@@ -5175,9 +5288,8 @@ ppc_elf_relocate_section (bfd *output_bfd,
        case R_PPC_REL14_BRNTAKEN:
          /* If these relocations are not to a named symbol, they can be
             handled right here, no need to bother the dynamic linker.  */
-         if (h == NULL
-             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0
-             || SYMBOL_REFERENCES_LOCAL (info, h))
+         if (SYMBOL_REFERENCES_LOCAL (info, h)
+             || strcmp (h->root.root.string, "_GLOBAL_OFFSET_TABLE_") == 0)
            break;
          /* fall through */
 
@@ -5207,8 +5319,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
                   || ELF_ST_VISIBILITY (h->other) == STV_DEFAULT
                   || h->root.type != bfd_link_hash_undefweak)
               && (MUST_BE_DYN_RELOC (r_type)
-                  || (h != NULL
-                      && !SYMBOL_CALLS_LOCAL (info, h))))
+                  || !SYMBOL_CALLS_LOCAL (info, h)))
              || (ELIMINATE_COPY_RELOCS
                  && !info->shared
                  && (input_section->flags & SEC_ALLOC) != 0
@@ -5263,8 +5374,7 @@ ppc_elf_relocate_section (bfd *output_bfd,
 
              if (skip)
                memset (&outrel, 0, sizeof outrel);
-             else if (h != NULL
-                      && !SYMBOL_REFERENCES_LOCAL (info, h))
+             else if (!SYMBOL_REFERENCES_LOCAL (info, h))
                {
                  unresolved_reloc = FALSE;
                  outrel.r_info = ELF32_R_INFO (h->dynindx, r_type);
@@ -5331,6 +5441,34 @@ ppc_elf_relocate_section (bfd *output_bfd,
            }
          break;
 
+       case R_PPC_RELAX32PC:
+         relocation -= (input_section->output_section->vma
+                        + input_section->output_offset
+                        + rel->r_offset - 4);
+         /* Fall thru */
+       case R_PPC_RELAX32:
+         {
+           unsigned long t0;
+           unsigned long t1;
+
+           t0 = bfd_get_32 (output_bfd, contents + rel->r_offset);
+           t1 = bfd_get_32 (output_bfd, contents + rel->r_offset + 4);
+
+           /* We're clearing the bits for R_PPC_ADDR16_HA
+              and R_PPC_ADDR16_LO here.  */
+           t0 &= ~0xffff;
+           t1 &= ~0xffff;
+
+           /* t0 is HA, t1 is LO */
+           relocation += addend;
+           t0 |= ((relocation + 0x8000) >> 16) & 0xffff;
+           t1 |= relocation & 0xffff;
+
+           bfd_put_32 (output_bfd, t0, contents + rel->r_offset);
+           bfd_put_32 (output_bfd, t1, contents + rel->r_offset + 4);
+         }
+         continue;
+
          /* Indirect .sdata relocation.  */
        case R_PPC_EMB_SDAI16:
          BFD_ASSERT (htab->sdata != NULL);
@@ -5681,7 +5819,7 @@ static bfd_boolean
 ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
 {
   int offset;
-  unsigned int raw_size;
+  unsigned int size;
 
   switch (note->descsz)
     {
@@ -5697,14 +5835,14 @@ ppc_elf_grok_prstatus (bfd *abfd, Elf_Internal_Note *note)
 
       /* pr_reg */
       offset = 72;
-      raw_size = 192;
+      size = 192;
 
       break;
     }
 
   /* Make a ".reg/999" section.  */
   return _bfd_elfcore_make_pseudosection (abfd, ".reg",
-                                         raw_size, note->descpos + offset);
+                                         size, note->descpos + offset);
 }
 
 static bfd_boolean
@@ -5850,7 +5988,7 @@ ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
       if (asec)
        {
          ++ num_input_sections;
-         output_section_size += asec->_raw_size;
+         output_section_size += asec->size;
        }
     }
 
@@ -5882,7 +6020,7 @@ ppc_elf_begin_write_processing (bfd *abfd, struct bfd_link_info *link_info)
       if (asec == NULL)
        continue;
 
-      length = asec->_raw_size;
+      length = asec->size;
       if (length < 24)
        {
          error_message = _("corrupt or empty %s section in %s");
@@ -5983,7 +6121,7 @@ ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
   if (apuinfo_list_length () == 0)
     return;
 
-  length = asec->_raw_size;
+  length = asec->size;
   if (length < 20)
     return;
 
@@ -6009,7 +6147,7 @@ ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
       length += 4;
     }
 
-  if (length != asec->_raw_size)
+  if (length != asec->size)
     (*_bfd_error_handler) (_("failed to compute new APUinfo section."));
 
   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
@@ -6019,6 +6157,36 @@ ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
 
   apuinfo_list_finish ();
 }
+
+/* Return address for Ith PLT stub in section PLT, for relocation REL
+   or (bfd_vma) -1 if it should not be included.  */
+
+static bfd_vma
+ppc_elf_plt_sym_val (bfd_vma i ATTRIBUTE_UNUSED,
+                    const asection *plt ATTRIBUTE_UNUSED,
+                    const arelent *rel)
+{
+  return rel->address;
+}
+
+/* Add extra PPC sections -- Note, for now, make .sbss2 and
+   .PPC.EMB.sbss0 a normal section, and not a bss section so
+   that the linker doesn't crater when trying to make more than
+   2 sections.  */
+
+static struct bfd_elf_special_section const ppc_elf_special_sections[]=
+{
+  { ".tags",             5,  0, SHT_ORDERED,  SHF_ALLOC },
+  { ".sdata",            6, -2, SHT_PROGBITS, SHF_ALLOC + SHF_WRITE },
+  { ".sbss",             5, -2, SHT_NOBITS,   SHF_ALLOC + SHF_WRITE },
+  { ".sdata2",           7, -2, SHT_PROGBITS, SHF_ALLOC },
+  { ".sbss2",            6, -2, SHT_PROGBITS, SHF_ALLOC },
+  { ".PPC.EMB.apuinfo", 16,  0, SHT_NOTE,     0 },
+  { ".PPC.EMB.sdata0",  15,  0, SHT_PROGBITS, SHF_ALLOC },
+  { ".PPC.EMB.sbss0",   14,  0, SHT_PROGBITS, SHF_ALLOC },
+  { ".plt",              4,  0, SHT_NOBITS,   SHF_ALLOC + SHF_EXECINSTR },
+  { NULL,                0,  0, 0,            0 }
+};
 \f
 #define TARGET_LITTLE_SYM      bfd_elf32_powerpcle_vec
 #define TARGET_LITTLE_NAME     "elf32-powerpcle"
@@ -6046,7 +6214,6 @@ ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
 #define elf_backend_can_gc_sections    1
 #define elf_backend_can_refcount       1
 #define elf_backend_got_header_size    12
-#define elf_backend_plt_header_size    PLT_INITIAL_ENTRY_SIZE
 #define elf_backend_rela_normal                1
 
 #define bfd_elf32_mkobject                     ppc_elf_mkobject
@@ -6078,5 +6245,7 @@ ppc_elf_final_write_processing (bfd *abfd, bfd_boolean linker ATTRIBUTE_UNUSED)
 #define elf_backend_begin_write_processing     ppc_elf_begin_write_processing
 #define elf_backend_final_write_processing     ppc_elf_final_write_processing
 #define elf_backend_write_section              ppc_elf_write_section
+#define elf_backend_special_sections           ppc_elf_special_sections
+#define elf_backend_plt_sym_val                        ppc_elf_plt_sym_val
 
 #include "elf32-target.h"
This page took 0.055293 seconds and 4 git commands to generate.