commit c2df1b3d34545f2ef046aca375574aef7a6a932a
parent f73976bc3b2384aae0124998f5746eb8f84677dd
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Sat, 18 Jan 2025 14:06:07 +0100
libmach/coff32: Unify string table handling
There are different places where the union hack is done to
extend the length of the strings. While the size of the
string can be different, all of them shared the zeroes
and offset fields, so a single function can be used.
Diffstat:
5 files changed, 29 insertions(+), 20 deletions(-)
diff --git a/include/bits/scc/coff32.h b/include/bits/scc/coff32.h
@@ -66,6 +66,8 @@ extern Section *coff32getsec(Obj *, int *, Section *);
extern Section *coff32setsec(Obj *, int *, Section *);
extern Map *coff32loadmap(Obj *, FILE *);
+extern char *coff32name(Coff32 *, void *);
+
/* globals */
extern struct arch coff32archs[];
diff --git a/src/libmach/coff32/Makefile b/src/libmach/coff32/Makefile
@@ -11,6 +11,7 @@ OBJS =\
coff32getsec.o\
coff32getsym.o\
coff32loadmap.o\
+ coff32name.o\
coff32new.o\
coff32pc2line.o\
coff32probe.o\
diff --git a/src/libmach/coff32/coff32getsec.c b/src/libmach/coff32/coff32getsec.c
@@ -5,15 +5,6 @@
#include "../libmach.h"
-static char *
-secname(Coff32 *coff, SCNHDR *scn)
-{
- if (scn->s_zeroes != 0)
- return scn->s_name;
-
- return &coff->strtbl[scn->s_offset];
-}
-
Section *
coff32getsec(Obj *obj, int *idx, Section *sec)
{
@@ -88,7 +79,7 @@ coff32getsec(Obj *obj, int *idx, Section *sec)
break;
}
- sec->name = secname(coff, scn);
+ sec->name = coff32name(coff, scn);
sec->index = n;
sec->size = scn->s_size;
sec->base = scn->s_vaddr;
diff --git a/src/libmach/coff32/coff32getsym.c b/src/libmach/coff32/coff32getsym.c
@@ -54,15 +54,6 @@ typeof(Coff32 *coff, SYMENT *ent)
return c;
}
-static char *
-symname(Coff32 *coff, SYMENT *ent)
-{
- if (ent->n_zeroes != 0)
- return ent->n_name;
-
- return &coff->strtbl[ent->n_offset];
-}
-
Symbol *
coff32getsym(Obj *obj, int *idx, Symbol *sym)
{
@@ -80,7 +71,7 @@ coff32getsym(Obj *obj, int *idx, Symbol *sym)
return NULL;
ent = &ep->u.sym;
- sym->name = symname(coff, ent);
+ sym->name = coff32name(coff, ent);
sym->type = typeof(coff, ent);
sym->stype = SYMOBJECT;
sym->value = ent->n_value;
diff --git a/src/libmach/coff32/coff32name.c b/src/libmach/coff32/coff32name.c
@@ -0,0 +1,24 @@
+#include <stdio.h>
+
+#include <scc/mach.h>
+#include <scc/coff32.h>
+
+#define NAMELEN 8
+
+union name {
+ char name[NAMELEN]; /* name */
+ struct {
+ long zeroes; /* if name[0-3] == 0 */
+ long offset; /* offset into string table */
+ } s;
+};
+
+char *
+coff32name(Coff32 *coff, void *hdr)
+{
+ union name *p = hdr;
+
+ if (p->s.zeroes != 0)
+ return p->name;
+ return &coff->strtbl[p->s.offset];
+}