blob: 79d5619b764b12226895ebb2e74e567a3ad47552 [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);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000120 virtual void emitPersonalityIndex(unsigned Index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000121 virtual void emitHandlerData();
122 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
123 virtual void emitPad(int64_t Offset);
124 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
125 bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000126 virtual void emitUnwindRaw(int64_t Offset,
127 const SmallVectorImpl<uint8_t> &Opcodes);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000128
Logan Chien8cbb80d2013-10-28 17:51:12 +0000129 virtual void switchVendor(StringRef Vendor);
130 virtual void emitAttribute(unsigned Attribute, unsigned Value);
131 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000132 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
133 StringRef StrinValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000134 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000135 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000136 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000137 virtual void finishAttributeSection();
138
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000139public:
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000140 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter,
141 bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000142};
143
144ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000145 MCInstPrinter &InstPrinter,
146 bool VerboseAsm)
147 : OS(OS), InstPrinter(InstPrinter), IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000148void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
149void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
150void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
151void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
152 OS << "\t.personality " << Personality->getName() << '\n';
153}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000154void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
155 OS << "\t.personalityindex " << Index << '\n';
156}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000157void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
158void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
159 int64_t Offset) {
160 OS << "\t.setfp\t";
161 InstPrinter.printRegName(OS, FpReg);
162 OS << ", ";
163 InstPrinter.printRegName(OS, SpReg);
164 if (Offset)
165 OS << ", #" << Offset;
166 OS << '\n';
167}
168void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
169 OS << "\t.pad\t#" << Offset << '\n';
170}
171void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
172 bool isVector) {
173 assert(RegList.size() && "RegList should not be empty");
174 if (isVector)
175 OS << "\t.vsave\t{";
176 else
177 OS << "\t.save\t{";
178
179 InstPrinter.printRegName(OS, RegList[0]);
180
181 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
182 OS << ", ";
183 InstPrinter.printRegName(OS, RegList[i]);
184 }
185
186 OS << "}\n";
187}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000188void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
189}
190void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000191 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
192 if (IsVerboseAsm) {
193 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
194 if (!Name.empty())
195 OS << "\t@ " << Name;
196 }
197 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000198}
199void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
200 StringRef String) {
201 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000202 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000203 OS << "\t.cpu\t" << String.lower();
204 break;
205 default:
206 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000207 if (IsVerboseAsm) {
208 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
209 if (!Name.empty())
210 OS << "\t@ " << Name;
211 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000212 break;
213 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000214 OS << "\n";
215}
216void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
217 unsigned IntValue,
218 StringRef StringValue) {
219 switch (Attribute) {
220 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
221 case ARMBuildAttrs::compatibility:
222 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
223 if (!StringValue.empty())
224 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000225 if (IsVerboseAsm)
226 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000227 break;
228 }
229 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000230}
Logan Chien439e8f92013-12-11 17:16:25 +0000231void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
232 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
233}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000234void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
235 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
236}
237void ARMTargetAsmStreamer::finishAttributeSection() {
238}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000239
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000240void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
241 OS << "\t.inst";
242 if (Suffix)
243 OS << "." << Suffix;
244 OS << "\t0x" << utohexstr(Inst) << "\n";
245}
246
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000247void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
248 const SmallVectorImpl<uint8_t> &Opcodes) {
249 OS << "\t.unwind_raw " << Offset;
250 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
251 OCE = Opcodes.end();
252 OCI != OCE; ++OCI)
253 OS << ", 0x" << utohexstr(*OCI);
254 OS << '\n';
255}
256
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000257class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000258private:
259 // This structure holds all attributes, accounting for
260 // their string/numeric value, so we can later emmit them
261 // in declaration order, keeping all in the same vector
262 struct AttributeItem {
263 enum {
264 HiddenAttribute = 0,
265 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000266 TextAttribute,
267 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000268 } Type;
269 unsigned Tag;
270 unsigned IntValue;
271 StringRef StringValue;
272
273 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
274 return (LHS.Tag < RHS.Tag);
275 }
276 };
277
278 StringRef CurrentVendor;
279 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000280 unsigned Arch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000281 SmallVector<AttributeItem, 64> Contents;
282
283 const MCSection *AttributeSection;
284
285 // FIXME: this should be in a more generic place, but
286 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
287 static size_t getULEBSize(int Value) {
288 size_t Size = 0;
289 do {
290 Value >>= 7;
291 Size += sizeof(int8_t); // Is this really necessary?
292 } while (Value);
293 return Size;
294 }
295
296 AttributeItem *getAttributeItem(unsigned Attribute) {
297 for (size_t i = 0; i < Contents.size(); ++i)
298 if (Contents[i].Tag == Attribute)
299 return &Contents[i];
300 return 0;
301 }
302
303 void setAttributeItem(unsigned Attribute, unsigned Value,
304 bool OverwriteExisting) {
305 // Look for existing attribute item
306 if (AttributeItem *Item = getAttributeItem(Attribute)) {
307 if (!OverwriteExisting)
308 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000309 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000310 Item->IntValue = Value;
311 return;
312 }
313
314 // Create new attribute item
315 AttributeItem Item = {
316 AttributeItem::NumericAttribute,
317 Attribute,
318 Value,
319 StringRef("")
320 };
321 Contents.push_back(Item);
322 }
323
324 void setAttributeItem(unsigned Attribute, StringRef Value,
325 bool OverwriteExisting) {
326 // Look for existing attribute item
327 if (AttributeItem *Item = getAttributeItem(Attribute)) {
328 if (!OverwriteExisting)
329 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000330 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000331 Item->StringValue = Value;
332 return;
333 }
334
335 // Create new attribute item
336 AttributeItem Item = {
337 AttributeItem::TextAttribute,
338 Attribute,
339 0,
340 Value
341 };
342 Contents.push_back(Item);
343 }
344
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000345 void setAttributeItems(unsigned Attribute, unsigned IntValue,
346 StringRef StringValue, bool OverwriteExisting) {
347 // Look for existing attribute item
348 if (AttributeItem *Item = getAttributeItem(Attribute)) {
349 if (!OverwriteExisting)
350 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000351 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000352 Item->IntValue = IntValue;
353 Item->StringValue = StringValue;
354 return;
355 }
356
357 // Create new attribute item
358 AttributeItem Item = {
359 AttributeItem::NumericAndTextAttributes,
360 Attribute,
361 IntValue,
362 StringValue
363 };
364 Contents.push_back(Item);
365 }
366
Logan Chien439e8f92013-12-11 17:16:25 +0000367 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000368 void emitFPUDefaultAttributes();
369
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000370 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000371
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000372 virtual void emitFnStart();
373 virtual void emitFnEnd();
374 virtual void emitCantUnwind();
375 virtual void emitPersonality(const MCSymbol *Personality);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000376 virtual void emitPersonalityIndex(unsigned Index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000377 virtual void emitHandlerData();
378 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
379 virtual void emitPad(int64_t Offset);
380 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
381 bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000382 virtual void emitUnwindRaw(int64_t Offset,
383 const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000384
385 virtual void switchVendor(StringRef Vendor);
386 virtual void emitAttribute(unsigned Attribute, unsigned Value);
387 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000388 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
389 StringRef StringValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000390 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000391 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000392 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000393 virtual void finishAttributeSection();
394
395 size_t calculateContentSize() const;
396
397public:
398 ARMTargetELFStreamer()
399 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
Logan Chien439e8f92013-12-11 17:16:25 +0000400 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000401 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000402};
403
Tim Northover5cc3dc82012-12-07 16:50:23 +0000404/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
405/// the appropriate points in the object files. These symbols are defined in the
406/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
407///
408/// In brief: $a, $t or $d should be emitted at the start of each contiguous
409/// region of ARM code, Thumb code or data in a section. In practice, this
410/// emission does not rely on explicit assembler directives but on inherent
411/// properties of the directives doing the emission (e.g. ".byte" is data, "add
412/// r0, r0, r0" an instruction).
413///
414/// As a result this system is orthogonal to the DataRegion infrastructure used
415/// by MachO. Beware!
416class ARMELFStreamer : public MCELFStreamer {
417public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000418 friend class ARMTargetELFStreamer;
419
420 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
421 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
422 bool IsThumb)
423 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
424 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000425 Reset();
426 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000427
428 ~ARMELFStreamer() {}
429
Logan Chien8cbb80d2013-10-28 17:51:12 +0000430 virtual void FinishImpl();
431
Logan Chien2bcc42c2013-01-30 15:39:04 +0000432 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000433 void emitFnStart();
434 void emitFnEnd();
435 void emitCantUnwind();
436 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000437 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000438 void emitHandlerData();
439 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
440 void emitPad(int64_t Offset);
441 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000442 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000443
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000444 virtual void ChangeSection(const MCSection *Section,
445 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000446 // We have to keep track of the mapping symbol state of any sections we
447 // use. Each one should start off as EMS_None, which is provided as the
448 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000449 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000450 LastEMS = LastMappingSymbols.lookup(Section);
451
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000452 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000453 }
454
455 /// This function is the one used to emit instruction data into the ELF
456 /// streamer. We override it to add the appropriate mapping symbol if
457 /// necessary.
458 virtual void EmitInstruction(const MCInst& Inst) {
459 if (IsThumb)
460 EmitThumbMappingSymbol();
461 else
462 EmitARMMappingSymbol();
463
464 MCELFStreamer::EmitInstruction(Inst);
465 }
466
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000467 virtual void emitInst(uint32_t Inst, char Suffix) {
468 unsigned Size;
469 char Buffer[4];
470 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
471
472 switch (Suffix) {
473 case '\0':
474 Size = 4;
475
476 assert(!IsThumb);
477 EmitARMMappingSymbol();
478 for (unsigned II = 0, IE = Size; II != IE; II++) {
479 const unsigned I = LittleEndian ? (Size - II - 1) : II;
480 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
481 }
482
483 break;
484 case 'n':
485 case 'w':
486 Size = (Suffix == 'n' ? 2 : 4);
487
488 assert(IsThumb);
489 EmitThumbMappingSymbol();
490 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
491 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
492 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
493 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
494 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
495 }
496
497 break;
498 default:
499 llvm_unreachable("Invalid Suffix");
500 }
501
502 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
503 }
504
Tim Northover5cc3dc82012-12-07 16:50:23 +0000505 /// This is one of the functions used to emit data into an ELF section, so the
506 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
507 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000508 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000509 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000510 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000511 }
512
513 /// This is one of the functions used to emit data into an ELF section, so the
514 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
515 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000516 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000517 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000518 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000519 }
520
521 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
522 MCELFStreamer::EmitAssemblerFlag(Flag);
523
524 switch (Flag) {
525 case MCAF_SyntaxUnified:
526 return; // no-op here.
527 case MCAF_Code16:
528 IsThumb = true;
529 return; // Change to Thumb mode
530 case MCAF_Code32:
531 IsThumb = false;
532 return; // Change to ARM mode
533 case MCAF_Code64:
534 return;
535 case MCAF_SubsectionsViaSymbols:
536 return;
537 }
538 }
539
540private:
541 enum ElfMappingSymbol {
542 EMS_None,
543 EMS_ARM,
544 EMS_Thumb,
545 EMS_Data
546 };
547
548 void EmitDataMappingSymbol() {
549 if (LastEMS == EMS_Data) return;
550 EmitMappingSymbol("$d");
551 LastEMS = EMS_Data;
552 }
553
554 void EmitThumbMappingSymbol() {
555 if (LastEMS == EMS_Thumb) return;
556 EmitMappingSymbol("$t");
557 LastEMS = EMS_Thumb;
558 }
559
560 void EmitARMMappingSymbol() {
561 if (LastEMS == EMS_ARM) return;
562 EmitMappingSymbol("$a");
563 LastEMS = EMS_ARM;
564 }
565
566 void EmitMappingSymbol(StringRef Name) {
567 MCSymbol *Start = getContext().CreateTempSymbol();
568 EmitLabel(Start);
569
Chandler Carruth1d94e932012-12-08 03:10:14 +0000570 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000571 getContext().GetOrCreateSymbol(Name + "." +
572 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000573
574 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
575 MCELF::SetType(SD, ELF::STT_NOTYPE);
576 MCELF::SetBinding(SD, ELF::STB_LOCAL);
577 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000578 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000579
580 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
581 Symbol->setVariableValue(Value);
582 }
583
584 void EmitThumbFunc(MCSymbol *Func) {
585 // FIXME: Anything needed here to flag the function as thumb?
586
587 getAssembler().setIsThumbFunc(Func);
588
589 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
590 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
591 }
592
Logan Chien2bcc42c2013-01-30 15:39:04 +0000593 // Helper functions for ARM exception handling directives
594 void Reset();
595
596 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000597 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000598 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000599
600 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
601 SectionKind Kind, const MCSymbol &Fn);
602 void SwitchToExTabSection(const MCSymbol &FnStart);
603 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000604
605 bool IsThumb;
606 int64_t MappingSymbolCounter;
607
608 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
609 ElfMappingSymbol LastEMS;
610
Logan Chien2bcc42c2013-01-30 15:39:04 +0000611 // ARM Exception Handling Frame Information
612 MCSymbol *ExTab;
613 MCSymbol *FnStart;
614 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000615 unsigned PersonalityIndex;
616 unsigned FPReg; // Frame pointer register
617 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
618 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
619 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000620 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000621 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000622 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000623 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000624};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000625} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000626
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000627ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Benjamin Kramer41882932013-10-09 17:23:41 +0000628 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000629 return *S;
630}
631
632void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
633void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
634void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
635void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
636 getStreamer().emitPersonality(Personality);
637}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000638void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
639 getStreamer().emitPersonalityIndex(Index);
640}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000641void ARMTargetELFStreamer::emitHandlerData() {
642 getStreamer().emitHandlerData();
643}
644void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
645 int64_t Offset) {
646 getStreamer().emitSetFP(FpReg, SpReg, Offset);
647}
648void ARMTargetELFStreamer::emitPad(int64_t Offset) {
649 getStreamer().emitPad(Offset);
650}
651void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
652 bool isVector) {
653 getStreamer().emitRegSave(RegList, isVector);
654}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000655void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
656 const SmallVectorImpl<uint8_t> &Opcodes) {
657 getStreamer().emitUnwindRaw(Offset, Opcodes);
658}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000659void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
660 assert(!Vendor.empty() && "Vendor cannot be empty.");
661
662 if (CurrentVendor == Vendor)
663 return;
664
665 if (!CurrentVendor.empty())
666 finishAttributeSection();
667
668 assert(Contents.empty() &&
669 ".ARM.attributes should be flushed before changing vendor");
670 CurrentVendor = Vendor;
671
672}
673void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
674 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
675}
676void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
677 StringRef Value) {
678 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
679}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000680void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
681 unsigned IntValue,
682 StringRef StringValue) {
683 setAttributeItems(Attribute, IntValue, StringValue,
684 /* OverwriteExisting= */ true);
685}
Logan Chien439e8f92013-12-11 17:16:25 +0000686void ARMTargetELFStreamer::emitArch(unsigned Value) {
687 Arch = Value;
688}
689void ARMTargetELFStreamer::emitArchDefaultAttributes() {
690 using namespace ARMBuildAttrs;
691 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
692 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
693
694 switch (Arch) {
695 case ARM::ARMV2:
696 case ARM::ARMV2A:
697 case ARM::ARMV3:
698 case ARM::ARMV3M:
699 case ARM::ARMV4:
700 case ARM::ARMV5:
701 setAttributeItem(ARM_ISA_use, Allowed, false);
702 break;
703
704 case ARM::ARMV4T:
705 case ARM::ARMV5T:
706 case ARM::ARMV5TE:
707 case ARM::ARMV6:
708 case ARM::ARMV6J:
709 setAttributeItem(ARM_ISA_use, Allowed, false);
710 setAttributeItem(THUMB_ISA_use, Allowed, false);
711 break;
712
713 case ARM::ARMV6T2:
714 setAttributeItem(ARM_ISA_use, Allowed, false);
715 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
716 break;
717
718 case ARM::ARMV6Z:
719 case ARM::ARMV6ZK:
720 setAttributeItem(ARM_ISA_use, Allowed, false);
721 setAttributeItem(THUMB_ISA_use, Allowed, false);
722 setAttributeItem(Virtualization_use, AllowTZ, false);
723 break;
724
725 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000726 setAttributeItem(THUMB_ISA_use, Allowed, false);
727 break;
728
729 case ARM::ARMV7:
730 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
731 break;
732
733 case ARM::ARMV7A:
734 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
735 setAttributeItem(ARM_ISA_use, Allowed, false);
736 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
737 break;
738
739 case ARM::ARMV7R:
740 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
741 setAttributeItem(ARM_ISA_use, Allowed, false);
742 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
743 break;
744
745 case ARM::ARMV7M:
746 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
747 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
748 break;
749
750 case ARM::ARMV8A:
751 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
752 setAttributeItem(ARM_ISA_use, Allowed, false);
753 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
754 setAttributeItem(MPextension_use, Allowed, false);
755 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
756 break;
757
758 case ARM::IWMMXT:
759 setAttributeItem(ARM_ISA_use, Allowed, false);
760 setAttributeItem(THUMB_ISA_use, Allowed, false);
761 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
762 break;
763
764 case ARM::IWMMXT2:
765 setAttributeItem(ARM_ISA_use, Allowed, false);
766 setAttributeItem(THUMB_ISA_use, Allowed, false);
767 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
768 break;
769
770 default:
771 report_fatal_error("Unknown Arch: " + Twine(Arch));
772 break;
773 }
774}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000775void ARMTargetELFStreamer::emitFPU(unsigned Value) {
776 FPU = Value;
777}
778void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
779 switch (FPU) {
780 case ARM::VFP:
781 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000782 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000783 ARMBuildAttrs::AllowFPv2,
784 /* OverwriteExisting= */ false);
785 break;
786
787 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000788 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000789 ARMBuildAttrs::AllowFPv3A,
790 /* OverwriteExisting= */ false);
791 break;
792
793 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000794 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000795 ARMBuildAttrs::AllowFPv3B,
796 /* OverwriteExisting= */ false);
797 break;
798
799 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000800 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000801 ARMBuildAttrs::AllowFPv4A,
802 /* OverwriteExisting= */ false);
803 break;
804
805 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000806 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000807 ARMBuildAttrs::AllowFPv4B,
808 /* OverwriteExisting= */ false);
809 break;
810
811 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000812 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000813 ARMBuildAttrs::AllowFPARMv8A,
814 /* OverwriteExisting= */ false);
815 break;
816
817 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000818 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000819 ARMBuildAttrs::AllowFPv3A,
820 /* OverwriteExisting= */ false);
821 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
822 ARMBuildAttrs::AllowNeon,
823 /* OverwriteExisting= */ false);
824 break;
825
826 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000827 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000828 ARMBuildAttrs::AllowFPv4A,
829 /* OverwriteExisting= */ false);
830 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
831 ARMBuildAttrs::AllowNeon2,
832 /* OverwriteExisting= */ false);
833 break;
834
835 case ARM::NEON_FP_ARMV8:
836 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000837 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000838 ARMBuildAttrs::AllowFPARMv8A,
839 /* OverwriteExisting= */ false);
840 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
841 ARMBuildAttrs::AllowNeonARMv8,
842 /* OverwriteExisting= */ false);
843 break;
844
Logan Chien05ae7442014-01-02 15:50:02 +0000845 case ARM::SOFTVFP:
846 break;
847
Logan Chien8cbb80d2013-10-28 17:51:12 +0000848 default:
849 report_fatal_error("Unknown FPU: " + Twine(FPU));
850 break;
851 }
852}
853size_t ARMTargetELFStreamer::calculateContentSize() const {
854 size_t Result = 0;
855 for (size_t i = 0; i < Contents.size(); ++i) {
856 AttributeItem item = Contents[i];
857 switch (item.Type) {
858 case AttributeItem::HiddenAttribute:
859 break;
860 case AttributeItem::NumericAttribute:
861 Result += getULEBSize(item.Tag);
862 Result += getULEBSize(item.IntValue);
863 break;
864 case AttributeItem::TextAttribute:
865 Result += getULEBSize(item.Tag);
866 Result += item.StringValue.size() + 1; // string + '\0'
867 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000868 case AttributeItem::NumericAndTextAttributes:
869 Result += getULEBSize(item.Tag);
870 Result += getULEBSize(item.IntValue);
871 Result += item.StringValue.size() + 1; // string + '\0';
872 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000873 }
874 }
875 return Result;
876}
877void ARMTargetELFStreamer::finishAttributeSection() {
878 // <format-version>
879 // [ <section-length> "vendor-name"
880 // [ <file-tag> <size> <attribute>*
881 // | <section-tag> <size> <section-number>* 0 <attribute>*
882 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
883 // ]+
884 // ]*
885
886 if (FPU != ARM::INVALID_FPU)
887 emitFPUDefaultAttributes();
888
Logan Chien439e8f92013-12-11 17:16:25 +0000889 if (Arch != ARM::INVALID_ARCH)
890 emitArchDefaultAttributes();
891
Logan Chien8cbb80d2013-10-28 17:51:12 +0000892 if (Contents.empty())
893 return;
894
895 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
896
897 ARMELFStreamer &Streamer = getStreamer();
898
899 // Switch to .ARM.attributes section
900 if (AttributeSection) {
901 Streamer.SwitchSection(AttributeSection);
902 } else {
903 AttributeSection =
904 Streamer.getContext().getELFSection(".ARM.attributes",
905 ELF::SHT_ARM_ATTRIBUTES,
906 0,
907 SectionKind::getMetadata());
908 Streamer.SwitchSection(AttributeSection);
909
910 // Format version
911 Streamer.EmitIntValue(0x41, 1);
912 }
913
914 // Vendor size + Vendor name + '\0'
915 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
916
917 // Tag + Tag Size
918 const size_t TagHeaderSize = 1 + 4;
919
920 const size_t ContentsSize = calculateContentSize();
921
922 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
923 Streamer.EmitBytes(CurrentVendor);
924 Streamer.EmitIntValue(0, 1); // '\0'
925
926 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
927 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
928
929 // Size should have been accounted for already, now
930 // emit each field as its type (ULEB or String)
931 for (size_t i = 0; i < Contents.size(); ++i) {
932 AttributeItem item = Contents[i];
933 Streamer.EmitULEB128IntValue(item.Tag);
934 switch (item.Type) {
935 default: llvm_unreachable("Invalid attribute type");
936 case AttributeItem::NumericAttribute:
937 Streamer.EmitULEB128IntValue(item.IntValue);
938 break;
939 case AttributeItem::TextAttribute:
940 Streamer.EmitBytes(item.StringValue.upper());
941 Streamer.EmitIntValue(0, 1); // '\0'
942 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000943 case AttributeItem::NumericAndTextAttributes:
944 Streamer.EmitULEB128IntValue(item.IntValue);
945 Streamer.EmitBytes(item.StringValue.upper());
946 Streamer.EmitIntValue(0, 1); // '\0'
947 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000948 }
949 }
950
951 Contents.clear();
952 FPU = ARM::INVALID_FPU;
953}
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000954void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
955 getStreamer().emitInst(Inst, Suffix);
956}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000957
958void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +0000959 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000960 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
961 ATS.finishAttributeSection();
962
963 MCELFStreamer::FinishImpl();
964}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000965
Logan Chien2bcc42c2013-01-30 15:39:04 +0000966inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
967 unsigned Type,
968 unsigned Flags,
969 SectionKind Kind,
970 const MCSymbol &Fn) {
971 const MCSectionELF &FnSection =
972 static_cast<const MCSectionELF &>(Fn.getSection());
973
974 // Create the name for new section
975 StringRef FnSecName(FnSection.getSectionName());
976 SmallString<128> EHSecName(Prefix);
977 if (FnSecName != ".text") {
978 EHSecName += FnSecName;
979 }
980
981 // Get .ARM.extab or .ARM.exidx section
982 const MCSectionELF *EHSection = NULL;
983 if (const MCSymbol *Group = FnSection.getGroup()) {
984 EHSection = getContext().getELFSection(
985 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
986 FnSection.getEntrySize(), Group->getName());
987 } else {
988 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
989 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000990 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +0000991
992 // Switch to .ARM.extab or .ARM.exidx section
993 SwitchSection(EHSection);
994 EmitCodeAlignment(4, 0);
995}
996
997inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
998 SwitchToEHSection(".ARM.extab",
999 ELF::SHT_PROGBITS,
1000 ELF::SHF_ALLOC,
1001 SectionKind::getDataRel(),
1002 FnStart);
1003}
1004
1005inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1006 SwitchToEHSection(".ARM.exidx",
1007 ELF::SHT_ARM_EXIDX,
1008 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1009 SectionKind::getDataRel(),
1010 FnStart);
1011}
1012
1013void ARMELFStreamer::Reset() {
1014 ExTab = NULL;
1015 FnStart = NULL;
1016 Personality = NULL;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001017 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001018 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001019 FPOffset = 0;
1020 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001021 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001022 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001023 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001024
Logan Chien325823a2013-06-09 12:22:30 +00001025 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001026 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001027}
1028
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001029void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001030 assert(FnStart == 0);
1031 FnStart = getContext().CreateTempSymbol();
1032 EmitLabel(FnStart);
1033}
1034
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001035void ARMELFStreamer::emitFnEnd() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001036 assert(FnStart && ".fnstart must preceeds .fnend");
1037
1038 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001039 if (!ExTab && !CantUnwind)
1040 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001041
1042 // Emit the exception index table entry
1043 SwitchToExIdxSection(*FnStart);
1044
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001045 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001046 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001047
1048 const MCSymbolRefExpr *FnStartRef =
1049 MCSymbolRefExpr::Create(FnStart,
1050 MCSymbolRefExpr::VK_ARM_PREL31,
1051 getContext());
1052
Rafael Espindola64e1af82013-07-02 15:49:13 +00001053 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001054
1055 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001056 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001057 } else if (ExTab) {
1058 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001059 const MCSymbolRefExpr *ExTabEntryRef =
1060 MCSymbolRefExpr::Create(ExTab,
1061 MCSymbolRefExpr::VK_ARM_PREL31,
1062 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001063 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001064 } else {
1065 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1066 // the second word of exception index table entry. The size of the unwind
1067 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001068 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001069 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001070 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001071 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +00001072 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001073 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001074 }
1075
Logan Chien4ea23b52013-05-10 16:17:24 +00001076 // Switch to the section containing FnStart
1077 SwitchSection(&FnStart->getSection());
1078
Logan Chien2bcc42c2013-01-30 15:39:04 +00001079 // Clean exception handling frame information
1080 Reset();
1081}
1082
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001083void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1084
1085// Add the R_ARM_NONE fixup at the same position
1086void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1087 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1088
1089 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1090 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1091
1092 AddValueSymbols(PersonalityRef);
1093 MCDataFragment *DF = getOrCreateDataFragment();
1094 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1095 PersonalityRef,
1096 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001097}
1098
Logan Chien325823a2013-06-09 12:22:30 +00001099void ARMELFStreamer::FlushPendingOffset() {
1100 if (PendingOffset != 0) {
1101 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1102 PendingOffset = 0;
1103 }
1104}
1105
Logan Chienc931fce2013-07-02 12:43:27 +00001106void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001107 // Emit the unwind opcode to restore $sp.
1108 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001109 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001110 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1111 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001112 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001113 } else {
1114 FlushPendingOffset();
1115 }
1116
1117 // Finalize the unwind opcode sequence
1118 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001119
1120 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1121 // section. Thus, we don't have to create an entry in the .ARM.extab
1122 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001123 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001124 return;
1125
1126 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001127 SwitchToExTabSection(*FnStart);
1128
1129 // Create .ARM.extab label for offset in .ARM.exidx
1130 assert(!ExTab);
1131 ExTab = getContext().CreateTempSymbol();
1132 EmitLabel(ExTab);
1133
Logan Chien4ea23b52013-05-10 16:17:24 +00001134 // Emit personality
1135 if (Personality) {
1136 const MCSymbolRefExpr *PersonalityRef =
1137 MCSymbolRefExpr::Create(Personality,
1138 MCSymbolRefExpr::VK_ARM_PREL31,
1139 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001140
Rafael Espindola64e1af82013-07-02 15:49:13 +00001141 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001142 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001143
1144 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +00001145 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001146 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +00001147
1148 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1149 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1150 // after the unwind opcodes. The handler data consists of several 32-bit
1151 // words, and should be terminated by zero.
1152 //
1153 // In case that the .handlerdata directive is not specified by the
1154 // programmer, we should emit zero to terminate the handler data.
1155 if (NoHandlerData && !Personality)
1156 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001157}
1158
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001159void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001160
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001161void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001162 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001163 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001164}
1165
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001166void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1167 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1168 PersonalityIndex = Index;
1169}
1170
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001171void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001172 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001173 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001174 "the operand of .setfp directive should be either $sp or $fp");
1175
1176 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001177 FPReg = NewFPReg;
1178
1179 if (NewSPReg == ARM::SP)
1180 FPOffset = SPOffset + Offset;
1181 else
1182 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001183}
1184
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001185void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001186 // Track the change of the $sp offset
1187 SPOffset -= Offset;
1188
1189 // To squash multiple .pad directives, we should delay the unwind opcode
1190 // until the .save, .vsave, .handlerdata, or .fnend directives.
1191 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001192}
1193
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001194void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001195 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001196 // Collect the registers in the register list
1197 unsigned Count = 0;
1198 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001199 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001200 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001201 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001202 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001203 unsigned Bit = (1u << Reg);
1204 if ((Mask & Bit) == 0) {
1205 Mask |= Bit;
1206 ++Count;
1207 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001208 }
Logan Chien325823a2013-06-09 12:22:30 +00001209
1210 // Track the change the $sp offset: For the .save directive, the
1211 // corresponding push instruction will decrease the $sp by (4 * Count).
1212 // For the .vsave directive, the corresponding vpush instruction will
1213 // decrease $sp by (8 * Count).
1214 SPOffset -= Count * (IsVector ? 8 : 4);
1215
1216 // Emit the opcode
1217 FlushPendingOffset();
1218 if (IsVector)
1219 UnwindOpAsm.EmitVFPRegSave(Mask);
1220 else
1221 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001222}
1223
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001224void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1225 const SmallVectorImpl<uint8_t> &Opcodes) {
1226 FlushPendingOffset();
1227 SPOffset = SPOffset - Offset;
1228 UnwindOpAsm.EmitRaw(Opcodes);
1229}
1230
Tim Northover5cc3dc82012-12-07 16:50:23 +00001231namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001232
1233MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1234 bool isVerboseAsm, bool useLoc, bool useCFI,
1235 bool useDwarfDirectory,
1236 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1237 MCAsmBackend *TAB, bool ShowInst) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +00001238 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1239 isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001240
1241 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1242 useDwarfDirectory, InstPrint, CE, TAB,
1243 ShowInst);
1244}
1245
Tim Northover5cc3dc82012-12-07 16:50:23 +00001246 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1247 raw_ostream &OS, MCCodeEmitter *Emitter,
1248 bool RelaxAll, bool NoExecStack,
1249 bool IsThumb) {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001250 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1251 ARMELFStreamer *S =
1252 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001253 // FIXME: This should eventually end up somewhere else where more
1254 // intelligent flag decisions can be made. For now we are just maintaining
1255 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1256 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1257
Tim Northover5cc3dc82012-12-07 16:50:23 +00001258 if (RelaxAll)
1259 S->getAssembler().setRelaxAll(true);
1260 if (NoExecStack)
1261 S->getAssembler().setNoExecStack(true);
1262 return S;
1263 }
1264
1265}
1266
1267