Skip to content

Commit

Permalink
hide private functions with __attribute__((visibility("hidden")))
Browse files Browse the repository at this point in the history
This allows us to enforce API boundaries and potentially enables
compiler optimisations.

We've been always hiding non-public symbols on Windows. This commit
brings that to the other platforms.
  • Loading branch information
xenu committed Jun 18, 2022
1 parent 2287d33 commit 0351a62
Show file tree
Hide file tree
Showing 18 changed files with 1,237 additions and 624 deletions.
30 changes: 30 additions & 0 deletions Configure
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,7 @@ d_attribute_nonnull=''
d_attribute_noreturn=''
d_attribute_pure=''
d_attribute_unused=''
d_attribute_visibility=''
d_attribute_warn_unused_result=''
d_printf_format_null=''
d_backtrace=''
Expand Down Expand Up @@ -11337,6 +11338,34 @@ set d_attribute_always_inline
eval $setvar
$rm -f attrib*

: Look for GCC-style attribute visibility
case "$d_attribute_visibility" in
'')
echo " "
echo "Checking whether your compiler can handle __attribute__((visibility)) ..." >&4
$cat >attrib.c <<'EOCP'
#include <stdio.h>
__attribute__((visibility("hidden"))) int I_will_be_hidden(void);
EOCP
if $cc $ccflags -c attrib.c >attrib.out 2>&1 ; then
if $compiler_warning attrib.out >/dev/null 2>&1; then
echo "Your C compiler doesn't support __attribute__((visibility))."
val="$undef"
else
echo "Your C compiler supports __attribute__((visibility))."
val="$define"
fi
else
echo "Your C compiler doesn't seem to understand __attribute__ at all."
val="$undef"
fi
;;
*) val="$d_attribute_visibility" ;;
esac
set d_attribute_visibility
eval $setvar
$rm -f attrib*

: see if getpgrp exists
set getpgrp d_getpgrp
eval $inlibc
Expand Down Expand Up @@ -24598,6 +24627,7 @@ d_attribute_nonnull='$d_attribute_nonnull'
d_attribute_noreturn='$d_attribute_noreturn'
d_attribute_pure='$d_attribute_pure'
d_attribute_unused='$d_attribute_unused'
d_attribute_visibility='$d_attribute_visibility'
d_attribute_warn_unused_result='$d_attribute_warn_unused_result'
d_backtrace='$d_backtrace'
d_bsd='$d_bsd'
Expand Down
1 change: 1 addition & 0 deletions Cross/config.sh-arm-linux
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ d_attribute_nonnull='undef'
d_attribute_noreturn='undef'
d_attribute_pure='undef'
d_attribute_unused='undef'
d_attribute_visibility='undef'
d_attribute_warn_unused_result='undef'
d_backtrace='undef'
d_bsd='undef'
Expand Down
1 change: 1 addition & 0 deletions Cross/config.sh-arm-linux-n770
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ d_attribute_nonnull='undef'
d_attribute_noreturn='undef'
d_attribute_pure='undef'
d_attribute_unused='undef'
d_attribute_visibility='undef'
d_attribute_warn_unused_result='undef'
d_backtrace='undef'
d_bsd='undef'
Expand Down
1 change: 1 addition & 0 deletions Porting/config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ d_attribute_nonnull='define'
d_attribute_noreturn='define'
d_attribute_pure='define'
d_attribute_unused='define'
d_attribute_visibility='undef'
d_attribute_warn_unused_result='define'
d_backtrace='define'
d_bsd='undef'
Expand Down
5 changes: 5 additions & 0 deletions config_h.SH
Original file line number Diff line number Diff line change
Expand Up @@ -1495,6 +1495,10 @@ sed <<!GROK!THIS! >$CONFIG_H -e 's!^#undef\(.*/\)\*!/\*#define\1 \*!' -e 's!^#un
* Can we handle GCC attribute for functions that should always be
* inlined.
*/
/* HASATTRIBUTE_VISIBILITY:
* Can we handle GCC attribute for functions that should have a
* different visibility.
*/
#$d_attribute_deprecated HASATTRIBUTE_DEPRECATED /**/
#$d_attribute_format HASATTRIBUTE_FORMAT /**/
#$d_printf_format_null PRINTF_FORMAT_NULL_OK /**/
Expand All @@ -1505,6 +1509,7 @@ sed <<!GROK!THIS! >$CONFIG_H -e 's!^#undef\(.*/\)\*!/\*#define\1 \*!' -e 's!^#un
#$d_attribute_unused HASATTRIBUTE_UNUSED /**/
#$d_attribute_warn_unused_result HASATTRIBUTE_WARN_UNUSED_RESULT /**/
#$d_attribute_always_inline HASATTRIBUTE_ALWAYS_INLINE /**/
#$d_attribute_visibility HASATTRIBUTE_VISIBILITY /**/
/* HAS_BACKTRACE:
* This symbol, if defined, indicates that the backtrace() routine is
Expand Down
1 change: 1 addition & 0 deletions configure.com
Original file line number Diff line number Diff line change
Expand Up @@ -6124,6 +6124,7 @@ $ WC "d_attribute_nonnull='undef'"
$ WC "d_attribute_noreturn='undef'"
$ WC "d_attribute_pure='undef'"
$ WC "d_attribute_unused='undef'"
$ WC "d_attribute_visibility='undef'"
$ WC "d_attribute_warn_unused_result='undef'"
$ WC "d_prctl='undef'"
$ WC "d_prctl_set_name='undef'"
Expand Down
10 changes: 10 additions & 0 deletions perl.h
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ Now a no-op.
# if PERL_GCC_VERSION_GE(4,7,0)
# define HASATTRIBUTE_ALWAYS_INLINE
# endif
# if PERL_GCC_VERSION_GE(3,3,0)
# define HASATTRIBUTE_VISIBILITY
# endif
# endif
#endif /* #ifndef PERL_MICRO */

