[RFC PATCH v1] objtool: Use target file endianness instead of a compiled constant
Peter Zijlstra
peterz at infradead.org
Thu Mar 31 19:08:14 AEDT 2022
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?
More information about the Linuxppc-dev
mailing list