Enable relro by default for AArch64, ARM and SCORE.
[external/binutils.git] / ld / emultempl / elf32.em
index 5fe576e..47fa549 100644 (file)
@@ -12,7 +12,7 @@ fragment <<EOF
 /* This file is is generated by a shell script.  DO NOT EDIT! */
 
 /* ${ELFSIZE} bit ELF emulation code for ${EMULATION_NAME}
-   Copyright (C) 1991-2015 Free Software Foundation, Inc.
+   Copyright (C) 1991-2016 Free Software Foundation, Inc.
    Written by Steve Chamberlain <sac@cygnus.com>
    ELF support by Ian Lance Taylor <ian@cygnus.com>
 
@@ -49,7 +49,6 @@ fragment <<EOF
 #include "ldmain.h"
 #include "ldmisc.h"
 #include "ldexp.h"
-#include "ldlex.h"
 #include "ldlang.h"
 #include "ldfile.h"
 #include "ldemul.h"
@@ -61,6 +60,7 @@ fragment <<EOF
 
 /* Declare functions used by various EXTRA_EM_FILEs.  */
 static void gld${EMULATION_NAME}_before_parse (void);
+static void gld${EMULATION_NAME}_after_parse (void);
 static void gld${EMULATION_NAME}_after_open (void);
 static void gld${EMULATION_NAME}_before_allocation (void);
 static void gld${EMULATION_NAME}_after_allocation (void);
@@ -103,6 +103,24 @@ gld${EMULATION_NAME}_before_parse (void)
   input_flags.dynamic = ${DYNAMIC_LINK-TRUE};
   config.has_shared = `if test -n "$GENERATE_SHLIB_SCRIPT" ; then echo TRUE ; else echo FALSE ; fi`;
   config.separate_code = `if test "x${SEPARATE_CODE}" = xyes ; then echo TRUE ; else echo FALSE ; fi`;
+  `if test -n "$CALL_NOP_BYTE" ; then echo link_info.call_nop_byte = $CALL_NOP_BYTE; fi`;
+  link_info.check_relocs_after_open_input = `if test "x${CHECK_RELOCS_AFTER_OPEN_INPUT}" = xyes ; then echo TRUE ; else echo FALSE ; fi`;
+  link_info.relro = DEFAULT_LD_Z_RELRO;
+}
+
+EOF
+fi
+
+if test x"$LDEMUL_AFTER_PARSE" != xgld"$EMULATION_NAME"_after_parse; then
+fragment <<EOF
+
+static void
+gld${EMULATION_NAME}_after_parse (void)
+{
+  if (bfd_link_pie (&link_info))
+    link_info.flags_1 |= (bfd_vma) DF_1_PIE;
+
+  after_parse_default ();
 }
 
 EOF
@@ -1241,12 +1259,16 @@ fragment <<EOF
          rp = bfd_elf_get_runpath_list (link_info.output_bfd, &link_info);
          for (; !found && rp != NULL; rp = rp->next)
            {
-             char *tmpname = gld${EMULATION_NAME}_add_sysroot (rp->name);
+             const char *tmpname = rp->name;
+
+             if (IS_ABSOLUTE_PATH (tmpname))
+               tmpname = gld${EMULATION_NAME}_add_sysroot (tmpname);
              found = (rp->by == l->by
                       && gld${EMULATION_NAME}_search_needed (tmpname,
                                                              &n,
                                                              force));
-             free (tmpname);
+             if (tmpname != rp->name)
+               free ((char *) tmpname);
            }
          if (found)
            break;
@@ -1434,6 +1456,11 @@ gld${EMULATION_NAME}_append_to_separated_string (char **to, char *op_arg)
     }
 }
 
+#if defined(__GNUC__) && GCC_VERSION < 4006
+  /* Work around a GCC uninitialized warning bug fixed in GCC 4.6.  */
+static struct bfd_link_hash_entry ehdr_start_empty;
+#endif
+
 /* This is called after the sections have been attached to output
    sections, but before any sizes or addresses have been set.  */
 
