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] LLD-LINK produces broken forward imports in source DEF #132411

Open
lhmouse opened this issue Mar 21, 2025 · 3 comments
Open

[lld-link] LLD-LINK produces broken forward imports in source DEF #132411

lhmouse opened this issue Mar 21, 2025 · 3 comments
Labels

Comments

@lhmouse
Copy link
Contributor

lhmouse commented Mar 21, 2025

First let's define an empty DLL that exports CreateThread, forwarded from Windows KERNEL32.DLL:

// test.c
int
__stdcall
_DllMainCRTStartup(void* i, unsigned long r, void* p)
{ return 1; }
; test.def
EXPORTS
  CreateThread = kernel32.CreateThread

Now build two DLLs, one with Microsoft LINK (good.dll) and one with LLD (bad.dll):

clang test.c -Wl,-DEF:test.def -shared -Wl,-nodefaultlib -Wl,kernel32.lib -o good.dll
clang test.c -Wl,-DEF:test.def -shared -Wl,-nodefaultlib -Wl,kernel32.lib -fuse-ld=lld -o bad.dll

The bad DLL will cause failure when it's loaded by another application, saying _kernel32.dll is not found.

Now let's open the good DLL with a hex editor:

Image

And this is the bad DLL. The import from _kernel32.CreateThread is exactly the issue.

Image

This bug exists only when targeting x86-32.

@lhmouse lhmouse changed the title [lld-link] LLD-LINK produces broken imports in source DEF [lld-link] LLD-LINK produces broken forward imports in source DEF Mar 21, 2025
lhmouse added a commit to lhmouse/mcfgthread that referenced this issue Mar 21, 2025
Clang-CL has bugs about forwarding imports, which breaks x86 builds _somehow_.
So we define them in source and export them.

Reference: llvm/llvm-project#132411
Signed-off-by: LIU Hao <lh_mouse@126.com>
@cjacek
Copy link
Contributor

cjacek commented Mar 21, 2025

This should fix the problem: cjacek@2845c53

@lhmouse
Copy link
Contributor Author

lhmouse commented Mar 22, 2025

This should fix the problem: cjacek@2845c53

The change looks correct to me. Thanks.

@llvmbot
Copy link
Member

llvmbot commented Mar 22, 2025

@llvm/issue-subscribers-lld-coff

Author: LIU Hao (lhmouse)

First let's define an empty DLL that exports `CreateThread`, forwarded from Windows KERNEL32.DLL:
// test.c
int
__stdcall
_DllMainCRTStartup(void* i, unsigned long r, void* p)
{ return 1; }
; test.def
EXPORTS
  CreateThread = kernel32.CreateThread

Now build two DLLs, one with Microsoft LINK (good.dll) and one with LLD (bad.dll):

clang test.c -Wl,-DEF:test.def -shared -Wl,-nodefaultlib -Wl,kernel32.lib -o good.dll
clang test.c -Wl,-DEF:test.def -shared -Wl,-nodefaultlib -Wl,kernel32.lib -fuse-ld=lld -o bad.dll

The bad DLL will cause failure when it's loaded by another application, saying _kernel32.dll is not found.

Now let's open the good DLL with a hex editor:

Image

And this is the bad DLL. The import from _kernel32.CreateThread is exactly the issue.

Image

This bug exists only when targeting x86-32.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants