Skip to content

Commit

Permalink
read/coff: add support for extended relocations (#399)
Browse files Browse the repository at this point in the history
  • Loading branch information
osiewicz authored Nov 20, 2021
1 parent 5b6b6a9 commit ffaf0de
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
20 changes: 18 additions & 2 deletions src/read/coff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,24 @@ impl pe::ImageSectionHeader {
&self,
data: R,
) -> read::Result<&'data [pe::ImageRelocation]> {
let pointer = self.pointer_to_relocations.get(LE).into();
let number = self.number_of_relocations.get(LE).into();
let mut pointer = self.pointer_to_relocations.get(LE).into();
let mut number: usize = self.number_of_relocations.get(LE).into();
if number == core::u16::MAX.into()
&& self.characteristics.get(LE) & pe::IMAGE_SCN_LNK_NRELOC_OVFL != 0
{
// Extended relocations. Read first relocation (which contains extended count) & adjust
// relocations pointer.
let extended_relocation_info = data
.read_at::<pe::ImageRelocation>(pointer)
.read_error("Invalid COFF relocation offset or number")?;
number = extended_relocation_info.virtual_address.get(LE) as usize;
if number == 0 {
return Err(Error("Invalid COFF relocation number"));
}
pointer += core::mem::size_of::<pe::ImageRelocation>() as u64;
// Extended relocation info does not contribute to the count of sections.
number -= 1;
}
data.read_slice_at(pointer, number)
.read_error("Invalid COFF relocation offset or number")
}
Expand Down
2 changes: 1 addition & 1 deletion testfiles
23 changes: 23 additions & 0 deletions tests/coff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use object::{pe, read, Object, ObjectSection};
use std::fs;
use std::path::PathBuf;

#[cfg(feature = "coff")]
#[test]
fn coff_extended_relocations() {
let path_to_obj: PathBuf = ["testfiles", "coff", "relocs_overflow.o"].iter().collect();
let contents = fs::read(&path_to_obj).expect("Could not read relocs_overflow.o");
let file =
read::coff::CoffFile::parse(&contents[..]).expect("Could not parse relocs_overflow.o");
let code_section = file
.section_by_name(".text")
.expect("Could not find .text section in relocs_overflow.o");
match code_section.flags() {
object::SectionFlags::Coff { characteristics } => {
assert!(characteristics & pe::IMAGE_SCN_LNK_NRELOC_OVFL != 0)
}
_ => panic!("Invalid section flags flavour."),
};
let relocations = code_section.relocations().collect::<Vec<_>>();
assert_eq!(relocations.len(), 65536);
}
1 change: 1 addition & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
mod coff;
mod round_trip;

0 comments on commit ffaf0de

Please sign in to comment.