scc

simple c99 compiler
git clone git://git.simple-cc.org/scc
Log | Files | Refs | README | LICENSE

commit 57dd14763bb392cfc855c6853582541750271ce5
parent 1b1fe72607dfe27b214377e74fce3c0ccf34f3a2
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 23 Aug 2019 16:17:52 +0100

[libmach] Add del method to Objops

Diffstat:
Minclude/scc/scc/mach.h | 2+-
Msrc/cmd/ld/pass1.c | 2+-
Msrc/cmd/nm.c | 2+-
Msrc/cmd/ranlib.c | 2+-
Msrc/cmd/size.c | 2+-
Msrc/cmd/strip.c | 4++--
Msrc/libmach/coff32/coff32.c | 1+
Msrc/libmach/coff32/coff32.h | 1+
Msrc/libmach/coff32/coff32del.c | 2++
Msrc/libmach/coff32/coff32read.c | 20++++++++------------
Msrc/libmach/libmach.h | 8+-------
Dsrc/libmach/objdel.c | 13-------------
Msrc/libmach/objfree.c | 28+++++-----------------------
Dsrc/libmach/objwrite.c | 23-----------------------
14 files changed, 25 insertions(+), 85 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -49,6 +49,7 @@ struct objops { 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 formember(FILE *fp, 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); diff --git a/src/cmd/ld/pass1.c b/src/cmd/ld/pass1.c @@ -198,7 +198,7 @@ newobject(FILE *fp, int type, int inlib) return; delete: - objdel(obj); + (*binops->del)(obj); return; } diff --git a/src/cmd/nm.c b/src/cmd/nm.c @@ -163,8 +163,8 @@ newobject(FILE *fp, int type) err = 0; error: + (*obj->ops->del)(obj); free(tbl.buf); - objdel(obj); if (err) error("object file corrupted"); } diff --git a/src/cmd/ranlib.c b/src/cmd/ranlib.c @@ -160,7 +160,7 @@ newmember(FILE *fp, char *nam, void *data) ret = 1; error: - objdel(obj); + (ops->del)(obj); return ret; } diff --git a/src/cmd/size.c b/src/cmd/size.c @@ -92,7 +92,7 @@ newobject(FILE *fp, int type) ttotal += total; err: - objdel(obj); + (*obj->ops->del)(obj); } static int diff --git a/src/cmd/strip.c b/src/cmd/strip.c @@ -71,12 +71,12 @@ strip(char *fname) } fclose(fp); - objdel(obj); + (*ops->del)(obj); return; err3: - objdel(obj); + (*ops->del)(obj); err2: if (fp) fclose(fp); diff --git a/src/libmach/coff32/coff32.c b/src/libmach/coff32/coff32.c @@ -12,4 +12,5 @@ Objops coff32 = { .setidx = coff32setidx, .addr2line = coff32addr2line, .strip = coff32strip, + .del = coff32del, }; diff --git a/src/libmach/coff32/coff32.h b/src/libmach/coff32/coff32.h @@ -30,6 +30,7 @@ extern int coff32setidx(long nsymbols, Objsymdef *head, FILE *fp); 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); diff --git a/src/libmach/coff32/coff32del.c b/src/libmach/coff32/coff32del.c @@ -18,4 +18,6 @@ coff32del(Obj *obj) } free(obj->data); obj->data = NULL; + + objdel(obj); } diff --git a/src/libmach/coff32/coff32read.c b/src/libmach/coff32/coff32read.c @@ -344,28 +344,24 @@ readfile(Obj *obj, FILE *fp) /* 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 diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h @@ -23,19 +23,13 @@ enum order { 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); diff --git a/src/libmach/objdel.c b/src/libmach/objdel.c @@ -1,13 +0,0 @@ -#include <stdio.h> -#include <stdlib.h> - -#include <scc/mach.h> - -#include "libmach.h" - -void -objdel(Obj *obj) -{ - objfree(obj, TARGETDEL | GENERICDEL); - free(obj); -} diff --git 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); } diff --git a/src/libmach/objwrite.c b/src/libmach/objwrite.c @@ -1,23 +0,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; -}