When running llvm-objcopy --dump-section=.foo=file, and .foo is an empty section, we currently either:
- Crash (in debug mode) for MachO/wasm
- Abort with a "empty section" type of error message for ELF
This patch changes the behavior to allow dumping an empty section to a file. The file created will be empty.
For ELF:
- If the type is SHT_NOBITS, then it really doesn't make sense to dump that kind of section, so continue to error. To be fully compatible with GNU objcopy (which also prints the message), we should not abort, but just print a warning and continue the rest of llvm-objcopy.
- If the type is something else (e.g. SHT_PROGBITS), but happens to have an empty size, then allow creating an empty file. This is incompatible with GNU objcopy (which prints a warning and doesn't create a file), but should be more useful; it means users can call llvm-objcopy without checking if the section is empty first.
This only adds test cases for ELF. For wasm, a binary was manually tested. For MachO, yaml2obj crashes when trying to create an empty section, so we'd need to fix that first.
Fixes llvm.org/PR45159
I think placing the test with ELF/dump-section.test is also fine.
You can add an empty section to the existing YAML in ELF/dump-section.test.