ref: f5f5501c8ea833e2e40f4de72b733c4b50fe86dd
parent: 8f241ba283b03cd4dffcd8bc1753bb3f500949f5
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Aug 8 16:31:21 EDT 2019
[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.
--- a/src/cmd/ld/ld.h
+++ b/src/cmd/ld/ld.h
@@ -6,11 +6,6 @@
typedef struct section Section;
enum {
- NOINSTALL,
- INSTALL,
-};
-
-enum {
OUTLIB,
INLIB,
};
@@ -43,6 +38,7 @@
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);
@@ -49,9 +45,8 @@
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);
--- a/src/cmd/ld/main.c
+++ b/src/cmd/ld/main.c
@@ -132,7 +132,7 @@
if (argc == 0)
goto usage;
++argv, --argc;
- lookup(*argv, INSTALL);
+ install(*argv);
break;
case 'o':
if (argc == 0)
--- 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 @@
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;
--- a/src/cmd/ld/pass3.c
+++ b/src/cmd/ld/pass3.c
@@ -17,14 +17,14 @@
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:
--- 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 @@
};
Symbol *
-lookup(char *name, int install)
+lookup(char *name)
{
- size_t len;
- char *s;
unsigned h;
Symbol *sym;
@@ -32,9 +29,19 @@
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));
s = malloc(len);
@@ -56,59 +63,6 @@
sym->prev = &refhead;
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