clang currently links against /usr/lib/crt0.o when building
a binary with the -static -pie options. That makes such
programs segfault on startup (gcc 4.9 seems to have the same
problem).

When building a static PIE program, we need to link against
/usr/lib/rcrt0.o, as shown below. Bitrig has this part also
in their local llvm tree. I'll try to get it into upstream for
clang 3.9 as well.

--- tools/clang/lib/Driver/Tools.cpp.orig       Fri Feb 12 23:51:41 2016
+++ tools/clang/lib/Driver/Tools.cpp    Sun Jun 26 20:24:44 2016
@@ -7600,6 +7600,10 @@ void openbsd::Linker::ConstructJob(Compilation &C, con
       if (Args.hasArg(options::OPT_pg))
         CmdArgs.push_back(
             Args.MakeArgString(getToolChain().GetFilePath("gcrt0.o")));
+      else if (Args.hasArg(options::OPT_static) &&
+               !Args.hasArg(options::OPT_nopie))
+        CmdArgs.push_back(
+            Args.MakeArgString(getToolChain().GetFilePath("rcrt0.o")));
       else
         CmdArgs.push_back(
             Args.MakeArgString(getToolChain().GetFilePath("crt0.o")));

Full diff against the ports tree below.

ok?

Index: Makefile
===================================================================
RCS file: /cvs/ports/devel/llvm/Makefile,v
retrieving revision 1.114
diff -u -p -r1.114 Makefile
--- Makefile    24 May 2016 07:53:23 -0000      1.114
+++ Makefile    27 Jun 2016 17:24:32 -0000
@@ -11,7 +11,7 @@ COMMENT =     modular, fast C/C++/ObjC compi
 LLVM_V =       3.8.0
 DISTNAME =     llvm-${LLVM_V}.src
 PKGNAME =      llvm-${LLVM_V}
-REVISION =     1
+REVISION =     2
 CATEGORIES =   devel
 DISTFILES =    llvm-${LLVM_V}.src${EXTRACT_SUFX} \
                cfe-${LLVM_V}.src${EXTRACT_SUFX}
Index: patches/patch-tools_clang_lib_Driver_Tools_cpp
===================================================================
RCS file: /cvs/ports/devel/llvm/patches/patch-tools_clang_lib_Driver_Tools_cpp,v
retrieving revision 1.30
diff -u -p -r1.30 patch-tools_clang_lib_Driver_Tools_cpp
--- patches/patch-tools_clang_lib_Driver_Tools_cpp      24 May 2016 07:53:23 
-0000      1.30
+++ patches/patch-tools_clang_lib_Driver_Tools_cpp      27 Jun 2016 17:24:32 
-0000
@@ -1,6 +1,5 @@
-$OpenBSD: patch-tools_clang_lib_Driver_Tools_cpp,v 1.30 2016/05/24 07:53:23 
ajacoutot Exp $
---- tools/clang/lib/Driver/Tools.cpp.orig      Fri Feb 12 17:51:41 2016
-+++ tools/clang/lib/Driver/Tools.cpp   Tue May 17 14:45:22 2016
+--- tools/clang/lib/Driver/Tools.cpp.orig      Fri Feb 12 23:51:41 2016
++++ tools/clang/lib/Driver/Tools.cpp   Sun Jun 26 20:24:44 2016
 @@ -78,7 +78,7 @@ static const char *getSparcAsmModeForCPU(StringRef Nam
            .Case("niagara2", "-Av9b")
            .Case("niagara3", "-Av9d")
@@ -10,7 +9,18 @@ $OpenBSD: patch-tools_clang_lib_Driver_T
    } else {
      return llvm::StringSwitch<const char *>(Name)
            .Case("v8", "-Av8")
-@@ -7611,15 +7611,17 @@ void openbsd::Linker::ConstructJob(Compilation &C, con
+@@ -7600,6 +7600,10 @@ void openbsd::Linker::ConstructJob(Compilation &C, con
+       if (Args.hasArg(options::OPT_pg))
+         CmdArgs.push_back(
+             Args.MakeArgString(getToolChain().GetFilePath("gcrt0.o")));
++      else if (Args.hasArg(options::OPT_static) &&
++               !Args.hasArg(options::OPT_nopie))
++        CmdArgs.push_back(
++            Args.MakeArgString(getToolChain().GetFilePath("rcrt0.o")));
+       else
+         CmdArgs.push_back(
+             Args.MakeArgString(getToolChain().GetFilePath("crt0.o")));
+@@ -7611,15 +7615,17 @@ void openbsd::Linker::ConstructJob(Compilation &C, con
      }
    }
  

Reply via email to