1 //===-- ARMAsmBackend.cpp - ARM Assembler Backend -------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #include "MCTargetDesc/ARMMCTargetDesc.h"
11 #include "MCTargetDesc/ARMAddressingModes.h"
12 #include "MCTargetDesc/ARMBaseInfo.h"
13 #include "MCTargetDesc/ARMFixupKinds.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCContext.h"
18 #include "llvm/MC/MCDirectives.h"
19 #include "llvm/MC/MCELFObjectWriter.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCFixupKindInfo.h"
22 #include "llvm/MC/MCMachObjectWriter.h"
23 #include "llvm/MC/MCObjectWriter.h"
24 #include "llvm/MC/MCSectionELF.h"
25 #include "llvm/MC/MCSectionMachO.h"
26 #include "llvm/MC/MCSubtargetInfo.h"
27 #include "llvm/MC/MCValue.h"
28 #include "llvm/Support/ELF.h"
29 #include "llvm/Support/ErrorHandling.h"
30 #include "llvm/Support/MachO.h"
31 #include "llvm/Support/raw_ostream.h"
35 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
37 ARMELFObjectWriter(uint8_t OSABI)
38 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
39 /*HasRelocationAddend*/ false) {}
42 class ARMAsmBackend : public MCAsmBackend {
43 const MCSubtargetInfo* STI;
44 bool isThumbMode; // Currently emitting Thumb code.
45 bool IsLittleEndian; // Big or little endian.
47 ARMAsmBackend(const Target &T, const StringRef TT, bool IsLittle)
48 : MCAsmBackend(), STI(ARM_MC::createARMMCSubtargetInfo(TT, "", "")),
49 isThumbMode(TT.startswith("thumb")), IsLittleEndian(IsLittle) {}
55 unsigned getNumFixupKinds() const override {
56 return ARM::NumTargetFixupKinds;
60 return (STI->getFeatureBits() & ARM::HasV6T2Ops) != 0;
63 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
64 const static MCFixupKindInfo InfosLE[ARM::NumTargetFixupKinds] = {
65 // This table *must* be in the order that the fixup_* kinds are defined in
68 // Name Offset (bits) Size (bits) Flags
69 { "fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
70 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
71 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
72 { "fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
73 { "fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
74 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
75 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
76 { "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
77 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
78 { "fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
79 { "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
80 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
81 { "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
82 { "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
83 { "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
84 { "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
85 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
86 { "fixup_arm_uncondbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
87 { "fixup_arm_condbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
88 { "fixup_arm_blx", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
89 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
90 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
91 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
92 { "fixup_arm_thumb_cp", 0, 8, MCFixupKindInfo::FKF_IsPCRel |
93 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
94 { "fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
95 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
96 { "fixup_arm_movt_hi16", 0, 20, 0 },
97 { "fixup_arm_movw_lo16", 0, 20, 0 },
98 { "fixup_t2_movt_hi16", 0, 20, 0 },
99 { "fixup_t2_movw_lo16", 0, 20, 0 },
100 { "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
101 { "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
102 { "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
103 { "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
105 const static MCFixupKindInfo InfosBE[ARM::NumTargetFixupKinds] = {
106 // This table *must* be in the order that the fixup_* kinds are defined in
109 // Name Offset (bits) Size (bits) Flags
110 { "fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
111 { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
112 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
113 { "fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
114 { "fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
115 { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
116 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
117 { "fixup_thumb_adr_pcrel_10",8, 8, MCFixupKindInfo::FKF_IsPCRel |
118 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
119 { "fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
120 { "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
121 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
122 { "fixup_arm_condbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
123 { "fixup_arm_uncondbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
124 { "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
125 { "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
126 { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
127 { "fixup_arm_uncondbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
128 { "fixup_arm_condbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
129 { "fixup_arm_blx", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
130 { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
131 { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
132 { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
133 { "fixup_arm_thumb_cp", 8, 8, MCFixupKindInfo::FKF_IsPCRel |
134 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
135 { "fixup_arm_thumb_bcc", 8, 8, MCFixupKindInfo::FKF_IsPCRel },
136 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
137 { "fixup_arm_movt_hi16", 12, 20, 0 },
138 { "fixup_arm_movw_lo16", 12, 20, 0 },
139 { "fixup_t2_movt_hi16", 12, 20, 0 },
140 { "fixup_t2_movw_lo16", 12, 20, 0 },
141 { "fixup_arm_movt_hi16_pcrel", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
142 { "fixup_arm_movw_lo16_pcrel", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
143 { "fixup_t2_movt_hi16_pcrel", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
144 { "fixup_t2_movw_lo16_pcrel", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
147 if (Kind < FirstTargetFixupKind)
148 return MCAsmBackend::getFixupKindInfo(Kind);
150 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
152 return (IsLittleEndian ? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
155 /// processFixupValue - Target hook to process the literal value of a fixup
157 void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
158 const MCFixup &Fixup, const MCFragment *DF,
159 MCValue &Target, uint64_t &Value,
160 bool &IsResolved) override;
163 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
164 uint64_t Value) const override;
166 bool mayNeedRelaxation(const MCInst &Inst) const override;
168 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
169 const MCRelaxableFragment *DF,
170 const MCAsmLayout &Layout) const override;
172 void relaxInstruction(const MCInst &Inst, MCInst &Res) const override;
174 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
176 void handleAssemblerFlag(MCAssemblerFlag Flag) override {
188 unsigned getPointerSize() const { return 4; }
189 bool isThumb() const { return isThumbMode; }
190 void setIsThumb(bool it) { isThumbMode = it; }
191 bool isLittle() const { return IsLittleEndian; }
193 } // end anonymous namespace
195 static unsigned getRelaxedOpcode(unsigned Op) {
198 case ARM::tBcc: return ARM::t2Bcc;
199 case ARM::tLDRpci: return ARM::t2LDRpci;
200 case ARM::tADR: return ARM::t2ADR;
201 case ARM::tB: return ARM::t2B;
202 case ARM::tCBZ: return ARM::tHINT;
203 case ARM::tCBNZ: return ARM::tHINT;
207 bool ARMAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
208 if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
213 bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
215 const MCRelaxableFragment *DF,
216 const MCAsmLayout &Layout) const {
217 switch ((unsigned)Fixup.getKind()) {
218 case ARM::fixup_arm_thumb_br: {
219 // Relaxing tB to t2B. tB has a signed 12-bit displacement with the
220 // low bit being an implied zero. There's an implied +4 offset for the
221 // branch, so we adjust the other way here to determine what's
224 // Relax if the value is too big for a (signed) i8.
225 int64_t Offset = int64_t(Value) - 4;
226 return Offset > 2046 || Offset < -2048;
228 case ARM::fixup_arm_thumb_bcc: {
229 // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the
230 // low bit being an implied zero. There's an implied +4 offset for the
231 // branch, so we adjust the other way here to determine what's
234 // Relax if the value is too big for a (signed) i8.
235 int64_t Offset = int64_t(Value) - 4;
236 return Offset > 254 || Offset < -256;
238 case ARM::fixup_thumb_adr_pcrel_10:
239 case ARM::fixup_arm_thumb_cp: {
240 // If the immediate is negative, greater than 1020, or not a multiple
241 // of four, the wide version of the instruction must be used.
242 int64_t Offset = int64_t(Value) - 4;
243 return Offset > 1020 || Offset < 0 || Offset & 3;
245 case ARM::fixup_arm_thumb_cb:
246 // If we have a Thumb CBZ or CBNZ instruction and its target is the next
247 // instruction it is is actually out of range for the instruction.
248 // It will be changed to a NOP.
249 int64_t Offset = (Value & ~1);
252 llvm_unreachable("Unexpected fixup kind in fixupNeedsRelaxation()!");
255 void ARMAsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const {
256 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
258 // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
259 if (RelaxedOp == Inst.getOpcode()) {
260 SmallString<256> Tmp;
261 raw_svector_ostream OS(Tmp);
262 Inst.dump_pretty(OS);
264 report_fatal_error("unexpected instruction to relax: " + OS.str());
267 // If we are changing Thumb CBZ or CBNZ instruction to a NOP, aka tHINT, we
268 // have to change the operands too.
269 if ((Inst.getOpcode() == ARM::tCBZ || Inst.getOpcode() == ARM::tCBNZ) &&
270 RelaxedOp == ARM::tHINT) {
271 Res.setOpcode(RelaxedOp);
272 Res.addOperand(MCOperand::CreateImm(0));
273 Res.addOperand(MCOperand::CreateImm(14));
274 Res.addOperand(MCOperand::CreateReg(0));
278 // The rest of instructions we're relaxing have the same operands.
279 // We just need to update to the proper opcode.
281 Res.setOpcode(RelaxedOp);
284 bool ARMAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
285 const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
286 const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
287 const uint32_t ARMv4_NopEncoding = 0xe1a00000; // using MOV r0,r0
288 const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
290 const uint16_t nopEncoding = hasNOP() ? Thumb2_16bitNopEncoding
291 : Thumb1_16bitNopEncoding;
292 uint64_t NumNops = Count / 2;
293 for (uint64_t i = 0; i != NumNops; ++i)
294 OW->Write16(nopEncoding);
300 const uint32_t nopEncoding = hasNOP() ? ARMv6T2_NopEncoding
302 uint64_t NumNops = Count / 4;
303 for (uint64_t i = 0; i != NumNops; ++i)
304 OW->Write32(nopEncoding);
305 // FIXME: should this function return false when unable to write exactly
306 // 'Count' bytes with NOP encodings?
308 default: break; // No leftover bytes to write
309 case 1: OW->Write8(0); break;
310 case 2: OW->Write16(0); break;
311 case 3: OW->Write16(0); OW->Write8(0xa0); break;
317 static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
318 MCContext *Ctx = NULL) {
319 unsigned Kind = Fixup.getKind();
322 llvm_unreachable("Unknown fixup kind!");
327 case ARM::fixup_arm_movt_hi16:
330 case ARM::fixup_arm_movw_lo16:
331 case ARM::fixup_arm_movt_hi16_pcrel:
332 case ARM::fixup_arm_movw_lo16_pcrel: {
333 unsigned Hi4 = (Value & 0xF000) >> 12;
334 unsigned Lo12 = Value & 0x0FFF;
335 // inst{19-16} = Hi4;
336 // inst{11-0} = Lo12;
337 Value = (Hi4 << 16) | (Lo12);
340 case ARM::fixup_t2_movt_hi16:
343 case ARM::fixup_t2_movw_lo16:
344 case ARM::fixup_t2_movt_hi16_pcrel: //FIXME: Shouldn't this be shifted like
345 // the other hi16 fixup?
346 case ARM::fixup_t2_movw_lo16_pcrel: {
347 unsigned Hi4 = (Value & 0xF000) >> 12;
348 unsigned i = (Value & 0x800) >> 11;
349 unsigned Mid3 = (Value & 0x700) >> 8;
350 unsigned Lo8 = Value & 0x0FF;
351 // inst{19-16} = Hi4;
353 // inst{14-12} = Mid3;
355 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
356 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
357 swapped |= (Value & 0x0000FFFF) << 16;
360 case ARM::fixup_arm_ldst_pcrel_12:
361 // ARM PC-relative values are offset by 8.
364 case ARM::fixup_t2_ldst_pcrel_12: {
365 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
368 if ((int64_t)Value < 0) {
372 if (Ctx && Value >= 4096)
373 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
374 Value |= isAdd << 23;
376 // Same addressing mode as fixup_arm_pcrel_10,
377 // but with 16-bit halfwords swapped.
378 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
379 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
380 swapped |= (Value & 0x0000FFFF) << 16;
386 case ARM::fixup_thumb_adr_pcrel_10:
387 return ((Value - 4) >> 2) & 0xff;
388 case ARM::fixup_arm_adr_pcrel_12: {
389 // ARM PC-relative values are offset by 8.
391 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
392 if ((int64_t)Value < 0) {
396 if (Ctx && ARM_AM::getSOImmVal(Value) == -1)
397 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
398 // Encode the immediate and shift the opcode into place.
399 return ARM_AM::getSOImmVal(Value) | (opc << 21);
402 case ARM::fixup_t2_adr_pcrel_12: {
405 if ((int64_t)Value < 0) {
410 uint32_t out = (opc << 21);
411 out |= (Value & 0x800) << 15;
412 out |= (Value & 0x700) << 4;
413 out |= (Value & 0x0FF);
415 uint64_t swapped = (out & 0xFFFF0000) >> 16;
416 swapped |= (out & 0x0000FFFF) << 16;
420 case ARM::fixup_arm_condbranch:
421 case ARM::fixup_arm_uncondbranch:
422 case ARM::fixup_arm_uncondbl:
423 case ARM::fixup_arm_condbl:
424 case ARM::fixup_arm_blx:
425 // These values don't encode the low two bits since they're always zero.
426 // Offset by 8 just as above.
427 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
428 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL)
430 return 0xffffff & ((Value - 8) >> 2);
431 case ARM::fixup_t2_uncondbranch: {
433 Value >>= 1; // Low bit is not encoded.
436 bool I = Value & 0x800000;
437 bool J1 = Value & 0x400000;
438 bool J2 = Value & 0x200000;
442 out |= I << 26; // S bit
443 out |= !J1 << 13; // J1 bit
444 out |= !J2 << 11; // J2 bit
445 out |= (Value & 0x1FF800) << 5; // imm6 field
446 out |= (Value & 0x0007FF); // imm11 field
448 uint64_t swapped = (out & 0xFFFF0000) >> 16;
449 swapped |= (out & 0x0000FFFF) << 16;
452 case ARM::fixup_t2_condbranch: {
454 Value >>= 1; // Low bit is not encoded.
457 out |= (Value & 0x80000) << 7; // S bit
458 out |= (Value & 0x40000) >> 7; // J2 bit
459 out |= (Value & 0x20000) >> 4; // J1 bit
460 out |= (Value & 0x1F800) << 5; // imm6 field
461 out |= (Value & 0x007FF); // imm11 field
463 uint32_t swapped = (out & 0xFFFF0000) >> 16;
464 swapped |= (out & 0x0000FFFF) << 16;
467 case ARM::fixup_arm_thumb_bl: {
468 // The value doesn't encode the low bit (always zero) and is offset by
469 // four. The 32-bit immediate value is encoded as
470 // imm32 = SignExtend(S:I1:I2:imm10:imm11:0)
471 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
472 // The value is encoded into disjoint bit positions in the destination
473 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
474 // J = either J1 or J2 bit
476 // BL: xxxxxSIIIIIIIIII xxJxJIIIIIIIIIII
478 // Note that the halfwords are stored high first, low second; so we need
479 // to transpose the fixup value here to map properly.
480 uint32_t offset = (Value - 4) >> 1;
481 uint32_t signBit = (offset & 0x800000) >> 23;
482 uint32_t I1Bit = (offset & 0x400000) >> 22;
483 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
484 uint32_t I2Bit = (offset & 0x200000) >> 21;
485 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
486 uint32_t imm10Bits = (offset & 0x1FF800) >> 11;
487 uint32_t imm11Bits = (offset & 0x000007FF);
490 uint32_t firstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10Bits);
491 uint32_t secondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
492 (uint16_t)imm11Bits);
493 Binary |= secondHalf << 16;
497 case ARM::fixup_arm_thumb_blx: {
498 // The value doesn't encode the low two bits (always zero) and is offset by
499 // four (see fixup_arm_thumb_cp). The 32-bit immediate value is encoded as
500 // imm32 = SignExtend(S:I1:I2:imm10H:imm10L:00)
501 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
502 // The value is encoded into disjoint bit positions in the destination
503 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
504 // J = either J1 or J2 bit, 0 = zero.
506 // BLX: xxxxxSIIIIIIIIII xxJxJIIIIIIIIII0
508 // Note that the halfwords are stored high first, low second; so we need
509 // to transpose the fixup value here to map properly.
510 uint32_t offset = (Value - 2) >> 2;
511 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
512 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL)
514 uint32_t signBit = (offset & 0x400000) >> 22;
515 uint32_t I1Bit = (offset & 0x200000) >> 21;
516 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
517 uint32_t I2Bit = (offset & 0x100000) >> 20;
518 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
519 uint32_t imm10HBits = (offset & 0xFFC00) >> 10;
520 uint32_t imm10LBits = (offset & 0x3FF);
523 uint32_t firstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10HBits);
524 uint32_t secondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
525 ((uint16_t)imm10LBits) << 1);
526 Binary |= secondHalf << 16;
530 case ARM::fixup_arm_thumb_cp:
531 // Offset by 4, and don't encode the low two bits. Two bytes of that
532 // 'off by 4' is implicitly handled by the half-word ordering of the
533 // Thumb encoding, so we only need to adjust by 2 here.
534 return ((Value - 2) >> 2) & 0xff;
535 case ARM::fixup_arm_thumb_cb: {
536 // Offset by 4 and don't encode the lower bit, which is always 0.
537 uint32_t Binary = (Value - 4) >> 1;
538 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
540 case ARM::fixup_arm_thumb_br:
541 // Offset by 4 and don't encode the lower bit, which is always 0.
542 return ((Value - 4) >> 1) & 0x7ff;
543 case ARM::fixup_arm_thumb_bcc:
544 // Offset by 4 and don't encode the lower bit, which is always 0.
545 return ((Value - 4) >> 1) & 0xff;
546 case ARM::fixup_arm_pcrel_10_unscaled: {
547 Value = Value - 8; // ARM fixups offset by an additional word and don't
548 // need to adjust for the half-word ordering.
550 if ((int64_t)Value < 0) {
554 // The value has the low 4 bits encoded in [3:0] and the high 4 in [11:8].
555 if (Ctx && Value >= 256)
556 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
557 Value = (Value & 0xf) | ((Value & 0xf0) << 4);
558 return Value | (isAdd << 23);
560 case ARM::fixup_arm_pcrel_10:
561 Value = Value - 4; // ARM fixups offset by an additional word and don't
562 // need to adjust for the half-word ordering.
564 case ARM::fixup_t2_pcrel_10: {
565 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
568 if ((int64_t)Value < 0) {
572 // These values don't encode the low two bits since they're always zero.
574 if (Ctx && Value >= 256)
575 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
576 Value |= isAdd << 23;
578 // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords
580 if (Kind == ARM::fixup_t2_pcrel_10) {
581 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
582 swapped |= (Value & 0x0000FFFF) << 16;
591 void ARMAsmBackend::processFixupValue(const MCAssembler &Asm,
592 const MCAsmLayout &Layout,
593 const MCFixup &Fixup,
594 const MCFragment *DF,
595 MCValue &Target, uint64_t &Value,
597 const MCSymbolRefExpr *A = Target.getSymA();
598 // Some fixups to thumb function symbols need the low bit (thumb bit)
600 if ((unsigned)Fixup.getKind() != ARM::fixup_arm_ldst_pcrel_12 &&
601 (unsigned)Fixup.getKind() != ARM::fixup_t2_ldst_pcrel_12 &&
602 (unsigned)Fixup.getKind() != ARM::fixup_arm_adr_pcrel_12 &&
603 (unsigned)Fixup.getKind() != ARM::fixup_thumb_adr_pcrel_10 &&
604 (unsigned)Fixup.getKind() != ARM::fixup_t2_adr_pcrel_12 &&
605 (unsigned)Fixup.getKind() != ARM::fixup_arm_thumb_cp) {
607 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
608 if (Asm.isThumbFunc(&Sym))
612 // For Thumb1 BL instruction, it is possible to be a long jump between
613 // the basic blocks of the same function. Thus, we would like to resolve
614 // the offset when the destination has the same MCFragment.
615 if (A && (unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_bl) {
616 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
617 MCSymbolData &SymData = Asm.getSymbolData(Sym);
618 IsResolved = (SymData.getFragment() == DF);
620 // We must always generate a relocation for BL/BLX instructions if we have
621 // a symbol to reference, as the linker relies on knowing the destination
622 // symbol's thumb-ness to get interworking right.
623 if (A && ((unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_blx ||
624 (unsigned)Fixup.getKind() == ARM::fixup_arm_blx ||
625 (unsigned)Fixup.getKind() == ARM::fixup_arm_uncondbl ||
626 (unsigned)Fixup.getKind() == ARM::fixup_arm_condbl))
629 // Try to get the encoded value for the fixup as-if we're mapping it into
630 // the instruction. This allows adjustFixupValue() to issue a diagnostic
631 // if the value aren't invalid.
632 (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
635 /// getFixupKindNumBytes - The number of bytes the fixup may change.
636 static unsigned getFixupKindNumBytes(unsigned Kind) {
639 llvm_unreachable("Unknown fixup kind!");
642 case ARM::fixup_arm_thumb_bcc:
643 case ARM::fixup_arm_thumb_cp:
644 case ARM::fixup_thumb_adr_pcrel_10:
648 case ARM::fixup_arm_thumb_br:
649 case ARM::fixup_arm_thumb_cb:
652 case ARM::fixup_arm_pcrel_10_unscaled:
653 case ARM::fixup_arm_ldst_pcrel_12:
654 case ARM::fixup_arm_pcrel_10:
655 case ARM::fixup_arm_adr_pcrel_12:
656 case ARM::fixup_arm_uncondbl:
657 case ARM::fixup_arm_condbl:
658 case ARM::fixup_arm_blx:
659 case ARM::fixup_arm_condbranch:
660 case ARM::fixup_arm_uncondbranch:
664 case ARM::fixup_t2_ldst_pcrel_12:
665 case ARM::fixup_t2_condbranch:
666 case ARM::fixup_t2_uncondbranch:
667 case ARM::fixup_t2_pcrel_10:
668 case ARM::fixup_t2_adr_pcrel_12:
669 case ARM::fixup_arm_thumb_bl:
670 case ARM::fixup_arm_thumb_blx:
671 case ARM::fixup_arm_movt_hi16:
672 case ARM::fixup_arm_movw_lo16:
673 case ARM::fixup_arm_movt_hi16_pcrel:
674 case ARM::fixup_arm_movw_lo16_pcrel:
675 case ARM::fixup_t2_movt_hi16:
676 case ARM::fixup_t2_movw_lo16:
677 case ARM::fixup_t2_movt_hi16_pcrel:
678 case ARM::fixup_t2_movw_lo16_pcrel:
683 /// getFixupKindContainerSizeBytes - The number of bytes of the
684 /// container involved in big endian.
685 static unsigned getFixupKindContainerSizeBytes(unsigned Kind) {
688 llvm_unreachable("Unknown fixup kind!");
697 case ARM::fixup_arm_thumb_bcc:
698 case ARM::fixup_arm_thumb_cp:
699 case ARM::fixup_thumb_adr_pcrel_10:
700 case ARM::fixup_arm_thumb_br:
701 case ARM::fixup_arm_thumb_cb:
702 // Instruction size is 2 bytes.
705 case ARM::fixup_arm_pcrel_10_unscaled:
706 case ARM::fixup_arm_ldst_pcrel_12:
707 case ARM::fixup_arm_pcrel_10:
708 case ARM::fixup_arm_adr_pcrel_12:
709 case ARM::fixup_arm_uncondbl:
710 case ARM::fixup_arm_condbl:
711 case ARM::fixup_arm_blx:
712 case ARM::fixup_arm_condbranch:
713 case ARM::fixup_arm_uncondbranch:
714 case ARM::fixup_t2_ldst_pcrel_12:
715 case ARM::fixup_t2_condbranch:
716 case ARM::fixup_t2_uncondbranch:
717 case ARM::fixup_t2_pcrel_10:
718 case ARM::fixup_t2_adr_pcrel_12:
719 case ARM::fixup_arm_thumb_bl:
720 case ARM::fixup_arm_thumb_blx:
721 case ARM::fixup_arm_movt_hi16:
722 case ARM::fixup_arm_movw_lo16:
723 case ARM::fixup_arm_movt_hi16_pcrel:
724 case ARM::fixup_arm_movw_lo16_pcrel:
725 case ARM::fixup_t2_movt_hi16:
726 case ARM::fixup_t2_movw_lo16:
727 case ARM::fixup_t2_movt_hi16_pcrel:
728 case ARM::fixup_t2_movw_lo16_pcrel:
729 // Instruction size is 4 bytes.
734 void ARMAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
735 unsigned DataSize, uint64_t Value) const {
736 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
737 Value = adjustFixupValue(Fixup, Value);
738 if (!Value) return; // Doesn't change encoding.
740 unsigned Offset = Fixup.getOffset();
741 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
743 // Used to point to big endian bytes.
744 unsigned FullSizeBytes;
746 FullSizeBytes = getFixupKindContainerSizeBytes(Fixup.getKind());
748 // For each byte of the fragment that the fixup touches, mask in the bits from
749 // the fixup value. The Value has been "split up" into the appropriate
751 for (unsigned i = 0; i != NumBytes; ++i) {
752 unsigned Idx = IsLittleEndian ? i : (FullSizeBytes - 1 - i);
753 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
759 // FIXME: This should be in a separate file.
760 // ELF is an ELF of course...
761 class ELFARMAsmBackend : public ARMAsmBackend {
764 ELFARMAsmBackend(const Target &T, const StringRef TT,
765 uint8_t _OSABI, bool _IsLittle)
766 : ARMAsmBackend(T, TT, _IsLittle), OSABI(_OSABI) { }
768 MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
769 return createARMELFObjectWriter(OS, OSABI, isLittle());
773 // FIXME: This should be in a separate file.
774 class DarwinARMAsmBackend : public ARMAsmBackend {
776 const MachO::CPUSubTypeARM Subtype;
777 DarwinARMAsmBackend(const Target &T, const StringRef TT,
778 MachO::CPUSubTypeARM st)
779 : ARMAsmBackend(T, TT, /* IsLittleEndian */ true), Subtype(st) {
780 HasDataInCodeSupport = true;
783 MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
784 return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
790 } // end anonymous namespace
792 MCAsmBackend *llvm::createARMAsmBackend(const Target &T,
793 const MCRegisterInfo &MRI,
794 StringRef TT, StringRef CPU,
796 Triple TheTriple(TT);
798 if (TheTriple.isOSBinFormatMachO()) {
799 MachO::CPUSubTypeARM CS =
800 StringSwitch<MachO::CPUSubTypeARM>(TheTriple.getArchName())
801 .Cases("armv4t", "thumbv4t", MachO::CPU_SUBTYPE_ARM_V4T)
802 .Cases("armv5e", "thumbv5e", MachO::CPU_SUBTYPE_ARM_V5TEJ)
803 .Cases("armv6", "thumbv6", MachO::CPU_SUBTYPE_ARM_V6)
804 .Cases("armv6m", "thumbv6m", MachO::CPU_SUBTYPE_ARM_V6M)
805 .Cases("armv7em", "thumbv7em", MachO::CPU_SUBTYPE_ARM_V7EM)
806 .Cases("armv7k", "thumbv7k", MachO::CPU_SUBTYPE_ARM_V7K)
807 .Cases("armv7m", "thumbv7m", MachO::CPU_SUBTYPE_ARM_V7M)
808 .Cases("armv7s", "thumbv7s", MachO::CPU_SUBTYPE_ARM_V7S)
809 .Default(MachO::CPU_SUBTYPE_ARM_V7);
811 return new DarwinARMAsmBackend(T, TT, CS);
815 // FIXME: Introduce yet another checker but assert(0).
816 if (TheTriple.isOSBinFormatCOFF())
817 assert(0 && "Windows not supported on ARM");
820 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
821 return new ELFARMAsmBackend(T, TT, OSABI, isLittle);
824 MCAsmBackend *llvm::createARMleAsmBackend(const Target &T,
825 const MCRegisterInfo &MRI,
826 StringRef TT, StringRef CPU) {
827 return createARMAsmBackend(T, MRI, TT, CPU, true);
830 MCAsmBackend *llvm::createARMbeAsmBackend(const Target &T,
831 const MCRegisterInfo &MRI,
832 StringRef TT, StringRef CPU) {
833 return createARMAsmBackend(T, MRI, TT, CPU, false);
836 MCAsmBackend *llvm::createThumbleAsmBackend(const Target &T,
837 const MCRegisterInfo &MRI,
838 StringRef TT, StringRef CPU) {
839 return createARMAsmBackend(T, MRI, TT, CPU, true);
842 MCAsmBackend *llvm::createThumbbeAsmBackend(const Target &T,
843 const MCRegisterInfo &MRI,
844 StringRef TT, StringRef CPU) {
845 return createARMAsmBackend(T, MRI, TT, CPU, false);