dylex 2.3.2

A high-performance dyld shared cache extractor for macOS and iOS
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
//! Mach-O context for reading and modifying Mach-O files.

use std::collections::HashMap;

use zerocopy::{FromBytes, IntoBytes};

use super::constants::*;
use super::structs::*;
use crate::error::{Error, Result};

// =============================================================================
// Segment Info
// =============================================================================

/// Parsed segment information.
#[derive(Debug, Clone)]
pub struct SegmentInfo {
    /// Segment fields widened to 64-bit integers for either on-disk ABI.
    /// Serialize with MachOContext::write_segment to preserve the input ABI.
    pub command: SegmentCommand64,
    /// Offset of the segment command in the file
    pub command_offset: usize,
    /// Sections in this segment
    pub sections: Vec<SectionInfo>,
}

impl SegmentInfo {
    /// Returns the segment name.
    pub fn name(&self) -> &str {
        self.command.name()
    }

    /// Returns a section by name.
    pub fn section(&self, name: &str) -> Option<&SectionInfo> {
        self.sections.iter().find(|s| s.section.name() == name)
    }

    /// Returns a mutable reference to a section by name.
    pub fn section_mut(&mut self, name: &str) -> Option<&mut SectionInfo> {
        self.sections.iter_mut().find(|s| s.section.name() == name)
    }
}

/// Parsed section information.
#[derive(Debug, Clone)]
pub struct SectionInfo {
    /// Section fields widened to 64-bit integers for either on-disk ABI.
    /// Serialize with MachOContext::write_section to preserve the input ABI.
    pub section: Section64,
    /// Offset of the section structure in the file
    pub struct_offset: usize,
}

impl SectionInfo {
    /// Returns the section name.
    pub fn name(&self) -> &str {
        self.section.name()
    }

    /// Returns the full name (segment,section).
    pub fn full_name(&self) -> String {
        format!("{},{}", self.section.segment_name(), self.section.name())
    }
}

// =============================================================================
// Load Command Info
// =============================================================================

/// Parsed load command information.
///
/// Represents the various types of load commands found in a Mach-O file.
/// The variant names correspond to the load command types.
#[derive(Debug, Clone)]
#[allow(missing_docs)] // Variants are self-documenting via names
pub enum LoadCommandInfo {
    Segment(SegmentInfo),
    Symtab {
        command: SymtabCommand,
        offset: usize,
    },
    Dysymtab {
        command: DysymtabCommand,
        offset: usize,
    },
    DyldInfo {
        command: DyldInfoCommand,
        offset: usize,
    },
    LinkeditData {
        command: LinkeditDataCommand,
        offset: usize,
    },
    Dylib {
        command: DylibCommand,
        name: String,
        offset: usize,
    },
    Uuid {
        command: UuidCommand,
        offset: usize,
    },
    BuildVersion {
        command: BuildVersionCommand,
        offset: usize,
    },
    FilesetEntry {
        command: FilesetEntryCommand,
        entry_id: String,
        offset: usize,
    },
    Unknown {
        cmd: u32,
        cmdsize: u32,
        offset: usize,
    },
}

impl LoadCommandInfo {
    /// Returns the load command offset.
    pub fn offset(&self) -> usize {
        match self {
            LoadCommandInfo::Segment(s) => s.command_offset,
            LoadCommandInfo::Symtab { offset, .. } => *offset,
            LoadCommandInfo::Dysymtab { offset, .. } => *offset,
            LoadCommandInfo::DyldInfo { offset, .. } => *offset,
            LoadCommandInfo::LinkeditData { offset, .. } => *offset,
            LoadCommandInfo::Dylib { offset, .. } => *offset,
            LoadCommandInfo::Uuid { offset, .. } => *offset,
            LoadCommandInfo::BuildVersion { offset, .. } => *offset,
            LoadCommandInfo::FilesetEntry { offset, .. } => *offset,
            LoadCommandInfo::Unknown { offset, .. } => *offset,
        }
    }

