summaryrefslogtreecommitdiffstats
path: root/vendor/object-0.26.2/examples/objdump.rs
blob: b6eb0003f2b93b8e712b72548cfef47581967fc0 (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
219
220
221
222
223
224
225
226
227
228
229
230
use object::read::archive::ArchiveFile;
use object::read::macho::{DyldCache, FatArch, FatHeader};
use object::{Endianness, Object, ObjectComdat, ObjectSection, ObjectSymbol};
use std::{env, fs, process};

fn main() {
    let mut args = env::args();
    let cmd = args.next().unwrap();
    if args.len() == 0 {
        eprintln!("Usage: {} <file> [<member>...]", cmd);
        process::exit(1);
    }
    let file_path = args.next().unwrap();
    let mut member_names: Vec<_> = args.map(|name| (name, false)).collect();

    let file = match fs::File::open(&file_path) {
        Ok(file) => file,
        Err(err) => {
            eprintln!("Failed to open file '{}': {}", file_path, err,);
            process::exit(1);
        }
    };
    let file = match unsafe { memmap2::Mmap::map(&file) } {
        Ok(mmap) => mmap,
        Err(err) => {
            eprintln!("Failed to map file '{}': {}", file_path, err,);
            process::exit(1);
        }
    };

    if let Ok(archive) = ArchiveFile::parse(&*file) {
        eprintln!("Format: Archive (kind: {:?})", archive.kind());
        for member in archive.members() {
            if let Ok(member) = member {
                if find_member(&mut member_names, member.name()) {
                    println!();
                    println!("{}:", String::from_utf8_lossy(member.name()));
                    if let Ok(data) = member.data(&*file) {
                        dump_object(data);
                    }
                }
            }
        }
    } else if let Ok(arches) = FatHeader::parse_arch32(&*file) {
        println!("Format: Mach-O Fat 32");
        for arch in arches {
            println!();
            println!("Fat Arch: {:?}", arch.architecture());
            if let Ok(data) = arch.data(&*file) {
                dump_object(data);
            }
        }
    } else if let Ok(arches) = FatHeader::parse_arch64(&*file) {
        println!("Format: Mach-O Fat 64");
        for arch in arches {
            println!();
            println!("Fat Arch: {:?}", arch.architecture());
            if let Ok(data) = arch.data(&*file) {
                dump_object(data);
            }
        }
    } else if let Ok(cache) = DyldCache::<Endianness>::parse(&*file) {
        println!("Format: dyld cache {:?}-endian", cache.endianness());
        println!("Architecture: {:?}", cache.architecture());
        for image in cache.images() {
            if let Ok(path) = image.path() {
                if find_member(&mut member_names, path.as_bytes()) {
                    println!();
                    println!("{}:", path);
                    let file = match image.parse_object() {
                        Ok(file) => file,
                        Err(err) => {
                            eprintln!("Failed to parse file: {}", err);
                            continue;
                        }
                    };
                    dump_parsed_object(&file);
                }
            }
        }
    } else {
        dump_object(&*file);
    }

    for (name, found) in member_names {
        if !found {
            eprintln!("Failed to find member '{}", name);
        }
    }
}

fn find_member(member_names: &mut [(String, bool)], name: &[u8]) -> bool {
    if member_names.is_empty() {
        return true;
    }
    match member_names.iter().position(|x| x.0.as_bytes() == name) {
        Some(i) => {
            member_names[i].1 = true;
            true
        }
        None => false,
    }
}

fn dump_object(data: &[u8]) {
    let file = match object::File::parse(data) {
        Ok(file) => file,
        Err(err) => {
            println!("Failed to parse file: {}", err);
            return;
        }
    };
    dump_parsed_object(&file);
}

fn dump_parsed_object(file: &object::File) {
    println!(
        "Format: {:?} {:?}-endian {}-bit",
        file.format(),
        file.endianness(),
        if file.is_64() { "64" } else { "32" }
    );
    println!("Architecture: {:?}", file.architecture());
    println!("Flags: {:x?}", file.flags());
    println!("Relative Address Base: {:x?}", file.relative_address_base());
    println!("Entry Address: {:x?}", file.entry());

    match file.mach_uuid() {
        Ok(Some(uuid)) => println!("Mach UUID: {:x?}", uuid),
        Ok(None) => {}
        Err(e) => println!("Failed to parse Mach UUID: {}", e),
    }
    match file.build_id() {
        Ok(Some(build_id)) => println!("Build ID: {:x?}", build_id),
        Ok(None) => {}
        Err(e) => println!("Failed to parse build ID: {}", e),
    }
    match file.gnu_debuglink() {
        Ok(Some((filename, crc))) => println!(
            "GNU debug link: {} CRC: {:08x}",
            String::from_utf8_lossy(filename),
            crc,
        ),
        Ok(None) => {}
        Err(e) => println!("Failed to parse GNU debug link: {}", e),
    }
    match file.gnu_debugaltlink() {
        Ok(Some((filename, build_id))) => println!(
            "GNU debug alt link: {}, build ID: {:x?}",
            String::from_utf8_lossy(filename),
            build_id,
        ),
        Ok(None) => {}
        Err(e) => println!("Failed to parse GNU debug alt link: {}", e),
    }
    match file.pdb_info() {
        Ok(Some(info)) => println!(
            "PDB file: {}, GUID: {:x?}, Age: {}",
            String::from_utf8_lossy(info.path()),
            info.guid(),
            info.age()
        ),
        Ok(None) => {}
        Err(e) => println!("Failed to parse PE CodeView info: {}", e),
    }

    for segment in file.segments() {
        println!("{:x?}", segment);
    }

    for section in file.sections() {
        println!("{}: {:x?}", section.index().0, section);
    }

    for comdat in file.comdats() {
        print!("{:?} Sections:", comdat);
        for section in comdat.sections() {
            print!(" {}", section.0);
        }
        println!();
    }

    println!();
    println!("Symbols");
    for symbol in file.symbols() {
        println!("{}: {:x?}", symbol.index().0, symbol);
    }

    for section in file.sections() {
        if section.relocations().next().is_some() {
            println!(
                "\n{} relocations",
                section.name().unwrap_or("<invalid name>")
            );
            for relocation in section.relocations() {
                println!("{:x?}", relocation);
            }
        }
    }

    println!();
    println!("Dynamic symbols");
    for symbol in file.dynamic_symbols() {
        println!("{}: {:x?}", symbol.index().0, symbol);
    }

    if let Some(relocations) = file.dynamic_relocations() {
        println!();
        println!("Dynamic relocations");
        for relocation in relocations {
            println!("{:x?}", relocation);
        }
    }

    let imports = file.imports().unwrap();
    if !imports.is_empty() {
        println!();
        for import in imports {
            println!("{:?}", import);
        }
    }

    let exports = file.exports().unwrap();
    if !exports.is_empty() {
        println!();
        for export in exports {
            println!("{:x?}", export);
        }
    }
}