commit b04982f0e88be2575b99a53e9780a85fb0befa65
parent 5070a211d2a2e76947fb9d05a236fd8fb4f728a6
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Thu, 10 Mar 2022 11:36:15 +0100
build: Use proper flags for the libc
There were several problems related to the libc:
- The libc was built using parts of the host libc headers
- The final executable was built without nopie option
To make harder the problem the options needed in OpenBSD are different
to the options needed in other systems.
Diffstat:
6 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/include/scc/scc/Makefile b/include/scc/scc/Makefile
@@ -18,7 +18,11 @@ cstd.h: cstd-$(STD).h
cp cstd-$(STD).h $@
ldflags.h:
- cp ldflags.def.h $@
+ set -e;\
+ rm -f $@;\
+ trap "rm -f $$$$.tmp" INT QUIT TERM HUP;\
+ sed 's/%NOPIE%/"$(NOPIE_LDFLAGS)",/' ldflags.def.h > $$$$.tmp && \
+ mv $$$$.tmp $@
syscrts.h:
cp syscrts.def.h $@
diff --git a/include/scc/scc/ldflags.def.h b/include/scc/scc/ldflags.def.h
@@ -1,5 +1,6 @@
char *ldflags[] = {
"-static",
"-z","nodefaultlib",
+ %NOPIE%
NULL
};
diff --git a/scripts/build/tool/gnu.mk b/scripts/build/tool/gnu.mk
@@ -6,6 +6,7 @@ ARCHIVE = ar
PRECOMP = cpp
ARCHIVE_FLAGS = -U
-NOPIE_CFLAGS = -nopie
+NOPIE_CFLAGS = -fno-pie
NOPIE_LDFLAGS = -nopie
TOOL_CFLAGS = -std=c99 -fno-stack-protector -static
+TOOL_LIBC_CFLAGS = -ffreestanding
diff --git a/scripts/config b/scripts/config
@@ -2,7 +2,7 @@
case `uname` in
OpenBSD)
- echo TOOL=${TOOL:-clang} HOST=bsd ARFLAGS="${ARFLAGS:--rv}"
+ echo TOOL=${TOOL:-clang} HOST=bsd ARFLAGS="${ARFLAGS:--rv}" NOPIE_LDFLAGS=-no-pie
;;
NetBSD)
#TODO: send a patch to NetBSD to solve this problem
diff --git a/src/cmd/cc/posix/cc.c b/src/cmd/cc/posix/cc.c
@@ -192,8 +192,10 @@ inittool(int tool)
die("cc: target tool path is too long");
break;
case LD:
- for (n = 0; ldflags[n]; ++n)
- addarg(tool, ldflags[n]);
+ for (n = 0; ldflags[n]; ++n) {
+ if (ldflags[n][0] != '\n')
+ addarg(tool, ldflags[n]);
+ }
addarg(tool, "-o");
t->outfile = outfile ? outfile : xstrdup("a.out");
addarg(tool, t->outfile);
diff --git a/src/libc/rules.mk b/src/libc/rules.mk
@@ -5,6 +5,7 @@ CINCLUDES =\
-I$(INCDIR)/bits/$(SYS)/$(ARCH)\
MORE_ARFLAGS = -u
+MORE_CFLAGS = $(TOOL_LIBC_CFLAGS) $(NOPIE_CFLAGS)
SYSERRNO = $(INCDIR)/bits/$(SYS)/sys/errno.h