shithub: scc

Download patch

ref: 200269262ccb43af46f6357140df8a232dad256d
parent: a0d56750847f36ffc56a813eec79c31de8fefeeb
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Aug 23 03:51:54 EDT 2019

[libmach] Add setidx and getidx methods to Objops

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -3,6 +3,7 @@
 typedef struct objsect Objsect;
 typedef struct objsym Objsym;
 typedef struct objsymdef Objsymdef;
+typedef struct objops Objops;
 typedef struct obj Obj;
 
 enum sectype {
@@ -43,8 +44,16 @@
 	Objsymdef *hash, *next;
 };
 
+struct objops {
+	int (*new)(Obj *obj);
+	int (*read)(Obj *obj, FILE *fp);
+	int (*setidx)(long nsyms, Objsymdef *def, FILE *fp);
+	int (*getidx)(long *nsyms, Objsymdef **def, FILE *fp);
+};
+
 struct obj {
 	int type;
+	Objops *ops;
 	char *index;
 	Objsym *htab[NR_SYMHASH];
 	Objsym *syms;
@@ -53,12 +62,8 @@
 	int nsecs;
 	int nsyms;
 	void *data;
-
-	int (*new)(Obj *obj);
-	int (*read)(Obj *obj, FILE *fp);
 };
 
-
 extern int formember(FILE *fp,
                      int (*fn)(FILE *, char *, void *),
                      void *data);
@@ -72,8 +77,6 @@
 extern int objpos(Obj *obj, FILE *fp, long pos);
 extern int archive(FILE *fp);
 extern long armember(FILE *fp, char *member);
-extern long setindex(int type, long nsyms, Objsymdef *def, FILE *fp);
-extern int getindex(int type, long *nsyms, Objsymdef **def, FILE *fp);
 
 
 /* TODO */
--- a/src/cmd/addr2line.c
+++ b/src/cmd/addr2line.c
@@ -93,7 +93,7 @@
 		return NULL;
 	}
 
-	if ((*obj->read)(obj, fp) < 0) {
+	if ((*obj->ops->read)(obj, fp) < 0) {
 		error("file corrupted");
 		return NULL;
 	}
--- a/src/cmd/ld/pass1.c
+++ b/src/cmd/ld/pass1.c
@@ -16,6 +16,7 @@
 };
 
 int bintype = -1;
+static Objops *binops;
 static Symbol refhead = {
 	.next = &refhead,
 	.prev = &refhead,
@@ -183,8 +184,9 @@
 		goto delete;
 	}
 	bintype = type;
+	binops = obj->ops;
  
