* elf64-ppc.c (is_ppc64_target): New function, extracted from..
[deliverable/binutils-gdb.git] / bfd / elf64-ppc.c
index 91f4619efd7dc7ad38d93882c9af5b8e75b5bf2e..865460575360e087545dacf9d67f62b56c15cfb3 100644 (file)
@@ -2153,7 +2153,8 @@ ppc64_elf_branch_reloc (bfd *abfd, arelent *reloc_entry, asymbol *symbol,
     return bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
                                  input_section, output_bfd, error_message);
 
-  if (strcmp (symbol->section->name, ".opd") == 0)
+  if (strcmp (symbol->section->name, ".opd") == 0
+      && (symbol->section->owner->flags & DYNAMIC) == 0)
     {
       bfd_vma dest = opd_entry_value (symbol->section,
                                      symbol->value + reloc_entry->addend,
@@ -2399,6 +2400,15 @@ ppc64_elf_mkobject (bfd *abfd)
   return TRUE;
 }
 
+static bfd_boolean
+is_ppc64_target (const struct bfd_target *targ)
+{
+  extern const bfd_target bfd_elf64_powerpc_vec;
+  extern const bfd_target bfd_elf64_powerpcle_vec;
+
+  return targ == &bfd_elf64_powerpc_vec || targ == &bfd_elf64_powerpcle_vec;
+}
+
 /* Fix bad default arch selected for a 64 bit input bfd when the
    default is 32 bit.  */
 
@@ -2549,7 +2559,7 @@ get_opd_info (asection * sec)
 static asection *synthetic_opd;
 static bfd_boolean synthetic_relocatable;
 
-/* Helper routine for ppc64_elf_get_synthetic_symtab.  */
+/* qsort comparison function for ppc64_elf_get_synthetic_symtab.  */
 
 static int
 compare_symbols (const void *ap, const void *bp)
@@ -2557,16 +2567,19 @@ compare_symbols (const void *ap, const void *bp)
   const asymbol *a = * (const asymbol **) ap;
   const asymbol *b = * (const asymbol **) bp;
 
-  if ((a->flags & BSF_SECTION_SYM) == 0 && (b->flags & BSF_SECTION_SYM))
+  /* Section symbols first.  */
+  if ((a->flags & BSF_SECTION_SYM) && !(b->flags & BSF_SECTION_SYM))
     return -1;
-  if ((a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM) == 0)
+  if (!(a->flags & BSF_SECTION_SYM) && (b->flags & BSF_SECTION_SYM))
     return 1;
 
+  /* then .opd symbols.  */
   if (a->section == synthetic_opd && b->section != synthetic_opd)
     return -1;
   if (a->section != synthetic_opd && b->section == synthetic_opd)
     return 1;
 
+  /* then other code symbols.  */
   if ((a->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
       == (SEC_CODE | SEC_ALLOC)
       && (b->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
@@ -2597,38 +2610,63 @@ compare_symbols (const void *ap, const void *bp)
   return 0;
 }
 
-/* Helper routine for ppc64_elf_get_synthetic_symtab.  */
+/* Search SYMS for a symbol of the given VALUE.  */
 
-static int
-compare_relocs (const void *ap, const void *bp)
+static asymbol *
+sym_exists_at (asymbol **syms, long lo, long hi, int id, bfd_vma value)
 {
-  const arelent *a = * (const arelent **) ap;
-  const arelent *b = * (const arelent **) bp;
-
-  if (a->address < b->address)
-    return -1;
-
-  if (a->address > b->address)
-    return 1;
+  long mid;
 
-  return 0;
+  if (id == -1)
+    {
+      while (lo < hi)
+       {
+         mid = (lo + hi) >> 1;
+         if (syms[mid]->value + syms[mid]->section->vma < value)
+           lo = mid + 1;
+         else if (syms[mid]->value + syms[mid]->section->vma > value)
+           hi = mid;
+         else
+           return syms[mid];
+       }
+    }
+  else
+    {
+      while (lo < hi)
+       {
+         mid = (lo + hi) >> 1;
+         if (syms[mid]->section->id < id)
+           lo = mid + 1;
+         else if (syms[mid]->section->id > id)
+           hi = mid;
+         else if (syms[mid]->value < value)
+           lo = mid + 1;
+         else if (syms[mid]->value > value)
+           hi = mid;
+         else
+           return syms[mid];
+       }
+    }
+  return NULL;
 }
 
-/* Create synthetic symbols.  */
+/* Create synthetic symbols, effectively restoring "dot-symbol" function
+   entry syms.  */
 
 static long
-ppc64_elf_get_synthetic_symtab (bfd *abfd, asymbol **relsyms, asymbol **ret)
+ppc64_elf_get_synthetic_symtab (bfd *abfd,
+                               long static_count, asymbol **static_syms,
+                               long dyn_count, asymbol **dyn_syms,
+                               asymbol **ret)
 {
   asymbol *s;
-  bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
-  arelent **relocs, **r;
-  long count, i;
-  size_t size;
+  long i;
+  long count;
   char *names;
-  asymbol **syms = NULL;
-  long symcount = 0, opdsymcount, relcount;
-  asection *relopd, *opd;
+  long symcount, codesecsym, codesecsymend, secsymend, opdsymend;
+  asection *opd;
   bfd_boolean relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
+  asymbol **syms;
 
   *ret = NULL;
 
@@ -2636,297 +2674,257 @@ ppc64_elf_get_synthetic_symtab (bfd *abfd, asymbol **relsyms, asymbol **ret)
   if (opd == NULL)
     return 0;
 
-  if ((bfd_get_file_flags (abfd) & HAS_SYMS))
-    {
-      long storage;
-      storage = bfd_get_symtab_upper_bound (abfd);
-      if (storage < 0)
-       return 0;
-
-      if (storage)
-       {
-         syms = bfd_malloc (storage);
-         if (syms == NULL)
-           return 0;
-       }
-
-      symcount = bfd_canonicalize_symtab (abfd, syms);
-      if (symcount < 0)
-       {
-         free (syms);
-         return 0;
-       }
-
-      if (symcount == 0)
-       {
-         free (syms);
-         syms = NULL;
-       }
-    }
-
+  symcount = static_count;
+  if (!relocatable)
+    symcount += dyn_count;
   if (symcount == 0)
-    {
-      long storage;
-
-      storage = bfd_get_dynamic_symtab_upper_bound (abfd);
-      if (storage < 0)
-       return 0;
+    return 0;
 
-      if (storage)
-       {
-         syms = bfd_malloc (storage);
-         if (syms == NULL)
-           return 0;
-       }
+  syms = bfd_malloc ((symcount + 1) * sizeof (*syms));
+  if (syms == NULL)
+    return 0;
 
-      symcount = bfd_canonicalize_dynamic_symtab (abfd, syms);
-      if (symcount < 0)
-       {
-         free (syms);
-         return 0;
-       }
+  if (!relocatable && static_count != 0 && dyn_count != 0)
+    {
+      /* Use both symbol tables.  */
+      memcpy (syms, static_syms, static_count * sizeof (*syms));
+      memcpy (syms + static_count, dyn_syms, (dyn_count + 1) * sizeof (*syms));
     }
+  else if (!relocatable && static_count == 0)
+    memcpy (syms, dyn_syms, (symcount + 1) * sizeof (*syms));
+  else
+    memcpy (syms, static_syms, (symcount + 1) * sizeof (*syms));
 
   synthetic_opd = opd;
   synthetic_relocatable = relocatable;
-  qsort (syms, symcount, sizeof (asymbol *), compare_symbols);
+  qsort (syms, symcount, sizeof (*syms), compare_symbols);
 
-  opdsymcount = symcount;
-  for (i = 0; i < symcount; ++i)
+  if (!relocatable && symcount > 1)
     {
-      if (syms[i]->flags & BSF_SECTION_SYM)
-       {
-         if (opdsymcount == symcount)
-           opdsymcount = i;
-         symcount = i;
-         break;
-       }
+      long j;
+      /* Trim duplicate syms, since we may have merged the normal and
+        dynamic symbols.  Actually, we only care about syms that have
+        different values, so trim any with the same value.  */ 
+      for (i = 1, j = 1; i < symcount; ++i)
+       if (syms[i - 1]->value + syms[i - 1]->section->vma
+           != syms[i]->value + syms[i]->section->vma)
+         syms[j++] = syms[i];
+      symcount = j;
+    }
 
-      if (syms[i]->section == opd)
-       continue;
+  i = 0;
+  if (syms[i]->section == opd)
+    ++i;
+  codesecsym = i;
 
-      if (opdsymcount == symcount)
-       opdsymcount = i;
+  for (; i < symcount; ++i)
+    if (((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
+        != (SEC_CODE | SEC_ALLOC))
+       || (syms[i]->flags & BSF_SECTION_SYM) == 0)
+      break;
+  codesecsymend = i;
 
-      if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
-         != (SEC_CODE | SEC_ALLOC))
-       {
-         symcount = i;
-         break;
-       }
-    }
+  for (; i < symcount; ++i)
+    if ((syms[i]->flags & BSF_SECTION_SYM) == 0)
+      break;
+  secsymend = i;
 
-  if (opdsymcount == 0)
-    {
-      free (syms);
-      return 0;
-    }
+  for (; i < symcount; ++i)
+    if (syms[i]->section != opd)
+      break;
+  opdsymend = i;
 
-  slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
-  if (! relocatable)
-    {
-      relopd = bfd_get_section_by_name (abfd, ".rela.opd");
-      if (relopd == NULL)
-       {
-         relopd = bfd_get_section_by_name (abfd, ".rela.dyn");
-         if (relopd == NULL)
-           {
-             free (syms);
-             return 0;
-           }
-       }
-      relcount = relopd->size / 24;
+  for (; i < symcount; ++i)
+    if ((syms[i]->section->flags & (SEC_CODE | SEC_ALLOC | SEC_THREAD_LOCAL))
+       != (SEC_CODE | SEC_ALLOC))
+      break;
+  symcount = i;
 
-      if (! relcount
-         || ! (*slurp_relocs) (abfd, relopd, relsyms, TRUE))
-       {
-         free (syms);
-         return 0;
-       }
-    }
-  else
+  count = 0;
+  if (opdsymend == secsymend)
+    goto done;
+
+  if (relocatable)
     {
-      relopd = opd;
+      bfd_boolean (*slurp_relocs) (bfd *, asection *, asymbol **, bfd_boolean);
+      arelent *r;
+      size_t size;
+      long relcount;
+
+      slurp_relocs = get_elf_backend_data (abfd)->s->slurp_reloc_table;
       relcount = (opd->flags & SEC_RELOC) ? opd->reloc_count : 0;
 
       if (! relcount
-         || ! (*slurp_relocs) (abfd, relopd, relsyms, FALSE))
+         || ! (*slurp_relocs) (abfd, opd, static_syms, FALSE))
+       goto done;
+
+      size = 0;
+      for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
        {
-         free (syms);
-         return 0;
-       }
-    }
+         asymbol *sym;
 
-  relocs = bfd_malloc (relcount * sizeof (arelent **));
-  if (relocs == NULL)
-    {
-      free (syms);
-      return 0;
-    }
+         while (r < opd->relocation + relcount
+                && r->address < syms[i]->value + opd->vma)
+           ++r;
 
-  for (i = 0; i < relcount; ++i)
-    relocs[i] = &relopd->relocation[i];
+         if (r == opd->relocation + relcount)
+           break;
 
-  qsort (relocs, relcount, sizeof (*relocs), compare_relocs);
+         if (r->address != syms[i]->value + opd->vma)
+           continue;
 
-  size = 0;
-  count = 0;
-  for (i = 0, r = relocs; i < opdsymcount; ++i)
-    {
-      long lo, hi, mid;
-      asymbol *sym;
+         if (r->howto->type != R_PPC64_ADDR64)
+           continue;
 
-      while (r < relocs + relcount
-            && (*r)->address < syms[i]->value + opd->vma)
-       ++r;
+         sym = *r->sym_ptr_ptr;
+         if (!sym_exists_at (syms, opdsymend, symcount,
+                             sym->section->id, sym->value + r->addend))
+           {
+             ++count;
+             size += sizeof (asymbol);
+             size += strlen (syms[i]->name) + 2;
+           }
+       }
 
-      if (r == relocs + relcount)
-       continue;
+      s = *ret = bfd_malloc (size);
+      if (s == NULL)
+       {
+         count = 0;
+         goto done;
+       }
 
-      if ((*r)->address != syms[i]->value + opd->vma)
-       continue;
+      names = (char *) (s + count);
 
-      if ((*r)->howto->type != (relocatable
-                               ? R_PPC64_ADDR64 : R_PPC64_RELATIVE))
-       continue;
+      for (i = secsymend, r = opd->relocation; i < opdsymend; ++i)
+       {
+         asymbol *sym;
 
-      lo = opdsymcount;
-      hi = symcount;
-      sym = *((*r)->sym_ptr_ptr);
-      if (relocatable)
-       while (lo < hi)
-         {
-           mid = (lo + hi) >> 1;
-           if (syms[mid]->section->id < sym->section->id)
-             lo = mid + 1;
-           else if (syms[mid]->section->id > sym->section->id)
-             hi = mid;
-           else if (syms[mid]->value < sym->value + (*r)->addend)
-             lo = mid + 1;
-           else if (syms[mid]->value > sym->value + (*r)->addend)
-             hi = mid;
-           else
-             break;
-         }
-      else
-       while (lo < hi)
-         {
-           mid = (lo + hi) >> 1;
-           if (syms[mid]->value + syms[mid]->section->vma < (*r)->addend)
-             lo = mid + 1;
-           else if (syms[mid]->value + syms[mid]->section->vma > (*r)->addend)
-             hi = mid;
-           else
-             break;
-         }
+         while (r < opd->relocation + relcount
+                && r->address < syms[i]->value + opd->vma)
+           ++r;
 
-      if (lo >= hi)
-       {
-         ++count;
-         size += sizeof (asymbol);
-         size += strlen (syms[i]->name) + 1;
-       }
-    }
+         if (r == opd->relocation + relcount)
+           break;
 
-  s = *ret = bfd_malloc (size);
-  if (s == NULL)
-    {
-      free (syms);
-      free (relocs);
-      return 0;
-    }
+         if (r->address != syms[i]->value + opd->vma)
+           continue;
 
-  names = (char *) (s + count);
+         if (r->howto->type != R_PPC64_ADDR64)
+           continue;
 
-  for (i = 0, r = relocs; i < opdsymcount; ++i)
+         sym = *r->sym_ptr_ptr;
+         if (!sym_exists_at (syms, opdsymend, symcount,
+                             sym->section->id, sym->value + r->addend))
+           {
+             size_t len;
+
+             *s = *syms[i];
+             s->section = sym->section;
+             s->value = sym->value + r->addend;
+             s->name = names;
+             *names++ = '.';
+             len = strlen (syms[i]->name);
+             memcpy (names, syms[i]->name, len + 1);
+             names += len + 1;
+             s++;
+           }
+       }
+    }
+  else
     {
-      long lo, hi, mid;
-      asymbol *sym;
+      bfd_byte *contents;
+      size_t size;
 
-      while (r < relocs + relcount
-            && (*r)->address < syms[i]->value + opd->vma)
-       ++r;
+      if (!bfd_malloc_and_get_section (abfd, opd, &contents))
+       {
+         if (contents)
+           {
+           free_contents_and_exit:
+             free (contents);
+           }
+         goto done;
+       }
 
-      if (r == relocs + relcount)
-       continue;
+      size = 0;
+      for (i = secsymend; i < opdsymend; ++i)
+       {
+         bfd_vma ent;
 
-      if ((*r)->address != syms[i]->value + opd->vma)
-       continue;
+         ent = bfd_get_64 (abfd, contents + syms[i]->value);
+         if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
+           {
+             ++count;
+             size += sizeof (asymbol);
+             size += strlen (syms[i]->name) + 2;
+           }
+       }
 
-      if ((*r)->howto->type != (relocatable
-                               ? R_PPC64_ADDR64 : R_PPC64_RELATIVE))
-       continue;
+      s = *ret = bfd_malloc (size);
+      if (s == NULL)
+       {
+         count = 0;
+         goto free_contents_and_exit;
+       }
 
-      lo = opdsymcount;
-      hi = symcount;
-      sym = *((*r)->sym_ptr_ptr);
-      if (relocatable)
-       while (lo < hi)
-         {
-           mid = (lo + hi) >> 1;
-           if (syms[mid]->section->id < sym->section->id)
-             lo = mid + 1;
-           else if (syms[mid]->section->id > sym->section->id)
-             hi = mid;
-           else if (syms[mid]->value < sym->value + (*r)->addend)
-             lo = mid + 1;
-           else if (syms[mid]->value > sym->value + (*r)->addend)
-             hi = mid;
-           else
-             break;
-         }
-      else
-       while (lo < hi)
-         {
-           mid = (lo + hi) >> 1;
-           if (syms[mid]->value + syms[mid]->section->vma < (*r)->addend)
-             lo = mid + 1;
-           else if (syms[mid]->value + syms[mid]->section->vma > (*r)->addend)
-             hi = mid;
-           else
-             break;
-         }
+      names = (char *) (s + count);
 
-      if (lo >= hi)
+      for (i = secsymend; i < opdsymend; ++i)
        {
-         size_t len;
+         bfd_vma ent;
 
-         *s = *syms[i];
-         
-         if (! relocatable)
+         ent = bfd_get_64 (abfd, contents + syms[i]->value);
+         if (!sym_exists_at (syms, opdsymend, symcount, -1, ent))
            {
-             asection *sec;
+             long lo, hi;
+             size_t len;
+             asection *sec = abfd->sections;
+
+             *s = *syms[i];
+             lo = codesecsym;
+             hi = codesecsymend;
+             while (lo < hi)
+               {
+                 long mid = (lo + hi) >> 1;
+                 if (syms[mid]->section->vma < ent)
+                   lo = mid + 1;
+                 else if (syms[mid]->section->vma > ent)
+                   hi = mid;
+                 else
+                   {
+                     sec = syms[mid]->section;
+                     break;
+                   }
+               }
 
-             s->section = &bfd_abs_section;
-             for (sec = abfd->sections; sec; sec = sec->next)
-               if ((sec->flags & (SEC_ALLOC | SEC_CODE))
-                   == (SEC_ALLOC | SEC_CODE)
-                   && (*r)->addend >= sec->vma
-                   && (*r)->addend < sec->vma + sec->size)
-                 {
-                   s->section = sec;
+             if (lo >= hi && lo > codesecsym)
+               sec = syms[lo - 1]->section;
+
+             for (; sec != NULL; sec = sec->next)
+               {
+                 if (sec->vma > ent)
                    break;
-                 }
-             s->value = (*r)->addend - sec->vma;
-           }
-         else
-           {
-             s->section = sym->section;
-             s->value = sym->value + (*r)->addend;
+                 if ((sec->flags & SEC_ALLOC) == 0
+                     || (sec->flags & SEC_LOAD) == 0)
+                   break;
+                 if ((sec->flags & SEC_CODE) != 0)
+                   s->section = sec;
+               }
+             s->value = ent - s->section->vma;
+             s->name = names;
+             *names++ = '.';
+             len = strlen (syms[i]->name);
+             memcpy (names, syms[i]->name, len + 1);
+             names += len + 1;
+             s++;
            }
-         s->name = names;
-         len = strlen (syms[i]->name);
-         memcpy (names, syms[i]->name, len + 1);
-         names += len + 1;
-         s++;
        }
+      free (contents);
     }
 
+ done:
   free (syms);
-  free (relocs);
   return count;
 }
