scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit 9deeff396080448b6a80cc295ee9b276a727f15a
parent 9f3f093fde9fb05a0763d886623c03137b6833dd
Author: Quentin Rameau <quinq@fifth.space>
Date:   Wed, 22 Mar 2017 16:25:53 +0100

[driver] Link against our crt, use ld instead of gcc

Diffstat:
MMakefile | 2+-
Mconfig.mk | 1+
Mdriver/posix/Makefile | 5++++-
Mdriver/posix/scc.c | 23++++++++++++++++++++---
Ainc/ldflags.def.h | 6++++++
5 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/Makefile b/Makefile @@ -76,4 +76,4 @@ clean: rm -f bin/cc* bin/scc distclean: clean - rm -f inc/sysincludes.h inc/syslibs.h + rm -f inc/sysincludes.h inc/syslibs.h inc/ldflags.h diff --git a/config.mk b/config.mk @@ -24,6 +24,7 @@ AS = as # for Plan9 add -D_SUSV2_SOURCE SCC_CFLAGS = -DARCH=\"$(ARCH)\" \ + -DSYS=\"$(SYS)\" \ $(CSTDINC) \ -DPREFIX=\"$(PREFIX)\" \ -g \ diff --git a/driver/posix/Makefile b/driver/posix/Makefile @@ -7,7 +7,7 @@ OBJS = scc.o all: scc -$(OBJS): ../../inc/cc.h ../../inc/arg.h ../../inc/syslibs.h +$(OBJS): ../../inc/cc.h ../../inc/arg.h ../../inc/syslibs.h ../../inc/ldflags.h scc: $(OBJS) ../../lib/libcc.a $(CC) $(SCC_LDFLAGS) $(OBJS) ../../lib/libcc.a -o $@ @@ -15,6 +15,9 @@ scc: $(OBJS) ../../lib/libcc.a ../../inc/syslibs.h: ../../inc/syslibs.def.h cp -f ../../inc/syslibs.def.h ../../inc/syslibs.h +../../inc/ldflags.h: ../../inc/ldflags.def.h + cp -f ../../inc/ldflags.def.h ../../inc/ldflags.h + ../../lib/libcc.a: cd ../../lib && $(MAKE) -e diff --git a/driver/posix/scc.c b/driver/posix/scc.c @@ -17,6 +17,7 @@ static char sccsid[] = "@(#) ./driver/posix/scc.c"; #include "../../inc/arg.h" #include "../../inc/cc.h" #include "../../inc/syslibs.h" +#include "../../inc/ldflags.h" enum { CC1, @@ -47,12 +48,12 @@ static struct tool { [QBE] = { .bin = "qbe", .cmd = "qbe", }, [TEEAS] = { .bin = "tee", .cmd = "tee", }, [AS] = { .bin = "as", .cmd = "as", }, - [LD] = { .bin = "gcc", .cmd = "gcc", }, /* TODO use ld */ + [LD] = { .bin = "ld", .cmd = "ld", }, [STRIP] = { .bin = "strip", .cmd = "strip", }, }; char *argv0; -static char *arch, *execpath, *objfile, *outfile; +static char *arch, *sys, *execpath, *objfile, *outfile; static char *tmpdir; static size_t tmpdirln; static struct items objtmp, objout; @@ -98,6 +99,7 @@ static int inittool(int tool) { struct tool *t = &tools[tool]; + char *crt; int n; if (t->init) @@ -115,7 +117,8 @@ inittool(int tool) die("scc: target tool path too long"); break; case LD: - addarg(tool, "-no-pie"); + for (n = 0; ldflags[n]; ++n) + addarg(tool, ldflags[n]); addarg(tool, "-o"); t->outfile = outfile ? outfile : xstrdup("a.out"); addarg(tool, t->outfile); @@ -123,6 +126,14 @@ inittool(int tool) addarg(tool, "-L"); addarg(tool, syslibs[n]); } + n = snprintf(NULL, 0, "%s-%s-%s.o", + PREFIX "/lib/scc/crt", arch, sys); + if (n < 0) + die("scc: wrong crt file name"); + crt = xmalloc(++n); + n = snprintf(crt, n, "%s-%s-%s.o", + PREFIX "/lib/scc/crt", arch, sys); + addarg(tool, crt); break; case AS: addarg(tool, "-o"); @@ -432,6 +443,8 @@ main(int argc, char *argv[]) if (!(arch = getenv("ARCH"))) arch = ARCH; + if (!(sys = getenv("SYS"))) + sys = SYS; if (!(execpath = getenv("SCCEXECPATH"))) execpath = PREFIX "/libexec/scc"; @@ -492,6 +505,9 @@ main(int argc, char *argv[]) case 's': sflag = 1; break; + case 't': + sys = EARGF(usage()); + break; case 'W': EARGF(usage()); case 'w': @@ -535,6 +551,7 @@ operand: return failure; if (link && !failure) { + addarg(LD, xstrdup("-lc")); spawn(settool(LD, NULL, LAST_TOOL)); validatetools(); } diff --git a/inc/ldflags.def.h b/inc/ldflags.def.h @@ -0,0 +1,6 @@ +char *ldflags[] = { + "-static", + /* on OpenBSD, disable pie */ + /* "-nopie", */ + NULL +};