MCELFStreamer.cpp revision 249423
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"
16234353Sdim#include "llvm/MC/MCAssembler.h"
17234353Sdim#include "llvm/MC/MCCodeEmitter.h"
18234353Sdim#include "llvm/MC/MCContext.h"
19249423Sdim#include "llvm/MC/MCELF.h"
20212793Sdim#include "llvm/MC/MCELFSymbolFlags.h"
21212793Sdim#include "llvm/MC/MCExpr.h"
22212793Sdim#include "llvm/MC/MCInst.h"
23234353Sdim#include "llvm/MC/MCObjectStreamer.h"
24212793Sdim#include "llvm/MC/MCSection.h"
25249423Sdim#include "llvm/MC/MCSectionELF.h"
26212793Sdim#include "llvm/MC/MCSymbol.h"
27218893Sdim#include "llvm/MC/MCValue.h"
28212793Sdim#include "llvm/Support/Debug.h"
29212793Sdim#include "llvm/Support/ELF.h"
30212793Sdim#include "llvm/Support/ErrorHandling.h"
31212793Sdim#include "llvm/Support/raw_ostream.h"
32212793Sdim
33212793Sdimusing namespace llvm;
34212793Sdim
35234353Sdim
36249423Sdiminline void MCELFStreamer::SetSection(StringRef Section, unsigned Type,
37249423Sdim                                      unsigned Flags, SectionKind Kind) {
38249423Sdim  SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
39249423Sdim}
40234353Sdim
41249423Sdiminline void MCELFStreamer::SetSectionData() {
42249423Sdim  SetSection(".data",
43249423Sdim             ELF::SHT_PROGBITS,
44249423Sdim             ELF::SHF_WRITE | ELF::SHF_ALLOC,
45249423Sdim             SectionKind::getDataRel());
46249423Sdim  EmitCodeAlignment(4, 0);
47249423Sdim}
48234353Sdim
49249423Sdiminline void MCELFStreamer::SetSectionText() {
50249423Sdim  SetSection(".text",
51249423Sdim             ELF::SHT_PROGBITS,
52249423Sdim             ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
53249423Sdim             SectionKind::getText());
54249423Sdim  EmitCodeAlignment(4, 0);
55249423Sdim}
56234353Sdim
57249423Sdiminline void MCELFStreamer::SetSectionBss() {
58249423Sdim  SetSection(".bss",
59249423Sdim             ELF::SHT_NOBITS,
60249423Sdim             ELF::SHF_WRITE | ELF::SHF_ALLOC,
61249423Sdim             SectionKind::getBSS());
62249423Sdim  EmitCodeAlignment(4, 0);
63249423Sdim}
64234353Sdim
65249423SdimMCELFStreamer::~MCELFStreamer() {
66249423Sdim}
67234353Sdim
68249423Sdimvoid MCELFStreamer::InitToTextSection() {
69249423Sdim  SetSectionText();
70234353Sdim}
71234353Sdim
72218893Sdimvoid MCELFStreamer::InitSections() {
73218893Sdim  // This emulates the same behavior of GNU as. This makes it easier
74218893Sdim  // to compare the output as the major sections are in the same order.
75218893Sdim  SetSectionText();
76218893Sdim  SetSectionData();
77218893Sdim  SetSectionBss();
78218893Sdim  SetSectionText();
79218893Sdim}
80218893Sdim
81212793Sdimvoid MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
82212793Sdim  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
83212793Sdim
84218893Sdim  MCObjectStreamer::EmitLabel(Symbol);
85212793Sdim
86218893Sdim  const MCSectionELF &Section =
87218893Sdim    static_cast<const MCSectionELF&>(Symbol->getSection());
88218893Sdim  MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
89218893Sdim  if (Section.getFlags() & ELF::SHF_TLS)
90221345Sdim    MCELF::SetType(SD, ELF::STT_TLS);
91212793Sdim}
92212793Sdim
93249423Sdimvoid MCELFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
94249423Sdim  EmitLabel(Symbol);
95249423Sdim}
96249423Sdim
97212793Sdimvoid MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
98212793Sdim  switch (Flag) {
99218893Sdim  case MCAF_SyntaxUnified: return; // no-op here.
100226633Sdim  case MCAF_Code16: return; // Change parsing mode; no-op here.
101226633Sdim  case MCAF_Code32: return; // Change parsing mode; no-op here.
102226633Sdim  case MCAF_Code64: return; // Change parsing mode; no-op here.
103212793Sdim  case MCAF_SubsectionsViaSymbols:
104212793Sdim    getAssembler().setSubsectionsViaSymbols(true);
105212793Sdim    return;
106212793Sdim  }
107212793Sdim
108234353Sdim  llvm_unreachable("invalid assembler flag!");
109212793Sdim}
110212793Sdim
111218893Sdimvoid MCELFStreamer::ChangeSection(const MCSection *Section) {
112249423Sdim  MCSectionData *CurSection = getCurrentSectionData();
113249423Sdim  if (CurSection && CurSection->isBundleLocked())
114249423Sdim    report_fatal_error("Unterminated .bundle_lock when changing a section");
115218893Sdim  const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
116218893Sdim  if (Grp)
117218893Sdim    getAssembler().getOrCreateSymbolData(*Grp);
118218893Sdim  this->MCObjectStreamer::ChangeSection(Section);
119218893Sdim}
120218893Sdim
121218893Sdimvoid MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
122218893Sdim  getAssembler().getOrCreateSymbolData(*Symbol);
123218893Sdim  MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
124218893Sdim  AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
125218893Sdim  const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
126218893Sdim  Alias->setVariableValue(Value);
127218893Sdim}
128218893Sdim
129212793Sdimvoid MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
130212793Sdim                                          MCSymbolAttr Attribute) {
131212793Sdim  // Indirect symbols are handled differently, to match how 'as' handles
132212793Sdim  // them. This makes writing matching .o files easier.
133212793Sdim  if (Attribute == MCSA_IndirectSymbol) {
134212793Sdim    // Note that we intentionally cannot use the symbol data here; this is
135212793Sdim    // important for matching the string table that 'as' generates.
136212793Sdim    IndirectSymbolData ISD;
137212793Sdim    ISD.Symbol = Symbol;
138212793Sdim    ISD.SectionData = getCurrentSectionData();
139212793Sdim    getAssembler().getIndirectSymbols().push_back(ISD);
140212793Sdim    return;
141212793Sdim  }
142212793Sdim
143212793Sdim  // Adding a symbol attribute always introduces the symbol, note that an
144212793Sdim  // important side effect of calling getOrCreateSymbolData here is to register
145212793Sdim  // the symbol with the assembler.
146212793Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
147212793Sdim
148212793Sdim  // The implementation of symbol attributes is designed to match 'as', but it
149212793Sdim  // leaves much to desired. It doesn't really make sense to arbitrarily add and
150212793Sdim  // remove flags, but 'as' allows this (in particular, see .desc).
151212793Sdim  //
152212793Sdim  // In the future it might be worth trying to make these operations more well
153212793Sdim  // defined.
154212793Sdim  switch (Attribute) {
155212793Sdim  case MCSA_LazyReference:
156212793Sdim  case MCSA_Reference:
157218893Sdim  case MCSA_SymbolResolver:
158212793Sdim  case MCSA_PrivateExtern:
159212793Sdim  case MCSA_WeakDefinition:
160212793Sdim  case MCSA_WeakDefAutoPrivate:
161212793Sdim  case MCSA_Invalid:
162212793Sdim  case MCSA_IndirectSymbol:
163234353Sdim    llvm_unreachable("Invalid symbol attribute for ELF!");
164212793Sdim
165243830Sdim  case MCSA_NoDeadStrip:
166218893Sdim  case MCSA_ELF_TypeGnuUniqueObject:
167218893Sdim    // Ignore for now.
168218893Sdim    break;
169218893Sdim
170212793Sdim  case MCSA_Global:
171221345Sdim    MCELF::SetBinding(SD, ELF::STB_GLOBAL);
172212793Sdim    SD.setExternal(true);
173218893Sdim    BindingExplicitlySet.insert(Symbol);
174212793Sdim    break;
175212793Sdim
176212793Sdim  case MCSA_WeakReference:
177212793Sdim  case MCSA_Weak:
178221345Sdim    MCELF::SetBinding(SD, ELF::STB_WEAK);
179218893Sdim    SD.setExternal(true);
180218893Sdim    BindingExplicitlySet.insert(Symbol);
181212793Sdim    break;
182212793Sdim
183212793Sdim  case MCSA_Local:
184221345Sdim    MCELF::SetBinding(SD, ELF::STB_LOCAL);
185218893Sdim    SD.setExternal(false);
186218893Sdim    BindingExplicitlySet.insert(Symbol);
187212793Sdim    break;
188212793Sdim
189212793Sdim  case MCSA_ELF_TypeFunction:
190221345Sdim    MCELF::SetType(SD, ELF::STT_FUNC);
191212793Sdim    break;
192212793Sdim
193234353Sdim  case MCSA_ELF_TypeIndFunction:
194234353Sdim    MCELF::SetType(SD, ELF::STT_GNU_IFUNC);
195234353Sdim    break;
196234353Sdim
197212793Sdim  case MCSA_ELF_TypeObject:
198221345Sdim    MCELF::SetType(SD, ELF::STT_OBJECT);
199212793Sdim    break;
200212793Sdim
201212793Sdim  case MCSA_ELF_TypeTLS:
202221345Sdim    MCELF::SetType(SD, ELF::STT_TLS);
203212793Sdim    break;
204212793Sdim
205212793Sdim  case MCSA_ELF_TypeCommon:
206221345Sdim    MCELF::SetType(SD, ELF::STT_COMMON);
207212793Sdim    break;
208212793Sdim
209212793Sdim  case MCSA_ELF_TypeNoType:
210221345Sdim    MCELF::SetType(SD, ELF::STT_NOTYPE);
211212793Sdim    break;
212212793Sdim
213212793Sdim  case MCSA_Protected:
214221345Sdim    MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
215212793Sdim    break;
216212793Sdim
217212793Sdim  case MCSA_Hidden:
218221345Sdim    MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
219212793Sdim    break;
220212793Sdim
221212793Sdim  case MCSA_Internal:
222221345Sdim    MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
223212793Sdim    break;
224212793Sdim  }
225212793Sdim}
226212793Sdim
227212793Sdimvoid MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
228212793Sdim                                       unsigned ByteAlignment) {
229212793Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
230212793Sdim
231218893Sdim  if (!BindingExplicitlySet.count(Symbol)) {
232221345Sdim    MCELF::SetBinding(SD, ELF::STB_GLOBAL);
233218893Sdim    SD.setExternal(true);
234218893Sdim  }
235218893Sdim
236221345Sdim  MCELF::SetType(SD, ELF::STT_OBJECT);
237218893Sdim
238221345Sdim  if (MCELF::GetBinding(SD) == ELF_STB_Local) {
239212793Sdim    const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
240234353Sdim                                                         ELF::SHT_NOBITS,
241234353Sdim                                                         ELF::SHF_WRITE |
242234353Sdim                                                         ELF::SHF_ALLOC,
243234353Sdim                                                         SectionKind::getBSS());
244218893Sdim    Symbol->setSection(*Section);
245212793Sdim
246218893Sdim    struct LocalCommon L = {&SD, Size, ByteAlignment};
247218893Sdim    LocalCommons.push_back(L);
248218893Sdim  } else {
249218893Sdim    SD.setCommon(Size, ByteAlignment);
250212793Sdim  }
251212793Sdim
252218893Sdim  SD.setSize(MCConstantExpr::Create(Size, getContext()));
253218893Sdim}
254212793Sdim
255249423Sdimvoid MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
256249423Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
257249423Sdim  SD.setSize(Value);
258249423Sdim}
259249423Sdim
260226633Sdimvoid MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
261226633Sdim                                          unsigned ByteAlignment) {
262218893Sdim  // FIXME: Should this be caught and done earlier?
263218893Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
264221345Sdim  MCELF::SetBinding(SD, ELF::STB_LOCAL);
265218893Sdim  SD.setExternal(false);
266218893Sdim  BindingExplicitlySet.insert(Symbol);
267226633Sdim  EmitCommonSymbol(Symbol, Size, ByteAlignment);
268212793Sdim}
269212793Sdim
270234353Sdimvoid MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size,
271234353Sdim                                  unsigned AddrSpace) {
272249423Sdim  if (getCurrentSectionData()->isBundleLocked())
273249423Sdim    report_fatal_error("Emitting values inside a locked bundle is forbidden");
274234353Sdim  fixSymbolsInTLSFixups(Value);
275234353Sdim  MCObjectStreamer::EmitValueImpl(Value, Size, AddrSpace);
276234353Sdim}
277234353Sdim
278249423Sdimvoid MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
279249423Sdim                                         int64_t Value,
280249423Sdim                                         unsigned ValueSize,
281249423Sdim                                         unsigned MaxBytesToEmit) {
282249423Sdim  if (getCurrentSectionData()->isBundleLocked())
283249423Sdim    report_fatal_error("Emitting values inside a locked bundle is forbidden");
284249423Sdim  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
285249423Sdim                                         ValueSize, MaxBytesToEmit);
286249423Sdim}
287234353Sdim
288249423Sdim
289212793Sdim// Add a symbol for the file name of this module. This is the second
290212793Sdim// entry in the module's symbol table (the first being the null symbol).
291212793Sdimvoid MCELFStreamer::EmitFileDirective(StringRef Filename) {
292212793Sdim  MCSymbol *Symbol = getAssembler().getContext().GetOrCreateSymbol(Filename);
293218893Sdim  Symbol->setSection(*getCurrentSection());
294212793Sdim  Symbol->setAbsolute();
295212793Sdim
296212793Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
297212793Sdim
298212793Sdim  SD.setFlags(ELF_STT_File | ELF_STB_Local | ELF_STV_Default);
299212793Sdim}
300212793Sdim
301218893Sdimvoid  MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
302218893Sdim  switch (expr->getKind()) {
303249423Sdim  case MCExpr::Target:
304249423Sdim    cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
305249423Sdim    break;
306218893Sdim  case MCExpr::Constant:
307218893Sdim    break;
308212793Sdim
309218893Sdim  case MCExpr::Binary: {
310218893Sdim    const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
311218893Sdim    fixSymbolsInTLSFixups(be->getLHS());
312218893Sdim    fixSymbolsInTLSFixups(be->getRHS());
313218893Sdim    break;
314218893Sdim  }
315212793Sdim
316218893Sdim  case MCExpr::SymbolRef: {
317218893Sdim    const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
318218893Sdim    switch (symRef.getKind()) {
319218893Sdim    default:
320218893Sdim      return;
321221345Sdim    case MCSymbolRefExpr::VK_GOTTPOFF:
322221345Sdim    case MCSymbolRefExpr::VK_INDNTPOFF:
323218893Sdim    case MCSymbolRefExpr::VK_NTPOFF:
324218893Sdim    case MCSymbolRefExpr::VK_GOTNTPOFF:
325218893Sdim    case MCSymbolRefExpr::VK_TLSGD:
326221345Sdim    case MCSymbolRefExpr::VK_TLSLD:
327218893Sdim    case MCSymbolRefExpr::VK_TLSLDM:
328218893Sdim    case MCSymbolRefExpr::VK_TPOFF:
329218893Sdim    case MCSymbolRefExpr::VK_DTPOFF:
330218893Sdim    case MCSymbolRefExpr::VK_ARM_TLSGD:
331221345Sdim    case MCSymbolRefExpr::VK_ARM_TPOFF:
332221345Sdim    case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
333234353Sdim    case MCSymbolRefExpr::VK_Mips_TLSGD:
334234353Sdim    case MCSymbolRefExpr::VK_Mips_GOTTPREL:
335234353Sdim    case MCSymbolRefExpr::VK_Mips_TPREL_HI:
336234353Sdim    case MCSymbolRefExpr::VK_Mips_TPREL_LO:
337249423Sdim    case MCSymbolRefExpr::VK_PPC_TPREL16_HA:
338249423Sdim    case MCSymbolRefExpr::VK_PPC_TPREL16_LO:
339249423Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL16_HA:
340249423Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL16_LO:
341249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_HA:
342249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TPREL16_LO:
343249423Sdim    case MCSymbolRefExpr::VK_PPC_TLS:
344249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_HA:
345249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD16_LO:
346249423Sdim    case MCSymbolRefExpr::VK_PPC_TLSGD:
347249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_HA:
348249423Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD16_LO:
349249423Sdim    case MCSymbolRefExpr::VK_PPC_TLSLD:
350218893Sdim      break;
351218893Sdim    }
352218893Sdim    MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
353221345Sdim    MCELF::SetType(SD, ELF::STT_TLS);
354218893Sdim    break;
355218893Sdim  }
356218893Sdim
357218893Sdim  case MCExpr::Unary:
358218893Sdim    fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
359218893Sdim    break;
360218893Sdim  }
361212793Sdim}
362212793Sdim
363218893Sdimvoid MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
364218893Sdim  this->MCObjectStreamer::EmitInstToFragment(Inst);
365249423Sdim  MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
366218893Sdim
367218893Sdim  for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
368218893Sdim    fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
369218893Sdim}
370218893Sdim
371212793Sdimvoid MCELFStreamer::EmitInstToData(const MCInst &Inst) {
372249423Sdim  MCAssembler &Assembler = getAssembler();
373212793Sdim  SmallVector<MCFixup, 4> Fixups;
374212793Sdim  SmallString<256> Code;
375212793Sdim  raw_svector_ostream VecOS(Code);
376249423Sdim  Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
377212793Sdim  VecOS.flush();
378212793Sdim
379218893Sdim  for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
380218893Sdim    fixSymbolsInTLSFixups(Fixups[i].getValue());
381218893Sdim
382249423Sdim  // There are several possibilities here:
383249423Sdim  //
384249423Sdim  // If bundling is disabled, append the encoded instruction to the current data
385249423Sdim  // fragment (or create a new such fragment if the current fragment is not a
386249423Sdim  // data fragment).
387249423Sdim  //
388249423Sdim  // If bundling is enabled:
389249423Sdim  // - If we're not in a bundle-locked group, emit the instruction into a
390249423Sdim  //   fragment of its own. If there are no fixups registered for the
391249423Sdim  //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
392249423Sdim  //   MCDataFragment.
393249423Sdim  // - If we're in a bundle-locked group, append the instruction to the current
394249423Sdim  //   data fragment because we want all the instructions in a group to get into
395249423Sdim  //   the same fragment. Be careful not to do that for the first instruction in
396249423Sdim  //   the group, though.
397249423Sdim  MCDataFragment *DF;
398249423Sdim
399249423Sdim  if (Assembler.isBundlingEnabled()) {
400249423Sdim    MCSectionData *SD = getCurrentSectionData();
401249423Sdim    if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst())
402249423Sdim      // If we are bundle-locked, we re-use the current fragment.
403249423Sdim      // The bundle-locking directive ensures this is a new data fragment.
404249423Sdim      DF = cast<MCDataFragment>(getCurrentFragment());
405249423Sdim    else if (!SD->isBundleLocked() && Fixups.size() == 0) {
406249423Sdim      // Optimize memory usage by emitting the instruction to a
407249423Sdim      // MCCompactEncodedInstFragment when not in a bundle-locked group and
408249423Sdim      // there are no fixups registered.
409249423Sdim      MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment(SD);
410249423Sdim      CEIF->getContents().append(Code.begin(), Code.end());
411249423Sdim      return;
412249423Sdim    } else {
413249423Sdim      DF = new MCDataFragment(SD);
414249423Sdim      if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) {
415249423Sdim        // If this is a new fragment created for a bundle-locked group, and the
416249423Sdim        // group was marked as "align_to_end", set a flag in the fragment.
417249423Sdim        DF->setAlignToBundleEnd(true);
418249423Sdim      }
419249423Sdim    }
420249423Sdim
421249423Sdim    // We're now emitting an instruction in a bundle group, so this flag has
422249423Sdim    // to be turned off.
423249423Sdim    SD->setBundleGroupBeforeFirstInst(false);
424249423Sdim  } else {
425249423Sdim    DF = getOrCreateDataFragment();
426249423Sdim  }
427249423Sdim
428212793Sdim  // Add the fixups and data.
429212793Sdim  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
430212793Sdim    Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
431249423Sdim    DF->getFixups().push_back(Fixups[i]);
432212793Sdim  }
433249423Sdim  DF->setHasInstructions(true);
434212793Sdim  DF->getContents().append(Code.begin(), Code.end());
435212793Sdim}
436212793Sdim
437249423Sdimvoid MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
438249423Sdim  assert(AlignPow2 <= 30 && "Invalid bundle alignment");
439249423Sdim  MCAssembler &Assembler = getAssembler();
440249423Sdim  if (Assembler.getBundleAlignSize() == 0 && AlignPow2 > 0)
441249423Sdim    Assembler.setBundleAlignSize(1 << AlignPow2);
442249423Sdim  else
443249423Sdim    report_fatal_error(".bundle_align_mode should be only set once per file");
444249423Sdim}
445249423Sdim
446249423Sdimvoid MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
447249423Sdim  MCSectionData *SD = getCurrentSectionData();
448249423Sdim
449249423Sdim  // Sanity checks
450249423Sdim  //
451249423Sdim  if (!getAssembler().isBundlingEnabled())
452249423Sdim    report_fatal_error(".bundle_lock forbidden when bundling is disabled");
453249423Sdim  else if (SD->isBundleLocked())
454249423Sdim    report_fatal_error("Nesting of .bundle_lock is forbidden");
455249423Sdim
456249423Sdim  SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd :
457249423Sdim                                      MCSectionData::BundleLocked);
458249423Sdim  SD->setBundleGroupBeforeFirstInst(true);
459249423Sdim}
460249423Sdim
461249423Sdimvoid MCELFStreamer::EmitBundleUnlock() {
462249423Sdim  MCSectionData *SD = getCurrentSectionData();
463249423Sdim
464249423Sdim  // Sanity checks
465249423Sdim  if (!getAssembler().isBundlingEnabled())
466249423Sdim    report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
467249423Sdim  else if (!SD->isBundleLocked())
468249423Sdim    report_fatal_error(".bundle_unlock without matching lock");
469249423Sdim  else if (SD->isBundleGroupBeforeFirstInst())
470249423Sdim    report_fatal_error("Empty bundle-locked group is forbidden");
471249423Sdim
472249423Sdim  SD->setBundleLockState(MCSectionData::NotBundleLocked);
473249423Sdim}
474249423Sdim
475234353Sdimvoid MCELFStreamer::FinishImpl() {
476223017Sdim  EmitFrames(true);
477212793Sdim
478218893Sdim  for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
479218893Sdim                                                e = LocalCommons.end();
480218893Sdim       i != e; ++i) {
481218893Sdim    MCSymbolData *SD = i->SD;
482218893Sdim    uint64_t Size = i->Size;
483218893Sdim    unsigned ByteAlignment = i->ByteAlignment;
484218893Sdim    const MCSymbol &Symbol = SD->getSymbol();
485218893Sdim    const MCSection &Section = Symbol.getSection();
486212793Sdim
487218893Sdim    MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
488218893Sdim    new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
489212793Sdim
490218893Sdim    MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
491218893Sdim    SD->setFragment(F);
492218893Sdim
493218893Sdim    // Update the maximum alignment of the section if necessary.
494218893Sdim    if (ByteAlignment > SectData.getAlignment())
495218893Sdim      SectData.setAlignment(ByteAlignment);
496212793Sdim  }
497212793Sdim
498234353Sdim  this->MCObjectStreamer::FinishImpl();
499212793Sdim}
500249423Sdimvoid MCELFStreamer::EmitTCEntry(const MCSymbol &S) {
501243830Sdim  // Creates a R_PPC64_TOC relocation
502249423Sdim  MCObjectStreamer::EmitSymbolValue(&S, 8);
503243830Sdim}
504243830Sdim
505226633SdimMCStreamer *llvm::createELFStreamer(MCContext &Context, MCAsmBackend &MAB,
506218893Sdim                                    raw_ostream &OS, MCCodeEmitter *CE,
507218893Sdim                                    bool RelaxAll, bool NoExecStack) {
508226633Sdim  MCELFStreamer *S = new MCELFStreamer(Context, MAB, OS, CE);
509212793Sdim  if (RelaxAll)
510212793Sdim    S->getAssembler().setRelaxAll(true);
511218893Sdim  if (NoExecStack)
512218893Sdim    S->getAssembler().setNoExecStack(true);
513212793Sdim  return S;
514212793Sdim}
515249423Sdim
516249423Sdimvoid MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
517249423Sdim  llvm_unreachable("Generic ELF doesn't support this directive");
518249423Sdim}
519249423Sdim
520249423SdimMCSymbolData &MCELFStreamer::getOrCreateSymbolData(MCSymbol *Symbol) {
521249423Sdim  return getAssembler().getOrCreateSymbolData(*Symbol);
522249423Sdim}
523249423Sdim
524249423Sdimvoid MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
525249423Sdim  llvm_unreachable("ELF doesn't support this directive");
526249423Sdim}
527249423Sdim
528249423Sdimvoid MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
529249423Sdim  llvm_unreachable("ELF doesn't support this directive");
530249423Sdim}
531249423Sdim
532249423Sdimvoid MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
533249423Sdim  llvm_unreachable("ELF doesn't support this directive");
534249423Sdim}
535249423Sdim
536249423Sdimvoid MCELFStreamer::EmitCOFFSymbolType(int Type) {
537249423Sdim  llvm_unreachable("ELF doesn't support this directive");
538249423Sdim}
539249423Sdim
540249423Sdimvoid MCELFStreamer::EndCOFFSymbolDef() {
541249423Sdim  llvm_unreachable("ELF doesn't support this directive");
542249423Sdim}
543249423Sdim
544249423Sdimvoid MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
545249423Sdim                                 uint64_t Size, unsigned ByteAlignment) {
546249423Sdim  llvm_unreachable("ELF doesn't support this directive");
547249423Sdim}
548249423Sdim
549249423Sdimvoid MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
550249423Sdim                                   uint64_t Size, unsigned ByteAlignment) {
551249423Sdim  llvm_unreachable("ELF doesn't support this directive");
552249423Sdim}
553