ref: d31939822e9036f76e1534b1f77e3ab5507cb20b
parent: 4bec4d5e37d09aeeb32dc583261a31f4f9675d3b
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon May 14 03:08:02 EDT 2018
[ld] Implement lookup()
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -15,6 +15,7 @@
struct symbol {
char *name;
+ struct symbol *hash;
};
struct objfile {
--- a/ld/obj.c
+++ b/ld/obj.c
@@ -7,9 +7,13 @@
#include "../inc/scc.h"
#include "ld.h"
+#define NR_SYM_HASH 64
+
Obj *objlst;
static Obj *tail;
+static Symbol *symtbl[NR_SYM_HASH];
+
Obj *
newobj(char *fname, char *member)
{
@@ -45,5 +49,31 @@
Symbol *
lookup(char *name)
{
- return NULL;
+ unsigned h, c;
+ char *s;
+ size_t len;
+ Symbol *sym;
+
+ for (h = 0; c = *name; ++s)
+ h = h*33 ^ c;
+ h &= NR_SYM_HASH-1;
+
+ for (sym = symtbl[h]; sym; sym = sym->hash) {
+ s = sym->name;
+ if (*name == *s && !strcmp(name, s))
+ return sym;
+ }
+
+ len = strlen(name) + 1;
+ sym = malloc(sizeof(*sym));
+ s = malloc(len);
+ if (!sym || !s)
+ outmem();
+ sym->hash = symtbl[h];
+ symtbl[h] = sym;
+ sym->name = s;
+ memset(sym, 0, sizeof(*sym));
+ memcpy(sym->name, name, len);
+
+ return sym;
}