Fix the behaviour of --allow-shlib-undefined, so that it does what it claims
[deliverable/binutils-gdb.git] / bfd / elf32-ppc.c
index c117f54b2009a6ca75431d9d2adc341ffd7c9a57..9a6881bf51e917c2ceb057a4c7a17c3b3ac13702 100644 (file)
@@ -1,23 +1,23 @@
 /* PowerPC-specific support for 32-bit ELF
-   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002
+   Copyright 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
    Free Software Foundation, Inc.
    Written by Ian Lance Taylor, Cygnus Support.
 
-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 2 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 /* This file is based on a preliminary PowerPC ELF ABI.  The
    information may not match the final PowerPC ELF ABI.  It includes
@@ -33,6 +33,14 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
 
 /* RELA relocations are used here.  */
 
+static struct bfd_hash_entry *ppc_elf_link_hash_newfunc
+  PARAMS ((struct bfd_hash_entry *entry, struct bfd_hash_table *table,
+          const char *string));
+static struct bfd_link_hash_table *ppc_elf_link_hash_table_create
+  PARAMS ((bfd *abfd));
+static void ppc_elf_copy_indirect_symbol
+  PARAMS ((struct elf_backend_data *bed, struct elf_link_hash_entry *dir,
+          struct elf_link_hash_entry *ind));
 static reloc_howto_type *ppc_elf_reloc_type_lookup
   PARAMS ((bfd *abfd, bfd_reloc_code_real_type code));
 static void ppc_elf_info_to_howto
@@ -77,6 +85,10 @@ static bfd_boolean ppc_elf_gc_sweep_hook
           const Elf_Internal_Rela *relocs));
 static bfd_boolean ppc_elf_adjust_dynamic_symbol
   PARAMS ((struct bfd_link_info *, struct elf_link_hash_entry *));
+static bfd_boolean allocate_dynrelocs
+  PARAMS ((struct elf_link_hash_entry *, PTR));
+static bfd_boolean readonly_dynrelocs
+  PARAMS ((struct elf_link_hash_entry *, PTR));
 static bfd_boolean ppc_elf_size_dynamic_sections
   PARAMS ((bfd *, struct bfd_link_info *));
 static bfd_boolean ppc_elf_relocate_section
@@ -98,9 +110,9 @@ static bfd_boolean ppc_elf_grok_prstatus
 static bfd_boolean ppc_elf_grok_psinfo
   PARAMS ((bfd *abfd, Elf_Internal_Note *note));
 
-#define BRANCH_PREDICT_BIT 0x200000            /* branch prediction bit for branch taken relocs */
-#define RA_REGISTER_MASK 0x001f0000            /* mask to set RA in memory instructions */
-#define RA_REGISTER_SHIFT 16                   /* value to shift register by to insert RA */
+#define BRANCH_PREDICT_BIT 0x200000            /* Branch prediction bit for branch taken relocs.  */
+#define RA_REGISTER_MASK 0x001f0000            /* Mask to set RA in memory instructions.  */
+#define RA_REGISTER_SHIFT 16                   /* Value to shift register by to insert RA.  */
 
 /* The name of the dynamic interpreter.  This is put in the .interp
    section.  */
@@ -134,6 +146,149 @@ static bfd_boolean ppc_elf_grok_psinfo
     || ELF_ST_VISIBILITY (H->other) != STV_DEFAULT)                    \
    && (H->elf_link_hash_flags & ELF_LINK_HASH_DEF_REGULAR) != 0)
 \f
