1 //===-- MCMachOStreamer.cpp - MachO Streamer ------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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"
36 class MCMachOStreamer : public MCObjectStreamer {
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.
43 bool DWARFMustBeAtTheEnd;
44 bool CreatedADWARFSection;
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;
51 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
53 void EmitDataRegion(DataRegionData::KindTy Kind);
54 void EmitDataRegionEnd();
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) {}
63 void reset() override {
64 CreatedADWARFSection = false;
65 HasSectionLabel.clear();
66 MCObjectStreamer::reset();
69 /// @name MCStreamer Interface
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");
89 void EmitCOFFSymbolStorageClass(int StorageClass) override {
90 llvm_unreachable("macho doesn't support this directive");
92 void EmitCOFFSymbolType(int Type) override {
93 llvm_unreachable("macho doesn't support this directive");
95 void EndCOFFSymbolDef() override {
96 llvm_unreachable("macho doesn't support this directive");
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;
105 void EmitFileDirective(StringRef Filename) override {
106 // FIXME: Just ignore the .file; it isn't important enough to fail the
109 // report_fatal_error("unsupported directive: '.file'");
112 void EmitIdent(StringRef IdentString) override {
113 llvm_unreachable("macho doesn't support this directive");
116 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
117 getAssembler().getLOHContainer().addDirective(Kind, Args);
120 void FinishImpl() override;
123 } // end anonymous namespace.
125 static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
126 // These sections are created by the assembler itself after the end of
128 StringRef SegName = MSec.getSegmentName();
129 StringRef SecName = MSec.getSectionName();
131 if (SegName == "__LD" && SecName == "__compact_unwind")
134 if (SegName == "__IMPORT") {
135 if (SecName == "__jump_table")
138 if (SecName == "__pointers")
142 if (SegName == "__TEXT" && SecName == "__eh_frame")
145 if (SegName == "__DATA" && SecName == "__nl_symbol_ptr")
151 void 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");
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;
172 void 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);
183 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol) {
184 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
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());
191 MCObjectStreamer::EmitLabel(Symbol);
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
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();
203 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
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();
213 MCObjectStreamer::EmitAssignment(Symbol, Value);
216 void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
217 // Create a temporary label to mark the start of the data region.
218 MCSymbol *Start = getContext().createTempSymbol();
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);
226 void 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();
236 void 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.
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);
251 void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
252 getAssembler().getLinkerOptions().push_back(Options);
255 void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
257 case MCDR_DataRegion:
258 EmitDataRegion(DataRegionData::Data);
260 case MCDR_DataRegionJT8:
261 EmitDataRegion(DataRegionData::JumpTable8);
263 case MCDR_DataRegionJT16:
264 EmitDataRegion(DataRegionData::JumpTable16);
266 case MCDR_DataRegionJT32:
267 EmitDataRegion(DataRegionData::JumpTable32);
269 case MCDR_DataRegionEnd:
275 void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
276 unsigned Minor, unsigned Update) {
277 getAssembler().setVersionMinInfo(Kind, Major, Minor, Update);
280 void 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();
287 bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym,
288 MCSymbolAttr Attribute) {
289 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
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;
298 ISD.Section = getCurrentSectionOnly();
299 getAssembler().getIndirectSymbols().push_back(ISD);
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);
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).
312 // In the future it might be worth trying to make these operations more well
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:
324 case MCSA_IndirectSymbol:
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
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);
342 case MCSA_LazyReference:
343 // FIXME: This requires -dynamic.
344 Symbol->setNoDeadStrip();
345 if (Symbol->isUndefined())
346 Symbol->setReferenceTypeUndefinedLazy(true);
349 // Since .reference sets the no dead strip bit, it is equivalent to
350 // .no_dead_strip in practice.
352 case MCSA_NoDeadStrip:
353 Symbol->setNoDeadStrip();
356 case MCSA_SymbolResolver:
357 Symbol->setSymbolResolver();
361 Symbol->setAltEntry();
364 case MCSA_PrivateExtern:
365 Symbol->setExternal(true);
366 Symbol->setPrivateExtern(true);
369 case MCSA_WeakReference:
370 // FIXME: This requires -dynamic.
371 if (Symbol->isUndefined())
372 Symbol->setWeakReference();
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();
381 case MCSA_WeakDefAutoPrivate:
382 Symbol->setWeakDefinition();
383 Symbol->setWeakReference();
390 void 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);
396 void 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!");
401 getAssembler().registerSymbol(*Symbol);
402 Symbol->setExternal(true);
403 Symbol->setCommon(Size, ByteAlignment);
406 void 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);
413 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
414 uint64_t Size, unsigned ByteAlignment) {
415 getAssembler().registerSection(*Section);
417 // The symbol may not be present, which only creates the section.
421 // On darwin all virtual sections have zerofill type.
422 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
424 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
426 getAssembler().registerSymbol(*Symbol);
428 // Emit an align fragment if necessary.
429 if (ByteAlignment != 1)
430 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, Section);
432 MCFragment *F = new MCFillFragment(0, Size, Section);
433 Symbol->setFragment(F);
435 // Update the maximum alignment on the zero fill section if necessary.
436 if (ByteAlignment > Section->getAlignment())
437 Section->setAlignment(ByteAlignment);
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.
442 void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
443 uint64_t Size, unsigned ByteAlignment) {
444 EmitZerofill(Section, Symbol, Size, ByteAlignment);
447 void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
448 const MCSubtargetInfo &STI) {
449 MCDataFragment *DF = getOrCreateDataFragment();
451 SmallVector<MCFixup, 4> Fixups;
452 SmallString<256> Code;
453 raw_svector_ostream VecOS(Code);
454 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
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);
461 DF->getContents().append(Code.begin(), Code.end());
464 void MCMachOStreamer::FinishImpl() {
465 EmitFrames(&getAssembler().getBackend());
467 // We have to set the fragment atom associations so we can relax properly for
470 // First, scan the symbol table to build a lookup table from fragments to
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;
483 // Set the fragment atom associations by tracking the last seen atom defining
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);
494 this->MCObjectStreamer::FinishImpl();
497 MCStreamer *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.
509 MCVersionMinType VersionType;
511 VersionType = MCVM_WatchOSVersionMin;
512 else if (TT.isTvOS())
513 VersionType = MCVM_TvOSVersionMin;
514 else if (TT.isMacOSX())
515 VersionType = MCVM_OSXVersionMin;
517 assert(TT.isiOS() && "Must only be iOS platform left");
518 VersionType = MCVM_IOSVersionMin;
520 S->EmitVersionMin(VersionType, Major, Minor, Update);
524 S->getAssembler().setRelaxAll(true);