Use -fvisibility=hidden for shared libraries
Andres Freund <andres@anarazel.de>
From: Andres Freund <andres@anarazel.de>
To: pgsql-hackers@postgresql.org
Date: 2021-11-01T02:03:11Z
Lists: pgsql-hackers
Attachments
Hi, Currently postgres builds extension shared libraries (i.e. pretty much everything but libpq) with global visibilty. I.e. symbols that are not static will be visible from outside the shared library. On the one platform where that behaviour is not available, namely windows, we emulate it by analyzing the input files to the shared library and exporting all the symbols therein. For the meson build [1] proposal that turned out to be a bit more verbose to implement than I'd liked. Thinking about the problem I realized that, at least I think so, there's really no good reason to force-export symbols in our shared libraries: Because the number of symbols required from shared libraries is typically small, and the majority of the symbols are generated via PG_FUNCTION_INFO_V1, it isn't a lot of work to explicitly export the necessary symbols. I think this is a much more feasible proposal than, as has been suggested in the past, using explicit visibility for the entire backend. The amount of functions that'd need to be annotated in the backend is huge, and I don't think we have a reasonable starting point for limiting what we'd export. There are a few extension libs that need manual export annotations. These are libraries that are not just referenced by the backend, but also from other extensions, e.g. for transforms. The patch e.g. reduces the number of exported symbols for - plpython: 61 -> 29 - plperl: 32 -> 17 - llvmjit: 84 -> 5 - postgres_fdw: 48 -> 15 - dict_snowball: 182 -> 8 Besides the smaller number of symbols (which can impact library load times a bit, although the numbers here are likely small enough to not matter), using -fvisibility=hidden also can improve code generation, because it allows the compiler & linker to generate a plain function call, rather than having to go through the elf symbol-interposition table. The attached patch is a prototype of this idea. Greetings, Andres Freund [1] https://www.postgresql.org/message-id/20211012083721.hvixq4pnh2pixr3j%40alap3.anarazel.de
Commits
-
Mark all symbols exported from extension libraries PGDLLEXPORT.
- 8cf64d35eacc 16.0 landed
-
Default to hidden visibility for extension libraries where possible
- 089480c07705 16.0 landed
-
Remove now superfluous declarations of dlsym()ed symbols.
- fd4bad165539 16.0 landed
-
Add central declarations for dlsym()ed symbols
- f2b73c8d75d5 16.0 landed