From 4730604bd3a361c68b92b18bf73a5daa15afe9f4 Mon Sep 17 00:00:00 2001 From: Reid Kleckner Date: Thu, 25 Apr 2019 23:04:20 +0000 Subject: [PATCH] [COFF] Statically link certain runtime library functions Statically link certain runtime library functions for MSVC/GNU Windows environments. This is consistent with MSVC behavior. Fixes LNK4286 and LNK4217 warnings from link.exe when linking the static CRT: LINK : warning LNK4286: symbol '__std_terminate' defined in 'libvcruntime.lib(ehhelpers.obj)' is imported by 'ASAN_NOINST_TEST_OBJECTS.asan_noinst_test.cc.x86_64-calls.o' LINK : warning LNK4286: symbol '__std_terminate' defined in 'libvcruntime.lib(ehhelpers.obj)' is imported by 'ASAN_NOINST_TEST_OBJECTS.asan_test_main.cc.x86_64-calls.o' LINK : warning LNK4217: symbol '_CxxThrowException' defined in 'libvcruntime.lib(throw.obj)' is imported by 'ASAN_NOINST_TEST_OBJECTS.gtest-all.cc.x86_64-calls.o' in function '"int `public: static class UnitTest::GetInstance * __cdecl testing::UnitTest::GetInstance(void)'::`1'::dtor$5" (?dtor$5@?0??GetInstance@UnitTest@testing@@SAPEAV12@XZ@4HA)' Reviewers: mstorsjo, efriedma, TomTan, compnerd, smeenai, mgrang Subscribers: abdulras, theraven, smeenai, pcc, mehdi_amini, javed.absar, inglorion, kristof.beyls, dexonsmith, cfe-commits Differential Revision: https://reviews.llvm.org/D55229 llvm-svn: 359250 --- clang/lib/CodeGen/CodeGenModule.cpp | 10 +++++++--- clang/test/CodeGen/ms-symbol-linkage.cpp | 20 ++++++++++++++++++++ clang/test/CodeGenCXX/runtime-dllstorage.cpp | 2 +- clang/test/CodeGenObjC/gnu-init.m | 4 ++-- clang/test/CodeGenObjCXX/msabi-stret.mm | 3 +-- 5 files changed, 31 insertions(+), 8 deletions(-) create mode 100644 clang/test/CodeGen/ms-symbol-linkage.cpp diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index e51963aa9afc..b490fa0faf2e 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -3000,9 +3000,13 @@ CodeGenModule::CreateRuntimeFunction(llvm::FunctionType *FTy, StringRef Name, if (F->empty()) { F->setCallingConv(getRuntimeCC()); - if (!Local && getTriple().isOSBinFormatCOFF() && - !getCodeGenOpts().LTOVisibilityPublicStd && - !getTriple().isWindowsGNUEnvironment()) { + // In Windows Itanium environments, try to mark runtime functions + // dllimport. For Mingw and MSVC, don't. We don't really know if the user + // will link their standard library statically or dynamically. Marking + // functions imported when they are not imported can cause linker errors + // and warnings. + if (!Local && getTriple().isWindowsItaniumEnvironment() && + !getCodeGenOpts().LTOVisibilityPublicStd) { const FunctionDecl *FD = GetRuntimeFunctionDecl(Context, Name); if (!FD || FD->hasAttr()) { F->setDLLStorageClass(llvm::GlobalValue::DLLImportStorageClass); diff --git a/clang/test/CodeGen/ms-symbol-linkage.cpp b/clang/test/CodeGen/ms-symbol-linkage.cpp new file mode 100644 index 000000000000..3674ad0debf8 --- /dev/null +++ b/clang/test/CodeGen/ms-symbol-linkage.cpp @@ -0,0 +1,20 @@ +// RUN: %clangxx -target aarch64-windows \ +// RUN: -fcxx-exceptions -c -o - %s \ +// RUN: | llvm-objdump -syms - 2>&1 | FileCheck %s + +void foo1() { throw 1; } +// CHECK-LABEL: foo1 +// CHECK-NOT: __imp__CxxThrowException + +void bar(); +void foo2() noexcept(true) { bar(); } +// CHECK-LABEL: foo2 +// CHECK-NOT: __imp___std_terminate + +struct A {}; +struct B { virtual void f(); }; +struct C : A, virtual B {}; +struct T {}; +T *foo3() { return dynamic_cast((C *)0); } +// CHECK-LABEL: foo3 +// CHECK-NOT: __imp___RTDynamicCast diff --git a/clang/test/CodeGenCXX/runtime-dllstorage.cpp b/clang/test/CodeGenCXX/runtime-dllstorage.cpp index 7220fa50b0ee..3c0c73f9aa39 100644 --- a/clang/test/CodeGenCXX/runtime-dllstorage.cpp +++ b/clang/test/CodeGenCXX/runtime-dllstorage.cpp @@ -108,7 +108,7 @@ void l() { // CHECK-MS-DAG: @_Init_thread_epoch = external thread_local global i32 // CHECK-MS-DAG: declare dso_local i32 @__tlregdtor(void ()*) // CHECK-MS-DAG: declare dso_local i32 @atexit(void ()*) -// CHECK-MS-DYNAMIC-DAG: declare dllimport {{.*}} void @_CxxThrowException +// CHECK-MS-DYNAMIC-DAG: declare {{.*}} void @_CxxThrowException // CHECK-MS-STATIC-DAG: declare {{.*}} void @_CxxThrowException // CHECK-MS-DAG: declare dso_local noalias i8* @"??2@YAPAXI@Z" // CHECK-MS-DAG: declare dso_local void @_Init_thread_header(i32*) diff --git a/clang/test/CodeGenObjC/gnu-init.m b/clang/test/CodeGenObjC/gnu-init.m index 32db49fdf90d..6d562b80cb91 100644 --- a/clang/test/CodeGenObjC/gnu-init.m +++ b/clang/test/CodeGenObjC/gnu-init.m @@ -100,6 +100,6 @@ // Check our load function is in a comdat. // CHECK-WIN: define linkonce_odr hidden void @.objcv2_load_function() comdat { -// Make sure we have dllimport on the load function -// CHECK-WIN: declare dllimport void @__objc_load +// Make sure we do not have dllimport on the load function +// CHECK-WIN: declare dso_local void @__objc_load diff --git a/clang/test/CodeGenObjCXX/msabi-stret.mm b/clang/test/CodeGenObjCXX/msabi-stret.mm index 765c23887bac..66e407af27e0 100644 --- a/clang/test/CodeGenObjCXX/msabi-stret.mm +++ b/clang/test/CodeGenObjCXX/msabi-stret.mm @@ -13,6 +13,5 @@ S f() { return [I m:S()]; } -// CHECK: declare dllimport void @objc_msgSend_stret(i8*, i8*, ...) +// CHECK: declare dso_local void @objc_msgSend_stret(i8*, i8*, ...) // CHECK-NOT: declare dllimport void @objc_msgSend(i8*, i8*, ...) -