From ef3756f46211efcdb13b0d0f436164b2e8d3f94a Mon Sep 17 00:00:00 2001 From: rfwang07 Date: Wed, 4 Sep 2024 16:15:30 +0800 Subject: [PATCH] Backport bugfix --- ...or-zero-value-weak-reference-symbols.patch | 96 +++++++++++++++++++ llvm-bolt.spec | 9 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 0007-BOLT-Skip-PLT-search-for-zero-value-weak-reference-symbols.patch diff --git a/0007-BOLT-Skip-PLT-search-for-zero-value-weak-reference-symbols.patch b/0007-BOLT-Skip-PLT-search-for-zero-value-weak-reference-symbols.patch new file mode 100644 index 0000000..905962d --- /dev/null +++ b/0007-BOLT-Skip-PLT-search-for-zero-value-weak-reference-symbols.patch @@ -0,0 +1,96 @@ +From 6c8933e1a095028d648a5a26aecee0f569304dd0 Mon Sep 17 00:00:00 2001 +From: sinan +Date: Wed, 7 Aug 2024 18:02:42 +0800 +Subject: [PATCH] [BOLT] Skip PLT search for zero-value weak reference symbols + (#69136) + +Take a common weak reference pattern for example +``` + __attribute__((weak)) void undef_weak_fun(); + + if (&undef_weak_fun) + undef_weak_fun(); +``` + +In this case, an undefined weak symbol `undef_weak_fun` has an address +of zero, and Bolt incorrectly changes the relocation for the +corresponding symbol to symbol@PLT, leading to incorrect runtime +behavior. +--- + bolt/lib/Rewrite/RewriteInstance.cpp | 11 +++++- + .../AArch64/update-weak-reference-symbol.s | 34 +++++++++++++++++++ + 2 files changed, 44 insertions(+), 1 deletion(-) + create mode 100644 bolt/test/AArch64/update-weak-reference-symbol.s + +diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp +index 78b4889bf2ae..d2e2ca2f7553 100644 +--- a/bolt/lib/Rewrite/RewriteInstance.cpp ++++ b/bolt/lib/Rewrite/RewriteInstance.cpp +@@ -2143,6 +2143,14 @@ bool RewriteInstance::analyzeRelocation( + if (!Relocation::isSupported(RType)) + return false; + ++ auto IsWeakReference = [](const SymbolRef &Symbol) { ++ Expected SymFlagsOrErr = Symbol.getFlags(); ++ if (!SymFlagsOrErr) ++ return false; ++ return (*SymFlagsOrErr & SymbolRef::SF_Undefined) && ++ (*SymFlagsOrErr & SymbolRef::SF_Weak); ++ }; ++ + const bool IsAArch64 = BC->isAArch64(); + + const size_t RelSize = Relocation::getSizeForType(RType); +@@ -2174,7 +2182,8 @@ bool RewriteInstance::analyzeRelocation( + // Section symbols are marked as ST_Debug. + IsSectionRelocation = (cantFail(Symbol.getType()) == SymbolRef::ST_Debug); + // Check for PLT entry registered with symbol name +- if (!SymbolAddress && (IsAArch64 || BC->isRISCV())) { ++ if (!SymbolAddress && !IsWeakReference(Symbol) && ++ (IsAArch64 || BC->isRISCV())) { + const BinaryData *BD = BC->getPLTBinaryDataByName(SymbolName); + SymbolAddress = BD ? BD->getAddress() : 0; + } +diff --git a/bolt/test/AArch64/update-weak-reference-symbol.s b/bolt/test/AArch64/update-weak-reference-symbol.s +new file mode 100644 +index 000000000000..600a06b8b6d8 +--- /dev/null ++++ b/bolt/test/AArch64/update-weak-reference-symbol.s +@@ -0,0 +1,34 @@ ++// This test checks whether BOLT can correctly handle relocations against weak symbols. ++ ++// RUN: %clang %cflags -Wl,-z,notext -shared -Wl,-q %s -o %t.so ++// RUN: llvm-bolt %t.so -o %t.so.bolt ++// RUN: llvm-nm -n %t.so.bolt > %t.out.txt ++// RUN: llvm-objdump -dj .rodata %t.so.bolt >> %t.out.txt ++// RUN: FileCheck %s --input-file=%t.out.txt ++ ++# CHECK: w func_1 ++# CHECK: {{0+}}[[#%x,ADDR:]] W func_2 ++ ++# CHECK: {{.*}} <.rodata>: ++# CHECK-NEXT: {{.*}} .word 0x00000000 ++# CHECK-NEXT: {{.*}} .word 0x00000000 ++# CHECK-NEXT: {{.*}} .word 0x{{[0]+}}[[#ADDR]] ++# CHECK-NEXT: {{.*}} .word 0x00000000 ++ ++ .text ++ .weak func_2 ++ .weak func_1 ++ .global wow ++ .type wow, %function ++wow: ++ bl func_1 ++ bl func_2 ++ ret ++ .type func_2, %function ++func_2: ++ ret ++ .section .rodata ++.LC0: ++ .xword func_1 ++.LC1: ++ .xword func_2 +-- +2.39.3 (Apple Git-146) + diff --git a/llvm-bolt.spec b/llvm-bolt.spec index fd0a8f9..cce50cd 100644 --- a/llvm-bolt.spec +++ b/llvm-bolt.spec @@ -22,7 +22,7 @@ Name: %{pkg_name} Version: %{bolt_version} -Release: 7 +Release: 8 Summary: BOLT is a post-link optimizer developed to speed up large applications License: Apache 2.0 URL: https://github.com/llvm/llvm-project/tree/main/bolt @@ -36,6 +36,7 @@ Patch3: 0003-AArch64-Add-AArch64-support-for-inline.patch Patch4: 0004-Bolt-Solving-pie-support-issue.patch Patch5: 0005-BOLT-AArch64-Don-t-change-layout-in-PatchEntries.patch Patch6: 0006-AArch64-Add-CFG-block-count-correction-optimization.patch +Patch7: 0007-BOLT-Skip-PLT-search-for-zero-value-weak-reference-symbols.patch BuildRequires: gcc BuildRequires: gcc-c++ @@ -147,6 +148,12 @@ rm -f %{buildroot}/%{_builddir}/%{bolt_srcdir}/%{_vpath_builddir}/%{_lib}/lib*.a %doc %{install_docdir} %changelog +* Wed Sep 04 2024 rfwang07 17.0.6-8 +- Type:Backport +- ID:NA +- SUG:NA +- DESC: Backport bugfix. + * Fri Jul 12 2024 rfwang07 17.0.6-7 - Type:Feature - ID:NA -- Gitee