shithub: scc

Download patch

ref: 434c646eaa1f91755ec1b329cf276256dea5954d
parent: 28bcaf2f4432e8d4ec3b224cf99cf4a19c60bb1a
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Nov 23 15:16:15 EST 2017

[lib/scc] Add functions to read myro files

These functions are the equivalent of the writemyro functions.

--- a/inc/myro.h
+++ b/inc/myro.h
@@ -44,3 +44,7 @@
 extern int writesec(FILE *fp, struct myrosect *sect);
 extern int writesym(FILE *fp, struct myrosym *sym);
 extern int writerel(FILE *fp, struct myrorel *rel);
+extern int readhdr(FILE *fp, struct myrohdr *hdr);
+extern int readsec(FILE *fp, struct myrosect *sect);
+extern int readsym(FILE *fp, struct myrosym *sym);
+extern int readrel(FILE *fp, struct myrorel *rel);
--- a/lib/scc/libdep.mk
+++ b/lib/scc/libdep.mk
@@ -10,3 +10,4 @@
           $(LIBDIR)/lunpack.o \
           $(LIBDIR)/lpack.o \
           $(LIBDIR)/wmyro.o \
+          $(LIBDIR)/rmyro.o \
--- /dev/null
+++ b/lib/scc/rmyro.c
@@ -1,0 +1,91 @@
+static char sccsid[] = "@(#) ./lib/scc/rmyro.c";
+
+#include <assert.h>
+#include <stdio.h>
+
+#include "../../inc/scc.h"
+#include "../../inc/myro.h"
+
+int
+readhdr(FILE *fp, struct myrohdr *hdr)
+{
+	unsigned char buf[MYROHDR_SIZ];
+	int len;
+
+	fread(buf, sizeof(buf), 1, fp);
+	if (ferror(fp))
+		return EOF;
+	len = lunpack(buf, "lqqqqq",
+	              &hdr->format,
+	              &hdr->entry,
+	              &hdr->strsize,
+	              &hdr->secsize,
+	              &hdr->symsize,
+	              &hdr->relsize);
+	assert(len == MYROHDR_SIZ);
+
+	return len;
+}
+
+int
+readsec(FILE *fp, struct myrosect *sect)
+{
+	unsigned char buf[MYROSECT_SIZ];
+	int len;
+
+	fread(buf, sizeof(buf), 1, fp);
+	if (ferror(fp))
+		return EOF;
+	len = lunpack(buf, "lsccqq",
+	              &sect->name,
+	              &sect->flags,
+	              &sect->fill,
+	              &sect->aligment,
+	              &sect->offset,
+	              &sect->len);
+	assert(len == MYROSECT_SIZ);
+
+	return len;
+}
+
+int
+readsym(FILE *fp, struct myrosym *sym)
+{
+	unsigned char buf[MYROSYM_SIZ];
+	int len;
+
+	fread(buf, sizeof(buf), 1, fp);
+	if (ferror(fp))
+		return EOF;
+	len = lunpack(buf, "llccqq",
+	              &sym->name,
+	              &sym->type,
+	              &sym->section,
+	              &sym->flags,
+	              &sym->offset,
+	              &sym->len);
+	assert(len == MYROSYM_SIZ);
+
+	return len;
+}
+
+int
+readrel(FILE *fp, struct myrorel *rel)
+{
+	unsigned char buf[MYROREL_SIZ];
+	int len;
+
+	fread(buf, sizeof(buf), 1, fp);
+	if (ferror(fp))
+		return EOF;
+	len = lunpack(buf, "lccccq",
+	              &rel->id,
+	              &rel->flags,
+	              &rel->size,
+	              &rel->nbits,
+	              &rel->shift,
+	              &rel->offset);
+	assert(len == MYROREL_SIZ);
+
+	return len;
+}
--- a/lib/scc/wmyro.c
+++ b/lib/scc/wmyro.c
@@ -1,5 +1,6 @@
 static char sccsid[] = "@(#) ./lib/scc/wmyro.c";
 
+#include <assert.h>
 #include <stdio.h>
 
 #include "../../inc/scc.h"
@@ -18,6 +19,7 @@
 	            hdr->secsize,
 	            hdr->symsize,
 	            hdr->relsize);
+	assert(MYROHDR_SIZ == len);
 	fwrite(buf, len, 1, fp);
 
 	return (ferror(fp)) ? EOF : len;
@@ -36,6 +38,7 @@
 	            sect->aligment,
 	            sect->offset,
 	            sect->len);
+	assert(MYROSECT_SIZ == len);
 	fwrite(buf, len, 1, fp);
 
 	return (ferror(fp)) ? EOF : len;
@@ -54,6 +57,7 @@
 	            sym->flags,
 	            sym->offset,
 	            sym->len);
+	assert(MYROSYM_SIZ == len);
 	fwrite(buf, len, 1, fp);
 
 	return (ferror(fp)) ? EOF : len;
@@ -72,6 +76,7 @@
 	            rel->nbits,
 	            rel->shift,
 	            rel->offset);
+	assert(MYROREL_SIZ == len);
 	fwrite(buf, len, 1, fp);
 
 	return (ferror(fp)) ? EOF : len;