1 //===- MCAsmBackend.cpp - Target MC Assembly Backend ----------------------===// 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/MC/MCAsmBackend.h" 10 #include "llvm/MC/MCDXContainerWriter.h" 11 #include "llvm/MC/MCELFObjectWriter.h" 12 #include "llvm/MC/MCFixupKindInfo.h" 13 #include "llvm/MC/MCMachObjectWriter.h" 14 #include "llvm/MC/MCObjectWriter.h" 15 #include "llvm/MC/MCSPIRVObjectWriter.h" 16 #include "llvm/MC/MCWasmObjectWriter.h" 17 #include "llvm/MC/MCWinCOFFObjectWriter.h" 18 #include "llvm/MC/MCXCOFFObjectWriter.h" 19 #include <cassert> 20 #include <cstddef> 21 #include <cstdint> 22 23 using namespace llvm; 24 25 MCAsmBackend::MCAsmBackend(support::endianness Endian, unsigned RelaxFixupKind) 26 : Endian(Endian), RelaxFixupKind(RelaxFixupKind) {} 27 28 MCAsmBackend::~MCAsmBackend() = default; 29 30 std::unique_ptr<MCObjectWriter> 31 MCAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const { 32 auto TW = createObjectTargetWriter(); 33 switch (TW->getFormat()) { 34 case Triple::ELF: 35 return createELFObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)), OS, 36 Endian == support::little); 37 case Triple::MachO: 38 return createMachObjectWriter(cast<MCMachObjectTargetWriter>(std::move(TW)), 39 OS, Endian == support::little); 40 case Triple::COFF: 41 return createWinCOFFObjectWriter( 42 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS); 43 case Triple::SPIRV: 44 return createSPIRVObjectWriter( 45 cast<MCSPIRVObjectTargetWriter>(std::move(TW)), OS); 46 case Triple::Wasm: 47 return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)), 48 OS); 49 case Triple::XCOFF: 50 return createXCOFFObjectWriter( 51 cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS); 52 case Triple::DXContainer: 53 return createDXContainerObjectWriter( 54 cast<MCDXContainerTargetWriter>(std::move(TW)), OS); 55 default: 56 llvm_unreachable("unexpected object format"); 57 } 58 } 59 60 std::unique_ptr<MCObjectWriter> 61 MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream &OS, 62 raw_pwrite_stream &DwoOS) const { 63 auto TW = createObjectTargetWriter(); 64 switch (TW->getFormat()) { 65 case Triple::COFF: 66 return createWinCOFFDwoObjectWriter( 67 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS, DwoOS); 68 case Triple::ELF: 69 return createELFDwoObjectWriter( 70 cast<MCELFObjectTargetWriter>(std::move(TW)), OS, DwoOS, 71 Endian == support::little); 72 case Triple::Wasm: 73 return createWasmDwoObjectWriter( 74 cast<MCWasmObjectTargetWriter>(std::move(TW)), OS, DwoOS); 75 default: 76 report_fatal_error("dwo only supported with COFF, ELF, and Wasm"); 77 } 78 } 79 80 std::optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const { 81 return std::nullopt; 82 } 83 84 const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { 85 static const MCFixupKindInfo Builtins[] = { 86 {"FK_NONE", 0, 0, 0}, 87 {"FK_Data_1", 0, 8, 0}, 88 {"FK_Data_2", 0, 16, 0}, 89 {"FK_Data_4", 0, 32, 0}, 90 {"FK_Data_8", 0, 64, 0}, 91 {"FK_Data_6b", 0, 6, 0}, 92 {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel}, 93 {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel}, 94 {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, 95 {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel}, 96 {"FK_GPRel_1", 0, 8, 0}, 97 {"FK_GPRel_2", 0, 16, 0}, 98 {"FK_GPRel_4", 0, 32, 0}, 99 {"FK_GPRel_8", 0, 64, 0}, 100 {"FK_DTPRel_4", 0, 32, 0}, 101 {"FK_DTPRel_8", 0, 64, 0}, 102 {"FK_TPRel_4", 0, 32, 0}, 103 {"FK_TPRel_8", 0, 64, 0}, 104 {"FK_SecRel_1", 0, 8, 0}, 105 {"FK_SecRel_2", 0, 16, 0}, 106 {"FK_SecRel_4", 0, 32, 0}, 107 {"FK_SecRel_8", 0, 64, 0}, 108 }; 109 110 assert((size_t)Kind <= std::size(Builtins) && "Unknown fixup kind"); 111 return Builtins[Kind]; 112 } 113 114 bool MCAsmBackend::fixupNeedsRelaxationAdvanced( 115 const MCFixup &Fixup, bool Resolved, uint64_t Value, 116 const MCRelaxableFragment *DF, const MCAsmLayout &Layout, 117 const bool WasForced) const { 118 if (!Resolved) 119 return true; 120 return fixupNeedsRelaxation(Fixup, Value, DF, Layout); 121 } 122 123 bool MCAsmBackend::isDarwinCanonicalPersonality(const MCSymbol *Sym) const { 124 // Consider a NULL personality (ie., no personality encoding) to be canonical 125 // because it's always at 0. 126 if (!Sym) 127 return true; 128 129 if (!Sym->isMachO()) 130 llvm_unreachable("Expected MachO symbols only"); 131 132 StringRef name = Sym->getName(); 133 // XXX: We intentionally leave out "___gcc_personality_v0" because, despite 134 // being system-defined like these two, it is not very commonly-used. 135 // Reserving an empty slot for it seems silly. 136 return name == "___gxx_personality_v0" || name == "___objc_personality_v0"; 137 } 138