blob: 4acf45cb68fa6a7e232c57dc408772a8b6f31f2e [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);
Matt Fleming3565a062010-08-16 18:57:57 +000087 virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
88 virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
89 assert(0 && "ELF doesn't support this directive");
90 }
91 virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
92 unsigned ByteAlignment);
93 virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
94 assert(0 && "ELF doesn't support this directive");
95 }
96
97 virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
98 assert(0 && "ELF doesn't support this directive");
99 }
100
101 virtual void EmitCOFFSymbolType(int Type) {
102 assert(0 && "ELF doesn't support this directive");
103 }
104
105 virtual void EndCOFFSymbolDef() {
106 assert(0 && "ELF doesn't support this directive");
107 }
108
109 virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
110 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
111 SD.setSize(Value);
112 }
113
114 virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
115 assert(0 && "ELF doesn't support this directive");
116 }
117 virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
118 unsigned Size = 0, unsigned ByteAlignment = 0) {
119 assert(0 && "ELF doesn't support this directive");
120 }
121 virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
122 uint64_t Size, unsigned ByteAlignment = 0) {
123 assert(0 && "ELF doesn't support this directive");
124 }
125 virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
126 virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
127 virtual void EmitGPRel32Value(const MCExpr *Value) {
128 assert(0 && "ELF doesn't support this directive");
129 }
130 virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
131 unsigned ValueSize = 1,
132 unsigned MaxBytesToEmit = 0);
133 virtual void EmitCodeAlignment(unsigned ByteAlignment,
134 unsigned MaxBytesToEmit = 0);
135 virtual void EmitValueToOffset(const MCExpr *Offset,
136 unsigned char Value = 0);
137
138 virtual void EmitFileDirective(StringRef Filename);
139 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
140 DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n");
141 }
142
Matt Fleming3565a062010-08-16 18:57:57 +0000143 virtual void Finish();
144
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000145private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000146 virtual void EmitInstToFragment(const MCInst &Inst);
147 virtual void EmitInstToData(const MCInst &Inst);
148
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000149 struct LocalCommon {
150 MCSymbolData *SD;
151 uint64_t Size;
152 unsigned ByteAlignment;
153 };
154 std::vector<LocalCommon> LocalCommons;
155
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000156 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
Matt Fleming3565a062010-08-16 18:57:57 +0000157 /// @}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000158 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
159 SectionKind Kind) {
160 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
161 }
162
163 void SetSectionData() {
164 SetSection(".data", MCSectionELF::SHT_PROGBITS,
165 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
166 SectionKind::getDataRel());
167 EmitCodeAlignment(4, 0);
168 }
169 void SetSectionText() {
170 SetSection(".text", MCSectionELF::SHT_PROGBITS,
171 MCSectionELF::SHF_EXECINSTR |
172 MCSectionELF::SHF_ALLOC, SectionKind::getText());
173 EmitCodeAlignment(4, 0);
174 }
175 void SetSectionBss() {
176 SetSection(".bss", MCSectionELF::SHT_NOBITS,
177 MCSectionELF::SHF_WRITE |
178 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
179 EmitCodeAlignment(4, 0);
180 }
Matt Fleming3565a062010-08-16 18:57:57 +0000181};
182
183} // end anonymous namespace.
184
Rafael Espindolad80781b2010-09-15 21:48:40 +0000185void MCELFStreamer::InitSections() {
186 // This emulates the same behavior of GNU as. This makes it easier
187 // to compare the output as the major sections are in the same order.
188 SetSectionText();
189 SetSectionData();
190 SetSectionBss();
191 SetSectionText();
192}
193
Matt Fleming3565a062010-08-16 18:57:57 +0000194void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
195 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
196
Rafael Espindola73ffea42010-09-25 05:42:19 +0000197 Symbol->setSection(*CurSection);
198
199 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
200
Rafael Espindolae1a25872010-11-11 19:04:55 +0000201 const MCSectionELF &Section =
202 static_cast<const MCSectionELF&>(Symbol->getSection());
203 if (Section.getFlags() & MCSectionELF::SHF_TLS)
204 SetType(SD, ELF::STT_TLS);
205
Matt Fleming3565a062010-08-16 18:57:57 +0000206 // FIXME: This is wasteful, we don't necessarily need to create a data
207 // fragment. Instead, we should mark the symbol as pointing into the data
208 // fragment if it exists, otherwise we should just queue the label and set its
209 // fragment pointer when we emit the next fragment.
210 MCDataFragment *F = getOrCreateDataFragment();
Rafael Espindola73ffea42010-09-25 05:42:19 +0000211
Matt Fleming3565a062010-08-16 18:57:57 +0000212 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
213 SD.setFragment(F);
214 SD.setOffset(F->getContents().size());
Matt Fleming3565a062010-08-16 18:57:57 +0000215}
216
217void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
218 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000219 case MCAF_SyntaxUnified: return; // no-op here.
220 case MCAF_Code16: return; // no-op here.
Jim Grosbachba219572010-11-05 22:40:09 +0000221 case MCAF_Code32: return; // no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000222 case MCAF_SubsectionsViaSymbols:
223 getAssembler().setSubsectionsViaSymbols(true);
224 return;
225 }
226
227 assert(0 && "invalid assembler flag!");
228}
229
Jim Grosbachce792992010-11-05 22:08:08 +0000230void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
231 // FIXME: Anything needed here to flag the function as thumb?
232}
233
Matt Fleming3565a062010-08-16 18:57:57 +0000234void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
235 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
236 // MCObjectStreamer.
237 // FIXME: Lift context changes into super class.
238 getAssembler().getOrCreateSymbolData(*Symbol);
239 Symbol->setVariableValue(AddValueSymbols(Value));
240}
241
Rafael Espindola484291c2010-11-01 14:28:48 +0000242// This is a hack. To be able to implement weakrefs the writer has to be able
243// to distinguish
244// .weakref foo, bar
245// .long foo
246// from
247// .weakref foo, bar
248// .long bar
249// since the first case should produce a weak undefined reference and the second
250// one a strong one.
251// If we created foo as a regular alias pointing to bar (foo = bar), then
252// MCExpr::EvaluateAsRelocatable would recurse on foo and the writer would
253// never see it used in a relocation.
254// What we do is create a MCTargetExpr that when evaluated produces a symbol
255// ref to a temporary symbol. This temporary symbol in turn is a variable
256// that equals the original symbol (tmp = bar). With this hack the writer
257// gets a relocation with tmp and can correctly implement weak references.
258
Benjamin Kramerf7985692010-11-05 19:56:38 +0000259namespace {
Rafael Espindola484291c2010-11-01 14:28:48 +0000260class WeakRefExpr : public MCTargetExpr {
261private:
262 const MCSymbolRefExpr *Alias;
263
264 explicit WeakRefExpr(const MCSymbolRefExpr *Alias_)
265 : MCTargetExpr(), Alias(Alias_) {}
266
267public:
268 virtual void PrintImpl(raw_ostream &OS) const {
269 llvm_unreachable("Unimplemented");
270 }
271
272 virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
273 const MCAsmLayout *Layout) const {
274 Res = MCValue::get(Alias, 0, 0);
275 return true;
276 }
277
278 static const WeakRefExpr *Create(const MCSymbol *Alias, MCContext &Ctx) {
279 const MCSymbolRefExpr *A = MCSymbolRefExpr::Create(Alias, Ctx);
280 return new (Ctx) WeakRefExpr(A);
281 }
282};
Benjamin Kramerf7985692010-11-05 19:56:38 +0000283} // end anonymous namespace
Rafael Espindola484291c2010-11-01 14:28:48 +0000284
285void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
286 getAssembler().getOrCreateSymbolData(*Symbol);
287 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
288 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
289
290 // Create the alias that actually points to Symbol
291 const MCSymbolRefExpr *SymRef = MCSymbolRefExpr::Create(Symbol, getContext());
292 MCSymbol *RealAlias = getContext().CreateTempSymbol();
293 RealAlias->setVariableValue(SymRef);
294
295 MCSymbolData &RealAliasSD = getAssembler().getOrCreateSymbolData(*RealAlias);
296 RealAliasSD.setFlags(RealAliasSD.getFlags() | ELF_Other_Weakref);
297
298 const MCExpr *Value = WeakRefExpr::Create(RealAlias, getContext());
299 Alias->setVariableValue(Value);
300}
301
Matt Fleming3565a062010-08-16 18:57:57 +0000302void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
303 MCSymbolAttr Attribute) {
304 // Indirect symbols are handled differently, to match how 'as' handles
305 // them. This makes writing matching .o files easier.
306 if (Attribute == MCSA_IndirectSymbol) {
307 // Note that we intentionally cannot use the symbol data here; this is
308 // important for matching the string table that 'as' generates.
309 IndirectSymbolData ISD;
310 ISD.Symbol = Symbol;
311 ISD.SectionData = getCurrentSectionData();
312 getAssembler().getIndirectSymbols().push_back(ISD);
313 return;
314 }
315
316 // Adding a symbol attribute always introduces the symbol, note that an
317 // important side effect of calling getOrCreateSymbolData here is to register
318 // the symbol with the assembler.
319 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
320
321 // The implementation of symbol attributes is designed to match 'as', but it
322 // leaves much to desired. It doesn't really make sense to arbitrarily add and
323 // remove flags, but 'as' allows this (in particular, see .desc).
324 //
325 // In the future it might be worth trying to make these operations more well
326 // defined.
327 switch (Attribute) {
328 case MCSA_LazyReference:
329 case MCSA_Reference:
330 case MCSA_NoDeadStrip:
331 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000332 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000333 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000334 case MCSA_Invalid:
335 case MCSA_ELF_TypeIndFunction:
336 case MCSA_IndirectSymbol:
337 assert(0 && "Invalid symbol attribute for ELF!");
338 break;
339
340 case MCSA_Global:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000341 SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000342 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000343 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000344 break;
345
Benjamin Kramer230c2742010-09-02 17:18:32 +0000346 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000347 case MCSA_Weak:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000348 SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000349 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000350 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000351 break;
352
353 case MCSA_Local:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000354 SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000355 SD.setExternal(false);
356 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000357 break;
358
359 case MCSA_ELF_TypeFunction:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000360 SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000361 break;
362
363 case MCSA_ELF_TypeObject:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000364 SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000365 break;
366
367 case MCSA_ELF_TypeTLS:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000368 SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000369 break;
370
371 case MCSA_ELF_TypeCommon:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000372 SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000373 break;
374
375 case MCSA_ELF_TypeNoType:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000376 SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000377 break;
378
379 case MCSA_Protected:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000380 SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000381 break;
382
383 case MCSA_Hidden:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000384 SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000385 break;
386
387 case MCSA_Internal:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000388 SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000389 break;
390 }
391}
392
393void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
394 unsigned ByteAlignment) {
395 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
396
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000397 if (!BindingExplicitlySet.count(Symbol)) {
398 SetBinding(SD, ELF::STB_GLOBAL);
399 SD.setExternal(true);
400 }
401
402 if (GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000403 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
404 MCSectionELF::SHT_NOBITS,
405 MCSectionELF::SHF_WRITE |
406 MCSectionELF::SHF_ALLOC,
407 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000408 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000409
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000410 struct LocalCommon L = {&SD, Size, ByteAlignment};
411 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000412 } else {
413 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000414 }
415
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000416 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000417}
418
419void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
420 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
421 // MCObjectStreamer.
422 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
423}
424
425void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
426 unsigned AddrSpace) {
427 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
428 // MCObjectStreamer.
429 MCDataFragment *DF = getOrCreateDataFragment();
430
431 // Avoid fixups when possible.
432 int64_t AbsValue;
433 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
434 // FIXME: Endianness assumption.
435 for (unsigned i = 0; i != Size; ++i)
436 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
437 } else {
438 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
439 MCFixup::getKindForSize(Size)));
440 DF->getContents().resize(DF->getContents().size() + Size, 0);
441 }
442}
443
444void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
445 int64_t Value, unsigned ValueSize,
446 unsigned MaxBytesToEmit) {
447 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
448 // MCObjectStreamer.
449 if (MaxBytesToEmit == 0)
450 MaxBytesToEmit = ByteAlignment;
451 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
452 getCurrentSectionData());
453
454 // Update the maximum alignment on the current section if necessary.
455 if (ByteAlignment > getCurrentSectionData()->getAlignment())
456 getCurrentSectionData()->setAlignment(ByteAlignment);
457}
458
459void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
460 unsigned MaxBytesToEmit) {
461 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
462 // MCObjectStreamer.
463 if (MaxBytesToEmit == 0)
464 MaxBytesToEmit = ByteAlignment;
465 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
466 getCurrentSectionData());
467 F->setEmitNops(true);
468
469 // Update the maximum alignment on the current section if necessary.
470 if (ByteAlignment > getCurrentSectionData()->getAlignment())
471 getCurrentSectionData()->setAlignment(ByteAlignment);
472}
473
474void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
475 unsigned char Value) {
476 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
477 // MCObjectStreamer.
478 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
479}
480
481// Add a symbol for the file name of this module. This is the second
482// entry in the module's symbol table (the first being the null symbol).
483void MCELFStreamer::EmitFileDirective(StringRef Filename) {
484 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
485 Symbol->setSection(*CurSection);
486 Symbol->setAbsolute();
487
488 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
489
490 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
491}
492
Benjamin Kramer93ded732010-08-27 10:40:51 +0000493void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
494 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
495
496 // Add the fixups and data.
497 //
498 // FIXME: Revisit this design decision when relaxation is done, we may be
499 // able to get away with not storing any extra data in the MCInst.
500 SmallVector<MCFixup, 4> Fixups;
501 SmallString<256> Code;
502 raw_svector_ostream VecOS(Code);
503 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
504 VecOS.flush();
505
506 IF->getCode() = Code;
507 IF->getFixups() = Fixups;
508}
509
510void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
511 MCDataFragment *DF = getOrCreateDataFragment();
512
513 SmallVector<MCFixup, 4> Fixups;
514 SmallString<256> Code;
515 raw_svector_ostream VecOS(Code);
516 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
517 VecOS.flush();
518
519 // Add the fixups and data.
520 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
521 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
522 DF->addFixup(Fixups[i]);
523 }
524 DF->getContents().append(Code.begin(), Code.end());
525}
526
Matt Fleming3565a062010-08-16 18:57:57 +0000527void MCELFStreamer::Finish() {
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000528 // FIXME: duplicated code with the MachO streamer.
529 // Dump out the dwarf file & directory tables and line tables.
530 if (getContext().hasDwarfFiles()) {
531 const MCSection *DwarfLineSection =
532 getContext().getELFSection(".debug_line", 0, 0,
533 SectionKind::getDataRelLocal());
534 MCDwarfFileTable::Emit(this, DwarfLineSection);
535 }
536
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000537 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
538 e = LocalCommons.end();
539 i != e; ++i) {
540 MCSymbolData *SD = i->SD;
541 uint64_t Size = i->Size;
542 unsigned ByteAlignment = i->ByteAlignment;
543 const MCSymbol &Symbol = SD->getSymbol();
544 const MCSection &Section = Symbol.getSection();
545
546 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
547 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
548
549 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
550 SD->setFragment(F);
551
552 // Update the maximum alignment of the section if necessary.
553 if (ByteAlignment > SectData.getAlignment())
554 SectData.setAlignment(ByteAlignment);
555 }
556
Rafael Espindola73ffea42010-09-25 05:42:19 +0000557 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000558}
559
560MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
561 raw_ostream &OS, MCCodeEmitter *CE,
562 bool RelaxAll) {
563 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
564 if (RelaxAll)
565 S->getAssembler().setRelaxAll(true);
566 return S;
567}