Skip to content

build: workaround link error with LTO and mingw - #8674

Merged
andrewrk merged 3 commits into
ziglang:masterfrom
xxxbxxx:master
Nov 15, 2021
Merged

andrewrk merged 3 commits into
ziglang:masterfrom
xxxbxxx:master

Conversation

@xxxbxxx

@xxxbxxx xxxbxxx commented May 2, 2021 •

Copy link
Copy Markdown
Contributor

The symbol "_tls_index" gets lost when using LTO.
Disabling LTO only on the object file that defines it allows the link to work.

I have no idea what I'm doing :)
I tried a few random things in tlssup.c to see if the symbol would stay, with no success.
Short of actually figuring out the issue, maybe it would be cleaner to just drop LTO on the whole mingw32.lib?

I also tried to test with x86_64-windows-msvc, but no idea how to make it work at all when linking with libC.

fixes #8531

Maybe related:
as soon as a xxx.c file is linked (even if no symbols are pulled from it), I get a warning:

warning(link): unexpected LLD stderr:
lld-link: warning: ignoring debug info with an invalid version (0) in N:\zigdbg\zig-cache\o\03a5f823f3c02149079e56dc64f92fb2\zigdbg.o

@LemonBoy

LemonBoy commented May 2, 2021

Copy link
Copy Markdown
Contributor

That's the correct solution. _tls_index is not referenced by anything within the mingw32 library itself and is thus dropped during the LTO process, while we want for that symbol to end up in the final object.

Linking to a github issue in the comment is not that useful, writing a few words on why it's being disabled is much better
Eg. // Can't let LTO drop any symbol defined in here

@xxxbxxx

xxxbxxx commented May 2, 2021

Copy link
Copy Markdown
Contributor Author

_tls_index is not referenced by anything within the mingw32 library itself and is thus dropped during the LTO process, while we want for that symbol to end up in the final object

mmm. I don't get it.
I mean mingw32.lib is just aggregated not-yet-linked object files (either machinecode or IR dumps in lto)
And then when the final program is linked, then the symbol is actually referenced (like others from the lib). Actually most symbols are like this in static libraries, so why this one is different?
Is it because it is used internally by the compiler/linker for some magical TLS stuff, and not by the actual executable code?

@xxxbxxx
xxxbxxx force-pushed the master branch 2 times, most recently from 074f90b to ad00754 Compare May 2, 2021 19:14
@LemonBoy

LemonBoy commented May 2, 2021

Copy link
Copy Markdown
Contributor

Is it because it is used internally by the compiler/linker for some magical TLS stuff, and not by the actual executable code?

After some cursory testing it seems that the external symbols introduced by LLVM during the expansion of the thread-local accesses are not taken into account when computing the LTO module index.

This is probably worth reporting upstream.

@xxxbxxx

xxxbxxx commented May 3, 2021

Copy link
Copy Markdown
Contributor Author

I've added a test for the issue. (linking a c file and a zig file, with threadlocal variables)

All is fine in debug builds.

However, in release builds:

  • macos fails to link, with error: MissingMainEntrypoint

  • windows-gnu gives a warning

warning(link): unexpected LLD stderr:
lld-link: warning: ignoring debug info with an invalid version (0) in D:\a\1\s\test\standalone\mix_c_files\zig-cache\o\adbf700d0f61e162e41124f3f0fce448\test.obj
  • freebsd also has a warning:
warning(link): unexpected LLD stderr:
ld.lld: warning: linking module flags 'Dwarf Version': IDs have conflicting values ('i32 2' from /usr/home/build/zig/test/standalone/mix_c_files/zig-cache/o/9589d9679386de4941703ed5f244b416/test.o with 'i32 4' from ld-temp.o)

I guess I can disable the test on macos, but I don't know about the debug info warnings.

@xxxbxxx

xxxbxxx commented May 3, 2021

Copy link
Copy Markdown
Contributor Author

Also, the test links with libC, but it's not actually used.

However if I remove it, of course, the workaround for windows release builds is not used, and once again the linker can't seem to find _tls_index ...

@xxxbxxx
xxxbxxx force-pushed the master branch 4 times, most recently from e1ec251 to fbe6d2a Compare May 24, 2021 22:44
xxxbxxx and others added 3 commits November 15, 2021 16:15
The symbol "_tls_index" gets lost when using LTO.
Disabling LTO on the object file that defines it allows the link to work.

fixes ziglang#8531
 * Annotate workarounds with their corresponding GitHub issue links.
 * Enable test coverage for LTO on Windows with the added c_compiler test.
@andrewrk
andrewrk merged commit 39c0d8b into ziglang:master Nov 15, 2021
@andrewrk

Copy link
Copy Markdown
Member

Thank you for this work, and apologies for the delayed merge. I'm adding a comment on #8531 now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

LLD with LTO enabled incorrectly drops _tls_index symbol, causing undefined reference errors

3 participants