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