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