    /// Returns the load command size.
    pub fn size(&self) -> u32 {
        match self {
            LoadCommandInfo::Segment(s) => s.command.cmdsize,
            LoadCommandInfo::Symtab { command, .. } => command.cmdsize,
            LoadCommandInfo::Dysymtab { command, .. } => command.cmdsize,
            LoadCommandInfo::DyldInfo { command, .. } => command.cmdsize,
            LoadCommandInfo::LinkeditData { command, .. } => command.cmdsize,
            LoadCommandInfo::Dylib { command, .. } => command.cmdsize,
            LoadCommandInfo::Uuid { command, .. } => command.cmdsize,
            LoadCommandInfo::BuildVersion { command, .. } => command.cmdsize,
            LoadCommandInfo::FilesetEntry { command, .. } => command.cmdsize,
            LoadCommandInfo::Unknown { cmdsize, .. } => *cmdsize,
        }
    }
}

// =============================================================================
// Mach-O Context
// =============================================================================

/// Pre-computed segment range for fast address lookups.
#[derive(Debug, Clone, Copy)]
struct SegmentRange {
    vmaddr: u64,
    vmsize: u64,
    fileoff: u64,
}

/// Context for working with a Mach-O file.
///
/// This provides a high-level interface for reading and modifying Mach-O files,
/// including segments, sections, load commands, and symbols.
#[derive(Debug)]
pub struct MachOContext {
    /// Widened header fields. For MH_MAGIC, `reserved` is synthetic zero;
    /// use `header_size()` and `sync_header()` for on-disk serialization.
    pub header: MachHeader64,
    /// Offset of this Mach-O within the containing file/cache
    pub base_offset: usize,
    /// Mutable copy of the Mach-O data
    pub data: Vec<u8>,
    /// Parsed load commands
    pub load_commands: Vec<LoadCommandInfo>,
    /// Segment lookup by name
    segment_indices: HashMap<String, usize>,
    /// Pre-computed segment ranges for fast address lookups (sorted by vmaddr)
    segment_ranges: Vec<SegmentRange>,
}

impl MachOContext {
    // Public parsed structures use widened integers, while raw bytes retain the
    // original ABI. Always serialize through these helpers for width-dependent data.
    fn read_header(data: &[u8]) -> Result<MachHeader64> {
        let magic = data
            .get(..4)
            .map(crate::util::read_u32_le)
            .ok_or(Error::BufferTooSmall {
                needed: 4,
                available: data.len(),
            })?;
        let size = match magic {
            MH_MAGIC => 28,
            MH_MAGIC_64 => 32,
            _ => return Err(Error::InvalidMachoMagic(magic)),
        };
        if data.len() < size {
            return Err(Error::BufferTooSmall {
                needed: size,
                available: data.len(),
            });
        }
        let mut bytes = [0u8; 32];
        bytes[..size].copy_from_slice(&data[..size]);
        Ok(MachHeader64::read_from_prefix(&bytes).unwrap().0)
    }

