From 487890009e4ed87198331d0dae9b0be52d37f68c Mon Sep 17 00:00:00 2001 From: Joseph Myers Date: Tue, 2 Aug 2016 17:40:35 +0000 Subject: [PATCH] Support __STDC_WANT_LIB_EXT2__ feature test macro. This patch implements support for the __STDC_WANT_LIB_EXT2__ feature test macro from ISO/IEC TR 24731-2:2010, thereby implementing one possible approach for supporting ISO C feature test macros. Recall that, as described in , these macros work based on the definition when affected headers are included, so cannot be handled once when the first system header is included because that might not be one of the headers the particular macro in question affects. expresses views on possible approaches for implementation and follows up on that. This patch arranges things so that the relevant condition is __GLIBC_USE (LIB_EXT2), following one of the suggestions given. Headers using these macros include , which in turn includes . Headers must define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION before including , to discourage inclusion outside glibc as requested. __USE_GNU conditions on affected functions are changed to __GLIBC_USE (LIB_EXT2), while it's added as an additional alternative on the conditions for functions already enabled for some POSIX versions. It would be possible to convert existing __USE_* conditionals to __GLIBC_USE (with the relevant __GLIBC_USE_* being defined in where __USE_* are presently defined), and so make them typo-proof (given -Wundef -Werror in glibc builds) because __GLIBC_USE is used with #if not #ifdef / #if defined. No attempt is made to enforce the rule about diagnosing different definitions of __STDC_WANT_LIB_EXT2__ when affected headers are included; such a diagnostic is incompatible with multiple-include guards on the affected headers, unless compiler extensions are added to support it. As previously noted, glibc does not implement all features from TR 24731-2:2010: the functions aswprintf vaswprintf getwdelim getwline are not in glibc, although they would be appropriate to add if someone wished to do so. But I think it makes sense to support the feature test macro if *any* of the controlled features are present in glibc. Tested for x86_64 and x86 (testsuite, and that installed stripped shared libraries are unchanged by the patch). * bits/libc-header-start.h: New file. * Makefile (headers): Add bits/libc-header-start.h. * include/features.h (__STDC_WANT_LIB_EXT2__): Document. (__GLIBC_USE): New macro. * libio/stdio.h: Define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include instead of including . (fmemopen): Declare also if [__GLIBC_USE (LIB_EXT2)]. (open_memstream): Likewise. (vasprintf): Declare if [__GLIBC_USE (LIB_EXT2)], not [__USE_GNU]. (__asprintf): Likewise. (asprintf): Likewise. (__getdelim): Declare also if [__GLIBC_USE (LIB_EXT2)]. (getdelim): Likewise. (getline): Likewise. * string/string.h: Define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include instead of including . (strdup): Declare also if [__GLIBC_USE (LIB_EXT2)] (strndup): Likewise. * wcsmbs/wchar.h: Define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include instead of including . (open_wmemstream): Declare also if [__GLIBC_USE (LIB_EXT2)]. * manual/creature.texi (__STDC_WANT_LIB_EXT2__): Document macro. --- ChangeLog | 28 ++++++++++++++++++++++++++++ Makefile | 3 ++- NEWS | 5 ++++- bits/libc-header-start.h | 43 +++++++++++++++++++++++++++++++++++++++++++ include/features.h | 17 ++++++++++++++++- libio/stdio.h | 9 +++++---- manual/creature.texi | 8 ++++++++ string/string.h | 8 +++++--- wcsmbs/wchar.h | 5 ++++- 9 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 bits/libc-header-start.h diff --git a/ChangeLog b/ChangeLog index 8c2eb8e48f..c2ff29c1f7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,31 @@ +2016-08-02 Joseph Myers + + * bits/libc-header-start.h: New file. + * Makefile (headers): Add bits/libc-header-start.h. + * include/features.h (__STDC_WANT_LIB_EXT2__): Document. + (__GLIBC_USE): New macro. + * libio/stdio.h: Define + __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include + instead of including . + (fmemopen): Declare also if [__GLIBC_USE (LIB_EXT2)]. + (open_memstream): Likewise. + (vasprintf): Declare if [__GLIBC_USE (LIB_EXT2)], not [__USE_GNU]. + (__asprintf): Likewise. + (asprintf): Likewise. + (__getdelim): Declare also if [__GLIBC_USE (LIB_EXT2)]. + (getdelim): Likewise. + (getline): Likewise. + * string/string.h: Define + __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include + instead of including . + (strdup): Declare also if [__GLIBC_USE (LIB_EXT2)] + (strndup): Likewise. + * wcsmbs/wchar.h: Define + __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION and include + instead of including . + (open_wmemstream): Declare also if [__GLIBC_USE (LIB_EXT2)]. + * manual/creature.texi (__STDC_WANT_LIB_EXT2__): Document macro. + 2016-08-02 Florian Weimer Support linking against compatibility symbols, for use in tests. diff --git a/Makefile b/Makefile index 32748b3d2e..4478c97126 100644 --- a/Makefile +++ b/Makefile @@ -60,7 +60,8 @@ endif # $(AUTOCONF) = no $(addprefix install-, no-libc.a bin lib data headers others) headers := limits.h values.h features.h gnu-versions.h \ - bits/xopen_lim.h gnu/libc-version.h stdc-predef.h + bits/xopen_lim.h gnu/libc-version.h stdc-predef.h \ + bits/libc-header-start.h echo-headers: subdir_echo-headers diff --git a/NEWS b/NEWS index 8169e8fe8b..d057c06b57 100644 --- a/NEWS +++ b/NEWS @@ -7,7 +7,10 @@ using `glibc' in the "product" field. Version 2.25 -[Add important changes here] +* The feature test macro __STDC_WANT_LIB_EXT2__, from ISO/IEC TR + 24731-2:2010, is supported to enable declarations of functions from that + TR. Note that not all functions from that TR are supported by the GNU C + Library. Security related changes: diff --git a/bits/libc-header-start.h b/bits/libc-header-start.h new file mode 100644 index 0000000000..c6663074a8 --- /dev/null +++ b/bits/libc-header-start.h @@ -0,0 +1,43 @@ +/* Handle feature test macros at the start of a header. + Copyright (C) 2016 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 + . */ + +/* This header is internal to glibc and should not be included outside + of glibc headers. Headers including it must define + __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION first. This header + cannot have multiple include guards because ISO C feature test + macros depend on the definition of the macro when an affected + header is included, not when the first system header is + included. */ + +#ifndef __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION +# error "Never include directly." +#endif + +#undef __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION + +#include + +/* ISO/IEC TR 24731-2:2010 defines the __STDC_WANT_LIB_EXT2__ + macro. */ +#undef __GLIBC_USE_LIB_EXT2 +#if (defined __USE_GNU \ + || (defined __STDC_WANT_LIB_EXT2__ && __STDC_WANT_LIB_EXT2__ > 0)) +# define __GLIBC_USE_LIB_EXT2 1 +#else +# define __GLIBC_USE_LIB_EXT2 0 +#endif diff --git a/include/features.h b/include/features.h index 56d1c571b6..404014115a 100644 --- a/include/features.h +++ b/include/features.h @@ -24,6 +24,7 @@ __STRICT_ANSI__ ISO Standard C. _ISOC99_SOURCE Extensions to ISO C89 from ISO C99. _ISOC11_SOURCE Extensions to ISO C99 from ISO C11. + __STDC_WANT_LIB_EXT2__ Extensions to ISO C99 from TR 27431-2:2010. _POSIX_SOURCE IEEE Std 1003.1. _POSIX_C_SOURCE If ==1, like _POSIX_SOURCE; if >=2 add IEEE Std 1003.2; if >=199309L, add IEEE Std 1003.1b-1993; @@ -58,6 +59,10 @@ These are defined by this file and are used by the header files to decide what to declare or define: + __GLIBC_USE (F) Define things from feature set F. This is defined + to 1 or 0; the subsequent macros are either defined + or undefined, and those tests should be moved to + __GLIBC_USE. __USE_ISOC11 Define ISO C11 things. __USE_ISOC99 Define ISO C99 things. __USE_ISOC95 Define ISO C90 AMD1 (C95) things. @@ -90,7 +95,14 @@ explicitly undefined if they are not explicitly defined. Feature-test macros that are not defined by the user or compiler but are implied by the other feature-test macros defined (or by the - lack of any definitions) are defined by the file. */ + lack of any definitions) are defined by the file. + + ISO C feature test macros depend on the definition of the macro + when an affected header is included, not when the first system + header is included, and so they are handled in + , which does not have a multiple include + guard. Feature test macros that can be handled from the first + system header included are handled here. */ /* Undefine everything, so we get a clean slate. */ @@ -139,6 +151,9 @@ # define __GNUC_PREREQ(maj, min) 0 #endif +/* Whether to use feature set F. */ +#define __GLIBC_USE(F) __GLIBC_USE_ ## F + /* _BSD_SOURCE and _SVID_SOURCE are deprecated aliases for _DEFAULT_SOURCE. If _DEFAULT_SOURCE is present we do not issue a warning; the expectation is that the source is being diff --git a/libio/stdio.h b/libio/stdio.h index 4511c3cc69..e37f901e5f 100644 --- a/libio/stdio.h +++ b/libio/stdio.h @@ -24,7 +24,8 @@ #if !defined __need_FILE && !defined __need___FILE # define _STDIO_H 1 -# include +# define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION +# include __BEGIN_DECLS @@ -316,7 +317,7 @@ extern FILE *fopencookie (void *__restrict __magic_cookie, _IO_cookie_io_functions_t __io_funcs) __THROW __wur; #endif -#ifdef __USE_XOPEN2K8 +#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2) /* Create a new stream that refers to a memory buffer. */ extern FILE *fmemopen (void *__s, size_t __len, const char *__modes) __THROW __wur; @@ -395,7 +396,7 @@ extern int vsnprintf (char *__restrict __s, size_t __maxlen, __END_NAMESPACE_C99 #endif -#ifdef __USE_GNU +#if __GLIBC_USE (LIB_EXT2) /* Write formatted output to a string dynamically allocated with `malloc'. Store the address of the string in *PTR. */ extern int vasprintf (char **__restrict __ptr, const char *__restrict __f, @@ -653,7 +654,7 @@ extern char *fgets_unlocked (char *__restrict __s, int __n, #endif -#ifdef __USE_XOPEN2K8 +#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2) /* Read up to (and including) a DELIMITER from STREAM into *LINEPTR (and null-terminate it). *LINEPTR is a pointer returned from malloc (or NULL), pointing to *N characters of space. It is realloc'd as diff --git a/manual/creature.texi b/manual/creature.texi index 3c686165f1..65c5928343 100644 --- a/manual/creature.texi +++ b/manual/creature.texi @@ -166,6 +166,14 @@ macro @code{_ISOC99_SOURCE} should be defined. @end defvr @comment (none) +@comment ISO +@defvr Macro __STDC_WANT_LIB_EXT2__ +If you define this macro to the value @code{1}, features from ISO/IEC +TR 24731-2:2010 (Dynamic Allocation Functions) are enabled. Only some +of the features from this TR are supported by @theglibc{}. +@end defvr + +@comment (none) @comment GNU @defvr Macro _GNU_SOURCE If you define this macro, everything is included: @w{ISO C89}, @w{ISO diff --git a/string/string.h b/string/string.h index c7f8fde7b8..57deaa4191 100644 --- a/string/string.h +++ b/string/string.h @@ -22,7 +22,8 @@ #ifndef _STRING_H #define _STRING_H 1 -#include +#define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION +#include __BEGIN_DECLS @@ -166,7 +167,8 @@ extern size_t strxfrm_l (char *__dest, const char *__src, size_t __n, __locale_t __l) __THROW __nonnull ((2, 4)); #endif -#if defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8 +#if (defined __USE_XOPEN_EXTENDED || defined __USE_XOPEN2K8 \ + || __GLIBC_USE (LIB_EXT2)) /* Duplicate S, returning an identical malloc'd string. */ extern char *strdup (const char *__s) __THROW __attribute_malloc__ __nonnull ((1)); @@ -175,7 +177,7 @@ extern char *strdup (const char *__s) /* Return a malloc'd copy of at most N bytes of STRING. The resultant string is terminated even if no null terminator appears before STRING[N]. */ -#if defined __USE_XOPEN2K8 +#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2) extern char *strndup (const char *__string, size_t __n) __THROW __attribute_malloc__ __nonnull ((1)); #endif diff --git a/wcsmbs/wchar.h b/wcsmbs/wchar.h index e4134167dc..9686fcde32 100644 --- a/wcsmbs/wchar.h +++ b/wcsmbs/wchar.h @@ -24,7 +24,8 @@ #if !defined __need_mbstate_t && !defined __need_wint_t # define _WCHAR_H 1 -# include +# define __GLIBC_INTERNAL_STARTING_HEADER_IMPLEMENTATION +# include #endif #ifdef _WCHAR_H @@ -574,10 +575,12 @@ extern wchar_t *wcpcpy (wchar_t *__restrict __dest, extern wchar_t *wcpncpy (wchar_t *__restrict __dest, const wchar_t *__restrict __src, size_t __n) __THROW; +#endif /* Wide character I/O functions. */ +#if defined __USE_XOPEN2K8 || __GLIBC_USE (LIB_EXT2) /* Like OPEN_MEMSTREAM, but the stream is wide oriented and produces a wide character string. */ extern __FILE *open_wmemstream (wchar_t **__bufloc, size_t *__sizeloc) __THROW; -- 2.11.4.GIT