OSDN Git Service

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