blob: 40278629a01256c331793a7f9ccae3bb372c605f [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
Joe Abbey8e72eb72014-09-16 09:18:23 +000010#include "MCTargetDesc/ARMAsmBackend.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000011#include "MCTargetDesc/ARMAddressingModes.h"
Joe Abbey8e72eb72014-09-16 09:18:23 +000012#include "MCTargetDesc/ARMAsmBackendDarwin.h"
13#include "MCTargetDesc/ARMAsmBackendELF.h"
14#include "MCTargetDesc/ARMAsmBackendWinCOFF.h"
Evan Chengad5f4852011-07-23 00:00:19 +000015#include "MCTargetDesc/ARMFixupKinds.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000016#include "MCTargetDesc/ARMMCTargetDesc.h"
Quentin Colombet77ca8b82013-01-14 21:34:09 +000017#include "llvm/ADT/StringSwitch.h"
Zachary Turner264b5d92017-06-07 03:48:56 +000018#include "llvm/BinaryFormat/ELF.h"
19#include "llvm/BinaryFormat/MachO.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000020#include "llvm/MC/MCAsmBackend.h"
Jason W Kimb3212452010-09-30 02:17:26 +000021#include "llvm/MC/MCAssembler.h"
Jim Grosbache78031a2012-04-30 22:30:43 +000022#include "llvm/MC/MCContext.h"
Jim Grosbach87055ed2010-12-08 01:16:55 +000023#include "llvm/MC/MCDirectives.h"
Rafael Espindolaf0e24d42010-12-17 16:59:53 +000024#include "llvm/MC/MCELFObjectWriter.h"
Jason W Kimb3212452010-09-30 02:17:26 +000025#include "llvm/MC/MCExpr.h"
Craig Topper6e80c282012-03-26 06:58:25 +000026#include "llvm/MC/MCFixupKindInfo.h"
Jason W Kimb3212452010-09-30 02:17:26 +000027#include "llvm/MC/MCObjectWriter.h"
Tim Northoverf8e47e42015-10-28 22:56:36 +000028#include "llvm/MC/MCRegisterInfo.h"
Jason W Kimb3212452010-09-30 02:17:26 +000029#include "llvm/MC/MCSectionELF.h"
30#include "llvm/MC/MCSectionMachO.h"
Jim Grosbach45e50d82011-08-16 17:06:20 +000031#include "llvm/MC/MCSubtargetInfo.h"
Jim Grosbach3b50c9e2012-01-18 00:23:57 +000032#include "llvm/MC/MCValue.h"
Tim Northoverf8e47e42015-10-28 22:56:36 +000033#include "llvm/Support/Debug.h"
Peter Collingbourne571a3302018-05-21 17:57:19 +000034#include "llvm/Support/EndianStream.h"
Jason W Kimb3212452010-09-30 02:17:26 +000035#include "llvm/Support/ErrorHandling.h"
Tim Northoverf8e47e42015-10-28 22:56:36 +000036#include "llvm/Support/Format.h"
Vedant Kumar366dd9fd2015-08-21 21:52:48 +000037#include "llvm/Support/TargetParser.h"
Jason W Kimb3212452010-09-30 02:17:26 +000038#include "llvm/Support/raw_ostream.h"
Jason W Kimb3212452010-09-30 02:17:26 +000039using namespace llvm;
40
41namespace {
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000042class ARMELFObjectWriter : public MCELFObjectTargetWriter {
43public:
Rafael Espindola1ad40952011-12-21 17:00:36 +000044 ARMELFObjectWriter(uint8_t OSABI)
Joe Abbey8e72eb72014-09-16 09:18:23 +000045 : MCELFObjectTargetWriter(/*Is64Bit*/ false, OSABI, ELF::EM_ARM,
46 /*HasRelocationAddend*/ false) {}
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000047};
Benjamin Kramerb32a5042016-01-27 19:29:42 +000048} // end anonymous namespace
Rafael Espindola6b5e56c2010-12-17 17:45:22 +000049
Joe Abbey8e72eb72014-09-16 09:18:23 +000050const MCFixupKindInfo &ARMAsmBackend::getFixupKindInfo(MCFixupKind Kind) const {
51 const static MCFixupKindInfo InfosLE[ARM::NumTargetFixupKinds] = {
52 // This table *must* be in the order that the fixup_* kinds are defined in
53 // ARMFixupKinds.h.
54 //
55 // Name Offset (bits) Size (bits) Flags
56 {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
57 {"fixup_t2_ldst_pcrel_12", 0, 32,
58 MCFixupKindInfo::FKF_IsPCRel |
59 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
60 {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
61 {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
62 {"fixup_t2_pcrel_10", 0, 32,
63 MCFixupKindInfo::FKF_IsPCRel |
64 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Oliver Stannard65b85382016-01-25 10:26:26 +000065 {"fixup_arm_pcrel_9", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
66 {"fixup_t2_pcrel_9", 0, 32,
67 MCFixupKindInfo::FKF_IsPCRel |
68 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Joe Abbey8e72eb72014-09-16 09:18:23 +000069 {"fixup_thumb_adr_pcrel_10", 0, 8,
70 MCFixupKindInfo::FKF_IsPCRel |
71 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
72 {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
73 {"fixup_t2_adr_pcrel_12", 0, 32,
74 MCFixupKindInfo::FKF_IsPCRel |
75 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
76 {"fixup_arm_condbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
77 {"fixup_arm_uncondbranch", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
78 {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
79 {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
80 {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
81 {"fixup_arm_uncondbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
82 {"fixup_arm_condbl", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
83 {"fixup_arm_blx", 0, 24, MCFixupKindInfo::FKF_IsPCRel},
84 {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
Tim Northover56048d52016-05-10 21:48:48 +000085 {"fixup_arm_thumb_blx", 0, 32,
86 MCFixupKindInfo::FKF_IsPCRel |
87 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Joe Abbey8e72eb72014-09-16 09:18:23 +000088 {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
89 {"fixup_arm_thumb_cp", 0, 8,
90 MCFixupKindInfo::FKF_IsPCRel |
91 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
92 {"fixup_arm_thumb_bcc", 0, 8, MCFixupKindInfo::FKF_IsPCRel},
93 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16
94 // - 19.
95 {"fixup_arm_movt_hi16", 0, 20, 0},
96 {"fixup_arm_movw_lo16", 0, 20, 0},
97 {"fixup_t2_movt_hi16", 0, 20, 0},
98 {"fixup_t2_movw_lo16", 0, 20, 0},
James Molloyb876c722016-04-01 09:40:47 +000099 {"fixup_arm_mod_imm", 0, 12, 0},
Peter Smithadde6672017-06-05 09:37:12 +0000100 {"fixup_t2_so_imm", 0, 26, 0},
Joe Abbey8e72eb72014-09-16 09:18:23 +0000101 };
102 const static MCFixupKindInfo InfosBE[ARM::NumTargetFixupKinds] = {
103 // This table *must* be in the order that the fixup_* kinds are defined in
104 // ARMFixupKinds.h.
105 //
106 // Name Offset (bits) Size (bits) Flags
107 {"fixup_arm_ldst_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
108 {"fixup_t2_ldst_pcrel_12", 0, 32,
109 MCFixupKindInfo::FKF_IsPCRel |
110 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
111 {"fixup_arm_pcrel_10_unscaled", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
112 {"fixup_arm_pcrel_10", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
113 {"fixup_t2_pcrel_10", 0, 32,
114 MCFixupKindInfo::FKF_IsPCRel |
115 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Oliver Stannard65b85382016-01-25 10:26:26 +0000116 {"fixup_arm_pcrel_9", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
117 {"fixup_t2_pcrel_9", 0, 32,
118 MCFixupKindInfo::FKF_IsPCRel |
119 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Joe Abbey8e72eb72014-09-16 09:18:23 +0000120 {"fixup_thumb_adr_pcrel_10", 8, 8,
121 MCFixupKindInfo::FKF_IsPCRel |
122 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
123 {"fixup_arm_adr_pcrel_12", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
124 {"fixup_t2_adr_pcrel_12", 0, 32,
125 MCFixupKindInfo::FKF_IsPCRel |
126 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
127 {"fixup_arm_condbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
128 {"fixup_arm_uncondbranch", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
129 {"fixup_t2_condbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
130 {"fixup_t2_uncondbranch", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
131 {"fixup_arm_thumb_br", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
132 {"fixup_arm_uncondbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
133 {"fixup_arm_condbl", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
134 {"fixup_arm_blx", 8, 24, MCFixupKindInfo::FKF_IsPCRel},
135 {"fixup_arm_thumb_bl", 0, 32, MCFixupKindInfo::FKF_IsPCRel},
Tim Northover56048d52016-05-10 21:48:48 +0000136 {"fixup_arm_thumb_blx", 0, 32,
137 MCFixupKindInfo::FKF_IsPCRel |
138 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
Joe Abbey8e72eb72014-09-16 09:18:23 +0000139 {"fixup_arm_thumb_cb", 0, 16, MCFixupKindInfo::FKF_IsPCRel},
140 {"fixup_arm_thumb_cp", 8, 8,
141 MCFixupKindInfo::FKF_IsPCRel |
142 MCFixupKindInfo::FKF_IsAlignedDownTo32Bits},
143 {"fixup_arm_thumb_bcc", 8, 8, MCFixupKindInfo::FKF_IsPCRel},
144 // movw / movt: 16-bits immediate but scattered into two chunks 0 - 12, 16
145 // - 19.
146 {"fixup_arm_movt_hi16", 12, 20, 0},
147 {"fixup_arm_movw_lo16", 12, 20, 0},
148 {"fixup_t2_movt_hi16", 12, 20, 0},
149 {"fixup_t2_movw_lo16", 12, 20, 0},
James Molloyb876c722016-04-01 09:40:47 +0000150 {"fixup_arm_mod_imm", 20, 12, 0},
Peter Smithadde6672017-06-05 09:37:12 +0000151 {"fixup_t2_so_imm", 26, 6, 0},
Joe Abbey8e72eb72014-09-16 09:18:23 +0000152 };
Jim Grosbach45e50d82011-08-16 17:06:20 +0000153
Joe Abbey8e72eb72014-09-16 09:18:23 +0000154 if (Kind < FirstTargetFixupKind)
155 return MCAsmBackend::getFixupKindInfo(Kind);
156
157 assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() &&
158 "Invalid kind!");
Peter Collingbourne571a3302018-05-21 17:57:19 +0000159 return (Endian == support::little ? InfosLE
160 : InfosBE)[Kind - FirstTargetFixupKind];
Joe Abbey8e72eb72014-09-16 09:18:23 +0000161}
162
163void ARMAsmBackend::handleAssemblerFlag(MCAssemblerFlag Flag) {
164 switch (Flag) {
165 default:
166 break;
167 case MCAF_Code16:
168 setIsThumb(true);
169 break;
170 case MCAF_Code32:
171 setIsThumb(false);
172 break;
Jim Grosbach45e50d82011-08-16 17:06:20 +0000173 }
Joe Abbey8e72eb72014-09-16 09:18:23 +0000174}
Jason W Kimb3212452010-09-30 02:17:26 +0000175
Tim Northover42335572015-04-06 18:44:42 +0000176unsigned ARMAsmBackend::getRelaxedOpcode(unsigned Op) const {
Alex Bradbury46db78b2018-01-03 13:46:21 +0000177 bool HasThumb2 = STI.getFeatureBits()[ARM::FeatureThumb2];
178 bool HasV8MBaselineOps = STI.getFeatureBits()[ARM::HasV8MBaselineOps];
Tim Northover42335572015-04-06 18:44:42 +0000179
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000180 switch (Op) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000181 default:
182 return Op;
183 case ARM::tBcc:
Aaron Ballmanac336242015-04-07 13:28:37 +0000184 return HasThumb2 ? (unsigned)ARM::t2Bcc : Op;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000185 case ARM::tLDRpci:
Aaron Ballmanac336242015-04-07 13:28:37 +0000186 return HasThumb2 ? (unsigned)ARM::t2LDRpci : Op;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000187 case ARM::tADR:
Aaron Ballmanac336242015-04-07 13:28:37 +0000188 return HasThumb2 ? (unsigned)ARM::t2ADR : Op;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000189 case ARM::tB:
Bradley Smitha1189102016-01-15 10:26:17 +0000190 return HasV8MBaselineOps ? (unsigned)ARM::t2B : Op;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000191 case ARM::tCBZ:
192 return ARM::tHINT;
193 case ARM::tCBNZ:
194 return ARM::tHINT;
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000195 }
196}
197
Jim Grosbachaba3de92012-01-18 18:52:16 +0000198bool ARMAsmBackend::mayNeedRelaxation(const MCInst &Inst) const {
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000199 if (getRelaxedOpcode(Inst.getOpcode()) != Inst.getOpcode())
200 return true;
Jason W Kimb3212452010-09-30 02:17:26 +0000201 return false;
202}
203
Tim Northover8d67b8e2015-10-02 18:07:18 +0000204const char *ARMAsmBackend::reasonForFixupRelaxation(const MCFixup &Fixup,
205 uint64_t Value) const {
Benjamin Kramer116e99a2012-01-19 21:11:13 +0000206 switch ((unsigned)Fixup.getKind()) {
Jim Grosbachc4aa60f2012-03-19 21:32:32 +0000207 case ARM::fixup_arm_thumb_br: {
208 // Relaxing tB to t2B. tB has a signed 12-bit displacement with the
209 // low bit being an implied zero. There's an implied +4 offset for the
210 // branch, so we adjust the other way here to determine what's
211 // encodable.
212 //
213 // Relax if the value is too big for a (signed) i8.
214 int64_t Offset = int64_t(Value) - 4;
Tim Northover8d67b8e2015-10-02 18:07:18 +0000215 if (Offset > 2046 || Offset < -2048)
216 return "out of range pc-relative fixup value";
217 break;
Jim Grosbachc4aa60f2012-03-19 21:32:32 +0000218 }
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000219 case ARM::fixup_arm_thumb_bcc: {
220 // Relaxing tBcc to t2Bcc. tBcc has a signed 9-bit displacement with the
221 // low bit being an implied zero. There's an implied +4 offset for the
222 // branch, so we adjust the other way here to determine what's
223 // encodable.
224 //
225 // Relax if the value is too big for a (signed) i8.
226 int64_t Offset = int64_t(Value) - 4;
Tim Northover8d67b8e2015-10-02 18:07:18 +0000227 if (Offset > 254 || Offset < -256)
228 return "out of range pc-relative fixup value";
229 break;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000230 }
Jim Grosbach44e5c392012-01-19 02:09:38 +0000231 case ARM::fixup_thumb_adr_pcrel_10:
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000232 case ARM::fixup_arm_thumb_cp: {
Jim Grosbachb008df42012-01-19 01:50:30 +0000233 // If the immediate is negative, greater than 1020, or not a multiple
234 // of four, the wide version of the instruction must be used.
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000235 int64_t Offset = int64_t(Value) - 4;
Tim Northover8d67b8e2015-10-02 18:07:18 +0000236 if (Offset & 3)
237 return "misaligned pc-relative fixup value";
238 else if (Offset > 1020 || Offset < 0)
239 return "out of range pc-relative fixup value";
240 break;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000241 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000242 case ARM::fixup_arm_thumb_cb: {
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000243 // If we have a Thumb CBZ or CBNZ instruction and its target is the next
Hiroshi Inoue9ff23802018-04-09 04:37:53 +0000244 // instruction it is actually out of range for the instruction.
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000245 // It will be changed to a NOP.
246 int64_t Offset = (Value & ~1);
Tim Northover8d67b8e2015-10-02 18:07:18 +0000247 if (Offset == 2)
248 return "will be converted to nop";
249 break;
Jim Grosbachcb80eb22012-01-18 21:54:16 +0000250 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000251 default:
252 llvm_unreachable("Unexpected fixup kind in reasonForFixupRelaxation()!");
253 }
254 return nullptr;
255}
256
257bool ARMAsmBackend::fixupNeedsRelaxation(const MCFixup &Fixup, uint64_t Value,
258 const MCRelaxableFragment *DF,
259 const MCAsmLayout &Layout) const {
260 return reasonForFixupRelaxation(Fixup, Value);
Jim Grosbach25b63fa2011-12-06 00:47:03 +0000261}
262
Nirav Dave86030622016-07-11 14:23:53 +0000263void ARMAsmBackend::relaxInstruction(const MCInst &Inst,
264 const MCSubtargetInfo &STI,
265 MCInst &Res) const {
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000266 unsigned RelaxedOp = getRelaxedOpcode(Inst.getOpcode());
267
268 // Sanity check w/ diagnostic if we get here w/ a bogus instruction.
269 if (RelaxedOp == Inst.getOpcode()) {
270 SmallString<256> Tmp;
271 raw_svector_ostream OS(Tmp);
272 Inst.dump_pretty(OS);
273 OS << "\n";
274 report_fatal_error("unexpected instruction to relax: " + OS.str());
275 }
276
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000277 // If we are changing Thumb CBZ or CBNZ instruction to a NOP, aka tHINT, we
278 // have to change the operands too.
279 if ((Inst.getOpcode() == ARM::tCBZ || Inst.getOpcode() == ARM::tCBNZ) &&
280 RelaxedOp == ARM::tHINT) {
281 Res.setOpcode(RelaxedOp);
Jim Grosbache9119e42015-05-13 18:37:00 +0000282 Res.addOperand(MCOperand::createImm(0));
283 Res.addOperand(MCOperand::createImm(14));
284 Res.addOperand(MCOperand::createReg(0));
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000285 return;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000286 }
Kevin Enderby9bd296a2014-01-10 00:43:32 +0000287
288 // The rest of instructions we're relaxing have the same operands.
Jim Grosbach34a7c6d2011-12-05 23:45:46 +0000289 // We just need to update to the proper opcode.
290 Res = Inst;
291 Res.setOpcode(RelaxedOp);
Jason W Kimb3212452010-09-30 02:17:26 +0000292}
293
Peter Collingbourne571a3302018-05-21 17:57:19 +0000294bool ARMAsmBackend::writeNopData(raw_ostream &OS, uint64_t Count) const {
Jim Grosbach45e50d82011-08-16 17:06:20 +0000295 const uint16_t Thumb1_16bitNopEncoding = 0x46c0; // using MOV r8,r8
296 const uint16_t Thumb2_16bitNopEncoding = 0xbf00; // NOP
Joe Abbey8e72eb72014-09-16 09:18:23 +0000297 const uint32_t ARMv4_NopEncoding = 0xe1a00000; // using MOV r0,r0
Jim Grosbach7ccdb7c2011-11-16 22:40:25 +0000298 const uint32_t ARMv6T2_NopEncoding = 0xe320f000; // NOP
Jim Grosbach87055ed2010-12-08 01:16:55 +0000299 if (isThumb()) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000300 const uint16_t nopEncoding =
301 hasNOP() ? Thumb2_16bitNopEncoding : Thumb1_16bitNopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000302 uint64_t NumNops = Count / 2;
303 for (uint64_t i = 0; i != NumNops; ++i)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000304 support::endian::write(OS, nopEncoding, Endian);
Jim Grosbach97f1de72010-12-17 19:03:02 +0000305 if (Count & 1)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000306 OS << '\0';
Jim Grosbach87055ed2010-12-08 01:16:55 +0000307 return true;
308 }
309 // ARM mode
Joe Abbey8e72eb72014-09-16 09:18:23 +0000310 const uint32_t nopEncoding =
311 hasNOP() ? ARMv6T2_NopEncoding : ARMv4_NopEncoding;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000312 uint64_t NumNops = Count / 4;
313 for (uint64_t i = 0; i != NumNops; ++i)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000314 support::endian::write(OS, nopEncoding, Endian);
Jim Grosbach45e50d82011-08-16 17:06:20 +0000315 // FIXME: should this function return false when unable to write exactly
316 // 'Count' bytes with NOP encodings?
Jim Grosbach97f1de72010-12-17 19:03:02 +0000317 switch (Count % 4) {
Joe Abbey8e72eb72014-09-16 09:18:23 +0000318 default:
319 break; // No leftover bytes to write
320 case 1:
Peter Collingbourne571a3302018-05-21 17:57:19 +0000321 OS << '\0';
Joe Abbey8e72eb72014-09-16 09:18:23 +0000322 break;
323 case 2:
Peter Collingbourne571a3302018-05-21 17:57:19 +0000324 OS.write("\0\0", 2);
Joe Abbey8e72eb72014-09-16 09:18:23 +0000325 break;
326 case 3:
Peter Collingbourne571a3302018-05-21 17:57:19 +0000327 OS.write("\0\0\xa0", 3);
Joe Abbey8e72eb72014-09-16 09:18:23 +0000328 break;
Jim Grosbach97f1de72010-12-17 19:03:02 +0000329 }
330
Rafael Espindola0ed15432010-10-25 17:50:35 +0000331 return true;
Jim Grosbach58bce992010-09-30 03:20:34 +0000332}
Jason W Kimb3212452010-09-30 02:17:26 +0000333
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000334static uint32_t swapHalfWords(uint32_t Value, bool IsLittleEndian) {
335 if (IsLittleEndian) {
336 // Note that the halfwords are stored high first and low second in thumb;
337 // so we need to swap the fixup value here to map properly.
338 uint32_t Swapped = (Value & 0xFFFF0000) >> 16;
339 Swapped |= (Value & 0x0000FFFF) << 16;
340 return Swapped;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000341 } else
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000342 return Value;
343}
344
345static uint32_t joinHalfWords(uint32_t FirstHalf, uint32_t SecondHalf,
346 bool IsLittleEndian) {
347 uint32_t Value;
348
349 if (IsLittleEndian) {
350 Value = (SecondHalf & 0xFFFF) << 16;
351 Value |= (FirstHalf & 0xFFFF);
352 } else {
353 Value = (SecondHalf & 0xFFFF);
354 Value |= (FirstHalf & 0xFFFF) << 16;
355 }
356
357 return Value;
358}
359
Rafael Espindola801b42d2017-06-23 22:52:36 +0000360unsigned ARMAsmBackend::adjustFixupValue(const MCAssembler &Asm,
361 const MCFixup &Fixup,
362 const MCValue &Target, uint64_t Value,
Peter Collingbourneec8236e2018-05-16 00:21:47 +0000363 bool IsResolved, MCContext &Ctx) const {
Jim Grosbache78031a2012-04-30 22:30:43 +0000364 unsigned Kind = Fixup.getKind();
Rafael Espindola801b42d2017-06-23 22:52:36 +0000365
366 // MachO tries to make .o files that look vaguely pre-linked, so for MOVW/MOVT
367 // and .word relocations they put the Thumb bit into the addend if possible.
368 // Other relocation types don't want this bit though (branches couldn't encode
369 // it if it *was* present, and no other relocations exist) and it can
370 // interfere with checking valid expressions.
371 if (const MCSymbolRefExpr *A = Target.getSymA()) {
372 if (A->hasSubsectionsViaSymbols() && Asm.isThumbFunc(&A->getSymbol()) &&
373 (Kind == FK_Data_4 || Kind == ARM::fixup_arm_movw_lo16 ||
374 Kind == ARM::fixup_arm_movt_hi16 || Kind == ARM::fixup_t2_movw_lo16 ||
375 Kind == ARM::fixup_t2_movt_hi16))
376 Value |= 1;
377 }
378
Jason W Kimfc5c5222010-12-01 22:46:50 +0000379 switch (Kind) {
380 default:
Alex Bradbury866113c2017-04-05 10:16:14 +0000381 Ctx.reportError(Fixup.getLoc(), "bad relocation fixup type");
Chad Rosier771db6f2017-01-18 15:02:54 +0000382 return 0;
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000383 case FK_Data_1:
384 case FK_Data_2:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000385 case FK_Data_4:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000386 return Value;
Saleem Abdulrasoolfc6b85b2014-05-08 01:35:57 +0000387 case FK_SecRel_2:
388 return Value;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000389 case FK_SecRel_4:
390 return Value;
Jason W Kimd5e6e542010-12-03 19:40:23 +0000391 case ARM::fixup_arm_movt_hi16:
Alex Bradbury46db78b2018-01-03 13:46:21 +0000392 if (IsResolved || !STI.getTargetTriple().isOSBinFormatELF())
Rafael Espindola5904e122014-03-29 06:26:49 +0000393 Value >>= 16;
Justin Bognerb03fd122016-08-17 05:10:15 +0000394 LLVM_FALLTHROUGH;
Rafael Espindola5904e122014-03-29 06:26:49 +0000395 case ARM::fixup_arm_movw_lo16: {
Jason W Kimd5e6e542010-12-03 19:40:23 +0000396 unsigned Hi4 = (Value & 0xF000) >> 12;
397 unsigned Lo12 = Value & 0x0FFF;
398 // inst{19-16} = Hi4;
399 // inst{11-0} = Lo12;
400 Value = (Hi4 << 16) | (Lo12);
401 return Value;
402 }
Evan Chengd4a5c052011-01-14 02:38:49 +0000403 case ARM::fixup_t2_movt_hi16:
Alex Bradbury46db78b2018-01-03 13:46:21 +0000404 if (IsResolved || !STI.getTargetTriple().isOSBinFormatELF())
Rafael Espindola5904e122014-03-29 06:26:49 +0000405 Value >>= 16;
Justin Bognerb03fd122016-08-17 05:10:15 +0000406 LLVM_FALLTHROUGH;
Rafael Espindola5904e122014-03-29 06:26:49 +0000407 case ARM::fixup_t2_movw_lo16: {
Evan Chengd4a5c052011-01-14 02:38:49 +0000408 unsigned Hi4 = (Value & 0xF000) >> 12;
409 unsigned i = (Value & 0x800) >> 11;
410 unsigned Mid3 = (Value & 0x700) >> 8;
411 unsigned Lo8 = Value & 0x0FF;
412 // inst{19-16} = Hi4;
413 // inst{26} = i;
414 // inst{14-12} = Mid3;
415 // inst{7-0} = Lo8;
Jim Grosbachd76f43e2011-09-30 22:02:45 +0000416 Value = (Hi4 << 16) | (i << 26) | (Mid3 << 12) | (Lo8);
Peter Collingbourne571a3302018-05-21 17:57:19 +0000417 return swapHalfWords(Value, Endian == support::little);
Evan Chengd4a5c052011-01-14 02:38:49 +0000418 }
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000419 case ARM::fixup_arm_ldst_pcrel_12:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000420 // ARM PC-relative values are offset by 8.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000421 Value -= 4;
Justin Bognerb03fd122016-08-17 05:10:15 +0000422 LLVM_FALLTHROUGH;
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000423 case ARM::fixup_t2_ldst_pcrel_12: {
424 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson3ef19d92010-12-09 20:27:52 +0000425 Value -= 4;
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000426 bool isAdd = true;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000427 if ((int64_t)Value < 0) {
428 Value = -Value;
429 isAdd = false;
430 }
Alex Bradbury866113c2017-04-05 10:16:14 +0000431 if (Value >= 4096) {
432 Ctx.reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000433 return 0;
434 }
Jason W Kimfc5c5222010-12-01 22:46:50 +0000435 Value |= isAdd << 23;
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000436
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000437 // Same addressing mode as fixup_arm_pcrel_10,
438 // but with 16-bit halfwords swapped.
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000439 if (Kind == ARM::fixup_t2_ldst_pcrel_12)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000440 return swapHalfWords(Value, Endian == support::little);
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000441
Jason W Kimfc5c5222010-12-01 22:46:50 +0000442 return Value;
443 }
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000444 case ARM::fixup_arm_adr_pcrel_12: {
445 // ARM PC-relative values are offset by 8.
446 Value -= 8;
447 unsigned opc = 4; // bits {24-21}. Default to add: 0b0100
448 if ((int64_t)Value < 0) {
449 Value = -Value;
450 opc = 2; // 0b0010
451 }
Alex Bradbury866113c2017-04-05 10:16:14 +0000452 if (ARM_AM::getSOImmVal(Value) == -1) {
453 Ctx.reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000454 return 0;
455 }
Jim Grosbachce2bd8d2010-12-02 00:28:45 +0000456 // Encode the immediate and shift the opcode into place.
457 return ARM_AM::getSOImmVal(Value) | (opc << 21);
458 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000459
Owen Anderson6d375e52010-12-14 00:36:49 +0000460 case ARM::fixup_t2_adr_pcrel_12: {
461 Value -= 4;
462 unsigned opc = 0;
463 if ((int64_t)Value < 0) {
464 Value = -Value;
465 opc = 5;
466 }
467
468 uint32_t out = (opc << 21);
Owen Anderson8543d4f2011-03-23 22:03:44 +0000469 out |= (Value & 0x800) << 15;
Owen Anderson6d375e52010-12-14 00:36:49 +0000470 out |= (Value & 0x700) << 4;
471 out |= (Value & 0x0FF);
Jim Grosbache34793e2010-12-14 16:25:15 +0000472
Peter Collingbourne571a3302018-05-21 17:57:19 +0000473 return swapHalfWords(out, Endian == support::little);
Owen Anderson6d375e52010-12-14 00:36:49 +0000474 }
Jim Grosbache34793e2010-12-14 16:25:15 +0000475
Jason W Kimd2e2f562011-02-04 19:47:15 +0000476 case ARM::fixup_arm_condbranch:
477 case ARM::fixup_arm_uncondbranch:
James Molloyfb5cd602012-03-30 09:15:32 +0000478 case ARM::fixup_arm_uncondbl:
479 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +0000480 case ARM::fixup_arm_blx:
Jason W Kimfc5c5222010-12-01 22:46:50 +0000481 // These values don't encode the low two bits since they're always zero.
482 // Offset by 8 just as above.
Joe Abbey8e72eb72014-09-16 09:18:23 +0000483 if (const MCSymbolRefExpr *SRE =
484 dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
Davide Italiano249c45d2016-03-15 00:25:22 +0000485 if (SRE->getKind() == MCSymbolRefExpr::VK_TLSCALL)
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000486 return 0;
Jim Grosbach9e199462010-12-06 23:57:07 +0000487 return 0xffffff & ((Value - 8) >> 2);
Owen Anderson578074b2010-12-13 19:31:11 +0000488 case ARM::fixup_t2_uncondbranch: {
Owen Anderson235c2762010-12-10 23:02:28 +0000489 Value = Value - 4;
Owen Anderson302d5fd2010-12-09 00:27:41 +0000490 Value >>= 1; // Low bit is not encoded.
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000491
Jim Grosbachf588c512010-12-13 19:25:46 +0000492 uint32_t out = 0;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000493 bool I = Value & 0x800000;
Owen Anderson578074b2010-12-13 19:31:11 +0000494 bool J1 = Value & 0x400000;
495 bool J2 = Value & 0x200000;
496 J1 ^= I;
497 J2 ^= I;
Jim Grosbache34793e2010-12-14 16:25:15 +0000498
Joe Abbey8e72eb72014-09-16 09:18:23 +0000499 out |= I << 26; // S bit
500 out |= !J1 << 13; // J1 bit
501 out |= !J2 << 11; // J2 bit
502 out |= (Value & 0x1FF800) << 5; // imm6 field
503 out |= (Value & 0x0007FF); // imm11 field
Jim Grosbache34793e2010-12-14 16:25:15 +0000504
Peter Collingbourne571a3302018-05-21 17:57:19 +0000505 return swapHalfWords(out, Endian == support::little);
Owen Anderson578074b2010-12-13 19:31:11 +0000506 }
507 case ARM::fixup_t2_condbranch: {
508 Value = Value - 4;
509 Value >>= 1; // Low bit is not encoded.
Jim Grosbache34793e2010-12-14 16:25:15 +0000510
Owen Anderson578074b2010-12-13 19:31:11 +0000511 uint64_t out = 0;
Owen Anderson14e41272010-12-09 01:02:09 +0000512 out |= (Value & 0x80000) << 7; // S bit
513 out |= (Value & 0x40000) >> 7; // J2 bit
514 out |= (Value & 0x20000) >> 4; // J1 bit
515 out |= (Value & 0x1F800) << 5; // imm6 field
516 out |= (Value & 0x007FF); // imm11 field
Jim Grosbach3aeb8672010-12-13 19:18:13 +0000517
Peter Collingbourne571a3302018-05-21 17:57:19 +0000518 return swapHalfWords(out, Endian == support::little);
Owen Anderson302d5fd2010-12-09 00:27:41 +0000519 }
Jim Grosbach9e199462010-12-06 23:57:07 +0000520 case ARM::fixup_arm_thumb_bl: {
Rafael Espindola801b42d2017-06-23 22:52:36 +0000521 // FIXME: We get both thumb1 and thumb2 in here, so we can only check for
522 // the less strict thumb2 value.
523 if (!isInt<26>(Value - 4)) {
524 Ctx.reportError(Fixup.getLoc(), "Relocation out of range");
525 return 0;
526 }
527
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000528 // The value doesn't encode the low bit (always zero) and is offset by
529 // four. The 32-bit immediate value is encoded as
530 // imm32 = SignExtend(S:I1:I2:imm10:imm11:0)
531 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
532 // The value is encoded into disjoint bit positions in the destination
533 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
534 // J = either J1 or J2 bit
535 //
536 // BL: xxxxxSIIIIIIIIII xxJxJIIIIIIIIIII
537 //
538 // Note that the halfwords are stored high first, low second; so we need
539 // to transpose the fixup value here to map properly.
540 uint32_t offset = (Value - 4) >> 1;
541 uint32_t signBit = (offset & 0x800000) >> 23;
542 uint32_t I1Bit = (offset & 0x400000) >> 22;
543 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
544 uint32_t I2Bit = (offset & 0x200000) >> 21;
545 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
546 uint32_t imm10Bits = (offset & 0x1FF800) >> 11;
547 uint32_t imm11Bits = (offset & 0x000007FF);
NAKAMURA Takumi8018a292013-06-11 06:52:36 +0000548
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000549 uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10Bits);
550 uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
Joe Abbey8e72eb72014-09-16 09:18:23 +0000551 (uint16_t)imm11Bits);
Peter Collingbourne571a3302018-05-21 17:57:19 +0000552 return joinHalfWords(FirstHalf, SecondHalf, Endian == support::little);
Bill Wendling3392bfc2010-12-09 00:39:08 +0000553 }
554 case ARM::fixup_arm_thumb_blx: {
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000555 // The value doesn't encode the low two bits (always zero) and is offset by
556 // four (see fixup_arm_thumb_cp). The 32-bit immediate value is encoded as
557 // imm32 = SignExtend(S:I1:I2:imm10H:imm10L:00)
558 // where I1 = NOT(J1 ^ S) and I2 = NOT(J2 ^ S).
559 // The value is encoded into disjoint bit positions in the destination
560 // opcode. x = unchanged, I = immediate value bit, S = sign extension bit,
561 // J = either J1 or J2 bit, 0 = zero.
562 //
563 // BLX: xxxxxSIIIIIIIIII xxJxJIIIIIIIIII0
564 //
565 // Note that the halfwords are stored high first, low second; so we need
566 // to transpose the fixup value here to map properly.
Alex Bradbury866113c2017-04-05 10:16:14 +0000567 if (Value % 4 != 0) {
568 Ctx.reportError(Fixup.getLoc(), "misaligned ARM call destination");
Tim Northover56048d52016-05-10 21:48:48 +0000569 return 0;
570 }
571
572 uint32_t offset = (Value - 4) >> 2;
Joe Abbey8e72eb72014-09-16 09:18:23 +0000573 if (const MCSymbolRefExpr *SRE =
574 dyn_cast<MCSymbolRefExpr>(Fixup.getValue()))
Davide Italiano249c45d2016-03-15 00:25:22 +0000575 if (SRE->getKind() == MCSymbolRefExpr::VK_TLSCALL)
Saleem Abdulrasool6e00ca82014-01-30 04:02:31 +0000576 offset = 0;
Saleem Abdulrasool077fd252014-01-26 22:29:36 +0000577 uint32_t signBit = (offset & 0x400000) >> 22;
578 uint32_t I1Bit = (offset & 0x200000) >> 21;
579 uint32_t J1Bit = (I1Bit ^ 0x1) ^ signBit;
580 uint32_t I2Bit = (offset & 0x100000) >> 20;
581 uint32_t J2Bit = (I2Bit ^ 0x1) ^ signBit;
582 uint32_t imm10HBits = (offset & 0xFFC00) >> 10;
583 uint32_t imm10LBits = (offset & 0x3FF);
NAKAMURA Takumi8018a292013-06-11 06:52:36 +0000584
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000585 uint32_t FirstHalf = (((uint16_t)signBit << 10) | (uint16_t)imm10HBits);
586 uint32_t SecondHalf = (((uint16_t)J1Bit << 13) | ((uint16_t)J2Bit << 11) |
Joe Abbey8e72eb72014-09-16 09:18:23 +0000587 ((uint16_t)imm10LBits) << 1);
Peter Collingbourne571a3302018-05-21 17:57:19 +0000588 return joinHalfWords(FirstHalf, SecondHalf, Endian == support::little);
Jim Grosbach9e199462010-12-06 23:57:07 +0000589 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000590 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000591 case ARM::fixup_arm_thumb_cp:
Tim Northover8d67b8e2015-10-02 18:07:18 +0000592 // On CPUs supporting Thumb2, this will be relaxed to an ldr.w, otherwise we
593 // could have an error on our hands.
Alex Bradbury46db78b2018-01-03 13:46:21 +0000594 if (!STI.getFeatureBits()[ARM::FeatureThumb2] && IsResolved) {
Tim Northover8d67b8e2015-10-02 18:07:18 +0000595 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000596 if (FixupDiagnostic) {
Alex Bradbury866113c2017-04-05 10:16:14 +0000597 Ctx.reportError(Fixup.getLoc(), FixupDiagnostic);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000598 return 0;
599 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000600 }
601 // Offset by 4, and don't encode the low two bits.
602 return ((Value - 4) >> 2) & 0xff;
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000603 case ARM::fixup_arm_thumb_cb: {
Prakhar Bahugunaa27c4a02016-08-16 10:41:56 +0000604 // CB instructions can only branch to offsets in [4, 126] in multiples of 2
605 // so ensure that the raw value LSB is zero and it lies in [2, 130].
606 // An offset of 2 will be relaxed to a NOP.
Alex Bradbury866113c2017-04-05 10:16:14 +0000607 if ((int64_t)Value < 2 || Value > 0x82 || Value & 1) {
608 Ctx.reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
Prakhar Bahuguna15ed7ec2016-08-16 10:41:52 +0000609 return 0;
610 }
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000611 // Offset by 4 and don't encode the lower bit, which is always 0.
Tim Northover8d67b8e2015-10-02 18:07:18 +0000612 // FIXME: diagnose if no Thumb2
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000613 uint32_t Binary = (Value - 4) >> 1;
Owen Andersonf636a642010-12-14 19:42:53 +0000614 return ((Binary & 0x20) << 4) | ((Binary & 0x1f) << 3);
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000615 }
Jim Grosbache119da12010-12-10 18:21:33 +0000616 case ARM::fixup_arm_thumb_br:
617 // Offset by 4 and don't encode the lower bit, which is always 0.
Alex Bradbury46db78b2018-01-03 13:46:21 +0000618 if (!STI.getFeatureBits()[ARM::FeatureThumb2] &&
619 !STI.getFeatureBits()[ARM::HasV8MBaselineOps]) {
Tim Northover8d67b8e2015-10-02 18:07:18 +0000620 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000621 if (FixupDiagnostic) {
Alex Bradbury866113c2017-04-05 10:16:14 +0000622 Ctx.reportError(Fixup.getLoc(), FixupDiagnostic);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000623 return 0;
624 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000625 }
Jim Grosbache119da12010-12-10 18:21:33 +0000626 return ((Value - 4) >> 1) & 0x7ff;
Jim Grosbach78485ad2010-12-10 17:13:40 +0000627 case ARM::fixup_arm_thumb_bcc:
628 // Offset by 4 and don't encode the lower bit, which is always 0.
Alex Bradbury46db78b2018-01-03 13:46:21 +0000629 if (!STI.getFeatureBits()[ARM::FeatureThumb2]) {
Tim Northover8d67b8e2015-10-02 18:07:18 +0000630 const char *FixupDiagnostic = reasonForFixupRelaxation(Fixup, Value);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000631 if (FixupDiagnostic) {
Alex Bradbury866113c2017-04-05 10:16:14 +0000632 Ctx.reportError(Fixup.getLoc(), FixupDiagnostic);
Oliver Stannard9be59af2015-11-17 10:00:43 +0000633 return 0;
634 }
Tim Northover8d67b8e2015-10-02 18:07:18 +0000635 }
Jim Grosbach78485ad2010-12-10 17:13:40 +0000636 return ((Value - 4) >> 1) & 0xff;
Jim Grosbach8648c102011-12-19 23:06:24 +0000637 case ARM::fixup_arm_pcrel_10_unscaled: {
638 Value = Value - 8; // ARM fixups offset by an additional word and don't
639 // need to adjust for the half-word ordering.
640 bool isAdd = true;
641 if ((int64_t)Value < 0) {
642 Value = -Value;
643 isAdd = false;
644 }
Jim Grosbach913cc302012-03-30 21:54:22 +0000645 // The value has the low 4 bits encoded in [3:0] and the high 4 in [11:8].
Alex Bradbury866113c2017-04-05 10:16:14 +0000646 if (Value >= 256) {
647 Ctx.reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000648 return 0;
649 }
Jim Grosbach913cc302012-03-30 21:54:22 +0000650 Value = (Value & 0xf) | ((Value & 0xf0) << 4);
Jim Grosbach8648c102011-12-19 23:06:24 +0000651 return Value | (isAdd << 23);
652 }
Jim Grosbach3c685612010-12-08 20:32:07 +0000653 case ARM::fixup_arm_pcrel_10:
Owen Anderson4743d752010-12-10 22:46:47 +0000654 Value = Value - 4; // ARM fixups offset by an additional word and don't
Jim Grosbach3c685612010-12-08 20:32:07 +0000655 // need to adjust for the half-word ordering.
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000656 LLVM_FALLTHROUGH;
Jim Grosbach3c685612010-12-08 20:32:07 +0000657 case ARM::fixup_t2_pcrel_10: {
658 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
Owen Anderson4743d752010-12-10 22:46:47 +0000659 Value = Value - 4;
Jason W Kimfc5c5222010-12-01 22:46:50 +0000660 bool isAdd = true;
661 if ((int64_t)Value < 0) {
662 Value = -Value;
663 isAdd = false;
664 }
665 // These values don't encode the low two bits since they're always zero.
666 Value >>= 2;
Alex Bradbury866113c2017-04-05 10:16:14 +0000667 if (Value >= 256) {
668 Ctx.reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
Oliver Stannard9be59af2015-11-17 10:00:43 +0000669 return 0;
670 }
Jason W Kimfc5c5222010-12-01 22:46:50 +0000671 Value |= isAdd << 23;
Jim Grosbach3c685612010-12-08 20:32:07 +0000672
Jim Grosbach8648c102011-12-19 23:06:24 +0000673 // Same addressing mode as fixup_arm_pcrel_10, but with 16-bit halfwords
674 // swapped.
Christian Pirkerfdce7ce2014-05-06 10:05:11 +0000675 if (Kind == ARM::fixup_t2_pcrel_10)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000676 return swapHalfWords(Value, Endian == support::little);
Jim Grosbach3c685612010-12-08 20:32:07 +0000677
Jason W Kimfc5c5222010-12-01 22:46:50 +0000678 return Value;
679 }
Oliver Stannard65b85382016-01-25 10:26:26 +0000680 case ARM::fixup_arm_pcrel_9:
681 Value = Value - 4; // ARM fixups offset by an additional word and don't
682 // need to adjust for the half-word ordering.
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000683 LLVM_FALLTHROUGH;
Oliver Stannard65b85382016-01-25 10:26:26 +0000684 case ARM::fixup_t2_pcrel_9: {
685 // Offset by 4, adjusted by two due to the half-word ordering of thumb.
686 Value = Value - 4;
687 bool isAdd = true;
688 if ((int64_t)Value < 0) {
689 Value = -Value;
690 isAdd = false;
691 }
692 // These values don't encode the low bit since it's always zero.
Alex Bradbury866113c2017-04-05 10:16:14 +0000693 if (Value & 1) {
694 Ctx.reportError(Fixup.getLoc(), "invalid value for this fixup");
Oliver Stannard65b85382016-01-25 10:26:26 +0000695 return 0;
696 }
697 Value >>= 1;
Alex Bradbury866113c2017-04-05 10:16:14 +0000698 if (Value >= 256) {
699 Ctx.reportError(Fixup.getLoc(), "out of range pc-relative fixup value");
Oliver Stannard65b85382016-01-25 10:26:26 +0000700 return 0;
701 }
702 Value |= isAdd << 23;
703
704 // Same addressing mode as fixup_arm_pcrel_9, but with 16-bit halfwords
705 // swapped.
706 if (Kind == ARM::fixup_t2_pcrel_9)
Peter Collingbourne571a3302018-05-21 17:57:19 +0000707 return swapHalfWords(Value, Endian == support::little);
Oliver Stannard65b85382016-01-25 10:26:26 +0000708
709 return Value;
710 }
James Molloyb876c722016-04-01 09:40:47 +0000711 case ARM::fixup_arm_mod_imm:
712 Value = ARM_AM::getSOImmVal(Value);
Alex Bradbury866113c2017-04-05 10:16:14 +0000713 if (Value >> 12) {
714 Ctx.reportError(Fixup.getLoc(), "out of range immediate fixup value");
James Molloyb876c722016-04-01 09:40:47 +0000715 return 0;
716 }
717 return Value;
Peter Smithd16c55d2017-06-06 10:22:49 +0000718 case ARM::fixup_t2_so_imm: {
Peter Smithadde6672017-06-05 09:37:12 +0000719 Value = ARM_AM::getT2SOImmVal(Value);
720 if ((int64_t)Value < 0) {
721 Ctx.reportError(Fixup.getLoc(), "out of range immediate fixup value");
722 return 0;
723 }
724 // Value will contain a 12-bit value broken up into a 4-bit shift in bits
725 // 11:8 and the 8-bit immediate in 0:7. The instruction has the immediate
726 // in 0:7. The 4-bit shift is split up into i:imm3 where i is placed at bit
727 // 10 of the upper half-word and imm3 is placed at 14:12 of the lower
728 // half-word.
729 uint64_t EncValue = 0;
730 EncValue |= (Value & 0x800) << 15;
731 EncValue |= (Value & 0x700) << 4;
732 EncValue |= (Value & 0xff);
Peter Collingbourne571a3302018-05-21 17:57:19 +0000733 return swapHalfWords(EncValue, Endian == support::little);
Jason W Kimfc5c5222010-12-01 22:46:50 +0000734 }
Peter Smithd16c55d2017-06-06 10:22:49 +0000735 }
Jason W Kimfc5c5222010-12-01 22:46:50 +0000736}
737
Rafael Espindola76287ab2017-06-30 22:47:27 +0000738bool ARMAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
739 const MCFixup &Fixup,
740 const MCValue &Target) {
Jim Grosbache78031a2012-04-30 22:30:43 +0000741 const MCSymbolRefExpr *A = Target.getSymA();
Rafael Espindola49b85482015-11-04 23:00:39 +0000742 const MCSymbol *Sym = A ? &A->getSymbol() : nullptr;
Florian Hahn28a61d62017-06-07 12:58:08 +0000743 const unsigned FixupKind = Fixup.getKind() ;
Rafael Espindola76287ab2017-06-30 22:47:27 +0000744 if ((unsigned)Fixup.getKind() == ARM::fixup_arm_thumb_bl) {
Rafael Espindola49b85482015-11-04 23:00:39 +0000745 assert(Sym && "How did we resolve this?");
746
747 // If the symbol is external the linker will handle it.
748 // FIXME: Should we handle it as an optimization?
Rafael Espindolae61a9022015-11-05 01:10:15 +0000749
750 // If the symbol is out of range, produce a relocation and hope the
751 // linker can handle it. GNU AS produces an error in this case.
Rafael Espindola801b42d2017-06-23 22:52:36 +0000752 if (Sym->isExternal())
Rafael Espindola76287ab2017-06-30 22:47:27 +0000753 return true;
Florian Hahn9afd9d92017-06-07 08:54:47 +0000754 }
755 // Create relocations for unconditional branches to function symbols with
756 // different execution mode in ELF binaries.
757 if (Sym && Sym->isELF()) {
Craig Topper781aa182018-05-05 01:57:00 +0000758 unsigned Type = cast<MCSymbolELF>(Sym)->getType();
Florian Hahn9afd9d92017-06-07 08:54:47 +0000759 if ((Type == ELF::STT_FUNC || Type == ELF::STT_GNU_IFUNC)) {
Florian Hahn9afd9d92017-06-07 08:54:47 +0000760 if (Asm.isThumbFunc(Sym) && (FixupKind == ARM::fixup_arm_uncondbranch))
Rafael Espindola76287ab2017-06-30 22:47:27 +0000761 return true;
Florian Hahn9afd9d92017-06-07 08:54:47 +0000762 if (!Asm.isThumbFunc(Sym) && (FixupKind == ARM::fixup_arm_thumb_br ||
763 FixupKind == ARM::fixup_arm_thumb_bl ||
Florian Hahn5991b5b2017-06-22 15:32:41 +0000764 FixupKind == ARM::fixup_t2_condbranch ||
Florian Hahn9afd9d92017-06-07 08:54:47 +0000765 FixupKind == ARM::fixup_t2_uncondbranch))
Rafael Espindola76287ab2017-06-30 22:47:27 +0000766 return true;
Florian Hahnfca7b832017-06-01 13:50:57 +0000767 }
Logan Chiend5c48aa2014-02-05 14:15:16 +0000768 }
Jim Grosbache78031a2012-04-30 22:30:43 +0000769 // We must always generate a relocation for BL/BLX instructions if we have
770 // a symbol to reference, as the linker relies on knowing the destination
771 // symbol's thumb-ness to get interworking right.
Florian Hahn28a61d62017-06-07 12:58:08 +0000772 if (A && (FixupKind == ARM::fixup_arm_thumb_blx ||
773 FixupKind == ARM::fixup_arm_blx ||
774 FixupKind == ARM::fixup_arm_uncondbl ||
775 FixupKind == ARM::fixup_arm_condbl))
Rafael Espindola76287ab2017-06-30 22:47:27 +0000776 return true;
777 return false;
Jim Grosbache78031a2012-04-30 22:30:43 +0000778}
779
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000780/// getFixupKindNumBytes - The number of bytes the fixup may change.
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000781static unsigned getFixupKindNumBytes(unsigned Kind) {
Jim Grosbach90987142010-11-09 01:37:15 +0000782 switch (Kind) {
Jim Grosbach9e199462010-12-06 23:57:07 +0000783 default:
784 llvm_unreachable("Unknown fixup kind!");
Bill Wendling8a6449c2010-12-08 01:57:09 +0000785
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000786 case FK_Data_1:
Jim Grosbach78485ad2010-12-10 17:13:40 +0000787 case ARM::fixup_arm_thumb_bcc:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000788 case ARM::fixup_arm_thumb_cp:
Jim Grosbach509dc2a2010-12-14 22:28:03 +0000789 case ARM::fixup_thumb_adr_pcrel_10:
Bill Wendling8a6449c2010-12-08 01:57:09 +0000790 return 1;
791
Jim Grosbach4416dfa2010-12-17 18:39:10 +0000792 case FK_Data_2:
Jim Grosbache119da12010-12-10 18:21:33 +0000793 case ARM::fixup_arm_thumb_br:
Jim Grosbach68b27eb2010-12-09 19:50:12 +0000794 case ARM::fixup_arm_thumb_cb:
James Molloyb876c722016-04-01 09:40:47 +0000795 case ARM::fixup_arm_mod_imm:
Bill Wendlinga7d6aa92010-12-08 23:01:43 +0000796 return 2;
797
Jim Grosbach8648c102011-12-19 23:06:24 +0000798 case ARM::fixup_arm_pcrel_10_unscaled:
Jim Grosbach9e199462010-12-06 23:57:07 +0000799 case ARM::fixup_arm_ldst_pcrel_12:
800 case ARM::fixup_arm_pcrel_10:
Oliver Stannard65b85382016-01-25 10:26:26 +0000801 case ARM::fixup_arm_pcrel_9:
Jim Grosbach9e199462010-12-06 23:57:07 +0000802 case ARM::fixup_arm_adr_pcrel_12:
James Molloyfb5cd602012-03-30 09:15:32 +0000803 case ARM::fixup_arm_uncondbl:
804 case ARM::fixup_arm_condbl:
Jim Grosbach7b811d32012-02-27 21:36:23 +0000805 case ARM::fixup_arm_blx:
Jason W Kimd2e2f562011-02-04 19:47:15 +0000806 case ARM::fixup_arm_condbranch:
807 case ARM::fixup_arm_uncondbranch:
Jim Grosbach9e199462010-12-06 23:57:07 +0000808 return 3;
Bill Wendling8a6449c2010-12-08 01:57:09 +0000809
810 case FK_Data_4:
Owen Anderson3e6ee1d2010-12-09 01:51:07 +0000811 case ARM::fixup_t2_ldst_pcrel_12:
Owen Anderson578074b2010-12-13 19:31:11 +0000812 case ARM::fixup_t2_condbranch:
813 case ARM::fixup_t2_uncondbranch:
Owen Anderson0f7142d2010-12-08 00:18:36 +0000814 case ARM::fixup_t2_pcrel_10:
Oliver Stannard65b85382016-01-25 10:26:26 +0000815 case ARM::fixup_t2_pcrel_9:
Owen Anderson6d375e52010-12-14 00:36:49 +0000816 case ARM::fixup_t2_adr_pcrel_12:
Jim Grosbach9e199462010-12-06 23:57:07 +0000817 case ARM::fixup_arm_thumb_bl:
Bill Wendling3392bfc2010-12-09 00:39:08 +0000818 case ARM::fixup_arm_thumb_blx:
Evan Chengd4a5c052011-01-14 02:38:49 +0000819 case ARM::fixup_arm_movt_hi16:
820 case ARM::fixup_arm_movw_lo16:
Evan Chengd4a5c052011-01-14 02:38:49 +0000821 case ARM::fixup_t2_movt_hi16:
822 case ARM::fixup_t2_movw_lo16:
Peter Smithadde6672017-06-05 09:37:12 +0000823 case ARM::fixup_t2_so_imm:
Jim Grosbach9e199462010-12-06 23:57:07 +0000824 return 4;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000825
Saleem Abdulrasoolfc6b85b2014-05-08 01:35:57 +0000826 case FK_SecRel_2:
827 return 2;
Saleem Abdulrasool729c7a02014-05-04 23:13:15 +0000828 case FK_SecRel_4:
829 return 4;
Jim Grosbach90987142010-11-09 01:37:15 +0000830 }
831}
832
Christian Pirker2a111602014-03-28 14:35:30 +0000833/// getFixupKindContainerSizeBytes - The number of bytes of the
834/// container involved in big endian.
835static unsigned getFixupKindContainerSizeBytes(unsigned Kind) {
836 switch (Kind) {
837 default:
838 llvm_unreachable("Unknown fixup kind!");
839
840 case FK_Data_1:
841 return 1;
842 case FK_Data_2:
843 return 2;
844 case FK_Data_4:
845 return 4;
846
847 case ARM::fixup_arm_thumb_bcc:
848 case ARM::fixup_arm_thumb_cp:
849 case ARM::fixup_thumb_adr_pcrel_10:
850 case ARM::fixup_arm_thumb_br:
851 case ARM::fixup_arm_thumb_cb:
852 // Instruction size is 2 bytes.
853 return 2;
854
855 case ARM::fixup_arm_pcrel_10_unscaled:
856 case ARM::fixup_arm_ldst_pcrel_12:
857 case ARM::fixup_arm_pcrel_10:
858 case ARM::fixup_arm_adr_pcrel_12:
859 case ARM::fixup_arm_uncondbl:
860 case ARM::fixup_arm_condbl:
861 case ARM::fixup_arm_blx:
862 case ARM::fixup_arm_condbranch:
863 case ARM::fixup_arm_uncondbranch:
864 case ARM::fixup_t2_ldst_pcrel_12:
865 case ARM::fixup_t2_condbranch:
866 case ARM::fixup_t2_uncondbranch:
867 case ARM::fixup_t2_pcrel_10:
868 case ARM::fixup_t2_adr_pcrel_12:
869 case ARM::fixup_arm_thumb_bl:
870 case ARM::fixup_arm_thumb_blx:
871 case ARM::fixup_arm_movt_hi16:
872 case ARM::fixup_arm_movw_lo16:
Christian Pirker2a111602014-03-28 14:35:30 +0000873 case ARM::fixup_t2_movt_hi16:
874 case ARM::fixup_t2_movw_lo16:
James Molloyb876c722016-04-01 09:40:47 +0000875 case ARM::fixup_arm_mod_imm:
Peter Smithadde6672017-06-05 09:37:12 +0000876 case ARM::fixup_t2_so_imm:
Christian Pirker2a111602014-03-28 14:35:30 +0000877 // Instruction size is 4 bytes.
878 return 4;
879 }
880}
881
Rafael Espindola801b42d2017-06-23 22:52:36 +0000882void ARMAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
883 const MCValue &Target,
884 MutableArrayRef<char> Data, uint64_t Value,
Rafael Espindola1beb7022017-07-11 23:18:25 +0000885 bool IsResolved) const {
Jim Grosbach9d6d77a2010-11-11 18:04:49 +0000886 unsigned NumBytes = getFixupKindNumBytes(Fixup.getKind());
Rafael Espindolaf3512922017-06-24 00:26:57 +0000887 MCContext &Ctx = Asm.getContext();
Peter Collingbourneec8236e2018-05-16 00:21:47 +0000888 Value = adjustFixupValue(Asm, Fixup, Target, Value, IsResolved, Ctx);
Joe Abbey8e72eb72014-09-16 09:18:23 +0000889 if (!Value)
890 return; // Doesn't change encoding.
Jim Grosbach90987142010-11-09 01:37:15 +0000891
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000892 unsigned Offset = Fixup.getOffset();
Rafael Espindola88d9e372017-06-21 23:06:53 +0000893 assert(Offset + NumBytes <= Data.size() && "Invalid fixup offset!");
Bill Wendlingf09c44c2010-12-07 23:11:00 +0000894
Christian Pirker2a111602014-03-28 14:35:30 +0000895 // Used to point to big endian bytes.
896 unsigned FullSizeBytes;
Peter Collingbourne571a3302018-05-21 17:57:19 +0000897 if (Endian == support::big) {
Christian Pirker2a111602014-03-28 14:35:30 +0000898 FullSizeBytes = getFixupKindContainerSizeBytes(Fixup.getKind());
Rafael Espindola88d9e372017-06-21 23:06:53 +0000899 assert((Offset + FullSizeBytes) <= Data.size() && "Invalid fixup size!");
Christian Pirker875629f2014-05-20 09:24:37 +0000900 assert(NumBytes <= FullSizeBytes && "Invalid fixup size!");
901 }
Christian Pirker2a111602014-03-28 14:35:30 +0000902
Benjamin Kramer07ea85a2012-11-24 14:36:43 +0000903 // For each byte of the fragment that the fixup touches, mask in the bits from
904 // the fixup value. The Value has been "split up" into the appropriate
905 // bitfields above.
Christian Pirker2a111602014-03-28 14:35:30 +0000906 for (unsigned i = 0; i != NumBytes; ++i) {
Peter Collingbourne571a3302018-05-21 17:57:19 +0000907 unsigned Idx = Endian == support::little ? i : (FullSizeBytes - 1 - i);
Christian Pirker2a111602014-03-28 14:35:30 +0000908 Data[Offset + Idx] |= uint8_t((Value >> (i * 8)) & 0xff);
909 }
Jason W Kimb3212452010-09-30 02:17:26 +0000910}
Bill Wendling721724e2010-12-07 23:05:20 +0000911
Tim Northoverf8e47e42015-10-28 22:56:36 +0000912namespace CU {
913
Adrian Prantl5f8f34e42018-05-01 15:54:18 +0000914/// Compact unwind encoding values.
Tim Northoverf8e47e42015-10-28 22:56:36 +0000915enum CompactUnwindEncodings {
916 UNWIND_ARM_MODE_MASK = 0x0F000000,
917 UNWIND_ARM_MODE_FRAME = 0x01000000,
918 UNWIND_ARM_MODE_FRAME_D = 0x02000000,
919 UNWIND_ARM_MODE_DWARF = 0x04000000,
920
921 UNWIND_ARM_FRAME_STACK_ADJUST_MASK = 0x00C00000,
922
923 UNWIND_ARM_FRAME_FIRST_PUSH_R4 = 0x00000001,
924 UNWIND_ARM_FRAME_FIRST_PUSH_R5 = 0x00000002,
925 UNWIND_ARM_FRAME_FIRST_PUSH_R6 = 0x00000004,
926
927 UNWIND_ARM_FRAME_SECOND_PUSH_R8 = 0x00000008,
928 UNWIND_ARM_FRAME_SECOND_PUSH_R9 = 0x00000010,
929 UNWIND_ARM_FRAME_SECOND_PUSH_R10 = 0x00000020,
930 UNWIND_ARM_FRAME_SECOND_PUSH_R11 = 0x00000040,
931 UNWIND_ARM_FRAME_SECOND_PUSH_R12 = 0x00000080,
932
933 UNWIND_ARM_FRAME_D_REG_COUNT_MASK = 0x00000F00,
934
935 UNWIND_ARM_DWARF_SECTION_OFFSET = 0x00FFFFFF
936};
937
938} // end CU namespace
939
940/// Generate compact unwind encoding for the function based on the CFI
941/// instructions. If the CFI instructions describe a frame that cannot be
942/// encoded in compact unwind, the method returns UNWIND_ARM_MODE_DWARF which
943/// tells the runtime to fallback and unwind using dwarf.
944uint32_t ARMAsmBackendDarwin::generateCompactUnwindEncoding(
945 ArrayRef<MCCFIInstruction> Instrs) const {
946 DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs() << "generateCU()\n");
947 // Only armv7k uses CFI based unwinding.
948 if (Subtype != MachO::CPU_SUBTYPE_ARM_V7K)
949 return 0;
950 // No .cfi directives means no frame.
951 if (Instrs.empty())
952 return 0;
953 // Start off assuming CFA is at SP+0.
954 int CFARegister = ARM::SP;
955 int CFARegisterOffset = 0;
956 // Mark savable registers as initially unsaved
957 DenseMap<unsigned, int> RegOffsets;
958 int FloatRegCount = 0;
959 // Process each .cfi directive and build up compact unwind info.
960 for (size_t i = 0, e = Instrs.size(); i != e; ++i) {
961 int Reg;
962 const MCCFIInstruction &Inst = Instrs[i];
963 switch (Inst.getOperation()) {
964 case MCCFIInstruction::OpDefCfa: // DW_CFA_def_cfa
965 CFARegisterOffset = -Inst.getOffset();
966 CFARegister = MRI.getLLVMRegNum(Inst.getRegister(), true);
967 break;
968 case MCCFIInstruction::OpDefCfaOffset: // DW_CFA_def_cfa_offset
969 CFARegisterOffset = -Inst.getOffset();
970 break;
971 case MCCFIInstruction::OpDefCfaRegister: // DW_CFA_def_cfa_register
972 CFARegister = MRI.getLLVMRegNum(Inst.getRegister(), true);
973 break;
974 case MCCFIInstruction::OpOffset: // DW_CFA_offset
975 Reg = MRI.getLLVMRegNum(Inst.getRegister(), true);
976 if (ARMMCRegisterClasses[ARM::GPRRegClassID].contains(Reg))
977 RegOffsets[Reg] = Inst.getOffset();
978 else if (ARMMCRegisterClasses[ARM::DPRRegClassID].contains(Reg)) {
979 RegOffsets[Reg] = Inst.getOffset();
980 ++FloatRegCount;
981 } else {
982 DEBUG_WITH_TYPE("compact-unwind",
983 llvm::dbgs() << ".cfi_offset on unknown register="
984 << Inst.getRegister() << "\n");
985 return CU::UNWIND_ARM_MODE_DWARF;
986 }
987 break;
988 case MCCFIInstruction::OpRelOffset: // DW_CFA_advance_loc
989 // Ignore
990 break;
991 default:
992 // Directive not convertable to compact unwind, bail out.
993 DEBUG_WITH_TYPE("compact-unwind",
994 llvm::dbgs()
995 << "CFI directive not compatiable with comact "
996 "unwind encoding, opcode=" << Inst.getOperation()
997 << "\n");
998 return CU::UNWIND_ARM_MODE_DWARF;
999 break;
1000 }
1001 }
1002
1003 // If no frame set up, return no unwind info.
1004 if ((CFARegister == ARM::SP) && (CFARegisterOffset == 0))
1005 return 0;
1006
1007 // Verify standard frame (lr/r7) was used.
1008 if (CFARegister != ARM::R7) {
1009 DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs() << "frame register is "
1010 << CFARegister
1011 << " instead of r7\n");
1012 return CU::UNWIND_ARM_MODE_DWARF;
1013 }
1014 int StackAdjust = CFARegisterOffset - 8;
1015 if (RegOffsets.lookup(ARM::LR) != (-4 - StackAdjust)) {
1016 DEBUG_WITH_TYPE("compact-unwind",
1017 llvm::dbgs()
1018 << "LR not saved as standard frame, StackAdjust="
1019 << StackAdjust
1020 << ", CFARegisterOffset=" << CFARegisterOffset
1021 << ", lr save at offset=" << RegOffsets[14] << "\n");
1022 return CU::UNWIND_ARM_MODE_DWARF;
1023 }
1024 if (RegOffsets.lookup(ARM::R7) != (-8 - StackAdjust)) {
1025 DEBUG_WITH_TYPE("compact-unwind",
1026 llvm::dbgs() << "r7 not saved as standard frame\n");
1027 return CU::UNWIND_ARM_MODE_DWARF;
1028 }
1029 uint32_t CompactUnwindEncoding = CU::UNWIND_ARM_MODE_FRAME;
1030
1031 // If var-args are used, there may be a stack adjust required.
1032 switch (StackAdjust) {
1033 case 0:
1034 break;
1035 case 4:
1036 CompactUnwindEncoding |= 0x00400000;
1037 break;
1038 case 8:
1039 CompactUnwindEncoding |= 0x00800000;
1040 break;
1041 case 12:
1042 CompactUnwindEncoding |= 0x00C00000;
1043 break;
1044 default:
1045 DEBUG_WITH_TYPE("compact-unwind", llvm::dbgs()
1046 << ".cfi_def_cfa stack adjust ("
1047 << StackAdjust << ") out of range\n");
1048 return CU::UNWIND_ARM_MODE_DWARF;
1049 }
1050
1051 // If r6 is saved, it must be right below r7.
1052 static struct {
1053 unsigned Reg;
1054 unsigned Encoding;
1055 } GPRCSRegs[] = {{ARM::R6, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R6},
1056 {ARM::R5, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R5},
1057 {ARM::R4, CU::UNWIND_ARM_FRAME_FIRST_PUSH_R4},
1058 {ARM::R12, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R12},
1059 {ARM::R11, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R11},
1060 {ARM::R10, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R10},
1061 {ARM::R9, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R9},
1062 {ARM::R8, CU::UNWIND_ARM_FRAME_SECOND_PUSH_R8}};
1063
1064 int CurOffset = -8 - StackAdjust;
1065 for (auto CSReg : GPRCSRegs) {
1066 auto Offset = RegOffsets.find(CSReg.Reg);
1067 if (Offset == RegOffsets.end())
1068 continue;
1069
1070 int RegOffset = Offset->second;
1071 if (RegOffset != CurOffset - 4) {
1072 DEBUG_WITH_TYPE("compact-unwind",
1073 llvm::dbgs() << MRI.getName(CSReg.Reg) << " saved at "
1074 << RegOffset << " but only supported at "
1075 << CurOffset << "\n");
1076 return CU::UNWIND_ARM_MODE_DWARF;
1077 }
1078 CompactUnwindEncoding |= CSReg.Encoding;
1079 CurOffset -= 4;
1080 }
1081
1082 // If no floats saved, we are done.
1083 if (FloatRegCount == 0)
1084 return CompactUnwindEncoding;
1085
1086 // Switch mode to include D register saving.
1087 CompactUnwindEncoding &= ~CU::UNWIND_ARM_MODE_MASK;
1088 CompactUnwindEncoding |= CU::UNWIND_ARM_MODE_FRAME_D;
1089
1090 // FIXME: supporting more than 4 saved D-registers compactly would be trivial,
1091 // but needs coordination with the linker and libunwind.
1092 if (FloatRegCount > 4) {
1093 DEBUG_WITH_TYPE("compact-unwind",
1094 llvm::dbgs() << "unsupported number of D registers saved ("
1095 << FloatRegCount << ")\n");
1096 return CU::UNWIND_ARM_MODE_DWARF;
1097 }
1098
1099 // Floating point registers must either be saved sequentially, or we defer to
1100 // DWARF. No gaps allowed here so check that each saved d-register is
1101 // precisely where it should be.
1102 static unsigned FPRCSRegs[] = { ARM::D8, ARM::D10, ARM::D12, ARM::D14 };
1103 for (int Idx = FloatRegCount - 1; Idx >= 0; --Idx) {
1104 auto Offset = RegOffsets.find(FPRCSRegs[Idx]);
1105 if (Offset == RegOffsets.end()) {
1106 DEBUG_WITH_TYPE("compact-unwind",
1107 llvm::dbgs() << FloatRegCount << " D-regs saved, but "
1108 << MRI.getName(FPRCSRegs[Idx])
1109 << " not saved\n");
1110 return CU::UNWIND_ARM_MODE_DWARF;
1111 } else if (Offset->second != CurOffset - 8) {
1112 DEBUG_WITH_TYPE("compact-unwind",
1113 llvm::dbgs() << FloatRegCount << " D-regs saved, but "
1114 << MRI.getName(FPRCSRegs[Idx])
1115 << " saved at " << Offset->second
1116 << ", expected at " << CurOffset - 8
1117 << "\n");
1118 return CU::UNWIND_ARM_MODE_DWARF;
1119 }
1120 CurOffset -= 8;
1121 }
1122
1123 return CompactUnwindEncoding | ((FloatRegCount - 1) << 8);
1124}
1125
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001126static MachO::CPUSubTypeARM getMachOSubTypeFromArch(StringRef Arch) {
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001127 ARM::ArchKind AK = ARM::parseArch(Arch);
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001128 switch (AK) {
1129 default:
1130 return MachO::CPU_SUBTYPE_ARM_V7;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001131 case ARM::ArchKind::ARMV4T:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001132 return MachO::CPU_SUBTYPE_ARM_V4T;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001133 case ARM::ArchKind::ARMV5T:
1134 case ARM::ArchKind::ARMV5TE:
1135 case ARM::ArchKind::ARMV5TEJ:
Artyom Skrobov2c2f3782015-11-12 15:51:41 +00001136 return MachO::CPU_SUBTYPE_ARM_V5;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001137 case ARM::ArchKind::ARMV6:
1138 case ARM::ArchKind::ARMV6K:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001139 return MachO::CPU_SUBTYPE_ARM_V6;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001140 case ARM::ArchKind::ARMV7A:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001141 return MachO::CPU_SUBTYPE_ARM_V7;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001142 case ARM::ArchKind::ARMV7S:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001143 return MachO::CPU_SUBTYPE_ARM_V7S;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001144 case ARM::ArchKind::ARMV7K:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001145 return MachO::CPU_SUBTYPE_ARM_V7K;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001146 case ARM::ArchKind::ARMV6M:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001147 return MachO::CPU_SUBTYPE_ARM_V6M;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001148 case ARM::ArchKind::ARMV7M:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001149 return MachO::CPU_SUBTYPE_ARM_V7M;
Florian Hahn67ddd1d2017-07-27 16:27:56 +00001150 case ARM::ArchKind::ARMV7EM:
Vedant Kumar366dd9fd2015-08-21 21:52:48 +00001151 return MachO::CPU_SUBTYPE_ARM_V7EM;
1152 }
1153}
1154
Peter Collingbourne76d463a2018-05-16 00:21:31 +00001155static MCAsmBackend *createARMAsmBackend(const Target &T,
1156 const MCSubtargetInfo &STI,
1157 const MCRegisterInfo &MRI,
1158 const MCTargetOptions &Options,
Peter Collingbourne571a3302018-05-21 17:57:19 +00001159 support::endianness Endian) {
Alex Bradbury46db78b2018-01-03 13:46:21 +00001160 const Triple &TheTriple = STI.getTargetTriple();
Daniel Sanders50f17232015-09-15 16:17:27 +00001161 switch (TheTriple.getObjectFormat()) {
Joe Abbey8e72eb72014-09-16 09:18:23 +00001162 default:
1163 llvm_unreachable("unsupported object format");
Daniel Sanders50f17232015-09-15 16:17:27 +00001164 case Triple::MachO: {
1165 MachO::CPUSubTypeARM CS = getMachOSubTypeFromArch(TheTriple.getArchName());
Alex Bradbury46db78b2018-01-03 13:46:21 +00001166 return new ARMAsmBackendDarwin(T, STI, MRI, CS);
Owen Anderson975ddf82011-04-01 21:07:39 +00001167 }
Daniel Sanders50f17232015-09-15 16:17:27 +00001168 case Triple::COFF:
1169 assert(TheTriple.isOSWindows() && "non-Windows ARM COFF is not supported");
Alex Bradbury46db78b2018-01-03 13:46:21 +00001170 return new ARMAsmBackendWinCOFF(T, STI);
Daniel Sanders50f17232015-09-15 16:17:27 +00001171 case Triple::ELF:
1172 assert(TheTriple.isOSBinFormatELF() && "using ELF for non-ELF target");
1173 uint8_t OSABI = MCELFObjectTargetWriter::getOSABI(TheTriple.getOS());
Peter Collingbourne571a3302018-05-21 17:57:19 +00001174 return new ARMAsmBackendELF(T, STI, OSABI, Endian);
Saleem Abdulrasool84b952b2014-04-27 03:48:22 +00001175 }
Jason W Kimb3212452010-09-30 02:17:26 +00001176}
Christian Pirker2a111602014-03-28 14:35:30 +00001177
Christian Pirkerdc9ff752014-04-01 15:19:30 +00001178MCAsmBackend *llvm::createARMLEAsmBackend(const Target &T,
Alex Bradburyb22f7512018-01-03 08:53:05 +00001179 const MCSubtargetInfo &STI,
Christian Pirker2a111602014-03-28 14:35:30 +00001180 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +00001181 const MCTargetOptions &Options) {
Peter Collingbourne571a3302018-05-21 17:57:19 +00001182 return createARMAsmBackend(T, STI, MRI, Options, support::little);
Christian Pirker2a111602014-03-28 14:35:30 +00001183}
1184
Christian Pirkerdc9ff752014-04-01 15:19:30 +00001185MCAsmBackend *llvm::createARMBEAsmBackend(const Target &T,
Alex Bradburyb22f7512018-01-03 08:53:05 +00001186 const MCSubtargetInfo &STI,
Christian Pirker2a111602014-03-28 14:35:30 +00001187 const MCRegisterInfo &MRI,
Joel Jones373d7d32016-07-25 17:18:28 +00001188 const MCTargetOptions &Options) {
Peter Collingbourne571a3302018-05-21 17:57:19 +00001189 return createARMAsmBackend(T, STI, MRI, Options, support::big);
Christian Pirker2a111602014-03-28 14:35:30 +00001190}