Line data Source code
1 : //! Module for the Mach-O file format support in LIEF.
2 : //!
3 : //! To get started, one can use [`crate::macho::parse`], [`crate::macho::FatBinary::parse`] or
4 : //! [`crate::Binary::parse`] to instantiate a [`crate::macho::FatBinary`].
5 : //!
6 : //! Even though the targeted Mach-O binary is not FAT, LIEF **always** return a [`crate::macho::FatBinary`]
7 : //! which can wrap a single [`Binary`].
8 : //!
9 : //! ```
10 : //! let fat = lief::macho::parse("non-fat.macho").unwrap();
11 : //! assert!(fat.iter().len() == 1);
12 : //!
13 : //! let fat = lief::macho::parse("real-fat.macho").unwrap();
14 : //! assert!(fat.iter().len() > 1);
15 : //! ```
16 : //!
17 : //! The [`Binary`] structure exposes the main interface to inspect or modify Mach-O binaries:
18 : //!
19 : //! ```
20 : //! fn inspect_macho(macho: &lief::macho::Binary) {
21 : //! for cmd in macho.commands() {
22 : //! println!("{:?}", cmd);
23 : //! }
24 : //! }
25 : //! ```
26 : //!
27 : pub mod binary;
28 : pub mod binding_info;
29 : pub mod commands;
30 : pub mod export_info;
31 : pub mod fat_binary;
32 : pub mod relocation;
33 : pub mod section;
34 : pub mod symbol;
35 : pub mod header;
36 : pub mod stub;
37 : pub mod builder;
38 :
39 : use std::path::Path;
40 : use lief_ffi as ffi;
41 :
42 : #[doc(inline)]
43 : pub use binary::Binary;
44 : #[doc(inline)]
45 : pub use binding_info::BindingInfo;
46 : #[doc(inline)]
47 : pub use export_info::ExportInfo;
48 : #[doc(inline)]
49 : pub use fat_binary::FatBinary;
50 : #[doc(inline)]
51 : pub use relocation::Relocation;
52 : #[doc(inline)]
53 : pub use section::Section;
54 : #[doc(inline)]
55 : pub use symbol::Symbol;
56 : #[doc(inline)]
57 : pub use commands::Commands;
58 : #[doc(inline)]
59 : pub use header::Header;
60 : #[doc(inline)]
61 : pub use stub::Stub;
62 :
63 : use crate::common::AsFFI;
64 :
65 : /// Parse a Mach-O file from the given file path
66 0 : pub fn parse<P: AsRef<Path>>(path: P) -> Option<FatBinary> {
67 0 : FatBinary::parse(path)
68 0 : }
69 :
70 : /// Check that the layout of the given binary is correct from the loader
71 : /// perspective
72 0 : pub fn check_layout(binary: &Binary) -> Result<(), String> {
73 0 : cxx::let_cxx_string!(error = "");
74 0 : unsafe {
75 0 : if ffi::MachO_Utils::check_layout(binary.as_ffi(), error.as_mut().get_unchecked_mut()) {
76 0 : return Ok(());
77 0 : }
78 0 : }
79 0 : Err(error.to_string())
80 0 : }
81 :
82 : /// Check that the layout of the given FAT binary is correct from the loader
83 : /// perspective
84 0 : pub fn check_fat_layout(fat: &FatBinary) -> Result<(), String> {
85 0 : cxx::let_cxx_string!(error = "");
86 0 : unsafe {
87 0 : if ffi::MachO_Utils::check_layout_fat(fat.as_ffi(), error.as_mut().get_unchecked_mut()) {
88 0 : return Ok(());
89 0 : }
90 0 : }
91 0 : Err(error.to_string())
92 0 : }
|