shithub: scc

Download patch

ref: 2fd2024f5922f93cfa3b743799fc03a749b94dc9
parent: a8d4f208d8e94b207c1625fa0008a9f247635dd2
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Feb 11 13:28:42 EST 2019

[libmach] Remove forsym()

This is overengineered.

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -53,10 +53,6 @@
                      int (*fn)(FILE *, char *, void *),
                      void *data);
 
-extern int forsym(Obj *obj,
-                  int (*fn)(Objsym *sym, void *data),
-                  void *data);
-
 extern int archive(FILE *fp);
 extern long armember(FILE *fp, char *member);
 extern int objtype(FILE *fp, char **name);
--- a/src/cmd/ld.c
+++ b/src/cmd/ld.c
@@ -164,7 +164,7 @@
 }
 
 static int
-newsym(Objsym *osym, void *obj)
+newsym(Objsym *osym, Obj *obj)
 {
 	Symbol *sym;
 
@@ -199,6 +199,7 @@
 {
 	int n;
 	Objlst *lst;
+	Objsym *sym;
 	Objsect *secp;
 
 	if ((lst = malloc(sizeof(*lst))) == NULL) {
@@ -221,7 +222,8 @@
 	else
 		objlast = objlast->next = lst;
 
-	forsym(obj, newsym, obj);
+	for (sym = obj->symbols; sym; sym = sym->next)
+		newsym(sym, obj);
 
 	return;
 
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -114,9 +114,8 @@
 }
 
 static int
-newsym(Objsym *sym, void *data)
+newsym(Objsym *sym, struct symtbl *tbl)
 {
-	struct symtbl *tbl = data;
 	Objsym **p;
 	size_t n, size;
 	int type = sym->type;
@@ -148,6 +147,7 @@
 {
 	int err = 1;
 	Obj *obj;
+	Objsym *sym;
 	struct symtbl tbl = {NULL, 0};
 
 	if ((obj = objnew(type)) == NULL) {
@@ -158,8 +158,8 @@
 	if (objread(obj, fp) < 0)
 		goto error;
 
-	if (!forsym(obj, newsym, &tbl))
-		goto error;
+	for (sym = obj->symbols; sym; sym = sym->next)
+		newsym(sym, &tbl);
 
 	printsyms(tbl.buf, tbl.nsyms);
 	err = 0;
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -83,7 +83,7 @@
 }
 
 static int
-newsymbol(Objsym *sym, void *data)
+newsymbol(Objsym *sym)
 {
 	Objsymdef *np;
 
@@ -119,6 +119,7 @@
 
 	int t, ret = 0;
 	Obj *obj;
+	Objsym *sym;
 
 	if (artype == -1 && (!strcmp(nam, "/") || !strcmp(nam, "__.SYMDEF")))
 		return 1;
@@ -148,9 +149,9 @@
 		goto error;
 	}
 
-	if (!forsym(obj, newsymbol, NULL)) {
-		error("traversing object file");
-		goto error;
+	for (sym = obj->symbols; sym; sym = sym->next) {
+		if (!newsymbol(sym))
+			goto error;
 	}
 
 	ret = 1;
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -18,7 +18,6 @@
        getindex.o \
        setindex.o \
        namindex.o \
-       forsym.o \
        formember.o \
        objtype.o \
        objwrite.o \
--- a/src/libmach/forsym.c
+++ /dev/null
@@ -1,19 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-int
-forsym(Obj *obj, int (*fn)(Objsym *, void *), void *data)
-{
-	int r;
-	Objsym *sym;
-
-	for (sym = obj->symbols; sym; sym = sym->next) {
-		r = (*fn)(sym, data);
-		if (r <= 0)
-			return r;
-	}
-	return 1;
-}
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -43,7 +43,7 @@
 extern int pack(int order, unsigned char *dst, char *fmt, ...);
 extern int unpack(int order, unsigned char *src, char *fmt, ...);
 extern int objpos(Obj *obj, FILE *fp, long pos);
-extern void objfree(Obj *obj, int what);
+extern int objfree(Obj *obj, int what);
 
 /* coff32 functions */
 extern long coff32index(int type, long nsyms, Objsymdef *head, FILE *fp);
--- a/src/libmach/objfree.c
+++ b/src/libmach/objfree.c
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include <scc/mach.h>
 
@@ -7,7 +8,7 @@
 
 extern delfun_t delv[];
 
-void
+int
 objfree(Obj *obj, int what)
 {
 	int fmt;
@@ -26,4 +27,6 @@
 		obj->symbols = NULL;
 		memset(obj->htab, 0, sizeof(obj->htab));
 	}
+
+	return 0;
 }