[PATCH] D117569: Constexpr not supported with __declspec(dllimport).

David Majnemer via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 20 11:45:23 PST 2022

majnemer added a comment.

In D117569#3258307 <https://reviews.llvm.org/D117569#3258307>, @zahiraam wrote:

> In D117569#3257121 <https://reviews.llvm.org/D117569#3257121>, @majnemer wrote:
>> I have a question regarding how this work with respect to the dllimport semantics known by the linker.
>> IIUC, we will now allow a program like:
>>   extern int __declspec(dllimport) dll_import_int;
>>   constexpr int& dll_import_constexpr_ref = dll_import_int;
>>   int& get() {
>>       return dll_import_constexpr_ref;
>>   }
>> Here, `get` will load `dll_import_constexpr_ref`. However, what will `dll_import_constexpr_ref` hold? It ought to hold the contents of `__imp_?dll_import_int@@3HA`. However, we can't dereference `__imp_?dll_import_int@@3HA` to get to its contents.
> @majnemer Thanks for the review.
> This test case doesn't link with MSVC. It will generate this error:
> Microsoft (R) Incremental Linker Version 14.29.30133.0
> Copyright (C) Microsoft Corporation.  All rights reserved.
> /out:test1.exe
> test1.obj
> test1.obj : error LNK2001: unresolved external symbol "int dll_import_int" (?dll_import_int@@3HA)
> test1.exe : fatal error LNK1120: 1 unresolved externals
> The symbols generated with MSVC are:
> 07 00000000 UNDEF  notype       External     | ?dll_import_int@@3HA (int dll_import_int)
> 015 00000000 SECT6  notype       Static       | ?dll_import_constexpr_ref@@3AEAHEA (int & dll_import_constexpr_ref)
> Without this patch this test case errors. With this patch clang will generate these symbols:
> 010 00000000 UNDEF  notype       External     | __imp_?dll_import_int@@3HA (__declspec(dllimport) int dll_import_int)
> 012 00000000 SECT5  notype       External     | ?dll_import_constexpr_ref@@3AEAHEA (int & dll_import_constexpr_ref)
> 013 00000000 UNDEF  notype       External     | ?dll_import_int@@3HA (int dll_import_int)
> and will error at link time with this error:
> test1-f1f63b.o : error LNK2019: unresolved external symbol "__declspec(dllimport) int dll_import_int" (__imp_?dll_import_int@@3HA) referenced in function "int & __cdecl get(void)" (?get@@YAAEAHXZ)
> test1-f1f63b.o : error LNK2001: unresolved external symbol "int dll_import_int" (?dll_import_int@@3HA)
> a.exe : fatal error LNK1120: 2 unresolved externals
> I think that's the behavior expected, right?

My interpretation is that MSVC has a bug: it is forming a reference to `?dll_import_int@@3HA` and not `__imp_?dll_import_int@@3HA`. Could you run a more complete experiment where you have a dll which exports the symbol and you try to import it?



More information about the cfe-commits mailing list