authorgravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-03-08 14:20:35+01:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2024-03-08 22:46:17+01:00
log310cef09724067f4df38b42c928cb120fdc09df8
tree43a0c6cc166c47a9371edc858b9853b4edb53b3f
parent109d2321b0d92f20e75dcc0b7074026cefe1090e

elf+aarch64: handle TLSDESC non-relaxed


2 files changed, 48 insertions(+), 8 deletions(-)

src/link/Elf.zig+1-2
...@@ -2105,7 +2105,6 @@ fn scanRelocs(self: *Elf) !void {...@@ -2105,7 +2105,6 @@ fn scanRelocs(self: *Elf) !void {
2105 }2105 }
2106 if (sym.flags.needs_tlsdesc) {2106 if (sym.flags.needs_tlsdesc) {
2107 log.debug("'{s}' needs TLSDESC", .{sym.name(self)});2107 log.debug("'{s}' needs TLSDESC", .{sym.name(self)});
2108 try self.dynsym.addSymbol(index, self);
2109 try self.got.addTlsDescSymbol(index, self);2108 try self.got.addTlsDescSymbol(index, self);
2110 }2109 }
2111 }2110 }
...@@ -4497,7 +4496,7 @@ fn writeAtoms(self: *Elf) !void {...@@ -4497,7 +4496,7 @@ fn writeAtoms(self: *Elf) !void {
4497 const buffer = try gpa.alloc(u8, sh_size);4496 const buffer = try gpa.alloc(u8, sh_size);
4498 defer gpa.free(buffer);4497 defer gpa.free(buffer);
4499 const padding_byte: u8 = if (shdr.sh_type == elf.SHT_PROGBITS and4498 const padding_byte: u8 = if (shdr.sh_type == elf.SHT_PROGBITS and
4500 shdr.sh_flags & elf.SHF_EXECINSTR != 0)4499 shdr.sh_flags & elf.SHF_EXECINSTR != 0 and self.getTarget().cpu.arch == .x86_64)
4501 0xcc // int34500 0xcc // int3
4502 else4501 else
4503 0;4502 0;
src/link/Elf/Atom.zig+47-6
...@@ -1622,6 +1622,22 @@ const aarch64 = struct {...@@ -1622,6 +1622,22 @@ const aarch64 = struct {
1622 }1622 }
1623 },1623 },
16241624
1625 .TLSLE_ADD_TPREL_HI12,
1626 .TLSLE_ADD_TPREL_LO12_NC,
1627 => {
1628 if (is_dyn_lib) try atom.reportPicError(symbol, rel, elf_file);
1629 },
1630
1631 .TLSDESC_ADR_PAGE21,
1632 .TLSDESC_LD64_LO12,
1633 .TLSDESC_ADD_LO12,
1634 => {
1635 const should_relax = elf_file.base.isStatic() or (!is_dyn_lib and !symbol.flags.import);
1636 if (!should_relax and true) { // TODO
1637 symbol.flags.needs_tlsdesc = true;
1638 }
1639 },
1640
1625 .ADD_ABS_LO12_NC,1641 .ADD_ABS_LO12_NC,
1626 .ADR_PREL_LO21,1642 .ADR_PREL_LO21,
1627 .LDST8_ABS_LO12_NC,1643 .LDST8_ABS_LO12_NC,
...@@ -1629,14 +1645,9 @@ const aarch64 = struct {...@@ -1629,14 +1645,9 @@ const aarch64 = struct {
1629 .LDST32_ABS_LO12_NC,1645 .LDST32_ABS_LO12_NC,
1630 .LDST64_ABS_LO12_NC,1646 .LDST64_ABS_LO12_NC,
1631 .LDST128_ABS_LO12_NC,1647 .LDST128_ABS_LO12_NC,
1648 .TLSDESC_CALL,
1632 => {},1649 => {},
16331650
1634 .TLSLE_ADD_TPREL_HI12,
1635 .TLSLE_ADD_TPREL_LO12_NC,
1636 => {
1637 if (is_dyn_lib) try atom.reportPicError(symbol, rel, elf_file);
1638 },
1639
1640 else => try atom.reportUnhandledRelocError(rel, elf_file),1651 else => try atom.reportUnhandledRelocError(rel, elf_file),
1641 }1652 }
1642 }1653 }
...@@ -1757,6 +1768,36 @@ const aarch64 = struct {...@@ -1757,6 +1768,36 @@ const aarch64 = struct {
1757 aarch64_util.writeAddImmInst(@bitCast(value), code[rel.r_offset..][0..4]);1768 aarch64_util.writeAddImmInst(@bitCast(value), code[rel.r_offset..][0..4]);
1758 },1769 },
17591770
1771 .TLSDESC_ADR_PAGE21 => {
1772 assert(target.flags.has_tlsdesc); // TODO relax
1773 const S_: i64 = @intCast(target.tlsDescAddress(elf_file));
1774 const saddr: u64 = @intCast(P);
1775 const taddr: u64 = @intCast(S_ + A);
1776 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1777 const pages: u21 = @bitCast(try aarch64_util.calcNumberOfPages(saddr, taddr));
1778 aarch64_util.writeAdrpInst(pages, code[rel.r_offset..][0..4]);
1779 },
1780
1781 .TLSDESC_LD64_LO12 => {
1782 const S_: i64 = @intCast(target.tlsDescAddress(elf_file));
1783 const taddr: u64 = @intCast(S_ + A);
1784 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1785 const offset: u12 = try math.divExact(u12, @truncate(taddr), 8);
1786 aarch64_util.writeLoadStoreRegInst(offset, code[rel.r_offset..][0..4]);
1787 },
1788
1789 .TLSDESC_ADD_LO12 => {
1790 const S_: i64 = @intCast(target.tlsDescAddress(elf_file));
1791 const taddr: u64 = @intCast(S_ + A);
1792 relocs_log.debug(" [{x} => {x}]", .{ P, taddr });
1793 const offset: u12 = @truncate(taddr);
1794 aarch64_util.writeAddImmInst(offset, code[rel.r_offset..][0..4]);
1795 },
1796
1797 .TLSDESC_CALL => if (!target.flags.has_tlsdesc) {
1798 mem.writeInt(u32, code[rel.r_offset..][0..4], aarch64_util.Instruction.nop().toU32(), .little);
1799 },
1800
1760 else => try atom.reportUnhandledRelocError(rel, elf_file),1801 else => try atom.reportUnhandledRelocError(rel, elf_file),
1761 }1802 }
1762 }1803 }