commit ee66e1b7156dbeae6df2a950b0aa832b1c7211b0
parent 9fa6e648d66374ab74185dc11c5fb385ddee8f54
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Fri, 9 Aug 2019 21:03:01 +0100
[ld] Move undefine dealing to pass1.c
The resolve part of the linking process is fully done in
pass1, so it doesn't make sense to have this logic in symbol,c
because it introduces non orthogonal functions that cannot
be reused in other passes.
Diffstat:
5 files changed, 62 insertions(+), 53 deletions(-)
diff --git a/src/cmd/ld/ld.h b/src/cmd/ld/ld.h
@@ -36,6 +36,7 @@ struct symbol {
/* passes */
extern void pass1(int argc, char *argv[]);
+extern Symbol *undef(char *name);
extern void pass2(int argc, char *argv[]);
extern void pass3(int argc, char *argv[]);
extern void pass4(int argc, char *argv[]);
@@ -47,9 +48,6 @@ extern void error(char *fmt, ...);
/* symbol.c */
extern Symbol *lookup(char *name);
extern Symbol *install(char *name);
-extern int moreundef(void);
-extern void listundef(void);
-extern int defasym(struct obj *obj);
extern int debugsym(void);
/* globals */
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;
- install(*argv);
+ undef(*argv);
break;
case 'o':
if (argc == 0)
diff --git a/src/cmd/ld/pass1.c b/src/cmd/ld/pass1.c
@@ -11,6 +11,11 @@
#include "ld.h"
static int bintype = -1;
+static Symbol refhead = {
+ .next = &refhead,
+ .prev = &refhead,
+};
+
Objlst *objhead, *objlast;
static Symbol *
@@ -37,6 +42,54 @@ define(Objsym *osym, Obj *obj)
return sym;
}
+Symbol *
+undef(char *name)
+{
+ Symbol *sym = install(name);
+
+ refhead.next->prev = sym;
+ sym->next = refhead.next;
+ refhead.next = sym;
+ sym->prev = &refhead;
+
+ return sym;
+}
+
+static int
+moreundef(void)
+{
+
+ return refhead.next != &refhead;
+}
+
+static void
+listundef(void)
+{
+ Symbol *sym, *p;
+
+ p = &refhead;
+ for (sym = p->next; sym != p; sym = sym->next) {
+ fprintf(stderr,
+ "ld: symbol '%s' not defined\n",
+ sym->name);
+ }
+}
+
+static int
+defasym(Obj *obj)
+{
+ Symbol *sym, *p;
+
+ p = &refhead;
+ for (sym = p->next; sym != p; sym = sym->next) {
+ if (objlookup(obj, sym->name, 0))
+ return 1;
+ }
+
+ return 0;
+}
+
+
static int
newsym(Objsym *osym, Obj *obj)
{
@@ -45,7 +98,7 @@ newsym(Objsym *osym, Obj *obj)
switch (osym->type) {
case 'U':
if ((sym = lookup(osym->name)) == NULL)
- sym = install(osym->name);
+ sym = undef(osym->name);
break;
case '?':
case 'N':
@@ -150,8 +203,7 @@ addlib(FILE *fp)
return;
}
- added = 1;
- while (moreundef() && added) {
+ for (added = 1; moreundef() && added; ) {
added = 0;
for (dp = def; dp; dp = dp->next) {
sym = lookup(dp->name);
diff --git a/src/cmd/ld/pass3.c b/src/cmd/ld/pass3.c
@@ -17,6 +17,10 @@ rebase(Obj *obj)
case 'T':
case 'D':
case 'B':
+ /*
+ * this lookup must succeed, otherwise
+ * we have an error in our code.
+ */
aux = lookup(sym->name);
aux->value += obj->secs[sym->sect].base;
case 't':
diff --git a/src/cmd/ld/symbol.c b/src/cmd/ld/symbol.c
@@ -11,11 +11,6 @@
static Symbol *symtab[NR_SYMBOL];
-static Symbol refhead = {
- .next = &refhead,
- .prev = &refhead,
-};
-
Symbol *
lookup(char *name)
{
@@ -23,7 +18,6 @@ lookup(char *name)
Symbol *sym;
h = genhash(name) % NR_SYMBOL;
-
for (sym = symtab[h]; sym; sym = sym->hash) {
if (!strcmp(name, sym->name))
return sym;
@@ -41,7 +35,6 @@ install(char *name)
char *s;
h = genhash(name) % NR_SYMBOL;
-
len = strlen(name) + 1;
sym = malloc(sizeof(*sym));
s = malloc(len);
@@ -56,49 +49,11 @@ install(char *name)
symtab[h] = sym;
sym->value = 0;
sym->size = 0;
-
- refhead.next->prev = sym;
- sym->next = refhead.next;
- refhead.next = sym;
- sym->prev = &refhead;
+ sym->next = sym->prev = NULL;
return sym;
}
-int
-moreundef(void)
-{
-
- return refhead.next != &refhead;
-}
-
-void
-listundef(void)
-{
- Symbol *sym, *p;
-
- p = &refhead;
- for (sym = p->next; sym != p; sym = sym->next) {
- fprintf(stderr,
- "ld: symbol '%s' not defined\n",
- sym->name);
- }
-}
-
-int
-defasym(Obj *obj)
-{
- Symbol *sym, *p;
-
- p = &refhead;
- for (sym = p->next; sym != p; sym = sym->next) {
- if (objlookup(obj, sym->name, 0))
- return 1;
- }
-
- return 0;
-}
-
#ifndef NDEBUG
int
debugsym(void)