authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-29 18:08:10+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-07-30 10:03:13+02:00
log3618824ea36e22fda19c4adcf0cec3ce3254df4c
tree99416970bf9fc9b87c3c9bc02e4bed02acbacab4
parent5ceac8ebbae5fedc56c74e2b3d4918742089ec45

elf: move entry tracking into LinkerDefined


3 files changed, 20 insertions(+), 19 deletions(-)

src/link/Elf.zig+8-16
......@@ -93,7 +93,6 @@ phdr_gnu_stack_index: ?u16 = null,
9393/// TODO I think ELF permits multiple TLS segments but for now, assume one per file.
9494phdr_tls_index: ?u16 = null,
9595
96entry_index: ?Symbol.Index = null,
9796page_size: u32,
9897default_sym_version: elf.Elf64_Versym,
9998
......@@ -1277,19 +1276,15 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
12771276 // Any qualifing unresolved symbol will be upgraded to an absolute, weak
12781277 // symbol for potential resolution at load-time.
12791278 try self.resolveSymbols();
1279 if (self.linkerDefinedPtr()) |obj| {
1280 try obj.initSymbols(self);
1281 }
12801282 self.markEhFrameAtomsDead();
12811283 try self.resolveMergeSections();
12821284
12831285 try self.convertCommonSymbols();
12841286 self.markImportsExports();
12851287
1286 // Look for entry address in objects if not set by the incremental compiler.
1287 if (self.entry_index == null) {
1288 if (self.entry_name) |name| {
1289 self.entry_index = self.globalByName(name);
1290 }
1291 }
1292
12931288 if (self.base.gc_sections) {
12941289 try gc.gcAtoms(self);
12951290
......@@ -1307,9 +1302,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
13071302 try self.finalizeMergeSections();
13081303 try self.initOutputSections();
13091304 try self.initMergeSections();
1310 if (self.linkerDefinedPtr()) |obj| {
1311 try obj.initSymbols(self);
1312 }
13131305 self.claimUnresolved();
13141306
13151307 // Scan and create missing synthetic entries such as GOT indirection.
......@@ -1385,7 +1377,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
13851377 else => |e| return e,
13861378 };
13871379
1388 if (self.entry_index == null and self.base.isExe()) {
1380 if (self.base.isExe() and self.linkerDefinedPtr().?.entry_index == null) {
13891381 log.debug("flushing. no_entry_point_found = true", .{});
13901382 comp.link_error_flags.no_entry_point_found = true;
13911383 } else {
......@@ -2917,10 +2909,10 @@ pub fn writeElfHeader(self: *Elf) !void {
29172909 mem.writeInt(u32, hdr_buf[index..][0..4], 1, endian);
29182910 index += 4;
29192911
2920 const e_entry = if (self.entry_index) |entry_index|
2921 @as(u64, @intCast(self.symbol(entry_index).address(.{}, self)))
2922 else
2923 0;
2912 const e_entry: u64 = if (self.linkerDefinedPtr()) |obj| blk: {
2913 const entry_index = obj.entry_index orelse break :blk 0;
2914 break :blk @intCast(self.symbol(entry_index).address(.{}, self));
2915 } else 0;
29242916 const phdr_table_offset = if (self.phdr_table_index) |phndx| self.phdrs.items[phndx].p_offset else 0;
29252917 switch (self.ptr_width) {
29262918 .p32 => {
src/link/Elf/LinkerDefined.zig+7
......@@ -4,6 +4,7 @@ symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
44strtab: std.ArrayListUnmanaged(u8) = .{},
55symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
66
7entry_index: ?Symbol.Index = null,
78dynamic_index: ?Symbol.Index = null,
89ehdr_start_index: ?Symbol.Index = null,
910init_array_start_index: ?Symbol.Index = null,
......@@ -39,6 +40,12 @@ pub fn init(self: *LinkerDefined, allocator: Allocator) !void {
3940pub fn initSymbols(self: *LinkerDefined, elf_file: *Elf) !void {
4041 const gpa = elf_file.base.comp.gpa;
4142
43 // Look for entry address in objects if not set by the incremental compiler.
44 if (self.entry_index == null) {
45 if (elf_file.entry_name) |name| {
46 self.entry_index = elf_file.globalByName(name);
47 }
48 }
4249 self.dynamic_index = try self.addGlobal("_DYNAMIC", elf_file);
4350 self.ehdr_start_index = try self.addGlobal("__ehdr_start", elf_file);
4451 self.init_array_start_index = try self.addGlobal("__init_array_start", elf_file);
src/link/Elf/gc.zig+5-3
......@@ -16,9 +16,11 @@ pub fn gcAtoms(elf_file: *Elf) !void {
1616}
1717
1818fn collectRoots(roots: *std.ArrayList(*Atom), files: []const File.Index, elf_file: *Elf) !void {
19 if (elf_file.entry_index) |index| {
20 const global = elf_file.symbol(index);
21 try markSymbol(global, roots, elf_file);
19 if (elf_file.linkerDefinedPtr()) |obj| {
20 if (obj.entry_index) |index| {
21 const global = elf_file.symbol(index);
22 try markSymbol(global, roots, elf_file);
23 }
2224 }
2325
2426 for (files) |index| {