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

lld-link.exe fails to find DATA imports without __declspec(dllimport) #128706

Open
stasoid opened this issue Feb 25, 2025 · 0 comments
Open

lld-link.exe fails to find DATA imports without __declspec(dllimport) #128706

stasoid opened this issue Feb 25, 2025 · 0 comments

Comments

@stasoid
Copy link

stasoid commented Feb 25, 2025

clang/lld-link version: 19.1.0.

This issue was encountered while porting Ladybird to Windows. Ladybird discord discussions: one two.

If data item (global or class static variable) is imported, lld-link refuses to link it unless __declspec(dllimport) is specified. Functions (global and member functions) don't have this problem. See also: WINDOWS_EXPORT_ALL_SYMBOLS in CMake.

I don't know if it is a bug or a feature, I could not find a similar issue. Does lld-link intentionally behave like Visual Studio link.exe here? It would be great if it behaved like on Linux instead. This is just confusing.

Simplified reproduction:

mylib.h:

struct A
{
    static int static_member; // error
    void member_function(); // OK
};

extern int global; // error
void global_function(); // OK

mylib.cpp:

#include "mylib.h"

int global;

int A::static_member;

void global_function()
{
}

void A::member_function()
{
}

myprog.cpp:

#include "mylib.h"

int main()
{
   global = 1;
   global_function();

   A::static_member = 1;
   A().member_function();
}

build.bat:

:: create dll.o
clang -c mylib.cpp

:: create exports.def
echo mylib.o > objs
cmake -E __create_def exports.def objs

:: create mylib.dll and mylib.lib
lld-link mylib.o /dll libcmt.lib /DEF:exports.def

:: create myprog.o
clang -c myprog.cpp

:: create myprog.exe
lld-link myprog.o mylib.lib libcmt.lib

The last command fails with these errors:

lld-link: error: undefined symbol: int global
>>> referenced by myprog.o:(main)

lld-link: error: undefined symbol: public: static int A::static_member
>>> referenced by myprog.o:(main)

The def/lib/dll export all 4 symbols:

> cat exports.def
EXPORTS
        ?global@@3HA     DATA
        ?static_member@A@@2HA    DATA
        ?global_function@@YAXXZ
        ?member_function@A@@QEAAXXZ

> dumpbin /exports mylib.lib
(some output skipped)

                  ?global@@3HA (int global)
                  ?global_function@@YAXXZ (void __cdecl global_function(void))
                  ?member_function@A@@QEAAXXZ (public: void __cdecl A::member_function(void))
                  ?static_member@A@@2HA (public: static int A::static_member)

> dumpbin /exports mylib.dll
(some output skipped)

          1    0 0001AAC8 ?global@@3HA
          2    1 00001000 ?global_function@@YAXXZ
          3    2 00001010 ?member_function@A@@QEAAXXZ
          4    3 0001AACC ?static_member@A@@2HA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants