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/ADT/DenseMap.h"
11 #include "llvm/ADT/SmallString.h"
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/ADT/Triple.h"
15 #include "llvm/MC/MCAsmBackend.h"
16 #include "llvm/MC/MCAssembler.h"
17 #include "llvm/MC/MCCodeEmitter.h"
18 #include "llvm/MC/MCContext.h"
19 #include "llvm/MC/MCDirectives.h"
20 #include "llvm/MC/MCExpr.h"
21 #include "llvm/MC/MCFixup.h"
22 #include "llvm/MC/MCFragment.h"
23 #include "llvm/MC/MCInst.h"
24 #include "llvm/MC/MCLinkerOptimizationHint.h"
25 #include "llvm/MC/MCObjectFileInfo.h"
26 #include "llvm/MC/MCObjectStreamer.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"
44 class MCMachOStreamer : public MCObjectStreamer {
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.
51 bool DWARFMustBeAtTheEnd;
52 bool CreatedADWARFSection;
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;
59 void EmitInstToData(const MCInst &Inst, const MCSubtargetInfo &STI) override;
61 void EmitDataRegion(DataRegionData::KindTy Kind);
62 void EmitDataRegionEnd();
65 MCMachOStreamer(MCContext &Context, std::unique_ptr<MCAsmBackend> MAB,
66 raw_pwrite_stream &OS, std::unique_ptr<MCCodeEmitter> Emitter,
67 bool DWARFMustBeAtTheEnd, bool label)
68 : MCObjectStreamer(Context, std::move(MAB), OS, std::move(Emitter)),
69 LabelSections(label), DWARFMustBeAtTheEnd(DWARFMustBeAtTheEnd),
70 CreatedADWARFSection(false) {}
73 void reset() override {
74 CreatedADWARFSection = false;
75 HasSectionLabel.clear();
76 MCObjectStreamer::reset();
79 /// @name MCStreamer Interface
82 void ChangeSection(MCSection *Sect, const MCExpr *Subsect) override;
83 void EmitLabel(MCSymbol *Symbol, SMLoc Loc = SMLoc()) override;
84 void EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) override;
85 void EmitEHSymAttributes(const MCSymbol *Symbol, MCSymbol *EHSymbol) override;
86 void EmitAssemblerFlag(MCAssemblerFlag Flag) override;
87 void EmitLinkerOptions(ArrayRef<std::string> Options) override;
88 void EmitDataRegion(MCDataRegionType Kind) override;
89 void EmitVersionMin(MCVersionMinType Kind, unsigned Major,
90 unsigned Minor, unsigned Update) override;
91 void EmitBuildVersion(unsigned Platform, unsigned Major,
92 unsigned Minor, unsigned Update) override;
93 void EmitThumbFunc(MCSymbol *Func) override;
94 bool EmitSymbolAttribute(MCSymbol *Symbol, MCSymbolAttr Attribute) override;
95 void EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) override;
96 void EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
97 unsigned ByteAlignment) override;
99 void EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
100 unsigned ByteAlignment) override;
101 void EmitZerofill(MCSection *Section, MCSymbol *Symbol = nullptr,
102 uint64_t Size = 0, unsigned ByteAlignment = 0) override;
103 void EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol, uint64_t Size,
104 unsigned ByteAlignment = 0) override;
106 void EmitIdent(StringRef IdentString) override {
107 llvm_unreachable("macho doesn't support this directive");
110 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
111 getAssembler().getLOHContainer().addDirective(Kind, Args);
114 void FinishImpl() override;
117 } // end anonymous namespace.
119 static bool canGoAfterDWARF(const MCSectionMachO &MSec) {
120 // These sections are created by the assembler itself after the end of
122 StringRef SegName = MSec.getSegmentName();
123 StringRef SecName = MSec.getSectionName();
125 if (SegName == "__LD" && SecName == "__compact_unwind")
128 if (SegName == "__IMPORT") {
129 if (SecName == "__jump_table")
132 if (SecName == "__pointers")
136 if (SegName == "__TEXT" && SecName == "__eh_frame")
139 if (SegName == "__DATA" && (SecName == "__nl_symbol_ptr" ||
140 SecName == "__thread_ptr"))
146 void MCMachOStreamer::ChangeSection(MCSection *Section,
147 const MCExpr *Subsection) {
148 // Change the section normally.
149 bool Created = changeSectionImpl(Section, Subsection);
150 const MCSectionMachO &MSec = *cast<MCSectionMachO>(Section);
151 StringRef SegName = MSec.getSegmentName();
152 if (SegName == "__DWARF")
153 CreatedADWARFSection = true;
154 else if (Created && DWARFMustBeAtTheEnd && !canGoAfterDWARF(MSec))
155 assert(!CreatedADWARFSection && "Creating regular section after DWARF");
157 // Output a linker-local symbol so we don't need section-relative local
158 // relocations. The linker hates us when we do that.
159 if (LabelSections && !HasSectionLabel[Section] &&
160 !Section->getBeginSymbol()) {
161 MCSymbol *Label = getContext().createLinkerPrivateTempSymbol();
162 Section->setBeginSymbol(Label);
163 HasSectionLabel[Section] = true;
167 void MCMachOStreamer::EmitEHSymAttributes(const MCSymbol *Symbol,
168 MCSymbol *EHSymbol) {
169 getAssembler().registerSymbol(*Symbol);
170 if (Symbol->isExternal())
171 EmitSymbolAttribute(EHSymbol, MCSA_Global);
172 if (cast<MCSymbolMachO>(Symbol)->isWeakDefinition())
173 EmitSymbolAttribute(EHSymbol, MCSA_WeakDefinition);
174 if (Symbol->isPrivateExtern())
175 EmitSymbolAttribute(EHSymbol, MCSA_PrivateExtern);
178 void MCMachOStreamer::EmitLabel(MCSymbol *Symbol, SMLoc Loc) {
179 // We have to create a new fragment if this is an atom defining symbol,
180 // fragments cannot span atoms.
181 if (getAssembler().isSymbolLinkerVisible(*Symbol))
182 insert(new MCDataFragment());
184 MCObjectStreamer::EmitLabel(Symbol, Loc);
186 // This causes the reference type flag to be cleared. Darwin 'as' was "trying"
187 // to clear the weak reference and weak definition bits too, but the
188 // implementation was buggy. For now we just try to match 'as', for
191 // FIXME: Cleanup this code, these bits should be emitted based on semantic
192 // properties, not on the order of definition, etc.
193 cast<MCSymbolMachO>(Symbol)->clearReferenceType();
196 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
199 if (Value->evaluateAsRelocatable(Res, nullptr, nullptr)) {
200 if (const MCSymbolRefExpr *SymAExpr = Res.getSymA()) {
201 const MCSymbol &SymA = SymAExpr->getSymbol();
202 if (!Res.getSymB() && (SymA.getName() == "" || Res.getConstant() != 0))
203 cast<MCSymbolMachO>(Symbol)->setAltEntry();
206 MCObjectStreamer::EmitAssignment(Symbol, Value);
209 void MCMachOStreamer::EmitDataRegion(DataRegionData::KindTy Kind) {
210 // Create a temporary label to mark the start of the data region.
211 MCSymbol *Start = getContext().createTempSymbol();
213 // Record the region for the object writer to use.
214 DataRegionData Data = { Kind, Start, nullptr };
215 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
216 Regions.push_back(Data);
219 void MCMachOStreamer::EmitDataRegionEnd() {
220 std::vector<DataRegionData> &Regions = getAssembler().getDataRegions();
221 assert(!Regions.empty() && "Mismatched .end_data_region!");
222 DataRegionData &Data = Regions.back();
223 assert(!Data.End && "Mismatched .end_data_region!");
224 // Create a temporary label to mark the end of the data region.
225 Data.End = getContext().createTempSymbol();
229 void MCMachOStreamer::EmitAssemblerFlag(MCAssemblerFlag Flag) {
230 // Let the target do whatever target specific stuff it needs to do.
231 getAssembler().getBackend().handleAssemblerFlag(Flag);
232 // Do any generic stuff we need to do.
234 case MCAF_SyntaxUnified: return; // no-op here.
235 case MCAF_Code16: return; // Change parsing mode; no-op here.
236 case MCAF_Code32: return; // Change parsing mode; no-op here.
237 case MCAF_Code64: return; // Change parsing mode; no-op here.
238 case MCAF_SubsectionsViaSymbols:
239 getAssembler().setSubsectionsViaSymbols(true);
244 void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
245 getAssembler().getLinkerOptions().push_back(Options);
248 void MCMachOStreamer::EmitDataRegion(MCDataRegionType Kind) {
250 case MCDR_DataRegion:
251 EmitDataRegion(DataRegionData::Data);
253 case MCDR_DataRegionJT8:
254 EmitDataRegion(DataRegionData::JumpTable8);
256 case MCDR_DataRegionJT16:
257 EmitDataRegion(DataRegionData::JumpTable16);
259 case MCDR_DataRegionJT32:
260 EmitDataRegion(DataRegionData::JumpTable32);
262 case MCDR_DataRegionEnd:
268 void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
269 unsigned Minor, unsigned Update) {
270 getAssembler().setVersionMin(Kind, Major, Minor, Update);
273 void MCMachOStreamer::EmitBuildVersion(unsigned Platform, unsigned Major,
274 unsigned Minor, unsigned Update) {
275 getAssembler().setBuildVersion((MachO::PlatformType)Platform, Major, Minor,
279 void MCMachOStreamer::EmitThumbFunc(MCSymbol *Symbol) {
280 // Remember that the function is a thumb function. Fixup and relocation
281 // values will need adjusted.
282 getAssembler().setIsThumbFunc(Symbol);
283 cast<MCSymbolMachO>(Symbol)->setThumbFunc();
286 bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym,
287 MCSymbolAttr Attribute) {
288 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
290 // Indirect symbols are handled differently, to match how 'as' handles
291 // them. This makes writing matching .o files easier.
292 if (Attribute == MCSA_IndirectSymbol) {
293 // Note that we intentionally cannot use the symbol data here; this is
294 // important for matching the string table that 'as' generates.
295 IndirectSymbolData ISD;
297 ISD.Section = getCurrentSectionOnly();
298 getAssembler().getIndirectSymbols().push_back(ISD);
302 // Adding a symbol attribute always introduces the symbol, note that an
303 // important side effect of calling registerSymbol here is to register
304 // the symbol with the assembler.
305 getAssembler().registerSymbol(*Symbol);
307 // The implementation of symbol attributes is designed to match 'as', but it
308 // leaves much to desired. It doesn't really make sense to arbitrarily add and
309 // remove flags, but 'as' allows this (in particular, see .desc).
311 // In the future it might be worth trying to make these operations more well
315 case MCSA_ELF_TypeFunction:
316 case MCSA_ELF_TypeIndFunction:
317 case MCSA_ELF_TypeObject:
318 case MCSA_ELF_TypeTLS:
319 case MCSA_ELF_TypeCommon:
320 case MCSA_ELF_TypeNoType:
321 case MCSA_ELF_TypeGnuUniqueObject:
323 case MCSA_IndirectSymbol:
331 Symbol->setExternal(true);
332 // This effectively clears the undefined lazy bit, in Darwin 'as', although
333 // it isn't very consistent because it implements this as part of symbol
336 // FIXME: Cleanup this code, these bits should be emitted based on semantic
337 // properties, not on the order of definition, etc.
338 Symbol->setReferenceTypeUndefinedLazy(false);
341 case MCSA_LazyReference:
342 // FIXME: This requires -dynamic.
343 Symbol->setNoDeadStrip();
344 if (Symbol->isUndefined())
345 Symbol->setReferenceTypeUndefinedLazy(true);
348 // Since .reference sets the no dead strip bit, it is equivalent to
349 // .no_dead_strip in practice.
351 case MCSA_NoDeadStrip:
352 Symbol->setNoDeadStrip();
355 case MCSA_SymbolResolver:
356 Symbol->setSymbolResolver();
360 Symbol->setAltEntry();
363 case MCSA_PrivateExtern:
364 Symbol->setExternal(true);
365 Symbol->setPrivateExtern(true);
368 case MCSA_WeakReference:
369 // FIXME: This requires -dynamic.
370 if (Symbol->isUndefined())
371 Symbol->setWeakReference();
374 case MCSA_WeakDefinition:
375 // FIXME: 'as' enforces that this is defined and global. The manual claims
376 // it has to be in a coalesced section, but this isn't enforced.
377 Symbol->setWeakDefinition();
380 case MCSA_WeakDefAutoPrivate:
381 Symbol->setWeakDefinition();
382 Symbol->setWeakReference();
389 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
390 // Encode the 'desc' value into the lowest implementation defined bits.
391 getAssembler().registerSymbol(*Symbol);
392 cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
395 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
396 unsigned ByteAlignment) {
397 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
398 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
400 getAssembler().registerSymbol(*Symbol);
401 Symbol->setExternal(true);
402 Symbol->setCommon(Size, ByteAlignment);
405 void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
406 unsigned ByteAlignment) {
407 // '.lcomm' is equivalent to '.zerofill'.
408 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
409 Symbol, Size, ByteAlignment);
412 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
413 uint64_t Size, unsigned ByteAlignment) {
414 getAssembler().registerSection(*Section);
416 // The symbol may not be present, which only creates the section.
420 // On darwin all virtual sections have zerofill type.
421 assert(Section->isVirtualSection() && "Section does not have zerofill type!");
423 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
425 getAssembler().registerSymbol(*Symbol);
427 // Emit an align fragment if necessary.
428 if (ByteAlignment != 1)
429 new MCAlignFragment(ByteAlignment, 0, 0, ByteAlignment, Section);
431 MCFragment *F = new MCFillFragment(0, Size, Section);
432 Symbol->setFragment(F);
434 // Update the maximum alignment on the zero fill section if necessary.
435 if (ByteAlignment > Section->getAlignment())
436 Section->setAlignment(ByteAlignment);
439 // This should always be called with the thread local bss section. Like the
440 // .zerofill directive this doesn't actually switch sections on us.
441 void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
442 uint64_t Size, unsigned ByteAlignment) {
443 EmitZerofill(Section, Symbol, Size, ByteAlignment);
446 void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
447 const MCSubtargetInfo &STI) {
448 MCDataFragment *DF = getOrCreateDataFragment();
450 SmallVector<MCFixup, 4> Fixups;
451 SmallString<256> Code;
452 raw_svector_ostream VecOS(Code);
453 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
455 // Add the fixups and data.
456 for (MCFixup &Fixup : Fixups) {
457 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
458 DF->getFixups().push_back(Fixup);
460 DF->getContents().append(Code.begin(), Code.end());
463 void MCMachOStreamer::FinishImpl() {
464 EmitFrames(&getAssembler().getBackend());
466 // We have to set the fragment atom associations so we can relax properly for
469 // First, scan the symbol table to build a lookup table from fragments to
471 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
472 for (const MCSymbol &Symbol : getAssembler().symbols()) {
473 if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
474 !Symbol.isVariable()) {
475 // An atom defining symbol should never be internal to a fragment.
476 assert(Symbol.getOffset() == 0 &&
477 "Invalid offset in atom defining symbol!");
478 DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
482 // Set the fragment atom associations by tracking the last seen atom defining
484 for (MCSection &Sec : getAssembler()) {
485 const MCSymbol *CurrentAtom = nullptr;
486 for (MCFragment &Frag : Sec) {
487 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
488 CurrentAtom = Symbol;
489 Frag.setAtom(CurrentAtom);
493 this->MCObjectStreamer::FinishImpl();
496 MCStreamer *llvm::createMachOStreamer(MCContext &Context,
497 std::unique_ptr<MCAsmBackend> &&MAB,
498 raw_pwrite_stream &OS,
499 std::unique_ptr<MCCodeEmitter> &&CE,
500 bool RelaxAll, bool DWARFMustBeAtTheEnd,
501 bool LabelSections) {
503 new MCMachOStreamer(Context, std::move(MAB), OS, std::move(CE),
504 DWARFMustBeAtTheEnd, LabelSections);
505 const Triple &Target = Context.getObjectFileInfo()->getTargetTriple();
506 S->EmitVersionForTarget(Target);
508 S->getAssembler().setRelaxAll(true);