blob: 982ba384deb47a6146d3e8b6a67c39432e84192d [file] [log] [blame]
Tim Northover5cc3dc82012-12-07 16:50:23 +00001//===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
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//
10// This file assembles .s files and emits ARM ELF .o object files. Different
11// from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
12// delimit regions of data and code.
13//
14//===----------------------------------------------------------------------===//
15
Logan Chien439e8f92013-12-11 17:16:25 +000016#include "ARMArchName.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000017#include "ARMFPUName.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000018#include "ARMRegisterInfo.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000019#include "ARMUnwindOpAsm.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000020#include "llvm/ADT/StringExtras.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000021#include "llvm/ADT/Twine.h"
22#include "llvm/MC/MCAsmBackend.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000023#include "llvm/MC/MCAsmInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000024#include "llvm/MC/MCAssembler.h"
25#include "llvm/MC/MCCodeEmitter.h"
26#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000027#include "llvm/MC/MCELF.h"
28#include "llvm/MC/MCELFStreamer.h"
29#include "llvm/MC/MCELFSymbolFlags.h"
30#include "llvm/MC/MCExpr.h"
31#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000032#include "llvm/MC/MCInstPrinter.h"
Rafael Espindola4c6f6132014-04-27 17:10:46 +000033#include "llvm/MC/MCObjectFileInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000034#include "llvm/MC/MCObjectStreamer.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000035#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000036#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000037#include "llvm/MC/MCSectionELF.h"
38#include "llvm/MC/MCStreamer.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000039#include "llvm/MC/MCSymbol.h"
40#include "llvm/MC/MCValue.h"
Saleem Abdulrasool278a9f42014-01-19 08:25:27 +000041#include "llvm/Support/ARMBuildAttributes.h"
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000042#include "llvm/Support/ARMEHABI.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/ELF.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000045#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000046#include "llvm/Support/LEB128.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000047#include "llvm/Support/raw_ostream.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000048#include <algorithm>
Tim Northover5cc3dc82012-12-07 16:50:23 +000049
50using namespace llvm;
51
Logan Chiend8bb4b72013-04-16 12:02:21 +000052static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000053 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
54 "Invalid personality index");
Logan Chiend8bb4b72013-04-16 12:02:21 +000055 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
56}
57
Logan Chien8cbb80d2013-10-28 17:51:12 +000058static const char *GetFPUName(unsigned ID) {
59 switch (ID) {
60 default:
61 llvm_unreachable("Unknown FPU kind");
62 break;
63#define ARM_FPU_NAME(NAME, ID) case ARM::ID: return NAME;
64#include "ARMFPUName.def"
65 }
Craig Topper062a2ba2014-04-25 05:30:21 +000066 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +000067}
68
Logan Chien439e8f92013-12-11 17:16:25 +000069static const char *GetArchName(unsigned ID) {
70 switch (ID) {
71 default:
72 llvm_unreachable("Unknown ARCH kind");
73 break;
74#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
75 case ARM::ID: return NAME;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +000076#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +000077#include "ARMArchName.def"
78 }
Craig Topper062a2ba2014-04-25 05:30:21 +000079 return nullptr;
Logan Chien439e8f92013-12-11 17:16:25 +000080}
81
82static const char *GetArchDefaultCPUName(unsigned ID) {
83 switch (ID) {
84 default:
85 llvm_unreachable("Unknown ARCH kind");
86 break;
87#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
88 case ARM::ID: return DEFAULT_CPU_NAME;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +000089#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +000090#include "ARMArchName.def"
91 }
Craig Topper062a2ba2014-04-25 05:30:21 +000092 return nullptr;
Logan Chien439e8f92013-12-11 17:16:25 +000093}
94
95static unsigned GetArchDefaultCPUArch(unsigned ID) {
96 switch (ID) {
97 default:
98 llvm_unreachable("Unknown ARCH kind");
99 break;
100#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
101 case ARM::ID: return ARMBuildAttrs::DEFAULT_CPU_ARCH;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +0000102#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +0000103#include "ARMArchName.def"
104 }
105 return 0;
106}
107
Tim Northover5cc3dc82012-12-07 16:50:23 +0000108namespace {
109
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000110class ARMELFStreamer;
111
112class ARMTargetAsmStreamer : public ARMTargetStreamer {
113 formatted_raw_ostream &OS;
114 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000115 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000116
Craig Topperca7e3e52014-03-10 03:19:03 +0000117 void emitFnStart() override;
118 void emitFnEnd() override;
119 void emitCantUnwind() override;
120 void emitPersonality(const MCSymbol *Personality) override;
121 void emitPersonalityIndex(unsigned Index) override;
122 void emitHandlerData() override;
123 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
124 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
125 void emitPad(int64_t Offset) override;
126 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
127 bool isVector) override;
128 void emitUnwindRaw(int64_t Offset,
129 const SmallVectorImpl<uint8_t> &Opcodes) override;
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000130
Craig Topperca7e3e52014-03-10 03:19:03 +0000131 void switchVendor(StringRef Vendor) override;
132 void emitAttribute(unsigned Attribute, unsigned Value) override;
133 void emitTextAttribute(unsigned Attribute, StringRef String) override;
134 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
135 StringRef StrinValue) override;
136 void emitArch(unsigned Arch) override;
137 void emitObjectArch(unsigned Arch) override;
138 void emitFPU(unsigned FPU) override;
139 void emitInst(uint32_t Inst, char Suffix = '\0') override;
140 void finishAttributeSection() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000141
Craig Topperca7e3e52014-03-10 03:19:03 +0000142 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000143
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000144public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000145 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
146 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000147};
148
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000149ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
150 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000151 MCInstPrinter &InstPrinter,
152 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000153 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
154 IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000155void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
156void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
157void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
158void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
159 OS << "\t.personality " << Personality->getName() << '\n';
160}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000161void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
162 OS << "\t.personalityindex " << Index << '\n';
163}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000164void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
165void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
166 int64_t Offset) {
167 OS << "\t.setfp\t";
168 InstPrinter.printRegName(OS, FpReg);
169 OS << ", ";
170 InstPrinter.printRegName(OS, SpReg);
171 if (Offset)
172 OS << ", #" << Offset;
173 OS << '\n';
174}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000175void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
176 assert((Reg != ARM::SP && Reg != ARM::PC) &&
177 "the operand of .movsp cannot be either sp or pc");
178
179 OS << "\t.movsp\t";
180 InstPrinter.printRegName(OS, Reg);
181 if (Offset)
182 OS << ", #" << Offset;
183 OS << '\n';
184}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000185void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
186 OS << "\t.pad\t#" << Offset << '\n';
187}
188void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
189 bool isVector) {
190 assert(RegList.size() && "RegList should not be empty");
191 if (isVector)
192 OS << "\t.vsave\t{";
193 else
194 OS << "\t.save\t{";
195
196 InstPrinter.printRegName(OS, RegList[0]);
197
198 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
199 OS << ", ";
200 InstPrinter.printRegName(OS, RegList[i]);
201 }
202
203 OS << "}\n";
204}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000205void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
206}
207void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000208 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
209 if (IsVerboseAsm) {
210 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
211 if (!Name.empty())
212 OS << "\t@ " << Name;
213 }
214 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000215}
216void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
217 StringRef String) {
218 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000219 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000220 OS << "\t.cpu\t" << String.lower();
221 break;
222 default:
223 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000224 if (IsVerboseAsm) {
225 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
226 if (!Name.empty())
227 OS << "\t@ " << Name;
228 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000229 break;
230 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000231 OS << "\n";
232}
233void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
234 unsigned IntValue,
235 StringRef StringValue) {
236 switch (Attribute) {
237 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
238 case ARMBuildAttrs::compatibility:
239 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
240 if (!StringValue.empty())
241 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000242 if (IsVerboseAsm)
243 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000244 break;
245 }
246 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000247}
Logan Chien439e8f92013-12-11 17:16:25 +0000248void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
249 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
250}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000251void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
252 OS << "\t.object_arch\t" << GetArchName(Arch) << '\n';
253}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000254void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
255 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
256}
257void ARMTargetAsmStreamer::finishAttributeSection() {
258}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000259void
260ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
261 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
262}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000263
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000264void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
265 OS << "\t.inst";
266 if (Suffix)
267 OS << "." << Suffix;
268 OS << "\t0x" << utohexstr(Inst) << "\n";
269}
270
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000271void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
272 const SmallVectorImpl<uint8_t> &Opcodes) {
273 OS << "\t.unwind_raw " << Offset;
274 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
275 OCE = Opcodes.end();
276 OCI != OCE; ++OCI)
277 OS << ", 0x" << utohexstr(*OCI);
278 OS << '\n';
279}
280
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000281class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000282private:
283 // This structure holds all attributes, accounting for
284 // their string/numeric value, so we can later emmit them
285 // in declaration order, keeping all in the same vector
286 struct AttributeItem {
287 enum {
288 HiddenAttribute = 0,
289 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000290 TextAttribute,
291 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000292 } Type;
293 unsigned Tag;
294 unsigned IntValue;
295 StringRef StringValue;
296
297 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
298 return (LHS.Tag < RHS.Tag);
299 }
300 };
301
302 StringRef CurrentVendor;
303 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000304 unsigned Arch;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000305 unsigned EmittedArch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000306 SmallVector<AttributeItem, 64> Contents;
307
308 const MCSection *AttributeSection;
309
Logan Chien8cbb80d2013-10-28 17:51:12 +0000310 AttributeItem *getAttributeItem(unsigned Attribute) {
311 for (size_t i = 0; i < Contents.size(); ++i)
312 if (Contents[i].Tag == Attribute)
313 return &Contents[i];
Craig Topper062a2ba2014-04-25 05:30:21 +0000314 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000315 }
316
317 void setAttributeItem(unsigned Attribute, unsigned Value,
318 bool OverwriteExisting) {
319 // Look for existing attribute item
320 if (AttributeItem *Item = getAttributeItem(Attribute)) {
321 if (!OverwriteExisting)
322 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000323 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000324 Item->IntValue = Value;
325 return;
326 }
327
328 // Create new attribute item
329 AttributeItem Item = {
330 AttributeItem::NumericAttribute,
331 Attribute,
332 Value,
333 StringRef("")
334 };
335 Contents.push_back(Item);
336 }
337
338 void setAttributeItem(unsigned Attribute, StringRef Value,
339 bool OverwriteExisting) {
340 // Look for existing attribute item
341 if (AttributeItem *Item = getAttributeItem(Attribute)) {
342 if (!OverwriteExisting)
343 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000344 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000345 Item->StringValue = Value;
346 return;
347 }
348
349 // Create new attribute item
350 AttributeItem Item = {
351 AttributeItem::TextAttribute,
352 Attribute,
353 0,
354 Value
355 };
356 Contents.push_back(Item);
357 }
358
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000359 void setAttributeItems(unsigned Attribute, unsigned IntValue,
360 StringRef StringValue, bool OverwriteExisting) {
361 // Look for existing attribute item
362 if (AttributeItem *Item = getAttributeItem(Attribute)) {
363 if (!OverwriteExisting)
364 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000365 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000366 Item->IntValue = IntValue;
367 Item->StringValue = StringValue;
368 return;
369 }
370
371 // Create new attribute item
372 AttributeItem Item = {
373 AttributeItem::NumericAndTextAttributes,
374 Attribute,
375 IntValue,
376 StringValue
377 };
378 Contents.push_back(Item);
379 }
380
Logan Chien439e8f92013-12-11 17:16:25 +0000381 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000382 void emitFPUDefaultAttributes();
383
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000384 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000385
Craig Topperca7e3e52014-03-10 03:19:03 +0000386 void emitFnStart() override;
387 void emitFnEnd() override;
388 void emitCantUnwind() override;
389 void emitPersonality(const MCSymbol *Personality) override;
390 void emitPersonalityIndex(unsigned Index) override;
391 void emitHandlerData() override;
392 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
393 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
394 void emitPad(int64_t Offset) override;
395 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
396 bool isVector) override;
397 void emitUnwindRaw(int64_t Offset,
398 const SmallVectorImpl<uint8_t> &Opcodes) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000399
Craig Topperca7e3e52014-03-10 03:19:03 +0000400 void switchVendor(StringRef Vendor) override;
401 void emitAttribute(unsigned Attribute, unsigned Value) override;
402 void emitTextAttribute(unsigned Attribute, StringRef String) override;
403 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
404 StringRef StringValue) override;
405 void emitArch(unsigned Arch) override;
406 void emitObjectArch(unsigned Arch) override;
407 void emitFPU(unsigned FPU) override;
408 void emitInst(uint32_t Inst, char Suffix = '\0') override;
409 void finishAttributeSection() override;
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000410 void emitLabel(MCSymbol *Symbol) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000411
Craig Topperca7e3e52014-03-10 03:19:03 +0000412 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000413
Logan Chien8cbb80d2013-10-28 17:51:12 +0000414 size_t calculateContentSize() const;
415
416public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000417 ARMTargetELFStreamer(MCStreamer &S)
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000418 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
419 Arch(ARM::INVALID_ARCH), EmittedArch(ARM::INVALID_ARCH),
Craig Topper062a2ba2014-04-25 05:30:21 +0000420 AttributeSection(nullptr) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000421};
422
Tim Northover5cc3dc82012-12-07 16:50:23 +0000423/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
424/// the appropriate points in the object files. These symbols are defined in the
425/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
426///
427/// In brief: $a, $t or $d should be emitted at the start of each contiguous
428/// region of ARM code, Thumb code or data in a section. In practice, this
429/// emission does not rely on explicit assembler directives but on inherent
430/// properties of the directives doing the emission (e.g. ".byte" is data, "add
431/// r0, r0, r0" an instruction).
432///
433/// As a result this system is orthogonal to the DataRegion infrastructure used
434/// by MachO. Beware!
435class ARMELFStreamer : public MCELFStreamer {
436public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000437 friend class ARMTargetELFStreamer;
438
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000439 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
440 MCCodeEmitter *Emitter, bool IsThumb)
441 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
442 MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000443 Reset();
444 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000445
446 ~ARMELFStreamer() {}
447
Craig Topperca7e3e52014-03-10 03:19:03 +0000448 void FinishImpl() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000449
Logan Chien2bcc42c2013-01-30 15:39:04 +0000450 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000451 void emitFnStart();
452 void emitFnEnd();
453 void emitCantUnwind();
454 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000455 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000456 void emitHandlerData();
457 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000458 void emitMovSP(unsigned Reg, int64_t Offset = 0);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000459 void emitPad(int64_t Offset);
460 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000461 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000462
Craig Topperca7e3e52014-03-10 03:19:03 +0000463 void ChangeSection(const MCSection *Section,
464 const MCExpr *Subsection) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000465 // We have to keep track of the mapping symbol state of any sections we
466 // use. Each one should start off as EMS_None, which is provided as the
467 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000468 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000469 LastEMS = LastMappingSymbols.lookup(Section);
470
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000471 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000472 }
473
474 /// This function is the one used to emit instruction data into the ELF
475 /// streamer. We override it to add the appropriate mapping symbol if
476 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000477 void EmitInstruction(const MCInst& Inst,
478 const MCSubtargetInfo &STI) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000479 if (IsThumb)
480 EmitThumbMappingSymbol();
481 else
482 EmitARMMappingSymbol();
483
David Woodhousee6c13e42014-01-28 23:12:42 +0000484 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000485 }
486
Craig Topperd25ff6f2014-03-10 03:22:59 +0000487 void emitInst(uint32_t Inst, char Suffix) {
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000488 unsigned Size;
489 char Buffer[4];
490 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
491
492 switch (Suffix) {
493 case '\0':
494 Size = 4;
495
496 assert(!IsThumb);
497 EmitARMMappingSymbol();
498 for (unsigned II = 0, IE = Size; II != IE; II++) {
499 const unsigned I = LittleEndian ? (Size - II - 1) : II;
500 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
501 }
502
503 break;
504 case 'n':
505 case 'w':
506 Size = (Suffix == 'n' ? 2 : 4);
507
508 assert(IsThumb);
509 EmitThumbMappingSymbol();
510 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
511 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
512 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
513 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
514 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
515 }
516
517 break;
518 default:
519 llvm_unreachable("Invalid Suffix");
520 }
521
522 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
523 }
524
Tim Northover5cc3dc82012-12-07 16:50:23 +0000525 /// This is one of the functions used to emit data into an ELF section, so the
526 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
527 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000528 void EmitBytes(StringRef Data) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000529 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000530 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000531 }
532
533 /// This is one of the functions used to emit data into an ELF section, so the
534 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
535 /// necessary.
Kevin Enderby96918bc2014-04-22 17:27:29 +0000536 void EmitValueImpl(const MCExpr *Value, unsigned Size,
537 const SMLoc &Loc) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000538 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000539 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000540 }
541
Craig Topperca7e3e52014-03-10 03:19:03 +0000542 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000543 MCELFStreamer::EmitAssemblerFlag(Flag);
544
545 switch (Flag) {
546 case MCAF_SyntaxUnified:
547 return; // no-op here.
548 case MCAF_Code16:
549 IsThumb = true;
550 return; // Change to Thumb mode
551 case MCAF_Code32:
552 IsThumb = false;
553 return; // Change to ARM mode
554 case MCAF_Code64:
555 return;
556 case MCAF_SubsectionsViaSymbols:
557 return;
558 }
559 }
560
561private:
562 enum ElfMappingSymbol {
563 EMS_None,
564 EMS_ARM,
565 EMS_Thumb,
566 EMS_Data
567 };
568
569 void EmitDataMappingSymbol() {
570 if (LastEMS == EMS_Data) return;
571 EmitMappingSymbol("$d");
572 LastEMS = EMS_Data;
573 }
574
575 void EmitThumbMappingSymbol() {
576 if (LastEMS == EMS_Thumb) return;
577 EmitMappingSymbol("$t");
578 LastEMS = EMS_Thumb;
579 }
580
581 void EmitARMMappingSymbol() {
582 if (LastEMS == EMS_ARM) return;
583 EmitMappingSymbol("$a");
584 LastEMS = EMS_ARM;
585 }
586
587 void EmitMappingSymbol(StringRef Name) {
588 MCSymbol *Start = getContext().CreateTempSymbol();
589 EmitLabel(Start);
590
Chandler Carruth1d94e932012-12-08 03:10:14 +0000591 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000592 getContext().GetOrCreateSymbol(Name + "." +
593 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000594
595 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
596 MCELF::SetType(SD, ELF::STT_NOTYPE);
597 MCELF::SetBinding(SD, ELF::STB_LOCAL);
598 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000599 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000600
601 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
602 Symbol->setVariableValue(Value);
603 }
604
Craig Topperca7e3e52014-03-10 03:19:03 +0000605 void EmitThumbFunc(MCSymbol *Func) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000606 // FIXME: Anything needed here to flag the function as thumb?
607
608 getAssembler().setIsThumbFunc(Func);
609
610 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
611 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
612 }
613
Logan Chien2bcc42c2013-01-30 15:39:04 +0000614 // Helper functions for ARM exception handling directives
615 void Reset();
616
617 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000618 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000619 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000620
621 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
622 SectionKind Kind, const MCSymbol &Fn);
623 void SwitchToExTabSection(const MCSymbol &FnStart);
624 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000625
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000626 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
627
Tim Northover5cc3dc82012-12-07 16:50:23 +0000628 bool IsThumb;
629 int64_t MappingSymbolCounter;
630
631 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
632 ElfMappingSymbol LastEMS;
633
Logan Chien2bcc42c2013-01-30 15:39:04 +0000634 // ARM Exception Handling Frame Information
635 MCSymbol *ExTab;
636 MCSymbol *FnStart;
637 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000638 unsigned PersonalityIndex;
639 unsigned FPReg; // Frame pointer register
640 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
641 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
642 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000643 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000644 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000645 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000646 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000647};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000648} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000649
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000650ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000651 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000652}
653
654void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
655void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
656void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
657void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
658 getStreamer().emitPersonality(Personality);
659}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000660void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
661 getStreamer().emitPersonalityIndex(Index);
662}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000663void ARMTargetELFStreamer::emitHandlerData() {
664 getStreamer().emitHandlerData();
665}
666void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
667 int64_t Offset) {
668 getStreamer().emitSetFP(FpReg, SpReg, Offset);
669}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000670void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
671 getStreamer().emitMovSP(Reg, Offset);
672}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000673void ARMTargetELFStreamer::emitPad(int64_t Offset) {
674 getStreamer().emitPad(Offset);
675}
676void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
677 bool isVector) {
678 getStreamer().emitRegSave(RegList, isVector);
679}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000680void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
681 const SmallVectorImpl<uint8_t> &Opcodes) {
682 getStreamer().emitUnwindRaw(Offset, Opcodes);
683}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000684void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
685 assert(!Vendor.empty() && "Vendor cannot be empty.");
686
687 if (CurrentVendor == Vendor)
688 return;
689
690 if (!CurrentVendor.empty())
691 finishAttributeSection();
692
693 assert(Contents.empty() &&
694 ".ARM.attributes should be flushed before changing vendor");
695 CurrentVendor = Vendor;
696
697}
698void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
699 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
700}
701void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
702 StringRef Value) {
703 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
704}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000705void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
706 unsigned IntValue,
707 StringRef StringValue) {
708 setAttributeItems(Attribute, IntValue, StringValue,
709 /* OverwriteExisting= */ true);
710}
Logan Chien439e8f92013-12-11 17:16:25 +0000711void ARMTargetELFStreamer::emitArch(unsigned Value) {
712 Arch = Value;
713}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000714void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
715 EmittedArch = Value;
716}
Logan Chien439e8f92013-12-11 17:16:25 +0000717void ARMTargetELFStreamer::emitArchDefaultAttributes() {
718 using namespace ARMBuildAttrs;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000719
Logan Chien439e8f92013-12-11 17:16:25 +0000720 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000721 if (EmittedArch == ARM::INVALID_ARCH)
722 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
723 else
724 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(EmittedArch), false);
Logan Chien439e8f92013-12-11 17:16:25 +0000725
726 switch (Arch) {
727 case ARM::ARMV2:
728 case ARM::ARMV2A:
729 case ARM::ARMV3:
730 case ARM::ARMV3M:
731 case ARM::ARMV4:
732 case ARM::ARMV5:
733 setAttributeItem(ARM_ISA_use, Allowed, false);
734 break;
735
736 case ARM::ARMV4T:
737 case ARM::ARMV5T:
738 case ARM::ARMV5TE:
739 case ARM::ARMV6:
740 case ARM::ARMV6J:
741 setAttributeItem(ARM_ISA_use, Allowed, false);
742 setAttributeItem(THUMB_ISA_use, Allowed, false);
743 break;
744
745 case ARM::ARMV6T2:
746 setAttributeItem(ARM_ISA_use, Allowed, false);
747 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
748 break;
749
750 case ARM::ARMV6Z:
751 case ARM::ARMV6ZK:
752 setAttributeItem(ARM_ISA_use, Allowed, false);
753 setAttributeItem(THUMB_ISA_use, Allowed, false);
754 setAttributeItem(Virtualization_use, AllowTZ, false);
755 break;
756
757 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000758 setAttributeItem(THUMB_ISA_use, Allowed, false);
759 break;
760
761 case ARM::ARMV7:
762 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
763 break;
764
765 case ARM::ARMV7A:
766 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
767 setAttributeItem(ARM_ISA_use, Allowed, false);
768 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
769 break;
770
771 case ARM::ARMV7R:
772 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
773 setAttributeItem(ARM_ISA_use, Allowed, false);
774 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
775 break;
776
777 case ARM::ARMV7M:
778 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
779 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
780 break;
781
782 case ARM::ARMV8A:
783 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
784 setAttributeItem(ARM_ISA_use, Allowed, false);
785 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
786 setAttributeItem(MPextension_use, Allowed, false);
787 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
788 break;
789
790 case ARM::IWMMXT:
791 setAttributeItem(ARM_ISA_use, Allowed, false);
792 setAttributeItem(THUMB_ISA_use, Allowed, false);
793 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
794 break;
795
796 case ARM::IWMMXT2:
797 setAttributeItem(ARM_ISA_use, Allowed, false);
798 setAttributeItem(THUMB_ISA_use, Allowed, false);
799 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
800 break;
801
802 default:
803 report_fatal_error("Unknown Arch: " + Twine(Arch));
804 break;
805 }
806}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000807void ARMTargetELFStreamer::emitFPU(unsigned Value) {
808 FPU = Value;
809}
810void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
811 switch (FPU) {
812 case ARM::VFP:
813 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000814 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000815 ARMBuildAttrs::AllowFPv2,
816 /* OverwriteExisting= */ false);
817 break;
818
819 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000820 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000821 ARMBuildAttrs::AllowFPv3A,
822 /* OverwriteExisting= */ false);
823 break;
824
825 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000826 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000827 ARMBuildAttrs::AllowFPv3B,
828 /* OverwriteExisting= */ false);
829 break;
830
831 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000832 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000833 ARMBuildAttrs::AllowFPv4A,
834 /* OverwriteExisting= */ false);
835 break;
836
837 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000838 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000839 ARMBuildAttrs::AllowFPv4B,
840 /* OverwriteExisting= */ false);
841 break;
842
843 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000844 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000845 ARMBuildAttrs::AllowFPARMv8A,
846 /* OverwriteExisting= */ false);
847 break;
848
849 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000850 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000851 ARMBuildAttrs::AllowFPv3A,
852 /* OverwriteExisting= */ false);
853 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
854 ARMBuildAttrs::AllowNeon,
855 /* OverwriteExisting= */ false);
856 break;
857
858 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000859 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000860 ARMBuildAttrs::AllowFPv4A,
861 /* OverwriteExisting= */ false);
862 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
863 ARMBuildAttrs::AllowNeon2,
864 /* OverwriteExisting= */ false);
865 break;
866
867 case ARM::NEON_FP_ARMV8:
868 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000869 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000870 ARMBuildAttrs::AllowFPARMv8A,
871 /* OverwriteExisting= */ false);
872 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
873 ARMBuildAttrs::AllowNeonARMv8,
874 /* OverwriteExisting= */ false);
875 break;
876
Logan Chien05ae7442014-01-02 15:50:02 +0000877 case ARM::SOFTVFP:
878 break;
879
Logan Chien8cbb80d2013-10-28 17:51:12 +0000880 default:
881 report_fatal_error("Unknown FPU: " + Twine(FPU));
882 break;
883 }
884}
885size_t ARMTargetELFStreamer::calculateContentSize() const {
886 size_t Result = 0;
887 for (size_t i = 0; i < Contents.size(); ++i) {
888 AttributeItem item = Contents[i];
889 switch (item.Type) {
890 case AttributeItem::HiddenAttribute:
891 break;
892 case AttributeItem::NumericAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000893 Result += getULEB128Size(item.Tag);
894 Result += getULEB128Size(item.IntValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000895 break;
896 case AttributeItem::TextAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000897 Result += getULEB128Size(item.Tag);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000898 Result += item.StringValue.size() + 1; // string + '\0'
899 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000900 case AttributeItem::NumericAndTextAttributes:
Logan Chien5b776b72014-02-22 14:00:39 +0000901 Result += getULEB128Size(item.Tag);
902 Result += getULEB128Size(item.IntValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000903 Result += item.StringValue.size() + 1; // string + '\0';
904 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000905 }
906 }
907 return Result;
908}
909void ARMTargetELFStreamer::finishAttributeSection() {
910 // <format-version>
911 // [ <section-length> "vendor-name"
912 // [ <file-tag> <size> <attribute>*
913 // | <section-tag> <size> <section-number>* 0 <attribute>*
914 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
915 // ]+
916 // ]*
917
918 if (FPU != ARM::INVALID_FPU)
919 emitFPUDefaultAttributes();
920
Logan Chien439e8f92013-12-11 17:16:25 +0000921 if (Arch != ARM::INVALID_ARCH)
922 emitArchDefaultAttributes();
923
Logan Chien8cbb80d2013-10-28 17:51:12 +0000924 if (Contents.empty())
925 return;
926
927 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
928
929 ARMELFStreamer &Streamer = getStreamer();
930
931 // Switch to .ARM.attributes section
932 if (AttributeSection) {
933 Streamer.SwitchSection(AttributeSection);
934 } else {
935 AttributeSection =
936 Streamer.getContext().getELFSection(".ARM.attributes",
937 ELF::SHT_ARM_ATTRIBUTES,
938 0,
939 SectionKind::getMetadata());
940 Streamer.SwitchSection(AttributeSection);
941
942 // Format version
943 Streamer.EmitIntValue(0x41, 1);
944 }
945
946 // Vendor size + Vendor name + '\0'
947 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
948
949 // Tag + Tag Size
950 const size_t TagHeaderSize = 1 + 4;
951
952 const size_t ContentsSize = calculateContentSize();
953
954 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
955 Streamer.EmitBytes(CurrentVendor);
956 Streamer.EmitIntValue(0, 1); // '\0'
957
958 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
959 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
960
961 // Size should have been accounted for already, now
962 // emit each field as its type (ULEB or String)
963 for (size_t i = 0; i < Contents.size(); ++i) {
964 AttributeItem item = Contents[i];
965 Streamer.EmitULEB128IntValue(item.Tag);
966 switch (item.Type) {
967 default: llvm_unreachable("Invalid attribute type");
968 case AttributeItem::NumericAttribute:
969 Streamer.EmitULEB128IntValue(item.IntValue);
970 break;
971 case AttributeItem::TextAttribute:
972 Streamer.EmitBytes(item.StringValue.upper());
973 Streamer.EmitIntValue(0, 1); // '\0'
974 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000975 case AttributeItem::NumericAndTextAttributes:
976 Streamer.EmitULEB128IntValue(item.IntValue);
977 Streamer.EmitBytes(item.StringValue.upper());
978 Streamer.EmitIntValue(0, 1); // '\0'
979 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000980 }
981 }
982
983 Contents.clear();
984 FPU = ARM::INVALID_FPU;
985}
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000986
987void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
988 ARMELFStreamer &Streamer = getStreamer();
989 if (!Streamer.IsThumb)
990 return;
991
992 const MCSymbolData &SD = Streamer.getOrCreateSymbolData(Symbol);
993 if (MCELF::GetType(SD) & (ELF::STT_FUNC << ELF_STT_Shift))
994 Streamer.EmitThumbFunc(Symbol);
995}
996
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000997void
998ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
999 getStreamer().EmitFixup(S, FK_Data_4);
1000}
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +00001001void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1002 getStreamer().emitInst(Inst, Suffix);
1003}
Logan Chien8cbb80d2013-10-28 17:51:12 +00001004
1005void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +00001006 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +00001007 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1008 ATS.finishAttributeSection();
1009
1010 MCELFStreamer::FinishImpl();
1011}
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001012
Logan Chien2bcc42c2013-01-30 15:39:04 +00001013inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
1014 unsigned Type,
1015 unsigned Flags,
1016 SectionKind Kind,
1017 const MCSymbol &Fn) {
1018 const MCSectionELF &FnSection =
1019 static_cast<const MCSectionELF &>(Fn.getSection());
1020
1021 // Create the name for new section
1022 StringRef FnSecName(FnSection.getSectionName());
1023 SmallString<128> EHSecName(Prefix);
1024 if (FnSecName != ".text") {
1025 EHSecName += FnSecName;
1026 }
1027
1028 // Get .ARM.extab or .ARM.exidx section
Craig Topper062a2ba2014-04-25 05:30:21 +00001029 const MCSectionELF *EHSection = nullptr;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001030 if (const MCSymbol *Group = FnSection.getGroup()) {
1031 EHSection = getContext().getELFSection(
1032 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
1033 FnSection.getEntrySize(), Group->getName());
1034 } else {
1035 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
1036 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001037 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001038
1039 // Switch to .ARM.extab or .ARM.exidx section
1040 SwitchSection(EHSection);
Rafael Espindola7b514962014-02-04 18:34:04 +00001041 EmitCodeAlignment(4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001042}
1043
1044inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1045 SwitchToEHSection(".ARM.extab",
1046 ELF::SHT_PROGBITS,
1047 ELF::SHF_ALLOC,
1048 SectionKind::getDataRel(),
1049 FnStart);
1050}
1051
1052inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1053 SwitchToEHSection(".ARM.exidx",
1054 ELF::SHT_ARM_EXIDX,
1055 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1056 SectionKind::getDataRel(),
1057 FnStart);
1058}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001059void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1060 MCDataFragment *Frag = getOrCreateDataFragment();
1061 Frag->getFixups().push_back(MCFixup::Create(Frag->getContents().size(), Expr,
1062 Kind));
1063}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001064
1065void ARMELFStreamer::Reset() {
Craig Topper062a2ba2014-04-25 05:30:21 +00001066 ExTab = nullptr;
1067 FnStart = nullptr;
1068 Personality = nullptr;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001069 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001070 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001071 FPOffset = 0;
1072 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001073 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001074 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001075 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001076
Logan Chien325823a2013-06-09 12:22:30 +00001077 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001078 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001079}
1080
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001081void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001082 assert(FnStart == 0);
1083 FnStart = getContext().CreateTempSymbol();
1084 EmitLabel(FnStart);
1085}
1086
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001087void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001088 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001089
1090 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001091 if (!ExTab && !CantUnwind)
1092 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001093
1094 // Emit the exception index table entry
1095 SwitchToExIdxSection(*FnStart);
1096
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001097 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001098 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001099
1100 const MCSymbolRefExpr *FnStartRef =
1101 MCSymbolRefExpr::Create(FnStart,
1102 MCSymbolRefExpr::VK_ARM_PREL31,
1103 getContext());
1104
Rafael Espindola64e1af82013-07-02 15:49:13 +00001105 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001106
1107 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001108 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001109 } else if (ExTab) {
1110 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001111 const MCSymbolRefExpr *ExTabEntryRef =
1112 MCSymbolRefExpr::Create(ExTab,
1113 MCSymbolRefExpr::VK_ARM_PREL31,
1114 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001115 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001116 } else {
1117 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1118 // the second word of exception index table entry. The size of the unwind
1119 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001120 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001121 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001122 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001123 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +00001124 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001125 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001126 }
1127
Logan Chien4ea23b52013-05-10 16:17:24 +00001128 // Switch to the section containing FnStart
1129 SwitchSection(&FnStart->getSection());
1130
Logan Chien2bcc42c2013-01-30 15:39:04 +00001131 // Clean exception handling frame information
1132 Reset();
1133}
1134
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001135void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1136
1137// Add the R_ARM_NONE fixup at the same position
1138void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1139 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1140
1141 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1142 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1143
1144 AddValueSymbols(PersonalityRef);
1145 MCDataFragment *DF = getOrCreateDataFragment();
1146 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1147 PersonalityRef,
1148 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001149}
1150
Logan Chien325823a2013-06-09 12:22:30 +00001151void ARMELFStreamer::FlushPendingOffset() {
1152 if (PendingOffset != 0) {
1153 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1154 PendingOffset = 0;
1155 }
1156}
1157
Logan Chienc931fce2013-07-02 12:43:27 +00001158void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001159 // Emit the unwind opcode to restore $sp.
1160 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001161 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001162 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1163 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001164 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001165 } else {
1166 FlushPendingOffset();
1167 }
1168
1169 // Finalize the unwind opcode sequence
1170 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001171
1172 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1173 // section. Thus, we don't have to create an entry in the .ARM.extab
1174 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001175 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001176 return;
1177
1178 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001179 SwitchToExTabSection(*FnStart);
1180
1181 // Create .ARM.extab label for offset in .ARM.exidx
1182 assert(!ExTab);
1183 ExTab = getContext().CreateTempSymbol();
1184 EmitLabel(ExTab);
1185
Logan Chien4ea23b52013-05-10 16:17:24 +00001186 // Emit personality
1187 if (Personality) {
1188 const MCSymbolRefExpr *PersonalityRef =
1189 MCSymbolRefExpr::Create(Personality,
1190 MCSymbolRefExpr::VK_ARM_PREL31,
1191 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001192
Rafael Espindola64e1af82013-07-02 15:49:13 +00001193 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001194 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001195
1196 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +00001197 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001198 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +00001199
1200 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1201 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1202 // after the unwind opcodes. The handler data consists of several 32-bit
1203 // words, and should be terminated by zero.
1204 //
1205 // In case that the .handlerdata directive is not specified by the
1206 // programmer, we should emit zero to terminate the handler data.
1207 if (NoHandlerData && !Personality)
1208 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001209}
1210
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001211void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001212
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001213void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001214 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001215 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001216}
1217
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001218void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1219 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1220 PersonalityIndex = Index;
1221}
1222
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001223void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001224 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001225 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001226 "the operand of .setfp directive should be either $sp or $fp");
1227
1228 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001229 FPReg = NewFPReg;
1230
1231 if (NewSPReg == ARM::SP)
1232 FPOffset = SPOffset + Offset;
1233 else
1234 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001235}
1236
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +00001237void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1238 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1239 "the operand of .movsp cannot be either sp or pc");
1240 assert(FPReg == ARM::SP && "current FP must be SP");
1241
1242 FlushPendingOffset();
1243
1244 FPReg = Reg;
1245 FPOffset = SPOffset + Offset;
1246
1247 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1248 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1249}
1250
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001251void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001252 // Track the change of the $sp offset
1253 SPOffset -= Offset;
1254
1255 // To squash multiple .pad directives, we should delay the unwind opcode
1256 // until the .save, .vsave, .handlerdata, or .fnend directives.
1257 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001258}
1259
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001260void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001261 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001262 // Collect the registers in the register list
1263 unsigned Count = 0;
1264 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001265 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001266 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001267 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001268 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001269 unsigned Bit = (1u << Reg);
1270 if ((Mask & Bit) == 0) {
1271 Mask |= Bit;
1272 ++Count;
1273 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001274 }
Logan Chien325823a2013-06-09 12:22:30 +00001275
1276 // Track the change the $sp offset: For the .save directive, the
1277 // corresponding push instruction will decrease the $sp by (4 * Count).
1278 // For the .vsave directive, the corresponding vpush instruction will
1279 // decrease $sp by (8 * Count).
1280 SPOffset -= Count * (IsVector ? 8 : 4);
1281
1282 // Emit the opcode
1283 FlushPendingOffset();
1284 if (IsVector)
1285 UnwindOpAsm.EmitVFPRegSave(Mask);
1286 else
1287 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001288}
1289
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001290void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1291 const SmallVectorImpl<uint8_t> &Opcodes) {
1292 FlushPendingOffset();
1293 SPOffset = SPOffset - Offset;
1294 UnwindOpAsm.EmitRaw(Opcodes);
1295}
1296
Tim Northover5cc3dc82012-12-07 16:50:23 +00001297namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001298
1299MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001300 bool isVerboseAsm, bool useCFI,
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001301 bool useDwarfDirectory,
1302 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1303 MCAsmBackend *TAB, bool ShowInst) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001304 MCStreamer *S =
Rafael Espindolab4eec1d2014-02-05 18:00:21 +00001305 llvm::createAsmStreamer(Ctx, OS, isVerboseAsm, useCFI, useDwarfDirectory,
1306 InstPrint, CE, TAB, ShowInst);
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001307 new ARMTargetAsmStreamer(*S, OS, *InstPrint, isVerboseAsm);
1308 return S;
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001309}
1310
Tim Northover5cc3dc82012-12-07 16:50:23 +00001311 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1312 raw_ostream &OS, MCCodeEmitter *Emitter,
1313 bool RelaxAll, bool NoExecStack,
1314 bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001315 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
1316 new ARMTargetELFStreamer(*S);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001317 // FIXME: This should eventually end up somewhere else where more
1318 // intelligent flag decisions can be made. For now we are just maintaining
1319 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1320 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1321
Tim Northover5cc3dc82012-12-07 16:50:23 +00001322 if (RelaxAll)
1323 S->getAssembler().setRelaxAll(true);
1324 if (NoExecStack)
1325 S->getAssembler().setNoExecStack(true);
1326 return S;
1327 }
1328
1329}
1330
1331