MachO: direct-to-object attribute for data-in-code markers.
[oota-llvm.git] / lib / MC / MCMachOStreamer.cpp
1 //
2 //                     The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8
9 #include "llvm/MC/MCStreamer.h"
10
11 #include "llvm/MC/MCAssembler.h"
12 #include "llvm/MC/MCContext.h"
13 #include "llvm/MC/MCCodeEmitter.h"
14 #include "llvm/MC/MCExpr.h"
15 #include "llvm/MC/MCInst.h"
16 #include "llvm/MC/MCObjectStreamer.h"
17 #include "llvm/MC/MCSection.h"
18 #include "llvm/MC/MCSymbol.h"
19 #include "llvm/MC/MCMachOSymbolFlags.h"
20 #include "llvm/MC/MCSectionMachO.h"
21 #include "llvm/MC/MCDwarf.h"
22 #include "llvm/MC/MCAsmBackend.h"
23 #include "llvm/Support/Dwarf.h"
24 #include "llvm/Support/ErrorHandling.h"
25 #include "llvm/Support/raw_ostream.h"
26
27 using namespace llvm;
28
29 namespace {
30
31 class MCMachOStreamer : public MCObjectStreamer {
32 private:
33   virtual void EmitInstToData(const MCInst &Inst);
34
35   void EmitDataRegion(DataRegionData::KindTy Kind);
36   void EmitDataRegionEnd();
37 public:
38   MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
39                   raw_ostream &OS, MCCodeEmitter *Emitter)
40     : MCObjectStreamer(Context, MAB, OS, Emitter) {}
41
42   /// @name MCStreamer Interface
43   /// @{
44
45   virtual void InitSections();
46   virtual void EmitLabel(MCSymbol *Symbol);
47   virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
48                                    MCSymbol *EHSymbol);
49   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
50   virtual void EmitDataRegion(MCDataRegionType Kind);
51   virtual void EmitThumbFunc(MCSymbol *Func);
52   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
53   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
54   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
55   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
56                                 unsigned ByteAlignment);
57   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
58     llvm_unreachable("macho doesn't support this directive");
59   }
60   virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
61     llvm_unreachable("macho doesn't support this directive");
62   }
63   virtual void EmitCOFFSymbolType(int Type) {
64     llvm_unreachable("macho doesn't support this directive");
65   }
66   virtual void EndCOFFSymbolDef() {
67     llvm_unreachable("macho doesn't support this directive");
68   }
69   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
70     llvm_unreachable("macho doesn't support this directive");
71   }
72   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
73                                      unsigned ByteAlignment);
74   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
75                             uint64_t Size = 0, unsigned ByteAlignment = 0);
76   virtual void EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
77                               uint64_t Size, unsigned ByteAlignment = 0);
78   virtual void EmitBytes(StringRef Data, unsigned AddrSpace);
79   virtual void EmitValueToAlignment(unsigned ByteAlignment, int64_t Value = 0,
80                                     unsigned ValueSize = 1,
81                                     unsigned MaxBytesToEmit = 0);
82   virtual void EmitCodeAlignment(unsigned ByteAlignment,
83                                  unsigned MaxBytesToEmit = 0);
84
85   virtual void EmitFileDirective(StringRef Filename) {
86     // FIXME: Just ignore the .file; it isn't important enough to fail the
87     // entire assembly.
88
89     //report_fatal_error("unsupported directive: '.file'");
90   }
91
92   virtual void FinishImpl();
93
94   /// @}
95 };
96
97 } // end anonymous namespace.
98
99 void MCMachOStreamer::InitSections() {
100   SwitchSection(getContext().getMachOSection("__TEXT", "__text",
101                                     MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
102                                     0, SectionKind::getText()));
103
104 }
105
106 void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
107                                           MCSymbol *EHSymbol) {
108   MCSymbolData &SD =
109     getAssembler().getOrCreateSymbolData(*Symbol);
110   if (SD.isExternal())
111     EmitSymbolAttribute(EHSymbol, MCSA_Global);
112   if (SD.getFlags() & SF_WeakDefinition)
113     EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
114   if (SD.isPrivateExtern())
115     EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
116 }
117
118 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
119   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
120
121   // isSymbolLinkerVisible uses the section.
122   Symbol->setSection(*getCurrentSection());
123   // We have to create a new fragment if this is an atom defining symbol,
124   // fragments cannot span atoms.
125   if (getAssembler().isSymbolLinkerVisible(*Symbol))
126     new MCDataFragment(getCurrentSectionData());
127
128   MCObjectStreamer::EmitLabel(Symbol);
129
130   MCSymbolData &SD = getAssembler().getSymbolData(*Symbol);
131   // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
132   // to clear the weak reference and weak definition bits too, but the
133   // implementation was buggy. For now we just try to match 'as', for
134   // diffability.
135   //
136   // FIXME: Cleanup this code, these bits should be emitted based on semantic
137   // properties, not on the order of definition, etc.
138   SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeMask);
139 }
140
141 void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
142   if (!getAssembler().getBackend().hasDataInCodeSupport())
143     return;
144   // Create a temporary label to mark the start of the data region.
145   MCSymbol *Start = getContext().CreateTempSymbol();
146   EmitLabel(Start);
147   // Record the region for the object writer to use.
148   DataRegionData Data = { Kind, Start, NULL };
149   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
150   Regions.push_back(Data);
151 }
152
153 void MCMachOStreamer::EmitDataRegionEnd() {
154   if (!getAssembler().getBackend().hasDataInCodeSupport())
155     return;
156   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
157   assert(Regions.size() && "Mismatched .end_data_region!");
158   DataRegionData &Data = Regions.back();
159   assert(Data.End == NULL && "Mismatched .end_data_region!");
160   // Create a temporary label to mark the end of the data region.
161   Data.End = getContext().CreateTempSymbol();
162   EmitLabel(Data.End);
163 }
164
165 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
166   // Let the target do whatever target specific stuff it needs to do.
167   getAssembler().getBackend().handleAssemblerFlag(Flag);
168   // Do any generic stuff we need to do.
169   switch (Flag) {
170   case MCAF_SyntaxUnified: return; // no-op here.
171   case MCAF_Code16: return; // Change parsing mode; no-op here.
172   case MCAF_Code32: return; // Change parsing mode; no-op here.
173   case MCAF_Code64: return; // Change parsing mode; no-op here.
174   case MCAF_SubsectionsViaSymbols:
175     getAssembler().setSubsectionsViaSymbols(true);
176     return;
177   }
178 }
179
180 void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
181   switch (Kind) {
182   case MCDR_DataRegion:
183     EmitDataRegion(DataRegionData::Data);
184     return;
185   case MCDR_DataRegionJT8:
186     EmitDataRegion(DataRegionData::JumpTable8);
187     return;
188   case MCDR_DataRegionJT16:
189     EmitDataRegion(DataRegionData::JumpTable16);
190     return;
191   case MCDR_DataRegionJT32:
192     EmitDataRegion(DataRegionData::JumpTable32);
193     return;
194   case MCDR_DataRegionEnd:
195     EmitDataRegionEnd();
196     return;
197   }
198 }
199
200 void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
201   // Remember that the function is a thumb function. Fixup and relocation
202   // values will need adjusted.
203   getAssembler().setIsThumbFunc(Symbol);
204
205   // Mark the thumb bit on the symbol.
206   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
207   SD.setFlags(SD.getFlags() | SF_ThumbFunc);
208 }
209
210 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
211   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
212   // MCObjectStreamer.
213   // FIXME: Lift context changes into super class.
214   getAssembler().getOrCreateSymbolData(*Symbol);
215   Symbol->setVariableValue(AddValueSymbols(Value));
216 }
217
218 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
219                                           MCSymbolAttr Attribute) {
220   // Indirect symbols are handled differently, to match how 'as' handles
221   // them. This makes writing matching .o files easier.
222   if (Attribute == MCSA_IndirectSymbol) {
223     // Note that we intentionally cannot use the symbol data here; this is
224     // important for matching the string table that 'as' generates.
225     IndirectSymbolData ISD;
226     ISD.Symbol = Symbol;
227     ISD.SectionData = getCurrentSectionData();
228     getAssembler().getIndirectSymbols().push_back(ISD);
229     return;
230   }
231
232   // Adding a symbol attribute always introduces the symbol, note that an
233   // important side effect of calling getOrCreateSymbolData here is to register
234   // the symbol with the assembler.
235   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
236
237   // The implementation of symbol attributes is designed to match 'as', but it
238   // leaves much to desired. It doesn't really make sense to arbitrarily add and
239   // remove flags, but 'as' allows this (in particular, see .desc).
240   //
241   // In the future it might be worth trying to make these operations more well
242   // defined.
243   switch (Attribute) {
244   case MCSA_Invalid:
245   case MCSA_ELF_TypeFunction:
246   case MCSA_ELF_TypeIndFunction:
247   case MCSA_ELF_TypeObject:
248   case MCSA_ELF_TypeTLS:
249   case MCSA_ELF_TypeCommon:
250   case MCSA_ELF_TypeNoType:
251   case MCSA_ELF_TypeGnuUniqueObject:
252   case MCSA_Hidden:
253   case MCSA_IndirectSymbol:
254   case MCSA_Internal:
255   case MCSA_Protected:
256   case MCSA_Weak:
257   case MCSA_Local:
258     llvm_unreachable("Invalid symbol attribute for Mach-O!");
259
260   case MCSA_Global:
261     SD.setExternal(true);
262     // This effectively clears the undefined lazy bit, in Darwin 'as', although
263     // it isn't very consistent because it implements this as part of symbol
264     // lookup.
265     //
266     // FIXME: Cleanup this code, these bits should be emitted based on semantic
267     // properties, not on the order of definition, etc.
268     SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
269     break;
270
271   case MCSA_LazyReference:
272     // FIXME: This requires -dynamic.
273     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
274     if (Symbol->isUndefined())
275       SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
276     break;
277
278     // Since .reference sets the no dead strip bit, it is equivalent to
279     // .no_dead_strip in practice.
280   case MCSA_Reference:
281   case MCSA_NoDeadStrip:
282     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
283     break;
284
285   case MCSA_SymbolResolver:
286     SD.setFlags(SD.getFlags() | SF_SymbolResolver);
287     break;
288
289   case MCSA_PrivateExtern:
290     SD.setExternal(true);
291     SD.setPrivateExtern(true);
292     break;
293
294   case MCSA_WeakReference:
295     // FIXME: This requires -dynamic.
296     if (Symbol->isUndefined())
297       SD.setFlags(SD.getFlags() | SF_WeakReference);
298     break;
299
300   case MCSA_WeakDefinition:
301     // FIXME: 'as' enforces that this is defined and global. The manual claims
302     // it has to be in a coalesced section, but this isn't enforced.
303     SD.setFlags(SD.getFlags() | SF_WeakDefinition);
304     break;
305
306   case MCSA_WeakDefAutoPrivate:
307     SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
308     break;
309   }
310 }
311
312 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
313   // Encode the 'desc' value into the lowest implementation defined bits.
314   assert(DescValue == (DescValue & SF_DescFlagsMask) &&
315          "Invalid .desc value!");
316   getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
317     DescValue & SF_DescFlagsMask);
318 }
319
320 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
321                                        unsigned ByteAlignment) {
322   // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
323   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
324
325   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
326   SD.setExternal(true);
327   SD.setCommon(Size, ByteAlignment);
328 }
329
330 void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
331                                             unsigned ByteAlignment) {
332   // '.lcomm' is equivalent to '.zerofill'.
333   return EmitZerofill(getContext().getMachOSection("__DATA", "__bss",
334                                                    MCSectionMachO::S_ZEROFILL,
335                                                    0, SectionKind::getBSS()),
336                       Symbol, Size, ByteAlignment);
337 }
338
339 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
340                                    uint64_t Size, unsigned ByteAlignment) {
341   MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
342
343   // The symbol may not be present, which only creates the section.
344   if (!Symbol)
345     return;
346
347   // FIXME: Assert that this section has the zerofill type.
348
349   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
350
351   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
352
353   // Emit an align fragment if necessary.
354   if (ByteAlignment != 1)
355     new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
356
357   MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
358   SD.setFragment(F);
359
360   Symbol->setSection(*Section);
361
362   // Update the maximum alignment on the zero fill section if necessary.
363   if (ByteAlignment > SectData.getAlignment())
364     SectData.setAlignment(ByteAlignment);
365 }
366
367 // This should always be called with the thread local bss section.  Like the
368 // .zerofill directive this doesn't actually switch sections on us.
369 void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
370                                      uint64_t Size, unsigned ByteAlignment) {
371   EmitZerofill(Section, Symbol, Size, ByteAlignment);
372   return;
373 }
374
375 void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
376   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
377   // MCObjectStreamer.
378   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
379 }
380
381 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
382                                            int64_t Value, unsigned ValueSize,
383                                            unsigned MaxBytesToEmit) {
384   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
385   // MCObjectStreamer.
386   if (MaxBytesToEmit == 0)
387     MaxBytesToEmit = ByteAlignment;
388   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
389                       getCurrentSectionData());
390
391   // Update the maximum alignment on the current section if necessary.
392   if (ByteAlignment > getCurrentSectionData()->getAlignment())
393     getCurrentSectionData()->setAlignment(ByteAlignment);
394 }
395
396 void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
397                                         unsigned MaxBytesToEmit) {
398   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
399   // MCObjectStreamer.
400   if (MaxBytesToEmit == 0)
401     MaxBytesToEmit = ByteAlignment;
402   MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
403                                            getCurrentSectionData());
404   F->setEmitNops(true);
405
406   // Update the maximum alignment on the current section if necessary.
407   if (ByteAlignment > getCurrentSectionData()->getAlignment())
408     getCurrentSectionData()->setAlignment(ByteAlignment);
409 }
410
411 void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
412   MCDataFragment *DF = getOrCreateDataFragment();
413
414   SmallVector<MCFixup, 4> Fixups;
415   SmallString<256> Code;
416   raw_svector_ostream VecOS(Code);
417   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
418   VecOS.flush();
419
420   // Add the fixups and data.
421   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
422     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
423     DF->addFixup(Fixups[i]);
424   }
425   DF->getContents().append(Code.begin(), Code.end());
426 }
427
428 void MCMachOStreamer::FinishImpl() {
429   EmitFrames(true);
430
431   // We have to set the fragment atom associations so we can relax properly for
432   // Mach-O.
433
434   // First, scan the symbol table to build a lookup table from fragments to
435   // defining symbols.
436   DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
437   for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
438          ie = getAssembler().symbol_end(); it != ie; ++it) {
439     if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
440         it->getFragment()) {
441       // An atom defining symbol should never be internal to a fragment.
442       assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
443       DefiningSymbolMap[it->getFragment()] = it;
444     }
445   }
446
447   // Set the fragment atom associations by tracking the last seen atom defining
448   // symbol.
449   for (MCAssembler::iterator it = getAssembler().begin(),
450          ie = getAssembler().end(); it != ie; ++it) {
451     MCSymbolData *CurrentAtom = 0;
452     for (MCSectionData::iterator it2 = it->begin(),
453            ie2 = it->end(); it2 != ie2; ++it2) {
454       if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
455         CurrentAtom = SD;
456       it2->setAtom(CurrentAtom);
457     }
458   }
459
460   this->MCObjectStreamer::FinishImpl();
461 }
462
463 MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
464                                       raw_ostream &OS, MCCodeEmitter *CE,
465                                       bool RelaxAll) {
466   MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
467   if (RelaxAll)
468     S->getAssembler().setRelaxAll(true);
469   return S;
470 }