--- b/include/Makefile +++ b/include/Makefile @@ -11,8 +11,8 @@ SUBDIR= arpa protocols rpcsvc rpc xlocale SUBDIR_PARALLEL= INCS= a.out.h ar.h assert.h bitstring.h complex.h cpio.h _ctype.h ctype.h \ db.h \ - dirent.h dlfcn.h elf.h elf-hints.h err.h fmtmsg.h fnmatch.h fstab.h \ - fts.h ftw.h getopt.h glob.h grp.h \ + dirent.h dlfcn.h elf.h elf-hints.h endian.h err.h fmtmsg.h fnmatch.h \ + fstab.h fts.h ftw.h getopt.h glob.h grp.h \ ieeefp.h ifaddrs.h \ inttypes.h iso646.h kenv.h langinfo.h libgen.h limits.h link.h \ locale.h malloc.h malloc_np.h memory.h monetary.h mpool.h mqueue.h \ --- /dev/null +++ b/include/endian.h @@ -0,0 +1,56 @@ +/*- + * Copyright (c) 2021 M Warner Losh + * + * 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. Linux doesn't define the hto* [bl]*toh macros when there's + * a strict name space. We request that the FreeBSD-specific + * [lb]e{16,32,64}{enc,dec} be omitted as well. + */ +#define __OMIT_ENDIAN_ENCODE_DECODE +#include + +/* + * The bswap* functions cause problems for some Linux code (like mesa), so + * remove them from the name space as well. You need to include sys/endian.h + * directly if you want these (or the other FreeBSD-specific macros) to be + * visible. + */ +#undef bswap16 +#undef bswap32 +#undef bswap64 + +/* + * 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_ */ --- b/sys/sys/endian.h +++ b/sys/sys/endian.h @@ -55,6 +55,7 @@ typedef __uint64_t uint64_t; #define _UINT64_T_DECLARED #endif +#if __BSD_VISIBLE /* * General byte order swapping functions. */ @@ -67,16 +68,16 @@ typedef __uint64_t uint64_t; * endian to host byte order functions as detailed in byteorder(9). */ #if _BYTE_ORDER == _LITTLE_ENDIAN -#define htobe16(x) bswap16((x)) -#define htobe32(x) bswap32((x)) -#define htobe64(x) bswap64((x)) +#define htobe16(x) __bswap16((x)) +#define htobe32(x) __bswap32((x)) +#define htobe64(x) __bswap64((x)) #define htole16(x) ((uint16_t)(x)) #define htole32(x) ((uint32_t)(x)) #define htole64(x) ((uint64_t)(x)) -#define be16toh(x) bswap16((x)) -#define be32toh(x) bswap32((x)) -#define be64toh(x) bswap64((x)) +#define be16toh(x) __bswap16((x)) +#define be32toh(x) __bswap32((x)) +#define be64toh(x) __bswap64((x)) #define le16toh(x) ((uint16_t)(x)) #define le32toh(x) ((uint32_t)(x)) #define le64toh(x) ((uint64_t)(x)) @@ -84,20 +85,20 @@ typedef __uint64_t uint64_t; #define htobe16(x) ((uint16_t)(x)) #define htobe32(x) ((uint32_t)(x)) #define htobe64(x) ((uint64_t)(x)) -#define htole16(x) bswap16((x)) -#define htole32(x) bswap32((x)) -#define htole64(x) bswap64((x)) +#define htole16(x) __bswap16((x)) +#define htole32(x) __bswap32((x)) +#define htole64(x) __bswap64((x)) #define be16toh(x) ((uint16_t)(x)) #define be32toh(x) ((uint32_t)(x)) #define be64toh(x) ((uint64_t)(x)) -#define le16toh(x) bswap16((x)) -#define le32toh(x) bswap32((x)) -#define le64toh(x) bswap64((x)) +#define le16toh(x) __bswap16((x)) +#define le32toh(x) __bswap32((x)) +#define le64toh(x) __bswap64((x)) #endif /* _BYTE_ORDER == _LITTLE_ENDIAN */ /* Alignment-agnostic encode/decode bytestream to/from little/big endian. */ - +#ifndef __OMIT_ENDIAN_ENCODE_DECODE static __inline uint16_t be16dec(const void *pp) { @@ -203,5 +204,6 @@ le64enc(void *pp, uint64_t u) le32enc(p, (uint32_t)(u & 0xffffffffU)); le32enc(p + 4, (uint32_t)(u >> 32)); } - +#endif /* __OMIT_ENDIAN_ENCODE_DECODE */ +#endif /* __BSD_VISIBLE */ #endif /* _SYS_ENDIAN_H_ */