xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformWindows.c (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
1 /*===- InstrProfilingPlatformWindows.c - Profile data on Windows ----------===*\
2 |*
3 |* Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 |* See https://llvm.org/LICENSE.txt for license information.
5 |* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 |*
7 \*===----------------------------------------------------------------------===*/
8 
9 #include <stddef.h>
10 
11 #include "InstrProfiling.h"
12 #include "InstrProfilingInternal.h"
13 
14 #if defined(_WIN32)
15 
16 #if defined(_MSC_VER)
17 /* Merge read-write sections into .data. */
18 #pragma comment(linker, "/MERGE:.lprfb=.data")
19 #pragma comment(linker, "/MERGE:.lprfd=.data")
20 #pragma comment(linker, "/MERGE:.lprfv=.data")
21 #pragma comment(linker, "/MERGE:.lprfnd=.data")
22 /* Do *NOT* merge .lprfn and .lcovmap into .rdata. llvm-cov must be able to find
23  * after the fact.
24  * Do *NOT* merge .lprfc .rdata. When binary profile correlation is enabled,
25  * llvm-cov must be able to find after the fact.
26  */
27 
28 /* Allocate read-only section bounds. */
29 #pragma section(".lprfn$A", read)
30 #pragma section(".lprfn$Z", read)
31 
32 /* Allocate read-write section bounds. */
33 #pragma section(".lprfd$A", read, write)
34 #pragma section(".lprfd$Z", read, write)
35 #pragma section(".lprfc$A", read, write)
36 #pragma section(".lprfc$Z", read, write)
37 #pragma section(".lprfb$A", read, write)
38 #pragma section(".lprfb$Z", read, write)
39 #pragma section(".lorderfile$A", read, write)
40 #pragma section(".lprfnd$A", read, write)
41 #pragma section(".lprfnd$Z", read, write)
42 #endif
43 
44 __llvm_profile_data COMPILER_RT_SECTION(".lprfd$A") DataStart = {0};
45 __llvm_profile_data COMPILER_RT_SECTION(".lprfd$Z") DataEnd = {0};
46 
47 const char COMPILER_RT_SECTION(".lprfn$A") NamesStart = '\0';
48 const char COMPILER_RT_SECTION(".lprfn$Z") NamesEnd = '\0';
49 
50 char COMPILER_RT_SECTION(".lprfc$A") CountersStart;
51 char COMPILER_RT_SECTION(".lprfc$Z") CountersEnd;
52 char COMPILER_RT_SECTION(".lprfb$A") BitmapStart;
53 char COMPILER_RT_SECTION(".lprfb$Z") BitmapEnd;
54 uint32_t COMPILER_RT_SECTION(".lorderfile$A") OrderFileStart;
55 
56 ValueProfNode COMPILER_RT_SECTION(".lprfnd$A") VNodesStart;
57 ValueProfNode COMPILER_RT_SECTION(".lprfnd$Z") VNodesEnd;
58 
__llvm_profile_begin_data(void)59 const __llvm_profile_data *__llvm_profile_begin_data(void) {
60   return &DataStart + 1;
61 }
__llvm_profile_end_data(void)62 const __llvm_profile_data *__llvm_profile_end_data(void) { return &DataEnd; }
63 
64 // Type profiling isn't implemented under MSVC ABI, so return NULL (rather than
65 // implementing linker magic on Windows) to make it more explicit. To elaborate,
66 // the current type profiling implementation maps a profiled vtable address to a
67 // vtable variable through vtables mangled name. Under MSVC ABI, the variable
68 // name for vtables might not be the mangled name (see
69 // MicrosoftCXXABI::getAddrOfVTable in MicrosoftCXXABI.cpp for more details on
70 // how a vtable name is computed). Note the mangled name is still in the vtable
71 // IR (just not variable name) for mapping purpose, but more implementation work
72 // is required.
__llvm_profile_begin_vtables(void)73 const VTableProfData *__llvm_profile_begin_vtables(void) { return NULL; }
__llvm_profile_end_vtables(void)74 const VTableProfData *__llvm_profile_end_vtables(void) { return NULL; }
75 
__llvm_profile_begin_names(void)76 const char *__llvm_profile_begin_names(void) { return &NamesStart + 1; }
__llvm_profile_end_names(void)77 const char *__llvm_profile_end_names(void) { return &NamesEnd; }
78 
79 // Type profiling isn't supported on Windows, so return NULl to make it more
80 // explicit.
__llvm_profile_begin_vtabnames(void)81 const char *__llvm_profile_begin_vtabnames(void) { return NULL; }
__llvm_profile_end_vtabnames(void)82 const char *__llvm_profile_end_vtabnames(void) { return NULL; }
83 
__llvm_profile_begin_counters(void)84 char *__llvm_profile_begin_counters(void) { return &CountersStart + 1; }
__llvm_profile_end_counters(void)85 char *__llvm_profile_end_counters(void) { return &CountersEnd; }
__llvm_profile_begin_bitmap(void)86 char *__llvm_profile_begin_bitmap(void) { return &BitmapStart + 1; }
__llvm_profile_end_bitmap(void)87 char *__llvm_profile_end_bitmap(void) { return &BitmapEnd; }
__llvm_profile_begin_orderfile(void)88 uint32_t *__llvm_profile_begin_orderfile(void) { return &OrderFileStart; }
89 
__llvm_profile_begin_vnodes(void)90 ValueProfNode *__llvm_profile_begin_vnodes(void) { return &VNodesStart + 1; }
__llvm_profile_end_vnodes(void)91 ValueProfNode *__llvm_profile_end_vnodes(void) { return &VNodesEnd; }
92 
93 ValueProfNode *CurrentVNode = &VNodesStart + 1;
94 ValueProfNode *EndVNode = &VNodesEnd;
95 
96 /* lld-link provides __buildid symbol which points to the 16 bytes build id when
97  * using /build-id flag. https://lld.llvm.org/windows_support.html#lld-flags */
98 #define BUILD_ID_LEN 16
99 COMPILER_RT_WEAK uint8_t __buildid[BUILD_ID_LEN] = {0};
__llvm_write_binary_ids(ProfDataWriter * Writer)100 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
101   static const uint8_t zeros[BUILD_ID_LEN] = {0};
102   if (memcmp(__buildid, zeros, BUILD_ID_LEN) != 0) {
103     if (Writer &&
104         lprofWriteOneBinaryId(Writer, BUILD_ID_LEN, __buildid, 0) == -1)
105       return -1;
106     return sizeof(uint64_t) + BUILD_ID_LEN;
107   }
108   return 0;
109 }
110 
111 #endif
112