Skip to content

Commit c5a306f

Browse files
authored
[BOLT] Fix LSDA section handling (#71821)
Currently BOLT finds LSDA secition by it's name .gcc_except_table.main . But sometimes it might have suffix e.g. .gcc_except_table.main. Find LSDA section by it's address, rather by it's name. Fixes #71804
1 parent ffd337b commit c5a306f

File tree

4 files changed

+62
-17
lines changed

4 files changed

+62
-17
lines changed

bolt/include/bolt/Rewrite/RewriteInstance.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -400,12 +400,6 @@ class RewriteInstance {
400400
/// Manage a pipeline of metadata handlers.
401401
class MetadataManager MetadataManager;
402402

403-
/// Get the contents of the LSDA section for this binary.
404-
ArrayRef<uint8_t> getLSDAData();
405-
406-
/// Get the mapped address of the LSDA section for this binary.
407-
uint64_t getLSDAAddress();
408-
409403
static const char TimerGroupName[];
410404

411405
static const char TimerGroupDesc[];
@@ -550,7 +544,6 @@ class RewriteInstance {
550544
}
551545

552546
/// Exception handling and stack unwinding information in this binary.
553-
ErrorOr<BinarySection &> LSDASection{std::errc::bad_address};
554547
ErrorOr<BinarySection &> EHFrameSection{std::errc::bad_address};
555548

556549
/// .note.gnu.build-id section.

bolt/lib/Rewrite/RewriteInstance.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1784,13 +1784,6 @@ void RewriteInstance::relocateEHFrameSection() {
17841784
check_error(std::move(E), "failed to patch EH frame");
17851785
}
17861786

1787-
ArrayRef<uint8_t> RewriteInstance::getLSDAData() {
1788-
return ArrayRef<uint8_t>(LSDASection->getData(),
1789-
LSDASection->getContents().size());
1790-
}
1791-
1792-
uint64_t RewriteInstance::getLSDAAddress() { return LSDASection->getAddress(); }
1793-
17941787
Error RewriteInstance::readSpecialSections() {
17951788
NamedRegionTimer T("readSpecialSections", "read special sections",
17961789
TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
@@ -1829,7 +1822,6 @@ Error RewriteInstance::readSpecialSections() {
18291822

18301823
HasTextRelocations = (bool)BC->getUniqueSectionByName(".rela.text");
18311824
HasSymbolTable = (bool)BC->getUniqueSectionByName(".symtab");
1832-
LSDASection = BC->getUniqueSectionByName(".gcc_except_table");
18331825
EHFrameSection = BC->getUniqueSectionByName(".eh_frame");
18341826
BuildIDSection = BC->getUniqueSectionByName(".note.gnu.build-id");
18351827

@@ -3200,8 +3192,14 @@ void RewriteInstance::disassembleFunctions() {
32003192

32013193
// Parse LSDA.
32023194
if (Function.getLSDAAddress() != 0 &&
3203-
!BC->getFragmentsToSkip().count(&Function))
3204-
Function.parseLSDA(getLSDAData(), getLSDAAddress());
3195+
!BC->getFragmentsToSkip().count(&Function)) {
3196+
ErrorOr<BinarySection &> LSDASection =
3197+
BC->getSectionForAddress(Function.getLSDAAddress());
3198+
check_error(LSDASection.getError(), "failed to get LSDA section");
3199+
ArrayRef<uint8_t> LSDAData = ArrayRef<uint8_t>(
3200+
LSDASection->getData(), LSDASection->getContents().size());
3201+
Function.parseLSDA(LSDAData, LSDASection->getAddress());
3202+
}
32053203
}
32063204
}
32073205

bolt/test/Inputs/lsda.ldscript

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
SECTIONS {
2+
.text : { *(.text*) }
3+
.gcc_except_table.main : { *(.gcc_except_table*) }
4+
. = 0x20000;
5+
.eh_frame : { *(.eh_frame) }
6+
. = 0x80000;
7+
}

bolt/test/lsda.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// This test check that LSDA section named by .gcc_except_table.main is
2+
// disassembled by BOLT.
3+
4+
// RUN: %clang++ %cxxflags -O3 -flto=thin -no-pie -c %s -o %t.o
5+
// RUN: %clang++ %cxxflags -flto=thin -no-pie -fuse-ld=lld %t.o -o %t.exe \
6+
// RUN: -Wl,-q -Wl,--script=%S/Inputs/lsda.ldscript
7+
// RUN: llvm-readelf -SW %t.exe | FileCheck %s
8+
// RUN: llvm-bolt %t.exe -o %t.bolt
9+
10+
// CHECK: .gcc_except_table.main
11+
12+
#include <iostream>
13+
14+
class MyException : public std::exception {
15+
public:
16+
const char *what() const throw() {
17+
return "Custom Exception: an error occurred!";
18+
}
19+
};
20+
21+
int divide(int a, int b) {
22+
if (b == 0) {
23+
throw MyException();
24+
}
25+
return a / b;
26+
}
27+
28+
int main() {
29+
try {
30+
int result = divide(10, 2); // normal case
31+
std::cout << "Result: " << result << std::endl;
32+
result = divide(5, 0); // will cause exception
33+
std::cout << "Result: " << result << std::endl;
34+
// this line will not execute
35+
} catch (const MyException &e) {
36+
// catch custom exception
37+
std::cerr << "Caught exception: " << e.what() << std::endl;
38+
} catch (const std::exception &e) {
39+
// catch other C++ exceptions
40+
std::cerr << "Caught exception: " << e.what() << std::endl;
41+
} catch (...) {
42+
// catch all other exceptions
43+
std::cerr << "Caught unknown exception" << std::endl;
44+
}
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)