+/* The PPC linker needs to keep track of the number of relocs that it
+   decides to copy as dynamic relocs in check_relocs for each symbol.
+   This is so that it can later discard them if they are found to be
+   unnecessary.  We store the information in a field extending the
+   regular ELF linker hash table.  */
+
+struct ppc_elf_dyn_relocs
+{
+  struct ppc_elf_dyn_relocs *next;
+
+  /* The input section of the reloc.  */
+  asection *sec;
+
+  /* Total number of relocs copied for the input section.  */
+  bfd_size_type count;
+};
+
+/* PPC ELF linker hash entry.  */
+
+struct ppc_elf_link_hash_entry
+{
+  struct elf_link_hash_entry root;
+
+  /* Track dynamic relocs copied for this symbol.  */
+  struct ppc_elf_dyn_relocs *dyn_relocs;
+};
+
+#define ppc_elf_hash_entry(ent) ((struct ppc_elf_link_hash_entry *) (ent))
+
+/* PPC ELF linker hash table.  */
+
+struct ppc_elf_link_hash_table
+{
+  struct elf_link_hash_table root;
+
+  /* Small local sym to section mapping cache.  */
+  struct sym_sec_cache sym_sec;
+};
+
+/* Get the PPC ELF linker hash table from a link_info structure.  */
+
+#define ppc_elf_hash_table(p) \
+  ((struct ppc_elf_link_hash_table *) (p)->hash)
+
+/* Create an entry in a PPC ELF linker hash table.  */
+
+static struct bfd_hash_entry *
+ppc_elf_link_hash_newfunc (entry, table, string)
+     struct bfd_hash_entry *entry;
+     struct bfd_hash_table *table;
+     const char *string;
+{
+  /* Allocate the structure if it has not already been allocated by a
+     subclass.  */
+  if (entry == NULL)
+    {
+      entry = bfd_hash_allocate (table,
+                                sizeof (struct ppc_elf_link_hash_entry));
+      if (entry == NULL)
+       return entry;
+    }
+
+  /* Call the allocation method of the superclass.  */
+  entry = _bfd_elf_link_hash_newfunc (entry, table, string);
+  if (entry != NULL)
+    ppc_elf_hash_entry (entry)->dyn_relocs = NULL;
+
+  return entry;
+}
+
+/* Create a PPC ELF linker hash table.  */
+
+static struct bfd_link_hash_table *
+ppc_elf_link_hash_table_create (abfd)
+     bfd *abfd;
+{
+  struct ppc_elf_link_hash_table *ret;
+
+  ret = ((struct ppc_elf_link_hash_table *)
+        bfd_malloc (sizeof (struct ppc_elf_link_hash_table)));
+  if (ret == NULL)
+    return NULL;
+
+  if (! _bfd_elf_link_hash_table_init (&ret->root, abfd,
+                                      ppc_elf_link_hash_newfunc))
+    {
+      free (ret);
+      return NULL;
+    }
+
+  ret->sym_sec.abfd = NULL;
+
+  return &ret->root.root;
+}
+
+/* Copy the extra info we tack onto an elf_link_hash_entry.  */
+
+static void
+ppc_elf_copy_indirect_symbol (bed, dir, ind)
+     struct elf_backend_data *bed;
+     struct elf_link_hash_entry *dir, *ind;
+{
+  struct ppc_elf_link_hash_entry *edir, *eind;
+
+  edir = (struct ppc_elf_link_hash_entry *) dir;
+  eind = (struct ppc_elf_link_hash_entry *) ind;
+
+  if (eind->dyn_relocs != NULL)
+    {
+      if (edir->dyn_relocs != NULL)
+       {
+         struct ppc_elf_dyn_relocs **pp;
+         struct ppc_elf_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_elf_dyn_relocs *q;
+
+             for (q = edir->dyn_relocs; q != NULL; q = q->next)
+               if (q->sec == p->sec)
+                 {
+                   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;
+    }
+
+  _bfd_elf_link_hash_copy_indirect (bed, dir, ind);
+}
+\f
 static reloc_howto_type *ppc_elf_howto_table[(int) R_PPC_max];
 
 static reloc_howto_type ppc_elf_howto_raw[] = {
@@ -726,7 +881,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
   /* The remaining relocs are from the Embedded ELF ABI, and are not
      in the SVR4 ELF ABI.  */
 
-  /* 32 bit value resulting from the addend minus the symbol */
+  /* 32 bit value resulting from the addend minus the symbol */
   HOWTO (R_PPC_EMB_NADDR32,    /* type */
         0,                     /* rightshift */
         2,                     /* size (0 = byte, 1 = short, 2 = long) */
@@ -741,7 +896,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0xffffffff,            /* dst_mask */
         FALSE),                /* pcrel_offset */
 
-  /* 16 bit value resulting from the addend minus the symbol */
+  /* 16 bit value resulting from the addend minus the symbol */
   HOWTO (R_PPC_EMB_NADDR16,    /* type */
         0,                     /* rightshift */
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
@@ -756,7 +911,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0xffff,                /* dst_mask */
         FALSE),                /* pcrel_offset */
 
-  /* 16 bit value resulting from the addend minus the symbol */
+  /* 16 bit value resulting from the addend minus the symbol */
   HOWTO (R_PPC_EMB_NADDR16_LO, /* type */
         0,                     /* rightshift */
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
@@ -771,7 +926,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0xffff,                /* dst_mask */
         FALSE),                /* pcrel_offset */
 
-  /* The high order 16 bits of the addend minus the symbol */
+  /* The high order 16 bits of the addend minus the symbol */
   HOWTO (R_PPC_EMB_NADDR16_HI, /* type */
         16,                    /* rightshift */
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
@@ -805,7 +960,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
 
   /* 16 bit value resulting from allocating a 4 byte word to hold an
      address in the .sdata section, and returning the offset from
-     _SDA_BASE_ for that relocation */
+     _SDA_BASE_ for that relocation */
   HOWTO (R_PPC_EMB_SDAI16,     /* type */
         0,                     /* rightshift */
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
@@ -894,7 +1049,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0xffff,                /* dst_mask */
         FALSE),                /* pcrel_offset */
 
-  /* GNU extension to record C++ vtable hierarchy */
+  /* GNU extension to record C++ vtable hierarchy */
   HOWTO (R_PPC_GNU_VTINHERIT,  /* type */
         0,                     /* rightshift */
         0,                     /* size (0 = byte, 1 = short, 2 = long) */
@@ -909,7 +1064,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0,                     /* dst_mask */
         FALSE),                /* pcrel_offset */
 
-  /* GNU extension to record C++ vtable member usage */
+  /* GNU extension to record C++ vtable member usage */
   HOWTO (R_PPC_GNU_VTENTRY,    /* type */
         0,                     /* rightshift */
         0,                     /* size (0 = byte, 1 = short, 2 = long) */
@@ -924,7 +1079,7 @@ static reloc_howto_type ppc_elf_howto_raw[] = {
         0,                     /* dst_mask */
         FALSE),                /* pcrel_offset */
 
-  /* Phony reloc to handle AIX style TOC entries */
+  /* Phony reloc to handle AIX style TOC entries */
   HOWTO (R_PPC_TOC16,          /* type */
         0,                     /* rightshift */
         1,                     /* size (0 = byte, 1 = short, 2 = long) */
@@ -958,7 +1113,7 @@ ppc_elf_howto_init ()
 /* 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
+   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.
@@ -974,8 +1129,7 @@ ppc_elf_howto_init ()
    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).
-*/
+      (ensure it is predicted taken).  */
 
 /* Sort sections by address.  */
 
@@ -1078,9 +1232,9 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
               The array rela_comb is built here for use in the EOP scan loop.  */
               switch (r_type)
                 {
-                case R_PPC_ADDR14_BRNTAKEN:     /* absolute, predicted not taken */
-                case R_PPC_REL14:               /* relative cond. br.  */
-                case R_PPC_REL14_BRNTAKEN:      /* rel. cond. br., predicted not taken */
+                case R_PPC_ADDR14_BRNTAKEN:     /* Absolute, predicted not taken */
+                case R_PPC_REL14:               /* Relative cond. br.  */
+                case R_PPC_REL14_BRNTAKEN:      /* Rel. cond. br., predicted not taken */
                   /* We should check the instruction.  */
                   break;
                 default:
@@ -1113,7 +1267,7 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
               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.  */
+                the relocation indicates the word is not a conditional branch.  */
               skip = FALSE;
               isec_offset = dot - isec->vma;
               for (; comb_curr<comb_count; ++comb_curr)
@@ -1142,7 +1296,7 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
                     {
                       bfd_vma target;
                       /* This branch is predicted as "normal".
-                      If this is a forward branch, it is problematic.  */
+                        If this is a forward branch, it is problematic.  */
 
                       target = insn & 0x0000Fffc;               /*extract*/
                       target = (target ^ 0x8000) - 0x8000;      /*sign extend*/
@@ -1150,21 +1304,21 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
                           target += dot;                        /*convert to abs*/
                       if (target > dot)
                         {
-                          insn |= 0x00200000;   /* set the prediction bit */
+                          insn |= 0x00200000;   /* Set the prediction bit.  */
                           modified = TRUE;
                         }
                     }
                 }
               else if ((insn & 0xFc00Fffe) == 0x4c000420)
                 {
-                  /* Instruction is BCCTRx */
+                  /* 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;   /* set the prediction bit */
+                        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;   /* Set the prediction bit.  */
                       modified = TRUE;
                    }
                 }
@@ -1174,10 +1328,10 @@ ppc_elf_relax_section (abfd, isec, link_info, again)
                   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;   /* set the prediction bit */
+                        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;   /* Set the prediction bit.  */
                       modified = TRUE;
                    }
                 }
@@ -1399,7 +1553,8 @@ ppc_elf_set_private_flags (abfd, flags)
 }
 
 /* Merge backend specific data from an object file to the output
-   object file when linking */
+   object file when linking.  */
+
 static bfd_boolean
 ppc_elf_merge_private_bfd_data (ibfd, obfd)
      bfd *ibfd;
@@ -1409,7 +1564,7 @@ ppc_elf_merge_private_bfd_data (ibfd, obfd)
   flagword new_flags;
   bfd_boolean error;
 
-  /* Check if we have the same endianess */
+  /* Check if we have the same endianess */
   if (! _bfd_generic_verify_endian_match (ibfd, obfd))
     return FALSE;
 
@@ -1419,16 +1574,16 @@ ppc_elf_merge_private_bfd_data (ibfd, obfd)
 
   new_flags = elf_elfheader (ibfd)->e_flags;
   old_flags = elf_elfheader (obfd)->e_flags;
-  if (!elf_flags_init (obfd))  /* First call, no flags set */
+  if (!elf_flags_init (obfd))  /* First call, no flags set */
     {
       elf_flags_init (obfd) = TRUE;
       elf_elfheader (obfd)->e_flags = new_flags;
     }
 
-  else if (new_flags == old_flags)     /* Compatible flags are ok */
+  else if (new_flags == old_flags)     /* Compatible flags are ok */
     ;
 
-  else                                 /* Incompatible flags */
+  else                                 /* Incompatible flags */
     {
       /* Warn about -mrelocatable mismatch.  Allow -mrelocatable-lib to be linked
          with either.  */
@@ -1461,13 +1616,13 @@ ppc_elf_merge_private_bfd_data (ibfd, obfd)
          && (old_flags & (EF_PPC_RELOCATABLE_LIB | EF_PPC_RELOCATABLE)))
        elf_elfheader (obfd)->e_flags |= EF_PPC_RELOCATABLE;
 
-      /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it */
+      /* Do not warn about eabi vs. V.4 mismatch, just or in the bit if any module uses it */
       elf_elfheader (obfd)->e_flags |= (new_flags & EF_PPC_EMB);
 
       new_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
       old_flags &= ~ (EF_PPC_RELOCATABLE | EF_PPC_RELOCATABLE_LIB | EF_PPC_EMB);
 
-      /* Warn about any other mismatches */
+      /* Warn about any other mismatches */
       if (new_flags != old_flags)
        {
          error = TRUE;
@@ -1540,11 +1695,11 @@ ppc_elf_create_linker_section (abfd, info, which)
   bfd *dynobj = elf_hash_table (info)->dynobj;
   elf_linker_section_t *lsect;
 
-  /* Record the first bfd section that needs the special section */
+  /* Record the first bfd section that needs the special section */
   if (!dynobj)
     dynobj = elf_hash_table (info)->dynobj = abfd;
 
-  /* If this is the first time, create the section */
+  /* If this is the first time, create the section */
   lsect = elf_linker_section (dynobj, which);
   if (!lsect)
     {
@@ -1903,6 +2058,65 @@ ppc_elf_adjust_dynamic_symbol (info, h)
   return TRUE;
 }
 \f
+/* Allocate space in associated reloc sections for dynamic relocs.  */
+
+static bfd_boolean
+allocate_dynrelocs (h, info)
+     struct elf_link_hash_entry *h;
+     PTR info ATTRIBUTE_UNUSED;
+{
+  struct ppc_elf_dyn_relocs *p;
+
+  if (h->root.type == bfd_link_hash_indirect)
+    return TRUE;
+
+  if (h->root.type == bfd_link_hash_warning)
+    /* When warning symbols are created, they **replace** the "real"
+       entry in the hash table, thus we never get to see the real
+       symbol in a hash traversal.  So look at it now.  */
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+  for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
+    {
+      asection *sreloc = elf_section_data (p->sec)->sreloc;
+      sreloc->_raw_size += p->count * sizeof (Elf32_External_Rela);
+    }
+
+  return TRUE;
+}
+
+/* Find any dynamic relocs that apply to read-only sections.  */
+
+static bfd_boolean
+readonly_dynrelocs (h, info)
+     struct elf_link_hash_entry *h;
+     PTR info;
+{
+  struct ppc_elf_dyn_relocs *p;
+
+  if (h->root.type == bfd_link_hash_indirect)
+    return TRUE;
+
+  if (h->root.type == bfd_link_hash_warning)
+    h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+  for (p = ppc_elf_hash_entry (h)->dyn_relocs; p != NULL; p = p->next)
+    {
+      asection *s = p->sec->output_section;
+
+      if (s != NULL
+         && ((s->flags & (SEC_READONLY | SEC_ALLOC))
+             == (SEC_READONLY | SEC_ALLOC)))
+       {
+         ((struct bfd_link_info *) info)->flags |= DF_TEXTREL;
+
+         /* Not an error, just cut short the traversal.  */
+         return FALSE;
+       }
+    }
+  return TRUE;
+}
+
 /* Set the sizes of the dynamic sections.  */
 
 static bfd_boolean
@@ -1914,6 +2128,7 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
   asection *s;
   bfd_boolean plt;
   bfd_boolean relocs;
+  bfd *ibfd;
 
 #ifdef DEBUG
   fprintf (stderr, "ppc_elf_size_dynamic_sections called\n");
@@ -1953,6 +2168,45 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
        }
     }
 
+  /* Allocate space for local sym dynamic relocs.  */
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
+    {
+      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
+       continue;
+
+      for (s = ibfd->sections; s != NULL; s = s->next)
+       {
+         struct ppc_elf_dyn_relocs *p;
+
+         for (p = ((struct ppc_elf_dyn_relocs *)
+                  elf_section_data (s)->local_dynrel);
+             p != NULL;
+             p = p->next)
+           {
+             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 if (p->count != 0)
+               {
+                 elf_section_data (p->sec)->sreloc->_raw_size
+                   += p->count * sizeof (Elf32_External_Rela);
+                 if ((p->sec->output_section->flags
+                      & (SEC_READONLY | SEC_ALLOC))
+                     == (SEC_READONLY | SEC_ALLOC))
+                   info->flags |= DF_TEXTREL;
+               }
+           }
+       }
+    }
+
+  /* Allocate space for global sym dynamic relocs.  */
+  elf_link_hash_traverse (elf_hash_table (info), allocate_dynrelocs, NULL);
+
   /* The check_relocs and adjust_dynamic_symbol entry points have
      determined the sizes of the various dynamic sections.  Allocate
      memory for them.  */
@@ -2064,11 +2318,16 @@ ppc_elf_size_dynamic_sections (output_bfd, info)
            return FALSE;
        }
 
