| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 1 | //===-- ARMAsmBackend.cpp - ARM Assembler Backend -------------------------===// | 
|  | 2 | // | 
|  | 3 | //                     The LLVM Compiler Infrastructure | 
|  | 4 | // | 
|  | 5 | // This file is distributed under the University of Illinois Open Source | 
|  | 6 | // License. See LICENSE.TXT for details. | 
|  | 7 | // | 
|  | 8 | //===----------------------------------------------------------------------===// | 
|  | 9 |  | 
| Jim Grosbach | 45e50d8 | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/ARMMCTargetDesc.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/ARMAddressingModes.h" | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 12 | #include "MCTargetDesc/ARMAsmBackend.h" | 
|  | 13 | #include "MCTargetDesc/ARMAsmBackendDarwin.h" | 
|  | 14 | #include "MCTargetDesc/ARMAsmBackendELF.h" | 
|  | 15 | #include "MCTargetDesc/ARMAsmBackendWinCOFF.h" | 
| Evan Cheng | ad5f485 | 2011-07-23 00:00:19 +0000 | [diff] [blame] | 16 | #include "MCTargetDesc/ARMBaseInfo.h" | 
|  | 17 | #include "MCTargetDesc/ARMFixupKinds.h" | 
| Quentin Colombet | 77ca8b8 | 2013-01-14 21:34:09 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/StringSwitch.h" | 
| Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCAsmBackend.h" | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAssembler.h" | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCContext.h" | 
| Jim Grosbach | 87055ed | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 22 | #include "llvm/MC/MCDirectives.h" | 
| Rafael Espindola | f0e24d4 | 2010-12-17 16:59:53 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCELFObjectWriter.h" | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCExpr.h" | 
| Craig Topper | 6e80c28 | 2012-03-26 06:58:25 +0000 | [diff] [blame] | 25 | #include "llvm/MC/MCFixupKindInfo.h" | 
| Daniel Dunbar | 73b8713 | 2010-12-16 16:08:33 +0000 | [diff] [blame] | 26 | #include "llvm/MC/MCMachObjectWriter.h" | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCObjectWriter.h" | 
| Tim Northover | f8e47e4 | 2015-10-28 22:56:36 +0000 | [diff] [blame] | 28 | #include "llvm/MC/MCRegisterInfo.h" | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 29 | #include "llvm/MC/MCSectionELF.h" | 
|  | 30 | #include "llvm/MC/MCSectionMachO.h" | 
| Jim Grosbach | 45e50d8 | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 31 | #include "llvm/MC/MCSubtargetInfo.h" | 
| Jim Grosbach | 3b50c9e | 2012-01-18 00:23:57 +0000 | [diff] [blame] | 32 | #include "llvm/MC/MCValue.h" | 
| Tim Northover | f8e47e4 | 2015-10-28 22:56:36 +0000 | [diff] [blame] | 33 | #include "llvm/Support/Debug.h" | 
| Wesley Peck | 1851090 | 2010-10-22 15:52:49 +0000 | [diff] [blame] | 34 | #include "llvm/Support/ELF.h" | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 35 | #include "llvm/Support/ErrorHandling.h" | 
| Tim Northover | f8e47e4 | 2015-10-28 22:56:36 +0000 | [diff] [blame] | 36 | #include "llvm/Support/Format.h" | 
| Charles Davis | 8bdfafd | 2013-09-01 04:28:48 +0000 | [diff] [blame] | 37 | #include "llvm/Support/MachO.h" | 
| Vedant Kumar | 366dd9fd | 2015-08-21 21:52:48 +0000 | [diff] [blame] | 38 | #include "llvm/Support/TargetParser.h" | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 39 | #include "llvm/Support/raw_ostream.h" | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 40 | using namespace llvm; | 
|  | 41 |  | 
|  | 42 | namespace { | 
| Rafael Espindola | 6b5e56c | 2010-12-17 17:45:22 +0000 | [diff] [blame] | 43 | class ARMELFObjectWriter : public MCELFObjectTargetWriter { | 
|  | 44 | public: | 
| Rafael Espindola | 1ad4095 | 2011-12-21 17:00:36 +0000 | [diff] [blame] | 45 | ARMELFObjectWriter(uint8_t OSABI) | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 46 | : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM, | 
|  | 47 | /*HasRelocationAddend*/ false) {} | 
| Rafael Espindola | 6b5e56c | 2010-12-17 17:45:22 +0000 | [diff] [blame] | 48 | }; | 
|  | 49 |  | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 50 | const MCFixupKindInfo &ARMAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { | 
|  | 51 | const static MCFixupKindInfo InfosLE[ARM::NumTargetFixupKinds] = { | 
|  | 52 | // This table *must* be in the order that the fixup_* kinds are defined in | 
|  | 53 | // ARMFixupKinds.h. | 
|  | 54 | // | 
|  | 55 | // Name                      Offset (bits) Size (bits)     Flags | 
|  | 56 | {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 57 | {"fixup_t2_ldst_pcrel_12", 0, 32, | 
|  | 58 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 59 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
|  | 60 | {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 61 | {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 62 | {"fixup_t2_pcrel_10", 0, 32, | 
|  | 63 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 64 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
| Oliver Stannard | 65b8538 | 2016-01-25 10:26:26 +0000 | [diff] [blame] | 65 | {"fixup_arm_pcrel_9", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 66 | {"fixup_t2_pcrel_9", 0, 32, | 
|  | 67 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 68 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 69 | {"fixup_thumb_adr_pcrel_10", 0, 8, | 
|  | 70 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 71 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
|  | 72 | {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 73 | {"fixup_t2_adr_pcrel_12", 0, 32, | 
|  | 74 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 75 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
|  | 76 | {"fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 77 | {"fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 78 | {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 79 | {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 80 | {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 81 | {"fixup_arm_uncondbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 82 | {"fixup_arm_condbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 83 | {"fixup_arm_blx", 0, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 84 | {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 85 | {"fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 86 | {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 87 | {"fixup_arm_thumb_cp", 0, 8, | 
|  | 88 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 89 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
|  | 90 | {"fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 91 | // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 | 
|  | 92 | // - 19. | 
|  | 93 | {"fixup_arm_movt_hi16", 0, 20, 0}, | 
|  | 94 | {"fixup_arm_movw_lo16", 0, 20, 0}, | 
|  | 95 | {"fixup_t2_movt_hi16", 0, 20, 0}, | 
|  | 96 | {"fixup_t2_movw_lo16", 0, 20, 0}, | 
|  | 97 | }; | 
|  | 98 | const static MCFixupKindInfo InfosBE[ARM::NumTargetFixupKinds] = { | 
|  | 99 | // This table *must* be in the order that the fixup_* kinds are defined in | 
|  | 100 | // ARMFixupKinds.h. | 
|  | 101 | // | 
|  | 102 | // Name                      Offset (bits) Size (bits)     Flags | 
|  | 103 | {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 104 | {"fixup_t2_ldst_pcrel_12", 0, 32, | 
|  | 105 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 106 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
|  | 107 | {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 108 | {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 109 | {"fixup_t2_pcrel_10", 0, 32, | 
|  | 110 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 111 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
| Oliver Stannard | 65b8538 | 2016-01-25 10:26:26 +0000 | [diff] [blame] | 112 | {"fixup_arm_pcrel_9", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 113 | {"fixup_t2_pcrel_9", 0, 32, | 
|  | 114 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 115 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 116 | {"fixup_thumb_adr_pcrel_10", 8, 8, | 
|  | 117 | 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, | 
|  | 121 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 122 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
|  | 123 | {"fixup_arm_condbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 124 | {"fixup_arm_uncondbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 125 | {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 126 | {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 127 | {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 128 | {"fixup_arm_uncondbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 129 | {"fixup_arm_condbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 130 | {"fixup_arm_blx", 8, 24, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 131 | {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 132 | {"fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 133 | {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 134 | {"fixup_arm_thumb_cp", 8, 8, | 
|  | 135 | MCFixupKindInfo::FKF_IsPCRel | | 
|  | 136 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, | 
|  | 137 | {"fixup_arm_thumb_bcc", 8, 8, MCFixupKindInfo::FKF_IsPCRel}, | 
|  | 138 | // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 | 
|  | 139 | // - 19. | 
|  | 140 | {"fixup_arm_movt_hi16", 12, 20, 0}, | 
|  | 141 | {"fixup_arm_movw_lo16", 12, 20, 0}, | 
|  | 142 | {"fixup_t2_movt_hi16", 12, 20, 0}, | 
|  | 143 | {"fixup_t2_movw_lo16", 12, 20, 0}, | 
|  | 144 | }; | 
| Jim Grosbach | 45e50d8 | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 145 |  | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 146 | if (Kind < FirstTargetFixupKind) | 
|  | 147 | return MCAsmBackend::getFixupKindInfo(Kind); | 
|  | 148 |  | 
|  | 149 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && | 
|  | 150 | "Invalid kind!"); | 
|  | 151 | return (IsLittleEndian ? InfosLE : InfosBE)[Kind - FirstTargetFixupKind]; | 
|  | 152 | } | 
|  | 153 |  | 
|  | 154 | void ARMAsmBackend::handleAssemblerFlag(MCAssemblerFlag Flag) { | 
|  | 155 | switch (Flag) { | 
|  | 156 | default: | 
|  | 157 | break; | 
|  | 158 | case MCAF_Code16: | 
|  | 159 | setIsThumb(true); | 
|  | 160 | break; | 
|  | 161 | case MCAF_Code32: | 
|  | 162 | setIsThumb(false); | 
|  | 163 | break; | 
| Jim Grosbach | 45e50d8 | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 164 | } | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 165 | } | 
| Chris Lattner | 9fdd10d | 2010-11-17 05:41:32 +0000 | [diff] [blame] | 166 | } // end anonymous namespace | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 167 |  | 
| Tim Northover | 4233557 | 2015-04-06 18:44:42 +0000 | [diff] [blame] | 168 | unsigned ARMAsmBackend::getRelaxedOpcode(unsigned Op) const { | 
| Michael Kuperstein | db0712f | 2015-05-26 10:47:10 +0000 | [diff] [blame] | 169 | bool HasThumb2 = STI->getFeatureBits()[ARM::FeatureThumb2]; | 
| Bradley Smith | a118910 | 2016-01-15 10:26:17 +0000 | [diff] [blame] | 170 | bool HasV8MBaselineOps = STI->getFeatureBits()[ARM::HasV8MBaselineOps]; | 
| Tim Northover | 4233557 | 2015-04-06 18:44:42 +0000 | [diff] [blame] | 171 |  | 
| Jim Grosbach | 34a7c6d | 2011-12-05 23:45:46 +0000 | [diff] [blame] | 172 | switch (Op) { | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 173 | default: | 
|  | 174 | return Op; | 
|  | 175 | case ARM::tBcc: | 
| Aaron Ballman | ac33624 | 2015-04-07 13:28:37 +0000 | [diff] [blame] | 176 | return HasThumb2 ? (unsigned)ARM::t2Bcc : Op; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 177 | case ARM::tLDRpci: | 
| Aaron Ballman | ac33624 | 2015-04-07 13:28:37 +0000 | [diff] [blame] | 178 | return HasThumb2 ? (unsigned)ARM::t2LDRpci : Op; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 179 | case ARM::tADR: | 
| Aaron Ballman | ac33624 | 2015-04-07 13:28:37 +0000 | [diff] [blame] | 180 | return HasThumb2 ? (unsigned)ARM::t2ADR : Op; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 181 | case ARM::tB: | 
| Bradley Smith | a118910 | 2016-01-15 10:26:17 +0000 | [diff] [blame] | 182 | return HasV8MBaselineOps ? (unsigned)ARM::t2B : Op; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 183 | case ARM::tCBZ: | 
|  | 184 | return ARM::tHINT; | 
|  | 185 | case ARM::tCBNZ: | 
|  | 186 | return ARM::tHINT; | 
| Jim Grosbach | 34a7c6d | 2011-12-05 23:45:46 +0000 | [diff] [blame] | 187 | } | 
|  | 188 | } | 
|  | 189 |  | 
| Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 190 | bool ARMAsmBackend::mayNeedRelaxation(const MCInst &Inst) const { | 
| Jim Grosbach | 34a7c6d | 2011-12-05 23:45:46 +0000 | [diff] [blame] | 191 | if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode()) | 
|  | 192 | return true; | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 193 | return false; | 
|  | 194 | } | 
|  | 195 |  | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 196 | const char *ARMAsmBackend::reasonForFixupRelaxation(const MCFixup &Fixup, | 
|  | 197 | uint64_t Value) const { | 
| Benjamin Kramer | 116e99a | 2012-01-19 21:11:13 +0000 | [diff] [blame] | 198 | switch ((unsigned)Fixup.getKind()) { | 
| Jim Grosbach | c4aa60f | 2012-03-19 21:32:32 +0000 | [diff] [blame] | 199 | case ARM::fixup_arm_thumb_br: { | 
|  | 200 | // Relaxing tB to t2B. tB has a signed 12-bit displacement with the | 
|  | 201 | // low bit being an implied zero. There's an implied +4 offset for the | 
|  | 202 | // branch, so we adjust the other way here to determine what's | 
|  | 203 | // encodable. | 
|  | 204 | // | 
|  | 205 | // Relax if the value is too big for a (signed) i8. | 
|  | 206 | int64_t Offset = int64_t(Value) - 4; | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 207 | if (Offset > 2046 || Offset < -2048) | 
|  | 208 | return "out of range pc-relative fixup value"; | 
|  | 209 | break; | 
| Jim Grosbach | c4aa60f | 2012-03-19 21:32:32 +0000 | [diff] [blame] | 210 | } | 
| Jim Grosbach | cb80eb2 | 2012-01-18 21:54:16 +0000 | [diff] [blame] | 211 | case ARM::fixup_arm_thumb_bcc: { | 
|  | 212 | // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the | 
|  | 213 | // low bit being an implied zero. There's an implied +4 offset for the | 
|  | 214 | // branch, so we adjust the other way here to determine what's | 
|  | 215 | // encodable. | 
|  | 216 | // | 
|  | 217 | // Relax if the value is too big for a (signed) i8. | 
|  | 218 | int64_t Offset = int64_t(Value) - 4; | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 219 | if (Offset > 254 || Offset < -256) | 
|  | 220 | return "out of range pc-relative fixup value"; | 
|  | 221 | break; | 
| Jim Grosbach | cb80eb2 | 2012-01-18 21:54:16 +0000 | [diff] [blame] | 222 | } | 
| Jim Grosbach | 44e5c39 | 2012-01-19 02:09:38 +0000 | [diff] [blame] | 223 | case ARM::fixup_thumb_adr_pcrel_10: | 
| Jim Grosbach | cb80eb2 | 2012-01-18 21:54:16 +0000 | [diff] [blame] | 224 | case ARM::fixup_arm_thumb_cp: { | 
| Jim Grosbach | b008df4 | 2012-01-19 01:50:30 +0000 | [diff] [blame] | 225 | // If the immediate is negative, greater than 1020, or not a multiple | 
|  | 226 | // of four, the wide version of the instruction must be used. | 
| Jim Grosbach | cb80eb2 | 2012-01-18 21:54:16 +0000 | [diff] [blame] | 227 | int64_t Offset = int64_t(Value) - 4; | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 228 | if (Offset & 3) | 
|  | 229 | return "misaligned pc-relative fixup value"; | 
|  | 230 | else if (Offset > 1020 || Offset < 0) | 
|  | 231 | return "out of range pc-relative fixup value"; | 
|  | 232 | break; | 
| Jim Grosbach | cb80eb2 | 2012-01-18 21:54:16 +0000 | [diff] [blame] | 233 | } | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 234 | case ARM::fixup_arm_thumb_cb: { | 
| Kevin Enderby | 9bd296a | 2014-01-10 00:43:32 +0000 | [diff] [blame] | 235 | // If we have a Thumb CBZ or CBNZ instruction and its target is the next | 
|  | 236 | // instruction it is is actually out of range for the instruction. | 
|  | 237 | // It will be changed to a NOP. | 
|  | 238 | int64_t Offset = (Value & ~1); | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 239 | if (Offset == 2) | 
|  | 240 | return "will be converted to nop"; | 
|  | 241 | break; | 
| Jim Grosbach | cb80eb2 | 2012-01-18 21:54:16 +0000 | [diff] [blame] | 242 | } | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 243 | default: | 
|  | 244 | llvm_unreachable("Unexpected fixup kind in reasonForFixupRelaxation()!"); | 
|  | 245 | } | 
|  | 246 | return nullptr; | 
|  | 247 | } | 
|  | 248 |  | 
|  | 249 | bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value, | 
|  | 250 | const MCRelaxableFragment *DF, | 
|  | 251 | const MCAsmLayout &Layout) const { | 
|  | 252 | return reasonForFixupRelaxation(Fixup, Value); | 
| Jim Grosbach | 25b63fa | 2011-12-06 00:47:03 +0000 | [diff] [blame] | 253 | } | 
|  | 254 |  | 
| Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 255 | void ARMAsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const { | 
| Jim Grosbach | 34a7c6d | 2011-12-05 23:45:46 +0000 | [diff] [blame] | 256 | unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode()); | 
|  | 257 |  | 
|  | 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); | 
|  | 263 | OS << "\n"; | 
|  | 264 | report_fatal_error("unexpected instruction to relax: " + OS.str()); | 
|  | 265 | } | 
|  | 266 |  | 
| Kevin Enderby | 9bd296a | 2014-01-10 00:43:32 +0000 | [diff] [blame] | 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); | 
| Jim Grosbach | e9119e4 | 2015-05-13 18:37:00 +0000 | [diff] [blame] | 272 | Res.addOperand(MCOperand::createImm(0)); | 
|  | 273 | Res.addOperand(MCOperand::createImm(14)); | 
|  | 274 | Res.addOperand(MCOperand::createReg(0)); | 
| Kevin Enderby | 9bd296a | 2014-01-10 00:43:32 +0000 | [diff] [blame] | 275 | return; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 276 | } | 
| Kevin Enderby | 9bd296a | 2014-01-10 00:43:32 +0000 | [diff] [blame] | 277 |  | 
|  | 278 | // The rest of instructions we're relaxing have the same operands. | 
| Jim Grosbach | 34a7c6d | 2011-12-05 23:45:46 +0000 | [diff] [blame] | 279 | // We just need to update to the proper opcode. | 
|  | 280 | Res = Inst; | 
|  | 281 | Res.setOpcode(RelaxedOp); | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 282 | } | 
|  | 283 |  | 
| Jim Grosbach | aba3de9 | 2012-01-18 18:52:16 +0000 | [diff] [blame] | 284 | bool ARMAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const { | 
| Jim Grosbach | 45e50d8 | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 285 | const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8 | 
|  | 286 | const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 287 | const uint32_t ARMv4_NopEncoding = 0xe1a00000;   // using MOV r0,r0 | 
| Jim Grosbach | 7ccdb7c | 2011-11-16 22:40:25 +0000 | [diff] [blame] | 288 | const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP | 
| Jim Grosbach | 87055ed | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 289 | if (isThumb()) { | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 290 | const uint16_t nopEncoding = | 
|  | 291 | hasNOP() ? Thumb2_16bitNopEncoding : Thumb1_16bitNopEncoding; | 
| Jim Grosbach | 97f1de7 | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 292 | uint64_t NumNops = Count / 2; | 
|  | 293 | for (uint64_t i = 0; i != NumNops; ++i) | 
| Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 294 | OW->write16(nopEncoding); | 
| Jim Grosbach | 97f1de7 | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 295 | if (Count & 1) | 
| Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 296 | OW->write8(0); | 
| Jim Grosbach | 87055ed | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 297 | return true; | 
|  | 298 | } | 
|  | 299 | // ARM mode | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 300 | const uint32_t nopEncoding = | 
|  | 301 | hasNOP() ? ARMv6T2_NopEncoding : ARMv4_NopEncoding; | 
| Jim Grosbach | 97f1de7 | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 302 | uint64_t NumNops = Count / 4; | 
|  | 303 | for (uint64_t i = 0; i != NumNops; ++i) | 
| Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 304 | OW->write32(nopEncoding); | 
| Jim Grosbach | 45e50d8 | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 305 | // FIXME: should this function return false when unable to write exactly | 
|  | 306 | // 'Count' bytes with NOP encodings? | 
| Jim Grosbach | 97f1de7 | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 307 | switch (Count % 4) { | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 308 | default: | 
|  | 309 | break; // No leftover bytes to write | 
|  | 310 | case 1: | 
| Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 311 | OW->write8(0); | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 312 | break; | 
|  | 313 | case 2: | 
| Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 314 | OW->write16(0); | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 315 | break; | 
|  | 316 | case 3: | 
| Jim Grosbach | 36e60e9 | 2015-06-04 22:24:41 +0000 | [diff] [blame] | 317 | OW->write16(0); | 
|  | 318 | OW->write8(0xa0); | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 319 | break; | 
| Jim Grosbach | 97f1de7 | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 320 | } | 
|  | 321 |  | 
| Rafael Espindola | 0ed1543 | 2010-10-25 17:50:35 +0000 | [diff] [blame] | 322 | return true; | 
| Jim Grosbach | 58bce99 | 2010-09-30 03:20:34 +0000 | [diff] [blame] | 323 | } | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 324 |  | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 325 | static uint32_t swapHalfWords(uint32_t Value, bool IsLittleEndian) { | 
|  | 326 | if (IsLittleEndian) { | 
|  | 327 | // Note that the halfwords are stored high first and low second in thumb; | 
|  | 328 | // so we need to swap the fixup value here to map properly. | 
|  | 329 | uint32_t Swapped = (Value & 0xFFFF0000) >> 16; | 
|  | 330 | Swapped |= (Value & 0x0000FFFF) << 16; | 
|  | 331 | return Swapped; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 332 | } else | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 333 | return Value; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | static uint32_t joinHalfWords(uint32_t FirstHalf, uint32_t SecondHalf, | 
|  | 337 | bool IsLittleEndian) { | 
|  | 338 | uint32_t Value; | 
|  | 339 |  | 
|  | 340 | if (IsLittleEndian) { | 
|  | 341 | Value = (SecondHalf & 0xFFFF) << 16; | 
|  | 342 | Value |= (FirstHalf & 0xFFFF); | 
|  | 343 | } else { | 
|  | 344 | Value = (SecondHalf & 0xFFFF); | 
|  | 345 | Value |= (FirstHalf & 0xFFFF) << 16; | 
|  | 346 | } | 
|  | 347 |  | 
|  | 348 | return Value; | 
|  | 349 | } | 
|  | 350 |  | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 351 | unsigned ARMAsmBackend::adjustFixupValue(const MCFixup &Fixup, uint64_t Value, | 
|  | 352 | bool IsPCRel, MCContext *Ctx, | 
|  | 353 | bool IsLittleEndian, | 
|  | 354 | bool IsResolved) const { | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 355 | unsigned Kind = Fixup.getKind(); | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 356 | switch (Kind) { | 
|  | 357 | default: | 
|  | 358 | llvm_unreachable("Unknown fixup kind!"); | 
| Jim Grosbach | 4416dfa | 2010-12-17 18:39:10 +0000 | [diff] [blame] | 359 | case FK_Data_1: | 
|  | 360 | case FK_Data_2: | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 361 | case FK_Data_4: | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 362 | return Value; | 
| Saleem Abdulrasool | fc6b85b | 2014-05-08 01:35:57 +0000 | [diff] [blame] | 363 | case FK_SecRel_2: | 
|  | 364 | return Value; | 
| Saleem Abdulrasool | 729c7a0 | 2014-05-04 23:13:15 +0000 | [diff] [blame] | 365 | case FK_SecRel_4: | 
|  | 366 | return Value; | 
| Jason W Kim | d5e6e54 | 2010-12-03 19:40:23 +0000 | [diff] [blame] | 367 | case ARM::fixup_arm_movt_hi16: | 
| Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 368 | if (!IsPCRel) | 
|  | 369 | Value >>= 16; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 370 | // Fallthrough | 
| Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 371 | case ARM::fixup_arm_movw_lo16: { | 
| Jason W Kim | d5e6e54 | 2010-12-03 19:40:23 +0000 | [diff] [blame] | 372 | unsigned Hi4 = (Value & 0xF000) >> 12; | 
|  | 373 | unsigned Lo12 = Value & 0x0FFF; | 
|  | 374 | // inst{19-16} = Hi4; | 
|  | 375 | // inst{11-0} = Lo12; | 
|  | 376 | Value = (Hi4 << 16) | (Lo12); | 
|  | 377 | return Value; | 
|  | 378 | } | 
| Evan Cheng | d4a5c05 | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 379 | case ARM::fixup_t2_movt_hi16: | 
| Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 380 | if (!IsPCRel) | 
|  | 381 | Value >>= 16; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 382 | // Fallthrough | 
| Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 383 | case ARM::fixup_t2_movw_lo16: { | 
| Evan Cheng | d4a5c05 | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 384 | unsigned Hi4 = (Value & 0xF000) >> 12; | 
|  | 385 | unsigned i = (Value & 0x800) >> 11; | 
|  | 386 | unsigned Mid3 = (Value & 0x700) >> 8; | 
|  | 387 | unsigned Lo8 = Value & 0x0FF; | 
|  | 388 | // inst{19-16} = Hi4; | 
|  | 389 | // inst{26} = i; | 
|  | 390 | // inst{14-12} = Mid3; | 
|  | 391 | // inst{7-0} = Lo8; | 
| Jim Grosbach | d76f43e | 2011-09-30 22:02:45 +0000 | [diff] [blame] | 392 | Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8); | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 393 | return swapHalfWords(Value, IsLittleEndian); | 
| Evan Cheng | d4a5c05 | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 394 | } | 
| Owen Anderson | 3e6ee1d | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 395 | case ARM::fixup_arm_ldst_pcrel_12: | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 396 | // ARM PC-relative values are offset by 8. | 
| Owen Anderson | 3ef19d9 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 397 | Value -= 4; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 398 | // FALLTHROUGH | 
| Owen Anderson | 3e6ee1d | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 399 | case ARM::fixup_t2_ldst_pcrel_12: { | 
|  | 400 | // Offset by 4, adjusted by two due to the half-word ordering of thumb. | 
| Owen Anderson | 3ef19d9 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 401 | Value -= 4; | 
| Owen Anderson | 3e6ee1d | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 402 | bool isAdd = true; | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 403 | if ((int64_t)Value < 0) { | 
|  | 404 | Value = -Value; | 
|  | 405 | isAdd = false; | 
|  | 406 | } | 
| Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 407 | if (Ctx && Value >= 4096) { | 
|  | 408 | Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value"); | 
|  | 409 | return 0; | 
|  | 410 | } | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 411 | Value |= isAdd << 23; | 
| Jim Grosbach | 3aeb867 | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 412 |  | 
| Owen Anderson | 3e6ee1d | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 413 | // Same addressing mode as fixup_arm_pcrel_10, | 
|  | 414 | // but with 16-bit halfwords swapped. | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 415 | if (Kind == ARM::fixup_t2_ldst_pcrel_12) | 
|  | 416 | return swapHalfWords(Value, IsLittleEndian); | 
| Jim Grosbach | 3aeb867 | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 417 |  | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 418 | return Value; | 
|  | 419 | } | 
| Jim Grosbach | ce2bd8d | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 420 | case ARM::fixup_arm_adr_pcrel_12: { | 
|  | 421 | // ARM PC-relative values are offset by 8. | 
|  | 422 | Value -= 8; | 
|  | 423 | unsigned opc = 4; // bits {24-21}. Default to add: 0b0100 | 
|  | 424 | if ((int64_t)Value < 0) { | 
|  | 425 | Value = -Value; | 
|  | 426 | opc = 2; // 0b0010 | 
|  | 427 | } | 
| Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 428 | if (Ctx && ARM_AM::getSOImmVal(Value) == -1) { | 
|  | 429 | Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value"); | 
|  | 430 | return 0; | 
|  | 431 | } | 
| Jim Grosbach | ce2bd8d | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 432 | // Encode the immediate and shift the opcode into place. | 
|  | 433 | return ARM_AM::getSOImmVal(Value) | (opc << 21); | 
|  | 434 | } | 
| Jim Grosbach | e34793e | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 435 |  | 
| Owen Anderson | 6d375e5 | 2010-12-14 00:36:49 +0000 | [diff] [blame] | 436 | case ARM::fixup_t2_adr_pcrel_12: { | 
|  | 437 | Value -= 4; | 
|  | 438 | unsigned opc = 0; | 
|  | 439 | if ((int64_t)Value < 0) { | 
|  | 440 | Value = -Value; | 
|  | 441 | opc = 5; | 
|  | 442 | } | 
|  | 443 |  | 
|  | 444 | uint32_t out = (opc << 21); | 
| Owen Anderson | 8543d4f | 2011-03-23 22:03:44 +0000 | [diff] [blame] | 445 | out |= (Value & 0x800) << 15; | 
| Owen Anderson | 6d375e5 | 2010-12-14 00:36:49 +0000 | [diff] [blame] | 446 | out |= (Value & 0x700) << 4; | 
|  | 447 | out |= (Value & 0x0FF); | 
| Jim Grosbach | e34793e | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 448 |  | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 449 | return swapHalfWords(out, IsLittleEndian); | 
| Owen Anderson | 6d375e5 | 2010-12-14 00:36:49 +0000 | [diff] [blame] | 450 | } | 
| Jim Grosbach | e34793e | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 451 |  | 
| Jason W Kim | d2e2f56 | 2011-02-04 19:47:15 +0000 | [diff] [blame] | 452 | case ARM::fixup_arm_condbranch: | 
|  | 453 | case ARM::fixup_arm_uncondbranch: | 
| James Molloy | fb5cd60 | 2012-03-30 09:15:32 +0000 | [diff] [blame] | 454 | case ARM::fixup_arm_uncondbl: | 
|  | 455 | case ARM::fixup_arm_condbl: | 
| Jim Grosbach | 7b811d3 | 2012-02-27 21:36:23 +0000 | [diff] [blame] | 456 | case ARM::fixup_arm_blx: | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 457 | // These values don't encode the low two bits since they're always zero. | 
|  | 458 | // Offset by 8 just as above. | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 459 | if (const MCSymbolRefExpr *SRE = | 
|  | 460 | dyn_cast<MCSymbolRefExpr>(Fixup.getValue())) | 
| Saleem Abdulrasool | 6e00ca8 | 2014-01-30 04:02:31 +0000 | [diff] [blame] | 461 | if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL) | 
|  | 462 | return 0; | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 463 | return 0xffffff & ((Value - 8) >> 2); | 
| Owen Anderson | 578074b | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 464 | case ARM::fixup_t2_uncondbranch: { | 
| Owen Anderson | 235c276 | 2010-12-10 23:02:28 +0000 | [diff] [blame] | 465 | Value = Value - 4; | 
| Owen Anderson | 302d5fd | 2010-12-09 00:27:41 +0000 | [diff] [blame] | 466 | Value >>= 1; // Low bit is not encoded. | 
| Jim Grosbach | 3aeb867 | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 467 |  | 
| Jim Grosbach | f588c51 | 2010-12-13 19:25:46 +0000 | [diff] [blame] | 468 | uint32_t out = 0; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 469 | bool I = Value & 0x800000; | 
| Owen Anderson | 578074b | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 470 | bool J1 = Value & 0x400000; | 
|  | 471 | bool J2 = Value & 0x200000; | 
|  | 472 | J1 ^= I; | 
|  | 473 | J2 ^= I; | 
| Jim Grosbach | e34793e | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 474 |  | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 475 | out |= I << 26;                 // S bit | 
|  | 476 | out |= !J1 << 13;               // J1 bit | 
|  | 477 | out |= !J2 << 11;               // J2 bit | 
|  | 478 | out |= (Value & 0x1FF800) << 5; // imm6 field | 
|  | 479 | out |= (Value & 0x0007FF);      // imm11 field | 
| Jim Grosbach | e34793e | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 480 |  | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 481 | return swapHalfWords(out, IsLittleEndian); | 
| Owen Anderson | 578074b | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 482 | } | 
|  | 483 | case ARM::fixup_t2_condbranch: { | 
|  | 484 | Value = Value - 4; | 
|  | 485 | Value >>= 1; // Low bit is not encoded. | 
| Jim Grosbach | e34793e | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 486 |  | 
| Owen Anderson | 578074b | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 487 | uint64_t out = 0; | 
| Owen Anderson | 14e4127 | 2010-12-09 01:02:09 +0000 | [diff] [blame] | 488 | out |= (Value & 0x80000) << 7; // S bit | 
|  | 489 | out |= (Value & 0x40000) >> 7; // J2 bit | 
|  | 490 | out |= (Value & 0x20000) >> 4; // J1 bit | 
|  | 491 | out |= (Value & 0x1F800) << 5; // imm6 field | 
|  | 492 | out |= (Value & 0x007FF);      // imm11 field | 
| Jim Grosbach | 3aeb867 | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 493 |  | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 494 | return swapHalfWords(out, IsLittleEndian); | 
| Owen Anderson | 302d5fd | 2010-12-09 00:27:41 +0000 | [diff] [blame] | 495 | } | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 496 | case ARM::fixup_arm_thumb_bl: { | 
| Saleem Abdulrasool | 077fd25 | 2014-01-26 22:29:36 +0000 | [diff] [blame] | 497 | // The value doesn't encode the low bit (always zero) and is offset by | 
|  | 498 | // four. The 32-bit immediate value is encoded as | 
|  | 499 | //   imm32 = SignExtend(S:I1:I2:imm10:imm11:0) | 
|  | 500 | // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S). | 
|  | 501 | // The value is encoded into disjoint bit positions in the destination | 
|  | 502 | // opcode. x = unchanged, I = immediate value bit, S = sign extension bit, | 
|  | 503 | // J = either J1 or J2 bit | 
|  | 504 | // | 
|  | 505 | //   BL:  xxxxxSIIIIIIIIII xxJxJIIIIIIIIIII | 
|  | 506 | // | 
|  | 507 | // Note that the halfwords are stored high first, low second; so we need | 
|  | 508 | // to transpose the fixup value here to map properly. | 
|  | 509 | uint32_t offset = (Value - 4) >> 1; | 
|  | 510 | uint32_t signBit = (offset & 0x800000) >> 23; | 
|  | 511 | uint32_t I1Bit = (offset & 0x400000) >> 22; | 
|  | 512 | uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit; | 
|  | 513 | uint32_t I2Bit = (offset & 0x200000) >> 21; | 
|  | 514 | uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit; | 
|  | 515 | uint32_t imm10Bits = (offset & 0x1FF800) >> 11; | 
|  | 516 | uint32_t imm11Bits = (offset & 0x000007FF); | 
| NAKAMURA Takumi | 8018a29 | 2013-06-11 06:52:36 +0000 | [diff] [blame] | 517 |  | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 518 | uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10Bits); | 
|  | 519 | uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) | | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 520 | (uint16_t)imm11Bits); | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 521 | return joinHalfWords(FirstHalf, SecondHalf, IsLittleEndian); | 
| Bill Wendling | 3392bfc | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 522 | } | 
|  | 523 | case ARM::fixup_arm_thumb_blx: { | 
| Saleem Abdulrasool | 077fd25 | 2014-01-26 22:29:36 +0000 | [diff] [blame] | 524 | // The value doesn't encode the low two bits (always zero) and is offset by | 
|  | 525 | // four (see fixup_arm_thumb_cp). The 32-bit immediate value is encoded as | 
|  | 526 | //   imm32 = SignExtend(S:I1:I2:imm10H:imm10L:00) | 
|  | 527 | // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S). | 
|  | 528 | // The value is encoded into disjoint bit positions in the destination | 
|  | 529 | // opcode. x = unchanged, I = immediate value bit, S = sign extension bit, | 
|  | 530 | // J = either J1 or J2 bit, 0 = zero. | 
|  | 531 | // | 
|  | 532 | //   BLX: xxxxxSIIIIIIIIII xxJxJIIIIIIIIII0 | 
|  | 533 | // | 
|  | 534 | // Note that the halfwords are stored high first, low second; so we need | 
|  | 535 | // to transpose the fixup value here to map properly. | 
|  | 536 | uint32_t offset = (Value - 2) >> 2; | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 537 | if (const MCSymbolRefExpr *SRE = | 
|  | 538 | dyn_cast<MCSymbolRefExpr>(Fixup.getValue())) | 
| Saleem Abdulrasool | 6e00ca8 | 2014-01-30 04:02:31 +0000 | [diff] [blame] | 539 | if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL) | 
|  | 540 | offset = 0; | 
| Saleem Abdulrasool | 077fd25 | 2014-01-26 22:29:36 +0000 | [diff] [blame] | 541 | uint32_t signBit = (offset & 0x400000) >> 22; | 
|  | 542 | uint32_t I1Bit = (offset & 0x200000) >> 21; | 
|  | 543 | uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit; | 
|  | 544 | uint32_t I2Bit = (offset & 0x100000) >> 20; | 
|  | 545 | uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit; | 
|  | 546 | uint32_t imm10HBits = (offset & 0xFFC00) >> 10; | 
|  | 547 | uint32_t imm10LBits = (offset & 0x3FF); | 
| NAKAMURA Takumi | 8018a29 | 2013-06-11 06:52:36 +0000 | [diff] [blame] | 548 |  | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 549 | uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10HBits); | 
|  | 550 | uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) | | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 551 | ((uint16_t)imm10LBits) << 1); | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 552 | return joinHalfWords(FirstHalf, SecondHalf, IsLittleEndian); | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 553 | } | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 554 | case ARM::fixup_thumb_adr_pcrel_10: | 
| Bill Wendling | 8a6449c | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 555 | case ARM::fixup_arm_thumb_cp: | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 556 | // On CPUs supporting Thumb2, this will be relaxed to an ldr.w, otherwise we | 
|  | 557 | // could have an error on our hands. | 
|  | 558 | if (Ctx && !STI->getFeatureBits()[ARM::FeatureThumb2] && IsResolved) { | 
|  | 559 | const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value); | 
| Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 560 | if (FixupDiagnostic) { | 
|  | 561 | Ctx->reportError(Fixup.getLoc(), FixupDiagnostic); | 
|  | 562 | return 0; | 
|  | 563 | } | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 564 | } | 
|  | 565 | // Offset by 4, and don't encode the low two bits. | 
|  | 566 | return ((Value - 4) >> 2) & 0xff; | 
| Jim Grosbach | 68b27eb | 2010-12-09 19:50:12 +0000 | [diff] [blame] | 567 | case ARM::fixup_arm_thumb_cb: { | 
| Bill Wendling | a7d6aa9 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 568 | // Offset by 4 and don't encode the lower bit, which is always 0. | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 569 | // FIXME: diagnose if no Thumb2 | 
| Bill Wendling | a7d6aa9 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 570 | uint32_t Binary = (Value - 4) >> 1; | 
| Owen Anderson | f636a64 | 2010-12-14 19:42:53 +0000 | [diff] [blame] | 571 | return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3); | 
| Bill Wendling | a7d6aa9 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 572 | } | 
| Jim Grosbach | e119da1 | 2010-12-10 18:21:33 +0000 | [diff] [blame] | 573 | case ARM::fixup_arm_thumb_br: | 
|  | 574 | // Offset by 4 and don't encode the lower bit, which is always 0. | 
| Bradley Smith | a118910 | 2016-01-15 10:26:17 +0000 | [diff] [blame] | 575 | if (Ctx && !STI->getFeatureBits()[ARM::FeatureThumb2] && | 
|  | 576 | !STI->getFeatureBits()[ARM::HasV8MBaselineOps]) { | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 577 | const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value); | 
| Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 578 | if (FixupDiagnostic) { | 
|  | 579 | Ctx->reportError(Fixup.getLoc(), FixupDiagnostic); | 
|  | 580 | return 0; | 
|  | 581 | } | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 582 | } | 
| Jim Grosbach | e119da1 | 2010-12-10 18:21:33 +0000 | [diff] [blame] | 583 | return ((Value - 4) >> 1) & 0x7ff; | 
| Jim Grosbach | 78485ad | 2010-12-10 17:13:40 +0000 | [diff] [blame] | 584 | case ARM::fixup_arm_thumb_bcc: | 
|  | 585 | // Offset by 4 and don't encode the lower bit, which is always 0. | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 586 | if (Ctx && !STI->getFeatureBits()[ARM::FeatureThumb2]) { | 
|  | 587 | const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value); | 
| Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 588 | if (FixupDiagnostic) { | 
|  | 589 | Ctx->reportError(Fixup.getLoc(), FixupDiagnostic); | 
|  | 590 | return 0; | 
|  | 591 | } | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 592 | } | 
| Jim Grosbach | 78485ad | 2010-12-10 17:13:40 +0000 | [diff] [blame] | 593 | return ((Value - 4) >> 1) & 0xff; | 
| Jim Grosbach | 8648c10 | 2011-12-19 23:06:24 +0000 | [diff] [blame] | 594 | case ARM::fixup_arm_pcrel_10_unscaled: { | 
|  | 595 | Value = Value - 8; // ARM fixups offset by an additional word and don't | 
|  | 596 | // need to adjust for the half-word ordering. | 
|  | 597 | bool isAdd = true; | 
|  | 598 | if ((int64_t)Value < 0) { | 
|  | 599 | Value = -Value; | 
|  | 600 | isAdd = false; | 
|  | 601 | } | 
| Jim Grosbach | 913cc30 | 2012-03-30 21:54:22 +0000 | [diff] [blame] | 602 | // The value has the low 4 bits encoded in [3:0] and the high 4 in [11:8]. | 
| Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 603 | if (Ctx && Value >= 256) { | 
|  | 604 | Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value"); | 
|  | 605 | return 0; | 
|  | 606 | } | 
| Jim Grosbach | 913cc30 | 2012-03-30 21:54:22 +0000 | [diff] [blame] | 607 | Value = (Value & 0xf) | ((Value & 0xf0) << 4); | 
| Jim Grosbach | 8648c10 | 2011-12-19 23:06:24 +0000 | [diff] [blame] | 608 | return Value | (isAdd << 23); | 
|  | 609 | } | 
| Jim Grosbach | 3c68561 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 610 | case ARM::fixup_arm_pcrel_10: | 
| Owen Anderson | 4743d75 | 2010-12-10 22:46:47 +0000 | [diff] [blame] | 611 | Value = Value - 4; // ARM fixups offset by an additional word and don't | 
| Jim Grosbach | 3c68561 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 612 | // need to adjust for the half-word ordering. | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 613 | // Fall through. | 
| Jim Grosbach | 3c68561 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 614 | case ARM::fixup_t2_pcrel_10: { | 
|  | 615 | // Offset by 4, adjusted by two due to the half-word ordering of thumb. | 
| Owen Anderson | 4743d75 | 2010-12-10 22:46:47 +0000 | [diff] [blame] | 616 | Value = Value - 4; | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 617 | bool isAdd = true; | 
|  | 618 | if ((int64_t)Value < 0) { | 
|  | 619 | Value = -Value; | 
|  | 620 | isAdd = false; | 
|  | 621 | } | 
|  | 622 | // These values don't encode the low two bits since they're always zero. | 
|  | 623 | Value >>= 2; | 
| Oliver Stannard | 9be59af | 2015-11-17 10:00:43 +0000 | [diff] [blame] | 624 | if (Ctx && Value >= 256) { | 
|  | 625 | Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value"); | 
|  | 626 | return 0; | 
|  | 627 | } | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 628 | Value |= isAdd << 23; | 
| Jim Grosbach | 3c68561 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 629 |  | 
| Jim Grosbach | 8648c10 | 2011-12-19 23:06:24 +0000 | [diff] [blame] | 630 | // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords | 
|  | 631 | // swapped. | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 632 | if (Kind == ARM::fixup_t2_pcrel_10) | 
|  | 633 | return swapHalfWords(Value, IsLittleEndian); | 
| Jim Grosbach | 3c68561 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 634 |  | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 635 | return Value; | 
|  | 636 | } | 
| Oliver Stannard | 65b8538 | 2016-01-25 10:26:26 +0000 | [diff] [blame] | 637 | case ARM::fixup_arm_pcrel_9: | 
|  | 638 | Value = Value - 4; // ARM fixups offset by an additional word and don't | 
|  | 639 | // need to adjust for the half-word ordering. | 
|  | 640 | // Fall through. | 
|  | 641 | case ARM::fixup_t2_pcrel_9: { | 
|  | 642 | // Offset by 4, adjusted by two due to the half-word ordering of thumb. | 
|  | 643 | Value = Value - 4; | 
|  | 644 | bool isAdd = true; | 
|  | 645 | if ((int64_t)Value < 0) { | 
|  | 646 | Value = -Value; | 
|  | 647 | isAdd = false; | 
|  | 648 | } | 
|  | 649 | // These values don't encode the low bit since it's always zero. | 
|  | 650 | if (Ctx && (Value & 1)) { | 
|  | 651 | Ctx->reportError(Fixup.getLoc(), "invalid value for this fixup"); | 
|  | 652 | return 0; | 
|  | 653 | } | 
|  | 654 | Value >>= 1; | 
|  | 655 | if (Ctx && Value >= 256) { | 
|  | 656 | Ctx->reportError(Fixup.getLoc(), "out of range pc-relative fixup value"); | 
|  | 657 | return 0; | 
|  | 658 | } | 
|  | 659 | Value |= isAdd << 23; | 
|  | 660 |  | 
|  | 661 | // Same addressing mode as fixup_arm_pcrel_9, but with 16-bit halfwords | 
|  | 662 | // swapped. | 
|  | 663 | if (Kind == ARM::fixup_t2_pcrel_9) | 
|  | 664 | return swapHalfWords(Value, IsLittleEndian); | 
|  | 665 |  | 
|  | 666 | return Value; | 
|  | 667 | } | 
| Jason W Kim | fc5c522 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 668 | } | 
|  | 669 | } | 
|  | 670 |  | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 671 | void ARMAsmBackend::processFixupValue(const MCAssembler &Asm, | 
|  | 672 | const MCAsmLayout &Layout, | 
|  | 673 | const MCFixup &Fixup, | 
|  | 674 | const MCFragment *DF, | 
| Rafael Espindola | 3e3de5e | 2014-03-28 16:06:09 +0000 | [diff] [blame] | 675 | const MCValue &Target, uint64_t &Value, | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 676 | bool &IsResolved) { | 
|  | 677 | const MCSymbolRefExpr *A = Target.getSymA(); | 
| Rafael Espindola | 49b8548 | 2015-11-04 23:00:39 +0000 | [diff] [blame] | 678 | const MCSymbol *Sym = A ? &A->getSymbol() : nullptr; | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 679 | // Some fixups to thumb function symbols need the low bit (thumb bit) | 
|  | 680 | // twiddled. | 
|  | 681 | if ((unsigned)Fixup.getKind() != ARM::fixup_arm_ldst_pcrel_12 && | 
|  | 682 | (unsigned)Fixup.getKind() != ARM::fixup_t2_ldst_pcrel_12 && | 
|  | 683 | (unsigned)Fixup.getKind() != ARM::fixup_arm_adr_pcrel_12 && | 
|  | 684 | (unsigned)Fixup.getKind() != ARM::fixup_thumb_adr_pcrel_10 && | 
|  | 685 | (unsigned)Fixup.getKind() != ARM::fixup_t2_adr_pcrel_12 && | 
|  | 686 | (unsigned)Fixup.getKind() != ARM::fixup_arm_thumb_cp) { | 
| Rafael Espindola | 49b8548 | 2015-11-04 23:00:39 +0000 | [diff] [blame] | 687 | if (Sym) { | 
|  | 688 | if (Asm.isThumbFunc(Sym)) | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 689 | Value |= 1; | 
|  | 690 | } | 
|  | 691 | } | 
| Rafael Espindola | 49b8548 | 2015-11-04 23:00:39 +0000 | [diff] [blame] | 692 | if (IsResolved && (unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_bl) { | 
|  | 693 | assert(Sym && "How did we resolve this?"); | 
|  | 694 |  | 
|  | 695 | // If the symbol is external the linker will handle it. | 
|  | 696 | // FIXME: Should we handle it as an optimization? | 
| Rafael Espindola | e61a902 | 2015-11-05 01:10:15 +0000 | [diff] [blame] | 697 |  | 
|  | 698 | // If the symbol is out of range, produce a relocation and hope the | 
|  | 699 | // linker can handle it. GNU AS produces an error in this case. | 
|  | 700 | if (Sym->isExternal() || Value >= 0x400004) | 
| Rafael Espindola | 49b8548 | 2015-11-04 23:00:39 +0000 | [diff] [blame] | 701 | IsResolved = false; | 
| Logan Chien | d5c48aa | 2014-02-05 14:15:16 +0000 | [diff] [blame] | 702 | } | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 703 | // We must always generate a relocation for BL/BLX instructions if we have | 
|  | 704 | // a symbol to reference, as the linker relies on knowing the destination | 
|  | 705 | // symbol's thumb-ness to get interworking right. | 
|  | 706 | if (A && ((unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_blx || | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 707 | (unsigned)Fixup.getKind() == ARM::fixup_arm_blx || | 
|  | 708 | (unsigned)Fixup.getKind() == ARM::fixup_arm_uncondbl || | 
|  | 709 | (unsigned)Fixup.getKind() == ARM::fixup_arm_condbl)) | 
|  | 710 | IsResolved = false; | 
|  | 711 |  | 
|  | 712 | // Try to get the encoded value for the fixup as-if we're mapping it into | 
|  | 713 | // the instruction. This allows adjustFixupValue() to issue a diagnostic | 
|  | 714 | // if the value aren't invalid. | 
| Christian Pirker | fdce7ce | 2014-05-06 10:05:11 +0000 | [diff] [blame] | 715 | (void)adjustFixupValue(Fixup, Value, false, &Asm.getContext(), | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 716 | IsLittleEndian, IsResolved); | 
| Jim Grosbach | e78031a | 2012-04-30 22:30:43 +0000 | [diff] [blame] | 717 | } | 
|  | 718 |  | 
| Bill Wendling | f09c44c | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 719 | /// getFixupKindNumBytes - The number of bytes the fixup may change. | 
| Jim Grosbach | 9d6d77a | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 720 | static unsigned getFixupKindNumBytes(unsigned Kind) { | 
| Jim Grosbach | 9098714 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 721 | switch (Kind) { | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 722 | default: | 
|  | 723 | llvm_unreachable("Unknown fixup kind!"); | 
| Bill Wendling | 8a6449c | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 724 |  | 
| Jim Grosbach | 4416dfa | 2010-12-17 18:39:10 +0000 | [diff] [blame] | 725 | case FK_Data_1: | 
| Jim Grosbach | 78485ad | 2010-12-10 17:13:40 +0000 | [diff] [blame] | 726 | case ARM::fixup_arm_thumb_bcc: | 
| Bill Wendling | 8a6449c | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 727 | case ARM::fixup_arm_thumb_cp: | 
| Jim Grosbach | 509dc2a | 2010-12-14 22:28:03 +0000 | [diff] [blame] | 728 | case ARM::fixup_thumb_adr_pcrel_10: | 
| Bill Wendling | 8a6449c | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 729 | return 1; | 
|  | 730 |  | 
| Jim Grosbach | 4416dfa | 2010-12-17 18:39:10 +0000 | [diff] [blame] | 731 | case FK_Data_2: | 
| Jim Grosbach | e119da1 | 2010-12-10 18:21:33 +0000 | [diff] [blame] | 732 | case ARM::fixup_arm_thumb_br: | 
| Jim Grosbach | 68b27eb | 2010-12-09 19:50:12 +0000 | [diff] [blame] | 733 | case ARM::fixup_arm_thumb_cb: | 
| Bill Wendling | a7d6aa9 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 734 | return 2; | 
|  | 735 |  | 
| Jim Grosbach | 8648c10 | 2011-12-19 23:06:24 +0000 | [diff] [blame] | 736 | case ARM::fixup_arm_pcrel_10_unscaled: | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 737 | case ARM::fixup_arm_ldst_pcrel_12: | 
|  | 738 | case ARM::fixup_arm_pcrel_10: | 
| Oliver Stannard | 65b8538 | 2016-01-25 10:26:26 +0000 | [diff] [blame] | 739 | case ARM::fixup_arm_pcrel_9: | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 740 | case ARM::fixup_arm_adr_pcrel_12: | 
| James Molloy | fb5cd60 | 2012-03-30 09:15:32 +0000 | [diff] [blame] | 741 | case ARM::fixup_arm_uncondbl: | 
|  | 742 | case ARM::fixup_arm_condbl: | 
| Jim Grosbach | 7b811d3 | 2012-02-27 21:36:23 +0000 | [diff] [blame] | 743 | case ARM::fixup_arm_blx: | 
| Jason W Kim | d2e2f56 | 2011-02-04 19:47:15 +0000 | [diff] [blame] | 744 | case ARM::fixup_arm_condbranch: | 
|  | 745 | case ARM::fixup_arm_uncondbranch: | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 746 | return 3; | 
| Bill Wendling | 8a6449c | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 747 |  | 
|  | 748 | case FK_Data_4: | 
| Owen Anderson | 3e6ee1d | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 749 | case ARM::fixup_t2_ldst_pcrel_12: | 
| Owen Anderson | 578074b | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 750 | case ARM::fixup_t2_condbranch: | 
|  | 751 | case ARM::fixup_t2_uncondbranch: | 
| Owen Anderson | 0f7142d | 2010-12-08 00:18:36 +0000 | [diff] [blame] | 752 | case ARM::fixup_t2_pcrel_10: | 
| Oliver Stannard | 65b8538 | 2016-01-25 10:26:26 +0000 | [diff] [blame] | 753 | case ARM::fixup_t2_pcrel_9: | 
| Owen Anderson | 6d375e5 | 2010-12-14 00:36:49 +0000 | [diff] [blame] | 754 | case ARM::fixup_t2_adr_pcrel_12: | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 755 | case ARM::fixup_arm_thumb_bl: | 
| Bill Wendling | 3392bfc | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 756 | case ARM::fixup_arm_thumb_blx: | 
| Evan Cheng | d4a5c05 | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 757 | case ARM::fixup_arm_movt_hi16: | 
|  | 758 | case ARM::fixup_arm_movw_lo16: | 
| Evan Cheng | d4a5c05 | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 759 | case ARM::fixup_t2_movt_hi16: | 
|  | 760 | case ARM::fixup_t2_movw_lo16: | 
| Jim Grosbach | 9e19946 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 761 | return 4; | 
| Saleem Abdulrasool | 729c7a0 | 2014-05-04 23:13:15 +0000 | [diff] [blame] | 762 |  | 
| Saleem Abdulrasool | fc6b85b | 2014-05-08 01:35:57 +0000 | [diff] [blame] | 763 | case FK_SecRel_2: | 
|  | 764 | return 2; | 
| Saleem Abdulrasool | 729c7a0 | 2014-05-04 23:13:15 +0000 | [diff] [blame] | 765 | case FK_SecRel_4: | 
|  | 766 | return 4; | 
| Jim Grosbach | 9098714 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 767 | } | 
|  | 768 | } | 
|  | 769 |  | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 770 | /// getFixupKindContainerSizeBytes - The number of bytes of the | 
|  | 771 | /// container involved in big endian. | 
|  | 772 | static unsigned getFixupKindContainerSizeBytes(unsigned Kind) { | 
|  | 773 | switch (Kind) { | 
|  | 774 | default: | 
|  | 775 | llvm_unreachable("Unknown fixup kind!"); | 
|  | 776 |  | 
|  | 777 | case FK_Data_1: | 
|  | 778 | return 1; | 
|  | 779 | case FK_Data_2: | 
|  | 780 | return 2; | 
|  | 781 | case FK_Data_4: | 
|  | 782 | return 4; | 
|  | 783 |  | 
|  | 784 | case ARM::fixup_arm_thumb_bcc: | 
|  | 785 | case ARM::fixup_arm_thumb_cp: | 
|  | 786 | case ARM::fixup_thumb_adr_pcrel_10: | 
|  | 787 | case ARM::fixup_arm_thumb_br: | 
|  | 788 | case ARM::fixup_arm_thumb_cb: | 
|  | 789 | // Instruction size is 2 bytes. | 
|  | 790 | return 2; | 
|  | 791 |  | 
|  | 792 | case ARM::fixup_arm_pcrel_10_unscaled: | 
|  | 793 | case ARM::fixup_arm_ldst_pcrel_12: | 
|  | 794 | case ARM::fixup_arm_pcrel_10: | 
|  | 795 | case ARM::fixup_arm_adr_pcrel_12: | 
|  | 796 | case ARM::fixup_arm_uncondbl: | 
|  | 797 | case ARM::fixup_arm_condbl: | 
|  | 798 | case ARM::fixup_arm_blx: | 
|  | 799 | case ARM::fixup_arm_condbranch: | 
|  | 800 | case ARM::fixup_arm_uncondbranch: | 
|  | 801 | case ARM::fixup_t2_ldst_pcrel_12: | 
|  | 802 | case ARM::fixup_t2_condbranch: | 
|  | 803 | case ARM::fixup_t2_uncondbranch: | 
|  | 804 | case ARM::fixup_t2_pcrel_10: | 
|  | 805 | case ARM::fixup_t2_adr_pcrel_12: | 
|  | 806 | case ARM::fixup_arm_thumb_bl: | 
|  | 807 | case ARM::fixup_arm_thumb_blx: | 
|  | 808 | case ARM::fixup_arm_movt_hi16: | 
|  | 809 | case ARM::fixup_arm_movw_lo16: | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 810 | case ARM::fixup_t2_movt_hi16: | 
|  | 811 | case ARM::fixup_t2_movw_lo16: | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 812 | // Instruction size is 4 bytes. | 
|  | 813 | return 4; | 
|  | 814 | } | 
|  | 815 | } | 
|  | 816 |  | 
| Benjamin Kramer | 07ea85a | 2012-11-24 14:36:43 +0000 | [diff] [blame] | 817 | void ARMAsmBackend::applyFixup(const MCFixup &Fixup, char *Data, | 
| Rafael Espindola | 5904e12 | 2014-03-29 06:26:49 +0000 | [diff] [blame] | 818 | unsigned DataSize, uint64_t Value, | 
|  | 819 | bool IsPCRel) const { | 
| Jim Grosbach | 9d6d77a | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 820 | unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind()); | 
| Tim Northover | 8d67b8e | 2015-10-02 18:07:18 +0000 | [diff] [blame] | 821 | Value = | 
|  | 822 | adjustFixupValue(Fixup, Value, IsPCRel, nullptr, IsLittleEndian, true); | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 823 | if (!Value) | 
|  | 824 | return; // Doesn't change encoding. | 
| Jim Grosbach | 9098714 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 825 |  | 
| Bill Wendling | f09c44c | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 826 | unsigned Offset = Fixup.getOffset(); | 
|  | 827 | assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!"); | 
|  | 828 |  | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 829 | // Used to point to big endian bytes. | 
|  | 830 | unsigned FullSizeBytes; | 
| Christian Pirker | 875629f | 2014-05-20 09:24:37 +0000 | [diff] [blame] | 831 | if (!IsLittleEndian) { | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 832 | FullSizeBytes = getFixupKindContainerSizeBytes(Fixup.getKind()); | 
| Christian Pirker | 875629f | 2014-05-20 09:24:37 +0000 | [diff] [blame] | 833 | assert((Offset + FullSizeBytes) <= DataSize && "Invalid fixup size!"); | 
|  | 834 | assert(NumBytes <= FullSizeBytes && "Invalid fixup size!"); | 
|  | 835 | } | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 836 |  | 
| Benjamin Kramer | 07ea85a | 2012-11-24 14:36:43 +0000 | [diff] [blame] | 837 | // For each byte of the fragment that the fixup touches, mask in the bits from | 
|  | 838 | // the fixup value. The Value has been "split up" into the appropriate | 
|  | 839 | // bitfields above. | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 840 | for (unsigned i = 0; i != NumBytes; ++i) { | 
|  | 841 | unsigned Idx = IsLittleEndian ? i : (FullSizeBytes - 1 - i); | 
|  | 842 | Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff); | 
|  | 843 | } | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 844 | } | 
| Bill Wendling | 721724e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 845 |  | 
| Tim Northover | f8e47e4 | 2015-10-28 22:56:36 +0000 | [diff] [blame] | 846 | namespace CU { | 
|  | 847 |  | 
|  | 848 | /// \brief Compact unwind encoding values. | 
|  | 849 | enum CompactUnwindEncodings { | 
|  | 850 | UNWIND_ARM_MODE_MASK                         = 0x0F000000, | 
|  | 851 | UNWIND_ARM_MODE_FRAME                        = 0x01000000, | 
|  | 852 | UNWIND_ARM_MODE_FRAME_D                      = 0x02000000, | 
|  | 853 | UNWIND_ARM_MODE_DWARF                        = 0x04000000, | 
|  | 854 |  | 
|  | 855 | UNWIND_ARM_FRAME_STACK_ADJUST_MASK           = 0x00C00000, | 
|  | 856 |  | 
|  | 857 | UNWIND_ARM_FRAME_FIRST_PUSH_R4               = 0x00000001, | 
|  | 858 | UNWIND_ARM_FRAME_FIRST_PUSH_R5               = 0x00000002, | 
|  | 859 | UNWIND_ARM_FRAME_FIRST_PUSH_R6               = 0x00000004, | 
|  | 860 |  | 
|  | 861 | UNWIND_ARM_FRAME_SECOND_PUSH_R8              = 0x00000008, | 
|  | 862 | UNWIND_ARM_FRAME_SECOND_PUSH_R9              = 0x00000010, | 
|  | 863 | UNWIND_ARM_FRAME_SECOND_PUSH_R10             = 0x00000020, | 
|  | 864 | UNWIND_ARM_FRAME_SECOND_PUSH_R11             = 0x00000040, | 
|  | 865 | UNWIND_ARM_FRAME_SECOND_PUSH_R12             = 0x00000080, | 
|  | 866 |  | 
|  | 867 | UNWIND_ARM_FRAME_D_REG_COUNT_MASK            = 0x00000F00, | 
|  | 868 |  | 
|  | 869 | UNWIND_ARM_DWARF_SECTION_OFFSET              = 0x00FFFFFF | 
|  | 870 | }; | 
|  | 871 |  | 
|  | 872 | } // end CU namespace | 
|  | 873 |  | 
|  | 874 | /// Generate compact unwind encoding for the function based on the CFI | 
|  | 875 | /// instructions. If the CFI instructions describe a frame that cannot be | 
|  | 876 | /// encoded in compact unwind, the method returns UNWIND_ARM_MODE_DWARF which | 
|  | 877 | /// tells the runtime to fallback and unwind using dwarf. | 
|  | 878 | uint32_t ARMAsmBackendDarwin::generateCompactUnwindEncoding( | 
|  | 879 | ArrayRef<MCCFIInstruction> Instrs) const { | 
|  | 880 | DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs() << "generateCU()\n"); | 
|  | 881 | // Only armv7k uses CFI based unwinding. | 
|  | 882 | if (Subtype != MachO::CPU_SUBTYPE_ARM_V7K) | 
|  | 883 | return 0; | 
|  | 884 | // No .cfi directives means no frame. | 
|  | 885 | if (Instrs.empty()) | 
|  | 886 | return 0; | 
|  | 887 | // Start off assuming CFA is at SP+0. | 
|  | 888 | int CFARegister = ARM::SP; | 
|  | 889 | int CFARegisterOffset = 0; | 
|  | 890 | // Mark savable registers as initially unsaved | 
|  | 891 | DenseMap<unsigned, int> RegOffsets; | 
|  | 892 | int FloatRegCount = 0; | 
|  | 893 | // Process each .cfi directive and build up compact unwind info. | 
|  | 894 | for (size_t i = 0, e = Instrs.size(); i != e; ++i) { | 
|  | 895 | int Reg; | 
|  | 896 | const MCCFIInstruction &Inst = Instrs[i]; | 
|  | 897 | switch (Inst.getOperation()) { | 
|  | 898 | case MCCFIInstruction::OpDefCfa: // DW_CFA_def_cfa | 
|  | 899 | CFARegisterOffset = -Inst.getOffset(); | 
|  | 900 | CFARegister = MRI.getLLVMRegNum(Inst.getRegister(), true); | 
|  | 901 | break; | 
|  | 902 | case MCCFIInstruction::OpDefCfaOffset: // DW_CFA_def_cfa_offset | 
|  | 903 | CFARegisterOffset = -Inst.getOffset(); | 
|  | 904 | break; | 
|  | 905 | case MCCFIInstruction::OpDefCfaRegister: // DW_CFA_def_cfa_register | 
|  | 906 | CFARegister = MRI.getLLVMRegNum(Inst.getRegister(), true); | 
|  | 907 | break; | 
|  | 908 | case MCCFIInstruction::OpOffset: // DW_CFA_offset | 
|  | 909 | Reg = MRI.getLLVMRegNum(Inst.getRegister(), true); | 
|  | 910 | if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg)) | 
|  | 911 | RegOffsets[Reg] = Inst.getOffset(); | 
|  | 912 | else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) { | 
|  | 913 | RegOffsets[Reg] = Inst.getOffset(); | 
|  | 914 | ++FloatRegCount; | 
|  | 915 | } else { | 
|  | 916 | DEBUG_WITH_TYPE("compact-unwind", | 
|  | 917 | llvm::dbgs() << ".cfi_offset on unknown register=" | 
|  | 918 | << Inst.getRegister() << "\n"); | 
|  | 919 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 920 | } | 
|  | 921 | break; | 
|  | 922 | case MCCFIInstruction::OpRelOffset: // DW_CFA_advance_loc | 
|  | 923 | // Ignore | 
|  | 924 | break; | 
|  | 925 | default: | 
|  | 926 | // Directive not convertable to compact unwind, bail out. | 
|  | 927 | DEBUG_WITH_TYPE("compact-unwind", | 
|  | 928 | llvm::dbgs() | 
|  | 929 | << "CFI directive not compatiable with comact " | 
|  | 930 | "unwind encoding, opcode=" << Inst.getOperation() | 
|  | 931 | << "\n"); | 
|  | 932 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 933 | break; | 
|  | 934 | } | 
|  | 935 | } | 
|  | 936 |  | 
|  | 937 | // If no frame set up, return no unwind info. | 
|  | 938 | if ((CFARegister == ARM::SP) && (CFARegisterOffset == 0)) | 
|  | 939 | return 0; | 
|  | 940 |  | 
|  | 941 | // Verify standard frame (lr/r7) was used. | 
|  | 942 | if (CFARegister != ARM::R7) { | 
|  | 943 | DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs() << "frame register is " | 
|  | 944 | << CFARegister | 
|  | 945 | << " instead of r7\n"); | 
|  | 946 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 947 | } | 
|  | 948 | int StackAdjust = CFARegisterOffset - 8; | 
|  | 949 | if (RegOffsets.lookup(ARM::LR) != (-4 - StackAdjust)) { | 
|  | 950 | DEBUG_WITH_TYPE("compact-unwind", | 
|  | 951 | llvm::dbgs() | 
|  | 952 | << "LR not saved as standard frame, StackAdjust=" | 
|  | 953 | << StackAdjust | 
|  | 954 | << ", CFARegisterOffset=" << CFARegisterOffset | 
|  | 955 | << ", lr save at offset=" << RegOffsets[14] << "\n"); | 
|  | 956 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 957 | } | 
|  | 958 | if (RegOffsets.lookup(ARM::R7) != (-8 - StackAdjust)) { | 
|  | 959 | DEBUG_WITH_TYPE("compact-unwind", | 
|  | 960 | llvm::dbgs() << "r7 not saved as standard frame\n"); | 
|  | 961 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 962 | } | 
|  | 963 | uint32_t CompactUnwindEncoding = CU::UNWIND_ARM_MODE_FRAME; | 
|  | 964 |  | 
|  | 965 | // If var-args are used, there may be a stack adjust required. | 
|  | 966 | switch (StackAdjust) { | 
|  | 967 | case 0: | 
|  | 968 | break; | 
|  | 969 | case 4: | 
|  | 970 | CompactUnwindEncoding |= 0x00400000; | 
|  | 971 | break; | 
|  | 972 | case 8: | 
|  | 973 | CompactUnwindEncoding |= 0x00800000; | 
|  | 974 | break; | 
|  | 975 | case 12: | 
|  | 976 | CompactUnwindEncoding |= 0x00C00000; | 
|  | 977 | break; | 
|  | 978 | default: | 
|  | 979 | DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs() | 
|  | 980 | << ".cfi_def_cfa stack adjust (" | 
|  | 981 | << StackAdjust << ") out of range\n"); | 
|  | 982 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 983 | } | 
|  | 984 |  | 
|  | 985 | // If r6 is saved, it must be right below r7. | 
|  | 986 | static struct { | 
|  | 987 | unsigned Reg; | 
|  | 988 | unsigned Encoding; | 
|  | 989 | } GPRCSRegs[] = {{ARM::R6, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R6}, | 
|  | 990 | {ARM::R5, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R5}, | 
|  | 991 | {ARM::R4, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R4}, | 
|  | 992 | {ARM::R12, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R12}, | 
|  | 993 | {ARM::R11, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R11}, | 
|  | 994 | {ARM::R10, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R10}, | 
|  | 995 | {ARM::R9, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R9}, | 
|  | 996 | {ARM::R8, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R8}}; | 
|  | 997 |  | 
|  | 998 | int CurOffset = -8 - StackAdjust; | 
|  | 999 | for (auto CSReg : GPRCSRegs) { | 
|  | 1000 | auto Offset = RegOffsets.find(CSReg.Reg); | 
|  | 1001 | if (Offset == RegOffsets.end()) | 
|  | 1002 | continue; | 
|  | 1003 |  | 
|  | 1004 | int RegOffset = Offset->second; | 
|  | 1005 | if (RegOffset != CurOffset - 4) { | 
|  | 1006 | DEBUG_WITH_TYPE("compact-unwind", | 
|  | 1007 | llvm::dbgs() << MRI.getName(CSReg.Reg) << " saved at " | 
|  | 1008 | << RegOffset << " but only supported at " | 
|  | 1009 | << CurOffset << "\n"); | 
|  | 1010 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 1011 | } | 
|  | 1012 | CompactUnwindEncoding |= CSReg.Encoding; | 
|  | 1013 | CurOffset -= 4; | 
|  | 1014 | } | 
|  | 1015 |  | 
|  | 1016 | // If no floats saved, we are done. | 
|  | 1017 | if (FloatRegCount == 0) | 
|  | 1018 | return CompactUnwindEncoding; | 
|  | 1019 |  | 
|  | 1020 | // Switch mode to include D register saving. | 
|  | 1021 | CompactUnwindEncoding &= ~CU::UNWIND_ARM_MODE_MASK; | 
|  | 1022 | CompactUnwindEncoding |= CU::UNWIND_ARM_MODE_FRAME_D; | 
|  | 1023 |  | 
|  | 1024 | // FIXME: supporting more than 4 saved D-registers compactly would be trivial, | 
|  | 1025 | // but needs coordination with the linker and libunwind. | 
|  | 1026 | if (FloatRegCount > 4) { | 
|  | 1027 | DEBUG_WITH_TYPE("compact-unwind", | 
|  | 1028 | llvm::dbgs() << "unsupported number of D registers saved (" | 
|  | 1029 | << FloatRegCount << ")\n"); | 
|  | 1030 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 1031 | } | 
|  | 1032 |  | 
|  | 1033 | // Floating point registers must either be saved sequentially, or we defer to | 
|  | 1034 | // DWARF. No gaps allowed here so check that each saved d-register is | 
|  | 1035 | // precisely where it should be. | 
|  | 1036 | static unsigned FPRCSRegs[] = { ARM::D8, ARM::D10, ARM::D12, ARM::D14 }; | 
|  | 1037 | for (int Idx = FloatRegCount - 1; Idx >= 0; --Idx) { | 
|  | 1038 | auto Offset = RegOffsets.find(FPRCSRegs[Idx]); | 
|  | 1039 | if (Offset == RegOffsets.end()) { | 
|  | 1040 | DEBUG_WITH_TYPE("compact-unwind", | 
|  | 1041 | llvm::dbgs() << FloatRegCount << " D-regs saved, but " | 
|  | 1042 | << MRI.getName(FPRCSRegs[Idx]) | 
|  | 1043 | << " not saved\n"); | 
|  | 1044 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 1045 | } else if (Offset->second != CurOffset - 8) { | 
|  | 1046 | DEBUG_WITH_TYPE("compact-unwind", | 
|  | 1047 | llvm::dbgs() << FloatRegCount << " D-regs saved, but " | 
|  | 1048 | << MRI.getName(FPRCSRegs[Idx]) | 
|  | 1049 | << " saved at " << Offset->second | 
|  | 1050 | << ", expected at " << CurOffset - 8 | 
|  | 1051 | << "\n"); | 
|  | 1052 | return CU::UNWIND_ARM_MODE_DWARF; | 
|  | 1053 | } | 
|  | 1054 | CurOffset -= 8; | 
|  | 1055 | } | 
|  | 1056 |  | 
|  | 1057 | return CompactUnwindEncoding | ((FloatRegCount - 1) << 8); | 
|  | 1058 | } | 
|  | 1059 |  | 
| Vedant Kumar | 366dd9fd | 2015-08-21 21:52:48 +0000 | [diff] [blame] | 1060 | static MachO::CPUSubTypeARM getMachOSubTypeFromArch(StringRef Arch) { | 
| Chandler Carruth | bb47b9a | 2015-08-30 02:09:48 +0000 | [diff] [blame] | 1061 | unsigned AK = ARM::parseArch(Arch); | 
| Vedant Kumar | 366dd9fd | 2015-08-21 21:52:48 +0000 | [diff] [blame] | 1062 | switch (AK) { | 
|  | 1063 | default: | 
|  | 1064 | return MachO::CPU_SUBTYPE_ARM_V7; | 
|  | 1065 | case ARM::AK_ARMV4T: | 
|  | 1066 | return MachO::CPU_SUBTYPE_ARM_V4T; | 
| Artyom Skrobov | 2c2f378 | 2015-11-12 15:51:41 +0000 | [diff] [blame] | 1067 | case ARM::AK_ARMV5T: | 
|  | 1068 | case ARM::AK_ARMV5TE: | 
|  | 1069 | case ARM::AK_ARMV5TEJ: | 
|  | 1070 | return MachO::CPU_SUBTYPE_ARM_V5; | 
| Vedant Kumar | 366dd9fd | 2015-08-21 21:52:48 +0000 | [diff] [blame] | 1071 | case ARM::AK_ARMV6: | 
|  | 1072 | case ARM::AK_ARMV6K: | 
|  | 1073 | return MachO::CPU_SUBTYPE_ARM_V6; | 
| Artyom Skrobov | 2c2f378 | 2015-11-12 15:51:41 +0000 | [diff] [blame] | 1074 | case ARM::AK_ARMV7A: | 
| Vedant Kumar | 366dd9fd | 2015-08-21 21:52:48 +0000 | [diff] [blame] | 1075 | return MachO::CPU_SUBTYPE_ARM_V7; | 
|  | 1076 | case ARM::AK_ARMV7S: | 
|  | 1077 | return MachO::CPU_SUBTYPE_ARM_V7S; | 
|  | 1078 | case ARM::AK_ARMV7K: | 
|  | 1079 | return MachO::CPU_SUBTYPE_ARM_V7K; | 
|  | 1080 | case ARM::AK_ARMV6M: | 
| Vedant Kumar | 366dd9fd | 2015-08-21 21:52:48 +0000 | [diff] [blame] | 1081 | return MachO::CPU_SUBTYPE_ARM_V6M; | 
|  | 1082 | case ARM::AK_ARMV7M: | 
|  | 1083 | return MachO::CPU_SUBTYPE_ARM_V7M; | 
|  | 1084 | case ARM::AK_ARMV7EM: | 
|  | 1085 | return MachO::CPU_SUBTYPE_ARM_V7EM; | 
|  | 1086 | } | 
|  | 1087 | } | 
|  | 1088 |  | 
| Bill Wendling | 58e2d3d | 2013-09-09 02:37:14 +0000 | [diff] [blame] | 1089 | MCAsmBackend *llvm::createARMAsmBackend(const Target &T, | 
| Daniel Sanders | 418caf5 | 2015-06-10 10:35:34 +0000 | [diff] [blame] | 1090 | const MCRegisterInfo &MRI, | 
| Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1091 | const Triple &TheTriple, StringRef CPU, | 
| Daniel Sanders | 418caf5 | 2015-06-10 10:35:34 +0000 | [diff] [blame] | 1092 | bool isLittle) { | 
| Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1093 | switch (TheTriple.getObjectFormat()) { | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 1094 | default: | 
|  | 1095 | llvm_unreachable("unsupported object format"); | 
| Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1096 | case Triple::MachO: { | 
|  | 1097 | MachO::CPUSubTypeARM CS = getMachOSubTypeFromArch(TheTriple.getArchName()); | 
| Tim Northover | f8e47e4 | 2015-10-28 22:56:36 +0000 | [diff] [blame] | 1098 | return new ARMAsmBackendDarwin(T, TheTriple, MRI, CS); | 
| Owen Anderson | 975ddf8 | 2011-04-01 21:07:39 +0000 | [diff] [blame] | 1099 | } | 
| Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1100 | case Triple::COFF: | 
|  | 1101 | assert(TheTriple.isOSWindows() && "non-Windows ARM COFF is not supported"); | 
|  | 1102 | return new ARMAsmBackendWinCOFF(T, TheTriple); | 
|  | 1103 | case Triple::ELF: | 
|  | 1104 | assert(TheTriple.isOSBinFormatELF() && "using ELF for non-ELF target"); | 
|  | 1105 | uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS()); | 
|  | 1106 | return new ARMAsmBackendELF(T, TheTriple, OSABI, isLittle); | 
| Saleem Abdulrasool | 84b952b | 2014-04-27 03:48:22 +0000 | [diff] [blame] | 1107 | } | 
| Jason W Kim | b321245 | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 1108 | } | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 1109 |  | 
| Christian Pirker | dc9ff75 | 2014-04-01 15:19:30 +0000 | [diff] [blame] | 1110 | MCAsmBackend *llvm::createARMLEAsmBackend(const Target &T, | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 1111 | const MCRegisterInfo &MRI, | 
| Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1112 | const Triple &TT, StringRef CPU) { | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 1113 | return createARMAsmBackend(T, MRI, TT, CPU, true); | 
|  | 1114 | } | 
|  | 1115 |  | 
| Christian Pirker | dc9ff75 | 2014-04-01 15:19:30 +0000 | [diff] [blame] | 1116 | MCAsmBackend *llvm::createARMBEAsmBackend(const Target &T, | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 1117 | const MCRegisterInfo &MRI, | 
| Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1118 | const Triple &TT, StringRef CPU) { | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 1119 | return createARMAsmBackend(T, MRI, TT, CPU, false); | 
|  | 1120 | } | 
|  | 1121 |  | 
| Christian Pirker | dc9ff75 | 2014-04-01 15:19:30 +0000 | [diff] [blame] | 1122 | MCAsmBackend *llvm::createThumbLEAsmBackend(const Target &T, | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 1123 | const MCRegisterInfo &MRI, | 
| Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1124 | const Triple &TT, StringRef CPU) { | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 1125 | return createARMAsmBackend(T, MRI, TT, CPU, true); | 
|  | 1126 | } | 
|  | 1127 |  | 
| Christian Pirker | dc9ff75 | 2014-04-01 15:19:30 +0000 | [diff] [blame] | 1128 | MCAsmBackend *llvm::createThumbBEAsmBackend(const Target &T, | 
| Joe Abbey | 8e72eb7 | 2014-09-16 09:18:23 +0000 | [diff] [blame] | 1129 | const MCRegisterInfo &MRI, | 
| Daniel Sanders | 50f1723 | 2015-09-15 16:17:27 +0000 | [diff] [blame] | 1130 | const Triple &TT, StringRef CPU) { | 
| Christian Pirker | 2a11160 | 2014-03-28 14:35:30 +0000 | [diff] [blame] | 1131 | return createARMAsmBackend(T, MRI, TT, CPU, false); | 
|  | 1132 | } |