ref: 7665b3ee492c998789d3c9c7ad79258411f6485a
parent: 569525f6497387c8088287ec682657cf3f8ff287
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Aug 22 14:46:30 EDT 2019
[libmach] Add new to Obj This is the first step to move to an object oriented interface.
--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -54,6 +54,8 @@
int nsecs;
int nsyms;
void *data;
+
+ int (*new)(Obj *obj);
};
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -4,7 +4,8 @@
TARGET = $(LIBDIR)/libmach.a
-OBJS = objnew.o \
+OBJS = mach.o \
+ objnew.o \
objpos.o \
objread.o \
objfree.o \
--- a/src/libmach/coff32/Makefile
+++ b/src/libmach/coff32/Makefile
@@ -2,7 +2,8 @@
PROJECTDIR =../../..
include $(PROJECTDIR)/scripts/rules.mk
-OBJS = coff32del.o \
+OBJS = coff32.o \
+ coff32del.o \
coff32new.o \
coff32probe.o \
coff32read.o \
--- /dev/null
+++ b/src/libmach/coff32/coff32.c
@@ -1,0 +1,10 @@
+#include <stdio.h>
+
+#include <scc/mach.h>
+
+#include "../libmach.h"
+#include "coff32.h"
+
+struct objfmt coff32 = {
+ .new = coff32new,
+};
--- a/src/libmach/coff32/coff32.h
+++ b/src/libmach/coff32/coff32.h
@@ -23,3 +23,5 @@
char *strtbl;
unsigned long strsiz;
};
+
+extern int coff32new(Obj *obj);
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -28,6 +28,10 @@
TARGETDEL = 1 << 1,
};
+struct objfmt {
+ int (*new)(Obj *obj);
+};
+
/* common functions */
extern int pack(int order, unsigned char *dst, char *fmt, ...);
extern int unpack(int order, unsigned char *src, char *fmt, ...);
@@ -36,7 +40,6 @@
/* coff32 functions */
/* TODO: Move this functions to a coff32 files */
extern long coff32index(int type, long nsyms, Objsymdef *head, FILE *fp);
-extern int coff32new(Obj *obj);
extern void coff32del(Obj *obj);
extern int coff32read(Obj *obj, FILE *fp);
extern int coff32write(Obj *obj, FILE *fp);
@@ -51,3 +54,6 @@
extern char *coff32namidx(void);
extern int coff32sync(Obj *obj);
+
+/* globals */
+extern struct objfmt *objfmts[];
--- /dev/null
+++ b/src/libmach/mach.c
@@ -1,0 +1,12 @@
+#include <stdio.h>
+
+#include <scc/mach.h>
+
+#include "libmach.h"
+
+extern struct objfmt coff32;
+
+struct objfmt *objfmts[] = {
+ [COFF32] = &coff32,
+ [NFORMATS] = NULL,
+};
--- a/src/libmach/objnew.c
+++ b/src/libmach/objnew.c
@@ -6,15 +6,12 @@
#include "libmach.h"
-static int (*funv[])(Obj *) = {
- [COFF32] = coff32new,
-};
-
Obj *
objnew(int type)
{
Obj *obj;
int fmt;
+ struct objfmt *op;
fmt = FORMAT(type);
if (fmt >= NFORMATS)
@@ -30,7 +27,10 @@
obj->nsecs = 0;
memset(obj->htab, 0, sizeof(obj->htab));
- if ((*funv[fmt])(obj) < 0) {
+ op = objfmts[fmt];
+ obj->new = op->new;
+
+ if ((*obj->new)(obj) < 0) {
free(obj);
return NULL;
}