Index: ELF/InputSection.cpp =================================================================== --- ELF/InputSection.cpp +++ ELF/InputSection.cpp @@ -125,6 +125,14 @@ } SymbolBody &Body = *File->getSymbolBody(SymIndex)->repl(); + + if (Type == Target->getTlsGlobalDynamicReloc()) { + Target->relocateOne(BufLoc, BufEnd, Type, AddrLoc, + Out::Got->getEntryAddr(Body) + + getAddend(RI)); + continue; + } + uintX_t SymVA = getSymVA(Body); if (Target->relocNeedsPlt(Type, Body)) { SymVA = Out::Plt->getEntryAddr(Body); Index: ELF/OutputSections.h =================================================================== --- ELF/OutputSections.h +++ ELF/OutputSections.h @@ -160,8 +160,8 @@ template struct DynamicReloc { typedef typename llvm::object::ELFFile::Elf_Rel Elf_Rel; - InputSectionBase &C; - const Elf_Rel &RI; + InputSectionBase *C; + const Elf_Rel *RI; }; template Index: ELF/OutputSections.cpp =================================================================== --- ELF/OutputSections.cpp +++ ELF/OutputSections.cpp @@ -80,6 +80,9 @@ template void GotSection::addEntry(SymbolBody *Sym) { Sym->GotIndex = Target->getGotHeaderEntriesNum() + Entries.size(); Entries.push_back(Sym); + // Global Dynamic TLS entries take two GOT slots. + if (Sym->isTLS()) + Entries.push_back(nullptr); } template uint32_t GotSection::addLocalModuleTlsIndex() { @@ -176,8 +179,13 @@ auto *P = reinterpret_cast(Buf); Buf += EntrySize; - InputSectionBase &C = Rel.C; - const Elf_Rel &RI = Rel.RI; + // Skip placeholder for global dynamic TLS relocation pair. It was already + // handled by the previous relocation. + if (!Rel.C || !Rel.RI) + continue; + + InputSectionBase &C = *Rel.C; + const Elf_Rel &RI = *Rel.RI; uint32_t SymIndex = RI.getSymbol(Config->Mips64EL); const ObjectFile &File = *C.getFile(); SymbolBody *Body = File.getSymbolBody(SymIndex); @@ -194,6 +202,17 @@ continue; } + if (Body && Type == Target->getTlsGlobalDynamicReloc()) { + P->setSymbolAndType(Body->getDynamicSymbolTableIndex(), + Target->getTlsModuleIndexReloc(), Config->Mips64EL); + P->r_offset = Out::Got->getEntryAddr(*Body); + auto *PNext = reinterpret_cast(Buf); + PNext->setSymbolAndType(Body->getDynamicSymbolTableIndex(), + Target->getTlsOffsetReloc(), Config->Mips64EL); + PNext->r_offset = Out::Got->getEntryAddr(*Body) + sizeof(uintX_t); + continue; + } + bool NeedsCopy = Body && Target->relocNeedsCopy(Type, *Body); bool NeedsGot = Body && Target->relocNeedsGot(Type, *Body); bool CanBePreempted = canBePreempted(Body, NeedsGot); Index: ELF/Target.h =================================================================== --- ELF/Target.h +++ ELF/Target.h @@ -30,7 +30,9 @@ unsigned getGotRefReloc() const { return GotRefReloc; } unsigned getRelativeReloc() const { return RelativeReloc; } unsigned getTlsLocalDynamicReloc() const { return TlsLocalDynamicReloc; } + unsigned getTlsGlobalDynamicReloc() const { return TlsGlobalDynamicReloc; } unsigned getTlsModuleIndexReloc() const { return TlsModuleIndexReloc; } + unsigned getTlsOffsetReloc() const { return TlsOffsetReloc; } unsigned getPltZeroEntrySize() const { return PltZeroEntrySize; } unsigned getPltEntrySize() const { return PltEntrySize; } bool supportsLazyRelocations() const { return LazyRelocations; } @@ -70,7 +72,9 @@ unsigned PltReloc; unsigned RelativeReloc; unsigned TlsLocalDynamicReloc = 0; + unsigned TlsGlobalDynamicReloc = 0; unsigned TlsModuleIndexReloc; + unsigned TlsOffsetReloc; unsigned PltEntrySize = 8; unsigned PltZeroEntrySize = 0; unsigned GotHeaderEntriesNum = 0; Index: ELF/Target.cpp =================================================================== --- ELF/Target.cpp +++ ELF/Target.cpp @@ -216,7 +216,9 @@ PltReloc = R_X86_64_JUMP_SLOT; RelativeReloc = R_X86_64_RELATIVE; TlsLocalDynamicReloc = R_X86_64_TLSLD; + TlsGlobalDynamicReloc = R_X86_64_TLSGD; TlsModuleIndexReloc = R_X86_64_DTPMOD64; + TlsOffsetReloc = R_X86_64_DTPOFF64; LazyRelocations = true; PltEntrySize = 16; PltZeroEntrySize = 16; @@ -335,6 +337,7 @@ case R_X86_64_GOTPCREL: case R_X86_64_PLT32: case R_X86_64_TLSLD: + case R_X86_64_TLSGD: write32le(Loc, SA - P); break; case R_X86_64_64: Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -196,7 +196,7 @@ if (Out::LocalModuleTlsIndexOffset == uint32_t(-1)) { Out::LocalModuleTlsIndexOffset = Out::Got->addLocalModuleTlsIndex(); - Out::RelaDyn->addReloc({C, RI}); + Out::RelaDyn->addReloc({&C, &RI}); } continue; } @@ -208,6 +208,19 @@ if (Body) Body = Body->repl(); + + if (Body && Body->isTLS()) { + if (Type != Target->getTlsGlobalDynamicReloc()) + continue; + if (Body->isInGot()) + continue; + Out::Got->addEntry(Body); + Out::RelaDyn->addReloc({&C, &RI}); + Out::RelaDyn->addReloc({nullptr, nullptr}); + Body->setUsedInDynamicReloc(); + continue; + } + bool NeedsGot = false; bool NeedsPlt = false; if (Body) { @@ -250,9 +263,9 @@ if (CBP) Body->setUsedInDynamicReloc(); if (NeedsPlt && Target->supportsLazyRelocations()) - Out::RelaPlt->addReloc({C, RI}); + Out::RelaPlt->addReloc({&C, &RI}); else - Out::RelaDyn->addReloc({C, RI}); + Out::RelaDyn->addReloc({&C, &RI}); } } Index: test/elf2/tls-dynamic.s =================================================================== --- test/elf2/tls-dynamic.s +++ test/elf2/tls-dynamic.s @@ -11,6 +11,10 @@ leaq a@dtpoff(%rax), %rcx leaq b@dtpoff(%rax), %rcx .long b@dtpoff, 0 + leaq c@tlsgd(%rip), %rdi + rex64 + callq __tls_get_addr@PLT + leaq c@dtpoff(%rax), %rcx .global a .hidden a @@ -23,8 +27,14 @@ .align 4 b: .long 0 + + .global c + .section .tbss,"awT",@nobits + .align 4 +c: + .long 0 -// Get the address of the got, and check that it has two entries. +// Get the address of the got, and check that it has 4 entries. // CHECK: Sections [ // CHECK: Name: .got @@ -35,15 +45,18 @@ // CHECK-NEXT: ] // CHECK-NEXT: Address: 0x20D0 // CHECK-NEXT: Offset: -// CHECK-NEXT: Size: 16 +// CHECK-NEXT: Size: 32 // CHECK: Relocations [ // CHECK: Section ({{.+}}) .rela.dyn { // CHECK-NEXT: 0x20D0 R_X86_64_DTPMOD64 - 0x0 +// CHECK-NEXT: 0x20E0 R_X86_64_DTPMOD64 c 0x0 +// CHECK-NEXT: 0x20E8 R_X86_64_DTPOFF64 c 0x0 // CHECK-NEXT: } // 4297 = (0x20D0 + -4) - (0x1000 + 3) // PC relative offset to got entry. // 4285 = (0x20D0 + -4) - (0x100c + 3) // PC relative offset to got entry. +// 4267 = (0x20E0 + -4) - (0x102e + 3) // PC relative offset to got entry. // DIS: Disassembly of section .text: // DIS-NEXT: .text: @@ -57,3 +70,6 @@ // DIS-NEXT: 1028: 00 00 // DIS-NEXT: 102a: 00 00 // DIS-NEXT: 102c: 00 00 +// DIS-NEXT: 102e: {{.+}} leaq 4267(%rip), %rdi +// DIS-NEXT: 1035: {{.+}} callq +// DIS-NEXT: 103b: {{.+}} leaq 8(%rax), %rcx