| ... | @@ -3927,13 +3927,25 @@ fn initSpecialPhdrs(self: *Elf) !void { | ... | @@ -3927,13 +3927,25 @@ fn initSpecialPhdrs(self: *Elf) !void { |
| 3927 | .@"align" = 1, | 3927 | .@"align" = 1, |
| 3928 | }); | 3928 | }); |
| 3929 | | 3929 | |
| 3930 | const has_tls = for (self.shdrs.items) |shdr| { | 3930 | const has_tls = has_tls: { |
| 3931 | if (shdr.sh_flags & elf.SHF_TLS != 0) break true; | 3931 | if (self.base.options.link_libc and self.isStatic()) { |
| 3932 | } else false; | 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 | }; |
| 3933 | if (has_tls) { | 3944 | if (has_tls) { |
| 3934 | self.phdr_tls_index = try self.addPhdr(.{ | 3945 | self.phdr_tls_index = try self.addPhdr(.{ |
| 3935 | .type = elf.PT_TLS, | 3946 | .type = elf.PT_TLS, |
| 3936 | .flags = elf.PF_R, | 3947 | .flags = elf.PF_R, |
| | 3948 | .@"align" = 1, |
| 3937 | }); | 3949 | }); |
| 3938 | } | 3950 | } |
| 3939 | } | 3951 | } |