Skip to content

Commit 5e55cab

Browse files
committed
[RISCV] Support Parsing Nonstandard Relocations
This allows nonstandard relocation names to be used in `.reloc` assembly directives (giving the correct relocation number). No translation is done by the assembler into `R_RISCV_CUSTOM<n>` names, and the assembler does not automatically add the relevant `R_RISCV_VENDOR` relocation with the vendor symbol. If we want, we can have a different directive that does this later. The first batch of relocations to be added are from Qualcomm's RISC-V psABI extensions.
1 parent bb27d5e commit 5e55cab

File tree

4 files changed

+54
-1
lines changed

4 files changed

+54
-1
lines changed

llvm/include/llvm/BinaryFormat/ELF.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,9 @@ enum : unsigned {
689689
// ELF Relocation types for RISC-V
690690
enum {
691691
#include "ELFRelocs/RISCV.def"
692+
#define ELF_RISCV_NONSTANDARD_RELOC(_vendor, name, value) name = value,
693+
#include "ELFRelocs/RISCV_nonstandard.def"
694+
#undef ELF_RISCV_NONSTANDARD_RELOC
692695
};
693696

694697
enum {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===--- RISC-V Nonstandard Relocation List ---------------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef ELF_RISCV_NONSTANDARD_RELOC
10+
#error "ELF_RISCV_NONSTANDARD_RELOC must be defined"
11+
#endif
12+
13+
/*
14+
ELF_RISCV_NONSTANDARD_RELOC(VENDOR, NAME, ID) defines information about
15+
nonstandard relocation codes. This can be used when parsing relocations, or
16+
when printing them, to provide better information.
17+
18+
VENDOR should be the symbol name expected in the associated `R_RISCV_VENDOR`
19+
relocation. NAME and ID work like `ELF_RELOC` but the mapping is not expected
20+
to be 1:1.
21+
22+
The mapping in RISCV.def is 1:1, and should be used when the only information
23+
available is the relocation enum value.
24+
*/
25+
26+
/* Qualcomm Nonstandard Relocations */
27+
ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_ABS20_U, 192)
28+
ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_BRANCH, 193)
29+
ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_32, 194)
30+
ELF_RISCV_NONSTANDARD_RELOC(QUALCOMM, R_RISCV_QC_E_JUMP_PLT, 195)

llvm/lib/Target/RISCV/MCTargetDesc/RISCVAsmBackend.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,12 @@ std::optional<MCFixupKind> RISCVAsmBackend::getFixupKind(StringRef Name) const {
3838
if (STI.getTargetTriple().isOSBinFormatELF()) {
3939
unsigned Type;
4040
Type = llvm::StringSwitch<unsigned>(Name)
41-
#define ELF_RELOC(X, Y) .Case(#X, Y)
41+
#define ELF_RELOC(NAME, ID) .Case(#NAME, ID)
4242
#include "llvm/BinaryFormat/ELFRelocs/RISCV.def"
4343
#undef ELF_RELOC
44+
#define ELF_RISCV_NONSTANDARD_RELOC(_VENDOR, NAME, ID) .Case(#NAME, ID)
45+
#include "llvm/BinaryFormat/ELFRelocs/RISCV_nonstandard.def"
46+
#undef ELF_RISCV_NONSTANDARD_RELOC
4447
.Case("BFD_RELOC_NONE", ELF::R_RISCV_NONE)
4548
.Case("BFD_RELOC_32", ELF::R_RISCV_32)
4649
.Case("BFD_RELOC_64", ELF::R_RISCV_64)

llvm/test/MC/RISCV/custom_reloc.s

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,20 @@
3434
nop
3535
# CHECK-ASM: nop
3636
# CHECK-OBJ: addi zero, zero, 0x0
37+
38+
.reloc ., R_RISCV_VENDOR, QUALCOMM
39+
.reloc ., R_RISCV_QC_ABS20_U, my_bar + 2
40+
addi a1, a1, 0
41+
# CHECK-ASM: [[L3:.L[^:]+]]:
42+
# CHECK-ASM-NEXT: .reloc [[L3]], R_RISCV_VENDOR, QUALCOMM
43+
# CHECK-ASM-NEXT: [[L4:.L[^:]+]]:
44+
# CHECK-ASM-NEXT: .reloc [[L4]], R_RISCV_QC_ABS20_U, my_bar+2
45+
# CHECK-ASM-NEXT: mv a1, a1
46+
47+
# CHECK-OBJ: addi a1, a1, 0
48+
# CHECK-OBJ-NEXT: R_RISCV_VENDOR QUALCOMM
49+
# CHECK-OBJ-NEXT: R_RISCV_CUSTOM192 my_bar+0x2
50+
51+
nop
52+
# CHECK-ASM: nop
53+
# CHECK-OBJ: addi zero, zero, 0x0

0 commit comments

Comments
 (0)