diff mbox series

[bpf-next,3/9] libbpf: simplify endianness check

Message ID 20190529011426.1328736-4-andriin@fb.com
State Changes Requested
Delegated to: BPF Maintainers
Headers show
Series [bpf-next,1/9] libbpf: fix detection of corrupted BPF instructions section | expand

Commit Message

Andrii Nakryiko May 29, 2019, 1:14 a.m. UTC
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.

Signed-off-by: Andrii Nakryiko <andriin@fb.com>
---
 tools/lib/bpf/libbpf.c | 37 ++++++++++++-------------------------
 1 file changed, 12 insertions(+), 25 deletions(-)

Comments

Song Liu May 29, 2019, 5:09 p.m. UTC | #1
On Tue, May 28, 2019 at 6:14 PM Andrii Nakryiko <andriin@fb.com> wrote:
>
> 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.
>
> Signed-off-by: Andrii Nakryiko <andriin@fb.com>
Acked-by: Song Liu <songliubraving@fb.com>

> ---
>  tools/lib/bpf/libbpf.c | 37 ++++++++++++-------------------------
>  1 file changed, 12 insertions(+), 25 deletions(-)
>
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index 7b80b9ae8a1f..c98f9942fba4 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -607,31 +607,18 @@ static int bpf_object__elf_init(struct bpf_object *obj)
>         return err;
>  }
>
> -static int
> -bpf_object__check_endianness(struct bpf_object *obj)
> -{
> -       static unsigned int const endian = 1;
> -
> -       switch (obj->efile.ehdr.e_ident[EI_DATA]) {
> -       case ELFDATA2LSB:
> -               /* We are big endian, BPF obj is little endian. */
> -               if (*(unsigned char const *)&endian != 1)
> -                       goto mismatch;
> -               break;
> -
> -       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");
> +static int bpf_object__check_endianness(struct bpf_object *obj)
> +{
> +#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
> +       if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2LSB)
> +               return 0;
> +#elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
> +       if (obj->efile.ehdr.e_ident[EI_DATA] == ELFDATA2MSB)
> +               return 0;
> +#else
> +# error "Unrecognized __BYTE_ORDER__"
> +#endif
> +       pr_warning("endianness mismatch.\n");
>         return -LIBBPF_ERRNO__ENDIAN;
>  }
>
> --
> 2.17.1
>
diff mbox series

Patch

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