shithub: scc

Download patch

ref: 6581062fb5bc6f44b84090da0a0f7b05f41344ee
parent: 30c13a046ae2be0b83933a64f197250430f21c68
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Wed Jun 1 08:50:36 EDT 2022

driver/posix: Add a template for ld execution

The command line for ld is going to depend deeply in the libc used.
As the scope of scc libc is to provide a simple c99 library can be
easily ported to different targets it is not going to support a full
POSIX interface. In order to compile POSIX programs with scc the
best approach is to use musl libc, which has portable headers that
scc can use without problems. Musl has a link command line totally
different to the one used by scc libc and for that reason the
template mechanism is provided.

--- a/include/scc/scc/.gitignore
+++ b/include/scc/scc/.gitignore
@@ -1,5 +1,3 @@
 cstd.h
-ldflags.h
-syscrts.h
 sysincludes.h
-syslibs.h
+sysld.h
--- a/include/scc/scc/Makefile
+++ b/include/scc/scc/Makefile
@@ -4,10 +4,8 @@
 include $(PROJECTDIR)/scripts/rules.mk
 
 SYSHDR =\
-	ldflags.h\
-	syscrts.h\
+	sysld.h\
 	sysincludes.h\
-	syslibs.h\
 	cstd.h\
 
 NODEP = 1
@@ -17,22 +15,16 @@
 cstd.h: cstd-$(STD).h
 	cp cstd-$(STD).h $@
 
-ldflags.h:
+sysld.h:
 	set -e;\
 	rm -f $@;\
 	trap "rm -f $$$$.tmp" INT QUIT TERM HUP;\
-	sed 's/%NOPIE%/"$(NOPIE_LDFLAGS)",/' ldflags.def.h | \
-	sed 's/"",//' > $$$$.tmp && \
+	sed -e 's/%NOPIE%/"$(NOPIE_LDFLAGS)",/' \
+	    -e 's/"",//'  sysld.def.h > $$$$.tmp && \
 	mv $$$$.tmp $@
 
-syscrts.h:
-	cp syscrts.def.h $@
-
 sysincludes.h:
 	cp sysincludes.def.h $@
-
-syslibs.h:
-	cp syslibs.def.h $@
 
 clean:
 	rm -f $(SYSHDR)
--- a/include/scc/scc/ldflags.def.h
+++ /dev/null
@@ -1,6 +1,0 @@
-char *ldflags[] = {
-	"-static",
-	"-z","nodefaultlib",
-	%NOPIE%
-	NULL
-};
--- a/include/scc/scc/syscrts.def.h
+++ /dev/null
@@ -1,9 +1,0 @@
-/* configure below your system crts */
-char *syscrtsb[] = {
-	"%p/lib/scc/%a-%s/crt.o",
-	NULL
-};
-
-char *syscrtse[] = {
-	NULL
-};
--- /dev/null
+++ b/include/scc/scc/sysincludes.musl.h
@@ -1,0 +1,5 @@
+/* configure below your standard sys include paths */
+char *sysincludes[] = {
+	"%p/include/",
+	NULL
+};
--- /dev/null
+++ b/include/scc/scc/sysld.def.h
@@ -1,0 +1,14 @@
+/* configure below your system linker command line */
+
+char *ldcmd[] = {
+	"-static",
+	"-z","nodefaultlib",
+	%NOPIE%
+	"-o","%o",
+	"-L","%p/lib/scc/%a-%s",
+	"%p/lib/scc/%a-%s/crt.o",
+	"%c",
+	"-lc",
+	"-lcrt",
+        NULL
+};
--- /dev/null
+++ b/include/scc/scc/sysld.musl.h
@@ -1,0 +1,22 @@
+/* configure below your system linker command line */
+
+#define GCCLIBPATH "/usr/lib/gcc/x86_64-unknown-linux-gnu/10.2/"
+
+char *ldcmd[] = {
+	"-static",
+	"-z","nodefaultlib",
+        %NOPIE%
+	"-o","%o",
+	"-L","%p/lib/",
+	"-L",GCCLIBPATH,
+	"%p/lib/Scrt1.o",
+	"%p/lib/crti.o",
+	GCCLIBPATH "crtbeginS.o",
+	"%c",
+	GCCLIBPATH "crtendS.o",
+	"%p/lib/crtn.o",
+	"-lc",
+	"-lgcc",
+	"-lgcc_eh",
+	NULL
+};
--- a/include/scc/scc/syslibs.def.h
+++ /dev/null
@@ -1,5 +1,0 @@
-/* configure below your standard sys include paths */
-char *syslibs[] = {
-	"%p/lib/scc/%a-%s",
-	NULL
-};
--- a/src/cmd/cc/posix/cc.c
+++ b/src/cmd/cc/posix/cc.c
@@ -26,10 +26,8 @@
 #include "config.h"
 #include <scc/arg.h>
 #include <scc/scc.h>
-#include <scc/syscrts.h>
 #include <scc/sysincludes.h>
-#include <scc/syslibs.h>
-#include <scc/ldflags.h>
+#include <scc/sysld.h>
 
 enum {
 	CC1,
@@ -85,58 +83,67 @@
 	}
 }
 
