blob: 471897de5c1cb2114ff9b523f4c5c21e7729eca7 [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 Chien8cbb80d2013-10-28 17:51:12 +000016#include "ARMBuildAttrs.h"
17#include "ARMFPUName.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000018#include "ARMRegisterInfo.h"
Logan Chien2bcc42c2013-01-30 15:39:04 +000019#include "ARMUnwindOp.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000020#include "ARMUnwindOpAsm.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000021#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000022#include "llvm/ADT/Twine.h"
23#include "llvm/MC/MCAsmBackend.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000024#include "llvm/MC/MCAssembler.h"
25#include "llvm/MC/MCCodeEmitter.h"
26#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000027#include "llvm/MC/MCELF.h"
28#include "llvm/MC/MCELFStreamer.h"
29#include "llvm/MC/MCELFSymbolFlags.h"
30#include "llvm/MC/MCExpr.h"
31#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000032#include "llvm/MC/MCInstPrinter.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000033#include "llvm/MC/MCObjectStreamer.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000034#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000035#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000036#include "llvm/MC/MCSectionELF.h"
37#include "llvm/MC/MCStreamer.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000038#include "llvm/MC/MCSymbol.h"
39#include "llvm/MC/MCValue.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000040#include "llvm/Support/Debug.h"
41#include "llvm/Support/ELF.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000042#include "llvm/Support/FormattedStream.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000043#include "llvm/Support/raw_ostream.h"
Logan Chien8cbb80d2013-10-28 17:51:12 +000044#include <algorithm>
Tim Northover5cc3dc82012-12-07 16:50:23 +000045
46using namespace llvm;
47
Logan Chiend8bb4b72013-04-16 12:02:21 +000048static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
49 assert(Index < NUM_PERSONALITY_INDEX && "Invalid personality index");
50 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
51}
52
Logan Chien8cbb80d2013-10-28 17:51:12 +000053static const char *GetFPUName(unsigned ID) {
54 switch (ID) {
55 default:
56 llvm_unreachable("Unknown FPU kind");
57 break;
58#define ARM_FPU_NAME(NAME, ID) case ARM::ID: return NAME;
59#include "ARMFPUName.def"
60 }
61 return NULL;
62}
63
Tim Northover5cc3dc82012-12-07 16:50:23 +000064namespace {
65
Rafael Espindolaa17151a2013-10-08 13:08:17 +000066class ARMELFStreamer;
67
68class ARMTargetAsmStreamer : public ARMTargetStreamer {
69 formatted_raw_ostream &OS;
70 MCInstPrinter &InstPrinter;
71
72 virtual void emitFnStart();
73 virtual void emitFnEnd();
74 virtual void emitCantUnwind();
75 virtual void emitPersonality(const MCSymbol *Personality);
76 virtual void emitHandlerData();
77 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
78 virtual void emitPad(int64_t Offset);
79 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
80 bool isVector);
81
Logan Chien8cbb80d2013-10-28 17:51:12 +000082 virtual void switchVendor(StringRef Vendor);
83 virtual void emitAttribute(unsigned Attribute, unsigned Value);
84 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
85 virtual void emitFPU(unsigned FPU);
86 virtual void finishAttributeSection();
87
Rafael Espindolaa17151a2013-10-08 13:08:17 +000088public:
89 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter);
90};
91
92ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
93 MCInstPrinter &InstPrinter)
94 : OS(OS), InstPrinter(InstPrinter) {}
95void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
96void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
97void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
98void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
99 OS << "\t.personality " << Personality->getName() << '\n';
100}
101void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
102void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
103 int64_t Offset) {
104 OS << "\t.setfp\t";
105 InstPrinter.printRegName(OS, FpReg);
106 OS << ", ";
107 InstPrinter.printRegName(OS, SpReg);
108 if (Offset)
109 OS << ", #" << Offset;
110 OS << '\n';
111}
112void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
113 OS << "\t.pad\t#" << Offset << '\n';
114}
115void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
116 bool isVector) {
117 assert(RegList.size() && "RegList should not be empty");
118 if (isVector)
119 OS << "\t.vsave\t{";
120 else
121 OS << "\t.save\t{";
122
123 InstPrinter.printRegName(OS, RegList[0]);
124
125 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
126 OS << ", ";
127 InstPrinter.printRegName(OS, RegList[i]);
128 }
129
130 OS << "}\n";
131}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000132void ARMTargetAsmStreamer::switchVendor(StringRef Vendor) {
133}
134void ARMTargetAsmStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
135 OS << "\t.eabi_attribute\t" << Attribute << ", " << Twine(Value) << "\n";
136}
137void ARMTargetAsmStreamer::emitTextAttribute(unsigned Attribute,
138 StringRef String) {
139 switch (Attribute) {
140 default: llvm_unreachable("Unsupported Text attribute in ASM Mode");
141 case ARMBuildAttrs::CPU_name:
142 OS << "\t.cpu\t" << String.lower() << "\n";
143 break;
144 }
145}
146void ARMTargetAsmStreamer::emitFPU(unsigned FPU) {
147 OS << "\t.fpu\t" << GetFPUName(FPU) << "\n";
148}
149void ARMTargetAsmStreamer::finishAttributeSection() {
150}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000151
152class ARMTargetELFStreamer : public ARMTargetStreamer {
Logan Chien8cbb80d2013-10-28 17:51:12 +0000153private:
154 // This structure holds all attributes, accounting for
155 // their string/numeric value, so we can later emmit them
156 // in declaration order, keeping all in the same vector
157 struct AttributeItem {
158 enum {
159 HiddenAttribute = 0,
160 NumericAttribute,
161 TextAttribute
162 } Type;
163 unsigned Tag;
164 unsigned IntValue;
165 StringRef StringValue;
166
167 static bool LessTag(const AttributeItem &LHS, const AttributeItem &RHS) {
168 return (LHS.Tag < RHS.Tag);
169 }
170 };
171
172 StringRef CurrentVendor;
173 unsigned FPU;
174 SmallVector<AttributeItem, 64> Contents;
175
176 const MCSection *AttributeSection;
177
178 // FIXME: this should be in a more generic place, but
179 // getULEBSize() is in MCAsmInfo and will be moved to MCDwarf
180 static size_t getULEBSize(int Value) {
181 size_t Size = 0;
182 do {
183 Value >>= 7;
184 Size += sizeof(int8_t); // Is this really necessary?
185 } while (Value);
186 return Size;
187 }
188
189 AttributeItem *getAttributeItem(unsigned Attribute) {
190 for (size_t i = 0; i < Contents.size(); ++i)
191 if (Contents[i].Tag == Attribute)
192 return &Contents[i];
193 return 0;
194 }
195
196 void setAttributeItem(unsigned Attribute, unsigned Value,
197 bool OverwriteExisting) {
198 // Look for existing attribute item
199 if (AttributeItem *Item = getAttributeItem(Attribute)) {
200 if (!OverwriteExisting)
201 return;
202 Item->IntValue = Value;
203 return;
204 }
205
206 // Create new attribute item
207 AttributeItem Item = {
208 AttributeItem::NumericAttribute,
209 Attribute,
210 Value,
211 StringRef("")
212 };
213 Contents.push_back(Item);
214 }
215
216 void setAttributeItem(unsigned Attribute, StringRef Value,
217 bool OverwriteExisting) {
218 // Look for existing attribute item
219 if (AttributeItem *Item = getAttributeItem(Attribute)) {
220 if (!OverwriteExisting)
221 return;
222 Item->StringValue = Value;
223 return;
224 }
225
226 // Create new attribute item
227 AttributeItem Item = {
228 AttributeItem::TextAttribute,
229 Attribute,
230 0,
231 Value
232 };
233 Contents.push_back(Item);
234 }
235
236 void emitFPUDefaultAttributes();
237
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000238 ARMELFStreamer &getStreamer();
Logan Chien8cbb80d2013-10-28 17:51:12 +0000239
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000240 virtual void emitFnStart();
241 virtual void emitFnEnd();
242 virtual void emitCantUnwind();
243 virtual void emitPersonality(const MCSymbol *Personality);
244 virtual void emitHandlerData();
245 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
246 virtual void emitPad(int64_t Offset);
247 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
248 bool isVector);
Logan Chien8cbb80d2013-10-28 17:51:12 +0000249
250 virtual void switchVendor(StringRef Vendor);
251 virtual void emitAttribute(unsigned Attribute, unsigned Value);
252 virtual void emitTextAttribute(unsigned Attribute, StringRef String);
253 virtual void emitFPU(unsigned FPU);
254 virtual void finishAttributeSection();
255
256 size_t calculateContentSize() const;
257
258public:
259 ARMTargetELFStreamer()
260 : ARMTargetStreamer(), CurrentVendor("aeabi"), FPU(ARM::INVALID_FPU),
261 AttributeSection(0) {
262 }
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000263};
264
Tim Northover5cc3dc82012-12-07 16:50:23 +0000265/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
266/// the appropriate points in the object files. These symbols are defined in the
267/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
268///
269/// In brief: $a, $t or $d should be emitted at the start of each contiguous
270/// region of ARM code, Thumb code or data in a section. In practice, this
271/// emission does not rely on explicit assembler directives but on inherent
272/// properties of the directives doing the emission (e.g. ".byte" is data, "add
273/// r0, r0, r0" an instruction).
274///
275/// As a result this system is orthogonal to the DataRegion infrastructure used
276/// by MachO. Beware!
277class ARMELFStreamer : public MCELFStreamer {
278public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000279 friend class ARMTargetELFStreamer;
280
281 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
282 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
283 bool IsThumb)
284 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
285 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000286 Reset();
287 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000288
289 ~ARMELFStreamer() {}
290
Logan Chien8cbb80d2013-10-28 17:51:12 +0000291 virtual void FinishImpl();
292
Logan Chien2bcc42c2013-01-30 15:39:04 +0000293 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000294 void emitFnStart();
295 void emitFnEnd();
296 void emitCantUnwind();
297 void emitPersonality(const MCSymbol *Per);
298 void emitHandlerData();
299 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
300 void emitPad(int64_t Offset);
301 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000302
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000303 virtual void ChangeSection(const MCSection *Section,
304 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000305 // We have to keep track of the mapping symbol state of any sections we
306 // use. Each one should start off as EMS_None, which is provided as the
307 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000308 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000309 LastEMS = LastMappingSymbols.lookup(Section);
310
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000311 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000312 }
313
314 /// This function is the one used to emit instruction data into the ELF
315 /// streamer. We override it to add the appropriate mapping symbol if
316 /// necessary.
317 virtual void EmitInstruction(const MCInst& Inst) {
318 if (IsThumb)
319 EmitThumbMappingSymbol();
320 else
321 EmitARMMappingSymbol();
322
323 MCELFStreamer::EmitInstruction(Inst);
324 }
325
326 /// This is one of the functions used to emit data into an ELF section, so the
327 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
328 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000329 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000330 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000331 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000332 }
333
334 /// This is one of the functions used to emit data into an ELF section, so the
335 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
336 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000337 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000338 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000339 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000340 }
341
342 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
343 MCELFStreamer::EmitAssemblerFlag(Flag);
344
345 switch (Flag) {
346 case MCAF_SyntaxUnified:
347 return; // no-op here.
348 case MCAF_Code16:
349 IsThumb = true;
350 return; // Change to Thumb mode
351 case MCAF_Code32:
352 IsThumb = false;
353 return; // Change to ARM mode
354 case MCAF_Code64:
355 return;
356 case MCAF_SubsectionsViaSymbols:
357 return;
358 }
359 }
360
361private:
362 enum ElfMappingSymbol {
363 EMS_None,
364 EMS_ARM,
365 EMS_Thumb,
366 EMS_Data
367 };
368
369 void EmitDataMappingSymbol() {
370 if (LastEMS == EMS_Data) return;
371 EmitMappingSymbol("$d");
372 LastEMS = EMS_Data;
373 }
374
375 void EmitThumbMappingSymbol() {
376 if (LastEMS == EMS_Thumb) return;
377 EmitMappingSymbol("$t");
378 LastEMS = EMS_Thumb;
379 }
380
381 void EmitARMMappingSymbol() {
382 if (LastEMS == EMS_ARM) return;
383 EmitMappingSymbol("$a");
384 LastEMS = EMS_ARM;
385 }
386
387 void EmitMappingSymbol(StringRef Name) {
388 MCSymbol *Start = getContext().CreateTempSymbol();
389 EmitLabel(Start);
390
Chandler Carruth1d94e932012-12-08 03:10:14 +0000391 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000392 getContext().GetOrCreateSymbol(Name + "." +
393 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000394
395 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
396 MCELF::SetType(SD, ELF::STT_NOTYPE);
397 MCELF::SetBinding(SD, ELF::STB_LOCAL);
398 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000399 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000400
401 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
402 Symbol->setVariableValue(Value);
403 }
404
405 void EmitThumbFunc(MCSymbol *Func) {
406 // FIXME: Anything needed here to flag the function as thumb?
407
408 getAssembler().setIsThumbFunc(Func);
409
410 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
411 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
412 }
413
Logan Chien2bcc42c2013-01-30 15:39:04 +0000414 // Helper functions for ARM exception handling directives
415 void Reset();
416
417 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000418 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000419 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000420
421 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
422 SectionKind Kind, const MCSymbol &Fn);
423 void SwitchToExTabSection(const MCSymbol &FnStart);
424 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000425
426 bool IsThumb;
427 int64_t MappingSymbolCounter;
428
429 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
430 ElfMappingSymbol LastEMS;
431
Logan Chien2bcc42c2013-01-30 15:39:04 +0000432 // ARM Exception Handling Frame Information
433 MCSymbol *ExTab;
434 MCSymbol *FnStart;
435 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000436 unsigned PersonalityIndex;
437 unsigned FPReg; // Frame pointer register
438 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
439 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
440 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000441 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000442 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000443 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000444 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000445};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000446} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000447
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000448ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
Benjamin Kramer41882932013-10-09 17:23:41 +0000449 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer);
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000450 return *S;
451}
452
453void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
454void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
455void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
456void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
457 getStreamer().emitPersonality(Personality);
458}
459void ARMTargetELFStreamer::emitHandlerData() {
460 getStreamer().emitHandlerData();
461}
462void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
463 int64_t Offset) {
464 getStreamer().emitSetFP(FpReg, SpReg, Offset);
465}
466void ARMTargetELFStreamer::emitPad(int64_t Offset) {
467 getStreamer().emitPad(Offset);
468}
469void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
470 bool isVector) {
471 getStreamer().emitRegSave(RegList, isVector);
472}
Logan Chien8cbb80d2013-10-28 17:51:12 +0000473void ARMTargetELFStreamer::switchVendor(StringRef Vendor) {
474 assert(!Vendor.empty() && "Vendor cannot be empty.");
475
476 if (CurrentVendor == Vendor)
477 return;
478
479 if (!CurrentVendor.empty())
480 finishAttributeSection();
481
482 assert(Contents.empty() &&
483 ".ARM.attributes should be flushed before changing vendor");
484 CurrentVendor = Vendor;
485
486}
487void ARMTargetELFStreamer::emitAttribute(unsigned Attribute, unsigned Value) {
488 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
489}
490void ARMTargetELFStreamer::emitTextAttribute(unsigned Attribute,
491 StringRef Value) {
492 setAttributeItem(Attribute, Value, /* OverwriteExisting= */ true);
493}
494void ARMTargetELFStreamer::emitFPU(unsigned Value) {
495 FPU = Value;
496}
497void ARMTargetELFStreamer::emitFPUDefaultAttributes() {
498 switch (FPU) {
499 case ARM::VFP:
500 case ARM::VFPV2:
501 setAttributeItem(ARMBuildAttrs::VFP_arch,
502 ARMBuildAttrs::AllowFPv2,
503 /* OverwriteExisting= */ false);
504 break;
505
506 case ARM::VFPV3:
507 setAttributeItem(ARMBuildAttrs::VFP_arch,
508 ARMBuildAttrs::AllowFPv3A,
509 /* OverwriteExisting= */ false);
510 break;
511
512 case ARM::VFPV3_D16:
513 setAttributeItem(ARMBuildAttrs::VFP_arch,
514 ARMBuildAttrs::AllowFPv3B,
515 /* OverwriteExisting= */ false);
516 break;
517
518 case ARM::VFPV4:
519 setAttributeItem(ARMBuildAttrs::VFP_arch,
520 ARMBuildAttrs::AllowFPv4A,
521 /* OverwriteExisting= */ false);
522 break;
523
524 case ARM::VFPV4_D16:
525 setAttributeItem(ARMBuildAttrs::VFP_arch,
526 ARMBuildAttrs::AllowFPv4B,
527 /* OverwriteExisting= */ false);
528 break;
529
530 case ARM::FP_ARMV8:
531 setAttributeItem(ARMBuildAttrs::VFP_arch,
532 ARMBuildAttrs::AllowFPARMv8A,
533 /* OverwriteExisting= */ false);
534 break;
535
536 case ARM::NEON:
537 setAttributeItem(ARMBuildAttrs::VFP_arch,
538 ARMBuildAttrs::AllowFPv3A,
539 /* OverwriteExisting= */ false);
540 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
541 ARMBuildAttrs::AllowNeon,
542 /* OverwriteExisting= */ false);
543 break;
544
545 case ARM::NEON_VFPV4:
546 setAttributeItem(ARMBuildAttrs::VFP_arch,
547 ARMBuildAttrs::AllowFPv4A,
548 /* OverwriteExisting= */ false);
549 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
550 ARMBuildAttrs::AllowNeon2,
551 /* OverwriteExisting= */ false);
552 break;
553
554 case ARM::NEON_FP_ARMV8:
555 case ARM::CRYPTO_NEON_FP_ARMV8:
556 setAttributeItem(ARMBuildAttrs::VFP_arch,
557 ARMBuildAttrs::AllowFPARMv8A,
558 /* OverwriteExisting= */ false);
559 setAttributeItem(ARMBuildAttrs::Advanced_SIMD_arch,
560 ARMBuildAttrs::AllowNeonARMv8,
561 /* OverwriteExisting= */ false);
562 break;
563
564 default:
565 report_fatal_error("Unknown FPU: " + Twine(FPU));
566 break;
567 }
568}
569size_t ARMTargetELFStreamer::calculateContentSize() const {
570 size_t Result = 0;
571 for (size_t i = 0; i < Contents.size(); ++i) {
572 AttributeItem item = Contents[i];
573 switch (item.Type) {
574 case AttributeItem::HiddenAttribute:
575 break;
576 case AttributeItem::NumericAttribute:
577 Result += getULEBSize(item.Tag);
578 Result += getULEBSize(item.IntValue);
579 break;
580 case AttributeItem::TextAttribute:
581 Result += getULEBSize(item.Tag);
582 Result += item.StringValue.size() + 1; // string + '\0'
583 break;
584 }
585 }
586 return Result;
587}
588void ARMTargetELFStreamer::finishAttributeSection() {
589 // <format-version>
590 // [ <section-length> "vendor-name"
591 // [ <file-tag> <size> <attribute>*
592 // | <section-tag> <size> <section-number>* 0 <attribute>*
593 // | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
594 // ]+
595 // ]*
596
597 if (FPU != ARM::INVALID_FPU)
598 emitFPUDefaultAttributes();
599
600 if (Contents.empty())
601 return;
602
603 std::sort(Contents.begin(), Contents.end(), AttributeItem::LessTag);
604
605 ARMELFStreamer &Streamer = getStreamer();
606
607 // Switch to .ARM.attributes section
608 if (AttributeSection) {
609 Streamer.SwitchSection(AttributeSection);
610 } else {
611 AttributeSection =
612 Streamer.getContext().getELFSection(".ARM.attributes",
613 ELF::SHT_ARM_ATTRIBUTES,
614 0,
615 SectionKind::getMetadata());
616 Streamer.SwitchSection(AttributeSection);
617
618 // Format version
619 Streamer.EmitIntValue(0x41, 1);
620 }
621
622 // Vendor size + Vendor name + '\0'
623 const size_t VendorHeaderSize = 4 + CurrentVendor.size() + 1;
624
625 // Tag + Tag Size
626 const size_t TagHeaderSize = 1 + 4;
627
628 const size_t ContentsSize = calculateContentSize();
629
630 Streamer.EmitIntValue(VendorHeaderSize + TagHeaderSize + ContentsSize, 4);
631 Streamer.EmitBytes(CurrentVendor);
632 Streamer.EmitIntValue(0, 1); // '\0'
633
634 Streamer.EmitIntValue(ARMBuildAttrs::File, 1);
635 Streamer.EmitIntValue(TagHeaderSize + ContentsSize, 4);
636
637 // Size should have been accounted for already, now
638 // emit each field as its type (ULEB or String)
639 for (size_t i = 0; i < Contents.size(); ++i) {
640 AttributeItem item = Contents[i];
641 Streamer.EmitULEB128IntValue(item.Tag);
642 switch (item.Type) {
643 default: llvm_unreachable("Invalid attribute type");
644 case AttributeItem::NumericAttribute:
645 Streamer.EmitULEB128IntValue(item.IntValue);
646 break;
647 case AttributeItem::TextAttribute:
648 Streamer.EmitBytes(item.StringValue.upper());
649 Streamer.EmitIntValue(0, 1); // '\0'
650 break;
651 }
652 }
653
654 Contents.clear();
655 FPU = ARM::INVALID_FPU;
656}
657
658void ARMELFStreamer::FinishImpl() {
659 MCTargetStreamer &TS = getTargetStreamer();
660 ARMTargetStreamer &ATS = static_cast<ARMTargetStreamer &>(TS);
661 ATS.finishAttributeSection();
662
663 MCELFStreamer::FinishImpl();
664}
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000665
Logan Chien2bcc42c2013-01-30 15:39:04 +0000666inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
667 unsigned Type,
668 unsigned Flags,
669 SectionKind Kind,
670 const MCSymbol &Fn) {
671 const MCSectionELF &FnSection =
672 static_cast<const MCSectionELF &>(Fn.getSection());
673
674 // Create the name for new section
675 StringRef FnSecName(FnSection.getSectionName());
676 SmallString<128> EHSecName(Prefix);
677 if (FnSecName != ".text") {
678 EHSecName += FnSecName;
679 }
680
681 // Get .ARM.extab or .ARM.exidx section
682 const MCSectionELF *EHSection = NULL;
683 if (const MCSymbol *Group = FnSection.getGroup()) {
684 EHSection = getContext().getELFSection(
685 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
686 FnSection.getEntrySize(), Group->getName());
687 } else {
688 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
689 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000690 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +0000691
692 // Switch to .ARM.extab or .ARM.exidx section
693 SwitchSection(EHSection);
694 EmitCodeAlignment(4, 0);
695}
696
697inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
698 SwitchToEHSection(".ARM.extab",
699 ELF::SHT_PROGBITS,
700 ELF::SHF_ALLOC,
701 SectionKind::getDataRel(),
702 FnStart);
703}
704
705inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
706 SwitchToEHSection(".ARM.exidx",
707 ELF::SHT_ARM_EXIDX,
708 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
709 SectionKind::getDataRel(),
710 FnStart);
711}
712
713void ARMELFStreamer::Reset() {
714 ExTab = NULL;
715 FnStart = NULL;
716 Personality = NULL;
Logan Chien325823a2013-06-09 12:22:30 +0000717 PersonalityIndex = NUM_PERSONALITY_INDEX;
718 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000719 FPOffset = 0;
720 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +0000721 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000722 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000723 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000724
Logan Chien325823a2013-06-09 12:22:30 +0000725 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000726 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +0000727}
728
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000729void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +0000730 assert(FnStart == 0);
731 FnStart = getContext().CreateTempSymbol();
732 EmitLabel(FnStart);
733}
734
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000735void ARMELFStreamer::emitFnEnd() {
Logan Chien2bcc42c2013-01-30 15:39:04 +0000736 assert(FnStart && ".fnstart must preceeds .fnend");
737
738 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +0000739 if (!ExTab && !CantUnwind)
740 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000741
742 // Emit the exception index table entry
743 SwitchToExIdxSection(*FnStart);
744
Logan Chiend8bb4b72013-04-16 12:02:21 +0000745 if (PersonalityIndex < NUM_PERSONALITY_INDEX)
746 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +0000747
748 const MCSymbolRefExpr *FnStartRef =
749 MCSymbolRefExpr::Create(FnStart,
750 MCSymbolRefExpr::VK_ARM_PREL31,
751 getContext());
752
Rafael Espindola64e1af82013-07-02 15:49:13 +0000753 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000754
755 if (CantUnwind) {
Rafael Espindola64e1af82013-07-02 15:49:13 +0000756 EmitIntValue(EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +0000757 } else if (ExTab) {
758 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +0000759 const MCSymbolRefExpr *ExTabEntryRef =
760 MCSymbolRefExpr::Create(ExTab,
761 MCSymbolRefExpr::VK_ARM_PREL31,
762 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +0000763 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +0000764 } else {
765 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
766 // the second word of exception index table entry. The size of the unwind
767 // opcodes should always be 4 bytes.
768 assert(PersonalityIndex == AEABI_UNWIND_CPP_PR0 &&
769 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +0000770 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +0000771 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +0000772 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +0000773 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +0000774 }
775
Logan Chien4ea23b52013-05-10 16:17:24 +0000776 // Switch to the section containing FnStart
777 SwitchSection(&FnStart->getSection());
778
Logan Chien2bcc42c2013-01-30 15:39:04 +0000779 // Clean exception handling frame information
780 Reset();
781}
782
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000783void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
784
785// Add the R_ARM_NONE fixup at the same position
786void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
787 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
788
789 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
790 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
791
792 AddValueSymbols(PersonalityRef);
793 MCDataFragment *DF = getOrCreateDataFragment();
794 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
795 PersonalityRef,
796 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +0000797}
798
Logan Chien325823a2013-06-09 12:22:30 +0000799void ARMELFStreamer::FlushPendingOffset() {
800 if (PendingOffset != 0) {
801 UnwindOpAsm.EmitSPOffset(-PendingOffset);
802 PendingOffset = 0;
803 }
804}
805
Logan Chienc931fce2013-07-02 12:43:27 +0000806void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +0000807 // Emit the unwind opcode to restore $sp.
808 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000809 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +0000810 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
811 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +0000812 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +0000813 } else {
814 FlushPendingOffset();
815 }
816
817 // Finalize the unwind opcode sequence
818 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +0000819
820 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
821 // section. Thus, we don't have to create an entry in the .ARM.extab
822 // section.
Logan Chienc931fce2013-07-02 12:43:27 +0000823 if (NoHandlerData && PersonalityIndex == AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +0000824 return;
825
826 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +0000827 SwitchToExTabSection(*FnStart);
828
829 // Create .ARM.extab label for offset in .ARM.exidx
830 assert(!ExTab);
831 ExTab = getContext().CreateTempSymbol();
832 EmitLabel(ExTab);
833
Logan Chien4ea23b52013-05-10 16:17:24 +0000834 // Emit personality
835 if (Personality) {
836 const MCSymbolRefExpr *PersonalityRef =
837 MCSymbolRefExpr::Create(Personality,
838 MCSymbolRefExpr::VK_ARM_PREL31,
839 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +0000840
Rafael Espindola64e1af82013-07-02 15:49:13 +0000841 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +0000842 }
Logan Chien2bcc42c2013-01-30 15:39:04 +0000843
844 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +0000845 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +0000846 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +0000847
848 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
849 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
850 // after the unwind opcodes. The handler data consists of several 32-bit
851 // words, and should be terminated by zero.
852 //
853 // In case that the .handlerdata directive is not specified by the
854 // programmer, we should emit zero to terminate the handler data.
855 if (NoHandlerData && !Personality)
856 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000857}
858
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000859void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +0000860
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000861void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +0000862 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000863 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000864}
865
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000866void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +0000867 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +0000868 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +0000869 "the operand of .setfp directive should be either $sp or $fp");
870
871 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +0000872 FPReg = NewFPReg;
873
874 if (NewSPReg == ARM::SP)
875 FPOffset = SPOffset + Offset;
876 else
877 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000878}
879
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000880void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +0000881 // Track the change of the $sp offset
882 SPOffset -= Offset;
883
884 // To squash multiple .pad directives, we should delay the unwind opcode
885 // until the .save, .vsave, .handlerdata, or .fnend directives.
886 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000887}
888
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000889void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +0000890 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +0000891 // Collect the registers in the register list
892 unsigned Count = 0;
893 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +0000894 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000895 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000896 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +0000897 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +0000898 unsigned Bit = (1u << Reg);
899 if ((Mask & Bit) == 0) {
900 Mask |= Bit;
901 ++Count;
902 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000903 }
Logan Chien325823a2013-06-09 12:22:30 +0000904
905 // Track the change the $sp offset: For the .save directive, the
906 // corresponding push instruction will decrease the $sp by (4 * Count).
907 // For the .vsave directive, the corresponding vpush instruction will
908 // decrease $sp by (8 * Count).
909 SPOffset -= Count * (IsVector ? 8 : 4);
910
911 // Emit the opcode
912 FlushPendingOffset();
913 if (IsVector)
914 UnwindOpAsm.EmitVFPRegSave(Mask);
915 else
916 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000917}
918
Tim Northover5cc3dc82012-12-07 16:50:23 +0000919namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000920
921MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
922 bool isVerboseAsm, bool useLoc, bool useCFI,
923 bool useDwarfDirectory,
924 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
925 MCAsmBackend *TAB, bool ShowInst) {
926 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint);
927
928 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
929 useDwarfDirectory, InstPrint, CE, TAB,
930 ShowInst);
931}
932
Tim Northover5cc3dc82012-12-07 16:50:23 +0000933 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
934 raw_ostream &OS, MCCodeEmitter *Emitter,
935 bool RelaxAll, bool NoExecStack,
936 bool IsThumb) {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000937 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
938 ARMELFStreamer *S =
939 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000940 // FIXME: This should eventually end up somewhere else where more
941 // intelligent flag decisions can be made. For now we are just maintaining
942 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
943 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
944
Tim Northover5cc3dc82012-12-07 16:50:23 +0000945 if (RelaxAll)
946 S->getAssembler().setRelaxAll(true);
947 if (NoExecStack)
948 S->getAssembler().setNoExecStack(true);
949 return S;
950 }
951
952}
953
954