Merge tag 'powerpc-6.2-1' of git://git.kernel.org/pub/scm/linux/kernel/git/powerpc...
[sfrench/cifs-2.6.git] / tools / objtool / include / objtool / endianness.h
index 10241341eff356b5cb4c41234362fe22804fc441..4d2aa9b0fe2fd5351691395d11b28270f7efc5df 100644 (file)
@@ -2,33 +2,33 @@
 #ifndef _OBJTOOL_ENDIANNESS_H
 #define _OBJTOOL_ENDIANNESS_H
 
-#include <arch/endianness.h>
 #include <linux/kernel.h>
 #include <endian.h>
-
-#ifndef __TARGET_BYTE_ORDER
-#error undefined arch __TARGET_BYTE_ORDER
-#endif
-
-#if __BYTE_ORDER != __TARGET_BYTE_ORDER
-#define __NEED_BSWAP 1
-#else
-#define __NEED_BSWAP 0
-#endif
+#include <objtool/elf.h>
 
 /*
- * Does a byte swap if target endianness doesn't match the host, i.e. cross
+ * Does a byte swap if target file endianness doesn't match the host, i.e. cross
  * compilation for little endian on big endian and vice versa.
  * To be used for multi-byte values conversion, which are read from / about
  * to be written to a target native endianness ELF file.
  */
-#define bswap_if_needed(val)                                           \
+static inline bool need_bswap(struct elf *elf)
+{
+       return (__BYTE_ORDER == __LITTLE_ENDIAN) ^
+              (elf->ehdr.e_ident[EI_DATA] == ELFDATA2LSB);
+}
+
+#define bswap_if_needed(elf, val)                                      \
 ({                                                                     \
        __typeof__(val) __ret;                                          \
+       bool __need_bswap = need_bswap(elf);                            \
        switch (sizeof(val)) {                                          \
-       case 8: __ret = __NEED_BSWAP ? bswap_64(val) : (val); break;    \
-       case 4: __ret = __NEED_BSWAP ? bswap_32(val) : (val); break;    \
-       case 2: __ret = __NEED_BSWAP ? bswap_16(val) : (val); break;    \
+       case 8:                                                         \
+               __ret = __need_bswap ? bswap_64(val) : (val); break;    \
+       case 4:                                                         \
+               __ret = __need_bswap ? bswap_32(val) : (val); break;    \
+       case 2:                                                         \
+               __ret = __need_bswap ? bswap_16(val) : (val); break;    \
        default:                                                        \
                BUILD_BUG(); break;                                     \
        }                                                               \