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

    <tr>
        <th>Summary</th>
        <td>
            Unresolved symbols __llvm_profile_write_buffer and __llvm_profile_get_size_for_buffer
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          Erich-McMillan
      </td>
    </tr>
</table>

<pre>
    Preface:
I'm not too experienced with compiler options, so I apologize if there's not enough info, let me know what I'm missing.
Thanks for taking the time to help :)

Issue:
Trying to following instructions for compiling with code coverage without file system as described here: https://clang.llvm.org/docs/SourceBasedCodeCoverage.html#introduction.

I am adding the following code in an attempt to obtain code coverage in a bare-metal environment:
```
int __llvm_profile_runtime;

char* AllocateCodeCoverageBuffer() {
   int temp = __llvm_profile_runtime;
   temp += 1;
   uint64_t size = __llvm_profile_get_size_for_buffer();

   char* buffer = AllocateZeroPool((UINTN) size);
   return buffer;
}

void WriteCodeCoverageBufferToSerial(char* buffer) {
   uint64_t buffer_size = __llvm_profile_get_size_for_buffer();
   buffer_size -= 1;

   __llvm_profile_write_buffer(buffer);

   DBG_TRACE_MSG_GUID(L"Code Coverage Results start for driver", gEfiCallerBaseName);
   PRINT_BYTES(buffer,buffer_size);
   DBG_TRACE_MSG_GUID(L"Code Coverage Results end for driver", gEfiCallerBaseName);
}
```

But I get the following error:
```
lld-link: error: undefined symbol: __llvm_profile_get_size_for_buffer
>>> referenced by CodeCoverage.c
>>>               my.lib(myCodeCoverage.obj)

lld-link: error: undefined symbol: __llvm_profile_write_buffer
>>> referenced by eCodeCoverage.c
>>>               my.lib(myCodeCoverage.obj)
NMAKE : fatal error U1077: '"C:\Program Files\LLVM\bin\lld-link.EXE"' : return code '0x1'
Stop.
```

GCC supports bare-metal environments, but when I investigated the llvm runtime code at compiler-rt\lib\profile\InstrProfilingBuffer.c it looks like it is strictly tied to stdio.h and stdlib.h, but even when I don't exclude those from the compile (i.e don't use -nostdlib) the compilation still fails.

I do see the .gcno files generated in the build output so it seem the compiler is emitting the profiled code correctly, but the linker is not happy about the symbols since it can't find them. Could be that `x86_64-unknown-windows-gnu` target is not supported, but I can't find any documentation on supported targets for --coverage.

More Info:
Host: Windows 10
Clang version: 
- clang version 13.0.1
- Target: x86_64-pc-windows-msvc
- Thread model: posix
Target: x86_64-unknown-windows-gnu (UEFI/Tianocore)
Compile/link command line options:
- compile options: `-g -fshort-wchar -fno-strict-aliasing -Wall -Werror -Wno-array-bounds -ffunction-sections -fdata-sections -include AutoGen.h -fno-common -DSTRING_ARRAY_NAME=FlipDxeStrings -Wno-parentheses-equality -Wno-tautological-compare -Wno-tautological-constant-out-of-range-compare -Wno-empty-body -Wno-unused-const-variable -Wno-varargs -Wno-unknown-warning-option -Wno-microsoft-enum-forward-reference -Wno-unused-but-set-variable -fno-stack-protector -funsigned-char -ftrap-function=undefined_behavior_has_been_optimized_away_by_clang -Wno-address -Wno-shift-negative-value -Wno-unknown-pragmas -Wno-incompatible-library-redeclaration -Wno-null-dereference -mno-implicit-float -mms-bitfields -mno-stack-arg-probe -m64 "-DEFIAPI=__attribute__((ms_abi))" -mno-red-zone -mcmodel=small -Oz -flto -target x86_64-unknown-windows-gnu -gcodeview -funwind-tables -ftest-coverage -fprofile-instr-generate -fcoverage-mapping`
- link options: `/NOLOGO /IGNORE:4001 /OPT:REF /OPT:ICF=10 /ALIGN:32 /FILEALIGN:32 /Machine:X64 /DLL /ENTRY:_ModuleEntryPoint /SUBSYSTEM:EFI_BOOT_SERVICE_DRIVER /SAFESEH:NO /BASE:0 /DEBUG:GHASH /lldmap /ALIGN:4096`


</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytWFlzIjkS_jX4RSGCw_h48AOnm1hfAbh7el4qVFWC0rhKYiUVNv3r90tVFQa2p2dnYh0EtqRU5pd3yrFJ93cvV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