[PATCH] D25676: [ELF] - Add support for -nopie

George Rimar via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 17 07:14:26 PDT 2016


grimar created this revision.
grimar added reviewers: ruiu, rafael.
grimar added subscribers: llvm-commits, grimar, evgeny777.

This is https://llvm.org/bugs/show_bug.cgi?id=30696,

in compare with custom ld that OpenBSD uses, we do not have -pie by default.
That means -nopie can be implemented as noop in lld in theory.

At the same time looks if OpenBSD uses -pie as default we might want to support easy switch for them.
That what this patch do, it introduces way where change of single constant allows to select the default value 
and -pie/-nopie options are also supported.


https://reviews.llvm.org/D25676

Files:
  ELF/Config.h
  ELF/Driver.cpp
  ELF/Options.td
  test/ELF/pie.s


Index: test/ELF/pie.s
===================================================================
--- test/ELF/pie.s
+++ test/ELF/pie.s
@@ -1,5 +1,12 @@
 # REQUIRES: x86
 # RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t1.o
+
+## Default is no PIE.
+# RUN: ld.lld %t1.o -o %t
+# RUN: llvm-readobj -file-headers -sections -program-headers -symbols -r %t \
+# RUN:   | FileCheck %s --check-prefix=NOPIE
+
+## Check -pie.
 # RUN: ld.lld -pie %t1.o -o %t
 # RUN: llvm-readobj -file-headers -sections -program-headers -symbols -r %t | FileCheck %s
 
@@ -98,5 +105,13 @@
 # CHECK-NEXT:    Alignment: 8
 # CHECK-NEXT:  }
 
+## Check -nopie
+# RUN: ld.lld -nopie %t1.o -o %t2
+# RUN: llvm-readobj -file-headers -r %t2 | FileCheck %s --check-prefix=NOPIE
+# NOPIE-NOT: Type: SharedObject
+
+# RUN: not ld.lld -pie -nopie %t1.o -o %t2 2>&1 | FileCheck %s --check-prefix=ERR
+# ERR: -pie can not be used with -nopie
+
 .globl _start
 _start:
Index: ELF/Options.td
===================================================================
--- ELF/Options.td
+++ ELF/Options.td
@@ -127,6 +127,8 @@
 def noinhibit_exec: F<"noinhibit-exec">,
   HelpText<"Retain the executable output file whenever it is still usable">;
 
+def nopie: F<"nopie">, HelpText<"Do not create a position independent executable">;
+
 def no_undefined: F<"no-undefined">,
   HelpText<"Report unresolved symbols even if the linker is creating a shared library">;
 
Index: ELF/Driver.cpp
===================================================================
--- ELF/Driver.cpp
+++ ELF/Driver.cpp
@@ -421,6 +421,14 @@
   return SortSectionPolicy::Default;
 }
 
+static bool isOutputPie(opt::InputArgList &Args) {
+  if (Args.hasArg(OPT_nopie) && Args.hasArg(OPT_pie))
+    error("-pie can not be used with -nopie");
+  if (!Args.hasArg(OPT_nopie) && !Args.hasArg(OPT_pie))
+    return Config->Pie;
+  return Args.hasArg(OPT_pie);
+}
+
 // Initializes Config members by the command line options.
 void LinkerDriver::readConfigs(opt::InputArgList &Args) {
   for (auto *Arg : Args.filtered(OPT_L))
@@ -456,7 +464,7 @@
   Config->NoGnuUnique = Args.hasArg(OPT_no_gnu_unique);
   Config->NoUndefinedVersion = Args.hasArg(OPT_no_undefined_version);
   Config->Nostdlib = Args.hasArg(OPT_nostdlib);
-  Config->Pie = Args.hasArg(OPT_pie);
+  Config->Pie = isOutputPie(Args);
   Config->PrintGcSections = Args.hasArg(OPT_print_gc_sections);
   Config->Relocatable = Args.hasArg(OPT_relocatable);
   Config->SaveTemps = Args.hasArg(OPT_save_temps);
Index: ELF/Config.h
===================================================================
--- ELF/Config.h
+++ ELF/Config.h
@@ -107,7 +107,7 @@
   bool Nostdlib;
   bool OFormatBinary;
   bool Pic;
-  bool Pie;
+  bool Pie = false;
   bool PrintGcSections;
   bool Rela;
   bool Relocatable;


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D25676.74840.patch
Type: text/x-patch
Size: 2851 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20161017/a11895a9/attachment.bin>


More information about the llvm-commits mailing list