| 1 |
#ifndef ELF_HPP |
| 2 |
#define ELF_HPP |
| 3 |
|
| 4 |
#include "cpptrace/forward.hpp" |
| 5 |
#include "utils/common.hpp" |
| 6 |
#include "utils/io/base_file.hpp" |
| 7 |
#include "utils/span.hpp" |
| 8 |
#include "utils/utils.hpp" |
| 9 |
|
| 10 |
#if IS_LINUX |
| 11 |
|
| 12 |
#include <cstdint> |
| 13 |
#include <string> |
| 14 |
#include <unordered_map> |
| 15 |
|
| 16 |
CPPTRACE_BEGIN_NAMESPACE |
| 17 |
namespace detail { |
| 18 |
// TODO: make methods const and a bunch of members mutable |
| 19 |
class elf { |
| 20 |
std::unique_ptr<base_file> file; |
| 21 |
bool is_little_endian; |
| 22 |
bool is_64; |
| 23 |
|
| 24 |
struct header_info { |
| 25 |
uint64_t e_phoff; |
| 26 |
uint32_t e_phnum; |
| 27 |
uint32_t e_phentsize; |
| 28 |
uint64_t e_shoff; |
| 29 |
uint32_t e_shnum; |
| 30 |
uint32_t e_shentsize; |
| 31 |
uint16_t e_shstrndx; |
| 32 |
}; |
| 33 |
bool tried_to_load_header = false; |
| 34 |
optional<header_info> header; |
| 35 |
|
| 36 |
struct section_info { |
| 37 |
uint32_t sh_name; |
| 38 |
uint32_t sh_type; |
| 39 |
uint64_t sh_addr; |
| 40 |
uint64_t sh_offset; |
| 41 |
uint64_t sh_size; |
| 42 |
uint64_t sh_entsize; |
| 43 |
uint32_t sh_link; |
| 44 |
}; |
| 45 |
bool tried_to_load_sections = false; |
| 46 |
bool did_load_sections = false; |
| 47 |
std::vector<section_info> sections; |
| 48 |
|
| 49 |
struct strtab_entry { |
| 50 |
bool tried_to_load_strtab = false; |
| 51 |
bool did_load_strtab = false; |
| 52 |
std::vector<char> data; |
| 53 |
}; |
| 54 |
std::unordered_map<std::size_t, strtab_entry> strtab_entries; |
| 55 |
|
| 56 |
struct symtab_entry { |
| 57 |
uint32_t st_name; |
| 58 |
unsigned char st_info; |
| 59 |
unsigned char st_other; |
| 60 |
uint16_t st_shndx; |
| 61 |
uint64_t st_value; |
| 62 |
uint64_t st_size; |
| 63 |
}; |
| 64 |
struct symtab_info { |
| 65 |
std::vector<symtab_entry> entries; |
| 66 |
std::size_t strtab_link = 0; |
| 67 |
}; |
| 68 |
bool tried_to_load_symtab = false; |
| 69 |
bool did_load_symtab = false; |
| 70 |
optional<symtab_info> symtab; |
| 71 |
|
| 72 |
bool tried_to_load_dynamic_symtab = false; |
| 73 |
bool did_load_dynamic_symtab = false; |
| 74 |
optional<symtab_info> dynamic_symtab; |
| 75 |
|
| 76 |
elf(std::unique_ptr<base_file> file, bool is_little_endian, bool is_64); |
| 77 |
|
| 78 |
static NODISCARD Result<elf, internal_error> open(std::unique_ptr<base_file> file); |
| 79 |
|
| 80 |
public: |
| 81 |
static NODISCARD Result<elf, internal_error> open(cstring_view object_path); |
| 82 |
static NODISCARD Result<elf, internal_error> open(cbspan object); |
| 83 |
|
| 84 |
elf(elf&&) = default; |
| 85 |
|
| 86 |
public: |
| 87 |
Result<std::uintptr_t, internal_error> get_module_image_base(); |
| 88 |
private: |
| 89 |
template<std::size_t Bits> |
| 90 |
Result<std::uintptr_t, internal_error> get_module_image_base_impl(); |
| 91 |
|
| 92 |
public: |
| 93 |
optional<std::string> lookup_symbol(frame_ptr pc); |
| 94 |
private: |
| 95 |
optional<std::string> lookup_symbol(frame_ptr pc, const optional<symtab_info>& maybe_symtab); |
| 96 |
|
| 97 |
public: |
| 98 |
struct pc_range { |
| 99 |
frame_ptr low; |
| 100 |
frame_ptr high; // not inclusive |
| 101 |
}; |
| 102 |
// for in-memory JIT elves |
| 103 |
Result<std::vector<pc_range>, internal_error> get_pc_ranges(); |
| 104 |
|
| 105 |
struct symbol_entry { |
| 106 |
std::string st_name; |
| 107 |
uint16_t st_shndx; |
| 108 |
uint64_t st_value; |
| 109 |
uint64_t st_size; |
| 110 |
}; |
| 111 |
Result<optional<std::vector<symbol_entry>>, internal_error> get_symtab_entries(); |
| 112 |
Result<optional<std::vector<symbol_entry>>, internal_error> get_dynamic_symtab_entries(); |
| 113 |
private: |
| 114 |
Result<optional<std::vector<symbol_entry>>, internal_error> resolve_symtab_entries( |
| 115 |
const Result<const optional<symtab_info> &, internal_error>& |
| 116 |
); |
| 117 |
|
| 118 |
private: |
| 119 |
template<typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> |
| 120 |
T byteswap_if_needed(T value); |
| 121 |
|
| 122 |
Result<const header_info&, internal_error> get_header_info(); |
| 123 |
template<std::size_t Bits> |
| 124 |
Result<const header_info&, internal_error> get_header_info_impl(); |
| 125 |
|
| 126 |
Result<const std::vector<section_info>&, internal_error> get_sections(); |
| 127 |
template<std::size_t Bits> |
| 128 |
Result<const std::vector<section_info>&, internal_error> get_sections_impl(); |
| 129 |
|
| 130 |
Result<const std::vector<char>&, internal_error> get_strtab(std::size_t index); |
| 131 |
|
| 132 |
Result<const optional<symtab_info>&, internal_error> get_symtab(); |
| 133 |
Result<const optional<symtab_info>&, internal_error> get_dynamic_symtab(); |
| 134 |
template<std::size_t Bits> |
| 135 |
Result<optional<symtab_info>, internal_error> get_symtab_impl(bool dynamic); |
| 136 |
}; |
| 137 |
|
| 138 |
NODISCARD Result<maybe_owned<elf>, internal_error> open_elf_cached(const std::string& object_path); |
| 139 |
} |
| 140 |
CPPTRACE_END_NAMESPACE |
| 141 |
|
| 142 |
#endif |
| 143 |
|
| 144 |
#endif |