-
 \f
 /* The following functions are specific to the ELF linker, while
    functions above are used generally.  Those named ppc64_elf_* are
@@ -4038,12 +4036,9 @@ ppc64_elf_check_directives (bfd *abfd ATTRIBUTE_UNUSED,
                            struct bfd_link_info *info)
 {
   struct ppc_link_hash_table *htab;
-  extern const bfd_target bfd_elf64_powerpc_vec;
-  extern const bfd_target bfd_elf64_powerpcle_vec;
 
   htab = ppc_hash_table (info);
-  if (htab->elf.root.creator != &bfd_elf64_powerpc_vec
-      && htab->elf.root.creator != &bfd_elf64_powerpcle_vec)
+  if (!is_ppc64_target (htab->elf.root.creator))
     return TRUE;
 
   elf_link_hash_traverse (&htab->elf, add_symbol_adjust, info);
@@ -5050,8 +5045,8 @@ ppc64_elf_gc_sweep_hook (bfd *abfd, struct bfd_link_info *info,
 
 struct sfpr_def_parms
 {
-  const char *name;
-  unsigned int lo, hi;
+  const char name[12];
+  unsigned char lo, hi;
   bfd_byte * (*write_ent) (bfd *, bfd_byte *, int);
   bfd_byte * (*write_tail) (bfd *, bfd_byte *, int);
 };
@@ -5065,7 +5060,7 @@ sfpr_define (struct bfd_link_info *info, const struct sfpr_def_parms *parm)
   unsigned int i;
   size_t len = strlen (parm->name);
   bfd_boolean writing = FALSE;
-  char sym[20];
+  char sym[16];
 
   memcpy (sym, parm->name, len);
   sym[len + 2] = 0;
@@ -6263,7 +6258,7 @@ ppc64_elf_edit_opd (bfd *obfd, struct bfd_link_info *info,
          p = bfd_malloc (need_pad->size + 8);
          if (p == NULL)
            return FALSE;
-                      
+
          if (! bfd_get_section_contents (need_pad->owner, need_pad,
                                          p, 0, need_pad->size))
            return FALSE;
@@ -6903,7 +6898,7 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
       Elf_Internal_Shdr *symtab_hdr;
       asection *srel;
 
-      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
+      if (!is_ppc64_target (ibfd->xvec))
        continue;
 
       if (ppc64_tlsld_got (ibfd)->refcount > 0)
@@ -7073,6 +7068,9 @@ ppc64_elf_size_dynamic_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
 
   for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link_next)
     {
+      if (!is_ppc64_target (ibfd->xvec))
+       continue;
+
       s = ppc64_elf_tdata (ibfd)->got;
       if (s != NULL && s != htab->got)
        {
@@ -10112,10 +10110,15 @@ ppc64_elf_finish_dynamic_sections (bfd *output_bfd,
     }
 
   /* We need to handle writing out multiple GOT sections ourselves,
-     since we didn't add them to DYNOBJ.  */
+     since we didn't add them to DYNOBJ.  We know dynobj is the first
+     bfd.  */
   while ((dynobj = dynobj->link_next) != NULL)
     {
       asection *s;
+
+      if (!is_ppc64_target (dynobj->xvec))
+       continue;
+
       s = ppc64_elf_tdata (dynobj)->got;
       if (s != NULL
          && s->size != 0
This page took 0.041322 seconds and 4 git commands to generate.