165492d95SNico Weber //===-- sanitizer_coverage_win_sections.cpp -------------------------------===// 265492d95SNico Weber // 365492d95SNico Weber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 465492d95SNico Weber // See https://llvm.org/LICENSE.txt for license information. 565492d95SNico Weber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 665492d95SNico Weber // 765492d95SNico Weber //===----------------------------------------------------------------------===// 865492d95SNico Weber // 965492d95SNico Weber // This file defines delimiters for Sanitizer Coverage's section. It contains 1065492d95SNico Weber // Windows specific tricks to coax the linker into giving us the start and stop 1165492d95SNico Weber // addresses of a section, as ELF linkers can do, to get the size of certain 1265492d95SNico Weber // arrays. According to https://msdn.microsoft.com/en-us/library/7977wcck.aspx 1365492d95SNico Weber // sections with the same name before "$" are sorted alphabetically by the 1465492d95SNico Weber // string that comes after "$" and merged into one section. We take advantage 1565492d95SNico Weber // of this by putting data we want the size of into the middle (M) of a section, 1665492d95SNico Weber // by using the letter "M" after "$". We get the start of this data (ie: 1765492d95SNico Weber // __start_section_name) by making the start variable come at the start of the 1865492d95SNico Weber // section (using the letter A after "$"). We do the same to get the end of the 1965492d95SNico Weber // data by using the letter "Z" after "$" to make the end variable come after 2065492d95SNico Weber // the data. Note that because of our technique the address of the start 2165492d95SNico Weber // variable is actually the address of data that comes before our middle 2265492d95SNico Weber // section. We also need to prevent the linker from adding any padding. Each 2365492d95SNico Weber // technique we use for this is explained in the comments below. 2465492d95SNico Weber //===----------------------------------------------------------------------===// 2565492d95SNico Weber 2665492d95SNico Weber #include "sanitizer_platform.h" 2765492d95SNico Weber #if SANITIZER_WINDOWS 2865492d95SNico Weber #include <stdint.h> 2965492d95SNico Weber 3065492d95SNico Weber extern "C" { 3165492d95SNico Weber // Use uint64_t so the linker won't need to add any padding if it tries to word 3265492d95SNico Weber // align the start of the 8-bit counters array. The array will always start 8 3365492d95SNico Weber // bytes after __start_sancov_cntrs. 34*c0fa6322SVitaly Buka #pragma section(".SCOV$CA", read, write) 3565492d95SNico Weber __declspec(allocate(".SCOV$CA")) uint64_t __start___sancov_cntrs = 0; 3665492d95SNico Weber 3765492d95SNico Weber // Even though we said not to align __stop__sancov_cntrs (using the "align" 3865492d95SNico Weber // declspec), MSVC's linker may try to align the section, .SCOV$CZ, containing 3965492d95SNico Weber // it. This can cause a mismatch between the number of PCs and counters since 4065492d95SNico Weber // each PCTable element is 8 bytes (unlike counters which are 1 byte) so no 4165492d95SNico Weber // padding would be added to align .SCOVP$Z, However, if .SCOV$CZ section is 1 4265492d95SNico Weber // byte, the linker won't try to align it on an 8-byte boundary, so use a 4365492d95SNico Weber // uint8_t for __stop_sancov_cntrs. 44*c0fa6322SVitaly Buka #pragma section(".SCOV$CZ", read, write) 4565492d95SNico Weber __declspec(allocate(".SCOV$CZ")) __declspec(align(1)) uint8_t 4665492d95SNico Weber __stop___sancov_cntrs = 0; 4765492d95SNico Weber 48*c0fa6322SVitaly Buka #pragma section(".SCOV$GA", read, write) 4965492d95SNico Weber __declspec(allocate(".SCOV$GA")) uint64_t __start___sancov_guards = 0; 50*c0fa6322SVitaly Buka #pragma section(".SCOV$GZ", read, write) 5165492d95SNico Weber __declspec(allocate(".SCOV$GZ")) __declspec(align(1)) uint8_t 5265492d95SNico Weber __stop___sancov_guards = 0; 5365492d95SNico Weber 5465492d95SNico Weber // The guard array and counter array should both be merged into the .data 5565492d95SNico Weber // section to reduce the number of PE sections. However, because PCTable is 5665492d95SNico Weber // constant it should be merged with the .rdata section. 5765492d95SNico Weber #pragma comment(linker, "/MERGE:.SCOV=.data") 5865492d95SNico Weber 59*c0fa6322SVitaly Buka #pragma section(".SCOVP$A", read) 6065492d95SNico Weber __declspec(allocate(".SCOVP$A")) uint64_t __start___sancov_pcs = 0; 61*c0fa6322SVitaly Buka #pragma section(".SCOVP$Z", read) 6265492d95SNico Weber __declspec(allocate(".SCOVP$Z")) __declspec(align(1)) uint8_t 6365492d95SNico Weber __stop___sancov_pcs = 0; 6465492d95SNico Weber 6565492d95SNico Weber #pragma comment(linker, "/MERGE:.SCOVP=.rdata") 6665492d95SNico Weber } 6765492d95SNico Weber #endif // SANITIZER_WINDOWS 68