diff options
| author | Andrii Nakryiko <andriin@fb.com> | 2019-05-29 13:36:05 -0400 |
|---|---|---|
| committer | Daniel Borkmann <daniel@iogearbox.net> | 2019-05-29 19:23:35 -0400 |
| commit | 12ef5634a855eb642b4d28781526fbd830aa7f7a (patch) | |
| tree | 4a6e09b6c1fc44bf51d12c6dddc923f9f190f58b /tools/lib | |
| parent | be5c5d4e9d8cbdd7fcdcbc45e86e34bc0bd1cefd (diff) | |
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>
Diffstat (limited to 'tools/lib')
| -rw-r--r-- | tools/lib/bpf/libbpf.c | 37 |
1 files changed, 12 insertions, 25 deletions
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 40690b7e500e..fb296eadc3f2 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c | |||
| @@ -607,31 +607,18 @@ errout: | |||
| 607 | return err; | 607 | return err; |
| 608 | } | 608 | } |
| 609 | 609 | ||
| 610 | static int | 610 | static int bpf_object__check_endianness(struct bpf_object *obj) |
| 611 | bpf_object__check_endianness(struct bpf_object *obj) | 611 | { |
| 612 | { | 612 | #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ |
| 613 | static unsigned int const endian = 1; | 613 | if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB) |
| 614 | 614 | return 0; | |
| 615 | switch (obj->efile.ehdr.e_ident[EI_DATA]) { | 615 | #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ |
| 616 | case ELFDATA2LSB: | 616 | if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB) |
| 617 | /* We are big endian, BPF obj is little endian. */ | 617 | return 0; |
| 618 | if (*(unsigned char const *)&endian != 1) | 618 | #else |
| 619 | goto mismatch; | 619 | # error "Unrecognized __BYTE_ORDER__" |
| 620 | break; | 620 | #endif |
| 621 | 621 | pr_warning("endianness mismatch.\n"); | |
| 622 | case ELFDATA2MSB: | ||
| 623 | /* We are little endian, BPF obj is big endian. */ | ||
| 624 | if (*(unsigned char const *)&endian != 0) | ||
| 625 | goto mismatch; | ||
| 626 | break; | ||
| 627 | default: | ||
| 628 | return -LIBBPF_ERRNO__ENDIAN; | ||
| 629 | } | ||
| 630 | |||
| 631 | return 0; | ||
| 632 | |||
| 633 | mismatch: | ||
| 634 | pr_warning("Error: endianness mismatch.\n"); | ||
| 635 | return -LIBBPF_ERRNO__ENDIAN; | 622 | return -LIBBPF_ERRNO__ENDIAN; |
| 636 | } | 623 | } |
| 637 | 624 | ||
