From 9cf32a0f1d22c5ece8a581e98166ddac8a6e61a7 Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Fri, 3 Nov 2017 21:30:06 +0000 Subject: [PATCH] Revert r317046, "Object: Move some code from ELF.h into ELF.cpp." This change resulted in a measured 1.5-2% perf regression linking chrome. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@317371 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ELF.h | 263 ++++++++++++++++++++++++++++++++++++++ lib/Object/ELF.cpp | 263 -------------------------------------- 2 files changed, 263 insertions(+), 263 deletions(-) diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 92fb46e8e935..c24b6310465e 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -204,6 +204,46 @@ getExtendedSymbolTableIndex(const typename ELFT::Sym *Sym, return ShndxTable[Index]; } +template +Expected +ELFFile::getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, + ArrayRef ShndxTable) const { + uint32_t Index = Sym->st_shndx; + if (Index == ELF::SHN_XINDEX) { + auto ErrorOrIndex = getExtendedSymbolTableIndex( + Sym, Syms.begin(), ShndxTable); + if (!ErrorOrIndex) + return ErrorOrIndex.takeError(); + return *ErrorOrIndex; + } + if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE) + return 0; + return Index; +} + +template +Expected +ELFFile::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, + ArrayRef ShndxTable) const { + auto SymsOrErr = symbols(SymTab); + if (!SymsOrErr) + return SymsOrErr.takeError(); + return getSection(Sym, *SymsOrErr, ShndxTable); +} + +template +Expected +ELFFile::getSection(const Elf_Sym *Sym, Elf_Sym_Range Symbols, + ArrayRef ShndxTable) const { + auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable); + if (!IndexOrErr) + return IndexOrErr.takeError(); + uint32_t Index = *IndexOrErr; + if (Index == 0) + return nullptr; + return getSection(Index); +} + template inline Expected getSymbol(typename ELFT::SymRange Symbols, uint32_t Index) { @@ -212,6 +252,15 @@ getSymbol(typename ELFT::SymRange Symbols, uint32_t Index) { return &Symbols[Index]; } +template +Expected +ELFFile::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const { + auto SymtabOrErr = symbols(Sec); + if (!SymtabOrErr) + return SymtabOrErr.takeError(); + return object::getSymbol(*SymtabOrErr, Index); +} + template template Expected> @@ -232,6 +281,119 @@ ELFFile::getSectionContentsAsArray(const Elf_Shdr *Sec) const { return makeArrayRef(Start, Size / sizeof(T)); } +template +Expected> +ELFFile::getSectionContents(const Elf_Shdr *Sec) const { + return getSectionContentsAsArray(Sec); +} + +template +StringRef ELFFile::getRelocationTypeName(uint32_t Type) const { + return getELFRelocationTypeName(getHeader()->e_machine, Type); +} + +template +void ELFFile::getRelocationTypeName(uint32_t Type, + SmallVectorImpl &Result) const { + if (!isMipsELF64()) { + StringRef Name = getRelocationTypeName(Type); + Result.append(Name.begin(), Name.end()); + } else { + // The Mips N64 ABI allows up to three operations to be specified per + // relocation record. Unfortunately there's no easy way to test for the + // presence of N64 ELFs as they have no special flag that identifies them + // as being N64. We can safely assume at the moment that all Mips + // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough + // information to disambiguate between old vs new ABIs. + uint8_t Type1 = (Type >> 0) & 0xFF; + uint8_t Type2 = (Type >> 8) & 0xFF; + uint8_t Type3 = (Type >> 16) & 0xFF; + + // Concat all three relocation type names. + StringRef Name = getRelocationTypeName(Type1); + Result.append(Name.begin(), Name.end()); + + Name = getRelocationTypeName(Type2); + Result.append(1, '/'); + Result.append(Name.begin(), Name.end()); + + Name = getRelocationTypeName(Type3); + Result.append(1, '/'); + Result.append(Name.begin(), Name.end()); + } +} + +template +Expected +ELFFile::getRelocationSymbol(const Elf_Rel *Rel, + const Elf_Shdr *SymTab) const { + uint32_t Index = Rel->getSymbol(isMips64EL()); + if (Index == 0) + return nullptr; + return getEntry(SymTab, Index); +} + +template +Expected +ELFFile::getSectionStringTable(Elf_Shdr_Range Sections) const { + uint32_t Index = getHeader()->e_shstrndx; + if (Index == ELF::SHN_XINDEX) + Index = Sections[0].sh_link; + + if (!Index) // no section string table. + return ""; + if (Index >= Sections.size()) + return createError("invalid section index"); + return getStringTable(&Sections[Index]); +} + +template ELFFile::ELFFile(StringRef Object) : Buf(Object) {} + +template +Expected> ELFFile::create(StringRef Object) { + if (sizeof(Elf_Ehdr) > Object.size()) + return createError("Invalid buffer"); + return ELFFile(Object); +} + +template +Expected ELFFile::sections() const { + const uintX_t SectionTableOffset = getHeader()->e_shoff; + if (SectionTableOffset == 0) + return ArrayRef(); + + if (getHeader()->e_shentsize != sizeof(Elf_Shdr)) + return createError( + "invalid section header entry size (e_shentsize) in ELF header"); + + const uint64_t FileSize = Buf.size(); + + if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) + return createError("section header table goes past the end of the file"); + + // Invalid address alignment of section headers + if (SectionTableOffset & (alignof(Elf_Shdr) - 1)) + return createError("invalid alignment of section headers"); + + const Elf_Shdr *First = + reinterpret_cast(base() + SectionTableOffset); + + uintX_t NumSections = getHeader()->e_shnum; + if (NumSections == 0) + NumSections = First->sh_size; + + if (NumSections > UINT64_MAX / sizeof(Elf_Shdr)) + return createError("section table goes past the end of file"); + + const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr); + + // Section table goes past end of file! + if (SectionTableOffset + SectionTableSize > FileSize) + return createError("section table goes past the end of file"); + + return makeArrayRef(First, NumSections); +} + template template Expected ELFFile::getEntry(uint32_t Section, @@ -254,6 +416,107 @@ Expected ELFFile::getEntry(const Elf_Shdr *Section, return reinterpret_cast(base() + Pos); } +template +Expected +ELFFile::getSection(uint32_t Index) const { + auto TableOrErr = sections(); + if (!TableOrErr) + return TableOrErr.takeError(); + return object::getSection(*TableOrErr, Index); +} + +template +Expected +ELFFile::getStringTable(const Elf_Shdr *Section) const { + if (Section->sh_type != ELF::SHT_STRTAB) + return createError("invalid sh_type for string table, expected SHT_STRTAB"); + auto V = getSectionContentsAsArray(Section); + if (!V) + return V.takeError(); + ArrayRef Data = *V; + if (Data.empty()) + return createError("empty string table"); + if (Data.back() != '\0') + return createError("string table non-null terminated"); + return StringRef(Data.begin(), Data.size()); +} + +template +Expected> +ELFFile::getSHNDXTable(const Elf_Shdr &Section) const { + auto SectionsOrErr = sections(); + if (!SectionsOrErr) + return SectionsOrErr.takeError(); + return getSHNDXTable(Section, *SectionsOrErr); +} + +template +Expected> +ELFFile::getSHNDXTable(const Elf_Shdr &Section, + Elf_Shdr_Range Sections) const { + assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX); + auto VOrErr = getSectionContentsAsArray(&Section); + if (!VOrErr) + return VOrErr.takeError(); + ArrayRef V = *VOrErr; + auto SymTableOrErr = object::getSection(Sections, Section.sh_link); + if (!SymTableOrErr) + return SymTableOrErr.takeError(); + const Elf_Shdr &SymTable = **SymTableOrErr; + if (SymTable.sh_type != ELF::SHT_SYMTAB && + SymTable.sh_type != ELF::SHT_DYNSYM) + return createError("invalid sh_type"); + if (V.size() != (SymTable.sh_size / sizeof(Elf_Sym))) + return createError("invalid section contents size"); + return V; +} + +template +Expected +ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec) const { + auto SectionsOrErr = sections(); + if (!SectionsOrErr) + return SectionsOrErr.takeError(); + return getStringTableForSymtab(Sec, *SectionsOrErr); +} + +template +Expected +ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec, + Elf_Shdr_Range Sections) const { + + if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM) + return createError( + "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM"); + auto SectionOrErr = object::getSection(Sections, Sec.sh_link); + if (!SectionOrErr) + return SectionOrErr.takeError(); + return getStringTable(*SectionOrErr); +} + +template +Expected +ELFFile::getSectionName(const Elf_Shdr *Section) const { + auto SectionsOrErr = sections(); + if (!SectionsOrErr) + return SectionsOrErr.takeError(); + auto Table = getSectionStringTable(*SectionsOrErr); + if (!Table) + return Table.takeError(); + return getSectionName(Section, *Table); +} + +template +Expected ELFFile::getSectionName(const Elf_Shdr *Section, + StringRef DotShstrtab) const { + uint32_t Offset = Section->sh_name; + if (Offset == 0) + return StringRef(); + if (Offset >= DotShstrtab.size()) + return createError("invalid string offset"); + return StringRef(DotShstrtab.data() + Offset); +} + /// This function returns the hash value for a symbol in the .dynsym section /// Name of the API remains consistent as specified in the libelf /// REF : http://www.sco.com/developers/gabi/latest/ch5.dynamic.html#hash diff --git a/lib/Object/ELF.cpp b/lib/Object/ELF.cpp index ef8c844a66f1..c72a1258c1ee 100644 --- a/lib/Object/ELF.cpp +++ b/lib/Object/ELF.cpp @@ -214,269 +214,6 @@ StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) { } } -template -Expected -ELFFile::getSectionIndex(const Elf_Sym *Sym, Elf_Sym_Range Syms, - ArrayRef ShndxTable) const { - uint32_t Index = Sym->st_shndx; - if (Index == ELF::SHN_XINDEX) { - auto ErrorOrIndex = getExtendedSymbolTableIndex( - Sym, Syms.begin(), ShndxTable); - if (!ErrorOrIndex) - return ErrorOrIndex.takeError(); - return *ErrorOrIndex; - } - if (Index == ELF::SHN_UNDEF || Index >= ELF::SHN_LORESERVE) - return 0; - return Index; -} - -template -Expected -ELFFile::getSection(const Elf_Sym *Sym, const Elf_Shdr *SymTab, - ArrayRef ShndxTable) const { - auto SymsOrErr = symbols(SymTab); - if (!SymsOrErr) - return SymsOrErr.takeError(); - return getSection(Sym, *SymsOrErr, ShndxTable); -} - -template -Expected -ELFFile::getSection(const Elf_Sym *Sym, Elf_Sym_Range Symbols, - ArrayRef ShndxTable) const { - auto IndexOrErr = getSectionIndex(Sym, Symbols, ShndxTable); - if (!IndexOrErr) - return IndexOrErr.takeError(); - uint32_t Index = *IndexOrErr; - if (Index == 0) - return nullptr; - return getSection(Index); -} - -template -Expected -ELFFile::getSymbol(const Elf_Shdr *Sec, uint32_t Index) const { - auto SymtabOrErr = symbols(Sec); - if (!SymtabOrErr) - return SymtabOrErr.takeError(); - return object::getSymbol(*SymtabOrErr, Index); -} - -template -Expected> -ELFFile::getSectionContents(const Elf_Shdr *Sec) const { - return getSectionContentsAsArray(Sec); -} - -template -StringRef ELFFile::getRelocationTypeName(uint32_t Type) const { - return getELFRelocationTypeName(getHeader()->e_machine, Type); -} - -template -void ELFFile::getRelocationTypeName(uint32_t Type, - SmallVectorImpl &Result) const { - if (!isMipsELF64()) { - StringRef Name = getRelocationTypeName(Type); - Result.append(Name.begin(), Name.end()); - } else { - // The Mips N64 ABI allows up to three operations to be specified per - // relocation record. Unfortunately there's no easy way to test for the - // presence of N64 ELFs as they have no special flag that identifies them - // as being N64. We can safely assume at the moment that all Mips - // ELFCLASS64 ELFs are N64. New Mips64 ABIs should provide enough - // information to disambiguate between old vs new ABIs. - uint8_t Type1 = (Type >> 0) & 0xFF; - uint8_t Type2 = (Type >> 8) & 0xFF; - uint8_t Type3 = (Type >> 16) & 0xFF; - - // Concat all three relocation type names. - StringRef Name = getRelocationTypeName(Type1); - Result.append(Name.begin(), Name.end()); - - Name = getRelocationTypeName(Type2); - Result.append(1, '/'); - Result.append(Name.begin(), Name.end()); - - Name = getRelocationTypeName(Type3); - Result.append(1, '/'); - Result.append(Name.begin(), Name.end()); - } -} - -template -Expected -ELFFile::getRelocationSymbol(const Elf_Rel *Rel, - const Elf_Shdr *SymTab) const { - uint32_t Index = Rel->getSymbol(isMips64EL()); - if (Index == 0) - return nullptr; - return getEntry(SymTab, Index); -} - -template -Expected -ELFFile::getSectionStringTable(Elf_Shdr_Range Sections) const { - uint32_t Index = getHeader()->e_shstrndx; - if (Index == ELF::SHN_XINDEX) - Index = Sections[0].sh_link; - - if (!Index) // no section string table. - return ""; - if (Index >= Sections.size()) - return createError("invalid section index"); - return getStringTable(&Sections[Index]); -} - -template ELFFile::ELFFile(StringRef Object) : Buf(Object) {} - -template -Expected> ELFFile::create(StringRef Object) { - if (sizeof(Elf_Ehdr) > Object.size()) - return createError("Invalid buffer"); - return ELFFile(Object); -} - -template -Expected ELFFile::sections() const { - const uintX_t SectionTableOffset = getHeader()->e_shoff; - if (SectionTableOffset == 0) - return ArrayRef(); - - if (getHeader()->e_shentsize != sizeof(Elf_Shdr)) - return createError( - "invalid section header entry size (e_shentsize) in ELF header"); - - const uint64_t FileSize = Buf.size(); - - if (SectionTableOffset + sizeof(Elf_Shdr) > FileSize) - return createError("section header table goes past the end of the file"); - - // Invalid address alignment of section headers - if (SectionTableOffset & (alignof(Elf_Shdr) - 1)) - return createError("invalid alignment of section headers"); - - const Elf_Shdr *First = - reinterpret_cast(base() + SectionTableOffset); - - uintX_t NumSections = getHeader()->e_shnum; - if (NumSections == 0) - NumSections = First->sh_size; - - if (NumSections > UINT64_MAX / sizeof(Elf_Shdr)) - return createError("section table goes past the end of file"); - - const uint64_t SectionTableSize = NumSections * sizeof(Elf_Shdr); - - // Section table goes past end of file! - if (SectionTableOffset + SectionTableSize > FileSize) - return createError("section table goes past the end of file"); - - return makeArrayRef(First, NumSections); -} - -template -Expected -ELFFile::getSection(uint32_t Index) const { - auto TableOrErr = sections(); - if (!TableOrErr) - return TableOrErr.takeError(); - return object::getSection(*TableOrErr, Index); -} - -template -Expected -ELFFile::getStringTable(const Elf_Shdr *Section) const { - if (Section->sh_type != ELF::SHT_STRTAB) - return createError("invalid sh_type for string table, expected SHT_STRTAB"); - auto V = getSectionContentsAsArray(Section); - if (!V) - return V.takeError(); - ArrayRef Data = *V; - if (Data.empty()) - return createError("empty string table"); - if (Data.back() != '\0') - return createError("string table non-null terminated"); - return StringRef(Data.begin(), Data.size()); -} - -template -Expected> -ELFFile::getSHNDXTable(const Elf_Shdr &Section) const { - auto SectionsOrErr = sections(); - if (!SectionsOrErr) - return SectionsOrErr.takeError(); - return getSHNDXTable(Section, *SectionsOrErr); -} - -template -Expected> -ELFFile::getSHNDXTable(const Elf_Shdr &Section, - Elf_Shdr_Range Sections) const { - assert(Section.sh_type == ELF::SHT_SYMTAB_SHNDX); - auto VOrErr = getSectionContentsAsArray(&Section); - if (!VOrErr) - return VOrErr.takeError(); - ArrayRef V = *VOrErr; - auto SymTableOrErr = object::getSection(Sections, Section.sh_link); - if (!SymTableOrErr) - return SymTableOrErr.takeError(); - const Elf_Shdr &SymTable = **SymTableOrErr; - if (SymTable.sh_type != ELF::SHT_SYMTAB && - SymTable.sh_type != ELF::SHT_DYNSYM) - return createError("invalid sh_type"); - if (V.size() != (SymTable.sh_size / sizeof(Elf_Sym))) - return createError("invalid section contents size"); - return V; -} - -template -Expected -ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec) const { - auto SectionsOrErr = sections(); - if (!SectionsOrErr) - return SectionsOrErr.takeError(); - return getStringTableForSymtab(Sec, *SectionsOrErr); -} - -template -Expected -ELFFile::getStringTableForSymtab(const Elf_Shdr &Sec, - Elf_Shdr_Range Sections) const { - - if (Sec.sh_type != ELF::SHT_SYMTAB && Sec.sh_type != ELF::SHT_DYNSYM) - return createError( - "invalid sh_type for symbol table, expected SHT_SYMTAB or SHT_DYNSYM"); - auto SectionOrErr = object::getSection(Sections, Sec.sh_link); - if (!SectionOrErr) - return SectionOrErr.takeError(); - return getStringTable(*SectionOrErr); -} - -template -Expected -ELFFile::getSectionName(const Elf_Shdr *Section) const { - auto SectionsOrErr = sections(); - if (!SectionsOrErr) - return SectionsOrErr.takeError(); - auto Table = getSectionStringTable(*SectionsOrErr); - if (!Table) - return Table.takeError(); - return getSectionName(Section, *Table); -} - -template -Expected ELFFile::getSectionName(const Elf_Shdr *Section, - StringRef DotShstrtab) const { - uint32_t Offset = Section->sh_name; - if (Offset == 0) - return StringRef(); - if (Offset >= DotShstrtab.size()) - return createError("invalid string offset"); - return StringRef(DotShstrtab.data() + Offset); -} - template Expected> ELFFile::android_relas(const Elf_Shdr *Sec) const {