Unbreak DJGPP build of GDB.
[deliverable/binutils-gdb.git] / bfd / elflink.c
index bf28885548f7b953d2755f9cb41337a52d6ae305..95209061313a800b34a8d7f9bd76d9ddf2bb9393 100644 (file)
@@ -54,6 +54,47 @@ struct elf_find_verdep_info
 static bfd_boolean _bfd_elf_fix_symbol_flags
   (struct elf_link_hash_entry *, struct elf_info_failed *);
 
+asection *
+_bfd_elf_section_for_symbol (struct elf_reloc_cookie *cookie,
+                            unsigned long r_symndx,
+                            bfd_boolean discard)
+{
+  if (r_symndx >= cookie->locsymcount
+      || ELF_ST_BIND (cookie->locsyms[r_symndx].st_info) != STB_LOCAL)
+    {
+      struct elf_link_hash_entry *h;
+
+      h = cookie->sym_hashes[r_symndx - cookie->extsymoff];
+
+      while (h->root.type == bfd_link_hash_indirect
+            || h->root.type == bfd_link_hash_warning)
+       h = (struct elf_link_hash_entry *) h->root.u.i.link;
+
+      if ((h->root.type == bfd_link_hash_defined
+          || h->root.type == bfd_link_hash_defweak)
+          && discarded_section (h->root.u.def.section))
+        return h->root.u.def.section;
+      else
+       return NULL;
+    }
+  else
+    {
+      /* It's not a relocation against a global symbol,
+        but it could be a relocation against a local
+        symbol for a discarded section.  */
+      asection *isec;
+      Elf_Internal_Sym *isym;
+
+      /* Need to: get the symbol; get the section.  */
+      isym = &cookie->locsyms[r_symndx];
+      isec = bfd_section_from_elf_index (cookie->abfd, isym->st_shndx);
+      if (isec != NULL
+         && discard ? discarded_section (isec) : 1)
+       return isec;
+     }
+  return NULL;
+}
+
 /* Define a symbol in a dynamic linkage section.  */
 
 struct elf_link_hash_entry *
@@ -77,9 +118,9 @@ _bfd_elf_define_linkage_sym (bfd *abfd,
     }
 
   bh = &h->root;
+  bed = get_elf_backend_data (abfd);
   if (!_bfd_generic_link_add_one_symbol (info, abfd, name, BSF_GLOBAL,
-                                        sec, 0, NULL, FALSE,
-                                        get_elf_backend_data (abfd)->collect,
+                                        sec, 0, NULL, FALSE, bed->collect,
                                         &bh))
     return NULL;
   h = (struct elf_link_hash_entry *) bh;
@@ -90,7 +131,6 @@ _bfd_elf_define_linkage_sym (bfd *abfd,
   if (ELF_ST_VISIBILITY (h->other) != STV_INTERNAL)
     h->other = (h->other & ~ELF_ST_VISIBILITY (-1)) | STV_HIDDEN;
 
-  bed = get_elf_backend_data (abfd);
   (*bed->elf_backend_hide_symbol) (info, h, TRUE);
   return h;
 }
@@ -845,7 +885,7 @@ _bfd_elf_link_renumber_dynsyms (bfd *output_bfd,
 
 static void
 elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
-                   const Elf_Internal_Sym *isym,
+                   const Elf_Internal_Sym *isym, asection *sec,
                    bfd_boolean definition, bfd_boolean dynamic)
 {
   const struct elf_backend_data *bed = get_elf_backend_data (abfd);
@@ -866,7 +906,9 @@ elf_merge_st_other (bfd *abfd, struct elf_link_hash_entry *h,
       if (symvis - 1 < hvis - 1)
        h->other = symvis | (h->other & ~ELF_ST_VISIBILITY (-1));
     }
-  else if (definition && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT)
+  else if (definition
+          && ELF_ST_VISIBILITY (isym->st_other) != STV_DEFAULT
+          && (sec->flags & SEC_READONLY) == 0)
     h->protected_def = 1;
 }
 
