shithub: scc

Download patch

ref: dda941923a06fad72864abc97d933957137e90d1
parent: 51f244d2ec7cce0cd116df73b29e3c9ff5cc3425
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Feb 11 16:58:22 EST 2019

[libmach] Add objsyms()

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -42,9 +42,11 @@
 struct object {
 	int type;
 	Objsym *htab[NR_SYMHASH];
-	Objsym *symbols;
+	Objsym *syms;;
+	Objsect *secs;
 	fpos_t pos;
 	int nsecs;
+	int nsyms;
 	void *data;
 };
 
@@ -60,7 +62,7 @@
 extern Objsym *objlookup(Obj *obj, char *name, int install);
 extern int objstrip(Obj *obj);
 extern int objwrite(Obj *obj, FILE *fp);
-extern int objsect(Obj *obj, Objsect **sect);
+extern int objsect(Obj *obj);
 extern int objsyms(Obj *obj);
 extern int archive(FILE *fp);
 extern long armember(FILE *fp, char *member);
--- a/src/cmd/ld.c
+++ b/src/cmd/ld.c
@@ -30,7 +30,6 @@
 
 struct objlst {
 	Obj *obj;
-	int nsect;
 	Objsect *sect;
 	struct objlst *next;
 };
@@ -207,16 +206,11 @@
 		return;
 	}
 
-	if ((n = objsect(obj, &secp)) < 0)
+	if (objsect(obj) < 0 || objsyms(obj) < 0)
 		goto err1;
 
-	if (objsyms(obj) < 0)
-		goto err2;
-
 	lst->obj = obj;
 	lst->next = NULL;
-	lst->nsect = n;
-	lst->sect = secp;
 
 	if (!objlast)
 		objlast = objhead = lst;
@@ -223,13 +217,11 @@
 	else
 		objlast = objlast->next = lst;
 
-	for (sym = obj->symbols; sym; sym = sym->next)
+	for (sym = obj->syms; sym; sym = sym->next)
 		newsym(sym, obj);
 
 	return;
 
-err2:
-	free(secp);
 err1:
 	free(lst);
 	error("out of memory");
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -158,7 +158,7 @@
 	if (objread(obj, fp) < 0 || objsyms(obj))
 		goto error;
 
-	for (sym = obj->symbols; sym; sym = sym->next)
+	for (sym = obj->syms; sym; sym = sym->next)
 		newsym(sym, &tbl);
 
 	printsyms(tbl.buf, tbl.nsyms);
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -149,7 +149,7 @@
 		goto error;
 	}
 
