elf: implement markLive for ZigModule

This commit is contained in:
Jakub Konka 2023-09-23 12:42:04 +02:00
parent 8960df0c01
commit ab8a5bfe83
3 changed files with 18 additions and 1 deletions

View File

@ -1493,6 +1493,7 @@ fn resolveSymbols(self: *Elf) error{Overflow}!void {
/// This routine will prune unneeded objects extracted from archives and
/// unneeded shared objects.
fn markLive(self: *Elf) void {
if (self.zig_module_index) |index| self.file(index).?.markLive(self);
for (self.objects.items) |index| {
const file_ptr = self.file(index).?;
if (file_ptr.isAlive()) file_ptr.markLive(self);

View File

@ -155,6 +155,22 @@ pub fn resetGlobals(self: *ZigModule, elf_file: *Elf) void {
}
}
pub fn markLive(self: *ZigModule, elf_file: *Elf) void {
for (self.globals(), 0..) |index, i| {
const esym = self.global_esyms.items[i];
if (esym.st_bind() == elf.STB_WEAK) continue;
const global = elf_file.symbol(index);
const file = global.file(elf_file) orelse continue;
const should_keep = esym.st_shndx == elf.SHN_UNDEF or
(esym.st_shndx == elf.SHN_COMMON and global.elfSym(elf_file).st_shndx != elf.SHN_COMMON);
if (should_keep and !file.isAlive()) {
file.setAlive();
file.markLive(elf_file);
}
}
}
pub fn updateSymtabSize(self: *ZigModule, elf_file: *Elf) void {
for (self.locals()) |local_index| {
const local = elf_file.symbol(local_index);

View File

@ -84,7 +84,7 @@ pub const File = union(enum) {
pub fn markLive(file: File, elf_file: *Elf) void {
switch (file) {
.zig_module, .linker_defined => unreachable,
.linker_defined => unreachable,
inline else => |x| x.markLive(elf_file),
}
}