1 //===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file assembles .s files and emits ELF .o object files.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/MC/MCELFStreamer.h"
15 #include "llvm/ADT/STLExtras.h"
16 #include "llvm/ADT/SmallPtrSet.h"
17 #include "llvm/MC/MCAsmBackend.h"
18 #include "llvm/MC/MCAsmLayout.h"
19 #include "llvm/MC/MCAsmInfo.h"
20 #include "llvm/MC/MCAssembler.h"
21 #include "llvm/MC/MCCodeEmitter.h"
22 #include "llvm/MC/MCContext.h"
23 #include "llvm/MC/MCELF.h"
24 #include "llvm/MC/MCELFSymbolFlags.h"
25 #include "llvm/MC/MCExpr.h"
26 #include "llvm/MC/MCInst.h"
27 #include "llvm/MC/MCObjectFileInfo.h"
28 #include "llvm/MC/MCObjectStreamer.h"
29 #include "llvm/MC/MCSection.h"
30 #include "llvm/MC/MCSectionELF.h"
31 #include "llvm/MC/MCSymbol.h"
32 #include "llvm/MC/MCValue.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ELF.h"
35 #include "llvm/Support/ErrorHandling.h"
36 #include "llvm/Support/TargetRegistry.h"
37 #include "llvm/Support/raw_ostream.h"
38 
39 using namespace llvm;
40 
~MCELFStreamer()41 MCELFStreamer::~MCELFStreamer() {
42 }
43 
mergeFragment(MCDataFragment * DF,MCEncodedFragmentWithFixups * EF)44 void MCELFStreamer::mergeFragment(MCDataFragment *DF,
45                                   MCEncodedFragmentWithFixups *EF) {
46   MCAssembler &Assembler = getAssembler();
47 
48   if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
49     uint64_t FSize = EF->getContents().size();
50 
51     if (FSize > Assembler.getBundleAlignSize())
52       report_fatal_error("Fragment can't be larger than a bundle size");
53 
54     uint64_t RequiredBundlePadding = computeBundlePadding(
55         Assembler, EF, DF->getContents().size(), FSize);
56 
57     if (RequiredBundlePadding > UINT8_MAX)
58       report_fatal_error("Padding cannot exceed 255 bytes");
59 
60     if (RequiredBundlePadding > 0) {
61       SmallString<256> Code;
62       raw_svector_ostream VecOS(Code);
63       MCObjectWriter *OW = Assembler.getBackend().createObjectWriter(VecOS);
64 
65       EF->setBundlePadding(static_cast<uint8_t>(RequiredBundlePadding));
66 
67       Assembler.writeFragmentPadding(*EF, FSize, OW);
68       VecOS.flush();
69       delete OW;
70 
71       DF->getContents().append(Code.begin(), Code.end());
72     }
73   }
74 
75   flushPendingLabels(DF, DF->getContents().size());
76 
77   for (unsigned i = 0, e = EF->getFixups().size(); i != e; ++i) {
78     EF->getFixups()[i].setOffset(EF->getFixups()[i].getOffset() +
79                                  DF->getContents().size());
80     DF->getFixups().push_back(EF->getFixups()[i]);
81   }
82   DF->setHasInstructions(true);
83   DF->getContents().append(EF->getContents().begin(), EF->getContents().end());
84 }
85 
InitSections(bool NoExecStack)86 void MCELFStreamer::InitSections(bool NoExecStack) {
87   // This emulates the same behavior of GNU as. This makes it easier
88   // to compare the output as the major sections are in the same order.
89   MCContext &Ctx = getContext();
90   SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
91   EmitCodeAlignment(4);
92 
93   SwitchSection(Ctx.getObjectFileInfo()->getDataSection());
94   EmitCodeAlignment(4);
95 
96   SwitchSection(Ctx.getObjectFileInfo()->getBSSSection());
97   EmitCodeAlignment(4);
98 
99   SwitchSection(Ctx.getObjectFileInfo()->getTextSection());
100 
101   if (NoExecStack)
102     SwitchSection(Ctx.getAsmInfo()->getNonexecutableStackSection(Ctx));
103 }
104 
EmitLabel(MCSymbol * Symbol)105 void MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
106   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
107 
108   MCObjectStreamer::EmitLabel(Symbol);
109 
110   const MCSectionELF &Section =
111     static_cast<const MCSectionELF&>(Symbol->getSection());
112   MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
113   if (Section.getFlags() & ELF::SHF_TLS)
114     MCELF::SetType(SD, ELF::STT_TLS);
115 }
116 
EmitAssemblerFlag(MCAssemblerFlag Flag)117 void MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
118   // Let the target do whatever target specific stuff it needs to do.
119   getAssembler().getBackend().handleAssemblerFlag(Flag);
120   // Do any generic stuff we need to do.
121   switch (Flag) {
122   case MCAF_SyntaxUnified: return; // no-op here.
123   case MCAF_Code16: return; // Change parsing mode; no-op here.
124   case MCAF_Code32: return; // Change parsing mode; no-op here.
125   case MCAF_Code64: return; // Change parsing mode; no-op here.
126   case MCAF_SubsectionsViaSymbols:
127     getAssembler().setSubsectionsViaSymbols(true);
128     return;
129   }
130 
131   llvm_unreachable("invalid assembler flag!");
132 }
133 
ChangeSection(const MCSection * Section,const MCExpr * Subsection)134 void MCELFStreamer::ChangeSection(const MCSection *Section,
135                                   const MCExpr *Subsection) {
136   MCSectionData *CurSection = getCurrentSectionData();
137   if (CurSection && CurSection->isBundleLocked())
138     report_fatal_error("Unterminated .bundle_lock when changing a section");
139 
140   MCAssembler &Asm = getAssembler();
141   auto *SectionELF = static_cast<const MCSectionELF *>(Section);
142   const MCSymbol *Grp = SectionELF->getGroup();
143   if (Grp)
144     Asm.getOrCreateSymbolData(*Grp);
145 
146   this->MCObjectStreamer::ChangeSection(Section, Subsection);
147   MCSymbol *SectionSymbol = getContext().getOrCreateSectionSymbol(*SectionELF);
148   if (SectionSymbol->isUndefined()) {
149     EmitLabel(SectionSymbol);
150     MCELF::SetType(Asm.getSymbolData(*SectionSymbol), ELF::STT_SECTION);
151   }
152 }
153 
EmitWeakReference(MCSymbol * Alias,const MCSymbol * Symbol)154 void MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
155   getAssembler().getOrCreateSymbolData(*Symbol);
156   const MCExpr *Value = MCSymbolRefExpr::Create(
157       Symbol, MCSymbolRefExpr::VK_WEAKREF, getContext());
158   Alias->setVariableValue(Value);
159 }
160 
161 // When GNU as encounters more than one .type declaration for an object it seems
162 // to use a mechanism similar to the one below to decide which type is actually
163 // used in the object file.  The greater of T1 and T2 is selected based on the
164 // following ordering:
165 //  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
166 // If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
167 // provided type).
CombineSymbolTypes(unsigned T1,unsigned T2)168 static unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
169   for (unsigned Type : {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
170                         ELF::STT_GNU_IFUNC, ELF::STT_TLS}) {
171     if (T1 == Type)
172       return T2;
173     if (T2 == Type)
174       return T1;
175   }
176 
177   return T2;
178 }
179 
EmitSymbolAttribute(MCSymbol * Symbol,MCSymbolAttr Attribute)180 bool MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
181                                         MCSymbolAttr Attribute) {
182   // Indirect symbols are handled differently, to match how 'as' handles
183   // them. This makes writing matching .o files easier.
184   if (Attribute == MCSA_IndirectSymbol) {
185     // Note that we intentionally cannot use the symbol data here; this is
186     // important for matching the string table that 'as' generates.
187     IndirectSymbolData ISD;
188     ISD.Symbol = Symbol;
189     ISD.SectionData = getCurrentSectionData();
190     getAssembler().getIndirectSymbols().push_back(ISD);
191     return true;
192   }
193 
194   // Adding a symbol attribute always introduces the symbol, note that an
195   // important side effect of calling getOrCreateSymbolData here is to register
196   // the symbol with the assembler.
197   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
198 
199   // The implementation of symbol attributes is designed to match 'as', but it
200   // leaves much to desired. It doesn't really make sense to arbitrarily add and
201   // remove flags, but 'as' allows this (in particular, see .desc).
202   //
203   // In the future it might be worth trying to make these operations more well
204   // defined.
205   switch (Attribute) {
206   case MCSA_LazyReference:
207   case MCSA_Reference:
208   case MCSA_SymbolResolver:
209   case MCSA_PrivateExtern:
210   case MCSA_WeakDefinition:
211   case MCSA_WeakDefAutoPrivate:
212   case MCSA_Invalid:
213   case MCSA_IndirectSymbol:
214     return false;
215 
216   case MCSA_NoDeadStrip:
217     // Ignore for now.
218     break;
219 
220   case MCSA_ELF_TypeGnuUniqueObject:
221     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD), ELF::STT_OBJECT));
222     MCELF::SetBinding(SD, ELF::STB_GNU_UNIQUE);
223     SD.setExternal(true);
224     BindingExplicitlySet.insert(Symbol);
225     break;
226 
227   case MCSA_Global:
228     MCELF::SetBinding(SD, ELF::STB_GLOBAL);
229     SD.setExternal(true);
230     BindingExplicitlySet.insert(Symbol);
231     break;
232 
233   case MCSA_WeakReference:
234   case MCSA_Weak:
235     MCELF::SetBinding(SD, ELF::STB_WEAK);
236     SD.setExternal(true);
237     BindingExplicitlySet.insert(Symbol);
238     break;
239 
240   case MCSA_Local:
241     MCELF::SetBinding(SD, ELF::STB_LOCAL);
242     SD.setExternal(false);
243     BindingExplicitlySet.insert(Symbol);
244     break;
245 
246   case MCSA_ELF_TypeFunction:
247     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
248                                           ELF::STT_FUNC));
249     break;
250 
251   case MCSA_ELF_TypeIndFunction:
252     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
253                                           ELF::STT_GNU_IFUNC));
254     break;
255 
256   case MCSA_ELF_TypeObject:
257     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
258                                           ELF::STT_OBJECT));
259     break;
260 
261   case MCSA_ELF_TypeTLS:
262     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
263                                           ELF::STT_TLS));
264     break;
265 
266   case MCSA_ELF_TypeCommon:
267     // TODO: Emit these as a common symbol.
268     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
269                                           ELF::STT_OBJECT));
270     break;
271 
272   case MCSA_ELF_TypeNoType:
273     MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
274                                           ELF::STT_NOTYPE));
275     break;
276 
277   case MCSA_Protected:
278     MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
279     break;
280 
281   case MCSA_Hidden:
282     MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
283     break;
284 
285   case MCSA_Internal:
286     MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
287     break;
288   }
289 
290   return true;
291 }
292 
EmitCommonSymbol(MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)293 void MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
294                                        unsigned ByteAlignment) {
295   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
296 
297   if (!BindingExplicitlySet.count(Symbol)) {
298     MCELF::SetBinding(SD, ELF::STB_GLOBAL);
299     SD.setExternal(true);
300   }
301 
302   MCELF::SetType(SD, ELF::STT_OBJECT);
303 
304   if (MCELF::GetBinding(SD) == ELF_STB_Local) {
305     const MCSection *Section = getAssembler().getContext().getELFSection(
306         ".bss", ELF::SHT_NOBITS, ELF::SHF_WRITE | ELF::SHF_ALLOC);
307 
308     AssignSection(Symbol, Section);
309 
310     struct LocalCommon L = {&SD, Size, ByteAlignment};
311     LocalCommons.push_back(L);
312   } else {
313     SD.setCommon(Size, ByteAlignment);
314   }
315 
316   SD.setSize(MCConstantExpr::Create(Size, getContext()));
317 }
318 
EmitELFSize(MCSymbol * Symbol,const MCExpr * Value)319 void MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
320   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
321   SD.setSize(Value);
322 }
323 
EmitLocalCommonSymbol(MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)324 void MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
325                                           unsigned ByteAlignment) {
326   // FIXME: Should this be caught and done earlier?
327   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
328   MCELF::SetBinding(SD, ELF::STB_LOCAL);
329   SD.setExternal(false);
330   BindingExplicitlySet.insert(Symbol);
331   EmitCommonSymbol(Symbol, Size, ByteAlignment);
332 }
333 
EmitValueImpl(const MCExpr * Value,unsigned Size,const SMLoc & Loc)334 void MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
335                                   const SMLoc &Loc) {
336   if (getCurrentSectionData()->isBundleLocked())
337     report_fatal_error("Emitting values inside a locked bundle is forbidden");
338   fixSymbolsInTLSFixups(Value);
339   MCObjectStreamer::EmitValueImpl(Value, Size, Loc);
340 }
341 
EmitValueToAlignment(unsigned ByteAlignment,int64_t Value,unsigned ValueSize,unsigned MaxBytesToEmit)342 void MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
343                                          int64_t Value,
344                                          unsigned ValueSize,
345                                          unsigned MaxBytesToEmit) {
346   if (getCurrentSectionData()->isBundleLocked())
347     report_fatal_error("Emitting values inside a locked bundle is forbidden");
348   MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
349                                          ValueSize, MaxBytesToEmit);
350 }
351 
352 // Add a symbol for the file name of this module. They start after the
353 // null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
354 // with the same name may appear.
EmitFileDirective(StringRef Filename)355 void MCELFStreamer::EmitFileDirective(StringRef Filename) {
356   getAssembler().addFileName(Filename);
357 }
358 
EmitIdent(StringRef IdentString)359 void MCELFStreamer::EmitIdent(StringRef IdentString) {
360   const MCSection *Comment = getAssembler().getContext().getELFSection(
361       ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS, 1, "");
362   PushSection();
363   SwitchSection(Comment);
364   if (!SeenIdent) {
365     EmitIntValue(0, 1);
366     SeenIdent = true;
367   }
368   EmitBytes(IdentString);
369   EmitIntValue(0, 1);
370   PopSection();
371 }
372 
fixSymbolsInTLSFixups(const MCExpr * expr)373 void MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
374   switch (expr->getKind()) {
375   case MCExpr::Target:
376     cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
377     break;
378   case MCExpr::Constant:
379     break;
380 
381   case MCExpr::Binary: {
382     const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
383     fixSymbolsInTLSFixups(be->getLHS());
384     fixSymbolsInTLSFixups(be->getRHS());
385     break;
386   }
387 
388   case MCExpr::SymbolRef: {
389     const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
390     switch (symRef.getKind()) {
391     default:
392       return;
393     case MCSymbolRefExpr::VK_GOTTPOFF:
394     case MCSymbolRefExpr::VK_INDNTPOFF:
395     case MCSymbolRefExpr::VK_NTPOFF:
396     case MCSymbolRefExpr::VK_GOTNTPOFF:
397     case MCSymbolRefExpr::VK_TLSGD:
398     case MCSymbolRefExpr::VK_TLSLD:
399     case MCSymbolRefExpr::VK_TLSLDM:
400     case MCSymbolRefExpr::VK_TPOFF:
401     case MCSymbolRefExpr::VK_DTPOFF:
402     case MCSymbolRefExpr::VK_Mips_TLSGD:
403     case MCSymbolRefExpr::VK_Mips_GOTTPREL:
404     case MCSymbolRefExpr::VK_Mips_TPREL_HI:
405     case MCSymbolRefExpr::VK_Mips_TPREL_LO:
406     case MCSymbolRefExpr::VK_PPC_DTPMOD:
407     case MCSymbolRefExpr::VK_PPC_TPREL:
408     case MCSymbolRefExpr::VK_PPC_TPREL_LO:
409     case MCSymbolRefExpr::VK_PPC_TPREL_HI:
410     case MCSymbolRefExpr::VK_PPC_TPREL_HA:
411     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
412     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
413     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
414     case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
415     case MCSymbolRefExpr::VK_PPC_DTPREL:
416     case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
417     case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
418     case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
419     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
420     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
421     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
422     case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
423     case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
424     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
425     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
426     case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
427     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
428     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
429     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
430     case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
431     case MCSymbolRefExpr::VK_PPC_TLS:
432     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
433     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
434     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
435     case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
436     case MCSymbolRefExpr::VK_PPC_TLSGD:
437     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
438     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
439     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
440     case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
441     case MCSymbolRefExpr::VK_PPC_TLSLD:
442       break;
443     }
444     MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
445     MCELF::SetType(SD, ELF::STT_TLS);
446     break;
447   }
448 
449   case MCExpr::Unary:
450     fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
451     break;
452   }
453 }
454 
EmitInstToFragment(const MCInst & Inst,const MCSubtargetInfo & STI)455 void MCELFStreamer::EmitInstToFragment(const MCInst &Inst,
456                                        const MCSubtargetInfo &STI) {
457   this->MCObjectStreamer::EmitInstToFragment(Inst, STI);
458   MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
459 
460   for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
461     fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
462 }
463 
EmitInstToData(const MCInst & Inst,const MCSubtargetInfo & STI)464 void MCELFStreamer::EmitInstToData(const MCInst &Inst,
465                                    const MCSubtargetInfo &STI) {
466   MCAssembler &Assembler = getAssembler();
467   SmallVector<MCFixup, 4> Fixups;
468   SmallString<256> Code;
469   raw_svector_ostream VecOS(Code);
470   Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups, STI);
471   VecOS.flush();
472 
473   for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
474     fixSymbolsInTLSFixups(Fixups[i].getValue());
475 
476   // There are several possibilities here:
477   //
478   // If bundling is disabled, append the encoded instruction to the current data
479   // fragment (or create a new such fragment if the current fragment is not a
480   // data fragment).
481   //
482   // If bundling is enabled:
483   // - If we're not in a bundle-locked group, emit the instruction into a
484   //   fragment of its own. If there are no fixups registered for the
485   //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
486   //   MCDataFragment.
487   // - If we're in a bundle-locked group, append the instruction to the current
488   //   data fragment because we want all the instructions in a group to get into
489   //   the same fragment. Be careful not to do that for the first instruction in
490   //   the group, though.
491   MCDataFragment *DF;
492 
493   if (Assembler.isBundlingEnabled()) {
494     MCSectionData *SD = getCurrentSectionData();
495     if (Assembler.getRelaxAll() && SD->isBundleLocked())
496       // If the -mc-relax-all flag is used and we are bundle-locked, we re-use
497       // the current bundle group.
498       DF = BundleGroups.back();
499     else if (Assembler.getRelaxAll() && !SD->isBundleLocked())
500       // When not in a bundle-locked group and the -mc-relax-all flag is used,
501       // we create a new temporary fragment which will be later merged into
502       // the current fragment.
503       DF = new MCDataFragment();
504     else if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst())
505       // If we are bundle-locked, we re-use the current fragment.
506       // The bundle-locking directive ensures this is a new data fragment.
507       DF = cast<MCDataFragment>(getCurrentFragment());
508     else if (!SD->isBundleLocked() && Fixups.size() == 0) {
509       // Optimize memory usage by emitting the instruction to a
510       // MCCompactEncodedInstFragment when not in a bundle-locked group and
511       // there are no fixups registered.
512       MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
513       insert(CEIF);
514       CEIF->getContents().append(Code.begin(), Code.end());
515       return;
516     } else {
517       DF = new MCDataFragment();
518       insert(DF);
519     }
520     if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) {
521       // If this fragment is for a group marked "align_to_end", set a flag
522       // in the fragment. This can happen after the fragment has already been
523       // created if there are nested bundle_align groups and an inner one
524       // is the one marked align_to_end.
525       DF->setAlignToBundleEnd(true);
526     }
527 
528     // We're now emitting an instruction in a bundle group, so this flag has
529     // to be turned off.
530     SD->setBundleGroupBeforeFirstInst(false);
531   } else {
532     DF = getOrCreateDataFragment();
533   }
534 
535   // Add the fixups and data.
536   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
537     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
538     DF->getFixups().push_back(Fixups[i]);
539   }
540   DF->setHasInstructions(true);
541   DF->getContents().append(Code.begin(), Code.end());
542 
543   if (Assembler.isBundlingEnabled() && Assembler.getRelaxAll()) {
544     MCSectionData *SD = getCurrentSectionData();
545     if (!SD->isBundleLocked()) {
546       mergeFragment(getOrCreateDataFragment(), DF);
547       delete DF;
548     }
549   }
550 }
551 
EmitBundleAlignMode(unsigned AlignPow2)552 void MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
553   assert(AlignPow2 <= 30 && "Invalid bundle alignment");
554   MCAssembler &Assembler = getAssembler();
555   if (AlignPow2 > 0 && (Assembler.getBundleAlignSize() == 0 ||
556                         Assembler.getBundleAlignSize() == 1U << AlignPow2))
557     Assembler.setBundleAlignSize(1U << AlignPow2);
558   else
559     report_fatal_error(".bundle_align_mode cannot be changed once set");
560 }
561 
EmitBundleLock(bool AlignToEnd)562 void MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
563   MCSectionData *SD = getCurrentSectionData();
564 
565   // Sanity checks
566   //
567   if (!getAssembler().isBundlingEnabled())
568     report_fatal_error(".bundle_lock forbidden when bundling is disabled");
569 
570   if (!SD->isBundleLocked())
571     SD->setBundleGroupBeforeFirstInst(true);
572 
573   if (getAssembler().getRelaxAll() && !SD->isBundleLocked()) {
574     // TODO: drop the lock state and set directly in the fragment
575     MCDataFragment *DF = new MCDataFragment();
576     BundleGroups.push_back(DF);
577   }
578 
579   SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd :
580                                       MCSectionData::BundleLocked);
581 }
582 
EmitBundleUnlock()583 void MCELFStreamer::EmitBundleUnlock() {
584   MCSectionData *SD = getCurrentSectionData();
585 
586   // Sanity checks
587   if (!getAssembler().isBundlingEnabled())
588     report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
589   else if (!SD->isBundleLocked())
590     report_fatal_error(".bundle_unlock without matching lock");
591   else if (SD->isBundleGroupBeforeFirstInst())
592     report_fatal_error("Empty bundle-locked group is forbidden");
593 
594   // When the -mc-relax-all flag is used, we emit instructions to fragments
595   // stored on a stack. When the bundle unlock is emited, we pop a fragment
596   // from the stack a merge it to the one below.
597   if (getAssembler().getRelaxAll()) {
598     assert(!BundleGroups.empty() && "There are no bundle groups");
599     MCDataFragment *DF = BundleGroups.back();
600 
601     // FIXME: Use BundleGroups to track the lock state instead.
602     SD->setBundleLockState(MCSectionData::NotBundleLocked);
603 
604     // FIXME: Use more separate fragments for nested groups.
605     if (!SD->isBundleLocked()) {
606       mergeFragment(getOrCreateDataFragment(), DF);
607       BundleGroups.pop_back();
608       delete DF;
609     }
610 
611     if (SD->getBundleLockState() != MCSectionData::BundleLockedAlignToEnd)
612       getOrCreateDataFragment()->setAlignToBundleEnd(false);
613   } else
614     SD->setBundleLockState(MCSectionData::NotBundleLocked);
615 }
616 
Flush()617 void MCELFStreamer::Flush() {
618   for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
619                                                 e = LocalCommons.end();
620        i != e; ++i) {
621     MCSymbolData *SD = i->SD;
622     uint64_t Size = i->Size;
623     unsigned ByteAlignment = i->ByteAlignment;
624     const MCSymbol &Symbol = SD->getSymbol();
625     const MCSection &Section = Symbol.getSection();
626 
627     MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
628     new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
629 
630     MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
631     SD->setFragment(F);
632 
633     // Update the maximum alignment of the section if necessary.
634     if (ByteAlignment > SectData.getAlignment())
635       SectData.setAlignment(ByteAlignment);
636   }
637 
638   LocalCommons.clear();
639 }
640 
FinishImpl()641 void MCELFStreamer::FinishImpl() {
642   EmitFrames(nullptr);
643 
644   Flush();
645 
646   this->MCObjectStreamer::FinishImpl();
647 }
648 
createELFStreamer(MCContext & Context,MCAsmBackend & MAB,raw_pwrite_stream & OS,MCCodeEmitter * CE,bool RelaxAll)649 MCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
650                                     raw_pwrite_stream &OS, MCCodeEmitter *CE,
651                                     bool RelaxAll) {
652   MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
653   if (RelaxAll)
654     S->getAssembler().setRelaxAll(true);
655   return S;
656 }
657 
EmitThumbFunc(MCSymbol * Func)658 void MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
659   llvm_unreachable("Generic ELF doesn't support this directive");
660 }
661 
EmitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)662 void MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
663   llvm_unreachable("ELF doesn't support this directive");
664 }
665 
BeginCOFFSymbolDef(const MCSymbol * Symbol)666 void MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
667   llvm_unreachable("ELF doesn't support this directive");
668 }
669 
EmitCOFFSymbolStorageClass(int StorageClass)670 void MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
671   llvm_unreachable("ELF doesn't support this directive");
672 }
673 
EmitCOFFSymbolType(int Type)674 void MCELFStreamer::EmitCOFFSymbolType(int Type) {
675   llvm_unreachable("ELF doesn't support this directive");
676 }
677 
EndCOFFSymbolDef()678 void MCELFStreamer::EndCOFFSymbolDef() {
679   llvm_unreachable("ELF doesn't support this directive");
680 }
681 
EmitZerofill(const MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)682 void MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
683                                  uint64_t Size, unsigned ByteAlignment) {
684   llvm_unreachable("ELF doesn't support this directive");
685 }
686 
EmitTBSSSymbol(const MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)687 void MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
688                                    uint64_t Size, unsigned ByteAlignment) {
689   llvm_unreachable("ELF doesn't support this directive");
690 }
691