diff --git a/clang/lib/Driver/ToolChains/Solaris.cpp b/clang/lib/Driver/ToolChains/Solaris.cpp --- a/clang/lib/Driver/ToolChains/Solaris.cpp +++ b/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"); diff --git a/clang/test/Driver/solaris-ld.c b/clang/test/Driver/solaris-ld.c --- a/clang/test/Driver/solaris-ld.c +++ b/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