1*68d75effSDimitry Andric //===-- sanitizer_coverage_win_sections.cpp -------------------------------===//
2*68d75effSDimitry Andric //
3*68d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*68d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*68d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*68d75effSDimitry Andric //
7*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
8*68d75effSDimitry Andric //
9*68d75effSDimitry Andric // This file defines delimiters for Sanitizer Coverage's section. It contains
10*68d75effSDimitry Andric // Windows specific tricks to coax the linker into giving us the start and stop
11*68d75effSDimitry Andric // addresses of a section, as ELF linkers can do, to get the size of certain
12*68d75effSDimitry Andric // arrays. According to https://msdn.microsoft.com/en-us/library/7977wcck.aspx
13*68d75effSDimitry Andric // sections with the same name before "$" are sorted alphabetically by the
14*68d75effSDimitry Andric // string that comes after "$" and merged into one section. We take advantage
15*68d75effSDimitry Andric // of this by putting data we want the size of into the middle (M) of a section,
16*68d75effSDimitry Andric // by using the letter "M" after "$". We get the start of this data (ie:
17*68d75effSDimitry Andric // __start_section_name) by making the start variable come at the start of the
18*68d75effSDimitry Andric // section (using the letter A after "$"). We do the same to get the end of the
19*68d75effSDimitry Andric // data by using the letter "Z" after "$" to make the end variable come after
20*68d75effSDimitry Andric // the data. Note that because of our technique the address of the start
21*68d75effSDimitry Andric // variable is actually the address of data that comes before our middle
22*68d75effSDimitry Andric // section. We also need to prevent the linker from adding any padding. Each
23*68d75effSDimitry Andric // technique we use for this is explained in the comments below.
24*68d75effSDimitry Andric //===----------------------------------------------------------------------===//
25*68d75effSDimitry Andric 
26*68d75effSDimitry Andric #include "sanitizer_platform.h"
27*68d75effSDimitry Andric #if SANITIZER_WINDOWS
28*68d75effSDimitry Andric #include <stdint.h>
29*68d75effSDimitry Andric 
30*68d75effSDimitry Andric extern "C" {
31*68d75effSDimitry Andric // Use uint64_t so the linker won't need to add any padding if it tries to word
32*68d75effSDimitry Andric // align the start of the 8-bit counters array. The array will always start 8
33*68d75effSDimitry Andric // bytes after __start_sancov_cntrs.
34*68d75effSDimitry Andric #pragma section(".SCOV$CA", read, write)
35*68d75effSDimitry Andric __declspec(allocate(".SCOV$CA")) uint64_t __start___sancov_cntrs = 0;
36*68d75effSDimitry Andric 
37*68d75effSDimitry Andric // Even though we said not to align __stop__sancov_cntrs (using the "align"
38*68d75effSDimitry Andric // declspec), MSVC's linker may try to align the section, .SCOV$CZ, containing
39*68d75effSDimitry Andric // it. This can cause a mismatch between the number of PCs and counters since
40*68d75effSDimitry Andric // each PCTable element is 8 bytes (unlike counters which are 1 byte) so no
41*68d75effSDimitry Andric // padding would be added to align .SCOVP$Z, However, if .SCOV$CZ section is 1
42*68d75effSDimitry Andric // byte, the linker won't try to align it on an 8-byte boundary, so use a
43*68d75effSDimitry Andric // uint8_t for __stop_sancov_cntrs.
44*68d75effSDimitry Andric #pragma section(".SCOV$CZ", read, write)
45*68d75effSDimitry Andric __declspec(allocate(".SCOV$CZ")) __declspec(align(1)) uint8_t
46*68d75effSDimitry Andric     __stop___sancov_cntrs = 0;
47*68d75effSDimitry Andric 
48*68d75effSDimitry Andric #pragma section(".SCOV$GA", read, write)
49*68d75effSDimitry Andric __declspec(allocate(".SCOV$GA")) uint64_t __start___sancov_guards = 0;
50*68d75effSDimitry Andric #pragma section(".SCOV$GZ", read, write)
51*68d75effSDimitry Andric __declspec(allocate(".SCOV$GZ")) __declspec(align(1)) uint8_t
52*68d75effSDimitry Andric     __stop___sancov_guards = 0;
53*68d75effSDimitry Andric 
54*68d75effSDimitry Andric // The guard array and counter array should both be merged into the .data
55*68d75effSDimitry Andric // section to reduce the number of PE sections. However, because PCTable is
56*68d75effSDimitry Andric // constant it should be merged with the .rdata section.
57*68d75effSDimitry Andric #pragma comment(linker, "/MERGE:.SCOV=.data")
58*68d75effSDimitry Andric 
59*68d75effSDimitry Andric #pragma section(".SCOVP$A", read)
60*68d75effSDimitry Andric __declspec(allocate(".SCOVP$A")) uint64_t __start___sancov_pcs = 0;
61*68d75effSDimitry Andric #pragma section(".SCOVP$Z", read)
62*68d75effSDimitry Andric __declspec(allocate(".SCOVP$Z")) __declspec(align(1)) uint8_t
63*68d75effSDimitry Andric     __stop___sancov_pcs = 0;
64*68d75effSDimitry Andric 
65*68d75effSDimitry Andric #pragma comment(linker, "/MERGE:.SCOVP=.rdata")
66*68d75effSDimitry Andric }
67*68d75effSDimitry Andric #endif  // SANITIZER_WINDOWS
68