[lld] r304024 - [lld][ELF]Add option to make .dynamic read only

Petr Hosek via llvm-commits llvm-commits at lists.llvm.org
Fri May 26 12:12:38 PDT 2017


Author: phosek
Date: Fri May 26 14:12:38 2017
New Revision: 304024

URL: http://llvm.org/viewvc/llvm-project?rev=304024&view=rev
Log:
[lld][ELF]Add option to make .dynamic read only

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

Added:
    lld/trunk/test/ELF/Inputs/rodynamic.s
    lld/trunk/test/ELF/rodynamic.s
Modified:
    lld/trunk/ELF/Config.h
    lld/trunk/ELF/Driver.cpp
    lld/trunk/ELF/SyntheticSections.cpp

Modified: lld/trunk/ELF/Config.h
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Config.h?rev=304024&r1=304023&r2=304024&view=diff
==============================================================================
--- lld/trunk/ELF/Config.h (original)
+++ lld/trunk/ELF/Config.h Fri May 26 14:12:38 2017
@@ -145,6 +145,7 @@ struct Configuration {
   bool ZNow;
   bool ZOrigin;
   bool ZRelro;
+  bool ZRodynamic;
   bool ZText;
   bool ExitEarly;
   bool ZWxneeded;

Modified: lld/trunk/ELF/Driver.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/Driver.cpp?rev=304024&r1=304023&r2=304024&view=diff
==============================================================================
--- lld/trunk/ELF/Driver.cpp (original)
+++ lld/trunk/ELF/Driver.cpp Fri May 26 14:12:38 2017
@@ -692,6 +692,7 @@ void LinkerDriver::readConfigs(opt::Inpu
   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");

Modified: lld/trunk/ELF/SyntheticSections.cpp
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/ELF/SyntheticSections.cpp?rev=304024&r1=304023&r2=304024&view=diff
==============================================================================
--- lld/trunk/ELF/SyntheticSections.cpp (original)
+++ lld/trunk/ELF/SyntheticSections.cpp Fri May 26 14:12:38 2017
@@ -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 DynamicSectio
   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});
 }
 

Added: lld/trunk/test/ELF/Inputs/rodynamic.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/Inputs/rodynamic.s?rev=304024&view=auto
==============================================================================
--- lld/trunk/test/ELF/Inputs/rodynamic.s (added)
+++ lld/trunk/test/ELF/Inputs/rodynamic.s Fri May 26 14:12:38 2017
@@ -0,0 +1,4 @@
+.global foo
+.type foo, @function
+foo:
+  ret

Added: lld/trunk/test/ELF/rodynamic.s
URL: http://llvm.org/viewvc/llvm-project/lld/trunk/test/ELF/rodynamic.s?rev=304024&view=auto
==============================================================================
--- lld/trunk/test/ELF/rodynamic.s (added)
+++ lld/trunk/test/ELF/rodynamic.s Fri May 26 14:12:38 2017
@@ -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:   ]




More information about the llvm-commits mailing list