Fix uses of reserved identifiers starting with an underscore followed by an uppercase...
[oota-llvm.git] / lib / MC / MCELFStreamer.cpp
1 //===- 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/MCELFStreamer.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmInfo.h"
19 #include "llvm/MC/MCAssembler.h"
20 #include "llvm/MC/MCCodeEmitter.h"
21 #include "llvm/MC/MCContext.h"
22 #include "llvm/MC/MCELF.h"
23 #include "llvm/MC/MCELFSymbolFlags.h"
24 #include "llvm/MC/MCExpr.h"
25 #include "llvm/MC/MCInst.h"
26 #include "llvm/MC/MCObjectFileInfo.h"
27 #include "llvm/MC/MCObjectStreamer.h"
28 #include "llvm/MC/MCSection.h"
29 #include "llvm/MC/MCSectionELF.h"
30 #include "llvm/MC/MCSymbol.h"
31 #include "llvm/MC/MCValue.h"
32 #include "llvm/Support/Debug.h"
33 #include "llvm/Support/ELF.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36
37 using namespace llvm;
38
39 MCELFStreamer::~MCELFStreamer() {
40 }
41
42 void MCELFStreamer::InitSections(bool NoExecStack) {
43   // This emulates the same behavior of GNU as. This makes it easier
44   // to compare the output as the major sections are in the same order.
45   MCContext &Ctx = getContext();
46   SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
47   EmitCodeAlignment(4);
48
49   SwitchSection(Ctx.getObjectFileInfo()->getDataSection());
50   EmitCodeAlignment(4);
51
52   SwitchSection(Ctx.getObjectFileInfo()->getBSSSection());
53   EmitCodeAlignment(4);
54
55   SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
56
57   if (NoExecStack)
58     SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
59 }
60
61 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
62   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
63
64   MCObjectStreamer::EmitLabel(Symbol);
65
66   const MCSectionELF &Section =
67     static_cast<const MCSectionELF&>(Symbol->getSection());
68   MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
69   if (Section.getFlags() & ELF::SHF_TLS)
70     MCELF::SetType(SD, ELF::STT_TLS);
71 }
72
73 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
74   // Let the target do whatever target specific stuff it needs to do.
75   getAssembler().getBackend().handleAssemblerFlag(Flag);
76   // Do any generic stuff we need to do.
77   switch (Flag) {
78   case MCAF_SyntaxUnified: return; // no-op here.
79   case MCAF_Code16: return; // Change parsing mode; no-op here.
80   case MCAF_Code32: return; // Change parsing mode; no-op here.
81   case MCAF_Code64: return; // Change parsing mode; no-op here.
82   case MCAF_SubsectionsViaSymbols:
83     getAssembler().setSubsectionsViaSymbols(true);
84     return;
85   }
86
87   llvm_unreachable("invalid assembler flag!");
88 }
89
90 void MCELFStreamer::ChangeSection(const MCSection *Section,
91                                   const MCExpr *Subsection) {
92   MCSectionData *CurSection = getCurrentSectionData();
93   if (CurSection && CurSection->isBundleLocked())
94     report_fatal_error("Unterminated .bundle_lock when changing a section");
95
96   MCAssembler &Asm = getAssembler();
97   auto *SectionELF = static_cast<const MCSectionELF *>(Section);
98   const MCSymbol *Grp = SectionELF->getGroup();
99   if (Grp)
100     Asm.getOrCreateSymbolData(*Grp);
101
102   this->MCObjectStreamer::ChangeSection(Section, Subsection);
103   MCSymbol *SectionSymbol = getContext().getOrCreateSectionSymbol(*SectionELF);
104   if (SectionSymbol->isUndefined()) {
105     EmitLabel(SectionSymbol);
106     MCELF::SetType(Asm.getSymbolData(*SectionSymbol), ELF::STT_SECTION);
107   }
108 }
109
110 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
111   getAssembler().getOrCreateSymbolData(*Symbol);
112   const MCExpr *Value = MCSymbolRefExpr::Create(
113       Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
114   Alias->setVariableValue(Value);
115 }
116
117 // When GNU as encounters more than one .type declaration for an object it seems
118 // to use a mechanism similar to the one below to decide which type is actually
119 // used in the object file.  The greater of T1 and T2 is selected based on the
120 // following ordering:
121 //  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
122 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
123 // provided type).
124 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
125   for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
126                         ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
127     if (T1 == Type)
128       return T2;
129     if (T2 == Type)
130       return T1;
131   }
132
133   return T2;
134 }
135
136 bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
137                                         MCSymbolAttr Attribute) {
138   // Indirect symbols are handled differently, to match how 'as' handles
139   // them. This makes writing matching .o files easier.
140   if (Attribute == MCSA_IndirectSymbol) {
141     // Note that we intentionally cannot use the symbol data here; this is
142     // important for matching the string table that 'as' generates.
143     IndirectSymbolData ISD;
144     ISD.Symbol = Symbol;
145     ISD.SectionData = getCurrentSectionData();
146     getAssembler().getIndirectSymbols().push_back(ISD);
147     return true;
148   }
149
150   // Adding a symbol attribute always introduces the symbol, note that an
151   // important side effect of calling getOrCreateSymbolData here is to register
152   // the symbol with the assembler.
153   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
154
155   // The implementation of symbol attributes is designed to match 'as', but it
156   // leaves much to desired. It doesn't really make sense to arbitrarily add and
157   // remove flags, but 'as' allows this (in particular, see .desc).
158   //
159   // In the future it might be worth trying to make these operations more well
160   // defined.
161   switch (Attribute) {
162   case MCSA_LazyReference:
163   case MCSA_Reference:
164   case MCSA_SymbolResolver:
165   case MCSA_PrivateExtern:
166   case MCSA_WeakDefinition:
167   case MCSA_WeakDefAutoPrivate:
168   case MCSA_Invalid:
169   case MCSA_IndirectSymbol:
170     return false;
171
172   case MCSA_NoDeadStrip:
173     // Ignore for now.
174     break;
175
176   case MCSA_ELF_TypeGnuUniqueObject:
177     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), ELF::STT_OBJECT));
178     MCELF::SetBinding(SD, ELF::STB_GNU_UNIQUE);
179     SD.setExternal(true);
180     BindingExplicitlySet.insert(Symbol);
181     break;
182
183   case MCSA_Global:
184     MCELF::SetBinding(SD, ELF::STB_GLOBAL);
185     SD.setExternal(true);
186     BindingExplicitlySet.insert(Symbol);
187     break;
188
189   case MCSA_WeakReference:
190   case MCSA_Weak:
191     MCELF::SetBinding(SD, ELF::STB_WEAK);
192     SD.setExternal(true);
193     BindingExplicitlySet.insert(Symbol);
194     break;
195
196   case MCSA_Local:
197     MCELF::SetBinding(SD, ELF::STB_LOCAL);
198     SD.setExternal(false);
199     BindingExplicitlySet.insert(Symbol);
200     break;
201
202   case MCSA_ELF_TypeFunction:
203     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
204                                           ELF::STT_FUNC));
205     break;
206
207   case MCSA_ELF_TypeIndFunction:
208     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
209                                           ELF::STT_GNU_IFUNC));
210     break;
211
212   case MCSA_ELF_TypeObject:
213     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
214                                           ELF::STT_OBJECT));
215     break;
216
217   case MCSA_ELF_TypeTLS:
218     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
219                                           ELF::STT_TLS));
220     break;
221
222   case MCSA_ELF_TypeCommon:
223     // TODO: Emit these as a common symbol.
224     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
225                                           ELF::STT_OBJECT));
226     break;
227
228   case MCSA_ELF_TypeNoType:
229     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
230                                           ELF::STT_NOTYPE));
231     break;
232
233   case MCSA_Protected:
234     MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
235     break;
236
237   case MCSA_Hidden:
238     MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
239     break;
240
241   case MCSA_Internal:
242     MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
243     break;
244   }
245
246   return true;
247 }
248
249 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
250                                        unsigned ByteAlignment) {
251   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
252
253   if (!BindingExplicitlySet.count(Symbol)) {
254     MCELF::SetBinding(SD, ELF::STB_GLOBAL);
255     SD.setExternal(true);
256   }
257
258   MCELF::SetType(SD, ELF::STT_OBJECT);
259
260   if (MCELF::GetBinding(SD) == ELF_STB_Local) {
261     const MCSection *Section = getAssembler().getContext().getELFSection(
262         ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
263
264     AssignSection(Symbol, Section);
265
266     struct LocalCommon L = {&SD, Size, ByteAlignment};
267     LocalCommons.push_back(L);
268   } else {
269     SD.setCommon(Size, ByteAlignment);
270   }
271
272   SD.setSize(MCConstantExpr::Create(Size, getContext()));
273 }
274
275 void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
276   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
277   SD.setSize(Value);
278 }
279
280 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
281                                           unsigned ByteAlignment) {
282   // FIXME: Should this be caught and done earlier?
283   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
284   MCELF::SetBinding(SD, ELF::STB_LOCAL);
285   SD.setExternal(false);
286   BindingExplicitlySet.insert(Symbol);
287   EmitCommonSymbol(Symbol, Size, ByteAlignment);
288 }
289
290 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
291                                   const SMLoc &Loc) {
292   if (getCurrentSectionData()->isBundleLocked())
293     report_fatal_error("Emitting values inside a locked bundle is forbidden");
294   fixSymbolsInTLSFixups(Value);
295   MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
296 }
297
298 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
299                                          int64_t Value,
300                                          unsigned ValueSize,
301                                          unsigned MaxBytesToEmit) {
302   if (getCurrentSectionData()->isBundleLocked())
303     report_fatal_error("Emitting values inside a locked bundle is forbidden");
304   MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
305                                          ValueSize, MaxBytesToEmit);
306 }
307
308 // Add a symbol for the file name of this module. They start after the
309 // null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
310 // with the same name may appear.
311 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
312   getAssembler().addFileName(Filename);
313 }
314
315 void MCELFStreamer::EmitIdent(StringRef IdentString) {
316   const MCSection *Comment = getAssembler().getContext().getELFSection(
317       ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
318   PushSection();
319   SwitchSection(Comment);
320   if (!SeenIdent) {
321     EmitIntValue(0, 1);
322     SeenIdent = true;
323   }
324   EmitBytes(IdentString);
325   EmitIntValue(0, 1);
326   PopSection();
327 }
328
329 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
330   switch (expr->getKind()) {
331   case MCExpr::Target:
332     cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
333     break;
334   case MCExpr::Constant:
335     break;
336
337   case MCExpr::Binary: {
338     const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
339     fixSymbolsInTLSFixups(be->getLHS());
340     fixSymbolsInTLSFixups(be->getRHS());
341     break;
342   }
343
344   case MCExpr::SymbolRef: {
345     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
346     switch (symRef.getKind()) {
347     default:
348       return;
349     case MCSymbolRefExpr::VK_GOTTPOFF:
350     case MCSymbolRefExpr::VK_INDNTPOFF:
351     case MCSymbolRefExpr::VK_NTPOFF:
352     case MCSymbolRefExpr::VK_GOTNTPOFF:
353     case MCSymbolRefExpr::VK_TLSGD:
354     case MCSymbolRefExpr::VK_TLSLD:
355     case MCSymbolRefExpr::VK_TLSLDM:
356     case MCSymbolRefExpr::VK_TPOFF:
357     case MCSymbolRefExpr::VK_DTPOFF:
358     case MCSymbolRefExpr::VK_Mips_TLSGD:
359     case MCSymbolRefExpr::VK_Mips_GOTTPREL:
360     case MCSymbolRefExpr::VK_Mips_TPREL_HI:
361     case MCSymbolRefExpr::VK_Mips_TPREL_LO:
362     case MCSymbolRefExpr::VK_PPC_DTPMOD:
363     case MCSymbolRefExpr::VK_PPC_TPREL:
364     case MCSymbolRefExpr::VK_PPC_TPREL_LO:
365     case MCSymbolRefExpr::VK_PPC_TPREL_HI:
366     case MCSymbolRefExpr::VK_PPC_TPREL_HA:
367     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
368     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
369     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
370     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
371     case MCSymbolRefExpr::VK_PPC_DTPREL:
372     case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
373     case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
374     case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
375     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
376     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
377     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
378     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
379     case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
380     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
381     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
382     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
383     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
384     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
385     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
386     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
387     case MCSymbolRefExpr::VK_PPC_TLS:
388     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
389     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
390     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
391     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
392     case MCSymbolRefExpr::VK_PPC_TLSGD:
393     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
394     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
395     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
396     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
397     case MCSymbolRefExpr::VK_PPC_TLSLD:
398       break;
399     }
400     MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
401     MCELF::SetType(SD, ELF::STT_TLS);
402     break;
403   }
404
405   case MCExpr::Unary:
406     fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
407     break;
408   }
409 }
410
411 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
412                                        const MCSubtargetInfo &STI) {
413   this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
414   MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
415
416   for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
417     fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
418 }
419
420 void MCELFStreamer::EmitInstToData(const MCInst &Inst,
421                                    const MCSubtargetInfo &STI) {
422   MCAssembler &Assembler = getAssembler();
423   SmallVector<MCFixup, 4> Fixups;
424   SmallString<256> Code;
425   raw_svector_ostream VecOS(Code);
426   Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
427   VecOS.flush();
428
429   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
430     fixSymbolsInTLSFixups(Fixups[i].getValue());
431
432   // There are several possibilities here:
433   //
434   // If bundling is disabled, append the encoded instruction to the current data
435   // fragment (or create a new such fragment if the current fragment is not a
436   // data fragment).
437   //
438   // If bundling is enabled:
439   // - If we're not in a bundle-locked group, emit the instruction into a
440   //   fragment of its own. If there are no fixups registered for the
441   //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
442   //   MCDataFragment.
443   // - If we're in a bundle-locked group, append the instruction to the current
444   //   data fragment because we want all the instructions in a group to get into
445   //   the same fragment. Be careful not to do that for the first instruction in
446   //   the group, though.
447   MCDataFragment *DF;
448
449   if (Assembler.isBundlingEnabled()) {
450     MCSectionData *SD = getCurrentSectionData();
451     if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst())
452       // If we are bundle-locked, we re-use the current fragment.
453       // The bundle-locking directive ensures this is a new data fragment.
454       DF = cast<MCDataFragment>(getCurrentFragment());
455     else if (!SD->isBundleLocked() && Fixups.size() == 0) {
456       // Optimize memory usage by emitting the instruction to a
457       // MCCompactEncodedInstFragment when not in a bundle-locked group and
458       // there are no fixups registered.
459       MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
460       insert(CEIF);
461       CEIF->getContents().append(Code.begin(), Code.end());
462       return;
463     } else {
464       DF = new MCDataFragment();
465       insert(DF);
466     }
467     if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) {
468       // If this fragment is for a group marked "align_to_end", set a flag
469       // in the fragment. This can happen after the fragment has already been
470       // created if there are nested bundle_align groups and an inner one
471       // is the one marked align_to_end.
472       DF->setAlignToBundleEnd(true);
473     }
474
475     // We're now emitting an instruction in a bundle group, so this flag has
476     // to be turned off.
477     SD->setBundleGroupBeforeFirstInst(false);
478   } else {
479     DF = getOrCreateDataFragment();
480   }
481
482   // Add the fixups and data.
483   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
484     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
485     DF->getFixups().push_back(Fixups[i]);
486   }
487   DF->setHasInstructions(true);
488   DF->getContents().append(Code.begin(), Code.end());
489 }
490
491 void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
492   assert(AlignPow2 <= 30 && "Invalid bundle alignment");
493   MCAssembler &Assembler = getAssembler();
494   if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
495                         Assembler.getBundleAlignSize() == 1U << AlignPow2))
496     Assembler.setBundleAlignSize(1U << AlignPow2);
497   else
498     report_fatal_error(".bundle_align_mode cannot be changed once set");
499 }
500
501 void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
502   MCSectionData *SD = getCurrentSectionData();
503
504   // Sanity checks
505   //
506   if (!getAssembler().isBundlingEnabled())
507     report_fatal_error(".bundle_lock forbidden when bundling is disabled");
508
509   if (!SD->isBundleLocked())
510     SD->setBundleGroupBeforeFirstInst(true);
511
512   SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd :
513                                       MCSectionData::BundleLocked);
514 }
515
516 void MCELFStreamer::EmitBundleUnlock() {
517   MCSectionData *SD = getCurrentSectionData();
518
519   // Sanity checks
520   if (!getAssembler().isBundlingEnabled())
521     report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
522   else if (!SD->isBundleLocked())
523     report_fatal_error(".bundle_unlock without matching lock");
524   else if (SD->isBundleGroupBeforeFirstInst())
525     report_fatal_error("Empty bundle-locked group is forbidden");
526
527   SD->setBundleLockState(MCSectionData::NotBundleLocked);
528 }
529
530 void MCELFStreamer::Flush() {
531   for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
532                                                 e = LocalCommons.end();
533        i != e; ++i) {
534     MCSymbolData *SD = i->SD;
535     uint64_t Size = i->Size;
536     unsigned ByteAlignment = i->ByteAlignment;
537     const MCSymbol &Symbol = SD->getSymbol();
538     const MCSection &Section = Symbol.getSection();
539
540     MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
541     new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
542
543     MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
544     SD->setFragment(F);
545
546     // Update the maximum alignment of the section if necessary.
547     if (ByteAlignment > SectData.getAlignment())
548       SectData.setAlignment(ByteAlignment);
549   }
550
551   LocalCommons.clear();
552 }
553
554 void MCELFStreamer::FinishImpl() {
555   EmitFrames(nullptr);
556
557   Flush();
558
559   this->MCObjectStreamer::FinishImpl();
560 }
561
562 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
563                                     raw_ostream &OS, MCCodeEmitter *CE,
564                                     bool RelaxAll) {
565   MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
566   if (RelaxAll)
567     S->getAssembler().setRelaxAll(true);
568   return S;
569 }
570
571 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
572   llvm_unreachable("Generic ELF doesn't support this directive");
573 }
574
575 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
576   llvm_unreachable("ELF doesn't support this directive");
577 }
578
579 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
580   llvm_unreachable("ELF doesn't support this directive");
581 }
582
583 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
584   llvm_unreachable("ELF doesn't support this directive");
585 }
586
587 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
588   llvm_unreachable("ELF doesn't support this directive");
589 }
590
591 void MCELFStreamer::EndCOFFSymbolDef() {
592   llvm_unreachable("ELF doesn't support this directive");
593 }
594
595 void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
596                                  uint64_t Size, unsigned ByteAlignment) {
597   llvm_unreachable("ELF doesn't support this directive");
598 }
599
600 void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
601                                    uint64_t Size, unsigned ByteAlignment) {
602   llvm_unreachable("ELF doesn't support this directive");
603 }