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 "ARMUnwindOp.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/Twine.h"
19 #include "llvm/MC/MCAsmBackend.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCELF.h"
24 #include "llvm/MC/MCELFStreamer.h"
25 #include "llvm/MC/MCELFSymbolFlags.h"
26 #include "llvm/MC/MCExpr.h"
27 #include "llvm/MC/MCInst.h"
28 #include "llvm/MC/MCObjectStreamer.h"
29 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCSectionELF.h"
31 #include "llvm/MC/MCStreamer.h"
32 #include "llvm/MC/MCSymbol.h"
33 #include "llvm/MC/MCValue.h"
34 #include "llvm/Support/Debug.h"
35 #include "llvm/Support/ELF.h"
36 #include "llvm/Support/ErrorHandling.h"
37 #include "llvm/Support/raw_ostream.h"
43 /// Extend the generic ELFStreamer class so that it can emit mapping symbols at
44 /// the appropriate points in the object files. These symbols are defined in the
45 /// ARM ELF ABI: infocenter.arm.com/help/topic/com.arm.../IHI0044D_aaelf.pdf.
47 /// In brief: $a, $t or $d should be emitted at the start of each contiguous
48 /// region of ARM code, Thumb code or data in a section. In practice, this
49 /// emission does not rely on explicit assembler directives but on inherent
50 /// properties of the directives doing the emission (e.g. ".byte" is data, "add
51 /// r0, r0, r0" an instruction).
53 /// As a result this system is orthogonal to the DataRegion infrastructure used
55 class ARMELFStreamer : public MCELFStreamer {
57 ARMELFStreamer(MCContext &Context, MCAsmBackend &TAB, raw_ostream &OS,
58 MCCodeEmitter *Emitter, bool IsThumb)
59 : MCELFStreamer(SK_ARMELFStreamer, Context, TAB, OS, Emitter),
60 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None), ExTab(0),
61 FnStart(0), Personality(0), CantUnwind(false) {}
65 // ARM exception handling directives
66 virtual void EmitFnStart();
67 virtual void EmitFnEnd();
68 virtual void EmitCantUnwind();
69 virtual void EmitPersonality(const MCSymbol *Per);
70 virtual void EmitHandlerData();
71 virtual void EmitSetFP(unsigned NewFpReg,
74 virtual void EmitPad(int64_t Offset);
75 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
78 virtual void ChangeSection(const MCSection *Section) {
79 // We have to keep track of the mapping symbol state of any sections we
80 // use. Each one should start off as EMS_None, which is provided as the
81 // default constructor by DenseMap::lookup.
82 LastMappingSymbols[getPreviousSection()] = LastEMS;
83 LastEMS = LastMappingSymbols.lookup(Section);
85 MCELFStreamer::ChangeSection(Section);
88 /// This function is the one used to emit instruction data into the ELF
89 /// streamer. We override it to add the appropriate mapping symbol if
91 virtual void EmitInstruction(const MCInst& Inst) {
93 EmitThumbMappingSymbol();
95 EmitARMMappingSymbol();
97 MCELFStreamer::EmitInstruction(Inst);
100 /// This is one of the functions used to emit data into an ELF section, so the
101 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
103 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {
104 EmitDataMappingSymbol();
105 MCELFStreamer::EmitBytes(Data, AddrSpace);
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 EmitValueImpl(const MCExpr *Value, unsigned Size,
112 unsigned AddrSpace) {
113 EmitDataMappingSymbol();
114 MCELFStreamer::EmitValueImpl(Value, Size, AddrSpace);
117 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
118 MCELFStreamer::EmitAssemblerFlag(Flag);
121 case MCAF_SyntaxUnified:
122 return; // no-op here.
125 return; // Change to Thumb mode
128 return; // Change to ARM mode
131 case MCAF_SubsectionsViaSymbols:
136 static bool classof(const MCStreamer *S) {
137 return S->getKind() == SK_ARMELFStreamer;
141 enum ElfMappingSymbol {
148 void EmitDataMappingSymbol() {
149 if (LastEMS == EMS_Data) return;
150 EmitMappingSymbol("$d");
154 void EmitThumbMappingSymbol() {
155 if (LastEMS == EMS_Thumb) return;
156 EmitMappingSymbol("$t");
160 void EmitARMMappingSymbol() {
161 if (LastEMS == EMS_ARM) return;
162 EmitMappingSymbol("$a");
166 void EmitMappingSymbol(StringRef Name) {
167 MCSymbol *Start = getContext().CreateTempSymbol();
171 getContext().GetOrCreateSymbol(Name + "." +
172 Twine(MappingSymbolCounter++));
174 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
175 MCELF::SetType(SD, ELF::STT_NOTYPE);
176 MCELF::SetBinding(SD, ELF::STB_LOCAL);
177 SD.setExternal(false);
178 Symbol->setSection(*getCurrentSection());
180 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
181 Symbol->setVariableValue(Value);
184 void EmitThumbFunc(MCSymbol *Func) {
185 // FIXME: Anything needed here to flag the function as thumb?
187 getAssembler().setIsThumbFunc(Func);
189 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
190 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
193 // Helper functions for ARM exception handling directives
196 void EmitPersonalityFixup(StringRef Name);
198 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
199 SectionKind Kind, const MCSymbol &Fn);
200 void SwitchToExTabSection(const MCSymbol &FnStart);
201 void SwitchToExIdxSection(const MCSymbol &FnStart);
204 int64_t MappingSymbolCounter;
206 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
207 ElfMappingSymbol LastEMS;
209 // ARM Exception Handling Frame Information
212 const MCSymbol *Personality;
217 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
221 const MCSymbol &Fn) {
222 const MCSectionELF &FnSection =
223 static_cast<const MCSectionELF &>(Fn.getSection());
225 // Create the name for new section
226 StringRef FnSecName(FnSection.getSectionName());
227 SmallString<128> EHSecName(Prefix);
228 if (FnSecName != ".text") {
229 EHSecName += FnSecName;
232 // Get .ARM.extab or .ARM.exidx section
233 const MCSectionELF *EHSection = NULL;
234 if (const MCSymbol *Group = FnSection.getGroup()) {
235 EHSection = getContext().getELFSection(
236 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
237 FnSection.getEntrySize(), Group->getName());
239 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
243 // Switch to .ARM.extab or .ARM.exidx section
244 SwitchSection(EHSection);
245 EmitCodeAlignment(4, 0);
248 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
249 SwitchToEHSection(".ARM.extab",
252 SectionKind::getDataRel(),
256 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
257 SwitchToEHSection(".ARM.exidx",
259 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
260 SectionKind::getDataRel(),
264 void ARMELFStreamer::Reset() {
271 // Add the R_ARM_NONE fixup at the same position
272 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
273 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
275 const MCSymbolRefExpr *PersonalityRef =
276 MCSymbolRefExpr::Create(PersonalitySym,
277 MCSymbolRefExpr::VK_ARM_NONE,
280 AddValueSymbols(PersonalityRef);
281 MCDataFragment *DF = getOrCreateDataFragment();
282 DF->getFixups().push_back(
283 MCFixup::Create(DF->getContents().size(), PersonalityRef,
284 MCFixup::getKindForSize(4, false)));
287 void ARMELFStreamer::EmitFnStart() {
288 assert(FnStart == 0);
289 FnStart = getContext().CreateTempSymbol();
293 void ARMELFStreamer::EmitFnEnd() {
294 assert(FnStart && ".fnstart must preceeds .fnend");
296 // Emit unwind opcodes if there is no .handlerdata directive
297 int PersonalityIndex = -1;
298 if (!ExTab && !CantUnwind) {
299 // For __aeabi_unwind_cpp_pr1, we have to emit opcodes in .ARM.extab.
300 SwitchToExTabSection(*FnStart);
302 // Create .ARM.extab label for offset in .ARM.exidx
303 ExTab = getContext().CreateTempSymbol();
306 PersonalityIndex = 1;
309 uint32_t NumExtraEntryWords = 0;
310 Entry |= NumExtraEntryWords << 24;
311 Entry |= (EHT_COMPACT | PersonalityIndex) << 16;
313 // TODO: This should be generated according to .save, .vsave, .setfp
314 // directives. Currently, we are simply generating FINISH opcode.
315 Entry |= UNWIND_OPCODE_FINISH << 8;
316 Entry |= UNWIND_OPCODE_FINISH;
318 EmitIntValue(Entry, 4, 0);
321 // Emit the exception index table entry
322 SwitchToExIdxSection(*FnStart);
324 if (PersonalityIndex == 1)
325 EmitPersonalityFixup("__aeabi_unwind_cpp_pr1");
327 const MCSymbolRefExpr *FnStartRef =
328 MCSymbolRefExpr::Create(FnStart,
329 MCSymbolRefExpr::VK_ARM_PREL31,
332 EmitValue(FnStartRef, 4, 0);
335 EmitIntValue(EXIDX_CANTUNWIND, 4, 0);
337 const MCSymbolRefExpr *ExTabEntryRef =
338 MCSymbolRefExpr::Create(ExTab,
339 MCSymbolRefExpr::VK_ARM_PREL31,
341 EmitValue(ExTabEntryRef, 4, 0);
344 // Clean exception handling frame information
348 void ARMELFStreamer::EmitCantUnwind() {
352 void ARMELFStreamer::EmitHandlerData() {
353 SwitchToExTabSection(*FnStart);
355 // Create .ARM.extab label for offset in .ARM.exidx
357 ExTab = getContext().CreateTempSymbol();
361 assert(Personality && ".personality directive must preceed .handlerdata");
363 const MCSymbolRefExpr *PersonalityRef =
364 MCSymbolRefExpr::Create(Personality,
365 MCSymbolRefExpr::VK_ARM_PREL31,
368 EmitValue(PersonalityRef, 4, 0);
370 // Emit unwind opcodes
372 uint32_t NumExtraEntryWords = 0;
374 // TODO: This should be generated according to .save, .vsave, .setfp
375 // directives. Currently, we are simply generating FINISH opcode.
376 Entry |= NumExtraEntryWords << 24;
377 Entry |= UNWIND_OPCODE_FINISH << 16;
378 Entry |= UNWIND_OPCODE_FINISH << 8;
379 Entry |= UNWIND_OPCODE_FINISH;
381 EmitIntValue(Entry, 4, 0);
384 void ARMELFStreamer::EmitPersonality(const MCSymbol *Per) {
388 void ARMELFStreamer::EmitSetFP(unsigned NewFpReg,
391 // TODO: Not implemented
394 void ARMELFStreamer::EmitPad(int64_t Offset) {
395 // TODO: Not implemented
398 void ARMELFStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
400 // TODO: Not implemented
404 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
405 raw_ostream &OS, MCCodeEmitter *Emitter,
406 bool RelaxAll, bool NoExecStack,
408 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
410 S->getAssembler().setRelaxAll(true);
412 S->getAssembler().setNoExecStack(true);