scc

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

commit 5e82a4f6e2a6beb00682e148ab5d7da555c9aa87
parent a21107f7b0ad490bf153b6c13dc787b997936a7e
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Thu, 23 Nov 2017 22:45:34 +0100

[lib/scc] Add magic number to myro header

It is better to include this field in the header because it makes
the code more orthogonal.

Diffstat:
Mas/myro.c | 7++-----
Minc/myro.h | 6+++++-
Mlib/scc/rmyro.c | 5++++-
Mlib/scc/wmyro.c | 5++++-
4 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/as/myro.c b/as/myro.c @@ -118,14 +118,11 @@ void writeout(char *name) { FILE *fp; - long hdrpos; - struct myrohdr hdr = {0}; + struct myrohdr hdr = { .magic = MYROMAGIC }; if ((fp = fopen(name, "wb")) == NULL) die("error opening output file '%s'\n", name); - fputs("uobj", fp); - hdrpos = ftell(fp); writehdr(fp, &hdr); hdr.strsize = writestrings(fp); hdr.secsize = writesections(fp); @@ -133,7 +130,7 @@ writeout(char *name) hdr.relsize = writerelocs(fp); writedata(fp); - fseek(fp, hdrpos, SEEK_SET); + fseek(fp, 0, SEEK_SET); writehdr(fp, &hdr); if (fclose(fp)) diff --git a/inc/myro.h b/inc/myro.h @@ -1,10 +1,14 @@ -#define MYROHDR_SIZ 48 +#define MYROHDR_SIZ 52 #define MYROSECT_SIZ 24 #define MYROSYM_SIZ 24 #define MYROREL_SIZ 20 +#define MYROMAGIC_SIZ 4 +#define MYROMAGIC "uobj" + struct myrohdr { + char magic[4]; unsigned long format; unsigned long long entry; unsigned long long strsize; diff --git a/lib/scc/rmyro.c b/lib/scc/rmyro.c @@ -2,6 +2,7 @@ static char sccsid[] = "@(#) ./lib/scc/rmyro.c"; #include <assert.h> #include <stdio.h> +#include <string.h> #include "../../inc/scc.h" #include "../../inc/myro.h" @@ -15,13 +16,15 @@ readhdr(FILE *fp, struct myrohdr *hdr) fread(buf, sizeof(buf), 1, fp); if (ferror(fp)) return EOF; - len = lunpack(buf, "lqqqqq", + strncpy(hdr->magic, buf, MYROMAGIC_SIZ); + len = lunpack(buf + MYROMAGIC_SIZ, "lqqqqq", &hdr->format, &hdr->entry, &hdr->strsize, &hdr->secsize, &hdr->symsize, &hdr->relsize); + len += MYROMAGIC_SIZ; assert(len == MYROHDR_SIZ); return len; diff --git a/lib/scc/wmyro.c b/lib/scc/wmyro.c @@ -2,6 +2,7 @@ static char sccsid[] = "@(#) ./lib/scc/wmyro.c"; #include <assert.h> #include <stdio.h> +#include <string.h> #include "../../inc/scc.h" #include "../../inc/myro.h" @@ -12,13 +13,15 @@ writehdr(FILE *fp, struct myrohdr *hdr) unsigned char buf[MYROHDR_SIZ]; int len; - len = lpack(buf, "lqqqqq", + strncpy(buf, hdr->magic, MYROMAGIC_SIZ); + len = lpack(buf + MYROMAGIC_SIZ, "lqqqqq", hdr->format, hdr->entry, hdr->strsize, hdr->secsize, hdr->symsize, hdr->relsize); + len += MYROMAGIC_SIZ; assert(MYROHDR_SIZ == len); fwrite(buf, len, 1, fp);