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

It crashes when I use zig build-lib to generate static library for msvc on windows #7066

Closed
waruqi opened this issue Nov 11, 2020 · 8 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. os-windows
Milestone

Comments

@waruqi
Copy link

waruqi commented Nov 11, 2020

example project: https://github.com/xmake-io/xmake/tree/master/tests/projects/zig/static_library

We can download xmake-latest.win64.exe install package to install xmake (dev version) from https://github.com/xmake-io/xmake/actions/runs/355512412

Then test this problem. (zig 0.7.0)

$ xmake f --zc=y:\zig\zig.exe
$ xmake -rv
[ 37%]: compiling.release src\main.zig
y:\zig\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\test\windows\x64\release\src\main.zig.obj src\main.zig
[ 50%]: compiling.release src\test.zig
y:\zig\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\testlib\windows\x64\release\src\test.zig.obj src\test.zig
[ 62%]: archiving.release testlib.lib
y:\zig\zig.exe build-lib --strip -femit-bin=build\windows\x64\release\testlib.lib build\.objs\testlib\windows\x64\release\src\test.zig.obj
error: LLVM ERROR: IO failure on output stream: Bad file descriptor

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Nov 11, 2020
@andrewrk andrewrk added this to the 0.7.1 milestone Nov 11, 2020
@andrewrk
Copy link
Member

andrewrk commented Dec 11, 2020

It would be better if xmake would do this instead of separate commands:

y:\zig\zig.exe build-lib src\test.zig --strip -femit-bin=build\windows\x64\release\testlib.lib -O ReleaseFast -target x86_64-windows-msvc 

Zig is crippled when it is unnecessarily forced to go through objects.

Anyway, I have followed the steps to reproduce the issue, but I am stuck on xmake not passing --subsystem:

C:\msys64\home\andy\dev\zig\zig-cache>"c:\Program Files\xmake\xmake.exe" -rv
checking for the zig linker (zcld) ... zig.exe
checking for flags (-O ReleaseFast) ... ok
checking for ccache ... no
[ 37%]: compiling.release src\main.zig
bin\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\test\windows\x64\release\src\main.zig.obj src\main.zig
[ 50%]: compiling.release src\test.zig
bin\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\testlib\windows\x64\release\src\test.zig.obj src\test.zig
[ 62%]: archiving.release testlib.lib
bin\zig.exe build-lib --strip -femit-bin=build\windows\x64\release\testlib.lib build\.objs\testlib\windows\x64\release\src\test.zig.obj
[ 87%]: linking.release test.exe
bin\zig.exe build-exe -target x86_64-windows-msvc -Lbuild\windows\x64\release --strip -ltestlib -femit-bin=build\windows\x64\release\test.exe build\.objs\test\windows\x64\release\src\main.zig.obj
error: lld-link: error: subsystem must be defined
attempt to unwrap error: LLDReportedFailure

(Note that --subsystem will not need to be passed if build-exe is given zig source instead of only objects)

@andrewrk
Copy link
Member

I hacked in --subsystem into the compiler, and made it to here:

C:\msys64\home\andy\dev\zig\zig-cache>"c:\Program Files\xmake\xmake.exe" -rv
[ 37%]: compiling.release src\test.zig
bin\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\testlib\windows\x64\release\src\test.zig.obj src\test.zig
[ 50%]: compiling.release src\main.zig
bin\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\test\windows\x64\release\src\main.zig.obj src\main.zig
[ 62%]: archiving.release testlib.lib
bin\zig.exe build-lib --strip -femit-bin=build\windows\x64\release\testlib.lib build\.objs\testlib\windows\x64\release\src\test.zig.obj
[ 87%]: linking.release test.exe
bin\zig.exe build-exe -target x86_64-windows-msvc -Lbuild\windows\x64\release --strip -ltestlib -femit-bin=build\windows\x64\release\test.exe build\.objs\test\windows\x64\release\src\main.zig.obj
error: lld-link: error: undefined symbol: WriteFile
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\start.zig:173
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(wWinMainCRTStartup)
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\start.zig:173
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(wWinMainCRTStartup)
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\start.zig:173
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(wWinMainCRTStartup)
>>> referenced 11 more times

lld-link: error: undefined symbol: ExitProcess
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\start.zig:173
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(wWinMainCRTStartup)
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\start.zig:173
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(wWinMainCRTStartup)

