authorgravatar for loic@gregoire.devLoïc Grégoire <loic@gregoire.dev> 2026-10-12 14:29:40-04:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2026-10-16 12:41:30+01:00
log6c1a5a1e5b12e775552e1e3ff52daa6576789487
tree019523796ece7d65c76abb8ec0e52c5ada41768f
parent626381706712eb593bcf0c843dd2275d71cf41da
signature Commit is signed but in an unrecognized format.

Elf2: allow custom NOBITS sections in objects

Elf2 previously was failing on objects with sections such as '.bss.something', now preserve those sections for ET_REL output and merge them into their respective .bss/.tbss for other output types.

1 files changed, 17 insertions(+), 17 deletions(-)

src/link/Elf2.zig+17-17
......@@ -7090,7 +7090,7 @@ fn mapInputSection(elf: *Elf, opts: struct {
70907090 .type = switch (opts.type) {
70917091 .PROGBITS => .NULL, // size is 0
70927092 .X86_64_UNWIND => .X86_64_UNWIND,
7093 .NOBITS => return error.UnsupportedNobitsSection,
7093 .NOBITS => if (elf.ehdrType() == .REL) .NOBITS else return error.UnsupportedNobitsSection,
70947094 else => unreachable,
70957095 },
70967096 .flags = flags: {
......@@ -7568,10 +7568,8 @@ fn loadObject(
75687568 shdr: ElfN.Shdr,
75697569 placement: union(enum) {
75707570 progbits: InputSection.Index,
7571 bss: struct {
7572 offset: u64,
7573 },
7574 tbss: struct {
7571 nobits: struct {
7572 shndx: Section.Index,
75757573 offset: u64,
75767574 },
75777575 none,
......@@ -7743,14 +7741,16 @@ fn loadObject(
77437741 },
77447742 };
77457743
7744 section.placement = .{ .nobits = .{
7745 .shndx = opts.shndx,
7746 .offset = offset,
7747 } };
77467748 if (opts.shndx == elf.shndx.bss) {
7747 section.placement = .{ .bss = .{ .offset = offset } };
77487749 input_has_bss = true;
77497750 } else if (opts.shndx == elf.shndx.tbss) {
7750 section.placement = .{ .tbss = .{ .offset = offset } };
77517751 input_has_tbss = true;
77527752 } else {
7753 unreachable;
7753 assert(elf.ehdrType() == .REL);
77547754 }
77557755
77567756 continue;
......@@ -7882,15 +7882,15 @@ fn loadObject(
78827882 elf.getNodeShndx(isi.node(elf)),
78837883 input_sym.value,
78847884 },
7885 .bss => |bss| .{
7886 .none,
7887 elf.shndx.bss,
7888 elf.shndx.bss.vaddr(elf) + bss.offset + input_sym.value,
7889 },
7890 .tbss => |tbss| .{
7885 .nobits => |nobits| .{
78917886 .none,
7892 elf.shndx.tbss,
7893 elf.tbssBaseOffset() + tbss.offset + input_sym.value,
7887 nobits.shndx,
7888 (if (elf.ehdrType() == .REL)
7889 @as(u64, 0)
7890 else if (nobits.shndx.flags(elf).TLS)
7891 elf.tbssBaseOffset()
7892 else
7893 nobits.shndx.vaddr(elf)) + nobits.offset + input_sym.value,
78947894 },
78957895 .none => unreachable,
78967896 };
......@@ -7960,7 +7960,7 @@ fn loadObject(
79607960 const loc_node = switch (loc_sec.placement) {
79617961 .none => continue,
79627962 .progbits => |isi| isi.node(elf),
7963 .bss, .tbss => return diags.failParse(
7963 .nobits => return diags.failParse(
79647964 path,
79657965 "relocations applying to BSS (SHT_NOBITS) sections are unsupported",
79667966 .{},