[ARM] Generate 8.1-m CSINC, CSNEG and CSINV instructions.
[llvm-core.git] / lib / MC / MCMachOStreamer.cpp
blob613f255a4ea47cfb9d19ce96288f07a6989010a2
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 //===----------------------------------------------------------------------===//
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>
40 using namespace llvm;
42 namespace {
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;
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();
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) {}
74 /// state management
75 void reset() override {
76 CreatedADWARFSection = false;
77 HasSectionLabel.clear();
78 MCObjectStreamer::reset();
81 /// @name MCStreamer Interface
82 /// @{
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;
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;
109 void EmitIdent(StringRef IdentString) override {
110 llvm_unreachable("macho doesn't support this directive");
113 void EmitLOHDirective(MCLOHType Kind, const MCLOHArgs &Args) override {
114 getAssembler().getLOHContainer().addDirective(Kind, Args);
117 void FinishImpl() override;
120 } // end anonymous namespace.
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();
128 if (SegName == "__LD" && SecName == "__compact_unwind")
129 return true;
131 if (SegName == "__IMPORT") {
132 if (SecName == "__jump_table")
133 return true;
135 if (SecName == "__pointers")
136 return true;
139 if (SegName == "__TEXT" && SecName == "__eh_frame")
140 return true;
142 if (SegName == "__DATA" && (SecName == "__nl_symbol_ptr" ||
143 SecName == "__thread_ptr"))
144 return true;
146 return false;
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");
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;
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);
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());
187 MCObjectStreamer::EmitLabel(Symbol, Loc);
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.
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();
199 void MCMachOStreamer::EmitAssignment(MCSymbol *Symbol, const MCExpr *Value) {
200 MCValue Res;
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();
209 MCObjectStreamer::EmitAssignment(Symbol, Value);
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);
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);
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;
247 void MCMachOStreamer::EmitLinkerOptions(ArrayRef<std::string> Options) {
248 getAssembler().getLinkerOptions().push_back(Options);
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;
271 void MCMachOStreamer::EmitVersionMin(MCVersionMinType Kind, unsigned Major,
272 unsigned Minor, unsigned Update,
273 VersionTuple SDKVersion) {
274 getAssembler().setVersionMin(Kind, Major, Minor, Update, SDKVersion);
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);
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();
291 bool MCMachOStreamer::EmitSymbolAttribute(MCSymbol *Sym,
292 MCSymbolAttr Attribute) {
293 MCSymbolMachO *Symbol = cast<MCSymbolMachO>(Sym);
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;
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);
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).
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 return false;
335 case MCSA_Global:
336 Symbol->setExternal(true);
337 // This effectively clears the undefined lazy bit, in Darwin 'as', although
338 // it isn't very consistent because it implements this as part of symbol
339 // lookup.
341 // FIXME: Cleanup this code, these bits should be emitted based on semantic
342 // properties, not on the order of definition, etc.
343 Symbol->setReferenceTypeUndefinedLazy(false);
344 break;
346 case MCSA_LazyReference:
347 // FIXME: This requires -dynamic.
348 Symbol->setNoDeadStrip();
349 if (Symbol->isUndefined())
350 Symbol->setReferenceTypeUndefinedLazy(true);
351 break;
353 // Since .reference sets the no dead strip bit, it is equivalent to
354 // .no_dead_strip in practice.
355 case MCSA_Reference:
356 case MCSA_NoDeadStrip:
357 Symbol->setNoDeadStrip();
358 break;
360 case MCSA_SymbolResolver:
361 Symbol->setSymbolResolver();
362 break;
364 case MCSA_AltEntry:
365 Symbol->setAltEntry();
366 break;
368 case MCSA_PrivateExtern:
369 Symbol->setExternal(true);
370 Symbol->setPrivateExtern(true);
371 break;
373 case MCSA_WeakReference:
374 // FIXME: This requires -dynamic.
375 if (Symbol->isUndefined())
376 Symbol->setWeakReference();
377 break;
379 case MCSA_WeakDefinition:
380 // FIXME: 'as' enforces that this is defined and global. The manual claims
381 // it has to be in a coalesced section, but this isn't enforced.
382 Symbol->setWeakDefinition();
383 break;
385 case MCSA_WeakDefAutoPrivate:
386 Symbol->setWeakDefinition();
387 Symbol->setWeakReference();
388 break;
390 case MCSA_Cold:
391 Symbol->setCold();
392 break;
395 return true;
398 void MCMachOStreamer::EmitSymbolDesc(MCSymbol *Symbol, unsigned DescValue) {
399 // Encode the 'desc' value into the lowest implementation defined bits.
400 getAssembler().registerSymbol(*Symbol);
401 cast<MCSymbolMachO>(Symbol)->setDesc(DescValue);
404 void MCMachOStreamer::EmitCommonSymbol(MCSymbol *Symbol, uint64_t Size,
405 unsigned ByteAlignment) {
406 // FIXME: Darwin 'as' does appear to allow redef of a .comm by itself.
407 assert(Symbol->isUndefined() && "Cannot define a symbol twice!");
409 getAssembler().registerSymbol(*Symbol);
410 Symbol->setExternal(true);
411 Symbol->setCommon(Size, ByteAlignment);
414 void MCMachOStreamer::EmitLocalCommonSymbol(MCSymbol *Symbol, uint64_t Size,
415 unsigned ByteAlignment) {
416 // '.lcomm' is equivalent to '.zerofill'.
417 return EmitZerofill(getContext().getObjectFileInfo()->getDataBSSSection(),
418 Symbol, Size, ByteAlignment);
421 void MCMachOStreamer::EmitZerofill(MCSection *Section, MCSymbol *Symbol,
422 uint64_t Size, unsigned ByteAlignment,
423 SMLoc Loc) {
424 // On darwin all virtual sections have zerofill type. Disallow the usage of
425 // .zerofill in non-virtual functions. If something similar is needed, use
426 // .space or .zero.
427 if (!Section->isVirtualSection()) {
428 getContext().reportError(
429 Loc, "The usage of .zerofill is restricted to sections of "
430 "ZEROFILL type. Use .zero or .space instead.");
431 return; // Early returning here shouldn't harm. EmitZeros should work on any
432 // section.
435 PushSection();
436 SwitchSection(Section);
438 // The symbol may not be present, which only creates the section.
439 if (Symbol) {
440 EmitValueToAlignment(ByteAlignment, 0, 1, 0);
441 EmitLabel(Symbol);
442 EmitZeros(Size);
444 PopSection();
447 // This should always be called with the thread local bss section. Like the
448 // .zerofill directive this doesn't actually switch sections on us.
449 void MCMachOStreamer::EmitTBSSSymbol(MCSection *Section, MCSymbol *Symbol,
450 uint64_t Size, unsigned ByteAlignment) {
451 EmitZerofill(Section, Symbol, Size, ByteAlignment);
454 void MCMachOStreamer::EmitInstToData(const MCInst &Inst,
455 const MCSubtargetInfo &STI) {
456 MCDataFragment *DF = getOrCreateDataFragment();
458 SmallVector<MCFixup, 4> Fixups;
459 SmallString<256> Code;
460 raw_svector_ostream VecOS(Code);
461 getAssembler().getEmitter().encodeInstruction(Inst, VecOS, Fixups, STI);
463 // Add the fixups and data.
464 for (MCFixup &Fixup : Fixups) {
465 Fixup.setOffset(Fixup.getOffset() + DF->getContents().size());
466 DF->getFixups().push_back(Fixup);
468 DF->setHasInstructions(STI);
469 DF->getContents().append(Code.begin(), Code.end());
472 void MCMachOStreamer::FinishImpl() {
473 EmitFrames(&getAssembler().getBackend());
475 // We have to set the fragment atom associations so we can relax properly for
476 // Mach-O.
478 // First, scan the symbol table to build a lookup table from fragments to
479 // defining symbols.
480 DenseMap<const MCFragment *, const MCSymbol *> DefiningSymbolMap;
481 for (const MCSymbol &Symbol : getAssembler().symbols()) {
482 if (getAssembler().isSymbolLinkerVisible(Symbol) && Symbol.isInSection() &&
483 !Symbol.isVariable()) {
484 // An atom defining symbol should never be internal to a fragment.
485 assert(Symbol.getOffset() == 0 &&
486 "Invalid offset in atom defining symbol!");
487 DefiningSymbolMap[Symbol.getFragment()] = &Symbol;
491 // Set the fragment atom associations by tracking the last seen atom defining
492 // symbol.
493 for (MCSection &Sec : getAssembler()) {
494 const MCSymbol *CurrentAtom = nullptr;
495 for (MCFragment &Frag : Sec) {
496 if (const MCSymbol *Symbol = DefiningSymbolMap.lookup(&Frag))
497 CurrentAtom = Symbol;
498 Frag.setAtom(CurrentAtom);
502 this->MCObjectStreamer::FinishImpl();
505 MCStreamer *llvm::createMachOStreamer(MCContext &Context,
506 std::unique_ptr<MCAsmBackend> &&MAB,
507 std::unique_ptr<MCObjectWriter> &&OW,
508 std::unique_ptr<MCCodeEmitter> &&CE,
509 bool RelaxAll, bool DWARFMustBeAtTheEnd,
510 bool LabelSections) {
511 MCMachOStreamer *S =
512 new MCMachOStreamer(Context, std::move(MAB), std::move(OW), std::move(CE),
513 DWARFMustBeAtTheEnd, LabelSections);
514 const Triple &Target = Context.getObjectFileInfo()->getTargetTriple();
515 S->EmitVersionForTarget(Target, Context.getObjectFileInfo()->getSDKVersion());
516 if (RelaxAll)
517 S->getAssembler().setRelaxAll(true);
518 return S;