@@ -1446,7 +1473,7 @@ gld${EMULATION_NAME}_before_allocation (void)
   struct elf_link_hash_entry *ehdr_start = NULL;
 #if defined(__GNUC__) && GCC_VERSION < 4006
   /* Work around a GCC uninitialized warning bug fixed in GCC 4.6.  */
-  struct bfd_link_hash_entry ehdr_start_save = ehdr_start_save;
+  struct bfd_link_hash_entry ehdr_start_save = ehdr_start_empty;
 #else
   struct bfd_link_hash_entry ehdr_start_save;
 #endif
@@ -1556,7 +1583,6 @@ ${ELF_INTERPRETER_SET_DEFAULT}
        asection *s;
        bfd_size_type sz;
        char *msg;
-       bfd_boolean ret;
 
        if (is->flags.just_syms)
          continue;
@@ -1572,11 +1598,9 @@ ${ELF_INTERPRETER_SET_DEFAULT}
          einfo ("%F%B: Can't read contents of section .gnu.warning: %E\n",
                 is->the_bfd);
        msg[sz] = '\0';
-       ret = link_info.callbacks->warning (&link_info, msg,
-                                           (const char *) NULL,
-                                           is->the_bfd, (asection *) NULL,
-                                           (bfd_vma) 0);
-       ASSERT (ret);
+       (*link_info.callbacks->warning) (&link_info, msg,
+                                        (const char *) NULL, is->the_bfd,
+                                        (asection *) NULL, (bfd_vma) 0);
        free (msg);
 
        /* Clobber the section size, so that we don't waste space
@@ -1827,6 +1851,8 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
   int isdyn = 0;
   int iself = s->owner->xvec->flavour == bfd_target_elf_flavour;
   unsigned int sh_type = iself ? elf_section_type (s) : SHT_NULL;
+  flagword flags;
+  asection *nexts;
 
   if (!bfd_link_relocatable (&link_info)
       && link_info.combreloc
@@ -1865,11 +1891,11 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
 
        if (os->bfd_section != NULL
            && (os->bfd_section->flags == 0
-               || (_bfd_elf_match_sections_by_type (link_info.output_bfd,
-                                                    os->bfd_section,
-                                                    s->owner, s)
-                   && ((s->flags ^ os->bfd_section->flags)
-                       & (SEC_LOAD | SEC_ALLOC)) == 0)))
+               || (((s->flags ^ os->bfd_section->flags)
+                    & (SEC_LOAD | SEC_ALLOC)) == 0
+                   && _bfd_elf_match_sections_by_type (link_info.output_bfd,
+                                                       os->bfd_section,
+                                                       s->owner, s))))
          {
            /* We already have an output section statement with this
               name, and its bfd section has compatible flags.
@@ -1919,6 +1945,26 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
       return os;
     }
 
+  flags = s->flags;
+  if (!bfd_link_relocatable (&link_info))
+    {
+      nexts = s;
+      while ((nexts = bfd_get_next_section_by_name (nexts->owner, nexts))
+            != NULL)
+       if (nexts->output_section == NULL
+           && (nexts->flags & SEC_EXCLUDE) == 0
+           && ((nexts->flags ^ flags) & (SEC_LOAD | SEC_ALLOC)) == 0
+           && (nexts->owner->flags & DYNAMIC) == 0
+           && nexts->owner->usrdata != NULL
+           && !(((lang_input_statement_type *) nexts->owner->usrdata)
+                ->flags.just_syms)
+           && _bfd_elf_match_sections_by_type (nexts->owner, nexts,
+                                               s->owner, s))
+         flags = (((flags ^ SEC_READONLY)
+                   | (nexts->flags ^ SEC_READONLY))
+                  ^ SEC_READONLY);
+    }
+
   /* Decide which segment the section should go in based on the
      section name and section flags.  We put loadable .note sections
      right after the .interp section, so that the PT_NOTE segment is
@@ -1926,27 +1972,27 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
      in the first page.  */
 
   place = NULL;
