-
Notifications
You must be signed in to change notification settings - Fork 163
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: Handle SHN_XINDEX for strtab section index (#304) #316
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this LGTM, thanks for adding this, quite surprised wasn't done before! feel free to check about logging, otherwise we're good.
@@ -51,7 +51,12 @@ fn parse_text_section_size_lazy(base: &[u8]) -> Result<u64, &'static str> { | |||
SectionHeader::parse(base, header.e_shoff as usize, header.e_shnum as usize, ctx) | |||
.map_err(|_| "parse section headers error")?; | |||
|
|||
let strtab_idx = header.e_shstrndx as usize; | |||
let strtab_idx = if header.e_shstrndx as u32 == SHN_XINDEX { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weird i feel like this was added already, long time ago, surprised to see it now
// FIXME: warn! here | ||
return Ok(Strtab::default()) | ||
} | ||
section_idx = section_headers[0].sh_link as usize; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just curious (can't remember why) it's always the first section header's sh_link that is used?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to. I actually looked what Gimli was doing, but the ELF docs at Oracle says:
If the section name string table section index is greater than or equal to SHN_LORESERVE (0xff00), this member has the value SHN_XINDEX (0xffff) and the actual index of the section name string table section is contained in the sh_link field of the section header at index 0. Otherwise, the sh_link member of the initial section header entry contains the value zero. See Table 12-6 and Table 12-7.
And thank you for stepping up and fixing that issue! |
This happens when the number of sections in an ELF object reaches 2^16, at which point the u16 shstrndx takes the special value SHN_XINDEX. Fix based on algorithm from: github.com/gimli-rs/object/blob/c476071/src/read/elf/file.rs#L582-L600
released in 0.5.3 on crates; thanks for fixing this @tux3! |
This attempts to fix #304.
With this patch, I no longer reproduce the issue using the same large object file that triggered it before.
I've also updated the test code to handle XINDEX for the strtab, however I don't know a good way to generate a test case for an ELF file with >=2^16 sections without embedding a very large blob, so I've left it for future work.
(If I've missed a simple way to do this, happy to do it now)