Skip to content

Commit 0351a62

Browse files
committed
hide private functions with __attribute__((visibility("hidden")))
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.
1 parent 2287d33 commit 0351a62

18 files changed

+1237
-624
lines changed

Configure

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,7 @@ d_attribute_nonnull=''
391391
d_attribute_noreturn=''
392392
d_attribute_pure=''
393393
d_attribute_unused=''
394+
d_attribute_visibility=''
394395
d_attribute_warn_unused_result=''
395396
d_printf_format_null=''
396397
d_backtrace=''
@@ -11337,6 +11338,34 @@ set d_attribute_always_inline
1133711338
eval $setvar
1133811339
$rm -f attrib*
1133911340

11341+
: Look for GCC-style attribute visibility
11342+
case "$d_attribute_visibility" in
11343+
'')
11344+
echo " "
11345+
echo "Checking whether your compiler can handle __attribute__((visibility)) ..." >&4
11346+
$cat >attrib.c <<'EOCP'
11347+
#include <stdio.h>
11348+
__attribute__((visibility("hidden"))) int I_will_be_hidden(void);
11349+
EOCP
11350+
if $cc $ccflags -c attrib.c >attrib.out 2>&1 ; then
11351+
if $compiler_warning attrib.out >/dev/null 2>&1; then
11352+
echo "Your C compiler doesn't support __attribute__((visibility))."
11353+
val="$undef"
11354+
else
11355+
echo "Your C compiler supports __attribute__((visibility))."
11356+
val="$define"
11357+
fi
11358+
else
11359+
echo "Your C compiler doesn't seem to understand __attribute__ at all."
11360+
val="$undef"
11361+
fi
11362+
;;
11363+
*) val="$d_attribute_visibility" ;;
11364+
esac
11365+
set d_attribute_visibility
11366+
eval $setvar
11367+
$rm -f attrib*
11368+
1134011369
: see if getpgrp exists
1134111370
set getpgrp d_getpgrp
1134211371
eval $inlibc
@@ -24598,6 +24627,7 @@ d_attribute_nonnull='$d_attribute_nonnull'
2459824627
d_attribute_noreturn='$d_attribute_noreturn'
2459924628
d_attribute_pure='$d_attribute_pure'
2460024629
d_attribute_unused='$d_attribute_unused'
24630+
d_attribute_visibility='$d_attribute_visibility'
2460124631
d_attribute_warn_unused_result='$d_attribute_warn_unused_result'
2460224632
d_backtrace='$d_backtrace'
2460324633
d_bsd='$d_bsd'

Cross/config.sh-arm-linux

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ d_attribute_nonnull='undef'
124124
d_attribute_noreturn='undef'
125125
d_attribute_pure='undef'
126126
d_attribute_unused='undef'
127+
d_attribute_visibility='undef'
127128
d_attribute_warn_unused_result='undef'
128129
d_backtrace='undef'
129130
d_bsd='undef'

Cross/config.sh-arm-linux-n770

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ d_attribute_nonnull='undef'
123123
d_attribute_noreturn='undef'
124124
d_attribute_pure='undef'
125125
d_attribute_unused='undef'
126+
d_attribute_visibility='undef'
126127
d_attribute_warn_unused_result='undef'
127128
d_backtrace='undef'
128129
d_bsd='undef'

Porting/config.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ d_attribute_nonnull='define'
141141
d_attribute_noreturn='define'
142142
d_attribute_pure='define'
143143
d_attribute_unused='define'
144+
d_attribute_visibility='undef'
144145
d_attribute_warn_unused_result='define'
145146
d_backtrace='define'
146147
d_bsd='undef'