@@ -1418,7 +1460,7 @@ _bfd_elf_merge_symbol (bfd *abfd,
       /* Merge st_other.  If the symbol already has a dynamic index,
         but visibility says it should not be visible, turn it into a
         local symbol.  */
-      elf_merge_st_other (abfd, h, sym, newdef, newdyn);
+      elf_merge_st_other (abfd, h, sym, sec, newdef, newdyn);
       if (h->dynindx != -1)
        switch (ELF_ST_VISIBILITY (h->other))
          {
@@ -2672,14 +2714,14 @@ _bfd_elf_adjust_dynamic_copy (struct bfd_link_info *info,
   /* Increment the size of DYNBSS to make room for the symbol.  */
   dynbss->size += h->size;
 
-  if (h->protected_def)
-    {
-      info->callbacks->einfo
-       (_("%P: copy reloc against protected `%T' is invalid\n"),
-        h->root.root.string);
-      bfd_set_error (bfd_error_bad_value);
-      return FALSE;
-    }
+  /* No error if extern_protected_data is true.  */
+  if (h->protected_def
+      && (!info->extern_protected_data
+         || (info->extern_protected_data < 0
+             && !get_elf_backend_data (dynbss->owner)->extern_protected_data)))
+    info->callbacks->einfo
+      (_("%P: copy reloc against protected `%T' is dangerous\n"),
+       h->root.root.string);
 
   return TRUE;
 }
@@ -2836,8 +2878,12 @@ _bfd_elf_symbol_refs_local_p (struct elf_link_hash_entry *h,
 
   bed = get_elf_backend_data (hash_table->dynobj);
 
-  /* STV_PROTECTED non-function symbols are local.  */
-  if (!bed->is_function_type (h->type))
+  /* If extern_protected_data is false, STV_PROTECTED non-function
+     symbols are local.  */
+  if ((!info->extern_protected_data
+       || (info->extern_protected_data < 0
+          && !bed->extern_protected_data))
+      && !bed->is_function_type (h->type))
     return TRUE;
 
   /* Function pointer equality tests may require that STV_PROTECTED
@@ -2936,6 +2982,10 @@ elf_link_is_defined_archive_symbol (bfd * abfd, carsym * symdef)
   if (abfd == NULL)
     return FALSE;
 
+  /* Return FALSE if the object has been claimed by plugin.  */
+  if (abfd->plugin_format == bfd_plugin_yes)
+    return FALSE;
+
   if (! bfd_check_format (abfd, bfd_object))
     return FALSE;
 
@@ -4356,7 +4406,7 @@ error_free_dyn:
            }
 
          /* Merge st_other field.  */
-         elf_merge_st_other (abfd, h, isym, definition, dynamic);
+         elf_merge_st_other (abfd, h, isym, sec, definition, dynamic);
 
          /* We don't want to make debug symbol dynamic.  */
          if (definition && (sec->flags & SEC_DEBUGGING) && !info->relocatable)
@@ -7441,8 +7491,6 @@ struct elf_outext_info
 {
   bfd_boolean failed;
   bfd_boolean localsyms;
-  bfd_boolean need_second_pass;
-  bfd_boolean second_pass;
   bfd_boolean file_sym_done;
   struct elf_final_link_info *flinfo;
 };
@@ -7793,28 +7841,34 @@ put_value (bfd_vma size,
 {
   location += (size - chunksz);
 
-  for (; size; size -= chunksz, location -= chunksz, x >>= (chunksz * 8))
+  for (; size; size -= chunksz, location -= chunksz)
     {
       switch (chunksz)
        {
-       default:
-       case 0:
-         abort ();
        case 1:
          bfd_put_8 (input_bfd, x, location);
+         x >>= 8;
          break;
        case 2:
          bfd_put_16 (input_bfd, x, location);
+         x >>= 16;
          break;
        case 4:
          bfd_put_32 (input_bfd, x, location);
+         /* Computed this way because x >>= 32 is undefined if x is a 32-bit value.  */
+         x >>= 16;
+         x >>= 16;
          break;
-       case 8:
 #ifdef BFD64
+       case 8:
          bfd_put_64 (input_bfd, x, location);
-#else
-         abort ();
+         /* Computed this way because x >>= 64 is undefined if x is a 64-bit value.  */
+         x >>= 32;
+         x >>= 32;
+         break;
 #endif
+       default:
+         abort ();
          break;
        }
     }
@@ -8821,27 +8875,6 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
     {
       if (!h->forced_local)
        return TRUE;
-      if (eoinfo->second_pass
-         && !((h->root.type == bfd_link_hash_defined
-               || h->root.type == bfd_link_hash_defweak)
-              && h->root.u.def.section->output_section != NULL))
-       return TRUE;
-
-      if (!eoinfo->file_sym_done
-         && (eoinfo->second_pass ? eoinfo->flinfo->filesym_count == 1
-                                 : eoinfo->flinfo->filesym_count > 1))
-       {
-         /* Output a FILE symbol so that following locals are not associated
-            with the wrong input file.  */
-         memset (&sym, 0, sizeof (sym));
-         sym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
-         sym.st_shndx = SHN_ABS;
-         if (!elf_link_output_sym (eoinfo->flinfo, NULL, &sym,
-                                   bfd_und_section_ptr, NULL))
-           return FALSE;
-
-         eoinfo->file_sym_done = TRUE;
-       }
     }
   else
     {
@@ -8925,8 +8958,9 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
      a regular file, or that we have been told to strip.  However, if
      h->indx is set to -2, the symbol is used by a reloc and we must
      output it.  */
+  strip = FALSE;
   if (h->indx == -2)
-    strip = FALSE;
+    ;
   else if ((h->def_dynamic
            || h->ref_dynamic
            || h->root.type == bfd_link_hash_new)
@@ -8952,12 +8986,11 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
           && h->root.u.undef.abfd != NULL
           && (h->root.u.undef.abfd->flags & BFD_PLUGIN) != 0)
     strip = TRUE;
-  else
-    strip = FALSE;
 
   /* If we're stripping it, and it's not a dynamic symbol, there's
-     nothing else to do unless it is a forced local symbol or a
-     STT_GNU_IFUNC symbol.  */
+     nothing else to do.   However, if it is a forced local symbol or
+     an ifunc symbol we need to give the backend finish_dynamic_symbol
+     function a chance to make it dynamic.  */
   if (strip
       && h->dynindx == -1
       && h->type != STT_GNU_IFUNC
@@ -9003,19 +9036,6 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
        input_sec = h->root.u.def.section;
        if (input_sec->output_section != NULL)
          {
-           if (eoinfo->localsyms && flinfo->filesym_count == 1)
-             {
-               bfd_boolean second_pass_sym
-                 = (input_sec->owner == flinfo->output_bfd
-                    || input_sec->owner == NULL
-                    || (input_sec->flags & SEC_LINKER_CREATED) != 0
-                    || (input_sec->owner->flags & BFD_LINKER_CREATED) != 0);
-
-               eoinfo->need_second_pass |= second_pass_sym;
-               if (eoinfo->second_pass != second_pass_sym)
-                 return TRUE;
-             }
-
            sym.st_shndx =
              _bfd_elf_section_from_bfd_section (flinfo->output_bfd,
                                                 input_sec->output_section);
@@ -9241,11 +9261,43 @@ elf_link_output_extsym (struct bfd_hash_entry *bh, void *data)
        }
     }
 
-  /* If we're stripping it, then it was just a dynamic symbol, and
-     there's nothing else to do.  */
-  if (strip || (input_sec->flags & SEC_EXCLUDE) != 0)
+  /* If the symbol is undefined, and we didn't output it to .dynsym,
+     strip it from .symtab too.  Obviously we can't do this for
+     relocatable output or when needed for --emit-relocs.  */
+  else if (input_sec == bfd_und_section_ptr
+          && h->indx != -2
+          && !flinfo->info->relocatable)
+    return TRUE;
+  /* Also strip others that we couldn't earlier due to dynamic symbol
+     processing.  */
+  if (strip)
+    return TRUE;
+  if ((input_sec->flags & SEC_EXCLUDE) != 0)
     return TRUE;
 
+  /* Output a FILE symbol so that following locals are not associated
+     with the wrong input file.  We need one for forced local symbols
+     if we've seen more than one FILE symbol or when we have exactly
+     one FILE symbol but global symbols are present in a file other
+     than the one with the FILE symbol.  We also need one if linker
+     defined symbols are present.  In practice these conditions are
+     always met, so just emit the FILE symbol unconditionally.  */
+  if (eoinfo->localsyms
+      && !eoinfo->file_sym_done
+      && eoinfo->flinfo->filesym_count != 0)
+    {
+      Elf_Internal_Sym fsym;
+
+      memset (&fsym, 0, sizeof (fsym));
+      fsym.st_info = ELF_ST_INFO (STB_LOCAL, STT_FILE);
+      fsym.st_shndx = SHN_ABS;
+      if (!elf_link_output_sym (eoinfo->flinfo, NULL, &fsym,
+                               bfd_und_section_ptr, NULL))
+       return FALSE;
+
+      eoinfo->file_sym_done = TRUE;
+    }
+
   indx = bfd_get_symcount (flinfo->output_bfd);
   ret = elf_link_output_sym (flinfo, h->root.root.string, &sym, input_sec, h);
   if (ret == 0)
@@ -9273,6 +9325,7 @@ elf_section_ignore_discarded_relocs (asection *sec)
     {
     case SEC_INFO_TYPE_STABS:
     case SEC_INFO_TYPE_EH_FRAME:
+    case SEC_INFO_TYPE_EH_FRAME_ENTRY:
       return TRUE;
     default:
       break;
@@ -9468,8 +9521,9 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
 
       *ppsection = isec;
 
-      /* Don't output the first, undefined, symbol.  */
-      if (ppsection == flinfo->sections)
+      /* Don't output the first, undefined, symbol.  In fact, don't
+        output any undefined local symbol.  */
+      if (isec == bfd_und_section_ptr)
        continue;
 
       if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
@@ -10203,6 +10257,14 @@ elf_link_input_bfd (struct elf_final_link_info *flinfo, bfd *input_bfd)
              return FALSE;
          }
          break;
+       case SEC_INFO_TYPE_EH_FRAME_ENTRY:
+         {
+           if (! _bfd_elf_write_section_eh_frame_entry (output_bfd,
+                                                        flinfo->info,
+                                                        o, contents))
+             return FALSE;
+         }
+         break;
        default:
          {
            /* FIXME: octets_per_byte.  */
@@ -10878,6 +10940,21 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
         to count upwards while actually outputting the relocations.  */
       esdo->rel.count = 0;
       esdo->rela.count = 0;
+
+      if (esdo->this_hdr.sh_offset == (file_ptr) -1)
+       {
+         /* Cache the section contents so that they can be compressed
+            later.  Use bfd_malloc since it will be freed by
+            bfd_compress_section_contents.  */
+         unsigned char *contents = esdo->this_hdr.contents;
+         if ((o->flags & SEC_ELF_COMPRESS) == 0 || contents != NULL)
+           abort ();
+         contents
+           = (unsigned char *) bfd_malloc (esdo->this_hdr.sh_size);
+         if (contents == NULL)
+           goto error_return;
+         esdo->this_hdr.contents = contents;
+       }
     }
 
   /* We have now assigned file positions for all the sections except
@@ -11058,6 +11135,9 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
        return FALSE;
     }
 
+  if (!_bfd_elf_fixup_eh_frame_hdr (info))
+    return FALSE;
+
   /* Since ELF permits relocations to be against local symbols, we
      must have the local symbols available when we do the relocations.
      Since we would rather only read the local symbols once, and we
@@ -11158,21 +11238,11 @@ bfd_elf_final_link (bfd *abfd, struct bfd_link_info *info)
   eoinfo.failed = FALSE;
   eoinfo.flinfo = &flinfo;
   eoinfo.localsyms = TRUE;
-  eoinfo.need_second_pass = FALSE;
-  eoinfo.second_pass = FALSE;
   eoinfo.file_sym_done = FALSE;
   bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
   if (eoinfo.failed)
     return FALSE;
 
-  if (eoinfo.need_second_pass)
-    {
-      eoinfo.second_pass = TRUE;
-      bfd_hash_traverse (&info->hash->table, elf_link_output_extsym, &eoinfo);
-      if (eoinfo.failed)
-       return FALSE;
-    }
-
   /* If backend needs to output some local symbols not present in the hash
      table, do it now.  */
   if (bed->elf_backend_output_arch_local_syms
@@ -11977,6 +12047,11 @@ _bfd_elf_gc_mark (struct bfd_link_info *info,
        }
     }
 
+  eh_frame = elf_section_eh_frame_entry (sec);
+  if (ret && eh_frame && !eh_frame->gc_mark)
+    if (!_bfd_elf_gc_mark (info, eh_frame, gc_mark_hook))
+      ret = FALSE;
+
   return ret;
 }
 
@@ -12107,7 +12182,6 @@ _bfd_elf_gc_mark_extra_sections (struct bfd_link_info *info,
                                isec->name, ilen) == 0)
                  {
                    dsec->gc_mark = 0;
-                   break;
                  }
              }
          }
@@ -12165,7 +12239,8 @@ elf_gc_sweep (bfd *abfd, struct bfd_link_info *info)
     {
       asection *o;
 
-      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
+      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
+         || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
        continue;
 
       for (o = sub->sections; o != NULL; o = o->next)
@@ -12392,6 +12467,36 @@ _bfd_elf_gc_keep (struct bfd_link_info *info)
     }
 }
 
