Skip to content

Commit 744ffdd

Browse files
nathanchancelinkjumper
authored andcommitted
kbuild: Fix changing ELF file type for output of gen_btf for big endian
commit e3a9ee9 upstream. Commit 90ceddc ("bpf: Support llvm-objcopy for vmlinux BTF") changed the ELF type of .btf.vmlinux.bin.o 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 updating the entire 16-bit e_type field rather than just a single byte, so that everything works correctly for all platforms and 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) While in the area, update the comment to mention that binutils 2.35+ matches LLD's behavior of rejecting an ET_EXEC input, which occurred after the comment was added. Cc: [email protected] Fixes: 90ceddc ("bpf: Support llvm-objcopy for vmlinux BTF") Link: llvm/llvm-project#75643 Suggested-by: Masahiro Yamada <[email protected]> Signed-off-by: Nathan Chancellor <[email protected]> Reviewed-by: Fangrui Song <[email protected]> Reviewed-by: Nicolas Schier <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Justin Stitt <[email protected]> Signed-off-by: Masahiro Yamada <[email protected]> [nathan: Fix silent conflict due to lack of 7d15369 in older trees] Signed-off-by: Nathan Chancellor <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4c97f49 commit 744ffdd

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

scripts/link-vmlinux.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,13 @@ gen_btf()
236236
${OBJCOPY} --only-section=.BTF --set-section-flags .BTF=alloc,readonly \
237237
--strip-all ${1} ${2} 2>/dev/null
238238
# Change e_type to ET_REL so that it can be used to link final vmlinux.
239-
# Unlike GNU ld, lld does not allow an ET_EXEC input.
240-
printf '\1' | dd of=${2} conv=notrunc bs=1 seek=16 status=none
239+
# GNU ld 2.35+ and lld do not allow an ET_EXEC input.
240+
if [ -n "${CONFIG_CPU_BIG_ENDIAN}" ]; then
241+
et_rel='\0\1'
242+
else
243+
et_rel='\1\0'
244+
fi
245+
printf "${et_rel}" | dd of=${2} conv=notrunc bs=1 seek=16 status=none
241246
}
242247

243248
# Create ${2} .S file with all symbols from the ${1} object file

0 commit comments

Comments
 (0)