Commit d3b1c903dd

Jakub Konka <kubkon@jakubkonka.com>
2023-10-15 21:29:48
elf: emit empty TLS phdr when linking against musl libc even if unneeded
1 parent 5192a2f
Changed files (1)
src
link
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,
         });
     }
 }