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

    <tr>
        <th>Summary</th>
        <td>
            Assertion failure when using bolt with shared library and --update-debug-sections
        </td>
    </tr>

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

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

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

<pre>
    Hi,

we have an issue with our shared libraries and bolt.
We use the main branch of llvm-project.

We can't manage to make --update-debug-sections work with shared libraries generated by our main compiler.
Even trivial libraries fail, so maybe there's something wrong with our compiler ? I believe we have some custom sections inserted in the shared libraries, are these an issue ?

```
/opt/1A/toolchain/x86_64-v19/bin/g++ --version
g++ (GCC) 9.2.1 20200225
Copyright (C) 2019 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```

Here is a simple example, with the following test.cpp:
```cpp
void f(){}
```
`/opt/1A/toolchain/x86_64-v19/bin/g++ test.cpp -march=broadwell -fPIC -shared -o test.so.1.1   -g `

Here's the resulting .so:
       
[test.zip](https://github.com/llvm/llvm-project/files/8196570/test.zip)
      

cat p.fdata
```
1 _init/1 e 1 _init/1 12 0 1

```
                                                                                                                                                                                                     
```                                                                                                                                                                                                                
llvm-bolt test.so.1.1 -o test.bolt.so.1.1 --data p.fdata  --update-debug-sections                                                                                                                                  
BOLT-INFO: shared object or position-independent executable detected                                                                                                                                               
BOLT-INFO: Target architecture: x86_64                                                                                                                                                                             
BOLT-INFO: BOLT version: 46266b3518b6190d4eaa8e4920371c87fd784f46                                                                                                                                                  
BOLT-INFO: first alloc address is 0x0                                                                                                                                                                              
BOLT-INFO: creating new program header table at address 0x200000, offset 0x200000                                                                                                                                  
BOLT-INFO: disabling -align-macro-fusion in non-relocation mode                                                                                                                                                    
BOLT-INFO: enabling lite mode                                                                                                                                                                                      
BOLT-INFO: pre-processing profile using branch profile reader                                                                                                                                                      
BOLT-WARNING: Ignored 0 functions due to cold fragments.                                                                                                                                                           
BOLT-INFO: 1 out of 8 functions in the binary (12.5%) have non-empty execution profile                                                                                                                             
BOLT-INFO: 0 instructions were shortened                                                                                                                                                                           
BOLT-INFO: UCE removed 0 blocks and 0 bytes of code.    
BOLT-INFO: SCTC: patched 0 tail calls (0 forward) tail calls (0 backward) from a total of 0 while removing 0 double jumps and removing 0 basic blocks totalling 0 bytes of code. CTCs total execution count is 0 and the number of times CTCs are taken is 0.
BOLT: 1 out of 9 functions were overwritten.
BOLT-INFO: rewritten functions cover 100.00% of the execution count of simple functions of this binary
BOLT-INFO: patched build-id (flipped last bit)
llvm-bolt: /home/klehrmann/src/llvm-project/bolt/lib/Core/DebugData.cpp:647: virtual std::__cxx11::string llvm::bolt::DebugInfoBinaryPatcher::patchBinary(llvm::StringRef): Assertion `Offset + ByteSequence.size() <= BinaryContents.size() && "Applied patch runs over binary size."' failed.
 #0 0x00007f780059526c llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /home/klehrmann/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:565:0
 #1 0x00007f7800595323 PrintStackTraceSignalHandler(void*) /home/klehrmann/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:632:0
 #2 0x00007f7800592fd2 llvm::sys::RunSignalHandlers() /home/klehrmann/src/llvm-project/llvm/lib/Support/Signals.cpp:97:0
 #3 0x00007f7800594bed SignalHandler(int) /home/klehrmann/src/llvm-project/llvm/lib/Support/Unix/Signals.inc:407:0
 #4 0x00007f77ffde2f60 (/lib/x86_64-linux-gnu/libc.so.6+0x43f60)
 #5 0x00007f77ffde2ed7 raise /build/glibc-KRRWSm/glibc-2.29/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #6 0x00007f77ffdc4535 abort /build/glibc-KRRWSm/glibc-2.29/stdlib/abort.c:81:7
 #7 0x00007f77ffdc440f _nl_load_domain /build/glibc-KRRWSm/glibc-2.29/intl/loadmsgcat.c:1177:9
 #8 0x00007f77ffdd4012 (/lib/x86_64-linux-gnu/libc.so.6+0x35012)
 #9 0x00007f77fe379f3c llvm::bolt::DebugInfoBinaryPatcher::patchBinary[abi:cxx11](llvm::StringRef) /home/klehrmann/src/llvm-project/bolt/lib/Core/DebugData.cpp:646:0
#10 0x00007f780399625d llvm::bolt::RewriteInstance::rewriteNoteSections() /home/klehrmann/src/llvm-project/bolt/lib/Rewrite/RewriteInstance.cpp:3830:0
#11 0x00007f7803998250 llvm::bolt::RewriteInstance::rewriteFile() /home/klehrmann/src/llvm-project/bolt/lib/Rewrite/RewriteInstance.cpp:5093:0
#12 0x00007f7803981f68 llvm::bolt::RewriteInstance::run() /home/klehrmann/src/llvm-project/bolt/lib/Rewrite/RewriteInstance.cpp:784:0
#13 0x0000558b68656cf2 main /home/klehrmann/src/llvm-project/bolt/tools/driver/llvm-bolt.cpp:255:0
#14 0x00007f77ffdc5b6b __libc_start_main /build/glibc-KRRWSm/glibc-2.29/csu/../csu/libc-start.c:342:3
#15 0x0000558b686556ba _start (/home/klehrmann/src/llvm-project/buildnodocker/bin/llvm-bolt+0xf6ba)
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: llvm-bolt test.so.1.1 -o test.bolt.so.1.1 --data p.fdata --update-debug-sections
Aborted (core dumped)

```

Any idea what is going wrong ?
We'd gladly share more information if need be.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzdWVlv4zgS_jXOC2FBt-yHPDhOPG1MbxLYaTT2yaBEyuZEErUkFcfz66eKkuIj7t5OzzSSXcKQJYpFfnWwDiqVbHf5SQz86cC9HriT9rrlZEOfOKEVEVo3nGyF2RDZKKI3VHFGCpEqqgTXMISRVBbGaSm_ctJoTsyGk5KKisCwKgPSnBTFUzmslfyDZ_3gF5KMVgM_MUBS0TVQS7h75GQ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