xref: /freebsd-src/contrib/llvm-project/llvm/lib/MC/MCMachOStreamer.cpp (revision 0eae32dcef82f6f06de6419a0d623d7def0cc8f6)
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/MC/TargetRegistry.h"
34 #include "llvm/Support/Casting.h"
35 #include "llvm/Support/ErrorHandling.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 emitDarwinTargetVariantBuildVersion(unsigned Platform, unsigned Major,
96                                            unsigned Minor, unsigned Update,
97                                            VersionTuple SDKVersion) override;
98   void emitThumbFunc(MCSymbol *Func) override;
99   bool emitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
100   void emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
101   void emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
102                         unsigned ByteAlignment) override;
103 
104   void emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
105                              unsigned ByteAlignment) override;
106   void emitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
107                     uint64_t Size = 0, unsigned ByteAlignment = 0,
108                     SMLoc Loc = SMLoc()) override;
109   void emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
110                       unsigned ByteAlignment = 0) override;
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 
125 static 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.getName();
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                               SecName == "__thread_ptr"))
147     return true;
148 
149   return false;
150 }
151 
152 void MCMachOStreamer::changeSection(MCSection *Section,
153                                     const MCExpr *Subsection) {
154   // Change the section normally.
155   bool Created = changeSectionImpl(Section, Subsection);
156   const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
157   StringRef SegName = MSec.getSegmentName();
158   if (SegName == "__DWARF")
159     CreatedADWARFSection = true;
160   else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
161     assert((!CreatedADWARFSection ||
162             Section == getContext().getObjectFileInfo()->getStackMapSection())
163            && "Creating regular section after DWARF");
164 
165   // Output a linker-local symbol so we don't need section-relative local
166   // relocations. The linker hates us when we do that.
167   if (LabelSections && !HasSectionLabel[Section] &&
168       !Section->getBeginSymbol()) {
169     MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
170     Section->setBeginSymbol(Label);
171     HasSectionLabel[Section] = true;
172   }
173 }
174 
175 void MCMachOStreamer::emitEHSymAttributes(const MCSymbol *Symbol,
176                                           MCSymbol *EHSymbol) {
177   getAssembler().registerSymbol(*Symbol);
178   if (Symbol->isExternal())
179     emitSymbolAttribute(EHSymbol, MCSA_Global);
180   if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
181     emitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
182   if (Symbol->isPrivateExtern())
183     emitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
184 }
185 
186 void MCMachOStreamer::emitLabel(MCSymbol *Symbol, SMLoc Loc) {
187   // We have to create a new fragment if this is an atom defining symbol,
188   // fragments cannot span atoms.
189   if (getAssembler().isSymbolLinkerVisible(*Symbol))
190     insert(new MCDataFragment());
191 
192   MCObjectStreamer::emitLabel(Symbol, Loc);
193 
194   // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
195   // to clear the weak reference and weak definition bits too, but the
196   // implementation was buggy. For now we just try to match 'as', for
197   // diffability.
198   //
199   // FIXME: Cleanup this code, these bits should be emitted based on semantic
200   // properties, not on the order of definition, etc.
201   cast<MCSymbolMachO>(Symbol)->clearReferenceType();
202 }
203 
204 void MCMachOStreamer::emitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
205   MCValue Res;
206 
207   if (Value->evaluateAsRelocatable(Res, nullptr, nullptr)) {
208     if (const MCSymbolRefExpr *SymAExpr = Res.getSymA()) {
209       const MCSymbol &SymA = SymAExpr->getSymbol();
210       if (!Res.getSymB() && (SymA.getName() == "" || Res.getConstant() != 0))
211         cast<MCSymbolMachO>(Symbol)->setAltEntry();
212     }
213   }
214   MCObjectStreamer::emitAssignment(Symbol, Value);
215 }
216 
217 void MCMachOStreamer::emitDataRegion(DataRegionData::KindTy Kind) {
218   // Create a temporary label to mark the start of the data region.
219   MCSymbol *Start = getContext().createTempSymbol();
220   emitLabel(Start);
221   // Record the region for the object writer to use.
222   DataRegionData Data = { Kind, Start, nullptr };
223   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
224   Regions.push_back(Data);
225 }
226 
227 void MCMachOStreamer::emitDataRegionEnd() {
228   std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
229   assert(!Regions.empty() && "Mismatched .end_data_region!");
230   DataRegionData &Data = Regions.back();
231   assert(!Data.End && "Mismatched .end_data_region!");
232   // Create a temporary label to mark the end of the data region.
233   Data.End = getContext().createTempSymbol();
234   emitLabel(Data.End);
235 }
236 
237 void MCMachOStreamer::emitAssemblerFlag(MCAssemblerFlag Flag) {
238   // Let the target do whatever target specific stuff it needs to do.
239   getAssembler().getBackend().handleAssemblerFlag(Flag);
240   // Do any generic stuff we need to do.
241   switch (Flag) {
242   case MCAF_SyntaxUnified: return; // no-op here.
243   case MCAF_Code16: return; // Change parsing mode; no-op here.
244   case MCAF_Code32: return; // Change parsing mode; no-op here.
245   case MCAF_Code64: return; // Change parsing mode; no-op here.
246   case MCAF_SubsectionsViaSymbols:
247     getAssembler().setSubsectionsViaSymbols(true);
248     return;
249   }
250 }
251 
252 void MCMachOStreamer::emitLinkerOptions(ArrayRef<std::string> Options) {
253   getAssembler().getLinkerOptions().push_back(Options);
254 }
255 
256 void MCMachOStreamer::emitDataRegion(MCDataRegionType Kind) {
257   switch (Kind) {
258   case MCDR_DataRegion:
259     emitDataRegion(DataRegionData::Data);
260     return;
261   case MCDR_DataRegionJT8:
262     emitDataRegion(DataRegionData::JumpTable8);
263     return;
264   case MCDR_DataRegionJT16:
265     emitDataRegion(DataRegionData::JumpTable16);
266     return;
267   case MCDR_DataRegionJT32:
268     emitDataRegion(DataRegionData::JumpTable32);
269     return;
270   case MCDR_DataRegionEnd:
271     emitDataRegionEnd();
272     return;
273   }
274 }
275 
276 void MCMachOStreamer::emitVersionMin(MCVersionMinType Kind, unsigned Major,
277                                      unsigned Minor, unsigned Update,
278                                      VersionTuple SDKVersion) {
279   getAssembler().setVersionMin(Kind, Major, Minor, Update, SDKVersion);
280 }
281 
282 void MCMachOStreamer::emitBuildVersion(unsigned Platform, unsigned Major,
283                                        unsigned Minor, unsigned Update,
284                                        VersionTuple SDKVersion) {
285   getAssembler().setBuildVersion((MachO::PlatformType)Platform, Major, Minor,
286                                  Update, SDKVersion);
287 }
288 
289 void MCMachOStreamer::emitDarwinTargetVariantBuildVersion(
290     unsigned Platform, unsigned Major, unsigned Minor, unsigned Update,
291     VersionTuple SDKVersion) {
292   getAssembler().setDarwinTargetVariantBuildVersion(
293       (MachO::PlatformType)Platform, Major, Minor, Update, SDKVersion);
294 }
295 
296 void MCMachOStreamer::emitThumbFunc(MCSymbol *Symbol) {
297   // Remember that the function is a thumb function. Fixup and relocation
298   // values will need adjusted.
299   getAssembler().setIsThumbFunc(Symbol);
300   cast<MCSymbolMachO>(Symbol)->setThumbFunc();
301 }
302 
303 bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym,
304                                           MCSymbolAttr Attribute) {
305   MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
306 
307   // Indirect symbols are handled differently, to match how 'as' handles
308   // them. This makes writing matching .o files easier.
309   if (Attribute == MCSA_IndirectSymbol) {
310     // Note that we intentionally cannot use the symbol data here; this is
311     // important for matching the string table that 'as' generates.
312     IndirectSymbolData ISD;
313     ISD.Symbol = Symbol;
314     ISD.Section = getCurrentSectionOnly();
315     getAssembler().getIndirectSymbols().push_back(ISD);
316     return true;
317   }
318 
319   // Adding a symbol attribute always introduces the symbol, note that an
320   // important side effect of calling registerSymbol here is to register
321   // the symbol with the assembler.
322   getAssembler().registerSymbol(*Symbol);
323 
324   // The implementation of symbol attributes is designed to match 'as', but it
325   // leaves much to desired. It doesn't really make sense to arbitrarily add and
326   // remove flags, but 'as' allows this (in particular, see .desc).
327   //
328   // In the future it might be worth trying to make these operations more well
329   // defined.
330   switch (Attribute) {
331   case MCSA_Invalid:
332   case MCSA_ELF_TypeFunction:
333   case MCSA_ELF_TypeIndFunction:
334   case MCSA_ELF_TypeObject:
335   case MCSA_ELF_TypeTLS:
336   case MCSA_ELF_TypeCommon:
337   case MCSA_ELF_TypeNoType:
338   case MCSA_ELF_TypeGnuUniqueObject:
339   case MCSA_Extern:
340   case MCSA_Hidden:
341   case MCSA_IndirectSymbol:
342   case MCSA_Internal:
343   case MCSA_Protected:
344   case MCSA_Weak:
345   case MCSA_Local:
346   case MCSA_LGlobal:
347     return false;
348 
349   case MCSA_Global:
350     Symbol->setExternal(true);
351     // This effectively clears the undefined lazy bit, in Darwin 'as', although
352     // it isn't very consistent because it implements this as part of symbol
353     // lookup.
354     //
355     // FIXME: Cleanup this code, these bits should be emitted based on semantic
356     // properties, not on the order of definition, etc.
357     Symbol->setReferenceTypeUndefinedLazy(false);
358     break;
359 
360   case MCSA_LazyReference:
361     // FIXME: This requires -dynamic.
362     Symbol->setNoDeadStrip();
363     if (Symbol->isUndefined())
364       Symbol->setReferenceTypeUndefinedLazy(true);
365     break;
366 
367     // Since .reference sets the no dead strip bit, it is equivalent to
368     // .no_dead_strip in practice.
369   case MCSA_Reference:
370   case MCSA_NoDeadStrip:
371     Symbol->setNoDeadStrip();
372     break;
373 
374   case MCSA_SymbolResolver:
375     Symbol->setSymbolResolver();
376     break;
377 
378   case MCSA_AltEntry:
379     Symbol->setAltEntry();
380     break;
381 
382   case MCSA_PrivateExtern:
383     Symbol->setExternal(true);
384     Symbol->setPrivateExtern(true);
385     break;
386 
387   case MCSA_WeakReference:
388     // FIXME: This requires -dynamic.
389     if (Symbol->isUndefined())
390       Symbol->setWeakReference();
391     break;
392 
393   case MCSA_WeakDefinition:
394     // FIXME: 'as' enforces that this is defined and global. The manual claims
395     // it has to be in a coalesced section, but this isn't enforced.
396     Symbol->setWeakDefinition();
397     break;
398 
399   case MCSA_WeakDefAutoPrivate:
400     Symbol->setWeakDefinition();
401     Symbol->setWeakReference();
402     break;
403 
404   case MCSA_Cold:
405     Symbol->setCold();
406     break;
407   }
408 
409   return true;
410 }
411 
412 void MCMachOStreamer::emitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
413   // Encode the 'desc' value into the lowest implementation defined bits.
414   getAssembler().registerSymbol(*Symbol);
415   cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
416 }
417 
418 void MCMachOStreamer::emitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
419                                        unsigned ByteAlignment) {
420   // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
421   assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
422 
423   getAssembler().registerSymbol(*Symbol);
424   Symbol->setExternal(true);
425   Symbol->setCommon(Size, ByteAlignment);
426 }
427 
428 void MCMachOStreamer::emitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
429                                             unsigned ByteAlignment) {
430   // '.lcomm' is equivalent to '.zerofill'.
431   return emitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
432                       Symbol, Size, ByteAlignment);
433 }
434 
435 void MCMachOStreamer::emitZerofill(MCSection *Section, MCSymbol *Symbol,
436                                    uint64_t Size, unsigned ByteAlignment,
437                                    SMLoc Loc) {
438   // On darwin all virtual sections have zerofill type. Disallow the usage of
439   // .zerofill in non-virtual functions. If something similar is needed, use
440   // .space or .zero.
441   if (!Section->isVirtualSection()) {
442     getContext().reportError(
443         Loc, "The usage of .zerofill is restricted to sections of "
444              "ZEROFILL type. Use .zero or .space instead.");
445     return; // Early returning here shouldn't harm. EmitZeros should work on any
446             // section.
447   }
448 
449   PushSection();
450   SwitchSection(Section);
451 
452   // The symbol may not be present, which only creates the section.
453   if (Symbol) {
454     emitValueToAlignment(ByteAlignment, 0, 1, 0);
455     emitLabel(Symbol);
456     emitZeros(Size);
457   }
458   PopSection();
459 }
460 
461 // This should always be called with the thread local bss section.  Like the
462 // .zerofill directive this doesn't actually switch sections on us.
463 void MCMachOStreamer::emitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
464                                      uint64_t Size, unsigned ByteAlignment) {
465   emitZerofill(Section, Symbol, Size, ByteAlignment);
466 }
467 
468 void MCMachOStreamer::emitInstToData(const MCInst &Inst,
469                                      const MCSubtargetInfo &STI) {
470   MCDataFragment *DF = getOrCreateDataFragment();
471 
472   SmallVector<MCFixup, 4> Fixups;
473   SmallString<256> Code;
474   raw_svector_ostream VecOS(Code);
475   getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
476 
477   // Add the fixups and data.
478   for (MCFixup &Fixup : Fixups) {
479     Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
480     DF->getFixups().push_back(Fixup);
481   }
482   DF->setHasInstructions(STI);
483   DF->getContents().append(Code.begin(), Code.end());
484 }
485 
486 void MCMachOStreamer::finishImpl() {
487   emitFrames(&getAssembler().getBackend());
488 
489   // We have to set the fragment atom associations so we can relax properly for
490   // Mach-O.
491 
492   // First, scan the symbol table to build a lookup table from fragments to
493   // defining symbols.
494   DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
495   for (const MCSymbol &Symbol : getAssembler().symbols()) {
496     if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
497         !Symbol.isVariable()) {
498       // An atom defining symbol should never be internal to a fragment.
499       assert(Symbol.getOffset() == 0 &&
500              "Invalid offset in atom defining symbol!");
501       DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
502     }
503   }
504 
505   // Set the fragment atom associations by tracking the last seen atom defining
506   // symbol.
507   for (MCSection &Sec : getAssembler()) {
508     const MCSymbol *CurrentAtom = nullptr;
509     for (MCFragment &Frag : Sec) {
510       if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
511         CurrentAtom = Symbol;
512       Frag.setAtom(CurrentAtom);
513     }
514   }
515 
516   this->MCObjectStreamer::finishImpl();
517 }
518 
519 MCStreamer *llvm::createMachOStreamer(MCContext &Context,
520                                       std::unique_ptr<MCAsmBackend> &&MAB,
521                                       std::unique_ptr<MCObjectWriter> &&OW,
522                                       std::unique_ptr<MCCodeEmitter> &&CE,
523                                       bool RelaxAll, bool DWARFMustBeAtTheEnd,
524                                       bool LabelSections) {
525   MCMachOStreamer *S =
526       new MCMachOStreamer(Context, std::move(MAB), std::move(OW), std::move(CE),
527                           DWARFMustBeAtTheEnd, LabelSections);
528   const Triple &Target = Context.getTargetTriple();
529   S->emitVersionForTarget(
530       Target, Context.getObjectFileInfo()->getSDKVersion(),
531       Context.getObjectFileInfo()->getDarwinTargetVariantTriple(),
532       Context.getObjectFileInfo()->getDarwinTargetVariantSDKVersion());
533   if (RelaxAll)
534     S->getAssembler().setRelaxAll(true);
535   return S;
536 }
537