macho-core 0.5.4

Byte-safe structural Mach-O parsing and validation
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
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout};

use crate::error::{Error, Result};

mod objc;

pub use objc::*;

/// Read a zerocopy-compatible struct from `data` at `offset`.
/// Returns a copy (not a reference) to handle unaligned data.
pub fn read_pod<T>(data: &[u8], offset: usize) -> Result<T>
where
    T: FromBytes + KnownLayout + Immutable + Copy,
{
    let size = size_of::<T>();
    let end = offset
        .checked_add(size)
        .ok_or_else(|| Error::bounds(offset as u64, size as u64, data.len() as u64))?;
    if end > data.len() {
        return Err(Error::bounds(offset as u64, size as u64, data.len() as u64));
    }
    T::read_from_bytes(&data[offset..end]).map_err(|e| {
        Error::format(format!(
            "failed to read {} at offset {offset:#x}: {e}",
            std::any::type_name::<T>()
        ))
    })
}

// Raw on-disk structures. All fields are in file byte order and must be
// interpreted through an `Endian` context. Every struct uses #[repr(C)]
// for predictable layout and derives zerocopy traits for safe parsing.

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawMachHeader32 type.
pub struct RawMachHeader32 {
    /// The magic field.
    pub magic: u32,
    /// The cputype field.
    pub cputype: i32,
    /// The cpusubtype field.
    pub cpusubtype: i32,
    /// The filetype field.
    pub filetype: u32,
    /// The ncmds field.
    pub ncmds: u32,
    /// The sizeofcmds field.
    pub sizeofcmds: u32,
    /// The flags field.
    pub flags: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawMachHeader64 type.
pub struct RawMachHeader64 {
    /// The magic field.
    pub magic: u32,
    /// The cputype field.
    pub cputype: i32,
    /// The cpusubtype field.
    pub cpusubtype: i32,
    /// The filetype field.
    pub filetype: u32,
    /// The ncmds field.
    pub ncmds: u32,
    /// The sizeofcmds field.
    pub sizeofcmds: u32,
    /// The flags field.
    pub flags: u32,
    /// The reserved field.
    pub reserved: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawFatHeader type.
pub struct RawFatHeader {
    /// The magic field.
    pub magic: u32,
    /// The nfat_arch field.
    pub nfat_arch: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawFatArch32 type.
pub struct RawFatArch32 {
    /// The cputype field.
    pub cputype: i32,
    /// The cpusubtype field.
    pub cpusubtype: i32,
    /// The offset field.
    pub offset: u32,
    /// The size field.
    pub size: u32,
    /// The align field.
    pub align: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawFatArch64 type.
pub struct RawFatArch64 {
    /// The cputype field.
    pub cputype: i32,
    /// The cpusubtype field.
    pub cpusubtype: i32,
    /// The offset field.
    pub offset: u64,
    /// The size field.
    pub size: u64,
    /// The align field.
    pub align: u32,
    /// The reserved field.
    pub reserved: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawLoadCommand type.
pub struct RawLoadCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawSegmentCommand32 type.
pub struct RawSegmentCommand32 {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The segname field.
    pub segname: [u8; 16],
    /// The vmaddr field.
    pub vmaddr: u32,
    /// The vmsize field.
    pub vmsize: u32,
    /// The fileoff field.
    pub fileoff: u32,
    /// The filesize field.
    pub filesize: u32,
    /// The maxprot field.
    pub maxprot: i32,
    /// The initprot field.
    pub initprot: i32,
    /// The nsects field.
    pub nsects: u32,
    /// The flags field.
    pub flags: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawSegmentCommand64 type.
pub struct RawSegmentCommand64 {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The segname field.
    pub segname: [u8; 16],
    /// The vmaddr field.
    pub vmaddr: u64,
    /// The vmsize field.
    pub vmsize: u64,
    /// The fileoff field.
    pub fileoff: u64,
    /// The filesize field.
    pub filesize: u64,
    /// The maxprot field.
    pub maxprot: i32,
    /// The initprot field.
    pub initprot: i32,
    /// The nsects field.
    pub nsects: u32,
    /// The flags field.
    pub flags: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawSection32 type.
pub struct RawSection32 {
    /// The sectname field.
    pub sectname: [u8; 16],
    /// The segname field.
    pub segname: [u8; 16],
    /// The addr field.
    pub addr: u32,
    /// The size field.
    pub size: u32,
    /// The offset field.
    pub offset: u32,
    /// The align field.
    pub align: u32,
    /// The reloff field.
    pub reloff: u32,
    /// The nreloc field.
    pub nreloc: u32,
    /// The flags field.
    pub flags: u32,
    /// The reserved1 field.
    pub reserved1: u32,
    /// The reserved2 field.
    pub reserved2: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawSection64 type.
pub struct RawSection64 {
    /// The sectname field.
    pub sectname: [u8; 16],
    /// The segname field.
    pub segname: [u8; 16],
    /// The addr field.
    pub addr: u64,
    /// The size field.
    pub size: u64,
    /// The offset field.
    pub offset: u32,
    /// The align field.
    pub align: u32,
    /// The reloff field.
    pub reloff: u32,
    /// The nreloc field.
    pub nreloc: u32,
    /// The flags field.
    pub flags: u32,
    /// The reserved1 field.
    pub reserved1: u32,
    /// The reserved2 field.
    pub reserved2: u32,
    /// The reserved3 field.
    pub reserved3: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawSymtabCommand type.
pub struct RawSymtabCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The symoff field.
    pub symoff: u32,
    /// The nsyms field.
    pub nsyms: u32,
    /// The stroff field.
    pub stroff: u32,
    /// The strsize field.
    pub strsize: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawDysymtabCommand type.
pub struct RawDysymtabCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The ilocalsym field.
    pub ilocalsym: u32,
    /// The nlocalsym field.
    pub nlocalsym: u32,
    /// The iextdefsym field.
    pub iextdefsym: u32,
    /// The nextdefsym field.
    pub nextdefsym: u32,
    /// The iundefsym field.
    pub iundefsym: u32,
    /// The nundefsym field.
    pub nundefsym: u32,
    /// The tocoff field.
    pub tocoff: u32,
    /// The ntoc field.
    pub ntoc: u32,
    /// The modtaboff field.
    pub modtaboff: u32,
    /// The nmodtab field.
    pub nmodtab: u32,
    /// The extrefsymoff field.
    pub extrefsymoff: u32,
    /// The nextrefsyms field.
    pub nextrefsyms: u32,
    /// The indirectsymoff field.
    pub indirectsymoff: u32,
    /// The nindirectsyms field.
    pub nindirectsyms: u32,
    /// The extreloff field.
    pub extreloff: u32,
    /// The nextrel field.
    pub nextrel: u32,
    /// The locreloff field.
    pub locreloff: u32,
    /// The nlocrel field.
    pub nlocrel: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawDylibCommand type.
pub struct RawDylibCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The name_offset field.
    pub name_offset: u32,
    /// The timestamp field.
    pub timestamp: u32,
    /// The current_version field.
    pub current_version: u32,
    /// The compatibility_version field.
    pub compatibility_version: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawUuidCommand type.
pub struct RawUuidCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The uuid field.
    pub uuid: [u8; 16],
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawBuildVersionCommand type.
pub struct RawBuildVersionCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The platform field.
    pub platform: u32,
    /// The minos field.
    pub minos: u32,
    /// The sdk field.
    pub sdk: u32,
    /// The ntools field.
    pub ntools: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawBuildToolVersion type.
pub struct RawBuildToolVersion {
    /// The tool field.
    pub tool: u32,
    /// The version field.
    pub version: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawEntryPointCommand type.
pub struct RawEntryPointCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The entryoff field.
    pub entryoff: u64,
    /// The stacksize field.
    pub stacksize: u64,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawSourceVersionCommand type.
pub struct RawSourceVersionCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The version field.
    pub version: u64,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawLinkeditDataCommand type.
pub struct RawLinkeditDataCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The dataoff field.
    pub dataoff: u32,
    /// The datasize field.
    pub datasize: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawDyldInfoCommand type.
pub struct RawDyldInfoCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The rebase_off field.
    pub rebase_off: u32,
    /// The rebase_size field.
    pub rebase_size: u32,
    /// The bind_off field.
    pub bind_off: u32,
    /// The bind_size field.
    pub bind_size: u32,
    /// The weak_bind_off field.
    pub weak_bind_off: u32,
    /// The weak_bind_size field.
    pub weak_bind_size: u32,
    /// The lazy_bind_off field.
    pub lazy_bind_off: u32,
    /// The lazy_bind_size field.
    pub lazy_bind_size: u32,
    /// The export_off field.
    pub export_off: u32,
    /// The export_size field.
    pub export_size: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawEncryptionInfoCommand type.
pub struct RawEncryptionInfoCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The cryptoff field.
    pub cryptoff: u32,
    /// The cryptsize field.
    pub cryptsize: u32,
    /// The cryptid field.
    pub cryptid: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawEncryptionInfoCommand64 type.
pub struct RawEncryptionInfoCommand64 {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The cryptoff field.
    pub cryptoff: u32,
    /// The cryptsize field.
    pub cryptsize: u32,
    /// The cryptid field.
    pub cryptid: u32,
    /// The pad field.
    pub pad: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawVersionMinCommand type.
pub struct RawVersionMinCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The version field.
    pub version: u32,
    /// The sdk field.
    pub sdk: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawNoteCommand type.
pub struct RawNoteCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The data_owner field.
    pub data_owner: [u8; 16],
    /// The offset field.
    pub offset: u64,
    /// The size field.
    pub size: u64,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawFilesetEntryCommand type.
pub struct RawFilesetEntryCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The vmaddr field.
    pub vmaddr: u64,
    /// The fileoff field.
    pub fileoff: u64,
    /// The entry_id_offset field.
    pub entry_id_offset: u32,
    /// The reserved field.
    pub reserved: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawStringCommand type.
pub struct RawStringCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The string_offset field.
    pub string_offset: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawLinkerOptionCommand type.
pub struct RawLinkerOptionCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The count field.
    pub count: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawPrebindCksumCommand type.
pub struct RawPrebindCksumCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The cksum field.
    pub cksum: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawTwolevelHintsCommand type.
pub struct RawTwolevelHintsCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The offset field.
    pub offset: u32,
    /// The nhints field.
    pub nhints: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawRoutinesCommand type.
pub struct RawRoutinesCommand {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The init_address field.
    pub init_address: u32,
    /// The init_module field.
    pub init_module: u32,
    /// The reserved1 field.
    pub reserved1: u32,
    /// The reserved2 field.
    pub reserved2: u32,
    /// The reserved3 field.
    pub reserved3: u32,
    /// The reserved4 field.
    pub reserved4: u32,
    /// The reserved5 field.
    pub reserved5: u32,
    /// The reserved6 field.
    pub reserved6: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawRoutinesCommand64 type.
pub struct RawRoutinesCommand64 {
    /// The cmd field.
    pub cmd: u32,
    /// The cmdsize field.
    pub cmdsize: u32,
    /// The init_address field.
    pub init_address: u64,
    /// The init_module field.
    pub init_module: u64,
    /// The reserved1 field.
    pub reserved1: u64,
    /// The reserved2 field.
    pub reserved2: u64,
    /// The reserved3 field.
    pub reserved3: u64,
    /// The reserved4 field.
    pub reserved4: u64,
    /// The reserved5 field.
    pub reserved5: u64,
    /// The reserved6 field.
    pub reserved6: u64,
}

// Symbol table entries (nlist)

#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawNlist32 type.
pub struct RawNlist32 {
    /// The n_strx field.
    pub n_strx: u32,
    /// The n_type field.
    pub n_type: u8,
    /// The n_sect field.
    pub n_sect: u8,
    /// The n_desc field.
    pub n_desc: i16,
    /// The n_value field.
    pub n_value: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawNlist64 type.
pub struct RawNlist64 {
    /// The n_strx field.
    pub n_strx: u32,
    /// The n_type field.
    pub n_type: u8,
    /// The n_sect field.
    pub n_sect: u8,
    /// The n_desc field.
    pub n_desc: u16,
    /// The n_value field.
    pub n_value: u64,
}

// Relocation entries

#[derive(Debug, Clone, Copy, FromBytes, IntoBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawRelocationInfo type.
pub struct RawRelocationInfo {
    /// The r_address field.
    pub r_address: i32,
    /// The r_symbolnum_and_flags field.
    pub r_symbolnum_and_flags: u32,
}

// Chained fixup structures

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawChainedFixupsHeader type.
pub struct RawChainedFixupsHeader {
    /// The fixups_version field.
    pub fixups_version: u32,
    /// The starts_offset field.
    pub starts_offset: u32,
    /// The imports_offset field.
    pub imports_offset: u32,
    /// The symbols_offset field.
    pub symbols_offset: u32,
    /// The imports_count field.
    pub imports_count: u32,
    /// The imports_format field.
    pub imports_format: u32,
    /// The symbols_format field.
    pub symbols_format: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawChainedImport type.
pub struct RawChainedImport {
    /// The packed field.
    pub packed: u32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawChainedImportAddend type.
pub struct RawChainedImportAddend {
    /// The packed field.
    pub packed: u32,
    /// The addend field.
    pub addend: i32,
}

#[derive(Debug, Clone, Copy, FromBytes, Immutable, KnownLayout)]
#[repr(C)]
/// The RawChainedImportAddend64 type.
pub struct RawChainedImportAddend64 {
    /// The packed field.
    pub packed: u64,
    /// The addend field.
    pub addend: u64,
}

/// Write a zerocopy-compatible struct into `buf` at `offset`.
pub fn write_pod<T>(buf: &mut [u8], offset: usize, value: &T) -> Result<()>
where
    T: IntoBytes + Immutable + KnownLayout,
{
    let size = size_of::<T>();
    let end = offset
        .checked_add(size)
        .ok_or_else(|| Error::bounds(offset as u64, size as u64, buf.len() as u64))?;
    if end > buf.len() {
        return Err(Error::bounds(offset as u64, size as u64, buf.len() as u64));
    }
    buf[offset..end].copy_from_slice(value.as_bytes());
    Ok(())
}

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

