ref: b780650286944308871d3fe28c98a41714cae927
parent: a32972000ab291dd3cefeceb5a62864b0e01a371
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Fri Aug 23 18:06:53 EDT 2019
[libmach] Remove Objsymdef from getidx() This is the second step to remove Objsymdef.
--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -60,7 +60,7 @@
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 {
--- a/src/cmd/ld/pass1.c
+++ b/src/cmd/ld/pass1.c
@@ -206,22 +206,22 @@
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 @@
break;
}
clean:
- free(def);
+ for (i = 0; i < n; i++)
+ free(names[i]);
+ free(names);
+ free(offs);
}
static int
--- a/src/libmach/coff32/coff32.h
+++ b/src/libmach/coff32/coff32.h
@@ -26,8 +26,8 @@
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 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);
--- 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);
}
--- 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 @@
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;
}