[PATCH] D67090: [llvm-objcopy] Add a new file offset assignment algorithm and support --only-keep-debug

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 3 03:23:14 PDT 2019


MaskRay created this revision.
MaskRay added reviewers: alexshap, grimar, jakehehrlich, jhenderson, rupprecht.
Herald added subscribers: llvm-commits, seiya, arichardson, emaste.
Herald added a reviewer: espindola.
Herald added a project: LLVM.

The current layout algorithm sets file offsets of segments first, then
file offsets of sections. If some sections are deleted or changed to
SHT_NOBITS, the current algorithm cannot update the p_filesz/p_memsz
fields.

--only-keep-debug is used to create separate debug files. It makes most (neither SHT_NOTE nor .debug*) sections SHT_NOBITS. The intended use case is:

  llvm-objcopy --only-keep-debug a a.dbg
  llvm-objcopy --strip-debug a b
  llvm-objcopy --add-gnu-debuglink=a.dbg b

The current layout algorithm is incapable of shrinking segments, so it
is not suitable for implementing the functionality.

This patch adds a new algorithm which processes sections first, then
segments. It bears a resemblance to lld/ELF/Writer.cpp.

Some updated tests:

- basic-only-keep-debug.test: deleted. --only-keep-debug is no longer a NOP.
- segment-shift.test: fixed a bug: p_offset of the second PT_LOAD should be kept as 0x3000 (not 0x2000).
- adjacent-segments.test, marker-segment.text, remove-section-in-segment.test, segment-test-remove-section.test, triple-overlap.test: the removed section does not take sh_offset ranges, so their position is taken by following sections
- preserve-segment-contents.test I'm not sure how to test this. They new layout algorithm no longer leaves holes in the file (the behavior matches GNU objcopy).

The old layoutSections and layoutSegments are renamed to
layoutBinarySections and layoutSegmentsForPartitions, respectively.
They are still used by BinaryWriter and
--extract-main-partition/--extract-partition. It is not straightforward
to migrate them to use the algorithm, so it is a TODO to unify them with
the new algorithm.


Repository:
  rL LLVM

https://reviews.llvm.org/D67090

Files:
  test/tools/llvm-objcopy/ELF/adjacent-segments.test
  test/tools/llvm-objcopy/ELF/basic-only-keep-debug.test
  test/tools/llvm-objcopy/ELF/marker-segment.test
  test/tools/llvm-objcopy/ELF/only-keep-debug.test
  test/tools/llvm-objcopy/ELF/remove-section-in-segment.test
  test/tools/llvm-objcopy/ELF/segment-shift.test
  test/tools/llvm-objcopy/ELF/segment-test-remove-section.test
  test/tools/llvm-objcopy/ELF/triple-overlap.test
  tools/llvm-objcopy/ELF/ELFObjcopy.cpp
  tools/llvm-objcopy/ELF/Object.cpp
  tools/llvm-objcopy/ELF/Object.h

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67090.218424.patch
Type: text/x-patch
Size: 24656 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190903/2424a92d/attachment.bin>


More information about the llvm-commits mailing list