• 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 "MCTargetDesc/LanaiMCTargetDesc.h"
13 #include "llvm/MC/MCELFObjectWriter.h"
14 #include "llvm/MC/MCSymbol.h"
15 #include "llvm/Support/ErrorHandling.h"
16 #include "llvm/Support/raw_ostream.h"
17 
18 using namespace llvm;
19 
20 namespace {
21 class LanaiELFObjectWriter : public MCELFObjectTargetWriter {
22 public:
23   explicit LanaiELFObjectWriter(uint8_t OSABI);
24 
25   ~LanaiELFObjectWriter() override;
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 } // namespace
34 
LanaiELFObjectWriter(uint8_t OSABI)35 LanaiELFObjectWriter::LanaiELFObjectWriter(uint8_t OSABI)
36     : MCELFObjectTargetWriter(/*Is64Bit=*/false, OSABI, ELF::EM_LANAI,
37                               /*HasRelocationAddend=*/true) {}
38 
~LanaiELFObjectWriter()39 LanaiELFObjectWriter::~LanaiELFObjectWriter() {}
40 
getRelocType(MCContext & Ctx,const MCValue & Target,const MCFixup & Fixup,bool IsPCRel) const41 unsigned LanaiELFObjectWriter::getRelocType(MCContext &Ctx,
42                                             const MCValue &Target,
43                                             const MCFixup &Fixup,
44                                             bool IsPCRel) const {
45   unsigned Type;
46   unsigned Kind = static_cast<unsigned>(Fixup.getKind());
47   switch (Kind) {
48   case Lanai::FIXUP_LANAI_21:
49     Type = ELF::R_LANAI_21;
50     break;
51   case Lanai::FIXUP_LANAI_21_F:
52     Type = ELF::R_LANAI_21_F;
53     break;
54   case Lanai::FIXUP_LANAI_25:
55     Type = ELF::R_LANAI_25;
56     break;
57   case Lanai::FIXUP_LANAI_32:
58   case FK_Data_4:
59     Type = ELF::R_LANAI_32;
60     break;
61   case Lanai::FIXUP_LANAI_HI16:
62     Type = ELF::R_LANAI_HI16;
63     break;
64   case Lanai::FIXUP_LANAI_LO16:
65     Type = ELF::R_LANAI_LO16;
66     break;
67   case Lanai::FIXUP_LANAI_NONE:
68     Type = ELF::R_LANAI_NONE;
69     break;
70 
71   default:
72     llvm_unreachable("Invalid fixup kind!");
73   }
74   return Type;
75 }
76 
needsRelocateWithSymbol(const MCSymbol & SD,unsigned Type) const77 bool LanaiELFObjectWriter::needsRelocateWithSymbol(const MCSymbol &SD,
78                                                    unsigned Type) const {
79   switch (Type) {
80   case ELF::R_LANAI_21:
81   case ELF::R_LANAI_21_F:
82   case ELF::R_LANAI_25:
83   case ELF::R_LANAI_32:
84   case ELF::R_LANAI_HI16:
85     return true;
86   default:
87     return false;
88   }
89 }
90 
createLanaiELFObjectWriter(raw_pwrite_stream & OS,uint8_t OSABI)91 MCObjectWriter *llvm::createLanaiELFObjectWriter(raw_pwrite_stream &OS,
92                                                  uint8_t OSABI) {
93   MCELFObjectTargetWriter *MOTW = new LanaiELFObjectWriter(OSABI);
94   return createELFObjectWriter(MOTW, OS, /*IsLittleEndian=*/false);
95 }
96