• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
MCMachOStreamer(MCContext & Context,MCAsmBackend & MAB,raw_ostream & OS,MCCodeEmitter * Emitter)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);
BeginCOFFSymbolDef(const MCSymbol * Symbol)57   virtual void BeginCOFFSymbolDef(const MCSymbol *Symbol) {
58     llvm_unreachable("macho doesn't support this directive");
59   }
EmitCOFFSymbolStorageClass(int StorageClass)60   virtual void EmitCOFFSymbolStorageClass(int StorageClass) {
61     llvm_unreachable("macho doesn't support this directive");
62   }
EmitCOFFSymbolType(int Type)63   virtual void EmitCOFFSymbolType(int Type) {
64     llvm_unreachable("macho doesn't support this directive");
65   }
EndCOFFSymbolDef()66   virtual void EndCOFFSymbolDef() {
67     llvm_unreachable("macho doesn't support this directive");
68   }
EmitELFSize(MCSymbol * Symbol,const MCExpr * Value)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 
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 
EmitDataRegion(DataRegionData::KindTy Kind)141 void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
142   // Create a temporary label to mark the start of the data region.
143   MCSymbol *Start = getContext().CreateTempSymbol();
144   EmitLabel(Start);
145   // Record the region for the object writer to use.
146   DataRegionData Data = { Kind, Start, NULL };
147   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
148   Regions.push_back(Data);
149 }
150 
EmitDataRegionEnd()151 void MCMachOStreamer::EmitDataRegionEnd() {
152   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
153   assert(Regions.size() && "Mismatched .end_data_region!");
154   DataRegionData &Data = Regions.back();
155   assert(Data.End == NULL && "Mismatched .end_data_region!");
156   // Create a temporary label to mark the end of the data region.
157   Data.End = getContext().CreateTempSymbol();
158   EmitLabel(Data.End);
159 }
160 
EmitAssemblerFlag(MCAssemblerFlag Flag)161 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
162   // Let the target do whatever target specific stuff it needs to do.
163   getAssembler().getBackend().handleAssemblerFlag(Flag);
164   // Do any generic stuff we need to do.
165   switch (Flag) {
166   case MCAF_SyntaxUnified: return; // no-op here.
167   case MCAF_Code16: return; // Change parsing mode; no-op here.
168   case MCAF_Code32: return; // Change parsing mode; no-op here.
169   case MCAF_Code64: return; // Change parsing mode; no-op here.
170   case MCAF_SubsectionsViaSymbols:
171     getAssembler().setSubsectionsViaSymbols(true);
172     return;
173   }
174 }
175 
EmitDataRegion(MCDataRegionType Kind)176 void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
177   switch (Kind) {
178   case MCDR_DataRegion:
179     EmitDataRegion(DataRegionData::Data);
180     return;
181   case MCDR_DataRegionJT8:
182     EmitDataRegion(DataRegionData::JumpTable8);
183     return;
184   case MCDR_DataRegionJT16:
185     EmitDataRegion(DataRegionData::JumpTable16);
186     return;
187   case MCDR_DataRegionJT32:
188     EmitDataRegion(DataRegionData::JumpTable32);
189     return;
190   case MCDR_DataRegionEnd:
191     EmitDataRegionEnd();
192     return;
193   }
194 }
195 
EmitThumbFunc(MCSymbol * Symbol)196 void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
197   // Remember that the function is a thumb function. Fixup and relocation
198   // values will need adjusted.
199   getAssembler().setIsThumbFunc(Symbol);
200 
201   // Mark the thumb bit on the symbol.
202   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
203   SD.setFlags(SD.getFlags() | SF_ThumbFunc);
204 }
205 
EmitAssignment(MCSymbol * Symbol,const MCExpr * Value)206 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
207   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
208   // MCObjectStreamer.
209   // FIXME: Lift context changes into super class.
210   getAssembler().getOrCreateSymbolData(*Symbol);
211   Symbol->setVariableValue(AddValueSymbols(Value));
212 }
213 
EmitSymbolAttribute(MCSymbol * Symbol,MCSymbolAttr Attribute)214 void MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Symbol,
215                                           MCSymbolAttr Attribute) {
216   // Indirect symbols are handled differently, to match how 'as' handles
217   // them. This makes writing matching .o files easier.
218   if (Attribute == MCSA_IndirectSymbol) {
219     // Note that we intentionally cannot use the symbol data here; this is
220     // important for matching the string table that 'as' generates.
221     IndirectSymbolData ISD;
222     ISD.Symbol = Symbol;
223     ISD.SectionData = getCurrentSectionData();
224     getAssembler().getIndirectSymbols().push_back(ISD);
225     return;
226   }
227 
228   // Adding a symbol attribute always introduces the symbol, note that an
229   // important side effect of calling getOrCreateSymbolData here is to register
230   // the symbol with the assembler.
231   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
232 
233   // The implementation of symbol attributes is designed to match 'as', but it
234   // leaves much to desired. It doesn't really make sense to arbitrarily add and
235   // remove flags, but 'as' allows this (in particular, see .desc).
236   //
237   // In the future it might be worth trying to make these operations more well
238   // defined.
239   switch (Attribute) {
240   case MCSA_Invalid:
241   case MCSA_ELF_TypeFunction:
242   case MCSA_ELF_TypeIndFunction:
243   case MCSA_ELF_TypeObject:
244   case MCSA_ELF_TypeTLS:
245   case MCSA_ELF_TypeCommon:
246   case MCSA_ELF_TypeNoType:
247   case MCSA_ELF_TypeGnuUniqueObject:
248   case MCSA_Hidden:
249   case MCSA_IndirectSymbol:
250   case MCSA_Internal:
251   case MCSA_Protected:
252   case MCSA_Weak:
253   case MCSA_Local:
254     llvm_unreachable("Invalid symbol attribute for Mach-O!");
255 
256   case MCSA_Global:
257     SD.setExternal(true);
258     // This effectively clears the undefined lazy bit, in Darwin 'as', although
259     // it isn't very consistent because it implements this as part of symbol
260     // lookup.
261     //
262     // FIXME: Cleanup this code, these bits should be emitted based on semantic
263     // properties, not on the order of definition, etc.
264     SD.setFlags(SD.getFlags() & ~SF_ReferenceTypeUndefinedLazy);
265     break;
266 
267   case MCSA_LazyReference:
268     // FIXME: This requires -dynamic.
269     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
270     if (Symbol->isUndefined())
271       SD.setFlags(SD.getFlags() | SF_ReferenceTypeUndefinedLazy);
272     break;
273 
274     // Since .reference sets the no dead strip bit, it is equivalent to
275     // .no_dead_strip in practice.
276   case MCSA_Reference:
277   case MCSA_NoDeadStrip:
278     SD.setFlags(SD.getFlags() | SF_NoDeadStrip);
279     break;
280 
281   case MCSA_SymbolResolver:
282     SD.setFlags(SD.getFlags() | SF_SymbolResolver);
283     break;
284 
285   case MCSA_PrivateExtern:
286     SD.setExternal(true);
287     SD.setPrivateExtern(true);
288     break;
289 
290   case MCSA_WeakReference:
291     // FIXME: This requires -dynamic.
292     if (Symbol->isUndefined())
293       SD.setFlags(SD.getFlags() | SF_WeakReference);
294     break;
295 
296   case MCSA_WeakDefinition:
297     // FIXME: 'as' enforces that this is defined and global. The manual claims
298     // it has to be in a coalesced section, but this isn't enforced.
299     SD.setFlags(SD.getFlags() | SF_WeakDefinition);
300     break;
301 
302   case MCSA_WeakDefAutoPrivate:
303     SD.setFlags(SD.getFlags() | SF_WeakDefinition | SF_WeakReference);
304     break;
305   }
306 }
307 
EmitSymbolDesc(MCSymbol * Symbol,unsigned DescValue)308 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
309   // Encode the 'desc' value into the lowest implementation defined bits.
310   assert(DescValue == (DescValue & SF_DescFlagsMask) &&
311          "Invalid .desc value!");
312   getAssembler().getOrCreateSymbolData(*Symbol).setFlags(
313     DescValue & SF_DescFlagsMask);
314 }
315 
EmitCommonSymbol(MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)316 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
317                                        unsigned ByteAlignment) {
318   // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
319   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
320 
321   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
322   SD.setExternal(true);
323   SD.setCommon(Size, ByteAlignment);
324 }
325 
EmitLocalCommonSymbol(MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)326 void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
327                                             unsigned ByteAlignment) {
328   // '.lcomm' is equivalent to '.zerofill'.
329   return EmitZerofill(getContext().getMachOSection("__DATA", "__bss",
330                                                    MCSectionMachO::S_ZEROFILL,
331                                                    0, SectionKind::getBSS()),
332                       Symbol, Size, ByteAlignment);
333 }
334 
EmitZerofill(const MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)335 void MCMachOStreamer::EmitZerofill(const MCSection *Section, MCSymbol *Symbol,
336                                    uint64_t Size, unsigned ByteAlignment) {
337   MCSectionData &SectData = getAssembler().getOrCreateSectionData(*Section);
338 
339   // The symbol may not be present, which only creates the section.
340   if (!Symbol)
341     return;
342 
343   // FIXME: Assert that this section has the zerofill type.
344 
345   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
346 
347   MCSymbolData &SD = getAssembler().getOrCreateSymbolData(*Symbol);
348 
349   // Emit an align fragment if necessary.
350   if (ByteAlignment != 1)
351     new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, &SectData);
352 
353   MCFragment *F = new MCFillFragment(0, 0, Size, &SectData);
354   SD.setFragment(F);
355 
356   Symbol->setSection(*Section);
357 
358   // Update the maximum alignment on the zero fill section if necessary.
359   if (ByteAlignment > SectData.getAlignment())
360     SectData.setAlignment(ByteAlignment);
361 }
362 
363 // This should always be called with the thread local bss section.  Like the
364 // .zerofill directive this doesn't actually switch sections on us.
EmitTBSSSymbol(const MCSection * Section,MCSymbol * Symbol,uint64_t Size,unsigned ByteAlignment)365 void MCMachOStreamer::EmitTBSSSymbol(const MCSection *Section, MCSymbol *Symbol,
366                                      uint64_t Size, unsigned ByteAlignment) {
367   EmitZerofill(Section, Symbol, Size, ByteAlignment);
368   return;
369 }
370 
EmitBytes(StringRef Data,unsigned AddrSpace)371 void MCMachOStreamer::EmitBytes(StringRef Data, unsigned AddrSpace) {
372   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
373   // MCObjectStreamer.
374   getOrCreateDataFragment()->getContents().append(Data.begin(), Data.end());
375 }
376 
EmitValueToAlignment(unsigned ByteAlignment,int64_t Value,unsigned ValueSize,unsigned MaxBytesToEmit)377 void MCMachOStreamer::EmitValueToAlignment(unsigned ByteAlignment,
378                                            int64_t Value, unsigned ValueSize,
379                                            unsigned MaxBytesToEmit) {
380   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
381   // MCObjectStreamer.
382   if (MaxBytesToEmit == 0)
383     MaxBytesToEmit = ByteAlignment;
384   new MCAlignFragment(ByteAlignment, Value, ValueSize, MaxBytesToEmit,
385                       getCurrentSectionData());
386 
387   // Update the maximum alignment on the current section if necessary.
388   if (ByteAlignment > getCurrentSectionData()->getAlignment())
389     getCurrentSectionData()->setAlignment(ByteAlignment);
390 }
391 
EmitCodeAlignment(unsigned ByteAlignment,unsigned MaxBytesToEmit)392 void MCMachOStreamer::EmitCodeAlignment(unsigned ByteAlignment,
393                                         unsigned MaxBytesToEmit) {
394   // TODO: This is exactly the same as WinCOFFStreamer. Consider merging into
395   // MCObjectStreamer.
396   if (MaxBytesToEmit == 0)
397     MaxBytesToEmit = ByteAlignment;
398   MCAlignFragment *F = new MCAlignFragment(ByteAlignment, 0, 1, MaxBytesToEmit,
399                                            getCurrentSectionData());
400   F->setEmitNops(true);
401 
402   // Update the maximum alignment on the current section if necessary.
403   if (ByteAlignment > getCurrentSectionData()->getAlignment())
404     getCurrentSectionData()->setAlignment(ByteAlignment);
405 }
406 
EmitInstToData(const MCInst & Inst)407 void MCMachOStreamer::EmitInstToData(const MCInst &Inst) {
408   MCDataFragment *DF = getOrCreateDataFragment();
409 
410   SmallVector<MCFixup, 4> Fixups;
411   SmallString<256> Code;
412   raw_svector_ostream VecOS(Code);
413   getAssembler().getEmitter().EncodeInstruction(Inst, VecOS, Fixups);
414   VecOS.flush();
415 
416   // Add the fixups and data.
417   for (unsigned i = 0, e = Fixups.size(); i != e; ++i) {
418     Fixups[i].setOffset(Fixups[i].getOffset() + DF->getContents().size());
419     DF->addFixup(Fixups[i]);
420   }
421   DF->getContents().append(Code.begin(), Code.end());
422 }
423 
FinishImpl()424 void MCMachOStreamer::FinishImpl() {
425   EmitFrames(true);
426 
427   // We have to set the fragment atom associations so we can relax properly for
428   // Mach-O.
429 
430   // First, scan the symbol table to build a lookup table from fragments to
431   // defining symbols.
432   DenseMap<const MCFragment*, MCSymbolData*> DefiningSymbolMap;
433   for (MCAssembler::symbol_iterator it = getAssembler().symbol_begin(),
434          ie = getAssembler().symbol_end(); it != ie; ++it) {
435     if (getAssembler().isSymbolLinkerVisible(it->getSymbol()) &&
436         it->getFragment()) {
437       // An atom defining symbol should never be internal to a fragment.
438       assert(it->getOffset() == 0 && "Invalid offset in atom defining symbol!");
439       DefiningSymbolMap[it->getFragment()] = it;
440     }
441   }
442 
443   // Set the fragment atom associations by tracking the last seen atom defining
444   // symbol.
445   for (MCAssembler::iterator it = getAssembler().begin(),
446          ie = getAssembler().end(); it != ie; ++it) {
447     MCSymbolData *CurrentAtom = 0;
448     for (MCSectionData::iterator it2 = it->begin(),
449            ie2 = it->end(); it2 != ie2; ++it2) {
450       if (MCSymbolData *SD = DefiningSymbolMap.lookup(it2))
451         CurrentAtom = SD;
452       it2->setAtom(CurrentAtom);
453     }
454   }
455 
456   this->MCObjectStreamer::FinishImpl();
457 }
458 
createMachOStreamer(MCContext & Context,MCAsmBackend & MAB,raw_ostream & OS,MCCodeEmitter * CE,bool RelaxAll)459 MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
460                                       raw_ostream &OS, MCCodeEmitter *CE,
461                                       bool RelaxAll) {
462   MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE);
463   if (RelaxAll)
464     S->getAssembler().setRelaxAll(true);
465   return S;
466 }
467