authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-23 12:42:04+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-09-23 17:17:20+02:00
logab8a5bfe83848c0d0ceb7ac08fe7f535783e1605
tree4105c2c6a1082a7e819244cb37dcd766b5844abf
parent8960df0c013f83a8adf05ce2ae9a9667d58ef41d

elf: implement markLive for ZigModule


3 files changed, 18 insertions(+), 1 deletions(-)

src/link/Elf.zig+1
...@@ -1493,6 +1493,7 @@ fn resolveSymbols(self: *Elf) error{Overflow}!void {...@@ -1493,6 +1493,7 @@ fn resolveSymbols(self: *Elf) error{Overflow}!void {
1493/// This routine will prune unneeded objects extracted from archives and1493/// This routine will prune unneeded objects extracted from archives and
1494/// unneeded shared objects.1494/// unneeded shared objects.
1495fn markLive(self: *Elf) void {1495fn markLive(self: *Elf) void {
1496 if (self.zig_module_index) |index| self.file(index).?.markLive(self);
1496 for (self.objects.items) |index| {1497 for (self.objects.items) |index| {
1497 const file_ptr = self.file(index).?;1498 const file_ptr = self.file(index).?;
1498 if (file_ptr.isAlive()) file_ptr.markLive(self);1499 if (file_ptr.isAlive()) file_ptr.markLive(self);
src/link/Elf/ZigModule.zig+16
...@@ -155,6 +155,22 @@ pub fn resetGlobals(self: *ZigModule, elf_file: *Elf) void {...@@ -155,6 +155,22 @@ pub fn resetGlobals(self: *ZigModule, elf_file: *Elf) void {
155 }155 }
156}156}
157157
158pub fn markLive(self: *ZigModule, elf_file: *Elf) void {
159 for (self.globals(), 0..) |index, i| {
160 const esym = self.global_esyms.items[i];
161 if (esym.st_bind() == elf.STB_WEAK) continue;
162
163 const global = elf_file.symbol(index);
164 const file = global.file(elf_file) orelse continue;
165 const should_keep = esym.st_shndx == elf.SHN_UNDEF or
166 (esym.st_shndx == elf.SHN_COMMON and global.elfSym(elf_file).st_shndx != elf.SHN_COMMON);
167 if (should_keep and !file.isAlive()) {
168 file.setAlive();
169 file.markLive(elf_file);
170 }
171 }
172}
173
158pub fn updateSymtabSize(self: *ZigModule, elf_file: *Elf) void {174pub fn updateSymtabSize(self: *ZigModule, elf_file: *Elf) void {
159 for (self.locals()) |local_index| {175 for (self.locals()) |local_index| {
160 const local = elf_file.symbol(local_index);176 const local = elf_file.symbol(local_index);
src/link/Elf/file.zig+1-1
...@@ -84,7 +84,7 @@ pub const File = union(enum) {...@@ -84,7 +84,7 @@ pub const File = union(enum) {
8484
85 pub fn markLive(file: File, elf_file: *Elf) void {85 pub fn markLive(file: File, elf_file: *Elf) void {
86 switch (file) {86 switch (file) {
87 .zig_module, .linker_defined => unreachable,87 .linker_defined => unreachable,
88 inline else => |x| x.markLive(elf_file),88 inline else => |x| x.markLive(elf_file),
89 }89 }
90 }90 }