shithub: scc

Download patch

ref: a0d56750847f36ffc56a813eec79c31de8fefeeb
parent: 80fae81bf042571f1d2902914302c8d552c4b95b
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Aug 22 19:03:09 EDT 2019

[libmach] Add read method to Obj

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -55,6 +55,7 @@
 	void *data;
 
 	int (*new)(Obj *obj);
+	int (*read)(Obj *obj, FILE *fp);
 };
 
 
@@ -65,7 +66,6 @@
 extern int objtype(FILE *fp, char **name);
 extern Obj *objnew(int type);
 extern void objdel(Obj *obj);
-extern int objread(Obj *obj, FILE *fp);
 extern Objsym *objlookup(Obj *obj, char *name, int install);
 extern int objstrip(Obj *obj);
 extern int objwrite(Obj *obj, FILE *fp);
--- a/src/cmd/addr2line.c
+++ b/src/cmd/addr2line.c
@@ -93,7 +93,7 @@
 		return NULL;
 	}
 
-	if (!objread(obj, fp)) {
+	if ((*obj->read)(obj, fp) < 0) {
 		error("file corrupted");
 		return NULL;
 	}
--- a/src/cmd/ld/pass1.c
+++ b/src/cmd/ld/pass1.c
@@ -184,7 +184,7 @@
 	}
 	bintype = type;
  
-	if (objread(obj, fp) < 0) {
+	if ((*obj->read)(obj, fp) < 0) {
 		error("object file corrupted");
 		goto delete;
 	}
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -153,7 +153,7 @@
 		return;
 	}
 
-	if (objread(obj, fp) < 0)
+	if ((*obj->read)(obj, fp) < 0)
 		goto error;
 
 	for (sym = obj->syms; sym; sym = sym->next)
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -146,7 +146,7 @@
 	}
 	namidx = obj->index;
 
-	if (objread(obj, fp) < 0) {
+	if ((*obj->read)(obj, fp) < 0) {
 		error("file corrupted");
 		goto error;
 	}
--- a/src/cmd/size.c
+++ b/src/cmd/size.c
@@ -50,7 +50,7 @@
 		return;
 	}
 
-	if (objread(obj, fp) < 0) {
+	if ((*obj->read)(obj, fp) < 0) {
 		error("file corrupted");
 		goto err;
 	}
--- a/src/cmd/strip.c
+++ b/src/cmd/strip.c
@@ -46,7 +46,7 @@
 		error("out of memory");
 		goto err3;
 	}
-	if (objread(obj, fp) < 0) {
+	if ((*obj->read)(obj, fp) < 0) {
 		error("file corrupted");
 		goto err3;
 	}
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -7,7 +7,6 @@
 OBJS = mach.o \
        objnew.o \
        objpos.o \
-       objread.o \
        objfree.o \
        objstrip.o \
        objdel.o \
--- a/src/libmach/coff32/coff32.c
+++ b/src/libmach/coff32/coff32.c
@@ -7,4 +7,5 @@
 
 struct objfmt coff32 = {
 	.new = coff32new,
+	.read = coff32read,
 };
--- a/src/libmach/coff32/coff32.h
+++ b/src/libmach/coff32/coff32.h
@@ -25,3 +25,4 @@
 };
 
 extern int coff32new(Obj *obj);
+extern int coff32read(Obj *obj, FILE *fp);
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -30,6 +30,7 @@
 
 struct objfmt {
 	int (*new)(Obj *obj);
+	int (*read)(Obj *obj, FILE *fp);
 };
 
 /* common functions */
@@ -41,7 +42,6 @@
 /* TODO: Move this functions to a coff32 files */
 extern long coff32index(int type, long nsyms, Objsymdef *head, FILE *fp);
 extern void coff32del(Obj *obj);
-extern int coff32read(Obj *obj, FILE *fp);
 extern int coff32write(Obj *obj, FILE *fp);
 extern void coff32strip(Obj *obj);
 extern int coff32probe(unsigned char *buf, char **name);
--- a/src/libmach/objnew.c
+++ b/src/libmach/objnew.c
@@ -29,6 +29,7 @@
 
 	op = objfmts[fmt];
 	obj->new = op->new;
+	obj->read = op->read;
 
 	if ((*obj->new)(obj) < 0) {
 		free(obj);
--- a/src/libmach/objread.c
+++ /dev/null
@@ -1,24 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-static int (*freadv[])(Obj *, FILE *) = {
-	[COFF32] = coff32read,
-};
-
-int
-objread(Obj *obj, FILE *fp)
-{
-	int fmt;
-
-	fmt = FORMAT(obj->type);
-	if (fmt >= NFORMATS)
-		return -1;
-
-	if ((*freadv[fmt])(obj, fp) < 0)
-		return -1;
-
-	return 0;
-}