+      /* If any dynamic relocs apply to a read-only section, then we
+        need a DT_TEXTREL entry.  */
+      if ((info->flags & DF_TEXTREL) == 0)
+       elf_link_hash_traverse (elf_hash_table (info), readonly_dynrelocs,
+                               (PTR) info);
+
       if ((info->flags & DF_TEXTREL) != 0)
        {
          if (!add_dynamic_entry (DT_TEXTREL, 0))
            return FALSE;
-         info->flags |= DF_TEXTREL;
        }
     }
 #undef add_dynamic_entry
@@ -2246,7 +2505,7 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
            }
          break;
 
-       /* Indirect .sdata relocation */
+       /* Indirect .sdata relocation */
        case R_PPC_EMB_SDAI16:
          if (info->shared)
            {
@@ -2280,7 +2539,7 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
 
          break;
 
-       /* Indirect .sdata2 relocation */
+       /* Indirect .sdata2 relocation */
        case R_PPC_EMB_SDA2I16:
          if (info->shared)
            {
@@ -2368,7 +2627,7 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
        case R_PPC_SECTOFF_HA:
          break;
 
-         /* This refers only to functions defined in the shared library */
+         /* This refers only to functions defined in the shared library */
        case R_PPC_LOCAL24PC:
          break;
 
@@ -2403,6 +2662,9 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
        default:
          if (info->shared)
            {
+             struct ppc_elf_dyn_relocs *p;
+             struct ppc_elf_dyn_relocs **head;
+
 #ifdef DEBUG
              fprintf (stderr, "ppc_elf_check_relocs need to create relocation for %s\n",
                       (h && h->root.root.string) ? h->root.root.string : "<unknown>");
@@ -2437,18 +2699,46 @@ ppc_elf_check_relocs (abfd, info, sec, relocs)
                          || ! bfd_set_section_alignment (dynobj, sreloc, 2))
                        return FALSE;
                    }
-                 if (sec->flags & SEC_READONLY)
-                   info->flags |= DF_TEXTREL;
+                 elf_section_data (sec)->sreloc = sreloc;
                }
 
