summaryrefslogtreecommitdiff
path: root/src/patchelf.h
blob: 4e229d673f16a5eacea3beea25d51a79ecb40555 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
#include <map>
#include <memory>
#include <optional>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>

#include "elf.h"

using FileContents = std::shared_ptr<std::vector<unsigned char>>;

#define ElfFileParams class Elf_Ehdr, class Elf_Phdr, class Elf_Shdr, class Elf_Addr, class Elf_Off, class Elf_Dyn, class Elf_Sym, class Elf_Versym, class Elf_Verdef, class Elf_Verdaux, class Elf_Verneed, class Elf_Vernaux, class Elf_Rel, class Elf_Rela, unsigned ElfClass
#define ElfFileParamNames Elf_Ehdr, Elf_Phdr, Elf_Shdr, Elf_Addr, Elf_Off, Elf_Dyn, Elf_Sym, Elf_Versym, Elf_Verdef, Elf_Verdaux, Elf_Verneed, Elf_Vernaux, Elf_Rel, Elf_Rela, ElfClass

template<class T>
struct span
{
    explicit span(T* d = {}, size_t l = {}) : data(d), len(l) {}
    span(T* from, T* to) : data(from), len(to-from) { assert(from <= to); }
    T& operator[](std::size_t i) { checkRange(i); return data[i]; }
    T* begin() { return data; }
    T* end() { return data + len; }
    auto size() const { return len; }
    explicit operator bool() const { return size() > 0; }

private:
    void checkRange(std::size_t i) {
        if (i >= size()) throw std::out_of_range("error: Span access out of range.");
    }

    T* data;
    size_t len;
};

template<ElfFileParams>
class ElfFile
{
public:

    const FileContents fileContents;

private:

    std::vector<Elf_Phdr> phdrs;
    std::vector<Elf_Shdr> shdrs;

    bool littleEndian;

    bool changed = false;

    bool isExecutable = false;

    using SectionName = std::string;
    using ReplacedSections = std::map<SectionName, std::string>;

    ReplacedSections replacedSections;

    std::string sectionNames; /* content of the .shstrtab section */

    /* Align on 4 or 8 bytes boundaries on 32- or 64-bit platforms
       respectively. */
    static constexpr size_t sectionAlignment = sizeof(Elf_Off);

    std::vector<SectionName> sectionsByOldIndex;

public:
    explicit ElfFile(FileContents fileContents);

    [[nodiscard]] bool isChanged() const noexcept
    {
        return changed;
    }

private:

    struct CompPhdr
    {
        const ElfFile * elfFile;
        bool operator ()(const Elf_Phdr & x, const Elf_Phdr & y) const noexcept
        {
            // A PHDR comes before everything else.
            if (elfFile->rdi(y.p_type) == PT_PHDR) return false;
            if (elfFile->rdi(x.p_type) == PT_PHDR) return true;

            // Sort non-PHDRs by address.
            return elfFile->rdi(x.p_paddr) < elfFile->rdi(y.p_paddr);
        }
    };

    void sortPhdrs();

    struct CompShdr
    {
        const ElfFile * elfFile;
        bool operator ()(const Elf_Shdr & x, const Elf_Shdr & y) const noexcept
        {
            return elfFile->rdi(x.sh_offset) < elfFile->rdi(y.sh_offset);
        }
    };

    [[nodiscard]] unsigned int getPageSize() const noexcept;

    void sortShdrs();

    void shiftFile(unsigned int extraPages, size_t sizeOffset, size_t extraBytes);

    [[nodiscard]] std::string getSectionName(const Elf_Shdr & shdr) const;

    const Elf_Shdr & findSectionHeader(const SectionName & sectionName) const;

    [[nodiscard]] std::optional<std::reference_wrapper<const Elf_Shdr>> tryFindSectionHeader(const SectionName & sectionName) const;

    template<class T> span<T> getSectionSpan(const Elf_Shdr & shdr) const;
    template<class T> span<T> getSectionSpan(const SectionName & sectionName);
    template<class T> span<T> tryGetSectionSpan(const SectionName & sectionName);

    [[nodiscard]] unsigned int getSectionIndex(const SectionName & sectionName) const;

    std::string & replaceSection(const SectionName & sectionName,
        unsigned int size);

    [[nodiscard]] bool haveReplacedSection(const SectionName & sectionName) const;

    void writeReplacedSections(Elf_Off & curOff,
        Elf_Addr startAddr, Elf_Off startOffset);

    void rewriteHeaders(Elf_Addr phdrAddress);

    void rewriteSectionsLibrary();

    void rewriteSectionsExecutable();

    void normalizeNoteSegments();

public:

    void rewriteSections(bool force = false);

    [[nodiscard]] std::string getInterpreter() const;

    typedef enum { printOsAbi, replaceOsAbi } osAbiMode;

    void modifyOsAbi(osAbiMode op, const std::string & newOsAbi);

    typedef enum { printSoname, replaceSoname } sonameMode;

    void modifySoname(sonameMode op, const std::string & newSoname);

    void setInterpreter(const std::string & newInterpreter);

    typedef enum { rpPrint, rpShrink, rpSet, rpAdd, rpRemove } RPathOp;

    void modifyRPath(RPathOp op, const std::vector<std::string> & allowedRpathPrefixes, std::string newRPath);
    std::string shrinkRPath(char* rpath, std::vector<std::string> &neededLibs, const std::vector<std::string> & allowedRpathPrefixes);
    void removeRPath(Elf_Shdr & shdrDynamic);

    void addNeeded(const std::set<std::string> & libs);

    void removeNeeded(const std::set<std::string> & libs);

    void replaceNeeded(const std::map<std::string, std::string> & libs);

    void printNeededLibs() const;

    void noDefaultLib();

    void addDebugTag();

    void renameDynamicSymbols(const std::unordered_map<std::string_view, std::string>&);

    void clearSymbolVersions(const std::set<std::string> & syms);

    enum class ExecstackMode { print, set, clear };

    void modifyExecstack(ExecstackMode op);

private:
    struct GnuHashTable {
        using BloomWord = Elf_Addr;
        struct Header {
            uint32_t numBuckets, symndx, maskwords, shift2;
        } m_hdr;
        span<BloomWord> m_bloomFilters;
        span<uint32_t> m_buckets, m_table;
    };
    GnuHashTable parseGnuHashTable(span<char> gh);

    struct HashTable {
        struct Header {
            uint32_t numBuckets, nchain;
        } m_hdr;
        span<uint32_t> m_buckets, m_chain;
    };
    HashTable parseHashTable(span<char> gh);

    void rebuildGnuHashTable(span<char> strTab, span<Elf_Sym> dynsyms);
    void rebuildHashTable(span<char> strTab, span<Elf_Sym> dynsyms);

    using Elf_Rel_Info = decltype(Elf_Rel::r_info);

    uint32_t rel_getSymId(const Elf_Rel_Info& info) const
    {
        if constexpr (std::is_same_v<Elf_Rel, Elf64_Rel>)
            return ELF64_R_SYM(info);
        else
            return ELF32_R_SYM(info);
    }

    Elf_Rel_Info rel_setSymId(Elf_Rel_Info info, uint32_t id) const
    {
        if constexpr (std::is_same_v<Elf_Rel, Elf64_Rel>)
        {
            constexpr Elf_Rel_Info idmask = (~Elf_Rel_Info()) << 32;
            info = (info & ~idmask) | (Elf_Rel_Info(id) << 32);
        }
        else
        {
            constexpr Elf_Rel_Info idmask = (~Elf_Rel_Info()) << 8;
            info = (info & ~idmask) | (Elf_Rel_Info(id) << 8);
        }
        return info;
    }

    template<class ElfRelType, class RemapFn>
    void changeRelocTableSymIds(const Elf_Shdr& shdr, RemapFn&& old2newSymId)
    {
        static_assert(std::is_same_v<ElfRelType, Elf_Rel> || std::is_same_v<ElfRelType, Elf_Rela>);

        for (auto& r : getSectionSpan<ElfRelType>(shdr))
        {
            auto info = rdi(r.r_info);
            auto oldSymIdx = rel_getSymId(info);
            auto newSymIdx = old2newSymId(oldSymIdx);
            if (newSymIdx != oldSymIdx)
                wri(r.r_info, rel_setSymId(info, newSymIdx));
        }
    }

    template<class StrIdxCallback>
    void forAllStringReferences(const Elf_Shdr& strTabHdr, StrIdxCallback&& fn);

    template<class T, class U>
    auto follow(U* ptr, size_t offset) -> T* {
        return offset ? (T*)(((char*)ptr)+offset) : nullptr;
    };

    template<class VdFn, class VaFn>
    void forAll_ElfVer(span<Elf_Verdef> vdspan, VdFn&& vdfn, VaFn&& vafn)
    {
        auto* vd = vdspan.begin();
        for (; vd; vd = follow<Elf_Verdef>(vd, rdi(vd->vd_next)))
        {
            vdfn(*vd);
            auto va = follow<Elf_Verdaux>(vd, rdi(vd->vd_aux));
            for (; va; va = follow<Elf_Verdaux>(va, rdi(va->vda_next)))
                vafn(*va);
        }
    }

    template<class VnFn, class VaFn>
    void forAll_ElfVer(span<Elf_Verneed> vnspan, VnFn&& vnfn, VaFn&& vafn)
    {
        auto* vn = vnspan.begin();
        for (; vn; vn = follow<Elf_Verneed>(vn, rdi(vn->vn_next)))
        {
            vnfn(*vn);
            auto va = follow<Elf_Vernaux>(vn, rdi(vn->vn_aux));
            for (; va; va = follow<Elf_Vernaux>(va, rdi(va->vna_next)))
                vafn(*va);
        }
    }

    /* Convert an integer in big or little endian representation (as
       specified by the ELF header) to this platform's integer
       representation. */
    template<class I>
    constexpr I rdi(I i) const noexcept;

    /* Convert back to the ELF representation. */
    template<class I, class U>
    constexpr inline I wri(I & t, U i) const
    {
        I val = static_cast<I>(i);
        if (static_cast<U>(val) != i)            
            throw std::runtime_error { "value truncation" };
        t = rdi(val);
        return val;
    }

    [[nodiscard]] Elf_Ehdr *hdr() noexcept {
      return reinterpret_cast<Elf_Ehdr *>(fileContents->data());
    }

    [[nodiscard]] const Elf_Ehdr *hdr() const noexcept {
      return reinterpret_cast<const Elf_Ehdr *>(fileContents->data());
    }
};