scc

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

commit 2c2ed02aeb49b2c87ee4271047110ee682bccc47
parent 6e437c91859e1364a61897e331d064d7697a0eec
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Mon, 11 Feb 2019 21:58:22 +0000

[libmach] Add objsyms()

Diffstat:
Minclude/scc/scc/mach.h | 6++++--
Msrc/cmd/ld.c | 12++----------
Msrc/cmd/nm.c | 2+-
Msrc/cmd/ranlib.c | 2+-
Msrc/cmd/size.c | 22++++++++--------------
Msrc/libmach/coff32/coff32getsect.c | 3++-
Msrc/libmach/libmach.h | 4++--
Msrc/libmach/objfree.c | 5+++--
Msrc/libmach/objlookup.c | 4++--
Msrc/libmach/objnew.c | 5++++-
Msrc/libmach/objsect.c | 4++--
Asrc/libmach/objsyms.c | 21+++++++++++++++++++++
12 files changed, 52 insertions(+), 38 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -42,9 +42,11 @@ struct objsymdef { struct object { int type; Objsym *htab[NR_SYMHASH]; - Objsym *symbols; + Objsym *syms;; + Objsect *secs; fpos_t pos; int nsecs; + int nsyms; void *data; }; @@ -60,7 +62,7 @@ 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, Objsect **sect); +extern int objsect(Obj *obj); extern int objsyms(Obj *obj); extern int archive(FILE *fp); extern long armember(FILE *fp, char *member); diff --git a/src/cmd/ld.c b/src/cmd/ld.c @@ -30,7 +30,6 @@ enum { struct objlst { Obj *obj; - int nsect; Objsect *sect; struct objlst *next; }; @@ -207,29 +206,22 @@ loadobj(Obj *obj) return; } - if ((n = objsect(obj, &secp)) < 0) + if (objsect(obj) < 0 || objsyms(obj) < 0) goto err1; - if (objsyms(obj) < 0) - goto err2; - lst->obj = obj; lst->next = NULL; - lst->nsect = n; - lst->sect = secp; if (!objlast) objlast = objhead = lst; else objlast = objlast->next = lst; - for (sym = obj->symbols; sym; sym = sym->next) + for (sym = obj->syms; sym; sym = sym->next) newsym(sym, obj); return; -err2: - free(secp); err1: free(lst); error("out of memory"); diff --git a/src/cmd/nm.c b/src/cmd/nm.c @@ -158,7 +158,7 @@ newobject(FILE *fp, int type) if (objread(obj, fp) < 0 || objsyms(obj)) goto error; - for (sym = obj->symbols; sym; sym = sym->next) + for (sym = obj->syms; sym; sym = sym->next) newsym(sym, &tbl); printsyms(tbl.buf, tbl.nsyms); diff --git a/src/cmd/ranlib.c b/src/cmd/ranlib.c @@ -149,7 +149,7 @@ newmember(FILE *fp, char *nam, void *data) goto error; } - for (sym = obj->symbols; sym; sym = sym->next) { + for (sym = obj->syms; sym; sym = sym->next) { if (!newsymbol(sym)) goto error; } diff --git a/src/cmd/size.c b/src/cmd/size.c @@ -39,7 +39,7 @@ error(char *fmt, ...) static void newobject(FILE *fp, int type) { - int n, i;; + int i; Obj *obj; unsigned long long total, *p; Objsect *secp; @@ -50,19 +50,15 @@ newobject(FILE *fp, int type) return; } - if (objread(obj, fp) < 0) { + if (objread(obj, fp) < 0 || objsect(obj) < 0) { error("file corrupted"); - goto err1; + goto err; } siz.text = siz.data = siz.bss = 0; - if ((n = objsect(obj, &secp)) < 0) { - error("out of memory"); - goto err1; - } - - for (i = 0; i < n; i++) { - switch (secp[i].type) { + for (i = 0; i < obj->nsyms; i++) { + secp = &obj->secs[i]; + switch (secp->type) { case 'T': p = &siz.text; break; @@ -78,7 +74,7 @@ newobject(FILE *fp, int type) if (*p > ULLONG_MAX - secp->size) { error("integer overflow"); - goto err2; + goto err; } *p += secp->size; @@ -96,9 +92,7 @@ newobject(FILE *fp, int type) tbss += siz.bss; ttotal += total; -err2: - free(secp); -err1: +err: objdel(obj); } diff --git a/src/libmach/coff32/coff32getsect.c b/src/libmach/coff32/coff32getsect.c @@ -7,7 +7,7 @@ #include "coff32.h" int -coff32getsect(Obj *obj, Objsect **sectp) +coff32getsect(Obj *obj) { int nsecs; unsigned sflags, type; @@ -68,6 +68,7 @@ coff32getsect(Obj *obj, Objsect **sectp) sp->type = type; nsecs++; } + obj->secs = secs; return 1; } diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h @@ -36,7 +36,7 @@ typedef int (*probefun_t)(unsigned char *buf, char **name); typedef int (*writefun_t)(Obj *obj, FILE *fp); typedef long (*setidxfun_t)(int, long, Objsymdef *, FILE *); typedef int (*getidxfun_t)(int t, long *n, Objsymdef **def, FILE *fp); -typedef int (*getsectfun_t)(Obj *obj, Objsect **secp); +typedef int (*getsectfun_t)(Obj *obj); typedef char *(*namidxfun_t)(void); typedef int (*getsymsfun_t)(Obj *obj); @@ -61,7 +61,7 @@ extern long coff32setidx(int order, long nsyms, Objsymdef *head, FILE *fp); extern int coff32getindex(int type, long *nsyms, Objsymdef **def, FILE *fp); extern int coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp); -extern int coff32getsect(Obj *obj, Objsect **secp); +extern int coff32getsect(Obj *obj); extern char *coff32namidx(void); extern int coff32getsyms(Obj *obj); diff --git a/src/libmach/objfree.c b/src/libmach/objfree.c @@ -23,8 +23,9 @@ objfree(Obj *obj, int what) } if (what & GENERICDEL) { - free(obj->symbols); - obj->symbols = NULL; + free(obj->secs); + free(obj->syms); + obj->syms = NULL; memset(obj->htab, 0, sizeof(obj->htab)); } diff --git a/src/libmach/objlookup.c b/src/libmach/objlookup.c @@ -28,8 +28,8 @@ objlookup(Obj *obj, char *name, int install) sym->value = 0; sym->hash = obj->htab[h]; obj->htab[h] = sym; - sym->next = obj->symbols; - obj->symbols = sym; + sym->next = obj->syms; + obj->syms = sym; return sym; } diff --git a/src/libmach/objnew.c b/src/libmach/objnew.c @@ -23,7 +23,10 @@ objnew(int type) return NULL; obj->type = type; - obj->symbols = NULL; + obj->syms = NULL; + obj->secs = NULL; + obj->nsyms = 0; + obj->nsecs = 0; memset(obj->htab, 0, sizeof(obj->htab)); fn = newv[fmt]; diff --git a/src/libmach/objsect.c b/src/libmach/objsect.c @@ -7,7 +7,7 @@ extern getsectfun_t getsectv[]; int -objsect(Obj *obj, Objsect **secp) +objsect(Obj *obj) { int fmt; getsectfun_t fn; @@ -17,5 +17,5 @@ objsect(Obj *obj, Objsect **secp) return -1; fn = getsectv[fmt]; - return (*fn)(obj, secp); + return (*fn)(obj); } diff --git a/src/libmach/objsyms.c b/src/libmach/objsyms.c @@ -0,0 +1,21 @@ +#include <stdio.h> + +#include <scc/mach.h> + +#include "libmach.h" + +extern getsymsfun_t getsymsv[]; + +int +objsyms(Obj *obj) +{ + int fmt; + getsymsfun_t fn; + + fmt = FORMAT(obj->type); + if (fmt >= NFORMATS) + return -1; + + fn = getsymsv[fmt]; + return (*fn)(obj); +}