blob: 95d4242e4042359f5ef6ad1ec28a3af3f271644f [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"
14#include "llvm/Support/ErrorHandling.h"
15
16using namespace llvm;
17
18namespace {
19class RISCVELFObjectWriter : public MCELFObjectTargetWriter {
20public:
21 RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit);
22
23 ~RISCVELFObjectWriter() override;
24
25protected:
26 unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
27 const MCFixup &Fixup, bool IsPCRel) const override;
28};
29}
30
31RISCVELFObjectWriter::RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
32 : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_RISCV,
Alex Bradburydd834842017-08-20 06:55:14 +000033 /*HasRelocationAddend*/ true) {}
Alex Bradbury6b2cca72016-11-01 23:47:30 +000034
35RISCVELFObjectWriter::~RISCVELFObjectWriter() {}
36
37unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
38 const MCValue &Target,
39 const MCFixup &Fixup,
40 bool IsPCRel) const {
Alex Bradbury9d3f1252017-09-28 08:26:24 +000041 // Determine the type of the relocation
42 switch ((unsigned)Fixup.getKind()) {
43 default:
44 llvm_unreachable("invalid fixup kind!");
45 case FK_Data_4:
46 return ELF::R_RISCV_32;
47 case FK_Data_8:
48 return ELF::R_RISCV_64;
49 case RISCV::fixup_riscv_hi20:
50 return ELF::R_RISCV_HI20;
51 case RISCV::fixup_riscv_lo12_i:
52 return ELF::R_RISCV_LO12_I;
53 case RISCV::fixup_riscv_lo12_s:
54 return ELF::R_RISCV_LO12_S;
55 case RISCV::fixup_riscv_pcrel_hi20:
56 return ELF::R_RISCV_PCREL_HI20;
57 case RISCV::fixup_riscv_jal:
58 return ELF::R_RISCV_JAL;
59 case RISCV::fixup_riscv_branch:
60 return ELF::R_RISCV_BRANCH;
61 }
Alex Bradbury6b2cca72016-11-01 23:47:30 +000062}
63
64MCObjectWriter *llvm::createRISCVELFObjectWriter(raw_pwrite_stream &OS,
65 uint8_t OSABI, bool Is64Bit) {
66 MCELFObjectTargetWriter *MOTW = new RISCVELFObjectWriter(OSABI, Is64Bit);
67 return createELFObjectWriter(MOTW, OS, /*IsLittleEndian*/ true);
68}