Index: lld/trunk/ELF/Target.cpp =================================================================== --- lld/trunk/ELF/Target.cpp +++ lld/trunk/ELF/Target.cpp @@ -178,6 +178,8 @@ RelExpr getRelExpr(uint32_t Type, const SymbolBody &S) const override; uint32_t getDynRel(uint32_t Type) const override; uint64_t getImplicitAddend(const uint8_t *Buf, uint32_t Type) const override; + bool isTlsGlobalDynamicRel(uint32_t Type) const override; + bool isTlsInitialExecRel(uint32_t Type) const override; void writeGotPlt(uint8_t *Buf, const SymbolBody &S) const override; void writePltHeader(uint8_t *Buf) const override; void writePlt(uint8_t *Buf, uint64_t GotEntryAddr, uint64_t PltEntryAddr, @@ -1491,6 +1493,8 @@ GotPltEntrySize = 4; PltEntrySize = 16; PltHeaderSize = 20; + // ARM uses Variant 1 TLS + TcbSize = 8; } RelExpr ARMTargetInfo::getRelExpr(uint32_t Type, const SymbolBody &S) const { @@ -1514,8 +1518,11 @@ // GOT(S) + A - GOT_ORG return R_GOT_OFF; case R_ARM_GOT_PREL: - // GOT(S) + - GOT_ORG + case R_ARM_TLS_IE32: + // GOT(S) + A - P return R_GOT_PC; + case R_ARM_TLS_GD32: + return R_TLSGD_PC; case R_ARM_BASE_PREL: // B(S) + A - P // FIXME: currently B(S) assumed to be .got, this may not hold for all @@ -1528,6 +1535,8 @@ case R_ARM_THM_MOVW_PREL_NC: case R_ARM_THM_MOVT_PREL: return R_PC; + case R_ARM_TLS_LE32: + return R_TLS; } } @@ -1613,6 +1622,9 @@ case R_ARM_GOT_BREL: case R_ARM_GOT_PREL: case R_ARM_REL32: + case R_ARM_TLS_GD32: + case R_ARM_TLS_IE32: + case R_ARM_TLS_LE32: write32le(Loc, Val); break; case R_ARM_PREL31: @@ -1736,6 +1748,9 @@ case R_ARM_GOT_BREL: case R_ARM_GOT_PREL: case R_ARM_REL32: + case R_ARM_TLS_GD32: + case R_ARM_TLS_IE32: + case R_ARM_TLS_LE32: return SignExtend64<32>(read32le(Buf)); case R_ARM_PREL31: return SignExtend64<31>(read32le(Buf)); @@ -1793,6 +1808,14 @@ } } +bool ARMTargetInfo::isTlsGlobalDynamicRel(uint32_t Type) const { + return Type == R_ARM_TLS_GD32; +} + +bool ARMTargetInfo::isTlsInitialExecRel(uint32_t Type) const { + return Type == R_ARM_TLS_IE32; +} + template MipsTargetInfo::MipsTargetInfo() { GotPltHeaderEntriesNum = 2; PageSize = 65536; Index: lld/trunk/test/ELF/arm-tls-gd32.s =================================================================== --- lld/trunk/test/ELF/arm-tls-gd32.s +++ lld/trunk/test/ELF/arm-tls-gd32.s @@ -0,0 +1,106 @@ +// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi +// RUN: ld.lld %t.o -o %t.so -shared +// RUN: llvm-readobj -s -dyn-relocations %t.so | FileCheck --check-prefix=SEC %s +// RUN: llvm-objdump -d -triple=armv7a-linux-gnueabi %t.so | FileCheck %s +// REQUIRES: arm + +// Test the handling of the global-dynamic TLS model. Dynamic Loader finds +// module index R_ARM_TLS_DTPMOD32 and the offset within the module +// R_ARM_TLS_DTPOFF32. One of the variables is hidden which permits relaxation +// to local dynamic + + .text + .syntax unified + .globl func + .p2align 2 + .type func,%function +func: +.L0: + nop +.L1: + nop +.L2: + nop + + .p2align 2 +// Generate R_ARM_TLS_GD32 relocations +// Allocates a pair of GOT entries dynamically relocated by R_ARM_TLS_DTPMOD32 +// and R_ARM_TLS_DTPOFF32 respectively. The literal contains the offset of the +// first GOT entry from the place +.Lt0: .word x(TLSGD) + (. - .L0 - 8) +.Lt1: .word y(TLSGD) + (. - .L1 - 8) +.Lt2: .word z(TLSGD) + (. - .L2 - 8) + +// __thread int x = 10 +// __thread int y; +// __thread int z __attribute((visibility("hidden"))) + + .hidden z + .globl z + .globl y + .globl x + + .section .tbss,"awT",%nobits + .p2align 2 +.TLSSTART: + .type z, %object +z: + .space 4 + .type y, %object +y: + .space 4 + .section .tdata,"awT",%progbits + .p2align 2 + .type x, %object +x: + .word 10 + +// SEC: Name: .tdata +// SEC-NEXT: Type: SHT_PROGBITS +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_TLS +// SEC-NEXT: SHF_WRITE +// SEC-NEXT: ] +// SEC-NEXT: Address: 0x2000 +// SEC: Size: 4 +// SEC: Name: .tbss +// SEC-NEXT: Type: SHT_NOBITS +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_TLS +// SEC-NEXT: SHF_WRITE +// SEC-NEXT: ] +// SEC-NEXT: Address: 0x2004 +// SEC: Size: 8 + +// SEC: Name: .got +// SEC-NEXT: Type: SHT_PROGBITS +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_WRITE +// SEC-NEXT: ] +// SEC-NEXT: Address: 0x204C +// SEC: Size: 24 + +// SEC: Dynamic Relocations { +// SEC-NEXT: 0x205C R_ARM_TLS_DTPMOD32 - +// SEC-NEXT: 0x204C R_ARM_TLS_DTPMOD32 x +// SEC-NEXT: 0x2050 R_ARM_TLS_DTPOFF32 x +// SEC-NEXT: 0x2054 R_ARM_TLS_DTPMOD32 y +// SEC-NEXT: 0x2058 R_ARM_TLS_DTPOFF32 y + + +// CHECK: Disassembly +// CHECK-NEXT: func: +// CHECK-NEXT: 1000: 00 f0 20 e3 nop +// CHECK-NEXT: 1004: 00 f0 20 e3 nop +// CHECK-NEXT: 1008: 00 f0 20 e3 nop + +// (0x204c - 0x100c) + (0x100c - 0x1000 - 8) = 0x1044 +// CHECK: 100c: 44 10 00 00 +// (0x2054 - 0x1010) + (0x1010 - 0x1004 - 8) = 0x1048 +// CHECK-NEXT: 1010: 48 10 00 00 +// (0x205c - 0x1014) + (0x1014 - 0x1008 - 8) = 0x104c +// CHECK-NEXT: 1014: 4c 10 00 00 + Index: lld/trunk/test/ELF/arm-tls-ie32.s =================================================================== --- lld/trunk/test/ELF/arm-tls-ie32.s +++ lld/trunk/test/ELF/arm-tls-ie32.s @@ -0,0 +1,96 @@ +// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi +// RUN: ld.lld %t.o -o %t.so -shared +// RUN: llvm-readobj -s -dyn-relocations %t.so | FileCheck --check-prefix=SEC %s +// RUN: llvm-objdump -d -triple=armv7a-linux-gnueabi %t.so | FileCheck %s +// REQUIRES: arm + +// Test the handling of the initial-exec TLS model. Relative location within +// static TLS is a run-time constant computed by dynamic loader as a result +// of the R_ARM_TLS_TPOFF32 relocation. + + .syntax unified + .arm + .globl func + .type func,%function + .p2align 2 +func: +.L0: + nop +.L1: + nop +.L2: + nop + + .p2align 2 +// Generate R_ARM_TLS_IE32 static relocations +// Allocates a GOT entry dynamically relocated by R_ARM_TLS_TPOFF32 +// literal contains the offset of the GOT entry from the place +.Lt0: .word x(gottpoff) + (. - .L0 - 8) +.Lt1: .word y(gottpoff) + (. - .L1 - 8) +.Lt2: .word .TLSSTART(gottpoff) + (. - .L2 - 8) + +// __thread int x = 10 +// __thread int y; +// __thread int z __attribute((visibility("hidden"))) + .hidden z + .globl z + .globl y + .globl x + + .section .tbss,"awT",%nobits + .p2align 2 +.TLSSTART: + .type z, %object +z: + .space 4 + .type y, %object +y: + .space 4 + .section .tdata,"awT",%progbits + .p2align 2 + .type x, %object +x: + .word 10 + +// SEC: Name: .tdata +// SEC-NEXT: Type: SHT_PROGBITS +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_TLS +// SEC-NEXT: SHF_WRITE +// SEC: Size: 4 +// SEC: Name: .tbss +// SEC-NEXT: Type: SHT_NOBITS +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_TLS +// SEC-NEXT: SHF_WRITE +// SEC: Size: 8 + +// SEC: Name: .got +// SEC-NEXT: Type: SHT_PROGBITS +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_WRITE +// SEC-NEXT: ] +// SEC-NEXT: Address: 0x204C +// SEC: Size: 12 + + +// SEC: Dynamic Relocations { +// SEC: 0x2054 R_ARM_TLS_TPOFF32 +// SEC: 0x204C R_ARM_TLS_TPOFF32 x +// SEC: 0x2050 R_ARM_TLS_TPOFF32 y + +// CHECK: Disassembly of section .text: +// CHECK-NEXT: func: +// CHECK-NEXT: 1000: 00 f0 20 e3 nop +// CHECK-NEXT: 1004: 00 f0 20 e3 nop +// CHECK-NEXT: 1008: 00 f0 20 e3 nop + +// (0x204c - 0x100c) + (0x100c - 0x1000 - 8) = 0x1044 +// CHECK: 100c: 44 10 00 00 +// (0x2050 - 0x1010) + (0x1010 - 0x1004 - 8) = 0x1044 +// CHECK-NEXT: 1010: 44 10 00 00 +// (0x2054 - 0x1014) + (0x1014 - 0x1008 - 8) = 0x1044 +// CHECK-NEXT: 1014: 44 10 00 00 Index: lld/trunk/test/ELF/arm-tls-le32.s =================================================================== --- lld/trunk/test/ELF/arm-tls-le32.s +++ lld/trunk/test/ELF/arm-tls-le32.s @@ -0,0 +1,77 @@ +// RUN: llvm-mc %s -o %t.o -filetype=obj -triple=armv7a-linux-gnueabi +// RUN: ld.lld %t.o -o %t +// RUN: llvm-readobj -s -dyn-relocations %t | FileCheck --check-prefix=SEC %s +// RUN: llvm-objdump -d -triple=armv7a-linux-gnueabi %t | FileCheck %s +// REQUIRES: arm + +// Test the handling of the local exec TLS model. TLS can be resolved +// statically for an application. The code sequences assume a thread pointer +// in r9 + + .text + .syntax unified + .globl _start + .p2align 2 + .type _start,%function +_start: + .p2align 2 +// Generate R_ARM_TLS_LE32 relocations. These resolve statically to the offset +// of the variable from the thread pointer +.Lt0: .word x(TPOFF) +.Lt1: .word y(TPOFF) +.Lt2: .word z(TPOFF) + +// __thread int x = 10 +// __thread int y; +// __thread int z __attribute((visibility("hidden"))) + + .hidden z + .globl z + .globl y + .globl x + + .section .tbss,"awT",%nobits + .p2align 2 +.TLSSTART: + .type z, %object +z: + .space 4 + .type y, %object +y: + .space 4 + .section .tdata,"awT",%progbits + .p2align 2 + .type x, %object +x: + .word 10 + +// SEC: Name: .tdata +// SEC-NEXT: Type: SHT_PROGBITS +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_TLS +// SEC-NEXT: SHF_WRITE +// SEC-NEXT: ] +// SEC-NEXT: Address: 0x12000 +// SEC: Size: 4 +// SEC: Name: .tbss +// SEC-NEXT: Type: SHT_NOBITS +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_TLS +// SEC-NEXT: SHF_WRITE +// SEC-NEXT: ] +// SEC-NEXT: Address: 0x12004 +// SEC: Size: 8 + +// SEC: Dynamic Relocations { +// SEC-NEXT: } + +// CHECK: Disassembly of section .text: +// CHECK-NEXT: _start: +// offset of x from Thread pointer = (TcbSize + 0x0 = 0x8) +// CHECK-NEXT: 11000: 08 00 00 00 +// offset of z from Thread pointer = (TcbSize + 0x8 = 0x10) +// CHECK-NEXT: 11004: 10 00 00 00 +// offset of y from Thread pointer = (TcbSize + 0x4 = 0xc) +// CHECK-NEXT: 11008: 0c 00 00 00