xref: /llvm-project/llvm/lib/MC/MCELFStreamer.cpp (revision ae3c85a708dcb8e9a1721d69943232af3e263099)
1 //===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file assembles .s files and emits ELF .o object files.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/MC/MCELFStreamer.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/SmallVector.h"
16 #include "llvm/BinaryFormat/ELF.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/MC/TargetRegistry.h"
33 #include "llvm/Support/Casting.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/LEB128.h"
36 #include "llvm/Support/raw_ostream.h"
37 #include <cassert>
38 #include <cstdint>
39 
40 using namespace llvm;
41 
42 MCELFStreamer::MCELFStreamer(MCContext &Context,
43                              std::unique_ptr<MCAsmBackend> TAB,
44                              std::unique_ptr<MCObjectWriter> OW,
45                              std::unique_ptr<MCCodeEmitter> Emitter)
46     : MCObjectStreamer(Context, std::move(TAB), std::move(OW),
47                        std::move(Emitter)) {}
48 
49 ELFObjectWriter &MCELFStreamer::getWriter() {
50   return static_cast<ELFObjectWriter &>(getAssembler().getWriter());
51 }
52 
53 bool MCELFStreamer::isBundleLocked() const {
54   return getCurrentSectionOnly()->isBundleLocked();
55 }
56 
57 void MCELFStreamer::initSections(bool NoExecStack, const MCSubtargetInfo &STI) {
58   MCContext &Ctx = getContext();
59   switchSection(Ctx.getObjectFileInfo()->getTextSection());
60   emitCodeAlignment(Align(Ctx.getObjectFileInfo()->getTextSectionAlignment()),
61                     &STI);
62 
63   if (NoExecStack)
64     switchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
65 }
66 
67 void MCELFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) {
68   auto *Symbol = cast<MCSymbolELF>(S);
69   MCObjectStreamer::emitLabel(Symbol, Loc);
70 
71   const MCSectionELF &Section =
72       static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
73   if (Section.getFlags() & ELF::SHF_TLS)
74     Symbol->setType(ELF::STT_TLS);
75 }
76 
77 void MCELFStreamer::emitLabelAtPos(MCSymbol *S, SMLoc Loc, MCDataFragment &F,
78                                    uint64_t Offset) {
79   auto *Symbol = cast<MCSymbolELF>(S);
80   MCObjectStreamer::emitLabelAtPos(Symbol, Loc, F, Offset);
81 
82   const MCSectionELF &Section =
83       static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
84   if (Section.getFlags() & ELF::SHF_TLS)
85     Symbol->setType(ELF::STT_TLS);
86 }
87 
88 void MCELFStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
89   // Let the target do whatever target specific stuff it needs to do.
90   getAssembler().getBackend().handleAssemblerFlag(Flag);
91   // Do any generic stuff we need to do.
92   switch (Flag) {
93   case MCAF_SyntaxUnified: return; // no-op here.
94   case MCAF_Code16: return; // Change parsing mode; no-op here.
95   case MCAF_Code32: return; // Change parsing mode; no-op here.
96   case MCAF_Code64: return; // Change parsing mode; no-op here.
97   case MCAF_SubsectionsViaSymbols:
98     getAssembler().setSubsectionsViaSymbols(true);
99     return;
100   }
101 
102   llvm_unreachable("invalid assembler flag!");
103 }
104 
105 // If bundle alignment is used and there are any instructions in the section, it
106 // needs to be aligned to at least the bundle size.
107 static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
108                                            MCSection *Section) {
109   if (Assembler.isBundlingEnabled() && Section->hasInstructions())
110     Section->ensureMinAlignment(Align(Assembler.getBundleAlignSize()));
111 }
112 
113 void MCELFStreamer::changeSection(MCSection *Section, uint32_t Subsection) {
114   MCAssembler &Asm = getAssembler();
115   if (auto *F = getCurrentFragment()) {
116     if (isBundleLocked())
117       report_fatal_error("Unterminated .bundle_lock when changing a section");
118 
119     // Ensure the previous section gets aligned if necessary.
120     setSectionAlignmentForBundling(Asm, F->getParent());
121   }
122   auto *SectionELF = static_cast<const MCSectionELF *>(Section);
123   const MCSymbol *Grp = SectionELF->getGroup();
124   if (Grp)
125     Asm.registerSymbol(*Grp);
126   if (SectionELF->getFlags() & ELF::SHF_GNU_RETAIN)
127     getWriter().markGnuAbi();
128 
129   changeSectionImpl(Section, Subsection);
130   Asm.registerSymbol(*Section->getBeginSymbol());
131 }
132 
133 void MCELFStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
134   getAssembler().registerSymbol(*Symbol);
135   const MCExpr *Value = MCSymbolRefExpr::create(
136       Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
137   Alias->setVariableValue(Value);
138 }
139 
140 // When GNU as encounters more than one .type declaration for an object it seems
141 // to use a mechanism similar to the one below to decide which type is actually
142 // used in the object file.  The greater of T1 and T2 is selected based on the
143 // following ordering:
144 //  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
145 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
146 // provided type).
147 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
148   for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
149                         ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
150     if (T1 == Type)
151       return T2;
152     if (T2 == Type)
153       return T1;
154   }
155 
156   return T2;
157 }
158 
159 bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
160   auto *Symbol = cast<MCSymbolELF>(S);
161 
162   // Adding a symbol attribute always introduces the symbol, note that an
163   // important side effect of calling registerSymbol here is to register
164   // the symbol with the assembler.
165   getAssembler().registerSymbol(*Symbol);
166 
167   // The implementation of symbol attributes is designed to match 'as', but it
168   // leaves much to desired. It doesn't really make sense to arbitrarily add and
169   // remove flags, but 'as' allows this (in particular, see .desc).
170   //
171   // In the future it might be worth trying to make these operations more well
172   // defined.
173   switch (Attribute) {
174   case MCSA_Cold:
175   case MCSA_Extern:
176   case MCSA_LazyReference:
177   case MCSA_Reference:
178   case MCSA_SymbolResolver:
179   case MCSA_PrivateExtern:
180   case MCSA_WeakDefinition:
181   case MCSA_WeakDefAutoPrivate:
182   case MCSA_Invalid:
183   case MCSA_IndirectSymbol:
184   case MCSA_Exported:
185   case MCSA_WeakAntiDep:
186     return false;
187 
188   case MCSA_NoDeadStrip:
189     // Ignore for now.
190     break;
191 
192   case MCSA_ELF_TypeGnuUniqueObject:
193     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
194     Symbol->setBinding(ELF::STB_GNU_UNIQUE);
195     getWriter().markGnuAbi();
196     break;
197 
198   case MCSA_Global:
199     // For `.weak x; .global x`, GNU as sets the binding to STB_WEAK while we
200     // traditionally set the binding to STB_GLOBAL. This is error-prone, so we
201     // error on such cases. Note, we also disallow changed binding from .local.
202     if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_GLOBAL)
203       getContext().reportError(getStartTokLoc(),
204                                Symbol->getName() +
205                                    " changed binding to STB_GLOBAL");
206     Symbol->setBinding(ELF::STB_GLOBAL);
207     break;
208 
209   case MCSA_WeakReference:
210   case MCSA_Weak:
211     // For `.global x; .weak x`, both MC and GNU as set the binding to STB_WEAK.
212     // We emit a warning for now but may switch to an error in the future.
213     if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_WEAK)
214       getContext().reportWarning(
215           getStartTokLoc(), Symbol->getName() + " changed binding to STB_WEAK");
216     Symbol->setBinding(ELF::STB_WEAK);
217     break;
218 
219   case MCSA_Local:
220     if (Symbol->isBindingSet() && Symbol->getBinding() != ELF::STB_LOCAL)
221       getContext().reportError(getStartTokLoc(),
222                                Symbol->getName() +
223                                    " changed binding to STB_LOCAL");
224     Symbol->setBinding(ELF::STB_LOCAL);
225     break;
226 
227   case MCSA_ELF_TypeFunction:
228     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
229     break;
230 
231   case MCSA_ELF_TypeIndFunction:
232     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
233     getWriter().markGnuAbi();
234     break;
235 
236   case MCSA_ELF_TypeObject:
237     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
238     break;
239 
240   case MCSA_ELF_TypeTLS:
241     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
242     break;
243 
244   case MCSA_ELF_TypeCommon:
245     // TODO: Emit these as a common symbol.
246     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
247     break;
248 
249   case MCSA_ELF_TypeNoType:
250     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
251     break;
252 
253   case MCSA_Protected:
254     Symbol->setVisibility(ELF::STV_PROTECTED);
255     break;
256 
257   case MCSA_Memtag:
258     Symbol->setMemtag(true);
259     break;
260 
261   case MCSA_Hidden:
262     Symbol->setVisibility(ELF::STV_HIDDEN);
263     break;
264 
265   case MCSA_Internal:
266     Symbol->setVisibility(ELF::STV_INTERNAL);
267     break;
268 
269   case MCSA_AltEntry:
270     llvm_unreachable("ELF doesn't support the .alt_entry attribute");
271 
272   case MCSA_LGlobal:
273     llvm_unreachable("ELF doesn't support the .lglobl attribute");
274   }
275 
276   return true;
277 }
278 
279 void MCELFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
280                                      Align ByteAlignment) {
281   auto *Symbol = cast<MCSymbolELF>(S);
282   getAssembler().registerSymbol(*Symbol);
283 
284   if (!Symbol->isBindingSet())
285     Symbol->setBinding(ELF::STB_GLOBAL);
286 
287   Symbol->setType(ELF::STT_OBJECT);
288 
289   if (Symbol->getBinding() == ELF::STB_LOCAL) {
290     MCSection &Section = *getAssembler().getContext().getELFSection(
291         ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
292     MCSectionSubPair P = getCurrentSection();
293     switchSection(&Section);
294 
295     emitValueToAlignment(ByteAlignment, 0, 1, 0);
296     emitLabel(Symbol);
297     emitZeros(Size);
298 
299     switchSection(P.first, P.second);
300   } else {
301     if (Symbol->declareCommon(Size, ByteAlignment))
302       report_fatal_error(Twine("Symbol: ") + Symbol->getName() +
303                          " redeclared as different type");
304   }
305 
306   cast<MCSymbolELF>(Symbol)
307       ->setSize(MCConstantExpr::create(Size, getContext()));
308 }
309 
310 void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
311   cast<MCSymbolELF>(Symbol)->setSize(Value);
312 }
313 
314 void MCELFStreamer::emitELFSymverDirective(const MCSymbol *OriginalSym,
315                                            StringRef Name,
316                                            bool KeepOriginalSym) {
317   getWriter().Symvers.push_back(ELFObjectWriter::Symver{
318       getStartTokLoc(), OriginalSym, Name, KeepOriginalSym});
319 }
320 
321 void MCELFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
322                                           Align ByteAlignment) {
323   auto *Symbol = cast<MCSymbolELF>(S);
324   // FIXME: Should this be caught and done earlier?
325   getAssembler().registerSymbol(*Symbol);
326   Symbol->setBinding(ELF::STB_LOCAL);
327   emitCommonSymbol(Symbol, Size, ByteAlignment);
328 }
329 
330 void MCELFStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
331                                   SMLoc Loc) {
332   if (isBundleLocked())
333     report_fatal_error("Emitting values inside a locked bundle is forbidden");
334   fixSymbolsInTLSFixups(Value);
335   MCObjectStreamer::emitValueImpl(Value, Size, Loc);
336 }
337 
338 void MCELFStreamer::emitValueToAlignment(Align Alignment, int64_t Value,
339                                          unsigned ValueSize,
340                                          unsigned MaxBytesToEmit) {
341   if (isBundleLocked())
342     report_fatal_error("Emitting values inside a locked bundle is forbidden");
343   MCObjectStreamer::emitValueToAlignment(Alignment, Value, ValueSize,
344                                          MaxBytesToEmit);
345 }
346 
347 void MCELFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
348                                        const MCSymbolRefExpr *To,
349                                        uint64_t Count) {
350   getWriter().getCGProfile().push_back({From, To, Count});
351 }
352 
353 void MCELFStreamer::emitIdent(StringRef IdentString) {
354   MCSection *Comment = getAssembler().getContext().getELFSection(
355       ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1);
356   pushSection();
357   switchSection(Comment);
358   if (!SeenIdent) {
359     emitInt8(0);
360     SeenIdent = true;
361   }
362   emitBytes(IdentString);
363   emitInt8(0);
364   popSection();
365 }
366 
367 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
368   switch (expr->getKind()) {
369   case MCExpr::Target:
370     cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
371     break;
372   case MCExpr::Constant:
373     break;
374 
375   case MCExpr::Binary: {
376     const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
377     fixSymbolsInTLSFixups(be->getLHS());
378     fixSymbolsInTLSFixups(be->getRHS());
379     break;
380   }
381 
382   case MCExpr::SymbolRef: {
383     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
384     switch (symRef.getKind()) {
385     default:
386       return;
387     case MCSymbolRefExpr::VK_GOTTPOFF:
388     case MCSymbolRefExpr::VK_INDNTPOFF:
389     case MCSymbolRefExpr::VK_NTPOFF:
390     case MCSymbolRefExpr::VK_GOTNTPOFF:
391     case MCSymbolRefExpr::VK_TLSCALL:
392     case MCSymbolRefExpr::VK_TLSDESC:
393     case MCSymbolRefExpr::VK_TLSGD:
394     case MCSymbolRefExpr::VK_TLSLD:
395     case MCSymbolRefExpr::VK_TLSLDM:
396     case MCSymbolRefExpr::VK_TPOFF:
397     case MCSymbolRefExpr::VK_TPREL:
398     case MCSymbolRefExpr::VK_DTPOFF:
399     case MCSymbolRefExpr::VK_DTPREL:
400     case MCSymbolRefExpr::VK_PPC_DTPMOD:
401     case MCSymbolRefExpr::VK_PPC_TPREL_LO:
402     case MCSymbolRefExpr::VK_PPC_TPREL_HI:
403     case MCSymbolRefExpr::VK_PPC_TPREL_HA:
404     case MCSymbolRefExpr::VK_PPC_TPREL_HIGH:
405     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA:
406     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
407     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
408     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
409     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
410     case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
411     case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
412     case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
413     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH:
414     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA:
415     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
416     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
417     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
418     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
419     case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
420     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
421     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
422     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
423     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_PCREL:
424     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
425     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
426     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
427     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
428     case MCSymbolRefExpr::VK_PPC_TLS:
429     case MCSymbolRefExpr::VK_PPC_TLS_PCREL:
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_GOT_TLSGD_PCREL:
435     case MCSymbolRefExpr::VK_PPC_TLSGD:
436     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
437     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
438     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
439     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
440     case MCSymbolRefExpr::VK_PPC_TLSLD:
441       break;
442     }
443     getAssembler().registerSymbol(symRef.getSymbol());
444     cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
445     break;
446   }
447 
448   case MCExpr::Unary:
449     fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
450     break;
451   }
452 }
453 
454 void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE,
455                                            uint64_t Offset) {
456   const MCSymbol *S = &SRE->getSymbol();
457   if (S->isTemporary()) {
458     if (!S->isInSection()) {
459       getContext().reportError(
460           SRE->getLoc(), Twine("Reference to undefined temporary symbol ") +
461                              "`" + S->getName() + "`");
462       return;
463     }
464     S = S->getSection().getBeginSymbol();
465     S->setUsedInReloc();
466     SRE = MCSymbolRefExpr::create(S, MCSymbolRefExpr::VK_None, getContext(),
467                                   SRE->getLoc());
468   }
469   const MCConstantExpr *MCOffset = MCConstantExpr::create(Offset, getContext());
470   if (std::optional<std::pair<bool, std::string>> Err =
471           MCObjectStreamer::emitRelocDirective(
472               *MCOffset, "BFD_RELOC_NONE", SRE, SRE->getLoc(),
473               *getContext().getSubtargetInfo()))
474     report_fatal_error("Relocation for CG Profile could not be created: " +
475                        Twine(Err->second));
476 }
477 
478 void MCELFStreamer::finalizeCGProfile() {
479   ELFObjectWriter &W = getWriter();
480   if (W.getCGProfile().empty())
481     return;
482   MCSection *CGProfile = getAssembler().getContext().getELFSection(
483       ".llvm.call-graph-profile", ELF::SHT_LLVM_CALL_GRAPH_PROFILE,
484       ELF::SHF_EXCLUDE, /*sizeof(Elf_CGProfile_Impl<>)=*/8);
485   pushSection();
486   switchSection(CGProfile);
487   uint64_t Offset = 0;
488   for (auto &E : W.getCGProfile()) {
489     finalizeCGProfileEntry(E.From, Offset);
490     finalizeCGProfileEntry(E.To, Offset);
491     emitIntValue(E.Count, sizeof(uint64_t));
492     Offset += sizeof(uint64_t);
493   }
494   popSection();
495 }
496 
497 void MCELFStreamer::emitInstToFragment(const MCInst &Inst,
498                                        const MCSubtargetInfo &STI) {
499   this->MCObjectStreamer::emitInstToFragment(Inst, STI);
500   MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
501 
502   for (auto &Fixup : F.getFixups())
503     fixSymbolsInTLSFixups(Fixup.getValue());
504 }
505 
506 // A fragment can only have one Subtarget, and when bundling is enabled we
507 // sometimes need to use the same fragment. We give an error if there
508 // are conflicting Subtargets.
509 static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
510                                   const MCSubtargetInfo *NewSTI) {
511   if (OldSTI && NewSTI && OldSTI != NewSTI)
512     report_fatal_error("A Bundle can only have one Subtarget.");
513 }
514 
515 void MCELFStreamer::emitInstToData(const MCInst &Inst,
516                                    const MCSubtargetInfo &STI) {
517   MCAssembler &Assembler = getAssembler();
518 
519   // There are several possibilities here:
520   //
521   // If bundling is disabled, append the encoded instruction to the current data
522   // fragment (or create a new such fragment if the current fragment is not a
523   // data fragment, or the Subtarget has changed).
524   //
525   // If bundling is enabled:
526   // - If we're not in a bundle-locked group, emit the instruction into a
527   //   fragment of its own.
528   // - If we're in a bundle-locked group, append the instruction to the current
529   //   data fragment because we want all the instructions in a group to get into
530   //   the same fragment. Be careful not to do that for the first instruction in
531   //   the group, though.
532   MCDataFragment *DF;
533 
534   if (Assembler.isBundlingEnabled()) {
535     MCSection &Sec = *getCurrentSectionOnly();
536     if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) {
537       // If we are bundle-locked, we re-use the current fragment.
538       // The bundle-locking directive ensures this is a new data fragment.
539       DF = cast<MCDataFragment>(getCurrentFragment());
540       CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
541     } else {
542       DF = getContext().allocFragment<MCDataFragment>();
543       insert(DF);
544     }
545     if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
546       // If this fragment is for a group marked "align_to_end", set a flag
547       // in the fragment. This can happen after the fragment has already been
548       // created if there are nested bundle_align groups and an inner one
549       // is the one marked align_to_end.
550       DF->setAlignToBundleEnd(true);
551     }
552 
553     // We're now emitting an instruction in a bundle group, so this flag has
554     // to be turned off.
555     Sec.setBundleGroupBeforeFirstInst(false);
556   } else {
557     DF = getOrCreateDataFragment(&STI);
558   }
559 
560   // Emit instruction directly into data fragment.
561   size_t FixupStartIndex = DF->getFixups().size();
562   size_t CodeOffset = DF->getContents().size();
563   Assembler.getEmitter().encodeInstruction(Inst, DF->getContents(),
564                                            DF->getFixups(), STI);
565 
566   auto Fixups = MutableArrayRef(DF->getFixups()).slice(FixupStartIndex);
567   for (auto &Fixup : Fixups) {
568     Fixup.setOffset(Fixup.getOffset() + CodeOffset);
569     fixSymbolsInTLSFixups(Fixup.getValue());
570   }
571 
572   DF->setHasInstructions(STI);
573   if (!Fixups.empty() && Fixups.back().getTargetKind() ==
574                              getAssembler().getBackend().RelaxFixupKind)
575     DF->setLinkerRelaxable();
576 }
577 
578 void MCELFStreamer::emitBundleAlignMode(Align Alignment) {
579   assert(Log2(Alignment) <= 30 && "Invalid bundle alignment");
580   MCAssembler &Assembler = getAssembler();
581   if (Alignment > 1 && (Assembler.getBundleAlignSize() == 0 ||
582                         Assembler.getBundleAlignSize() == Alignment.value()))
583     Assembler.setBundleAlignSize(Alignment.value());
584   else
585     report_fatal_error(".bundle_align_mode cannot be changed once set");
586 }
587 
588 void MCELFStreamer::emitBundleLock(bool AlignToEnd) {
589   MCSection &Sec = *getCurrentSectionOnly();
590 
591   if (!getAssembler().isBundlingEnabled())
592     report_fatal_error(".bundle_lock forbidden when bundling is disabled");
593 
594   if (!isBundleLocked())
595     Sec.setBundleGroupBeforeFirstInst(true);
596 
597   Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
598                                     : MCSection::BundleLocked);
599 }
600 
601 void MCELFStreamer::emitBundleUnlock() {
602   MCSection &Sec = *getCurrentSectionOnly();
603 
604   if (!getAssembler().isBundlingEnabled())
605     report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
606   else if (!isBundleLocked())
607     report_fatal_error(".bundle_unlock without matching lock");
608   else if (Sec.isBundleGroupBeforeFirstInst())
609     report_fatal_error("Empty bundle-locked group is forbidden");
610 
611   Sec.setBundleLockState(MCSection::NotBundleLocked);
612 }
613 
614 void MCELFStreamer::finishImpl() {
615   // Emit the .gnu attributes section if any attributes have been added.
616   if (!GNUAttributes.empty()) {
617     MCSection *DummyAttributeSection = nullptr;
618     createAttributesSection("gnu", ".gnu.attributes", ELF::SHT_GNU_ATTRIBUTES,
619                             DummyAttributeSection, GNUAttributes);
620   }
621 
622   // Ensure the last section gets aligned if necessary.
623   if (MCFragment *F = getCurrentFragment())
624     setSectionAlignmentForBundling(getAssembler(), F->getParent());
625 
626   finalizeCGProfile();
627   emitFrames(nullptr);
628 
629   this->MCObjectStreamer::finishImpl();
630 }
631 
632 void MCELFStreamer::emitThumbFunc(MCSymbol *Func) {
633   llvm_unreachable("Generic ELF doesn't support this directive");
634 }
635 
636 void MCELFStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
637   llvm_unreachable("ELF doesn't support this directive");
638 }
639 
640 void MCELFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
641                                  uint64_t Size, Align ByteAlignment,
642                                  SMLoc Loc) {
643   llvm_unreachable("ELF doesn't support this directive");
644 }
645 
646 void MCELFStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
647                                    uint64_t Size, Align ByteAlignment) {
648   llvm_unreachable("ELF doesn't support this directive");
649 }
650 
651 void MCELFStreamer::setAttributeItem(unsigned Attribute, unsigned Value,
652                                      bool OverwriteExisting) {
653   // Look for existing attribute item
654   if (AttributeItem *Item = getAttributeItem(Attribute)) {
655     if (!OverwriteExisting)
656       return;
657     Item->Type = AttributeItem::NumericAttribute;
658     Item->IntValue = Value;
659     return;
660   }
661 
662   // Create new attribute item
663   AttributeItem Item = {AttributeItem::NumericAttribute, Attribute, Value,
664                         std::string(StringRef(""))};
665   Contents.push_back(Item);
666 }
667 
668 void MCELFStreamer::setAttributeItem(unsigned Attribute, StringRef Value,
669                                      bool OverwriteExisting) {
670   // Look for existing attribute item
671   if (AttributeItem *Item = getAttributeItem(Attribute)) {
672     if (!OverwriteExisting)
673       return;
674     Item->Type = AttributeItem::TextAttribute;
675     Item->StringValue = std::string(Value);
676     return;
677   }
678 
679   // Create new attribute item
680   AttributeItem Item = {AttributeItem::TextAttribute, Attribute, 0,
681                         std::string(Value)};
682   Contents.push_back(Item);
683 }
684 
685 void MCELFStreamer::setAttributeItems(unsigned Attribute, unsigned IntValue,
686                                       StringRef StringValue,
687                                       bool OverwriteExisting) {
688   // Look for existing attribute item
689   if (AttributeItem *Item = getAttributeItem(Attribute)) {
690     if (!OverwriteExisting)
691       return;
692     Item->Type = AttributeItem::NumericAndTextAttributes;
693     Item->IntValue = IntValue;
694     Item->StringValue = std::string(StringValue);
695     return;
696   }
697 
698   // Create new attribute item
699   AttributeItem Item = {AttributeItem::NumericAndTextAttributes, Attribute,
700                         IntValue, std::string(StringValue)};
701   Contents.push_back(Item);
702 }
703 
704 MCELFStreamer::AttributeItem *
705 MCELFStreamer::getAttributeItem(unsigned Attribute) {
706   for (AttributeItem &Item : Contents)
707     if (Item.Tag == Attribute)
708       return &Item;
709   return nullptr;
710 }
711 
712 size_t
713 MCELFStreamer::calculateContentSize(SmallVector<AttributeItem, 64> &AttrsVec) {
714   size_t Result = 0;
715   for (const AttributeItem &Item : AttrsVec) {
716     switch (Item.Type) {
717     case AttributeItem::HiddenAttribute:
718       break;
719     case AttributeItem::NumericAttribute:
720       Result += getULEB128Size(Item.Tag);
721       Result += getULEB128Size(Item.IntValue);
722       break;
723     case AttributeItem::TextAttribute:
724       Result += getULEB128Size(Item.Tag);
725       Result += Item.StringValue.size() + 1; // string + '\0'
726       break;
727     case AttributeItem::NumericAndTextAttributes:
728       Result += getULEB128Size(Item.Tag);
729       Result += getULEB128Size(Item.IntValue);
730       Result += Item.StringValue.size() + 1; // string + '\0';
731       break;
732     }
733   }
734   return Result;
735 }
736 
737 void MCELFStreamer::createAttributesSection(
738     StringRef Vendor, const Twine &Section, unsigned Type,
739     MCSection *&AttributeSection, SmallVector<AttributeItem, 64> &AttrsVec) {
740   // <format-version>
741   // [ <section-length> "vendor-name"
742   // [ <file-tag> <size> <attribute>*
743   //   | <section-tag> <size> <section-number>* 0 <attribute>*
744   //   | <symbol-tag> <size> <symbol-number>* 0 <attribute>*
745   //   ]+
746   // ]*
747 
748   // Switch section to AttributeSection or get/create the section.
749   if (AttributeSection) {
750     switchSection(AttributeSection);
751   } else {
752     AttributeSection = getContext().getELFSection(Section, Type, 0);
753     switchSection(AttributeSection);
754 
755     // Format version
756     emitInt8(0x41);
757   }
758 
759   // Vendor size + Vendor name + '\0'
760   const size_t VendorHeaderSize = 4 + Vendor.size() + 1;
761 
762   // Tag + Tag Size
763   const size_t TagHeaderSize = 1 + 4;
764 
765   const size_t ContentsSize = calculateContentSize(AttrsVec);
766 
767   emitInt32(VendorHeaderSize + TagHeaderSize + ContentsSize);
768   emitBytes(Vendor);
769   emitInt8(0); // '\0'
770 
771   emitInt8(ARMBuildAttrs::File);
772   emitInt32(TagHeaderSize + ContentsSize);
773 
774   // Size should have been accounted for already, now
775   // emit each field as its type (ULEB or String)
776   for (const AttributeItem &Item : AttrsVec) {
777     emitULEB128IntValue(Item.Tag);
778     switch (Item.Type) {
779     default:
780       llvm_unreachable("Invalid attribute type");
781     case AttributeItem::NumericAttribute:
782       emitULEB128IntValue(Item.IntValue);
783       break;
784     case AttributeItem::TextAttribute:
785       emitBytes(Item.StringValue);
786       emitInt8(0); // '\0'
787       break;
788     case AttributeItem::NumericAndTextAttributes:
789       emitULEB128IntValue(Item.IntValue);
790       emitBytes(Item.StringValue);
791       emitInt8(0); // '\0'
792       break;
793     }
794   }
795 
796   AttrsVec.clear();
797 }
798 
799 MCStreamer *llvm::createELFStreamer(MCContext &Context,
800                                     std::unique_ptr<MCAsmBackend> &&MAB,
801                                     std::unique_ptr<MCObjectWriter> &&OW,
802                                     std::unique_ptr<MCCodeEmitter> &&CE) {
803   MCELFStreamer *S =
804       new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
805   return S;
806 }
807