blob: 4c079262943ac899b39fc295fb4edb205467ad25 [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;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000293 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000294 Item->IntValue = Value;
295 return;
296 }
297
298 // Create new attribute item
299 AttributeItem Item = {
300 AttributeItem::NumericAttribute,
301 Attribute,
302 Value,
303 StringRef("")
304 };
305 Contents.push_back(Item);
306 }
307
308 void setAttributeItem(unsigned Attribute, StringRef 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::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000315 Item->StringValue = Value;
316 return;
317 }
318
319 // Create new attribute item
320 AttributeItem Item = {
321 AttributeItem::TextAttribute,
322 Attribute,
323 0,
324 Value
325 };
326 Contents.push_back(Item);
327 }
328
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000329 void setAttributeItems(unsigned Attribute, unsigned IntValue,
330 StringRef StringValue, 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::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000336 Item->IntValue = IntValue;
337 Item->StringValue = StringValue;
338 return;
339 }
340
341 // Create new attribute item
342 AttributeItem Item = {
343 AttributeItem::NumericAndTextAttributes,
344 Attribute,
345 IntValue,
346 StringValue
347 };
348 Contents.push_back(Item);
349 }
350
Logan Chien439e8f92013-12-11 17:16:25 +0000351 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000352 void emitFPUDefaultAttributes();
353
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000354 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000355
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000356 virtual void emitFnStart();
357 virtual void emitFnEnd();
358 virtual void emitCantUnwind();
359 virtual void emitPersonality(const MCSymbol *Personality);
360 virtual void emitHandlerData();
361 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
362 virtual void emitPad(int64_t Offset);
363 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
364 bool isVector);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000365
366 virtual void switchVendor(StringRef Vendor);
367 virtual void emitAttribute(unsigned Attribute, unsigned Value);
368 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000369 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
370 StringRef StringValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000371 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000372 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000373 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000374 virtual void finishAttributeSection();
375
376 size_t calculateContentSize() const;
377
378public:
379 ARMTargetELFStreamer()
380 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
Logan Chien439e8f92013-12-11 17:16:25 +0000381 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000382 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000383};
384
Tim Northover5cc3dc82012-12-07 16:50:23 +0000385/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
386/// the appropriate points in the object files. These symbols are defined in the
387/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
388///
389/// In brief: $a, $t or $d should be emitted at the start of each contiguous
390/// region of ARM code, Thumb code or data in a section. In practice, this
391/// emission does not rely on explicit assembler directives but on inherent
392/// properties of the directives doing the emission (e.g. ".byte" is data, "add
393/// r0, r0, r0" an instruction).
394///
395/// As a result this system is orthogonal to the DataRegion infrastructure used
396/// by MachO. Beware!
397class ARMELFStreamer : public MCELFStreamer {
398public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000399 friend class ARMTargetELFStreamer;
400
401 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
402 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
403 bool IsThumb)
404 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
405 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000406 Reset();
407 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000408
409 ~ARMELFStreamer() {}
410
Logan Chien8cbb80d2013-10-28 17:51:12 +0000411 virtual void FinishImpl();
412
Logan Chien2bcc42c2013-01-30 15:39:04 +0000413 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000414 void emitFnStart();
415 void emitFnEnd();
416 void emitCantUnwind();
417 void emitPersonality(const MCSymbol *Per);
418 void emitHandlerData();
419 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
420 void emitPad(int64_t Offset);
421 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000422
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000423 virtual void ChangeSection(const MCSection *Section,
424 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000425 // We have to keep track of the mapping symbol state of any sections we
426 // use. Each one should start off as EMS_None, which is provided as the
427 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000428 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000429 LastEMS = LastMappingSymbols.lookup(Section);
430
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000431 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000432 }
433
434 /// This function is the one used to emit instruction data into the ELF
435 /// streamer. We override it to add the appropriate mapping symbol if
436 /// necessary.
437 virtual void EmitInstruction(const MCInst& Inst) {
438 if (IsThumb)
439 EmitThumbMappingSymbol();
440 else
441 EmitARMMappingSymbol();
442
443 MCELFStreamer::EmitInstruction(Inst);
444 }
445
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000446 virtual void emitInst(uint32_t Inst, char Suffix) {
447 unsigned Size;
448 char Buffer[4];
449 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
450
451 switch (Suffix) {
452 case '\0':
453 Size = 4;
454
455 assert(!IsThumb);
456 EmitARMMappingSymbol();
457 for (unsigned II = 0, IE = Size; II != IE; II++) {
458 const unsigned I = LittleEndian ? (Size - II - 1) : II;
459 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
460 }
461
462 break;
463 case 'n':
464 case 'w':
465 Size = (Suffix == 'n' ? 2 : 4);
466
467 assert(IsThumb);
468 EmitThumbMappingSymbol();
469 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
470 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
471 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
472 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
473 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
474 }
475
476 break;
477 default:
478 llvm_unreachable("Invalid Suffix");
479 }
480
481 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
482 }
483
Tim Northover5cc3dc82012-12-07 16:50:23 +0000484 /// This is one of the functions used to emit data into an ELF section, so the
485 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
486 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000487 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000488 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000489 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000490 }
491
492 /// This is one of the functions used to emit data into an ELF section, so the
493 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
494 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000495 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000496 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000497 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000498 }
499
500 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
501 MCELFStreamer::EmitAssemblerFlag(Flag);
502
503 switch (Flag) {
504 case MCAF_SyntaxUnified:
505 return; // no-op here.
506 case MCAF_Code16:
507 IsThumb = true;
508 return; // Change to Thumb mode
509 case MCAF_Code32:
510 IsThumb = false;
511 return; // Change to ARM mode
512 case MCAF_Code64:
513 return;
514 case MCAF_SubsectionsViaSymbols:
515 return;
516 }
517 }
518
519private:
520 enum ElfMappingSymbol {
521 EMS_None,
522 EMS_ARM,
523 EMS_Thumb,
524 EMS_Data
525 };
526
527 void EmitDataMappingSymbol() {
528 if (LastEMS == EMS_Data) return;
529 EmitMappingSymbol("$d");
530 LastEMS = EMS_Data;
531 }
532
533 void EmitThumbMappingSymbol() {
534 if (LastEMS == EMS_Thumb) return;
535 EmitMappingSymbol("$t");
536 LastEMS = EMS_Thumb;
537 }
538
539 void EmitARMMappingSymbol() {
540 if (LastEMS == EMS_ARM) return;
541 EmitMappingSymbol("$a");
542 LastEMS = EMS_ARM;
543 }
544
545 void EmitMappingSymbol(StringRef Name) {
546 MCSymbol *Start = getContext().CreateTempSymbol();
547 EmitLabel(Start);
548
Chandler Carruth1d94e932012-12-08 03:10:14 +0000549 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000550 getContext().GetOrCreateSymbol(Name + "." +
551 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000552
553 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
554 MCELF::SetType(SD, ELF::STT_NOTYPE);
555 MCELF::SetBinding(SD, ELF::STB_LOCAL);
556 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000557 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000558
559 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
560 Symbol->setVariableValue(Value);
561 }
562
563 void EmitThumbFunc(MCSymbol *Func) {
564 // FIXME: Anything needed here to flag the function as thumb?
565
566 getAssembler().setIsThumbFunc(Func);
567
568 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
569 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
570 }
571
Logan Chien2bcc42c2013-01-30 15:39:04 +0000572 // Helper functions for ARM exception handling directives
573 void Reset();
574
575 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000576 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000577 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000578
579 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
580 SectionKind Kind, const MCSymbol &Fn);
581 void SwitchToExTabSection(const MCSymbol &FnStart);
582 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000583
584 bool IsThumb;
585 int64_t MappingSymbolCounter;
586
587 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
588 ElfMappingSymbol LastEMS;
589
Logan Chien2bcc42c2013-01-30 15:39:04 +0000590 // ARM Exception Handling Frame Information
591 MCSymbol *ExTab;
592 MCSymbol *FnStart;
593 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000594 unsigned PersonalityIndex;
595 unsigned FPReg; // Frame pointer register
596 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
597 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
598 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000599 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000600 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000601 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000602 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000603};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000604} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000605
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000606ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Benjamin Kramer41882932013-10-09 17:23:41 +0000607 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000608 return *S;
609}
610
611void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
612void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
613void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
614void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
615 getStreamer().emitPersonality(Personality);
616}
617void ARMTargetELFStreamer::emitHandlerData() {
618 getStreamer().emitHandlerData();
619}
620void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
621 int64_t Offset) {
622 getStreamer().emitSetFP(FpReg, SpReg, Offset);
623}
624void ARMTargetELFStreamer::emitPad(int64_t Offset) {
625 getStreamer().emitPad(Offset);
626}
627void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
628 bool isVector) {
629 getStreamer().emitRegSave(RegList, isVector);
630}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000631void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
632 assert(!Vendor.empty() && "Vendor cannot be empty.");
633
634 if (CurrentVendor == Vendor)
635 return;
636
637 if (!CurrentVendor.empty())
638 finishAttributeSection();
639
640 assert(Contents.empty() &&
641 ".ARM.attributes should be flushed before changing vendor");
642 CurrentVendor = Vendor;
643
644}
645void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
646 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
647}
648void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
649 StringRef Value) {
650 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
651}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000652void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
653 unsigned IntValue,
654 StringRef StringValue) {
655 setAttributeItems(Attribute, IntValue, StringValue,
656 /* OverwriteExisting= */ true);
657}
Logan Chien439e8f92013-12-11 17:16:25 +0000658void ARMTargetELFStreamer::emitArch(unsigned Value) {
659 Arch = Value;
660}
661void ARMTargetELFStreamer::emitArchDefaultAttributes() {
662 using namespace ARMBuildAttrs;
663 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
664 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
665
666 switch (Arch) {
667 case ARM::ARMV2:
668 case ARM::ARMV2A:
669 case ARM::ARMV3:
670 case ARM::ARMV3M:
671 case ARM::ARMV4:
672 case ARM::ARMV5:
673 setAttributeItem(ARM_ISA_use, Allowed, false);
674 break;
675
676 case ARM::ARMV4T:
677 case ARM::ARMV5T:
678 case ARM::ARMV5TE:
679 case ARM::ARMV6:
680 case ARM::ARMV6J:
681 setAttributeItem(ARM_ISA_use, Allowed, false);
682 setAttributeItem(THUMB_ISA_use, Allowed, false);
683 break;
684
685 case ARM::ARMV6T2:
686 setAttributeItem(ARM_ISA_use, Allowed, false);
687 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
688 break;
689
690 case ARM::ARMV6Z:
691 case ARM::ARMV6ZK:
692 setAttributeItem(ARM_ISA_use, Allowed, false);
693 setAttributeItem(THUMB_ISA_use, Allowed, false);
694 setAttributeItem(Virtualization_use, AllowTZ, false);
695 break;
696
697 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000698 setAttributeItem(THUMB_ISA_use, Allowed, false);
699 break;
700
701 case ARM::ARMV7:
702 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
703 break;
704
705 case ARM::ARMV7A:
706 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
707 setAttributeItem(ARM_ISA_use, Allowed, false);
708 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
709 break;
710
711 case ARM::ARMV7R:
712 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
713 setAttributeItem(ARM_ISA_use, Allowed, false);
714 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
715 break;
716
717 case ARM::ARMV7M:
718 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
719 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
720 break;
721
722 case ARM::ARMV8A:
723 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
724 setAttributeItem(ARM_ISA_use, Allowed, false);
725 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
726 setAttributeItem(MPextension_use, Allowed, false);
727 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
728 break;
729
730 case ARM::IWMMXT:
731 setAttributeItem(ARM_ISA_use, Allowed, false);
732 setAttributeItem(THUMB_ISA_use, Allowed, false);
733 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
734 break;
735
736 case ARM::IWMMXT2:
737 setAttributeItem(ARM_ISA_use, Allowed, false);
738 setAttributeItem(THUMB_ISA_use, Allowed, false);
739 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
740 break;
741
742 default:
743 report_fatal_error("Unknown Arch: " + Twine(Arch));
744 break;
745 }
746}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000747void ARMTargetELFStreamer::emitFPU(unsigned Value) {
748 FPU = Value;
749}
750void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
751 switch (FPU) {
752 case ARM::VFP:
753 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000754 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000755 ARMBuildAttrs::AllowFPv2,
756 /* OverwriteExisting= */ false);
757 break;
758
759 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000760 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000761 ARMBuildAttrs::AllowFPv3A,
762 /* OverwriteExisting= */ false);
763 break;
764
765 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000766 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000767 ARMBuildAttrs::AllowFPv3B,
768 /* OverwriteExisting= */ false);
769 break;
770
771 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000772 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000773 ARMBuildAttrs::AllowFPv4A,
774 /* OverwriteExisting= */ false);
775 break;
776
777 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000778 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000779 ARMBuildAttrs::AllowFPv4B,
780 /* OverwriteExisting= */ false);
781 break;
782
783 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000784 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000785 ARMBuildAttrs::AllowFPARMv8A,
786 /* OverwriteExisting= */ false);
787 break;
788
789 case ARM::NEON:
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 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
794 ARMBuildAttrs::AllowNeon,
795 /* OverwriteExisting= */ false);
796 break;
797
798 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000799 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000800 ARMBuildAttrs::AllowFPv4A,
801 /* OverwriteExisting= */ false);
802 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
803 ARMBuildAttrs::AllowNeon2,
804 /* OverwriteExisting= */ false);
805 break;
806
807 case ARM::NEON_FP_ARMV8:
808 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000809 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000810 ARMBuildAttrs::AllowFPARMv8A,
811 /* OverwriteExisting= */ false);
812 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
813 ARMBuildAttrs::AllowNeonARMv8,
814 /* OverwriteExisting= */ false);
815 break;
816
Logan Chien05ae7442014-01-02 15:50:02 +0000817 case ARM::SOFTVFP:
818 break;
819
Logan Chien8cbb80d2013-10-28 17:51:12 +0000820 default:
821 report_fatal_error("Unknown FPU: " + Twine(FPU));
822 break;
823 }
824}
825size_t ARMTargetELFStreamer::calculateContentSize() const {
826 size_t Result = 0;
827 for (size_t i = 0; i < Contents.size(); ++i) {
828 AttributeItem item = Contents[i];
829 switch (item.Type) {
830 case AttributeItem::HiddenAttribute:
831 break;
832 case AttributeItem::NumericAttribute:
833 Result += getULEBSize(item.Tag);
834 Result += getULEBSize(item.IntValue);
835 break;
836 case AttributeItem::TextAttribute:
837 Result += getULEBSize(item.Tag);
838 Result += item.StringValue.size() + 1; // string + '\0'
839 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000840 case AttributeItem::NumericAndTextAttributes:
841 Result += getULEBSize(item.Tag);
842 Result += getULEBSize(item.IntValue);
843 Result += item.StringValue.size() + 1; // string + '\0';
844 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000845 }
846 }
847 return Result;
848}
849void ARMTargetELFStreamer::finishAttributeSection() {
850 // <format-version>
851 // [ <section-length> "vendor-name"
852 // [ <file-tag> <size> <attribute>*
853 // | <section-tag> <size> <section-number>* 0 <attribute>*
854 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
855 // ]+
856 // ]*
857
858 if (FPU != ARM::INVALID_FPU)
859 emitFPUDefaultAttributes();
860
Logan Chien439e8f92013-12-11 17:16:25 +0000861 if (Arch != ARM::INVALID_ARCH)
862 emitArchDefaultAttributes();
863
Logan Chien8cbb80d2013-10-28 17:51:12 +0000864 if (Contents.empty())
865 return;
866
867 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
868
869 ARMELFStreamer &Streamer = getStreamer();
870
871 // Switch to .ARM.attributes section
872 if (AttributeSection) {
873 Streamer.SwitchSection(AttributeSection);
874 } else {
875 AttributeSection =
876 Streamer.getContext().getELFSection(".ARM.attributes",
877 ELF::SHT_ARM_ATTRIBUTES,
878 0,
879 SectionKind::getMetadata());
880 Streamer.SwitchSection(AttributeSection);
881
882 // Format version
883 Streamer.EmitIntValue(0x41, 1);
884 }
885
886 // Vendor size + Vendor name + '\0'
887 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
888
889 // Tag + Tag Size
890 const size_t TagHeaderSize = 1 + 4;
891
892 const size_t ContentsSize = calculateContentSize();
893
894 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
895 Streamer.EmitBytes(CurrentVendor);
896 Streamer.EmitIntValue(0, 1); // '\0'
897
898 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
899 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
900
901 // Size should have been accounted for already, now
902 // emit each field as its type (ULEB or String)
903 for (size_t i = 0; i < Contents.size(); ++i) {
904 AttributeItem item = Contents[i];
905 Streamer.EmitULEB128IntValue(item.Tag);
906 switch (item.Type) {
907 default: llvm_unreachable("Invalid attribute type");
908 case AttributeItem::NumericAttribute:
909 Streamer.EmitULEB128IntValue(item.IntValue);
910 break;
911 case AttributeItem::TextAttribute:
912 Streamer.EmitBytes(item.StringValue.upper());
913 Streamer.EmitIntValue(0, 1); // '\0'
914 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000915 case AttributeItem::NumericAndTextAttributes:
916 Streamer.EmitULEB128IntValue(item.IntValue);
917 Streamer.EmitBytes(item.StringValue.upper());
918 Streamer.EmitIntValue(0, 1); // '\0'
919 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000920 }
921 }
922
923 Contents.clear();
924 FPU = ARM::INVALID_FPU;
925}
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000926void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
927 getStreamer().emitInst(Inst, Suffix);
928}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000929
930void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +0000931 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000932 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
933 ATS.finishAttributeSection();
934
935 MCELFStreamer::FinishImpl();
936}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000937
Logan Chien2bcc42c2013-01-30 15:39:04 +0000938inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
939 unsigned Type,
940 unsigned Flags,
941 SectionKind Kind,
942 const MCSymbol &Fn) {
943 const MCSectionELF &FnSection =
944 static_cast<const MCSectionELF &>(Fn.getSection());
945
946 // Create the name for new section
947 StringRef FnSecName(FnSection.getSectionName());
948 SmallString<128> EHSecName(Prefix);
949 if (FnSecName != ".text") {
950 EHSecName += FnSecName;
951 }
952
953 // Get .ARM.extab or .ARM.exidx section
954 const MCSectionELF *EHSection = NULL;
955 if (const MCSymbol *Group = FnSection.getGroup()) {
956 EHSection = getContext().getELFSection(
957 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
958 FnSection.getEntrySize(), Group->getName());
959 } else {
960 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
961 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000962 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +0000963
964 // Switch to .ARM.extab or .ARM.exidx section
965 SwitchSection(EHSection);
966 EmitCodeAlignment(4, 0);
967}
968
969inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
970 SwitchToEHSection(".ARM.extab",
971 ELF::SHT_PROGBITS,
972 ELF::SHF_ALLOC,
973 SectionKind::getDataRel(),
974 FnStart);
975}
976
977inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
978 SwitchToEHSection(".ARM.exidx",
979 ELF::SHT_ARM_EXIDX,
980 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
981 SectionKind::getDataRel(),
982 FnStart);
983}
984
985void ARMELFStreamer::Reset() {
986 ExTab = NULL;
987 FnStart = NULL;
988 Personality = NULL;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +0000989 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +0000990 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000991 FPOffset = 0;
992 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +0000993 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000994 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000995 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000996
Logan Chien325823a2013-06-09 12:22:30 +0000997 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000998 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +0000999}
1000
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001001void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001002 assert(FnStart == 0);
1003 FnStart = getContext().CreateTempSymbol();
1004 EmitLabel(FnStart);
1005}
1006
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001007void ARMELFStreamer::emitFnEnd() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001008 assert(FnStart && ".fnstart must preceeds .fnend");
1009
1010 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001011 if (!ExTab && !CantUnwind)
1012 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001013
1014 // Emit the exception index table entry
1015 SwitchToExIdxSection(*FnStart);
1016
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001017 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001018 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001019
1020 const MCSymbolRefExpr *FnStartRef =
1021 MCSymbolRefExpr::Create(FnStart,
1022 MCSymbolRefExpr::VK_ARM_PREL31,
1023 getContext());
1024
Rafael Espindola64e1af82013-07-02 15:49:13 +00001025 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001026
1027 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001028 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001029 } else if (ExTab) {
1030 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001031 const MCSymbolRefExpr *ExTabEntryRef =
1032 MCSymbolRefExpr::Create(ExTab,
1033 MCSymbolRefExpr::VK_ARM_PREL31,
1034 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001035 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001036 } else {
1037 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1038 // the second word of exception index table entry. The size of the unwind
1039 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001040 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001041 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001042 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001043 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +00001044 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001045 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001046 }
1047
Logan Chien4ea23b52013-05-10 16:17:24 +00001048 // Switch to the section containing FnStart
1049 SwitchSection(&FnStart->getSection());
1050
Logan Chien2bcc42c2013-01-30 15:39:04 +00001051 // Clean exception handling frame information
1052 Reset();
1053}
1054
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001055void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1056
1057// Add the R_ARM_NONE fixup at the same position
1058void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1059 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1060
1061 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1062 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1063
1064 AddValueSymbols(PersonalityRef);
1065 MCDataFragment *DF = getOrCreateDataFragment();
1066 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1067 PersonalityRef,
1068 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001069}
1070
Logan Chien325823a2013-06-09 12:22:30 +00001071void ARMELFStreamer::FlushPendingOffset() {
1072 if (PendingOffset != 0) {
1073 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1074 PendingOffset = 0;
1075 }
1076}
1077
Logan Chienc931fce2013-07-02 12:43:27 +00001078void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001079 // Emit the unwind opcode to restore $sp.
1080 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001081 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001082 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1083 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001084 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001085 } else {
1086 FlushPendingOffset();
1087 }
1088
1089 // Finalize the unwind opcode sequence
1090 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001091
1092 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1093 // section. Thus, we don't have to create an entry in the .ARM.extab
1094 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001095 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001096 return;
1097
1098 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001099 SwitchToExTabSection(*FnStart);
1100
1101 // Create .ARM.extab label for offset in .ARM.exidx
1102 assert(!ExTab);
1103 ExTab = getContext().CreateTempSymbol();
1104 EmitLabel(ExTab);
1105
Logan Chien4ea23b52013-05-10 16:17:24 +00001106 // Emit personality
1107 if (Personality) {
1108 const MCSymbolRefExpr *PersonalityRef =
1109 MCSymbolRefExpr::Create(Personality,
1110 MCSymbolRefExpr::VK_ARM_PREL31,
1111 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001112
Rafael Espindola64e1af82013-07-02 15:49:13 +00001113 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001114 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001115
1116 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +00001117 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001118 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +00001119
1120 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1121 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1122 // after the unwind opcodes. The handler data consists of several 32-bit
1123 // words, and should be terminated by zero.
1124 //
1125 // In case that the .handlerdata directive is not specified by the
1126 // programmer, we should emit zero to terminate the handler data.
1127 if (NoHandlerData && !Personality)
1128 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001129}
1130
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001131void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001132
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001133void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001134 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001135 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001136}
1137
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001138void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001139 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001140 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001141 "the operand of .setfp directive should be either $sp or $fp");
1142
1143 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001144 FPReg = NewFPReg;
1145
1146 if (NewSPReg == ARM::SP)
1147 FPOffset = SPOffset + Offset;
1148 else
1149 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001150}
1151
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001152void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001153 // Track the change of the $sp offset
1154 SPOffset -= Offset;
1155
1156 // To squash multiple .pad directives, we should delay the unwind opcode
1157 // until the .save, .vsave, .handlerdata, or .fnend directives.
1158 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001159}
1160
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001161void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001162 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001163 // Collect the registers in the register list
1164 unsigned Count = 0;
1165 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001166 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001167 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001168 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001169 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001170 unsigned Bit = (1u << Reg);
1171 if ((Mask & Bit) == 0) {
1172 Mask |= Bit;
1173 ++Count;
1174 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001175 }
Logan Chien325823a2013-06-09 12:22:30 +00001176
1177 // Track the change the $sp offset: For the .save directive, the
1178 // corresponding push instruction will decrease the $sp by (4 * Count).
1179 // For the .vsave directive, the corresponding vpush instruction will
1180 // decrease $sp by (8 * Count).
1181 SPOffset -= Count * (IsVector ? 8 : 4);
1182
1183 // Emit the opcode
1184 FlushPendingOffset();
1185 if (IsVector)
1186 UnwindOpAsm.EmitVFPRegSave(Mask);
1187 else
1188 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001189}
1190
Tim Northover5cc3dc82012-12-07 16:50:23 +00001191namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001192
1193MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1194 bool isVerboseAsm, bool useLoc, bool useCFI,
1195 bool useDwarfDirectory,
1196 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1197 MCAsmBackend *TAB, bool ShowInst) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +00001198 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1199 isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001200
1201 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1202 useDwarfDirectory, InstPrint, CE, TAB,
1203 ShowInst);
1204}
1205
Tim Northover5cc3dc82012-12-07 16:50:23 +00001206 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1207 raw_ostream &OS, MCCodeEmitter *Emitter,
1208 bool RelaxAll, bool NoExecStack,
1209 bool IsThumb) {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001210 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1211 ARMELFStreamer *S =
1212 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001213 // FIXME: This should eventually end up somewhere else where more
1214 // intelligent flag decisions can be made. For now we are just maintaining
1215 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1216 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1217
Tim Northover5cc3dc82012-12-07 16:50:23 +00001218 if (RelaxAll)
1219 S->getAssembler().setRelaxAll(true);
1220 if (NoExecStack)
1221 S->getAssembler().setNoExecStack(true);
1222 return S;
1223 }
1224
1225}
1226
1227