authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-13 21:32:54+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:05+02:00
log85d451f96cd8f2854fa6a092625b8a2f3c474a73
tree0c001659b2021fe860cb982f137aacf271a5e668
parent7be983ac9217a596b7f35e7ef4c49fda0270e10b

elf: re-enable self-hosted backends


4 files changed, 110 insertions(+), 21 deletions(-)

src/link/Elf.zig+62-6
......@@ -762,6 +762,7 @@ pub fn initMetadata(self: *Elf) !void {
762762 .name = ".zig.got",
763763 .phdr_index = self.phdr_zig_got_index.?,
764764 .alignment = ptr_size,
765 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
765766 });
766767 }
767768
......@@ -785,7 +786,7 @@ pub fn initMetadata(self: *Elf) !void {
785786
786787 if (self.zig_bss_section_index == null) {
787788 self.zig_bss_section_index = try self.allocateAllocSection(.{
788 .name = ".bss.zig",
789 .name = ".zig.bss",
789790 .phdr_index = self.phdr_zig_load_zerofill_index.?,
790791 .alignment = ptr_size,
791792 .flags = elf.SHF_ALLOC | elf.SHF_WRITE,
......@@ -1558,7 +1559,8 @@ pub fn flushModule(self: *Elf, comp: *Compilation, prog_node: *std.Progress.Node
15581559 for (zig_module.atoms.items) |atom_index| {
15591560 const atom_ptr = self.atom(atom_index) orelse continue;
15601561 if (!atom_ptr.flags.alive) continue;
1561 const shdr = &self.shdrs.items[atom_ptr.outputShndx().?];
1562 const out_shndx = atom_ptr.outputShndx() orelse continue;
1563 const shdr = &self.shdrs.items[out_shndx];
15621564 if (shdr.sh_type == elf.SHT_NOBITS) continue;
15631565 const code = try zig_module.codeAlloc(self, atom_index);
15641566 defer gpa.free(code);
......@@ -3744,6 +3746,9 @@ fn initSections(self: *Elf) !void {
37443746 const needs_rela_dyn = blk: {
37453747 if (self.got.flags.needs_rela or self.got.flags.needs_tlsld or
37463748 self.copy_rel.symbols.items.len > 0) break :blk true;
3749 if (self.zig_module_index) |index| {
3750 if (self.file(index).?.zig_module.num_dynrelocs > 0) break :blk true;
3751 }
37473752 for (self.objects.items) |index| {
37483753 if (self.file(index).?.object.num_dynrelocs > 0) break :blk true;
37493754 }
......@@ -4243,6 +4248,33 @@ fn sortSections(self: *Elf) !void {
42434248 shdr.sh_link = self.dynsymtab_section_index.?;
42444249 shdr.sh_info = self.plt_section_index.?;
42454250 }
4251
4252 if (self.zig_module_index) |index| {
4253 const zig_module = self.file(index).?.zig_module;
4254 for (zig_module.atoms.items) |atom_index| {
4255 const atom_ptr = self.atom(atom_index) orelse continue;
4256 if (!atom_ptr.flags.alive) continue;
4257 const out_shndx = atom_ptr.outputShndx() orelse continue;
4258 atom_ptr.output_section_index = backlinks[out_shndx];
4259 }
4260
4261 for (zig_module.locals()) |local_index| {
4262 const local = self.symbol(local_index);
4263 const atom_ptr = local.atom(self) orelse continue;
4264 if (!atom_ptr.flags.alive) continue;
4265 const out_shndx = local.outputShndx() orelse continue;
4266 local.output_section_index = backlinks[out_shndx];
4267 }
4268
4269 for (zig_module.globals()) |global_index| {
4270 const global = self.symbol(global_index);
4271 const atom_ptr = global.atom(self) orelse continue;
4272 if (!atom_ptr.flags.alive) continue;
4273 if (global.file(self).?.index() != index) continue;
4274 const out_shndx = global.outputShndx() orelse continue;
4275 global.output_section_index = backlinks[out_shndx];
4276 }
4277 }
42464278}
42474279
42484280fn updateSectionSizes(self: *Elf) !void {
......@@ -4286,6 +4318,9 @@ fn updateSectionSizes(self: *Elf) !void {
42864318
42874319 if (self.rela_dyn_section_index) |shndx| {
42884320 var num = self.got.numRela(self) + self.copy_rel.numRela();
4321 if (self.zig_module_index) |index| {
4322 num += self.file(index).?.zig_module.num_dynrelocs;
4323 }
42894324 for (self.objects.items) |index| {
42904325 num += self.file(index).?.object.num_dynrelocs;
42914326 }
......@@ -4373,9 +4408,10 @@ fn shdrToPhdrFlags(sh_flags: u64) u32 {
43734408fn calcNumberOfSegments(self: *Elf) usize {
43744409 var count: usize = 0;
43754410 var flags: u64 = 0;
4376 for (self.shdrs.items) |shdr| {
4411 for (self.shdrs.items, 0..) |shdr, shndx| {
43774412 if (shdr.sh_type == elf.SHT_NULL) continue;
43784413 if (shdr.sh_flags & elf.SHF_ALLOC == 0) continue;
4414 if (self.isZigSection(@intCast(shndx))) continue;
43794415 if (flags != shdrToPhdrFlags(shdr.sh_flags)) count += 1;
43804416 flags = shdrToPhdrFlags(shdr.sh_flags);
43814417 }
......@@ -4474,7 +4510,10 @@ fn allocateAllocSections(self: *Elf) error{OutOfMemory}!void {
44744510 }
44754511 }
44764512 }
4477 covers[nphdrs - 1].len = shndx - covers[nphdrs - 1].start;
4513
4514 if (nphdrs > 0) {
4515 covers[nphdrs - 1].len = shndx - covers[nphdrs - 1].start;
4516 }
44784517
44794518 // Now we can proceed with allocating the sections in virtual memory.
44804519 // As the base address we take the end address of the PHDR table.
......@@ -4647,6 +4686,7 @@ fn writeAtoms(self: *Elf) !void {
46474686 undefs.deinit();
46484687 }
46494688
4689 // TODO iterate over `output_sections` directly
46504690 for (self.shdrs.items, 0..) |shdr, shndx| {
46514691 if (shdr.sh_type == elf.SHT_NULL) continue;
46524692 if (shdr.sh_type == elf.SHT_NOBITS) continue;
......@@ -5830,7 +5870,10 @@ fn fmtDumpState(
58305870 if (self.zig_module_index) |index| {
58315871 const zig_module = self.file(index).?.zig_module;
58325872 try writer.print("zig_module({d}) : {s}\n", .{ index, zig_module.path });
5833 try writer.print("{}\n", .{zig_module.fmtSymtab(self)});
5873 try writer.print("{}{}\n", .{
5874 zig_module.fmtAtoms(self),
5875 zig_module.fmtSymtab(self),
5876 });
58345877 }
58355878
58365879 for (self.objects.items) |index| {
......@@ -5872,7 +5915,7 @@ fn fmtDumpState(
58725915 self.fmtShdr(shdr),
58735916 });
58745917 }
5875 try writer.writeAll("Output phdrs\n");
5918 try writer.writeAll("\nOutput phdrs\n");
58765919 for (self.phdrs.items, 0..) |phdr, phndx| {
58775920 try writer.print("phdr{d} : {}\n", .{ phndx, self.fmtPhdr(phdr) });
58785921 }
......@@ -5983,6 +6026,19 @@ pub const null_sym = elf.Elf64_Sym{
59836026 .st_size = 0,
59846027};
59856028
6029pub const null_shdr = elf.Elf64_Shdr{
6030 .sh_name = 0,
6031 .sh_type = 0,
6032 .sh_flags = 0,
6033 .sh_addr = 0,
6034 .sh_offset = 0,
6035 .sh_size = 0,
6036 .sh_link = 0,
6037 .sh_info = 0,
6038 .sh_addralign = 0,
6039 .sh_entsize = 0,
6040};
6041
59866042const SystemLib = struct {
59876043 needed: bool = false,
59886044 path: []const u8,
src/link/Elf/Atom.zig+21-13
......@@ -50,8 +50,11 @@ pub fn file(self: Atom, elf_file: *Elf) ?File {
5050}
5151
5252pub fn inputShdr(self: Atom, elf_file: *Elf) Object.ElfShdr {
53 const object = self.file(elf_file).?.object;
54 return object.shdrs.items[self.input_section_index];
53 return switch (self.file(elf_file).?) {
54 .object => |x| x.shdrs.items[self.input_section_index],
55 .zig_module => |x| x.inputShdr(self.atom_index, elf_file),
56 else => unreachable,
57 };
5558}
5659
5760pub fn outputShndx(self: Atom) ?u16 {
......@@ -199,7 +202,7 @@ pub fn allocate(self: *Atom, elf_file: *Elf) !void {
199202 _ = free_list.swapRemove(i);
200203 }
201204
202 self.flags.allocated = true;
205 self.flags.alive = true;
203206}
204207
205208pub fn shrink(self: *Atom, elf_file: *Elf) void {
......@@ -471,7 +474,11 @@ fn scanReloc(
471474 elf_file: *Elf,
472475) error{OutOfMemory}!void {
473476 const is_writeable = self.inputShdr(elf_file).sh_flags & elf.SHF_WRITE != 0;
474 const object = self.file(elf_file).?.object;
477 const num_dynrelocs = switch (self.file(elf_file).?) {
478 .linker_defined => unreachable,
479 .shared_object => unreachable,
480 inline else => |x| &x.num_dynrelocs,
481 };
475482
476483 switch (action) {
477484 .none => {},
......@@ -500,7 +507,7 @@ fn scanReloc(
500507 try self.reportTextRelocError(symbol, rel, elf_file);
501508 }
502509 }
503 object.num_dynrelocs += 1;
510 num_dynrelocs.* += 1;
504511 } else {
505512 symbol.flags.needs_copy_rel = true;
506513 }
......@@ -517,7 +524,7 @@ fn scanReloc(
517524
518525 .dyn_cplt => {
519526 if (is_writeable) {
520 object.num_dynrelocs += 1;
527 num_dynrelocs.* += 1;
521528 } else {
522529 symbol.flags.needs_plt = true;
523530 symbol.flags.is_canonical = true;
......@@ -532,7 +539,7 @@ fn scanReloc(
532539 try self.reportTextRelocError(symbol, rel, elf_file);
533540 }
534541 }
535 object.num_dynrelocs += 1;
542 num_dynrelocs.* += 1;
536543
537544 if (action == .ifunc) elf_file.num_ifunc_dynrelocs += 1;
538545 },
......@@ -898,9 +905,13 @@ fn resolveDynAbsReloc(
898905 const A = rel.r_addend;
899906 const S = @as(i64, @intCast(target.address(.{}, elf_file)));
900907 const is_writeable = self.inputShdr(elf_file).sh_flags & elf.SHF_WRITE != 0;
901 const object = self.file(elf_file).?.object;
902908
903 try elf_file.rela_dyn.ensureUnusedCapacity(elf_file.base.allocator, object.num_dynrelocs);
909 const num_dynrelocs = switch (self.file(elf_file).?) {
910 .linker_defined => unreachable,
911 .shared_object => unreachable,
912 inline else => |x| x.num_dynrelocs,
913 };
914 try elf_file.rela_dyn.ensureUnusedCapacity(elf_file.base.allocator, num_dynrelocs);
904915
905916 switch (action) {
906917 .@"error",
......@@ -1165,7 +1176,7 @@ fn format2(
11651176 _ = unused_fmt_string;
11661177 const atom = ctx.atom;
11671178 const elf_file = ctx.elf_file;
1168 try writer.print("atom({d}) : {s} : @{x} : sect({d}) : align({x}) : size({x})", .{
1179 try writer.print("atom({d}) : {s} : @{x} : shdr({d}) : align({x}) : size({x})", .{
11691180 atom.atom_index, atom.name(elf_file), atom.value,
11701181 atom.output_section_index, atom.alignment, atom.size,
11711182 });
......@@ -1191,9 +1202,6 @@ pub const Flags = packed struct {
11911202
11921203 /// Specifies if the atom has been visited during garbage collection.
11931204 visited: bool = false,
1194
1195 /// Specifies whether this atom has been allocated in the output section.
1196 allocated: bool = false,
11971205};
11981206
11991207const x86_64 = struct {
src/link/Elf/Symbol.zig+1-1
......@@ -314,7 +314,7 @@ fn format2(
314314 try writer.writeAll(" : absolute");
315315 }
316316 } else if (symbol.outputShndx()) |shndx| {
317 try writer.print(" : sect({d})", .{shndx});
317 try writer.print(" : shdr({d})", .{shndx});
318318 }
319319 if (symbol.atom(ctx.elf_file)) |atom_ptr| {
320320 try writer.print(" : atom({d})", .{atom_ptr.atom_index});
src/link/Elf/ZigModule.zig+26-1
......@@ -16,6 +16,8 @@ globals_lookup: std.AutoHashMapUnmanaged(u32, Symbol.Index) = .{},
1616atoms: std.ArrayListUnmanaged(Atom.Index) = .{},
1717relocs: std.ArrayListUnmanaged(std.ArrayListUnmanaged(elf.Elf64_Rela)) = .{},
1818
19num_dynrelocs: u32 = 0,
20
1921output_symtab_size: Elf.SymtabSize = .{},
2022
2123pub fn deinit(self: *ZigModule, allocator: Allocator) void {
......@@ -79,6 +81,22 @@ pub fn addAtom(self: *ZigModule, elf_file: *Elf) !Symbol.Index {
7981 return symbol_index;
8082}
8183
84/// TODO actually create fake input shdrs and return that instead.
85pub fn inputShdr(self: ZigModule, atom_index: Atom.Index, elf_file: *Elf) Object.ElfShdr {
86 _ = self;
87 const shdr = shdr: {
88 const atom = elf_file.atom(atom_index) orelse break :shdr Elf.null_shdr;
89 const shndx = atom.outputShndx() orelse break :shdr Elf.null_shdr;
90 var shdr = elf_file.shdrs.items[shndx];
91 shdr.sh_addr = 0;
92 shdr.sh_offset = 0;
93 shdr.sh_size = atom.size;
94 shdr.sh_addralign = atom.alignment.toByteUnits(1);
95 break :shdr shdr;
96 };
97 return Object.ElfShdr.fromElf64Shdr(shdr) catch unreachable;
98}
99
82100pub fn resolveSymbols(self: *ZigModule, elf_file: *Elf) void {
83101 for (self.globals(), 0..) |index, i| {
84102 const esym_index = @as(Symbol.Index, @intCast(i)) | 0x10000000;
......@@ -145,6 +163,8 @@ pub fn scanRelocs(self: *ZigModule, elf_file: *Elf, undefs: anytype) !void {
145163 for (self.atoms.items) |atom_index| {
146164 const atom = elf_file.atom(atom_index) orelse continue;
147165 if (!atom.flags.alive) continue;
166 const shdr = atom.inputShdr(elf_file);
167 if (shdr.sh_type == elf.SHT_NOBITS) continue;
148168 if (atom.scanRelocsRequiresCode(elf_file)) {
149169 // TODO ideally we don't have to fetch the code here.
150170 // Perhaps it would make sense to save the code until flushModule where we
......@@ -273,7 +293,10 @@ pub fn codeAlloc(self: ZigModule, elf_file: *Elf, atom_index: Atom.Index) ![]u8
273293 const code = try gpa.alloc(u8, size);
274294 errdefer gpa.free(code);
275295 const amt = try elf_file.base.file.?.preadAll(code, file_offset);
276 if (amt != code.len) return error.InputOutput;
296 if (amt != code.len) {
297 log.err("fetching code for {s} failed", .{atom.name(elf_file)});
298 return error.InputOutput;
299 }
277300 return code;
278301}
279302
......@@ -334,11 +357,13 @@ fn formatAtoms(
334357const assert = std.debug.assert;
335358const std = @import("std");
336359const elf = std.elf;
360const log = std.log.scoped(.link);
337361
338362const Allocator = std.mem.Allocator;
339363const Atom = @import("Atom.zig");
340364const Elf = @import("../Elf.zig");
341365const File = @import("file.zig").File;
342366const Module = @import("../../Module.zig");
367const Object = @import("Object.zig");
343368const Symbol = @import("Symbol.zig");
344369const ZigModule = @This();