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