tools/lib/bpf: add pr_warn() to more -EINVAL cases

Message ID 20231207180919.2379718-1-slyich@gmail.com
State New
Headers
Series tools/lib/bpf: add pr_warn() to more -EINVAL cases |

Commit Message

Sergei Trofimovich Dec. 7, 2023, 6:09 p.m. UTC
  Before the change on `i686-linux` `systemd` build failed as:

    $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)

After the change it fails as:

    $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    libbpf: ELF section #9 has inconsistent alignment in src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
    Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)

Now it's slightly easier to figure out what is wrong with an ELF file.

CC: Alexei Starovoitov <ast@kernel.org>
CC: Daniel Borkmann <daniel@iogearbox.net>
CC: Andrii Nakryiko <andrii@kernel.org>
CC: Martin KaFai Lau <martin.lau@linux.dev>
CC: Song Liu <song@kernel.org>
CC: Yonghong Song <yonghong.song@linux.dev>
CC: John Fastabend <john.fastabend@gmail.com>
CC: KP Singh <kpsingh@kernel.org>
CC: Stanislav Fomichev <sdf@google.com>
CC: Hao Luo <haoluo@google.com>
CC: Jiri Olsa <jolsa@kernel.org>
CC: bpf@vger.kernel.org
Signed-off-by: Sergei Trofimovich <slyich@gmail.com>
---
 tools/lib/bpf/linker.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)
  

Comments

Eduard Zingerman Dec. 7, 2023, 10:58 p.m. UTC | #1
On Thu, 2023-12-07 at 18:09 +0000, Sergei Trofimovich wrote:
> Before the change on `i686-linux` `systemd` build failed as:
> 
>     $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)
> 
> After the change it fails as:
> 
>     $ bpftool gen object src/core/bpf/socket_bind/socket-bind.bpf.o src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     libbpf: ELF section #9 has inconsistent alignment in src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o
>     Error: failed to link 'src/core/bpf/socket_bind/socket-bind.bpf.unstripped.o': Invalid argument (22)
> 
> Now it's slightly easier to figure out what is wrong with an ELF file.

Hi Sergei,

Thank you for adding these prints.
Could you please make a few adjustments, as noted below.
Also, please add "libbpf:" prefix in subject and mention
linker_sanity_check_elf in it, e.g.:

  libbpf: add pr_warn() for EINVAL cases in linker_sanity_check_elf
  
or something like that.

[...]

> diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
> index 5ced96d99f8c..71bb4916b762 100644
> --- a/tools/lib/bpf/linker.c
> +++ b/tools/lib/bpf/linker.c
> @@ -719,13 +719,22 @@ static int linker_sanity_check_elf(struct src_obj *obj)
>  			return -EINVAL;
>  		}
>  
> -		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
> +		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) {
> +			pr_warn("ELF section #%zu alignment is non pow-of-2 alignment in %s\n",
> +				sec->sec_idx, obj->filename);

Could you please also print values for shdr->sh_addralign here?
And also print shdr->sh_addralign/data->d_align, shdr->sh_size/data->d_size
in corresponding pr_warn() calls below.

>  			return -EINVAL;
> -		if (sec->shdr->sh_addralign != sec->data->d_align)
> +		}
> +		if (sec->shdr->sh_addralign != sec->data->d_align) {
> +			pr_warn("ELF section #%zu has inconsistent alignment in %s\n",
> +				sec->sec_idx, obj->filename);
>  			return -EINVAL;
> +		}
>  
> -		if (sec->shdr->sh_size != sec->data->d_size)
> +		if (sec->shdr->sh_size != sec->data->d_size) {
> +			pr_warn("ELF section #%zu has inconsistent section size in %s\n",
> +				sec->sec_idx, obj->filename);
>  			return -EINVAL;
> +		}
>  
>  		switch (sec->shdr->sh_type) {
>  		case SHT_SYMTAB:

A few lines below this one there is:

		case SHT_PROGBITS:
			if (sec->shdr->sh_flags & SHF_EXECINSTR) {
				if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0)
					return -EINVAL;
			}
			break;

Could you please add pr_warn() there as well?
  

Patch

diff --git a/tools/lib/bpf/linker.c b/tools/lib/bpf/linker.c
index 5ced96d99f8c..71bb4916b762 100644
--- a/tools/lib/bpf/linker.c
+++ b/tools/lib/bpf/linker.c
@@ -719,13 +719,22 @@  static int linker_sanity_check_elf(struct src_obj *obj)
 			return -EINVAL;
 		}
 
-		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
+		if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) {
+			pr_warn("ELF section #%zu alignment is non pow-of-2 alignment in %s\n",
+				sec->sec_idx, obj->filename);
 			return -EINVAL;
-		if (sec->shdr->sh_addralign != sec->data->d_align)
+		}
+		if (sec->shdr->sh_addralign != sec->data->d_align) {
+			pr_warn("ELF section #%zu has inconsistent alignment in %s\n",
+				sec->sec_idx, obj->filename);
 			return -EINVAL;
+		}
 
-		if (sec->shdr->sh_size != sec->data->d_size)
+		if (sec->shdr->sh_size != sec->data->d_size) {
+			pr_warn("ELF section #%zu has inconsistent section size in %s\n",
+				sec->sec_idx, obj->filename);
 			return -EINVAL;
+		}
 
 		switch (sec->shdr->sh_type) {
 		case SHT_SYMTAB: