objtool: Fix x86 orc generation on big endian cross-compiles
authorVasily Gorbik <gor@linux.ibm.com>
Thu, 12 Nov 2020 23:03:29 +0000 (00:03 +0100)
committerJosh Poimboeuf <jpoimboe@redhat.com>
Thu, 14 Jan 2021 00:13:13 +0000 (18:13 -0600)
Correct objtool orc generation endianness problems to enable fully
functional x86 cross-compiles on big endian hardware.

Introduce bswap_if_needed() macro, which does a byte swap if target
endianness doesn't match the host, i.e. cross-compilation for little
endian on big endian and vice versa.  The macro is used for conversion
of multi-byte values which are read from / about to be written to a
target native endianness ELF file.

Signed-off-by: Vasily Gorbik <gor@linux.ibm.com>
Acked-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
arch/x86/include/asm/orc_types.h
tools/arch/x86/include/asm/orc_types.h
tools/objtool/arch/x86/include/arch_endianness.h [new file with mode: 0644]
tools/objtool/check.c
tools/objtool/endianness.h [new file with mode: 0644]
tools/objtool/orc_dump.c
tools/objtool/orc_gen.c
tools/objtool/special.c

index fdbffec4cfdeaba82e7fe6c1c76b169316cbb308..5a2baf28a1dcdaf546cd213e4d0eef389a43ad50 100644 (file)
@@ -40,6 +40,8 @@
 #define ORC_REG_MAX                    15
 
 #ifndef __ASSEMBLY__
+#include <asm/byteorder.h>
+
 /*
  * This struct is more or less a vastly simplified version of the DWARF Call
  * Frame Information standard.  It contains only the necessary parts of DWARF
 struct orc_entry {
        s16             sp_offset;
        s16             bp_offset;
+#if defined(__LITTLE_ENDIAN_BITFIELD)
        unsigned        sp_reg:4;
        unsigned        bp_reg:4;
        unsigned        type:2;
        unsigned        end:1;
+#elif defined(__BIG_ENDIAN_BITFIELD)
+       unsigned        bp_reg:4;
+       unsigned        sp_reg:4;
+       unsigned        unused:5;
+       unsigned        end:1;
+       unsigned        type:2;
+#endif
 } __packed;
 
 #endif /* __ASSEMBLY__ */
index fdbffec4cfdeaba82e7fe6c1c76b169316cbb308..5a2baf28a1dcdaf546cd213e4d0eef389a43ad50 100644 (file)
@@ -40,6 +40,8 @@
 #define ORC_REG_MAX                    15
 
 #ifndef __ASSEMBLY__
+#include <asm/byteorder.h>
+
 /*
  * This struct is more or less a vastly simplified version of the DWARF Call
  * Frame Information standard.  It contains only the necessary parts of DWARF
 struct orc_entry {
        s16             sp_offset;
        s16             bp_offset;
+#if defined(__LITTLE_ENDIAN_BITFIELD)
        unsigned        sp_reg:4;
        unsigned        bp_reg:4;
        unsigned        type:2;
        unsigned        end:1;
+#elif defined(__BIG_ENDIAN_BITFIELD)
+       unsigned        bp_reg:4;
+       unsigned        sp_reg:4;
+       unsigned        unused:5;
+       unsigned        end:1;
+       unsigned        type:2;
+#endif
 } __packed;
 
 #endif /* __ASSEMBLY__ */
diff --git a/tools/objtool/arch/x86/include/arch_endianness.h b/tools/objtool/arch/x86/include/arch_endianness.h
new file mode 100644 (file)
index 0000000..7c36252
--- /dev/null
@@ -0,0 +1,9 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _ARCH_ENDIANNESS_H
+#define _ARCH_ENDIANNESS_H
+
+#include <endian.h>
+
+#define __TARGET_BYTE_ORDER __LITTLE_ENDIAN
+
+#endif /* _ARCH_ENDIANNESS_H */
index 270adc38d896b4fc3d4c41eed8bf45995eb41af3..8cda0ef0652219dea7edfdf0d270a96a8faeba1e 100644 (file)
@@ -13,6 +13,7 @@
 #include "special.h"
 #include "warn.h"
 #include "arch_elf.h"
