<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/71804>71804</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [BOLT] Assertion `!HasError && "Cannot get value when an error exists!
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            BOLT
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          llongint
      </td>
    </tr>
</table>

<pre>
    I encountered the following error when using bolt to optimize the new version of the database. I wanted to ask if anyone has encountered this issue before. I was able to reproduce this error on a small test case. Is this error expected or is there something wrong with my linker script or compile options?

```css
/home/test/BiShengKernel/build4/bin/llvm-bolt pre.exe -o pre.opt
BOLT-INFO: Target architecture: aarch64
BOLT-INFO: BOLT version: 042c444c9a68
BOLT-INFO: first alloc address is 0x0
BOLT-INFO: creating new program header table at address 0x200000, offset 0x200000
BOLT-INFO: enabling relocation mode
BOLT-INFO: disabling -align-macro-fusion on non-x86 platform
llvm-bolt: /home/test/BiShengKernel/llvm/include/llvm/Support/ErrorOr.h:237: llvm::ErrorOr<T>::storage_type* llvm::ErrorOr<T>::getStorage() [with T = llvm::bolt::BinarySection&; llvm::ErrorOr<T>::storage_type = std::reference_wrapper<llvm::bolt::BinarySection>]: Assertion `!HasError && "Cannot get value when an error exists!"' failed.
 #0 0x0000aaaaeb2e5738 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/test/BiShengKernel/llvm/lib/Support/Unix/Signals.inc:565:22
 #1 0x0000aaaaeb2e5808 PrintStackTraceSignalHandler(void*) /home/test/BiShengKernel/llvm/lib/Support/Unix/Signals.inc:632:1
 #2 0x0000aaaaeb2e3640 llvm::sys::RunSignalHandlers() /home/test/BiShengKernel/llvm/lib/Support/Signals.cpp:103:20
 #3 0x0000aaaaeb2e4fd0 SignalHandler(int) /home/test/BiShengKernel/llvm/lib/Support/Unix/Signals.inc:407:1
 #4 0x0000ffff9570e7c0 (linux-vdso.so.1+0x7c0)
 #5 0x0000ffff953c4e80 raise (/lib64/libc.so.6+0x34e80)
 #6 0x0000ffff953c6374 abort (/lib64/libc.so.6+0x36374)
 #7 0x0000ffff953bddd4 (/lib64/libc.so.6+0x2ddd4)
 #8 0x0000ffff953bde5c (/lib64/libc.so.6+0x2de5c)
 #9 0x0000aaaaeb3849d0 llvm::ErrorOr<llvm::bolt::BinarySection&>::getStorage() /home/test/BiShengKernel/llvm/include/llvm/Support/ErrorOr.h:238:54
#10 0x0000aaaaeb383c90 llvm::ErrorOr<llvm::bolt::BinarySection&>::operator->() /home/test/BiShengKernel/llvm/include/llvm/Support/ErrorOr.h:158:21
#11 0x0000aaaaeb3950c8 llvm::bolt::RewriteInstance::getLSDAData() /home/test/BiShengKernel/bolt/lib/Rewrite/RewriteInstance.cpp:1658:48
#12 0x0000aaaaeb39c8ec llvm::bolt::RewriteInstance::disassembleFunctions() /home/test/BiShengKernel/bolt/lib/Rewrite/RewriteInstance.cpp:3033:25
#13 0x0000aaaaeb390c74 llvm::bolt::RewriteInstance::run() /home/test/BiShengKernel/bolt/lib/Rewrite/RewriteInstance.cpp:823:27
#14 0x0000aaaaea278258 main /home/test/BiShengKernel/bolt/tools/driver/llvm-bolt.cpp:244:29
#15 0x0000ffff953b0bec __libc_start_main (/lib64/libc.so.6+0x20bec)
#16 0x0000aaaaea2771dc _start (/home/test/BiShengKernel/build4/bin/llvm-bolt+0x53d1dc)
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /home/test/BiShengKernel/build4/bin/llvm-bolt pre.exe -o pre.opt
./test.sh: line 9: 74574 Aborted                 (core dumped) $CPATH/llvm-bolt $1.exe -o $1.opt
```

pre.cpp:
```cpp
#include <iostream>

class MyException : public std::exception {
public:
  const char *what() const throw() {
    return "Custom Exception: an error occurred!";
 }
};

int divide(int a, int b) {
  if (b == 0) {
    throw MyException();
  }
  return a / b;
}

int main() {
  try {
    int result = divide(10, 2); // normal case
    std::cout << "Result: " << result << std::endl;
    result = divide(5, 0); // will cause exception
    std::cout << "Result: " << result << std::endl;
 // this line will not execute
  } catch (const MyException &e) {
 // catch custom exception
    std::cerr << "Caught exception: " << e.what() << std::endl;
  } catch (const std::exception &e) {
    // catch other C++ exceptions
    std::cerr << "Caught exception: " << e.what() << std::endl;
  } catch (...) {
    // catch all other exceptions
    std::cerr << "Caught unknown exception" << std::endl;
 }

  return 0;
}
```
linker script:
```lds
SECTIONS {
  .text : { *(.text*) }
  . = 0x20000;
  .eh_frame : { *(.eh_frame) }
  . = 0x80000;
}
```

Reproduction steps and commands:
```bash
cd /home/test/test/issue/
/home/test/BiShengKernel/build4/bin/clang++ -O3 pre.cpp -c -o pre.o -O3 -flto=thin -Wl,-q -L/home/test/BiShengKernel/build/lib/aarch64-unknown-linux-gnu -lunwind -w
/home/test/BiShengKernel/build4/bin/clang++ pre.o -O3 -Wl,-q -fuse-ld=lld -no-pie -o pre.exe -O3 -flto=thin -Wl,-q -L/home/test/BiShengKernel/build/lib/aarch64-unknown-linux-gnu -lunwind -Wl,--script=mylld.lds 
/home/test/BiShengKernel/build4/bin/llvm-bolt pre.exe -o pre.opt
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzEWV1z4yrS_jXkpssuGSR_XOTCseM6U--8J1OTbO1lCkHbYgeBFlCc7K_fAsmW7WRmcmYnu6mUIyN4-ummu-km3Hu1M4jXpLghxfqKt6Gy7lpra3bKhKvSypfrT4BG2NYEdCghVAhbq7XdK7MDdM462FdooPVxoLQ6QLBgm6Bq9S9M8w3u4QmdV9aA3aYhyQMvuccxfII9NyEiW-D-G6gtcPNiDULF_YVo5UF53yKUuLWuX-yBlxrjcoeNs7IV2E3tyFkDHHzNtYaAPoDopPrTOfjcoIgcrAMV36BD8LbGUEWl9s7GTxUqqF9AK_MNHXjhVBPiCmHrRmlMOlvjCduQbE2yZf85zbpf4X0_QjeVrZHQ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