blob: 75e58de3199ab4ce629d4773ae03cccfc6949527 [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
Rafael Espindolaf7c10a32010-09-21 00:24:38 +000016#include "llvm/ADT/SmallPtrSet.h"
Matt Fleming3565a062010-08-16 18:57:57 +000017#include "llvm/MC/MCAssembler.h"
18#include "llvm/MC/MCContext.h"
19#include "llvm/MC/MCCodeEmitter.h"
20#include "llvm/MC/MCELFSymbolFlags.h"
21#include "llvm/MC/MCExpr.h"
22#include "llvm/MC/MCInst.h"
23#include "llvm/MC/MCObjectStreamer.h"
24#include "llvm/MC/MCSection.h"
25#include "llvm/MC/MCSectionELF.h"
26#include "llvm/MC/MCSymbol.h"
Rafael Espindola484291c2010-11-01 14:28:48 +000027#include "llvm/MC/MCValue.h"
Matt Fleming3565a062010-08-16 18:57:57 +000028#include "llvm/Support/Debug.h"
29#include "llvm/Support/ELF.h"
30#include "llvm/Support/ErrorHandling.h"
31#include "llvm/Support/raw_ostream.h"
32#include "llvm/Target/TargetAsmBackend.h"
33
34using namespace llvm;
35
36namespace {
37
Rafael Espindolae1a25872010-11-11 19:04:55 +000038static void SetBinding(MCSymbolData &SD, unsigned Binding) {
39 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
40 Binding == ELF::STB_WEAK);
41 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
42 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
43}
44
45static unsigned GetBinding(const MCSymbolData &SD) {
46 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
47 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
48 Binding == ELF::STB_WEAK);
49 return Binding;
50}
51
52static void SetType(MCSymbolData &SD, unsigned Type) {
53 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
54 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
55 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
56 Type == ELF::STT_TLS);
57
58 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STT_Shift);
59 SD.setFlags(OtherFlags | (Type << ELF_STT_Shift));
60}
61
62static void SetVisibility(MCSymbolData &SD, unsigned Visibility) {
63 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
64 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
65
66 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STV_Shift);
67 SD.setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
68}
69
Matt Fleming3565a062010-08-16 18:57:57 +000070class MCELFStreamer : public MCObjectStreamer {
71public:
72 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
73 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola59ff3c92010-09-22 22:27:05 +000074 : MCObjectStreamer(Context, TAB, OS, Emitter, false) {}
Matt Fleming3565a062010-08-16 18:57:57 +000075
76 ~MCELFStreamer() {}
77
78 /// @name MCStreamer Interface
79 /// @{
80
Rafael Espindolad80781b2010-09-15 21:48:40 +000081 virtual void InitSections();
Matt Fleming3565a062010-08-16 18:57:57 +000082 virtual void EmitLabel(MCSymbol *Symbol);
83 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
Jim Grosbachce792992010-11-05 22:08:08 +000084 virtual void EmitThumbFunc(MCSymbol *Func);
Matt Fleming3565a062010-08-16 18:57:57 +000085 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +000086 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Rafael Espindola1f4f9e32010-11-14 04:17:37 +000087 virtual void SwitchSection(const MCSection *Section);
Matt Fleming3565a062010-08-16 18:57:57 +000088 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
89 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
90 assert(0 && "ELF doesn't support this directive");
91 }
92 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
93 unsigned ByteAlignment);
94 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
95 assert(0 && "ELF doesn't support this directive");
96 }
97
98 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
99 assert(0 && "ELF doesn't support this directive");
100 }
101
102 virtual void EmitCOFFSymbolType(int Type) {
103 assert(0 && "ELF doesn't support this directive");
104 }
105
106 virtual void EndCOFFSymbolDef() {
107 assert(0 && "ELF doesn't support this directive");
108 }
109
110 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
111 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
112 SD.setSize(Value);
113 }
114
115 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
116 assert(0 && "ELF doesn't support this directive");
117 }
118 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
119 unsigned Size = 0, unsigned ByteAlignment = 0) {
120 assert(0 && "ELF doesn't support this directive");
121 }
122 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
123 uint64_t Size, unsigned ByteAlignment = 0) {
124 assert(0 && "ELF doesn't support this directive");
125 }
126 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
127 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
128 virtual void EmitGPRel32Value(const MCExpr *Value) {
129 assert(0 && "ELF doesn't support this directive");
130 }
131 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
132 unsigned ValueSize = 1,
133 unsigned MaxBytesToEmit = 0);
134 virtual void EmitCodeAlignment(unsigned ByteAlignment,
135 unsigned MaxBytesToEmit = 0);
136 virtual void EmitValueToOffset(const MCExpr *Offset,
137 unsigned char Value = 0);
138
139 virtual void EmitFileDirective(StringRef Filename);
Matt Fleming3565a062010-08-16 18:57:57 +0000140
Matt Fleming3565a062010-08-16 18:57:57 +0000141 virtual void Finish();
142
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000143private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000144 virtual void EmitInstToFragment(const MCInst &Inst);
145 virtual void EmitInstToData(const MCInst &Inst);
146
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000147 struct LocalCommon {
148 MCSymbolData *SD;
149 uint64_t Size;
150 unsigned ByteAlignment;
151 };
152 std::vector<LocalCommon> LocalCommons;
153
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000154 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
Matt Fleming3565a062010-08-16 18:57:57 +0000155 /// @}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000156 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
157 SectionKind Kind) {
158 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
159 }
160
161 void SetSectionData() {
162 SetSection(".data", MCSectionELF::SHT_PROGBITS,
163 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
164 SectionKind::getDataRel());
165 EmitCodeAlignment(4, 0);
166 }
167 void SetSectionText() {
168 SetSection(".text", MCSectionELF::SHT_PROGBITS,
169 MCSectionELF::SHF_EXECINSTR |
170 MCSectionELF::SHF_ALLOC, SectionKind::getText());
171 EmitCodeAlignment(4, 0);
172 }
173 void SetSectionBss() {
174 SetSection(".bss", MCSectionELF::SHT_NOBITS,
175 MCSectionELF::SHF_WRITE |
176 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
177 EmitCodeAlignment(4, 0);
178 }
Matt Fleming3565a062010-08-16 18:57:57 +0000179};
180
181} // end anonymous namespace.
182
Rafael Espindolad80781b2010-09-15 21:48:40 +0000183void MCELFStreamer::InitSections() {
184 // This emulates the same behavior of GNU as. This makes it easier
185 // to compare the output as the major sections are in the same order.
186 SetSectionText();
187 SetSectionData();
188 SetSectionBss();
189 SetSectionText();
190}
191
Matt Fleming3565a062010-08-16 18:57:57 +0000192void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
193 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
194
Rafael Espindola73ffea42010-09-25 05:42:19 +0000195 Symbol->setSection(*CurSection);
196
197 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
198
Rafael Espindolae1a25872010-11-11 19:04:55 +0000199 const MCSectionELF &Section =
200 static_cast<const MCSectionELF&>(Symbol->getSection());
201 if (Section.getFlags() & MCSectionELF::SHF_TLS)
202 SetType(SD, ELF::STT_TLS);
203
Matt Fleming3565a062010-08-16 18:57:57 +0000204 // FIXME: This is wasteful, we don't necessarily need to create a data
205 // fragment. Instead, we should mark the symbol as pointing into the data
206 // fragment if it exists, otherwise we should just queue the label and set its
207 // fragment pointer when we emit the next fragment.
208 MCDataFragment *F = getOrCreateDataFragment();
Rafael Espindola73ffea42010-09-25 05:42:19 +0000209
Matt Fleming3565a062010-08-16 18:57:57 +0000210 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
211 SD.setFragment(F);
212 SD.setOffset(F->getContents().size());
Matt Fleming3565a062010-08-16 18:57:57 +0000213}
214
215void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
216 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000217 case MCAF_SyntaxUnified: return; // no-op here.
218 case MCAF_Code16: return; // no-op here.
Jim Grosbachba219572010-11-05 22:40:09 +0000219 case MCAF_Code32: return; // no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000220 case MCAF_SubsectionsViaSymbols:
221 getAssembler().setSubsectionsViaSymbols(true);
222 return;
223 }
224
225 assert(0 && "invalid assembler flag!");
226}
227
Jim Grosbachce792992010-11-05 22:08:08 +0000228void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
229 // FIXME: Anything needed here to flag the function as thumb?
230}
231
Matt Fleming3565a062010-08-16 18:57:57 +0000232void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
233 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
234 // MCObjectStreamer.
235 // FIXME: Lift context changes into super class.
236 getAssembler().getOrCreateSymbolData(*Symbol);
237 Symbol->setVariableValue(AddValueSymbols(Value));
238}
239
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000240void MCELFStreamer::SwitchSection(const MCSection *Section) {
241 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
242 if (Grp)
243 getAssembler().getOrCreateSymbolData(*Grp);
244 this->MCObjectStreamer::SwitchSection(Section);
245}
246
Rafael Espindola484291c2010-11-01 14:28:48 +0000247void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
248 getAssembler().getOrCreateSymbolData(*Symbol);
249 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
250 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
Rafael Espindola94ed5fc2010-11-15 16:33:49 +0000251 const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
Rafael Espindola484291c2010-11-01 14:28:48 +0000252 Alias->setVariableValue(Value);
253}
254
Matt Fleming3565a062010-08-16 18:57:57 +0000255void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
256 MCSymbolAttr Attribute) {
257 // Indirect symbols are handled differently, to match how 'as' handles
258 // them. This makes writing matching .o files easier.
259 if (Attribute == MCSA_IndirectSymbol) {
260 // Note that we intentionally cannot use the symbol data here; this is
261 // important for matching the string table that 'as' generates.
262 IndirectSymbolData ISD;
263 ISD.Symbol = Symbol;
264 ISD.SectionData = getCurrentSectionData();
265 getAssembler().getIndirectSymbols().push_back(ISD);
266 return;
267 }
268
269 // Adding a symbol attribute always introduces the symbol, note that an
270 // important side effect of calling getOrCreateSymbolData here is to register
271 // the symbol with the assembler.
272 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
273
274 // The implementation of symbol attributes is designed to match 'as', but it
275 // leaves much to desired. It doesn't really make sense to arbitrarily add and
276 // remove flags, but 'as' allows this (in particular, see .desc).
277 //
278 // In the future it might be worth trying to make these operations more well
279 // defined.
280 switch (Attribute) {
281 case MCSA_LazyReference:
282 case MCSA_Reference:
283 case MCSA_NoDeadStrip:
284 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000285 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000286 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000287 case MCSA_Invalid:
288 case MCSA_ELF_TypeIndFunction:
289 case MCSA_IndirectSymbol:
290 assert(0 && "Invalid symbol attribute for ELF!");
291 break;
292
Rafael Espindola7e528a12010-11-14 01:34:31 +0000293 case MCSA_ELF_TypeGnuUniqueObject:
294 // Ignore for now.
295 break;
296
Matt Fleming3565a062010-08-16 18:57:57 +0000297 case MCSA_Global:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000298 SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000299 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000300 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000301 break;
302
Benjamin Kramer230c2742010-09-02 17:18:32 +0000303 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000304 case MCSA_Weak:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000305 SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000306 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000307 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000308 break;
309
310 case MCSA_Local:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000311 SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000312 SD.setExternal(false);
313 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000314 break;
315
316 case MCSA_ELF_TypeFunction:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000317 SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000318 break;
319
320 case MCSA_ELF_TypeObject:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000321 SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000322 break;
323
324 case MCSA_ELF_TypeTLS:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000325 SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000326 break;
327
328 case MCSA_ELF_TypeCommon:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000329 SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000330 break;
331
332 case MCSA_ELF_TypeNoType:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000333 SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000334 break;
335
336 case MCSA_Protected:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000337 SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000338 break;
339
340 case MCSA_Hidden:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000341 SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000342 break;
343
344 case MCSA_Internal:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000345 SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000346 break;
347 }
348}
349
350void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
351 unsigned ByteAlignment) {
352 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
353
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000354 if (!BindingExplicitlySet.count(Symbol)) {
355 SetBinding(SD, ELF::STB_GLOBAL);
356 SD.setExternal(true);
357 }
358
Rafael Espindola55d02f32010-11-14 21:11:16 +0000359 SetType(SD, ELF::STT_OBJECT);
360
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000361 if (GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000362 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
363 MCSectionELF::SHT_NOBITS,
364 MCSectionELF::SHF_WRITE |
365 MCSectionELF::SHF_ALLOC,
366 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000367 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000368
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000369 struct LocalCommon L = {&SD, Size, ByteAlignment};
370 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000371 } else {
372 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000373 }
374
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000375 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000376}
377
378void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
379 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
380 // MCObjectStreamer.
381 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
382}
383
384void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
385 unsigned AddrSpace) {
386 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
387 // MCObjectStreamer.
388 MCDataFragment *DF = getOrCreateDataFragment();
389
390 // Avoid fixups when possible.
391 int64_t AbsValue;
392 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
393 // FIXME: Endianness assumption.
394 for (unsigned i = 0; i != Size; ++i)
395 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
396 } else {
397 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
398 MCFixup::getKindForSize(Size)));
399 DF->getContents().resize(DF->getContents().size() + Size, 0);
400 }
401}
402
403void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
404 int64_t Value, unsigned ValueSize,
405 unsigned MaxBytesToEmit) {
406 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
407 // MCObjectStreamer.
408 if (MaxBytesToEmit == 0)
409 MaxBytesToEmit = ByteAlignment;
410 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
411 getCurrentSectionData());
412
413 // Update the maximum alignment on the current section if necessary.
414 if (ByteAlignment > getCurrentSectionData()->getAlignment())
415 getCurrentSectionData()->setAlignment(ByteAlignment);
416}
417
418void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
419 unsigned MaxBytesToEmit) {
420 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
421 // MCObjectStreamer.
422 if (MaxBytesToEmit == 0)
423 MaxBytesToEmit = ByteAlignment;
424 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
425 getCurrentSectionData());
426 F->setEmitNops(true);
427
428 // Update the maximum alignment on the current section if necessary.
429 if (ByteAlignment > getCurrentSectionData()->getAlignment())
430 getCurrentSectionData()->setAlignment(ByteAlignment);
431}
432
433void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
434 unsigned char Value) {
435 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
436 // MCObjectStreamer.
437 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
438}
439
440// Add a symbol for the file name of this module. This is the second
441// entry in the module's symbol table (the first being the null symbol).
442void MCELFStreamer::EmitFileDirective(StringRef Filename) {
443 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
444 Symbol->setSection(*CurSection);
445 Symbol->setAbsolute();
446
447 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
448
449 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
450}
451
Benjamin Kramer93ded732010-08-27 10:40:51 +0000452void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
453 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
454
455 // Add the fixups and data.
456 //
457 // FIXME: Revisit this design decision when relaxation is done, we may be
458 // able to get away with not storing any extra data in the MCInst.
459 SmallVector<MCFixup, 4> Fixups;
460 SmallString<256> Code;
461 raw_svector_ostream VecOS(Code);
462 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
463 VecOS.flush();
464
465 IF->getCode() = Code;
466 IF->getFixups() = Fixups;
467}
468
469void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
470 MCDataFragment *DF = getOrCreateDataFragment();
471
472 SmallVector<MCFixup, 4> Fixups;
473 SmallString<256> Code;
474 raw_svector_ostream VecOS(Code);
475 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
476 VecOS.flush();
477
478 // Add the fixups and data.
479 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
480 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
481 DF->addFixup(Fixups[i]);
482 }
483 DF->getContents().append(Code.begin(), Code.end());
484}
485
Matt Fleming3565a062010-08-16 18:57:57 +0000486void MCELFStreamer::Finish() {
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000487 // FIXME: duplicated code with the MachO streamer.
488 // Dump out the dwarf file & directory tables and line tables.
489 if (getContext().hasDwarfFiles()) {
490 const MCSection *DwarfLineSection =
491 getContext().getELFSection(".debug_line", 0, 0,
492 SectionKind::getDataRelLocal());
493 MCDwarfFileTable::Emit(this, DwarfLineSection);
494 }
495
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000496 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
497 e = LocalCommons.end();
498 i != e; ++i) {
499 MCSymbolData *SD = i->SD;
500 uint64_t Size = i->Size;
501 unsigned ByteAlignment = i->ByteAlignment;
502 const MCSymbol &Symbol = SD->getSymbol();
503 const MCSection &Section = Symbol.getSection();
504
505 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
506 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
507
508 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
509 SD->setFragment(F);
510
511 // Update the maximum alignment of the section if necessary.
512 if (ByteAlignment > SectData.getAlignment())
513 SectData.setAlignment(ByteAlignment);
514 }
515
Rafael Espindola73ffea42010-09-25 05:42:19 +0000516 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000517}
518
519MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
520 raw_ostream &OS, MCCodeEmitter *CE,
521 bool RelaxAll) {
522 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
523 if (RelaxAll)
524 S->getAssembler().setRelaxAll(true);
525 return S;
526}