xref: /freebsd/contrib/llvm-project/llvm/lib/Object/ELFObjectFile.cpp (revision 7ef62cebc2f965b0f640263e179276928885e33d)
1 //===- ELFObjectFile.cpp - ELF object file implementation -----------------===//
2 //
3 // 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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Part of the ELFObjectFile class implementation.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Object/ELFObjectFile.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/BinaryFormat/ELF.h"
16 #include "llvm/MC/MCInstrAnalysis.h"
17 #include "llvm/MC/SubtargetFeature.h"
18 #include "llvm/MC/TargetRegistry.h"
19 #include "llvm/Object/ELF.h"
20 #include "llvm/Object/ELFTypes.h"
21 #include "llvm/Object/Error.h"
22 #include "llvm/Support/ARMAttributeParser.h"
23 #include "llvm/Support/ARMBuildAttributes.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/MathExtras.h"
26 #include "llvm/Support/RISCVAttributeParser.h"
27 #include "llvm/Support/RISCVAttributes.h"
28 #include "llvm/Support/RISCVISAInfo.h"
29 #include <algorithm>
30 #include <cstddef>
31 #include <cstdint>
32 #include <memory>
33 #include <optional>
34 #include <string>
35 #include <utility>
36 
37 using namespace llvm;
38 using namespace object;
39 
40 const EnumEntry<unsigned> llvm::object::ElfSymbolTypes[NumElfSymbolTypes] = {
41     {"None", "NOTYPE", ELF::STT_NOTYPE},
42     {"Object", "OBJECT", ELF::STT_OBJECT},
43     {"Function", "FUNC", ELF::STT_FUNC},
44     {"Section", "SECTION", ELF::STT_SECTION},
45     {"File", "FILE", ELF::STT_FILE},
46     {"Common", "COMMON", ELF::STT_COMMON},
47     {"TLS", "TLS", ELF::STT_TLS},
48     {"Unknown", "<unknown>: 7", 7},
49     {"Unknown", "<unknown>: 8", 8},
50     {"Unknown", "<unknown>: 9", 9},
51     {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC},
52     {"OS Specific", "<OS specific>: 11", 11},
53     {"OS Specific", "<OS specific>: 12", 12},
54     {"Proc Specific", "<processor specific>: 13", 13},
55     {"Proc Specific", "<processor specific>: 14", 14},
56     {"Proc Specific", "<processor specific>: 15", 15}
57 };
58 
59 ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
60     : ObjectFile(Type, Source) {}
61 
62 template <class ELFT>
63 static Expected<std::unique_ptr<ELFObjectFile<ELFT>>>
64 createPtr(MemoryBufferRef Object, bool InitContent) {
65   auto Ret = ELFObjectFile<ELFT>::create(Object, InitContent);
66   if (Error E = Ret.takeError())
67     return std::move(E);
68   return std::make_unique<ELFObjectFile<ELFT>>(std::move(*Ret));
69 }
70 
71 Expected<std::unique_ptr<ObjectFile>>
72 ObjectFile::createELFObjectFile(MemoryBufferRef Obj, bool InitContent) {
73   std::pair<unsigned char, unsigned char> Ident =
74       getElfArchType(Obj.getBuffer());
75   std::size_t MaxAlignment =
76       1ULL << countTrailingZeros(
77           reinterpret_cast<uintptr_t>(Obj.getBufferStart()));
78 
79   if (MaxAlignment < 2)
80     return createError("Insufficient alignment");
81 
82   if (Ident.first == ELF::ELFCLASS32) {
83     if (Ident.second == ELF::ELFDATA2LSB)
84       return createPtr<ELF32LE>(Obj, InitContent);
85     else if (Ident.second == ELF::ELFDATA2MSB)
86       return createPtr<ELF32BE>(Obj, InitContent);
87     else
88       return createError("Invalid ELF data");
89   } else if (Ident.first == ELF::ELFCLASS64) {
90     if (Ident.second == ELF::ELFDATA2LSB)
91       return createPtr<ELF64LE>(Obj, InitContent);
92     else if (Ident.second == ELF::ELFDATA2MSB)
93       return createPtr<ELF64BE>(Obj, InitContent);
94     else
95       return createError("Invalid ELF data");
96   }
97   return createError("Invalid ELF class");
98 }
99 
100 SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
101   SubtargetFeatures Features;
102   unsigned PlatformFlags = getPlatformFlags();
103 
104   switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
105   case ELF::EF_MIPS_ARCH_1:
106     break;
107   case ELF::EF_MIPS_ARCH_2:
108     Features.AddFeature("mips2");
109     break;
110   case ELF::EF_MIPS_ARCH_3:
111     Features.AddFeature("mips3");
112     break;
113   case ELF::EF_MIPS_ARCH_4:
114     Features.AddFeature("mips4");
115     break;
116   case ELF::EF_MIPS_ARCH_5:
117     Features.AddFeature("mips5");
118     break;
119   case ELF::EF_MIPS_ARCH_32:
120     Features.AddFeature("mips32");
121     break;
122   case ELF::EF_MIPS_ARCH_64:
123     Features.AddFeature("mips64");
124     break;
125   case ELF::EF_MIPS_ARCH_32R2:
126     Features.AddFeature("mips32r2");
127     break;
128   case ELF::EF_MIPS_ARCH_64R2:
129     Features.AddFeature("mips64r2");
130     break;
131   case ELF::EF_MIPS_ARCH_32R6:
132     Features.AddFeature("mips32r6");
133     break;
134   case ELF::EF_MIPS_ARCH_64R6:
135     Features.AddFeature("mips64r6");
136     break;
137   default:
138     llvm_unreachable("Unknown EF_MIPS_ARCH value");
139   }
140 
141   switch (PlatformFlags & ELF::EF_MIPS_MACH) {
142   case ELF::EF_MIPS_MACH_NONE:
143     // No feature associated with this value.
144     break;
145   case ELF::EF_MIPS_MACH_OCTEON:
146     Features.AddFeature("cnmips");
147     break;
148   default:
149     llvm_unreachable("Unknown EF_MIPS_ARCH value");
150   }
151 
152   if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
153     Features.AddFeature("mips16");
154   if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
155     Features.AddFeature("micromips");
156 
157   return Features;
158 }
159 
160 SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
161   SubtargetFeatures Features;
162   ARMAttributeParser Attributes;
163   if (Error E = getBuildAttributes(Attributes)) {
164     consumeError(std::move(E));
165     return SubtargetFeatures();
166   }
167 
168   // both ARMv7-M and R have to support thumb hardware div
169   bool isV7 = false;
170   std::optional<unsigned> Attr =
171       Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
172   if (Attr)
173     isV7 = *Attr == ARMBuildAttrs::v7;
174 
175   Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
176   if (Attr) {
177     switch (*Attr) {
178     case ARMBuildAttrs::ApplicationProfile:
179       Features.AddFeature("aclass");
180       break;
181     case ARMBuildAttrs::RealTimeProfile:
182       Features.AddFeature("rclass");
183       if (isV7)
184         Features.AddFeature("hwdiv");
185       break;
186     case ARMBuildAttrs::MicroControllerProfile:
187       Features.AddFeature("mclass");
188       if (isV7)
189         Features.AddFeature("hwdiv");
190       break;
191     }
192   }
193 
194   Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);
195   if (Attr) {
196     switch (*Attr) {
197     default:
198       break;
199     case ARMBuildAttrs::Not_Allowed:
200       Features.AddFeature("thumb", false);
201       Features.AddFeature("thumb2", false);
202       break;
203     case ARMBuildAttrs::AllowThumb32:
204       Features.AddFeature("thumb2");
205       break;
206     }
207   }
208 
209   Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
210   if (Attr) {
211     switch (*Attr) {
212     default:
213       break;
214     case ARMBuildAttrs::Not_Allowed:
215       Features.AddFeature("vfp2sp", false);
216       Features.AddFeature("vfp3d16sp", false);
217       Features.AddFeature("vfp4d16sp", false);
218       break;
219     case ARMBuildAttrs::AllowFPv2:
220       Features.AddFeature("vfp2");
221       break;
222     case ARMBuildAttrs::AllowFPv3A:
223     case ARMBuildAttrs::AllowFPv3B:
224       Features.AddFeature("vfp3");
225       break;
226     case ARMBuildAttrs::AllowFPv4A:
227     case ARMBuildAttrs::AllowFPv4B:
228       Features.AddFeature("vfp4");
229       break;
230     }
231   }
232 
233   Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch);
234   if (Attr) {
235     switch (*Attr) {
236     default:
237       break;
238     case ARMBuildAttrs::Not_Allowed:
239       Features.AddFeature("neon", false);
240       Features.AddFeature("fp16", false);
241       break;
242     case ARMBuildAttrs::AllowNeon:
243       Features.AddFeature("neon");
244       break;
245     case ARMBuildAttrs::AllowNeon2:
246       Features.AddFeature("neon");
247       Features.AddFeature("fp16");
248       break;
249     }
250   }
251 
252   Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch);
253   if (Attr) {
254     switch (*Attr) {
255     default:
256       break;
257     case ARMBuildAttrs::Not_Allowed:
258       Features.AddFeature("mve", false);
259       Features.AddFeature("mve.fp", false);
260       break;
261     case ARMBuildAttrs::AllowMVEInteger:
262       Features.AddFeature("mve.fp", false);
263       Features.AddFeature("mve");
264       break;
265     case ARMBuildAttrs::AllowMVEIntegerAndFloat:
266       Features.AddFeature("mve.fp");
267       break;
268     }
269   }
270 
271   Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use);
272   if (Attr) {
273     switch (*Attr) {
274     default:
275       break;
276     case ARMBuildAttrs::DisallowDIV:
277       Features.AddFeature("hwdiv", false);
278       Features.AddFeature("hwdiv-arm", false);
279       break;
280     case ARMBuildAttrs::AllowDIVExt:
281       Features.AddFeature("hwdiv");
282       Features.AddFeature("hwdiv-arm");
283       break;
284     }
285   }
286 
287   return Features;
288 }
289 
290 Expected<SubtargetFeatures> ELFObjectFileBase::getRISCVFeatures() const {
291   SubtargetFeatures Features;
292   unsigned PlatformFlags = getPlatformFlags();
293 
294   if (PlatformFlags & ELF::EF_RISCV_RVC) {
295     Features.AddFeature("c");
296   }
297 
298   RISCVAttributeParser Attributes;
299   if (Error E = getBuildAttributes(Attributes)) {
300     return std::move(E);
301   }
302 
303   std::optional<StringRef> Attr =
304       Attributes.getAttributeString(RISCVAttrs::ARCH);
305   if (Attr) {
306     auto ParseResult = RISCVISAInfo::parseNormalizedArchString(*Attr);
307     if (!ParseResult)
308       return ParseResult.takeError();
309     auto &ISAInfo = *ParseResult;
310 
311     if (ISAInfo->getXLen() == 32)
312       Features.AddFeature("64bit", false);
313     else if (ISAInfo->getXLen() == 64)
314       Features.AddFeature("64bit");
315     else
316       llvm_unreachable("XLEN should be 32 or 64.");
317 
318     Features.addFeaturesVector(ISAInfo->toFeatureVector());
319   }
320 
321   return Features;
322 }
323 
324 SubtargetFeatures ELFObjectFileBase::getLoongArchFeatures() const {
325   SubtargetFeatures Features;
326 
327   switch (getPlatformFlags() & ELF::EF_LOONGARCH_ABI_MODIFIER_MASK) {
328   case ELF::EF_LOONGARCH_ABI_SOFT_FLOAT:
329     break;
330   case ELF::EF_LOONGARCH_ABI_DOUBLE_FLOAT:
331     Features.AddFeature("d");
332     // D implies F according to LoongArch ISA spec.
333     [[fallthrough]];
334   case ELF::EF_LOONGARCH_ABI_SINGLE_FLOAT:
335     Features.AddFeature("f");
336     break;
337   }
338 
339   return Features;
340 }
341 
342 Expected<SubtargetFeatures> ELFObjectFileBase::getFeatures() const {
343   switch (getEMachine()) {
344   case ELF::EM_MIPS:
345     return getMIPSFeatures();
346   case ELF::EM_ARM:
347     return getARMFeatures();
348   case ELF::EM_RISCV:
349     return getRISCVFeatures();
350   case ELF::EM_LOONGARCH:
351     return getLoongArchFeatures();
352   default:
353     return SubtargetFeatures();
354   }
355 }
356 
357 std::optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
358   switch (getEMachine()) {
359   case ELF::EM_AMDGPU:
360     return getAMDGPUCPUName();
361   case ELF::EM_PPC64:
362     return StringRef("future");
363   default:
364     return std::nullopt;
365   }
366 }
367 
368 StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
369   assert(getEMachine() == ELF::EM_AMDGPU);
370   unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
371 
372   switch (CPU) {
373   // Radeon HD 2000/3000 Series (R600).
374   case ELF::EF_AMDGPU_MACH_R600_R600:
375     return "r600";
376   case ELF::EF_AMDGPU_MACH_R600_R630:
377     return "r630";
378   case ELF::EF_AMDGPU_MACH_R600_RS880:
379     return "rs880";
380   case ELF::EF_AMDGPU_MACH_R600_RV670:
381     return "rv670";
382 
383   // Radeon HD 4000 Series (R700).
384   case ELF::EF_AMDGPU_MACH_R600_RV710:
385     return "rv710";
386   case ELF::EF_AMDGPU_MACH_R600_RV730:
387     return "rv730";
388   case ELF::EF_AMDGPU_MACH_R600_RV770:
389     return "rv770";
390 
391   // Radeon HD 5000 Series (Evergreen).
392   case ELF::EF_AMDGPU_MACH_R600_CEDAR:
393     return "cedar";
394   case ELF::EF_AMDGPU_MACH_R600_CYPRESS:
395     return "cypress";
396   case ELF::EF_AMDGPU_MACH_R600_JUNIPER:
397     return "juniper";
398   case ELF::EF_AMDGPU_MACH_R600_REDWOOD:
399     return "redwood";
400   case ELF::EF_AMDGPU_MACH_R600_SUMO:
401     return "sumo";
402 
403   // Radeon HD 6000 Series (Northern Islands).
404   case ELF::EF_AMDGPU_MACH_R600_BARTS:
405     return "barts";
406   case ELF::EF_AMDGPU_MACH_R600_CAICOS:
407     return "caicos";
408   case ELF::EF_AMDGPU_MACH_R600_CAYMAN:
409     return "cayman";
410   case ELF::EF_AMDGPU_MACH_R600_TURKS:
411     return "turks";
412 
413   // AMDGCN GFX6.
414   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX600:
415     return "gfx600";
416   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX601:
417     return "gfx601";
418   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX602:
419     return "gfx602";
420 
421   // AMDGCN GFX7.
422   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX700:
423     return "gfx700";
424   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX701:
425     return "gfx701";
426   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX702:
427     return "gfx702";
428   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX703:
429     return "gfx703";
430   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX704:
431     return "gfx704";
432   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX705:
433     return "gfx705";
434 
435   // AMDGCN GFX8.
436   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX801:
437     return "gfx801";
438   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX802:
439     return "gfx802";
440   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX803:
441     return "gfx803";
442   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX805:
443     return "gfx805";
444   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX810:
445     return "gfx810";
446 
447   // AMDGCN GFX9.
448   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX900:
449     return "gfx900";
450   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX902:
451     return "gfx902";
452   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX904:
453     return "gfx904";
454   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX906:
455     return "gfx906";
456   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX908:
457     return "gfx908";
458   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX909:
459     return "gfx909";
460   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A:
461     return "gfx90a";
462   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C:
463     return "gfx90c";
464   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX940:
465     return "gfx940";
466 
467   // AMDGCN GFX10.
468   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010:
469     return "gfx1010";
470   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011:
471     return "gfx1011";
472   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012:
473     return "gfx1012";
474   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013:
475     return "gfx1013";
476   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030:
477     return "gfx1030";
478   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031:
479     return "gfx1031";
480   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032:
481     return "gfx1032";
482   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033:
483     return "gfx1033";
484   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034:
485     return "gfx1034";
486   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035:
487     return "gfx1035";
488   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1036:
489     return "gfx1036";
490 
491   // AMDGCN GFX11.
492   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1100:
493     return "gfx1100";
494   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1101:
495     return "gfx1101";
496   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1102:
497     return "gfx1102";
498   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1103:
499     return "gfx1103";
500   default:
501     llvm_unreachable("Unknown EF_AMDGPU_MACH value");
502   }
503 }
504 
505 // FIXME Encode from a tablegen description or target parser.
506 void ELFObjectFileBase::setARMSubArch(Triple &TheTriple) const {
507   if (TheTriple.getSubArch() != Triple::NoSubArch)
508     return;
509 
510   ARMAttributeParser Attributes;
511   if (Error E = getBuildAttributes(Attributes)) {
512     // TODO Propagate Error.
513     consumeError(std::move(E));
514     return;
515   }
516 
517   std::string Triple;
518   // Default to ARM, but use the triple if it's been set.
519   if (TheTriple.isThumb())
520     Triple = "thumb";
521   else
522     Triple = "arm";
523 
524   std::optional<unsigned> Attr =
525       Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
526   if (Attr) {
527     switch (*Attr) {
528     case ARMBuildAttrs::v4:
529       Triple += "v4";
530       break;
531     case ARMBuildAttrs::v4T:
532       Triple += "v4t";
533       break;
534     case ARMBuildAttrs::v5T:
535       Triple += "v5t";
536       break;
537     case ARMBuildAttrs::v5TE:
538       Triple += "v5te";
539       break;
540     case ARMBuildAttrs::v5TEJ:
541       Triple += "v5tej";
542       break;
543     case ARMBuildAttrs::v6:
544       Triple += "v6";
545       break;
546     case ARMBuildAttrs::v6KZ:
547       Triple += "v6kz";
548       break;
549     case ARMBuildAttrs::v6T2:
550       Triple += "v6t2";
551       break;
552     case ARMBuildAttrs::v6K:
553       Triple += "v6k";
554       break;
555     case ARMBuildAttrs::v7: {
556       std::optional<unsigned> ArchProfileAttr =
557           Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
558       if (ArchProfileAttr &&
559           *ArchProfileAttr == ARMBuildAttrs::MicroControllerProfile)
560         Triple += "v7m";
561       else
562         Triple += "v7";
563       break;
564     }
565     case ARMBuildAttrs::v6_M:
566       Triple += "v6m";
567       break;
568     case ARMBuildAttrs::v6S_M:
569       Triple += "v6sm";
570       break;
571     case ARMBuildAttrs::v7E_M:
572       Triple += "v7em";
573       break;
574     case ARMBuildAttrs::v8_A:
575       Triple += "v8a";
576       break;
577     case ARMBuildAttrs::v8_R:
578       Triple += "v8r";
579       break;
580     case ARMBuildAttrs::v8_M_Base:
581       Triple += "v8m.base";
582       break;
583     case ARMBuildAttrs::v8_M_Main:
584       Triple += "v8m.main";
585       break;
586     case ARMBuildAttrs::v8_1_M_Main:
587       Triple += "v8.1m.main";
588       break;
589     case ARMBuildAttrs::v9_A:
590       Triple += "v9a";
591       break;
592     }
593   }
594   if (!isLittleEndian())
595     Triple += "eb";
596 
597   TheTriple.setArchName(Triple);
598 }
599 
600 std::vector<std::pair<std::optional<DataRefImpl>, uint64_t>>
601 ELFObjectFileBase::getPltAddresses() const {
602   std::string Err;
603   const auto Triple = makeTriple();
604   const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
605   if (!T)
606     return {};
607   uint64_t JumpSlotReloc = 0;
608   switch (Triple.getArch()) {
609     case Triple::x86:
610       JumpSlotReloc = ELF::R_386_JUMP_SLOT;
611       break;
612     case Triple::x86_64:
613       JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
614       break;
615     case Triple::aarch64:
616     case Triple::aarch64_be:
617       JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
618       break;
619     default:
620       return {};
621   }
622   std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
623   std::unique_ptr<const MCInstrAnalysis> MIA(
624       T->createMCInstrAnalysis(MII.get()));
625   if (!MIA)
626     return {};
627   std::optional<SectionRef> Plt, RelaPlt, GotPlt;
628   for (const SectionRef &Section : sections()) {
629     Expected<StringRef> NameOrErr = Section.getName();
630     if (!NameOrErr) {
631       consumeError(NameOrErr.takeError());
632       continue;
633     }
634     StringRef Name = *NameOrErr;
635 
636     if (Name == ".plt")
637       Plt = Section;
638     else if (Name == ".rela.plt" || Name == ".rel.plt")
639       RelaPlt = Section;
640     else if (Name == ".got.plt")
641       GotPlt = Section;
642   }
643   if (!Plt || !RelaPlt || !GotPlt)
644     return {};
645   Expected<StringRef> PltContents = Plt->getContents();
646   if (!PltContents) {
647     consumeError(PltContents.takeError());
648     return {};
649   }
650   auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
651                                         arrayRefFromStringRef(*PltContents),
652                                         GotPlt->getAddress(), Triple);
653   // Build a map from GOT entry virtual address to PLT entry virtual address.
654   DenseMap<uint64_t, uint64_t> GotToPlt;
655   for (const auto &Entry : PltEntries)
656     GotToPlt.insert(std::make_pair(Entry.second, Entry.first));
657   // Find the relocations in the dynamic relocation table that point to
658   // locations in the GOT for which we know the corresponding PLT entry.
659   std::vector<std::pair<std::optional<DataRefImpl>, uint64_t>> Result;
660   for (const auto &Relocation : RelaPlt->relocations()) {
661     if (Relocation.getType() != JumpSlotReloc)
662       continue;
663     auto PltEntryIter = GotToPlt.find(Relocation.getOffset());
664     if (PltEntryIter != GotToPlt.end()) {
665       symbol_iterator Sym = Relocation.getSymbol();
666       if (Sym == symbol_end())
667         Result.emplace_back(std::nullopt, PltEntryIter->second);
668       else
669         Result.emplace_back(Sym->getRawDataRefImpl(), PltEntryIter->second);
670     }
671   }
672   return Result;
673 }
674 
675 template <class ELFT>
676 Expected<std::vector<BBAddrMap>> static readBBAddrMapImpl(
677     const ELFFile<ELFT> &EF, std::optional<unsigned> TextSectionIndex) {
678   using Elf_Shdr = typename ELFT::Shdr;
679   std::vector<BBAddrMap> BBAddrMaps;
680   const auto &Sections = cantFail(EF.sections());
681   for (const Elf_Shdr &Sec : Sections) {
682     if (Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP &&
683         Sec.sh_type != ELF::SHT_LLVM_BB_ADDR_MAP_V0)
684       continue;
685     if (TextSectionIndex) {
686       Expected<const Elf_Shdr *> TextSecOrErr = EF.getSection(Sec.sh_link);
687       if (!TextSecOrErr)
688         return createError("unable to get the linked-to section for " +
689                            describe(EF, Sec) + ": " +
690                            toString(TextSecOrErr.takeError()));
691       if (*TextSectionIndex != std::distance(Sections.begin(), *TextSecOrErr))
692         continue;
693     }
694     Expected<std::vector<BBAddrMap>> BBAddrMapOrErr = EF.decodeBBAddrMap(Sec);
695     if (!BBAddrMapOrErr)
696       return createError("unable to read " + describe(EF, Sec) + ": " +
697                          toString(BBAddrMapOrErr.takeError()));
698     std::move(BBAddrMapOrErr->begin(), BBAddrMapOrErr->end(),
699               std::back_inserter(BBAddrMaps));
700   }
701   return BBAddrMaps;
702 }
703 
704 template <class ELFT>
705 static Expected<std::vector<VersionEntry>>
706 readDynsymVersionsImpl(const ELFFile<ELFT> &EF,
707                        ELFObjectFileBase::elf_symbol_iterator_range Symbols) {
708   using Elf_Shdr = typename ELFT::Shdr;
709   const Elf_Shdr *VerSec = nullptr;
710   const Elf_Shdr *VerNeedSec = nullptr;
711   const Elf_Shdr *VerDefSec = nullptr;
712   // The user should ensure sections() can't fail here.
713   for (const Elf_Shdr &Sec : cantFail(EF.sections())) {
714     if (Sec.sh_type == ELF::SHT_GNU_versym)
715       VerSec = &Sec;
716     else if (Sec.sh_type == ELF::SHT_GNU_verdef)
717       VerDefSec = &Sec;
718     else if (Sec.sh_type == ELF::SHT_GNU_verneed)
719       VerNeedSec = &Sec;
720   }
721   if (!VerSec)
722     return std::vector<VersionEntry>();
723 
724   Expected<SmallVector<std::optional<VersionEntry>, 0>> MapOrErr =
725       EF.loadVersionMap(VerNeedSec, VerDefSec);
726   if (!MapOrErr)
727     return MapOrErr.takeError();
728 
729   std::vector<VersionEntry> Ret;
730   size_t I = 0;
731   for (const ELFSymbolRef &Sym : Symbols) {
732     ++I;
733     Expected<const typename ELFT::Versym *> VerEntryOrErr =
734         EF.template getEntry<typename ELFT::Versym>(*VerSec, I);
735     if (!VerEntryOrErr)
736       return createError("unable to read an entry with index " + Twine(I) +
737                          " from " + describe(EF, *VerSec) + ": " +
738                          toString(VerEntryOrErr.takeError()));
739 
740     Expected<uint32_t> FlagsOrErr = Sym.getFlags();
741     if (!FlagsOrErr)
742       return createError("unable to read flags for symbol with index " +
743                          Twine(I) + ": " + toString(FlagsOrErr.takeError()));
744 
745     bool IsDefault;
746     Expected<StringRef> VerOrErr = EF.getSymbolVersionByIndex(
747         (*VerEntryOrErr)->vs_index, IsDefault, *MapOrErr,
748         (*FlagsOrErr) & SymbolRef::SF_Undefined);
749     if (!VerOrErr)
750       return createError("unable to get a version for entry " + Twine(I) +
751                          " of " + describe(EF, *VerSec) + ": " +
752                          toString(VerOrErr.takeError()));
753 
754     Ret.push_back({(*VerOrErr).str(), IsDefault});
755   }
756 
757   return Ret;
758 }
759 
760 Expected<std::vector<VersionEntry>>
761 ELFObjectFileBase::readDynsymVersions() const {
762   elf_symbol_iterator_range Symbols = getDynamicSymbolIterators();
763   if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
764     return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
765   if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
766     return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
767   if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
768     return readDynsymVersionsImpl(Obj->getELFFile(), Symbols);
769   return readDynsymVersionsImpl(cast<ELF64BEObjectFile>(this)->getELFFile(),
770                                 Symbols);
771 }
772 
773 Expected<std::vector<BBAddrMap>> ELFObjectFileBase::readBBAddrMap(
774     std::optional<unsigned> TextSectionIndex) const {
775   if (const auto *Obj = dyn_cast<ELF32LEObjectFile>(this))
776     return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex);
777   if (const auto *Obj = dyn_cast<ELF64LEObjectFile>(this))
778     return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex);
779   if (const auto *Obj = dyn_cast<ELF32BEObjectFile>(this))
780     return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex);
781   if (const auto *Obj = cast<ELF64BEObjectFile>(this))
782     return readBBAddrMapImpl(Obj->getELFFile(), TextSectionIndex);
783   else
784     llvm_unreachable("Unsupported binary format");
785 }
786