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

    <tr>
        <th>Summary</th>
        <td>
            Using __llvm_profile_{get_size_for_buffer,write_buffer,reset_counters}() in freestanding environments
        </td>
    </tr>

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

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

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

<pre>
    Hello,

I'm trying to use LLVM profiling on a kernel, and would like to export the data to a buffer using the provided `__llvm_profile_{get_size_for_buffer,write_buffer,reset_counters}()` helpers. I've declared external prototypes for them at the caller translation unit, and also defined `__llvm_profile_runtime` on that same translation unit as a global symbol.

However, when attempting to link the resulting object files together, I get the following errors:

```
# ld  -melf_x86_64_fbsd  -T arch/x86/xen.lds -N prelink.o --build-id=sha1 ./common/symbols-dummy.o -o ./.xen-syms.0
ld: error: undefined hidden symbol: __llvm_profile_get_size_for_buffer
>>> referenced by lib/xxhash64.c
>>> prelink.o:(get_size)
>>> referenced by lib/xxhash64.c
>>> prelink.o:(dump)

ld: error: undefined hidden symbol: __llvm_profile_reset_counters
>>> referenced by lib/xxhash64.c
>>> prelink.o:(reset_counters)

ld: error: undefined hidden symbol: __llvm_profile_write_buffer
>>> referenced by lib/xxhash64.c
>>> prelink.o:(dump)
```

I can kind of workaround those by adding the compiler_rt profiling library to the linker phase:

```
# ld /usr/lib/clang/19/lib/freebsd/libclang_rt.profile-x86_64.a -melf_x86_64_fbsd -T arch/x86/xen.lds -N prelink.o --build-id=sha1 ./common/symbols-dummy.o -o ./.xen-syms.0
ld: error: /usr/lib/clang/19/lib/freebsd/libclang_rt.profile-x86_64.a(InstrProfilingPlatformLinux.o):(function __llvm_write_binary_ids: .text+0xf3): relocation R_X86_64_PC32 out of range: 137644034653121 is not in [-2147483648, 2147483647]; references '__ehdr_start'
>>> referenced by InstrProfilingPlatformLinux.c:179 (/usr/src/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c:179)
>>> defined in <internal>

ld: error: /usr/lib/clang/19/lib/freebsd/libclang_rt.profile-x86_64.a(InstrProfilingPlatformLinux.o):(function __llvm_write_binary_ids: .text+0x118): relocation R_X86_64_PC32 out of range: 137644034653028 is not in [-2147483648, 2147483647]; references '__ehdr_start'
>>> referenced by InstrProfilingPlatformLinux.c:0 (/usr/src/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c:0)
>>> defined in <internal>

ld: error: /usr/lib/clang/19/lib/freebsd/libclang_rt.profile-x86_64.a(InstrProfilingPlatformLinux.o):(function __llvm_write_binary_ids: .text+0x11f): relocation R_X86_64_PC32 out of range: 137644034653053 is not in [-2147483648, 2147483647]; references '__ehdr_start'
>>> referenced by InstrProfilingPlatformLinux.c:0 (/usr/src/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c:0)
>>> defined in <internal>

ld: error: /usr/lib/clang/19/lib/freebsd/libclang_rt.profile-x86_64.a(InstrProfilingPlatformLinux.o):(function __llvm_write_binary_ids: .text+0x19e): relocation R_X86_64_PC32 out of range: 137644034652894 is not in [-2147483648, 2147483647]; references '__ehdr_start'
>>> referenced by InstrProfilingPlatformLinux.c:0 (/usr/src/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformLinux.c:0)
>>> defined in <internal>
```

But that's ugly, as I don't know a programatic way to get the name and path of the required compiler_rt profiling library, neither does it solve the issue, as now there are undefined references to `__ehdr_start`.  Is there some guidance I'm missing about how to use profiling on freestanding environments?
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWFtv47YS_jX0y8CCRF1sP_jBSY6xAfYcLA7aom8CJY4kNhTpDilf-usLSvJukk2DAgnaxWIBI4goci7ffDPmZ-Gcag3iluU3LL9biMF3lrZkLy3SorLysv2AWlvGb1m8Y_HunvFVD54uyrTgLQwO4ePHX_4L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