Skip to contentSkip to conversation

objtool and cross compilation

RFC PATCH Superseded Vasily Gorbik 4 participants 9 messages Sep 30, 2020

Subsystems (2)

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

cover objtool and cross compilation

This is based on v5.9-rc7, before "other architectures support" patches starting pouring in.

Currently objtool seems to be the only tool from build tools needed which breaks x86 cross compilation on big endian systems.

But besides x86 cross compilation, endianness awareness is also needed for big endian architectures objtool support in general.

We have working prototype of objtool support and orc unwinder for s390 made originally by Martin Schwidefsky. I'm trying to bring it in shape again and refactor to share more code with "generic" part.

But first things first. These 2 patches point to endianness problems which should be addressed. And I'd be glad to get any ideas how to make them less ugly.

New "other architectures support" patches currently move only some problematic parts into x86 arch specific folder. But the main problem is that arch/x86/lib/insn.c and arch/x86/include/asm/insn.h are shared across the kernel source and the tools, and there is no common way to address endianness problems.

Since big endian stuff is only needed for the objtool and not for the kernel I can try to hide alternative big endian definitions in tools only header which is included only if __KERNEL__ is not defined. But that kind of defeats the idea of sharing those files 1 to 1 with tools.

Thoughts? Any suggestions are welcome.

Martin Schwidefsky (1):
objtool: x86 instruction decoder and big endian cross compiles

Vasily Gorbik (1):
objtool: fix x86 orc generation on big endian cross compiles

 arch/x86/include/asm/insn.h            | 43 ++++++++++++
 arch/x86/include/asm/orc_types.h       | 24 +++++++
 arch/x86/lib/insn.c                    | 95 +++++++++++---------------
 tools/arch/x86/include/asm/insn.h      | 43 ++++++++++++
 tools/arch/x86/include/asm/orc_types.h | 24 +++++++
 tools/arch/x86/lib/insn.c              | 95 +++++++++++---------------
 tools/objtool/check.c                  |  4 +-
 tools/objtool/elf.c                    | 34 +++++----
 tools/objtool/orc_dump.c               |  4 +-
 tools/objtool/orc_gen.c                |  2 +
 tools/objtool/special.c                |  4 +-
 11 files changed, 243 insertions(+), 129 deletions(-)
Signature · 6 lines
⣿⣿⣿⣿⢋⡀⣀⠹⣿⣿⣿⣿
⣿⣿⣿⣿⠠⣶⡦⠀⣿⣿⣿⣿
⣿⣿⣿⠏⣴⣮⣴⣧⠈⢿⣿⣿
⣿⣿⡏⢰⣿⠖⣠⣿⡆⠈⣿⣿
⣿⢛⣵⣄⠙⣶⣶⡟⣅⣠⠹⣿
⣿⣜⣛⠻⢎⣉⣉⣀⠿⣫⣵⣿
#1 lore raw

RFC PATCH 1/2 objtool: x86 instruction decoder and big endian cross compiles

From: Martin Schwidefsky <[email protected]>

Make the x86 instruction decoder of the objtool usable on big endian machines. This is useful for compile tests on non x86, big endian hardware.

[ gor: more endianness problems findings fixes / rebasing ]
---
 arch/x86/include/asm/insn.h       | 43 ++++++++++++++
 arch/x86/lib/insn.c               | 95 +++++++++++++------------------
 tools/arch/x86/include/asm/insn.h | 43 ++++++++++++++
 tools/arch/x86/lib/insn.c         | 95 +++++++++++++------------------
 tools/objtool/check.c             |  4 +-
 tools/objtool/elf.c               | 34 ++++++-----
 tools/objtool/special.c           |  4 +-
 7 files changed, 191 insertions(+), 127 deletions(-)
arch/x86/include/asm/insn.h +43 -0
index 5c1ae3eff9d4..8d9864b09552 100644
@@ -8,8 +8,17 @@
  */
 
 /* insn_attr_t is defined in inat.h */
+#ifdef __KERNEL__
+#include <linux/swab.h>
+#include <asm/byteorder.h>
+#else
+#include <endian.h>
+#endif
 #include <asm/inat.h>
 
