Skip to content

Commit c64a3ff

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 bc627a4 commit c64a3ff

File tree

3 files changed

+51
-1
lines changed

3 files changed

+51
-1
lines changed
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_LO20_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
@@ -33,3 +33,20 @@
3333
nop
3434
# CHECK-ASM: nop
3535
# CHECK-OBJ: addi zero, zero, 0x0
36+
37+
.reloc ., R_RISCV_VENDOR, QUALCOMM
38+
.reloc ., R_RISCV_QC_LO20_U, my_bar + 2
39+
addi a1, a1, 0
40+
# CHECK-ASM: [[L3:.L[^:]+]]:
41+
# CHECK-ASM-NEXT: .reloc [[L3]], R_RISCV_VENDOR, QUALCOMM
42+
# CHECK-ASM-NEXT: [[L4:.L[^:]+]]:
43+
# CHECK-ASM-NEXT: .reloc [[L4]], R_RISCV_QC_LO20_U, my_bar+2
44+
# CHECK-ASM-NEXT: mv a1, a1
45+
46+
# CHECK-OBJ: addi a1, a1, 0
47+
# CHECK-OBJ-NEXT: R_RISCV_VENDOR QUALCOMM
48+
# CHECK-OBJ-NEXT: R_RISCV_CUSTOM192 my_bar+0x2
49+
50+
nop
51+
# CHECK-ASM: nop
52+
# CHECK-OBJ: addi zero, zero, 0x0

0 commit comments

Comments
 (0)