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,...@@ -93,7 +93,6 @@ phdr_gnu_stack_index: ?u16 = null,
93/// TODO I think ELF permits multiple TLS segments but for now, assume one per file.93/// TODO I think ELF permits multiple TLS segments but for now, assume one per file.
94phdr_tls_index: ?u16 = null,94phdr_tls_index: ?u16 = null,
9595
96entry_index: ?Symbol.Index = null,
97page_size: u32,96page_size: u32,
98default_sym_version: elf.Elf64_Versym,97default_sym_version: elf.Elf64_Versym,
9998
...@@ -1277,19 +1276,15 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1277,19 +1276,15 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1277 // Any qualifing unresolved symbol will be upgraded to an absolute, weak1276 // Any qualifing unresolved symbol will be upgraded to an absolute, weak
1278 // symbol for potential resolution at load-time.1277 // symbol for potential resolution at load-time.
1279 try self.resolveSymbols();1278 try self.resolveSymbols();
1279 if (self.linkerDefinedPtr()) |obj| {
1280 try obj.initSymbols(self);
1281 }
1280 self.markEhFrameAtomsDead();1282 self.markEhFrameAtomsDead();
1281 try self.resolveMergeSections();1283 try self.resolveMergeSections();
12821284
1283 try self.convertCommonSymbols();1285 try self.convertCommonSymbols();
1284 self.markImportsExports();1286 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
1293 if (self.base.gc_sections) {1288 if (self.base.gc_sections) {
1294 try gc.gcAtoms(self);1289 try gc.gcAtoms(self);
12951290
...@@ -1307,9 +1302,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod...@@ -1307,9 +1302,6 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1307 try self.finalizeMergeSections();1302 try self.finalizeMergeSections();
1308 try self.initOutputSections();1303 try self.initOutputSections();
1309 try self.initMergeSections();1304 try self.initMergeSections();
1310 if (self.linkerDefinedPtr()) |obj| {
1311 try obj.initSymbols(self);
1312 }
1313 self.claimUnresolved();1305 self.claimUnresolved();
13141306
1315 // Scan and create missing synthetic entries such as GOT indirection.1307 // 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...@@ -1385,7 +1377,7 @@ pub fn flushModule(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_nod
1385 else => |e| return e,1377 else => |e| return e,
1386 };1378 };
13871379
1388 if (self.entry_index == null and self.base.isExe()) {1380 if (self.base.isExe() and self.linkerDefinedPtr().?.entry_index == null) {
1389 log.debug("flushing. no_entry_point_found = true", .{});1381 log.debug("flushing. no_entry_point_found = true", .{});
1390 comp.link_error_flags.no_entry_point_found = true;1382 comp.link_error_flags.no_entry_point_found = true;
1391 } else {1383 } else {
...@@ -2917,10 +2909,10 @@ pub fn writeElfHeader(self: *Elf) !void {...@@ -2917,10 +2909,10 @@ pub fn writeElfHeader(self: *Elf) !void {
2917 mem.writeInt(u32, hdr_buf[index..][0..4], 1, endian);2909 mem.writeInt(u32, hdr_buf[index..][0..4], 1, endian);
2918 index += 4;2910 index += 4;
29192911
2920 const e_entry = if (self.entry_index) |entry_index|2912 const e_entry: u64 = if (self.linkerDefinedPtr()) |obj| blk: {
2921 @as(u64, @intCast(self.symbol(entry_index).address(.{}, self)))2913 const entry_index = obj.entry_index orelse break :blk 0;
2922 else2914 break :blk @intCast(self.symbol(entry_index).address(.{}, self));
2923 0;2915 } else 0;
2924 const phdr_table_offset = if (self.phdr_table_index) |phndx| self.phdrs.items[phndx].p_offset else 0;2916 const phdr_table_offset = if (self.phdr_table_index) |phndx| self.phdrs.items[phndx].p_offset else 0;
2925 switch (self.ptr_width) {2917 switch (self.ptr_width) {
2926 .p32 => {2918 .p32 => {
src/link/Elf/LinkerDefined.zig+7
...@@ -4,6 +4,7 @@ symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},...@@ -4,6 +4,7 @@ symtab: std.ArrayListUnmanaged(elf.Elf64_Sym) = .{},
4strtab: std.ArrayListUnmanaged(u8) = .{},4strtab: std.ArrayListUnmanaged(u8) = .{},
5symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},5symbols: std.ArrayListUnmanaged(Symbol.Index) = .{},
66
7entry_index: ?Symbol.Index = null,
7dynamic_index: ?Symbol.Index = null,8dynamic_index: ?Symbol.Index = null,
8ehdr_start_index: ?Symbol.Index = null,9ehdr_start_index: ?Symbol.Index = null,
9init_array_start_index: ?Symbol.Index = null,10init_array_start_index: ?Symbol.Index = null,
...@@ -39,6 +40,12 @@ pub fn init(self: *LinkerDefined, allocator: Allocator) !void {...@@ -39,6 +40,12 @@ pub fn init(self: *LinkerDefined, allocator: Allocator) !void {
39pub fn initSymbols(self: *LinkerDefined, elf_file: *Elf) !void {40pub fn initSymbols(self: *LinkerDefined, elf_file: *Elf) !void {
40 const gpa = elf_file.base.comp.gpa;41 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 }
42 self.dynamic_index = try self.addGlobal("_DYNAMIC", elf_file);49 self.dynamic_index = try self.addGlobal("_DYNAMIC", elf_file);
43 self.ehdr_start_index = try self.addGlobal("__ehdr_start", elf_file);50 self.ehdr_start_index = try self.addGlobal("__ehdr_start", elf_file);
44 self.init_array_start_index = try self.addGlobal("__init_array_start", elf_file);51 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 {...@@ -16,9 +16,11 @@ pub fn gcAtoms(elf_file: *Elf) !void {
16}16}
1717
18fn collectRoots(roots: *std.ArrayList(*Atom), files: []const File.Index, elf_file: *Elf) !void {18fn collectRoots(roots: *std.ArrayList(*Atom), files: []const File.Index, elf_file: *Elf) !void {
19 if (elf_file.entry_index) |index| {19 if (elf_file.linkerDefinedPtr()) |obj| {
20 const global = elf_file.symbol(index);20 if (obj.entry_index) |index| {
21 try markSymbol(global, roots, elf_file);21 const global = elf_file.symbol(index);
22 try markSymbol(global, roots, elf_file);
23 }
22 }24 }
2325
24 for (files) |index| {26 for (files) |index| {