Add sample object with with > 0xff00 section headers

Also add unit tests that we properly parse e_shnum and e_shstrndx out of
shdr0 in that case.
This commit is contained in:
Christopher Cole 2022-11-09 15:57:29 -08:00
parent f747747929
commit 33e3435a7f
No known key found for this signature in database
GPG Key ID: 0AC856975983E9DB
3 changed files with 46 additions and 0 deletions

BIN
sample-objects/shnum.x86_64 Normal file

Binary file not shown.

View File

@ -875,6 +875,30 @@ mod interface_tests {
assert_eq!(shdr.sh_type, abi::SHT_GNU_HASH); assert_eq!(shdr.sh_type, abi::SHT_GNU_HASH);
} }
#[test]
fn shnum_and_shstrndx_in_shdr0() {
let path = std::path::PathBuf::from("sample-objects/shnum.x86_64");
let file_data = std::fs::read(path).expect("Could not read file.");
let slice = file_data.as_slice();
let file = ElfBytes::<AnyEndian>::minimal_parse(slice).unwrap();
let (shdrs, strtab) = file
.section_headers_with_strtab()
.expect("shdrs should be parsable")
.expect("File should have shdrs");
let shdrs_len = shdrs.len();
assert_eq!(shdrs_len, 0xFF15);
let shdr = shdrs.get(shdrs_len - 1).unwrap();
let name = strtab
.get(shdr.sh_name as usize)
.expect("Failed to get section name");
assert_eq!(name, ".shstrtab");
assert_eq!(shdr.sh_type, abi::SHT_STRTAB);
}
#[test] #[test]
fn section_header_by_name() { fn section_header_by_name() {
let path = std::path::PathBuf::from("sample-objects/basic.x86_64"); let path = std::path::PathBuf::from("sample-objects/basic.x86_64");

View File

@ -700,6 +700,28 @@ mod interface_tests {
assert_eq!(shdr_4.sh_type, abi::SHT_GNU_HASH); assert_eq!(shdr_4.sh_type, abi::SHT_GNU_HASH);
} }
#[test]
fn shnum_and_shstrndx_in_shdr0() {
let path = std::path::PathBuf::from("sample-objects/shnum.x86_64");
let io = std::fs::File::open(path).expect("Could not open file.");
let mut file = ElfStream::<AnyEndian, _>::open_stream(io).expect("Open test1");
let (shdrs, strtab) = file
.section_headers_with_strtab()
.expect("shdrs should be parsable");
let shdrs_len = shdrs.len();
assert_eq!(shdrs_len, 0xFF15);
let shdr = shdrs.get(shdrs_len - 1).unwrap();
let name = strtab
.get(shdr.sh_name as usize)
.expect("Failed to get section name");
assert_eq!(name, ".shstrtab");
assert_eq!(shdr.sh_type, abi::SHT_STRTAB);
}
#[test] #[test]
fn section_header_by_name() { fn section_header_by_name() {
let path = std::path::PathBuf::from("sample-objects/basic.x86_64"); let path = std::path::PathBuf::from("sample-objects/basic.x86_64");