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 "llvm/MC/MCELFStreamer.h"
17 #include "llvm/ADT/SmallPtrSet.h"
18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCSectionELF.h"
23 #include "llvm/MC/MCStreamer.h"
24 #include "llvm/MC/MCELF.h"
25 #include "llvm/MC/MCELFStreamer.h"
26 #include "llvm/MC/MCELFSymbolFlags.h"
27 #include "llvm/MC/MCExpr.h"
28 #include "llvm/MC/MCInst.h"
29 #include "llvm/MC/MCObjectStreamer.h"
30 #include "llvm/MC/MCSection.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCValue.h"
33 #include "llvm/MC/MCAsmBackend.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) {
65 virtual void ChangeSection(const MCSection *Section) {
66 // We have to keep track of the mapping symbol state of any sections we
67 // use. Each one should start off as EMS_None, which is provided as the
68 // default constructor by DenseMap::lookup.
69 LastMappingSymbols[getPreviousSection()] = LastEMS;
70 LastEMS = LastMappingSymbols.lookup(Section);
72 MCELFStreamer::ChangeSection(Section);
75 /// This function is the one used to emit instruction data into the ELF
76 /// streamer. We override it to add the appropriate mapping symbol if
78 virtual void EmitInstruction(const MCInst& Inst) {
80 EmitThumbMappingSymbol();
82 EmitARMMappingSymbol();
84 MCELFStreamer::EmitInstruction(Inst);
87 /// This is one of the functions used to emit data into an ELF section, so the
88 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
90 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {
91 EmitDataMappingSymbol();
92 MCELFStreamer::EmitBytes(Data, AddrSpace);
95 /// This is one of the functions used to emit data into an ELF section, so the
96 /// ARM streamer overrides it to add the appropriate mapping symbol ($d) if
98 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
100 EmitDataMappingSymbol();
101 MCELFStreamer::EmitValueImpl(Value, Size, AddrSpace);
104 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
105 MCELFStreamer::EmitAssemblerFlag(Flag);
108 case MCAF_SyntaxUnified:
109 return; // no-op here.
112 return; // Change to Thumb mode
115 return; // Change to ARM mode
118 case MCAF_SubsectionsViaSymbols:
124 enum ElfMappingSymbol {
131 void EmitDataMappingSymbol() {
132 if (LastEMS == EMS_Data) return;
133 EmitMappingSymbol("$d");
137 void EmitThumbMappingSymbol() {
138 if (LastEMS == EMS_Thumb) return;
139 EmitMappingSymbol("$t");
143 void EmitARMMappingSymbol() {
144 if (LastEMS == EMS_ARM) return;
145 EmitMappingSymbol("$a");
149 void EmitMappingSymbol(StringRef Name) {
150 MCSymbol *Start = getContext().CreateTempSymbol();
153 StringRef UniqueName = Name.str() + "." + itostr(MappingSymbolCounter++);
154 MCSymbol *Symbol = getContext().GetOrCreateSymbol(UniqueName);
156 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
157 MCELF::SetType(SD, ELF::STT_NOTYPE);
158 MCELF::SetBinding(SD, ELF::STB_LOCAL);
159 SD.setExternal(false);
160 Symbol->setSection(*getCurrentSection());
162 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
163 Symbol->setVariableValue(Value);
166 void EmitThumbFunc(MCSymbol *Func) {
167 // FIXME: Anything needed here to flag the function as thumb?
169 getAssembler().setIsThumbFunc(Func);
171 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
172 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
177 int64_t MappingSymbolCounter;
179 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
180 ElfMappingSymbol LastEMS;
187 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
188 raw_ostream &OS, MCCodeEmitter *Emitter,
189 bool RelaxAll, bool NoExecStack,
191 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
193 S->getAssembler().setRelaxAll(true);
195 S->getAssembler().setNoExecStack(true);