blob: cab2bbcb81bcde6ea03fba298b09f445d0c66e3a [file] [log] [blame]
Alex Bradbury6b2cca72016-11-01 23:47:30 +00001//===-- RISCVELFObjectWriter.cpp - RISCV ELF Writer -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alex Bradbury6b2cca72016-11-01 23:47:30 +00006//
7//===----------------------------------------------------------------------===//
8
Alex Bradbury9d3f1252017-09-28 08:26:24 +00009#include "MCTargetDesc/RISCVFixupKinds.h"
Alex Bradbury7cb3cd32019-08-20 12:32:31 +000010#include "MCTargetDesc/RISCVMCExpr.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000011#include "MCTargetDesc/RISCVMCTargetDesc.h"
12#include "llvm/MC/MCELFObjectWriter.h"
13#include "llvm/MC/MCFixup.h"
Alex Bradbury5c1eef42017-10-11 12:09:06 +000014#include "llvm/MC/MCObjectWriter.h"
Alex Bradbury6b2cca72016-11-01 23:47:30 +000015#include "llvm/Support/ErrorHandling.h"
16
17using namespace llvm;
18
19namespace {
20class RISCVELFObjectWriter : public MCELFObjectTargetWriter {
21public:
22 RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit);
23
24 ~RISCVELFObjectWriter() override;
25
Shiva Chen98f93892018-04-25 14:18:55 +000026 // Return true if the given relocation must be with a symbol rather than
27 // section plus offset.
28 bool needsRelocateWithSymbol(const MCSymbol &Sym,
29 unsigned Type) const override {
30 // TODO: this is very conservative, update once RISC-V psABI requirements
31 // are clarified.
32 return true;
33 }
34
Alex Bradbury6b2cca72016-11-01 23:47:30 +000035protected:
36 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
37 const MCFixup &Fixup, bool IsPCRel) const override;
38};
39}
40
41RISCVELFObjectWriter::RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
42 : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_RISCV,
Alex Bradburydd834842017-08-20 06:55:14 +000043 /*HasRelocationAddend*/ true) {}
Alex Bradbury6b2cca72016-11-01 23:47:30 +000044
45RISCVELFObjectWriter::~RISCVELFObjectWriter() {}
46
47unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
48 const MCValue &Target,
49 const MCFixup &Fixup,
50 bool IsPCRel) const {
Alex Bradbury7cb3cd32019-08-20 12:32:31 +000051 const MCExpr *Expr = Fixup.getValue();
Alex Bradbury9d3f1252017-09-28 08:26:24 +000052 // Determine the type of the relocation
Sam Clegg90b6bb72019-08-23 01:00:55 +000053 unsigned Kind = Fixup.getTargetKind();
Alex Bradbury4ac0b9b2019-07-16 03:47:34 +000054 if (IsPCRel) {
55 switch (Kind) {
56 default:
57 llvm_unreachable("invalid fixup kind!");
58 case FK_Data_4:
59 case FK_PCRel_4:
60 return ELF::R_RISCV_32_PCREL;
61 case RISCV::fixup_riscv_pcrel_hi20:
62 return ELF::R_RISCV_PCREL_HI20;
63 case RISCV::fixup_riscv_pcrel_lo12_i:
64 return ELF::R_RISCV_PCREL_LO12_I;
65 case RISCV::fixup_riscv_pcrel_lo12_s:
66 return ELF::R_RISCV_PCREL_LO12_S;
67 case RISCV::fixup_riscv_got_hi20:
68 return ELF::R_RISCV_GOT_HI20;
69 case RISCV::fixup_riscv_tls_got_hi20:
70 return ELF::R_RISCV_TLS_GOT_HI20;
71 case RISCV::fixup_riscv_tls_gd_hi20:
72 return ELF::R_RISCV_TLS_GD_HI20;
73 case RISCV::fixup_riscv_jal:
74 return ELF::R_RISCV_JAL;
75 case RISCV::fixup_riscv_branch:
76 return ELF::R_RISCV_BRANCH;
77 case RISCV::fixup_riscv_rvc_jump:
78 return ELF::R_RISCV_RVC_JUMP;
79 case RISCV::fixup_riscv_rvc_branch:
80 return ELF::R_RISCV_RVC_BRANCH;
81 case RISCV::fixup_riscv_call:
82 return ELF::R_RISCV_CALL;
83 case RISCV::fixup_riscv_call_plt:
84 return ELF::R_RISCV_CALL_PLT;
85 }
86 }
87
88 switch (Kind) {
Alex Bradbury9d3f1252017-09-28 08:26:24 +000089 default:
90 llvm_unreachable("invalid fixup kind!");
91 case FK_Data_4:
Alex Bradbury7cb3cd32019-08-20 12:32:31 +000092 if (Expr->getKind() == MCExpr::Target &&
93 cast<RISCVMCExpr>(Expr)->getKind() == RISCVMCExpr::VK_RISCV_32_PCREL)
94 return ELF::R_RISCV_32_PCREL;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000095 return ELF::R_RISCV_32;
96 case FK_Data_8:
97 return ELF::R_RISCV_64;
Alex Bradbury257d5b52018-05-23 12:36:18 +000098 case FK_Data_Add_1:
99 return ELF::R_RISCV_ADD8;
100 case FK_Data_Add_2:
101 return ELF::R_RISCV_ADD16;
102 case FK_Data_Add_4:
103 return ELF::R_RISCV_ADD32;
104 case FK_Data_Add_8:
105 return ELF::R_RISCV_ADD64;
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +0000106 case FK_Data_Add_6b:
107 return ELF::R_RISCV_SET6;
Alex Bradbury257d5b52018-05-23 12:36:18 +0000108 case FK_Data_Sub_1:
109 return ELF::R_RISCV_SUB8;
110 case FK_Data_Sub_2:
111 return ELF::R_RISCV_SUB16;
112 case FK_Data_Sub_4:
113 return ELF::R_RISCV_SUB32;
114 case FK_Data_Sub_8:
115 return ELF::R_RISCV_SUB64;
Hsiangkai Wang18ccfad2019-07-19 02:03:34 +0000116 case FK_Data_Sub_6b:
117 return ELF::R_RISCV_SUB6;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000118 case RISCV::fixup_riscv_hi20:
119 return ELF::R_RISCV_HI20;
120 case RISCV::fixup_riscv_lo12_i:
121 return ELF::R_RISCV_LO12_I;
122 case RISCV::fixup_riscv_lo12_s:
123 return ELF::R_RISCV_LO12_S;
Lewis Revillaa79a3f2019-04-04 14:13:37 +0000124 case RISCV::fixup_riscv_tprel_hi20:
125 return ELF::R_RISCV_TPREL_HI20;
126 case RISCV::fixup_riscv_tprel_lo12_i:
127 return ELF::R_RISCV_TPREL_LO12_I;
128 case RISCV::fixup_riscv_tprel_lo12_s:
129 return ELF::R_RISCV_TPREL_LO12_S;
130 case RISCV::fixup_riscv_tprel_add:
131 return ELF::R_RISCV_TPREL_ADD;
Shiva Chen43bfe842018-05-24 06:21:23 +0000132 case RISCV::fixup_riscv_relax:
133 return ELF::R_RISCV_RELAX;
Shiva Chen5af037f2019-01-30 11:16:59 +0000134 case RISCV::fixup_riscv_align:
135 return ELF::R_RISCV_ALIGN;
Alex Bradbury9d3f1252017-09-28 08:26:24 +0000136 }
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000137}
138
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000139std::unique_ptr<MCObjectTargetWriter>
140llvm::createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
Jonas Devlieghere0eaee542019-08-15 15:54:37 +0000141 return std::make_unique<RISCVELFObjectWriter>(OSABI, Is64Bit);
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000142}