LLVM  9.0.0svn
RISCVELFObjectWriter.cpp
Go to the documentation of this file.
1 //===-- RISCVELFObjectWriter.cpp - RISCV 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 
12 #include "llvm/MC/MCFixup.h"
13 #include "llvm/MC/MCObjectWriter.h"
15 
16 using namespace llvm;
17 
18 namespace {
19 class RISCVELFObjectWriter : public MCELFObjectTargetWriter {
20 public:
21  RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit);
22 
23  ~RISCVELFObjectWriter() 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  // TODO: this is very conservative, update once RISC-V psABI requirements
30  // are clarified.
31  return true;
32  }
33 
34 protected:
35  unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
36  const MCFixup &Fixup, bool IsPCRel) const override;
37 };
38 }
39 
40 RISCVELFObjectWriter::RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
41  : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_RISCV,
42  /*HasRelocationAddend*/ true) {}
43 
44 RISCVELFObjectWriter::~RISCVELFObjectWriter() {}
45 
47  const MCValue &Target,
48  const MCFixup &Fixup,
49  bool IsPCRel) const {
50  // Determine the type of the relocation
51  unsigned Kind = Fixup.getKind();
52  if (IsPCRel) {
53  switch (Kind) {
54  default:
55  llvm_unreachable("invalid fixup kind!");
56  case FK_Data_4:
57  case FK_PCRel_4:
58  return ELF::R_RISCV_32_PCREL;
60  return ELF::R_RISCV_PCREL_HI20;
62  return ELF::R_RISCV_PCREL_LO12_I;
64  return ELF::R_RISCV_PCREL_LO12_S;
66  return ELF::R_RISCV_GOT_HI20;
68  return ELF::R_RISCV_TLS_GOT_HI20;
70  return ELF::R_RISCV_TLS_GD_HI20;
72  return ELF::R_RISCV_JAL;
74  return ELF::R_RISCV_BRANCH;
76  return ELF::R_RISCV_RVC_JUMP;
78  return ELF::R_RISCV_RVC_BRANCH;
80  return ELF::R_RISCV_CALL;
82  return ELF::R_RISCV_CALL_PLT;
83  }
84  }
85 
86  switch (Kind) {
87  default:
88  llvm_unreachable("invalid fixup kind!");
89  case FK_Data_4:
90  return ELF::R_RISCV_32;
91  case FK_Data_8:
92  return ELF::R_RISCV_64;
93  case FK_Data_Add_1:
94  return ELF::R_RISCV_ADD8;
95  case FK_Data_Add_2:
96  return ELF::R_RISCV_ADD16;
97  case FK_Data_Add_4:
98  return ELF::R_RISCV_ADD32;
99  case FK_Data_Add_8:
100  return ELF::R_RISCV_ADD64;
101  case FK_Data_Sub_1:
102  return ELF::R_RISCV_SUB8;
103  case FK_Data_Sub_2:
104  return ELF::R_RISCV_SUB16;
105  case FK_Data_Sub_4:
106  return ELF::R_RISCV_SUB32;
107  case FK_Data_Sub_8:
108  return ELF::R_RISCV_SUB64;
110  return ELF::R_RISCV_HI20;
112  return ELF::R_RISCV_LO12_I;
114  return ELF::R_RISCV_LO12_S;
116  return ELF::R_RISCV_TPREL_HI20;
118  return ELF::R_RISCV_TPREL_LO12_I;
120  return ELF::R_RISCV_TPREL_LO12_S;
122  return ELF::R_RISCV_TPREL_ADD;
124  return ELF::R_RISCV_RELAX;
126  return ELF::R_RISCV_ALIGN;
127  }
128 }
129 
130 std::unique_ptr<MCObjectTargetWriter>
131 llvm::createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
132  return llvm::make_unique<RISCVELFObjectWriter>(OSABI, Is64Bit);
133 }
std::unique_ptr< MCObjectTargetWriter > createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
This class represents lattice values for constants.
Definition: AllocatorList.h:23
This represents an "assembler immediate".
Definition: MCValue.h:39
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:41
A eight-byte sub fixup.
Definition: MCFixup.h:51
block Block Frequency true
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:74
A eight-byte add fixup.
Definition: MCFixup.h:47
static unsigned getRelocType(const MCValue &Target, const MCFixupKind FixupKind, const bool IsPCRel)
Translates generic PPC fixup kind to Mach-O/PPC relocation type enum.
A four-byte fixup.
Definition: MCFixup.h:26
Context object for machine code objects.
Definition: MCContext.h:64
A one-byte add fixup.
Definition: MCFixup.h:44
The instances of the Type class are immutable: once they are created, they are never changed...
Definition: Type.h:45
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
A four-byte pc relative fixup.
Definition: MCFixup.h:30
A four-byte add fixup.
Definition: MCFixup.h:46
Target - Wrapper for Target specific information.
A two-byte sub fixup.
Definition: MCFixup.h:49
A eight-byte fixup.
Definition: MCFixup.h:27
A one-byte sub fixup.
Definition: MCFixup.h:48
A four-byte sub fixup.
Definition: MCFixup.h:50
A two-byte add fixup.
Definition: MCFixup.h:45
MCFixupKind getKind() const
Definition: MCFixup.h:123