From abdd9e7330c1a36a31df65c278c7c7b6bace7c2a Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 3 Jul 2015 12:00:05 +0000 Subject: [PATCH 1/1] Continue to remove the notion that ELF has dynamic and static symbols. The ELFObjectFile now just reasons about a section/index pair, removing one of the users that force ELF.h to maintain the difference. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@241344 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/Object/ELF.h | 4 +++ include/llvm/Object/ELFObjectFile.h | 49 +++++++++++++++++++---------- 2 files changed, 37 insertions(+), 16 deletions(-) diff --git a/include/llvm/Object/ELF.h b/include/llvm/Object/ELF.h index 7ec47527939..a2ea7bc111e 100644 --- a/include/llvm/Object/ELF.h +++ b/include/llvm/Object/ELF.h @@ -234,6 +234,10 @@ public: const T *getEntry(uint32_t Section, uint32_t Entry) const; template const T *getEntry(const Elf_Shdr *Section, uint32_t Entry) const; + + const Elf_Shdr *getDotSymtabSec() const { return dot_symtab_sec; } + const Elf_Shdr *getDotDynSymSec() const { return DotDynSymSec; } + ErrorOr getStringTable(const Elf_Shdr *Section) const; const char *getDynamicString(uintX_t Offset) const; ErrorOr getSymbolVersion(const Elf_Shdr *section, diff --git a/include/llvm/Object/ELFObjectFile.h b/include/llvm/Object/ELFObjectFile.h index 5738d128b08..4966983822c 100644 --- a/include/llvm/Object/ELFObjectFile.h +++ b/include/llvm/Object/ELFObjectFile.h @@ -244,13 +244,25 @@ protected: } const Elf_Sym *toELFSymIter(DataRefImpl Sym) const { - return reinterpret_cast(Sym.p & ~uintptr_t(1)); + return EF.template getEntry(Sym.d.a, Sym.d.b); } - DataRefImpl toDRI(const Elf_Sym *Sym, bool IsDynamic) const { + DataRefImpl toDRI(const Elf_Shdr *SymTable, unsigned SymbolNum) const { DataRefImpl DRI; - DRI.p = - reinterpret_cast(Sym) | static_cast(IsDynamic); + if (!SymTable) { + DRI.d.a = 0; + DRI.d.b = 0; + return DRI; + } + uint32_t Type = SymTable->sh_type; + assert(Type == ELF::SHT_SYMTAB || Type == ELF::SHT_DYNSYM); + + uintptr_t SHT = reinterpret_cast(EF.section_begin()); + unsigned SymTableIndex = + (reinterpret_cast(SymTable) - SHT) / sizeof(Elf_Shdr); + + DRI.d.a = SymTableIndex; + DRI.d.b = SymbolNum; return DRI; } @@ -343,14 +355,16 @@ typedef ELFObjectFile> ELF64BEObjectFile; template void ELFObjectFile::moveSymbolNext(DataRefImpl &Sym) const { - const Elf_Sym *S = toELFSymIter(Sym); - Sym = toDRI(++S, Sym.p & 1); + ++Sym.d.b; } template ErrorOr ELFObjectFile::getSymbolName(DataRefImpl Sym) const { const Elf_Sym *ESym = toELFSymIter(Sym); - return EF.getSymbolName(ESym, Sym.p & 1); + const Elf_Shdr *SymTableSec = *EF.getSection(Sym.d.a); + const Elf_Shdr *StringTableSec = *EF.getSection(SymTableSec->sh_link); + StringRef SymTable = *EF.getStringTable(StringTableSec); + return ESym->getName(SymTable); } template @@ -481,8 +495,7 @@ uint32_t ELFObjectFile::getSymbolFlags(DataRefImpl Sym) const { Result |= SymbolRef::SF_FormatSpecific; if (EF.getHeader()->e_machine == ELF::EM_ARM) { - ErrorOr NameOrErr = EF.getSymbolName(ESym, Sym.p & 1); - if (NameOrErr) { + if (ErrorOr NameOrErr = getSymbolName(Sym)) { StringRef Name = *NameOrErr; if (Name.startswith("$d") || Name.startswith("$t") || Name.startswith("$a")) @@ -669,9 +682,9 @@ ELFObjectFile::getRelocationSymbol(DataRefImpl Rel) const { bool IsDyn = Rel.d.b & 1; DataRefImpl SymbolData; if (IsDyn) - SymbolData = toDRI(EF.dynamic_symbol_begin() + symbolIdx, true); + SymbolData = toDRI(EF.getDotDynSymSec(), symbolIdx); else - SymbolData = toDRI(EF.symbol_begin() + symbolIdx, false); + SymbolData = toDRI(EF.getDotSymtabSec(), symbolIdx); return symbol_iterator(SymbolRef(SymbolData, this)); } @@ -768,26 +781,30 @@ ELFObjectFile::ELFObjectFile(MemoryBufferRef Object, std::error_code &EC) template basic_symbol_iterator ELFObjectFile::symbol_begin_impl() const { - DataRefImpl Sym = toDRI(EF.symbol_begin(), false); + DataRefImpl Sym = toDRI(EF.getDotSymtabSec(), 0); return basic_symbol_iterator(SymbolRef(Sym, this)); } template basic_symbol_iterator ELFObjectFile::symbol_end_impl() const { - DataRefImpl Sym = toDRI(EF.symbol_end(), false); + const Elf_Shdr *SymTab = EF.getDotSymtabSec(); + if (!SymTab) + return symbol_begin_impl(); + DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym)); return basic_symbol_iterator(SymbolRef(Sym, this)); } template elf_symbol_iterator ELFObjectFile::dynamic_symbol_begin() const { - DataRefImpl Sym = toDRI(EF.dynamic_symbol_begin(), true); + DataRefImpl Sym = toDRI(EF.getDotDynSymSec(), 0); return symbol_iterator(SymbolRef(Sym, this)); } template elf_symbol_iterator ELFObjectFile::dynamic_symbol_end() const { - DataRefImpl Sym = toDRI(EF.dynamic_symbol_end(), true); - return symbol_iterator(SymbolRef(Sym, this)); + const Elf_Shdr *SymTab = EF.getDotDynSymSec(); + DataRefImpl Sym = toDRI(SymTab, SymTab->sh_size / sizeof(Elf_Sym)); + return basic_symbol_iterator(SymbolRef(Sym, this)); } template -- 2.34.1