Using Both LTO and Stack Protector Fails
If I try to compile a simple hello world using BOTH -flto and -fstack-protector-strong, I get undefined symbols to __stack_chk_guard. If I remove either flag, it compiles successfully. Adding -lssp manually changes nothing. This occurs for both i686 and x86_64.
packager@af6574b4c827:~$ x86_64-w64-mingw32-clang++ -flto -fstack-protector-strong test.cpp -o test
lld-link: error: undefined symbol: __stack_chk_guard
>>> referenced by test.exe.lto.obj:(.refptr.__stack_chk_guard)
clang: error: linker command failed with exit code 1 (use -v to see invocation)
I am still using the llvm-12 branch of llvm-mingw, but I have tested and this occurs with the latest packaged release as well. Both clang 13 and 14 in the CLANG64 MSYS environment which I think are based off this project have the same issue. MinGW GCC seems to have no issue with this combination of flags.
As for a MCVE, the "hello world" program I am referring to is simply:
// test.cpp
#include <iostream>
int main(int argc, char *argv[])
{
std::cout << "Hello World!" << std::endl;
return 0;
}
Is this issue also happening when you use -flto=full? I'm asking because the last time I compiled with -flto=full -fstack-protector-strong it worked fine.
Edit: I tested again and it didn't fail on my machine
I think I know what the issue is here... First lld runs through all the input object files and input static libraries, and pulls in as many object files from the static libraries as needed to fulfill all symbol references. After that, it does LTO compilation of all inputs that were in LLVM IR form. In this case, the LTO object files don't indicate undefined references to the __stack_chk_guard symbol anywhere, so the linker skips those parts of libssp.a even if you specified -lssp.
If you'd add e.g. -Wl,--require-defined,__stack_chk_guard to the linking command, it'd trigger the linker to include those object files (even if there's no indication of them being needed at the time). (That's not a proper fix, but potentially a workaround until the issue is fixed.)
Is this issue also happening when you use
-flto=full? I'm asking because the last time I compiled with-flto=full -fstack-protector-strongit worked fine.Edit: I tested again and it didn't fail on my machine
I presume in this case, you have some object file in your build, which is built with -fstack-protector-strong but without LTO, which pulls in the right routines before doing the LTO compilation.
Even specifying -flto=full gives me the same error and as this is literally just a single hello world c++ source file, so there are no other object files to worry about aside from the compiler libraries that get linked to. Adding -Wl,--require-defined,__stack_chk_guard though does solve the problem without error for me so thanks for that!