1 #![cfg(all(feature = "read", feature = "write"))]
4 use object
::read
::{Object, ObjectComdat, ObjectSection, ObjectSymbol}
;
5 use object
::{read, write}
;
7 Architecture
, BinaryFormat
, ComdatKind
, Endianness
, SectionKind
, SymbolFlags
, SymbolKind
,
12 fn coff_x86_64_comdat() {
14 write
::Object
::new(BinaryFormat
::Coff
, Architecture
::X86_64
, Endianness
::Little
);
16 let (section1
, offset
) =
17 object
.add_subsection(write
::StandardSection
::Text
, b
"s1", &[0, 1, 2, 3], 4);
18 object
.section_symbol(section1
);
20 object
.add_subsection(write
::StandardSection
::Data
, b
"s1", &[0, 1, 2, 3], 4);
21 object
.section_symbol(section2
);
23 let symbol
= object
.add_symbol(write
::Symbol
{
27 kind
: SymbolKind
::Data
,
28 scope
: SymbolScope
::Linkage
,
30 section
: write
::SymbolSection
::Section(section1
),
31 flags
: SymbolFlags
::None
,
34 object
.add_comdat(write
::Comdat
{
35 kind
: ComdatKind
::NoDuplicates
,
37 sections
: vec
![section1
, section2
],
40 let bytes
= object
.write().unwrap();
42 //std::fs::write(&"comdat.o", &bytes).unwrap();
44 let object
= read
::File
::parse(&bytes
).unwrap();
45 assert_eq
!(object
.format(), BinaryFormat
::Coff
);
46 assert_eq
!(object
.architecture(), Architecture
::X86_64
);
48 let mut sections
= object
.sections();
50 let section1
= sections
.next().unwrap();
51 println
!("{:?}", section1
);
52 let section1_index
= section1
.index();
53 assert_eq
!(section1
.name(), Ok(".text$s1"));
54 assert_eq
!(section1
.kind(), SectionKind
::Text
);
55 assert_eq
!(section1
.address(), 0);
56 assert_eq
!(section1
.size(), 4);
58 let section2
= sections
.next().unwrap();
59 println
!("{:?}", section2
);
60 let section2_index
= section2
.index();
61 assert_eq
!(section2
.name(), Ok(".data$s1"));
62 assert_eq
!(section2
.kind(), SectionKind
::Data
);
63 assert_eq
!(section2
.address(), 0);
64 assert_eq
!(section2
.size(), 4);
66 let mut symbols
= object
.symbols();
68 let symbol
= symbols
.next().unwrap();
69 println
!("{:?}", symbol
);
70 assert_eq
!(symbol
.name(), Ok(".text$s1"));
71 assert_eq
!(symbol
.kind(), SymbolKind
::Section
);
74 read
::SymbolSection
::Section(section1
.index())
78 SymbolFlags
::CoffSection
{
79 selection
: pe
::IMAGE_COMDAT_SELECT_NODUPLICATES
,
80 associative_section
: None
84 let symbol
= symbols
.next().unwrap();
85 println
!("{:?}", symbol
);
86 assert_eq
!(symbol
.name(), Ok(".data$s1"));
87 assert_eq
!(symbol
.kind(), SymbolKind
::Section
);
90 read
::SymbolSection
::Section(section2
.index())
94 SymbolFlags
::CoffSection
{
95 selection
: pe
::IMAGE_COMDAT_SELECT_ASSOCIATIVE
,
96 associative_section
: Some(section1_index
)
100 let symbol
= symbols
.next().unwrap();
101 let symbol_index
= symbol
.index();
102 println
!("{:?}", symbol
);
103 assert_eq
!(symbol
.name(), Ok("s1"));
104 assert_eq
!(symbol
.kind(), SymbolKind
::Data
);
107 read
::SymbolSection
::Section(section1
.index())
109 assert_eq
!(symbol
.scope(), SymbolScope
::Linkage
);
110 assert_eq
!(symbol
.is_weak(), false);
111 assert_eq
!(symbol
.is_undefined(), false);
112 assert_eq
!(symbol
.address(), 0);
114 let symbol
= symbols
.next();
115 assert
!(symbol
.is_none(), format
!("unexpected symbol {:?}", symbol
));
117 let mut comdats
= object
.comdats();
119 let comdat
= comdats
.next().unwrap();
120 println
!("{:?}", comdat
);
121 assert_eq
!(comdat
.kind(), ComdatKind
::NoDuplicates
);
122 assert_eq
!(comdat
.symbol(), symbol_index
);
124 let mut comdat_sections
= comdat
.sections();
125 assert_eq
!(comdat_sections
.next(), Some(section1_index
));
126 assert_eq
!(comdat_sections
.next(), Some(section2_index
));
127 assert_eq
!(comdat_sections
.next(), None
);
131 fn elf_x86_64_common() {
133 write
::Object
::new(BinaryFormat
::Elf
, Architecture
::X86_64
, Endianness
::Little
);
135 let (section1
, offset
) =
136 object
.add_subsection(write
::StandardSection
::Text
, b
"s1", &[0, 1, 2, 3], 4);
138 object
.add_subsection(write
::StandardSection
::Data
, b
"s1", &[0, 1, 2, 3], 4);
140 let symbol
= object
.add_symbol(write
::Symbol
{
141 name
: b
"s1".to_vec(),
144 kind
: SymbolKind
::Data
,
145 scope
: SymbolScope
::Linkage
,
147 section
: write
::SymbolSection
::Section(section1
),
148 flags
: SymbolFlags
::None
,
151 object
.add_comdat(write
::Comdat
{
152 kind
: ComdatKind
::Any
,
154 sections
: vec
![section1
, section2
],
157 let bytes
= object
.write().unwrap();
159 //std::fs::write(&"comdat.o", &bytes).unwrap();
161 let object
= read
::File
::parse(&bytes
).unwrap();
162 assert_eq
!(object
.format(), BinaryFormat
::Elf
);
163 assert_eq
!(object
.architecture(), Architecture
::X86_64
);
165 let mut sections
= object
.sections();
167 let section
= sections
.next().unwrap();
168 println
!("{:?}", section
);
169 assert_eq
!(section
.name(), Ok(""));
171 let section
= sections
.next().unwrap();
172 println
!("{:?}", section
);
173 assert_eq
!(section
.name(), Ok(".group"));
175 let section1
= sections
.next().unwrap();
176 println
!("{:?}", section1
);
177 let section1_index
= section1
.index();
178 assert_eq
!(section1
.name(), Ok(".text.s1"));
179 assert_eq
!(section1
.kind(), SectionKind
::Text
);
180 assert_eq
!(section1
.address(), 0);
181 assert_eq
!(section1
.size(), 4);
183 let section2
= sections
.next().unwrap();
184 println
!("{:?}", section2
);
185 let section2_index
= section2
.index();
186 assert_eq
!(section2
.name(), Ok(".data.s1"));
187 assert_eq
!(section2
.kind(), SectionKind
::Data
);
188 assert_eq
!(section2
.address(), 0);
189 assert_eq
!(section2
.size(), 4);
191 let mut symbols
= object
.symbols();
193 let symbol
= symbols
.next().unwrap();
194 println
!("{:?}", symbol
);
195 assert_eq
!(symbol
.name(), Ok(""));
197 let symbol
= symbols
.next().unwrap();
198 let symbol_index
= symbol
.index();
199 println
!("{:?}", symbol
);
200 assert_eq
!(symbol
.name(), Ok("s1"));
201 assert_eq
!(symbol
.kind(), SymbolKind
::Data
);
204 read
::SymbolSection
::Section(section1
.index())
206 assert_eq
!(symbol
.scope(), SymbolScope
::Linkage
);
207 assert_eq
!(symbol
.is_weak(), false);
208 assert_eq
!(symbol
.is_undefined(), false);
209 assert_eq
!(symbol
.address(), 0);
211 let symbol
= symbols
.next();
212 assert
!(symbol
.is_none(), format
!("unexpected symbol {:?}", symbol
));
214 let mut comdats
= object
.comdats();
216 let comdat
= comdats
.next().unwrap();
217 println
!("{:?}", comdat
);
218 assert_eq
!(comdat
.kind(), ComdatKind
::Any
);
219 assert_eq
!(comdat
.symbol(), symbol_index
);
221 let mut comdat_sections
= comdat
.sections();
222 assert_eq
!(comdat_sections
.next(), Some(section1_index
));
223 assert_eq
!(comdat_sections
.next(), Some(section2_index
));
224 assert_eq
!(comdat_sections
.next(), None
);