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