#![warn(missing_docs)]
#![warn(clippy::all)]
pub mod arm;
pub mod arm64;
pub mod converter;
pub mod dyld;
pub mod error;
pub mod macho;
pub mod util;
pub use converter::{
RuntimeImageReference, extract_image_with_selected_images, referenced_runtime_images,
selected_merge_images,
};
pub use dyld::{
ContainingImages, DyldContext, ImageEntry, MappingEntry, StringHit, StringQuery,
StringSearchOutcome, parse_hex_bytes,
};
pub use error::{Error, Result};
pub use macho::MachOContext;
use std::collections::{HashSet, VecDeque};
use std::fs;
use std::path::Path;
use std::sync::Arc;
use converter::{
ExtractionContext, MergeContext, fix_header_and_load_commands, fix_merged_pointers,
fix_merged_stubs, fix_objc, fix_stubs, include_shared_regions, inject_dependency_segments,
merge_images, optimize_linkedit, optimize_offsets, process_slide_info, rebuild_merged_linkedit,
resolve_indirect_pointers, update_merged_load_commands, write_macho,
};
pub fn extract_image<P: AsRef<Path>>(
cache: &Arc<DyldContext>,
image_path: &str,
output_path: P,
) -> Result<()> {
extract_image_with_options(cache, image_path, output_path, ExtractionOptions::default())
}
#[derive(Debug, Clone)]
pub struct ExtractionOptions {
pub verbosity: u8,
pub skip_slide_info: bool,
pub skip_linkedit: bool,
pub skip_stubs: bool,
pub skip_objc: bool,
}
impl Default for ExtractionOptions {
fn default() -> Self {
Self {
verbosity: 1,
skip_slide_info: false,
skip_linkedit: false,
skip_stubs: false,
skip_objc: false,
}
}
}
#[derive(Debug, Clone)]
pub struct MergeExtractionOptions {
pub verbosity: u8,
pub max_depth: usize,
}
impl Default for MergeExtractionOptions {
fn default() -> Self {
Self {
verbosity: 1,
max_depth: 1,
}
}
}
pub fn extract_image_with_merged_deps<P: AsRef<Path>>(
cache: &Arc<DyldContext>,
image_path: &str,
output_path: P,
options: MergeExtractionOptions,
) -> Result<()> {
let output_path = output_path.as_ref();
let mut ctx = MergeContext::new(Arc::clone(cache), image_path.to_string(), options.max_depth)
.with_verbosity(options.verbosity);
merge_images(&mut ctx)?;
include_shared_regions(&mut ctx)?;
inject_dependency_segments(&mut ctx)?;
update_merged_load_commands(&mut ctx)?;
fix_merged_stubs(&mut ctx)?;
fix_merged_pointers(&mut ctx)?;
resolve_indirect_pointers(&mut ctx)?;
rebuild_merged_linkedit(&mut ctx)?;
if let Some(parent) = output_path.parent() {
if !parent.as_os_str().is_empty() {
fs::create_dir_all(parent).map_err(|e| Error::FileOpen {
path: parent.to_path_buf(),
source: e,
})?;
}
}
fs::write(output_path, &ctx.data).map_err(|e| Error::FileOpen {
path: output_path.to_path_buf(),
source: e,
})?;
Ok(())
}
pub fn extract_image_with_options<P: AsRef<Path>>(
cache: &Arc<DyldContext>,
image_path: &str,
output_path: P,
options: ExtractionOptions,
) -> Result<()> {
let image = cache
.find_image(image_path)
.ok_or_else(|| Error::ImageNotFound {
name: image_path.to_string(),
})?;
let macho = converter::materialize_image(cache, image.address, options.skip_linkedit)?;
let mut ctx =
ExtractionContext::new(Arc::clone(cache), macho, image.path.clone(), image.address)
.with_verbosity(options.verbosity);
fix_header_and_load_commands(&mut ctx)?;
if !options.skip_slide_info {
process_slide_info(&mut ctx)?;
}
if !options.skip_linkedit {
optimize_linkedit(&mut ctx)?;
}
if !options.skip_stubs {
fix_stubs(&mut ctx)?;
converter::include_cache_support(&mut ctx)?;
}
if !options.skip_objc {
fix_objc(&mut ctx)?;
}
let procedures = optimize_offsets(&mut ctx)?;
write_macho(&ctx, &procedures, output_path)?;
Ok(())
}
pub fn list_images(cache: &DyldContext) -> impl Iterator<Item = &ImageEntry> {
cache.iter_images()
}
pub fn find_images<'a>(
cache: &'a DyldContext,
filter: &'a str,
) -> impl Iterator<Item = &'a ImageEntry> {
cache
.iter_images()
.filter(move |img| img.matches_filter(filter))
}
#[derive(Debug, Clone, Default)]
pub struct DependencyExtractionStats {
pub root_images: usize,
pub dependencies: usize,
pub total_extracted: usize,
pub failed: usize,
pub skipped: usize,
}
#[derive(Debug, Clone)]
struct ImageToExtract {
path: String,
depth: usize,
}
fn get_image_dependencies(cache: &DyldContext, image_path: &str) -> Result<Vec<String>> {
let image = cache
.find_image(image_path)
.ok_or_else(|| Error::ImageNotFound {
name: image_path.into(),
})?;
let macho = cache.image_header(image.address)?;
Ok(macho.dependencies())
}
fn build_dependency_graph(
cache: &DyldContext,
root_paths: &[String],
max_depth: Option<usize>,
) -> (Vec<ImageToExtract>, Vec<String>) {
let mut to_extract: Vec<ImageToExtract> = Vec::new();
let mut visited: HashSet<String> = HashSet::new();
let mut not_in_cache: Vec<String> = Vec::new();
let mut queue: VecDeque<ImageToExtract> = VecDeque::new();
for path in root_paths {
if !visited.contains(path) {
visited.insert(path.clone());
queue.push_back(ImageToExtract {
path: path.clone(),
depth: 0,
});
}
}
while let Some(item) = queue.pop_front() {
if let Some(max) = max_depth {
if item.depth > max {
continue;
}
}
if cache.find_image(&item.path).is_none() {
if !not_in_cache.contains(&item.path) {
not_in_cache.push(item.path.clone());
}
continue;
}
to_extract.push(item.clone());
let should_traverse = match max_depth {
Some(max) => item.depth < max,
None => true,
};
if should_traverse {
match get_image_dependencies(cache, &item.path) {
Ok(deps) => {
for dep_path in deps {
let normalized = normalize_dependency_path(&dep_path);
if !visited.contains(&normalized) {
visited.insert(normalized.clone());
queue.push_back(ImageToExtract {
path: normalized,
depth: item.depth + 1,
});
}
}
}
Err(_) => {
}
}
}
}
(to_extract, not_in_cache)
}
fn normalize_dependency_path(path: &str) -> String {
if let Some(rest) = path.strip_prefix("@rpath/") {
if rest.contains('/') {
return format!("/System/Library/{}", rest);
}
if rest.ends_with(".dylib") {
return format!("/usr/lib/{}", rest);
}
let framework_name = rest.trim_end_matches(".dylib");
return format!(
"/System/Library/Frameworks/{}.framework/{}",
framework_name, framework_name
);
}
if let Some(rest) = path.strip_prefix("@executable_path/") {
return format!("/usr/lib/{}", rest);
}
if let Some(rest) = path.strip_prefix("@loader_path/") {
return format!("/usr/lib/{}", rest);
}
path.to_string()
}
pub fn extract_images_with_dependencies<P, F>(
cache: &Arc<DyldContext>,
root_paths: &[String],
output_dir: P,
options: ExtractionOptions,
max_depth: Option<usize>,
mut progress_callback: F,
) -> Result<DependencyExtractionStats>
where
P: AsRef<Path>,
F: FnMut(usize, usize, &str),
{
let output_dir = output_dir.as_ref();
let root_count = root_paths.len();
let (images_to_extract, not_in_cache) = build_dependency_graph(cache, root_paths, max_depth);
let total = images_to_extract.len();
let mut stats = DependencyExtractionStats {
root_images: root_count,
dependencies: total.saturating_sub(root_count),
total_extracted: 0,
failed: 0,
skipped: not_in_cache.len(),
};
fs::create_dir_all(output_dir).map_err(|e| Error::FileOpen {
path: output_dir.to_path_buf(),
source: e,
})?;
let mut extracted: HashSet<String> = HashSet::new();
for (index, item) in images_to_extract.iter().enumerate() {
if extracted.contains(&item.path) {
continue;
}
progress_callback(index + 1, total, &item.path);
let relative_path = item.path.trim_start_matches('/');
let output_path = output_dir.join(relative_path);
if let Some(parent) = output_path.parent() {
if let Err(_) = fs::create_dir_all(parent) {
stats.failed += 1;
continue;
}
}
match extract_image_with_options(cache, &item.path, &output_path, options.clone()) {
Ok(_) => {
stats.total_extracted += 1;
extracted.insert(item.path.clone());
}
Err(_) => {
stats.failed += 1;
}
}
}
Ok(stats)
}