Open mgorny opened 2 months ago
@llvm/issue-subscribers-tools-llvm-objcopy-strip
Author: Michał Górny (mgorny)
llvm-objcopy doesn't support -O binary
for non-ELF inputs currently (note that the -O
option is listed under ELF-specific currently in the command guide). I expect all it's doing is ignoring the option and doing regular object copying from input to output file (so from PE/COFF to PE/COFF). Somebody would need to add support for -O binary
to the COFF mode of llvm-objcopy for this to work.
I wish it failed rather than silently ignoring it and giving people non-booting systems, though.
A contribution would be welcome: we already have the framework for unsupported options (see https://github.com/llvm/llvm-project/blob/64cfce95d38d6884d501fd1ece959e7809a94025/llvm/lib/ObjCopy/ConfigManager.cpp#L16), but it looks like this one got missed for some reason. NB: I haven't actually sanity checked that the option is expected to be unsupported - I've only looked at the docs.
Using --dump-section
instead of -O
and -j
seems to work as a workaround:
nowa-gentoo-laptop nowa # objcopy -O binary -j.linux /efi/EFI/Linux/linux-6.10.7-gentoo-dist.efi /tmp/linux
nowa-gentoo-laptop nowa # llvm-objcopy -O binary -j.linux /efi/EFI/Linux/linux-6.10.7-gentoo-dist.efi /tmp/linux-llvm
nowa-gentoo-laptop nowa # diff /tmp/linux /tmp/linux-llvm
Binary files /tmp/linux and /tmp/linux-llvm differ
nowa-gentoo-laptop nowa # sbverify /tmp/linux --cert /root/kernel_key.pem
Signature verification OK
nowa-gentoo-laptop nowa # sbverify /tmp/linux-llvm --cert /root/kernel_key.pem
zsh: segmentation fault (core dumped) sbverify /tmp/linux-llvm --cert /root/kernel_key.pem
nowa-gentoo-laptop nowa # objcopy /efi/EFI/Linux/linux-6.10.7-gentoo-dist.efi --dump-section .linux=/tmp/linux-dumped
nowa-gentoo-laptop nowa # llvm-objcopy /efi/EFI/Linux/linux-6.10.7-gentoo-dist.efi --dump-section .linux=/tmp/linux-dumped-llvm
nowa-gentoo-laptop nowa # diff /tmp/linux-dumped /tmp/linux-dumped-llvm
nowa-gentoo-laptop nowa # sbverify /tmp/linux-dumped --cert /root/kernel_key.pem
Signature verification OK
nowa-gentoo-laptop nowa # sbverify /tmp/linux-dumped-llvm --cert /root/kernel_key.pem
Signature verification OK
In Gentoo, we're using
objcopy
to extract the Linux kernel image from UKI image.Reproducer:
Comparing the files created by GNU objcopy and LLVM objcopy:
The LLVM file has additional 512 bytes at the front:
And 16 bytes (of padding?) at the end:
In fact, if I run GNU objcopy without
-O binary
, I get roughly the same format as LLVM gives. This leads me to conclude that LLVM objcopy does not implement-O binary
correctly, and instead uses PE output, same as the original file.