Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an alias object to the import lib #2734

Merged
merged 16 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 20 additions & 12 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ cmake_minimum_required(VERSION 3.22)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
project(msvc_standard_libraries LANGUAGES CXX)

if(NOT DEFINED VCLIBS_TARGET_ARCHITECTURE)
set(VCLIBS_TARGET_ARCHITECTURE "${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
endif()
string(TOLOWER "${VCLIBS_TARGET_ARCHITECTURE}" VCLIBS_TARGET_ARCHITECTURE)
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

if(VCLIBS_TARGET_ARCHITECTURE MATCHES "^x64$|^x86$")
enable_language(ASM_MASM)
endif()

# add the tools subdirectory _before_ we change all the flags
add_subdirectory(tools EXCLUDE_FROM_ALL)
# these allow the targets to show up in the top-level
Expand All @@ -21,37 +30,33 @@ endif()
option(BUILD_TESTING "Enable testing" ON)
set(VCLIBS_SUFFIX "_oss" CACHE STRING "suffix for built DLL names to avoid conflicts with distributed DLLs")

if(NOT DEFINED VCLIBS_TARGET_ARCHITECTURE)
set(VCLIBS_TARGET_ARCHITECTURE "${CMAKE_CXX_COMPILER_ARCHITECTURE_ID}")
endif()

set(CMAKE_CXX_FLAGS "")
set(CMAKE_CXX_FLAGS_DEBUG "")
set(CMAKE_CXX_FLAGS_RELEASE "")
set(CMAKE_CXX_STANDARD_LIBRARIES "kernel32.lib")
set(CMAKE_CXX_STANDARD_LIBRARIES_INIT "kernel32.lib")
set(CMAKE_MSVC_RUNTIME_LIBRARY "")

if("${VCLIBS_TARGET_ARCHITECTURE}" MATCHES "^[xX]86$")
if("${VCLIBS_TARGET_ARCHITECTURE}" MATCHES "^x86$")
set(VCLIBS_TARGET_ARCHITECTURE "x86")
set(VCLIBS_I386_OR_AMD64 "i386")
set(VCLIBS_X86_OR_X64 "x86")
# Note that we set _WIN32_WINNT to a high level to make declarations available, but still engage downlevel
# runtime dynamic linking by setting our own _STL_WIN32_WINNT back to Windows XP.
add_compile_definitions(_X86_ _VCRT_WIN32_WINNT=0x0501 _STL_WIN32_WINNT=0x0501)
add_compile_options(/arch:IA32)
elseif(VCLIBS_TARGET_ARCHITECTURE MATCHES "^[xX]64$")
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:IA32>)
elseif(VCLIBS_TARGET_ARCHITECTURE MATCHES "^x64$")
set(VCLIBS_TARGET_ARCHITECTURE "x64")
set(VCLIBS_I386_OR_AMD64 "amd64")
set(VCLIBS_X86_OR_X64 "x64")
add_compile_definitions(_AMD64_ _VCRT_WIN32_WINNT=0x0501 _STL_WIN32_WINNT=0x0501)
elseif(VCLIBS_TARGET_ARCHITECTURE MATCHES "^[aA][rR][mM][vV]7$")
elseif(VCLIBS_TARGET_ARCHITECTURE MATCHES "^armv7$")
set(VCLIBS_TARGET_ARCHITECTURE "arm")
set(VCLIBS_I386_OR_AMD64 "arm")
set(VCLIBS_X86_OR_X64 "arm")
add_compile_definitions(_ARM_ _VCRT_WIN32_WINNT=0x0602 _STL_WIN32_WINNT=0x0602)
string(APPEND CMAKE_CXX_STANDARD_LIBRARIES " Synchronization.lib")
elseif(VCLIBS_TARGET_ARCHITECTURE MATCHES "^[aA][rR][mM]64$")
elseif(VCLIBS_TARGET_ARCHITECTURE MATCHES "^arm64$")
set(VCLIBS_TARGET_ARCHITECTURE "arm64")
set(VCLIBS_I386_OR_AMD64 "arm64")
set(VCLIBS_X86_OR_X64 "arm64")
Expand All @@ -65,9 +70,12 @@ add_compile_definitions(
_ALLOW_ITERATOR_DEBUG_LEVEL_MISMATCH WIN32_LEAN_AND_MEAN STRICT _CRT_STDIO_ARBITRARY_WIDE_SPECIFIERS
_CRT_DECLARE_NONSTDC_NAMES=1 _WIN32_WINNT=0x0A00 NTDDI_VERSION=NTDDI_WIN10_CO)

