diff options
Diffstat (limited to 'vendor/object/tests')
-rw-r--r-- | vendor/object/tests/read/coff.rs | 2 | ||||
-rw-r--r-- | vendor/object/tests/round_trip/elf.rs | 73 | ||||
-rw-r--r-- | vendor/object/tests/round_trip/mod.rs | 187 |
3 files changed, 259 insertions, 3 deletions
diff --git a/vendor/object/tests/read/coff.rs b/vendor/object/tests/read/coff.rs index 3e61ec284..dcf3b3c6a 100644 --- a/vendor/object/tests/read/coff.rs +++ b/vendor/object/tests/read/coff.rs @@ -8,7 +8,7 @@ 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"); + 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"); diff --git a/vendor/object/tests/round_trip/elf.rs b/vendor/object/tests/round_trip/elf.rs index 048db5795..0aafadc86 100644 --- a/vendor/object/tests/round_trip/elf.rs +++ b/vendor/object/tests/round_trip/elf.rs @@ -1,5 +1,5 @@ use object::read::elf::{FileHeader, SectionHeader}; -use object::read::{Object, ObjectSymbol}; +use object::read::{Object, ObjectSection, ObjectSymbol}; use object::{ elf, read, write, Architecture, BinaryFormat, Endianness, LittleEndian, SectionIndex, SectionKind, SymbolFlags, SymbolKind, SymbolScope, SymbolSection, U32, @@ -42,6 +42,37 @@ fn symtab_shndx() { } } +#[test] +fn aligned_sections() { + let mut object = + write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little); + + let text_section_id = object.add_section(vec![], b".text".to_vec(), SectionKind::Text); + let text_section = object.section_mut(text_section_id); + text_section.set_data(&[][..], 4096); + + let data_section_id = object.add_section(vec![], b".data".to_vec(), SectionKind::Data); + let data_section = object.section_mut(data_section_id); + data_section.set_data(&b"1234"[..], 16); + + let bytes = object.write().unwrap(); + + let object = read::File::parse(&*bytes).unwrap(); + assert_eq!(object.format(), BinaryFormat::Elf); + assert_eq!(object.architecture(), Architecture::X86_64); + + let mut sections = object.sections(); + let _ = sections.next().unwrap(); + + let section = sections.next().unwrap(); + assert_eq!(section.name(), Ok(".text")); + assert_eq!(section.file_range(), Some((4096, 0))); + + let section = sections.next().unwrap(); + assert_eq!(section.name(), Ok(".data")); + assert_eq!(section.file_range(), Some((4096, 4))); +} + #[cfg(feature = "compression")] #[test] fn compression_zlib() { @@ -216,3 +247,43 @@ fn note() { assert_eq!(note.n_type(endian), 2); assert!(notes.next().unwrap().is_none()); } + +#[test] +fn gnu_property() { + gnu_property_inner::<elf::FileHeader32<Endianness>>(Architecture::I386); + gnu_property_inner::<elf::FileHeader64<Endianness>>(Architecture::X86_64); +} + +fn gnu_property_inner<Elf: FileHeader<Endian = Endianness>>(architecture: Architecture) { + let endian = Endianness::Little; + let mut object = write::Object::new(BinaryFormat::Elf, architecture, endian); + object.add_elf_gnu_property_u32( + elf::GNU_PROPERTY_X86_FEATURE_1_AND, + elf::GNU_PROPERTY_X86_FEATURE_1_IBT | elf::GNU_PROPERTY_X86_FEATURE_1_SHSTK, + ); + + let bytes = &*object.write().unwrap(); + + //std::fs::write(&"note.o", &bytes).unwrap(); + + let header = Elf::parse(bytes).unwrap(); + assert_eq!(header.endian().unwrap(), endian); + let sections = header.sections(endian, bytes).unwrap(); + let section = sections.section(SectionIndex(1)).unwrap(); + assert_eq!( + sections.section_name(endian, section).unwrap(), + b".note.gnu.property" + ); + assert_eq!(section.sh_flags(endian).into(), u64::from(elf::SHF_ALLOC)); + let mut notes = section.notes(endian, bytes).unwrap().unwrap(); + let note = notes.next().unwrap().unwrap(); + let mut props = note.gnu_properties(endian).unwrap(); + let prop = props.next().unwrap().unwrap(); + assert_eq!(prop.pr_type(), elf::GNU_PROPERTY_X86_FEATURE_1_AND); + assert_eq!( + prop.data_u32(endian).unwrap(), + elf::GNU_PROPERTY_X86_FEATURE_1_IBT | elf::GNU_PROPERTY_X86_FEATURE_1_SHSTK + ); + assert!(props.next().unwrap().is_none()); + assert!(notes.next().unwrap().is_none()); +} diff --git a/vendor/object/tests/round_trip/mod.rs b/vendor/object/tests/round_trip/mod.rs index 89bed8bf3..8f8dd79c9 100644 --- a/vendor/object/tests/round_trip/mod.rs +++ b/vendor/object/tests/round_trip/mod.rs @@ -1,7 +1,7 @@ #![cfg(all(feature = "read", feature = "write"))] use object::read::{Object, ObjectSection, ObjectSymbol}; -use object::{read, write}; +use object::{read, write, SectionIndex}; use object::{ Architecture, BinaryFormat, Endianness, RelocationEncoding, RelocationKind, SectionKind, SymbolFlags, SymbolKind, SymbolScope, SymbolSection, @@ -449,3 +449,188 @@ fn macho_x86_64() { assert_eq!(symbol.name(), "_func1"); assert_eq!(map.get(func1_offset - 1), None); } + +#[test] +fn macho_any() { + for (arch, endian) in [ + (Architecture::Aarch64, Endianness::Little), + (Architecture::Aarch64_Ilp32, Endianness::Little), + /* TODO: + (Architecture::Arm, Endianness::Little), + */ + (Architecture::I386, Endianness::Little), + (Architecture::X86_64, Endianness::Little), + /* TODO: + (Architecture::PowerPc, Endianness::Big), + (Architecture::PowerPc64, Endianness::Big), + */ + ] + .iter() + .copied() + { + let mut object = write::Object::new(BinaryFormat::MachO, arch, endian); + + let section = object.section_id(write::StandardSection::Data); + object.append_section_data(section, &[1; 30], 4); + let symbol = object.section_symbol(section); + + object + .add_relocation( + section, + write::Relocation { + offset: 8, + size: 32, + kind: RelocationKind::Absolute, + encoding: RelocationEncoding::Generic, + symbol, + addend: 0, + }, + ) + .unwrap(); + if arch.address_size().unwrap().bytes() >= 8 { + object + .add_relocation( + section, + write::Relocation { + offset: 16, + size: 64, + kind: RelocationKind::Absolute, + encoding: RelocationEncoding::Generic, + symbol, + addend: 0, + }, + ) + .unwrap(); + } + + let bytes = object.write().unwrap(); + let object = read::File::parse(&*bytes).unwrap(); + println!("{:?}", object.architecture()); + assert_eq!(object.format(), BinaryFormat::MachO); + assert_eq!(object.architecture(), arch); + assert_eq!(object.endianness(), endian); + + let mut sections = object.sections(); + + let data = sections.next().unwrap(); + println!("{:?}", data); + assert_eq!(data.segment_name(), Ok(Some("__DATA"))); + assert_eq!(data.name(), Ok("__data")); + assert_eq!(data.kind(), SectionKind::Data); + + let mut relocations = data.relocations(); + + let (offset, relocation) = relocations.next().unwrap(); + println!("{:?}", relocation); + assert_eq!(offset, 8); + assert_eq!(relocation.kind(), RelocationKind::Absolute); + assert_eq!(relocation.encoding(), RelocationEncoding::Generic); + assert_eq!(relocation.size(), 32); + assert_eq!(relocation.addend(), 0); + + if arch.address_size().unwrap().bytes() >= 8 { + let (offset, relocation) = relocations.next().unwrap(); + println!("{:?}", relocation); + assert_eq!(offset, 16); + assert_eq!(relocation.kind(), RelocationKind::Absolute); + assert_eq!(relocation.encoding(), RelocationEncoding::Generic); + assert_eq!(relocation.size(), 64); + assert_eq!(relocation.addend(), 0); + } + } +} + +#[cfg(feature = "xcoff")] +#[test] +fn xcoff_powerpc() { + for arch in [Architecture::PowerPc, Architecture::PowerPc64] { + let mut object = write::Object::new(BinaryFormat::Xcoff, arch, Endianness::Big); + + object.add_file_symbol(b"file.c".to_vec()); + + let text = object.section_id(write::StandardSection::Text); + object.append_section_data(text, &[1; 30], 4); + + let func1_offset = object.append_section_data(text, &[1; 30], 4); + assert_eq!(func1_offset, 32); + let func1_symbol = object.add_symbol(write::Symbol { + name: b"func1".to_vec(), + value: func1_offset, + size: 32, + kind: SymbolKind::Text, + scope: SymbolScope::Linkage, + weak: false, + section: write::SymbolSection::Section(text), + flags: SymbolFlags::None, + }); + + object + .add_relocation( + text, + write::Relocation { + offset: 8, + size: 64, + kind: RelocationKind::Absolute, + encoding: RelocationEncoding::Generic, + symbol: func1_symbol, + addend: 0, + }, + ) + .unwrap(); + + let bytes = object.write().unwrap(); + let object = read::File::parse(&*bytes).unwrap(); + assert_eq!(object.format(), BinaryFormat::Xcoff); + assert_eq!(object.architecture(), arch); + assert_eq!(object.endianness(), Endianness::Big); + + let mut sections = object.sections(); + + let text = sections.next().unwrap(); + println!("{:?}", text); + let text_index = text.index().0; + assert_eq!(text.name(), Ok(".text")); + assert_eq!(text.kind(), SectionKind::Text); + assert_eq!(text.address(), 0); + assert_eq!(text.size(), 62); + assert_eq!(&text.data().unwrap()[..30], &[1; 30]); + assert_eq!(&text.data().unwrap()[32..62], &[1; 30]); + + let mut symbols = object.symbols(); + + let mut symbol = symbols.next().unwrap(); + println!("{:?}", symbol); + assert_eq!(symbol.name(), Ok("file.c")); + assert_eq!(symbol.address(), 0); + assert_eq!(symbol.kind(), SymbolKind::File); + assert_eq!(symbol.section_index(), None); + assert_eq!(symbol.scope(), SymbolScope::Compilation); + assert_eq!(symbol.is_weak(), false); + assert_eq!(symbol.is_undefined(), false); + + symbol = symbols.next().unwrap(); + println!("{:?}", symbol); + let func1_symbol = symbol.index(); + assert_eq!(symbol.name(), Ok("func1")); + assert_eq!(symbol.address(), func1_offset); + assert_eq!(symbol.kind(), SymbolKind::Text); + assert_eq!(symbol.section_index(), Some(SectionIndex(text_index))); + assert_eq!(symbol.scope(), SymbolScope::Linkage); + assert_eq!(symbol.is_weak(), false); + assert_eq!(symbol.is_undefined(), false); + + let mut relocations = text.relocations(); + + let (offset, relocation) = relocations.next().unwrap(); + println!("{:?}", relocation); + assert_eq!(offset, 8); + assert_eq!(relocation.kind(), RelocationKind::Absolute); + assert_eq!(relocation.encoding(), RelocationEncoding::Generic); + assert_eq!(relocation.size(), 64); + assert_eq!( + relocation.target(), + read::RelocationTarget::Symbol(func1_symbol) + ); + assert_eq!(relocation.addend(), 0); + } +} |