blob: e7c777e26f9b05e1e10253737f089200bb7e709c [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"
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +000018#include "ARMArchExtName.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000019#include "ARMRegisterInfo.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000020#include "ARMUnwindOpAsm.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000021#include "llvm/ADT/StringExtras.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000022#include "llvm/ADT/Twine.h"
23#include "llvm/MC/MCAsmBackend.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000024#include "llvm/MC/MCAsmInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000025#include "llvm/MC/MCAssembler.h"
26#include "llvm/MC/MCCodeEmitter.h"
27#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000028#include "llvm/MC/MCELF.h"
29#include "llvm/MC/MCELFStreamer.h"
30#include "llvm/MC/MCELFSymbolFlags.h"
31#include "llvm/MC/MCExpr.h"
32#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000033#include "llvm/MC/MCInstPrinter.h"
Rafael Espindola4c6f6132014-04-27 17:10:46 +000034#include "llvm/MC/MCObjectFileInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000035#include "llvm/MC/MCObjectStreamer.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000036#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000037#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000038#include "llvm/MC/MCSectionELF.h"
39#include "llvm/MC/MCStreamer.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000040#include "llvm/MC/MCSymbol.h"
41#include "llvm/MC/MCValue.h"
Saleem Abdulrasool278a9f42014-01-19 08:25:27 +000042#include "llvm/Support/ARMBuildAttributes.h"
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000043#include "llvm/Support/ARMEHABI.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000044#include "llvm/Support/Debug.h"
45#include "llvm/Support/ELF.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000046#include "llvm/Support/FormattedStream.h"
Logan Chien5b776b72014-02-22 14:00:39 +000047#include "llvm/Support/LEB128.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000048#include "llvm/Support/raw_ostream.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000049#include <algorithm>
Tim Northover5cc3dc82012-12-07 16:50:23 +000050
51using namespace llvm;
52
Logan Chiend8bb4b72013-04-16 12:02:21 +000053static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000054 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
55 "Invalid personality index");
Logan Chiend8bb4b72013-04-16 12:02:21 +000056 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
57}
58
Logan Chien8cbb80d2013-10-28 17:51:12 +000059static const char *GetFPUName(unsigned ID) {
60 switch (ID) {
61 default:
62 llvm_unreachable("Unknown FPU kind");
63 break;
64#define ARM_FPU_NAME(NAME, ID) case ARM::ID: return NAME;
65#include "ARMFPUName.def"
66 }
Craig Topper062a2ba2014-04-25 05:30:21 +000067 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +000068}
69
Logan Chien439e8f92013-12-11 17:16:25 +000070static const char *GetArchName(unsigned ID) {
71 switch (ID) {
72 default:
73 llvm_unreachable("Unknown ARCH kind");
74 break;
75#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
76 case ARM::ID: return NAME;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +000077#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +000078#include "ARMArchName.def"
79 }
Craig Topper062a2ba2014-04-25 05:30:21 +000080 return nullptr;
Logan Chien439e8f92013-12-11 17:16:25 +000081}
82
83static const char *GetArchDefaultCPUName(unsigned ID) {
84 switch (ID) {
85 default:
86 llvm_unreachable("Unknown ARCH kind");
87 break;
88#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
89 case ARM::ID: return DEFAULT_CPU_NAME;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +000090#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +000091#include "ARMArchName.def"
92 }
Craig Topper062a2ba2014-04-25 05:30:21 +000093 return nullptr;
Logan Chien439e8f92013-12-11 17:16:25 +000094}
95
96static unsigned GetArchDefaultCPUArch(unsigned ID) {
97 switch (ID) {
98 default:
99 llvm_unreachable("Unknown ARCH kind");
100 break;
101#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
102 case ARM::ID: return ARMBuildAttrs::DEFAULT_CPU_ARCH;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +0000103#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +0000104#include "ARMArchName.def"
105 }
106 return 0;
107}
108
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000109static const char *GetArchExtName(unsigned ID) {
110 switch (ID) {
111 default:
112 llvm_unreachable("Unknown ARCH Extension kind");
113 break;
114#define ARM_ARCHEXT_NAME(NAME, ID) \
115 case ARM::ID: \
116 return NAME;
117#include "ARMArchExtName.def"
118 }
119 return nullptr;
120}
121
Tim Northover5cc3dc82012-12-07 16:50:23 +0000122namespace {
123
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000124class ARMELFStreamer;
125
126class ARMTargetAsmStreamer : public ARMTargetStreamer {
127 formatted_raw_ostream &OS;
128 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000129 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000130
Craig Topperca7e3e52014-03-10 03:19:03 +0000131 void emitFnStart() override;
132 void emitFnEnd() override;
133 void emitCantUnwind() override;
134 void emitPersonality(const MCSymbol *Personality) override;
135 void emitPersonalityIndex(unsigned Index) override;
136 void emitHandlerData() override;
137 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
138 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
139 void emitPad(int64_t Offset) override;
140 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
141 bool isVector) override;
142 void emitUnwindRaw(int64_t Offset,
143 const SmallVectorImpl<uint8_t> &Opcodes) override;
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000144
Craig Topperca7e3e52014-03-10 03:19:03 +0000145 void switchVendor(StringRef Vendor) override;
146 void emitAttribute(unsigned Attribute, unsigned Value) override;
147 void emitTextAttribute(unsigned Attribute, StringRef String) override;
148 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
149 StringRef StrinValue) override;
150 void emitArch(unsigned Arch) override;
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000151 void emitArchExtension(unsigned ArchExt) override;
Craig Topperca7e3e52014-03-10 03:19:03 +0000152 void emitObjectArch(unsigned Arch) override;
153 void emitFPU(unsigned FPU) override;
154 void emitInst(uint32_t Inst, char Suffix = '\0') override;
155 void finishAttributeSection() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000156
Craig Topperca7e3e52014-03-10 03:19:03 +0000157 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000158 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000159
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000160public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000161 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
162 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000163};
164
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000165ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
166 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000167 MCInstPrinter &InstPrinter,
168 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000169 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
170 IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000171void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
172void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
173void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
174void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
175 OS << "\t.personality " << Personality->getName() << '\n';
176}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000177void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
178 OS << "\t.personalityindex " << Index << '\n';
179}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000180void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
181void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
182 int64_t Offset) {
183 OS << "\t.setfp\t";
184 InstPrinter.printRegName(OS, FpReg);
185 OS << ", ";
186 InstPrinter.printRegName(OS, SpReg);
187 if (Offset)
188 OS << ", #" << Offset;
189 OS << '\n';
190}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000191void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
192 assert((Reg != ARM::SP && Reg != ARM::PC) &&
193 "the operand of .movsp cannot be either sp or pc");
194
195 OS << "\t.movsp\t";
196 InstPrinter.printRegName(OS, Reg);
197 if (Offset)
198 OS << ", #" << Offset;
199 OS << '\n';
200}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000201void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
202 OS << "\t.pad\t#" << Offset << '\n';
203}
204void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
205 bool isVector) {
206 assert(RegList.size() && "RegList should not be empty");
207 if (isVector)
208 OS << "\t.vsave\t{";
209 else
210 OS << "\t.save\t{";
211
212 InstPrinter.printRegName(OS, RegList[0]);
213
214 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
215 OS << ", ";
216 InstPrinter.printRegName(OS, RegList[i]);
217 }
218
219 OS << "}\n";
220}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000221void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
222}
223void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000224 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
225 if (IsVerboseAsm) {
226 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
227 if (!Name.empty())
228 OS << "\t@ " << Name;
229 }
230 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000231}
232void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
233 StringRef String) {
234 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000235 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000236 OS << "\t.cpu\t" << String.lower();
237 break;
238 default:
239 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000240 if (IsVerboseAsm) {
241 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
242 if (!Name.empty())
243 OS << "\t@ " << Name;
244 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000245 break;
246 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000247 OS << "\n";
248}
249void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
250 unsigned IntValue,
251 StringRef StringValue) {
252 switch (Attribute) {
253 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
254 case ARMBuildAttrs::compatibility:
255 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
256 if (!StringValue.empty())
257 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000258 if (IsVerboseAsm)
259 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000260 break;
261 }
262 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000263}
Logan Chien439e8f92013-12-11 17:16:25 +0000264void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
265 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
266}
Sumanth Gundapanenia9049ea2015-02-26 18:07:35 +0000267void ARMTargetAsmStreamer::emitArchExtension(unsigned ArchExt) {
268 OS << "\t.arch_extension\t" << GetArchExtName(ArchExt) << "\n";
269}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000270void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
271 OS << "\t.object_arch\t" << GetArchName(Arch) << '\n';
272}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000273void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
274 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
275}
276void ARMTargetAsmStreamer::finishAttributeSection() {
277}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000278void
279ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
280 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
281}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000282
Rafael Espindola466d6632014-04-27 20:23:58 +0000283void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
284 OS << "\t.thumb_set\t" << *Symbol << ", " << *Value << '\n';
285}
286
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000287void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
288 OS << "\t.inst";
289 if (Suffix)
290 OS << "." << Suffix;
291 OS << "\t0x" << utohexstr(Inst) << "\n";
292}
293
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000294void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
295 const SmallVectorImpl<uint8_t> &Opcodes) {
296 OS << "\t.unwind_raw " << Offset;
297 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
298 OCE = Opcodes.end();
299 OCI != OCE; ++OCI)
300 OS << ", 0x" << utohexstr(*OCI);
301 OS << '\n';
302}
303
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000304class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000305private:
306 // This structure holds all attributes, accounting for
307 // their string/numeric value, so we can later emmit them
308 // in declaration order, keeping all in the same vector
309 struct AttributeItem {
310 enum {
311 HiddenAttribute = 0,
312 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000313 TextAttribute,
314 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000315 } Type;
316 unsigned Tag;
317 unsigned IntValue;
318 StringRef StringValue;
319
320 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
Charlie Turner8b2caa42015-01-05 13:12:17 +0000321 // The conformance tag must be emitted first when serialised
322 // into an object file. Specifically, the addenda to the ARM ABI
323 // states that (2.3.7.4):
324 //
325 // "To simplify recognition by consumers in the common case of
326 // claiming conformity for the whole file, this tag should be
327 // emitted first in a file-scope sub-subsection of the first
328 // public subsection of the attributes section."
329 //
330 // So it is special-cased in this comparison predicate when the
331 // attributes are sorted in finishAttributeSection().
332 return (RHS.Tag != ARMBuildAttrs::conformance) &&
333 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
Logan Chien8cbb80d2013-10-28 17:51:12 +0000334 }
335 };
336
337 StringRef CurrentVendor;
338 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000339 unsigned Arch;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000340 unsigned EmittedArch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000341 SmallVector<AttributeItem, 64> Contents;
342
343 const MCSection *AttributeSection;
344
Logan Chien8cbb80d2013-10-28 17:51:12 +0000345 AttributeItem *getAttributeItem(unsigned Attribute) {
346 for (size_t i = 0; i < Contents.size(); ++i)
347 if (Contents[i].Tag == Attribute)
348 return &Contents[i];
Craig Topper062a2ba2014-04-25 05:30:21 +0000349 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000350 }
351
352 void setAttributeItem(unsigned Attribute, unsigned Value,
353 bool OverwriteExisting) {
354 // Look for existing attribute item
355 if (AttributeItem *Item = getAttributeItem(Attribute)) {
356 if (!OverwriteExisting)
357 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000358 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000359 Item->IntValue = Value;
360 return;
361 }
362
363 // Create new attribute item
364 AttributeItem Item = {
365 AttributeItem::NumericAttribute,
366 Attribute,
367 Value,
368 StringRef("")
369 };
370 Contents.push_back(Item);
371 }
372
373 void setAttributeItem(unsigned Attribute, StringRef Value,
374 bool OverwriteExisting) {
375 // Look for existing attribute item
376 if (AttributeItem *Item = getAttributeItem(Attribute)) {
377 if (!OverwriteExisting)
378 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000379 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000380 Item->StringValue = Value;
381 return;
382 }
383
384 // Create new attribute item
385 AttributeItem Item = {
386 AttributeItem::TextAttribute,
387 Attribute,
388 0,
389 Value
390 };
391 Contents.push_back(Item);
392 }
393
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000394 void setAttributeItems(unsigned Attribute, unsigned IntValue,
395 StringRef StringValue, bool OverwriteExisting) {
396 // Look for existing attribute item
397 if (AttributeItem *Item = getAttributeItem(Attribute)) {
398 if (!OverwriteExisting)
399 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000400 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000401 Item->IntValue = IntValue;
402 Item->StringValue = StringValue;
403 return;
404 }
405
406 // Create new attribute item
407 AttributeItem Item = {
408 AttributeItem::NumericAndTextAttributes,
409 Attribute,
410 IntValue,
411 StringValue
412 };
413 Contents.push_back(Item);
414 }
415
Logan Chien439e8f92013-12-11 17:16:25 +0000416 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000417 void emitFPUDefaultAttributes();
418
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000419 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000420
Craig Topperca7e3e52014-03-10 03:19:03 +0000421 void emitFnStart() override;
422 void emitFnEnd() override;
423 void emitCantUnwind() override;
424 void emitPersonality(const MCSymbol *Personality) override;
425 void emitPersonalityIndex(unsigned Index) override;
426 void emitHandlerData() override;
427 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
428 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
429 void emitPad(int64_t Offset) override;
430 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
431 bool isVector) override;
432 void emitUnwindRaw(int64_t Offset,
433 const SmallVectorImpl<uint8_t> &Opcodes) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000434
Craig Topperca7e3e52014-03-10 03:19:03 +0000435 void switchVendor(StringRef Vendor) override;
436 void emitAttribute(unsigned Attribute, unsigned Value) override;
437 void emitTextAttribute(unsigned Attribute, StringRef String) override;
438 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
439 StringRef StringValue) override;
440 void emitArch(unsigned Arch) override;
441 void emitObjectArch(unsigned Arch) override;
442 void emitFPU(unsigned FPU) override;
443 void emitInst(uint32_t Inst, char Suffix = '\0') override;
444 void finishAttributeSection() override;
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000445 void emitLabel(MCSymbol *Symbol) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000446
Craig Topperca7e3e52014-03-10 03:19:03 +0000447 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000448 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000449
Logan Chien8cbb80d2013-10-28 17:51:12 +0000450 size_t calculateContentSize() const;
451
452public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000453 ARMTargetELFStreamer(MCStreamer &S)
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000454 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
455 Arch(ARM::INVALID_ARCH), EmittedArch(ARM::INVALID_ARCH),
Craig Topper062a2ba2014-04-25 05:30:21 +0000456 AttributeSection(nullptr) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000457};
458
Tim Northover5cc3dc82012-12-07 16:50:23 +0000459/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
460/// the appropriate points in the object files. These symbols are defined in the
461/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
462///
463/// In brief: $a, $t or $d should be emitted at the start of each contiguous
464/// region of ARM code, Thumb code or data in a section. In practice, this
465/// emission does not rely on explicit assembler directives but on inherent
466/// properties of the directives doing the emission (e.g. ".byte" is data, "add
467/// r0, r0, r0" an instruction).
468///
469/// As a result this system is orthogonal to the DataRegion infrastructure used
470/// by MachO. Beware!
471class ARMELFStreamer : public MCELFStreamer {
472public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000473 friend class ARMTargetELFStreamer;
474
Rafael Espindola5560a4c2015-04-14 22:14:34 +0000475 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_pwrite_stream &OS,
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000476 MCCodeEmitter *Emitter, bool IsThumb)
477 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
478 MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000479 Reset();
480 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000481
482 ~ARMELFStreamer() {}
483
Craig Topperca7e3e52014-03-10 03:19:03 +0000484 void FinishImpl() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000485
Logan Chien2bcc42c2013-01-30 15:39:04 +0000486 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000487 void emitFnStart();
488 void emitFnEnd();
489 void emitCantUnwind();
490 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000491 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000492 void emitHandlerData();
493 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000494 void emitMovSP(unsigned Reg, int64_t Offset = 0);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000495 void emitPad(int64_t Offset);
496 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000497 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000498
Craig Topperca7e3e52014-03-10 03:19:03 +0000499 void ChangeSection(const MCSection *Section,
500 const MCExpr *Subsection) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000501 // We have to keep track of the mapping symbol state of any sections we
502 // use. Each one should start off as EMS_None, which is provided as the
503 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000504 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000505 LastEMS = LastMappingSymbols.lookup(Section);
506
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000507 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000508 }
509
510 /// This function is the one used to emit instruction data into the ELF
511 /// streamer. We override it to add the appropriate mapping symbol if
512 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000513 void EmitInstruction(const MCInst& Inst,
514 const MCSubtargetInfo &STI) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000515 if (IsThumb)
516 EmitThumbMappingSymbol();
517 else
518 EmitARMMappingSymbol();
519
David Woodhousee6c13e42014-01-28 23:12:42 +0000520 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000521 }
522
Craig Topperd25ff6f2014-03-10 03:22:59 +0000523 void emitInst(uint32_t Inst, char Suffix) {
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000524 unsigned Size;
525 char Buffer[4];
526 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
527
528 switch (Suffix) {
529 case '\0':
530 Size = 4;
531
532 assert(!IsThumb);
533 EmitARMMappingSymbol();
534 for (unsigned II = 0, IE = Size; II != IE; II++) {
535 const unsigned I = LittleEndian ? (Size - II - 1) : II;
536 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
537 }
538
539 break;
540 case 'n':
541 case 'w':
542 Size = (Suffix == 'n' ? 2 : 4);
543
544 assert(IsThumb);
545 EmitThumbMappingSymbol();
546 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
547 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
548 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
549 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
550 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
551 }
552
553 break;
554 default:
555 llvm_unreachable("Invalid Suffix");
556 }
557
558 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
559 }
560
Tim Northover5cc3dc82012-12-07 16:50:23 +0000561 /// This is one of the functions used to emit data into an ELF section, so the
562 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
563 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000564 void EmitBytes(StringRef Data) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000565 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000566 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000567 }
568
569 /// This is one of the functions used to emit data into an ELF section, so the
570 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
571 /// necessary.
Kevin Enderby96918bc2014-04-22 17:27:29 +0000572 void EmitValueImpl(const MCExpr *Value, unsigned Size,
573 const SMLoc &Loc) override {
Saleem Abdulrasoolfe781972015-01-11 04:39:18 +0000574 if (const MCSymbolRefExpr *SRE = dyn_cast_or_null<MCSymbolRefExpr>(Value))
575 if (SRE->getKind() == MCSymbolRefExpr::VK_ARM_SBREL && !(Size == 4))
576 getContext().FatalError(Loc, "relocated expression must be 32-bit");
577
Tim Northover5cc3dc82012-12-07 16:50:23 +0000578 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000579 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000580 }
581
Craig Topperca7e3e52014-03-10 03:19:03 +0000582 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000583 MCELFStreamer::EmitAssemblerFlag(Flag);
584
585 switch (Flag) {
586 case MCAF_SyntaxUnified:
587 return; // no-op here.
588 case MCAF_Code16:
589 IsThumb = true;
590 return; // Change to Thumb mode
591 case MCAF_Code32:
592 IsThumb = false;
593 return; // Change to ARM mode
594 case MCAF_Code64:
595 return;
596 case MCAF_SubsectionsViaSymbols:
597 return;
598 }
599 }
600
601private:
602 enum ElfMappingSymbol {
603 EMS_None,
604 EMS_ARM,
605 EMS_Thumb,
606 EMS_Data
607 };
608
609 void EmitDataMappingSymbol() {
610 if (LastEMS == EMS_Data) return;
611 EmitMappingSymbol("$d");
612 LastEMS = EMS_Data;
613 }
614
615 void EmitThumbMappingSymbol() {
616 if (LastEMS == EMS_Thumb) return;
617 EmitMappingSymbol("$t");
618 LastEMS = EMS_Thumb;
619 }
620
621 void EmitARMMappingSymbol() {
622 if (LastEMS == EMS_ARM) return;
623 EmitMappingSymbol("$a");
624 LastEMS = EMS_ARM;
625 }
626
627 void EmitMappingSymbol(StringRef Name) {
628 MCSymbol *Start = getContext().CreateTempSymbol();
629 EmitLabel(Start);
630
Chandler Carruth1d94e932012-12-08 03:10:14 +0000631 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000632 getContext().GetOrCreateSymbol(Name + "." +
633 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000634
635 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
636 MCELF::SetType(SD, ELF::STT_NOTYPE);
637 MCELF::SetBinding(SD, ELF::STB_LOCAL);
638 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000639 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000640
641 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
642 Symbol->setVariableValue(Value);
643 }
644
Craig Topperca7e3e52014-03-10 03:19:03 +0000645 void EmitThumbFunc(MCSymbol *Func) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000646 getAssembler().setIsThumbFunc(Func);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000647 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000648 }
649
Logan Chien2bcc42c2013-01-30 15:39:04 +0000650 // Helper functions for ARM exception handling directives
651 void Reset();
652
653 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000654 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000655 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000656
657 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
658 SectionKind Kind, const MCSymbol &Fn);
659 void SwitchToExTabSection(const MCSymbol &FnStart);
660 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000661
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000662 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
663
Tim Northover5cc3dc82012-12-07 16:50:23 +0000664 bool IsThumb;
665 int64_t MappingSymbolCounter;
666
667 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
668 ElfMappingSymbol LastEMS;
669
Logan Chien2bcc42c2013-01-30 15:39:04 +0000670 // ARM Exception Handling Frame Information
671 MCSymbol *ExTab;
672 MCSymbol *FnStart;
673 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000674 unsigned PersonalityIndex;
675 unsigned FPReg; // Frame pointer register
676 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
677 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
678 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000679 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000680 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000681 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000682 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000683};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000684} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000685
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000686ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000687 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000688}
689
690void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
691void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
692void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
693void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
694 getStreamer().emitPersonality(Personality);
695}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000696void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
697 getStreamer().emitPersonalityIndex(Index);
698}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000699void ARMTargetELFStreamer::emitHandlerData() {
700 getStreamer().emitHandlerData();
701}
702void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
703 int64_t Offset) {
704 getStreamer().emitSetFP(FpReg, SpReg, Offset);
705}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000706void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
707 getStreamer().emitMovSP(Reg, Offset);
708}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000709void ARMTargetELFStreamer::emitPad(int64_t Offset) {
710 getStreamer().emitPad(Offset);
711}
712void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
713 bool isVector) {
714 getStreamer().emitRegSave(RegList, isVector);
715}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000716void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
717 const SmallVectorImpl<uint8_t> &Opcodes) {
718 getStreamer().emitUnwindRaw(Offset, Opcodes);
719}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000720void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
721 assert(!Vendor.empty() && "Vendor cannot be empty.");
722
723 if (CurrentVendor == Vendor)
724 return;
725
726 if (!CurrentVendor.empty())
727 finishAttributeSection();
728
729 assert(Contents.empty() &&
730 ".ARM.attributes should be flushed before changing vendor");
731 CurrentVendor = Vendor;
732
733}
734void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
735 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
736}
737void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
738 StringRef Value) {
739 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
740}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000741void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
742 unsigned IntValue,
743 StringRef StringValue) {
744 setAttributeItems(Attribute, IntValue, StringValue,
745 /* OverwriteExisting= */ true);
746}
Logan Chien439e8f92013-12-11 17:16:25 +0000747void ARMTargetELFStreamer::emitArch(unsigned Value) {
748 Arch = Value;
749}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000750void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
751 EmittedArch = Value;
752}
Logan Chien439e8f92013-12-11 17:16:25 +0000753void ARMTargetELFStreamer::emitArchDefaultAttributes() {
754 using namespace ARMBuildAttrs;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000755
Logan Chien439e8f92013-12-11 17:16:25 +0000756 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000757 if (EmittedArch == ARM::INVALID_ARCH)
758 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
759 else
760 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(EmittedArch), false);
Logan Chien439e8f92013-12-11 17:16:25 +0000761
762 switch (Arch) {
763 case ARM::ARMV2:
764 case ARM::ARMV2A:
765 case ARM::ARMV3:
766 case ARM::ARMV3M:
767 case ARM::ARMV4:
768 case ARM::ARMV5:
769 setAttributeItem(ARM_ISA_use, Allowed, false);
770 break;
771
772 case ARM::ARMV4T:
773 case ARM::ARMV5T:
774 case ARM::ARMV5TE:
775 case ARM::ARMV6:
776 case ARM::ARMV6J:
777 setAttributeItem(ARM_ISA_use, Allowed, false);
778 setAttributeItem(THUMB_ISA_use, Allowed, false);
779 break;
780
781 case ARM::ARMV6T2:
782 setAttributeItem(ARM_ISA_use, Allowed, false);
783 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
784 break;
785
Renato Golin12350602015-03-17 11:55:28 +0000786 case ARM::ARMV6K:
Logan Chien439e8f92013-12-11 17:16:25 +0000787 case ARM::ARMV6Z:
788 case ARM::ARMV6ZK:
789 setAttributeItem(ARM_ISA_use, Allowed, false);
790 setAttributeItem(THUMB_ISA_use, Allowed, false);
791 setAttributeItem(Virtualization_use, AllowTZ, false);
792 break;
793
794 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000795 setAttributeItem(THUMB_ISA_use, Allowed, false);
796 break;
797
798 case ARM::ARMV7:
799 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
800 break;
801
802 case ARM::ARMV7A:
803 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
804 setAttributeItem(ARM_ISA_use, Allowed, false);
805 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
806 break;
807
808 case ARM::ARMV7R:
809 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
810 setAttributeItem(ARM_ISA_use, Allowed, false);
811 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
812 break;
813
814 case ARM::ARMV7M:
815 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
816 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
817 break;
818
819 case ARM::ARMV8A:
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000820 case ARM::ARMV8_1A:
Logan Chien439e8f92013-12-11 17:16:25 +0000821 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
822 setAttributeItem(ARM_ISA_use, Allowed, false);
823 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
824 setAttributeItem(MPextension_use, Allowed, false);
825 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
826 break;
827
828 case ARM::IWMMXT:
829 setAttributeItem(ARM_ISA_use, Allowed, false);
830 setAttributeItem(THUMB_ISA_use, Allowed, false);
831 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
832 break;
833
834 case ARM::IWMMXT2:
835 setAttributeItem(ARM_ISA_use, Allowed, false);
836 setAttributeItem(THUMB_ISA_use, Allowed, false);
837 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
838 break;
839
840 default:
841 report_fatal_error("Unknown Arch: " + Twine(Arch));
842 break;
843 }
844}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000845void ARMTargetELFStreamer::emitFPU(unsigned Value) {
846 FPU = Value;
847}
848void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
849 switch (FPU) {
850 case ARM::VFP:
851 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000852 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000853 ARMBuildAttrs::AllowFPv2,
854 /* OverwriteExisting= */ false);
855 break;
856
857 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000858 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000859 ARMBuildAttrs::AllowFPv3A,
860 /* OverwriteExisting= */ false);
861 break;
862
863 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000864 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000865 ARMBuildAttrs::AllowFPv3B,
866 /* OverwriteExisting= */ false);
867 break;
868
869 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000870 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000871 ARMBuildAttrs::AllowFPv4A,
872 /* OverwriteExisting= */ false);
873 break;
874
875 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000876 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000877 ARMBuildAttrs::AllowFPv4B,
878 /* OverwriteExisting= */ false);
879 break;
880
881 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000882 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000883 ARMBuildAttrs::AllowFPARMv8A,
884 /* OverwriteExisting= */ false);
885 break;
886
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000887 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
888 // uses the FP_ARMV8_D16 build attribute.
889 case ARM::FPV5_D16:
890 setAttributeItem(ARMBuildAttrs::FP_arch,
891 ARMBuildAttrs::AllowFPARMv8B,
892 /* OverwriteExisting= */ false);
893 break;
894
Logan Chien8cbb80d2013-10-28 17:51:12 +0000895 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000896 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000897 ARMBuildAttrs::AllowFPv3A,
898 /* OverwriteExisting= */ false);
899 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
900 ARMBuildAttrs::AllowNeon,
901 /* OverwriteExisting= */ false);
902 break;
903
904 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000905 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000906 ARMBuildAttrs::AllowFPv4A,
907 /* OverwriteExisting= */ false);
908 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
909 ARMBuildAttrs::AllowNeon2,
910 /* OverwriteExisting= */ false);
911 break;
912
913 case ARM::NEON_FP_ARMV8:
914 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000915 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000916 ARMBuildAttrs::AllowFPARMv8A,
917 /* OverwriteExisting= */ false);
Vladimir Sukharevc632cda2015-03-26 17:05:54 +0000918 // 'Advanced_SIMD_arch' must be emitted not here, but within
919 // ARMAsmPrinter::emitAttributes(), depending on hasV8Ops() and hasV8_1a()
Logan Chien8cbb80d2013-10-28 17:51:12 +0000920 break;
921
Logan Chien05ae7442014-01-02 15:50:02 +0000922 case ARM::SOFTVFP:
923 break;
924
Logan Chien8cbb80d2013-10-28 17:51:12 +0000925 default:
926 report_fatal_error("Unknown FPU: " + Twine(FPU));
927 break;
928 }
929}
930size_t ARMTargetELFStreamer::calculateContentSize() const {
931 size_t Result = 0;
932 for (size_t i = 0; i < Contents.size(); ++i) {
933 AttributeItem item = Contents[i];
934 switch (item.Type) {
935 case AttributeItem::HiddenAttribute:
936 break;
937 case AttributeItem::NumericAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000938 Result += getULEB128Size(item.Tag);
939 Result += getULEB128Size(item.IntValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000940 break;
941 case AttributeItem::TextAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000942 Result += getULEB128Size(item.Tag);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000943 Result += item.StringValue.size() + 1; // string + '\0'
944 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000945 case AttributeItem::NumericAndTextAttributes:
Logan Chien5b776b72014-02-22 14:00:39 +0000946 Result += getULEB128Size(item.Tag);
947 Result += getULEB128Size(item.IntValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000948 Result += item.StringValue.size() + 1; // string + '\0';
949 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000950 }
951 }
952 return Result;
953}
954void ARMTargetELFStreamer::finishAttributeSection() {
955 // <format-version>
956 // [ <section-length> "vendor-name"
957 // [ <file-tag> <size> <attribute>*
958 // | <section-tag> <size> <section-number>* 0 <attribute>*
959 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
960 // ]+
961 // ]*
962
963 if (FPU != ARM::INVALID_FPU)
964 emitFPUDefaultAttributes();
965
Logan Chien439e8f92013-12-11 17:16:25 +0000966 if (Arch != ARM::INVALID_ARCH)
967 emitArchDefaultAttributes();
968
Logan Chien8cbb80d2013-10-28 17:51:12 +0000969 if (Contents.empty())
970 return;
971
972 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
973
974 ARMELFStreamer &Streamer = getStreamer();
975
976 // Switch to .ARM.attributes section
977 if (AttributeSection) {
978 Streamer.SwitchSection(AttributeSection);
979 } else {
Rafael Espindolaba31e272015-01-29 17:33:21 +0000980 AttributeSection = Streamer.getContext().getELFSection(
981 ".ARM.attributes", ELF::SHT_ARM_ATTRIBUTES, 0);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000982 Streamer.SwitchSection(AttributeSection);
983
984 // Format version
985 Streamer.EmitIntValue(0x41, 1);
986 }
987
988 // Vendor size + Vendor name + '\0'
989 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
990
991 // Tag + Tag Size
992 const size_t TagHeaderSize = 1 + 4;
993
994 const size_t ContentsSize = calculateContentSize();
995
996 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
997 Streamer.EmitBytes(CurrentVendor);
998 Streamer.EmitIntValue(0, 1); // '\0'
999
1000 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
1001 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
1002
1003 // Size should have been accounted for already, now
1004 // emit each field as its type (ULEB or String)
1005 for (size_t i = 0; i < Contents.size(); ++i) {
1006 AttributeItem item = Contents[i];
1007 Streamer.EmitULEB128IntValue(item.Tag);
1008 switch (item.Type) {
1009 default: llvm_unreachable("Invalid attribute type");
1010 case AttributeItem::NumericAttribute:
1011 Streamer.EmitULEB128IntValue(item.IntValue);
1012 break;
1013 case AttributeItem::TextAttribute:
Charlie Turner8d433692014-11-27 12:13:56 +00001014 Streamer.EmitBytes(item.StringValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +00001015 Streamer.EmitIntValue(0, 1); // '\0'
1016 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001017 case AttributeItem::NumericAndTextAttributes:
1018 Streamer.EmitULEB128IntValue(item.IntValue);
Charlie Turner8d433692014-11-27 12:13:56 +00001019 Streamer.EmitBytes(item.StringValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001020 Streamer.EmitIntValue(0, 1); // '\0'
1021 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001022 }
1023 }
1024
1025 Contents.clear();
1026 FPU = ARM::INVALID_FPU;
1027}
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001028
1029void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1030 ARMELFStreamer &Streamer = getStreamer();
1031 if (!Streamer.IsThumb)
1032 return;
1033
1034 const MCSymbolData &SD = Streamer.getOrCreateSymbolData(Symbol);
Scott Douglass7650a9b2014-06-30 09:37:24 +00001035 unsigned Type = MCELF::GetType(SD);
1036 if (Type == ELF_STT_Func || Type == ELF_STT_GnuIFunc)
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001037 Streamer.EmitThumbFunc(Symbol);
1038}
1039
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001040void
1041ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1042 getStreamer().EmitFixup(S, FK_Data_4);
1043}
Rafael Espindola466d6632014-04-27 20:23:58 +00001044
1045void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1046 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
Rafael Espindola4a042942014-05-01 12:45:43 +00001047 const MCSymbol &Sym = SRE->getSymbol();
1048 if (!Sym.isDefined()) {
Rafael Espindola466d6632014-04-27 20:23:58 +00001049 getStreamer().EmitAssignment(Symbol, Value);
1050 return;
1051 }
1052 }
1053
1054 getStreamer().EmitThumbFunc(Symbol);
1055 getStreamer().EmitAssignment(Symbol, Value);
1056}
1057
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +00001058void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1059 getStreamer().emitInst(Inst, Suffix);
1060}
Logan Chien8cbb80d2013-10-28 17:51:12 +00001061
1062void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +00001063 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +00001064 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1065 ATS.finishAttributeSection();
1066
1067 MCELFStreamer::FinishImpl();
1068}
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001069
Logan Chien2bcc42c2013-01-30 15:39:04 +00001070inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
1071 unsigned Type,
1072 unsigned Flags,
1073 SectionKind Kind,
1074 const MCSymbol &Fn) {
1075 const MCSectionELF &FnSection =
1076 static_cast<const MCSectionELF &>(Fn.getSection());
1077
1078 // Create the name for new section
1079 StringRef FnSecName(FnSection.getSectionName());
1080 SmallString<128> EHSecName(Prefix);
1081 if (FnSecName != ".text") {
1082 EHSecName += FnSecName;
1083 }
1084
1085 // Get .ARM.extab or .ARM.exidx section
Rafael Espindola61e8ce32015-04-06 04:25:18 +00001086 const MCSymbol *Group = FnSection.getGroup();
1087 if (Group)
1088 Flags |= ELF::SHF_GROUP;
1089 const MCSectionELF *EHSection =
1090 getContext().getELFSection(EHSecName, Type, Flags, 0, Group,
1091 FnSection.getUniqueID(), nullptr, &FnSection);
1092
Logan Chiend8bb4b72013-04-16 12:02:21 +00001093 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001094
1095 // Switch to .ARM.extab or .ARM.exidx section
1096 SwitchSection(EHSection);
Rafael Espindola7b514962014-02-04 18:34:04 +00001097 EmitCodeAlignment(4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001098}
1099
1100inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1101 SwitchToEHSection(".ARM.extab",
1102 ELF::SHT_PROGBITS,
1103 ELF::SHF_ALLOC,
1104 SectionKind::getDataRel(),
1105 FnStart);
1106}
1107
1108inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1109 SwitchToEHSection(".ARM.exidx",
1110 ELF::SHT_ARM_EXIDX,
1111 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1112 SectionKind::getDataRel(),
1113 FnStart);
1114}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001115void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1116 MCDataFragment *Frag = getOrCreateDataFragment();
1117 Frag->getFixups().push_back(MCFixup::Create(Frag->getContents().size(), Expr,
1118 Kind));
1119}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001120
1121void ARMELFStreamer::Reset() {
Craig Topper062a2ba2014-04-25 05:30:21 +00001122 ExTab = nullptr;
1123 FnStart = nullptr;
1124 Personality = nullptr;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001125 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001126 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001127 FPOffset = 0;
1128 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001129 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001130 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001131 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001132
Logan Chien325823a2013-06-09 12:22:30 +00001133 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001134 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001135}
1136
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001137void ARMELFStreamer::emitFnStart() {
Craig Toppere73658d2014-04-28 04:05:08 +00001138 assert(FnStart == nullptr);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001139 FnStart = getContext().CreateTempSymbol();
1140 EmitLabel(FnStart);
1141}
1142
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001143void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001144 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001145
1146 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001147 if (!ExTab && !CantUnwind)
1148 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001149
1150 // Emit the exception index table entry
1151 SwitchToExIdxSection(*FnStart);
1152
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001153 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001154 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001155
1156 const MCSymbolRefExpr *FnStartRef =
1157 MCSymbolRefExpr::Create(FnStart,
1158 MCSymbolRefExpr::VK_ARM_PREL31,
1159 getContext());
1160
Rafael Espindola64e1af82013-07-02 15:49:13 +00001161 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001162
1163 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001164 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001165 } else if (ExTab) {
1166 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001167 const MCSymbolRefExpr *ExTabEntryRef =
1168 MCSymbolRefExpr::Create(ExTab,
1169 MCSymbolRefExpr::VK_ARM_PREL31,
1170 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001171 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001172 } else {
1173 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1174 // the second word of exception index table entry. The size of the unwind
1175 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001176 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001177 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001178 assert(Opcodes.size() == 4u &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001179 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
Christian Pirker39db7ec2014-05-13 16:44:30 +00001180 uint64_t Intval = Opcodes[0] |
1181 Opcodes[1] << 8 |
1182 Opcodes[2] << 16 |
1183 Opcodes[3] << 24;
1184 EmitIntValue(Intval, Opcodes.size());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001185 }
1186
Logan Chien4ea23b52013-05-10 16:17:24 +00001187 // Switch to the section containing FnStart
1188 SwitchSection(&FnStart->getSection());
1189
Logan Chien2bcc42c2013-01-30 15:39:04 +00001190 // Clean exception handling frame information
1191 Reset();
1192}
1193
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001194void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1195
1196// Add the R_ARM_NONE fixup at the same position
1197void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1198 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1199
1200 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1201 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1202
Rafael Espindola2be12812014-06-25 15:29:54 +00001203 visitUsedExpr(*PersonalityRef);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001204 MCDataFragment *DF = getOrCreateDataFragment();
1205 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1206 PersonalityRef,
1207 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001208}
1209
Logan Chien325823a2013-06-09 12:22:30 +00001210void ARMELFStreamer::FlushPendingOffset() {
1211 if (PendingOffset != 0) {
1212 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1213 PendingOffset = 0;
1214 }
1215}
1216
Logan Chienc931fce2013-07-02 12:43:27 +00001217void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001218 // Emit the unwind opcode to restore $sp.
1219 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001220 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001221 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1222 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001223 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001224 } else {
1225 FlushPendingOffset();
1226 }
1227
1228 // Finalize the unwind opcode sequence
1229 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001230
1231 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1232 // section. Thus, we don't have to create an entry in the .ARM.extab
1233 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001234 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001235 return;
1236
1237 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001238 SwitchToExTabSection(*FnStart);
1239
1240 // Create .ARM.extab label for offset in .ARM.exidx
1241 assert(!ExTab);
1242 ExTab = getContext().CreateTempSymbol();
1243 EmitLabel(ExTab);
1244
Logan Chien4ea23b52013-05-10 16:17:24 +00001245 // Emit personality
1246 if (Personality) {
1247 const MCSymbolRefExpr *PersonalityRef =
1248 MCSymbolRefExpr::Create(Personality,
1249 MCSymbolRefExpr::VK_ARM_PREL31,
1250 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001251
Rafael Espindola64e1af82013-07-02 15:49:13 +00001252 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001253 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001254
1255 // Emit unwind opcodes
Christian Pirker39db7ec2014-05-13 16:44:30 +00001256 assert((Opcodes.size() % 4) == 0 &&
1257 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1258 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1259 uint64_t Intval = Opcodes[I] |
1260 Opcodes[I + 1] << 8 |
1261 Opcodes[I + 2] << 16 |
1262 Opcodes[I + 3] << 24;
1263 EmitIntValue(Intval, 4);
1264 }
Logan Chienc931fce2013-07-02 12:43:27 +00001265
1266 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1267 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1268 // after the unwind opcodes. The handler data consists of several 32-bit
1269 // words, and should be terminated by zero.
1270 //
1271 // In case that the .handlerdata directive is not specified by the
1272 // programmer, we should emit zero to terminate the handler data.
1273 if (NoHandlerData && !Personality)
1274 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001275}
1276
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001277void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001278
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001279void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001280 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001281 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001282}
1283
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001284void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1285 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1286 PersonalityIndex = Index;
1287}
1288
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001289void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001290 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001291 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001292 "the operand of .setfp directive should be either $sp or $fp");
1293
1294 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001295 FPReg = NewFPReg;
1296
1297 if (NewSPReg == ARM::SP)
1298 FPOffset = SPOffset + Offset;
1299 else
1300 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001301}
1302
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +00001303void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1304 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1305 "the operand of .movsp cannot be either sp or pc");
1306 assert(FPReg == ARM::SP && "current FP must be SP");
1307
1308 FlushPendingOffset();
1309
1310 FPReg = Reg;
1311 FPOffset = SPOffset + Offset;
1312
1313 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1314 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1315}
1316
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001317void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001318 // Track the change of the $sp offset
1319 SPOffset -= Offset;
1320
1321 // To squash multiple .pad directives, we should delay the unwind opcode
1322 // until the .save, .vsave, .handlerdata, or .fnend directives.
1323 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001324}
1325
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001326void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001327 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001328 // Collect the registers in the register list
1329 unsigned Count = 0;
1330 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001331 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001332 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001333 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001334 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001335 unsigned Bit = (1u << Reg);
1336 if ((Mask & Bit) == 0) {
1337 Mask |= Bit;
1338 ++Count;
1339 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001340 }
Logan Chien325823a2013-06-09 12:22:30 +00001341
1342 // Track the change the $sp offset: For the .save directive, the
1343 // corresponding push instruction will decrease the $sp by (4 * Count).
1344 // For the .vsave directive, the corresponding vpush instruction will
1345 // decrease $sp by (8 * Count).
1346 SPOffset -= Count * (IsVector ? 8 : 4);
1347
1348 // Emit the opcode
1349 FlushPendingOffset();
1350 if (IsVector)
1351 UnwindOpAsm.EmitVFPRegSave(Mask);
1352 else
1353 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001354}
1355
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001356void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1357 const SmallVectorImpl<uint8_t> &Opcodes) {
1358 FlushPendingOffset();
1359 SPOffset = SPOffset - Offset;
1360 UnwindOpAsm.EmitRaw(Opcodes);
1361}
1362
Tim Northover5cc3dc82012-12-07 16:50:23 +00001363namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001364
Rafael Espindola73870dd2015-03-16 21:43:42 +00001365MCTargetStreamer *createARMTargetAsmStreamer(MCStreamer &S,
1366 formatted_raw_ostream &OS,
1367 MCInstPrinter *InstPrint,
1368 bool isVerboseAsm) {
1369 return new ARMTargetAsmStreamer(S, OS, *InstPrint, isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001370}
1371
Peter Collingbourne20c72592015-02-19 00:45:02 +00001372MCTargetStreamer *createARMNullTargetStreamer(MCStreamer &S) {
1373 return new ARMTargetStreamer(S);
1374}
1375
Rafael Espindolacd584a82015-03-19 01:50:16 +00001376MCTargetStreamer *createARMObjectTargetStreamer(MCStreamer &S,
1377 const MCSubtargetInfo &STI) {
1378 Triple TT(STI.getTargetTriple());
1379 if (TT.getObjectFormat() == Triple::ELF)
1380 return new ARMTargetELFStreamer(S);
1381 return new ARMTargetStreamer(S);
1382}
1383
Rafael Espindola7b61ddf2014-10-15 16:12:52 +00001384MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
Rafael Espindola5560a4c2015-04-14 22:14:34 +00001385 raw_pwrite_stream &OS,
1386 MCCodeEmitter *Emitter, bool RelaxAll,
1387 bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001388 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001389 // FIXME: This should eventually end up somewhere else where more
1390 // intelligent flag decisions can be made. For now we are just maintaining
1391 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1392 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1393
Tim Northover5cc3dc82012-12-07 16:50:23 +00001394 if (RelaxAll)
1395 S->getAssembler().setRelaxAll(true);
Tim Northover5cc3dc82012-12-07 16:50:23 +00001396 return S;
1397 }
1398
1399}
1400
1401