blob: 824dee00c2ab8ce973b3f9120c7940d3ef33d4a5 [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"
Evan Chengad5f4852011-07-23 00:00:19 +000012#include "MCTargetDesc/ARMBaseInfo.h"
13#include "MCTargetDesc/ARMFixupKinds.h"
Quentin Colombet77ca8b82013-01-14 21:34:09 +000014#include "llvm/ADT/StringSwitch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000015#include "llvm/MC/MCAsmBackend.h"
Jason W Kimb3212452010-09-30 02:17:26 +000016#include "llvm/MC/MCAssembler.h"
Jim Grosbache78031a2012-04-30 22:30:43 +000017#include "llvm/MC/MCContext.h"
Jim Grosbach87055ed2010-12-08 01:16:55 +000018#include "llvm/MC/MCDirectives.h"
Rafael Espindolaf0e24d42010-12-17 16:59:53 +000019#include "llvm/MC/MCELFObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000020#include "llvm/MC/MCExpr.h"
Craig Topper6e80c282012-03-26 06:58:25 +000021#include "llvm/MC/MCFixupKindInfo.h"
Daniel Dunbar73b87132010-12-16 16:08:33 +000022#include "llvm/MC/MCMachObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000023#include "llvm/MC/MCObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000024#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach45e50d82011-08-16 17:06:20 +000026#include "llvm/MC/MCSubtargetInfo.h"
Jim Grosbach3b50c9e2012-01-18 00:23:57 +000027#include "llvm/MC/MCValue.h"
Wesley Peck18510902010-10-22 15:52:49 +000028#include "llvm/Support/ELF.h"
Jason W Kimb3212452010-09-30 02:17:26 +000029#include "llvm/Support/ErrorHandling.h"
Charles Davis8bdfafd2013-09-01 04:28:48 +000030#include "llvm/Support/MachO.h"
Jason W Kimb3212452010-09-30 02:17:26 +000031#include "llvm/Support/raw_ostream.h"
Jason W Kimb3212452010-09-30 02:17:26 +000032using namespace llvm;
33
34namespace {
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000035class ARMELFObjectWriter : public MCELFObjectTargetWriter {
36public:
Rafael Espindola1ad40952011-12-21 17:00:36 +000037 ARMELFObjectWriter(uint8_t OSABI)
38 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
Rafael Espindolafdaae0d2010-12-18 03:27:34 +000039 /*HasRelocationAddend*/ false) {}
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000040};
41
Evan Cheng5928e692011-07-25 23:24:55 +000042class ARMAsmBackend : public MCAsmBackend {
Jim Grosbach45e50d82011-08-16 17:06:20 +000043 const MCSubtargetInfo* STI;
Christian Pirker2a111602014-03-28 14:35:30 +000044 bool isThumbMode; // Currently emitting Thumb code.
45 bool IsLittleEndian; // Big or little endian.
Jason W Kimb3212452010-09-30 02:17:26 +000046public:
Christian Pirker2a111602014-03-28 14:35:30 +000047 ARMAsmBackend(const Target &T, const StringRef TT, bool IsLittle)
Jim Grosbach45e50d82011-08-16 17:06:20 +000048 : MCAsmBackend(), STI(ARM_MC::createARMMCSubtargetInfo(TT, "", "")),
Christian Pirker2a111602014-03-28 14:35:30 +000049 isThumbMode(TT.startswith("thumb")), IsLittleEndian(IsLittle) {}
Jim Grosbach45e50d82011-08-16 17:06:20 +000050
51 ~ARMAsmBackend() {
52 delete STI;
53 }
Jason W Kimb3212452010-09-30 02:17:26 +000054
Craig Topperca7e3e52014-03-10 03:19:03 +000055 unsigned getNumFixupKinds() const override {
56 return ARM::NumTargetFixupKinds;
57 }
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000058
Jim Grosbach45e50d82011-08-16 17:06:20 +000059 bool hasNOP() const {
60 return (STI->getFeatureBits() & ARM::HasV6T2Ops) != 0;
61 }
62
Craig Topperca7e3e52014-03-10 03:19:03 +000063 const MCFixupKindInfo &getFixupKindInfo(MCFixupKind Kind) const override {
Christian Pirker2a111602014-03-28 14:35:30 +000064 const static MCFixupKindInfo InfosLE[ARM::NumTargetFixupKinds] = {
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000065// This table *must* be in the order that the fixup_* kinds are defined in
66// ARMFixupKinds.h.
67//
68// Name Offset (bits) Size (bits) Flags
Jim Grosbachd3f02cb2011-11-16 22:48:37 +000069{ "fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000070{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
71 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jim Grosbach8648c102011-12-19 23:06:24 +000072{ "fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachfb2f1d62011-11-01 01:24:45 +000073{ "fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000074{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
75 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
76{ "fixup_thumb_adr_pcrel_10",0, 8, MCFixupKindInfo::FKF_IsPCRel |
77 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jim Grosbachd3f02cb2011-11-16 22:48:37 +000078{ "fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000079{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
80 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Jason W Kimd2e2f562011-02-04 19:47:15 +000081{ "fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
82{ "fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000083{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
84{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
85{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
James Molloyfb5cd602012-03-30 09:15:32 +000086{ "fixup_arm_uncondbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
87{ "fixup_arm_condbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach7b811d32012-02-27 21:36:23 +000088{ "fixup_arm_blx", 0, 24, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000089{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbachf00b9cc2011-08-18 16:57:50 +000090{ "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +000091{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
Jim Grosbach3d6c6292012-04-26 20:48:12 +000092{ "fixup_arm_thumb_cp", 0, 8, MCFixupKindInfo::FKF_IsPCRel |
93 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Eric Christopher368976f2011-05-28 03:16:22 +000094{ "fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel },
Evan Chengd4a5c052011-01-14 02:38:49 +000095// movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
96{ "fixup_arm_movt_hi16", 0, 20, 0 },
97{ "fixup_arm_movw_lo16", 0, 20, 0 },
98{ "fixup_t2_movt_hi16", 0, 20, 0 },
99{ "fixup_t2_movw_lo16", 0, 20, 0 },
100{ "fixup_arm_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
101{ "fixup_arm_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
102{ "fixup_t2_movt_hi16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
103{ "fixup_t2_movw_lo16_pcrel", 0, 20, MCFixupKindInfo::FKF_IsPCRel },
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +0000104 };
Christian Pirker2a111602014-03-28 14:35:30 +0000105 const static MCFixupKindInfo InfosBE[ARM::NumTargetFixupKinds] = {
106// This table *must* be in the order that the fixup_* kinds are defined in
107// ARMFixupKinds.h.
108//
109// Name Offset (bits) Size (bits) Flags
110{ "fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
111{ "fixup_t2_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
112 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
113{ "fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
114{ "fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
115{ "fixup_t2_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
116 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
117{ "fixup_thumb_adr_pcrel_10",8, 8, MCFixupKindInfo::FKF_IsPCRel |
118 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
119{ "fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
120{ "fixup_t2_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel |
121 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
122{ "fixup_arm_condbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
123{ "fixup_arm_uncondbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
124{ "fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
125{ "fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
126{ "fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
127{ "fixup_arm_uncondbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
128{ "fixup_arm_condbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
129{ "fixup_arm_blx", 8, 24, MCFixupKindInfo::FKF_IsPCRel },
130{ "fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
131{ "fixup_arm_thumb_blx", 0, 32, MCFixupKindInfo::FKF_IsPCRel },
132{ "fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel },
133{ "fixup_arm_thumb_cp", 8, 8, MCFixupKindInfo::FKF_IsPCRel |
134 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
135{ "fixup_arm_thumb_bcc", 8, 8, MCFixupKindInfo::FKF_IsPCRel },
136// movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16 - 19.
137{ "fixup_arm_movt_hi16", 12, 20, 0 },
138{ "fixup_arm_movw_lo16", 12, 20, 0 },
139{ "fixup_t2_movt_hi16", 12, 20, 0 },
140{ "fixup_t2_movw_lo16", 12, 20, 0 },
141{ "fixup_arm_movt_hi16_pcrel", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
142{ "fixup_arm_movw_lo16_pcrel", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
143{ "fixup_t2_movt_hi16_pcrel", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
144{ "fixup_t2_movw_lo16_pcrel", 12, 20, MCFixupKindInfo::FKF_IsPCRel },
145 };
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +0000146
147 if (Kind < FirstTargetFixupKind)
Evan Cheng5928e692011-07-25 23:24:55 +0000148 return MCAsmBackend::getFixupKindInfo(Kind);
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +0000149
150 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
151 "Invalid kind!");
Christian Pirker2a111602014-03-28 14:35:30 +0000152 return (IsLittleEndian ? InfosLE : InfosBE)[Kind - FirstTargetFixupKind];
Daniel Dunbar0c9d9fd2010-12-16 03:20:06 +0000153 }
154
Jim Grosbach3b50c9e2012-01-18 00:23:57 +0000155 /// processFixupValue - Target hook to process the literal value of a fixup
156 /// if necessary.
157 void processFixupValue(const MCAssembler &Asm, const MCAsmLayout &Layout,
158 const MCFixup &Fixup, const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000159 const MCValue &Target, uint64_t &Value,
Craig Topperca7e3e52014-03-10 03:19:03 +0000160 bool &IsResolved) override;
Jim Grosbach3b50c9e2012-01-18 00:23:57 +0000161
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000162
163 void applyFixup(const MCFixup &Fixup, char *Data, unsigned DataSize,
Craig Topperca7e3e52014-03-10 03:19:03 +0000164 uint64_t Value) const override;
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000165
Craig Topperca7e3e52014-03-10 03:19:03 +0000166 bool mayNeedRelaxation(const MCInst &Inst) const override;
Jason W Kimb3212452010-09-30 02:17:26 +0000167
Craig Topperca7e3e52014-03-10 03:19:03 +0000168 bool fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000169 const MCRelaxableFragment *DF,
Craig Topperca7e3e52014-03-10 03:19:03 +0000170 const MCAsmLayout &Layout) const override;
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000171
Craig Topperca7e3e52014-03-10 03:19:03 +0000172 void relaxInstruction(const MCInst &Inst, MCInst &Res) const override;
Jason W Kimb3212452010-09-30 02:17:26 +0000173
Craig Topperca7e3e52014-03-10 03:19:03 +0000174 bool writeNopData(uint64_t Count, MCObjectWriter *OW) const override;
Jim Grosbach7e872962010-09-30 17:45:51 +0000175
Craig Topperca7e3e52014-03-10 03:19:03 +0000176 void handleAssemblerFlag(MCAssemblerFlag Flag) override {
Jim Grosbach87055ed2010-12-08 01:16:55 +0000177 switch (Flag) {
178 default: break;
179 case MCAF_Code16:
180 setIsThumb(true);
181 break;
182 case MCAF_Code32:
183 setIsThumb(false);
184 break;
185 }
Jim Grosbach7e872962010-09-30 17:45:51 +0000186 }
Jim Grosbach87055ed2010-12-08 01:16:55 +0000187
188 unsigned getPointerSize() const { return 4; }
189 bool isThumb() const { return isThumbMode; }
190 void setIsThumb(bool it) { isThumbMode = it; }
Christian Pirker2a111602014-03-28 14:35:30 +0000191 bool isLittle() const { return IsLittleEndian; }
Jason W Kimb3212452010-09-30 02:17:26 +0000192};
Chris Lattner9fdd10d2010-11-17 05:41:32 +0000193} // end anonymous namespace
Jason W Kimb3212452010-09-30 02:17:26 +0000194
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000195static unsigned getRelaxedOpcode(unsigned Op) {
196 switch (Op) {
197 default: return Op;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000198 case ARM::tBcc: return ARM::t2Bcc;
Mihai Popa8a9da5b2013-07-22 15:49:36 +0000199 case ARM::tLDRpci: return ARM::t2LDRpci;
Jim Grosbach44e5c392012-01-19 02:09:38 +0000200 case ARM::tADR: return ARM::t2ADR;
Jim Grosbachc4aa60f2012-03-19 21:32:32 +0000201 case ARM::tB: return ARM::t2B;
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000202 case ARM::tCBZ: return ARM::tHINT;
203 case ARM::tCBNZ: return ARM::tHINT;
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000204 }
205}
206
Jim Grosbachaba3de92012-01-18 18:52:16 +0000207bool ARMAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000208 if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
209 return true;
Jason W Kimb3212452010-09-30 02:17:26 +0000210 return false;
211}
212
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000213bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup,
214 uint64_t Value,
Eli Bendersky4d9ada02013-01-08 00:22:56 +0000215 const MCRelaxableFragment *DF,
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000216 const MCAsmLayout &Layout) const {
Benjamin Kramer116e99a2012-01-19 21:11:13 +0000217 switch ((unsigned)Fixup.getKind()) {
Jim Grosbachc4aa60f2012-03-19 21:32:32 +0000218 case ARM::fixup_arm_thumb_br: {
219 // Relaxing tB to t2B. tB has a signed 12-bit displacement with the
220 // low bit being an implied zero. There's an implied +4 offset for the
221 // branch, so we adjust the other way here to determine what's
222 // encodable.
223 //
224 // Relax if the value is too big for a (signed) i8.
225 int64_t Offset = int64_t(Value) - 4;
226 return Offset > 2046 || Offset < -2048;
227 }
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000228 case ARM::fixup_arm_thumb_bcc: {
229 // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the
230 // low bit being an implied zero. There's an implied +4 offset for the
231 // branch, so we adjust the other way here to determine what's
232 // encodable.
233 //
234 // Relax if the value is too big for a (signed) i8.
235 int64_t Offset = int64_t(Value) - 4;
236 return Offset > 254 || Offset < -256;
237 }
Jim Grosbach44e5c392012-01-19 02:09:38 +0000238 case ARM::fixup_thumb_adr_pcrel_10:
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000239 case ARM::fixup_arm_thumb_cp: {
Jim Grosbachb008df42012-01-19 01:50:30 +0000240 // If the immediate is negative, greater than 1020, or not a multiple
241 // of four, the wide version of the instruction must be used.
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000242 int64_t Offset = int64_t(Value) - 4;
Jim Grosbachb008df42012-01-19 01:50:30 +0000243 return Offset > 1020 || Offset < 0 || Offset & 3;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000244 }
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000245 case ARM::fixup_arm_thumb_cb:
246 // If we have a Thumb CBZ or CBNZ instruction and its target is the next
247 // instruction it is is actually out of range for the instruction.
248 // It will be changed to a NOP.
249 int64_t Offset = (Value & ~1);
250 return Offset == 2;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000251 }
Benjamin Kramer116e99a2012-01-19 21:11:13 +0000252 llvm_unreachable("Unexpected fixup kind in fixupNeedsRelaxation()!");
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000253}
254
Jim Grosbachaba3de92012-01-18 18:52:16 +0000255void ARMAsmBackend::relaxInstruction(const MCInst &Inst, MCInst &Res) const {
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000256 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
257
258 // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
259 if (RelaxedOp == Inst.getOpcode()) {
260 SmallString<256> Tmp;
261 raw_svector_ostream OS(Tmp);
262 Inst.dump_pretty(OS);
263 OS << "\n";
264 report_fatal_error("unexpected instruction to relax: " + OS.str());
265 }
266
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000267 // If we are changing Thumb CBZ or CBNZ instruction to a NOP, aka tHINT, we
268 // have to change the operands too.
269 if ((Inst.getOpcode() == ARM::tCBZ || Inst.getOpcode() == ARM::tCBNZ) &&
270 RelaxedOp == ARM::tHINT) {
271 Res.setOpcode(RelaxedOp);
272 Res.addOperand(MCOperand::CreateImm(0));
273 Res.addOperand(MCOperand::CreateImm(14));
274 Res.addOperand(MCOperand::CreateReg(0));
275 return;
276 }
277
278 // The rest of instructions we're relaxing have the same operands.
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000279 // We just need to update to the proper opcode.
280 Res = Inst;
281 Res.setOpcode(RelaxedOp);
Jason W Kimb3212452010-09-30 02:17:26 +0000282}
283
Jim Grosbachaba3de92012-01-18 18:52:16 +0000284bool ARMAsmBackend::writeNopData(uint64_t Count, MCObjectWriter *OW) const {
Jim Grosbach45e50d82011-08-16 17:06:20 +0000285 const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
286 const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
David Sehr05176ca2012-12-05 21:01:27 +0000287 const uint32_t ARMv4_NopEncoding = 0xe1a00000; // using MOV r0,r0
Jim Grosbach7ccdb7c2011-11-16 22:40:25 +0000288 const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
Jim Grosbach87055ed2010-12-08 01:16:55 +0000289 if (isThumb()) {
Jim Grosbach45e50d82011-08-16 17:06:20 +0000290 const uint16_t nopEncoding = hasNOP() ? Thumb2_16bitNopEncoding
291 : Thumb1_16bitNopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000292 uint64_t NumNops = Count / 2;
293 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach45e50d82011-08-16 17:06:20 +0000294 OW->Write16(nopEncoding);
Jim Grosbach97f1de72010-12-17 19:03:02 +0000295 if (Count & 1)
296 OW->Write8(0);
Jim Grosbach87055ed2010-12-08 01:16:55 +0000297 return true;
298 }
299 // ARM mode
Jim Grosbach45e50d82011-08-16 17:06:20 +0000300 const uint32_t nopEncoding = hasNOP() ? ARMv6T2_NopEncoding
301 : ARMv4_NopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000302 uint64_t NumNops = Count / 4;
303 for (uint64_t i = 0; i != NumNops; ++i)
Jim Grosbach45e50d82011-08-16 17:06:20 +0000304 OW->Write32(nopEncoding);
305 // FIXME: should this function return false when unable to write exactly
306 // 'Count' bytes with NOP encodings?
Jim Grosbach97f1de72010-12-17 19:03:02 +0000307 switch (Count % 4) {
308 default: break; // No leftover bytes to write
309 case 1: OW->Write8(0); break;
310 case 2: OW->Write16(0); break;
311 case 3: OW->Write16(0); OW->Write8(0xa0); break;
312 }
313
Rafael Espindola0ed15432010-10-25 17:50:35 +0000314 return true;
Jim Grosbach58bce992010-09-30 03:20:34 +0000315}
Jason W Kimb3212452010-09-30 02:17:26 +0000316
Jim Grosbache78031a2012-04-30 22:30:43 +0000317static unsigned adjustFixupValue(const MCFixup &Fixup, uint64_t Value,
318 MCContext *Ctx = NULL) {
319 unsigned Kind = Fixup.getKind();
Jason W Kimfc5c5222010-12-01 22:46:50 +0000320 switch (Kind) {
321 default:
322 llvm_unreachable("Unknown fixup kind!");
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000323 case FK_Data_1:
324 case FK_Data_2:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000325 case FK_Data_4:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000326 return Value;
Jason W Kimd5e6e542010-12-03 19:40:23 +0000327 case ARM::fixup_arm_movt_hi16:
Evan Chengd4a5c052011-01-14 02:38:49 +0000328 Value >>= 16;
329 // Fallthrough
330 case ARM::fixup_arm_movw_lo16:
Jason W Kimd0c937d2011-05-19 20:55:25 +0000331 case ARM::fixup_arm_movt_hi16_pcrel:
Jason W Kim9c5b65d2011-01-12 00:19:25 +0000332 case ARM::fixup_arm_movw_lo16_pcrel: {
Jason W Kimd5e6e542010-12-03 19:40:23 +0000333 unsigned Hi4 = (Value & 0xF000) >> 12;
334 unsigned Lo12 = Value & 0x0FFF;
335 // inst{19-16} = Hi4;
336 // inst{11-0} = Lo12;
337 Value = (Hi4 << 16) | (Lo12);
338 return Value;
339 }
Evan Chengd4a5c052011-01-14 02:38:49 +0000340 case ARM::fixup_t2_movt_hi16:
Evan Chengd4a5c052011-01-14 02:38:49 +0000341 Value >>= 16;
342 // Fallthrough
343 case ARM::fixup_t2_movw_lo16:
Jim Grosbach6629b572011-06-24 20:06:59 +0000344 case ARM::fixup_t2_movt_hi16_pcrel: //FIXME: Shouldn't this be shifted like
345 // the other hi16 fixup?
Evan Chengd4a5c052011-01-14 02:38:49 +0000346 case ARM::fixup_t2_movw_lo16_pcrel: {
347 unsigned Hi4 = (Value & 0xF000) >> 12;
348 unsigned i = (Value & 0x800) >> 11;
349 unsigned Mid3 = (Value & 0x700) >> 8;
350 unsigned Lo8 = Value & 0x0FF;
351 // inst{19-16} = Hi4;
352 // inst{26} = i;
353 // inst{14-12} = Mid3;
354 // inst{7-0} = Lo8;
Jim Grosbachd76f43e2011-09-30 22:02:45 +0000355 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
Evan Chengd4a5c052011-01-14 02:38:49 +0000356 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
357 swapped |= (Value & 0x0000FFFF) << 16;
358 return swapped;
359 }
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000360 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000361 // ARM PC-relative values are offset by 8.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000362 Value -= 4;
Owen Andersoncb4d8f22010-12-09 21:34:47 +0000363 // FALLTHROUGH
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000364 case ARM::fixup_t2_ldst_pcrel_12: {
365 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000366 Value -= 4;
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000367 bool isAdd = true;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000368 if ((int64_t)Value < 0) {
369 Value = -Value;
370 isAdd = false;
371 }
Jim Grosbache78031a2012-04-30 22:30:43 +0000372 if (Ctx && Value >= 4096)
373 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
Jason W Kimfc5c5222010-12-01 22:46:50 +0000374 Value |= isAdd << 23;
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000375
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000376 // Same addressing mode as fixup_arm_pcrel_10,
377 // but with 16-bit halfwords swapped.
378 if (Kind == ARM::fixup_t2_ldst_pcrel_12) {
379 uint64_t swapped = (Value & 0xFFFF0000) >> 16;
380 swapped |= (Value & 0x0000FFFF) << 16;
381 return swapped;
382 }
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000383
Jason W Kimfc5c5222010-12-01 22:46:50 +0000384 return Value;
385 }
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000386 case ARM::fixup_thumb_adr_pcrel_10:
387 return ((Value - 4) >> 2) & 0xff;
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000388 case ARM::fixup_arm_adr_pcrel_12: {
389 // ARM PC-relative values are offset by 8.
390 Value -= 8;
391 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
392 if ((int64_t)Value < 0) {
393 Value = -Value;
394 opc = 2; // 0b0010
395 }
Jim Grosbache78031a2012-04-30 22:30:43 +0000396 if (Ctx && ARM_AM::getSOImmVal(Value) == -1)
397 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000398 // Encode the immediate and shift the opcode into place.
399 return ARM_AM::getSOImmVal(Value) | (opc << 21);
400 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000401
Owen Anderson6d375e52010-12-14 00:36:49 +0000402 case ARM::fixup_t2_adr_pcrel_12: {
403 Value -= 4;
404 unsigned opc = 0;
405 if ((int64_t)Value < 0) {
406 Value = -Value;
407 opc = 5;
408 }
409
410 uint32_t out = (opc << 21);
Owen Anderson8543d4f2011-03-23 22:03:44 +0000411 out |= (Value & 0x800) << 15;
Owen Anderson6d375e52010-12-14 00:36:49 +0000412 out |= (Value & 0x700) << 4;
413 out |= (Value & 0x0FF);
Jim Grosbache34793e2010-12-14 16:25:15 +0000414
Owen Anderson6d375e52010-12-14 00:36:49 +0000415 uint64_t swapped = (out & 0xFFFF0000) >> 16;
416 swapped |= (out & 0x0000FFFF) << 16;
417 return swapped;
418 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000419
Jason W Kimd2e2f562011-02-04 19:47:15 +0000420 case ARM::fixup_arm_condbranch:
421 case ARM::fixup_arm_uncondbranch:
James Molloyfb5cd602012-03-30 09:15:32 +0000422 case ARM::fixup_arm_uncondbl:
423 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +0000424 case ARM::fixup_arm_blx:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000425 // These values don't encode the low two bits since they're always zero.
426 // Offset by 8 just as above.
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000427 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
428 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL)
429 return 0;
Jim Grosbach9e199462010-12-06 23:57:07 +0000430 return 0xffffff & ((Value - 8) >> 2);
Owen Anderson578074b2010-12-13 19:31:11 +0000431 case ARM::fixup_t2_uncondbranch: {
Owen Anderson235c2762010-12-10 23:02:28 +0000432 Value = Value - 4;
Owen Anderson302d5fd2010-12-09 00:27:41 +0000433 Value >>= 1; // Low bit is not encoded.
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000434
Jim Grosbachf588c512010-12-13 19:25:46 +0000435 uint32_t out = 0;
Owen Anderson578074b2010-12-13 19:31:11 +0000436 bool I = Value & 0x800000;
437 bool J1 = Value & 0x400000;
438 bool J2 = Value & 0x200000;
439 J1 ^= I;
440 J2 ^= I;
Jim Grosbache34793e2010-12-14 16:25:15 +0000441
Owen Anderson578074b2010-12-13 19:31:11 +0000442 out |= I << 26; // S bit
443 out |= !J1 << 13; // J1 bit
444 out |= !J2 << 11; // J2 bit
445 out |= (Value & 0x1FF800) << 5; // imm6 field
446 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache34793e2010-12-14 16:25:15 +0000447
Owen Anderson578074b2010-12-13 19:31:11 +0000448 uint64_t swapped = (out & 0xFFFF0000) >> 16;
449 swapped |= (out & 0x0000FFFF) << 16;
450 return swapped;
451 }
452 case ARM::fixup_t2_condbranch: {
453 Value = Value - 4;
454 Value >>= 1; // Low bit is not encoded.
Jim Grosbache34793e2010-12-14 16:25:15 +0000455
Owen Anderson578074b2010-12-13 19:31:11 +0000456 uint64_t out = 0;
Owen Anderson14e41272010-12-09 01:02:09 +0000457 out |= (Value & 0x80000) << 7; // S bit
458 out |= (Value & 0x40000) >> 7; // J2 bit
459 out |= (Value & 0x20000) >> 4; // J1 bit
460 out |= (Value & 0x1F800) << 5; // imm6 field
461 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000462
Jim Grosbachf588c512010-12-13 19:25:46 +0000463 uint32_t swapped = (out & 0xFFFF0000) >> 16;
Owen Anderson302d5fd2010-12-09 00:27:41 +0000464 swapped |= (out & 0x0000FFFF) << 16;
465 return swapped;
466 }
Jim Grosbach9e199462010-12-06 23:57:07 +0000467 case ARM::fixup_arm_thumb_bl: {
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000468 // The value doesn't encode the low bit (always zero) and is offset by
469 // four. The 32-bit immediate value is encoded as
470 // imm32 = SignExtend(S:I1:I2:imm10:imm11:0)
471 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
472 // The value is encoded into disjoint bit positions in the destination
473 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
474 // J = either J1 or J2 bit
475 //
476 // BL: xxxxxSIIIIIIIIII xxJxJIIIIIIIIIII
477 //
478 // Note that the halfwords are stored high first, low second; so we need
479 // to transpose the fixup value here to map properly.
480 uint32_t offset = (Value - 4) >> 1;
481 uint32_t signBit = (offset & 0x800000) >> 23;
482 uint32_t I1Bit = (offset & 0x400000) >> 22;
483 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
484 uint32_t I2Bit = (offset & 0x200000) >> 21;
485 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
486 uint32_t imm10Bits = (offset & 0x1FF800) >> 11;
487 uint32_t imm11Bits = (offset & 0x000007FF);
NAKAMURA Takumi8018a292013-06-11 06:52:36 +0000488
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000489 uint32_t Binary = 0;
490 uint32_t firstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10Bits);
491 uint32_t secondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
492 (uint16_t)imm11Bits);
493 Binary |= secondHalf << 16;
494 Binary |= firstHalf;
495 return Binary;
Bill Wendling3392bfc2010-12-09 00:39:08 +0000496 }
497 case ARM::fixup_arm_thumb_blx: {
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000498 // The value doesn't encode the low two bits (always zero) and is offset by
499 // four (see fixup_arm_thumb_cp). The 32-bit immediate value is encoded as
500 // imm32 = SignExtend(S:I1:I2:imm10H:imm10L:00)
501 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
502 // The value is encoded into disjoint bit positions in the destination
503 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
504 // J = either J1 or J2 bit, 0 = zero.
505 //
506 // BLX: xxxxxSIIIIIIIIII xxJxJIIIIIIIIII0
507 //
508 // Note that the halfwords are stored high first, low second; so we need
509 // to transpose the fixup value here to map properly.
510 uint32_t offset = (Value - 2) >> 2;
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000511 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
512 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_TLSCALL)
513 offset = 0;
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000514 uint32_t signBit = (offset & 0x400000) >> 22;
515 uint32_t I1Bit = (offset & 0x200000) >> 21;
516 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
517 uint32_t I2Bit = (offset & 0x100000) >> 20;
518 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
519 uint32_t imm10HBits = (offset & 0xFFC00) >> 10;
520 uint32_t imm10LBits = (offset & 0x3FF);
NAKAMURA Takumi8018a292013-06-11 06:52:36 +0000521
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000522 uint32_t Binary = 0;
523 uint32_t firstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10HBits);
524 uint32_t secondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
525 ((uint16_t)imm10LBits) << 1);
526 Binary |= secondHalf << 16;
527 Binary |= firstHalf;
528 return Binary;
Jim Grosbach9e199462010-12-06 23:57:07 +0000529 }
Bill Wendling8a6449c2010-12-08 01:57:09 +0000530 case ARM::fixup_arm_thumb_cp:
Jim Grosbach3c685612010-12-08 20:32:07 +0000531 // Offset by 4, and don't encode the low two bits. Two bytes of that
532 // 'off by 4' is implicitly handled by the half-word ordering of the
533 // Thumb encoding, so we only need to adjust by 2 here.
534 return ((Value - 2) >> 2) & 0xff;
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000535 case ARM::fixup_arm_thumb_cb: {
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000536 // Offset by 4 and don't encode the lower bit, which is always 0.
537 uint32_t Binary = (Value - 4) >> 1;
Owen Andersonf636a642010-12-14 19:42:53 +0000538 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000539 }
Jim Grosbache119da12010-12-10 18:21:33 +0000540 case ARM::fixup_arm_thumb_br:
541 // Offset by 4 and don't encode the lower bit, which is always 0.
542 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach78485ad2010-12-10 17:13:40 +0000543 case ARM::fixup_arm_thumb_bcc:
544 // Offset by 4 and don't encode the lower bit, which is always 0.
545 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach8648c102011-12-19 23:06:24 +0000546 case ARM::fixup_arm_pcrel_10_unscaled: {
547 Value = Value - 8; // ARM fixups offset by an additional word and don't
548 // need to adjust for the half-word ordering.
549 bool isAdd = true;
550 if ((int64_t)Value < 0) {
551 Value = -Value;
552 isAdd = false;
553 }
Jim Grosbach913cc302012-03-30 21:54:22 +0000554 // 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 +0000555 if (Ctx && Value >= 256)
556 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
Jim Grosbach913cc302012-03-30 21:54:22 +0000557 Value = (Value & 0xf) | ((Value & 0xf0) << 4);
Jim Grosbach8648c102011-12-19 23:06:24 +0000558 return Value | (isAdd << 23);
559 }
Jim Grosbach3c685612010-12-08 20:32:07 +0000560 case ARM::fixup_arm_pcrel_10:
Owen Anderson4743d752010-12-10 22:46:47 +0000561 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach3c685612010-12-08 20:32:07 +0000562 // need to adjust for the half-word ordering.
563 // Fall through.
564 case ARM::fixup_t2_pcrel_10: {
565 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson4743d752010-12-10 22:46:47 +0000566 Value = Value - 4;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000567 bool isAdd = true;
568 if ((int64_t)Value < 0) {
569 Value = -Value;
570 isAdd = false;
571 }
572 // These values don't encode the low two bits since they're always zero.
573 Value >>= 2;
Jim Grosbache78031a2012-04-30 22:30:43 +0000574 if (Ctx && Value >= 256)
575 Ctx->FatalError(Fixup.getLoc(), "out of range pc-relative fixup value");
Jason W Kimfc5c5222010-12-01 22:46:50 +0000576 Value |= isAdd << 23;
Jim Grosbach3c685612010-12-08 20:32:07 +0000577
Jim Grosbach8648c102011-12-19 23:06:24 +0000578 // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords
579 // swapped.
Owen Anderson0f7142d2010-12-08 00:18:36 +0000580 if (Kind == ARM::fixup_t2_pcrel_10) {
Jim Grosbachf588c512010-12-13 19:25:46 +0000581 uint32_t swapped = (Value & 0xFFFF0000) >> 16;
Owen Anderson72ce4532010-12-08 00:21:33 +0000582 swapped |= (Value & 0x0000FFFF) << 16;
Owen Anderson0f7142d2010-12-08 00:18:36 +0000583 return swapped;
584 }
Jim Grosbach3c685612010-12-08 20:32:07 +0000585
Jason W Kimfc5c5222010-12-01 22:46:50 +0000586 return Value;
587 }
588 }
589}
590
Jim Grosbache78031a2012-04-30 22:30:43 +0000591void ARMAsmBackend::processFixupValue(const MCAssembler &Asm,
592 const MCAsmLayout &Layout,
593 const MCFixup &Fixup,
594 const MCFragment *DF,
Rafael Espindola3e3de5e2014-03-28 16:06:09 +0000595 const MCValue &Target, uint64_t &Value,
Jim Grosbache78031a2012-04-30 22:30:43 +0000596 bool &IsResolved) {
597 const MCSymbolRefExpr *A = Target.getSymA();
598 // Some fixups to thumb function symbols need the low bit (thumb bit)
599 // twiddled.
600 if ((unsigned)Fixup.getKind() != ARM::fixup_arm_ldst_pcrel_12 &&
601 (unsigned)Fixup.getKind() != ARM::fixup_t2_ldst_pcrel_12 &&
602 (unsigned)Fixup.getKind() != ARM::fixup_arm_adr_pcrel_12 &&
603 (unsigned)Fixup.getKind() != ARM::fixup_thumb_adr_pcrel_10 &&
604 (unsigned)Fixup.getKind() != ARM::fixup_t2_adr_pcrel_12 &&
605 (unsigned)Fixup.getKind() != ARM::fixup_arm_thumb_cp) {
606 if (A) {
607 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
608 if (Asm.isThumbFunc(&Sym))
609 Value |= 1;
610 }
611 }
Logan Chiend5c48aa2014-02-05 14:15:16 +0000612 // For Thumb1 BL instruction, it is possible to be a long jump between
613 // the basic blocks of the same function. Thus, we would like to resolve
614 // the offset when the destination has the same MCFragment.
615 if (A && (unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_bl) {
616 const MCSymbol &Sym = A->getSymbol().AliasedSymbol();
617 MCSymbolData &SymData = Asm.getSymbolData(Sym);
618 IsResolved = (SymData.getFragment() == DF);
619 }
Jim Grosbache78031a2012-04-30 22:30:43 +0000620 // We must always generate a relocation for BL/BLX instructions if we have
621 // a symbol to reference, as the linker relies on knowing the destination
622 // symbol's thumb-ness to get interworking right.
623 if (A && ((unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_blx ||
Jim Grosbache78031a2012-04-30 22:30:43 +0000624 (unsigned)Fixup.getKind() == ARM::fixup_arm_blx ||
625 (unsigned)Fixup.getKind() == ARM::fixup_arm_uncondbl ||
626 (unsigned)Fixup.getKind() == ARM::fixup_arm_condbl))
627 IsResolved = false;
628
629 // Try to get the encoded value for the fixup as-if we're mapping it into
630 // the instruction. This allows adjustFixupValue() to issue a diagnostic
631 // if the value aren't invalid.
632 (void)adjustFixupValue(Fixup, Value, &Asm.getContext());
633}
634
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000635/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000636static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach90987142010-11-09 01:37:15 +0000637 switch (Kind) {
Jim Grosbach9e199462010-12-06 23:57:07 +0000638 default:
639 llvm_unreachable("Unknown fixup kind!");
Bill Wendling8a6449c2010-12-08 01:57:09 +0000640
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000641 case FK_Data_1:
Jim Grosbach78485ad2010-12-10 17:13:40 +0000642 case ARM::fixup_arm_thumb_bcc:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000643 case ARM::fixup_arm_thumb_cp:
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000644 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000645 return 1;
646
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000647 case FK_Data_2:
Jim Grosbache119da12010-12-10 18:21:33 +0000648 case ARM::fixup_arm_thumb_br:
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000649 case ARM::fixup_arm_thumb_cb:
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000650 return 2;
651
Jim Grosbach8648c102011-12-19 23:06:24 +0000652 case ARM::fixup_arm_pcrel_10_unscaled:
Jim Grosbach9e199462010-12-06 23:57:07 +0000653 case ARM::fixup_arm_ldst_pcrel_12:
654 case ARM::fixup_arm_pcrel_10:
655 case ARM::fixup_arm_adr_pcrel_12:
James Molloyfb5cd602012-03-30 09:15:32 +0000656 case ARM::fixup_arm_uncondbl:
657 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +0000658 case ARM::fixup_arm_blx:
Jason W Kimd2e2f562011-02-04 19:47:15 +0000659 case ARM::fixup_arm_condbranch:
660 case ARM::fixup_arm_uncondbranch:
Jim Grosbach9e199462010-12-06 23:57:07 +0000661 return 3;
Bill Wendling8a6449c2010-12-08 01:57:09 +0000662
663 case FK_Data_4:
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000664 case ARM::fixup_t2_ldst_pcrel_12:
Owen Anderson578074b2010-12-13 19:31:11 +0000665 case ARM::fixup_t2_condbranch:
666 case ARM::fixup_t2_uncondbranch:
Owen Anderson0f7142d2010-12-08 00:18:36 +0000667 case ARM::fixup_t2_pcrel_10:
Owen Anderson6d375e52010-12-14 00:36:49 +0000668 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach9e199462010-12-06 23:57:07 +0000669 case ARM::fixup_arm_thumb_bl:
Bill Wendling3392bfc2010-12-09 00:39:08 +0000670 case ARM::fixup_arm_thumb_blx:
Evan Chengd4a5c052011-01-14 02:38:49 +0000671 case ARM::fixup_arm_movt_hi16:
672 case ARM::fixup_arm_movw_lo16:
673 case ARM::fixup_arm_movt_hi16_pcrel:
674 case ARM::fixup_arm_movw_lo16_pcrel:
675 case ARM::fixup_t2_movt_hi16:
676 case ARM::fixup_t2_movw_lo16:
677 case ARM::fixup_t2_movt_hi16_pcrel:
678 case ARM::fixup_t2_movw_lo16_pcrel:
Jim Grosbach9e199462010-12-06 23:57:07 +0000679 return 4;
Jim Grosbach90987142010-11-09 01:37:15 +0000680 }
681}
682
Christian Pirker2a111602014-03-28 14:35:30 +0000683/// getFixupKindContainerSizeBytes - The number of bytes of the
684/// container involved in big endian.
685static unsigned getFixupKindContainerSizeBytes(unsigned Kind) {
686 switch (Kind) {
687 default:
688 llvm_unreachable("Unknown fixup kind!");
689
690 case FK_Data_1:
691 return 1;
692 case FK_Data_2:
693 return 2;
694 case FK_Data_4:
695 return 4;
696
697 case ARM::fixup_arm_thumb_bcc:
698 case ARM::fixup_arm_thumb_cp:
699 case ARM::fixup_thumb_adr_pcrel_10:
700 case ARM::fixup_arm_thumb_br:
701 case ARM::fixup_arm_thumb_cb:
702 // Instruction size is 2 bytes.
703 return 2;
704
705 case ARM::fixup_arm_pcrel_10_unscaled:
706 case ARM::fixup_arm_ldst_pcrel_12:
707 case ARM::fixup_arm_pcrel_10:
708 case ARM::fixup_arm_adr_pcrel_12:
709 case ARM::fixup_arm_uncondbl:
710 case ARM::fixup_arm_condbl:
711 case ARM::fixup_arm_blx:
712 case ARM::fixup_arm_condbranch:
713 case ARM::fixup_arm_uncondbranch:
714 case ARM::fixup_t2_ldst_pcrel_12:
715 case ARM::fixup_t2_condbranch:
716 case ARM::fixup_t2_uncondbranch:
717 case ARM::fixup_t2_pcrel_10:
718 case ARM::fixup_t2_adr_pcrel_12:
719 case ARM::fixup_arm_thumb_bl:
720 case ARM::fixup_arm_thumb_blx:
721 case ARM::fixup_arm_movt_hi16:
722 case ARM::fixup_arm_movw_lo16:
723 case ARM::fixup_arm_movt_hi16_pcrel:
724 case ARM::fixup_arm_movw_lo16_pcrel:
725 case ARM::fixup_t2_movt_hi16:
726 case ARM::fixup_t2_movw_lo16:
727 case ARM::fixup_t2_movt_hi16_pcrel:
728 case ARM::fixup_t2_movw_lo16_pcrel:
729 // Instruction size is 4 bytes.
730 return 4;
731 }
732}
733
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000734void ARMAsmBackend::applyFixup(const MCFixup &Fixup, char *Data,
735 unsigned DataSize, uint64_t Value) const {
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000736 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Jim Grosbache78031a2012-04-30 22:30:43 +0000737 Value = adjustFixupValue(Fixup, Value);
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000738 if (!Value) return; // Doesn't change encoding.
Jim Grosbach90987142010-11-09 01:37:15 +0000739
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000740 unsigned Offset = Fixup.getOffset();
741 assert(Offset + NumBytes <= DataSize && "Invalid fixup offset!");
742
Christian Pirker2a111602014-03-28 14:35:30 +0000743 // Used to point to big endian bytes.
744 unsigned FullSizeBytes;
745 if (!IsLittleEndian)
746 FullSizeBytes = getFixupKindContainerSizeBytes(Fixup.getKind());
747
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000748 // For each byte of the fragment that the fixup touches, mask in the bits from
749 // the fixup value. The Value has been "split up" into the appropriate
750 // bitfields above.
Christian Pirker2a111602014-03-28 14:35:30 +0000751 for (unsigned i = 0; i != NumBytes; ++i) {
752 unsigned Idx = IsLittleEndian ? i : (FullSizeBytes - 1 - i);
753 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
754 }
Jason W Kimb3212452010-09-30 02:17:26 +0000755}
Bill Wendling721724e2010-12-07 23:05:20 +0000756
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000757namespace {
758
759// FIXME: This should be in a separate file.
760// ELF is an ELF of course...
761class ELFARMAsmBackend : public ARMAsmBackend {
762public:
763 uint8_t OSABI;
764 ELFARMAsmBackend(const Target &T, const StringRef TT,
Christian Pirker2a111602014-03-28 14:35:30 +0000765 uint8_t _OSABI, bool _IsLittle)
766 : ARMAsmBackend(T, TT, _IsLittle), OSABI(_OSABI) { }
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000767
Craig Topperca7e3e52014-03-10 03:19:03 +0000768 MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
Christian Pirker2a111602014-03-28 14:35:30 +0000769 return createARMELFObjectWriter(OS, OSABI, isLittle());
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000770 }
771};
772
773// FIXME: This should be in a separate file.
774class DarwinARMAsmBackend : public ARMAsmBackend {
775public:
Charles Davis8bdfafd2013-09-01 04:28:48 +0000776 const MachO::CPUSubTypeARM Subtype;
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000777 DarwinARMAsmBackend(const Target &T, const StringRef TT,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000778 MachO::CPUSubTypeARM st)
Christian Pirker2a111602014-03-28 14:35:30 +0000779 : ARMAsmBackend(T, TT, /* IsLittleEndian */ true), Subtype(st) {
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000780 HasDataInCodeSupport = true;
781 }
782
Craig Topperca7e3e52014-03-10 03:19:03 +0000783 MCObjectWriter *createObjectWriter(raw_ostream &OS) const override {
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000784 return createARMMachObjectWriter(OS, /*Is64Bit=*/false,
Charles Davis8bdfafd2013-09-01 04:28:48 +0000785 MachO::CPU_TYPE_ARM,
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000786 Subtype);
787 }
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000788};
789
Jim Grosbach689651c2010-09-30 03:21:00 +0000790} // end anonymous namespace
Jason W Kimb3212452010-09-30 02:17:26 +0000791
Bill Wendling58e2d3d2013-09-09 02:37:14 +0000792MCAsmBackend *llvm::createARMAsmBackend(const Target &T,
793 const MCRegisterInfo &MRI,
Christian Pirker2a111602014-03-28 14:35:30 +0000794 StringRef TT, StringRef CPU,
795 bool isLittle) {
Owen Anderson975ddf82011-04-01 21:07:39 +0000796 Triple TheTriple(TT);
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000797
Tim Northoverd6a729b2014-01-06 14:28:05 +0000798 if (TheTriple.isOSBinFormatMachO()) {
Charles Davis8bdfafd2013-09-01 04:28:48 +0000799 MachO::CPUSubTypeARM CS =
800 StringSwitch<MachO::CPUSubTypeARM>(TheTriple.getArchName())
801 .Cases("armv4t", "thumbv4t", MachO::CPU_SUBTYPE_ARM_V4T)
802 .Cases("armv5e", "thumbv5e", MachO::CPU_SUBTYPE_ARM_V5TEJ)
803 .Cases("armv6", "thumbv6", MachO::CPU_SUBTYPE_ARM_V6)
804 .Cases("armv6m", "thumbv6m", MachO::CPU_SUBTYPE_ARM_V6M)
805 .Cases("armv7em", "thumbv7em", MachO::CPU_SUBTYPE_ARM_V7EM)
Charles Davis8bdfafd2013-09-01 04:28:48 +0000806 .Cases("armv7k", "thumbv7k", MachO::CPU_SUBTYPE_ARM_V7K)
807 .Cases("armv7m", "thumbv7m", MachO::CPU_SUBTYPE_ARM_V7M)
808 .Cases("armv7s", "thumbv7s", MachO::CPU_SUBTYPE_ARM_V7S)
809 .Default(MachO::CPU_SUBTYPE_ARM_V7);
Quentin Colombet77ca8b82013-01-14 21:34:09 +0000810
811 return new DarwinARMAsmBackend(T, TT, CS);
Owen Anderson975ddf82011-04-01 21:07:39 +0000812 }
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000813
NAKAMURA Takumi4d5ee8042013-06-11 10:01:42 +0000814#if 0
815 // FIXME: Introduce yet another checker but assert(0).
NAKAMURA Takumi76380ab2013-06-11 06:52:43 +0000816 if (TheTriple.isOSBinFormatCOFF())
NAKAMURA Takumi4d5ee8042013-06-11 10:01:42 +0000817 assert(0 && "Windows not supported on ARM");
818#endif
Daniel Dunbar2b9b0e32011-04-19 21:14:45 +0000819
Rafael Espindola1ad40952011-12-21 17:00:36 +0000820 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(Triple(TT).getOS());
Christian Pirker2a111602014-03-28 14:35:30 +0000821 return new ELFARMAsmBackend(T, TT, OSABI, isLittle);
Jason W Kimb3212452010-09-30 02:17:26 +0000822}
Christian Pirker2a111602014-03-28 14:35:30 +0000823
824MCAsmBackend *llvm::createARMleAsmBackend(const Target &T,
825 const MCRegisterInfo &MRI,
826 StringRef TT, StringRef CPU) {
827 return createARMAsmBackend(T, MRI, TT, CPU, true);
828}
829
830MCAsmBackend *llvm::createARMbeAsmBackend(const Target &T,
831 const MCRegisterInfo &MRI,
832 StringRef TT, StringRef CPU) {
833 return createARMAsmBackend(T, MRI, TT, CPU, false);
834}
835
836MCAsmBackend *llvm::createThumbleAsmBackend(const Target &T,
837 const MCRegisterInfo &MRI,
838 StringRef TT, StringRef CPU) {
839 return createARMAsmBackend(T, MRI, TT, CPU, true);
840}
841
842MCAsmBackend *llvm::createThumbbeAsmBackend(const Target &T,
843 const MCRegisterInfo &MRI,
844 StringRef TT, StringRef CPU) {
845 return createARMAsmBackend(T, MRI, TT, CPU, false);
846}
847