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
|
//===- ELF.cpp - ELF object file implementation ---------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/Object/ELF.h"
#include "llvm/BinaryFormat/ELF.h"
#include "llvm/Support/LEB128.h"
using namespace llvm;
using namespace object;
#define STRINGIFY_ENUM_CASE(ns, name) \
case ns::name: \
return #name;
#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
uint32_t Type) {
switch (Machine) {
case ELF::EM_X86_64:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
default:
break;
}
break;
case ELF::EM_386:
case ELF::EM_IAMCU:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/i386.def"
default:
break;
}
break;
case ELF::EM_MIPS:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
default:
break;
}
break;
case ELF::EM_AARCH64:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
default:
break;
}
break;
case ELF::EM_ARM:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
default:
break;
}
break;
case ELF::EM_ARC_COMPACT:
case ELF::EM_ARC_COMPACT2:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
default:
break;
}
break;
case ELF::EM_AVR:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/AVR.def"
default:
break;
}
break;
case ELF::EM_HEXAGON:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
default:
break;
}
break;
case ELF::EM_LANAI:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
default:
break;
}
break;
case ELF::EM_PPC:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
default:
break;
}
break;
case ELF::EM_PPC64:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
default:
break;
}
break;
case ELF::EM_RISCV:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
default:
break;
}
break;
case ELF::EM_S390:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
default:
break;
}
break;
case ELF::EM_SPARC:
case ELF::EM_SPARC32PLUS:
case ELF::EM_SPARCV9:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
default:
break;
}
break;
case ELF::EM_AMDGPU:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
default:
break;
}
break;
case ELF::EM_BPF:
switch (Type) {
#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
default:
break;
}
break;
default:
break;
}
return "Unknown";
}
#undef ELF_RELOC
uint32_t llvm::object::getELFRelrRelocationType(uint32_t Machine) {
switch (Machine) {
case ELF::EM_X86_64:
return ELF::R_X86_64_RELATIVE;
case ELF::EM_386:
case ELF::EM_IAMCU:
return ELF::R_386_RELATIVE;
case ELF::EM_MIPS:
break;
case ELF::EM_AARCH64:
return ELF::R_AARCH64_RELATIVE;
case ELF::EM_ARM:
return ELF::R_ARM_RELATIVE;
case ELF::EM_ARC_COMPACT:
case ELF::EM_ARC_COMPACT2:
return ELF::R_ARC_RELATIVE;
case ELF::EM_AVR:
break;
case ELF::EM_HEXAGON:
return ELF::R_HEX_RELATIVE;
case ELF::EM_LANAI:
break;
case ELF::EM_PPC:
break;
case ELF::EM_PPC64:
return ELF::R_PPC64_RELATIVE;
case ELF::EM_RISCV:
return ELF::R_RISCV_RELATIVE;
case ELF::EM_S390:
return ELF::R_390_RELATIVE;
case ELF::EM_SPARC:
case ELF::EM_SPARC32PLUS:
case ELF::EM_SPARCV9:
return ELF::R_SPARC_RELATIVE;
case ELF::EM_AMDGPU:
break;
case ELF::EM_BPF:
break;
default:
break;
}
return 0;
}
StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
switch (Machine) {
case ELF::EM_ARM:
switch (Type) {
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
}
break;
case ELF::EM_HEXAGON:
switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
break;
case ELF::EM_X86_64:
switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
break;
case ELF::EM_MIPS:
case ELF::EM_MIPS_RS3_LE:
switch (Type) {
STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
}
break;
default:
break;
}
switch (Type) {
STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
STRINGIFY_ENUM_CASE(ELF, SHT_REL);
STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
default:
return "Unknown";
}
}
template <class ELFT>
Expected<std::vector<typename ELFT::Rela>>
ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
// This function decodes the contents of an SHT_RELR packed relocation
// section.
//
// Proposal for adding SHT_RELR sections to generic-abi is here:
// https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
//
// The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
// like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
//
// i.e. start with an address, followed by any number of bitmaps. The address
// entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
// relocations each, at subsequent offsets following the last address entry.
//
// The bitmap entries must have 1 in the least significant bit. The assumption
// here is that an address cannot have 1 in lsb. Odd addresses are not
// supported.
//
// Excluding the least significant bit in the bitmap, each non-zero bit in
// the bitmap represents a relocation to be applied to a corresponding machine
// word that follows the base address word. The second least significant bit
// represents the machine word immediately following the initial address, and
// each bit that follows represents the next word, in linear order. As such,
// a single bitmap can encode up to 31 relocations in a 32-bit object, and
// 63 relocations in a 64-bit object.
//
// This encoding has a couple of interesting properties:
// 1. Looking at any entry, it is clear whether it's an address or a bitmap:
// even means address, odd means bitmap.
// 2. Just a simple list of addresses is a valid encoding.
Elf_Rela Rela;
Rela.r_info = 0;
Rela.r_addend = 0;
Rela.setType(getRelrRelocationType(), false);
std::vector<Elf_Rela> Relocs;
// Word type: uint32_t for Elf32, and uint64_t for Elf64.
typedef typename ELFT::uint Word;
// Word size in number of bytes.
const size_t WordSize = sizeof(Word);
// Number of bits used for the relocation offsets bitmap.
// These many relative relocations can be encoded in a single entry.
const size_t NBits = 8*WordSize - 1;
Word Base = 0;
for (const Elf_Relr &R : relrs) {
Word Entry = R;
if ((Entry&1) == 0) {
// Even entry: encodes the offset for next relocation.
Rela.r_offset = Entry;
Relocs.push_back(Rela);
// Set base offset for subsequent bitmap entries.
Base = Entry + WordSize;
continue;
}
// Odd entry: encodes bitmap for relocations starting at base.
Word Offset = Base;
while (Entry != 0) {
Entry >>= 1;
if ((Entry&1) != 0) {
Rela.r_offset = Offset;
Relocs.push_back(Rela);
}
Offset += WordSize;
}
// Advance base offset by NBits words.
Base += NBits * WordSize;
}
return Relocs;
}
template <class ELFT>
Expected<std::vector<typename ELFT::Rela>>
ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
// This function reads relocations in Android's packed relocation format,
// which is based on SLEB128 and delta encoding.
Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
if (!ContentsOrErr)
return ContentsOrErr.takeError();
const uint8_t *Cur = ContentsOrErr->begin();
const uint8_t *End = ContentsOrErr->end();
if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
Cur[2] != 'S' || Cur[3] != '2')
return createError("invalid packed relocation header");
Cur += 4;
const char *ErrStr = nullptr;
auto ReadSLEB = [&]() -> int64_t {
if (ErrStr)
return 0;
unsigned Len;
int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
Cur += Len;
return Result;
};
uint64_t NumRelocs = ReadSLEB();
uint64_t Offset = ReadSLEB();
uint64_t Addend = 0;
if (ErrStr)
return createError(ErrStr);
std::vector<Elf_Rela> Relocs;
Relocs.reserve(NumRelocs);
while (NumRelocs) {
uint64_t NumRelocsInGroup = ReadSLEB();
if (NumRelocsInGroup > NumRelocs)
return createError("relocation group unexpectedly large");
NumRelocs -= NumRelocsInGroup;
uint64_t GroupFlags = ReadSLEB();
bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
uint64_t GroupOffsetDelta;
if (GroupedByOffsetDelta)
GroupOffsetDelta = ReadSLEB();
uint64_t GroupRInfo;
if (GroupedByInfo)
GroupRInfo = ReadSLEB();
if (GroupedByAddend && GroupHasAddend)
Addend += ReadSLEB();
for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
Elf_Rela R;
Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
R.r_offset = Offset;
R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
if (GroupHasAddend) {
if (!GroupedByAddend)
Addend += ReadSLEB();
R.r_addend = Addend;
} else {
R.r_addend = 0;
}
Relocs.push_back(R);
if (ErrStr)
return createError(ErrStr);
}
if (ErrStr)
return createError(ErrStr);
}
return Relocs;
}
template class llvm::object::ELFFile<ELF32LE>;
template class llvm::object::ELFFile<ELF32BE>;
template class llvm::object::ELFFile<ELF64LE>;
template class llvm::object::ELFFile<ELF64BE>;
|