ref: 5df46147ef1c3abe8adb2afb464861736065e7a2
parent: 434c646eaa1f91755ec1b329cf276256dea5954d
author: Roberto E. Vargas Caballero <k0ga@shike2.com>
date: Thu Nov 23 17:45:34 EST 2017
[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.
--- a/as/myro.c
+++ b/as/myro.c
@@ -118,14 +118,11 @@
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 @@
hdr.relsize = writerelocs(fp);
writedata(fp);
- fseek(fp, hdrpos, SEEK_SET);
+ fseek(fp, 0, SEEK_SET);
writehdr(fp, &hdr);
if (fclose(fp))
--- 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;
--- a/lib/scc/rmyro.c
+++ b/lib/scc/rmyro.c
@@ -2,6 +2,7 @@
#include <assert.h>
#include <stdio.h>
+#include <string.h>
#include "../../inc/scc.h"
#include "../../inc/myro.h"
@@ -15,7 +16,8 @@
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,
@@ -22,6 +24,7 @@
&hdr->secsize,
&hdr->symsize,
&hdr->relsize);
+ len += MYROMAGIC_SIZ;
assert(len == MYROHDR_SIZ);
return len;
--- a/lib/scc/wmyro.c
+++ b/lib/scc/wmyro.c
@@ -2,6 +2,7 @@
#include <assert.h>
#include <stdio.h>
+#include <string.h>
#include "../../inc/scc.h"
#include "../../inc/myro.h"
@@ -12,7 +13,8 @@
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,
@@ -19,6 +21,7 @@
hdr->secsize,
hdr->symsize,
hdr->relsize);
+ len += MYROMAGIC_SIZ;
assert(MYROHDR_SIZ == len);
fwrite(buf, len, 1, fp);