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) : Endian(Endian) {} 26 27 MCAsmBackend::~MCAsmBackend() = default; 28 29 std::unique_ptr<MCObjectWriter> 30 MCAsmBackend::createObjectWriter(raw_pwrite_stream &OS) const { 31 auto TW = createObjectTargetWriter(); 32 switch (TW->getFormat()) { 33 case Triple::ELF: 34 return createELFObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)), OS, 35 Endian == support::little); 36 case Triple::MachO: 37 return createMachObjectWriter(cast<MCMachObjectTargetWriter>(std::move(TW)), 38 OS, Endian == support::little); 39 case Triple::COFF: 40 return createWinCOFFObjectWriter( 41 cast<MCWinCOFFObjectTargetWriter>(std::move(TW)), OS); 42 case Triple::SPIRV: 43 return createSPIRVObjectWriter( 44 cast<MCSPIRVObjectTargetWriter>(std::move(TW)), OS); 45 case Triple::Wasm: 46 return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)), 47 OS); 48 case Triple::XCOFF: 49 return createXCOFFObjectWriter( 50 cast<MCXCOFFObjectTargetWriter>(std::move(TW)), OS); 51 case Triple::DXContainer: 52 return createDXContainerObjectWriter( 53 cast<MCDXContainerTargetWriter>(std::move(TW)), OS); 54 default: 55 llvm_unreachable("unexpected object format"); 56 } 57 } 58 59 std::unique_ptr<MCObjectWriter> 60 MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream &OS, 61 raw_pwrite_stream &DwoOS) const { 62 auto TW = createObjectTargetWriter(); 63 switch (TW->getFormat()) { 64 case Triple::ELF: 65 return createELFDwoObjectWriter( 66 cast<MCELFObjectTargetWriter>(std::move(TW)), OS, DwoOS, 67 Endian == support::little); 68 case Triple::Wasm: 69 return createWasmDwoObjectWriter( 70 cast<MCWasmObjectTargetWriter>(std::move(TW)), OS, DwoOS); 71 default: 72 report_fatal_error("dwo only supported with ELF and Wasm"); 73 } 74 } 75 76 std::optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const { 77 return std::nullopt; 78 } 79 80 const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { 81 static const MCFixupKindInfo Builtins[] = { 82 {"FK_NONE", 0, 0, 0}, 83 {"FK_Data_1", 0, 8, 0}, 84 {"FK_Data_2", 0, 16, 0}, 85 {"FK_Data_4", 0, 32, 0}, 86 {"FK_Data_8", 0, 64, 0}, 87 {"FK_Data_6b", 0, 6, 0}, 88 {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel}, 89 {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel}, 90 {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, 91 {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel}, 92 {"FK_GPRel_1", 0, 8, 0}, 93 {"FK_GPRel_2", 0, 16, 0}, 94 {"FK_GPRel_4", 0, 32, 0}, 95 {"FK_GPRel_8", 0, 64, 0}, 96 {"FK_DTPRel_4", 0, 32, 0}, 97 {"FK_DTPRel_8", 0, 64, 0}, 98 {"FK_TPRel_4", 0, 32, 0}, 99 {"FK_TPRel_8", 0, 64, 0}, 100 {"FK_SecRel_1", 0, 8, 0}, 101 {"FK_SecRel_2", 0, 16, 0}, 102 {"FK_SecRel_4", 0, 32, 0}, 103 {"FK_SecRel_8", 0, 64, 0}, 104 }; 105 106 assert((size_t)Kind <= std::size(Builtins) && "Unknown fixup kind"); 107 return Builtins[Kind]; 108 } 109 110 bool MCAsmBackend::fixupNeedsRelaxationAdvanced( 111 const MCFixup &Fixup, bool Resolved, uint64_t Value, 112 const MCRelaxableFragment *DF, const MCAsmLayout &Layout, 113 const bool WasForced) const { 114 if (!Resolved) 115 return true; 116 return fixupNeedsRelaxation(Fixup, Value, DF, Layout); 117 } 118