authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-09 22:31:52+02:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-10-16 19:33:05+02:00
log0b37a9c78d5b554fa137cfd5c66c756cf4a64893
tree7886c25492c4541c73fed6b0ddc10f99c9de14b3
parent898c87bd2ab7d49fbbcb1d01e6b72fec54bdfd09

elf: fix GotSection.write in presence of TLSLD symbol


1 files changed, 13 insertions(+), 10 deletions(-)

src/link/Elf/synthetic_sections.zig+13-10
...@@ -366,15 +366,18 @@ pub const GotSection = struct {...@@ -366,15 +366,18 @@ pub const GotSection = struct {
366 const apply_relocs = true; // TODO add user option for this366 const apply_relocs = true; // TODO add user option for this
367367
368 for (got.entries.items) |entry| {368 for (got.entries.items) |entry| {
369 const symbol = elf_file.symbol(entry.symbol_index);369 const symbol = switch (entry.tag) {
370 .tlsld => null,
371 inline else => elf_file.symbol(entry.symbol_index),
372 };
370 switch (entry.tag) {373 switch (entry.tag) {
371 .got => {374 .got => {
372 const value = blk: {375 const value = blk: {
373 const value = symbol.address(.{ .plt = false }, elf_file);376 const value = symbol.?.address(.{ .plt = false }, elf_file);
374 if (symbol.flags.import) break :blk 0;377 if (symbol.?.flags.import) break :blk 0;
375 if (symbol.isIFunc(elf_file))378 if (symbol.?.isIFunc(elf_file))
376 break :blk if (apply_relocs) value else 0;379 break :blk if (apply_relocs) value else 0;
377 if (elf_file.base.options.pic and !symbol.isAbs(elf_file))380 if (elf_file.base.options.pic and !symbol.?.isAbs(elf_file))
378 break :blk if (apply_relocs) value else 0;381 break :blk if (apply_relocs) value else 0;
379 break :blk value;382 break :blk value;
380 };383 };
...@@ -385,26 +388,26 @@ pub const GotSection = struct {...@@ -385,26 +388,26 @@ pub const GotSection = struct {
385 try writeInt(0, elf_file, writer);388 try writeInt(0, elf_file, writer);
386 },389 },
387 .tlsgd => {390 .tlsgd => {
388 if (symbol.flags.import) {391 if (symbol.?.flags.import) {
389 try writeInt(0, elf_file, writer);392 try writeInt(0, elf_file, writer);
390 try writeInt(0, elf_file, writer);393 try writeInt(0, elf_file, writer);
391 } else {394 } else {
392 try writeInt(if (is_dyn_lib) @as(u64, 0) else 1, elf_file, writer);395 try writeInt(if (is_dyn_lib) @as(u64, 0) else 1, elf_file, writer);
393 const offset = symbol.address(.{}, elf_file) - elf_file.dtpAddress();396 const offset = symbol.?.address(.{}, elf_file) - elf_file.dtpAddress();
394 try writeInt(offset, elf_file, writer);397 try writeInt(offset, elf_file, writer);
395 }398 }
396 },399 },
397 .gottp => {400 .gottp => {
398 if (symbol.flags.import) {401 if (symbol.?.flags.import) {
399 try writeInt(0, elf_file, writer);402 try writeInt(0, elf_file, writer);
400 } else if (is_dyn_lib) {403 } else if (is_dyn_lib) {
401 const offset = if (apply_relocs)404 const offset = if (apply_relocs)
402 symbol.address(.{}, elf_file) - elf_file.tlsAddress()405 symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()
403 else406 else
404 0;407 0;
405 try writeInt(offset, elf_file, writer);408 try writeInt(offset, elf_file, writer);
406 } else {409 } else {
407 const offset = @as(i64, @intCast(symbol.address(.{}, elf_file))) -410 const offset = @as(i64, @intCast(symbol.?.address(.{}, elf_file))) -
408 @as(i64, @intCast(elf_file.tpAddress()));411 @as(i64, @intCast(elf_file.tpAddress()));
409 try writeInt(offset, elf_file, writer);412 try writeInt(offset, elf_file, writer);
410 }413 }