From ffaf0de82a17f7783acc06f43fe5bcdd548705bc Mon Sep 17 00:00:00 2001 From: Piotr Osiewicz <24362066+osiewicz@users.noreply.github.com> Date: Sat, 20 Nov 2021 06:17:29 +0100 Subject: [PATCH] read/coff: add support for extended relocations (#399) --- src/read/coff/section.rs | 20 ++++++++++++++++++-- testfiles | 2 +- tests/coff.rs | 23 +++++++++++++++++++++++ tests/integration.rs | 1 + 4 files changed, 43 insertions(+), 3 deletions(-) create mode 100644 tests/coff.rs diff --git a/src/read/coff/section.rs b/src/read/coff/section.rs index 48c1529b..cb2edec9 100644 --- a/src/read/coff/section.rs +++ b/src/read/coff/section.rs @@ -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::(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::() 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") } diff --git a/testfiles b/testfiles index 26f9fc09..fd4ed2a6 160000 --- a/testfiles +++ b/testfiles @@ -1 +1 @@ -Subproject commit 26f9fc09c3eec709dd88b4f4675da59ab2d9dff2 +Subproject commit fd4ed2a6230c80a1b6e216ad0504905f987ce200 diff --git a/tests/coff.rs b/tests/coff.rs new file mode 100644 index 00000000..3e61ec28 --- /dev/null +++ b/tests/coff.rs @@ -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::>(); + assert_eq!(relocations.len(), 65536); +} diff --git a/tests/integration.rs b/tests/integration.rs index ee859f2c..e31e50f0 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -1 +1,2 @@ +mod coff; mod round_trip;