Rework file shifting to avoid sections crossing multiple segments #415
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #403.
The previous logic was incorrect in some cases.
The way it worked before is we chose a point where to create a gap (startOffset), just after a .dynstr or the last non-interp PROGBITS. We then calculated how much space to add, move everything and create a PT_LOAD with that size.
The size of that PT_LOAD was incorrect however, as it only took into account the additional space we were adding - it did not take into account the space before
startOffset
, so it could be too small. The offsets of subsequent segments had similar logic. This created the potential for a section to overlap between segments.To fix this, I've changed it so we don't add a new segment at all. Instead, we find the existing PT_LOAD segment that contains
startOffset
and increase the size of that by the calculated amount. Everything afterstartOffset
is moved and has offsets adjusted as before, while everything before is now kept as is with only virtual memory offsets adjusted.