2012-02-18 12:03:15 +00:00
|
|
|
//===-- ARMMCTargetDesc.cpp - ARM Target Descriptions ---------------------===//
|
2011-07-06 22:02:34 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file provides ARM specific target descriptions.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2013-01-02 10:22:59 +00:00
|
|
|
#include "ARMBaseInfo.h"
|
2012-12-07 16:50:23 +00:00
|
|
|
#include "ARMMCAsmInfo.h"
|
2013-01-30 16:30:19 +00:00
|
|
|
#include "ARMMCTargetDesc.h"
|
2011-07-25 21:20:24 +00:00
|
|
|
#include "InstPrinter/ARMInstPrinter.h"
|
2013-01-30 16:30:19 +00:00
|
|
|
#include "llvm/ADT/Triple.h"
|
2011-08-23 20:15:21 +00:00
|
|
|
#include "llvm/MC/MCCodeGenInfo.h"
|
2013-10-05 16:42:21 +00:00
|
|
|
#include "llvm/MC/MCELFStreamer.h"
|
2011-08-23 20:15:21 +00:00
|
|
|
#include "llvm/MC/MCInstrAnalysis.h"
|
2011-07-06 22:02:34 +00:00
|
|
|
#include "llvm/MC/MCInstrInfo.h"
|
|
|
|
#include "llvm/MC/MCRegisterInfo.h"
|
2014-04-27 03:48:22 +00:00
|
|
|
#include "llvm/MC/MCStreamer.h"
|
2011-07-06 22:02:34 +00:00
|
|
|
#include "llvm/MC/MCSubtargetInfo.h"
|
2011-07-23 00:00:19 +00:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2011-08-24 18:08:43 +00:00
|
|
|
#include "llvm/Support/TargetRegistry.h"
|
2011-07-06 22:02:34 +00:00
|
|
|
|
2013-09-12 10:28:05 +00:00
|
|
|
using namespace llvm;
|
|
|
|
|
2011-07-06 22:02:34 +00:00
|
|
|
#define GET_REGINFO_MC_DESC
|
|
|
|
#include "ARMGenRegisterInfo.inc"
|
|
|
|
|
2013-09-12 10:28:05 +00:00
|
|
|
static bool getMCRDeprecationInfo(MCInst &MI, MCSubtargetInfo &STI,
|
|
|
|
std::string &Info) {
|
2013-09-17 09:54:57 +00:00
|
|
|
if (STI.getFeatureBits() & llvm::ARM::HasV7Ops &&
|
|
|
|
(MI.getOperand(0).isImm() && MI.getOperand(0).getImm() == 15) &&
|
2013-09-12 10:28:05 +00:00
|
|
|
(MI.getOperand(1).isImm() && MI.getOperand(1).getImm() == 0) &&
|
2013-09-17 09:54:57 +00:00
|
|
|
// Checks for the deprecated CP15ISB encoding:
|
|
|
|
// mcr p15, #0, rX, c7, c5, #4
|
|
|
|
(MI.getOperand(3).isImm() && MI.getOperand(3).getImm() == 7)) {
|
|
|
|
if ((MI.getOperand(5).isImm() && MI.getOperand(5).getImm() == 4)) {
|
|
|
|
if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 5) {
|
|
|
|
Info = "deprecated since v7, use 'isb'";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Checks for the deprecated CP15DSB encoding:
|
|
|
|
// mcr p15, #0, rX, c7, c10, #4
|
|
|
|
if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 10) {
|
|
|
|
Info = "deprecated since v7, use 'dsb'";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Checks for the deprecated CP15DMB encoding:
|
|
|
|
// mcr p15, #0, rX, c7, c10, #5
|
|
|
|
if (MI.getOperand(4).isImm() && MI.getOperand(4).getImm() == 10 &&
|
|
|
|
(MI.getOperand(5).isImm() && MI.getOperand(5).getImm() == 5)) {
|
|
|
|
Info = "deprecated since v7, use 'dmb'";
|
|
|
|
return true;
|
|
|
|
}
|
2013-09-12 10:28:05 +00:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-10-03 09:31:51 +00:00
|
|
|
static bool getITDeprecationInfo(MCInst &MI, MCSubtargetInfo &STI,
|
|
|
|
std::string &Info) {
|
|
|
|
if (STI.getFeatureBits() & llvm::ARM::HasV8Ops &&
|
|
|
|
MI.getOperand(1).isImm() && MI.getOperand(1).getImm() != 8) {
|
|
|
|
Info = "applying IT instruction to more than one subsequent instruction is deprecated";
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2011-07-06 22:02:34 +00:00
|
|
|
#define GET_INSTRINFO_MC_DESC
|
|
|
|
#include "ARMGenInstrInfo.inc"
|
|
|
|
|
|
|
|
#define GET_SUBTARGETINFO_MC_DESC
|
|
|
|
#include "ARMGenSubtargetInfo.inc"
|
|
|
|
|
|
|
|
|
2012-04-26 01:13:36 +00:00
|
|
|
std::string ARM_MC::ParseARMTriple(StringRef TT, StringRef CPU) {
|
2013-01-30 16:30:19 +00:00
|
|
|
Triple triple(TT);
|
|
|
|
|
2011-07-07 00:08:19 +00:00
|
|
|
// Set the boolean corresponding to the current target triple, or the default
|
|
|
|
// if one cannot be determined, to true.
|
|
|
|
unsigned Len = TT.size();
|
|
|
|
unsigned Idx = 0;
|
|
|
|
|
2011-09-05 18:35:03 +00:00
|
|
|
// FIXME: Enhance Triple helper class to extract ARM version.
|
2014-03-28 14:35:30 +00:00
|
|
|
bool isThumb = triple.getArch() == Triple::thumb ||
|
|
|
|
triple.getArch() == Triple::thumbeb;
|
2011-07-07 00:08:19 +00:00
|
|
|
if (Len >= 5 && TT.substr(0, 4) == "armv")
|
|
|
|
Idx = 4;
|
2014-03-28 14:35:30 +00:00
|
|
|
else if (Len >= 7 && TT.substr(0, 6) == "armebv")
|
|
|
|
Idx = 6;
|
2013-12-18 21:29:44 +00:00
|
|
|
else if (Len >= 7 && TT.substr(0, 6) == "thumbv")
|
|
|
|
Idx = 6;
|
2014-03-28 14:35:30 +00:00
|
|
|
else if (Len >= 9 && TT.substr(0, 8) == "thumbebv")
|
|
|
|
Idx = 8;
|
2011-07-07 00:08:19 +00:00
|
|
|
|
2012-04-27 01:27:19 +00:00
|
|
|
bool NoCPU = CPU == "generic" || CPU.empty();
|
2011-07-07 00:08:19 +00:00
|
|
|
std::string ARMArchFeature;
|
|
|
|
if (Idx) {
|
|
|
|
unsigned SubVer = TT[Idx];
|
2013-06-26 16:58:26 +00:00
|
|
|
if (SubVer == '8') {
|
2013-10-14 13:16:57 +00:00
|
|
|
if (NoCPU)
|
2014-04-09 06:18:26 +00:00
|
|
|
// v8a: FeatureDB, FeatureFPARMv8, FeatureNEON, FeatureDSPThumb2,
|
|
|
|
// FeatureMP, FeatureHWDiv, FeatureHWDivARM, FeatureTrustZone,
|
|
|
|
// FeatureT2XtPk, FeatureCrypto, FeatureCRC
|
|
|
|
ARMArchFeature = "+v8,+db,+fp-armv8,+neon,+t2dsp,+mp,+hwdiv,+hwdiv-arm,"
|
|
|
|
"+trustzone,+t2xtpk,+crypto,+crc";
|
2013-10-14 13:16:57 +00:00
|
|
|
else
|
|
|
|
// Use CPU to figure out the exact features
|
|
|
|
ARMArchFeature = "+v8";
|
2013-06-26 16:58:26 +00:00
|
|
|
} else if (SubVer == '7') {
|
2011-07-07 00:08:19 +00:00
|
|
|
if (Len >= Idx+2 && TT[Idx+1] == 'm') {
|
2013-06-10 23:20:58 +00:00
|
|
|
isThumb = true;
|
2012-04-27 01:27:19 +00:00
|
|
|
if (NoCPU)
|
|
|
|
// v7m: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureMClass
|
|
|
|
ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+mclass";
|
|
|
|
else
|
|
|
|
// Use CPU to figure out the exact features.
|
|
|
|
ARMArchFeature = "+v7";
|
2011-07-07 00:08:19 +00:00
|
|
|
} else if (Len >= Idx+3 && TT[Idx+1] == 'e'&& TT[Idx+2] == 'm') {
|
2012-04-27 01:27:19 +00:00
|
|
|
if (NoCPU)
|
|
|
|
// v7em: FeatureNoARM, FeatureDB, FeatureHWDiv, FeatureDSPThumb2,
|
|
|
|
// FeatureT2XtPk, FeatureMClass
|
|
|
|
ARMArchFeature = "+v7,+noarm,+db,+hwdiv,+t2dsp,t2xtpk,+mclass";
|
|
|
|
else
|
|
|
|
// Use CPU to figure out the exact features.
|
|
|
|
ARMArchFeature = "+v7";
|
2012-09-29 21:43:49 +00:00
|
|
|
} else if (Len >= Idx+2 && TT[Idx+1] == 's') {
|
|
|
|
if (NoCPU)
|
2014-01-09 20:24:00 +00:00
|
|
|
// v7s: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureHasRAS
|
2012-09-29 21:43:49 +00:00
|
|
|
// Swift
|
2014-01-09 20:24:00 +00:00
|
|
|
ARMArchFeature = "+v7,+swift,+neon,+db,+t2dsp,+ras";
|
2012-09-29 21:43:49 +00:00
|
|
|
else
|
|
|
|
// Use CPU to figure out the exact features.
|
|
|
|
ARMArchFeature = "+v7";
|
2012-04-26 01:13:36 +00:00
|
|
|
} else {
|
|
|
|
// v7 CPUs have lots of different feature sets. If no CPU is specified,
|
|
|
|
// then assume v7a (e.g. cortex-a8) feature set. Otherwise, return
|
|
|
|
// the "minimum" feature set and use CPU string to figure out the exact
|
|
|
|
// features.
|
2012-04-27 01:27:19 +00:00
|
|
|
if (NoCPU)
|
2012-04-26 01:13:36 +00:00
|
|
|
// v7a: FeatureNEON, FeatureDB, FeatureDSPThumb2, FeatureT2XtPk
|
|
|
|
ARMArchFeature = "+v7,+neon,+db,+t2dsp,+t2xtpk";
|
|
|
|
else
|
|
|
|
// Use CPU to figure out the exact features.
|
|
|
|
ARMArchFeature = "+v7";
|
|
|
|
}
|
2011-07-07 00:08:19 +00:00
|
|
|
} else if (SubVer == '6') {
|
2012-02-10 20:38:46 +00:00
|
|
|
if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == '2')
|
2011-07-07 00:08:19 +00:00
|
|
|
ARMArchFeature = "+v6t2";
|
2012-04-27 01:27:19 +00:00
|
|
|
else if (Len >= Idx+2 && TT[Idx+1] == 'm') {
|
2013-06-10 23:20:58 +00:00
|
|
|
isThumb = true;
|
2012-04-27 01:27:19 +00:00
|
|
|
if (NoCPU)
|
|
|
|
// v6m: FeatureNoARM, FeatureMClass
|
2013-10-07 16:55:23 +00:00
|
|
|
ARMArchFeature = "+v6m,+noarm,+mclass";
|
2012-04-27 01:27:19 +00:00
|
|
|
else
|
|
|
|
ARMArchFeature = "+v6";
|
|
|
|
} else
|
2011-07-07 03:55:05 +00:00
|
|
|
ARMArchFeature = "+v6";
|
2011-07-07 00:08:19 +00:00
|
|
|
} else if (SubVer == '5') {
|
2011-07-07 03:55:05 +00:00
|
|
|
if (Len >= Idx+3 && TT[Idx+1] == 't' && TT[Idx+2] == 'e')
|
2011-07-07 00:08:19 +00:00
|
|
|
ARMArchFeature = "+v5te";
|
2011-07-07 03:55:05 +00:00
|
|
|
else
|
|
|
|
ARMArchFeature = "+v5t";
|
|
|
|
} else if (SubVer == '4' && Len >= Idx+2 && TT[Idx+1] == 't')
|
|
|
|
ARMArchFeature = "+v4t";
|
2011-07-07 00:08:19 +00:00
|
|
|
}
|
|
|
|
|
2011-07-07 08:26:46 +00:00
|
|
|
if (isThumb) {
|
|
|
|
if (ARMArchFeature.empty())
|
2011-07-07 19:05:12 +00:00
|
|
|
ARMArchFeature = "+thumb-mode";
|
2011-07-07 08:26:46 +00:00
|
|
|
else
|
2011-07-07 19:05:12 +00:00
|
|
|
ARMArchFeature += ",+thumb-mode";
|
2011-07-07 08:26:46 +00:00
|
|
|
}
|
|
|
|
|
2013-01-30 16:30:19 +00:00
|
|
|
if (triple.isOSNaCl()) {
|
|
|
|
if (ARMArchFeature.empty())
|
|
|
|
ARMArchFeature = "+nacl-trap";
|
|
|
|
else
|
|
|
|
ARMArchFeature += ",+nacl-trap";
|
|
|
|
}
|
|
|
|
|
2011-07-07 00:08:19 +00:00
|
|
|
return ARMArchFeature;
|
|
|
|
}
|
2011-07-08 01:53:10 +00:00
|
|
|
|
|
|
|
MCSubtargetInfo *ARM_MC::createARMMCSubtargetInfo(StringRef TT, StringRef CPU,
|
|
|
|
StringRef FS) {
|
2012-04-26 01:13:36 +00:00
|
|
|
std::string ArchFS = ARM_MC::ParseARMTriple(TT, CPU);
|
2011-07-08 01:53:10 +00:00
|
|
|
if (!FS.empty()) {
|
|
|
|
if (!ArchFS.empty())
|
|
|
|
ArchFS = ArchFS + "," + FS.str();
|
|
|
|
else
|
|
|
|
ArchFS = FS;
|
|
|
|
}
|
|
|
|
|
|
|
|
MCSubtargetInfo *X = new MCSubtargetInfo();
|
2011-07-11 03:57:24 +00:00
|
|
|
InitARMMCSubtargetInfo(X, TT, CPU, ArchFS);
|
2011-07-08 01:53:10 +00:00
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
2011-07-14 23:50:31 +00:00
|
|
|
static MCInstrInfo *createARMMCInstrInfo() {
|
|
|
|
MCInstrInfo *X = new MCInstrInfo();
|
|
|
|
InitARMMCInstrInfo(X);
|
2011-07-08 01:53:10 +00:00
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
2011-07-18 20:57:22 +00:00
|
|
|
static MCRegisterInfo *createARMMCRegisterInfo(StringRef Triple) {
|
2011-07-14 23:50:31 +00:00
|
|
|
MCRegisterInfo *X = new MCRegisterInfo();
|
2012-12-19 23:38:53 +00:00
|
|
|
InitARMMCRegisterInfo(X, ARM::LR, 0, 0, ARM::PC);
|
2011-07-14 23:50:31 +00:00
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
2013-05-13 01:16:13 +00:00
|
|
|
static MCAsmInfo *createARMMCAsmInfo(const MCRegisterInfo &MRI, StringRef TT) {
|
2011-07-14 23:50:31 +00:00
|
|
|
Triple TheTriple(TT);
|
|
|
|
|
2014-01-27 22:38:14 +00:00
|
|
|
MCAsmInfo *MAI;
|
2014-04-02 20:32:05 +00:00
|
|
|
switch (TheTriple.getOS()) {
|
|
|
|
case llvm::Triple::Darwin:
|
|
|
|
case llvm::Triple::IOS:
|
|
|
|
case llvm::Triple::MacOSX:
|
2014-03-28 14:35:30 +00:00
|
|
|
MAI = new ARMMCAsmInfoDarwin(TT);
|
2014-04-02 20:32:05 +00:00
|
|
|
break;
|
|
|
|
case llvm::Triple::Win32:
|
|
|
|
switch (TheTriple.getEnvironment()) {
|
|
|
|
case llvm::Triple::Itanium:
|
|
|
|
MAI = new ARMCOFFMCAsmInfoGNU();
|
|
|
|
break;
|
|
|
|
case llvm::Triple::MSVC:
|
|
|
|
MAI = new ARMCOFFMCAsmInfoMicrosoft();
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
llvm_unreachable("invalid environment");
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
if (TheTriple.isOSBinFormatMachO())
|
|
|
|
MAI = new ARMMCAsmInfoDarwin(TT);
|
|
|
|
else
|
|
|
|
MAI = new ARMELFMCAsmInfo(TT);
|
|
|
|
break;
|
|
|
|
}
|
2011-07-14 23:50:31 +00:00
|
|
|
|
2014-01-27 22:38:14 +00:00
|
|
|
unsigned Reg = MRI.getDwarfRegNum(ARM::SP, true);
|
2014-04-25 05:30:21 +00:00
|
|
|
MAI->addInitialFrameState(MCCFIInstruction::createDefCfa(nullptr, Reg, 0));
|
2014-01-27 22:38:14 +00:00
|
|
|
|
|
|
|
return MAI;
|
2011-07-14 23:50:31 +00:00
|
|
|
}
|
|
|
|
|
2011-07-23 00:00:19 +00:00
|
|
|
static MCCodeGenInfo *createARMMCCodeGenInfo(StringRef TT, Reloc::Model RM,
|
2011-11-16 08:38:26 +00:00
|
|
|
CodeModel::Model CM,
|
|
|
|
CodeGenOpt::Level OL) {
|
2011-07-19 06:37:02 +00:00
|
|
|
MCCodeGenInfo *X = new MCCodeGenInfo();
|
2011-09-30 17:41:35 +00:00
|
|
|
if (RM == Reloc::Default) {
|
|
|
|
Triple TheTriple(TT);
|
|
|
|
// Default relocation model on Darwin is PIC, not DynamicNoPIC.
|
|
|
|
RM = TheTriple.isOSDarwin() ? Reloc::PIC_ : Reloc::DynamicNoPIC;
|
|
|
|
}
|
2011-11-16 08:38:26 +00:00
|
|
|
X->InitMCCodeGenInfo(RM, CM, OL);
|
2011-07-19 06:37:02 +00:00
|
|
|
return X;
|
|
|
|
}
|
|
|
|
|
2011-07-23 00:00:19 +00:00
|
|
|
// This is duplicated code. Refactor this.
|
2011-07-26 00:42:34 +00:00
|
|
|
static MCStreamer *createMCStreamer(const Target &T, StringRef TT,
|
2011-07-25 23:24:55 +00:00
|
|
|
MCContext &Ctx, MCAsmBackend &MAB,
|
2011-07-23 00:00:19 +00:00
|
|
|
raw_ostream &OS,
|
|
|
|
MCCodeEmitter *Emitter,
|
2014-01-26 06:38:58 +00:00
|
|
|
const MCSubtargetInfo &STI,
|
2011-07-23 00:00:19 +00:00
|
|
|
bool RelaxAll,
|
|
|
|
bool NoExecStack) {
|
|
|
|
Triple TheTriple(TT);
|
|
|
|
|
2014-04-27 03:48:22 +00:00
|
|
|
switch (TheTriple.getObjectFormat()) {
|
|
|
|
default: llvm_unreachable("unsupported object format");
|
|
|
|
case Triple::MachO: {
|
2014-02-04 17:22:40 +00:00
|
|
|
MCStreamer *S = createMachOStreamer(Ctx, MAB, OS, Emitter, false);
|
|
|
|
new ARMTargetStreamer(*S);
|
|
|
|
return S;
|
|
|
|
}
|
2014-04-27 03:48:22 +00:00
|
|
|
case Triple::COFF:
|
|
|
|
assert(TheTriple.isOSWindows() && "non-Windows ARM COFF is not supported");
|
|
|
|
return createARMWinCOFFStreamer(Ctx, MAB, *Emitter, OS);
|
|
|
|
case Triple::ELF:
|
|
|
|
return createARMELFStreamer(Ctx, MAB, OS, Emitter, false, NoExecStack,
|
|
|
|
TheTriple.getArch() == Triple::thumb);
|
2011-07-23 00:00:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-25 21:20:24 +00:00
|
|
|
static MCInstPrinter *createARMMCInstPrinter(const Target &T,
|
|
|
|
unsigned SyntaxVariant,
|
2011-09-07 17:24:38 +00:00
|
|
|
const MCAsmInfo &MAI,
|
2012-04-02 06:09:36 +00:00
|
|
|
const MCInstrInfo &MII,
|
2012-03-05 19:33:20 +00:00
|
|
|
const MCRegisterInfo &MRI,
|
2011-09-07 17:24:38 +00:00
|
|
|
const MCSubtargetInfo &STI) {
|
2011-07-25 21:20:24 +00:00
|
|
|
if (SyntaxVariant == 0)
|
2012-04-02 06:09:36 +00:00
|
|
|
return new ARMInstPrinter(MAI, MII, MRI, STI);
|
2014-04-25 05:30:21 +00:00
|
|
|
return nullptr;
|
2011-07-25 21:20:24 +00:00
|
|
|
}
|
|
|
|
|
2013-05-24 22:51:52 +00:00
|
|
|
static MCRelocationInfo *createARMMCRelocationInfo(StringRef TT,
|
|
|
|
MCContext &Ctx) {
|
Add MCSymbolizer for symbolic/annotated disassembly.
This is a basic first step towards symbolization of disassembled
instructions. This used to be done using externally provided (C API)
callbacks. This patch introduces:
- the MCSymbolizer class, that mimics the same functions that were used
in the X86 and ARM disassemblers to symbolize immediate operands and
to annotate loads based off PC (for things like c string literals).
- the MCExternalSymbolizer class, which implements the old C API.
- the MCRelocationInfo class, which provides a way for targets to
translate relocations (either object::RelocationRef, or disassembler
C API VariantKinds) to MCExprs.
- the MCObjectSymbolizer class, which does symbolization using what it
finds in an object::ObjectFile. This makes simple symbolization (with
no fancy relocation stuff) work for all object formats!
- x86-64 Mach-O and ELF MCRelocationInfos.
- A basic ARM Mach-O MCRelocationInfo, that provides just enough to
support the C API VariantKinds.
Most of what works in otool (the only user of the old symbolization API
that I know of) for x86-64 symbolic disassembly (-tvV) works, namely:
- symbol references: call _foo; jmp 15 <_foo+50>
- relocations: call _foo-_bar; call _foo-4
- __cf?string: leaq 193(%rip), %rax ## literal pool for "hello"
Stub support is the main missing part (because libObject doesn't know,
among other things, about mach-o indirect symbols).
As for the MCSymbolizer API, instead of relying on the disassemblers
to call the tryAdding* methods, maybe this could be done automagically
using InstrInfo? For instance, even though PC-relative LEAs are used
to get the address of string literals in a typical Mach-O file, a MOV
would be used in an ELF file. And right now, the explicit symbolization
only recognizes PC-relative LEAs. InstrInfo should have already have
most of what is needed to know what to symbolize, so this can
definitely be improved.
I'd also like to remove object::RelocationRef::getValueString (it seems
only used by relocation printing in objdump), as simply printing the
created MCExpr is definitely enough (and cleaner than string concats).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182625 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 00:39:57 +00:00
|
|
|
Triple TheTriple(TT);
|
2013-12-10 16:57:43 +00:00
|
|
|
if (TheTriple.isOSBinFormatMachO())
|
Add MCSymbolizer for symbolic/annotated disassembly.
This is a basic first step towards symbolization of disassembled
instructions. This used to be done using externally provided (C API)
callbacks. This patch introduces:
- the MCSymbolizer class, that mimics the same functions that were used
in the X86 and ARM disassemblers to symbolize immediate operands and
to annotate loads based off PC (for things like c string literals).
- the MCExternalSymbolizer class, which implements the old C API.
- the MCRelocationInfo class, which provides a way for targets to
translate relocations (either object::RelocationRef, or disassembler
C API VariantKinds) to MCExprs.
- the MCObjectSymbolizer class, which does symbolization using what it
finds in an object::ObjectFile. This makes simple symbolization (with
no fancy relocation stuff) work for all object formats!
- x86-64 Mach-O and ELF MCRelocationInfos.
- A basic ARM Mach-O MCRelocationInfo, that provides just enough to
support the C API VariantKinds.
Most of what works in otool (the only user of the old symbolization API
that I know of) for x86-64 symbolic disassembly (-tvV) works, namely:
- symbol references: call _foo; jmp 15 <_foo+50>
- relocations: call _foo-_bar; call _foo-4
- __cf?string: leaq 193(%rip), %rax ## literal pool for "hello"
Stub support is the main missing part (because libObject doesn't know,
among other things, about mach-o indirect symbols).
As for the MCSymbolizer API, instead of relying on the disassemblers
to call the tryAdding* methods, maybe this could be done automagically
using InstrInfo? For instance, even though PC-relative LEAs are used
to get the address of string literals in a typical Mach-O file, a MOV
would be used in an ELF file. And right now, the explicit symbolization
only recognizes PC-relative LEAs. InstrInfo should have already have
most of what is needed to know what to symbolize, so this can
definitely be improved.
I'd also like to remove object::RelocationRef::getValueString (it seems
only used by relocation printing in objdump), as simply printing the
created MCExpr is definitely enough (and cleaner than string concats).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182625 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 00:39:57 +00:00
|
|
|
return createARMMachORelocationInfo(Ctx);
|
|
|
|
// Default to the stock relocation info.
|
2013-05-24 22:51:52 +00:00
|
|
|
return llvm::createMCRelocationInfo(TT, Ctx);
|
Add MCSymbolizer for symbolic/annotated disassembly.
This is a basic first step towards symbolization of disassembled
instructions. This used to be done using externally provided (C API)
callbacks. This patch introduces:
- the MCSymbolizer class, that mimics the same functions that were used
in the X86 and ARM disassemblers to symbolize immediate operands and
to annotate loads based off PC (for things like c string literals).
- the MCExternalSymbolizer class, which implements the old C API.
- the MCRelocationInfo class, which provides a way for targets to
translate relocations (either object::RelocationRef, or disassembler
C API VariantKinds) to MCExprs.
- the MCObjectSymbolizer class, which does symbolization using what it
finds in an object::ObjectFile. This makes simple symbolization (with
no fancy relocation stuff) work for all object formats!
- x86-64 Mach-O and ELF MCRelocationInfos.
- A basic ARM Mach-O MCRelocationInfo, that provides just enough to
support the C API VariantKinds.
Most of what works in otool (the only user of the old symbolization API
that I know of) for x86-64 symbolic disassembly (-tvV) works, namely:
- symbol references: call _foo; jmp 15 <_foo+50>
- relocations: call _foo-_bar; call _foo-4
- __cf?string: leaq 193(%rip), %rax ## literal pool for "hello"
Stub support is the main missing part (because libObject doesn't know,
among other things, about mach-o indirect symbols).
As for the MCSymbolizer API, instead of relying on the disassemblers
to call the tryAdding* methods, maybe this could be done automagically
using InstrInfo? For instance, even though PC-relative LEAs are used
to get the address of string literals in a typical Mach-O file, a MOV
would be used in an ELF file. And right now, the explicit symbolization
only recognizes PC-relative LEAs. InstrInfo should have already have
most of what is needed to know what to symbolize, so this can
definitely be improved.
I'd also like to remove object::RelocationRef::getValueString (it seems
only used by relocation printing in objdump), as simply printing the
created MCExpr is definitely enough (and cleaner than string concats).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182625 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 00:39:57 +00:00
|
|
|
}
|
|
|
|
|
2011-08-08 18:56:44 +00:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ARMMCInstrAnalysis : public MCInstrAnalysis {
|
|
|
|
public:
|
|
|
|
ARMMCInstrAnalysis(const MCInstrInfo *Info) : MCInstrAnalysis(Info) {}
|
|
|
|
|
2014-03-10 03:19:03 +00:00
|
|
|
bool isUnconditionalBranch(const MCInst &Inst) const override {
|
2011-08-08 18:56:44 +00:00
|
|
|
// BCCs with the "always" predicate are unconditional branches.
|
|
|
|
if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
|
|
|
|
return true;
|
|
|
|
return MCInstrAnalysis::isUnconditionalBranch(Inst);
|
|
|
|
}
|
|
|
|
|
2014-03-10 03:19:03 +00:00
|
|
|
bool isConditionalBranch(const MCInst &Inst) const override {
|
2011-08-08 18:56:44 +00:00
|
|
|
// BCCs with the "always" predicate are unconditional branches.
|
|
|
|
if (Inst.getOpcode() == ARM::Bcc && Inst.getOperand(1).getImm()==ARMCC::AL)
|
|
|
|
return false;
|
|
|
|
return MCInstrAnalysis::isConditionalBranch(Inst);
|
|
|
|
}
|
|
|
|
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182628 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 01:07:04 +00:00
|
|
|
bool evaluateBranch(const MCInst &Inst, uint64_t Addr,
|
2014-03-10 03:19:03 +00:00
|
|
|
uint64_t Size, uint64_t &Target) const override {
|
2011-08-08 18:56:44 +00:00
|
|
|
// We only handle PCRel branches for now.
|
|
|
|
if (Info->get(Inst.getOpcode()).OpInfo[0].OperandType!=MCOI::OPERAND_PCREL)
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182628 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 01:07:04 +00:00
|
|
|
return false;
|
2011-08-08 18:56:44 +00:00
|
|
|
|
|
|
|
int64_t Imm = Inst.getOperand(0).getImm();
|
|
|
|
// FIXME: This is not right for thumb.
|
MC: Disassembled CFG reconstruction.
This patch builds on some existing code to do CFG reconstruction from
a disassembled binary:
- MCModule represents the binary, and has a list of MCAtoms.
- MCAtom represents either disassembled instructions (MCTextAtom), or
contiguous data (MCDataAtom), and covers a specific range of addresses.
- MCBasicBlock and MCFunction form the reconstructed CFG. An MCBB is
backed by an MCTextAtom, and has the usual successors/predecessors.
- MCObjectDisassembler creates a module from an ObjectFile using a
disassembler. It first builds an atom for each section. It can also
construct the CFG, and this splits the text atoms into basic blocks.
MCModule and MCAtom were only sketched out; MCFunction and MCBB were
implemented under the experimental "-cfg" llvm-objdump -macho option.
This cleans them up for further use; llvm-objdump -d -cfg now generates
graphviz files for each function found in the binary.
In the future, MCObjectDisassembler may be the right place to do
"intelligent" disassembly: for example, handling constant islands is just
a matter of splitting the atom, using information that may be available
in the ObjectFile. Also, better initial atom formation than just using
sections is possible using symbols (and things like Mach-O's
function_starts load command).
This brings two minor regressions in llvm-objdump -macho -cfg:
- The printing of a relocation's referenced symbol.
- An annotation on loop BBs, i.e., which are their own successor.
Relocation printing is replaced by the MCSymbolizer; the basic CFG
annotation will be superseded by more related functionality.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182628 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 01:07:04 +00:00
|
|
|
Target = Addr+Imm+8; // In ARM mode the PC is always off by 8 bytes.
|
|
|
|
return true;
|
2011-08-08 18:56:44 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
static MCInstrAnalysis *createARMMCInstrAnalysis(const MCInstrInfo *Info) {
|
|
|
|
return new ARMMCInstrAnalysis(Info);
|
|
|
|
}
|
2011-07-23 00:00:19 +00:00
|
|
|
|
2011-07-22 21:58:54 +00:00
|
|
|
// Force static initialization.
|
|
|
|
extern "C" void LLVMInitializeARMTargetMC() {
|
|
|
|
// Register the MC asm info.
|
2014-04-01 15:19:30 +00:00
|
|
|
RegisterMCAsmInfoFn X(TheARMLETarget, createARMMCAsmInfo);
|
|
|
|
RegisterMCAsmInfoFn Y(TheARMBETarget, createARMMCAsmInfo);
|
|
|
|
RegisterMCAsmInfoFn A(TheThumbLETarget, createARMMCAsmInfo);
|
|
|
|
RegisterMCAsmInfoFn B(TheThumbBETarget, createARMMCAsmInfo);
|
2011-07-22 21:58:54 +00:00
|
|
|
|
|
|
|
// Register the MC codegen info.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCCodeGenInfo(TheARMLETarget, createARMMCCodeGenInfo);
|
|
|
|
TargetRegistry::RegisterMCCodeGenInfo(TheARMBETarget, createARMMCCodeGenInfo);
|
|
|
|
TargetRegistry::RegisterMCCodeGenInfo(TheThumbLETarget, createARMMCCodeGenInfo);
|
|
|
|
TargetRegistry::RegisterMCCodeGenInfo(TheThumbBETarget, createARMMCCodeGenInfo);
|
2011-07-22 21:58:54 +00:00
|
|
|
|
|
|
|
// Register the MC instruction info.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCInstrInfo(TheARMLETarget, createARMMCInstrInfo);
|
|
|
|
TargetRegistry::RegisterMCInstrInfo(TheARMBETarget, createARMMCInstrInfo);
|
|
|
|
TargetRegistry::RegisterMCInstrInfo(TheThumbLETarget, createARMMCInstrInfo);
|
|
|
|
TargetRegistry::RegisterMCInstrInfo(TheThumbBETarget, createARMMCInstrInfo);
|
2011-07-22 21:58:54 +00:00
|
|
|
|
|
|
|
// Register the MC register info.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCRegInfo(TheARMLETarget, createARMMCRegisterInfo);
|
|
|
|
TargetRegistry::RegisterMCRegInfo(TheARMBETarget, createARMMCRegisterInfo);
|
|
|
|
TargetRegistry::RegisterMCRegInfo(TheThumbLETarget, createARMMCRegisterInfo);
|
|
|
|
TargetRegistry::RegisterMCRegInfo(TheThumbBETarget, createARMMCRegisterInfo);
|
2011-07-22 21:58:54 +00:00
|
|
|
|
|
|
|
// Register the MC subtarget info.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCSubtargetInfo(TheARMLETarget,
|
2014-03-28 14:35:30 +00:00
|
|
|
ARM_MC::createARMMCSubtargetInfo);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCSubtargetInfo(TheARMBETarget,
|
2011-07-22 21:58:54 +00:00
|
|
|
ARM_MC::createARMMCSubtargetInfo);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCSubtargetInfo(TheThumbLETarget,
|
2014-03-28 14:35:30 +00:00
|
|
|
ARM_MC::createARMMCSubtargetInfo);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCSubtargetInfo(TheThumbBETarget,
|
2011-07-22 21:58:54 +00:00
|
|
|
ARM_MC::createARMMCSubtargetInfo);
|
2011-07-23 00:00:19 +00:00
|
|
|
|
2011-08-23 20:15:21 +00:00
|
|
|
// Register the MC instruction analyzer.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCInstrAnalysis(TheARMLETarget,
|
2014-03-28 14:35:30 +00:00
|
|
|
createARMMCInstrAnalysis);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCInstrAnalysis(TheARMBETarget,
|
2011-08-23 20:15:21 +00:00
|
|
|
createARMMCInstrAnalysis);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCInstrAnalysis(TheThumbLETarget,
|
2014-03-28 14:35:30 +00:00
|
|
|
createARMMCInstrAnalysis);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCInstrAnalysis(TheThumbBETarget,
|
2011-08-23 20:15:21 +00:00
|
|
|
createARMMCInstrAnalysis);
|
|
|
|
|
2011-07-23 00:00:19 +00:00
|
|
|
// Register the MC Code Emitter
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCCodeEmitter(TheARMLETarget,
|
|
|
|
createARMLEMCCodeEmitter);
|
|
|
|
TargetRegistry::RegisterMCCodeEmitter(TheARMBETarget,
|
|
|
|
createARMBEMCCodeEmitter);
|
|
|
|
TargetRegistry::RegisterMCCodeEmitter(TheThumbLETarget,
|
|
|
|
createARMLEMCCodeEmitter);
|
|
|
|
TargetRegistry::RegisterMCCodeEmitter(TheThumbBETarget,
|
|
|
|
createARMBEMCCodeEmitter);
|
2011-07-23 00:00:19 +00:00
|
|
|
|
|
|
|
// Register the asm backend.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCAsmBackend(TheARMLETarget, createARMLEAsmBackend);
|
|
|
|
TargetRegistry::RegisterMCAsmBackend(TheARMBETarget, createARMBEAsmBackend);
|
|
|
|
TargetRegistry::RegisterMCAsmBackend(TheThumbLETarget,
|
|
|
|
createThumbLEAsmBackend);
|
|
|
|
TargetRegistry::RegisterMCAsmBackend(TheThumbBETarget,
|
|
|
|
createThumbBEAsmBackend);
|
2011-07-23 00:00:19 +00:00
|
|
|
|
|
|
|
// Register the object streamer.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCObjectStreamer(TheARMLETarget, createMCStreamer);
|
|
|
|
TargetRegistry::RegisterMCObjectStreamer(TheARMBETarget, createMCStreamer);
|
|
|
|
TargetRegistry::RegisterMCObjectStreamer(TheThumbLETarget, createMCStreamer);
|
|
|
|
TargetRegistry::RegisterMCObjectStreamer(TheThumbBETarget, createMCStreamer);
|
2011-07-25 21:20:24 +00:00
|
|
|
|
2013-10-08 13:08:17 +00:00
|
|
|
// Register the asm streamer.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterAsmStreamer(TheARMLETarget, createMCAsmStreamer);
|
|
|
|
TargetRegistry::RegisterAsmStreamer(TheARMBETarget, createMCAsmStreamer);
|
|
|
|
TargetRegistry::RegisterAsmStreamer(TheThumbLETarget, createMCAsmStreamer);
|
|
|
|
TargetRegistry::RegisterAsmStreamer(TheThumbBETarget, createMCAsmStreamer);
|
2013-10-08 13:08:17 +00:00
|
|
|
|
2011-07-25 21:20:24 +00:00
|
|
|
// Register the MCInstPrinter.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCInstPrinter(TheARMLETarget, createARMMCInstPrinter);
|
|
|
|
TargetRegistry::RegisterMCInstPrinter(TheARMBETarget, createARMMCInstPrinter);
|
|
|
|
TargetRegistry::RegisterMCInstPrinter(TheThumbLETarget,
|
2014-03-28 14:35:30 +00:00
|
|
|
createARMMCInstPrinter);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCInstPrinter(TheThumbBETarget,
|
2014-03-28 14:35:30 +00:00
|
|
|
createARMMCInstPrinter);
|
Add MCSymbolizer for symbolic/annotated disassembly.
This is a basic first step towards symbolization of disassembled
instructions. This used to be done using externally provided (C API)
callbacks. This patch introduces:
- the MCSymbolizer class, that mimics the same functions that were used
in the X86 and ARM disassemblers to symbolize immediate operands and
to annotate loads based off PC (for things like c string literals).
- the MCExternalSymbolizer class, which implements the old C API.
- the MCRelocationInfo class, which provides a way for targets to
translate relocations (either object::RelocationRef, or disassembler
C API VariantKinds) to MCExprs.
- the MCObjectSymbolizer class, which does symbolization using what it
finds in an object::ObjectFile. This makes simple symbolization (with
no fancy relocation stuff) work for all object formats!
- x86-64 Mach-O and ELF MCRelocationInfos.
- A basic ARM Mach-O MCRelocationInfo, that provides just enough to
support the C API VariantKinds.
Most of what works in otool (the only user of the old symbolization API
that I know of) for x86-64 symbolic disassembly (-tvV) works, namely:
- symbol references: call _foo; jmp 15 <_foo+50>
- relocations: call _foo-_bar; call _foo-4
- __cf?string: leaq 193(%rip), %rax ## literal pool for "hello"
Stub support is the main missing part (because libObject doesn't know,
among other things, about mach-o indirect symbols).
As for the MCSymbolizer API, instead of relying on the disassemblers
to call the tryAdding* methods, maybe this could be done automagically
using InstrInfo? For instance, even though PC-relative LEAs are used
to get the address of string literals in a typical Mach-O file, a MOV
would be used in an ELF file. And right now, the explicit symbolization
only recognizes PC-relative LEAs. InstrInfo should have already have
most of what is needed to know what to symbolize, so this can
definitely be improved.
I'd also like to remove object::RelocationRef::getValueString (it seems
only used by relocation printing in objdump), as simply printing the
created MCExpr is definitely enough (and cleaner than string concats).
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@182625 91177308-0d34-0410-b5e6-96231b3b80d8
2013-05-24 00:39:57 +00:00
|
|
|
|
|
|
|
// Register the MC relocation info.
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCRelocationInfo(TheARMLETarget,
|
2014-03-28 14:35:30 +00:00
|
|
|
createARMMCRelocationInfo);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCRelocationInfo(TheARMBETarget,
|
2014-03-28 14:35:30 +00:00
|
|
|
createARMMCRelocationInfo);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCRelocationInfo(TheThumbLETarget,
|
2013-05-24 22:51:52 +00:00
|
|
|
createARMMCRelocationInfo);
|
2014-04-01 15:19:30 +00:00
|
|
|
TargetRegistry::RegisterMCRelocationInfo(TheThumbBETarget,
|
2013-05-24 22:51:52 +00:00
|
|
|
createARMMCRelocationInfo);
|
2011-07-19 06:37:02 +00:00
|
|
|
}
|