1 //===- lib/MC/ARMELFStreamer.cpp - ELF Object Output for ARM --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #include "ARMRegisterInfo.h"
17 #include "ARMUnwindOp.h"
18 #include "ARMUnwindOpAsm.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/Twine.h"
21 #include "llvm/MC/MCAsmBackend.h"
22 #include "llvm/MC/MCAssembler.h"
23 #include "llvm/MC/MCCodeEmitter.h"
24 #include "llvm/MC/MCContext.h"
25 #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"
30 #include "llvm/MC/MCObjectStreamer.h"
31 #include "llvm/MC/MCRegisterInfo.h"
32 #include "llvm/MC/MCSection.h"
33 #include "llvm/MC/MCSectionELF.h"
34 #include "llvm/MC/MCStreamer.h"
35 #include "llvm/MC/MCSymbol.h"
36 #include "llvm/MC/MCValue.h"
37 #include "llvm/Support/Debug.h"
38 #include "llvm/Support/ELF.h"
39 #include "llvm/Support/raw_ostream.h"
43 static std::string GetAEABIUnwindPersonalityName(unsigned Index) {
44 assert(Index < NUM_PERSONALITY_INDEX && "Invalid personality index");
45 return (Twine("__aeabi_unwind_cpp_pr") + Twine(Index)).str();
50 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
51 /// the appropriate points in the object files. These symbols are defined in the
52 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
54 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
55 /// region of ARM code, Thumb code or data in a section. In practice, this
56 /// emission does not rely on explicit assembler directives but on inherent
57 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
58 /// r0, r0, r0" an instruction).
60 /// As a result this system is orthogonal to the DataRegion infrastructure used
62 class ARMELFStreamer : public MCELFStreamer {
64 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
65 MCCodeEmitter *Emitter, bool IsThumb)
66 : MCELFStreamer(SK_ARMELFStreamer, Context, TAB, OS, Emitter),
67 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None) {
73 // ARM exception handling directives
74 virtual void EmitFnStart();
75 virtual void EmitFnEnd();
76 virtual void EmitCantUnwind();
77 virtual void EmitPersonality(const MCSymbol *Per);
78 virtual void EmitHandlerData();
79 virtual void EmitSetFP(unsigned NewFpReg,
82 virtual void EmitPad(int64_t Offset);
83 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
86 virtual void ChangeSection(const MCSection *Section) {
87 // We have to keep track of the mapping symbol state of any sections we
88 // use. Each one should start off as EMS_None, which is provided as the
89 // default constructor by DenseMap::lookup.
90 LastMappingSymbols[getPreviousSection()] = LastEMS;
91 LastEMS = LastMappingSymbols.lookup(Section);
93 MCELFStreamer::ChangeSection(Section);
96 /// This function is the one used to emit instruction data into the ELF
97 /// streamer. We override it to add the appropriate mapping symbol if
99 virtual void EmitInstruction(const MCInst& Inst) {
101 EmitThumbMappingSymbol();
103 EmitARMMappingSymbol();
105 MCELFStreamer::EmitInstruction(Inst);
108 /// This is one of the functions used to emit data into an ELF section, so the
109 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
111 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {
112 EmitDataMappingSymbol();
113 MCELFStreamer::EmitBytes(Data, AddrSpace);
116 /// This is one of the functions used to emit data into an ELF section, so the
117 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
119 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
120 unsigned AddrSpace) {
121 EmitDataMappingSymbol();
122 MCELFStreamer::EmitValueImpl(Value, Size, AddrSpace);
125 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
126 MCELFStreamer::EmitAssemblerFlag(Flag);
129 case MCAF_SyntaxUnified:
130 return; // no-op here.
133 return; // Change to Thumb mode
136 return; // Change to ARM mode
139 case MCAF_SubsectionsViaSymbols:
144 static bool classof(const MCStreamer *S) {
145 return S->getKind() == SK_ARMELFStreamer;
149 enum ElfMappingSymbol {
156 void EmitDataMappingSymbol() {
157 if (LastEMS == EMS_Data) return;
158 EmitMappingSymbol("$d");
162 void EmitThumbMappingSymbol() {
163 if (LastEMS == EMS_Thumb) return;
164 EmitMappingSymbol("$t");
168 void EmitARMMappingSymbol() {
169 if (LastEMS == EMS_ARM) return;
170 EmitMappingSymbol("$a");
174 void EmitMappingSymbol(StringRef Name) {
175 MCSymbol *Start = getContext().CreateTempSymbol();
179 getContext().GetOrCreateSymbol(Name + "." +
180 Twine(MappingSymbolCounter++));
182 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
183 MCELF::SetType(SD, ELF::STT_NOTYPE);
184 MCELF::SetBinding(SD, ELF::STB_LOCAL);
185 SD.setExternal(false);
186 Symbol->setSection(*getCurrentSection());
188 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
189 Symbol->setVariableValue(Value);
192 void EmitThumbFunc(MCSymbol *Func) {
193 // FIXME: Anything needed here to flag the function as thumb?
195 getAssembler().setIsThumbFunc(Func);
197 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
198 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
201 // Helper functions for ARM exception handling directives
204 void EmitPersonalityFixup(StringRef Name);
205 void CollectUnwindOpcodes();
207 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
208 SectionKind Kind, const MCSymbol &Fn);
209 void SwitchToExTabSection(const MCSymbol &FnStart);
210 void SwitchToExIdxSection(const MCSymbol &FnStart);
213 int64_t MappingSymbolCounter;
215 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
216 ElfMappingSymbol LastEMS;
218 // ARM Exception Handling Frame Information
221 const MCSymbol *Personality;
222 uint32_t VFPRegSave; // Register mask for {d31-d0}
223 uint32_t RegSave; // Register mask for {r15-r0}
229 UnwindOpcodeAssembler UnwindOpAsm;
231 } // end anonymous namespace
233 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
237 const MCSymbol &Fn) {
238 const MCSectionELF &FnSection =
239 static_cast<const MCSectionELF &>(Fn.getSection());
241 // Create the name for new section
242 StringRef FnSecName(FnSection.getSectionName());
243 SmallString<128> EHSecName(Prefix);
244 if (FnSecName != ".text") {
245 EHSecName += FnSecName;
248 // Get .ARM.extab or .ARM.exidx section
249 const MCSectionELF *EHSection = NULL;
250 if (const MCSymbol *Group = FnSection.getGroup()) {
251 EHSection = getContext().getELFSection(
252 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
253 FnSection.getEntrySize(), Group->getName());
255 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
257 assert(EHSection && "Failed to get the required EH section");
259 // Switch to .ARM.extab or .ARM.exidx section
260 SwitchSection(EHSection);
261 EmitCodeAlignment(4, 0);
264 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
265 SwitchToEHSection(".ARM.extab",
268 SectionKind::getDataRel(),
272 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
273 SwitchToEHSection(".ARM.exidx",
275 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
276 SectionKind::getDataRel(),
280 void ARMELFStreamer::Reset() {
281 const MCRegisterInfo &MRI = getContext().getRegisterInfo();
288 FPReg = MRI.getEncodingValue(ARM::SP);
297 // Add the R_ARM_NONE fixup at the same position
298 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
299 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
301 const MCSymbolRefExpr *PersonalityRef =
302 MCSymbolRefExpr::Create(PersonalitySym,
303 MCSymbolRefExpr::VK_ARM_NONE,
306 AddValueSymbols(PersonalityRef);
307 MCDataFragment *DF = getOrCreateDataFragment();
308 DF->getFixups().push_back(
309 MCFixup::Create(DF->getContents().size(), PersonalityRef,
310 MCFixup::getKindForSize(4, false)));
313 void ARMELFStreamer::CollectUnwindOpcodes() {
315 UnwindOpAsm.EmitSetFP(FPReg);
316 UnwindOpAsm.EmitSPOffset(-FPOffset);
318 UnwindOpAsm.EmitSPOffset(SPOffset);
320 UnwindOpAsm.EmitVFPRegSave(VFPRegSave);
321 UnwindOpAsm.EmitRegSave(RegSave);
322 UnwindOpAsm.Finalize();
325 void ARMELFStreamer::EmitFnStart() {
326 assert(FnStart == 0);
327 FnStart = getContext().CreateTempSymbol();
331 void ARMELFStreamer::EmitFnEnd() {
332 assert(FnStart && ".fnstart must preceeds .fnend");
334 // Emit unwind opcodes if there is no .handlerdata directive
335 if (!ExTab && !CantUnwind) {
336 CollectUnwindOpcodes();
338 unsigned PersonalityIndex = UnwindOpAsm.getPersonalityIndex();
339 if (PersonalityIndex == AEABI_UNWIND_CPP_PR1 ||
340 PersonalityIndex == AEABI_UNWIND_CPP_PR2) {
341 // For the __aeabi_unwind_cpp_pr1 and __aeabi_unwind_cpp_pr2, we have to
342 // emit the unwind opcodes in the corresponding ".ARM.extab" section, and
343 // then emit a reference to these unwind opcodes in the second word of
344 // the exception index table entry.
345 SwitchToExTabSection(*FnStart);
346 ExTab = getContext().CreateTempSymbol();
348 EmitBytes(UnwindOpAsm.data(), 0);
352 // Emit the exception index table entry
353 SwitchToExIdxSection(*FnStart);
355 unsigned PersonalityIndex = UnwindOpAsm.getPersonalityIndex();
356 if (PersonalityIndex < NUM_PERSONALITY_INDEX)
357 EmitPersonalityFixup(GetAEABIUnwindPersonalityName(PersonalityIndex));
359 const MCSymbolRefExpr *FnStartRef =
360 MCSymbolRefExpr::Create(FnStart,
361 MCSymbolRefExpr::VK_ARM_PREL31,
364 EmitValue(FnStartRef, 4, 0);
367 EmitIntValue(EXIDX_CANTUNWIND, 4, 0);
369 // Emit a reference to the unwind opcodes in the ".ARM.extab" section.
370 const MCSymbolRefExpr *ExTabEntryRef =
371 MCSymbolRefExpr::Create(ExTab,
372 MCSymbolRefExpr::VK_ARM_PREL31,
374 EmitValue(ExTabEntryRef, 4, 0);
376 // For the __aeabi_unwind_cpp_pr0, we have to emit the unwind opcodes in
377 // the second word of exception index table entry. The size of the unwind
378 // opcodes should always be 4 bytes.
379 assert(PersonalityIndex == AEABI_UNWIND_CPP_PR0 &&
380 "Compact model must use __aeabi_cpp_unwind_pr0 as personality");
381 assert(UnwindOpAsm.size() == 4u &&
382 "Unwind opcode size for __aeabi_cpp_unwind_pr0 must be equal to 4");
383 EmitBytes(UnwindOpAsm.data(), 0);
386 // Clean exception handling frame information
390 void ARMELFStreamer::EmitCantUnwind() {
394 void ARMELFStreamer::EmitHandlerData() {
395 SwitchToExTabSection(*FnStart);
397 // Create .ARM.extab label for offset in .ARM.exidx
399 ExTab = getContext().CreateTempSymbol();
403 assert(Personality && ".personality directive must preceed .handlerdata");
405 const MCSymbolRefExpr *PersonalityRef =
406 MCSymbolRefExpr::Create(Personality,
407 MCSymbolRefExpr::VK_ARM_PREL31,
410 EmitValue(PersonalityRef, 4, 0);
412 // Emit unwind opcodes
413 CollectUnwindOpcodes();
414 EmitBytes(UnwindOpAsm.data(), 0);
417 void ARMELFStreamer::EmitPersonality(const MCSymbol *Per) {
419 UnwindOpAsm.setPersonality(Per);
422 void ARMELFStreamer::EmitSetFP(unsigned NewFPReg,
425 assert(SPOffset == 0 &&
426 "Current implementation assumes .setfp precedes .pad");
428 const MCRegisterInfo &MRI = getContext().getRegisterInfo();
430 uint16_t NewFPRegEncVal = MRI.getEncodingValue(NewFPReg);
432 uint16_t NewSPRegEncVal = MRI.getEncodingValue(NewSPReg);
435 assert((NewSPReg == ARM::SP || NewSPRegEncVal == FPReg) &&
436 "the operand of .setfp directive should be either $sp or $fp");
439 FPReg = NewFPRegEncVal;
443 void ARMELFStreamer::EmitPad(int64_t Offset) {
447 void ARMELFStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
449 const MCRegisterInfo &MRI = getContext().getRegisterInfo();
452 unsigned Max = IsVector ? 32 : 16;
454 uint32_t &RegMask = IsVector ? VFPRegSave : RegSave;
456 for (size_t i = 0; i < RegList.size(); ++i) {
457 unsigned Reg = MRI.getEncodingValue(RegList[i]);
458 assert(Reg < Max && "Register encoded value out of range");
459 RegMask |= 1u << Reg;
464 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
465 raw_ostream &OS, MCCodeEmitter *Emitter,
466 bool RelaxAll, bool NoExecStack,
468 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
470 S->getAssembler().setRelaxAll(true);
472 S->getAssembler().setNoExecStack(true);