Use dynrelro for symbols in relro sections too
authorAlan Modra <amodra@gmail.com>
Wed, 28 Dec 2016 06:34:15 +0000 (17:04 +1030)
committerAlan Modra <amodra@gmail.com>
Wed, 28 Dec 2016 11:40:38 +0000 (22:10 +1030)
PR ld/20995
bfd/
* elflink.c (elf_link_add_object_symbols): Mark relro sections
in dynamic objects SEC_READONLY.
ld/
* testsuite/ld-elf/pr20995c.s: New test file.
* testsuite/ld-elf/pr20995-2so.r: Likewise.
* testsuite/ld-elf/elf.exp: Run it.

bfd/ChangeLog
bfd/elflink.c
ld/ChangeLog
ld/testsuite/ld-elf/elf.exp
ld/testsuite/ld-elf/pr20995-2so.r [new file with mode: 0644]
ld/testsuite/ld-elf/pr20995c.s [new file with mode: 0644]

index 4d7bd7763c53ad60eb3d42ce404a15560af965dd..d1b012fe0bc536219402bfe57c39794ed97a4c05 100644 (file)
@@ -1,3 +1,9 @@
+2016-12-28  Alan Modra  <amodra@gmail.com>
+
+       PR ld/20995
+       * elflink.c (elf_link_add_object_symbols): Mark relro sections
+       in dynamic objects SEC_READONLY.
+
 2016-12-26  Alan Modra  <amodra@gmail.com>
 
        PR ld/20995
index d8d40f1441bafbeb3e39d845fe981f8993ea0436..2fd8883fd60484974b0ac1c872bddc2fcc81d7b1 100644 (file)
@@ -3819,6 +3819,7 @@ elf_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
       const char *soname = NULL;
       char *audit = NULL;
       struct bfd_link_needed_list *rpath = NULL, *runpath = NULL;
+      const Elf_Internal_Phdr *phdr;
       int ret;
 
       /* ld --just-symbols and dynamic objects don't mix very well.
@@ -3968,6 +3969,21 @@ error_free_dyn:
          *pn = rpath;
        }
 
+      /* If we have a PT_GNU_RELRO program header, mark as read-only
+        all sections contained fully therein.  This makes relro
+        shared library sections appear as they will at run-time.  */
+      phdr = elf_tdata (abfd)->phdr + elf_elfheader (abfd)->e_phnum;
+      while (--phdr >= elf_tdata (abfd)->phdr)
+       if (phdr->p_type == PT_GNU_RELRO)
+         {
+           for (s = abfd->sections; s != NULL; s = s->next)
+             if ((s->flags & SEC_ALLOC) != 0
+                 && s->vma >= phdr->p_vaddr
+                 && s->vma + s->size <= phdr->p_vaddr + phdr->p_memsz)
+               s->flags |= SEC_READONLY;
+           break;
+         }
+
       /* We do not want to include any of the sections in a dynamic
         object in the output file.  We hack by simply clobbering the
         list of sections in the BFD.  This could be handled more
index 1b8f59aa600f2a5311a4ce3f08502e4f68faa2a5..be179edce54bb4a2a9cc052e5bfddb24ab36f583 100644 (file)
@@ -1,3 +1,10 @@
+2016-12-28  Alan Modra  <amodra@gmail.com>
+
+       PR ld/20995
+       * testsuite/ld-elf/pr20995c.s: New test file.
+       * testsuite/ld-elf/pr20995-2so.r: Likewise.
+       * testsuite/ld-elf/elf.exp: Run it.
+
 2016-12-26  Alan Modra  <amodra@gmail.com>
 
        PR ld/20995
index 9b1fbeb9a83227fc1254761b64740cd02f764a27..47f13832edd816da4827bc4e7da363351cf43945 100644 (file)
@@ -134,6 +134,12 @@ if { [check_shared_lib_support] } then {
            "-shared" "" ""
            {pr20995b.s} {} "pr20995.so"}
     }
+    setup_xfail "tic6x-*-*"
+    run_ld_link_tests {
+       {"Build pr20995-2.so"
+           "-shared -z relro" "" ""
+           {pr20995c.s} {{readelf {-l --wide} pr20995-2so.r}} "pr20995-2.so"}
+    }
     # These targets don't copy dynamic variables into .bss.
     setup_xfail "alpha-*-*" "bfin-*-*" "ia64-*-*" "xtensa-*-*"
     run_ld_link_tests {
@@ -141,6 +147,12 @@ if { [check_shared_lib_support] } then {
            "" "tmpdir/pr20995.so" ""
            {pr20995a.s} {{readelf {-S --wide} pr20995.r}} "pr20995"}
     }
+    setup_xfail "alpha-*-*" "bfin-*-*" "ia64-*-*" "xtensa-*-*"
+    run_ld_link_tests {
+       {"pr20995-2"
+           "" "tmpdir/pr20995-2.so" ""
+           {pr20995a.s} {{readelf {-S --wide} pr20995.r}} "pr20995-2"}
+    }
 }
 
 set test_list [lsort [glob -nocomplain $srcdir/$subdir/*.d]]
diff --git a/ld/testsuite/ld-elf/pr20995-2so.r b/ld/testsuite/ld-elf/pr20995-2so.r
new file mode 100644 (file)
index 0000000..3cd4c27
--- /dev/null
@@ -0,0 +1,3 @@
+#...
+  GNU_RELRO .*
+#pass
diff --git a/ld/testsuite/ld-elf/pr20995c.s b/ld/testsuite/ld-elf/pr20995c.s
new file mode 100644 (file)
index 0000000..ccca92d
--- /dev/null
@@ -0,0 +1,13 @@
+       .data
+       .type rw,%object
+       .globl rw
+rw:
+       .dc.a 0
+       .size rw, . - rw
+
+       .section .data.rel.ro,"aw",%progbits
+       .type ro,%object
+       .globl ro
+ro:
+       .dc.a 0
+       .size ro, . - ro
This page took 0.039817 seconds and 4 git commands to generate.