blob: 2a12fa240b6080931bbe068d6bd5b10a56de9398 [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
38class MCELFStreamer : public MCObjectStreamer {
39public:
40 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
41 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola59ff3c92010-09-22 22:27:05 +000042 : MCObjectStreamer(Context, TAB, OS, Emitter, false) {}
Matt Fleming3565a062010-08-16 18:57:57 +000043
44 ~MCELFStreamer() {}
45
46 /// @name MCStreamer Interface
47 /// @{
48
Rafael Espindolad80781b2010-09-15 21:48:40 +000049 virtual void InitSections();
Matt Fleming3565a062010-08-16 18:57:57 +000050 virtual void EmitLabel(MCSymbol *Symbol);
51 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
52 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
Rafael Espindola484291c2010-11-01 14:28:48 +000053 virtual void EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +000054 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
55 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
56 assert(0 && "ELF doesn't support this directive");
57 }
58 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
59 unsigned ByteAlignment);
60 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
61 assert(0 && "ELF doesn't support this directive");
62 }
63
64 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
65 assert(0 && "ELF doesn't support this directive");
66 }
67
68 virtual void EmitCOFFSymbolType(int Type) {
69 assert(0 && "ELF doesn't support this directive");
70 }
71
72 virtual void EndCOFFSymbolDef() {
73 assert(0 && "ELF doesn't support this directive");
74 }
75
76 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
77 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
78 SD.setSize(Value);
79 }
80
81 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
82 assert(0 && "ELF doesn't support this directive");
83 }
84 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
85 unsigned Size = 0, unsigned ByteAlignment = 0) {
86 assert(0 && "ELF doesn't support this directive");
87 }
88 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
89 uint64_t Size, unsigned ByteAlignment = 0) {
90 assert(0 && "ELF doesn't support this directive");
91 }
92 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
93 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
94 virtual void EmitGPRel32Value(const MCExpr *Value) {
95 assert(0 && "ELF doesn't support this directive");
96 }
97 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
98 unsigned ValueSize = 1,
99 unsigned MaxBytesToEmit = 0);
100 virtual void EmitCodeAlignment(unsigned ByteAlignment,
101 unsigned MaxBytesToEmit = 0);
102 virtual void EmitValueToOffset(const MCExpr *Offset,
103 unsigned char Value = 0);
104
105 virtual void EmitFileDirective(StringRef Filename);
106 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
107 DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n");
108 }
109
Matt Fleming3565a062010-08-16 18:57:57 +0000110 virtual void Finish();
111
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000112private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000113 virtual void EmitInstToFragment(const MCInst &Inst);
114 virtual void EmitInstToData(const MCInst &Inst);
115
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000116 struct LocalCommon {
117 MCSymbolData *SD;
118 uint64_t Size;
119 unsigned ByteAlignment;
120 };
121 std::vector<LocalCommon> LocalCommons;
122
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000123 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
Matt Fleming3565a062010-08-16 18:57:57 +0000124 /// @}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000125 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
126 SectionKind Kind) {
127 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
128 }
129
130 void SetSectionData() {
131 SetSection(".data", MCSectionELF::SHT_PROGBITS,
132 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
133 SectionKind::getDataRel());
134 EmitCodeAlignment(4, 0);
135 }
136 void SetSectionText() {
137 SetSection(".text", MCSectionELF::SHT_PROGBITS,
138 MCSectionELF::SHF_EXECINSTR |
139 MCSectionELF::SHF_ALLOC, SectionKind::getText());
140 EmitCodeAlignment(4, 0);
141 }
142 void SetSectionBss() {
143 SetSection(".bss", MCSectionELF::SHT_NOBITS,
144 MCSectionELF::SHF_WRITE |
145 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
146 EmitCodeAlignment(4, 0);
147 }
Matt Fleming3565a062010-08-16 18:57:57 +0000148};
149
150} // end anonymous namespace.
151
Rafael Espindolad80781b2010-09-15 21:48:40 +0000152void MCELFStreamer::InitSections() {
153 // This emulates the same behavior of GNU as. This makes it easier
154 // to compare the output as the major sections are in the same order.
155 SetSectionText();
156 SetSectionData();
157 SetSectionBss();
158 SetSectionText();
159}
160
Matt Fleming3565a062010-08-16 18:57:57 +0000161void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
162 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
163
Rafael Espindola73ffea42010-09-25 05:42:19 +0000164 Symbol->setSection(*CurSection);
165
166 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
167
Matt Fleming3565a062010-08-16 18:57:57 +0000168 // FIXME: This is wasteful, we don't necessarily need to create a data
169 // fragment. Instead, we should mark the symbol as pointing into the data
170 // fragment if it exists, otherwise we should just queue the label and set its
171 // fragment pointer when we emit the next fragment.
172 MCDataFragment *F = getOrCreateDataFragment();
Rafael Espindola73ffea42010-09-25 05:42:19 +0000173
Matt Fleming3565a062010-08-16 18:57:57 +0000174 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
175 SD.setFragment(F);
176 SD.setOffset(F->getContents().size());
Matt Fleming3565a062010-08-16 18:57:57 +0000177}
178
179void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
180 switch (Flag) {
Jason W Kimafd1cc22010-09-30 02:45:56 +0000181 case MCAF_SyntaxUnified: return; // no-op here?
Matt Fleming3565a062010-08-16 18:57:57 +0000182 case MCAF_SubsectionsViaSymbols:
183 getAssembler().setSubsectionsViaSymbols(true);
184 return;
185 }
186
187 assert(0 && "invalid assembler flag!");
188}
189
190void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
191 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
192 // MCObjectStreamer.
193 // FIXME: Lift context changes into super class.
194 getAssembler().getOrCreateSymbolData(*Symbol);
195 Symbol->setVariableValue(AddValueSymbols(Value));
196}
197
Rafael Espindola484291c2010-11-01 14:28:48 +0000198// This is a hack. To be able to implement weakrefs the writer has to be able
199// to distinguish
200// .weakref foo, bar
201// .long foo
202// from
203// .weakref foo, bar
204// .long bar
205// since the first case should produce a weak undefined reference and the second
206// one a strong one.
207// If we created foo as a regular alias pointing to bar (foo = bar), then
208// MCExpr::EvaluateAsRelocatable would recurse on foo and the writer would
209// never see it used in a relocation.
210// What we do is create a MCTargetExpr that when evaluated produces a symbol
211// ref to a temporary symbol. This temporary symbol in turn is a variable
212// that equals the original symbol (tmp = bar). With this hack the writer
213// gets a relocation with tmp and can correctly implement weak references.
214
Benjamin Kramerf7985692010-11-05 19:56:38 +0000215namespace {
Rafael Espindola484291c2010-11-01 14:28:48 +0000216class WeakRefExpr : public MCTargetExpr {
217private:
218 const MCSymbolRefExpr *Alias;
219
220 explicit WeakRefExpr(const MCSymbolRefExpr *Alias_)
221 : MCTargetExpr(), Alias(Alias_) {}
222
223public:
224 virtual void PrintImpl(raw_ostream &OS) const {
225 llvm_unreachable("Unimplemented");
226 }
227
228 virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
229 const MCAsmLayout *Layout) const {
230 Res = MCValue::get(Alias, 0, 0);
231 return true;
232 }
233
234 static const WeakRefExpr *Create(const MCSymbol *Alias, MCContext &Ctx) {
235 const MCSymbolRefExpr *A = MCSymbolRefExpr::Create(Alias, Ctx);
236 return new (Ctx) WeakRefExpr(A);
237 }
238};
Benjamin Kramerf7985692010-11-05 19:56:38 +0000239} // end anonymous namespace
Rafael Espindola484291c2010-11-01 14:28:48 +0000240
241void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
242 getAssembler().getOrCreateSymbolData(*Symbol);
243 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
244 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
245
246 // Create the alias that actually points to Symbol
247 const MCSymbolRefExpr *SymRef = MCSymbolRefExpr::Create(Symbol, getContext());
248 MCSymbol *RealAlias = getContext().CreateTempSymbol();
249 RealAlias->setVariableValue(SymRef);
250
251 MCSymbolData &RealAliasSD = getAssembler().getOrCreateSymbolData(*RealAlias);
252 RealAliasSD.setFlags(RealAliasSD.getFlags() | ELF_Other_Weakref);
253
254 const MCExpr *Value = WeakRefExpr::Create(RealAlias, getContext());
255 Alias->setVariableValue(Value);
256}
257
Rafael Espindolaf7133842010-09-13 17:39:45 +0000258static void SetBinding(MCSymbolData &SD, unsigned Binding) {
259 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
260 Binding == ELF::STB_WEAK);
261 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
262 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
263}
264
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000265static unsigned GetBinding(const MCSymbolData &SD) {
266 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
267 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
268 Binding == ELF::STB_WEAK);
269 return Binding;
270}
271
Rafael Espindolaf7133842010-09-13 17:39:45 +0000272static void SetType(MCSymbolData &SD, unsigned Type) {
273 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
274 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
275 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
276 Type == ELF::STT_TLS);
277
278 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STT_Shift);
279 SD.setFlags(OtherFlags | (Type << ELF_STT_Shift));
280}
281
282static void SetVisibility(MCSymbolData &SD, unsigned Visibility) {
283 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
284 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
285
286 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STV_Shift);
287 SD.setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
288}
289
Matt Fleming3565a062010-08-16 18:57:57 +0000290void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
291 MCSymbolAttr Attribute) {
292 // Indirect symbols are handled differently, to match how 'as' handles
293 // them. This makes writing matching .o files easier.
294 if (Attribute == MCSA_IndirectSymbol) {
295 // Note that we intentionally cannot use the symbol data here; this is
296 // important for matching the string table that 'as' generates.
297 IndirectSymbolData ISD;
298 ISD.Symbol = Symbol;
299 ISD.SectionData = getCurrentSectionData();
300 getAssembler().getIndirectSymbols().push_back(ISD);
301 return;
302 }
303
304 // Adding a symbol attribute always introduces the symbol, note that an
305 // important side effect of calling getOrCreateSymbolData here is to register
306 // the symbol with the assembler.
307 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
308
309 // The implementation of symbol attributes is designed to match 'as', but it
310 // leaves much to desired. It doesn't really make sense to arbitrarily add and
311 // remove flags, but 'as' allows this (in particular, see .desc).
312 //
313 // In the future it might be worth trying to make these operations more well
314 // defined.
315 switch (Attribute) {
316 case MCSA_LazyReference:
317 case MCSA_Reference:
318 case MCSA_NoDeadStrip:
319 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000320 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000321 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000322 case MCSA_Invalid:
323 case MCSA_ELF_TypeIndFunction:
324 case MCSA_IndirectSymbol:
325 assert(0 && "Invalid symbol attribute for ELF!");
326 break;
327
328 case MCSA_Global:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000329 SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000330 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000331 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000332 break;
333
Benjamin Kramer230c2742010-09-02 17:18:32 +0000334 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000335 case MCSA_Weak:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000336 SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000337 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000338 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000339 break;
340
341 case MCSA_Local:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000342 SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000343 SD.setExternal(false);
344 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000345 break;
346
347 case MCSA_ELF_TypeFunction:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000348 SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000349 break;
350
351 case MCSA_ELF_TypeObject:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000352 SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000353 break;
354
355 case MCSA_ELF_TypeTLS:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000356 SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000357 break;
358
359 case MCSA_ELF_TypeCommon:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000360 SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000361 break;
362
363 case MCSA_ELF_TypeNoType:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000364 SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000365 break;
366
367 case MCSA_Protected:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000368 SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000369 break;
370
371 case MCSA_Hidden:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000372 SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000373 break;
374
375 case MCSA_Internal:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000376 SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000377 break;
378 }
379}
380
381void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
382 unsigned ByteAlignment) {
383 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
384
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000385 if (!BindingExplicitlySet.count(Symbol)) {
386 SetBinding(SD, ELF::STB_GLOBAL);
387 SD.setExternal(true);
388 }
389
390 if (GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000391 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
392 MCSectionELF::SHT_NOBITS,
393 MCSectionELF::SHF_WRITE |
394 MCSectionELF::SHF_ALLOC,
395 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000396 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000397
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000398 struct LocalCommon L = {&SD, Size, ByteAlignment};
399 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000400 } else {
401 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000402 }
403
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000404 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000405}
406
407void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
408 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
409 // MCObjectStreamer.
410 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
411}
412
413void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
414 unsigned AddrSpace) {
415 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
416 // MCObjectStreamer.
417 MCDataFragment *DF = getOrCreateDataFragment();
418
419 // Avoid fixups when possible.
420 int64_t AbsValue;
421 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
422 // FIXME: Endianness assumption.
423 for (unsigned i = 0; i != Size; ++i)
424 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
425 } else {
426 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
427 MCFixup::getKindForSize(Size)));
428 DF->getContents().resize(DF->getContents().size() + Size, 0);
429 }
430}
431
432void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
433 int64_t Value, unsigned ValueSize,
434 unsigned MaxBytesToEmit) {
435 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
436 // MCObjectStreamer.
437 if (MaxBytesToEmit == 0)
438 MaxBytesToEmit = ByteAlignment;
439 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
440 getCurrentSectionData());
441
442 // Update the maximum alignment on the current section if necessary.
443 if (ByteAlignment > getCurrentSectionData()->getAlignment())
444 getCurrentSectionData()->setAlignment(ByteAlignment);
445}
446
447void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
448 unsigned MaxBytesToEmit) {
449 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
450 // MCObjectStreamer.
451 if (MaxBytesToEmit == 0)
452 MaxBytesToEmit = ByteAlignment;
453 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
454 getCurrentSectionData());
455 F->setEmitNops(true);
456
457 // Update the maximum alignment on the current section if necessary.
458 if (ByteAlignment > getCurrentSectionData()->getAlignment())
459 getCurrentSectionData()->setAlignment(ByteAlignment);
460}
461
462void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
463 unsigned char Value) {
464 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
465 // MCObjectStreamer.
466 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
467}
468
469// Add a symbol for the file name of this module. This is the second
470// entry in the module's symbol table (the first being the null symbol).
471void MCELFStreamer::EmitFileDirective(StringRef Filename) {
472 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
473 Symbol->setSection(*CurSection);
474 Symbol->setAbsolute();
475
476 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
477
478 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
479}
480
Benjamin Kramer93ded732010-08-27 10:40:51 +0000481void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
482 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
483
484 // Add the fixups and data.
485 //
486 // FIXME: Revisit this design decision when relaxation is done, we may be
487 // able to get away with not storing any extra data in the MCInst.
488 SmallVector<MCFixup, 4> Fixups;
489 SmallString<256> Code;
490 raw_svector_ostream VecOS(Code);
491 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
492 VecOS.flush();
493
494 IF->getCode() = Code;
495 IF->getFixups() = Fixups;
496}
497
498void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
499 MCDataFragment *DF = getOrCreateDataFragment();
500
501 SmallVector<MCFixup, 4> Fixups;
502 SmallString<256> Code;
503 raw_svector_ostream VecOS(Code);
504 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
505 VecOS.flush();
506
507 // Add the fixups and data.
508 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
509 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
510 DF->addFixup(Fixups[i]);
511 }
512 DF->getContents().append(Code.begin(), Code.end());
513}
514
Matt Fleming3565a062010-08-16 18:57:57 +0000515void MCELFStreamer::Finish() {
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000516 // FIXME: duplicated code with the MachO streamer.
517 // Dump out the dwarf file & directory tables and line tables.
518 if (getContext().hasDwarfFiles()) {
519 const MCSection *DwarfLineSection =
520 getContext().getELFSection(".debug_line", 0, 0,
521 SectionKind::getDataRelLocal());
522 MCDwarfFileTable::Emit(this, DwarfLineSection);
523 }
524
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000525 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
526 e = LocalCommons.end();
527 i != e; ++i) {
528 MCSymbolData *SD = i->SD;
529 uint64_t Size = i->Size;
530 unsigned ByteAlignment = i->ByteAlignment;
531 const MCSymbol &Symbol = SD->getSymbol();
532 const MCSection &Section = Symbol.getSection();
533
534 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
535 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
536
537 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
538 SD->setFragment(F);
539
540 // Update the maximum alignment of the section if necessary.
541 if (ByteAlignment > SectData.getAlignment())
542 SectData.setAlignment(ByteAlignment);
543 }
544
Rafael Espindola73ffea42010-09-25 05:42:19 +0000545 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000546}
547
548MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
549 raw_ostream &OS, MCCodeEmitter *CE,
550 bool RelaxAll) {
551 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
552 if (RelaxAll)
553 S->getAssembler().setRelaxAll(true);
554 return S;
555}