1 //===-- LoongArchELFObjectWriter.cpp - LoongArch ELF Writer ---*- C++ -*---===// 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 "MCTargetDesc/LoongArchFixupKinds.h" 10 #include "MCTargetDesc/LoongArchMCTargetDesc.h" 11 #include "llvm/BinaryFormat/ELF.h" 12 #include "llvm/MC/MCContext.h" 13 #include "llvm/MC/MCELFObjectWriter.h" 14 #include "llvm/MC/MCFixup.h" 15 #include "llvm/MC/MCObjectWriter.h" 16 #include "llvm/Support/ErrorHandling.h" 17 18 using namespace llvm; 19 20 namespace { 21 class LoongArchELFObjectWriter : public MCELFObjectTargetWriter { 22 public: 23 LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit); 24 25 ~LoongArchELFObjectWriter() override; 26 27 protected: 28 unsigned getRelocType(MCContext &Ctx, const MCValue &Target, 29 const MCFixup &Fixup, bool IsPCRel) const override; 30 }; 31 } // end namespace 32 33 LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) 34 : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH, 35 /*HasRelocationAddend*/ true) {} 36 37 LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {} 38 39 unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx, 40 const MCValue &Target, 41 const MCFixup &Fixup, 42 bool IsPCRel) const { 43 // Determine the type of the relocation 44 unsigned Kind = Fixup.getTargetKind(); 45 46 if (Kind >= FirstLiteralRelocationKind) 47 return Kind - FirstLiteralRelocationKind; 48 49 switch (Kind) { 50 default: 51 Ctx.reportError(Fixup.getLoc(), "Unsupported relocation type"); 52 return ELF::R_LARCH_NONE; 53 case FK_Data_1: 54 Ctx.reportError(Fixup.getLoc(), "1-byte data relocations not supported"); 55 return ELF::R_LARCH_NONE; 56 case FK_Data_2: 57 Ctx.reportError(Fixup.getLoc(), "2-byte data relocations not supported"); 58 return ELF::R_LARCH_NONE; 59 case FK_Data_4: 60 return IsPCRel ? ELF::R_LARCH_32_PCREL : ELF::R_LARCH_32; 61 case FK_Data_8: 62 return ELF::R_LARCH_64; 63 case LoongArch::fixup_loongarch_b16: 64 return ELF::R_LARCH_B16; 65 case LoongArch::fixup_loongarch_b21: 66 return ELF::R_LARCH_B21; 67 case LoongArch::fixup_loongarch_b26: 68 return ELF::R_LARCH_B26; 69 case LoongArch::fixup_loongarch_abs_hi20: 70 return ELF::R_LARCH_ABS_HI20; 71 case LoongArch::fixup_loongarch_abs_lo12: 72 return ELF::R_LARCH_ABS_LO12; 73 case LoongArch::fixup_loongarch_abs64_lo20: 74 return ELF::R_LARCH_ABS64_LO20; 75 case LoongArch::fixup_loongarch_abs64_hi12: 76 return ELF::R_LARCH_ABS64_HI12; 77 case LoongArch::fixup_loongarch_tls_le_hi20: 78 return ELF::R_LARCH_TLS_LE_HI20; 79 case LoongArch::fixup_loongarch_tls_le_lo12: 80 return ELF::R_LARCH_TLS_LE_LO12; 81 case LoongArch::fixup_loongarch_tls_le64_lo20: 82 return ELF::R_LARCH_TLS_LE64_LO20; 83 case LoongArch::fixup_loongarch_tls_le64_hi12: 84 return ELF::R_LARCH_TLS_LE64_HI12; 85 // TODO: Handle more fixup-kinds. 86 } 87 } 88 89 std::unique_ptr<MCObjectTargetWriter> 90 llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) { 91 return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit); 92 } 93