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