lld-link: error: undefined symbol: GetLastError
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\start.zig:173
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(wWinMainCRTStartup)
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\log.zig:194
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(std.log.scoped(default).err)
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\fmt.zig:732
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(std.fmt.formatBuf)

lld-link: error: undefined symbol: NtCreateKeyedEvent
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\mutex.zig:239
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(std.mutex.WindowsMutex.acquireSlow)
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\log.zig:194
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(std.log.scoped(default).err)

lld-link: error: undefined symbol: NtWaitForKeyedEvent
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\mutex.zig:254
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(std.mutex.WindowsMutex.acquireSlow)

lld-link: error: undefined symbol: NtReleaseKeyedEvent
>>> referenced by C:\msys64\home\andy\dev\zig\zig-cache\lib\zig\std\log.zig:194
>>>               build\.objs\test\windows\x64\release\src\main.zig.obj:(std.log.scoped(default).err)
attempt to unwrap error: LLDReportedFailure

This is the same problem from #5825. xmake either needs to give build-exe the zig source files instead of objects, or it needs to pass -lkernel32 -lntdll. How can I test adding these flags with the lua script?

@andrewrk
Copy link
Member

I hacked the compiler to include -lkernel32 and -lntdll and got this output:

C:\msys64\home\andy\dev\zig\zig-cache>"c:\Program Files\xmake\xmake.exe" -rv
[ 37%]: compiling.release src\test.zig
bin\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\testlib\windows\x64\release\src\test.zig.obj src\test.zig
[ 50%]: compiling.release src\main.zig
bin\zig.exe build-obj -target x86_64-windows-msvc -O ReleaseFast -femit-bin=build\.objs\test\windows\x64\release\src\main.zig.obj src\main.zig
[ 62%]: archiving.release testlib.lib
bin\zig.exe build-lib --strip -femit-bin=build\windows\x64\release\testlib.lib build\.objs\testlib\windows\x64\release\src\test.zig.obj
[ 87%]: linking.release test.exe
bin\zig.exe build-exe -target x86_64-windows-msvc -Lbuild\windows\x64\release --strip -ltestlib -femit-bin=build\windows\x64\release\test.exe build\.objs\test\windows\x64\release\src\main.zig.obj
[100%]: build ok!

So I believe that the error: LLVM ERROR: IO failure on output stream: Bad file descriptor has been solved. Please let me know if you can reproduce it.

@waruqi
Copy link
Author

waruqi commented Dec 12, 2020

So I believe that the error: LLVM ERROR: IO failure on output stream: Bad file descriptor has been solved. Please let me know if you can reproduce it.

Ok, I will test it later. Thanks.

@waruqi
Copy link
Author

waruqi commented Dec 12, 2020

It would be better if xmake would do this instead of separate commands:

y:\zig\zig.exe build-lib src\test.zig --strip -femit-bin=build\windows\x64\release\testlib.lib -O ReleaseFast -target x86_64-windows-msvc
Zig is crippled when it is unnecessarily forced to go through objects.

If so, how to compile c/c++ and link other language files together? For example asm, objc, dlang and etc.

@andrewrk
Copy link
Member

For C/C++, you can put them into the command line too! 😁

y:\zig\zig.exe build-lib src\test.zig --strip -femit-bin=build\windows\x64\release\testlib.lib -O ReleaseFast -target x86_64-windows-msvc foo.c bar.cpp -lc++ -lc

For other objects, it's no problem - you can use zig as an archiver or linker, it will work fine (see it worked in #7066 (comment)) but you just have to add those flags to tell zig what subsystem and what libs to link.

Most of the time if you are adding objects from other languages, they will depend on libc, in which case you can pass -lc and zig will bring in the appropriate Windows libraries as needed.

@waruqi
Copy link
Author

waruqi commented Dec 12, 2020

For other objects, it's no problem - you can use zig as an archiver or linker, it will work fine (see it worked in #7066 (comment)) but you just have to add those flags to tell zig what subsystem and what libs to link.

Ok, if zig really can’t know, I will pass these flags to zig, thank you.

@waruqi
Copy link
Author

waruqi commented Dec 12, 2020

It works fine on local disk, but it still does not work on mounted share disk. (virtualbox/win7)

But I think this should be a bug of llvm. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. os-windows
Projects
None yet
Development

No branches or pull requests

2 participants