Skip to content

Conversation

@kito-cheng
Copy link
Member

@kito-cheng kito-cheng commented Dec 18, 2025

Use getRISCVVendorRelocationTypeName to resolve RISCV vendor-specific relocation names (R_RISCV_CUSTOM192-255) when preceded by R_RISCV_VENDOR.

This improves the output of llvm-readobj and llvm-objdump to show vendor-specific names like R_RISCV_QC_ABS20_U, R_RISCV_QC_E_BRANCH (QUALCOMM) and R_RISCV_NDS_BRANCH_10 (ANDES) instead of generic R_RISCV_CUSTOM* names.

Per RISC-V psABI, R_RISCV_VENDOR must be placed immediately before its associated vendor-specific relocation, so the vendor symbol is consumed after one use. Unknown vendors fall back to R_RISCV_CUSTOM*.

…me for RISCV vendor relocations

Use getRISCVVendorRelocationTypeName to resolve RISCV vendor-specific
relocation names (R_RISCV_CUSTOM192-255) when preceded by R_RISCV_VENDOR.

This improves the output of llvm-readobj and llvm-objdump to show
vendor-specific names like R_RISCV_QC_ABS20_U, R_RISCV_QC_E_BRANCH
(QUALCOMM) and R_RISCV_NDS_BRANCH_10 (ANDES) instead of generic
R_RISCV_CUSTOM* names.

Per RISC-V psABI, R_RISCV_VENDOR must be placed immediately before its
associated vendor-specific relocation, so the vendor symbol is consumed
after one use. Unknown vendors fall back to R_RISCV_CUSTOM*.
@llvmbot
Copy link
Member

llvmbot commented Dec 18, 2025

@llvm/pr-subscribers-backend-risc-v

@llvm/pr-subscribers-llvm-binary-utilities

Author: Kito Cheng (kito-cheng)

Changes

…me for RISCV vendor relocations

Use getRISCVVendorRelocationTypeName to resolve RISCV vendor-specific relocation names (R_RISCV_CUSTOM192-255) when preceded by R_RISCV_VENDOR.

This improves the output of llvm-readobj and llvm-objdump to show vendor-specific names like R_RISCV_QC_ABS20_U, R_RISCV_QC_E_BRANCH (QUALCOMM) and R_RISCV_NDS_BRANCH_10 (ANDES) instead of generic R_RISCV_CUSTOM* names.

Per RISC-V psABI, R_RISCV_VENDOR must be placed immediately before its associated vendor-specific relocation, so the vendor symbol is consumed after one use. Unknown vendors fall back to R_RISCV_CUSTOM*.


Patch is 27.08 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/172811.diff

13 Files Affected:

  • (modified) llvm/test/MC/RISCV/custom_reloc.s (+2-2)
  • (modified) llvm/test/MC/RISCV/rv32-relaxation-xqci.s (+6-6)
  • (modified) llvm/test/MC/RISCV/vendor-symbol.s (+8-8)
  • (modified) llvm/test/MC/RISCV/xandesperf-relocation.s (+4-4)
  • (modified) llvm/test/MC/RISCV/xqcibi-linker-relaxation.s (+2-2)
  • (modified) llvm/test/MC/RISCV/xqcibi-relocations.s (+3-3)
  • (modified) llvm/test/MC/RISCV/xqcilb-relocations.s (+10-10)
  • (modified) llvm/test/MC/RISCV/xqcili-linker-relaxation.s (+2-2)
  • (modified) llvm/test/MC/RISCV/xqcili-relocations.s (+11-11)
  • (added) llvm/test/tools/llvm-objdump/ELF/RISCV/riscv-vendor-reloc.s (+64)
  • (added) llvm/test/tools/llvm-readobj/ELF/RISCV/riscv-vendor-reloc.s (+62)
  • (modified) llvm/tools/llvm-objdump/llvm-objdump.cpp (+40-7)
  • (modified) llvm/tools/llvm-readobj/ELFDumper.cpp (+44-8)
diff --git a/llvm/test/MC/RISCV/custom_reloc.s b/llvm/test/MC/RISCV/custom_reloc.s
index a68f71063ea92..b5e1ef90ca589 100644
--- a/llvm/test/MC/RISCV/custom_reloc.s
+++ b/llvm/test/MC/RISCV/custom_reloc.s
@@ -46,7 +46,7 @@
 
   # CHECK-OBJ:      addi a1, a1, 0
   # CHECK-OBJ-NEXT: R_RISCV_VENDOR    QUALCOMM
-  # CHECK-OBJ-NEXT: R_RISCV_CUSTOM192 my_bar+0x2
+  # CHECK-OBJ-NEXT: R_RISCV_QC_ABS20_U my_bar+0x2
 
   .reloc ., R_RISCV_VENDOR,     ANDES
   .reloc ., R_RISCV_NDS_BRANCH_10, my_bar + 2
@@ -59,7 +59,7 @@
 
   # CHECK-OBJ:      addi a1, a1, 0
   # CHECK-OBJ-NEXT: R_RISCV_VENDOR    ANDES
-  # CHECK-OBJ-NEXT: R_RISCV_CUSTOM241 my_bar+0x2
+  # CHECK-OBJ-NEXT: R_RISCV_NDS_BRANCH_10 my_bar+0x2
 
   nop
   # CHECK-ASM: nop
diff --git a/llvm/test/MC/RISCV/rv32-relaxation-xqci.s b/llvm/test/MC/RISCV/rv32-relaxation-xqci.s
index b38aa373c90f0..37a55cc3be753 100644
--- a/llvm/test/MC/RISCV/rv32-relaxation-xqci.s
+++ b/llvm/test/MC/RISCV/rv32-relaxation-xqci.s
@@ -40,7 +40,7 @@ start:
 # CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
   c.j undef
 # CHECK: qc.e.j {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
-# CHECK: R_RISCV_CUSTOM195 undef
+# CHECK: R_RISCV_QC_E_CALL_PLT undef
 
   c.jal NEAR
 # CHECK: c.jal {{0x[0-9a-f]+}} <NEAR>
@@ -56,7 +56,7 @@ start:
 # CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
   c.jal undef
 # CHECK: qc.e.jal {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
-# CHECK: R_RISCV_CUSTOM195 undef
+# CHECK: R_RISCV_QC_E_CALL_PLT undef
 
   jal zero, NEAR
 # CHECK: c.j {{0x[0-9a-f]+}} <NEAR>
@@ -72,7 +72,7 @@ start:
 # CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
   jal zero, undef
 # CHECK: qc.e.j {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
-# CHECK: R_RISCV_CUSTOM195 undef
+# CHECK: R_RISCV_QC_E_CALL_PLT undef
 
   jal ra, NEAR
 # CHECK: c.jal {{0x[0-9a-f]+}} <NEAR>
@@ -88,7 +88,7 @@ start:
 # CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
   jal ra, undef
 # CHECK: qc.e.jal {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
-# CHECK: R_RISCV_CUSTOM195 undef
+# CHECK: R_RISCV_QC_E_CALL_PLT undef
 
   qc.e.j NEAR
 # CHECK: c.j {{0x[0-9a-f]+}} <NEAR>
@@ -104,7 +104,7 @@ start:
 # CHECK: qc.e.j {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
   qc.e.j undef
 # CHECK: qc.e.j {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
-# CHECK: R_RISCV_CUSTOM195 undef
+# CHECK: R_RISCV_QC_E_CALL_PLT undef
 
   qc.e.jal NEAR
 # CHECK: c.jal {{0x[0-9a-f]+}} <NEAR>
@@ -120,7 +120,7 @@ start:
 # CHECK: qc.e.jal {{0x[0-9a-f]+}} <EXT_JUMP_NEGATIVE>
   qc.e.jal undef
 # CHECK: qc.e.jal {{0x[0-9a-f]+}} <start+{{0x[0-9a-f]+}}>
-# CHECK: R_RISCV_CUSTOM195 undef
+# CHECK: R_RISCV_QC_E_CALL_PLT undef
 
 
 
diff --git a/llvm/test/MC/RISCV/vendor-symbol.s b/llvm/test/MC/RISCV/vendor-symbol.s
index 9595f218d78fa..d075d69f89ea8 100644
--- a/llvm/test/MC/RISCV/vendor-symbol.s
+++ b/llvm/test/MC/RISCV/vendor-symbol.s
@@ -29,14 +29,14 @@ ANDES:
 
 # CHECK-LABEL: Relocation section '.rela.text'
 ## Note the different values for the "Sym. Value" Field