-             sreloc->_raw_size += sizeof (Elf32_External_Rela);
+             /* If this is a global symbol, we count the number of
+                relocations we need for this symbol.  */
+             if (h != NULL)
+               {
+                 head = &ppc_elf_hash_entry (h)->dyn_relocs;
+               }
+             else
+               {
+                 /* 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, &ppc_elf_hash_table (info)->sym_sec,
+                       sec, r_symndx));
+                 if (s == NULL)
+                   return FALSE;
+
+                 head = ((struct ppc_elf_dyn_relocs **)
+                         &elf_section_data (s)->local_dynrel);
+               }
+
+             p = *head;
+             if (p == NULL || p->sec != sec)
+               {
+                 p = ((struct ppc_elf_dyn_relocs *)
+                      bfd_alloc (elf_hash_table (info)->dynobj, sizeof *p));
+                 if (p == NULL)
+                   return FALSE;
+                 p->next = *head;
+                 *head = p;
+                 p->sec = sec;
+                 p->count = 0;
+               }
 
-             /* FIXME: We should here do what the m68k and i386
-                backends do: if the reloc is pc-relative, record it
-                in case it turns out that the reloc is unnecessary
-                because the symbol is forced local by versioning or
-                we are linking with -Bdynamic.  Fortunately this
-                case is not frequent.  */
+             p->count++;
            }
 
          break;
