shithub: scc

Download patch

ref: 0b07caedf14634c43f549bcab131ad34ab4ffed7
parent: 3e6e8f74f042a31ace6ae54247790f9a9230285a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Feb 10 06:12:45 EST 2019

[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.

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -63,6 +63,10 @@
                    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);
--- a/src/cmd/ld/main.c
+++ b/src/cmd/ld/main.c
@@ -18,6 +18,11 @@
 typedef struct objlst Objlst;
 typedef struct symbol Symbol;
 
+enum {
+	OUTLIB,
+	INLIB,
+};
+
 struct objlst {
 	Obj *obj;
 	struct objlst *next;
@@ -175,9 +180,10 @@
 }
 
 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 @@
 
 	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 @@
 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 @@
 			continue;
 
 		if ((t = objtype(fp, NULL)) != -1)
-			newobject(fp, t);
+			newobject(fp, t, OUTLIB);
 		else if (archive(fp))
 			newlibrary(fp);
 		else
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -19,6 +19,7 @@
        objstrip.o \
        forsym.o \
        forsect.o \
+       foridx.o \
        objtype.o \
        objwrite.o \
        objfree.o \