config_h.SH

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,10 @@ sed <<!GROK!THIS! >$CONFIG_H -e 's!^#undef\(.*/\)\*!/\*#define\1 \*!' -e 's!^#un
14951495
* Can we handle GCC attribute for functions that should always be
14961496
* inlined.
14971497
*/
1498+
/* HASATTRIBUTE_VISIBILITY:
1499+
* Can we handle GCC attribute for functions that should have a
1500+
* different visibility.
1501+
*/
14981502
#$d_attribute_deprecated HASATTRIBUTE_DEPRECATED /**/
14991503
#$d_attribute_format HASATTRIBUTE_FORMAT /**/
15001504
#$d_printf_format_null PRINTF_FORMAT_NULL_OK /**/
@@ -1505,6 +1509,7 @@ sed <<!GROK!THIS! >$CONFIG_H -e 's!^#undef\(.*/\)\*!/\*#define\1 \*!' -e 's!^#un
15051509
#$d_attribute_unused HASATTRIBUTE_UNUSED /**/
15061510
#$d_attribute_warn_unused_result HASATTRIBUTE_WARN_UNUSED_RESULT /**/
15071511
#$d_attribute_always_inline HASATTRIBUTE_ALWAYS_INLINE /**/
1512+
#$d_attribute_visibility HASATTRIBUTE_VISIBILITY /**/
15081513
15091514
/* HAS_BACKTRACE:
15101515
* This symbol, if defined, indicates that the backtrace() routine is

configure.com

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6124,6 +6124,7 @@ $ WC "d_attribute_nonnull='undef'"
61246124
$ WC "d_attribute_noreturn='undef'"
61256125
$ WC "d_attribute_pure='undef'"
61266126
$ WC "d_attribute_unused='undef'"
6127+
$ WC "d_attribute_visibility='undef'"
61276128
$ WC "d_attribute_warn_unused_result='undef'"
61286129
$ WC "d_prctl='undef'"
61296130
$ WC "d_prctl_set_name='undef'"

perl.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,9 @@ Now a no-op.
364364
# if PERL_GCC_VERSION_GE(4,7,0)
365365
# define HASATTRIBUTE_ALWAYS_INLINE
366366
# endif
367+
# if PERL_GCC_VERSION_GE(3,3,0)
368+
# define HASATTRIBUTE_VISIBILITY
369+
# endif
367370
# endif
368371
#endif /* #ifndef PERL_MICRO */
369372

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

401408
/* If we haven't defined the attributes yet, define them to blank. */
402409
#ifndef __attribute__deprecated__
@@ -426,6 +433,9 @@ Now a no-op.
426433
#ifndef __attribute__always_inline__
427434
# define __attribute__always_inline__
428435
#endif
436+
#ifndef __attribute__visibility__
437+
# define __attribute__visibility__(x)
438+
#endif
429439

430440
/* Some OS warn on NULL format to printf */
431441
#ifdef PRINTF_FORMAT_NULL_OK

plan9/config_sh.sample

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ d_attribute_nonnull='undef'
124124
d_attribute_noreturn='undef'
125125
d_attribute_pure='undef'
126126
d_attribute_unused='undef'
127+
d_attribute_visibility='undef'
127128
d_attribute_warn_unused_result='undef'
128129
d_backtrace='undef'
129130
d_bsd='undef'

pod/perldelta.pod

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,16 @@ C<PL_use_safe_putenv> has been removed and C<PERL_USE_SAFE_PUTENV> is always
366366
defined. This means XS modules can now call C<setenv> and C<putenv> without
367367
causing segfaults. [L<perl #19399|https://github.com/Perl/perl5/issues/19399>]
368368

369+
=item *
370+
371+
Internal C API functions are now hidden with C<__attribute__((hidden))> on the
372+
platforms that support it. This means they are no longer callable from XS
373+
modules on those platforms.
374+
375+
It should be noted that those functions have always been hidden on Windows. This
376+
change merely brings that to the other platforms.
377+
[L<perl #19655|https://github.com/Perl/perl5/pull/19655>]
378+
369379
=back
370380

371381
=head1 Selected Bug Fixes

0 commit comments

Comments
 (0)