zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

commit d3b1c903dddee56b0f2ab06a00848b1b0017f062 (tree)
parent 5192a2fbbe0222ada60d9abccbadf5e8a2d50684
Author: Jakub Konka <kubkon@jakubkonka.com>
Date:   Sun, 15 Oct 2023 21:29:48 +0200

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

Diffstat:
Msrc/link/Elf.zig | 18+++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/link/Elf.zig b/src/link/Elf.zig @@ -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, }); } }