2021-01-07 13:37:06 +00:00
|
|
|
#include <Common/getHashOfLoadedBinary.h>
|
|
|
|
|
2022-06-10 08:22:31 +00:00
|
|
|
#if defined(OS_LINUX)
|
2021-01-07 13:37:06 +00:00
|
|
|
|
2021-01-07 03:01:15 +00:00
|
|
|
#include <link.h>
|
|
|
|
#include <array>
|
2023-03-06 14:53:58 +00:00
|
|
|
#include <base/hex.h>
|
2021-01-07 03:01:15 +00:00
|
|
|
|
|
|
|
|
|
|
|
static int callback(dl_phdr_info * info, size_t, void * data)
|
|
|
|
{
|
|
|
|
SipHash & hash = *reinterpret_cast<SipHash*>(data);
|
|
|
|
|
|
|
|
for (size_t header_index = 0; header_index < info->dlpi_phnum; ++header_index)
|
|
|
|
{
|
|
|
|
const auto & phdr = info->dlpi_phdr[header_index];
|
|
|
|
|
|
|
|
if (phdr.p_type == PT_LOAD && (phdr.p_flags & PF_X))
|
|
|
|
{
|
|
|
|
hash.update(phdr.p_filesz);
|
|
|
|
hash.update(reinterpret_cast<const char *>(info->dlpi_addr + phdr.p_vaddr), phdr.p_filesz);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1; /// Do not continue iterating.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
SipHash getHashOfLoadedBinary()
|
|
|
|
{
|
|
|
|
SipHash hash;
|
|
|
|
dl_iterate_phdr(callback, &hash);
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string getHashOfLoadedBinaryHex()
|
|
|
|
{
|
|
|
|
SipHash hash = getHashOfLoadedBinary();
|
2023-06-22 03:15:12 +00:00
|
|
|
const auto checksum = hash.get128();
|
2021-01-07 03:01:15 +00:00
|
|
|
return getHexUIntUppercase(checksum);
|
|
|
|
}
|
2021-01-07 13:37:06 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
SipHash getHashOfLoadedBinary()
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
std::string getHashOfLoadedBinaryHex()
|
|
|
|
{
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|