mirror of
https://github.com/Gericom/teak-llvm.git
synced 2025-06-26 14:58:59 -04:00

%lo(), %hi(), and %pcrel_hi() are supported and test cases have been added to ensure the appropriate fixups and relocations are generated. I've added an instruction format field which is used in RISCVMCCodeEmitter to, for instance, tell whether it should emit a lo12_i fixup or a lo12_s fixup (RISC-V has two 12-bit immediate encodings depending on the instruction type). Differential Revision: https://reviews.llvm.org/D23568 llvm-svn: 314389
69 lines
2.3 KiB
C++
69 lines
2.3 KiB
C++
//===-- RISCVELFObjectWriter.cpp - RISCV ELF Writer -----------------------===//
|
|
//
|
|
// The LLVM Compiler Infrastructure
|
|
//
|
|
// This file is distributed under the University of Illinois Open Source
|
|
// License. See LICENSE.TXT for details.
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "MCTargetDesc/RISCVFixupKinds.h"
|
|
#include "MCTargetDesc/RISCVMCTargetDesc.h"
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
|
#include "llvm/MC/MCFixup.h"
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
using namespace llvm;
|
|
|
|
namespace {
|
|
class RISCVELFObjectWriter : public MCELFObjectTargetWriter {
|
|
public:
|
|
RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit);
|
|
|
|
~RISCVELFObjectWriter() override;
|
|
|
|
protected:
|
|
unsigned getRelocType(MCContext &Ctx, const MCValue &Target,
|
|
const MCFixup &Fixup, bool IsPCRel) const override;
|
|
};
|
|
}
|
|
|
|
RISCVELFObjectWriter::RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit)
|
|
: MCELFObjectTargetWriter(Is64Bit, OSABI, ELF::EM_RISCV,
|
|
/*HasRelocationAddend*/ true) {}
|
|
|
|
RISCVELFObjectWriter::~RISCVELFObjectWriter() {}
|
|
|
|
unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
|
|
const MCValue &Target,
|
|
const MCFixup &Fixup,
|
|
bool IsPCRel) const {
|
|
// Determine the type of the relocation
|
|
switch ((unsigned)Fixup.getKind()) {
|
|
default:
|
|
llvm_unreachable("invalid fixup kind!");
|
|
case FK_Data_4:
|
|
return ELF::R_RISCV_32;
|
|
case FK_Data_8:
|
|
return ELF::R_RISCV_64;
|
|
case RISCV::fixup_riscv_hi20:
|
|
return ELF::R_RISCV_HI20;
|
|
case RISCV::fixup_riscv_lo12_i:
|
|
return ELF::R_RISCV_LO12_I;
|
|
case RISCV::fixup_riscv_lo12_s:
|
|
return ELF::R_RISCV_LO12_S;
|
|
case RISCV::fixup_riscv_pcrel_hi20:
|
|
return ELF::R_RISCV_PCREL_HI20;
|
|
case RISCV::fixup_riscv_jal:
|
|
return ELF::R_RISCV_JAL;
|
|
case RISCV::fixup_riscv_branch:
|
|
return ELF::R_RISCV_BRANCH;
|
|
}
|
|
}
|
|
|
|
MCObjectWriter *llvm::createRISCVELFObjectWriter(raw_pwrite_stream &OS,
|
|
uint8_t OSABI, bool Is64Bit) {
|
|
MCELFObjectTargetWriter *MOTW = new RISCVELFObjectWriter(OSABI, Is64Bit);
|
|
return createELFObjectWriter(MOTW, OS, /*IsLittleEndian*/ true);
|
|
}
|