aboutsummaryrefslogtreecommitdiffstats
path: root/tools/fix_elf/patch.h
blob: de5b1a3b8cfcc73ac649b5da54c6e896e97c0e22 (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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#pragma once

#include "elf.h"

#include <util/generic/string.h>
#include <util/stream/file.h>
#include <util/system/filemap.h>

namespace NElf {

template<typename TTo, typename TFrom>
inline TTo Offset(TFrom from, size_t off) {
    return reinterpret_cast<TTo>(reinterpret_cast<char*>(from) + off);
}

bool IsElf(const TString& path);

class TElf {
public:
    TElf(const TString& path)
        : Map(path, TFileMap::oRdWr)
    {
        Map.Map(0, Map.Length());
        Begin = reinterpret_cast<char*>(Map.Ptr());

        if (Map.Length() < static_cast<i64>(sizeof(Elf64_Ehdr)) || TStringBuf(Begin, SELFMAG) != ELFMAG) { 
            ythrow yexception() << path << " is not an ELF file";
        }
    }

    Elf64_Ehdr* GetHeader() const noexcept {
        return reinterpret_cast<Elf64_Ehdr*>(Begin);
    }

    char* GetPtr(size_t offset = 0) const noexcept {
        return Begin + offset;
    }

    Elf64_Shdr* GetSectionByType(Elf64_Word type) const {
        Elf64_Shdr* r = nullptr;

        for (Elf64_Shdr* p = GetSectionBegin(), *end = GetSectionEnd(); p != end; ++p) {
            if (p->sh_type == type) {
                if (r) {
                    ythrow yexception() << "More than one section of type " << type << Endl;
                }

                r = p;
            }
        }

        return r;
    }

    size_t GetSectionCount() const noexcept {
        size_t count = GetHeader()->e_shnum;
        if (count == 0) {
            count = GetSection(0)->sh_size;
        }

        return count;
    }

    Elf64_Shdr* GetSectionBegin() const noexcept {
        return reinterpret_cast<Elf64_Shdr*>(Begin + GetHeader()->e_shoff);
    }

    Elf64_Shdr* GetSectionEnd() const noexcept {
        return reinterpret_cast<Elf64_Shdr*>(Begin + GetHeader()->e_shoff) + GetSectionCount();
    }

    Elf64_Shdr* GetSection(size_t i) const noexcept {
        return GetSectionBegin() + i;
    }

    Elf64_Shdr* GetSectionsNameSection() const noexcept {
        size_t index = GetHeader()->e_shstrndx;
        if (index == SHN_XINDEX) {
            index = GetSection(0)->sh_link;
        }
        return GetSection(index);
    }

private:
    TFileMap Map;
    char* Begin;
};

class TSection {
public:
    TSection(TElf* elf, Elf64_Shdr* this_)
        : Elf(elf)
        , This(this_)
    {
    }

    bool IsNull() const noexcept {
        return !This;
    }

    char* GetPtr(size_t offset = 0) const noexcept {
        return Elf->GetPtr(This->sh_offset) + offset;
    }

    TStringBuf GetStr(size_t offset) const noexcept {
        return GetPtr(offset);
    }

    TStringBuf GetName() const noexcept {
        return TSection{Elf, Elf->GetSectionsNameSection()}.GetPtr(This->sh_name);
    }

    size_t GetLink() const noexcept {
        return This->sh_link;
    }

    size_t GetSize() const noexcept {
        return This->sh_size;
    }

    size_t GetEntryCount() const noexcept {
        return GetSize() / This->sh_entsize;
    }

    template<typename TTo = char>
    TTo* GetEntry(size_t i) const noexcept {
        return reinterpret_cast<TTo*>(GetPtr(i * This->sh_entsize));
    }

private:
    TElf* Elf;
    Elf64_Shdr* This;
};

class TVerneedSection : public TSection {
public:
    TVerneedSection(TElf* elf)
        : TSection(elf, elf->GetSectionByType(SHT_GNU_verneed))
    {
    }

    Elf64_Verneed* GetFirstVerneed() const noexcept {
        if (!GetSize()) {
            return nullptr;
        }

        return reinterpret_cast<Elf64_Verneed*>(GetPtr());
    }

    Elf64_Verneed* GetNextVerneed(Elf64_Verneed* v) const noexcept {
        if (!v->vn_next) {
            return nullptr;
        }

        return Offset<Elf64_Verneed*>(v, v->vn_next);
    }

    Elf64_Vernaux* GetFirstVernaux(Elf64_Verneed* v) const noexcept {
        if (!v->vn_cnt) {
            return nullptr;
        }

        return Offset<Elf64_Vernaux*>(v, v->vn_aux);
    }

    Elf64_Vernaux* GetNextVernaux(Elf64_Vernaux* v) const noexcept {
        if (!v->vna_next) {
            return nullptr;
        }

        return Offset<Elf64_Vernaux*>(v, v->vna_next);
    }
};

}