xref: /llvm-project/bolt/test/lsda-section-name.cpp (revision c7b7875e1e3e27995f0c8ec53f7ded305dc9d730)
1 // This test check that LSDA section named by .gcc_except_table.main is
2 // disassembled by BOLT.
3 
4 // RUN: %clang++ %cxxflags -O3 -no-pie -c %s -o %t.o
5 // RUN: %clang++ %cxxflags -O3 -no-pie -fuse-ld=lld %t.o -o %t
6 // RUN: llvm-objcopy --rename-section .gcc_except_table=.gcc_except_table.main %t
7 // RUN: llvm-readelf -SW %t | FileCheck %s
8 // RUN: llvm-bolt %t -o %t.bolt
9 
10 // CHECK: .gcc_except_table.main
11 
12 #include <iostream>
13 
14 class MyException : public std::exception {
15 public:
what() const16   const char *what() const throw() {
17     return "Custom Exception: an error occurred!";
18   }
19 };
20 
divide(int a,int b)21 int divide(int a, int b) {
22   if (b == 0) {
23     throw MyException();
24   }
25   return a / b;
26 }
27 
main()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 }
48