-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
export.go
1108 lines (1016 loc) · 37.5 KB
/
export.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
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"path/filepath"
"sort"
"github.com/blacktop/go-macho/pkg/codesign"
ctypes "github.com/blacktop/go-macho/pkg/codesign/types"
"github.com/blacktop/go-macho/pkg/fixupchains"
"github.com/blacktop/go-macho/pkg/trie"
"github.com/blacktop/go-macho/types"
)
var reexportDeps uint64
type segInfo struct {
Start uint64
End uint64
// Size uint64
}
type segMapInfo struct {
Name string
Old segInfo
New segInfo
}
func (i segMapInfo) LessThan(o segMapInfo) bool {
return i.Old.Start < o.Old.Start
}
type exportSegMap []segMapInfo
func (m exportSegMap) Len() int {
return len(m)
}
func (m exportSegMap) Less(i, j int) bool {
return m[i].LessThan(m[j])
}
func (m exportSegMap) Swap(i, j int) {
m[i], m[j] = m[j], m[i]
}
func (m exportSegMap) Remap(offset uint64) (uint64, error) {
for _, segInfo := range m {
if segInfo.Old.Start <= offset && offset <= segInfo.Old.End {
return segInfo.New.Start + (offset - segInfo.Old.Start), nil
}
}
return 0, fmt.Errorf("failed to remap offset %#x", offset)
}
func (m exportSegMap) RemapSeg(name string, offset uint64) (uint64, uint64, error) {
for _, segInfo := range m {
if segInfo.Name == name {
return segInfo.New.Start + (offset - segInfo.Old.Start), (segInfo.New.End - segInfo.New.Start), nil
}
}
return 0, 0, fmt.Errorf("failed to remap offset %#x", offset)
}
func pageAlign(off, align uint64) uint64 {
if (off % align) != 0 {
off += align - (off % align)
}
return off
}
// Export exports an in-memory or cached dylib|kext MachO to a file
func (f *File) Export(path string, dcf *fixupchains.DyldChainedFixups, baseAddress uint64, locals []Symbol) (err error) {
var buf bytes.Buffer
var lebuf *bytes.Buffer
var segMap exportSegMap
inCache := f.FileHeader.Flags.DylibInCache()
// create segment offset map
var newSegOffset uint64
for _, seg := range f.Segments() {
segMap = append(segMap, segMapInfo{
Name: seg.Name,
Old: segInfo{
Start: seg.Offset,
End: seg.Offset + seg.Filesz,
},
New: segInfo{
Start: newSegOffset,
End: newSegOffset + pageAlign(seg.Filesz, 0x1000),
},
})
newSegOffset += pageAlign(seg.Filesz, 0x1000)
}
sort.Sort(segMap)
if err := f.optimizeLoadCommands(segMap); err != nil {
return fmt.Errorf("failed to optimize load commands: %v", err)
}
if inCache {
lebuf, err = f.optimizeLinkedit(locals)
if err != nil {
return fmt.Errorf("failed to optimize load commands: %v", err)
}
}
if err := f.optimizeObjC(segMap); err != nil {
return fmt.Errorf("failed to optimize ObjC: %v", err)
}
// if err := f.optimizeStubs(segMap); err != nil {
// return fmt.Errorf("failed to optimize ObjC: %v", err)
// }
if inCache {
f.FileHeader.Flags &= 0x7FFFFFFF // remove in-cache bit
}
if err := f.FileHeader.Write(&buf, f.ByteOrder); err != nil {
return fmt.Errorf("failed to write file header to buffer: %v", err)
}
if err := f.writeLoadCommands(&buf); err != nil {
return fmt.Errorf("failed to write load commands: %v", err)
}
endOfLoadsOffset := uint64(buf.Len())
// Write out segment data to buffer
for _, seg := range f.Segments() {
if seg.Filesz > 0 {
switch seg.Name {
case "__TEXT":
dat := make([]byte, seg.Filesz)
if _, err := f.cr.ReadAtAddr(dat, seg.Addr); err != nil {
return fmt.Errorf("failed to read segment %s data: %v", seg.Name, err)
}
if _, err := buf.Write(dat[endOfLoadsOffset:]); err != nil {
return fmt.Errorf("failed to write segment %s to export buffer: %v", seg.Name, err)
}
case "__LINKEDIT":
if inCache {
if _, err := buf.Write(lebuf.Bytes()); err != nil {
return fmt.Errorf("failed to write optimized segment %s to export buffer: %v", seg.Name, err)
}
} else {
dat := make([]byte, seg.Filesz)
if _, err := f.cr.ReadAtAddr(dat, seg.Addr); err != nil {
return fmt.Errorf("failed to read segment %s data: %v", seg.Name, err)
}
if _, err := buf.Write(dat); err != nil {
return fmt.Errorf("failed to write segment %s to export buffer: %v", seg.Name, err)
}
}
default:
dat := make([]byte, seg.Filesz)
if _, err := f.cr.ReadAtAddr(dat, seg.Addr); err != nil {
return fmt.Errorf("failed to read segment %s data: %v", seg.Name, err)
}
if _, err := buf.Write(dat); err != nil {
return fmt.Errorf("failed to write segment %s to export buffer: %v", seg.Name, err)
}
}
}
}
os.MkdirAll(filepath.Dir(path), os.ModePerm)
if err := os.WriteFile(path, buf.Bytes(), 0755); err != nil {
return fmt.Errorf("failed to write exported MachO to file %s: %w", path, err)
}
// FIXME: fixup chains are not yet supported (this should be done in the linkedit optimization step and create a REAL LC_DYLD_CHAINED_FIXUPS load command)
// if dcf != nil {
// newFile, err := os.OpenFile(path, os.O_WRONLY, 0755)
// if err != nil {
// return fmt.Errorf("failed to open exported MachO %s: %v", path, err)
// }
// defer newFile.Close()
// fi, err := newFile.Stat()
// if err != nil {
// return fmt.Errorf("failed to stat file %s: %v", path, err)
// }
// fileSize := fi.Size()
// for _, start := range dcf.Starts {
// if start.PageStarts != nil {
// for _, fixup := range start.Fixups {
// off, err := segMap.Remap(fixup.Offset())
// if err != nil {
// continue
// }
// if off == 0 || off >= uint64(fileSize) {
// continue
// }
// if _, err := newFile.Seek(int64(off), io.SeekStart); err != nil {
// return fmt.Errorf("failed to seek in exported file to offset %#x from the start: %v", off, err)
// }
// switch fx := fixup.(type) {
// case fixupchains.Bind:
// // var addend string
// // addr := uint64(f.Offset()) + m.GetBaseAddress()
// // if fullAddend := dcf.Imports[f.Ordinal()].Addend() + f.Addend(); fullAddend > 0 {
// // addend = fmt.Sprintf(" + %#x", fullAddend)
// // addr += fullAddend
// // }
// // sec = m.FindSectionForVMAddr(addr)
// // lib := m.LibraryOrdinalName(dcf.Imports[f.Ordinal()].LibOrdinal())
// // if sec != nil && sec != lastSec {
// // fmt.Printf("%s.%s\n", sec.Seg, sec.Name)
// // }
// // fmt.Printf("%s\t%s/%s%s\n", fixupchains.Bind(f).String(m.GetBaseAddress()), lib, f.Name(), addend)
// case fixupchains.Rebase:
// addr := uint64(fx.Target()) + baseAddress
// if err := binary.Write(newFile, f.ByteOrder, addr); err != nil {
// return fmt.Errorf("failed to write fixup address %#x: %v", addr, err)
// }
// }
// }
// }
// }
// }
return nil
}
func (f *File) CodeSign(config *codesign.Config) error {
var cs *CodeSignature
config.InitSlotHashes() // initialize slot hashes with default empty slot hashes
config.IsMain = f.Type == types.MH_EXECUTE
text := f.Segment("__TEXT")
if text == nil {
return fmt.Errorf("failed to find __TEXT segment")
}
config.TextOffset = uint64(text.Offset)
config.TextSize = uint64(text.Filesz)
// check if there is an embedded Info.plist
if infoPlist, err := f.GetEmbeddedInfoPlist(); err == nil {
config.InfoPlist = infoPlist
}
linkedit := f.Segment("__LINKEDIT")
if linkedit == nil {
return fmt.Errorf("failed to find __LINKEDIT segment")
}
if config.ResourceDirSlotHash != nil {
config.SlotHashes.ResourceDir = config.ResourceDirSlotHash
}
if cs = f.CodeSignature(); cs != nil { // existing code signature
// import settings from existing code signature
if len(cs.CodeDirectories) > 0 {
if config.ID == "" {
config.ID = cs.CodeDirectories[0].ID
}
if config.TeamID == "" {
config.TeamID = cs.CodeDirectories[0].TeamID
}
if config.Flags == ctypes.ADHOC {
config.Flags &= ^ctypes.LINKER_SIGNED // remove linker signed flag TODO: should I do this?
}
if config.Entitlements == nil {
config.Entitlements = []byte(cs.Entitlements)
}
if config.EntitlementsDER == nil {
config.EntitlementsDER = []byte(cs.EntitlementsDER)
}
if config.SpecialSlots == nil {
config.SpecialSlots = cs.CodeDirectories[0].SpecialSlots
}
if config.RuntimeVersion == 0 {
if cs.CodeDirectories[0].Header.Runtime != 0 {
config.RuntimeVersion = cs.CodeDirectories[0].Header.Runtime
} else if bvs := f.BuildVersions(); len(bvs) > 0 {
config.RuntimeVersion = bvs[0].Sdk
} else if vm := f.VersionMin(); vm != nil {
config.RuntimeVersion = vm.Sdk
}
}
}
} else { // create NEW code signature
if config.ID == "" {
return fmt.Errorf("you must supply an ID")
}
// infer runtime version from build or min version load commands if necessary
if config.Flags&ctypes.RUNTIME != 0 {
if config.RuntimeVersion == 0 {
if bvs := f.BuildVersions(); len(bvs) > 0 {
config.RuntimeVersion = bvs[0].Sdk
} else if vm := f.VersionMin(); vm != nil {
config.RuntimeVersion = vm.Sdk
}
}
} else {
config.RuntimeVersion = 0
}
cs = &CodeSignature{
CodeSignatureCmd: types.CodeSignatureCmd{
LoadCmd: types.LC_CODE_SIGNATURE,
Len: uint32(binary.Size(types.CodeSignatureCmd{})),
},
}
cs.Offset = pointerAlign(uint32(linkedit.Offset + linkedit.Filesz))
// add NEW codesignature load command
f.AddLoad(cs)
// refresh
cs = f.CodeSignature()
}
config.CodeSize = uint64(cs.Offset)
// cache __LINKEDIT data (up to but not including any existing code signature) for saving later.
// if the actual data doesn't go up to a page boundary (because we're adding a new signature), pad it with zeroes
ledata := make([]byte, uint64(cs.Offset)-linkedit.Offset)
size := uint64(len(ledata))
if size > linkedit.Filesz {
size = linkedit.Filesz
}
if n, err := f.cr.ReadAtAddr(ledata[:size], linkedit.Addr); err != nil {
return fmt.Errorf("failed to read __LINKEDIT data: read=%d, %v", n, err)
}
f.ledata = bytes.NewBuffer(ledata)
// update __LINKEDIT segment sizes
linkedit.Filesz = pageAlign(uint64(len(ledata))+codesign.EstimateCodeSignatureSize(config), 0x4000)
linkedit.Memsz = pageAlign(linkedit.Filesz, 0x8000)
// update LC_CODE_SIGNATURE size
cs.Size = uint32((linkedit.Offset + linkedit.Filesz) - uint64(cs.Offset))
// read data to be signed; pad to beginning of code signature if necessary (in case we added a signature to a not-page-aligned-size file)
data := make([]byte, cs.Offset)
if _, err := f.ReadAt(data[:linkedit.Offset+size], 0); err != nil {
return fmt.Errorf("failed to read codesign data: %v", err)
}
// write modified file header and load commands (including __LINKEDIT and CodeSignature), since they are covered by hashes
var buf bytes.Buffer
if err := f.FileHeader.Write(&buf, f.ByteOrder); err != nil {
return fmt.Errorf("failed to write updated header: %v", err)
}
if err := f.writeLoadCommands(&buf); err != nil {
return fmt.Errorf("failed to write updated load commands: %v", err)
}
copy(data, buf.Bytes())
// sign data and add it to the new LINKEDIT segment
csdata, err := codesign.Sign(bytes.NewReader(data), config)
if err != nil {
return fmt.Errorf("failed to create codesignature data: %v", err)
}
if _, err := f.ledata.Write(csdata); err != nil {
return fmt.Errorf("failed to write codesign data to linkedit segment data: %v", err)
}
if linkedit.Filesz < uint64(f.ledata.Len()) {
return fmt.Errorf("new linkedit data is larger than expected")
} else if linkedit.Filesz > uint64(f.ledata.Len()) { // pad with zeros
if _, err := f.ledata.Write(make([]byte, linkedit.Filesz-uint64(f.ledata.Len()))); err != nil {
return fmt.Errorf("failed to write linkedit segment padding: %v", err)
}
}
return nil
}
func (f *File) Save(outpath string) error {
var buf bytes.Buffer
if err := f.FileHeader.Write(&buf, f.ByteOrder); err != nil {
return fmt.Errorf("failed to write file header to buffer: %v", err)
}
if err := f.writeLoadCommands(&buf); err != nil {
return fmt.Errorf("failed to write load commands: %v", err)
}
endOfLoadsOffset := uint64(buf.Len())
// Write out segment data to buffer
for _, seg := range f.Segments() {
if seg.Filesz > 0 {
switch seg.Name {
case "__TEXT":
dat := make([]byte, seg.Filesz)
if _, err := f.cr.ReadAtAddr(dat, seg.Addr); err != nil {
return fmt.Errorf("failed to read segment %s data: %v", seg.Name, err)
}
if _, err := buf.Write(dat[endOfLoadsOffset:]); err != nil {
return fmt.Errorf("failed to write segment %s to export buffer: %v", seg.Name, err)
}
case "__LINKEDIT":
if f.ledata != nil && f.ledata.Len() > 0 && f.CodeSignature() != nil {
if _, err := buf.Write(f.ledata.Bytes()); err != nil {
return fmt.Errorf("failed to write segment %s to export buffer: %v", seg.Name, err)
}
} else {
dat := make([]byte, seg.Filesz)
if _, err := f.cr.ReadAtAddr(dat, seg.Addr); err != nil {
return fmt.Errorf("failed to read segment %s data: %v", seg.Name, err)
}
if _, err := buf.Write(dat); err != nil {
return fmt.Errorf("failed to write segment %s to export buffer: %v", seg.Name, err)
}
}
default:
dat := make([]byte, seg.Filesz)
if _, err := f.cr.ReadAtAddr(dat, seg.Addr); err != nil {
return fmt.Errorf("failed to read segment %s data: %v", seg.Name, err)
}
if _, err := buf.Write(dat); err != nil {
return fmt.Errorf("failed to write segment %s to export buffer: %v", seg.Name, err)
}
}
}
}
os.MkdirAll(filepath.Dir(outpath), os.ModePerm)
if err := os.WriteFile(outpath, buf.Bytes(), 0755); err != nil {
return fmt.Errorf("failed to save MachO to file %s: %v", outpath, err)
}
return nil
}
func (f *File) optimizeLoadCommands(segMap exportSegMap) error {
var depIndex uint64
for _, l := range f.Loads {
switch l.Command() {
case types.LC_SEGMENT:
fallthrough
case types.LC_SEGMENT_64:
seg := l.(*Segment)
off, sz, err := segMap.RemapSeg(seg.Name, seg.Offset)
if err != nil {
return fmt.Errorf("failed to remap offset in segment %s: %v", seg.Name, err)
}
seg.Offset = off
seg.Filesz = sz
seg.Memsz = sz
for i := uint32(0); i < seg.Nsect; i++ {
if f.Sections[i+seg.Firstsect].Offset != 0 {
off, err := segMap.Remap(uint64(f.Sections[i+seg.Firstsect].Offset))
if err != nil {
// return fmt.Errorf("failed to remap offset in section %s.%s: %v", seg.Name, f.Sections[i+seg.Firstsect].Name, err)
continue // FIXME: this is so that libcorecrypto.dylib will work as it has normal offsets for some reason
}
f.Sections[i+seg.Firstsect].Offset = uint32(off)
}
// roff, err := segMap.Remap(uint64(f.Sections[i+seg.Firstsect].Reloff))
// if err != nil {
// return fmt.Errorf("failed to remap rel offset in section %s: %v", f.Sections[i+seg.Firstsect].Name, err)
// }
// f.Sections[i+seg.Firstsect].Reloff = uint32(roff)
}
case types.LC_SYMTAB:
// symtab := l.(*Symtab)
// _ = symtab
// symoff, err := segMap.Remap(uint64(l.(*Symtab).Symoff))
// if err != nil {
// return fmt.Errorf("failed to remap symbol offset in %s: %v", l.Command(), err)
// }
// stroff, err := segMap.Remap(uint64(l.(*Symtab).Stroff))
// if err != nil {
// return fmt.Errorf("failed to remap string offset in %s: %v", l.Command(), err)
// }
// l.(*Symtab).Symoff = uint32(symoff)
// l.(*Symtab).Stroff = uint32(stroff)
case types.LC_DYSYMTAB:
// if l.(*Dysymtab).Tocoffset > 0 {
// tocoffset, err := segMap.Remap(uint64(l.(*Dysymtab).Tocoffset))
// if err != nil {
// return fmt.Errorf("failed to remap Tocoffset in %s: %v", l.Command(), err)
// }
// l.(*Dysymtab).Tocoffset = uint32(tocoffset)
// }
// if l.(*Dysymtab).Modtaboff > 0 {
// modtaboff, err := segMap.Remap(uint64(l.(*Dysymtab).Modtaboff))
// if err != nil {
// return fmt.Errorf("failed to remap Modtaboff in %s: %v", l.Command(), err)
// }
// l.(*Dysymtab).Modtaboff = uint32(modtaboff)
// }
// if l.(*Dysymtab).Extrefsymoff > 0 {
// extrefsymoff, err := segMap.Remap(uint64(l.(*Dysymtab).Extrefsymoff))
// if err != nil {
// return fmt.Errorf("failed to remap Extrefsymoff %s: %v", l.Command(), err)
// }
// l.(*Dysymtab).Extrefsymoff = uint32(extrefsymoff)
// }
// if l.(*Dysymtab).Indirectsymoff > 0 {
// indirectsymoff, err := segMap.Remap(uint64(l.(*Dysymtab).Indirectsymoff))
// if err != nil {
// return fmt.Errorf("failed to remap Indirectsymoff in %s: %v", l.Command(), err)
// }
// l.(*Dysymtab).Indirectsymoff = uint32(indirectsymoff)
// }
// if l.(*Dysymtab).Extreloff > 0 {
// extreloff, err := segMap.Remap(uint64(l.(*Dysymtab).Extreloff))
// if err != nil {
// return fmt.Errorf("failed to remap Extreloff in %s: %v", l.Command(), err)
// }
// l.(*Dysymtab).Extreloff = uint32(extreloff)
// }
// if l.(*Dysymtab).Locreloff > 0 {
// locreloff, err := segMap.Remap(uint64(l.(*Dysymtab).Locreloff))
// if err != nil {
// return fmt.Errorf("failed to remap Locreloff in %s: %v", l.Command(), err)
// }
// l.(*Dysymtab).Locreloff = uint32(locreloff)
// }
case types.LC_CODE_SIGNATURE:
// off, err := segMap.Remap(uint64(l.(*CodeSignature).Offset))
// if err != nil {
// return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
// }
// l.(*CodeSignature).Offset = uint32(off)
case types.LC_SEGMENT_SPLIT_INFO:
// <rdar://problem/23212513> dylibs iOS 9 dyld caches have bogus LC_SEGMENT_SPLIT_INFO
// off, err := segMap.Remap(uint64(l.(*SplitInfo).Offset))
// if err != nil {
// return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
// }
// l.(*SplitInfo).Offset = uint32(off)
case types.LC_ENCRYPTION_INFO:
off, err := segMap.Remap(uint64(l.(*EncryptionInfo).Offset))
if err != nil {
return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
}
l.(*EncryptionInfo).Offset = uint32(off)
case types.LC_DYLD_INFO:
// if l.(*DyldInfo).RebaseOff > 0 {
// rebaseOff, err := segMap.Remap(uint64(l.(*DyldInfo).RebaseOff))
// if err != nil {
// return fmt.Errorf("failed to remap RebaseOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfoOnly).RebaseOff = uint32(rebaseOff)
// }
// if l.(*DyldInfoOnly).BindOff > 0 {
// bindOff, err := segMap.Remap(uint64(l.(*DyldInfoOnly).BindOff))
// if err != nil {
// return fmt.Errorf("failed to remap BindOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfoOnly).BindOff = uint32(bindOff)
// }
// if l.(*DyldInfo).WeakBindOff > 0 {
// weakBindOff, err := segMap.Remap(uint64(l.(*DyldInfo).WeakBindOff))
// if err != nil {
// return fmt.Errorf("failed to remap WeakBindOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfo).WeakBindOff = uint32(weakBindOff)
// }
// if l.(*DyldInfo).LazyBindOff > 0 {
// lazyBindOff, err := segMap.Remap(uint64(l.(*DyldInfo).LazyBindOff))
// if err != nil {
// return fmt.Errorf("failed to remap LazyBindOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfo).LazyBindOff = uint32(lazyBindOff)
// }
// if l.(*DyldInfo).ExportOff > 0 {
// exportOff, err := segMap.Remap(uint64(l.(*DyldInfo).ExportOff))
// if err != nil {
// return fmt.Errorf("failed to remap ExportOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfo).ExportOff = uint32(exportOff)
// }
case types.LC_DYLD_INFO_ONLY:
// if l.(*DyldInfoOnly).RebaseOff > 0 {
// rebaseOff, err := segMap.Remap(uint64(l.(*DyldInfoOnly).RebaseOff))
// if err != nil {
// return fmt.Errorf("failed to remap RebaseOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfoOnly).RebaseOff = uint32(rebaseOff)
// }
// if l.(*DyldInfoOnly).BindOff > 0 {
// bindOff, err := segMap.Remap(uint64(l.(*DyldInfoOnly).BindOff))
// if err != nil {
// return fmt.Errorf("failed to remap BindOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfoOnly).BindOff = uint32(bindOff)
// }
// if l.(*DyldInfoOnly).WeakBindOff > 0 {
// weakBindOff, err := segMap.Remap(uint64(l.(*DyldInfoOnly).WeakBindOff))
// if err != nil {
// return fmt.Errorf("failed to remap WeakBindOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfoOnly).WeakBindOff = uint32(weakBindOff)
// }
// if l.(*DyldInfoOnly).LazyBindOff > 0 {
// lazyBindOff, err := segMap.Remap(uint64(l.(*DyldInfoOnly).LazyBindOff))
// if err != nil {
// return fmt.Errorf("failed to remap LazyBindOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfoOnly).LazyBindOff = uint32(lazyBindOff)
// }
// if l.(*DyldInfoOnly).ExportOff > 0 {
// exportOff, err := segMap.Remap(uint64(l.(*DyldInfoOnly).ExportOff))
// if err != nil {
// return fmt.Errorf("failed to remap ExportOff in %s: %v", l.Command(), err)
// }
// l.(*DyldInfoOnly).ExportOff = uint32(exportOff)
// }
case types.LC_FUNCTION_STARTS:
// off, err := segMap.Remap(uint64(l.(*FunctionStarts).Offset))
// if err != nil {
// return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
// }
// l.(*FunctionStarts).Offset = uint32(off)
case types.LC_MAIN:
// TODO:is this an offset or vmaddr ?
off, err := segMap.Remap(l.(*EntryPoint).EntryOffset)
if err != nil {
return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
}
l.(*EntryPoint).EntryOffset = off
case types.LC_DATA_IN_CODE:
// off, err := segMap.Remap(uint64(l.(*DataInCode).Offset))
// if err != nil {
// return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
// }
// l.(*DataInCode).Offset = uint32(off)
case types.LC_DYLIB_CODE_SIGN_DRS:
off, err := segMap.Remap(uint64(l.(*DylibCodeSignDrs).Offset))
if err != nil {
return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
}
l.(*DylibCodeSignDrs).Offset = uint32(off)
case types.LC_ENCRYPTION_INFO_64:
off, err := segMap.Remap(uint64(l.(*EncryptionInfo64).Offset))
if err != nil {
return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
}
l.(*EncryptionInfo64).Offset = uint32(off)
case types.LC_LINKER_OPTIMIZATION_HINT:
off, err := segMap.Remap(uint64(l.(*LinkerOptimizationHint).Offset))
if err != nil {
return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
}
l.(*LinkerOptimizationHint).Offset = uint32(off)
case types.LC_DYLD_EXPORTS_TRIE:
// off, err := segMap.Remap(uint64(l.(*DyldExportsTrie).Offset))
// if err != nil {
// return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
// }
// l.(*DyldExportsTrie).Offset = uint32(off)
case types.LC_DYLD_CHAINED_FIXUPS:
off, err := segMap.Remap(uint64(l.(*DyldChainedFixups).Offset))
if err != nil {
return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
}
l.(*DyldChainedFixups).Offset = uint32(off)
case types.LC_FILESET_ENTRY:
off, err := segMap.Remap(l.(*FilesetEntry).FileOffset)
if err != nil {
return fmt.Errorf("failed to remap offset in %s: %v", l.Command(), err)
}
l.(*FilesetEntry).FileOffset = off
case types.LC_LOAD_DYLIB:
fallthrough
case types.LC_LOAD_WEAK_DYLIB:
fallthrough
case types.LC_REEXPORT_DYLIB:
fallthrough
case types.LC_LOAD_UPWARD_DYLIB:
depIndex++
if l.Command() == types.LC_REEXPORT_DYLIB {
reexportDeps = depIndex
}
}
}
return nil
}
type stub struct {
ADRP uint64
LDR uint64
BR uint64
}
type authStub struct {
ADRP uint32
ADD uint32
LDR uint32
BRAA uint32
}
// TODO: 🚧 finish this
func (f *File) optimizeStubs(segMap exportSegMap) (*bytes.Buffer, error) {
var buf bytes.Buffer
writeAuthStub := func(saddr, paddr uint64) ([]byte, error) {
adrpDelta := (paddr & ^uint64(4096)) - (saddr & ^uint64(4096))
immhi := (adrpDelta >> 9) & (0x00FFFFE0)
immlo := (adrpDelta << 17) & (0x60000000)
addOffset := paddr - (paddr & ^uint64(4096))
imm12 := (addOffset << 10) & 0x3FFC00
stub := authStub{
ADRP: 0x90000011 | uint32(immlo|immhi),
ADD: 0x91000231 | uint32(imm12),
LDR: 0xF9400230,
BRAA: 0xD71F0A11,
}
if err := binary.Write(&buf, binary.LittleEndian, stub); err != nil {
return nil, err
}
return buf.Bytes(), nil
}
if autGOT := f.Section("__DATA_CONST", "__auth_got"); autGOT != nil {
data, err := autGOT.Data()
if err != nil {
return nil, err
}
gots := make([]uint64, autGOT.Size/f.pointerSize())
if err := binary.Read(bytes.NewReader(data), binary.LittleEndian, &gots); err != nil {
return nil, err
}
for i, got := range gots {
if got == 0 {
continue
}
writeAuthStub(autGOT.Addr+uint64(i)*f.pointerSize(), got)
}
}
return &buf, nil
}
func (f *File) optimizeObjC(segMap exportSegMap) error {
// classes, err := f.GetObjCClasses()
// if err != nil {
// if errors.Is(err, ErrObjcSectionNotFound) {
// return nil
// }
// return err
// }
// for _, class := range classes {
// if _, err := f.GetOffset(class.ClassPtr); err != nil {
// fmt.Println(class)
// } else {
// fmt.Println("WRITE TO LINKEDIT")
// }
// }
return nil // TODO: impliment this
}
func (f *File) optimizeLinkedit(locals []Symbol) (*bytes.Buffer, error) {
var err error
var newSymCount uint32
var lebuf bytes.Buffer
var newSymNames bytes.Buffer
var exports []trie.TrieExport
linkedit := f.Segment("__LINKEDIT")
if linkedit == nil {
return nil, fmt.Errorf("unable to find __LINKEDIT segment")
}
// TODO: LC_DYLD_CHAINED_FIXUPS
// optimize LC_DYLD_EXPORTS_TRIE
if dexpTrie := f.DyldExportsTrie(); dexpTrie != nil {
exports, err = f.DyldExports()
if err != nil {
return nil, fmt.Errorf("failed to get LC_DYLD_EXPORTS_TRIE exports: %v", err)
}
dat := make([]byte, dexpTrie.Size)
if _, err = f.cr.ReadAt(dat, int64(dexpTrie.Offset)); err != nil {
return nil, fmt.Errorf("failed to read LC_DYLD_EXPORTS_TRIE data: %v", err)
}
dexpTrie.Offset = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write LC_DYLD_EXPORTS_TRIE data: %v", err)
}
pad := linkedit.Offset + (uint64(lebuf.Len()) % f.pointerSize())
if _, err := lebuf.Write(make([]byte, pad)); err != nil {
return nil, fmt.Errorf("failed to write LC_DYLD_EXPORTS_TRIE padding: %v", err)
}
}
// optimize LC_DATA_IN_CODE
if dataNCode := f.DataInCode(); dataNCode != nil {
dat := make([]byte, dataNCode.Size)
if _, err = f.cr.ReadAt(dat, int64(dataNCode.Offset)); err != nil {
return nil, fmt.Errorf("failed to read LC_DATA_IN_CODE data: %v", err)
}
dataNCode.Offset = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write LC_DATA_IN_CODE data: %v", err)
}
pad := linkedit.Offset + (uint64(lebuf.Len()) % f.pointerSize())
if _, err := lebuf.Write(make([]byte, pad)); err != nil {
return nil, fmt.Errorf("failed to write LC_DATA_IN_CODE padding: %v", err)
}
}
// TODO: LC_SEGMENT_SPLIT_INFO
// TODO: LC_DYLIB_CODE_SIGN_DRS
// TODO: LC_LINKER_OPTIMIZATION_HINT
// optimize LC_FUNCTION_STARTS
if fstarts := f.FunctionStarts(); fstarts != nil {
dat := make([]byte, fstarts.Size)
if _, err := f.cr.ReadAt(dat, int64(fstarts.Offset)); err != nil {
return nil, fmt.Errorf("failed to read LC_FUNCTION_STARTS data: %v", err)
}
fstarts.Offset = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write LC_FUNCTION_STARTS data: %v", err)
}
pad := linkedit.Offset + (uint64(lebuf.Len()) % f.pointerSize())
if _, err := lebuf.Write(make([]byte, pad)); err != nil {
return nil, fmt.Errorf("failed to write LC_FUNCTION_STARTS padding: %v", err)
}
}
// optimize LC_DYLD_INFO|LC_DYLD_INFO_ONLY
if dinfo := f.DyldInfo(); dinfo != nil {
if dinfo.RebaseSize > 0 {
dat := make([]byte, dinfo.RebaseSize)
if _, err := f.cr.ReadAt(dat, int64(dinfo.RebaseOff)); err != nil {
return nil, fmt.Errorf("failed to read %s rebase data: %v", dinfo.LoadCmd, err)
}
dinfo.RebaseOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s rebase data: %v", dinfo.LoadCmd, err)
}
}
if dinfo.BindSize > 0 {
dat := make([]byte, dinfo.BindSize)
if _, err := f.cr.ReadAt(dat, int64(dinfo.BindOff)); err != nil {
return nil, fmt.Errorf("failed to read %s bind data: %v", dinfo.LoadCmd, err)
}
dinfo.BindOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s bind data: %v", dinfo.LoadCmd, err)
}
}
if dinfo.WeakBindSize > 0 {
dat := make([]byte, dinfo.WeakBindSize)
if _, err := f.cr.ReadAt(dat, int64(dinfo.WeakBindOff)); err != nil {
return nil, fmt.Errorf("failed to read %s weak bind data: %v", dinfo.LoadCmd, err)
}
dinfo.WeakBindOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s weak bind data: %v", dinfo.LoadCmd, err)
}
}
if dinfo.LazyBindSize > 0 {
dat := make([]byte, dinfo.LazyBindSize)
if _, err := f.cr.ReadAt(dat, int64(dinfo.LazyBindOff)); err != nil {
return nil, fmt.Errorf("failed to read %s lazy bind data: %v", dinfo.LoadCmd, err)
}
dinfo.LazyBindOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s lazy bind data: %v", dinfo.LoadCmd, err)
}
}
if dinfo.ExportSize > 0 {
dat := make([]byte, dinfo.ExportSize)
if _, err := f.cr.ReadAt(dat, int64(dinfo.ExportOff)); err != nil {
return nil, fmt.Errorf("failed to read %s export data: %v", dinfo.LoadCmd, err)
}
dinfo.ExportOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s export data: %v", dinfo.LoadCmd, err)
}
}
} else if dionly := f.DyldInfoOnly(); dionly != nil {
if dionly.RebaseSize > 0 {
dat := make([]byte, dionly.RebaseSize)
if _, err := f.cr.ReadAt(dat, int64(dionly.RebaseOff)); err != nil {
return nil, fmt.Errorf("failed to read %s rebase data: %v", dionly.LoadCmd, err)
}
dionly.RebaseOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s rebase data: %v", dionly.LoadCmd, err)
}
}
if dionly.BindSize > 0 {
dat := make([]byte, dionly.BindSize)
if _, err := f.cr.ReadAt(dat, int64(dionly.BindOff)); err != nil {
return nil, fmt.Errorf("failed to read %s bind data: %v", dionly.LoadCmd, err)
}
dionly.BindOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s bind data: %v", dionly.LoadCmd, err)
}
}
if dionly.WeakBindSize > 0 {
dat := make([]byte, dionly.WeakBindSize)
if _, err := f.cr.ReadAt(dat, int64(dionly.WeakBindOff)); err != nil {
return nil, fmt.Errorf("failed to read %s weak bind data: %v", dionly.LoadCmd, err)
}
dionly.WeakBindOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s weak bind data: %v", dionly.LoadCmd, err)
}
}
if dionly.LazyBindSize > 0 {
dat := make([]byte, dionly.LazyBindSize)
if _, err := f.cr.ReadAt(dat, int64(dionly.LazyBindOff)); err != nil {
return nil, fmt.Errorf("failed to read %s lazy bind data: %v", dionly.LoadCmd, err)
}
dionly.LazyBindOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s lazy bind data: %v", dionly.LoadCmd, err)
}
}
if dionly.ExportSize > 0 {
dat := make([]byte, dionly.ExportSize)
if _, err := f.cr.ReadAt(dat, int64(dionly.ExportOff)); err != nil {
return nil, fmt.Errorf("failed to read %s export data: %v", dionly.LoadCmd, err)
}
dionly.ExportOff = uint32(linkedit.Offset) + uint32(lebuf.Len())
if _, err := lebuf.Write(dat); err != nil {
return nil, fmt.Errorf("failed to write %s export data: %v", dionly.LoadCmd, err)
}
}
pad := linkedit.Offset + (uint64(lebuf.Len()) % f.pointerSize())
if _, err := lebuf.Write(make([]byte, pad)); err != nil {
return nil, fmt.Errorf("failed to write LC_DYLD_INFO|LC_DYLD_INFO_ONLY padding: %v", err)
}
}
// TODO: LC_CODE_SIGNATURE
newSymTabOffset := uint64(lebuf.Len())
// first pool entry is always empty string
newSymNames.WriteString("\x00")
// local symbols are first in dylibs, if this cache has unmapped locals, insert them all first
for _, lsym := range locals {
if err := binary.Write(&lebuf, binary.LittleEndian, types.Nlist64{
Nlist: types.Nlist{
Name: uint32(newSymNames.Len()),
Type: lsym.Type,
Sect: lsym.Sect,
Desc: lsym.Desc,
},
Value: lsym.Value,
}); err != nil {
return nil, fmt.Errorf("failed to write local nlist entry to NEW linkedit data: %v", err)
}
if _, err := newSymNames.WriteString(lsym.Name + "\x00"); err != nil {
return nil, fmt.Errorf("failed to write local symbol name string to NEW linkedit data: %v", err)
}
newSymCount++
}
// now start copying symbol table from start of externs instead of start of locals
// for _, sym := range f.Symtab.Syms[f.Dysymtab.Iextdefsym:] {
if f.Symtab != nil {
for _, sym := range f.Symtab.Syms {
if sym.Name == "<redacted>" {
continue
}
if err := binary.Write(&lebuf, binary.LittleEndian, types.Nlist64{
Nlist: types.Nlist{
Name: uint32(newSymNames.Len()),
Type: sym.Type,
Sect: sym.Sect,
Desc: sym.Desc,
},
Value: sym.Value,
}); err != nil {
return nil, fmt.Errorf("failed to write symtab nlist entry to NEW linkedit data: %v", err)
}
if _, err := newSymNames.WriteString(sym.Name + "\x00"); err != nil {
return nil, fmt.Errorf("failed to write symbol name string to NEW linkedit data: %v", err)
}
newSymCount++
}
}
// get all re-exports from LC_DYLD_EXPORTS_TRIE
for _, exp := range exports {
// If the symbol comes from a dylib that is re-exported, this is not an individual symbol re-export
// if ( _reexportDeps.count((int)entry.info.other) != 0 )
// return true;
// if !exp.Flags.Regular() || exp.Flags.ReExport() || reexportDeps == exp.Other {
if !exp.Flags.Regular() || exp.Flags.ReExport() {
if err := binary.Write(&lebuf, binary.LittleEndian, types.Nlist64{