Skip to content

Commit a9c1ae8

Browse files
authored
[BOLT][AArch64] Fix another cause of extra entry point misidentification (#155055)
1 parent 337707a commit a9c1ae8

File tree

3 files changed

+45
-4
lines changed

3 files changed

+45
-4
lines changed

bolt/lib/Core/BinaryFunction.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3773,6 +3773,8 @@ MCSymbol *BinaryFunction::addEntryPointAtOffset(uint64_t Offset) {
37733773
assert(Offset && "cannot add primary entry point");
37743774

37753775
const uint64_t EntryPointAddress = getAddress() + Offset;
3776+
assert(!isInConstantIsland(EntryPointAddress) &&
3777+
"cannot add entry point that points to constant data");
37763778
MCSymbol *LocalSymbol = getOrCreateLocalLabel(EntryPointAddress);
37773779

37783780
MCSymbol *EntrySymbol = getSecondaryEntryPointSymbol(LocalSymbol);

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2935,7 +2935,8 @@ void RewriteInstance::handleRelocation(const SectionRef &RelocatedSection,
29352935
ReferencedSymbol = nullptr;
29362936
ExtractedValue = Address;
29372937
} else if (RefFunctionOffset) {
2938-
if (ContainingBF && ContainingBF != ReferencedBF) {
2938+
if (ContainingBF && ContainingBF != ReferencedBF &&
2939+
!ReferencedBF->isInConstantIsland(Address)) {
29392940
ReferencedSymbol =
29402941
ReferencedBF->addEntryPointAtOffset(RefFunctionOffset);
29412942
} else {

bolt/test/AArch64/validate-secondary-entry-point.s

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,23 @@
11
# This test is to verify that BOLT won't take a label pointing to constant
2-
# island as a secondary entry point (function `_start` doesn't have ELF size
3-
# set originally) and the function won't otherwise be mistaken as non-simple.
2+
# island as a secondary entry point. This could happen when function doesn't
3+
# have ELF size set if it is from assembly code, or a constant island is
4+
# referenced by another function discovered during relocation processing.
45

5-
# RUN: %clang %cflags -pie %s -o %t.so -Wl,-q -Wl,--init=_foo -Wl,--fini=_foo
6+
# RUN: split-file %s %t
7+
8+
# RUN: %clang %cflags -pie %t/tt.asm -o %t.so \
9+
# RUN: -Wl,-q -Wl,--init=_foo -Wl,--fini=_foo
610
# RUN: llvm-bolt %t.so -o %t.bolt.so --print-cfg 2>&1 | FileCheck %s
711
# CHECK-NOT: BOLT-WARNING: reference in the middle of instruction detected \
812
# CHECK-NOT: function _start at offset 0x{{[0-9a-f]+}}
913
# CHECK: Binary Function "_start" after building cfg
1014

15+
# RUN: %clang %cflags -ffunction-sections -shared %t/tt.c %t/ss.c -o %tt.so \
16+
# RUN: -Wl,-q -Wl,--init=_start -Wl,--fini=_start \
17+
# RUN: -Wl,--version-script=%t/linker_script
18+
# RUN: llvm-bolt %tt.so -o %tt.bolted.so
19+
20+
;--- tt.asm
1121
.text
1222

1323
.global _foo
@@ -32,3 +42,31 @@ _bar:
3242

3343
# Dummy relocation to force relocation mode
3444
.reloc 0, R_AARCH64_NONE
45+
46+
;--- tt.c
47+
void _start() {}
48+
49+
__attribute__((naked)) void foo() {
50+
asm("ldr x16, .L_fnptr\n"
51+
"blr x16\n"
52+
"ret\n"
53+
54+
"_rodatx:"
55+
".global _rodatx;"
56+
".quad 0;"
57+
".L_fnptr:"
58+
".quad 0;");
59+
}
60+
61+
;--- ss.c
62+
__attribute__((visibility("hidden"))) extern void* _rodatx;
63+
void* bar() { return &_rodatx; }
64+
65+
;--- linker_script
66+
{
67+
global:
68+
_start;
69+
foo;
70+
bar;
71+
local: *;
72+
};

0 commit comments

Comments
 (0)