[cfe-dev] [PATCH] -fPIE -pie fails on FreeBSD

Brooks Davis brooks at freebsd.org
Tue Aug 28 06:21:42 PDT 2012


On Mon, Aug 27, 2012 at 11:43:00AM -0500, Brooks Davis wrote:
> 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.

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

-- Brooks

Index: pic.c
===================================================================
--- pic.c	(revision 162742)
+++ pic.c	(working copy)
@@ -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 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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 188 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20120828/493c94c5/attachment.sig>


More information about the cfe-dev mailing list