-static char *
-path(char *s)
+static void
+addarg(int tool, char *arg)
 {
-	char *arg, buff[FILENAME_MAX];
+	struct tool *t = &tools[tool];
+	char *p, buff[FILENAME_MAX];
 	size_t len, cnt;
+	int n;
 
-	for (cnt = 0 ; *s && cnt < FILENAME_MAX; ++s) {
-		if (*s != '%') {
-			buff[cnt++] = *s;
+	if (t->args.n < 1)
+		t->args.n = 1;
+
+	if (!arg || arg[0] == '-') {
+		newitem(&t->args, arg);
+		return;
+	}
+
+	if (!strcmp(arg, "%c")) {
+		for (n = 0; n < linkargs.n; ++n)
+			newitem(&t->args, linkargs.s[n]);
+		return;
+	}
+
+	for (cnt = 0 ; *arg && cnt < FILENAME_MAX; ++arg) {
+		if (*arg != '%') {
+			buff[cnt++] = *arg;
 			continue;
 		}
 
-		switch (*++s) {
+		switch (*++arg) {
 		case 'a':
-			arg = arch;
+			p = arch;
 			break;
 		case 's':
-			arg = sys;
+			p = sys;
 			break;
 		case 'p':
-			arg = prefix;
+			p = libprefix;
 			break;
 		case 'b':
-			arg = abi;
+			p = abi;
 			break;
+		case 'o':
+			p = outfile;
+			break;
 		default:
-			buff[cnt++] = *s;
+			buff[cnt++] = *arg;
 			continue;
 		}
 
-		len = strlen(arg);
+		len = strlen(p);
 		if (len + cnt >= FILENAME_MAX)
 			die("cc: pathname too long");
-		memcpy(buff+cnt, arg, len);
+		memcpy(buff+cnt, p, len);
 		cnt += len;
 	}
 
-	if (cnt < FILENAME_MAX) {
-		buff[cnt] = '\0';
-		return xstrdup(buff);
-	}
-}
+	if (cnt >= FILENAME_MAX)
+		abort();
 
-static void
-addarg(int tool, char *arg)
-{
-	struct tool *t = &tools[tool];
-
-	if (t->args.n < 1)
-		t->args.n = 1;
-
-	newitem(&t->args, arg);
+	buff[cnt] = '\0';
+	newitem(&t->args, xstrdup(buff));
 }
 
 static void
@@ -176,7 +183,7 @@
 			addarg(tool, "-w");
 		for (n = 0; sysincludes[n]; ++n) {
 			addarg(tool, "-I");
-			addarg(tool, path(sysincludes[n]));
+			addarg(tool, sysincludes[n]);
 		}
 	case CC2:
 		fmt = cc12fmt(tool);
@@ -190,20 +197,11 @@
 			die("cc: target tool path is too long");
 		break;
 	case LD:
-		for (n = 0; ldflags[n]; ++n)
-			addarg(tool, ldflags[n]);
-		addarg(tool, "-o");
-		t->outfile = outfile ? outfile : "a.out";
-		addarg(tool, t->outfile);
-		for (n = 0; syslibs[n]; ++n) {
-			addarg(tool, "-L");
-			addarg(tool, path(syslibs[n]));
-		}
+		t->outfile = outfile;
 		if (sflag)
 			addarg(tool, "-s");
-
-		for (n = 0; syscrtsb[n]; ++n)
-			addarg(tool, path(syscrtsb[n]));
+		for (n = 0; ldcmd[n]; n++)
+			addarg(tool, ldcmd[n]);
 		break;
 	case AS:
 		addarg(tool, "-o");
@@ -620,6 +618,9 @@
 			fputs("cc: could not open /dev/null\n", stderr);
 	}
 
+	if (!outfile)
+		outfile = "a.out";
+
 	if (!(tmpdir = getenv("TMPDIR")) || !tmpdir[0])
 		tmpdir = ".";
 	tmpdirln = strlen(tmpdir);
@@ -631,15 +632,6 @@
 
 	if (link && !failure) {
 		inittool(LD);
-		for (n = 0; n < linkargs.n; ++n)
-			addarg(LD, linkargs.s[n]);
-
-		addarg(LD, "-lc");
-		addarg(LD, "-lcrt");
-
-		for (n = 0; syscrtse[n]; ++n)
-			addarg(LD, path(syscrtse[n]));
-
 		spawn(settool(LD, NULL, LAST_TOOL));
 		validatetools();
 	}
--- a/src/cmd/cc/posix/deps.mk
+++ b/src/cmd/cc/posix/deps.mk
@@ -1,8 +1,6 @@
 #deps
 cc.o: $(INCDIR)/scc/scc/arg.h
-cc.o: $(INCDIR)/scc/scc/ldflags.h
 cc.o: $(INCDIR)/scc/scc/scc.h
-cc.o: $(INCDIR)/scc/scc/syscrts.h
 cc.o: $(INCDIR)/scc/scc/sysincludes.h
-cc.o: $(INCDIR)/scc/scc/syslibs.h
+cc.o: $(INCDIR)/scc/scc/sysld.h
 cc.o: config.h