scc

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

commit 0c97aafd0bcc755fd297d534ffd5119a9d79a6cf
parent 5407099465e7f5282aebcf438f2baa02fb90082c
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu,  8 Aug 2019 20:31:21 +0100

[ld] Move code from symbol.c pass1.c

There was code in symbol.c that was used only in pass1.c,
an it wasn't general enough o be included in symbol.c
These additional interfaces were making less orthogonal
the meaning of symbol.c and they had to be moved away.

Diffstat:
Msrc/cmd/ld/ld.h | 11+++--------
Msrc/cmd/ld/main.c | 2+-
Msrc/cmd/ld/pass1.c | 60+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
Msrc/cmd/ld/pass3.c | 4++--
Msrc/cmd/ld/symbol.c | 72+++++++++++++-----------------------------------------------------------
5 files changed, 78 insertions(+), 71 deletions(-)

diff --git a/src/cmd/ld/ld.h b/src/cmd/ld/ld.h @@ -6,11 +6,6 @@ typedef struct symbol Symbol; typedef struct section Section; enum { - NOINSTALL, - INSTALL, -}; - -enum { OUTLIB, INLIB, }; @@ -43,15 +38,15 @@ struct symbol { extern void pass1(int argc, char *argv[]); extern void pass2(int argc, char *argv[]); extern void pass3(int argc, char *argv[]); +extern void pass4(int argc, char *argv[]); /* main.c */ extern char *errstr(void); extern void error(char *fmt, ...); /* symbol.c */ -extern Symbol *lookup(char *name, int install); -extern Symbol *define(struct objsym *osym, struct obj *obj); -extern int newsym(struct objsym *osym, struct obj *obj); +extern Symbol *lookup(char *name); +extern Symbol *install(char *name); extern int moreundef(void); extern void listundef(void); extern int defasym(struct obj *obj); diff --git a/src/cmd/ld/main.c b/src/cmd/ld/main.c @@ -132,7 +132,7 @@ main(int argc, char *argv[]) if (argc == 0) goto usage; ++argv, --argc; - lookup(*argv, INSTALL); + install(*argv); break; case 'o': if (argc == 0) diff --git a/src/cmd/ld/pass1.c b/src/cmd/ld/pass1.c @@ -1,3 +1,4 @@ +#include <ctype.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -12,6 +13,63 @@ static int bintype = -1; Objlst *objhead, *objlast; +static Symbol * +define(Objsym *osym, Obj *obj) +{ + Symbol *sym = lookup(osym->name); + + if (!sym) { + sym = install(osym->name); + } else if (sym->def && sym->def->type != 'C') { + error("%s: symbol redefined", osym->name); + return NULL; + } + + sym->obj = obj; + sym->def = osym; + sym->size = osym->size; + sym->value = osym->value; + + sym->next->prev = sym->prev; + sym->prev->next = sym->next; + sym->next = sym->prev = NULL; + + return sym; +} + +static int +newsym(Objsym *osym, Obj *obj) +{ + Symbol *sym; + + switch (osym->type) { + case 'U': + if ((sym = lookup(osym->name)) == NULL) + sym = install(osym->name); + break; + case '?': + case 'N': + break; + case 'C': + sym = lookup(osym->name); + if (!sym || !sym->def) { + sym = define(osym, obj); + break; + } + if (sym->def->type != 'C') + break; + if (sym->size < osym->size) + sym->size = osym->size; + break; + default: + if (isupper(osym->type)) + define(osym, obj); + break; + } + + return 1; +} + static void addobj(Obj *obj, FILE *fp) { @@ -96,7 +154,7 @@ addlib(FILE *fp) while (moreundef() && added) { added = 0; for (dp = def; dp; dp = dp->next) { - sym = lookup(dp->name, NOINSTALL); + sym = lookup(dp->name); if (!sym || sym->def) continue; diff --git a/src/cmd/ld/pass3.c b/src/cmd/ld/pass3.c @@ -17,14 +17,14 @@ rebase(Obj *obj) case 'T': case 'D': case 'B': - aux = lookup(sym->name, NOINSTALL); + aux = lookup(sym->name); aux->value += obj->secs[sym->sect].base; case 't': case 'd': case 'b': sym->value += obj->secs[sym->sect].base; - case 'N': case 'U': + case 'N': case '?': break; default: diff --git a/src/cmd/ld/symbol.c b/src/cmd/ld/symbol.c @@ -1,4 +1,3 @@ -#include <ctype.h> #include <stdio.h> #include <stdlib.h> #include <string.h> @@ -18,10 +17,8 @@ static Symbol refhead = { }; Symbol * -lookup(char *name, int install) +lookup(char *name) { - size_t len; - char *s; unsigned h; Symbol *sym; @@ -32,8 +29,18 @@ lookup(char *name, int install) return sym; } - if (!install) - return NULL; + return NULL; +} + +Symbol * +install(char *name) +{ + unsigned h; + size_t len; + Symbol *sym; + char *s; + + h = genhash(name) % NR_SYMBOL; len = strlen(name) + 1; sym = malloc(sizeof(*sym)); @@ -58,59 +65,6 @@ lookup(char *name, int install) return sym; } -Symbol * -define(Objsym *osym, Obj *obj) -{ - Symbol *sym = lookup(osym->name, INSTALL); - - if (sym->def && sym->def->type != 'C') { - error("%s: symbol redefined", osym->name); - return NULL; - } - - sym->obj = obj; - sym->def = osym; - sym->size = osym->size; - sym->value = osym->value; - - sym->next->prev = sym->prev; - sym->prev->next = sym->next; - sym->next = sym->prev = NULL; - - return sym; -} - -int -newsym(Objsym *osym, Obj *obj) -{ - Symbol *sym; - - switch (osym->type) { - case 'U': - lookup(osym->name, INSTALL); - case '?': - case 'N': - break; - case 'C': - sym = lookup(osym->name, NOINSTALL); - if (!sym || !sym->def) { - define(osym, obj); - break; - } - if (sym->def->type != 'C') - break; - if (sym->size < osym->size) - sym->size = osym->size; - break; - default: - if (isupper(osym->type)) - define(osym, obj); - break; - } - - return 1; -} - int moreundef(void) {