authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-15 21:29:48+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:06+02:00
logd3b1c903dddee56b0f2ab06a00848b1b0017f062
tree4da9ca356cc4d511b9c14770b19d2e3ee04e090d
parent5192a2fbbe0222ada60d9abccbadf5e8a2d50684

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


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

src/link/Elf.zig+15-3
......@@ -3927,13 +3927,25 @@ fn initSpecialPhdrs(self: *Elf) !void {
39273927 .@"align" = 1,
39283928 });
39293929
3930 const has_tls = for (self.shdrs.items) |shdr| {
3931 if (shdr.sh_flags & elf.SHF_TLS != 0) break true;
3932 } else false;
3930 const has_tls = has_tls: {
3931 if (self.base.options.link_libc and self.isStatic()) {
3932 // Even if we don't emit any TLS data, linking against musl-libc without
3933 // empty TLS phdr leads to a bizarre segfault in `__copy_tls` function.
3934 // So far I haven't been able to work out why that is, but adding an empty
3935 // TLS phdr seems to fix it, so let's go with it for now.
3936 // TODO try to investigate more
3937 break :has_tls true;
3938 }
3939 for (self.shdrs.items) |shdr| {
3940 if (shdr.sh_flags & elf.SHF_TLS != 0) break :has_tls true;
3941 }
3942 break :has_tls false;
3943 };
39333944 if (has_tls) {
39343945 self.phdr_tls_index = try self.addPhdr(.{
39353946 .type = elf.PT_TLS,
39363947 .flags = elf.PF_R,
3948 .@"align" = 1,
39373949 });
39383950 }
39393951}