| 1 |
#include "binary/module_base.hpp" |
| 2 |
|
| 3 |
#include "platform/platform.hpp" |
| 4 |
#include "utils/utils.hpp" |
| 5 |
|
| 6 |
#include <string> |
| 7 |
#include <mutex> |
| 8 |
#include <unordered_map> |
| 9 |
|
| 10 |
#if IS_LINUX || IS_APPLE |
| 11 |
#include <unistd.h> |
| 12 |
#include <dlfcn.h> |
| 13 |
#if IS_APPLE |
| 14 |
#include "binary/mach-o.hpp" |
| 15 |
#else |
| 16 |
#include "binary/elf.hpp" |
| 17 |
#endif |
| 18 |
#elif IS_WINDOWS |
| 19 |
#include "binary/pe.hpp" |
| 20 |
#endif |
| 21 |
|
| 22 |
CPPTRACE_BEGIN_NAMESPACE |
| 23 |
namespace detail { |
| 24 |
#if IS_LINUX |
| 25 |
Result<std::uintptr_t, internal_error> get_module_image_base(const std::string& object_path) { |
| 26 |
static std::mutex mutex; |
| 27 |
std::lock_guard<std::mutex> lock(mutex); |
| 28 |
static std::unordered_map<std::string, std::uintptr_t> cache; |
| 29 |
auto it = cache.find(object_path); |
| 30 |
if(it == cache.end()) { |
| 31 |
// arguably it'd be better to release the lock while computing this, but also arguably it's good to not |
| 32 |
// have two threads try to do the same computation |
| 33 |
auto elf_object = open_elf_cached(object_path); |
| 34 |
// TODO: Cache the error |
| 35 |
if(!elf_object) { |
| 36 |
return elf_object.unwrap_error(); |
| 37 |
} |
| 38 |
auto base = elf_object.unwrap_value()->get_module_image_base(); |
| 39 |
if(base.is_error()) { |
| 40 |
return std::move(base).unwrap_error(); |
| 41 |
} |
| 42 |
cache.insert(it, {object_path, base.unwrap_value()}); |
| 43 |
return base; |
| 44 |
} else { |
| 45 |
return it->second; |
| 46 |
} |
| 47 |
} |
| 48 |
#elif IS_APPLE |
| 49 |
Result<std::uintptr_t, internal_error> get_module_image_base(const std::string& object_path) { |
| 50 |
// We have to parse the Mach-O to find the offset of the text section..... |
| 51 |
// I don't know how addresses are handled if there is more than one __TEXT load command. I'm assuming for |
| 52 |
// now that there is only one, and I'm using only the first section entry within that load command. |
| 53 |
static std::mutex mutex; |
| 54 |
std::lock_guard<std::mutex> lock(mutex); |
| 55 |
static std::unordered_map<std::string, std::uintptr_t> cache; |
| 56 |
auto it = cache.find(object_path); |
| 57 |
if(it == cache.end()) { |
| 58 |
// arguably it'd be better to release the lock while computing this, but also arguably it's good to not |
| 59 |
// have two threads try to do the same computation |
| 60 |
auto mach_o_object = open_mach_o_cached(object_path); |
| 61 |
// TODO: Cache the error |
| 62 |
if(!mach_o_object) { |
| 63 |
return mach_o_object.unwrap_error(); |
| 64 |
} |
| 65 |
auto base = mach_o_object.unwrap_value()->get_text_vmaddr(); |
| 66 |
if(!base) { |
| 67 |
return std::move(base).unwrap_error(); |
| 68 |
} |
| 69 |
cache.insert(it, {object_path, base.unwrap_value()}); |
| 70 |
return base; |
| 71 |
} else { |
| 72 |
return it->second; |
| 73 |
} |
| 74 |
} |
| 75 |
#else // Windows |
| 76 |
Result<std::uintptr_t, internal_error> get_module_image_base(const std::string& object_path) { |
| 77 |
static std::mutex mutex; |
| 78 |
std::lock_guard<std::mutex> lock(mutex); |
| 79 |
static std::unordered_map<std::string, std::uintptr_t> cache; |
| 80 |
auto it = cache.find(object_path); |
| 81 |
if(it == cache.end()) { |
| 82 |
// arguably it'd be better to release the lock while computing this, but also arguably it's good to not |
| 83 |
// have two threads try to do the same computation |
| 84 |
auto base = pe_get_module_image_base(object_path); |
| 85 |
// TODO: Cache the error |
| 86 |
if(!base) { |
| 87 |
return std::move(base).unwrap_error(); |
| 88 |
} |
| 89 |
cache.insert(it, {object_path, base.unwrap_value()}); |
| 90 |
return base; |
| 91 |
} else { |
| 92 |
return it->second; |
| 93 |
} |
| 94 |
} |
| 95 |
#endif |
| 96 |
} |
| 97 |
CPPTRACE_END_NAMESPACE |