Commit 7258d143e3

Jakub Konka <kubkon@jakubkonka.com>
2024-03-08 14:38:21
elf+aarch64: fix incorrectly emitted TLSDESC relocs
1 parent b5642b4
Changed files (1)
src/link/Elf/synthetic_sections.zig
@@ -634,8 +634,17 @@ pub const GotSection = struct {
                     }
                 },
                 .tlsdesc => {
-                    try writeInt(0, elf_file, writer);
-                    try writeInt(0, elf_file, writer);
+                    if (symbol.?.flags.import) {
+                        try writeInt(0, elf_file, writer);
+                        try writeInt(0, elf_file, writer);
+                    } else {
+                        try writeInt(0, elf_file, writer);
+                        const offset = if (apply_relocs)
+                            @as(i64, @intCast(symbol.?.address(.{}, elf_file))) - @as(i64, @intCast(elf_file.tlsAddress()))
+                        else
+                            0;
+                        try writeInt(offset, elf_file, writer);
+                    }
                 },
             }
         }
@@ -738,8 +747,9 @@ pub const GotSection = struct {
                     const offset = symbol.?.tlsDescAddress(elf_file);
                     elf_file.addRelaDynAssumeCapacity(.{
                         .offset = offset,
-                        .sym = extra.?.dynamic,
+                        .sym = if (symbol.?.flags.import) extra.?.dynamic else 0,
                         .type = relocation.encode(.tlsdesc, cpu_arch),
+                        .addend = if (symbol.?.flags.import) 0 else @intCast(symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()),
                     });
                 },
             }