Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -370,6 +370,8 @@ static StringTableSection *StrTab; static SymbolTableSection *DynSymTab; static SymbolTableSection *SymTab; + static + typename llvm::object::ELFFile::uintX_t ThreadInitializationImageVA; }; template DynamicSection *Out::Dynamic; @@ -389,6 +391,9 @@ template StringTableSection *Out::StrTab; template SymbolTableSection *Out::DynSymTab; template SymbolTableSection *Out::SymTab; +template +typename llvm::object::ELFFile::uintX_t + Out::ThreadInitializationImageVA; } } #endif Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -644,6 +644,9 @@ case SymbolBody::DefinedRegularKind: { const auto &DR = cast>(S); InputSectionBase &SC = DR.Section; + if (DR.Sym.getType() == STT_TLS) + return (SC.OutSec->getVA() + SC.getOffset(DR.Sym)) - + Out::ThreadInitializationImageVA; return SC.OutSec->getVA() + SC.getOffset(DR.Sym); } case SymbolBody::DefinedCommonKind: Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -339,6 +339,9 @@ error("R_X86_64_32S out of range"); write32le(Loc, SA); break; + case R_X86_64_TPOFF32: + write32le(Loc, SA); + break; default: error("unrecognized reloc " + Twine(Type)); } Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -702,8 +702,10 @@ if (Sec->getSize() && (Sec->getFlags() & SHF_ALLOC) && (Sec->getFlags() & SHF_TLS)) { - if (!TlsPhdr.p_vaddr) + if (!TlsPhdr.p_vaddr) { setPhdr(&TlsPhdr, PT_TLS, PF_R, FileOff, VA, 0, Sec->getAlign()); + Out::ThreadInitializationImageVA = VA; + } if (Sec->getType() != SHT_NOBITS) VA = RoundUpToAlignment(VA, Sec->getAlign()); uintX_t TVA = RoundUpToAlignment(VA + ThreadBSSOffset, Sec->getAlign()); Index: test/elf2/tls.s =================================================================== --- test/elf2/tls.s +++ test/elf2/tls.s @@ -1,21 +1,34 @@ // REQUIRES: x86 // RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t // RUN: ld.lld2 %t -o %tout -// RUN: llvm-readobj -sections -program-headers %tout | FileCheck %s +// RUN: llvm-readobj -symbols -sections -program-headers %tout | FileCheck %s +// RUN: llvm-objdump -d %tout | FileCheck %s --check-prefix=DIS .global _start _start: + movl %fs:a@tpoff, %eax + movl %fs:b@tpoff, %eax + movl %fs:c@tpoff, %eax + movl %fs:d@tpoff, %eax + .global a .section .tbss,"awT",@nobits +a: .long 0 + .global b .section .tdata,"awT",@progbits +b: .long 1 + .global c .section .thread_bss,"awT",@nobits +c: .long 0 + .global d .section .thread_data,"awT",@progbits +d: .long 2 // CHECK: Name: .tdata @@ -77,9 +90,9 @@ // CHECK-NEXT: SHF_WRITE // CHECK-NEXT: ] -// 0x1100C = TBSS_ADDR + 4 +// 0x1200C = TBSS_ADDR + 4 -// CHECK-NEXT: Address: 0x1100C +// CHECK-NEXT: Address: 0x1200C // CHECK-NEXT: Offset: // CHECK-NEXT: Size: 4 // CHECK-NEXT: Link: @@ -97,11 +110,49 @@ // CHECK-NEXT: ] // CHECK-NEXT: Address: [[TBSS_ADDR]] +// CHECK: Symbols [ +// CHECK: Name: a +// CHECK-NEXT: Value: 0x8 +// CHECK-NEXT: Size: +// CHECK-NEXT: Binding: Global +// CHECK-NEXT: Type: TLS +// CHECK-NEXT: Other: 0 +// CHECK-NEXT: Section: .tbss +// CHECK-NEXT: } +// CHECK-NEXT: Symbol { +// CHECK-NEXT: Name: b +// CHECK-NEXT: Value: 0x0 +// CHECK-NEXT: Size: +// CHECK-NEXT: Binding: Global +// CHECK-NEXT: Type: TLS +// CHECK-NEXT: Other: 0 +// CHECK-NEXT: Section: .tdata +// CHECK-NEXT: } +// CHECK-NEXT: Symbol { +// CHECK-NEXT: Name: c +// CHECK-NEXT: Value: 0xC +// CHECK-NEXT: Size: +// CHECK-NEXT: Binding: Global +// CHECK-NEXT: Type: TLS +// CHECK-NEXT: Other: 0 +// CHECK-NEXT: Section: .thread_bss +// CHECK-NEXT: } +// CHECK-NEXT: Symbol { +// CHECK-NEXT: Name: d +// CHECK-NEXT: Value: 0x4 +// CHECK-NEXT: Size: +// CHECK-NEXT: Binding: Global +// CHECK-NEXT: Type: TLS +// CHECK-NEXT: Other: 0 +// CHECK-NEXT: Section: .thread_data +// CHECK-NEXT: } + // Check that the TLS NOBITS sections weren't added to the R/W PT_LOAD's size. // CHECK: ProgramHeaders [ // CHECK: Type: PT_LOAD // CHECK: Type: PT_LOAD +// CHECK: Type: PT_LOAD // CHECK: FileSize: 8 // CHECK-NEXT: MemSize: 8 // CHECK-NEXT: Flags [ @@ -119,3 +170,10 @@ // CHECK-NEXT: ] // CHECK-NEXT: Alignment: // CHECK-NEXT: } + +// DIS: Disassembly of section .text: +// DIS-NEXT: _start: +// DIS-NEXT: 11000: {{.+}} movl %fs:8, %eax +// DIS-NEXT: 11008: {{.+}} movl %fs:0, %eax +// DIS-NEXT: 11010: {{.+}} movl %fs:12, %eax +// DIS-NEXT: 11018: {{.+}} movl %fs:4, %eax