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

Zahira Ammarguellat via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jan 20 15:13:16 PST 2022


zahiraam added a comment.

In D117569#3259255 <https://reviews.llvm.org/D117569#3259255>, @majnemer wrote:

> 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?

This is what I did (before the complete experiment). 
For this test case:

int& get() {

  extern int __declspec(dllimport) dll_import_int;
  constexpr int& dll_import_constexpr_ref = dll_import_int;
  
  return dll_import_constexpr_ref;

}

int main() {

  get();
  
  return 0;

}
This is generating this symbol (with imp):
ksh-3.2$ dumpbin /symbols test2.obj  | grep dll
015 00000000 UNDEF  notype       External     | **__imp**_?dll_import_int@@3HA (__declspec(dllimport) int dll_import_int)
ksh-3.2$

With clang (with this patch) it is generating the same symbol:
ksh-3.2$ dumpbin /symbols test2.o  | grep dll
00E 00000000 UNDEF  notype       External     | __imp_?dll_import_int@@3HA (__declspec(dllimport) int dll_import_int)
ksh-3.2$

For this test case:
extern int __declspec(dllexport) dll_export_int;
constexpr int& dll_export_constexpr_ref = dll_export_int;

int& get() {

  extern int __declspec(dllimport) dll_import_int;
  constexpr int& dll_import_constexpr_ref = dll_import_int;
  return dll_export_constexpr_ref;

}

int main() {

  get();
  
  return 0;

}

MSVC is generating these symbols:
ksh-3.2$ dumpbin /symbols test3.obj  | grep dll
007 00000000 UNDEF  notype       External     | ?dll_export_int@@3HA (int dll_export_int)
018 00000000 SECT6  notype       Static       | ?dll_export_constexpr_ref@@3AEAHEA (int & dll_export_constexpr_ref)
019 00000000 UNDEF  notype       External     | __imp_?dll_import_int@@3HA (__declspec(dllimport) int dll_import_int)
ksh-3.2$

clang (with this patch) is generating an error:
test3.cpp:2:16: error: constexpr variable 'dll_export_constexpr_ref' must be

  initialized by a constant expression

constexpr int& dll_export_constexpr_ref = dll_export_int;

  ^                          ~~~~~~~~~~~~~~

1 error generated.

Your thoughts?


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D117569/new/

https://reviews.llvm.org/D117569



More information about the cfe-commits mailing list