[ELF][PPC32] Make R_PPC32_PLTREL retain .got

PLT usage needs the first 12 bytes of the .got section. We need to keep .got and
DT_GOT_PPC even if .got/_GLOBAL_OFFSET_TABLE_ are not referenced (large PIC code
may only reference .got2), which is the case in OpenBSD's ld.so, leading
to a misleading error, "unsupported insecure BSS PLT object".

Fix this by adding R_PPC32_PLTREL to the list of hasGotOffRel.

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D114982

GitOrigin-RevId: 885fb9a257faa14ec33ad972df81801483c85da2
diff --git a/ELF/Relocations.cpp b/ELF/Relocations.cpp
index 477f4f4..7a6fda1 100644
--- a/ELF/Relocations.cpp
+++ b/ELF/Relocations.cpp
@@ -1371,8 +1371,8 @@
   if (oneof<R_GOTPLTONLY_PC, R_GOTPLTREL, R_GOTPLT, R_PLT_GOTPLT,
             R_TLSDESC_GOTPLT, R_TLSGD_GOTPLT>(expr)) {
     in.gotPlt->hasGotPltOffRel = true;
-  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC64_TOCBASE, R_PPC64_RELAX_TOC>(
-                 expr)) {
+  } else if (oneof<R_GOTONLY_PC, R_GOTREL, R_PPC32_PLTREL, R_PPC64_TOCBASE,
+                   R_PPC64_RELAX_TOC>(expr)) {
     in.got->hasGotOffRel = true;
   }
 
diff --git a/test/ELF/ppc32-ifunc-nonpreemptible-pic.s b/test/ELF/ppc32-ifunc-nonpreemptible-pic.s
index a88927f..a4b7c8b 100644
--- a/test/ELF/ppc32-ifunc-nonpreemptible-pic.s
+++ b/test/ELF/ppc32-ifunc-nonpreemptible-pic.s
@@ -7,12 +7,12 @@
 # RUN: llvm-objdump -d --no-show-raw-insn %t | FileCheck %s
 
 # RELOC:      .rela.dyn {
-# RELOC-NEXT:   0x30248 R_PPC_RELATIVE - 0x101A8
-# RELOC-NEXT:   0x3024C R_PPC_IRELATIVE - 0x10188
+# RELOC-NEXT:   0x30254 R_PPC_RELATIVE - 0x101A8
+# RELOC-NEXT:   0x30258 R_PPC_IRELATIVE - 0x10188
 # RELOC-NEXT: }
 
 # SYM: 000101a8 0 FUNC GLOBAL DEFAULT {{.*}} func
-# HEX: 0x00030248 00000000
+# HEX: 0x00030254 00000000
 
 .section .got2,"aw"
 .long func
diff --git a/test/ELF/ppc32-reloc-pltrel.s b/test/ELF/ppc32-reloc-pltrel.s
new file mode 100644
index 0000000..1741848
--- /dev/null
+++ b/test/ELF/ppc32-reloc-pltrel.s
@@ -0,0 +1,35 @@
+# REQUIRES: ppc
+
+## Ensure R_PPC_PLTREL retains .got even in the absence of
+## .got/_GLOBAL_OFFSET_TABLE_ references.
+
+# RUN: llvm-mc -filetype=obj -triple=powerpc %s -o %t.o
+# RUN: ld.lld -shared %t.o -o %t.so
+# RUN: llvm-readobj -Sdr %t.so | FileCheck %s
+
+.section .got2,"aw",@progbits
+.set .LTOC, .+0x8000
+
+.text
+.L0:
+addis 30,30,.LTOC-.L0@ha
+addi 30,30,.LTOC-.L0@l
+bl baz+0x8000@plt
+
+## DT_PPC_GOT must point to .got, which must have the 12-byte header.
+## The only relocation is an R_PPC_JMP_SLOT.
+
+# CHECK:      Sections [
+# CHECK:        Name: .got (
+# CHECK:        Address:
+# CHECK-SAME:   {{ }}[[#%x,GOT:]]
+# CHECK:        Size:
+# CHECK-SAME:   {{ 12$}}
+# CHECK:      DynamicSection [
+# CHECK-NEXT:   Tag        Type     Name/Value
+# CHECK:        0x70000000 PPC_GOT  [[#GOT]]
+# CHECK:      Relocations [
+# CHECK-NEXT:   Section ([[#]]) .rela.plt {
+# CHECK-NEXT:     0x[[#%x,]] R_PPC_JMP_SLOT baz 0x0
+# CHECK-NEXT:   }
+# CHECK-NEXT: ]
diff --git a/test/ELF/ppc32-reloc-rel.s b/test/ELF/ppc32-reloc-rel.s
index 29501fd..b89e0b4 100644
--- a/test/ELF/ppc32-reloc-rel.s
+++ b/test/ELF/ppc32-reloc-rel.s
@@ -11,13 +11,13 @@
   beq 1f
 1:
 # CHECK-LABEL: section .R_PPC_REL14:
-# CHECK: 100100b4: bt 2, 0x100100b8
+# CHECK: 100100f4: bt 2, 0x100100f8
 
 .section .R_PPC_REL24,"ax",@progbits
   b 1f
 1:
 # CHECK-LABEL: section .R_PPC_REL24:
-# CHECK: b 0x100100bc
+# CHECK: b 0x100100fc
 
 .section .R_PPC_REL32,"ax",@progbits
   .long 1f - .
@@ -29,10 +29,10 @@
   b 1f@PLT+32768
 1:
 # CHECK-LABEL: section .R_PPC_PLTREL24:
-# CHECK: b 0x100100c4
+# CHECK: b 0x10010104
 
 .section .R_PPC_LOCAL24PC,"ax",@progbits
   b 1f@local
 1:
 # CHECK-LABEL: section .R_PPC_LOCAL24PC:
-# CHECK: b 0x100100c8
+# CHECK: b 0x10010108