-	for (sym = obj->symbols; sym; sym = sym->next) {
+	for (sym = obj->syms; sym; sym = sym->next) {
 		if (!newsymbol(sym))
 			goto error;
 	}
--- a/src/cmd/size.c
+++ b/src/cmd/size.c
@@ -39,7 +39,7 @@
 static void
 newobject(FILE *fp, int type)
 {
-	int n, i;;
+	int i;
 	Obj *obj;
 	unsigned long long total, *p;
 	Objsect *secp;
@@ -50,19 +50,15 @@
 		return;
 	}
 
-	if (objread(obj, fp) < 0) {
+	if (objread(obj, fp) < 0 || objsect(obj) < 0) {
 		error("file corrupted");
-		goto err1;
+		goto err;
 	}
 
 	siz.text = siz.data = siz.bss = 0;
-	if ((n = objsect(obj, &secp)) < 0) {
-		error("out of memory");
-		goto err1;
-	}
-
-	for (i = 0; i < n; i++) {
-		switch (secp[i].type) {
+	for (i = 0; i < obj->nsyms; i++) {
+		secp = &obj->secs[i];
+		switch (secp->type) {
 		case 'T':
 			p = &siz.text;
 			break;
@@ -78,7 +74,7 @@
 
 		if (*p > ULLONG_MAX - secp->size) {
 			error("integer overflow");
-			goto err2;
+			goto err;
 		}
 			
 		*p += secp->size;
@@ -96,9 +92,7 @@
 	tbss += siz.bss;
 	ttotal += total;
 
-err2:
-	free(secp);
-err1:
+err:
 	objdel(obj);
 }
 
--- a/src/libmach/coff32/coff32getsect.c
+++ b/src/libmach/coff32/coff32getsect.c
@@ -7,7 +7,7 @@
 #include "coff32.h"
 
 int
-coff32getsect(Obj *obj, Objsect **sectp)
+coff32getsect(Obj *obj)
 {
 	int nsecs;
 	unsigned sflags, type;
@@ -68,6 +68,7 @@
 		sp->type = type;
 		nsecs++;
 	}
+	obj->secs = secs;
 
 	return 1;
 }
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -36,7 +36,7 @@
 typedef int (*writefun_t)(Obj *obj, FILE *fp);
 typedef long (*setidxfun_t)(int, long, Objsymdef *, FILE *);
 typedef int (*getidxfun_t)(int t, long *n, Objsymdef **def, FILE *fp);
-typedef int (*getsectfun_t)(Obj *obj, Objsect **secp);
+typedef int (*getsectfun_t)(Obj *obj);
 typedef char *(*namidxfun_t)(void);
 typedef int (*getsymsfun_t)(Obj *obj);
 
@@ -61,7 +61,7 @@
 extern int coff32getindex(int type, long *nsyms, Objsymdef **def, FILE *fp);
 extern int coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp);
 
-extern int coff32getsect(Obj *obj, Objsect **secp);
+extern int coff32getsect(Obj *obj);
 
 extern char *coff32namidx(void);
 extern int coff32getsyms(Obj *obj);
--- a/src/libmach/objfree.c
+++ b/src/libmach/objfree.c
@@ -23,8 +23,9 @@
 	}
 
 	if (what & GENERICDEL) {
-		free(obj->symbols);
-		obj->symbols = NULL;
+		free(obj->secs);
+		free(obj->syms);
+		obj->syms = NULL;
 		memset(obj->htab, 0, sizeof(obj->htab));
 	}
 
--- a/src/libmach/objlookup.c
+++ b/src/libmach/objlookup.c
@@ -28,8 +28,8 @@
 	sym->value = 0;
 	sym->hash = obj->htab[h];
 	obj->htab[h] = sym;
-	sym->next = obj->symbols;
-	obj->symbols = sym;
+	sym->next = obj->syms;
+	obj->syms = sym;
 
 	return sym;
 }
--- a/src/libmach/objnew.c
+++ b/src/libmach/objnew.c
@@ -23,7 +23,10 @@
 		return NULL;
 
 	obj->type = type;
-	obj->symbols = NULL;
+	obj->syms = NULL;
+	obj->secs = NULL;
+	obj->nsyms = 0;
+	obj->nsecs = 0;
 	memset(obj->htab, 0, sizeof(obj->htab));
 
 	fn = newv[fmt];
--- a/src/libmach/objsect.c
+++ b/src/libmach/objsect.c
@@ -7,7 +7,7 @@
 extern getsectfun_t getsectv[];
 
 int
-objsect(Obj *obj, Objsect **secp)
+objsect(Obj *obj)
 {
 	int fmt;
 	getsectfun_t fn;
@@ -17,5 +17,5 @@
 		return -1;
 
 	fn = getsectv[fmt];
-	return  (*fn)(obj, secp);
+	return  (*fn)(obj);
 }
--- /dev/null
+++ b/src/libmach/objsyms.c
@@ -1,0 +1,21 @@
+#include <stdio.h>
+
+#include <scc/mach.h>
+
+#include "libmach.h"
+
+extern getsymsfun_t getsymsv[];
+
+int
+objsyms(Obj *obj)
+{
+	int fmt;
+	getsymsfun_t fn;
+
+	fmt = FORMAT(obj->type);
+	if (fmt >= NFORMATS)
+		return -1;
+
+	fn = getsymsv[fmt];
+	return  (*fn)(obj);
+}