2016-11-02 07:47:30 +08:00
|
|
|
//===-- 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2017-09-28 16:26:24 +08:00
|
|
|
#include "MCTargetDesc/RISCVFixupKinds.h"
|
2016-11-02 07:47:30 +08:00
|
|
|
#include "MCTargetDesc/RISCVMCTargetDesc.h"
|
|
|
|
#include "llvm/MC/MCELFObjectWriter.h"
|
|
|
|
#include "llvm/MC/MCFixup.h"
|
2017-10-11 20:09:06 +08:00
|
|
|
#include "llvm/MC/MCObjectWriter.h"
|
2016-11-02 07:47:30 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
class RISCVELFObjectWriter : public MCELFObjectTargetWriter {
|
|
|
|
public:
|
|
|
|
RISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit);
|
|
|
|
|
|
|
|
~RISCVELFObjectWriter() override;
|
|
|
|
|
2018-04-25 22:18:55 +08:00
|
|
|
// Return true if the given relocation must be with a symbol rather than
|
|
|
|
// section plus offset.
|
|
|
|
bool needsRelocateWithSymbol(const MCSymbol &Sym,
|
|
|
|
unsigned Type) const override {
|
|
|
|
// TODO: this is very conservative, update once RISC-V psABI requirements
|
|
|
|
// are clarified.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-11-02 07:47:30 +08:00
|
|
|
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,
|
2017-08-20 14:55:14 +08:00
|
|
|
/*HasRelocationAddend*/ true) {}
|
2016-11-02 07:47:30 +08:00
|
|
|
|
|
|
|
RISCVELFObjectWriter::~RISCVELFObjectWriter() {}
|
|
|
|
|
|
|
|
unsigned RISCVELFObjectWriter::getRelocType(MCContext &Ctx,
|
|
|
|
const MCValue &Target,
|
|
|
|
const MCFixup &Fixup,
|
|
|
|
bool IsPCRel) const {
|
2017-09-28 16:26:24 +08:00
|
|
|
// 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;
|
[RISCV] Add symbol diff relocation support for RISC-V
For RISC-V it is desirable to have relaxation happen in the linker once
addresses are known, and as such the size between two instructions/byte
sequences in a section could change.
For most assembler expressions, this is fine, as the absolute address results
in the expression being converted to a fixup, and finally relocations.
However, for expressions such as .quad .L2-.L1, the assembler folds this down
to a constant once fragments are laid out, under the assumption that the
difference can no longer change, although in the case of linker relaxation the
differences can change at link time, so the constant is incorrect. One place
where this commonly appears is in debug information, where the size of a
function expression is in a form similar to the above.
This patch extends the assembler to allow an AsmBackend to declare that it
does not want the assembler to fold down this expression, and instead generate
a pair of relocations that allow the linker to carry out the calculation. In
this case, the expression is not folded, but when it comes to emitting a
fixup, the generic FK_Data_* fixups are converted into a pair, one for the
addition half, one for the subtraction, and this is passed to the relocation
generating methods as usual. I have named these FK_Data_Add_* and
FK_Data_Sub_* to indicate which half these are for.
For RISC-V, which supports this via e.g. the R_RISCV_ADD64, R_RISCV_SUB64 pair
of relocations, these are also set to always emit relocations relative to
local symbols rather than section offsets. This is to deal with the fact that
if relocations were calculated on e.g. .text+8 and .text+4, the result 12
would be stored rather than 4 as both addends are added in the linker.
Differential Revision: https://reviews.llvm.org/D45181
Patch by Simon Cook.
llvm-svn: 333079
2018-05-23 20:36:18 +08:00
|
|
|
case FK_Data_Add_1:
|
|
|
|
return ELF::R_RISCV_ADD8;
|
|
|
|
case FK_Data_Add_2:
|
|
|
|
return ELF::R_RISCV_ADD16;
|
|
|
|
case FK_Data_Add_4:
|
|
|
|
return ELF::R_RISCV_ADD32;
|
|
|
|
case FK_Data_Add_8:
|
|
|
|
return ELF::R_RISCV_ADD64;
|
|
|
|
case FK_Data_Sub_1:
|
|
|
|
return ELF::R_RISCV_SUB8;
|
|
|
|
case FK_Data_Sub_2:
|
|
|
|
return ELF::R_RISCV_SUB16;
|
|
|
|
case FK_Data_Sub_4:
|
|
|
|
return ELF::R_RISCV_SUB32;
|
|
|
|
case FK_Data_Sub_8:
|
|
|
|
return ELF::R_RISCV_SUB64;
|
2017-09-28 16:26:24 +08:00
|
|
|
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;
|
2018-02-06 08:55:23 +08:00
|
|
|
case RISCV::fixup_riscv_pcrel_lo12_i:
|
|
|
|
return ELF::R_RISCV_PCREL_LO12_I;
|
|
|
|
case RISCV::fixup_riscv_pcrel_lo12_s:
|
|
|
|
return ELF::R_RISCV_PCREL_LO12_S;
|
2017-09-28 16:26:24 +08:00
|
|
|
case RISCV::fixup_riscv_jal:
|
|
|
|
return ELF::R_RISCV_JAL;
|
|
|
|
case RISCV::fixup_riscv_branch:
|
|
|
|
return ELF::R_RISCV_BRANCH;
|
2017-12-07 21:19:57 +08:00
|
|
|
case RISCV::fixup_riscv_rvc_jump:
|
|
|
|
return ELF::R_RISCV_RVC_JUMP;
|
|
|
|
case RISCV::fixup_riscv_rvc_branch:
|
|
|
|
return ELF::R_RISCV_RVC_BRANCH;
|
2018-04-25 22:18:55 +08:00
|
|
|
case RISCV::fixup_riscv_call:
|
|
|
|
return ELF::R_RISCV_CALL;
|
2018-05-24 14:21:23 +08:00
|
|
|
case RISCV::fixup_riscv_relax:
|
|
|
|
return ELF::R_RISCV_RELAX;
|
2017-09-28 16:26:24 +08:00
|
|
|
}
|
2016-11-02 07:47:30 +08:00
|
|
|
}
|
|
|
|
|
2018-05-22 03:20:29 +08:00
|
|
|
std::unique_ptr<MCObjectTargetWriter>
|
|
|
|
llvm::createRISCVELFObjectWriter(uint8_t OSABI, bool Is64Bit) {
|
|
|
|
return llvm::make_unique<RISCVELFObjectWriter>(OSABI, Is64Bit);
|
2016-11-02 07:47:30 +08:00
|
|
|
}
|