scc

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

commit 8e5f2840faf77a508145d99b8c77dc4f691a59ba
parent dc8b78a3c50b7e18ac720e59302373ac08758943
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Fri, 23 Aug 2019 19:26:02 +0100

[libmach] Stop using Objsymdef in setidx

This type is not needed and it is creating confusion. This is
the first step to remove the type.

Diffstat:
Minclude/scc/scc/mach.h | 2+-
Msrc/cmd/ranlib.c | 28++++++++++++++++++++++++----
Msrc/libmach/coff32/coff32.h | 5+++--
Msrc/libmach/coff32/coff32setidx.c | 4++--
Msrc/libmach/coff32/coff32xsetidx.c | 12++++++------
5 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/include/scc/scc/mach.h b/include/scc/scc/mach.h @@ -59,7 +59,7 @@ struct objops { int (*addseg)(Obj *obj, void *seg); int (*strip)(Obj *obj); int (*addr2line)(Obj *, unsigned long long , char *, int *); - int (*setidx)(long nsyms, Objsymdef *def, FILE *fp); + int (*setidx)(long nsyms, char *names[], long offset[], FILE *fp); int (*getidx)(long *nsyms, Objsymdef **def, FILE *fp); }; diff --git a/src/cmd/ranlib.c b/src/cmd/ranlib.c @@ -265,17 +265,22 @@ copy(FILE *from, char *fname) return ret; } + static void ranlib(char *fname) { - int c; + long *offs, i; + char **names; FILE *fp, *idx, *out; + Objsymdef *dp; struct fprop prop; errno = 0; nolib = 0; artype = -1; nsymbols = 0; + offs = NULL; + names = NULL; filename = fname; freehash(); @@ -288,10 +293,18 @@ ranlib(char *fname) if (!readsyms(fp)) goto error; - if (nolib) + if (nolib || nsymbols == 0) goto error; - if ((*ops->setidx)(nsymbols, head, idx) < 0) + offs = malloc(sizeof(long) * nsymbols); + names = malloc(sizeof(*names) * nsymbols); + + for (dp = head, i = 0; i < nsymbols; dp = dp->next, i++) { + offs[i] = dp->offset; + names[i] = dp->name; + } + + if ((*ops->setidx)(nsymbols, names, offs, idx) < 0) goto error; if (getstat(fname, &prop) < 0) @@ -302,18 +315,25 @@ ranlib(char *fname) if (!merge(out, &prop, fp, idx)) goto error; + free(offs); + free(names); fclose(fp); fclose(idx); + offs = NULL; + names = NULL; fp = idx = NULL; if (!copy(out, fname)) goto error; - fclose(out); return; error: + if (offs) + free(offs); + if (names) + free(names); if (errno) error(errstr()); if (idx) diff --git a/src/libmach/coff32/coff32.h b/src/libmach/coff32/coff32.h @@ -26,7 +26,7 @@ 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 coff32setidx(long nsymbols, char *names[], long offs[], FILE *fp); extern int coff32getidx(long *nsyms, Objsymdef **def, FILE *fp); extern int coff32addr2line(Obj *, unsigned long long , char *, int *); extern int coff32strip(Obj *obj); @@ -34,5 +34,6 @@ extern void coff32del(Obj *obj); extern int coff32write(Obj *obj, FILE *fp); extern int coff32probe(unsigned char *buf, char **name); -extern int coff32xsetidx(int order, long nsyms, Objsymdef *head, FILE *fp); +extern int coff32xsetidx(int order, + long nsymbols, char *names[], long offs[], FILE *fp); extern int coff32xgetidx(int order, long *nsyms, Objsymdef **def, FILE *fp); diff --git a/src/libmach/coff32/coff32setidx.c b/src/libmach/coff32/coff32setidx.c @@ -6,7 +6,7 @@ #include "coff32.h" int -coff32setidx(long nsymbols, Objsymdef *head, FILE *fp) +coff32setidx(long nsymbols, char *names[], long offs[], FILE *fp) { - return coff32xsetidx(BIG_ENDIAN, nsymbols, head, fp); + return coff32xsetidx(BIG_ENDIAN, nsymbols, names, offs, fp); } diff --git a/src/libmach/coff32/coff32xsetidx.c b/src/libmach/coff32/coff32xsetidx.c @@ -6,7 +6,7 @@ #include "../libmach.h" int -coff32xsetidx(int order, long nsyms, Objsymdef *head, FILE *fp) +coff32xsetidx(int order, long nsyms, char *names[], long offs[], FILE *fp) { long i, n; size_t len; @@ -17,15 +17,15 @@ coff32xsetidx(int order, long nsyms, Objsymdef *head, FILE *fp) fwrite(buff, 4, 1, fp); n = 4; - for (def = head; def; def = def->next) { - pack(order, buff, "l", (long) def->offset); + for (i = 0; i < nsyms; i++) { + pack(order, buff, "l", offs[i]); 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); + for (i = 0; i < nsyms; i++) { + len = strlen(names[i]) + 1; + fwrite(names[i], len, 1, fp); n += len; }