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 {
366366 const apply_relocs = true; // TODO add user option for this
367367
368368 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 };
370373 switch (entry.tag) {
371374 .got => {
372375 const value = blk: {
373 const value = symbol.address(.{ .plt = false }, elf_file);
374 if (symbol.flags.import) break :blk 0;
375 if (symbol.isIFunc(elf_file))
376 const value = symbol.?.address(.{ .plt = false }, elf_file);
377 if (symbol.?.flags.import) break :blk 0;
378 if (symbol.?.isIFunc(elf_file))
376379 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))
378381 break :blk if (apply_relocs) value else 0;
379382 break :blk value;
380383 };
......@@ -385,26 +388,26 @@ pub const GotSection = struct {
385388 try writeInt(0, elf_file, writer);
386389 },
387390 .tlsgd => {
388 if (symbol.flags.import) {
391 if (symbol.?.flags.import) {
389392 try writeInt(0, elf_file, writer);
390393 try writeInt(0, elf_file, writer);
391394 } else {
392395 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();
394397 try writeInt(offset, elf_file, writer);
395398 }
396399 },
397400 .gottp => {
398 if (symbol.flags.import) {
401 if (symbol.?.flags.import) {
399402 try writeInt(0, elf_file, writer);
400403 } else if (is_dyn_lib) {
401404 const offset = if (apply_relocs)
402 symbol.address(.{}, elf_file) - elf_file.tlsAddress()
405 symbol.?.address(.{}, elf_file) - elf_file.tlsAddress()
403406 else
404407 0;
405408 try writeInt(offset, elf_file, writer);
406409 } else {
407 const offset = @as(i64, @intCast(symbol.address(.{}, elf_file))) -
410 const offset = @as(i64, @intCast(symbol.?.address(.{}, elf_file))) -
408411 @as(i64, @intCast(elf_file.tpAddress()));
409412 try writeInt(offset, elf_file, writer);
410413 }