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/STLExtras.h" 12 #include "llvm/MC/MCCodePadder.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/MCWasmObjectWriter.h" 18 #include "llvm/MC/MCWinCOFFObjectWriter.h" 19 #include <cassert> 20 #include <cstddef> 21 #include <cstdint> 22 23 using namespace llvm; 24 25 MCAsmBackend::MCAsmBackend(support::endianness Endian) 26 : CodePadder(new MCCodePadder()), Endian(Endian) {} 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::Wasm: 44 return createWasmObjectWriter(cast<MCWasmObjectTargetWriter>(std::move(TW)), 45 OS); 46 default: 47 llvm_unreachable("unexpected object format"); 48 } 49 } 50 51 std::unique_ptr<MCObjectWriter> 52 MCAsmBackend::createDwoObjectWriter(raw_pwrite_stream &OS, 53 raw_pwrite_stream &DwoOS) const { 54 auto TW = createObjectTargetWriter(); 55 if (TW->getFormat() != Triple::ELF) 56 report_fatal_error("dwo only supported with ELF"); 57 return createELFDwoObjectWriter(cast<MCELFObjectTargetWriter>(std::move(TW)), 58 OS, DwoOS, Endian == support::little); 59 } 60 61 Optional<MCFixupKind> MCAsmBackend::getFixupKind(StringRef Name) const { 62 return None; 63 } 64 65 const MCFixupKindInfo &MCAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { 66 static const MCFixupKindInfo Builtins[] = { 67 {"FK_NONE", 0, 0, 0}, 68 {"FK_Data_1", 0, 8, 0}, 69 {"FK_Data_2", 0, 16, 0}, 70 {"FK_Data_4", 0, 32, 0}, 71 {"FK_Data_8", 0, 64, 0}, 72 {"FK_PCRel_1", 0, 8, MCFixupKindInfo::FKF_IsPCRel}, 73 {"FK_PCRel_2", 0, 16, MCFixupKindInfo::FKF_IsPCRel}, 74 {"FK_PCRel_4", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, 75 {"FK_PCRel_8", 0, 64, MCFixupKindInfo::FKF_IsPCRel}, 76 {"FK_GPRel_1", 0, 8, 0}, 77 {"FK_GPRel_2", 0, 16, 0}, 78 {"FK_GPRel_4", 0, 32, 0}, 79 {"FK_GPRel_8", 0, 64, 0}, 80 {"FK_DTPRel_4", 0, 32, 0}, 81 {"FK_DTPRel_8", 0, 64, 0}, 82 {"FK_TPRel_4", 0, 32, 0}, 83 {"FK_TPRel_8", 0, 64, 0}, 84 {"FK_SecRel_1", 0, 8, 0}, 85 {"FK_SecRel_2", 0, 16, 0}, 86 {"FK_SecRel_4", 0, 32, 0}, 87 {"FK_SecRel_8", 0, 64, 0}, 88 {"FK_Data_Add_1", 0, 8, 0}, 89 {"FK_Data_Add_2", 0, 16, 0}, 90 {"FK_Data_Add_4", 0, 32, 0}, 91 {"FK_Data_Add_8", 0, 64, 0}, 92 {"FK_Data_Sub_1", 0, 8, 0}, 93 {"FK_Data_Sub_2", 0, 16, 0}, 94 {"FK_Data_Sub_4", 0, 32, 0}, 95 {"FK_Data_Sub_8", 0, 64, 0}}; 96 97 assert((size_t)Kind <= array_lengthof(Builtins) && "Unknown fixup kind"); 98 return Builtins[Kind]; 99 } 100 101 bool MCAsmBackend::fixupNeedsRelaxationAdvanced( 102 const MCFixup &Fixup, bool Resolved, uint64_t Value, 103 const MCRelaxableFragment *DF, const MCAsmLayout &Layout, 104 const bool WasForced) const { 105 if (!Resolved) 106 return true; 107 return fixupNeedsRelaxation(Fixup, Value, DF, Layout); 108 } 109 110 void MCAsmBackend::handleCodePaddingBasicBlockStart( 111 MCObjectStreamer *OS, const MCCodePaddingContext &Context) { 112 CodePadder->handleBasicBlockStart(OS, Context); 113 } 114 115 void MCAsmBackend::handleCodePaddingBasicBlockEnd( 116 const MCCodePaddingContext &Context) { 117 CodePadder->handleBasicBlockEnd(Context); 118 } 119 120 void MCAsmBackend::handleCodePaddingInstructionBegin(const MCInst &Inst) { 121 CodePadder->handleInstructionBegin(Inst); 122 } 123 124 void MCAsmBackend::handleCodePaddingInstructionEnd(const MCInst &Inst) { 125 CodePadder->handleInstructionEnd(Inst); 126 } 127 128 bool MCAsmBackend::relaxFragment(MCPaddingFragment *PF, MCAsmLayout &Layout) { 129 return CodePadder->relaxFragment(PF, Layout); 130 } 131