[llvm][lld] Support R_RISCV_GOT32_PCREL (#72587)

This is the followup implementation to
https://github.com/riscv-non-isa/riscv-elf-psabi-doc/pull/402 that
supports this relocation in llvm and lld.

GitOrigin-RevId: f7678c81fe96dc8a350d947b77ce5311a9f99612
diff --git a/ELF/Arch/RISCV.cpp b/ELF/Arch/RISCV.cpp
index 1d3d179..62498de 100644
--- a/ELF/Arch/RISCV.cpp
+++ b/ELF/Arch/RISCV.cpp
@@ -290,6 +290,7 @@
   case R_RISCV_PLT32:
     return R_PLT_PC;
   case R_RISCV_GOT_HI20:
+  case R_RISCV_GOT32_PCREL:
     return R_GOT_PC;
   case R_RISCV_PCREL_LO12_I:
   case R_RISCV_PCREL_LO12_S:
@@ -499,6 +500,8 @@
   case R_RISCV_SET32:
   case R_RISCV_32_PCREL:
   case R_RISCV_PLT32:
+  case R_RISCV_GOT32_PCREL:
+    checkInt(loc, val, 32, rel);
     write32le(loc, val);
     return;
 
diff --git a/test/ELF/riscv64-reloc-got32-pcrel.s b/test/ELF/riscv64-reloc-got32-pcrel.s
new file mode 100644
index 0000000..24bd828
--- /dev/null
+++ b/test/ELF/riscv64-reloc-got32-pcrel.s
@@ -0,0 +1,27 @@
+// REQUIRES: riscv
+// RUN: llvm-mc -filetype=obj -triple=riscv64 %s -o %t.o
+// RUN: ld.lld %t.o -o %t.so -shared --noinhibit-exec 2>&1 | FileCheck %s --check-prefix=WARN
+// RUN: llvm-readelf -S %t.so | FileCheck --check-prefix=SEC %s
+// RUN: llvm-objdump --no-print-imm-hex -s -d %t.so | FileCheck %s
+
+// SEC: .got PROGBITS 0000000000002390
+
+  .section .data
+  .globl bar
+bar:
+
+  .globl _start
+_start:  // PC = 0x33a8
+// bar@GOTPCREL   = 0x2398 (got entry for `bar`) - 0x33a8 (.) = 0xf0efffff
+// bar@GOTPCREL+4 = 0x2398 (got entry for `bar`) - 0x33ac (.) + 4 = 0xf0efffff
+// bar@GOTPCREL-4 = 0x2398 (got entry for `bar`) - 0x33b0 (.) - 4 = 0xe4efffff
+// CHECK:      Contents of section .data:
+// CHECK-NEXT:  {{.*}} f0efffff f0efffff e4efffff
+  .word bar@GOTPCREL
+  .word bar@GOTPCREL+4
+  .word bar@GOTPCREL-4
+
+// WARN: relocation R_RISCV_GOT32_PCREL out of range: {{.*}} is not in [-2147483648, 2147483647]; references 'baz'
+// WARN: relocation R_RISCV_GOT32_PCREL out of range: {{.*}} is not in [-2147483648, 2147483647]; references 'baz'
+  .word baz@GOTPCREL+0xffffffff
+  .word baz@GOTPCREL-0xffffffff