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"
18263508Sdim#include "llvm/MC/MCAsmBackend.h"
19234353Sdim#include "llvm/MC/MCCodeEmitter.h"
20234353Sdim#include "llvm/MC/MCContext.h"
21249423Sdim#include "llvm/MC/MCELF.h"
22212793Sdim#include "llvm/MC/MCELFSymbolFlags.h"
23212793Sdim#include "llvm/MC/MCExpr.h"
24212793Sdim#include "llvm/MC/MCInst.h"
25234353Sdim#include "llvm/MC/MCObjectStreamer.h"
26212793Sdim#include "llvm/MC/MCSection.h"
27249423Sdim#include "llvm/MC/MCSectionELF.h"
28212793Sdim#include "llvm/MC/MCSymbol.h"
29218893Sdim#include "llvm/MC/MCValue.h"
30212793Sdim#include "llvm/Support/Debug.h"
31212793Sdim#include "llvm/Support/ELF.h"
32212793Sdim#include "llvm/Support/ErrorHandling.h"
33212793Sdim#include "llvm/Support/raw_ostream.h"
34212793Sdim
35212793Sdimusing namespace llvm;
36212793Sdim
37234353Sdim
38249423Sdiminline void MCELFStreamer::SetSection(StringRef Section, unsigned Type,
39249423Sdim                                      unsigned Flags, SectionKind Kind) {
40249423Sdim  SwitchSection(getContext().getELFSection(Section, Type, Flags, Kind));
41249423Sdim}
42234353Sdim
43249423Sdiminline void MCELFStreamer::SetSectionData() {
44249423Sdim  SetSection(".data",
45249423Sdim             ELF::SHT_PROGBITS,
46249423Sdim             ELF::SHF_WRITE | ELF::SHF_ALLOC,
47249423Sdim             SectionKind::getDataRel());
48249423Sdim  EmitCodeAlignment(4, 0);
49249423Sdim}
50234353Sdim
51249423Sdiminline void MCELFStreamer::SetSectionText() {
52249423Sdim  SetSection(".text",
53249423Sdim             ELF::SHT_PROGBITS,
54249423Sdim             ELF::SHF_EXECINSTR | ELF::SHF_ALLOC,
55249423Sdim             SectionKind::getText());
56249423Sdim  EmitCodeAlignment(4, 0);
57249423Sdim}
58234353Sdim
59249423Sdiminline void MCELFStreamer::SetSectionBss() {
60249423Sdim  SetSection(".bss",
61249423Sdim             ELF::SHT_NOBITS,
62249423Sdim             ELF::SHF_WRITE | ELF::SHF_ALLOC,
63249423Sdim             SectionKind::getBSS());
64249423Sdim  EmitCodeAlignment(4, 0);
65249423Sdim}
66234353Sdim
67249423SdimMCELFStreamer::~MCELFStreamer() {
68249423Sdim}
69234353Sdim
70249423Sdimvoid MCELFStreamer::InitToTextSection() {
71249423Sdim  SetSectionText();
72234353Sdim}
73234353Sdim
74218893Sdimvoid MCELFStreamer::InitSections() {
75218893Sdim  // This emulates the same behavior of GNU as. This makes it easier
76218893Sdim  // to compare the output as the major sections are in the same order.
77218893Sdim  SetSectionText();
78218893Sdim  SetSectionData();
79218893Sdim  SetSectionBss();
80218893Sdim  SetSectionText();
81218893Sdim}
82218893Sdim
83212793Sdimvoid MCELFStreamer::EmitLabel(MCSymbol *Symbol) {
84212793Sdim  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
85212793Sdim
86218893Sdim  MCObjectStreamer::EmitLabel(Symbol);
87212793Sdim
88218893Sdim  const MCSectionELF &Section =
89218893Sdim    static_cast<const MCSectionELF&>(Symbol->getSection());
90218893Sdim  MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
91218893Sdim  if (Section.getFlags() & ELF::SHF_TLS)
92221345Sdim    MCELF::SetType(SD, ELF::STT_TLS);
93212793Sdim}
94212793Sdim
95249423Sdimvoid MCELFStreamer::EmitDebugLabel(MCSymbol *Symbol) {
96249423Sdim  EmitLabel(Symbol);
97249423Sdim}
98249423Sdim
99212793Sdimvoid MCELFStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
100263508Sdim  // Let the target do whatever target specific stuff it needs to do.
101263508Sdim  getAssembler().getBackend().handleAssemblerFlag(Flag);
102263508Sdim  // Do any generic stuff we need to do.
103212793Sdim  switch (Flag) {
104218893Sdim  case MCAF_SyntaxUnified: return; // no-op here.
105226633Sdim  case MCAF_Code16: return; // Change parsing mode; no-op here.
106226633Sdim  case MCAF_Code32: return; // Change parsing mode; no-op here.
107226633Sdim  case MCAF_Code64: return; // Change parsing mode; no-op here.
108212793Sdim  case MCAF_SubsectionsViaSymbols:
109212793Sdim    getAssembler().setSubsectionsViaSymbols(true);
110212793Sdim    return;
111212793Sdim  }
112212793Sdim
113234353Sdim  llvm_unreachable("invalid assembler flag!");
114212793Sdim}
115212793Sdim
116251662Sdimvoid MCELFStreamer::ChangeSection(const MCSection *Section,
117251662Sdim                                  const MCExpr *Subsection) {
118249423Sdim  MCSectionData *CurSection = getCurrentSectionData();
119249423Sdim  if (CurSection && CurSection->isBundleLocked())
120249423Sdim    report_fatal_error("Unterminated .bundle_lock when changing a section");
121218893Sdim  const MCSymbol *Grp = static_cast<const MCSectionELF *>(Section)->getGroup();
122218893Sdim  if (Grp)
123218893Sdim    getAssembler().getOrCreateSymbolData(*Grp);
124251662Sdim  this->MCObjectStreamer::ChangeSection(Section, Subsection);
125218893Sdim}
126218893Sdim
127218893Sdimvoid MCELFStreamer::EmitWeakReference(MCSymbol *Alias, const MCSymbol *Symbol) {
128218893Sdim  getAssembler().getOrCreateSymbolData(*Symbol);
129218893Sdim  MCSymbolData &AliasSD = getAssembler().getOrCreateSymbolData(*Alias);
130218893Sdim  AliasSD.setFlags(AliasSD.getFlags() | ELF_Other_Weakref);
131218893Sdim  const MCExpr *Value = MCSymbolRefExpr::Create(Symbol, getContext());
132218893Sdim  Alias->setVariableValue(Value);
133218893Sdim}
134218893Sdim
135251662Sdim// When GNU as encounters more than one .type declaration for an object it seems
136251662Sdim// to use a mechanism similar to the one below to decide which type is actually
137251662Sdim// used in the object file.  The greater of T1 and T2 is selected based on the
138251662Sdim// following ordering:
139251662Sdim//  STT_NOTYPE < STT_OBJECT < STT_FUNC < STT_GNU_IFUNC < STT_TLS < anything else
140251662Sdim// If neither T1 < T2 nor T2 < T1 according to this ordering, use T2 (the user
141251662Sdim// provided type).
142251662Sdimstatic unsigned CombineSymbolTypes(unsigned T1, unsigned T2) {
143251662Sdim  unsigned TypeOrdering[] = {ELF::STT_NOTYPE, ELF::STT_OBJECT, ELF::STT_FUNC,
144251662Sdim                             ELF::STT_GNU_IFUNC, ELF::STT_TLS};
145251662Sdim  for (unsigned i = 0; i != array_lengthof(TypeOrdering); ++i) {
146251662Sdim    if (T1 == TypeOrdering[i])
147251662Sdim      return T2;
148251662Sdim    if (T2 == TypeOrdering[i])
149251662Sdim      return T1;
150251662Sdim  }
151251662Sdim
152251662Sdim  return T2;
153251662Sdim}
154251662Sdim
155263508Sdimbool MCELFStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
156263508Sdim                                        MCSymbolAttr Attribute) {
157212793Sdim  // Indirect symbols are handled differently, to match how 'as' handles
158212793Sdim  // them. This makes writing matching .o files easier.
159212793Sdim  if (Attribute == MCSA_IndirectSymbol) {
160212793Sdim    // Note that we intentionally cannot use the symbol data here; this is
161212793Sdim    // important for matching the string table that 'as' generates.
162212793Sdim    IndirectSymbolData ISD;
163212793Sdim    ISD.Symbol = Symbol;
164212793Sdim    ISD.SectionData = getCurrentSectionData();
165212793Sdim    getAssembler().getIndirectSymbols().push_back(ISD);
166263508Sdim    return true;
167212793Sdim  }
168212793Sdim
169212793Sdim  // Adding a symbol attribute always introduces the symbol, note that an
170212793Sdim  // important side effect of calling getOrCreateSymbolData here is to register
171212793Sdim  // the symbol with the assembler.
172212793Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
173212793Sdim
174212793Sdim  // The implementation of symbol attributes is designed to match 'as', but it
175212793Sdim  // leaves much to desired. It doesn't really make sense to arbitrarily add and
176212793Sdim  // remove flags, but 'as' allows this (in particular, see .desc).
177212793Sdim  //
178212793Sdim  // In the future it might be worth trying to make these operations more well
179212793Sdim  // defined.
180212793Sdim  switch (Attribute) {
181212793Sdim  case MCSA_LazyReference:
182212793Sdim  case MCSA_Reference:
183218893Sdim  case MCSA_SymbolResolver:
184212793Sdim  case MCSA_PrivateExtern:
185212793Sdim  case MCSA_WeakDefinition:
186212793Sdim  case MCSA_WeakDefAutoPrivate:
187212793Sdim  case MCSA_Invalid:
188212793Sdim  case MCSA_IndirectSymbol:
189263508Sdim    return false;
190212793Sdim
191243830Sdim  case MCSA_NoDeadStrip:
192218893Sdim  case MCSA_ELF_TypeGnuUniqueObject:
193218893Sdim    // Ignore for now.
194218893Sdim    break;
195218893Sdim
196212793Sdim  case MCSA_Global:
197221345Sdim    MCELF::SetBinding(SD, ELF::STB_GLOBAL);
198212793Sdim    SD.setExternal(true);
199218893Sdim    BindingExplicitlySet.insert(Symbol);
200212793Sdim    break;
201212793Sdim
202212793Sdim  case MCSA_WeakReference:
203212793Sdim  case MCSA_Weak:
204221345Sdim    MCELF::SetBinding(SD, ELF::STB_WEAK);
205218893Sdim    SD.setExternal(true);
206218893Sdim    BindingExplicitlySet.insert(Symbol);
207212793Sdim    break;
208212793Sdim
209212793Sdim  case MCSA_Local:
210221345Sdim    MCELF::SetBinding(SD, ELF::STB_LOCAL);
211218893Sdim    SD.setExternal(false);
212218893Sdim    BindingExplicitlySet.insert(Symbol);
213212793Sdim    break;
214212793Sdim
215212793Sdim  case MCSA_ELF_TypeFunction:
216251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
217251662Sdim                                          ELF::STT_FUNC));
218212793Sdim    break;
219212793Sdim
220234353Sdim  case MCSA_ELF_TypeIndFunction:
221251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
222251662Sdim                                          ELF::STT_GNU_IFUNC));
223234353Sdim    break;
224234353Sdim
225212793Sdim  case MCSA_ELF_TypeObject:
226251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
227251662Sdim                                          ELF::STT_OBJECT));
228212793Sdim    break;
229212793Sdim
230212793Sdim  case MCSA_ELF_TypeTLS:
231251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
232251662Sdim                                          ELF::STT_TLS));
233212793Sdim    break;
234212793Sdim
235212793Sdim  case MCSA_ELF_TypeCommon:
236251662Sdim    // TODO: Emit these as a common symbol.
237251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
238251662Sdim                                          ELF::STT_OBJECT));
239212793Sdim    break;
240212793Sdim
241212793Sdim  case MCSA_ELF_TypeNoType:
242251662Sdim    MCELF::SetType(SD, CombineSymbolTypes(MCELF::GetType(SD),
243251662Sdim                                          ELF::STT_NOTYPE));
244212793Sdim    break;
245212793Sdim
246212793Sdim  case MCSA_Protected:
247221345Sdim    MCELF::SetVisibility(SD, ELF::STV_PROTECTED);
248212793Sdim    break;
249212793Sdim
250212793Sdim  case MCSA_Hidden:
251221345Sdim    MCELF::SetVisibility(SD, ELF::STV_HIDDEN);
252212793Sdim    break;
253212793Sdim
254212793Sdim  case MCSA_Internal:
255221345Sdim    MCELF::SetVisibility(SD, ELF::STV_INTERNAL);
256212793Sdim    break;
257212793Sdim  }
258263508Sdim
259263508Sdim  return true;
260212793Sdim}
261212793Sdim
262212793Sdimvoid MCELFStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
263212793Sdim                                       unsigned ByteAlignment) {
264212793Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
265212793Sdim
266218893Sdim  if (!BindingExplicitlySet.count(Symbol)) {
267221345Sdim    MCELF::SetBinding(SD, ELF::STB_GLOBAL);
268218893Sdim    SD.setExternal(true);
269218893Sdim  }
270218893Sdim
271221345Sdim  MCELF::SetType(SD, ELF::STT_OBJECT);
272218893Sdim
273221345Sdim  if (MCELF::GetBinding(SD) == ELF_STB_Local) {
274212793Sdim    const MCSection *Section = getAssembler().getContext().getELFSection(".bss",
275234353Sdim                                                         ELF::SHT_NOBITS,
276234353Sdim                                                         ELF::SHF_WRITE |
277234353Sdim                                                         ELF::SHF_ALLOC,
278234353Sdim                                                         SectionKind::getBSS());
279212793Sdim
280263508Sdim    AssignSection(Symbol, Section);
281263508Sdim
282218893Sdim    struct LocalCommon L = {&SD, Size, ByteAlignment};
283218893Sdim    LocalCommons.push_back(L);
284218893Sdim  } else {
285218893Sdim    SD.setCommon(Size, ByteAlignment);
286212793Sdim  }
287212793Sdim
288218893Sdim  SD.setSize(MCConstantExpr::Create(Size, getContext()));
289218893Sdim}
290212793Sdim
291249423Sdimvoid MCELFStreamer::EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
292249423Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
293249423Sdim  SD.setSize(Value);
294249423Sdim}
295249423Sdim
296226633Sdimvoid MCELFStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
297226633Sdim                                          unsigned ByteAlignment) {
298218893Sdim  // FIXME: Should this be caught and done earlier?
299218893Sdim  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
300221345Sdim  MCELF::SetBinding(SD, ELF::STB_LOCAL);
301218893Sdim  SD.setExternal(false);
302218893Sdim  BindingExplicitlySet.insert(Symbol);
303226633Sdim  EmitCommonSymbol(Symbol, Size, ByteAlignment);
304212793Sdim}
305212793Sdim
306263508Sdimvoid MCELFStreamer::EmitValueImpl(const MCExpr *Value, unsigned Size) {
307249423Sdim  if (getCurrentSectionData()->isBundleLocked())
308249423Sdim    report_fatal_error("Emitting values inside a locked bundle is forbidden");
309234353Sdim  fixSymbolsInTLSFixups(Value);
310263508Sdim  MCObjectStreamer::EmitValueImpl(Value, Size);
311234353Sdim}
312234353Sdim
313249423Sdimvoid MCELFStreamer::EmitValueToAlignment(unsigned ByteAlignment,
314249423Sdim                                         int64_t Value,
315249423Sdim                                         unsigned ValueSize,
316249423Sdim                                         unsigned MaxBytesToEmit) {
317249423Sdim  if (getCurrentSectionData()->isBundleLocked())
318249423Sdim    report_fatal_error("Emitting values inside a locked bundle is forbidden");
319249423Sdim  MCObjectStreamer::EmitValueToAlignment(ByteAlignment, Value,
320249423Sdim                                         ValueSize, MaxBytesToEmit);
321249423Sdim}
322234353Sdim
323263508Sdim// Add a symbol for the file name of this module. They start after the
324263508Sdim// null symbol and don't count as normal symbol, i.e. a non-STT_FILE symbol
325263508Sdim// with the same name may appear.
326212793Sdimvoid MCELFStreamer::EmitFileDirective(StringRef Filename) {
327263508Sdim  getAssembler().addFileName(Filename);
328263508Sdim}
329212793Sdim
330263508Sdimvoid MCELFStreamer::EmitIdent(StringRef IdentString) {
331263508Sdim  const MCSection *Comment = getAssembler().getContext().getELFSection(
332263508Sdim      ".comment", ELF::SHT_PROGBITS, ELF::SHF_MERGE | ELF::SHF_STRINGS,
333263508Sdim      SectionKind::getReadOnly(), 1, "");
334263508Sdim  PushSection();
335263508Sdim  SwitchSection(Comment);
336263508Sdim  if (!SeenIdent) {
337263508Sdim    EmitIntValue(0, 1);
338263508Sdim    SeenIdent = true;
339263508Sdim  }
340263508Sdim  EmitBytes(IdentString);
341263508Sdim  EmitIntValue(0, 1);
342263508Sdim  PopSection();
343212793Sdim}
344212793Sdim
345263508Sdimvoid MCELFStreamer::fixSymbolsInTLSFixups(const MCExpr *expr) {
346218893Sdim  switch (expr->getKind()) {
347249423Sdim  case MCExpr::Target:
348249423Sdim    cast<MCTargetExpr>(expr)->fixELFSymbolsInTLSFixups(getAssembler());
349249423Sdim    break;
350218893Sdim  case MCExpr::Constant:
351218893Sdim    break;
352212793Sdim
353218893Sdim  case MCExpr::Binary: {
354218893Sdim    const MCBinaryExpr *be = cast<MCBinaryExpr>(expr);
355218893Sdim    fixSymbolsInTLSFixups(be->getLHS());
356218893Sdim    fixSymbolsInTLSFixups(be->getRHS());
357218893Sdim    break;
358218893Sdim  }
359212793Sdim
360218893Sdim  case MCExpr::SymbolRef: {
361218893Sdim    const MCSymbolRefExpr &symRef = *cast<MCSymbolRefExpr>(expr);
362218893Sdim    switch (symRef.getKind()) {
363218893Sdim    default:
364218893Sdim      return;
365221345Sdim    case MCSymbolRefExpr::VK_GOTTPOFF:
366221345Sdim    case MCSymbolRefExpr::VK_INDNTPOFF:
367218893Sdim    case MCSymbolRefExpr::VK_NTPOFF:
368218893Sdim    case MCSymbolRefExpr::VK_GOTNTPOFF:
369218893Sdim    case MCSymbolRefExpr::VK_TLSGD:
370221345Sdim    case MCSymbolRefExpr::VK_TLSLD:
371218893Sdim    case MCSymbolRefExpr::VK_TLSLDM:
372218893Sdim    case MCSymbolRefExpr::VK_TPOFF:
373218893Sdim    case MCSymbolRefExpr::VK_DTPOFF:
374218893Sdim    case MCSymbolRefExpr::VK_ARM_TLSGD:
375221345Sdim    case MCSymbolRefExpr::VK_ARM_TPOFF:
376221345Sdim    case MCSymbolRefExpr::VK_ARM_GOTTPOFF:
377234353Sdim    case MCSymbolRefExpr::VK_Mips_TLSGD:
378234353Sdim    case MCSymbolRefExpr::VK_Mips_GOTTPREL:
379234353Sdim    case MCSymbolRefExpr::VK_Mips_TPREL_HI:
380234353Sdim    case MCSymbolRefExpr::VK_Mips_TPREL_LO:
381263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPMOD:
382263508Sdim    case MCSymbolRefExpr::VK_PPC_TPREL:
383263508Sdim    case MCSymbolRefExpr::VK_PPC_TPREL_LO:
384263508Sdim    case MCSymbolRefExpr::VK_PPC_TPREL_HI:
385263508Sdim    case MCSymbolRefExpr::VK_PPC_TPREL_HA:
386263508Sdim    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHER:
387263508Sdim    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHERA:
388263508Sdim    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHEST:
389263508Sdim    case MCSymbolRefExpr::VK_PPC_TPREL_HIGHESTA:
390263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL:
391263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL_LO:
392263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL_HI:
393263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL_HA:
394263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHER:
395263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHERA:
396263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHEST:
397263508Sdim    case MCSymbolRefExpr::VK_PPC_DTPREL_HIGHESTA:
398263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TPREL:
399263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TPREL_LO:
400263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HI:
401263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TPREL_HA:
402263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_DTPREL:
403263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_LO:
404263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HI:
405263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_DTPREL_HA:
406249423Sdim    case MCSymbolRefExpr::VK_PPC_TLS:
407263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD:
408263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_LO:
409263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HI:
410263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSGD_HA:
411249423Sdim    case MCSymbolRefExpr::VK_PPC_TLSGD:
412263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD:
413263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_LO:
414263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HI:
415263508Sdim    case MCSymbolRefExpr::VK_PPC_GOT_TLSLD_HA:
416249423Sdim    case MCSymbolRefExpr::VK_PPC_TLSLD:
417218893Sdim      break;
418218893Sdim    }
419218893Sdim    MCSymbolData &SD = getAssembler().getOrCreateSymbolData(symRef.getSymbol());
420221345Sdim    MCELF::SetType(SD, ELF::STT_TLS);
421218893Sdim    break;
422218893Sdim  }
423218893Sdim
424218893Sdim  case MCExpr::Unary:
425218893Sdim    fixSymbolsInTLSFixups(cast<MCUnaryExpr>(expr)->getSubExpr());
426218893Sdim    break;
427218893Sdim  }
428212793Sdim}
429212793Sdim
430218893Sdimvoid MCELFStreamer::EmitInstToFragment(const MCInst &Inst) {
431218893Sdim  this->MCObjectStreamer::EmitInstToFragment(Inst);
432249423Sdim  MCRelaxableFragment &F = *cast<MCRelaxableFragment>(getCurrentFragment());
433218893Sdim
434218893Sdim  for (unsigned i = 0, e = F.getFixups().size(); i != e; ++i)
435218893Sdim    fixSymbolsInTLSFixups(F.getFixups()[i].getValue());
436218893Sdim}
437218893Sdim
438212793Sdimvoid MCELFStreamer::EmitInstToData(const MCInst &Inst) {
439249423Sdim  MCAssembler &Assembler = getAssembler();
440212793Sdim  SmallVector<MCFixup, 4> Fixups;
441212793Sdim  SmallString<256> Code;
442212793Sdim  raw_svector_ostream VecOS(Code);
443249423Sdim  Assembler.getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
444212793Sdim  VecOS.flush();
445212793Sdim
446218893Sdim  for (unsigned i = 0, e = Fixups.size(); i != e; ++i)
447218893Sdim    fixSymbolsInTLSFixups(Fixups[i].getValue());
448218893Sdim
449249423Sdim  // There are several possibilities here:
450249423Sdim  //
451249423Sdim  // If bundling is disabled, append the encoded instruction to the current data
452249423Sdim  // fragment (or create a new such fragment if the current fragment is not a
453249423Sdim  // data fragment).
454249423Sdim  //
455249423Sdim  // If bundling is enabled:
456249423Sdim  // - If we're not in a bundle-locked group, emit the instruction into a
457249423Sdim  //   fragment of its own. If there are no fixups registered for the
458249423Sdim  //   instruction, emit a MCCompactEncodedInstFragment. Otherwise, emit a
459249423Sdim  //   MCDataFragment.
460249423Sdim  // - If we're in a bundle-locked group, append the instruction to the current
461249423Sdim  //   data fragment because we want all the instructions in a group to get into
462249423Sdim  //   the same fragment. Be careful not to do that for the first instruction in
463249423Sdim  //   the group, though.
464249423Sdim  MCDataFragment *DF;
465249423Sdim
466249423Sdim  if (Assembler.isBundlingEnabled()) {
467249423Sdim    MCSectionData *SD = getCurrentSectionData();
468249423Sdim    if (SD->isBundleLocked() && !SD->isBundleGroupBeforeFirstInst())
469249423Sdim      // If we are bundle-locked, we re-use the current fragment.
470249423Sdim      // The bundle-locking directive ensures this is a new data fragment.
471249423Sdim      DF = cast<MCDataFragment>(getCurrentFragment());
472249423Sdim    else if (!SD->isBundleLocked() && Fixups.size() == 0) {
473249423Sdim      // Optimize memory usage by emitting the instruction to a
474249423Sdim      // MCCompactEncodedInstFragment when not in a bundle-locked group and
475249423Sdim      // there are no fixups registered.
476251662Sdim      MCCompactEncodedInstFragment *CEIF = new MCCompactEncodedInstFragment();
477251662Sdim      insert(CEIF);
478249423Sdim      CEIF->getContents().append(Code.begin(), Code.end());
479249423Sdim      return;
480249423Sdim    } else {
481251662Sdim      DF = new MCDataFragment();
482251662Sdim      insert(DF);
483249423Sdim      if (SD->getBundleLockState() == MCSectionData::BundleLockedAlignToEnd) {
484249423Sdim        // If this is a new fragment created for a bundle-locked group, and the
485249423Sdim        // group was marked as "align_to_end", set a flag in the fragment.
486249423Sdim        DF->setAlignToBundleEnd(true);
487249423Sdim      }
488249423Sdim    }
489249423Sdim
490249423Sdim    // We're now emitting an instruction in a bundle group, so this flag has
491249423Sdim    // to be turned off.
492249423Sdim    SD->setBundleGroupBeforeFirstInst(false);
493249423Sdim  } else {
494249423Sdim    DF = getOrCreateDataFragment();
495249423Sdim  }
496249423Sdim
497212793Sdim  // Add the fixups and data.
498212793Sdim  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
499212793Sdim    Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
500249423Sdim    DF->getFixups().push_back(Fixups[i]);
501212793Sdim  }
502249423Sdim  DF->setHasInstructions(true);
503212793Sdim  DF->getContents().append(Code.begin(), Code.end());
504212793Sdim}
505212793Sdim
506249423Sdimvoid MCELFStreamer::EmitBundleAlignMode(unsigned AlignPow2) {
507249423Sdim  assert(AlignPow2 <= 30 && "Invalid bundle alignment");
508249423Sdim  MCAssembler &Assembler = getAssembler();
509249423Sdim  if (Assembler.getBundleAlignSize() == 0 && AlignPow2 > 0)
510249423Sdim    Assembler.setBundleAlignSize(1 << AlignPow2);
511249423Sdim  else
512249423Sdim    report_fatal_error(".bundle_align_mode should be only set once per file");
513249423Sdim}
514249423Sdim
515249423Sdimvoid MCELFStreamer::EmitBundleLock(bool AlignToEnd) {
516249423Sdim  MCSectionData *SD = getCurrentSectionData();
517249423Sdim
518249423Sdim  // Sanity checks
519249423Sdim  //
520249423Sdim  if (!getAssembler().isBundlingEnabled())
521249423Sdim    report_fatal_error(".bundle_lock forbidden when bundling is disabled");
522249423Sdim  else if (SD->isBundleLocked())
523249423Sdim    report_fatal_error("Nesting of .bundle_lock is forbidden");
524249423Sdim
525249423Sdim  SD->setBundleLockState(AlignToEnd ? MCSectionData::BundleLockedAlignToEnd :
526249423Sdim                                      MCSectionData::BundleLocked);
527249423Sdim  SD->setBundleGroupBeforeFirstInst(true);
528249423Sdim}
529249423Sdim
530249423Sdimvoid MCELFStreamer::EmitBundleUnlock() {
531249423Sdim  MCSectionData *SD = getCurrentSectionData();
532249423Sdim
533249423Sdim  // Sanity checks
534249423Sdim  if (!getAssembler().isBundlingEnabled())
535249423Sdim    report_fatal_error(".bundle_unlock forbidden when bundling is disabled");
536249423Sdim  else if (!SD->isBundleLocked())
537249423Sdim    report_fatal_error(".bundle_unlock without matching lock");
538249423Sdim  else if (SD->isBundleGroupBeforeFirstInst())
539249423Sdim    report_fatal_error("Empty bundle-locked group is forbidden");
540249423Sdim
541249423Sdim  SD->setBundleLockState(MCSectionData::NotBundleLocked);
542249423Sdim}
543249423Sdim
544263508Sdimvoid MCELFStreamer::Flush() {
545218893Sdim  for (std::vector<LocalCommon>::const_iterator i = LocalCommons.begin(),
546218893Sdim                                                e = LocalCommons.end();
547218893Sdim       i != e; ++i) {
548218893Sdim    MCSymbolData *SD = i->SD;
549218893Sdim    uint64_t Size = i->Size;
550218893Sdim    unsigned ByteAlignment = i->ByteAlignment;
551218893Sdim    const MCSymbol &Symbol = SD->getSymbol();
552218893Sdim    const MCSection &Section = Symbol.getSection();
553212793Sdim
554218893Sdim    MCSectionData &SectData = getAssembler().getOrCreateSectionData(Section);
555218893Sdim    new MCAlignFragment(ByteAlignment, 0, 1, ByteAlignment, &SectData);
556212793Sdim
557218893Sdim    MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
558218893Sdim    SD->setFragment(F);
559218893Sdim
560218893Sdim    // Update the maximum alignment of the section if necessary.
561218893Sdim    if (ByteAlignment > SectData.getAlignment())
562218893Sdim      SectData.setAlignment(ByteAlignment);
563212793Sdim  }
564212793Sdim
565263508Sdim  LocalCommons.clear();
566263508Sdim}
567263508Sdim
568263508Sdimvoid MCELFStreamer::FinishImpl() {
569263508Sdim  EmitFrames(NULL, true);
570263508Sdim
571263508Sdim  Flush();
572263508Sdim
573234353Sdim  this->MCObjectStreamer::FinishImpl();
574212793Sdim}
575243830Sdim
576263508SdimMCStreamer *llvm::createELFStreamer(MCContext &Context,
577263508Sdim                                    MCTargetStreamer *Streamer,
578263508Sdim                                    MCAsmBackend &MAB, raw_ostream &OS,
579263508Sdim                                    MCCodeEmitter *CE, bool RelaxAll,
580263508Sdim                                    bool NoExecStack) {
581263508Sdim  MCELFStreamer *S = new MCELFStreamer(Context, Streamer, MAB, OS, CE);
582212793Sdim  if (RelaxAll)
583212793Sdim    S->getAssembler().setRelaxAll(true);
584218893Sdim  if (NoExecStack)
585218893Sdim    S->getAssembler().setNoExecStack(true);
586212793Sdim  return S;
587212793Sdim}
588249423Sdim
589249423Sdimvoid MCELFStreamer::EmitThumbFunc(MCSymbol *Func) {
590249423Sdim  llvm_unreachable("Generic ELF doesn't support this directive");
591249423Sdim}
592249423Sdim
593249423SdimMCSymbolData &MCELFStreamer::getOrCreateSymbolData(MCSymbol *Symbol) {
594249423Sdim  return getAssembler().getOrCreateSymbolData(*Symbol);
595249423Sdim}
596249423Sdim
597249423Sdimvoid MCELFStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
598249423Sdim  llvm_unreachable("ELF doesn't support this directive");
599249423Sdim}
600249423Sdim
601249423Sdimvoid MCELFStreamer::BeginCOFFSymbolDef(const MCSymbol *Symbol) {
602249423Sdim  llvm_unreachable("ELF doesn't support this directive");
603249423Sdim}
604249423Sdim
605249423Sdimvoid MCELFStreamer::EmitCOFFSymbolStorageClass(int StorageClass) {
606249423Sdim  llvm_unreachable("ELF doesn't support this directive");
607249423Sdim}
608249423Sdim
609249423Sdimvoid MCELFStreamer::EmitCOFFSymbolType(int Type) {
610249423Sdim  llvm_unreachable("ELF doesn't support this directive");
611249423Sdim}
612249423Sdim
613249423Sdimvoid MCELFStreamer::EndCOFFSymbolDef() {
614249423Sdim  llvm_unreachable("ELF doesn't support this directive");
615249423Sdim}
616249423Sdim
617249423Sdimvoid MCELFStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
618249423Sdim                                 uint64_t Size, unsigned ByteAlignment) {
619249423Sdim  llvm_unreachable("ELF doesn't support this directive");
620249423Sdim}
621249423Sdim
622249423Sdimvoid MCELFStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
623249423Sdim                                   uint64_t Size, unsigned ByteAlignment) {
624249423Sdim  llvm_unreachable("ELF doesn't support this directive");
625249423Sdim}
626