shithub: scc

Download patch

ref: a8d4f208d8e94b207c1625fa0008a9f247635dd2
parent: 4691d9aba548d25e59960b02334f98991a534a4c
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Feb 11 11:56:45 EST 2019

[libmach] Remove objreset()

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -42,7 +42,7 @@
 struct object {
 	int type;
 	Objsym *htab[NR_SYMHASH];
-	Objsym *head;
+	Objsym *symbols;
 	fpos_t pos;
 	int nsecs;
 	void *data;
@@ -61,8 +61,7 @@
 extern long armember(FILE *fp, char *member);
 extern int objtype(FILE *fp, char **name);
 extern Obj *objnew(int type);
-extern int objdel(Obj *obj);
-extern int objreset(Obj *obj);
+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);
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -12,7 +12,7 @@
        objnew.o \
        objpos.o \
        objread.o \
-       objreset.o \
+       objfree.o \
        objstrip.o \
        objsect.o \
        getindex.o \
@@ -22,7 +22,6 @@
        formember.o \
        objtype.o \
        objwrite.o \
-       objfree.o \
        pack.o \
        unpack.o \
        new.o \
--- a/src/libmach/coff32/coff32read.c
+++ b/src/libmach/coff32/coff32read.c
@@ -445,6 +445,6 @@
 	return 0;
 
 error:
-	objreset(obj);
+	objfree(obj, TARGETDEL);
 	return -1;
 }
--- a/src/libmach/deps.mk
+++ b/src/libmach/deps.mk
@@ -69,8 +69,6 @@
 ./objpos.o: ./libmach.h
 ./objread.o: $(INCDIR)/scc/scc/mach.h
 ./objread.o: ./libmach.h
-./objreset.o: $(INCDIR)/scc/scc/mach.h
-./objreset.o: ./libmach.h
 ./objsect.o: $(INCDIR)/scc/scc/mach.h
 ./objsect.o: ./libmach.h
 ./objstrip.o: $(INCDIR)/scc/scc/mach.h
--- a/src/libmach/forsym.c
+++ b/src/libmach/forsym.c
@@ -10,7 +10,7 @@
 	int r;
 	Objsym *sym;
 
-	for (sym = obj->head; sym; sym = sym->next) {
+	for (sym = obj->symbols; sym; sym = sym->next) {
 		r = (*fn)(sym, data);
 		if (r <= 0)
 			return r;
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -23,8 +23,9 @@
 	BIG_ENDIAN,
 };
 
-enum freeflags {
-	FREESYM,
+enum deltype {
+	GENERICDEL = 1 << 0,
+	TARGETDEL  = 1 << 1,
 };
 
 typedef int (*newfun_t)(Obj *obj);
--- a/src/libmach/objdel.c
+++ b/src/libmach/objdel.c
@@ -5,10 +5,9 @@
 
 #include "libmach.h"
 
-int
+void
 objdel(Obj *obj)
 {
-	if (objreset(obj) < 0)
-		return -1;
+	objfree(obj, TARGETDEL | GENERICDEL);
 	free(obj);
 }
--- a/src/libmach/objfree.c
+++ b/src/libmach/objfree.c
@@ -1,29 +1,29 @@
 #include <stdio.h>
 #include <stdlib.h>
-#include <string.h>
 
 #include <scc/mach.h>
 
 #include "libmach.h"
 
-static void
-delsyms(Obj *obj)
-{
-	Objsym *sym, *next;
+extern delfun_t delv[];
 
-	for (sym = obj->head; sym; sym = next) {
-		next = sym->next;
-		free(sym->name);
-		free(sym);
-	}
-
-	obj->head = NULL;
-	memset(obj->htab, 0, sizeof(obj->htab));
-}
-
 void
 objfree(Obj *obj, int what)
 {
-	if (what & FREESYM)
-		delsyms(obj);
+	int fmt;
+	delfun_t fn;
+
+	if (what & TARGETDEL) {
+		fmt = FORMAT(obj->type);
+		if (fmt < NFORMATS)
+			return -1;
+		fn = delv[fmt];
+		(*fn)(obj);
+	}
+
+	if (what & GENERICDEL) {
+		free(obj->symbols);
+		obj->symbols = NULL;
+		memset(obj->htab, 0, sizeof(obj->htab));
+	}
 }
--- a/src/libmach/objlookup.c
+++ b/src/libmach/objlookup.c
@@ -9,8 +9,6 @@
 objlookup(Obj *obj, char *name, int install)
 {
 	unsigned h;
-	size_t len;
-	char *s;
 	Objsym *sym;
 
 	h = genhash(name) % NR_SYMHASH;
@@ -23,19 +21,15 @@
 
 	if ((sym = malloc(sizeof(*sym))) == NULL)
 		return NULL;
-	len = strlen(name) + 1;
-	if ((s = malloc(len)) == NULL) {
-		free(sym);
-		return NULL;
-	}
-	sym->name = memcpy(s, name, len);
+
+	sym->name = name;
 	sym->type = 'U';
 	sym->size = 0;
 	sym->value = 0;
 	sym->hash = obj->htab[h];
 	obj->htab[h] = sym;
-	sym->next = obj->head;
-	obj->head = sym;
+	sym->next = obj->symbols;
+	obj->symbols = sym;
 
 	return sym;
 }
--- a/src/libmach/objnew.c
+++ b/src/libmach/objnew.c
@@ -23,7 +23,7 @@
 		return NULL;
 
 	obj->type = type;
-	obj->head = NULL;
+	obj->symbols = NULL;
 	memset(obj->htab, 0, sizeof(obj->htab));
 
 	fn = newv[fmt];
--- a/src/libmach/objreset.c
+++ /dev/null
@@ -1,22 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-extern delfun_t delv[];
-
-int
-objreset(Obj *obj)
-{
-	int fmt;
-	delfun_t fn;
-
-	fmt = FORMAT(obj->type);
-	if (fmt < NFORMATS)
-		return -1;
-	fn = delv[fmt];
-	(*fn)(obj);
-	objfree(obj, FREESYM);
-	return 0;
-}
--- a/src/libmach/objstrip.c
+++ b/src/libmach/objstrip.c
@@ -17,6 +17,8 @@
 		return -1;
 	fn = stripv[fmt];
 	(*fn)(obj);
-	objfree(obj, FREESYM);
+
+	objfree(obj, GENERICDEL);
+
 	return 0;
 }