    /// Whether the on-disk image uses the 64-bit Mach-O ABI.
    pub fn is_64(&self) -> bool {
        self.header.magic == MH_MAGIC_64
    }
    /// On-disk header size in bytes.
    pub fn header_size(&self) -> usize {
        if self.is_64() { 32 } else { 28 }
    }
    /// On-disk pointer width in bytes.
    pub fn pointer_size(&self) -> usize {
        if self.is_64() { 8 } else { 4 }
    }
    /// On-disk segment command size in bytes, excluding sections.
    pub fn segment_command_size(&self) -> usize {
        if self.is_64() { 72 } else { 56 }
    }
    /// On-disk section record size in bytes.
    pub fn section_size(&self) -> usize {
        if self.is_64() { 80 } else { 68 }
    }
    /// On-disk symbol record size in bytes.
    pub fn nlist_size(&self) -> usize {
        if self.is_64() { 16 } else { 12 }
    }
    /// Reads a pointer without consuming an adjacent 32-bit slot.
    pub fn read_pointer(&self, offset: usize) -> Result<u64> {
        if self.is_64() {
            self.read_u64(offset)
        } else {
            Ok(self.read_u32(offset)? as u64)
        }
    }
    /// Writes an ABI-sized pointer, rejecting truncation.
    pub fn write_pointer(&mut self, offset: usize, value: u64) -> Result<()> {
        if self.is_64() {
            self.write_u64(offset, value)
        } else {
            let value = u32::try_from(value).map_err(|_| Error::Parse {
                offset,
                reason: "32-bit pointer overflow".into(),
            })?;
            self.write_u32(offset, value)
        }
    }
    /// Serializes a widened segment using this image's on-disk ABI.
    pub fn write_segment(&mut self, offset: usize, segment: &SegmentCommand64) -> Result<()> {
        self.write_at(offset, &self.segment_bytes(segment)?)
    }
    /// Encodes a widened segment without mutating the image.
    pub fn segment_bytes(&self, segment: &SegmentCommand64) -> Result<Vec<u8>> {
        let offset = 0;
        if self.is_64() {
            return Ok(segment.as_bytes().to_vec());
        }
        let mut bytes = Vec::with_capacity(56);
        bytes.extend_from_slice(&LC_SEGMENT.to_le_bytes());
        bytes.extend_from_slice(&segment.cmdsize.to_le_bytes());
        bytes.extend_from_slice(&segment.segname);
        for value in [
            segment.vmaddr,
            segment.vmsize,
            segment.fileoff,
            segment.filesize,
        ] {
            let value = u32::try_from(value).map_err(|_| Error::Parse {
                offset,
                reason: "32-bit segment field overflow".into(),
            })?;
            bytes.extend_from_slice(&value.to_le_bytes());
        }
        for value in [
            segment.maxprot,
            segment.initprot,
            segment.nsects,
            segment.flags,
        ] {
            bytes.extend_from_slice(&value.to_le_bytes());
        }
        Ok(bytes)
    }
    /// Serializes a widened section using this image's on-disk ABI.
    pub fn write_section(&mut self, offset: usize, section: &Section64) -> Result<()> {
        self.write_at(offset, &self.section_bytes(section)?)
    }
    /// Encodes a widened section without mutating the image.
    pub fn section_bytes(&self, section: &Section64) -> Result<Vec<u8>> {
        let offset = 0;
        if self.is_64() {
            return Ok(section.as_bytes().to_vec());
        }
        let mut bytes = Vec::with_capacity(68);
        bytes.extend_from_slice(&section.sectname);
        bytes.extend_from_slice(&section.segname);
        for value in [section.addr, section.size] {
            let value = u32::try_from(value).map_err(|_| Error::Parse {
                offset,
                reason: "32-bit section field overflow".into(),
            })?;
            bytes.extend_from_slice(&value.to_le_bytes());
        }
        for value in [
            section.offset,
            section.align,
            section.reloff,
            section.nreloc,
            section.flags,
            section.reserved1,
            section.reserved2,
        ] {
            bytes.extend_from_slice(&value.to_le_bytes());
        }
        Ok(bytes)
    }

    /// Creates a new MachO context from raw data.
    ///
    /// # Arguments
    /// * `data` - The raw Mach-O data (will be copied)
    /// * `base_offset` - Offset of this Mach-O within a larger file (for address conversion)
    pub fn new(data: &[u8], base_offset: usize) -> Result<Self> {
        let header = Self::read_header(data)?;

        let mut ctx = Self {
            header: header.clone(),
            base_offset,
            data: data.to_vec(),
            load_commands: Vec::with_capacity(32), // Pre-allocate for typical dylib
            segment_indices: HashMap::new(),
            segment_ranges: Vec::with_capacity(8), // Pre-allocate for typical segments
        };

        ctx.parse_load_commands()?;
        ctx.build_segment_ranges();

        Ok(ctx)
    }

    /// Builds the segment_ranges cache for fast address lookups.
    fn build_segment_ranges(&mut self) {
        self.segment_ranges.clear();
        // Collect segment data without borrowing self
        for lc in &self.load_commands {
            if let LoadCommandInfo::Segment(seg) = lc {
                if seg.command.vmsize > 0 {
                    self.segment_ranges.push(SegmentRange {
                        vmaddr: seg.command.vmaddr,
                        vmsize: seg.command.filesize,
                        fileoff: seg.command.fileoff,
                    });
                }
            }
        }
        // Sort by vmaddr for binary search
        self.segment_ranges.sort_by_key(|r| r.vmaddr);
    }

    /// Rebuilds parsed commands and address indexes after changing raw commands.
    pub fn refresh(&mut self) -> Result<()> {
        self.header = Self::read_header(&self.data)?;
        self.load_commands.clear();
        self.segment_indices.clear();
        self.parse_load_commands()?;
        self.build_segment_ranges();
        Ok(())
    }

    /// Creates a context from a slice within a dyld cache.
    pub fn from_cache_slice(cache_data: &[u8], offset: usize, size: usize) -> Result<Self> {
        if offset + size > cache_data.len() {
            return Err(Error::BufferTooSmall {
                needed: offset + size,
                available: cache_data.len(),
            });
        }
        Self::new(&cache_data[offset..offset + size], offset)
    }

