blob: 57b52aa7add0f3a1f7c2384764fe5eb97ba0b631 [file] [log] [blame]
Alex Bradbury6b2cca72016-11-01 23:47:30 +00001//===-- RISCVELFObjectWriter.cpp - RISCV 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
Alex Bradbury9d3f1252017-09-28 08:26:24 +000010#include "MCTargetDesc/RISCVFixupKinds.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 Bradbury9d3f1252017-09-28 08:26:24 +000051 // Determine the type of the relocation
52 switch ((unsigned)Fixup.getKind()) {
53 default:
54 llvm_unreachable("invalid fixup kind!");
55 case FK_Data_4:
56 return ELF::R_RISCV_32;
57 case FK_Data_8:
58 return ELF::R_RISCV_64;
Alex Bradbury257d5b52018-05-23 12:36:18 +000059 case FK_Data_Add_1:
60 return ELF::R_RISCV_ADD8;
61 case FK_Data_Add_2:
62 return ELF::R_RISCV_ADD16;
63 case FK_Data_Add_4:
64 return ELF::R_RISCV_ADD32;
65 case FK_Data_Add_8:
66 return ELF::R_RISCV_ADD64;
67 case FK_Data_Sub_1:
68 return ELF::R_RISCV_SUB8;
69 case FK_Data_Sub_2:
70 return ELF::R_RISCV_SUB16;
71 case FK_Data_Sub_4:
72 return ELF::R_RISCV_SUB32;
73 case FK_Data_Sub_8:
74 return ELF::R_RISCV_SUB64;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000075 case RISCV::fixup_riscv_hi20:
76 return ELF::R_RISCV_HI20;
77 case RISCV::fixup_riscv_lo12_i:
78 return ELF::R_RISCV_LO12_I;
79 case RISCV::fixup_riscv_lo12_s:
80 return ELF::R_RISCV_LO12_S;
81 case RISCV::fixup_riscv_pcrel_hi20:
82 return ELF::R_RISCV_PCREL_HI20;
Ahmed Charles646ab872018-02-06 00:55:23 +000083 case RISCV::fixup_riscv_pcrel_lo12_i:
84 return ELF::R_RISCV_PCREL_LO12_I;
85 case RISCV::fixup_riscv_pcrel_lo12_s:
86 return ELF::R_RISCV_PCREL_LO12_S;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000087 case RISCV::fixup_riscv_jal:
88 return ELF::R_RISCV_JAL;
89 case RISCV::fixup_riscv_branch:
90 return ELF::R_RISCV_BRANCH;
Alex Bradburyf8f4b902017-12-07 13:19:57 +000091 case RISCV::fixup_riscv_rvc_jump:
92 return ELF::R_RISCV_RVC_JUMP;
93 case RISCV::fixup_riscv_rvc_branch:
94 return ELF::R_RISCV_RVC_BRANCH;
Shiva Chen98f93892018-04-25 14:18:55 +000095 case RISCV::fixup_riscv_call:
96 return ELF::R_RISCV_CALL;
Alex Bradbury9d3f1252017-09-28 08:26:24 +000097 }
Alex Bradbury6b2cca72016-11-01 23:47:30 +000098}
99
Peter Collingbournedcd7d6c2018-05-21 19:20:29 +0000100std::unique_ptr<MCObjectTargetWriter>
101llvm::createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
102 return llvm::make_unique<RISCVELFObjectWriter>(OSABI, Is64Bit);
Alex Bradbury6b2cca72016-11-01 23:47:30 +0000103}