Re: [PATCH] kbuild: Fix changing ELF file type for output of gen_btf for big endian

Kees Cook
2024-02-10T00:49:01Z

On Thu, Feb 08, 2024 at 01:21:06PM -0700, Nathan Chancellor wrote:
> Commit 90ceddcb4950 ("bpf: Support llvm-objcopy for vmlinux BTF")
> changed the ELF type of .btf.vmlinux.bin.o from ET_EXEC to ET_REL via
> dd, which works fine for little endian platforms:
>
> 00000000 7f 45 4c 46 02 01 01 00 00 00 00 00 00 00 00 00 |.ELF............|
> -00000010 03 00 b7 00 01 00 00 00 00 00 00 80 00 80 ff ff |................|
> +00000010 01 00 b7 00 01 00 00 00 00 00 00 80 00 80 ff ff |................|
>
> However, for big endian platforms, it changes the wrong byte, resulting
> in an invalid ELF file type, which ld.lld rejects:
>
> 00000000 7f 45 4c 46 02 02 01 00 00 00 00 00 00 00 00 00 |.ELF............|
> -00000010 00 03 00 16 00 00 00 01 00 00 00 00 00 10 00 00 |................|
> +00000010 01 03 00 16 00 00 00 01 00 00 00 00 00 10 00 00 |................|
>
> Type: <unknown>: 103
>
> ld.lld: error: .btf.vmlinux.bin.o: unknown file type
>
> Fix this by using a different seek value for dd when targeting big
> endian, so that the correct byte gets changed and everything works
> correctly for all linkers.
>
> 00000000 7f 45 4c 46 02 02 01 00 00 00 00 00 00 00 00 00 |.ELF............|
> -00000010 00 03 00 16 00 00 00 01 00 00 00 00 00 10 00 00 |................|
> +00000010 00 01 00 16 00 00 00 01 00 00 00 00 00 10 00 00 |................|
>
> Type: REL (Relocatable file)
>
> Cc: stable@vger.kernel.org
> Fixes: 90ceddcb4950 ("bpf: Support llvm-objcopy for vmlinux BTF")
> Link: https://github.com/llvm/llvm-project/pull/75643
> Signed-off-by: Nathan Chancellor <nathan@kernel.org>

Yeah, looks good to me.

Reviewed-by: Kees Cook <keescook@chromium.org>

--
Kees Cook