--only-keep-debug is used to create separate debug files. In GNU
objcopy, it makes non-SHT_NOTE SHF_ALLOC|SHT_GROUP sections SHT_NOBITS.
The intended use case is:
llvm-objcopy --only-keep-debug a a.dbg # or llvm-strip --only-keep-debug a -o a.dbg llvm-objcopy --strip-debug a b llvm-objcopy --add-gnu-debuglink=a.dbg b
The current sh_offset assignment algorithm is incapable of deleting
space used by sections that are made SHT_NOBITS. Add a new one
customized for --only-keep-debug.
After the rewrite of sh_offset fields, p_offset/p_filesz in program
headers are no longer correct. Just delete program headers. gdb and lldb
can still debug b.
We cannot delete the sections that are made SHT_NOBITS, because gdb will
// gdb/symfile.c:addr_info_make_relative warning: section .got not found in /tmp/a.dbg warning: section .bss.rel.ro not found in /tmp/c/ccls.dbg ...
lldb does not have the warnings.
GNU objcopy does some more things like making SHT_GROUP sections
SHT_NOBITS. --only-keep-debug is not intended for object files and
SHT_GROUP are not kept in ET_EXEC/ET_DYN. We don't copy the behavior.