From 316a6ff3d08f986e7b38f81415fd611c2e876fd6 Mon Sep 17 00:00:00 2001 From: YongKang Zhu Date: Tue, 29 Apr 2025 08:13:44 -0700 Subject: [PATCH] [BOLT][RelVTable] Skip special handling on non virtual function pointer relocations (#137406) Besides virtual function pointers vtable could contain other kinds of entries like those for RTTI data that also require relocations. We need to skip special handling on relocations for non virtual function pointers in relative vtable. Co-authored-by: Maksim Panchenko --- bolt/lib/Rewrite/RewriteInstance.cpp | 25 +++++++------ ...ip-non-vfuncptr-reloc-in-relative-vtable.s | 35 +++++++++++++++++++ 2 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 bolt/test/AArch64/skip-non-vfuncptr-reloc-in-relative-vtable.s diff --git a/bolt/lib/Rewrite/RewriteInstance.cpp b/bolt/lib/Rewrite/RewriteInstance.cpp index 69fb736d7bde..1c2420005903 100644 --- a/bolt/lib/Rewrite/RewriteInstance.cpp +++ b/bolt/lib/Rewrite/RewriteInstance.cpp @@ -2697,20 +2697,19 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection, BD->nameStartsWith("_ZTCN"))) { // construction vtable BinaryFunction *BF = BC->getBinaryFunctionContainingAddress( SymbolAddress, /*CheckPastEnd*/ false, /*UseMaxSize*/ true); - if (!BF || BF->getAddress() != SymbolAddress) { - BC->errs() - << "BOLT-ERROR: the virtual function table entry at offset 0x" - << Twine::utohexstr(Rel.getOffset()); - if (BF) - BC->errs() << " points to the middle of a function @ 0x" - << Twine::utohexstr(BF->getAddress()) << "\n"; - else - BC->errs() << " does not point to any function\n"; - exit(1); + if (BF) { + if (BF->getAddress() != SymbolAddress) { + BC->errs() + << "BOLT-ERROR: the virtual function table entry at offset 0x" + << Twine::utohexstr(Rel.getOffset()) + << " points to the middle of a function @ 0x" + << Twine::utohexstr(BF->getAddress()) << "\n"; + exit(1); + } + BC->addRelocation(Rel.getOffset(), BF->getSymbol(), RType, Addend, + ExtractedValue); + return; } - BC->addRelocation(Rel.getOffset(), BF->getSymbol(), RType, Addend, - ExtractedValue); - return; } } diff --git a/bolt/test/AArch64/skip-non-vfuncptr-reloc-in-relative-vtable.s b/bolt/test/AArch64/skip-non-vfuncptr-reloc-in-relative-vtable.s new file mode 100644 index 000000000000..41a00313427d --- /dev/null +++ b/bolt/test/AArch64/skip-non-vfuncptr-reloc-in-relative-vtable.s @@ -0,0 +1,35 @@ +// Test the fix that BOLT should skip special handling of any non-virtual +// function pointer relocations in relative vtable. + +// RUN: llvm-mc -filetype=obj -triple aarch64-unknown-gnu %s -o %t.o +// RUN: %clang %cxxflags -fuse-ld=lld %t.o -o %t.so -Wl,-q +// RUN: llvm-bolt %t.so -o %t.bolted.so + + .text + .p2align 2 + .type foo,@function +foo: + .cfi_startproc + adrp x8, _ZTV3gooE + add x8, x8, :lo12:_ZTV3gooE + ldr x0, [x8] + ret +.Lfunc_end0: + .size foo, .Lfunc_end0-foo + .cfi_endproc + + .type _fake_rtti_data,@object + .section .rodata.cst16._fake_rtti_data,"aMG",@progbits,16,_fake_rtti_data,comdat + .p2align 3, 0x0 +_fake_rtti_data: + .ascii "_FAKE_RTTI_DATA_" + .size _fake_rtti_data, 16 + + .type _ZTV3gooE,@object + .section .rodata,"a",@progbits + .p2align 2, 0x0 +_ZTV3gooE: + .word 0 + .word _fake_rtti_data-_ZTV3gooE-8 + .word foo@PLT-_ZTV3gooE-8 + .size _ZTV3gooE, 12