blob: 4a35055c9a5a3a1cd01efa5040a8ecd875f484ab [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
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"
38
39using namespace llvm;
40
41namespace {
42
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.
46///
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).
52///
53/// As a result this system is orthogonal to the DataRegion infrastructure used
54/// by MachO. Beware!
55class ARMELFStreamer : public MCELFStreamer {
56public:
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 }
62
63 ~ARMELFStreamer() {}
64
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);
71
72 MCELFStreamer::ChangeSection(Section);
73 }
74
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
77 /// necessary.
78 virtual void EmitInstruction(const MCInst& Inst) {
79 if (IsThumb)
80 EmitThumbMappingSymbol();
81 else
82 EmitARMMappingSymbol();
83
84 MCELFStreamer::EmitInstruction(Inst);
85 }
86
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
89 /// necessary.
90 virtual void EmitBytes(StringRef Data, unsigned AddrSpace) {
91 EmitDataMappingSymbol();
92 MCELFStreamer::EmitBytes(Data, AddrSpace);
93 }
94
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
97 /// necessary.
98 virtual void EmitValueImpl(const MCExpr *Value, unsigned Size,
99 unsigned AddrSpace) {
100 EmitDataMappingSymbol();
101 MCELFStreamer::EmitValueImpl(Value, Size, AddrSpace);
102 }
103
104 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag) {
105 MCELFStreamer::EmitAssemblerFlag(Flag);
106
107 switch (Flag) {
108 case MCAF_SyntaxUnified:
109 return; // no-op here.
110 case MCAF_Code16:
111 IsThumb = true;
112 return; // Change to Thumb mode
113 case MCAF_Code32:
114 IsThumb = false;
115 return; // Change to ARM mode
116 case MCAF_Code64:
117 return;
118 case MCAF_SubsectionsViaSymbols:
119 return;
120 }
121 }
122
123private:
124 enum ElfMappingSymbol {
125 EMS_None,
126 EMS_ARM,
127 EMS_Thumb,
128 EMS_Data
129 };
130
131 void EmitDataMappingSymbol() {
132 if (LastEMS == EMS_Data) return;
133 EmitMappingSymbol("$d");
134 LastEMS = EMS_Data;
135 }
136
137 void EmitThumbMappingSymbol() {
138 if (LastEMS == EMS_Thumb) return;
139 EmitMappingSymbol("$t");
140 LastEMS = EMS_Thumb;
141 }
142
143 void EmitARMMappingSymbol() {
144 if (LastEMS == EMS_ARM) return;
145 EmitMappingSymbol("$a");
146 LastEMS = EMS_ARM;
147 }
148
149 void EmitMappingSymbol(StringRef Name) {
150 MCSymbol *Start = getContext().CreateTempSymbol();
151 EmitLabel(Start);
152
Chandler Carruth1d94e932012-12-08 03:10:14 +0000153 MCSymbol *Symbol =
154 getContext().GetOrCreateSymbol(Name.str() + "." +
155 itostr(MappingSymbolCounter++));
Tim Northover5cc3dc82012-12-07 16:50:23 +0000156
157 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
158 MCELF::SetType(SD, ELF::STT_NOTYPE);
159 MCELF::SetBinding(SD, ELF::STB_LOCAL);
160 SD.setExternal(false);
161 Symbol->setSection(*getCurrentSection());
162
163 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
164 Symbol->setVariableValue(Value);
165 }
166
167 void EmitThumbFunc(MCSymbol *Func) {
168 // FIXME: Anything needed here to flag the function as thumb?
169
170 getAssembler().setIsThumbFunc(Func);
171
172 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
173 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
174 }
175
176
177 bool IsThumb;
178 int64_t MappingSymbolCounter;
179
180 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
181 ElfMappingSymbol LastEMS;
182
183 /// @}
184};
185}
186
187namespace llvm {
188 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
189 raw_ostream &OS, MCCodeEmitter *Emitter,
190 bool RelaxAll, bool NoExecStack,
191 bool IsThumb) {
192 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
193 if (RelaxAll)
194 S->getAssembler().setRelaxAll(true);
195 if (NoExecStack)
196 S->getAssembler().setNoExecStack(true);
197 return S;
198 }
199
200}
201
202