w6a+w6l: DATAR directive for absolute-address relocs in .data
Unblock literal initialisers for str/slice/struct globals by wiring
an R_X86_64_64 relocation kind through both assembler and static
linker.
w6a:
- new A_DATAR directive, syntax `DATAR slot+off(SB),target(SB)`,
records an R_X86_64_64 reloc at slot+off in .data pointing at
target. The slot must be pre-defined by a prior DATAW;
- parse_operand learned the `name+disp(SB)` shape so the slot's
byte offset can be addressed explicitly;
- Areloc carries a `section` flag (0=.text / 1=.data) and obj.c
splits the reloc list into .rela.text and .rela.data, emitting
the latter conditionally with sh_info pointing at .data.
w6l:
- Lrel grows the same `section` flag; obj.c loads `.rela.data`
sections into the global reloc list with offsets shifted by
each input's data_off;
- pass.c handles R_X86_64_64: target VA is data_va+sym.val for
in_data symbols (else text_va+sym.val), addend is added, and
the 8-byte slot is patched in l->data (or l->text).
Inputs without DATAR are unaffected — bootstrap, 991 (selfhost .o
diff) and 992 (selfhost exe diff) keep their byte-identical
output. 520_datar covers the new path: asm a DATAW+DATAR pair,
verify .rela.data has exactly one R_X86_64_64 entry, link, run,
confirm the relocated pointer feeds a 5-byte write that prints
"hello".
Selfhost mirror + w6c emission for str/slice/struct literal init
land in follow-ups.
This commit is contained in:
@@ -111,27 +111,42 @@ typedef struct {
|
||||
int
|
||||
a_emit_elf(Asm *a, FILE *f)
|
||||
{
|
||||
Buf shstr = {0}, str = {0}, sym = {0}, rela = {0};
|
||||
Buf shstr = {0}, str = {0}, sym = {0}, rela = {0}, relad = {0};
|
||||
stput(&shstr, ""); /* idx 0 = empty */
|
||||
stput(&str, "");
|
||||
|
||||
const int has_data = (a->datalen > 0);
|
||||
int has_data_relocs = 0;
|
||||
for (Areloc *r = a->relocs; r; r = r->next) {
|
||||
if (r->section == 1) { has_data_relocs = 1; break; }
|
||||
}
|
||||
|
||||
/* 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
|
||||
* Without data, without data-relocs:
|
||||
* 1=.text, 2=.rela.text, 3=.symtab, 4=.strtab, 5=.shstrtab
|
||||
* With data, no data-relocs:
|
||||
* 1=.text, 2=.rela.text, 3=.data, 4=.symtab, 5=.strtab, 6=.shstrtab
|
||||
* With data + data-relocs:
|
||||
* 1=.text, 2=.rela.text, 3=.data, 4=.rela.data, 5=.symtab,
|
||||
* 6=.strtab, 7=.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;
|
||||
const u16 SH_RELAD = (has_data && has_data_relocs) ? 4 : 0;
|
||||
const u16 SH_SYMTAB = has_data
|
||||
? (has_data_relocs ? 5 : 4)
|
||||
: 3;
|
||||
const u16 SH_STRTAB = (u16)(SH_SYMTAB + 1);
|
||||
const u16 SH_SHSTR = (u16)(SH_STRTAB + 1);
|
||||
|
||||
/* Section name offsets. Append .data's name only when used so the
|
||||
* .shstrtab buffer stays byte-identical for the no-DATAW case. */
|
||||
* .shstrtab buffer stays byte-identical for the no-DATAW case.
|
||||
* Same for .rela.data — only present when data relocs exist. */
|
||||
u32 shn_text = stput(&shstr, ".text");
|
||||
u32 shn_rela = stput(&shstr, ".rela.text");
|
||||
u32 shn_data = has_data ? stput(&shstr, ".data") : 0;
|
||||
u32 shn_relad = (has_data && has_data_relocs)
|
||||
? stput(&shstr, ".rela.data") : 0;
|
||||
u32 shn_symtab = stput(&shstr, ".symtab");
|
||||
u32 shn_strtab = stput(&shstr, ".strtab");
|
||||
u32 shn_shstrtab = stput(&shstr, ".shstrtab");
|
||||
@@ -168,13 +183,14 @@ a_emit_elf(Asm *a, FILE *f)
|
||||
s->idx = idx++;
|
||||
}
|
||||
|
||||
/* Build relocations */
|
||||
/* Build relocations — split into two buffers so we can emit
|
||||
* .rela.text and (conditionally) .rela.data separately. */
|
||||
for (Areloc *r = a->relocs; r; r = r->next) {
|
||||
Rela64 re;
|
||||
re.r_offset = r->off;
|
||||
re.r_info = ELF64_R_INFO((u64)r->sym->idx, (u64)r->kind);
|
||||
re.r_addend = r->addend;
|
||||
bput(&rela, &re, sizeof re);
|
||||
bput(r->section == 1 ? &relad : &rela, &re, sizeof re);
|
||||
}
|
||||
|
||||
/* Layout offsets in the file */
|
||||
@@ -182,13 +198,16 @@ a_emit_elf(Asm *a, FILE *f)
|
||||
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_relad = off; if (has_data && has_data_relocs) off += relad.n;
|
||||
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 = has_data ? 7 : 6; /* null + reals */
|
||||
const int NSECT = has_data
|
||||
? (has_data_relocs ? 8 : 7)
|
||||
: 6; /* null + reals */
|
||||
|
||||
Ehdr eh = {0};
|
||||
memcpy(eh.e_ident, "\x7f""ELF", 4);
|
||||
@@ -208,6 +227,7 @@ a_emit_elf(Asm *a, FILE *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);
|
||||
if (has_data && has_data_relocs) fwrite(relad.p, 1, relad.n, f);
|
||||
fwrite(sym.p, 1, sym.n, f);
|
||||
fwrite(str.p, 1, str.n, f);
|
||||
fwrite(shstr.p, 1, shstr.n, f);
|
||||
@@ -249,6 +269,20 @@ a_emit_elf(Asm *a, FILE *f)
|
||||
fwrite(&sh, 1, sizeof sh, f);
|
||||
}
|
||||
|
||||
if (has_data && has_data_relocs) {
|
||||
memset(&sh, 0, sizeof sh);
|
||||
sh.sh_name = shn_relad;
|
||||
sh.sh_type = SHT_RELA;
|
||||
sh.sh_flags = SHF_INFO_LINK;
|
||||
sh.sh_offset = off_relad;
|
||||
sh.sh_size = relad.n;
|
||||
sh.sh_link = SH_SYMTAB;
|
||||
sh.sh_info = SH_DATA; /* applies to .data */
|
||||
sh.sh_addralign = 8;
|
||||
sh.sh_entsize = sizeof(Rela64);
|
||||
fwrite(&sh, 1, sizeof sh, f);
|
||||
}
|
||||
|
||||
memset(&sh, 0, sizeof sh);
|
||||
sh.sh_name = shn_symtab;
|
||||
sh.sh_type = SHT_SYMTAB;
|
||||
@@ -276,6 +310,6 @@ a_emit_elf(Asm *a, FILE *f)
|
||||
sh.sh_addralign = 1;
|
||||
fwrite(&sh, 1, sizeof sh, f);
|
||||
|
||||
free(shstr.p); free(str.p); free(sym.p); free(rela.p);
|
||||
free(shstr.p); free(str.p); free(sym.p); free(rela.p); free(relad.p);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user