[lld][ELF]Add option to make .dynamic read only
authorPetr Hosek <phosek@chromium.org>
Fri, 26 May 2017 19:12:38 +0000 (19:12 +0000)
committerPetr Hosek <phosek@chromium.org>
Fri, 26 May 2017 19:12:38 +0000 (19:12 +0000)
The .dynamic section of an ELF almost doesn't need to be written to with
the exception of the DT_DEBUG entry. For several reasons having a read
only .dynamic section would be useful. This change adds the -z keyword
"rodynamic" which forces .dynamic to be read-only. In this case DT_DEBUG
will not be emited.

Patch by Jake Ehrlich

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

llvm-svn: 304024

lld/ELF/Config.h
lld/ELF/Driver.cpp
lld/ELF/SyntheticSections.cpp
lld/test/ELF/Inputs/rodynamic.s [new file with mode: 0644]
lld/test/ELF/rodynamic.s [new file with mode: 0644]

index 57a0e5a..54f6dc2 100644 (file)
@@ -145,6 +145,7 @@ struct Configuration {
   bool ZNow;
   bool ZOrigin;
   bool ZRelro;
+  bool ZRodynamic;
   bool ZText;
   bool ExitEarly;
   bool ZWxneeded;
index a15e88b..3254044 100644 (file)
@@ -692,6 +692,7 @@ void LinkerDriver::readConfigs(opt::InputArgList &Args) {
   Config->ZNow = hasZOption(Args, "now");
   Config->ZOrigin = hasZOption(Args, "origin");
   Config->ZRelro = !hasZOption(Args, "norelro");
+  Config->ZRodynamic = hasZOption(Args, "rodynamic");
   Config->ZStackSize = getZOptionValue(Args, "stack-size", 0);
   Config->ZText = !hasZOption(Args, "notext");
   Config->ZWxneeded = hasZOption(Args, "wxneeded");
index ba2d437..d3db326 100644 (file)
@@ -1005,10 +1005,11 @@ DynamicSection<ELFT>::DynamicSection()
                        ".dynamic") {
   this->Entsize = ELFT::Is64Bits ? 16 : 8;
 
-  // .dynamic section is not writable on MIPS.
+  // .dynamic section is not writable on MIPS and on Fuchsia OS
+  // which passes -z rodynamic.
   // See "Special Section" in Chapter 4 in the following document:
   // ftp://www.linux-mips.org/pub/linux/mips/doc/ABI/mipsabi.pdf
-  if (Config->EMachine == EM_MIPS)
+  if (Config->EMachine == EM_MIPS || Config->ZRodynamic)
     this->Flags = SHF_ALLOC;
 
   addEntries();
@@ -1053,7 +1054,15 @@ template <class ELFT> void DynamicSection<ELFT>::addEntries() {
   if (DtFlags1)
     add({DT_FLAGS_1, DtFlags1});
 
-  if (!Config->Shared && !Config->Relocatable)
+  // DT_DEBUG is a pointer to debug informaion used by debuggers at runtime. We
+  // need it for each process, so we don't write it for DSOs. The loader writes
+  // the pointer into this entry.
+  //
+  // DT_DEBUG is the only .dynamic entry that needs to be written to. Some
+  // systems (currently only Fuchsia OS) provide other means to give the
+  // debugger this information. Such systems may choose make .dynamic read-only.
+  // If the target is such a system (used -z rodynamic) don't write DT_DEBUG.
+  if (!Config->Shared && !Config->Relocatable && !Config->ZRodynamic)
     add({DT_DEBUG, (uint64_t)0});
 }
 
diff --git a/lld/test/ELF/Inputs/rodynamic.s b/lld/test/ELF/Inputs/rodynamic.s
new file mode 100644 (file)
index 0000000..a06f4be
--- /dev/null
@@ -0,0 +1,4 @@
+.global foo
+.type foo, @function
+foo:
+  ret
diff --git a/lld/test/ELF/rodynamic.s b/lld/test/ELF/rodynamic.s
new file mode 100644 (file)
index 0000000..441e5c3
--- /dev/null
@@ -0,0 +1,35 @@
+# RUN: llvm-mc %s -o %t.o -filetype=obj -triple=x86_64-pc-linux
+# RUN: llvm-mc %p/Inputs/rodynamic.s -o %t.so.o -filetype=obj -triple=x86_64-pc-linux
+
+# RUN: ld.lld -shared %t.so.o -o %t.so
+# RUN: ld.lld %t.o %t.so -o %t.exe
+# RUN: llvm-readobj -dynamic-table %t.exe | FileCheck -check-prefix=DEFDEBUG %s
+# RUN: llvm-readobj -sections %t.exe | FileCheck -check-prefix=DEFSEC %s
+
+# RUN: ld.lld -shared -z rodynamic %t.so.o -o %t.so
+# RUN: ld.lld -z rodynamic %t.o %t.so -o %t.exe
+# RUN: llvm-readobj -dynamic-table %t.exe | FileCheck -check-prefix=RODEBUG %s
+# RUN: llvm-readobj -sections %t.exe | FileCheck -check-prefix=ROSEC %s
+
+.globl _start
+_start:
+  call foo
+
+# DEFDEBUG: DEBUG
+
+# DEFSEC:      Section {
+# DEFSEC:        Name: .dynamic
+# DEFSEC-NEXT:   Type: SHT_DYNAMIC
+# DEFSEC-NEXT:   Flags [
+# DEFSEC-NEXT:     SHF_ALLOC
+# DEFSEC-NEXT:     SHF_WRITE
+# DEFSEC-NEXT:   ]
+
+# RODEBUG-NOT: DEBUG
+
+# ROSEC:      Section {
+# ROSEC:        Name: .dynamic
+# ROSEC-NEXT:   Type: SHT_DYNAMIC
+# ROSEC-NEXT:   Flags [
+# ROSEC-NEXT:     SHF_ALLOC
+# ROSEC-NEXT:   ]