Link to crtend.S when PIE in the FreeBSD driver. Patch by Brooks Davis!


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@163388 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Driver/Tools.cpp b/lib/Driver/Tools.cpp
index d4a5372..1117202 100644
--- a/lib/Driver/Tools.cpp
+++ b/lib/Driver/Tools.cpp
@@ -5353,10 +5353,10 @@
 
   if (!Args.hasArg(options::OPT_nostdlib) &&
       !Args.hasArg(options::OPT_nostartfiles)) {
-    if (!Args.hasArg(options::OPT_shared))
-      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
-    else
+    if (Args.hasArg(options::OPT_shared) || Args.hasArg(options::OPT_pie))
       CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtendS.o")));
+    else
+      CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtend.o")));
     CmdArgs.push_back(Args.MakeArgString(ToolChain.GetFilePath("crtn.o")));
   }
 
diff --git a/test/Driver/pic.c b/test/Driver/pic.c
index 3952f85..636a449 100644
--- a/test/Driver/pic.c
+++ b/test/Driver/pic.c
@@ -23,6 +23,11 @@
 // CHECK-PIE2-NOT: "-mrelocation-model"
 // CHECK-PIE2: "-pie-level" "2"
 //
+// CHECK-PIE3: "{{.*}}ld"
+// CHECK-PIE3: "-pie"
+// CHECK-PIE3: "Scrt1.o" "crti.o" "crtbeginS.o"
+// CHECK-PIE3: "crtendS.o" "crtn.o"
+//
 // RUN: %clang -c %s -target i386-unknown-unknown -### 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
 // RUN: %clang -c %s -target i386-unknown-unknown -fpic -### 2>&1 \
@@ -70,6 +75,15 @@
 // RUN: %clang -c %s -target i386-apple-darwin -fno-PIC -### 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC
 //
+// Make sure -pie is passed to along to ld and that the right *crt* files
+// are linked in.
+// RUN: %clang %s -target i386-unknown-freebsd -fPIE -pie -### \
+// RUN: --sysroot=%S/Inputs/basic_freebsd_tree 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-PIE3
+// RUN: %clang %s -target i386-linux-gnu -fPIE -pie -### \
+// RUN: --sysroot=%S/Inputs/basic_linux_tree 2>&1 \
+// RUN:   | FileCheck %s --check-prefix=CHECK-PIE3
+//
 // Disregard any of the PIC-specific flags if we have a trump-card flag.
 // RUN: %clang -c %s -target i386-unknown-unknown -mkernel -fPIC -### 2>&1 \
 // RUN:   | FileCheck %s --check-prefix=CHECK-NO-PIC