From 3fcfd11de305b4538d2d781d0eea37d4be3c080b Mon Sep 17 00:00:00 2001 From: Garvit Surana <251430630+garvitsurana271@users.noreply.github.com> Date: Sat, 21 Nov 2026 22:18:15 +0000 Subject: [PATCH] fix(xtask): name code outside an image's __text by its section, so stubs read as __auth_stubs or __objc_stubs, where the symbolizer gave them the name of the last __text function before them --- xtask/src/symbolize/code.rs | 62 ++++++++++++++++++++++++++++++--- xtask/src/symbolize/fixtures.rs | 43 +++++++++++++++-------- 2 files changed, 85 insertions(+), 20 deletions(-) diff --git a/xtask/src/symbolize/code.rs b/xtask/src/symbolize/code.rs index 32079cced1..4708f3eff3 100644 --- a/xtask/src/symbolize/code.rs +++ b/xtask/src/symbolize/code.rs @@ -10,7 +10,9 @@ use anyhow::{Context, Result, anyhow, bail, ensure}; use object::macho::{self, LinkeditDataCommand}; use object::read::macho::{DyldCache, MachOFile64}; -use object::{Endianness, Object, ObjectSegment, ObjectSymbol, SegmentFlags, SymbolKind}; +use object::{ + Endianness, Object, ObjectSection, ObjectSegment, ObjectSymbol, SegmentFlags, SymbolKind, +}; use std::collections::{HashMap, HashSet}; use std::path::{Path, PathBuf}; @@ -62,6 +64,9 @@ pub(super) struct Image { /// Code symbols and function starts, ascending. symbols: Vec<(u64, String)>, starts: Vec, + /// The sections of its executable segments, `(start, end, name)`: + /// code outside `__text` (stubs) is named by its section. + sections: Vec<(u64, u64, String)>, } #[derive(Debug, Default)] @@ -279,13 +284,32 @@ impl Code { mut starts: Vec, ) { let index = self.images.len(); - let ranges = &mut self.files[file_index].ranges; + let mut code = Vec::new(); for segment in file.segments() { if executable(segment.flags()) && segment.size() != 0 { let start = segment.address(); - ranges.push((start, start + segment.size(), index)); + code.push((start, start + segment.size(), index)); } } + // The sections that lie in its code. + let mut sections: Vec<(u64, u64, String)> = file + .sections() + .filter(|section| { + code.iter() + .any(|range| range.0 <= section.address() && section.address() < range.1) + }) + .filter_map(|section| { + let start = section.address(); + Some(( + start, + start + section.size(), + section.name().ok()?.to_owned(), + )) + }) + .collect(); + sections.sort(); + let ranges = &mut self.files[file_index].ranges; + ranges.extend(code); ranges.sort_unstable(); let mut symbols: Vec<(u64, String)> = file .symbols() @@ -301,6 +325,7 @@ impl Code { path: path.to_owned(), symbols, starts, + sections, }); } @@ -338,6 +363,21 @@ impl Code { return unnamed; }; let image = &self.images[index]; + // Stubs and other code outside `__text` hold no function: the + // symbol before them would name the last function of `__text`. + let section = image + .sections + .iter() + .find(|section| section.0 <= address && address < section.1); + if let Some((start, _, name)) = section + && name != "__text" + { + return Named { + image: Some(&image.path), + function: name.clone(), + start: *start, + }; + } let symbol = image .symbols .partition_point(|symbol| symbol.0 <= address) @@ -554,7 +594,7 @@ impl Drop for Mapped { #[cfg(test)] mod tests { - use super::super::fixtures::{self, ALPHA, BETA, CACHE_BASE, CACHE_PATH, PAGE, UNNAMED}; + use super::super::fixtures::{self, ALPHA, BETA, CACHE_BASE, CACHE_PATH, PAGE, STUBS, UNNAMED}; use super::*; const BASE: u64 = 0x7000_0000; @@ -617,6 +657,17 @@ mod tests { ); assert_eq!(code.name(0, BASE + 0x2020).function, "_beta"); assert_eq!(code.name(0, BASE + 0x2020).start, BASE + BETA); + // Code outside `__text` is named by its section, not by the + // function before it. + assert_eq!( + code.name(0, BASE + STUBS + 0x10), + Named { + image, + function: "__auth_stubs".into(), + start: BASE + STUBS + } + ); + assert_eq!(code.name(0, BASE + STUBS - 4).function, "_beta"); // Outside every image. assert_eq!(code.name(0, 0x10).image, None); assert_eq!( @@ -654,7 +705,8 @@ mod tests { code.name(0, CACHE_BASE + UNNAMED).function, format!("sub_{:x}", CACHE_BASE + UNNAMED) ); - assert_eq!(code.name(0, CACHE_BASE + 0x2ffc).function, "_beta"); + assert_eq!(code.name(0, CACHE_BASE + STUBS - 4).function, "_beta"); + assert_eq!(code.name(0, CACHE_BASE + 0x2ffc).function, "__auth_stubs"); assert_eq!( code.loaded, ["dyld_shared_cache_arm64e: 1 image, 2 of 2 wanted pages"] diff --git a/xtask/src/symbolize/fixtures.rs b/xtask/src/symbolize/fixtures.rs index 932e3e5053..721dbcab00 100644 --- a/xtask/src/symbolize/fixtures.rs +++ b/xtask/src/symbolize/fixtures.rs @@ -14,6 +14,9 @@ const LINKEDIT: usize = PAGE; pub(super) const ALPHA: u64 = 0x1000; pub(super) const UNNAMED: u64 = 0x1400; pub(super) const BETA: u64 = 0x1800; +/// Where `__text` ends and the `__auth_stubs` section, the rest of the +/// last page, starts. +pub(super) const STUBS: u64 = 0x2c00; /// The dyld cache's image path and its unslid base. pub(super) const CACHE_PATH: &str = "/usr/lib/system/libsynthetic.dylib"; pub(super) const CACHE_BASE: u64 = 0x1_8000_0000; @@ -38,23 +41,23 @@ fn name16(out: &mut Vec, name: &[u8]) { out.extend_from_slice(&field); } -/// An `LC_SEGMENT_64` with at most one section, `(name, address, size, -/// file offset, flags)`. +/// An `LC_SEGMENT_64` with `sections`, each `(name, address, size, file +/// offset, flags)`. fn segment( name: &[u8], address: u64, size: u64, offset: u64, protection: u32, - section: Option<(&[u8], u64, u64, u32, u32)>, + sections: &[(&[u8], u64, u64, u32, u32)], ) -> Vec { - let sections = u32::from(section.is_some()); + let count = sections.len() as u32; let mut out = Vec::new(); - u32s(&mut out, &[0x19, 72 + 80 * sections]); + u32s(&mut out, &[0x19, 72 + 80 * count]); name16(&mut out, name); u64s(&mut out, &[address, size, offset, size]); - u32s(&mut out, &[protection, protection, sections, 0]); - if let Some((section, address, size, offset, flags)) = section { + u32s(&mut out, &[protection, protection, count, 0]); + for &(section, address, size, offset, flags) in sections { name16(&mut out, section); name16(&mut out, name); u64s(&mut out, &[address, size]); @@ -83,13 +86,23 @@ fn image( TEXT as u64, text_offset.into(), 5, - Some(( - b"__text", - base + ALPHA, - (TEXT - PAGE) as u64, - text_offset + ALPHA as u32, - 0x8000_0400, - )), + &[ + ( + b"__text", + base + ALPHA, + STUBS - ALPHA, + text_offset + ALPHA as u32, + 0x8000_0400, + ), + // S_SYMBOL_STUBS, pure instructions. + ( + b"__auth_stubs", + base + STUBS, + TEXT as u64 - STUBS, + text_offset + STUBS as u32, + 0x8000_0408, + ), + ], )); commands.extend(segment( b"__LINKEDIT", @@ -97,7 +110,7 @@ fn image( LINKEDIT as u64, linkedit_offset.into(), 1, - None, + &[], )); // LC_SYMTAB: two symbols at 0, their strings at 0x200. u32s(