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/MCELF.h"
24 #include "llvm/MC/MCELFSymbolFlags.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCObjectFileInfo.h"
28 #include "llvm/MC/MCObjectStreamer.h"
29 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCSectionELF.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCValue.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ELF.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/raw_ostream.h"
41 bool MCELFStreamer::isBundleLocked() const {
42 return getCurrentSectionOnly()->isBundleLocked();
45 MCELFStreamer::~MCELFStreamer() {
48 void MCELFStreamer::mergeFragment(MCDataFragment *DF,
49 MCEncodedFragmentWithFixups *EF) {
50 MCAssembler &Assembler = getAssembler();
52 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
53 uint64_t FSize = EF->getContents().size();
55 if (FSize > Assembler.getBundleAlignSize())
56 report_fatal_error("Fragment can't be larger than a bundle size");
58 uint64_t RequiredBundlePadding = computeBundlePadding(
59 Assembler, EF, DF->getContents().size(), FSize);
61 if (RequiredBundlePadding > UINT8_MAX)
62 report_fatal_error("Padding cannot exceed 255 bytes");
64 if (RequiredBundlePadding > 0) {
65 SmallString<256> Code;
66 raw_svector_ostream VecOS(Code);
67 MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
69 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
71 Assembler.writeFragmentPadding(*EF, FSize, OW);
75 DF->getContents().append(Code.begin(), Code.end());
79 flushPendingLabels(DF, DF->getContents().size());
81 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
82 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
83 DF->getContents().size());
84 DF->getFixups().push_back(EF->getFixups()[i]);
86 DF->setHasInstructions(true);
87 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
90 void MCELFStreamer::InitSections(bool NoExecStack) {
91 // This emulates the same behavior of GNU as. This makes it easier
92 // to compare the output as the major sections are in the same order.
93 MCContext &Ctx = getContext();
94 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
97 SwitchSection(Ctx.getObjectFileInfo()->getDataSection());
100 SwitchSection(Ctx.getObjectFileInfo()->getBSSSection());
101 EmitCodeAlignment(4);
103 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
106 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
109 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
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 MCELF::SetType(*Symbol, 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.getOrCreateSymbolData(*Grp);
160 this->MCObjectStreamer::ChangeSection(Section, Subsection);
161 MCContext &Ctx = getContext();
162 MCSymbol *Begin = Section->getBeginSymbol();
164 Begin = Ctx.getOrCreateSectionSymbol(*SectionELF);
165 Section->setBeginSymbol(Begin);
167 if (Begin->isUndefined()) {
168 Asm.getOrCreateSymbolData(*Begin);
169 MCELF::SetType(*Begin, ELF::STT_SECTION);
173 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
174 getAssembler().getOrCreateSymbolData(*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 *Symbol,
200 MCSymbolAttr Attribute) {
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 getOrCreateSymbolData here is to register
215 // the symbol with the assembler.
216 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*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:
241 *Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_OBJECT));
242 MCELF::SetBinding(*Symbol, ELF::STB_GNU_UNIQUE);
243 SD.setExternal(true);
244 BindingExplicitlySet.insert(Symbol);
248 MCELF::SetBinding(*Symbol, ELF::STB_GLOBAL);
249 SD.setExternal(true);
250 BindingExplicitlySet.insert(Symbol);
253 case MCSA_WeakReference:
255 MCELF::SetBinding(*Symbol, ELF::STB_WEAK);
256 SD.setExternal(true);
257 BindingExplicitlySet.insert(Symbol);
261 MCELF::SetBinding(*Symbol, ELF::STB_LOCAL);
262 SD.setExternal(false);
263 BindingExplicitlySet.insert(Symbol);
266 case MCSA_ELF_TypeFunction:
267 MCELF::SetType(*Symbol,
268 CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_FUNC));
271 case MCSA_ELF_TypeIndFunction:
272 MCELF::SetType(*Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol),
273 ELF::STT_GNU_IFUNC));
276 case MCSA_ELF_TypeObject:
278 *Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_OBJECT));
281 case MCSA_ELF_TypeTLS:
282 MCELF::SetType(*Symbol,
283 CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_TLS));
286 case MCSA_ELF_TypeCommon:
287 // TODO: Emit these as a common symbol.
289 *Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_OBJECT));
292 case MCSA_ELF_TypeNoType:
294 *Symbol, CombineSymbolTypes(MCELF::GetType(*Symbol), ELF::STT_NOTYPE));
298 MCELF::SetVisibility(*Symbol, ELF::STV_PROTECTED);
302 MCELF::SetVisibility(*Symbol, ELF::STV_HIDDEN);
306 MCELF::SetVisibility(*Symbol, ELF::STV_INTERNAL);
313 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
314 unsigned ByteAlignment) {
315 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
317 if (!BindingExplicitlySet.count(Symbol)) {
318 MCELF::SetBinding(*Symbol, ELF::STB_GLOBAL);
319 SD.setExternal(true);
322 MCELF::SetType(*Symbol, ELF::STT_OBJECT);
324 if (MCELF::GetBinding(*Symbol) == ELF_STB_Local) {
325 MCSection *Section = getAssembler().getContext().getELFSection(
326 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
328 AssignSection(Symbol, Section);
330 struct LocalCommon L = {Symbol, Size, ByteAlignment};
331 LocalCommons.push_back(L);
333 Symbol->setCommon(Size, ByteAlignment);
336 Symbol->setSize(MCConstantExpr::Create(Size, getContext()));
339 void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
340 Symbol->setSize(Value);
343 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
344 unsigned ByteAlignment) {
345 // FIXME: Should this be caught and done earlier?
346 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
347 MCELF::SetBinding(*Symbol, ELF::STB_LOCAL);
348 SD.setExternal(false);
349 BindingExplicitlySet.insert(Symbol);
350 EmitCommonSymbol(Symbol, Size, ByteAlignment);
353 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
355 if (isBundleLocked())
356 report_fatal_error("Emitting values inside a locked bundle is forbidden");
357 fixSymbolsInTLSFixups(Value);
358 MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
361 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
364 unsigned MaxBytesToEmit) {
365 if (isBundleLocked())
366 report_fatal_error("Emitting values inside a locked bundle is forbidden");
367 MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
368 ValueSize, MaxBytesToEmit);
371 // Add a symbol for the file name of this module. They start after the
372 // null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
373 // with the same name may appear.
374 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
375 getAssembler().addFileName(Filename);
378 void MCELFStreamer::EmitIdent(StringRef IdentString) {
379 MCSection *Comment = getAssembler().getContext().getELFSection(
380 ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
382 SwitchSection(Comment);
387 EmitBytes(IdentString);
392 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
393 switch (expr->getKind()) {
395 cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
397 case MCExpr::Constant:
400 case MCExpr::Binary: {
401 const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
402 fixSymbolsInTLSFixups(be->getLHS());
403 fixSymbolsInTLSFixups(be->getRHS());
407 case MCExpr::SymbolRef: {
408 const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
409 switch (symRef.getKind()) {
412 case MCSymbolRefExpr::VK_GOTTPOFF:
413 case MCSymbolRefExpr::VK_INDNTPOFF:
414 case MCSymbolRefExpr::VK_NTPOFF:
415 case MCSymbolRefExpr::VK_GOTNTPOFF:
416 case MCSymbolRefExpr::VK_TLSGD:
417 case MCSymbolRefExpr::VK_TLSLD:
418 case MCSymbolRefExpr::VK_TLSLDM:
419 case MCSymbolRefExpr::VK_TPOFF:
420 case MCSymbolRefExpr::VK_DTPOFF:
421 case MCSymbolRefExpr::VK_Mips_TLSGD:
422 case MCSymbolRefExpr::VK_Mips_GOTTPREL:
423 case MCSymbolRefExpr::VK_Mips_TPREL_HI:
424 case MCSymbolRefExpr::VK_Mips_TPREL_LO:
425 case MCSymbolRefExpr::VK_PPC_DTPMOD:
426 case MCSymbolRefExpr::VK_PPC_TPREL:
427 case MCSymbolRefExpr::VK_PPC_TPREL_LO:
428 case MCSymbolRefExpr::VK_PPC_TPREL_HI:
429 case MCSymbolRefExpr::VK_PPC_TPREL_HA:
430 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
431 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
432 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
433 case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
434 case MCSymbolRefExpr::VK_PPC_DTPREL:
435 case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
436 case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
437 case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
438 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
439 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
440 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
441 case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
442 case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
443 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
444 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
445 case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
446 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
447 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
448 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
449 case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
450 case MCSymbolRefExpr::VK_PPC_TLS:
451 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
452 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
453 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
454 case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
455 case MCSymbolRefExpr::VK_PPC_TLSGD:
456 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
457 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
458 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
459 case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
460 case MCSymbolRefExpr::VK_PPC_TLSLD:
463 getAssembler().getOrCreateSymbolData(symRef.getSymbol());
464 MCELF::SetType(symRef.getSymbol(), ELF::STT_TLS);
469 fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
474 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
475 const MCSubtargetInfo &STI) {
476 this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
477 MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
479 for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
480 fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
483 void MCELFStreamer::EmitInstToData(const MCInst &Inst,
484 const MCSubtargetInfo &STI) {
485 MCAssembler &Assembler = getAssembler();
486 SmallVector<MCFixup, 4> Fixups;
487 SmallString<256> Code;
488 raw_svector_ostream VecOS(Code);
489 Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
492 for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
493 fixSymbolsInTLSFixups(Fixups[i].getValue());
495 // There are several possibilities here:
497 // If bundling is disabled, append the encoded instruction to the current data
498 // fragment (or create a new such fragment if the current fragment is not a
501 // If bundling is enabled:
502 // - If we're not in a bundle-locked group, emit the instruction into a
503 // fragment of its own. If there are no fixups registered for the
504 // instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
506 // - If we're in a bundle-locked group, append the instruction to the current
507 // data fragment because we want all the instructions in a group to get into
508 // the same fragment. Be careful not to do that for the first instruction in
509 // the group, though.
512 if (Assembler.isBundlingEnabled()) {
513 MCSection &Sec = *getCurrentSectionOnly();
514 if (Assembler.getRelaxAll() && isBundleLocked())
515 // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
516 // the current bundle group.
517 DF = BundleGroups.back();
518 else if (Assembler.getRelaxAll() && !isBundleLocked())
519 // When not in a bundle-locked group and the -mc-relax-all flag is used,
520 // we create a new temporary fragment which will be later merged into
521 // the current fragment.
522 DF = new MCDataFragment();
523 else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst())
524 // If we are bundle-locked, we re-use the current fragment.
525 // The bundle-locking directive ensures this is a new data fragment.
526 DF = cast<MCDataFragment>(getCurrentFragment());
527 else if (!isBundleLocked() && Fixups.size() == 0) {
528 // Optimize memory usage by emitting the instruction to a
529 // MCCompactEncodedInstFragment when not in a bundle-locked group and
530 // there are no fixups registered.
531 MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
533 CEIF->getContents().append(Code.begin(), Code.end());
536 DF = new MCDataFragment();
539 if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
540 // If this fragment is for a group marked "align_to_end", set a flag
541 // in the fragment. This can happen after the fragment has already been
542 // created if there are nested bundle_align groups and an inner one
543 // is the one marked align_to_end.
544 DF->setAlignToBundleEnd(true);
547 // We're now emitting an instruction in a bundle group, so this flag has
549 Sec.setBundleGroupBeforeFirstInst(false);
551 DF = getOrCreateDataFragment();
554 // Add the fixups and data.
555 for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
556 Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
557 DF->getFixups().push_back(Fixups[i]);
559 DF->setHasInstructions(true);
560 DF->getContents().append(Code.begin(), Code.end());
562 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
563 if (!isBundleLocked()) {
564 mergeFragment(getOrCreateDataFragment(), DF);
570 void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
571 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
572 MCAssembler &Assembler = getAssembler();
573 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
574 Assembler.getBundleAlignSize() == 1U << AlignPow2))
575 Assembler.setBundleAlignSize(1U << AlignPow2);
577 report_fatal_error(".bundle_align_mode cannot be changed once set");
580 void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
581 MCSection &Sec = *getCurrentSectionOnly();
585 if (!getAssembler().isBundlingEnabled())
586 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
588 if (!isBundleLocked())
589 Sec.setBundleGroupBeforeFirstInst(true);
591 if (getAssembler().getRelaxAll() && !isBundleLocked()) {
592 // TODO: drop the lock state and set directly in the fragment
593 MCDataFragment *DF = new MCDataFragment();
594 BundleGroups.push_back(DF);
597 Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
598 : MCSection::BundleLocked);
601 void MCELFStreamer::EmitBundleUnlock() {
602 MCSection &Sec = *getCurrentSectionOnly();
605 if (!getAssembler().isBundlingEnabled())
606 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
607 else if (!isBundleLocked())
608 report_fatal_error(".bundle_unlock without matching lock");
609 else if (Sec.isBundleGroupBeforeFirstInst())
610 report_fatal_error("Empty bundle-locked group is forbidden");
612 // When the -mc-relax-all flag is used, we emit instructions to fragments
613 // stored on a stack. When the bundle unlock is emited, we pop a fragment
614 // from the stack a merge it to the one below.
615 if (getAssembler().getRelaxAll()) {
616 assert(!BundleGroups.empty() && "There are no bundle groups");
617 MCDataFragment *DF = BundleGroups.back();
619 // FIXME: Use BundleGroups to track the lock state instead.
620 Sec.setBundleLockState(MCSection::NotBundleLocked);
622 // FIXME: Use more separate fragments for nested groups.
623 if (!isBundleLocked()) {
624 mergeFragment(getOrCreateDataFragment(), DF);
625 BundleGroups.pop_back();
629 if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
630 getOrCreateDataFragment()->setAlignToBundleEnd(false);
632 Sec.setBundleLockState(MCSection::NotBundleLocked);
635 void MCELFStreamer::Flush() {
636 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
637 e = LocalCommons.end();
639 const MCSymbol &Symbol = *i->Symbol;
640 uint64_t Size = i->Size;
641 unsigned ByteAlignment = i->ByteAlignment;
642 MCSection &Section = Symbol.getSection();
644 getAssembler().registerSection(Section);
645 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &Section);
647 MCFragment *F = new MCFillFragment(0, 0, Size, &Section);
648 Symbol.getData().setFragment(F);
650 // Update the maximum alignment of the section if necessary.
651 if (ByteAlignment > Section.getAlignment())
652 Section.setAlignment(ByteAlignment);
655 LocalCommons.clear();
658 void MCELFStreamer::FinishImpl() {
659 // Ensure the last section gets aligned if necessary.
660 MCSection *CurSection = getCurrentSectionOnly();
661 setSectionAlignmentForBundling(getAssembler(), CurSection);
667 this->MCObjectStreamer::FinishImpl();
670 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
671 raw_pwrite_stream &OS, MCCodeEmitter *CE,
673 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
675 S->getAssembler().setRelaxAll(true);
679 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
680 llvm_unreachable("Generic ELF doesn't support this directive");
683 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
684 llvm_unreachable("ELF doesn't support this directive");
687 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
688 llvm_unreachable("ELF doesn't support this directive");
691 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
692 llvm_unreachable("ELF doesn't support this directive");
695 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
696 llvm_unreachable("ELF doesn't support this directive");
699 void MCELFStreamer::EndCOFFSymbolDef() {
700 llvm_unreachable("ELF doesn't support this directive");
703 void MCELFStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
704 uint64_t Size, unsigned ByteAlignment) {
705 llvm_unreachable("ELF doesn't support this directive");
708 void MCELFStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
709 uint64_t Size, unsigned ByteAlignment) {
710 llvm_unreachable("ELF doesn't support this directive");