blob: f17a6da23d7d34c36dc33692511334330989af8c [file] [log] [blame]
Eugene Zelenko1df42fa2017-04-24 23:21:38 +00001//===- ELF.cpp - ELF object file implementation ---------------------------===//
Michael J. Spencer126973b2013-08-08 22:27:13 +00002//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michael J. Spencer126973b2013-08-08 22:27:13 +00006//
7//===----------------------------------------------------------------------===//
8
9#include "llvm/Object/ELF.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000010#include "llvm/BinaryFormat/ELF.h"
Peter Collingbourne689e6c052017-10-25 03:37:12 +000011#include "llvm/Support/LEB128.h"
Michael J. Spencer126973b2013-08-08 22:27:13 +000012
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000013using namespace llvm;
14using namespace object;
Michael J. Spencer126973b2013-08-08 22:27:13 +000015
Rafael Espindola3ba25732017-05-02 14:04:52 +000016#define STRINGIFY_ENUM_CASE(ns, name) \
17 case ns::name: \
18 return #name;
19
20#define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
Michael J. Spencer126973b2013-08-08 22:27:13 +000021
Eugene Zelenko1df42fa2017-04-24 23:21:38 +000022StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
23 uint32_t Type) {
Michael J. Spencer126973b2013-08-08 22:27:13 +000024 switch (Machine) {
25 case ELF::EM_X86_64:
26 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000027#include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +000028 default:
29 break;
30 }
31 break;
32 case ELF::EM_386:
Michael Kupersteina3b79dd2015-11-04 11:21:50 +000033 case ELF::EM_IAMCU:
Michael J. Spencer126973b2013-08-08 22:27:13 +000034 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000035#include "llvm/BinaryFormat/ELFRelocs/i386.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +000036 default:
37 break;
38 }
39 break;
40 case ELF::EM_MIPS:
41 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000042#include "llvm/BinaryFormat/ELFRelocs/Mips.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +000043 default:
44 break;
45 }
46 break;
47 case ELF::EM_AARCH64:
48 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000049#include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +000050 default:
51 break;
52 }
53 break;
54 case ELF::EM_ARM:
55 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000056#include "llvm/BinaryFormat/ELFRelocs/ARM.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +000057 default:
58 break;
59 }
60 break;
Leslie Zhai49277d12017-09-13 01:49:49 +000061 case ELF::EM_ARC_COMPACT:
62 case ELF::EM_ARC_COMPACT2:
63 switch (Type) {
64#include "llvm/BinaryFormat/ELFRelocs/ARC.def"
65 default:
66 break;
67 }
68 break;
Dylan McKay35047ed2016-09-28 13:23:42 +000069 case ELF::EM_AVR:
70 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000071#include "llvm/BinaryFormat/ELFRelocs/AVR.def"
Dylan McKay35047ed2016-09-28 13:23:42 +000072 default:
73 break;
74 }
75 break;
Michael J. Spencer126973b2013-08-08 22:27:13 +000076 case ELF::EM_HEXAGON:
77 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000078#include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +000079 default:
80 break;
81 }
82 break;
Jacques Pienaarea9f25a2016-03-01 21:21:42 +000083 case ELF::EM_LANAI:
84 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000085#include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
Jacques Pienaarea9f25a2016-03-01 21:21:42 +000086 default:
87 break;
88 }
89 break;
Michael J. Spencer126973b2013-08-08 22:27:13 +000090 case ELF::EM_PPC:
91 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000092#include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +000093 default:
94 break;
95 }
96 break;
97 case ELF::EM_PPC64:
98 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +000099#include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +0000100 default:
101 break;
102 }
103 break;
Alex Bradbury1524f622016-11-01 16:59:37 +0000104 case ELF::EM_RISCV:
105 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +0000106#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
Alex Bradbury1524f622016-11-01 16:59:37 +0000107 default:
108 break;
109 }
110 break;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000111 case ELF::EM_S390:
112 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +0000113#include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
Michael J. Spencer126973b2013-08-08 22:27:13 +0000114 default:
115 break;
116 }
117 break;
Venkatraman Govindarajucdee0ed2014-01-26 03:21:28 +0000118 case ELF::EM_SPARC:
119 case ELF::EM_SPARC32PLUS:
120 case ELF::EM_SPARCV9:
121 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +0000122#include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
Venkatraman Govindarajucdee0ed2014-01-26 03:21:28 +0000123 default:
124 break;
125 }
126 break;
Tom Stellardf8db61c2016-06-17 22:38:08 +0000127 case ELF::EM_AMDGPU:
128 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +0000129#include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
Tom Stellardf8db61c2016-06-17 22:38:08 +0000130 default:
131 break;
132 }
Adrian Prantl0e6694d2017-12-19 22:05:25 +0000133 break;
Alexei Starovoitovcfb51f52016-07-15 22:27:55 +0000134 case ELF::EM_BPF:
135 switch (Type) {
Zachary Turner264b5d92017-06-07 03:48:56 +0000136#include "llvm/BinaryFormat/ELFRelocs/BPF.def"
Alexei Starovoitovcfb51f52016-07-15 22:27:55 +0000137 default:
138 break;
139 }
Tom Stellardf8db61c2016-06-17 22:38:08 +0000140 break;
Anton Korobeynikov49045c62018-11-15 12:29:43 +0000141 case ELF::EM_MSP430:
142 switch (Type) {
143#include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
144 default:
145 break;
146 }
147 break;
Michael J. Spencer126973b2013-08-08 22:27:13 +0000148 default:
149 break;
150 }
151 return "Unknown";
152}
153
Tim Northover242785c2014-11-21 20:16:07 +0000154#undef ELF_RELOC
Rafael Espindola3ba25732017-05-02 14:04:52 +0000155
Fangrui Songd2ed5be2018-12-14 07:46:58 +0000156uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000157 switch (Machine) {
158 case ELF::EM_X86_64:
159 return ELF::R_X86_64_RELATIVE;
160 case ELF::EM_386:
161 case ELF::EM_IAMCU:
162 return ELF::R_386_RELATIVE;
163 case ELF::EM_MIPS:
164 break;
165 case ELF::EM_AARCH64:
166 return ELF::R_AARCH64_RELATIVE;
167 case ELF::EM_ARM:
168 return ELF::R_ARM_RELATIVE;
169 case ELF::EM_ARC_COMPACT:
170 case ELF::EM_ARC_COMPACT2:
171 return ELF::R_ARC_RELATIVE;
172 case ELF::EM_AVR:
173 break;
174 case ELF::EM_HEXAGON:
175 return ELF::R_HEX_RELATIVE;
176 case ELF::EM_LANAI:
177 break;
178 case ELF::EM_PPC:
179 break;
180 case ELF::EM_PPC64:
181 return ELF::R_PPC64_RELATIVE;
182 case ELF::EM_RISCV:
183 return ELF::R_RISCV_RELATIVE;
184 case ELF::EM_S390:
185 return ELF::R_390_RELATIVE;
186 case ELF::EM_SPARC:
187 case ELF::EM_SPARC32PLUS:
188 case ELF::EM_SPARCV9:
189 return ELF::R_SPARC_RELATIVE;
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000190 case ELF::EM_AMDGPU:
191 break;
192 case ELF::EM_BPF:
193 break;
194 default:
195 break;
196 }
197 return 0;
198}
199
Rafael Espindola3ba25732017-05-02 14:04:52 +0000200StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
201 switch (Machine) {
202 case ELF::EM_ARM:
203 switch (Type) {
204 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
205 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
206 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
207 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
208 STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
209 }
210 break;
211 case ELF::EM_HEXAGON:
212 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED); }
213 break;
214 case ELF::EM_X86_64:
215 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
216 break;
217 case ELF::EM_MIPS:
218 case ELF::EM_MIPS_RS3_LE:
219 switch (Type) {
220 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
221 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
Rafael Espindola3ba25732017-05-02 14:04:52 +0000222 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
Fangrui Song61cd3682019-02-21 10:19:08 +0000223 STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
Rafael Espindola3ba25732017-05-02 14:04:52 +0000224 }
225 break;
226 default:
227 break;
228 }
229
230 switch (Type) {
231 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
232 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
233 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
234 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
235 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
236 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
237 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
238 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
239 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
240 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
241 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
242 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
243 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
244 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
245 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
246 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
247 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000248 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
Peter Collingbourne5c54f152017-10-27 17:49:40 +0000249 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
250 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000251 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
Peter Collingbournef0e26e72017-06-14 18:52:12 +0000252 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000253 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000254 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
Peter Collingbourne3e227332018-07-17 22:17:18 +0000255 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
Ben Dunbobbin1d165152019-05-17 03:44:15 +0000256 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
Peter Collingbourne31fda092019-05-29 03:29:01 +0000257 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
Fangrui Song9d2504b2019-09-06 00:53:28 +0000258 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
259 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
Rafael Espindola3ba25732017-05-02 14:04:52 +0000260 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
261 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
262 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
263 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
264 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
265 default:
266 return "Unknown";
267 }
268}
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000269
270template <class ELFT>
271Expected<std::vector<typename ELFT::Rela>>
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000272ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
273 // This function decodes the contents of an SHT_RELR packed relocation
274 // section.
275 //
276 // Proposal for adding SHT_RELR sections to generic-abi is here:
277 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
278 //
279 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
280 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
281 //
282 // i.e. start with an address, followed by any number of bitmaps. The address
283 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
284 // relocations each, at subsequent offsets following the last address entry.
285 //
286 // The bitmap entries must have 1 in the least significant bit. The assumption
287 // here is that an address cannot have 1 in lsb. Odd addresses are not
288 // supported.
289 //
290 // Excluding the least significant bit in the bitmap, each non-zero bit in
291 // the bitmap represents a relocation to be applied to a corresponding machine
292 // word that follows the base address word. The second least significant bit
293 // represents the machine word immediately following the initial address, and
294 // each bit that follows represents the next word, in linear order. As such,
295 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
296 // 63 relocations in a 64-bit object.
297 //
298 // This encoding has a couple of interesting properties:
299 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
300 // even means address, odd means bitmap.
301 // 2. Just a simple list of addresses is a valid encoding.
302
303 Elf_Rela Rela;
304 Rela.r_info = 0;
305 Rela.r_addend = 0;
Fangrui Songd2ed5be2018-12-14 07:46:58 +0000306 Rela.setType(getRelativeRelocationType(), false);
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000307 std::vector<Elf_Rela> Relocs;
308
309 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
310 typedef typename ELFT::uint Word;
311
312 // Word size in number of bytes.
313 const size_t WordSize = sizeof(Word);
314
315 // Number of bits used for the relocation offsets bitmap.
316 // These many relative relocations can be encoded in a single entry.
317 const size_t NBits = 8*WordSize - 1;
318
319 Word Base = 0;
320 for (const Elf_Relr &R : relrs) {
321 Word Entry = R;
322 if ((Entry&1) == 0) {
323 // Even entry: encodes the offset for next relocation.
324 Rela.r_offset = Entry;
325 Relocs.push_back(Rela);
326 // Set base offset for subsequent bitmap entries.
327 Base = Entry + WordSize;
328 continue;
329 }
330
331 // Odd entry: encodes bitmap for relocations starting at base.
332 Word Offset = Base;
333 while (Entry != 0) {
334 Entry >>= 1;
335 if ((Entry&1) != 0) {
336 Rela.r_offset = Offset;
337 Relocs.push_back(Rela);
338 }
339 Offset += WordSize;
340 }
341
342 // Advance base offset by NBits words.
343 Base += NBits * WordSize;
344 }
345
346 return Relocs;
347}
348
349template <class ELFT>
350Expected<std::vector<typename ELFT::Rela>>
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000351ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
352 // This function reads relocations in Android's packed relocation format,
353 // which is based on SLEB128 and delta encoding.
354 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
355 if (!ContentsOrErr)
356 return ContentsOrErr.takeError();
357 const uint8_t *Cur = ContentsOrErr->begin();
358 const uint8_t *End = ContentsOrErr->end();
359 if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
360 Cur[2] != 'S' || Cur[3] != '2')
361 return createError("invalid packed relocation header");
362 Cur += 4;
363
364 const char *ErrStr = nullptr;
365 auto ReadSLEB = [&]() -> int64_t {
366 if (ErrStr)
367 return 0;
368 unsigned Len;
369 int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
370 Cur += Len;
371 return Result;
372 };
373
374 uint64_t NumRelocs = ReadSLEB();
375 uint64_t Offset = ReadSLEB();
376 uint64_t Addend = 0;
377
378 if (ErrStr)
379 return createError(ErrStr);
380
381 std::vector<Elf_Rela> Relocs;
382 Relocs.reserve(NumRelocs);
383 while (NumRelocs) {
384 uint64_t NumRelocsInGroup = ReadSLEB();
385 if (NumRelocsInGroup > NumRelocs)
386 return createError("relocation group unexpectedly large");
387 NumRelocs -= NumRelocsInGroup;
388
389 uint64_t GroupFlags = ReadSLEB();
390 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
391 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
392 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
393 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
394
395 uint64_t GroupOffsetDelta;
396 if (GroupedByOffsetDelta)
397 GroupOffsetDelta = ReadSLEB();
398
399 uint64_t GroupRInfo;
400 if (GroupedByInfo)
401 GroupRInfo = ReadSLEB();
402
403 if (GroupedByAddend && GroupHasAddend)
404 Addend += ReadSLEB();
405
Peter Collingbourne62e4fc42018-08-15 17:58:22 +0000406 if (!GroupHasAddend)
407 Addend = 0;
408
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000409 for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
410 Elf_Rela R;
411 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
412 R.r_offset = Offset;
413 R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
Peter Collingbourne62e4fc42018-08-15 17:58:22 +0000414 if (GroupHasAddend && !GroupedByAddend)
415 Addend += ReadSLEB();
416 R.r_addend = Addend;
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000417 Relocs.push_back(R);
418
419 if (ErrStr)
420 return createError(ErrStr);
421 }
422
423 if (ErrStr)
424 return createError(ErrStr);
425 }
426
427 return Relocs;
428}
429
Paul Semel0913dcd2018-07-25 11:09:20 +0000430template <class ELFT>
Xing GUOb2858782019-03-02 04:20:28 +0000431std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
Paul Semel0913dcd2018-07-25 11:09:20 +0000432 uint64_t Type) const {
433#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
434 case value: \
435 return #tag;
436
437#define DYNAMIC_TAG(n, v)
438 switch (Arch) {
Peter Smith49d72212019-06-04 11:44:33 +0000439 case ELF::EM_AARCH64:
440 switch (Type) {
441#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
442#include "llvm/BinaryFormat/DynamicTags.def"
443#undef AARCH64_DYNAMIC_TAG
444 }
445 break;
446
Paul Semel0913dcd2018-07-25 11:09:20 +0000447 case ELF::EM_HEXAGON:
448 switch (Type) {
449#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
450#include "llvm/BinaryFormat/DynamicTags.def"
451#undef HEXAGON_DYNAMIC_TAG
452 }
Jonas Hahnfeldc64d73c2019-03-13 10:38:17 +0000453 break;
Paul Semel0913dcd2018-07-25 11:09:20 +0000454
455 case ELF::EM_MIPS:
456 switch (Type) {
457#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
458#include "llvm/BinaryFormat/DynamicTags.def"
459#undef MIPS_DYNAMIC_TAG
460 }
Jonas Hahnfeldc64d73c2019-03-13 10:38:17 +0000461 break;
Paul Semel0913dcd2018-07-25 11:09:20 +0000462
463 case ELF::EM_PPC64:
464 switch (Type) {
465#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
466#include "llvm/BinaryFormat/DynamicTags.def"
467#undef PPC64_DYNAMIC_TAG
468 }
Jonas Hahnfeldc64d73c2019-03-13 10:38:17 +0000469 break;
Paul Semel0913dcd2018-07-25 11:09:20 +0000470 }
471#undef DYNAMIC_TAG
472 switch (Type) {
473// Now handle all dynamic tags except the architecture specific ones
Peter Smith49d72212019-06-04 11:44:33 +0000474#define AARCH64_DYNAMIC_TAG(name, value)
Paul Semel0913dcd2018-07-25 11:09:20 +0000475#define MIPS_DYNAMIC_TAG(name, value)
476#define HEXAGON_DYNAMIC_TAG(name, value)
477#define PPC64_DYNAMIC_TAG(name, value)
478// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
479#define DYNAMIC_TAG_MARKER(name, value)
Georgii Rymar301cb912019-12-23 14:54:36 +0300480#define DYNAMIC_TAG(name, value) case value: return #name;
Paul Semel0913dcd2018-07-25 11:09:20 +0000481#include "llvm/BinaryFormat/DynamicTags.def"
482#undef DYNAMIC_TAG
Peter Smith49d72212019-06-04 11:44:33 +0000483#undef AARCH64_DYNAMIC_TAG
Paul Semel0913dcd2018-07-25 11:09:20 +0000484#undef MIPS_DYNAMIC_TAG
485#undef HEXAGON_DYNAMIC_TAG
486#undef PPC64_DYNAMIC_TAG
487#undef DYNAMIC_TAG_MARKER
488#undef DYNAMIC_STRINGIFY_ENUM
489 default:
Xing GUOb2858782019-03-02 04:20:28 +0000490 return "<unknown:>0x" + utohexstr(Type, true);
Paul Semel0913dcd2018-07-25 11:09:20 +0000491 }
492}
493
494template <class ELFT>
Xing GUOb2858782019-03-02 04:20:28 +0000495std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
Paul Semel0913dcd2018-07-25 11:09:20 +0000496 return getDynamicTagAsString(getHeader()->e_machine, Type);
497}
498
499template <class ELFT>
500Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
501 ArrayRef<Elf_Dyn> Dyn;
502 size_t DynSecSize = 0;
503
504 auto ProgramHeadersOrError = program_headers();
505 if (!ProgramHeadersOrError)
506 return ProgramHeadersOrError.takeError();
507
508 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
509 if (Phdr.p_type == ELF::PT_DYNAMIC) {
510 Dyn = makeArrayRef(
511 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
512 Phdr.p_filesz / sizeof(Elf_Dyn));
513 DynSecSize = Phdr.p_filesz;
514 break;
515 }
516 }
517
518 // If we can't find the dynamic section in the program headers, we just fall
519 // back on the sections.
520 if (Dyn.empty()) {
521 auto SectionsOrError = sections();
522 if (!SectionsOrError)
523 return SectionsOrError.takeError();
524
525 for (const Elf_Shdr &Sec : *SectionsOrError) {
526 if (Sec.sh_type == ELF::SHT_DYNAMIC) {
527 Expected<ArrayRef<Elf_Dyn>> DynOrError =
528 getSectionContentsAsArray<Elf_Dyn>(&Sec);
529 if (!DynOrError)
530 return DynOrError.takeError();
531 Dyn = *DynOrError;
532 DynSecSize = Sec.sh_size;
533 break;
534 }
535 }
536
537 if (!Dyn.data())
538 return ArrayRef<Elf_Dyn>();
539 }
540
541 if (Dyn.empty())
George Rimard0921a42019-07-05 11:28:49 +0000542 // TODO: this error is untested.
Paul Semel0913dcd2018-07-25 11:09:20 +0000543 return createError("invalid empty dynamic section");
544
545 if (DynSecSize % sizeof(Elf_Dyn) != 0)
George Rimard0921a42019-07-05 11:28:49 +0000546 // TODO: this error is untested.
Paul Semel0913dcd2018-07-25 11:09:20 +0000547 return createError("malformed dynamic section");
548
549 if (Dyn.back().d_tag != ELF::DT_NULL)
George Rimard0921a42019-07-05 11:28:49 +0000550 // TODO: this error is untested.
Paul Semel0913dcd2018-07-25 11:09:20 +0000551 return createError("dynamic sections must be DT_NULL terminated");
552
553 return Dyn;
554}
555
556template <class ELFT>
557Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
558 auto ProgramHeadersOrError = program_headers();
559 if (!ProgramHeadersOrError)
560 return ProgramHeadersOrError.takeError();
561
562 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
563
564 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
565 if (Phdr.p_type == ELF::PT_LOAD)
566 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
567
568 const Elf_Phdr *const *I =
569 std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
570 [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
571 return VAddr < Phdr->p_vaddr;
572 });
573
574 if (I == LoadSegments.begin())
George Rimard0921a42019-07-05 11:28:49 +0000575 return createError("virtual address is not in any segment: 0x" +
576 Twine::utohexstr(VAddr));
Paul Semel0913dcd2018-07-25 11:09:20 +0000577 --I;
578 const Elf_Phdr &Phdr = **I;
579 uint64_t Delta = VAddr - Phdr.p_vaddr;
580 if (Delta >= Phdr.p_filesz)
George Rimard0921a42019-07-05 11:28:49 +0000581 return createError("virtual address is not in any segment: 0x" +
582 Twine::utohexstr(VAddr));
Paul Semel0913dcd2018-07-25 11:09:20 +0000583 return base() + Phdr.p_offset + Delta;
584}
585
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000586template class llvm::object::ELFFile<ELF32LE>;
587template class llvm::object::ELFFile<ELF32BE>;
588template class llvm::object::ELFFile<ELF64LE>;
589template class llvm::object::ELFFile<ELF64BE>;