diff --git a/Makefile b/Makefile index b07e3caf..64d53d14 100644 --- a/Makefile +++ b/Makefile @@ -210,7 +210,8 @@ $(BIN) $(LIB) $(OBJ)/wcc $(OBJ)/ww $(OBJ)/wwdump $(OBJ)/w6c $(OBJ)/w6a $(OBJ)/w6 # ---- tests ------------------------------------------------------------- # Each phase adds a $(BIN)/test_ target; the runner walks them. TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ - $(BIN)/test_w6c $(BIN)/test_w6a $(BIN)/test_w6l $(BIN)/test_arch \ + $(BIN)/test_w6c $(BIN)/test_w6a $(BIN)/test_dataw $(BIN)/test_w6l \ + $(BIN)/test_arch \ $(BIN)/test_e2e $(BIN)/test_ffi $(BIN)/test_dyn $(BIN)/test_stdlib \ $(BIN)/test_at_test \ $(BIN)/test_selfhost $(BIN)/test_w6a_ww $(BIN)/test_w6l_ww \ @@ -235,6 +236,9 @@ $(BIN)/test_w6c: test/wcc/400_w6c.c $(BIN)/w6c | $(BIN) $(BIN)/test_w6a: test/wcc/500_w6a.c $(BIN)/w6c $(BIN)/w6a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_dataw: test/wcc/510_dataw.c $(BIN)/w6a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_w6l: test/wcc/600_w6l.c $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l | $(BIN) $(CC) $(CFLAGS) -o $@ $< diff --git a/cmd/w6a/a.h b/cmd/w6a/a.h index 0bee42d2..b141a564 100644 --- a/cmd/w6a/a.h +++ b/cmd/w6a/a.h @@ -52,6 +52,7 @@ struct Asym { const char *name; int defined; /* 1 if we own its address */ int is_text; /* if 1, address is in .text */ + int is_data; /* if 1, address is in .data (mutually exclusive with is_text) */ int is_global; /* exported (TEXT) */ u64 addr; /* offset within section if defined */ int idx; /* ELF symtab index, filled at emit time */ @@ -81,6 +82,12 @@ struct Asm { u8 *text; u64 textcap, textlen; + /* output data section (writable). Empty unless any DATAW directive + * was seen; in that case obj.c emits an extra .data PROGBITS + * section with SHF_WRITE. */ + u8 *data; + u64 datacap, datalen; + /* symbols */ Asym *syms; Areloc *relocs; @@ -102,6 +109,7 @@ int a_emit_elf(Asm*, FILE *out); Asym *a_intern(Asm*, const char *name); void a_emit_byte(Asm*, u8); void a_emit_u32(Asm*, u32); +void a_emit_data_byte(Asm*, u8); void a_addreloc(Asm*, u64 off, int kind, Asym *s, i64 add); #endif diff --git a/cmd/w6a/asm.c b/cmd/w6a/asm.c index c5a3271b..577da513 100644 --- a/cmd/w6a/asm.c +++ b/cmd/w6a/asm.c @@ -54,6 +54,17 @@ a_emit_u32(Asm *a, u32 v) a_emit_byte(a, (u8)((v >> 24) & 0xff)); } +void +a_emit_data_byte(Asm *a, u8 b) +{ + if (a->datalen + 1 > a->datacap) { + u64 nc = a->datacap ? a->datacap * 2 : 256; + a->data = realloc(a->data, nc); + a->datacap = nc; + } + a->data[a->datalen++] = b; +} + void a_addreloc(Asm *a, u64 off, int kind, Asym *s, i64 add) { @@ -310,6 +321,20 @@ a_encode(Asm *a) a_emit_byte(a, p->bytes[i]); break; } + case A_DATAW: { + /* Writable variant: bytes go into .data (RW) instead + * of .text. obj.c emits the extra section conditionally + * on datalen > 0 so .o output stays byte-identical for + * inputs that don't use DATAW. */ + Asym *s = a_intern(a, p->to.sym); + s->defined = 1; + s->is_data = 1; + s->is_global = 1; + s->addr = a->datalen; + for (u64 i = 0; i < p->nbytes; i++) + a_emit_data_byte(a, p->bytes[i]); + break; + } case A_RET: a_emit_byte(a, 0xC3); break; diff --git a/cmd/w6a/obj.c b/cmd/w6a/obj.c index 351941f6..f7a402ca 100644 --- a/cmd/w6a/obj.c +++ b/cmd/w6a/obj.c @@ -5,10 +5,15 @@ * [0] ELF header * [1] Section .text (program bytes) * [2] Section .rela.text (relocations) - * [3] Section .symtab - * [4] Section .strtab - * [5] Section .shstrtab - * [6] Section header table + * [3] Section .data (writable; only present if datalen > 0) + * [4] Section .symtab + * [5] Section .strtab + * [6] Section .shstrtab + * [7] Section header table + * + * When no DATAW directive appears in the input, the .data section is + * omitted entirely so output stays byte-identical to the pre-DATAW + * format. Test 991 (selfhost .o byte-diff) depends on this. * * Symtab indices: 0 = STN_UNDEF, 1 = file (skipped), 2.. = our syms. * For simplicity we emit GLOBAL symbols only (no LOCAL ordering rules @@ -33,6 +38,7 @@ #define SHT_STRTAB 3 #define SHT_RELA 4 +#define SHF_WRITE 0x1 #define SHF_ALLOC 0x2 #define SHF_EXECINSTR 0x4 #define SHF_INFO_LINK 0x40 @@ -40,6 +46,7 @@ #define STB_LOCAL 0 #define STB_GLOBAL 1 #define STT_NOTYPE 0 +#define STT_OBJECT 1 #define STT_FUNC 2 #define ELF64_ST_INFO(b,t) (((b) << 4) + ((t) & 0xf)) @@ -108,9 +115,23 @@ a_emit_elf(Asm *a, FILE *f) stput(&shstr, ""); /* idx 0 = empty */ stput(&str, ""); - /* Section name offsets */ + const int has_data = (a->datalen > 0); + + /* Section indices. + * Without data: 1=.text, 2=.rela.text, 3=.symtab, 4=.strtab, 5=.shstrtab + * With data: 1=.text, 2=.rela.text, 3=.data, 4=.symtab, 5=.strtab, 6=.shstrtab + */ + const u16 SH_TEXT = 1; + const u16 SH_DATA = has_data ? 3 : 0; + const u16 SH_SYMTAB = has_data ? 4 : 3; + const u16 SH_STRTAB = has_data ? 5 : 4; + const u16 SH_SHSTR = has_data ? 6 : 5; + + /* Section name offsets. Append .data's name only when used so the + * .shstrtab buffer stays byte-identical for the no-DATAW case. */ u32 shn_text = stput(&shstr, ".text"); u32 shn_rela = stput(&shstr, ".rela.text"); + u32 shn_data = has_data ? stput(&shstr, ".data") : 0; u32 shn_symtab = stput(&shstr, ".symtab"); u32 shn_strtab = stput(&shstr, ".strtab"); u32 shn_shstrtab = stput(&shstr, ".shstrtab"); @@ -121,17 +142,22 @@ a_emit_elf(Asm *a, FILE *f) bput(&sym, &z, sizeof z); } - /* Section indices: 1=.text, 2=.rela.text, 3=.symtab, 4=.strtab, 5=.shstrtab */ - const u16 SH_TEXT = 1; - - /* Build symbols (defined = global; undefined = global UND) */ + /* Build symbols (defined = global; undefined = global UND). Data + * symbols carry STT_OBJECT and st_shndx=SH_DATA; everything else + * keeps the legacy STT_FUNC/SH_TEXT shape so non-DATAW outputs + * stay byte-identical. */ int idx = 1; for (Asym *s = a->syms; s; s = s->next) { Sym64 e = {0}; e.st_name = stput(&str, s->name); if (s->defined) { - e.st_info = ELF64_ST_INFO(STB_GLOBAL, STT_FUNC); - e.st_shndx = SH_TEXT; + if (s->is_data) { + e.st_info = ELF64_ST_INFO(STB_GLOBAL, STT_OBJECT); + e.st_shndx = SH_DATA; + } else { + e.st_info = ELF64_ST_INFO(STB_GLOBAL, STT_FUNC); + e.st_shndx = SH_TEXT; + } e.st_value = s->addr; e.st_size = 0; } else { @@ -155,13 +181,14 @@ a_emit_elf(Asm *a, FILE *f) u64 off = sizeof(Ehdr); u64 off_text = off; off += a->textlen; u64 off_rela = off; off += rela.n; + u64 off_data = off; if (has_data) off += a->datalen; u64 off_sym = off; off += sym.n; u64 off_str = off; off += str.n; u64 off_shstr= off; off += shstr.n; /* align to 8 */ while (off % 8) off++; u64 off_shdr = off; - const int NSECT = 6; /* null + 5 real */ + const int NSECT = has_data ? 7 : 6; /* null + reals */ Ehdr eh = {0}; memcpy(eh.e_ident, "\x7f""ELF", 4); @@ -175,11 +202,12 @@ a_emit_elf(Asm *a, FILE *f) eh.e_ehsize = sizeof(Ehdr); eh.e_shentsize = sizeof(Shdr); eh.e_shnum = NSECT; - eh.e_shstrndx = 5; + eh.e_shstrndx = SH_SHSTR; fwrite(&eh, 1, sizeof eh, f); if (a->textlen) fwrite(a->text, 1, a->textlen, f); fwrite(rela.p, 1, rela.n, f); + if (has_data) fwrite(a->data, 1, a->datalen, f); fwrite(sym.p, 1, sym.n, f); fwrite(str.p, 1, str.n, f); fwrite(shstr.p, 1, shstr.n, f); @@ -204,18 +232,29 @@ a_emit_elf(Asm *a, FILE *f) sh.sh_flags = SHF_INFO_LINK; sh.sh_offset = off_rela; sh.sh_size = rela.n; - sh.sh_link = 3; /* symtab */ + sh.sh_link = SH_SYMTAB; sh.sh_info = 1; /* applies to .text */ sh.sh_addralign = 8; sh.sh_entsize = sizeof(Rela64); fwrite(&sh, 1, sizeof sh, f); + if (has_data) { + memset(&sh, 0, sizeof sh); + sh.sh_name = shn_data; + sh.sh_type = SHT_PROGBITS; + sh.sh_flags = SHF_ALLOC | SHF_WRITE; + sh.sh_offset = off_data; + sh.sh_size = a->datalen; + sh.sh_addralign = 8; + fwrite(&sh, 1, sizeof sh, f); + } + memset(&sh, 0, sizeof sh); sh.sh_name = shn_symtab; sh.sh_type = SHT_SYMTAB; sh.sh_offset = off_sym; sh.sh_size = sym.n; - sh.sh_link = 4; /* strtab */ + sh.sh_link = SH_STRTAB; sh.sh_info = 1; /* one local: STN_UNDEF */ sh.sh_addralign = 8; sh.sh_entsize = sizeof(Sym64); diff --git a/cmd/w6a/parse.c b/cmd/w6a/parse.c index 2cbb6901..94e9f7da 100644 --- a/cmd/w6a/parse.c +++ b/cmd/w6a/parse.c @@ -120,6 +120,7 @@ opcode_lookup(const char *m) { "SYSCALL", A_SYSCALL }, { "TEXT", A_TEXT }, { "DATA", A_DATA }, + { "DATAW", A_DATAW }, { NULL, 0 } }; for (int i = 0; tab[i].m; i++) @@ -324,8 +325,9 @@ a_parse(Asm *a) prg->from.type = D_CONST; prg->from.offset = a_parsenum(m, NULL); } - } else if (op == A_DATA) { - /* DATA name(SB),"escaped bytes" */ + } else if (op == A_DATA || op == A_DATAW) { + /* DATA name(SB),"escaped bytes" — read-only in .text + * DATAW name(SB),"escaped bytes" — writable in .data */ char nbuf[128] = {0}; int nn = 0; while (*m && *m != '(' && nn < 127) nbuf[nn++] = *m++; diff --git a/cmd/w6c/6.out.h b/cmd/w6c/6.out.h index 23729b1b..5d53d6a6 100644 --- a/cmd/w6c/6.out.h +++ b/cmd/w6c/6.out.h @@ -40,6 +40,7 @@ enum { A_NOP = 0, A_TEXT, A_DATA, + A_DATAW, /* writable DATA: lands in .data (RW) instead of .text */ A_GLOBL, A_END, diff --git a/test/wcc/510_dataw.c b/test/wcc/510_dataw.c new file mode 100644 index 00000000..52bc50f9 --- /dev/null +++ b/test/wcc/510_dataw.c @@ -0,0 +1,284 @@ +/* + * 510_dataw — DATAW directive smoke. Assemble a tiny .s that uses + * DATAW and verify the resulting .o has a writable .data section + * containing the right bytes, with a symbol that points to it. + * + * Also confirms the no-DATAW path stays byte-identical to the + * pre-DATAW format (6 sections, no .data); the selfhost .o diff in + * 991 depends on that invariant. + */ +#include +#include +#include +#include +#include + +#define SHT_PROGBITS 1 +#define SHT_SYMTAB 2 +#define SHF_WRITE 0x1 +#define SHF_ALLOC 0x2 +#define SHF_EXECINSTR 0x4 + +#pragma pack(push, 1) +typedef struct { + uint8_t e_ident[16]; + uint16_t e_type, e_machine; + uint32_t e_version; + uint64_t e_entry, e_phoff, e_shoff; + uint32_t e_flags; + uint16_t e_ehsize, e_phentsize, e_phnum, e_shentsize, e_shnum, e_shstrndx; +} Ehdr; + +typedef struct { + uint32_t sh_name, sh_type; + uint64_t sh_flags, sh_addr, sh_offset, sh_size; + uint32_t sh_link, sh_info; + uint64_t sh_addralign, sh_entsize; +} Shdr; + +typedef struct { + uint32_t st_name; + uint8_t st_info, st_other; + uint16_t st_shndx; + uint64_t st_value, st_size; +} Sym64; +#pragma pack(pop) + +static int +write_file(const char *path, const char *body) +{ + FILE *f = fopen(path, "wb"); + if (!f) return -1; + fputs(body, f); + fclose(f); + return 0; +} + +static int +slurp(const char *path, uint8_t **out, size_t *outn) +{ + FILE *f = fopen(path, "rb"); + if (!f) return -1; + fseek(f, 0, SEEK_END); + long n = ftell(f); + fseek(f, 0, SEEK_SET); + uint8_t *b = malloc((size_t)n); + if (fread(b, 1, (size_t)n, f) != (size_t)n) { + free(b); + fclose(f); + return -1; + } + fclose(f); + *out = b; + *outn = (size_t)n; + return 0; +} + +/* Find the section with the given name. Returns index, or -1. */ +static int +find_section(const uint8_t *buf, const char *name) +{ + const Ehdr *eh = (const Ehdr *)buf; + const Shdr *sh = (const Shdr *)(buf + eh->e_shoff); + const char *shstr = (const char *)(buf + sh[eh->e_shstrndx].sh_offset); + for (uint16_t i = 0; i < eh->e_shnum; i++) { + if (strcmp(shstr + sh[i].sh_name, name) == 0) + return (int)i; + } + return -1; +} + +/* Find a symbol by name in the .symtab. Returns pointer, or NULL. */ +static const Sym64 * +find_symbol(const uint8_t *buf, const char *name) +{ + const Ehdr *eh = (const Ehdr *)buf; + const Shdr *sh = (const Shdr *)(buf + eh->e_shoff); + int idx_symtab = -1; + for (uint16_t i = 0; i < eh->e_shnum; i++) + if (sh[i].sh_type == SHT_SYMTAB) { idx_symtab = i; break; } + if (idx_symtab < 0) return NULL; + int idx_strtab = (int)sh[idx_symtab].sh_link; + const Sym64 *syms = (const Sym64 *)(buf + sh[idx_symtab].sh_offset); + uint64_t nsyms = sh[idx_symtab].sh_size / sizeof(Sym64); + const char *str = (const char *)(buf + sh[idx_strtab].sh_offset); + for (uint64_t i = 1; i < nsyms; i++) { + if (strcmp(str + syms[i].st_name, name) == 0) + return &syms[i]; + } + return NULL; +} + +static int +test_dataw_emits_writable_section(const char *bin) +{ + char src[64], obj[64]; + snprintf(src, sizeof src, "/tmp/wwt_dataw_%d.s", getpid()); + snprintf(obj, sizeof obj, "/tmp/wwt_dataw_%d.o", getpid()); + + /* Hand-crafted .s: one writable data symbol holding 8 bytes (the + * little-endian encoding of the i64 42) plus a trivial TEXT so + * the assembler has something in .text to anchor. */ + const char *body = + "TEXT _start(SB),$0\n" + "\tMOVQ\t$0, AX\n" + "\tRET\n" + "DATAW counter(SB),\"\\x2a\\x00\\x00\\x00\\x00\\x00\\x00\\x00\"\n"; + if (write_file(src, body) < 0) { + fprintf(stderr, "dataw: cannot write %s\n", src); + return -1; + } + + char cmd[512]; + snprintf(cmd, sizeof cmd, "%s/w6a -o %s %s", bin, obj, src); + if (system(cmd) != 0) { + fprintf(stderr, "dataw: w6a failed\n"); + unlink(src); + return -1; + } + + uint8_t *buf = NULL; + size_t n = 0; + if (slurp(obj, &buf, &n) < 0) { + fprintf(stderr, "dataw: cannot read .o\n"); + unlink(src); unlink(obj); + return -1; + } + + const Ehdr *eh = (const Ehdr *)buf; + int rc = 0; + + /* Expect 7 sections: NULL, .text, .rela.text, .data, .symtab, .strtab, .shstrtab. */ + if (eh->e_shnum != 7) { + fprintf(stderr, "dataw: e_shnum=%u, want 7\n", eh->e_shnum); + rc = -1; + } + + int idx_data = find_section(buf, ".data"); + if (idx_data < 0) { + fprintf(stderr, "dataw: .data section not found\n"); + rc = -1; + goto out; + } + const Shdr *sh = (const Shdr *)(buf + eh->e_shoff); + const Shdr *sd = &sh[idx_data]; + if (sd->sh_type != SHT_PROGBITS) { + fprintf(stderr, "dataw: .data sh_type=%u, want PROGBITS\n", sd->sh_type); + rc = -1; + } + if ((sd->sh_flags & (SHF_ALLOC | SHF_WRITE)) != (SHF_ALLOC | SHF_WRITE)) { + fprintf(stderr, "dataw: .data flags=0x%lx, want ALLOC|WRITE\n", + (unsigned long)sd->sh_flags); + rc = -1; + } + if ((sd->sh_flags & SHF_EXECINSTR) != 0) { + fprintf(stderr, "dataw: .data is executable, want non-X\n"); + rc = -1; + } + if (sd->sh_size != 8) { + fprintf(stderr, "dataw: .data size=%lu, want 8\n", + (unsigned long)sd->sh_size); + rc = -1; + } + + /* Verify the bytes themselves match the LE i64 42 we wrote. */ + const uint8_t *db = buf + sd->sh_offset; + uint8_t want[8] = { 0x2a, 0, 0, 0, 0, 0, 0, 0 }; + if (memcmp(db, want, 8) != 0) { + fprintf(stderr, "dataw: .data contents mismatch\n"); + rc = -1; + } + + const Sym64 *s = find_symbol(buf, "counter"); + if (s == NULL) { + fprintf(stderr, "dataw: symbol 'counter' not in .symtab\n"); + rc = -1; + goto out; + } + if ((int)s->st_shndx != idx_data) { + fprintf(stderr, "dataw: 'counter'.st_shndx=%u, want %d (.data)\n", + s->st_shndx, idx_data); + rc = -1; + } + if (s->st_value != 0) { + fprintf(stderr, "dataw: 'counter'.st_value=%lu, want 0\n", + (unsigned long)s->st_value); + rc = -1; + } + +out: + free(buf); + unlink(src); + unlink(obj); + return rc; +} + +static int +test_no_dataw_keeps_legacy_layout(const char *bin) +{ + char src[64], obj[64]; + snprintf(src, sizeof src, "/tmp/wwt_nodataw_%d.s", getpid()); + snprintf(obj, sizeof obj, "/tmp/wwt_nodataw_%d.o", getpid()); + + /* A .s without any DATAW must still produce the legacy 6-section + * layout. Test 991 (selfhost .o byte-diff) relies on this. */ + const char *body = + "TEXT _start(SB),$0\n" + "\tMOVQ\t$0, AX\n" + "\tRET\n"; + if (write_file(src, body) < 0) { + fprintf(stderr, "nodataw: cannot write %s\n", src); + return -1; + } + + char cmd[512]; + snprintf(cmd, sizeof cmd, "%s/w6a -o %s %s", bin, obj, src); + if (system(cmd) != 0) { + fprintf(stderr, "nodataw: w6a failed\n"); + unlink(src); + return -1; + } + + uint8_t *buf = NULL; + size_t n = 0; + if (slurp(obj, &buf, &n) < 0) { + fprintf(stderr, "nodataw: cannot read .o\n"); + unlink(src); unlink(obj); + return -1; + } + + const Ehdr *eh = (const Ehdr *)buf; + int rc = 0; + if (eh->e_shnum != 6) { + fprintf(stderr, "nodataw: e_shnum=%u, want 6 (legacy)\n", + eh->e_shnum); + rc = -1; + } + if (find_section(buf, ".data") >= 0) { + fprintf(stderr, "nodataw: .data section present, want absent\n"); + rc = -1; + } + + free(buf); + unlink(src); + unlink(obj); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + + int fail = 0; + if (test_dataw_emits_writable_section(bin) != 0) fail++; + if (test_no_dataw_keeps_legacy_layout(bin) != 0) fail++; + + if (fail) { + fprintf(stderr, "dataw: %d/2 subtests failed\n", fail); + return 1; + } + printf("dataw: 2/2 ok\n"); + return 0; +}