+#include "endianness.h"
 
 #include <linux/objtool.h>
 #include <linux/hashtable.h>
@@ -1435,7 +1436,7 @@ static int read_unwind_hints(struct objtool_file *file)
                cfa = &insn->cfi.cfa;
 
                if (hint->type == UNWIND_HINT_TYPE_RET_OFFSET) {
-                       insn->ret_offset = hint->sp_offset;
+                       insn->ret_offset = bswap_if_needed(hint->sp_offset);
                        continue;
                }
 
@@ -1447,7 +1448,7 @@ static int read_unwind_hints(struct objtool_file *file)
                        return -1;
                }
 
-               cfa->offset = hint->sp_offset;
+               cfa->offset = bswap_if_needed(hint->sp_offset);
                insn->cfi.type = hint->type;
                insn->cfi.end = hint->end;
        }
diff --git a/tools/objtool/endianness.h b/tools/objtool/endianness.h
new file mode 100644 (file)
index 0000000..ebece31
--- /dev/null
@@ -0,0 +1,38 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+#ifndef _OBJTOOL_ENDIANNESS_H
+#define _OBJTOOL_ENDIANNESS_H
+
+#include <linux/kernel.h>
+#include <endian.h>
+#include "arch_endianness.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
+
+/*
+ * Does a byte swap if target 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)                                           \
+({                                                                     \
+       __typeof__(val) __ret;                                          \
+       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;    \
+       default:                                                        \
+               BUILD_BUG(); break;                                     \
+       }                                                               \
+       __ret;                                                          \
+})
+
+#endif /* _OBJTOOL_ENDIANNESS_H */
index 5e6a95368d351a181d0bdfd35927602b9b5ae076..4e818a22e44b90a8bb96715ed331534f2dbc6f8a 100644 (file)
@@ -8,6 +8,7 @@
 #include <asm/orc_types.h>
 #include "objtool.h"
 #include "warn.h"
+#include "endianness.h"
 
 static const char *reg_name(unsigned int reg)
 {
@@ -197,11 +198,11 @@ int orc_dump(const char *_objname)
 
                printf(" sp:");
 
-               print_reg(orc[i].sp_reg, orc[i].sp_offset);
+               print_reg(orc[i].sp_reg, bswap_if_needed(orc[i].sp_offset));
 
                printf(" bp:");
 
-               print_reg(orc[i].bp_reg, orc[i].bp_offset);
+               print_reg(orc[i].bp_reg, bswap_if_needed(orc[i].bp_offset));
 
                printf(" type:%s end:%d\n",
                       orc_type_name(orc[i].type), orc[i].end);
index 9ce68b385a1b81f43789a86cb3720e3e9485348c..1be7e16b25950fab17971dbfde8116f345ce659e 100644 (file)
@@ -11,6 +11,7 @@
 
 #include "check.h"
 #include "warn.h"
+#include "endianness.h"
 
 int create_orc(struct objtool_file *file)
 {
@@ -96,6 +97,8 @@ static int create_orc_entry(struct elf *elf, struct section *u_sec, struct secti
        /* populate ORC data */
        orc = (struct orc_entry *)u_sec->data->d_buf + idx;
        memcpy(orc, o, sizeof(*orc));
+       orc->sp_offset = bswap_if_needed(orc->sp_offset);
+       orc->bp_offset = bswap_if_needed(orc->bp_offset);
 
        /* populate reloc for ip */
        reloc = malloc(sizeof(*reloc));
index 1a2420febd08a4f48e954490f92676d99466a54d..ab7cb1e13411f6e4f8db73d28c2afaa9bf5c1abb 100644 (file)
@@ -15,6 +15,7 @@
 #include "special.h"
 #include "warn.h"
 #include "arch_special.h"
+#include "endianness.h"
 
 struct special_entry {
        const char *sec;
@@ -77,8 +78,9 @@ static int get_alt_entry(struct elf *elf, struct special_entry *entry,
        if (entry->feature) {
                unsigned short feature;
 
-               feature = *(unsigned short *)(sec->data->d_buf + offset +
-                                             entry->feature);
+               feature = bswap_if_needed(*(unsigned short *)(sec->data->d_buf +
+                                                             offset +
+                                                             entry->feature));
                arch_handle_alternative(feature, alt);
        }