Jason W Kim | d4d4f4f | 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 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 10 | #include "ARM.h" |
Jim Grosbach | dff84b0 | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 11 | #include "ARMAddressingModes.h" |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 12 | #include "ARMFixupKinds.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 13 | #include "llvm/ADT/Twine.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCAssembler.h" |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCDirectives.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCExpr.h" |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCObjectFormat.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCObjectWriter.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCSectionELF.h" |
| 20 | #include "llvm/MC/MCSectionMachO.h" |
Daniel Dunbar | 36d76a8 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 21 | #include "llvm/Object/MachOFormat.h" |
Wesley Peck | eecb858 | 2010-10-22 15:52:49 +0000 | [diff] [blame] | 22 | #include "llvm/Support/ELF.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
| 24 | #include "llvm/Support/raw_ostream.h" |
Jim Grosbach | dff84b0 | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 25 | #include "llvm/Target/TargetAsmBackend.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetRegistry.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
| 29 | namespace { |
| 30 | class ARMAsmBackend : public TargetAsmBackend { |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 31 | bool isThumbMode; // Currently emitting Thumb code. |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 32 | public: |
Jim Grosbach | 022ab37 | 2010-12-08 15:36:45 +0000 | [diff] [blame] | 33 | ARMAsmBackend(const Target &T) : TargetAsmBackend(), isThumbMode(false) {} |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 34 | |
| 35 | bool MayNeedRelaxation(const MCInst &Inst) const; |
| 36 | |
| 37 | void RelaxInstruction(const MCInst &Inst, MCInst &Res) const; |
| 38 | |
| 39 | bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const; |
Jim Grosbach | 3787a40 | 2010-09-30 17:45:51 +0000 | [diff] [blame] | 40 | |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 41 | void HandleAssemblerFlag(MCAssemblerFlag Flag) { |
| 42 | switch (Flag) { |
| 43 | default: break; |
| 44 | case MCAF_Code16: |
| 45 | setIsThumb(true); |
| 46 | break; |
| 47 | case MCAF_Code32: |
| 48 | setIsThumb(false); |
| 49 | break; |
| 50 | } |
Jim Grosbach | 3787a40 | 2010-09-30 17:45:51 +0000 | [diff] [blame] | 51 | } |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 52 | |
| 53 | unsigned getPointerSize() const { return 4; } |
| 54 | bool isThumb() const { return isThumbMode; } |
| 55 | void setIsThumb(bool it) { isThumbMode = it; } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 56 | }; |
Chris Lattner | b75c651 | 2010-11-17 05:41:32 +0000 | [diff] [blame] | 57 | } // end anonymous namespace |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 58 | |
| 59 | bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const { |
| 60 | // FIXME: Thumb targets, different move constant targets.. |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const { |
| 65 | assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented"); |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const { |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 70 | if (isThumb()) { |
| 71 | assert (((Count & 1) == 0) && "Unaligned Nop data fragment!"); |
| 72 | // FIXME: 0xbf00 is the ARMv7 value. For v6 and before, we'll need to |
| 73 | // use 0x46c0 (which is a 'mov r8, r8' insn). |
| 74 | Count /= 2; |
| 75 | for (uint64_t i = 0; i != Count; ++i) |
| 76 | OW->Write16(0xbf00); |
| 77 | return true; |
| 78 | } |
| 79 | // ARM mode |
| 80 | Count /= 4; |
Jim Grosbach | e50e6bc | 2010-11-11 23:41:09 +0000 | [diff] [blame] | 81 | for (uint64_t i = 0; i != Count; ++i) |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 82 | OW->Write32(0xe1a00000); |
Rafael Espindola | cecbc3d | 2010-10-25 17:50:35 +0000 | [diff] [blame] | 83 | return true; |
Jim Grosbach | 87dc3aa | 2010-09-30 03:20:34 +0000 | [diff] [blame] | 84 | } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 85 | |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 86 | static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) { |
| 87 | switch (Kind) { |
| 88 | default: |
| 89 | llvm_unreachable("Unknown fixup kind!"); |
| 90 | case FK_Data_4: |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 91 | return Value; |
Jason W Kim | 2ccf148 | 2010-12-03 19:40:23 +0000 | [diff] [blame] | 92 | case ARM::fixup_arm_movt_hi16: |
| 93 | case ARM::fixup_arm_movw_lo16: { |
| 94 | unsigned Hi4 = (Value & 0xF000) >> 12; |
| 95 | unsigned Lo12 = Value & 0x0FFF; |
| 96 | // inst{19-16} = Hi4; |
| 97 | // inst{11-0} = Lo12; |
| 98 | Value = (Hi4 << 16) | (Lo12); |
| 99 | return Value; |
| 100 | } |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 101 | case ARM::fixup_arm_ldst_pcrel_12: |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 102 | // ARM PC-relative values are offset by 8. |
Owen Anderson | 05018c2 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 103 | Value -= 4; |
Owen Anderson | fe7fac7 | 2010-12-09 21:34:47 +0000 | [diff] [blame] | 104 | // FALLTHROUGH |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 105 | case ARM::fixup_t2_ldst_pcrel_12: { |
| 106 | // Offset by 4, adjusted by two due to the half-word ordering of thumb. |
Owen Anderson | 05018c2 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 107 | Value -= 4; |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 108 | bool isAdd = true; |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 109 | if ((int64_t)Value < 0) { |
| 110 | Value = -Value; |
| 111 | isAdd = false; |
| 112 | } |
| 113 | assert ((Value < 4096) && "Out of range pc-relative fixup value!"); |
| 114 | Value |= isAdd << 23; |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 115 | |
| 116 | // Same addressing mode as fixup_arm_pcrel_10, |
| 117 | // but with 16-bit halfwords swapped. |
| 118 | if (Kind == ARM::fixup_t2_ldst_pcrel_12) { |
| 119 | uint64_t swapped = (Value & 0xFFFF0000) >> 16; |
| 120 | swapped |= (Value & 0x0000FFFF) << 16; |
| 121 | return swapped; |
| 122 | } |
| 123 | |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 124 | return Value; |
| 125 | } |
Jim Grosbach | dff84b0 | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 126 | case ARM::fixup_arm_adr_pcrel_12: { |
| 127 | // ARM PC-relative values are offset by 8. |
| 128 | Value -= 8; |
| 129 | unsigned opc = 4; // bits {24-21}. Default to add: 0b0100 |
| 130 | if ((int64_t)Value < 0) { |
| 131 | Value = -Value; |
| 132 | opc = 2; // 0b0010 |
| 133 | } |
| 134 | assert(ARM_AM::getSOImmVal(Value) != -1 && |
| 135 | "Out of range pc-relative fixup value!"); |
| 136 | // Encode the immediate and shift the opcode into place. |
| 137 | return ARM_AM::getSOImmVal(Value) | (opc << 21); |
| 138 | } |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 139 | case ARM::fixup_arm_branch: |
| 140 | // These values don't encode the low two bits since they're always zero. |
| 141 | // Offset by 8 just as above. |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 142 | return 0xffffff & ((Value - 8) >> 2); |
Owen Anderson | fb20d89 | 2010-12-09 00:27:41 +0000 | [diff] [blame] | 143 | case ARM::fixup_t2_branch: { |
Owen Anderson | 63ee220 | 2010-12-10 23:02:28 +0000 | [diff] [blame^] | 144 | Value = Value - 4; |
Owen Anderson | fb20d89 | 2010-12-09 00:27:41 +0000 | [diff] [blame] | 145 | Value >>= 1; // Low bit is not encoded. |
| 146 | |
| 147 | uint64_t out = 0; |
Owen Anderson | 8f07943 | 2010-12-09 01:02:09 +0000 | [diff] [blame] | 148 | out |= (Value & 0x80000) << 7; // S bit |
| 149 | out |= (Value & 0x40000) >> 7; // J2 bit |
| 150 | out |= (Value & 0x20000) >> 4; // J1 bit |
| 151 | out |= (Value & 0x1F800) << 5; // imm6 field |
| 152 | out |= (Value & 0x007FF); // imm11 field |
Owen Anderson | fb20d89 | 2010-12-09 00:27:41 +0000 | [diff] [blame] | 153 | |
| 154 | uint64_t swapped = (out & 0xFFFF0000) >> 16; |
| 155 | swapped |= (out & 0x0000FFFF) << 16; |
| 156 | return swapped; |
| 157 | } |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 158 | case ARM::fixup_arm_thumb_bl: { |
| 159 | // The value doesn't encode the low bit (always zero) and is offset by |
| 160 | // four. The value is encoded into disjoint bit positions in the destination |
| 161 | // opcode. x = unchanged, I = immediate value bit, S = sign extension bit |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 162 | // |
| 163 | // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII |
| 164 | // |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 165 | // Note that the halfwords are stored high first, low second; so we need |
| 166 | // to transpose the fixup value here to map properly. |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 167 | unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0; |
Bill Wendling | 797b7aa | 2010-12-09 00:44:33 +0000 | [diff] [blame] | 168 | uint32_t Binary = 0; |
| 169 | Value = 0x3fffff & ((Value - 4) >> 1); |
| 170 | Binary = (Value & 0x7ff) << 16; // Low imm11 value. |
| 171 | Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value. |
| 172 | Binary |= isNeg << 10; // Sign bit. |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 173 | return Binary; |
| 174 | } |
| 175 | case ARM::fixup_arm_thumb_blx: { |
| 176 | // The value doesn't encode the low two bits (always zero) and is offset by |
| 177 | // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit |
| 178 | // positions in the destination opcode. x = unchanged, I = immediate value |
| 179 | // bit, S = sign extension bit, 0 = zero. |
| 180 | // |
| 181 | // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0 |
| 182 | // |
| 183 | // Note that the halfwords are stored high first, low second; so we need |
| 184 | // to transpose the fixup value here to map properly. |
| 185 | unsigned isNeg = (int64_t(Value) < 0) ? 1 : 0; |
Bill Wendling | 797b7aa | 2010-12-09 00:44:33 +0000 | [diff] [blame] | 186 | uint32_t Binary = 0; |
| 187 | Value = 0xfffff & ((Value - 2) >> 2); |
| 188 | Binary = (Value & 0x3ff) << 17; // Low imm10L value. |
| 189 | Binary |= (Value & 0xffc00) >> 10; // High imm10H value. |
| 190 | Binary |= isNeg << 10; // Sign bit. |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 191 | return Binary; |
| 192 | } |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 193 | case ARM::fixup_arm_thumb_cp: |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 194 | // Offset by 4, and don't encode the low two bits. Two bytes of that |
| 195 | // 'off by 4' is implicitly handled by the half-word ordering of the |
| 196 | // Thumb encoding, so we only need to adjust by 2 here. |
| 197 | return ((Value - 2) >> 2) & 0xff; |
Jim Grosbach | b492a7c | 2010-12-09 19:50:12 +0000 | [diff] [blame] | 198 | case ARM::fixup_arm_thumb_cb: { |
Bill Wendling | dff2f71 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 199 | // Offset by 4 and don't encode the lower bit, which is always 0. |
| 200 | uint32_t Binary = (Value - 4) >> 1; |
| 201 | return ((Binary & 0x20) << 9) | ((Binary & 0x1f) << 3); |
| 202 | } |
Jim Grosbach | e246717 | 2010-12-10 18:21:33 +0000 | [diff] [blame] | 203 | case ARM::fixup_arm_thumb_br: |
| 204 | // Offset by 4 and don't encode the lower bit, which is always 0. |
| 205 | return ((Value - 4) >> 1) & 0x7ff; |
Jim Grosbach | 0108645 | 2010-12-10 17:13:40 +0000 | [diff] [blame] | 206 | case ARM::fixup_arm_thumb_bcc: |
| 207 | // Offset by 4 and don't encode the lower bit, which is always 0. |
| 208 | return ((Value - 4) >> 1) & 0xff; |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 209 | case ARM::fixup_arm_pcrel_10: |
Owen Anderson | e2e0f58 | 2010-12-10 22:46:47 +0000 | [diff] [blame] | 210 | Value = Value - 4; // ARM fixups offset by an additional word and don't |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 211 | // need to adjust for the half-word ordering. |
| 212 | // Fall through. |
| 213 | case ARM::fixup_t2_pcrel_10: { |
| 214 | // Offset by 4, adjusted by two due to the half-word ordering of thumb. |
Owen Anderson | e2e0f58 | 2010-12-10 22:46:47 +0000 | [diff] [blame] | 215 | Value = Value - 4; |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 216 | bool isAdd = true; |
| 217 | if ((int64_t)Value < 0) { |
| 218 | Value = -Value; |
| 219 | isAdd = false; |
| 220 | } |
| 221 | // These values don't encode the low two bits since they're always zero. |
| 222 | Value >>= 2; |
| 223 | assert ((Value < 256) && "Out of range pc-relative fixup value!"); |
| 224 | Value |= isAdd << 23; |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 225 | |
Owen Anderson | cc78f5c | 2010-12-08 19:31:11 +0000 | [diff] [blame] | 226 | // Same addressing mode as fixup_arm_pcrel_10, |
| 227 | // but with 16-bit halfwords swapped. |
Owen Anderson | d8e351b | 2010-12-08 00:18:36 +0000 | [diff] [blame] | 228 | if (Kind == ARM::fixup_t2_pcrel_10) { |
Owen Anderson | 255eafb | 2010-12-08 00:21:33 +0000 | [diff] [blame] | 229 | uint64_t swapped = (Value & 0xFFFF0000) >> 16; |
| 230 | swapped |= (Value & 0x0000FFFF) << 16; |
Owen Anderson | d8e351b | 2010-12-08 00:18:36 +0000 | [diff] [blame] | 231 | return swapped; |
| 232 | } |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 233 | |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 234 | return Value; |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 239 | namespace { |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 240 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 241 | // FIXME: This should be in a separate file. |
| 242 | // ELF is an ELF of course... |
| 243 | class ELFARMAsmBackend : public ARMAsmBackend { |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 244 | MCELFObjectFormat Format; |
| 245 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 246 | public: |
| 247 | Triple::OSType OSType; |
| 248 | ELFARMAsmBackend(const Target &T, Triple::OSType _OSType) |
| 249 | : ARMAsmBackend(T), OSType(_OSType) { |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 250 | HasScatteredSymbols = true; |
| 251 | } |
| 252 | |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 253 | virtual const MCObjectFormat &getObjectFormat() const { |
| 254 | return Format; |
| 255 | } |
| 256 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 257 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 258 | uint64_t Value) const; |
| 259 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 260 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 261 | return createELFObjectWriter(OS, /*Is64Bit=*/false, |
| 262 | OSType, ELF::EM_ARM, |
| 263 | /*IsLittleEndian=*/true, |
| 264 | /*HasRelocationAddend=*/false); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 265 | } |
| 266 | }; |
| 267 | |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 268 | // FIXME: Raise this to share code between Darwin and ELF. |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 269 | void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data, |
| 270 | unsigned DataSize, uint64_t Value) const { |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 271 | unsigned NumBytes = 4; // FIXME: 2 for Thumb |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 272 | Value = adjustFixupValue(Fixup.getKind(), Value); |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 273 | if (!Value) return; // Doesn't change encoding. |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 274 | |
| 275 | unsigned Offset = Fixup.getOffset(); |
| 276 | assert(Offset % NumBytes == 0 && "Offset mod NumBytes is nonzero!"); |
| 277 | |
| 278 | // For each byte of the fragment that the fixup touches, mask in the bits from |
| 279 | // the fixup value. The Value has been "split up" into the appropriate |
| 280 | // bitfields above. |
| 281 | for (unsigned i = 0; i != NumBytes; ++i) |
| 282 | Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | // FIXME: This should be in a separate file. |
| 286 | class DarwinARMAsmBackend : public ARMAsmBackend { |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 287 | MCMachOObjectFormat Format; |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 288 | public: |
Chris Lattner | b75c651 | 2010-11-17 05:41:32 +0000 | [diff] [blame] | 289 | DarwinARMAsmBackend(const Target &T) : ARMAsmBackend(T) { |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 290 | HasScatteredSymbols = true; |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Rafael Espindola | f230df9 | 2010-10-16 18:23:53 +0000 | [diff] [blame] | 293 | virtual const MCObjectFormat &getObjectFormat() const { |
| 294 | return Format; |
| 295 | } |
| 296 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 297 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 298 | uint64_t Value) const; |
| 299 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 300 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
Jim Grosbach | c9d1439 | 2010-11-05 18:48:58 +0000 | [diff] [blame] | 301 | // FIXME: Subtarget info should be derived. Force v7 for now. |
Daniel Dunbar | 36d76a8 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 302 | return createMachObjectWriter(OS, /*Is64Bit=*/false, |
| 303 | object::mach::CTM_ARM, |
| 304 | object::mach::CSARM_V7, |
Daniel Dunbar | 115a3dd | 2010-11-13 07:33:40 +0000 | [diff] [blame] | 305 | /*IsLittleEndian=*/true); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 309 | return false; |
| 310 | } |
| 311 | }; |
| 312 | |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 313 | /// getFixupKindNumBytes - The number of bytes the fixup may change. |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 314 | static unsigned getFixupKindNumBytes(unsigned Kind) { |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 315 | switch (Kind) { |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 316 | default: |
| 317 | llvm_unreachable("Unknown fixup kind!"); |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 318 | |
Jim Grosbach | 0108645 | 2010-12-10 17:13:40 +0000 | [diff] [blame] | 319 | case ARM::fixup_arm_thumb_bcc: |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 320 | case ARM::fixup_arm_thumb_cp: |
| 321 | return 1; |
| 322 | |
Jim Grosbach | e246717 | 2010-12-10 18:21:33 +0000 | [diff] [blame] | 323 | case ARM::fixup_arm_thumb_br: |
Jim Grosbach | b492a7c | 2010-12-09 19:50:12 +0000 | [diff] [blame] | 324 | case ARM::fixup_arm_thumb_cb: |
Bill Wendling | dff2f71 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 325 | return 2; |
| 326 | |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 327 | case ARM::fixup_arm_ldst_pcrel_12: |
| 328 | case ARM::fixup_arm_pcrel_10: |
| 329 | case ARM::fixup_arm_adr_pcrel_12: |
| 330 | case ARM::fixup_arm_branch: |
| 331 | return 3; |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 332 | |
| 333 | case FK_Data_4: |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 334 | case ARM::fixup_t2_ldst_pcrel_12: |
Owen Anderson | fb20d89 | 2010-12-09 00:27:41 +0000 | [diff] [blame] | 335 | case ARM::fixup_t2_branch: |
Owen Anderson | d8e351b | 2010-12-08 00:18:36 +0000 | [diff] [blame] | 336 | case ARM::fixup_t2_pcrel_10: |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 337 | case ARM::fixup_arm_thumb_bl: |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 338 | case ARM::fixup_arm_thumb_blx: |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 339 | return 4; |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 340 | } |
| 341 | } |
| 342 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 343 | void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data, |
| 344 | unsigned DataSize, uint64_t Value) const { |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 345 | unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind()); |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 346 | Value = adjustFixupValue(Fixup.getKind(), Value); |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 347 | if (!Value) return; // Doesn't change encoding. |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 348 | |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 349 | unsigned Offset = Fixup.getOffset(); |
| 350 | assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!"); |
| 351 | |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 352 | // For each byte of the fragment that the fixup touches, mask in the |
| 353 | // bits from the fixup value. |
| 354 | for (unsigned i = 0; i != NumBytes; ++i) |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 355 | Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 356 | } |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 357 | |
Jim Grosbach | f73fd72 | 2010-09-30 03:21:00 +0000 | [diff] [blame] | 358 | } // end anonymous namespace |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 359 | |
| 360 | TargetAsmBackend *llvm::createARMAsmBackend(const Target &T, |
| 361 | const std::string &TT) { |
| 362 | switch (Triple(TT).getOS()) { |
| 363 | case Triple::Darwin: |
| 364 | return new DarwinARMAsmBackend(T); |
| 365 | case Triple::MinGW32: |
| 366 | case Triple::Cygwin: |
| 367 | case Triple::Win32: |
| 368 | assert(0 && "Windows not supported on ARM"); |
| 369 | default: |
| 370 | return new ELFARMAsmBackend(T, Triple(TT).getOS()); |
| 371 | } |
| 372 | } |