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

NOBITS with sh_offset==0 causes build::elf::Builder::write error #644

Closed
allvpv opened this issue Mar 17, 2024 · 1 comment · Fixed by #645
Closed

NOBITS with sh_offset==0 causes build::elf::Builder::write error #644

allvpv opened this issue Mar 17, 2024 · 1 comment · Fixed by #645

Comments

@allvpv
Copy link

allvpv commented Mar 17, 2024

Hello. AFAIK sh_offset field is completely meaningless in case of SHT_NOBITS section. Sometimes it is equal to 0: I encountered such a binary "in the wild". In this case, however, you cannot write the (valid) binary parsed by the ELF builder: it throws an error.

Probably the simplest possible solution would be to add another conditional (section.sh_type != SHT_NOBITS) around this fragments of code from build/elf.rs:

if section.sh_offset < writer.reserved_len() as u64 { ... }
// The input sh_offset needs to be preserved so that offsets in program
// headers are correct.
writer.reserve_until(section.sh_offset as usize);
if out_section.offset as u64 != section.sh_offset {...}

Let me know what do you think.

@philipc
Copy link
Contributor

philipc commented Mar 18, 2024

Thanks for the report. #645 fixes this similar to how you suggest.

Running on a few more binaries in the wild shows that there's likely other things that need fixing too.

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 a pull request may close this issue.

2 participants