]> git.proxmox.com Git - rustc.git/blob - vendor/object-0.26.2/examples/objdump.rs
New upstream version 1.58.1+dfsg1
[rustc.git] / vendor / object-0.26.2 / examples / objdump.rs
1 use object::read::archive::ArchiveFile;
2 use object::read::macho::{DyldCache, FatArch, FatHeader};
3 use object::{Endianness, Object, ObjectComdat, ObjectSection, ObjectSymbol};
4 use std::{env, fs, process};
5
6 fn main() {
7 let mut args = env::args();
8 let cmd = args.next().unwrap();
9 if args.len() == 0 {
10 eprintln!("Usage: {} <file> [<member>...]", cmd);
11 process::exit(1);
12 }
13 let file_path = args.next().unwrap();
14 let mut member_names: Vec<_> = args.map(|name| (name, false)).collect();
15
16 let file = match fs::File::open(&file_path) {
17 Ok(file) => file,
18 Err(err) => {
19 eprintln!("Failed to open file '{}': {}", file_path, err,);
20 process::exit(1);
21 }
22 };
23 let file = match unsafe { memmap2::Mmap::map(&file) } {
24 Ok(mmap) => mmap,
25 Err(err) => {
26 eprintln!("Failed to map file '{}': {}", file_path, err,);
27 process::exit(1);
28 }
29 };
30
31 if let Ok(archive) = ArchiveFile::parse(&*file) {
32 eprintln!("Format: Archive (kind: {:?})", archive.kind());
33 for member in archive.members() {
34 if let Ok(member) = member {
35 if find_member(&mut member_names, member.name()) {
36 println!();
37 println!("{}:", String::from_utf8_lossy(member.name()));
38 if let Ok(data) = member.data(&*file) {
39 dump_object(data);
40 }
41 }
42 }
43 }
44 } else if let Ok(arches) = FatHeader::parse_arch32(&*file) {
45 println!("Format: Mach-O Fat 32");
46 for arch in arches {
47 println!();
48 println!("Fat Arch: {:?}", arch.architecture());
49 if let Ok(data) = arch.data(&*file) {
50 dump_object(data);
51 }
52 }
53 } else if let Ok(arches) = FatHeader::parse_arch64(&*file) {
54 println!("Format: Mach-O Fat 64");
55 for arch in arches {
56 println!();
57 println!("Fat Arch: {:?}", arch.architecture());
58 if let Ok(data) = arch.data(&*file) {
59 dump_object(data);
60 }
61 }
62 } else if let Ok(cache) = DyldCache::<Endianness>::parse(&*file) {
63 println!("Format: dyld cache {:?}-endian", cache.endianness());
64 println!("Architecture: {:?}", cache.architecture());
65 for image in cache.images() {
66 if let Ok(path) = image.path() {
67 if find_member(&mut member_names, path.as_bytes()) {
68 println!();
69 println!("{}:", path);
70 let file = match image.parse_object() {
71 Ok(file) => file,
72 Err(err) => {
73 eprintln!("Failed to parse file: {}", err);
74 continue;
75 }
76 };
77 dump_parsed_object(&file);
78 }
79 }
80 }
81 } else {
82 dump_object(&*file);
83 }
84
85 for (name, found) in member_names {
86 if !found {
87 eprintln!("Failed to find member '{}", name);
88 }
89 }
90 }
91
92 fn find_member(member_names: &mut [(String, bool)], name: &[u8]) -> bool {
93 if member_names.is_empty() {
94 return true;
95 }
96 match member_names.iter().position(|x| x.0.as_bytes() == name) {
97 Some(i) => {
98 member_names[i].1 = true;
99 true
100 }
101 None => false,
102 }
103 }
104
105 fn dump_object(data: &[u8]) {
106 let file = match object::File::parse(data) {
107 Ok(file) => file,
108 Err(err) => {
109 println!("Failed to parse file: {}", err);
110 return;
111 }
112 };
113 dump_parsed_object(&file);
114 }
115
116 fn dump_parsed_object(file: &object::File) {
117 println!(
118 "Format: {:?} {:?}-endian {}-bit",
119 file.format(),
120 file.endianness(),
121 if file.is_64() { "64" } else { "32" }
122 );
123 println!("Architecture: {:?}", file.architecture());
124 println!("Flags: {:x?}", file.flags());
125 println!("Relative Address Base: {:x?}", file.relative_address_base());
126 println!("Entry Address: {:x?}", file.entry());
127
128 match file.mach_uuid() {
129 Ok(Some(uuid)) => println!("Mach UUID: {:x?}", uuid),
130 Ok(None) => {}
131 Err(e) => println!("Failed to parse Mach UUID: {}", e),
132 }
133 match file.build_id() {
134 Ok(Some(build_id)) => println!("Build ID: {:x?}", build_id),
135 Ok(None) => {}
136 Err(e) => println!("Failed to parse build ID: {}", e),
137 }
138 match file.gnu_debuglink() {
139 Ok(Some((filename, crc))) => println!(
140 "GNU debug link: {} CRC: {:08x}",
141 String::from_utf8_lossy(filename),
142 crc,
143 ),
144 Ok(None) => {}
145 Err(e) => println!("Failed to parse GNU debug link: {}", e),
146 }
147 match file.gnu_debugaltlink() {
148 Ok(Some((filename, build_id))) => println!(
149 "GNU debug alt link: {}, build ID: {:x?}",
150 String::from_utf8_lossy(filename),
151 build_id,
152 ),
153 Ok(None) => {}
154 Err(e) => println!("Failed to parse GNU debug alt link: {}", e),
155 }
156 match file.pdb_info() {
157 Ok(Some(info)) => println!(
158 "PDB file: {}, GUID: {:x?}, Age: {}",
159 String::from_utf8_lossy(info.path()),
160 info.guid(),
161 info.age()
162 ),
163 Ok(None) => {}
164 Err(e) => println!("Failed to parse PE CodeView info: {}", e),
165 }
166
167 for segment in file.segments() {
168 println!("{:x?}", segment);
169 }
170
171 for section in file.sections() {
172 println!("{}: {:x?}", section.index().0, section);
173 }
174
175 for comdat in file.comdats() {
176 print!("{:?} Sections:", comdat);
177 for section in comdat.sections() {
178 print!(" {}", section.0);
179 }
180 println!();
181 }
182
183 println!();
184 println!("Symbols");
185 for symbol in file.symbols() {
186 println!("{}: {:x?}", symbol.index().0, symbol);
187 }
188
189 for section in file.sections() {
190 if section.relocations().next().is_some() {
191 println!(
192 "\n{} relocations",
193 section.name().unwrap_or("<invalid name>")
194 );
195 for relocation in section.relocations() {
196 println!("{:x?}", relocation);
197 }
198 }
199 }
200
201 println!();
202 println!("Dynamic symbols");
203 for symbol in file.dynamic_symbols() {
204 println!("{}: {:x?}", symbol.index().0, symbol);
205 }
206
207 if let Some(relocations) = file.dynamic_relocations() {
208 println!();
209 println!("Dynamic relocations");
210 for relocation in relocations {
211 println!("{:x?}", relocation);
212 }
213 }
214
215 let imports = file.imports().unwrap();
216 if !imports.is_empty() {
217 println!();
218 for import in imports {
219 println!("{:?}", import);
220 }
221 }
222
223 let exports = file.exports().unwrap();
224 if !exports.is_empty() {
225 println!();
226 for export in exports {
227 println!("{:x?}", export);
228 }
229 }
230 }