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 } // end 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   // Determine the type of the relocation
49   unsigned Kind = Fixup.getTargetKind();
50 
51   if (Kind >= FirstLiteralRelocationKind)
52     return Kind - FirstLiteralRelocationKind;
53 
54   switch (Kind) {
55   // TODO: Implement this when we defined fixup kind.
56   default:
57     return ELF::R_LARCH_NONE;
58   }
59 }
60 
61 std::unique_ptr<MCObjectTargetWriter>
62 llvm::createLoongArchELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
63   return std::make_unique<LoongArchELFObjectWriter>(OSABI, Is64Bit);
64 }
65