From 805639d679acb4bf956671e38196caaf5a15590b Mon Sep 17 00:00:00 2001 From: Andrew Werner Date: Wed, 19 Jun 2024 09:56:41 -0400 Subject: [PATCH] tests/read/macho: add testing for go Mach-O executables This commit updates the submodule for the testfiles to include the Mach-O binaries produced by the Go compiler and then exercises the logic in the previous commits. The new testfiles commit is due to https://github.com/gimli-rs/object-testfiles/pull/14. --- testfiles | 2 +- tests/read/macho.rs | 49 +++++++++++++++++++++++++++++++++++++++++++++ tests/read/mod.rs | 1 + 3 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 tests/read/macho.rs diff --git a/testfiles b/testfiles index 14d80667..cebc8967 160000 --- a/testfiles +++ b/testfiles @@ -1 +1 @@ -Subproject commit 14d806678c02be9b6571d9af402eea0da1bde802 +Subproject commit cebc89674360f005d415bae42ca66fa4bbfe40a5 diff --git a/tests/read/macho.rs b/tests/read/macho.rs new file mode 100644 index 00000000..59f314be --- /dev/null +++ b/tests/read/macho.rs @@ -0,0 +1,49 @@ +#[cfg(feature = "std")] +use object::{Object, ObjectSection as _}; + +// Test that we can read compressed sections in Mach-O files as produced +// by the Go compiler. +#[cfg(feature = "std")] +#[test] +fn test_go_macho() { + let macho_testfiles = std::path::Path::new("testfiles/macho"); + + // Section names we expect to find, whether they should be + // compressed, and the actual name of the section in the file. + const EXPECTED: &[(&str, bool, &str)] = &[ + (".debug_abbrev", true, "__zdebug_abbrev"), + (".debug_gdb_scripts", false, "__debug_gdb_scri"), + (".debug_ranges", true, "__zdebug_ranges"), + ("__data", false, "__data"), + ]; + + for file in &["go-aarch64", "go-x86_64"] { + let path = macho_testfiles.join(file); + let file = std::fs::File::open(path).unwrap(); + let reader = object::read::ReadCache::new(file); + let object = object::read::File::parse(&reader).unwrap(); + for &(name, compressed, actual_name) in EXPECTED { + let section = object.section_by_name(name).unwrap(); + assert_eq!(section.name(), Ok(actual_name)); + let compressed_file_range = section.compressed_file_range().unwrap(); + let size = section.size(); + if compressed { + assert_eq!( + compressed_file_range.format, + object::CompressionFormat::Zlib + ); + assert_eq!(compressed_file_range.compressed_size, size - 12); + assert!( + compressed_file_range.uncompressed_size > compressed_file_range.compressed_size, + "decompressed size is greater than compressed size" + ); + } else { + assert_eq!( + compressed_file_range.format, + object::CompressionFormat::None + ); + assert_eq!(compressed_file_range.compressed_size, size); + } + } + } +} diff --git a/tests/read/mod.rs b/tests/read/mod.rs index ef402104..48e005ee 100644 --- a/tests/read/mod.rs +++ b/tests/read/mod.rs @@ -2,3 +2,4 @@ mod coff; mod elf; +mod macho;