Debian Codegraph @

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

raw
1 use object::read::{Object, ObjectSection};
2 use object::{read, write};
3 use object::{
4 Architecture, BinaryFormat, Endianness, RelocationEncoding, RelocationFlags, RelocationKind,
5 SymbolFlags, SymbolKind, SymbolScope,
6 };
7
8 #[test]
9 fn reloc_overflow() {
10 let mut object =
11 write::Object::new(BinaryFormat::Coff, Architecture::X86_64, Endianness::Little);
12 let text = object.section_id(write::StandardSection::Text);
13 object.append_section_data(text, &[0; 4], 4);
14 let symbol = object.add_symbol(write::Symbol {
15 name: b"f".to_vec(),
16 value: 0,
17 size: 4,
18 kind: SymbolKind::Text,
19 scope: SymbolScope::Linkage,
20 weak: false,
21 section: write::SymbolSection::Section(text),
22 flags: SymbolFlags::None,
23 });
24 for i in 0..0x10000 {
25 object
26 .add_relocation(
27 text,
28 write::Relocation {
29 offset: i,
30 symbol,
31 addend: 0,
32 flags: RelocationFlags::Generic {
33 kind: RelocationKind::Absolute,
34 encoding: RelocationEncoding::Generic,
35 size: 64,
36 },
37 },
38 )
39 .unwrap();
40 }
41 let bytes = object.write().unwrap();
42
43 //std::fs::write(&"reloc_overflow.o", &bytes).unwrap();
44
45 let object = read::File::parse(&*bytes).unwrap();
46 assert_eq!(object.format(), BinaryFormat::Coff);
47 assert_eq!(object.architecture(), Architecture::X86_64);
48
49 let section = object.sections().next().unwrap();
50 assert_eq!(section.name(), Ok(".text"));
51
52 let mut i = 0;
53 for (offset, _relocation) in section.relocations() {
54 assert_eq!(offset, i);
55 i += 1;
56 }
57 assert_eq!(i, 0x10000);
58 }