blob: 5a6bd9e302dab638aacf096ab6649c2afc2a54da (
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
#pragma once
#if defined(__ELF__) && !defined(OS_FREEBSD)
#include <IO/MMapReadBufferFromFile.h>
#include <string>
#include <optional>
#include <functional>
#include <elf.h>
#include <link.h>
using ElfAddr = ElfW(Addr);
using ElfEhdr = ElfW(Ehdr);
using ElfOff = ElfW(Off);
using ElfPhdr = ElfW(Phdr);
using ElfShdr = ElfW(Shdr);
using ElfNhdr = ElfW(Nhdr);
using ElfSym = ElfW(Sym);
namespace DB
{
/** Allow to navigate sections in ELF.
*/
class Elf final
{
public:
struct Section
{
const ElfShdr & header;
const char * name() const;
const char * begin() const;
const char * end() const;
size_t size() const;
Section(const ElfShdr & header_, const Elf & elf_);
private:
const Elf & elf;
};
explicit Elf(const std::string & path);
bool iterateSections(std::function<bool(const Section & section, size_t idx)> && pred) const;
std::optional<Section> findSection(std::function<bool(const Section & section, size_t idx)> && pred) const;
std::optional<Section> findSectionByName(const char * name) const;
const char * begin() const { return mapped; }
const char * end() const { return mapped + elf_size; }
size_t size() const { return elf_size; }
/// Obtain build id from SHT_NOTE of section headers (fallback to PT_NOTES section of program headers).
/// Return empty string if does not exist.
/// The string is returned in binary. Note that "readelf -n ./clickhouse-server" prints it in hex.
String getBuildID() const;
static String getBuildID(const char * nhdr_pos, size_t size);
/// Hash of the binary for integrity checks.
String getStoredBinaryHash() const;
private:
MMapReadBufferFromFile in;
size_t elf_size;
const char * mapped;
const ElfEhdr * header;
const ElfShdr * section_headers;
const ElfPhdr * program_headers;
const char * section_names = nullptr;
};
}
#endif
|