summaryrefslogtreecommitdiffstats
path: root/third_party/rust/object/tests/round_trip/elf.rs
blob: 048db5795a9c1706c416581b4af676a52e145d89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
use object::read::elf::{FileHeader, SectionHeader};
use object::read::{Object, ObjectSymbol};
use object::{
    elf, read, write, Architecture, BinaryFormat, Endianness, LittleEndian, SectionIndex,
    SectionKind, SymbolFlags, SymbolKind, SymbolScope, SymbolSection, U32,
};
use std::io::Write;

#[test]
fn symtab_shndx() {
    let mut object =
        write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);

    for i in 0..0x10000 {
        let name = format!("func{}", i).into_bytes();
        let (section, offset) =
            object.add_subsection(write::StandardSection::Text, &name, &[0xcc], 1);
        object.add_symbol(write::Symbol {
            name,
            value: offset,
            size: 1,
            kind: SymbolKind::Text,
            scope: SymbolScope::Linkage,
            weak: false,
            section: write::SymbolSection::Section(section),
            flags: SymbolFlags::None,
        });
    }
    let bytes = object.write().unwrap();

    //std::fs::write(&"symtab_shndx.o", &bytes).unwrap();

    let object = read::File::parse(&*bytes).unwrap();
    assert_eq!(object.format(), BinaryFormat::Elf);
    assert_eq!(object.architecture(), Architecture::X86_64);

    for symbol in object.symbols().skip(1) {
        assert_eq!(
            symbol.section(),
            SymbolSection::Section(SectionIndex(symbol.index().0))
        );
    }
}

#[cfg(feature = "compression")]
#[test]
fn compression_zlib() {
    use object::read::ObjectSection;
    use object::LittleEndian as LE;

    let data = b"test data data data";
    let len = data.len() as u64;

    let mut ch = object::elf::CompressionHeader64::<LE>::default();
    ch.ch_type.set(LE, object::elf::ELFCOMPRESS_ZLIB);
    ch.ch_size.set(LE, len);
    ch.ch_addralign.set(LE, 1);

    let mut buf = Vec::new();
    buf.write(object::bytes_of(&ch)).unwrap();
    let mut encoder = flate2::write::ZlibEncoder::new(buf, flate2::Compression::default());
    encoder.write_all(data).unwrap();
    let compressed = encoder.finish().unwrap();

    let mut object =
        write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
    let section = object.add_section(
        Vec::new(),
        b".debug_info".to_vec(),
        object::SectionKind::Other,
    );
    object.section_mut(section).set_data(compressed, 1);
    object.section_mut(section).flags = object::SectionFlags::Elf {
        sh_flags: object::elf::SHF_COMPRESSED.into(),
    };
    let bytes = object.write().unwrap();

    //std::fs::write(&"compression.o", &bytes).unwrap();

    let object = read::File::parse(&*bytes).unwrap();
    assert_eq!(object.format(), BinaryFormat::Elf);
    assert_eq!(object.architecture(), Architecture::X86_64);

    let section = object.section_by_name(".debug_info").unwrap();
    let uncompressed = section.uncompressed_data().unwrap();
    assert_eq!(data, &*uncompressed);
}

#[cfg(feature = "compression")]
#[test]
fn compression_gnu() {
    use object::read::ObjectSection;
    use std::io::Write;

    let data = b"test data data data";
    let len = data.len() as u32;

    let mut buf = Vec::new();
    buf.write_all(b"ZLIB\0\0\0\0").unwrap();
    buf.write_all(&len.to_be_bytes()).unwrap();
    let mut encoder = flate2::write::ZlibEncoder::new(buf, flate2::Compression::default());
    encoder.write_all(data).unwrap();
    let compressed = encoder.finish().unwrap();

    let mut object =
        write::Object::new(BinaryFormat::Elf, Architecture::X86_64, Endianness::Little);
    let section = object.add_section(
        Vec::new(),
        b".zdebug_info".to_vec(),
        object::SectionKind::Other,
    );
    object.section_mut(section).set_data(compressed, 1);
    let bytes = object.write().unwrap();

    //std::fs::write(&"compression.o", &bytes).unwrap();

    let object = read::File::parse(&*bytes).unwrap();
    assert_eq!(object.format(), BinaryFormat::Elf);
    assert_eq!(object.architecture(), Architecture::X86_64);

    let section = object.section_by_name(".zdebug_info").unwrap();
    let uncompressed = section.uncompressed_data().unwrap();
    assert_eq!(data, &*uncompressed);
}

