[PATCH] -fPIE -pie fails on FreeBSD

A colleague reported that on FreeBSD the following failed to
create a position independent executable:

echo "int main() { return 0; }" | clang -x c -fPIE -pie -

I've tracked down the problems and the following patch addresses them.
It adds -pie to the args to ld and links with the correct *crt*.o
archives.

The patch also switches the -static case to link with crtbeginT.o which
aligns with Linux and is what GCC does.

This patch also converges towards the style of the equivalent linux code
since the logic was cribbed from there.

-- Brooks

P.S. There the Linux code links with (S)crt1.o in the -pg case rather
than gcrt1.o as gcc does. I've not patched this case.

--- tools/clang/lib/Driver/Tools.cpp.orig
+++ tools/clang/lib/Driver/Tools.cpp
@@ -5153,7 +5184,9 @@
                                  const InputInfoList &Inputs,
                                  const ArgList &Args,
                                  const char *LinkingOutput) const {
- const Driver &D = getToolChain().getDriver();
+ const toolchains::Linux& ToolChain =
+ static_cast<const toolchains::Linux&>(getToolChain());
+ const Driver &D = ToolChain.getDriver();
   ArgStringList CmdArgs;

   // Silence warning for "clang -g foo.o -o foo"
@@ -5167,6 +5200,9 @@
   if (!D.SysRoot.empty())
     CmdArgs.push_back(Args.MakeArgString("--sysroot=" + D.SysRoot));

+ if (Args.hasArg(options::OPT_pie))
+ CmdArgs.push_back("-pie");

Here is a patch that adds tests for -fPIE -pie in the FreeBSD and Linux
cases to complement this change.

-- Brooks

Index: pic.c