mirror of
https://github.com/encounter/object.git
synced 2026-03-30 11:32:22 -07:00
read/coff: add support for extended relocations (#399)
This commit is contained in:
@@ -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")
|
||||
}
|
||||
|
||||
+1
-1
Submodule testfiles updated: 26f9fc09c3...fd4ed2a623
@@ -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 +1,2 @@
|
||||
mod coff;
|
||||
mod round_trip;
|
||||
|
||||
Reference in New Issue
Block a user