Expand Down Expand Up @@ -397,6 +400,10 @@ Now a no-op.
# define __attribute__always_inline__ __attribute__((always_inline))
# endif
#endif
#if defined(HASATTRIBUTE_VISIBILITY) && !defined(_WIN32)
/* On Windows, instead of this, we use __declspec(dllexport) and a .def file */
# define __attribute__visibility__(x) __attribute__((visibility(x)))
#endif

/* If we haven't defined the attributes yet, define them to blank. */
#ifndef __attribute__deprecated__
Expand Down Expand Up @@ -426,6 +433,9 @@ Now a no-op.
#ifndef __attribute__always_inline__
# define __attribute__always_inline__
#endif
#ifndef __attribute__visibility__
# define __attribute__visibility__(x)
#endif

/* Some OS warn on NULL format to printf */
#ifdef PRINTF_FORMAT_NULL_OK
Expand Down
1 change: 1 addition & 0 deletions plan9/config_sh.sample
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ d_attribute_nonnull='undef'
d_attribute_noreturn='undef'
d_attribute_pure='undef'
d_attribute_unused='undef'
d_attribute_visibility='undef'
d_attribute_warn_unused_result='undef'
d_backtrace='undef'
d_bsd='undef'
Expand Down
10 changes: 10 additions & 0 deletions pod/perldelta.pod
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,16 @@ C<PL_use_safe_putenv> has been removed and C<PERL_USE_SAFE_PUTENV> is always
defined. This means XS modules can now call C<setenv> and C<putenv> without
causing segfaults. [L<perl #19399|https://github.com/Perl/perl5/issues/19399>]

=item *

Internal C API functions are now hidden with C<__attribute__((hidden))> on the
platforms that support it. This means they are no longer callable from XS
modules on those platforms.

It should be noted that those functions have always been hidden on Windows. This
change merely brings that to the other platforms.
[L<perl #19655|https://github.com/Perl/perl5/pull/19655>]

=back

=head1 Selected Bug Fixes
Expand Down

0 comments on commit 0351a62

Please sign in to comment.