| 1 |
#ifndef MACHO_HPP |
| 2 |
#define MACHO_HPP |
| 3 |
|
| 4 |
#include "utils/common.hpp" |
| 5 |
#include "utils/utils.hpp" |
| 6 |
#include "utils/span.hpp" |
| 7 |
#include "utils/io/base_file.hpp" |
| 8 |
|
| 9 |
#if IS_APPLE |
| 10 |
|
| 11 |
#include <cstdint> |
| 12 |
#include <limits> |
| 13 |
#include <memory> |
| 14 |
#include <string> |
| 15 |
#include <unordered_map> |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
#include <mach-o/arch.h> |
| 19 |
#include <mach-o/loader.h> |
| 20 |
#include <mach-o/nlist.h> |
| 21 |
|
| 22 |
CPPTRACE_BEGIN_NAMESPACE |
| 23 |
namespace detail { |
| 24 |
bool file_is_mach_o(cstring_view object_path) noexcept; |
| 25 |
|
| 26 |
struct load_command_entry { |
| 27 |
std::uint32_t file_offset; |
| 28 |
std::uint32_t cmd; |
| 29 |
std::uint32_t cmdsize; |
| 30 |
}; |
| 31 |
|
| 32 |
class mach_o { |
| 33 |
public: |
| 34 |
struct debug_map_entry { |
| 35 |
uint64_t source_address; |
| 36 |
uint64_t size; |
| 37 |
std::string name; |
| 38 |
}; |
| 39 |
|
| 40 |
struct symbol_entry { |
| 41 |
uint64_t address; |
| 42 |
std::string name; |
| 43 |
}; |
| 44 |
|
| 45 |
// map from object file to a vector of symbols to resolve |
| 46 |
using debug_map = std::unordered_map<std::string, std::vector<debug_map_entry>>; |
| 47 |
|
| 48 |
private: |
| 49 |
std::unique_ptr<base_file> file; |
| 50 |
std::uint32_t magic; |
| 51 |
cpu_type_t cputype; |
| 52 |
cpu_subtype_t cpusubtype; |
| 53 |
std::uint32_t filetype; |
| 54 |
std::uint32_t n_load_commands; |
| 55 |
std::uint32_t sizeof_load_commands; |
| 56 |
std::uint32_t flags; |
| 57 |
std::size_t bits = 0; // 32 or 64 once load_mach is called |
| 58 |
|
| 59 |
std::size_t load_base = 0; |
| 60 |
std::size_t fat_index = std::numeric_limits<std::size_t>::max(); |
| 61 |
|
| 62 |
std::vector<load_command_entry> load_commands; |
| 63 |
|
| 64 |
struct symtab_info_data { |
| 65 |
symtab_command symtab; |
| 66 |
optional<std::vector<char>> stringtab; |
| 67 |
Result<const char*, internal_error> get_string(std::size_t index) const; |
| 68 |
}; |
| 69 |
|
| 70 |
bool tried_to_load_symtab = false; |
| 71 |
optional<symtab_info_data> symtab_info; |
| 72 |
|
| 73 |
bool tried_to_load_symbols = false; |
| 74 |
optional<std::vector<symbol_entry>> symbols; |
| 75 |
|
| 76 |
mach_o(std::unique_ptr<base_file> file, std::uint32_t magic) : file(std::move(file)), magic(magic) {} |
| 77 |
|
| 78 |
Result<monostate, internal_error> load(); |
| 79 |
|
| 80 |
static NODISCARD Result<mach_o, internal_error> open(std::unique_ptr<base_file> file); |
| 81 |
|
| 82 |
public: |
| 83 |
static NODISCARD Result<mach_o, internal_error> open(cstring_view object_path); |
| 84 |
static NODISCARD Result<mach_o, internal_error> open(cbspan object); |
| 85 |
|
| 86 |
mach_o(mach_o&&) = default; |
| 87 |
~mach_o() = default; |
| 88 |
|
| 89 |
Result<std::uintptr_t, internal_error> get_text_vmaddr(); |
| 90 |
|
| 91 |
std::size_t get_fat_index() const; |
| 92 |
|
| 93 |
void print_segments() const; |
| 94 |
|
| 95 |
struct pc_range { |
| 96 |
frame_ptr low; |
| 97 |
frame_ptr high; // not inclusive |
| 98 |
}; |
| 99 |
// for in-memory JIT mach-o's |
| 100 |
Result<std::vector<pc_range>, internal_error> get_pc_ranges(); |
| 101 |
|
| 102 |
Result<std::reference_wrapper<optional<symtab_info_data>>, internal_error> get_symtab_info(); |
| 103 |
|
| 104 |
void print_symbol_table_entry( |
| 105 |
const nlist_64& entry, |
| 106 |
const char* stringtab, |
| 107 |
std::size_t stringsize, |
| 108 |
std::size_t j |
| 109 |
) const; |
| 110 |
|
| 111 |
void print_symbol_table(); |
| 112 |
|
| 113 |
// produce information similar to dsymutil -dump-debug-map |
| 114 |
Result<debug_map, internal_error> get_debug_map(); |
| 115 |
|
| 116 |
Result<const std::vector<symbol_entry>&, internal_error> symbol_table(); |
| 117 |
|
| 118 |
optional<std::string> lookup_symbol(frame_ptr pc); |
| 119 |
|
| 120 |
// produce information similar to dsymutil -dump-debug-map |
| 121 |
static void print_debug_map(const debug_map& debug_map); |
| 122 |
|
| 123 |
private: |
| 124 |
template<std::size_t Bits> |
| 125 |
Result<monostate, internal_error> load_mach(); |
| 126 |
|
| 127 |
Result<monostate, internal_error> load_fat_mach(); |
| 128 |
|
| 129 |
template<std::size_t Bits> |
| 130 |
Result<segment_command_64, internal_error> load_segment_command(std::uint32_t offset) const; |
| 131 |
|
| 132 |
Result<symtab_command, internal_error> load_symbol_table_command(std::uint32_t offset) const; |
| 133 |
|
| 134 |
template<std::size_t Bits> |
| 135 |
Result<nlist_64, internal_error> load_symtab_entry(std::uint32_t symbol_base, std::size_t index) const; |
| 136 |
|
| 137 |
Result<std::vector<char>, internal_error> load_string_table(std::uint32_t offset, std::uint32_t byte_count) const; |
| 138 |
|
| 139 |
bool should_swap() const; |
| 140 |
}; |
| 141 |
|
| 142 |
Result<bool, internal_error> macho_is_fat(cstring_view object_path); |
| 143 |
|
| 144 |
NODISCARD Result<maybe_owned<mach_o>, internal_error> open_mach_o_cached(const std::string& object_path); |
| 145 |
} |
| 146 |
CPPTRACE_END_NAMESPACE |
| 147 |
|
| 148 |
#endif |
| 149 |
|
| 150 |
#endif |