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