1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _OBJTOOL_ENDIANNESS_H
3 #define _OBJTOOL_ENDIANNESS_H
5 #include <arch/endianness.h>
6 #include <linux/kernel.h>
9 #ifndef __TARGET_BYTE_ORDER
10 #error undefined arch __TARGET_BYTE_ORDER
13 #if __BYTE_ORDER != __TARGET_BYTE_ORDER
14 #define __NEED_BSWAP 1
16 #define __NEED_BSWAP 0
20 * Does a byte swap if target endianness doesn't match the host, i.e. cross
21 * compilation for little endian on big endian and vice versa.
22 * To be used for multi-byte values conversion, which are read from / about
23 * to be written to a target native endianness ELF file.
25 #define bswap_if_needed(val) \
27 __typeof__(val) __ret; \
28 switch (sizeof(val)) { \
29 case 8: __ret = __NEED_BSWAP ? bswap_64(val) : (val); break; \
30 case 4: __ret = __NEED_BSWAP ? bswap_32(val) : (val); break; \
31 case 2: __ret = __NEED_BSWAP ? bswap_16(val) : (val); break; \
38 #endif /* _OBJTOOL_ENDIANNESS_H */