10b57cec5SDimitry Andric /*===- InstrProfilingPlatformWindows.c - Profile data on Windows ----------===*\
20b57cec5SDimitry Andric |*
30b57cec5SDimitry Andric |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric |* See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric |*
70b57cec5SDimitry Andric \*===----------------------------------------------------------------------===*/
80b57cec5SDimitry Andric
9*0fca6ea1SDimitry Andric #include <stddef.h>
10*0fca6ea1SDimitry Andric
110b57cec5SDimitry Andric #include "InstrProfiling.h"
12fe6060f1SDimitry Andric #include "InstrProfilingInternal.h"
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #if defined(_WIN32)
150b57cec5SDimitry Andric
160b57cec5SDimitry Andric #if defined(_MSC_VER)
170b57cec5SDimitry Andric /* Merge read-write sections into .data. */
185f757f3fSDimitry Andric #pragma comment(linker, "/MERGE:.lprfb=.data")
190b57cec5SDimitry Andric #pragma comment(linker, "/MERGE:.lprfd=.data")
200b57cec5SDimitry Andric #pragma comment(linker, "/MERGE:.lprfv=.data")
210b57cec5SDimitry Andric #pragma comment(linker, "/MERGE:.lprfnd=.data")
220b57cec5SDimitry Andric /* Do *NOT* merge .lprfn and .lcovmap into .rdata. llvm-cov must be able to find
230b57cec5SDimitry Andric * after the fact.
245f757f3fSDimitry Andric * Do *NOT* merge .lprfc .rdata. When binary profile correlation is enabled,
255f757f3fSDimitry Andric * llvm-cov must be able to find after the fact.
260b57cec5SDimitry Andric */
270b57cec5SDimitry Andric
280b57cec5SDimitry Andric /* Allocate read-only section bounds. */
290b57cec5SDimitry Andric #pragma section(".lprfn$A", read)
300b57cec5SDimitry Andric #pragma section(".lprfn$Z", read)
310b57cec5SDimitry Andric
320b57cec5SDimitry Andric /* Allocate read-write section bounds. */
330b57cec5SDimitry Andric #pragma section(".lprfd$A", read, write)
340b57cec5SDimitry Andric #pragma section(".lprfd$Z", read, write)
350b57cec5SDimitry Andric #pragma section(".lprfc$A", read, write)
360b57cec5SDimitry Andric #pragma section(".lprfc$Z", read, write)
375f757f3fSDimitry Andric #pragma section(".lprfb$A", read, write)
385f757f3fSDimitry Andric #pragma section(".lprfb$Z", read, write)
390b57cec5SDimitry Andric #pragma section(".lorderfile$A", read, write)
400b57cec5SDimitry Andric #pragma section(".lprfnd$A", read, write)
410b57cec5SDimitry Andric #pragma section(".lprfnd$Z", read, write)
420b57cec5SDimitry Andric #endif
430b57cec5SDimitry Andric
440b57cec5SDimitry Andric __llvm_profile_data COMPILER_RT_SECTION(".lprfd$A") DataStart = {0};
450b57cec5SDimitry Andric __llvm_profile_data COMPILER_RT_SECTION(".lprfd$Z") DataEnd = {0};
460b57cec5SDimitry Andric
470b57cec5SDimitry Andric const char COMPILER_RT_SECTION(".lprfn$A") NamesStart = '\0';
480b57cec5SDimitry Andric const char COMPILER_RT_SECTION(".lprfn$Z") NamesEnd = '\0';
490b57cec5SDimitry Andric
5004eeddc0SDimitry Andric char COMPILER_RT_SECTION(".lprfc$A") CountersStart;
5104eeddc0SDimitry Andric char COMPILER_RT_SECTION(".lprfc$Z") CountersEnd;
525f757f3fSDimitry Andric char COMPILER_RT_SECTION(".lprfb$A") BitmapStart;
535f757f3fSDimitry Andric char COMPILER_RT_SECTION(".lprfb$Z") BitmapEnd;
540b57cec5SDimitry Andric uint32_t COMPILER_RT_SECTION(".lorderfile$A") OrderFileStart;
550b57cec5SDimitry Andric
560b57cec5SDimitry Andric ValueProfNode COMPILER_RT_SECTION(".lprfnd$A") VNodesStart;
570b57cec5SDimitry Andric ValueProfNode COMPILER_RT_SECTION(".lprfnd$Z") VNodesEnd;
580b57cec5SDimitry Andric
__llvm_profile_begin_data(void)590b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_begin_data(void) {
600b57cec5SDimitry Andric return &DataStart + 1;
610b57cec5SDimitry Andric }
__llvm_profile_end_data(void)620b57cec5SDimitry Andric const __llvm_profile_data *__llvm_profile_end_data(void) { return &DataEnd; }
630b57cec5SDimitry Andric
64*0fca6ea1SDimitry Andric // Type profiling isn't implemented under MSVC ABI, so return NULL (rather than
65*0fca6ea1SDimitry Andric // implementing linker magic on Windows) to make it more explicit. To elaborate,
66*0fca6ea1SDimitry Andric // the current type profiling implementation maps a profiled vtable address to a
67*0fca6ea1SDimitry Andric // vtable variable through vtables mangled name. Under MSVC ABI, the variable
68*0fca6ea1SDimitry Andric // name for vtables might not be the mangled name (see
69*0fca6ea1SDimitry Andric // MicrosoftCXXABI::getAddrOfVTable in MicrosoftCXXABI.cpp for more details on
70*0fca6ea1SDimitry Andric // how a vtable name is computed). Note the mangled name is still in the vtable
71*0fca6ea1SDimitry Andric // IR (just not variable name) for mapping purpose, but more implementation work
72*0fca6ea1SDimitry Andric // is required.
__llvm_profile_begin_vtables(void)73*0fca6ea1SDimitry Andric const VTableProfData *__llvm_profile_begin_vtables(void) { return NULL; }
__llvm_profile_end_vtables(void)74*0fca6ea1SDimitry Andric const VTableProfData *__llvm_profile_end_vtables(void) { return NULL; }
75*0fca6ea1SDimitry Andric
__llvm_profile_begin_names(void)760b57cec5SDimitry Andric const char *__llvm_profile_begin_names(void) { return &NamesStart + 1; }
__llvm_profile_end_names(void)770b57cec5SDimitry Andric const char *__llvm_profile_end_names(void) { return &NamesEnd; }
780b57cec5SDimitry Andric
79*0fca6ea1SDimitry Andric // Type profiling isn't supported on Windows, so return NULl to make it more
80*0fca6ea1SDimitry Andric // explicit.
__llvm_profile_begin_vtabnames(void)81*0fca6ea1SDimitry Andric const char *__llvm_profile_begin_vtabnames(void) { return NULL; }
__llvm_profile_end_vtabnames(void)82*0fca6ea1SDimitry Andric const char *__llvm_profile_end_vtabnames(void) { return NULL; }
83*0fca6ea1SDimitry Andric
__llvm_profile_begin_counters(void)8404eeddc0SDimitry Andric char *__llvm_profile_begin_counters(void) { return &CountersStart + 1; }
__llvm_profile_end_counters(void)8504eeddc0SDimitry Andric char *__llvm_profile_end_counters(void) { return &CountersEnd; }
__llvm_profile_begin_bitmap(void)865f757f3fSDimitry Andric char *__llvm_profile_begin_bitmap(void) { return &BitmapStart + 1; }
__llvm_profile_end_bitmap(void)875f757f3fSDimitry Andric char *__llvm_profile_end_bitmap(void) { return &BitmapEnd; }
__llvm_profile_begin_orderfile(void)880b57cec5SDimitry Andric uint32_t *__llvm_profile_begin_orderfile(void) { return &OrderFileStart; }
890b57cec5SDimitry Andric
__llvm_profile_begin_vnodes(void)900b57cec5SDimitry Andric ValueProfNode *__llvm_profile_begin_vnodes(void) { return &VNodesStart + 1; }
__llvm_profile_end_vnodes(void)910b57cec5SDimitry Andric ValueProfNode *__llvm_profile_end_vnodes(void) { return &VNodesEnd; }
920b57cec5SDimitry Andric
930b57cec5SDimitry Andric ValueProfNode *CurrentVNode = &VNodesStart + 1;
940b57cec5SDimitry Andric ValueProfNode *EndVNode = &VNodesEnd;
950b57cec5SDimitry Andric
96*0fca6ea1SDimitry Andric /* lld-link provides __buildid symbol which points to the 16 bytes build id when
97cb14a3feSDimitry Andric * using /build-id flag. https://lld.llvm.org/windows_support.html#lld-flags */
98cb14a3feSDimitry Andric #define BUILD_ID_LEN 16
99*0fca6ea1SDimitry Andric COMPILER_RT_WEAK uint8_t __buildid[BUILD_ID_LEN] = {0};
__llvm_write_binary_ids(ProfDataWriter * Writer)100fe6060f1SDimitry Andric COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
101*0fca6ea1SDimitry Andric static const uint8_t zeros[BUILD_ID_LEN] = {0};
102*0fca6ea1SDimitry Andric if (memcmp(__buildid, zeros, BUILD_ID_LEN) != 0) {
103cb14a3feSDimitry Andric if (Writer &&
104cb14a3feSDimitry Andric lprofWriteOneBinaryId(Writer, BUILD_ID_LEN, __buildid, 0) == -1)
105cb14a3feSDimitry Andric return -1;
106cb14a3feSDimitry Andric return sizeof(uint64_t) + BUILD_ID_LEN;
107cb14a3feSDimitry Andric }
108fe6060f1SDimitry Andric return 0;
109fe6060f1SDimitry Andric }
110fe6060f1SDimitry Andric
1110b57cec5SDimitry Andric #endif
112