xref: /freebsd/contrib/llvm-project/compiler-rt/lib/profile/InstrProfilingPlatformWindows.c (revision 1db9f3b21e39176dd5b67cf8ac378633b172463e)
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 "InstrProfiling.h"
10 #include "InstrProfilingInternal.h"
11 
12 #if defined(_WIN32)
13 
14 #if defined(_MSC_VER)
15 /* Merge read-write sections into .data. */
16 #pragma comment(linker, "/MERGE:.lprfb=.data")
17 #pragma comment(linker, "/MERGE:.lprfd=.data")
18 #pragma comment(linker, "/MERGE:.lprfv=.data")
19 #pragma comment(linker, "/MERGE:.lprfnd=.data")
20 /* Do *NOT* merge .lprfn and .lcovmap into .rdata. llvm-cov must be able to find
21  * after the fact.
22  * Do *NOT* merge .lprfc .rdata. When binary profile correlation is enabled,
23  * llvm-cov must be able to find after the fact.
24  */
25 
26 /* Allocate read-only section bounds. */
27 #pragma section(".lprfn$A", read)
28 #pragma section(".lprfn$Z", read)
29 
30 /* Allocate read-write section bounds. */
31 #pragma section(".lprfd$A", read, write)
32 #pragma section(".lprfd$Z", read, write)
33 #pragma section(".lprfc$A", read, write)
34 #pragma section(".lprfc$Z", read, write)
35 #pragma section(".lprfb$A", read, write)
36 #pragma section(".lprfb$Z", read, write)
37 #pragma section(".lorderfile$A", read, write)
38 #pragma section(".lprfnd$A", read, write)
39 #pragma section(".lprfnd$Z", read, write)
40 #endif
41 
42 __llvm_profile_data COMPILER_RT_SECTION(".lprfd$A") DataStart = {0};
43 __llvm_profile_data COMPILER_RT_SECTION(".lprfd$Z") DataEnd = {0};
44 
45 const char COMPILER_RT_SECTION(".lprfn$A") NamesStart = '\0';
46 const char COMPILER_RT_SECTION(".lprfn$Z") NamesEnd = '\0';
47 
48 char COMPILER_RT_SECTION(".lprfc$A") CountersStart;
49 char COMPILER_RT_SECTION(".lprfc$Z") CountersEnd;
50 char COMPILER_RT_SECTION(".lprfb$A") BitmapStart;
51 char COMPILER_RT_SECTION(".lprfb$Z") BitmapEnd;
52 uint32_t COMPILER_RT_SECTION(".lorderfile$A") OrderFileStart;
53 
54 ValueProfNode COMPILER_RT_SECTION(".lprfnd$A") VNodesStart;
55 ValueProfNode COMPILER_RT_SECTION(".lprfnd$Z") VNodesEnd;
56 
57 const __llvm_profile_data *__llvm_profile_begin_data(void) {
58   return &DataStart + 1;
59 }
60 const __llvm_profile_data *__llvm_profile_end_data(void) { return &DataEnd; }
61 
62 const char *__llvm_profile_begin_names(void) { return &NamesStart + 1; }
63 const char *__llvm_profile_end_names(void) { return &NamesEnd; }
64 
65 char *__llvm_profile_begin_counters(void) { return &CountersStart + 1; }
66 char *__llvm_profile_end_counters(void) { return &CountersEnd; }
67 char *__llvm_profile_begin_bitmap(void) { return &BitmapStart + 1; }
68 char *__llvm_profile_end_bitmap(void) { return &BitmapEnd; }
69 uint32_t *__llvm_profile_begin_orderfile(void) { return &OrderFileStart; }
70 
71 ValueProfNode *__llvm_profile_begin_vnodes(void) { return &VNodesStart + 1; }
72 ValueProfNode *__llvm_profile_end_vnodes(void) { return &VNodesEnd; }
73 
74 ValueProfNode *CurrentVNode = &VNodesStart + 1;
75 ValueProfNode *EndVNode = &VNodesEnd;
76 
77 /* lld-link provides __buildid symbol which ponits to the 16 bytes build id when
78  * using /build-id flag. https://lld.llvm.org/windows_support.html#lld-flags */
79 #define BUILD_ID_LEN 16
80 COMPILER_RT_WEAK extern uint8_t __buildid[BUILD_ID_LEN];
81 COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
82   if (*__buildid) {
83     if (Writer &&
84         lprofWriteOneBinaryId(Writer, BUILD_ID_LEN, __buildid, 0) == -1)
85       return -1;
86     return sizeof(uint64_t) + BUILD_ID_LEN;
87   }
88   return 0;
89 }
90 
91 #endif
92