Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 1 | //===-- 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 Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 10 | #include "MCTargetDesc/RISCVFixupKinds.h" |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 11 | #include "MCTargetDesc/RISCVMCTargetDesc.h" |
| 12 | #include "llvm/MC/MCELFObjectWriter.h" |
| 13 | #include "llvm/MC/MCFixup.h" |
| 14 | #include "llvm/Support/ErrorHandling.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 | protected: |
| 26 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| 27 | const MCFixup &Fixup, bool IsPCRel) const override; |
| 28 | }; |
| 29 | } |
| 30 | |
| 31 | RISCVELFObjectWriter::RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit) |
| 32 | : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_RISCV, |
Alex Bradbury | dd83484 | 2017-08-20 06:55:14 +0000 | [diff] [blame] | 33 | /*HasRelocationAddend*/ true) {} |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 34 | |
| 35 | RISCVELFObjectWriter::~RISCVELFObjectWriter() {} |
| 36 | |
| 37 | unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx, |
| 38 | const MCValue &Target, |
| 39 | const MCFixup &Fixup, |
| 40 | bool IsPCRel) const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 41 | // 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 Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | MCObjectWriter *llvm::createRISCVELFObjectWriter(raw_pwrite_stream &OS, |
| 65 | uint8_t OSABI, bool Is64Bit) { |
Alex Bradbury | 8cc99f1 | 2017-10-10 07:19:18 +0000 | [diff] [blame] | 66 | return createELFObjectWriter( |
| 67 | llvm::make_unique<RISCVELFObjectWriter>(OSABI, Is64Bit), OS, |
| 68 | /*IsLittleEndian=*/false); |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 69 | } |