#[test]
fn note() {
    let endian = Endianness::Little;
    let mut object = write::Object::new(BinaryFormat::Elf, Architecture::X86_64, endian);

    // Add note section with align = 4.
    let mut buffer = Vec::new();

    buffer
        .write(object::bytes_of(&elf::NoteHeader32 {
            n_namesz: U32::new(endian, 6),
            n_descsz: U32::new(endian, 11),
            n_type: U32::new(endian, 1),
        }))
        .unwrap();
    buffer.write(b"name1\0\0\0").unwrap();
    buffer.write(b"descriptor\0\0").unwrap();

    buffer
        .write(object::bytes_of(&elf::NoteHeader32 {
            n_namesz: U32::new(endian, 6),
            n_descsz: U32::new(endian, 11),
            n_type: U32::new(endian, 2),
        }))
        .unwrap();
    buffer.write(b"name2\0\0\0").unwrap();
    buffer.write(b"descriptor\0\0").unwrap();

    let section = object.add_section(Vec::new(), b".note4".to_vec(), SectionKind::Note);
    object.section_mut(section).set_data(buffer, 4);

    // Add note section with align = 8.
    let mut buffer = Vec::new();

    buffer
        .write(object::bytes_of(&elf::NoteHeader32 {
            n_namesz: U32::new(endian, 6),
            n_descsz: U32::new(endian, 11),
            n_type: U32::new(endian, 1),
        }))
        .unwrap();
    buffer.write(b"name1\0\0\0\0\0\0\0").unwrap();
    buffer.write(b"descriptor\0\0\0\0\0\0").unwrap();

    buffer
        .write(object::bytes_of(&elf::NoteHeader32 {
            n_namesz: U32::new(endian, 4),
            n_descsz: U32::new(endian, 11),
            n_type: U32::new(endian, 2),
        }))
        .unwrap();
    buffer.write(b"abc\0").unwrap();
    buffer.write(b"descriptor\0\0\0\0\0\0").unwrap();

    let section = object.add_section(Vec::new(), b".note8".to_vec(), SectionKind::Note);
    object.section_mut(section).set_data(buffer, 8);

    let bytes = &*object.write().unwrap();

    //std::fs::write(&"note.o", &bytes).unwrap();

    let header = elf::FileHeader64::parse(bytes).unwrap();
    let endian: LittleEndian = header.endian().unwrap();
    let sections = header.sections(endian, bytes).unwrap();

    let section = sections.section(SectionIndex(1)).unwrap();
    assert_eq!(sections.section_name(endian, section).unwrap(), b".note4");
    assert_eq!(section.sh_addralign(endian), 4);
    let mut notes = section.notes(endian, bytes).unwrap().unwrap();
    let note = notes.next().unwrap().unwrap();
    assert_eq!(note.name(), b"name1");
    assert_eq!(note.desc(), b"descriptor\0");
    assert_eq!(note.n_type(endian), 1);
    let note = notes.next().unwrap().unwrap();
    assert_eq!(note.name(), b"name2");
    assert_eq!(note.desc(), b"descriptor\0");
    assert_eq!(note.n_type(endian), 2);
    assert!(notes.next().unwrap().is_none());

    let section = sections.section(SectionIndex(2)).unwrap();
    assert_eq!(sections.section_name(endian, section).unwrap(), b".note8");
    assert_eq!(section.sh_addralign(endian), 8);
    let mut notes = section.notes(endian, bytes).unwrap().unwrap();
    let note = notes.next().unwrap().unwrap();
    assert_eq!(note.name(), b"name1");
    assert_eq!(note.desc(), b"descriptor\0");
    assert_eq!(note.n_type(endian), 1);
    let note = notes.next().unwrap().unwrap();
    assert_eq!(note.name(), b"abc");
    assert_eq!(note.desc(), b"descriptor\0");
    assert_eq!(note.n_type(endian), 2);
    assert!(notes.next().unwrap().is_none());
}