-# CHECK: R_RISCV_VENDOR    00000000 QUALCOMM + 0
-# CHECK: R_RISCV_CUSTOM193 00000006 QUALCOMM + 0
-# CHECK: R_RISCV_VENDOR    00000000 QUALCOMM + 0
-# CHECK: R_RISCV_CUSTOM193 00000006 QUALCOMM + 0
-# CHECK: R_RISCV_VENDOR    00000000 ANDES + 0
-# CHECK: R_RISCV_CUSTOM241 00000014 ANDES + 0
-# CHECK: R_RISCV_VENDOR    00000000 ANDES + 0
-# CHECK: R_RISCV_CUSTOM241 00000014 ANDES + 0
+# CHECK: R_RISCV_VENDOR       00000000 QUALCOMM + 0
+# CHECK: R_RISCV_QC_E_BRANCH  00000006 QUALCOMM + 0
+# CHECK: R_RISCV_VENDOR       00000000 QUALCOMM + 0
+# CHECK: R_RISCV_QC_E_BRANCH  00000006 QUALCOMM + 0
+# CHECK: R_RISCV_VENDOR       00000000 ANDES + 0
+# CHECK: R_RISCV_NDS_BRANCH_10 00000014 ANDES + 0
+# CHECK: R_RISCV_VENDOR       00000000 ANDES + 0
+# CHECK: R_RISCV_NDS_BRANCH_10 00000014 ANDES + 0
 
 
 # CHECK-LABEL: Symbol table '.symtab'
diff --git a/llvm/test/MC/RISCV/xandesperf-relocation.s b/llvm/test/MC/RISCV/xandesperf-relocation.s
index 4df75f04dbfe8..3feaa5f2b5f6f 100644
--- a/llvm/test/MC/RISCV/xandesperf-relocation.s
+++ b/llvm/test/MC/RISCV/xandesperf-relocation.s
@@ -14,23 +14,23 @@
 # ASM: nds.bbc t0, 7, foo
 # OBJ: nds.bbc t0, 0x7, 0x4 <.text+0x4>
 # OBJ-NEXT: R_RISCV_VENDOR ANDES{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM241 foo{{$}}
+# OBJ-NEXT: R_RISCV_NDS_BRANCH_10 foo{{$}}
 nds.bbc t0, 7, foo
 
 # ASM: nds.bbs t0, 7, foo
 # OBJ-NEXT: nds.bbs t0, 0x7, 0x8 <.text+0x8>
 # OBJ-NEXT: R_RISCV_VENDOR ANDES{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM241 foo{{$}}
+# OBJ-NEXT: R_RISCV_NDS_BRANCH_10 foo{{$}}
 nds.bbs t0, 7, foo
 
 # ASM: nds.beqc t0, 7, foo
 # OBJ-NEXT: nds.beqc t0, 0x7, 0xc <.text+0xc>
 # OBJ-NEXT: R_RISCV_VENDOR ANDES{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM241 foo{{$}}
+# OBJ-NEXT: R_RISCV_NDS_BRANCH_10 foo{{$}}
 nds.beqc t0, 7, foo
 
 # ASM: nds.bnec t0, 7, foo
 # OBJ-NEXT: nds.bnec t0, 0x7, 0x10 <.text+0x10>
 # OBJ-NEXT: R_RISCV_VENDOR ANDES{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM241 foo{{$}}
+# OBJ-NEXT: R_RISCV_NDS_BRANCH_10 foo{{$}}
 nds.bnec t0, 7, foo
diff --git a/llvm/test/MC/RISCV/xqcibi-linker-relaxation.s b/llvm/test/MC/RISCV/xqcibi-linker-relaxation.s
index 2066b55b41ad3..87ec3a075226f 100644
--- a/llvm/test/MC/RISCV/xqcibi-linker-relaxation.s
+++ b/llvm/test/MC/RISCV/xqcibi-linker-relaxation.s
@@ -15,7 +15,7 @@ branch_over_relaxable:
   jal x1, foo
 # CHECK: qc.e.jal 0x0 <branch_over_relaxable>
 # CHECK-NEXT: R_RISCV_VENDOR QUALCOMM
-# CHECK-NEXT: R_RISCV_CUSTOM195 foo
+# CHECK-NEXT: R_RISCV_QC_E_CALL_PLT foo
 # CHECK-NEXT: R_RISCV_RELAX *ABS*
   bne a0, a1, branch_over_relaxable
 # CHECK-NEXT: bne a0, a1, 0x6 <branch_over_relaxable+0x6>
@@ -24,7 +24,7 @@ branch_over_relaxable:
   qc.e.bnei a0, 0x21, branch_over_relaxable
 # CHECK-NEXT: qc.e.bnei a0, 0x21, 0xa <branch_over_relaxable+0xa>
 # CHECK-NEXT: R_RISCV_VENDOR QUALCOMM
