25 const int fd = open(path.c_str(), O_RDONLY);
32 if (fstat(fd, &info) == 0 && info.st_size > 0)
34 void* map = mmap(
nullptr,
static_cast<size_t>(info.st_size), PROT_READ, MAP_PRIVATE, fd, 0);
35 if (map != MAP_FAILED)
37 _base =
static_cast<const uint8_t*
>(map);
38 _size =
static_cast<size_t>(info.st_size);
48 munmap(
const_cast<uint8_t*
>(_base), _size);
55 explicit operator bool()
const {
return _base !=
nullptr; }
58 const T*
At(
size_t offset,
size_t count = 1)
const
60 if (!_base || count == 0 || offset > _size || (_size - offset) /
sizeof(
T) < count)
64 return reinterpret_cast<const T*
>(_base + offset);
68 const uint8_t* _base =
nullptr;
74 const auto* header = elf.
At<Elf64_Ehdr>(0);
75 if (!header || std::memcmp(header->e_ident, ELFMAG, SELFMAG) != 0 || header->e_ident[EI_CLASS] != ELFCLASS64)
80 const auto* sections = elf.
At<Elf64_Shdr>(header->e_shoff, header->e_shnum);
81 if (!sections || header->e_shentsize !=
sizeof(Elf64_Shdr))
87 for (
const Elf64_Word wanted : {Elf64_Word{SHT_SYMTAB}, Elf64_Word{SHT_DYNSYM}})
89 for (uint16_t i = 0; i < header->e_shnum; ++i)
91 const Elf64_Shdr& section = sections[i];
92 if (section.sh_type != wanted || section.sh_entsize !=
sizeof(Elf64_Sym) ||
93 section.sh_link >= header->e_shnum)
98 const Elf64_Shdr& strings = sections[section.sh_link];
99 const auto* names = elf.
At<
char>(strings.sh_offset, strings.sh_size);
100 const auto* symbols = elf.
At<Elf64_Sym>(section.sh_offset, section.sh_size /
sizeof(Elf64_Sym));
101 if (!names || !symbols)
106 for (
size_t s = 0; s < section.sh_size /
sizeof(Elf64_Sym); ++s)
108 if (symbols[s].st_name >= strings.sh_size || symbols[s].st_value == 0)
112 if (symbol == names + symbols[s].st_name)
114 return symbols[s].st_value;
125 const std::string fileName = std::filesystem::path(module.
Path).filename().string();
131 if (module.
Path.empty())
143 return const_cast<uint8_t*
>(
module.Base + value + 2 * sizeof(void*));
148 std::vector<ScanRange> ranges;
161 const auto typeInfoVptr = [](
const char* symbol) -> uintptr_t {
162 void* table = dlsym(RTLD_DEFAULT, symbol);
163 return table ?
reinterpret_cast<uintptr_t
>(table) + 2 *
sizeof(
void*) : 0;
166 const TypeInfoKinds found{.SingleBase = typeInfoVptr(
"_ZTVN10__cxxabiv120__si_class_type_infoE"),
167 .MultipleBases = typeInfoVptr(
"_ZTVN10__cxxabiv121__vmi_class_type_infoE")};
178 return std::unexpected(
Error::NotFound(std::format(
"no vtable for '{}'", className)));
181 const void* typeInfo =
static_cast<void**
>(primary)[-1];
190 return std::unexpected(offset.error());
197 std::vector<ScanRange> ranges;
MappedFile(const std::string &path)
MappedFile(const MappedFile &)=delete
const T * At(size_t offset, size_t count=1) const
MappedFile & operator=(const MappedFile &)=delete
void * FindVirtualTableByTypeInfo(std::span< const ScanRange > ranges, const void *typeInfo, intptr_t offsetToTop)
bool FindModuleAndRanges(std::string_view fileName, Image &module, std::vector< ScanRange > &ranges)
static bool FindRanges(const Image &module, std::vector< ScanRange > &ranges)
@ NotFound
The named thing does not exist (no such player, convar, row).
std::expected< T, Error > Result
static uint64_t FindSymbolValue(const MappedFile &elf, const std::string &symbol)
Result< int > FindBaseOffsetByTypeInfo(const void *typeInfo, std::string_view baseName, const TypeInfoKinds &kinds)
Result< BaseSubobject > FindBaseIn(const Image &module, std::string_view className, std::string_view baseName)
static const TypeInfoKinds & ExportedTypeInfoKinds()
void * FindVirtualTableByTypeName(std::span< const ScanRange > ranges, std::string_view className)
std::string LengthPrefixedName(std::string_view className)
void * FindVirtualTableIn(const Image &module, std::string_view className)
static Error NotFound(std::string detail)