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 | |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/ARMMCTargetDesc.h" |
Evan Cheng | be74029 | 2011-07-23 00:00:19 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/ARMBaseInfo.h" |
| 12 | #include "MCTargetDesc/ARMFixupKinds.h" |
Evan Cheng | ee04a6d | 2011-07-20 23:34:39 +0000 | [diff] [blame] | 13 | #include "MCTargetDesc/ARMAddressingModes.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/Twine.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 15 | #include "llvm/MC/MCAssembler.h" |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 16 | #include "llvm/MC/MCDirectives.h" |
Rafael Espindola | 285b3e5 | 2010-12-17 16:59:53 +0000 | [diff] [blame] | 17 | #include "llvm/MC/MCELFObjectWriter.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 18 | #include "llvm/MC/MCExpr.h" |
Daniel Dunbar | aa4b7dd | 2010-12-16 16:08:33 +0000 | [diff] [blame] | 19 | #include "llvm/MC/MCMachObjectWriter.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCObjectWriter.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 21 | #include "llvm/MC/MCSectionELF.h" |
| 22 | #include "llvm/MC/MCSectionMachO.h" |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 23 | #include "llvm/MC/MCAsmBackend.h" |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 24 | #include "llvm/MC/MCSubtargetInfo.h" |
Daniel Dunbar | 36d76a8 | 2010-11-27 04:38:36 +0000 | [diff] [blame] | 25 | #include "llvm/Object/MachOFormat.h" |
Wesley Peck | eecb858 | 2010-10-22 15:52:49 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ELF.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 27 | #include "llvm/Support/ErrorHandling.h" |
| 28 | #include "llvm/Support/raw_ostream.h" |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 29 | using namespace llvm; |
| 30 | |
| 31 | namespace { |
Rafael Espindola | 6024c97 | 2010-12-17 17:45:22 +0000 | [diff] [blame] | 32 | class ARMELFObjectWriter : public MCELFObjectTargetWriter { |
| 33 | public: |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 34 | ARMELFObjectWriter(Triple::OSType OSType) |
| 35 | : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSType, ELF::EM_ARM, |
| 36 | /*HasRelocationAddend*/ false) {} |
Rafael Espindola | 6024c97 | 2010-12-17 17:45:22 +0000 | [diff] [blame] | 37 | }; |
| 38 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 39 | class ARMAsmBackend : public MCAsmBackend { |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 40 | const MCSubtargetInfo* STI; |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 41 | bool isThumbMode; // Currently emitting Thumb code. |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 42 | public: |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 43 | ARMAsmBackend(const Target &T, const StringRef TT) |
| 44 | : MCAsmBackend(), STI(ARM_MC::createARMMCSubtargetInfo(TT, "", "")), |
Jim Grosbach | b9d3ff8 | 2011-08-24 22:27:35 +0000 | [diff] [blame] | 45 | isThumbMode(TT.startswith("thumb")) {} |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 46 | |
| 47 | ~ARMAsmBackend() { |
| 48 | delete STI; |
| 49 | } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 50 | |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 51 | unsigned getNumFixupKinds() const { return ARM::NumTargetFixupKinds; } |
| 52 | |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 53 | bool hasNOP() const { |
| 54 | return (STI->getFeatureBits() & ARM::HasV6T2Ops) != 0; |
| 55 | } |
| 56 | |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 57 | const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const { |
| 58 | const static MCFixupKindInfo Infos[ARM::NumTargetFixupKinds] = { |
| 59 | // This table *must* be in the order that the fixup_* kinds are defined in |
| 60 | // ARMFixupKinds.h. |
| 61 | // |
| 62 | // Name Offset (bits) Size (bits) Flags |
| 63 | { "fixup_arm_ldst_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 64 | { "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel | |
| 65 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, |
| 66 | { "fixup_arm_pcrel_10", 1, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 67 | { "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel | |
| 68 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, |
| 69 | { "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel | |
| 70 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, |
| 71 | { "fixup_arm_adr_pcrel_12", 1, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 72 | { "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel | |
| 73 | MCFixupKindInfo::FKF_IsAlignedDownTo32Bits}, |
Jason W Kim | 685c350 | 2011-02-04 19:47:15 +0000 | [diff] [blame] | 74 | { "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel }, |
| 75 | { "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel }, |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 76 | { "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel }, |
| 77 | { "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel }, |
| 78 | { "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
| 79 | { "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel }, |
Jim Grosbach | 90b5a08 | 2011-08-18 16:57:50 +0000 | [diff] [blame] | 80 | { "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel }, |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 81 | { "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel }, |
Jim Grosbach | 67b95f9 | 2011-08-19 18:20:48 +0000 | [diff] [blame] | 82 | { "fixup_arm_thumb_cp", 0, 8, MCFixupKindInfo::FKF_IsPCRel }, |
Eric Christopher | fea51fc | 2011-05-28 03:16:22 +0000 | [diff] [blame] | 83 | { "fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel }, |
Evan Cheng | f3eb3bb | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 84 | // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19. |
| 85 | { "fixup_arm_movt_hi16", 0, 20, 0 }, |
| 86 | { "fixup_arm_movw_lo16", 0, 20, 0 }, |
| 87 | { "fixup_t2_movt_hi16", 0, 20, 0 }, |
| 88 | { "fixup_t2_movw_lo16", 0, 20, 0 }, |
| 89 | { "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel }, |
| 90 | { "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel }, |
| 91 | { "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel }, |
| 92 | { "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel }, |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | if (Kind < FirstTargetFixupKind) |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 96 | return MCAsmBackend::getFixupKindInfo(Kind); |
Daniel Dunbar | 2761fc4 | 2010-12-16 03:20:06 +0000 | [diff] [blame] | 97 | |
| 98 | assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 99 | "Invalid kind!"); |
| 100 | return Infos[Kind - FirstTargetFixupKind]; |
| 101 | } |
| 102 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 103 | bool MayNeedRelaxation(const MCInst &Inst) const; |
| 104 | |
| 105 | void RelaxInstruction(const MCInst &Inst, MCInst &Res) const; |
| 106 | |
| 107 | bool WriteNopData(uint64_t Count, MCObjectWriter *OW) const; |
Jim Grosbach | 3787a40 | 2010-09-30 17:45:51 +0000 | [diff] [blame] | 108 | |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 109 | void HandleAssemblerFlag(MCAssemblerFlag Flag) { |
| 110 | switch (Flag) { |
| 111 | default: break; |
| 112 | case MCAF_Code16: |
| 113 | setIsThumb(true); |
| 114 | break; |
| 115 | case MCAF_Code32: |
| 116 | setIsThumb(false); |
| 117 | break; |
| 118 | } |
Jim Grosbach | 3787a40 | 2010-09-30 17:45:51 +0000 | [diff] [blame] | 119 | } |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 120 | |
| 121 | unsigned getPointerSize() const { return 4; } |
| 122 | bool isThumb() const { return isThumbMode; } |
| 123 | void setIsThumb(bool it) { isThumbMode = it; } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 124 | }; |
Chris Lattner | b75c651 | 2010-11-17 05:41:32 +0000 | [diff] [blame] | 125 | } // end anonymous namespace |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 126 | |
| 127 | bool ARMAsmBackend::MayNeedRelaxation(const MCInst &Inst) const { |
| 128 | // FIXME: Thumb targets, different move constant targets.. |
| 129 | return false; |
| 130 | } |
| 131 | |
| 132 | void ARMAsmBackend::RelaxInstruction(const MCInst &Inst, MCInst &Res) const { |
| 133 | assert(0 && "ARMAsmBackend::RelaxInstruction() unimplemented"); |
| 134 | return; |
| 135 | } |
| 136 | |
| 137 | bool ARMAsmBackend::WriteNopData(uint64_t Count, MCObjectWriter *OW) const { |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 138 | const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8 |
| 139 | const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP |
| 140 | const uint32_t ARMv4_NopEncoding = 0xe1a0000; // using MOV r0,r0 |
| 141 | const uint32_t ARMv6T2_NopEncoding = 0xe3207800; // NOP |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 142 | if (isThumb()) { |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 143 | const uint16_t nopEncoding = hasNOP() ? Thumb2_16bitNopEncoding |
| 144 | : Thumb1_16bitNopEncoding; |
Jim Grosbach | a3dbd3a | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 145 | uint64_t NumNops = Count / 2; |
| 146 | for (uint64_t i = 0; i != NumNops; ++i) |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 147 | OW->Write16(nopEncoding); |
Jim Grosbach | a3dbd3a | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 148 | if (Count & 1) |
| 149 | OW->Write8(0); |
Jim Grosbach | 5be6d2a | 2010-12-08 01:16:55 +0000 | [diff] [blame] | 150 | return true; |
| 151 | } |
| 152 | // ARM mode |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 153 | const uint32_t nopEncoding = hasNOP() ? ARMv6T2_NopEncoding |
| 154 | : ARMv4_NopEncoding; |
Jim Grosbach | a3dbd3a | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 155 | uint64_t NumNops = Count / 4; |
| 156 | for (uint64_t i = 0; i != NumNops; ++i) |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 157 | OW->Write32(nopEncoding); |
| 158 | // FIXME: should this function return false when unable to write exactly |
| 159 | // 'Count' bytes with NOP encodings? |
Jim Grosbach | a3dbd3a | 2010-12-17 19:03:02 +0000 | [diff] [blame] | 160 | switch (Count % 4) { |
| 161 | default: break; // No leftover bytes to write |
| 162 | case 1: OW->Write8(0); break; |
| 163 | case 2: OW->Write16(0); break; |
| 164 | case 3: OW->Write16(0); OW->Write8(0xa0); break; |
| 165 | } |
| 166 | |
Rafael Espindola | cecbc3d | 2010-10-25 17:50:35 +0000 | [diff] [blame] | 167 | return true; |
Jim Grosbach | 87dc3aa | 2010-09-30 03:20:34 +0000 | [diff] [blame] | 168 | } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 169 | |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 170 | static unsigned adjustFixupValue(unsigned Kind, uint64_t Value) { |
| 171 | switch (Kind) { |
| 172 | default: |
| 173 | llvm_unreachable("Unknown fixup kind!"); |
Jim Grosbach | 6ec6eeb | 2010-12-17 18:39:10 +0000 | [diff] [blame] | 174 | case FK_Data_1: |
| 175 | case FK_Data_2: |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 176 | case FK_Data_4: |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 177 | return Value; |
Jason W Kim | 2ccf148 | 2010-12-03 19:40:23 +0000 | [diff] [blame] | 178 | case ARM::fixup_arm_movt_hi16: |
Evan Cheng | f3eb3bb | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 179 | Value >>= 16; |
| 180 | // Fallthrough |
| 181 | case ARM::fixup_arm_movw_lo16: |
Jason W Kim | 861b9c6 | 2011-05-19 20:55:25 +0000 | [diff] [blame] | 182 | case ARM::fixup_arm_movt_hi16_pcrel: |
Jason W Kim | 86a97f2 | 2011-01-12 00:19:25 +0000 | [diff] [blame] | 183 | case ARM::fixup_arm_movw_lo16_pcrel: { |
Jason W Kim | 2ccf148 | 2010-12-03 19:40:23 +0000 | [diff] [blame] | 184 | unsigned Hi4 = (Value & 0xF000) >> 12; |
| 185 | unsigned Lo12 = Value & 0x0FFF; |
Jason W Kim | 861b9c6 | 2011-05-19 20:55:25 +0000 | [diff] [blame] | 186 | assert ((((int64_t)Value) >= -0x8000) && (((int64_t)Value) <= 0x7fff) && |
| 187 | "Out of range pc-relative fixup value!"); |
Jason W Kim | 2ccf148 | 2010-12-03 19:40:23 +0000 | [diff] [blame] | 188 | // inst{19-16} = Hi4; |
| 189 | // inst{11-0} = Lo12; |
| 190 | Value = (Hi4 << 16) | (Lo12); |
| 191 | return Value; |
| 192 | } |
Evan Cheng | f3eb3bb | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 193 | case ARM::fixup_t2_movt_hi16: |
Evan Cheng | f3eb3bb | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 194 | Value >>= 16; |
| 195 | // Fallthrough |
| 196 | case ARM::fixup_t2_movw_lo16: |
Jim Grosbach | 8b45456 | 2011-06-24 20:06:59 +0000 | [diff] [blame] | 197 | case ARM::fixup_t2_movt_hi16_pcrel: //FIXME: Shouldn't this be shifted like |
| 198 | // the other hi16 fixup? |
Evan Cheng | f3eb3bb | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 199 | case ARM::fixup_t2_movw_lo16_pcrel: { |
| 200 | unsigned Hi4 = (Value & 0xF000) >> 12; |
| 201 | unsigned i = (Value & 0x800) >> 11; |
| 202 | unsigned Mid3 = (Value & 0x700) >> 8; |
| 203 | unsigned Lo8 = Value & 0x0FF; |
| 204 | // inst{19-16} = Hi4; |
| 205 | // inst{26} = i; |
| 206 | // inst{14-12} = Mid3; |
| 207 | // inst{7-0} = Lo8; |
Jim Grosbach | 8b45456 | 2011-06-24 20:06:59 +0000 | [diff] [blame] | 208 | // The value comes in as the whole thing, not just the portion required |
| 209 | // for this fixup, so we need to mask off the bits not handled by this |
| 210 | // portion (lo vs. hi). |
| 211 | Value &= 0xffff; |
Evan Cheng | f3eb3bb | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 212 | Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8); |
Evan Cheng | f3eb3bb | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 213 | uint64_t swapped = (Value & 0xFFFF0000) >> 16; |
| 214 | swapped |= (Value & 0x0000FFFF) << 16; |
| 215 | return swapped; |
| 216 | } |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 217 | case ARM::fixup_arm_ldst_pcrel_12: |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 218 | // ARM PC-relative values are offset by 8. |
Owen Anderson | 05018c2 | 2010-12-09 20:27:52 +0000 | [diff] [blame] | 219 | Value -= 4; |
Owen Anderson | fe7fac7 | 2010-12-09 21:34:47 +0000 | [diff] [blame] | 220 | // FALLTHROUGH |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 221 | case ARM::fixup_t2_ldst_pcrel_12: { |
| 222 | // 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] | 223 | Value -= 4; |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 224 | bool isAdd = true; |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 225 | if ((int64_t)Value < 0) { |
| 226 | Value = -Value; |
| 227 | isAdd = false; |
| 228 | } |
| 229 | assert ((Value < 4096) && "Out of range pc-relative fixup value!"); |
| 230 | Value |= isAdd << 23; |
Jim Grosbach | 7e294cf | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 231 | |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 232 | // Same addressing mode as fixup_arm_pcrel_10, |
| 233 | // but with 16-bit halfwords swapped. |
| 234 | if (Kind == ARM::fixup_t2_ldst_pcrel_12) { |
| 235 | uint64_t swapped = (Value & 0xFFFF0000) >> 16; |
| 236 | swapped |= (Value & 0x0000FFFF) << 16; |
| 237 | return swapped; |
| 238 | } |
Jim Grosbach | 7e294cf | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 239 | |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 240 | return Value; |
| 241 | } |
Jim Grosbach | d40963c | 2010-12-14 22:28:03 +0000 | [diff] [blame] | 242 | case ARM::fixup_thumb_adr_pcrel_10: |
| 243 | return ((Value - 4) >> 2) & 0xff; |
Jim Grosbach | dff84b0 | 2010-12-02 00:28:45 +0000 | [diff] [blame] | 244 | case ARM::fixup_arm_adr_pcrel_12: { |
| 245 | // ARM PC-relative values are offset by 8. |
| 246 | Value -= 8; |
| 247 | unsigned opc = 4; // bits {24-21}. Default to add: 0b0100 |
| 248 | if ((int64_t)Value < 0) { |
| 249 | Value = -Value; |
| 250 | opc = 2; // 0b0010 |
| 251 | } |
| 252 | assert(ARM_AM::getSOImmVal(Value) != -1 && |
| 253 | "Out of range pc-relative fixup value!"); |
| 254 | // Encode the immediate and shift the opcode into place. |
| 255 | return ARM_AM::getSOImmVal(Value) | (opc << 21); |
| 256 | } |
Jim Grosbach | e8eb1ea | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 257 | |
Owen Anderson | a838a25 | 2010-12-14 00:36:49 +0000 | [diff] [blame] | 258 | case ARM::fixup_t2_adr_pcrel_12: { |
| 259 | Value -= 4; |
| 260 | unsigned opc = 0; |
| 261 | if ((int64_t)Value < 0) { |
| 262 | Value = -Value; |
| 263 | opc = 5; |
| 264 | } |
| 265 | |
| 266 | uint32_t out = (opc << 21); |
Owen Anderson | 741ad15 | 2011-03-23 22:03:44 +0000 | [diff] [blame] | 267 | out |= (Value & 0x800) << 15; |
Owen Anderson | a838a25 | 2010-12-14 00:36:49 +0000 | [diff] [blame] | 268 | out |= (Value & 0x700) << 4; |
| 269 | out |= (Value & 0x0FF); |
Jim Grosbach | e8eb1ea | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 270 | |
Owen Anderson | a838a25 | 2010-12-14 00:36:49 +0000 | [diff] [blame] | 271 | uint64_t swapped = (out & 0xFFFF0000) >> 16; |
| 272 | swapped |= (out & 0x0000FFFF) << 16; |
| 273 | return swapped; |
| 274 | } |
Jim Grosbach | e8eb1ea | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 275 | |
Jason W Kim | 685c350 | 2011-02-04 19:47:15 +0000 | [diff] [blame] | 276 | case ARM::fixup_arm_condbranch: |
| 277 | case ARM::fixup_arm_uncondbranch: |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 278 | // These values don't encode the low two bits since they're always zero. |
| 279 | // Offset by 8 just as above. |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 280 | return 0xffffff & ((Value - 8) >> 2); |
Owen Anderson | c266600 | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 281 | case ARM::fixup_t2_uncondbranch: { |
Owen Anderson | 63ee220 | 2010-12-10 23:02:28 +0000 | [diff] [blame] | 282 | Value = Value - 4; |
Owen Anderson | fb20d89 | 2010-12-09 00:27:41 +0000 | [diff] [blame] | 283 | Value >>= 1; // Low bit is not encoded. |
Jim Grosbach | 7e294cf | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 284 | |
Jim Grosbach | 56a2535 | 2010-12-13 19:25:46 +0000 | [diff] [blame] | 285 | uint32_t out = 0; |
Owen Anderson | c266600 | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 286 | bool I = Value & 0x800000; |
| 287 | bool J1 = Value & 0x400000; |
| 288 | bool J2 = Value & 0x200000; |
| 289 | J1 ^= I; |
| 290 | J2 ^= I; |
Jim Grosbach | e8eb1ea | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 291 | |
Owen Anderson | c266600 | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 292 | out |= I << 26; // S bit |
| 293 | out |= !J1 << 13; // J1 bit |
| 294 | out |= !J2 << 11; // J2 bit |
| 295 | out |= (Value & 0x1FF800) << 5; // imm6 field |
| 296 | out |= (Value & 0x0007FF); // imm11 field |
Jim Grosbach | e8eb1ea | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 297 | |
Owen Anderson | c266600 | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 298 | uint64_t swapped = (out & 0xFFFF0000) >> 16; |
| 299 | swapped |= (out & 0x0000FFFF) << 16; |
| 300 | return swapped; |
| 301 | } |
| 302 | case ARM::fixup_t2_condbranch: { |
| 303 | Value = Value - 4; |
| 304 | Value >>= 1; // Low bit is not encoded. |
Jim Grosbach | e8eb1ea | 2010-12-14 16:25:15 +0000 | [diff] [blame] | 305 | |
Owen Anderson | c266600 | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 306 | uint64_t out = 0; |
Owen Anderson | 8f07943 | 2010-12-09 01:02:09 +0000 | [diff] [blame] | 307 | out |= (Value & 0x80000) << 7; // S bit |
| 308 | out |= (Value & 0x40000) >> 7; // J2 bit |
| 309 | out |= (Value & 0x20000) >> 4; // J1 bit |
| 310 | out |= (Value & 0x1F800) << 5; // imm6 field |
| 311 | out |= (Value & 0x007FF); // imm11 field |
Jim Grosbach | 7e294cf | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 312 | |
Jim Grosbach | 56a2535 | 2010-12-13 19:25:46 +0000 | [diff] [blame] | 313 | uint32_t swapped = (out & 0xFFFF0000) >> 16; |
Owen Anderson | fb20d89 | 2010-12-09 00:27:41 +0000 | [diff] [blame] | 314 | swapped |= (out & 0x0000FFFF) << 16; |
| 315 | return swapped; |
| 316 | } |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 317 | case ARM::fixup_arm_thumb_bl: { |
| 318 | // The value doesn't encode the low bit (always zero) and is offset by |
| 319 | // four. The value is encoded into disjoint bit positions in the destination |
| 320 | // opcode. x = unchanged, I = immediate value bit, S = sign extension bit |
Jim Grosbach | 7e294cf | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 321 | // |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 322 | // BL: xxxxxSIIIIIIIIII xxxxxIIIIIIIIIII |
Jim Grosbach | 7e294cf | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 323 | // |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 324 | // Note that the halfwords are stored high first, low second; so we need |
| 325 | // to transpose the fixup value here to map properly. |
Rafael Espindola | 298c8e1 | 2011-05-20 20:01:01 +0000 | [diff] [blame] | 326 | unsigned isNeg = (int64_t(Value - 4) < 0) ? 1 : 0; |
Bill Wendling | 797b7aa | 2010-12-09 00:44:33 +0000 | [diff] [blame] | 327 | uint32_t Binary = 0; |
| 328 | Value = 0x3fffff & ((Value - 4) >> 1); |
| 329 | Binary = (Value & 0x7ff) << 16; // Low imm11 value. |
| 330 | Binary |= (Value & 0x1ffc00) >> 11; // High imm10 value. |
| 331 | Binary |= isNeg << 10; // Sign bit. |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 332 | return Binary; |
| 333 | } |
| 334 | case ARM::fixup_arm_thumb_blx: { |
| 335 | // The value doesn't encode the low two bits (always zero) and is offset by |
| 336 | // four (see fixup_arm_thumb_cp). The value is encoded into disjoint bit |
| 337 | // positions in the destination opcode. x = unchanged, I = immediate value |
| 338 | // bit, S = sign extension bit, 0 = zero. |
Jim Grosbach | 7e294cf | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 339 | // |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 340 | // BLX: xxxxxSIIIIIIIIII xxxxxIIIIIIIIII0 |
Jim Grosbach | 7e294cf | 2010-12-13 19:18:13 +0000 | [diff] [blame] | 341 | // |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 342 | // Note that the halfwords are stored high first, low second; so we need |
| 343 | // to transpose the fixup value here to map properly. |
Rafael Espindola | 298c8e1 | 2011-05-20 20:01:01 +0000 | [diff] [blame] | 344 | unsigned isNeg = (int64_t(Value-4) < 0) ? 1 : 0; |
Bill Wendling | 797b7aa | 2010-12-09 00:44:33 +0000 | [diff] [blame] | 345 | uint32_t Binary = 0; |
| 346 | Value = 0xfffff & ((Value - 2) >> 2); |
| 347 | Binary = (Value & 0x3ff) << 17; // Low imm10L value. |
| 348 | Binary |= (Value & 0xffc00) >> 10; // High imm10H value. |
| 349 | Binary |= isNeg << 10; // Sign bit. |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 350 | return Binary; |
| 351 | } |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 352 | case ARM::fixup_arm_thumb_cp: |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 353 | // Offset by 4, and don't encode the low two bits. Two bytes of that |
| 354 | // 'off by 4' is implicitly handled by the half-word ordering of the |
| 355 | // Thumb encoding, so we only need to adjust by 2 here. |
| 356 | return ((Value - 2) >> 2) & 0xff; |
Jim Grosbach | b492a7c | 2010-12-09 19:50:12 +0000 | [diff] [blame] | 357 | case ARM::fixup_arm_thumb_cb: { |
Bill Wendling | dff2f71 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 358 | // Offset by 4 and don't encode the lower bit, which is always 0. |
| 359 | uint32_t Binary = (Value - 4) >> 1; |
Owen Anderson | 86abd48 | 2010-12-14 19:42:53 +0000 | [diff] [blame] | 360 | return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3); |
Bill Wendling | dff2f71 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 361 | } |
Jim Grosbach | e246717 | 2010-12-10 18:21:33 +0000 | [diff] [blame] | 362 | case ARM::fixup_arm_thumb_br: |
| 363 | // Offset by 4 and don't encode the lower bit, which is always 0. |
| 364 | return ((Value - 4) >> 1) & 0x7ff; |
Jim Grosbach | 0108645 | 2010-12-10 17:13:40 +0000 | [diff] [blame] | 365 | case ARM::fixup_arm_thumb_bcc: |
| 366 | // Offset by 4 and don't encode the lower bit, which is always 0. |
| 367 | return ((Value - 4) >> 1) & 0xff; |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 368 | case ARM::fixup_arm_pcrel_10: |
Owen Anderson | e2e0f58 | 2010-12-10 22:46:47 +0000 | [diff] [blame] | 369 | 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] | 370 | // need to adjust for the half-word ordering. |
| 371 | // Fall through. |
| 372 | case ARM::fixup_t2_pcrel_10: { |
| 373 | // 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] | 374 | Value = Value - 4; |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 375 | bool isAdd = true; |
| 376 | if ((int64_t)Value < 0) { |
| 377 | Value = -Value; |
| 378 | isAdd = false; |
| 379 | } |
| 380 | // These values don't encode the low two bits since they're always zero. |
| 381 | Value >>= 2; |
| 382 | assert ((Value < 256) && "Out of range pc-relative fixup value!"); |
| 383 | Value |= isAdd << 23; |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 384 | |
Owen Anderson | cc78f5c | 2010-12-08 19:31:11 +0000 | [diff] [blame] | 385 | // Same addressing mode as fixup_arm_pcrel_10, |
| 386 | // but with 16-bit halfwords swapped. |
Owen Anderson | d8e351b | 2010-12-08 00:18:36 +0000 | [diff] [blame] | 387 | if (Kind == ARM::fixup_t2_pcrel_10) { |
Jim Grosbach | 56a2535 | 2010-12-13 19:25:46 +0000 | [diff] [blame] | 388 | uint32_t swapped = (Value & 0xFFFF0000) >> 16; |
Owen Anderson | 255eafb | 2010-12-08 00:21:33 +0000 | [diff] [blame] | 389 | swapped |= (Value & 0x0000FFFF) << 16; |
Owen Anderson | d8e351b | 2010-12-08 00:18:36 +0000 | [diff] [blame] | 390 | return swapped; |
| 391 | } |
Jim Grosbach | 0c2c217 | 2010-12-08 20:32:07 +0000 | [diff] [blame] | 392 | |
Jason W Kim | 0c628c2 | 2010-12-01 22:46:50 +0000 | [diff] [blame] | 393 | return Value; |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 398 | namespace { |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 399 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 400 | // FIXME: This should be in a separate file. |
| 401 | // ELF is an ELF of course... |
| 402 | class ELFARMAsmBackend : public ARMAsmBackend { |
| 403 | public: |
| 404 | Triple::OSType OSType; |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 405 | ELFARMAsmBackend(const Target &T, const StringRef TT, |
| 406 | Triple::OSType _OSType) |
| 407 | : ARMAsmBackend(T, TT), OSType(_OSType) { } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 408 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 409 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 410 | uint64_t Value) const; |
| 411 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 412 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
Rafael Espindola | bff66a8 | 2010-12-18 03:27:34 +0000 | [diff] [blame] | 413 | return createELFObjectWriter(new ARMELFObjectWriter(OSType), OS, |
| 414 | /*IsLittleEndian*/ true); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 415 | } |
| 416 | }; |
| 417 | |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 418 | // FIXME: Raise this to share code between Darwin and ELF. |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 419 | void ELFARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data, |
| 420 | unsigned DataSize, uint64_t Value) const { |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 421 | unsigned NumBytes = 4; // FIXME: 2 for Thumb |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 422 | Value = adjustFixupValue(Fixup.getKind(), Value); |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 423 | if (!Value) return; // Doesn't change encoding. |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 424 | |
| 425 | unsigned Offset = Fixup.getOffset(); |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 426 | |
| 427 | // For each byte of the fragment that the fixup touches, mask in the bits from |
| 428 | // the fixup value. The Value has been "split up" into the appropriate |
| 429 | // bitfields above. |
| 430 | for (unsigned i = 0; i != NumBytes; ++i) |
| 431 | Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | // FIXME: This should be in a separate file. |
| 435 | class DarwinARMAsmBackend : public ARMAsmBackend { |
| 436 | public: |
Owen Anderson | 1721324 | 2011-04-01 21:07:39 +0000 | [diff] [blame] | 437 | const object::mach::CPUSubtypeARM Subtype; |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 438 | DarwinARMAsmBackend(const Target &T, const StringRef TT, |
| 439 | object::mach::CPUSubtypeARM st) |
| 440 | : ARMAsmBackend(T, TT), Subtype(st) { } |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 441 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 442 | MCObjectWriter *createObjectWriter(raw_ostream &OS) const { |
Jim Grosbach | 2fc6898 | 2011-06-22 20:14:52 +0000 | [diff] [blame] | 443 | return createARMMachObjectWriter(OS, /*Is64Bit=*/false, |
| 444 | object::mach::CTM_ARM, |
| 445 | Subtype); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 446 | } |
| 447 | |
Owen Anderson | 1721324 | 2011-04-01 21:07:39 +0000 | [diff] [blame] | 448 | void ApplyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize, |
| 449 | uint64_t Value) const; |
| 450 | |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 451 | virtual bool doesSectionRequireSymbols(const MCSection &Section) const { |
| 452 | return false; |
| 453 | } |
| 454 | }; |
| 455 | |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 456 | /// getFixupKindNumBytes - The number of bytes the fixup may change. |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 457 | static unsigned getFixupKindNumBytes(unsigned Kind) { |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 458 | switch (Kind) { |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 459 | default: |
| 460 | llvm_unreachable("Unknown fixup kind!"); |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 461 | |
Jim Grosbach | 6ec6eeb | 2010-12-17 18:39:10 +0000 | [diff] [blame] | 462 | case FK_Data_1: |
Jim Grosbach | 0108645 | 2010-12-10 17:13:40 +0000 | [diff] [blame] | 463 | case ARM::fixup_arm_thumb_bcc: |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 464 | case ARM::fixup_arm_thumb_cp: |
Jim Grosbach | d40963c | 2010-12-14 22:28:03 +0000 | [diff] [blame] | 465 | case ARM::fixup_thumb_adr_pcrel_10: |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 466 | return 1; |
| 467 | |
Jim Grosbach | 6ec6eeb | 2010-12-17 18:39:10 +0000 | [diff] [blame] | 468 | case FK_Data_2: |
Jim Grosbach | e246717 | 2010-12-10 18:21:33 +0000 | [diff] [blame] | 469 | case ARM::fixup_arm_thumb_br: |
Jim Grosbach | b492a7c | 2010-12-09 19:50:12 +0000 | [diff] [blame] | 470 | case ARM::fixup_arm_thumb_cb: |
Bill Wendling | dff2f71 | 2010-12-08 23:01:43 +0000 | [diff] [blame] | 471 | return 2; |
| 472 | |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 473 | case ARM::fixup_arm_ldst_pcrel_12: |
| 474 | case ARM::fixup_arm_pcrel_10: |
| 475 | case ARM::fixup_arm_adr_pcrel_12: |
Jason W Kim | 685c350 | 2011-02-04 19:47:15 +0000 | [diff] [blame] | 476 | case ARM::fixup_arm_condbranch: |
| 477 | case ARM::fixup_arm_uncondbranch: |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 478 | return 3; |
Bill Wendling | b8958b0 | 2010-12-08 01:57:09 +0000 | [diff] [blame] | 479 | |
| 480 | case FK_Data_4: |
Owen Anderson | d7b3f58 | 2010-12-09 01:51:07 +0000 | [diff] [blame] | 481 | case ARM::fixup_t2_ldst_pcrel_12: |
Owen Anderson | c266600 | 2010-12-13 19:31:11 +0000 | [diff] [blame] | 482 | case ARM::fixup_t2_condbranch: |
| 483 | case ARM::fixup_t2_uncondbranch: |
Owen Anderson | d8e351b | 2010-12-08 00:18:36 +0000 | [diff] [blame] | 484 | case ARM::fixup_t2_pcrel_10: |
Owen Anderson | a838a25 | 2010-12-14 00:36:49 +0000 | [diff] [blame] | 485 | case ARM::fixup_t2_adr_pcrel_12: |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 486 | case ARM::fixup_arm_thumb_bl: |
Bill Wendling | 09aa3f0 | 2010-12-09 00:39:08 +0000 | [diff] [blame] | 487 | case ARM::fixup_arm_thumb_blx: |
Evan Cheng | f3eb3bb | 2011-01-14 02:38:49 +0000 | [diff] [blame] | 488 | case ARM::fixup_arm_movt_hi16: |
| 489 | case ARM::fixup_arm_movw_lo16: |
| 490 | case ARM::fixup_arm_movt_hi16_pcrel: |
| 491 | case ARM::fixup_arm_movw_lo16_pcrel: |
| 492 | case ARM::fixup_t2_movt_hi16: |
| 493 | case ARM::fixup_t2_movw_lo16: |
| 494 | case ARM::fixup_t2_movt_hi16_pcrel: |
| 495 | case ARM::fixup_t2_movw_lo16_pcrel: |
Jim Grosbach | 662a816 | 2010-12-06 23:57:07 +0000 | [diff] [blame] | 496 | return 4; |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 497 | } |
| 498 | } |
| 499 | |
Rafael Espindola | 179821a | 2010-12-06 19:08:48 +0000 | [diff] [blame] | 500 | void DarwinARMAsmBackend::ApplyFixup(const MCFixup &Fixup, char *Data, |
| 501 | unsigned DataSize, uint64_t Value) const { |
Jim Grosbach | c466b93 | 2010-11-11 18:04:49 +0000 | [diff] [blame] | 502 | unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind()); |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 503 | Value = adjustFixupValue(Fixup.getKind(), Value); |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 504 | if (!Value) return; // Doesn't change encoding. |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 505 | |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 506 | unsigned Offset = Fixup.getOffset(); |
| 507 | assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!"); |
| 508 | |
Jim Grosbach | 679cbd3 | 2010-11-09 01:37:15 +0000 | [diff] [blame] | 509 | // For each byte of the fragment that the fixup touches, mask in the |
| 510 | // bits from the fixup value. |
| 511 | for (unsigned i = 0; i != NumBytes; ++i) |
Bill Wendling | d832fa0 | 2010-12-07 23:11:00 +0000 | [diff] [blame] | 512 | Data[Offset + i] |= uint8_t((Value >> (i * 8)) & 0xff); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 513 | } |
Bill Wendling | 52e635e | 2010-12-07 23:05:20 +0000 | [diff] [blame] | 514 | |
Jim Grosbach | f73fd72 | 2010-09-30 03:21:00 +0000 | [diff] [blame] | 515 | } // end anonymous namespace |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 516 | |
Evan Cheng | 78c10ee | 2011-07-25 23:24:55 +0000 | [diff] [blame] | 517 | MCAsmBackend *llvm::createARMAsmBackend(const Target &T, StringRef TT) { |
Owen Anderson | 1721324 | 2011-04-01 21:07:39 +0000 | [diff] [blame] | 518 | Triple TheTriple(TT); |
Daniel Dunbar | 912225e | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 519 | |
| 520 | if (TheTriple.isOSDarwin()) { |
Evan Cheng | a6eb256 | 2011-06-14 18:08:33 +0000 | [diff] [blame] | 521 | if (TheTriple.getArchName() == "armv4t" || |
| 522 | TheTriple.getArchName() == "thumbv4t") |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 523 | return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V4T); |
Evan Cheng | a6eb256 | 2011-06-14 18:08:33 +0000 | [diff] [blame] | 524 | else if (TheTriple.getArchName() == "armv5e" || |
| 525 | TheTriple.getArchName() == "thumbv5e") |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 526 | return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V5TEJ); |
Evan Cheng | a6eb256 | 2011-06-14 18:08:33 +0000 | [diff] [blame] | 527 | else if (TheTriple.getArchName() == "armv6" || |
Owen Anderson | 1721324 | 2011-04-01 21:07:39 +0000 | [diff] [blame] | 528 | TheTriple.getArchName() == "thumbv6") |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 529 | return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V6); |
| 530 | return new DarwinARMAsmBackend(T, TT, object::mach::CSARM_V7); |
Owen Anderson | 1721324 | 2011-04-01 21:07:39 +0000 | [diff] [blame] | 531 | } |
Daniel Dunbar | 912225e | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 532 | |
| 533 | if (TheTriple.isOSWindows()) |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 534 | assert(0 && "Windows not supported on ARM"); |
Daniel Dunbar | 912225e | 2011-04-19 21:14:45 +0000 | [diff] [blame] | 535 | |
Jim Grosbach | d0d3f7e | 2011-08-16 17:06:20 +0000 | [diff] [blame] | 536 | return new ELFARMAsmBackend(T, TT, Triple(TT).getOS()); |
Jason W Kim | d4d4f4f | 2010-09-30 02:17:26 +0000 | [diff] [blame] | 537 | } |