ref: d8b1cead3719c4c4254159f993fbc8222a2d2f09
parent: 18853825ec9395caec2bfd42e7fd95f4cd723402
author: Roberto E. Vargas Caballero <[email protected]>
date: Fri Jan 4 13:31:18 EST 2019
[libmach] Don't expose all the functions of object formats
--- a/src/libmach/coff32.c
+++ b/src/libmach/coff32.c
@@ -94,8 +94,8 @@
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 @@
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 @@
return c;
}
-int
-coff32read(Obj *obj, Symbol *sym)
+static int
+read(Obj *obj, Symbol *sym)
{
int t;
char *s;
@@ -275,8 +275,8 @@
return 1;
}
-void
-coff32close(Obj *obj)
+static void
+close(Obj *obj)
{
struct coff32 *coff = obj->data;
@@ -285,3 +285,10 @@
free(coff->strtbl);
free(obj->data);
}
+
+struct format objcoff32 = {
+ .probe = probe,
+ .open = open,
+ .read = read,
+ .close = close,
+};
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -26,11 +26,15 @@
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;
--- a/src/libmach/object.c
+++ b/src/libmach/object.c
@@ -9,23 +9,17 @@
#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] = &objcoff32,
+ [NFORMATS] = NULL,
};
-static struct format fmts[] = {
- [COFF32] = {coff32probe, coff32open, coff32read, coff32close},
- [NFORMATS] = {NULL},
-};
-
int
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 @@
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 @@
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 @@
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 @@
{
struct format *op;
- op = &fmts[FORMAT(obj->type)];
+ op = fmts[FORMAT(obj->type)];
(*op->close)(obj);
}