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/LoongArchMCTargetDesc.h" 10 #include "llvm/MC/MCContext.h" 11 #include "llvm/MC/MCELFObjectWriter.h" 12 #include "llvm/MC/MCFixup.h" 13 #include "llvm/MC/MCObjectWriter.h" 14 #include "llvm/Support/ErrorHandling.h" 15 16 using namespace llvm; 17 18 namespace { 19 class LoongArchELFObjectWriter : public MCELFObjectTargetWriter { 20 public: 21 LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit); 22 23 ~LoongArchELFObjectWriter() override; 24 25 // Return true if the given relocation must be with a symbol rather than 26 // section plus offset. 27 bool needsRelocateWithSymbol(const MCSymbol &Sym, 28 unsigned Type) const override { 29 return true; 30 } 31 32 protected: 33 unsigned getRelocType(MCContext &Ctx, const MCValue &Target, 34 const MCFixup &Fixup, bool IsPCRel) const override; 35 }; 36 } // namespace 37 38 LoongArchELFObjectWriter::LoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) 39 : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_LOONGARCH, 40 /*HasRelocationAddend*/ true) {} 41 42 LoongArchELFObjectWriter::~LoongArchELFObjectWriter() {} 43 44 unsigned LoongArchELFObjectWriter::getRelocType(MCContext &Ctx, 45 const MCValue &Target, 46 const MCFixup &Fixup, 47 bool IsPCRel) const { 48 const MCExpr *Expr = Fixup.getValue(); 49 // Determine the type of the relocation 50 unsigned Kind = Fixup.getTargetKind(); 51 52 if (Kind >= FirstLiteralRelocationKind) 53 return Kind - FirstLiteralRelocationKind; 54 55 switch (Kind) { 56 // TODO: Implement this when we defined fixup kind. 57 default: 58 return ELF::R_LARCH_NONE; 59 } 60 } 61 62 std::unique_ptr<MCObjectTargetWriter> 63 llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) { 64 return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit); 65 } 66