1 //===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file assembles .s files and emits ELF .o object files.
12 //===----------------------------------------------------------------------===//
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/MCAsmLayout.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCExpr.h"
24 #include "llvm/MC/MCInst.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCObjectStreamer.h"
27 #include "llvm/MC/MCSection.h"
28 #include "llvm/MC/MCSectionELF.h"
29 #include "llvm/MC/MCSymbolELF.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/TargetRegistry.h"
36 #include "llvm/Support/raw_ostream.h"
40 bool MCELFStreamer::isBundleLocked() const {
41 return getCurrentSectionOnly()->isBundleLocked();
44 MCELFStreamer::~MCELFStreamer() {
47 void MCELFStreamer::mergeFragment(MCDataFragment *DF,
49 MCAssembler &Assembler = getAssembler();
51 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
52 uint64_t FSize = EF->getContents().size();
54 if (FSize > Assembler.getBundleAlignSize())
55 report_fatal_error("Fragment can't be larger than a bundle size");
57 uint64_t RequiredBundlePadding = computeBundlePadding(
58 Assembler, EF, DF->getContents().size(), FSize);
60 if (RequiredBundlePadding > UINT8_MAX)
61 report_fatal_error("Padding cannot exceed 255 bytes");
63 if (RequiredBundlePadding > 0) {
64 SmallString<256> Code;
65 raw_svector_ostream VecOS(Code);
66 MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
68 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
70 Assembler.writeFragmentPadding(*EF, FSize, OW);
74 DF->getContents().append(Code.begin(), Code.end());
78 flushPendingLabels(DF, DF->getContents().size());
80 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
81 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
82 DF->getContents().size());
83 DF->getFixups().push_back(EF->getFixups()[i]);
85 DF->setHasInstructions(true);
86 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
89 void MCELFStreamer::InitSections(bool NoExecStack) {
90 // This emulates the same behavior of GNU as. This makes it easier
91 // to compare the output as the major sections are in the same order.
92 MCContext &Ctx = getContext();
93 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
96 SwitchSection(Ctx.getObjectFileInfo()->getDataSection());
99 SwitchSection(Ctx.getObjectFileInfo()->getBSSSection());
100 EmitCodeAlignment(4);
102 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
105 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
108 void MCELFStreamer::EmitLabel(MCSymbol *S) {
109 auto *Symbol = cast<MCSymbolELF>(S);
110 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
112 MCObjectStreamer::EmitLabel(Symbol);
114 const MCSectionELF &Section =
115 static_cast<const MCSectionELF&>(Symbol->getSection());
116 if (Section.getFlags() & ELF::SHF_TLS)
117 Symbol->setType(ELF::STT_TLS);
120 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
121 // Let the target do whatever target specific stuff it needs to do.
122 getAssembler().getBackend().handleAssemblerFlag(Flag);
123 // Do any generic stuff we need to do.
125 case MCAF_SyntaxUnified: return; // no-op here.
126 case MCAF_Code16: return; // Change parsing mode; no-op here.
127 case MCAF_Code32: return; // Change parsing mode; no-op here.
128 case MCAF_Code64: return; // Change parsing mode; no-op here.
129 case MCAF_SubsectionsViaSymbols:
130 getAssembler().setSubsectionsViaSymbols(true);
134 llvm_unreachable("invalid assembler flag!");
137 // If bundle aligment is used and there are any instructions in the section, it
138 // needs to be aligned to at least the bundle size.
139 static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
140 MCSection *Section) {
141 if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
142 Section->getAlignment() < Assembler.getBundleAlignSize())
143 Section->setAlignment(Assembler.getBundleAlignSize());
146 void MCELFStreamer::ChangeSection(MCSection *Section,
147 const MCExpr *Subsection) {
148 MCSection *CurSection = getCurrentSectionOnly();
149 if (CurSection && isBundleLocked())
150 report_fatal_error("Unterminated .bundle_lock when changing a section");
152 MCAssembler &Asm = getAssembler();
153 // Ensure the previous section gets aligned if necessary.
154 setSectionAlignmentForBundling(Asm, CurSection);
155 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
156 const MCSymbol *Grp = SectionELF->getGroup();
158 Asm.registerSymbol(*Grp);
160 this->MCObjectStreamer::ChangeSection(Section, Subsection);
161 MCContext &Ctx = getContext();
162 auto *Begin = cast_or_null<MCSymbolELF>(Section->getBeginSymbol());
164 Begin = Ctx.getOrCreateSectionSymbol(*SectionELF);
165 Section->setBeginSymbol(Begin);
167 if (Begin->isUndefined()) {
168 Asm.registerSymbol(*Begin);
169 Begin->setType(ELF::STT_SECTION);
173 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
174 getAssembler().registerSymbol(*Symbol);
175 const MCExpr *Value = MCSymbolRefExpr::create(
176 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
177 Alias->setVariableValue(Value);
180 // When GNU as encounters more than one .type declaration for an object it seems
181 // to use a mechanism similar to the one below to decide which type is actually
182 // used in the object file. The greater of T1 and T2 is selected based on the
183 // following ordering:
184 // STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
185 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
187 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
188 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
189 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
199 bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
200 auto *Symbol = cast<MCSymbolELF>(S);
201 // Indirect symbols are handled differently, to match how 'as' handles
202 // them. This makes writing matching .o files easier.
203 if (Attribute == MCSA_IndirectSymbol) {
204 // Note that we intentionally cannot use the symbol data here; this is
205 // important for matching the string table that 'as' generates.
206 IndirectSymbolData ISD;
208 ISD.Section = getCurrentSectionOnly();
209 getAssembler().getIndirectSymbols().push_back(ISD);
213 // Adding a symbol attribute always introduces the symbol, note that an
214 // important side effect of calling registerSymbol here is to register
215 // the symbol with the assembler.
216 getAssembler().registerSymbol(*Symbol);
218 // The implementation of symbol attributes is designed to match 'as', but it
219 // leaves much to desired. It doesn't really make sense to arbitrarily add and
220 // remove flags, but 'as' allows this (in particular, see .desc).
222 // In the future it might be worth trying to make these operations more well
225 case MCSA_LazyReference:
227 case MCSA_SymbolResolver:
228 case MCSA_PrivateExtern:
229 case MCSA_WeakDefinition:
230 case MCSA_WeakDefAutoPrivate:
232 case MCSA_IndirectSymbol:
235 case MCSA_NoDeadStrip:
239 case MCSA_ELF_TypeGnuUniqueObject:
240 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
241 Symbol->setBinding(ELF::STB_GNU_UNIQUE);
242 Symbol->setExternal(true);
246 Symbol->setBinding(ELF::STB_GLOBAL);
247 Symbol->setExternal(true);
250 case MCSA_WeakReference:
252 Symbol->setBinding(ELF::STB_WEAK);
253 Symbol->setExternal(true);
257 Symbol->setBinding(ELF::STB_LOCAL);
258 Symbol->setExternal(false);
261 case MCSA_ELF_TypeFunction:
262 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
265 case MCSA_ELF_TypeIndFunction:
266 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
269 case MCSA_ELF_TypeObject:
270 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
273 case MCSA_ELF_TypeTLS:
274 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
277 case MCSA_ELF_TypeCommon:
278 // TODO: Emit these as a common symbol.
279 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
282 case MCSA_ELF_TypeNoType:
283 Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
287 Symbol->setVisibility(ELF::STV_PROTECTED);
291 Symbol->setVisibility(ELF::STV_HIDDEN);
295 Symbol->setVisibility(ELF::STV_INTERNAL);
302 void MCELFStreamer::EmitCommonSymbol(MCSymbol *S, uint64_t Size,
303 unsigned ByteAlignment) {
304 auto *Symbol = cast<MCSymbolELF>(S);
305 getAssembler().registerSymbol(*Symbol);
307 if (!Symbol->isBindingSet()) {
308 Symbol->setBinding(ELF::STB_GLOBAL);
309 Symbol->setExternal(true);
312 Symbol->setType(ELF::STT_OBJECT);
314 if (Symbol->getBinding() == ELF::STB_LOCAL) {
315 MCSection *Section = getAssembler().getContext().getELFSection(
316 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
318 AssignSection(Symbol, Section);
320 struct LocalCommon L = {Symbol, Size, ByteAlignment};
321 LocalCommons.push_back(L);
323 if(Symbol->declareCommon(Size, ByteAlignment))
324 report_fatal_error("Symbol: " + Symbol->getName() +
325 " redeclared as different type");
328 cast<MCSymbolELF>(Symbol)
329 ->setSize(MCConstantExpr::create(Size, getContext()));
332 void MCELFStreamer::emitELFSize(MCSymbolELF *Symbol, const MCExpr *Value) {
333 Symbol->setSize(Value);
336 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
337 unsigned ByteAlignment) {
338 auto *Symbol = cast<MCSymbolELF>(S);
339 // FIXME: Should this be caught and done earlier?
340 getAssembler().registerSymbol(*Symbol);
341 Symbol->setBinding(ELF::STB_LOCAL);
342 Symbol->setExternal(false);
343 EmitCommonSymbol(Symbol, Size, ByteAlignment);
346 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
348 if (isBundleLocked())
349 report_fatal_error("Emitting values inside a locked bundle is forbidden");
350 fixSymbolsInTLSFixups(Value);
351 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
354 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
357 unsigned MaxBytesToEmit) {
358 if (isBundleLocked())
359 report_fatal_error("Emitting values inside a locked bundle is forbidden");
360 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
361 ValueSize, MaxBytesToEmit);
364 // Add a symbol for the file name of this module. They start after the
365 // null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
366 // with the same name may appear.
367 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
368 getAssembler().addFileName(Filename);
371 void MCELFStreamer::EmitIdent(StringRef IdentString) {
372 MCSection *Comment = getAssembler().getContext().getELFSection(
373 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
375 SwitchSection(Comment);
380 EmitBytes(IdentString);
385 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
386 switch (expr->getKind()) {
388 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
390 case MCExpr::Constant:
393 case MCExpr::Binary: {
394 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
395 fixSymbolsInTLSFixups(be->getLHS());
396 fixSymbolsInTLSFixups(be->getRHS());
400 case MCExpr::SymbolRef: {
401 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
402 switch (symRef.getKind()) {
405 case MCSymbolRefExpr::VK_GOTTPOFF:
406 case MCSymbolRefExpr::VK_INDNTPOFF:
407 case MCSymbolRefExpr::VK_NTPOFF:
408 case MCSymbolRefExpr::VK_GOTNTPOFF:
409 case MCSymbolRefExpr::VK_TLSGD:
410 case MCSymbolRefExpr::VK_TLSLD:
411 case MCSymbolRefExpr::VK_TLSLDM:
412 case MCSymbolRefExpr::VK_TPOFF:
413 case MCSymbolRefExpr::VK_DTPOFF:
414 case MCSymbolRefExpr::VK_Mips_TLSGD:
415 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
416 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
417 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
418 case MCSymbolRefExpr::VK_PPC_DTPMOD:
419 case MCSymbolRefExpr::VK_PPC_TPREL:
420 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
421 case MCSymbolRefExpr::VK_PPC_TPREL_HI:
422 case MCSymbolRefExpr::VK_PPC_TPREL_HA:
423 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
424 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
425 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
426 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
427 case MCSymbolRefExpr::VK_PPC_DTPREL:
428 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
429 case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
430 case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
431 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
432 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
433 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
434 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
435 case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
436 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
437 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
438 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
439 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
440 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
441 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
442 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
443 case MCSymbolRefExpr::VK_PPC_TLS:
444 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
445 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
446 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
447 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
448 case MCSymbolRefExpr::VK_PPC_TLSGD:
449 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
450 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
451 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
452 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
453 case MCSymbolRefExpr::VK_PPC_TLSLD:
456 getAssembler().registerSymbol(symRef.getSymbol());
457 cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
462 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
467 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
468 const MCSubtargetInfo &STI) {
469 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
470 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
472 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
473 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
476 void MCELFStreamer::EmitInstToData(const MCInst &Inst,
477 const MCSubtargetInfo &STI) {
478 MCAssembler &Assembler = getAssembler();
479 SmallVector<MCFixup, 4> Fixups;
480 SmallString<256> Code;
481 raw_svector_ostream VecOS(Code);
482 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
485 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
486 fixSymbolsInTLSFixups(Fixups[i].getValue());
488 // There are several possibilities here:
490 // If bundling is disabled, append the encoded instruction to the current data
491 // fragment (or create a new such fragment if the current fragment is not a
494 // If bundling is enabled:
495 // - If we're not in a bundle-locked group, emit the instruction into a
496 // fragment of its own. If there are no fixups registered for the
497 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
499 // - If we're in a bundle-locked group, append the instruction to the current
500 // data fragment because we want all the instructions in a group to get into
501 // the same fragment. Be careful not to do that for the first instruction in
502 // the group, though.
505 if (Assembler.isBundlingEnabled()) {
506 MCSection &Sec = *getCurrentSectionOnly();
507 if (Assembler.getRelaxAll() && isBundleLocked())
508 // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
509 // the current bundle group.
510 DF = BundleGroups.back();
511 else if (Assembler.getRelaxAll() && !isBundleLocked())
512 // When not in a bundle-locked group and the -mc-relax-all flag is used,
513 // we create a new temporary fragment which will be later merged into
514 // the current fragment.
515 DF = new MCDataFragment();
516 else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst())
517 // If we are bundle-locked, we re-use the current fragment.
518 // The bundle-locking directive ensures this is a new data fragment.
519 DF = cast<MCDataFragment>(getCurrentFragment());
520 else if (!isBundleLocked() && Fixups.size() == 0) {
521 // Optimize memory usage by emitting the instruction to a
522 // MCCompactEncodedInstFragment when not in a bundle-locked group and
523 // there are no fixups registered.
524 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
526 CEIF->getContents().append(Code.begin(), Code.end());
529 DF = new MCDataFragment();
532 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
533 // If this fragment is for a group marked "align_to_end", set a flag
534 // in the fragment. This can happen after the fragment has already been
535 // created if there are nested bundle_align groups and an inner one
536 // is the one marked align_to_end.
537 DF->setAlignToBundleEnd(true);
540 // We're now emitting an instruction in a bundle group, so this flag has
542 Sec.setBundleGroupBeforeFirstInst(false);
544 DF = getOrCreateDataFragment();
547 // Add the fixups and data.
548 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
549 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
550 DF->getFixups().push_back(Fixups[i]);
552 DF->setHasInstructions(true);
553 DF->getContents().append(Code.begin(), Code.end());
555 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
556 if (!isBundleLocked()) {
557 mergeFragment(getOrCreateDataFragment(), DF);
563 void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
564 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
565 MCAssembler &Assembler = getAssembler();
566 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
567 Assembler.getBundleAlignSize() == 1U << AlignPow2))
568 Assembler.setBundleAlignSize(1U << AlignPow2);
570 report_fatal_error(".bundle_align_mode cannot be changed once set");
573 void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
574 MCSection &Sec = *getCurrentSectionOnly();
578 if (!getAssembler().isBundlingEnabled())
579 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
581 if (!isBundleLocked())
582 Sec.setBundleGroupBeforeFirstInst(true);
584 if (getAssembler().getRelaxAll() && !isBundleLocked()) {
585 // TODO: drop the lock state and set directly in the fragment
586 MCDataFragment *DF = new MCDataFragment();
587 BundleGroups.push_back(DF);
590 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
591 : MCSection::BundleLocked);
594 void MCELFStreamer::EmitBundleUnlock() {
595 MCSection &Sec = *getCurrentSectionOnly();
598 if (!getAssembler().isBundlingEnabled())
599 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
600 else if (!isBundleLocked())
601 report_fatal_error(".bundle_unlock without matching lock");
602 else if (Sec.isBundleGroupBeforeFirstInst())
603 report_fatal_error("Empty bundle-locked group is forbidden");
605 // When the -mc-relax-all flag is used, we emit instructions to fragments
606 // stored on a stack. When the bundle unlock is emited, we pop a fragment
607 // from the stack a merge it to the one below.
608 if (getAssembler().getRelaxAll()) {
609 assert(!BundleGroups.empty() && "There are no bundle groups");
610 MCDataFragment *DF = BundleGroups.back();
612 // FIXME: Use BundleGroups to track the lock state instead.
613 Sec.setBundleLockState(MCSection::NotBundleLocked);
615 // FIXME: Use more separate fragments for nested groups.
616 if (!isBundleLocked()) {
617 mergeFragment(getOrCreateDataFragment(), DF);
618 BundleGroups.pop_back();
622 if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
623 getOrCreateDataFragment()->setAlignToBundleEnd(false);
625 Sec.setBundleLockState(MCSection::NotBundleLocked);
628 void MCELFStreamer::Flush() {
629 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
630 e = LocalCommons.end();
632 const MCSymbol &Symbol = *i->Symbol;
633 uint64_t Size = i->Size;
634 unsigned ByteAlignment = i->ByteAlignment;
635 MCSection &Section = Symbol.getSection();
637 getAssembler().registerSection(Section);
638 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &Section);
640 MCFragment *F = new MCFillFragment(0, 0, Size, &Section);
641 Symbol.setFragment(F);
643 // Update the maximum alignment of the section if necessary.
644 if (ByteAlignment > Section.getAlignment())
645 Section.setAlignment(ByteAlignment);
648 LocalCommons.clear();
651 void MCELFStreamer::FinishImpl() {
652 // Ensure the last section gets aligned if necessary.
653 MCSection *CurSection = getCurrentSectionOnly();
654 setSectionAlignmentForBundling(getAssembler(), CurSection);
660 this->MCObjectStreamer::FinishImpl();
663 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
664 raw_pwrite_stream &OS, MCCodeEmitter *CE,
666 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
668 S->getAssembler().setRelaxAll(true);
672 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
673 llvm_unreachable("Generic ELF doesn't support this directive");
676 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
677 llvm_unreachable("ELF doesn't support this directive");
680 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
681 llvm_unreachable("ELF doesn't support this directive");
684 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
685 llvm_unreachable("ELF doesn't support this directive");
688 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
689 llvm_unreachable("ELF doesn't support this directive");
692 void MCELFStreamer::EndCOFFSymbolDef() {
693 llvm_unreachable("ELF doesn't support this directive");
696 void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
697 uint64_t Size, unsigned ByteAlignment) {
698 llvm_unreachable("ELF doesn't support this directive");
701 void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
702 uint64_t Size, unsigned ByteAlignment) {
703 llvm_unreachable("ELF doesn't support this directive");