ref: 4f93908533138a0efbdffd880003438f98af736e
parent: f13bf4d88bf6ab600d9b7faf524de685d3870ab6
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Aug 9 17:03:01 EDT 2019
[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.
--- a/src/cmd/ld/ld.h
+++ b/src/cmd/ld/ld.h
@@ -36,6 +36,7 @@
/* 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 @@
/* 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 */
--- a/src/cmd/ld/main.c
+++ b/src/cmd/ld/main.c
@@ -132,7 +132,7 @@
if (argc == 0)
goto usage;
++argv, --argc;
- install(*argv);
+ undef(*argv);
break;
case 'o':
if (argc == 0)
--- 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,7 +42,55 @@
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)
{
Symbol *sym;
@@ -45,7 +98,7 @@
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 @@
return;
}
- added = 1;
- while (moreundef() && added) {
+ for (added = 1; moreundef() && added; ) {
added = 0;
for (dp = def; dp; dp = dp->next) {
sym = lookup(dp->name);
--- a/src/cmd/ld/pass3.c
+++ b/src/cmd/ld/pass3.c
@@ -17,6 +17,10 @@
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':
--- 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 @@
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 @@
char *s;
h = genhash(name) % NR_SYMBOL;
-
len = strlen(name) + 1;
sym = malloc(sizeof(*sym));
s = malloc(len);
@@ -56,47 +49,9 @@
symtab[h] = sym;
sym->value = 0;
sym->size = 0;
+ sym->next = sym->prev = NULL;
- refhead.next->prev = sym;
- sym->next = refhead.next;
- refhead.next = sym;
- sym->prev = &refhead;
-
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