    /// Parses all load commands.
    fn parse_load_commands(&mut self) -> Result<()> {
        let mut offset = self.header_size();
        let end_offset = self.header_size() + self.header.sizeofcmds as usize;

        for _ in 0..self.header.ncmds {
            if offset + LoadCommand::SIZE > end_offset
                || offset + LoadCommand::SIZE > self.data.len()
            {
                return Err(Error::LoadCommandOverflow { offset });
            }

            let lc = LoadCommand::read_from_prefix(&self.data[offset..])
                .map_err(|_| Error::Parse {
                    offset,
                    reason: "failed to parse load command".into(),
                })?
                .0;

            if lc.cmdsize < 8
                || offset + lc.cmdsize as usize > end_offset
                || offset + lc.cmdsize as usize > self.data.len()
            {
                return Err(Error::LoadCommandOverflow { offset });
            }

            let cmd_data = &self.data[offset..offset + lc.cmdsize as usize];
            let cmd_info = self.parse_load_command(lc.cmd, cmd_data, offset)?;

            // Track segment indices
            if let LoadCommandInfo::Segment(ref seg) = cmd_info {
                self.segment_indices
                    .insert(seg.name().to_string(), self.load_commands.len());
            }

            self.load_commands.push(cmd_info);
            offset += lc.cmdsize as usize;
        }

        Ok(())
    }

    /// Parses a single load command.
    fn parse_load_command(&self, cmd: u32, data: &[u8], offset: usize) -> Result<LoadCommandInfo> {
        match cmd {
            LC_SEGMENT | LC_SEGMENT_64 => {
                let is_64 = cmd == LC_SEGMENT_64;
                if is_64 != self.is_64() {
                    return Err(Error::Parse {
                        offset,
                        reason: "segment command width differs from Mach-O header".into(),
                    });
                }
                let segment_size = self.segment_command_size();
                let section_size = self.section_size();
                if data.len() < segment_size {
                    return Err(Error::LoadCommandOverflow { offset });
                }
                let seg = if is_64 {
                    SegmentCommand64::read_from_prefix(data).unwrap().0
                } else {
                    let u = |i| crate::util::read_u32_le(&data[i..]);
                    SegmentCommand64 {
                        cmd,
                        cmdsize: u(4),
                        segname: data[8..24].try_into().unwrap(),
                        vmaddr: u(24) as u64,
                        vmsize: u(28) as u64,
                        fileoff: u(32) as u64,
                        filesize: u(36) as u64,
                        maxprot: u(40),
                        initprot: u(44),
                        nsects: u(48),
                        flags: u(52),
                    }
                };
                if (seg.nsects as usize)
                    .checked_mul(section_size)
                    .and_then(|n| segment_size.checked_add(n))
                    .is_none_or(|n| n > data.len())
                {
                    return Err(Error::LoadCommandOverflow { offset });
                }
                let mut sections = Vec::with_capacity(seg.nsects as usize);
                for i in 0..seg.nsects as usize {
                    let local = segment_size + i * section_size;
                    let raw = &data[local..local + section_size];
                    let section = if is_64 {
                        Section64::read_from_prefix(raw).unwrap().0
                    } else {
                        let u = |i| crate::util::read_u32_le(&raw[i..]);
                        Section64 {
                            sectname: raw[..16].try_into().unwrap(),
                            segname: raw[16..32].try_into().unwrap(),
                            addr: u(32) as u64,
                            size: u(36) as u64,
                            offset: u(40),
                            align: u(44),
                            reloff: u(48),
                            nreloc: u(52),
                            flags: u(56),
                            reserved1: u(60),
                            reserved2: u(64),
                            reserved3: 0,
                        }
                    };
                    sections.push(SectionInfo {
                        section,
                        struct_offset: offset + local,
                    });
                }
                Ok(LoadCommandInfo::Segment(SegmentInfo {
                    command: seg,
                    command_offset: offset,
                    sections,
                }))
            }

            LC_SYMTAB => {
                let symtab = SymtabCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse symtab command".into(),
                    })?
                    .0;

                Ok(LoadCommandInfo::Symtab {
                    command: symtab.clone(),
                    offset,
                })
            }

