• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/MCCodeEmitter.h"
15 #include "llvm/MC/MCExpr.h"
16 #include "llvm/MC/MCInst.h"
17 #include "llvm/MC/MCObjectStreamer.h"
18 #include "llvm/MC/MCSection.h"
19 #include "llvm/MC/MCSymbol.h"
20 #include "llvm/MC/MCMachOSymbolFlags.h"
21 #include "llvm/MC/MCSectionMachO.h"
22 #include "llvm/MC/MCDwarf.h"
23 #include "llvm/MC/MCAsmBackend.h"
24 #include "llvm/Support/Dwarf.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include "llvm/Support/raw_ostream.h"
27 
28 using namespace llvm;
29 
30 namespace {
31 
32 class MCMachOStreamer : public MCObjectStreamer {
33 private:
34   virtual void EmitInstToData(const MCInst &Inst);
35 
36 public:
MCMachOStreamer(MCContext & Context,MCAsmBackend & MAB,raw_ostream & OS,MCCodeEmitter * Emitter)37   MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
38                   raw_ostream &OS, MCCodeEmitter *Emitter)
39     : MCObjectStreamer(Context, MAB, OS, Emitter) {}
40 
41   /// @name MCStreamer Interface
42   /// @{
43 
44   virtual void InitSections();
45   virtual void EmitLabel(MCSymbol *Symbol);
46   virtual void EmitEHSymAttributes(const MCSymbol *Symbol,
47                                    MCSymbol *EHSymbol);
48   virtual void EmitAssemblerFlag(MCAssemblerFlag Flag);
49   virtual void EmitThumbFunc(MCSymbol *Func);
50   virtual void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value);
51   virtual void EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute);
52   virtual void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue);
53   virtual void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
54                                 unsigned ByteAlignment);
BeginCOFFSymbolDef(const MCSymbol * Symbol)55   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
56     llvm_unreachable("macho doesn't support this directive");
57   }
EmitCOFFSymbolStorageClass(int StorageClass)58   virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
59     llvm_unreachable("macho doesn't support this directive");
60   }
EmitCOFFSymbolType(int Type)61   virtual void EmitCOFFSymbolType(int Type) {
62     llvm_unreachable("macho doesn't support this directive");
63   }
EndCOFFSymbolDef()64   virtual void EndCOFFSymbolDef() {
65     llvm_unreachable("macho doesn't support this directive");
66   }
EmitELFSize(MCSymbol * Symbol,const MCExpr * Value)67   virtual void EmitELFSize(MCSymbol *Symbol, const MCExpr *Value) {
68     llvm_unreachable("macho doesn't support this directive");
69   }
EmitLocalCommonSymbol(MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)70   virtual void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
71                                      unsigned ByteAlignment) {
72     llvm_unreachable("macho doesn't support this directive");
73   }
74   virtual void EmitZerofill(const MCSection *Section, MCSymbol *Symbol = 0,
75                             unsigned 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 
EmitFileDirective(StringRef Filename)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 
InitSections()99 void MCMachOStreamer::InitSections() {
100   SwitchSection(getContext().getMachOSection("__TEXT", "__text",
101                                     MCSectionMachO::S_ATTR_PURE_INSTRUCTIONS,
102                                     0, SectionKind::getText()));
103 
104 }
105 
EmitEHSymAttributes(const MCSymbol * Symbol,MCSymbol * EHSymbol)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 
EmitLabel(MCSymbol * Symbol)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 
EmitAssemblerFlag(MCAssemblerFlag Flag)141 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
142   // Let the target do whatever target specific stuff it needs to do.
143   getAssembler().getBackend().handleAssemblerFlag(Flag);
144   // Do any generic stuff we need to do.
145   switch (Flag) {
146   case MCAF_SyntaxUnified: return; // no-op here.
147   case MCAF_Code16: return; // Change parsing mode; no-op here.
148   case MCAF_Code32: return; // Change parsing mode; no-op here.
149   case MCAF_Code64: return; // Change parsing mode; no-op here.
150   case MCAF_SubsectionsViaSymbols:
151     getAssembler().setSubsectionsViaSymbols(true);
152     return;
153   }
154 }
155 
EmitThumbFunc(MCSymbol * Symbol)156 void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
157   // Remember that the function is a thumb function. Fixup and relocation
158   // values will need adjusted.
159   getAssembler().setIsThumbFunc(Symbol);
160 
161   // Mark the thumb bit on the symbol.
162   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
163   SD.setFlags(SD.getFlags() | SF_ThumbFunc);
164 }
165 
EmitAssignment(MCSymbol * Symbol,const MCExpr * Value)166 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
167   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
168   // MCObjectStreamer.
169   // FIXME: Lift context changes into super class.
170   getAssembler().getOrCreateSymbolData(*Symbol);
171   Symbol->setVariableValue(AddValueSymbols(Value));
172 }
173 
EmitSymbolAttribute(MCSymbol * Symbol,MCSymbolAttr Attribute)174 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
175                                           MCSymbolAttr Attribute) {
176   // Indirect symbols are handled differently, to match how 'as' handles
177   // them. This makes writing matching .o files easier.
178   if (Attribute == MCSA_IndirectSymbol) {
179     // Note that we intentionally cannot use the symbol data here; this is
180     // important for matching the string table that 'as' generates.
181     IndirectSymbolData ISD;
182     ISD.Symbol = Symbol;
183     ISD.SectionData = getCurrentSectionData();
184     getAssembler().getIndirectSymbols().push_back(ISD);
185     return;
186   }
187 
188   // Adding a symbol attribute always introduces the symbol, note that an
189   // important side effect of calling getOrCreateSymbolData here is to register
190   // the symbol with the assembler.
191   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
192 
193   // The implementation of symbol attributes is designed to match 'as', but it
194   // leaves much to desired. It doesn't really make sense to arbitrarily add and
195   // remove flags, but 'as' allows this (in particular, see .desc).
196   //
197   // In the future it might be worth trying to make these operations more well
198   // defined.
199   switch (Attribute) {
200   case MCSA_Invalid:
201   case MCSA_ELF_TypeFunction:
202   case MCSA_ELF_TypeIndFunction:
203   case MCSA_ELF_TypeObject:
204   case MCSA_ELF_TypeTLS:
205   case MCSA_ELF_TypeCommon:
206   case MCSA_ELF_TypeNoType:
207   case MCSA_ELF_TypeGnuUniqueObject:
208   case MCSA_Hidden:
209   case MCSA_IndirectSymbol:
210   case MCSA_Internal:
211   case MCSA_Protected:
212   case MCSA_Weak:
213   case MCSA_Local:
214     llvm_unreachable("Invalid symbol attribute for Mach-O!");
215 
216   case MCSA_Global:
217     SD.setExternal(true);
218     // This effectively clears the undefined lazy bit, in Darwin 'as', although
219     // it isn't very consistent because it implements this as part of symbol
220     // lookup.
221     //
222     // FIXME: Cleanup this code, these bits should be emitted based on semantic
223     // properties, not on the order of definition, etc.
224     SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
225     break;
226 
227   case MCSA_LazyReference:
228     // FIXME: This requires -dynamic.
229     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
230     if (Symbol->isUndefined())
231       SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
232     break;
233 
234     // Since .reference sets the no dead strip bit, it is equivalent to
235     // .no_dead_strip in practice.
236   case MCSA_Reference:
237   case MCSA_NoDeadStrip:
238     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
239     break;
240 
241   case MCSA_SymbolResolver:
242     SD.setFlags(SD.getFlags() | SF_SymbolResolver);
243     break;
244 
245   case MCSA_PrivateExtern:
246     SD.setExternal(true);
247     SD.setPrivateExtern(true);
248     break;
249 
250   case MCSA_WeakReference:
251     // FIXME: This requires -dynamic.
252     if (Symbol->isUndefined())
253       SD.setFlags(SD.getFlags() | SF_WeakReference);
254     break;
255 
256   case MCSA_WeakDefinition:
257     // FIXME: 'as' enforces that this is defined and global. The manual claims
258     // it has to be in a coalesced section, but this isn't enforced.
259     SD.setFlags(SD.getFlags() | SF_WeakDefinition);
260     break;
261 
262   case MCSA_WeakDefAutoPrivate:
263     SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
264     break;
265   }
266 }
267 
EmitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)268 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
269   // Encode the 'desc' value into the lowest implementation defined bits.
270   assert(DescValue == (DescValue & SF_DescFlagsMask) &&
271          "Invalid .desc value!");
272   getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
273     DescValue & SF_DescFlagsMask);
274 }
275 
EmitCommonSymbol(MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)276 void 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 = getAssembler().getOrCreateSymbolData(*Symbol);
282   SD.setExternal(true);
283   SD.setCommon(Size, ByteAlignment);
284 }
285 
EmitZerofill(const MCSection * Section,MCSymbol * Symbol,unsigned Size,unsigned ByteAlignment)286 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
287                                    unsigned Size, unsigned ByteAlignment) {
288   MCSectionData &SectData = getAssembler().getOrCreateSectionData(*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 = getAssembler().getOrCreateSymbolData(*Symbol);
299 
300   // Emit an align fragment if necessary.
301   if (ByteAlignment != 1)
302     new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
303 
304   MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
305   SD.setFragment(F);
306 
307   Symbol->setSection(*Section);
308 
309   // Update the maximum alignment on the zero fill section if necessary.
310   if (ByteAlignment > SectData.getAlignment())
311     SectData.setAlignment(ByteAlignment);
312 }
313 
314 // This should always be called with the thread local bss section.  Like the
315 // .zerofill directive this doesn't actually switch sections on us.
EmitTBSSSymbol(const MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)316 void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
317                                      uint64_t Size, unsigned ByteAlignment) {
318   EmitZerofill(Section, Symbol, Size, ByteAlignment);
319   return;
320 }
321 
EmitBytes(StringRef Data,unsigned AddrSpace)322 void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
323   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
324   // MCObjectStreamer.
325   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
326 }
327 
EmitValueToAlignment(unsigned ByteAlignment,int64_t Value,unsigned ValueSize,unsigned MaxBytesToEmit)328 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
329                                            int64_t Value, unsigned ValueSize,
330                                            unsigned MaxBytesToEmit) {
331   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
332   // MCObjectStreamer.
333   if (MaxBytesToEmit == 0)
334     MaxBytesToEmit = ByteAlignment;
335   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
336                       getCurrentSectionData());
337 
338   // Update the maximum alignment on the current section if necessary.
339   if (ByteAlignment > getCurrentSectionData()->getAlignment())
340     getCurrentSectionData()->setAlignment(ByteAlignment);
341 }
342 
EmitCodeAlignment(unsigned ByteAlignment,unsigned MaxBytesToEmit)343 void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
344                                         unsigned MaxBytesToEmit) {
345   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
346   // MCObjectStreamer.
347   if (MaxBytesToEmit == 0)
348     MaxBytesToEmit = ByteAlignment;
349   MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
350                                            getCurrentSectionData());
351   F->setEmitNops(true);
352 
353   // Update the maximum alignment on the current section if necessary.
354   if (ByteAlignment > getCurrentSectionData()->getAlignment())
355     getCurrentSectionData()->setAlignment(ByteAlignment);
356 }
357 
EmitInstToData(const MCInst & Inst)358 void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
359   MCDataFragment *DF = getOrCreateDataFragment();
360 
361   SmallVector<MCFixup, 4> Fixups;
362   SmallString<256> Code;
363   raw_svector_ostream VecOS(Code);
364   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
365   VecOS.flush();
366 
367   // Add the fixups and data.
368   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
369     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
370     DF->addFixup(Fixups[i]);
371   }
372   DF->getContents().append(Code.begin(), Code.end());
373 }
374 
FinishImpl()375 void MCMachOStreamer::FinishImpl() {
376   EmitFrames(true);
377 
378   // We have to set the fragment atom associations so we can relax properly for
379   // Mach-O.
380 
381   // First, scan the symbol table to build a lookup table from fragments to
382   // defining symbols.
383   DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
384   for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
385          ie = getAssembler().symbol_end(); it != ie; ++it) {
386     if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
387         it->getFragment()) {
388       // An atom defining symbol should never be internal to a fragment.
389       assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
390       DefiningSymbolMap[it->getFragment()] = it;
391     }
392   }
393 
394   // Set the fragment atom associations by tracking the last seen atom defining
395   // symbol.
396   for (MCAssembler::iterator it = getAssembler().begin(),
397          ie = getAssembler().end(); it != ie; ++it) {
398     MCSymbolData *CurrentAtom = 0;
399     for (MCSectionData::iterator it2 = it->begin(),
400            ie2 = it->end(); it2 != ie2; ++it2) {
401       if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
402         CurrentAtom = SD;
403       it2->setAtom(CurrentAtom);
404     }
405   }
406 
407   this->MCObjectStreamer::FinishImpl();
408 }
409 
createMachOStreamer(MCContext & Context,MCAsmBackend & MAB,raw_ostream & OS,MCCodeEmitter * CE,bool RelaxAll)410 MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
411                                       raw_ostream &OS, MCCodeEmitter *CE,
412                                       bool RelaxAll) {
413   MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
414   if (RelaxAll)
415     S->getAssembler().setRelaxAll(true);
416   return S;
417 }
418