shithub: scc

Download patch

ref: 349ceccbc07453503347623eea9476c815473d01
parent: be5ee314716947438f2d9818ae7cecd93d78c977
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Mar 10 06:36:15 EST 2022

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.

--- a/include/scc/scc/Makefile
+++ b/include/scc/scc/Makefile
@@ -18,7 +18,11 @@
 	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 $@
--- 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
 };
--- a/scripts/build/tool/gnu.mk
+++ b/scripts/build/tool/gnu.mk
@@ -6,6 +6,7 @@
 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
--- 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
--- a/src/cmd/cc/posix/cc.c
+++ b/src/cmd/cc/posix/cc.c
@@ -192,8 +192,10 @@
 			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);
--- a/src/libc/rules.mk
+++ b/src/libc/rules.mk
@@ -5,6 +5,7 @@
 	-I$(INCDIR)/bits/$(SYS)/$(ARCH)\
 
 MORE_ARFLAGS = -u
+MORE_CFLAGS = $(TOOL_LIBC_CFLAGS) $(NOPIE_CFLAGS)
 
 SYSERRNO = $(INCDIR)/bits/$(SYS)/sys/errno.h