@@ -2514,6 +2804,8 @@ ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
   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);
   local_got_refcounts = elf_local_got_refcounts (abfd);
@@ -2552,9 +2844,26 @@ ppc_elf_gc_sweep_hook (abfd, info, sec, relocs)
            if (h->plt.refcount > 0)
              h->plt.refcount--;
          }
-       break;
+       /* Fall through */
 
       default:
+       r_symndx = ELF32_R_SYM (rel->r_info);
+       if (r_symndx >= symtab_hdr->sh_info)
+         {
+           struct ppc_elf_dyn_relocs **pp, *p;
+
+           h = sym_hashes[r_symndx - symtab_hdr->sh_info];
+
+           for (pp = &ppc_elf_hash_entry (h)->dyn_relocs;
+                (p = *pp) != NULL;
+                pp = &p->next)
+             if (p->sec == sec)
+               {
+                 if (--p->count == 0)
+                   *pp = p->next;
+                 break;
+               }
+         }
        break;
       }
 
@@ -3087,7 +3396,6 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          else if (h->root.type == bfd_link_hash_undefweak)
            relocation = 0;
          else if (info->shared
-                  && (!info->symbolic || info->allow_shlib_undefined)
                   && !info->no_undefined
                   && ELF_ST_VISIBILITY (h->other) == STV_DEFAULT)
            relocation = 0;
