blob: 0b2e3b0e67bbd411b9df2dc6c87abd16fbe9faf3 [file] [log] [blame]
Jason W Kimb3212452010-09-30 02:17:26 +00001//===-- 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 Grosbach45e50d82011-08-16 17:06:20 +000010#include "MCTargetDesc/ARMMCTargetDesc.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000011#include "MCTargetDesc/ARMAddressingModes.h"
Joe Abbey8e72eb72014-09-16 09:18:23 +000012#include "MCTargetDesc/ARMAsmBackend.h"
13#include "MCTargetDesc/ARMAsmBackendDarwin.h"
14#include "MCTargetDesc/ARMAsmBackendELF.h"
15#include "MCTargetDesc/ARMAsmBackendWinCOFF.h"
Evan Chengad5f4852011-07-23 00:00:19 +000016#include "MCTargetDesc/ARMBaseInfo.h"
17#include "MCTargetDesc/ARMFixupKinds.h"
Quentin Colombet77ca8b82013-01-14 21:34:09 +000018#include "llvm/ADT/StringSwitch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000019#include "llvm/MC/MCAsmBackend.h"
Jason W Kimb3212452010-09-30 02:17:26 +000020#include "llvm/MC/MCAssembler.h"
Jim Grosbache78031a2012-04-30 22:30:43 +000021#include "llvm/MC/MCContext.h"
Jim Grosbach87055ed2010-12-08 01:16:55 +000022#include "llvm/MC/MCDirectives.h"
Rafael Espindolaf0e24d42010-12-17 16:59:53 +000023#include "llvm/MC/MCELFObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000024#include "llvm/MC/MCExpr.h"
Craig Topper6e80c282012-03-26 06:58:25 +000025#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar73b87132010-12-16 16:08:33 +000026#include "llvm/MC/MCMachObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000027#include "llvm/MC/MCObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000028#include "llvm/MC/MCSectionELF.h"
29#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach45e50d82011-08-16 17:06:20 +000030#include "llvm/MC/MCSubtargetInfo.h"
Jim Grosbach3b50c9e2012-01-18 00:23:57 +000031#include "llvm/MC/MCValue.h"
Wesley Peck18510902010-10-22 15:52:49 +000032#include "llvm/Support/ELF.h"
Jason W Kimb3212452010-09-30 02:17:26 +000033#include "llvm/Support/ErrorHandling.h"
Charles Davis8bdfafd2013-09-01 04:28:48 +000034#include "llvm/Support/MachO.h"
Jason W Kimb3212452010-09-30 02:17:26 +000035#include "llvm/Support/raw_ostream.h"
Jason W Kimb3212452010-09-30 02:17:26 +000036using namespace llvm;
37
38namespace {
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000039class ARMELFObjectWriter : public MCELFObjectTargetWriter {
40public:
Rafael Espindola1ad40952011-12-21 17:00:36 +000041 ARMELFObjectWriter(uint8_t OSABI)
Joe Abbey8e72eb72014-09-16 09:18:23 +000042 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
43 /*HasRelocationAddend*/ false) {}
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000044};
45
Joe Abbey8e72eb72014-09-16 09:18:23 +000046const MCFixupKindInfo &ARMAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
47 const static MCFixupKindInfo InfosLE[ARM::NumTargetFixupKinds] = {
48 // This table *must* be in the order that the fixup_* kinds are defined in
49 // ARMFixupKinds.h.
50 //
51 // Name Offset (bits) Size (bits) Flags
52 {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
53 {"fixup_t2_ldst_pcrel_12", 0, 32,
54 MCFixupKindInfo::FKF_IsPCRel |
55 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
56 {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
57 {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
58 {"fixup_t2_pcrel_10", 0, 32,
59 MCFixupKindInfo::FKF_IsPCRel |
60 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
61 {"fixup_thumb_adr_pcrel_10", 0, 8,
62 MCFixupKindInfo::FKF_IsPCRel |
63 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
64 {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
65 {"fixup_t2_adr_pcrel_12", 0, 32,
66 MCFixupKindInfo::FKF_IsPCRel |
67 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
68 {"fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
69 {"fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
70 {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
71 {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
72 {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
73 {"fixup_arm_uncondbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
74 {"fixup_arm_condbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
75 {"fixup_arm_blx", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
76 {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
77 {"fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
78 {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
79 {"fixup_arm_thumb_cp", 0, 8,
80 MCFixupKindInfo::FKF_IsPCRel |
81 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
82 {"fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
83 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16
84 // - 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 };
90 const static MCFixupKindInfo InfosBE[ARM::NumTargetFixupKinds] = {
91 // This table *must* be in the order that the fixup_* kinds are defined in
92 // ARMFixupKinds.h.
93 //
94 // Name Offset (bits) Size (bits) Flags
95 {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
96 {"fixup_t2_ldst_pcrel_12", 0, 32,
97 MCFixupKindInfo::FKF_IsPCRel |
98 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
99 {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
100 {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
101 {"fixup_t2_pcrel_10", 0, 32,
102 MCFixupKindInfo::FKF_IsPCRel |
103 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
104 {"fixup_thumb_adr_pcrel_10", 8, 8,
105 MCFixupKindInfo::FKF_IsPCRel |
106 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
107 {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
108 {"fixup_t2_adr_pcrel_12", 0, 32,
109 MCFixupKindInfo::FKF_IsPCRel |
110 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
111 {"fixup_arm_condbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
112 {"fixup_arm_uncondbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
113 {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
114 {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
115 {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
116 {"fixup_arm_uncondbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
117 {"fixup_arm_condbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
118 {"fixup_arm_blx", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
119 {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
120 {"fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
121 {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
122 {"fixup_arm_thumb_cp", 8, 8,
123 MCFixupKindInfo::FKF_IsPCRel |
124 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
125 {"fixup_arm_thumb_bcc", 8, 8, MCFixupKindInfo::FKF_IsPCRel},
126 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16
127 // - 19.
128 {"fixup_arm_movt_hi16", 12, 20, 0},
129 {"fixup_arm_movw_lo16", 12, 20, 0},
130 {"fixup_t2_movt_hi16", 12, 20, 0},
131 {"fixup_t2_movw_lo16", 12, 20, 0},
132 };
Jim Grosbach45e50d82011-08-16 17:06:20 +0000133
Joe Abbey8e72eb72014-09-16 09:18:23 +0000134 if (Kind < FirstTargetFixupKind)
135 return MCAsmBackend::getFixupKindInfo(Kind);
136
137 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
138 "Invalid kind!");
139 return (IsLittleEndian ? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
140}
141
142void ARMAsmBackend::handleAssemblerFlag(MCAssemblerFlag Flag) {
143 switch (Flag) {
144 default:
145 break;
146 case MCAF_Code16:
147 setIsThumb(true);
148 break;
149 case MCAF_Code32:
150 setIsThumb(false);
151 break;
Jim Grosbach45e50d82011-08-16 17:06:20 +0000152 }
Joe Abbey8e72eb72014-09-16 09:18:23 +0000153}
Chris Lattner9fdd10d2010-11-17 05:41:32 +0000154} // end anonymous namespace
Jason W Kimb3212452010-09-30 02:17:26 +0000155
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000156static unsigned getRelaxedOpcode(unsigned Op) {
157 switch (Op) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000158 default:
159 return Op;
160 case ARM::tBcc:
161 return ARM::t2Bcc;
162 case ARM::tLDRpci:
163 return ARM::t2LDRpci;
164 case ARM::tADR:
165 return ARM::t2ADR;
166 case ARM::tB:
167 return ARM::t2B;
168 case ARM::tCBZ:
169 return ARM::tHINT;
170 case ARM::tCBNZ:
171 return ARM::tHINT;
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000172 }
173}
174
Jim Grosbachaba3de92012-01-18 18:52:16 +0000175bool ARMAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000176 if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
177 return true;
Jason W Kimb3212452010-09-30 02:17:26 +0000178 return false;
179}
180
Joe Abbey8e72eb72014-09-16 09:18:23 +0000181bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000182 const MCRelaxableFragment *DF,
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000183 const MCAsmLayout &Layout) const {
Benjamin Kramer116e99a2012-01-19 21:11:13 +0000184 switch ((unsigned)Fixup.getKind()) {
Jim Grosbachc4aa60f2012-03-19 21:32:32 +0000185 case ARM::fixup_arm_thumb_br: {
186 // Relaxing tB to t2B. tB has a signed 12-bit displacement with the
187 // low bit being an implied zero. There's an implied +4 offset for the
188 // branch, so we adjust the other way here to determine what's
189 // encodable.
190 //
191 // Relax if the value is too big for a (signed) i8.
192 int64_t Offset = int64_t(Value) - 4;
193 return Offset > 2046 || Offset < -2048;
194 }
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000195 case ARM::fixup_arm_thumb_bcc: {
196 // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the
197 // low bit being an implied zero. There's an implied +4 offset for the
198 // branch, so we adjust the other way here to determine what's
199 // encodable.
200 //
201 // Relax if the value is too big for a (signed) i8.
202 int64_t Offset = int64_t(Value) - 4;
203 return Offset > 254 || Offset < -256;
204 }
Jim Grosbach44e5c392012-01-19 02:09:38 +0000205 case ARM::fixup_thumb_adr_pcrel_10:
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000206 case ARM::fixup_arm_thumb_cp: {
Jim Grosbachb008df42012-01-19 01:50:30 +0000207 // If the immediate is negative, greater than 1020, or not a multiple
208 // of four, the wide version of the instruction must be used.
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000209 int64_t Offset = int64_t(Value) - 4;
Jim Grosbachb008df42012-01-19 01:50:30 +0000210 return Offset > 1020 || Offset < 0 || Offset & 3;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000211 }
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000212 case ARM::fixup_arm_thumb_cb:
213 // If we have a Thumb CBZ or CBNZ instruction and its target is the next
214 // instruction it is is actually out of range for the instruction.
215 // It will be changed to a NOP.
216 int64_t Offset = (Value & ~1);
217 return Offset == 2;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000218 }
Benjamin Kramer116e99a2012-01-19 21:11:13 +0000219 llvm_unreachable("Unexpected fixup kind in fixupNeedsRelaxation()!");
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000220}
221
Jim Grosbachaba3de92012-01-18 18:52:16 +0000222void ARMAsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000223 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
224
225 // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
226 if (RelaxedOp == Inst.getOpcode()) {
227 SmallString<256> Tmp;
228 raw_svector_ostream OS(Tmp);
229 Inst.dump_pretty(OS);
230 OS << "\n";
231 report_fatal_error("unexpected instruction to relax: " + OS.str());
232 }
233
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000234 // If we are changing Thumb CBZ or CBNZ instruction to a NOP, aka tHINT, we
235 // have to change the operands too.
236 if ((Inst.getOpcode() == ARM::tCBZ || Inst.getOpcode() == ARM::tCBNZ) &&
237 RelaxedOp == ARM::tHINT) {
238 Res.setOpcode(RelaxedOp);
239 Res.addOperand(MCOperand::CreateImm(0));
240 Res.addOperand(MCOperand::CreateImm(14));
241 Res.addOperand(MCOperand::CreateReg(0));
242 return;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000243 }
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000244
245 // The rest of instructions we're relaxing have the same operands.
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000246 // We just need to update to the proper opcode.
247 Res = Inst;
248 Res.setOpcode(RelaxedOp);
Jason W Kimb3212452010-09-30 02:17:26 +0000249}
250
Jim Grosbachaba3de92012-01-18 18:52:16 +0000251bool ARMAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach45e50d82011-08-16 17:06:20 +0000252 const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
253 const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
Joe Abbey8e72eb72014-09-16 09:18:23 +0000254 const uint32_t ARMv4_NopEncoding = 0xe1a00000; // using MOV r0,r0
Jim Grosbach7ccdb7c2011-11-16 22:40:25 +0000255 const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
Jim Grosbach87055ed2010-12-08 01:16:55 +0000256 if (isThumb()) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000257 const uint16_t nopEncoding =
258 hasNOP() ? Thumb2_16bitNopEncoding : Thumb1_16bitNopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000259 uint64_t NumNops = Count / 2;
260 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach45e50d82011-08-16 17:06:20 +0000261 OW->Write16(nopEncoding);
Jim Grosbach97f1de72010-12-17 19:03:02 +0000262 if (Count & 1)
263 OW->Write8(0);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000264 return true;
265 }
266 // ARM mode
Joe Abbey8e72eb72014-09-16 09:18:23 +0000267 const uint32_t nopEncoding =
268 hasNOP() ? ARMv6T2_NopEncoding : ARMv4_NopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000269 uint64_t NumNops = Count / 4;
270 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach45e50d82011-08-16 17:06:20 +0000271 OW->Write32(nopEncoding);
272 // FIXME: should this function return false when unable to write exactly
273 // 'Count' bytes with NOP encodings?
Jim Grosbach97f1de72010-12-17 19:03:02 +0000274 switch (Count % 4) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000275 default:
276 break; // No leftover bytes to write
277 case 1:
278 OW->Write8(0);
279 break;
280 case 2:
281 OW->Write16(0);
282 break;
283 case 3:
284 OW->Write16(0);
285 OW->Write8(0xa0);
286 break;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000287 }
288
Rafael Espindola0ed15432010-10-25 17:50:35 +0000289 return true;
Jim Grosbach58bce992010-09-30 03:20:34 +0000290}
Jason W Kimb3212452010-09-30 02:17:26 +0000291
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000292static uint32_t swapHalfWords(uint32_t Value, bool IsLittleEndian) {
293 if (IsLittleEndian) {
294 // Note that the halfwords are stored high first and low second in thumb;
295 // so we need to swap the fixup value here to map properly.
296 uint32_t Swapped = (Value & 0xFFFF0000) >> 16;
297 Swapped |= (Value & 0x0000FFFF) << 16;
298 return Swapped;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000299 } else
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000300 return Value;
301}
302
303static uint32_t joinHalfWords(uint32_t FirstHalf, uint32_t SecondHalf,
304 bool IsLittleEndian) {
305 uint32_t Value;
306
307 if (IsLittleEndian) {
308 Value = (SecondHalf & 0xFFFF) << 16;
309 Value |= (FirstHalf & 0xFFFF);
310 } else {
311 Value = (SecondHalf & 0xFFFF);
312 Value |= (FirstHalf & 0xFFFF) << 16;
313 }
314
315 return Value;
316}
317
Jim Grosbache78031a2012-04-30 22:30:43 +0000318static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000319 bool IsPCRel, MCContext *Ctx,
320 bool IsLittleEndian) {
Jim Grosbache78031a2012-04-30 22:30:43 +0000321 unsigned Kind = Fixup.getKind();
Jason W Kimfc5c5222010-12-01 22:46:50 +0000322 switch (Kind) {
323 default:
324 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000325 case FK_Data_1:
326 case FK_Data_2:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000327 case FK_Data_4:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000328 return Value;
Saleem Abdulrasoolfc6b85b2014-05-08 01:35:57 +0000329 case FK_SecRel_2:
330 return Value;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000331 case FK_SecRel_4:
332 return Value;
Jason W Kimd5e6e542010-12-03 19:40:23 +0000333 case ARM::fixup_arm_movt_hi16:
Rafael Espindola5904e122014-03-29 06:26:49 +0000334 if (!IsPCRel)
335 Value >>= 16;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000336 // Fallthrough
Rafael Espindola5904e122014-03-29 06:26:49 +0000337 case ARM::fixup_arm_movw_lo16: {
Jason W Kimd5e6e542010-12-03 19:40:23 +0000338 unsigned Hi4 = (Value & 0xF000) >> 12;
339 unsigned Lo12 = Value & 0x0FFF;
340 // inst{19-16} = Hi4;
341 // inst{11-0} = Lo12;
342 Value = (Hi4 << 16) | (Lo12);
343 return Value;
344 }
Evan Chengd4a5c052011-01-14 02:38:49 +0000345 case ARM::fixup_t2_movt_hi16:
Rafael Espindola5904e122014-03-29 06:26:49 +0000346 if (!IsPCRel)
347 Value >>= 16;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000348 // Fallthrough
Rafael Espindola5904e122014-03-29 06:26:49 +0000349 case ARM::fixup_t2_movw_lo16: {
Evan Chengd4a5c052011-01-14 02:38:49 +0000350 unsigned Hi4 = (Value & 0xF000) >> 12;
351 unsigned i = (Value & 0x800) >> 11;
352 unsigned Mid3 = (Value & 0x700) >> 8;
353 unsigned Lo8 = Value & 0x0FF;
354 // inst{19-16} = Hi4;
355 // inst{26} = i;
356 // inst{14-12} = Mid3;
357 // inst{7-0} = Lo8;
Jim Grosbachd76f43e2011-09-30 22:02:45 +0000358 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000359 return swapHalfWords(Value, IsLittleEndian);
Evan Chengd4a5c052011-01-14 02:38:49 +0000360 }
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000361 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000362 // ARM PC-relative values are offset by 8.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000363 Value -= 4;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000364 // FALLTHROUGH
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000365 case ARM::fixup_t2_ldst_pcrel_12: {
366 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000367 Value -= 4;
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000368 bool isAdd = true;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000369 if ((int64_t)Value < 0) {
370 Value = -Value;
371 isAdd = false;
372 }
Jim Grosbache78031a2012-04-30 22:30:43 +0000373 if (Ctx && Value >= 4096)
374 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
Jason W Kimfc5c5222010-12-01 22:46:50 +0000375 Value |= isAdd << 23;
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000376
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000377 // Same addressing mode as fixup_arm_pcrel_10,
378 // but with 16-bit halfwords swapped.
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000379 if (Kind == ARM::fixup_t2_ldst_pcrel_12)
380 return swapHalfWords(Value, IsLittleEndian);
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000381
Jason W Kimfc5c5222010-12-01 22:46:50 +0000382 return Value;
383 }
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000384 case ARM::fixup_thumb_adr_pcrel_10:
385 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000386 case ARM::fixup_arm_adr_pcrel_12: {
387 // ARM PC-relative values are offset by 8.
388 Value -= 8;
389 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
390 if ((int64_t)Value < 0) {
391 Value = -Value;
392 opc = 2; // 0b0010
393 }
Jim Grosbache78031a2012-04-30 22:30:43 +0000394 if (Ctx && ARM_AM::getSOImmVal(Value) == -1)
395 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000396 // Encode the immediate and shift the opcode into place.
397 return ARM_AM::getSOImmVal(Value) | (opc << 21);
398 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000399
Owen Anderson6d375e52010-12-14 00:36:49 +0000400 case ARM::fixup_t2_adr_pcrel_12: {
401 Value -= 4;
402 unsigned opc = 0;
403 if ((int64_t)Value < 0) {
404 Value = -Value;
405 opc = 5;
406 }
407
408 uint32_t out = (opc << 21);
Owen Anderson8543d4f2011-03-23 22:03:44 +0000409 out |= (Value & 0x800) << 15;
Owen Anderson6d375e52010-12-14 00:36:49 +0000410 out |= (Value & 0x700) << 4;
411 out |= (Value & 0x0FF);
Jim Grosbache34793e2010-12-14 16:25:15 +0000412
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000413 return swapHalfWords(out, IsLittleEndian);
Owen Anderson6d375e52010-12-14 00:36:49 +0000414 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000415
Jason W Kimd2e2f562011-02-04 19:47:15 +0000416 case ARM::fixup_arm_condbranch:
417 case ARM::fixup_arm_uncondbranch:
James Molloyfb5cd602012-03-30 09:15:32 +0000418 case ARM::fixup_arm_uncondbl:
419 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +0000420 case ARM::fixup_arm_blx:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000421 // These values don't encode the low two bits since they're always zero.
422 // Offset by 8 just as above.
Joe Abbey8e72eb72014-09-16 09:18:23 +0000423 if (const MCSymbolRefExpr *SRE =
424 dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000425 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL)
426 return 0;
Jim Grosbach9e199462010-12-06 23:57:07 +0000427 return 0xffffff & ((Value - 8) >> 2);
Owen Anderson578074b2010-12-13 19:31:11 +0000428 case ARM::fixup_t2_uncondbranch: {
Owen Anderson235c2762010-12-10 23:02:28 +0000429 Value = Value - 4;
Owen Anderson302d5fd2010-12-09 00:27:41 +0000430 Value >>= 1; // Low bit is not encoded.
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000431
Jim Grosbachf588c512010-12-13 19:25:46 +0000432 uint32_t out = 0;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000433 bool I = Value & 0x800000;
Owen Anderson578074b2010-12-13 19:31:11 +0000434 bool J1 = Value & 0x400000;
435 bool J2 = Value & 0x200000;
436 J1 ^= I;
437 J2 ^= I;
Jim Grosbache34793e2010-12-14 16:25:15 +0000438
Joe Abbey8e72eb72014-09-16 09:18:23 +0000439 out |= I << 26; // S bit
440 out |= !J1 << 13; // J1 bit
441 out |= !J2 << 11; // J2 bit
442 out |= (Value & 0x1FF800) << 5; // imm6 field
443 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache34793e2010-12-14 16:25:15 +0000444
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000445 return swapHalfWords(out, IsLittleEndian);
Owen Anderson578074b2010-12-13 19:31:11 +0000446 }
447 case ARM::fixup_t2_condbranch: {
448 Value = Value - 4;
449 Value >>= 1; // Low bit is not encoded.
Jim Grosbache34793e2010-12-14 16:25:15 +0000450
Owen Anderson578074b2010-12-13 19:31:11 +0000451 uint64_t out = 0;
Owen Anderson14e41272010-12-09 01:02:09 +0000452 out |= (Value & 0x80000) << 7; // S bit
453 out |= (Value & 0x40000) >> 7; // J2 bit
454 out |= (Value & 0x20000) >> 4; // J1 bit
455 out |= (Value & 0x1F800) << 5; // imm6 field
456 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000457
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000458 return swapHalfWords(out, IsLittleEndian);
Owen Anderson302d5fd2010-12-09 00:27:41 +0000459 }
Jim Grosbach9e199462010-12-06 23:57:07 +0000460 case ARM::fixup_arm_thumb_bl: {
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000461 // The value doesn't encode the low bit (always zero) and is offset by
462 // four. The 32-bit immediate value is encoded as
463 // imm32 = SignExtend(S:I1:I2:imm10:imm11:0)
464 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
465 // The value is encoded into disjoint bit positions in the destination
466 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
467 // J = either J1 or J2 bit
468 //
469 // BL: xxxxxSIIIIIIIIII xxJxJIIIIIIIIIII
470 //
471 // Note that the halfwords are stored high first, low second; so we need
472 // to transpose the fixup value here to map properly.
473 uint32_t offset = (Value - 4) >> 1;
474 uint32_t signBit = (offset & 0x800000) >> 23;
475 uint32_t I1Bit = (offset & 0x400000) >> 22;
476 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
477 uint32_t I2Bit = (offset & 0x200000) >> 21;
478 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
479 uint32_t imm10Bits = (offset & 0x1FF800) >> 11;
480 uint32_t imm11Bits = (offset & 0x000007FF);
NAKAMURA Takumi8018a292013-06-11 06:52:36 +0000481
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000482 uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10Bits);
483 uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
Joe Abbey8e72eb72014-09-16 09:18:23 +0000484 (uint16_t)imm11Bits);
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000485 return joinHalfWords(FirstHalf, SecondHalf, IsLittleEndian);
Bill Wendling3392bfc2010-12-09 00:39:08 +0000486 }
487 case ARM::fixup_arm_thumb_blx: {
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000488 // The value doesn't encode the low two bits (always zero) and is offset by
489 // four (see fixup_arm_thumb_cp). The 32-bit immediate value is encoded as
490 // imm32 = SignExtend(S:I1:I2:imm10H:imm10L:00)
491 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
492 // The value is encoded into disjoint bit positions in the destination
493 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
494 // J = either J1 or J2 bit, 0 = zero.
495 //
496 // BLX: xxxxxSIIIIIIIIII xxJxJIIIIIIIIII0
497 //
498 // Note that the halfwords are stored high first, low second; so we need
499 // to transpose the fixup value here to map properly.
500 uint32_t offset = (Value - 2) >> 2;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000501 if (const MCSymbolRefExpr *SRE =
502 dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000503 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL)
504 offset = 0;
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000505 uint32_t signBit = (offset & 0x400000) >> 22;
506 uint32_t I1Bit = (offset & 0x200000) >> 21;
507 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
508 uint32_t I2Bit = (offset & 0x100000) >> 20;
509 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
510 uint32_t imm10HBits = (offset & 0xFFC00) >> 10;
511 uint32_t imm10LBits = (offset & 0x3FF);
NAKAMURA Takumi8018a292013-06-11 06:52:36 +0000512
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000513 uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10HBits);
514 uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
Joe Abbey8e72eb72014-09-16 09:18:23 +0000515 ((uint16_t)imm10LBits) << 1);
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000516 return joinHalfWords(FirstHalf, SecondHalf, IsLittleEndian);
Jim Grosbach9e199462010-12-06 23:57:07 +0000517 }
Bill Wendling8a6449c2010-12-08 01:57:09 +0000518 case ARM::fixup_arm_thumb_cp:
Jim Grosbach3c685612010-12-08 20:32:07 +0000519 // Offset by 4, and don't encode the low two bits. Two bytes of that
520 // 'off by 4' is implicitly handled by the half-word ordering of the
521 // Thumb encoding, so we only need to adjust by 2 here.
522 return ((Value - 2) >> 2) & 0xff;
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000523 case ARM::fixup_arm_thumb_cb: {
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000524 // Offset by 4 and don't encode the lower bit, which is always 0.
525 uint32_t Binary = (Value - 4) >> 1;
Owen Andersonf636a642010-12-14 19:42:53 +0000526 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000527 }
Jim Grosbache119da12010-12-10 18:21:33 +0000528 case ARM::fixup_arm_thumb_br:
529 // Offset by 4 and don't encode the lower bit, which is always 0.
530 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach78485ad2010-12-10 17:13:40 +0000531 case ARM::fixup_arm_thumb_bcc:
532 // Offset by 4 and don't encode the lower bit, which is always 0.
533 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach8648c102011-12-19 23:06:24 +0000534 case ARM::fixup_arm_pcrel_10_unscaled: {
535 Value = Value - 8; // ARM fixups offset by an additional word and don't
536 // need to adjust for the half-word ordering.
537 bool isAdd = true;
538 if ((int64_t)Value < 0) {
539 Value = -Value;
540 isAdd = false;
541 }
Jim Grosbach913cc302012-03-30 21:54:22 +0000542 // The value has the low 4 bits encoded in [3:0] and the high 4 in [11:8].
Jim Grosbache78031a2012-04-30 22:30:43 +0000543 if (Ctx && Value >= 256)
544 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
Jim Grosbach913cc302012-03-30 21:54:22 +0000545 Value = (Value & 0xf) | ((Value & 0xf0) << 4);
Jim Grosbach8648c102011-12-19 23:06:24 +0000546 return Value | (isAdd << 23);
547 }
Jim Grosbach3c685612010-12-08 20:32:07 +0000548 case ARM::fixup_arm_pcrel_10:
Owen Anderson4743d752010-12-10 22:46:47 +0000549 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach3c685612010-12-08 20:32:07 +0000550 // need to adjust for the half-word ordering.
Joe Abbey8e72eb72014-09-16 09:18:23 +0000551 // Fall through.
Jim Grosbach3c685612010-12-08 20:32:07 +0000552 case ARM::fixup_t2_pcrel_10: {
553 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson4743d752010-12-10 22:46:47 +0000554 Value = Value - 4;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000555 bool isAdd = true;
556 if ((int64_t)Value < 0) {
557 Value = -Value;
558 isAdd = false;
559 }
560 // These values don't encode the low two bits since they're always zero.
561 Value >>= 2;
Jim Grosbache78031a2012-04-30 22:30:43 +0000562 if (Ctx && Value >= 256)
563 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
Jason W Kimfc5c5222010-12-01 22:46:50 +0000564 Value |= isAdd << 23;
Jim Grosbach3c685612010-12-08 20:32:07 +0000565
Jim Grosbach8648c102011-12-19 23:06:24 +0000566 // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords
567 // swapped.
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000568 if (Kind == ARM::fixup_t2_pcrel_10)
569 return swapHalfWords(Value, IsLittleEndian);
Jim Grosbach3c685612010-12-08 20:32:07 +0000570
Jason W Kimfc5c5222010-12-01 22:46:50 +0000571 return Value;
572 }
573 }
574}
575
Jim Grosbache78031a2012-04-30 22:30:43 +0000576void ARMAsmBackend::processFixupValue(const MCAssembler &Asm,
577 const MCAsmLayout &Layout,
578 const MCFixup &Fixup,
579 const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000580 const MCValue &Target, uint64_t &Value,
Jim Grosbache78031a2012-04-30 22:30:43 +0000581 bool &IsResolved) {
582 const MCSymbolRefExpr *A = Target.getSymA();
583 // Some fixups to thumb function symbols need the low bit (thumb bit)
584 // twiddled.
585 if ((unsigned)Fixup.getKind() != ARM::fixup_arm_ldst_pcrel_12 &&
586 (unsigned)Fixup.getKind() != ARM::fixup_t2_ldst_pcrel_12 &&
587 (unsigned)Fixup.getKind() != ARM::fixup_arm_adr_pcrel_12 &&
588 (unsigned)Fixup.getKind() != ARM::fixup_thumb_adr_pcrel_10 &&
589 (unsigned)Fixup.getKind() != ARM::fixup_t2_adr_pcrel_12 &&
590 (unsigned)Fixup.getKind() != ARM::fixup_arm_thumb_cp) {
591 if (A) {
592 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
593 if (Asm.isThumbFunc(&Sym))
594 Value |= 1;
595 }
596 }
Logan Chiend5c48aa2014-02-05 14:15:16 +0000597 // For Thumb1 BL instruction, it is possible to be a long jump between
598 // the basic blocks of the same function. Thus, we would like to resolve
599 // the offset when the destination has the same MCFragment.
600 if (A && (unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_bl) {
601 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
David Blaikie908f4d42014-04-24 16:59:40 +0000602 const MCSymbolData &SymData = Asm.getSymbolData(Sym);
Logan Chiend5c48aa2014-02-05 14:15:16 +0000603 IsResolved = (SymData.getFragment() == DF);
604 }
Jim Grosbache78031a2012-04-30 22:30:43 +0000605 // We must always generate a relocation for BL/BLX instructions if we have
606 // a symbol to reference, as the linker relies on knowing the destination
607 // symbol's thumb-ness to get interworking right.
608 if (A && ((unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_blx ||
Jim Grosbache78031a2012-04-30 22:30:43 +0000609 (unsigned)Fixup.getKind() == ARM::fixup_arm_blx ||
610 (unsigned)Fixup.getKind() == ARM::fixup_arm_uncondbl ||
611 (unsigned)Fixup.getKind() == ARM::fixup_arm_condbl))
612 IsResolved = false;
613
614 // Try to get the encoded value for the fixup as-if we're mapping it into
615 // the instruction. This allows adjustFixupValue() to issue a diagnostic
616 // if the value aren't invalid.
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000617 (void)adjustFixupValue(Fixup, Value, false, &Asm.getContext(),
618 IsLittleEndian);
Jim Grosbache78031a2012-04-30 22:30:43 +0000619}
620
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000621/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000622static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach90987142010-11-09 01:37:15 +0000623 switch (Kind) {
Jim Grosbach9e199462010-12-06 23:57:07 +0000624 default:
625 llvm_unreachable("Unknown fixup kind!");
Bill Wendling8a6449c2010-12-08 01:57:09 +0000626
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000627 case FK_Data_1:
Jim Grosbach78485ad2010-12-10 17:13:40 +0000628 case ARM::fixup_arm_thumb_bcc:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000629 case ARM::fixup_arm_thumb_cp:
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000630 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000631 return 1;
632
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000633 case FK_Data_2:
Jim Grosbache119da12010-12-10 18:21:33 +0000634 case ARM::fixup_arm_thumb_br:
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000635 case ARM::fixup_arm_thumb_cb:
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000636 return 2;
637
Jim Grosbach8648c102011-12-19 23:06:24 +0000638 case ARM::fixup_arm_pcrel_10_unscaled:
Jim Grosbach9e199462010-12-06 23:57:07 +0000639 case ARM::fixup_arm_ldst_pcrel_12:
640 case ARM::fixup_arm_pcrel_10:
641 case ARM::fixup_arm_adr_pcrel_12:
James Molloyfb5cd602012-03-30 09:15:32 +0000642 case ARM::fixup_arm_uncondbl:
643 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +0000644 case ARM::fixup_arm_blx:
Jason W Kimd2e2f562011-02-04 19:47:15 +0000645 case ARM::fixup_arm_condbranch:
646 case ARM::fixup_arm_uncondbranch:
Jim Grosbach9e199462010-12-06 23:57:07 +0000647 return 3;
Bill Wendling8a6449c2010-12-08 01:57:09 +0000648
649 case FK_Data_4:
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000650 case ARM::fixup_t2_ldst_pcrel_12:
Owen Anderson578074b2010-12-13 19:31:11 +0000651 case ARM::fixup_t2_condbranch:
652 case ARM::fixup_t2_uncondbranch:
Owen Anderson0f7142d2010-12-08 00:18:36 +0000653 case ARM::fixup_t2_pcrel_10:
Owen Anderson6d375e52010-12-14 00:36:49 +0000654 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach9e199462010-12-06 23:57:07 +0000655 case ARM::fixup_arm_thumb_bl:
Bill Wendling3392bfc2010-12-09 00:39:08 +0000656 case ARM::fixup_arm_thumb_blx:
Evan Chengd4a5c052011-01-14 02:38:49 +0000657 case ARM::fixup_arm_movt_hi16:
658 case ARM::fixup_arm_movw_lo16:
Evan Chengd4a5c052011-01-14 02:38:49 +0000659 case ARM::fixup_t2_movt_hi16:
660 case ARM::fixup_t2_movw_lo16:
Jim Grosbach9e199462010-12-06 23:57:07 +0000661 return 4;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000662
Saleem Abdulrasoolfc6b85b2014-05-08 01:35:57 +0000663 case FK_SecRel_2:
664 return 2;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000665 case FK_SecRel_4:
666 return 4;
Jim Grosbach90987142010-11-09 01:37:15 +0000667 }
668}
669
Christian Pirker2a111602014-03-28 14:35:30 +0000670/// getFixupKindContainerSizeBytes - The number of bytes of the
671/// container involved in big endian.
672static unsigned getFixupKindContainerSizeBytes(unsigned Kind) {
673 switch (Kind) {
674 default:
675 llvm_unreachable("Unknown fixup kind!");
676
677 case FK_Data_1:
678 return 1;
679 case FK_Data_2:
680 return 2;
681 case FK_Data_4:
682 return 4;
683
684 case ARM::fixup_arm_thumb_bcc:
685 case ARM::fixup_arm_thumb_cp:
686 case ARM::fixup_thumb_adr_pcrel_10:
687 case ARM::fixup_arm_thumb_br:
688 case ARM::fixup_arm_thumb_cb:
689 // Instruction size is 2 bytes.
690 return 2;
691
692 case ARM::fixup_arm_pcrel_10_unscaled:
693 case ARM::fixup_arm_ldst_pcrel_12:
694 case ARM::fixup_arm_pcrel_10:
695 case ARM::fixup_arm_adr_pcrel_12:
696 case ARM::fixup_arm_uncondbl:
697 case ARM::fixup_arm_condbl:
698 case ARM::fixup_arm_blx:
699 case ARM::fixup_arm_condbranch:
700 case ARM::fixup_arm_uncondbranch:
701 case ARM::fixup_t2_ldst_pcrel_12:
702 case ARM::fixup_t2_condbranch:
703 case ARM::fixup_t2_uncondbranch:
704 case ARM::fixup_t2_pcrel_10:
705 case ARM::fixup_t2_adr_pcrel_12:
706 case ARM::fixup_arm_thumb_bl:
707 case ARM::fixup_arm_thumb_blx:
708 case ARM::fixup_arm_movt_hi16:
709 case ARM::fixup_arm_movw_lo16:
Christian Pirker2a111602014-03-28 14:35:30 +0000710 case ARM::fixup_t2_movt_hi16:
711 case ARM::fixup_t2_movw_lo16:
Christian Pirker2a111602014-03-28 14:35:30 +0000712 // Instruction size is 4 bytes.
713 return 4;
714 }
715}
716
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000717void ARMAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
Rafael Espindola5904e122014-03-29 06:26:49 +0000718 unsigned DataSize, uint64_t Value,
719 bool IsPCRel) const {
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000720 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000721 Value = adjustFixupValue(Fixup, Value, IsPCRel, nullptr, IsLittleEndian);
Joe Abbey8e72eb72014-09-16 09:18:23 +0000722 if (!Value)
723 return; // Doesn't change encoding.
Jim Grosbach90987142010-11-09 01:37:15 +0000724
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000725 unsigned Offset = Fixup.getOffset();
726 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
727
Christian Pirker2a111602014-03-28 14:35:30 +0000728 // Used to point to big endian bytes.
729 unsigned FullSizeBytes;
Christian Pirker875629f2014-05-20 09:24:37 +0000730 if (!IsLittleEndian) {
Christian Pirker2a111602014-03-28 14:35:30 +0000731 FullSizeBytes = getFixupKindContainerSizeBytes(Fixup.getKind());
Christian Pirker875629f2014-05-20 09:24:37 +0000732 assert((Offset + FullSizeBytes) <= DataSize && "Invalid fixup size!");
733 assert(NumBytes <= FullSizeBytes && "Invalid fixup size!");
734 }
Christian Pirker2a111602014-03-28 14:35:30 +0000735
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000736 // For each byte of the fragment that the fixup touches, mask in the bits from
737 // the fixup value. The Value has been "split up" into the appropriate
738 // bitfields above.
Christian Pirker2a111602014-03-28 14:35:30 +0000739 for (unsigned i = 0; i != NumBytes; ++i) {
740 unsigned Idx = IsLittleEndian ? i : (FullSizeBytes - 1 - i);
741 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
742 }
Jason W Kimb3212452010-09-30 02:17:26 +0000743}
Bill Wendling721724e2010-12-07 23:05:20 +0000744
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000745MCAsmBackend *llvm::createARMAsmBackend(const Target &T,
Joe Abbey8e72eb72014-09-16 09:18:23 +0000746 const MCRegisterInfo &MRI, StringRef TT,
747 StringRef CPU, bool isLittle) {
Owen Anderson975ddf82011-04-01 21:07:39 +0000748 Triple TheTriple(TT);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000749
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000750 switch (TheTriple.getObjectFormat()) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000751 default:
752 llvm_unreachable("unsupported object format");
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000753 case Triple::MachO: {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000754 MachO::CPUSubTypeARM CS =
Joe Abbey8e72eb72014-09-16 09:18:23 +0000755 StringSwitch<MachO::CPUSubTypeARM>(TheTriple.getArchName())
756 .Cases("armv4t", "thumbv4t", MachO::CPU_SUBTYPE_ARM_V4T)
757 .Cases("armv5e", "thumbv5e", MachO::CPU_SUBTYPE_ARM_V5TEJ)
758 .Cases("armv6", "thumbv6", MachO::CPU_SUBTYPE_ARM_V6)
759 .Cases("armv6m", "thumbv6m", MachO::CPU_SUBTYPE_ARM_V6M)
760 .Cases("armv7em", "thumbv7em", MachO::CPU_SUBTYPE_ARM_V7EM)
761 .Cases("armv7k", "thumbv7k", MachO::CPU_SUBTYPE_ARM_V7K)
762 .Cases("armv7m", "thumbv7m", MachO::CPU_SUBTYPE_ARM_V7M)
763 .Cases("armv7s", "thumbv7s", MachO::CPU_SUBTYPE_ARM_V7S)
764 .Default(MachO::CPU_SUBTYPE_ARM_V7);
Quentin Colombet77ca8b82013-01-14 21:34:09 +0000765
Joe Abbey8e72eb72014-09-16 09:18:23 +0000766 return new ARMAsmBackendDarwin(T, TT, CS);
Owen Anderson975ddf82011-04-01 21:07:39 +0000767 }
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000768 case Triple::COFF:
769 assert(TheTriple.isOSWindows() && "non-Windows ARM COFF is not supported");
Joe Abbey8e72eb72014-09-16 09:18:23 +0000770 return new ARMAsmBackendWinCOFF(T, TT);
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000771 case Triple::ELF:
772 assert(TheTriple.isOSBinFormatELF() && "using ELF for non-ELF target");
773 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
Joe Abbey8e72eb72014-09-16 09:18:23 +0000774 return new ARMAsmBackendELF(T, TT, OSABI, isLittle);
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +0000775 }
Jason W Kimb3212452010-09-30 02:17:26 +0000776}
Christian Pirker2a111602014-03-28 14:35:30 +0000777
Christian Pirkerdc9ff752014-04-01 15:19:30 +0000778MCAsmBackend *llvm::createARMLEAsmBackend(const Target &T,
Christian Pirker2a111602014-03-28 14:35:30 +0000779 const MCRegisterInfo &MRI,
780 StringRef TT, StringRef CPU) {
781 return createARMAsmBackend(T, MRI, TT, CPU, true);
782}
783
Christian Pirkerdc9ff752014-04-01 15:19:30 +0000784MCAsmBackend *llvm::createARMBEAsmBackend(const Target &T,
Christian Pirker2a111602014-03-28 14:35:30 +0000785 const MCRegisterInfo &MRI,
786 StringRef TT, StringRef CPU) {
787 return createARMAsmBackend(T, MRI, TT, CPU, false);
788}
789
Christian Pirkerdc9ff752014-04-01 15:19:30 +0000790MCAsmBackend *llvm::createThumbLEAsmBackend(const Target &T,
Joe Abbey8e72eb72014-09-16 09:18:23 +0000791 const MCRegisterInfo &MRI,
792 StringRef TT, StringRef CPU) {
Christian Pirker2a111602014-03-28 14:35:30 +0000793 return createARMAsmBackend(T, MRI, TT, CPU, true);
794}
795
Christian Pirkerdc9ff752014-04-01 15:19:30 +0000796MCAsmBackend *llvm::createThumbBEAsmBackend(const Target &T,
Joe Abbey8e72eb72014-09-16 09:18:23 +0000797 const MCRegisterInfo &MRI,
798 StringRef TT, StringRef CPU) {
Christian Pirker2a111602014-03-28 14:35:30 +0000799 return createARMAsmBackend(T, MRI, TT, CPU, false);
800}