Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ 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,13 @@ // 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_TLS_LDM32: + return R_TLSLD_PC; case R_ARM_BASE_PREL: // B(S) + A - P // FIXME: currently B(S) assumed to be .got, this may not hold for all @@ -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: test/ELF/arm-tls-gd32.s =================================================================== --- /dev/null +++ test/ELF/arm-tls-gd32.s @@ -0,0 +1,125 @@ +// 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: + ldr r0, .Lt0 +.L0: + add r0, pc, r0 + bl __tls_get_addr + ldr r1, [r0] + + ldr r0, .Lt1 +.L1: + add r0, pc, r0 + bl __tls_get_addr + ldr r1, [r0] + + ldr r0, .Lt2 +.L2: + add r0, pc, r0 + bl __tls_get_addr + ldr r1, [r0] + + .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 (74) +// SEC-NEXT: Type: SHT_PROGBITS (0x1) +// SEC-NEXT: Flags [ +// SEC-NEXT: SHF_ALLOC +// SEC-NEXT: SHF_WRITE +// SEC-NEXT: ] +// SEC-NEXT: Address: 0x206C +// SEC: Size: 24 + +// SEC: Dynamic Relocations { +// SEC-NEXT: 0x207C R_ARM_TLS_DTPMOD32 - +// SEC-NEXT: 0x206C R_ARM_TLS_DTPMOD32 x +// SEC-NEXT: 0x2070 R_ARM_TLS_DTPOFF32 x +// SEC-NEXT: 0x2074 R_ARM_TLS_DTPMOD32 y +// SEC-NEXT: 0x2078 R_ARM_TLS_DTPOFF32 y +// SEC-NEXT: 0x300C R_ARM_JUMP_SLOT __tls_get_addr + +// CHECK: Disassembly +// CHECK-NEXT: func: +// CHECK-NEXT: 1000: 28 00 9f e5 ldr r0, [pc, #40] +// CHECK-NEXT: 1004: 00 00 8f e0 add r0, pc, r0 +// CHECK-NEXT: 1008: 11 00 00 eb bl #68 +// CHECK-NEXT: 100c: 00 10 90 e5 ldr r1, [r0] +// CHECK-NEXT: 1010: 1c 00 9f e5 ldr r0, [pc, #28] +// CHECK-NEXT: 1014: 00 00 8f e0 add r0, pc, r0 +// CHECK-NEXT: 1018: 0d 00 00 eb bl #52 +// CHECK-NEXT: 101c: 00 10 90 e5 ldr r1, [r0] +// CHECK-NEXT: 1020: 10 00 9f e5 ldr r0, [pc, #16] +// CHECK-NEXT: 1024: 00 00 8f e0 add r0, pc, r0 +// CHECK-NEXT: 1028: 09 00 00 eb bl #36 +// CHECK-NEXT: 102c: 00 10 90 e5 ldr r1, [r0] + +// (0x206c - 0x1030) + (0x1030 - 0x1004 - 8) = 0x1060 +// CHECK: 1030: 60 10 00 00 +// (0x2074 - 0x1034) + (0x1034 - 0x1014 - 8) = 0x1058 +// CHECK-NEXT: 1034: 58 10 00 00 +// (0x207C - 0x1038) + (0x1038 - 0x1024 - 8) = 0x1050 +// CHECK-NEXT: 1038: 50 10 00 00 Index: test/ELF/arm-tls-ie32.s =================================================================== --- /dev/null +++ test/ELF/arm-tls-ie32.s @@ -0,0 +1,113 @@ +// 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: + // The code sequences in func are illustrative of code to load an offset + // from a thread pointer in r9 using the initial exec model. + ldr r0, .Lt0 +.LPIC0: + ldr r0, [pc, r0] + ldr r0, [r9, r0] + + ldr r1, .Lt1 +.LPIC1: + ldr r1, [pc, r1] + ldr r1, [r9, r1] + + ldr r2, .Lt2 +.LPIC2: + ldr r2, [pc, r2] + ldr r2, [r9, r2] + + .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) + (. - .LPIC0 - 8) +.Lt1: .word y(gottpoff) + (. - .LPIC1 - 8) +.Lt2: .word .TLSSTART(gottpoff) + (. - .LPIC2 - 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: 1c 00 9f e5 ldr r0, [pc, #28] +// CHECK-NEXT: 1004: 00 00 9f e7 ldr r0, [pc, r0] +// CHECK-NEXT: 1008: 00 00 99 e7 ldr r0, [r9, r0] +// CHECK-NEXT: 100c: 14 10 9f e5 ldr r1, [pc, #20] +// CHECK-NEXT: 1010: 01 10 9f e7 ldr r1, [pc, r1] +// CHECK-NEXT: 1014: 01 10 99 e7 ldr r1, [r9, r1] +// CHECK-NEXT: 1018: 0c 20 9f e5 ldr r2, [pc, #12] +// CHECK-NEXT: 101c: 02 20 9f e7 ldr r2, [pc, r2] +// CHECK-NEXT: 1020: 02 20 99 e7 ldr r2, [r9, r2] + +// (0x204c - 0x1024) + (0x1024 - 0x1004 - 8) = 0x1040 +// CHECK: 1024: 40 10 00 00 +// (0x2050 - 0x1028) + (0x1028 - 0x1010 - 8) = 0x1038 +// CHECK-NEXT: 1028: 38 10 00 00 +// (0x2054 - 0x102c) + (0x102c - 0x101c - 8) = 0x1030 +// CHECK-NEXT: 102c: 30 10 00 00 Index: test/ELF/arm-tls-le32.s =================================================================== --- /dev/null +++ test/ELF/arm-tls-le32.s @@ -0,0 +1,93 @@ +// 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: + ldr r0, .Lt0 + ldr r0, [r9, r0] + + ldr r1, .Lt1 + ldr r1, [r9, r1] + + ldr r2, .Lt2 + ldr r2, [r9, r2] + + .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: +// CHECK-NEXT: 11000: 10 00 9f e5 ldr r0, [pc, #16] +// CHECK-NEXT: 11004: 00 00 99 e7 ldr r0, [r9, r0] +// CHECK-NEXT: 11008: 0c 10 9f e5 ldr r1, [pc, #12] +// CHECK-NEXT: 1100c: 01 10 99 e7 ldr r1, [r9, r1] +// CHECK-NEXT: 11010: 08 20 9f e5 ldr r2, [pc, #8] +// CHECK-NEXT: 11014: 02 20 99 e7 ldr r2, [r9, r2] +// CHECK: $d.1: +// offset of x from Thread pointer = 0x0 +// CHECK-NEXT: 11018: 00 00 00 00 +// offset of z from Thread pointer = 0x8 +// CHECK-NEXT: 1101c: 08 00 00 00 +// offset of y from Thread pointer = 0x4 +// CHECK-NEXT: 11020: 04 00 00 00