Skip to content

Commit 32fa058

Browse files
trofianakryiko
authored andcommitted
libbpf: Add pr_warn() for EINVAL cases in linker_sanity_check_elf
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 addr=8 != d=4 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. Signed-off-by: Sergei Trofimovich <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Acked-by: Eduard Zingerman <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 09115c3 commit 32fa058

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

tools/lib/bpf/linker.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -719,13 +719,25 @@ static int linker_sanity_check_elf(struct src_obj *obj)
719719
return -EINVAL;
720720
}
721721

722-
if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign))
722+
if (sec->shdr->sh_addralign && !is_pow_of_2(sec->shdr->sh_addralign)) {
723+
pr_warn("ELF section #%zu alignment %llu is non pow-of-2 alignment in %s\n",
724+
sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign,
725+
obj->filename);
723726
return -EINVAL;
724-
if (sec->shdr->sh_addralign != sec->data->d_align)
727+
}
728+
if (sec->shdr->sh_addralign != sec->data->d_align) {
729+
pr_warn("ELF section #%zu has inconsistent alignment addr=%llu != d=%llu in %s\n",
730+
sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign,
731+
(long long unsigned)sec->data->d_align, obj->filename);
725732
return -EINVAL;
733+
}
726734

727-
if (sec->shdr->sh_size != sec->data->d_size)
735+
if (sec->shdr->sh_size != sec->data->d_size) {
736+
pr_warn("ELF section #%zu has inconsistent section size sh=%llu != d=%llu in %s\n",
737+
sec->sec_idx, (long long unsigned)sec->shdr->sh_size,
738+
(long long unsigned)sec->data->d_size, obj->filename);
728739
return -EINVAL;
740+
}
729741

730742
switch (sec->shdr->sh_type) {
731743
case SHT_SYMTAB:
@@ -737,8 +749,12 @@ static int linker_sanity_check_elf(struct src_obj *obj)
737749
break;
738750
case SHT_PROGBITS:
739751
if (sec->shdr->sh_flags & SHF_EXECINSTR) {
740-
if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0)
752+
if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0) {
753+
pr_warn("ELF section #%zu has unexpected size alignment %llu in %s\n",
754+
sec->sec_idx, (long long unsigned)sec->shdr->sh_size,
755+
obj->filename);
741756
return -EINVAL;
757+
}
742758
}
743759
break;
744760
case SHT_NOBITS:

0 commit comments

Comments
 (0)