From 2ffbb193352b361d7a334a85f056aa2f11c4df9b Mon Sep 17 00:00:00 2001 From: Simon Atanasyan Date: Sat, 24 May 2014 16:45:31 +0000 Subject: [PATCH] [Mips] Handle R_MIPS_TLS_TPREL_HI16 / R_MIPS_TLS_TPREL_LO16 relocations. llvm-svn: 209582 --- lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h | 48 ++++++++++------- .../ELF/Mips/MipsRelocationHandler.cpp | 20 +++++++ .../ReaderWriter/ELF/Mips/MipsRelocationPass.cpp | 14 +++++ .../ReaderWriter/ELF/Mips/MipsTargetHandler.cpp | 2 + lld/test/elf/Mips/tls-1.test | 63 ++++++++++++++++++++++ 5 files changed, 127 insertions(+), 20 deletions(-) create mode 100644 lld/test/elf/Mips/tls-1.test diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h b/lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h index 08f6879..f908d12 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsELFFile.h @@ -69,7 +69,7 @@ public: MipsELFFile(std::unique_ptr mb, bool atomizeStrings, error_code &ec) - : ELFFile(std::move(mb), atomizeStrings, ec), _gp0(0) {} + : ELFFile(std::move(mb), atomizeStrings, ec) {} static ErrorOr> create(std::unique_ptr mb, bool atomizeStrings) { @@ -101,8 +101,9 @@ public: if ((ec = file->createAtoms())) return ec; - // Retrieve registry usage descriptor and GP value. - if ((ec = file->readRegInfo())) + // Retrieve some auxiliary data like GP value, TLS section address etc + // from the object file. + if ((ec = file->readAuxData())) return ec; return std::move(file); @@ -113,7 +114,10 @@ public: } /// \brief gp register value stored in the .reginfo section. - int64_t getGP0() const { return _gp0; } + int64_t getGP0() const { return *_gp0; } + + /// \brief .tdata section address plus fixed offset. + uint64_t getTPOffset() const { return *_tpOff; } private: typedef llvm::object::Elf_Sym_Impl Elf_Sym; @@ -121,7 +125,10 @@ private: typedef llvm::object::Elf_Rel_Impl Elf_Rel; typedef typename llvm::object::ELFFile::Elf_Rel_Iter Elf_Rel_Iter; - int64_t _gp0; + enum { TP_OFFSET = 0x7000 }; + + llvm::Optional _gp0; + llvm::Optional _tpOff; ErrorOr *> handleDefinedSymbol( StringRef symName, StringRef sectionName, const Elf_Sym *sym, @@ -133,24 +140,23 @@ private: referenceStart, referenceEnd, referenceList); } - error_code readRegInfo() { + error_code readAuxData() { typedef llvm::object::Elf_RegInfo Elf_RegInfo; for (const Elf_Shdr §ion : this->_objFile->sections()) { - if (section.sh_type != llvm::ELF::SHT_MIPS_REGINFO) - continue; - - auto contents = this->getSectionContents(§ion); - if (error_code ec = contents.getError()) - return ec; - - // FIXME (simon): Show error in case of invalid section size. - if (contents.get().size() == sizeof(Elf_RegInfo)) { - const auto *regInfo = - reinterpret_cast(contents.get().data()); - _gp0 = regInfo->ri_gp_value; - } - break; + if (!_gp0.hasValue() && section.sh_type == llvm::ELF::SHT_MIPS_REGINFO) { + auto contents = this->getSectionContents(§ion); + if (error_code ec = contents.getError()) + return ec; + + ArrayRef raw = contents.get(); + + // FIXME (simon): Show error in case of invalid section size. + assert(raw.size() == sizeof(Elf_RegInfo) && + "Invalid size of RegInfo section"); + _gp0 = reinterpret_cast(raw.data())->ri_gp_value; + } else if (!_tpOff.hasValue() && section.sh_flags & llvm::ELF::SHF_TLS) + _tpOff = section.sh_addr + TP_OFFSET; } return error_code::success(); } @@ -195,6 +201,8 @@ private: case llvm::ELF::R_MIPS_HI16: case llvm::ELF::R_MIPS_LO16: case llvm::ELF::R_MIPS_GOT16: + case llvm::ELF::R_MIPS_TLS_TPREL_HI16: + case llvm::ELF::R_MIPS_TLS_TPREL_LO16: return *(int16_t *)ap; default: return 0; diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp b/lld/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp index 4b514f9..8ee6766 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsRelocationHandler.cpp @@ -101,6 +101,20 @@ static void relocCall16(uint8_t *location, uint64_t P, uint64_t S, int64_t A, applyReloc(location, G, 0xffff); } +/// \brief R_MIPS_TLS_TPREL_HI16 +/// (S + A) >> 16 +static void relocTLSTpRelHi16(uint8_t *location, uint64_t S, int64_t A) { + int32_t result = S + A + 0x8000; + applyReloc(location, result >> 16, 0xffff); +} + +/// \brief R_MIPS_TLS_TPREL_LO16 +/// S + A +static void relocTLSTpRelLo16(uint8_t *location, uint64_t S, int64_t A) { + int32_t result = S + A; + applyReloc(location, result, 0xffff); +} + /// \brief R_MIPS_GPREL32 /// local: rel32 A + S + GP0 – GP (truncate) static void relocGPRel32(uint8_t *location, uint64_t P, uint64_t S, int64_t A, @@ -162,6 +176,12 @@ error_code MipsTargetRelocationHandler::applyRelocation( case R_MIPS_CALL16: relocCall16(location, relocVAddress, targetVAddress, ref.addend(), gpAddr); break; + case R_MIPS_TLS_TPREL_HI16: + relocTLSTpRelHi16(location, targetVAddress, ref.addend()); + break; + case R_MIPS_TLS_TPREL_LO16: + relocTLSTpRelLo16(location, targetVAddress, ref.addend()); + break; case R_MIPS_GPREL32: relocGPRel32(location, relocVAddress, targetVAddress, ref.addend(), gpAddr); break; diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp b/lld/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp index 8bd398b..16f8af8 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsRelocationPass.cpp @@ -205,6 +205,7 @@ private: void handle26(Reference &ref); void handleGOT(Reference &ref); void handleGPRel(const MipsELFDefinedAtom &atom, Reference &ref); + void handleTLS(const MipsELFDefinedAtom &atom, Reference &ref); const GOTAtom *getLocalGOTEntry(const Reference &ref); const GOTAtom *getGlobalGOTEntry(const Atom *a); @@ -325,6 +326,10 @@ void RelocationPass::handleReference(const MipsELFDefinedAtom &atom, case R_MIPS_GPREL32: handleGPRel(atom, ref); break; + case R_MIPS_TLS_TPREL_HI16: + case R_MIPS_TLS_TPREL_LO16: + handleTLS(atom, ref); + break; } } @@ -508,6 +513,15 @@ void RelocationPass::handleGPRel(const MipsELFDefinedAtom &atom, } template +void RelocationPass::handleTLS(const MipsELFDefinedAtom &atom, + Reference &ref) { + assert((ref.kindValue() == R_MIPS_TLS_TPREL_HI16 || + ref.kindValue() == R_MIPS_TLS_TPREL_LO16) && + "Unexpected kind of relocation"); + ref.setAddend(ref.addend() - atom.file().getTPOffset()); +} + +template bool RelocationPass::isLocalCall(const Atom *a) const { Atom::Scope scope; if (auto *da = dyn_cast(a)) diff --git a/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp b/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp index 69a6753..1c90031 100644 --- a/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp +++ b/lld/lib/ReaderWriter/ELF/Mips/MipsTargetHandler.cpp @@ -57,6 +57,8 @@ const Registry::KindStrings MipsTargetHandler::kindStrings[] = { LLD_KIND_STRING_ENTRY(R_MIPS_CALL16), LLD_KIND_STRING_ENTRY(R_MIPS_GPREL32), LLD_KIND_STRING_ENTRY(R_MIPS_JALR), + LLD_KIND_STRING_ENTRY(R_MIPS_TLS_TPREL_HI16), + LLD_KIND_STRING_ENTRY(R_MIPS_TLS_TPREL_LO16), LLD_KIND_STRING_ENTRY(R_MIPS_COPY), LLD_KIND_STRING_ENTRY(R_MIPS_JUMP_SLOT), LLD_KIND_STRING_ENTRY(R_MIPS_PC32), diff --git a/lld/test/elf/Mips/tls-1.test b/lld/test/elf/Mips/tls-1.test new file mode 100644 index 0000000..fce5ae2 --- /dev/null +++ b/lld/test/elf/Mips/tls-1.test @@ -0,0 +1,63 @@ +# Check handling of R_MIPS_TLS_TPREL_HI16 / R_MIPS_TLS_TPREL_LO16 relocations. + +# RUN: yaml2obj -format=elf -o %t.o %s +# RUN: lld -flavor gnu -target mipsel -e L0 -o %t.exe %t.o +# RUN: llvm-objdump -s %t.exe | FileCheck %s + +# CHECK: Contents of section .text: +# CHECK: 400160 00000000 01000000 03800000 04800000 ................ + +!ELF +FileHeader: + Class: ELFCLASS32 + Data: ELFDATA2LSB + Type: ET_REL + Machine: EM_MIPS + Flags: [ EF_MIPS_NOREORDER, EF_MIPS_CPIC, + EF_MIPS_ABI_O32, EF_MIPS_ARCH_32R2 ] +Sections: + - Name: .text + Type: SHT_PROGBITS + Flags: [ SHF_ALLOC, SHF_EXECINSTR ] + AddressAlign: 0x10 + Content: '01000000020000000300000004000000' + - Name: .rel.text + Type: SHT_REL + Info: .text + AddressAlign: 0x04 + Relocations: + - Offset: 0x00 + Symbol: L1 + Type: R_MIPS_TLS_TPREL_HI16 + - Offset: 0x04 + Symbol: L2 + Type: R_MIPS_TLS_TPREL_HI16 + - Offset: 0x08 + Symbol: L2 + Type: R_MIPS_TLS_TPREL_LO16 + - Offset: 0x0C + Symbol: L1 + Type: R_MIPS_TLS_TPREL_LO16 + - Name: .tdata + Type: SHT_PROGBITS + Flags: [ SHF_WRITE, SHF_ALLOC, SHF_TLS ] + AddressAlign: 0x04 + Address: 0x1000 + Size: 0x20000 + +Symbols: + Global: + - Name: L0 + Type: STT_FUNC + Section: .text + Size: 0x58 + - Name: L1 + Type: STT_TLS + Section: .tdata + Value: 0x00 + Size: 0x04 + - Name: L2 + Type: STT_TLS + Section: .tdata + Value: 0x10000 + Size: 0x04 -- 2.7.4