Index: ELF/Relocations.cpp =================================================================== --- ELF/Relocations.cpp +++ ELF/Relocations.cpp @@ -154,8 +154,9 @@ Config->Shared) { if (In::Got->addDynTlsEntry(Body)) { uintX_t Off = In::Got->getGlobalDynOffset(Body); + bool UseSymVA = Target->useSymVAForTls(Body); In::RelaDyn->addReloc( - {Target->TlsDescRel, In::Got, Off, false, &Body, 0}); + {Target->TlsDescRel, In::Got, Off, UseSymVA, &Body, 0}); } if (Expr != R_TLSDESC_CALL) C.Relocations.push_back({Expr, Type, Offset, Addend, &Body}); Index: ELF/Target.h =================================================================== --- ELF/Target.h +++ ELF/Target.h @@ -49,6 +49,10 @@ // a dynamic relocation. virtual bool usesOnlyLowPageBits(uint32_t Type) const; + // Returns true if a dynamic relocation create from a TLS relocation should + // use the symbol VA. + virtual bool useSymVAForTls(const SymbolBody &S) const { return false; }; + // Decide whether a Thunk is needed for the relocation from File // targeting S. Returns one of: // Expr if there is no Thunk required Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -164,6 +164,7 @@ void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, int32_t Index, unsigned RelOff) const override; bool usesOnlyLowPageBits(uint32_t Type) const override; + bool useSymVAForTls(const SymbolBody &S) const override; void relocateOne(uint8_t *Loc, uint32_t Type, uint64_t Val) const override; RelExpr adjustRelaxExpr(uint32_t Type, const uint8_t *Data, RelExpr Expr) const override; @@ -1280,6 +1281,12 @@ Type == R_AARCH64_TLSIE_LD64_GOTTPREL_LO12_NC; } +bool AArch64TargetInfo::useSymVAForTls(const SymbolBody &S) const { + // R_AARCH64_TLSDESC against a local symbol, the dynamic relocation + // should have the symbol VA as its addend. + return (Config->Shared && S.isLocal()); +} + bool AArch64TargetInfo::isPicRel(uint32_t Type) const { return Type == R_AARCH64_ABS32 || Type == R_AARCH64_ABS64; } Index: test/ELF/aarch64-tlsdesc.s =================================================================== --- test/ELF/aarch64-tlsdesc.s +++ test/ELF/aarch64-tlsdesc.s @@ -4,21 +4,69 @@ // RUN: llvm-objdump -d %t.so | FileCheck %s // RUN: llvm-readobj -r %t.so | FileCheck --check-prefix=REL %s + .text adrp x0, :tlsdesc:a ldr x1, [x0, :tlsdesc_lo12:a] add x0, x0, :tlsdesc_lo12:a .tlsdesccall a blr x1 +// Create relocation against local TLS symbols where linker should +// create target specific dynamic TLSDESC relocation where addend is +// the symbol VMA in tls block. + // CHECK: 10000: {{.*}} adrp x0, #65536 // CHECK-NEXT: 10004: {{.*}} ldr x1, [x0, #144] // CHECK-NEXT: 10008: {{.*}} add x0, x0, #144 // CHECK-NEXT: 1000c: {{.*}} blr x1 + adrp x0, :tlsdesc:local1 + ldr x1, [x0, :tlsdesc_lo12:local1] + add x0, x0, :tlsdesc_lo12:local1 + .tlsdesccall a + blr x1 + +// CHECK: 10010: {{.*}} adrp x0, #65536 +// CHECK-NEXT: 10014: {{.*}} ldr x1, [x0, #160] +// CHECK-NEXT: 10018: {{.*}} add x0, x0, #160 +// CHECK-NEXT: 1001c: {{.*}} blr x1 + + adrp x0, :tlsdesc:local2 + ldr x1, [x0, :tlsdesc_lo12:local2] + add x0, x0, :tlsdesc_lo12:local2 + .tlsdesccall a + blr x1 + +// CHECK: 10020: {{.*}} adrp x0, #65536 +// CHECK-NEXT: 10024: {{.*}} ldr x1, [x0, #176] +// CHECK-NEXT: 10028: {{.*}} add x0, x0, #176 +// CHECK-NEXT: 1002c: {{.*}} blr x1 + + .section .tbss,"awT",@nobits + .type local1,@object + .p2align 2 +local1: + .word 0 + .size local1, 4 + + .type local2,@object + .p2align 3 +local2: + .xword 0 + .size local2, 8 + + +// 0x1000 + 4096 + 160 = 0x20A0 +// 0x1000 + 4096 + 176 = 0x20B0 // 0x1000 + 4096 + 144 = 0x2090 +// R_AARCH64_TLSDESC - 0x0 -> start of tls block +// R_AARCH64_TLSDESC - 0x8 -> align (sizeof (local1), 8) + // REL: Relocations [ // REL-NEXT: Section (4) .rela.dyn { +// REL-NEXT: 0x200A0 R_AARCH64_TLSDESC - 0x0 +// REL-NEXT: 0x200B0 R_AARCH64_TLSDESC - 0x8 // REL-NEXT: 0x20090 R_AARCH64_TLSDESC a 0x0 // REL-NEXT: } // REL-NEXT: ]