-  if ((s->flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
+  if ((flags & (SEC_ALLOC | SEC_DEBUGGING)) == 0)
     place = &hold[orphan_nonalloc];
-  else if ((s->flags & SEC_ALLOC) == 0)
+  else if ((flags & SEC_ALLOC) == 0)
     ;
-  else if ((s->flags & SEC_LOAD) != 0
+  else if ((flags & SEC_LOAD) != 0
           && ((iself && sh_type == SHT_NOTE)
               || (!iself && CONST_STRNEQ (secname, ".note"))))
     place = &hold[orphan_interp];
-  else if ((s->flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
+  else if ((flags & (SEC_LOAD | SEC_HAS_CONTENTS | SEC_THREAD_LOCAL)) == 0)
     place = &hold[orphan_bss];
-  else if ((s->flags & SEC_SMALL_DATA) != 0)
+  else if ((flags & SEC_SMALL_DATA) != 0)
     place = &hold[orphan_sdata];
-  else if ((s->flags & SEC_THREAD_LOCAL) != 0)
+  else if ((flags & SEC_THREAD_LOCAL) != 0)
     place = &hold[orphan_tdata];
-  else if ((s->flags & SEC_READONLY) == 0)
+  else if ((flags & SEC_READONLY) == 0)
     place = &hold[orphan_data];
   else if (((iself && (sh_type == SHT_RELA || sh_type == SHT_REL))
            || (!iself && CONST_STRNEQ (secname, ".rel")))
-          && (s->flags & SEC_LOAD) != 0)
+          && (flags & SEC_LOAD) != 0)
     place = &hold[orphan_rel];
-  else if ((s->flags & SEC_CODE) == 0)
+  else if ((flags & SEC_CODE) == 0)
     place = &hold[orphan_rodata];
   else
     place = &hold[orphan_text];
@@ -1963,8 +2009,9 @@ gld${EMULATION_NAME}_place_orphan (asection *s,
        }
       after = place->os;
       if (after == NULL)
-       after = lang_output_section_find_by_flags
-         (s, &place->os, _bfd_elf_match_sections_by_type);
+       after
+         = lang_output_section_find_by_flags (s, flags, &place->os,
+                                              _bfd_elf_match_sections_by_type);
       if (after == NULL)
        /* *ABS* is always the first output section statement.  */
        after = &lang_output_section_statement.head->output_section_statement;
@@ -2290,13 +2337,6 @@ fragment <<EOF
 
 EOF
 fi
-if test x"$GENERATE_PIE_SCRIPT" = xyes; then
-fragment <<EOF
-    case OPTION_PIE:
-      link_info.flags_1 |= (bfd_vma) DF_1_PIE;
-      break;
-EOF
-fi
 fragment <<EOF
     case 'z':
       if (strcmp (optarg, "defs") == 0)
@@ -2387,6 +2427,10 @@ fragment <<EOF
        link_info.relro = TRUE;
       else if (strcmp (optarg, "norelro") == 0)
        link_info.relro = FALSE;
+      else if (strcmp (optarg, "common") == 0)
+       link_info.elf_stt_common = elf_stt_common;
+      else if (strcmp (optarg, "nocommon") == 0)
+       link_info.elf_stt_common = no_elf_stt_common;
       else if (strcmp (optarg, "text") == 0)
        link_info.error_textrel = TRUE;
       else if (strcmp (optarg, "notext") == 0)
@@ -2459,7 +2503,7 @@ struct ld_emulation_xfer_struct ld_${EMULATION_NAME}_emulation =
   ${LDEMUL_BEFORE_PARSE-gld${EMULATION_NAME}_before_parse},
   ${LDEMUL_SYSLIB-syslib_default},
   ${LDEMUL_HLL-hll_default},
-  ${LDEMUL_AFTER_PARSE-after_parse_default},
+  ${LDEMUL_AFTER_PARSE-gld${EMULATION_NAME}_after_parse},
   ${LDEMUL_AFTER_OPEN-gld${EMULATION_NAME}_after_open},
   ${LDEMUL_AFTER_ALLOCATION-gld${EMULATION_NAME}_after_allocation},
   ${LDEMUL_SET_OUTPUT_ARCH-set_output_arch_default},