Skip to content

[RISCV] Support Parsing Nonstandard Relocations #119909

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions llvm/include/llvm/BinaryFormat/ELF.h
Original file line number Diff line number Diff line change
Expand Up @@ -689,6 +689,9 @@ enum : unsigned {
// ELF Relocation types for RISC-V
enum {
#include "ELFRelocs/RISCV.def"
#define ELF_RISCV_NONSTANDARD_RELOC(_vendor, name, value) name = value,
#include "ELFRelocs/RISCV_nonstandard.def"
#undef ELF_RISCV_NONSTANDARD_RELOC
};

enum {
Expand Down
28 changes: 28 additions & 0 deletions llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//===--- RISC-V Nonstandard Relocation List ---------------------*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef ELF_RISCV_NONSTANDARD_RELOC
#error "ELF_RISCV_NONSTANDARD_RELOC must be defined"
#endif

// ELF_RISCV_NONSTANDARD_RELOC(VENDOR, NAME, ID) defines information about
// nonstandard relocation codes. This can be used when parsing relocations, or
// when printing them, to provide better information.
//
// VENDOR should be the symbol name expected in the associated `R_RISCV_VENDOR`
// relocation. NAME and ID work like `ELF_RELOC` but the mapping is not expected
// to be 1:1.
//
// The mapping in RISCV.def is 1:1, and should be used when the only information
// available is the relocation enum value.

// Qualcomm Nonstandard Relocations
ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_ABS20_U, 192)
ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_BRANCH, 193)
ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_32, 194)
ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_JUMP_PLT, 195)
5 changes: 4 additions & 1 deletion llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,12 @@ std::optional<MCFixupKind> RISCVAsmBackend::getFixupKind(StringRef Name) const {
if (STI.getTargetTriple().isOSBinFormatELF()) {
unsigned Type;
Type = llvm::StringSwitch<unsigned>(Name)
#define ELF_RELOC(X, Y) .Case(#X, Y)
#define ELF_RELOC(NAME, ID) .Case(#NAME, ID)
#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
#undef ELF_RELOC
#define ELF_RISCV_NONSTANDARD_RELOC(_VENDOR, NAME, ID) .Case(#NAME, ID)
#include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
#undef ELF_RISCV_NONSTANDARD_RELOC
.Case("BFD_RELOC_NONE", ELF::R_RISCV_NONE)
.Case("BFD_RELOC_32", ELF::R_RISCV_32)
.Case("BFD_RELOC_64", ELF::R_RISCV_64)
Expand Down
21 changes: 19 additions & 2 deletions llvm/test/MC/RISCV/custom_reloc.s
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,33 @@
.reloc ., R_RISCV_VENDOR, VENDOR_NAME
.reloc ., R_RISCV_CUSTOM192, my_foo + 1
addi a0, a0, 0
# CHECK-ASM: [[L1:.L[^:]+]]:
# CHECK-ASM: [[L1:.L[^:]+]]:
# CHECK-ASM-NEXT: .reloc [[L1]], R_RISCV_VENDOR, VENDOR_NAME
# CHECK-ASM-NEXT: [[L2:.L[^:]+]]:
# CHECK-ASM-NEXT: .reloc [[L2]], R_RISCV_CUSTOM192, my_foo+1
# CHECK-ASM-NEXT: mv a0, a0

# CHECK-OBJ: addi a0, a0, 0
# CHECK-OBJ: addi a0, a0, 0
# CHECK-OBJ-NEXT: R_RISCV_VENDOR VENDOR_NAME
# CHECK-OBJ-NEXT: R_RISCV_CUSTOM192 my_foo+0x1

nop
# CHECK-ASM: nop
# CHECK-OBJ: addi zero, zero, 0x0

.reloc ., R_RISCV_VENDOR, QUALCOMM
.reloc ., R_RISCV_QC_ABS20_U, my_bar + 2
addi a1, a1, 0
# CHECK-ASM: [[L3:.L[^:]+]]:
# CHECK-ASM-NEXT: .reloc [[L3]], R_RISCV_VENDOR, QUALCOMM
# CHECK-ASM-NEXT: [[L4:.L[^:]+]]:
# CHECK-ASM-NEXT: .reloc [[L4]], R_RISCV_QC_ABS20_U, my_bar+2
# CHECK-ASM-NEXT: mv a1, a1

# CHECK-OBJ: addi a1, a1, 0
# CHECK-OBJ-NEXT: R_RISCV_VENDOR QUALCOMM
# CHECK-OBJ-NEXT: R_RISCV_CUSTOM192 my_bar+0x2

nop
# CHECK-ASM: nop
# CHECK-OBJ: addi zero, zero, 0x0
Loading