xref: /openbsd-src/lib/libm/hidden/namespace.h (revision c9ad05d3055dda7d02ab88ed4f078c7f44e6d442)
1*c9ad05d3Sguenther /*	$OpenBSD: namespace.h,v 1.3 2018/03/12 06:19:19 guenther Exp $	*/
22f2c0062Sguenther 
32f2c0062Sguenther #ifndef _LIBM_NAMESPACE_H_
42f2c0062Sguenther #define _LIBM_NAMESPACE_H_
52f2c0062Sguenther 
62f2c0062Sguenther /*
72f2c0062Sguenther  * Copyright (c) 2015 Philip Guenther <guenther@openbsd.org>
82f2c0062Sguenther  *
92f2c0062Sguenther  * Permission to use, copy, modify, and distribute this software for any
102f2c0062Sguenther  * purpose with or without fee is hereby granted, provided that the above
112f2c0062Sguenther  * copyright notice and this permission notice appear in all copies.
122f2c0062Sguenther  *
132f2c0062Sguenther  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
142f2c0062Sguenther  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
152f2c0062Sguenther  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
162f2c0062Sguenther  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
172f2c0062Sguenther  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
182f2c0062Sguenther  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
192f2c0062Sguenther  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
202f2c0062Sguenther  */
212f2c0062Sguenther 
222f2c0062Sguenther /*
232f2c0062Sguenther  * The goal: calls from inside libc to other libc functions should be via
242f2c0062Sguenther  * identifiers that are of hidden visibility and--to avoid confusion--are
252f2c0062Sguenther  * in the reserved namespace.  By doing this these calls are protected
262f2c0062Sguenther  * from overriding by applications and on many platforms can avoid creation
272f2c0062Sguenther  * or use of GOT or PLT entries.  I've chosen a prefix of "_libm_" for this.
282f2c0062Sguenther  * These will not be declared directly; instead, the gcc "asm labels"
292f2c0062Sguenther  * extension will be used rename the function.
302f2c0062Sguenther  *
312f2c0062Sguenther  * In order to actually set up the desired asm labels, we use these in
322f2c0062Sguenther  * the internal .h files:
332f2c0062Sguenther  *   PROTO_NORMAL(x)		Symbols used both internally and externally
342f2c0062Sguenther  *	This makes gcc convert use of x to use _libm_x instead.  Use
352f2c0062Sguenther  *	DEF_STD(x) or DEF_NONSTD(x) to create the external alias.
362f2c0062Sguenther  *	ex: PROTO_NORMAL(ceil)
372f2c0062Sguenther  *
382f2c0062Sguenther  *   PROTO_STD_DEPRECATED(x)	Standard C symbols that are not used internally
392f2c0062Sguenther  * 	This just marks the symbol as deprecated, with no renaming.
402f2c0062Sguenther  *	Do not use DEF_*(x) with this.
412f2c0062Sguenther  *	ex: PROTO_STD_DEPRECATED(tgammal)
422f2c0062Sguenther  *
432f2c0062Sguenther  *   PROTO_DEPRECATED(x)	Symbols not in C that are not used internally
442f2c0062Sguenther  * 	This marks the symbol as deprecated and, in the static lib, weak.
452f2c0062Sguenther  *	No renaming is done.  Do not use DEF_*(x) with this.
462f2c0062Sguenther  *	ex: PROTO_DEPRECATED(creat)
472f2c0062Sguenther  *
482f2c0062Sguenther  * Finally, to create the expected aliases, we use these in the .c files
492f2c0062Sguenther  * where the definitions are:
502f2c0062Sguenther  *   DEF_STD(x)		Symbols reserved to or specified by ISO C
512f2c0062Sguenther  *	This defines x as a strong alias for _libm_x; this must only
522f2c0062Sguenther  *	be used for symbols that are reserved by the C standard
532f2c0062Sguenther  *	(or reserved in the external identifier namespace).
542f2c0062Sguenther  *	Matches with PROTO_NORMAL()
552f2c0062Sguenther  *	ex: DEF_STD(fopen)
562f2c0062Sguenther  *
572f2c0062Sguenther  *   DEF_NONSTD(x)		Symbols used internally and not in ISO C
582f2c0062Sguenther  *	This defines x as a alias for _libm_x, weak in the static version
592f2c0062Sguenther  *	Matches with PROTO_NORMAL()
602f2c0062Sguenther  *	ex: DEF_NONSTD(lseek)
612f2c0062Sguenther  *
622f2c0062Sguenther  *   LDBL_CLONE(x)		long double aliases that are used
632f2c0062Sguenther  *	This defines xl and _libm_xl as aliases for _libm_x.
642f2c0062Sguenther  *	Matches with LDBL_PROTO_NORMAL()
652f2c0062Sguenther  *
662f2c0062Sguenther  *   LDBL_UNUSED_CLONE(x)	long double aliases that are unused
672f2c0062Sguenther  *	This defines xl as an alias for _libm_x.
682f2c0062Sguenther  *	Matches with LDBL_PROTO_STD_DEPRECATED()
692f2c0062Sguenther  *
702f2c0062Sguenther  *   LDBL_MAYBE_CLONE(x)
712f2c0062Sguenther  *   LDBL_MAYBE_UNUSED_CLONE(x)
722f2c0062Sguenther  *	Like LDBL_CLONE() and LDBL_UNUSED_CLONE(), except they do nothing
732f2c0062Sguenther  *	if LDBL_MANT_DIG != DBL_MANT_DIG
742f2c0062Sguenther  *
752f2c0062Sguenther  *   MAKE_UNUSED_CLONE(dst, src)	Unused symbols that are exact clones
762f2c0062Sguenther  *					of other symbols
772f2c0062Sguenther  *	This declares dst as being the same type as dst, and makes
782f2c0062Sguenther  *	_libm_dst a strong, hidden alias for _libm_src.  You still need to
792f2c0062Sguenther  *	DEF_STD(dst) or DEF_NONSTD(dst) to alias dst itself
802f2c0062Sguenther  *	ex: MAKE_UNUSED_CLONE(nexttoward, nextafter);
812f2c0062Sguenther  */
822f2c0062Sguenther 
832f2c0062Sguenther #include <sys/cdefs.h>	/* for __dso_hidden and __{weak,strong}_alias */
842f2c0062Sguenther 
852f2c0062Sguenther #ifndef PIC
862f2c0062Sguenther # define WEAK_IN_STATIC_ALIAS(x,y)	__weak_alias(x,y)
872f2c0062Sguenther # define WEAK_IN_STATIC			__attribute__((weak))
882f2c0062Sguenther #else
892f2c0062Sguenther # define WEAK_IN_STATIC_ALIAS(x,y)	__strong_alias(x,y)
902f2c0062Sguenther # define WEAK_IN_STATIC			/* nothing */
912f2c0062Sguenther #endif
922f2c0062Sguenther 
932f2c0062Sguenther #define	HIDDEN(x)		_libm_##x
942f2c0062Sguenther #define	HIDDEN_STRING(x)	"_libm_" __STRING(x)
952f2c0062Sguenther 
962f2c0062Sguenther #define	PROTO_NORMAL(x)		__dso_hidden typeof(x) HIDDEN(x), x asm(HIDDEN_STRING(x))
972f2c0062Sguenther #define	PROTO_STD_DEPRECATED(x)	typeof(x) HIDDEN(x), x __attribute__((deprecated))
982f2c0062Sguenther #define PROTO_DEPRECATED(x)	PROTO_STD_DEPRECATED(x) WEAK_IN_STATIC
992f2c0062Sguenther 
1002f2c0062Sguenther #define	DEF_STD(x)		__strong_alias(x, HIDDEN(x))
1012f2c0062Sguenther #define DEF_NONSTD(x)		WEAK_IN_STATIC_ALIAS(x, HIDDEN(x))
1022f2c0062Sguenther 
1032f2c0062Sguenther #define	MAKE_UNUSED_CLONE(dst, src)	__strong_alias(dst, src)
1042f2c0062Sguenther #define LDBL_UNUSED_CLONE(x)		__strong_alias(x##l, HIDDEN(x))
1057ebae471Sguenther #define LDBL_NONSTD_UNUSED_CLONE(x)	WEAK_IN_STATIC_ALIAS(x##l, HIDDEN(x))
1062f2c0062Sguenther #define LDBL_CLONE(x)		LDBL_UNUSED_CLONE(x); \
1072f2c0062Sguenther 				__dso_hidden typeof(HIDDEN(x##l)) HIDDEN(x##l) \
1082f2c0062Sguenther 				__attribute__((alias (HIDDEN_STRING(x))))
109*c9ad05d3Sguenther #define LDBL_NONSTD_CLONE(x)	LDBL_NONSTD_UNUSED_CLONE(x); \
110*c9ad05d3Sguenther 				__dso_hidden typeof(HIDDEN(x##l)) HIDDEN(x##l) \
111*c9ad05d3Sguenther 				__attribute__((alias (HIDDEN_STRING(x))))
1122f2c0062Sguenther 
1132f2c0062Sguenther #if __LDBL_MANT_DIG__ == __DBL_MANT_DIG__
1142f2c0062Sguenther # define LDBL_PROTO_NORMAL(x)		typeof(x) HIDDEN(x)
1152f2c0062Sguenther # define LDBL_PROTO_STD_DEPRECATED(x)	typeof(x) HIDDEN(x)
1162f2c0062Sguenther # define LDBL_MAYBE_CLONE(x)		LDBL_CLONE(x)
1172f2c0062Sguenther # define LDBL_MAYBE_UNUSED_CLONE(x)	LDBL_UNUSED_CLONE(x)
1187ebae471Sguenther # define LDBL_MAYBE_NONSTD_UNUSED_CLONE(x)	LDBL_NONSTD_UNUSED_CLONE(x)
119*c9ad05d3Sguenther # define LDBL_MAYBE_NONSTD_CLONE(x)	LDBL_NONSTD_CLONE(x)
1202f2c0062Sguenther #else
1212f2c0062Sguenther # define LDBL_PROTO_NORMAL(x)		PROTO_NORMAL(x)
1222f2c0062Sguenther # define LDBL_PROTO_STD_DEPRECATED(x)	PROTO_STD_DEPRECATED(x)
1232f2c0062Sguenther # define LDBL_MAYBE_CLONE(x)		__asm("")
1242f2c0062Sguenther # define LDBL_MAYBE_UNUSED_CLONE(x)	__asm("")
1257ebae471Sguenther # define LDBL_MAYBE_NONSTD_UNUSED_CLONE(x)	__asm("")
126*c9ad05d3Sguenther # define LDBL_MAYBE_NONSTD_CLONE(x)	__asm("")
1272f2c0062Sguenther #endif
1282f2c0062Sguenther 
1292f2c0062Sguenther #endif	/* _LIBM_NAMESPACE_H_ */
130