summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoakim Sindholt <opensource@zhasha.com>2015-06-05 21:47:36 +0200
committerJoakim Sindholt <opensource@zhasha.com>2015-06-05 21:47:36 +0200
commit3e82275d9de881807c622fef3b58ac4fd55e289c (patch)
treea4153a834871485081530b3b79ab7c9a4e81b9b7
parent7d35f0bb17a4ee34f17aceaac51e904363948e57 (diff)
switch to proper local-exec TLS model
This entails a lot of Bad Stuff™ because the toolchain doesn't actually support -static -pie.
-rw-r--r--bin/build-libc.sh2
-rw-r--r--bin/nuke-pt_interp.pl33
-rw-r--r--bin/p9c.sh3
-rw-r--r--bin/p9l.sh40
-rw-r--r--src/clang_linux_builtin_386.mk4
-rw-r--r--src/clang_linux_builtin_amd64.mk4
-rw-r--r--src/clang_linux_builtin_arm.mk4
7 files changed, 61 insertions, 29 deletions
diff --git a/bin/build-libc.sh b/bin/build-libc.sh
index d69faa1..b193690 100644
--- a/bin/build-libc.sh
+++ b/bin/build-libc.sh
@@ -49,7 +49,7 @@ Linux)
cd "${SRCDIR}/musl" && \
make distclean && \
CC="clang --target=${CLANGARCH} -integrated-as" \
- CFLAGS="-g -Os -pipe -fPIC" \
+ CFLAGS="-g -Os -pipe -fPIE -ftls-model=local-exec" \
GNUTARGET="${CLANGARCH}" ./configure \
--target="${MUSLARCH}" \
--prefix="${OBJDIR}" \
diff --git a/bin/nuke-pt_interp.pl b/bin/nuke-pt_interp.pl
new file mode 100644
index 0000000..be5336c
--- /dev/null
+++ b/bin/nuke-pt_interp.pl
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+use strict;
+
+my $fname = $ARGV[0];
+open(F,"+<$fname") or die "Couldn't open $fname for reading+writing";
+
+my $buf;
+read(F, $buf, 6);
+my ($width, $endian) = unpack("x4CC", $buf);
+if (($width != 1 && $width != 2) ||
+ ($endian != 1 && $endian != 2)) { die "Invalid EI_CLASS or EI_DATA"; }
+
+my $offset;
+if ($width == 1) {
+ seek(F, 0x2A, 0);
+ $offset = 0x34;
+} else {
+ seek(F, 0x36, 0);
+ $offset = 0x40;
+}
+
+read(F, $buf, 4);
+my ($phentsize, $phnum) = unpack($endian == 1 ? "vv" : "nn", $buf);
+
+for (my $i = 0; $i < $phnum; $i++) {
+ seek(F, $offset + $phentsize * $i, 0);
+ read(F, $buf, 4);
+ if (unpack($endian == 1 ? "V" : "N", $buf) == 3) {
+ seek(F, -4, 1);
+ print F pack("N", 0);
+ }
+}
+close F;
diff --git a/bin/p9c.sh b/bin/p9c.sh
index 318ff3d..ecec012 100644
--- a/bin/p9c.sh
+++ b/bin/p9c.sh
@@ -182,7 +182,8 @@ do
fi
clang \
- "$TARG" -pipe -std=c11 -g -fPIC -fdata-sections -ffunction-sections \
+ "$TARG" -pipe -std=c11 -g -fPIE -ftls-model=local-exec \
+ -fdata-sections -ffunction-sections \
-D_POSIX_SOURCE=1 -D_POSIX_C_SOURCE=200809L -D_BSD_SOURCE \
-D_XOPEN_SOURCE -Wdeclaration-after-statement \
-I"${DIR}/../include" -I. -include "${DIR}/../src/visibility.h" \
diff --git a/bin/p9l.sh b/bin/p9l.sh
index 90fe953..3f758c3 100644
--- a/bin/p9l.sh
+++ b/bin/p9l.sh
@@ -175,27 +175,25 @@ CRTEND=()
if [ -e "${LIBDIR}/crti.o" ]; then CRTSTART+=("${LIBDIR}/crti.o"); fi
if [ -e "${LIBDIR}/rcrt1.o" ]; then CRTSTART+=("${LIBDIR}/rcrt1.o"); fi
if [ -e "${LIBDIR}/crtn.o" ]; then CRTEND+=("${LIBDIR}/crtn.o"); fi
-if [[ ${#CRTSTART[@]} -eq 0 && ${#CRTEND[@]} -eq 0 ]]
-then
- clang \
- -Wl,-shared,-Bstatic,-Bsymbolic,--gc-sections,-X,-e,"${START_SYM}" \
- "${OPTS[@]}" -o "${OUTPUT_NAME}" -pipe "${FARGS[@]}" "${LIBS[@]}"
- if [ $? -ne 0 ]; then exit 1; fi
-else
- LTOOPTS=()
- #if [ $USE_LTO == true ]
- #then
- # LTOOPTS+=("--plugin")
- # LTOOPTS+=("$(clang -print-file-name=LLVMgold.so)")
- # LTOOPTS+=("--plugin-opt=-march=${MARCH}")
- #fi
- ld.gold \
- --gc-sections -shared -Bstatic -Bsymbolic -X -e "${START_SYM}" \
- -nostdlib "${LTOOPTS[@]}" -L"${LIBDIR}" "${OPTS[@]}" -o "${OUTPUT_NAME}" \
- "${CRTSTART[@]}" '-(' "${FARGS[@]}" "${LIBS[@]}" '-)' "${CRTEND[@]}" \
- -lc -lrt -lpthread -lcompiler_rt "${GLOBALLIBS[@]}"
- if [ $? -ne 0 ]; then exit 1; fi
-fi
+
+LTOOPTS=()
+#if [ $USE_LTO == true ]
+#then
+# LTOOPTS+=("--plugin")
+# LTOOPTS+=("$(clang -print-file-name=LLVMgold.so)")
+# LTOOPTS+=("--plugin-opt=-march=${MARCH}")
+#fi
+ld.gold \
+ --gc-sections -pie -Bstatic -Bsymbolic -E -X -e "${START_SYM}" \
+ -z now -z nodlopen -z nodump -z nodelete -z noexecstack -z text \
+ -nostdlib "${LTOOPTS[@]}" -L"${LIBDIR}" "${OPTS[@]}" -o "${OUTPUT_NAME}" \
+ "${CRTSTART[@]}" '-(' "${FARGS[@]}" "${LIBS[@]}" '-)' "${CRTEND[@]}" \
+ -lc -lrt -lpthread -lcompiler_rt "${GLOBALLIBS[@]}"
+if [ $? -ne 0 ]; then exit 1; fi
+
+perl -- "${DIR}/nuke-pt_interp.pl" "${OUTPUT_NAME}"
+if [ $? -ne 0 ]; then exit 1; fi
+objcopy -R .interp "${OUTPUT_FILE}" &>/dev/null
if [ $USE_STRIP == true ]
then
diff --git a/src/clang_linux_builtin_386.mk b/src/clang_linux_builtin_386.mk
index c22387c..0ce45a7 100644
--- a/src/clang_linux_builtin_386.mk
+++ b/src/clang_linux_builtin_386.mk
@@ -6,8 +6,8 @@ CC := clang
Arch := unknown
Configs := builtins-i386
-CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer -fPIC -integrated-as \
- --sysroot=$(ProjSrcRoot)/../../386 -isystem /include
+CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer -fPIE -ftls-model=local-exec \
+ -integrated-as --sysroot=$(ProjSrcRoot)/../../386 -isystem /include
Arch.builtins-i386 := i686
CFLAGS.builtins-i386 := $(CFLAGS) -target i686-linux-gnu
diff --git a/src/clang_linux_builtin_amd64.mk b/src/clang_linux_builtin_amd64.mk
index 83ec498..3505bed 100644
--- a/src/clang_linux_builtin_amd64.mk
+++ b/src/clang_linux_builtin_amd64.mk
@@ -6,8 +6,8 @@ CC := clang
Arch := unknown
Configs := builtins-x86_64
-CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer -fPIC -integrated-as \
- --sysroot=$(ProjSrcRoot)/../../amd64 -isystem /include
+CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer -fPIE -ftls-model=local-exec \
+ -integrated-as --sysroot=$(ProjSrcRoot)/../../amd64 -isystem /include
Arch.builtins-x86_64 := x86_64
CFLAGS.builtins-x86_64 := $(CFLAGS) -target x86_64-linux-gnu
diff --git a/src/clang_linux_builtin_arm.mk b/src/clang_linux_builtin_arm.mk
index 6edc134..c4cf2a2 100644
--- a/src/clang_linux_builtin_arm.mk
+++ b/src/clang_linux_builtin_arm.mk
@@ -6,8 +6,8 @@ CC := clang
Arch := unknown
Configs := builtins-arm
-CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer -fPIC -integrated-as \
- --sysroot=$(ProjSrcRoot)/../../arm -isystem /include
+CFLAGS := -Wall -Werror -O3 -fomit-frame-pointer -fPIE -ftls-model=local-exec \
+ -integrated-as --sysroot=$(ProjSrcRoot)/../../arm -isystem /include
Arch.builtins-arm := armv7
CFLAGS.builtins-arm := $(CFLAGS) -target armv7a-linux-eabi