Skip to content

Commit

Permalink
Add getVmtSection() method to dynamic library classes
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkrupinski committed Jul 13, 2023
1 parent 07d60d6 commit 81fd6aa
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
14 changes: 12 additions & 2 deletions Source/Platform/Linux/LinuxDynamicLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ class LinuxDynamicLibrary {
}

[[nodiscard]] MemorySection getCodeSection() const noexcept
{
return getSection(".text");
}

[[nodiscard]] MemorySection getVmtSection() const noexcept
{
return getSection(".data.rel.ro");
}

private:
[[nodiscard]] MemorySection getSection(const char* sectionName) const noexcept
{
void* base = nullptr;
std::size_t size = 0;
Expand All @@ -57,7 +68,7 @@ class LinuxDynamicLibrary {
for (auto i = 0; i < ehdr->e_shnum; ++i) {
const auto shdr = (ElfW(Shdr)*)(std::uintptr_t(shdrs) + i * ehdr->e_shentsize);

if (std::strcmp(strTab + shdr->sh_name, ".text") != 0)
if (std::strcmp(strTab + shdr->sh_name, sectionName) != 0)
continue;

base = (void*)(linkMap->l_addr + shdr->sh_offset);
Expand All @@ -75,7 +86,6 @@ class LinuxDynamicLibrary {
return MemorySection{ std::span{ reinterpret_cast<const std::byte*>(base), size } };
}

private:
[[nodiscard]] void* getModuleHandle(const char* libraryName)
{
const auto handle = PlatformApi::dlopen(libraryName, RTLD_LAZY | RTLD_NOLOAD);
Expand Down
9 changes: 9 additions & 0 deletions Source/Platform/Windows/PortableExecutable.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,15 @@ class PortableExecutable {
return {};
}

[[nodiscard]] MemorySection getVmtSection() const noexcept
{
for (const auto& section : getSectionHeaders()) {
if ((section.Characteristics & IMAGE_SCN_MEM_READ) != 0 && std::memcmp(section.Name, ".rdata", 6) == 0)
return MemorySection{ std::span{ base + section.VirtualAddress, section.Misc.VirtualSize } };
}
return {};
}

[[nodiscard]] SafeAddress getExport(const char* name) const noexcept
{
const auto exportDataDirectory = getExportDataDirectory();
Expand Down
7 changes: 7 additions & 0 deletions Source/Platform/Windows/WindowsDynamicLibrary.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ class WindowsDynamicLibrary {
return {};
}

[[nodiscard]] MemorySection getVmtSection() const noexcept
{
if (handle)
return portableExecutable().getVmtSection();
return {};
}

[[nodiscard]] HMODULE getHandle() const noexcept
{
return handle;
Expand Down

0 comments on commit 81fd6aa

Please sign in to comment.