Index: ELF/Config.h =================================================================== --- ELF/Config.h +++ ELF/Config.h @@ -15,6 +15,7 @@ #include "llvm/ADT/StringSet.h" #include "llvm/Support/CodeGen.h" #include "llvm/Support/ELF.h" +#include "llvm/Support/Endian.h" #include @@ -206,6 +207,13 @@ bool isMips64EL() const { return EMachine == llvm::ELF::EM_MIPS && EKind == ELF64LEKind; } + + // Returns target endianess. + llvm::support::endianness getEndian() const { + return (EKind == ELF32LEKind || EKind == ELF64LEKind) + ? llvm::support::endianness::little + : llvm::support::endianness::big; + } }; // The only instance of Configuration struct. Index: ELF/InputFiles.h =================================================================== --- ELF/InputFiles.h +++ ELF/InputFiles.h @@ -74,6 +74,11 @@ StringRef getName() const { return MB.getBufferIdentifier(); } MemoryBufferRef MB; + ArrayRef getSections() const { + assert(FileKind == ObjectKind || FileKind == BinaryKind); + return Sections; + } + // Filename of .a which contained this file. If this file was // not in an archive file, it is the empty string. We use this // string for creating error messages. @@ -94,6 +99,8 @@ protected: InputFile(Kind K, MemoryBufferRef M) : MB(M), FileKind(K) {} + std::vector Sections; + private: const Kind FileKind; }; @@ -155,7 +162,6 @@ explicit ObjectFile(MemoryBufferRef M); void parse(llvm::DenseSet &ComdatGroups); - ArrayRef getSections() const { return Sections; } InputSectionBase *getSection(const Elf_Sym &Sym) const; SymbolBody &getSymbolBody(uint32_t SymbolIndex) const { @@ -196,9 +202,6 @@ bool shouldMerge(const Elf_Shdr &Sec); SymbolBody *createSymbolBody(const Elf_Sym *Sym); - // List of all sections defined by this file. - std::vector Sections; - // List of all symbols referenced or defined by this file. std::vector SymbolBodies; @@ -320,10 +323,6 @@ explicit BinaryFile(MemoryBufferRef M) : InputFile(BinaryKind, M) {} static bool classof(const InputFile *F) { return F->kind() == BinaryKind; } template void parse(); - ArrayRef getSections() const { return Sections; } - -private: - std::vector Sections; }; InputFile *createObjectFile(MemoryBufferRef MB, StringRef ArchiveName = "", Index: ELF/SyntheticSections.h =================================================================== --- ELF/SyntheticSections.h +++ ELF/SyntheticSections.h @@ -499,9 +499,7 @@ size_t HeaderSize; }; -template class GdbIndexSection final : public SyntheticSection { - typedef typename ELFT::uint uintX_t; - +class GdbIndexSection final : public SyntheticSection { const unsigned OffsetTypeSize = 4; const unsigned CuListOffset = 6 * OffsetTypeSize; const unsigned CompilationUnitSize = 16; @@ -516,7 +514,7 @@ bool empty() const override; // Pairs of [CU Offset, CU length]. - std::vector> CompilationUnits; + std::vector> CompilationUnits; llvm::StringTableBuilder StringPool; @@ -769,7 +767,7 @@ static SymbolTableSection *DynSymTab; static EhFrameHeader *EhFrameHdr; static GnuHashTableSection *GnuHashTab; - static GdbIndexSection *GdbIndex; + static GdbIndexSection *GdbIndex; static GotSection *Got; static EhFrameSection *EhFrame; static MipsGotSection *MipsGot; @@ -800,7 +798,7 @@ template StringTableSection *In::DynStrTab; template SymbolTableSection *In::DynSymTab; template EhFrameHeader *In::EhFrameHdr; -template GdbIndexSection *In::GdbIndex; +template GdbIndexSection *In::GdbIndex; template GnuHashTableSection *In::GnuHashTab; template GotSection *In::Got; template EhFrameSection *In::EhFrame; Index: ELF/SyntheticSections.cpp =================================================================== --- ELF/SyntheticSections.cpp +++ ELF/SyntheticSections.cpp @@ -1700,8 +1700,7 @@ return (HeaderSize == 0) ? In::Plt->getSize() : 0; } -template -GdbIndexSection::GdbIndexSection() +GdbIndexSection::GdbIndexSection() : SyntheticSection(0, SHT_PROGBITS, 1, ".gdb_index"), StringPool(llvm::StringTableBuilder::ELF) {} @@ -1723,7 +1722,6 @@ return Ret; } -template static InputSectionBase *findSection(ArrayRef Arr, uint64_t Offset) { for (InputSectionBase *S : Arr) @@ -1734,7 +1732,6 @@ return nullptr; } -template static std::vector readAddressArea(DWARFContext &Dwarf, InputSection *Sec, size_t CurrentCU) { std::vector Ret; @@ -1743,11 +1740,9 @@ DWARFAddressRangesVector Ranges; CU->collectAddressRanges(Ranges); - ArrayRef Sections = - Sec->template getFile()->getSections(); - + ArrayRef Sections = Sec->File->getSections(); for (std::pair &R : Ranges) - if (InputSectionBase *S = findSection(Sections, R.first)) + if (InputSectionBase *S = findSection(Sections, R.first)) Ret.push_back({S, R.first - S->getOffsetInFile(), R.second - S->getOffsetInFile(), CurrentCU}); ++CurrentCU; @@ -1756,10 +1751,11 @@ } static std::vector> -readPubNamesAndTypes(DWARFContext &Dwarf, bool IsLE) { +readPubNamesAndTypes(DWARFContext &Dwarf) { StringRef Data[] = {Dwarf.getGnuPubNamesSection(), Dwarf.getGnuPubTypesSection()}; + bool IsLE = Config->getEndian() == endianness::little; std::vector> Ret; for (StringRef D : Data) { DWARFDebugPubTable PubTable(D, IsLE, true); @@ -1781,13 +1777,11 @@ std::unique_ptr clone() const override { return {}; } }; -template void GdbIndexSection::readDwarf(InputSection *Sec) { - elf::ObjectFile *File = Sec->template getFile(); - +void GdbIndexSection::readDwarf(InputSection *Sec) { Expected> Obj = - object::ObjectFile::createObjectFile(File->MB); + object::ObjectFile::createObjectFile(Sec->File->MB); if (!Obj) { - error(toString(File) + ": error creating DWARF context"); + error(toString(Sec->File) + ": error creating DWARF context"); return; } @@ -1798,11 +1792,11 @@ for (std::pair &P : readCuList(Dwarf, Sec)) CompilationUnits.push_back(P); - for (AddressEntry &Ent : readAddressArea(Dwarf, Sec, CuId)) + for (AddressEntry &Ent : readAddressArea(Dwarf, Sec, CuId)) AddressArea.push_back(Ent); std::vector> NamesAndTypes = - readPubNamesAndTypes(Dwarf, ELFT::TargetEndianness == support::little); + readPubNamesAndTypes(Dwarf); for (std::pair &Pair : NamesAndTypes) { uint32_t Hash = hash(Pair.first); @@ -1821,7 +1815,7 @@ } } -template void GdbIndexSection::finalizeContents() { +void GdbIndexSection::finalizeContents() { if (Finalized) return; Finalized = true; @@ -1850,12 +1844,12 @@ StringPool.finalizeInOrder(); } -template size_t GdbIndexSection::getSize() const { - const_cast *>(this)->finalizeContents(); +size_t GdbIndexSection::getSize() const { + const_cast(this)->finalizeContents(); return StringPoolOffset + StringPool.getSize(); } -template void GdbIndexSection::writeTo(uint8_t *Buf) { +void GdbIndexSection::writeTo(uint8_t *Buf) { write32le(Buf, 7); // Write version. write32le(Buf + 4, CuListOffset); // CU list offset. write32le(Buf + 8, CuTypesOffset); // Types CU list offset. @@ -1865,7 +1859,7 @@ Buf += 24; // Write the CU list. - for (std::pair CU : CompilationUnits) { + for (std::pair CU : CompilationUnits) { write64le(Buf, CU.first); write64le(Buf + 8, CU.second); Buf += 16; @@ -1873,7 +1867,7 @@ // Write the address area. for (AddressEntry &E : AddressArea) { - uintX_t BaseAddr = E.Section->OutSec->Addr + E.Section->getOffset(0); + uint64_t BaseAddr = E.Section->OutSec->Addr + E.Section->getOffset(0); write64le(Buf, BaseAddr + E.LowAddress); write64le(Buf + 8, BaseAddr + E.HighAddress); write32le(Buf + 16, E.CuIndex); @@ -1909,9 +1903,7 @@ StringPool.write(Buf); } -template bool GdbIndexSection::empty() const { - return !Out::DebugInfo; -} +bool GdbIndexSection::empty() const { return !Out::DebugInfo; } template EhFrameHeader::EhFrameHeader() @@ -2347,11 +2339,6 @@ template class elf::PltSection; template class elf::PltSection; -template class elf::GdbIndexSection; -template class elf::GdbIndexSection; -template class elf::GdbIndexSection; -template class elf::GdbIndexSection; - template class elf::EhFrameHeader; template class elf::EhFrameHeader; template class elf::EhFrameHeader; Index: ELF/Writer.cpp =================================================================== --- ELF/Writer.cpp +++ ELF/Writer.cpp @@ -435,7 +435,7 @@ Add(In::IgotPlt); if (Config->GdbIndex) { - In::GdbIndex = make>(); + In::GdbIndex = make(); Add(In::GdbIndex); }