Skip to content

<chrono>: Including header pulls in a dynamic initializer, incompatible with /kernel #1926

@kobykahane

Description

@kobykahane

Describe the bug
With Visual Studio 16.10 (Preview 3), just including <chrono> now pulls in a dynamic initializer for std::numpunct<char>::id and std::numpunct<wchar_t>::id, which was previously not the case in 16.9. Presumably this is the result of #1821 or #1870.
Since we use <chrono> as part of kernel mode driver projects, this now results in a linker warning:

repro.obj : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators

Since the kernel mode build environment does not have startup code that calls these initializers, this is problematic. For these specific initializers the warning is benign in the sense formatting-related chrono functionality cannot be consumed anyway. However, suppressing the warning globally (in our project) is also undesirable because it would allow developers to sneak in their own initializers which are never invoked.

It's clear to us that <chrono> isn't officially supported for consumption in the kernel mode build environment and isn't really a part of the freestanding subset. However, since this is a regression, it seems appropriate to bring it to your attention for consideration.

Command-line test case

C:\...\repro>type repro.cpp
#include <chrono>

extern "C" int atexit(void (__cdecl *func)(void));

int atexit(void (__cdecl *func)(void))
{
  return 0;
}

C:\...\repro>cl /EHsc /Z7 /std:c++latest /permissive- /GS- /LDd repro.cpp /link /noentry /nodefaultlib
Microsoft (R) Incremental Linker Version 14.29.30035.0
Copyright (C) Microsoft Corporation.  All rights reserved.
...
repro.obj : warning LNK4210: .CRT section exists; there may be unhandled static initializers or terminators

while with 16.9:

C:\...\repro>cl /EHsc /Z7 /std:c++latest /permissive- /GS- /LDd repro.cpp /link /noentry /nodefaultlib
Microsoft (R) C/C++ Optimizing Compiler Version 19.28.29915 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.
...
repro.cpp
...
repro.obj

with no warning.

Expected behavior
Ideally just including <chrono> without consuming any of the new formatting related functionality won't pull in any redundant dynamic initializers.

STL version

Microsoft Visual Studio Community 2019 Preview
Version 16.10.0 Preview 3.0

Additional context
The repro command lines above do not use /kernel or the kernel mode build environment, but simulate the same situation by not linking with the usual user mode startup code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingchronoC++20 chronofixedSomething works now, yay!

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions