blob: a2d94f5ba15e1a24d39bda1c0e03c5a83b2c8bb3 [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);
140 virtual void EmitDwarfFileDirective(unsigned FileNo, StringRef Filename) {
141 DEBUG(dbgs() << "FIXME: MCELFStreamer:EmitDwarfFileDirective not implemented\n");
142 }
143
Matt Fleming3565a062010-08-16 18:57:57 +0000144 virtual void Finish();
145
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000146private:
Rafael Espindolaf89671d2010-11-01 16:27:31 +0000147 virtual void EmitInstToFragment(const MCInst &Inst);
148 virtual void EmitInstToData(const MCInst &Inst);
149
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000150 struct LocalCommon {
151 MCSymbolData *SD;
152 uint64_t Size;
153 unsigned ByteAlignment;
154 };
155 std::vector<LocalCommon> LocalCommons;
156
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000157 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
Matt Fleming3565a062010-08-16 18:57:57 +0000158 /// @}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000159 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
160 SectionKind Kind) {
161 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
162 }
163
164 void SetSectionData() {
165 SetSection(".data", MCSectionELF::SHT_PROGBITS,
166 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
167 SectionKind::getDataRel());
168 EmitCodeAlignment(4, 0);
169 }
170 void SetSectionText() {
171 SetSection(".text", MCSectionELF::SHT_PROGBITS,
172 MCSectionELF::SHF_EXECINSTR |
173 MCSectionELF::SHF_ALLOC, SectionKind::getText());
174 EmitCodeAlignment(4, 0);
175 }
176 void SetSectionBss() {
177 SetSection(".bss", MCSectionELF::SHT_NOBITS,
178 MCSectionELF::SHF_WRITE |
179 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
180 EmitCodeAlignment(4, 0);
181 }
Matt Fleming3565a062010-08-16 18:57:57 +0000182};
183
184} // end anonymous namespace.
185
Rafael Espindolad80781b2010-09-15 21:48:40 +0000186void MCELFStreamer::InitSections() {
187 // This emulates the same behavior of GNU as. This makes it easier
188 // to compare the output as the major sections are in the same order.
189 SetSectionText();
190 SetSectionData();
191 SetSectionBss();
192 SetSectionText();
193}
194
Matt Fleming3565a062010-08-16 18:57:57 +0000195void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
196 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
197
Rafael Espindola73ffea42010-09-25 05:42:19 +0000198 Symbol->setSection(*CurSection);
199
200 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
201
Rafael Espindolae1a25872010-11-11 19:04:55 +0000202 const MCSectionELF &Section =
203 static_cast<const MCSectionELF&>(Symbol->getSection());
204 if (Section.getFlags() & MCSectionELF::SHF_TLS)
205 SetType(SD, ELF::STT_TLS);
206
Matt Fleming3565a062010-08-16 18:57:57 +0000207 // FIXME: This is wasteful, we don't necessarily need to create a data
208 // fragment. Instead, we should mark the symbol as pointing into the data
209 // fragment if it exists, otherwise we should just queue the label and set its
210 // fragment pointer when we emit the next fragment.
211 MCDataFragment *F = getOrCreateDataFragment();
Rafael Espindola73ffea42010-09-25 05:42:19 +0000212
Matt Fleming3565a062010-08-16 18:57:57 +0000213 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
214 SD.setFragment(F);
215 SD.setOffset(F->getContents().size());
Matt Fleming3565a062010-08-16 18:57:57 +0000216}
217
218void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
219 switch (Flag) {
Jim Grosbachce792992010-11-05 22:08:08 +0000220 case MCAF_SyntaxUnified: return; // no-op here.
221 case MCAF_Code16: return; // no-op here.
Jim Grosbachba219572010-11-05 22:40:09 +0000222 case MCAF_Code32: return; // no-op here.
Matt Fleming3565a062010-08-16 18:57:57 +0000223 case MCAF_SubsectionsViaSymbols:
224 getAssembler().setSubsectionsViaSymbols(true);
225 return;
226 }
227
228 assert(0 && "invalid assembler flag!");
229}
230
Jim Grosbachce792992010-11-05 22:08:08 +0000231void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
232 // FIXME: Anything needed here to flag the function as thumb?
233}
234
Matt Fleming3565a062010-08-16 18:57:57 +0000235void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
236 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
237 // MCObjectStreamer.
238 // FIXME: Lift context changes into super class.
239 getAssembler().getOrCreateSymbolData(*Symbol);
240 Symbol->setVariableValue(AddValueSymbols(Value));
241}
242
Rafael Espindola484291c2010-11-01 14:28:48 +0000243// This is a hack. To be able to implement weakrefs the writer has to be able
244// to distinguish
245// .weakref foo, bar
246// .long foo
247// from
248// .weakref foo, bar
249// .long bar
250// since the first case should produce a weak undefined reference and the second
251// one a strong one.
252// If we created foo as a regular alias pointing to bar (foo = bar), then
253// MCExpr::EvaluateAsRelocatable would recurse on foo and the writer would
254// never see it used in a relocation.
255// What we do is create a MCTargetExpr that when evaluated produces a symbol
256// ref to a temporary symbol. This temporary symbol in turn is a variable
257// that equals the original symbol (tmp = bar). With this hack the writer
258// gets a relocation with tmp and can correctly implement weak references.
259
Benjamin Kramerf7985692010-11-05 19:56:38 +0000260namespace {
Rafael Espindola484291c2010-11-01 14:28:48 +0000261class WeakRefExpr : public MCTargetExpr {
262private:
263 const MCSymbolRefExpr *Alias;
264
265 explicit WeakRefExpr(const MCSymbolRefExpr *Alias_)
266 : MCTargetExpr(), Alias(Alias_) {}
267
268public:
269 virtual void PrintImpl(raw_ostream &OS) const {
270 llvm_unreachable("Unimplemented");
271 }
272
273 virtual bool EvaluateAsRelocatableImpl(MCValue &Res,
274 const MCAsmLayout *Layout) const {
275 Res = MCValue::get(Alias, 0, 0);
276 return true;
277 }
278
279 static const WeakRefExpr *Create(const MCSymbol *Alias, MCContext &Ctx) {
280 const MCSymbolRefExpr *A = MCSymbolRefExpr::Create(Alias, Ctx);
281 return new (Ctx) WeakRefExpr(A);
282 }
283};
Benjamin Kramerf7985692010-11-05 19:56:38 +0000284} // end anonymous namespace
Rafael Espindola484291c2010-11-01 14:28:48 +0000285
Rafael Espindola1f4f9e32010-11-14 04:17:37 +0000286void MCELFStreamer::SwitchSection(const MCSection *Section) {
287 const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
288 if (Grp)
289 getAssembler().getOrCreateSymbolData(*Grp);
290 this->MCObjectStreamer::SwitchSection(Section);
291}
292
Rafael Espindola484291c2010-11-01 14:28:48 +0000293void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
294 getAssembler().getOrCreateSymbolData(*Symbol);
295 MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
296 AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
297
298 // Create the alias that actually points to Symbol
299 const MCSymbolRefExpr *SymRef = MCSymbolRefExpr::Create(Symbol, getContext());
300 MCSymbol *RealAlias = getContext().CreateTempSymbol();
301 RealAlias->setVariableValue(SymRef);
302
303 MCSymbolData &RealAliasSD = getAssembler().getOrCreateSymbolData(*RealAlias);
304 RealAliasSD.setFlags(RealAliasSD.getFlags() | ELF_Other_Weakref);
305
306 const MCExpr *Value = WeakRefExpr::Create(RealAlias, getContext());
307 Alias->setVariableValue(Value);
308}
309
Matt Fleming3565a062010-08-16 18:57:57 +0000310void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
311 MCSymbolAttr Attribute) {
312 // Indirect symbols are handled differently, to match how 'as' handles
313 // them. This makes writing matching .o files easier.
314 if (Attribute == MCSA_IndirectSymbol) {
315 // Note that we intentionally cannot use the symbol data here; this is
316 // important for matching the string table that 'as' generates.
317 IndirectSymbolData ISD;
318 ISD.Symbol = Symbol;
319 ISD.SectionData = getCurrentSectionData();
320 getAssembler().getIndirectSymbols().push_back(ISD);
321 return;
322 }
323
324 // Adding a symbol attribute always introduces the symbol, note that an
325 // important side effect of calling getOrCreateSymbolData here is to register
326 // the symbol with the assembler.
327 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
328
329 // The implementation of symbol attributes is designed to match 'as', but it
330 // leaves much to desired. It doesn't really make sense to arbitrarily add and
331 // remove flags, but 'as' allows this (in particular, see .desc).
332 //
333 // In the future it might be worth trying to make these operations more well
334 // defined.
335 switch (Attribute) {
336 case MCSA_LazyReference:
337 case MCSA_Reference:
338 case MCSA_NoDeadStrip:
339 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000340 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000341 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000342 case MCSA_Invalid:
343 case MCSA_ELF_TypeIndFunction:
344 case MCSA_IndirectSymbol:
345 assert(0 && "Invalid symbol attribute for ELF!");
346 break;
347
Rafael Espindola7e528a12010-11-14 01:34:31 +0000348 case MCSA_ELF_TypeGnuUniqueObject:
349 // Ignore for now.
350 break;
351
Matt Fleming3565a062010-08-16 18:57:57 +0000352 case MCSA_Global:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000353 SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000354 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000355 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000356 break;
357
Benjamin Kramer230c2742010-09-02 17:18:32 +0000358 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000359 case MCSA_Weak:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000360 SetBinding(SD, ELF::STB_WEAK);
Rafael Espindola3223f192010-10-06 16:47:31 +0000361 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000362 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000363 break;
364
365 case MCSA_Local:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000366 SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000367 SD.setExternal(false);
368 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000369 break;
370
371 case MCSA_ELF_TypeFunction:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000372 SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000373 break;
374
375 case MCSA_ELF_TypeObject:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000376 SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000377 break;
378
379 case MCSA_ELF_TypeTLS:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000380 SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000381 break;
382
383 case MCSA_ELF_TypeCommon:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000384 SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000385 break;
386
387 case MCSA_ELF_TypeNoType:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000388 SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000389 break;
390
391 case MCSA_Protected:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000392 SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000393 break;
394
395 case MCSA_Hidden:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000396 SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000397 break;
398
399 case MCSA_Internal:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000400 SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000401 break;
402 }
403}
404
405void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
406 unsigned ByteAlignment) {
407 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
408
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000409 if (!BindingExplicitlySet.count(Symbol)) {
410 SetBinding(SD, ELF::STB_GLOBAL);
411 SD.setExternal(true);
412 }
413
Rafael Espindola55d02f32010-11-14 21:11:16 +0000414 SetType(SD, ELF::STT_OBJECT);
415
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000416 if (GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000417 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
418 MCSectionELF::SHT_NOBITS,
419 MCSectionELF::SHF_WRITE |
420 MCSectionELF::SHF_ALLOC,
421 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000422 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000423
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000424 struct LocalCommon L = {&SD, Size, ByteAlignment};
425 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000426 } else {
427 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000428 }
429
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000430 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000431}
432
433void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
434 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
435 // MCObjectStreamer.
436 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
437}
438
439void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
440 unsigned AddrSpace) {
441 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
442 // MCObjectStreamer.
443 MCDataFragment *DF = getOrCreateDataFragment();
444
445 // Avoid fixups when possible.
446 int64_t AbsValue;
447 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
448 // FIXME: Endianness assumption.
449 for (unsigned i = 0; i != Size; ++i)
450 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
451 } else {
452 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
453 MCFixup::getKindForSize(Size)));
454 DF->getContents().resize(DF->getContents().size() + Size, 0);
455 }
456}
457
458void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
459 int64_t Value, unsigned ValueSize,
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 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
466 getCurrentSectionData());
467
468 // Update the maximum alignment on the current section if necessary.
469 if (ByteAlignment > getCurrentSectionData()->getAlignment())
470 getCurrentSectionData()->setAlignment(ByteAlignment);
471}
472
473void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
474 unsigned MaxBytesToEmit) {
475 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
476 // MCObjectStreamer.
477 if (MaxBytesToEmit == 0)
478 MaxBytesToEmit = ByteAlignment;
479 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
480 getCurrentSectionData());
481 F->setEmitNops(true);
482
483 // Update the maximum alignment on the current section if necessary.
484 if (ByteAlignment > getCurrentSectionData()->getAlignment())
485 getCurrentSectionData()->setAlignment(ByteAlignment);
486}
487
488void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
489 unsigned char Value) {
490 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
491 // MCObjectStreamer.
492 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
493}
494
495// Add a symbol for the file name of this module. This is the second
496// entry in the module's symbol table (the first being the null symbol).
497void MCELFStreamer::EmitFileDirective(StringRef Filename) {
498 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
499 Symbol->setSection(*CurSection);
500 Symbol->setAbsolute();
501
502 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
503
504 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
505}
506
Benjamin Kramer93ded732010-08-27 10:40:51 +0000507void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
508 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
509
510 // Add the fixups and data.
511 //
512 // FIXME: Revisit this design decision when relaxation is done, we may be
513 // able to get away with not storing any extra data in the MCInst.
514 SmallVector<MCFixup, 4> Fixups;
515 SmallString<256> Code;
516 raw_svector_ostream VecOS(Code);
517 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
518 VecOS.flush();
519
520 IF->getCode() = Code;
521 IF->getFixups() = Fixups;
522}
523
524void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
525 MCDataFragment *DF = getOrCreateDataFragment();
526
527 SmallVector<MCFixup, 4> Fixups;
528 SmallString<256> Code;
529 raw_svector_ostream VecOS(Code);
530 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
531 VecOS.flush();
532
533 // Add the fixups and data.
534 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
535 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
536 DF->addFixup(Fixups[i]);
537 }
538 DF->getContents().append(Code.begin(), Code.end());
539}
540
Matt Fleming3565a062010-08-16 18:57:57 +0000541void MCELFStreamer::Finish() {
Rafael Espindolac70a1d92010-11-01 17:07:14 +0000542 // FIXME: duplicated code with the MachO streamer.
543 // Dump out the dwarf file & directory tables and line tables.
544 if (getContext().hasDwarfFiles()) {
545 const MCSection *DwarfLineSection =
546 getContext().getELFSection(".debug_line", 0, 0,
547 SectionKind::getDataRelLocal());
548 MCDwarfFileTable::Emit(this, DwarfLineSection);
549 }
550
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000551 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
552 e = LocalCommons.end();
553 i != e; ++i) {
554 MCSymbolData *SD = i->SD;
555 uint64_t Size = i->Size;
556 unsigned ByteAlignment = i->ByteAlignment;
557 const MCSymbol &Symbol = SD->getSymbol();
558 const MCSection &Section = Symbol.getSection();
559
560 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
561 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
562
563 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
564 SD->setFragment(F);
565
566 // Update the maximum alignment of the section if necessary.
567 if (ByteAlignment > SectData.getAlignment())
568 SectData.setAlignment(ByteAlignment);
569 }
570
Rafael Espindola73ffea42010-09-25 05:42:19 +0000571 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000572}
573
574MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
575 raw_ostream &OS, MCCodeEmitter *CE,
576 bool RelaxAll) {
577 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
578 if (RelaxAll)
579 S->getAssembler().setRelaxAll(true);
580 return S;
581}