2017-04-25 07:21:38 +08:00
|
|
|
//===- ELFObjectFile.cpp - ELF object file implementation -----------------===//
|
2011-01-20 14:38:47 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2011-01-20 14:38:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-02-12 14:12:10 +08:00
|
|
|
// Part of the ELFObjectFile class implementation.
|
2011-01-20 14:38:47 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Object/ELFObjectFile.h"
|
2017-04-25 07:21:38 +08:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2017-06-07 11:48:56 +08:00
|
|
|
#include "llvm/BinaryFormat/ELF.h"
|
2018-08-24 23:21:56 +08:00
|
|
|
#include "llvm/MC/MCInstrAnalysis.h"
|
2017-04-25 07:21:38 +08:00
|
|
|
#include "llvm/MC/SubtargetFeature.h"
|
|
|
|
#include "llvm/Object/ELF.h"
|
|
|
|
#include "llvm/Object/ELFTypes.h"
|
|
|
|
#include "llvm/Object/Error.h"
|
2017-01-18 23:52:11 +08:00
|
|
|
#include "llvm/Support/ARMAttributeParser.h"
|
2017-06-06 19:49:48 +08:00
|
|
|
#include "llvm/Support/ARMBuildAttributes.h"
|
2017-04-25 07:21:38 +08:00
|
|
|
#include "llvm/Support/Endian.h"
|
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2013-01-05 04:36:28 +08:00
|
|
|
#include "llvm/Support/MathExtras.h"
|
2020-02-04 22:20:10 +08:00
|
|
|
#include "llvm/Support/RISCVAttributeParser.h"
|
|
|
|
#include "llvm/Support/RISCVAttributes.h"
|
2018-08-24 23:21:56 +08:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2017-04-25 07:21:38 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
#include <system_error>
|
|
|
|
#include <utility>
|
2011-01-20 14:38:47 +08:00
|
|
|
|
2017-04-25 07:21:38 +08:00
|
|
|
using namespace llvm;
|
2012-02-12 14:12:10 +08:00
|
|
|
using namespace object;
|
2011-09-09 04:52:17 +08:00
|
|
|
|
2019-03-09 06:00:50 +08:00
|
|
|
const EnumEntry<unsigned> llvm::object::ElfSymbolTypes[NumElfSymbolTypes] = {
|
|
|
|
{"None", "NOTYPE", ELF::STT_NOTYPE},
|
|
|
|
{"Object", "OBJECT", ELF::STT_OBJECT},
|
|
|
|
{"Function", "FUNC", ELF::STT_FUNC},
|
|
|
|
{"Section", "SECTION", ELF::STT_SECTION},
|
|
|
|
{"File", "FILE", ELF::STT_FILE},
|
|
|
|
{"Common", "COMMON", ELF::STT_COMMON},
|
|
|
|
{"TLS", "TLS", ELF::STT_TLS},
|
2019-08-10 00:54:51 +08:00
|
|
|
{"Unknown", "<unknown>: 7", 7},
|
|
|
|
{"Unknown", "<unknown>: 8", 8},
|
|
|
|
{"Unknown", "<unknown>: 9", 9},
|
|
|
|
{"GNU_IFunc", "IFUNC", ELF::STT_GNU_IFUNC},
|
|
|
|
{"OS Specific", "<OS specific>: 11", 11},
|
|
|
|
{"OS Specific", "<OS specific>: 12", 12},
|
|
|
|
{"Proc Specific", "<processor specific>: 13", 13},
|
|
|
|
{"Proc Specific", "<processor specific>: 14", 14},
|
|
|
|
{"Proc Specific", "<processor specific>: 15", 15}
|
|
|
|
};
|
2019-03-09 06:00:50 +08:00
|
|
|
|
2014-08-20 02:44:46 +08:00
|
|
|
ELFObjectFileBase::ELFObjectFileBase(unsigned int Type, MemoryBufferRef Source)
|
|
|
|
: ObjectFile(Type, Source) {}
|
2014-08-18 01:52:10 +08:00
|
|
|
|
2017-10-11 05:21:16 +08:00
|
|
|
template <class ELFT>
|
|
|
|
static Expected<std::unique_ptr<ELFObjectFile<ELFT>>>
|
2020-11-09 17:18:18 +08:00
|
|
|
createPtr(MemoryBufferRef Object, bool InitContent) {
|
|
|
|
auto Ret = ELFObjectFile<ELFT>::create(Object, InitContent);
|
2017-10-11 05:21:16 +08:00
|
|
|
if (Error E = Ret.takeError())
|
2020-02-10 23:06:45 +08:00
|
|
|
return std::move(E);
|
2019-08-15 23:54:37 +08:00
|
|
|
return std::make_unique<ELFObjectFile<ELFT>>(std::move(*Ret));
|
2017-10-11 05:21:16 +08:00
|
|
|
}
|
|
|
|
|
2017-10-11 04:00:07 +08:00
|
|
|
Expected<std::unique_ptr<ObjectFile>>
|
2020-11-09 17:18:18 +08:00
|
|
|
ObjectFile::createELFObjectFile(MemoryBufferRef Obj, bool InitContent) {
|
2014-07-05 19:38:52 +08:00
|
|
|
std::pair<unsigned char, unsigned char> Ident =
|
2014-08-20 02:44:46 +08:00
|
|
|
getElfArchType(Obj.getBuffer());
|
2013-01-05 04:36:28 +08:00
|
|
|
std::size_t MaxAlignment =
|
2020-03-23 10:20:04 +08:00
|
|
|
1ULL << countTrailingZeros(
|
|
|
|
reinterpret_cast<uintptr_t>(Obj.getBufferStart()));
|
2013-01-05 04:36:28 +08:00
|
|
|
|
2015-06-02 20:05:27 +08:00
|
|
|
if (MaxAlignment < 2)
|
2017-10-11 04:00:07 +08:00
|
|
|
return createError("Insufficient alignment");
|
2015-06-02 20:05:27 +08:00
|
|
|
|
|
|
|
if (Ident.first == ELF::ELFCLASS32) {
|
|
|
|
if (Ident.second == ELF::ELFDATA2LSB)
|
2020-11-09 17:18:18 +08:00
|
|
|
return createPtr<ELF32LE>(Obj, InitContent);
|
2015-06-02 20:05:27 +08:00
|
|
|
else if (Ident.second == ELF::ELFDATA2MSB)
|
2020-11-09 17:18:18 +08:00
|
|
|
return createPtr<ELF32BE>(Obj, InitContent);
|
2015-06-02 20:05:27 +08:00
|
|
|
else
|
2017-10-11 04:00:07 +08:00
|
|
|
return createError("Invalid ELF data");
|
2015-06-05 07:14:43 +08:00
|
|
|
} else if (Ident.first == ELF::ELFCLASS64) {
|
2015-06-02 20:05:27 +08:00
|
|
|
if (Ident.second == ELF::ELFDATA2LSB)
|
2020-11-09 17:18:18 +08:00
|
|
|
return createPtr<ELF64LE>(Obj, InitContent);
|
2015-06-02 20:05:27 +08:00
|
|
|
else if (Ident.second == ELF::ELFDATA2MSB)
|
2020-11-09 17:18:18 +08:00
|
|
|
return createPtr<ELF64BE>(Obj, InitContent);
|
2015-06-02 20:05:27 +08:00
|
|
|
else
|
2017-10-11 04:00:07 +08:00
|
|
|
return createError("Invalid ELF data");
|
2011-10-11 11:18:58 +08:00
|
|
|
}
|
2017-10-11 05:21:16 +08:00
|
|
|
return createError("Invalid ELF class");
|
2011-09-09 04:52:17 +08:00
|
|
|
}
|
|
|
|
|
2017-01-18 23:52:11 +08:00
|
|
|
SubtargetFeatures ELFObjectFileBase::getMIPSFeatures() const {
|
|
|
|
SubtargetFeatures Features;
|
2018-01-30 02:27:30 +08:00
|
|
|
unsigned PlatformFlags = getPlatformFlags();
|
2017-01-18 23:52:11 +08:00
|
|
|
|
|
|
|
switch (PlatformFlags & ELF::EF_MIPS_ARCH) {
|
|
|
|
case ELF::EF_MIPS_ARCH_1:
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_2:
|
|
|
|
Features.AddFeature("mips2");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_3:
|
|
|
|
Features.AddFeature("mips3");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_4:
|
|
|
|
Features.AddFeature("mips4");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_5:
|
|
|
|
Features.AddFeature("mips5");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_32:
|
|
|
|
Features.AddFeature("mips32");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_64:
|
|
|
|
Features.AddFeature("mips64");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_32R2:
|
|
|
|
Features.AddFeature("mips32r2");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_64R2:
|
|
|
|
Features.AddFeature("mips64r2");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_32R6:
|
|
|
|
Features.AddFeature("mips32r6");
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_ARCH_64R6:
|
|
|
|
Features.AddFeature("mips64r6");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown EF_MIPS_ARCH value");
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (PlatformFlags & ELF::EF_MIPS_MACH) {
|
|
|
|
case ELF::EF_MIPS_MACH_NONE:
|
|
|
|
// No feature associated with this value.
|
|
|
|
break;
|
|
|
|
case ELF::EF_MIPS_MACH_OCTEON:
|
|
|
|
Features.AddFeature("cnmips");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown EF_MIPS_ARCH value");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PlatformFlags & ELF::EF_MIPS_ARCH_ASE_M16)
|
|
|
|
Features.AddFeature("mips16");
|
|
|
|
if (PlatformFlags & ELF::EF_MIPS_MICROMIPS)
|
|
|
|
Features.AddFeature("micromips");
|
|
|
|
|
|
|
|
return Features;
|
|
|
|
}
|
2016-06-16 17:17:03 +08:00
|
|
|
|
2017-01-18 23:52:11 +08:00
|
|
|
SubtargetFeatures ELFObjectFileBase::getARMFeatures() const {
|
|
|
|
SubtargetFeatures Features;
|
|
|
|
ARMAttributeParser Attributes;
|
2020-02-22 03:32:33 +08:00
|
|
|
if (Error E = getBuildAttributes(Attributes)) {
|
|
|
|
consumeError(std::move(E));
|
2017-01-18 23:52:11 +08:00
|
|
|
return SubtargetFeatures();
|
2020-02-22 03:32:33 +08:00
|
|
|
}
|
2017-01-18 23:52:11 +08:00
|
|
|
|
|
|
|
// both ARMv7-M and R have to support thumb hardware div
|
|
|
|
bool isV7 = false;
|
2020-02-04 22:20:10 +08:00
|
|
|
Optional<unsigned> Attr =
|
|
|
|
Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
|
|
|
|
if (Attr.hasValue())
|
|
|
|
isV7 = Attr.getValue() == ARMBuildAttrs::v7;
|
2017-01-18 23:52:11 +08:00
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
Attr = Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch_profile);
|
|
|
|
if (Attr.hasValue()) {
|
|
|
|
switch (Attr.getValue()) {
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::ApplicationProfile:
|
|
|
|
Features.AddFeature("aclass");
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::RealTimeProfile:
|
|
|
|
Features.AddFeature("rclass");
|
|
|
|
if (isV7)
|
|
|
|
Features.AddFeature("hwdiv");
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::MicroControllerProfile:
|
|
|
|
Features.AddFeature("mclass");
|
|
|
|
if (isV7)
|
|
|
|
Features.AddFeature("hwdiv");
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
Attr = Attributes.getAttributeValue(ARMBuildAttrs::THUMB_ISA_use);
|
|
|
|
if (Attr.hasValue()) {
|
|
|
|
switch (Attr.getValue()) {
|
2017-01-18 23:52:11 +08:00
|
|
|
default:
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::Not_Allowed:
|
|
|
|
Features.AddFeature("thumb", false);
|
|
|
|
Features.AddFeature("thumb2", false);
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::AllowThumb32:
|
|
|
|
Features.AddFeature("thumb2");
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
Attr = Attributes.getAttributeValue(ARMBuildAttrs::FP_arch);
|
|
|
|
if (Attr.hasValue()) {
|
|
|
|
switch (Attr.getValue()) {
|
2017-01-18 23:52:11 +08:00
|
|
|
default:
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::Not_Allowed:
|
2019-09-18 05:42:38 +08:00
|
|
|
Features.AddFeature("vfp2sp", false);
|
[ARM] Replace fp-only-sp and d16 with fp64 and d32.
Those two subtarget features were awkward because their semantics are
reversed: each one indicates the _lack_ of support for something in
the architecture, rather than the presence. As a consequence, you
don't get the behavior you want if you combine two sets of feature
bits.
Each SubtargetFeature for an FP architecture version now comes in four
versions, one for each combination of those options. So you can still
say (for example) '+vfp2' in a feature string and it will mean what
it's always meant, but there's a new string '+vfp2d16sp' meaning the
version without those extra options.
A lot of this change is just mechanically replacing positive checks
for the old features with negative checks for the new ones. But one
more interesting change is that I've rearranged getFPUFeatures() so
that the main FPU feature is appended to the output list *before*
rather than after the features derived from the Restriction field, so
that -fp64 and -d32 can override defaults added by the main feature.
Reviewers: dmgreen, samparker, SjoerdMeijer
Subscribers: srhines, javed.absar, eraman, kristof.beyls, hiraditya, zzheng, Petar.Avramovic, cfe-commits, llvm-commits
Tags: #clang, #llvm
Differential Revision: https://reviews.llvm.org/D60691
llvm-svn: 361845
2019-05-29 00:13:20 +08:00
|
|
|
Features.AddFeature("vfp3d16sp", false);
|
|
|
|
Features.AddFeature("vfp4d16sp", false);
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::AllowFPv2:
|
|
|
|
Features.AddFeature("vfp2");
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::AllowFPv3A:
|
|
|
|
case ARMBuildAttrs::AllowFPv3B:
|
|
|
|
Features.AddFeature("vfp3");
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::AllowFPv4A:
|
|
|
|
case ARMBuildAttrs::AllowFPv4B:
|
|
|
|
Features.AddFeature("vfp4");
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
|
|
|
}
|
2017-01-18 23:52:11 +08:00
|
|
|
}
|
2016-06-16 17:17:03 +08:00
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
Attr = Attributes.getAttributeValue(ARMBuildAttrs::Advanced_SIMD_arch);
|
|
|
|
if (Attr.hasValue()) {
|
|
|
|
switch (Attr.getValue()) {
|
2017-01-18 23:52:11 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::Not_Allowed:
|
|
|
|
Features.AddFeature("neon", false);
|
|
|
|
Features.AddFeature("fp16", false);
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::AllowNeon:
|
|
|
|
Features.AddFeature("neon");
|
2016-06-16 17:17:03 +08:00
|
|
|
break;
|
2017-01-18 23:52:11 +08:00
|
|
|
case ARMBuildAttrs::AllowNeon2:
|
|
|
|
Features.AddFeature("neon");
|
|
|
|
Features.AddFeature("fp16");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
Attr = Attributes.getAttributeValue(ARMBuildAttrs::MVE_arch);
|
|
|
|
if (Attr.hasValue()) {
|
|
|
|
switch (Attr.getValue()) {
|
2019-05-30 20:57:04 +08:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::Not_Allowed:
|
|
|
|
Features.AddFeature("mve", false);
|
|
|
|
Features.AddFeature("mve.fp", false);
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::AllowMVEInteger:
|
|
|
|
Features.AddFeature("mve.fp", false);
|
|
|
|
Features.AddFeature("mve");
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::AllowMVEIntegerAndFloat:
|
|
|
|
Features.AddFeature("mve.fp");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
Attr = Attributes.getAttributeValue(ARMBuildAttrs::DIV_use);
|
|
|
|
if (Attr.hasValue()) {
|
|
|
|
switch (Attr.getValue()) {
|
2016-06-16 17:17:03 +08:00
|
|
|
default:
|
2017-01-18 23:52:11 +08:00
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::DisallowDIV:
|
|
|
|
Features.AddFeature("hwdiv", false);
|
|
|
|
Features.AddFeature("hwdiv-arm", false);
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::AllowDIVExt:
|
|
|
|
Features.AddFeature("hwdiv");
|
|
|
|
Features.AddFeature("hwdiv-arm");
|
|
|
|
break;
|
2016-06-16 17:17:03 +08:00
|
|
|
}
|
2017-01-18 23:52:11 +08:00
|
|
|
}
|
2016-06-16 17:17:03 +08:00
|
|
|
|
2017-01-18 23:52:11 +08:00
|
|
|
return Features;
|
|
|
|
}
|
2016-06-16 17:17:03 +08:00
|
|
|
|
2018-02-02 14:01:02 +08:00
|
|
|
SubtargetFeatures ELFObjectFileBase::getRISCVFeatures() const {
|
|
|
|
SubtargetFeatures Features;
|
|
|
|
unsigned PlatformFlags = getPlatformFlags();
|
|
|
|
|
|
|
|
if (PlatformFlags & ELF::EF_RISCV_RVC) {
|
|
|
|
Features.AddFeature("c");
|
|
|
|
}
|
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
// Add features according to the ELF attribute section.
|
|
|
|
// If there are any unrecognized features, ignore them.
|
|
|
|
RISCVAttributeParser Attributes;
|
[RISCV] Consume error from parsing attributes section
Summary:
We don't consume the error from getBuildAttributes, so an assertions
build crashes with "Program aborted due to an unhandled Error:".
Explicitly consume it like the ARM version in that case.
Reviewers: asb, jhenderson, MaskRay, HsiangKai
Reviewed By: MaskRay
Subscribers: kristof.beyls, hiraditya, simoncook, kito-cheng, shiva0217, rogfer01, rkruppe, psnobl, benna, Jim, lenary, s.egerton, sameer.abuasal, luismarques, evandro, danielkiss, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77841
2020-04-10 08:30:48 +08:00
|
|
|
if (Error E = getBuildAttributes(Attributes)) {
|
|
|
|
// TODO Propagate Error.
|
|
|
|
consumeError(std::move(E));
|
2020-02-04 22:20:10 +08:00
|
|
|
return Features; // Keep "c" feature if there is one in PlatformFlags.
|
[RISCV] Consume error from parsing attributes section
Summary:
We don't consume the error from getBuildAttributes, so an assertions
build crashes with "Program aborted due to an unhandled Error:".
Explicitly consume it like the ARM version in that case.
Reviewers: asb, jhenderson, MaskRay, HsiangKai
Reviewed By: MaskRay
Subscribers: kristof.beyls, hiraditya, simoncook, kito-cheng, shiva0217, rogfer01, rkruppe, psnobl, benna, Jim, lenary, s.egerton, sameer.abuasal, luismarques, evandro, danielkiss, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D77841
2020-04-10 08:30:48 +08:00
|
|
|
}
|
2020-02-04 22:20:10 +08:00
|
|
|
|
|
|
|
Optional<StringRef> Attr = Attributes.getAttributeString(RISCVAttrs::ARCH);
|
|
|
|
if (Attr.hasValue()) {
|
|
|
|
// The Arch pattern is [rv32|rv64][i|e]version(_[m|a|f|d|c]version)*
|
|
|
|
// Version string pattern is (major)p(minor). Major and minor are optional.
|
|
|
|
// For example, a version number could be 2p0, 2, or p92.
|
|
|
|
StringRef Arch = Attr.getValue();
|
|
|
|
if (Arch.consume_front("rv32"))
|
|
|
|
Features.AddFeature("64bit", false);
|
|
|
|
else if (Arch.consume_front("rv64"))
|
|
|
|
Features.AddFeature("64bit");
|
|
|
|
|
|
|
|
while (!Arch.empty()) {
|
|
|
|
switch (Arch[0]) {
|
|
|
|
default:
|
|
|
|
break; // Ignore unexpected features.
|
|
|
|
case 'i':
|
|
|
|
Features.AddFeature("e", false);
|
|
|
|
break;
|
|
|
|
case 'd':
|
|
|
|
Features.AddFeature("f"); // D-ext will imply F-ext.
|
|
|
|
LLVM_FALLTHROUGH;
|
|
|
|
case 'e':
|
|
|
|
case 'm':
|
|
|
|
case 'a':
|
|
|
|
case 'f':
|
|
|
|
case 'c':
|
|
|
|
Features.AddFeature(Arch.take_front());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Handle version numbers.
|
|
|
|
Arch = Arch.drop_until([](char c) { return c == '_' || c == '\0'; });
|
|
|
|
Arch = Arch.drop_while([](char c) { return c == '_'; });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-02 14:01:02 +08:00
|
|
|
return Features;
|
|
|
|
}
|
|
|
|
|
2017-01-18 23:52:11 +08:00
|
|
|
SubtargetFeatures ELFObjectFileBase::getFeatures() const {
|
|
|
|
switch (getEMachine()) {
|
|
|
|
case ELF::EM_MIPS:
|
|
|
|
return getMIPSFeatures();
|
|
|
|
case ELF::EM_ARM:
|
|
|
|
return getARMFeatures();
|
2018-02-02 14:01:02 +08:00
|
|
|
case ELF::EM_RISCV:
|
|
|
|
return getRISCVFeatures();
|
2016-06-16 17:17:03 +08:00
|
|
|
default:
|
|
|
|
return SubtargetFeatures();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 17:51:46 +08:00
|
|
|
Optional<StringRef> ELFObjectFileBase::tryGetCPUName() const {
|
|
|
|
switch (getEMachine()) {
|
|
|
|
case ELF::EM_AMDGPU:
|
|
|
|
return getAMDGPUCPUName();
|
|
|
|
default:
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
StringRef ELFObjectFileBase::getAMDGPUCPUName() const {
|
|
|
|
assert(getEMachine() == ELF::EM_AMDGPU);
|
|
|
|
unsigned CPU = getPlatformFlags() & ELF::EF_AMDGPU_MACH;
|
|
|
|
|
|
|
|
switch (CPU) {
|
|
|
|
// Radeon HD 2000/3000 Series (R600).
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_R600:
|
|
|
|
return "r600";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_R630:
|
|
|
|
return "r630";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_RS880:
|
|
|
|
return "rs880";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_RV670:
|
|
|
|
return "rv670";
|
|
|
|
|
|
|
|
// Radeon HD 4000 Series (R700).
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_RV710:
|
|
|
|
return "rv710";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_RV730:
|
|
|
|
return "rv730";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_RV770:
|
|
|
|
return "rv770";
|
|
|
|
|
|
|
|
// Radeon HD 5000 Series (Evergreen).
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_CEDAR:
|
|
|
|
return "cedar";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_CYPRESS:
|
|
|
|
return "cypress";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_JUNIPER:
|
|
|
|
return "juniper";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_REDWOOD:
|
|
|
|
return "redwood";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_SUMO:
|
|
|
|
return "sumo";
|
|
|
|
|
|
|
|
// Radeon HD 6000 Series (Northern Islands).
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_BARTS:
|
|
|
|
return "barts";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_CAICOS:
|
|
|
|
return "caicos";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_CAYMAN:
|
|
|
|
return "cayman";
|
|
|
|
case ELF::EF_AMDGPU_MACH_R600_TURKS:
|
|
|
|
return "turks";
|
|
|
|
|
|
|
|
// AMDGCN GFX6.
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX600:
|
|
|
|
return "gfx600";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX601:
|
|
|
|
return "gfx601";
|
2020-10-07 01:23:59 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX602:
|
|
|
|
return "gfx602";
|
2020-07-24 17:51:46 +08:00
|
|
|
|
|
|
|
// AMDGCN GFX7.
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX700:
|
|
|
|
return "gfx700";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX701:
|
|
|
|
return "gfx701";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX702:
|
|
|
|
return "gfx702";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX703:
|
|
|
|
return "gfx703";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX704:
|
|
|
|
return "gfx704";
|
2020-10-07 01:23:59 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX705:
|
|
|
|
return "gfx705";
|
2020-07-24 17:51:46 +08:00
|
|
|
|
|
|
|
// AMDGCN GFX8.
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX801:
|
|
|
|
return "gfx801";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX802:
|
|
|
|
return "gfx802";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX803:
|
|
|
|
return "gfx803";
|
2020-10-07 01:23:59 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX805:
|
|
|
|
return "gfx805";
|
2020-07-24 17:51:46 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX810:
|
|
|
|
return "gfx810";
|
|
|
|
|
|
|
|
// AMDGCN GFX9.
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX900:
|
|
|
|
return "gfx900";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX902:
|
|
|
|
return "gfx902";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX904:
|
|
|
|
return "gfx904";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX906:
|
|
|
|
return "gfx906";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX908:
|
|
|
|
return "gfx908";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX909:
|
|
|
|
return "gfx909";
|
2021-02-18 05:37:46 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90A:
|
|
|
|
return "gfx90a";
|
2020-10-07 01:23:59 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX90C:
|
|
|
|
return "gfx90c";
|
2020-07-24 17:51:46 +08:00
|
|
|
|
|
|
|
// AMDGCN GFX10.
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1010:
|
|
|
|
return "gfx1010";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1011:
|
|
|
|
return "gfx1011";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1012:
|
|
|
|
return "gfx1012";
|
2021-06-09 04:54:42 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1013:
|
|
|
|
return "gfx1013";
|
2020-07-24 17:51:46 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1030:
|
|
|
|
return "gfx1030";
|
2020-10-26 09:16:59 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1031:
|
|
|
|
return "gfx1031";
|
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1032:
|
|
|
|
return "gfx1032";
|
2020-10-30 16:21:12 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1033:
|
|
|
|
return "gfx1033";
|
2021-05-14 02:21:40 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1034:
|
|
|
|
return "gfx1034";
|
2021-06-25 02:32:41 +08:00
|
|
|
case ELF::EF_AMDGPU_MACH_AMDGCN_GFX1035:
|
|
|
|
return "gfx1035";
|
2020-07-24 17:51:46 +08:00
|
|
|
default:
|
|
|
|
llvm_unreachable("Unknown EF_AMDGPU_MACH value");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-18 21:52:12 +08:00
|
|
|
// FIXME Encode from a tablegen description or target parser.
|
|
|
|
void ELFObjectFileBase::setARMSubArch(Triple &TheTriple) const {
|
|
|
|
if (TheTriple.getSubArch() != Triple::NoSubArch)
|
|
|
|
return;
|
|
|
|
|
|
|
|
ARMAttributeParser Attributes;
|
2020-02-22 03:32:33 +08:00
|
|
|
if (Error E = getBuildAttributes(Attributes)) {
|
|
|
|
// TODO Propagate Error.
|
|
|
|
consumeError(std::move(E));
|
2017-01-18 21:52:12 +08:00
|
|
|
return;
|
2020-02-22 03:32:33 +08:00
|
|
|
}
|
2017-01-18 21:52:12 +08:00
|
|
|
|
|
|
|
std::string Triple;
|
|
|
|
// Default to ARM, but use the triple if it's been set.
|
2017-08-13 01:40:18 +08:00
|
|
|
if (TheTriple.isThumb())
|
2017-01-18 21:52:12 +08:00
|
|
|
Triple = "thumb";
|
|
|
|
else
|
|
|
|
Triple = "arm";
|
|
|
|
|
2020-02-04 22:20:10 +08:00
|
|
|
Optional<unsigned> Attr =
|
|
|
|
Attributes.getAttributeValue(ARMBuildAttrs::CPU_arch);
|
|
|
|
if (Attr.hasValue()) {
|
|
|
|
switch (Attr.getValue()) {
|
2017-01-18 21:52:12 +08:00
|
|
|
case ARMBuildAttrs::v4:
|
|
|
|
Triple += "v4";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v4T:
|
|
|
|
Triple += "v4t";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v5T:
|
|
|
|
Triple += "v5t";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v5TE:
|
|
|
|
Triple += "v5te";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v5TEJ:
|
|
|
|
Triple += "v5tej";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v6:
|
|
|
|
Triple += "v6";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v6KZ:
|
|
|
|
Triple += "v6kz";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v6T2:
|
|
|
|
Triple += "v6t2";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v6K:
|
|
|
|
Triple += "v6k";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v7:
|
|
|
|
Triple += "v7";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v6_M:
|
|
|
|
Triple += "v6m";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v6S_M:
|
|
|
|
Triple += "v6sm";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v7E_M:
|
|
|
|
Triple += "v7em";
|
|
|
|
break;
|
2019-08-28 14:37:22 +08:00
|
|
|
case ARMBuildAttrs::v8_A:
|
|
|
|
Triple += "v8a";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v8_R:
|
|
|
|
Triple += "v8r";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v8_M_Base:
|
|
|
|
Triple += "v8m.base";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v8_M_Main:
|
|
|
|
Triple += "v8m.main";
|
|
|
|
break;
|
|
|
|
case ARMBuildAttrs::v8_1_M_Main:
|
|
|
|
Triple += "v8.1m.main";
|
|
|
|
break;
|
2017-01-18 21:52:12 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!isLittleEndian())
|
|
|
|
Triple += "eb";
|
|
|
|
|
|
|
|
TheTriple.setArchName(Triple);
|
|
|
|
}
|
2018-08-24 23:21:56 +08:00
|
|
|
|
2020-08-13 23:13:26 +08:00
|
|
|
std::vector<std::pair<Optional<DataRefImpl>, uint64_t>>
|
2018-08-24 23:21:56 +08:00
|
|
|
ELFObjectFileBase::getPltAddresses() const {
|
|
|
|
std::string Err;
|
|
|
|
const auto Triple = makeTriple();
|
|
|
|
const auto *T = TargetRegistry::lookupTarget(Triple.str(), Err);
|
|
|
|
if (!T)
|
|
|
|
return {};
|
|
|
|
uint64_t JumpSlotReloc = 0;
|
|
|
|
switch (Triple.getArch()) {
|
|
|
|
case Triple::x86:
|
|
|
|
JumpSlotReloc = ELF::R_386_JUMP_SLOT;
|
|
|
|
break;
|
|
|
|
case Triple::x86_64:
|
|
|
|
JumpSlotReloc = ELF::R_X86_64_JUMP_SLOT;
|
|
|
|
break;
|
|
|
|
case Triple::aarch64:
|
2021-02-09 00:50:25 +08:00
|
|
|
case Triple::aarch64_be:
|
2018-08-24 23:21:56 +08:00
|
|
|
JumpSlotReloc = ELF::R_AARCH64_JUMP_SLOT;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return {};
|
|
|
|
}
|
2018-08-25 05:03:35 +08:00
|
|
|
std::unique_ptr<const MCInstrInfo> MII(T->createMCInstrInfo());
|
2018-08-25 03:40:35 +08:00
|
|
|
std::unique_ptr<const MCInstrAnalysis> MIA(
|
2018-08-25 05:03:35 +08:00
|
|
|
T->createMCInstrAnalysis(MII.get()));
|
2018-08-24 23:21:56 +08:00
|
|
|
if (!MIA)
|
|
|
|
return {};
|
|
|
|
Optional<SectionRef> Plt = None, RelaPlt = None, GotPlt = None;
|
|
|
|
for (const SectionRef &Section : sections()) {
|
2019-08-14 19:10:11 +08:00
|
|
|
Expected<StringRef> NameOrErr = Section.getName();
|
|
|
|
if (!NameOrErr) {
|
|
|
|
consumeError(NameOrErr.takeError());
|
2018-08-24 23:21:56 +08:00
|
|
|
continue;
|
2019-08-14 19:10:11 +08:00
|
|
|
}
|
|
|
|
StringRef Name = *NameOrErr;
|
|
|
|
|
2018-08-24 23:21:56 +08:00
|
|
|
if (Name == ".plt")
|
|
|
|
Plt = Section;
|
|
|
|
else if (Name == ".rela.plt" || Name == ".rel.plt")
|
|
|
|
RelaPlt = Section;
|
|
|
|
else if (Name == ".got.plt")
|
|
|
|
GotPlt = Section;
|
|
|
|
}
|
|
|
|
if (!Plt || !RelaPlt || !GotPlt)
|
|
|
|
return {};
|
2019-05-16 21:24:04 +08:00
|
|
|
Expected<StringRef> PltContents = Plt->getContents();
|
|
|
|
if (!PltContents) {
|
|
|
|
consumeError(PltContents.takeError());
|
2018-08-24 23:21:56 +08:00
|
|
|
return {};
|
2019-05-16 21:24:04 +08:00
|
|
|
}
|
|
|
|
auto PltEntries = MIA->findPltEntries(Plt->getAddress(),
|
|
|
|
arrayRefFromStringRef(*PltContents),
|
2018-08-24 23:21:56 +08:00
|
|
|
GotPlt->getAddress(), Triple);
|
|
|
|
// Build a map from GOT entry virtual address to PLT entry virtual address.
|
|
|
|
DenseMap<uint64_t, uint64_t> GotToPlt;
|
|
|
|
for (const auto &Entry : PltEntries)
|
|
|
|
GotToPlt.insert(std::make_pair(Entry.second, Entry.first));
|
|
|
|
// Find the relocations in the dynamic relocation table that point to
|
|
|
|
// locations in the GOT for which we know the corresponding PLT entry.
|
2020-08-13 23:13:26 +08:00
|
|
|
std::vector<std::pair<Optional<DataRefImpl>, uint64_t>> Result;
|
2018-08-24 23:21:56 +08:00
|
|
|
for (const auto &Relocation : RelaPlt->relocations()) {
|
|
|
|
if (Relocation.getType() != JumpSlotReloc)
|
|
|
|
continue;
|
|
|
|
auto PltEntryIter = GotToPlt.find(Relocation.getOffset());
|
2020-08-13 23:13:26 +08:00
|
|
|
if (PltEntryIter != GotToPlt.end()) {
|
|
|
|
symbol_iterator Sym = Relocation.getSymbol();
|
|
|
|
if (Sym == symbol_end())
|
|
|
|
Result.emplace_back(None, PltEntryIter->second);
|
|
|
|
else
|
|
|
|
Result.emplace_back(Sym->getRawDataRefImpl(), PltEntryIter->second);
|
|
|
|
}
|
2018-08-24 23:21:56 +08:00
|
|
|
}
|
|
|
|
return Result;
|
|
|
|
}
|