scc

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

commit de4cba8d93311300fadf3cbe165b44090520d4d7
parent 14e979f0753a3a3ccd51fc81a6c3aa238e093f2f
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 22 Aug 2019 07:53:00 +0100

[libmach] Simplify objread() interface

There were two additional funcitons, objsyms() and
objsect() because objread() only read the target
file in a target structure. This mean that after every
objread() we had to a objsyms() and in some cases
a objsect(). This patch simplifies the interface and
it makes objread() to generate the symbols and sections
machine independent form always.

Diffstat:
Minclude/scc/scc/mach.h | 2--
Msrc/cmd/ld/pass1.c | 5-----
Msrc/cmd/nm.c | 2+-
Msrc/cmd/ranlib.c | 2+-
Msrc/cmd/size.c | 2+-
Msrc/libmach/Makefile | 2--
Msrc/libmach/objread.c | 58++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Dsrc/libmach/objsect.c | 21---------------------
Dsrc/libmach/objsyms.c | 21---------------------
9 files changed, 57 insertions(+), 58 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -68,8 +68,6 @@ extern int objread(Obj *obj, FILE *fp); extern Objsym *objlookup(Obj *obj, char *name, int install); extern int objstrip(Obj *obj); extern int objwrite(Obj *obj, FILE *fp); -extern int objsect(Obj *obj); -extern int objsyms(Obj *obj); extern int objpos(Obj *obj, FILE *fp, long pos); extern int archive(FILE *fp); extern long armember(FILE *fp, char *member); diff --git a/src/cmd/ld/pass1.c b/src/cmd/ld/pass1.c @@ -188,11 +188,6 @@ newobject(FILE *fp, int type, int inlib) error("object file corrupted"); goto delete; } - - if (objsyms(obj) < 0 || objsect(obj) < 0) { - error("object file corrupted"); - goto delete; - } if (inlib && !is_needed(obj)) goto delete; diff --git a/src/cmd/nm.c b/src/cmd/nm.c @@ -153,7 +153,7 @@ newobject(FILE *fp, int type) return; } - if (objread(obj, fp) < 0 || objsyms(obj) < 0) + if (objread(obj, fp) < 0) goto error; for (sym = obj->syms; sym; sym = sym->next) diff --git a/src/cmd/ranlib.c b/src/cmd/ranlib.c @@ -146,7 +146,7 @@ newmember(FILE *fp, char *nam, void *data) } namidx = obj->index; - if (objread(obj, fp) < 0 || objsyms(obj) < 0) { + if (objread(obj, fp) < 0) { error("file corrupted"); goto error; } diff --git a/src/cmd/size.c b/src/cmd/size.c @@ -50,7 +50,7 @@ newobject(FILE *fp, int type) return; } - if (objread(obj, fp) < 0 || objsect(obj) < 0) { + if (objread(obj, fp) < 0) { error("file corrupted"); goto err; } diff --git a/src/libmach/Makefile b/src/libmach/Makefile @@ -9,8 +9,6 @@ OBJS = objnew.o \ objread.o \ objfree.o \ objstrip.o \ - objsect.o \ - objsyms.o \ objdel.o \ objaddseg.o \ objsync.o \ diff --git a/src/libmach/objread.c b/src/libmach/objread.c @@ -4,12 +4,44 @@ #include "libmach.h" -static int (*funv[])(Obj *, FILE *) = { +static int (*freadv[])(Obj *, FILE *) = { [COFF32] = coff32read, }; -int -objread(Obj *obj, FILE *fp) +static int (*fsymsv[])(Obj *) = { + [COFF32] = coff32getsyms, +}; + +static int (*fsectsv[])(Obj *) = { + [COFF32] = coff32getsect, +}; + +static int +getsects(Obj *obj) +{ + int fmt; + + fmt = FORMAT(obj->type); + if (fmt >= NFORMATS) + return -1; + + return (*fsectsv[fmt])(obj); +} + +static int +getsyms(Obj *obj) +{ + int fmt; + + fmt = FORMAT(obj->type); + if (fmt >= NFORMATS) + return -1; + + return (*fsymsv[fmt])(obj); +} + +static int +readfile(Obj *obj, FILE *fp) { int fmt; @@ -17,9 +49,27 @@ objread(Obj *obj, FILE *fp) if (fmt >= NFORMATS) return -1; - if ((*funv[fmt])(obj, fp) < 0) + if ((*freadv[fmt])(obj, fp) < 0) return -1; obj->fp = fp; return 0; } + +int +objread(Obj *obj, FILE *fp) +{ + objfree(obj, TARGETDEL | GENERICDEL); + + if (readfile(obj, fp) < 0) + goto err; + if (getsyms(obj) < 0) + goto err; + if (getsects(obj) < 0) + goto err; + return 0; + +err: + objfree(obj, TARGETDEL | GENERICDEL); + return -1; +} diff --git a/src/libmach/objsect.c b/src/libmach/objsect.c @@ -1,21 +0,0 @@ -#include <stdio.h> - -#include <scc/mach.h> - -#include "libmach.h" - -static int (*funv[])(Obj *) = { - [COFF32] = coff32getsect, -}; - -int -objsect(Obj *obj) -{ - int fmt; - - fmt = FORMAT(obj->type); - if (fmt >= NFORMATS) - return -1; - - return (*funv[fmt])(obj); -} diff --git a/src/libmach/objsyms.c b/src/libmach/objsyms.c @@ -1,21 +0,0 @@ -#include <stdio.h> - -#include <scc/mach.h> - -#include "libmach.h" - -static int (*funv[])(Obj *) = { - [COFF32] = coff32getsyms, -}; - -int -objsyms(Obj *obj) -{ - int fmt; - - fmt = FORMAT(obj->type); - if (fmt >= NFORMATS) - return -1; - - return (*funv[fmt])(obj); -}