scc

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

commit 4830eb16ae153c0ef348a2d69e21f89b3191b238
parent 818f368daded1b80748ab4fbbb1df718e65984ca
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 23 Aug 2019 07:51:54 +0100

[libmach] Add setidx and getidx methods to Objops

Diffstat:
Minclude/scc/scc/mach.h | 15+++++++++------
Msrc/cmd/addr2line.c | 2+-
Msrc/cmd/ld/pass1.c | 6++++--
Msrc/cmd/nm.c | 2+-
Msrc/cmd/ranlib.c | 11+++++------
Msrc/cmd/size.c | 2+-
Msrc/cmd/strip.c | 2+-
Msrc/libmach/Makefile | 2--
Msrc/libmach/coff32/Makefile | 6++++--
Msrc/libmach/coff32/coff32.c | 4+++-
Msrc/libmach/coff32/coff32.h | 5+++++
Msrc/libmach/coff32/coff32getidx.c | 54++----------------------------------------------------
Dsrc/libmach/coff32/coff32getindex.c | 12------------
Msrc/libmach/coff32/coff32setidx.c | 29++++-------------------------
Dsrc/libmach/coff32/coff32setindex.c | 12------------
Asrc/libmach/coff32/coff32xgetidx.c | 62++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Asrc/libmach/coff32/coff32xsetidx.c | 33+++++++++++++++++++++++++++++++++
Msrc/libmach/coff32/deps.mk | 17++++++++++-------
Msrc/libmach/deps.mk | 8++------
Dsrc/libmach/getindex.c | 21---------------------
Msrc/libmach/libmach.h | 15++-------------
Msrc/libmach/mach.c | 4+---
Msrc/libmach/objnew.c | 9++++-----
Dsrc/libmach/setindex.c | 21---------------------
24 files changed, 154 insertions(+), 200 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -3,6 +3,7 @@ typedef struct objsect Objsect; typedef struct objsym Objsym; typedef struct objsymdef Objsymdef; +typedef struct objops Objops; typedef struct obj Obj; enum sectype { @@ -43,8 +44,16 @@ struct objsymdef { Objsymdef *hash, *next; }; +struct objops { + int (*new)(Obj *obj); + int (*read)(Obj *obj, FILE *fp); + int (*setidx)(long nsyms, Objsymdef *def, FILE *fp); + int (*getidx)(long *nsyms, Objsymdef **def, FILE *fp); +}; + struct obj { int type; + Objops *ops; char *index; Objsym *htab[NR_SYMHASH]; Objsym *syms; @@ -53,12 +62,8 @@ struct obj { int nsecs; int nsyms; void *data; - - int (*new)(Obj *obj); - int (*read)(Obj *obj, FILE *fp); }; - extern int formember(FILE *fp, int (*fn)(FILE *, char *, void *), void *data); @@ -72,8 +77,6 @@ extern int objwrite(Obj *obj, FILE *fp); extern int objpos(Obj *obj, FILE *fp, long pos); extern int archive(FILE *fp); extern long armember(FILE *fp, char *member); -extern long setindex(int type, long nsyms, Objsymdef *def, FILE *fp); -extern int getindex(int type, long *nsyms, Objsymdef **def, FILE *fp); /* TODO */ diff --git a/src/cmd/addr2line.c b/src/cmd/addr2line.c @@ -93,7 +93,7 @@ loadexe(char *fname) return NULL; } - if ((*obj->read)(obj, fp) < 0) { + if ((*obj->ops->read)(obj, fp) < 0) { error("file corrupted"); return NULL; } diff --git a/src/cmd/ld/pass1.c b/src/cmd/ld/pass1.c @@ -16,6 +16,7 @@ enum { }; int bintype = -1; +static Objops *binops; static Symbol refhead = { .next = &refhead, .prev = &refhead, @@ -183,8 +184,9 @@ newobject(FILE *fp, int type, int inlib) goto delete; } bintype = type; + binops = obj->ops; - if ((*obj->read)(obj, fp) < 0) { + if ((*binops->read)(obj, fp) < 0) { error("object file corrupted"); goto delete; } @@ -208,7 +210,7 @@ addlib(FILE *fp) Objsymdef *def, *dp; Symbol *sym; - if (getindex(bintype, &n, &def, fp) < 0) { + if ((*binops->getidx)(&n, &def, fp) < 0) { error("corrupted index"); return; } diff --git a/src/cmd/nm.c b/src/cmd/nm.c @@ -153,7 +153,7 @@ newobject(FILE *fp, int type) return; } - if ((*obj->read)(obj, fp) < 0) + if ((*obj->ops->read)(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 @@ -19,6 +19,7 @@ static char *namidx; static long nsymbols; static int status, artype, nolib; +static Objops *ops; static char *filename, *membname; static Objsymdef *htab[NR_SYMDEF], *head; static long offset; @@ -117,7 +118,6 @@ newsymbol(Objsym *sym) static int newmember(FILE *fp, char *nam, void *data) { - int t, ret = 0; Obj *obj; Objsym *sym; @@ -144,9 +144,10 @@ newmember(FILE *fp, char *nam, void *data) error("out of memory"); return 0; } + ops = obj->ops; namidx = obj->index; - if ((*obj->read)(obj, fp) < 0) { + if ((*ops->read)(obj, fp) < 0) { error("file corrupted"); goto error; } @@ -269,7 +270,6 @@ ranlib(char *fname) { int c; FILE *fp, *idx, *out; - long siz; struct fprop prop; errno = 0; @@ -291,13 +291,12 @@ ranlib(char *fname) if (nolib) goto error; - siz = setindex(artype, nsymbols, head, idx); - if (siz <= 0) + if ((*ops->setidx)(nsymbols, head, idx) < 0) goto error; if (getstat(fname, &prop) < 0) goto error; - prop.size = siz; + prop.size = ftell(idx); prop.time = time(NULL); if (!merge(out, &prop, fp, idx)) diff --git a/src/cmd/size.c b/src/cmd/size.c @@ -50,7 +50,7 @@ newobject(FILE *fp, int type) return; } - if ((*obj->read)(obj, fp) < 0) { + if ((*obj->ops->read)(obj, fp) < 0) { error("file corrupted"); goto err; } diff --git a/src/cmd/strip.c b/src/cmd/strip.c @@ -46,7 +46,7 @@ strip(char *fname) error("out of memory"); goto err3; } - if ((*obj->read)(obj, fp) < 0) { + if ((*obj->ops->read)(obj, fp) < 0) { error("file corrupted"); goto err3; } diff --git a/src/libmach/Makefile b/src/libmach/Makefile @@ -16,8 +16,6 @@ OBJS = mach.o \ archive.o \ armember.o \ objlookup.o \ - getindex.o \ - setindex.o \ formember.o \ objtype.o \ objwrite.o \ diff --git a/src/libmach/coff32/Makefile b/src/libmach/coff32/Makefile @@ -9,8 +9,10 @@ OBJS = coff32.o \ coff32read.o \ coff32strip.o \ coff32write.o \ - coff32setindex.o \ - coff32getindex.o \ + coff32setidx.o \ + coff32xsetidx.o \ + coff32getidx.o \ + coff32xgetidx.o \ coff32setidx.o \ coff32getidx.o \ coff32sync.o \ diff --git a/src/libmach/coff32/coff32.c b/src/libmach/coff32/coff32.c @@ -5,7 +5,9 @@ #include "../libmach.h" #include "coff32.h" -struct objfmt coff32 = { +Objops coff32 = { .new = coff32new, .read = coff32read, + .getidx = coff32getidx, + .setidx = coff32setidx, }; diff --git a/src/libmach/coff32/coff32.h b/src/libmach/coff32/coff32.h @@ -26,3 +26,8 @@ struct coff32 { extern int coff32new(Obj *obj); extern int coff32read(Obj *obj, FILE *fp); +extern int coff32setidx(long nsymbols, Objsymdef *head, FILE *fp); +extern int coff32getidx(long *nsyms, Objsymdef **def, FILE *fp); + +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/coff32getidx.c b/src/libmach/coff32/coff32getidx.c @@ -1,62 +1,12 @@ #include <stdio.h> -#include <stdlib.h> -#include <string.h> #include <scc/mach.h> -#include <scc/cstd.h> #include "../libmach.h" #include "coff32.h" int -coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp) +coff32getidx(long *nsyms, Objsymdef **def, FILE *fp) { - int j, c; - long i, n; - char *s; - Objsymdef *bp; - unsigned char buf[EXTIDENTSIZ]; - - if (fread(buf, 4, 1, fp) != 1) - return -1; - unpack(order, buf, "l", &n); - - if (n <= 0) - return -1; - - if ((bp = calloc(sizeof(*bp), n)) == NULL) - return -1; - - for (i = 1; i < n-1; i++) - bp[i].next = &bp[i-1]; - - for (i = 0; i < n; i++) { - fread(buf, 4, 1, fp); - unpack(order, buf, "l", &bp[i].offset); - } - - for (i = 0; i < n; i++) { - for (j = 0; (c = getc(fp)) != EOF && c != '\0'; j++) - buf[j] = c; - buf[j++] = '\0'; - - if ((s = malloc(j)) == NULL) - goto error; - memcpy(s, buf, j); - bp[i].name = s; - } - - if (ferror(fp)) - goto error; - - *nsyms = n; - *def = bp; - - return 0; - -error: - for (i = 0; i < n; i++) - free(bp[i].name); - free(bp); - return -1; + return coff32xgetidx(BIG_ENDIAN, nsyms, def, fp); } diff --git a/src/libmach/coff32/coff32getindex.c b/src/libmach/coff32/coff32getindex.c @@ -1,12 +0,0 @@ -#include <stdio.h> - -#include <scc/mach.h> - -#include "../libmach.h" -#include "coff32.h" - -int -coff32getindex(int type, long *nsyms, Objsymdef **def, FILE *fp) -{ - return coff32getidx(BIG_ENDIAN, nsyms, def, fp); -} diff --git a/src/libmach/coff32/coff32setidx.c b/src/libmach/coff32/coff32setidx.c @@ -1,33 +1,12 @@ #include <stdio.h> -#include <string.h> #include <scc/mach.h> #include "../libmach.h" +#include "coff32.h" -long -coff32setidx(int order, long nsyms, Objsymdef *head, FILE *fp) +int +coff32setidx(long nsymbols, Objsymdef *head, FILE *fp) { - long i, n; - size_t len; - Objsymdef *def; - unsigned char buff[4]; - - pack(order, buff, "l", nsyms); - fwrite(buff, 4, 1, fp); - n = 4; - - for (def = head; def; def = def->next) { - pack(order, buff, "l", (long) def->offset); - fwrite(buff, 4, 1, fp); - n += 4; - } - - for (def = head; def; def = def->next) { - len = strlen(def->name) + 1; - fwrite(def->name, len, 1, fp); - n += len; - } - - return fflush(fp) == EOF ? -1 : n; + return coff32xsetidx(BIG_ENDIAN, nsymbols, head, fp); } diff --git a/src/libmach/coff32/coff32setindex.c b/src/libmach/coff32/coff32setindex.c @@ -1,12 +0,0 @@ -#include <stdio.h> - -#include <scc/mach.h> - -#include "../libmach.h" -#include "coff32.h" - -long -coff32setindex(int type, long nsymbols, Objsymdef *head, FILE *fp) -{ - return coff32setidx(BIG_ENDIAN, nsymbols, head, fp); -} diff --git a/src/libmach/coff32/coff32xgetidx.c b/src/libmach/coff32/coff32xgetidx.c @@ -0,0 +1,62 @@ +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <scc/mach.h> +#include <scc/cstd.h> + +#include "../libmach.h" +#include "coff32.h" + +int +coff32xgetidx(int order, long *nsyms, Objsymdef **def, FILE *fp) +{ + int j, c; + long i, n; + char *s; + Objsymdef *bp; + unsigned char buf[EXTIDENTSIZ]; + + if (fread(buf, 4, 1, fp) != 1) + return -1; + unpack(order, buf, "l", &n); + + if (n <= 0) + return -1; + + if ((bp = calloc(sizeof(*bp), n)) == NULL) + return -1; + + for (i = 1; i < n-1; i++) + bp[i].next = &bp[i-1]; + + for (i = 0; i < n; i++) { + fread(buf, 4, 1, fp); + unpack(order, buf, "l", &bp[i].offset); + } + + for (i = 0; i < n; i++) { + for (j = 0; (c = getc(fp)) != EOF && c != '\0'; j++) + buf[j] = c; + buf[j++] = '\0'; + + if ((s = malloc(j)) == NULL) + goto error; + memcpy(s, buf, j); + bp[i].name = s; + } + + if (ferror(fp)) + goto error; + + *nsyms = n; + *def = bp; + + return 0; + +error: + for (i = 0; i < n; i++) + free(bp[i].name); + free(bp); + return -1; +} diff --git a/src/libmach/coff32/coff32xsetidx.c b/src/libmach/coff32/coff32xsetidx.c @@ -0,0 +1,33 @@ +#include <stdio.h> +#include <string.h> + +#include <scc/mach.h> + +#include "../libmach.h" + +int +coff32xsetidx(int order, long nsyms, Objsymdef *head, FILE *fp) +{ + long i, n; + size_t len; + Objsymdef *def; + unsigned char buff[4]; + + pack(order, buff, "l", nsyms); + fwrite(buff, 4, 1, fp); + n = 4; + + for (def = head; def; def = def->next) { + pack(order, buff, "l", (long) def->offset); + fwrite(buff, 4, 1, fp); + n += 4; + } + + for (def = head; def; def = def->next) { + len = strlen(def->name) + 1; + fwrite(def->name, len, 1, fp); + n += len; + } + + return fflush(fp) == EOF ? -1 : 0; +} diff --git a/src/libmach/coff32/deps.mk b/src/libmach/coff32/deps.mk @@ -1,14 +1,13 @@ #deps +./coff32.o: $(INCDIR)/scc/scc/mach.h +./coff32.o: ./../libmach.h +./coff32.o: ./coff32.h ./coff32del.o: $(INCDIR)/scc/scc/mach.h ./coff32del.o: ./../libmach.h ./coff32del.o: ./coff32.h -./coff32getidx.o: $(INCDIR)/scc/scc/cstd.h ./coff32getidx.o: $(INCDIR)/scc/scc/mach.h ./coff32getidx.o: ./../libmach.h ./coff32getidx.o: ./coff32.h -./coff32getindex.o: $(INCDIR)/scc/scc/mach.h -./coff32getindex.o: ./../libmach.h -./coff32getindex.o: ./coff32.h ./coff32new.o: $(INCDIR)/scc/scc/mach.h ./coff32new.o: ./../libmach.h ./coff32new.o: ./coff32.h @@ -20,9 +19,7 @@ ./coff32read.o: ./coff32.h ./coff32setidx.o: $(INCDIR)/scc/scc/mach.h ./coff32setidx.o: ./../libmach.h -./coff32setindex.o: $(INCDIR)/scc/scc/mach.h -./coff32setindex.o: ./../libmach.h -./coff32setindex.o: ./coff32.h +./coff32setidx.o: ./coff32.h ./coff32strip.o: $(INCDIR)/scc/scc/mach.h ./coff32strip.o: ./../libmach.h ./coff32strip.o: ./coff32.h @@ -31,3 +28,9 @@ ./coff32write.o: $(INCDIR)/scc/scc/mach.h ./coff32write.o: ./../libmach.h ./coff32write.o: ./coff32.h +./coff32xgetidx.o: $(INCDIR)/scc/scc/cstd.h +./coff32xgetidx.o: $(INCDIR)/scc/scc/mach.h +./coff32xgetidx.o: ./../libmach.h +./coff32xgetidx.o: ./coff32.h +./coff32xsetidx.o: $(INCDIR)/scc/scc/mach.h +./coff32xsetidx.o: ./../libmach.h diff --git a/src/libmach/deps.mk b/src/libmach/deps.mk @@ -7,8 +7,8 @@ ./armember.o: $(INCDIR)/scc/scc/mach.h ./formember.o: $(INCDIR)/scc/scc/ar.h ./formember.o: $(INCDIR)/scc/scc/mach.h -./getindex.o: $(INCDIR)/scc/scc/mach.h -./getindex.o: ./libmach.h +./mach.o: $(INCDIR)/scc/scc/mach.h +./mach.o: ./libmach.h ./objaddseg.o: $(INCDIR)/scc/scc/mach.h ./objdel.o: $(INCDIR)/scc/scc/mach.h ./objdel.o: ./libmach.h @@ -20,8 +20,6 @@ ./objnew.o: ./libmach.h ./objpos.o: $(INCDIR)/scc/scc/mach.h ./objpos.o: ./libmach.h -./objread.o: $(INCDIR)/scc/scc/mach.h -./objread.o: ./libmach.h ./objstrip.o: $(INCDIR)/scc/scc/mach.h ./objstrip.o: ./libmach.h ./objsync.o: $(INCDIR)/scc/scc/mach.h @@ -32,7 +30,5 @@ ./objwrite.o: ./libmach.h ./pack.o: $(INCDIR)/scc/scc/mach.h ./pack.o: ./libmach.h -./setindex.o: $(INCDIR)/scc/scc/mach.h -./setindex.o: ./libmach.h ./unpack.o: $(INCDIR)/scc/scc/mach.h ./unpack.o: ./libmach.h diff --git a/src/libmach/getindex.c b/src/libmach/getindex.c @@ -1,21 +0,0 @@ -#include <stdio.h> - -#include <scc/mach.h> - -#include "libmach.h" - -static int (*funv[])(int, long*, Objsymdef**, FILE*) = { - [COFF32] = coff32getidx, -}; - -int -getindex(int type, long *nsyms, Objsymdef **head, FILE *fp) -{ - int fmt; - - fmt = FORMAT(type); - if (fmt >= NFORMATS) - return -1; - - return (*funv[fmt])(type, nsyms, head, fp); -} diff --git a/src/libmach/libmach.h b/src/libmach/libmach.h @@ -28,11 +28,6 @@ enum deltype { TARGETDEL = 1 << 1, }; -struct objfmt { - int (*new)(Obj *obj); - int (*read)(Obj *obj, FILE *fp); -}; - /* common functions */ extern int pack(int order, unsigned char *dst, char *fmt, ...); extern int unpack(int order, unsigned char *src, char *fmt, ...); @@ -40,20 +35,14 @@ extern int objfree(Obj *obj, int what); /* coff32 functions */ /* TODO: Move this functions to a coff32 files */ -extern long coff32index(int type, long nsyms, Objsymdef *head, FILE *fp); extern void coff32del(Obj *obj); extern int coff32write(Obj *obj, FILE *fp); extern void coff32strip(Obj *obj); extern int coff32probe(unsigned char *buf, char **name); -extern long coff32setindex(int type, long nsymbols, Objsymdef *head, FILE *fp); -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 char *coff32namidx(void); extern int coff32sync(Obj *obj); /* globals */ -extern struct objfmt *objfmts[]; +extern Objops *objops[]; +extern Objops coff32; diff --git a/src/libmach/mach.c b/src/libmach/mach.c @@ -4,9 +4,7 @@ #include "libmach.h" -extern struct objfmt coff32; - -struct objfmt *objfmts[] = { +Objops *objops[] = { [COFF32] = &coff32, [NFORMATS] = NULL, }; diff --git a/src/libmach/objnew.c b/src/libmach/objnew.c @@ -11,7 +11,7 @@ objnew(int type) { Obj *obj; int fmt; - struct objfmt *op; + Objops *ops; fmt = FORMAT(type); if (fmt >= NFORMATS) @@ -27,11 +27,10 @@ objnew(int type) obj->nsecs = 0; memset(obj->htab, 0, sizeof(obj->htab)); - op = objfmts[fmt]; - obj->new = op->new; - obj->read = op->read; + ops = objops[fmt]; + obj->ops = ops; - if ((*obj->new)(obj) < 0) { + if ((*ops->new)(obj) < 0) { free(obj); return NULL; } diff --git a/src/libmach/setindex.c b/src/libmach/setindex.c @@ -1,21 +0,0 @@ -#include <stdio.h> - -#include <scc/mach.h> - -#include "libmach.h" - -static long (*funv[])(int, long, Objsymdef *, FILE *) = { - [COFF32] = coff32setidx, -}; - -long -setindex(int type, long nsyms, Objsymdef *head, FILE *fp) -{ - int fmt; - - fmt = FORMAT(type); - if (fmt >= NFORMATS) - return -1; - - return (*funv[fmt])(type, nsyms, head, fp); -}