forked from blacktop/go-macho
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
3051 lines (2855 loc) · 88.5 KB
/
file.go
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
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package macho
// High level access to low level data structures.
import (
"bufio"
"bytes"
"compress/zlib"
"encoding/binary"
"errors"
"fmt"
"io"
"log"
"os"
"path/filepath"
"strings"
"sync"
"unicode"
"github.com/blacktop/go-dwarf"
"github.com/blacktop/go-macho/internal/saferio"
"github.com/blacktop/go-macho/pkg/codesign"
"github.com/blacktop/go-macho/pkg/fixupchains"
"github.com/blacktop/go-macho/pkg/trie"
"github.com/blacktop/go-macho/pkg/xar"
"github.com/blacktop/go-macho/types"
)
// A File represents an open Mach-O file.
type File struct {
FileTOC
Symtab *Symtab
Dysymtab *Dysymtab
vma *types.VMAddrConverter
dcf *fixupchains.DyldChainedFixups
exp []trie.TrieExport
exptrieData []byte
binds types.Binds
objc map[uint64]any
swift map[uint64]any
ledata *bytes.Buffer // tmp storage of linkedit data
sharedCacheRelativeSelectorBaseVMAddress uint64 // objc_opt version 16
mu sync.Mutex
sr types.MachoReader
cr types.MachoReader
closer io.Closer
}
/*
* Mach-O reader
*/
var ErrMachOArchNotSupported = errors.New("MachO arch not supported")
var ErrMachOSectionNotFound = errors.New("MachO missing required section")
var ErrMachODyldInfoNotFound = errors.New("LC_DYLD_INFO(_ONLY) not found")
var ErrMachONoBindInfo = errors.New("MachO does not contain bind information (fixups)")
// FormatError is returned by some operations if the data does
// not have the correct format for an object file.
type FormatError struct {
off int64
msg string
val any
}
func (e *FormatError) Error() string {
msg := e.msg
if e.val != nil {
msg += fmt.Sprintf(" '%v'", e.val)
}
msg += fmt.Sprintf(" in record at byte %#x", e.off)
return msg
}
func loadInSlice(c types.LoadCmd, list []types.LoadCmd) bool {
for _, b := range list {
if b == c {
return true
}
}
return false
}
// FileConfig is a MachO file config object
type FileConfig struct {
Offset int64
LoadIncluding []types.LoadCmd
LoadExcluding []types.LoadCmd
VMAddrConverter types.VMAddrConverter
SectionReader types.MachoReader
CacheReader types.MachoReader
RelativeSelectorBase uint64
}
// Open opens the named file using os.Open and prepares it for use as a Mach-O binary.
func Open(name string) (*File, error) {
f, err := os.Open(name)
if err != nil {
return nil, err
}
ff, err := NewFile(f)
if err != nil {
f.Close()
return nil, err
}
ff.closer = f
return ff, nil
}
// Close closes the File.
// If the File was created using NewFile directly instead of Open,
// Close has no effect.
func (f *File) Close() error {
var err error
if f.closer != nil {
err = f.closer.Close()
f.closer = nil
}
return err
}
// NewFile creates a new File for accessing a Mach-O binary in an underlying reader.
// The Mach-O binary is expected to start at position 0 in the ReaderAt.
func NewFile(r io.ReaderAt, config ...FileConfig) (*File, error) {
var loadIncluding []types.LoadCmd
var loadExcluding []types.LoadCmd
f := new(File)
f.objc = make(map[uint64]any)
f.swift = make(map[uint64]any)
f.vma = &types.VMAddrConverter{
Converter: f.convertToVMAddr,
VMAddr2Offet: f.getOffset,
Offet2VMAddr: f.getVMAddress,
}
f.sr = types.NewCustomSectionReader(r, f.vma, 0, 1<<63-1)
f.cr = f.sr
if config != nil {
if config[0].SectionReader != nil {
f.sr = config[0].SectionReader
f.sr.Seek(config[0].Offset, io.SeekStart)
f.cr = f.sr
}
if config[0].CacheReader != nil {
f.cr = config[0].CacheReader
}
if config[0].VMAddrConverter.Converter != nil {
f.vma = &config[0].VMAddrConverter
}
loadIncluding = config[0].LoadIncluding
loadExcluding = config[0].LoadExcluding
f.sharedCacheRelativeSelectorBaseVMAddress = config[0].RelativeSelectorBase
}
// Read and decode Mach magic to determine byte order, size.
// Magic32 and Magic64 differ only in the bottom bit.
var ident [4]byte
if _, err := r.ReadAt(ident[0:], 0); err != nil {
return nil, fmt.Errorf("failed to parse magic: %v", err)
}
be := binary.BigEndian.Uint32(ident[0:])
le := binary.LittleEndian.Uint32(ident[0:])
switch types.Magic32.Int() &^ 1 {
case be &^ 1:
f.ByteOrder = binary.BigEndian
f.Magic = types.Magic(be)
case le &^ 1:
f.ByteOrder = binary.LittleEndian
f.Magic = types.Magic(le)
default:
return nil, &FormatError{0, "invalid magic number", nil}
}
// Read entire file header.
if err := binary.Read(f.sr, f.ByteOrder, &f.FileHeader); err != nil {
return nil, fmt.Errorf("failed to parse header: %v", err)
}
// Then load commands.
offset := int64(types.FileHeaderSize32)
if f.Magic == types.Magic64 {
offset = types.FileHeaderSize64
}
dat, err := saferio.ReadDataAt(r, uint64(f.SizeCommands), offset)
if err != nil {
return nil, err
}
c := saferio.SliceCap[Load](uint64(f.NCommands))
if c < 0 {
return nil, &FormatError{offset, "too many load commands", nil}
}
f.Loads = make([]Load, 0, c)
bo := f.ByteOrder
for i := uint32(0); i < f.NCommands; i++ {
// Each load command begins with uint32 command and length.
if len(dat) < 8 {
return nil, &FormatError{offset, "command block too small", nil}
}
cmd, siz := types.LoadCmd(bo.Uint32(dat[0:4])), bo.Uint32(dat[4:8])
if siz < 8 || siz > uint32(len(dat)) {
return nil, &FormatError{offset, "invalid command block size", nil}
}
var cmddat []byte
cmddat, dat = dat[0:siz], dat[siz:]
offset += int64(siz)
var s *Segment
// skip unwanted load commands
if len(loadIncluding) > 0 && !loadInSlice(cmd, loadIncluding) {
continue
} else if loadInSlice(cmd, loadExcluding) {
continue
}
switch cmd {
default:
log.Printf("found NEW load command: %s (please let the author know via https://github.com/blacktop/go-macho/issues)", cmd)
f.Loads = append(f.Loads, LoadCmdBytes{types.LoadCmd(cmd), LoadBytes(cmddat)})
case types.LC_SEGMENT:
var seg32 types.Segment32
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &seg32); err != nil {
return nil, fmt.Errorf("failed to read LC_SEGMENT: %v", err)
}
s = new(Segment)
s.LoadBytes = cmddat
s.LoadCmd = cmd
s.Len = siz
s.Name = cstring(seg32.Name[0:])
s.Addr = uint64(seg32.Addr)
s.Memsz = uint64(seg32.Memsz)
s.Offset = uint64(seg32.Offset)
s.Filesz = uint64(seg32.Filesz)
s.Maxprot = seg32.Maxprot
s.Prot = seg32.Prot
s.Nsect = seg32.Nsect
s.Flag = seg32.Flag
s.Firstsect = uint32(len(f.Sections))
for i := 0; i < int(s.Nsect); i++ {
var sh32 types.Section32
if err := binary.Read(b, bo, &sh32); err != nil {
return nil, fmt.Errorf("failed to read Section32: %v", err)
}
sh := new(types.Section)
sh.Type = 32
sh.Name = cstring(sh32.Name[0:])
sh.Seg = cstring(sh32.Seg[0:])
sh.Addr = uint64(sh32.Addr)
sh.Size = uint64(sh32.Size)
sh.Offset = sh32.Offset
sh.Align = sh32.Align
sh.Reloff = sh32.Reloff
sh.Nreloc = sh32.Nreloc
sh.Flags = sh32.Flags
sh.Reserved1 = sh32.Reserve1
sh.Reserved2 = sh32.Reserve2
sh.SetReaders(f.cr, io.NewSectionReader(f.cr, int64(sh32.Offset), int64(sh32.Size)))
if err := f.pushSection(sh, f.cr); err != nil {
return nil, fmt.Errorf("failed to pushSection32: %v", err)
}
s.sections = append(s.sections, sh)
}
f.Loads = append(f.Loads, s)
case types.LC_SEGMENT_64:
var seg64 types.Segment64
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &seg64); err != nil {
return nil, fmt.Errorf("failed to read LC_SEGMENT_64: %v", err)
}
s = new(Segment)
s.LoadBytes = cmddat
s.LoadCmd = cmd
s.Len = siz
s.Name = cstring(seg64.Name[0:])
s.Addr = seg64.Addr
s.Memsz = seg64.Memsz
s.Offset = seg64.Offset
s.Filesz = seg64.Filesz
s.Maxprot = seg64.Maxprot
s.Prot = seg64.Prot
s.Nsect = seg64.Nsect
s.Flag = seg64.Flag
s.Firstsect = uint32(len(f.Sections))
for i := 0; i < int(s.Nsect); i++ {
var sh64 types.Section64
if err := binary.Read(b, bo, &sh64); err != nil {
return nil, fmt.Errorf("failed to read Section64: %v", err)
}
sh := new(types.Section)
sh.Type = 64
sh.Name = cstring(sh64.Name[0:])
sh.Seg = cstring(sh64.Seg[0:])
sh.Addr = sh64.Addr
sh.Size = sh64.Size
sh.Offset = sh64.Offset
sh.Align = sh64.Align
sh.Reloff = sh64.Reloff
sh.Nreloc = sh64.Nreloc
sh.Flags = sh64.Flags
sh.Reserved1 = sh64.Reserve1
sh.Reserved2 = sh64.Reserve2
sh.Reserved3 = sh64.Reserve3
sh.SetReaders(f.cr, io.NewSectionReader(f.cr, int64(sh64.Offset), int64(sh64.Size)))
if err := f.pushSection(sh, f.cr); err != nil {
return nil, fmt.Errorf("failed to pushSection64: %v", err)
}
s.sections = append(s.sections, sh)
}
f.Loads = append(f.Loads, s)
case types.LC_SYMTAB:
var hdr types.SymtabCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_SYMTAB: %v", err)
}
strtab, err := saferio.ReadDataAt(f.cr, uint64(hdr.Strsize), int64(hdr.Stroff))
if err != nil {
return nil, fmt.Errorf("failed to read data at Stroff=%#x; %v", int64(hdr.Stroff), err)
}
var symsz int
if f.Magic == types.Magic64 {
symsz = 16
} else {
symsz = 12
}
symdat, err := saferio.ReadDataAt(f.cr, uint64(hdr.Nsyms)*uint64(symsz), int64(hdr.Symoff))
if err != nil {
return nil, fmt.Errorf("failed to read data at Symoff=%#x; %v", int64(hdr.Symoff), err)
}
st, err := f.parseSymtab(symdat, strtab, cmddat, &hdr, offset)
if err != nil {
return nil, fmt.Errorf("failed to read parseSymtab: %v", err)
}
st.LoadBytes = cmddat
st.LoadCmd = cmd
st.Len = siz
f.Loads = append(f.Loads, st)
f.Symtab = st
case types.LC_SYMSEG:
var led types.SymsegCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &led); err != nil {
return nil, fmt.Errorf("failed to read LC_SYMSEG: %v", err)
}
l := new(SymSeg)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.Offset = led.Offset
l.Size = led.Size
f.Loads = append(f.Loads, l)
case types.LC_THREAD:
var t types.ThreadCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &t); err != nil {
return nil, fmt.Errorf("failed to read LC_THREAD: %v", err)
}
l := new(Thread)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.bo = bo
if f.isArm() || f.isArm64() || f.isArm64e() {
l.IsArm = true
}
for {
var thread types.ThreadState
err := binary.Read(b, bo, &thread.Flavor)
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to read LC_THREAD flavor: %v", err)
}
if err := binary.Read(b, bo, &thread.Count); err != nil {
return nil, fmt.Errorf("failed to read LC_THREAD count: %v", err)
}
thread.Data = make([]byte, thread.Count*uint32(binary.Size(uint32(0))))
if err := binary.Read(b, bo, &thread.Data); err != nil {
return nil, fmt.Errorf("failed to read LC_THREAD state struct data: %v", err)
}
l.Threads = append(l.Threads, thread)
}
f.Loads = append(f.Loads, l)
case types.LC_UNIXTHREAD:
var ut types.UnixThreadCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &ut); err != nil {
return nil, fmt.Errorf("failed to read LC_UNIXTHREAD: %v", err)
}
l := new(UnixThread)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.bo = bo
if f.isArm() || f.isArm64() || f.isArm64e() {
l.IsArm = true
}
for {
var thread types.ThreadState
err := binary.Read(b, bo, &thread.Flavor)
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to read LC_UNIXTHREAD flavor: %v", err)
}
if err := binary.Read(b, bo, &thread.Count); err != nil {
return nil, fmt.Errorf("failed to read LC_UNIXTHREAD count: %v", err)
}
thread.Data = make([]byte, thread.Count*uint32(binary.Size(uint32(0))))
if err := binary.Read(b, bo, &thread.Data); err != nil {
return nil, fmt.Errorf("failed to read LC_UNIXTHREAD state struct data: %v", err)
}
l.Threads = append(l.Threads, thread)
}
f.Loads = append(f.Loads, l)
case types.LC_LOADFVMLIB:
var hdr types.LoadFvmLibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_LOADFVMLIB: %v", err)
}
l := new(LoadFvmlib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
l.MinorVersion = hdr.MinorVersion
l.HeaderAddr = hdr.HeaderAddr
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in LC_LOADFVMLIB command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
f.Loads = append(f.Loads, l)
case types.LC_IDFVMLIB:
var hdr types.IDFvmLibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_IDFVMLIB: %v", err)
}
l := new(IDFvmlib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
l.MinorVersion = hdr.MinorVersion
l.HeaderAddr = hdr.HeaderAddr
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in LC_IDFVMLIB command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
f.Loads = append(f.Loads, l)
case types.LC_IDENT:
var hdr types.IdentCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_IDENT: %v", err)
}
l := new(Ident)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
br := bufio.NewReader(b)
for {
o, err := br.ReadString('\x00')
if err == io.EOF {
break
}
if err != nil {
return nil, fmt.Errorf("failed to read LC_IDENT options: %v", err)
}
l.StrTable = append(l.StrTable, o)
}
f.Loads = append(f.Loads, l)
case types.LC_FVMFILE:
var hdr types.FvmFileCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_FVMFILE: %v", err)
}
l := new(FvmFile)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
l.HeaderAddr = hdr.HeaderAddr
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in LC_FVMFILE command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
f.Loads = append(f.Loads, l)
case types.LC_PREPAGE:
var hdr types.PrePageCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_PREPAGE: %v", err)
}
l := new(Prepage)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
f.Loads = append(f.Loads, l)
case types.LC_DYSYMTAB:
var hdr types.DysymtabCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_DYSYMTAB: %v", err)
}
if f.Symtab == nil {
return nil, &FormatError{offset, "dynamic symbol table seen before any ordinary symbol table", nil}
} else if hdr.Iundefsym > uint32(len(f.Symtab.Syms)) {
return nil, &FormatError{offset, fmt.Sprintf(
"undefined symbols index in dynamic symbol table command is greater than symbol table length (%d > %d)",
hdr.Iundefsym, len(f.Symtab.Syms)), nil}
} else if hdr.Iundefsym+hdr.Nundefsym > uint32(len(f.Symtab.Syms)) {
return nil, &FormatError{offset, fmt.Sprintf(
"number of undefined symbols after index in dynamic symbol table command is greater than symbol table length (%d > %d)",
hdr.Iundefsym+hdr.Nundefsym, len(f.Symtab.Syms)), nil}
}
dat, err := saferio.ReadDataAt(f.cr, uint64(hdr.Nindirectsyms)*4, int64(hdr.Indirectsymoff))
if err != nil {
return nil, fmt.Errorf("failed to read data at Indirectsymoff @ %#x: %w", int64(hdr.Indirectsymoff), err)
}
x := make([]uint32, hdr.Nindirectsyms)
if err := binary.Read(bytes.NewReader(dat), bo, x); err != nil {
return nil, fmt.Errorf("failed to read Nindirectsyms: %v", err)
}
// TODO: parse DylibTableOfContents if Ntoc > 0
// TODO: parse DylibModule if Nmodtab > 0
// TODO: parse DylibReference if Nextrefsyms > 0
// TODO: parse RelocInfo if Nlocrel > 0
st := new(Dysymtab)
st.LoadBytes = cmddat
st.LoadCmd = cmd
st.Len = siz
st.DysymtabCmd = hdr
st.IndirectSyms = x
f.Loads = append(f.Loads, st)
f.Dysymtab = st
case types.LC_LOAD_DYLIB:
var hdr types.DylibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_LOAD_DYLIB: %v", err)
}
l := new(LoadDylib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in dynamic library command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
l.Timestamp = hdr.Timestamp
l.CurrentVersion = hdr.CurrentVersion
l.CompatVersion = hdr.CompatVersion
f.Loads = append(f.Loads, l)
case types.LC_ID_DYLIB:
var hdr types.DylibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_ID_DYLIB: %v", err)
}
l := new(IDDylib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in dynamic library ident command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
l.Timestamp = hdr.Timestamp
l.CurrentVersion = hdr.CurrentVersion
l.CompatVersion = hdr.CompatVersion
f.Loads = append(f.Loads, l)
case types.LC_LOAD_DYLINKER:
var hdr types.DylinkerCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_LOAD_DYLINKER: %v", err)
}
l := new(LoadDylinker)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in load dylinker command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
f.Loads = append(f.Loads, l)
case types.LC_ID_DYLINKER:
var hdr types.IDDylinkerCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_ID_DYLINKER: %v", err)
}
l := new(DylinkerID)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in load dylinker command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
f.Loads = append(f.Loads, l)
case types.LC_PREBOUND_DYLIB:
var hdr types.PreboundDylibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_PREBOUND_DYLIB: %v", err)
}
l := new(PreboundDylib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in LC_PREBOUND_DYLIB command", hdr.NameOffset}
}
l.NumModules = hdr.NumModules
l.Name = cstring(cmddat[hdr.NameOffset:])
if hdr.LinkedModulesOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid linked modules in LC_PREBOUND_DYLIB command", hdr.NameOffset}
}
l.LinkedModulesBitVector = cstring(cmddat[hdr.LinkedModulesOffset:])
f.Loads = append(f.Loads, l)
case types.LC_ROUTINES:
var rt types.RoutinesCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &rt); err != nil {
return nil, fmt.Errorf("failed to read LC_ROUTINES: %v", err)
}
l := new(Routines)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.InitAddress = rt.InitAddress
l.InitModule = rt.InitModule
f.Loads = append(f.Loads, l)
case types.LC_SUB_FRAMEWORK:
var sf types.SubFrameworkCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &sf); err != nil {
return nil, fmt.Errorf("failed to read LC_SUB_FRAMEWORK: %v", err)
}
l := new(SubFramework)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.FrameworkOffset = sf.FrameworkOffset
if sf.FrameworkOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid framework in sub-framework command", sf.FrameworkOffset}
}
l.Framework = cstring(cmddat[sf.FrameworkOffset:])
f.Loads = append(f.Loads, l)
case types.LC_SUB_UMBRELLA:
var su types.SubUmbrellaCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &su); err != nil {
return nil, fmt.Errorf("failed to read LC_SUB_UMBRELLA: %v", err)
}
l := new(SubUmbrella)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.UmbrellaOffset = su.UmbrellaOffset
if su.UmbrellaOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid framework in sub-umbrella command", su.UmbrellaOffset}
}
l.Umbrella = cstring(cmddat[su.UmbrellaOffset:])
f.Loads = append(f.Loads, l)
case types.LC_SUB_CLIENT:
var sc types.SubClientCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &sc); err != nil {
return nil, fmt.Errorf("failed to read LC_SUB_CLIENT: %v", err)
}
l := new(SubClient)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.ClientOffset = sc.ClientOffset
if sc.ClientOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid path in sub client command", sc.ClientOffset}
}
l.Name = cstring(cmddat[sc.ClientOffset:])
f.Loads = append(f.Loads, l)
case types.LC_SUB_LIBRARY:
var s types.SubLibraryCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &s); err != nil {
return nil, fmt.Errorf("failed to read LC_SUB_LIBRARY: %v", err)
}
l := new(SubLibrary)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.LibraryOffset = s.LibraryOffset
if s.LibraryOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid framework in sub-library command", s.LibraryOffset}
}
l.Library = cstring(cmddat[s.LibraryOffset:])
f.Loads = append(f.Loads, l)
case types.LC_TWOLEVEL_HINTS:
var t types.TwolevelHintsCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &t); err != nil {
return nil, fmt.Errorf("failed to read LC_TWOLEVEL_HINTS: %v", err)
}
l := new(TwolevelHints)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.Offset = t.Offset
l.Hints = make([]types.TwolevelHint, t.NumHints)
if err := binary.Read(b, bo, &l.Hints); err != nil {
return nil, fmt.Errorf("failed to read hints data: %v", err)
}
f.Loads = append(f.Loads, l)
case types.LC_PREBIND_CKSUM:
var p types.PrebindCksumCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &p); err != nil {
return nil, fmt.Errorf("failed to read LC_PREBIND_CKSUM: %v", err)
}
l := new(PrebindCheckSum)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.CheckSum = p.CheckSum
f.Loads = append(f.Loads, l)
case types.LC_LOAD_WEAK_DYLIB:
var hdr types.DylibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_LOAD_WEAK_DYLIB: %v", err)
}
l := new(WeakDylib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in weak dynamic library command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
l.Timestamp = hdr.Timestamp
l.CurrentVersion = hdr.CurrentVersion
l.CompatVersion = hdr.CompatVersion
f.Loads = append(f.Loads, l)
case types.LC_ROUTINES_64:
var r64 types.Routines64Cmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &r64); err != nil {
return nil, fmt.Errorf("failed to read LC_ROUTINES_64: %v", err)
}
l := new(Routines64)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.InitAddress = r64.InitAddress
l.InitModule = r64.InitModule
f.Loads = append(f.Loads, l)
case types.LC_UUID:
var u types.UUIDCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &u); err != nil {
return nil, fmt.Errorf("failed to read LC_UUID: %v", err)
}
l := new(UUID)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.UUID = u.UUID
f.Loads = append(f.Loads, l)
case types.LC_RPATH:
var hdr types.RpathCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_RPATH: %v", err)
}
l := new(Rpath)
if hdr.PathOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid path in rpath command", hdr.PathOffset}
}
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.PathOffset = hdr.PathOffset
if hdr.PathOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid path in rpath command", hdr.PathOffset}
}
l.Path = cstring(cmddat[hdr.PathOffset:])
f.Loads = append(f.Loads, l)
case types.LC_CODE_SIGNATURE:
var hdr types.CodeSignatureCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_CODE_SIGNATURE: %v", err)
}
l := new(CodeSignature)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.Offset = hdr.Offset
l.Size = hdr.Size
csdat := make([]byte, hdr.Size)
if _, err := f.cr.ReadAt(csdat, int64(hdr.Offset)); err != nil {
return nil, fmt.Errorf("failed to read CS data at offset=%#x; %v", int64(hdr.Offset), err)
}
cs, err := codesign.ParseCodeSignature(csdat)
if err != nil {
return nil, fmt.Errorf("failed to ParseCodeSignature: %v", err)
}
l.CodeSignature = *cs
f.Loads = append(f.Loads, l)
case types.LC_SEGMENT_SPLIT_INFO:
var hdr types.SegmentSplitInfoCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_SEGMENT_SPLIT_INFO: %v", err)
}
l := new(SplitInfo)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.Offset = hdr.Offset
l.Size = hdr.Size
if l.Size > 0 {
ldat := make([]byte, l.Size)
if _, err := f.cr.ReadAt(ldat, int64(l.Offset)); err != nil {
return nil, fmt.Errorf("failed to read SplitInfo data at offset=%#x; %v", int64(hdr.Offset), err)
}
fsr := bytes.NewReader(ldat)
if err := binary.Read(fsr, bo, &l.Version); err != nil {
return nil, fmt.Errorf("failed to read LC_SEGMENT_SPLIT_INFO Version: %v", err)
}
}
f.Loads = append(f.Loads, l)
case types.LC_REEXPORT_DYLIB:
var hdr types.ReExportDylibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_REEXPORT_DYLIB: %v", err)
}
l := new(ReExportDylib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in dynamic library command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
l.Timestamp = hdr.Timestamp
l.CurrentVersion = hdr.CurrentVersion
l.CompatVersion = hdr.CompatVersion
f.Loads = append(f.Loads, l)
case types.LC_LAZY_LOAD_DYLIB:
var hdr types.LazyLoadDylibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_LAZY_LOAD_DYLIB: %v", err)
}
l := new(LazyLoadDylib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in load upwardl dylib command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
l.Timestamp = hdr.Timestamp
l.CurrentVersion = hdr.CurrentVersion
l.CompatVersion = hdr.CompatVersion
f.Loads = append(f.Loads, l)
case types.LC_ENCRYPTION_INFO:
var ei types.EncryptionInfoCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &ei); err != nil {
return nil, fmt.Errorf("failed to read LC_ENCRYPTION_INFO: %v", err)
}
l := new(EncryptionInfo)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.Offset = ei.Offset
l.Size = ei.Size
l.CryptID = ei.CryptID
f.Loads = append(f.Loads, l)
case types.LC_DYLD_INFO:
var info types.DyldInfoCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &info); err != nil {
return nil, fmt.Errorf("failed to read LC_DYLD_INFO: %v", err)
}
l := new(DyldInfo)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.RebaseOff = info.RebaseOff
l.RebaseSize = info.RebaseSize
l.BindOff = info.BindOff
l.BindSize = info.BindSize
l.WeakBindOff = info.WeakBindOff
l.WeakBindSize = info.WeakBindSize
l.LazyBindOff = info.LazyBindOff
l.LazyBindSize = info.LazyBindSize
l.ExportOff = info.ExportOff
l.ExportSize = info.ExportSize
f.Loads = append(f.Loads, l)
case types.LC_DYLD_INFO_ONLY:
var info types.DyldInfoOnlyCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &info); err != nil {
return nil, fmt.Errorf("failed to read LC_DYLD_INFO_ONLY: %v", err)
}
l := new(DyldInfoOnly)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.RebaseOff = info.RebaseOff
l.RebaseSize = info.RebaseSize
l.BindOff = info.BindOff
l.BindSize = info.BindSize
l.WeakBindOff = info.WeakBindOff
l.WeakBindSize = info.WeakBindSize
l.LazyBindOff = info.LazyBindOff
l.LazyBindSize = info.LazyBindSize
l.ExportOff = info.ExportOff
l.ExportSize = info.ExportSize
f.Loads = append(f.Loads, l)
case types.LC_LOAD_UPWARD_DYLIB:
var hdr types.LoadUpwardDylibCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &hdr); err != nil {
return nil, fmt.Errorf("failed to read LC_LOAD_UPWARD_DYLIB: %v", err)
}
l := new(UpwardDylib)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.NameOffset = hdr.NameOffset
if hdr.NameOffset >= uint32(len(cmddat)) {
return nil, &FormatError{offset, "invalid name in load upwardl dylib command", hdr.NameOffset}
}
l.Name = cstring(cmddat[hdr.NameOffset:])
l.Timestamp = hdr.Timestamp
l.CurrentVersion = hdr.CurrentVersion
l.CompatVersion = hdr.CompatVersion
f.Loads = append(f.Loads, l)
case types.LC_VERSION_MIN_MACOSX:
var verMin types.VersionMinMacOSCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &verMin); err != nil {
return nil, fmt.Errorf("failed to read LC_VERSION_MIN_MACOSX: %v", err)
}
l := new(VersionMinMacOSX)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.Version = verMin.Version
l.Sdk = verMin.Sdk
f.Loads = append(f.Loads, l)
case types.LC_VERSION_MIN_IPHONEOS:
var verMin types.VersionMinIPhoneOSCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &verMin); err != nil {
return nil, fmt.Errorf("failed to read LC_VERSION_MIN_IPHONEOS: %v", err)
}
l := new(VersionMiniPhoneOS)
l.LoadBytes = cmddat
l.LoadCmd = cmd
l.Len = siz
l.Version = verMin.Version
l.Sdk = verMin.Sdk
f.Loads = append(f.Loads, l)
case types.LC_FUNCTION_STARTS:
var led types.LinkEditDataCmd
b := bytes.NewReader(cmddat)
if err := binary.Read(b, bo, &led); err != nil {