Debian Codegraph @

third_party/rust/object/tests/read/macho.rs

raw
1 #[cfg(feature = "std")]
2 use object::{Object, ObjectSection as _};
3
4 // Test that we can read compressed sections in Mach-O files as produced
5 // by the Go compiler.
6 #[cfg(feature = "std")]
7 #[test]
8 fn test_go_macho() {
9 let macho_testfiles = std::path::Path::new("testfiles/macho");
10
11 // Section names we expect to find, whether they should be
12 // compressed, and the actual name of the section in the file.
13 const EXPECTED: &[(&str, bool, &str)] = &[
14 (".debug_abbrev", true, "__zdebug_abbrev"),
15 (".debug_gdb_scripts", false, "__debug_gdb_scri"),
16 (".debug_ranges", true, "__zdebug_ranges"),
17 ("__data", false, "__data"),
18 ];
19
20 for file in &["go-aarch64", "go-x86_64"] {
21 let path = macho_testfiles.join(file);
22 let file = std::fs::File::open(path).unwrap();
23 let reader = object::read::ReadCache::new(file);
24 let object = object::read::File::parse(&reader).unwrap();
25 for &(name, compressed, actual_name) in EXPECTED {
26 let section = object.section_by_name(name).unwrap();
27 assert_eq!(section.name(), Ok(actual_name));
28 let compressed_file_range = section.compressed_file_range().unwrap();
29 let size = section.size();
30 if compressed {
31 assert_eq!(
32 compressed_file_range.format,
33 object::CompressionFormat::Zlib
34 );
35 assert_eq!(compressed_file_range.compressed_size, size - 12);
36 assert!(
37 compressed_file_range.uncompressed_size > compressed_file_range.compressed_size,
38 "decompressed size is greater than compressed size"
39 );
40 } else {
41 assert_eq!(
42 compressed_file_range.format,
43 object::CompressionFormat::None
44 );
45 assert_eq!(compressed_file_range.compressed_size, size);
46 }
47 }
48 }
49 }