Skip to content

Commit 0b41062

Browse files
mtvectru
authored andcommitted
[BOLT] Reject symbols pointing to section end
Sometimes, symbols are present that point to the end of a section (i.e., one-past the highest valid address). Currently, BOLT either rejects those symbols when they don't point to another existing section, or errs when they do and the other section is not executable. I suppose BOLT would accept the symbol when it points to an executable section. In any case, these symbols should not be considered while discovering functions and should not result in an error. This patch implements that. Note that this patch checks explicitly for symbols whose value equals the end of their section. It might make more sense to verify that the symbol's value is within [section start, section end). However, I'm not sure if this could every happen *and* its value does not equal the end. Another way to implement this is to verify that the BinarySection we find at the symbol's address actually corresponds to the symbol's section. I'm not sure what the best approach is so feedback is welcome. Reviewed By: yota9, rafauler Differential Revision: https://reviews.llvm.org/D146215 (cherry picked from commit 54ab954)
1 parent 14ed120 commit 0b41062

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1043,6 +1043,16 @@ void RewriteInstance::discoverFileObjects() {
10431043
LLVM_DEBUG(dbgs() << "BOLT-DEBUG: considering symbol " << UniqueName
10441044
<< " for function\n");
10451045

1046+
if (Address == Section->getAddress() + Section->getSize()) {
1047+
assert(SymbolSize == 0 &&
1048+
"unexpect non-zero sized symbol at end of section");
1049+
LLVM_DEBUG(
1050+
dbgs()
1051+
<< "BOLT-DEBUG: rejecting as symbol points to end of its section\n");
1052+
registerName(SymbolSize);
1053+
continue;
1054+
}
1055+
10461056
if (!Section->isText()) {
10471057
assert(SymbolType != SymbolRef::ST_Function &&
10481058
"unexpected function inside non-code section");

bolt/test/X86/section-end-sym.s

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Check that BOLT doesn't consider end-of-section symbols (e.g., _etext) as
2+
## functions.
3+
4+
# REQUIRES: system-linux
5+
6+
# RUN: llvm-mc -filetype=obj -triple x86_64-unknown-linux %s -o %t.o
7+
# RUN: ld.lld %t.o -o %t.exe -q
8+
# RUN: llvm-bolt %t.exe -o /dev/null --print-cfg --debug-only=bolt 2>&1 \
9+
# RUN: | FileCheck %s
10+
11+
# CHECK: considering symbol etext for function
12+
# CHECK-NEXT: rejecting as symbol points to end of its section
13+
# CHECK-NOT: Binary Function "etext{{.*}}" after building cfg
14+
15+
16+
.text
17+
.globl _start
18+
.type _start,@function
19+
_start:
20+
retq
21+
.size _start, .-_start
22+
23+
.align 0x1000
24+
.globl etext
25+
etext:
26+
27+
.data
28+
.Lfoo:
29+
.word 0

0 commit comments

Comments
 (0)