Open NotNite opened 3 months ago
There's code in the object-rewrite
crate to do this, but there's no public API for it. It's possible we could move this code to object::write::elf
, but I'm uncertain what a public API for this would look like. The main issue is that it relies on heuristics, so I'd prefer if a public API allowed those heuristics to be changed.
Another option would be to extend the object-rewrite
API to allow adding a section.
I tried using object-rewrite, but it seems to not want to move to adjust for the new program header entry:
[2024-08-11T01:09:20Z INFO object_rewrite::elf] Immovable program headers (end address 10238) overlaps immovable .init (start address 10200)
Commenting out the block addition for the program headers (just for fun, probably would break things anyways) leads to the same error as before:
[2024-08-11T01:08:53Z INFO object_rewrite::elf] Moving 1 sections, adding 1 PT_LOAD segments, splitting 0 segments
[2024-08-11T01:08:53Z INFO object_rewrite::elf] Moved .sprxpatcher to offset 247b10, addr 277b10
[2024-08-11T01:08:53Z INFO object_rewrite::elf] Added PT_LOAD segment with p_flags 5, offset 247b10, addr 277b10, size 8e
Error: Unsupported sh_offset value 0x200 for section '.init', expected at least 0x270
I'm unsure if resizing the program headers is even something that's possible... 🤔
Ah okay, in my tests so far the section following the program headers has been safe to move.
I'm not sure, but it's likely that the .init
section isn't safe to move without patching any relative addressing instructions within it. That's not something this crate can do, since it requires disassembling/assembling instructions.
The alternative is to try moving the elf header and program headers down a page to make room. I think this is theoretically possible, but not something I've tried.
Could you not keep the ELF header in the same spot and move the program header table to the end of the file? Program header entries if I recall don't use any sort of relative positioning / can be placed anywhere in the ELF.
That is a possibility. There was a linux bug that meant it didn't support program headers that aren't in the first PT_LOAD
segment, but that seems to be fixed: https://github.com/torvalds/linux/commit/0da1d5002745cdc721bc018b582a8a9704d56c42
object::build::elf::Builder
will need support for moving it too (currently a TODO).
Hi, I was wondering if it's possible to add a custom section/segment to an ELF file. For context, I'm currently working on a tool to patch PlayStation 3 ELFs to allow custom code. I need to add a bit of shellcode into the executable, so I wanted to make a custom section to store my code in and then modify the entrypoint. My code to modify the executable is a little complex, so I made this simple example:
However, when running this, it panics:
I assume this is because the addition of the new section/segment into the program header table has shifted the file offsets by 0x38 (the size of the new entry). Is it possible to repair these offsets such that the modified ELF writes successfully? I tried just shifting every sh_offset by 0x38, but that didn't seem to work. I suspect there is more work to be done here (or it's just flat out impossible).