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 MCELFStreamer::~MCELFStreamer() {
44 void MCELFStreamer::mergeFragment(MCDataFragment *DF,
45 MCEncodedFragmentWithFixups *EF) {
46 MCAssembler &Assembler = getAssembler();
48 if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
49 uint64_t FSize = EF->getContents().size();
51 if (FSize > Assembler.getBundleAlignSize())
52 report_fatal_error("Fragment can't be larger than a bundle size");
54 uint64_t RequiredBundlePadding = computeBundlePadding(
55 Assembler, EF, DF->getContents().size(), FSize);
57 if (RequiredBundlePadding > UINT8_MAX)
58 report_fatal_error("Padding cannot exceed 255 bytes");
60 if (RequiredBundlePadding > 0) {
61 SmallString<256> Code;
62 raw_svector_ostream VecOS(Code);
63 MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
65 EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
67 Assembler.writeFragmentPadding(*EF, FSize, OW);
71 DF->getContents().append(Code.begin(), Code.end());
75 flushPendingLabels(DF, DF->getContents().size());
77 for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
78 EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
79 DF->getContents().size());
80 DF->getFixups().push_back(EF->getFixups()[i]);
82 DF->setHasInstructions(true);
83 DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
86 void MCELFStreamer::InitSections(bool NoExecStack) {
87 // This emulates the same behavior of GNU as. This makes it easier
88 // to compare the output as the major sections are in the same order.
89 MCContext &Ctx = getContext();
90 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
93 SwitchSection(Ctx.getObjectFileInfo()->getDataSection());
96 SwitchSection(Ctx.getObjectFileInfo()->getBSSSection());
99 SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
102 SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
105 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
106 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
108 MCObjectStreamer::EmitLabel(Symbol);
110 const MCSectionELF &Section =
111 static_cast<const MCSectionELF&>(Symbol->getSection());
112 MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
113 if (Section.getFlags() & ELF::SHF_TLS)
114 MCELF::SetType(SD, ELF::STT_TLS);
117 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
118 // Let the target do whatever target specific stuff it needs to do.
119 getAssembler().getBackend().handleAssemblerFlag(Flag);
120 // Do any generic stuff we need to do.
122 case MCAF_SyntaxUnified: return; // no-op here.
123 case MCAF_Code16: return; // Change parsing mode; no-op here.
124 case MCAF_Code32: return; // Change parsing mode; no-op here.
125 case MCAF_Code64: return; // Change parsing mode; no-op here.
126 case MCAF_SubsectionsViaSymbols:
127 getAssembler().setSubsectionsViaSymbols(true);
131 llvm_unreachable("invalid assembler flag!");
134 // If bundle aligment is used and there are any instructions in the section, it
135 // needs to be aligned to at least the bundle size.
136 static void setSectionAlignmentForBundling(
137 const MCAssembler &Assembler, MCSectionData *Section) {
138 if (Assembler.isBundlingEnabled() && Section &&
139 Section->hasInstructions() &&
140 Section->getAlignment() < Assembler.getBundleAlignSize())
141 Section->setAlignment(Assembler.getBundleAlignSize());
144 void MCELFStreamer::ChangeSection(const MCSection *Section,
145 const MCExpr *Subsection) {
146 MCSectionData *CurSection = getCurrentSectionData();
147 if (CurSection && CurSection->isBundleLocked())
148 report_fatal_error("Unterminated .bundle_lock when changing a section");
150 MCAssembler &Asm = getAssembler();
151 // Ensure the previous section gets aligned if necessary.
152 setSectionAlignmentForBundling(Asm, CurSection);
153 auto *SectionELF = static_cast<const MCSectionELF *>(Section);
154 const MCSymbol *Grp = SectionELF->getGroup();
156 Asm.getOrCreateSymbolData(*Grp);
158 this->MCObjectStreamer::ChangeSection(Section, Subsection);
159 MCSymbol *SectionSymbol = getContext().getOrCreateSectionSymbol(*SectionELF);
160 if (SectionSymbol->isUndefined()) {
161 EmitLabel(SectionSymbol);
162 MCELF::SetType(Asm.getSymbolData(*SectionSymbol), ELF::STT_SECTION);
166 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
167 getAssembler().getOrCreateSymbolData(*Symbol);
168 const MCExpr *Value = MCSymbolRefExpr::Create(
169 Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
170 Alias->setVariableValue(Value);
173 // When GNU as encounters more than one .type declaration for an object it seems
174 // to use a mechanism similar to the one below to decide which type is actually
175 // used in the object file. The greater of T1 and T2 is selected based on the
176 // following ordering:
177 // STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
178 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
180 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
181 for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
182 ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
192 bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
193 MCSymbolAttr Attribute) {
194 // Indirect symbols are handled differently, to match how 'as' handles
195 // them. This makes writing matching .o files easier.
196 if (Attribute == MCSA_IndirectSymbol) {
197 // Note that we intentionally cannot use the symbol data here; this is
198 // important for matching the string table that 'as' generates.
199 IndirectSymbolData ISD;
201 ISD.SectionData = getCurrentSectionData();
202 getAssembler().getIndirectSymbols().push_back(ISD);
206 // Adding a symbol attribute always introduces the symbol, note that an
207 // important side effect of calling getOrCreateSymbolData here is to register
208 // the symbol with the assembler.
209 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
211 // The implementation of symbol attributes is designed to match 'as', but it
212 // leaves much to desired. It doesn't really make sense to arbitrarily add and
213 // remove flags, but 'as' allows this (in particular, see .desc).
215 // In the future it might be worth trying to make these operations more well
218 case MCSA_LazyReference:
220 case MCSA_SymbolResolver:
221 case MCSA_PrivateExtern:
222 case MCSA_WeakDefinition:
223 case MCSA_WeakDefAutoPrivate:
225 case MCSA_IndirectSymbol:
228 case MCSA_NoDeadStrip:
232 case MCSA_ELF_TypeGnuUniqueObject:
233 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), ELF::STT_OBJECT));
234 MCELF::SetBinding(SD, ELF::STB_GNU_UNIQUE);
235 SD.setExternal(true);
236 BindingExplicitlySet.insert(Symbol);
240 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
241 SD.setExternal(true);
242 BindingExplicitlySet.insert(Symbol);
245 case MCSA_WeakReference:
247 MCELF::SetBinding(SD, ELF::STB_WEAK);
248 SD.setExternal(true);
249 BindingExplicitlySet.insert(Symbol);
253 MCELF::SetBinding(SD, ELF::STB_LOCAL);
254 SD.setExternal(false);
255 BindingExplicitlySet.insert(Symbol);
258 case MCSA_ELF_TypeFunction:
259 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
263 case MCSA_ELF_TypeIndFunction:
264 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
265 ELF::STT_GNU_IFUNC));
268 case MCSA_ELF_TypeObject:
269 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
273 case MCSA_ELF_TypeTLS:
274 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
278 case MCSA_ELF_TypeCommon:
279 // TODO: Emit these as a common symbol.
280 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
284 case MCSA_ELF_TypeNoType:
285 MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
290 MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
294 MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
298 MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
305 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
306 unsigned ByteAlignment) {
307 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
309 if (!BindingExplicitlySet.count(Symbol)) {
310 MCELF::SetBinding(SD, ELF::STB_GLOBAL);
311 SD.setExternal(true);
314 MCELF::SetType(SD, ELF::STT_OBJECT);
316 if (MCELF::GetBinding(SD) == ELF_STB_Local) {
317 const MCSection *Section = getAssembler().getContext().getELFSection(
318 ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
320 AssignSection(Symbol, Section);
322 struct LocalCommon L = {&SD, Size, ByteAlignment};
323 LocalCommons.push_back(L);
325 SD.setCommon(Size, ByteAlignment);
328 SD.setSize(MCConstantExpr::Create(Size, getContext()));
331 void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
332 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
336 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
337 unsigned ByteAlignment) {
338 // FIXME: Should this be caught and done earlier?
339 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
340 MCELF::SetBinding(SD, ELF::STB_LOCAL);
341 SD.setExternal(false);
342 BindingExplicitlySet.insert(Symbol);
343 EmitCommonSymbol(Symbol, Size, ByteAlignment);
346 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
348 if (getCurrentSectionData()->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 (getCurrentSectionData()->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 const 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 MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
457 MCELF::SetType(SD, 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 MCSectionData *SD = getCurrentSectionData();
507 if (Assembler.getRelaxAll() && SD->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() && !SD->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 (SD->isBundleLocked() && !SD->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 (!SD->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 (SD->getBundleLockState() == MCSectionData::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 SD->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 MCSectionData *SD = getCurrentSectionData();
557 if (!SD->isBundleLocked()) {
558 mergeFragment(getOrCreateDataFragment(), DF);
564 void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
565 assert(AlignPow2 <= 30 && "Invalid bundle alignment");
566 MCAssembler &Assembler = getAssembler();
567 if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
568 Assembler.getBundleAlignSize() == 1U << AlignPow2))
569 Assembler.setBundleAlignSize(1U << AlignPow2);
571 report_fatal_error(".bundle_align_mode cannot be changed once set");
574 void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
575 MCSectionData *SD = getCurrentSectionData();
579 if (!getAssembler().isBundlingEnabled())
580 report_fatal_error(".bundle_lock forbidden when bundling is disabled");
582 if (!SD->isBundleLocked())
583 SD->setBundleGroupBeforeFirstInst(true);
585 if (getAssembler().getRelaxAll() && !SD->isBundleLocked()) {
586 // TODO: drop the lock state and set directly in the fragment
587 MCDataFragment *DF = new MCDataFragment();
588 BundleGroups.push_back(DF);
591 SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd :
592 MCSectionData::BundleLocked);
595 void MCELFStreamer::EmitBundleUnlock() {
596 MCSectionData *SD = getCurrentSectionData();
599 if (!getAssembler().isBundlingEnabled())
600 report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
601 else if (!SD->isBundleLocked())
602 report_fatal_error(".bundle_unlock without matching lock");
603 else if (SD->isBundleGroupBeforeFirstInst())
604 report_fatal_error("Empty bundle-locked group is forbidden");
606 // When the -mc-relax-all flag is used, we emit instructions to fragments
607 // stored on a stack. When the bundle unlock is emited, we pop a fragment
608 // from the stack a merge it to the one below.
609 if (getAssembler().getRelaxAll()) {
610 assert(!BundleGroups.empty() && "There are no bundle groups");
611 MCDataFragment *DF = BundleGroups.back();
613 // FIXME: Use BundleGroups to track the lock state instead.
614 SD->setBundleLockState(MCSectionData::NotBundleLocked);
616 // FIXME: Use more separate fragments for nested groups.
617 if (!SD->isBundleLocked()) {
618 mergeFragment(getOrCreateDataFragment(), DF);
619 BundleGroups.pop_back();
623 if (SD->getBundleLockState() != MCSectionData::BundleLockedAlignToEnd)
624 getOrCreateDataFragment()->setAlignToBundleEnd(false);
626 SD->setBundleLockState(MCSectionData::NotBundleLocked);
629 void MCELFStreamer::Flush() {
630 for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
631 e = LocalCommons.end();
633 MCSymbolData *SD = i->SD;
634 uint64_t Size = i->Size;
635 unsigned ByteAlignment = i->ByteAlignment;
636 const MCSymbol &Symbol = SD->getSymbol();
637 const MCSection &Section = Symbol.getSection();
639 MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
640 new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
642 MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
645 // Update the maximum alignment of the section if necessary.
646 if (ByteAlignment > SectData.getAlignment())
647 SectData.setAlignment(ByteAlignment);
650 LocalCommons.clear();
653 void MCELFStreamer::FinishImpl() {
654 // Ensure the last section gets aligned if necessary.
655 setSectionAlignmentForBundling(getAssembler(), getCurrentSectionData());
661 this->MCObjectStreamer::FinishImpl();
664 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
665 raw_pwrite_stream &OS, MCCodeEmitter *CE,
667 MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
669 S->getAssembler().setRelaxAll(true);
673 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
674 llvm_unreachable("Generic ELF doesn't support this directive");
677 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
678 llvm_unreachable("ELF doesn't support this directive");
681 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
682 llvm_unreachable("ELF doesn't support this directive");
685 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
686 llvm_unreachable("ELF doesn't support this directive");
689 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
690 llvm_unreachable("ELF doesn't support this directive");
693 void MCELFStreamer::EndCOFFSymbolDef() {
694 llvm_unreachable("ELF doesn't support this directive");
697 void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
698 uint64_t Size, unsigned ByteAlignment) {
699 llvm_unreachable("ELF doesn't support this directive");
702 void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
703 uint64_t Size, unsigned ByteAlignment) {
704 llvm_unreachable("ELF doesn't support this directive");