scc

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

commit 0cbf86c7857fb758292db3a42d0be08eaf327aa8
parent 8e5f2840faf77a508145d99b8c77dc4f691a59ba
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 23 Aug 2019 22:06:53 +0100

[libmach] Remove Objsymdef from getidx()

This is the second step to remove Objsymdef.

Diffstat:
Minclude/scc/scc/mach.h | 2+-
Msrc/cmd/ld/pass1.c | 17++++++++++-------
Msrc/libmach/coff32/coff32.h | 7++++---
Msrc/libmach/coff32/coff32getidx.c | 4++--
Msrc/libmach/coff32/coff32xgetidx.c | 46++++++++++++++++++++++++++++------------------
5 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -60,7 +60,7 @@ struct objops { int (*strip)(Obj *obj); int (*addr2line)(Obj *, unsigned long long , char *, int *); int (*setidx)(long nsyms, char *names[], long offset[], FILE *fp); - int (*getidx)(long *nsyms, Objsymdef **def, FILE *fp); + int (*getidx)(long *nsyms, char ***names, long **offset, FILE *fp); }; struct obj { diff --git a/src/cmd/ld/pass1.c b/src/cmd/ld/pass1.c @@ -206,22 +206,22 @@ static void addlib(FILE *fp) { int t, added; - long n; - Objsymdef *def, *dp; + long n, i, *offs; + char **names; Symbol *sym; - if ((*binops->getidx)(&n, &def, fp) < 0) { + if ((*binops->getidx)(&n, &names, &offs, fp) < 0) { error("corrupted index"); return; } for (added = 0; moreundef(); added = 0) { - for (dp = def; dp; dp = dp->next) { - sym = lookup(dp->name); + for (i = 0; i < n; i++) { + sym = lookup(names[i]); if (!sym || sym->def) continue; - if (fseek(fp, dp->offset, SEEK_SET) == EOF) { + if (fseek(fp, offs[i], SEEK_SET) == EOF) { error(errstr()); goto clean; } @@ -244,7 +244,10 @@ addlib(FILE *fp) break; } clean: - free(def); + for (i = 0; i < n; i++) + free(names[i]); + free(names); + free(offs); } static int diff --git a/src/libmach/coff32/coff32.h b/src/libmach/coff32/coff32.h @@ -26,8 +26,8 @@ struct coff32 { extern int coff32new(Obj *obj); extern int coff32read(Obj *obj, FILE *fp); -extern int coff32setidx(long nsymbols, char *names[], long offs[], FILE *fp); -extern int coff32getidx(long *nsyms, Objsymdef **def, FILE *fp); +extern int coff32setidx(long nsyms, char **names, long *offs, FILE *fp); +extern int coff32getidx(long *nsyms, char ***namep, long **offsp, FILE *fp); extern int coff32addr2line(Obj *, unsigned long long , char *, int *); extern int coff32strip(Obj *obj); extern void coff32del(Obj *obj); @@ -36,4 +36,5 @@ extern int coff32probe(unsigned char *buf, char **name); extern int coff32xsetidx(int order, long nsymbols, char *names[], long offs[], FILE *fp); -extern int coff32xgetidx(int order, long *nsyms, Objsymdef **def, FILE *fp); +extern int coff32xgetidx(int order, + long *nsyms, char ***namep, long **offsp, FILE *fp); diff --git a/src/libmach/coff32/coff32getidx.c b/src/libmach/coff32/coff32getidx.c @@ -6,7 +6,7 @@ #include "coff32.h" int -coff32getidx(long *nsyms, Objsymdef **def, FILE *fp) +coff32getidx(long *nsyms, char ***namep, long **offsp, FILE *fp) { - return coff32xgetidx(BIG_ENDIAN, nsyms, def, fp); + return coff32xgetidx(BIG_ENDIAN, nsyms, namep, offsp, fp); } diff --git a/src/libmach/coff32/coff32xgetidx.c b/src/libmach/coff32/coff32xgetidx.c @@ -9,13 +9,12 @@ #include "coff32.h" int -coff32xgetidx(int order, long *nsyms, Objsymdef **def, FILE *fp) +coff32xgetidx(int order, long *nsyms, char ***namep, long **offsp, FILE *fp) { - int j, c; long i, n; - char *s; - Objsymdef *bp; - unsigned char buf[EXTIDENTSIZ]; + long *offs; + char **names; + unsigned char buf[EXTIDENTSIZ+1]; if (fread(buf, 4, 1, fp) != 1) return -1; @@ -24,39 +23,50 @@ coff32xgetidx(int order, long *nsyms, Objsymdef **def, FILE *fp) if (n <= 0) return -1; - if ((bp = calloc(sizeof(*bp), n)) == NULL) + if ((names = calloc(sizeof(char *), n)) == NULL) return -1; - for (i = 1; i < n-1; i++) - bp[i].next = &bp[i-1]; + if ((offs = calloc(sizeof(long), n)) == NULL) + goto err1; for (i = 0; i < n; i++) { fread(buf, 4, 1, fp); - unpack(order, buf, "l", &bp[i].offset); + unpack(order, buf, "l", offs[i]); } for (i = 0; i < n; i++) { - for (j = 0; (c = getc(fp)) != EOF && c != '\0'; j++) + int j, c; + char *s; + + for (j = 0; j < EXTIDENTSIZ; j++) { + if ((c = getc(fp)) == EOF || c == '\0') + break; buf[j] = c; - buf[j++] = '\0'; + } + if (c != '\0') + goto err2; + buf[j] = '\0'; if ((s = malloc(j)) == NULL) - goto error; + goto err2; memcpy(s, buf, j); - bp[i].name = s; + names[i]= s; } if (ferror(fp)) - goto error; + goto err2; + *offsp = offs; + *namep = names; *nsyms = n; - *def = bp; return 0; -error: +err2: + free(offs); +err1: for (i = 0; i < n; i++) - free(bp[i].name); - free(bp); + free(names[i]); + free(*names); return -1; }