blob: c2e3503278bba645c0a2a81ba03bf20da050f376 [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"
Tim Northover5cc3dc82012-12-07 16:50:23 +000020#include "llvm/ADT/SmallPtrSet.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"
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"
Tim Northover5cc3dc82012-12-07 16:50:23 +000046#include "llvm/Support/raw_ostream.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000047#include <algorithm>
Tim Northover5cc3dc82012-12-07 16:50:23 +000048
49using namespace llvm;
50
Logan Chiend8bb4b72013-04-16 12:02:21 +000051static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000052 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
53 "Invalid personality index");
Logan Chiend8bb4b72013-04-16 12:02:21 +000054 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
55}
56
Logan Chien8cbb80d2013-10-28 17:51:12 +000057static const char *GetFPUName(unsigned ID) {
58 switch (ID) {
59 default:
60 llvm_unreachable("Unknown FPU kind");
61 break;
62#define ARM_FPU_NAME(NAME, ID) case ARM::ID: return NAME;
63#include "ARMFPUName.def"
64 }
65 return NULL;
66}
67
Logan Chien439e8f92013-12-11 17:16:25 +000068static const char *GetArchName(unsigned ID) {
69 switch (ID) {
70 default:
71 llvm_unreachable("Unknown ARCH kind");
72 break;
73#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
74 case ARM::ID: return NAME;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +000075#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +000076#include "ARMArchName.def"
77 }
78 return NULL;
79}
80
81static const char *GetArchDefaultCPUName(unsigned ID) {
82 switch (ID) {
83 default:
84 llvm_unreachable("Unknown ARCH kind");
85 break;
86#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
87 case ARM::ID: return DEFAULT_CPU_NAME;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +000088#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +000089#include "ARMArchName.def"
90 }
91 return NULL;
92}
93
94static unsigned GetArchDefaultCPUArch(unsigned ID) {
95 switch (ID) {
96 default:
97 llvm_unreachable("Unknown ARCH kind");
98 break;
99#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
100 case ARM::ID: return ARMBuildAttrs::DEFAULT_CPU_ARCH;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +0000101#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +0000102#include "ARMArchName.def"
103 }
104 return 0;
105}
106
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000107void ARMTargetStreamer::anchor() {}
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000108ARMTargetStreamer::ARMTargetStreamer(MCStreamer &S) : MCTargetStreamer(S) {}
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000109
Tim Northover5cc3dc82012-12-07 16:50:23 +0000110namespace {
111
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000112class ARMELFStreamer;
113
114class ARMTargetAsmStreamer : public ARMTargetStreamer {
115 formatted_raw_ostream &OS;
116 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000117 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000118
119 virtual void emitFnStart();
120 virtual void emitFnEnd();
121 virtual void emitCantUnwind();
122 virtual void emitPersonality(const MCSymbol *Personality);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000123 virtual void emitPersonalityIndex(unsigned Index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000124 virtual void emitHandlerData();
125 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
126 virtual void emitPad(int64_t Offset);
127 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
128 bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000129 virtual void emitUnwindRaw(int64_t Offset,
130 const SmallVectorImpl<uint8_t> &Opcodes);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000131
Logan Chien8cbb80d2013-10-28 17:51:12 +0000132 virtual void switchVendor(StringRef Vendor);
133 virtual void emitAttribute(unsigned Attribute, unsigned Value);
134 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000135 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
136 StringRef StrinValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000137 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000138 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000139 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000140 virtual void finishAttributeSection();
141
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000142 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
143
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000144public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000145 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
146 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000147};
148
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000149ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
150 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000151 MCInstPrinter &InstPrinter,
152 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000153 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
154 IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000155void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
156void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
157void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
158void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
159 OS << "\t.personality " << Personality->getName() << '\n';
160}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000161void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
162 OS << "\t.personalityindex " << Index << '\n';
163}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000164void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
165void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
166 int64_t Offset) {
167 OS << "\t.setfp\t";
168 InstPrinter.printRegName(OS, FpReg);
169 OS << ", ";
170 InstPrinter.printRegName(OS, SpReg);
171 if (Offset)
172 OS << ", #" << Offset;
173 OS << '\n';
174}
175void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
176 OS << "\t.pad\t#" << Offset << '\n';
177}
178void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
179 bool isVector) {
180 assert(RegList.size() && "RegList should not be empty");
181 if (isVector)
182 OS << "\t.vsave\t{";
183 else
184 OS << "\t.save\t{";
185
186 InstPrinter.printRegName(OS, RegList[0]);
187
188 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
189 OS << ", ";
190 InstPrinter.printRegName(OS, RegList[i]);
191 }
192
193 OS << "}\n";
194}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000195void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
196}
197void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000198 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
199 if (IsVerboseAsm) {
200 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
201 if (!Name.empty())
202 OS << "\t@ " << Name;
203 }
204 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000205}
206void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
207 StringRef String) {
208 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000209 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000210 OS << "\t.cpu\t" << String.lower();
211 break;
212 default:
213 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000214 if (IsVerboseAsm) {
215 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
216 if (!Name.empty())
217 OS << "\t@ " << Name;
218 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000219 break;
220 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000221 OS << "\n";
222}
223void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
224 unsigned IntValue,
225 StringRef StringValue) {
226 switch (Attribute) {
227 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
228 case ARMBuildAttrs::compatibility:
229 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
230 if (!StringValue.empty())
231 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000232 if (IsVerboseAsm)
233 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000234 break;
235 }
236 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000237}
Logan Chien439e8f92013-12-11 17:16:25 +0000238void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
239 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
240}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000241void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
242 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
243}
244void ARMTargetAsmStreamer::finishAttributeSection() {
245}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000246void
247ARMTargetAsmStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
248 OS << "\t.tlsdescseq\t" << S->getSymbol().getName();
249}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000250
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000251void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
252 OS << "\t.inst";
253 if (Suffix)
254 OS << "." << Suffix;
255 OS << "\t0x" << utohexstr(Inst) << "\n";
256}
257
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000258void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
259 const SmallVectorImpl<uint8_t> &Opcodes) {
260 OS << "\t.unwind_raw " << Offset;
261 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
262 OCE = Opcodes.end();
263 OCI != OCE; ++OCI)
264 OS << ", 0x" << utohexstr(*OCI);
265 OS << '\n';
266}
267
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000268class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000269private:
270 // This structure holds all attributes, accounting for
271 // their string/numeric value, so we can later emmit them
272 // in declaration order, keeping all in the same vector
273 struct AttributeItem {
274 enum {
275 HiddenAttribute = 0,
276 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000277 TextAttribute,
278 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000279 } Type;
280 unsigned Tag;
281 unsigned IntValue;
282 StringRef StringValue;
283
284 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
285 return (LHS.Tag < RHS.Tag);
286 }
287 };
288
289 StringRef CurrentVendor;
290 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000291 unsigned Arch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000292 SmallVector<AttributeItem, 64> Contents;
293
294 const MCSection *AttributeSection;
295
296 // FIXME: this should be in a more generic place, but
297 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
298 static size_t getULEBSize(int Value) {
299 size_t Size = 0;
300 do {
301 Value >>= 7;
302 Size += sizeof(int8_t); // Is this really necessary?
303 } while (Value);
304 return Size;
305 }
306
307 AttributeItem *getAttributeItem(unsigned Attribute) {
308 for (size_t i = 0; i < Contents.size(); ++i)
309 if (Contents[i].Tag == Attribute)
310 return &Contents[i];
311 return 0;
312 }
313
314 void setAttributeItem(unsigned Attribute, unsigned Value,
315 bool OverwriteExisting) {
316 // Look for existing attribute item
317 if (AttributeItem *Item = getAttributeItem(Attribute)) {
318 if (!OverwriteExisting)
319 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000320 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000321 Item->IntValue = Value;
322 return;
323 }
324
325 // Create new attribute item
326 AttributeItem Item = {
327 AttributeItem::NumericAttribute,
328 Attribute,
329 Value,
330 StringRef("")
331 };
332 Contents.push_back(Item);
333 }
334
335 void setAttributeItem(unsigned Attribute, StringRef Value,
336 bool OverwriteExisting) {
337 // Look for existing attribute item
338 if (AttributeItem *Item = getAttributeItem(Attribute)) {
339 if (!OverwriteExisting)
340 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000341 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000342 Item->StringValue = Value;
343 return;
344 }
345
346 // Create new attribute item
347 AttributeItem Item = {
348 AttributeItem::TextAttribute,
349 Attribute,
350 0,
351 Value
352 };
353 Contents.push_back(Item);
354 }
355
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000356 void setAttributeItems(unsigned Attribute, unsigned IntValue,
357 StringRef StringValue, bool OverwriteExisting) {
358 // Look for existing attribute item
359 if (AttributeItem *Item = getAttributeItem(Attribute)) {
360 if (!OverwriteExisting)
361 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000362 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000363 Item->IntValue = IntValue;
364 Item->StringValue = StringValue;
365 return;
366 }
367
368 // Create new attribute item
369 AttributeItem Item = {
370 AttributeItem::NumericAndTextAttributes,
371 Attribute,
372 IntValue,
373 StringValue
374 };
375 Contents.push_back(Item);
376 }
377
Logan Chien439e8f92013-12-11 17:16:25 +0000378 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000379 void emitFPUDefaultAttributes();
380
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000381 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000382
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000383 virtual void emitFnStart();
384 virtual void emitFnEnd();
385 virtual void emitCantUnwind();
386 virtual void emitPersonality(const MCSymbol *Personality);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000387 virtual void emitPersonalityIndex(unsigned Index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000388 virtual void emitHandlerData();
389 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
390 virtual void emitPad(int64_t Offset);
391 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
392 bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000393 virtual void emitUnwindRaw(int64_t Offset,
394 const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000395
396 virtual void switchVendor(StringRef Vendor);
397 virtual void emitAttribute(unsigned Attribute, unsigned Value);
398 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000399 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
400 StringRef StringValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000401 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000402 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000403 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000404 virtual void finishAttributeSection();
405
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000406 virtual void AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *SRE);
407
Logan Chien8cbb80d2013-10-28 17:51:12 +0000408 size_t calculateContentSize() const;
409
410public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000411 ARMTargetELFStreamer(MCStreamer &S)
412 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
413 Arch(ARM::INVALID_ARCH), AttributeSection(0) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000414};
415
Tim Northover5cc3dc82012-12-07 16:50:23 +0000416/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
417/// the appropriate points in the object files. These symbols are defined in the
418/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
419///
420/// In brief: $a, $t or $d should be emitted at the start of each contiguous
421/// region of ARM code, Thumb code or data in a section. In practice, this
422/// emission does not rely on explicit assembler directives but on inherent
423/// properties of the directives doing the emission (e.g. ".byte" is data, "add
424/// r0, r0, r0" an instruction).
425///
426/// As a result this system is orthogonal to the DataRegion infrastructure used
427/// by MachO. Beware!
428class ARMELFStreamer : public MCELFStreamer {
429public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000430 friend class ARMTargetELFStreamer;
431
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000432 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
433 MCCodeEmitter *Emitter, bool IsThumb)
434 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
435 MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000436 Reset();
437 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000438
439 ~ARMELFStreamer() {}
440
Logan Chien8cbb80d2013-10-28 17:51:12 +0000441 virtual void FinishImpl();
442
Logan Chien2bcc42c2013-01-30 15:39:04 +0000443 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000444 void emitFnStart();
445 void emitFnEnd();
446 void emitCantUnwind();
447 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000448 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000449 void emitHandlerData();
450 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
451 void emitPad(int64_t Offset);
452 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000453 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000454
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000455 virtual void ChangeSection(const MCSection *Section,
456 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000457 // We have to keep track of the mapping symbol state of any sections we
458 // use. Each one should start off as EMS_None, which is provided as the
459 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000460 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000461 LastEMS = LastMappingSymbols.lookup(Section);
462
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000463 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000464 }
465
466 /// This function is the one used to emit instruction data into the ELF
467 /// streamer. We override it to add the appropriate mapping symbol if
468 /// necessary.
David Woodhousee6c13e42014-01-28 23:12:42 +0000469 virtual void EmitInstruction(const MCInst& Inst, const MCSubtargetInfo &STI) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000470 if (IsThumb)
471 EmitThumbMappingSymbol();
472 else
473 EmitARMMappingSymbol();
474
David Woodhousee6c13e42014-01-28 23:12:42 +0000475 MCELFStreamer::EmitInstruction(Inst, STI);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000476 }
477
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000478 virtual void emitInst(uint32_t Inst, char Suffix) {
479 unsigned Size;
480 char Buffer[4];
481 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
482
483 switch (Suffix) {
484 case '\0':
485 Size = 4;
486
487 assert(!IsThumb);
488 EmitARMMappingSymbol();
489 for (unsigned II = 0, IE = Size; II != IE; II++) {
490 const unsigned I = LittleEndian ? (Size - II - 1) : II;
491 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
492 }
493
494 break;
495 case 'n':
496 case 'w':
497 Size = (Suffix == 'n' ? 2 : 4);
498
499 assert(IsThumb);
500 EmitThumbMappingSymbol();
501 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
502 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
503 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
504 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
505 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
506 }
507
508 break;
509 default:
510 llvm_unreachable("Invalid Suffix");
511 }
512
513 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
514 }
515
Tim Northover5cc3dc82012-12-07 16:50:23 +0000516 /// This is one of the functions used to emit data into an ELF section, so the
517 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
518 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000519 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000520 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000521 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000522 }
523
524 /// This is one of the functions used to emit data into an ELF section, so the
525 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
526 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000527 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000528 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000529 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000530 }
531
532 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
533 MCELFStreamer::EmitAssemblerFlag(Flag);
534
535 switch (Flag) {
536 case MCAF_SyntaxUnified:
537 return; // no-op here.
538 case MCAF_Code16:
539 IsThumb = true;
540 return; // Change to Thumb mode
541 case MCAF_Code32:
542 IsThumb = false;
543 return; // Change to ARM mode
544 case MCAF_Code64:
545 return;
546 case MCAF_SubsectionsViaSymbols:
547 return;
548 }
549 }
550
551private:
552 enum ElfMappingSymbol {
553 EMS_None,
554 EMS_ARM,
555 EMS_Thumb,
556 EMS_Data
557 };
558
559 void EmitDataMappingSymbol() {
560 if (LastEMS == EMS_Data) return;
561 EmitMappingSymbol("$d");
562 LastEMS = EMS_Data;
563 }
564
565 void EmitThumbMappingSymbol() {
566 if (LastEMS == EMS_Thumb) return;
567 EmitMappingSymbol("$t");
568 LastEMS = EMS_Thumb;
569 }
570
571 void EmitARMMappingSymbol() {
572 if (LastEMS == EMS_ARM) return;
573 EmitMappingSymbol("$a");
574 LastEMS = EMS_ARM;
575 }
576
577 void EmitMappingSymbol(StringRef Name) {
578 MCSymbol *Start = getContext().CreateTempSymbol();
579 EmitLabel(Start);
580
Chandler Carruth1d94e932012-12-08 03:10:14 +0000581 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000582 getContext().GetOrCreateSymbol(Name + "." +
583 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000584
585 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
586 MCELF::SetType(SD, ELF::STT_NOTYPE);
587 MCELF::SetBinding(SD, ELF::STB_LOCAL);
588 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000589 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000590
591 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
592 Symbol->setVariableValue(Value);
593 }
594
595 void EmitThumbFunc(MCSymbol *Func) {
596 // FIXME: Anything needed here to flag the function as thumb?
597
598 getAssembler().setIsThumbFunc(Func);
599
600 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
601 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
602 }
603
Logan Chien2bcc42c2013-01-30 15:39:04 +0000604 // Helper functions for ARM exception handling directives
605 void Reset();
606
607 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000608 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000609 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000610
611 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
612 SectionKind Kind, const MCSymbol &Fn);
613 void SwitchToExTabSection(const MCSymbol &FnStart);
614 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000615
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000616 void EmitFixup(const MCExpr *Expr, MCFixupKind Kind);
617
Tim Northover5cc3dc82012-12-07 16:50:23 +0000618 bool IsThumb;
619 int64_t MappingSymbolCounter;
620
621 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
622 ElfMappingSymbol LastEMS;
623
Logan Chien2bcc42c2013-01-30 15:39:04 +0000624 // ARM Exception Handling Frame Information
625 MCSymbol *ExTab;
626 MCSymbol *FnStart;
627 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000628 unsigned PersonalityIndex;
629 unsigned FPReg; // Frame pointer register
630 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
631 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
632 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000633 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000634 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000635 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000636 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000637};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000638} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000639
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000640ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000641 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000642}
643
644void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
645void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
646void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
647void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
648 getStreamer().emitPersonality(Personality);
649}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000650void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
651 getStreamer().emitPersonalityIndex(Index);
652}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000653void ARMTargetELFStreamer::emitHandlerData() {
654 getStreamer().emitHandlerData();
655}
656void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
657 int64_t Offset) {
658 getStreamer().emitSetFP(FpReg, SpReg, Offset);
659}
660void ARMTargetELFStreamer::emitPad(int64_t Offset) {
661 getStreamer().emitPad(Offset);
662}
663void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
664 bool isVector) {
665 getStreamer().emitRegSave(RegList, isVector);
666}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000667void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
668 const SmallVectorImpl<uint8_t> &Opcodes) {
669 getStreamer().emitUnwindRaw(Offset, Opcodes);
670}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000671void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
672 assert(!Vendor.empty() && "Vendor cannot be empty.");
673
674 if (CurrentVendor == Vendor)
675 return;
676
677 if (!CurrentVendor.empty())
678 finishAttributeSection();
679
680 assert(Contents.empty() &&
681 ".ARM.attributes should be flushed before changing vendor");
682 CurrentVendor = Vendor;
683
684}
685void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
686 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
687}
688void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
689 StringRef Value) {
690 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
691}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000692void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
693 unsigned IntValue,
694 StringRef StringValue) {
695 setAttributeItems(Attribute, IntValue, StringValue,
696 /* OverwriteExisting= */ true);
697}
Logan Chien439e8f92013-12-11 17:16:25 +0000698void ARMTargetELFStreamer::emitArch(unsigned Value) {
699 Arch = Value;
700}
701void ARMTargetELFStreamer::emitArchDefaultAttributes() {
702 using namespace ARMBuildAttrs;
703 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
704 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
705
706 switch (Arch) {
707 case ARM::ARMV2:
708 case ARM::ARMV2A:
709 case ARM::ARMV3:
710 case ARM::ARMV3M:
711 case ARM::ARMV4:
712 case ARM::ARMV5:
713 setAttributeItem(ARM_ISA_use, Allowed, false);
714 break;
715
716 case ARM::ARMV4T:
717 case ARM::ARMV5T:
718 case ARM::ARMV5TE:
719 case ARM::ARMV6:
720 case ARM::ARMV6J:
721 setAttributeItem(ARM_ISA_use, Allowed, false);
722 setAttributeItem(THUMB_ISA_use, Allowed, false);
723 break;
724
725 case ARM::ARMV6T2:
726 setAttributeItem(ARM_ISA_use, Allowed, false);
727 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
728 break;
729
730 case ARM::ARMV6Z:
731 case ARM::ARMV6ZK:
732 setAttributeItem(ARM_ISA_use, Allowed, false);
733 setAttributeItem(THUMB_ISA_use, Allowed, false);
734 setAttributeItem(Virtualization_use, AllowTZ, false);
735 break;
736
737 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000738 setAttributeItem(THUMB_ISA_use, Allowed, false);
739 break;
740
741 case ARM::ARMV7:
742 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
743 break;
744
745 case ARM::ARMV7A:
746 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
747 setAttributeItem(ARM_ISA_use, Allowed, false);
748 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
749 break;
750
751 case ARM::ARMV7R:
752 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
753 setAttributeItem(ARM_ISA_use, Allowed, false);
754 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
755 break;
756
757 case ARM::ARMV7M:
758 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
759 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
760 break;
761
762 case ARM::ARMV8A:
763 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
764 setAttributeItem(ARM_ISA_use, Allowed, false);
765 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
766 setAttributeItem(MPextension_use, Allowed, false);
767 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
768 break;
769
770 case ARM::IWMMXT:
771 setAttributeItem(ARM_ISA_use, Allowed, false);
772 setAttributeItem(THUMB_ISA_use, Allowed, false);
773 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
774 break;
775
776 case ARM::IWMMXT2:
777 setAttributeItem(ARM_ISA_use, Allowed, false);
778 setAttributeItem(THUMB_ISA_use, Allowed, false);
779 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
780 break;
781
782 default:
783 report_fatal_error("Unknown Arch: " + Twine(Arch));
784 break;
785 }
786}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000787void ARMTargetELFStreamer::emitFPU(unsigned Value) {
788 FPU = Value;
789}
790void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
791 switch (FPU) {
792 case ARM::VFP:
793 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000794 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000795 ARMBuildAttrs::AllowFPv2,
796 /* OverwriteExisting= */ false);
797 break;
798
799 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000800 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000801 ARMBuildAttrs::AllowFPv3A,
802 /* OverwriteExisting= */ false);
803 break;
804
805 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000806 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000807 ARMBuildAttrs::AllowFPv3B,
808 /* OverwriteExisting= */ false);
809 break;
810
811 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000812 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000813 ARMBuildAttrs::AllowFPv4A,
814 /* OverwriteExisting= */ false);
815 break;
816
817 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000818 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000819 ARMBuildAttrs::AllowFPv4B,
820 /* OverwriteExisting= */ false);
821 break;
822
823 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000824 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000825 ARMBuildAttrs::AllowFPARMv8A,
826 /* OverwriteExisting= */ false);
827 break;
828
829 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000830 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000831 ARMBuildAttrs::AllowFPv3A,
832 /* OverwriteExisting= */ false);
833 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
834 ARMBuildAttrs::AllowNeon,
835 /* OverwriteExisting= */ false);
836 break;
837
838 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000839 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000840 ARMBuildAttrs::AllowFPv4A,
841 /* OverwriteExisting= */ false);
842 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
843 ARMBuildAttrs::AllowNeon2,
844 /* OverwriteExisting= */ false);
845 break;
846
847 case ARM::NEON_FP_ARMV8:
848 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000849 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000850 ARMBuildAttrs::AllowFPARMv8A,
851 /* OverwriteExisting= */ false);
852 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
853 ARMBuildAttrs::AllowNeonARMv8,
854 /* OverwriteExisting= */ false);
855 break;
856
Logan Chien05ae7442014-01-02 15:50:02 +0000857 case ARM::SOFTVFP:
858 break;
859
Logan Chien8cbb80d2013-10-28 17:51:12 +0000860 default:
861 report_fatal_error("Unknown FPU: " + Twine(FPU));
862 break;
863 }
864}
865size_t ARMTargetELFStreamer::calculateContentSize() const {
866 size_t Result = 0;
867 for (size_t i = 0; i < Contents.size(); ++i) {
868 AttributeItem item = Contents[i];
869 switch (item.Type) {
870 case AttributeItem::HiddenAttribute:
871 break;
872 case AttributeItem::NumericAttribute:
873 Result += getULEBSize(item.Tag);
874 Result += getULEBSize(item.IntValue);
875 break;
876 case AttributeItem::TextAttribute:
877 Result += getULEBSize(item.Tag);
878 Result += item.StringValue.size() + 1; // string + '\0'
879 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000880 case AttributeItem::NumericAndTextAttributes:
881 Result += getULEBSize(item.Tag);
882 Result += getULEBSize(item.IntValue);
883 Result += item.StringValue.size() + 1; // string + '\0';
884 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000885 }
886 }
887 return Result;
888}
889void ARMTargetELFStreamer::finishAttributeSection() {
890 // <format-version>
891 // [ <section-length> "vendor-name"
892 // [ <file-tag> <size> <attribute>*
893 // | <section-tag> <size> <section-number>* 0 <attribute>*
894 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
895 // ]+
896 // ]*
897
898 if (FPU != ARM::INVALID_FPU)
899 emitFPUDefaultAttributes();
900
Logan Chien439e8f92013-12-11 17:16:25 +0000901 if (Arch != ARM::INVALID_ARCH)
902 emitArchDefaultAttributes();
903
Logan Chien8cbb80d2013-10-28 17:51:12 +0000904 if (Contents.empty())
905 return;
906
907 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
908
909 ARMELFStreamer &Streamer = getStreamer();
910
911 // Switch to .ARM.attributes section
912 if (AttributeSection) {
913 Streamer.SwitchSection(AttributeSection);
914 } else {
915 AttributeSection =
916 Streamer.getContext().getELFSection(".ARM.attributes",
917 ELF::SHT_ARM_ATTRIBUTES,
918 0,
919 SectionKind::getMetadata());
920 Streamer.SwitchSection(AttributeSection);
921
922 // Format version
923 Streamer.EmitIntValue(0x41, 1);
924 }
925
926 // Vendor size + Vendor name + '\0'
927 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
928
929 // Tag + Tag Size
930 const size_t TagHeaderSize = 1 + 4;
931
932 const size_t ContentsSize = calculateContentSize();
933
934 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
935 Streamer.EmitBytes(CurrentVendor);
936 Streamer.EmitIntValue(0, 1); // '\0'
937
938 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
939 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
940
941 // Size should have been accounted for already, now
942 // emit each field as its type (ULEB or String)
943 for (size_t i = 0; i < Contents.size(); ++i) {
944 AttributeItem item = Contents[i];
945 Streamer.EmitULEB128IntValue(item.Tag);
946 switch (item.Type) {
947 default: llvm_unreachable("Invalid attribute type");
948 case AttributeItem::NumericAttribute:
949 Streamer.EmitULEB128IntValue(item.IntValue);
950 break;
951 case AttributeItem::TextAttribute:
952 Streamer.EmitBytes(item.StringValue.upper());
953 Streamer.EmitIntValue(0, 1); // '\0'
954 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000955 case AttributeItem::NumericAndTextAttributes:
956 Streamer.EmitULEB128IntValue(item.IntValue);
957 Streamer.EmitBytes(item.StringValue.upper());
958 Streamer.EmitIntValue(0, 1); // '\0'
959 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000960 }
961 }
962
963 Contents.clear();
964 FPU = ARM::INVALID_FPU;
965}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +0000966void
967ARMTargetELFStreamer::AnnotateTLSDescriptorSequence(const MCSymbolRefExpr *S) {
968 getStreamer().EmitFixup(S, FK_Data_4);
969}
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000970void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
971 getStreamer().emitInst(Inst, Suffix);
972}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000973
974void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +0000975 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000976 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
977 ATS.finishAttributeSection();
978
979 MCELFStreamer::FinishImpl();
980}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000981
Logan Chien2bcc42c2013-01-30 15:39:04 +0000982inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
983 unsigned Type,
984 unsigned Flags,
985 SectionKind Kind,
986 const MCSymbol &Fn) {
987 const MCSectionELF &FnSection =
988 static_cast<const MCSectionELF &>(Fn.getSection());
989
990 // Create the name for new section
991 StringRef FnSecName(FnSection.getSectionName());
992 SmallString<128> EHSecName(Prefix);
993 if (FnSecName != ".text") {
994 EHSecName += FnSecName;
995 }
996
997 // Get .ARM.extab or .ARM.exidx section
998 const MCSectionELF *EHSection = NULL;
999 if (const MCSymbol *Group = FnSection.getGroup()) {
1000 EHSection = getContext().getELFSection(
1001 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
1002 FnSection.getEntrySize(), Group->getName());
1003 } else {
1004 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
1005 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001006 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001007
1008 // Switch to .ARM.extab or .ARM.exidx section
1009 SwitchSection(EHSection);
1010 EmitCodeAlignment(4, 0);
1011}
1012
1013inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1014 SwitchToEHSection(".ARM.extab",
1015 ELF::SHT_PROGBITS,
1016 ELF::SHF_ALLOC,
1017 SectionKind::getDataRel(),
1018 FnStart);
1019}
1020
1021inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1022 SwitchToEHSection(".ARM.exidx",
1023 ELF::SHT_ARM_EXIDX,
1024 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1025 SectionKind::getDataRel(),
1026 FnStart);
1027}
Saleem Abdulrasool56e06e82014-01-30 04:02:47 +00001028void ARMELFStreamer::EmitFixup(const MCExpr *Expr, MCFixupKind Kind) {
1029 MCDataFragment *Frag = getOrCreateDataFragment();
1030 Frag->getFixups().push_back(MCFixup::Create(Frag->getContents().size(), Expr,
1031 Kind));
1032}
Logan Chien2bcc42c2013-01-30 15:39:04 +00001033
1034void ARMELFStreamer::Reset() {
1035 ExTab = NULL;
1036 FnStart = NULL;
1037 Personality = NULL;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001038 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001039 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001040 FPOffset = 0;
1041 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001042 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001043 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001044 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001045
Logan Chien325823a2013-06-09 12:22:30 +00001046 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001047 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001048}
1049
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001050void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001051 assert(FnStart == 0);
1052 FnStart = getContext().CreateTempSymbol();
1053 EmitLabel(FnStart);
1054}
1055
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001056void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001057 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001058
1059 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001060 if (!ExTab && !CantUnwind)
1061 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001062
1063 // Emit the exception index table entry
1064 SwitchToExIdxSection(*FnStart);
1065
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001066 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001067 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001068
1069 const MCSymbolRefExpr *FnStartRef =
1070 MCSymbolRefExpr::Create(FnStart,
1071 MCSymbolRefExpr::VK_ARM_PREL31,
1072 getContext());
1073
Rafael Espindola64e1af82013-07-02 15:49:13 +00001074 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001075
1076 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001077 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001078 } else if (ExTab) {
1079 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001080 const MCSymbolRefExpr *ExTabEntryRef =
1081 MCSymbolRefExpr::Create(ExTab,
1082 MCSymbolRefExpr::VK_ARM_PREL31,
1083 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001084 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001085 } else {
1086 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1087 // the second word of exception index table entry. The size of the unwind
1088 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001089 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001090 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001091 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001092 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +00001093 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001094 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001095 }
1096
Logan Chien4ea23b52013-05-10 16:17:24 +00001097 // Switch to the section containing FnStart
1098 SwitchSection(&FnStart->getSection());
1099
Logan Chien2bcc42c2013-01-30 15:39:04 +00001100 // Clean exception handling frame information
1101 Reset();
1102}
1103
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001104void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1105
1106// Add the R_ARM_NONE fixup at the same position
1107void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1108 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1109
1110 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1111 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1112
1113 AddValueSymbols(PersonalityRef);
1114 MCDataFragment *DF = getOrCreateDataFragment();
1115 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1116 PersonalityRef,
1117 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001118}
1119
Logan Chien325823a2013-06-09 12:22:30 +00001120void ARMELFStreamer::FlushPendingOffset() {
1121 if (PendingOffset != 0) {
1122 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1123 PendingOffset = 0;
1124 }
1125}
1126
Logan Chienc931fce2013-07-02 12:43:27 +00001127void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001128 // Emit the unwind opcode to restore $sp.
1129 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001130 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001131 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1132 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001133 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001134 } else {
1135 FlushPendingOffset();
1136 }
1137
1138 // Finalize the unwind opcode sequence
1139 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001140
1141 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1142 // section. Thus, we don't have to create an entry in the .ARM.extab
1143 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001144 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001145 return;
1146
1147 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001148 SwitchToExTabSection(*FnStart);
1149
1150 // Create .ARM.extab label for offset in .ARM.exidx
1151 assert(!ExTab);
1152 ExTab = getContext().CreateTempSymbol();
1153 EmitLabel(ExTab);
1154
Logan Chien4ea23b52013-05-10 16:17:24 +00001155 // Emit personality
1156 if (Personality) {
1157 const MCSymbolRefExpr *PersonalityRef =
1158 MCSymbolRefExpr::Create(Personality,
1159 MCSymbolRefExpr::VK_ARM_PREL31,
1160 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001161
Rafael Espindola64e1af82013-07-02 15:49:13 +00001162 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001163 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001164
1165 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +00001166 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001167 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +00001168
1169 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1170 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1171 // after the unwind opcodes. The handler data consists of several 32-bit
1172 // words, and should be terminated by zero.
1173 //
1174 // In case that the .handlerdata directive is not specified by the
1175 // programmer, we should emit zero to terminate the handler data.
1176 if (NoHandlerData && !Personality)
1177 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001178}
1179
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001180void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001181
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001182void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001183 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001184 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001185}
1186
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001187void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1188 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1189 PersonalityIndex = Index;
1190}
1191
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001192void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001193 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001194 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001195 "the operand of .setfp directive should be either $sp or $fp");
1196
1197 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001198 FPReg = NewFPReg;
1199
1200 if (NewSPReg == ARM::SP)
1201 FPOffset = SPOffset + Offset;
1202 else
1203 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001204}
1205
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001206void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001207 // Track the change of the $sp offset
1208 SPOffset -= Offset;
1209
1210 // To squash multiple .pad directives, we should delay the unwind opcode
1211 // until the .save, .vsave, .handlerdata, or .fnend directives.
1212 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001213}
1214
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001215void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001216 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001217 // Collect the registers in the register list
1218 unsigned Count = 0;
1219 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001220 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001221 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001222 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001223 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001224 unsigned Bit = (1u << Reg);
1225 if ((Mask & Bit) == 0) {
1226 Mask |= Bit;
1227 ++Count;
1228 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001229 }
Logan Chien325823a2013-06-09 12:22:30 +00001230
1231 // Track the change the $sp offset: For the .save directive, the
1232 // corresponding push instruction will decrease the $sp by (4 * Count).
1233 // For the .vsave directive, the corresponding vpush instruction will
1234 // decrease $sp by (8 * Count).
1235 SPOffset -= Count * (IsVector ? 8 : 4);
1236
1237 // Emit the opcode
1238 FlushPendingOffset();
1239 if (IsVector)
1240 UnwindOpAsm.EmitVFPRegSave(Mask);
1241 else
1242 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001243}
1244
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001245void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1246 const SmallVectorImpl<uint8_t> &Opcodes) {
1247 FlushPendingOffset();
1248 SPOffset = SPOffset - Offset;
1249 UnwindOpAsm.EmitRaw(Opcodes);
1250}
1251
Tim Northover5cc3dc82012-12-07 16:50:23 +00001252namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001253
1254MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1255 bool isVerboseAsm, bool useLoc, bool useCFI,
1256 bool useDwarfDirectory,
1257 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1258 MCAsmBackend *TAB, bool ShowInst) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001259 MCStreamer *S =
1260 llvm::createAsmStreamer(Ctx, OS, isVerboseAsm, useLoc, useCFI,
1261 useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
1262 new ARMTargetAsmStreamer(*S, OS, *InstPrint, isVerboseAsm);
1263 return S;
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001264}
1265
Tim Northover5cc3dc82012-12-07 16:50:23 +00001266 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1267 raw_ostream &OS, MCCodeEmitter *Emitter,
1268 bool RelaxAll, bool NoExecStack,
1269 bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001270 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
1271 new ARMTargetELFStreamer(*S);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001272 // FIXME: This should eventually end up somewhere else where more
1273 // intelligent flag decisions can be made. For now we are just maintaining
1274 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1275 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1276
Tim Northover5cc3dc82012-12-07 16:50:23 +00001277 if (RelaxAll)
1278 S->getAssembler().setRelaxAll(true);
1279 if (NoExecStack)
1280 S->getAssembler().setNoExecStack(true);
1281 return S;
1282 }
1283
1284}
1285
1286