LLVM API Documentation

ARMAsmBackend.cpp
Go to the documentation of this file.
00001 //===-- ARMAsmBackend.cpp - ARM Assembler Backend -------------------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 
00010 #include "MCTargetDesc/ARMMCTargetDesc.h"
00011 #include "MCTargetDesc/ARMAddressingModes.h"
00012 #include "MCTargetDesc/ARMAsmBackend.h"
00013 #include "MCTargetDesc/ARMAsmBackendDarwin.h"
00014 #include "MCTargetDesc/ARMAsmBackendELF.h"
00015 #include "MCTargetDesc/ARMAsmBackendWinCOFF.h"
00016 #include "MCTargetDesc/ARMBaseInfo.h"
00017 #include "MCTargetDesc/ARMFixupKinds.h"
00018 #include "llvm/ADT/StringSwitch.h"
00019 #include "llvm/MC/MCAsmBackend.h"
00020 #include "llvm/MC/MCAssembler.h"
00021 #include "llvm/MC/MCContext.h"
00022 #include "llvm/MC/MCDirectives.h"
00023 #include "llvm/MC/MCELFObjectWriter.h"
00024 #include "llvm/MC/MCExpr.h"
00025 #include "llvm/MC/MCFixupKindInfo.h"
00026 #include "llvm/MC/MCMachObjectWriter.h"
00027 #include "llvm/MC/MCObjectWriter.h"
00028 #include "llvm/MC/MCSectionELF.h"
00029 #include "llvm/MC/MCSectionMachO.h"
00030 #include "llvm/MC/MCSubtargetInfo.h"
00031 #include "llvm/MC/MCValue.h"
00032 #include "llvm/Support/ELF.h"
00033 #include "llvm/Support/ErrorHandling.h"
00034 #include "llvm/Support/MachO.h"
00035 #include "llvm/Support/raw_ostream.h"
00036 using namespace llvm;
00037 
00038 namespace {
00039 class ARMELFObjectWriter : public MCELFObjectTargetWriter {
00040 public:
00041   ARMELFObjectWriter(uint8_t OSABI)
00042       : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
00043                                 /*HasRelocationAddend*/ false) {}
00044 };
00045 
00046 const MCFixupKindInfo &ARMAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
00047   const static MCFixupKindInfo InfosLE[ARM::NumTargetFixupKinds] = {
00048       // This table *must* be in the order that the fixup_* kinds are defined in
00049       // ARMFixupKinds.h.
00050       //
00051       // Name                      Offset (bits) Size (bits)     Flags
00052       {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00053       {"fixup_t2_ldst_pcrel_12", 0, 32,
00054        MCFixupKindInfo::FKF_IsPCRel |
00055            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00056       {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00057       {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00058       {"fixup_t2_pcrel_10", 0, 32,
00059        MCFixupKindInfo::FKF_IsPCRel |
00060            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00061       {"fixup_thumb_adr_pcrel_10", 0, 8,
00062        MCFixupKindInfo::FKF_IsPCRel |
00063            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00064       {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00065       {"fixup_t2_adr_pcrel_12", 0, 32,
00066        MCFixupKindInfo::FKF_IsPCRel |
00067            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00068       {"fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
00069       {"fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
00070       {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00071       {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00072       {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
00073       {"fixup_arm_uncondbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
00074       {"fixup_arm_condbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
00075       {"fixup_arm_blx", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
00076       {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00077       {"fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00078       {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
00079       {"fixup_arm_thumb_cp", 0, 8,
00080        MCFixupKindInfo::FKF_IsPCRel |
00081            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00082       {"fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
00083       // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16
00084       // - 19.
00085       {"fixup_arm_movt_hi16", 0, 20, 0},
00086       {"fixup_arm_movw_lo16", 0, 20, 0},
00087       {"fixup_t2_movt_hi16", 0, 20, 0},
00088       {"fixup_t2_movw_lo16", 0, 20, 0},
00089   };
00090   const static MCFixupKindInfo InfosBE[ARM::NumTargetFixupKinds] = {
00091       // This table *must* be in the order that the fixup_* kinds are defined in
00092       // ARMFixupKinds.h.
00093       //
00094       // Name                      Offset (bits) Size (bits)     Flags
00095       {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00096       {"fixup_t2_ldst_pcrel_12", 0, 32,
00097        MCFixupKindInfo::FKF_IsPCRel |
00098            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00099       {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00100       {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00101       {"fixup_t2_pcrel_10", 0, 32,
00102        MCFixupKindInfo::FKF_IsPCRel |
00103            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00104       {"fixup_thumb_adr_pcrel_10", 8, 8,
00105        MCFixupKindInfo::FKF_IsPCRel |
00106            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00107       {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00108       {"fixup_t2_adr_pcrel_12", 0, 32,
00109        MCFixupKindInfo::FKF_IsPCRel |
00110            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00111       {"fixup_arm_condbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
00112       {"fixup_arm_uncondbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
00113       {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00114       {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00115       {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
00116       {"fixup_arm_uncondbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
00117       {"fixup_arm_condbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
00118       {"fixup_arm_blx", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
00119       {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00120       {"fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
00121       {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
00122       {"fixup_arm_thumb_cp", 8, 8,
00123        MCFixupKindInfo::FKF_IsPCRel |
00124            MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
00125       {"fixup_arm_thumb_bcc", 8, 8, MCFixupKindInfo::FKF_IsPCRel},
00126       // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16
00127       // - 19.
00128       {"fixup_arm_movt_hi16", 12, 20, 0},
00129       {"fixup_arm_movw_lo16", 12, 20, 0},
00130       {"fixup_t2_movt_hi16", 12, 20, 0},
00131       {"fixup_t2_movw_lo16", 12, 20, 0},
00132   };
00133 
00134   if (Kind < FirstTargetFixupKind)
00135     return MCAsmBackend::getFixupKindInfo(Kind);
00136 
00137   assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
00138          "Invalid kind!");
00139   return (IsLittleEndian ? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
00140 }
00141 
00142 void ARMAsmBackend::handleAssemblerFlag(MCAssemblerFlag Flag) {
00143   switch (Flag) {
00144   default:
00145     break;
00146   case MCAF_Code16:
00147     setIsThumb(true);
00148     break;
00149   case MCAF_Code32:
00150     setIsThumb(false);
00151     break;
00152   }
00153 }
00154 } // end anonymous namespace
00155 
00156 static unsigned getRelaxedOpcode(unsigned Op) {
00157   switch (Op) {
00158   default:
00159     return Op;
00160   case ARM::tBcc:
00161     return ARM::t2Bcc;
00162   case ARM::tLDRpci:
00163     return ARM::t2LDRpci;
00164   case ARM::tADR:
00165     return ARM::t2ADR;
00166   case ARM::tB:
00167     return ARM::t2B;
00168   case ARM::tCBZ:
00169     return ARM::tHINT;
00170   case ARM::tCBNZ:
00171     return ARM::tHINT;
00172   }
00173 }
00174 
00175 bool ARMAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
00176   if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
00177     return true;
00178   return false;
00179 }
00180 
00181 bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
00182                                          const MCRelaxableFragment *DF,
00183                                          const MCAsmLayout &Layout) const {
00184   switch ((unsigned)Fixup.getKind()) {
00185   case ARM::fixup_arm_thumb_br: {
00186     // Relaxing tB to t2B. tB has a signed 12-bit displacement with the
00187     // low bit being an implied zero. There's an implied +4 offset for the
00188     // branch, so we adjust the other way here to determine what's
00189     // encodable.
00190     //
00191     // Relax if the value is too big for a (signed) i8.
00192     int64_t Offset = int64_t(Value) - 4;
00193     return Offset > 2046 || Offset < -2048;
00194   }
00195   case ARM::fixup_arm_thumb_bcc: {
00196     // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the
00197     // low bit being an implied zero. There's an implied +4 offset for the
00198     // branch, so we adjust the other way here to determine what's
00199     // encodable.
00200     //
00201     // Relax if the value is too big for a (signed) i8.
00202     int64_t Offset = int64_t(Value) - 4;
00203     return Offset > 254 || Offset < -256;
00204   }
00205   case ARM::fixup_thumb_adr_pcrel_10:
00206   case ARM::fixup_arm_thumb_cp: {
00207     // If the immediate is negative, greater than 1020, or not a multiple
00208     // of four, the wide version of the instruction must be used.
00209     int64_t Offset = int64_t(Value) - 4;
00210     return Offset > 1020 || Offset < 0 || Offset & 3;
00211   }
00212   case ARM::fixup_arm_thumb_cb:
00213     // If we have a Thumb CBZ or CBNZ instruction and its target is the next
00214     // instruction it is is actually out of range for the instruction.
00215     // It will be changed to a NOP.
00216     int64_t Offset = (Value & ~1);
00217     return Offset == 2;
00218   }
00219   llvm_unreachable("Unexpected fixup kind in fixupNeedsRelaxation()!");
00220 }
00221 
00222 void ARMAsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const {
00223   unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
00224 
00225   // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
00226   if (RelaxedOp == Inst.getOpcode()) {
00227     SmallString<256> Tmp;
00228     raw_svector_ostream OS(Tmp);
00229     Inst.dump_pretty(OS);
00230     OS << "\n";
00231     report_fatal_error("unexpected instruction to relax: " + OS.str());
00232   }
00233 
00234   // If we are changing Thumb CBZ or CBNZ instruction to a NOP, aka tHINT, we
00235   // have to change the operands too.
00236   if ((Inst.getOpcode() == ARM::tCBZ || Inst.getOpcode() == ARM::tCBNZ) &&
00237       RelaxedOp == ARM::tHINT) {
00238     Res.setOpcode(RelaxedOp);
00239     Res.addOperand(MCOperand::CreateImm(0));
00240     Res.addOperand(MCOperand::CreateImm(14));
00241     Res.addOperand(MCOperand::CreateReg(0));
00242     return;
00243   }
00244 
00245   // The rest of instructions we're relaxing have the same operands.
00246   // We just need to update to the proper opcode.
00247   Res = Inst;
00248   Res.setOpcode(RelaxedOp);
00249 }
00250 
00251 bool ARMAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
00252   const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
00253   const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
00254   const uint32_t ARMv4_NopEncoding = 0xe1a00000;   // using MOV r0,r0
00255   const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
00256   if (isThumb()) {
00257     const uint16_t nopEncoding =
00258         hasNOP() ? Thumb2_16bitNopEncoding : Thumb1_16bitNopEncoding;
00259     uint64_t NumNops = Count / 2;
00260     for (uint64_t i = 0; i != NumNops; ++i)
00261       OW->Write16(nopEncoding);
00262     if (Count & 1)
00263       OW->Write8(0);
00264     return true;
00265   }
00266   // ARM mode
00267   const uint32_t nopEncoding =
00268       hasNOP() ? ARMv6T2_NopEncoding : ARMv4_NopEncoding;
00269   uint64_t NumNops = Count / 4;
00270   for (uint64_t i = 0; i != NumNops; ++i)
00271     OW->Write32(nopEncoding);
00272   // FIXME: should this function return false when unable to write exactly
00273   // 'Count' bytes with NOP encodings?
00274   switch (Count % 4) {
00275   default:
00276     break; // No leftover bytes to write
00277   case 1:
00278     OW->Write8(0);
00279     break;
00280   case 2:
00281     OW->Write16(0);
00282     break;
00283   case 3:
00284     OW->Write16(0);
00285     OW->Write8(0xa0);
00286     break;
00287   }
00288 
00289   return true;
00290 }
00291 
00292 static uint32_t swapHalfWords(uint32_t Value, bool IsLittleEndian) {
00293   if (IsLittleEndian) {
00294     // Note that the halfwords are stored high first and low second in thumb;
00295     // so we need to swap the fixup value here to map properly.
00296     uint32_t Swapped = (Value & 0xFFFF0000) >> 16;
00297     Swapped |= (Value & 0x0000FFFF) << 16;
00298     return Swapped;
00299   } else
00300     return Value;
00301 }
00302 
00303 static uint32_t joinHalfWords(uint32_t FirstHalf, uint32_t SecondHalf,
00304                               bool IsLittleEndian) {
00305   uint32_t Value;
00306 
00307   if (IsLittleEndian) {
00308     Value = (SecondHalf & 0xFFFF) << 16;
00309     Value |= (FirstHalf & 0xFFFF);
00310   } else {
00311     Value = (SecondHalf & 0xFFFF);
00312     Value |= (FirstHalf & 0xFFFF) << 16;
00313   }
00314 
00315   return Value;
00316 }
00317 
00318 static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
00319                                  bool IsPCRel, MCContext *Ctx,
00320                                  bool IsLittleEndian) {
00321   unsigned Kind = Fixup.getKind();
00322   switch (Kind) {
00323   default:
00324     llvm_unreachable("Unknown fixup kind!");
00325   case FK_Data_1:
00326   case FK_Data_2:
00327   case FK_Data_4:
00328     return Value;
00329   case FK_SecRel_2:
00330     return Value;
00331   case FK_SecRel_4:
00332     return Value;
00333   case ARM::fixup_arm_movt_hi16:
00334     if (!IsPCRel)
00335       Value >>= 16;
00336   // Fallthrough
00337   case ARM::fixup_arm_movw_lo16: {
00338     unsigned Hi4 = (Value & 0xF000) >> 12;
00339     unsigned Lo12 = Value & 0x0FFF;
00340     // inst{19-16} = Hi4;
00341     // inst{11-0} = Lo12;
00342     Value = (Hi4 << 16) | (Lo12);
00343     return Value;
00344   }
00345   case ARM::fixup_t2_movt_hi16:
00346     if (!IsPCRel)
00347       Value >>= 16;
00348   // Fallthrough
00349   case ARM::fixup_t2_movw_lo16: {
00350     unsigned Hi4 = (Value & 0xF000) >> 12;
00351     unsigned i = (Value & 0x800) >> 11;
00352     unsigned Mid3 = (Value & 0x700) >> 8;
00353     unsigned Lo8 = Value & 0x0FF;
00354     // inst{19-16} = Hi4;
00355     // inst{26} = i;
00356     // inst{14-12} = Mid3;
00357     // inst{7-0} = Lo8;
00358     Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
00359     return swapHalfWords(Value, IsLittleEndian);
00360   }
00361   case ARM::fixup_arm_ldst_pcrel_12:
00362     // ARM PC-relative values are offset by 8.
00363     Value -= 4;
00364   // FALLTHROUGH
00365   case ARM::fixup_t2_ldst_pcrel_12: {
00366     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
00367     Value -= 4;
00368     bool isAdd = true;
00369     if ((int64_t)Value < 0) {
00370       Value = -Value;
00371       isAdd = false;
00372     }
00373     if (Ctx && Value >= 4096)
00374       Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
00375     Value |= isAdd << 23;
00376 
00377     // Same addressing mode as fixup_arm_pcrel_10,
00378     // but with 16-bit halfwords swapped.
00379     if (Kind == ARM::fixup_t2_ldst_pcrel_12)
00380       return swapHalfWords(Value, IsLittleEndian);
00381 
00382     return Value;
00383   }
00384   case ARM::fixup_thumb_adr_pcrel_10:
00385     return ((Value - 4) >> 2) & 0xff;
00386   case ARM::fixup_arm_adr_pcrel_12: {
00387     // ARM PC-relative values are offset by 8.
00388     Value -= 8;
00389     unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
00390     if ((int64_t)Value < 0) {
00391       Value = -Value;
00392       opc = 2; // 0b0010
00393     }
00394     if (Ctx && ARM_AM::getSOImmVal(Value) == -1)
00395       Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
00396     // Encode the immediate and shift the opcode into place.
00397     return ARM_AM::getSOImmVal(Value) | (opc << 21);
00398   }
00399 
00400   case ARM::fixup_t2_adr_pcrel_12: {
00401     Value -= 4;
00402     unsigned opc = 0;
00403     if ((int64_t)Value < 0) {
00404       Value = -Value;
00405       opc = 5;
00406     }
00407 
00408     uint32_t out = (opc << 21);
00409     out |= (Value & 0x800) << 15;
00410     out |= (Value & 0x700) << 4;
00411     out |= (Value & 0x0FF);
00412 
00413     return swapHalfWords(out, IsLittleEndian);
00414   }
00415 
00416   case ARM::fixup_arm_condbranch:
00417   case ARM::fixup_arm_uncondbranch:
00418   case ARM::fixup_arm_uncondbl:
00419   case ARM::fixup_arm_condbl:
00420   case ARM::fixup_arm_blx:
00421     // These values don't encode the low two bits since they're always zero.
00422     // Offset by 8 just as above.
00423     if (const MCSymbolRefExpr *SRE =
00424             dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
00425       if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL)
00426         return 0;
00427     return 0xffffff & ((Value - 8) >> 2);
00428   case ARM::fixup_t2_uncondbranch: {
00429     Value = Value - 4;
00430     Value >>= 1; // Low bit is not encoded.
00431 
00432     uint32_t out = 0;
00433     bool I = Value & 0x800000;
00434     bool J1 = Value & 0x400000;
00435     bool J2 = Value & 0x200000;
00436     J1 ^= I;
00437     J2 ^= I;
00438 
00439     out |= I << 26;                 // S bit
00440     out |= !J1 << 13;               // J1 bit
00441     out |= !J2 << 11;               // J2 bit
00442     out |= (Value & 0x1FF800) << 5; // imm6 field
00443     out |= (Value & 0x0007FF);      // imm11 field
00444 
00445     return swapHalfWords(out, IsLittleEndian);
00446   }
00447   case ARM::fixup_t2_condbranch: {
00448     Value = Value - 4;
00449     Value >>= 1; // Low bit is not encoded.
00450 
00451     uint64_t out = 0;
00452     out |= (Value & 0x80000) << 7; // S bit
00453     out |= (Value & 0x40000) >> 7; // J2 bit
00454     out |= (Value & 0x20000) >> 4; // J1 bit
00455     out |= (Value & 0x1F800) << 5; // imm6 field
00456     out |= (Value & 0x007FF);      // imm11 field
00457 
00458     return swapHalfWords(out, IsLittleEndian);
00459   }
00460   case ARM::fixup_arm_thumb_bl: {
00461     // The value doesn't encode the low bit (always zero) and is offset by
00462     // four. The 32-bit immediate value is encoded as
00463     //   imm32 = SignExtend(S:I1:I2:imm10:imm11:0)
00464     // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
00465     // The value is encoded into disjoint bit positions in the destination
00466     // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
00467     // J = either J1 or J2 bit
00468     //
00469     //   BL:  xxxxxSIIIIIIIIII xxJxJIIIIIIIIIII
00470     //
00471     // Note that the halfwords are stored high first, low second; so we need
00472     // to transpose the fixup value here to map properly.
00473     uint32_t offset = (Value - 4) >> 1;
00474     uint32_t signBit = (offset & 0x800000) >> 23;
00475     uint32_t I1Bit = (offset & 0x400000) >> 22;
00476     uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
00477     uint32_t I2Bit = (offset & 0x200000) >> 21;
00478     uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
00479     uint32_t imm10Bits = (offset & 0x1FF800) >> 11;
00480     uint32_t imm11Bits = (offset & 0x000007FF);
00481 
00482     uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10Bits);
00483     uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
00484                            (uint16_t)imm11Bits);
00485     return joinHalfWords(FirstHalf, SecondHalf, IsLittleEndian);
00486   }
00487   case ARM::fixup_arm_thumb_blx: {
00488     // The value doesn't encode the low two bits (always zero) and is offset by
00489     // four (see fixup_arm_thumb_cp). The 32-bit immediate value is encoded as
00490     //   imm32 = SignExtend(S:I1:I2:imm10H:imm10L:00)
00491     // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
00492     // The value is encoded into disjoint bit positions in the destination
00493     // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
00494     // J = either J1 or J2 bit, 0 = zero.
00495     //
00496     //   BLX: xxxxxSIIIIIIIIII xxJxJIIIIIIIIII0
00497     //
00498     // Note that the halfwords are stored high first, low second; so we need
00499     // to transpose the fixup value here to map properly.
00500     uint32_t offset = (Value - 2) >> 2;
00501     if (const MCSymbolRefExpr *SRE =
00502             dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
00503       if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL)
00504         offset = 0;
00505     uint32_t signBit = (offset & 0x400000) >> 22;
00506     uint32_t I1Bit = (offset & 0x200000) >> 21;
00507     uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
00508     uint32_t I2Bit = (offset & 0x100000) >> 20;
00509     uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
00510     uint32_t imm10HBits = (offset & 0xFFC00) >> 10;
00511     uint32_t imm10LBits = (offset & 0x3FF);
00512 
00513     uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10HBits);
00514     uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
00515                            ((uint16_t)imm10LBits) << 1);
00516     return joinHalfWords(FirstHalf, SecondHalf, IsLittleEndian);
00517   }
00518   case ARM::fixup_arm_thumb_cp:
00519     // Offset by 4, and don't encode the low two bits. Two bytes of that
00520     // 'off by 4' is implicitly handled by the half-word ordering of the
00521     // Thumb encoding, so we only need to adjust by 2 here.
00522     return ((Value - 2) >> 2) & 0xff;
00523   case ARM::fixup_arm_thumb_cb: {
00524     // Offset by 4 and don't encode the lower bit, which is always 0.
00525     uint32_t Binary = (Value - 4) >> 1;
00526     return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
00527   }
00528   case ARM::fixup_arm_thumb_br:
00529     // Offset by 4 and don't encode the lower bit, which is always 0.
00530     return ((Value - 4) >> 1) & 0x7ff;
00531   case ARM::fixup_arm_thumb_bcc:
00532     // Offset by 4 and don't encode the lower bit, which is always 0.
00533     return ((Value - 4) >> 1) & 0xff;
00534   case ARM::fixup_arm_pcrel_10_unscaled: {
00535     Value = Value - 8; // ARM fixups offset by an additional word and don't
00536                        // need to adjust for the half-word ordering.
00537     bool isAdd = true;
00538     if ((int64_t)Value < 0) {
00539       Value = -Value;
00540       isAdd = false;
00541     }
00542     // The value has the low 4 bits encoded in [3:0] and the high 4 in [11:8].
00543     if (Ctx && Value >= 256)
00544       Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
00545     Value = (Value & 0xf) | ((Value & 0xf0) << 4);
00546     return Value | (isAdd << 23);
00547   }
00548   case ARM::fixup_arm_pcrel_10:
00549     Value = Value - 4; // ARM fixups offset by an additional word and don't
00550                        // need to adjust for the half-word ordering.
00551                        // Fall through.
00552   case ARM::fixup_t2_pcrel_10: {
00553     // Offset by 4, adjusted by two due to the half-word ordering of thumb.
00554     Value = Value - 4;
00555     bool isAdd = true;
00556     if ((int64_t)Value < 0) {
00557       Value = -Value;
00558       isAdd = false;
00559     }
00560     // These values don't encode the low two bits since they're always zero.
00561     Value >>= 2;
00562     if (Ctx && Value >= 256)
00563       Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
00564     Value |= isAdd << 23;
00565 
00566     // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords
00567     // swapped.
00568     if (Kind == ARM::fixup_t2_pcrel_10)
00569       return swapHalfWords(Value, IsLittleEndian);
00570 
00571     return Value;
00572   }
00573   }
00574 }
00575 
00576 void ARMAsmBackend::processFixupValue(const MCAssembler &Asm,
00577                                       const MCAsmLayout &Layout,
00578                                       const MCFixup &Fixup,
00579                                       const MCFragment *DF,
00580                                       const MCValue &Target, uint64_t &Value,
00581                                       bool &IsResolved) {
00582   const MCSymbolRefExpr *A = Target.getSymA();
00583   // Some fixups to thumb function symbols need the low bit (thumb bit)
00584   // twiddled.
00585   if ((unsigned)Fixup.getKind() != ARM::fixup_arm_ldst_pcrel_12 &&
00586       (unsigned)Fixup.getKind() != ARM::fixup_t2_ldst_pcrel_12 &&
00587       (unsigned)Fixup.getKind() != ARM::fixup_arm_adr_pcrel_12 &&
00588       (unsigned)Fixup.getKind() != ARM::fixup_thumb_adr_pcrel_10 &&
00589       (unsigned)Fixup.getKind() != ARM::fixup_t2_adr_pcrel_12 &&
00590       (unsigned)Fixup.getKind() != ARM::fixup_arm_thumb_cp) {
00591     if (A) {
00592       const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
00593       if (Asm.isThumbFunc(&Sym))
00594         Value |= 1;
00595     }
00596   }
00597   // For Thumb1 BL instruction, it is possible to be a long jump between
00598   // the basic blocks of the same function.  Thus, we would like to resolve
00599   // the offset when the destination has the same MCFragment.
00600   if (A && (unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_bl) {
00601     const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
00602     const MCSymbolData &SymData = Asm.getSymbolData(Sym);
00603     IsResolved = (SymData.getFragment() == DF);
00604   }
00605   // We must always generate a relocation for BL/BLX instructions if we have
00606   // a symbol to reference, as the linker relies on knowing the destination
00607   // symbol's thumb-ness to get interworking right.
00608   if (A && ((unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_blx ||
00609             (unsigned)Fixup.getKind() == ARM::fixup_arm_blx ||
00610             (unsigned)Fixup.getKind() == ARM::fixup_arm_uncondbl ||
00611             (unsigned)Fixup.getKind() == ARM::fixup_arm_condbl))
00612     IsResolved = false;
00613 
00614   // Try to get the encoded value for the fixup as-if we're mapping it into
00615   // the instruction. This allows adjustFixupValue() to issue a diagnostic
00616   // if the value aren't invalid.
00617   (void)adjustFixupValue(Fixup, Value, false, &Asm.getContext(),
00618                          IsLittleEndian);
00619 }
00620 
00621 /// getFixupKindNumBytes - The number of bytes the fixup may change.
00622 static unsigned getFixupKindNumBytes(unsigned Kind) {
00623   switch (Kind) {
00624   default:
00625     llvm_unreachable("Unknown fixup kind!");
00626 
00627   case FK_Data_1:
00628   case ARM::fixup_arm_thumb_bcc:
00629   case ARM::fixup_arm_thumb_cp:
00630   case ARM::fixup_thumb_adr_pcrel_10:
00631     return 1;
00632 
00633   case FK_Data_2:
00634   case ARM::fixup_arm_thumb_br:
00635   case ARM::fixup_arm_thumb_cb:
00636     return 2;
00637 
00638   case ARM::fixup_arm_pcrel_10_unscaled:
00639   case ARM::fixup_arm_ldst_pcrel_12:
00640   case ARM::fixup_arm_pcrel_10:
00641   case ARM::fixup_arm_adr_pcrel_12:
00642   case ARM::fixup_arm_uncondbl:
00643   case ARM::fixup_arm_condbl:
00644   case ARM::fixup_arm_blx:
00645   case ARM::fixup_arm_condbranch:
00646   case ARM::fixup_arm_uncondbranch:
00647     return 3;
00648 
00649   case FK_Data_4:
00650   case ARM::fixup_t2_ldst_pcrel_12:
00651   case ARM::fixup_t2_condbranch:
00652   case ARM::fixup_t2_uncondbranch:
00653   case ARM::fixup_t2_pcrel_10:
00654   case ARM::fixup_t2_adr_pcrel_12:
00655   case ARM::fixup_arm_thumb_bl:
00656   case ARM::fixup_arm_thumb_blx:
00657   case ARM::fixup_arm_movt_hi16:
00658   case ARM::fixup_arm_movw_lo16:
00659   case ARM::fixup_t2_movt_hi16:
00660   case ARM::fixup_t2_movw_lo16:
00661     return 4;
00662 
00663   case FK_SecRel_2:
00664     return 2;
00665   case FK_SecRel_4:
00666     return 4;
00667   }
00668 }
00669 
00670 /// getFixupKindContainerSizeBytes - The number of bytes of the
00671 /// container involved in big endian.
00672 static unsigned getFixupKindContainerSizeBytes(unsigned Kind) {
00673   switch (Kind) {
00674   default:
00675     llvm_unreachable("Unknown fixup kind!");
00676 
00677   case FK_Data_1:
00678     return 1;
00679   case FK_Data_2:
00680     return 2;
00681   case FK_Data_4:
00682     return 4;
00683 
00684   case ARM::fixup_arm_thumb_bcc:
00685   case ARM::fixup_arm_thumb_cp:
00686   case ARM::fixup_thumb_adr_pcrel_10:
00687   case ARM::fixup_arm_thumb_br:
00688   case ARM::fixup_arm_thumb_cb:
00689     // Instruction size is 2 bytes.
00690     return 2;
00691 
00692   case ARM::fixup_arm_pcrel_10_unscaled:
00693   case ARM::fixup_arm_ldst_pcrel_12:
00694   case ARM::fixup_arm_pcrel_10:
00695   case ARM::fixup_arm_adr_pcrel_12:
00696   case ARM::fixup_arm_uncondbl:
00697   case ARM::fixup_arm_condbl:
00698   case ARM::fixup_arm_blx:
00699   case ARM::fixup_arm_condbranch:
00700   case ARM::fixup_arm_uncondbranch:
00701   case ARM::fixup_t2_ldst_pcrel_12:
00702   case ARM::fixup_t2_condbranch:
00703   case ARM::fixup_t2_uncondbranch:
00704   case ARM::fixup_t2_pcrel_10:
00705   case ARM::fixup_t2_adr_pcrel_12:
00706   case ARM::fixup_arm_thumb_bl:
00707   case ARM::fixup_arm_thumb_blx:
00708   case ARM::fixup_arm_movt_hi16:
00709   case ARM::fixup_arm_movw_lo16:
00710   case ARM::fixup_t2_movt_hi16:
00711   case ARM::fixup_t2_movw_lo16:
00712     // Instruction size is 4 bytes.
00713     return 4;
00714   }
00715 }
00716 
00717 void ARMAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
00718                                unsigned DataSize, uint64_t Value,
00719                                bool IsPCRel) const {
00720   unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
00721   Value = adjustFixupValue(Fixup, Value, IsPCRel, nullptr, IsLittleEndian);
00722   if (!Value)
00723     return; // Doesn't change encoding.
00724 
00725   unsigned Offset = Fixup.getOffset();
00726   assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
00727 
00728   // Used to point to big endian bytes.
00729   unsigned FullSizeBytes;
00730   if (!IsLittleEndian) {
00731     FullSizeBytes = getFixupKindContainerSizeBytes(Fixup.getKind());
00732     assert((Offset + FullSizeBytes) <= DataSize && "Invalid fixup size!");
00733     assert(NumBytes <= FullSizeBytes && "Invalid fixup size!");
00734   }
00735 
00736   // For each byte of the fragment that the fixup touches, mask in the bits from
00737   // the fixup value. The Value has been "split up" into the appropriate
00738   // bitfields above.
00739   for (unsigned i = 0; i != NumBytes; ++i) {
00740     unsigned Idx = IsLittleEndian ? i : (FullSizeBytes - 1 - i);
00741     Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
00742   }
00743 }
00744 
00745 MCAsmBackend *llvm::createARMAsmBackend(const Target &T,
00746                                         const MCRegisterInfo &MRI, StringRef TT,
00747                                         StringRef CPU, bool isLittle) {
00748   Triple TheTriple(TT);
00749 
00750   switch (TheTriple.getObjectFormat()) {
00751   default:
00752     llvm_unreachable("unsupported object format");
00753   case Triple::MachO: {
00754     MachO::CPUSubTypeARM CS =
00755         StringSwitch<MachO::CPUSubTypeARM>(TheTriple.getArchName())
00756             .Cases("armv4t", "thumbv4t", MachO::CPU_SUBTYPE_ARM_V4T)
00757             .Cases("armv5e", "thumbv5e", MachO::CPU_SUBTYPE_ARM_V5TEJ)
00758             .Cases("armv6", "thumbv6", MachO::CPU_SUBTYPE_ARM_V6)
00759             .Cases("armv6m", "thumbv6m", MachO::CPU_SUBTYPE_ARM_V6M)
00760             .Cases("armv7em", "thumbv7em", MachO::CPU_SUBTYPE_ARM_V7EM)
00761             .Cases("armv7k", "thumbv7k", MachO::CPU_SUBTYPE_ARM_V7K)
00762             .Cases("armv7m", "thumbv7m", MachO::CPU_SUBTYPE_ARM_V7M)
00763             .Cases("armv7s", "thumbv7s", MachO::CPU_SUBTYPE_ARM_V7S)
00764             .Default(MachO::CPU_SUBTYPE_ARM_V7);
00765 
00766     return new ARMAsmBackendDarwin(T, TT, CS);
00767   }
00768   case Triple::COFF:
00769     assert(TheTriple.isOSWindows() && "non-Windows ARM COFF is not supported");
00770     return new ARMAsmBackendWinCOFF(T, TT);
00771   case Triple::ELF:
00772     assert(TheTriple.isOSBinFormatELF() && "using ELF for non-ELF target");
00773     uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
00774     return new ARMAsmBackendELF(T, TT, OSABI, isLittle);
00775   }
00776 }
00777 
00778 MCAsmBackend *llvm::createARMLEAsmBackend(const Target &T,
00779                                           const MCRegisterInfo &MRI,
00780                                           StringRef TT, StringRef CPU) {
00781   return createARMAsmBackend(T, MRI, TT, CPU, true);
00782 }
00783 
00784 MCAsmBackend *llvm::createARMBEAsmBackend(const Target &T,
00785                                           const MCRegisterInfo &MRI,
00786                                           StringRef TT, StringRef CPU) {
00787   return createARMAsmBackend(T, MRI, TT, CPU, false);
00788 }
00789 
00790 MCAsmBackend *llvm::createThumbLEAsmBackend(const Target &T,
00791                                             const MCRegisterInfo &MRI,
00792                                             StringRef TT, StringRef CPU) {
00793   return createARMAsmBackend(T, MRI, TT, CPU, true);
00794 }
00795 
00796 MCAsmBackend *llvm::createThumbBEAsmBackend(const Target &T,
00797                                             const MCRegisterInfo &MRI,
00798                                             StringRef TT, StringRef CPU) {
00799   return createARMAsmBackend(T, MRI, TT, CPU, false);
00800 }