• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- LanaiELFObjectWriter.cpp - Lanai ELF Writer -----------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 
10 #include "MCTargetDesc/LanaiBaseInfo.h"
11 #include "MCTargetDesc/LanaiFixupKinds.h"
12 #include "llvm/BinaryFormat/ELF.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCObjectWriter.h"
15 #include "llvm/Support/ErrorHandling.h"
16 
17 using namespace llvm;
18 
19 namespace {
20 
21 class LanaiELFObjectWriter : public MCELFObjectTargetWriter {
22 public:
23   explicit LanaiELFObjectWriter(uint8_t OSABI);
24 
25   ~LanaiELFObjectWriter() override = default;
26 
27 protected:
28   unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
29                         const MCFixup &Fixup, bool IsPCRel) const override;
30   bool needsRelocateWithSymbol(const MCSymbol &SD,
31                                unsigned Type) const override;
32 };
33 
34 } // end anonymous namespace
35 
LanaiELFObjectWriter(uint8_t OSABI)36 LanaiELFObjectWriter::LanaiELFObjectWriter(uint8_t OSABI)
37     : MCELFObjectTargetWriter(/*Is64Bit_=*/false, OSABI, ELF::EM_LANAI,
38                               /*HasRelocationAddend=*/true) {}
39 
getRelocType(MCContext &,const MCValue &,const MCFixup & Fixup,bool) const40 unsigned LanaiELFObjectWriter::getRelocType(MCContext & /*Ctx*/,
41                                             const MCValue & /*Target*/,
42                                             const MCFixup &Fixup,
43                                             bool /*IsPCRel*/) const {
44   unsigned Type;
45   unsigned Kind = static_cast<unsigned>(Fixup.getKind());
46   switch (Kind) {
47   case Lanai::FIXUP_LANAI_21:
48     Type = ELF::R_LANAI_21;
49     break;
50   case Lanai::FIXUP_LANAI_21_F:
51     Type = ELF::R_LANAI_21_F;
52     break;
53   case Lanai::FIXUP_LANAI_25:
54     Type = ELF::R_LANAI_25;
55     break;
56   case Lanai::FIXUP_LANAI_32:
57   case FK_Data_4:
58     Type = ELF::R_LANAI_32;
59     break;
60   case Lanai::FIXUP_LANAI_HI16:
61     Type = ELF::R_LANAI_HI16;
62     break;
63   case Lanai::FIXUP_LANAI_LO16:
64     Type = ELF::R_LANAI_LO16;
65     break;
66   case Lanai::FIXUP_LANAI_NONE:
67     Type = ELF::R_LANAI_NONE;
68     break;
69 
70   default:
71     llvm_unreachable("Invalid fixup kind!");
72   }
73   return Type;
74 }
75 
needsRelocateWithSymbol(const MCSymbol &,unsigned Type) const76 bool LanaiELFObjectWriter::needsRelocateWithSymbol(const MCSymbol & /*SD*/,
77                                                    unsigned Type) const {
78   switch (Type) {
79   case ELF::R_LANAI_21:
80   case ELF::R_LANAI_21_F:
81   case ELF::R_LANAI_25:
82   case ELF::R_LANAI_32:
83   case ELF::R_LANAI_HI16:
84     return true;
85   default:
86     return false;
87   }
88 }
89 
90 std::unique_ptr<MCObjectTargetWriter>
createLanaiELFObjectWriter(uint8_t OSABI)91 llvm::createLanaiELFObjectWriter(uint8_t OSABI) {
92   return llvm::make_unique<LanaiELFObjectWriter>(OSABI);
93 }
94