[lld] 2539b4a - [LLD][ELF] Allow empty (.init|.preinit|.fini)_array to be RELRO

Peter Smith via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 31 04:55:27 PDT 2020


Author: Peter Smith
Date: 2020-03-31T12:53:12+01:00
New Revision: 2539b4ae4765b14cd7774e801ba12f0a200ef734

URL: https://github.com/llvm/llvm-project/commit/2539b4ae4765b14cd7774e801ba12f0a200ef734
DIFF: https://github.com/llvm/llvm-project/commit/2539b4ae4765b14cd7774e801ba12f0a200ef734.diff

LOG: [LLD][ELF] Allow empty (.init|.preinit|.fini)_array to be RELRO

The default GNU linker script uses the following idiom for the array
sections. I'll use .init_array here, but this also applies to
.preinit_array and .fini_array sections.

  .init_array    :
  {
    PROVIDE_HIDDEN (__init_array_start = .);
    KEEP (*(.init_array))
    PROVIDE_HIDDEN (__init_array_end = .);
  }

The C-library will take references to the _start and _end symbols to
process the array. This will make LLD keep the OutputSection even if there
are no .init_array sections. As the current check for RELRO uses the
section type for .init_array the above example with no .init_array
InputSections fails the checks as there are no .init_array sections to give
the OutputSection a type of SHT_INIT_ARRAY. This often leads to a
non-contiguous RELRO error message.

The simple fix is to a textual section match as well as a section type
match.

Differential Revision: https://reviews.llvm.org/D76915

Added: 
    lld/test/ELF/relro-init-fini-script.s

Modified: 
    lld/ELF/Writer.cpp

Removed: 
    


################################################################################
diff  --git a/lld/ELF/Writer.cpp b/lld/ELF/Writer.cpp
index 5dcffa560e1e..801ca7a0dc5e 100644
--- a/lld/ELF/Writer.cpp
+++ b/lld/ELF/Writer.cpp
@@ -819,7 +819,8 @@ static bool isRelroSection(const OutputSection *sec) {
   StringRef s = sec->name;
   return s == ".data.rel.ro" || s == ".bss.rel.ro" || s == ".ctors" ||
          s == ".dtors" || s == ".jcr" || s == ".eh_frame" ||
-         s == ".openbsd.randomdata";
+         s == ".fini_array" || s == ".init_array" ||
+         s == ".openbsd.randomdata" || s == ".preinit_array";
 }
 
 // We compute a rank for each section. The rank indicates where the

diff  --git a/lld/test/ELF/relro-init-fini-script.s b/lld/test/ELF/relro-init-fini-script.s
new file mode 100644
index 000000000000..ab7c928d49b9
--- /dev/null
+++ b/lld/test/ELF/relro-init-fini-script.s
@@ -0,0 +1,39 @@
+// REQUIRES: x86
+// RUN: llvm-mc -filetype=obj -triple=x86_64-none-elf %s -o %t.o
+// RUN: echo "SECTIONS { \
+// RUN:  .dynamic 0x10000 : { *(.dynamic) } \
+// RUN:  .preinit_array : { PROVIDE_HIDDEN (__preinit_array_start = .); \
+// RUN:                  KEEP (*(.preinit_array)) } \
+// RUN:  .init_array : { PROVIDE_HIDDEN (__init_array_start = .); \
+// RUN:                  KEEP (*(.init_array)) } \
+// RUN:  .fini_array : { PROVIDE_HIDDEN (__fini_array_start = .); \
+// RUN:                  KEEP (*(.fini_array)) } \
+// RUN:  .data.rel.ro : { *(.data.rel.ro) } \
+// RUN:  .data : { *(.data) } } " > %t.script
+// RUN: ld.lld %t.o -o %t.so --shared --script=%t.script
+// RUN: llvm-readelf -S %t.so | FileCheck %s
+// RUN: llvm-readobj --segments %t.so | FileCheck %s --check-prefix=PHDR
+
+/// Check that an empty .init_array, .fini_array or .preinit_array that is
+/// kept due to symbol references, is still counted as RELRO. The _array
+/// sections are zero size. The RELRO extent is [.dynamic, .data.rel.ro)
+
+// CHECK:      .dynamic       DYNAMIC         0000000000010000 002000 000110
+// CHECK-NEXT: .preinit_array PROGBITS        {{0+}}[[# %x,ADDR:]]
+// CHECK-NEXT: .init_array    PROGBITS        {{0+}}[[# ADDR]]
+// CHECK-NEXT: .fini_array    PROGBITS        {{0+}}[[# ADDR]]
+// CHECK-NEXT: .data.rel.ro   PROGBITS        0000000000010110 002110 000008
+
+// PHDR:      Type: PT_GNU_RELRO
+// PHDR-NEXT: Offset: 0x2000
+// PHDR-NEXT: VirtualAddress: 0x10000
+// PHDR-NEXT: PhysicalAddress: 0x10000
+// PHDR-NEXT: FileSize: 280
+ .section .data.rel.ro, "aw", %progbits
+ .global foo
+ .quad foo
+
+ .data
+ .quad __init_array_start
+ .quad __fini_array_start
+ .quad __preinit_array_start


        


More information about the llvm-commits mailing list