This is an archive of the discontinued LLVM Phabricator instance.

[llvm-objcopy] Fix major layout bugs in llvm-objcopy
ClosedPublic

Authored by jakehehrlich on Sep 29 2017, 5:07 PM.

Details

Summary

Somehow a few massive errors slipped though the cracks of testing.

  1. The code in Segment::finalize was left over from the old layout algorithm. In certain situations this would cause very strange issues with segment layout. For instance in the shift-segments.test case it would cause the second segment to have the same offset as the first.
  2. In debugging this I discovered another issue. Namely section alignment was not being computed based on Section->Align but instead Section->Offset which is bizarre and makes no sense. I have no clue how it worked in the first place. This issue is also fixed
  3. Fixing #2 exposed a bug where things were not being written past the end of the file that technically should have been. This was because in certain cases (like overlapping-segments) the end of the file wouldn't always be bumped if the offset could be chosen relative to an existing segment that already had it's offset chosen. For fully nested segments this is fine but for overlapping segments this leaves the end of the file short. So I changed how the offset is bumped when looping though segments.

Diff Detail

Repository
rL LLVM

Event Timeline

jakehehrlich created this revision.Sep 29 2017, 5:07 PM
jhenderson edited edge metadata.Oct 2 2017, 1:31 AM

I'm equally at a loss as to how the align bug wasn't picked up earlier. I think it would be wise to take a look and see which test(s) should be covering this case, and why they weren't failing before.

test/tools/llvm-objcopy/segment-shift.test
56 ↗(On Diff #117242)

This address looks wrong. Shouldn't it be 0x3000, i.e. the address of .text3?

tools/llvm-objcopy/Object.cpp
33 ↗(On Diff #117242)

I don't think we need this function at all anymore.

jhenderson added inline comments.
test/tools/llvm-objcopy/segment-shift.test
44 ↗(On Diff #117242)

Actually, the same goes for this segment too - the address looks wrong.

  1. Removed all traces of Segment::finalize
  2. Added proper addresses to program headers in shift test to make sure addresses are also being tested
test/tools/llvm-objcopy/segment-shift.test
56 ↗(On Diff #117242)

In yaml2obj the addresses of the program headers default to 0x0 and the user has to manually add them (this "feature" of yaml2obj is my fault). I'll add the desired addresses to make things consistent

This revision is now accepted and ready to land.Oct 4 2017, 9:09 AM
This revision was automatically updated to reflect the committed changes.