Skip to content

Commit

Permalink
fix(win/modules): iterate_symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
Curve committed May 5, 2024
1 parent ffe4f05 commit 2ad495e
Showing 1 changed file with 9 additions and 27 deletions.
36 changes: 9 additions & 27 deletions src/module.win.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
#include "module.hpp"

#include <windows.h>

#include <imagehlp.h>
#include <psapi.h>

#include <functional>
Expand Down Expand Up @@ -195,43 +193,27 @@ namespace lime

void module::impl::iterate_symbols(const module::impl::callback_t &callback) const
{
char path[MAX_PATH];
GetModuleFileNameA(handle, path, MAX_PATH);

_LOADED_IMAGE image;

if (!MapAndLoad(path, nullptr, &image, TRUE, TRUE))
{
return;
}
const auto base = reinterpret_cast<std::uintptr_t>(info.lpBaseOfDll);

ULONG size{};
const auto *export_directory = reinterpret_cast<_IMAGE_EXPORT_DIRECTORY *>(
ImageDirectoryEntryToData(image.MappedAddress, false, IMAGE_DIRECTORY_ENTRY_EXPORT, &size));
auto *header = reinterpret_cast<IMAGE_DOS_HEADER *>(base);
auto *nt_header = reinterpret_cast<IMAGE_NT_HEADERS *>(base + header->e_lfanew);

if (!export_directory)
{
UnMapAndLoad(&image);
return;
}
auto *exports = reinterpret_cast<IMAGE_EXPORT_DIRECTORY *>(
base + nt_header->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);

const auto *names = reinterpret_cast<std::uintptr_t *>(
ImageRvaToVa(image.FileHeader, image.MappedAddress, export_directory->AddressOfNames, nullptr));
auto *names = reinterpret_cast<std::uint32_t *>(base + exports->AddressOfNames);

for (auto i = 0u; export_directory->NumberOfNames > i; i++)
for (DWORD i = 0; i < exports->NumberOfNames; i++)
{
const auto *address = ImageRvaToVa(image.FileHeader, image.MappedAddress, names[i], nullptr);
const auto *name = reinterpret_cast<const char *>(address);
const auto *current = reinterpret_cast<const char *>(base + names[i]);

if (!callback(name))
if (!callback(current))
{
continue;
}

break;
}

UnMapAndLoad(&image);
}

std::optional<module> module::impl::get(HMODULE module)
Expand Down

0 comments on commit 2ad495e

Please sign in to comment.