MC: Make sure to clear *all* of MCMachOStreamer's state
[oota-llvm.git] / lib / MC / MCMachOStreamer.cpp
1 //===-- MCMachOStreamer.cpp - MachO Streamer ------------------------------===//
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 #include "llvm/ADT/DenseMap.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/MC/MCAsmBackend.h"
14 #include "llvm/MC/MCAssembler.h"
15 #include "llvm/MC/MCCodeEmitter.h"
16 #include "llvm/MC/MCContext.h"
17 #include "llvm/MC/MCDwarf.h"
18 #include "llvm/MC/MCExpr.h"
19 #include "llvm/MC/MCInst.h"
20 #include "llvm/MC/MCLinkerOptimizationHint.h"
21 #include "llvm/MC/MCObjectFileInfo.h"
22 #include "llvm/MC/MCObjectStreamer.h"
23 #include "llvm/MC/MCSection.h"
24 #include "llvm/MC/MCSectionMachO.h"
25 #include "llvm/MC/MCSymbolMachO.h"
26 #include "llvm/Support/Dwarf.h"
27 #include "llvm/Support/ErrorHandling.h"
28 #include "llvm/Support/TargetRegistry.h"
29 #include "llvm/Support/raw_ostream.h"
30
31 using namespace llvm;
32
33 namespace {
34
35 class MCMachOStreamer : public MCObjectStreamer {
36 private:
37   /// LabelSections - true if each section change should emit a linker local
38   /// label for use in relocations for assembler local references. Obviates the
39   /// need for local relocations. False by default.
40   bool LabelSections;
41
42   bool DWARFMustBeAtTheEnd;
43   bool CreatedADWARFSection;
44
45   /// HasSectionLabel - map of which sections have already had a non-local
46   /// label emitted to them. Used so we don't emit extraneous linker local
47   /// labels in the middle of the section.
48   DenseMap<const MCSection*, bool> HasSectionLabel;
49
50   void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
51
52   void EmitDataRegion(DataRegionData::KindTy Kind);
53   void EmitDataRegionEnd();
54
55 public:
56   MCMachOStreamer(MCContext &Context, MCAsmBackend &MAB, raw_pwrite_stream &OS,
57                   MCCodeEmitter *Emitter, bool DWARFMustBeAtTheEnd, bool label)
58       : MCObjectStreamer(Context, MAB, OS, Emitter), LabelSections(label),
59         DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd), CreatedADWARFSection(false) {}
60
61   /// state management
62   void reset() override {
63     CreatedADWARFSection = false;
64     HasSectionLabel.clear();
65     MCObjectStreamer::reset();
66   }
67
68   /// @name MCStreamer Interface
69   /// @{
70
71   void ChangeSection(MCSection *Sect, const MCExpr *Subsect) override;
72   void EmitLabel(MCSymbol *Symbol) override;
73   void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
74   void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
75   void EmitLinkerOptions(ArrayRef<std::string> Options) override;
76   void EmitDataRegion(MCDataRegionType Kind) override;
77   void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
78                       unsigned Minor, unsigned Update) override;
79   void EmitThumbFunc(MCSymbol *Func) override;
80   bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
81   void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
82   void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
83                         unsigned ByteAlignment) override;
84   void BeginCOFFSymbolDef(const MCSymbol *Symbol) override {
85     llvm_unreachable("macho doesn't support this directive");
86   }
87   void EmitCOFFSymbolStorageClass(int StorageClass) override {
88     llvm_unreachable("macho doesn't support this directive");
89   }
90   void EmitCOFFSymbolType(int Type) override {
91     llvm_unreachable("macho doesn't support this directive");
92   }
93   void EndCOFFSymbolDef() override {
94     llvm_unreachable("macho doesn't support this directive");
95   }
96   void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
97                              unsigned ByteAlignment) override;
98   void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
99                     uint64_t Size = 0, unsigned ByteAlignment = 0) override;
100   void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
101                       unsigned ByteAlignment = 0) override;
102
103   void EmitFileDirective(StringRef Filename) override {
104     // FIXME: Just ignore the .file; it isn't important enough to fail the
105     // entire assembly.
106
107     // report_fatal_error("unsupported directive: '.file'");
108   }
109
110   void EmitIdent(StringRef IdentString) override {
111     llvm_unreachable("macho doesn't support this directive");
112   }
113
114   void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
115     getAssembler().getLOHContainer().addDirective(Kind, Args);
116   }
117
118   void FinishImpl() override;
119 };
120
121 } // end anonymous namespace.
122
123 static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
124   // These sections are created by the assembler itself after the end of
125   // the .s file.
126   StringRef SegName = MSec.getSegmentName();
127   StringRef SecName = MSec.getSectionName();
128
129   if (SegName == "__LD" && SecName == "__compact_unwind")
130     return true;
131
132   if (SegName == "__IMPORT") {
133     if (SecName == "__jump_table")
134       return true;
135
136     if (SecName == "__pointers")
137       return true;
138   }
139
140   if (SegName == "__TEXT" && SecName == "__eh_frame")
141     return true;
142
143   if (SegName == "__DATA" && SecName == "__nl_symbol_ptr")
144     return true;
145
146   return false;
147 }
148
149 void MCMachOStreamer::ChangeSection(MCSection *Section,
150                                     const MCExpr *Subsection) {
151   // Change the section normally.
152   bool Created = MCObjectStreamer::changeSectionImpl(Section, Subsection);
153   const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
154   StringRef SegName = MSec.getSegmentName();
155   if (SegName == "__DWARF")
156     CreatedADWARFSection = true;
157   else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
158     assert(!CreatedADWARFSection && "Creating regular section after DWARF");
159
160   // Output a linker-local symbol so we don't need section-relative local
161   // relocations. The linker hates us when we do that.
162   if (LabelSections && !HasSectionLabel[Section] &&
163       !Section->getBeginSymbol()) {
164     MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
165     Section->setBeginSymbol(Label);
166     HasSectionLabel[Section] = true;
167   }
168 }
169
170 void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
171                                           MCSymbol *EHSymbol) {
172   getAssembler().registerSymbol(*Symbol);
173   if (Symbol->isExternal())
174     EmitSymbolAttribute(EHSymbol, MCSA_Global);
175   if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
176     EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
177   if (Symbol->isPrivateExtern())
178     EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
179 }
180
181 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
182   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
183
184   // We have to create a new fragment if this is an atom defining symbol,
185   // fragments cannot span atoms.
186   if (getAssembler().isSymbolLinkerVisible(*Symbol))
187     insert(new MCDataFragment());
188
189   MCObjectStreamer::EmitLabel(Symbol);
190
191   // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
192   // to clear the weak reference and weak definition bits too, but the
193   // implementation was buggy. For now we just try to match 'as', for
194   // diffability.
195   //
196   // FIXME: Cleanup this code, these bits should be emitted based on semantic
197   // properties, not on the order of definition, etc.
198   cast<MCSymbolMachO>(Symbol)->clearReferenceType();
199 }
200
201 void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
202   if (!getAssembler().getBackend().hasDataInCodeSupport())
203     return;
204   // Create a temporary label to mark the start of the data region.
205   MCSymbol *Start = getContext().createTempSymbol();
206   EmitLabel(Start);
207   // Record the region for the object writer to use.
208   DataRegionData Data = { Kind, Start, nullptr };
209   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
210   Regions.push_back(Data);
211 }
212
213 void MCMachOStreamer::EmitDataRegionEnd() {
214   if (!getAssembler().getBackend().hasDataInCodeSupport())
215     return;
216   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
217   assert(!Regions.empty() && "Mismatched .end_data_region!");
218   DataRegionData &Data = Regions.back();
219   assert(!Data.End && "Mismatched .end_data_region!");
220   // Create a temporary label to mark the end of the data region.
221   Data.End = getContext().createTempSymbol();
222   EmitLabel(Data.End);
223 }
224
225 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
226   // Let the target do whatever target specific stuff it needs to do.
227   getAssembler().getBackend().handleAssemblerFlag(Flag);
228   // Do any generic stuff we need to do.
229   switch (Flag) {
230   case MCAF_SyntaxUnified: return; // no-op here.
231   case MCAF_Code16: return; // Change parsing mode; no-op here.
232   case MCAF_Code32: return; // Change parsing mode; no-op here.
233   case MCAF_Code64: return; // Change parsing mode; no-op here.
234   case MCAF_SubsectionsViaSymbols:
235     getAssembler().setSubsectionsViaSymbols(true);
236     return;
237   }
238 }
239
240 void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
241   getAssembler().getLinkerOptions().push_back(Options);
242 }
243
244 void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
245   switch (Kind) {
246   case MCDR_DataRegion:
247     EmitDataRegion(DataRegionData::Data);
248     return;
249   case MCDR_DataRegionJT8:
250     EmitDataRegion(DataRegionData::JumpTable8);
251     return;
252   case MCDR_DataRegionJT16:
253     EmitDataRegion(DataRegionData::JumpTable16);
254     return;
255   case MCDR_DataRegionJT32:
256     EmitDataRegion(DataRegionData::JumpTable32);
257     return;
258   case MCDR_DataRegionEnd:
259     EmitDataRegionEnd();
260     return;
261   }
262 }
263
264 void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
265                                      unsigned Minor, unsigned Update) {
266   getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
267 }
268
269 void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
270   // Remember that the function is a thumb function. Fixup and relocation
271   // values will need adjusted.
272   getAssembler().setIsThumbFunc(Symbol);
273   cast<MCSymbolMachO>(Symbol)->setThumbFunc();
274 }
275
276 bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym,
277                                           MCSymbolAttr Attribute) {
278   MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
279
280   // Indirect symbols are handled differently, to match how 'as' handles
281   // them. This makes writing matching .o files easier.
282   if (Attribute == MCSA_IndirectSymbol) {
283     // Note that we intentionally cannot use the symbol data here; this is
284     // important for matching the string table that 'as' generates.
285     IndirectSymbolData ISD;
286     ISD.Symbol = Symbol;
287     ISD.Section = getCurrentSectionOnly();
288     getAssembler().getIndirectSymbols().push_back(ISD);
289     return true;
290   }
291
292   // Adding a symbol attribute always introduces the symbol, note that an
293   // important side effect of calling registerSymbol here is to register
294   // the symbol with the assembler.
295   getAssembler().registerSymbol(*Symbol);
296
297   // The implementation of symbol attributes is designed to match 'as', but it
298   // leaves much to desired. It doesn't really make sense to arbitrarily add and
299   // remove flags, but 'as' allows this (in particular, see .desc).
300   //
301   // In the future it might be worth trying to make these operations more well
302   // defined.
303   switch (Attribute) {
304   case MCSA_Invalid:
305   case MCSA_ELF_TypeFunction:
306   case MCSA_ELF_TypeIndFunction:
307   case MCSA_ELF_TypeObject:
308   case MCSA_ELF_TypeTLS:
309   case MCSA_ELF_TypeCommon:
310   case MCSA_ELF_TypeNoType:
311   case MCSA_ELF_TypeGnuUniqueObject:
312   case MCSA_Hidden:
313   case MCSA_IndirectSymbol:
314   case MCSA_Internal:
315   case MCSA_Protected:
316   case MCSA_Weak:
317   case MCSA_Local:
318     return false;
319
320   case MCSA_Global:
321     Symbol->setExternal(true);
322     // This effectively clears the undefined lazy bit, in Darwin 'as', although
323     // it isn't very consistent because it implements this as part of symbol
324     // lookup.
325     //
326     // FIXME: Cleanup this code, these bits should be emitted based on semantic
327     // properties, not on the order of definition, etc.
328     Symbol->setReferenceTypeUndefinedLazy(false);
329     break;
330
331   case MCSA_LazyReference:
332     // FIXME: This requires -dynamic.
333     Symbol->setNoDeadStrip();
334     if (Symbol->isUndefined())
335       Symbol->setReferenceTypeUndefinedLazy(true);
336     break;
337
338     // Since .reference sets the no dead strip bit, it is equivalent to
339     // .no_dead_strip in practice.
340   case MCSA_Reference:
341   case MCSA_NoDeadStrip:
342     Symbol->setNoDeadStrip();
343     break;
344
345   case MCSA_SymbolResolver:
346     Symbol->setSymbolResolver();
347     break;
348
349   case MCSA_PrivateExtern:
350     Symbol->setExternal(true);
351     Symbol->setPrivateExtern(true);
352     break;
353
354   case MCSA_WeakReference:
355     // FIXME: This requires -dynamic.
356     if (Symbol->isUndefined())
357       Symbol->setWeakReference();
358     break;
359
360   case MCSA_WeakDefinition:
361     // FIXME: 'as' enforces that this is defined and global. The manual claims
362     // it has to be in a coalesced section, but this isn't enforced.
363     Symbol->setWeakDefinition();
364     break;
365
366   case MCSA_WeakDefAutoPrivate:
367     Symbol->setWeakDefinition();
368     Symbol->setWeakReference();
369     break;
370   }
371
372   return true;
373 }
374
375 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
376   // Encode the 'desc' value into the lowest implementation defined bits.
377   getAssembler().registerSymbol(*Symbol);
378   cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
379 }
380
381 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
382                                        unsigned ByteAlignment) {
383   // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
384   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
385
386   getAssembler().registerSymbol(*Symbol);
387   Symbol->setExternal(true);
388   Symbol->setCommon(Size, ByteAlignment);
389 }
390
391 void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
392                                             unsigned ByteAlignment) {
393   // '.lcomm' is equivalent to '.zerofill'.
394   return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
395                       Symbol, Size, ByteAlignment);
396 }
397
398 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
399                                    uint64_t Size, unsigned ByteAlignment) {
400   getAssembler().registerSection(*Section);
401
402   // The symbol may not be present, which only creates the section.
403   if (!Symbol)
404     return;
405
406   // On darwin all virtual sections have zerofill type.
407   assert(Section->isVirtualSection() && "Section does not have zerofill type!");
408
409   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
410
411   getAssembler().registerSymbol(*Symbol);
412
413   // Emit an align fragment if necessary.
414   if (ByteAlignment != 1)
415     new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, Section);
416
417   MCFragment *F = new MCFillFragment(0, 0, Size, Section);
418   Symbol->setFragment(F);
419
420   // Update the maximum alignment on the zero fill section if necessary.
421   if (ByteAlignment > Section->getAlignment())
422     Section->setAlignment(ByteAlignment);
423 }
424
425 // This should always be called with the thread local bss section.  Like the
426 // .zerofill directive this doesn't actually switch sections on us.
427 void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
428                                      uint64_t Size, unsigned ByteAlignment) {
429   EmitZerofill(Section, Symbol, Size, ByteAlignment);
430   return;
431 }
432
433 void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
434                                      const MCSubtargetInfo &STI) {
435   MCDataFragment *DF = getOrCreateDataFragment();
436
437   SmallVector<MCFixup, 4> Fixups;
438   SmallString<256> Code;
439   raw_svector_ostream VecOS(Code);
440   getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
441
442   // Add the fixups and data.
443   for (MCFixup &Fixup : Fixups) {
444     Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
445     DF->getFixups().push_back(Fixup);
446   }
447   DF->getContents().append(Code.begin(), Code.end());
448 }
449
450 void MCMachOStreamer::FinishImpl() {
451   EmitFrames(&getAssembler().getBackend());
452
453   // We have to set the fragment atom associations so we can relax properly for
454   // Mach-O.
455
456   // First, scan the symbol table to build a lookup table from fragments to
457   // defining symbols.
458   DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
459   for (const MCSymbol &Symbol : getAssembler().symbols()) {
460     if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
461         !Symbol.isVariable()) {
462       // An atom defining symbol should never be internal to a fragment.
463       assert(Symbol.getOffset() == 0 &&
464              "Invalid offset in atom defining symbol!");
465       DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
466     }
467   }
468
469   // Set the fragment atom associations by tracking the last seen atom defining
470   // symbol.
471   for (MCSection &Sec : getAssembler()) {
472     const MCSymbol *CurrentAtom = nullptr;
473     for (MCFragment &Frag : Sec) {
474       if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
475         CurrentAtom = Symbol;
476       Frag.setAtom(CurrentAtom);
477     }
478   }
479
480   this->MCObjectStreamer::FinishImpl();
481 }
482
483 MCStreamer *llvm::createMachOStreamer(MCContext &Context, MCAsmBackend &MAB,
484                                       raw_pwrite_stream &OS, MCCodeEmitter *CE,
485                                       bool RelaxAll, bool DWARFMustBeAtTheEnd,
486                                       bool LabelSections) {
487   MCMachOStreamer *S = new MCMachOStreamer(Context, MAB, OS, CE,
488                                            DWARFMustBeAtTheEnd, LabelSections);
489   const Triple &TT = Context.getObjectFileInfo()->getTargetTriple();
490   if (TT.isOSDarwin()) {
491     unsigned Major, Minor, Update;
492     TT.getOSVersion(Major, Minor, Update);
493     // If there is a version specified, Major will be non-zero.
494     if (Major) {
495       MCVersionMinType VersionType;
496       if (TT.isWatchOS())
497         VersionType = MCVM_WatchOSVersionMin;
498       else if (TT.isTvOS())
499         VersionType = MCVM_TvOSVersionMin;
500       else if (TT.isMacOSX())
501         VersionType = MCVM_OSXVersionMin;
502       else {
503         assert(TT.isiOS() && "Must only be iOS platform left");
504         VersionType = MCVM_IOSVersionMin;
505       }
506       S->EmitVersionMin(VersionType, Major, Minor, Update);
507     }
508   }
509   if (RelaxAll)
510     S->getAssembler().setRelaxAll(true);
511   return S;
512 }