Skip to content

Commit 31b6c6f

Browse files
committed
[BOLT] Fix LSDA section handling
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 9d4094a commit 31b6c6f

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
@@ -391,12 +391,6 @@ class RewriteInstance {
391391
/// Manage a pipeline of metadata handlers.
392392
class MetadataManager MetadataManager;
393393

394-
/// Get the contents of the LSDA section for this binary.
395-
ArrayRef<uint8_t> getLSDAData();
396-
397-
/// Get the mapped address of the LSDA section for this binary.
398-
uint64_t getLSDAAddress();
399-
400394
static const char TimerGroupName[];
401395

402396
static const char TimerGroupDesc[];
@@ -540,7 +534,6 @@ class RewriteInstance {
540534
}
541535

542536
/// Exception handling and stack unwinding information in this binary.
543-
ErrorOr<BinarySection &> LSDASection{std::errc::bad_address};
544537
ErrorOr<BinarySection &> EHFrameSection{std::errc::bad_address};
545538

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

bolt/lib/Rewrite/RewriteInstance.cpp

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

1709-
ArrayRef<uint8_t> RewriteInstance::getLSDAData() {
1710-
return ArrayRef<uint8_t>(LSDASection->getData(),
1711-
LSDASection->getContents().size());
1712-
}
1713-
1714-
uint64_t RewriteInstance::getLSDAAddress() { return LSDASection->getAddress(); }
1715-
17161709
Error RewriteInstance::readSpecialSections() {
17171710
NamedRegionTimer T("readSpecialSections", "read special sections",
17181711
TimerGroupName, TimerGroupDesc, opts::TimeRewrite);
@@ -1751,7 +1744,6 @@ Error RewriteInstance::readSpecialSections() {
17511744

17521745
HasTextRelocations = (bool)BC->getUniqueSectionByName(".rela.text");
17531746
HasSymbolTable = (bool)BC->getUniqueSectionByName(".symtab");
1754-
LSDASection = BC->getUniqueSectionByName(".gcc_except_table");
17551747
EHFrameSection = BC->getUniqueSectionByName(".eh_frame");
17561748
BuildIDSection = BC->getUniqueSectionByName(".note.gnu.build-id");
17571749

@@ -3109,8 +3101,14 @@ void RewriteInstance::disassembleFunctions() {
31093101

31103102
// Parse LSDA.
31113103
if (Function.getLSDAAddress() != 0 &&
3112-
!BC->getFragmentsToSkip().count(&Function))
3113-
Function.parseLSDA(getLSDAData(), getLSDAAddress());
3104+
!BC->getFragmentsToSkip().count(&Function)) {
3105+
ErrorOr<BinarySection &> LSDASection =
3106+
BC->getSectionForAddress(Function.getLSDAAddress());
3107+
check_error(LSDASection.getError(), "failed to get LSDA section");
3108+
ArrayRef<uint8_t> LSDAData = ArrayRef<uint8_t>(
3109+
LSDASection->getData(), LSDASection->getContents().size());
3110+
Function.parseLSDA(LSDAData, LSDASection->getAddress());
3111+
}
31143112
}
31153113
}
31163114

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)