[lld] [LLD][ELF][AArch64] Mark .plt and .iplt with PURECODE flag (PR #134798)
Csanád Hajdú via llvm-commits
llvm-commits at lists.llvm.org
Thu Apr 10 06:08:45 PDT 2025
https://github.com/Il-Capitano updated https://github.com/llvm/llvm-project/pull/134798
>From f8e78ec37398fe5c7755176d0cbfa64428124fe7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Csan=C3=A1d=20Hajd=C3=BA?= <csanad.hajdu at arm.com>
Date: Mon, 24 Mar 2025 16:46:01 +0100
Subject: [PATCH 1/5] [LLD][ELF][AArch64] Mark .plt and .iplt with PURECODE
flag
Mark the synthetic sections `.plt` and `.iplt` with the
`SHF_AARCH64_PURECODE` section flag, allowing them to be placed in an
executable-only segment.
---
lld/ELF/SyntheticSections.cpp | 10 ++++++++++
lld/test/ELF/aarch64-gnu-ifunc-plt.s | 20 ++++++++++++++++++--
lld/test/ELF/aarch64-plt.s | 2 ++
3 files changed, 30 insertions(+), 2 deletions(-)
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 3bb9815336a7c..88e2a369000e1 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -2610,6 +2610,11 @@ PltSection::PltSection(Ctx &ctx)
// modify the instructions in the PLT entries.
if (ctx.arg.emachine == EM_SPARCV9)
this->flags |= SHF_WRITE;
+
+ // On AArch64, PLT entries only do loads from the .got.plt section, so the
+ // .plt section can be marked with the SHF_AARCH64_PURECODE section flag.
+ if (ctx.arg.emachine == EM_AARCH64)
+ this->flags |= SHF_AARCH64_PURECODE;
}
void PltSection::writeTo(uint8_t *buf) {
@@ -2658,6 +2663,11 @@ IpltSection::IpltSection(Ctx &ctx)
name = ".glink";
addralign = 4;
}
+
+ // On AArch64, PLT entries only do loads from the .got.plt section, so the
+ // .iplt section can be marked with the SHF_AARCH64_PURECODE section flag.
+ if (ctx.arg.emachine == EM_AARCH64)
+ this->flags |= SHF_AARCH64_PURECODE;
}
void IpltSection::writeTo(uint8_t *buf) {
diff --git a/lld/test/ELF/aarch64-gnu-ifunc-plt.s b/lld/test/ELF/aarch64-gnu-ifunc-plt.s
index bde6f1420540f..dfecb5c9e6ea1 100644
--- a/lld/test/ELF/aarch64-gnu-ifunc-plt.s
+++ b/lld/test/ELF/aarch64-gnu-ifunc-plt.s
@@ -5,7 +5,7 @@
// RUN: ld.lld --hash-style=sysv %t.so %t.o -o %tout
// RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn %tout | FileCheck %s --check-prefix=DISASM
// RUN: llvm-objdump -s %tout | FileCheck %s --check-prefix=GOTPLT
-// RUN: llvm-readobj --dynamic-table -r %tout | FileCheck %s
+// RUN: llvm-readobj --dynamic-table -r -S %tout | FileCheck %s
// RUN: llvm-mc -filetype=obj -triple=aarch64_be %S/Inputs/shared2.s -o %t1.be.o
// RUN: ld.lld %t1.be.o --shared --soname=t.so -o %t.be.so
@@ -13,7 +13,23 @@
// RUN: ld.lld --hash-style=sysv %t.be.so %t.be.o -o %t.be
// RUN: llvm-objdump --no-print-imm-hex -d --no-show-raw-insn %t.be | FileCheck %s --check-prefix=DISASM
// RUN: llvm-objdump -s %t.be | FileCheck %s --check-prefix=GOTPLT_BE
-// RUN: llvm-readobj --dynamic-table -r %t.be | FileCheck %s
+// RUN: llvm-readobj --dynamic-table -r -S %t.be | FileCheck %s
+
+// Check that the .iplt section has the SHF_AARCH64_PURECODE section flag set
+// CHECK: Sections [
+// CHECK: Name: .iplt
+// CHECK-NEXT: Type: SHT_PROGBITS
+// CHECK-NEXT: Flags [
+// CHECK-NEXT: SHF_AARCH64_PURECODE
+// CHECK-NEXT: SHF_ALLOC
+// CHECK-NEXT: SHF_EXECINSTR
+// CHECK-NEXT: ]
+// CHECK-NEXT: Address: 0x210330
+// CHECK-NEXT: Offset:
+// CHECK-NEXT: Size: 32
+// CHECK-NEXT: Link:
+// CHECK-NEXT: Info:
+// CHECK-NEXT: AddressAlignment: 16
// Check that the PLTRELSZ tag does not include the IRELATIVE relocations
// CHECK: DynamicSection [
diff --git a/lld/test/ELF/aarch64-plt.s b/lld/test/ELF/aarch64-plt.s
index 7d83a40dfc278..88e719484921b 100644
--- a/lld/test/ELF/aarch64-plt.s
+++ b/lld/test/ELF/aarch64-plt.s
@@ -14,6 +14,7 @@
// CHECKDSO: Name: .plt
// CHECKDSO-NEXT: Type: SHT_PROGBITS
// CHECKDSO-NEXT: Flags [
+// CHECKDSO-NEXT: SHF_AARCH64_PURECODE
// CHECKDSO-NEXT: SHF_ALLOC
// CHECKDSO-NEXT: SHF_EXECINSTR
// CHECKDSO-NEXT: ]
@@ -109,6 +110,7 @@
// CHECKEXE: Name: .plt
// CHECKEXE-NEXT: Type: SHT_PROGBITS
// CHECKEXE-NEXT: Flags [
+// CHECKEXE-NEXT: SHF_AARCH64_PURECODE
// CHECKEXE-NEXT: SHF_ALLOC
// CHECKEXE-NEXT: SHF_EXECINSTR
// CHECKEXE-NEXT: ]
>From 4fe2ab7b409ef998c3a500dba3b9e1f0b91a58ce Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Csan=C3=A1d=20Hajd=C3=BA?= <csanad.hajdu at arm.com>
Date: Tue, 8 Apr 2025 18:47:11 +0200
Subject: [PATCH 2/5] Move EM_AARCH64 check to be alphabetically ordered
---
lld/ELF/SyntheticSections.cpp | 20 ++++++++++----------
1 file changed, 10 insertions(+), 10 deletions(-)
diff --git a/lld/ELF/SyntheticSections.cpp b/lld/ELF/SyntheticSections.cpp
index 88e2a369000e1..196ecc4fee8c8 100644
--- a/lld/ELF/SyntheticSections.cpp
+++ b/lld/ELF/SyntheticSections.cpp
@@ -2594,6 +2594,11 @@ PltSection::PltSection(Ctx &ctx)
: SyntheticSection(ctx, ".plt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
16),
headerSize(ctx.target->pltHeaderSize) {
+ // On AArch64, PLT entries only do loads from the .got.plt section, so the
+ // .plt section can be marked with the SHF_AARCH64_PURECODE section flag.
+ if (ctx.arg.emachine == EM_AARCH64)
+ this->flags |= SHF_AARCH64_PURECODE;
+
// On PowerPC, this section contains lazy symbol resolvers.
if (ctx.arg.emachine == EM_PPC64) {
name = ".glink";
@@ -2610,11 +2615,6 @@ PltSection::PltSection(Ctx &ctx)
// modify the instructions in the PLT entries.
if (ctx.arg.emachine == EM_SPARCV9)
this->flags |= SHF_WRITE;
-
- // On AArch64, PLT entries only do loads from the .got.plt section, so the
- // .plt section can be marked with the SHF_AARCH64_PURECODE section flag.
- if (ctx.arg.emachine == EM_AARCH64)
- this->flags |= SHF_AARCH64_PURECODE;
}
void PltSection::writeTo(uint8_t *buf) {
@@ -2659,15 +2659,15 @@ void PltSection::addSymbols() {
IpltSection::IpltSection(Ctx &ctx)
: SyntheticSection(ctx, ".iplt", SHT_PROGBITS, SHF_ALLOC | SHF_EXECINSTR,
16) {
- if (ctx.arg.emachine == EM_PPC || ctx.arg.emachine == EM_PPC64) {
- name = ".glink";
- addralign = 4;
- }
-
// On AArch64, PLT entries only do loads from the .got.plt section, so the
// .iplt section can be marked with the SHF_AARCH64_PURECODE section flag.
if (ctx.arg.emachine == EM_AARCH64)
this->flags |= SHF_AARCH64_PURECODE;
+
+ if (ctx.arg.emachine == EM_PPC || ctx.arg.emachine == EM_PPC64) {
+ name = ".glink";
+ addralign = 4;
+ }
}
void IpltSection::writeTo(uint8_t *buf) {
>From 9cc56c4f5c39d97295d9db133880bcd0c9a5c8ee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Csan=C3=A1d=20Hajd=C3=BA?= <csanad.hajdu at arm.com>
Date: Thu, 10 Apr 2025 14:38:34 +0200
Subject: [PATCH 3/5] Add test to make sure .plt and .iplt will end up in XO
segment
---
lld/test/ELF/aarch64-execute-only-plt.s | 35 +++++++++++++++++++++++++
1 file changed, 35 insertions(+)
create mode 100644 lld/test/ELF/aarch64-execute-only-plt.s
diff --git a/lld/test/ELF/aarch64-execute-only-plt.s b/lld/test/ELF/aarch64-execute-only-plt.s
new file mode 100644
index 0000000000000..b6e3929b94037
--- /dev/null
+++ b/lld/test/ELF/aarch64-execute-only-plt.s
@@ -0,0 +1,35 @@
+// REQUIRES: aarch64
+
+// RUN: llvm-mc -filetype=obj -triple=aarch64 %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64 %S/Inputs/plt-aarch64.s -o %t2.o
+// RUN: ld.lld -shared %t2.o -o %t2.so
+// RUN: ld.lld %t.o %t2.so -o %t
+// RUN: llvm-readelf -S -l %t | FileCheck %s
+
+// CHECK-LABEL: Section Headers:
+// CHECK: .text PROGBITS 0000000000210300 000300 000010 00 AXy 0 0 4
+// CHECK: .plt PROGBITS 0000000000210310 000310 000030 00 AXy 0 0 16
+// CHECK: .iplt PROGBITS 0000000000210340 000340 000010 00 AXy 0 0 16
+
+// CHECK-LABEL: Program Headers:
+// CHECK: PHDR
+// CHECK-NEXT: LOAD
+// CHECK-NEXT: LOAD 0x000300 0x0000000000210300 0x0000000000210300 0x000050 0x000050 E 0x10000
+
+// CHECK-LABEL: Section to Segment mapping:
+/// Index should match the index of the LOAD segment above.
+// CHECK: 02 .text .plt .iplt
+
+.global bar
+
+.section .text,"axy", at progbits,unique,0
+.global _start
+_start:
+ bl foo
+ bl bar
+ ret
+
+.globl foo
+.type foo STT_GNU_IFUNC
+foo:
+ ret
>From d373afc840e52fb378b0bbc78614da5df4bd91f4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Csan=C3=A1d=20Hajd=C3=BA?= <csanad.hajdu at arm.com>
Date: Thu, 10 Apr 2025 14:57:10 +0200
Subject: [PATCH 4/5] Try to fix windows CI failure
---
lld/test/ELF/aarch64-execute-only-plt.s | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/lld/test/ELF/aarch64-execute-only-plt.s b/lld/test/ELF/aarch64-execute-only-plt.s
index b6e3929b94037..2c3609e0b3096 100644
--- a/lld/test/ELF/aarch64-execute-only-plt.s
+++ b/lld/test/ELF/aarch64-execute-only-plt.s
@@ -1,8 +1,8 @@
// REQUIRES: aarch64
-// RUN: llvm-mc -filetype=obj -triple=aarch64 %s -o %t.o
-// RUN: llvm-mc -filetype=obj -triple=aarch64 %S/Inputs/plt-aarch64.s -o %t2.o
-// RUN: ld.lld -shared %t2.o -o %t2.so
+// RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu %s -o %t.o
+// RUN: llvm-mc -filetype=obj -triple=aarch64-linux-gnu %S/Inputs/plt-aarch64.s -o %t2.o
+// RUN: ld.lld -shared %t2.o -soname=t2.so -o %t2.so
// RUN: ld.lld %t.o %t2.so -o %t
// RUN: llvm-readelf -S -l %t | FileCheck %s
>From add7b16c2e80479122ccaa15a37a7ce59b9919c4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Csan=C3=A1d=20Hajd=C3=BA?= <csanad.hajdu at arm.com>
Date: Thu, 10 Apr 2025 15:07:52 +0200
Subject: [PATCH 5/5] Actually fix test
---
lld/test/ELF/aarch64-execute-only-plt.s | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
diff --git a/lld/test/ELF/aarch64-execute-only-plt.s b/lld/test/ELF/aarch64-execute-only-plt.s
index 2c3609e0b3096..b74df4cf0cdb1 100644
--- a/lld/test/ELF/aarch64-execute-only-plt.s
+++ b/lld/test/ELF/aarch64-execute-only-plt.s
@@ -7,14 +7,14 @@
// RUN: llvm-readelf -S -l %t | FileCheck %s
// CHECK-LABEL: Section Headers:
-// CHECK: .text PROGBITS 0000000000210300 000300 000010 00 AXy 0 0 4
-// CHECK: .plt PROGBITS 0000000000210310 000310 000030 00 AXy 0 0 16
-// CHECK: .iplt PROGBITS 0000000000210340 000340 000010 00 AXy 0 0 16
+// CHECK: .text PROGBITS 00000000002102a0 0002a0 000010 00 AXy 0 0 4
+// CHECK: .plt PROGBITS 00000000002102b0 0002b0 000030 00 AXy 0 0 16
+// CHECK: .iplt PROGBITS 00000000002102e0 0002e0 000010 00 AXy 0 0 16
// CHECK-LABEL: Program Headers:
// CHECK: PHDR
// CHECK-NEXT: LOAD
-// CHECK-NEXT: LOAD 0x000300 0x0000000000210300 0x0000000000210300 0x000050 0x000050 E 0x10000
+// CHECK-NEXT: LOAD 0x0002a0 0x00000000002102a0 0x00000000002102a0 0x000050 0x000050 E 0x10000
// CHECK-LABEL: Section to Segment mapping:
/// Index should match the index of the LOAD segment above.
More information about the llvm-commits
mailing list