shithub: scc

Download patch

ref: 61f5fa44e9f8b7ce6ee4cd5bd71c10845287960d
parent: dccebc26a1d24f28a40b96d428e6409999ae01f8
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Aug 22 05:25:14 EDT 2019

[libmach] Remove target getsyms and getsect

These function are only called from objread(), so
it is better to join these functions in only one file
instead of having 3 different files.

--- a/src/libmach/coff32/Makefile
+++ b/src/libmach/coff32/Makefile
@@ -12,8 +12,6 @@
        coff32getindex.o \
        coff32setidx.o \
        coff32getidx.o \
-       coff32getsect.o \
-       coff32getsyms.o \
        coff32sync.o \
 
 all: $(OBJS)
--- a/src/libmach/coff32/coff32getsect.c
+++ /dev/null
@@ -1,78 +1,0 @@
-#include <stdio.h>
-#include <stdlib.h>
-
-#include <scc/mach.h>
-
-#include "../libmach.h"
-#include "coff32.h"
-
-int
-coff32getsect(Obj *obj)
-{
-	int i;
-	unsigned sflags, type;
-	unsigned long flags;
-	FILHDR *hdr;
-	struct coff32 *coff;
-	SCNHDR *scn;
-	Objsect *secs, *sp;
-
-	coff  = obj->data;
-	hdr = &coff->hdr;
-
-	secs = malloc(sizeof(Objsect) * hdr->f_nscns);
-	if (!secs)
-		return -1;
-
-	for (i = 0; i < hdr->f_nscns; i++) {
-		sp = &secs[i];
-		sp->next = (i < hdr->f_nscns-1) ? &secs[i+1] : NULL;
-		scn = &coff->scns[i];
-		flags = scn->s_flags;
-
-		if (flags & STYP_TEXT) {
-			type = 'T';
-			sflags = SALLOC | SRELOC | SLOAD | SEXEC | SREAD;
-			if (flags & STYP_NOLOAD)
-				sflags |= SSHARED;
-		} else if (flags & STYP_DATA) {
-			type = 'D';
-			sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
-			if (flags & STYP_NOLOAD)
-				sflags |= SSHARED;
-		} else if (flags & STYP_BSS) {
-			type = 'B';
-			sflags = SALLOC | SREAD | SWRITE;
-		} else if (flags & STYP_INFO) {
-			type = 'N';
-			sflags = 0;
-		} else if (flags & STYP_LIB) {
-			type = 'T';
-			sflags = SRELOC;
-		} else if (flags & STYP_DSECT) {
-			type = 'D';
-			sflags = SRELOC;
-		} else if (flags & STYP_PAD) {
-			type = 'D';
-			sflags = SLOAD;
-		} else {
-			type = 'D';  /* We assume that STYP_REG is data */
-			sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
-		}
-
-		if (flags & STYP_NOLOAD)
-			sflags &= ~SLOAD;
-
-		sp->name = scn->s_name;
-		sp->id = i;
-		sp->seek = scn->s_scnptr;
-		sp->size = scn->s_size;
-		sp->type = type;
-		sp->flags = sflags;
-		sp->align = 4; /* TODO: Check how align is defined in coff */
-	}
-	obj->secs = secs;
-	obj->nsecs = i;
-
-	return 1;
-}
--- a/src/libmach/coff32/coff32getsyms.c
+++ /dev/null
@@ -1,91 +1,0 @@
-#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 -1;
-
-		if ((s = symname(coff, ent)) == NULL)
-			return -1;
-
-		if ((sym = objlookup(obj, s, 1)) == NULL)
-			return -1;
-
-		sym->type = t;
-		sym->value = ent->n_value;
-		sym->size = (sym->type == 'C') ? ent->n_value : 0;
-		sym->sect = ent->n_scnum-1;
-	}
-
-	return i;
-}
--- a/src/libmach/coff32/coff32read.c
+++ b/src/libmach/coff32/coff32read.c
@@ -1,4 +1,5 @@
 #include <assert.h>
+#include <ctype.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -336,8 +337,8 @@
 	return 1;
 }
 
