blob: 867782b791b4ee5c1b16f68bfc75ccbf2d6c8840 [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 Chiend8bb4b72013-04-16 12:02:21 +000016#include "ARMRegisterInfo.h"
Logan Chien2bcc42c2013-01-30 15:39:04 +000017#include "ARMUnwindOp.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000018#include "ARMUnwindOpAsm.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000019#include "llvm/ADT/SmallPtrSet.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000020#include "llvm/ADT/Twine.h"
21#include "llvm/MC/MCAsmBackend.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000022#include "llvm/MC/MCAssembler.h"
23#include "llvm/MC/MCCodeEmitter.h"
24#include "llvm/MC/MCContext.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000025#include "llvm/MC/MCELF.h"
26#include "llvm/MC/MCELFStreamer.h"
27#include "llvm/MC/MCELFSymbolFlags.h"
28#include "llvm/MC/MCExpr.h"
29#include "llvm/MC/MCInst.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000030#include "llvm/MC/MCInstPrinter.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000031#include "llvm/MC/MCObjectStreamer.h"
Logan Chiend8bb4b72013-04-16 12:02:21 +000032#include "llvm/MC/MCRegisterInfo.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000033#include "llvm/MC/MCSection.h"
Benjamin Kramerf242d8c2012-12-08 10:45:24 +000034#include "llvm/MC/MCSectionELF.h"
35#include "llvm/MC/MCStreamer.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000036#include "llvm/MC/MCSymbol.h"
37#include "llvm/MC/MCValue.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000038#include "llvm/Support/Debug.h"
39#include "llvm/Support/ELF.h"
Rafael Espindolaa17151a2013-10-08 13:08:17 +000040#include "llvm/Support/FormattedStream.h"
Tim Northover5cc3dc82012-12-07 16:50:23 +000041#include "llvm/Support/raw_ostream.h"
42
43using namespace llvm;
44
Logan Chiend8bb4b72013-04-16 12:02:21 +000045static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
46 assert(Index < NUM_PERSONALITY_INDEX && "Invalid personality index");
47 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
48}
49
Tim Northover5cc3dc82012-12-07 16:50:23 +000050namespace {
51
Rafael Espindolaa17151a2013-10-08 13:08:17 +000052class ARMELFStreamer;
53
54class ARMTargetAsmStreamer : public ARMTargetStreamer {
55 formatted_raw_ostream &OS;
56 MCInstPrinter &InstPrinter;
57
58 virtual void emitFnStart();
59 virtual void emitFnEnd();
60 virtual void emitCantUnwind();
61 virtual void emitPersonality(const MCSymbol *Personality);
62 virtual void emitHandlerData();
63 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
64 virtual void emitPad(int64_t Offset);
65 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
66 bool isVector);
67
68public:
69 ARMTargetAsmStreamer(formatted_raw_ostream &OS, MCInstPrinter &InstPrinter);
70};
71
72ARMTargetAsmStreamer::ARMTargetAsmStreamer(formatted_raw_ostream &OS,
73 MCInstPrinter &InstPrinter)
74 : OS(OS), InstPrinter(InstPrinter) {}
75void ARMTargetAsmStreamer::emitFnStart() { OS << "\t.fnstart\n"; }
76void ARMTargetAsmStreamer::emitFnEnd() { OS << "\t.fnend\n"; }
77void ARMTargetAsmStreamer::emitCantUnwind() { OS << "\t.cantunwind\n"; }
78void ARMTargetAsmStreamer::emitPersonality(const MCSymbol *Personality) {
79 OS << "\t.personality " << Personality->getName() << '\n';
80}
81void ARMTargetAsmStreamer::emitHandlerData() { OS << "\t.handlerdata\n"; }
82void ARMTargetAsmStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
83 int64_t Offset) {
84 OS << "\t.setfp\t";
85 InstPrinter.printRegName(OS, FpReg);
86 OS << ", ";
87 InstPrinter.printRegName(OS, SpReg);
88 if (Offset)
89 OS << ", #" << Offset;
90 OS << '\n';
91}
92void ARMTargetAsmStreamer::emitPad(int64_t Offset) {
93 OS << "\t.pad\t#" << Offset << '\n';
94}
95void ARMTargetAsmStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
96 bool isVector) {
97 assert(RegList.size() && "RegList should not be empty");
98 if (isVector)
99 OS << "\t.vsave\t{";
100 else
101 OS << "\t.save\t{";
102
103 InstPrinter.printRegName(OS, RegList[0]);
104
105 for (unsigned i = 1, e = RegList.size(); i != e; ++i) {
106 OS << ", ";
107 InstPrinter.printRegName(OS, RegList[i]);
108 }
109
110 OS << "}\n";
111}
112
113class ARMTargetELFStreamer : public ARMTargetStreamer {
114 ARMELFStreamer &getStreamer();
115 virtual void emitFnStart();
116 virtual void emitFnEnd();
117 virtual void emitCantUnwind();
118 virtual void emitPersonality(const MCSymbol *Personality);
119 virtual void emitHandlerData();
120 virtual void emitSetFP(unsigned FpReg, unsigned SpReg, int64_t Offset = 0);
121 virtual void emitPad(int64_t Offset);
122 virtual void emitRegSave(const SmallVectorImpl<unsigned> &RegList,
123 bool isVector);
124};
125
Tim Northover5cc3dc82012-12-07 16:50:23 +0000126/// Extend the generic ELFStreamer class so that it can emit mapping symbols at
127/// the appropriate points in the object files. These symbols are defined in the
128/// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
129///
130/// In brief: $a, $t or $d should be emitted at the start of each contiguous
131/// region of ARM code, Thumb code or data in a section. In practice, this
132/// emission does not rely on explicit assembler directives but on inherent
133/// properties of the directives doing the emission (e.g. ".byte" is data, "add
134/// r0, r0, r0" an instruction).
135///
136/// As a result this system is orthogonal to the DataRegion infrastructure used
137/// by MachO. Beware!
138class ARMELFStreamer : public MCELFStreamer {
139public:
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000140 friend class ARMTargetELFStreamer;
141
142 ARMELFStreamer(MCContext &Context, MCTargetStreamer *TargetStreamer,
143 MCAsmBackend &TAB, raw_ostream &OS, MCCodeEmitter *Emitter,
144 bool IsThumb)
145 : MCELFStreamer(Context, TargetStreamer, TAB, OS, Emitter),
146 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
Logan Chiend8bb4b72013-04-16 12:02:21 +0000147 Reset();
148 }
Tim Northover5cc3dc82012-12-07 16:50:23 +0000149
150 ~ARMELFStreamer() {}
151
Logan Chien2bcc42c2013-01-30 15:39:04 +0000152 // ARM exception handling directives
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000153 void emitFnStart();
154 void emitFnEnd();
155 void emitCantUnwind();
156 void emitPersonality(const MCSymbol *Per);
157 void emitHandlerData();
158 void emitSetFP(unsigned NewFpReg, unsigned NewSpReg, int64_t Offset = 0);
159 void emitPad(int64_t Offset);
160 void emitRegSave(const SmallVectorImpl<unsigned> &RegList, bool isVector);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000161
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000162 virtual void ChangeSection(const MCSection *Section,
163 const MCExpr *Subsection) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000164 // We have to keep track of the mapping symbol state of any sections we
165 // use. Each one should start off as EMS_None, which is provided as the
166 // default constructor by DenseMap::lookup.
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000167 LastMappingSymbols[getPreviousSection().first] = LastEMS;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000168 LastEMS = LastMappingSymbols.lookup(Section);
169
Peter Collingbourne2f495b92013-04-17 21:18:16 +0000170 MCELFStreamer::ChangeSection(Section, Subsection);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000171 }
172
173 /// This function is the one used to emit instruction data into the ELF
174 /// streamer. We override it to add the appropriate mapping symbol if
175 /// necessary.
176 virtual void EmitInstruction(const MCInst& Inst) {
177 if (IsThumb)
178 EmitThumbMappingSymbol();
179 else
180 EmitARMMappingSymbol();
181
182 MCELFStreamer::EmitInstruction(Inst);
183 }
184
185 /// This is one of the functions used to emit data into an ELF section, so the
186 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
187 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000188 virtual void EmitBytes(StringRef Data) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000189 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000190 MCELFStreamer::EmitBytes(Data);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000191 }
192
193 /// This is one of the functions used to emit data into an ELF section, so the
194 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
195 /// necessary.
Rafael Espindola64e1af82013-07-02 15:49:13 +0000196 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size) {
Tim Northover5cc3dc82012-12-07 16:50:23 +0000197 EmitDataMappingSymbol();
Rafael Espindola64e1af82013-07-02 15:49:13 +0000198 MCELFStreamer::EmitValueImpl(Value, Size);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000199 }
200
201 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
202 MCELFStreamer::EmitAssemblerFlag(Flag);
203
204 switch (Flag) {
205 case MCAF_SyntaxUnified:
206 return; // no-op here.
207 case MCAF_Code16:
208 IsThumb = true;
209 return; // Change to Thumb mode
210 case MCAF_Code32:
211 IsThumb = false;
212 return; // Change to ARM mode
213 case MCAF_Code64:
214 return;
215 case MCAF_SubsectionsViaSymbols:
216 return;
217 }
218 }
219
220private:
221 enum ElfMappingSymbol {
222 EMS_None,
223 EMS_ARM,
224 EMS_Thumb,
225 EMS_Data
226 };
227
228 void EmitDataMappingSymbol() {
229 if (LastEMS == EMS_Data) return;
230 EmitMappingSymbol("$d");
231 LastEMS = EMS_Data;
232 }
233
234 void EmitThumbMappingSymbol() {
235 if (LastEMS == EMS_Thumb) return;
236 EmitMappingSymbol("$t");
237 LastEMS = EMS_Thumb;
238 }
239
240 void EmitARMMappingSymbol() {
241 if (LastEMS == EMS_ARM) return;
242 EmitMappingSymbol("$a");
243 LastEMS = EMS_ARM;
244 }
245
246 void EmitMappingSymbol(StringRef Name) {
247 MCSymbol *Start = getContext().CreateTempSymbol();
248 EmitLabel(Start);
249
Chandler Carruth1d94e932012-12-08 03:10:14 +0000250 MCSymbol *Symbol =
Benjamin Kramerf242d8c2012-12-08 10:45:24 +0000251 getContext().GetOrCreateSymbol(Name + "." +
252 Twine(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000253
254 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
255 MCELF::SetType(SD, ELF::STT_NOTYPE);
256 MCELF::SetBinding(SD, ELF::STB_LOCAL);
257 SD.setExternal(false);
Richard Mitton21101b32013-09-19 23:21:01 +0000258 AssignSection(Symbol, getCurrentSection().first);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000259
260 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
261 Symbol->setVariableValue(Value);
262 }
263
264 void EmitThumbFunc(MCSymbol *Func) {
265 // FIXME: Anything needed here to flag the function as thumb?
266
267 getAssembler().setIsThumbFunc(Func);
268
269 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
270 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
271 }
272
Logan Chien2bcc42c2013-01-30 15:39:04 +0000273 // Helper functions for ARM exception handling directives
274 void Reset();
275
276 void EmitPersonalityFixup(StringRef Name);
Logan Chien325823a2013-06-09 12:22:30 +0000277 void FlushPendingOffset();
Logan Chienc931fce2013-07-02 12:43:27 +0000278 void FlushUnwindOpcodes(bool NoHandlerData);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000279
280 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
281 SectionKind Kind, const MCSymbol &Fn);
282 void SwitchToExTabSection(const MCSymbol &FnStart);
283 void SwitchToExIdxSection(const MCSymbol &FnStart);
Tim Northover5cc3dc82012-12-07 16:50:23 +0000284
285 bool IsThumb;
286 int64_t MappingSymbolCounter;
287
288 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
289 ElfMappingSymbol LastEMS;
290
Logan Chien2bcc42c2013-01-30 15:39:04 +0000291 // ARM Exception Handling Frame Information
292 MCSymbol *ExTab;
293 MCSymbol *FnStart;
294 const MCSymbol *Personality;
Logan Chien325823a2013-06-09 12:22:30 +0000295 unsigned PersonalityIndex;
296 unsigned FPReg; // Frame pointer register
297 int64_t FPOffset; // Offset: (final frame pointer) - (initial $sp)
298 int64_t SPOffset; // Offset: (final $sp) - (initial $sp)
299 int64_t PendingOffset; // Offset: (final $sp) - (emitted $sp)
Logan Chiend8bb4b72013-04-16 12:02:21 +0000300 bool UsedFP;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000301 bool CantUnwind;
Logan Chien325823a2013-06-09 12:22:30 +0000302 SmallVector<uint8_t, 64> Opcodes;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000303 UnwindOpcodeAssembler UnwindOpAsm;
Tim Northover5cc3dc82012-12-07 16:50:23 +0000304};
Logan Chiend8bb4b72013-04-16 12:02:21 +0000305} // end anonymous namespace
Tim Northover5cc3dc82012-12-07 16:50:23 +0000306
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000307ARMELFStreamer &ARMTargetELFStreamer::getStreamer() {
308 ARMELFStreamer *S = static_cast<ARMELFStreamer *>(Streamer.get());
309 return *S;
310}
311
312void ARMTargetELFStreamer::emitFnStart() { getStreamer().emitFnStart(); }
313void ARMTargetELFStreamer::emitFnEnd() { getStreamer().emitFnEnd(); }
314void ARMTargetELFStreamer::emitCantUnwind() { getStreamer().emitCantUnwind(); }
315void ARMTargetELFStreamer::emitPersonality(const MCSymbol *Personality) {
316 getStreamer().emitPersonality(Personality);
317}
318void ARMTargetELFStreamer::emitHandlerData() {
319 getStreamer().emitHandlerData();
320}
321void ARMTargetELFStreamer::emitSetFP(unsigned FpReg, unsigned SpReg,
322 int64_t Offset) {
323 getStreamer().emitSetFP(FpReg, SpReg, Offset);
324}
325void ARMTargetELFStreamer::emitPad(int64_t Offset) {
326 getStreamer().emitPad(Offset);
327}
328void ARMTargetELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
329 bool isVector) {
330 getStreamer().emitRegSave(RegList, isVector);
331}
332
Logan Chien2bcc42c2013-01-30 15:39:04 +0000333inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
334 unsigned Type,
335 unsigned Flags,
336 SectionKind Kind,
337 const MCSymbol &Fn) {
338 const MCSectionELF &FnSection =
339 static_cast<const MCSectionELF &>(Fn.getSection());
340
341 // Create the name for new section
342 StringRef FnSecName(FnSection.getSectionName());
343 SmallString<128> EHSecName(Prefix);
344 if (FnSecName != ".text") {
345 EHSecName += FnSecName;
346 }
347
348 // Get .ARM.extab or .ARM.exidx section
349 const MCSectionELF *EHSection = NULL;
350 if (const MCSymbol *Group = FnSection.getGroup()) {
351 EHSection = getContext().getELFSection(
352 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
353 FnSection.getEntrySize(), Group->getName());
354 } else {
355 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
356 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000357 assert(EHSection && "Failed to get the required EH section");
Logan Chien2bcc42c2013-01-30 15:39:04 +0000358
359 // Switch to .ARM.extab or .ARM.exidx section
360 SwitchSection(EHSection);
361 EmitCodeAlignment(4, 0);
362}
363
364inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
365 SwitchToEHSection(".ARM.extab",
366 ELF::SHT_PROGBITS,
367 ELF::SHF_ALLOC,
368 SectionKind::getDataRel(),
369 FnStart);
370}
371
372inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
373 SwitchToEHSection(".ARM.exidx",
374 ELF::SHT_ARM_EXIDX,
375 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
376 SectionKind::getDataRel(),
377 FnStart);
378}
379
380void ARMELFStreamer::Reset() {
381 ExTab = NULL;
382 FnStart = NULL;
383 Personality = NULL;
Logan Chien325823a2013-06-09 12:22:30 +0000384 PersonalityIndex = NUM_PERSONALITY_INDEX;
385 FPReg = ARM::SP;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000386 FPOffset = 0;
387 SPOffset = 0;
Logan Chien325823a2013-06-09 12:22:30 +0000388 PendingOffset = 0;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000389 UsedFP = false;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000390 CantUnwind = false;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000391
Logan Chien325823a2013-06-09 12:22:30 +0000392 Opcodes.clear();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000393 UnwindOpAsm.Reset();
Logan Chien2bcc42c2013-01-30 15:39:04 +0000394}
395
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000396void ARMELFStreamer::emitFnStart() {
Logan Chien2bcc42c2013-01-30 15:39:04 +0000397 assert(FnStart == 0);
398 FnStart = getContext().CreateTempSymbol();
399 EmitLabel(FnStart);
400}
401
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000402void ARMELFStreamer::emitFnEnd() {
Logan Chien2bcc42c2013-01-30 15:39:04 +0000403 assert(FnStart && ".fnstart must preceeds .fnend");
404
405 // Emit unwind opcodes if there is no .handlerdata directive
Logan Chien4ea23b52013-05-10 16:17:24 +0000406 if (!ExTab && !CantUnwind)
407 FlushUnwindOpcodes(true);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000408
409 // Emit the exception index table entry
410 SwitchToExIdxSection(*FnStart);
411
Logan Chiend8bb4b72013-04-16 12:02:21 +0000412 if (PersonalityIndex < NUM_PERSONALITY_INDEX)
413 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
Logan Chien2bcc42c2013-01-30 15:39:04 +0000414
415 const MCSymbolRefExpr *FnStartRef =
416 MCSymbolRefExpr::Create(FnStart,
417 MCSymbolRefExpr::VK_ARM_PREL31,
418 getContext());
419
Rafael Espindola64e1af82013-07-02 15:49:13 +0000420 EmitValue(FnStartRef, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000421
422 if (CantUnwind) {
Rafael Espindola64e1af82013-07-02 15:49:13 +0000423 EmitIntValue(EXIDX_CANTUNWIND, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +0000424 } else if (ExTab) {
425 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
Logan Chien2bcc42c2013-01-30 15:39:04 +0000426 const MCSymbolRefExpr *ExTabEntryRef =
427 MCSymbolRefExpr::Create(ExTab,
428 MCSymbolRefExpr::VK_ARM_PREL31,
429 getContext());
Rafael Espindola64e1af82013-07-02 15:49:13 +0000430 EmitValue(ExTabEntryRef, 4);
Logan Chiend8bb4b72013-04-16 12:02:21 +0000431 } else {
432 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
433 // the second word of exception index table entry. The size of the unwind
434 // opcodes should always be 4 bytes.
435 assert(PersonalityIndex == AEABI_UNWIND_CPP_PR0 &&
436 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
Logan Chien325823a2013-06-09 12:22:30 +0000437 assert(Opcodes.size() == 4u &&
Logan Chiend8bb4b72013-04-16 12:02:21 +0000438 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
Logan Chien325823a2013-06-09 12:22:30 +0000439 EmitBytes(StringRef(reinterpret_cast<const char*>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +0000440 Opcodes.size()));
Logan Chien2bcc42c2013-01-30 15:39:04 +0000441 }
442
Logan Chien4ea23b52013-05-10 16:17:24 +0000443 // Switch to the section containing FnStart
444 SwitchSection(&FnStart->getSection());
445
Logan Chien2bcc42c2013-01-30 15:39:04 +0000446 // Clean exception handling frame information
447 Reset();
448}
449
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000450void ARMELFStreamer::emitCantUnwind() { CantUnwind = true; }
451
452// Add the R_ARM_NONE fixup at the same position
453void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
454 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
455
456 const MCSymbolRefExpr *PersonalityRef = MCSymbolRefExpr::Create(
457 PersonalitySym, MCSymbolRefExpr::VK_ARM_NONE, getContext());
458
459 AddValueSymbols(PersonalityRef);
460 MCDataFragment *DF = getOrCreateDataFragment();
461 DF->getFixups().push_back(MCFixup::Create(DF->getContents().size(),
462 PersonalityRef,
463 MCFixup::getKindForSize(4, false)));
Logan Chien2bcc42c2013-01-30 15:39:04 +0000464}
465
Logan Chien325823a2013-06-09 12:22:30 +0000466void ARMELFStreamer::FlushPendingOffset() {
467 if (PendingOffset != 0) {
468 UnwindOpAsm.EmitSPOffset(-PendingOffset);
469 PendingOffset = 0;
470 }
471}
472
Logan Chienc931fce2013-07-02 12:43:27 +0000473void ARMELFStreamer::FlushUnwindOpcodes(bool NoHandlerData) {
Logan Chien325823a2013-06-09 12:22:30 +0000474 // Emit the unwind opcode to restore $sp.
475 if (UsedFP) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000476 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chien325823a2013-06-09 12:22:30 +0000477 int64_t LastRegSaveSPOffset = SPOffset - PendingOffset;
478 UnwindOpAsm.EmitSPOffset(LastRegSaveSPOffset - FPOffset);
Bill Wendlingbc07a892013-06-18 07:20:20 +0000479 UnwindOpAsm.EmitSetSP(MRI->getEncodingValue(FPReg));
Logan Chien325823a2013-06-09 12:22:30 +0000480 } else {
481 FlushPendingOffset();
482 }
483
484 // Finalize the unwind opcode sequence
485 UnwindOpAsm.Finalize(PersonalityIndex, Opcodes);
Logan Chien4ea23b52013-05-10 16:17:24 +0000486
487 // For compact model 0, we have to emit the unwind opcodes in the .ARM.exidx
488 // section. Thus, we don't have to create an entry in the .ARM.extab
489 // section.
Logan Chienc931fce2013-07-02 12:43:27 +0000490 if (NoHandlerData && PersonalityIndex == AEABI_UNWIND_CPP_PR0)
Logan Chien4ea23b52013-05-10 16:17:24 +0000491 return;
492
493 // Switch to .ARM.extab section.
Logan Chien2bcc42c2013-01-30 15:39:04 +0000494 SwitchToExTabSection(*FnStart);
495
496 // Create .ARM.extab label for offset in .ARM.exidx
497 assert(!ExTab);
498 ExTab = getContext().CreateTempSymbol();
499 EmitLabel(ExTab);
500
Logan Chien4ea23b52013-05-10 16:17:24 +0000501 // Emit personality
502 if (Personality) {
503 const MCSymbolRefExpr *PersonalityRef =
504 MCSymbolRefExpr::Create(Personality,
505 MCSymbolRefExpr::VK_ARM_PREL31,
506 getContext());
Logan Chien2bcc42c2013-01-30 15:39:04 +0000507
Rafael Espindola64e1af82013-07-02 15:49:13 +0000508 EmitValue(PersonalityRef, 4);
Logan Chien4ea23b52013-05-10 16:17:24 +0000509 }
Logan Chien2bcc42c2013-01-30 15:39:04 +0000510
511 // Emit unwind opcodes
Logan Chien325823a2013-06-09 12:22:30 +0000512 EmitBytes(StringRef(reinterpret_cast<const char *>(Opcodes.data()),
Rafael Espindola64e1af82013-07-02 15:49:13 +0000513 Opcodes.size()));
Logan Chienc931fce2013-07-02 12:43:27 +0000514
515 // According to ARM EHABI section 9.2, if the __aeabi_unwind_cpp_pr1() or
516 // __aeabi_unwind_cpp_pr2() is used, then the handler data must be emitted
517 // after the unwind opcodes. The handler data consists of several 32-bit
518 // words, and should be terminated by zero.
519 //
520 // In case that the .handlerdata directive is not specified by the
521 // programmer, we should emit zero to terminate the handler data.
522 if (NoHandlerData && !Personality)
523 EmitIntValue(0, 4);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000524}
525
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000526void ARMELFStreamer::emitHandlerData() { FlushUnwindOpcodes(false); }
Logan Chien4ea23b52013-05-10 16:17:24 +0000527
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000528void ARMELFStreamer::emitPersonality(const MCSymbol *Per) {
Logan Chien2bcc42c2013-01-30 15:39:04 +0000529 Personality = Per;
Logan Chiend8bb4b72013-04-16 12:02:21 +0000530 UnwindOpAsm.setPersonality(Per);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000531}
532
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000533void ARMELFStreamer::emitSetFP(unsigned NewFPReg, unsigned NewSPReg,
Logan Chien2bcc42c2013-01-30 15:39:04 +0000534 int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +0000535 assert((NewSPReg == ARM::SP || NewSPReg == FPReg) &&
Logan Chiend8bb4b72013-04-16 12:02:21 +0000536 "the operand of .setfp directive should be either $sp or $fp");
537
538 UsedFP = true;
Logan Chien325823a2013-06-09 12:22:30 +0000539 FPReg = NewFPReg;
540
541 if (NewSPReg == ARM::SP)
542 FPOffset = SPOffset + Offset;
543 else
544 FPOffset += Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000545}
546
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000547void ARMELFStreamer::emitPad(int64_t Offset) {
Logan Chien325823a2013-06-09 12:22:30 +0000548 // Track the change of the $sp offset
549 SPOffset -= Offset;
550
551 // To squash multiple .pad directives, we should delay the unwind opcode
552 // until the .save, .vsave, .handlerdata, or .fnend directives.
553 PendingOffset -= Offset;
Logan Chien2bcc42c2013-01-30 15:39:04 +0000554}
555
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000556void ARMELFStreamer::emitRegSave(const SmallVectorImpl<unsigned> &RegList,
Logan Chien2bcc42c2013-01-30 15:39:04 +0000557 bool IsVector) {
Logan Chien325823a2013-06-09 12:22:30 +0000558 // Collect the registers in the register list
559 unsigned Count = 0;
560 uint32_t Mask = 0;
Bill Wendlingbc07a892013-06-18 07:20:20 +0000561 const MCRegisterInfo *MRI = getContext().getRegisterInfo();
Logan Chiend8bb4b72013-04-16 12:02:21 +0000562 for (size_t i = 0; i < RegList.size(); ++i) {
Bill Wendlingbc07a892013-06-18 07:20:20 +0000563 unsigned Reg = MRI->getEncodingValue(RegList[i]);
Aaron Ballmanab1d27e2013-06-10 16:45:40 +0000564 assert(Reg < (IsVector ? 32U : 16U) && "Register out of range");
Logan Chien325823a2013-06-09 12:22:30 +0000565 unsigned Bit = (1u << Reg);
566 if ((Mask & Bit) == 0) {
567 Mask |= Bit;
568 ++Count;
569 }
Logan Chiend8bb4b72013-04-16 12:02:21 +0000570 }
Logan Chien325823a2013-06-09 12:22:30 +0000571
572 // Track the change the $sp offset: For the .save directive, the
573 // corresponding push instruction will decrease the $sp by (4 * Count).
574 // For the .vsave directive, the corresponding vpush instruction will
575 // decrease $sp by (8 * Count).
576 SPOffset -= Count * (IsVector ? 8 : 4);
577
578 // Emit the opcode
579 FlushPendingOffset();
580 if (IsVector)
581 UnwindOpAsm.EmitVFPRegSave(Mask);
582 else
583 UnwindOpAsm.EmitRegSave(Mask);
Logan Chien2bcc42c2013-01-30 15:39:04 +0000584}
585
Tim Northover5cc3dc82012-12-07 16:50:23 +0000586namespace llvm {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000587
588MCStreamer *createMCAsmStreamer(MCContext &Ctx, formatted_raw_ostream &OS,
589 bool isVerboseAsm, bool useLoc, bool useCFI,
590 bool useDwarfDirectory,
591 MCInstPrinter *InstPrint, MCCodeEmitter *CE,
592 MCAsmBackend *TAB, bool ShowInst) {
593 ARMTargetAsmStreamer *S = new ARMTargetAsmStreamer(OS, *InstPrint);
594
595 return llvm::createAsmStreamer(Ctx, S, OS, isVerboseAsm, useLoc, useCFI,
596 useDwarfDirectory, InstPrint, CE, TAB,
597 ShowInst);
598}
599
Tim Northover5cc3dc82012-12-07 16:50:23 +0000600 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
601 raw_ostream &OS, MCCodeEmitter *Emitter,
602 bool RelaxAll, bool NoExecStack,
603 bool IsThumb) {
Rafael Espindolaa17151a2013-10-08 13:08:17 +0000604 ARMTargetELFStreamer *TS = new ARMTargetELFStreamer();
605 ARMELFStreamer *S =
606 new ARMELFStreamer(Context, TS, TAB, OS, Emitter, IsThumb);
Rafael Espindolaac4ad252013-10-05 16:42:21 +0000607 // FIXME: This should eventually end up somewhere else where more
608 // intelligent flag decisions can be made. For now we are just maintaining
609 // the status quo for ARM and setting EF_ARM_EABI_VER5 as the default.
610 S->getAssembler().setELFHeaderEFlags(ELF::EF_ARM_EABI_VER5);
611
Tim Northover5cc3dc82012-12-07 16:50:23 +0000612 if (RelaxAll)
613 S->getAssembler().setRelaxAll(true);
614 if (NoExecStack)
615 S->getAssembler().setNoExecStack(true);
616 return S;
617 }
618
619}
620
621