diff --git a/src/Utilities/Linker.cpp b/src/Utilities/Linker.cpp new file mode 100644 index 0000000..03fd5fd --- /dev/null +++ b/src/Utilities/Linker.cpp @@ -0,0 +1,49 @@ +#include +#include +#include + +#include "Linker.h" + +struct dlinfo_t { + // Full path to shared library on filesystem. + const char* library = nullptr; + + // Absolute 'start address' in memory. + uintptr_t address = 0; + + // Size in memory - accuracy is questionable. + size_t size = 0; +}; + +std::vector libraries; + +bool Linker::GetLibraryInformation(const char* library, uintptr_t* address, size_t* size) { + if (libraries.size() == 0) { + dl_iterate_phdr([] (struct dl_phdr_info* info, size_t, void*) { + dlinfo_t library_info = {}; + + library_info.library = info->dlpi_name; + library_info.address = info->dlpi_addr + info->dlpi_phdr[0].p_vaddr; + library_info.size = info->dlpi_phdr[0].p_memsz; + + libraries.push_back(library_info); + + return 0; + }, nullptr); + } + + for (const dlinfo_t& current: libraries) { + if (!strcasestr(current.library, library)) + continue; + + if (address) + *address = current.address; + + if (size) + *size = current.size; + + return true; + } + + return false; +} \ No newline at end of file diff --git a/src/Utilities/Linker.h b/src/Utilities/Linker.h new file mode 100644 index 0000000..971581d --- /dev/null +++ b/src/Utilities/Linker.h @@ -0,0 +1,6 @@ +#pragma once + +namespace Linker { + // Iterate loaded shared libraries and optionally return base address and size. + bool GetLibraryInformation(const char*, uintptr_t* = nullptr, size_t* = nullptr); +} \ No newline at end of file