10b57cec5SDimitry Andric //===- ELFObjectFile.cpp - ELF object file implementation -----------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Part of the ELFObjectFile class implementation.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "llvm/Object/ELFObjectFile.h"
140b57cec5SDimitry Andric #include "llvm/ADT/Triple.h"
150b57cec5SDimitry Andric #include "llvm/BinaryFormat/ELF.h"
160b57cec5SDimitry Andric #include "llvm/MC/MCInstrAnalysis.h"
170b57cec5SDimitry Andric #include "llvm/MC/SubtargetFeature.h"
180b57cec5SDimitry Andric #include "llvm/Object/ELF.h"
190b57cec5SDimitry Andric #include "llvm/Object/ELFTypes.h"
200b57cec5SDimitry Andric #include "llvm/Object/Error.h"
210b57cec5SDimitry Andric #include "llvm/Support/ARMAttributeParser.h"
220b57cec5SDimitry Andric #include "llvm/Support/ARMBuildAttributes.h"
230b57cec5SDimitry Andric #include "llvm/Support/Endian.h"
240b57cec5SDimitry Andric #include "llvm/Support/ErrorHandling.h"
250b57cec5SDimitry Andric #include "llvm/Support/MathExtras.h"
265ffd83dbSDimitry Andric #include "llvm/Support/RISCVAttributeParser.h"
275ffd83dbSDimitry Andric #include "llvm/Support/RISCVAttributes.h"
280b57cec5SDimitry Andric #include "llvm/Support/TargetRegistry.h"
290b57cec5SDimitry Andric #include <algorithm>
300b57cec5SDimitry Andric #include <cstddef>
310b57cec5SDimitry Andric #include <cstdint>
320b57cec5SDimitry Andric #include <memory>
330b57cec5SDimitry Andric #include <string>
340b57cec5SDimitry Andric #include <system_error>
350b57cec5SDimitry Andric #include <utility>
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric using namespace llvm;
380b57cec5SDimitry Andric using namespace object;
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric const EnumEntry<unsigned> llvm::object::ElfSymbolTypes[NumElfSymbolTypes] = {
410b57cec5SDimitry Andric     {"None", "NOTYPE", ELF::STT_NOTYPE},
420b57cec5SDimitry Andric     {"Object", "OBJECT", ELF::STT_OBJECT},
430b57cec5SDimitry Andric     {"Function", "FUNC", ELF::STT_FUNC},
440b57cec5SDimitry Andric     {"Section", "SECTION", ELF::STT_SECTION},
450b57cec5SDimitry Andric     {"File", "FILE", ELF::STT_FILE},
460b57cec5SDimitry Andric     {"Common", "COMMON", ELF::STT_COMMON},
470b57cec5SDimitry Andric     {"TLS", "TLS", ELF::STT_TLS},
488bcb0991SDimitry Andric     {"Unknown", "<unknown>: 7", 7},
498bcb0991SDimitry Andric     {"Unknown", "<unknown>: 8", 8},
508bcb0991SDimitry Andric     {"Unknown", "<unknown>: 9", 9},
518bcb0991SDimitry Andric     {"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC},
528bcb0991SDimitry Andric     {"OS Specific", "<OS specific>: 11", 11},
538bcb0991SDimitry Andric     {"OS Specific", "<OS specific>: 12", 12},
548bcb0991SDimitry Andric     {"Proc Specific", "<processor specific>: 13", 13},
558bcb0991SDimitry Andric     {"Proc Specific", "<processor specific>: 14", 14},
568bcb0991SDimitry Andric     {"Proc Specific", "<processor specific>: 15", 15}
578bcb0991SDimitry Andric };
580b57cec5SDimitry Andric 
ELFObjectFileBase(unsigned int Type,MemoryBufferRef Source)590b57cec5SDimitry Andric ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
600b57cec5SDimitry Andric     : ObjectFile(Type, Source) {}
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric template <class ELFT>
630b57cec5SDimitry Andric static Expected<std::unique_ptr<ELFObjectFile<ELFT>>>
createPtr(MemoryBufferRef Object,bool InitContent)64af732203SDimitry Andric createPtr(MemoryBufferRef Object, bool InitContent) {
65af732203SDimitry Andric   auto Ret = ELFObjectFile<ELFT>::create(Object, InitContent);
660b57cec5SDimitry Andric   if (Error E = Ret.takeError())
670b57cec5SDimitry Andric     return std::move(E);
688bcb0991SDimitry Andric   return std::make_unique<ELFObjectFile<ELFT>>(std::move(*Ret));
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric Expected<std::unique_ptr<ObjectFile>>
createELFObjectFile(MemoryBufferRef Obj,bool InitContent)72af732203SDimitry Andric ObjectFile::createELFObjectFile(MemoryBufferRef Obj, bool InitContent) {
730b57cec5SDimitry Andric   std::pair<unsigned char, unsigned char> Ident =
740b57cec5SDimitry Andric       getElfArchType(Obj.getBuffer());
750b57cec5SDimitry Andric   std::size_t MaxAlignment =
76af732203SDimitry Andric       1ULL << countTrailingZeros(
77af732203SDimitry Andric           reinterpret_cast<uintptr_t>(Obj.getBufferStart()));
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   if (MaxAlignment < 2)
800b57cec5SDimitry Andric     return createError("Insufficient alignment");
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   if (Ident.first == ELF::ELFCLASS32) {
830b57cec5SDimitry Andric     if (Ident.second == ELF::ELFDATA2LSB)
84af732203SDimitry Andric       return createPtr<ELF32LE>(Obj, InitContent);
850b57cec5SDimitry Andric     else if (Ident.second == ELF::ELFDATA2MSB)
86af732203SDimitry Andric       return createPtr<ELF32BE>(Obj, InitContent);
870b57cec5SDimitry Andric     else
880b57cec5SDimitry Andric       return createError("Invalid ELF data");
890b57cec5SDimitry Andric   } else if (Ident.first == ELF::ELFCLASS64) {
900b57cec5SDimitry Andric     if (Ident.second == ELF::ELFDATA2LSB)
91af732203SDimitry Andric       return createPtr<ELF64LE>(Obj, InitContent);
920b57cec5SDimitry Andric     else if (Ident.second == ELF::ELFDATA2MSB)
93af732203SDimitry Andric       return createPtr<ELF64BE>(Obj, InitContent);
940b57cec5SDimitry Andric     else
950b57cec5SDimitry Andric       return createError("Invalid ELF data");
960b57cec5SDimitry Andric   }
970b57cec5SDimitry Andric   return createError("Invalid ELF class");
980b57cec5SDimitry Andric }
990b57cec5SDimitry Andric 
getMIPSFeatures() const1000b57cec5SDimitry Andric SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
1010b57cec5SDimitry Andric   SubtargetFeatures Features;
1020b57cec5SDimitry Andric   unsigned PlatformFlags = getPlatformFlags();
1030b57cec5SDimitry Andric 
1040b57cec5SDimitry Andric   switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
1050b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_1:
1060b57cec5SDimitry Andric     break;
1070b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_2:
1080b57cec5SDimitry Andric     Features.AddFeature("mips2");
1090b57cec5SDimitry Andric     break;
1100b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_3:
1110b57cec5SDimitry Andric     Features.AddFeature("mips3");
1120b57cec5SDimitry Andric     break;
1130b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_4:
1140b57cec5SDimitry Andric     Features.AddFeature("mips4");
1150b57cec5SDimitry Andric     break;
1160b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_5:
1170b57cec5SDimitry Andric     Features.AddFeature("mips5");
1180b57cec5SDimitry Andric     break;
1190b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_32:
1200b57cec5SDimitry Andric     Features.AddFeature("mips32");
1210b57cec5SDimitry Andric     break;
1220b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_64:
1230b57cec5SDimitry Andric     Features.AddFeature("mips64");
1240b57cec5SDimitry Andric     break;
1250b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_32R2:
1260b57cec5SDimitry Andric     Features.AddFeature("mips32r2");
1270b57cec5SDimitry Andric     break;
1280b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_64R2:
1290b57cec5SDimitry Andric     Features.AddFeature("mips64r2");
1300b57cec5SDimitry Andric     break;
1310b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_32R6:
1320b57cec5SDimitry Andric     Features.AddFeature("mips32r6");
1330b57cec5SDimitry Andric     break;
1340b57cec5SDimitry Andric   case ELF::EF_MIPS_ARCH_64R6:
1350b57cec5SDimitry Andric     Features.AddFeature("mips64r6");
1360b57cec5SDimitry Andric     break;
1370b57cec5SDimitry Andric   default:
1380b57cec5SDimitry Andric     llvm_unreachable("Unknown EF_MIPS_ARCH value");
1390b57cec5SDimitry Andric   }
1400b57cec5SDimitry Andric 
1410b57cec5SDimitry Andric   switch (PlatformFlags & ELF::EF_MIPS_MACH) {
1420b57cec5SDimitry Andric   case ELF::EF_MIPS_MACH_NONE:
1430b57cec5SDimitry Andric     // No feature associated with this value.
1440b57cec5SDimitry Andric     break;
1450b57cec5SDimitry Andric   case ELF::EF_MIPS_MACH_OCTEON:
1460b57cec5SDimitry Andric     Features.AddFeature("cnmips");
1470b57cec5SDimitry Andric     break;
1480b57cec5SDimitry Andric   default:
1490b57cec5SDimitry Andric     llvm_unreachable("Unknown EF_MIPS_ARCH value");
1500b57cec5SDimitry Andric   }
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
1530b57cec5SDimitry Andric     Features.AddFeature("mips16");
1540b57cec5SDimitry Andric   if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
1550b57cec5SDimitry Andric     Features.AddFeature("micromips");
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric   return Features;
1580b57cec5SDimitry Andric }
1590b57cec5SDimitry Andric 
getARMFeatures() const1600b57cec5SDimitry Andric SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
1610b57cec5SDimitry Andric   SubtargetFeatures Features;
1620b57cec5SDimitry Andric   ARMAttributeParser Attributes;
1635ffd83dbSDimitry Andric   if (Error E = getBuildAttributes(Attributes)) {
1645ffd83dbSDimitry Andric     consumeError(std::move(E));
1650b57cec5SDimitry Andric     return SubtargetFeatures();
1665ffd83dbSDimitry Andric   }
1670b57cec5SDimitry Andric 
1680b57cec5SDimitry Andric   // both ARMv7-M and R have to support thumb hardware div
1690b57cec5SDimitry Andric   bool isV7 = false;
1705ffd83dbSDimitry Andric   Optional<unsigned> Attr =
1715ffd83dbSDimitry Andric       Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
1725ffd83dbSDimitry Andric   if (Attr.hasValue())
1735ffd83dbSDimitry Andric     isV7 = Attr.getValue() == ARMBuildAttrs::v7;
1740b57cec5SDimitry Andric 
1755ffd83dbSDimitry Andric   Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
1765ffd83dbSDimitry Andric   if (Attr.hasValue()) {
1775ffd83dbSDimitry Andric     switch (Attr.getValue()) {
1780b57cec5SDimitry Andric     case ARMBuildAttrs::ApplicationProfile:
1790b57cec5SDimitry Andric       Features.AddFeature("aclass");
1800b57cec5SDimitry Andric       break;
1810b57cec5SDimitry Andric     case ARMBuildAttrs::RealTimeProfile:
1820b57cec5SDimitry Andric       Features.AddFeature("rclass");
1830b57cec5SDimitry Andric       if (isV7)
1840b57cec5SDimitry Andric         Features.AddFeature("hwdiv");
1850b57cec5SDimitry Andric       break;
1860b57cec5SDimitry Andric     case ARMBuildAttrs::MicroControllerProfile:
1870b57cec5SDimitry Andric       Features.AddFeature("mclass");
1880b57cec5SDimitry Andric       if (isV7)
1890b57cec5SDimitry Andric         Features.AddFeature("hwdiv");
1900b57cec5SDimitry Andric       break;
1910b57cec5SDimitry Andric     }
1920b57cec5SDimitry Andric   }
1930b57cec5SDimitry Andric 
1945ffd83dbSDimitry Andric   Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);
1955ffd83dbSDimitry Andric   if (Attr.hasValue()) {
1965ffd83dbSDimitry Andric     switch (Attr.getValue()) {
1970b57cec5SDimitry Andric     default:
1980b57cec5SDimitry Andric       break;
1990b57cec5SDimitry Andric     case ARMBuildAttrs::Not_Allowed:
2000b57cec5SDimitry Andric       Features.AddFeature("thumb", false);
2010b57cec5SDimitry Andric       Features.AddFeature("thumb2", false);
2020b57cec5SDimitry Andric       break;
2030b57cec5SDimitry Andric     case ARMBuildAttrs::AllowThumb32:
2040b57cec5SDimitry Andric       Features.AddFeature("thumb2");
2050b57cec5SDimitry Andric       break;
2060b57cec5SDimitry Andric     }
2070b57cec5SDimitry Andric   }
2080b57cec5SDimitry Andric 
2095ffd83dbSDimitry Andric   Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
2105ffd83dbSDimitry Andric   if (Attr.hasValue()) {
2115ffd83dbSDimitry Andric     switch (Attr.getValue()) {
2120b57cec5SDimitry Andric     default:
2130b57cec5SDimitry Andric       break;
2140b57cec5SDimitry Andric     case ARMBuildAttrs::Not_Allowed:
2158bcb0991SDimitry Andric       Features.AddFeature("vfp2sp", false);
2160b57cec5SDimitry Andric       Features.AddFeature("vfp3d16sp", false);
2170b57cec5SDimitry Andric       Features.AddFeature("vfp4d16sp", false);
2180b57cec5SDimitry Andric       break;
2190b57cec5SDimitry Andric     case ARMBuildAttrs::AllowFPv2:
2200b57cec5SDimitry Andric       Features.AddFeature("vfp2");
2210b57cec5SDimitry Andric       break;
2220b57cec5SDimitry Andric     case ARMBuildAttrs::AllowFPv3A:
2230b57cec5SDimitry Andric     case ARMBuildAttrs::AllowFPv3B:
2240b57cec5SDimitry Andric       Features.AddFeature("vfp3");
2250b57cec5SDimitry Andric       break;
2260b57cec5SDimitry Andric     case ARMBuildAttrs::AllowFPv4A:
2270b57cec5SDimitry Andric     case ARMBuildAttrs::AllowFPv4B:
2280b57cec5SDimitry Andric       Features.AddFeature("vfp4");
2290b57cec5SDimitry Andric       break;
2300b57cec5SDimitry Andric     }
2310b57cec5SDimitry Andric   }
2320b57cec5SDimitry Andric 
2335ffd83dbSDimitry Andric   Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch);
2345ffd83dbSDimitry Andric   if (Attr.hasValue()) {
2355ffd83dbSDimitry Andric     switch (Attr.getValue()) {
2360b57cec5SDimitry Andric     default:
2370b57cec5SDimitry Andric       break;
2380b57cec5SDimitry Andric     case ARMBuildAttrs::Not_Allowed:
2390b57cec5SDimitry Andric       Features.AddFeature("neon", false);
2400b57cec5SDimitry Andric       Features.AddFeature("fp16", false);
2410b57cec5SDimitry Andric       break;
2420b57cec5SDimitry Andric     case ARMBuildAttrs::AllowNeon:
2430b57cec5SDimitry Andric       Features.AddFeature("neon");
2440b57cec5SDimitry Andric       break;
2450b57cec5SDimitry Andric     case ARMBuildAttrs::AllowNeon2:
2460b57cec5SDimitry Andric       Features.AddFeature("neon");
2470b57cec5SDimitry Andric       Features.AddFeature("fp16");
2480b57cec5SDimitry Andric       break;
2490b57cec5SDimitry Andric     }
2500b57cec5SDimitry Andric   }
2510b57cec5SDimitry Andric 
2525ffd83dbSDimitry Andric   Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch);
2535ffd83dbSDimitry Andric   if (Attr.hasValue()) {
2545ffd83dbSDimitry Andric     switch (Attr.getValue()) {
2550b57cec5SDimitry Andric     default:
2560b57cec5SDimitry Andric       break;
2570b57cec5SDimitry Andric     case ARMBuildAttrs::Not_Allowed:
2580b57cec5SDimitry Andric       Features.AddFeature("mve", false);
2590b57cec5SDimitry Andric       Features.AddFeature("mve.fp", false);
2600b57cec5SDimitry Andric       break;
2610b57cec5SDimitry Andric     case ARMBuildAttrs::AllowMVEInteger:
2620b57cec5SDimitry Andric       Features.AddFeature("mve.fp", false);
2630b57cec5SDimitry Andric       Features.AddFeature("mve");
2640b57cec5SDimitry Andric       break;
2650b57cec5SDimitry Andric     case ARMBuildAttrs::AllowMVEIntegerAndFloat:
2660b57cec5SDimitry Andric       Features.AddFeature("mve.fp");
2670b57cec5SDimitry Andric       break;
2680b57cec5SDimitry Andric     }
2690b57cec5SDimitry Andric   }
2700b57cec5SDimitry Andric 
2715ffd83dbSDimitry Andric   Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use);
2725ffd83dbSDimitry Andric   if (Attr.hasValue()) {
2735ffd83dbSDimitry Andric     switch (Attr.getValue()) {
2740b57cec5SDimitry Andric     default:
2750b57cec5SDimitry Andric       break;
2760b57cec5SDimitry Andric     case ARMBuildAttrs::DisallowDIV:
2770b57cec5SDimitry Andric       Features.AddFeature("hwdiv", false);
2780b57cec5SDimitry Andric       Features.AddFeature("hwdiv-arm", false);
2790b57cec5SDimitry Andric       break;
2800b57cec5SDimitry Andric     case ARMBuildAttrs::AllowDIVExt:
2810b57cec5SDimitry Andric       Features.AddFeature("hwdiv");
2820b57cec5SDimitry Andric       Features.AddFeature("hwdiv-arm");
2830b57cec5SDimitry Andric       break;
2840b57cec5SDimitry Andric     }
2850b57cec5SDimitry Andric   }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric   return Features;
2880b57cec5SDimitry Andric }
2890b57cec5SDimitry Andric 
getRISCVFeatures() const2900b57cec5SDimitry Andric SubtargetFeatures ELFObjectFileBase::getRISCVFeatures() const {
2910b57cec5SDimitry Andric   SubtargetFeatures Features;
2920b57cec5SDimitry Andric   unsigned PlatformFlags = getPlatformFlags();
2930b57cec5SDimitry Andric 
2940b57cec5SDimitry Andric   if (PlatformFlags & ELF::EF_RISCV_RVC) {
2950b57cec5SDimitry Andric     Features.AddFeature("c");
2960b57cec5SDimitry Andric   }
2970b57cec5SDimitry Andric 
2985ffd83dbSDimitry Andric   // Add features according to the ELF attribute section.
2995ffd83dbSDimitry Andric   // If there are any unrecognized features, ignore them.
3005ffd83dbSDimitry Andric   RISCVAttributeParser Attributes;
3015ffd83dbSDimitry Andric   if (Error E = getBuildAttributes(Attributes)) {
3025ffd83dbSDimitry Andric     // TODO Propagate Error.
3035ffd83dbSDimitry Andric     consumeError(std::move(E));
3045ffd83dbSDimitry Andric     return Features; // Keep "c" feature if there is one in PlatformFlags.
3055ffd83dbSDimitry Andric   }
3065ffd83dbSDimitry Andric 
3075ffd83dbSDimitry Andric   Optional<StringRef> Attr = Attributes.getAttributeString(RISCVAttrs::ARCH);
3085ffd83dbSDimitry Andric   if (Attr.hasValue()) {
3095ffd83dbSDimitry Andric     // The Arch pattern is [rv32|rv64][i|e]version(_[m|a|f|d|c]version)*
3105ffd83dbSDimitry Andric     // Version string pattern is (major)p(minor). Major and minor are optional.
3115ffd83dbSDimitry Andric     // For example, a version number could be 2p0, 2, or p92.
3125ffd83dbSDimitry Andric     StringRef Arch = Attr.getValue();
3135ffd83dbSDimitry Andric     if (Arch.consume_front("rv32"))
3145ffd83dbSDimitry Andric       Features.AddFeature("64bit", false);
3155ffd83dbSDimitry Andric     else if (Arch.consume_front("rv64"))
3165ffd83dbSDimitry Andric       Features.AddFeature("64bit");
3175ffd83dbSDimitry Andric 
3185ffd83dbSDimitry Andric     while (!Arch.empty()) {
3195ffd83dbSDimitry Andric       switch (Arch[0]) {
3205ffd83dbSDimitry Andric       default:
3215ffd83dbSDimitry Andric         break; // Ignore unexpected features.
3225ffd83dbSDimitry Andric       case 'i':
3235ffd83dbSDimitry Andric         Features.AddFeature("e", false);
3245ffd83dbSDimitry Andric         break;
3255ffd83dbSDimitry Andric       case 'd':
3265ffd83dbSDimitry Andric         Features.AddFeature("f"); // D-ext will imply F-ext.
3275ffd83dbSDimitry Andric         LLVM_FALLTHROUGH;
3285ffd83dbSDimitry Andric       case 'e':
3295ffd83dbSDimitry Andric       case 'm':
3305ffd83dbSDimitry Andric       case 'a':
3315ffd83dbSDimitry Andric       case 'f':
3325ffd83dbSDimitry Andric       case 'c':
3335ffd83dbSDimitry Andric         Features.AddFeature(Arch.take_front());
3345ffd83dbSDimitry Andric         break;
3355ffd83dbSDimitry Andric       }
3365ffd83dbSDimitry Andric 
3375ffd83dbSDimitry Andric       // FIXME: Handle version numbers.
3385ffd83dbSDimitry Andric       Arch = Arch.drop_until([](char c) { return c == '_' || c == '\0'; });
3395ffd83dbSDimitry Andric       Arch = Arch.drop_while([](char c) { return c == '_'; });
3405ffd83dbSDimitry Andric     }
3415ffd83dbSDimitry Andric   }
3425ffd83dbSDimitry Andric 
3430b57cec5SDimitry Andric   return Features;
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric 
getFeatures() const3460b57cec5SDimitry Andric SubtargetFeatures ELFObjectFileBase::getFeatures() const {
3470b57cec5SDimitry Andric   switch (getEMachine()) {
3480b57cec5SDimitry Andric   case ELF::EM_MIPS:
3490b57cec5SDimitry Andric     return getMIPSFeatures();
3500b57cec5SDimitry Andric   case ELF::EM_ARM:
3510b57cec5SDimitry Andric     return getARMFeatures();
3520b57cec5SDimitry Andric   case ELF::EM_RISCV:
3530b57cec5SDimitry Andric     return getRISCVFeatures();
3540b57cec5SDimitry Andric   default:
3550b57cec5SDimitry Andric     return SubtargetFeatures();
3560b57cec5SDimitry Andric   }
3570b57cec5SDimitry Andric }
3580b57cec5SDimitry Andric 
tryGetCPUName() const359af732203SDimitry Andric Optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
360af732203SDimitry Andric   switch (getEMachine()) {
361af732203SDimitry Andric   case ELF::EM_AMDGPU:
362af732203SDimitry Andric     return getAMDGPUCPUName();
363af732203SDimitry Andric   default:
364af732203SDimitry Andric     return None;
365af732203SDimitry Andric   }
366af732203SDimitry Andric }
367af732203SDimitry Andric 
getAMDGPUCPUName() const368af732203SDimitry Andric StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
369af732203SDimitry Andric   assert(getEMachine() == ELF::EM_AMDGPU);
370af732203SDimitry Andric   unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
371af732203SDimitry Andric 
372af732203SDimitry Andric   switch (CPU) {
373af732203SDimitry Andric   // Radeon HD 2000/3000 Series (R600).
374af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_R600:
375af732203SDimitry Andric     return "r600";
376af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_R630:
377af732203SDimitry Andric     return "r630";
378af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_RS880:
379af732203SDimitry Andric     return "rs880";
380af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_RV670:
381af732203SDimitry Andric     return "rv670";
382af732203SDimitry Andric 
383af732203SDimitry Andric   // Radeon HD 4000 Series (R700).
384af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_RV710:
385af732203SDimitry Andric     return "rv710";
386af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_RV730:
387af732203SDimitry Andric     return "rv730";
388af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_RV770:
389af732203SDimitry Andric     return "rv770";
390af732203SDimitry Andric 
391af732203SDimitry Andric   // Radeon HD 5000 Series (Evergreen).
392af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_CEDAR:
393af732203SDimitry Andric     return "cedar";
394af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_CYPRESS:
395af732203SDimitry Andric     return "cypress";
396af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_JUNIPER:
397af732203SDimitry Andric     return "juniper";
398af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_REDWOOD:
399af732203SDimitry Andric     return "redwood";
400af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_SUMO:
401af732203SDimitry Andric     return "sumo";
402af732203SDimitry Andric 
403af732203SDimitry Andric   // Radeon HD 6000 Series (Northern Islands).
404af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_BARTS:
405af732203SDimitry Andric     return "barts";
406af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_CAICOS:
407af732203SDimitry Andric     return "caicos";
408af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_CAYMAN:
409af732203SDimitry Andric     return "cayman";
410af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_R600_TURKS:
411af732203SDimitry Andric     return "turks";
412af732203SDimitry Andric 
413af732203SDimitry Andric   // AMDGCN GFX6.
414af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX600:
415af732203SDimitry Andric     return "gfx600";
416af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX601:
417af732203SDimitry Andric     return "gfx601";
418af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX602:
419af732203SDimitry Andric     return "gfx602";
420af732203SDimitry Andric 
421af732203SDimitry Andric   // AMDGCN GFX7.
422af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX700:
423af732203SDimitry Andric     return "gfx700";
424af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX701:
425af732203SDimitry Andric     return "gfx701";
426af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX702:
427af732203SDimitry Andric     return "gfx702";
428af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX703:
429af732203SDimitry Andric     return "gfx703";
430af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX704:
431af732203SDimitry Andric     return "gfx704";
432af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX705:
433af732203SDimitry Andric     return "gfx705";
434af732203SDimitry Andric 
435af732203SDimitry Andric   // AMDGCN GFX8.
436af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX801:
437af732203SDimitry Andric     return "gfx801";
438af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX802:
439af732203SDimitry Andric     return "gfx802";
440af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX803:
441af732203SDimitry Andric     return "gfx803";
442af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX805:
443af732203SDimitry Andric     return "gfx805";
444af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX810:
445af732203SDimitry Andric     return "gfx810";
446af732203SDimitry Andric 
447af732203SDimitry Andric   // AMDGCN GFX9.
448af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX900:
449af732203SDimitry Andric     return "gfx900";
450af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX902:
451af732203SDimitry Andric     return "gfx902";
452af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX904:
453af732203SDimitry Andric     return "gfx904";
454af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX906:
455af732203SDimitry Andric     return "gfx906";
456af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX908:
457af732203SDimitry Andric     return "gfx908";
458af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX909:
459af732203SDimitry Andric     return "gfx909";
460*5f7ddb14SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A:
461*5f7ddb14SDimitry Andric     return "gfx90a";
462af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C:
463af732203SDimitry Andric     return "gfx90c";
464af732203SDimitry Andric 
465af732203SDimitry Andric   // AMDGCN GFX10.
466af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010:
467af732203SDimitry Andric     return "gfx1010";
468af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011:
469af732203SDimitry Andric     return "gfx1011";
470af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012:
471af732203SDimitry Andric     return "gfx1012";
472*5f7ddb14SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013:
473*5f7ddb14SDimitry Andric     return "gfx1013";
474af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030:
475af732203SDimitry Andric     return "gfx1030";
476af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031:
477af732203SDimitry Andric     return "gfx1031";
478af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032:
479af732203SDimitry Andric     return "gfx1032";
480af732203SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033:
481af732203SDimitry Andric     return "gfx1033";
482*5f7ddb14SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034:
483*5f7ddb14SDimitry Andric     return "gfx1034";
484*5f7ddb14SDimitry Andric   case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035:
485*5f7ddb14SDimitry Andric     return "gfx1035";
486af732203SDimitry Andric   default:
487af732203SDimitry Andric     llvm_unreachable("Unknown EF_AMDGPU_MACH value");
488af732203SDimitry Andric   }
489af732203SDimitry Andric }
490af732203SDimitry Andric 
4910b57cec5SDimitry Andric // FIXME Encode from a tablegen description or target parser.
setARMSubArch(Triple & TheTriple) const4920b57cec5SDimitry Andric void ELFObjectFileBase::setARMSubArch(Triple &TheTriple) const {
4930b57cec5SDimitry Andric   if (TheTriple.getSubArch() != Triple::NoSubArch)
4940b57cec5SDimitry Andric     return;
4950b57cec5SDimitry Andric 
4960b57cec5SDimitry Andric   ARMAttributeParser Attributes;
4975ffd83dbSDimitry Andric   if (Error E = getBuildAttributes(Attributes)) {
4985ffd83dbSDimitry Andric     // TODO Propagate Error.
4995ffd83dbSDimitry Andric     consumeError(std::move(E));
5000b57cec5SDimitry Andric     return;
5015ffd83dbSDimitry Andric   }
5020b57cec5SDimitry Andric 
5030b57cec5SDimitry Andric   std::string Triple;
5040b57cec5SDimitry Andric   // Default to ARM, but use the triple if it's been set.
5050b57cec5SDimitry Andric   if (TheTriple.isThumb())
5060b57cec5SDimitry Andric     Triple = "thumb";
5070b57cec5SDimitry Andric   else
5080b57cec5SDimitry Andric     Triple = "arm";
5090b57cec5SDimitry Andric 
5105ffd83dbSDimitry Andric   Optional<unsigned> Attr =
5115ffd83dbSDimitry Andric       Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
5125ffd83dbSDimitry Andric   if (Attr.hasValue()) {
5135ffd83dbSDimitry Andric     switch (Attr.getValue()) {
5140b57cec5SDimitry Andric     case ARMBuildAttrs::v4:
5150b57cec5SDimitry Andric       Triple += "v4";
5160b57cec5SDimitry Andric       break;
5170b57cec5SDimitry Andric     case ARMBuildAttrs::v4T:
5180b57cec5SDimitry Andric       Triple += "v4t";
5190b57cec5SDimitry Andric       break;
5200b57cec5SDimitry Andric     case ARMBuildAttrs::v5T:
5210b57cec5SDimitry Andric       Triple += "v5t";
5220b57cec5SDimitry Andric       break;
5230b57cec5SDimitry Andric     case ARMBuildAttrs::v5TE:
5240b57cec5SDimitry Andric       Triple += "v5te";
5250b57cec5SDimitry Andric       break;
5260b57cec5SDimitry Andric     case ARMBuildAttrs::v5TEJ:
5270b57cec5SDimitry Andric       Triple += "v5tej";
5280b57cec5SDimitry Andric       break;
5290b57cec5SDimitry Andric     case ARMBuildAttrs::v6:
5300b57cec5SDimitry Andric       Triple += "v6";
5310b57cec5SDimitry Andric       break;
5320b57cec5SDimitry Andric     case ARMBuildAttrs::v6KZ:
5330b57cec5SDimitry Andric       Triple += "v6kz";
5340b57cec5SDimitry Andric       break;
5350b57cec5SDimitry Andric     case ARMBuildAttrs::v6T2:
5360b57cec5SDimitry Andric       Triple += "v6t2";
5370b57cec5SDimitry Andric       break;
5380b57cec5SDimitry Andric     case ARMBuildAttrs::v6K:
5390b57cec5SDimitry Andric       Triple += "v6k";
5400b57cec5SDimitry Andric       break;
5410b57cec5SDimitry Andric     case ARMBuildAttrs::v7:
5420b57cec5SDimitry Andric       Triple += "v7";
5430b57cec5SDimitry Andric       break;
5440b57cec5SDimitry Andric     case ARMBuildAttrs::v6_M:
5450b57cec5SDimitry Andric       Triple += "v6m";
5460b57cec5SDimitry Andric       break;
5470b57cec5SDimitry Andric     case ARMBuildAttrs::v6S_M:
5480b57cec5SDimitry Andric       Triple += "v6sm";
5490b57cec5SDimitry Andric       break;
5500b57cec5SDimitry Andric     case ARMBuildAttrs::v7E_M:
5510b57cec5SDimitry Andric       Triple += "v7em";
5520b57cec5SDimitry Andric       break;
5538bcb0991SDimitry Andric     case ARMBuildAttrs::v8_A:
5548bcb0991SDimitry Andric       Triple += "v8a";
5558bcb0991SDimitry Andric       break;
5568bcb0991SDimitry Andric     case ARMBuildAttrs::v8_R:
5578bcb0991SDimitry Andric       Triple += "v8r";
5588bcb0991SDimitry Andric       break;
5598bcb0991SDimitry Andric     case ARMBuildAttrs::v8_M_Base:
5608bcb0991SDimitry Andric       Triple += "v8m.base";
5618bcb0991SDimitry Andric       break;
5628bcb0991SDimitry Andric     case ARMBuildAttrs::v8_M_Main:
5638bcb0991SDimitry Andric       Triple += "v8m.main";
5648bcb0991SDimitry Andric       break;
5658bcb0991SDimitry Andric     case ARMBuildAttrs::v8_1_M_Main:
5668bcb0991SDimitry Andric       Triple += "v8.1m.main";
5678bcb0991SDimitry Andric       break;
5680b57cec5SDimitry Andric     }
5690b57cec5SDimitry Andric   }
5700b57cec5SDimitry Andric   if (!isLittleEndian())
5710b57cec5SDimitry Andric     Triple += "eb";
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   TheTriple.setArchName(Triple);
5740b57cec5SDimitry Andric }
5750b57cec5SDimitry Andric 
576af732203SDimitry Andric std::vector<std::pair<Optional<DataRefImpl>, uint64_t>>
getPltAddresses() const5770b57cec5SDimitry Andric ELFObjectFileBase::getPltAddresses() const {
5780b57cec5SDimitry Andric   std::string Err;
5790b57cec5SDimitry Andric   const auto Triple = makeTriple();
5800b57cec5SDimitry Andric   const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
5810b57cec5SDimitry Andric   if (!T)
5820b57cec5SDimitry Andric     return {};
5830b57cec5SDimitry Andric   uint64_t JumpSlotReloc = 0;
5840b57cec5SDimitry Andric   switch (Triple.getArch()) {
5850b57cec5SDimitry Andric     case Triple::x86:
5860b57cec5SDimitry Andric       JumpSlotReloc = ELF::R_386_JUMP_SLOT;
5870b57cec5SDimitry Andric       break;
5880b57cec5SDimitry Andric     case Triple::x86_64:
5890b57cec5SDimitry Andric       JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
5900b57cec5SDimitry Andric       break;
5910b57cec5SDimitry Andric     case Triple::aarch64:
592*5f7ddb14SDimitry Andric     case Triple::aarch64_be:
5930b57cec5SDimitry Andric       JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
5940b57cec5SDimitry Andric       break;
5950b57cec5SDimitry Andric     default:
5960b57cec5SDimitry Andric       return {};
5970b57cec5SDimitry Andric   }
5980b57cec5SDimitry Andric   std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
5990b57cec5SDimitry Andric   std::unique_ptr<const MCInstrAnalysis> MIA(
6000b57cec5SDimitry Andric       T->createMCInstrAnalysis(MII.get()));
6010b57cec5SDimitry Andric   if (!MIA)
6020b57cec5SDimitry Andric     return {};
6030b57cec5SDimitry Andric   Optional<SectionRef> Plt = None, RelaPlt = None, GotPlt = None;
6040b57cec5SDimitry Andric   for (const SectionRef &Section : sections()) {
6058bcb0991SDimitry Andric     Expected<StringRef> NameOrErr = Section.getName();
6068bcb0991SDimitry Andric     if (!NameOrErr) {
6078bcb0991SDimitry Andric       consumeError(NameOrErr.takeError());
6080b57cec5SDimitry Andric       continue;
6098bcb0991SDimitry Andric     }
6108bcb0991SDimitry Andric     StringRef Name = *NameOrErr;
6118bcb0991SDimitry Andric 
6120b57cec5SDimitry Andric     if (Name == ".plt")
6130b57cec5SDimitry Andric       Plt = Section;
6140b57cec5SDimitry Andric     else if (Name == ".rela.plt" || Name == ".rel.plt")
6150b57cec5SDimitry Andric       RelaPlt = Section;
6160b57cec5SDimitry Andric     else if (Name == ".got.plt")
6170b57cec5SDimitry Andric       GotPlt = Section;
6180b57cec5SDimitry Andric   }
6190b57cec5SDimitry Andric   if (!Plt || !RelaPlt || !GotPlt)
6200b57cec5SDimitry Andric     return {};
6210b57cec5SDimitry Andric   Expected<StringRef> PltContents = Plt->getContents();
6220b57cec5SDimitry Andric   if (!PltContents) {
6230b57cec5SDimitry Andric     consumeError(PltContents.takeError());
6240b57cec5SDimitry Andric     return {};
6250b57cec5SDimitry Andric   }
6260b57cec5SDimitry Andric   auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
6270b57cec5SDimitry Andric                                         arrayRefFromStringRef(*PltContents),
6280b57cec5SDimitry Andric                                         GotPlt->getAddress(), Triple);
6290b57cec5SDimitry Andric   // Build a map from GOT entry virtual address to PLT entry virtual address.
6300b57cec5SDimitry Andric   DenseMap<uint64_t, uint64_t> GotToPlt;
6310b57cec5SDimitry Andric   for (const auto &Entry : PltEntries)
6320b57cec5SDimitry Andric     GotToPlt.insert(std::make_pair(Entry.second, Entry.first));
6330b57cec5SDimitry Andric   // Find the relocations in the dynamic relocation table that point to
6340b57cec5SDimitry Andric   // locations in the GOT for which we know the corresponding PLT entry.
635af732203SDimitry Andric   std::vector<std::pair<Optional<DataRefImpl>, uint64_t>> Result;
6360b57cec5SDimitry Andric   for (const auto &Relocation : RelaPlt->relocations()) {
6370b57cec5SDimitry Andric     if (Relocation.getType() != JumpSlotReloc)
6380b57cec5SDimitry Andric       continue;
6390b57cec5SDimitry Andric     auto PltEntryIter = GotToPlt.find(Relocation.getOffset());
640af732203SDimitry Andric     if (PltEntryIter != GotToPlt.end()) {
641af732203SDimitry Andric       symbol_iterator Sym = Relocation.getSymbol();
642af732203SDimitry Andric       if (Sym == symbol_end())
643af732203SDimitry Andric         Result.emplace_back(None, PltEntryIter->second);
644af732203SDimitry Andric       else
645af732203SDimitry Andric         Result.emplace_back(Sym->getRawDataRefImpl(), PltEntryIter->second);
646af732203SDimitry Andric     }
6470b57cec5SDimitry Andric   }
6480b57cec5SDimitry Andric   return Result;
6490b57cec5SDimitry Andric }
650