proxygen
|
#include <Elf.h>
Public Types | |
enum | { kSuccess = 0, kSystemError = -1, kInvalidElfFile = -2 } |
typedef std::pair< const ElfShdr *, const ElfSym * > | Symbol |
Public Member Functions | |
ElfFile () noexcept | |
ElfFile (const char *name, bool readOnly=true) | |
int | openNoThrow (const char *name, bool readOnly=true, const char **msg=nullptr) noexcept |
int | openAndFollow (const char *name, bool readOnly=true, const char **msg=nullptr) noexcept |
void | open (const char *name, bool readOnly=true) |
~ElfFile () | |
ElfFile (ElfFile &&other) noexcept | |
ElfFile & | operator= (ElfFile &&other) |
const ElfEhdr & | elfHeader () const |
uintptr_t | getBaseAddress () const |
const ElfShdr * | getSectionByName (const char *name) const |
const ElfShdr * | getSectionByIndex (size_t idx) const |
const char * | getSectionName (const ElfShdr §ion) const |
folly::StringPiece | getSectionBody (const ElfShdr §ion) const |
const char * | getString (const ElfShdr &stringTable, size_t offset) const |
template<class Fn > | |
const char * | iterateStrings (const ElfShdr &stringTable, Fn fn) const |
template<class Fn > | |
const ElfPhdr * | iterateProgramHeaders (Fn fn) const |
template<class Fn > | |
const ElfShdr * | iterateSections (Fn fn) const |
template<class Fn > | |
const ElfShdr * | iterateSectionsWithType (uint32_t type, Fn fn) const |
template<class Fn > | |
const ElfShdr * | iterateSectionsWithTypes (std::initializer_list< uint32_t > types, Fn fn) const |
template<class Fn > | |
const ElfSym * | iterateSymbols (const ElfShdr §ion, Fn fn) const |
template<class Fn > | |
const ElfSym * | iterateSymbolsWithType (const ElfShdr §ion, uint32_t type, Fn fn) const |
template<class Fn > | |
const ElfSym * | iterateSymbolsWithTypes (const ElfShdr §ion, std::initializer_list< uint32_t > types, Fn fn) const |
Symbol | getDefinitionByAddress (uintptr_t address) const |
Symbol | getSymbolByName (const char *name) const |
template<class T > | |
const T & | getSymbolValue (const ElfSym *symbol) const |
template<class T > | |
const T & | getAddressValue (const ElfAddr addr) const |
const char * | getSymbolName (Symbol symbol) const |
const ElfShdr * | getSectionContainingAddress (ElfAddr addr) const |
Private Member Functions | |
bool | init (const char **msg) |
void | reset () |
ElfFile (const ElfFile &)=delete | |
ElfFile & | operator= (const ElfFile &)=delete |
void | validateStringTable (const ElfShdr &stringTable) const |
template<class T > | |
const std::enable_if< std::is_pod< T >::value, T >::type & | at (ElfOff offset) const |
template<class T > | |
const T & | valueAt (const ElfShdr §ion, const ElfAddr addr) const |
Private Attributes | |
char | filepath_ [kFilepathMaxLen] = {} |
int | fd_ |
char * | file_ |
size_t | length_ |
uintptr_t | baseAddress_ |
Static Private Attributes | |
static constexpr size_t | kFilepathMaxLen = 512 |
ELF file parser.
We handle native files only (32-bit files on a 32-bit platform, 64-bit files on a 64-bit platform), and only executables (ET_EXEC) and shared objects (ET_DYN).
typedef std::pair<const ElfShdr*, const ElfSym*> folly::symbolizer::ElfFile::Symbol |
anonymous enum |
|
noexcept |
|
explicit |
Definition at line 45 of file Elf.cpp.
References open().
folly::symbolizer::ElfFile::~ElfFile | ( | ) |
|
noexcept |
Definition at line 158 of file Elf.cpp.
References filepath_, and kFilepathMaxLen.
|
privatedelete |
|
inlineprivate |
Definition at line 242 of file Elf.h.
References file_, filepath_, FOLLY_SAFE_CHECK, kFilepathMaxLen, length_, and folly::T.
|
inline |
Retrieve the ELF header
Definition at line 90 of file Elf.h.
Referenced by getSectionByIndex(), getSectionByName(), getSectionName(), init(), iterateProgramHeaders(), iterateSections(), and valueAt().
|
inline |
Get the value of the object stored at the given address.
This is the function that you want to use in conjunction with getSymbolValue() to follow pointers. For example, to get the value of a char* symbol, you'd do something like this:
auto sym = getSymbolByName("someGlobalValue"); auto addr = getSymbolValue<ElfAddr>(sym.second); const char* str = &getSymbolValue<const char>(addr);
Definition at line 218 of file Elf.h.
References addr, ElfFile(), FOLLY_SAFE_CHECK, getSectionContainingAddress(), getSymbolName(), init(), operator=(), reset(), and validateStringTable().
Referenced by TEST_F().
|
inline |
Get the base address, the address where the file should be loaded if no relocations happened.
Definition at line 98 of file Elf.h.
References baseAddress_, getSectionBody(), getSectionByIndex(), getSectionByName(), getSectionName(), getString(), iterateProgramHeaders(), iterateSections(), iterateSectionsWithType(), iterateSectionsWithTypes(), iterateStrings(), iterateSymbols(), iterateSymbolsWithType(), iterateSymbolsWithTypes(), type, and uint32_t.
ElfFile::Symbol folly::symbolizer::ElfFile::getDefinitionByAddress | ( | uintptr_t | address | ) | const |
Definition at line 370 of file Elf.cpp.
References iterateSectionsWithType(), iterateSymbolsWithTypes(), and STT_GNU_IFUNC.
folly::StringPiece folly::symbolizer::ElfFile::getSectionBody | ( | const ElfShdr & | section | ) | const |
Get the actual section body
Definition at line 318 of file Elf.cpp.
References file_.
Referenced by getBaseAddress(), folly::symbolizer::Dwarf::getSection(), and openAndFollow().
const ElfShdr * folly::symbolizer::ElfFile::getSectionByIndex | ( | size_t | idx | ) | const |
Find a section given its index in the section header table
Definition at line 313 of file Elf.cpp.
References elfHeader(), and FOLLY_SAFE_CHECK.
Referenced by getBaseAddress(), getSectionByName(), getSectionName(), getSymbolByName(), getSymbolName(), and getSymbolValue().
const ElfShdr * folly::symbolizer::ElfFile::getSectionByName | ( | const char * | name | ) | const |
Find a section given its name
Definition at line 352 of file Elf.cpp.
References elfHeader(), file_, getSectionByIndex(), iterateSections(), and start.
Referenced by getBaseAddress(), folly::symbolizer::Dwarf::getSection(), and openAndFollow().
Find the section containing the given address
Definition at line 437 of file Elf.cpp.
References iterateSections().
Referenced by getAddressValue().
const char * folly::symbolizer::ElfFile::getSectionName | ( | const ElfShdr & | section | ) | const |
Retrieve the name of a section
Definition at line 343 of file Elf.cpp.
References elfHeader(), getSectionByIndex(), and getString().
Referenced by getBaseAddress().
const char * folly::symbolizer::ElfFile::getString | ( | const ElfShdr & | stringTable, |
size_t | offset | ||
) | const |
Retrieve a string from a string table section
Definition at line 334 of file Elf.cpp.
References file_, FOLLY_SAFE_CHECK, and validateStringTable().
Referenced by getBaseAddress(), getSectionName(), getSymbolByName(), and getSymbolName().
ElfFile::Symbol folly::symbolizer::ElfFile::getSymbolByName | ( | const char * | name | ) | const |
Find symbol definition by name.
If a symbol with this name cannot be found, a <nullptr, nullptr> Symbol will be returned. This is O(N) in the number of symbols in the file.
Returns {nullptr, nullptr} if not found.
Definition at line 398 of file Elf.cpp.
References getSectionByIndex(), getString(), iterateSectionsWithType(), iterateSymbolsWithTypes(), and STT_GNU_IFUNC.
Referenced by TEST_F().
const char * folly::symbolizer::ElfFile::getSymbolName | ( | Symbol | symbol | ) | const |
Retrieve symbol name.
Definition at line 443 of file Elf.cpp.
References getSectionByIndex(), and getString().
Referenced by getAddressValue().
|
inline |
Get the value of a symbol.
Definition at line 199 of file Elf.h.
References FOLLY_SAFE_CHECK, and getSectionByIndex().
Referenced by TEST_F().
|
private |
Definition at line 206 of file Elf.cpp.
References baseAddress_, elfHeader(), EXPECTED_CLASS, fd_, h, iterateProgramHeaders(), folly::kIsLittleEndian, length_, and fizz::detail::read().
Referenced by getAddressValue(), and openNoThrow().
const ElfPhdr * folly::symbolizer::ElfFile::iterateProgramHeaders | ( | Fn | fn | ) | const |
Iterate over program headers as long as fn(section) returns false. Returns a pointer to the current ("found") section when fn returned true, or nullptr if fn returned false for all sections.
Definition at line 25 of file Elf-inl.h.
References elfHeader(), i, length_, and ptr.
Referenced by getBaseAddress(), init(), and TEST_F().
const ElfShdr * folly::symbolizer::ElfFile::iterateSections | ( | Fn | fn | ) | const |
Iterate over all sections for as long as fn(section) returns false. Returns a pointer to the current ("found") section when fn returned true, or nullptr if fn returned false for all sections.
Definition at line 43 of file Elf-inl.h.
References elfHeader(), i, length_, and ptr.
Referenced by getBaseAddress(), getSectionByName(), getSectionContainingAddress(), iterateSectionsWithType(), and iterateSectionsWithTypes().
const ElfShdr * folly::symbolizer::ElfFile::iterateSectionsWithType | ( | uint32_t | type, |
Fn | fn | ||
) | const |
Iterate over all sections with a given type. Similar to iterateSections(), but filtered only for sections with the given type.
Definition at line 61 of file Elf-inl.h.
References iterateSections().
Referenced by getBaseAddress(), getDefinitionByAddress(), and getSymbolByName().
const ElfShdr * folly::symbolizer::ElfFile::iterateSectionsWithTypes | ( | std::initializer_list< uint32_t > | types, |
Fn | fn | ||
) | const |
Iterate over all sections with a given types. Similar to iterateSectionWithTypes(), but filtered on multiple types.
Definition at line 67 of file Elf-inl.h.
References iterateSections().
Referenced by getBaseAddress().
const char * folly::symbolizer::ElfFile::iterateStrings | ( | const ElfShdr & | stringTable, |
Fn | fn | ||
) | const |
Iterate over all strings in a string table section for as long as fn(str) returns false. Returns the current ("found") string when fn returned true, or nullptr if fn returned false for all strings in the table.
Definition at line 77 of file Elf-inl.h.
References folly::test::end(), file_, ptr, start, and validateStringTable().
Referenced by getBaseAddress().
const ElfSym * folly::symbolizer::ElfFile::iterateSymbols | ( | const ElfShdr & | section, |
Fn | fn | ||
) | const |
Iterate over all symbols witin a given section.
Returns a pointer to the current ("found") symbol when fn returned true, or nullptr if fn returned false for all symbols.
Definition at line 92 of file Elf-inl.h.
References folly::test::end(), and FOLLY_SAFE_CHECK.
Referenced by getBaseAddress(), iterateSymbolsWithType(), and iterateSymbolsWithTypes().
const ElfSym * folly::symbolizer::ElfFile::iterateSymbolsWithType | ( | const ElfShdr & | section, |
uint32_t | type, | ||
Fn | fn | ||
) | const |
Definition at line 112 of file Elf-inl.h.
References iterateSymbols().
Referenced by getBaseAddress().
const ElfSym * folly::symbolizer::ElfFile::iterateSymbolsWithTypes | ( | const ElfShdr & | section, |
std::initializer_list< uint32_t > | types, | ||
Fn | fn | ||
) | const |
Definition at line 123 of file Elf-inl.h.
References iterateSymbols().
Referenced by getBaseAddress(), getDefinitionByAddress(), and getSymbolByName().
void folly::symbolizer::ElfFile::open | ( | const char * | name, |
bool | readOnly = true |
||
) |
Definition at line 53 of file Elf.cpp.
References kSuccess, kSystemError, openNoThrow(), and folly::throwSystemError().
Referenced by ElfFile(), and openNoThrow().
|
noexcept |
Definition at line 110 of file Elf.cpp.
References getSectionBody(), getSectionByName(), kSuccess, name, openNoThrow(), and reset().
|
noexcept |
Definition at line 63 of file Elf.cpp.
References fd_, file_, filepath_, FOLLY_SAFE_CHECK, folly::gen::guard(), init(), kFilepathMaxLen, kInvalidElfFile, kSuccess, kSystemError, length_, folly::makeGuard(), name, open(), and reset().
Referenced by open(), openAndFollow(), and TEST_F().
Definition at line 172 of file Elf.cpp.
References baseAddress_, fd_, file_, filepath_, kFilepathMaxLen, length_, and reset().
Referenced by getAddressValue().
|
private |
Definition at line 192 of file Elf.cpp.
References folly::netops::close(), fd_, file_, filepath_, and length_.
Referenced by getAddressValue(), openAndFollow(), openNoThrow(), operator=(), and ~ElfFile().
|
private |
Definition at line 322 of file Elf.cpp.
References file_, FOLLY_SAFE_CHECK, and start.
Referenced by getAddressValue(), getString(), and iterateStrings().
|
inlineprivate |
Definition at line 262 of file Elf.h.
References elfHeader(), FOLLY_SAFE_CHECK, and folly::T.
|
private |
Definition at line 289 of file Elf.h.
Referenced by getBaseAddress(), init(), and operator=().
|
private |
Definition at line 285 of file Elf.h.
Referenced by init(), openNoThrow(), operator=(), and reset().
|
private |
Definition at line 286 of file Elf.h.
Referenced by at(), getSectionBody(), getSectionByName(), getString(), iterateStrings(), openNoThrow(), operator=(), reset(), and validateStringTable().
|
private |
Definition at line 284 of file Elf.h.
Referenced by at(), ElfFile(), openNoThrow(), operator=(), and reset().
|
staticprivate |
Definition at line 283 of file Elf.h.
Referenced by at(), ElfFile(), openNoThrow(), and operator=().
|
private |
Definition at line 287 of file Elf.h.
Referenced by at(), init(), iterateProgramHeaders(), iterateSections(), openNoThrow(), operator=(), and reset().