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

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

View File

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

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

View File

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