LLD: Support --unresolved-symbols=@path/to/file option for lld
lt;dr
Let's add another use case for the --unresolved-symbols linker flag. This flag will read a list of symbols that may be undefined at linking time. The usage would be --unresolved-symbols=@/path/to/file.
Context:
The common problem when linking statically with CUDA-like libraries, such as cuDNN and TensorRT, is related to undefined symbols that should be defined in the libc++ standard library (libcstd++.so). This can cause issues if the symbols are not present on the development host, but are present in the production environment.
Previously, our approach was straightforward: we simply created a library with empty stubs. The static linking worked fine, and the stubs were not used in the development environment. However, the latest update of TensorRT requires a more complex function that cannot be replaced with a simple stub - the inner implementation of std::thread.
A naive approach to solving this problem would be to simply update libcstd++.so on all development hosts. However, this approach is difficult to complete in a short period of time and is also not scalable for the long term.
Proposal
Let's extend the behavior of the --unresolved-symbols flag to include one more use case: --unresolved-symbols=@/path/to/file. This will read a list of symbols from the specified file and ignore errors related to any unresolved symbols mentioned in the list.
The default usage of the --unresolved-symbols=ignore-all flag is not appropriate in this case, as it may hide the actual problem with unresolved symbols.
Previously, our approach was straightforward: we simply created a library with empty stubs. The static linking worked fine, and the stubs were not used in the development environment. However, the latest update of TensorRT requires a more complex function that cannot be replaced with a simple stub - the inner implementation of std::thread.
What is the symbol of the innter implementation of std::thread? Why is it not feasible to define the symbol in a shared object stub?
A naive approach to solving this problem would be to simply update libcstd++.so on all development hosts. However, this approach is difficult to complete in a short period of time and is also not scalable for the long term.
Adding a new linker option requires more than just convenience; we need to thoroughly analyze its interaction with existing options and confirm that current alternatives aren't sufficient.
What is the symbol of the innter implementation of std::thread?
Actually, it's not just one symbol, it's actually two:
_ZNSt6thread15_M_start_threadESt10unique_ptrINS_6_StateESt14default_deleteIS1_EEPFvvEorstd::thread::_M_start_thread(std::unique_ptr<std::thread::_State, std::default_delete<std::thread::_State> >, void (*)())_ZNSt6thread6_StateD2Evorstd::thread::_State::~_State()
Why is it not feasible to define the symbol in a shared object stub?
You mean that we could create a shared library with stubs and link our programs to it? This might cause a problem if the first library we wanted to resolve via dlopen was our shared-stubs-library.
Actually, my best solution (without patching the linker) that I can think of is to simply download the appropriate version of libstd++ on the developer's machine when they are trying to link a program that is causing this issue. This way, all development hosts will have the minimum required version of libstd++ installed.
I re-read the context of your comments, and it still feels quite vague. Could you clarify what you mean by "empty stub"? Are you referring to an archive or a shared object file? How does the executable reference the "missing" symbols? Is the PLT or GOT involved? Are the symbols in the dead code path so their values don't matter?
Could you concretely describe the scenario as an LLD lit test, to make it clearer?