public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Christophe Leroy <christophe.leroy@csgroup.eu>
Cc: Sathvika Vasireddy <sv@linux.ibm.com>,
	Josh Poimboeuf <jpoimboe@redhat.com>,
	linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org
Subject: Re: [RFC PATCH v1] objtool: Use target file endianness instead of a compiled constant
Date: Thu, 31 Mar 2022 10:08:14 +0200	[thread overview]
Message-ID: <20220331080814.GQ8939@worktop.programming.kicks-ass.net> (raw)
In-Reply-To: <7c503552c4bc1f0e1aa4775765f8f24e9d1b0a04.1648713067.git.christophe.leroy@csgroup.eu>

On Thu, Mar 31, 2022 at 09:52:07AM +0200, Christophe Leroy wrote:
> Some architectures like powerpc support both endianness, it's
> therefore not possible to fix the endianness via arch/endianness.h
> because there is no easy way to get the target endianness at
> build time.
> 
> Use the endianness recorded in the file objtool is working on.
> 

> +#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(GElf_Ehdr *ehdr)
> +{
> +	return (__BYTE_ORDER == __LITTLE_ENDIAN) ^
> +	       (ehdr->e_ident[EI_DATA] == ELFDATA2LSB);
> +}
> +
> +#define bswap_if_needed(ehdr, val)					\
>  ({									\
>  	__typeof__(val) __ret;						\
> +	bool __need_bswap = need_bswap(ehdr);				\
>  	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;					\
>  	}								\

Far less painfull that I imagined it would be,.. but I think I prefer
passing in elf, as opposed to elf->ehdr, would that work?

  reply	other threads:[~2022-03-31  8:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-31  7:52 [RFC PATCH v1] objtool: Use target file endianness instead of a compiled constant Christophe Leroy
2022-03-31  8:08 ` Peter Zijlstra [this message]
2022-03-31  8:10   ` Christophe Leroy

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220331080814.GQ8939@worktop.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=christophe.leroy@csgroup.eu \
    --cc=jpoimboe@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linuxppc-dev@lists.ozlabs.org \
    --cc=sv@linux.ibm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox