LLVM 22.0.0git
RISCVELFObjectWriter.cpp
Go to the documentation of this file.
1//===-- RISCVELFObjectWriter.cpp - RISC-V 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/MCContext.h"
14#include "llvm/MC/MCFixup.h"
16#include "llvm/MC/MCValue.h"
18
19using namespace llvm;
20
21namespace {
22class RISCVELFObjectWriter : public MCELFObjectTargetWriter {
23public:
24 RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit);
25
26 ~RISCVELFObjectWriter() override;
27
28 // Return true if the given relocation must be with a symbol rather than
29 // section plus offset.
30 bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const override {
31 // TODO: this is very conservative, update once RISC-V psABI requirements
32 // are clarified.
33 return true;
34 }
35
36protected:
37 unsigned getRelocType(const MCFixup &, const MCValue &,
38 bool IsPCRel) const override;
39};
40}
41
42RISCVELFObjectWriter::RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
43 : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_RISCV,
44 /*HasRelocationAddend*/ true) {}
45
46RISCVELFObjectWriter::~RISCVELFObjectWriter() = default;
47
48unsigned RISCVELFObjectWriter::getRelocType(const MCFixup &Fixup,
49 const MCValue &Target,
50 bool IsPCRel) const {
51 auto Kind = Fixup.getKind();
52 auto Spec = Target.getSpecifier();
53 switch (Spec) {
54 case ELF::R_RISCV_TPREL_HI20:
55 case ELF::R_RISCV_TLS_GOT_HI20:
56 case ELF::R_RISCV_TLS_GD_HI20:
57 case ELF::R_RISCV_TLSDESC_HI20:
58 if (auto *SA = const_cast<MCSymbol *>(Target.getAddSym()))
59 static_cast<MCSymbolELF *>(SA)->setType(ELF::STT_TLS);
60 break;
61 case ELF::R_RISCV_PLT32:
62 case ELF::R_RISCV_GOT32_PCREL:
63 if (Kind == FK_Data_4)
64 break;
66 " can only be used in a .word directive");
67 return ELF::R_RISCV_NONE;
68 default:
69 break;
70 }
71
72 // Extract the relocation type from the fixup kind, after applying STT_TLS as
73 // needed.
74 if (mc::isRelocation(Fixup.getKind()))
75 return Kind;
76
77 if (IsPCRel) {
78 switch (Kind) {
79 default:
80 reportError(Fixup.getLoc(), "unsupported relocation type");
81 return ELF::R_RISCV_NONE;
82 case FK_Data_4:
83 return ELF::R_RISCV_32_PCREL;
85 return ELF::R_RISCV_PCREL_HI20;
87 return ELF::R_RISCV_PCREL_LO12_I;
89 return ELF::R_RISCV_PCREL_LO12_S;
91 return ELF::R_RISCV_JAL;
93 return ELF::R_RISCV_BRANCH;
95 return ELF::R_RISCV_RVC_JUMP;
97 return ELF::R_RISCV_RVC_BRANCH;
99 return ELF::R_RISCV_CALL_PLT;
101 return ELF::R_RISCV_CALL_PLT;
103 return ELF::R_RISCV_QC_E_BRANCH;
105 return ELF::R_RISCV_QC_E_CALL_PLT;
107 return ELF::R_RISCV_NDS_BRANCH_10;
108 }
109 }
110
111 switch (Kind) {
112 default:
113 reportError(Fixup.getLoc(), "unsupported relocation type");
114 return ELF::R_RISCV_NONE;
115
116 case FK_Data_1:
117 reportError(Fixup.getLoc(), "1-byte data relocations not supported");
118 return ELF::R_RISCV_NONE;
119 case FK_Data_2:
120 reportError(Fixup.getLoc(), "2-byte data relocations not supported");
121 return ELF::R_RISCV_NONE;
122 case FK_Data_4:
123 switch (Spec) {
124 case ELF::R_RISCV_32_PCREL:
125 case ELF::R_RISCV_GOT32_PCREL:
126 case ELF::R_RISCV_PLT32:
127 return Spec;
128 }
129 return ELF::R_RISCV_32;
130 case FK_Data_8:
131 return ELF::R_RISCV_64;
133 return ELF::R_RISCV_HI20;
135 return ELF::R_RISCV_LO12_I;
137 return ELF::R_RISCV_LO12_S;
139 reportError(Fixup.getLoc(), "No relocation for CI-type instructions");
140 return ELF::R_RISCV_NONE;
142 return ELF::R_RISCV_QC_E_32;
144 return ELF::R_RISCV_QC_ABS20_U;
145 }
146}
147
148std::unique_ptr<MCObjectTargetWriter>
150 return std::make_unique<RISCVELFObjectWriter>(OSABI, Is64Bit);
151}
static Error reportError(StringRef Message)
PowerPC TLS Dynamic Call Fixup
virtual unsigned getRelocType(const MCFixup &Fixup, const MCValue &Target, bool IsPCRel) const =0
virtual bool needsRelocateWithSymbol(const MCValue &, unsigned Type) const
Encode information on a single operation to perform on a byte sequence (e.g., an encoded instruction)...
Definition: MCFixup.h:61
MCSymbol - Instances of this class represent a symbol name in the MC file, and MCSymbols are created ...
Definition: MCSymbol.h:42
Target - Wrapper for Target specific information.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
@ EM_RISCV
Definition: ELF.h:322
@ STT_TLS
Definition: ELF.h:1414
@ fixup_riscv_pcrel_lo12_i
@ fixup_riscv_pcrel_lo12_s
@ fixup_riscv_nds_branch_10
@ fixup_riscv_qc_e_call_plt
StringRef getSpecifierName(Specifier Kind)
Definition: RISCVMCExpr.cpp:46
bool isRelocation(MCFixupKind FixupKind)
Definition: MCFixup.h:130
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
std::unique_ptr< MCObjectTargetWriter > createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
@ FK_Data_8
A eight-byte fixup.
Definition: MCFixup.h:37
@ FK_Data_1
A one-byte fixup.
Definition: MCFixup.h:34
@ FK_Data_4
A four-byte fixup.
Definition: MCFixup.h:36
@ FK_Data_2
A two-byte fixup.
Definition: MCFixup.h:35