44 ARMELFObjectWriter(uint8_t OSABI)
51 if (STI.getTargetTriple().isOSBinFormatELF() && Name ==
"R_ARM_NONE")
58 unsigned IsPCRelConstant =
65 {
"fixup_arm_ldst_pcrel_12", 0, 32, IsPCRelConstant},
66 {
"fixup_t2_ldst_pcrel_12", 0, 32,
68 {
"fixup_arm_pcrel_10_unscaled", 0, 32, IsPCRelConstant},
69 {
"fixup_arm_pcrel_10", 0, 32, IsPCRelConstant},
70 {
"fixup_t2_pcrel_10", 0, 32,
74 {
"fixup_t2_pcrel_9", 0, 32,
76 {
"fixup_thumb_adr_pcrel_10", 0, 8,
78 {
"fixup_arm_adr_pcrel_12", 0, 32, IsPCRelConstant},
79 {
"fixup_t2_adr_pcrel_12", 0, 32,
90 {
"fixup_arm_thumb_blx", 0, 32,
94 {
"fixup_arm_thumb_cp", 0, 8,
100 {
"fixup_arm_movt_hi16", 0, 20, 0},
101 {
"fixup_arm_movw_lo16", 0, 20, 0},
102 {
"fixup_t2_movt_hi16", 0, 20, 0},
103 {
"fixup_t2_movw_lo16", 0, 20, 0},
104 {
"fixup_arm_mod_imm", 0, 12, 0},
105 {
"fixup_t2_so_imm", 0, 26, 0},
110 {
"fixup_bfcsel_else_target", 0, 32, 0},
120 {
"fixup_t2_ldst_pcrel_12", 0, 32,
125 {
"fixup_t2_pcrel_10", 0, 32,
129 {
"fixup_t2_pcrel_9", 0, 32,
132 {
"fixup_thumb_adr_pcrel_10", 8, 8,
136 {
"fixup_t2_adr_pcrel_12", 0, 32,
148 {
"fixup_arm_thumb_blx", 0, 32,
152 {
"fixup_arm_thumb_cp", 8, 8,
158 {
"fixup_arm_movt_hi16", 12, 20, 0},
159 {
"fixup_arm_movw_lo16", 12, 20, 0},
160 {
"fixup_t2_movt_hi16", 12, 20, 0},
161 {
"fixup_t2_movw_lo16", 12, 20, 0},
162 {
"fixup_arm_mod_imm", 20, 12, 0},
163 {
"fixup_t2_so_imm", 26, 6, 0},
168 {
"fixup_bfcsel_else_target", 0, 32, 0},
198 bool HasV8MBaselineOps = STI.
getFeatureBits()[ARM::HasV8MBaselineOps];
204 return HasThumb2 ? (
unsigned)ARM::t2Bcc : Op;
206 return HasThumb2 ? (
unsigned)ARM::t2LDRpci : Op;
208 return HasThumb2 ? (
unsigned)ARM::t2ADR : Op;
210 return HasV8MBaselineOps ? (
unsigned)ARM::t2B : Op;
226 int64_t
Offset = int64_t(Value) - 4;
227 if (Offset < Min || Offset > Max)
228 return "out of range pc-relative fixup value";
233 uint64_t
Value)
const {
242 int64_t
Offset = int64_t(Value) - 4;
243 if (Offset > 2046 || Offset < -2048)
244 return "out of range pc-relative fixup value";
254 int64_t
Offset = int64_t(Value) - 4;
255 if (Offset > 254 || Offset < -256)
256 return "out of range pc-relative fixup value";
263 int64_t
Offset = int64_t(Value) - 4;
265 return "misaligned pc-relative fixup value";
266 else if (Offset > 1020 || Offset < 0)
267 return "out of range pc-relative fixup value";
274 int64_t
Offset = (Value & ~1);
276 return "will be converted to nop";
300 if (Value != 2 && Value != 4)
301 return "out of range label-relative fixup value";
314 return reasonForFixupRelaxation(Fixup, Value);
334 RelaxedOp == ARM::tHINT) {
349 const uint16_t Thumb1_16bitNopEncoding = 0x46c0;
350 const uint16_t Thumb2_16bitNopEncoding = 0xbf00;
351 const uint32_t ARMv4_NopEncoding = 0xe1a00000;
352 const uint32_t ARMv6T2_NopEncoding = 0xe320f000;
355 hasNOP() ? Thumb2_16bitNopEncoding : Thumb1_16bitNopEncoding;
356 uint64_t NumNops = Count / 2;
357 for (uint64_t i = 0; i != NumNops; ++i)
365 hasNOP() ? ARMv6T2_NopEncoding : ARMv4_NopEncoding;
366 uint64_t NumNops = Count / 4;
367 for (uint64_t i = 0; i != NumNops; ++i)
381 OS.
write(
"\0\0\xa0", 3);
389 if (IsLittleEndian) {
392 uint32_t Swapped = (Value & 0xFFFF0000) >> 16;
393 Swapped |= (Value & 0x0000FFFF) << 16;
400 bool IsLittleEndian) {
403 if (IsLittleEndian) {
404 Value = (SecondHalf & 0xFFFF) << 16;
405 Value |= (FirstHalf & 0xFFFF);
407 Value = (SecondHalf & 0xFFFF);
408 Value |= (FirstHalf & 0xFFFF) << 16;
427 if (A->hasSubsectionsViaSymbols() && Asm.
isThumbFunc(&A->getSymbol()) &&
428 A->getSymbol().isExternal() &&
454 unsigned Hi4 = (Value & 0xF000) >> 12;
455 unsigned Lo12 = Value & 0x0FFF;
458 Value = (Hi4 << 16) | (Lo12);
467 unsigned Hi4 = (Value & 0xF000) >> 12;
468 unsigned i = (Value & 0x800) >> 11;
469 unsigned Mid3 = (Value & 0x700) >> 8;
470 unsigned Lo8 = Value & 0x0FF;
475 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
486 if ((int64_t)Value < 0) {
494 Value |= isAdd << 23;
507 if ((int64_t)Value < 0) {
522 if ((int64_t)Value < 0) {
528 out |= (Value & 0x800) << 15;
529 out |= (Value & 0x700) << 4;
530 out |= (Value & 0x0FF);
543 dyn_cast<MCSymbolRefExpr>(Fixup.
getValue()))
546 return 0xffffff & ((Value - 8) >> 2);
549 if (!isInt<25>(Value)) {
557 bool I = Value & 0x800000;
558 bool J1 = Value & 0x400000;
559 bool J2 = Value & 0x200000;
566 out |= (Value & 0x1FF800) << 5;
567 out |= (Value & 0x0007FF);
573 if (!isInt<21>(Value)) {
581 out |= (Value & 0x80000) << 7;
582 out |= (Value & 0x40000) >> 7;
583 out |= (Value & 0x20000) >> 4;
584 out |= (Value & 0x1F800) << 5;
585 out |= (Value & 0x007FF);
590 if (!isInt<25>(Value - 4) ||
594 !isInt<23>(Value - 4))) {
612 uint32_t signBit = (offset & 0x800000) >> 23;
613 uint32_t I1Bit = (offset & 0x400000) >> 22;
614 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
615 uint32_t I2Bit = (offset & 0x200000) >> 21;
616 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
617 uint32_t imm10Bits = (offset & 0x1FF800) >> 11;
618 uint32_t imm11Bits = (offset & 0x000007FF);
638 if (Value % 4 != 0) {
645 dyn_cast<MCSymbolRefExpr>(Fixup.
getValue()))
648 uint32_t signBit = (offset & 0x400000) >> 22;
649 uint32_t I1Bit = (offset & 0x200000) >> 21;
650 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
651 uint32_t I2Bit = (offset & 0x100000) >> 20;
652 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
653 uint32_t imm10HBits = (offset & 0xFFC00) >> 10;
654 uint32_t imm10LBits = (offset & 0x3FF);
667 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
668 if (FixupDiagnostic) {
674 return ((Value - 4) >> 2) & 0xff;
686 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
693 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
694 if (FixupDiagnostic) {
699 return ((Value - 4) >> 1) & 0x7ff;
704 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
705 if (FixupDiagnostic) {
710 return ((Value - 4) >> 1) & 0xff;
715 if ((int64_t)Value < 0) {
724 Value = (Value & 0xf) | ((Value & 0xf0) << 4);
725 return Value | (isAdd << 23);
735 if ((int64_t)Value < 0) {
745 Value |= isAdd << 23;
762 if ((int64_t)Value < 0) {
776 Value |= isAdd << 23;
794 if ((int64_t)Value < 0) {
803 uint64_t EncValue = 0;
804 EncValue |= (Value & 0x800) << 15;
805 EncValue |= (Value & 0x700) << 4;
806 EncValue |= (Value & 0xff);
810 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
811 if (FixupDiagnostic) {
815 uint32_t out = (((Value - 4) >> 1) & 0xf) << 23;
821 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
822 if (FixupDiagnostic) {
829 out |= (((Value - 4) >> 1) & 0x1) << 11;
830 out |= (((Value - 4) >> 1) & 0x7fe);
831 out |= (((Value - 4) >> 1) & HighBitMask) << 5;
840 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
841 if (FixupDiagnostic) {
845 uint32_t out = ((Value >> 2) & 1) << 17;
850 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
851 if (FixupDiagnostic) {
855 uint64_t real_value = Value - 4;
858 real_value = -real_value;
859 out |= ((real_value >> 1) & 0x1) << 11;
860 out |= ((real_value >> 1) & 0x7fe);
875 assert(Sym &&
"How did we resolve this?");
887 if (Sym && Sym->
isELF()) {
1045 assert(Offset + NumBytes <= Data.
size() &&
"Invalid fixup offset!");
1048 unsigned FullSizeBytes;
1051 assert((Offset + FullSizeBytes) <= Data.
size() &&
"Invalid fixup size!");
1052 assert(NumBytes <= FullSizeBytes &&
"Invalid fixup size!");
1058 for (
unsigned i = 0; i != NumBytes; ++i) {
1059 unsigned Idx = Endian ==
support::little ? i : (FullSizeBytes - 1 - i);
1060 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
1106 unsigned CFARegister = ARM::SP;
1107 int CFARegisterOffset = 0;
1110 int FloatRegCount = 0;
1112 for (
size_t i = 0,
e = Instrs.
size(); i !=
e; ++i) {
1128 if (ARMMCRegisterClasses[ARM::GPRRegClassID].
contains(Reg))
1130 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].
contains(Reg)) {
1135 llvm::dbgs() <<
".cfi_offset on unknown register=" 1147 <<
"CFI directive not compatiable with comact " 1156 if ((CFARegister == ARM::SP) && (CFARegisterOffset == 0))
1160 if (CFARegister != ARM::R7) {
1163 <<
" instead of r7\n");
1166 int StackAdjust = CFARegisterOffset - 8;
1167 if (RegOffsets.
lookup(ARM::LR) != (-4 - StackAdjust)) {
1170 <<
"LR not saved as standard frame, StackAdjust=" 1172 <<
", CFARegisterOffset=" << CFARegisterOffset
1173 <<
", lr save at offset=" << RegOffsets[14] <<
"\n");
1176 if (RegOffsets.
lookup(ARM::R7) != (-8 - StackAdjust)) {
1178 llvm::dbgs() <<
"r7 not saved as standard frame\n");
1184 switch (StackAdjust) {
1188 CompactUnwindEncoding |= 0x00400000;
1191 CompactUnwindEncoding |= 0x00800000;
1194 CompactUnwindEncoding |= 0x00C00000;
1198 <<
".cfi_def_cfa stack adjust (" 1199 << StackAdjust <<
") out of range\n");
1216 int CurOffset = -8 - StackAdjust;
1217 for (
auto CSReg : GPRCSRegs) {
1222 int RegOffset =
Offset->second;
1223 if (RegOffset != CurOffset - 4) {
1226 << RegOffset <<
" but only supported at " 1227 << CurOffset <<
"\n");
1230 CompactUnwindEncoding |= CSReg.Encoding;
1235 if (FloatRegCount == 0)
1236 return CompactUnwindEncoding;
1244 if (FloatRegCount > 4) {
1246 llvm::dbgs() <<
"unsupported number of D registers saved (" 1247 << FloatRegCount <<
")\n");
1254 static unsigned FPRCSRegs[] = { ARM::D8, ARM::D10, ARM::D12, ARM::D14 };
1255 for (
int Idx = FloatRegCount - 1; Idx >= 0; --Idx) {
1256 auto Offset = RegOffsets.
find(FPRCSRegs[Idx]);
1259 llvm::dbgs() << FloatRegCount <<
" D-regs saved, but " 1260 <<
MRI.getName(FPRCSRegs[Idx])
1263 }
else if (
Offset->second != CurOffset - 8) {
1265 llvm::dbgs() << FloatRegCount <<
" D-regs saved, but " 1266 <<
MRI.getName(FPRCSRegs[Idx])
1267 <<
" saved at " <<
Offset->second
1268 <<
", expected at " << CurOffset - 8
1275 return CompactUnwindEncoding | ((FloatRegCount - 1) << 8);
1283 case ARM::ArchKind::ARMV4T:
1285 case ARM::ArchKind::ARMV5T:
1286 case ARM::ArchKind::ARMV5TE:
1287 case ARM::ArchKind::ARMV5TEJ:
1289 case ARM::ArchKind::ARMV6:
1290 case ARM::ArchKind::ARMV6K:
1292 case ARM::ArchKind::ARMV7A:
1294 case ARM::ArchKind::ARMV7S:
1296 case ARM::ArchKind::ARMV7K:
1298 case ARM::ArchKind::ARMV6M:
1300 case ARM::ArchKind::ARMV7M:
1302 case ARM::ArchKind::ARMV7EM:
1322 return new ARMAsmBackendWinCOFF(T, STI);
1326 return new ARMAsmBackendELF(T, STI, OSABI, Endian);
MCAsmBackend * createARMLEAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
unsigned getRelaxedOpcode(unsigned Op, const MCSubtargetInfo &STI) const
This fixup kind should be resolved if defined.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
This represents an "assembler immediate".
const char * reasonForFixupRelaxation(const MCFixup &Fixup, uint64_t Value) const
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
bool isOSBinFormatELF() const
Tests whether the OS uses the ELF binary format.
OSType getOS() const
getOS - Get the parsed operating system type of this triple.
virtual const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const
Get information on a fixup kind.
void dump_pretty(raw_ostream &OS, const MCInstPrinter *Printer=nullptr, StringRef Separator=" ") const
Dump the MCInst as prettily as possible using the additional MC structures, if given.
A raw_ostream that writes to an SmallVector or SmallString.
static Lanai::Fixups FixupKind(const MCExpr *Expr)
static bool isThumb(const MCSubtargetInfo &STI)
return AArch64::GPR64RegClass contains(Reg)
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
const Triple & getTargetTriple() const
Is this fixup kind PCrelative? This is used by the assembler backend to evaluate fixup values in a ta...
#define DEBUG_WITH_TYPE(TYPE, X)
DEBUG_WITH_TYPE macro - This macro should be used by passes to emit debug information.
MCContext & getContext() const
int64_t getConstant() const
static MCOperand createReg(unsigned Reg)
const FeatureBitset & getFeatureBits() const
static MachO::CPUSubTypeARM getMachOSubTypeFromArch(StringRef Arch)
Encapsulates the layout of an assembly file at a particular point in time.
Represent a reference to a symbol from inside an expression.
A four-byte section relative fixup.
Context object for machine code objects.
A two-byte section relative fixup.
.code16 (X86) / .code 16 (ARM)
MCAsmBackend * createARMBEAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options)
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Instances of this class represent a single low-level machine instruction.
static MCAsmBackend * createARMAsmBackend(const Target &T, const MCSubtargetInfo &STI, const MCRegisterInfo &MRI, const MCTargetOptions &Options, support::endianness Endian)
Flag
These should be considered private to the implementation of the MCInstrDesc class.
MCRegisterInfo base class - We assume that the target defines a static array of MCRegisterDesc object...
static const char * checkPCRelOffset(uint64_t Value, int64_t Min, int64_t Max)
iterator find(const_arg_type_t< KeyT > Val)
void write(void *memory, value_type value, endianness endian)
Write a value to memory with a particular endianness.
bool isOSWindows() const
Tests whether the OS is Windows.
A relaxable fragment holds on to its MCInst, since it may need to be relaxed during the assembler lay...
uint32_t generateCompactUnwindEncoding(ArrayRef< MCCFIInstruction > Instrs) const override
Generate compact unwind encoding for the function based on the CFI instructions.
unsigned const MachineRegisterInfo * MRI
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
The instances of the Type class are immutable: once they are created, they are never changed...
CompactUnwindEncodings
Compact unwind encoding values.
static uint32_t swapHalfWords(uint32_t Value, bool IsLittleEndian)
size_t size() const
size - Get the array size.
bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, const MCRelaxableFragment *DF, const MCAsmLayout &Layout) const override
Simple predicate for targets where !Resolved implies requiring relaxation.
MCFixupKind
Extensible enumeration to represent the type of a fixup.
static uint64_t adjustFixupValue(const MCFixup &Fixup, const MCValue &Target, uint64_t Value, MCContext &Ctx, const Triple &TheTriple, bool IsResolved)
const MCSymbolRefExpr * getSymA() const
OpType getOperation() const
const MCFixupKindInfo & getFixupKindInfo(MCFixupKind Kind) const override
Get information on a fixup kind.
int getT2SOImmVal(unsigned Arg)
getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit into a Thumb-2 shifter_oper...
void reportError(SMLoc L, const Twine &Msg)
Should this fixup kind force a 4-byte aligned effective PC value?
uint32_t getOffset() const
unsigned adjustFixupValue(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, uint64_t Value, bool IsResolved, MCContext &Ctx, const MCSubtargetInfo *STI) const
bool writeNopData(raw_ostream &OS, uint64_t Count) const override
Write an (optimal) nop sequence of Count bytes to the given output.
static wasm::ValType getType(const TargetRegisterClass *RC)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Triple - Helper class for working with autoconf configuration names.
static RegisterPass< DebugifyFunctionPass > DF("debugify-function", "Attach debug info to a function")
raw_ostream & write(unsigned char C)
unsigned getRegister() const
static unsigned getFixupKindContainerSizeBytes(unsigned Kind)
getFixupKindContainerSizeBytes - The number of bytes of the container involved in big endian...
StringRef getArchName() const
getArchName - Get the architecture (first) component of the triple.
void setOpcode(unsigned Op)
int getSOImmVal(unsigned Arg)
getSOImmVal - Given a 32-bit immediate, if it is something that can fit into an shifter_operand immed...
ArchKind parseArch(StringRef Arch)
const MCSymbol & getSymbol() const
void relaxInstruction(const MCInst &Inst, const MCSubtargetInfo &STI, MCInst &Res) const override
Relax the instruction in the given fragment to the next wider instruction.
virtual Optional< MCFixupKind > getFixupKind(StringRef Name) const
Map a relocation name used in .reloc to a fixup kind.
StringRef str()
Return a StringRef for the vector contents.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Target - Wrapper for Target specific information.
bool isThumbFunc(const MCSymbol *Func) const
Check whether a given symbol has been flagged with .thumb_func.
.code32 (X86) / .code 32 (ARM)
void handleAssemblerFlag(MCAssemblerFlag Flag) override
Handle any target-specific assembler flags. By default, do nothing.
ObjectFormatType getObjectFormat() const
getFormat - Get the object format for this triple.
static unsigned getRelaxedOpcode(const MCInst &Inst, bool is16BitMode)
Generic base class for all target subtargets.
Target independent information on a fixup kind.
ValueT lookup(const_arg_type_t< KeyT > Val) const
lookup - Return the entry for the specified key, or a default constructed value if no such entry exis...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool mayNeedRelaxation(const MCInst &Inst, const MCSubtargetInfo &STI) const override
Check whether the given instruction may need relaxation.
void applyFixup(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target, MutableArrayRef< char > Data, uint64_t Value, bool IsResolved, const MCSubtargetInfo *STI) const override
Apply the Value for given Fixup into the provided data fragment, at the offset specified by the fixup...
LLVM Value Representation.
Generic interface to target specific assembler backends.
unsigned getTargetKind() const
#define LLVM_FALLTHROUGH
LLVM_FALLTHROUGH - Mark fallthrough cases in switch statements.
This class implements an extremely fast bulk output stream that can only output to a stream...
const MCExpr * getValue() const
void addOperand(const MCOperand &Op)
StringRef - Represent a constant reference to a string, i.e.
unsigned getOpcode() const
static unsigned getFixupKindNumBytes(unsigned Kind)
getFixupKindNumBytes - The number of bytes the fixup may change.
static MCOperand createImm(int64_t Val)
static uint32_t joinHalfWords(uint32_t FirstHalf, uint32_t SecondHalf, bool IsLittleEndian)
MCFixupKind getKind() const
bool empty() const
empty - Check if the array is empty.
Optional< MCFixupKind > getFixupKind(StringRef Name) const override
Map a relocation name used in .reloc to a fixup kind.
bool shouldForceRelocation(const MCAssembler &Asm, const MCFixup &Fixup, const MCValue &Target) override
Hook to check if a relocation is needed for some target specific reason.