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