Commit 12ef5634 authored by Andrii Nakryiko's avatar Andrii Nakryiko Committed by Daniel Borkmann

libbpf: simplify endianness check

Rewrite endianness check to use "more canonical" way, using
compiler-defined macros, similar to few other places in libbpf. It also
is more obvious and shorter.
Acked-by: default avatarSong Liu <songliubraving@fb.com>
Signed-off-by: default avatarAndrii Nakryiko <andriin@fb.com>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent be5c5d4e
...@@ -607,31 +607,18 @@ static int bpf_object__elf_init(struct bpf_object *obj) ...@@ -607,31 +607,18 @@ static int bpf_object__elf_init(struct bpf_object *obj)
return err; return err;
} }
static int static int bpf_object__check_endianness(struct bpf_object *obj)
bpf_object__check_endianness(struct bpf_object *obj) {
{ #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
static unsigned int const endian = 1; if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
return 0;
switch (obj->efile.ehdr.e_ident[EI_DATA]) { #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
case ELFDATA2LSB: if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
/* We are big endian, BPF obj is little endian. */ return 0;
if (*(unsigned char const *)&endian != 1) #else
goto mismatch; # error "Unrecognized __BYTE_ORDER__"
break; #endif
pr_warning("endianness mismatch.\n");
case ELFDATA2MSB:
/* We are little endian, BPF obj is big endian. */
if (*(unsigned char const *)&endian != 0)
goto mismatch;
break;
default:
return -LIBBPF_ERRNO__ENDIAN;
}
return 0;
mismatch:
pr_warning("Error: endianness mismatch.\n");
return -LIBBPF_ERRNO__ENDIAN; return -LIBBPF_ERRNO__ENDIAN;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment