Debian Codegraph @

third_party/rust/object/tests/round_trip/section_flags.rs

raw
1 #![cfg(all(feature = "read", feature = "write"))]
2
3 use object::read::{Object, ObjectSection};
4 use object::{read, write};
5 use object::{Architecture, BinaryFormat, Endianness, SectionFlags, SectionKind};
6
7 #[test]
8 fn coff_x86_64_section_flags() {
9 let mut object =
10 write::Object::new(BinaryFormat::Coff, Architecture::X86_64, Endianness::Little);
11
12 let section = object.add_section(Vec::new(), b".text".to_vec(), SectionKind::Text);
13 object.section_mut(section).flags = SectionFlags::Coff {
14 characteristics: object::pe::IMAGE_SCN_MEM_WRITE,
15 };
16
17 let bytes = object.write().unwrap();
18
19 let object = read::File::parse(&*bytes).unwrap();
20 assert_eq!(object.format(), BinaryFormat::Coff);
21 assert_eq!(object.architecture(), Architecture::X86_64);
22
23 let mut sections = object.sections();
24 let section = sections.next().unwrap();
25 assert_eq!(section.name(), Ok(".text"));
26 assert_eq!(
27 section.flags(),
28 SectionFlags::Coff {
29 characteristics: object::pe::IMAGE_SCN_MEM_WRITE | object::pe::IMAGE_SCN_ALIGN_1BYTES,
30 }
31 );
32 }
33
34 #[test]
35 fn elf_x86_64_section_flags() {
36 let mut object =
37 write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
38
39 let section = object.add_section(Vec::new(), b".text".to_vec(), SectionKind::Text);
40 object.section_mut(section).flags = SectionFlags::Elf {
41 sh_flags: object::elf::SHF_WRITE.into(),
42 };
43
44 let bytes = object.write().unwrap();
45
46 let object = read::File::parse(&*bytes).unwrap();
47 assert_eq!(object.format(), BinaryFormat::Elf);
48 assert_eq!(object.architecture(), Architecture::X86_64);
49
50 let mut sections = object.sections();
51 let section = sections.next().unwrap();
52 assert_eq!(section.name(), Ok(".text"));
53 assert_eq!(
54 section.flags(),
55 SectionFlags::Elf {
56 sh_flags: object::elf::SHF_WRITE.into(),
57 }
58 );
59 }
60
61 #[test]
62 fn macho_x86_64_section_flags() {
63 let mut object = write::Object::new(
64 BinaryFormat::MachO,
65 Architecture::X86_64,
66 Endianness::Little,
67 );
68
69 let section = object.add_section(Vec::new(), b".text".to_vec(), SectionKind::Text);
70 object.section_mut(section).flags = SectionFlags::MachO {
71 flags: object::macho::S_ATTR_SELF_MODIFYING_CODE,
72 };
73
74 let bytes = object.write().unwrap();
75
76 let object = read::File::parse(&*bytes).unwrap();
77 assert_eq!(object.format(), BinaryFormat::MachO);
78 assert_eq!(object.architecture(), Architecture::X86_64);
79
80 let mut sections = object.sections();
81 let section = sections.next().unwrap();
82 assert_eq!(section.name(), Ok(".text"));
83 assert_eq!(
84 section.flags(),
85 SectionFlags::MachO {
86 flags: object::macho::S_ATTR_SELF_MODIFYING_CODE,
87 }
88 );
89 }