mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	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 <slyich@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20231208215100.435876-1-slyich@gmail.com
			
			
This commit is contained in:
		
							parent
							
								
									09115c33e6
								
							
						
					
					
						commit
						32fa058398
					
				
					 1 changed files with 20 additions and 4 deletions
				
			
		| 
						 | 
					@ -719,13 +719,25 @@ static int linker_sanity_check_elf(struct src_obj *obj)
 | 
				
			||||||
			return -EINVAL;
 | 
								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 %llu is non pow-of-2 alignment in %s\n",
 | 
				
			||||||
 | 
									sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign,
 | 
				
			||||||
 | 
									obj->filename);
 | 
				
			||||||
			return -EINVAL;
 | 
								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 addr=%llu != d=%llu in %s\n",
 | 
				
			||||||
 | 
									sec->sec_idx, (long long unsigned)sec->shdr->sh_addralign,
 | 
				
			||||||
 | 
									(long long unsigned)sec->data->d_align, obj->filename);
 | 
				
			||||||
			return -EINVAL;
 | 
								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 sh=%llu != d=%llu in %s\n",
 | 
				
			||||||
 | 
									sec->sec_idx, (long long unsigned)sec->shdr->sh_size,
 | 
				
			||||||
 | 
									(long long unsigned)sec->data->d_size, obj->filename);
 | 
				
			||||||
			return -EINVAL;
 | 
								return -EINVAL;
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		switch (sec->shdr->sh_type) {
 | 
							switch (sec->shdr->sh_type) {
 | 
				
			||||||
		case SHT_SYMTAB:
 | 
							case SHT_SYMTAB:
 | 
				
			||||||
| 
						 | 
					@ -737,9 +749,13 @@ static int linker_sanity_check_elf(struct src_obj *obj)
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case SHT_PROGBITS:
 | 
							case SHT_PROGBITS:
 | 
				
			||||||
			if (sec->shdr->sh_flags & SHF_EXECINSTR) {
 | 
								if (sec->shdr->sh_flags & SHF_EXECINSTR) {
 | 
				
			||||||
				if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0)
 | 
									if (sec->shdr->sh_size % sizeof(struct bpf_insn) != 0) {
 | 
				
			||||||
 | 
										pr_warn("ELF section #%zu has unexpected size alignment %llu in %s\n",
 | 
				
			||||||
 | 
											sec->sec_idx, (long long unsigned)sec->shdr->sh_size,
 | 
				
			||||||
 | 
											obj->filename);
 | 
				
			||||||
					return -EINVAL;
 | 
										return -EINVAL;
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
		case SHT_NOBITS:
 | 
							case SHT_NOBITS:
 | 
				
			||||||
			break;
 | 
								break;
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in a new issue