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

    <tr>
        <th>Summary</th>
        <td>
            [BOLT] Segmentation fault after optimization of shared library with relocation mode enabled
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    Hi guys!

We are trying to optimize one of our shared libraries with BOLT but faced an issue with segfault after the library is optimized or instrumented. It's enough just to link the processed library to any application, even simple one like "hello world", to reproduce the issue, and it happens during loading other dynamic libraries that the initial library depends on.
```
Breakpoint 6, 0x00007ffff60c8620 in __cxa_atexit@plt () from /home/ppreblagin/src/git/test-router/lib/../lib/liblog4cplus.so.8
Continuing.

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff6271b5e in __GI__IO_fwrite (buf=0x55555556b2a0, size=93824992326432, count=7, fp=0x636f6d7070616374) at iofwrite.c:35
35      iofwrite.c: No such file or directory.
#0  0x00007ffff6271b5e in __GI__IO_fwrite (buf=0x55555556b2a0, size=93824992326432, count=7, fp=0x636f6d7070616374) at iofwrite.c:35

#1  0x00007ffff75a9c68 in std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) const () from lib/router.so.26
#2 0x00007ffff61313c9 in std::pair<std::_Rb_tree_iterator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<Catch::IReporterFactory> > >, bool> std::_Rb_tree<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<Catch::IReporterFactory> >, std::_Select1st<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<Catch::IReporterFactory> > >, std::less<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const, std::shared_ptr<Catch::IReporterFactory> > > >::_M_emplace_unique<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::shared_ptr<Catch::IReporterFactory> const&>(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::shared_ptr<Catch::IReporterFactory> const&) () from /home/ppreblagin/src/git/test-router/lib/../lib/liblog4cplus.so.8
#3 0x00007ffff6131e57 in Catch::ReporterRegistrar<Catch::ConsoleReporter>::ReporterRegistrar(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) ()
   from /home/ppreblagin/src/git/test-router/lib/../lib/liblog4cplus.so.8
#4 0x00007ffff60dd969 in _GLOBAL__sub_I_global_init.cxx () from /home/ppreblagin/src/git/test-router/lib/../lib/liblog4cplus.so.8
#5 0x00007ffff7fe438a in call_init (l=<optimized out>, argc=argc@entry=1, argv=argv@entry=0x7fffffffe3b8, env=env@entry=0x7fffffffe3c8) at dl-init.c:72
#6  0x00007ffff7fe4486 in call_init (env=0x7fffffffe3c8, argv=0x7fffffffe3b8, argc=1, l=<optimized out>) at dl-init.c:30
#7 _dl_init (main_map=0x7ffff7ffe190, argc=1, argv=0x7fffffffe3b8, env=0x7fffffffe3c8) at dl-init.c:119
#8  0x00007ffff7fd60ca in _dl_start_user () from /lib64/ld-linux-x86-64.so.2
#9  0x0000000000000001 in ?? ()
#10 0x00007fffffffe64c in ?? ()
#11 0x0000000000000000 in ?? ()
```
The library is compiled and linked with
```
-O3 -DNDEBUG -fPIC -Wall -Wextra -Wnon-virtual-dtor -Wcast-align -fno-reorder-blocks-and-partition -std=c++17
```
and 
```
-fPIC -O3 -DNDEBUG -Wl,--exclude-libs=ALL -Wl,--disable-new-dtags -Wl,--start-group -Wl,--emit-relocs -shared -Wl,-soname,router.so.26 <list-of-dependencies>
```
After a research I figured out it's related to relocation mode, and `-relocs=false` helps to avoid the issue sacrificing some optimization options.  

I've attached logs to the ticket:
[gdb.txt](https://github.com/llvm/llvm-project/files/12326707/gdb.txt)
[llvm-bolt.txt](https://github.com/llvm/llvm-project/files/12326708/llvm-bolt.txt)
[perf2bolt.txt](https://github.com/llvm/llvm-project/files/12326709/perf2bolt.txt)

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzcWFFv47gR_jX0y0CGRNqS_eCH2D5vA6S3h91t8yhQ5EjihSZVkkqc-_UFJduxk-wBba_Z9gTDsjzDme8bDknNcO9VYxBXZL4m8-2E96G1bvXUptl8Uln5vPqLgqZ_9oRmJN2S9Gb8vkfgDiG4Z2UaCBZsF9Re_YZgDYKtwfYOfMsdStCqctwp9PCkQgvrz3ffoOoD1FygBG5Aed_jKPTY1LzXAXgd0EFo8Tj8GZQ_O5FgHSjjg-v3aALKKdwGQgsPaGzftPBr70NEpZV5GIx0zgr0_ozmOUq5eQbedVoJHpQ1hG4AH9GAV_tOj0y0ekAglLaotYUn67QklEbNYMFh56zsBQ4uBhZRwo0EFaDlXYfGg-xdjJG2XMa7DS06kM-G75W4iE1oeRjtGBUU12egEjs00oM10-MM5OnxMzyuHfKHzioTII_u00OapmlR13Wdp2KR0xSUgbIUB17ygAcVyCztdABCF4QuoXZ2D4TuWrtHQndd57DSvFGG0J13gtBdowKhu4A-JM72AR2hO60qQnfT6fmnVpW2zUx0uvdTb6eLEd3GmqBMr0wzvcyfX5xtHN-DQ4HqESXENOQavt5--vrTp79HHl-xiZM7TA0MSXG0cEmQFlk1x5Hgp9uyvP1c1k9OhThpi6qvCdumh_l45RXlabTs1W9I2HbJFnS2XFJG8xkb5lTY3gTCtkV8qLthcM7yOpdFWqR5lrNiFkPGAyg7-pkKwm7YfETG5jBcV0L42YLvRQu1iknlQCqHIlj3fIoIZSnA_y6rM8rsCmUx50uRLyJKHyRhN4TdxCw7ZNn4UHG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