blob: e9ffc5890ad74a910dab6f17b892f4cb19a3c98d [file] [log] [blame]
Tim Northover5cc3dc82012-12-07 16:50:23 +00001//===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file assembles .s files and emits ARM ELF .o object files. Different
11// from generic ELF streamer in emitting mapping symbols ($a, $t and $d) to
12// delimit regions of data and code.
13//
14//===----------------------------------------------------------------------===//
15
Logan Chien439e8f92013-12-11 17:16:25 +000016#include "ARMArchName.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000017#include "ARMFPUName.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000018#include "ARMRegisterInfo.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000019#include "ARMUnwindOpAsm.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000020#include "llvm/ADT/SmallPtrSet.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000021#include "llvm/ADT/StringExtras.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000022#include "llvm/ADT/Twine.h"
23#include "llvm/MC/MCAsmBackend.h"
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +000024#include "llvm/MC/MCAsmInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000025#include "llvm/MC/MCAssembler.h"
26#include "llvm/MC/MCCodeEmitter.h"
27#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000028#include "llvm/MC/MCELF.h"
29#include "llvm/MC/MCELFStreamer.h"
30#include "llvm/MC/MCELFSymbolFlags.h"
31#include "llvm/MC/MCExpr.h"
32#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000033#include "llvm/MC/MCInstPrinter.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000034#include "llvm/MC/MCObjectStreamer.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000035#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000036#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000037#include "llvm/MC/MCSectionELF.h"
38#include "llvm/MC/MCStreamer.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000039#include "llvm/MC/MCSymbol.h"
40#include "llvm/MC/MCValue.h"
Saleem Abdulrasool278a9f42014-01-19 08:25:27 +000041#include "llvm/Support/ARMBuildAttributes.h"
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000042#include "llvm/Support/ARMEHABI.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000043#include "llvm/Support/Debug.h"
44#include "llvm/Support/ELF.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000045#include "llvm/Support/FormattedStream.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000046#include "llvm/Support/raw_ostream.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000047#include <algorithm>
Tim Northover5cc3dc82012-12-07 16:50:23 +000048
49using namespace llvm;
50
Logan Chiend8bb4b72013-04-16 12:02:21 +000051static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +000052 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX &&
53 "Invalid personality index");
Logan Chiend8bb4b72013-04-16 12:02:21 +000054 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
55}
56
Logan Chien8cbb80d2013-10-28 17:51:12 +000057static const char *GetFPUName(unsigned ID) {
58 switch (ID) {
59 default:
60 llvm_unreachable("Unknown FPU kind");
61 break;
62#define ARM_FPU_NAME(NAME, ID) case ARM::ID: return NAME;
63#include "ARMFPUName.def"
64 }
65 return NULL;
66}
67
Logan Chien439e8f92013-12-11 17:16:25 +000068static const char *GetArchName(unsigned ID) {
69 switch (ID) {
70 default:
71 llvm_unreachable("Unknown ARCH kind");
72 break;
73#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
74 case ARM::ID: return NAME;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +000075#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +000076#include "ARMArchName.def"
77 }
78 return NULL;
79}
80
81static const char *GetArchDefaultCPUName(unsigned ID) {
82 switch (ID) {
83 default:
84 llvm_unreachable("Unknown ARCH kind");
85 break;
86#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
87 case ARM::ID: return DEFAULT_CPU_NAME;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +000088#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +000089#include "ARMArchName.def"
90 }
91 return NULL;
92}
93
94static unsigned GetArchDefaultCPUArch(unsigned ID) {
95 switch (ID) {
96 default:
97 llvm_unreachable("Unknown ARCH kind");
98 break;
99#define ARM_ARCH_NAME(NAME, ID, DEFAULT_CPU_NAME, DEFAULT_CPU_ARCH) \
100 case ARM::ID: return ARMBuildAttrs::DEFAULT_CPU_ARCH;
Joerg Sonnenbergera13f8b42013-12-26 11:50:28 +0000101#define ARM_ARCH_ALIAS(NAME, ID) /* empty */
Logan Chien439e8f92013-12-11 17:16:25 +0000102#include "ARMArchName.def"
103 }
104 return 0;
105}
106
Greg Fitzgerald1f6a6082014-01-22 18:32:35 +0000107static bool isThumb(const MCSubtargetInfo& STI) {
108 return (STI.getFeatureBits() & ARM::ModeThumb) != 0;
109}
110
111void ARMTargetStreamer::anchor() {}
112
113void ARMTargetStreamer::emitInlineAsmEnd(const MCSubtargetInfo &StartInfo,
114 MCSubtargetInfo *EndInfo) {
115 // If either end mode is unknown (EndInfo == NULL) or different than
116 // the start mode, then restore the start mode.
117 const bool WasThumb = isThumb(StartInfo);
118 if (EndInfo == NULL || WasThumb != isThumb(*EndInfo)) {
119 assert(Streamer);
120 Streamer->EmitAssemblerFlag(WasThumb ? MCAF_Code16 : MCAF_Code32);
121 if (EndInfo)
122 EndInfo->ToggleFeature(ARM::ModeThumb);
123 }
124}
125
Tim Northover5cc3dc82012-12-07 16:50:23 +0000126namespace {
127
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000128class ARMELFStreamer;
129
130class ARMTargetAsmStreamer : public ARMTargetStreamer {
131 formatted_raw_ostream &OS;
132 MCInstPrinter &InstPrinter;
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000133 bool IsVerboseAsm;
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000134
135 virtual void emitFnStart();
136 virtual void emitFnEnd();
137 virtual void emitCantUnwind();
138 virtual void emitPersonality(const MCSymbol *Personality);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000139 virtual void emitPersonalityIndex(unsigned Index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000140 virtual void emitHandlerData();
141 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
142 virtual void emitPad(int64_t Offset);
143 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
144 bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000145 virtual void emitUnwindRaw(int64_t Offset,
146 const SmallVectorImpl<uint8_t> &Opcodes);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000147
Logan Chien8cbb80d2013-10-28 17:51:12 +0000148 virtual void switchVendor(StringRef Vendor);
149 virtual void emitAttribute(unsigned Attribute, unsigned Value);
150 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000151 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
152 StringRef StrinValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000153 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000154 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000155 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000156 virtual void finishAttributeSection();
157
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000158public:
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000159 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter,
160 bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000161};
162
163ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000164 MCInstPrinter &InstPrinter,
165 bool VerboseAsm)
166 : OS(OS), InstPrinter(InstPrinter), IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000167void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
168void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
169void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
170void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
171 OS << "\t.personality " << Personality->getName() << '\n';
172}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000173void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
174 OS << "\t.personalityindex " << Index << '\n';
175}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000176void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
177void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
178 int64_t Offset) {
179 OS << "\t.setfp\t";
180 InstPrinter.printRegName(OS, FpReg);
181 OS << ", ";
182 InstPrinter.printRegName(OS, SpReg);
183 if (Offset)
184 OS << ", #" << Offset;
185 OS << '\n';
186}
187void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
188 OS << "\t.pad\t#" << Offset << '\n';
189}
190void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
191 bool isVector) {
192 assert(RegList.size() && "RegList should not be empty");
193 if (isVector)
194 OS << "\t.vsave\t{";
195 else
196 OS << "\t.save\t{";
197
198 InstPrinter.printRegName(OS, RegList[0]);
199
200 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
201 OS << ", ";
202 InstPrinter.printRegName(OS, RegList[i]);
203 }
204
205 OS << "}\n";
206}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000207void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
208}
209void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000210 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
211 if (IsVerboseAsm) {
212 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
213 if (!Name.empty())
214 OS << "\t@ " << Name;
215 }
216 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000217}
218void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
219 StringRef String) {
220 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000221 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000222 OS << "\t.cpu\t" << String.lower();
223 break;
224 default:
225 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000226 if (IsVerboseAsm) {
227 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
228 if (!Name.empty())
229 OS << "\t@ " << Name;
230 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000231 break;
232 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000233 OS << "\n";
234}
235void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
236 unsigned IntValue,
237 StringRef StringValue) {
238 switch (Attribute) {
239 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
240 case ARMBuildAttrs::compatibility:
241 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
242 if (!StringValue.empty())
243 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000244 if (IsVerboseAsm)
245 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000246 break;
247 }
248 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000249}
Logan Chien439e8f92013-12-11 17:16:25 +0000250void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
251 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
252}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000253void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
254 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
255}
256void ARMTargetAsmStreamer::finishAttributeSection() {
257}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000258
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000259void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
260 OS << "\t.inst";
261 if (Suffix)
262 OS << "." << Suffix;
263 OS << "\t0x" << utohexstr(Inst) << "\n";
264}
265
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000266void ARMTargetAsmStreamer::emitUnwindRaw(int64_t Offset,
267 const SmallVectorImpl<uint8_t> &Opcodes) {
268 OS << "\t.unwind_raw " << Offset;
269 for (SmallVectorImpl<uint8_t>::const_iterator OCI = Opcodes.begin(),
270 OCE = Opcodes.end();
271 OCI != OCE; ++OCI)
272 OS << ", 0x" << utohexstr(*OCI);
273 OS << '\n';
274}
275
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000276class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000277private:
278 // This structure holds all attributes, accounting for
279 // their string/numeric value, so we can later emmit them
280 // in declaration order, keeping all in the same vector
281 struct AttributeItem {
282 enum {
283 HiddenAttribute = 0,
284 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000285 TextAttribute,
286 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000287 } Type;
288 unsigned Tag;
289 unsigned IntValue;
290 StringRef StringValue;
291
292 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
293 return (LHS.Tag < RHS.Tag);
294 }
295 };
296
297 StringRef CurrentVendor;
298 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000299 unsigned Arch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000300 SmallVector<AttributeItem, 64> Contents;
301
302 const MCSection *AttributeSection;
303
304 // FIXME: this should be in a more generic place, but
305 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
306 static size_t getULEBSize(int Value) {
307 size_t Size = 0;
308 do {
309 Value >>= 7;
310 Size += sizeof(int8_t); // Is this really necessary?
311 } while (Value);
312 return Size;
313 }
314
315 AttributeItem *getAttributeItem(unsigned Attribute) {
316 for (size_t i = 0; i < Contents.size(); ++i)
317 if (Contents[i].Tag == Attribute)
318 return &Contents[i];
319 return 0;
320 }
321
322 void setAttributeItem(unsigned Attribute, unsigned Value,
323 bool OverwriteExisting) {
324 // Look for existing attribute item
325 if (AttributeItem *Item = getAttributeItem(Attribute)) {
326 if (!OverwriteExisting)
327 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000328 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000329 Item->IntValue = Value;
330 return;
331 }
332
333 // Create new attribute item
334 AttributeItem Item = {
335 AttributeItem::NumericAttribute,
336 Attribute,
337 Value,
338 StringRef("")
339 };
340 Contents.push_back(Item);
341 }
342
343 void setAttributeItem(unsigned Attribute, StringRef Value,
344 bool OverwriteExisting) {
345 // Look for existing attribute item
346 if (AttributeItem *Item = getAttributeItem(Attribute)) {
347 if (!OverwriteExisting)
348 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000349 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000350 Item->StringValue = Value;
351 return;
352 }
353
354 // Create new attribute item
355 AttributeItem Item = {
356 AttributeItem::TextAttribute,
357 Attribute,
358 0,
359 Value
360 };
361 Contents.push_back(Item);
362 }
363
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000364 void setAttributeItems(unsigned Attribute, unsigned IntValue,
365 StringRef StringValue, bool OverwriteExisting) {
366 // Look for existing attribute item
367 if (AttributeItem *Item = getAttributeItem(Attribute)) {
368 if (!OverwriteExisting)
369 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000370 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000371 Item->IntValue = IntValue;
372 Item->StringValue = StringValue;
373 return;
374 }
375
376 // Create new attribute item
377 AttributeItem Item = {
378 AttributeItem::NumericAndTextAttributes,
379 Attribute,
380 IntValue,
381 StringValue
382 };
383 Contents.push_back(Item);
384 }
385
Logan Chien439e8f92013-12-11 17:16:25 +0000386 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000387 void emitFPUDefaultAttributes();
388
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000389 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000390
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000391 virtual void emitFnStart();
392 virtual void emitFnEnd();
393 virtual void emitCantUnwind();
394 virtual void emitPersonality(const MCSymbol *Personality);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000395 virtual void emitPersonalityIndex(unsigned Index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000396 virtual void emitHandlerData();
397 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
398 virtual void emitPad(int64_t Offset);
399 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
400 bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000401 virtual void emitUnwindRaw(int64_t Offset,
402 const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000403
404 virtual void switchVendor(StringRef Vendor);
405 virtual void emitAttribute(unsigned Attribute, unsigned Value);
406 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000407 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
408 StringRef StringValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000409 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000410 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000411 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000412 virtual void finishAttributeSection();
413
414 size_t calculateContentSize() const;
415
416public:
417 ARMTargetELFStreamer()
418 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
Logan Chien439e8f92013-12-11 17:16:25 +0000419 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000420 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000421};
422
Tim Northover5cc3dc82012-12-07 16:50:23 +0000423/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
424/// the appropriate points in the object files. These symbols are defined in the
425/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
426///
427/// In brief: $a, $t or $d should be emitted at the start of each contiguous
428/// region of ARM code, Thumb code or data in a section. In practice, this
429/// emission does not rely on explicit assembler directives but on inherent
430/// properties of the directives doing the emission (e.g. ".byte" is data, "add
431/// r0, r0, r0" an instruction).
432///
433/// As a result this system is orthogonal to the DataRegion infrastructure used
434/// by MachO. Beware!
435class ARMELFStreamer : public MCELFStreamer {
436public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000437 friend class ARMTargetELFStreamer;
438
439 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
440 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
441 bool IsThumb)
442 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
443 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000444 Reset();
445 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000446
447 ~ARMELFStreamer() {}
448
Logan Chien8cbb80d2013-10-28 17:51:12 +0000449 virtual void FinishImpl();
450
Logan Chien2bcc42c2013-01-30 15:39:04 +0000451 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000452 void emitFnStart();
453 void emitFnEnd();
454 void emitCantUnwind();
455 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000456 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000457 void emitHandlerData();
458 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
459 void emitPad(int64_t Offset);
460 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000461 void emitUnwindRaw(int64_t Offset, const SmallVectorImpl<uint8_t> &Opcodes);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000462
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000463 virtual void ChangeSection(const MCSection *Section,
464 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000465 // We have to keep track of the mapping symbol state of any sections we
466 // use. Each one should start off as EMS_None, which is provided as the
467 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000468 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000469 LastEMS = LastMappingSymbols.lookup(Section);
470
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000471 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000472 }
473
474 /// This function is the one used to emit instruction data into the ELF
475 /// streamer. We override it to add the appropriate mapping symbol if
476 /// necessary.
477 virtual void EmitInstruction(const MCInst& Inst) {
478 if (IsThumb)
479 EmitThumbMappingSymbol();
480 else
481 EmitARMMappingSymbol();
482
483 MCELFStreamer::EmitInstruction(Inst);
484 }
485
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000486 virtual void emitInst(uint32_t Inst, char Suffix) {
487 unsigned Size;
488 char Buffer[4];
489 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
490
491 switch (Suffix) {
492 case '\0':
493 Size = 4;
494
495 assert(!IsThumb);
496 EmitARMMappingSymbol();
497 for (unsigned II = 0, IE = Size; II != IE; II++) {
498 const unsigned I = LittleEndian ? (Size - II - 1) : II;
499 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
500 }
501
502 break;
503 case 'n':
504 case 'w':
505 Size = (Suffix == 'n' ? 2 : 4);
506
507 assert(IsThumb);
508 EmitThumbMappingSymbol();
509 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
510 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
511 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
512 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
513 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
514 }
515
516 break;
517 default:
518 llvm_unreachable("Invalid Suffix");
519 }
520
521 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
522 }
523
Tim Northover5cc3dc82012-12-07 16:50:23 +0000524 /// This is one of the functions used to emit data into an ELF section, so the
525 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
526 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000527 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000528 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000529 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000530 }
531
532 /// This is one of the functions used to emit data into an ELF section, so the
533 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
534 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000535 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000536 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000537 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000538 }
539
540 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
541 MCELFStreamer::EmitAssemblerFlag(Flag);
542
543 switch (Flag) {
544 case MCAF_SyntaxUnified:
545 return; // no-op here.
546 case MCAF_Code16:
547 IsThumb = true;
548 return; // Change to Thumb mode
549 case MCAF_Code32:
550 IsThumb = false;
551 return; // Change to ARM mode
552 case MCAF_Code64:
553 return;
554 case MCAF_SubsectionsViaSymbols:
555 return;
556 }
557 }
558
559private:
560 enum ElfMappingSymbol {
561 EMS_None,
562 EMS_ARM,
563 EMS_Thumb,
564 EMS_Data
565 };
566
567 void EmitDataMappingSymbol() {
568 if (LastEMS == EMS_Data) return;
569 EmitMappingSymbol("$d");
570 LastEMS = EMS_Data;
571 }
572
573 void EmitThumbMappingSymbol() {
574 if (LastEMS == EMS_Thumb) return;
575 EmitMappingSymbol("$t");
576 LastEMS = EMS_Thumb;
577 }
578
579 void EmitARMMappingSymbol() {
580 if (LastEMS == EMS_ARM) return;
581 EmitMappingSymbol("$a");
582 LastEMS = EMS_ARM;
583 }
584
585 void EmitMappingSymbol(StringRef Name) {
586 MCSymbol *Start = getContext().CreateTempSymbol();
587 EmitLabel(Start);
588
Chandler Carruth1d94e932012-12-08 03:10:14 +0000589 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000590 getContext().GetOrCreateSymbol(Name + "." +
591 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000592
593 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
594 MCELF::SetType(SD, ELF::STT_NOTYPE);
595 MCELF::SetBinding(SD, ELF::STB_LOCAL);
596 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000597 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000598
599 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
600 Symbol->setVariableValue(Value);
601 }
602
603 void EmitThumbFunc(MCSymbol *Func) {
604 // FIXME: Anything needed here to flag the function as thumb?
605
606 getAssembler().setIsThumbFunc(Func);
607
608 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
609 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
610 }
611
Logan Chien2bcc42c2013-01-30 15:39:04 +0000612 // Helper functions for ARM exception handling directives
613 void Reset();
614
615 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000616 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000617 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000618
619 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
620 SectionKind Kind, const MCSymbol &Fn);
621 void SwitchToExTabSection(const MCSymbol &FnStart);
622 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000623
624 bool IsThumb;
625 int64_t MappingSymbolCounter;
626
627 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
628 ElfMappingSymbol LastEMS;
629
Logan Chien2bcc42c2013-01-30 15:39:04 +0000630 // ARM Exception Handling Frame Information
631 MCSymbol *ExTab;
632 MCSymbol *FnStart;
633 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000634 unsigned PersonalityIndex;
635 unsigned FPReg; // Frame pointer register
636 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
637 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
638 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000639 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000640 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000641 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000642 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000643};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000644} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000645
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000646ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Benjamin Kramer41882932013-10-09 17:23:41 +0000647 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000648 return *S;
649}
650
651void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
652void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
653void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
654void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
655 getStreamer().emitPersonality(Personality);
656}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000657void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
658 getStreamer().emitPersonalityIndex(Index);
659}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000660void ARMTargetELFStreamer::emitHandlerData() {
661 getStreamer().emitHandlerData();
662}
663void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
664 int64_t Offset) {
665 getStreamer().emitSetFP(FpReg, SpReg, Offset);
666}
667void ARMTargetELFStreamer::emitPad(int64_t Offset) {
668 getStreamer().emitPad(Offset);
669}
670void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
671 bool isVector) {
672 getStreamer().emitRegSave(RegList, isVector);
673}
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +0000674void ARMTargetELFStreamer::emitUnwindRaw(int64_t Offset,
675 const SmallVectorImpl<uint8_t> &Opcodes) {
676 getStreamer().emitUnwindRaw(Offset, Opcodes);
677}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000678void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
679 assert(!Vendor.empty() && "Vendor cannot be empty.");
680
681 if (CurrentVendor == Vendor)
682 return;
683
684 if (!CurrentVendor.empty())
685 finishAttributeSection();
686
687 assert(Contents.empty() &&
688 ".ARM.attributes should be flushed before changing vendor");
689 CurrentVendor = Vendor;
690
691}
692void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
693 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
694}
695void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
696 StringRef Value) {
697 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
698}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000699void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
700 unsigned IntValue,
701 StringRef StringValue) {
702 setAttributeItems(Attribute, IntValue, StringValue,
703 /* OverwriteExisting= */ true);
704}
Logan Chien439e8f92013-12-11 17:16:25 +0000705void ARMTargetELFStreamer::emitArch(unsigned Value) {
706 Arch = Value;
707}
708void ARMTargetELFStreamer::emitArchDefaultAttributes() {
709 using namespace ARMBuildAttrs;
710 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
711 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
712
713 switch (Arch) {
714 case ARM::ARMV2:
715 case ARM::ARMV2A:
716 case ARM::ARMV3:
717 case ARM::ARMV3M:
718 case ARM::ARMV4:
719 case ARM::ARMV5:
720 setAttributeItem(ARM_ISA_use, Allowed, false);
721 break;
722
723 case ARM::ARMV4T:
724 case ARM::ARMV5T:
725 case ARM::ARMV5TE:
726 case ARM::ARMV6:
727 case ARM::ARMV6J:
728 setAttributeItem(ARM_ISA_use, Allowed, false);
729 setAttributeItem(THUMB_ISA_use, Allowed, false);
730 break;
731
732 case ARM::ARMV6T2:
733 setAttributeItem(ARM_ISA_use, Allowed, false);
734 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
735 break;
736
737 case ARM::ARMV6Z:
738 case ARM::ARMV6ZK:
739 setAttributeItem(ARM_ISA_use, Allowed, false);
740 setAttributeItem(THUMB_ISA_use, Allowed, false);
741 setAttributeItem(Virtualization_use, AllowTZ, false);
742 break;
743
744 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000745 setAttributeItem(THUMB_ISA_use, Allowed, false);
746 break;
747
748 case ARM::ARMV7:
749 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
750 break;
751
752 case ARM::ARMV7A:
753 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
754 setAttributeItem(ARM_ISA_use, Allowed, false);
755 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
756 break;
757
758 case ARM::ARMV7R:
759 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
760 setAttributeItem(ARM_ISA_use, Allowed, false);
761 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
762 break;
763
764 case ARM::ARMV7M:
765 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
766 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
767 break;
768
769 case ARM::ARMV8A:
770 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
771 setAttributeItem(ARM_ISA_use, Allowed, false);
772 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
773 setAttributeItem(MPextension_use, Allowed, false);
774 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
775 break;
776
777 case ARM::IWMMXT:
778 setAttributeItem(ARM_ISA_use, Allowed, false);
779 setAttributeItem(THUMB_ISA_use, Allowed, false);
780 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
781 break;
782
783 case ARM::IWMMXT2:
784 setAttributeItem(ARM_ISA_use, Allowed, false);
785 setAttributeItem(THUMB_ISA_use, Allowed, false);
786 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
787 break;
788
789 default:
790 report_fatal_error("Unknown Arch: " + Twine(Arch));
791 break;
792 }
793}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000794void ARMTargetELFStreamer::emitFPU(unsigned Value) {
795 FPU = Value;
796}
797void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
798 switch (FPU) {
799 case ARM::VFP:
800 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000801 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000802 ARMBuildAttrs::AllowFPv2,
803 /* OverwriteExisting= */ false);
804 break;
805
806 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000807 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000808 ARMBuildAttrs::AllowFPv3A,
809 /* OverwriteExisting= */ false);
810 break;
811
812 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000813 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000814 ARMBuildAttrs::AllowFPv3B,
815 /* OverwriteExisting= */ false);
816 break;
817
818 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000819 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000820 ARMBuildAttrs::AllowFPv4A,
821 /* OverwriteExisting= */ false);
822 break;
823
824 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000825 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000826 ARMBuildAttrs::AllowFPv4B,
827 /* OverwriteExisting= */ false);
828 break;
829
830 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000831 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000832 ARMBuildAttrs::AllowFPARMv8A,
833 /* OverwriteExisting= */ false);
834 break;
835
836 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000837 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000838 ARMBuildAttrs::AllowFPv3A,
839 /* OverwriteExisting= */ false);
840 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
841 ARMBuildAttrs::AllowNeon,
842 /* OverwriteExisting= */ false);
843 break;
844
845 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000846 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000847 ARMBuildAttrs::AllowFPv4A,
848 /* OverwriteExisting= */ false);
849 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
850 ARMBuildAttrs::AllowNeon2,
851 /* OverwriteExisting= */ false);
852 break;
853
854 case ARM::NEON_FP_ARMV8:
855 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000856 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000857 ARMBuildAttrs::AllowFPARMv8A,
858 /* OverwriteExisting= */ false);
859 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
860 ARMBuildAttrs::AllowNeonARMv8,
861 /* OverwriteExisting= */ false);
862 break;
863
Logan Chien05ae7442014-01-02 15:50:02 +0000864 case ARM::SOFTVFP:
865 break;
866
Logan Chien8cbb80d2013-10-28 17:51:12 +0000867 default:
868 report_fatal_error("Unknown FPU: " + Twine(FPU));
869 break;
870 }
871}
872size_t ARMTargetELFStreamer::calculateContentSize() const {
873 size_t Result = 0;
874 for (size_t i = 0; i < Contents.size(); ++i) {
875 AttributeItem item = Contents[i];
876 switch (item.Type) {
877 case AttributeItem::HiddenAttribute:
878 break;
879 case AttributeItem::NumericAttribute:
880 Result += getULEBSize(item.Tag);
881 Result += getULEBSize(item.IntValue);
882 break;
883 case AttributeItem::TextAttribute:
884 Result += getULEBSize(item.Tag);
885 Result += item.StringValue.size() + 1; // string + '\0'
886 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000887 case AttributeItem::NumericAndTextAttributes:
888 Result += getULEBSize(item.Tag);
889 Result += getULEBSize(item.IntValue);
890 Result += item.StringValue.size() + 1; // string + '\0';
891 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000892 }
893 }
894 return Result;
895}
896void ARMTargetELFStreamer::finishAttributeSection() {
897 // <format-version>
898 // [ <section-length> "vendor-name"
899 // [ <file-tag> <size> <attribute>*
900 // | <section-tag> <size> <section-number>* 0 <attribute>*
901 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
902 // ]+
903 // ]*
904
905 if (FPU != ARM::INVALID_FPU)
906 emitFPUDefaultAttributes();
907
Logan Chien439e8f92013-12-11 17:16:25 +0000908 if (Arch != ARM::INVALID_ARCH)
909 emitArchDefaultAttributes();
910
Logan Chien8cbb80d2013-10-28 17:51:12 +0000911 if (Contents.empty())
912 return;
913
914 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
915
916 ARMELFStreamer &Streamer = getStreamer();
917
918 // Switch to .ARM.attributes section
919 if (AttributeSection) {
920 Streamer.SwitchSection(AttributeSection);
921 } else {
922 AttributeSection =
923 Streamer.getContext().getELFSection(".ARM.attributes",
924 ELF::SHT_ARM_ATTRIBUTES,
925 0,
926 SectionKind::getMetadata());
927 Streamer.SwitchSection(AttributeSection);
928
929 // Format version
930 Streamer.EmitIntValue(0x41, 1);
931 }
932
933 // Vendor size + Vendor name + '\0'
934 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
935
936 // Tag + Tag Size
937 const size_t TagHeaderSize = 1 + 4;
938
939 const size_t ContentsSize = calculateContentSize();
940
941 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
942 Streamer.EmitBytes(CurrentVendor);
943 Streamer.EmitIntValue(0, 1); // '\0'
944
945 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
946 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
947
948 // Size should have been accounted for already, now
949 // emit each field as its type (ULEB or String)
950 for (size_t i = 0; i < Contents.size(); ++i) {
951 AttributeItem item = Contents[i];
952 Streamer.EmitULEB128IntValue(item.Tag);
953 switch (item.Type) {
954 default: llvm_unreachable("Invalid attribute type");
955 case AttributeItem::NumericAttribute:
956 Streamer.EmitULEB128IntValue(item.IntValue);
957 break;
958 case AttributeItem::TextAttribute:
959 Streamer.EmitBytes(item.StringValue.upper());
960 Streamer.EmitIntValue(0, 1); // '\0'
961 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000962 case AttributeItem::NumericAndTextAttributes:
963 Streamer.EmitULEB128IntValue(item.IntValue);
964 Streamer.EmitBytes(item.StringValue.upper());
965 Streamer.EmitIntValue(0, 1); // '\0'
966 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000967 }
968 }
969
970 Contents.clear();
971 FPU = ARM::INVALID_FPU;
972}
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000973void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
974 getStreamer().emitInst(Inst, Suffix);
975}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000976
977void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +0000978 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000979 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
980 ATS.finishAttributeSection();
981
982 MCELFStreamer::FinishImpl();
983}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000984
Logan Chien2bcc42c2013-01-30 15:39:04 +0000985inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
986 unsigned Type,
987 unsigned Flags,
988 SectionKind Kind,
989 const MCSymbol &Fn) {
990 const MCSectionELF &FnSection =
991 static_cast<const MCSectionELF &>(Fn.getSection());
992
993 // Create the name for new section
994 StringRef FnSecName(FnSection.getSectionName());
995 SmallString<128> EHSecName(Prefix);
996 if (FnSecName != ".text") {
997 EHSecName += FnSecName;
998 }
999
1000 // Get .ARM.extab or .ARM.exidx section
1001 const MCSectionELF *EHSection = NULL;
1002 if (const MCSymbol *Group = FnSection.getGroup()) {
1003 EHSection = getContext().getELFSection(
1004 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
1005 FnSection.getEntrySize(), Group->getName());
1006 } else {
1007 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
1008 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001009 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +00001010
1011 // Switch to .ARM.extab or .ARM.exidx section
1012 SwitchSection(EHSection);
1013 EmitCodeAlignment(4, 0);
1014}
1015
1016inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
1017 SwitchToEHSection(".ARM.extab",
1018 ELF::SHT_PROGBITS,
1019 ELF::SHF_ALLOC,
1020 SectionKind::getDataRel(),
1021 FnStart);
1022}
1023
1024inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
1025 SwitchToEHSection(".ARM.exidx",
1026 ELF::SHT_ARM_EXIDX,
1027 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
1028 SectionKind::getDataRel(),
1029 FnStart);
1030}
1031
1032void ARMELFStreamer::Reset() {
1033 ExTab = NULL;
1034 FnStart = NULL;
1035 Personality = NULL;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001036 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +00001037 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001038 FPOffset = 0;
1039 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001040 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001041 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001042 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001043
Logan Chien325823a2013-06-09 12:22:30 +00001044 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001045 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001046}
1047
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001048void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001049 assert(FnStart == 0);
1050 FnStart = getContext().CreateTempSymbol();
1051 EmitLabel(FnStart);
1052}
1053
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001054void ARMELFStreamer::emitFnEnd() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001055 assert(FnStart && ".fnstart must preceeds .fnend");
1056
1057 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001058 if (!ExTab && !CantUnwind)
1059 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001060
1061 // Emit the exception index table entry
1062 SwitchToExIdxSection(*FnStart);
1063
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001064 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001065 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001066
1067 const MCSymbolRefExpr *FnStartRef =
1068 MCSymbolRefExpr::Create(FnStart,
1069 MCSymbolRefExpr::VK_ARM_PREL31,
1070 getContext());
1071
Rafael Espindola64e1af82013-07-02 15:49:13 +00001072 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001073
1074 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001075 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001076 } else if (ExTab) {
1077 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001078 const MCSymbolRefExpr *ExTabEntryRef =
1079 MCSymbolRefExpr::Create(ExTab,
1080 MCSymbolRefExpr::VK_ARM_PREL31,
1081 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001082 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001083 } else {
1084 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1085 // the second word of exception index table entry. The size of the unwind
1086 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001087 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001088 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001089 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001090 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +00001091 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001092 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001093 }
1094
Logan Chien4ea23b52013-05-10 16:17:24 +00001095 // Switch to the section containing FnStart
1096 SwitchSection(&FnStart->getSection());
1097
Logan Chien2bcc42c2013-01-30 15:39:04 +00001098 // Clean exception handling frame information
1099 Reset();
1100}
1101
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001102void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1103
1104// Add the R_ARM_NONE fixup at the same position
1105void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1106 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1107
1108 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1109 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1110
1111 AddValueSymbols(PersonalityRef);
1112 MCDataFragment *DF = getOrCreateDataFragment();
1113 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1114 PersonalityRef,
1115 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001116}
1117
Logan Chien325823a2013-06-09 12:22:30 +00001118void ARMELFStreamer::FlushPendingOffset() {
1119 if (PendingOffset != 0) {
1120 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1121 PendingOffset = 0;
1122 }
1123}
1124
Logan Chienc931fce2013-07-02 12:43:27 +00001125void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001126 // Emit the unwind opcode to restore $sp.
1127 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001128 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001129 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1130 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001131 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001132 } else {
1133 FlushPendingOffset();
1134 }
1135
1136 // Finalize the unwind opcode sequence
1137 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001138
1139 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1140 // section. Thus, we don't have to create an entry in the .ARM.extab
1141 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001142 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001143 return;
1144
1145 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001146 SwitchToExTabSection(*FnStart);
1147
1148 // Create .ARM.extab label for offset in .ARM.exidx
1149 assert(!ExTab);
1150 ExTab = getContext().CreateTempSymbol();
1151 EmitLabel(ExTab);
1152
Logan Chien4ea23b52013-05-10 16:17:24 +00001153 // Emit personality
1154 if (Personality) {
1155 const MCSymbolRefExpr *PersonalityRef =
1156 MCSymbolRefExpr::Create(Personality,
1157 MCSymbolRefExpr::VK_ARM_PREL31,
1158 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001159
Rafael Espindola64e1af82013-07-02 15:49:13 +00001160 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001161 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001162
1163 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +00001164 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001165 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +00001166
1167 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1168 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1169 // after the unwind opcodes. The handler data consists of several 32-bit
1170 // words, and should be terminated by zero.
1171 //
1172 // In case that the .handlerdata directive is not specified by the
1173 // programmer, we should emit zero to terminate the handler data.
1174 if (NoHandlerData && !Personality)
1175 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001176}
1177
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001178void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001179
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001180void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001181 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001182 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001183}
1184
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001185void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1186 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1187 PersonalityIndex = Index;
1188}
1189
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001190void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001191 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001192 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001193 "the operand of .setfp directive should be either $sp or $fp");
1194
1195 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001196 FPReg = NewFPReg;
1197
1198 if (NewSPReg == ARM::SP)
1199 FPOffset = SPOffset + Offset;
1200 else
1201 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001202}
1203
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001204void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001205 // Track the change of the $sp offset
1206 SPOffset -= Offset;
1207
1208 // To squash multiple .pad directives, we should delay the unwind opcode
1209 // until the .save, .vsave, .handlerdata, or .fnend directives.
1210 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001211}
1212
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001213void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001214 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001215 // Collect the registers in the register list
1216 unsigned Count = 0;
1217 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001218 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001219 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001220 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001221 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001222 unsigned Bit = (1u << Reg);
1223 if ((Mask & Bit) == 0) {
1224 Mask |= Bit;
1225 ++Count;
1226 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001227 }
Logan Chien325823a2013-06-09 12:22:30 +00001228
1229 // Track the change the $sp offset: For the .save directive, the
1230 // corresponding push instruction will decrease the $sp by (4 * Count).
1231 // For the .vsave directive, the corresponding vpush instruction will
1232 // decrease $sp by (8 * Count).
1233 SPOffset -= Count * (IsVector ? 8 : 4);
1234
1235 // Emit the opcode
1236 FlushPendingOffset();
1237 if (IsVector)
1238 UnwindOpAsm.EmitVFPRegSave(Mask);
1239 else
1240 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001241}
1242
Saleem Abdulrasoold9f08602014-01-21 02:33:10 +00001243void ARMELFStreamer::emitUnwindRaw(int64_t Offset,
1244 const SmallVectorImpl<uint8_t> &Opcodes) {
1245 FlushPendingOffset();
1246 SPOffset = SPOffset - Offset;
1247 UnwindOpAsm.EmitRaw(Opcodes);
1248}
1249
Tim Northover5cc3dc82012-12-07 16:50:23 +00001250namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001251
1252MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1253 bool isVerboseAsm, bool useLoc, bool useCFI,
1254 bool useDwarfDirectory,
1255 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1256 MCAsmBackend *TAB, bool ShowInst) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +00001257 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1258 isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001259
1260 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1261 useDwarfDirectory, InstPrint, CE, TAB,
1262 ShowInst);
1263}
1264
Tim Northover5cc3dc82012-12-07 16:50:23 +00001265 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1266 raw_ostream &OS, MCCodeEmitter *Emitter,
1267 bool RelaxAll, bool NoExecStack,
1268 bool IsThumb) {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001269 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1270 ARMELFStreamer *S =
1271 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001272 // FIXME: This should eventually end up somewhere else where more
1273 // intelligent flag decisions can be made. For now we are just maintaining
1274 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1275 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1276
Tim Northover5cc3dc82012-12-07 16:50:23 +00001277 if (RelaxAll)
1278 S->getAssembler().setRelaxAll(true);
1279 if (NoExecStack)
1280 S->getAssembler().setNoExecStack(true);
1281 return S;
1282 }
1283
1284}
1285
1286