blob: 1e3dc617447cacdfe72583875812fd9443e33476 [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
Tim Northover5cc3dc82012-12-07 16:50:23 +0000107namespace {
108
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000109class ARMELFStreamer;
110
111class ARMTargetAsmStreamer : public ARMTargetStreamer {
112 formatted_raw_ostream &OS;
113 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000114 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000115
116 virtual void emitFnStart();
117 virtual void emitFnEnd();
118 virtual void emitCantUnwind();
119 virtual void emitPersonality(const MCSymbol *Personality);
120 virtual void emitHandlerData();
121 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
122 virtual void emitPad(int64_t Offset);
123 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
124 bool isVector);
125
Logan Chien8cbb80d2013-10-28 17:51:12 +0000126 virtual void switchVendor(StringRef Vendor);
127 virtual void emitAttribute(unsigned Attribute, unsigned Value);
128 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000129 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
130 StringRef StrinValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000131 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000132 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000133 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000134 virtual void finishAttributeSection();
135
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000136public:
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000137 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter,
138 bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000139};
140
141ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000142 MCInstPrinter &InstPrinter,
143 bool VerboseAsm)
144 : OS(OS), InstPrinter(InstPrinter), IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000145void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
146void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
147void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
148void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
149 OS << "\t.personality " << Personality->getName() << '\n';
150}
151void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
152void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
153 int64_t Offset) {
154 OS << "\t.setfp\t";
155 InstPrinter.printRegName(OS, FpReg);
156 OS << ", ";
157 InstPrinter.printRegName(OS, SpReg);
158 if (Offset)
159 OS << ", #" << Offset;
160 OS << '\n';
161}
162void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
163 OS << "\t.pad\t#" << Offset << '\n';
164}
165void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
166 bool isVector) {
167 assert(RegList.size() && "RegList should not be empty");
168 if (isVector)
169 OS << "\t.vsave\t{";
170 else
171 OS << "\t.save\t{";
172
173 InstPrinter.printRegName(OS, RegList[0]);
174
175 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
176 OS << ", ";
177 InstPrinter.printRegName(OS, RegList[i]);
178 }
179
180 OS << "}\n";
181}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000182void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
183}
184void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000185 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
186 if (IsVerboseAsm) {
187 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
188 if (!Name.empty())
189 OS << "\t@ " << Name;
190 }
191 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000192}
193void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
194 StringRef String) {
195 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000196 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000197 OS << "\t.cpu\t" << String.lower();
198 break;
199 default:
200 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000201 if (IsVerboseAsm) {
202 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
203 if (!Name.empty())
204 OS << "\t@ " << Name;
205 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000206 break;
207 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000208 OS << "\n";
209}
210void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
211 unsigned IntValue,
212 StringRef StringValue) {
213 switch (Attribute) {
214 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
215 case ARMBuildAttrs::compatibility:
216 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
217 if (!StringValue.empty())
218 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000219 if (IsVerboseAsm)
220 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000221 break;
222 }
223 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000224}
Logan Chien439e8f92013-12-11 17:16:25 +0000225void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
226 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
227}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000228void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
229 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
230}
231void ARMTargetAsmStreamer::finishAttributeSection() {
232}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000233
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000234void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
235 OS << "\t.inst";
236 if (Suffix)
237 OS << "." << Suffix;
238 OS << "\t0x" << utohexstr(Inst) << "\n";
239}
240
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000241class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000242private:
243 // This structure holds all attributes, accounting for
244 // their string/numeric value, so we can later emmit them
245 // in declaration order, keeping all in the same vector
246 struct AttributeItem {
247 enum {
248 HiddenAttribute = 0,
249 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000250 TextAttribute,
251 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000252 } Type;
253 unsigned Tag;
254 unsigned IntValue;
255 StringRef StringValue;
256
257 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
258 return (LHS.Tag < RHS.Tag);
259 }
260 };
261
262 StringRef CurrentVendor;
263 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000264 unsigned Arch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000265 SmallVector<AttributeItem, 64> Contents;
266
267 const MCSection *AttributeSection;
268
269 // FIXME: this should be in a more generic place, but
270 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
271 static size_t getULEBSize(int Value) {
272 size_t Size = 0;
273 do {
274 Value >>= 7;
275 Size += sizeof(int8_t); // Is this really necessary?
276 } while (Value);
277 return Size;
278 }
279
280 AttributeItem *getAttributeItem(unsigned Attribute) {
281 for (size_t i = 0; i < Contents.size(); ++i)
282 if (Contents[i].Tag == Attribute)
283 return &Contents[i];
284 return 0;
285 }
286
287 void setAttributeItem(unsigned Attribute, unsigned Value,
288 bool OverwriteExisting) {
289 // Look for existing attribute item
290 if (AttributeItem *Item = getAttributeItem(Attribute)) {
291 if (!OverwriteExisting)
292 return;
293 Item->IntValue = Value;
294 return;
295 }
296
297 // Create new attribute item
298 AttributeItem Item = {
299 AttributeItem::NumericAttribute,
300 Attribute,
301 Value,
302 StringRef("")
303 };
304 Contents.push_back(Item);
305 }
306
307 void setAttributeItem(unsigned Attribute, StringRef Value,
308 bool OverwriteExisting) {
309 // Look for existing attribute item
310 if (AttributeItem *Item = getAttributeItem(Attribute)) {
311 if (!OverwriteExisting)
312 return;
313 Item->StringValue = Value;
314 return;
315 }
316
317 // Create new attribute item
318 AttributeItem Item = {
319 AttributeItem::TextAttribute,
320 Attribute,
321 0,
322 Value
323 };
324 Contents.push_back(Item);
325 }
326
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000327 void setAttributeItems(unsigned Attribute, unsigned IntValue,
328 StringRef StringValue, bool OverwriteExisting) {
329 // Look for existing attribute item
330 if (AttributeItem *Item = getAttributeItem(Attribute)) {
331 if (!OverwriteExisting)
332 return;
333 Item->IntValue = IntValue;
334 Item->StringValue = StringValue;
335 return;
336 }
337
338 // Create new attribute item
339 AttributeItem Item = {
340 AttributeItem::NumericAndTextAttributes,
341 Attribute,
342 IntValue,
343 StringValue
344 };
345 Contents.push_back(Item);
346 }
347
Logan Chien439e8f92013-12-11 17:16:25 +0000348 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000349 void emitFPUDefaultAttributes();
350
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000351 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000352
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000353 virtual void emitFnStart();
354 virtual void emitFnEnd();
355 virtual void emitCantUnwind();
356 virtual void emitPersonality(const MCSymbol *Personality);
357 virtual void emitHandlerData();
358 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
359 virtual void emitPad(int64_t Offset);
360 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
361 bool isVector);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000362
363 virtual void switchVendor(StringRef Vendor);
364 virtual void emitAttribute(unsigned Attribute, unsigned Value);
365 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000366 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
367 StringRef StringValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000368 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000369 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000370 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000371 virtual void finishAttributeSection();
372
373 size_t calculateContentSize() const;
374
375public:
376 ARMTargetELFStreamer()
377 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
Logan Chien439e8f92013-12-11 17:16:25 +0000378 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000379 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000380};
381
Tim Northover5cc3dc82012-12-07 16:50:23 +0000382/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
383/// the appropriate points in the object files. These symbols are defined in the
384/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
385///
386/// In brief: $a, $t or $d should be emitted at the start of each contiguous
387/// region of ARM code, Thumb code or data in a section. In practice, this
388/// emission does not rely on explicit assembler directives but on inherent
389/// properties of the directives doing the emission (e.g. ".byte" is data, "add
390/// r0, r0, r0" an instruction).
391///
392/// As a result this system is orthogonal to the DataRegion infrastructure used
393/// by MachO. Beware!
394class ARMELFStreamer : public MCELFStreamer {
395public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000396 friend class ARMTargetELFStreamer;
397
398 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
399 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
400 bool IsThumb)
401 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
402 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000403 Reset();
404 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000405
406 ~ARMELFStreamer() {}
407
Logan Chien8cbb80d2013-10-28 17:51:12 +0000408 virtual void FinishImpl();
409
Logan Chien2bcc42c2013-01-30 15:39:04 +0000410 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000411 void emitFnStart();
412 void emitFnEnd();
413 void emitCantUnwind();
414 void emitPersonality(const MCSymbol *Per);
415 void emitHandlerData();
416 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
417 void emitPad(int64_t Offset);
418 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000419
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000420 virtual void ChangeSection(const MCSection *Section,
421 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000422 // We have to keep track of the mapping symbol state of any sections we
423 // use. Each one should start off as EMS_None, which is provided as the
424 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000425 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000426 LastEMS = LastMappingSymbols.lookup(Section);
427
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000428 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000429 }
430
431 /// This function is the one used to emit instruction data into the ELF
432 /// streamer. We override it to add the appropriate mapping symbol if
433 /// necessary.
434 virtual void EmitInstruction(const MCInst& Inst) {
435 if (IsThumb)
436 EmitThumbMappingSymbol();
437 else
438 EmitARMMappingSymbol();
439
440 MCELFStreamer::EmitInstruction(Inst);
441 }
442
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000443 virtual void emitInst(uint32_t Inst, char Suffix) {
444 unsigned Size;
445 char Buffer[4];
446 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
447
448 switch (Suffix) {
449 case '\0':
450 Size = 4;
451
452 assert(!IsThumb);
453 EmitARMMappingSymbol();
454 for (unsigned II = 0, IE = Size; II != IE; II++) {
455 const unsigned I = LittleEndian ? (Size - II - 1) : II;
456 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
457 }
458
459 break;
460 case 'n':
461 case 'w':
462 Size = (Suffix == 'n' ? 2 : 4);
463
464 assert(IsThumb);
465 EmitThumbMappingSymbol();
466 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
467 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
468 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
469 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
470 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
471 }
472
473 break;
474 default:
475 llvm_unreachable("Invalid Suffix");
476 }
477
478 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
479 }
480
Tim Northover5cc3dc82012-12-07 16:50:23 +0000481 /// This is one of the functions used to emit data into an ELF section, so the
482 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
483 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000484 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000485 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000486 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000487 }
488
489 /// This is one of the functions used to emit data into an ELF section, so the
490 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
491 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000492 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000493 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000494 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000495 }
496
497 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
498 MCELFStreamer::EmitAssemblerFlag(Flag);
499
500 switch (Flag) {
501 case MCAF_SyntaxUnified:
502 return; // no-op here.
503 case MCAF_Code16:
504 IsThumb = true;
505 return; // Change to Thumb mode
506 case MCAF_Code32:
507 IsThumb = false;
508 return; // Change to ARM mode
509 case MCAF_Code64:
510 return;
511 case MCAF_SubsectionsViaSymbols:
512 return;
513 }
514 }
515
516private:
517 enum ElfMappingSymbol {
518 EMS_None,
519 EMS_ARM,
520 EMS_Thumb,
521 EMS_Data
522 };
523
524 void EmitDataMappingSymbol() {
525 if (LastEMS == EMS_Data) return;
526 EmitMappingSymbol("$d");
527 LastEMS = EMS_Data;
528 }
529
530 void EmitThumbMappingSymbol() {
531 if (LastEMS == EMS_Thumb) return;
532 EmitMappingSymbol("$t");
533 LastEMS = EMS_Thumb;
534 }
535
536 void EmitARMMappingSymbol() {
537 if (LastEMS == EMS_ARM) return;
538 EmitMappingSymbol("$a");
539 LastEMS = EMS_ARM;
540 }
541
542 void EmitMappingSymbol(StringRef Name) {
543 MCSymbol *Start = getContext().CreateTempSymbol();
544 EmitLabel(Start);
545
Chandler Carruth1d94e932012-12-08 03:10:14 +0000546 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000547 getContext().GetOrCreateSymbol(Name + "." +
548 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000549
550 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
551 MCELF::SetType(SD, ELF::STT_NOTYPE);
552 MCELF::SetBinding(SD, ELF::STB_LOCAL);
553 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000554 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000555
556 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
557 Symbol->setVariableValue(Value);
558 }
559
560 void EmitThumbFunc(MCSymbol *Func) {
561 // FIXME: Anything needed here to flag the function as thumb?
562
563 getAssembler().setIsThumbFunc(Func);
564
565 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
566 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
567 }
568
Logan Chien2bcc42c2013-01-30 15:39:04 +0000569 // Helper functions for ARM exception handling directives
570 void Reset();
571
572 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000573 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000574 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000575
576 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
577 SectionKind Kind, const MCSymbol &Fn);
578 void SwitchToExTabSection(const MCSymbol &FnStart);
579 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000580
581 bool IsThumb;
582 int64_t MappingSymbolCounter;
583
584 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
585 ElfMappingSymbol LastEMS;
586
Logan Chien2bcc42c2013-01-30 15:39:04 +0000587 // ARM Exception Handling Frame Information
588 MCSymbol *ExTab;
589 MCSymbol *FnStart;
590 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000591 unsigned PersonalityIndex;
592 unsigned FPReg; // Frame pointer register
593 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
594 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
595 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000596 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000597 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000598 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000599 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000600};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000601} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000602
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000603ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Benjamin Kramer41882932013-10-09 17:23:41 +0000604 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000605 return *S;
606}
607
608void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
609void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
610void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
611void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
612 getStreamer().emitPersonality(Personality);
613}
614void ARMTargetELFStreamer::emitHandlerData() {
615 getStreamer().emitHandlerData();
616}
617void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
618 int64_t Offset) {
619 getStreamer().emitSetFP(FpReg, SpReg, Offset);
620}
621void ARMTargetELFStreamer::emitPad(int64_t Offset) {
622 getStreamer().emitPad(Offset);
623}
624void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
625 bool isVector) {
626 getStreamer().emitRegSave(RegList, isVector);
627}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000628void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
629 assert(!Vendor.empty() && "Vendor cannot be empty.");
630
631 if (CurrentVendor == Vendor)
632 return;
633
634 if (!CurrentVendor.empty())
635 finishAttributeSection();
636
637 assert(Contents.empty() &&
638 ".ARM.attributes should be flushed before changing vendor");
639 CurrentVendor = Vendor;
640
641}
642void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
643 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
644}
645void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
646 StringRef Value) {
647 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
648}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000649void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
650 unsigned IntValue,
651 StringRef StringValue) {
652 setAttributeItems(Attribute, IntValue, StringValue,
653 /* OverwriteExisting= */ true);
654}
Logan Chien439e8f92013-12-11 17:16:25 +0000655void ARMTargetELFStreamer::emitArch(unsigned Value) {
656 Arch = Value;
657}
658void ARMTargetELFStreamer::emitArchDefaultAttributes() {
659 using namespace ARMBuildAttrs;
660 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
661 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
662
663 switch (Arch) {
664 case ARM::ARMV2:
665 case ARM::ARMV2A:
666 case ARM::ARMV3:
667 case ARM::ARMV3M:
668 case ARM::ARMV4:
669 case ARM::ARMV5:
670 setAttributeItem(ARM_ISA_use, Allowed, false);
671 break;
672
673 case ARM::ARMV4T:
674 case ARM::ARMV5T:
675 case ARM::ARMV5TE:
676 case ARM::ARMV6:
677 case ARM::ARMV6J:
678 setAttributeItem(ARM_ISA_use, Allowed, false);
679 setAttributeItem(THUMB_ISA_use, Allowed, false);
680 break;
681
682 case ARM::ARMV6T2:
683 setAttributeItem(ARM_ISA_use, Allowed, false);
684 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
685 break;
686
687 case ARM::ARMV6Z:
688 case ARM::ARMV6ZK:
689 setAttributeItem(ARM_ISA_use, Allowed, false);
690 setAttributeItem(THUMB_ISA_use, Allowed, false);
691 setAttributeItem(Virtualization_use, AllowTZ, false);
692 break;
693
694 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000695 setAttributeItem(THUMB_ISA_use, Allowed, false);
696 break;
697
698 case ARM::ARMV7:
699 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
700 break;
701
702 case ARM::ARMV7A:
703 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
704 setAttributeItem(ARM_ISA_use, Allowed, false);
705 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
706 break;
707
708 case ARM::ARMV7R:
709 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
710 setAttributeItem(ARM_ISA_use, Allowed, false);
711 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
712 break;
713
714 case ARM::ARMV7M:
715 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
716 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
717 break;
718
719 case ARM::ARMV8A:
720 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
721 setAttributeItem(ARM_ISA_use, Allowed, false);
722 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
723 setAttributeItem(MPextension_use, Allowed, false);
724 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
725 break;
726
727 case ARM::IWMMXT:
728 setAttributeItem(ARM_ISA_use, Allowed, false);
729 setAttributeItem(THUMB_ISA_use, Allowed, false);
730 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
731 break;
732
733 case ARM::IWMMXT2:
734 setAttributeItem(ARM_ISA_use, Allowed, false);
735 setAttributeItem(THUMB_ISA_use, Allowed, false);
736 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
737 break;
738
739 default:
740 report_fatal_error("Unknown Arch: " + Twine(Arch));
741 break;
742 }
743}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000744void ARMTargetELFStreamer::emitFPU(unsigned Value) {
745 FPU = Value;
746}
747void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
748 switch (FPU) {
749 case ARM::VFP:
750 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000751 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000752 ARMBuildAttrs::AllowFPv2,
753 /* OverwriteExisting= */ false);
754 break;
755
756 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000757 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000758 ARMBuildAttrs::AllowFPv3A,
759 /* OverwriteExisting= */ false);
760 break;
761
762 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000763 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000764 ARMBuildAttrs::AllowFPv3B,
765 /* OverwriteExisting= */ false);
766 break;
767
768 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000769 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000770 ARMBuildAttrs::AllowFPv4A,
771 /* OverwriteExisting= */ false);
772 break;
773
774 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000775 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000776 ARMBuildAttrs::AllowFPv4B,
777 /* OverwriteExisting= */ false);
778 break;
779
780 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000781 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000782 ARMBuildAttrs::AllowFPARMv8A,
783 /* OverwriteExisting= */ false);
784 break;
785
786 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000787 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000788 ARMBuildAttrs::AllowFPv3A,
789 /* OverwriteExisting= */ false);
790 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
791 ARMBuildAttrs::AllowNeon,
792 /* OverwriteExisting= */ false);
793 break;
794
795 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000796 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000797 ARMBuildAttrs::AllowFPv4A,
798 /* OverwriteExisting= */ false);
799 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
800 ARMBuildAttrs::AllowNeon2,
801 /* OverwriteExisting= */ false);
802 break;
803
804 case ARM::NEON_FP_ARMV8:
805 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000806 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000807 ARMBuildAttrs::AllowFPARMv8A,
808 /* OverwriteExisting= */ false);
809 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
810 ARMBuildAttrs::AllowNeonARMv8,
811 /* OverwriteExisting= */ false);
812 break;
813
Logan Chien05ae7442014-01-02 15:50:02 +0000814 case ARM::SOFTVFP:
815 break;
816
Logan Chien8cbb80d2013-10-28 17:51:12 +0000817 default:
818 report_fatal_error("Unknown FPU: " + Twine(FPU));
819 break;
820 }
821}
822size_t ARMTargetELFStreamer::calculateContentSize() const {
823 size_t Result = 0;
824 for (size_t i = 0; i < Contents.size(); ++i) {
825 AttributeItem item = Contents[i];
826 switch (item.Type) {
827 case AttributeItem::HiddenAttribute:
828 break;
829 case AttributeItem::NumericAttribute:
830 Result += getULEBSize(item.Tag);
831 Result += getULEBSize(item.IntValue);
832 break;
833 case AttributeItem::TextAttribute:
834 Result += getULEBSize(item.Tag);
835 Result += item.StringValue.size() + 1; // string + '\0'
836 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000837 case AttributeItem::NumericAndTextAttributes:
838 Result += getULEBSize(item.Tag);
839 Result += getULEBSize(item.IntValue);
840 Result += item.StringValue.size() + 1; // string + '\0';
841 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000842 }
843 }
844 return Result;
845}
846void ARMTargetELFStreamer::finishAttributeSection() {
847 // <format-version>
848 // [ <section-length> "vendor-name"
849 // [ <file-tag> <size> <attribute>*
850 // | <section-tag> <size> <section-number>* 0 <attribute>*
851 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
852 // ]+
853 // ]*
854
855 if (FPU != ARM::INVALID_FPU)
856 emitFPUDefaultAttributes();
857
Logan Chien439e8f92013-12-11 17:16:25 +0000858 if (Arch != ARM::INVALID_ARCH)
859 emitArchDefaultAttributes();
860
Logan Chien8cbb80d2013-10-28 17:51:12 +0000861 if (Contents.empty())
862 return;
863
864 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
865
866 ARMELFStreamer &Streamer = getStreamer();
867
868 // Switch to .ARM.attributes section
869 if (AttributeSection) {
870 Streamer.SwitchSection(AttributeSection);
871 } else {
872 AttributeSection =
873 Streamer.getContext().getELFSection(".ARM.attributes",
874 ELF::SHT_ARM_ATTRIBUTES,
875 0,
876 SectionKind::getMetadata());
877 Streamer.SwitchSection(AttributeSection);
878
879 // Format version
880 Streamer.EmitIntValue(0x41, 1);
881 }
882
883 // Vendor size + Vendor name + '\0'
884 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
885
886 // Tag + Tag Size
887 const size_t TagHeaderSize = 1 + 4;
888
889 const size_t ContentsSize = calculateContentSize();
890
891 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
892 Streamer.EmitBytes(CurrentVendor);
893 Streamer.EmitIntValue(0, 1); // '\0'
894
895 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
896 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
897
898 // Size should have been accounted for already, now
899 // emit each field as its type (ULEB or String)
900 for (size_t i = 0; i < Contents.size(); ++i) {
901 AttributeItem item = Contents[i];
902 Streamer.EmitULEB128IntValue(item.Tag);
903 switch (item.Type) {
904 default: llvm_unreachable("Invalid attribute type");
905 case AttributeItem::NumericAttribute:
906 Streamer.EmitULEB128IntValue(item.IntValue);
907 break;
908 case AttributeItem::TextAttribute:
909 Streamer.EmitBytes(item.StringValue.upper());
910 Streamer.EmitIntValue(0, 1); // '\0'
911 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000912 case AttributeItem::NumericAndTextAttributes:
913 Streamer.EmitULEB128IntValue(item.IntValue);
914 Streamer.EmitBytes(item.StringValue.upper());
915 Streamer.EmitIntValue(0, 1); // '\0'
916 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000917 }
918 }
919
920 Contents.clear();
921 FPU = ARM::INVALID_FPU;
922}
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000923void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
924 getStreamer().emitInst(Inst, Suffix);
925}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000926
927void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +0000928 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000929 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
930 ATS.finishAttributeSection();
931
932 MCELFStreamer::FinishImpl();
933}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000934
Logan Chien2bcc42c2013-01-30 15:39:04 +0000935inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
936 unsigned Type,
937 unsigned Flags,
938 SectionKind Kind,
939 const MCSymbol &Fn) {
940 const MCSectionELF &FnSection =
941 static_cast<const MCSectionELF &>(Fn.getSection());
942
943 // Create the name for new section
944 StringRef FnSecName(FnSection.getSectionName());
945 SmallString<128> EHSecName(Prefix);
946 if (FnSecName != ".text") {
947 EHSecName += FnSecName;
948 }
949
950 // Get .ARM.extab or .ARM.exidx section
951 const MCSectionELF *EHSection = NULL;
952 if (const MCSymbol *Group = FnSection.getGroup()) {
953 EHSection = getContext().getELFSection(
954 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
955 FnSection.getEntrySize(), Group->getName());
956 } else {
957 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
958 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000959 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +0000960
961 // Switch to .ARM.extab or .ARM.exidx section
962 SwitchSection(EHSection);
963 EmitCodeAlignment(4, 0);
964}
965
966inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
967 SwitchToEHSection(".ARM.extab",
968 ELF::SHT_PROGBITS,
969 ELF::SHF_ALLOC,
970 SectionKind::getDataRel(),
971 FnStart);
972}
973
974inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
975 SwitchToEHSection(".ARM.exidx",
976 ELF::SHT_ARM_EXIDX,
977 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
978 SectionKind::getDataRel(),
979 FnStart);
980}
981
982void ARMELFStreamer::Reset() {
983 ExTab = NULL;
984 FnStart = NULL;
985 Personality = NULL;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +0000986 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +0000987 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000988 FPOffset = 0;
989 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +0000990 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000991 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000992 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000993
Logan Chien325823a2013-06-09 12:22:30 +0000994 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000995 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +0000996}
997
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000998void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +0000999 assert(FnStart == 0);
1000 FnStart = getContext().CreateTempSymbol();
1001 EmitLabel(FnStart);
1002}
1003
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001004void ARMELFStreamer::emitFnEnd() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001005 assert(FnStart && ".fnstart must preceeds .fnend");
1006
1007 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001008 if (!ExTab && !CantUnwind)
1009 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001010
1011 // Emit the exception index table entry
1012 SwitchToExIdxSection(*FnStart);
1013
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001014 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001015 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001016
1017 const MCSymbolRefExpr *FnStartRef =
1018 MCSymbolRefExpr::Create(FnStart,
1019 MCSymbolRefExpr::VK_ARM_PREL31,
1020 getContext());
1021
Rafael Espindola64e1af82013-07-02 15:49:13 +00001022 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001023
1024 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001025 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001026 } else if (ExTab) {
1027 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001028 const MCSymbolRefExpr *ExTabEntryRef =
1029 MCSymbolRefExpr::Create(ExTab,
1030 MCSymbolRefExpr::VK_ARM_PREL31,
1031 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001032 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001033 } else {
1034 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1035 // the second word of exception index table entry. The size of the unwind
1036 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001037 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001038 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001039 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001040 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +00001041 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001042 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001043 }
1044
Logan Chien4ea23b52013-05-10 16:17:24 +00001045 // Switch to the section containing FnStart
1046 SwitchSection(&FnStart->getSection());
1047
Logan Chien2bcc42c2013-01-30 15:39:04 +00001048 // Clean exception handling frame information
1049 Reset();
1050}
1051
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001052void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1053
1054// Add the R_ARM_NONE fixup at the same position
1055void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1056 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1057
1058 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1059 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1060
1061 AddValueSymbols(PersonalityRef);
1062 MCDataFragment *DF = getOrCreateDataFragment();
1063 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1064 PersonalityRef,
1065 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001066}
1067
Logan Chien325823a2013-06-09 12:22:30 +00001068void ARMELFStreamer::FlushPendingOffset() {
1069 if (PendingOffset != 0) {
1070 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1071 PendingOffset = 0;
1072 }
1073}
1074
Logan Chienc931fce2013-07-02 12:43:27 +00001075void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001076 // Emit the unwind opcode to restore $sp.
1077 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001078 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001079 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1080 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001081 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001082 } else {
1083 FlushPendingOffset();
1084 }
1085
1086 // Finalize the unwind opcode sequence
1087 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001088
1089 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1090 // section. Thus, we don't have to create an entry in the .ARM.extab
1091 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001092 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001093 return;
1094
1095 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001096 SwitchToExTabSection(*FnStart);
1097
1098 // Create .ARM.extab label for offset in .ARM.exidx
1099 assert(!ExTab);
1100 ExTab = getContext().CreateTempSymbol();
1101 EmitLabel(ExTab);
1102
Logan Chien4ea23b52013-05-10 16:17:24 +00001103 // Emit personality
1104 if (Personality) {
1105 const MCSymbolRefExpr *PersonalityRef =
1106 MCSymbolRefExpr::Create(Personality,
1107 MCSymbolRefExpr::VK_ARM_PREL31,
1108 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001109
Rafael Espindola64e1af82013-07-02 15:49:13 +00001110 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001111 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001112
1113 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +00001114 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001115 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +00001116
1117 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1118 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1119 // after the unwind opcodes. The handler data consists of several 32-bit
1120 // words, and should be terminated by zero.
1121 //
1122 // In case that the .handlerdata directive is not specified by the
1123 // programmer, we should emit zero to terminate the handler data.
1124 if (NoHandlerData && !Personality)
1125 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001126}
1127
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001128void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001129
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001130void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001131 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001132 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001133}
1134
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001135void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001136 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001137 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001138 "the operand of .setfp directive should be either $sp or $fp");
1139
1140 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001141 FPReg = NewFPReg;
1142
1143 if (NewSPReg == ARM::SP)
1144 FPOffset = SPOffset + Offset;
1145 else
1146 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001147}
1148
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001149void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001150 // Track the change of the $sp offset
1151 SPOffset -= Offset;
1152
1153 // To squash multiple .pad directives, we should delay the unwind opcode
1154 // until the .save, .vsave, .handlerdata, or .fnend directives.
1155 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001156}
1157
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001158void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001159 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001160 // Collect the registers in the register list
1161 unsigned Count = 0;
1162 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001163 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001164 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001165 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001166 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001167 unsigned Bit = (1u << Reg);
1168 if ((Mask & Bit) == 0) {
1169 Mask |= Bit;
1170 ++Count;
1171 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001172 }
Logan Chien325823a2013-06-09 12:22:30 +00001173
1174 // Track the change the $sp offset: For the .save directive, the
1175 // corresponding push instruction will decrease the $sp by (4 * Count).
1176 // For the .vsave directive, the corresponding vpush instruction will
1177 // decrease $sp by (8 * Count).
1178 SPOffset -= Count * (IsVector ? 8 : 4);
1179
1180 // Emit the opcode
1181 FlushPendingOffset();
1182 if (IsVector)
1183 UnwindOpAsm.EmitVFPRegSave(Mask);
1184 else
1185 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001186}
1187
Tim Northover5cc3dc82012-12-07 16:50:23 +00001188namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001189
1190MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1191 bool isVerboseAsm, bool useLoc, bool useCFI,
1192 bool useDwarfDirectory,
1193 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1194 MCAsmBackend *TAB, bool ShowInst) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +00001195 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1196 isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001197
1198 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1199 useDwarfDirectory, InstPrint, CE, TAB,
1200 ShowInst);
1201}
1202
Tim Northover5cc3dc82012-12-07 16:50:23 +00001203 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1204 raw_ostream &OS, MCCodeEmitter *Emitter,
1205 bool RelaxAll, bool NoExecStack,
1206 bool IsThumb) {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001207 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1208 ARMELFStreamer *S =
1209 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001210 // FIXME: This should eventually end up somewhere else where more
1211 // intelligent flag decisions can be made. For now we are just maintaining
1212 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1213 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1214
Tim Northover5cc3dc82012-12-07 16:50:23 +00001215 if (RelaxAll)
1216 S->getAssembler().setRelaxAll(true);
1217 if (NoExecStack)
1218 S->getAssembler().setNoExecStack(true);
1219 return S;
1220 }
1221
1222}
1223
1224