use std::fs::{File, OpenOptions};
use std::io::{BufWriter, Write};
use std::path::Path;
use memmap2::MmapMut;
use crate::error::{Error, Result};
use crate::macho::{
LC_CODE_SIGNATURE, LC_DATA_IN_CODE, LC_DYLD_CHAINED_FIXUPS, LC_DYLD_EXPORTS_TRIE, LC_DYLD_INFO,
LC_DYLD_INFO_ONLY, LC_DYSYMTAB, LC_FUNCTION_STARTS, LC_SEGMENT_SPLIT_INFO, LC_SYMTAB,
};
use super::{ExtractionContext, WriteProcedure, WriteSource};
const MMAP_THRESHOLD: u64 = 1024 * 1024;
#[allow(dead_code)]
const PAGE_SIZE: u64 = 0x4000;
pub fn optimize_offsets(ctx: &mut ExtractionContext) -> Result<Vec<WriteProcedure>> {
ctx.info("Optimizing file offsets...");
let mut procedures = Vec::new();
let header_size = (ctx.macho.header_size() + ctx.macho.header.sizeofcmds as usize) as u64;
let segments: Vec<_> = ctx
.macho
.segments()
.filter(|s| s.command.filesize > 0)
.cloned()
.collect();
let mut old_linkedit_off = None;
let mut new_linkedit_off = None;
let mut write_offset = 0u64;
for (index, segment) in segments.iter().enumerate() {
let old_fileoff = segment.command.fileoff;
let filesize = segment.command.filesize;
if segment.name() == "__LINKEDIT" {
old_linkedit_off = Some(old_fileoff);
new_linkedit_off = Some(write_offset);
}
if index == 0 {
let remainder = filesize.checked_sub(header_size).ok_or(Error::Parse {
offset: segment.command_offset,
reason: "first segment smaller than header".into(),
})?;
procedures.push(WriteProcedure::from_macho(0, 0, header_size));
if remainder != 0 {
procedures.push(WriteProcedure::from_macho(
header_size,
old_fileoff + header_size,
remainder,
));
}
} else {
procedures.push(WriteProcedure::from_macho(
write_offset,
old_fileoff,
filesize,
));
}
let mut command = segment.command;
command.fileoff = write_offset;
ctx.macho.write_segment(segment.command_offset, &command)?;
for section in &segment.sections {
let mut record = section.section;
if record.offset != 0 {
record.offset = (record.offset as u64)
.checked_sub(old_fileoff)
.and_then(|delta| write_offset.checked_add(delta))
.and_then(|value| u32::try_from(value).ok())
.ok_or(Error::Parse {
offset: section.struct_offset,
reason: "section offset overflow".into(),
})?;
}
ctx.macho.write_section(section.struct_offset, &record)?;
}
write_offset = write_offset.checked_add(filesize).ok_or(Error::Parse {
offset: segment.command_offset,
reason: "output size overflow".into(),
})?;
}
if ctx.has_extra_segment() {
procedures.push(WriteProcedure::from_extra(
write_offset,
0,
ctx.extra_segment_size() as u64,
));
}
if let (Some(old_off), Some(new_off)) = (old_linkedit_off, new_linkedit_off) {
if old_off != new_off {
update_linkedit_load_commands(ctx, old_off, new_off)?;
}
}
Ok(procedures)
}
fn update_linkedit_load_commands(
ctx: &mut ExtractionContext,
old_linkedit_off: u64,
new_linkedit_off: u64,
) -> Result<()> {
let delta = new_linkedit_off as i64 - old_linkedit_off as i64;
let mut offset = ctx.macho.header_size(); let end_offset = ctx.macho.header_size() + ctx.macho.header.sizeofcmds as usize;
while offset < end_offset {
if offset + 8 > ctx.macho.data.len() {
break;
}
let cmd = ctx.macho.read_u32(offset)?;
let cmdsize = ctx.macho.read_u32(offset + 4)?;
match cmd {
LC_SYMTAB => {
let nsyms = ctx.macho.read_u32(offset + 12)?;
let strsize = ctx.macho.read_u32(offset + 20)?;
if nsyms > 0 {
update_offset_field(ctx, offset + 8, delta)?; }
if strsize > 0 {
update_offset_field(ctx, offset + 16, delta)?; }
}
LC_DYSYMTAB => {
if ctx.macho.read_u32(offset + 36)? > 0 {
update_offset_field(ctx, offset + 32, delta)?;
}
if ctx.macho.read_u32(offset + 44)? > 0 {
update_offset_field(ctx, offset + 40, delta)?;
}
if ctx.macho.read_u32(offset + 52)? > 0 {
update_offset_field(ctx, offset + 48, delta)?;
}
if ctx.macho.read_u32(offset + 60)? > 0 {
update_offset_field(ctx, offset + 56, delta)?;
}
if ctx.macho.read_u32(offset + 68)? > 0 {
update_offset_field(ctx, offset + 64, delta)?;
}
if ctx.macho.read_u32(offset + 76)? > 0 {
update_offset_field(ctx, offset + 72, delta)?;
}
}
LC_DYLD_INFO | LC_DYLD_INFO_ONLY => {
if ctx.macho.read_u32(offset + 12)? > 0 {
update_offset_field(ctx, offset + 8, delta)?;
}
if ctx.macho.read_u32(offset + 20)? > 0 {
update_offset_field(ctx, offset + 16, delta)?;
}
if ctx.macho.read_u32(offset + 28)? > 0 {
update_offset_field(ctx, offset + 24, delta)?;
}
if ctx.macho.read_u32(offset + 36)? > 0 {
update_offset_field(ctx, offset + 32, delta)?;
}
if ctx.macho.read_u32(offset + 44)? > 0 {
update_offset_field(ctx, offset + 40, delta)?;
}
}
LC_FUNCTION_STARTS
| LC_DATA_IN_CODE
| LC_CODE_SIGNATURE
| LC_SEGMENT_SPLIT_INFO
| LC_DYLD_EXPORTS_TRIE
| LC_DYLD_CHAINED_FIXUPS => {
let datasize = ctx.macho.read_u32(offset + 12)?;
if datasize > 0 {
update_offset_field(ctx, offset + 8, delta)?;
}
}
_ => {}
}
offset += cmdsize as usize;
}
Ok(())
}
fn update_offset_field(ctx: &mut ExtractionContext, offset: usize, delta: i64) -> Result<()> {
let old_value = ctx.macho.read_u32(offset)?;
if old_value != 0 {
let new_value = (old_value as i64 + delta) as u32;
ctx.macho.write_u32(offset, new_value)?;
}
Ok(())
}
pub fn write_macho<P: AsRef<Path>>(
ctx: &ExtractionContext,
procedures: &[WriteProcedure],
output_path: P,
) -> Result<()> {
let path = output_path.as_ref();
if let Some(parent) = path.parent() {
std::fs::create_dir_all(parent).map_err(|e| Error::FileWrite {
path: path.to_path_buf(),
source: e,
})?;
}
let content_size = procedures
.iter()
.map(|p| p.write_offset + p.size)
.max()
.unwrap_or(0);
let page_size = 4096u64;
let total_size = (content_size + page_size - 1) & !(page_size - 1);
if total_size >= MMAP_THRESHOLD {
write_macho_mmap(ctx, procedures, path, total_size)
} else {
write_macho_buffered(ctx, procedures, path, total_size)
}
}
#[inline(never)]
fn write_macho_mmap(
ctx: &ExtractionContext,
procedures: &[WriteProcedure],
path: &Path,
total_size: u64,
) -> Result<()> {
let file = OpenOptions::new()
.read(true)
.write(true)
.create(true)
.truncate(true)
.open(path)
.map_err(|e| Error::FileWrite {
path: path.to_path_buf(),
source: e,
})?;
file.set_len(total_size).map_err(|e| Error::FileWrite {
path: path.to_path_buf(),
source: e,
})?;
let mut mmap = unsafe {
MmapMut::map_mut(&file).map_err(|e| Error::FileWrite {
path: path.to_path_buf(),
source: std::io::Error::new(std::io::ErrorKind::Other, e),
})?
};
for proc in procedures {
let src_data = match proc.source {
WriteSource::Cache { subcache_index } => {
let cache_data = ctx.cache.data_for_subcache(subcache_index);
let start = proc.read_offset as usize;
let end = start + proc.size as usize;
if end <= cache_data.len() {
&cache_data[start..end]
} else {
continue;
}
}
WriteSource::Macho => {
let start = proc.read_offset as usize;
let end = start + proc.size as usize;
if end <= ctx.macho.data.len() {
&ctx.macho.data[start..end]
} else {
continue;
}
}
WriteSource::ExtraSegment => {
let start = proc.read_offset as usize;
let end = start + proc.size as usize;
if end <= ctx.extra_segment_data.len() {
&ctx.extra_segment_data[start..end]
} else {
continue;
}
}
};
let dst_start = proc.write_offset as usize;
let dst_end = dst_start + src_data.len();
if dst_end <= mmap.len() {
mmap[dst_start..dst_end].copy_from_slice(src_data);
}
}
mmap.flush().map_err(|e| Error::FileWrite {
path: path.to_path_buf(),
source: e,
})?;
Ok(())
}
#[inline(never)]
fn write_macho_buffered(
ctx: &ExtractionContext,
procedures: &[WriteProcedure],
path: &Path,
total_size: u64,
) -> Result<()> {
let file = File::create(path).map_err(|e| Error::FileWrite {
path: path.to_path_buf(),
source: e,
})?;
let mut writer = BufWriter::with_capacity(64 * 1024, file);
let mut output = vec![0u8; total_size as usize];
for proc in procedures {
let src_data = match proc.source {
WriteSource::Cache { subcache_index } => {
let cache_data = ctx.cache.data_for_subcache(subcache_index);
let start = proc.read_offset as usize;
let end = start + proc.size as usize;
if end <= cache_data.len() {
&cache_data[start..end]
} else {
continue;
}
}
WriteSource::Macho => {
let start = proc.read_offset as usize;
let end = start + proc.size as usize;
if end <= ctx.macho.data.len() {
&ctx.macho.data[start..end]
} else {
continue;
}
}
WriteSource::ExtraSegment => {
let start = proc.read_offset as usize;
let end = start + proc.size as usize;
if end <= ctx.extra_segment_data.len() {
&ctx.extra_segment_data[start..end]
} else {
continue;
}
}
};
let dst_start = proc.write_offset as usize;
let dst_end = dst_start + src_data.len();
if dst_end <= output.len() {
output[dst_start..dst_end].copy_from_slice(src_data);
}
}
writer.write_all(&output).map_err(|e| Error::FileWrite {
path: path.to_path_buf(),
source: e,
})?;
writer.flush().map_err(|e| Error::FileWrite {
path: path.to_path_buf(),
source: e,
})?;
Ok(())
}
#[inline]
#[allow(dead_code)]
fn align_to(value: u64, alignment: u64) -> u64 {
(value + alignment - 1) & !(alignment - 1)
}
pub(crate) fn assemble_macho(
ctx: &ExtractionContext,
procedures: &[WriteProcedure],
) -> Result<Vec<u8>> {
let bad = || Error::Parse {
offset: 0,
reason: "Mach-O write extent out of bounds".into(),
};
let size = procedures.iter().try_fold(0u64, |size, p| {
p.write_offset
.checked_add(p.size)
.map(|end| size.max(end))
.ok_or_else(bad)
})?;
let size = size
.checked_add(4095)
.map(|v| v & !4095)
.and_then(|v| usize::try_from(v).ok())
.ok_or_else(bad)?;
let mut output = vec![0; size];
for p in procedures {
let source = match p.source {
WriteSource::Macho => &ctx.macho.data[..],
WriteSource::ExtraSegment => &ctx.extra_segment_data[..],
WriteSource::Cache { subcache_index } => ctx.cache.data_for_subcache(subcache_index),
};
let start = usize::try_from(p.read_offset).map_err(|_| bad())?;
let len = usize::try_from(p.size).map_err(|_| bad())?;
let end = start.checked_add(len).ok_or_else(bad)?;
let dest = usize::try_from(p.write_offset).map_err(|_| bad())?;
let dest_end = dest.checked_add(len).ok_or_else(bad)?;
output
.get_mut(dest..dest_end)
.ok_or_else(bad)?
.copy_from_slice(source.get(start..end).ok_or_else(bad)?);
}
Ok(output)
}