[lld] [lld][ELF] Allow Arm PC-relative relocations in PIC links (PR #77304)

Eleanor Bonnici via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 11 03:10:54 PST 2024


https://github.com/eleanor-arm updated https://github.com/llvm/llvm-project/pull/77304

>From db1a8c1f2d66ae07cb7915157585719b56f09daf Mon Sep 17 00:00:00 2001
From: Eleanor Bonnici <eleanor.bonnici at arm.com>
Date: Mon, 8 Jan 2024 13:50:27 +0000
Subject: [PATCH] [lld][ELF] Allow Arm PC-relative relocations in PIC links

The relocations that map to R_ARM_PCA are equivalent to R_PC. They are
PC-relative and safe to use in shared libraries, but have a different
relocation code as they are evaluated differently. Now that LLVM may
generate these relocations in object files, they may occur in
shared libraries or position-independent executables.
---
 lld/ELF/Relocations.cpp | 8 ++++----
 lld/test/ELF/arm-adr.s  | 2 ++
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 210b4d1eb1a7a6..cb8f2f125eebb8 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -210,10 +210,10 @@ bool lld::elf::needsGot(RelExpr expr) {
 // True if this expression is of the form Sym - X, where X is a position in the
 // file (PC, or GOT for example).
 static bool isRelExpr(RelExpr expr) {
-  return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_MIPS_GOTREL, R_PPC64_CALL,
-               R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC, R_RELAX_GOT_PC,
-               R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC, R_LOONGARCH_PAGE_PC>(
-      expr);
+  return oneof<R_PC, R_GOTREL, R_GOTPLTREL, R_ARM_PCA, R_MIPS_GOTREL,
+               R_PPC64_CALL, R_PPC64_RELAX_TOC, R_AARCH64_PAGE_PC,
+               R_RELAX_GOT_PC, R_RISCV_PC_INDIRECT, R_PPC64_RELAX_GOT_PC,
+               R_LOONGARCH_PAGE_PC>(expr);
 }
 
 static RelExpr toPlt(RelExpr expr) {
diff --git a/lld/test/ELF/arm-adr.s b/lld/test/ELF/arm-adr.s
index 05e8cc2f92db3e..fee78f97a12e85 100644
--- a/lld/test/ELF/arm-adr.s
+++ b/lld/test/ELF/arm-adr.s
@@ -1,6 +1,8 @@
 // REQUIRES: arm
 // RUN: llvm-mc --triple=armv7a-none-eabi --arm-add-build-attributes -filetype=obj -o %t.o %s
 // RUN: ld.lld %t.o -o %t
+/// R_ARM_ALU_PC_G0 referencing a non-preemptible symbol can be used in PIC links.
+// RUN: ld.lld %t.o --shared -o /dev/null
 // RUN: llvm-objdump -d --no-show-raw-insn --triple=armv7a-none-eabi %t | FileCheck %s
 
 /// Test the short range cases of R_ARM_ALU_PC_G0. The range of the instruction



More information about the llvm-commits mailing list