44#include < elf.h>
55#include < string>
66
7+ /* *
8+ * @brief Holds the ELF File Mapping
9+ */
710union ElfPack {
11+ /* *
12+ * @brief Pointer to ELF Header
13+ */
814 Elf32_Ehdr* header;
15+
16+ /* *
17+ * @brief Pointer to ELF64 Header
18+ */
919 Elf64_Ehdr* header64;
20+
21+ /* *
22+ * @brief ELF File mapping base address
23+ */
1024 uintptr_t base;
25+
26+ /* *
27+ * @brief Pointer to the ELF Mapping
28+ */
1129 void * baseV;
30+
31+ /* *
32+ * @brief Lazy int version of the Mapping Pointer, to easily do checks
33+ */
1234 int res;
1335};
1436
37+ /* *
38+ * @brief Initializes the ELF Library, Notify Callback, Cleanup, Frees the ELF library
39+ * @returns true if all the operations was sucessfully, false otherwise
40+ */
1541bool ElfOpen (const std::string& fullModulePath, std::function<void (ElfPack libMap)> callback);
42+
43+ /* *
44+ * @brief Check if ELF file is 64 bits.
45+ * @returns true if ELF File is 64 bits, false otherwise
46+ */
1647bool ElfPeekIs64 (const std::string& fullModulePath, bool & outResult);
48+
49+ /* *
50+ * @brief Get a ELF Section by its given Index.
51+ * @param sectionIdx: the given section index
52+ * @returns a pointer to a section header if valid, nullptr otherwise
53+ */
1754Elf32_Shdr* ElfSectionByIndex (ElfPack libMap, unsigned int sectionIdx);
55+
56+ /* *
57+ * @brief Traverses all sections within the ELF File
58+ * @param callback: will be reported, all the given sections
59+ */
1860void ElfForEachSection (ElfPack libMap, std::function<bool (Elf32_Shdr* pCurrentSection)> callback);
61+
62+ /* *
63+ * @brief Lookup an ELF Section by its given type
64+ * @param sectionType: ELF Section Type
65+ * @returns A pointer to the section if it exists; nullptr otherwise.
66+ */
1967Elf32_Shdr* ElfLookupSectionByType (ElfPack libMap, uint32_t sectionType);
68+
69+ /* *
70+ * @brief Retrieve the ELF Section Headers Name Blob (shstr) Entry.
71+ * @returns A pointer to the char blob entry if exist; nullptr otherwise
72+ */
2073const char * ElfGetSectionHeadersStringBlob (ElfPack libMap);
74+
75+ /* *
76+ * @brief Retrieve ELF Section name
77+ * @param sectionHdr: Pointer to ELF Section Header
78+ * @returns A Pointer to the section name if exist; nullptr otherwise.
79+ */
2180const char * ElfGetSectionName (ElfPack libMap, Elf32_Shdr* sectionHdr);
81+
82+ /* *
83+ * @brief Lookup a ELF Header by its name
84+ * @param sectionName: Name of the section (ex: ".rodata", ".text" ...)
85+ * @returns A Pointer to the ELF Section if found; nullptr otherwise.
86+ */
2287Elf32_Shdr* ElfLookupSectionByName (ElfPack libMap, const std::string& sectionName);
88+
89+ /* *
90+ * @brief Retrieve any available Symbol Table ELF Section.
91+ * @returns A pointer to the Symbol Table ELF Section if exist; nullptr otherwise;
92+ * @note This function first searches for an SHT_SYMTAB type, and if none is found,
93+ * it searches for an SHT_DYNSYM type.
94+ */
2395Elf32_Shdr* ElfGetSymbolSection (ElfPack libMap);
96+
97+ /* *
98+ * @brief Traverse the symbol table.
99+ * @param callback: A callback, for each symbol found, this callback will be invocated with the actual symbol & its name.
100+ * @returns true if a symbol table to traverse was found, nullptr otherwise.
101+ */
24102bool ElfForEachSymbol (ElfPack libMap, std::function<bool (Elf32_Sym* pCurrentSym, const char * pCurrSymName)> callback);
103+
104+ /* *
105+ * @brief Lookup a symbol by its name.
106+ * @param symbolName: The Name of the symbol to look for.
107+ * @param outSymbolOff: (optional) A Pointer to variable where resulting relative displacement of the symbol will be saved if found.
108+ * @returns true if the symbol was found, false otherwise.
109+ * @note Symbol lookup may fail for various reasons, such as the absence of a symbol table or the symbol not being present in the symbol table.
110+ */
25111bool ElfLookupSymbol (ElfPack libMap, const std::string& symbolName, uint64_t * outSymbolOff = nullptr );
0 commit comments