blob: b63bedc7030e53f5c10b5760defcc581915e977d [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"
27#include "llvm/Support/Debug.h"
28#include "llvm/Support/ELF.h"
29#include "llvm/Support/ErrorHandling.h"
30#include "llvm/Support/raw_ostream.h"
31#include "llvm/Target/TargetAsmBackend.h"
32
33using namespace llvm;
34
35namespace {
36
37class MCELFStreamer : public MCObjectStreamer {
Benjamin Kramer93ded732010-08-27 10:40:51 +000038 void EmitInstToFragment(const MCInst &Inst);
39 void EmitInstToData(const MCInst &Inst);
Matt Fleming3565a062010-08-16 18:57:57 +000040public:
41 MCELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
42 raw_ostream &OS, MCCodeEmitter *Emitter)
Rafael Espindola59ff3c92010-09-22 22:27:05 +000043 : MCObjectStreamer(Context, TAB, OS, Emitter, false) {}
Matt Fleming3565a062010-08-16 18:57:57 +000044
45 ~MCELFStreamer() {}
46
47 /// @name MCStreamer Interface
48 /// @{
49
Rafael Espindolad80781b2010-09-15 21:48:40 +000050 virtual void InitSections();
Matt Fleming3565a062010-08-16 18:57:57 +000051 virtual void EmitLabel(MCSymbol *Symbol);
52 virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
53 virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
54 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
110 virtual void EmitInstruction(const MCInst &Inst);
111 virtual void Finish();
112
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000113private:
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000114 struct LocalCommon {
115 MCSymbolData *SD;
116 uint64_t Size;
117 unsigned ByteAlignment;
118 };
119 std::vector<LocalCommon> LocalCommons;
120
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000121 SmallPtrSet<MCSymbol *, 16> BindingExplicitlySet;
Matt Fleming3565a062010-08-16 18:57:57 +0000122 /// @}
Rafael Espindolad80781b2010-09-15 21:48:40 +0000123 void SetSection(StringRef Section, unsigned Type, unsigned Flags,
124 SectionKind Kind) {
125 SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
126 }
127
128 void SetSectionData() {
129 SetSection(".data", MCSectionELF::SHT_PROGBITS,
130 MCSectionELF::SHF_WRITE |MCSectionELF::SHF_ALLOC,
131 SectionKind::getDataRel());
132 EmitCodeAlignment(4, 0);
133 }
134 void SetSectionText() {
135 SetSection(".text", MCSectionELF::SHT_PROGBITS,
136 MCSectionELF::SHF_EXECINSTR |
137 MCSectionELF::SHF_ALLOC, SectionKind::getText());
138 EmitCodeAlignment(4, 0);
139 }
140 void SetSectionBss() {
141 SetSection(".bss", MCSectionELF::SHT_NOBITS,
142 MCSectionELF::SHF_WRITE |
143 MCSectionELF::SHF_ALLOC, SectionKind::getBSS());
144 EmitCodeAlignment(4, 0);
145 }
Matt Fleming3565a062010-08-16 18:57:57 +0000146};
147
148} // end anonymous namespace.
149
Rafael Espindolad80781b2010-09-15 21:48:40 +0000150void MCELFStreamer::InitSections() {
151 // This emulates the same behavior of GNU as. This makes it easier
152 // to compare the output as the major sections are in the same order.
153 SetSectionText();
154 SetSectionData();
155 SetSectionBss();
156 SetSectionText();
157}
158
Matt Fleming3565a062010-08-16 18:57:57 +0000159void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
160 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
161
Rafael Espindola73ffea42010-09-25 05:42:19 +0000162 Symbol->setSection(*CurSection);
163
164 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
165
Matt Fleming3565a062010-08-16 18:57:57 +0000166 // FIXME: This is wasteful, we don't necessarily need to create a data
167 // fragment. Instead, we should mark the symbol as pointing into the data
168 // fragment if it exists, otherwise we should just queue the label and set its
169 // fragment pointer when we emit the next fragment.
170 MCDataFragment *F = getOrCreateDataFragment();
Rafael Espindola73ffea42010-09-25 05:42:19 +0000171
Matt Fleming3565a062010-08-16 18:57:57 +0000172 assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
173 SD.setFragment(F);
174 SD.setOffset(F->getContents().size());
Matt Fleming3565a062010-08-16 18:57:57 +0000175}
176
177void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
178 switch (Flag) {
Jason W Kimafd1cc22010-09-30 02:45:56 +0000179 case MCAF_SyntaxUnified: return; // no-op here?
Matt Fleming3565a062010-08-16 18:57:57 +0000180 case MCAF_SubsectionsViaSymbols:
181 getAssembler().setSubsectionsViaSymbols(true);
182 return;
183 }
184
185 assert(0 && "invalid assembler flag!");
186}
187
188void MCELFStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
189 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
190 // MCObjectStreamer.
191 // FIXME: Lift context changes into super class.
192 getAssembler().getOrCreateSymbolData(*Symbol);
193 Symbol->setVariableValue(AddValueSymbols(Value));
194}
195
Rafael Espindolaf7133842010-09-13 17:39:45 +0000196static void SetBinding(MCSymbolData &SD, unsigned Binding) {
197 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
198 Binding == ELF::STB_WEAK);
199 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STB_Shift);
200 SD.setFlags(OtherFlags | (Binding << ELF_STB_Shift));
201}
202
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000203static unsigned GetBinding(const MCSymbolData &SD) {
204 uint32_t Binding = (SD.getFlags() & (0xf << ELF_STB_Shift)) >> ELF_STB_Shift;
205 assert(Binding == ELF::STB_LOCAL || Binding == ELF::STB_GLOBAL ||
206 Binding == ELF::STB_WEAK);
207 return Binding;
208}
209
Rafael Espindolaf7133842010-09-13 17:39:45 +0000210static void SetType(MCSymbolData &SD, unsigned Type) {
211 assert(Type == ELF::STT_NOTYPE || Type == ELF::STT_OBJECT ||
212 Type == ELF::STT_FUNC || Type == ELF::STT_SECTION ||
213 Type == ELF::STT_FILE || Type == ELF::STT_COMMON ||
214 Type == ELF::STT_TLS);
215
216 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STT_Shift);
217 SD.setFlags(OtherFlags | (Type << ELF_STT_Shift));
218}
219
220static void SetVisibility(MCSymbolData &SD, unsigned Visibility) {
221 assert(Visibility == ELF::STV_DEFAULT || Visibility == ELF::STV_INTERNAL ||
222 Visibility == ELF::STV_HIDDEN || Visibility == ELF::STV_PROTECTED);
223
224 uint32_t OtherFlags = SD.getFlags() & ~(0xf << ELF_STV_Shift);
225 SD.setFlags(OtherFlags | (Visibility << ELF_STV_Shift));
226}
227
Matt Fleming3565a062010-08-16 18:57:57 +0000228void MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
229 MCSymbolAttr Attribute) {
230 // Indirect symbols are handled differently, to match how 'as' handles
231 // them. This makes writing matching .o files easier.
232 if (Attribute == MCSA_IndirectSymbol) {
233 // Note that we intentionally cannot use the symbol data here; this is
234 // important for matching the string table that 'as' generates.
235 IndirectSymbolData ISD;
236 ISD.Symbol = Symbol;
237 ISD.SectionData = getCurrentSectionData();
238 getAssembler().getIndirectSymbols().push_back(ISD);
239 return;
240 }
241
242 // Adding a symbol attribute always introduces the symbol, note that an
243 // important side effect of calling getOrCreateSymbolData here is to register
244 // the symbol with the assembler.
245 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
246
247 // The implementation of symbol attributes is designed to match 'as', but it
248 // leaves much to desired. It doesn't really make sense to arbitrarily add and
249 // remove flags, but 'as' allows this (in particular, see .desc).
250 //
251 // In the future it might be worth trying to make these operations more well
252 // defined.
253 switch (Attribute) {
254 case MCSA_LazyReference:
255 case MCSA_Reference:
256 case MCSA_NoDeadStrip:
257 case MCSA_PrivateExtern:
Matt Fleming3565a062010-08-16 18:57:57 +0000258 case MCSA_WeakDefinition:
Eli Friedmanf8020a32010-08-16 19:15:06 +0000259 case MCSA_WeakDefAutoPrivate:
Matt Fleming3565a062010-08-16 18:57:57 +0000260 case MCSA_Invalid:
261 case MCSA_ELF_TypeIndFunction:
262 case MCSA_IndirectSymbol:
263 assert(0 && "Invalid symbol attribute for ELF!");
264 break;
265
266 case MCSA_Global:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000267 SetBinding(SD, ELF::STB_GLOBAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000268 SD.setExternal(true);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000269 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000270 break;
271
Benjamin Kramer230c2742010-09-02 17:18:32 +0000272 case MCSA_WeakReference:
Matt Fleming3565a062010-08-16 18:57:57 +0000273 case MCSA_Weak:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000274 SetBinding(SD, ELF::STB_WEAK);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000275 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000276 break;
277
278 case MCSA_Local:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000279 SetBinding(SD, ELF::STB_LOCAL);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000280 SD.setExternal(false);
281 BindingExplicitlySet.insert(Symbol);
Matt Fleming3565a062010-08-16 18:57:57 +0000282 break;
283
284 case MCSA_ELF_TypeFunction:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000285 SetType(SD, ELF::STT_FUNC);
Matt Fleming3565a062010-08-16 18:57:57 +0000286 break;
287
288 case MCSA_ELF_TypeObject:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000289 SetType(SD, ELF::STT_OBJECT);
Matt Fleming3565a062010-08-16 18:57:57 +0000290 break;
291
292 case MCSA_ELF_TypeTLS:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000293 SetType(SD, ELF::STT_TLS);
Matt Fleming3565a062010-08-16 18:57:57 +0000294 break;
295
296 case MCSA_ELF_TypeCommon:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000297 SetType(SD, ELF::STT_COMMON);
Matt Fleming3565a062010-08-16 18:57:57 +0000298 break;
299
300 case MCSA_ELF_TypeNoType:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000301 SetType(SD, ELF::STT_NOTYPE);
Matt Fleming3565a062010-08-16 18:57:57 +0000302 break;
303
304 case MCSA_Protected:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000305 SetVisibility(SD, ELF::STV_PROTECTED);
Matt Fleming3565a062010-08-16 18:57:57 +0000306 break;
307
308 case MCSA_Hidden:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000309 SetVisibility(SD, ELF::STV_HIDDEN);
Matt Fleming3565a062010-08-16 18:57:57 +0000310 break;
311
312 case MCSA_Internal:
Rafael Espindolaf7133842010-09-13 17:39:45 +0000313 SetVisibility(SD, ELF::STV_INTERNAL);
Matt Fleming3565a062010-08-16 18:57:57 +0000314 break;
315 }
316}
317
318void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
319 unsigned ByteAlignment) {
320 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
321
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000322 if (!BindingExplicitlySet.count(Symbol)) {
323 SetBinding(SD, ELF::STB_GLOBAL);
324 SD.setExternal(true);
325 }
326
327 if (GetBinding(SD) == ELF_STB_Local) {
Matt Fleming3565a062010-08-16 18:57:57 +0000328 const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
329 MCSectionELF::SHT_NOBITS,
330 MCSectionELF::SHF_WRITE |
331 MCSectionELF::SHF_ALLOC,
332 SectionKind::getBSS());
Matt Fleming3565a062010-08-16 18:57:57 +0000333 Symbol->setSection(*Section);
Rafael Espindola19635722010-09-22 17:43:04 +0000334
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000335 struct LocalCommon L = {&SD, Size, ByteAlignment};
336 LocalCommons.push_back(L);
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000337 } else {
338 SD.setCommon(Size, ByteAlignment);
Matt Fleming3565a062010-08-16 18:57:57 +0000339 }
340
Rafael Espindolaf7c10a32010-09-21 00:24:38 +0000341 SD.setSize(MCConstantExpr::Create(Size, getContext()));
Matt Fleming3565a062010-08-16 18:57:57 +0000342}
343
344void MCELFStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
345 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
346 // MCObjectStreamer.
347 getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
348}
349
350void MCELFStreamer::EmitValue(const MCExpr *Value, unsigned Size,
351 unsigned AddrSpace) {
352 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
353 // MCObjectStreamer.
354 MCDataFragment *DF = getOrCreateDataFragment();
355
356 // Avoid fixups when possible.
357 int64_t AbsValue;
358 if (AddValueSymbols(Value)->EvaluateAsAbsolute(AbsValue)) {
359 // FIXME: Endianness assumption.
360 for (unsigned i = 0; i != Size; ++i)
361 DF->getContents().push_back(uint8_t(AbsValue >> (i * 8)));
362 } else {
363 DF->addFixup(MCFixup::Create(DF->getContents().size(), AddValueSymbols(Value),
364 MCFixup::getKindForSize(Size)));
365 DF->getContents().resize(DF->getContents().size() + Size, 0);
366 }
367}
368
369void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
370 int64_t Value, unsigned ValueSize,
371 unsigned MaxBytesToEmit) {
372 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
373 // MCObjectStreamer.
374 if (MaxBytesToEmit == 0)
375 MaxBytesToEmit = ByteAlignment;
376 new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
377 getCurrentSectionData());
378
379 // Update the maximum alignment on the current section if necessary.
380 if (ByteAlignment > getCurrentSectionData()->getAlignment())
381 getCurrentSectionData()->setAlignment(ByteAlignment);
382}
383
384void MCELFStreamer::EmitCodeAlignment(unsigned ByteAlignment,
385 unsigned MaxBytesToEmit) {
386 // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
387 // MCObjectStreamer.
388 if (MaxBytesToEmit == 0)
389 MaxBytesToEmit = ByteAlignment;
390 MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
391 getCurrentSectionData());
392 F->setEmitNops(true);
393
394 // Update the maximum alignment on the current section if necessary.
395 if (ByteAlignment > getCurrentSectionData()->getAlignment())
396 getCurrentSectionData()->setAlignment(ByteAlignment);
397}
398
399void MCELFStreamer::EmitValueToOffset(const MCExpr *Offset,
400 unsigned char Value) {
401 // TODO: This is exactly the same as MCMachOStreamer. Consider merging into
402 // MCObjectStreamer.
403 new MCOrgFragment(*Offset, Value, getCurrentSectionData());
404}
405
406// Add a symbol for the file name of this module. This is the second
407// entry in the module's symbol table (the first being the null symbol).
408void MCELFStreamer::EmitFileDirective(StringRef Filename) {
409 MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
410 Symbol->setSection(*CurSection);
411 Symbol->setAbsolute();
412
413 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
414
415 SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
416}
417
Benjamin Kramer93ded732010-08-27 10:40:51 +0000418void MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
419 MCInstFragment *IF = new MCInstFragment(Inst, getCurrentSectionData());
420
421 // Add the fixups and data.
422 //
423 // FIXME: Revisit this design decision when relaxation is done, we may be
424 // able to get away with not storing any extra data in the MCInst.
425 SmallVector<MCFixup, 4> Fixups;
426 SmallString<256> Code;
427 raw_svector_ostream VecOS(Code);
428 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
429 VecOS.flush();
430
431 IF->getCode() = Code;
432 IF->getFixups() = Fixups;
433}
434
435void MCELFStreamer::EmitInstToData(const MCInst &Inst) {
436 MCDataFragment *DF = getOrCreateDataFragment();
437
438 SmallVector<MCFixup, 4> Fixups;
439 SmallString<256> Code;
440 raw_svector_ostream VecOS(Code);
441 getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
442 VecOS.flush();
443
444 // Add the fixups and data.
445 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
446 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
447 DF->addFixup(Fixups[i]);
448 }
449 DF->getContents().append(Code.begin(), Code.end());
450}
451
Matt Fleming3565a062010-08-16 18:57:57 +0000452void MCELFStreamer::EmitInstruction(const MCInst &Inst) {
453 // Scan for values.
454 for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
455 if (Inst.getOperand(i).isExpr())
456 AddValueSymbols(Inst.getOperand(i).getExpr());
457
458 getCurrentSectionData()->setHasInstructions(true);
459
Benjamin Kramer93ded732010-08-27 10:40:51 +0000460 // If this instruction doesn't need relaxation, just emit it as data.
461 if (!getAssembler().getBackend().MayNeedRelaxation(Inst)) {
462 EmitInstToData(Inst);
Matt Fleming3565a062010-08-16 18:57:57 +0000463 return;
464 }
465
Benjamin Kramer93ded732010-08-27 10:40:51 +0000466 // Otherwise, if we are relaxing everything, relax the instruction as much as
467 // possible and emit it as data.
468 if (getAssembler().getRelaxAll()) {
469 MCInst Relaxed;
470 getAssembler().getBackend().RelaxInstruction(Inst, Relaxed);
471 while (getAssembler().getBackend().MayNeedRelaxation(Relaxed))
472 getAssembler().getBackend().RelaxInstruction(Relaxed, Relaxed);
473 EmitInstToData(Relaxed);
474 return;
Matt Fleming3565a062010-08-16 18:57:57 +0000475 }
Benjamin Kramer93ded732010-08-27 10:40:51 +0000476
477 // Otherwise emit to a separate fragment.
478 EmitInstToFragment(Inst);
Matt Fleming3565a062010-08-16 18:57:57 +0000479}
480
481void MCELFStreamer::Finish() {
Rafael Espindola9e3922e2010-09-29 14:52:01 +0000482 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
483 e = LocalCommons.end();
484 i != e; ++i) {
485 MCSymbolData *SD = i->SD;
486 uint64_t Size = i->Size;
487 unsigned ByteAlignment = i->ByteAlignment;
488 const MCSymbol &Symbol = SD->getSymbol();
489 const MCSection &Section = Symbol.getSection();
490
491 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
492 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
493
494 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
495 SD->setFragment(F);
496
497 // Update the maximum alignment of the section if necessary.
498 if (ByteAlignment > SectData.getAlignment())
499 SectData.setAlignment(ByteAlignment);
500 }
501
Rafael Espindola73ffea42010-09-25 05:42:19 +0000502 this->MCObjectStreamer::Finish();
Matt Fleming3565a062010-08-16 18:57:57 +0000503}
504
505MCStreamer *llvm::createELFStreamer(MCContext &Context, TargetAsmBackend &TAB,
506 raw_ostream &OS, MCCodeEmitter *CE,
507 bool RelaxAll) {
508 MCELFStreamer *S = new MCELFStreamer(Context, TAB, OS, CE);
509 if (RelaxAll)
510 S->getAssembler().setRelaxAll(true);
511 return S;
512}