@@ -3178,7 +3486,6 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
              /* When generating a shared object, these relocations
                  are copied into the output file to be resolved at run
                  time.  */
-
              if (sreloc == NULL)
                {
                  const char *name;
@@ -3221,11 +3528,10 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                }
              else
                {
+                 outrel.r_addend = relocation + rel->r_addend;
+
                  if (r_type == R_PPC_ADDR32)
-                   {
-                     outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
-                     outrel.r_addend = relocation + rel->r_addend;
-                   }
+                   outrel.r_info = ELF32_R_INFO (0, R_PPC_RELATIVE);
                  else
                    {
                      long indx;
@@ -3239,7 +3545,8 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                                          == bfd_link_hash_defweak));
                          sec = h->root.u.def.section;
                        }
-                     if (sec != NULL && bfd_is_abs_section (sec))
+
+                     if (bfd_is_abs_section (sec))
                        indx = 0;
                      else if (sec == NULL || sec->owner == NULL)
                        {
@@ -3250,6 +3557,11 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                        {
                          asection *osec;
 
+                         /* We are turning this relocation into one
+                            against a section symbol.  It would be
+                            proper to subtract the symbol's value,
+                            osec->vma, from the emitted reloc addend,
+                            but ld.so expects buggy relocs.  */
                          osec = sec->output_section;
                          indx = elf_section_data (osec)->dynindx;
                          BFD_ASSERT (indx > 0);
@@ -3264,7 +3576,6 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                        }
 
                      outrel.r_info = ELF32_R_INFO (indx, r_type);
-                     outrel.r_addend = relocation + rel->r_addend;
                    }
                }
 
