Align SizeOfImage to section boundaries - #5
Open
zanieb wants to merge 2 commits into
Open
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates PE image writing logic so SizeOfImage is computed from the highest section’s virtual extent and rounded to SectionAlignment, matching the PE specification and avoiding misaligned image sizes after adding a resource section.
Changes:
- Recompute
SizeOfImagefrom the max(VirtualAddress + VirtualSize)across sections and align it toSectionAlignment. - Add a regression test asserting
SizeOfImageis a multiple ofSectionAlignmentafter resource transfer.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/tests.rs | Adds an assertion that SizeOfImage remains section-aligned after modifying resources. |
| src/image.rs | Replaces incremental SizeOfImage updates with a recomputed, section-aligned value based on section virtual extents. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
+567
to
+572
| let size_of_image = section_table | ||
| .iter() | ||
| .map(|section| section.virtual_address + section.virtual_size) | ||
| .max() | ||
| .map(|size| aligned_to(size, windows_header.section_alignment())) | ||
| .unwrap_or_else(|| windows_header.size_of_image()); |
Author
There was a problem hiding this comment.
We can't just use u64, it's a u32 field. I can look at an alternative here though 🤔
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
The PE specification requires
SizeOfImageto describe the loaded image and be a multiple ofSectionAlignment. Adding a resource section currently increases it by the section’s file-aligned size, which can leave the image misaligned. Instead, we should calculateSizeOfImagefrom the highest section’s virtual extent and round it toSectionAlignment.