forked from gimli-rs/object
-
Notifications
You must be signed in to change notification settings - Fork 0
/
objdump.rs
286 lines (266 loc) · 9.18 KB
/
objdump.rs
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use object::read::archive::ArchiveFile;
use object::read::coff;
use object::read::macho::{DyldCache, FatArch, MachOFatFile32, MachOFatFile64};
use object::{Endianness, FileKind, Object, ObjectComdat, ObjectSection, ObjectSymbol};
use std::io::{Result, Write};
pub fn print<W: Write, E: Write>(
w: &mut W,
e: &mut E,
file: &[u8],
extra_files: &[&[u8]],
member_names: Vec<String>,
) -> Result<()> {
let mut member_names: Vec<_> = member_names.into_iter().map(|name| (name, false)).collect();
if let Ok(archive) = ArchiveFile::parse(file) {
writeln!(w, "Format: Archive (kind: {:?})", archive.kind())?;
for member in archive.members() {
match member {
Ok(member) => {
if find_member(&mut member_names, member.name()) {
writeln!(w)?;
writeln!(w, "{}:", String::from_utf8_lossy(member.name()))?;
if let Ok(data) = member.data(file) {
if FileKind::parse(data) == Ok(FileKind::CoffImport) {
dump_import(w, e, data)?;
} else {
dump_object(w, e, data)?;
}
}
}
}
Err(err) => writeln!(e, "Failed to parse archive member: {}", err)?,
}
}
} else if let Ok(fat) = MachOFatFile32::parse(file) {
writeln!(w, "Format: Mach-O Fat 32")?;
for arch in fat.arches() {
writeln!(w)?;
writeln!(w, "Fat Arch: {:?}", arch.architecture())?;
match arch.data(file) {
Ok(data) => dump_object(w, e, data)?,
Err(err) => writeln!(e, "Failed to parse Fat 32 data: {}", err)?,
}
}
} else if let Ok(fat) = MachOFatFile64::parse(file) {
writeln!(w, "Format: Mach-O Fat 64")?;
for arch in fat.arches() {
writeln!(w)?;
writeln!(w, "Fat Arch: {:?}", arch.architecture())?;
match arch.data(file) {
Ok(data) => dump_object(w, e, data)?,
Err(err) => writeln!(e, "Failed to parse Fat 64 data: {}", err)?,
}
}
} else if let Ok(cache) = DyldCache::<Endianness>::parse(file, extra_files) {
writeln!(w, "Format: dyld cache {:?}-endian", cache.endianness())?;
writeln!(w, "Architecture: {:?}", cache.architecture())?;
for image in cache.images() {
let path = match image.path() {
Ok(path) => path,
Err(err) => {
writeln!(e, "Failed to parse dyld image name: {}", err)?;
continue;
}
};
if !find_member(&mut member_names, path.as_bytes()) {
continue;
}
writeln!(w)?;
writeln!(w, "{}:", path)?;
let file = match image.parse_object() {
Ok(file) => file,
Err(err) => {
writeln!(e, "Failed to parse file: {}", err)?;
continue;
}
};
dump_parsed_object(w, e, &file)?;
}
} else {
dump_object(w, e, file)?;
}
for (name, found) in member_names {
if !found {
writeln!(e, "Failed to find member '{}", name)?;
}
}
Ok(())
}
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<W: Write, E: Write>(w: &mut W, e: &mut E, data: &[u8]) -> Result<()> {
match object::File::parse(data) {
Ok(file) => {
dump_parsed_object(w, e, &file)?;
}
Err(err) => {
writeln!(e, "Failed to parse file: {}", err)?;
}
}
Ok(())
}
fn dump_parsed_object<W: Write, E: Write>(w: &mut W, e: &mut E, file: &object::File) -> Result<()> {
writeln!(
w,
"Format: {:?} {:?}-endian {}-bit",
file.format(),
file.endianness(),
if file.is_64() { "64" } else { "32" }
)?;
writeln!(w, "Kind: {:?}", file.kind())?;
writeln!(w, "Architecture: {:?}", file.architecture())?;
if let Some(sub_architecture) = file.sub_architecture() {
writeln!(w, "Sub-Architecture: {:?}", sub_architecture)?;
}
writeln!(w, "Flags: {:x?}", file.flags())?;
writeln!(
w,
"Relative Address Base: {:x?}",
file.relative_address_base()
)?;
writeln!(w, "Entry Address: {:x?}", file.entry())?;
match file.mach_uuid() {
Ok(Some(uuid)) => writeln!(w, "Mach UUID: {:x?}", uuid)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse Mach UUID: {}", err)?,
}
match file.build_id() {
Ok(Some(build_id)) => writeln!(w, "Build ID: {:x?}", build_id)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse build ID: {}", err)?,
}
match file.gnu_debuglink() {
Ok(Some((filename, crc))) => writeln!(
w,
"GNU debug link: {} CRC: {:08x}",
String::from_utf8_lossy(filename),
crc,
)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse GNU debug link: {}", err)?,
}
match file.gnu_debugaltlink() {
Ok(Some((filename, build_id))) => writeln!(
w,
"GNU debug alt link: {}, build ID: {:x?}",
String::from_utf8_lossy(filename),
build_id,
)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse GNU debug alt link: {}", err)?,
}
match file.pdb_info() {
Ok(Some(info)) => writeln!(
w,
"PDB file: {}, GUID: {:x?}, Age: {}",
String::from_utf8_lossy(info.path()),
info.guid(),
info.age()
)?,
Ok(None) => {}
Err(err) => writeln!(e, "Failed to parse PE CodeView info: {}", err)?,
}
for segment in file.segments() {
writeln!(w, "{:x?}", segment)?;
}
for section in file.sections() {
writeln!(w, "{}: {:x?}", section.index(), section)?;
}
for comdat in file.comdats() {
write!(w, "{:?} Sections:", comdat)?;
for section in comdat.sections() {
write!(w, " {}", section)?;
}
writeln!(w)?;
}
writeln!(w)?;
writeln!(w, "Symbols")?;
for symbol in file.symbols() {
writeln!(w, "{}: {:x?}", symbol.index(), symbol)?;
}
for section in file.sections() {
if section.relocations().next().is_some() {
writeln!(
w,
"\n{} relocations",
section.name().unwrap_or("<invalid name>")
)?;
for relocation in section.relocations() {
writeln!(w, "{:x?}", relocation)?;
}
}
}
writeln!(w)?;
writeln!(w, "Dynamic symbols")?;
for symbol in file.dynamic_symbols() {
writeln!(w, "{}: {:x?}", symbol.index(), symbol)?;
}
if let Some(relocations) = file.dynamic_relocations() {
writeln!(w)?;
writeln!(w, "Dynamic relocations")?;
for relocation in relocations {
writeln!(w, "{:x?}", relocation)?;
}
}
match file.imports() {
Ok(imports) => {
if !imports.is_empty() {
writeln!(w)?;
for import in imports {
writeln!(w, "{:x?}", import)?;
}
}
}
Err(err) => writeln!(e, "Failed to parse imports: {}", err)?,
}
match file.exports() {
Ok(exports) => {
if !exports.is_empty() {
writeln!(w)?;
for export in exports {
writeln!(w, "{:x?}", export)?;
}
}
}
Err(err) => writeln!(e, "Failed to parse exports: {}", err)?,
}
writeln!(w)?;
writeln!(w, "Symbol map")?;
for symbol in file.symbol_map().symbols() {
writeln!(w, "0x{:x} \"{}\"", symbol.address(), symbol.name())?;
}
Ok(())
}
fn dump_import<W: Write, E: Write>(w: &mut W, e: &mut E, data: &[u8]) -> Result<()> {
let file = match coff::ImportFile::parse(data) {
Ok(import) => import,
Err(err) => {
writeln!(e, "Failed to parse short import: {}", err)?;
return Ok(());
}
};
writeln!(w, "Format: Short Import File")?;
writeln!(w, "Architecture: {:?}", file.architecture())?;
if let Some(sub_architecture) = file.sub_architecture() {
writeln!(w, "Sub-Architecture: {:?}", sub_architecture)?;
}
writeln!(w, "DLL: {:?}", String::from_utf8_lossy(file.dll()))?;
writeln!(w, "Symbol: {:?}", String::from_utf8_lossy(file.symbol()))?;
write!(w, "Import: ")?;
match file.import() {
coff::ImportName::Ordinal(n) => writeln!(w, "Ordinal({})", n)?,
coff::ImportName::Name(name) => writeln!(w, "Name({:?})", String::from_utf8_lossy(name))?,
}
writeln!(w, "Type: {:?}", file.import_type())?;
Ok(())
}