blob: f9403063dd6f98df9cee3bea5ab4bbdc64c86cd2 [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;
Rafael Espindola466d6632014-04-27 20:23:58 +0000143 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000144
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000145public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000146 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
147 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000148};
149
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000150ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
151 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000152 MCInstPrinter &InstPrinter,
153 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000154 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
155 IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000156void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
157void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
158void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
159void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
160 OS << "\t.personality " << Personality->getName() << '\n';
161}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000162void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
163 OS << "\t.personalityindex " << Index << '\n';
164}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000165void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
166void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
167 int64_t Offset) {
168 OS << "\t.setfp\t";
169 InstPrinter.printRegName(OS, FpReg);
170 OS << ", ";
171 InstPrinter.printRegName(OS, SpReg);
172 if (Offset)
173 OS << ", #" << Offset;
174 OS << '\n';
175}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000176void ARMTargetAsmStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
177 assert((Reg != ARM::SP && Reg != ARM::PC) &&
178 "the operand of .movsp cannot be either sp or pc");
179
180 OS << "\t.movsp\t";
181 InstPrinter.printRegName(OS, Reg);
182 if (Offset)
183 OS << ", #" << Offset;
184 OS << '\n';
185}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000186void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
187 OS << "\t.pad\t#" << Offset << '\n';
188}
189void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
190 bool isVector) {
191 assert(RegList.size() && "RegList should not be empty");
192 if (isVector)
193 OS << "\t.vsave\t{";
194 else
195 OS << "\t.save\t{";
196
197 InstPrinter.printRegName(OS, RegList[0]);
198
199 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
200 OS << ", ";
201 InstPrinter.printRegName(OS, RegList[i]);
202 }
203
204 OS << "}\n";
205}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000206void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
207}
208void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000209 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
210 if (IsVerboseAsm) {
211 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
212 if (!Name.empty())
213 OS << "\t@ " << Name;
214 }
215 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000216}
217void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
218 StringRef String) {
219 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000220 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000221 OS << "\t.cpu\t" << String.lower();
222 break;
223 default:
224 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000225 if (IsVerboseAsm) {
226 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
227 if (!Name.empty())
228 OS << "\t@ " << Name;
229 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000230 break;
231 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000232 OS << "\n";
233}
234void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
235 unsigned IntValue,
236 StringRef StringValue) {
237 switch (Attribute) {
238 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
239 case ARMBuildAttrs::compatibility:
240 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
241 if (!StringValue.empty())
242 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000243 if (IsVerboseAsm)
244 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000245 break;
246 }
247 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000248}
Logan Chien439e8f92013-12-11 17:16:25 +0000249void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
250 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
251}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000252void ARMTargetAsmStreamer::emitObjectArch(unsigned Arch) {
253 OS << "\t.object_arch\t" << GetArchName(Arch) << '\n';
254}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000255void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
256 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
257}
258void ARMTargetAsmStreamer::finishAttributeSection() {
259}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000260void
261ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
262 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
263}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000264
Rafael Espindola466d6632014-04-27 20:23:58 +0000265void ARMTargetAsmStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
266 OS << "\t.thumb_set\t" << *Symbol << ", " << *Value << '\n';
267}
268
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000269void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
270 OS << "\t.inst";
271 if (Suffix)
272 OS << "." << Suffix;
273 OS << "\t0x" << utohexstr(Inst) << "\n";
274}
275
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000276void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
277 const SmallVectorImpl<uint8_t> &Opcodes) {
278 OS << "\t.unwind_raw " << Offset;
279 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
280 OCE = Opcodes.end();
281 OCI != OCE; ++OCI)
282 OS << ", 0x" << utohexstr(*OCI);
283 OS << '\n';
284}
285
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000286class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000287private:
288 // This structure holds all attributes, accounting for
289 // their string/numeric value, so we can later emmit them
290 // in declaration order, keeping all in the same vector
291 struct AttributeItem {
292 enum {
293 HiddenAttribute = 0,
294 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000295 TextAttribute,
296 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000297 } Type;
298 unsigned Tag;
299 unsigned IntValue;
300 StringRef StringValue;
301
302 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
Charlie Turner8b2caa42015-01-05 13:12:17 +0000303 // The conformance tag must be emitted first when serialised
304 // into an object file. Specifically, the addenda to the ARM ABI
305 // states that (2.3.7.4):
306 //
307 // "To simplify recognition by consumers in the common case of
308 // claiming conformity for the whole file, this tag should be
309 // emitted first in a file-scope sub-subsection of the first
310 // public subsection of the attributes section."
311 //
312 // So it is special-cased in this comparison predicate when the
313 // attributes are sorted in finishAttributeSection().
314 return (RHS.Tag != ARMBuildAttrs::conformance) &&
315 ((LHS.Tag == ARMBuildAttrs::conformance) || (LHS.Tag < RHS.Tag));
Logan Chien8cbb80d2013-10-28 17:51:12 +0000316 }
317 };
318
319 StringRef CurrentVendor;
320 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000321 unsigned Arch;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000322 unsigned EmittedArch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000323 SmallVector<AttributeItem, 64> Contents;
324
325 const MCSection *AttributeSection;
326
Logan Chien8cbb80d2013-10-28 17:51:12 +0000327 AttributeItem *getAttributeItem(unsigned Attribute) {
328 for (size_t i = 0; i < Contents.size(); ++i)
329 if (Contents[i].Tag == Attribute)
330 return &Contents[i];
Craig Topper062a2ba2014-04-25 05:30:21 +0000331 return nullptr;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000332 }
333
334 void setAttributeItem(unsigned Attribute, unsigned Value,
335 bool OverwriteExisting) {
336 // Look for existing attribute item
337 if (AttributeItem *Item = getAttributeItem(Attribute)) {
338 if (!OverwriteExisting)
339 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000340 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000341 Item->IntValue = Value;
342 return;
343 }
344
345 // Create new attribute item
346 AttributeItem Item = {
347 AttributeItem::NumericAttribute,
348 Attribute,
349 Value,
350 StringRef("")
351 };
352 Contents.push_back(Item);
353 }
354
355 void setAttributeItem(unsigned Attribute, StringRef Value,
356 bool OverwriteExisting) {
357 // Look for existing attribute item
358 if (AttributeItem *Item = getAttributeItem(Attribute)) {
359 if (!OverwriteExisting)
360 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000361 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000362 Item->StringValue = Value;
363 return;
364 }
365
366 // Create new attribute item
367 AttributeItem Item = {
368 AttributeItem::TextAttribute,
369 Attribute,
370 0,
371 Value
372 };
373 Contents.push_back(Item);
374 }
375
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000376 void setAttributeItems(unsigned Attribute, unsigned IntValue,
377 StringRef StringValue, bool OverwriteExisting) {
378 // Look for existing attribute item
379 if (AttributeItem *Item = getAttributeItem(Attribute)) {
380 if (!OverwriteExisting)
381 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000382 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000383 Item->IntValue = IntValue;
384 Item->StringValue = StringValue;
385 return;
386 }
387
388 // Create new attribute item
389 AttributeItem Item = {
390 AttributeItem::NumericAndTextAttributes,
391 Attribute,
392 IntValue,
393 StringValue
394 };
395 Contents.push_back(Item);
396 }
397
Logan Chien439e8f92013-12-11 17:16:25 +0000398 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000399 void emitFPUDefaultAttributes();
400
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000401 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000402
Craig Topperca7e3e52014-03-10 03:19:03 +0000403 void emitFnStart() override;
404 void emitFnEnd() override;
405 void emitCantUnwind() override;
406 void emitPersonality(const MCSymbol *Personality) override;
407 void emitPersonalityIndex(unsigned Index) override;
408 void emitHandlerData() override;
409 void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0) override;
410 void emitMovSP(unsigned Reg, int64_t Offset = 0) override;
411 void emitPad(int64_t Offset) override;
412 void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
413 bool isVector) override;
414 void emitUnwindRaw(int64_t Offset,
415 const SmallVectorImpl<uint8_t> &Opcodes) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000416
Craig Topperca7e3e52014-03-10 03:19:03 +0000417 void switchVendor(StringRef Vendor) override;
418 void emitAttribute(unsigned Attribute, unsigned Value) override;
419 void emitTextAttribute(unsigned Attribute, StringRef String) override;
420 void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
421 StringRef StringValue) override;
422 void emitArch(unsigned Arch) override;
423 void emitObjectArch(unsigned Arch) override;
424 void emitFPU(unsigned FPU) override;
425 void emitInst(uint32_t Inst, char Suffix = '\0') override;
426 void finishAttributeSection() override;
Rafael Espindola4c6f6132014-04-27 17:10:46 +0000427 void emitLabel(MCSymbol *Symbol) override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000428
Craig Topperca7e3e52014-03-10 03:19:03 +0000429 void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE) override;
Rafael Espindola466d6632014-04-27 20:23:58 +0000430 void emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) override;
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000431
Logan Chien8cbb80d2013-10-28 17:51:12 +0000432 size_t calculateContentSize() const;
433
434public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000435 ARMTargetELFStreamer(MCStreamer &S)
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000436 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
437 Arch(ARM::INVALID_ARCH), EmittedArch(ARM::INVALID_ARCH),
Craig Topper062a2ba2014-04-25 05:30:21 +0000438 AttributeSection(nullptr) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000439};
440
Tim Northover5cc3dc82012-12-07 16:50:23 +0000441/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
442/// the appropriate points in the object files. These symbols are defined in the
443/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
444///
445/// In brief: $a, $t or $d should be emitted at the start of each contiguous
446/// region of ARM code, Thumb code or data in a section. In practice, this
447/// emission does not rely on explicit assembler directives but on inherent
448/// properties of the directives doing the emission (e.g. ".byte" is data, "add
449/// r0, r0, r0" an instruction).
450///
451/// As a result this system is orthogonal to the DataRegion infrastructure used
452/// by MachO. Beware!
453class ARMELFStreamer : public MCELFStreamer {
454public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000455 friend class ARMTargetELFStreamer;
456
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000457 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
458 MCCodeEmitter *Emitter, bool IsThumb)
459 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
460 MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000461 Reset();
462 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000463
464 ~ARMELFStreamer() {}
465
Craig Topperca7e3e52014-03-10 03:19:03 +0000466 void FinishImpl() override;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000467
Logan Chien2bcc42c2013-01-30 15:39:04 +0000468 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000469 void emitFnStart();
470 void emitFnEnd();
471 void emitCantUnwind();
472 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000473 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000474 void emitHandlerData();
475 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000476 void emitMovSP(unsigned Reg, int64_t Offset = 0);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000477 void emitPad(int64_t Offset);
478 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000479 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000480
Craig Topperca7e3e52014-03-10 03:19:03 +0000481 void ChangeSection(const MCSection *Section,
482 const MCExpr *Subsection) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000483 // We have to keep track of the mapping symbol state of any sections we
484 // use. Each one should start off as EMS_None, which is provided as the
485 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000486 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000487 LastEMS = LastMappingSymbols.lookup(Section);
488
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000489 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000490 }
491
492 /// This function is the one used to emit instruction data into the ELF
493 /// streamer. We override it to add the appropriate mapping symbol if
494 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000495 void EmitInstruction(const MCInst& Inst,
496 const MCSubtargetInfo &STI) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000497 if (IsThumb)
498 EmitThumbMappingSymbol();
499 else
500 EmitARMMappingSymbol();
501
David Woodhousee6c13e42014-01-28 23:12:42 +0000502 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000503 }
504
Craig Topperd25ff6f2014-03-10 03:22:59 +0000505 void emitInst(uint32_t Inst, char Suffix) {
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000506 unsigned Size;
507 char Buffer[4];
508 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
509
510 switch (Suffix) {
511 case '\0':
512 Size = 4;
513
514 assert(!IsThumb);
515 EmitARMMappingSymbol();
516 for (unsigned II = 0, IE = Size; II != IE; II++) {
517 const unsigned I = LittleEndian ? (Size - II - 1) : II;
518 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
519 }
520
521 break;
522 case 'n':
523 case 'w':
524 Size = (Suffix == 'n' ? 2 : 4);
525
526 assert(IsThumb);
527 EmitThumbMappingSymbol();
528 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
529 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
530 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
531 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
532 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
533 }
534
535 break;
536 default:
537 llvm_unreachable("Invalid Suffix");
538 }
539
540 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
541 }
542
Tim Northover5cc3dc82012-12-07 16:50:23 +0000543 /// This is one of the functions used to emit data into an ELF section, so the
544 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
545 /// necessary.
Craig Topperca7e3e52014-03-10 03:19:03 +0000546 void EmitBytes(StringRef Data) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000547 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000548 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000549 }
550
551 /// This is one of the functions used to emit data into an ELF section, so the
552 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
553 /// necessary.
Kevin Enderby96918bc2014-04-22 17:27:29 +0000554 void EmitValueImpl(const MCExpr *Value, unsigned Size,
555 const SMLoc &Loc) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000556 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000557 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000558 }
559
Craig Topperca7e3e52014-03-10 03:19:03 +0000560 void EmitAssemblerFlag(MCAssemblerFlag Flag) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000561 MCELFStreamer::EmitAssemblerFlag(Flag);
562
563 switch (Flag) {
564 case MCAF_SyntaxUnified:
565 return; // no-op here.
566 case MCAF_Code16:
567 IsThumb = true;
568 return; // Change to Thumb mode
569 case MCAF_Code32:
570 IsThumb = false;
571 return; // Change to ARM mode
572 case MCAF_Code64:
573 return;
574 case MCAF_SubsectionsViaSymbols:
575 return;
576 }
577 }
578
579private:
580 enum ElfMappingSymbol {
581 EMS_None,
582 EMS_ARM,
583 EMS_Thumb,
584 EMS_Data
585 };
586
587 void EmitDataMappingSymbol() {
588 if (LastEMS == EMS_Data) return;
589 EmitMappingSymbol("$d");
590 LastEMS = EMS_Data;
591 }
592
593 void EmitThumbMappingSymbol() {
594 if (LastEMS == EMS_Thumb) return;
595 EmitMappingSymbol("$t");
596 LastEMS = EMS_Thumb;
597 }
598
599 void EmitARMMappingSymbol() {
600 if (LastEMS == EMS_ARM) return;
601 EmitMappingSymbol("$a");
602 LastEMS = EMS_ARM;
603 }
604
605 void EmitMappingSymbol(StringRef Name) {
606 MCSymbol *Start = getContext().CreateTempSymbol();
607 EmitLabel(Start);
608
Chandler Carruth1d94e932012-12-08 03:10:14 +0000609 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000610 getContext().GetOrCreateSymbol(Name + "." +
611 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000612
613 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
614 MCELF::SetType(SD, ELF::STT_NOTYPE);
615 MCELF::SetBinding(SD, ELF::STB_LOCAL);
616 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000617 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000618
619 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
620 Symbol->setVariableValue(Value);
621 }
622
Craig Topperca7e3e52014-03-10 03:19:03 +0000623 void EmitThumbFunc(MCSymbol *Func) override {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000624 getAssembler().setIsThumbFunc(Func);
Rafael Espindolab60c8292014-04-29 12:46:50 +0000625 EmitSymbolAttribute(Func, MCSA_ELF_TypeFunction);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000626 }
627
Logan Chien2bcc42c2013-01-30 15:39:04 +0000628 // Helper functions for ARM exception handling directives
629 void Reset();
630
631 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000632 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000633 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000634
635 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
636 SectionKind Kind, const MCSymbol &Fn);
637 void SwitchToExTabSection(const MCSymbol &FnStart);
638 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000639
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000640 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
641
Tim Northover5cc3dc82012-12-07 16:50:23 +0000642 bool IsThumb;
643 int64_t MappingSymbolCounter;
644
645 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
646 ElfMappingSymbol LastEMS;
647
Logan Chien2bcc42c2013-01-30 15:39:04 +0000648 // ARM Exception Handling Frame Information
649 MCSymbol *ExTab;
650 MCSymbol *FnStart;
651 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000652 unsigned PersonalityIndex;
653 unsigned FPReg; // Frame pointer register
654 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
655 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
656 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000657 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000658 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000659 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000660 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000661};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000662} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000663
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000664ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000665 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000666}
667
668void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
669void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
670void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
671void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
672 getStreamer().emitPersonality(Personality);
673}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000674void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
675 getStreamer().emitPersonalityIndex(Index);
676}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000677void ARMTargetELFStreamer::emitHandlerData() {
678 getStreamer().emitHandlerData();
679}
680void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
681 int64_t Offset) {
682 getStreamer().emitSetFP(FpReg, SpReg, Offset);
683}
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +0000684void ARMTargetELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
685 getStreamer().emitMovSP(Reg, Offset);
686}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000687void ARMTargetELFStreamer::emitPad(int64_t Offset) {
688 getStreamer().emitPad(Offset);
689}
690void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
691 bool isVector) {
692 getStreamer().emitRegSave(RegList, isVector);
693}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000694void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
695 const SmallVectorImpl<uint8_t> &Opcodes) {
696 getStreamer().emitUnwindRaw(Offset, Opcodes);
697}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000698void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
699 assert(!Vendor.empty() && "Vendor cannot be empty.");
700
701 if (CurrentVendor == Vendor)
702 return;
703
704 if (!CurrentVendor.empty())
705 finishAttributeSection();
706
707 assert(Contents.empty() &&
708 ".ARM.attributes should be flushed before changing vendor");
709 CurrentVendor = Vendor;
710
711}
712void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
713 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
714}
715void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
716 StringRef Value) {
717 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
718}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000719void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
720 unsigned IntValue,
721 StringRef StringValue) {
722 setAttributeItems(Attribute, IntValue, StringValue,
723 /* OverwriteExisting= */ true);
724}
Logan Chien439e8f92013-12-11 17:16:25 +0000725void ARMTargetELFStreamer::emitArch(unsigned Value) {
726 Arch = Value;
727}
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000728void ARMTargetELFStreamer::emitObjectArch(unsigned Value) {
729 EmittedArch = Value;
730}
Logan Chien439e8f92013-12-11 17:16:25 +0000731void ARMTargetELFStreamer::emitArchDefaultAttributes() {
732 using namespace ARMBuildAttrs;
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000733
Logan Chien439e8f92013-12-11 17:16:25 +0000734 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
Saleem Abdulrasool4c4789b2014-01-30 04:46:41 +0000735 if (EmittedArch == ARM::INVALID_ARCH)
736 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
737 else
738 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(EmittedArch), false);
Logan Chien439e8f92013-12-11 17:16:25 +0000739
740 switch (Arch) {
741 case ARM::ARMV2:
742 case ARM::ARMV2A:
743 case ARM::ARMV3:
744 case ARM::ARMV3M:
745 case ARM::ARMV4:
746 case ARM::ARMV5:
747 setAttributeItem(ARM_ISA_use, Allowed, false);
748 break;
749
750 case ARM::ARMV4T:
751 case ARM::ARMV5T:
752 case ARM::ARMV5TE:
753 case ARM::ARMV6:
754 case ARM::ARMV6J:
755 setAttributeItem(ARM_ISA_use, Allowed, false);
756 setAttributeItem(THUMB_ISA_use, Allowed, false);
757 break;
758
759 case ARM::ARMV6T2:
760 setAttributeItem(ARM_ISA_use, Allowed, false);
761 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
762 break;
763
764 case ARM::ARMV6Z:
765 case ARM::ARMV6ZK:
766 setAttributeItem(ARM_ISA_use, Allowed, false);
767 setAttributeItem(THUMB_ISA_use, Allowed, false);
768 setAttributeItem(Virtualization_use, AllowTZ, false);
769 break;
770
771 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000772 setAttributeItem(THUMB_ISA_use, Allowed, false);
773 break;
774
775 case ARM::ARMV7:
776 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
777 break;
778
779 case ARM::ARMV7A:
780 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
781 setAttributeItem(ARM_ISA_use, Allowed, false);
782 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
783 break;
784
785 case ARM::ARMV7R:
786 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
787 setAttributeItem(ARM_ISA_use, Allowed, false);
788 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
789 break;
790
791 case ARM::ARMV7M:
792 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
793 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
794 break;
795
796 case ARM::ARMV8A:
797 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
798 setAttributeItem(ARM_ISA_use, Allowed, false);
799 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
800 setAttributeItem(MPextension_use, Allowed, false);
801 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
802 break;
803
804 case ARM::IWMMXT:
805 setAttributeItem(ARM_ISA_use, Allowed, false);
806 setAttributeItem(THUMB_ISA_use, Allowed, false);
807 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
808 break;
809
810 case ARM::IWMMXT2:
811 setAttributeItem(ARM_ISA_use, Allowed, false);
812 setAttributeItem(THUMB_ISA_use, Allowed, false);
813 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
814 break;
815
816 default:
817 report_fatal_error("Unknown Arch: " + Twine(Arch));
818 break;
819 }
820}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000821void ARMTargetELFStreamer::emitFPU(unsigned Value) {
822 FPU = Value;
823}
824void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
825 switch (FPU) {
826 case ARM::VFP:
827 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000828 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000829 ARMBuildAttrs::AllowFPv2,
830 /* OverwriteExisting= */ false);
831 break;
832
833 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000834 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000835 ARMBuildAttrs::AllowFPv3A,
836 /* OverwriteExisting= */ false);
837 break;
838
839 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000840 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000841 ARMBuildAttrs::AllowFPv3B,
842 /* OverwriteExisting= */ false);
843 break;
844
845 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000846 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000847 ARMBuildAttrs::AllowFPv4A,
848 /* OverwriteExisting= */ false);
849 break;
850
851 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000852 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000853 ARMBuildAttrs::AllowFPv4B,
854 /* OverwriteExisting= */ false);
855 break;
856
857 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000858 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000859 ARMBuildAttrs::AllowFPARMv8A,
860 /* OverwriteExisting= */ false);
861 break;
862
Oliver Stannard37e4daa2014-10-01 09:02:17 +0000863 // FPV5_D16 is identical to FP_ARMV8 except for the number of D registers, so
864 // uses the FP_ARMV8_D16 build attribute.
865 case ARM::FPV5_D16:
866 setAttributeItem(ARMBuildAttrs::FP_arch,
867 ARMBuildAttrs::AllowFPARMv8B,
868 /* OverwriteExisting= */ false);
869 break;
870
Logan Chien8cbb80d2013-10-28 17:51:12 +0000871 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000872 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000873 ARMBuildAttrs::AllowFPv3A,
874 /* OverwriteExisting= */ false);
875 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
876 ARMBuildAttrs::AllowNeon,
877 /* OverwriteExisting= */ false);
878 break;
879
880 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000881 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000882 ARMBuildAttrs::AllowFPv4A,
883 /* OverwriteExisting= */ false);
884 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
885 ARMBuildAttrs::AllowNeon2,
886 /* OverwriteExisting= */ false);
887 break;
888
889 case ARM::NEON_FP_ARMV8:
890 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000891 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000892 ARMBuildAttrs::AllowFPARMv8A,
893 /* OverwriteExisting= */ false);
894 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
895 ARMBuildAttrs::AllowNeonARMv8,
896 /* OverwriteExisting= */ false);
897 break;
898
Logan Chien05ae7442014-01-02 15:50:02 +0000899 case ARM::SOFTVFP:
900 break;
901
Logan Chien8cbb80d2013-10-28 17:51:12 +0000902 default:
903 report_fatal_error("Unknown FPU: " + Twine(FPU));
904 break;
905 }
906}
907size_t ARMTargetELFStreamer::calculateContentSize() const {
908 size_t Result = 0;
909 for (size_t i = 0; i < Contents.size(); ++i) {
910 AttributeItem item = Contents[i];
911 switch (item.Type) {
912 case AttributeItem::HiddenAttribute:
913 break;
914 case AttributeItem::NumericAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000915 Result += getULEB128Size(item.Tag);
916 Result += getULEB128Size(item.IntValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000917 break;
918 case AttributeItem::TextAttribute:
Logan Chien5b776b72014-02-22 14:00:39 +0000919 Result += getULEB128Size(item.Tag);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000920 Result += item.StringValue.size() + 1; // string + '\0'
921 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000922 case AttributeItem::NumericAndTextAttributes:
Logan Chien5b776b72014-02-22 14:00:39 +0000923 Result += getULEB128Size(item.Tag);
924 Result += getULEB128Size(item.IntValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000925 Result += item.StringValue.size() + 1; // string + '\0';
926 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000927 }
928 }
929 return Result;
930}
931void ARMTargetELFStreamer::finishAttributeSection() {
932 // <format-version>
933 // [ <section-length> "vendor-name"
934 // [ <file-tag> <size> <attribute>*
935 // | <section-tag> <size> <section-number>* 0 <attribute>*
936 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
937 // ]+
938 // ]*
939
940 if (FPU != ARM::INVALID_FPU)
941 emitFPUDefaultAttributes();
942
Logan Chien439e8f92013-12-11 17:16:25 +0000943 if (Arch != ARM::INVALID_ARCH)
944 emitArchDefaultAttributes();
945
Logan Chien8cbb80d2013-10-28 17:51:12 +0000946 if (Contents.empty())
947 return;
948
949 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
950
951 ARMELFStreamer &Streamer = getStreamer();
952
953 // Switch to .ARM.attributes section
954 if (AttributeSection) {
955 Streamer.SwitchSection(AttributeSection);
956 } else {
957 AttributeSection =
958 Streamer.getContext().getELFSection(".ARM.attributes",
959 ELF::SHT_ARM_ATTRIBUTES,
960 0,
961 SectionKind::getMetadata());
962 Streamer.SwitchSection(AttributeSection);
963
964 // Format version
965 Streamer.EmitIntValue(0x41, 1);
966 }
967
968 // Vendor size + Vendor name + '\0'
969 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
970
971 // Tag + Tag Size
972 const size_t TagHeaderSize = 1 + 4;
973
974 const size_t ContentsSize = calculateContentSize();
975
976 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
977 Streamer.EmitBytes(CurrentVendor);
978 Streamer.EmitIntValue(0, 1); // '\0'
979
980 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
981 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
982
983 // Size should have been accounted for already, now
984 // emit each field as its type (ULEB or String)
985 for (size_t i = 0; i < Contents.size(); ++i) {
986 AttributeItem item = Contents[i];
987 Streamer.EmitULEB128IntValue(item.Tag);
988 switch (item.Type) {
989 default: llvm_unreachable("Invalid attribute type");
990 case AttributeItem::NumericAttribute:
991 Streamer.EmitULEB128IntValue(item.IntValue);
992 break;
993 case AttributeItem::TextAttribute:
Charlie Turner8d433692014-11-27 12:13:56 +0000994 Streamer.EmitBytes(item.StringValue);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000995 Streamer.EmitIntValue(0, 1); // '\0'
996 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000997 case AttributeItem::NumericAndTextAttributes:
998 Streamer.EmitULEB128IntValue(item.IntValue);
Charlie Turner8d433692014-11-27 12:13:56 +0000999 Streamer.EmitBytes(item.StringValue);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +00001000 Streamer.EmitIntValue(0, 1); // '\0'
1001 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +00001002 }
1003 }
1004
1005 Contents.clear();
1006 FPU = ARM::INVALID_FPU;
1007}
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001008
1009void ARMTargetELFStreamer::emitLabel(MCSymbol *Symbol) {
1010 ARMELFStreamer &Streamer = getStreamer();
1011 if (!Streamer.IsThumb)
1012 return;
1013
1014 const MCSymbolData &SD = Streamer.getOrCreateSymbolData(Symbol);
Scott Douglass7650a9b2014-06-30 09:37:24 +00001015 unsigned Type = MCELF::GetType(SD);
1016 if (Type == ELF_STT_Func || Type == ELF_STT_GnuIFunc)
Rafael Espindola4c6f6132014-04-27 17:10:46 +00001017 Streamer.EmitThumbFunc(Symbol);
1018}
1019
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001020void
1021ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
1022 getStreamer().EmitFixup(S, FK_Data_4);
1023}
Rafael Espindola466d6632014-04-27 20:23:58 +00001024
1025void ARMTargetELFStreamer::emitThumbSet(MCSymbol *Symbol, const MCExpr *Value) {
1026 if (const MCSymbolRefExpr *SRE = dyn_cast<MCSymbolRefExpr>(Value)) {
Rafael Espindola4a042942014-05-01 12:45:43 +00001027 const MCSymbol &Sym = SRE->getSymbol();
1028 if (!Sym.isDefined()) {
Rafael Espindola466d6632014-04-27 20:23:58 +00001029 getStreamer().EmitAssignment(Symbol, Value);
1030 return;
1031 }
1032 }
1033
1034 getStreamer().EmitThumbFunc(Symbol);
1035 getStreamer().EmitAssignment(Symbol, Value);
1036}
1037
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +00001038void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
1039 getStreamer().emitInst(Inst, Suffix);
1040}
Logan Chien8cbb80d2013-10-28 17:51:12 +00001041
1042void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +00001043 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +00001044 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
1045 ATS.finishAttributeSection();
1046
1047 MCELFStreamer::FinishImpl();
1048}
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001049
Logan Chien2bcc42c2013-01-30 15:39:04 +00001050inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
1051 unsigned Type,
1052 unsigned Flags,
1053 SectionKind Kind,
1054 const MCSymbol &Fn) {
1055 const MCSectionELF &FnSection =
1056 static_cast<const MCSectionELF &>(Fn.getSection());
1057
1058 // Create the name for new section
1059 StringRef FnSecName(FnSection.getSectionName());
1060 SmallString<128> EHSecName(Prefix);
1061 if (FnSecName != ".text") {
1062 EHSecName += FnSecName;
1063 }
1064
1065 // Get .ARM.extab or .ARM.exidx section
Craig Topper062a2ba2014-04-25 05:30:21 +00001066 const MCSectionELF *EHSection = nullptr;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001067 if (const MCSymbol *Group = FnSection.getGroup()) {
1068 EHSection = getContext().getELFSection(
1069 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
1070 FnSection.getEntrySize(), Group->getName());
1071 } else {
1072 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
1073 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001074 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001075
1076 // Switch to .ARM.extab or .ARM.exidx section
1077 SwitchSection(EHSection);
Rafael Espindola7b514962014-02-04 18:34:04 +00001078 EmitCodeAlignment(4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001079}
1080
1081inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1082 SwitchToEHSection(".ARM.extab",
1083 ELF::SHT_PROGBITS,
1084 ELF::SHF_ALLOC,
1085 SectionKind::getDataRel(),
1086 FnStart);
1087}
1088
1089inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1090 SwitchToEHSection(".ARM.exidx",
1091 ELF::SHT_ARM_EXIDX,
1092 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1093 SectionKind::getDataRel(),
1094 FnStart);
1095}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001096void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1097 MCDataFragment *Frag = getOrCreateDataFragment();
1098 Frag->getFixups().push_back(MCFixup::Create(Frag->getContents().size(), Expr,
1099 Kind));
1100}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001101
1102void ARMELFStreamer::Reset() {
Craig Topper062a2ba2014-04-25 05:30:21 +00001103 ExTab = nullptr;
1104 FnStart = nullptr;
1105 Personality = nullptr;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001106 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001107 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001108 FPOffset = 0;
1109 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001110 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001111 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001112 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001113
Logan Chien325823a2013-06-09 12:22:30 +00001114 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001115 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001116}
1117
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001118void ARMELFStreamer::emitFnStart() {
Craig Toppere73658d2014-04-28 04:05:08 +00001119 assert(FnStart == nullptr);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001120 FnStart = getContext().CreateTempSymbol();
1121 EmitLabel(FnStart);
1122}
1123
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001124void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001125 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001126
1127 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001128 if (!ExTab && !CantUnwind)
1129 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001130
1131 // Emit the exception index table entry
1132 SwitchToExIdxSection(*FnStart);
1133
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001134 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001135 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001136
1137 const MCSymbolRefExpr *FnStartRef =
1138 MCSymbolRefExpr::Create(FnStart,
1139 MCSymbolRefExpr::VK_ARM_PREL31,
1140 getContext());
1141
Rafael Espindola64e1af82013-07-02 15:49:13 +00001142 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001143
1144 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001145 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001146 } else if (ExTab) {
1147 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001148 const MCSymbolRefExpr *ExTabEntryRef =
1149 MCSymbolRefExpr::Create(ExTab,
1150 MCSymbolRefExpr::VK_ARM_PREL31,
1151 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001152 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001153 } else {
1154 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1155 // the second word of exception index table entry. The size of the unwind
1156 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001157 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001158 "Compact model must use __aeabi_unwind_cpp_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001159 assert(Opcodes.size() == 4u &&
Jonathan Roelofs4971b402014-05-15 02:24:50 +00001160 "Unwind opcode size for __aeabi_unwind_cpp_pr0 must be equal to 4");
Christian Pirker39db7ec2014-05-13 16:44:30 +00001161 uint64_t Intval = Opcodes[0] |
1162 Opcodes[1] << 8 |
1163 Opcodes[2] << 16 |
1164 Opcodes[3] << 24;
1165 EmitIntValue(Intval, Opcodes.size());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001166 }
1167
Logan Chien4ea23b52013-05-10 16:17:24 +00001168 // Switch to the section containing FnStart
1169 SwitchSection(&FnStart->getSection());
1170
Logan Chien2bcc42c2013-01-30 15:39:04 +00001171 // Clean exception handling frame information
1172 Reset();
1173}
1174
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001175void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1176
1177// Add the R_ARM_NONE fixup at the same position
1178void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1179 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1180
1181 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1182 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1183
Rafael Espindola2be12812014-06-25 15:29:54 +00001184 visitUsedExpr(*PersonalityRef);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001185 MCDataFragment *DF = getOrCreateDataFragment();
1186 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1187 PersonalityRef,
1188 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001189}
1190
Logan Chien325823a2013-06-09 12:22:30 +00001191void ARMELFStreamer::FlushPendingOffset() {
1192 if (PendingOffset != 0) {
1193 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1194 PendingOffset = 0;
1195 }
1196}
1197
Logan Chienc931fce2013-07-02 12:43:27 +00001198void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001199 // Emit the unwind opcode to restore $sp.
1200 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001201 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001202 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1203 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001204 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001205 } else {
1206 FlushPendingOffset();
1207 }
1208
1209 // Finalize the unwind opcode sequence
1210 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001211
1212 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1213 // section. Thus, we don't have to create an entry in the .ARM.extab
1214 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001215 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001216 return;
1217
1218 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001219 SwitchToExTabSection(*FnStart);
1220
1221 // Create .ARM.extab label for offset in .ARM.exidx
1222 assert(!ExTab);
1223 ExTab = getContext().CreateTempSymbol();
1224 EmitLabel(ExTab);
1225
Logan Chien4ea23b52013-05-10 16:17:24 +00001226 // Emit personality
1227 if (Personality) {
1228 const MCSymbolRefExpr *PersonalityRef =
1229 MCSymbolRefExpr::Create(Personality,
1230 MCSymbolRefExpr::VK_ARM_PREL31,
1231 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001232
Rafael Espindola64e1af82013-07-02 15:49:13 +00001233 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001234 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001235
1236 // Emit unwind opcodes
Christian Pirker39db7ec2014-05-13 16:44:30 +00001237 assert((Opcodes.size() % 4) == 0 &&
1238 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be multiple of 4");
1239 for (unsigned I = 0; I != Opcodes.size(); I += 4) {
1240 uint64_t Intval = Opcodes[I] |
1241 Opcodes[I + 1] << 8 |
1242 Opcodes[I + 2] << 16 |
1243 Opcodes[I + 3] << 24;
1244 EmitIntValue(Intval, 4);
1245 }
Logan Chienc931fce2013-07-02 12:43:27 +00001246
1247 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1248 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1249 // after the unwind opcodes. The handler data consists of several 32-bit
1250 // words, and should be terminated by zero.
1251 //
1252 // In case that the .handlerdata directive is not specified by the
1253 // programmer, we should emit zero to terminate the handler data.
1254 if (NoHandlerData && !Personality)
1255 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001256}
1257
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001258void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001259
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001260void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001261 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001262 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001263}
1264
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001265void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1266 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1267 PersonalityIndex = Index;
1268}
1269
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001270void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001271 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001272 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001273 "the operand of .setfp directive should be either $sp or $fp");
1274
1275 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001276 FPReg = NewFPReg;
1277
1278 if (NewSPReg == ARM::SP)
1279 FPOffset = SPOffset + Offset;
1280 else
1281 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001282}
1283
Saleem Abdulrasool5d962d32014-01-30 04:46:24 +00001284void ARMELFStreamer::emitMovSP(unsigned Reg, int64_t Offset) {
1285 assert((Reg != ARM::SP && Reg != ARM::PC) &&
1286 "the operand of .movsp cannot be either sp or pc");
1287 assert(FPReg == ARM::SP && "current FP must be SP");
1288
1289 FlushPendingOffset();
1290
1291 FPReg = Reg;
1292 FPOffset = SPOffset + Offset;
1293
1294 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
1295 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
1296}
1297
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001298void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001299 // Track the change of the $sp offset
1300 SPOffset -= Offset;
1301
1302 // To squash multiple .pad directives, we should delay the unwind opcode
1303 // until the .save, .vsave, .handlerdata, or .fnend directives.
1304 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001305}
1306
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001307void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001308 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001309 // Collect the registers in the register list
1310 unsigned Count = 0;
1311 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001312 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001313 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001314 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001315 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001316 unsigned Bit = (1u << Reg);
1317 if ((Mask & Bit) == 0) {
1318 Mask |= Bit;
1319 ++Count;
1320 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001321 }
Logan Chien325823a2013-06-09 12:22:30 +00001322
1323 // Track the change the $sp offset: For the .save directive, the
1324 // corresponding push instruction will decrease the $sp by (4 * Count).
1325 // For the .vsave directive, the corresponding vpush instruction will
1326 // decrease $sp by (8 * Count).
1327 SPOffset -= Count * (IsVector ? 8 : 4);
1328
1329 // Emit the opcode
1330 FlushPendingOffset();
1331 if (IsVector)
1332 UnwindOpAsm.EmitVFPRegSave(Mask);
1333 else
1334 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001335}
1336
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001337void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1338 const SmallVectorImpl<uint8_t> &Opcodes) {
1339 FlushPendingOffset();
1340 SPOffset = SPOffset - Offset;
1341 UnwindOpAsm.EmitRaw(Opcodes);
1342}
1343
Tim Northover5cc3dc82012-12-07 16:50:23 +00001344namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001345
1346MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
Rafael Espindola566fcfe2014-05-07 13:00:43 +00001347 bool isVerboseAsm, bool useDwarfDirectory,
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001348 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1349 MCAsmBackend *TAB, bool ShowInst) {
Rafael Espindola566fcfe2014-05-07 13:00:43 +00001350 MCStreamer *S = llvm::createAsmStreamer(
1351 Ctx, OS, isVerboseAsm, useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001352 new ARMTargetAsmStreamer(*S, OS, *InstPrint, isVerboseAsm);
1353 return S;
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001354}
1355
Rafael Espindola1fc003e2014-06-20 13:11:28 +00001356MCStreamer *createARMNullStreamer(MCContext &Ctx) {
1357 MCStreamer *S = llvm::createNullStreamer(Ctx);
1358 new ARMTargetStreamer(*S);
1359 return S;
1360}
1361
Rafael Espindola7b61ddf2014-10-15 16:12:52 +00001362MCELFStreamer *createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1363 raw_ostream &OS, MCCodeEmitter *Emitter,
1364 bool RelaxAll, bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001365 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
1366 new ARMTargetELFStreamer(*S);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001367 // FIXME: This should eventually end up somewhere else where more
1368 // intelligent flag decisions can be made. For now we are just maintaining
1369 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1370 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1371
Tim Northover5cc3dc82012-12-07 16:50:23 +00001372 if (RelaxAll)
1373 S->getAssembler().setRelaxAll(true);
Tim Northover5cc3dc82012-12-07 16:50:23 +00001374 return S;
1375 }
1376
1377}
1378
1379