1249423Sdim//===- lib/MC/MCELFStreamer.cpp - ELF Object Output -----------------------===//
2212793Sdim//
3212793Sdim//                     The LLVM Compiler Infrastructure
4212793Sdim//
5212793Sdim// This file is distributed under the University of Illinois Open Source
6212793Sdim// License. See LICENSE.TXT for details.
7212793Sdim//
8212793Sdim//===----------------------------------------------------------------------===//
9212793Sdim//
10212793Sdim// This file assembles .s files and emits ELF .o object files.
11212793Sdim//
12212793Sdim//===----------------------------------------------------------------------===//
13212793Sdim
14249423Sdim#include "llvm/MC/MCELFStreamer.h"
15234353Sdim#include "llvm/ADT/SmallPtrSet.h"
16251662Sdim#include "llvm/ADT/STLExtras.h"
17234353Sdim#include "llvm/MC/MCAssembler.h"
18234353Sdim#include "llvm/MC/MCCodeEmitter.h"
19234353Sdim#include "llvm/MC/MCContext.h"
20249423Sdim#include "llvm/MC/MCELF.h"
21212793Sdim#include "llvm/MC/MCELFSymbolFlags.h"
22212793Sdim#include "llvm/MC/MCExpr.h"
23212793Sdim#include "llvm/MC/MCInst.h"
24234353Sdim#include "llvm/MC/MCObjectStreamer.h"
25212793Sdim#include "llvm/MC/MCSection.h"
26249423Sdim#include "llvm/MC/MCSectionELF.h"
27212793Sdim#include "llvm/MC/MCSymbol.h"
28218893Sdim#include "llvm/MC/MCValue.h"
29212793Sdim#include "llvm/Support/Debug.h"
30212793Sdim#include "llvm/Support/ELF.h"
31212793Sdim#include "llvm/Support/ErrorHandling.h"
32212793Sdim#include "llvm/Support/raw_ostream.h"
33212793Sdim
34212793Sdimusing namespace llvm;
35212793Sdim
36234353Sdim
37249423Sdiminline void MCELFStreamer::SetSection(StringRef Section, unsigned Type,
38249423Sdim                                      unsigned Flags, SectionKind Kind) {
39249423Sdim  SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
40249423Sdim}
41234353Sdim
42249423Sdiminline void MCELFStreamer::SetSectionData() {
43249423Sdim  SetSection(".data",
44249423Sdim             ELF::SHT_PROGBITS,
45249423Sdim             ELF::SHF_WRITE | ELF::SHF_ALLOC,
46249423Sdim             SectionKind::getDataRel());
47249423Sdim  EmitCodeAlignment(4, 0);
48249423Sdim}
49234353Sdim
50249423Sdiminline void MCELFStreamer::SetSectionText() {
51249423Sdim  SetSection(".text",
52249423Sdim             ELF::SHT_PROGBITS,
53249423Sdim             ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
54249423Sdim             SectionKind::getText());
55249423Sdim  EmitCodeAlignment(4, 0);
56249423Sdim}
57234353Sdim
58249423Sdiminline void MCELFStreamer::SetSectionBss() {
59249423Sdim  SetSection(".bss",
60249423Sdim             ELF::SHT_NOBITS,
61249423Sdim             ELF::SHF_WRITE | ELF::SHF_ALLOC,
62249423Sdim             SectionKind::getBSS());
63249423Sdim  EmitCodeAlignment(4, 0);
64249423Sdim}
65234353Sdim
66249423SdimMCELFStreamer::~MCELFStreamer() {
67249423Sdim}
68234353Sdim
69249423Sdimvoid MCELFStreamer::InitToTextSection() {
70249423Sdim  SetSectionText();
71234353Sdim}
72234353Sdim
73218893Sdimvoid MCELFStreamer::InitSections() {
74218893Sdim  // This emulates the same behavior of GNU as. This makes it easier
75218893Sdim  // to compare the output as the major sections are in the same order.
76218893Sdim  SetSectionText();
77218893Sdim  SetSectionData();
78218893Sdim  SetSectionBss();
79218893Sdim  SetSectionText();
80218893Sdim}
81218893Sdim
82212793Sdimvoid MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
83212793Sdim  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
84212793Sdim
85218893Sdim  MCObjectStreamer::EmitLabel(Symbol);
86212793Sdim
87218893Sdim  const MCSectionELF &Section =
88218893Sdim    static_cast<const MCSectionELF&>(Symbol->getSection());
89218893Sdim  MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
90218893Sdim  if (Section.getFlags() & ELF::SHF_TLS)
91221345Sdim    MCELF::SetType(SD, ELF::STT_TLS);
92212793Sdim}
93212793Sdim
94249423Sdimvoid MCELFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
95249423Sdim  EmitLabel(Symbol);
96249423Sdim}
97249423Sdim
98212793Sdimvoid MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
99212793Sdim  switch (Flag) {
100218893Sdim  case MCAF_SyntaxUnified: return; // no-op here.
101226633Sdim  case MCAF_Code16: return; // Change parsing mode; no-op here.
102226633Sdim  case MCAF_Code32: return; // Change parsing mode; no-op here.
103226633Sdim  case MCAF_Code64: return; // Change parsing mode; no-op here.
104212793Sdim  case MCAF_SubsectionsViaSymbols:
105212793Sdim    getAssembler().setSubsectionsViaSymbols(true);
106212793Sdim    return;
107212793Sdim  }
108212793Sdim
109234353Sdim  llvm_unreachable("invalid assembler flag!");
110212793Sdim}
111212793Sdim
112251662Sdimvoid MCELFStreamer::ChangeSection(const MCSection *Section,
113251662Sdim                                  const MCExpr *Subsection) {
114249423Sdim  MCSectionData *CurSection = getCurrentSectionData();
115249423Sdim  if (CurSection && CurSection->isBundleLocked())
116249423Sdim    report_fatal_error("Unterminated .bundle_lock when changing a section");
117218893Sdim  const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
118218893Sdim  if (Grp)
119218893Sdim    getAssembler().getOrCreateSymbolData(*Grp);
120251662Sdim  this->MCObjectStreamer::ChangeSection(Section, Subsection);
121218893Sdim}
122218893Sdim
123218893Sdimvoid MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
124218893Sdim  getAssembler().getOrCreateSymbolData(*Symbol);
125218893Sdim  MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
126218893Sdim  AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
127218893Sdim  const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
128218893Sdim  Alias->setVariableValue(Value);
129218893Sdim}
130218893Sdim
131251662Sdim// When GNU as encounters more than one .type declaration for an object it seems
132251662Sdim// to use a mechanism similar to the one below to decide which type is actually
133251662Sdim// used in the object file.  The greater of T1 and T2 is selected based on the
134251662Sdim// following ordering:
135251662Sdim//  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
136251662Sdim// If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
137251662Sdim// provided type).
138251662Sdimstatic unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
139251662Sdim  unsigned TypeOrdering[] = {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
140251662Sdim                             ELF::STT_GNU_IFUNC, ELF::STT_TLS};
141251662Sdim  for (unsigned i = 0; i != array_lengthof(TypeOrdering); ++i) {
142251662Sdim    if (T1 == TypeOrdering[i])
143251662Sdim      return T2;
144251662Sdim    if (T2 == TypeOrdering[i])
145251662Sdim      return T1;
146251662Sdim  }
147251662Sdim
148251662Sdim  return T2;
149251662Sdim}
150251662Sdim
151212793Sdimvoid MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
152212793Sdim                                          MCSymbolAttr Attribute) {
153212793Sdim  // Indirect symbols are handled differently, to match how 'as' handles
154212793Sdim  // them. This makes writing matching .o files easier.
155212793Sdim  if (Attribute == MCSA_IndirectSymbol) {
156212793Sdim    // Note that we intentionally cannot use the symbol data here; this is
157212793Sdim    // important for matching the string table that 'as' generates.
158212793Sdim    IndirectSymbolData ISD;
159212793Sdim    ISD.Symbol = Symbol;
160212793Sdim    ISD.SectionData = getCurrentSectionData();
161212793Sdim    getAssembler().getIndirectSymbols().push_back(ISD);
162212793Sdim    return;
163212793Sdim  }
164212793Sdim
165212793Sdim  // Adding a symbol attribute always introduces the symbol, note that an
166212793Sdim  // important side effect of calling getOrCreateSymbolData here is to register
167212793Sdim  // the symbol with the assembler.
168212793Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
169212793Sdim
170212793Sdim  // The implementation of symbol attributes is designed to match 'as', but it
171212793Sdim  // leaves much to desired. It doesn't really make sense to arbitrarily add and
172212793Sdim  // remove flags, but 'as' allows this (in particular, see .desc).
173212793Sdim  //
174212793Sdim  // In the future it might be worth trying to make these operations more well
175212793Sdim  // defined.
176212793Sdim  switch (Attribute) {
177212793Sdim  case MCSA_LazyReference:
178212793Sdim  case MCSA_Reference:
179218893Sdim  case MCSA_SymbolResolver:
180212793Sdim  case MCSA_PrivateExtern:
181212793Sdim  case MCSA_WeakDefinition:
182212793Sdim  case MCSA_WeakDefAutoPrivate:
183212793Sdim  case MCSA_Invalid:
184212793Sdim  case MCSA_IndirectSymbol:
185234353Sdim    llvm_unreachable("Invalid symbol attribute for ELF!");
186212793Sdim
187243830Sdim  case MCSA_NoDeadStrip:
188218893Sdim  case MCSA_ELF_TypeGnuUniqueObject:
189218893Sdim    // Ignore for now.
190218893Sdim    break;
191218893Sdim
192212793Sdim  case MCSA_Global:
193221345Sdim    MCELF::SetBinding(SD, ELF::STB_GLOBAL);
194212793Sdim    SD.setExternal(true);
195218893Sdim    BindingExplicitlySet.insert(Symbol);
196212793Sdim    break;
197212793Sdim
198212793Sdim  case MCSA_WeakReference:
199212793Sdim  case MCSA_Weak:
200221345Sdim    MCELF::SetBinding(SD, ELF::STB_WEAK);
201218893Sdim    SD.setExternal(true);
202218893Sdim    BindingExplicitlySet.insert(Symbol);
203212793Sdim    break;
204212793Sdim
205212793Sdim  case MCSA_Local:
206221345Sdim    MCELF::SetBinding(SD, ELF::STB_LOCAL);
207218893Sdim    SD.setExternal(false);
208218893Sdim    BindingExplicitlySet.insert(Symbol);
209212793Sdim    break;
210212793Sdim
211212793Sdim  case MCSA_ELF_TypeFunction:
212251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
213251662Sdim                                          ELF::STT_FUNC));
214212793Sdim    break;
215212793Sdim
216234353Sdim  case MCSA_ELF_TypeIndFunction:
217251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
218251662Sdim                                          ELF::STT_GNU_IFUNC));
219234353Sdim    break;
220234353Sdim
221212793Sdim  case MCSA_ELF_TypeObject:
222251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
223251662Sdim                                          ELF::STT_OBJECT));
224212793Sdim    break;
225212793Sdim
226212793Sdim  case MCSA_ELF_TypeTLS:
227251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
228251662Sdim                                          ELF::STT_TLS));
229212793Sdim    break;
230212793Sdim
231212793Sdim  case MCSA_ELF_TypeCommon:
232251662Sdim    // TODO: Emit these as a common symbol.
233251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
234251662Sdim                                          ELF::STT_OBJECT));
235212793Sdim    break;
236212793Sdim
237212793Sdim  case MCSA_ELF_TypeNoType:
238251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
239251662Sdim                                          ELF::STT_NOTYPE));
240212793Sdim    break;
241212793Sdim
242212793Sdim  case MCSA_Protected:
243221345Sdim    MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
244212793Sdim    break;
245212793Sdim
246212793Sdim  case MCSA_Hidden:
247221345Sdim    MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
248212793Sdim    break;
249212793Sdim
250212793Sdim  case MCSA_Internal:
251221345Sdim    MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
252212793Sdim    break;
253212793Sdim  }
254212793Sdim}
255212793Sdim
256212793Sdimvoid MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
257212793Sdim                                       unsigned ByteAlignment) {
258212793Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
259212793Sdim
260218893Sdim  if (!BindingExplicitlySet.count(Symbol)) {
261221345Sdim    MCELF::SetBinding(SD, ELF::STB_GLOBAL);
262218893Sdim    SD.setExternal(true);
263218893Sdim  }
264218893Sdim
265221345Sdim  MCELF::SetType(SD, ELF::STT_OBJECT);
266218893Sdim
267221345Sdim  if (MCELF::GetBinding(SD) == ELF_STB_Local) {
268212793Sdim    const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
269234353Sdim                                                         ELF::SHT_NOBITS,
270234353Sdim                                                         ELF::SHF_WRITE |
271234353Sdim                                                         ELF::SHF_ALLOC,
272234353Sdim                                                         SectionKind::getBSS());
273218893Sdim    Symbol->setSection(*Section);
274212793Sdim
275218893Sdim    struct LocalCommon L = {&SD, Size, ByteAlignment};
276218893Sdim    LocalCommons.push_back(L);
277218893Sdim  } else {
278218893Sdim    SD.setCommon(Size, ByteAlignment);
279212793Sdim  }
280212793Sdim
281218893Sdim  SD.setSize(MCConstantExpr::Create(Size, getContext()));
282218893Sdim}
283212793Sdim
284249423Sdimvoid MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
285249423Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
286249423Sdim  SD.setSize(Value);
287249423Sdim}
288249423Sdim
289226633Sdimvoid MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
290226633Sdim                                          unsigned ByteAlignment) {
291218893Sdim  // FIXME: Should this be caught and done earlier?
292218893Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
293221345Sdim  MCELF::SetBinding(SD, ELF::STB_LOCAL);
294218893Sdim  SD.setExternal(false);
295218893Sdim  BindingExplicitlySet.insert(Symbol);
296226633Sdim  EmitCommonSymbol(Symbol, Size, ByteAlignment);
297212793Sdim}
298212793Sdim
299234353Sdimvoid MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
300234353Sdim                                  unsigned AddrSpace) {
301249423Sdim  if (getCurrentSectionData()->isBundleLocked())
302249423Sdim    report_fatal_error("Emitting values inside a locked bundle is forbidden");
303234353Sdim  fixSymbolsInTLSFixups(Value);
304234353Sdim  MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
305234353Sdim}
306234353Sdim
307249423Sdimvoid MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
308249423Sdim                                         int64_t Value,
309249423Sdim                                         unsigned ValueSize,
310249423Sdim                                         unsigned MaxBytesToEmit) {
311249423Sdim  if (getCurrentSectionData()->isBundleLocked())
312249423Sdim    report_fatal_error("Emitting values inside a locked bundle is forbidden");
313249423Sdim  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
314249423Sdim                                         ValueSize, MaxBytesToEmit);
315249423Sdim}
316234353Sdim
317249423Sdim
318212793Sdim// Add a symbol for the file name of this module. This is the second
319212793Sdim// entry in the module's symbol table (the first being the null symbol).
320212793Sdimvoid MCELFStreamer::EmitFileDirective(StringRef Filename) {
321212793Sdim  MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
322251662Sdim  Symbol->setSection(*getCurrentSection().first);
323212793Sdim  Symbol->setAbsolute();
324212793Sdim
325212793Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
326212793Sdim
327212793Sdim  SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
328212793Sdim}
329212793Sdim
330218893Sdimvoid  MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
331218893Sdim  switch (expr->getKind()) {
332249423Sdim  case MCExpr::Target:
333249423Sdim    cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
334249423Sdim    break;
335218893Sdim  case MCExpr::Constant:
336218893Sdim    break;
337212793Sdim
338218893Sdim  case MCExpr::Binary: {
339218893Sdim    const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
340218893Sdim    fixSymbolsInTLSFixups(be->getLHS());
341218893Sdim    fixSymbolsInTLSFixups(be->getRHS());
342218893Sdim    break;
343218893Sdim  }
344212793Sdim
345218893Sdim  case MCExpr::SymbolRef: {
346218893Sdim    const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
347218893Sdim    switch (symRef.getKind()) {
348218893Sdim    default:
349218893Sdim      return;
350221345Sdim    case MCSymbolRefExpr::VK_GOTTPOFF:
351221345Sdim    case MCSymbolRefExpr::VK_INDNTPOFF:
352218893Sdim    case MCSymbolRefExpr::VK_NTPOFF:
353218893Sdim    case MCSymbolRefExpr::VK_GOTNTPOFF:
354218893Sdim    case MCSymbolRefExpr::VK_TLSGD:
355221345Sdim    case MCSymbolRefExpr::VK_TLSLD:
356218893Sdim    case MCSymbolRefExpr::VK_TLSLDM:
357218893Sdim    case MCSymbolRefExpr::VK_TPOFF:
358218893Sdim    case MCSymbolRefExpr::VK_DTPOFF:
359218893Sdim    case MCSymbolRefExpr::VK_ARM_TLSGD:
360221345Sdim    case MCSymbolRefExpr::VK_ARM_TPOFF:
361221345Sdim    case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
362234353Sdim    case MCSymbolRefExpr::VK_Mips_TLSGD:
363234353Sdim    case MCSymbolRefExpr::VK_Mips_GOTTPREL:
364234353Sdim    case MCSymbolRefExpr::VK_Mips_TPREL_HI:
365234353Sdim    case MCSymbolRefExpr::VK_Mips_TPREL_LO:
366249423Sdim    case MCSymbolRefExpr::VK_PPC_TPREL16_HA:
367249423Sdim    case MCSymbolRefExpr::VK_PPC_TPREL16_LO:
368249423Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL16_HA:
369249423Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL16_LO:
370249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA:
371249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO:
372249423Sdim    case MCSymbolRefExpr::VK_PPC_TLS:
373249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA:
374249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO:
375249423Sdim    case MCSymbolRefExpr::VK_PPC_TLSGD:
376249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA:
377249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO:
378249423Sdim    case MCSymbolRefExpr::VK_PPC_TLSLD:
379218893Sdim      break;
380218893Sdim    }
381218893Sdim    MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
382221345Sdim    MCELF::SetType(SD, ELF::STT_TLS);
383218893Sdim    break;
384218893Sdim  }
385218893Sdim
386218893Sdim  case MCExpr::Unary:
387218893Sdim    fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
388218893Sdim    break;
389218893Sdim  }
390212793Sdim}
391212793Sdim
392218893Sdimvoid MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
393218893Sdim  this->MCObjectStreamer::EmitInstToFragment(Inst);
394249423Sdim  MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
395218893Sdim
396218893Sdim  for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
397218893Sdim    fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
398218893Sdim}
399218893Sdim
400212793Sdimvoid MCELFStreamer::EmitInstToData(const MCInst &Inst) {
401249423Sdim  MCAssembler &Assembler = getAssembler();
402212793Sdim  SmallVector<MCFixup, 4> Fixups;
403212793Sdim  SmallString<256> Code;
404212793Sdim  raw_svector_ostream VecOS(Code);
405249423Sdim  Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
406212793Sdim  VecOS.flush();
407212793Sdim
408218893Sdim  for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
409218893Sdim    fixSymbolsInTLSFixups(Fixups[i].getValue());
410218893Sdim
411249423Sdim  // There are several possibilities here:
412249423Sdim  //
413249423Sdim  // If bundling is disabled, append the encoded instruction to the current data
414249423Sdim  // fragment (or create a new such fragment if the current fragment is not a
415249423Sdim  // data fragment).
416249423Sdim  //
417249423Sdim  // If bundling is enabled:
418249423Sdim  // - If we're not in a bundle-locked group, emit the instruction into a
419249423Sdim  //   fragment of its own. If there are no fixups registered for the
420249423Sdim  //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
421249423Sdim  //   MCDataFragment.
422249423Sdim  // - If we're in a bundle-locked group, append the instruction to the current
423249423Sdim  //   data fragment because we want all the instructions in a group to get into
424249423Sdim  //   the same fragment. Be careful not to do that for the first instruction in
425249423Sdim  //   the group, though.
426249423Sdim  MCDataFragment *DF;
427249423Sdim
428249423Sdim  if (Assembler.isBundlingEnabled()) {
429249423Sdim    MCSectionData *SD = getCurrentSectionData();
430249423Sdim    if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst())
431249423Sdim      // If we are bundle-locked, we re-use the current fragment.
432249423Sdim      // The bundle-locking directive ensures this is a new data fragment.
433249423Sdim      DF = cast<MCDataFragment>(getCurrentFragment());
434249423Sdim    else if (!SD->isBundleLocked() && Fixups.size() == 0) {
435249423Sdim      // Optimize memory usage by emitting the instruction to a
436249423Sdim      // MCCompactEncodedInstFragment when not in a bundle-locked group and
437249423Sdim      // there are no fixups registered.
438251662Sdim      MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
439251662Sdim      insert(CEIF);
440249423Sdim      CEIF->getContents().append(Code.begin(), Code.end());
441249423Sdim      return;
442249423Sdim    } else {
443251662Sdim      DF = new MCDataFragment();
444251662Sdim      insert(DF);
445249423Sdim      if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) {
446249423Sdim        // If this is a new fragment created for a bundle-locked group, and the
447249423Sdim        // group was marked as "align_to_end", set a flag in the fragment.
448249423Sdim        DF->setAlignToBundleEnd(true);
449249423Sdim      }
450249423Sdim    }
451249423Sdim
452249423Sdim    // We're now emitting an instruction in a bundle group, so this flag has
453249423Sdim    // to be turned off.
454249423Sdim    SD->setBundleGroupBeforeFirstInst(false);
455249423Sdim  } else {
456249423Sdim    DF = getOrCreateDataFragment();
457249423Sdim  }
458249423Sdim
459212793Sdim  // Add the fixups and data.
460212793Sdim  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
461212793Sdim    Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
462249423Sdim    DF->getFixups().push_back(Fixups[i]);
463212793Sdim  }
464249423Sdim  DF->setHasInstructions(true);
465212793Sdim  DF->getContents().append(Code.begin(), Code.end());
466212793Sdim}
467212793Sdim
468249423Sdimvoid MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
469249423Sdim  assert(AlignPow2 <= 30 && "Invalid bundle alignment");
470249423Sdim  MCAssembler &Assembler = getAssembler();
471249423Sdim  if (Assembler.getBundleAlignSize() == 0 && AlignPow2 > 0)
472249423Sdim    Assembler.setBundleAlignSize(1 << AlignPow2);
473249423Sdim  else
474249423Sdim    report_fatal_error(".bundle_align_mode should be only set once per file");
475249423Sdim}
476249423Sdim
477249423Sdimvoid MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
478249423Sdim  MCSectionData *SD = getCurrentSectionData();
479249423Sdim
480249423Sdim  // Sanity checks
481249423Sdim  //
482249423Sdim  if (!getAssembler().isBundlingEnabled())
483249423Sdim    report_fatal_error(".bundle_lock forbidden when bundling is disabled");
484249423Sdim  else if (SD->isBundleLocked())
485249423Sdim    report_fatal_error("Nesting of .bundle_lock is forbidden");
486249423Sdim
487249423Sdim  SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd :
488249423Sdim                                      MCSectionData::BundleLocked);
489249423Sdim  SD->setBundleGroupBeforeFirstInst(true);
490249423Sdim}
491249423Sdim
492249423Sdimvoid MCELFStreamer::EmitBundleUnlock() {
493249423Sdim  MCSectionData *SD = getCurrentSectionData();
494249423Sdim
495249423Sdim  // Sanity checks
496249423Sdim  if (!getAssembler().isBundlingEnabled())
497249423Sdim    report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
498249423Sdim  else if (!SD->isBundleLocked())
499249423Sdim    report_fatal_error(".bundle_unlock without matching lock");
500249423Sdim  else if (SD->isBundleGroupBeforeFirstInst())
501249423Sdim    report_fatal_error("Empty bundle-locked group is forbidden");
502249423Sdim
503249423Sdim  SD->setBundleLockState(MCSectionData::NotBundleLocked);
504249423Sdim}
505249423Sdim
506234353Sdimvoid MCELFStreamer::FinishImpl() {
507223017Sdim  EmitFrames(true);
508212793Sdim
509218893Sdim  for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
510218893Sdim                                                e = LocalCommons.end();
511218893Sdim       i != e; ++i) {
512218893Sdim    MCSymbolData *SD = i->SD;
513218893Sdim    uint64_t Size = i->Size;
514218893Sdim    unsigned ByteAlignment = i->ByteAlignment;
515218893Sdim    const MCSymbol &Symbol = SD->getSymbol();
516218893Sdim    const MCSection &Section = Symbol.getSection();
517212793Sdim
518218893Sdim    MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
519218893Sdim    new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
520212793Sdim
521218893Sdim    MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
522218893Sdim    SD->setFragment(F);
523218893Sdim
524218893Sdim    // Update the maximum alignment of the section if necessary.
525218893Sdim    if (ByteAlignment > SectData.getAlignment())
526218893Sdim      SectData.setAlignment(ByteAlignment);
527212793Sdim  }
528212793Sdim
529234353Sdim  this->MCObjectStreamer::FinishImpl();
530212793Sdim}
531249423Sdimvoid MCELFStreamer::EmitTCEntry(const MCSymbol &S) {
532243830Sdim  // Creates a R_PPC64_TOC relocation
533249423Sdim  MCObjectStreamer::EmitSymbolValue(&S, 8);
534243830Sdim}
535243830Sdim
536226633SdimMCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
537218893Sdim                                    raw_ostream &OS, MCCodeEmitter *CE,
538218893Sdim                                    bool RelaxAll, bool NoExecStack) {
539226633Sdim  MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
540212793Sdim  if (RelaxAll)
541212793Sdim    S->getAssembler().setRelaxAll(true);
542218893Sdim  if (NoExecStack)
543218893Sdim    S->getAssembler().setNoExecStack(true);
544212793Sdim  return S;
545212793Sdim}
546249423Sdim
547249423Sdimvoid MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
548249423Sdim  llvm_unreachable("Generic ELF doesn't support this directive");
549249423Sdim}
550249423Sdim
551249423SdimMCSymbolData &MCELFStreamer::getOrCreateSymbolData(MCSymbol *Symbol) {
552249423Sdim  return getAssembler().getOrCreateSymbolData(*Symbol);
553249423Sdim}
554249423Sdim
555249423Sdimvoid MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
556249423Sdim  llvm_unreachable("ELF doesn't support this directive");
557249423Sdim}
558249423Sdim
559249423Sdimvoid MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
560249423Sdim  llvm_unreachable("ELF doesn't support this directive");
561249423Sdim}
562249423Sdim
563249423Sdimvoid MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
564249423Sdim  llvm_unreachable("ELF doesn't support this directive");
565249423Sdim}
566249423Sdim
567249423Sdimvoid MCELFStreamer::EmitCOFFSymbolType(int Type) {
568249423Sdim  llvm_unreachable("ELF doesn't support this directive");
569249423Sdim}
570249423Sdim
571249423Sdimvoid MCELFStreamer::EndCOFFSymbolDef() {
572249423Sdim  llvm_unreachable("ELF doesn't support this directive");
573249423Sdim}
574249423Sdim
575249423Sdimvoid MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
576249423Sdim                                 uint64_t Size, unsigned ByteAlignment) {
577249423Sdim  llvm_unreachable("ELF doesn't support this directive");
578249423Sdim}
579249423Sdim
580249423Sdimvoid MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
581249423Sdim                                   uint64_t Size, unsigned ByteAlignment) {
582249423Sdim  llvm_unreachable("ELF doesn't support this directive");
583249423Sdim}
584