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

The address of a dllimported global variable cannot be used in a constant initializer #20329

Closed
rnk opened this issue Jun 5, 2014 · 6 comments
Assignees
Labels
bugzilla Issues migrated from bugzilla llvm:core

Comments

@rnk
Copy link
Collaborator

rnk commented Jun 5, 2014

Bugzilla Link 19955
Resolution FIXED
Resolved on Jun 25, 2014 23:25
Version trunk
OS Windows NT
CC @asl,@compnerd,@majnemer,@ehsan,@zmodem

Extended Description

Consider:

extern "C" int __declspec(dllimport) x;
extern "C" int *y = &x;

Clang creates this LLVM IR:

@x = external dllimport global i32
@y = global i32* @x, align 4

This is lowered to:

        .data
        .globl  _y
        .align  4
_y:
        .long   _x

Which is incorrect, because x is dllimported, and its address isn't a link time constant. It must be loaded from __imp_x. Clang should emit something like the following IR instead:

@y = global i32* null, align 4
@x = external dllimport global i32
@llvm.global_ctors = appending global [1 x { i32, void ()*, i8* }] [{ i32, void ()*, i8* } { i32 65535, void ()* @init, i8* null }]
define internal void @init() {
entry:
  store i32* @x, i32** @y, align 4
  ret void
}

This is consistent with what MSVC does, and it would be important to match them if x was selectany or a static data member of a class template.

However, this is really a bug in LLVM, because LLVM will optimize that IR back to Clang's original output:

$ opt t.ll -O2 -S -o -
@y = global i32* @x, align 4
@x = external dllimport global i32
@llvm.global_ctors = appending global [0 x { i32, void ()*, i8* }] zeroinitializer

If y were a function, this would actually link correctly, because import libraries provide thunks for functions. The only downside is that the address of a thunk isn't really the address of the function, but few programs rely on function identity. There's no way to create a forwarding thunk for data, so we get a link failure with this test case.

@rnk
Copy link
Collaborator Author

rnk commented Jun 5, 2014

assigned to @majnemer

@rnk
Copy link
Collaborator Author

rnk commented Jun 18, 2014

*** Bug llvm/llvm-bugzilla-archive#20076 has been marked as a duplicate of this bug. ***

@majnemer
Copy link
Mannequin

majnemer mannequin commented Jun 23, 2014

Fixes are available.
LLVM-related: http://reviews.llvm.org/D4249
Clang-related: http://reviews.llvm.org/D4250

@majnemer
Copy link
Mannequin

majnemer mannequin commented Jun 24, 2014

Fixed in r211571, r211570, r211568.

@ehsan
Copy link
Contributor

ehsan commented Jun 26, 2014

Thanks, I verified this on our side!

@rnk
Copy link
Collaborator Author

rnk commented Nov 26, 2021

mentioned in issue llvm/llvm-bugzilla-archive#20076

@llvmbot llvmbot transferred this issue from llvm/llvm-bugzilla-archive Dec 9, 2021
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bugzilla Issues migrated from bugzilla llvm:core
Projects
None yet
Development

No branches or pull requests

2 participants