Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fix lgamma setting signgam for ISO C (bug 15421).
The lgamma (and likewise lgammaf, lgammal) function wrongly sets the
signgam variable even when building for strict ISO C conformance
(-std=c99 / -std=c11), although the user may define such a variable
and it's only in the implementation namespace for POSIX with XSI
extensions enabled.

Following discussions starting at
<https://sourceware.org/ml/libc-alpha/2013-04/msg00767.html> and
<https://sourceware.org/ml/libc-alpha/2015-10/msg00844.html>, it seems
that the safest approach for fixing this particular issue is for
signgam to become a weak alias for a newly exported symbol __signgam,
with the library functions only setting __signgam, at which point
static linker magic will preserve the alias for newly linked binaries
that refer to the library's signgam rather than defining their own,
while breaking the alias for programs that define their own signgam,
with new symbol versions for lgamma functions and with compat symbols
for existing binaries that set both signgam and __signgam.

This patch implements that approach for the fix.  signgam is made into
a weak alias.  The four symbols __signgam, lgamma, lgammaf, lgammal
get new symbol versions at version GLIBC_2.23, with the existing
versions of lgamma, lgammaf and lgammal becoming compat symbols.

When the compat versions are built, gamma, gammaf and gammal are
aliases for the compat versions (i.e. always set signgam); this is OK
as they are not ISO C functions, and avoids adding new symbol versions
for them unnecessarily.  When the compat versions are not built
(i.e. for static linking and for future glibc ports), gamma, gammaf
and gammal are aliases for the new versions that set __signgam.  The
ldbl-opt versions are updated accordingly.

The lgamma wrappers are adjusted so that the same source files,
included from different files with different definitions of
USE_AS_COMPAT, can build either the new versions or the compat
versions.  Similar changes are made to the ia64 versions (untested).

Tests are added that the lgamma functions do not interfere with a user
variable called signgam for ISO C, with various choices for the size
of that variable, whether it is initialized, and for static and
dynamic linking.  The conformtest whitelist entry is removed as well.

