Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -159,18 +159,38 @@ continue; } + const Elf_Shdr *SymTab = File->getSymbolTable(); + SymbolBody *PBody = nullptr; + if (SymIndex >= SymTab->sh_info) + PBody = File->getSymbolBody(SymIndex)->repl(); + + if (Target->isTlsOptimized(Type, PBody)) { + uintX_t SymVA; + if (!PBody) + SymVA = getLocalRelTarget(*File, RI, 0); + else if (Target->relocNeedsGot(Type, *PBody)) + SymVA = Out::Got->getEntryAddr(*PBody); + else + SymVA = getSymVA(*PBody); + // By optimizing TLS relocations, it is sometimes needed to skip + // relocations that immediately follow TLS relocations. This function + // knows how many slots we need to skip. + I += Target->relocateTlsOptimize(BufLoc, BufEnd, Type, AddrLoc, SymVA, + *PBody); + continue; + } + // Handle relocations for local symbols -- they never get // resolved so we don't allocate a SymbolBody. - const Elf_Shdr *SymTab = File->getSymbolTable(); - if (SymIndex < SymTab->sh_info) { - uintX_t SymVA = getLocalRelTarget(*File, RI); + uintX_t A = getAddend(RI); + if (!PBody) { + uintX_t SymVA = getLocalRelTarget(*File, RI, A); Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA, 0, findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs)); continue; } - SymbolBody &Body = *File->getSymbolBody(SymIndex)->repl(); - + SymbolBody &Body = *PBody; if (Target->isTlsGlobalDynamicReloc(Type) && !Target->isTlsOptimized(Type, &Body)) { Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, @@ -179,18 +199,6 @@ continue; } - if (Target->isTlsOptimized(Type, &Body)) { - uintX_t SymVA = Target->relocNeedsGot(Type, Body) - ? Out::Got->getEntryAddr(Body) - : getSymVA(Body); - // By optimizing TLS relocations, it is sometimes needed to skip - // relocations that immediately follow TLS relocations. This function - // knows how many slots we need to skip. - I += Target->relocateTlsOptimize(BufLoc, BufEnd, Type, AddrLoc, SymVA, - Body); - continue; - } - uintX_t SymVA = getSymVA(Body); if (Target->relocNeedsPlt(Type, Body)) { SymVA = Out::Plt->getEntryAddr(Body); @@ -206,7 +214,6 @@ Target->isSizeDynReloc(Type, Body)) { continue; } - uintX_t A = getAddend(RI); uintX_t Size = getSymSize(Body); Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A, findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs)); Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -54,7 +54,8 @@ template typename llvm::object::ELFFile::uintX_t getLocalRelTarget(const ObjectFile &File, - const llvm::object::Elf_Rel_Impl &Rel); + const llvm::object::Elf_Rel_Impl &Rel, + typename llvm::object::ELFFile::uintX_t Addend); bool canBePreempted(const SymbolBody *Body, bool NeedsGot); template bool includeInSymtab(const SymbolBody &B); Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -302,9 +302,11 @@ else if (Body) Addend = getSymVA(cast>(*Body)) + OrigAddend; else if (IsRela) - Addend = getLocalRelTarget(File, static_cast(RI)); + Addend = + getLocalRelTarget(File, static_cast(RI), + getAddend(static_cast(RI))); else - Addend = getLocalRelTarget(File, RI); + Addend = getLocalRelTarget(File, RI, 0); if (IsRela) static_cast(P)->r_addend = Addend; @@ -813,12 +815,11 @@ template typename ELFFile::uintX_t lld::elf2::getLocalRelTarget(const ObjectFile &File, - const Elf_Rel_Impl &RI) { + const Elf_Rel_Impl &RI, + typename ELFFile::uintX_t Addend) { typedef typename ELFFile::Elf_Sym Elf_Sym; typedef typename ELFFile::uintX_t uintX_t; - uintX_t Addend = getAddend(RI); - // PPC64 has a special relocation representing the TOC base pointer // that does not have a corresponding symbol. if (Config->EMachine == EM_PPC64 && RI.getType(false) == R_PPC64_TOC) @@ -1447,29 +1448,20 @@ template ELFFile::uintX_t getLocalRelTarget(const ObjectFile &, - const ELFFile::Elf_Rel &); -template ELFFile::uintX_t -getLocalRelTarget(const ObjectFile &, - const ELFFile::Elf_Rel &); -template ELFFile::uintX_t -getLocalRelTarget(const ObjectFile &, - const ELFFile::Elf_Rel &); -template ELFFile::uintX_t -getLocalRelTarget(const ObjectFile &, - const ELFFile::Elf_Rel &); - -template ELFFile::uintX_t -getLocalRelTarget(const ObjectFile &, - const ELFFile::Elf_Rela &); + const ELFFile::Elf_Rel &, + ELFFile::uintX_t Addend); template ELFFile::uintX_t getLocalRelTarget(const ObjectFile &, - const ELFFile::Elf_Rela &); + const ELFFile::Elf_Rel &, + ELFFile::uintX_t Addend); template ELFFile::uintX_t getLocalRelTarget(const ObjectFile &, - const ELFFile::Elf_Rela &); + const ELFFile::Elf_Rel &, + ELFFile::uintX_t Addend); template ELFFile::uintX_t getLocalRelTarget(const ObjectFile &, - const ELFFile::Elf_Rela &); + const ELFFile::Elf_Rel &, + ELFFile::uintX_t Addend); template bool includeInSymtab(const SymbolBody &); template bool includeInSymtab(const SymbolBody &); Index: test/ELF/tls-opt-local.s =================================================================== --- test/ELF/tls-opt-local.s +++ test/ELF/tls-opt-local.s @@ -0,0 +1,52 @@ +// RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t.o +// RUN: ld.lld %t.o -o %t1 +// RUN: llvm-readobj -r %t1 | FileCheck --check-prefix=NORELOC %s +// RUN: llvm-objdump -d %t1 | FileCheck --check-prefix=DISASM %s + +// NORELOC: Relocations [ +// NORELOC-NEXT: ] + +// DISASM: Disassembly of section .text: +// DISASM-NEXT: _start: +// DISASM-NEXT: 11000: 48 c7 c0 f8 ff ff ff movq $-8, %rax +// DISASM-NEXT: 11007: 49 c7 c7 f8 ff ff ff movq $-8, %r15 +// DISASM-NEXT: 1100e: 48 8d 80 f8 ff ff ff leaq -8(%rax), %rax +// DISASM-NEXT: 11015: 4d 8d bf f8 ff ff ff leaq -8(%r15), %r15 +// DISASM-NEXT: 1101c: 48 81 c4 f8 ff ff ff addq $-8, %rsp +// DISASM-NEXT: 11023: 49 81 c4 f8 ff ff ff addq $-8, %r12 +// DISASM-NEXT: 1102a: 48 c7 c0 fc ff ff ff movq $-4, %rax +// DISASM-NEXT: 11031: 49 c7 c7 fc ff ff ff movq $-4, %r15 +// DISASM-NEXT: 11038: 48 8d 80 fc ff ff ff leaq -4(%rax), %rax +// DISASM-NEXT: 1103f: 4d 8d bf fc ff ff ff leaq -4(%r15), %r15 +// DISASM-NEXT: 11046: 48 81 c4 fc ff ff ff addq $-4, %rsp +// DISASM-NEXT: 1104d: 49 81 c4 fc ff ff ff addq $-4, %r12 + +.section .tbss,"awT",@nobits + +.type tls0,@object +.align 4 +tls0: + .long 0 + .size tls0, 4 + +.type tls1,@object +.align 4 +tls1: + .long 0 + .size tls1, 4 + +.section .text +.globl _start +_start: + movq tls0@GOTTPOFF(%rip), %rax + movq tls0@GOTTPOFF(%rip), %r15 + addq tls0@GOTTPOFF(%rip), %rax + addq tls0@GOTTPOFF(%rip), %r15 + addq tls0@GOTTPOFF(%rip), %rsp + addq tls0@GOTTPOFF(%rip), %r12 + movq tls1@GOTTPOFF(%rip), %rax + movq tls1@GOTTPOFF(%rip), %r15 + addq tls1@GOTTPOFF(%rip), %rax + addq tls1@GOTTPOFF(%rip), %r15 + addq tls1@GOTTPOFF(%rip), %rsp + addq tls1@GOTTPOFF(%rip), %r12