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

    <tr>
        <th>Summary</th>
        <td>
            [BOLT][AArch64] unable to run BOLT on libffi
        </td>
    </tr>

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

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

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

<pre>
    We originally ran into this building CPython for python-build-standalone, but here's a minimal reproducer:

```
ubuntu@ubuntu:~/libffi-3.5.1$ ./configure CFLAGS=-Wl,-emit-relocs
ubuntu@ubuntu:~/libffi-3.5.1$ make -j8
ubuntu@ubuntu:~/libffi-3.5.1$ llvm-bolt-21 aarch64-unknown-linux-gnu/.libs/libffi.so.8.2.0 -o=foo
BOLT-INFO: shared object or position-independent executable detected
BOLT-INFO: Target architecture: aarch64
BOLT-INFO: BOLT version: <unknown>
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-WARNING: function ffi_go_closure_SYSV/1 has an object detected in a padding region at address 0x18048
BOLT-WARNING: function __clear_cache/1 has an object detected in a padding region at address 0x18068
BOLT-WARNING: 1 collisions detected while hashing binary objects. Use -v=1 to see the list.
BOLT-INFO: number of removed linker-inserted veneers: 0
BOLT-INFO: 0 out of 134 functions in the binary (0.0%) have non-empty execution profile
BOLT-INFO: removed 17 empty blocks
BOLT-ERROR: cannot relax ADR in non-simple function trampoline_code_table/1
```

This is with https://github.com/libffi/libffi/releases/download/v3.5.1/libffi-3.5.1.tar.gz, Ubuntu Plucky, and the the current version from apt.llvm.org (bolt-21=1:21~++20250630095742+59a7185dd9d6-1~exp1~20250630215911.150).

I think this is because [trampoline_code_table is defined as hand-written assembly](https://github.com/libffi/libffi/blob/v3.5.1/src/aarch64/sysv.S#L580) that does an `adr` and has no `nop`s for BOLT to work with.

A naive attempt at throwing some extra `nop`s into that function gave a worse failure, which I guess makes sense because it's loading some PC-relative addresses and I surely got those addresses wrong. But also it would be cool to be able to run BOLT without requiring changes in third-party code.

FWIW I think I would be happy leaving this un-optimized, if that's an option, but the `adr` relaxation pass looks unconditional and it seems to error out instead of skipping optimizing this one function. I don't totally understand whether this pass really needs to be mandatory.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJykVt9v40YO_mvGL4QEaWT5x4MfnKQuAhS7i-z2gnsKRhpKmno01M2M7LgP-7cfKMvJNi2KHi5wYFmiyG8-kh-pQjCtQ9yJ8k6UDws1xo78rkVqmrioSF92zwjkTWucsvYCXjkwLhLEzgSoRmO1cS3cf7nEjhw05GGYLpPpWRKiclpZcijkPVRjhA49CrkOoKA3zvTKgsfBkx5r9KLYi2z6rLL5k-3HanRxFMtsvij234U8WFM1jUmKtExzIZeQCnmoyTWmHT3C_eGX_c9fRfGQPFsh7xPsTUw8WqrDP_bYqyNC8tvmH79g7alPKrIxkTko5etutUxGd3R0dok1bnxNWjcKeUitqcKbgzRQukllmkFConhoiES2v_v8y7fk8dPhsyj2EDrlUQNVv2EdgTmmYKIhlxincUCn0UXAV6zHqCqLoDFiHVF_dPRN-RYjMDTDFqNHvj1j_WjNP-CEPhhy_FsU9_NpRPHTR-PG-BBBWUs1KK09hgAmQPaafbSsParIVePwDIOn1qseOlQaPVzhq_jmInuVGf9x-VDTBIzvtz74Racqy36nPCvmB3rSeLN73j99evz08wR2dPX0vGnMS0svtaUwenz5-u-v_xLykEOnAih3Y_xGJxgHCgal9TVMyy7-ADbfZMvN3wV8eaktKv9Sq7rD_zfU6q9C5VCTtYaTFt7dnTtjkWN17K8yTvnLHDOk8GtASE6ieMghEgREiB2CNSGmH1l2Y1-hB2rAY08n1GCNO6JPjAvoOdQJHaIPbPynHGVAY-SX82L5Rkrgw3LAGZaQmyzNhCyF3EKnTgiOXIL9EC9zkTMZg6fGWPwY4YYqX8P1jcp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