Fix ELF inject when sections offsets are not in ascending order #34
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.
Here's structure of ELF segments in the file I was working with:
Program Headers:
Type Offset VirtAddr PhysAddr FileSiz MemSiz Flg Align
LOAD 0x0000e0 0x81000000 0x00000000 0x234c04 0x234c04 R E 0x10
LOAD 0x234cf0 0x81235000 0x00000000 0x0be60 0x3c918 RW 0x10
LOOS+0 0x242ce0 0x00000000 0x00000000 0x59040 0x00000 0x10
LOOS+0 0x29bd20 0x00000000 0x00000000 0x02d3c 0x00000 0x10
LOOS+0xfffff01 0x240b50 0x00000000 0x00000000 0x02186 0x00000 0x10
LOOS+0 is PT_SCE_RELA
LOOS+0xfffff01 is PT_SCE_LIBVERSION
Offsets of segments here is not in ascending order and the same mapping used in SELF container.
But current implementation of elf_injector tool implicitly relies on ascending order of offsets when applying offset correction.
In my case when offsets of 2-nd PT_LOAD and 1-st PT_SCE_RELA were corrected, writing compressed PT_SCE_LIBVERSION segment could overwrite already written PT_LOAD/PT_SCE_RELA segments.
I implemented sorting of compressed segments by offsets before computing offset correction.