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,
58 raw_ostream &OS, MCCodeEmitter *Emitter, bool IsThumb)
59 : MCELFStreamer(Context, TAB, OS, Emitter),
60 IsThumb(IsThumb), MappingSymbolCounter(0), LastEMS(EMS_None),
61 ExTab(0), FnStart(0), Personality(0), CantUnwind(false) {
66 // ARM exception handling directives
67 virtual void EmitFnStart();
68 virtual void EmitFnEnd();
69 virtual void EmitCantUnwind();
70 virtual void EmitPersonality(const MCSymbol *Per);
71 virtual void EmitHandlerData();
72 virtual void EmitSetFP(unsigned NewFpReg,
75 virtual void EmitPad(int64_t Offset);
76 virtual void EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
79 virtual void ChangeSection(const MCSection *Section) {
80 // We have to keep track of the mapping symbol state of any sections we
81 // use. Each one should start off as EMS_None, which is provided as the
82 // default constructor by DenseMap::lookup.
83 LastMappingSymbols[getPreviousSection()] = LastEMS;
84 LastEMS = LastMappingSymbols.lookup(Section);
86 MCELFStreamer::ChangeSection(Section);
89 /// This function is the one used to emit instruction data into the ELF
90 /// streamer. We override it to add the appropriate mapping symbol if
92 virtual void EmitInstruction(const MCInst& Inst) {
94 EmitThumbMappingSymbol();
96 EmitARMMappingSymbol();
98 MCELFStreamer::EmitInstruction(Inst);
101 /// This is one of the functions used to emit data into an ELF section, so the
102 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
104 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {
105 EmitDataMappingSymbol();
106 MCELFStreamer::EmitBytes(Data, AddrSpace);
109 /// This is one of the functions used to emit data into an ELF section, so the
110 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
112 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
113 unsigned AddrSpace) {
114 EmitDataMappingSymbol();
115 MCELFStreamer::EmitValueImpl(Value, Size, AddrSpace);
118 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
119 MCELFStreamer::EmitAssemblerFlag(Flag);
122 case MCAF_SyntaxUnified:
123 return; // no-op here.
126 return; // Change to Thumb mode
129 return; // Change to ARM mode
132 case MCAF_SubsectionsViaSymbols:
138 enum ElfMappingSymbol {
145 void EmitDataMappingSymbol() {
146 if (LastEMS == EMS_Data) return;
147 EmitMappingSymbol("$d");
151 void EmitThumbMappingSymbol() {
152 if (LastEMS == EMS_Thumb) return;
153 EmitMappingSymbol("$t");
157 void EmitARMMappingSymbol() {
158 if (LastEMS == EMS_ARM) return;
159 EmitMappingSymbol("$a");
163 void EmitMappingSymbol(StringRef Name) {
164 MCSymbol *Start = getContext().CreateTempSymbol();
168 getContext().GetOrCreateSymbol(Name + "." +
169 Twine(MappingSymbolCounter++));
171 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
172 MCELF::SetType(SD, ELF::STT_NOTYPE);
173 MCELF::SetBinding(SD, ELF::STB_LOCAL);
174 SD.setExternal(false);
175 Symbol->setSection(*getCurrentSection());
177 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
178 Symbol->setVariableValue(Value);
181 void EmitThumbFunc(MCSymbol *Func) {
182 // FIXME: Anything needed here to flag the function as thumb?
184 getAssembler().setIsThumbFunc(Func);
186 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
187 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
190 // Helper functions for ARM exception handling directives
193 void EmitPersonalityFixup(StringRef Name);
195 void SwitchToEHSection(const char *Prefix, unsigned Type, unsigned Flags,
196 SectionKind Kind, const MCSymbol &Fn);
197 void SwitchToExTabSection(const MCSymbol &FnStart);
198 void SwitchToExIdxSection(const MCSymbol &FnStart);
201 int64_t MappingSymbolCounter;
203 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
204 ElfMappingSymbol LastEMS;
206 // ARM Exception Handling Frame Information
209 const MCSymbol *Personality;
214 inline void ARMELFStreamer::SwitchToEHSection(const char *Prefix,
218 const MCSymbol &Fn) {
219 const MCSectionELF &FnSection =
220 static_cast<const MCSectionELF &>(Fn.getSection());
222 // Create the name for new section
223 StringRef FnSecName(FnSection.getSectionName());
224 SmallString<128> EHSecName(Prefix);
225 if (FnSecName != ".text") {
226 EHSecName += FnSecName;
229 // Get .ARM.extab or .ARM.exidx section
230 const MCSectionELF *EHSection = NULL;
231 if (const MCSymbol *Group = FnSection.getGroup()) {
232 EHSection = getContext().getELFSection(
233 EHSecName, Type, Flags | ELF::SHF_GROUP, Kind,
234 FnSection.getEntrySize(), Group->getName());
236 EHSection = getContext().getELFSection(EHSecName, Type, Flags, Kind);
240 // Switch to .ARM.extab or .ARM.exidx section
241 SwitchSection(EHSection);
242 EmitCodeAlignment(4, 0);
245 inline void ARMELFStreamer::SwitchToExTabSection(const MCSymbol &FnStart) {
246 SwitchToEHSection(".ARM.extab",
249 SectionKind::getDataRel(),
253 inline void ARMELFStreamer::SwitchToExIdxSection(const MCSymbol &FnStart) {
254 SwitchToEHSection(".ARM.exidx",
256 ELF::SHF_ALLOC | ELF::SHF_LINK_ORDER,
257 SectionKind::getDataRel(),
261 void ARMELFStreamer::Reset() {
268 // Add the R_ARM_NONE fixup at the same position
269 void ARMELFStreamer::EmitPersonalityFixup(StringRef Name) {
270 const MCSymbol *PersonalitySym = getContext().GetOrCreateSymbol(Name);
272 const MCSymbolRefExpr *PersonalityRef =
273 MCSymbolRefExpr::Create(PersonalitySym,
274 MCSymbolRefExpr::VK_ARM_NONE,
277 AddValueSymbols(PersonalityRef);
278 MCDataFragment *DF = getOrCreateDataFragment();
279 DF->getFixups().push_back(
280 MCFixup::Create(DF->getContents().size(), PersonalityRef,
281 MCFixup::getKindForSize(4, false)));
284 void ARMELFStreamer::EmitFnStart() {
285 assert(FnStart == 0);
286 FnStart = getContext().CreateTempSymbol();
290 void ARMELFStreamer::EmitFnEnd() {
291 assert(FnStart && ".fnstart must preceeds .fnend");
293 // Emit unwind opcodes if there is no .handlerdata directive
294 int PersonalityIndex = -1;
295 if (!ExTab && !CantUnwind) {
296 // For __aeabi_unwind_cpp_pr1, we have to emit opcodes in .ARM.extab.
297 SwitchToExTabSection(*FnStart);
299 // Create .ARM.extab label for offset in .ARM.exidx
300 ExTab = getContext().CreateTempSymbol();
303 PersonalityIndex = 1;
306 uint32_t NumExtraEntryWords = 0;
307 Entry |= NumExtraEntryWords << 24;
308 Entry |= (EHT_COMPACT | PersonalityIndex) << 16;
310 // TODO: This should be generated according to .save, .vsave, .setfp
311 // directives. Currently, we are simply generating FINISH opcode.
312 Entry |= UNWIND_OPCODE_FINISH << 8;
313 Entry |= UNWIND_OPCODE_FINISH;
315 EmitIntValue(Entry, 4, 0);
318 // Emit the exception index table entry
319 SwitchToExIdxSection(*FnStart);
321 if (PersonalityIndex == 1)
322 EmitPersonalityFixup("__aeabi_unwind_cpp_pr1");
324 const MCSymbolRefExpr *FnStartRef =
325 MCSymbolRefExpr::Create(FnStart,
326 MCSymbolRefExpr::VK_ARM_PREL31,
329 EmitValue(FnStartRef, 4, 0);
332 EmitIntValue(EXIDX_CANTUNWIND, 4, 0);
334 const MCSymbolRefExpr *ExTabEntryRef =
335 MCSymbolRefExpr::Create(ExTab,
336 MCSymbolRefExpr::VK_ARM_PREL31,
338 EmitValue(ExTabEntryRef, 4, 0);
341 // Clean exception handling frame information
345 void ARMELFStreamer::EmitCantUnwind() {
349 void ARMELFStreamer::EmitHandlerData() {
350 SwitchToExTabSection(*FnStart);
352 // Create .ARM.extab label for offset in .ARM.exidx
354 ExTab = getContext().CreateTempSymbol();
358 assert(Personality && ".personality directive must preceed .handlerdata");
360 const MCSymbolRefExpr *PersonalityRef =
361 MCSymbolRefExpr::Create(Personality,
362 MCSymbolRefExpr::VK_ARM_PREL31,
365 EmitValue(PersonalityRef, 4, 0);
367 // Emit unwind opcodes
369 uint32_t NumExtraEntryWords = 0;
371 // TODO: This should be generated according to .save, .vsave, .setfp
372 // directives. Currently, we are simply generating FINISH opcode.
373 Entry |= NumExtraEntryWords << 24;
374 Entry |= UNWIND_OPCODE_FINISH << 16;
375 Entry |= UNWIND_OPCODE_FINISH << 8;
376 Entry |= UNWIND_OPCODE_FINISH;
378 EmitIntValue(Entry, 4, 0);
381 void ARMELFStreamer::EmitPersonality(const MCSymbol *Per) {
385 void ARMELFStreamer::EmitSetFP(unsigned NewFpReg,
388 // TODO: Not implemented
391 void ARMELFStreamer::EmitPad(int64_t Offset) {
392 // TODO: Not implemented
395 void ARMELFStreamer::EmitRegSave(const SmallVectorImpl<unsigned> &RegList,
397 // TODO: Not implemented
401 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
402 raw_ostream &OS, MCCodeEmitter *Emitter,
403 bool RelaxAll, bool NoExecStack,
405 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
407 S->getAssembler().setRelaxAll(true);
409 S->getAssembler().setNoExecStack(true);