Skip to content

MSVC: Configure test failure on non-AMD64 hosts with default VS 2015+ target architecture #4320

Closed

Description

@jcbrill

The generated c source file for test conf.CheckFunc('printf') in test\Configure\config-h.py produces a linker error due to an unresolved printf symbol for non-AMD64 Windows hosts using the default target architecture for VS2015 and later.

The crux of the problem is using printf without an explicit #include <stdio.h> for VS2015 and later with a target architecture that is not AMD64.

For VS2015 and later on non-AMD64 hosts and using the default target architecture, the config-h.py test fails due to printf:

STDOUT =========================================================================
2c2
< Checking for C function printf()... yes
---
> Checking for C function printf()... no

It appears that starting with VS2015, printf is only exported from the AMD64 libraries. The MSVC <stdio.h> header generates inline functions that invoke a printf compatible function that is exported from the architecture specific libraries. Without inclusion of <stdio.h>, the test will fail for VS2015+ when the target architecture is not AMD64.

Test environments:

  • Windows 10 x86 (VMWare) Host with VS2019 BuildTools (default target architecture is 'x86')
  • Windows 11 ARM64 Host with VS2022 Community installed (WIP branch: default target architecture is 'ARM64')

See below for additional details.

Activity

  1. jcbrill commented on Mar 8, 2023

    @jcbrill
    ContributorAuthor

    Research details, notes, and comments.

    MSVC Libraries with printf or _printf exported

    • The libraries searched below should be comprehensive when no compiler or linker options are specified.

      • VC6-VS2013: all architectures
      • VS2015-VS2022: AMD64 architectures only
    • dumpbin /LINKERMEMBER <library> (non-default installation paths):

      libcmt.txt
          _printf C:\Software\MSVS-1998-60-Pro\VC98\Lib\LIBCMT.LIB
          _printf C:\Software\MSVS-2002-70-Pro\Vc7\lib\libcmt.lib
          _printf C:\Software\MSVS-2003-71-Pro\Vc7\lib\libcmt.lib
          _printf C:\Software\MSVS-2005-80-Pro\VC\lib\libcmt.lib
           printf C:\Software\MSVS-2005-80-Pro\VC\lib\amd64\libcmt.lib
          _printf C:\Software\MSVS-2008-90-Pro\VC\lib\libcmt.lib
           printf C:\Software\MSVS-2008-90-Pro\VC\lib\amd64\libcmt.lib
           printf C:\Software\MSVS-2008-90-Pro\VC\lib\ia64\libcmt.lib
          _printf C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\libcmt.lib
           printf C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\amd64\libcmt.lib
           printf C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\lib\ia64\libcmt.lib
          _printf C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\libcmt.lib
           printf C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\amd64\libcmt.lib
           printf C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\lib\arm\libcmt.lib
          _printf C:\Software\MSVS-2013-120-Com\VC\lib\libcmt.lib
           printf C:\Software\MSVS-2013-120-Com\VC\lib\amd64\libcmt.lib
           printf C:\Software\MSVS-2013-120-Com\VC\lib\arm\libcmt.lib
           printf C:\Program Files (x86)\Microsoft SDKs\Windows\v5.0\Lib\IA64\libcmt.lib
           printf C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib\AMD64\libcmt.lib
           printf C:\Program Files\Microsoft Platform SDK for Windows Server 2003 R2\Lib\IA64\libcmt.lib
           printf C:\Program Files\Microsoft Platform SDK\Lib\AMD64\libcmt.lib
           printf C:\Program Files\Microsoft Platform SDK\Lib\IA64\libcmt.lib
          _printf C:\Program Files\Microsoft SDKs\Windows\v6.0\VC\LIB\libcmt.lib
           printf C:\Program Files\Microsoft SDKs\Windows\v6.0\VC\LIB\IA64\libcmt.lib
           printf C:\Program Files\Microsoft SDKs\Windows\v6.0\VC\LIB\x64\libcmt.lib
      oldnames.txt
      kernel32.txt
      runtimeobject.txt
      libvcruntime.txt
      libucrt.txt
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10150.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10240.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.10586.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.14393.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.15063.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.16299.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17134.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.17763.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.18362.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.19041.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.20348.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22000.0\ucrt\x64\libucrt.lib
           printf C:\Program Files (x86)\Windows Kits\10\Lib\10.0.22621.0\ucrt\x64\libucrt.lib
      uuid.txt
      

    Generated c source file for conf.CheckFunc('printf')

    • The generated c source file is:

      #include <assert.h>
      
      #ifdef __cplusplus
      extern "C"
      #endif
      char printf(void);
      
      #if _MSC_VER && !__INTEL_COMPILER
          #pragma function(printf)
      #endif
      
      int main(void) {
          #if defined (__stub_printf) || defined (__stub___printf)
              #error "printf has a GNU stub, cannot check"
          #else
              printf();
          #endif
          return 0;
      }
      

      VS2015+ with a non-AMD64 default target architecture results in linker error:

      • error LNK2019: unresolved external symbol _printf referenced in function _main

    Documentation: 2.1. Building Simple C / C++ Programs

    • Here's the famous "Hello, World!" program in C:

      int
      main()
      {
          printf("Hello, world!\n");
      }
      
    • And here's how to build it using SCons. Save the code above into hello.c, and enter the following into a file named SConstruct:

      Program('hello.c')
      

      The example fails to build for VS2015 and later if the default target architecture is not 'AMD64'. For example, VS2019 on a 32-bit Windows host.

    • Adding #include <stdio.h> will build with the default target architecture on non-AMD64 Windows hosts with VS2015 and later:

      #include <stdio.h>
      int
      main()
      {
          printf("Hello, world!\n");
      }
      

      The #include <stdio.h> causes the MSVC c preprocessor to generate inline functions for printf that dispatch to functions that are exported from the architecture-specific libraries.

  2. added
    MSVC
    on Apr 19, 2023
  3. jcbrill commented on Nov 22, 2023

    @jcbrill
    ContributorAuthor

    @mwichmann When you have some time, this one should not take too long to resolve one way or the other.

    The easiest solutions are to pick a function other than printf or explicitly include the stdio header (which may not be as easy in the test suite).

    With c/c++ headers using inline generation, renaming via the preprocessor, and the like, not explicitly including the associated header seems questionable. Fifteen years ago, probably not an issue.

    In this case, printf is supported when the header is included but mostly because it is remapped to a function that is exported.

  4. mwichmann commented on Nov 22, 2023

    @mwichmann
    Collaborator

    Example/test/configure code should be complete according to current language standards, not just "ancient C". We've fixed a bunch of those over time, including your #4413 above. So including a required header is not just fine, it's the right thing to do. Is it that simple? I see no problem with picking a different function to use either, I know in past lives I've run into some interesting problems with GCC which remaps some standard library names to a different name so you don't see the expected symbol in a generated binary (in fact, that scenario seems to be present in your research above with the test code punting on gcc)

  5. jcbrill commented on Nov 22, 2023

    @jcbrill
    ContributorAuthor

    So including a required header is not just fine, it's the right thing to do. Is it that simple?

    Unfortunately, No. It took a quick test to remember the issues.

    The root cause is has to do with the code generation for CheckFunc.

    Inlining for msvc-like compilers that define _MSC_VER is explicitly disabled via generation of a #pragma function() block.

    The MSVC #pragma function() documentation:

    Tells the compiler to generate calls to functions specified in the pragma's argument list, instead of inlining them.

    Case 1: the generated c source file without any modifications is (r1 = conf.CheckFunc('printf')):

    #include <assert.h>
    
    #if _MSC_VER && !__INTEL_COMPILER
         #pragma function(printf)
    #endif
    
    int main(void) {
    #if defined (__stub_printf) || defined (__stub___printf)
      #error "printf has a GNU stub, cannot check"
    #else
      printf();
    #endif
    
      return 0;
    }

    A warning is produced but linking is successful since the function is available for a 64-bit amd64 build on 64-bit windows:

    test.c(5): warning C4163: 'printf': not available as an intrinsic function
    

    Case 2: the generated c source file including the stdio header is (r1 = conf.CheckFunc('printf', header='#include <stdio.h>')):

    #include <assert.h>
    #include <stdio.h>
    
    #if _MSC_VER && !__INTEL_COMPILER
         #pragma function(printf)
    #endif
    
    int main(void) {
    #if defined (__stub_printf) || defined (__stub___printf)
      #error "printf has a GNU stub, cannot check"
    #else
      printf();
    #endif
    
      return 0;
    }

    A compiler warning and error is produced due to the usage of the printf function:

    test.c(5): warning C4163: 'printf': not available as an intrinsic function
    test.c(12): error C2198: 'printf': too few arguments for call
    

    Since a signature is not generated, including the header is not going to work unless the function being tested takes no arguments.

    Resolution options:

    • Comment out the printf test case
    • Find another standard library function that is exported in modern msvc runtime libraries on all platforms (64/amd64, 64/arm64, 32/x86).
    • Ignore the problem and quietly close the issue (nothing to see here...)

    Any thoughts?

  6. mwichmann commented on Nov 23, 2023

    @mwichmann
    Collaborator

    Always interesting when it's a test that fails. Test design is hard, and people get it wrong because they got lucky and it passed "by chance" (see the recent PCH discussion, where "order matters", but the existing test passed because the first choice happened to have the sources in an order that worked).

    If there's an easy choice for a standard C library function that doesn't trigger these problems, then great - but I wouldn't spend an age looking. Calling the function in a way that modern compilers don't object to would help (if you include the header to get the proto, then match the proto). I've been in exactly that place before - code generated automatically from a symbol, so you don't know the calling convention, you just want to know the symbol links ok. Compilers hadn't gotten as fussy circa the middle of the 2000 decade, so I don't think we had to solve it.

  7. jcbrill commented on Nov 23, 2023

    @jcbrill
    ContributorAuthor

    To be fair, I usually only run the MSVC/MSVS (~79 tests) tests on arm64. The configure test failed when running the entire suite (which takes forever on the low-end arm64 test box). I think I also tried in a 32-bit virtual machine and the test failed as well but I'm not positive.

    It strikes me as odd that the MS runtime library exports aren't the same for all of the platform versions of the libraries.

    I don't really understand why the intrinsics/inline methods are explicitly guarded against for MSVC. It appears that the configure test wants the function to be exported from the runtime or fail otherwise.

    I'm not exactly sure of the utility of the configure behavior as the "correct" usage of printf via including the header and an appropriate signature will work on any of the platforms. The fact that it is not exported from the runtime or necessarily implemented as the literal symbol printf seems less important than actually being able to use the printf symbol successfully.

  8. jcbrill commented on Nov 23, 2023

    @jcbrill
    ContributorAuthor

    @mwichmann Possible working solution.

    The two CheckFunc methods (SCons/SConf.py and SCons/Conftest.py) accept a header definition.

    From the CheckFunc documentation in SCons/Conftest.py:

    def CheckFunc(context, function_name, header = None, language = None):
        """
        Configure check for a function "function_name".
        "language" should be "C" or "C++" and is used to select the compiler.
        Default is "C".
        Optional "header" can be defined to define a function prototype, include a
        header file or anything else that comes before main().
        ...
        """
        ...

    Passing a header to provide a prototype for functions that take arguments can be an issue with "modern" compilers by default as the generated function invocation has an empty argument list.

    A working solution is to allow a function arguments string to be defined for the generated function invocation.

    CheckFunc in SCons/Conftest.py:

    def CheckFunc(context, function_name, header = None, language = None, funcargs = None):
        """
        Configure check for a function "function_name".
        "language" should be "C" or "C++" and is used to select the compiler.
        Default is "C".
        Optional "header" can be defined to define a function prototype, include a
        header file or anything else that comes before main().
        Optional "funcargs" can be defined to define an argument list for the
        generated function invocation.
        ...
        """
        ...
        if not funcargs:
            funcargs = ''
        ...
        
        text = """
    ...    
    #if defined (__stub_%(name)s) || defined (__stub___%(name)s)
      #error "%(name)s has a GNU stub, cannot check"
    #else
      %(name)s(%(args)s);
    #endif
    ...
    """ % { 'name': function_name,
            'include': includetext,
            'hdr': header,
            'args': funcargs}
        ...

    CheckFunc in SCons/SConf.py:

    def CheckFunc(context, function_name, header = None, language = None, funcargs = None) -> bool:
        res = SCons.Conftest.CheckFunc(context, function_name, header = header, language = language, funcargs = funcargs)
        context.did_show_result = 1
        return not res

    The printf check function call in test/Configure/config-h.py becomes:

    r1 = conf.CheckFunc('printf', header='#include <stdio.h>', funcargs='""')

    The annotated generated c source file is:

    #include <assert.h>
    #include <stdio.h>  // <---- header='#include <stdio.h>'
    
    #if _MSC_VER && !__INTEL_COMPILER
         #pragma function(printf)  // <---- function_name='printf'
    #endif
    
    int main(void) {
    #if defined (__stub_printf) || defined (__stub___printf)
      #error "printf has a GNU stub, cannot check"
    #else
      printf("");  // <---- function_name='printf', funcargs='""'
    #endif
    
      return 0;
    }

    The config-h.py test with the updated printf check function appears to pass on the following configurations:

    • Windows 10 64-bit AMD64
    • Windows 11 64-but ARM64
    • WIndows 10 32-bit x86
    • Ubuntu [WSL2] 64-bit

    If deemed worthy, a PR is easy enough.

    Happy Thanksgiving!
    Joe

  9. mwichmann commented on Nov 23, 2023

    @mwichmann
    Collaborator

    Sounds good to me... is something also needed for the call to memmove, which is in <string.h> and takes three arguments? Come to think of it, why are there two checks for "C library" functions? Sometimes it helps if test authors leave their thoughts behind for us late-comers :-) 🦃

  10. jcbrill commented on Nov 23, 2023

    @jcbrill
    ContributorAuthor

    is something also needed for the call to memmove, which is in <string.h> and takes three arguments?

    I don't think so.

    The check function test code generation without an explicit header definition is relying on the c compiler implicit prototype definition (i.e., a function returning int and assuming the number and types of arguments are correct) and that the linker will resolve the function name. The success of the linker is effectively the test result. The implicit function definition is "good-enough" as the function is not actually invoked via the generated executable.

    The original problem above, is that the msvc runtime libraries only export the "printf" symbol in the 64-bit amd library. Running the test on 32-bit windows or arm64 windows result in linker errors causing the tests to fail. If I remember correctly inline functions may have been generated/included (caveat emptor: could be disinformation) via the stdio header which resolves the symbol missing issue.

    Once the header is included the function invocation needs to match the prototype. Without the header, there is no type checking (assume correct) nor are there any inline definitions.

    memmove is likely either an intrinsic or exported explicitly in all runtime libraries leading to success.

    Come to think of it, why are there two checks for "C library" functions?

    Perhaps they are just simple/easy checks that don't require linking with libraries other than the default runtime?

    Otherwise, there would be a need to explicitly include other libraries for the linker which likely might be challenging across all supported platforms. Although, I suppose a library could be generated, compiled, and linked with the configure test.

    P.S.: I have real-world experience with the implicit function definition problem. The simulation tool with which we used extensive custom c library calls stopped producing c function prototypes and did not document the change between major versions. When testing the new version, our models were crashing for mysterious reasons. Digging through the generated c code, eventually it became obvious that (1) prototypes were no longer begin generated and (2) without generated prototypes the call stack was being interpreted incorrectly (e.g., int in an invocation where a float was expected in the function, etc). In our case, the number of arguments was always correct but the implicit conversion between int and float was allowed by both c and the simulation language. Ultimately, the solution was to generate the function prototypes at runtime by walking the simulation language's internal symbol table and comparing the generated temporary file to the active prototype file and swapping the files and failing with a "need to build again" message when the prototypes changed.

  11. mwichmann commented on Nov 23, 2023

    @mwichmann
    Collaborator

    Fair enough. I'll try to remember this - I have a substantial revamp queued for SConf/Conftest for more accurate API (and user) docs, some typing, etc. (there's a fair bit inconsistent, some of which I myself caused in past efforts to untangle it, not completely understanding everything). Anyway - I should maybe add something about the use of a header and funcargs other than just a bare statement that they exist. Along the lines of (wording not considered): If you include a header which provides a prototype declaration for the function being checked, the compiler may perform a more rigorous type check to match the prototype. If this causes unexpected failures, use funcargs to supply arguments to match the prototype.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    MSVC

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions