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