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