commit 18542295c55bbb5378505b1d2790ba96f4a11188
parent ed8597efe922ee43a91b1f96f6701a683cbbd941
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sun, 10 Feb 2019 11:12:45 +0000
[ld] Load objects from libraries if needed
When the library doesn't have an index then it is needed
to traverse the library and check if every object defines
a symbol that is needed.
Diffstat:
3 files changed, 37 insertions(+), 5 deletions(-)
diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h
@@ -63,6 +63,10 @@ extern int forsect(Obj *obj,
int (*fn)(Objsect *sect, void *data),
void *data);
+extern int foridx(FILE *fp,
+ int (*fn)(Objsymdef *def, void *data),
+ void *data);
+
extern int archive(FILE *fp);
extern long armember(FILE *fp, char *member);
extern int objtype(FILE *fp, char **name);
diff --git a/src/cmd/ld/main.c b/src/cmd/ld/main.c
@@ -18,6 +18,11 @@ static char sccsid[] = "@(#) ./ld/main.c";
typedef struct objlst Objlst;
typedef struct symbol Symbol;
+enum {
+ OUTLIB,
+ INLIB,
+};
+
struct objlst {
Obj *obj;
struct objlst *next;
@@ -175,9 +180,10 @@ load(Obj *obj)
}
static void
-newobject(FILE *fp, int type)
+newobject(FILE *fp, int type, int inlib)
{
Obj *obj;
+ Symbol *sym;
if ((obj = objnew(type)) == NULL) {
error("out of memory");
@@ -186,14 +192,22 @@ newobject(FILE *fp, int type)
if (objread(obj, fp) < 0) {
error("object file corrupted");
- goto error;
+ goto delete;
}
+ if (inlib) {
+ for (sym = refhead.next; sym != &refhead; sym = sym->next) {
+ if (objlookup(obj, sym->name))
+ break;
+ }
+ if (sym == &refhead)
+ goto delete;
+ }
load(obj);
return;
-error:
+delete:
objdel(obj);
return;
}
@@ -201,13 +215,26 @@ error:
static int
newmember(FILE *fp, char *name, void *data)
{
+ int t;
+
+ if ((t = objtype(fp, NULL)) == -1)
+ return 1;
+ newobject(fp, t, INLIB);
return 1;
}
static int
-newlibrary(FILE *fp)
+newidx(Objsymdef *def, void *data)
{
+ /* TODO */
+ return 0;
+}
+static int
+newlibrary(FILE *fp)
+{
+ if (foridx(fp, newidx, NULL))
+ return 1;
return artraverse(fp, newmember, NULL);
}
@@ -250,7 +277,7 @@ pass1(int argc, char *argv[])
continue;
if ((t = objtype(fp, NULL)) != -1)
- newobject(fp, t);
+ newobject(fp, t, OUTLIB);
else if (archive(fp))
newlibrary(fp);
else
diff --git a/src/libmach/Makefile b/src/libmach/Makefile
@@ -19,6 +19,7 @@ OBJS = addr2line.o \
objstrip.o \
forsym.o \
forsect.o \
+ foridx.o \
objtype.o \
objwrite.o \
objfree.o \