-	if ((*obj->read)(obj, fp) < 0) {
+	if ((*binops->read)(obj, fp) < 0) {
 		error("object file corrupted");
 		goto delete;
 	}
@@ -208,7 +210,7 @@
 	Objsymdef *def, *dp;
 	Symbol *sym;
 
-	if (getindex(bintype, &n, &def, fp) < 0) {
+	if ((*binops->getidx)(&n, &def, fp) < 0) {
 		error("corrupted index");
 		return;
 	}
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -153,7 +153,7 @@
 		return;
 	}
 
-	if ((*obj->read)(obj, fp) < 0)
+	if ((*obj->ops->read)(obj, fp) < 0)
 		goto error;
 
 	for (sym = obj->syms; sym; sym = sym->next)
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -19,6 +19,7 @@
 static char *namidx;
 static long nsymbols;
 static int status, artype, nolib;
+static Objops *ops;
 static char *filename, *membname;
 static Objsymdef *htab[NR_SYMDEF], *head;
 static long offset;
@@ -117,7 +118,6 @@
 static int
 newmember(FILE *fp, char *nam, void *data)
 {
-
 	int t, ret = 0;
 	Obj *obj;
 	Objsym *sym;
@@ -144,9 +144,10 @@
 		error("out of memory");
 		return 0;
 	}
+	ops = obj->ops;
 	namidx = obj->index;
 
-	if ((*obj->read)(obj, fp) < 0) {
+	if ((*ops->read)(obj, fp) < 0) {
 		error("file corrupted");
 		goto error;
 	}
@@ -269,7 +270,6 @@
 {
 	int c;
 	FILE *fp, *idx, *out;
-	long siz;
 	struct fprop prop;
 
 	errno = 0;
@@ -291,13 +291,12 @@
 	if (nolib)
 		goto error;
 
-	siz = setindex(artype, nsymbols, head, idx);
-	if (siz <= 0)
+	if ((*ops->setidx)(nsymbols, head, idx) < 0)
 		goto error;
 
 	if (getstat(fname, &prop) < 0)
 		goto error;
-	prop.size = siz;
+	prop.size = ftell(idx);
 	prop.time = time(NULL);
 
 	if (!merge(out, &prop, fp, idx))
--- a/src/cmd/size.c
+++ b/src/cmd/size.c
@@ -50,7 +50,7 @@
 		return;
 	}
 
-	if ((*obj->read)(obj, fp) < 0) {
+	if ((*obj->ops->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 ((*obj->read)(obj, fp) < 0) {
+	if ((*obj->ops->read)(obj, fp) < 0) {
 		error("file corrupted");
 		goto err3;
 	}
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -16,8 +16,6 @@
        archive.o \
        armember.o \
        objlookup.o \
-       getindex.o \
-       setindex.o \
        formember.o \
        objtype.o \
        objwrite.o \
--- a/src/libmach/coff32/Makefile
+++ b/src/libmach/coff32/Makefile
@@ -9,8 +9,10 @@
        coff32read.o \
        coff32strip.o \
        coff32write.o \
-       coff32setindex.o \
-       coff32getindex.o \
+       coff32setidx.o \
+       coff32xsetidx.o \
+       coff32getidx.o \
+       coff32xgetidx.o \
        coff32setidx.o \
        coff32getidx.o \
        coff32sync.o \
--- a/src/libmach/coff32/coff32.c
+++ b/src/libmach/coff32/coff32.c
@@ -5,7 +5,9 @@
 #include "../libmach.h"
 #include "coff32.h"
 
-struct objfmt coff32 = {
+Objops coff32 = {
 	.new = coff32new,
 	.read = coff32read,
+	.getidx = coff32getidx,
+	.setidx = coff32setidx,
 };
--- a/src/libmach/coff32/coff32.h
+++ b/src/libmach/coff32/coff32.h
@@ -26,3 +26,8 @@
 
 extern int coff32new(Obj *obj);
 extern int coff32read(Obj *obj, FILE *fp);
+extern int coff32setidx(long nsymbols, Objsymdef *head, FILE *fp);
+extern int coff32getidx(long *nsyms, Objsymdef **def, FILE *fp);
+
+extern int coff32xsetidx(int order, long nsyms, Objsymdef *head, FILE *fp);
+extern int coff32xgetidx(int order, long *nsyms, Objsymdef **def, FILE *fp);
--- a/src/libmach/coff32/coff32getidx.c
+++ b/src/libmach/coff32/coff32getidx.c
@@ -1,62 +1,12 @@
 #include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 
 #include <scc/mach.h>
-#include <scc/cstd.h>
 
 #include "../libmach.h"
 #include "coff32.h"
 
 int
-coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp)
+coff32getidx(long *nsyms, Objsymdef **def, FILE *fp)
 {
-	int j, c;
-	long i, n;
-	char *s;
-	Objsymdef *bp;
-	unsigned char buf[EXTIDENTSIZ];
-
-	if (fread(buf, 4, 1, fp) != 1)
-		return -1;
-	unpack(order, buf, "l", &n);
-
-	if (n <= 0)
-		return -1;
-
-	if ((bp = calloc(sizeof(*bp), n)) == NULL)
-		return -1;
-
-	for (i = 1; i < n-1; i++)
-		bp[i].next = &bp[i-1];
-
-	for (i = 0; i < n; i++) {
-		fread(buf, 4, 1, fp);
-		unpack(order, buf, "l", &bp[i].offset);
-	}
-
-	for (i = 0; i < n; i++) {
-		for (j = 0; (c = getc(fp)) != EOF && c != '\0'; j++)
-			buf[j] = c;
-		buf[j++] = '\0';
-
-		if ((s = malloc(j)) == NULL)
-			goto error;
-		memcpy(s, buf, j);
-		bp[i].name = s;
-	}
-
-	if (ferror(fp))
-		goto error;
-
-	*nsyms = n;
-	*def = bp;
-
-	return 0;
-
-error:
-	for (i = 0; i < n; i++)
-		free(bp[i].name);
-	free(bp);
-	return -1;
+	return coff32xgetidx(BIG_ENDIAN, nsyms, def, fp);
 }
--- a/src/libmach/coff32/coff32getindex.c
+++ /dev/null
@@ -1,12 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "../libmach.h"
-#include "coff32.h"
-
-int
-coff32getindex(int type, long *nsyms, Objsymdef **def, FILE *fp)
-{
-	return coff32getidx(BIG_ENDIAN, nsyms, def, fp);
-}
--- a/src/libmach/coff32/coff32setidx.c
+++ b/src/libmach/coff32/coff32setidx.c
@@ -1,33 +1,12 @@
 #include <stdio.h>
-#include <string.h>
 
 #include <scc/mach.h>
 
 #include "../libmach.h"
+#include "coff32.h"
 
-long
-coff32setidx(int order, long nsyms, Objsymdef *head, FILE *fp)
+int
+coff32setidx(long nsymbols, Objsymdef *head, FILE *fp)
 {
-	long i, n;
-	size_t len;
-	Objsymdef *def;
-	unsigned char buff[4];
-
-	pack(order, buff, "l", nsyms);
-	fwrite(buff, 4, 1, fp);
-	n = 4;
-
-	for (def = head; def; def = def->next) {
-		pack(order, buff, "l", (long) def->offset);
-		fwrite(buff, 4, 1, fp);
-		n += 4;
-	}
-
-	for (def = head; def; def = def->next) {
-		len = strlen(def->name) + 1;
-		fwrite(def->name, len, 1, fp);
-		n += len;
-	}
-
-	return fflush(fp) == EOF ? -1 : n;
+	return coff32xsetidx(BIG_ENDIAN, nsymbols, head, fp);
 }
--- a/src/libmach/coff32/coff32setindex.c
+++ /dev/null
@@ -1,12 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "../libmach.h"
-#include "coff32.h"
-
-long
-coff32setindex(int type, long nsymbols, Objsymdef *head, FILE *fp)
-{
-	return coff32setidx(BIG_ENDIAN, nsymbols, head, fp);
-}
--- /dev/null
+++ b/src/libmach/coff32/coff32xgetidx.c
@@ -1,0 +1,62 @@
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <scc/mach.h>
+#include <scc/cstd.h>
+
+#include "../libmach.h"
+#include "coff32.h"
+
+int
+coff32xgetidx(int order, long *nsyms, Objsymdef **def, FILE *fp)
+{
+	int j, c;
+	long i, n;
+	char *s;
+	Objsymdef *bp;
+	unsigned char buf[EXTIDENTSIZ];
+
+	if (fread(buf, 4, 1, fp) != 1)
+		return -1;
+	unpack(order, buf, "l", &n);
+
+	if (n <= 0)
+		return -1;
+
+	if ((bp = calloc(sizeof(*bp), n)) == NULL)
+		return -1;
+
+	for (i = 1; i < n-1; i++)
+		bp[i].next = &bp[i-1];
+
+	for (i = 0; i < n; i++) {
+		fread(buf, 4, 1, fp);
+		unpack(order, buf, "l", &bp[i].offset);
+	}
+
+	for (i = 0; i < n; i++) {
+		for (j = 0; (c = getc(fp)) != EOF && c != '\0'; j++)
+			buf[j] = c;
+		buf[j++] = '\0';
+
+		if ((s = malloc(j)) == NULL)
+			goto error;
+		memcpy(s, buf, j);
+		bp[i].name = s;
+	}
+
+	if (ferror(fp))
+		goto error;
+
+	*nsyms = n;
+	*def = bp;
+
+	return 0;
+
+error:
+	for (i = 0; i < n; i++)
+		free(bp[i].name);
+	free(bp);
+	return -1;
+}
--- /dev/null
+++ b/src/libmach/coff32/coff32xsetidx.c
@@ -1,0 +1,33 @@
+#include <stdio.h>
+#include <string.h>
+
+#include <scc/mach.h>
+
+#include "../libmach.h"
+
+int
+coff32xsetidx(int order, long nsyms, Objsymdef *head, FILE *fp)
+{
+	long i, n;
+	size_t len;
+	Objsymdef *def;
+	unsigned char buff[4];
+
+	pack(order, buff, "l", nsyms);
+	fwrite(buff, 4, 1, fp);
+	n = 4;
+
+	for (def = head; def; def = def->next) {
+		pack(order, buff, "l", (long) def->offset);
+		fwrite(buff, 4, 1, fp);
+		n += 4;
+	}
+
+	for (def = head; def; def = def->next) {
+		len = strlen(def->name) + 1;
+		fwrite(def->name, len, 1, fp);
+		n += len;
+	}
+
+	return fflush(fp) == EOF ? -1 : 0;
+}
--- a/src/libmach/coff32/deps.mk
+++ b/src/libmach/coff32/deps.mk
@@ -1,14 +1,13 @@
 #deps
+./coff32.o: $(INCDIR)/scc/scc/mach.h
+./coff32.o: ./../libmach.h
+./coff32.o: ./coff32.h
 ./coff32del.o: $(INCDIR)/scc/scc/mach.h
 ./coff32del.o: ./../libmach.h
 ./coff32del.o: ./coff32.h
-./coff32getidx.o: $(INCDIR)/scc/scc/cstd.h
 ./coff32getidx.o: $(INCDIR)/scc/scc/mach.h
 ./coff32getidx.o: ./../libmach.h
 ./coff32getidx.o: ./coff32.h
-./coff32getindex.o: $(INCDIR)/scc/scc/mach.h
-./coff32getindex.o: ./../libmach.h
-./coff32getindex.o: ./coff32.h
 ./coff32new.o: $(INCDIR)/scc/scc/mach.h
 ./coff32new.o: ./../libmach.h
 ./coff32new.o: ./coff32.h
@@ -20,9 +19,7 @@
 ./coff32read.o: ./coff32.h
 ./coff32setidx.o: $(INCDIR)/scc/scc/mach.h
 ./coff32setidx.o: ./../libmach.h
-./coff32setindex.o: $(INCDIR)/scc/scc/mach.h
-./coff32setindex.o: ./../libmach.h
-./coff32setindex.o: ./coff32.h
+./coff32setidx.o: ./coff32.h
 ./coff32strip.o: $(INCDIR)/scc/scc/mach.h
 ./coff32strip.o: ./../libmach.h
 ./coff32strip.o: ./coff32.h
@@ -31,3 +28,9 @@
 ./coff32write.o: $(INCDIR)/scc/scc/mach.h
 ./coff32write.o: ./../libmach.h
 ./coff32write.o: ./coff32.h
+./coff32xgetidx.o: $(INCDIR)/scc/scc/cstd.h
+./coff32xgetidx.o: $(INCDIR)/scc/scc/mach.h
+./coff32xgetidx.o: ./../libmach.h
+./coff32xgetidx.o: ./coff32.h
+./coff32xsetidx.o: $(INCDIR)/scc/scc/mach.h
+./coff32xsetidx.o: ./../libmach.h
--- a/src/libmach/deps.mk
+++ b/src/libmach/deps.mk
@@ -7,8 +7,8 @@
 ./armember.o: $(INCDIR)/scc/scc/mach.h
 ./formember.o: $(INCDIR)/scc/scc/ar.h
 ./formember.o: $(INCDIR)/scc/scc/mach.h
-./getindex.o: $(INCDIR)/scc/scc/mach.h
-./getindex.o: ./libmach.h
+./mach.o: $(INCDIR)/scc/scc/mach.h
+./mach.o: ./libmach.h
 ./objaddseg.o: $(INCDIR)/scc/scc/mach.h
 ./objdel.o: $(INCDIR)/scc/scc/mach.h
 ./objdel.o: ./libmach.h
@@ -20,8 +20,6 @@
 ./objnew.o: ./libmach.h
 ./objpos.o: $(INCDIR)/scc/scc/mach.h
 ./objpos.o: ./libmach.h
-./objread.o: $(INCDIR)/scc/scc/mach.h
-./objread.o: ./libmach.h
 ./objstrip.o: $(INCDIR)/scc/scc/mach.h
 ./objstrip.o: ./libmach.h
 ./objsync.o: $(INCDIR)/scc/scc/mach.h
@@ -32,7 +30,5 @@
 ./objwrite.o: ./libmach.h
 ./pack.o: $(INCDIR)/scc/scc/mach.h
 ./pack.o: ./libmach.h
-./setindex.o: $(INCDIR)/scc/scc/mach.h
-./setindex.o: ./libmach.h
 ./unpack.o: $(INCDIR)/scc/scc/mach.h
 ./unpack.o: ./libmach.h
--- a/src/libmach/getindex.c
+++ /dev/null
@@ -1,21 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-static int (*funv[])(int, long*, Objsymdef**, FILE*) = {
-	[COFF32] = coff32getidx,
-};
-
-int
-getindex(int type, long *nsyms, Objsymdef **head, FILE *fp)
-{
-	int fmt;
-
-	fmt = FORMAT(type);
-	if (fmt >= NFORMATS)
-		return -1;
-
-	return (*funv[fmt])(type, nsyms, head, fp);
-}
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -28,11 +28,6 @@
 	TARGETDEL  = 1 << 1,
 };
 
-struct objfmt {
-	int (*new)(Obj *obj);
-	int (*read)(Obj *obj, FILE *fp);
-};
-
 /* common functions */
 extern int pack(int order, unsigned char *dst, char *fmt, ...);
 extern int unpack(int order, unsigned char *src, char *fmt, ...);
@@ -40,20 +35,14 @@
 
 /* coff32 functions */
 /* 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 coff32write(Obj *obj, FILE *fp);
 extern void coff32strip(Obj *obj);
 extern int coff32probe(unsigned char *buf, char **name);
 
-extern long coff32setindex(int type, long nsymbols, Objsymdef *head, FILE *fp);
-extern long coff32setidx(int order, long nsyms, Objsymdef *head, FILE *fp);
-
-extern int coff32getindex(int type, long *nsyms, Objsymdef **def, FILE *fp);
-extern int coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp);
-
 extern char *coff32namidx(void);
 extern int coff32sync(Obj *obj);
 
 /* globals */
-extern struct objfmt *objfmts[];
+extern Objops *objops[];
+extern Objops coff32;
--- a/src/libmach/mach.c
+++ b/src/libmach/mach.c
@@ -4,9 +4,7 @@
 
 #include "libmach.h"
 
-extern struct objfmt coff32;
-
-struct objfmt *objfmts[] = {
+Objops *objops[] = {
 	[COFF32] = &coff32,
 	[NFORMATS] = NULL,
 };
--- a/src/libmach/objnew.c
+++ b/src/libmach/objnew.c
@@ -11,7 +11,7 @@
 {
 	Obj *obj;
 	int fmt;
-	struct objfmt *op;
+	Objops *ops;
 
 	fmt = FORMAT(type);
 	if (fmt >= NFORMATS)
@@ -27,11 +27,10 @@
 	obj->nsecs = 0;
 	memset(obj->htab, 0, sizeof(obj->htab));
 
-	op = objfmts[fmt];
-	obj->new = op->new;
-	obj->read = op->read;
+	ops = objops[fmt];
+	obj->ops = ops;
 
-	if ((*obj->new)(obj) < 0) {
+	if ((*ops->new)(obj) < 0) {
 		free(obj);
 		return NULL;
 	}
--- a/src/libmach/setindex.c
+++ /dev/null
@@ -1,21 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-static long (*funv[])(int, long, Objsymdef *, FILE *) = {
-	[COFF32] = coff32setidx,
-};
-
-long
-setindex(int type, long nsyms, Objsymdef *head, FILE *fp)
-{
-	int fmt;
-
-	fmt = FORMAT(type);
-	if (fmt >= NFORMATS)
-		return -1;
-
-	return (*funv[fmt])(type, nsyms, head, fp);
-}