Debian Codegraph @

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

raw
1 #![cfg(all(feature = "read", feature = "write"))]
2
3 use object::read::{Object, ObjectSection, ObjectSymbol};
4 use object::{read, write};
5 use object::{
6 Architecture, BinaryFormat, Endianness, SectionKind, SymbolFlags, SymbolKind, SymbolScope,
7 };
8
9 #[test]
10 fn coff_x86_64_bss() {
11 let mut object =
12 write::Object::new(BinaryFormat::Coff, Architecture::X86_64, Endianness::Little);
13
14 let section = object.section_id(write::StandardSection::UninitializedData);
15
16 let _bss_section_symbol = object.section_symbol(section);
17
18 let symbol = object.add_symbol(write::Symbol {
19 name: b"v1".to_vec(),
20 value: 0,
21 size: 0,
22 kind: SymbolKind::Data,
23 scope: SymbolScope::Linkage,
24 weak: false,
25 section: write::SymbolSection::Undefined,
26 flags: SymbolFlags::None,
27 });
28 object.add_symbol_bss(symbol, section, 18, 4);
29
30 let symbol = object.add_symbol(write::Symbol {
31 name: b"v2".to_vec(),
32 value: 0,
33 size: 0,
34 kind: SymbolKind::Data,
35 scope: SymbolScope::Linkage,
36 weak: false,
37 section: write::SymbolSection::Undefined,
38 flags: SymbolFlags::None,
39 });
40 object.add_symbol_bss(symbol, section, 34, 8);
41
42 let bytes = object.write().unwrap();
43
44 //std::fs::write(&"bss.o", &bytes).unwrap();
45
46 let object = read::File::parse(&*bytes).unwrap();
47 assert_eq!(object.format(), BinaryFormat::Coff);
48 assert_eq!(object.architecture(), Architecture::X86_64);
49
50 let mut sections = object.sections();
51
52 let bss = sections.next().unwrap();
53 println!("{:?}", bss);
54 let bss_index = bss.index();
55 assert_eq!(bss.name(), Ok(".bss"));
56 assert_eq!(bss.kind(), SectionKind::UninitializedData);
57 assert_eq!(bss.size(), 58);
58 assert_eq!(bss.data(), Ok(&[][..]));
59
60 let section = sections.next();
61 assert!(section.is_none(), "unexpected section {:?}", section);
62
63 let mut symbols = object.symbols();
64
65 let section_symbol = symbols.next().unwrap();
66 println!("{:?}", section_symbol);
67 assert_eq!(section_symbol.name(), Ok(".bss"));
68 assert_eq!(section_symbol.kind(), SymbolKind::Section);
69 assert_eq!(section_symbol.section_index(), Some(bss_index));
70 assert_eq!(section_symbol.scope(), SymbolScope::Compilation);
71 assert!(!section_symbol.is_weak());
72 assert!(!section_symbol.is_undefined());
73 assert_eq!(section_symbol.address(), 0);
74
75 let symbol = symbols.next().unwrap();
76 println!("{:?}", symbol);
77 assert_eq!(symbol.name(), Ok("v1"));
78 assert_eq!(symbol.kind(), SymbolKind::Data);
79 assert_eq!(symbol.section_index(), Some(bss_index));
80 assert_eq!(symbol.scope(), SymbolScope::Linkage);
81 assert!(!symbol.is_weak());
82 assert!(!symbol.is_undefined());
83 assert_eq!(symbol.address(), 0);
84
85 let symbol = symbols.next().unwrap();
86 println!("{:?}", symbol);
87 assert_eq!(symbol.name(), Ok("v2"));
88 assert_eq!(symbol.kind(), SymbolKind::Data);
89 assert_eq!(symbol.section_index(), Some(bss_index));
90 assert_eq!(symbol.scope(), SymbolScope::Linkage);
91 assert!(!symbol.is_weak());
92 assert!(!symbol.is_undefined());
93 assert_eq!(symbol.address(), 24);
94
95 let symbol = symbols.next();
96 assert!(symbol.is_none(), "unexpected symbol {:?}", symbol);
97 }
98
99 #[test]
100 fn elf_x86_64_bss() {
101 let mut object =
102 write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
103
104 let section = object.section_id(write::StandardSection::UninitializedData);
105
106 let symbol = object.add_symbol(write::Symbol {
107 name: b"v1".to_vec(),
108 value: 0,
109 size: 0,
110 kind: SymbolKind::Data,
111 scope: SymbolScope::Linkage,
112 weak: false,
113 section: write::SymbolSection::Undefined,
114 flags: SymbolFlags::None,
115 });
116 object.add_symbol_bss(symbol, section, 18, 4);
117
118 let symbol = object.add_symbol(write::Symbol {
119 name: b"v2".to_vec(),
120 value: 0,
121 size: 0,
122 kind: SymbolKind::Data,
123 scope: SymbolScope::Linkage,
124 weak: false,
125 section: write::SymbolSection::Undefined,
126 flags: SymbolFlags::None,
127 });
128 object.add_symbol_bss(symbol, section, 34, 8);
129
130 let bytes = object.write().unwrap();
131
132 //std::fs::write(&"bss.o", &bytes).unwrap();
133
134 let object = read::File::parse(&*bytes).unwrap();
135 assert_eq!(object.format(), BinaryFormat::Elf);
136 assert_eq!(object.architecture(), Architecture::X86_64);
137
138 let mut sections = object.sections();
139
140 let bss = sections.next().unwrap();
141 println!("{:?}", bss);
142 let bss_index = bss.index();
143 assert_eq!(bss.name(), Ok(".bss"));
144 assert_eq!(bss.kind(), SectionKind::UninitializedData);
145 assert_eq!(bss.size(), 58);
146 assert_eq!(bss.data(), Ok(&[][..]));
147
148 let mut symbols = object.symbols();
149
150 let symbol = symbols.next().unwrap();
151 println!("{:?}", symbol);
152 assert_eq!(symbol.name(), Ok("v1"));
153 assert_eq!(symbol.kind(), SymbolKind::Data);
154 assert_eq!(symbol.section_index(), Some(bss_index));
155 assert_eq!(symbol.scope(), SymbolScope::Linkage);
156 assert!(!symbol.is_weak());
157 assert!(!symbol.is_undefined());
158 assert_eq!(symbol.address(), 0);
159 assert_eq!(symbol.size(), 18);
160
161 let symbol = symbols.next().unwrap();
162 println!("{:?}", symbol);
163 assert_eq!(symbol.name(), Ok("v2"));
164 assert_eq!(symbol.kind(), SymbolKind::Data);
165 assert_eq!(symbol.section_index(), Some(bss_index));
166 assert_eq!(symbol.scope(), SymbolScope::Linkage);
167 assert!(!symbol.is_weak());
168 assert!(!symbol.is_undefined());
169 assert_eq!(symbol.address(), 24);
170 assert_eq!(symbol.size(), 34);
171
172 let symbol = symbols.next();
173 assert!(symbol.is_none(), "unexpected symbol {:?}", symbol);
174 }
175
176 #[test]
177 fn macho_x86_64_bss() {
178 let mut object = write::Object::new(
179 BinaryFormat::MachO,
180 Architecture::X86_64,
181 Endianness::Little,
182 );
183
184 let section = object.section_id(write::StandardSection::UninitializedData);
185
186 let symbol = object.add_symbol(write::Symbol {
187 name: b"v1".to_vec(),
188 value: 0,
189 size: 0,
190 kind: SymbolKind::Data,
191 scope: SymbolScope::Linkage,
192 weak: false,
193 section: write::SymbolSection::Undefined,
194 flags: SymbolFlags::None,
195 });
196 object.add_symbol_bss(symbol, section, 18, 4);
197
198 let symbol = object.add_symbol(write::Symbol {
199 name: b"v2".to_vec(),
200 value: 0,
201 size: 0,
202 kind: SymbolKind::Data,
203 scope: SymbolScope::Linkage,
204 weak: false,
205 section: write::SymbolSection::Undefined,
206 flags: SymbolFlags::None,
207 });
208 object.add_symbol_bss(symbol, section, 34, 8);
209
210 let bytes = object.write().unwrap();
211
212 //std::fs::write(&"bss.o", &bytes).unwrap();
213
214 let object = read::File::parse(&*bytes).unwrap();
215 assert_eq!(object.format(), BinaryFormat::MachO);
216 assert_eq!(object.architecture(), Architecture::X86_64);
217
218 let mut sections = object.sections();
219
220 let bss = sections.next().unwrap();
221 println!("{:?}", bss);
222 let bss_index = bss.index();
223 assert_eq!(bss.name(), Ok("__bss"));
224 assert_eq!(bss.segment_name(), Ok(Some("__DATA")));
225 assert_eq!(bss.kind(), SectionKind::UninitializedData);
226 assert_eq!(bss.size(), 58);
227 assert_eq!(bss.data(), Ok(&[][..]));
228
229 let section = sections.next();
230 assert!(section.is_none(), "unexpected section {:?}", section);
231
232 let mut symbols = object.symbols();
233
234 let symbol = symbols.next().unwrap();
235 println!("{:?}", symbol);
236 assert_eq!(symbol.name(), Ok("_v1"));
237 assert_eq!(symbol.kind(), SymbolKind::Data);
238 assert_eq!(symbol.section_index(), Some(bss_index));
239 assert_eq!(symbol.scope(), SymbolScope::Linkage);
240 assert!(!symbol.is_weak());
241 assert!(!symbol.is_undefined());
242 assert_eq!(symbol.address(), 0);
243
244 let symbol = symbols.next().unwrap();
245 println!("{:?}", symbol);
246 assert_eq!(symbol.name(), Ok("_v2"));
247 assert_eq!(symbol.kind(), SymbolKind::Data);
248 assert_eq!(symbol.section_index(), Some(bss_index));
249 assert_eq!(symbol.scope(), SymbolScope::Linkage);
250 assert!(!symbol.is_weak());
251 assert!(!symbol.is_undefined());
252 assert_eq!(symbol.address(), 24);
253
254 let symbol = symbols.next();
255 assert!(symbol.is_none(), "unexpected symbol {:?}", symbol);
256 }