[llvm-objcopy][ELF] --only-keep-debug: set offset/size of segments with no sections...
authorFangrui Song <i@maskray.me>
Wed, 5 May 2021 17:26:57 +0000 (10:26 -0700)
committerFangrui Song <i@maskray.me>
Wed, 5 May 2021 17:26:57 +0000 (10:26 -0700)
commitb3336bfa2e6a38f16c4ecf4d77bd0f97ec5a46eb
tree76189759352b4c638793e3d859569be328570dd8
parentba5c122647c79586a6d060ca649e586feb7f57a0
[llvm-objcopy][ELF] --only-keep-debug: set offset/size of segments with no sections to zero

PR50160: we currently ignore non-PT_PHDR segments with no sections, not
accounting for its p_offset and p_filesz: this can cause an out-of-bounds write
in `writeSegmentData` if the p_offset+p_filesz is larger than the total file
size.

This can be fixed by setting p_offset=p_filesz=0. The logic nicely unifies with
the logic added in D90897.

Reviewed By: jhenderson, rupprecht

Differential Revision: https://reviews.llvm.org/D101560
llvm/test/tools/llvm-objcopy/ELF/only-keep-debug.test
llvm/tools/llvm-objcopy/ELF/Object.cpp