MCMachOStreamer.cpp revision 202878
166174Sbsd//===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===//
266174Sbsd//
366174Sbsd//                     The LLVM Compiler Infrastructure
466174Sbsd//
566174Sbsd// This file is distributed under the University of Illinois Open Source
666174Sbsd// License. See LICENSE.TXT for details.
766174Sbsd//
866174Sbsd//===----------------------------------------------------------------------===//
966174Sbsd
1066174Sbsd#include "llvm/MC/MCStreamer.h"
1166174Sbsd
1266174Sbsd#include "llvm/MC/MCAssembler.h"
1366174Sbsd#include "llvm/MC/MCContext.h"
1466174Sbsd#include "llvm/MC/MCCodeEmitter.h"
1566174Sbsd#include "llvm/MC/MCExpr.h"
1666174Sbsd#include "llvm/MC/MCInst.h"
1766174Sbsd#include "llvm/MC/MCSection.h"
1866174Sbsd#include "llvm/MC/MCSymbol.h"
1966174Sbsd#include "llvm/MC/MCValue.h"
2066174Sbsd#include "llvm/Support/ErrorHandling.h"
2166174Sbsd#include "llvm/Support/raw_ostream.h"
2266174Sbsdusing namespace llvm;
2366174Sbsd
2466174Sbsdnamespace {
2566174Sbsd
2666174Sbsdclass MCMachOStreamer : public MCStreamer {
2766174Sbsd  /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
2866174Sbsd  /// 16 bits of the implementation defined flags.
2992986Sobrien  enum SymbolFlags { // See <mach-o/nlist.h>.
3092986Sobrien    SF_DescFlagsMask                        = 0xFFFF,
3166174Sbsd
3266174Sbsd    // Reference type flags.
3366174Sbsd    SF_ReferenceTypeMask                    = 0x0007,
3466174Sbsd    SF_ReferenceTypeUndefinedNonLazy        = 0x0000,
3566174Sbsd    SF_ReferenceTypeUndefinedLazy           = 0x0001,
3666174Sbsd    SF_ReferenceTypeDefined                 = 0x0002,
3766174Sbsd    SF_ReferenceTypePrivateDefined          = 0x0003,
3866174Sbsd    SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
3966174Sbsd    SF_ReferenceTypePrivateUndefinedLazy    = 0x0005,
4066174Sbsd
4166174Sbsd    // Other 'desc' flags.
4266174Sbsd    SF_NoDeadStrip                          = 0x0020,
4366174Sbsd    SF_WeakReference                        = 0x0040,
44105604Ssam    SF_WeakDefinition                       = 0x0080
4566174Sbsd  };
4666174Sbsd
4766174Sbsdprivate:
4866174Sbsd  MCAssembler Assembler;
4966174Sbsd
5066174Sbsd  MCCodeEmitter *Emitter;
5166174Sbsd
5266174Sbsd  MCSectionData *CurSectionData;
5366174Sbsd
5466174Sbsd  DenseMap<const MCSection*, MCSectionData*> SectionMap;
5566174Sbsd
5666174Sbsd  DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
5766174Sbsd
5866174Sbsdprivate:
5966174Sbsd  MCFragment *getCurrentFragment() const {
6066174Sbsd    assert(CurSectionData && "No current section!");
6166174Sbsd
6266174Sbsd    if (!CurSectionData->empty())
6366174Sbsd      return &CurSectionData->getFragmentList().back();
6466174Sbsd
6566174Sbsd    return 0;
6666174Sbsd  }
6766174Sbsd
6866174Sbsd  MCSectionData &getSectionData(const MCSection &Section) {
6966174Sbsd    MCSectionData *&Entry = SectionMap[&Section];
7066174Sbsd
7166174Sbsd    if (!Entry)
7266174Sbsd      Entry = new MCSectionData(Section, &Assembler);
7366174Sbsd
7466174Sbsd    return *Entry;
75105604Ssam  }
7666174Sbsd
7766174Sbsd  MCSymbolData &getSymbolData(const MCSymbol &Symbol) {
7866174Sbsd    MCSymbolData *&Entry = SymbolMap[&Symbol];
7966174Sbsd
8066174Sbsd    if (!Entry)
81105604Ssam      Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
8266174Sbsd
8366174Sbsd    return *Entry;
8466174Sbsd  }
85
86public:
87  MCMachOStreamer(MCContext &Context, raw_ostream &_OS, MCCodeEmitter *_Emitter)
88    : MCStreamer(Context), Assembler(Context, _OS), Emitter(_Emitter),
89      CurSectionData(0) {}
90  ~MCMachOStreamer() {}
91
92  const MCExpr *AddValueSymbols(const MCExpr *Value) {
93    switch (Value->getKind()) {
94    case MCExpr::Constant:
95      break;
96
97    case MCExpr::Binary: {
98      const MCBinaryExpr *BE = cast<MCBinaryExpr>(Value);
99      AddValueSymbols(BE->getLHS());
100      AddValueSymbols(BE->getRHS());
101      break;
102    }
103
104    case MCExpr::SymbolRef:
105      getSymbolData(cast<MCSymbolRefExpr>(Value)->getSymbol());
106      break;
107
108    case MCExpr::Unary:
109      AddValueSymbols(cast<MCUnaryExpr>(Value)->getSubExpr());
110      break;
111    }
112
113    return Value;
114  }
115
116  /// @name MCStreamer Interface
117  /// @{
118
119  virtual void SwitchSection(const MCSection *Section);
120  virtual void EmitLabel(MCSymbol *Symbol);
121  virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
122  virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
123  virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
124  virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
125  virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
126                                unsigned ByteAlignment);
127  virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size) {
128    assert(0 && "macho doesn't support this directive");
129  }
130  virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
131                            unsigned Size = 0, unsigned ByteAlignment = 0);
132  virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
133  virtual void EmitValue(const MCExpr *Value, unsigned Size,unsigned AddrSpace);
134  virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
135                                    unsigned ValueSize = 1,
136                                    unsigned MaxBytesToEmit = 0);
137  virtual void EmitValueToOffset(const MCExpr *Offset,
138                                 unsigned char Value = 0);
139  virtual void EmitInstruction(const MCInst &Inst);
140  virtual void Finish();
141
142  /// @}
143};
144
145} // end anonymous namespace.
146
147void MCMachOStreamer::SwitchSection(const MCSection *Section) {
148  assert(Section && "Cannot switch to a null section!");
149
150  // If already in this section, then this is a noop.
151  if (Section == CurSection) return;
152
153  CurSection = Section;
154  CurSectionData = &getSectionData(*Section);
155}
156
157void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
158  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
159
160  // FIXME: We should also use offsets into Fill fragments.
161  MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
162  if (!F)
163    F = new MCDataFragment(CurSectionData);
164
165  MCSymbolData &SD = getSymbolData(*Symbol);
166  assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
167  SD.setFragment(F);
168  SD.setOffset(F->getContents().size());
169
170  // This causes the reference type and weak reference flags to be cleared.
171  SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
172
173  Symbol->setSection(*CurSection);
174}
175
176void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
177  switch (Flag) {
178  case MCAF_SubsectionsViaSymbols:
179    Assembler.setSubsectionsViaSymbols(true);
180    return;
181  }
182
183  assert(0 && "invalid assembler flag!");
184}
185
186void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
187  // Only absolute symbols can be redefined.
188  assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
189         "Cannot define a symbol twice!");
190
191  // FIXME: Lift context changes into super class.
192  // FIXME: Set associated section.
193  Symbol->setValue(Value);
194}
195
196void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
197                                          MCSymbolAttr Attribute) {
198  // Indirect symbols are handled differently, to match how 'as' handles
199  // them. This makes writing matching .o files easier.
200  if (Attribute == MCSA_IndirectSymbol) {
201    // Note that we intentionally cannot use the symbol data here; this is
202    // important for matching the string table that 'as' generates.
203    IndirectSymbolData ISD;
204    ISD.Symbol = Symbol;
205    ISD.SectionData = CurSectionData;
206    Assembler.getIndirectSymbols().push_back(ISD);
207    return;
208  }
209
210  // Adding a symbol attribute always introduces the symbol, note that an
211  // important side effect of calling getSymbolData here is to register the
212  // symbol with the assembler.
213  MCSymbolData &SD = getSymbolData(*Symbol);
214
215  // The implementation of symbol attributes is designed to match 'as', but it
216  // leaves much to desired. It doesn't really make sense to arbitrarily add and
217  // remove flags, but 'as' allows this (in particular, see .desc).
218  //
219  // In the future it might be worth trying to make these operations more well
220  // defined.
221  switch (Attribute) {
222  case MCSA_Invalid:
223  case MCSA_IndirectSymbol:
224  case MCSA_Hidden:
225  case MCSA_Internal:
226  case MCSA_Protected:
227  case MCSA_Weak:
228  case MCSA_Local:
229    assert(0 && "Invalid symbol attribute for Mach-O!");
230    break;
231
232  case MCSA_Global:
233    SD.setExternal(true);
234    break;
235
236  case MCSA_LazyReference:
237    // FIXME: This requires -dynamic.
238    SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
239    if (Symbol->isUndefined())
240      SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
241    break;
242
243    // Since .reference sets the no dead strip bit, it is equivalent to
244    // .no_dead_strip in practice.
245  case MCSA_Reference:
246  case MCSA_NoDeadStrip:
247    SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
248    break;
249
250  case MCSA_PrivateExtern:
251    SD.setExternal(true);
252    SD.setPrivateExtern(true);
253    break;
254
255  case MCSA_WeakReference:
256    // FIXME: This requires -dynamic.
257    if (Symbol->isUndefined())
258      SD.setFlags(SD.getFlags() | SF_WeakReference);
259    break;
260
261  case MCSA_WeakDefinition:
262    // FIXME: 'as' enforces that this is defined and global. The manual claims
263    // it has to be in a coalesced section, but this isn't enforced.
264    SD.setFlags(SD.getFlags() | SF_WeakDefinition);
265    break;
266  }
267}
268
269void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
270  // Encode the 'desc' value into the lowest implementation defined bits.
271  assert(DescValue == (DescValue & SF_DescFlagsMask) &&
272         "Invalid .desc value!");
273  getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
274}
275
276void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
277                                       unsigned ByteAlignment) {
278  // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
279  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
280
281  MCSymbolData &SD = getSymbolData(*Symbol);
282  SD.setExternal(true);
283  SD.setCommon(Size, ByteAlignment);
284}
285
286void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
287                                   unsigned Size, unsigned ByteAlignment) {
288  MCSectionData &SectData = getSectionData(*Section);
289
290  // The symbol may not be present, which only creates the section.
291  if (!Symbol)
292    return;
293
294  // FIXME: Assert that this section has the zerofill type.
295
296  assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
297
298  MCSymbolData &SD = getSymbolData(*Symbol);
299
300  MCFragment *F = new MCZeroFillFragment(Size, ByteAlignment, &SectData);
301  SD.setFragment(F);
302
303  Symbol->setSection(*Section);
304
305  // Update the maximum alignment on the zero fill section if necessary.
306  if (ByteAlignment > SectData.getAlignment())
307    SectData.setAlignment(ByteAlignment);
308}
309
310void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
311  MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
312  if (!DF)
313    DF = new MCDataFragment(CurSectionData);
314  DF->getContents().append(Data.begin(), Data.end());
315}
316
317void MCMachOStreamer::EmitValue(const MCExpr *Value, unsigned Size,
318                                unsigned AddrSpace) {
319  new MCFillFragment(*AddValueSymbols(Value), Size, 1, CurSectionData);
320}
321
322void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
323                                           int64_t Value, unsigned ValueSize,
324                                           unsigned MaxBytesToEmit) {
325  if (MaxBytesToEmit == 0)
326    MaxBytesToEmit = ByteAlignment;
327  new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
328                      CurSectionData);
329
330  // Update the maximum alignment on the current section if necessary.
331  if (ByteAlignment > CurSectionData->getAlignment())
332    CurSectionData->setAlignment(ByteAlignment);
333}
334
335void MCMachOStreamer::EmitValueToOffset(const MCExpr *Offset,
336                                        unsigned char Value) {
337  new MCOrgFragment(*Offset, Value, CurSectionData);
338}
339
340void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
341  // Scan for values.
342  for (unsigned i = 0; i != Inst.getNumOperands(); ++i)
343    if (Inst.getOperand(i).isExpr())
344      AddValueSymbols(Inst.getOperand(i).getExpr());
345
346  if (!Emitter)
347    llvm_unreachable("no code emitter available!");
348
349  // FIXME: Emitting an instruction should cause S_ATTR_SOME_INSTRUCTIONS to
350  //        be set for the current section.
351  // FIXME: Relocations!
352  SmallString<256> Code;
353  raw_svector_ostream VecOS(Code);
354  Emitter->EncodeInstruction(Inst, VecOS);
355  EmitBytes(VecOS.str(), 0);
356}
357
358void MCMachOStreamer::Finish() {
359  Assembler.Finish();
360}
361
362MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS,
363                                      MCCodeEmitter *CE) {
364  return new MCMachOStreamer(Context, OS, CE);
365}
366