Index: lld/trunk/ELF/InputSection.cpp =================================================================== --- lld/trunk/ELF/InputSection.cpp +++ lld/trunk/ELF/InputSection.cpp @@ -161,60 +161,66 @@ continue; } + const Elf_Shdr *SymTab = File->getSymbolTable(); + SymbolBody *Body = nullptr; + if (SymIndex >= SymTab->sh_info) + Body = File->getSymbolBody(SymIndex)->repl(); + + if (Target->isTlsOptimized(Type, Body)) { + uintX_t SymVA; + if (!Body) + SymVA = getLocalRelTarget(*File, RI, 0); + else if (Target->relocNeedsGot(Type, *Body)) + SymVA = Out::Got->getEntryAddr(*Body); + else + SymVA = 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; + } + // 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 (!Body) { + 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(); - if (Target->isTlsGlobalDynamicReloc(Type) && - !Target->isTlsOptimized(Type, &Body)) { + !Target->isTlsOptimized(Type, Body)) { Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, - Out::Got->getGlobalDynAddr(Body) + + Out::Got->getGlobalDynAddr(*Body) + getAddend(RI)); 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); + uintX_t SymVA = getSymVA(*Body); + if (Target->relocNeedsPlt(Type, *Body)) { + SymVA = Out::Plt->getEntryAddr(*Body); Type = Target->getPltRefReloc(Type); - } else if (Target->relocNeedsGot(Type, Body)) { - SymVA = Out::Got->getEntryAddr(Body); - if (Body.isTls()) + } else if (Target->relocNeedsGot(Type, *Body)) { + SymVA = Out::Got->getEntryAddr(*Body); + if (Body->isTls()) Type = Target->getTlsGotReloc(Type); - } else if (!Target->needsCopyRel(Type, Body) && - isa>(Body)) { + } else if (!Target->needsCopyRel(Type, *Body) && + isa>(*Body)) { continue; - } else if (Target->isTlsDynReloc(Type, Body) || - Target->isSizeDynReloc(Type, Body)) { + } else if (Target->isTlsDynReloc(Type, *Body) || + Target->isSizeDynReloc(Type, *Body)) { continue; } else if (Config->EMachine == EM_MIPS) { - if (Type == R_MIPS_HI16 && &Body == Config->MipsGpDisp) + if (Type == R_MIPS_HI16 && Body == Config->MipsGpDisp) SymVA = getMipsGpAddr() - AddrLoc; - else if (Type == R_MIPS_LO16 && &Body == Config->MipsGpDisp) + else if (Type == R_MIPS_LO16 && Body == Config->MipsGpDisp) SymVA = getMipsGpAddr() - AddrLoc + 4; } - uintX_t A = getAddend(RI); - uintX_t Size = getSymSize(Body); + uintX_t Size = getSymSize(*Body); Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, SymVA + A, Size + A, findMipsPairedReloc(Buf, SymIndex, Type, NextRelocs)); } Index: lld/trunk/ELF/OutputSections.h =================================================================== --- lld/trunk/ELF/OutputSections.h +++ lld/trunk/ELF/OutputSections.h @@ -59,7 +59,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: lld/trunk/ELF/OutputSections.cpp =================================================================== --- lld/trunk/ELF/OutputSections.cpp +++ lld/trunk/ELF/OutputSections.cpp @@ -307,9 +307,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; @@ -819,12 +821,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) @@ -1500,29 +1501,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: lld/trunk/test/ELF/tls-opt-local.s =================================================================== --- lld/trunk/test/ELF/tls-opt-local.s +++ lld/trunk/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