+bfd_boolean
+bfd_elf_parse_eh_frame_entries (bfd *abfd ATTRIBUTE_UNUSED,
+                               struct bfd_link_info *info)
+{
+  bfd *ibfd = info->input_bfds;
+
+  for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
+    {
+      asection *sec;
+      struct elf_reloc_cookie cookie;
+
+      if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
+       continue;
+
+      if (!init_reloc_cookie (&cookie, info, ibfd))
+       return FALSE;
+
+      for (sec = ibfd->sections; sec; sec = sec->next)
+       {
+         if (CONST_STRNEQ (bfd_section_name (ibfd, sec), ".eh_frame_entry")
+             && init_reloc_cookie_rels (&cookie, info, ibfd, sec))
+           {
+             _bfd_elf_parse_eh_frame_entry (info, sec, &cookie);
+             fini_reloc_cookie_rels (&cookie, sec);
+           }
+       }
+    }
+  return TRUE;
+}
+
 /* Do mark and sweep of unused sections.  */
 
 bfd_boolean
@@ -12415,7 +12520,9 @@ bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
 
   /* Try to parse each bfd's .eh_frame section.  Point elf_eh_frame_section
      at the .eh_frame section if we can mark the FDEs individually.  */
-  for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
+  for (sub = info->input_bfds;
+       info->eh_frame_hdr_type != COMPACT_EH_HDR && sub != NULL;
+       sub = sub->link.next)
     {
       asection *sec;
       struct elf_reloc_cookie cookie;
@@ -12452,7 +12559,8 @@ bfd_elf_gc_sections (bfd *abfd, struct bfd_link_info *info)
     {
       asection *o;
 
-      if (bfd_get_flavour (sub) != bfd_target_elf_flavour)
+      if (bfd_get_flavour (sub) != bfd_target_elf_flavour
+         || !(*bed->relocs_compatible) (sub->xvec, abfd->xvec))
        continue;
 
       /* Start at sections marked with SEC_KEEP (ref _bfd_elf_gc_keep).
@@ -12918,7 +13026,9 @@ bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
        }
     }
 
-  o = bfd_get_section_by_name (output_bfd, ".eh_frame");
+  o = NULL;
+  if (info->eh_frame_hdr_type != COMPACT_EH_HDR)
+    o = bfd_get_section_by_name (output_bfd, ".eh_frame");
   if (o != NULL)
     {
       asection *i;
@@ -12966,7 +13076,10 @@ bfd_elf_discard_info (bfd *output_bfd, struct bfd_link_info *info)
        }
     }
 
-  if (info->eh_frame_hdr
+  if (info->eh_frame_hdr_type == COMPACT_EH_HDR)
+    _bfd_elf_end_eh_frame_parsing (info);
+
+  if (info->eh_frame_hdr_type
       && !info->relocatable
       && _bfd_elf_discard_section_eh_frame_hdr (output_bfd, info))
     changed = 1;
@@ -13272,7 +13385,7 @@ _bfd_elf_copy_link_hash_symbol_type (bfd *abfd,
   ehdest->target_internal = ehsrc->target_internal;
 
   isym.st_other = ehsrc->other;
-  elf_merge_st_other (abfd, ehdest, &isym, TRUE, FALSE);
+  elf_merge_st_other (abfd, ehdest, &isym, NULL, TRUE, FALSE);
 }
 
 /* Append a RELA relocation REL to section S in BFD.  */
This page took 0.034659 seconds and 4 git commands to generate.