blob: e58d62382a554b65f9fdf0f2f51dda6452baf218 [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;
Kai Wang581ba352020-02-04 22:20:10 +0800226 case ELF::EM_RISCV:
227 switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
228 break;
Rafael Espindola3ba25732017-05-02 14:04:52 +0000229 default:
230 break;
231 }
232
233 switch (Type) {
234 STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
235 STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
236 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
237 STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
238 STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
239 STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
240 STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
241 STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
242 STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
243 STRINGIFY_ENUM_CASE(ELF, SHT_REL);
244 STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
245 STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
246 STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
247 STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
248 STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
249 STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
250 STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000251 STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
Peter Collingbourne5c54f152017-10-27 17:49:40 +0000252 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
253 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000254 STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
Peter Collingbournef0e26e72017-06-14 18:52:12 +0000255 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
Saleem Abdulrasoolb36fbbc2018-01-30 16:29:29 +0000256 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
Michael J. Spencerae6eeae2018-06-02 16:33:01 +0000257 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
Peter Collingbourne3e227332018-07-17 22:17:18 +0000258 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
Ben Dunbobbin1d165152019-05-17 03:44:15 +0000259 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
Peter Collingbourne31fda092019-05-29 03:29:01 +0000260 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
Fangrui Song9d2504b2019-09-06 00:53:28 +0000261 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
262 STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
Rafael Espindola3ba25732017-05-02 14:04:52 +0000263 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
264 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
265 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
266 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
267 STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
268 default:
269 return "Unknown";
270 }
271}
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000272
273template <class ELFT>
274Expected<std::vector<typename ELFT::Rela>>
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000275ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
276 // This function decodes the contents of an SHT_RELR packed relocation
277 // section.
278 //
279 // Proposal for adding SHT_RELR sections to generic-abi is here:
280 // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
281 //
282 // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
283 // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
284 //
285 // i.e. start with an address, followed by any number of bitmaps. The address
286 // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
287 // relocations each, at subsequent offsets following the last address entry.
288 //
289 // The bitmap entries must have 1 in the least significant bit. The assumption
290 // here is that an address cannot have 1 in lsb. Odd addresses are not
291 // supported.
292 //
293 // Excluding the least significant bit in the bitmap, each non-zero bit in
294 // the bitmap represents a relocation to be applied to a corresponding machine
295 // word that follows the base address word. The second least significant bit
296 // represents the machine word immediately following the initial address, and
297 // each bit that follows represents the next word, in linear order. As such,
298 // a single bitmap can encode up to 31 relocations in a 32-bit object, and
299 // 63 relocations in a 64-bit object.
300 //
301 // This encoding has a couple of interesting properties:
302 // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
303 // even means address, odd means bitmap.
304 // 2. Just a simple list of addresses is a valid encoding.
305
306 Elf_Rela Rela;
307 Rela.r_info = 0;
308 Rela.r_addend = 0;
Fangrui Songd2ed5be2018-12-14 07:46:58 +0000309 Rela.setType(getRelativeRelocationType(), false);
Jake Ehrlich0f440d82018-06-28 21:07:34 +0000310 std::vector<Elf_Rela> Relocs;
311
312 // Word type: uint32_t for Elf32, and uint64_t for Elf64.
313 typedef typename ELFT::uint Word;
314
315 // Word size in number of bytes.
316 const size_t WordSize = sizeof(Word);
317
318 // Number of bits used for the relocation offsets bitmap.
319 // These many relative relocations can be encoded in a single entry.
320 const size_t NBits = 8*WordSize - 1;
321
322 Word Base = 0;
323 for (const Elf_Relr &R : relrs) {
324 Word Entry = R;
325 if ((Entry&1) == 0) {
326 // Even entry: encodes the offset for next relocation.
327 Rela.r_offset = Entry;
328 Relocs.push_back(Rela);
329 // Set base offset for subsequent bitmap entries.
330 Base = Entry + WordSize;
331 continue;
332 }
333
334 // Odd entry: encodes bitmap for relocations starting at base.
335 Word Offset = Base;
336 while (Entry != 0) {
337 Entry >>= 1;
338 if ((Entry&1) != 0) {
339 Rela.r_offset = Offset;
340 Relocs.push_back(Rela);
341 }
342 Offset += WordSize;
343 }
344
345 // Advance base offset by NBits words.
346 Base += NBits * WordSize;
347 }
348
349 return Relocs;
350}
351
352template <class ELFT>
353Expected<std::vector<typename ELFT::Rela>>
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000354ELFFile<ELFT>::android_relas(const Elf_Shdr *Sec) const {
355 // This function reads relocations in Android's packed relocation format,
356 // which is based on SLEB128 and delta encoding.
357 Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
358 if (!ContentsOrErr)
359 return ContentsOrErr.takeError();
360 const uint8_t *Cur = ContentsOrErr->begin();
361 const uint8_t *End = ContentsOrErr->end();
362 if (ContentsOrErr->size() < 4 || Cur[0] != 'A' || Cur[1] != 'P' ||
363 Cur[2] != 'S' || Cur[3] != '2')
364 return createError("invalid packed relocation header");
365 Cur += 4;
366
367 const char *ErrStr = nullptr;
368 auto ReadSLEB = [&]() -> int64_t {
369 if (ErrStr)
370 return 0;
371 unsigned Len;
372 int64_t Result = decodeSLEB128(Cur, &Len, End, &ErrStr);
373 Cur += Len;
374 return Result;
375 };
376
377 uint64_t NumRelocs = ReadSLEB();
378 uint64_t Offset = ReadSLEB();
379 uint64_t Addend = 0;
380
381 if (ErrStr)
382 return createError(ErrStr);
383
384 std::vector<Elf_Rela> Relocs;
385 Relocs.reserve(NumRelocs);
386 while (NumRelocs) {
387 uint64_t NumRelocsInGroup = ReadSLEB();
388 if (NumRelocsInGroup > NumRelocs)
389 return createError("relocation group unexpectedly large");
390 NumRelocs -= NumRelocsInGroup;
391
392 uint64_t GroupFlags = ReadSLEB();
393 bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
394 bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
395 bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
396 bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
397
398 uint64_t GroupOffsetDelta;
399 if (GroupedByOffsetDelta)
400 GroupOffsetDelta = ReadSLEB();
401
402 uint64_t GroupRInfo;
403 if (GroupedByInfo)
404 GroupRInfo = ReadSLEB();
405
406 if (GroupedByAddend && GroupHasAddend)
407 Addend += ReadSLEB();
408
Peter Collingbourne62e4fc42018-08-15 17:58:22 +0000409 if (!GroupHasAddend)
410 Addend = 0;
411
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000412 for (uint64_t I = 0; I != NumRelocsInGroup; ++I) {
413 Elf_Rela R;
414 Offset += GroupedByOffsetDelta ? GroupOffsetDelta : ReadSLEB();
415 R.r_offset = Offset;
416 R.r_info = GroupedByInfo ? GroupRInfo : ReadSLEB();
Peter Collingbourne62e4fc42018-08-15 17:58:22 +0000417 if (GroupHasAddend && !GroupedByAddend)
418 Addend += ReadSLEB();
419 R.r_addend = Addend;
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000420 Relocs.push_back(R);
421
422 if (ErrStr)
423 return createError(ErrStr);
424 }
425
426 if (ErrStr)
427 return createError(ErrStr);
428 }
429
430 return Relocs;
431}
432
Paul Semel0913dcd2018-07-25 11:09:20 +0000433template <class ELFT>
Xing GUOb2858782019-03-02 04:20:28 +0000434std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
Paul Semel0913dcd2018-07-25 11:09:20 +0000435 uint64_t Type) const {
436#define DYNAMIC_STRINGIFY_ENUM(tag, value) \
437 case value: \
438 return #tag;
439
440#define DYNAMIC_TAG(n, v)
441 switch (Arch) {
Peter Smith49d72212019-06-04 11:44:33 +0000442 case ELF::EM_AARCH64:
443 switch (Type) {
444#define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
445#include "llvm/BinaryFormat/DynamicTags.def"
446#undef AARCH64_DYNAMIC_TAG
447 }
448 break;
449
Paul Semel0913dcd2018-07-25 11:09:20 +0000450 case ELF::EM_HEXAGON:
451 switch (Type) {
452#define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
453#include "llvm/BinaryFormat/DynamicTags.def"
454#undef HEXAGON_DYNAMIC_TAG
455 }
Jonas Hahnfeldc64d73c2019-03-13 10:38:17 +0000456 break;
Paul Semel0913dcd2018-07-25 11:09:20 +0000457
458 case ELF::EM_MIPS:
459 switch (Type) {
460#define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
461#include "llvm/BinaryFormat/DynamicTags.def"
462#undef MIPS_DYNAMIC_TAG
463 }
Jonas Hahnfeldc64d73c2019-03-13 10:38:17 +0000464 break;
Paul Semel0913dcd2018-07-25 11:09:20 +0000465
466 case ELF::EM_PPC64:
467 switch (Type) {
468#define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
469#include "llvm/BinaryFormat/DynamicTags.def"
470#undef PPC64_DYNAMIC_TAG
471 }
Jonas Hahnfeldc64d73c2019-03-13 10:38:17 +0000472 break;
Paul Semel0913dcd2018-07-25 11:09:20 +0000473 }
474#undef DYNAMIC_TAG
475 switch (Type) {
476// Now handle all dynamic tags except the architecture specific ones
Peter Smith49d72212019-06-04 11:44:33 +0000477#define AARCH64_DYNAMIC_TAG(name, value)
Paul Semel0913dcd2018-07-25 11:09:20 +0000478#define MIPS_DYNAMIC_TAG(name, value)
479#define HEXAGON_DYNAMIC_TAG(name, value)
480#define PPC64_DYNAMIC_TAG(name, value)
481// Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
482#define DYNAMIC_TAG_MARKER(name, value)
Georgii Rymar301cb912019-12-23 14:54:36 +0300483#define DYNAMIC_TAG(name, value) case value: return #name;
Paul Semel0913dcd2018-07-25 11:09:20 +0000484#include "llvm/BinaryFormat/DynamicTags.def"
485#undef DYNAMIC_TAG
Peter Smith49d72212019-06-04 11:44:33 +0000486#undef AARCH64_DYNAMIC_TAG
Paul Semel0913dcd2018-07-25 11:09:20 +0000487#undef MIPS_DYNAMIC_TAG
488#undef HEXAGON_DYNAMIC_TAG
489#undef PPC64_DYNAMIC_TAG
490#undef DYNAMIC_TAG_MARKER
491#undef DYNAMIC_STRINGIFY_ENUM
492 default:
Xing GUOb2858782019-03-02 04:20:28 +0000493 return "<unknown:>0x" + utohexstr(Type, true);
Paul Semel0913dcd2018-07-25 11:09:20 +0000494 }
495}
496
497template <class ELFT>
Xing GUOb2858782019-03-02 04:20:28 +0000498std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
Paul Semel0913dcd2018-07-25 11:09:20 +0000499 return getDynamicTagAsString(getHeader()->e_machine, Type);
500}
501
502template <class ELFT>
503Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
504 ArrayRef<Elf_Dyn> Dyn;
505 size_t DynSecSize = 0;
506
507 auto ProgramHeadersOrError = program_headers();
508 if (!ProgramHeadersOrError)
509 return ProgramHeadersOrError.takeError();
510
511 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
512 if (Phdr.p_type == ELF::PT_DYNAMIC) {
513 Dyn = makeArrayRef(
514 reinterpret_cast<const Elf_Dyn *>(base() + Phdr.p_offset),
515 Phdr.p_filesz / sizeof(Elf_Dyn));
516 DynSecSize = Phdr.p_filesz;
517 break;
518 }
519 }
520
521 // If we can't find the dynamic section in the program headers, we just fall
522 // back on the sections.
523 if (Dyn.empty()) {
524 auto SectionsOrError = sections();
525 if (!SectionsOrError)
526 return SectionsOrError.takeError();
527
528 for (const Elf_Shdr &Sec : *SectionsOrError) {
529 if (Sec.sh_type == ELF::SHT_DYNAMIC) {
530 Expected<ArrayRef<Elf_Dyn>> DynOrError =
531 getSectionContentsAsArray<Elf_Dyn>(&Sec);
532 if (!DynOrError)
533 return DynOrError.takeError();
534 Dyn = *DynOrError;
535 DynSecSize = Sec.sh_size;
536 break;
537 }
538 }
539
540 if (!Dyn.data())
541 return ArrayRef<Elf_Dyn>();
542 }
543
544 if (Dyn.empty())
George Rimard0921a42019-07-05 11:28:49 +0000545 // TODO: this error is untested.
Paul Semel0913dcd2018-07-25 11:09:20 +0000546 return createError("invalid empty dynamic section");
547
548 if (DynSecSize % sizeof(Elf_Dyn) != 0)
George Rimard0921a42019-07-05 11:28:49 +0000549 // TODO: this error is untested.
Paul Semel0913dcd2018-07-25 11:09:20 +0000550 return createError("malformed dynamic section");
551
552 if (Dyn.back().d_tag != ELF::DT_NULL)
George Rimard0921a42019-07-05 11:28:49 +0000553 // TODO: this error is untested.
Paul Semel0913dcd2018-07-25 11:09:20 +0000554 return createError("dynamic sections must be DT_NULL terminated");
555
556 return Dyn;
557}
558
559template <class ELFT>
560Expected<const uint8_t *> ELFFile<ELFT>::toMappedAddr(uint64_t VAddr) const {
561 auto ProgramHeadersOrError = program_headers();
562 if (!ProgramHeadersOrError)
563 return ProgramHeadersOrError.takeError();
564
565 llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
566
567 for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
568 if (Phdr.p_type == ELF::PT_LOAD)
569 LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
570
571 const Elf_Phdr *const *I =
572 std::upper_bound(LoadSegments.begin(), LoadSegments.end(), VAddr,
573 [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
574 return VAddr < Phdr->p_vaddr;
575 });
576
577 if (I == LoadSegments.begin())
George Rimard0921a42019-07-05 11:28:49 +0000578 return createError("virtual address is not in any segment: 0x" +
579 Twine::utohexstr(VAddr));
Paul Semel0913dcd2018-07-25 11:09:20 +0000580 --I;
581 const Elf_Phdr &Phdr = **I;
582 uint64_t Delta = VAddr - Phdr.p_vaddr;
583 if (Delta >= Phdr.p_filesz)
George Rimard0921a42019-07-05 11:28:49 +0000584 return createError("virtual address is not in any segment: 0x" +
585 Twine::utohexstr(VAddr));
Georgii Rymar30c1f9a2020-03-24 17:26:52 +0300586
587 uint64_t Offset = Phdr.p_offset + Delta;
588 if (Offset >= getBufSize())
589 return createError("can't map virtual address 0x" +
590 Twine::utohexstr(VAddr) + " to the segment with index " +
591 Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
592 ": the segment ends at 0x" +
593 Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
594 ", which is greater than the file size (0x" +
595 Twine::utohexstr(getBufSize()) + ")");
596
597 return base() + Offset;
Paul Semel0913dcd2018-07-25 11:09:20 +0000598}
599
Peter Collingbourne689e6c052017-10-25 03:37:12 +0000600template class llvm::object::ELFFile<ELF32LE>;
601template class llvm::object::ELFFile<ELF32BE>;
602template class llvm::object::ELFFile<ELF64LE>;
603template class llvm::object::ELFFile<ELF64BE>;