Coverage Report

Created: 2026-03-30 11:02

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/common/elf.cpp
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
// This file is copied from
18
// https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/Elf.cpp
19
// and modified by Doris
20
21
#if defined(__ELF__) && !defined(__FreeBSD__)
22
23
#include "common/elf.h"
24
25
#include <fcntl.h>
26
#include <fmt/format.h>
27
#include <sys/mman.h>
28
#include <unistd.h>
29
30
#include <cstring>
31
#include <system_error>
32
33
#include "common/logging.h"
34
#include "common/macros.h"
35
#include "util/unaligned.h"
36
37
namespace doris {
38
39
110
Elf::Elf(const std::string& path) {
40
110
    _file = path;
41
110
    std::error_code ec;
42
110
    elf_size = std::filesystem::file_size(_file, ec);
43
110
    if (ec) {
44
0
        LOG(FATAL) << fmt::format("failed to get file size {}: ({}), {}", _file.native(),
45
0
                                  ec.value(), ec.message());
46
0
    }
47
    /// Check if it's an elf.
48
110
    if (elf_size < sizeof(ElfEhdr)) {
49
0
        LOG(FATAL) << fmt::format("The size of supposedly ELF file '{}' is too small", path);
50
0
    }
51
110
    RETRY_ON_EINTR(_fd, open(_file.c_str(), O_RDONLY));
52
110
    if (_fd < 0) {
53
0
        LOG(FATAL) << fmt::format("failed to open {}", _file.native());
54
0
    }
55
110
    mapped = static_cast<char*>(mmap(nullptr, elf_size, PROT_READ, MAP_SHARED, _fd, 0));
56
110
    if (MAP_FAILED == mapped) {
57
0
        LOG(FATAL) << fmt::format("MMappedFileDescriptor: Cannot mmap {}, read from {}.", elf_size,
58
0
                                  path);
59
0
    }
60
61
110
    header = reinterpret_cast<const ElfEhdr*>(mapped);
62
63
110
    if (memcmp(header->e_ident,
64
110
               "\x7F"
65
110
               "ELF",
66
110
               4) != 0) {
67
0
        LOG(FATAL) << fmt::format("The file '{}' is not ELF according to magic", path);
68
0
    }
69
70
    /// Get section header.
71
110
    ElfOff section_header_offset = header->e_shoff;
72
110
    uint16_t section_header_num_entries = header->e_shnum;
73
74
110
    if (!section_header_offset || !section_header_num_entries ||
75
110
        section_header_offset + section_header_num_entries * sizeof(ElfShdr) > elf_size) {
76
0
        LOG(FATAL) << fmt::format(
77
0
                "The ELF '{}' is truncated (section header points after end of file)", path);
78
0
    }
79
80
110
    section_headers = reinterpret_cast<const ElfShdr*>(mapped + section_header_offset);
81
82
    /// The string table with section names.
83
3.89k
    auto section_names_strtab = findSection([&](const Section& section, size_t idx) {
84
3.89k
        return section.header.sh_type == SHT_STRTAB && header->e_shstrndx == idx;
85
3.89k
    });
86
87
110
    if (!section_names_strtab) {
88
0
        LOG(FATAL) << fmt::format("The ELF '{}' doesn't have string table with section names",
89
0
                                  path);
90
0
    }
91
92
110
    ElfOff section_names_offset = section_names_strtab->header.sh_offset;
93
110
    if (section_names_offset >= elf_size) {
94
0
        LOG(FATAL) << fmt::format(
95
0
                "The ELF '{}' is truncated (section names string table points after end of file)",
96
0
                path);
97
0
    }
98
110
    section_names = reinterpret_cast<const char*>(mapped + section_names_offset);
99
100
    /// Get program headers
101
102
110
    ElfOff program_header_offset = header->e_phoff;
103
110
    uint16_t program_header_num_entries = header->e_phnum;
104
105
110
    if (!program_header_offset || !program_header_num_entries ||
106
110
        program_header_offset + program_header_num_entries * sizeof(ElfPhdr) > elf_size) {
107
0
        LOG(FATAL) << fmt::format(
108
0
                "The ELF '{}' is truncated (program header points after end of file)", path);
109
0
    }
110
110
    program_headers = reinterpret_cast<const ElfPhdr*>(mapped + program_header_offset);
111
110
}
112
113
102
Elf::~Elf() {
114
102
    if (mapped) {
115
102
        munmap(static_cast<void*>(mapped), elf_size);
116
102
    }
117
102
    if (_fd > 0) {
118
102
        int res = ::close(_fd);
119
102
        if (-1 == res) {
120
0
            LOG(WARNING) << fmt::format("failed to close {}", _file.native());
121
0
        }
122
102
        _fd = -1;
123
102
    }
124
102
}
125
126
1.01M
Elf::Section::Section(const ElfShdr& header_, const Elf& elf_) : header(header_), elf(elf_) {}
127
128
19.7k
bool Elf::iterateSections(std::function<bool(const Section& section, size_t idx)>&& pred) const {
129
1.01M
    for (size_t idx = 0; idx < header->e_shnum; ++idx) {
130
1.01M
        Section section(section_headers[idx], *this);
131
132
        /// Sections spans after end of file.
133
1.01M
        if (section.header.sh_offset + section.header.sh_size > elf_size) {
134
18
            continue;
135
18
        }
136
137
1.01M
        if (pred(section, idx)) {
138
16.6k
            return true;
139
16.6k
        }
140
1.01M
    }
141
3.05k
    return false;
142
19.7k
}
143
144
std::optional<Elf::Section> Elf::findSection(
145
19.5k
        std::function<bool(const Section& section, size_t idx)>&& pred) const {
146
19.5k
    std::optional<Elf::Section> result;
147
148
1.00M
    iterateSections([&](const Section& section, size_t idx) {
149
1.00M
        if (pred(section, idx)) {
150
16.5k
            result.emplace(section);
151
16.5k
            return true;
152
16.5k
        }
153
992k
        return false;
154
1.00M
    });
155
156
19.5k
    return result;
157
19.5k
}
158
159
19.4k
std::optional<Elf::Section> Elf::findSectionByName(const char* name) const {
160
19.4k
    return findSection(
161
1.00M
            [&](const Section& section, size_t) { return 0 == strcmp(name, section.name()); });
162
19.4k
}
163
164
110
std::string Elf::getBuildID() const {
165
    /// Section headers are the first choice for a debuginfo file
166
998
    if (std::string build_id; iterateSections([&build_id](const Section& section, size_t) {
167
998
            if (section.header.sh_type == SHT_NOTE) {
168
186
                build_id = Elf::getBuildID(section.begin(), section.size());
169
186
                if (!build_id.empty()) {
170
98
                    return true;
171
98
                }
172
186
            }
173
900
            return false;
174
998
        })) {
175
98
        return build_id;
176
98
    }
177
178
    /// fallback to PHDR
179
144
    for (size_t idx = 0; idx < header->e_phnum; ++idx) {
180
144
        const ElfPhdr& phdr = program_headers[idx];
181
182
144
        if (phdr.p_type == PT_NOTE) {
183
12
            return getBuildID(mapped + phdr.p_offset, phdr.p_filesz);
184
12
        }
185
144
    }
186
187
0
    return {};
188
12
}
189
190
#if defined(OS_SUNOS)
191
std::string Elf::getBuildID(const char* nhdr_pos, size_t size) {
192
    return {};
193
}
194
#else
195
305
std::string Elf::getBuildID(const char* nhdr_pos, size_t size) {
196
305
    const char* nhdr_end = nhdr_pos + size;
197
198
496
    while (nhdr_pos < nhdr_end) {
199
310
        ElfNhdr nhdr = unaligned_load<ElfNhdr>(nhdr_pos);
200
201
310
        nhdr_pos += sizeof(ElfNhdr) + nhdr.n_namesz;
202
310
        if (nhdr.n_type == NT_GNU_BUILD_ID) {
203
119
            const char* build_id = nhdr_pos;
204
119
            return {build_id, nhdr.n_descsz};
205
119
        }
206
191
        nhdr_pos += nhdr.n_descsz;
207
191
    }
208
209
186
    return {};
210
305
}
211
#endif // OS_SUNOS
212
213
0
std::string Elf::getStoredBinaryHash() const {
214
0
    if (auto section = findSectionByName(".clickhouse.hash")) {
215
0
        return {section->begin(), section->end()};
216
0
    } else {
217
0
        return {};
218
0
    }
219
0
}
220
221
1.00M
const char* Elf::Section::name() const {
222
1.00M
    if (!elf.section_names) {
223
0
        LOG(FATAL) << fmt::format("Section names are not initialized");
224
0
    }
225
226
    /// TODO buffer overflow is possible, we may need to check strlen.
227
1.00M
    return elf.section_names + header.sh_name;
228
1.00M
}
229
230
16.6k
const char* Elf::Section::begin() const {
231
16.6k
    return elf.mapped + header.sh_offset;
232
16.6k
}
233
234
22
const char* Elf::Section::end() const {
235
22
    return begin() + size();
236
22
}
237
238
16.6k
size_t Elf::Section::size() const {
239
16.6k
    return header.sh_size;
240
16.6k
}
241
242
} // namespace doris
243
244
#endif