tools: elf: don't get raw_data for no_copy sections

The `xmas-elf` crate doesn't deal with `raw_data()` of sections that are
`NO_COPY`. These sections may point to data that is outside of the file.

In order to avoid hitting this assert, don't call `raw_data()` on
sections that are marked `NO_COPY`.

Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
Sean Cross 2022-06-10 16:52:57 +08:00
parent ebc8fd90f3
commit aa436cbcc7

View file

@ -315,11 +315,13 @@ pub fn read_minielf<P: AsRef<Path>>(filename: P) -> Result<MiniElf, ElfReadError
debug!(" link: {:?}", s.link());
let size = s.size();
let no_copy = s.get_type() == Ok(ShType::NoBits);
if s.flags() & SHF_ALLOC == 0 {
debug!("section has no allocations -- skipping");
continue;
}
if s.get_type() == Ok(ShType::NoBits) {
if no_copy {
flags |= MiniElfFlags::NOCOPY;
}
if s.flags() & SHF_EXECINSTR != 0 {
@ -335,17 +337,17 @@ pub fn read_minielf<P: AsRef<Path>>(filename: P) -> Result<MiniElf, ElfReadError
name,
s.offset(),
program_offset,
s.raw_data(&elf).len(),
if no_copy { 0 } else { s.raw_data(&elf).len() },
program_offset
);
let section_data = s.raw_data(&elf);
debug!(
"Section start: {:02x} {:02x} {:02x} {:02x} going into offset 0x{:08x}",
section_data[0], section_data[1], section_data[2], section_data[3], program_offset
);
// If this section gets copied, add it to the program stream.
if s.get_type() != Ok(ShType::NoBits) {
let section_data = s.raw_data(&elf);
debug!(
"Section start: {:02x} {:02x} {:02x} {:02x} going into offset 0x{:08x}",
section_data[0], section_data[1], section_data[2], section_data[3], program_offset
);
program_data
.seek(SeekFrom::Start(program_offset))
.map_err(ElfReadError::FileSeekError)?;