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(".lprfnd$A", read, write)
40#pragma section(".lprfnd$Z", read, write)
41#endif
42
43/* Pin the section-boundary sentinels to the same alignment as the records the
44 * compiler emits into .lprfd$M (INSTR_PROF_DATA_ALIGNMENT). Without this the
45 * AMD64 preferred-alignment heuristic over-aligns these >=16-byte globals to 16
46 * bytes; when sizeof(__llvm_profile_data) is not a multiple of 16 the 16-byte
47 * $Z sentinel leaves a gap after the 8-byte-aligned $M records, which
48 * __llvm_profile_get_num_data rounds up into a phantom data record. */
49COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
50__llvm_profile_data COMPILER_RT_SECTION(".lprfd$A") DataStart = {0};
51COMPILER_RT_ALIGNAS(INSTR_PROF_DATA_ALIGNMENT)
52__llvm_profile_data COMPILER_RT_SECTION(".lprfd$Z") DataEnd = {0};
53
54const char COMPILER_RT_SECTION(".lprfn$A") NamesStart = '\0';
55const char COMPILER_RT_SECTION(".lprfn$Z") NamesEnd = '\0';
56
57char COMPILER_RT_SECTION(".lprfc$A") CountersStart;
58char COMPILER_RT_SECTION(".lprfc$Z") CountersEnd;
59char COMPILER_RT_SECTION(".lprfb$A") BitmapStart;
60char COMPILER_RT_SECTION(".lprfb$Z") BitmapEnd;
61
62ValueProfNode COMPILER_RT_SECTION(".lprfnd$A") VNodesStart;
63ValueProfNode COMPILER_RT_SECTION(".lprfnd$Z") VNodesEnd;
64
65const __llvm_profile_data *__llvm_profile_begin_data(void) {
66 return &DataStart + 1;
67}
68const __llvm_profile_data *__llvm_profile_end_data(void) { return &DataEnd; }
69
70// Type profiling isn't implemented under MSVC ABI, so return NULL (rather than
71// implementing linker magic on Windows) to make it more explicit. To elaborate,
72// the current type profiling implementation maps a profiled vtable address to a
73// vtable variable through vtables mangled name. Under MSVC ABI, the variable
74// name for vtables might not be the mangled name (see
75// MicrosoftCXXABI::getAddrOfVTable in MicrosoftCXXABI.cpp for more details on
76// how a vtable name is computed). Note the mangled name is still in the vtable
77// IR (just not variable name) for mapping purpose, but more implementation work
78// is required.
79const VTableProfData *__llvm_profile_begin_vtables(void) { return NULL; }
80const VTableProfData *__llvm_profile_end_vtables(void) { return NULL; }
81
82const char *__llvm_profile_begin_names(void) { return &NamesStart + 1; }
83const char *__llvm_profile_end_names(void) { return &NamesEnd; }
84
85// Type profiling isn't supported on Windows, so return NULl to make it more
86// explicit.
87const char *__llvm_profile_begin_vtabnames(void) { return NULL; }
88const char *__llvm_profile_end_vtabnames(void) { return NULL; }
89
90char *__llvm_profile_begin_counters(void) { return &CountersStart + 1; }
91char *__llvm_profile_end_counters(void) { return &CountersEnd; }
92char *__llvm_profile_begin_bitmap(void) { return &BitmapStart + 1; }
93char *__llvm_profile_end_bitmap(void) { return &BitmapEnd; }
94
95ValueProfNode *__llvm_profile_begin_vnodes(void) { return &VNodesStart + 1; }
96ValueProfNode *__llvm_profile_end_vnodes(void) { return &VNodesEnd; }
97
98ValueProfNode *CurrentVNode = &VNodesStart + 1;
99ValueProfNode *EndVNode = &VNodesEnd;
100
101/* lld-link provides __buildid symbol which points to the 16 bytes build id when
102 * using /build-id flag. https://lld.llvm.org/windows_support.html#lld-flags */
103#define BUILD_ID_LEN 16
104COMPILER_RT_WEAK uint8_t __buildid[BUILD_ID_LEN] = {0};
105COMPILER_RT_VISIBILITY int __llvm_write_binary_ids(ProfDataWriter *Writer) {
106 static const uint8_t zeros[BUILD_ID_LEN] = {0};
107 if (memcmp(__buildid, zeros, BUILD_ID_LEN) != 0) {
108 if (Writer &&
109 lprofWriteOneBinaryId(Writer, BUILD_ID_LEN, __buildid, 0) == -1)
110 return -1;
111 return sizeof(uint64_t) + BUILD_ID_LEN;
112 }
113 return 0;
114}
115
116#endif
117