aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Common/getHashOfLoadedBinary.cpp
blob: 6487bcd4f1c178cc8a42361a1f159f4aa5b34c87 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <Common/getHashOfLoadedBinary.h>

#if defined(OS_LINUX)

#include <link.h>
#include <array>
#include <base/hex.h>


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();
    const auto checksum = hash.get128();
    return getHexUIntUppercase(checksum);
}

#else

SipHash getHashOfLoadedBinary()
{
    return {};
}


std::string getHashOfLoadedBinaryHex()
{
    return {};
}

#endif