blob: 10241341eff356b5cb4c41234362fe22804fc441 [file] [log] [blame]
Vasily Gorbik8bfe2732020-11-13 00:03:29 +01001/* SPDX-License-Identifier: GPL-2.0-or-later */
2#ifndef _OBJTOOL_ENDIANNESS_H
3#define _OBJTOOL_ENDIANNESS_H
4
Vasily Gorbik77860322020-11-13 00:03:32 +01005#include <arch/endianness.h>
Vasily Gorbik8bfe2732020-11-13 00:03:29 +01006#include <linux/kernel.h>
7#include <endian.h>
Vasily Gorbik8bfe2732020-11-13 00:03:29 +01008
9#ifndef __TARGET_BYTE_ORDER
10#error undefined arch __TARGET_BYTE_ORDER
11#endif
12
13#if __BYTE_ORDER != __TARGET_BYTE_ORDER
14#define __NEED_BSWAP 1
15#else
16#define __NEED_BSWAP 0
17#endif
18
19/*
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.
24 */
25#define bswap_if_needed(val) \
26({ \
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; \
32 default: \
33 BUILD_BUG(); break; \
34 } \
35 __ret; \
36})
37
38#endif /* _OBJTOOL_ENDIANNESS_H */