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