xref: /freebsd/contrib/llvm-project/llvm/lib/Target/Lanai/MCTargetDesc/LanaiELFObjectWriter.cpp (revision 1ed2ef42e01771f5d8ca9be61e07dcf0fd47feba)
1 //===-- LanaiELFObjectWriter.cpp - Lanai ELF Writer -----------------------===//
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/LanaiBaseInfo.h"
10 #include "MCTargetDesc/LanaiFixupKinds.h"
11 #include "llvm/BinaryFormat/ELF.h"
12 #include "llvm/MC/MCELFObjectWriter.h"
13 #include "llvm/MC/MCObjectWriter.h"
14 #include "llvm/Support/ErrorHandling.h"
15 
16 using namespace llvm;
17 
18 namespace {
19 
20 class LanaiELFObjectWriter : public MCELFObjectTargetWriter {
21 public:
22   explicit LanaiELFObjectWriter(uint8_t OSABI);
23 
24   ~LanaiELFObjectWriter() override = default;
25 
26 protected:
27   unsigned getRelocType(const MCFixup &, const MCValue &,
28                         bool IsPCRel) const override;
29   bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const override;
30 };
31 
32 } // end anonymous namespace
33 
34 LanaiELFObjectWriter::LanaiELFObjectWriter(uint8_t OSABI)
35     : MCELFObjectTargetWriter(/*Is64Bit_=*/false, OSABI, ELF::EM_LANAI,
36                               /*HasRelocationAddend_=*/true) {}
37 
38 unsigned LanaiELFObjectWriter::getRelocType(const MCFixup &Fixup,
39                                             const MCValue &, bool) const {
40   unsigned Type;
41   unsigned Kind = static_cast<unsigned>(Fixup.getKind());
42   switch (Kind) {
43   case Lanai::FIXUP_LANAI_21:
44     Type = ELF::R_LANAI_21;
45     break;
46   case Lanai::FIXUP_LANAI_21_F:
47     Type = ELF::R_LANAI_21_F;
48     break;
49   case Lanai::FIXUP_LANAI_25:
50     Type = ELF::R_LANAI_25;
51     break;
52   case Lanai::FIXUP_LANAI_32:
53   case FK_Data_4:
54     Type = ELF::R_LANAI_32;
55     break;
56   case Lanai::FIXUP_LANAI_HI16:
57     Type = ELF::R_LANAI_HI16;
58     break;
59   case Lanai::FIXUP_LANAI_LO16:
60     Type = ELF::R_LANAI_LO16;
61     break;
62   case Lanai::FIXUP_LANAI_NONE:
63     Type = ELF::R_LANAI_NONE;
64     break;
65 
66   default:
67     llvm_unreachable("Invalid fixup kind!");
68   }
69   return Type;
70 }
71 
72 bool LanaiELFObjectWriter::needsRelocateWithSymbol(const MCValue &,
73                                                    unsigned Type) const {
74   switch (Type) {
75   case ELF::R_LANAI_21:
76   case ELF::R_LANAI_21_F:
77   case ELF::R_LANAI_25:
78   case ELF::R_LANAI_32:
79   case ELF::R_LANAI_HI16:
80     return true;
81   default:
82     return false;
83   }
84 }
85 
86 std::unique_ptr<MCObjectTargetWriter>
87 llvm::createLanaiELFObjectWriter(uint8_t OSABI) {
88   return std::make_unique<LanaiELFObjectWriter>(OSABI);
89 }
90