Always add a .note.GNU-stack section if -r.
authorRui Ueyama <ruiu@google.com>
Wed, 29 Aug 2018 07:27:09 +0000 (07:27 +0000)
committerRui Ueyama <ruiu@google.com>
Wed, 29 Aug 2018 07:27:09 +0000 (07:27 +0000)
With this patch, lld creates a .note.GNU_stack and adds that to an
output file if it is creating a re-linkable object file (i.e. if -r
is given). If we don't do this, and if you use GNU linkers as a final
linker, they create an executable whose stack area is executable,
which is considered pretty bad these days.

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

llvm-svn: 340902

lld/ELF/SyntheticSections.h
lld/ELF/Writer.cpp
lld/test/ELF/relocatable-bss.s
lld/test/ELF/relocatable-comdat-multiple.s
lld/test/ELF/relocatable-comdat.s
lld/test/ELF/relocatable-comdat2.s
lld/test/ELF/relocatable-many-sections.s
lld/test/ELF/relocatable.s

index a9ee134..a14e831 100644 (file)
@@ -135,6 +135,15 @@ protected:
   uint64_t Size = 0;
 };
 
+// .note.GNU-stack section.
+class GnuStackSection : public SyntheticSection {
+public:
+  GnuStackSection()
+      : SyntheticSection(0, llvm::ELF::SHT_PROGBITS, 1, ".note.GNU-stack") {}
+  void writeTo(uint8_t *Buf) override {}
+  size_t getSize() const override { return 0; }
+};
+
 // .note.gnu.build-id section.
 class BuildIdSection : public SyntheticSection {
   // First 16 bytes are a header.
index 90462ec..ee18892 100644 (file)
@@ -399,6 +399,14 @@ template <class ELFT> static void createSyntheticSections() {
   InX::Iplt = make<PltSection>(true);
   Add(InX::Iplt);
 
+  // .note.GNU-stack is always added when we are creating a re-linkable
+  // object file. Other linkers are using the presence of this marker
+  // section to control the executable-ness of the stack area, but that
+  // is irrelevant these days. Stack area should always be non-executable
+  // by default. So we emit this section unconditionally.
+  if (Config->Relocatable)
+    Add(make<GnuStackSection>());
+
   if (!Config->Relocatable) {
     if (Config->EhFrameHdr) {
       InX::EhFrameHdr = make<EhFrameHeader>();
index becfbae..716eb4a 100644 (file)
@@ -20,7 +20,7 @@
 # CHECK-NEXT:  Version:
 # CHECK-NEXT:  Entry:
 # CHECK-NEXT:   ProgramHeaderOffset:
-# CHECK-NEXT:   SectionHeaderOffset: 0xD8
+# CHECK-NEXT:   SectionHeaderOffset: 0xE8
 # CHECK-NEXT:  Flags [
 # CHECK-NEXT:  ]
 # CHECK-NEXT:  HeaderSize:
index bb7a784..6a8d833 100644 (file)
@@ -8,7 +8,7 @@
 # CHECK-NEXT:   Group {
 # CHECK-NEXT:     Name: .group
 # CHECK-NEXT:     Index: 2
-# CHECK-NEXT:     Link: 8
+# CHECK-NEXT:     Link: 9
 # CHECK-NEXT:     Info: 1
 # CHECK-NEXT:     Type: COMDAT
 # CHECK-NEXT:     Signature: aaa
@@ -20,7 +20,7 @@
 # CHECK-NEXT:   Group {
 # CHECK-NEXT:     Name: .group
 # CHECK-NEXT:     Index: 5
-# CHECK-NEXT:     Link: 8
+# CHECK-NEXT:     Link: 9
 # CHECK-NEXT:     Info: 6
 # CHECK-NEXT:     Type: COMDAT
 # CHECK-NEXT:     Signature: bbb
index 11aa30c..98ef993 100644 (file)
@@ -30,7 +30,7 @@
 # CHECK-NEXT:    Group {
 # CHECK-NEXT:      Name: .group
 # CHECK-NEXT:      Index: 2
-# CHECK-NEXT:      Link: 5
+# CHECK-NEXT:      Link: 6
 # CHECK-NEXT:      Info: 1 
 # CHECK-NEXT:      Type: COMDAT
 # CHECK-NEXT:      Signature: abc
index 27844fe..3318fb6 100644 (file)
@@ -13,7 +13,7 @@
 # CHECK-NEXT:   Group {
 # CHECK-NEXT:     Name: .group
 # CHECK-NEXT:     Index: 2
-# CHECK-NEXT:     Link: 7
+# CHECK-NEXT:     Link: 8
 # CHECK-NEXT:     Info: 1
 # CHECK-NEXT:     Type: COMDAT
 # CHECK-NEXT:     Signature: bar
@@ -24,7 +24,7 @@
 # CHECK-NEXT:   Group {
 # CHECK-NEXT:     Name: .group
 # CHECK-NEXT:     Index: 4
-# CHECK-NEXT:     Link: 7
+# CHECK-NEXT:     Link: 8
 # CHECK-NEXT:     Info: 2
 # CHECK-NEXT:     Type: COMDAT
 # CHECK-NEXT:     Signature: zed
index 347f3f7..0d84c6b 100644 (file)
@@ -9,8 +9,8 @@
 ## sections amount is greater than SHN_LORESERVE.
 # RUN: llvm-readobj -file-headers %t | FileCheck %s --check-prefix=HDR
 # HDR:      ElfHeader {
-# HDR:        SectionHeaderCount: 0 (65543)
-# HDR-NEXT:   StringTableSectionIndex: 65535 (65541)
+# HDR:        SectionHeaderCount: 0 (65544)
+# HDR-NEXT:   StringTableSectionIndex: 65535 (65542)
 
 ## Check that:
 ## 1) 65541 is the index of .shstrtab section.
 ## 3) .symtab_shndxr entry size and alignment == 4.
 ## 4) .symtab_shndxr has size equal to
 ##    (sizeof(.symtab) / entsize(.symtab)) * entsize(.symtab_shndxr) = 0x4 * 0x180048 / 0x18 == 0x04000c
+
 # RUN: llvm-readelf -sections -symbols %t | FileCheck %s
-##              [Nr]    Name           Type                   Address          Off    Size   ES Flg  Lk    Inf    Al
-# CHECK:        [65538] .bar
-# CHECK-NEXT:   [65539] .symtab        SYMTAB                 0000000000000000 000040 180078 18      65542 65539  8
-# CHECK-NEXT:   [65540] .symtab_shndxr SYMTAB SECTION INDICES 0000000000000000 1800b8 040014 04      65539 0      4
-# CHECK-NEXT:   [65541] .shstrtab      STRTAB                 0000000000000000 1c00cc 0f0035 00      0     0      1
-# CHECK-NEXT:   [65542] .strtab        STRTAB                 0000000000000000 2b0101 00000c 00
+#        [Nr] Name              Type            Address          Off    Size   ES Flg Lk Inf Al
+# CHECK: [65539] .note.GNU-stack PROGBITS       0000000000000000 000040 000000 00      0   0  1
+# CHECK: [65540] .symtab        SYMTAB          0000000000000000 000040 180078 18     65543 65539  8
+# CHECK: [65541] .symtab_shndxr SYMTAB SECTION INDICES 0000000000000000 1800b8 040014 04     65540   0  4
+# CHECK: [65542] .shstrtab      STRTAB          0000000000000000 1c00cc 0f0045 00      0   0  1
+# CHECK: [65543] .strtab        STRTAB          0000000000000000 2b0111 00000c 00      0   0  1
+
 # 5) Check we are able to represent symbol foo with section (.bar) index  > 0xFF00 (SHN_LORESERVE).
 # CHECK: GLOBAL DEFAULT  65538 foo
 
index 7cb2a08..7235ea0 100644 (file)
@@ -4,6 +4,7 @@
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %p/Inputs/relocatable2.s -o %t3.o
 # RUN: ld.lld -r %t1.o %t2.o %t3.o -o %t
 # RUN: llvm-readobj -file-headers -sections -program-headers -symbols -r %t | FileCheck %s
+# RUN: llvm-objdump -section-headers %t | FileCheck -check-prefix=SECTION %s
 # RUN: llvm-objdump -s -d %t | FileCheck -check-prefix=CHECKTEXT %s
 
 ## Test --relocatable alias
@@ -37,8 +38,8 @@
 # CHECK-NEXT:  ProgramHeaderEntrySize: 0
 # CHECK-NEXT:  ProgramHeaderCount: 0
 # CHECK-NEXT:  SectionHeaderEntrySize: 64
-# CHECK-NEXT:  SectionHeaderCount: 7
-# CHECK-NEXT:  StringTableSectionIndex: 5
+# CHECK-NEXT:  SectionHeaderCount: 8
+# CHECK-NEXT:  StringTableSectionIndex: 6
 # CHECK-NEXT:  }
 
 # CHECK:       Relocations [
 # CHECK-NEXT:    0x4E R_X86_64_32S yyy 0x0
 # CHECK-NEXT:  }
 
+# SECTION: Sections:
+# SECTION: Idx Name          Size      Address          Type
+# SECTION:   0               00000000 0000000000000000
+# SECTION:   1 .text         00000056 0000000000000000 TEXT
+# SECTION:   2 .rela.text    00000090 0000000000000000
+# SECTION:   3 .bss          00000018 0000000000000000 BSS
+# SECTION:   4 .note.GNU-stack 00000000 0000000000000000
+# SECTION:   5 .symtab       00000168 0000000000000000
+# SECTION:   6 .shstrtab     00000041 0000000000000000
+# SECTION:   7 .strtab       0000002d 0000000000000000
+
 # CHECKTEXT:      Disassembly of section .text:
 # CHECKTEXT-NEXT: main:
 # CHECKTEXT-NEXT: 0: c7 04 25 00 00 00 00 05 00 00 00 movl $5, 0