blob: 10891a461c113caf5f2d0196a0c9d224b7ddbbc3 [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
153 StringRef UniqueName = Name.str() + "." + itostr(MappingSymbolCounter++);
154 MCSymbol *Symbol = getContext().GetOrCreateSymbol(UniqueName);
155
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());
161
162 const MCExpr *Value = MCSymbolRefExpr::Create(Start, getContext());
163 Symbol->setVariableValue(Value);
164 }
165
166 void EmitThumbFunc(MCSymbol *Func) {
167 // FIXME: Anything needed here to flag the function as thumb?
168
169 getAssembler().setIsThumbFunc(Func);
170
171 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Func);
172 SD.setFlags(SD.getFlags() | ELF_Other_ThumbFunc);
173 }
174
175
176 bool IsThumb;
177 int64_t MappingSymbolCounter;
178
179 DenseMap<const MCSection *, ElfMappingSymbol> LastMappingSymbols;
180 ElfMappingSymbol LastEMS;
181
182 /// @}
183};
184}
185
186namespace llvm {
187 MCELFStreamer* createARMELFStreamer(MCContext &Context, MCAsmBackend &TAB,
188 raw_ostream &OS, MCCodeEmitter *Emitter,
189 bool RelaxAll, bool NoExecStack,
190 bool IsThumb) {
191 ARMELFStreamer *S = new ARMELFStreamer(Context, TAB, OS, Emitter, IsThumb);
192 if (RelaxAll)
193 S->getAssembler().setRelaxAll(true);
194 if (NoExecStack)
195 S->getAssembler().setNoExecStack(true);
196 return S;
197 }
198
199}
200
201