| 1 | //! Support for reading Mach-O files. |
| 2 | //! |
| 3 | //! Traits are used to abstract over the difference between 32-bit and 64-bit Mach-O |
| 4 | //! files. The primary trait for this is [`MachHeader`]. |
| 5 | //! |
| 6 | //! ## High level API |
| 7 | //! |
| 8 | //! [`MachOFile`] implements the [`Object`](crate::read::Object) trait for Mach-O files. |
| 9 | //! [`MachOFile`] is parameterised by [`MachHeader`] to allow reading both 32-bit and |
| 10 | //! 64-bit Mach-O files. There are type aliases for these parameters ([`MachOFile32`] and |
| 11 | //! [`MachOFile64`]). |
| 12 | //! |
| 13 | //! ## Low level API |
| 14 | //! |
| 15 | //! The [`MachHeader`] trait can be directly used to parse both [`macho::MachHeader32`] |
| 16 | //! and [`macho::MachHeader64`]. Additionally, [`FatHeader`] and the [`FatArch`] trait |
| 17 | //! can be used to iterate images in multi-architecture binaries, and [`DyldCache`] can |
| 18 | //! be used to locate images in a dyld shared cache. |
| 19 | //! |
| 20 | //! ### Example for low level API |
| 21 | //! ```no_run |
| 22 | //! use object::macho; |
| 23 | //! use object::read::macho::{MachHeader, Nlist}; |
| 24 | //! use std::error::Error; |
| 25 | //! use std::fs; |
| 26 | //! |
| 27 | //! /// Reads a file and displays the name of each symbol. |
| 28 | //! fn main() -> Result<(), Box<dyn Error>> { |
| 29 | //! # #[cfg (feature = "std" )] { |
| 30 | //! let data = fs::read("path/to/binary" )?; |
| 31 | //! let header = macho::MachHeader64::<object::Endianness>::parse(&*data, 0)?; |
| 32 | //! let endian = header.endian()?; |
| 33 | //! let mut commands = header.load_commands(endian, &*data, 0)?; |
| 34 | //! while let Some(command) = commands.next()? { |
| 35 | //! if let Some(symtab_command) = command.symtab()? { |
| 36 | //! let symbols = symtab_command.symbols::<macho::MachHeader64<_>, _>(endian, &*data)?; |
| 37 | //! for symbol in symbols.iter() { |
| 38 | //! let name = symbol.name(endian, symbols.strings())?; |
| 39 | //! println!("{}" , String::from_utf8_lossy(name)); |
| 40 | //! } |
| 41 | //! } |
| 42 | //! } |
| 43 | //! # } |
| 44 | //! Ok(()) |
| 45 | //! } |
| 46 | //! ``` |
| 47 | #[cfg (doc)] |
| 48 | use crate::macho; |
| 49 | |
| 50 | mod dyld_cache; |
| 51 | pub use dyld_cache::*; |
| 52 | |
| 53 | mod fat; |
| 54 | pub use fat::*; |
| 55 | |
| 56 | mod file; |
| 57 | pub use file::*; |
| 58 | |
| 59 | mod load_command; |
| 60 | pub use load_command::*; |
| 61 | |
| 62 | mod segment; |
| 63 | pub use segment::*; |
| 64 | |
| 65 | mod section; |
| 66 | pub use section::*; |
| 67 | |
| 68 | mod symbol; |
| 69 | pub use symbol::*; |
| 70 | |
| 71 | mod relocation; |
| 72 | pub use relocation::*; |
| 73 | |