[PATCH] D42872: Fix handling of zero-size segments in llvm-objcopy

James Henderson via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 7 01:48:28 PST 2018


jhenderson requested changes to this revision.
jhenderson added a comment.
This revision now requires changes to proceed.

In https://reviews.llvm.org/D42872#1000085, @jakehehrlich wrote:

> I added James Henderson as I seem to recall we had this issue some time ago and apparently I added the problem back again somewhere along the way. I seem to recall James had an opinion on what counted as a dummy segment. Knowing past me I probably conservatively said a dummy segment is a segment with MemSize zero. I think the less conservative answer of if FileSize is zero is better now though. Alternatively we could try and make a comprehensive list of every dummy PT_* type
>
> @jhenderson, @ruiu  what do you think? what should indicate that the offset of a segment should be left alone?


I don't remember the details of our original discussion, but we cannot simply rely on FileSize being zero. Imagine a data segment with only .bss. It would need a file offset and address, and a MemSize, but no file size. At the very least, we need to pay attention to both. In the current state, such a segment will not be moved, which would be incorrect.

Also, we cannot rely on just the MemSize either, since although it is not common, I know of at least one target that has segments with no memory size, but which do have a file size - such segments contain informational contents, beyond just being a marker.

In https://reviews.llvm.org/D42872#1000077, @jakehehrlich wrote:

> What tells us that a segment is a marker segment? I think FileSize == 0 is probably a good choice but I want to make sure we don't later have to come back and fix this.


I don't think there is any well-defined method for this, beyond knowing about specific segment types (possible by looking at the documentation for all known program headers). However, segments that are completely empty (both for file and memory contents) can probably all be harmlessly put at offset 0, or really anywhere, I guess.

In https://reviews.llvm.org/D42872#1000077, @jakehehrlich wrote:

> 1. My code assumes that the only way a program header will have offset zero is if there is a PT_PHDR header. We should probably check for a PT_PHDR header and follow the ParentSegment pointers up until its a nullptr to get the proper starting offset.


I think it's actually legal to embed the elf header in a loadable segment, not necessarily nested inside a PT_PHDR, so I'd be wary about any assumption in this regards. We need to firstly find out which segment the program header table is in, using the elf header to determine the offset, and the program headers themselves to determine what covers it, and then use this information to perform the layout correctly.


Repository:
  rL LLVM

https://reviews.llvm.org/D42872





More information about the llvm-commits mailing list