    #[test]
    fn read_pod_valid() {
        let data = 0xDEADBEEFu32.to_le_bytes();
        let val: u32 = read_pod(&data, 0).unwrap();
        assert_eq!(val, 0xDEADBEEF);
    }

    #[test]
    fn read_pod_truncated() {
        let data = [0u8; 2];
        let result: Result<u32> = read_pod(&data, 0);
        assert!(result.is_err());
    }

    #[test]
    fn read_pod_offset_overflow() {
        let data = [0u8; 8];
        let result: Result<u32> = read_pod(&data, usize::MAX);
        assert!(result.is_err());
    }

    #[test]
    fn raw_header_size() {
        assert_eq!(size_of::<RawMachHeader32>(), 28);
        assert_eq!(size_of::<RawMachHeader64>(), 32);
    }

    #[test]
    fn raw_segment_size() {
        assert_eq!(size_of::<RawSegmentCommand32>(), 56);
        assert_eq!(size_of::<RawSegmentCommand64>(), 72);
    }

    #[test]
    fn raw_section_size() {
        assert_eq!(size_of::<RawSection32>(), 68);
        assert_eq!(size_of::<RawSection64>(), 80);
    }

    #[test]
    fn raw_nlist_size() {
        assert_eq!(size_of::<RawNlist32>(), 12);
        assert_eq!(size_of::<RawNlist64>(), 16);
    }

    #[test]
    fn raw_relocation_size() {
        assert_eq!(size_of::<RawRelocationInfo>(), 8);
    }

    #[test]
    fn raw_chained_fixup_sizes() {
        assert_eq!(size_of::<RawChainedFixupsHeader>(), 28);
        assert_eq!(size_of::<RawChainedImport>(), 4);
        assert_eq!(size_of::<RawChainedImportAddend>(), 8);
        assert_eq!(size_of::<RawChainedImportAddend64>(), 16);
    }

    #[test]
    fn write_pod_round_trip() {
        let mut buf = [0u8; 16];
        let val: u32 = 0xCAFEBABE;
        write_pod(&mut buf, 4, &val).unwrap();
        let read_back: u32 = read_pod(&buf, 4).unwrap();
        assert_eq!(read_back, val);
    }

    #[test]
    fn write_pod_bounds() {
        let mut buf = [0u8; 4];
        let val: u64 = 0;
        assert!(write_pod(&mut buf, 0, &val).is_err());
    }
}