blob: 707a3d3f2ed8c6cbecb8b863c82fc44370963b55 [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
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000142public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000143 ARMTargetAsmStreamer(MCStreamer &S, formatted_raw_ostream &OS,
144 MCInstPrinter &InstPrinter, bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000145};
146
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000147ARMTargetAsmStreamer::ARMTargetAsmStreamer(MCStreamer &S,
148 formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000149 MCInstPrinter &InstPrinter,
150 bool VerboseAsm)
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000151 : ARMTargetStreamer(S), OS(OS), InstPrinter(InstPrinter),
152 IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000153void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
154void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
155void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
156void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
157 OS << "\t.personality " << Personality->getName() << '\n';
158}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000159void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
160 OS << "\t.personalityindex " << Index << '\n';
161}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000162void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
163void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
164 int64_t Offset) {
165 OS << "\t.setfp\t";
166 InstPrinter.printRegName(OS, FpReg);
167 OS << ", ";
168 InstPrinter.printRegName(OS, SpReg);
169 if (Offset)
170 OS << ", #" << Offset;
171 OS << '\n';
172}
173void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
174 OS << "\t.pad\t#" << Offset << '\n';
175}
176void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
177 bool isVector) {
178 assert(RegList.size() && "RegList should not be empty");
179 if (isVector)
180 OS << "\t.vsave\t{";
181 else
182 OS << "\t.save\t{";
183
184 InstPrinter.printRegName(OS, RegList[0]);
185
186 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
187 OS << ", ";
188 InstPrinter.printRegName(OS, RegList[i]);
189 }
190
191 OS << "}\n";
192}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000193void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
194}
195void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000196 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
197 if (IsVerboseAsm) {
198 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
199 if (!Name.empty())
200 OS << "\t@ " << Name;
201 }
202 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000203}
204void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
205 StringRef String) {
206 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000207 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000208 OS << "\t.cpu\t" << String.lower();
209 break;
210 default:
211 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000212 if (IsVerboseAsm) {
213 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
214 if (!Name.empty())
215 OS << "\t@ " << Name;
216 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000217 break;
218 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000219 OS << "\n";
220}
221void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
222 unsigned IntValue,
223 StringRef StringValue) {
224 switch (Attribute) {
225 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
226 case ARMBuildAttrs::compatibility:
227 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
228 if (!StringValue.empty())
229 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000230 if (IsVerboseAsm)
231 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000232 break;
233 }
234 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000235}
Logan Chien439e8f92013-12-11 17:16:25 +0000236void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
237 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
238}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000239void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
240 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
241}
242void ARMTargetAsmStreamer::finishAttributeSection() {
243}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000244
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000245void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
246 OS << "\t.inst";
247 if (Suffix)
248 OS << "." << Suffix;
249 OS << "\t0x" << utohexstr(Inst) << "\n";
250}
251
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000252void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
253 const SmallVectorImpl<uint8_t> &Opcodes) {
254 OS << "\t.unwind_raw " << Offset;
255 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
256 OCE = Opcodes.end();
257 OCI != OCE; ++OCI)
258 OS << ", 0x" << utohexstr(*OCI);
259 OS << '\n';
260}
261
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000262class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000263private:
264 // This structure holds all attributes, accounting for
265 // their string/numeric value, so we can later emmit them
266 // in declaration order, keeping all in the same vector
267 struct AttributeItem {
268 enum {
269 HiddenAttribute = 0,
270 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000271 TextAttribute,
272 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000273 } Type;
274 unsigned Tag;
275 unsigned IntValue;
276 StringRef StringValue;
277
278 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
279 return (LHS.Tag < RHS.Tag);
280 }
281 };
282
283 StringRef CurrentVendor;
284 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000285 unsigned Arch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000286 SmallVector<AttributeItem, 64> Contents;
287
288 const MCSection *AttributeSection;
289
290 // FIXME: this should be in a more generic place, but
291 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
292 static size_t getULEBSize(int Value) {
293 size_t Size = 0;
294 do {
295 Value >>= 7;
296 Size += sizeof(int8_t); // Is this really necessary?
297 } while (Value);
298 return Size;
299 }
300
301 AttributeItem *getAttributeItem(unsigned Attribute) {
302 for (size_t i = 0; i < Contents.size(); ++i)
303 if (Contents[i].Tag == Attribute)
304 return &Contents[i];
305 return 0;
306 }
307
308 void setAttributeItem(unsigned Attribute, unsigned Value,
309 bool OverwriteExisting) {
310 // Look for existing attribute item
311 if (AttributeItem *Item = getAttributeItem(Attribute)) {
312 if (!OverwriteExisting)
313 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000314 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000315 Item->IntValue = Value;
316 return;
317 }
318
319 // Create new attribute item
320 AttributeItem Item = {
321 AttributeItem::NumericAttribute,
322 Attribute,
323 Value,
324 StringRef("")
325 };
326 Contents.push_back(Item);
327 }
328
329 void setAttributeItem(unsigned Attribute, StringRef Value,
330 bool OverwriteExisting) {
331 // Look for existing attribute item
332 if (AttributeItem *Item = getAttributeItem(Attribute)) {
333 if (!OverwriteExisting)
334 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000335 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000336 Item->StringValue = Value;
337 return;
338 }
339
340 // Create new attribute item
341 AttributeItem Item = {
342 AttributeItem::TextAttribute,
343 Attribute,
344 0,
345 Value
346 };
347 Contents.push_back(Item);
348 }
349
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000350 void setAttributeItems(unsigned Attribute, unsigned IntValue,
351 StringRef StringValue, bool OverwriteExisting) {
352 // Look for existing attribute item
353 if (AttributeItem *Item = getAttributeItem(Attribute)) {
354 if (!OverwriteExisting)
355 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000356 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000357 Item->IntValue = IntValue;
358 Item->StringValue = StringValue;
359 return;
360 }
361
362 // Create new attribute item
363 AttributeItem Item = {
364 AttributeItem::NumericAndTextAttributes,
365 Attribute,
366 IntValue,
367 StringValue
368 };
369 Contents.push_back(Item);
370 }
371
Logan Chien439e8f92013-12-11 17:16:25 +0000372 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000373 void emitFPUDefaultAttributes();
374
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000375 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000376
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000377 virtual void emitFnStart();
378 virtual void emitFnEnd();
379 virtual void emitCantUnwind();
380 virtual void emitPersonality(const MCSymbol *Personality);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000381 virtual void emitPersonalityIndex(unsigned Index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000382 virtual void emitHandlerData();
383 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
384 virtual void emitPad(int64_t Offset);
385 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
386 bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000387 virtual void emitUnwindRaw(int64_t Offset,
388 const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000389
390 virtual void switchVendor(StringRef Vendor);
391 virtual void emitAttribute(unsigned Attribute, unsigned Value);
392 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000393 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
394 StringRef StringValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000395 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000396 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000397 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000398 virtual void finishAttributeSection();
399
400 size_t calculateContentSize() const;
401
402public:
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000403 ARMTargetELFStreamer(MCStreamer &S)
404 : ARMTargetStreamer(S), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
405 Arch(ARM::INVALID_ARCH), AttributeSection(0) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000406};
407
Tim Northover5cc3dc82012-12-07 16:50:23 +0000408/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
409/// the appropriate points in the object files. These symbols are defined in the
410/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
411///
412/// In brief: $a, $t or $d should be emitted at the start of each contiguous
413/// region of ARM code, Thumb code or data in a section. In practice, this
414/// emission does not rely on explicit assembler directives but on inherent
415/// properties of the directives doing the emission (e.g. ".byte" is data, "add
416/// r0, r0, r0" an instruction).
417///
418/// As a result this system is orthogonal to the DataRegion infrastructure used
419/// by MachO. Beware!
420class ARMELFStreamer : public MCELFStreamer {
421public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000422 friend class ARMTargetELFStreamer;
423
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000424 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
425 MCCodeEmitter *Emitter, bool IsThumb)
426 : MCELFStreamer(Context, TAB, OS, Emitter), IsThumb(IsThumb),
427 MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000428 Reset();
429 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000430
431 ~ARMELFStreamer() {}
432
Logan Chien8cbb80d2013-10-28 17:51:12 +0000433 virtual void FinishImpl();
434
Logan Chien2bcc42c2013-01-30 15:39:04 +0000435 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000436 void emitFnStart();
437 void emitFnEnd();
438 void emitCantUnwind();
439 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000440 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000441 void emitHandlerData();
442 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
443 void emitPad(int64_t Offset);
444 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000445 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000446
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000447 virtual void ChangeSection(const MCSection *Section,
448 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000449 // We have to keep track of the mapping symbol state of any sections we
450 // use. Each one should start off as EMS_None, which is provided as the
451 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000452 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000453 LastEMS = LastMappingSymbols.lookup(Section);
454
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000455 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000456 }
457
458 /// This function is the one used to emit instruction data into the ELF
459 /// streamer. We override it to add the appropriate mapping symbol if
460 /// necessary.
461 virtual void EmitInstruction(const MCInst& Inst) {
462 if (IsThumb)
463 EmitThumbMappingSymbol();
464 else
465 EmitARMMappingSymbol();
466
467 MCELFStreamer::EmitInstruction(Inst);
468 }
469
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000470 virtual void emitInst(uint32_t Inst, char Suffix) {
471 unsigned Size;
472 char Buffer[4];
473 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
474
475 switch (Suffix) {
476 case '\0':
477 Size = 4;
478
479 assert(!IsThumb);
480 EmitARMMappingSymbol();
481 for (unsigned II = 0, IE = Size; II != IE; II++) {
482 const unsigned I = LittleEndian ? (Size - II - 1) : II;
483 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
484 }
485
486 break;
487 case 'n':
488 case 'w':
489 Size = (Suffix == 'n' ? 2 : 4);
490
491 assert(IsThumb);
492 EmitThumbMappingSymbol();
493 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
494 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
495 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
496 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
497 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
498 }
499
500 break;
501 default:
502 llvm_unreachable("Invalid Suffix");
503 }
504
505 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
506 }
507
Tim Northover5cc3dc82012-12-07 16:50:23 +0000508 /// This is one of the functions used to emit data into an ELF section, so the
509 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
510 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000511 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000512 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000513 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000514 }
515
516 /// 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 EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000520 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000521 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000522 }
523
524 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
525 MCELFStreamer::EmitAssemblerFlag(Flag);
526
527 switch (Flag) {
528 case MCAF_SyntaxUnified:
529 return; // no-op here.
530 case MCAF_Code16:
531 IsThumb = true;
532 return; // Change to Thumb mode
533 case MCAF_Code32:
534 IsThumb = false;
535 return; // Change to ARM mode
536 case MCAF_Code64:
537 return;
538 case MCAF_SubsectionsViaSymbols:
539 return;
540 }
541 }
542
543private:
544 enum ElfMappingSymbol {
545 EMS_None,
546 EMS_ARM,
547 EMS_Thumb,
548 EMS_Data
549 };
550
551 void EmitDataMappingSymbol() {
552 if (LastEMS == EMS_Data) return;
553 EmitMappingSymbol("$d");
554 LastEMS = EMS_Data;
555 }
556
557 void EmitThumbMappingSymbol() {
558 if (LastEMS == EMS_Thumb) return;
559 EmitMappingSymbol("$t");
560 LastEMS = EMS_Thumb;
561 }
562
563 void EmitARMMappingSymbol() {
564 if (LastEMS == EMS_ARM) return;
565 EmitMappingSymbol("$a");
566 LastEMS = EMS_ARM;
567 }
568
569 void EmitMappingSymbol(StringRef Name) {
570 MCSymbol *Start = getContext().CreateTempSymbol();
571 EmitLabel(Start);
572
Chandler Carruth1d94e932012-12-08 03:10:14 +0000573 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000574 getContext().GetOrCreateSymbol(Name + "." +
575 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000576
577 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
578 MCELF::SetType(SD, ELF::STT_NOTYPE);
579 MCELF::SetBinding(SD, ELF::STB_LOCAL);
580 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000581 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000582
583 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
584 Symbol->setVariableValue(Value);
585 }
586
587 void EmitThumbFunc(MCSymbol *Func) {
588 // FIXME: Anything needed here to flag the function as thumb?
589
590 getAssembler().setIsThumbFunc(Func);
591
592 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
593 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
594 }
595
Logan Chien2bcc42c2013-01-30 15:39:04 +0000596 // Helper functions for ARM exception handling directives
597 void Reset();
598
599 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000600 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000601 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000602
603 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
604 SectionKind Kind, const MCSymbol &Fn);
605 void SwitchToExTabSection(const MCSymbol &FnStart);
606 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000607
608 bool IsThumb;
609 int64_t MappingSymbolCounter;
610
611 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
612 ElfMappingSymbol LastEMS;
613
Logan Chien2bcc42c2013-01-30 15:39:04 +0000614 // ARM Exception Handling Frame Information
615 MCSymbol *ExTab;
616 MCSymbol *FnStart;
617 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000618 unsigned PersonalityIndex;
619 unsigned FPReg; // Frame pointer register
620 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
621 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
622 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000623 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000624 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000625 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000626 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000627};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000628} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000629
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000630ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Rafael Espindola24ea09e2014-01-26 06:06:37 +0000631 return static_cast<ARMELFStreamer &>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000632}
633
634void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
635void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
636void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
637void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
638 getStreamer().emitPersonality(Personality);
639}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000640void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
641 getStreamer().emitPersonalityIndex(Index);
642}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000643void ARMTargetELFStreamer::emitHandlerData() {
644 getStreamer().emitHandlerData();
645}
646void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
647 int64_t Offset) {
648 getStreamer().emitSetFP(FpReg, SpReg, Offset);
649}
650void ARMTargetELFStreamer::emitPad(int64_t Offset) {
651 getStreamer().emitPad(Offset);
652}
653void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
654 bool isVector) {
655 getStreamer().emitRegSave(RegList, isVector);
656}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000657void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
658 const SmallVectorImpl<uint8_t> &Opcodes) {
659 getStreamer().emitUnwindRaw(Offset, Opcodes);
660}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000661void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
662 assert(!Vendor.empty() && "Vendor cannot be empty.");
663
664 if (CurrentVendor == Vendor)
665 return;
666
667 if (!CurrentVendor.empty())
668 finishAttributeSection();
669
670 assert(Contents.empty() &&
671 ".ARM.attributes should be flushed before changing vendor");
672 CurrentVendor = Vendor;
673
674}
675void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
676 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
677}
678void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
679 StringRef Value) {
680 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
681}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000682void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
683 unsigned IntValue,
684 StringRef StringValue) {
685 setAttributeItems(Attribute, IntValue, StringValue,
686 /* OverwriteExisting= */ true);
687}
Logan Chien439e8f92013-12-11 17:16:25 +0000688void ARMTargetELFStreamer::emitArch(unsigned Value) {
689 Arch = Value;
690}
691void ARMTargetELFStreamer::emitArchDefaultAttributes() {
692 using namespace ARMBuildAttrs;
693 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
694 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
695
696 switch (Arch) {
697 case ARM::ARMV2:
698 case ARM::ARMV2A:
699 case ARM::ARMV3:
700 case ARM::ARMV3M:
701 case ARM::ARMV4:
702 case ARM::ARMV5:
703 setAttributeItem(ARM_ISA_use, Allowed, false);
704 break;
705
706 case ARM::ARMV4T:
707 case ARM::ARMV5T:
708 case ARM::ARMV5TE:
709 case ARM::ARMV6:
710 case ARM::ARMV6J:
711 setAttributeItem(ARM_ISA_use, Allowed, false);
712 setAttributeItem(THUMB_ISA_use, Allowed, false);
713 break;
714
715 case ARM::ARMV6T2:
716 setAttributeItem(ARM_ISA_use, Allowed, false);
717 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
718 break;
719
720 case ARM::ARMV6Z:
721 case ARM::ARMV6ZK:
722 setAttributeItem(ARM_ISA_use, Allowed, false);
723 setAttributeItem(THUMB_ISA_use, Allowed, false);
724 setAttributeItem(Virtualization_use, AllowTZ, false);
725 break;
726
727 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000728 setAttributeItem(THUMB_ISA_use, Allowed, false);
729 break;
730
731 case ARM::ARMV7:
732 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
733 break;
734
735 case ARM::ARMV7A:
736 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
737 setAttributeItem(ARM_ISA_use, Allowed, false);
738 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
739 break;
740
741 case ARM::ARMV7R:
742 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
743 setAttributeItem(ARM_ISA_use, Allowed, false);
744 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
745 break;
746
747 case ARM::ARMV7M:
748 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
749 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
750 break;
751
752 case ARM::ARMV8A:
753 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
754 setAttributeItem(ARM_ISA_use, Allowed, false);
755 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
756 setAttributeItem(MPextension_use, Allowed, false);
757 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
758 break;
759
760 case ARM::IWMMXT:
761 setAttributeItem(ARM_ISA_use, Allowed, false);
762 setAttributeItem(THUMB_ISA_use, Allowed, false);
763 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
764 break;
765
766 case ARM::IWMMXT2:
767 setAttributeItem(ARM_ISA_use, Allowed, false);
768 setAttributeItem(THUMB_ISA_use, Allowed, false);
769 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
770 break;
771
772 default:
773 report_fatal_error("Unknown Arch: " + Twine(Arch));
774 break;
775 }
776}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000777void ARMTargetELFStreamer::emitFPU(unsigned Value) {
778 FPU = Value;
779}
780void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
781 switch (FPU) {
782 case ARM::VFP:
783 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000784 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000785 ARMBuildAttrs::AllowFPv2,
786 /* OverwriteExisting= */ false);
787 break;
788
789 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000790 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000791 ARMBuildAttrs::AllowFPv3A,
792 /* OverwriteExisting= */ false);
793 break;
794
795 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000796 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000797 ARMBuildAttrs::AllowFPv3B,
798 /* OverwriteExisting= */ false);
799 break;
800
801 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000802 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000803 ARMBuildAttrs::AllowFPv4A,
804 /* OverwriteExisting= */ false);
805 break;
806
807 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000808 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000809 ARMBuildAttrs::AllowFPv4B,
810 /* OverwriteExisting= */ false);
811 break;
812
813 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000814 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000815 ARMBuildAttrs::AllowFPARMv8A,
816 /* OverwriteExisting= */ false);
817 break;
818
819 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000820 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000821 ARMBuildAttrs::AllowFPv3A,
822 /* OverwriteExisting= */ false);
823 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
824 ARMBuildAttrs::AllowNeon,
825 /* OverwriteExisting= */ false);
826 break;
827
828 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000829 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000830 ARMBuildAttrs::AllowFPv4A,
831 /* OverwriteExisting= */ false);
832 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
833 ARMBuildAttrs::AllowNeon2,
834 /* OverwriteExisting= */ false);
835 break;
836
837 case ARM::NEON_FP_ARMV8:
838 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000839 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000840 ARMBuildAttrs::AllowFPARMv8A,
841 /* OverwriteExisting= */ false);
842 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
843 ARMBuildAttrs::AllowNeonARMv8,
844 /* OverwriteExisting= */ false);
845 break;
846
Logan Chien05ae7442014-01-02 15:50:02 +0000847 case ARM::SOFTVFP:
848 break;
849
Logan Chien8cbb80d2013-10-28 17:51:12 +0000850 default:
851 report_fatal_error("Unknown FPU: " + Twine(FPU));
852 break;
853 }
854}
855size_t ARMTargetELFStreamer::calculateContentSize() const {
856 size_t Result = 0;
857 for (size_t i = 0; i < Contents.size(); ++i) {
858 AttributeItem item = Contents[i];
859 switch (item.Type) {
860 case AttributeItem::HiddenAttribute:
861 break;
862 case AttributeItem::NumericAttribute:
863 Result += getULEBSize(item.Tag);
864 Result += getULEBSize(item.IntValue);
865 break;
866 case AttributeItem::TextAttribute:
867 Result += getULEBSize(item.Tag);
868 Result += item.StringValue.size() + 1; // string + '\0'
869 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000870 case AttributeItem::NumericAndTextAttributes:
871 Result += getULEBSize(item.Tag);
872 Result += getULEBSize(item.IntValue);
873 Result += item.StringValue.size() + 1; // string + '\0';
874 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000875 }
876 }
877 return Result;
878}
879void ARMTargetELFStreamer::finishAttributeSection() {
880 // <format-version>
881 // [ <section-length> "vendor-name"
882 // [ <file-tag> <size> <attribute>*
883 // | <section-tag> <size> <section-number>* 0 <attribute>*
884 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
885 // ]+
886 // ]*
887
888 if (FPU != ARM::INVALID_FPU)
889 emitFPUDefaultAttributes();
890
Logan Chien439e8f92013-12-11 17:16:25 +0000891 if (Arch != ARM::INVALID_ARCH)
892 emitArchDefaultAttributes();
893
Logan Chien8cbb80d2013-10-28 17:51:12 +0000894 if (Contents.empty())
895 return;
896
897 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
898
899 ARMELFStreamer &Streamer = getStreamer();
900
901 // Switch to .ARM.attributes section
902 if (AttributeSection) {
903 Streamer.SwitchSection(AttributeSection);
904 } else {
905 AttributeSection =
906 Streamer.getContext().getELFSection(".ARM.attributes",
907 ELF::SHT_ARM_ATTRIBUTES,
908 0,
909 SectionKind::getMetadata());
910 Streamer.SwitchSection(AttributeSection);
911
912 // Format version
913 Streamer.EmitIntValue(0x41, 1);
914 }
915
916 // Vendor size + Vendor name + '\0'
917 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
918
919 // Tag + Tag Size
920 const size_t TagHeaderSize = 1 + 4;
921
922 const size_t ContentsSize = calculateContentSize();
923
924 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
925 Streamer.EmitBytes(CurrentVendor);
926 Streamer.EmitIntValue(0, 1); // '\0'
927
928 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
929 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
930
931 // Size should have been accounted for already, now
932 // emit each field as its type (ULEB or String)
933 for (size_t i = 0; i < Contents.size(); ++i) {
934 AttributeItem item = Contents[i];
935 Streamer.EmitULEB128IntValue(item.Tag);
936 switch (item.Type) {
937 default: llvm_unreachable("Invalid attribute type");
938 case AttributeItem::NumericAttribute:
939 Streamer.EmitULEB128IntValue(item.IntValue);
940 break;
941 case AttributeItem::TextAttribute:
942 Streamer.EmitBytes(item.StringValue.upper());
943 Streamer.EmitIntValue(0, 1); // '\0'
944 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000945 case AttributeItem::NumericAndTextAttributes:
946 Streamer.EmitULEB128IntValue(item.IntValue);
947 Streamer.EmitBytes(item.StringValue.upper());
948 Streamer.EmitIntValue(0, 1); // '\0'
949 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000950 }
951 }
952
953 Contents.clear();
954 FPU = ARM::INVALID_FPU;
955}
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000956void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
957 getStreamer().emitInst(Inst, Suffix);
958}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000959
960void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +0000961 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000962 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
963 ATS.finishAttributeSection();
964
965 MCELFStreamer::FinishImpl();
966}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000967
Logan Chien2bcc42c2013-01-30 15:39:04 +0000968inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
969 unsigned Type,
970 unsigned Flags,
971 SectionKind Kind,
972 const MCSymbol &Fn) {
973 const MCSectionELF &FnSection =
974 static_cast<const MCSectionELF &>(Fn.getSection());
975
976 // Create the name for new section
977 StringRef FnSecName(FnSection.getSectionName());
978 SmallString<128> EHSecName(Prefix);
979 if (FnSecName != ".text") {
980 EHSecName += FnSecName;
981 }
982
983 // Get .ARM.extab or .ARM.exidx section
984 const MCSectionELF *EHSection = NULL;
985 if (const MCSymbol *Group = FnSection.getGroup()) {
986 EHSection = getContext().getELFSection(
987 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
988 FnSection.getEntrySize(), Group->getName());
989 } else {
990 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
991 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000992 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +0000993
994 // Switch to .ARM.extab or .ARM.exidx section
995 SwitchSection(EHSection);
996 EmitCodeAlignment(4, 0);
997}
998
999inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1000 SwitchToEHSection(".ARM.extab",
1001 ELF::SHT_PROGBITS,
1002 ELF::SHF_ALLOC,
1003 SectionKind::getDataRel(),
1004 FnStart);
1005}
1006
1007inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1008 SwitchToEHSection(".ARM.exidx",
1009 ELF::SHT_ARM_EXIDX,
1010 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1011 SectionKind::getDataRel(),
1012 FnStart);
1013}
1014
1015void ARMELFStreamer::Reset() {
1016 ExTab = NULL;
1017 FnStart = NULL;
1018 Personality = NULL;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001019 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001020 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001021 FPOffset = 0;
1022 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001023 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001024 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001025 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001026
Logan Chien325823a2013-06-09 12:22:30 +00001027 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001028 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001029}
1030
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001031void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001032 assert(FnStart == 0);
1033 FnStart = getContext().CreateTempSymbol();
1034 EmitLabel(FnStart);
1035}
1036
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001037void ARMELFStreamer::emitFnEnd() {
Alp Tokercb402912014-01-24 17:20:08 +00001038 assert(FnStart && ".fnstart must precedes .fnend");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001039
1040 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001041 if (!ExTab && !CantUnwind)
1042 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001043
1044 // Emit the exception index table entry
1045 SwitchToExIdxSection(*FnStart);
1046
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001047 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001048 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001049
1050 const MCSymbolRefExpr *FnStartRef =
1051 MCSymbolRefExpr::Create(FnStart,
1052 MCSymbolRefExpr::VK_ARM_PREL31,
1053 getContext());
1054
Rafael Espindola64e1af82013-07-02 15:49:13 +00001055 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001056
1057 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001058 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001059 } else if (ExTab) {
1060 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001061 const MCSymbolRefExpr *ExTabEntryRef =
1062 MCSymbolRefExpr::Create(ExTab,
1063 MCSymbolRefExpr::VK_ARM_PREL31,
1064 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001065 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001066 } else {
1067 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1068 // the second word of exception index table entry. The size of the unwind
1069 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001070 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001071 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001072 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001073 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +00001074 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001075 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001076 }
1077
Logan Chien4ea23b52013-05-10 16:17:24 +00001078 // Switch to the section containing FnStart
1079 SwitchSection(&FnStart->getSection());
1080
Logan Chien2bcc42c2013-01-30 15:39:04 +00001081 // Clean exception handling frame information
1082 Reset();
1083}
1084
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001085void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1086
1087// Add the R_ARM_NONE fixup at the same position
1088void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1089 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1090
1091 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1092 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1093
1094 AddValueSymbols(PersonalityRef);
1095 MCDataFragment *DF = getOrCreateDataFragment();
1096 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1097 PersonalityRef,
1098 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001099}
1100
Logan Chien325823a2013-06-09 12:22:30 +00001101void ARMELFStreamer::FlushPendingOffset() {
1102 if (PendingOffset != 0) {
1103 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1104 PendingOffset = 0;
1105 }
1106}
1107
Logan Chienc931fce2013-07-02 12:43:27 +00001108void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001109 // Emit the unwind opcode to restore $sp.
1110 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001111 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001112 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1113 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001114 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001115 } else {
1116 FlushPendingOffset();
1117 }
1118
1119 // Finalize the unwind opcode sequence
1120 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001121
1122 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1123 // section. Thus, we don't have to create an entry in the .ARM.extab
1124 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001125 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001126 return;
1127
1128 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001129 SwitchToExTabSection(*FnStart);
1130
1131 // Create .ARM.extab label for offset in .ARM.exidx
1132 assert(!ExTab);
1133 ExTab = getContext().CreateTempSymbol();
1134 EmitLabel(ExTab);
1135
Logan Chien4ea23b52013-05-10 16:17:24 +00001136 // Emit personality
1137 if (Personality) {
1138 const MCSymbolRefExpr *PersonalityRef =
1139 MCSymbolRefExpr::Create(Personality,
1140 MCSymbolRefExpr::VK_ARM_PREL31,
1141 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001142
Rafael Espindola64e1af82013-07-02 15:49:13 +00001143 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001144 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001145
1146 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +00001147 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001148 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +00001149
1150 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1151 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1152 // after the unwind opcodes. The handler data consists of several 32-bit
1153 // words, and should be terminated by zero.
1154 //
1155 // In case that the .handlerdata directive is not specified by the
1156 // programmer, we should emit zero to terminate the handler data.
1157 if (NoHandlerData && !Personality)
1158 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001159}
1160
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001161void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001162
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001163void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001164 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001165 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001166}
1167
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001168void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1169 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1170 PersonalityIndex = Index;
1171}
1172
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001173void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001174 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001175 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001176 "the operand of .setfp directive should be either $sp or $fp");
1177
1178 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001179 FPReg = NewFPReg;
1180
1181 if (NewSPReg == ARM::SP)
1182 FPOffset = SPOffset + Offset;
1183 else
1184 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001185}
1186
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001187void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001188 // Track the change of the $sp offset
1189 SPOffset -= Offset;
1190
1191 // To squash multiple .pad directives, we should delay the unwind opcode
1192 // until the .save, .vsave, .handlerdata, or .fnend directives.
1193 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001194}
1195
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001196void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001197 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001198 // Collect the registers in the register list
1199 unsigned Count = 0;
1200 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001201 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001202 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001203 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001204 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001205 unsigned Bit = (1u << Reg);
1206 if ((Mask & Bit) == 0) {
1207 Mask |= Bit;
1208 ++Count;
1209 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001210 }
Logan Chien325823a2013-06-09 12:22:30 +00001211
1212 // Track the change the $sp offset: For the .save directive, the
1213 // corresponding push instruction will decrease the $sp by (4 * Count).
1214 // For the .vsave directive, the corresponding vpush instruction will
1215 // decrease $sp by (8 * Count).
1216 SPOffset -= Count * (IsVector ? 8 : 4);
1217
1218 // Emit the opcode
1219 FlushPendingOffset();
1220 if (IsVector)
1221 UnwindOpAsm.EmitVFPRegSave(Mask);
1222 else
1223 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001224}
1225
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001226void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1227 const SmallVectorImpl<uint8_t> &Opcodes) {
1228 FlushPendingOffset();
1229 SPOffset = SPOffset - Offset;
1230 UnwindOpAsm.EmitRaw(Opcodes);
1231}
1232
Tim Northover5cc3dc82012-12-07 16:50:23 +00001233namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001234
1235MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1236 bool isVerboseAsm, bool useLoc, bool useCFI,
1237 bool useDwarfDirectory,
1238 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1239 MCAsmBackend *TAB, bool ShowInst) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001240 MCStreamer *S =
1241 llvm::createAsmStreamer(Ctx, OS, isVerboseAsm, useLoc, useCFI,
1242 useDwarfDirectory, InstPrint, CE, TAB, ShowInst);
1243 new ARMTargetAsmStreamer(*S, OS, *InstPrint, isVerboseAsm);
1244 return S;
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001245}
1246
Tim Northover5cc3dc82012-12-07 16:50:23 +00001247 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1248 raw_ostream &OS, MCCodeEmitter *Emitter,
1249 bool RelaxAll, bool NoExecStack,
1250 bool IsThumb) {
Rafael Espindola24ea09e2014-01-26 06:06:37 +00001251 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
1252 new ARMTargetELFStreamer(*S);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001253 // FIXME: This should eventually end up somewhere else where more
1254 // intelligent flag decisions can be made. For now we are just maintaining
1255 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1256 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1257
Tim Northover5cc3dc82012-12-07 16:50:23 +00001258 if (RelaxAll)
1259 S->getAssembler().setRelaxAll(true);
1260 if (NoExecStack)
1261 S->getAssembler().setNoExecStack(true);
1262 return S;
1263 }
1264
1265}
1266
1267