            LC_DYSYMTAB => {
                let dysymtab = DysymtabCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse dysymtab command".into(),
                    })?
                    .0;

                Ok(LoadCommandInfo::Dysymtab {
                    command: dysymtab.clone(),
                    offset,
                })
            }

            LC_DYLD_INFO | LC_DYLD_INFO_ONLY => {
                let dyld_info = DyldInfoCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse dyld info command".into(),
                    })?
                    .0;

                Ok(LoadCommandInfo::DyldInfo {
                    command: dyld_info.clone(),
                    offset,
                })
            }

            LC_CODE_SIGNATURE
            | LC_SEGMENT_SPLIT_INFO
            | LC_FUNCTION_STARTS
            | LC_DATA_IN_CODE
            | LC_DYLD_EXPORTS_TRIE
            | LC_DYLD_CHAINED_FIXUPS
            | LC_LINKER_OPTIMIZATION_HINT
            | LC_ATOM_INFO => {
                let linkedit = LinkeditDataCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse linkedit data command".into(),
                    })?
                    .0;

                Ok(LoadCommandInfo::LinkeditData {
                    command: linkedit.clone(),
                    offset,
                })
            }

            LC_LOAD_DYLIB | LC_LOAD_WEAK_DYLIB | LC_REEXPORT_DYLIB | LC_LAZY_LOAD_DYLIB
            | LC_LOAD_UPWARD_DYLIB | LC_ID_DYLIB => {
                let dylib = DylibCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse dylib command".into(),
                    })?
                    .0;

                // Read the dylib name
                let name_offset = dylib.dylib.name_offset as usize;
                let name = if name_offset < data.len() {
                    let name_bytes = &data[name_offset..];
                    let end = name_bytes
                        .iter()
                        .position(|&b| b == 0)
                        .unwrap_or(name_bytes.len());
                    String::from_utf8_lossy(&name_bytes[..end]).to_string()
                } else {
                    String::new()
                };

                Ok(LoadCommandInfo::Dylib {
                    command: dylib.clone(),
                    name,
                    offset,
                })
            }

            LC_UUID => {
                let uuid = UuidCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse uuid command".into(),
                    })?
                    .0;

                Ok(LoadCommandInfo::Uuid {
                    command: uuid.clone(),
                    offset,
                })
            }

            LC_BUILD_VERSION => {
                let build_version = BuildVersionCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse build version command".into(),
                    })?
                    .0;

                Ok(LoadCommandInfo::BuildVersion {
                    command: build_version.clone(),
                    offset,
                })
            }

            LC_FILESET_ENTRY => {
                let entry = FilesetEntryCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse fileset entry command".into(),
                    })?
                    .0;

                // Read the entry ID
                let id_offset = entry.entry_id_offset as usize;
                let entry_id = if id_offset < data.len() {
                    let name_bytes = &data[id_offset..];
                    let end = name_bytes
                        .iter()
                        .position(|&b| b == 0)
                        .unwrap_or(name_bytes.len());
                    String::from_utf8_lossy(&name_bytes[..end]).to_string()
                } else {
                    String::new()
                };

                Ok(LoadCommandInfo::FilesetEntry {
                    command: entry.clone(),
                    entry_id,
                    offset,
                })
            }

            _ => {
                let lc = LoadCommand::read_from_prefix(data)
                    .map_err(|_| Error::Parse {
                        offset,
                        reason: "failed to parse load command".into(),
                    })?
                    .0;

                Ok(LoadCommandInfo::Unknown {
                    cmd,
                    cmdsize: lc.cmdsize,
                    offset,
                })
            }
        }
    }

    /// Returns a reference to a segment by name.
    pub fn segment(&self, name: &str) -> Option<&SegmentInfo> {
        self.segment_indices.get(name).and_then(|&idx| {
            if let LoadCommandInfo::Segment(ref seg) = self.load_commands[idx] {
                Some(seg)
            } else {
                None
            }
        })
    }

    /// Returns a mutable reference to a segment by name.
    pub fn segment_mut(&mut self, name: &str) -> Option<&mut SegmentInfo> {
        let idx = *self.segment_indices.get(name)?;
        if let LoadCommandInfo::Segment(ref mut seg) = self.load_commands[idx] {
            Some(seg)
        } else {
            None
        }
    }

    /// Returns an iterator over all segments.
    pub fn segments(&self) -> impl Iterator<Item = &SegmentInfo> {
        self.load_commands.iter().filter_map(|lc| {
            if let LoadCommandInfo::Segment(seg) = lc {
                Some(seg)
            } else {
                None
            }
        })
    }

    /// Returns a section by segment and section name.
    pub fn section(&self, segment: &str, section: &str) -> Option<&SectionInfo> {
        self.segment(segment)?.section(section)
    }

    /// Returns the __TEXT segment.
    pub fn text_segment(&self) -> Option<&SegmentInfo> {
        self.segment("__TEXT")
    }

    /// Returns the __DATA segment.
    pub fn data_segment(&self) -> Option<&SegmentInfo> {
        self.segment("__DATA")
    }

    /// Returns the __LINKEDIT segment.
    pub fn linkedit_segment(&self) -> Option<&SegmentInfo> {
        self.segment("__LINKEDIT")
    }

    /// Returns the symbol table command.
    pub fn symtab(&self) -> Option<&SymtabCommand> {
        self.load_commands.iter().find_map(|lc| {
            if let LoadCommandInfo::Symtab { command, .. } = lc {
                Some(command)
            } else {
                None
            }
        })
    }

    /// Returns the dynamic symbol table command.
    pub fn dysymtab(&self) -> Option<&DysymtabCommand> {
        self.load_commands.iter().find_map(|lc| {
            if let LoadCommandInfo::Dysymtab { command, .. } = lc {
                Some(command)
            } else {
                None
            }
        })
    }

    /// Returns the dyld info command.
    pub fn dyld_info(&self) -> Option<&DyldInfoCommand> {
        self.load_commands.iter().find_map(|lc| {
            if let LoadCommandInfo::DyldInfo { command, .. } = lc {
                Some(command)
            } else {
                None
            }
        })
    }

    /// Returns an iterator over dependency dylibs.
    pub fn dylibs(&self) -> impl Iterator<Item = (&str, u32)> {
        self.load_commands.iter().filter_map(|lc| {
            if let LoadCommandInfo::Dylib { command, name, .. } = lc {
                Some((name.as_str(), command.cmd))
            } else {
                None
            }
        })
    }

    /// Returns a list of all dependency paths (LC_LOAD_DYLIB, LC_LOAD_WEAK_DYLIB, etc.)
    /// excluding LC_ID_DYLIB (which identifies the image itself).
    pub fn dependencies(&self) -> Vec<String> {
        self.load_commands
            .iter()
            .filter_map(|lc| {
                if let LoadCommandInfo::Dylib { command, name, .. } = lc {
                    // Include all dylib load commands except LC_ID_DYLIB
                    match command.cmd {
                        LC_LOAD_DYLIB | LC_LOAD_WEAK_DYLIB | LC_REEXPORT_DYLIB
                        | LC_LAZY_LOAD_DYLIB | LC_LOAD_UPWARD_DYLIB => Some(name.clone()),
                        _ => None,
                    }
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns true if this is an ARM64 binary.
    pub fn is_arm64(&self) -> bool {
        self.header.is_arm64()
    }

    /// Returns true if this is an ARM64e binary (with pointer authentication).
    pub fn is_arm64e(&self) -> bool {
        self.header.is_arm64e()
    }

    /// Reads data at the specified offset within the Mach-O.
    #[inline]
    pub fn read_at(&self, offset: usize, len: usize) -> Result<&[u8]> {
        if offset + len > self.data.len() {
            return Err(Error::BufferTooSmall {
                needed: offset + len,
                available: self.data.len(),
            });
        }
        Ok(&self.data[offset..offset + len])
    }

    /// Reads a u32 at the specified offset.
    #[inline(always)]
    pub fn read_u32(&self, offset: usize) -> Result<u32> {
        let bytes = self.read_at(offset, 4)?;
        Ok(u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]))
    }

    /// Reads a u64 at the specified offset.
    #[inline(always)]
    pub fn read_u64(&self, offset: usize) -> Result<u64> {
        let bytes = self.read_at(offset, 8)?;
        Ok(u64::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5], bytes[6], bytes[7],
        ]))
    }

    /// Writes data at the specified offset.
    #[inline]
    pub fn write_at(&mut self, offset: usize, data: &[u8]) -> Result<()> {
        if offset + data.len() > self.data.len() {
            return Err(Error::BufferTooSmall {
                needed: offset + data.len(),
                available: self.data.len(),
            });
        }
        self.data[offset..offset + data.len()].copy_from_slice(data);
        Ok(())
    }

    /// Writes a u32 at the specified offset.
    #[inline(always)]
    pub fn write_u32(&mut self, offset: usize, value: u32) -> Result<()> {
        self.write_at(offset, &value.to_le_bytes())
    }

    /// Writes a u64 at the specified offset.
    #[inline(always)]
    pub fn write_u64(&mut self, offset: usize, value: u64) -> Result<()> {
        self.write_at(offset, &value.to_le_bytes())
    }

    /// Writes a structure at the specified offset.
    pub fn write_struct<T: IntoBytes + Immutable>(
        &mut self,
        offset: usize,
        value: &T,
    ) -> Result<()> {
        let bytes = value.as_bytes();
        self.write_at(offset, bytes)
    }

    /// Updates the header in the data buffer.
    pub fn sync_header(&mut self) -> Result<()> {
        let header = self.header.clone();
        self.write_at(0, &header.as_bytes()[..self.header_size()])
    }

    /// Returns the total size of load commands.
    pub fn load_commands_size(&self) -> usize {
        self.load_commands.iter().map(|lc| lc.size() as usize).sum()
    }

    /// Returns the available space for load commands.
    pub fn available_load_command_space(&self) -> usize {
        let text = self
            .text_segment()
            .map(|s| s.command.fileoff as usize)
            .unwrap_or(usize::MAX);
        let used = self.header_size() + self.load_commands_size();
        text.saturating_sub(used)
    }

    /// Converts a virtual address to a file offset within this Mach-O.
    /// Hot path - called for every pointer in slide info processing.
    /// Uses binary search on pre-computed segment ranges for O(log n) lookup.
    #[inline]
    pub fn addr_to_offset(&self, addr: u64) -> Option<usize> {
        // Binary search: find the first segment where vmaddr + vmsize > addr
        let idx = self
            .segment_ranges
            .partition_point(|r| r.vmaddr + r.vmsize <= addr);
        if idx < self.segment_ranges.len() {
            let range = &self.segment_ranges[idx];
            if addr >= range.vmaddr && addr < range.vmaddr + range.vmsize {
                let offset = range.fileoff + (addr - range.vmaddr);
                return Some(offset as usize);
            }
        }
        None
    }

    /// Converts a file offset to a virtual address.
    pub fn offset_to_addr(&self, offset: usize) -> Option<u64> {
        let offset = offset as u64;
        for seg in self.segments() {
            if offset >= seg.command.fileoff && offset < seg.command.fileoff + seg.command.filesize
            {
                let addr = seg.command.vmaddr + (offset - seg.command.fileoff);
                return Some(addr);
            }
        }
        None
    }

    /// Returns true if the address is within this Mach-O.
    pub fn contains_addr(&self, addr: u64) -> bool {
        self.segments()
            .any(|seg| addr >= seg.command.vmaddr && addr < seg.command.vmaddr + seg.command.vmsize)
    }

    /// Returns the raw data.
    pub fn as_bytes(&self) -> &[u8] {
        &self.data
    }

    /// Returns the raw data as mutable.
    pub fn as_bytes_mut(&mut self) -> &mut [u8] {
        &mut self.data
    }
}

