ref: dccebc26a1d24f28a40b96d428e6409999ae01f8
parent: a91a01b018c181d150937db6f74cb1f7d0e3ebde
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Aug 22 03:53:00 EDT 2019
[libmach] Simplify objread() interface There were two additional funcitons, objsyms() and objsect() because objread() only read the target file in a target structure. This mean that after every objread() we had to a objsyms() and in some cases a objsect(). This patch simplifies the interface and it makes objread() to generate the symbols and sections machine independent form always.
--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -68,8 +68,6 @@
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);
-extern int objsyms(Obj *obj);
extern int objpos(Obj *obj, FILE *fp, long pos);
extern int archive(FILE *fp);
extern long armember(FILE *fp, char *member);
--- a/src/cmd/ld/pass1.c
+++ b/src/cmd/ld/pass1.c
@@ -188,11 +188,6 @@
error("object file corrupted");
goto delete;
}
-
- if (objsyms(obj) < 0 || objsect(obj) < 0) {
- error("object file corrupted");
- goto delete;
- }
if (inlib && !is_needed(obj))
goto delete;
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -153,7 +153,7 @@
return;
}
- if (objread(obj, fp) < 0 || objsyms(obj) < 0)
+ if (objread(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 || objsyms(obj) < 0) {
+ if (objread(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 || objsect(obj) < 0) {
+ if (objread(obj, fp) < 0) {
error("file corrupted");
goto err;
}
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -9,8 +9,6 @@
objread.o \
objfree.o \
objstrip.o \
- objsect.o \
- objsyms.o \
objdel.o \
objaddseg.o \
objsync.o \
--- a/src/libmach/objread.c
+++ b/src/libmach/objread.c
@@ -4,12 +4,20 @@
#include "libmach.h"
-static int (*funv[])(Obj *, FILE *) = {
+static int (*freadv[])(Obj *, FILE *) = {
[COFF32] = coff32read,
};
-int
-objread(Obj *obj, FILE *fp)
+static int (*fsymsv[])(Obj *) = {
+ [COFF32] = coff32getsyms,
+};
+
+static int (*fsectsv[])(Obj *) = {
+ [COFF32] = coff32getsect,
+};
+
+static int
+getsects(Obj *obj)
{
int fmt;
@@ -17,9 +25,51 @@
if (fmt >= NFORMATS)
return -1;
- if ((*funv[fmt])(obj, fp) < 0)
+ return (*fsectsv[fmt])(obj);
+}
+
+static int
+getsyms(Obj *obj)
+{
+ int fmt;
+
+ fmt = FORMAT(obj->type);
+ if (fmt >= NFORMATS)
return -1;
+
+ return (*fsymsv[fmt])(obj);
+}
+
+static int
+readfile(Obj *obj, FILE *fp)
+{
+ int fmt;
+
+ fmt = FORMAT(obj->type);
+ if (fmt >= NFORMATS)
+ return -1;
+
+ if ((*freadv[fmt])(obj, fp) < 0)
+ return -1;
obj->fp = fp;
return 0;
+}
+
+int
+objread(Obj *obj, FILE *fp)
+{
+ objfree(obj, TARGETDEL | GENERICDEL);
+
+ if (readfile(obj, fp) < 0)
+ goto err;
+ if (getsyms(obj) < 0)
+ goto err;
+ if (getsects(obj) < 0)
+ goto err;
+ return 0;
+
+err:
+ objfree(obj, TARGETDEL | GENERICDEL);
+ return -1;
}
--- a/src/libmach/objsect.c
+++ /dev/null
@@ -1,21 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-static int (*funv[])(Obj *) = {
- [COFF32] = coff32getsect,
-};
-
-int
-objsect(Obj *obj)
-{
- int fmt;
-
- fmt = FORMAT(obj->type);
- if (fmt >= NFORMATS)
- return -1;
-
- return (*funv[fmt])(obj);
-}
--- a/src/libmach/objsyms.c
+++ /dev/null
@@ -1,21 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-static int (*funv[])(Obj *) = {
- [COFF32] = coff32getsyms,
-};
-
-int
-objsyms(Obj *obj)
-{
- int fmt;
-
- fmt = FORMAT(obj->type);
- if (fmt >= NFORMATS)
- return -1;
-
- return (*funv[fmt])(obj);
-}