elf: emit empty TLS phdr when linking against musl libc even if unneeded

This commit is contained in:
Jakub Konka 2023-10-15 21:29:48 +02:00
parent 5192a2fbbe
commit d3b1c903dd

View File

@ -3927,13 +3927,25 @@ fn initSpecialPhdrs(self: *Elf) !void {
.@"align" = 1,
});
const has_tls = for (self.shdrs.items) |shdr| {
if (shdr.sh_flags & elf.SHF_TLS != 0) break true;
} else false;
const has_tls = has_tls: {
if (self.base.options.link_libc and self.isStatic()) {
// Even if we don't emit any TLS data, linking against musl-libc without
// empty TLS phdr leads to a bizarre segfault in `__copy_tls` function.
// So far I haven't been able to work out why that is, but adding an empty
// TLS phdr seems to fix it, so let's go with it for now.
// TODO try to investigate more
break :has_tls true;
}
for (self.shdrs.items) |shdr| {
if (shdr.sh_flags & elf.SHF_TLS != 0) break :has_tls true;
}
break :has_tls false;
};
if (has_tls) {
self.phdr_tls_index = try self.addPhdr(.{
.type = elf.PT_TLS,
.flags = elf.PF_R,
.@"align" = 1,
});
}
}