ref: 41bef85434965386c93e3eb76e4f26e460ff5c13
parent: 28163a945e4ed77893ac692ddff3ab35432c676b
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Aug 23 12:17:52 EDT 2019
[libmach] Add del method to Objops
--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -49,6 +49,7 @@
int (*read)(Obj *obj, FILE *fp);
int (*addr2line)(Obj *, unsigned long long , char *, int *);
int (*strip)(Obj *obj);
+ void (*del)(Obj *obj);
int (*setidx)(long nsyms, Objsymdef *def, FILE *fp);
int (*getidx)(long *nsyms, Objsymdef **def, FILE *fp);
};
@@ -72,7 +73,6 @@
extern int objtype(FILE *fp, char **name);
extern Obj *objnew(int type);
-extern void objdel(Obj *obj);
extern Objsym *objlookup(Obj *obj, char *name, int install);
extern int objwrite(Obj *obj, FILE *fp);
extern int objpos(Obj *obj, FILE *fp, long pos);
--- a/src/cmd/ld/pass1.c
+++ b/src/cmd/ld/pass1.c
@@ -198,7 +198,7 @@
return;
delete:
- objdel(obj);
+ (*binops->del)(obj);
return;
}
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -163,8 +163,8 @@
err = 0;
error:
+ (*obj->ops->del)(obj);
free(tbl.buf);
- objdel(obj);
if (err)
error("object file corrupted");
}
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -160,7 +160,7 @@
ret = 1;
error:
- objdel(obj);
+ (ops->del)(obj);
return ret;
}
--- a/src/cmd/size.c
+++ b/src/cmd/size.c
@@ -92,7 +92,7 @@
ttotal += total;
err:
- objdel(obj);
+ (*obj->ops->del)(obj);
}
static int
--- a/src/cmd/strip.c
+++ b/src/cmd/strip.c
@@ -71,12 +71,12 @@
}
fclose(fp);
- objdel(obj);
+ (*ops->del)(obj);
return;
err3:
- objdel(obj);
+ (*ops->del)(obj);
err2:
if (fp)
fclose(fp);
--- a/src/libmach/coff32/coff32.c
+++ b/src/libmach/coff32/coff32.c
@@ -12,4 +12,5 @@
.setidx = coff32setidx,
.addr2line = coff32addr2line,
.strip = coff32strip,
+ .del = coff32del,
};
--- a/src/libmach/coff32/coff32.h
+++ b/src/libmach/coff32/coff32.h
@@ -30,6 +30,7 @@
extern int coff32getidx(long *nsyms, Objsymdef **def, FILE *fp);
extern int coff32addr2line(Obj *, unsigned long long , char *, int *);
extern int coff32strip(Obj *obj);
+extern void coff32del(Obj *obj);
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/coff32del.c
+++ b/src/libmach/coff32/coff32del.c
@@ -18,4 +18,6 @@
}
free(obj->data);
obj->data = NULL;
+
+ objdel(obj);
}
--- a/src/libmach/coff32/coff32read.c
+++ b/src/libmach/coff32/coff32read.c
@@ -344,28 +344,24 @@
/* TODO: Add validation of the different fields */
if ((off = ftell(fp)) == EOF)
- goto error;
+ return -1;
obj->pos = off;
if (!readhdr(obj, fp))
- goto error;
+ return -1;
if (!readaout(obj, fp))
- goto error;
+ return -1;
if (!readscns(obj, fp))
- goto error;
+ return -1;
if (!readents(obj, fp))
- goto error;
+ return -1;
if (!readstr(obj, fp))
- goto error;
+ return -1;
if (!readreloc(obj, fp))
- goto error;
+ return -1;
if (!readlines(obj, fp))
- goto error;
+ return -1;
return 0;
-
-error:
- objfree(obj, TARGETDEL);
- return -1;
}
static int
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -23,19 +23,13 @@
BIG_ENDIAN,
};
-enum deltype {
- GENERICDEL = 1 << 0,
- TARGETDEL = 1 << 1,
-};
-
/* common functions */
extern int pack(int order, unsigned char *dst, char *fmt, ...);
extern int unpack(int order, unsigned char *src, char *fmt, ...);
-extern int objfree(Obj *obj, int what);
+extern void objfree(Obj *obj);
/* coff32 functions */
/* TODO: Move this functions to a coff32 files */
-extern void coff32del(Obj *obj);
extern int coff32write(Obj *obj, FILE *fp);
extern int coff32probe(unsigned char *buf, char **name);
--- a/src/libmach/objdel.c
+++ /dev/null
@@ -1,13 +1,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-void
-objdel(Obj *obj)
-{
- objfree(obj, TARGETDEL | GENERICDEL);
- free(obj);
-}
--- a/src/libmach/objfree.c
+++ b/src/libmach/objfree.c
@@ -6,28 +6,10 @@
#include "libmach.h"
-static void (*funv[])(Obj *) = {
- [COFF32] = coff32del,
-};
-
-int
-objfree(Obj *obj, int what)
+void
+objdel(Obj *obj)
{
- int fmt;
-
- if (what & TARGETDEL) {
- fmt = FORMAT(obj->type);
- if (fmt < NFORMATS)
- return -1;
- (*funv[fmt])(obj);
- }
-
- if (what & GENERICDEL) {
- free(obj->secs);
- free(obj->syms);
- obj->syms = NULL;
- memset(obj->htab, 0, sizeof(obj->htab));
- }
-
- return 0;
+ free(obj->secs);
+ free(obj->syms);
+ free(obj);
}
--- a/src/libmach/objwrite.c
+++ /dev/null
@@ -1,23 +1,0 @@
-#include <stdio.h>
-
-#include <scc/mach.h>
-
-#include "libmach.h"
-
-static int (*funv[])(Obj *, FILE *) = {
- [COFF32] = coff32write,
-};
-
-int
-objwrite(Obj *obj, FILE *fp)
-{
- int fmt;
-
- fmt = FORMAT(obj->type);
- if (fmt >= NFORMATS)
- return -1;
-
- if ((*funv[fmt])(obj, fp) < 0)
- return -1;
- return 0;
-}