scc

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

commit 4eb5378e2f513589a9c0664544f2b87b3208aedc
parent f20800ef9f4be10663fa2f34763471c4359baa0b
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri,  4 Jan 2019 18:31:18 +0000

[libmach] Don't expose all the functions of object formats

Diffstat:
Msrc/libmach/coff32.c | 23+++++++++++++++--------
Msrc/libmach/libmach.h | 12++++++++----
Msrc/libmach/object.c | 28+++++++++++-----------------
3 files changed, 34 insertions(+), 29 deletions(-)

diff --git a/src/libmach/coff32.c b/src/libmach/coff32.c @@ -94,8 +94,8 @@ unpack_ent(int order, unsigned char *buf, SYMENT *ent) unpack(order, "ll", buf, &ent->n_zeroes, &ent->n_offset); } -int -coff32probe(unsigned char *buf, char **name) +static int +probe(unsigned char *buf, char **name) { struct arch *ap; @@ -109,8 +109,8 @@ coff32probe(unsigned char *buf, char **name) return -1; } -int -coff32open(FILE *fp, int type, Obj *obj) +static int +open(FILE *fp, int type, Obj *obj) { int order; long i, siz; @@ -249,8 +249,8 @@ typeof(Coff32 *coff, SYMENT *ent) return c; } -int -coff32read(Obj *obj, Symbol *sym) +static int +read(Obj *obj, Symbol *sym) { int t; char *s; @@ -275,8 +275,8 @@ coff32read(Obj *obj, Symbol *sym) return 1; } -void -coff32close(Obj *obj) +static void +close(Obj *obj) { struct coff32 *coff = obj->data; @@ -285,3 +285,10 @@ coff32close(Obj *obj) free(coff->strtbl); free(obj->data); } + +struct format objcoff32 = { + .probe = probe, + .open = open, + .read = read, + .close = close, +}; diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h @@ -26,11 +26,15 @@ enum order { BIG_ENDIAN, }; +struct format { + int (*probe)(unsigned char *buf, char **name); + int (*open)(FILE *fp, int type, Obj *obj); + int (*read)(Obj *obj, Symbol *sym); + void (*close)(Obj *obj); +}; + extern int pack(int order, unsigned char *dst, char *fmt, ...); extern int unpack(int order, unsigned char *src, char *fmt, ...); /* coff32.c */ -int coff32probe(unsigned char *buf, char **name); -int coff32open(FILE *fp, int type, Obj *obj); -int coff32read(Obj *obj, Symbol *sym); -void coff32close(Obj *obj); +extern struct format objcoff32; diff --git a/src/libmach/object.c b/src/libmach/object.c @@ -9,16 +9,9 @@ static char sccsid[] = "@(#) ./libmach/object.c"; #include "libmach.h" -struct format { - int (*probe)(unsigned char *buf, char **name); - int (*open)(FILE *fp, int type, Obj *obj); - int (*read)(Obj *obj, Symbol *sym); - void (*close)(Obj *obj); -}; - -static struct format fmts[] = { - [COFF32] = {coff32probe, coff32open, coff32read, coff32close}, - [NFORMATS] = {NULL}, +static struct format *fmts[] = { + [COFF32] = &objcoff32, + [NFORMATS] = NULL, }; int @@ -26,6 +19,7 @@ objtest(FILE *fp, char **name) { int n, i; int (*fn)(unsigned char *, char **); + struct format **bp, *op; fpos_t pos; unsigned char buf[NBYTES]; @@ -36,11 +30,11 @@ objtest(FILE *fp, char **name) if (n != 1 || ferror(fp)) return -1; - for (i = 0; i < NFORMATS; i++) { - fn = fmts[i].probe; - if (!fn) + for (bp = fmts; bp < &fmts[NFORMATS]; ++bp) { + op = *bp; + if (!op || !op->probe) continue; - n = (*fn)(buf, name); + n = (*op->probe)(buf, name); if (n == -1) continue; return n; @@ -58,7 +52,7 @@ objopen(FILE *fp, int type, Obj *obj) obj->symtbl = NULL; obj->data = NULL; obj->nsym = obj->cursym = 0; - op = &fmts[FORMAT(type)]; + op = fmts[FORMAT(type)]; if ((*op->open)(fp, type, obj) < 0) return -1; return 0; @@ -98,7 +92,7 @@ objread(FILE *fp, Obj *obj, int (*filter)(Symbol *)) Symbol sym, *p; struct format *op; - op = &fmts[FORMAT(obj->type)]; + op = fmts[FORMAT(obj->type)]; while ((r = (*op->read)(obj, &sym)) > 0) { if (filter && (*filter)(&sym)) continue; @@ -113,6 +107,6 @@ objclose(Obj *obj) { struct format *op; - op = &fmts[FORMAT(obj->type)]; + op = fmts[FORMAT(obj->type)]; (*op->close)(obj); }