[PATCH] D30530: [ELF] - Implemented -z text/-znotext

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 3 05:37:45 PST 2017


grimar updated this revision to Diff 90465.
grimar added a comment.

- Addressed review comments.


https://reviews.llvm.org/D30530

Files:
  ELF/Config.h
  ELF/Driver.cpp
  ELF/Relocations.cpp
  ELF/SyntheticSections.cpp
  test/ELF/ztext-text-notext.s


Index: test/ELF/ztext-text-notext.s
===================================================================
--- test/ELF/ztext-text-notext.s
+++ test/ELF/ztext-text-notext.s
@@ -0,0 +1,18 @@
+# REQUIRES: x86
+# RUN: llvm-mc -filetype=obj -triple=x86_64-pc-linux %s -o %t.o
+# RUN: ld.lld -z notext %t.o -o %t -shared
+# RUN: llvm-readobj  -dynamic-table -r %t | FileCheck %s
+
+# CHECK:      Relocations [
+# CHECK-NEXT:    Section {{.*}} .rela.dyn {
+# CHECK-NEXT:      0x1000 R_X86_64_RELATIVE - 0x1000
+# CHECK-NEXT:    }
+# CHECK-NEXT:  ]
+# CHECK: DynamicSection [
+# CHECK: 0x0000000000000016 TEXTREL 0x0
+
+# RUN: not ld.lld -z notext -z text %t.o -o %t -shared 2>&1 | FileCheck %s --check-prefix=ERR
+# ERR: can't create dynamic relocation R_X86_64_64 against local symbol in readonly segment
+
+foo:
+.quad foo
Index: ELF/SyntheticSections.cpp
===================================================================
--- ELF/SyntheticSections.cpp
+++ ELF/SyntheticSections.cpp
@@ -1106,6 +1106,8 @@
   add({DT_SYMENT, sizeof(Elf_Sym)});
   add({DT_STRTAB, In<ELFT>::DynStrTab});
   add({DT_STRSZ, In<ELFT>::DynStrTab->getSize()});
+  if (!Config->ZText)
+    add({DT_TEXTREL, (uint64_t)0});
   if (In<ELFT>::GnuHashTab)
     add({DT_GNU_HASH, In<ELFT>::GnuHashTab});
   if (In<ELFT>::HashTab)
Index: ELF/Relocations.cpp
===================================================================
--- ELF/Relocations.cpp
+++ ELF/Relocations.cpp
@@ -520,11 +520,12 @@
   // only memory. We can hack around it if we are producing an executable and
   // the refered symbol can be preemepted to refer to the executable.
   if (Config->Shared || (Config->pic() && !isRelExpr(Expr))) {
-    error(S.getLocation<ELFT>(RelOff) + ": can't create dynamic relocation " +
-          toString(Type) + " against " +
-          (Body.getName().empty() ? "local symbol in readonly segment"
-                                  : "symbol '" + toString(Body) + "'") +
-          " defined in " + toString(Body.File));
+    if (Config->ZText)
+      error(S.getLocation<ELFT>(RelOff) + ": can't create dynamic relocation " +
+            toString(Type) + " against " +
+            (Body.getName().empty() ? "local symbol in readonly segment"
+                                    : "symbol '" + toString(Body) + "'") +
+            " defined in " + toString(Body.File));
     return Expr;
   }
   if (Body.getVisibility() != STV_DEFAULT) {
Index: ELF/Driver.cpp
===================================================================
--- ELF/Driver.cpp
+++ ELF/Driver.cpp
@@ -267,6 +267,17 @@
   return false;
 }
 
+static bool getZTextOption(opt::InputArgList &Args) {
+  for (auto *Arg : llvm::reverse(Args.filtered(OPT_z))) {
+    StringRef S = Arg->getValue();
+    if (S == "text")
+      return true;
+    if (S == "notext")
+      return false;
+  }
+  return true;
+}
+
 static uint64_t getZOptionValue(opt::InputArgList &Args, StringRef Key,
                                 uint64_t Default) {
   for (auto *Arg : Args.filtered(OPT_z)) {
@@ -599,6 +610,7 @@
   Config->ZOrigin = hasZOption(Args, "origin");
   Config->ZRelro = !hasZOption(Args, "norelro");
   Config->ZStackSize = getZOptionValue(Args, "stack-size", 0);
+  Config->ZText = getZTextOption(Args);
   Config->ZWxneeded = hasZOption(Args, "wxneeded");
 
   if (Config->LTOO > 3)
Index: ELF/Config.h
===================================================================
--- ELF/Config.h
+++ ELF/Config.h
@@ -143,6 +143,7 @@
   bool ZNow;
   bool ZOrigin;
   bool ZRelro;
+  bool ZText;
   bool ExitEarly;
   bool ZWxneeded;
   DiscardPolicy Discard;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D30530.90465.patch
Type: text/x-patch
Size: 3587 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170303/79845bb2/attachment.bin>


More information about the llvm-commits mailing list