GNOME Bugzilla – Bug 778287
G_MODULE_EXPORT and -fvisibility=hidden
Last modified: 2017-03-23 14:59:20 UTC
G_MODULE_EXPORT should contain __attribute__((visibility("default"))) to export the symbol when compiling with gcc flag -fvisibility=hidden Test case: ------- #include <gmodule.h> G_MODULE_EXPORT const char version[] = "1.2.3"; ------- gcc $(pkg-config --cflags glib-2.0) glib-visibility.c -shared && nm -D [...] 0000000000000629 R version gcc $(pkg-config --cflags glib-2.0) glib-visibility.c -shared -fvisibility=hidden && nm -D [no symbol named version]
It should probably be defined the same way that _GLIB_EXTERN is defined in configure.ac (which is what GLib uses internally for exporting its API when -fvisibility=hidden is enabled). See bug #688681 for the details on that one. I can’t see a reason why adding __attribute__((visibility("default"))) to symbols which are declared as G_MODULE_EXPORT should cause problems. It would change the effective symbol visibility in projects which compile with anything other than -fvisibility=default (for example, -fvisibility=internal), but that’s what we want.
If your project changes the visibility rules, then I think it's your responsibility to add the annotation itself, instead of modifying G_MODULE_EXPORT for everyone. The visibility rules inside GLib do not propagate to dependent libraries.
(In reply to Emmanuele Bassi (:ebassi) from comment #2) > If your project changes the visibility rules, then I think it's your > responsibility to add the annotation itself, instead of modifying > G_MODULE_EXPORT for everyone. The visibility rules inside GLib do not > propagate to dependent libraries. G_MODULE_EXPORT is not used within GLib: it’s orthogonal to _GLIB_EXTERN. It’s defined for external modules to use to publicly export their symbols. This bug is precisely the use case it’s for.
I was writing a plugin for wireshark when I hit this bug, but I wasn't using any build system. I am happy with any solution now when we have had a discussion about it.
Created attachment 345611 [details] [review] gmodule: Add the visibility attribute to G_MODULE_EXPORT on gcc For versions of GCC which support it (≥ 4), define G_MODULE_EXPORT as __attribute__((visibility("default"))). This is normally a no-op, unless compiling with -fvisibility=hidden, in which case it marks a symbol to be publicly exported from the library, which is what G_MODULE_EXPORT is for. Previously G_MODULE_EXPORT has only worked on Windows. The compatibility check for whether the compiler supports __attribute__((visibility)) is based on the __GNUC__ define, and is similar to the check done in configure.ac for defining G_GNUC_INTERNAL. Signed-off-by: Philip Withnall <withnall@endlessm.com>
Here’s a potential patch for the problem. I don’t have any compilers which don’t support __attribute__((visibility)) though, so I can’t really test that the feature check works. It will break with a compiler which defines __GNUC__ ≥ 4 but doesn’t support __attribute__((visibility)).
Review of attachment 345611 [details] [review]: This makes sense to me. +1
Attachment 345611 [details] pushed as 5533293 - gmodule: Add the visibility attribute to G_MODULE_EXPORT on gcc