@@ -3300,7 +3611,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
            }
          break;
 
-       /* branch taken prediction relocations */
+       /* Branch taken prediction relocations.  */
        case (int) R_PPC_ADDR14_BRTAKEN:
        case (int) R_PPC_REL14_BRTAKEN:
          insn = bfd_get_32 (output_bfd, contents + offset);
@@ -3311,7 +3622,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
          break;
 
-       /* branch not taken predicition relocations */
+       /* Branch not taken predicition relocations.  */
        case (int) R_PPC_ADDR14_BRNTAKEN:
        case (int) R_PPC_REL14_BRNTAKEN:
          insn = bfd_get_32 (output_bfd, contents + offset);
@@ -3322,7 +3633,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          bfd_put_32 (output_bfd, (bfd_vma) insn, contents + offset);
          break;
 
-       /* GOT16 relocations */
+       /* GOT16 relocations */
        case (int) R_PPC_GOT16:
        case (int) R_PPC_GOT16_LO:
        case (int) R_PPC_GOT16_HI:
@@ -3410,9 +3721,11 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
 
              relocation = sgot->output_offset + off - 4;
            }
+         if (r_type == R_PPC_GOT16_HA)
+           addend += ((relocation + addend) & 0x8000) << 1;
          break;
 
-       /* Indirect .sdata relocation */
+       /* Indirect .sdata relocation */
        case (int) R_PPC_EMB_SDAI16:
          BFD_ASSERT (sdata != NULL);
          relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
@@ -3420,7 +3733,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                                                                R_PPC_RELATIVE);
          break;
 
-       /* Indirect .sdata2 relocation */
+       /* Indirect .sdata2 relocation */
        case (int) R_PPC_EMB_SDA2I16:
          BFD_ASSERT (sdata2 != NULL);
          relocation = bfd_elf32_finish_pointer_linker_section (output_bfd, input_bfd, info,
@@ -3460,7 +3773,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
                        + h->plt.offset);
          break;
 
-       /* relocate against _SDA_BASE_ */
+       /* Relocate against _SDA_BASE_.  */
        case (int) R_PPC_SDAREL16:
          {
            const char *name;
@@ -3510,7 +3823,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          }
          break;
 
-       /* relocate against either _SDA_BASE_, _SDA2_BASE_, or 0 */
+       /* Relocate against either _SDA_BASE_, _SDA2_BASE_, or 0.  */
        case (int) R_PPC_EMB_SDA21:
        case (int) R_PPC_EMB_RELSDA:
          {
@@ -3567,7 +3880,7 @@ ppc_elf_relocate_section (output_bfd, info, input_bfd, input_section,
          }
          break;
 
-       /* Relocate against the beginning of the section */
+       /* Relocate against the beginning of the section */
        case (int) R_PPC_SECTOFF:
        case (int) R_PPC_SECTOFF_LO:
        case (int) R_PPC_SECTOFF_HI:
@@ -3718,7 +4031,8 @@ ppc_elf_reloc_type_class (rela)
     }
 }
 \f
-/* Support for core dump NOTE sections */
+/* Support for core dump NOTE sections.  */
+
 static bfd_boolean
 ppc_elf_grok_prstatus (abfd, note)
      bfd *abfd;
@@ -3732,7 +4046,7 @@ ppc_elf_grok_prstatus (abfd, note)
       default:
        return FALSE;
 
-      case 268:                /* Linux/PPC */
+      case 268:                /* Linux/PPC */
        /* pr_cursig */
        elf_tdata (abfd)->core_signal = bfd_get_16 (abfd, note->descdata + 12);
 
@@ -3761,7 +4075,7 @@ ppc_elf_grok_psinfo (abfd, note)
       default:
        return FALSE;
 
-      case 128:                /* Linux/PPC elf_prpsinfo */
+      case 128:                /* Linux/PPC elf_prpsinfo */
        elf_tdata (abfd)->core_program
         = _bfd_elfcore_strndup (abfd, note->descdata + 32, 16);
        elf_tdata (abfd)->core_command
