[PATCH] D158206: [Driver] Add PIE support on Solaris

Rainer Orth via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Aug 17 12:37:36 PDT 2023


ro created this revision.
ro added a reviewer: MaskRay.
ro added a project: clang.
Herald added subscribers: pengfei, fedor.sergeev, jyknight.
Herald added a project: All.
ro requested review of this revision.

`clang` currently lacks PIE support on Solaris.  This patch fixes this.

Tested on `amd64-pc-solaris2.11`, `sparcv9-sun-solaris2.11`, and `x86_64-pc-linux-gnu`.

I'll submit a follow-up patch to make use of `crtbeginS.o` and `crtendS.o` shortly.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D158206

Files:
  clang/lib/Driver/ToolChains/Solaris.cpp
  clang/test/Driver/solaris-ld.c


Index: clang/test/Driver/solaris-ld.c
===================================================================
--- clang/test/Driver/solaris-ld.c
+++ clang/test/Driver/solaris-ld.c
@@ -106,6 +106,20 @@
 // CHECK-SPARC32-SHARED-NOT: "-lgcc"
 // CHECK-SPARC32-SHARED-NOT: "-lm"
 
+// Check the right ld flags are present with -pie.
+// RUN: %clang -### %s -pie 2>&1 \
+// RUN:     --target=sparc-sun-solaris2.11 \
+// RUN:     --gcc-toolchain="" \
+// RUN:     --sysroot=%S/Inputs/solaris_sparc_tree \
+// RUN:   | FileCheck --check-prefix=CHECK-PIE %s
+// RUN: %clang -### %s -nopie 2>&1 \
+// RUN:     --target=sparc-sun-solaris2.11 \
+// RUN:     --gcc-toolchain="" \
+// RUN:     --sysroot=%S/Inputs/solaris_sparc_tree \
+// RUN:   | FileCheck --check-prefix=CHECK-NOPIE %s
+// CHECK-PIE: "-ztype=pie"
+// CHECK-NOPIE-NOT: "-ztype=pie'
+
 // -r suppresses default -l and crt*.o, values-*.o like -nostdlib.
 // RUN: %clang -### %s --target=sparc-sun-solaris2.11 -r 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CHECK-RELOCATABLE
Index: clang/lib/Driver/ToolChains/Solaris.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Solaris.cpp
+++ clang/lib/Driver/ToolChains/Solaris.cpp
@@ -52,6 +52,9 @@
                                    const InputInfoList &Inputs,
                                    const ArgList &Args,
                                    const char *LinkingOutput) const {
+  const bool IsPIE =
+      !Args.hasArg(options::OPT_shared) &&
+      (Args.hasArg(options::OPT_pie) || getToolChain().isPIEDefault(Args));
   ArgStringList CmdArgs;
 
   // Demangle C++ names in errors
@@ -62,6 +65,9 @@
     CmdArgs.push_back("_start");
   }
 
+  if (IsPIE)
+    CmdArgs.push_back("-ztype=pie");
+
   if (Args.hasArg(options::OPT_static)) {
     CmdArgs.push_back("-Bstatic");
     CmdArgs.push_back("-dn");


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D158206.551226.patch
Type: text/x-patch
Size: 1870 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230817/6c36b720/attachment-0001.bin>


More information about the cfe-commits mailing list