scc

simple C compiler
git clone git://git.2f30.org/scc
Log | Files | Refs | README | LICENSE

commit a21107f7b0ad490bf153b6c13dc787b997936a7e
parent d002ec15cf34d032d59872e75afb96bb4eddc4a6
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 23 Nov 2017 20:16:15 +0100

[lib/scc] Add functions to read myro files

These functions are the equivalent of the writemyro functions.

Diffstat:
Minc/myro.h | 4++++
Mlib/scc/libdep.mk | 1+
Alib/scc/rmyro.c | 91+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlib/scc/wmyro.c | 5+++++
4 files changed, 101 insertions(+), 0 deletions(-)

diff --git a/inc/myro.h b/inc/myro.h @@ -44,3 +44,7 @@ extern int writehdr(FILE *fp, struct myrohdr *hdr); 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); diff --git a/lib/scc/libdep.mk b/lib/scc/libdep.mk @@ -10,3 +10,4 @@ LIB-OBJ = $(LIBDIR)/debug.o \ $(LIBDIR)/lunpack.o \ $(LIBDIR)/lpack.o \ $(LIBDIR)/wmyro.o \ + $(LIBDIR)/rmyro.o \ diff --git a/lib/scc/rmyro.c b/lib/scc/rmyro.c @@ -0,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; +} diff --git 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 @@ writehdr(FILE *fp, struct myrohdr *hdr) hdr->secsize, hdr->symsize, hdr->relsize); + assert(MYROHDR_SIZ == len); fwrite(buf, len, 1, fp); return (ferror(fp)) ? EOF : len; @@ -36,6 +38,7 @@ writesec(FILE *fp, struct myrosect *sect) sect->aligment, sect->offset, sect->len); + assert(MYROSECT_SIZ == len); fwrite(buf, len, 1, fp); return (ferror(fp)) ? EOF : len; @@ -54,6 +57,7 @@ writesym(FILE *fp, struct myrosym *sym) sym->flags, sym->offset, sym->len); + assert(MYROSYM_SIZ == len); fwrite(buf, len, 1, fp); return (ferror(fp)) ? EOF : len; @@ -72,6 +76,7 @@ writerel(FILE *fp, struct myrorel *rel) rel->nbits, rel->shift, rel->offset); + assert(MYROREL_SIZ == len); fwrite(buf, len, 1, fp); return (ferror(fp)) ? EOF : len;