blob: fcc62626c37c2f4848301e303659f37553ab6cc2 [file] [log] [blame]
Matt Fleming3565a062010-08-16 18:57:57 +00001//===- lib/MC/MCELFStreamer.cpp - ELF Object Output ------------===//
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 ELF .o object files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/MC/MCStreamer.h"
15
16#include "llvm/MC/MCAssembler.h"
17#include "llvm/MC/MCContext.h"
18#include "llvm/MC/MCCodeEmitter.h"
19#include "llvm/MC/MCELFSymbolFlags.h"
20#include "llvm/MC/MCExpr.h"
21#include "llvm/MC/MCInst.h"
22#include "llvm/MC/MCObjectStreamer.h"
23#include "llvm/MC/MCSection.h"
24#include "llvm/MC/MCSectionELF.h"
25#include "llvm/MC/MCSymbol.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/ELF.h"
28#include "llvm/Support/ErrorHandling.h"
29#include "llvm/Support/raw_ostream.h"
30#include "llvm/Target/TargetAsmBackend.h"
31
32using namespace llvm;
33
34namespace {
35
36class MCELFStreamer : public MCObjectStreamer {
Benjamin Kramer93ded732010-08-27 10:40:51 +000037 void EmitInstToFragment(const MCInst &Inst);
38 void EmitInstToData(const MCInst &Inst);
Matt Fleming3565a062010-08-16 18:57:57 +000039public:
40 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
41 raw_ostream &OS, MCCodeEmitter *Emitter)
42 : MCObjectStreamer(Context, TAB, OS, Emitter) {}
43
44 ~MCELFStreamer() {}
45
46 /// @name MCStreamer Interface
47 /// @{
48
49 virtual void EmitLabel(MCSymbol *Symbol);
50 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
51 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
52 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
53 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
54 assert(0 && "ELF doesn't support this directive");
55 }
56 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
57 unsigned ByteAlignment);
58 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
59 assert(0 && "ELF doesn't support this directive");
60 }
61
62 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
63 assert(0 && "ELF doesn't support this directive");
64 }
65
66 virtual void EmitCOFFSymbolType(int Type) {
67 assert(0 && "ELF doesn't support this directive");
68 }
69
70 virtual void EndCOFFSymbolDef() {
71 assert(0 && "ELF doesn't support this directive");
72 }
73
74 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
75 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
76 SD.setSize(Value);
77 }
78
79 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
80 assert(0 && "ELF doesn't support this directive");
81 }
82 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
83 unsigned Size = 0, unsigned ByteAlignment = 0) {
84 assert(0 && "ELF doesn't support this directive");
85 }
86 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
87 uint64_t Size, unsigned ByteAlignment = 0) {
88 assert(0 && "ELF doesn't support this directive");
89 }
90 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
91 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
92 virtual void EmitGPRel32Value(const MCExpr *Value) {
93 assert(0 && "ELF doesn't support this directive");
94 }
95 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
96 unsigned ValueSize = 1,
97 unsigned MaxBytesToEmit = 0);
98 virtual void EmitCodeAlignment(unsigned ByteAlignment,
99 unsigned MaxBytesToEmit = 0);
100 virtual void EmitValueToOffset(const MCExpr *Offset,
101 unsigned char Value = 0);
102
103 virtual void EmitFileDirective(StringRef Filename);
104 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
105 DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n");
106 }
107
108 virtual void EmitInstruction(const MCInst &Inst);
109 virtual void Finish();
110
111 /// @}
112};
113
114} // end anonymous namespace.
115
116void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
117 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
118
119 // FIXME: This is wasteful, we don't necessarily need to create a data
120 // fragment. Instead, we should mark the symbol as pointing into the data
121 // fragment if it exists, otherwise we should just queue the label and set its
122 // fragment pointer when we emit the next fragment.
123 MCDataFragment *F = getOrCreateDataFragment();
124 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
125 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
126 SD.setFragment(F);
127 SD.setOffset(F->getContents().size());
128
129 Symbol->setSection(*CurSection);
130}
131
132void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
133 switch (Flag) {
134 case MCAF_SubsectionsViaSymbols:
135 getAssembler().setSubsectionsViaSymbols(true);
136 return;
137 }
138
139 assert(0 && "invalid assembler flag!");
140}
141
142void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
143 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
144 // MCObjectStreamer.
145 // FIXME: Lift context changes into super class.
146 getAssembler().getOrCreateSymbolData(*Symbol);
147 Symbol->setVariableValue(AddValueSymbols(Value));
148}
149
150void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
151 MCSymbolAttr Attribute) {
152 // Indirect symbols are handled differently, to match how 'as' handles
153 // them. This makes writing matching .o files easier.
154 if (Attribute == MCSA_IndirectSymbol) {
155 // Note that we intentionally cannot use the symbol data here; this is
156 // important for matching the string table that 'as' generates.
157 IndirectSymbolData ISD;
158 ISD.Symbol = Symbol;
159 ISD.SectionData = getCurrentSectionData();
160 getAssembler().getIndirectSymbols().push_back(ISD);
161 return;
162 }
163
164 // Adding a symbol attribute always introduces the symbol, note that an
165 // important side effect of calling getOrCreateSymbolData here is to register
166 // the symbol with the assembler.
167 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
168
169 // The implementation of symbol attributes is designed to match 'as', but it
170 // leaves much to desired. It doesn't really make sense to arbitrarily add and
171 // remove flags, but 'as' allows this (in particular, see .desc).
172 //
173 // In the future it might be worth trying to make these operations more well
174 // defined.
175 switch (Attribute) {
176 case MCSA_LazyReference:
177 case MCSA_Reference:
178 case MCSA_NoDeadStrip:
179 case MCSA_PrivateExtern:
180 case MCSA_WeakReference:
181 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000182 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000183 case MCSA_Invalid:
184 case MCSA_ELF_TypeIndFunction:
185 case MCSA_IndirectSymbol:
186 assert(0 && "Invalid symbol attribute for ELF!");
187 break;
188
189 case MCSA_Global:
190 SD.setFlags(SD.getFlags() | ELF_STB_Global);
191 SD.setExternal(true);
192 break;
193
194 case MCSA_Weak:
195 SD.setFlags(SD.getFlags() | ELF_STB_Weak);
196 break;
197
198 case MCSA_Local:
199 SD.setFlags(SD.getFlags() | ELF_STB_Local);
200 break;
201
202 case MCSA_ELF_TypeFunction:
203 SD.setFlags(SD.getFlags() | ELF_STT_Func);
204 break;
205
206 case MCSA_ELF_TypeObject:
207 SD.setFlags(SD.getFlags() | ELF_STT_Object);
208 break;
209
210 case MCSA_ELF_TypeTLS:
211 SD.setFlags(SD.getFlags() | ELF_STT_Tls);
212 break;
213
214 case MCSA_ELF_TypeCommon:
215 SD.setFlags(SD.getFlags() | ELF_STT_Common);
216 break;
217
218 case MCSA_ELF_TypeNoType:
219 SD.setFlags(SD.getFlags() | ELF_STT_Notype);
220 break;
221
222 case MCSA_Protected:
223 SD.setFlags(SD.getFlags() | ELF_STV_Protected);
224 break;
225
226 case MCSA_Hidden:
227 SD.setFlags(SD.getFlags() | ELF_STV_Hidden);
228 break;
229
230 case MCSA_Internal:
231 SD.setFlags(SD.getFlags() | ELF_STV_Internal);
232 break;
233 }
234}
235
236void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
237 unsigned ByteAlignment) {
238 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
239
240 if ((SD.getFlags() & (0xf << ELF_STB_Shift)) == ELF_STB_Local) {
241 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
242 MCSectionELF::SHT_NOBITS,
243 MCSectionELF::SHF_WRITE |
244 MCSectionELF::SHF_ALLOC,
245 SectionKind::getBSS());
246
247 MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
248 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
249 SD.setFragment(F);
250 Symbol->setSection(*Section);
251 SD.setSize(MCConstantExpr::Create(Size, getContext()));
252 } else {
253 SD.setExternal(true);
254 }
255
256 SD.setCommon(Size, ByteAlignment);
257}
258
259void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
260 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
261 // MCObjectStreamer.
262 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
263}
264
265void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
266 unsigned AddrSpace) {
267 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
268 // MCObjectStreamer.
269 MCDataFragment *DF = getOrCreateDataFragment();
270
271 // Avoid fixups when possible.
272 int64_t AbsValue;
273 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
274 // FIXME: Endianness assumption.
275 for (unsigned i = 0; i != Size; ++i)
276 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
277 } else {
278 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
279 MCFixup::getKindForSize(Size)));
280 DF->getContents().resize(DF->getContents().size() + Size, 0);
281 }
282}
283
284void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
285 int64_t Value, unsigned ValueSize,
286 unsigned MaxBytesToEmit) {
287 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
288 // MCObjectStreamer.
289 if (MaxBytesToEmit == 0)
290 MaxBytesToEmit = ByteAlignment;
291 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
292 getCurrentSectionData());
293
294 // Update the maximum alignment on the current section if necessary.
295 if (ByteAlignment > getCurrentSectionData()->getAlignment())
296 getCurrentSectionData()->setAlignment(ByteAlignment);
297}
298
299void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
300 unsigned MaxBytesToEmit) {
301 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
302 // MCObjectStreamer.
303 if (MaxBytesToEmit == 0)
304 MaxBytesToEmit = ByteAlignment;
305 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
306 getCurrentSectionData());
307 F->setEmitNops(true);
308
309 // Update the maximum alignment on the current section if necessary.
310 if (ByteAlignment > getCurrentSectionData()->getAlignment())
311 getCurrentSectionData()->setAlignment(ByteAlignment);
312}
313
314void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
315 unsigned char Value) {
316 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
317 // MCObjectStreamer.
318 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
319}
320
321// Add a symbol for the file name of this module. This is the second
322// entry in the module's symbol table (the first being the null symbol).
323void MCELFStreamer::EmitFileDirective(StringRef Filename) {
324 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
325 Symbol->setSection(*CurSection);
326 Symbol->setAbsolute();
327
328 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
329
330 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
331}
332
Benjamin Kramer93ded732010-08-27 10:40:51 +0000333void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
334 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
335
336 // Add the fixups and data.
337 //
338 // FIXME: Revisit this design decision when relaxation is done, we may be
339 // able to get away with not storing any extra data in the MCInst.
340 SmallVector<MCFixup, 4> Fixups;
341 SmallString<256> Code;
342 raw_svector_ostream VecOS(Code);
343 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
344 VecOS.flush();
345
346 IF->getCode() = Code;
347 IF->getFixups() = Fixups;
348}
349
350void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
351 MCDataFragment *DF = getOrCreateDataFragment();
352
353 SmallVector<MCFixup, 4> Fixups;
354 SmallString<256> Code;
355 raw_svector_ostream VecOS(Code);
356 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
357 VecOS.flush();
358
359 // Add the fixups and data.
360 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
361 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
362 DF->addFixup(Fixups[i]);
363 }
364 DF->getContents().append(Code.begin(), Code.end());
365}
366
Matt Fleming3565a062010-08-16 18:57:57 +0000367void MCELFStreamer::EmitInstruction(const MCInst &Inst) {
368 // Scan for values.
369 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
370 if (Inst.getOperand(i).isExpr())
371 AddValueSymbols(Inst.getOperand(i).getExpr());
372
373 getCurrentSectionData()->setHasInstructions(true);
374
Benjamin Kramer93ded732010-08-27 10:40:51 +0000375 // If this instruction doesn't need relaxation, just emit it as data.
376 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
377 EmitInstToData(Inst);
Matt Fleming3565a062010-08-16 18:57:57 +0000378 return;
379 }
380
Benjamin Kramer93ded732010-08-27 10:40:51 +0000381 // Otherwise, if we are relaxing everything, relax the instruction as much as
382 // possible and emit it as data.
383 if (getAssembler().getRelaxAll()) {
384 MCInst Relaxed;
385 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
386 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
387 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
388 EmitInstToData(Relaxed);
389 return;
Matt Fleming3565a062010-08-16 18:57:57 +0000390 }
Benjamin Kramer93ded732010-08-27 10:40:51 +0000391
392 // Otherwise emit to a separate fragment.
393 EmitInstToFragment(Inst);
Matt Fleming3565a062010-08-16 18:57:57 +0000394}
395
396void MCELFStreamer::Finish() {
397 getAssembler().Finish();
398}
399
400MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
401 raw_ostream &OS, MCCodeEmitter *CE,
402 bool RelaxAll) {
403 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
404 if (RelaxAll)
405 S->getAssembler().setRelaxAll(true);
406 return S;
407}