|
23 | 23 |
|
24 | 24 | using namespace llvm;
|
25 | 25 |
|
| 26 | +Optional<MCFixupKind> RISCVAsmBackend::getFixupKind(StringRef Name) const { |
| 27 | + if (STI.getTargetTriple().isOSBinFormatELF()) { |
| 28 | + unsigned Type; |
| 29 | + Type = llvm::StringSwitch<unsigned>(Name) |
| 30 | +#define ELF_RELOC(X, Y) .Case(#X, Y) |
| 31 | +#include "llvm/BinaryFormat/ELFRelocs/RISCV.def" |
| 32 | +#undef ELF_RELOC |
| 33 | + .Default(-1u); |
| 34 | + if (Type != -1u) |
| 35 | + return static_cast<MCFixupKind>(FirstLiteralRelocationKind + Type); |
| 36 | + } |
| 37 | + return None; |
| 38 | +} |
| 39 | + |
| 40 | +const MCFixupKindInfo & |
| 41 | +RISCVAsmBackend::getFixupKindInfo(MCFixupKind Kind) const { |
| 42 | + const static MCFixupKindInfo Infos[] = { |
| 43 | + // This table *must* be in the order that the fixup_* kinds are defined in |
| 44 | + // RISCVFixupKinds.h. |
| 45 | + // |
| 46 | + // name offset bits flags |
| 47 | + {"fixup_riscv_hi20", 12, 20, 0}, |
| 48 | + {"fixup_riscv_lo12_i", 20, 12, 0}, |
| 49 | + {"fixup_riscv_lo12_s", 0, 32, 0}, |
| 50 | + {"fixup_riscv_pcrel_hi20", 12, 20, |
| 51 | + MCFixupKindInfo::FKF_IsPCRel | MCFixupKindInfo::FKF_IsTarget}, |
| 52 | + {"fixup_riscv_pcrel_lo12_i", 20, 12, |
| 53 | + MCFixupKindInfo::FKF_IsPCRel | MCFixupKindInfo::FKF_IsTarget}, |
| 54 | + {"fixup_riscv_pcrel_lo12_s", 0, 32, |
| 55 | + MCFixupKindInfo::FKF_IsPCRel | MCFixupKindInfo::FKF_IsTarget}, |
| 56 | + {"fixup_riscv_got_hi20", 12, 20, MCFixupKindInfo::FKF_IsPCRel}, |
| 57 | + {"fixup_riscv_tprel_hi20", 12, 20, 0}, |
| 58 | + {"fixup_riscv_tprel_lo12_i", 20, 12, 0}, |
| 59 | + {"fixup_riscv_tprel_lo12_s", 0, 32, 0}, |
| 60 | + {"fixup_riscv_tprel_add", 0, 0, 0}, |
| 61 | + {"fixup_riscv_tls_got_hi20", 12, 20, MCFixupKindInfo::FKF_IsPCRel}, |
| 62 | + {"fixup_riscv_tls_gd_hi20", 12, 20, MCFixupKindInfo::FKF_IsPCRel}, |
| 63 | + {"fixup_riscv_jal", 12, 20, MCFixupKindInfo::FKF_IsPCRel}, |
| 64 | + {"fixup_riscv_branch", 0, 32, MCFixupKindInfo::FKF_IsPCRel}, |
| 65 | + {"fixup_riscv_rvc_jump", 2, 11, MCFixupKindInfo::FKF_IsPCRel}, |
| 66 | + {"fixup_riscv_rvc_branch", 0, 16, MCFixupKindInfo::FKF_IsPCRel}, |
| 67 | + {"fixup_riscv_call", 0, 64, MCFixupKindInfo::FKF_IsPCRel}, |
| 68 | + {"fixup_riscv_call_plt", 0, 64, MCFixupKindInfo::FKF_IsPCRel}, |
| 69 | + {"fixup_riscv_relax", 0, 0, 0}, |
| 70 | + {"fixup_riscv_align", 0, 0, 0}}; |
| 71 | + static_assert((array_lengthof(Infos)) == RISCV::NumTargetFixupKinds, |
| 72 | + "Not all fixup kinds added to Infos array"); |
| 73 | + |
| 74 | + // Fixup kinds from .reloc directive are like R_RISCV_NONE. They |
| 75 | + // do not require any extra processing. |
| 76 | + if (Kind >= FirstLiteralRelocationKind) |
| 77 | + return MCAsmBackend::getFixupKindInfo(FK_NONE); |
| 78 | + |
| 79 | + if (Kind < FirstTargetFixupKind) |
| 80 | + return MCAsmBackend::getFixupKindInfo(Kind); |
| 81 | + |
| 82 | + assert(unsigned(Kind - FirstTargetFixupKind) < getNumFixupKinds() && |
| 83 | + "Invalid kind!"); |
| 84 | + return Infos[Kind - FirstTargetFixupKind]; |
| 85 | +} |
| 86 | + |
26 | 87 | // If linker relaxation is enabled, or the relax option had previously been
|
27 | 88 | // enabled, always emit relocations even if the fixup can be resolved. This is
|
28 | 89 | // necessary for correctness as offsets may change during relaxation.
|
29 | 90 | bool RISCVAsmBackend::shouldForceRelocation(const MCAssembler &Asm,
|
30 | 91 | const MCFixup &Fixup,
|
31 | 92 | const MCValue &Target) {
|
| 93 | + if (Fixup.getKind() >= FirstLiteralRelocationKind) |
| 94 | + return true; |
32 | 95 | switch (Fixup.getTargetKind()) {
|
33 | 96 | default:
|
34 | 97 | break;
|
@@ -318,8 +381,11 @@ void RISCVAsmBackend::applyFixup(const MCAssembler &Asm, const MCFixup &Fixup,
|
318 | 381 | MutableArrayRef<char> Data, uint64_t Value,
|
319 | 382 | bool IsResolved,
|
320 | 383 | const MCSubtargetInfo *STI) const {
|
| 384 | + MCFixupKind Kind = Fixup.getKind(); |
| 385 | + if (Kind >= FirstLiteralRelocationKind) |
| 386 | + return; |
321 | 387 | MCContext &Ctx = Asm.getContext();
|
322 |
| - MCFixupKindInfo Info = getFixupKindInfo(Fixup.getKind()); |
| 388 | + MCFixupKindInfo Info = getFixupKindInfo(Kind); |
323 | 389 | if (!Value)
|
324 | 390 | return; // Doesn't change encoding.
|
325 | 391 | // Apply any target-specific value adjustments.
|
|
0 commit comments