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