From 15b4f66b0a9a3be6caf1898d22a13c39e662006f Mon Sep 17 00:00:00 2001 From: Szabolcs Nagy Date: Wed, 18 Jan 2023 12:56:46 +0000 Subject: [PATCH] bfd: aarch64: Fix stubs that may break BTI PR30076 Insert two stubs in a BTI enabled binary when fixing long calls: The first is near the call site and uses an indirect jump like before, but it targets the second stub that is near the call target site and uses a direct jump. This is needed when a single stub breaks BTI compatibility. The stub layout is kept fixed between sizing and building the stubs, so the location of the second stub is known at build time, this may introduce padding between stubs when those are relaxed. Stub layout with BTI disabled is unchanged. --- bfd/elfnn-aarch64.c | 175 ++++++++++++++++++++++-- ld/testsuite/ld-aarch64/aarch64-elf.exp | 3 + ld/testsuite/ld-aarch64/bti-far-1.d | 83 +++++++++++ ld/testsuite/ld-aarch64/bti-far-2.d | 62 +++++++++ ld/testsuite/ld-aarch64/bti-far.ld | 15 ++ ld/testsuite/ld-aarch64/bti-far.s | 29 ++++ 6 files changed, 352 insertions(+), 15 deletions(-) create mode 100644 ld/testsuite/ld-aarch64/bti-far-1.d create mode 100644 ld/testsuite/ld-aarch64/bti-far-2.d create mode 100644 ld/testsuite/ld-aarch64/bti-far.ld create mode 100644 ld/testsuite/ld-aarch64/bti-far.s diff --git a/bfd/elfnn-aarch64.c b/bfd/elfnn-aarch64.c index f858d10c596..d9ebeae922c 100644 --- a/bfd/elfnn-aarch64.c +++ b/bfd/elfnn-aarch64.c @@ -2354,6 +2354,9 @@ elfNN_aarch64_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED, name can be changed. The only requirement is the %s be present. */ #define STUB_ENTRY_NAME "__%s_veneer" +/* Stub name for a BTI landing stub. */ +#define BTI_STUB_ENTRY_NAME "__%s_bti_veneer" + /* The name of the dynamic interpreter. This is put in the .interp section. */ #define ELF_DYNAMIC_INTERPRETER "/lib/ld.so.1" @@ -2406,6 +2409,12 @@ static const uint32_t aarch64_long_branch_stub[] = 0x00000000, }; +static const uint32_t aarch64_bti_direct_branch_stub[] = +{ + 0xd503245f, /* bti c */ + 0x14000000, /* b