On 20 May 2021, at 11:49, Victor Stinner <vstinner@python.org> wrote:
On Wed, May 19, 2021 at 12:45 PM Ronald Oussoren <ronaldoussoren@mac.com> wrote:
Do you mean “new C file” instead of “new C function”? Because all C compilers I’ve used so far default to including whose object files when a program uses at least one symbol from that object file. That’s why Py_FrozenMain() is currently the only missing symbol, the rest of the core has sufficiently large source code files to ensure all of them will be included. The issue could crop up for other symbols if, for example, functions that are included only for backward compatibility were to be moved to separate files.
Sorry, you're right: "new C file".
If no symbol of an object file (.o) is used, GCC ignores all symbols of the object file. If at least one symbol is used, all symbols of this object file are exported. It's weird, but it's the behavior that I saw in my manual tests.
It is not weird, but documented behaviour ;-). And that’s probably due to historical accidents, such simplicity in early linkers.
It's even more surprising because the issue goes away if no static library (libpython.a) is used. It really depends on how exactly C files are linked altogether.
Linking a shared library will include everything that’s included in the link, or at least every object file containing visible symbols. I’ve not checked if visibility=hidden changes can result in object files being dropped, but that’s not interesting for our current discussion as symbols in the public API aren’t. hidden.
The linker will exclude unused object files in static libraries to avoid creating unnecessarily large output files, otherwise every binary that links staticly to libc would get huge. There are AFAIK 3 ways to avoid this (1) don’t link with libpython.a, but include all object files in the link command, (2) use (linker-specific) flags to include all files in libpython.a or (3) ensure that all object files contain a symbol that is referenced from the file containing the main function (directly or indirectly). IMHO that’s something we don’t have to look into right now, Py_FrozenMain is currently the only problematic symbol and is not useable from extensions.
Ronald
Victor
Night gathers, and now my watch begins. It shall not end until my death.
—
Twitter / micro.blog: @ronaldoussoren Blog: https://blog.ronaldoussoren.net/