Coverage Report

Created: 2026-03-14 11:58

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
134
Elf::Elf(const std::string& path) {
40
134
    _file = path;
41
134
    std::error_code ec;
42
134
    elf_size = std::filesystem::file_size(_file, ec);
43
134
    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
134
    if (elf_size < sizeof(ElfEhdr)) {
49
0
        LOG(FATAL) << fmt::format("The size of supposedly ELF file '{}' is too small", path);
50
0
    }
51
134
    RETRY_ON_EINTR(_fd, open(_file.c_str(), O_RDONLY));
52
134
    if (_fd < 0) {
53
0
        LOG(FATAL) << fmt::format("failed to open {}", _file.native());
54
0
    }
55
134
    mapped = static_cast<char*>(mmap(nullptr, elf_size, PROT_READ, MAP_SHARED, _fd, 0));
56
134
    if (MAP_FAILED == mapped) {
57
0
        LOG(FATAL) << fmt::format("MMappedFileDescriptor: Cannot mmap {}, read from {}.", elf_size,
58
0
                                  path);
59
0
    }
60
61
134
    header = reinterpret_cast<const ElfEhdr*>(mapped);
62
63
134
    if (memcmp(header->e_ident,
64
134
               "\x7F"
65
134
               "ELF",
66
134
               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
134
    ElfOff section_header_offset = header->e_shoff;
72
134
    uint16_t section_header_num_entries = header->e_shnum;
73
74
134
    if (!section_header_offset || !section_header_num_entries ||
75
134
        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
134
    section_headers = reinterpret_cast<const ElfShdr*>(mapped + section_header_offset);
81
82
    /// The string table with section names.
83
4.71k
    auto section_names_strtab = findSection([&](const Section& section, size_t idx) {
84
4.71k
        return section.header.sh_type == SHT_STRTAB && header->e_shstrndx == idx;
85
4.71k
    });
86
87
134
    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
134
    ElfOff section_names_offset = section_names_strtab->header.sh_offset;
93
134
    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
134
    section_names = reinterpret_cast<const char*>(mapped + section_names_offset);
99
100
    /// Get program headers
101
102
134
    ElfOff program_header_offset = header->e_phoff;
103
134
    uint16_t program_header_num_entries = header->e_phnum;
104
105
134
    if (!program_header_offset || !program_header_num_entries ||
106
134
        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
134
    program_headers = reinterpret_cast<const ElfPhdr*>(mapped + program_header_offset);
111
134
}
112
113
122
Elf::~Elf() {
114
122
    if (mapped) {
115
122
        munmap(static_cast<void*>(mapped), elf_size);
116
122
    }
117
122
    if (_fd > 0) {
118
122
        int res = ::close(_fd);
119
122
        if (-1 == res) {
120
0
            LOG(WARNING) << fmt::format("failed to close {}", _file.native());
121
0
        }
122
122
        _fd = -1;
123
122
    }
124
122
}
125
126
62.1M
Elf::Section::Section(const ElfShdr& header_, const Elf& elf_) : header(header_), elf(elf_) {}
127
128
1.31M
bool Elf::iterateSections(std::function<bool(const Section& section, size_t idx)>&& pred) const {
129
62.1M
    for (size_t idx = 0; idx < header->e_shnum; ++idx) {
130
62.1M
        Section section(section_headers[idx], *this);
131
132
        /// Sections spans after end of file.
133
62.1M
        if (section.header.sh_offset + section.header.sh_size > elf_size) {
134
22
            continue;
135
22
        }
136
137
62.1M
        if (pred(section, idx)) {
138
1.30M
            return true;
139
1.30M
        }
140
62.1M
    }
141
2.95k
    return false;
142
1.31M
}
143
144
std::optional<Elf::Section> Elf::findSection(
145
1.31M
        std::function<bool(const Section& section, size_t idx)>&& pred) const {
146
1.31M
    std::optional<Elf::Section> result;
147
148
62.1M
    iterateSections([&](const Section& section, size_t idx) {
149
62.1M
        if (pred(section, idx)) {
150
1.30M
            result.emplace(section);
151
1.30M
            return true;
152
1.30M
        }
153
60.8M
        return false;
154
62.1M
    });
155
156
1.31M
    return result;
157
1.31M
}
158
159
1.31M
std::optional<Elf::Section> Elf::findSectionByName(const char* name) const {
160
1.31M
    return findSection(
161
62.1M
            [&](const Section& section, size_t) { return 0 == strcmp(name, section.name()); });
162
1.31M
}
163
164
134
std::string Elf::getBuildID() const {
165
    /// Section headers are the first choice for a debuginfo file
166
1.17k
    if (std::string build_id; iterateSections([&build_id](const Section& section, size_t) {
167
1.17k
            if (section.header.sh_type == SHT_NOTE) {
168
229
                build_id = Elf::getBuildID(section.begin(), section.size());
169
229
                if (!build_id.empty()) {
170
120
                    return true;
171
120
                }
172
229
            }
173
1.05k
            return false;
174
1.17k
        })) {
175
120
        return build_id;
176
120
    }
177
178
    /// fallback to PHDR
179
168
    for (size_t idx = 0; idx < header->e_phnum; ++idx) {
180
168
        const ElfPhdr& phdr = program_headers[idx];
181
182
168
        if (phdr.p_type == PT_NOTE) {
183
14
            return getBuildID(mapped + phdr.p_offset, phdr.p_filesz);
184
14
        }
185
168
    }
186
187
0
    return {};
188
14
}
189
190
#if defined(OS_SUNOS)
191
std::string Elf::getBuildID(const char* nhdr_pos, size_t size) {
192
    return {};
193
}
194
#else
195
374
std::string Elf::getBuildID(const char* nhdr_pos, size_t size) {
196
374
    const char* nhdr_end = nhdr_pos + size;
197
198
610
    while (nhdr_pos < nhdr_end) {
199
380
        ElfNhdr nhdr = unaligned_load<ElfNhdr>(nhdr_pos);
200
201
380
        nhdr_pos += sizeof(ElfNhdr) + nhdr.n_namesz;
202
380
        if (nhdr.n_type == NT_GNU_BUILD_ID) {
203
144
            const char* build_id = nhdr_pos;
204
144
            return {build_id, nhdr.n_descsz};
205
144
        }
206
236
        nhdr_pos += nhdr.n_descsz;
207
236
    }
208
209
230
    return {};
210
374
}
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
62.1M
const char* Elf::Section::name() const {
222
62.1M
    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
62.1M
    return elf.section_names + header.sh_name;
228
62.1M
}
229
230
1.30M
const char* Elf::Section::begin() const {
231
1.30M
    return elf.mapped + header.sh_offset;
232
1.30M
}
233
234
25
const char* Elf::Section::end() const {
235
25
    return begin() + size();
236
25
}
237
238
1.30M
size_t Elf::Section::size() const {
239
1.30M
    return header.sh_size;
240
1.30M
}
241
242
} // namespace doris
243
244
#endif