Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

elf: Workaround for segment first section address check in phdr rewrite #12

Merged
merged 1 commit into from
Jan 25, 2024

Conversation

stephanosio
Copy link
Member

rewrite_elf_program_header() currently makes a questionable assumption that the LMA of a segment will be aligned at the alignment of the first section in the segment, and attempts to align the address of the segment LMA to the alignment of the first section using align_power() before comparing it to the actual LMA of the first section for the purpose of verifying that the first section starts at the beginning of the segment.

This is not a problem when the LMA is equal to the VMA and both are aligned at the section alignment; but, for the sections that have different VMA and LMA, only the VMA is guaranteed to be aligned at the section alignment, and the LMA may or may not be aligned at the same boundary, leading to rewrite_elf_program_header() returning false even for valid ELF files that do not contain any segments whose first section does not start at the beginning of the segment.

This patch adds an alternate check directly comparing the segment and section LMAs for the segments that do not contain any headers; in case a segment contains a file or program header, the function may still erroneously return false.

A more fundamental fix should re-implement the function such that it uses VMA for verifying that the first section in a segment starts at the beginning of the segment.


Fixes zephyrproject-rtos/zephyr#58080

rewrite_elf_program_header() currently makes a questionable assumption
that the LMA of a segment will be aligned at the alignment of the first
section in the segment, and attempts to align the address of the segment
LMA to the alignment of the first section using align_power() before
comparing it to the actual LMA of the first section for the purpose of
verifying that the first section starts at the beginning of the segment.

This is not a problem when the LMA is equal to the VMA and both are
aligned at the section alignment; but, for the sections that have
different VMA and LMA, only the VMA is guaranteed to be aligned at the
section alignment, and the LMA may or may not be aligned at the same
boundary, leading to rewrite_elf_program_header() returning false even
for valid ELF files that do not contain any segments whose first section
does not start at the beginning of the segment.

This patch adds an alternate check directly comparing the segment and
section LMAs for the segments that do not contain any headers; in case a
segment contains a file or program header, the function may still
erroneously return false.

A more fundamental fix should re-implement the function such that it
uses VMA for verifying that the first section in a segment starts at the
beginning of the segment.

Signed-off-by: Stephanos Ioannidis <root@stephanos.io>
Copy link
Member Author

@stephanosio stephanosio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stephanosio stephanosio merged commit 81f63d1 into zephyr-binutils-2_38 Jan 25, 2024
@stephanosio stephanosio deleted the elf_phdr_rewrite_lma_neq_vma branch January 31, 2024 18:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Build issues observed when using CONFIG_BUILD_OUTPUT_ADJUST_LMA
1 participant