-# CHECK-NEXT: R_RISCV_CUSTOM193 branch_over_relaxable
+# CHECK-NEXT: R_RISCV_QC_E_BRANCH branch_over_relaxable
 # CHECK-NOT: R_RISCV_RELAX
   ret
 # CHECK-NEXT: c.jr ra
diff --git a/llvm/test/MC/RISCV/xqcibi-relocations.s b/llvm/test/MC/RISCV/xqcibi-relocations.s
index 931cd7c9314bb..408bb18ad62cb 100644
--- a/llvm/test/MC/RISCV/xqcibi-relocations.s
+++ b/llvm/test/MC/RISCV/xqcibi-relocations.s
@@ -26,7 +26,7 @@ qc.bnei t1, 10, undef
 # ASM: qc.e.bgeui s0, 20, undef
 # OBJ-NEXT: qc.e.bgeui s0, 0x14, 0x4 <this_section+0x4>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM193 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_BRANCH undef{{$}}
 qc.e.bgeui s0, 20, undef
 
 
@@ -47,7 +47,7 @@ qc.bnei t2, 12, same_section_extern
 # ASM: qc.e.bgeui s1, 22, same_section_extern
 # OBJ-NEXT: qc.e.bgeui s1, 0x16, 0x18 <this_section+0x18>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM193 same_section_extern{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_BRANCH same_section_extern{{$}}
 qc.e.bgeui s1, 22, same_section_extern
 
 
@@ -59,7 +59,7 @@ qc.bnei t3, 13, other_section
 # ASM: qc.e.bgeui s2, 23, other_section
 # OBJ-NEXT: qc.e.bgeui s2, 0x17, 0x22 <this_section+0x22>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM193 other_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_BRANCH other_section{{$}}
 qc.e.bgeui s2, 23, other_section
 
 
diff --git a/llvm/test/MC/RISCV/xqcilb-relocations.s b/llvm/test/MC/RISCV/xqcilb-relocations.s
index 48c8c6931c8af..5d58696018223 100644
--- a/llvm/test/MC/RISCV/xqcilb-relocations.s
+++ b/llvm/test/MC/RISCV/xqcilb-relocations.s
@@ -21,13 +21,13 @@ this_section:
 # ASM: qc.e.j undef
 # OBJ: qc.e.j 0x0 <this_section>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT undef{{$}}
 qc.e.j undef
 
 # ASM: qc.e.jal undef
 # OBJ-NEXT: qc.e.jal 0x6 <this_section+0x6>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT undef{{$}}
 qc.e.jal undef
 
 
@@ -42,26 +42,26 @@ qc.e.jal same_section
 # ASM: qc.e.j same_section_extern
 # OBJ-NEXT: qc.e.j 0x18 <this_section+0x18>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 same_section_extern{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT same_section_extern{{$}}
 qc.e.j same_section_extern
 
 # ASM: qc.e.jal same_section_extern
 # OBJ-NEXT: qc.e.jal 0x1e <this_section+0x1e>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 same_section_extern{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT same_section_extern{{$}}
 qc.e.jal same_section_extern
 
 
 # ASM: qc.e.j other_section
 # OBJ-NEXT: qc.e.j 0x24 <this_section+0x24>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 other_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT other_section{{$}}
 qc.e.j other_section
 
 # ASM: qc.e.jal other_section
 # OBJ-NEXT: qc.e.jal 0x2a <this_section+0x2a>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 other_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT other_section{{$}}
 qc.e.jal other_section
 
 
@@ -81,14 +81,14 @@ same_section_extern:
 # ASM: qc.e.j same_section
 # OBJ: qc.e.j 0x38 <same_section_extern+0x4>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 same_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT same_section{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 qc.e.j same_section
 
 # ASM: qc.e.jal same_section
 # OBJ-NEXT: qc.e.jal 0x3e <same_section_extern+0xa>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 same_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT same_section{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 qc.e.jal same_section
 
