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