w6a: DATAW directive for writable .data section

First step toward top-level mutable `let`. Adds a sibling directive to
DATA whose bytes land in a separate writable .data PROGBITS section
(SHF_ALLOC|SHF_WRITE, STT_OBJECT) instead of .text. The section is
emitted only when DATAW was used, so inputs without it produce a
byte-identical .o — tests 991 (selfhost .o diff) and 995 (self-rebuild)
keep passing unchanged.

w6l still treats data-resident syms as undefined; that's the next step.
This commit is contained in:
2026-05-12 11:35:50 +09:00
parent 922877309b
commit 1b0955c97b
7 changed files with 381 additions and 18 deletions

View File

@@ -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);