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/TargetRegistry.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include <cassert>
37 #include <cstdint>
38 
39 using namespace llvm;
40 
MCELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> TAB,std::unique_ptr<MCObjectWriter> OW,std::unique_ptr<MCCodeEmitter> Emitter)41 MCELFStreamer::MCELFStreamer(MCContext &Context,
42                              std::unique_ptr<MCAsmBackend> TAB,
43                              std::unique_ptr<MCObjectWriter> OW,
44                              std::unique_ptr<MCCodeEmitter> Emitter)
45     : MCObjectStreamer(Context, std::move(TAB), std::move(OW),
46                        std::move(Emitter)) {}
47 
isBundleLocked() const48 bool MCELFStreamer::isBundleLocked() const {
49   return getCurrentSectionOnly()->isBundleLocked();
50 }
51 
mergeFragment(MCDataFragment * DF,MCDataFragment * EF)52 void MCELFStreamer::mergeFragment(MCDataFragment *DF,
53                                   MCDataFragment *EF) {
54   MCAssembler &Assembler = getAssembler();
55 
56   if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
57     uint64_t FSize = EF->getContents().size();
58 
59     if (FSize > Assembler.getBundleAlignSize())
60       report_fatal_error("Fragment can't be larger than a bundle size");
61 
62     uint64_t RequiredBundlePadding = computeBundlePadding(
63         Assembler, EF, DF->getContents().size(), FSize);
64 
65     if (RequiredBundlePadding > UINT8_MAX)
66       report_fatal_error("Padding cannot exceed 255 bytes");
67 
68     if (RequiredBundlePadding > 0) {
69       SmallString<256> Code;
70       raw_svector_ostream VecOS(Code);
71       EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
72       Assembler.writeFragmentPadding(VecOS, *EF, FSize);
73 
74       DF->getContents().append(Code.begin(), Code.end());
75     }
76   }
77 
78   flushPendingLabels(DF, DF->getContents().size());
79 
80   for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
81     EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
82                                  DF->getContents().size());
83     DF->getFixups().push_back(EF->getFixups()[i]);
84   }
85   if (DF->getSubtargetInfo() == nullptr && EF->getSubtargetInfo())
86     DF->setHasInstructions(*EF->getSubtargetInfo());
87   DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
88 }
89 
InitSections(bool NoExecStack)90 void MCELFStreamer::InitSections(bool NoExecStack) {
91   MCContext &Ctx = getContext();
92   SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
93   emitCodeAlignment(4);
94 
95   if (NoExecStack)
96     SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
97 }
98 
emitLabel(MCSymbol * S,SMLoc Loc)99 void MCELFStreamer::emitLabel(MCSymbol *S, SMLoc Loc) {
100   auto *Symbol = cast<MCSymbolELF>(S);
101   MCObjectStreamer::emitLabel(Symbol, Loc);
102 
103   const MCSectionELF &Section =
104       static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
105   if (Section.getFlags() & ELF::SHF_TLS)
106     Symbol->setType(ELF::STT_TLS);
107 }
108 
emitLabelAtPos(MCSymbol * S,SMLoc Loc,MCFragment * F,uint64_t Offset)109 void MCELFStreamer::emitLabelAtPos(MCSymbol *S, SMLoc Loc, MCFragment *F,
110                                    uint64_t Offset) {
111   auto *Symbol = cast<MCSymbolELF>(S);
112   MCObjectStreamer::emitLabelAtPos(Symbol, Loc, F, Offset);
113 
114   const MCSectionELF &Section =
115       static_cast<const MCSectionELF &>(*getCurrentSectionOnly());
116   if (Section.getFlags() & ELF::SHF_TLS)
117     Symbol->setType(ELF::STT_TLS);
118 }
119 
emitAssemblerFlag(MCAssemblerFlag Flag)120 void MCELFStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
121   // Let the target do whatever target specific stuff it needs to do.
122   getAssembler().getBackend().handleAssemblerFlag(Flag);
123   // Do any generic stuff we need to do.
124   switch (Flag) {
125   case MCAF_SyntaxUnified: return; // no-op here.
126   case MCAF_Code16: return; // Change parsing mode; no-op here.
127   case MCAF_Code32: return; // Change parsing mode; no-op here.
128   case MCAF_Code64: return; // Change parsing mode; no-op here.
129   case MCAF_SubsectionsViaSymbols:
130     getAssembler().setSubsectionsViaSymbols(true);
131     return;
132   }
133 
134   llvm_unreachable("invalid assembler flag!");
135 }
136 
137 // If bundle alignment is used and there are any instructions in the section, it
138 // needs to be aligned to at least the bundle size.
setSectionAlignmentForBundling(const MCAssembler & Assembler,MCSection * Section)139 static void setSectionAlignmentForBundling(const MCAssembler &Assembler,
140                                            MCSection *Section) {
141   if (Section && Assembler.isBundlingEnabled() && Section->hasInstructions() &&
142       Section->getAlignment() < Assembler.getBundleAlignSize())
143     Section->setAlignment(Align(Assembler.getBundleAlignSize()));
144 }
145 
changeSection(MCSection * Section,const MCExpr * Subsection)146 void MCELFStreamer::changeSection(MCSection *Section,
147                                   const MCExpr *Subsection) {
148   MCSection *CurSection = getCurrentSectionOnly();
149   if (CurSection && isBundleLocked())
150     report_fatal_error("Unterminated .bundle_lock when changing a section");
151 
152   MCAssembler &Asm = getAssembler();
153   // Ensure the previous section gets aligned if necessary.
154   setSectionAlignmentForBundling(Asm, CurSection);
155   auto *SectionELF = static_cast<const MCSectionELF *>(Section);
156   const MCSymbol *Grp = SectionELF->getGroup();
157   if (Grp)
158     Asm.registerSymbol(*Grp);
159 
160   changeSectionImpl(Section, Subsection);
161   Asm.registerSymbol(*Section->getBeginSymbol());
162 }
163 
emitWeakReference(MCSymbol * Alias,const MCSymbol * Symbol)164 void MCELFStreamer::emitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
165   getAssembler().registerSymbol(*Symbol);
166   const MCExpr *Value = MCSymbolRefExpr::create(
167       Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
168   Alias->setVariableValue(Value);
169 }
170 
171 // When GNU as encounters more than one .type declaration for an object it seems
172 // to use a mechanism similar to the one below to decide which type is actually
173 // used in the object file.  The greater of T1 and T2 is selected based on the
174 // following ordering:
175 //  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
176 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
177 // provided type).
CombineSymbolTypes(unsigned T1,unsigned T2)178 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
179   for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
180                         ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
181     if (T1 == Type)
182       return T2;
183     if (T2 == Type)
184       return T1;
185   }
186 
187   return T2;
188 }
189 
emitSymbolAttribute(MCSymbol * S,MCSymbolAttr Attribute)190 bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
191   auto *Symbol = cast<MCSymbolELF>(S);
192 
193   // Adding a symbol attribute always introduces the symbol, note that an
194   // important side effect of calling registerSymbol here is to register
195   // the symbol with the assembler.
196   getAssembler().registerSymbol(*Symbol);
197 
198   // The implementation of symbol attributes is designed to match 'as', but it
199   // leaves much to desired. It doesn't really make sense to arbitrarily add and
200   // remove flags, but 'as' allows this (in particular, see .desc).
201   //
202   // In the future it might be worth trying to make these operations more well
203   // defined.
204   switch (Attribute) {
205   case MCSA_Cold:
206   case MCSA_Extern:
207   case MCSA_LazyReference:
208   case MCSA_Reference:
209   case MCSA_SymbolResolver:
210   case MCSA_PrivateExtern:
211   case MCSA_WeakDefinition:
212   case MCSA_WeakDefAutoPrivate:
213   case MCSA_Invalid:
214   case MCSA_IndirectSymbol:
215     return false;
216 
217   case MCSA_NoDeadStrip:
218     // Ignore for now.
219     break;
220 
221   case MCSA_ELF_TypeGnuUniqueObject:
222     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
223     Symbol->setBinding(ELF::STB_GNU_UNIQUE);
224     Symbol->setExternal(true);
225     break;
226 
227   case MCSA_Global:
228     Symbol->setBinding(ELF::STB_GLOBAL);
229     Symbol->setExternal(true);
230     break;
231 
232   case MCSA_WeakReference:
233   case MCSA_Weak:
234     Symbol->setBinding(ELF::STB_WEAK);
235     Symbol->setExternal(true);
236     break;
237 
238   case MCSA_Local:
239     Symbol->setBinding(ELF::STB_LOCAL);
240     Symbol->setExternal(false);
241     break;
242 
243   case MCSA_ELF_TypeFunction:
244     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_FUNC));
245     break;
246 
247   case MCSA_ELF_TypeIndFunction:
248     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_GNU_IFUNC));
249     break;
250 
251   case MCSA_ELF_TypeObject:
252     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
253     break;
254 
255   case MCSA_ELF_TypeTLS:
256     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_TLS));
257     break;
258 
259   case MCSA_ELF_TypeCommon:
260     // TODO: Emit these as a common symbol.
261     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_OBJECT));
262     break;
263 
264   case MCSA_ELF_TypeNoType:
265     Symbol->setType(CombineSymbolTypes(Symbol->getType(), ELF::STT_NOTYPE));
266     break;
267 
268   case MCSA_Protected:
269     Symbol->setVisibility(ELF::STV_PROTECTED);
270     break;
271 
272   case MCSA_Hidden:
273     Symbol->setVisibility(ELF::STV_HIDDEN);
274     break;
275 
276   case MCSA_Internal:
277     Symbol->setVisibility(ELF::STV_INTERNAL);
278     break;
279 
280   case MCSA_AltEntry:
281     llvm_unreachable("ELF doesn't support the .alt_entry attribute");
282 
283   case MCSA_LGlobal:
284     llvm_unreachable("ELF doesn't support the .lglobl attribute");
285   }
286 
287   return true;
288 }
289 
emitCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment,TailPaddingAmount TailPadding)290 void MCELFStreamer::emitCommonSymbol(MCSymbol *S, uint64_t Size,
291                                      unsigned ByteAlignment,
292                                      TailPaddingAmount TailPadding) {
293   auto *Symbol = cast<MCSymbolELF>(S);
294   getAssembler().registerSymbol(*Symbol);
295 
296   if (!Symbol->isBindingSet()) {
297     Symbol->setBinding(ELF::STB_GLOBAL);
298     Symbol->setExternal(true);
299   }
300 
301   Symbol->setType(ELF::STT_OBJECT);
302 
303   if (Symbol->getBinding() == ELF::STB_LOCAL) {
304     MCSection &Section = *getAssembler().getContext().getELFSection(
305         ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
306     MCSectionSubPair P = getCurrentSection();
307     SwitchSection(&Section);
308 
309     emitValueToAlignment(ByteAlignment, 0, 1, 0);
310     emitLabel(Symbol);
311     emitZeros(Size + static_cast<uint64_t>(TailPadding));
312 
313     SwitchSection(P.first, P.second);
314   } else {
315     if (Symbol->declareCommon(Size + static_cast<uint64_t>(TailPadding),
316                               ByteAlignment))
317       report_fatal_error("Symbol: " + Symbol->getName() +
318                          " redeclared as different type");
319   }
320 
321   cast<MCSymbolELF>(Symbol)
322       ->setSize(MCConstantExpr::create(Size, getContext()));
323 }
324 
emitELFSize(MCSymbol * Symbol,const MCExpr * Value)325 void MCELFStreamer::emitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
326   cast<MCSymbolELF>(Symbol)->setSize(Value);
327 }
328 
emitELFSymverDirective(StringRef AliasName,const MCSymbol * Aliasee)329 void MCELFStreamer::emitELFSymverDirective(StringRef AliasName,
330                                            const MCSymbol *Aliasee) {
331   getAssembler().Symvers.push_back({AliasName, Aliasee});
332 }
333 
emitLocalCommonSymbol(MCSymbol * S,uint64_t Size,unsigned ByteAlignment,TailPaddingAmount TailPadding)334 void MCELFStreamer::emitLocalCommonSymbol(MCSymbol *S, uint64_t Size,
335                                           unsigned ByteAlignment,
336                                           TailPaddingAmount TailPadding) {
337   auto *Symbol = cast<MCSymbolELF>(S);
338   // FIXME: Should this be caught and done earlier?
339   getAssembler().registerSymbol(*Symbol);
340   Symbol->setBinding(ELF::STB_LOCAL);
341   Symbol->setExternal(false);
342   emitCommonSymbol(Symbol, Size, ByteAlignment, TailPadding);
343 }
344 
emitValueImpl(const MCExpr * Value,unsigned Size,SMLoc Loc)345 void MCELFStreamer::emitValueImpl(const MCExpr *Value, unsigned Size,
346                                   SMLoc Loc) {
347   if (isBundleLocked())
348     report_fatal_error("Emitting values inside a locked bundle is forbidden");
349   fixSymbolsInTLSFixups(Value);
350   MCObjectStreamer::emitValueImpl(Value, Size, Loc);
351 }
352 
emitValueToAlignment(unsigned ByteAlignment,int64_t Value,unsigned ValueSize,unsigned MaxBytesToEmit)353 void MCELFStreamer::emitValueToAlignment(unsigned ByteAlignment,
354                                          int64_t Value,
355                                          unsigned ValueSize,
356                                          unsigned MaxBytesToEmit) {
357   if (isBundleLocked())
358     report_fatal_error("Emitting values inside a locked bundle is forbidden");
359   MCObjectStreamer::emitValueToAlignment(ByteAlignment, Value,
360                                          ValueSize, MaxBytesToEmit);
361 }
362 
emitCGProfileEntry(const MCSymbolRefExpr * From,const MCSymbolRefExpr * To,uint64_t Count)363 void MCELFStreamer::emitCGProfileEntry(const MCSymbolRefExpr *From,
364                                        const MCSymbolRefExpr *To,
365                                        uint64_t Count) {
366   getAssembler().CGProfile.push_back({From, To, Count});
367 }
368 
emitIdent(StringRef IdentString)369 void MCELFStreamer::emitIdent(StringRef IdentString) {
370   MCSection *Comment = getAssembler().getContext().getELFSection(
371       ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
372   PushSection();
373   SwitchSection(Comment);
374   if (!SeenIdent) {
375     emitInt8(0);
376     SeenIdent = true;
377   }
378   emitBytes(IdentString);
379   emitInt8(0);
380   PopSection();
381 }
382 
fixSymbolsInTLSFixups(const MCExpr * expr)383 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
384   switch (expr->getKind()) {
385   case MCExpr::Target:
386     cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
387     break;
388   case MCExpr::Constant:
389     break;
390 
391   case MCExpr::Binary: {
392     const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
393     fixSymbolsInTLSFixups(be->getLHS());
394     fixSymbolsInTLSFixups(be->getRHS());
395     break;
396   }
397 
398   case MCExpr::SymbolRef: {
399     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
400     switch (symRef.getKind()) {
401     default:
402       return;
403     case MCSymbolRefExpr::VK_GOTTPOFF:
404     case MCSymbolRefExpr::VK_INDNTPOFF:
405     case MCSymbolRefExpr::VK_NTPOFF:
406     case MCSymbolRefExpr::VK_GOTNTPOFF:
407     case MCSymbolRefExpr::VK_TLSCALL:
408     case MCSymbolRefExpr::VK_TLSDESC:
409     case MCSymbolRefExpr::VK_TLSGD:
410     case MCSymbolRefExpr::VK_TLSLD:
411     case MCSymbolRefExpr::VK_TLSLDM:
412     case MCSymbolRefExpr::VK_TPOFF:
413     case MCSymbolRefExpr::VK_TPREL:
414     case MCSymbolRefExpr::VK_DTPOFF:
415     case MCSymbolRefExpr::VK_DTPREL:
416     case MCSymbolRefExpr::VK_PPC_DTPMOD:
417     case MCSymbolRefExpr::VK_PPC_TPREL_LO:
418     case MCSymbolRefExpr::VK_PPC_TPREL_HI:
419     case MCSymbolRefExpr::VK_PPC_TPREL_HA:
420     case MCSymbolRefExpr::VK_PPC_TPREL_HIGH:
421     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHA:
422     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
423     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
424     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
425     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
426     case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
427     case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
428     case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
429     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGH:
430     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHA:
431     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
432     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
433     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
434     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
435     case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
436     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
437     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
438     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
439     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
440     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
441     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
442     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
443     case MCSymbolRefExpr::VK_PPC_TLS:
444     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
445     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
446     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
447     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
448     case MCSymbolRefExpr::VK_PPC_TLSGD:
449     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
450     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
451     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
452     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
453     case MCSymbolRefExpr::VK_PPC_TLSLD:
454       break;
455     }
456     getAssembler().registerSymbol(symRef.getSymbol());
457     cast<MCSymbolELF>(symRef.getSymbol()).setType(ELF::STT_TLS);
458     break;
459   }
460 
461   case MCExpr::Unary:
462     fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
463     break;
464   }
465 }
466 
finalizeCGProfileEntry(const MCSymbolRefExpr * & SRE)467 void MCELFStreamer::finalizeCGProfileEntry(const MCSymbolRefExpr *&SRE) {
468   const MCSymbol *S = &SRE->getSymbol();
469   if (S->isTemporary()) {
470     if (!S->isInSection()) {
471       getContext().reportError(
472           SRE->getLoc(), Twine("Reference to undefined temporary symbol ") +
473                              "`" + S->getName() + "`");
474       return;
475     }
476     S = S->getSection().getBeginSymbol();
477     S->setUsedInReloc();
478     SRE =
479         MCSymbolRefExpr::create(S, SRE->getKind(), getContext(), SRE->getLoc());
480     return;
481   }
482   // Not a temporary, referece it as a weak undefined.
483   bool Created;
484   getAssembler().registerSymbol(*S, &Created);
485   if (Created) {
486     cast<MCSymbolELF>(S)->setBinding(ELF::STB_WEAK);
487     cast<MCSymbolELF>(S)->setExternal(true);
488   }
489 }
490 
finalizeCGProfile()491 void MCELFStreamer::finalizeCGProfile() {
492   for (MCAssembler::CGProfileEntry &E : getAssembler().CGProfile) {
493     finalizeCGProfileEntry(E.From);
494     finalizeCGProfileEntry(E.To);
495   }
496 }
497 
emitInstToFragment(const MCInst & Inst,const MCSubtargetInfo & STI)498 void MCELFStreamer::emitInstToFragment(const MCInst &Inst,
499                                        const MCSubtargetInfo &STI) {
500   this->MCObjectStreamer::emitInstToFragment(Inst, STI);
501   MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
502 
503   for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
504     fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
505 }
506 
507 // A fragment can only have one Subtarget, and when bundling is enabled we
508 // sometimes need to use the same fragment. We give an error if there
509 // are conflicting Subtargets.
CheckBundleSubtargets(const MCSubtargetInfo * OldSTI,const MCSubtargetInfo * NewSTI)510 static void CheckBundleSubtargets(const MCSubtargetInfo *OldSTI,
511                                   const MCSubtargetInfo *NewSTI) {
512   if (OldSTI && NewSTI && OldSTI != NewSTI)
513     report_fatal_error("A Bundle can only have one Subtarget.");
514 }
515 
emitInstToData(const MCInst & Inst,const MCSubtargetInfo & STI)516 void MCELFStreamer::emitInstToData(const MCInst &Inst,
517                                    const MCSubtargetInfo &STI) {
518   MCAssembler &Assembler = getAssembler();
519   SmallVector<MCFixup, 4> Fixups;
520   SmallString<256> Code;
521   raw_svector_ostream VecOS(Code);
522   Assembler.getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
523 
524   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
525     fixSymbolsInTLSFixups(Fixups[i].getValue());
526 
527   // There are several possibilities here:
528   //
529   // If bundling is disabled, append the encoded instruction to the current data
530   // fragment (or create a new such fragment if the current fragment is not a
531   // data fragment, or the Subtarget has changed).
532   //
533   // If bundling is enabled:
534   // - If we're not in a bundle-locked group, emit the instruction into a
535   //   fragment of its own. If there are no fixups registered for the
536   //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
537   //   MCDataFragment.
538   // - If we're in a bundle-locked group, append the instruction to the current
539   //   data fragment because we want all the instructions in a group to get into
540   //   the same fragment. Be careful not to do that for the first instruction in
541   //   the group, though.
542   MCDataFragment *DF;
543 
544   if (Assembler.isBundlingEnabled()) {
545     MCSection &Sec = *getCurrentSectionOnly();
546     if (Assembler.getRelaxAll() && isBundleLocked()) {
547       // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
548       // the current bundle group.
549       DF = BundleGroups.back();
550       CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
551     }
552     else if (Assembler.getRelaxAll() && !isBundleLocked())
553       // When not in a bundle-locked group and the -mc-relax-all flag is used,
554       // we create a new temporary fragment which will be later merged into
555       // the current fragment.
556       DF = new MCDataFragment();
557     else if (isBundleLocked() && !Sec.isBundleGroupBeforeFirstInst()) {
558       // If we are bundle-locked, we re-use the current fragment.
559       // The bundle-locking directive ensures this is a new data fragment.
560       DF = cast<MCDataFragment>(getCurrentFragment());
561       CheckBundleSubtargets(DF->getSubtargetInfo(), &STI);
562     }
563     else if (!isBundleLocked() && Fixups.size() == 0) {
564       // Optimize memory usage by emitting the instruction to a
565       // MCCompactEncodedInstFragment when not in a bundle-locked group and
566       // there are no fixups registered.
567       MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
568       insert(CEIF);
569       CEIF->getContents().append(Code.begin(), Code.end());
570       CEIF->setHasInstructions(STI);
571       return;
572     } else {
573       DF = new MCDataFragment();
574       insert(DF);
575     }
576     if (Sec.getBundleLockState() == MCSection::BundleLockedAlignToEnd) {
577       // If this fragment is for a group marked "align_to_end", set a flag
578       // in the fragment. This can happen after the fragment has already been
579       // created if there are nested bundle_align groups and an inner one
580       // is the one marked align_to_end.
581       DF->setAlignToBundleEnd(true);
582     }
583 
584     // We're now emitting an instruction in a bundle group, so this flag has
585     // to be turned off.
586     Sec.setBundleGroupBeforeFirstInst(false);
587   } else {
588     DF = getOrCreateDataFragment(&STI);
589   }
590 
591   // Add the fixups and data.
592   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
593     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
594     DF->getFixups().push_back(Fixups[i]);
595   }
596   DF->setHasInstructions(STI);
597   DF->getContents().append(Code.begin(), Code.end());
598 
599   if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
600     if (!isBundleLocked()) {
601       mergeFragment(getOrCreateDataFragment(&STI), DF);
602       delete DF;
603     }
604   }
605 }
606 
emitBundleAlignMode(unsigned AlignPow2)607 void MCELFStreamer::emitBundleAlignMode(unsigned AlignPow2) {
608   assert(AlignPow2 <= 30 && "Invalid bundle alignment");
609   MCAssembler &Assembler = getAssembler();
610   if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
611                         Assembler.getBundleAlignSize() == 1U << AlignPow2))
612     Assembler.setBundleAlignSize(1U << AlignPow2);
613   else
614     report_fatal_error(".bundle_align_mode cannot be changed once set");
615 }
616 
emitBundleLock(bool AlignToEnd)617 void MCELFStreamer::emitBundleLock(bool AlignToEnd) {
618   MCSection &Sec = *getCurrentSectionOnly();
619 
620   // Sanity checks
621   //
622   if (!getAssembler().isBundlingEnabled())
623     report_fatal_error(".bundle_lock forbidden when bundling is disabled");
624 
625   if (!isBundleLocked())
626     Sec.setBundleGroupBeforeFirstInst(true);
627 
628   if (getAssembler().getRelaxAll() && !isBundleLocked()) {
629     // TODO: drop the lock state and set directly in the fragment
630     MCDataFragment *DF = new MCDataFragment();
631     BundleGroups.push_back(DF);
632   }
633 
634   Sec.setBundleLockState(AlignToEnd ? MCSection::BundleLockedAlignToEnd
635                                     : MCSection::BundleLocked);
636 }
637 
emitBundleUnlock()638 void MCELFStreamer::emitBundleUnlock() {
639   MCSection &Sec = *getCurrentSectionOnly();
640 
641   // Sanity checks
642   if (!getAssembler().isBundlingEnabled())
643     report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
644   else if (!isBundleLocked())
645     report_fatal_error(".bundle_unlock without matching lock");
646   else if (Sec.isBundleGroupBeforeFirstInst())
647     report_fatal_error("Empty bundle-locked group is forbidden");
648 
649   // When the -mc-relax-all flag is used, we emit instructions to fragments
650   // stored on a stack. When the bundle unlock is emitted, we pop a fragment
651   // from the stack a merge it to the one below.
652   if (getAssembler().getRelaxAll()) {
653     assert(!BundleGroups.empty() && "There are no bundle groups");
654     MCDataFragment *DF = BundleGroups.back();
655 
656     // FIXME: Use BundleGroups to track the lock state instead.
657     Sec.setBundleLockState(MCSection::NotBundleLocked);
658 
659     // FIXME: Use more separate fragments for nested groups.
660     if (!isBundleLocked()) {
661       mergeFragment(getOrCreateDataFragment(DF->getSubtargetInfo()), DF);
662       BundleGroups.pop_back();
663       delete DF;
664     }
665 
666     if (Sec.getBundleLockState() != MCSection::BundleLockedAlignToEnd)
667       getOrCreateDataFragment()->setAlignToBundleEnd(false);
668   } else
669     Sec.setBundleLockState(MCSection::NotBundleLocked);
670 }
671 
finishImpl()672 void MCELFStreamer::finishImpl() {
673   // Ensure the last section gets aligned if necessary.
674   MCSection *CurSection = getCurrentSectionOnly();
675   setSectionAlignmentForBundling(getAssembler(), CurSection);
676 
677   finalizeCGProfile();
678   emitFrames(nullptr);
679 
680   this->MCObjectStreamer::finishImpl();
681 }
682 
emitThumbFunc(MCSymbol * Func)683 void MCELFStreamer::emitThumbFunc(MCSymbol *Func) {
684   llvm_unreachable("Generic ELF doesn't support this directive");
685 }
686 
emitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)687 void MCELFStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
688   llvm_unreachable("ELF doesn't support this directive");
689 }
690 
emitZerofill(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment,TailPaddingAmount TailPadding,SMLoc Loc)691 void MCELFStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
692                                  uint64_t Size, unsigned ByteAlignment,
693                                  TailPaddingAmount TailPadding, SMLoc Loc) {
694   llvm_unreachable("ELF doesn't support this directive");
695 }
696 
emitTBSSSymbol(MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment,TailPaddingAmount TailPadding)697 void MCELFStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
698                                    uint64_t Size, unsigned ByteAlignment,
699                                    TailPaddingAmount TailPadding) {
700   llvm_unreachable("ELF doesn't support this directive");
701 }
702 
createELFStreamer(MCContext & Context,std::unique_ptr<MCAsmBackend> && MAB,std::unique_ptr<MCObjectWriter> && OW,std::unique_ptr<MCCodeEmitter> && CE,bool RelaxAll)703 MCStreamer *llvm::createELFStreamer(MCContext &Context,
704                                     std::unique_ptr<MCAsmBackend> &&MAB,
705                                     std::unique_ptr<MCObjectWriter> &&OW,
706                                     std::unique_ptr<MCCodeEmitter> &&CE,
707                                     bool RelaxAll) {
708   MCELFStreamer *S =
709       new MCELFStreamer(Context, std::move(MAB), std::move(OW), std::move(CE));
710   if (RelaxAll)
711     S->getAssembler().setRelaxAll(true);
712   return S;
713 }
714