-int
-coff32read(Obj *obj, FILE *fp)
+static int
+readfile(Obj *obj, FILE *fp)
 {
 	/* TODO: Add validation of the different fields */
 	if (fgetpos(fp, &obj->pos))
@@ -361,4 +362,171 @@
 error:
 	objfree(obj, TARGETDEL);
 	return -1;
+}
+
+static int
+convsect(Obj *obj)
+{
+	int i;
+	unsigned sflags, type;
+	unsigned long flags;
+	FILHDR *hdr;
+	struct coff32 *coff;
+	SCNHDR *scn;
+	Objsect *secs, *sp;
+
+	coff  = obj->data;
+	hdr = &coff->hdr;
+
+	secs = malloc(sizeof(Objsect) * hdr->f_nscns);
+	if (!secs)
+		return -1;
+
+	for (i = 0; i < hdr->f_nscns; i++) {
+		sp = &secs[i];
+		sp->next = (i < hdr->f_nscns-1) ? &secs[i+1] : NULL;
+		scn = &coff->scns[i];
+		flags = scn->s_flags;
+
+		if (flags & STYP_TEXT) {
+			type = 'T';
+			sflags = SALLOC | SRELOC | SLOAD | SEXEC | SREAD;
+			if (flags & STYP_NOLOAD)
+				sflags |= SSHARED;
+		} else if (flags & STYP_DATA) {
+			type = 'D';
+			sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
+			if (flags & STYP_NOLOAD)
+				sflags |= SSHARED;
+		} else if (flags & STYP_BSS) {
+			type = 'B';
+			sflags = SALLOC | SREAD | SWRITE;
+		} else if (flags & STYP_INFO) {
+			type = 'N';
+			sflags = 0;
+		} else if (flags & STYP_LIB) {
+			type = 'T';
+			sflags = SRELOC;
+		} else if (flags & STYP_DSECT) {
+			type = 'D';
+			sflags = SRELOC;
+		} else if (flags & STYP_PAD) {
+			type = 'D';
+			sflags = SLOAD;
+		} else {
+			type = 'D';  /* We assume that STYP_REG is data */
+			sflags = SALLOC | SRELOC | SLOAD | SWRITE | SREAD;
+		}
+
+		if (flags & STYP_NOLOAD)
+			sflags &= ~SLOAD;
+
+		sp->name = scn->s_name;
+		sp->id = i;
+		sp->seek = scn->s_scnptr;
+		sp->size = scn->s_size;
+		sp->type = type;
+		sp->flags = sflags;
+		sp->align = 4; /* TODO: Check how align is defined in coff */
+	}
+	obj->secs = secs;
+	obj->nsecs = i;
+
+	return 1;
+}
+
+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
+convsyms(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 -1;
+
+		if ((s = symname(coff, ent)) == NULL)
+			return -1;
+
+		if ((sym = objlookup(obj, s, 1)) == NULL)
+			return -1;
+
+		sym->type = t;
+		sym->value = ent->n_value;
+		sym->size = (sym->type == 'C') ? ent->n_value : 0;
+		sym->sect = ent->n_scnum-1;
+	}
+
+	return i;
+}
+
+int
+coff32read(Obj *obj, FILE *fp)
+{
+	if (readfile(obj, fp) < 0)
+		return -1;
+	if (convsect(obj) < 0)
+		return -1;
+	if (convsyms(obj) < 0)
+		return -1;
+	return 0;
 }
--- a/src/libmach/libmach.h
+++ b/src/libmach/libmach.h
@@ -49,8 +49,5 @@
 extern int coff32getindex(int type, long *nsyms, Objsymdef **def, FILE *fp);
 extern int coff32getidx(int order, long *nsyms, Objsymdef **def, FILE *fp);
 
-extern int coff32getsect(Obj *obj);
-
 extern char *coff32namidx(void);
-extern int coff32getsyms(Obj *obj);
 extern int coff32sync(Obj *obj);
--- a/src/libmach/objread.c
+++ b/src/libmach/objread.c
@@ -8,16 +8,8 @@
 	[COFF32] = coff32read,
 };
 
-static int (*fsymsv[])(Obj *) = {
-	[COFF32] = coff32getsyms,
-};
-
-static int (*fsectsv[])(Obj *) = {
-	[COFF32] = coff32getsect,
-};
-
-static int
-getsects(Obj *obj)
+int
+objread(Obj *obj, FILE *fp)
 {
 	int fmt;
 
@@ -25,51 +17,9 @@
 	if (fmt >= NFORMATS)
 		return -1;
 
-	return  (*fsectsv[fmt])(obj);
-}
-
-static int
-getsyms(Obj *obj)
-{
-	int fmt;
-
-	fmt = FORMAT(obj->type);
-	if (fmt >= NFORMATS)
-		return -1;
-
-	return  (*fsymsv[fmt])(obj);
-}
-
-static int
-readfile(Obj *obj, FILE *fp)
-{
-	int fmt;
-
-	fmt = FORMAT(obj->type);
-	if (fmt >= NFORMATS)
-		return -1;
-
 	if ((*freadv[fmt])(obj, fp) < 0)
 		return -1;
 	obj->fp = fp;
 
 	return 0;
-}
-
-int
-objread(Obj *obj, FILE *fp)
-{
-	objfree(obj, TARGETDEL | GENERICDEL);
-
-	if (readfile(obj, fp) < 0)
-		goto err;
-	if (getsyms(obj) < 0)
-		goto err;
-	if (getsects(obj) < 0)
-		goto err;
-	return 0;
-
-err:
-	objfree(obj, TARGETDEL | GENERICDEL);
-	return -1;
 }