add_compile_options(/diagnostics:caret /W4 /WX /w14265 /w15038 /d1FastFail /guard:cf /Z7 /Gy /Zp8 /std:c++latest /permissive- /Zc:threadSafeInit- /Zl)
add_compile_options(/WX /Gy
"$<$<COMPILE_LANGUAGE:CXX>:/diagnostics:caret;/W4;/w14265;/w15038;/d1FastFail;/guard:cf;/Z7;/Zp8;/std:c++latest;/permissive-;/Zc:threadSafeInit-;/Zl>"
# note that /Zi generates debug info inside the object file, it's the same as /Z7 for msvc
"$<$<COMPILE_LANGUAGE:ASM_MASM>:/Zi;/W3;/nologo>")

set(VCLIBS_DEBUG_OPTIONS "/Od")
set(VCLIBS_DEBUG_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:/Od>")

# TRANSITION: Potentially remove `/Os` if it results in speed improvements.
# This requires benchmarking!
Expand All @@ -76,7 +84,7 @@ set(VCLIBS_DEBUG_OPTIONS "/Od")
# in the release DLL (a gain of 18.4%).
# So, make certain that that tradeoff is considered when or if `/Os` is removed.
# See GH-2108 for more info.
set(VCLIBS_RELEASE_OPTIONS /O2 /Os)
set(VCLIBS_RELEASE_OPTIONS "$<$<COMPILE_LANGUAGE:CXX>:/O2;/Os>")

set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/out/lib/${VCLIBS_I386_OR_AMD64}")
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY "${PROJECT_BINARY_DIR}/out/lib/${VCLIBS_I386_OR_AMD64}")
Expand Down
5 changes: 3 additions & 2 deletions stl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ set(IMPLIB_SOURCES
${CMAKE_CURRENT_LIST_DIR}/src/syserror_import_lib.cpp
${CMAKE_CURRENT_LIST_DIR}/src/vector_algorithms.cpp
${CMAKE_CURRENT_LIST_DIR}/src/xonce2.cpp
${CMAKE_CURRENT_LIST_DIR}/src/alias.asm
barcharcraz marked this conversation as resolved.
Show resolved Hide resolved
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
)

