shithub: scc

Download patch

ref: 51f244d2ec7cce0cd116df73b29e3c9ff5cc3425
parent: 2fd2024f5922f93cfa3b743799fc03a749b94dc9
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Mon Feb 11 14:25:55 EST 2019

[libmach] Add objsyms()

Don't do any load in objread(). This make the code
more orthogonal.

--- a/include/scc/scc/mach.h
+++ b/include/scc/scc/mach.h
@@ -53,8 +53,6 @@
                      int (*fn)(FILE *, char *, void *),
                      void *data);
 
-extern int archive(FILE *fp);
-extern long armember(FILE *fp, char *member);
 extern int objtype(FILE *fp, char **name);
 extern Obj *objnew(int type);
 extern void objdel(Obj *obj);
@@ -63,6 +61,9 @@
 extern int objstrip(Obj *obj);
 extern int objwrite(Obj *obj, FILE *fp);
 extern int objsect(Obj *obj, Objsect **sect);
+extern int objsyms(Obj *obj);
+extern int archive(FILE *fp);
+extern long armember(FILE *fp, char *member);
 extern long setindex(int type, long nsyms, Objsymdef *def, FILE *fp);
 extern int getindex(int type, long *nsyms, Objsymdef **def, FILE *fp);
 extern char *namindex(int type);
--- a/src/cmd/ld.c
+++ b/src/cmd/ld.c
@@ -207,11 +207,12 @@
 		return;
 	}
 
-	if ((n = objsect(obj, &secp)) < 0) {
-		error("out of memory");
+	if ((n = objsect(obj, &secp)) < 0)
 		goto err1;
-	}
 
+	if (objsyms(obj) < 0)
+		goto err2;
+
 	lst->obj = obj;
 	lst->next = NULL;
 	lst->nsect = n;
@@ -227,8 +228,11 @@
 
 	return;
 
+err2:
+	free(secp);
 err1:
 	free(lst);
+	error("out of memory");
 }
 
 static void
--- a/src/cmd/nm.c
+++ b/src/cmd/nm.c
@@ -155,7 +155,7 @@
 		return;
 	}
 
-	if (objread(obj, fp) < 0)
+	if (objread(obj, fp) < 0 || objsyms(obj))
 		goto error;
 
 	for (sym = obj->symbols; sym; sym = sym->next)
--- a/src/cmd/ranlib.c
+++ b/src/cmd/ranlib.c
@@ -144,7 +144,7 @@
 		return 0;
 	}
 
-	if (objread(obj, fp) < 0) {
+	if (objread(obj, fp) < 0 || objsyms(obj) < 0) {
 		error("file corrupted");
 		goto error;
 	}
--- a/src/libmach/.gitignore
+++ b/src/libmach/.gitignore
@@ -10,3 +10,4 @@
 setidx.c
 namidx.c
 getsect.c
+getsyms.c
--- a/src/libmach/Makefile
+++ b/src/libmach/Makefile
@@ -4,17 +4,18 @@
 
 TARGET = $(LIBDIR)/libmach.a
 
-OBJS = addr2line.o \
-       archive.o \
-       armember.o \
-       objdel.o \
-       objlookup.o \
-       objnew.o \
+OBJS = objnew.o \
        objpos.o \
        objread.o \
        objfree.o \
        objstrip.o \
        objsect.o \
+       objsyms.o \
+       objdel.o \
+       addr2line.o \
+       archive.o \
+       armember.o \
+       objlookup.o \
        getindex.o \
        setindex.o \
        namindex.o \
@@ -33,13 +34,14 @@
        setidx.o \
        namidx.o \
        getsect.o \
+       getsyms.o \
 
-
 DIRS = coff32
 
 TBLS = setidx.c \
        getidx.c \
        getsect.c \
+       getsyms.c \
        namidx.c \
        new.c \
        read.c \
--- a/src/libmach/coff32/Makefile
+++ b/src/libmach/coff32/Makefile
@@ -14,6 +14,7 @@
        coff32getidx.o \
        coff32namidx.o \
        coff32getsect.o \
+       coff32getsyms.o \
 
 all: $(OBJS)
 
--- /dev/null
+++ b/src/libmach/coff32/coff32getsyms.c
@@ -1,0 +1,90 @@
+#include <stdio.h>
+#include <ctype.h>
+
+#include <scc/mach.h>
+
+#include "../libmach.h"
+#include "coff32.h"
+
+static int
+typeof(Coff32 *coff, SYMENT *ent)
+{
+	int c;
+	SCNHDR *scn;
+	long flags;
+
+	switch (ent->n_scnum) {
+	case N_DEBUG:
+		c = 'N';
+		break;
+	case N_ABS:
+		c = 'a';
+		break;
+	case N_UNDEF:
+		c = (ent->n_value != 0) ? 'C' : 'U';
+		break;
+	default:
+		if (ent->n_scnum > coff->hdr.f_nscns)
+			return -1;
+		scn = &coff->scns[ent->n_scnum-1];
+		flags = scn->s_flags;
+		if (flags & STYP_TEXT)
+			c = 't';
+		else if (flags & STYP_DATA)
+			c = 'd';
+		else if (flags & STYP_BSS)
+			c = 'b';
+		else
+			c = '?';
+		break;
+	}
+
+	if (ent->n_sclass == C_EXT)
+		c = toupper(c);
+
+	return c;
+}
+
+static char *
+symname(Coff32 *coff, SYMENT *ent)
+{
+	long off;
+
+	if (ent->n_zeroes != 0)
+		return ent->n_name;
+
+	off = ent->n_offset;
+	if (off >= coff->strsiz)
+		return NULL;
+	return &coff->strtbl[off];
+}
+
+int
+coff32getsyms(Obj *obj)
+{
+	int t;
+	long i;
+	char *s;
+	Objsym *sym;
+	SYMENT *ent;
+	Coff32 *coff = obj->data;
+
+	for (i = 0; i < coff->hdr.f_nsyms; i += ent->n_numaux + 1) {
+		ent = &coff->ents[i];
+
+		if ((t = typeof(coff, ent)) < 0)
+			return 0;
+
+		if ((s = symname(coff, ent)) == NULL)
+			return 0;
+
+		if ((sym = objlookup(obj, s, 1)) == NULL)
+			return 0;
+
+		sym->type = t;
+		sym->value = ent->n_value;
+		sym->size = (sym->type == 'C') ? ent->n_value : 0;
+	}
+
+	return 1;
+}
--- a/src/libmach/coff32/coff32read.c
+++ b/src/libmach/coff32/coff32read.c
@@ -1,5 +1,4 @@
 #include <assert.h>
-#include <ctype.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -245,89 +244,6 @@
 }
 
 static int
-typeof(Coff32 *coff, SYMENT *ent)
-{
-	int c;
-	SCNHDR *scn;
-	long flags;
-
-	switch (ent->n_scnum) {
-	case N_DEBUG:
-		c = 'N';
-		break;
-	case N_ABS:
-		c = 'a';
-		break;
-	case N_UNDEF:
-		c = (ent->n_value != 0) ? 'C' : 'U';
-		break;
-	default:
-		if (ent->n_scnum > coff->hdr.f_nscns)
-			return -1;
-		scn = &coff->scns[ent->n_scnum-1];
-		flags = scn->s_flags;
-		if (flags & STYP_TEXT)
-			c = 't';
-		else if (flags & STYP_DATA)
-			c = 'd';
-		else if (flags & STYP_BSS)
-			c = 'b';
-		else
-			c = '?';
-		break;
-	}
-
-	if (ent->n_sclass == C_EXT)
-		c = toupper(c);
-
-	return c;
-}
-
-static char *
-symname(Coff32 *coff, SYMENT *ent)
-{
-	long off;
-
-	if (ent->n_zeroes != 0)
-		return ent->n_name;
-
-	off = ent->n_offset;
-	if (off >= coff->strsiz)
-		return NULL;
-	return &coff->strtbl[off];
-}
-
-static int
-loadsyms(Obj *obj)
-{
-	int t;
-	long i;
-	char *s;
-	Objsym *sym;
-	SYMENT *ent;
-	Coff32 *coff = obj->data;
-
-	for (i = 0; i < coff->hdr.f_nsyms; i += ent->n_numaux + 1) {
-		ent = &coff->ents[i];
-
-		if ((t = typeof(coff, ent)) < 0)
-			return 0;
-
-		if ((s = symname(coff, ent)) == NULL)
-			return 0;
-
-		if ((sym = objlookup(obj, s, 1)) == NULL)
-			return 0;
-
-		sym->type = t;
-		sym->value = ent->n_value;
-		sym->size = (sym->type == 'C') ? ent->n_value : 0;
-	}
-
-	return 1;
-}
-
-static int
 readscns(Obj *obj, FILE *fp)
 {
 	FILHDR *hdr;
@@ -439,8 +355,6 @@
 	if (!readreloc(obj, fp))
 		goto error;
 	if (!readlines(obj, fp))
-		goto error;
-	if (!loadsyms(obj))
 		goto error;
 	return 0;
 
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -38,6 +38,7 @@
 typedef int (*getidxfun_t)(int t, long *n, Objsymdef **def, FILE *fp);
 typedef int (*getsectfun_t)(Obj *obj, Objsect **secp);
 typedef char *(*namidxfun_t)(void);
+typedef int (*getsymsfun_t)(Obj *obj);
 
 /* common functions */
 extern int pack(int order, unsigned char *dst, char *fmt, ...);
@@ -63,3 +64,4 @@
 extern int coff32getsect(Obj *obj, Objsect **secp);
 
 extern char *coff32namidx(void);
+extern int coff32getsyms(Obj *obj);