Tested for x86_64, x86, mips64 and powerpc, including looking at
objdump --dynamic-syms output to make sure the expected sets of
symbols were aliases.  Also spot-tested that a binary built with old
glibc works properly (i.e. gets signgam set) when run with new glibc.

	[BZ #15421]
	* sysdeps/ieee754/s_signgam.c (signgam): Rename to __signgam,
	initialize with 0 and define as weak alias of __signgam.
	* include/math.h [!_ISOMAC] (__signgam): Declare.
	* math/Makefile (libm-calls): Add w_lgamma_compat.
	(tests): Add test-signgam-uchar, test-signgam-uchar-init,
	test-signgam-uint, test-signgam-uint-init, test-signgam-ullong and
	test-signgam-ullong-init.
	(tests-static): Add test-signgam-uchar-static,
	test-signgam-uchar-init-static, test-signgam-uint-static,
	test-signgam-uint-init-static, test-signgam-ullong-static and
	test-signgam-ullong-init-static.
	(CFLAGS-test-signgam-uchar.c): New variable.
	(CFLAGS-test-signgam-uchar-init.c): Likewise.
	(CFLAGS-test-signgam-uchar-static.c): Likewise.
	(CFLAGS-test-signgam-uchar-init-static.c): Likewise.
	(CFLAGS-test-signgam-uint.c): Likewise.
	(CFLAGS-test-signgam-uint-init.c): Likewise.
	(CFLAGS-test-signgam-uint-static.c): Likewise.
	(CFLAGS-test-signgam-uint-init-static.c): Likewise.
	(CFLAGS-test-signgam-ullong.c): Likewise.
	(CFLAGS-test-signgam-ullong-init.c): Likewise.
	(CFLAGS-test-signgam-ullong-static.c): Likewise.
	(CFLAGS-test-signgam-ullong-init-static.c): Likewise.
	* math/Versions (libm): Add GLIBC_2.23.
	* math/lgamma-compat.h: New file.
	* math/test-signgam-main.c: Likewise.
	* math/test-signgam-uchar-init-static.c: Likewise.
	* math/test-signgam-uchar-init.c: Likewise.
	* math/test-signgam-uchar-static.c: Likewise.
	* math/test-signgam-uchar.c: Likewise.
	* math/test-signgam-uint-init-static.c: Likewise.
	* math/test-signgam-uint-init.c: Likewise.
	* math/test-signgam-uint-static.c: Likewise.
	* math/test-signgam-uint.c: Likewise.
	* math/test-signgam-ullong-init-static.c: Likewise.
	* math/test-signgam-ullong-init.c: Likewise.
	* math/test-signgam-ullong-static.c: Likewise.
	* math/test-signgam-ullong.c: Likewise.
	* math/w_lgamma.c: Rename to w_lgamma_main.c and replace by
	wrapper of w_lgamma_main.c.
	* math/w_lgamma_compat.c: New file.
	* math/w_lgamma_compatf.c: Likewise.
	* math/w_lgamma_compatl.c: Likewise.
	* math/w_lgamma_main.c: New file.  Based on w_lgamma.c.  Include
	<lgamma-compat.h>.  Condition contents on [BUILD_LGAMMA].  Support
	defining compatibility symbols.
	(__lgamma): Change to LGFUNC (__lgamma).  Use CALL_LGAMMA.
	* math/w_lgammaf.c: Rename to w_lgammaf_main.c and replace by
	wrapper of w_lgammaf_main.c.
	* math/w_lgammaf_main.c: New file.  Based on w_lgammaf.c.  Include
	<lgamma-compat.h>.  Condition contents on [BUILD_LGAMMA].  Support
	defining compatibility symbols.
	(__lgammaf): Change to LGFUNC (__lgammaf).  Use CALL_LGAMMA.
	* math/w_lgammal.c: Rename to w_lgammal_main.c and replace by
	wrapper of w_lgammal_main.c.
	* math/w_lgammal_main.c: New file.  Based on w_lgammal.c.  Include
	<lgamma-compat.h>.  Condition contents on [BUILD_LGAMMA].  Support
	defining compatibility symbols.
	(__lgammal): Change to LGFUNC (__lgammal).  Use CALL_LGAMMA.
	* sysdeps/ia64/fpu/lgamma-compat.h: New file.
	* sysdeps/ia64/fpu/w_lgamma.c: Move to ....
	* sysdeps/ia64/fpu/w_lgamma_main.c: ...here.  Include
	<lgamma-compat.h>.
	(__ieee754_lgamma): Change to LGFUNC (lgamma).  Use CALL_LGAMMA.
	(__ieee754_gamma): Define as alias.
	* sysdeps/ia64/fpu/w_lgammaf.c: Move to ....
	* sysdeps/ia64/fpu/w_lgammaf_main.c: ...here.  Include
	<lgamma-compat.h>.
	(__ieee754_lgammaf): Change to LGFUNC (lgammaf).  Use CALL_LGAMMA.
	(__ieee754_gammaf): Define as alias.
	* sysdeps/ia64/fpu/w_lgammal.c: Move to ....
	* sysdeps/ia64/fpu/w_lgammal_main.c: ...here.  Include
	<lgamma-compat.h>.
	(__ieee754_lgammal): Change to LGFUNC (lgammal).  Use CALL_LGAMMA.
	(__ieee754_gammal): Define as alias.
	* sysdeps/ieee754/ldbl-opt/w_lgamma.c: Move to ....
	* sysdeps/ieee754/ldbl-opt/w_lgamma_compat.c: ...here.  Include
	<math/w_lgamma_compat.c>.
	[LONG_DOUBLE_COMPAT(libm, GLIBC_2_0)] (__lgammal_dbl_compat):
	Define as alias of __lgamma_compat and use in defining lgammal.
	* sysdeps/ieee754/ldbl-opt/w_lgammal.c: Move to ....
	* sysdeps/ieee754/ldbl-opt/w_lgamma_compatl.c: ...here.  Include
	<math/lgamma-compat.h> and <math/w_lgamma_compatl.c>.
	(USE_AS_COMPAT): New macro.
	(LGAMMA_OLD_VER): Undefine and redefine.
	(lgammal): Do not define here.
	(gammal): Only define here if [GAMMA_ALIAS].
	* conform/linknamespace.pl (@whitelist): Remove signgam.
	* sysdeps/nacl/libm.abilist: Update.
	* sysdeps/unix/sysv/linux/aarch64/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/alpha/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/arm/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/hppa/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/i386/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/ia64/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/m68k/coldfire/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/m68k/m680x0/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/microblaze/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/mips/mips32/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/mips/mips64/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/nios2/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libm.abilist:
	Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libm.abilist:
	Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc64/libm-le.abilist:
	Likewise.
	* sysdeps/unix/sysv/linux/powerpc/powerpc64/libm.abilist:
	Likewise.
	* sysdeps/unix/sysv/linux/s390/s390-32/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/s390/s390-64/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/sh/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/sparc/sparc32/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/sparc/sparc64/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libm.abilist:
	Likewise.
	* sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libm.abilist:
	Likewise.
	* sysdeps/unix/sysv/linux/tile/tilepro/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/x86_64/64/libm.abilist: Likewise.
	* sysdeps/unix/sysv/linux/x86_64/x32/libm.abilist: Likewise.
  • Loading branch information
Joseph Myers committed Nov 20, 2015
1 parent 8c873bf commit d709042
Show file tree
Hide file tree
Showing 65 changed files with 721 additions and 204 deletions.
125 changes: 125 additions & 0 deletions ChangeLog
@@ -1,3 +1,128 @@
2015-11-20 Joseph Myers <joseph@codesourcery.com>

[BZ #15421]
* sysdeps/ieee754/s_signgam.c (signgam): Rename to __signgam,
initialize with 0 and define as weak alias of __signgam.
* include/math.h [!_ISOMAC] (__signgam): Declare.
* math/Makefile (libm-calls): Add w_lgamma_compat.
(tests): Add test-signgam-uchar, test-signgam-uchar-init,
test-signgam-uint, test-signgam-uint-init, test-signgam-ullong and
test-signgam-ullong-init.
(tests-static): Add test-signgam-uchar-static,
test-signgam-uchar-init-static, test-signgam-uint-static,
test-signgam-uint-init-static, test-signgam-ullong-static and
test-signgam-ullong-init-static.
(CFLAGS-test-signgam-uchar.c): New variable.
(CFLAGS-test-signgam-uchar-init.c): Likewise.
(CFLAGS-test-signgam-uchar-static.c): Likewise.
(CFLAGS-test-signgam-uchar-init-static.c): Likewise.
(CFLAGS-test-signgam-uint.c): Likewise.
(CFLAGS-test-signgam-uint-init.c): Likewise.
(CFLAGS-test-signgam-uint-static.c): Likewise.
(CFLAGS-test-signgam-uint-init-static.c): Likewise.
(CFLAGS-test-signgam-ullong.c): Likewise.
(CFLAGS-test-signgam-ullong-init.c): Likewise.
(CFLAGS-test-signgam-ullong-static.c): Likewise.
(CFLAGS-test-signgam-ullong-init-static.c): Likewise.
* math/Versions (libm): Add GLIBC_2.23.
* math/lgamma-compat.h: New file.
* math/test-signgam-main.c: Likewise.
* math/test-signgam-uchar-init-static.c: Likewise.
* math/test-signgam-uchar-init.c: Likewise.
* math/test-signgam-uchar-static.c: Likewise.
* math/test-signgam-uchar.c: Likewise.
* math/test-signgam-uint-init-static.c: Likewise.
* math/test-signgam-uint-init.c: Likewise.
* math/test-signgam-uint-static.c: Likewise.
* math/test-signgam-uint.c: Likewise.
* math/test-signgam-ullong-init-static.c: Likewise.
* math/test-signgam-ullong-init.c: Likewise.
* math/test-signgam-ullong-static.c: Likewise.
* math/test-signgam-ullong.c: Likewise.
* math/w_lgamma.c: Rename to w_lgamma_main.c and replace by
wrapper of w_lgamma_main.c.
* math/w_lgamma_compat.c: New file.
* math/w_lgamma_compatf.c: Likewise.
* math/w_lgamma_compatl.c: Likewise.
* math/w_lgamma_main.c: New file. Based on w_lgamma.c. Include
<lgamma-compat.h>. Condition contents on [BUILD_LGAMMA]. Support
defining compatibility symbols.
(__lgamma): Change to LGFUNC (__lgamma). Use CALL_LGAMMA.
* math/w_lgammaf.c: Rename to w_lgammaf_main.c and replace by
wrapper of w_lgammaf_main.c.
* math/w_lgammaf_main.c: New file. Based on w_lgammaf.c. Include
<lgamma-compat.h>. Condition contents on [BUILD_LGAMMA]. Support
defining compatibility symbols.
(__lgammaf): Change to LGFUNC (__lgammaf). Use CALL_LGAMMA.
* math/w_lgammal.c: Rename to w_lgammal_main.c and replace by
wrapper of w_lgammal_main.c.
* math/w_lgammal_main.c: New file. Based on w_lgammal.c. Include
<lgamma-compat.h>. Condition contents on [BUILD_LGAMMA]. Support
defining compatibility symbols.
(__lgammal): Change to LGFUNC (__lgammal). Use CALL_LGAMMA.
* sysdeps/ia64/fpu/lgamma-compat.h: New file.
* sysdeps/ia64/fpu/w_lgamma.c: Move to ....
* sysdeps/ia64/fpu/w_lgamma_main.c: ...here. Include
<lgamma-compat.h>.
(__ieee754_lgamma): Change to LGFUNC (lgamma). Use CALL_LGAMMA.
(__ieee754_gamma): Define as alias.
* sysdeps/ia64/fpu/w_lgammaf.c: Move to ....
* sysdeps/ia64/fpu/w_lgammaf_main.c: ...here. Include
<lgamma-compat.h>.
(__ieee754_lgammaf): Change to LGFUNC (lgammaf). Use CALL_LGAMMA.
(__ieee754_gammaf): Define as alias.
* sysdeps/ia64/fpu/w_lgammal.c: Move to ....
* sysdeps/ia64/fpu/w_lgammal_main.c: ...here. Include
<lgamma-compat.h>.
(__ieee754_lgammal): Change to LGFUNC (lgammal). Use CALL_LGAMMA.
(__ieee754_gammal): Define as alias.
* sysdeps/ieee754/ldbl-opt/w_lgamma.c: Move to ....
* sysdeps/ieee754/ldbl-opt/w_lgamma_compat.c: ...here. Include
<math/w_lgamma_compat.c>.
[LONG_DOUBLE_COMPAT(libm, GLIBC_2_0)] (__lgammal_dbl_compat):
Define as alias of __lgamma_compat and use in defining lgammal.
* sysdeps/ieee754/ldbl-opt/w_lgammal.c: Move to ....
* sysdeps/ieee754/ldbl-opt/w_lgamma_compatl.c: ...here. Include
<math/lgamma-compat.h> and <math/w_lgamma_compatl.c>.
(USE_AS_COMPAT): New macro.
(LGAMMA_OLD_VER): Undefine and redefine.
(lgammal): Do not define here.
(gammal): Only define here if [GAMMA_ALIAS].
* conform/linknamespace.pl (@whitelist): Remove signgam.
* sysdeps/nacl/libm.abilist: Update.
* sysdeps/unix/sysv/linux/aarch64/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/alpha/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/arm/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/hppa/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/i386/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/ia64/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/m68k/coldfire/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/m68k/m680x0/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/microblaze/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/mips/mips32/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/mips/mips64/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/nios2/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc32/fpu/libm.abilist:
Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc32/nofpu/libm.abilist:
Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc64/libm-le.abilist:
Likewise.
* sysdeps/unix/sysv/linux/powerpc/powerpc64/libm.abilist:
Likewise.
* sysdeps/unix/sysv/linux/s390/s390-32/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/s390/s390-64/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/sh/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc32/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/sparc/sparc64/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/tile/tilegx/tilegx32/libm.abilist:
Likewise.
* sysdeps/unix/sysv/linux/tile/tilegx/tilegx64/libm.abilist:
Likewise.
* sysdeps/unix/sysv/linux/tile/tilepro/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/x86_64/64/libm.abilist: Likewise.
* sysdeps/unix/sysv/linux/x86_64/x32/libm.abilist: Likewise.

2015-11-20 Adhemerval Zanella <adhemerval.zanella@linaro.org>

[BZ #16364]
Expand Down
6 changes: 6 additions & 0 deletions NEWS
Expand Up @@ -42,6 +42,12 @@ Version 2.23
* Optimized string, wcsmbs and memory functions for IBM z13.
Implemented by Stefan Liebler.

* Newly linked programs that define a variable called signgam will no longer
have it set by the lgamma, lgammaf and lgammal functions. Programs that
require signgam to be set by those functions must ensure that they use the
variable provided by the GNU C Library and declared in <math.h>, without
defining their own copy.

* The minimum GCC version that can be used to build this version of the GNU
C Library is GCC 4.7. Older GCC versions, and non-GNU compilers, can
still be used to compile programs using the GNU C Library.
Expand Down
4 changes: 1 addition & 3 deletions conform/linknamespace.pl
Expand Up @@ -40,8 +40,6 @@

# The following whitelisted symbols are also allowed for now.
#
# * Bug 15421: lgamma wrongly sets signgam for ISO C.
#
# * Bug 17576: stdin, stdout, stderr only reserved with external
# linkage when stdio.h included (and possibly not then), not
# generally.
Expand All @@ -52,7 +50,7 @@
# * False positive: matherr only used conditionally. matherrf/matherrl are used
# by IA64 too for the same reason.
#
@whitelist = qw(signgam stdin stdout stderr re_syntax_options matherr matherrf
@whitelist = qw(stdin stdout stderr re_syntax_options matherr matherrf
matherrl);
foreach my $sym (@whitelist) {
$stdsyms{$sym} = 1;
Expand Down
2 changes: 2 additions & 0 deletions include/math.h
Expand Up @@ -6,6 +6,8 @@
/* Now define the internal interfaces. */
extern int __matherr (struct exception *__exc);

extern int __signgam;

# if IS_IN (libc) || IS_IN (libm)
hidden_proto (__finite)
hidden_proto (__isinf)
Expand Down
24 changes: 21 additions & 3 deletions math/Makefile
Expand Up @@ -62,7 +62,8 @@ libm-calls = e_acos e_acosh e_asin e_atan2 e_atanh e_cosh e_exp e_fmod \
s_casinh s_cacosh s_catanh s_csqrt s_cpow s_cproj s_clog10 \
s_fma s_lrint s_llrint s_lround s_llround e_exp10 w_log2 \
s_issignaling $(calls:s_%=m_%) x2y2m1 k_casinh \
gamma_product k_standard lgamma_neg lgamma_product
gamma_product k_standard lgamma_neg lgamma_product \
w_lgamma_compat

dbl-only-routines := branred doasin dosincos halfulp mpa mpatan2 \
mpatan mpexp mplog mpsqrt mptan sincos32 slowexp \
Expand Down Expand Up @@ -110,8 +111,13 @@ tests = test-matherr test-fenv atest-exp atest-sincos atest-exp2 basic-test \
test-fenv-tls test-fenv-preserve test-fenv-return test-fenvinline \
test-nearbyint-except test-fenv-clear test-signgam-finite \
test-signgam-finite-c99 test-signgam-finite-c11 \
test-nearbyint-except-2 $(tests-static)
tests-static = test-fpucw-static test-fpucw-ieee-static
test-nearbyint-except-2 test-signgam-uchar test-signgam-uchar-init \
test-signgam-uint test-signgam-uint-init test-signgam-ullong \
test-signgam-ullong-init $(tests-static)
tests-static = test-fpucw-static test-fpucw-ieee-static \
test-signgam-uchar-static test-signgam-uchar-init-static \
test-signgam-uint-static test-signgam-uint-init-static \
test-signgam-ullong-static test-signgam-ullong-init-static
# We do the `long double' tests only if this data type is available and
# distinct from `double'.
test-longdouble-yes = test-ldouble test-ildoubl test-ldouble-finite
Expand Down Expand Up @@ -200,6 +206,18 @@ CPPFLAGS-test-ildoubl.c = -U__LIBC_INTERNAL_MATH_INLINES \
CFLAGS-test-signgam-finite.c = -ffinite-math-only
CFLAGS-test-signgam-finite-c99.c = -ffinite-math-only -std=c99
CFLAGS-test-signgam-finite-c11.c = -ffinite-math-only -std=c11
CFLAGS-test-signgam-uchar.c = -std=c99
CFLAGS-test-signgam-uchar-init.c = -std=c99
CFLAGS-test-signgam-uchar-static.c = -std=c99
CFLAGS-test-signgam-uchar-init-static.c = -std=c99
CFLAGS-test-signgam-uint.c = -std=c99
CFLAGS-test-signgam-uint-init.c = -std=c99
CFLAGS-test-signgam-uint-static.c = -std=c99
CFLAGS-test-signgam-uint-init-static.c = -std=c99
CFLAGS-test-signgam-ullong.c = -std=c99
CFLAGS-test-signgam-ullong-init.c = -std=c99
CFLAGS-test-signgam-ullong-static.c = -std=c99
CFLAGS-test-signgam-ullong-init-static.c = -std=c99

# The -lieee module sets the _LIB_VERSION_ switch to IEEE mode
# for error handling in the -lm functions.
Expand Down
7 changes: 7 additions & 0 deletions math/Versions
Expand Up @@ -203,4 +203,11 @@ libm {
GLIBC_2.18 {
__issignaling; __issignalingf; __issignalingl;
}
GLIBC_2.23 {
# The __signgam name must be exported for the signgam weak alias
# to work. New symbol versions of lgamma* that set __signgam are
# needed to preserve compatibility with old binaries that have a
# dynamic symbol for signgam but not __signgam.
lgamma; lgammaf; lgammal; __signgam;
}
}
73 changes: 73 additions & 0 deletions math/lgamma-compat.h
@@ -0,0 +1,73 @@
/* ABI compatibility for lgamma functions.
Copyright (C) 2015 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

#ifndef LGAMMA_COMPAT_H
#define LGAMMA_COMPAT_H 1

#include <shlib-compat.h>

/* XSI POSIX requires lgamma to set signgam, but ISO C does not permit
this. Namespace issues can be avoided if the functions set
__signgam and signgam is a weak alias, but this only works if both
signgam and __signgam were exported from the glibc version the
program was linked against. Before glibc 2.23, lgamma functions
set signgam which was not a weak alias for __signgam, so old
binaries have dynamic symbols for signgam only and the versions of
lgamma used for old binaries must set both signgam and __signgam.
Those versions also do a check of _LIB_VERSION != _ISOC_ to match
old glibc.
Users of this file define USE_AS_COMPAT to 0 when building the main
version of lgamma, 1 when building the compatibility version. */

#define LGAMMA_OLD_VER GLIBC_2_0
#define LGAMMA_NEW_VER GLIBC_2_23
#define HAVE_LGAMMA_COMPAT SHLIB_COMPAT (libm, LGAMMA_OLD_VER, LGAMMA_NEW_VER)

/* Whether to build this version at all. */
#define BUILD_LGAMMA (HAVE_LGAMMA_COMPAT || !USE_AS_COMPAT)

/* The name to use for this version. */
#if USE_AS_COMPAT
# define LGFUNC(FUNC) FUNC ## _compat
#else
# define LGFUNC(FUNC) FUNC
#endif

/* If there is a compatibility version, gamma (not an ISO C function,
so never a problem for it to set signgam) points directly to it
rather than having separate versions. */
#define GAMMA_ALIAS (USE_AS_COMPAT ? HAVE_LGAMMA_COMPAT : !HAVE_LGAMMA_COMPAT)

/* How to call the underlying lgamma_r function. */
#define CALL_LGAMMA(TYPE, FUNC, ARG) \
({ \
TYPE lgamma_tmp; \
int local_signgam; \
if (USE_AS_COMPAT) \
{ \
lgamma_tmp = FUNC ((ARG), &local_signgam); \
if (_LIB_VERSION != _ISOC_) \
signgam = __signgam = local_signgam; \
} \
else \
lgamma_tmp = FUNC ((ARG), &__signgam); \
lgamma_tmp; \
})

#endif /* lgamma-compat.h. */
71 changes: 71 additions & 0 deletions math/test-signgam-main.c
@@ -0,0 +1,71 @@
/* Test lgamma functions do not set signgam for ISO C.
Copyright (C) 2015 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, see
<http://www.gnu.org/licenses/>. */

#undef _LIBC
#undef _GNU_SOURCE
#define _ISOMAC

#include <math.h>
#include <stdio.h>

#define INITVAL ((TYPE) -1 / 3)

#if DO_INIT
TYPE signgam = INITVAL;
#else
TYPE signgam;
#endif

#define RUN_TESTS(FUNC, TYPE) \
do \
{ \
volatile TYPE a, b, c __attribute__ ((unused)); \
a = 0.5; \
b = -0.5; \
signgam = INITVAL; \
c = FUNC (a); \
if (signgam == INITVAL) \
puts ("PASS: " #FUNC " (0.5) setting signgam"); \
else \
{ \
puts ("FAIL: " #FUNC " (0.5) setting signgam"); \
result = 1; \
} \
signgam = INITVAL; \
c = FUNC (b); \
if (signgam == INITVAL) \
puts ("PASS: " #FUNC " (-0.5) setting signgam"); \
else \
{ \
puts ("FAIL: " #FUNC " (-0.5) setting signgam"); \
result = 1; \
} \
} \
while (0)

int
main (void)
{
int result = 0;
RUN_TESTS (lgammaf, float);
RUN_TESTS (lgamma, double);
#ifndef NO_LONG_DOUBLE
RUN_TESTS (lgammal, long double);
#endif
return result;
}
1 change: 1 addition & 0 deletions math/test-signgam-uchar-init-static.c
@@ -0,0 +1 @@
#include "test-signgam-uchar-init.c"
3 changes: 3 additions & 0 deletions math/test-signgam-uchar-init.c
@@ -0,0 +1,3 @@
#define TYPE unsigned char
#define DO_INIT 1
#include "test-signgam-main.c"
1 change: 1 addition & 0 deletions math/test-signgam-uchar-static.c
@@ -0,0 +1 @@
#include "test-signgam-uchar.c"
3 changes: 3 additions & 0 deletions math/test-signgam-uchar.c
@@ -0,0 +1,3 @@
#define TYPE unsigned char
#define DO_INIT 0
#include "test-signgam-main.c"
1 change: 1 addition & 0 deletions math/test-signgam-uint-init-static.c
@@ -0,0 +1 @@
#include "test-signgam-uint-init.c"
3 changes: 3 additions & 0 deletions math/test-signgam-uint-init.c
@@ -0,0 +1,3 @@
#define TYPE unsigned int
#define DO_INIT 1
#include "test-signgam-main.c"
1 change: 1 addition & 0 deletions math/test-signgam-uint-static.c
@@ -0,0 +1 @@
#include "test-signgam-uint.c"
3 changes: 3 additions & 0 deletions math/test-signgam-uint.c
@@ -0,0 +1,3 @@
#define TYPE unsigned int
#define DO_INIT 0
#include "test-signgam-main.c"
1 change: 1 addition & 0 deletions math/test-signgam-ullong-init-static.c
@@ -0,0 +1 @@
#include "test-signgam-ullong-init.c"

0 comments on commit d709042

Please sign in to comment.