[lld] [ELF] --pack-dyn-relocs=android+relr: place IRELATIVE in .rela.plt (PR #86751)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 26 18:57:06 PDT 2024


https://github.com/MaskRay updated https://github.com/llvm/llvm-project/pull/86751

>From 40f0b099b1ac002143f9f6a3313afcd8f049086a Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 26 Mar 2024 17:01:50 -0700
Subject: [PATCH 1/3] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20in?=
 =?UTF-8?q?itial=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.5-bogner
---
 lld/ELF/Relocations.cpp              | 10 +++++--
 lld/test/ELF/pack-dyn-relocs-ifunc.s | 43 ++++++++++++++++++++++++++++
 2 files changed, 51 insertions(+), 2 deletions(-)
 create mode 100644 lld/test/ELF/pack-dyn-relocs-ifunc.s

diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 33c50133bec495..2ffb40c5fa4398 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -1659,10 +1659,16 @@ static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) {
   // original section/value pairs. For non-GOT non-PLT relocation case below, we
   // may alter section/value, so create a copy of the symbol to make
   // section/value fixed.
+  //
+  // Prior to Android V, there was a bug that caused RELR relocations to be
+  // applied after packed relocations. This meant that resolvers referenced
+  // IRELATIVE relocations in the packed relocation section could not read
+  // globals with RELR relocations. Work around this by placing IRELATIVE in
+  // .rela.plt.
   auto *directSym = makeDefined(cast<Defined>(sym));
   directSym->allocateAux();
-  addPltEntry(*in.iplt, *in.igotPlt, *mainPart->relaDyn, target->iRelativeRel,
-              *directSym);
+  auto &dyn = config->androidPackDynRelocs ? *in.relaPlt : *mainPart->relaDyn;
+  addPltEntry(*in.iplt, *in.igotPlt, dyn, target->iRelativeRel, *directSym);
   sym.allocateAux();
   symAux.back().pltIdx = symAux[directSym->auxIdx].pltIdx;
 
diff --git a/lld/test/ELF/pack-dyn-relocs-ifunc.s b/lld/test/ELF/pack-dyn-relocs-ifunc.s
new file mode 100644
index 00000000000000..ada771aa08ace3
--- /dev/null
+++ b/lld/test/ELF/pack-dyn-relocs-ifunc.s
@@ -0,0 +1,43 @@
+# REQUIRES: aarch64
+## Prior to Android V, there was a bug that caused RELR relocations to be
+## applied after packed relocations. This meant that resolvers referenced
+## IRELATIVE relocations in the packed relocation section could not read
+## globals with RELR relocations. Work around this by placing IRELATIVE in
+## .rela.plt
+
+# RUN: rm -rf %t && split-file %s %t && cd %t
+# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android a.s -o a.o
+# RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android b.s -o b.o
+# RUN: ld.lld -shared b.o -o b.so
+# RUN: ld.lld --pack-dyn-relocs=android -z separate-loadable-segments a.o b.so -o a
+# RUN: llvm-readobj -r a | FileCheck %s
+# RUN: llvm-objdump -d a | FileCheck %s --check-prefix=ASM
+
+# CHECK:      .rela.plt {
+# CHECK-NEXT:   0x230020 R_AARCH64_JUMP_SLOT bar 0x0
+# CHECK-NEXT:   0x230028 R_AARCH64_IRELATIVE - {{.*}}
+# CHECK-NEXT: }
+
+# ASM:      <.iplt>:
+# ASM-NEXT: adrp    x16, 0x230000
+# ASM-NEXT: ldr     x17, [x16, #0x28]
+
+#--- a.s
+.text
+.type foo, %gnu_indirect_function
+.globl foo
+foo:
+  ret
+
+.globl _start
+_start:
+  bl foo
+  bl bar
+
+.data
+.quad .data
+
+#--- b.s
+.globl bar
+bar:
+  ret

>From 16e51612739c3bfdea962071d4b0eb2eeab3310b Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 26 Mar 2024 17:15:53 -0700
Subject: [PATCH 2/3] .

Created using spr 1.3.5-bogner
---
 lld/ELF/Relocations.cpp              |  2 +-
 lld/test/ELF/pack-dyn-relocs-ifunc.s | 18 ++++++++++++------
 2 files changed, 13 insertions(+), 7 deletions(-)

diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index 2ffb40c5fa4398..af46b1d326ac88 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -1664,7 +1664,7 @@ static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) {
   // applied after packed relocations. This meant that resolvers referenced
   // IRELATIVE relocations in the packed relocation section could not read
   // globals with RELR relocations. Work around this by placing IRELATIVE in
-  // .rela.plt.
+  // .rela.plt when --pack-relative-relocs=android+relr is enabled.
   auto *directSym = makeDefined(cast<Defined>(sym));
   directSym->allocateAux();
   auto &dyn = config->androidPackDynRelocs ? *in.relaPlt : *mainPart->relaDyn;
diff --git a/lld/test/ELF/pack-dyn-relocs-ifunc.s b/lld/test/ELF/pack-dyn-relocs-ifunc.s
index ada771aa08ace3..8f705d77b2b1af 100644
--- a/lld/test/ELF/pack-dyn-relocs-ifunc.s
+++ b/lld/test/ELF/pack-dyn-relocs-ifunc.s
@@ -3,24 +3,29 @@
 ## applied after packed relocations. This meant that resolvers referenced
 ## IRELATIVE relocations in the packed relocation section could not read
 ## globals with RELR relocations. Work around this by placing IRELATIVE in
-## .rela.plt
+## .rela.plt when --pack-relative-relocs=android+relr is enabled.
 
 # RUN: rm -rf %t && split-file %s %t && cd %t
 # RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android a.s -o a.o
 # RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android b.s -o b.o
 # RUN: ld.lld -shared b.o -o b.so
-# RUN: ld.lld --pack-dyn-relocs=android -z separate-loadable-segments a.o b.so -o a
+# RUN: ld.lld -pie --pack-dyn-relocs=android+relr -z separate-loadable-segments a.o b.so -o a
 # RUN: llvm-readobj -r a | FileCheck %s
 # RUN: llvm-objdump -d a | FileCheck %s --check-prefix=ASM
 
+# CHECK:      .relr.dyn {
+# CHECK-NEXT:   0x30000 R_AARCH64_RELATIVE -
+# CHECK-NEXT: }
 # CHECK:      .rela.plt {
-# CHECK-NEXT:   0x230020 R_AARCH64_JUMP_SLOT bar 0x0
-# CHECK-NEXT:   0x230028 R_AARCH64_IRELATIVE - {{.*}}
+# CHECK-NEXT:   0x30020 R_AARCH64_JUMP_SLOT bar 0x0
+# CHECK-NEXT:   0x30028 R_AARCH64_IRELATIVE - 0x10000
 # CHECK-NEXT: }
 
 # ASM:      <.iplt>:
-# ASM-NEXT: adrp    x16, 0x230000
-# ASM-NEXT: ldr     x17, [x16, #0x28]
+# ASM-NEXT:   adrp    x16, 0x30000
+# ASM-NEXT:   ldr     x17, [x16, #0x28]
+# ASM-NEXT:   add     x16, x16, #0x28
+# ASM-NEXT:   br      x17
 
 #--- a.s
 .text
@@ -35,6 +40,7 @@ _start:
   bl bar
 
 .data
+.balign 8
 .quad .data
 
 #--- b.s

>From 51e41399fc0c8f40fc703e8e1abb8c64e5f9a83a Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Tue, 26 Mar 2024 17:24:16 -0700
Subject: [PATCH 3/3] improve comment.

Created using spr 1.3.5-bogner
---
 lld/ELF/Relocations.cpp              | 7 ++++---
 lld/test/ELF/pack-dyn-relocs-ifunc.s | 6 +++---
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/lld/ELF/Relocations.cpp b/lld/ELF/Relocations.cpp
index af46b1d326ac88..804f7e31a42190 100644
--- a/lld/ELF/Relocations.cpp
+++ b/lld/ELF/Relocations.cpp
@@ -1662,9 +1662,10 @@ static bool handleNonPreemptibleIfunc(Symbol &sym, uint16_t flags) {
   //
   // Prior to Android V, there was a bug that caused RELR relocations to be
   // applied after packed relocations. This meant that resolvers referenced
-  // IRELATIVE relocations in the packed relocation section could not read
-  // globals with RELR relocations. Work around this by placing IRELATIVE in
-  // .rela.plt when --pack-relative-relocs=android+relr is enabled.
+  // IRELATIVE relocations in the packed relocation section would read
+  // unrelocated globals with RELR relocations when
+  // --pack-relative-relocs=android+relr is enabled. Work around this by placing
+  // IRELATIVE in .rela.plt.
   auto *directSym = makeDefined(cast<Defined>(sym));
   directSym->allocateAux();
   auto &dyn = config->androidPackDynRelocs ? *in.relaPlt : *mainPart->relaDyn;
diff --git a/lld/test/ELF/pack-dyn-relocs-ifunc.s b/lld/test/ELF/pack-dyn-relocs-ifunc.s
index 8f705d77b2b1af..b4e8f4faaa606c 100644
--- a/lld/test/ELF/pack-dyn-relocs-ifunc.s
+++ b/lld/test/ELF/pack-dyn-relocs-ifunc.s
@@ -1,9 +1,9 @@
 # REQUIRES: aarch64
 ## Prior to Android V, there was a bug that caused RELR relocations to be
 ## applied after packed relocations. This meant that resolvers referenced
-## IRELATIVE relocations in the packed relocation section could not read
-## globals with RELR relocations. Work around this by placing IRELATIVE in
-## .rela.plt when --pack-relative-relocs=android+relr is enabled.
+## IRELATIVE relocations in the packed relocation section would read
+## unrelocated globals when --pack-relative-relocs=android+relr is enabled.
+## Work around this by placing IRELATIVE in .rela.plt.
 
 # RUN: rm -rf %t && split-file %s %t && cd %t
 # RUN: llvm-mc -filetype=obj -triple=aarch64-linux-android a.s -o a.o



More information about the llvm-commits mailing list