Coverage Report

Created: 2024-11-20 15:52

/root/doris/be/src/common/elf.h
Line
Count
Source (jump to first uncovered line)
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.h
19
// and modified by Doris
20
21
#pragma once
22
23
#if defined(__ELF__) && !defined(__FreeBSD__)
24
25
#include <elf.h>
26
#include <link.h>
27
28
#include <filesystem>
29
#include <functional>
30
#include <optional>
31
#include <string>
32
33
using ElfAddr = ElfW(Addr);
34
using ElfEhdr = ElfW(Ehdr);
35
using ElfOff = ElfW(Off);
36
using ElfPhdr = ElfW(Phdr);
37
using ElfShdr = ElfW(Shdr);
38
using ElfNhdr = ElfW(Nhdr);
39
using ElfSym = ElfW(Sym);
40
41
namespace doris {
42
43
/** Allow to navigate sections in ELF.
44
  */
45
class Elf final {
46
public:
47
    struct Section {
48
        const ElfShdr& header;
49
        [[nodiscard]] const char* name() const;
50
51
        [[nodiscard]] const char* begin() const;
52
        [[nodiscard]] const char* end() const;
53
        [[nodiscard]] size_t size() const;
54
55
        Section(const ElfShdr& header_, const Elf& elf_);
56
57
    private:
58
        const Elf& elf;
59
    };
60
61
    explicit Elf(const std::string& path);
62
    ~Elf();
63
64
    bool iterateSections(std::function<bool(const Section& section, size_t idx)>&& pred) const;
65
    std::optional<Section> findSection(
66
            std::function<bool(const Section& section, size_t idx)>&& pred) const;
67
    std::optional<Section> findSectionByName(const char* name) const;
68
69
0
    [[nodiscard]] const char* begin() const { return mapped; }
70
16.6M
    [[nodiscard]] const char* end() const { return mapped + elf_size; }
71
12
    [[nodiscard]] size_t size() const { return elf_size; }
72
73
    /// Obtain build id from SHT_NOTE of section headers (fallback to PT_NOTES section of program headers).
74
    /// Return empty string if does not exist.
75
    /// The string is returned in binary. Note that "readelf -n ./clickhouse-server" prints it in hex.
76
    [[nodiscard]] std::string getBuildID() const;
77
    static std::string getBuildID(const char* nhdr_pos, size_t size);
78
79
    /// Hash of the binary for integrity checks.
80
    [[nodiscard]] std::string getStoredBinaryHash() const;
81
82
private:
83
    int _fd = -1;
84
    std::filesystem::path _file;
85
    size_t elf_size;
86
    char* mapped = nullptr;
87
    const ElfEhdr* header = nullptr;
88
    const ElfShdr* section_headers = nullptr;
89
    const ElfPhdr* program_headers = nullptr;
90
    const char* section_names = nullptr;
91
};
92
93
} // namespace doris
94
95
#endif