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" |
Alex Bradbury | 5c1eef4 | 2017-10-11 12:09:06 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCObjectWriter.h" |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 15 | #include "llvm/Support/ErrorHandling.h" |
| 16 | |
| 17 | using namespace llvm; |
| 18 | |
| 19 | namespace { |
| 20 | class RISCVELFObjectWriter : public MCELFObjectTargetWriter { |
| 21 | public: |
| 22 | RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit); |
| 23 | |
| 24 | ~RISCVELFObjectWriter() override; |
| 25 | |
Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 26 | // 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 Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 35 | protected: |
| 36 | unsigned getRelocType(MCContext &Ctx, const MCValue &Target, |
| 37 | const MCFixup &Fixup, bool IsPCRel) const override; |
| 38 | }; |
| 39 | } |
| 40 | |
| 41 | RISCVELFObjectWriter::RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit) |
| 42 | : MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_RISCV, |
Alex Bradbury | dd83484 | 2017-08-20 06:55:14 +0000 | [diff] [blame] | 43 | /*HasRelocationAddend*/ true) {} |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 44 | |
| 45 | RISCVELFObjectWriter::~RISCVELFObjectWriter() {} |
| 46 | |
| 47 | unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx, |
| 48 | const MCValue &Target, |
| 49 | const MCFixup &Fixup, |
| 50 | bool IsPCRel) const { |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 51 | // 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; |
| 59 | case RISCV::fixup_riscv_hi20: |
| 60 | return ELF::R_RISCV_HI20; |
| 61 | case RISCV::fixup_riscv_lo12_i: |
| 62 | return ELF::R_RISCV_LO12_I; |
| 63 | case RISCV::fixup_riscv_lo12_s: |
| 64 | return ELF::R_RISCV_LO12_S; |
| 65 | case RISCV::fixup_riscv_pcrel_hi20: |
| 66 | return ELF::R_RISCV_PCREL_HI20; |
Ahmed Charles | 646ab87 | 2018-02-06 00:55:23 +0000 | [diff] [blame] | 67 | case RISCV::fixup_riscv_pcrel_lo12_i: |
| 68 | return ELF::R_RISCV_PCREL_LO12_I; |
| 69 | case RISCV::fixup_riscv_pcrel_lo12_s: |
| 70 | return ELF::R_RISCV_PCREL_LO12_S; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 71 | case RISCV::fixup_riscv_jal: |
| 72 | return ELF::R_RISCV_JAL; |
| 73 | case RISCV::fixup_riscv_branch: |
| 74 | return ELF::R_RISCV_BRANCH; |
Alex Bradbury | f8f4b90 | 2017-12-07 13:19:57 +0000 | [diff] [blame] | 75 | case RISCV::fixup_riscv_rvc_jump: |
| 76 | return ELF::R_RISCV_RVC_JUMP; |
| 77 | case RISCV::fixup_riscv_rvc_branch: |
| 78 | return ELF::R_RISCV_RVC_BRANCH; |
Shiva Chen | 98f9389 | 2018-04-25 14:18:55 +0000 | [diff] [blame] | 79 | case RISCV::fixup_riscv_call: |
| 80 | return ELF::R_RISCV_CALL; |
Alex Bradbury | 9d3f125 | 2017-09-28 08:26:24 +0000 | [diff] [blame] | 81 | } |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Peter Collingbourne | dcd7d6c | 2018-05-21 19:20:29 +0000 | [diff] [blame] | 84 | std::unique_ptr<MCObjectTargetWriter> |
| 85 | llvm::createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit) { |
| 86 | return llvm::make_unique<RISCVELFObjectWriter>(OSABI, Is64Bit); |
Alex Bradbury | 6b2cca7 | 2016-11-01 23:47:30 +0000 | [diff] [blame] | 87 | } |