@@ -3901,6 +4215,9 @@ ppc_elf_begin_write_processing (abfd, link_info)
   unsigned long length;
   const char *  error_message = NULL;
 
+  if (link_info == NULL)
+    return;
+
   /* Scan the input bfds, looking for apuinfo sections.  */
   num_input_sections = 0;
   output_section_size = 0;
@@ -3939,7 +4256,7 @@ ppc_elf_begin_write_processing (abfd, link_info)
       unsigned long    datum;
       char *           ptr;
 
-      
+
       asec = bfd_get_section_by_name (ibfd, APUINFO_SECTION_NAME);
       if (asec == NULL)
        continue;
@@ -3950,7 +4267,7 @@ ppc_elf_begin_write_processing (abfd, link_info)
          error_message = _("corrupt or empty %s section in %s");
          goto fail;
        }
-      
+
       if (bfd_seek (ibfd, asec->filepos, SEEK_SET) != 0
          || (bfd_bread (buffer + offset, length, ibfd) != length))
        {
@@ -4004,7 +4321,7 @@ ppc_elf_begin_write_processing (abfd, link_info)
   if (! bfd_set_section_size  (abfd, asec, output_section_size))
     ibfd = abfd,
       error_message = _("warning: unable to set size of %s section in %s");
-  
+
  fail:
   free (buffer);
 
@@ -4023,7 +4340,7 @@ ppc_elf_write_section (abfd, asec, contents)
      asection * asec;
      bfd_byte * contents ATTRIBUTE_UNUSED;
 {
-  return strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
+  return apuinfo_list_length () && strcmp (asec->name, APUINFO_SECTION_NAME) == 0;
 }
 
 
@@ -4044,6 +4361,9 @@ ppc_elf_final_write_processing (abfd, linker)
   if (asec == NULL)
     return;
 
+  if (apuinfo_list_length () == 0)
+    return;
+
   length = asec->_raw_size;
   if (length < 20)
     return;
@@ -4061,7 +4381,7 @@ ppc_elf_final_write_processing (abfd, linker)
   bfd_put_32 (abfd, num_entries, buffer + 4);
   bfd_put_32 (abfd, 0x2, buffer + 8);
   strcpy (buffer + 12, APUINFO_LABEL);
-  
+
   length = 20;
   for (i = 0; i < num_entries; i++)
     {
@@ -4071,10 +4391,10 @@ ppc_elf_final_write_processing (abfd, linker)
 
   if (length != asec->_raw_size)
     _bfd_error_handler (_("failed to compute new APUinfo section."));
-  
+
   if (! bfd_set_section_contents (abfd, asec, buffer, (file_ptr) 0, length))
     _bfd_error_handler (_("failed to install new APUinfo section."));
-    
+
   free (buffer);
 
   apuinfo_list_finish ();
@@ -4110,6 +4430,7 @@ ppc_elf_final_write_processing (abfd, linker)
 #define bfd_elf32_bfd_reloc_type_lookup                ppc_elf_reloc_type_lookup
 #define bfd_elf32_bfd_set_private_flags                ppc_elf_set_private_flags
 #define bfd_elf32_bfd_final_link               _bfd_elf32_gc_common_final_link
+#define bfd_elf32_bfd_link_hash_table_create   ppc_elf_link_hash_table_create
 
 #define elf_backend_object_p                   ppc_elf_object_p
 #define elf_backend_gc_mark_hook               ppc_elf_gc_mark_hook
@@ -4118,6 +4439,7 @@ ppc_elf_final_write_processing (abfd, linker)
 #define elf_backend_relocate_section           ppc_elf_relocate_section
 #define elf_backend_create_dynamic_sections    ppc_elf_create_dynamic_sections
 #define elf_backend_check_relocs               ppc_elf_check_relocs
+#define elf_backend_copy_indirect_symbol       ppc_elf_copy_indirect_symbol
 #define elf_backend_adjust_dynamic_symbol      ppc_elf_adjust_dynamic_symbol
 #define elf_backend_add_symbol_hook            ppc_elf_add_symbol_hook
 #define elf_backend_size_dynamic_sections      ppc_elf_size_dynamic_sections
This page took 0.038274 seconds and 4 git commands to generate.