ref: 5750587d1344416e2a6d6ee1395e250558de97ac
parent: 7edf451bf7accdac3edb62dbba35e0713072a9f0
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Sun Feb 10 14:48:15 EST 2019
[libmach] Implement coff32getidx()
--- 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;
}