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

Zahira Ammarguellat via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 20 07:45:52 PST 2022

zahiraam added a comment.

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.

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?



More information about the cfe-commits mailing list