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 {
14931493/// This routine will prune unneeded objects extracted from archives and
14941494/// unneeded shared objects.
14951495fn markLive(self: *Elf) void {
1496 if (self.zig_module_index) |index| self.file(index).?.markLive(self);
14961497 for (self.objects.items) |index| {
14971498 const file_ptr = self.file(index).?;
14981499 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 {
155155 }
156156}
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
158174pub fn updateSymtabSize(self: *ZigModule, elf_file: *Elf) void {
159175 for (self.locals()) |local_index| {
160176 const local = elf_file.symbol(local_index);
src/link/Elf/file.zig+1-1
......@@ -84,7 +84,7 @@ pub const File = union(enum) {
8484
8585 pub fn markLive(file: File, elf_file: *Elf) void {
8686 switch (file) {
87 .zig_module, .linker_defined => unreachable,
87 .linker_defined => unreachable,
8888 inline else => |x| x.markLive(elf_file),
8989 }
9090 }