scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | Submodules | README | LICENSE

commit 6581062fb5bc6f44b84090da0a0f7b05f41344ee
parent 30c13a046ae2be0b83933a64f197250430f21c68
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  1 Jun 2022 12:50:36 +0200

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.

Diffstat:
Minclude/scc/scc/.gitignore | 4+---
Minclude/scc/scc/Makefile | 16++++------------
Dinclude/scc/scc/ldflags.def.h | 6------
Dinclude/scc/scc/syscrts.def.h | 9---------
Ainclude/scc/scc/sysincludes.musl.h | 5+++++
Ainclude/scc/scc/sysld.def.h | 14++++++++++++++
Ainclude/scc/scc/sysld.musl.h | 22++++++++++++++++++++++
Dinclude/scc/scc/syslibs.def.h | 5-----
Msrc/cmd/cc/posix/cc.c | 98++++++++++++++++++++++++++++++++++++-------------------------------------------
Msrc/cmd/cc/posix/deps.mk | 4+---
10 files changed, 92 insertions(+), 91 deletions(-)

diff --git 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 diff --git a/include/scc/scc/Makefile b/include/scc/scc/Makefile @@ -4,10 +4,8 @@ PROJECTDIR = ../../.. include $(PROJECTDIR)/scripts/rules.mk SYSHDR =\ - ldflags.h\ - syscrts.h\ + sysld.h\ sysincludes.h\ - syslibs.h\ cstd.h\ NODEP = 1 @@ -17,22 +15,16 @@ all: $(SYSHDR) 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) diff --git a/include/scc/scc/ldflags.def.h b/include/scc/scc/ldflags.def.h @@ -1,6 +0,0 @@ -char *ldflags[] = { - "-static", - "-z","nodefaultlib", - %NOPIE% - NULL -}; diff --git a/include/scc/scc/syscrts.def.h b/include/scc/scc/syscrts.def.h @@ -1,9 +0,0 @@ -/* configure below your system crts */ -char *syscrtsb[] = { - "%p/lib/scc/%a-%s/crt.o", - NULL -}; - -char *syscrtse[] = { - NULL -}; diff --git a/include/scc/scc/sysincludes.musl.h b/include/scc/scc/sysincludes.musl.h @@ -0,0 +1,5 @@ +/* configure below your standard sys include paths */ +char *sysincludes[] = { + "%p/include/", + NULL +}; diff --git a/include/scc/scc/sysld.def.h b/include/scc/scc/sysld.def.h @@ -0,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 +}; diff --git a/include/scc/scc/sysld.musl.h b/include/scc/scc/sysld.musl.h @@ -0,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 +}; diff --git a/include/scc/scc/syslibs.def.h b/include/scc/scc/syslibs.def.h @@ -1,5 +0,0 @@ -/* configure below your standard sys include paths */ -char *syslibs[] = { - "%p/lib/scc/%a-%s", - NULL -}; diff --git 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 @@ terminate(void) } } -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; + + 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 ; *s && cnt < FILENAME_MAX; ++s) { - if (*s != '%') { - buff[cnt++] = *s; + 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); - } -} - -static void -addarg(int tool, char *arg) -{ - struct tool *t = &tools[tool]; - - if (t->args.n < 1) - t->args.n = 1; + if (cnt >= FILENAME_MAX) + abort(); - newitem(&t->args, arg); + buff[cnt] = '\0'; + newitem(&t->args, xstrdup(buff)); } static void @@ -176,7 +183,7 @@ inittool(int tool) 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 @@ inittool(int tool) 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 @@ operand: 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 @@ operand: 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(); } diff --git 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