[PATCH] D75949: [llvm-objcopy] Allow empty sections in --dump-section

Jordan Rupprecht via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Mar 10 10:53:37 PDT 2020


rupprecht created this revision.
Herald added subscribers: llvm-commits, abrachet, MaskRay, sunfish, aheejin, sbc100, emaste.
Herald added a reviewer: espindola.
Herald added a reviewer: alexshap.
Herald added a reviewer: jhenderson.
Herald added a project: LLVM.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75949

Files:
  llvm/test/tools/llvm-objcopy/ELF/dump-section-empty.test
  llvm/test/tools/llvm-objcopy/ELF/dump-section.test
  llvm/tools/llvm-objcopy/ELF/ELFObjcopy.cpp
  llvm/tools/llvm-objcopy/ELF/Object.cpp
  llvm/tools/llvm-objcopy/ELF/Object.h
  llvm/tools/llvm-objcopy/MachO/MachOObjcopy.cpp
  llvm/tools/llvm-objcopy/wasm/WasmObjcopy.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75949.249450.patch
Type: text/x-patch
Size: 6801 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200310/a5f92097/attachment.bin>


More information about the llvm-commits mailing list