# The following files are linked in msvcp140[d][_clr].dll.
Expand Down Expand Up @@ -467,7 +468,7 @@ function(add_stl_dlls D_SUFFIX THIS_CONFIG_DEFINITIONS THIS_CONFIG_COMPILE_OPTIO
# import library 'statics'
add_library(msvcp${D_SUFFIX}_implib_objects OBJECT ${IMPLIB_SOURCES})
target_compile_definitions(msvcp${D_SUFFIX}_implib_objects PRIVATE "_DLL;${THIS_CONFIG_DEFINITIONS}")
target_compile_options(msvcp${D_SUFFIX}_implib_objects PRIVATE "${THIS_CONFIG_COMPILE_OPTIONS};/EHsc") # No /GL!
target_compile_options(msvcp${D_SUFFIX}_implib_objects PRIVATE "${THIS_CONFIG_COMPILE_OPTIONS};$<$<COMPILE_LANGUAGE:CXX>:/EHsc>") # No /GL!

add_library(msvcp${D_SUFFIX}_satellite_objects OBJECT ${SATELLITE_DLL_SOURCES})
target_compile_options(msvcp${D_SUFFIX}_satellite_objects PRIVATE "${THIS_CONFIG_COMPILE_OPTIONS};${GL_FLAG};/EHsc")
Expand Down Expand Up @@ -550,7 +551,7 @@ function(add_stl_statics FLAVOR_SUFFIX THIS_CONFIG_DEFINITIONS THIS_CONFIG_COMPI

add_library(libcpmt${FLAVOR_SUFFIX} STATIC ${HEADERS} ${IMPLIB_SOURCES} ${SOURCES} ${INITIALIZER_SOURCES} ${STATIC_SOURCES})
target_compile_definitions(libcpmt${FLAVOR_SUFFIX} PRIVATE "${THIS_CONFIG_DEFINITIONS};_ANNOTATE_VECTOR;_ANNOTATE_STRING")
target_compile_options(libcpmt${FLAVOR_SUFFIX} PRIVATE "${THIS_CONFIG_COMPILE_OPTIONS};/EHsc")
target_compile_options(libcpmt${FLAVOR_SUFFIX} PRIVATE "${THIS_CONFIG_COMPILE_OPTIONS};$<$<COMPILE_LANGUAGE:CXX>:/EHsc>")
target_link_libraries(libcpmt${FLAVOR_SUFFIX} PRIVATE Boost::math libcpmt${FLAVOR_SUFFIX}_eha)
endfunction()

Expand Down
56 changes: 56 additions & 0 deletions stl/src/alias.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
; weak symbol aliases

; Copyright (c) Microsoft Corporation.
; SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved

; Note that while this is a masm file, it contains no code
; masm does generate a .text section with zero size

; mangle identifiers for stdcall calling convention
; on x86_64 this does nothing, on x86 it adds a leading
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
; underscore and @sz, sz should be the size of the parameters
mangle MACRO name, sz
; This checks if we're in x86_64 mode (using ml64)
; note that RAX isn't really an assembler symbol,
; and this check only works in the IFDEF direction
; That is:
; IFDEF RAX
; meow
; ENDIF
; will expand to meow on only x64 system, however:
; IFNDEF RAX
; meow
; ENDIF
; will expand to meow on _BOTH_ x64 and x86 systems.
IFDEF RAX
EXITM <name>
ELSE
EXITM <_&name&@&sz&>
ENDIF
ENDM

; generate the name of the imp symbol for a function
imp_name MACRO name, sz
EXITM @CatStr(__imp_, mangle(name, sz))
ENDM

create_alias MACRO oldname, newname, size
; use EXTERN name : PROC instead of
; EXTERN name : PROTO ...
; to avoid masm adding underscores to the names
; of (object) symbols.
; Note that the MASM symbol always has the name that appears in
; the source file (after macro expansion), but when _using_ the symbol
; masm will add underscores
EXTERN imp_name(newname, size) : PROC
; the documentation for "ALIAS <alias> = <actual-name>" says angle brackets
; are required around the alias, but this is not the case, it seems to just require
; a single "text item", which angle brackets create, but which is also the result
; of expanding these macros
ALIAS imp_name(oldname, size)=imp_name(newname,size)
barcharcraz marked this conversation as resolved.
Show resolved Hide resolved
ENDM

create_alias __std_init_once_begin_initialize, InitOnceBeginInitialize, 16
create_alias __std_init_once_complete, InitOnceComplete, 12

END
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Copyright (c) Microsoft Corporation.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

RUNALL_INCLUDE ..\prefix.lst
RUNALL_CROSSLIST
PM_CL="/MD"
PM_CL="/MDd"
PM_CL="/MT"
PM_CL="/MTd"
19 changes: 19 additions & 0 deletions tests/std/tests/GH_002655_alternate_name_broke_linker/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
#include <yvals.h>

#if defined(_M_IX86) || defined(_M_X64)
extern "C" int __declspec(dllimport) __stdcall __std_init_once_begin_initialize(void**, unsigned long, int*, void**);
extern "C" int __declspec(dllimport) __stdcall __std_init_once_complete(void**, unsigned long, void*);
barcharcraz marked this conversation as resolved.
Show resolved Hide resolved
#endif
barcharcraz marked this conversation as resolved.
Show resolved Hide resolved

int main() {
#if defined(_M_IX86) || defined(_M_X64)
void* once = 0;
int pending = 0;
__std_init_once_begin_initialize(&once, 0, &pending, 0);
__std_init_once_complete(&once, 0, 0);
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
#endif
barcharcraz marked this conversation as resolved.
Show resolved Hide resolved
return 0;
}