bed0d43d3797fed36edc16cef986291fdc71ba49
[oota-llvm.git] / lib / MC / MCMachOStreamer.cpp
1 //===- lib/MC/MCMachOStreamer.cpp - Mach-O Object Output ------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCStreamer.h"
11
12 #include "llvm/MC/MCAssembler.h"
13 #include "llvm/MC/MCContext.h"
14 #include "llvm/MC/MCSection.h"
15 #include "llvm/MC/MCSymbol.h"
16 #include "llvm/Support/ErrorHandling.h"
17 using namespace llvm;
18
19 namespace {
20
21 class MCMachOStreamer : public MCStreamer {
22   /// SymbolFlags - We store the value for the 'desc' symbol field in the lowest
23   /// 16 bits of the implementation defined flags.
24   enum SymbolFlags { // See <mach-o/nlist.h>.
25     SF_DescFlagsMask                        = 0xFFFF,
26
27     // Reference type flags.
28     SF_ReferenceTypeMask                    = 0x0007,
29     SF_ReferenceTypeUndefinedNonLazy        = 0x0000,
30     SF_ReferenceTypeUndefinedLazy           = 0x0001,
31     SF_ReferenceTypeDefined                 = 0x0002,
32     SF_ReferenceTypePrivateDefined          = 0x0003,
33     SF_ReferenceTypePrivateUndefinedNonLazy = 0x0004,
34     SF_ReferenceTypePrivateUndefinedLazy    = 0x0005,
35
36     // Other 'desc' flags.
37     SF_NoDeadStrip                          = 0x0020,
38     SF_WeakReference                        = 0x0040,
39     SF_WeakDefinition                       = 0x0080
40   };
41
42 private:
43   MCAssembler Assembler;
44
45   MCSectionData *CurSectionData;
46
47   DenseMap<const MCSection*, MCSectionData*> SectionMap;
48   
49   DenseMap<const MCSymbol*, MCSymbolData*> SymbolMap;
50
51 private:
52   MCFragment *getCurrentFragment() const {
53     assert(CurSectionData && "No current section!");
54
55     if (!CurSectionData->empty())
56       return &CurSectionData->getFragmentList().back();
57
58     return 0;
59   }
60
61   MCSymbolData &getSymbolData(MCSymbol &Symbol) {
62     MCSymbolData *&Entry = SymbolMap[&Symbol];
63
64     if (!Entry)
65       Entry = new MCSymbolData(Symbol, 0, 0, &Assembler);
66
67     return *Entry;
68   }
69
70 public:
71   MCMachOStreamer(MCContext &Context, raw_ostream &_OS)
72     : MCStreamer(Context), Assembler(_OS), CurSectionData(0) {}
73   ~MCMachOStreamer() {}
74
75   /// @name MCStreamer Interface
76   /// @{
77
78   virtual void SwitchSection(const MCSection *Section);
79
80   virtual void EmitLabel(MCSymbol *Symbol);
81
82   virtual void EmitAssemblerFlag(AssemblerFlag Flag);
83
84   virtual void EmitAssignment(MCSymbol *Symbol, const MCValue &Value,
85                               bool MakeAbsolute = false);
86
87   virtual void EmitSymbolAttribute(MCSymbol *Symbol, SymbolAttr Attribute);
88
89   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
90
91   virtual void EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value);
92
93   virtual void EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
94                                 unsigned Pow2Alignment, bool IsLocal);
95
96   virtual void EmitZerofill(MCSection *Section, MCSymbol *Symbol = NULL,
97                             unsigned Size = 0, unsigned Pow2Alignment = 0);
98
99   virtual void EmitBytes(const StringRef &Data);
100
101   virtual void EmitValue(const MCValue &Value, unsigned Size);
102
103   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
104                                     unsigned ValueSize = 1,
105                                     unsigned MaxBytesToEmit = 0);
106
107   virtual void EmitValueToOffset(const MCValue &Offset,
108                                  unsigned char Value = 0);
109
110   virtual void EmitInstruction(const MCInst &Inst);
111
112   virtual void Finish();
113
114   /// @}
115 };
116
117 } // end anonymous namespace.
118
119 void MCMachOStreamer::SwitchSection(const MCSection *Section) {
120   assert(Section && "Cannot switch to a null section!");
121   
122   // If already in this section, then this is a noop.
123   if (Section == CurSection) return;
124   
125   CurSection = Section;
126   MCSectionData *&Entry = SectionMap[Section];
127
128   if (!Entry)
129     Entry = new MCSectionData(*Section, &Assembler);
130
131   CurSectionData = Entry;
132 }
133
134 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
135   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
136
137   // FIXME: We should also use offsets into Fill fragments.
138   MCDataFragment *F = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
139   if (!F)
140     F = new MCDataFragment(CurSectionData);
141
142   MCSymbolData &SD = getSymbolData(*Symbol);
143   assert(!SD.getFragment() && "Unexpected fragment on symbol data!");
144   SD.setFragment(F);
145   SD.setOffset(F->getContents().size());
146
147   // This causes the reference type and weak reference flags to be cleared.
148   SD.setFlags(SD.getFlags() & ~(SF_WeakReference | SF_ReferenceTypeMask));
149   
150   Symbol->setSection(*CurSection);
151 }
152
153 void MCMachOStreamer::EmitAssemblerFlag(AssemblerFlag Flag) {
154   llvm_unreachable("FIXME: Not yet implemented!");
155 }
156
157 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol,
158                                      const MCValue &Value,
159                                      bool MakeAbsolute) {
160   // Only absolute symbols can be redefined.
161   assert((Symbol->isUndefined() || Symbol->isAbsolute()) &&
162          "Cannot define a symbol twice!");
163
164   llvm_unreachable("FIXME: Not yet implemented!");
165 }
166
167 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
168                                           SymbolAttr Attribute) {
169   // Indirect symbols are handled differently, to match how 'as' handles
170   // them. This makes writing matching .o files easier.
171   if (Attribute == MCStreamer::IndirectSymbol) {
172     // Note that we intentionally cannot use the symbol data here; this is
173     // important for matching the string table that 'as' generates.
174     IndirectSymbolData ISD;
175     ISD.Symbol = Symbol;
176     ISD.SectionData = CurSectionData;
177     Assembler.getIndirectSymbols().push_back(ISD);
178     return;
179   }
180
181   // Adding a symbol attribute always introduces the symbol, note that an
182   // important side effect of calling getSymbolData here is to register the
183   // symbol with the assembler.
184   MCSymbolData &SD = getSymbolData(*Symbol);
185
186   // The implementation of symbol attributes is designed to match 'as', but it
187   // leaves much to desired. It doesn't really make sense to arbitrarily add and
188   // remove flags, but 'as' allows this (in particular, see .desc).
189   //
190   // In the future it might be worth trying to make these operations more well
191   // defined.
192   switch (Attribute) {
193   case MCStreamer::IndirectSymbol:
194   case MCStreamer::Hidden:
195   case MCStreamer::Internal:
196   case MCStreamer::Protected:
197   case MCStreamer::Weak:
198     assert(0 && "Invalid symbol attribute for Mach-O!");
199     break;
200
201   case MCStreamer::Global:
202     getSymbolData(*Symbol).setExternal(true);
203     break;
204
205   case MCStreamer::LazyReference:
206     // FIXME: This requires -dynamic.
207     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
208     if (Symbol->isUndefined())
209       SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
210     break;
211
212     // Since .reference sets the no dead strip bit, it is equivalent to
213     // .no_dead_strip in practice.
214   case MCStreamer::Reference:
215   case MCStreamer::NoDeadStrip:
216     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
217     break;
218
219   case MCStreamer::PrivateExtern:
220     SD.setExternal(true);
221     SD.setPrivateExtern(true);
222     break;
223
224   case MCStreamer::WeakReference:
225     // FIXME: This requires -dynamic.
226     if (Symbol->isUndefined())
227       SD.setFlags(SD.getFlags() | SF_WeakReference);
228     break;
229
230   case MCStreamer::WeakDefinition:
231     // FIXME: 'as' enforces that this is defined and global. The manual claims
232     // it has to be in a coalesced section, but this isn't enforced.
233     SD.setFlags(SD.getFlags() | SF_WeakDefinition);
234     break;
235   }
236 }
237
238 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
239   // Encode the 'desc' value into the lowest implementation defined bits.
240   assert(DescValue == (DescValue & SF_DescFlagsMask) && 
241          "Invalid .desc value!");
242   getSymbolData(*Symbol).setFlags(DescValue & SF_DescFlagsMask);
243 }
244
245 void MCMachOStreamer::EmitLocalSymbol(MCSymbol *Symbol, const MCValue &Value) {
246   llvm_unreachable("FIXME: Not yet implemented!");
247 }
248
249 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, unsigned Size,
250                                        unsigned Pow2Alignment,
251                                        bool IsLocal) {
252   llvm_unreachable("FIXME: Not yet implemented!");
253 }
254
255 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
256                                    unsigned Size, unsigned Pow2Alignment) {
257   llvm_unreachable("FIXME: Not yet implemented!");
258 }
259
260 void MCMachOStreamer::EmitBytes(const StringRef &Data) {
261   MCDataFragment *DF = dyn_cast_or_null<MCDataFragment>(getCurrentFragment());
262   if (!DF)
263     DF = new MCDataFragment(CurSectionData);
264   DF->getContents().append(Data.begin(), Data.end());
265 }
266
267 void MCMachOStreamer::EmitValue(const MCValue &Value, unsigned Size) {
268   new MCFillFragment(Value, Size, 1, CurSectionData);
269 }
270
271 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
272                                            int64_t Value, unsigned ValueSize,
273                                            unsigned MaxBytesToEmit) {
274   if (MaxBytesToEmit == 0)
275     MaxBytesToEmit = ByteAlignment;
276   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
277                       CurSectionData);
278
279   // Update the maximum alignment on the current section if necessary.
280   if (ByteAlignment > CurSectionData->getAlignment())
281     CurSectionData->setAlignment(ByteAlignment);
282 }
283
284 void MCMachOStreamer::EmitValueToOffset(const MCValue &Offset,
285                                         unsigned char Value) {
286   new MCOrgFragment(Offset, Value, CurSectionData);
287 }
288
289 void MCMachOStreamer::EmitInstruction(const MCInst &Inst) {
290   llvm_unreachable("FIXME: Not yet implemented!");
291 }
292
293 void MCMachOStreamer::Finish() {
294   Assembler.Finish();
295 }
296
297 MCStreamer *llvm::createMachOStreamer(MCContext &Context, raw_ostream &OS) {
298   return new MCMachOStreamer(Context, OS);
299 }