blob: 82543a45f24d824aac8c971a1a2f685b788efd5e [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);
126
Logan Chien8cbb80d2013-10-28 17:51:12 +0000127 virtual void switchVendor(StringRef Vendor);
128 virtual void emitAttribute(unsigned Attribute, unsigned Value);
129 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000130 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
131 StringRef StrinValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000132 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000133 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000134 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000135 virtual void finishAttributeSection();
136
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000137public:
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000138 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter,
139 bool VerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000140};
141
142ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000143 MCInstPrinter &InstPrinter,
144 bool VerboseAsm)
145 : OS(OS), InstPrinter(InstPrinter), IsVerboseAsm(VerboseAsm) {}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000146void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
147void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
148void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
149void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
150 OS << "\t.personality " << Personality->getName() << '\n';
151}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000152void ARMTargetAsmStreamer::emitPersonalityIndex(unsigned Index) {
153 OS << "\t.personalityindex " << Index << '\n';
154}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000155void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
156void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
157 int64_t Offset) {
158 OS << "\t.setfp\t";
159 InstPrinter.printRegName(OS, FpReg);
160 OS << ", ";
161 InstPrinter.printRegName(OS, SpReg);
162 if (Offset)
163 OS << ", #" << Offset;
164 OS << '\n';
165}
166void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
167 OS << "\t.pad\t#" << Offset << '\n';
168}
169void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
170 bool isVector) {
171 assert(RegList.size() && "RegList should not be empty");
172 if (isVector)
173 OS << "\t.vsave\t{";
174 else
175 OS << "\t.save\t{";
176
177 InstPrinter.printRegName(OS, RegList[0]);
178
179 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
180 OS << ", ";
181 InstPrinter.printRegName(OS, RegList[i]);
182 }
183
184 OS << "}\n";
185}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000186void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
187}
188void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000189 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value);
190 if (IsVerboseAsm) {
191 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
192 if (!Name.empty())
193 OS << "\t@ " << Name;
194 }
195 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000196}
197void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
198 StringRef String) {
199 switch (Attribute) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000200 case ARMBuildAttrs::CPU_name:
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000201 OS << "\t.cpu\t" << String.lower();
202 break;
203 default:
204 OS << "\t.eabi_attribute\t" << Attribute << ", \"" << String << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000205 if (IsVerboseAsm) {
206 StringRef Name = ARMBuildAttrs::AttrTypeAsString(Attribute);
207 if (!Name.empty())
208 OS << "\t@ " << Name;
209 }
Logan Chien8cbb80d2013-10-28 17:51:12 +0000210 break;
211 }
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000212 OS << "\n";
213}
214void ARMTargetAsmStreamer::emitIntTextAttribute(unsigned Attribute,
215 unsigned IntValue,
216 StringRef StringValue) {
217 switch (Attribute) {
218 default: llvm_unreachable("unsupported multi-value attribute in asm mode");
219 case ARMBuildAttrs::compatibility:
220 OS << "\t.eabi_attribute\t" << Attribute << ", " << IntValue;
221 if (!StringValue.empty())
222 OS << ", \"" << StringValue << "\"";
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +0000223 if (IsVerboseAsm)
224 OS << "\t@ " << ARMBuildAttrs::AttrTypeAsString(Attribute);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000225 break;
226 }
227 OS << "\n";
Logan Chien8cbb80d2013-10-28 17:51:12 +0000228}
Logan Chien439e8f92013-12-11 17:16:25 +0000229void ARMTargetAsmStreamer::emitArch(unsigned Arch) {
230 OS << "\t.arch\t" << GetArchName(Arch) << "\n";
231}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000232void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
233 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
234}
235void ARMTargetAsmStreamer::finishAttributeSection() {
236}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000237
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000238void ARMTargetAsmStreamer::emitInst(uint32_t Inst, char Suffix) {
239 OS << "\t.inst";
240 if (Suffix)
241 OS << "." << Suffix;
242 OS << "\t0x" << utohexstr(Inst) << "\n";
243}
244
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000245class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000246private:
247 // This structure holds all attributes, accounting for
248 // their string/numeric value, so we can later emmit them
249 // in declaration order, keeping all in the same vector
250 struct AttributeItem {
251 enum {
252 HiddenAttribute = 0,
253 NumericAttribute,
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000254 TextAttribute,
255 NumericAndTextAttributes
Logan Chien8cbb80d2013-10-28 17:51:12 +0000256 } Type;
257 unsigned Tag;
258 unsigned IntValue;
259 StringRef StringValue;
260
261 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
262 return (LHS.Tag < RHS.Tag);
263 }
264 };
265
266 StringRef CurrentVendor;
267 unsigned FPU;
Logan Chien439e8f92013-12-11 17:16:25 +0000268 unsigned Arch;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000269 SmallVector<AttributeItem, 64> Contents;
270
271 const MCSection *AttributeSection;
272
273 // FIXME: this should be in a more generic place, but
274 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
275 static size_t getULEBSize(int Value) {
276 size_t Size = 0;
277 do {
278 Value >>= 7;
279 Size += sizeof(int8_t); // Is this really necessary?
280 } while (Value);
281 return Size;
282 }
283
284 AttributeItem *getAttributeItem(unsigned Attribute) {
285 for (size_t i = 0; i < Contents.size(); ++i)
286 if (Contents[i].Tag == Attribute)
287 return &Contents[i];
288 return 0;
289 }
290
291 void setAttributeItem(unsigned Attribute, unsigned Value,
292 bool OverwriteExisting) {
293 // Look for existing attribute item
294 if (AttributeItem *Item = getAttributeItem(Attribute)) {
295 if (!OverwriteExisting)
296 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000297 Item->Type = AttributeItem::NumericAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000298 Item->IntValue = Value;
299 return;
300 }
301
302 // Create new attribute item
303 AttributeItem Item = {
304 AttributeItem::NumericAttribute,
305 Attribute,
306 Value,
307 StringRef("")
308 };
309 Contents.push_back(Item);
310 }
311
312 void setAttributeItem(unsigned Attribute, StringRef Value,
313 bool OverwriteExisting) {
314 // Look for existing attribute item
315 if (AttributeItem *Item = getAttributeItem(Attribute)) {
316 if (!OverwriteExisting)
317 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000318 Item->Type = AttributeItem::TextAttribute;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000319 Item->StringValue = Value;
320 return;
321 }
322
323 // Create new attribute item
324 AttributeItem Item = {
325 AttributeItem::TextAttribute,
326 Attribute,
327 0,
328 Value
329 };
330 Contents.push_back(Item);
331 }
332
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000333 void setAttributeItems(unsigned Attribute, unsigned IntValue,
334 StringRef StringValue, bool OverwriteExisting) {
335 // Look for existing attribute item
336 if (AttributeItem *Item = getAttributeItem(Attribute)) {
337 if (!OverwriteExisting)
338 return;
Saleem Abdulrasool93900052014-01-19 08:25:41 +0000339 Item->Type = AttributeItem::NumericAndTextAttributes;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000340 Item->IntValue = IntValue;
341 Item->StringValue = StringValue;
342 return;
343 }
344
345 // Create new attribute item
346 AttributeItem Item = {
347 AttributeItem::NumericAndTextAttributes,
348 Attribute,
349 IntValue,
350 StringValue
351 };
352 Contents.push_back(Item);
353 }
354
Logan Chien439e8f92013-12-11 17:16:25 +0000355 void emitArchDefaultAttributes();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000356 void emitFPUDefaultAttributes();
357
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000358 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000359
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000360 virtual void emitFnStart();
361 virtual void emitFnEnd();
362 virtual void emitCantUnwind();
363 virtual void emitPersonality(const MCSymbol *Personality);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000364 virtual void emitPersonalityIndex(unsigned Index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000365 virtual void emitHandlerData();
366 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
367 virtual void emitPad(int64_t Offset);
368 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
369 bool isVector);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000370
371 virtual void switchVendor(StringRef Vendor);
372 virtual void emitAttribute(unsigned Attribute, unsigned Value);
373 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000374 virtual void emitIntTextAttribute(unsigned Attribute, unsigned IntValue,
375 StringRef StringValue);
Logan Chien439e8f92013-12-11 17:16:25 +0000376 virtual void emitArch(unsigned Arch);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000377 virtual void emitFPU(unsigned FPU);
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000378 virtual void emitInst(uint32_t Inst, char Suffix = '\0');
Logan Chien8cbb80d2013-10-28 17:51:12 +0000379 virtual void finishAttributeSection();
380
381 size_t calculateContentSize() const;
382
383public:
384 ARMTargetELFStreamer()
385 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
Logan Chien439e8f92013-12-11 17:16:25 +0000386 Arch(ARM::INVALID_ARCH), AttributeSection(0) {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000387 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000388};
389
Tim Northover5cc3dc82012-12-07 16:50:23 +0000390/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
391/// the appropriate points in the object files. These symbols are defined in the
392/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
393///
394/// In brief: $a, $t or $d should be emitted at the start of each contiguous
395/// region of ARM code, Thumb code or data in a section. In practice, this
396/// emission does not rely on explicit assembler directives but on inherent
397/// properties of the directives doing the emission (e.g. ".byte" is data, "add
398/// r0, r0, r0" an instruction).
399///
400/// As a result this system is orthogonal to the DataRegion infrastructure used
401/// by MachO. Beware!
402class ARMELFStreamer : public MCELFStreamer {
403public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000404 friend class ARMTargetELFStreamer;
405
406 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
407 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
408 bool IsThumb)
409 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
410 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000411 Reset();
412 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000413
414 ~ARMELFStreamer() {}
415
Logan Chien8cbb80d2013-10-28 17:51:12 +0000416 virtual void FinishImpl();
417
Logan Chien2bcc42c2013-01-30 15:39:04 +0000418 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000419 void emitFnStart();
420 void emitFnEnd();
421 void emitCantUnwind();
422 void emitPersonality(const MCSymbol *Per);
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000423 void emitPersonalityIndex(unsigned index);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000424 void emitHandlerData();
425 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
426 void emitPad(int64_t Offset);
427 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000428
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000429 virtual void ChangeSection(const MCSection *Section,
430 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000431 // We have to keep track of the mapping symbol state of any sections we
432 // use. Each one should start off as EMS_None, which is provided as the
433 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000434 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000435 LastEMS = LastMappingSymbols.lookup(Section);
436
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000437 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000438 }
439
440 /// This function is the one used to emit instruction data into the ELF
441 /// streamer. We override it to add the appropriate mapping symbol if
442 /// necessary.
443 virtual void EmitInstruction(const MCInst& Inst) {
444 if (IsThumb)
445 EmitThumbMappingSymbol();
446 else
447 EmitARMMappingSymbol();
448
449 MCELFStreamer::EmitInstruction(Inst);
450 }
451
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000452 virtual void emitInst(uint32_t Inst, char Suffix) {
453 unsigned Size;
454 char Buffer[4];
455 const bool LittleEndian = getContext().getAsmInfo()->isLittleEndian();
456
457 switch (Suffix) {
458 case '\0':
459 Size = 4;
460
461 assert(!IsThumb);
462 EmitARMMappingSymbol();
463 for (unsigned II = 0, IE = Size; II != IE; II++) {
464 const unsigned I = LittleEndian ? (Size - II - 1) : II;
465 Buffer[Size - II - 1] = uint8_t(Inst >> I * CHAR_BIT);
466 }
467
468 break;
469 case 'n':
470 case 'w':
471 Size = (Suffix == 'n' ? 2 : 4);
472
473 assert(IsThumb);
474 EmitThumbMappingSymbol();
475 for (unsigned II = 0, IE = Size; II != IE; II = II + 2) {
476 const unsigned I0 = LittleEndian ? II + 0 : (Size - II - 1);
477 const unsigned I1 = LittleEndian ? II + 1 : (Size - II - 2);
478 Buffer[Size - II - 2] = uint8_t(Inst >> I0 * CHAR_BIT);
479 Buffer[Size - II - 1] = uint8_t(Inst >> I1 * CHAR_BIT);
480 }
481
482 break;
483 default:
484 llvm_unreachable("Invalid Suffix");
485 }
486
487 MCELFStreamer::EmitBytes(StringRef(Buffer, Size));
488 }
489
Tim Northover5cc3dc82012-12-07 16:50:23 +0000490 /// This is one of the functions used to emit data into an ELF section, so the
491 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
492 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000493 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000494 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000495 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000496 }
497
498 /// This is one of the functions used to emit data into an ELF section, so the
499 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
500 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000501 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000502 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000503 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000504 }
505
506 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
507 MCELFStreamer::EmitAssemblerFlag(Flag);
508
509 switch (Flag) {
510 case MCAF_SyntaxUnified:
511 return; // no-op here.
512 case MCAF_Code16:
513 IsThumb = true;
514 return; // Change to Thumb mode
515 case MCAF_Code32:
516 IsThumb = false;
517 return; // Change to ARM mode
518 case MCAF_Code64:
519 return;
520 case MCAF_SubsectionsViaSymbols:
521 return;
522 }
523 }
524
525private:
526 enum ElfMappingSymbol {
527 EMS_None,
528 EMS_ARM,
529 EMS_Thumb,
530 EMS_Data
531 };
532
533 void EmitDataMappingSymbol() {
534 if (LastEMS == EMS_Data) return;
535 EmitMappingSymbol("$d");
536 LastEMS = EMS_Data;
537 }
538
539 void EmitThumbMappingSymbol() {
540 if (LastEMS == EMS_Thumb) return;
541 EmitMappingSymbol("$t");
542 LastEMS = EMS_Thumb;
543 }
544
545 void EmitARMMappingSymbol() {
546 if (LastEMS == EMS_ARM) return;
547 EmitMappingSymbol("$a");
548 LastEMS = EMS_ARM;
549 }
550
551 void EmitMappingSymbol(StringRef Name) {
552 MCSymbol *Start = getContext().CreateTempSymbol();
553 EmitLabel(Start);
554
Chandler Carruth1d94e932012-12-08 03:10:14 +0000555 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000556 getContext().GetOrCreateSymbol(Name + "." +
557 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000558
559 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
560 MCELF::SetType(SD, ELF::STT_NOTYPE);
561 MCELF::SetBinding(SD, ELF::STB_LOCAL);
562 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000563 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000564
565 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
566 Symbol->setVariableValue(Value);
567 }
568
569 void EmitThumbFunc(MCSymbol *Func) {
570 // FIXME: Anything needed here to flag the function as thumb?
571
572 getAssembler().setIsThumbFunc(Func);
573
574 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
575 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
576 }
577
Logan Chien2bcc42c2013-01-30 15:39:04 +0000578 // Helper functions for ARM exception handling directives
579 void Reset();
580
581 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000582 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000583 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000584
585 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
586 SectionKind Kind, const MCSymbol &Fn);
587 void SwitchToExTabSection(const MCSymbol &FnStart);
588 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000589
590 bool IsThumb;
591 int64_t MappingSymbolCounter;
592
593 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
594 ElfMappingSymbol LastEMS;
595
Logan Chien2bcc42c2013-01-30 15:39:04 +0000596 // ARM Exception Handling Frame Information
597 MCSymbol *ExTab;
598 MCSymbol *FnStart;
599 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000600 unsigned PersonalityIndex;
601 unsigned FPReg; // Frame pointer register
602 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
603 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
604 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000605 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000606 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000607 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000608 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000609};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000610} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000611
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000612ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Benjamin Kramer41882932013-10-09 17:23:41 +0000613 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000614 return *S;
615}
616
617void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
618void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
619void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
620void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
621 getStreamer().emitPersonality(Personality);
622}
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +0000623void ARMTargetELFStreamer::emitPersonalityIndex(unsigned Index) {
624 getStreamer().emitPersonalityIndex(Index);
625}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000626void ARMTargetELFStreamer::emitHandlerData() {
627 getStreamer().emitHandlerData();
628}
629void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
630 int64_t Offset) {
631 getStreamer().emitSetFP(FpReg, SpReg, Offset);
632}
633void ARMTargetELFStreamer::emitPad(int64_t Offset) {
634 getStreamer().emitPad(Offset);
635}
636void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
637 bool isVector) {
638 getStreamer().emitRegSave(RegList, isVector);
639}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000640void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
641 assert(!Vendor.empty() && "Vendor cannot be empty.");
642
643 if (CurrentVendor == Vendor)
644 return;
645
646 if (!CurrentVendor.empty())
647 finishAttributeSection();
648
649 assert(Contents.empty() &&
650 ".ARM.attributes should be flushed before changing vendor");
651 CurrentVendor = Vendor;
652
653}
654void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
655 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
656}
657void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
658 StringRef Value) {
659 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
660}
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000661void ARMTargetELFStreamer::emitIntTextAttribute(unsigned Attribute,
662 unsigned IntValue,
663 StringRef StringValue) {
664 setAttributeItems(Attribute, IntValue, StringValue,
665 /* OverwriteExisting= */ true);
666}
Logan Chien439e8f92013-12-11 17:16:25 +0000667void ARMTargetELFStreamer::emitArch(unsigned Value) {
668 Arch = Value;
669}
670void ARMTargetELFStreamer::emitArchDefaultAttributes() {
671 using namespace ARMBuildAttrs;
672 setAttributeItem(CPU_name, GetArchDefaultCPUName(Arch), false);
673 setAttributeItem(CPU_arch, GetArchDefaultCPUArch(Arch), false);
674
675 switch (Arch) {
676 case ARM::ARMV2:
677 case ARM::ARMV2A:
678 case ARM::ARMV3:
679 case ARM::ARMV3M:
680 case ARM::ARMV4:
681 case ARM::ARMV5:
682 setAttributeItem(ARM_ISA_use, Allowed, false);
683 break;
684
685 case ARM::ARMV4T:
686 case ARM::ARMV5T:
687 case ARM::ARMV5TE:
688 case ARM::ARMV6:
689 case ARM::ARMV6J:
690 setAttributeItem(ARM_ISA_use, Allowed, false);
691 setAttributeItem(THUMB_ISA_use, Allowed, false);
692 break;
693
694 case ARM::ARMV6T2:
695 setAttributeItem(ARM_ISA_use, Allowed, false);
696 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
697 break;
698
699 case ARM::ARMV6Z:
700 case ARM::ARMV6ZK:
701 setAttributeItem(ARM_ISA_use, Allowed, false);
702 setAttributeItem(THUMB_ISA_use, Allowed, false);
703 setAttributeItem(Virtualization_use, AllowTZ, false);
704 break;
705
706 case ARM::ARMV6M:
Logan Chien439e8f92013-12-11 17:16:25 +0000707 setAttributeItem(THUMB_ISA_use, Allowed, false);
708 break;
709
710 case ARM::ARMV7:
711 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
712 break;
713
714 case ARM::ARMV7A:
715 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
716 setAttributeItem(ARM_ISA_use, Allowed, false);
717 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
718 break;
719
720 case ARM::ARMV7R:
721 setAttributeItem(CPU_arch_profile, RealTimeProfile, false);
722 setAttributeItem(ARM_ISA_use, Allowed, false);
723 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
724 break;
725
726 case ARM::ARMV7M:
727 setAttributeItem(CPU_arch_profile, MicroControllerProfile, false);
728 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
729 break;
730
731 case ARM::ARMV8A:
732 setAttributeItem(CPU_arch_profile, ApplicationProfile, false);
733 setAttributeItem(ARM_ISA_use, Allowed, false);
734 setAttributeItem(THUMB_ISA_use, AllowThumb32, false);
735 setAttributeItem(MPextension_use, Allowed, false);
736 setAttributeItem(Virtualization_use, AllowTZVirtualization, false);
737 break;
738
739 case ARM::IWMMXT:
740 setAttributeItem(ARM_ISA_use, Allowed, false);
741 setAttributeItem(THUMB_ISA_use, Allowed, false);
742 setAttributeItem(WMMX_arch, AllowWMMXv1, false);
743 break;
744
745 case ARM::IWMMXT2:
746 setAttributeItem(ARM_ISA_use, Allowed, false);
747 setAttributeItem(THUMB_ISA_use, Allowed, false);
748 setAttributeItem(WMMX_arch, AllowWMMXv2, false);
749 break;
750
751 default:
752 report_fatal_error("Unknown Arch: " + Twine(Arch));
753 break;
754 }
755}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000756void ARMTargetELFStreamer::emitFPU(unsigned Value) {
757 FPU = Value;
758}
759void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
760 switch (FPU) {
761 case ARM::VFP:
762 case ARM::VFPV2:
Logan Chiena39510a2013-12-18 17:23:15 +0000763 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000764 ARMBuildAttrs::AllowFPv2,
765 /* OverwriteExisting= */ false);
766 break;
767
768 case ARM::VFPV3:
Logan Chiena39510a2013-12-18 17:23:15 +0000769 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000770 ARMBuildAttrs::AllowFPv3A,
771 /* OverwriteExisting= */ false);
772 break;
773
774 case ARM::VFPV3_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000775 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000776 ARMBuildAttrs::AllowFPv3B,
777 /* OverwriteExisting= */ false);
778 break;
779
780 case ARM::VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000781 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000782 ARMBuildAttrs::AllowFPv4A,
783 /* OverwriteExisting= */ false);
784 break;
785
786 case ARM::VFPV4_D16:
Logan Chiena39510a2013-12-18 17:23:15 +0000787 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000788 ARMBuildAttrs::AllowFPv4B,
789 /* OverwriteExisting= */ false);
790 break;
791
792 case ARM::FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000793 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000794 ARMBuildAttrs::AllowFPARMv8A,
795 /* OverwriteExisting= */ false);
796 break;
797
798 case ARM::NEON:
Logan Chiena39510a2013-12-18 17:23:15 +0000799 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000800 ARMBuildAttrs::AllowFPv3A,
801 /* OverwriteExisting= */ false);
802 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
803 ARMBuildAttrs::AllowNeon,
804 /* OverwriteExisting= */ false);
805 break;
806
807 case ARM::NEON_VFPV4:
Logan Chiena39510a2013-12-18 17:23:15 +0000808 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000809 ARMBuildAttrs::AllowFPv4A,
810 /* OverwriteExisting= */ false);
811 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
812 ARMBuildAttrs::AllowNeon2,
813 /* OverwriteExisting= */ false);
814 break;
815
816 case ARM::NEON_FP_ARMV8:
817 case ARM::CRYPTO_NEON_FP_ARMV8:
Logan Chiena39510a2013-12-18 17:23:15 +0000818 setAttributeItem(ARMBuildAttrs::FP_arch,
Logan Chien8cbb80d2013-10-28 17:51:12 +0000819 ARMBuildAttrs::AllowFPARMv8A,
820 /* OverwriteExisting= */ false);
821 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
822 ARMBuildAttrs::AllowNeonARMv8,
823 /* OverwriteExisting= */ false);
824 break;
825
Logan Chien05ae7442014-01-02 15:50:02 +0000826 case ARM::SOFTVFP:
827 break;
828
Logan Chien8cbb80d2013-10-28 17:51:12 +0000829 default:
830 report_fatal_error("Unknown FPU: " + Twine(FPU));
831 break;
832 }
833}
834size_t ARMTargetELFStreamer::calculateContentSize() const {
835 size_t Result = 0;
836 for (size_t i = 0; i < Contents.size(); ++i) {
837 AttributeItem item = Contents[i];
838 switch (item.Type) {
839 case AttributeItem::HiddenAttribute:
840 break;
841 case AttributeItem::NumericAttribute:
842 Result += getULEBSize(item.Tag);
843 Result += getULEBSize(item.IntValue);
844 break;
845 case AttributeItem::TextAttribute:
846 Result += getULEBSize(item.Tag);
847 Result += item.StringValue.size() + 1; // string + '\0'
848 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000849 case AttributeItem::NumericAndTextAttributes:
850 Result += getULEBSize(item.Tag);
851 Result += getULEBSize(item.IntValue);
852 Result += item.StringValue.size() + 1; // string + '\0';
853 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000854 }
855 }
856 return Result;
857}
858void ARMTargetELFStreamer::finishAttributeSection() {
859 // <format-version>
860 // [ <section-length> "vendor-name"
861 // [ <file-tag> <size> <attribute>*
862 // | <section-tag> <size> <section-number>* 0 <attribute>*
863 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
864 // ]+
865 // ]*
866
867 if (FPU != ARM::INVALID_FPU)
868 emitFPUDefaultAttributes();
869
Logan Chien439e8f92013-12-11 17:16:25 +0000870 if (Arch != ARM::INVALID_ARCH)
871 emitArchDefaultAttributes();
872
Logan Chien8cbb80d2013-10-28 17:51:12 +0000873 if (Contents.empty())
874 return;
875
876 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
877
878 ARMELFStreamer &Streamer = getStreamer();
879
880 // Switch to .ARM.attributes section
881 if (AttributeSection) {
882 Streamer.SwitchSection(AttributeSection);
883 } else {
884 AttributeSection =
885 Streamer.getContext().getELFSection(".ARM.attributes",
886 ELF::SHT_ARM_ATTRIBUTES,
887 0,
888 SectionKind::getMetadata());
889 Streamer.SwitchSection(AttributeSection);
890
891 // Format version
892 Streamer.EmitIntValue(0x41, 1);
893 }
894
895 // Vendor size + Vendor name + '\0'
896 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
897
898 // Tag + Tag Size
899 const size_t TagHeaderSize = 1 + 4;
900
901 const size_t ContentsSize = calculateContentSize();
902
903 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
904 Streamer.EmitBytes(CurrentVendor);
905 Streamer.EmitIntValue(0, 1); // '\0'
906
907 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
908 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
909
910 // Size should have been accounted for already, now
911 // emit each field as its type (ULEB or String)
912 for (size_t i = 0; i < Contents.size(); ++i) {
913 AttributeItem item = Contents[i];
914 Streamer.EmitULEB128IntValue(item.Tag);
915 switch (item.Type) {
916 default: llvm_unreachable("Invalid attribute type");
917 case AttributeItem::NumericAttribute:
918 Streamer.EmitULEB128IntValue(item.IntValue);
919 break;
920 case AttributeItem::TextAttribute:
921 Streamer.EmitBytes(item.StringValue.upper());
922 Streamer.EmitIntValue(0, 1); // '\0'
923 break;
Saleem Abdulrasool87ccd362014-01-07 02:28:42 +0000924 case AttributeItem::NumericAndTextAttributes:
925 Streamer.EmitULEB128IntValue(item.IntValue);
926 Streamer.EmitBytes(item.StringValue.upper());
927 Streamer.EmitIntValue(0, 1); // '\0'
928 break;
Logan Chien8cbb80d2013-10-28 17:51:12 +0000929 }
930 }
931
932 Contents.clear();
933 FPU = ARM::INVALID_FPU;
934}
Saleem Abdulrasoolc0da2cb2013-12-19 05:17:58 +0000935void ARMTargetELFStreamer::emitInst(uint32_t Inst, char Suffix) {
936 getStreamer().emitInst(Inst, Suffix);
937}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000938
939void ARMELFStreamer::FinishImpl() {
Rafael Espindola4a1a3602014-01-14 01:21:46 +0000940 MCTargetStreamer &TS = *getTargetStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000941 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
942 ATS.finishAttributeSection();
943
944 MCELFStreamer::FinishImpl();
945}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000946
Logan Chien2bcc42c2013-01-30 15:39:04 +0000947inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
948 unsigned Type,
949 unsigned Flags,
950 SectionKind Kind,
951 const MCSymbol &Fn) {
952 const MCSectionELF &FnSection =
953 static_cast<const MCSectionELF &>(Fn.getSection());
954
955 // Create the name for new section
956 StringRef FnSecName(FnSection.getSectionName());
957 SmallString<128> EHSecName(Prefix);
958 if (FnSecName != ".text") {
959 EHSecName += FnSecName;
960 }
961
962 // Get .ARM.extab or .ARM.exidx section
963 const MCSectionELF *EHSection = NULL;
964 if (const MCSymbol *Group = FnSection.getGroup()) {
965 EHSection = getContext().getELFSection(
966 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
967 FnSection.getEntrySize(), Group->getName());
968 } else {
969 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
970 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000971 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +0000972
973 // Switch to .ARM.extab or .ARM.exidx section
974 SwitchSection(EHSection);
975 EmitCodeAlignment(4, 0);
976}
977
978inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
979 SwitchToEHSection(".ARM.extab",
980 ELF::SHT_PROGBITS,
981 ELF::SHF_ALLOC,
982 SectionKind::getDataRel(),
983 FnStart);
984}
985
986inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
987 SwitchToEHSection(".ARM.exidx",
988 ELF::SHT_ARM_EXIDX,
989 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
990 SectionKind::getDataRel(),
991 FnStart);
992}
993
994void ARMELFStreamer::Reset() {
995 ExTab = NULL;
996 FnStart = NULL;
997 Personality = NULL;
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +0000998 PersonalityIndex = ARM::EHABI::NUM_PERSONALITY_INDEX;
Logan Chien325823a2013-06-09 12:22:30 +0000999 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001000 FPOffset = 0;
1001 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +00001002 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001003 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001004 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001005
Logan Chien325823a2013-06-09 12:22:30 +00001006 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001007 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +00001008}
1009
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001010void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001011 assert(FnStart == 0);
1012 FnStart = getContext().CreateTempSymbol();
1013 EmitLabel(FnStart);
1014}
1015
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001016void ARMELFStreamer::emitFnEnd() {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001017 assert(FnStart && ".fnstart must preceeds .fnend");
1018
1019 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +00001020 if (!ExTab && !CantUnwind)
1021 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001022
1023 // Emit the exception index table entry
1024 SwitchToExIdxSection(*FnStart);
1025
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001026 if (PersonalityIndex < ARM::EHABI::NUM_PERSONALITY_INDEX)
Logan Chiend8bb4b72013-04-16 12:02:21 +00001027 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001028
1029 const MCSymbolRefExpr *FnStartRef =
1030 MCSymbolRefExpr::Create(FnStart,
1031 MCSymbolRefExpr::VK_ARM_PREL31,
1032 getContext());
1033
Rafael Espindola64e1af82013-07-02 15:49:13 +00001034 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001035
1036 if (CantUnwind) {
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001037 EmitIntValue(ARM::EHABI::EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001038 } else if (ExTab) {
1039 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001040 const MCSymbolRefExpr *ExTabEntryRef =
1041 MCSymbolRefExpr::Create(ExTab,
1042 MCSymbolRefExpr::VK_ARM_PREL31,
1043 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +00001044 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +00001045 } else {
1046 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
1047 // the second word of exception index table entry. The size of the unwind
1048 // opcodes should always be 4 bytes.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001049 assert(PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0 &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001050 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +00001051 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001052 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +00001053 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001054 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001055 }
1056
Logan Chien4ea23b52013-05-10 16:17:24 +00001057 // Switch to the section containing FnStart
1058 SwitchSection(&FnStart->getSection());
1059
Logan Chien2bcc42c2013-01-30 15:39:04 +00001060 // Clean exception handling frame information
1061 Reset();
1062}
1063
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001064void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
1065
1066// Add the R_ARM_NONE fixup at the same position
1067void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
1068 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
1069
1070 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
1071 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
1072
1073 AddValueSymbols(PersonalityRef);
1074 MCDataFragment *DF = getOrCreateDataFragment();
1075 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
1076 PersonalityRef,
1077 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +00001078}
1079
Logan Chien325823a2013-06-09 12:22:30 +00001080void ARMELFStreamer::FlushPendingOffset() {
1081 if (PendingOffset != 0) {
1082 UnwindOpAsm.EmitSPOffset(-PendingOffset);
1083 PendingOffset = 0;
1084 }
1085}
1086
Logan Chienc931fce2013-07-02 12:43:27 +00001087void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +00001088 // Emit the unwind opcode to restore $sp.
1089 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001090 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +00001091 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
1092 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +00001093 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +00001094 } else {
1095 FlushPendingOffset();
1096 }
1097
1098 // Finalize the unwind opcode sequence
1099 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +00001100
1101 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
1102 // section. Thus, we don't have to create an entry in the .ARM.extab
1103 // section.
Saleem Abdulrasoolb961c992014-01-06 00:15:00 +00001104 if (NoHandlerData && PersonalityIndex == ARM::EHABI::AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +00001105 return;
1106
1107 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +00001108 SwitchToExTabSection(*FnStart);
1109
1110 // Create .ARM.extab label for offset in .ARM.exidx
1111 assert(!ExTab);
1112 ExTab = getContext().CreateTempSymbol();
1113 EmitLabel(ExTab);
1114
Logan Chien4ea23b52013-05-10 16:17:24 +00001115 // Emit personality
1116 if (Personality) {
1117 const MCSymbolRefExpr *PersonalityRef =
1118 MCSymbolRefExpr::Create(Personality,
1119 MCSymbolRefExpr::VK_ARM_PREL31,
1120 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +00001121
Rafael Espindola64e1af82013-07-02 15:49:13 +00001122 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +00001123 }
Logan Chien2bcc42c2013-01-30 15:39:04 +00001124
1125 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +00001126 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +00001127 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +00001128
1129 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
1130 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
1131 // after the unwind opcodes. The handler data consists of several 32-bit
1132 // words, and should be terminated by zero.
1133 //
1134 // In case that the .handlerdata directive is not specified by the
1135 // programmer, we should emit zero to terminate the handler data.
1136 if (NoHandlerData && !Personality)
1137 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001138}
1139
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001140void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +00001141
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001142void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +00001143 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +00001144 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001145}
1146
Saleem Abdulrasool662f5c12014-01-21 02:33:02 +00001147void ARMELFStreamer::emitPersonalityIndex(unsigned Index) {
1148 assert(Index < ARM::EHABI::NUM_PERSONALITY_INDEX && "invalid index");
1149 PersonalityIndex = Index;
1150}
1151
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001152void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001153 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001154 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +00001155 "the operand of .setfp directive should be either $sp or $fp");
1156
1157 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +00001158 FPReg = NewFPReg;
1159
1160 if (NewSPReg == ARM::SP)
1161 FPOffset = SPOffset + Offset;
1162 else
1163 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001164}
1165
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001166void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +00001167 // Track the change of the $sp offset
1168 SPOffset -= Offset;
1169
1170 // To squash multiple .pad directives, we should delay the unwind opcode
1171 // until the .save, .vsave, .handlerdata, or .fnend directives.
1172 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +00001173}
1174
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001175void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +00001176 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +00001177 // Collect the registers in the register list
1178 unsigned Count = 0;
1179 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +00001180 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +00001181 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +00001182 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +00001183 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +00001184 unsigned Bit = (1u << Reg);
1185 if ((Mask & Bit) == 0) {
1186 Mask |= Bit;
1187 ++Count;
1188 }
Logan Chiend8bb4b72013-04-16 12:02:21 +00001189 }
Logan Chien325823a2013-06-09 12:22:30 +00001190
1191 // Track the change the $sp offset: For the .save directive, the
1192 // corresponding push instruction will decrease the $sp by (4 * Count).
1193 // For the .vsave directive, the corresponding vpush instruction will
1194 // decrease $sp by (8 * Count).
1195 SPOffset -= Count * (IsVector ? 8 : 4);
1196
1197 // Emit the opcode
1198 FlushPendingOffset();
1199 if (IsVector)
1200 UnwindOpAsm.EmitVFPRegSave(Mask);
1201 else
1202 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +00001203}
1204
Tim Northover5cc3dc82012-12-07 16:50:23 +00001205namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001206
1207MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
1208 bool isVerboseAsm, bool useLoc, bool useCFI,
1209 bool useDwarfDirectory,
1210 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
1211 MCAsmBackend *TAB, bool ShowInst) {
Saleem Abdulrasoolf16e68a2014-01-07 02:28:50 +00001212 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint,
1213 isVerboseAsm);
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001214
1215 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
1216 useDwarfDirectory, InstPrint, CE, TAB,
1217 ShowInst);
1218}
1219
Tim Northover5cc3dc82012-12-07 16:50:23 +00001220 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
1221 raw_ostream &OS, MCCodeEmitter *Emitter,
1222 bool RelaxAll, bool NoExecStack,
1223 bool IsThumb) {
Rafael Espindolaa17151a2013-10-08 13:08:17 +00001224 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
1225 ARMELFStreamer *S =
1226 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +00001227 // FIXME: This should eventually end up somewhere else where more
1228 // intelligent flag decisions can be made. For now we are just maintaining
1229 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
1230 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
1231
Tim Northover5cc3dc82012-12-07 16:50:23 +00001232 if (RelaxAll)
1233 S->getAssembler().setRelaxAll(true);
1234 if (NoExecStack)
1235 S->getAssembler().setNoExecStack(true);
1236 return S;
1237 }
1238
1239}
1240
1241