forked from shinh/sold
-
Notifications
You must be signed in to change notification settings - Fork 2
/
utils.cc
575 lines (534 loc) · 17.6 KB
/
utils.cc
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
// read_uleb128, read_sleb128, sold_Unwind_Ptr, read_encoded_value_with_base
// were copied from sysdeps/generic/unwind-pe.h in glibc. Free Software
// Foundation, Inc. holds the copyright for them. The sold authors hold the
// copyright for the other part of this source code.
//
// Copyright (C) 2021 The sold authors
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// Copyright (C) 2012-2021 Free Software Foundation, Inc.
// Written by Ian Lance Taylor, Google.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// (1) Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// (2) Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in
// the documentation and/or other materials provided with the
// distribution.
//
// (3) The name of the author may not be used to
// endorse or promote products derived from this software without
// specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
// IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
// HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
// IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
#include "utils.h"
#include <iomanip>
// Ubuntu 18.04 doesn't have DT_SYMTAB_SHNDX definition.
#ifndef DT_SYMTAB_SHNDX
#define DT_SYMTAB_SHNDX 34
#endif
std::vector<std::string> SplitString(const std::string& str, const std::string& sep) {
std::vector<std::string> ret;
if (str.empty()) return ret;
size_t index = 0;
while (true) {
size_t next = str.find(sep, index);
ret.push_back(str.substr(index, next - index));
if (next == std::string::npos) break;
index = next + 1;
}
return ret;
}
bool HasPrefix(const std::string& str, const std::string& prefix) {
ssize_t size_diff = str.size() - prefix.size();
return size_diff >= 0 && str.substr(0, prefix.size()) == prefix;
}
uintptr_t AlignNext(uintptr_t a, uintptr_t mask) {
return (a + mask) & ~mask;
}
void WriteBuf(FILE* fp, const void* buf, size_t size) {
CHECK(fwrite(buf, 1, size, fp) == size);
}
void EmitZeros(FILE* fp, uintptr_t cnt) {
std::string zero(cnt, '\0');
WriteBuf(fp, zero.data(), zero.size());
}
void EmitPad(FILE* fp, uintptr_t to) {
uint pos = ftell(fp);
CHECK_GE(pos, 0);
CHECK_LE(pos, to);
EmitZeros(fp, to - pos);
}
void MemcpyFile(FILE* fp, uintptr_t offset, const void* src, size_t size) {
uint pos = ftell(fp);
CHECK_GE(offset, 0);
CHECK_GE(size, 0);
CHECK_LT(offset + size, pos);
fseek(fp, offset, SEEK_SET);
fwrite(src, 1, size, fp);
fseek(fp, pos, SEEK_SET);
}
void EmitAlign(FILE* fp) {
long pos = ftell(fp);
CHECK(pos >= 0);
EmitZeros(fp, AlignNext(pos) - pos);
}
bool IsTLS(const Elf_Sym& sym) {
return ELF_ST_TYPE(sym.st_info) == STT_TLS;
}
bool IsDefined(const Elf_Sym& sym) {
return (sym.st_value || IsTLS(sym)) && sym.st_shndx != SHN_UNDEF;
}
std::ostream& operator<<(std::ostream& os, const Syminfo& s) {
auto f = os.flags();
os << "Syminfo{name=" << s.name << ", soname=" << s.soname << ", version=" << s.version << ", versym=" << s.versym << ", sym=0x"
<< std::hex << std::setfill('0') << std::setw(16) << s.sym << "}";
os.flags(f);
return os;
}
std::string ShowDW_EH_PE(uint8_t type) {
if (type == DW_EH_PE_omit) {
return "DW_EH_PE_omit";
}
std::string ret;
switch (type & 0xf) {
case DW_EH_PE_absptr:
ret = "DW_EH_PE_absptr";
break;
case DW_EH_PE_uleb128:
ret = "DW_EH_PE_uleb128";
break;
case DW_EH_PE_udata2:
ret = "DW_EH_PE_udata2";
break;
case DW_EH_PE_udata4:
ret = "DW_EH_PE_udata4";
break;
case DW_EH_PE_udata8:
ret = "DW_EH_PE_udata8";
break;
case DW_EH_PE_sleb128:
ret = "DW_EH_PE_sleb128";
break;
case DW_EH_PE_sdata2:
ret = "DW_EH_PE_sdata2";
break;
case DW_EH_PE_sdata4:
ret = "DW_EH_PE_sdata4";
break;
case DW_EH_PE_sdata8:
ret = "DW_EH_PE_sdata8";
break;
}
switch (type & 0xf0) {
case DW_EH_PE_pcrel:
ret += " + DW_EH_PE_pcrel";
break;
case DW_EH_PE_textrel:
ret += " + DW_EH_PE_textrel";
break;
case DW_EH_PE_datarel:
ret += " + DW_EH_PE_datarel";
break;
case DW_EH_PE_funcrel:
ret += " + DW_EH_PE_funcrel";
break;
case DW_EH_PE_aligned:
ret += " + DW_EH_PE_aligned";
break;
}
if (type == DW_EH_PE_SOLD_DUMMY) {
ret = "DW_EH_PE_SOLD_DUMMY(0xEE)";
} else if (ret == "") {
ret = HexString(type, 2);
}
return ret;
}
std::string ShowDynamicEntryType(int type) {
switch (type) {
case DT_NULL:
return "DT_NULL";
case DT_NEEDED:
return "DT_NEEDED";
case DT_PLTRELSZ:
return "DT_PLTRELSZ";
case DT_PLTGOT:
return "DT_PLTGOT";
case DT_HASH:
return "DT_HASH";
case DT_STRTAB:
return "DT_STRTAB";
case DT_SYMTAB:
return "DT_SYMTAB";
case DT_RELA:
return "DT_RELA";
case DT_RELASZ:
return "DT_RELASZ";
case DT_RELAENT:
return "DT_RELAENT";
case DT_STRSZ:
return "DT_STRSZ";
case DT_SYMENT:
return "DT_SYMENT";
case DT_INIT:
return "DT_INIT";
case DT_FINI:
return "DT_FINI";
case DT_SONAME:
return "DT_SONAME";
case DT_RPATH:
return "DT_RPATH";
case DT_SYMBOLIC:
return "DT_SYMBOLIC";
case DT_REL:
return "DT_REL";
case DT_RELSZ:
return "DT_RELSZ";
case DT_RELENT:
return "DT_RELENT";
case DT_PLTREL:
return "DT_PLTREL";
case DT_DEBUG:
return "DT_DEBUG";
case DT_TEXTREL:
return "DT_TEXTREL";
case DT_JMPREL:
return "DT_JMPREL";
case DT_BIND_NOW:
return "DT_BIND_NOW";
case DT_INIT_ARRAY:
return "DT_INIT_ARRAY";
case DT_FINI_ARRAY:
return "DT_FINI_ARRAY";
case DT_INIT_ARRAYSZ:
return "DT_INIT_ARRAYSZ";
case DT_FINI_ARRAYSZ:
return "DT_FINI_ARRAYSZ";
case DT_RUNPATH:
return "DT_RUNPATH";
case DT_FLAGS:
return "DT_FLAGS";
case DT_ENCODING:
return "DT_ENCODING";
case DT_PREINIT_ARRAYSZ:
return "DT_PREINIT_ARRAYSZ";
case DT_SYMTAB_SHNDX:
return "DT_SYMTAB_SHNDX";
case DT_LOOS:
return "DT_LOOS";
case DT_HIOS:
return "DT_HIOS";
case DT_LOPROC:
return "DT_LOPROC";
case DT_HIPROC:
return "DT_HIPROC";
case DT_PROCNUM:
return "DT_PROCNUM";
case DT_VALRNGLO:
return "DT_VALRNGLO";
case DT_GNU_PRELINKED:
return "DT_GNU_PRELINKED";
case DT_GNU_CONFLICTSZ:
return "DT_GNU_CONFLICTSZ";
case DT_GNU_LIBLISTSZ:
return "DT_GNU_LIBLISTSZ";
case DT_CHECKSUM:
return "DT_CHECKSUM";
case DT_PLTPADSZ:
return "DT_PLTPADSZ";
case DT_MOVEENT:
return "DT_MOVEENT";
case DT_MOVESZ:
return "DT_MOVESZ";
case DT_FEATURE_1:
return "DT_FEATURE_1";
case DT_POSFLAG_1:
return "DT_POSFLAG_1";
case DT_SYMINSZ:
return "DT_SYMINSZ";
case DT_SYMINENT:
return "DT_SYMINENT";
case DT_ADDRRNGLO:
return "DT_ADDRRNGLO";
case DT_GNU_HASH:
return "DT_GNU_HASH";
case DT_TLSDESC_PLT:
return "DT_TLSDESC_PLT";
case DT_TLSDESC_GOT:
return "DT_TLSDESC_GOT";
case DT_GNU_CONFLICT:
return "DT_GNU_CONFLICT";
case DT_GNU_LIBLIST:
return "DT_GNU_LIBLIST";
case DT_CONFIG:
return "DT_CONFIG";
case DT_DEPAUDIT:
return "DT_DEPAUDIT";
case DT_AUDIT:
return "DT_AUDIT";
case DT_PLTPAD:
return "DT_PLTPAD";
case DT_MOVETAB:
return "DT_MOVETAB";
case DT_SYMINFO:
return "DT_SYMINFO";
case DT_VERSYM:
return "DT_VERSYM";
case DT_RELACOUNT:
return "DT_RELACOUNT";
case DT_RELCOUNT:
return "DT_RELCOUNT";
case DT_FLAGS_1:
return "DT_FLAGS_";
case DT_VERDEF:
return "DT_VERDE";
case DT_VERDEFNUM:
return "DT_VERDEFNU";
case DT_VERNEED:
return "DT_VERNEE";
case DT_VERNEEDNUM:
return "DT_VERNEEDNUM";
case DT_AUXILIARY:
return "DT_AUXILIARY";
default:
LOG(FATAL) << "Unknown type" << SOLD_LOG_BITS(type);
}
}
std::string ShowRelocationType(int type) {
switch (type) {
case R_X86_64_NONE:
return "R_X86_64_NONE";
case R_X86_64_64:
return "R_X86_64_64";
case R_X86_64_PC32:
return "R_X86_64_PC32";
case R_X86_64_GOT32:
return "R_X86_64_GOT32";
case R_X86_64_PLT32:
return "R_X86_64_PLT32";
case R_X86_64_COPY:
return "R_X86_64_COPY";
case R_X86_64_GLOB_DAT:
return "R_X86_64_GLOB_DAT";
case R_X86_64_JUMP_SLOT:
return "R_X86_64_JUMP_SLOT";
case R_X86_64_RELATIVE:
return "R_X86_64_RELATIVE";
case R_X86_64_GOTPCREL:
return "R_X86_64_GOTPCREL";
case R_X86_64_32:
return "R_X86_64_32";
case R_X86_64_32S:
return "R_X86_64_32S";
case R_X86_64_16:
return "R_X86_64_16";
case R_X86_64_PC16:
return "R_X86_64_PC16";
case R_X86_64_8:
return "R_X86_64_8";
case R_X86_64_PC8:
return "R_X86_64_PC8";
case R_X86_64_DTPMOD64:
return "R_X86_64_DTPMOD64";
case R_X86_64_DTPOFF64:
return "R_X86_64_DTPOFF64";
case R_X86_64_TPOFF64:
return "R_X86_64_TPOFF64";
case R_X86_64_TLSGD:
return "R_X86_64_TLSGD";
case R_X86_64_TLSLD:
return "R_X86_64_TLSLD";
case R_X86_64_DTPOFF32:
return "R_X86_64_DTPOFF32";
case R_X86_64_GOTTPOFF:
return "R_X86_64_GOTTPOFF";
case R_X86_64_TPOFF32:
return "R_X86_64_TPOFF32";
case R_X86_64_PC64:
return "R_X86_64_PC64";
case R_X86_64_GOTOFF64:
return "R_X86_64_GOTOFF64";
case R_X86_64_GOTPC32:
return "R_X86_64_GOTPC32";
case R_X86_64_GOT64:
return "R_X86_64_GOT64";
case R_X86_64_GOTPCREL64:
return "R_X86_64_GOTPCREL64";
case R_X86_64_GOTPC64:
return "R_X86_64_GOTPC64";
case R_X86_64_GOTPLT64:
return "R_X86_64_GOTPLT64";
case R_X86_64_PLTOFF64:
return "R_X86_64_PLTOFF64";
case R_X86_64_SIZE32:
return "R_X86_64_SIZE32";
case R_X86_64_SIZE64:
return "R_X86_64_SIZE64";
case R_X86_64_GOTPC32_TLSDESC:
return "R_X86_64_GOTPC32_TLSDESC";
case R_X86_64_TLSDESC:
return "R_X86_64_TLSDESC";
case R_X86_64_IRELATIVE:
return "R_X86_64_IRELATIVE";
case R_X86_64_RELATIVE64:
return "R_X86_64_RELATIVE64";
case R_X86_64_GOTPCRELX:
return "R_X86_64_GOTPCRELX";
case R_X86_64_REX_GOTPCRELX:
return "R_X86_64_REX_GOTPCRELX";
case R_X86_64_NUM:
return "R_X86_64_NUM";
default: {
return HexString(type, 4);
}
}
}
std::ostream& operator<<(std::ostream& os, const Elf_Rel& r) {
os << "Elf_Rela{r_offset=" << SOLD_LOG_32BITS(r.r_offset) << ", r_info=" << SOLD_LOG_32BITS(r.r_info)
<< ", ELF_R_SYM(r.r_info)=" << SOLD_LOG_16BITS(ELF_R_SYM(r.r_info))
<< ", ELF_R_TYPE(r.r_info)=" << ShowRelocationType(ELF_R_TYPE(r.r_info)) << ", r_addend=" << SOLD_LOG_32BITS(r.r_addend) << "}";
return os;
}
bool is_special_ver_ndx(Elf64_Versym versym) {
return (versym == VER_NDX_LOCAL || versym == VER_NDX_GLOBAL);
}
std::string special_ver_ndx_to_str(Elf64_Versym versym) {
if (versym == VER_NDX_LOCAL) {
return std::string("VER_NDX_LOCAL");
} else if (versym == VER_NDX_GLOBAL) {
return std::string("VER_NDX_GLOBAL");
} else if (versym == NO_VERSION_INFO) {
return std::string("NO_VERSION_INFO");
} else {
LOG(FATAL) << "This versym (= " << versym << ") is not special.";
exit(1);
}
}
// Copy from sysdeps/generic/unwind-pe.h in glibc
const char* read_uleb128(const char* p, uint32_t* val) {
unsigned int shift = 0;
unsigned char byte;
uint32_t result;
result = 0;
do {
byte = *p++;
result |= (byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
*val = result;
return p;
}
// Copy from sysdeps/generic/unwind-pe.h in glibc
const char* read_sleb128(const char* p, int32_t* val) {
unsigned int shift = 0;
unsigned char byte;
int32_t result;
result = 0;
do {
byte = *p++;
result |= (byte & 0x7f) << shift;
shift += 7;
} while (byte & 0x80);
/* Sign-extend a negative value. */
if (shift < 8 * sizeof(result) && (byte & 0x40) != 0) result |= -(1L << shift);
*val = (int32_t)result;
return p;
}
// Copy from sysdeps/generic/unwind-pe.h in glibc
typedef unsigned sold_Unwind_Internal_Ptr __attribute__((__mode__(__pointer__)));
#define DW_EH_PE_indirect 0x80
const char* read_encoded_value_with_base(unsigned char encoding, sold_Unwind_Ptr base, const char* p, uint32_t* val) {
union unaligned {
void* ptr;
unsigned u2 __attribute__((mode(HI)));
unsigned u4 __attribute__((mode(SI)));
unsigned u8 __attribute__((mode(DI)));
signed s2 __attribute__((mode(HI)));
signed s4 __attribute__((mode(SI)));
signed s8 __attribute__((mode(DI)));
} __attribute__((__packed__));
union unaligned* u = (union unaligned*)p;
sold_Unwind_Internal_Ptr result;
if (encoding == DW_EH_PE_aligned) {
sold_Unwind_Internal_Ptr a = (sold_Unwind_Internal_Ptr)p;
a = (a + sizeof(void*) - 1) & -sizeof(void*);
result = *(sold_Unwind_Internal_Ptr*)a;
p = (char*)(a + sizeof(void*));
} else {
switch (encoding & 0x0f) {
case DW_EH_PE_absptr:
result = (sold_Unwind_Internal_Ptr)u->ptr;
p += sizeof(void*);
break;
case DW_EH_PE_uleb128: {
uint32_t tmp;
p = read_uleb128(p, &tmp);
result = (sold_Unwind_Internal_Ptr)tmp;
} break;
case DW_EH_PE_sleb128: {
int32_t tmp;
p = read_sleb128(p, &tmp);
result = (sold_Unwind_Internal_Ptr)tmp;
} break;
case DW_EH_PE_udata2:
result = u->u2;
p += 2;
break;
case DW_EH_PE_udata4:
result = u->u4;
p += 4;
break;
case DW_EH_PE_udata8:
result = u->u8;
p += 8;
break;
case DW_EH_PE_sdata2:
result = u->s2;
p += 2;
break;
case DW_EH_PE_sdata4:
result = u->s4;
p += 4;
break;
case DW_EH_PE_sdata8:
result = u->s8;
p += 8;
break;
default:
LOG(FATAL) << SOLD_LOG_8BITS(encoding & 0x0f);
}
if (result != 0) {
result += ((encoding & 0x70) == DW_EH_PE_pcrel ? (sold_Unwind_Internal_Ptr)u : base);
if (encoding & DW_EH_PE_indirect) result = *(sold_Unwind_Internal_Ptr*)result;
}
}
*val = result;
return p;
}