@@ -99,14 +99,14 @@ qc.e.j undef
 # ASM: j undef
 # OBJ: qc.e.j 0x44 <same_section_extern+0x10>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT undef{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 
 qc.e.jal undef
 # ASM: jal undef
 # OBJ: qc.e.jal 0x4a <same_section_extern+0x16>
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM195 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_CALL_PLT undef{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 
 .section .text.other, "ax", @progbits
diff --git a/llvm/test/MC/RISCV/xqcili-linker-relaxation.s b/llvm/test/MC/RISCV/xqcili-linker-relaxation.s
index ace677979ee13..bb32414d06b80 100644
--- a/llvm/test/MC/RISCV/xqcili-linker-relaxation.s
+++ b/llvm/test/MC/RISCV/xqcili-linker-relaxation.s
@@ -14,7 +14,7 @@
   qc.e.li a0, sym
 # CHECK-NEXT: qc.e.li a0, 0x0
 # CHECK-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# CHECK-NEXT: R_RISCV_CUSTOM194 sym{{$}}
+# CHECK-NEXT: R_RISCV_QC_E_32 sym{{$}}
 # CHECK-NEXT: R_RISCV_RELAX *ABS*{{$}}
 .L1:
 # CHECK: <.L1>:
@@ -29,7 +29,7 @@
   qc.li a0,  %qc.abs20(sym)
 # CHECK-NEXT: qc.li a0, 0x0
 # CHECK-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# CHECK-NEXT: R_RISCV_CUSTOM192 sym{{$}}
+# CHECK-NEXT: R_RISCV_QC_ABS20_U sym{{$}}
 # CHECK-NEXT: R_RISCV_RELAX *ABS*{{$}}
 .L2:
 # CHECK: <.L2>:
diff --git a/llvm/test/MC/RISCV/xqcili-relocations.s b/llvm/test/MC/RISCV/xqcili-relocations.s
index 7eff61fc782d8..b09a7d0481da6 100644
--- a/llvm/test/MC/RISCV/xqcili-relocations.s
+++ b/llvm/test/MC/RISCV/xqcili-relocations.s
@@ -22,13 +22,13 @@ this_section:
 # ASM: qc.li a0, %qc.abs20(undef)
 # OBJ: qc.li a0, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM192 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_ABS20_U undef{{$}}
 qc.li a0, %qc.abs20(undef)
 
 # ASM: qc.e.li s0, undef
 # OBJ-NEXT: qc.e.li s0, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM194 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_32 undef{{$}}
 qc.e.li s0, undef
 
 
@@ -44,25 +44,25 @@ qc.e.li s1, abs_symbol
 # ASM: qc.li a2, %qc.abs20(same_section)
 # OBJ-NEXT: qc.li a2, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM192 same_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_ABS20_U same_section{{$}}
 qc.li a2, %qc.abs20(same_section)
 
 # ASM: qc.e.li s2, same_section
 # OBJ-NEXT: qc.e.li s2, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM194 same_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_32 same_section{{$}}
 qc.e.li s2, same_section
 
 # ASM: qc.li a3, %qc.abs20(other_section)
 # OBJ-NEXT: qc.li a3, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM192 other_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_ABS20_U other_section{{$}}
 qc.li a3, %qc.abs20(other_section)
 
 # ASM: qc.e.li s3, other_section
 # OBJ-NEXT: qc.e.li s3, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM194 other_section{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_32 other_section{{$}}
 qc.e.li s3, other_section
 
 # ASM-LABEL: same_section:
@@ -75,7 +75,7 @@ same_section:
 # ASM: qc.li a1, %qc.abs20(0)
 # OBJ: qc.li a1, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM192 *ABS*{{$}}
+# OBJ-NEXT: R_RISCV_QC_ABS20_U *ABS*{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 qc.li a1, %qc.abs20(abs_symbol)
 
@@ -86,14 +86,14 @@ qc.e.li s1, abs_symbol
 # ASM: qc.li a1, %qc.abs20(undef)
 # OBJ-NEXT: qc.li a1, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM192 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_ABS20_U undef{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 qc.li a1, %qc.abs20(undef)
 
 # ASM: qc.e.li s1, undef
 # OBJ-NEXT: qc.e.li s1, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM194 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_32 undef{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 qc.e.li s1, undef
 
@@ -103,14 +103,14 @@ qc.e.li s1, undef
 # ASM: qc.li a1, %qc.abs20(undef)
 # OBJ-NEXT: qc.li a1, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM192 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_ABS20_U undef{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 qc.li a1, %qc.abs20(undef)
 
 # ASM: qc.e.li a2, undef
 # OBJ-NEXT: qc.e.li a2, 0x0
 # OBJ-NEXT: R_RISCV_VENDOR QUALCOMM{{$}}
-# OBJ-NEXT: R_RISCV_CUSTOM194 undef{{$}}
+# OBJ-NEXT: R_RISCV_QC_E_32 undef{{$}}
 # OBJ-NEXT: R_RISCV_RELAX
 qc.e.li a2, undef
 
diff --git a/llvm/test/tools/llvm-objdump/ELF/RISCV/riscv-vendor-reloc.s b/llvm/test/tools/llvm-objdump/ELF/RISCV/riscv-vendor-reloc.s
new file mode 100644
index 0000000000000..f8e58ccd69bef
--- /dev/null
+++ b/llvm/test/tools/llvm-objdump/ELF/RISCV/riscv-vendor-reloc.s
@@ -0,0 +1,64 @@
+# RUN: llvm-mc -triple riscv32 %s -filetype=obj -o %t32.o
+# RUN: llvm-mc -triple riscv64 %s -filetype=obj -o %t64.o
+
+## Test llvm-objdump -r (relocation dump mode)
+# RUN: llvm-objdump -r %t32.o | FileCheck %s --check-prefix=RELOC
+# RUN: llvm-objdump -r %t64.o | FileCheck %s --check-prefix=RELOC
+
+## Test llvm-objdump -dr (disassembly with relocations)
+# RUN: llvm-objdump -dr %t32.o | FileCheck %s --check-prefix=DISASM
+# RUN: llvm-objdump -dr %t64.o | FileCheck %s --check-prefix=DISASM
+
+## Test that llvm-objdump correctly resolves RISCV vendor-specific relocation
+## names when preceded by R_RISCV_VENDOR, and falls back to R_RISCV_CUSTOM*
+## when there is no preceding vendor relocation or the vendor is unknown.
+
+.text
+  nop
+
+## Test 1: Known vendor (QUALCOMM) - should resolve to vendor-specific name
+  .reloc ., R_RISCV_VENDOR, QUALCOMM
+  .reloc ., R_RISCV_CUSTOM192, foo
+  nop
+
+## Test 2: Vendor symbol is consumed after one use per RISC-V psABI.
+## The second R_RISCV_CUSTOM192 without a preceding R_RISCV_VENDOR should
+## remain as R_RISCV_CUSTOM192.
+  .reloc ., R_RISCV_CUSTOM192, bar
+  nop
+
+## Test 3: Known vendor (ANDES) - should resolve to vendor-specific name
+  .reloc ., R_RISCV_VENDOR, ANDES
+  .reloc ., R_RISCV_CUSTOM241, baz
+  nop
+
+## Test 4: Unknown vendor - should fall back to R_RISCV_CUSTOM*
+  .reloc ., R_RISCV_VENDOR, UNKNOWN_VENDOR
+  .reloc ., R_RISCV_CUSTOM200, qux
+  nop
+
+## Test 5: Another known vendor after unknown - should work correctly
+  .reloc ., R_RISCV_VENDOR, QUALCOMM
+  .reloc ., R_RISCV_CUSTOM193, quux
+  nop
+
+# RELOC:      RELOCATION RECORDS FOR [.text]:
+# RELOC:      R_RISCV_VENDOR       QUALCOMM
+# RELOC-NEXT: R_RISCV_QC_ABS20_U   foo
+# RELOC-NEXT: R_RISCV_CUSTOM192    bar
+# RELOC-NEXT: R_RISCV_VENDOR       ANDES
+# RELOC-NEXT: R_RISCV_NDS_BRANCH_10 baz
+# RELOC-NEXT: R_RISCV_VENDOR       UNKNOWN_VENDOR
+# RELOC-NEXT: R_RISCV_CUSTOM200    qux
+# RELOC-NEXT: R_RISCV_VENDOR       QUALCOMM
+# RELOC-NEXT: R_RISCV_QC_E_BRANCH  quux
+
+# DISASM:      R_RISCV_VENDOR       QUALCOMM
+# DISASM-NEXT: R_RISCV_QC_ABS20_U   foo
+# DISASM:      R_RISCV_CUSTOM192    bar
+# DISASM:      R_RISCV_VENDOR       ANDES
+# DISASM-NEXT: R_RISCV_NDS_BRANCH_10 baz
+# DISASM:      R_RISCV_VENDOR       UNKNOWN_VENDOR
+# DISASM-NEXT: R_RISCV_CUSTOM200    qux
+# DISASM:      R_RISCV_VENDOR       QUALCOMM
+# DISASM-NEXT: R_RISCV_QC_E_BRANCH  quux
diff --git a/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv-vendor-reloc.s b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv-vendor-reloc.s
new file mode 100644
index 0000000000000..f920f9d003ab9
--- /dev/null
+++ b/llvm/test/tools/llvm-readobj/ELF/RISCV/riscv-vendor-reloc.s
@@ -0,0 +1,62 @@
+# RUN: llvm-mc -triple riscv32 %s -filetype=obj -o %t32.o
+# RUN: llvm-mc -triple riscv64 %s -filetype=obj -o %t64.o
+# RUN: llvm-readelf -r %t32.o | FileCheck %s --check-prefix=GNU
+# RUN: llvm-readelf -r %t64.o | FileCheck %s --check-prefix=GNU
+# RUN: llvm-readobj -r %t32.o | FileCheck %s --check-prefix=LLVM
+# RUN: llvm-readobj -r %t64.o | FileCheck %s --check-prefix=LLVM
+
+## Test that llvm-readelf/llvm-readobj correctly resolves RISCV vendor-specific
+## relocation names when preceded by R_RISCV_VENDOR, and falls back to
+## R_RISCV_CUSTOM* when there is no preceding vendor relocation or the vendor
+## is unknown.
+
+.text
+  nop
+
+## Test 1: Known vendor (QUALCOMM) - should resolve to vendor-specific name
+  .reloc ., R_RISCV_VENDOR, QUALCOMM
+  .reloc ., R_RISCV_CUSTOM192, foo
+  nop
+
+## Test 2: Vendor symbol is consumed after one use per RISC-V psABI.
+## The second R_RISCV_CUSTOM192 without a preceding R_RISCV_VENDOR should
+## remain as R_RISCV_CUSTOM192.
+  .reloc ., R_RISCV_CUSTOM192, bar
+  nop
+
+## Test 3: Known vendor (ANDES) - should resolve to vendor-specific name
+  .reloc ., R_RISCV_VENDOR, ANDES
+  .reloc ., R_RISCV_CUSTOM241, baz
+  nop
+
+## Test 4: Unknown vendor - should fall back to R_RISCV_CUSTOM*
+  .reloc ., R_RISCV_VENDOR, UNKNOWN_VENDOR
+  .reloc ., R_RISCV_CUSTOM200, qux
+  nop
+
+## Test 5: Another known vendor after unknown - should work correctly
+  .reloc ., R_RISCV_VENDOR, QUALCOMM
+  .reloc ., R_RISCV_CUSTOM193, quux
+  nop
+
+# GNU:      Relocation section '.rela.text'
+# GNU:      R_RISCV_VENDOR       {{.*}} QUALCOMM + 0
+# GNU-NEXT: R_RISCV_QC_ABS20_U   {{.*}} foo + 0
+# GNU-NEXT: R_RISCV_CUSTOM192    {{.*}} bar + 0
+# GNU-NEXT: R_RISCV_VENDOR       {{.*}} ANDES + 0
+# GNU-NEXT: R_RISCV_NDS_BRANCH_10 {{.*}} baz + 0
+# GNU-NEXT: R_RISCV_VENDOR       {{.*}} UNKNOWN_VENDOR + 0
+# GNU-NEXT: R_RISCV_CUSTOM200    {{.*}} qux + 0
+# GNU-NEXT: R_RISCV_VENDOR       {{.*}} QUALCOMM + 0
+# GNU-NEXT: R_RISCV_QC_E_BRANCH  {{.*}} quux + 0
+
+# LLVM:      Relocations [
+# LLVM:        R_RISCV_VENDOR QUALCOMM 0x0
+# LLVM-NEXT:   R_RISCV_QC_ABS20_U foo 0x0
+# LLVM-NEXT:   R_RISCV_CUSTOM192 bar 0x0
+# LLVM-NEXT:   R_RISCV_VENDOR ANDES 0x0
+# LLVM-NEXT:   R_RISCV_NDS_BRANCH_10 baz 0x0
+# LLVM-NEXT:   R_RISCV_VENDOR UNKNOWN_VENDOR 0x0
+# LLVM-NEXT:   R_RISCV_CUSTOM200 qux 0x0
+# LLVM-NEXT:   R_RISCV_VENDOR QUALCOMM 0x0
+# LLVM-NEXT:   R_RISCV_QC_E_BRANCH quux 0x0
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 38c3f31441c06..53c842de8625b 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -646,13 +646,43 @@ static bool hasMappingSymbols(const ObjectFile &Obj) {
          isRISCVElf(Obj);
 }
 
+/// Get relocation type name, resolving RISCV vendor-specific relocations
+/// when preceded by R_RISCV_VENDOR.
+static StringRef getRelocTypeName(const RelocationRef &Rel,
+                                  SmallVectorImpl<char> &RelocName,
+                                  std::string &CurrentRISCVVendorSymbol) {
+  Rel.getTypeName(RelocName);
+  const ObjectFile *Obj = Rel.getObject();
+  if (!isRISCVElf(*Obj))
+    return StringRef(RelocName.data(), RelocName.size());
+
+  uint64_t Type = Rel.getType();
+  if (Type == ELF::R_RISCV_VENDOR) {
+    symbol_iterator SI = Rel.getSymbol();
+    if (SI != Obj->symbol_end()) {
+      if (Expected<StringRef> SymName = SI->getName())
+        CurrentRISCVVendorSymbol = SymName->str();
+    }
+  } else if (Type >= ELF::R_RISCV_CUSTOM192 &&
+             Type <= ELF::R_RISCV_CUSTOM255 &&
+             !CurrentRISCVVendorSymbol.empty()) {
+    StringRef VendorRelocNa...
[truncated]

@github-actions
Copy link

github-actions bot commented Dec 18, 2025

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff origin/main HEAD --extensions cpp -- llvm/tools/llvm-objdump/llvm-objdump.cpp llvm/tools/llvm-readobj/ELFDumper.cpp --diff_from_common_commit

⚠️
The reproduction instructions above might return results for more than one PR
in a stack if you are using a stacked PR workflow. You can limit the results by
changing origin/main to the base branch/commit you want to compare against.
⚠️

View the diff from clang-format here.
diff --git a/llvm/tools/llvm-objdump/llvm-objdump.cpp b/llvm/tools/llvm-objdump/llvm-objdump.cpp
index 29173b765..0b994a662 100644
--- a/llvm/tools/llvm-objdump/llvm-objdump.cpp
+++ b/llvm/tools/llvm-objdump/llvm-objdump.cpp
@@ -671,8 +671,8 @@ static StringRef getRelocTypeName(const RelocationRef &Rel,
     // Per RISC-V psABI, R_RISCV_VENDOR must be placed immediately before the
     // vendor-specific relocation at the same offset. Clear the vendor symbol
     // if this relocation doesn't form a valid pair.
-    if (Offset != CurrentRISCVVendorOffset ||
-        Type < ELF::R_RISCV_CUSTOM192 || Type > ELF::R_RISCV_CUSTOM255) {
+    if (Offset != CurrentRISCVVendorOffset || Type < ELF::R_RISCV_CUSTOM192 ||
+        Type > ELF::R_RISCV_CUSTOM255) {
       CurrentRISCVVendorSymbol.clear();
     } else {
       // Valid vendor relocation pair - use vendor-specific name.
@@ -2839,9 +2839,9 @@ void Dumper::printRelocations() {
         SmallString<32> ValueStr;
         if (Address < StartAddress || Address > StopAddress || getHidden(Reloc))
           continue;
-        StringRef Name = getRelocTypeName(Reloc, RelocName,
-                                          CurrentRISCVVendorSymbol,
-                                          CurrentRISCVVendorOffset);
+        StringRef Name =
+            getRelocTypeName(Reloc, RelocName, CurrentRISCVVendorSymbol,
+                             CurrentRISCVVendorOffset);
         if (Error E =
                 getRelocationValueString(Reloc, SymbolDescription, ValueStr))
           reportUniqueWarning(std::move(E));

…iate adjacency

Per RISC-V psABI, R_RISCV_VENDOR must be placed immediately before the
vendor-specific relocation at the same offset. This fixes two edge cases:
- Different offsets between R_RISCV_VENDOR and R_RISCV_CUSTOM*
- Non-CUSTOM relocation in between breaks the pairing
@kito-cheng
Copy link
Member Author

ChangeLog:

  • Handle following two case correctly:
    • Different offsets between R_RISCV_VENDOR and R_RISCV_CUSTOM*
    • Non-CUSTOM relocation in between breaks the pairing

@MaskRay MaskRay changed the title [RISCV][llvm-readobj,llvm-objdump] Use getRISCVVendorRelocationTypeNa… [RISCV][llvm-readobj,llvm-objdump] Use getRISCVVendorRelocationTypeName for RISCV vendor relocations Dec 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants