scc

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

commit e51e26441da26a60332f18f67358b2195def63be
parent aa999e2efe9944cd1b47ea2f7de9c9dd9ae53e93
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Sun, 10 Feb 2019 19:48:15 +0000

[libmach] Implement coff32getidx()

Diffstat:
Msrc/libmach/coff32/coff32getidx.c | 51+++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 51 insertions(+), 0 deletions(-)

diff --git a/src/libmach/coff32/coff32getidx.c b/src/libmach/coff32/coff32getidx.c @@ -1,6 +1,9 @@ #include <stdio.h> +#include <stdlib.h> +#include <string.h> #include <scc/mach.h> +#include <scc/cstd.h> #include "../libmach.h" #include "coff32.h" @@ -8,4 +11,52 @@ int coff32getidx(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; }