scc

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

commit 265ef001f34a875817f168233015c58c18559607
parent 437728beacff833edd64de96c6e9548cbd0fa813
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  9 May 2018 19:05:57 +0100

[ld] Add basic options parsing

In this version we force to have all the options before any file name.
-l options are considered file names as they are shorthands for libx
names.

Diffstat:
Minc/syslibs.def.h | 5++++-
Mld/Makefile | 2+-
Mld/ld.h | 6++++++
Mld/main.c | 107++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Mld/obj.c | 6++++++
5 files changed, 101 insertions(+), 25 deletions(-)

diff --git a/inc/syslibs.def.h b/inc/syslibs.def.h @@ -1,4 +1,7 @@ -char *syslibs[] = { + +#define MAX_LIB_PATHS 32 + +char *syslibs[MAX_LIB_PATHS + 1] = { PREFIX "/lib/scc/" , /* configure below your standard sys include paths */ PREFIX "/lib/", diff --git a/ld/Makefile b/ld/Makefile @@ -28,8 +28,8 @@ coff32.c: ../inc/scc.h coff32.c: ld.h formats.c: ld.h main.c: ../inc/ar.h -main.c: ../inc/arg.h main.c: ../inc/scc.h +main.c: ../inc/syslibs.h main.c: ld.h obj.c: ../inc/scc.h obj.c: ld.h diff --git a/ld/ld.h b/ld/ld.h @@ -1,5 +1,6 @@ typedef struct obj Obj; +typedef struct symbol Symbol; struct obj { char *fname; @@ -8,6 +9,10 @@ struct obj { struct obj *next; }; +struct symbol { + char *name; +}; + #ifdef stdin struct objfile { int (*probe)(char *fname, char *member, FILE *fp); @@ -18,3 +23,4 @@ struct objfile { /* obj.c */ extern Obj *newobj(char *fname); +extern Symbol *lookup(char *name); diff --git a/ld/main.c b/ld/main.c @@ -7,13 +7,15 @@ static char sccsid[] = "@(#) ./ld/main.c"; #include <stdlib.h> #include <string.h> -#include "../inc/arg.h" #include "../inc/scc.h" #include "../inc/ar.h" +#include "../inc/syslibs.h" #include "ld.h" char *argv0; +char *output = "a.out", *entry, *datasiz; int pass; +int sflag, xflag, Xflag, rflag, dflag; static int object(char *fname, char *member, FILE *fp) @@ -148,36 +150,95 @@ pass2(int argc, char *argv[]) static void usage(void) { - fputs("usage: ld [options] [@file] file ...\n", stderr); + fputs("usage: ld [options] file ...\n", stderr); exit(1); } +static void +Lpath(char *path) +{ + char **bp; + + for (bp = syslibs; bp < &syslibs[MAX_LIB_PATHS] && *bp; ++bp) + ; + if (bp == &syslibs[MAX_LIB_PATHS]) { + fputs("ld: too many -L options\n", stderr); + exit(1); + } + *bp = path; +} + int main(int argc, char *argv[]) { - unsigned i; - - ARGBEGIN { - case 's': - case 'u': - case 'l': - case 'x': - case 'X': - case 'r': - case 'd': - case 'n': - case 'i': - case 'o': - case 'e': - case 'O': - case 'D': - break; - default: - usage(); - } ARGEND + char *cp, **p; + for (--argc; *++argv; --argc) { + if (argv[0][0] != '-' || argv[0][1] == 'l') + break; + if (argv[0][1] == '-') { + --argc, ++argv; + break; + | + for (cp = &argv[0][1]; *cp; ++cp) { + switch (*cp) { + case 's': + sflag = 1; + break; + case 'x': + xflag = 1; + break; + case 'X': + Xflag = 1; + break; + case 'r': + rflag = 1; + break; + case 'd': + dflag = 1; + break; + case 'i': + case 'n': + /* TODO */ + break; + case 'L': + if (argc == 0) + goto usage; + ++argv, --argc; + Lpath(*argv); + break; + case 'u': + if (argc == 0) + goto usage; + ++argv, --argc; + lookup(*argv); + break; + case 'o': + if (argc == 0) + goto usage; + ++argv, --argc; + output = *argv; + break; + case 'e': + if (argc == 0) + goto usage; + ++argv, --argc; + entry = *argv; + break; + case 'D': + if (argc == 0) + goto usage; + ++argv, --argc; + datasiz = *argv; + break; + default: + usage: + usage(); + } + } + } - if (argc == 0) + if (argc < 0) usage(); pass1(argc, argv); diff --git a/ld/obj.c b/ld/obj.c @@ -31,3 +31,9 @@ newobj(char *fname) return obj; } + +Symbol * +lookup(char *name) +{ + return NULL; +}