+#if defined(__BYTE_ORDER) ? \
+	__BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
+
 struct insn_field {
 	union {
 		insn_value_t value;
@@ -20,6 +29,40 @@ struct insn_field {
 	unsigned char nbytes;
 };
 
+static inline void insn_field_set(struct insn_field *p, insn_value_t v,
+				  unsigned char n)
+{
+	p->value = v;
+	p->nbytes = n;
+}
+
+#else
+
+struct insn_field {
+	insn_value_t value;
+	union {
+		insn_value_t little;
+		insn_byte_t bytes[4];
+	};
+	/* !0 if we've run insn_get_xxx() for this field */
+	unsigned char got;
+	unsigned char nbytes;
+};
+
+static inline void insn_field_set(struct insn_field *p, insn_value_t v,
+				  unsigned char n)
+{
+	p->value = v;
+#ifdef __KERNEL__
+	p->little = __swap32(v);
+#else
+	p->little = __bswap_32(v);
+#endif
+	p->nbytes = n;
+}
+
+#endif
+
 struct insn {
 	struct insn_field prefixes;	/*
 					 * Prefixes
arch/x86/lib/insn.c +41 -54

208 lines not on this page · raw message

tools/arch/x86/include/asm/insn.h +43 -0

57 lines not on this page · raw message

tools/arch/x86/lib/insn.c +41 -54

208 lines not on this page · raw message

tools/objtool/check.c +2 -2
index 90a66891441a..010a8296e381 100644
@@ -1344,7 +1344,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 = le16toh(hint->sp_offset);
 			continue;
 		}
 
@@ -1381,7 +1381,7 @@ static int read_unwind_hints(struct objtool_file *file)
 			return -1;
 		}
 
-		cfa->offset = hint->sp_offset;
+		cfa->offset = le16toh(hint->sp_offset);
 		insn->cfi.type = hint->type;
 		insn->cfi.end = hint->end;
 	}
tools/objtool/elf.c +19 -15
index 3ddbd66f1a37..6e77735a7505 100644
@@ -829,25 +829,27 @@ static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
 {
 	struct reloc *reloc;
 	int idx = 0, size;
-	GElf_Rel *relocs;
+	void *buf;
 
 	/* Allocate a buffer for relocations */
-	size = nr * sizeof(*relocs);
-	relocs = malloc(size);
-	if (!relocs) {
+	size = nr * sizeof(GElf_Rel);
+	buf = malloc(size);
+	if (!buf) {
 		perror("malloc");
 		return -1;
 	}
 
-	sec->data->d_buf = relocs;
+	sec->data->d_buf = buf;
 	sec->data->d_size = size;
+	sec->data->d_type = ELF_T_REL;
 
 	sec->sh.sh_size = size;
 
 	idx = 0;
 	list_for_each_entry(reloc, &sec->reloc_list, list) {
-		relocs[idx].r_offset = reloc->offset;
-		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		reloc->rel.r_offset = reloc->offset;
+		reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		gelf_update_rel(sec->data, idx, &reloc->rel);
 		idx++;
 	}
 
@@ -858,26 +860,28 @@ static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
 {
 	struct reloc *reloc;
 	int idx = 0, size;
-	GElf_Rela *relocs;
+	void *buf;
 
 	/* Allocate a buffer for relocations with addends */
-	size = nr * sizeof(*relocs);
-	relocs = malloc(size);
-	if (!relocs) {
+	size = nr * sizeof(GElf_Rela);
+	buf = malloc(size);
+	if (!buf) {
 		perror("malloc");
 		return -1;
 	}
 
-	sec->data->d_buf = relocs;
+	sec->data->d_buf = buf;
 	sec->data->d_size = size;
+	sec->data->d_type = ELF_T_RELA;
 
 	sec->sh.sh_size = size;
 
 	idx = 0;
 	list_for_each_entry(reloc, &sec->reloc_list, list) {
-		relocs[idx].r_offset = reloc->offset;
-		relocs[idx].r_addend = reloc->addend;
-		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		reloc->rela.r_offset = reloc->offset;
+		reloc->rela.r_addend = reloc->addend;
+		reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		gelf_update_rela(sec->data, idx, &reloc->rela);
 		idx++;
 	}
 
tools/objtool/special.c +2 -2
index e893f1e48e44..230f14f766b4 100644
@@ -90,8 +90,8 @@ 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 = le16toh(*(unsigned short *)(sec->data->d_buf +
+						      offset + entry->feature));
 
 		/*
 		 * It has been requested that we don't validate the !POPCNT
Signature · 6 lines
⣿⣿⣿⣿⢋⡀⣀⠹⣿⣿⣿⣿
⣿⣿⣿⣿⠠⣶⡦⠀⣿⣿⣿⣿
⣿⣿⣿⠏⣴⣮⣴⣧⠈⢿⣿⣿
⣿⣿⡏⢰⣿⠖⣠⣿⡆⠈⣿⣿
⣿⢛⣵⣄⠙⣶⣶⡟⣅⣠⠹⣿
⣿⣜⣛⠻⢎⣉⣉⣀⠿⣫⣵⣿
#2 lore raw

RFC PATCH 2/2 objtool: fix x86 orc generation on big endian cross compiles

In reply to Vasily Gorbik

Correct objtool orc generation endianness problems to enable fully functional x86 cross compiles on big endian hardware.

---
 arch/x86/include/asm/orc_types.h       | 24 ++++++++++++++++++++++++
 tools/arch/x86/include/asm/orc_types.h | 24 ++++++++++++++++++++++++
 tools/objtool/orc_dump.c               |  4 ++--
 tools/objtool/orc_gen.c                |  2 ++
 4 files changed, 52 insertions(+), 2 deletions(-)
arch/x86/include/asm/orc_types.h +24 -0
index d25534940bde..931f99b70af3 100644
@@ -61,6 +61,13 @@
 #define UNWIND_HINT_TYPE_RET_OFFSET	3
 
 #ifndef __ASSEMBLY__
+#ifdef __KERNEL__
+#include <linux/swab.h>
+#include <asm/byteorder.h>
+#else
+#include <endian.h>
+#endif
+
 /*
  * 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
@@ -69,6 +76,9 @@
  * the stack for a given code address.  Each instance of the struct corresponds
  * to one or more code locations.
  */
+#if defined(__BYTE_ORDER) ? \
+	__BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
+
 struct orc_entry {
 	s16		sp_offset;
 	s16		bp_offset;
@@ -78,6 +88,20 @@ struct orc_entry {
 	unsigned	end:1;
 } __packed;
 
+#else
+
+struct orc_entry {
+	s16		sp_offset;
+	s16		bp_offset;
+	unsigned	bp_reg:4;
+	unsigned	sp_reg:4;
+	unsigned	unused:5;
+	unsigned	end:1;
+	unsigned	type:2;
+} __packed;
+
+#endif
+
 /*
  * This struct is used by asm and inline asm code to manually annotate the
  * location of registers on the stack for the ORC unwinder.
tools/arch/x86/include/asm/orc_types.h +24 -0
index d25534940bde..931f99b70af3 100644
@@ -61,6 +61,13 @@
 #define UNWIND_HINT_TYPE_RET_OFFSET	3
 
 #ifndef __ASSEMBLY__
+#ifdef __KERNEL__
+#include <linux/swab.h>
+#include <asm/byteorder.h>
+#else
+#include <endian.h>
+#endif
+
 /*
  * 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
@@ -69,6 +76,9 @@
  * the stack for a given code address.  Each instance of the struct corresponds
  * to one or more code locations.
  */
+#if defined(__BYTE_ORDER) ? \
+	__BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
+
 struct orc_entry {
 	s16		sp_offset;
 	s16		bp_offset;
@@ -78,6 +88,20 @@ struct orc_entry {
 	unsigned	end:1;
 } __packed;
 
+#else
+
+struct orc_entry {
+	s16		sp_offset;
+	s16		bp_offset;
+	unsigned	bp_reg:4;
+	unsigned	sp_reg:4;
+	unsigned	unused:5;
+	unsigned	end:1;
+	unsigned	type:2;
+} __packed;
+
+#endif
+
 /*
  * This struct is used by asm and inline asm code to manually annotate the
  * location of registers on the stack for the ORC unwinder.
tools/objtool/orc_dump.c +2 -2
index fca46e006fc2..0fbf8521c891 100644
@@ -196,11 +196,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, (s16)le16toh(orc[i].sp_offset));
 
 		printf(" bp:");
 
-		print_reg(orc[i].bp_reg, orc[i].bp_offset);
+		print_reg(orc[i].bp_reg, (s16)le16toh(orc[i].bp_offset));
 
 		printf(" type:%s end:%d\n",
 		       orc_type_name(orc[i].type), orc[i].end);
tools/objtool/orc_gen.c +2 -0
index 968f55e6dd94..b3978fad93e6 100644
@@ -90,6 +90,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 = htole16(orc->sp_offset);
+	orc->bp_offset = htole16(orc->bp_offset);
 
 	/* populate reloc for ip */
 	reloc = malloc(sizeof(*reloc));
Signature · 6 lines
⣿⣿⣿⣿⢋⡀⣀⠹⣿⣿⣿⣿
⣿⣿⣿⣿⠠⣶⡦⠀⣿⣿⣿⣿
⣿⣿⣿⠏⣴⣮⣴⣧⠈⢿⣿⣿
⣿⣿⡏⢰⣿⠖⣠⣿⡆⠈⣿⣿
⣿⢛⣵⣄⠙⣶⣶⡟⣅⣠⠹⣿
⣿⣜⣛⠻⢎⣉⣉⣀⠿⣫⣵⣿
maintainer
#3 lore raw
In reply to Vasily Gorbik
Quoted 2 lines · Vasily Gorbik

This is based on v5.9-rc7, before "other architectures support" patches starting pouring in.

Please base on: tip/objtool/core, as is they no longer apply.

#4 lore raw

v2 cover objtool and cross compilation

rfc v1 - rfc v2:
- rebased onto tip/objtool/core
- reformatted couple of lines

Currently objtool seems to be the only tool from build tools needed which breaks x86 cross compilation on big endian systems.

But besides x86 cross compilation, endianness awareness is also needed for big endian architectures objtool support in general.

We have working prototype of objtool support and orc unwinder for s390 made originally by Martin Schwidefsky. I'm trying to bring it in shape again and refactor to share more code with "generic" part.

But first things first. These 2 patches point to endianness problems which should be addressed. And I'd be glad to get any ideas how to make them less ugly.

New "other architectures support" patches currently move only some problematic parts into x86 arch specific folder. But the main problem is that arch/x86/lib/insn.c and arch/x86/include/asm/insn.h are shared across the kernel source and the tools, and there is no common way to address endianness problems.

Since big endian stuff is only needed for the objtool and not for the kernel I can try to hide alternative big endian definitions in tools only header which is included only if __KERNEL__ is not defined. But that kind of defeats the idea of sharing those files 1 to 1 with tools.

Thoughts? Any suggestions are welcome.

Martin Schwidefsky (1):
objtool: x86 instruction decoder and big endian cross compiles

Vasily Gorbik (1):
objtool: fix x86 orc generation on big endian cross compiles

 arch/x86/include/asm/insn.h            | 42 ++++++++++++
 arch/x86/include/asm/orc_types.h       | 24 +++++++
 arch/x86/lib/insn.c                    | 95 +++++++++++---------------
 tools/arch/x86/include/asm/insn.h      | 42 ++++++++++++
 tools/arch/x86/include/asm/orc_types.h | 24 +++++++
 tools/arch/x86/lib/insn.c              | 95 +++++++++++---------------
 tools/objtool/arch/x86/special.c       |  2 +-
 tools/objtool/check.c                  |  4 +-
 tools/objtool/elf.c                    | 34 +++++----
 tools/objtool/orc_dump.c               |  4 +-
 tools/objtool/orc_gen.c                |  2 +
 11 files changed, 240 insertions(+), 128 deletions(-)
Signature · 6 lines
⣿⣿⣿⣿⢋⡀⣀⠹⣿⣿⣿⣿
⣿⣿⣿⣿⠠⣶⡦⠀⣿⣿⣿⣿
⣿⣿⣿⠏⣴⣮⣴⣧⠈⢿⣿⣿
⣿⣿⡏⢰⣿⠖⣠⣿⡆⠈⣿⣿
⣿⢛⣵⣄⠙⣶⣶⡟⣅⣠⠹⣿
⣿⣜⣛⠻⢎⣉⣉⣀⠿⣫⣵⣿
#5 lore raw

RFC PATCH v2 1/2 objtool: x86 instruction decoder and big endian cross compiles

From: Martin Schwidefsky <[email protected]>

Make the x86 instruction decoder of the objtool usable on big endian machines. This is useful for compile tests on non x86, big endian hardware.

[ gor: more endianness problems findings fixes / rebasing ]
---
 arch/x86/include/asm/insn.h       | 42 ++++++++++++++
 arch/x86/lib/insn.c               | 95 +++++++++++++------------------
 tools/arch/x86/include/asm/insn.h | 42 ++++++++++++++
 tools/arch/x86/lib/insn.c         | 95 +++++++++++++------------------
 tools/objtool/arch/x86/special.c  |  2 +-
 tools/objtool/check.c             |  4 +-
 tools/objtool/elf.c               | 34 ++++++-----
 7 files changed, 188 insertions(+), 126 deletions(-)
arch/x86/include/asm/insn.h +42 -0
index 5c1ae3eff9d4..e5a2bcc41ac4 100644
@@ -8,8 +8,16 @@
  */
 
 /* insn_attr_t is defined in inat.h */
+#ifdef __KERNEL__
+#include <linux/swab.h>
+#include <asm/byteorder.h>
+#else
+#include <endian.h>
+#endif
 #include <asm/inat.h>
 
+#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
+
 struct insn_field {
 	union {
 		insn_value_t value;
@@ -20,6 +28,40 @@ struct insn_field {
 	unsigned char nbytes;
 };
 
+static inline void insn_field_set(struct insn_field *p, insn_value_t v,
+				  unsigned char n)
+{
+	p->value = v;
+	p->nbytes = n;
+}
+
+#else
+
+struct insn_field {
+	insn_value_t value;
+	union {
+		insn_value_t little;
+		insn_byte_t bytes[4];
+	};
+	/* !0 if we've run insn_get_xxx() for this field */
+	unsigned char got;
+	unsigned char nbytes;
+};
+
+static inline void insn_field_set(struct insn_field *p, insn_value_t v,
+				  unsigned char n)
+{
+	p->value = v;
+#ifdef __KERNEL__
+	p->little = __swap32(v);
+#else
+	p->little = __bswap_32(v);
+#endif
+	p->nbytes = n;
+}
+
+#endif
+
 struct insn {
 	struct insn_field prefixes;	/*
 					 * Prefixes
arch/x86/lib/insn.c +41 -54

208 lines not on this page · raw message

tools/arch/x86/include/asm/insn.h +42 -0

56 lines not on this page · raw message

tools/arch/x86/lib/insn.c +41 -54

208 lines not on this page · raw message

tools/objtool/arch/x86/special.c +1 -1
index fd4af88c0ea5..287ddf6d8a9e 100644
@@ -9,7 +9,7 @@
 
 void arch_handle_alternative(unsigned short feature, struct special_alt *alt)
 {
-	switch (feature) {
+	switch (le16toh(feature)) {
 	case X86_FEATURE_SMAP:
 		/*
 		 * If UACCESS validation is enabled; force that alternative;
tools/objtool/check.c +2 -2
index 2df9f769412e..6f2574bf0008 100644
@@ -1370,7 +1370,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 = le16toh(hint->sp_offset);
 			continue;
 		}
 
@@ -1382,7 +1382,7 @@ static int read_unwind_hints(struct objtool_file *file)
 			return -1;
 		}
 
-		cfa->offset = hint->sp_offset;
+		cfa->offset = le16toh(hint->sp_offset);
 		insn->cfi.type = hint->type;
 		insn->cfi.end = hint->end;
 	}
tools/objtool/elf.c +19 -15
index 4e1d7460574b..5c0341b0cde3 100644
@@ -829,25 +829,27 @@ static int elf_rebuild_rel_reloc_section(struct section *sec, int nr)
 {
 	struct reloc *reloc;
 	int idx = 0, size;
-	GElf_Rel *relocs;
+	void *buf;
 
 	/* Allocate a buffer for relocations */
-	size = nr * sizeof(*relocs);
-	relocs = malloc(size);
-	if (!relocs) {
+	size = nr * sizeof(GElf_Rel);
+	buf = malloc(size);
+	if (!buf) {
 		perror("malloc");
 		return -1;
 	}
 
-	sec->data->d_buf = relocs;
+	sec->data->d_buf = buf;
 	sec->data->d_size = size;
+	sec->data->d_type = ELF_T_REL;
 
 	sec->sh.sh_size = size;
 
 	idx = 0;
 	list_for_each_entry(reloc, &sec->reloc_list, list) {
-		relocs[idx].r_offset = reloc->offset;
-		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		reloc->rel.r_offset = reloc->offset;
+		reloc->rel.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		gelf_update_rel(sec->data, idx, &reloc->rel);
 		idx++;
 	}
 
@@ -858,26 +860,28 @@ static int elf_rebuild_rela_reloc_section(struct section *sec, int nr)
 {
 	struct reloc *reloc;
 	int idx = 0, size;
-	GElf_Rela *relocs;
+	void *buf;
 
 	/* Allocate a buffer for relocations with addends */
-	size = nr * sizeof(*relocs);
-	relocs = malloc(size);
-	if (!relocs) {
+	size = nr * sizeof(GElf_Rela);
+	buf = malloc(size);
+	if (!buf) {
 		perror("malloc");
 		return -1;
 	}
 
-	sec->data->d_buf = relocs;
+	sec->data->d_buf = buf;
 	sec->data->d_size = size;
+	sec->data->d_type = ELF_T_RELA;
 
 	sec->sh.sh_size = size;
 
 	idx = 0;
 	list_for_each_entry(reloc, &sec->reloc_list, list) {
-		relocs[idx].r_offset = reloc->offset;
-		relocs[idx].r_addend = reloc->addend;
-		relocs[idx].r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		reloc->rela.r_offset = reloc->offset;
+		reloc->rela.r_addend = reloc->addend;
+		reloc->rela.r_info = GELF_R_INFO(reloc->sym->idx, reloc->type);
+		gelf_update_rela(sec->data, idx, &reloc->rela);
 		idx++;
 	}
 
Signature · 6 lines
⣿⣿⣿⣿⢋⡀⣀⠹⣿⣿⣿⣿
⣿⣿⣿⣿⠠⣶⡦⠀⣿⣿⣿⣿
⣿⣿⣿⠏⣴⣮⣴⣧⠈⢿⣿⣿
⣿⣿⡏⢰⣿⠖⣠⣿⡆⠈⣿⣿
⣿⢛⣵⣄⠙⣶⣶⡟⣅⣠⠹⣿
⣿⣜⣛⠻⢎⣉⣉⣀⠿⣫⣵⣿
#6 lore raw

Re: [RFC PATCH v2 1/2] objtool: x86 instruction decoder and big endian cross compiles

Quoted 10 lines · Vasily Gorbik

From: Martin Schwidefsky <[email protected]>

Make the x86 instruction decoder of the objtool usable on big endian machines. This is useful for compile tests on non x86, big endian hardware.

[ gor: more endianness problems findings fixes / rebasing ]
Quoted 9 lines
---
 arch/x86/include/asm/insn.h       | 42 ++++++++++++++
 arch/x86/lib/insn.c               | 95 +++++++++++++------------------
 tools/arch/x86/include/asm/insn.h | 42 ++++++++++++++
 tools/arch/x86/lib/insn.c         | 95 +++++++++++++------------------
 tools/objtool/arch/x86/special.c  |  2 +-
 tools/objtool/check.c             |  4 +-
 tools/objtool/elf.c               | 34 ++++++-----
 7 files changed, 188 insertions(+), 126 deletions(-)

Can the insn changes be done in a separate patch from the objtool changes?

Also, 'Masami Hiramatsu <[email protected]>' should be on Cc for the insn changes since he wrote the original code.

Signature · 1 line
Josh
#7 lore raw

RFC PATCH v2 2/2 objtool: fix x86 orc generation on big endian cross compiles

In reply to Vasily Gorbik

Correct objtool orc generation endianness problems to enable fully functional x86 cross compiles on big endian hardware.

---
 arch/x86/include/asm/orc_types.h       | 24 ++++++++++++++++++++++++
 tools/arch/x86/include/asm/orc_types.h | 24 ++++++++++++++++++++++++
 tools/objtool/orc_dump.c               |  4 ++--
 tools/objtool/orc_gen.c                |  2 ++
 4 files changed, 52 insertions(+), 2 deletions(-)
arch/x86/include/asm/orc_types.h +24 -0
index fdbffec4cfde..c72bee8e6ec0 100644
@@ -40,6 +40,13 @@
 #define ORC_REG_MAX			15
 
 #ifndef __ASSEMBLY__
+#ifdef __KERNEL__
+#include <linux/swab.h>
+#include <asm/byteorder.h>
+#else
+#include <endian.h>
+#endif
+
 /*
  * 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
@@ -48,6 +55,9 @@
  * the stack for a given code address.  Each instance of the struct corresponds
  * to one or more code locations.
  */
+
+#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
+
 struct orc_entry {
 	s16		sp_offset;
 	s16		bp_offset;
@@ -57,6 +67,20 @@ struct orc_entry {
 	unsigned	end:1;
 } __packed;
 
+#else
+
+struct orc_entry {
+	s16		sp_offset;
+	s16		bp_offset;
+	unsigned	bp_reg:4;
+	unsigned	sp_reg:4;
+	unsigned	unused:5;
+	unsigned	end:1;
+	unsigned	type:2;
+} __packed;
+
+#endif
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ORC_TYPES_H */
tools/arch/x86/include/asm/orc_types.h +24 -0
index fdbffec4cfde..c72bee8e6ec0 100644
@@ -40,6 +40,13 @@
 #define ORC_REG_MAX			15
 
 #ifndef __ASSEMBLY__
+#ifdef __KERNEL__
+#include <linux/swab.h>
+#include <asm/byteorder.h>
+#else
+#include <endian.h>
+#endif
+
 /*
  * 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
@@ -48,6 +55,9 @@
  * the stack for a given code address.  Each instance of the struct corresponds
  * to one or more code locations.
  */
+
+#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
+
 struct orc_entry {
 	s16		sp_offset;
 	s16		bp_offset;
@@ -57,6 +67,20 @@ struct orc_entry {
 	unsigned	end:1;
 } __packed;
 
+#else
+
+struct orc_entry {
+	s16		sp_offset;
+	s16		bp_offset;
+	unsigned	bp_reg:4;
+	unsigned	sp_reg:4;
+	unsigned	unused:5;
+	unsigned	end:1;
+	unsigned	type:2;
+} __packed;
+
+#endif
+
 #endif /* __ASSEMBLY__ */
 
 #endif /* _ORC_TYPES_H */
tools/objtool/orc_dump.c +2 -2
index 5e6a95368d35..19fa6f65040e 100644
@@ -197,11 +197,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, (s16)le16toh(orc[i].sp_offset));
 
 		printf(" bp:");
 
-		print_reg(orc[i].bp_reg, orc[i].bp_offset);
+		print_reg(orc[i].bp_reg, (s16)le16toh(orc[i].bp_offset));
 
 		printf(" type:%s end:%d\n",
 		       orc_type_name(orc[i].type), orc[i].end);
tools/objtool/orc_gen.c +2 -0
index 235663b96adc..ab9cff93fabd 100644
@@ -96,6 +96,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 = htole16(orc->sp_offset);
+	orc->bp_offset = htole16(orc->bp_offset);
 
 	/* populate reloc for ip */
 	reloc = malloc(sizeof(*reloc));
Signature · 6 lines
⣿⣿⣿⣿⢋⡀⣀⠹⣿⣿⣿⣿
⣿⣿⣿⣿⠠⣶⡦⠀⣿⣿⣿⣿
⣿⣿⣿⠏⣴⣮⣴⣧⠈⢿⣿⣿
⣿⣿⡏⢰⣿⠖⣠⣿⡆⠈⣿⣿
⣿⢛⣵⣄⠙⣶⣶⡟⣅⣠⠹⣿
⣿⣜⣛⠻⢎⣉⣉⣀⠿⣫⣵⣿
#8 lore raw

RE: [RFC PATCH v2 2/2] objtool: fix x86 orc generation on big endian cross compiles

From: Vasily Gorbik

Quoted 5 lines

Sent: 30 September 2020 13:24

Correct objtool orc generation endianness problems to enable fully functional x86 cross compiles on big endian hardware.

...

Quoted 13 lines
arch/x86/include/asm/orc_types.h +6 -0
+
+#if defined(__BYTE_ORDER) ? __BYTE_ORDER == __LITTLE_ENDIAN : defined(__LITTLE_ENDIAN)
+
 struct orc_entry {
 	s16		sp_offset;
 	s16		bp_offset;
@@ -57,6 +67,20 @@ struct orc_entry {
 	unsigned	end:1;
 } __packed;
 
+#else
+
+struct orc_entry {

arch/x86/include/asm/orc_types.h struct orc_entry { · line 82

Quoted 10 lines
arch/x86/include/asm/orc_types.h +10 -0
+	s16		sp_offset;
+	s16		bp_offset;
+	unsigned	bp_reg:4;
+	unsigned	sp_reg:4;
+	unsigned	unused:5;
+	unsigned	end:1;
+	unsigned	type:2;
+} __packed;
+
+#endif

Shouldn't that be checking BITFIELD_ENDIAN.

I also think that (in the other patch) you can assume that you don't need to byteswap if __KERNEL__ is defined.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)