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: Song Liu <songliubraving@fb.com>
Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
This commit is contained in:
Andrii Nakryiko 2019-05-29 10:36:05 -07:00 коммит произвёл Daniel Borkmann
Родитель be5c5d4e9d
Коммит 12ef5634a8
1 изменённых файлов: 11 добавлений и 24 удалений

Просмотреть файл

@ -607,31 +607,18 @@ errout:
return err; return err;
} }
static int static int bpf_object__check_endianness(struct bpf_object *obj)
bpf_object__check_endianness(struct bpf_object *obj)
{ {
static unsigned int const endian = 1; #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
switch (obj->efile.ehdr.e_ident[EI_DATA]) { return 0;
case ELFDATA2LSB: #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
/* We are big endian, BPF obj is little endian. */ if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
if (*(unsigned char const *)&endian != 1) return 0;
goto mismatch; #else
break; # error "Unrecognized __BYTE_ORDER__"
#endif
case ELFDATA2MSB: pr_warning("endianness mismatch.\n");
/* 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;
} }