aboutsummaryrefslogtreecommitdiffstats
path: root/tools/fix_elf/patch.cpp
blob: c71bd09b42234e1964b7028c2b6d5c2142a18c41 (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
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
#include "patch.h"

#include <library/cpp/getopt/last_getopt.h> 

#include <util/generic/algorithm.h>
#include <util/generic/hash.h>
#include <util/stream/null.h>
#include <util/string/cast.h>
#include <util/system/defaults.h>

namespace NElf {

bool IsElf(const TString& path) {
    TUnbufferedFileInput in(path);
    char buffer[EI_NIDENT];
    size_t nread = in.Load(buffer, sizeof(buffer));

    if (nread != sizeof(buffer) || TStringBuf(buffer, SELFMAG) != ELFMAG) {
        Cerr << "fix_elf skip " << path << " (not an ELF file)";
        return false;
    }

    if (buffer[EI_CLASS] != ELFCLASS64) {
        Cerr << "fix_elf skip " << path << " (ELF class is not ELF64)";
        return false;
    }

#ifdef _little_endian_
    if (buffer[EI_DATA] != ELFDATA2LSB) {
        Cerr << "fix_elf skip " << path << " (ELF byte order is not native LSB)";
        return false;
    }
#else
    if (buffer[EI_DATA] != ELFDATA2MSB) {
        Cerr << "fix_elf skip " << path << " (ELF byte order is not native MSB)";
        return false;
    }
#endif

    if (buffer[EI_VERSION] != 1) {
        Cerr << "fix_elf skip " << path << " (ELF version is not 1)";
        return false;
    }

    return true;
}

} // namespace NElf

using namespace NElf;

void ReadNum(TStringBuf& src, TStringBuf& dst) {
    const char* c = src.data();
    while (isdigit(*c)) {
        ++c;
    }
    size_t len = c - src.data();

    dst = TStringBuf(src.data(), len);
    src.Skip(len);
}

int NumericStrCmp(TStringBuf s1, TStringBuf s2) {
    while (!s1.empty() || !s2.empty()) {
        char c1 = *s1.data();
        char c2 = *s2.data();

        if (isdigit(c1) && isdigit(c2)) {
            TStringBuf num1, num2;
            ReadNum(s1, num1);
            ReadNum(s2, num2);

            int c = FromString<int>(num1) - FromString<int>(num2);
            if (c) {
                return c;
            }

        } else {
            int c = int(c1) - int(c2);
            if (c) {
                return c;
            }
        }

        s1.Skip(1);
        s2.Skip(1);
    }

    return 0;
}

class TVernauxCmp {
public:
    TVernauxCmp(TSection strSect)
        : StrSect(strSect)
    {
    }

    bool operator()(Elf64_Vernaux* v1, Elf64_Vernaux* v2) {
        TStringBuf s1 = StrSect.GetStr(v1->vna_name);
        TStringBuf s2 = StrSect.GetStr(v2->vna_name);

        return NumericStrCmp(s1, s2) < 0;
    }

private:
    TSection StrSect;
};

void Patch(const TString& path, const TString& library, IOutputStream& verboseOut) {
    TElf elf(path);

    TVerneedSection verneedSect(&elf);
    if (verneedSect.IsNull()) {
        verboseOut << "No symbol versions section" << Endl;
        return;
    }

    TSection verStrings(&elf, elf.GetSection(verneedSect.GetLink()));

    TStringBuf skipFrom("GLIBC_2.14");
    TStringBuf patchFrom("GLIBC_2.2.5");

    TVector<Elf64_Vernaux*> patchAux;

    Elf64_Vernaux* patchFromAux = nullptr;

    Elf64_Verneed* verneed = verneedSect.GetFirstVerneed();
    while (verneed) {

        TStringBuf file = verStrings.GetStr(verneed->vn_file);
        verboseOut << file;

        if (file != library) {
            verboseOut << " skipped" << Endl;

        } else {
            verboseOut << Endl;

            Elf64_Vernaux* vernaux = verneedSect.GetFirstVernaux(verneed);
            while (vernaux) {

                TStringBuf name = verStrings.GetStr(vernaux->vna_name);
                verboseOut << "\t" << name;

                if (!patchFromAux && name == patchFrom) {
                    verboseOut << " taken as patch source" << Endl;
                    patchFromAux = vernaux;

                } else {

                    if (NumericStrCmp(name, skipFrom) < 0) {
                        verboseOut << " skipped" << Endl;

                    } else {
                        verboseOut << " will be patched" << Endl;
                        patchAux.push_back(vernaux);
                    }
                }
                vernaux = verneedSect.GetNextVernaux(vernaux);
            }
        }
        verneed = verneedSect.GetNextVerneed(verneed);
    }

    if (patchAux.empty()) {
        verboseOut << "Nothing to patch" << Endl;
        return;
    }

    if (!patchFromAux) {
        ythrow yexception() << path << ": no ELF64_Vernaux source to patch from";
    }

    TSection dynsymSect(&elf, elf.GetSectionByType(SHT_DYNSYM));
    TSection symstrSect(&elf, elf.GetSection(dynsymSect.GetLink()));
    TSection dynverSect(&elf, elf.GetSectionByType(SHT_GNU_versym));

    for (size_t i = 0, c = dynsymSect.GetEntryCount(); i < c; ++i) {
        Elf64_Sym* sym = dynsymSect.GetEntry<Elf64_Sym>(i);
        Elf64_Half* ver = dynverSect.GetEntry<Elf64_Half>(i);
        for (auto aux : patchAux) {
            if (*ver == aux->vna_other) {
                *ver = 0;
                verboseOut << "Symbol " << i << ": " << symstrSect.GetStr(sym->st_name)
                    << "@" << verStrings.GetStr(aux->vna_name) << " version removed" << Endl;
            }
        }
    }

    for (auto aux : patchAux) {
        TStringBuf name = verStrings.GetStr(aux->vna_name);
        aux->vna_name = patchFromAux->vna_name;
        aux->vna_hash = patchFromAux->vna_hash;
        verboseOut << "Version dependence " << name << " [" << aux->vna_other
            << "] patched from " << patchFrom << " [" << patchFromAux->vna_other << "]" << Endl;
    }
}

void PatchGnuUnique(const TString& path, IOutputStream& verboseOut) {
    TElf elf(path);

    for (Elf64_Shdr* it = elf.GetSectionBegin(), *end = elf.GetSectionEnd(); it != end; ++it) {
        if (it->sh_type == SHT_SYMTAB) {

            TSection section{&elf, it};
            verboseOut << "Found symbol section [" << section.GetName() << ']' << Endl;

            for (size_t i = 0, count = section.GetEntryCount(); i < count; ++i) {
                Elf64_Sym* symbol = section.GetEntry<Elf64_Sym>(i);
                auto& info = symbol->st_info;

                if (ELF64_ST_BIND(info) == STB_GNU_UNIQUE) {
                    verboseOut << "Found GNU unique symbol #" << i << Endl;
                    info = ELF64_ST_INFO(STB_GLOBAL, ELF64_ST_TYPE(info));
                }
            }
        }
    }
}

int main(int argc, char* argv[]) {
    bool verbose = false;
    bool rewrite_unique = false;

    using namespace NLastGetopt;

    TOpts opts = NLastGetopt::TOpts::Default();
    opts.AddHelpOption();

    opts.AddLongOption('v', "verbose").NoArgument().StoreValue(&verbose, true);
    opts.AddLongOption('u', "rewrite-gnu-unique", "Change STB_GNU_UNIQUE to STB_GLOBAL").NoArgument().StoreValue(&rewrite_unique, true);

    opts.SetFreeArgsMin(1);
    opts.SetFreeArgTitle(0, "<file>", "File");

    TOptsParseResult res(&opts, argc, argv);
    TVector<TString> files = res.GetFreeArgs();

    IOutputStream& verboseOut = verbose ? Cout : Cnull;

    bool first = true;
    for (auto path : files) {

        if (!IsElf(path)) {
            continue;
        }

        if (!first) {
            verboseOut << Endl;
        }
        first = false;

        verboseOut << "Patching " << path << Endl;

        try {
            if (rewrite_unique) {
                PatchGnuUnique(path, verboseOut);
            } else {
                Patch(path, "libc.so.6", verboseOut);
                Patch(path, "libm.so.6", verboseOut);
            }
        } catch (const yexception& e) {
            Cerr << "Patching failed: " << e.what() << Endl;
        }
    }

    return 0;
}