[llvm] [RISCV] Add Vendor Reloc and Fallback Names (PR #116974)

Sam Elliott via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 20 05:57:52 PST 2024


https://github.com/lenary created https://github.com/llvm/llvm-project/pull/116974

The trunk psABI document specifies that `R_RISCV_VENDOR` is 191, and that relocations 192-255 should be printed as `R_RISCV_CUSTOM<enum val>` in the absence of better information.

The testcase shows that these parse in `.reloc` directives, and also print correctly from both the assembler and objdump. There is almost certainly a better way to test this.

>From a9eeb161add8462c559b0f5bf37fc945846090bd Mon Sep 17 00:00:00 2001
From: Sam Elliott <quic_aelliott at quicinc.com>
Date: Wed, 20 Nov 2024 05:49:17 -0800
Subject: [PATCH] [RISCV] Add Vendor Reloc and Fallbacks

The trunk psABI document specifies that R_RISCV_VENDOR is 191, and that
relocations 192-255 should be printed as R_RISCV_CUSTOM<enum val> in
the absence of better information.
---
 .../llvm/BinaryFormat/ELFRelocs/RISCV.def     | 65 +++++++++++++++++++
 llvm/test/MC/RISCV/custom_reloc.s             | 43 ++++++++++++
 2 files changed, 108 insertions(+)
 create mode 100644 llvm/test/MC/RISCV/custom_reloc.s

diff --git a/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV.def b/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV.def
index d4be34e3b37e5e..2d7ee0814fe4a1 100644
--- a/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV.def
+++ b/llvm/include/llvm/BinaryFormat/ELFRelocs/RISCV.def
@@ -61,3 +61,68 @@ ELF_RELOC(R_RISCV_TLSDESC_HI20,      62)
 ELF_RELOC(R_RISCV_TLSDESC_LOAD_LO12, 63)
 ELF_RELOC(R_RISCV_TLSDESC_ADD_LO12,  64)
 ELF_RELOC(R_RISCV_TLSDESC_CALL,      65)
+ELF_RELOC(R_RISCV_VENDOR,           191)
+ELF_RELOC(R_RISCV_CUSTOM192,        192)
+ELF_RELOC(R_RISCV_CUSTOM193,        193)
+ELF_RELOC(R_RISCV_CUSTOM194,        194)
+ELF_RELOC(R_RISCV_CUSTOM195,        195)
+ELF_RELOC(R_RISCV_CUSTOM196,        196)
+ELF_RELOC(R_RISCV_CUSTOM197,        197)
+ELF_RELOC(R_RISCV_CUSTOM198,        198)
+ELF_RELOC(R_RISCV_CUSTOM199,        199)
+ELF_RELOC(R_RISCV_CUSTOM200,        200)
+ELF_RELOC(R_RISCV_CUSTOM201,        201)
+ELF_RELOC(R_RISCV_CUSTOM202,        202)
+ELF_RELOC(R_RISCV_CUSTOM203,        203)
+ELF_RELOC(R_RISCV_CUSTOM204,        204)
+ELF_RELOC(R_RISCV_CUSTOM205,        205)
+ELF_RELOC(R_RISCV_CUSTOM206,        206)
+ELF_RELOC(R_RISCV_CUSTOM207,        207)
+ELF_RELOC(R_RISCV_CUSTOM208,        208)
+ELF_RELOC(R_RISCV_CUSTOM209,        209)
+ELF_RELOC(R_RISCV_CUSTOM210,        210)
+ELF_RELOC(R_RISCV_CUSTOM211,        211)
+ELF_RELOC(R_RISCV_CUSTOM212,        212)
+ELF_RELOC(R_RISCV_CUSTOM213,        213)
+ELF_RELOC(R_RISCV_CUSTOM214,        214)
+ELF_RELOC(R_RISCV_CUSTOM215,        215)
+ELF_RELOC(R_RISCV_CUSTOM216,        216)
+ELF_RELOC(R_RISCV_CUSTOM217,        217)
+ELF_RELOC(R_RISCV_CUSTOM218,        218)
+ELF_RELOC(R_RISCV_CUSTOM219,        219)
+ELF_RELOC(R_RISCV_CUSTOM220,        220)
+ELF_RELOC(R_RISCV_CUSTOM221,        221)
+ELF_RELOC(R_RISCV_CUSTOM222,        222)
+ELF_RELOC(R_RISCV_CUSTOM223,        223)
+ELF_RELOC(R_RISCV_CUSTOM224,        224)
+ELF_RELOC(R_RISCV_CUSTOM225,        225)
+ELF_RELOC(R_RISCV_CUSTOM226,        226)
+ELF_RELOC(R_RISCV_CUSTOM227,        227)
+ELF_RELOC(R_RISCV_CUSTOM228,        228)
+ELF_RELOC(R_RISCV_CUSTOM229,        229)
+ELF_RELOC(R_RISCV_CUSTOM230,        230)
+ELF_RELOC(R_RISCV_CUSTOM231,        231)
+ELF_RELOC(R_RISCV_CUSTOM232,        232)
+ELF_RELOC(R_RISCV_CUSTOM233,        233)
+ELF_RELOC(R_RISCV_CUSTOM234,        234)
+ELF_RELOC(R_RISCV_CUSTOM235,        235)
+ELF_RELOC(R_RISCV_CUSTOM236,        236)
+ELF_RELOC(R_RISCV_CUSTOM237,        237)
+ELF_RELOC(R_RISCV_CUSTOM238,        238)
+ELF_RELOC(R_RISCV_CUSTOM239,        239)
+ELF_RELOC(R_RISCV_CUSTOM240,        240)
+ELF_RELOC(R_RISCV_CUSTOM241,        241)
+ELF_RELOC(R_RISCV_CUSTOM242,        242)
+ELF_RELOC(R_RISCV_CUSTOM243,        243)
+ELF_RELOC(R_RISCV_CUSTOM244,        244)
+ELF_RELOC(R_RISCV_CUSTOM245,        245)
+ELF_RELOC(R_RISCV_CUSTOM246,        246)
+ELF_RELOC(R_RISCV_CUSTOM247,        247)
+ELF_RELOC(R_RISCV_CUSTOM248,        248)
+ELF_RELOC(R_RISCV_CUSTOM249,        249)
+ELF_RELOC(R_RISCV_CUSTOM250,        250)
+ELF_RELOC(R_RISCV_CUSTOM251,        251)
+ELF_RELOC(R_RISCV_CUSTOM252,        252)
+ELF_RELOC(R_RISCV_CUSTOM253,        253)
+ELF_RELOC(R_RISCV_CUSTOM254,        254)
+ELF_RELOC(R_RISCV_CUSTOM255,        255)
diff --git a/llvm/test/MC/RISCV/custom_reloc.s b/llvm/test/MC/RISCV/custom_reloc.s
new file mode 100644
index 00000000000000..3058cffe4b5914
--- /dev/null
+++ b/llvm/test/MC/RISCV/custom_reloc.s
@@ -0,0 +1,43 @@
+# RUN: llvm-mc -triple riscv32 < %s \
+# RUN:   | FileCheck -check-prefix=CHECK-ASM %s
+# RUN: llvm-mc -triple riscv64 < %s \
+# RUN:   | FileCheck -check-prefix=CHECK-ASM %s
+
+# RUN: llvm-mc -filetype=obj -triple riscv32 < %s \
+# RUN:   | llvm-objdump -dr -M no-aliases - \
+# RUN:   | FileCheck -check-prefix=CHECK-OBJ %s
+# RUN: llvm-mc -filetype=obj -triple riscv64 < %s \
+# RUN:   | llvm-objdump -dr -M no-aliases  - \
+# RUN:   | FileCheck -check-prefix=CHECK-OBJ %s
+
+  # CHECK-ASM: .text
+
+  # If `.set` is used instead, the relocation expression will be evaluated.
+  # This leaves an undef, global symbol called VENDOR_NAME, which I'm not happy
+  # about.
+  .global VENDOR_NAME
+
+  # CHECK-OBJ: <.text>:
+
+  nop
+  # CHECK-ASM: nop
+  # CHECK-OBJ: addi zero, zero, 0x0
+
+  .reloc ., R_RISCV_VENDOR,    VENDOR_NAME
+  .reloc ., R_RISCV_CUSTOM192, my_foo + 1
+  addi a0, a0, 0
+  # 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-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
+
+



More information about the llvm-commits mailing list