use zerocopy::Immutable;

#[cfg(test)]
mod tests {
    use super::*;

    fn create_minimal_macho() -> Vec<u8> {
        let mut data = vec![0u8; 0x1000];

        // Write header
        let header = MachHeader64 {
            magic: MH_MAGIC_64,
            cputype: CPU_TYPE_ARM64,
            cpusubtype: CPU_SUBTYPE_ARM64_ALL,
            filetype: MH_DYLIB,
            ncmds: 1,
            sizeofcmds: SegmentCommand64::SIZE as u32,
            flags: 0,
            reserved: 0,
        };

        data[..MachHeader64::SIZE].copy_from_slice(header.as_bytes());

        // Write a simple TEXT segment
        let mut seg = SegmentCommand64::default();
        seg.set_name("__TEXT");
        seg.vmaddr = 0x100000000;
        seg.vmsize = 0x1000;
        seg.fileoff = 0;
        seg.filesize = 0x1000;

        data[MachHeader64::SIZE..MachHeader64::SIZE + SegmentCommand64::SIZE]
            .copy_from_slice(seg.as_bytes());

        data
    }

    #[test]
    fn test_parse_minimal_macho() {
        let data = create_minimal_macho();
        let ctx = MachOContext::new(&data, 0).unwrap();

        assert!(ctx.header.is_valid());
        assert!(ctx.is_arm64());
        assert_eq!(ctx.header.ncmds, 1);
        assert!(ctx.segment("__TEXT").is_some());
    }
}