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

    <tr>
        <th>Summary</th>
        <td>
            BOLT instrumentation fails on aarch64 both on linux_intel-x86 and MacOs_M2(compatible?)
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          cirt-os
      </td>
    </tr>
</table>

<pre>
    I have made attempts to perform bolt optimization on an aarch64 binary,but come accross with such many problems.

1. First I did bolt on on linux_intel-x86, and failed 
```
$ ./build/bin/llvm-bolt test2 -instrument -o t2_instr
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-WARNING: debug info will be stripped from the binary. Use -update-debug-sections to keep it.
BOLT-INFO: enabling relocation mode
BOLT-INFO: forcing -jump-tables=move for instrumentation
BOLT-INFO: disabling -align-macro-fusion on non-x86 platform
./build/bin/llvm-bolt: 'test2': No relocation for first DT_FINI_ARRAY slot.
```
And the error comes from:
```
Error RewriteInstance::discoverRtFiniAddress() {
  ...
  if (const Relocation *Reloc = FiniArraySection->getDynamicRelocationAt(0)) {
    BC->FiniFunctionAddress = Reloc->Addend;
    return Error::success();
  }

  if (const Relocation *Reloc = FiniArraySection->getRelocationAt(0)) {
    BC->FiniFunctionAddress = Reloc->Value;
    return Error::success();
  }

  return createStringError(std::errc::not_supported,
                           "No relocation for first DT_FINI_ARRAY slot");
}
```
I checked that the Offset of fini_function in this binary did not start with 0x0 but with 0x8. Interesting, but why would threre be a deviation of offset calculation.
```
DynamicRelocations: Type size:213 ,Type:1027, Offset:0x8, Addend:0x1460, Value:0x0
DynamicRelocations: Type size:213 ,Type:1027, Offset:0x10, Addend:0x1454, Value:0x0
``` 
2. Then I changed the offset manually and recompiled llvm-bolt,And failed again:
```
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-WARNING: debug info will be stripped from the binary. Use -update-debug-sections to keep it.
BOLT-INFO: enabling relocation mode
BOLT-INFO: forcing -jump-tables=move for instrumentation
BOLT-INFO: disabling -align-macro-fusion on non-x86 platform
BOLT-INFO: number of removed linker-inserted veneers: 0
BOLT-INFO: 0 out of 9 functions in the binary (0.0%) have non-empty execution profile
BOLT-INSTRUMENTER: Number of indirect call site descriptors: 5
BOLT-INSTRUMENTER: Number of indirect call target descriptors: 8
BOLT-INSTRUMENTER: Number of function descriptors: 8
BOLT-INSTRUMENTER: Number of branch counters: 90
BOLT-INSTRUMENTER: Number of ST leaf node counters: 0
BOLT-INSTRUMENTER: Number of direct call counters: 40
BOLT-INSTRUMENTER: Total number of counters: 130
BOLT-INSTRUMENTER: Total size of counters: 1040 bytes (static alloc memory)
BOLT-INSTRUMENTER: Total size of string table emitted: 922 bytes in file
BOLT-INSTRUMENTER: Total size of descriptors: 4224 bytes in file
BOLT-INSTRUMENTER: Profile will be saved to file t1.fdata
BOLT-INFO: Starting stub-insertion pass
BOLT-INFO: Inserted 0 stubs in the hot area and 0 stubs in the cold area. Shared 0 times, iterated 1 times.
BOLT-INFO: padding code to 0x600000 to accommodate hot text
BOLT-ERROR: linking object with arch x86_64 into context with arch aarch64
```
Bolt cross-compiled on x86 machine seems not supported. The library `libbolt_rt_instr.a` seems to be generated dynamicly while performing
> cmake -G Ninja ../llvm-project/llvm -DLLVM_TARGETS_TO_BUILD="X86;AArch64" -DCMAKE_BUILD_TYPE=Release -DLLVM_ENABLE_ASSERTIONS=ON -DLLVM_ENABLE_PROJECTS="bolt"  

It seems that, `libbolt_rt_instr.a` was built on x86, and can't be linked with aarch64 binary, too bad.

3.  At last, I used a Mac with M2 chip based on ARM, hoping that it would be compatible with aarch64, and then had a try on MacOs, but unfortunately it turned out to be like this:
```
BOLT-INFO: shared object or position-independent executable detected
BOLT-INFO: Target architecture: aarch64
BOLT-INFO: BOLT version: 058e4454e8965f0d96ecbec61395d5a788c37f7c
BOLT-INFO: first alloc address is 0x0
BOLT-INFO: creating new program header table at address 0x200000, offset 0x200000
BOLT-WARNING: debug info will be stripped from the binary. Use -update-debug-sections to keep it.
BOLT-INFO: enabling relocation mode
BOLT-INFO: forcing -jump-tables=move for instrumentation
BOLT-INFO: disabling -align-macro-fusion on non-x86 platform
BOLT-INFO: number of removed linker-inserted veneers: 0
BOLT-INFO: 0 out of 9 functions in the binary (0.0%) have non-empty execution profile
BOLT-INSTRUMENTER: Number of indirect call site descriptors: 5
BOLT-INSTRUMENTER: Number of indirect call target descriptors: 8
BOLT-INSTRUMENTER: Number of function descriptors: 8
BOLT-INSTRUMENTER: Number of branch counters: 30
BOLT-INSTRUMENTER: Number of ST leaf node counters: 18
BOLT-INSTRUMENTER: Number of direct call counters: 0
BOLT-INSTRUMENTER: Total number of counters: 48
BOLT-INSTRUMENTER: Total size of counters: 384 bytes (static alloc memory)
BOLT-INSTRUMENTER: Total size of string table emitted: 922 bytes in file
BOLT-INSTRUMENTER: Total size of descriptors: 4368 bytes in file
BOLT-INSTRUMENTER: Profile will be saved to file /tmp/prof.fdata
BOLT-INFO: Starting stub-insertion pass
BOLT-INFO: Inserted 0 stubs in the hot area and 0 stubs in the cold area. Shared 0 times, iterated 1 times.
BOLT-INFO: padding code to 0x600000 to accommodate hot text
BOLT-ERROR: library not found: ./build/lib/libbolt_rt_instr.a
``` 
So confusing,`libbolt_rt_instr.a` was not generated. Moreover, the directory `build/tools/bolt/bolt_rt-bins`  did not exist as well. 

**Over all, are these known problem? Did i miss something, or are there any way to fix them. Thanks.**
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsWU1v4zgS_TXMpWBBp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