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

read/coff: Add support for extended relocations. #399

Merged
merged 7 commits into from
Nov 20, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions src/read/coff/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,21 @@ 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 and read extended count & adjust
// relocations pointer.
let extended_relocation_info: &pe::ImageRelocation = data
.read_at(pointer)
.read_error("Invalid COFF relocation offset or number")
.expect("Invalid COFF relocation offset or number");
philipc marked this conversation as resolved.
Show resolved Hide resolved
// Extended relocation info does not contribute to the count of sections.
number = extended_relocation_info.virtual_address.get(LE) as usize - 1;
philipc marked this conversation as resolved.
Show resolved Hide resolved
pointer += core::mem::size_of::<pe::ImageRelocation>() as u64;
}
data.read_slice_at(pointer, number)
.read_error("Invalid COFF relocation offset or number")
}
Expand Down
25 changes: 25 additions & 0 deletions tests/coff.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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.obj"]
.iter()
.collect();
let contents = fs::read(&path_to_obj).expect("Could not read relocs_overflow.obj");
let file =
read::coff::CoffFile::parse(&contents[..]).expect("Could not parse relocs_overflow.obj");
let code_section = file
.section_by_name(".text")
.expect("Could not find .text section in relocs_overflow.obj");
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);
}
philipc marked this conversation as resolved.
Show resolved Hide resolved
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;