112f3ca4cSDimitry Andric //===- ELF.cpp - ELF object file implementation ---------------------------===//
2f8af5cf6SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f8af5cf6SDimitry Andric //
7f8af5cf6SDimitry Andric //===----------------------------------------------------------------------===//
8f8af5cf6SDimitry Andric
9f8af5cf6SDimitry Andric #include "llvm/Object/ELF.h"
107fa27ce4SDimitry Andric #include "llvm/ADT/StringExtras.h"
117ab83427SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
12344a3780SDimitry Andric #include "llvm/Support/DataExtractor.h"
13f8af5cf6SDimitry Andric
1412f3ca4cSDimitry Andric using namespace llvm;
1512f3ca4cSDimitry Andric using namespace object;
16f8af5cf6SDimitry Andric
17a303c417SDimitry Andric #define STRINGIFY_ENUM_CASE(ns, name) \
18a303c417SDimitry Andric case ns::name: \
19a303c417SDimitry Andric return #name;
20a303c417SDimitry Andric
21a303c417SDimitry Andric #define ELF_RELOC(name, value) STRINGIFY_ENUM_CASE(ELF, name)
22f8af5cf6SDimitry Andric
getELFRelocationTypeName(uint32_t Machine,uint32_t Type)2312f3ca4cSDimitry Andric StringRef llvm::object::getELFRelocationTypeName(uint32_t Machine,
2412f3ca4cSDimitry Andric uint32_t Type) {
25f8af5cf6SDimitry Andric switch (Machine) {
26344a3780SDimitry Andric case ELF::EM_68K:
27344a3780SDimitry Andric switch (Type) {
28344a3780SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/M68k.def"
29344a3780SDimitry Andric default:
30344a3780SDimitry Andric break;
31344a3780SDimitry Andric }
32344a3780SDimitry Andric break;
33f8af5cf6SDimitry Andric case ELF::EM_X86_64:
34f8af5cf6SDimitry Andric switch (Type) {
357ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/x86_64.def"
36f8af5cf6SDimitry Andric default:
37f8af5cf6SDimitry Andric break;
38f8af5cf6SDimitry Andric }
39f8af5cf6SDimitry Andric break;
40f8af5cf6SDimitry Andric case ELF::EM_386:
41dd58ef01SDimitry Andric case ELF::EM_IAMCU:
42f8af5cf6SDimitry Andric switch (Type) {
437ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/i386.def"
44f8af5cf6SDimitry Andric default:
45f8af5cf6SDimitry Andric break;
46f8af5cf6SDimitry Andric }
47f8af5cf6SDimitry Andric break;
48f8af5cf6SDimitry Andric case ELF::EM_MIPS:
49f8af5cf6SDimitry Andric switch (Type) {
507ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Mips.def"
51f8af5cf6SDimitry Andric default:
52f8af5cf6SDimitry Andric break;
53f8af5cf6SDimitry Andric }
54f8af5cf6SDimitry Andric break;
55f8af5cf6SDimitry Andric case ELF::EM_AARCH64:
56f8af5cf6SDimitry Andric switch (Type) {
577ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/AArch64.def"
58f8af5cf6SDimitry Andric default:
59f8af5cf6SDimitry Andric break;
60f8af5cf6SDimitry Andric }
61f8af5cf6SDimitry Andric break;
62f8af5cf6SDimitry Andric case ELF::EM_ARM:
63f8af5cf6SDimitry Andric switch (Type) {
647ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/ARM.def"
65f8af5cf6SDimitry Andric default:
66f8af5cf6SDimitry Andric break;
67f8af5cf6SDimitry Andric }
68f8af5cf6SDimitry Andric break;
69044eb2f6SDimitry Andric case ELF::EM_ARC_COMPACT:
70044eb2f6SDimitry Andric case ELF::EM_ARC_COMPACT2:
71044eb2f6SDimitry Andric switch (Type) {
72044eb2f6SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/ARC.def"
73044eb2f6SDimitry Andric default:
74044eb2f6SDimitry Andric break;
75044eb2f6SDimitry Andric }
76044eb2f6SDimitry Andric break;
77b915e9e0SDimitry Andric case ELF::EM_AVR:
78b915e9e0SDimitry Andric switch (Type) {
797ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/AVR.def"
80b915e9e0SDimitry Andric default:
81b915e9e0SDimitry Andric break;
82b915e9e0SDimitry Andric }
83b915e9e0SDimitry Andric break;
84f8af5cf6SDimitry Andric case ELF::EM_HEXAGON:
85f8af5cf6SDimitry Andric switch (Type) {
867ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Hexagon.def"
87f8af5cf6SDimitry Andric default:
88f8af5cf6SDimitry Andric break;
89f8af5cf6SDimitry Andric }
90f8af5cf6SDimitry Andric break;
9101095a5dSDimitry Andric case ELF::EM_LANAI:
9201095a5dSDimitry Andric switch (Type) {
937ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Lanai.def"
9401095a5dSDimitry Andric default:
9501095a5dSDimitry Andric break;
9601095a5dSDimitry Andric }
9701095a5dSDimitry Andric break;
98f8af5cf6SDimitry Andric case ELF::EM_PPC:
99f8af5cf6SDimitry Andric switch (Type) {
1007ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/PowerPC.def"
101f8af5cf6SDimitry Andric default:
102f8af5cf6SDimitry Andric break;
103f8af5cf6SDimitry Andric }
104f8af5cf6SDimitry Andric break;
105f8af5cf6SDimitry Andric case ELF::EM_PPC64:
106f8af5cf6SDimitry Andric switch (Type) {
1077ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/PowerPC64.def"
108f8af5cf6SDimitry Andric default:
109f8af5cf6SDimitry Andric break;
110f8af5cf6SDimitry Andric }
111f8af5cf6SDimitry Andric break;
112b915e9e0SDimitry Andric case ELF::EM_RISCV:
113b915e9e0SDimitry Andric switch (Type) {
1147ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
115b915e9e0SDimitry Andric default:
116b915e9e0SDimitry Andric break;
117b915e9e0SDimitry Andric }
118b915e9e0SDimitry Andric break;
119f8af5cf6SDimitry Andric case ELF::EM_S390:
120f8af5cf6SDimitry Andric switch (Type) {
1217ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/SystemZ.def"
122f8af5cf6SDimitry Andric default:
123f8af5cf6SDimitry Andric break;
124f8af5cf6SDimitry Andric }
125f8af5cf6SDimitry Andric break;
1265ca98fd9SDimitry Andric case ELF::EM_SPARC:
1275ca98fd9SDimitry Andric case ELF::EM_SPARC32PLUS:
1285ca98fd9SDimitry Andric case ELF::EM_SPARCV9:
1295ca98fd9SDimitry Andric switch (Type) {
1307ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Sparc.def"
1315ca98fd9SDimitry Andric default:
1325ca98fd9SDimitry Andric break;
1335ca98fd9SDimitry Andric }
1345ca98fd9SDimitry Andric break;
13501095a5dSDimitry Andric case ELF::EM_AMDGPU:
13601095a5dSDimitry Andric switch (Type) {
1377ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/AMDGPU.def"
13801095a5dSDimitry Andric default:
13901095a5dSDimitry Andric break;
14001095a5dSDimitry Andric }
141c7dac04cSDimitry Andric break;
14201095a5dSDimitry Andric case ELF::EM_BPF:
14301095a5dSDimitry Andric switch (Type) {
1447ab83427SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/BPF.def"
14501095a5dSDimitry Andric default:
14601095a5dSDimitry Andric break;
14701095a5dSDimitry Andric }
14801095a5dSDimitry Andric break;
149d8e91e46SDimitry Andric case ELF::EM_MSP430:
150d8e91e46SDimitry Andric switch (Type) {
151d8e91e46SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/MSP430.def"
152d8e91e46SDimitry Andric default:
153d8e91e46SDimitry Andric break;
154d8e91e46SDimitry Andric }
155d8e91e46SDimitry Andric break;
156cfca06d7SDimitry Andric case ELF::EM_VE:
157cfca06d7SDimitry Andric switch (Type) {
158cfca06d7SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/VE.def"
159cfca06d7SDimitry Andric default:
160cfca06d7SDimitry Andric break;
161cfca06d7SDimitry Andric }
162cfca06d7SDimitry Andric break;
163b60736ecSDimitry Andric case ELF::EM_CSKY:
164b60736ecSDimitry Andric switch (Type) {
165b60736ecSDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/CSKY.def"
166b60736ecSDimitry Andric default:
167b60736ecSDimitry Andric break;
168b60736ecSDimitry Andric }
169b60736ecSDimitry Andric break;
170145449b1SDimitry Andric case ELF::EM_LOONGARCH:
171145449b1SDimitry Andric switch (Type) {
172145449b1SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/LoongArch.def"
173145449b1SDimitry Andric default:
174145449b1SDimitry Andric break;
175145449b1SDimitry Andric }
176145449b1SDimitry Andric break;
177e3b55780SDimitry Andric case ELF::EM_XTENSA:
178e3b55780SDimitry Andric switch (Type) {
179e3b55780SDimitry Andric #include "llvm/BinaryFormat/ELFRelocs/Xtensa.def"
180e3b55780SDimitry Andric default:
181e3b55780SDimitry Andric break;
182e3b55780SDimitry Andric }
183e3b55780SDimitry Andric break;
184f8af5cf6SDimitry Andric default:
185f8af5cf6SDimitry Andric break;
186f8af5cf6SDimitry Andric }
187f8af5cf6SDimitry Andric return "Unknown";
188f8af5cf6SDimitry Andric }
189f8af5cf6SDimitry Andric
19067c32a98SDimitry Andric #undef ELF_RELOC
191a303c417SDimitry Andric
getELFRelativeRelocationType(uint32_t Machine)192d8e91e46SDimitry Andric uint32_t llvm::object::getELFRelativeRelocationType(uint32_t Machine) {
193eb11fae6SDimitry Andric switch (Machine) {
194eb11fae6SDimitry Andric case ELF::EM_X86_64:
195eb11fae6SDimitry Andric return ELF::R_X86_64_RELATIVE;
196eb11fae6SDimitry Andric case ELF::EM_386:
197eb11fae6SDimitry Andric case ELF::EM_IAMCU:
198eb11fae6SDimitry Andric return ELF::R_386_RELATIVE;
199eb11fae6SDimitry Andric case ELF::EM_MIPS:
200eb11fae6SDimitry Andric break;
201eb11fae6SDimitry Andric case ELF::EM_AARCH64:
202eb11fae6SDimitry Andric return ELF::R_AARCH64_RELATIVE;
203eb11fae6SDimitry Andric case ELF::EM_ARM:
204eb11fae6SDimitry Andric return ELF::R_ARM_RELATIVE;
205eb11fae6SDimitry Andric case ELF::EM_ARC_COMPACT:
206eb11fae6SDimitry Andric case ELF::EM_ARC_COMPACT2:
207eb11fae6SDimitry Andric return ELF::R_ARC_RELATIVE;
208eb11fae6SDimitry Andric case ELF::EM_AVR:
209eb11fae6SDimitry Andric break;
210eb11fae6SDimitry Andric case ELF::EM_HEXAGON:
211eb11fae6SDimitry Andric return ELF::R_HEX_RELATIVE;
212eb11fae6SDimitry Andric case ELF::EM_LANAI:
213eb11fae6SDimitry Andric break;
214eb11fae6SDimitry Andric case ELF::EM_PPC:
215eb11fae6SDimitry Andric break;
216eb11fae6SDimitry Andric case ELF::EM_PPC64:
217eb11fae6SDimitry Andric return ELF::R_PPC64_RELATIVE;
218eb11fae6SDimitry Andric case ELF::EM_RISCV:
219eb11fae6SDimitry Andric return ELF::R_RISCV_RELATIVE;
220eb11fae6SDimitry Andric case ELF::EM_S390:
221eb11fae6SDimitry Andric return ELF::R_390_RELATIVE;
222eb11fae6SDimitry Andric case ELF::EM_SPARC:
223eb11fae6SDimitry Andric case ELF::EM_SPARC32PLUS:
224eb11fae6SDimitry Andric case ELF::EM_SPARCV9:
225eb11fae6SDimitry Andric return ELF::R_SPARC_RELATIVE;
226b60736ecSDimitry Andric case ELF::EM_CSKY:
227b60736ecSDimitry Andric return ELF::R_CKCORE_RELATIVE;
22877fc4c14SDimitry Andric case ELF::EM_VE:
22977fc4c14SDimitry Andric return ELF::R_VE_RELATIVE;
230eb11fae6SDimitry Andric case ELF::EM_AMDGPU:
231eb11fae6SDimitry Andric break;
232eb11fae6SDimitry Andric case ELF::EM_BPF:
233eb11fae6SDimitry Andric break;
234e3b55780SDimitry Andric case ELF::EM_LOONGARCH:
235e3b55780SDimitry Andric return ELF::R_LARCH_RELATIVE;
236eb11fae6SDimitry Andric default:
237eb11fae6SDimitry Andric break;
238eb11fae6SDimitry Andric }
239eb11fae6SDimitry Andric return 0;
240eb11fae6SDimitry Andric }
241eb11fae6SDimitry Andric
getELFSectionTypeName(uint32_t Machine,unsigned Type)242a303c417SDimitry Andric StringRef llvm::object::getELFSectionTypeName(uint32_t Machine, unsigned Type) {
243a303c417SDimitry Andric switch (Machine) {
244a303c417SDimitry Andric case ELF::EM_ARM:
245a303c417SDimitry Andric switch (Type) {
246a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_EXIDX);
247a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_PREEMPTMAP);
248a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_ATTRIBUTES);
249a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_DEBUGOVERLAY);
250a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ARM_OVERLAYSECTION);
251a303c417SDimitry Andric }
252a303c417SDimitry Andric break;
253a303c417SDimitry Andric case ELF::EM_HEXAGON:
254ac9a064cSDimitry Andric switch (Type) {
255ac9a064cSDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_HEX_ORDERED);
256ac9a064cSDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_HEXAGON_ATTRIBUTES);
257ac9a064cSDimitry Andric }
258a303c417SDimitry Andric break;
259a303c417SDimitry Andric case ELF::EM_X86_64:
260a303c417SDimitry Andric switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_X86_64_UNWIND); }
261a303c417SDimitry Andric break;
262a303c417SDimitry Andric case ELF::EM_MIPS:
263a303c417SDimitry Andric case ELF::EM_MIPS_RS3_LE:
264a303c417SDimitry Andric switch (Type) {
265a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_REGINFO);
266a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_OPTIONS);
267a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_DWARF);
268e6d15924SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_MIPS_ABIFLAGS);
269a303c417SDimitry Andric }
270a303c417SDimitry Andric break;
271c0981da4SDimitry Andric case ELF::EM_MSP430:
272c0981da4SDimitry Andric switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_MSP430_ATTRIBUTES); }
273c0981da4SDimitry Andric break;
274cfca06d7SDimitry Andric case ELF::EM_RISCV:
275cfca06d7SDimitry Andric switch (Type) { STRINGIFY_ENUM_CASE(ELF, SHT_RISCV_ATTRIBUTES); }
276cfca06d7SDimitry Andric break;
2777fa27ce4SDimitry Andric case ELF::EM_AARCH64:
2787fa27ce4SDimitry Andric switch (Type) {
279b1c73532SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_AUTH_RELR);
2807fa27ce4SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_DYNAMIC);
2817fa27ce4SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_AARCH64_MEMTAG_GLOBALS_STATIC);
2827fa27ce4SDimitry Andric }
283a303c417SDimitry Andric default:
284a303c417SDimitry Andric break;
285a303c417SDimitry Andric }
286a303c417SDimitry Andric
287a303c417SDimitry Andric switch (Type) {
288a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_NULL);
289a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_PROGBITS);
290a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB);
291a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_STRTAB);
292a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_RELA);
293a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_HASH);
294a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_DYNAMIC);
295a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_NOTE);
296a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_NOBITS);
297a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_REL);
298a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_SHLIB);
299a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_DYNSYM);
300a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_INIT_ARRAY);
301a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_FINI_ARRAY);
302a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_PREINIT_ARRAY);
303a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GROUP);
304a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_SYMTAB_SHNDX);
305eb11fae6SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_RELR);
306ac9a064cSDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_CREL);
307044eb2f6SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_REL);
308044eb2f6SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELA);
309eb11fae6SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_ANDROID_RELR);
3107c7aba6eSDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ODRTAB);
311eb11fae6SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LINKER_OPTIONS);
312eb11fae6SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_CALL_GRAPH_PROFILE);
313eb11fae6SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_ADDRSIG);
314e6d15924SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_DEPENDENT_LIBRARIES);
315e6d15924SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_SYMPART);
3161d5ae102SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_EHDR);
3171d5ae102SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_PART_PHDR);
318145449b1SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP_V0);
319b60736ecSDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_BB_ADDR_MAP);
3201f917f69SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_OFFLOADING);
3217fa27ce4SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_LLVM_LTO);
322a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_ATTRIBUTES);
323a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_HASH);
324a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verdef);
325a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_verneed);
326a303c417SDimitry Andric STRINGIFY_ENUM_CASE(ELF, SHT_GNU_versym);
327a303c417SDimitry Andric default:
328a303c417SDimitry Andric return "Unknown";
329a303c417SDimitry Andric }
330a303c417SDimitry Andric }
331044eb2f6SDimitry Andric
332044eb2f6SDimitry Andric template <class ELFT>
333b60736ecSDimitry Andric std::vector<typename ELFT::Rel>
decode_relrs(Elf_Relr_Range relrs) const334eb11fae6SDimitry Andric ELFFile<ELFT>::decode_relrs(Elf_Relr_Range relrs) const {
335eb11fae6SDimitry Andric // This function decodes the contents of an SHT_RELR packed relocation
336eb11fae6SDimitry Andric // section.
337eb11fae6SDimitry Andric //
338eb11fae6SDimitry Andric // Proposal for adding SHT_RELR sections to generic-abi is here:
339eb11fae6SDimitry Andric // https://groups.google.com/forum/#!topic/generic-abi/bX460iggiKg
340eb11fae6SDimitry Andric //
341eb11fae6SDimitry Andric // The encoded sequence of Elf64_Relr entries in a SHT_RELR section looks
342eb11fae6SDimitry Andric // like [ AAAAAAAA BBBBBBB1 BBBBBBB1 ... AAAAAAAA BBBBBB1 ... ]
343eb11fae6SDimitry Andric //
344eb11fae6SDimitry Andric // i.e. start with an address, followed by any number of bitmaps. The address
345eb11fae6SDimitry Andric // entry encodes 1 relocation. The subsequent bitmap entries encode up to 63
346eb11fae6SDimitry Andric // relocations each, at subsequent offsets following the last address entry.
347eb11fae6SDimitry Andric //
348eb11fae6SDimitry Andric // The bitmap entries must have 1 in the least significant bit. The assumption
349eb11fae6SDimitry Andric // here is that an address cannot have 1 in lsb. Odd addresses are not
350eb11fae6SDimitry Andric // supported.
351eb11fae6SDimitry Andric //
352eb11fae6SDimitry Andric // Excluding the least significant bit in the bitmap, each non-zero bit in
353eb11fae6SDimitry Andric // the bitmap represents a relocation to be applied to a corresponding machine
354eb11fae6SDimitry Andric // word that follows the base address word. The second least significant bit
355eb11fae6SDimitry Andric // represents the machine word immediately following the initial address, and
356eb11fae6SDimitry Andric // each bit that follows represents the next word, in linear order. As such,
357eb11fae6SDimitry Andric // a single bitmap can encode up to 31 relocations in a 32-bit object, and
358eb11fae6SDimitry Andric // 63 relocations in a 64-bit object.
359eb11fae6SDimitry Andric //
360eb11fae6SDimitry Andric // This encoding has a couple of interesting properties:
361eb11fae6SDimitry Andric // 1. Looking at any entry, it is clear whether it's an address or a bitmap:
362eb11fae6SDimitry Andric // even means address, odd means bitmap.
363eb11fae6SDimitry Andric // 2. Just a simple list of addresses is a valid encoding.
364eb11fae6SDimitry Andric
365b60736ecSDimitry Andric Elf_Rel Rel;
366b60736ecSDimitry Andric Rel.r_info = 0;
367b60736ecSDimitry Andric Rel.setType(getRelativeRelocationType(), false);
368b60736ecSDimitry Andric std::vector<Elf_Rel> Relocs;
369eb11fae6SDimitry Andric
370eb11fae6SDimitry Andric // Word type: uint32_t for Elf32, and uint64_t for Elf64.
371c0981da4SDimitry Andric using Addr = typename ELFT::uint;
372eb11fae6SDimitry Andric
373c0981da4SDimitry Andric Addr Base = 0;
374c0981da4SDimitry Andric for (Elf_Relr R : relrs) {
375c0981da4SDimitry Andric typename ELFT::uint Entry = R;
376eb11fae6SDimitry Andric if ((Entry & 1) == 0) {
377eb11fae6SDimitry Andric // Even entry: encodes the offset for next relocation.
378b60736ecSDimitry Andric Rel.r_offset = Entry;
379b60736ecSDimitry Andric Relocs.push_back(Rel);
380eb11fae6SDimitry Andric // Set base offset for subsequent bitmap entries.
381c0981da4SDimitry Andric Base = Entry + sizeof(Addr);
382c0981da4SDimitry Andric } else {
383eb11fae6SDimitry Andric // Odd entry: encodes bitmap for relocations starting at base.
384c0981da4SDimitry Andric for (Addr Offset = Base; (Entry >>= 1) != 0; Offset += sizeof(Addr))
385eb11fae6SDimitry Andric if ((Entry & 1) != 0) {
386b60736ecSDimitry Andric Rel.r_offset = Offset;
387b60736ecSDimitry Andric Relocs.push_back(Rel);
388eb11fae6SDimitry Andric }
389c0981da4SDimitry Andric Base += (CHAR_BIT * sizeof(Entry) - 1) * sizeof(Addr);
390eb11fae6SDimitry Andric }
391eb11fae6SDimitry Andric }
392eb11fae6SDimitry Andric
393eb11fae6SDimitry Andric return Relocs;
394eb11fae6SDimitry Andric }
395eb11fae6SDimitry Andric
396eb11fae6SDimitry Andric template <class ELFT>
397ac9a064cSDimitry Andric Expected<uint64_t>
getCrelHeader(ArrayRef<uint8_t> Content) const398ac9a064cSDimitry Andric ELFFile<ELFT>::getCrelHeader(ArrayRef<uint8_t> Content) const {
399ac9a064cSDimitry Andric DataExtractor Data(Content, isLE(), sizeof(typename ELFT::Addr));
400ac9a064cSDimitry Andric Error Err = Error::success();
401ac9a064cSDimitry Andric uint64_t Hdr = 0;
402ac9a064cSDimitry Andric Hdr = Data.getULEB128(&Hdr, &Err);
403ac9a064cSDimitry Andric if (Err)
404ac9a064cSDimitry Andric return Err;
405ac9a064cSDimitry Andric return Hdr;
406ac9a064cSDimitry Andric }
407ac9a064cSDimitry Andric
408ac9a064cSDimitry Andric template <class ELFT>
409ac9a064cSDimitry Andric Expected<typename ELFFile<ELFT>::RelsOrRelas>
decodeCrel(ArrayRef<uint8_t> Content) const410ac9a064cSDimitry Andric ELFFile<ELFT>::decodeCrel(ArrayRef<uint8_t> Content) const {
411ac9a064cSDimitry Andric std::vector<Elf_Rel> Rels;
412ac9a064cSDimitry Andric std::vector<Elf_Rela> Relas;
413ac9a064cSDimitry Andric size_t I = 0;
414ac9a064cSDimitry Andric bool HasAddend;
415ac9a064cSDimitry Andric Error Err = object::decodeCrel<ELFT::Is64Bits>(
416ac9a064cSDimitry Andric Content,
417ac9a064cSDimitry Andric [&](uint64_t Count, bool HasA) {
418ac9a064cSDimitry Andric HasAddend = HasA;
419ac9a064cSDimitry Andric if (HasAddend)
420ac9a064cSDimitry Andric Relas.resize(Count);
421ac9a064cSDimitry Andric else
422ac9a064cSDimitry Andric Rels.resize(Count);
423ac9a064cSDimitry Andric },
424ac9a064cSDimitry Andric [&](Elf_Crel Crel) {
425ac9a064cSDimitry Andric if (HasAddend) {
426ac9a064cSDimitry Andric Relas[I].r_offset = Crel.r_offset;
427ac9a064cSDimitry Andric Relas[I].setSymbolAndType(Crel.r_symidx, Crel.r_type, false);
428ac9a064cSDimitry Andric Relas[I++].r_addend = Crel.r_addend;
429ac9a064cSDimitry Andric } else {
430ac9a064cSDimitry Andric Rels[I].r_offset = Crel.r_offset;
431ac9a064cSDimitry Andric Rels[I++].setSymbolAndType(Crel.r_symidx, Crel.r_type, false);
432ac9a064cSDimitry Andric }
433ac9a064cSDimitry Andric });
434ac9a064cSDimitry Andric if (Err)
435ac9a064cSDimitry Andric return std::move(Err);
436ac9a064cSDimitry Andric return std::make_pair(std::move(Rels), std::move(Relas));
437ac9a064cSDimitry Andric }
438ac9a064cSDimitry Andric
439ac9a064cSDimitry Andric template <class ELFT>
440ac9a064cSDimitry Andric Expected<typename ELFFile<ELFT>::RelsOrRelas>
crels(const Elf_Shdr & Sec) const441ac9a064cSDimitry Andric ELFFile<ELFT>::crels(const Elf_Shdr &Sec) const {
442ac9a064cSDimitry Andric Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
443ac9a064cSDimitry Andric if (!ContentsOrErr)
444ac9a064cSDimitry Andric return ContentsOrErr.takeError();
445ac9a064cSDimitry Andric return decodeCrel(*ContentsOrErr);
446ac9a064cSDimitry Andric }
447ac9a064cSDimitry Andric
448ac9a064cSDimitry Andric template <class ELFT>
449eb11fae6SDimitry Andric Expected<std::vector<typename ELFT::Rela>>
android_relas(const Elf_Shdr & Sec) const450b60736ecSDimitry Andric ELFFile<ELFT>::android_relas(const Elf_Shdr &Sec) const {
451044eb2f6SDimitry Andric // This function reads relocations in Android's packed relocation format,
452044eb2f6SDimitry Andric // which is based on SLEB128 and delta encoding.
453044eb2f6SDimitry Andric Expected<ArrayRef<uint8_t>> ContentsOrErr = getSectionContents(Sec);
454044eb2f6SDimitry Andric if (!ContentsOrErr)
455044eb2f6SDimitry Andric return ContentsOrErr.takeError();
456344a3780SDimitry Andric ArrayRef<uint8_t> Content = *ContentsOrErr;
457344a3780SDimitry Andric if (Content.size() < 4 || Content[0] != 'A' || Content[1] != 'P' ||
458344a3780SDimitry Andric Content[2] != 'S' || Content[3] != '2')
459044eb2f6SDimitry Andric return createError("invalid packed relocation header");
460344a3780SDimitry Andric DataExtractor Data(Content, isLE(), ELFT::Is64Bits ? 8 : 4);
461344a3780SDimitry Andric DataExtractor::Cursor Cur(/*Offset=*/4);
462044eb2f6SDimitry Andric
463344a3780SDimitry Andric uint64_t NumRelocs = Data.getSLEB128(Cur);
464344a3780SDimitry Andric uint64_t Offset = Data.getSLEB128(Cur);
465044eb2f6SDimitry Andric uint64_t Addend = 0;
466044eb2f6SDimitry Andric
467344a3780SDimitry Andric if (!Cur)
468344a3780SDimitry Andric return std::move(Cur.takeError());
469044eb2f6SDimitry Andric
470044eb2f6SDimitry Andric std::vector<Elf_Rela> Relocs;
471044eb2f6SDimitry Andric Relocs.reserve(NumRelocs);
472044eb2f6SDimitry Andric while (NumRelocs) {
473344a3780SDimitry Andric uint64_t NumRelocsInGroup = Data.getSLEB128(Cur);
474344a3780SDimitry Andric if (!Cur)
475344a3780SDimitry Andric return std::move(Cur.takeError());
476044eb2f6SDimitry Andric if (NumRelocsInGroup > NumRelocs)
477044eb2f6SDimitry Andric return createError("relocation group unexpectedly large");
478044eb2f6SDimitry Andric NumRelocs -= NumRelocsInGroup;
479044eb2f6SDimitry Andric
480344a3780SDimitry Andric uint64_t GroupFlags = Data.getSLEB128(Cur);
481044eb2f6SDimitry Andric bool GroupedByInfo = GroupFlags & ELF::RELOCATION_GROUPED_BY_INFO_FLAG;
482044eb2f6SDimitry Andric bool GroupedByOffsetDelta = GroupFlags & ELF::RELOCATION_GROUPED_BY_OFFSET_DELTA_FLAG;
483044eb2f6SDimitry Andric bool GroupedByAddend = GroupFlags & ELF::RELOCATION_GROUPED_BY_ADDEND_FLAG;
484044eb2f6SDimitry Andric bool GroupHasAddend = GroupFlags & ELF::RELOCATION_GROUP_HAS_ADDEND_FLAG;
485044eb2f6SDimitry Andric
486044eb2f6SDimitry Andric uint64_t GroupOffsetDelta;
487044eb2f6SDimitry Andric if (GroupedByOffsetDelta)
488344a3780SDimitry Andric GroupOffsetDelta = Data.getSLEB128(Cur);
489044eb2f6SDimitry Andric
490044eb2f6SDimitry Andric uint64_t GroupRInfo;
491044eb2f6SDimitry Andric if (GroupedByInfo)
492344a3780SDimitry Andric GroupRInfo = Data.getSLEB128(Cur);
493044eb2f6SDimitry Andric
494044eb2f6SDimitry Andric if (GroupedByAddend && GroupHasAddend)
495344a3780SDimitry Andric Addend += Data.getSLEB128(Cur);
496044eb2f6SDimitry Andric
497d8e91e46SDimitry Andric if (!GroupHasAddend)
498d8e91e46SDimitry Andric Addend = 0;
499d8e91e46SDimitry Andric
500344a3780SDimitry Andric for (uint64_t I = 0; Cur && I != NumRelocsInGroup; ++I) {
501044eb2f6SDimitry Andric Elf_Rela R;
502344a3780SDimitry Andric Offset += GroupedByOffsetDelta ? GroupOffsetDelta : Data.getSLEB128(Cur);
503044eb2f6SDimitry Andric R.r_offset = Offset;
504344a3780SDimitry Andric R.r_info = GroupedByInfo ? GroupRInfo : Data.getSLEB128(Cur);
505d8e91e46SDimitry Andric if (GroupHasAddend && !GroupedByAddend)
506344a3780SDimitry Andric Addend += Data.getSLEB128(Cur);
507044eb2f6SDimitry Andric R.r_addend = Addend;
508044eb2f6SDimitry Andric Relocs.push_back(R);
509044eb2f6SDimitry Andric }
510344a3780SDimitry Andric if (!Cur)
511344a3780SDimitry Andric return std::move(Cur.takeError());
512044eb2f6SDimitry Andric }
513044eb2f6SDimitry Andric
514044eb2f6SDimitry Andric return Relocs;
515044eb2f6SDimitry Andric }
516044eb2f6SDimitry Andric
517eb11fae6SDimitry Andric template <class ELFT>
getDynamicTagAsString(unsigned Arch,uint64_t Type) const518e6d15924SDimitry Andric std::string ELFFile<ELFT>::getDynamicTagAsString(unsigned Arch,
519eb11fae6SDimitry Andric uint64_t Type) const {
520eb11fae6SDimitry Andric #define DYNAMIC_STRINGIFY_ENUM(tag, value) \
521eb11fae6SDimitry Andric case value: \
522eb11fae6SDimitry Andric return #tag;
523eb11fae6SDimitry Andric
524eb11fae6SDimitry Andric #define DYNAMIC_TAG(n, v)
525eb11fae6SDimitry Andric switch (Arch) {
526e6d15924SDimitry Andric case ELF::EM_AARCH64:
527e6d15924SDimitry Andric switch (Type) {
528e6d15924SDimitry Andric #define AARCH64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
529e6d15924SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def"
530e6d15924SDimitry Andric #undef AARCH64_DYNAMIC_TAG
531e6d15924SDimitry Andric }
532e6d15924SDimitry Andric break;
533e6d15924SDimitry Andric
534eb11fae6SDimitry Andric case ELF::EM_HEXAGON:
535eb11fae6SDimitry Andric switch (Type) {
536eb11fae6SDimitry Andric #define HEXAGON_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
537eb11fae6SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def"
538eb11fae6SDimitry Andric #undef HEXAGON_DYNAMIC_TAG
539eb11fae6SDimitry Andric }
540e6d15924SDimitry Andric break;
541eb11fae6SDimitry Andric
542eb11fae6SDimitry Andric case ELF::EM_MIPS:
543eb11fae6SDimitry Andric switch (Type) {
544eb11fae6SDimitry Andric #define MIPS_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
545eb11fae6SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def"
546eb11fae6SDimitry Andric #undef MIPS_DYNAMIC_TAG
547eb11fae6SDimitry Andric }
548e6d15924SDimitry Andric break;
549eb11fae6SDimitry Andric
550c0981da4SDimitry Andric case ELF::EM_PPC:
551c0981da4SDimitry Andric switch (Type) {
552c0981da4SDimitry Andric #define PPC_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
553c0981da4SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def"
554c0981da4SDimitry Andric #undef PPC_DYNAMIC_TAG
555c0981da4SDimitry Andric }
556c0981da4SDimitry Andric break;
557c0981da4SDimitry Andric
558eb11fae6SDimitry Andric case ELF::EM_PPC64:
559eb11fae6SDimitry Andric switch (Type) {
560eb11fae6SDimitry Andric #define PPC64_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
561eb11fae6SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def"
562eb11fae6SDimitry Andric #undef PPC64_DYNAMIC_TAG
563eb11fae6SDimitry Andric }
564e6d15924SDimitry Andric break;
565c0981da4SDimitry Andric
566c0981da4SDimitry Andric case ELF::EM_RISCV:
567c0981da4SDimitry Andric switch (Type) {
568c0981da4SDimitry Andric #define RISCV_DYNAMIC_TAG(name, value) DYNAMIC_STRINGIFY_ENUM(name, value)
569c0981da4SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def"
570c0981da4SDimitry Andric #undef RISCV_DYNAMIC_TAG
571c0981da4SDimitry Andric }
572c0981da4SDimitry Andric break;
573eb11fae6SDimitry Andric }
574eb11fae6SDimitry Andric #undef DYNAMIC_TAG
575eb11fae6SDimitry Andric switch (Type) {
576eb11fae6SDimitry Andric // Now handle all dynamic tags except the architecture specific ones
577e6d15924SDimitry Andric #define AARCH64_DYNAMIC_TAG(name, value)
578eb11fae6SDimitry Andric #define MIPS_DYNAMIC_TAG(name, value)
579eb11fae6SDimitry Andric #define HEXAGON_DYNAMIC_TAG(name, value)
580c0981da4SDimitry Andric #define PPC_DYNAMIC_TAG(name, value)
581eb11fae6SDimitry Andric #define PPC64_DYNAMIC_TAG(name, value)
582c0981da4SDimitry Andric #define RISCV_DYNAMIC_TAG(name, value)
583eb11fae6SDimitry Andric // Also ignore marker tags such as DT_HIOS (maps to DT_VERNEEDNUM), etc.
584eb11fae6SDimitry Andric #define DYNAMIC_TAG_MARKER(name, value)
585706b4fc4SDimitry Andric #define DYNAMIC_TAG(name, value) case value: return #name;
586eb11fae6SDimitry Andric #include "llvm/BinaryFormat/DynamicTags.def"
587eb11fae6SDimitry Andric #undef DYNAMIC_TAG
588e6d15924SDimitry Andric #undef AARCH64_DYNAMIC_TAG
589eb11fae6SDimitry Andric #undef MIPS_DYNAMIC_TAG
590eb11fae6SDimitry Andric #undef HEXAGON_DYNAMIC_TAG
591c0981da4SDimitry Andric #undef PPC_DYNAMIC_TAG
592eb11fae6SDimitry Andric #undef PPC64_DYNAMIC_TAG
593c0981da4SDimitry Andric #undef RISCV_DYNAMIC_TAG
594eb11fae6SDimitry Andric #undef DYNAMIC_TAG_MARKER
595eb11fae6SDimitry Andric #undef DYNAMIC_STRINGIFY_ENUM
596eb11fae6SDimitry Andric default:
597e6d15924SDimitry Andric return "<unknown:>0x" + utohexstr(Type, true);
598eb11fae6SDimitry Andric }
599eb11fae6SDimitry Andric }
600eb11fae6SDimitry Andric
601eb11fae6SDimitry Andric template <class ELFT>
getDynamicTagAsString(uint64_t Type) const602e6d15924SDimitry Andric std::string ELFFile<ELFT>::getDynamicTagAsString(uint64_t Type) const {
603b60736ecSDimitry Andric return getDynamicTagAsString(getHeader().e_machine, Type);
604eb11fae6SDimitry Andric }
605eb11fae6SDimitry Andric
606eb11fae6SDimitry Andric template <class ELFT>
dynamicEntries() const607eb11fae6SDimitry Andric Expected<typename ELFT::DynRange> ELFFile<ELFT>::dynamicEntries() const {
608eb11fae6SDimitry Andric ArrayRef<Elf_Dyn> Dyn;
609eb11fae6SDimitry Andric
610eb11fae6SDimitry Andric auto ProgramHeadersOrError = program_headers();
611eb11fae6SDimitry Andric if (!ProgramHeadersOrError)
612eb11fae6SDimitry Andric return ProgramHeadersOrError.takeError();
613eb11fae6SDimitry Andric
614eb11fae6SDimitry Andric for (const Elf_Phdr &Phdr : *ProgramHeadersOrError) {
615eb11fae6SDimitry Andric if (Phdr.p_type == ELF::PT_DYNAMIC) {
616ac9a064cSDimitry Andric const uint8_t *DynOffset = base() + Phdr.p_offset;
617ac9a064cSDimitry Andric if (DynOffset > end())
618ac9a064cSDimitry Andric return createError(
619ac9a064cSDimitry Andric "dynamic section offset past file size: corrupted ELF");
620ac9a064cSDimitry Andric Dyn = ArrayRef(reinterpret_cast<const Elf_Dyn *>(DynOffset),
621eb11fae6SDimitry Andric Phdr.p_filesz / sizeof(Elf_Dyn));
622eb11fae6SDimitry Andric break;
623eb11fae6SDimitry Andric }
624eb11fae6SDimitry Andric }
625eb11fae6SDimitry Andric
626eb11fae6SDimitry Andric // If we can't find the dynamic section in the program headers, we just fall
627eb11fae6SDimitry Andric // back on the sections.
628eb11fae6SDimitry Andric if (Dyn.empty()) {
629eb11fae6SDimitry Andric auto SectionsOrError = sections();
630eb11fae6SDimitry Andric if (!SectionsOrError)
631eb11fae6SDimitry Andric return SectionsOrError.takeError();
632eb11fae6SDimitry Andric
633eb11fae6SDimitry Andric for (const Elf_Shdr &Sec : *SectionsOrError) {
634eb11fae6SDimitry Andric if (Sec.sh_type == ELF::SHT_DYNAMIC) {
635eb11fae6SDimitry Andric Expected<ArrayRef<Elf_Dyn>> DynOrError =
636b60736ecSDimitry Andric getSectionContentsAsArray<Elf_Dyn>(Sec);
637eb11fae6SDimitry Andric if (!DynOrError)
638eb11fae6SDimitry Andric return DynOrError.takeError();
639eb11fae6SDimitry Andric Dyn = *DynOrError;
640eb11fae6SDimitry Andric break;
641eb11fae6SDimitry Andric }
642eb11fae6SDimitry Andric }
643eb11fae6SDimitry Andric
644eb11fae6SDimitry Andric if (!Dyn.data())
645eb11fae6SDimitry Andric return ArrayRef<Elf_Dyn>();
646eb11fae6SDimitry Andric }
647eb11fae6SDimitry Andric
648eb11fae6SDimitry Andric if (Dyn.empty())
649eb11fae6SDimitry Andric return createError("invalid empty dynamic section");
650eb11fae6SDimitry Andric
651eb11fae6SDimitry Andric if (Dyn.back().d_tag != ELF::DT_NULL)
652eb11fae6SDimitry Andric return createError("dynamic sections must be DT_NULL terminated");
653eb11fae6SDimitry Andric
654eb11fae6SDimitry Andric return Dyn;
655eb11fae6SDimitry Andric }
656eb11fae6SDimitry Andric
657eb11fae6SDimitry Andric template <class ELFT>
658b60736ecSDimitry Andric Expected<const uint8_t *>
toMappedAddr(uint64_t VAddr,WarningHandler WarnHandler) const659b60736ecSDimitry Andric ELFFile<ELFT>::toMappedAddr(uint64_t VAddr, WarningHandler WarnHandler) const {
660eb11fae6SDimitry Andric auto ProgramHeadersOrError = program_headers();
661eb11fae6SDimitry Andric if (!ProgramHeadersOrError)
662eb11fae6SDimitry Andric return ProgramHeadersOrError.takeError();
663eb11fae6SDimitry Andric
664eb11fae6SDimitry Andric llvm::SmallVector<Elf_Phdr *, 4> LoadSegments;
665eb11fae6SDimitry Andric
666eb11fae6SDimitry Andric for (const Elf_Phdr &Phdr : *ProgramHeadersOrError)
667eb11fae6SDimitry Andric if (Phdr.p_type == ELF::PT_LOAD)
668eb11fae6SDimitry Andric LoadSegments.push_back(const_cast<Elf_Phdr *>(&Phdr));
669eb11fae6SDimitry Andric
670b60736ecSDimitry Andric auto SortPred = [](const Elf_Phdr_Impl<ELFT> *A,
671b60736ecSDimitry Andric const Elf_Phdr_Impl<ELFT> *B) {
672b60736ecSDimitry Andric return A->p_vaddr < B->p_vaddr;
673b60736ecSDimitry Andric };
674b60736ecSDimitry Andric if (!llvm::is_sorted(LoadSegments, SortPred)) {
675b60736ecSDimitry Andric if (Error E =
676b60736ecSDimitry Andric WarnHandler("loadable segments are unsorted by virtual address"))
677b60736ecSDimitry Andric return std::move(E);
678b60736ecSDimitry Andric llvm::stable_sort(LoadSegments, SortPred);
679b60736ecSDimitry Andric }
680b60736ecSDimitry Andric
681b60736ecSDimitry Andric const Elf_Phdr *const *I = llvm::upper_bound(
682b60736ecSDimitry Andric LoadSegments, VAddr, [](uint64_t VAddr, const Elf_Phdr_Impl<ELFT> *Phdr) {
683eb11fae6SDimitry Andric return VAddr < Phdr->p_vaddr;
684eb11fae6SDimitry Andric });
685eb11fae6SDimitry Andric
686eb11fae6SDimitry Andric if (I == LoadSegments.begin())
687e6d15924SDimitry Andric return createError("virtual address is not in any segment: 0x" +
688e6d15924SDimitry Andric Twine::utohexstr(VAddr));
689eb11fae6SDimitry Andric --I;
690eb11fae6SDimitry Andric const Elf_Phdr &Phdr = **I;
691eb11fae6SDimitry Andric uint64_t Delta = VAddr - Phdr.p_vaddr;
692eb11fae6SDimitry Andric if (Delta >= Phdr.p_filesz)
693e6d15924SDimitry Andric return createError("virtual address is not in any segment: 0x" +
694e6d15924SDimitry Andric Twine::utohexstr(VAddr));
695cfca06d7SDimitry Andric
696cfca06d7SDimitry Andric uint64_t Offset = Phdr.p_offset + Delta;
697cfca06d7SDimitry Andric if (Offset >= getBufSize())
698cfca06d7SDimitry Andric return createError("can't map virtual address 0x" +
699cfca06d7SDimitry Andric Twine::utohexstr(VAddr) + " to the segment with index " +
700cfca06d7SDimitry Andric Twine(&Phdr - (*ProgramHeadersOrError).data() + 1) +
701cfca06d7SDimitry Andric ": the segment ends at 0x" +
702cfca06d7SDimitry Andric Twine::utohexstr(Phdr.p_offset + Phdr.p_filesz) +
703cfca06d7SDimitry Andric ", which is greater than the file size (0x" +
704cfca06d7SDimitry Andric Twine::utohexstr(getBufSize()) + ")");
705cfca06d7SDimitry Andric
706cfca06d7SDimitry Andric return base() + Offset;
707eb11fae6SDimitry Andric }
708eb11fae6SDimitry Andric
709312c0ed1SDimitry Andric // Helper to extract and decode the next ULEB128 value as unsigned int.
710312c0ed1SDimitry Andric // Returns zero and sets ULEBSizeErr if the ULEB128 value exceeds the unsigned
711312c0ed1SDimitry Andric // int limit.
712312c0ed1SDimitry Andric // Also returns zero if ULEBSizeErr is already in an error state.
713312c0ed1SDimitry Andric // ULEBSizeErr is an out variable if an error occurs.
714312c0ed1SDimitry Andric template <typename IntTy, std::enable_if_t<std::is_unsigned_v<IntTy>, int> = 0>
readULEB128As(DataExtractor & Data,DataExtractor::Cursor & Cur,Error & ULEBSizeErr)715312c0ed1SDimitry Andric static IntTy readULEB128As(DataExtractor &Data, DataExtractor::Cursor &Cur,
716312c0ed1SDimitry Andric Error &ULEBSizeErr) {
717312c0ed1SDimitry Andric // Bail out and do not extract data if ULEBSizeErr is already set.
718312c0ed1SDimitry Andric if (ULEBSizeErr)
719312c0ed1SDimitry Andric return 0;
720312c0ed1SDimitry Andric uint64_t Offset = Cur.tell();
721312c0ed1SDimitry Andric uint64_t Value = Data.getULEB128(Cur);
722312c0ed1SDimitry Andric if (Value > std::numeric_limits<IntTy>::max()) {
723312c0ed1SDimitry Andric ULEBSizeErr = createError("ULEB128 value at offset 0x" +
724312c0ed1SDimitry Andric Twine::utohexstr(Offset) + " exceeds UINT" +
725312c0ed1SDimitry Andric Twine(std::numeric_limits<IntTy>::digits) +
726312c0ed1SDimitry Andric "_MAX (0x" + Twine::utohexstr(Value) + ")");
727312c0ed1SDimitry Andric return 0;
728312c0ed1SDimitry Andric }
729312c0ed1SDimitry Andric return static_cast<IntTy>(Value);
730312c0ed1SDimitry Andric }
731312c0ed1SDimitry Andric
732312c0ed1SDimitry Andric template <typename ELFT>
733312c0ed1SDimitry Andric static Expected<std::vector<BBAddrMap>>
decodeBBAddrMapImpl(const ELFFile<ELFT> & EF,const typename ELFFile<ELFT>::Elf_Shdr & Sec,const typename ELFFile<ELFT>::Elf_Shdr * RelaSec,std::vector<PGOAnalysisMap> * PGOAnalyses)734312c0ed1SDimitry Andric decodeBBAddrMapImpl(const ELFFile<ELFT> &EF,
735312c0ed1SDimitry Andric const typename ELFFile<ELFT>::Elf_Shdr &Sec,
736312c0ed1SDimitry Andric const typename ELFFile<ELFT>::Elf_Shdr *RelaSec,
737312c0ed1SDimitry Andric std::vector<PGOAnalysisMap> *PGOAnalyses) {
738312c0ed1SDimitry Andric bool IsRelocatable = EF.getHeader().e_type == ELF::ET_REL;
7397fa27ce4SDimitry Andric
7407fa27ce4SDimitry Andric // This DenseMap maps the offset of each function (the location of the
7417fa27ce4SDimitry Andric // reference to the function in the SHT_LLVM_BB_ADDR_MAP section) to the
7427fa27ce4SDimitry Andric // addend (the location of the function in the text section).
7437fa27ce4SDimitry Andric llvm::DenseMap<uint64_t, uint64_t> FunctionOffsetTranslations;
7447fa27ce4SDimitry Andric if (IsRelocatable && RelaSec) {
7457fa27ce4SDimitry Andric assert(RelaSec &&
7467fa27ce4SDimitry Andric "Can't read a SHT_LLVM_BB_ADDR_MAP section in a relocatable "
7477fa27ce4SDimitry Andric "object file without providing a relocation section.");
748312c0ed1SDimitry Andric Expected<typename ELFFile<ELFT>::Elf_Rela_Range> Relas = EF.relas(*RelaSec);
7497fa27ce4SDimitry Andric if (!Relas)
7507fa27ce4SDimitry Andric return createError("unable to read relocations for section " +
751312c0ed1SDimitry Andric describe(EF, Sec) + ": " +
7527fa27ce4SDimitry Andric toString(Relas.takeError()));
753312c0ed1SDimitry Andric for (typename ELFFile<ELFT>::Elf_Rela Rela : *Relas)
7547fa27ce4SDimitry Andric FunctionOffsetTranslations[Rela.r_offset] = Rela.r_addend;
7557fa27ce4SDimitry Andric }
756ac9a064cSDimitry Andric auto GetAddressForRelocation =
757ac9a064cSDimitry Andric [&](unsigned RelocationOffsetInSection) -> Expected<unsigned> {
758ac9a064cSDimitry Andric auto FOTIterator =
759ac9a064cSDimitry Andric FunctionOffsetTranslations.find(RelocationOffsetInSection);
760ac9a064cSDimitry Andric if (FOTIterator == FunctionOffsetTranslations.end()) {
761ac9a064cSDimitry Andric return createError("failed to get relocation data for offset: " +
762ac9a064cSDimitry Andric Twine::utohexstr(RelocationOffsetInSection) +
763ac9a064cSDimitry Andric " in section " + describe(EF, Sec));
764ac9a064cSDimitry Andric }
765ac9a064cSDimitry Andric return FOTIterator->second;
766ac9a064cSDimitry Andric };
767312c0ed1SDimitry Andric Expected<ArrayRef<uint8_t>> ContentsOrErr = EF.getSectionContents(Sec);
768344a3780SDimitry Andric if (!ContentsOrErr)
769344a3780SDimitry Andric return ContentsOrErr.takeError();
770344a3780SDimitry Andric ArrayRef<uint8_t> Content = *ContentsOrErr;
771312c0ed1SDimitry Andric DataExtractor Data(Content, EF.isLE(), ELFT::Is64Bits ? 8 : 4);
772c0981da4SDimitry Andric std::vector<BBAddrMap> FunctionEntries;
773344a3780SDimitry Andric
774344a3780SDimitry Andric DataExtractor::Cursor Cur(0);
775344a3780SDimitry Andric Error ULEBSizeErr = Error::success();
7767fa27ce4SDimitry Andric Error MetadataDecodeErr = Error::success();
777344a3780SDimitry Andric
778ac9a064cSDimitry Andric // Helper lampda to extract the (possiblly relocatable) address stored at Cur.
779ac9a064cSDimitry Andric auto ExtractAddress = [&]() -> Expected<typename ELFFile<ELFT>::uintX_t> {
780ac9a064cSDimitry Andric uint64_t RelocationOffsetInSection = Cur.tell();
781ac9a064cSDimitry Andric auto Address =
782ac9a064cSDimitry Andric static_cast<typename ELFFile<ELFT>::uintX_t>(Data.getAddress(Cur));
783ac9a064cSDimitry Andric if (!Cur)
784ac9a064cSDimitry Andric return Cur.takeError();
785ac9a064cSDimitry Andric if (!IsRelocatable)
786ac9a064cSDimitry Andric return Address;
787ac9a064cSDimitry Andric assert(Address == 0);
788ac9a064cSDimitry Andric Expected<unsigned> AddressOrErr =
789ac9a064cSDimitry Andric GetAddressForRelocation(RelocationOffsetInSection);
790ac9a064cSDimitry Andric if (!AddressOrErr)
791ac9a064cSDimitry Andric return AddressOrErr.takeError();
792ac9a064cSDimitry Andric return *AddressOrErr;
793ac9a064cSDimitry Andric };
794ac9a064cSDimitry Andric
795145449b1SDimitry Andric uint8_t Version = 0;
796312c0ed1SDimitry Andric uint8_t Feature = 0;
797ac9a064cSDimitry Andric BBAddrMap::Features FeatEnable{};
7987fa27ce4SDimitry Andric while (!ULEBSizeErr && !MetadataDecodeErr && Cur &&
7997fa27ce4SDimitry Andric Cur.tell() < Content.size()) {
800145449b1SDimitry Andric if (Sec.sh_type == ELF::SHT_LLVM_BB_ADDR_MAP) {
801145449b1SDimitry Andric Version = Data.getU8(Cur);
802145449b1SDimitry Andric if (!Cur)
803145449b1SDimitry Andric break;
804e3b55780SDimitry Andric if (Version > 2)
805145449b1SDimitry Andric return createError("unsupported SHT_LLVM_BB_ADDR_MAP version: " +
806145449b1SDimitry Andric Twine(static_cast<int>(Version)));
807312c0ed1SDimitry Andric Feature = Data.getU8(Cur); // Feature byte
808312c0ed1SDimitry Andric if (!Cur)
809312c0ed1SDimitry Andric break;
810ac9a064cSDimitry Andric auto FeatEnableOrErr = BBAddrMap::Features::decode(Feature);
811312c0ed1SDimitry Andric if (!FeatEnableOrErr)
812312c0ed1SDimitry Andric return FeatEnableOrErr.takeError();
813ac9a064cSDimitry Andric FeatEnable = *FeatEnableOrErr;
814312c0ed1SDimitry Andric if (Feature != 0 && Version < 2 && Cur)
815312c0ed1SDimitry Andric return createError(
816312c0ed1SDimitry Andric "version should be >= 2 for SHT_LLVM_BB_ADDR_MAP when "
817312c0ed1SDimitry Andric "PGO features are enabled: version = " +
818312c0ed1SDimitry Andric Twine(static_cast<int>(Version)) +
819312c0ed1SDimitry Andric " feature = " + Twine(static_cast<int>(Feature)));
820145449b1SDimitry Andric }
821ac9a064cSDimitry Andric uint32_t NumBlocksInBBRange = 0;
822ac9a064cSDimitry Andric uint32_t NumBBRanges = 1;
823ac9a064cSDimitry Andric typename ELFFile<ELFT>::uintX_t RangeBaseAddress = 0;
824c0981da4SDimitry Andric std::vector<BBAddrMap::BBEntry> BBEntries;
825ac9a064cSDimitry Andric if (FeatEnable.MultiBBRange) {
826ac9a064cSDimitry Andric NumBBRanges = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
827ac9a064cSDimitry Andric if (!Cur || ULEBSizeErr)
828ac9a064cSDimitry Andric break;
829ac9a064cSDimitry Andric if (!NumBBRanges)
830ac9a064cSDimitry Andric return createError("invalid zero number of BB ranges at offset " +
831ac9a064cSDimitry Andric Twine::utohexstr(Cur.tell()) + " in " +
832ac9a064cSDimitry Andric describe(EF, Sec));
833ac9a064cSDimitry Andric } else {
834ac9a064cSDimitry Andric auto AddressOrErr = ExtractAddress();
835ac9a064cSDimitry Andric if (!AddressOrErr)
836ac9a064cSDimitry Andric return AddressOrErr.takeError();
837ac9a064cSDimitry Andric RangeBaseAddress = *AddressOrErr;
838ac9a064cSDimitry Andric NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
839ac9a064cSDimitry Andric }
840ac9a064cSDimitry Andric std::vector<BBAddrMap::BBRangeEntry> BBRangeEntries;
841ac9a064cSDimitry Andric uint32_t TotalNumBlocks = 0;
842ac9a064cSDimitry Andric for (uint32_t BBRangeIndex = 0; BBRangeIndex < NumBBRanges;
843ac9a064cSDimitry Andric ++BBRangeIndex) {
844145449b1SDimitry Andric uint32_t PrevBBEndOffset = 0;
845ac9a064cSDimitry Andric if (FeatEnable.MultiBBRange) {
846ac9a064cSDimitry Andric auto AddressOrErr = ExtractAddress();
847ac9a064cSDimitry Andric if (!AddressOrErr)
848ac9a064cSDimitry Andric return AddressOrErr.takeError();
849ac9a064cSDimitry Andric RangeBaseAddress = *AddressOrErr;
850ac9a064cSDimitry Andric NumBlocksInBBRange = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
851ac9a064cSDimitry Andric }
852ac9a064cSDimitry Andric for (uint32_t BlockIndex = 0; !MetadataDecodeErr && !ULEBSizeErr && Cur &&
853ac9a064cSDimitry Andric (BlockIndex < NumBlocksInBBRange);
8547fa27ce4SDimitry Andric ++BlockIndex) {
855312c0ed1SDimitry Andric uint32_t ID = Version >= 2
856312c0ed1SDimitry Andric ? readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr)
857312c0ed1SDimitry Andric : BlockIndex;
858312c0ed1SDimitry Andric uint32_t Offset = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
859312c0ed1SDimitry Andric uint32_t Size = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
860312c0ed1SDimitry Andric uint32_t MD = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
861145449b1SDimitry Andric if (Version >= 1) {
862145449b1SDimitry Andric // Offset is calculated relative to the end of the previous BB.
863145449b1SDimitry Andric Offset += PrevBBEndOffset;
864145449b1SDimitry Andric PrevBBEndOffset = Offset + Size;
865145449b1SDimitry Andric }
8667fa27ce4SDimitry Andric Expected<BBAddrMap::BBEntry::Metadata> MetadataOrErr =
8677fa27ce4SDimitry Andric BBAddrMap::BBEntry::Metadata::decode(MD);
8687fa27ce4SDimitry Andric if (!MetadataOrErr) {
8697fa27ce4SDimitry Andric MetadataDecodeErr = MetadataOrErr.takeError();
8707fa27ce4SDimitry Andric break;
8717fa27ce4SDimitry Andric }
8727fa27ce4SDimitry Andric BBEntries.push_back({ID, Offset, Size, *MetadataOrErr});
873344a3780SDimitry Andric }
874ac9a064cSDimitry Andric TotalNumBlocks += BBEntries.size();
875ac9a064cSDimitry Andric BBRangeEntries.push_back({RangeBaseAddress, std::move(BBEntries)});
876ac9a064cSDimitry Andric }
877ac9a064cSDimitry Andric FunctionEntries.push_back({std::move(BBRangeEntries)});
878312c0ed1SDimitry Andric
879ac9a064cSDimitry Andric if (PGOAnalyses || FeatEnable.hasPGOAnalysis()) {
880312c0ed1SDimitry Andric // Function entry count
881312c0ed1SDimitry Andric uint64_t FuncEntryCount =
882312c0ed1SDimitry Andric FeatEnable.FuncEntryCount
883312c0ed1SDimitry Andric ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
884312c0ed1SDimitry Andric : 0;
885312c0ed1SDimitry Andric
886312c0ed1SDimitry Andric std::vector<PGOAnalysisMap::PGOBBEntry> PGOBBEntries;
887aca2e42cSDimitry Andric for (uint32_t BlockIndex = 0;
888ac9a064cSDimitry Andric FeatEnable.hasPGOAnalysisBBData() && !MetadataDecodeErr &&
889ac9a064cSDimitry Andric !ULEBSizeErr && Cur && (BlockIndex < TotalNumBlocks);
890312c0ed1SDimitry Andric ++BlockIndex) {
891312c0ed1SDimitry Andric // Block frequency
892312c0ed1SDimitry Andric uint64_t BBF = FeatEnable.BBFreq
893312c0ed1SDimitry Andric ? readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr)
894312c0ed1SDimitry Andric : 0;
895312c0ed1SDimitry Andric
896312c0ed1SDimitry Andric // Branch probability
897312c0ed1SDimitry Andric llvm::SmallVector<PGOAnalysisMap::PGOBBEntry::SuccessorEntry, 2>
898312c0ed1SDimitry Andric Successors;
899312c0ed1SDimitry Andric if (FeatEnable.BrProb) {
900312c0ed1SDimitry Andric auto SuccCount = readULEB128As<uint64_t>(Data, Cur, ULEBSizeErr);
901312c0ed1SDimitry Andric for (uint64_t I = 0; I < SuccCount; ++I) {
902312c0ed1SDimitry Andric uint32_t BBID = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
903312c0ed1SDimitry Andric uint32_t BrProb = readULEB128As<uint32_t>(Data, Cur, ULEBSizeErr);
904312c0ed1SDimitry Andric if (PGOAnalyses)
905312c0ed1SDimitry Andric Successors.push_back({BBID, BranchProbability::getRaw(BrProb)});
906312c0ed1SDimitry Andric }
907312c0ed1SDimitry Andric }
908312c0ed1SDimitry Andric
909312c0ed1SDimitry Andric if (PGOAnalyses)
910312c0ed1SDimitry Andric PGOBBEntries.push_back({BlockFrequency(BBF), std::move(Successors)});
911312c0ed1SDimitry Andric }
912312c0ed1SDimitry Andric
913312c0ed1SDimitry Andric if (PGOAnalyses)
914312c0ed1SDimitry Andric PGOAnalyses->push_back(
915312c0ed1SDimitry Andric {FuncEntryCount, std::move(PGOBBEntries), FeatEnable});
916312c0ed1SDimitry Andric }
917344a3780SDimitry Andric }
9187fa27ce4SDimitry Andric // Either Cur is in the error state, or we have an error in ULEBSizeErr or
9197fa27ce4SDimitry Andric // MetadataDecodeErr (but not both), but we join all errors here to be safe.
9207fa27ce4SDimitry Andric if (!Cur || ULEBSizeErr || MetadataDecodeErr)
9217fa27ce4SDimitry Andric return joinErrors(joinErrors(Cur.takeError(), std::move(ULEBSizeErr)),
9227fa27ce4SDimitry Andric std::move(MetadataDecodeErr));
923344a3780SDimitry Andric return FunctionEntries;
924344a3780SDimitry Andric }
925344a3780SDimitry Andric
9267fa27ce4SDimitry Andric template <class ELFT>
927312c0ed1SDimitry Andric Expected<std::vector<BBAddrMap>>
decodeBBAddrMap(const Elf_Shdr & Sec,const Elf_Shdr * RelaSec,std::vector<PGOAnalysisMap> * PGOAnalyses) const928312c0ed1SDimitry Andric ELFFile<ELFT>::decodeBBAddrMap(const Elf_Shdr &Sec, const Elf_Shdr *RelaSec,
929312c0ed1SDimitry Andric std::vector<PGOAnalysisMap> *PGOAnalyses) const {
930312c0ed1SDimitry Andric size_t OriginalPGOSize = PGOAnalyses ? PGOAnalyses->size() : 0;
931312c0ed1SDimitry Andric auto AddrMapsOrErr = decodeBBAddrMapImpl(*this, Sec, RelaSec, PGOAnalyses);
932312c0ed1SDimitry Andric // remove new analyses when an error occurs
933312c0ed1SDimitry Andric if (!AddrMapsOrErr && PGOAnalyses)
934312c0ed1SDimitry Andric PGOAnalyses->resize(OriginalPGOSize);
935312c0ed1SDimitry Andric return std::move(AddrMapsOrErr);
936312c0ed1SDimitry Andric }
937312c0ed1SDimitry Andric
938312c0ed1SDimitry Andric template <class ELFT>
9397fa27ce4SDimitry Andric Expected<
9407fa27ce4SDimitry Andric MapVector<const typename ELFT::Shdr *, const typename ELFT::Shdr *>>
getSectionAndRelocations(std::function<Expected<bool> (const Elf_Shdr &)> IsMatch) const9417fa27ce4SDimitry Andric ELFFile<ELFT>::getSectionAndRelocations(
9427fa27ce4SDimitry Andric std::function<Expected<bool>(const Elf_Shdr &)> IsMatch) const {
9437fa27ce4SDimitry Andric MapVector<const Elf_Shdr *, const Elf_Shdr *> SecToRelocMap;
9447fa27ce4SDimitry Andric Error Errors = Error::success();
9457fa27ce4SDimitry Andric for (const Elf_Shdr &Sec : cantFail(this->sections())) {
9467fa27ce4SDimitry Andric Expected<bool> DoesSectionMatch = IsMatch(Sec);
9477fa27ce4SDimitry Andric if (!DoesSectionMatch) {
9487fa27ce4SDimitry Andric Errors = joinErrors(std::move(Errors), DoesSectionMatch.takeError());
9497fa27ce4SDimitry Andric continue;
9507fa27ce4SDimitry Andric }
9517fa27ce4SDimitry Andric if (*DoesSectionMatch) {
9527fa27ce4SDimitry Andric if (SecToRelocMap.insert(std::make_pair(&Sec, (const Elf_Shdr *)nullptr))
9537fa27ce4SDimitry Andric .second)
9547fa27ce4SDimitry Andric continue;
9557fa27ce4SDimitry Andric }
9567fa27ce4SDimitry Andric
9577fa27ce4SDimitry Andric if (Sec.sh_type != ELF::SHT_RELA && Sec.sh_type != ELF::SHT_REL)
9587fa27ce4SDimitry Andric continue;
9597fa27ce4SDimitry Andric
9607fa27ce4SDimitry Andric Expected<const Elf_Shdr *> RelSecOrErr = this->getSection(Sec.sh_info);
9617fa27ce4SDimitry Andric if (!RelSecOrErr) {
9627fa27ce4SDimitry Andric Errors = joinErrors(std::move(Errors),
9637fa27ce4SDimitry Andric createError(describe(*this, Sec) +
9647fa27ce4SDimitry Andric ": failed to get a relocated section: " +
9657fa27ce4SDimitry Andric toString(RelSecOrErr.takeError())));
9667fa27ce4SDimitry Andric continue;
9677fa27ce4SDimitry Andric }
9687fa27ce4SDimitry Andric const Elf_Shdr *ContentsSec = *RelSecOrErr;
9697fa27ce4SDimitry Andric Expected<bool> DoesRelTargetMatch = IsMatch(*ContentsSec);
9707fa27ce4SDimitry Andric if (!DoesRelTargetMatch) {
9717fa27ce4SDimitry Andric Errors = joinErrors(std::move(Errors), DoesRelTargetMatch.takeError());
9727fa27ce4SDimitry Andric continue;
9737fa27ce4SDimitry Andric }
9747fa27ce4SDimitry Andric if (*DoesRelTargetMatch)
9757fa27ce4SDimitry Andric SecToRelocMap[ContentsSec] = &Sec;
9767fa27ce4SDimitry Andric }
9777fa27ce4SDimitry Andric if(Errors)
9787fa27ce4SDimitry Andric return std::move(Errors);
9797fa27ce4SDimitry Andric return SecToRelocMap;
9807fa27ce4SDimitry Andric }
9817fa27ce4SDimitry Andric
982044eb2f6SDimitry Andric template class llvm::object::ELFFile<ELF32LE>;
983044eb2f6SDimitry Andric template class llvm::object::ELFFile<ELF32BE>;
984044eb2f6SDimitry Andric template class llvm::object::ELFFile<ELF64LE>;
985044eb2f6SDimitry Andric template class llvm::object::ELFFile<ELF64BE>;
986