In a C compiler, usage of an external function can be split into two completely separate steps: A header file provides a (forward) declaration, while the actual implementation will not be relevant until linking.
With Rust, its module system and Crates, those two steps are more entwined: A use
or extern crate
statement will lead to both, the external symbol being available (declared) and its Crate getting linked (plus stuff like monomorphization, which is irrelevant for now).
However, the declaration part will not work if the external Crate is available in binary format only. So I suspect an rlib does provide the information required to use its symbols (function signatures etc.), probably as part of Crate metadata. Am I right here? Does Crate metadata really contain information about function signatures?
This gets even more interesting when taking name mangling into account. The mangling v0 RFC points out that the current (legacy) mangling scheme depends on compiler internals and does not generate predictable names:
Since the current scheme generates its hash from the values of various compiler internal data structures, an alternative compiler implementation could not predict the symbol name, even for simple cases.
In my experience, this means that the mangled name of a function depends on a lot of obscure factors and can't even be derived by the same compiler implementation without access to the full source code. This yields the question of how the compiler knows the mangled names of external functions.
Once again, I would expect this information to be part of Crate metadata. But is it really?
Unfortunately, documentation on both Crate metadata and mangling is pretty sparse, so I appreciate any clues!