shithub: scc

Download patch

ref: e72d3db350f7b4a720009bcf1b1bf803cc74f8e9
parent: a7140d1787745b7e481425f820507051a132cfb1
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri May 11 15:56:02 EDT 2018

[ld/coff32] Create readobj()

This function loads all the metadata of the object file in memory.

--- a/ld/coff32.c
+++ b/ld/coff32.c
@@ -103,6 +103,9 @@
 			return -1;
 		getscn(buff, &scn[i]);
 	}
+	obj->scnhdr = scn;
+
+	return 0;
 }
 
 static void
@@ -151,23 +154,21 @@
 			return -1;
 		getsym(buff, &ent[i]);
 	}
+	obj->enthdr = ent;
+
 	return 0;
 }
 
 static void
-pass1(char *fname, char *member, FILE *fp)
+readobj(Obj *obj)
 {
 	unsigned char buff[FILHSZ];
 	FILHDR *hdr;
-	Obj *obj;
 	char *strtbl;
 	long symoff, secoff, stroff, pos;
 
-	obj = newobj(fname, member);
-	obj->fp = fp;
-
-	pos = ftell(fp);
-	if (fread(buff, FILHSZ, 1, fp) != 1)
+	pos = ftell(obj->fp);
+	if (fread(buff, FILHSZ, 1, obj->fp) != 1)
 		goto bad_file;
 
 	if ((hdr = malloc(sizeof(*hdr))) == NULL)
@@ -186,11 +187,22 @@
 		goto bad_file;
 	if (readsects(obj, secoff) < 0)
 		goto bad_file;
+	return;
 
 bad_file:
-	if (ferror(fp))
-		die("ld: %s: %s", fname, strerror(errno));
-	die("ld: %s: corrupted file", fname);
+	if (ferror(obj->fp))
+		die("ld: %s: %s", obj->fname, strerror(errno));
+	die("ld: %s: corrupted file", obj->fname);
+}
+
+static void
+pass1(char *fname, char *member, FILE *fp)
+{
+	Obj *obj;
+
+	obj = newobj(fname, member);
+	obj->fp = fp;
+	readobj(obj);
 }
 
 static void
--- a/ld/ld.h
+++ b/ld/ld.h
@@ -7,7 +7,8 @@
 	char *member;
 	FILE *fp;
 	void *filhdr;
-	void *sechdr;
+	void *scnhdr;
+	void *enthdr;
 	char *strtbl;
 	struct obj *next;
 };
--- a/ld/main.c
+++ b/ld/main.c
@@ -130,7 +130,7 @@
 	if ((fp = fopen(fname, "rb")) == NULL)
 		die("ld: %s: %s", fname, strerror(errno));
 
-	if (!object(fname, fname, fp) && !archive(fname, fp))
+	if (!object(fname, NULL, fp) && !archive(fname, fp))
 		die("ld: %s: File format not recognized", fname);
 
 	if (ferror(fp))
--- a/ld/obj.c
+++ b/ld/obj.c
@@ -15,16 +15,23 @@
 {
 	Obj *obj;
 	char *s, *t;
-	size_t l1 = strlen(member), l2 = strlen(fname);
+	size_t len;
 
+	len = strlen(fname);
 	obj = malloc(sizeof(*obj));
-	s = malloc(l1+1);
-	t = malloc(l2+1);
-	if (!obj || !s || !t)
+	s = malloc(len+1);
+	if (!obj || !s)
 		outmem();
+	obj->fname = memcpy(s, fname, len+1);
 
-	obj->fname = memcpy(s, fname, l1);
-	obj->member = memcpy(t, member, l2);
+	if (!member) {
+		obj->member = NULL;
+	} else {
+		len = strlen(member);
+		if ((s = malloc(len+1)) == NULL)
+			outmem();
+		obj->member = memcpy(s, member, len+1);
+	}
 	obj->next = NULL;
 
 	if (!objlst)