1894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===//
2894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
3894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//                     The LLVM Compiler Infrastructure
4894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
5894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This file is distributed under the University of Illinois Open Source
6894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// License. See LICENSE.TXT for details.
7894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//
8894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman//===----------------------------------------------------------------------===//
9894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCStreamer.h"
11894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCAssembler.h"
13894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCContext.h"
14894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCCodeEmitter.h"
15894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCExpr.h"
16894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCInst.h"
17894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCObjectStreamer.h"
18894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCSection.h"
19894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCSymbol.h"
20894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCMachOSymbolFlags.h"
21894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCSectionMachO.h"
22894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/MC/MCDwarf.h"
2319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/MC/MCAsmBackend.h"
2419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman#include "llvm/Support/Dwarf.h"
25894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/ErrorHandling.h"
26894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman#include "llvm/Support/raw_ostream.h"
27894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
28894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanusing namespace llvm;
29894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
30894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumannamespace {
31894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
32894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanclass MCMachOStreamer : public MCObjectStreamer {
33894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanprivate:
3419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  virtual void EmitInstToData(const MCInst &Inst);
35894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
36894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanpublic:
3719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
38894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                  raw_ostream &OS, MCCodeEmitter *Emitter)
3919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    : MCObjectStreamer(Context, MAB, OS, Emitter) {}
40894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
41894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// @name MCStreamer Interface
42894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// @{
43894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
4419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  virtual void InitSections();
45894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitLabel(MCSymbol *Symbol);
4619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
4719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                   MCSymbol *EHSymbol);
48894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
4919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  virtual void EmitThumbFunc(MCSymbol *Func);
50894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
51894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
52894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
53894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
54894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                unsigned ByteAlignment);
55894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
56894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "macho doesn't support this directive");
57894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
58894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
59894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "macho doesn't support this directive");
60894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
61894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitCOFFSymbolType(int Type) {
62894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "macho doesn't support this directive");
63894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
64894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EndCOFFSymbolDef() {
65894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "macho doesn't support this directive");
66894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
67894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
68894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "macho doesn't support this directive");
69894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
7019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
7119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                     unsigned ByteAlignment) {
72894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "macho doesn't support this directive");
73894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
74894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
75894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                            unsigned Size = 0, unsigned ByteAlignment = 0);
76894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
77894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                              uint64_t Size, unsigned ByteAlignment = 0);
78894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
79894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
80894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    unsigned ValueSize = 1,
81894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                    unsigned MaxBytesToEmit = 0);
82894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitCodeAlignment(unsigned ByteAlignment,
83894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                 unsigned MaxBytesToEmit = 0);
84894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
85894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void EmitFileDirective(StringRef Filename) {
86894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: Just ignore the .file; it isn't important enough to fail the
87894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // entire assembly.
88894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
89894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //report_fatal_error("unsupported directive: '.file'");
90894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
91894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
92894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  virtual void Finish();
93894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
94894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  /// @}
95894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman};
96894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
97894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman} // end anonymous namespace.
98894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
9919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid MCMachOStreamer::InitSections() {
10019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SwitchSection(getContext().getMachOSection("__TEXT", "__text",
10119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
10219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                    0, SectionKind::getText()));
103894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
105894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
10619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
10719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman                                          MCSymbol *EHSymbol) {
10819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MCSymbolData &SD =
10919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    getAssembler().getOrCreateSymbolData(*Symbol);
11019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (SD.isExternal())
11119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EmitSymbolAttribute(EHSymbol, MCSA_Global);
11219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (SD.getFlags() & SF_WeakDefinition)
11319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
11419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (SD.isPrivateExtern())
11519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
11619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
117894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
11819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
11919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
12019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
12119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // isSymbolLinkerVisible uses the section.
12219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  Symbol->setSection(*getCurrentSection());
123894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We have to create a new fragment if this is an atom defining symbol,
124894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // fragments cannot span atoms.
12519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  if (getAssembler().isSymbolLinkerVisible(*Symbol))
126894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    new MCDataFragment(getCurrentSectionData());
127894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
12819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MCObjectStreamer::EmitLabel(Symbol);
129894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
13019bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
131894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
132894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // to clear the weak reference and weak definition bits too, but the
133894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // implementation was buggy. For now we just try to match 'as', for
134894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // diffability.
135894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
136894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: Cleanup this code, these bits should be emitted based on semantic
137894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // properties, not on the order of definition, etc.
138894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
139894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
140894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
141894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
14219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Let the target do whatever target specific stuff it needs to do.
14319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getAssembler().getBackend().HandleAssemblerFlag(Flag);
14419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Do any generic stuff we need to do.
145894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Flag) {
14619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MCAF_SyntaxUnified: return; // no-op here.
14719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MCAF_Code16: return; // Change parsing mode; no-op here.
14819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MCAF_Code32: return; // Change parsing mode; no-op here.
14919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MCAF_Code64: return; // Change parsing mode; no-op here.
150894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCAF_SubsectionsViaSymbols:
151894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getAssembler().setSubsectionsViaSymbols(true);
152894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
15319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  default:
15419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    llvm_unreachable("invalid assembler flag!");
155894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
15619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman}
15719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
15819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Baumanvoid MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
15919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // FIXME: Flag the function ISA as thumb with DW_AT_APPLE_isa.
160894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
16119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Remember that the function is a thumb function. Fixup and relocation
16219bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // values will need adjusted.
16319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  getAssembler().setIsThumbFunc(Symbol);
16419bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
16519bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  // Mark the thumb bit on the symbol.
16619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
16719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  SD.setFlags(SD.getFlags() | SF_ThumbFunc);
168894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
169894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
170894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
171894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
172894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // MCObjectStreamer.
173894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: Lift context changes into super class.
174894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getAssembler().getOrCreateSymbolData(*Symbol);
175894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Symbol->setVariableValue(AddValueSymbols(Value));
176894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
177894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
178894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
179894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                          MCSymbolAttr Attribute) {
180894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Indirect symbols are handled differently, to match how 'as' handles
181894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // them. This makes writing matching .o files easier.
182894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (Attribute == MCSA_IndirectSymbol) {
183894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Note that we intentionally cannot use the symbol data here; this is
184894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // important for matching the string table that 'as' generates.
185894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    IndirectSymbolData ISD;
186894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ISD.Symbol = Symbol;
187894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    ISD.SectionData = getCurrentSectionData();
188894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getAssembler().getIndirectSymbols().push_back(ISD);
189894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
190894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
191894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
192894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Adding a symbol attribute always introduces the symbol, note that an
193894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // important side effect of calling getOrCreateSymbolData here is to register
194894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // the symbol with the assembler.
195894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
196894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
197894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The implementation of symbol attributes is designed to match 'as', but it
198894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // leaves much to desired. It doesn't really make sense to arbitrarily add and
199894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // remove flags, but 'as' allows this (in particular, see .desc).
200894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  //
201894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // In the future it might be worth trying to make these operations more well
202894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // defined.
203894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  switch (Attribute) {
204894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_Invalid:
205894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_ELF_TypeFunction:
206894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_ELF_TypeIndFunction:
207894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_ELF_TypeObject:
208894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_ELF_TypeTLS:
209894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_ELF_TypeCommon:
210894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_ELF_TypeNoType:
21119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MCSA_ELF_TypeGnuUniqueObject:
212894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_Hidden:
21319bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MCSA_IndirectSymbol:
214894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_Internal:
215894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_Protected:
216894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_Weak:
217894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_Local:
218894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    assert(0 && "Invalid symbol attribute for Mach-O!");
219894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
220894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
221894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_Global:
222894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SD.setExternal(true);
223894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // This effectively clears the undefined lazy bit, in Darwin 'as', although
224894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // it isn't very consistent because it implements this as part of symbol
225894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // lookup.
226894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    //
227894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: Cleanup this code, these bits should be emitted based on semantic
228894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // properties, not on the order of definition, etc.
229894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
230894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
231894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
232894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_LazyReference:
233894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: This requires -dynamic.
234894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
235894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Symbol->isUndefined())
236894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
237894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
238894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
239894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // Since .reference sets the no dead strip bit, it is equivalent to
240894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // .no_dead_strip in practice.
241894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_Reference:
242894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_NoDeadStrip:
243894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
244894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
245894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
24619bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  case MCSA_SymbolResolver:
24719bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    SD.setFlags(SD.getFlags() | SF_SymbolResolver);
24819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman    break;
24919bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman
250894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_PrivateExtern:
251894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SD.setExternal(true);
252894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SD.setPrivateExtern(true);
253894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
254894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
255894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_WeakReference:
256894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: This requires -dynamic.
257894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (Symbol->isUndefined())
258894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      SD.setFlags(SD.getFlags() | SF_WeakReference);
259894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
260894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
261894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_WeakDefinition:
262894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // FIXME: 'as' enforces that this is defined and global. The manual claims
263894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    // it has to be in a coalesced section, but this isn't enforced.
264894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SD.setFlags(SD.getFlags() | SF_WeakDefinition);
265894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
266894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
267894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  case MCSA_WeakDefAutoPrivate:
268894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
269894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    break;
270894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
271894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
272894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
273894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
274894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Encode the 'desc' value into the lowest implementation defined bits.
275894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(DescValue == (DescValue & SF_DescFlagsMask) &&
276894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         "Invalid .desc value!");
277894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
278894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DescValue & SF_DescFlagsMask);
279894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
280894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
281894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
282894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                       unsigned ByteAlignment) {
283894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
284894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
285894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
286894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
287894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SD.setExternal(true);
288894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SD.setCommon(Size, ByteAlignment);
289894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
290894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
291894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
292894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                   unsigned Size, unsigned ByteAlignment) {
293894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
294894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
295894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // The symbol may not be present, which only creates the section.
296894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (!Symbol)
297894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    return;
298894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
299894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // FIXME: Assert that this section has the zerofill type.
300894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
301894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
302894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
303894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
304894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
305894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Emit an align fragment if necessary.
306894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ByteAlignment != 1)
307894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
308894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
309894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
310894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SD.setFragment(F);
311894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
312894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  Symbol->setSection(*Section);
313894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
314894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Update the maximum alignment on the zero fill section if necessary.
315894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ByteAlignment > SectData.getAlignment())
316894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    SectData.setAlignment(ByteAlignment);
317894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
318894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
319894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// This should always be called with the thread local bss section.  Like the
320894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman// .zerofill directive this doesn't actually switch sections on us.
321894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
322894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                     uint64_t Size, unsigned ByteAlignment) {
323894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  EmitZerofill(Section, Symbol, Size, ByteAlignment);
324894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return;
325894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
326894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
327894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
328894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
329894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // MCObjectStreamer.
330894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
331894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
332894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
333894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
334894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           int64_t Value, unsigned ValueSize,
335894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           unsigned MaxBytesToEmit) {
336894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
337894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // MCObjectStreamer.
338894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MaxBytesToEmit == 0)
339894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MaxBytesToEmit = ByteAlignment;
340894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
341894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                      getCurrentSectionData());
342894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
343894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Update the maximum alignment on the current section if necessary.
344894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ByteAlignment > getCurrentSectionData()->getAlignment())
345894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getCurrentSectionData()->setAlignment(ByteAlignment);
346894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
347894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
348894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
349894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                        unsigned MaxBytesToEmit) {
350894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
351894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // MCObjectStreamer.
352894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (MaxBytesToEmit == 0)
353894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MaxBytesToEmit = ByteAlignment;
354894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
355894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                           getCurrentSectionData());
356894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  F->setEmitNops(true);
357894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
358894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Update the maximum alignment on the current section if necessary.
359894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (ByteAlignment > getCurrentSectionData()->getAlignment())
360894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    getCurrentSectionData()->setAlignment(ByteAlignment);
361894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
362894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
363894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
364894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  MCDataFragment *DF = getOrCreateDataFragment();
365894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
366894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallVector<MCFixup, 4> Fixups;
367894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  SmallString<256> Code;
368894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  raw_svector_ostream VecOS(Code);
369894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
370894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  VecOS.flush();
371894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
372894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Add the fixups and data.
373894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
374894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
375894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    DF->addFixup(Fixups[i]);
376894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
377894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DF->getContents().append(Code.begin(), Code.end());
378894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
379894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
380894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Baumanvoid MCMachOStreamer::Finish() {
38119bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  EmitFrames(true);
382894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
383894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // We have to set the fragment atom associations so we can relax properly for
384894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Mach-O.
385894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
386894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // First, scan the symbol table to build a lookup table from fragments to
387894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // defining symbols.
388894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
389894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
390894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         ie = getAssembler().symbol_end(); it != ie; ++it) {
391894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
392894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        it->getFragment()) {
393894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      // An atom defining symbol should never be internal to a fragment.
394894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
395894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      DefiningSymbolMap[it->getFragment()] = it;
396894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
397894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
398894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
399894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // Set the fragment atom associations by tracking the last seen atom defining
400894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  // symbol.
401894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  for (MCAssembler::iterator it = getAssembler().begin(),
402894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman         ie = getAssembler().end(); it != ie; ++it) {
403894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    MCSymbolData *CurrentAtom = 0;
404894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    for (MCSectionData::iterator it2 = it->begin(),
405894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman           ie2 = it->end(); it2 != ie2; ++it2) {
406894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
407894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman        CurrentAtom = SD;
408894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman      it2->setAtom(CurrentAtom);
409894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    }
410894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  }
411894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
412894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  this->MCObjectStreamer::Finish();
413894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
414894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman
41519bac1e08be200c31efd26f0f5fd144c9b3eefd3John BaumanMCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
416894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      raw_ostream &OS, MCCodeEmitter *CE,
417894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman                                      bool RelaxAll) {
41819bac1e08be200c31efd26f0f5fd144c9b3eefd3John Bauman  MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
419894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  if (RelaxAll)
420894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman    S->getAssembler().setRelaxAll(true);
421894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman  return S;
422894018228b0e0bdbd7aa7e8f47d4a9458789ca82John Bauman}
423