HardenedBSD/include/endian.h
Warner Losh 30e0d2a510 linux: For better compatibility, provide compatible endian.h
Add endian.h. This includes sys/endian.h and then adds extra defines
that glibc defines with double underscores for our
_{BIG,BYTE,LITTLE,PDP}_ENDIAN macros. We also define __FLOAT_WORD_ORDER
to be the same as _BYTE_ENDIAN since FreeBSD doesn't currently define
this, and the default with glibc is exactly this for our platforms.
Move common parts of endian.h and sys/endian.h into sys/_endian.h
to limit namespace pollution from endian.h

All this gives us good compatibility with Linux. There may be one or two
upstreams that haven't integrated the patches I tried to send up.

There are some minor differences:
	o The extra glibc macros are not defined. These are all
	  controlled with either __ at the start, or only defined
	  when glibc is being built. We also don't define macros
	  that are used internally in glibc that would pollute
	  the namespace.
	o For complete compatibility, this change must also be
	  paired with providing a glibc-compatible byteswap.h.

Sponsored by:		Netflix
Reviewed by:		mhorne, markj, jhb
Differential Revision:	https://reviews.freebsd.org/D31962
2023-01-20 16:32:45 -07:00

44 lines
1.2 KiB
C

/*-
* Copyright (c) 2021 M. Warner Losh <imp@FreeBSD.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
/*
* A mostly Linux/glibc-compatible endian.h
*/
#ifndef _ENDIAN_H_
#define _ENDIAN_H_
/*
* FreeBSD's sys/_endian.h is very close to the interface provided on Linux by
* glibc's endian.h.
*/
#include <sys/_endian.h>
/*
* glibc uses double underscore for these symbols. Define these unconditionally.
* The compiler defines __BYTE_ORDER__ these days, so we don't do anything
* with that since sys/endian.h defines _BYTE_ORDER based on it.
*/
#define __BIG_ENDIAN _BIG_ENDIAN
#define __BYTE_ORDER _BYTE_ORDER
#define __LITTLE_ENDIAN _LITTLE_ENDIAN
#define __PDP_ENDIAN _PDP_ENDIAN
/*
* FreeBSD's sys/endian.h and machine/endian.h doesn't define a separate
* byte order for floats. Use the host non-float byte order.
*/
#define __FLOAT_WORD_ORDER _BYTE_ORDER
/*
* We don't define BIG_ENDI, LITTLE_ENDI, HIGH_HALF and LOW_HALF macros that
* glibc's endian.h defines since those appear to be internal to internal to
* glibc. We also don't try to emulate the various helper macros that glibc
* uses to limit namespace visibility.
*/
#endif /* _ENDIAN_H_ */