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 all 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
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);
}
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;