forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDyldInfo.cpp
More file actions
2226 lines (1882 loc) · 71.5 KB
/
Copy pathDyldInfo.cpp
File metadata and controls
2226 lines (1882 loc) · 71.5 KB
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
/* Copyright 2017 - 2025 R. Thomas
* Copyright 2017 - 2025 Quarkslab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <sstream>
#include "logging.hpp"
#include "frozen.hpp"
#include "LIEF/iostream.hpp"
#include "LIEF/Abstract/Binary.hpp"
#include "LIEF/BinaryStream/SpanStream.hpp"
#include "LIEF/Visitor.hpp"
#include "LIEF/MachO/Binary.hpp"
#include "LIEF/MachO/DyldInfo.hpp"
#include "LIEF/MachO/DyldBindingInfo.hpp"
#include "LIEF/MachO/ExportInfo.hpp"
#include "LIEF/MachO/RelocationDyld.hpp"
#include "LIEF/MachO/SegmentCommand.hpp"
#include "LIEF/MachO/Symbol.hpp"
#include "LIEF/MachO/DylibCommand.hpp"
#include "MachO/exports_trie.hpp"
#include "MachO/Structures.hpp"
#include "Object.tcc"
namespace LIEF {
namespace MachO {
struct ThreadedBindData {
std::string symbol_name;
int64_t addend = 0;
int64_t library_ordinal = 0;
uint8_t symbol_flags = 0;
uint8_t type = 0;
};
DyldInfo::DyldInfo() = default;
DyldInfo::~DyldInfo() = default;
DyldInfo& DyldInfo::operator=(DyldInfo other) {
swap(other);
return *this;
}
DyldInfo::DyldInfo(const DyldInfo& copy) :
LoadCommand::LoadCommand{copy},
rebase_{copy.rebase_},
rebase_opcodes_{copy.rebase_opcodes_},
bind_{copy.bind_},
bind_opcodes_{copy.bind_opcodes_},
weak_bind_{copy.weak_bind_},
weak_bind_opcodes_{copy.weak_bind_opcodes_},
lazy_bind_{copy.lazy_bind_},
lazy_bind_opcodes_{copy.lazy_bind_opcodes_},
export_{copy.export_},
export_trie_{copy.export_trie_}
{}
DyldInfo::DyldInfo(const details::dyld_info_command& dyld_info_cmd) :
LoadCommand::LoadCommand{LoadCommand::TYPE(dyld_info_cmd.cmd), dyld_info_cmd.cmdsize},
rebase_{dyld_info_cmd.rebase_off, dyld_info_cmd.rebase_size},
bind_{dyld_info_cmd.bind_off, dyld_info_cmd.bind_size},
weak_bind_{dyld_info_cmd.weak_bind_off, dyld_info_cmd.weak_bind_size},
lazy_bind_{dyld_info_cmd.lazy_bind_off, dyld_info_cmd.lazy_bind_size},
export_{dyld_info_cmd.export_off, dyld_info_cmd.export_size}
{}
void DyldInfo::swap(DyldInfo& other) noexcept {
LoadCommand::swap(other);
std::swap(rebase_, other.rebase_);
std::swap(rebase_opcodes_, other.rebase_opcodes_);
std::swap(bind_, other.bind_);
std::swap(bind_opcodes_, other.bind_opcodes_);
std::swap(weak_bind_, other.weak_bind_);
std::swap(weak_bind_opcodes_, other.weak_bind_opcodes_);
std::swap(lazy_bind_, other.lazy_bind_);
std::swap(lazy_bind_opcodes_, other.lazy_bind_opcodes_);
std::swap(export_, other.export_);
std::swap(export_trie_, other.export_trie_);
std::swap(export_info_, other.export_info_);
std::swap(binding_info_, other.binding_info_);
std::swap(binary_, other.binary_);
}
void DyldInfo::rebase_opcodes(buffer_t raw) {
if (raw.size() > rebase_opcodes_.size()) {
LIEF_WARN("Can't update rebase opcodes. The provided data is larger than the original ones");
return;
}
std::move(std::begin(raw), std::end(raw), rebase_opcodes_.data());
}
std::string DyldInfo::show_rebases_opcodes() const {
static constexpr char tab[] = " ";
if (binary_ == nullptr) {
LIEF_WARN("Can't print rebase opcode");
return "";
}
size_t pint_v = static_cast<LIEF::Binary*>(binary_)->header().is_64() ? sizeof(uint64_t) : sizeof(uint32_t);
std::ostringstream output;
bool done = false;
uint8_t type = 0;
uint32_t segment_index = 0;
uint64_t segment_offset = 0;
uint32_t count = 0;
uint32_t skip = 0;
SpanStream rebase_stream = rebase_opcodes();
Binary::it_segments segments = binary_->segments();
while (!done && rebase_stream.pos() < rebase_stream.size()) {
auto val = rebase_stream.read<uint8_t>();
if (!val) {
break;
}
uint8_t imm = *val & IMMEDIATE_MASK;
auto opcode = REBASE_OPCODES(*val & OPCODE_MASK);
switch(opcode) {
case REBASE_OPCODES::DONE:
{
output << "[" << to_string(opcode) << "]" << '\n';
done = true;
break;
}
case REBASE_OPCODES::SET_TYPE_IMM:
{
type = imm;
output << "[" << to_string(opcode) << "] ";
output << "Type: " << to_string(REBASE_TYPE(type));
output << '\n';
break;
}
case REBASE_OPCODES::SET_SEGMENT_AND_OFFSET_ULEB:
{
if (auto val = rebase_stream.read_uleb128()) {
segment_offset = *val;
} else {
LIEF_ERR("Can't read REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB segment offset");
break;
}
segment_index = imm;
output << "[" << to_string(opcode) << "] ";
output << "Segment Index := " << std::dec << segment_index << " (" << segments[segment_index].name() << ") ";
output << "Segment Offset := " << std::hex << std::showbase << segment_offset;
output << '\n';
break;
}
case REBASE_OPCODES::ADD_ADDR_ULEB:
{
uint64_t val = 0;
if (auto res = rebase_stream.read_uleb128()) {
val = *res;
} else {
LIEF_ERR("Can't read REBASE_OPCODE_ADD_ADDR_ULEB segment offset");
break;
}
segment_offset += val;
output << "[" << to_string(opcode) << "] ";
output << "Segment Offset += " << std::hex << std::showbase << val << " (" << segment_offset << ")";
output << '\n';
break;
}
case REBASE_OPCODES::ADD_ADDR_IMM_SCALED:
{
segment_offset += (imm * pint_v);
output << "[" << to_string(opcode) << "]" ;
output << "Segment Offset += " << std::hex << std::showbase << (imm * pint_v) << " (" << segment_offset << ")";
output << '\n';
break;
}
case REBASE_OPCODES::DO_REBASE_IMM_TIMES:
{
output << "[" << to_string(opcode) << "]" << '\n';
output << tab << "for i in range(" << std::dec << static_cast<uint32_t>(imm) << "):" << '\n';
for (size_t i = 0; i < imm; ++i) {
output << tab << tab;
output << "rebase(";
output << to_string(REBASE_TYPE(type));
output << ", ";
output << segments[segment_index].name();
output << ", ";
output << std::hex << std::showbase << segment_offset;
output << ")" << '\n';
segment_offset += pint_v;
output << tab << tab;
output << "Segment Offset += " << std::hex << std::showbase << pint_v << " (" << segment_offset << ")";
output << '\n' << '\n';
}
output << '\n';
break;
}
case REBASE_OPCODES::DO_REBASE_ULEB_TIMES:
{
if (auto res = rebase_stream.read_uleb128()) {
count = *res;
} else {
LIEF_ERR("Can't read REBASE_OPCODE_DO_REBASE_ULEB_TIMES count");
break;
}
output << "[" << to_string(opcode) << "]" << '\n';
output << tab << "for i in range(" << std::dec << static_cast<uint32_t>(count) << "):" << '\n';
for (size_t i = 0; i < count; ++i) {
output << tab << tab;
output << "rebase(";
output << to_string(REBASE_TYPE(type));
output << ", ";
output << segments[segment_index].name();
output << ", ";
output << std::hex << std::showbase << segment_offset;
output << ")" << '\n';
segment_offset += pint_v;
output << tab << tab;
output << "Segment Offset += " << std::hex << std::showbase << pint_v << " (" << segment_offset << ")";
output << '\n' << '\n';
}
output << '\n';
break;
}
case REBASE_OPCODES::DO_REBASE_ADD_ADDR_ULEB:
{
output << "[" << to_string(opcode) << "]" << '\n';
output << tab;
output << "rebase(";
output << to_string(REBASE_TYPE(type));
output << ", ";
output << segments[segment_index].name();
output << ", ";
output << std::hex << std::showbase << segment_offset;
output << ")" << '\n';
uint64_t val = 0;
if (auto res = rebase_stream.read_uleb128()) {
val = *res;
} else {
LIEF_ERR("Can't read REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB value");
break;
}
segment_offset += val + pint_v;
output << tab;
output << "Segment Offset += " << std::hex << std::showbase << (val + pint_v) << " (" << segment_offset << ")";
output << '\n';
break;
}
case REBASE_OPCODES::DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
{
output << "[" << to_string(static_cast<REBASE_OPCODES>(opcode)) << "]" << '\n';
// Count
if (auto res = rebase_stream.read_uleb128()) {
count = *res;
} else {
LIEF_ERR("Can't read REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB count");
break;
}
// Skip
if (auto res = rebase_stream.read_uleb128()) {
skip = *res;
} else {
LIEF_ERR("Can't read REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB skip");
break;
}
output << tab << "for i in range(" << std::dec << static_cast<uint32_t>(count) << "):" << '\n';
for (size_t i = 0; i < count; ++i) {
output << tab << tab;
output << "rebase(";
output << to_string(REBASE_TYPE(type));
output << ", ";
output << segments[segment_index].name();
output << ", ";
output << std::hex << std::showbase << segment_offset;
output << ")" << '\n';
segment_offset += skip + pint_v;
output << tab << tab;
output << "Segment Offset += " << std::hex << std::showbase << skip << " + " << pint_v << " (" << segment_offset << ")";
output << '\n' << '\n';
}
break;
}
default:
{
output << "[UNSUPPORTED OPCODE - " << std::showbase << std::hex << static_cast<uint32_t>(opcode) << "]" << '\n';
break;
}
}
}
return output.str();
}
void DyldInfo::bind_opcodes(buffer_t raw) {
if (raw.size() > bind_opcodes_.size()) {
LIEF_WARN("Can't update bind opcodes. The provided data is larger than the original ones");
return;
}
std::move(std::begin(raw), std::end(raw), bind_opcodes_.data());
}
std::string DyldInfo::show_bind_opcodes() const {
std::ostringstream output;
show_bindings(output, bind_opcodes(), /* is_lazy = */ false);
return output.str();
}
void DyldInfo::show_bindings(std::ostream& output, span<const uint8_t> bind_opcodes, bool is_lazy) const {
if (binary_ == nullptr) {
LIEF_WARN("Can't print bind opcodes");
return;
}
size_t pint_v = static_cast<LIEF::Binary*>(binary_)->header().is_64() ?
sizeof(uint64_t) :
sizeof(uint32_t);
uint8_t type = is_lazy ? static_cast<uint8_t>(DyldBindingInfo::TYPE::POINTER) : 0;
uint8_t segment_idx = 0;
uint64_t segment_offset = 0;
std::string symbol_name;
int64_t library_ordinal = 0;
int64_t addend = 0;
uint32_t count = 0;
uint32_t skip = 0;
bool is_weak_import = false;
bool done = false;
size_t ordinal_table_size = 0;
bool use_threaded_rebase_bind = false;
uint8_t symbol_flags = 0;
std::vector<ThreadedBindData> ordinal_table;
Binary::it_segments segments = binary_->segments();
Binary::it_libraries libraries = binary_->libraries();
const std::string tab = " ";
SpanStream bind_stream = bind_opcodes;
while (!done && bind_stream.pos() < bind_stream.size()) {
auto val = bind_stream.read<uint8_t>();
if (!val) {
break;
}
uint8_t imm = *val & IMMEDIATE_MASK;
auto opcode = BIND_OPCODES(*val & OPCODE_MASK);
switch (opcode) {
case BIND_OPCODES::DONE:
{
output << "[" << to_string(opcode) << "]" << '\n';
if (!is_lazy) {
done = true;
}
break;
}
case BIND_OPCODES::SET_DYLIB_ORDINAL_IMM:
{
output << "[" << to_string(opcode) << "]" << '\n';
library_ordinal = imm;
output << tab << "Library Ordinal := " << std::dec << static_cast<uint32_t>(imm) << '\n';
break;
}
case BIND_OPCODES::SET_DYLIB_ORDINAL_ULEB:
{
output << "[" << to_string(opcode) << "]" << '\n';
if (auto res = bind_stream.read_uleb128()) {
library_ordinal = *res;
} else {
LIEF_ERR("Can't read BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB library ordinal value");
break;
}
output << tab << "Library Ordinal := " << std::dec << library_ordinal << '\n';
break;
}
case BIND_OPCODES::SET_DYLIB_SPECIAL_IMM:
{
output << "[" << to_string(opcode) << "]" << '\n';
// the special ordinals are negative numbers
if (imm == 0) {
library_ordinal = 0;
} else {
int8_t sign_extended = static_cast<int8_t>(OPCODE_MASK) | imm;
library_ordinal = sign_extended;
}
output << tab << "Library Ordinal := " << std::dec << library_ordinal << '\n';
break;
}
case BIND_OPCODES::SET_SYMBOL_TRAILING_FLAGS_IMM:
{
output << "[" << to_string(opcode) << "]" << '\n';
if (auto res = bind_stream.read_string()) {
symbol_name = std::move(*res);
} else {
LIEF_ERR("Can't read BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM symbol's name");
break;
}
symbol_flags = imm;
is_weak_import = (imm & BIND_SYMBOL_FLAGS::WEAK_IMPORT) != 0;
output << tab << "Symbol name := " << symbol_name << '\n';
output << tab << "Is Weak ? " << std::boolalpha << is_weak_import << '\n';
break;
}
case BIND_OPCODES::SET_TYPE_IMM:
{
output << "[" << to_string(opcode) << "]" << '\n';
type = imm;
output << tab << "Type := " << to_string(DyldBindingInfo::TYPE(type)) << '\n';
break;
}
case BIND_OPCODES::SET_ADDEND_SLEB:
{
output << "[" << to_string(opcode) << "]" << '\n';
if (auto res = bind_stream.read_sleb128()) {
addend = *res;
} else {
LIEF_ERR("Can't read BIND_OPCODE_SET_ADDEND_SLEB addend");
break;
}
output << tab << "Addend := " << std::dec << addend << '\n';
break;
}
case BIND_OPCODES::SET_SEGMENT_AND_OFFSET_ULEB:
{
output << "[" << to_string(opcode) << "]" << '\n';
segment_idx = imm;
if (auto res = bind_stream.read_uleb128()) {
segment_offset = *res;
} else {
LIEF_ERR("Can't read BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB segment offset");
break;
}
output << tab << "Segment := " << segments[segment_idx].name() << '\n';
output << tab << "Segment Offset := " << std::hex << std::showbase << segment_offset << '\n';
break;
}
case BIND_OPCODES::ADD_ADDR_ULEB:
{
output << "[" << to_string(opcode) << "]" << '\n';
uint64_t val = 0;
if (auto res = bind_stream.read_uleb128()) {
val = *res;
} else {
LIEF_ERR("Can't read BIND_OPCODE_ADD_ADDR_ULEB val");
break;
}
segment_offset += val;
output << tab << "Segment Offset += " << std::hex << std::showbase << val << " (" << segment_offset << ")" << '\n';
break;
}
case BIND_OPCODES::DO_BIND:
{
if (!use_threaded_rebase_bind) {
output << "[" << to_string(opcode) << "]" << '\n';
output << tab;
output << "bind(";
output << to_string(DyldBindingInfo::TYPE(type));
output << ", ";
output << segments[segment_idx].name();
output << ", ";
output << std::hex << std::showbase << segment_offset;
output << ", ";
output << symbol_name;
output << ", library_ordinal=";
output << (library_ordinal > 0 ? libraries[library_ordinal - 1].name() : std::to_string(library_ordinal));
output << ", addend=";
output << std::dec << addend;
output << ", is_weak_import=";
output << std::boolalpha << is_weak_import;
output << ")" << '\n';
segment_offset += pint_v;
output << tab << "Segment Offset += " << std::hex << std::showbase << pint_v << " (" << segment_offset << ")" << '\n';
} else {
ordinal_table.push_back(ThreadedBindData{symbol_name, addend, library_ordinal, symbol_flags, type});
}
break;
}
case BIND_OPCODES::DO_BIND_ADD_ADDR_ULEB:
{
output << "[" << to_string(opcode) << "]" << '\n';
output << tab;
output << "bind(";
output << to_string(DyldBindingInfo::TYPE(type));
output << ", ";
output << segments[segment_idx].name();
output << ", ";
output << std::hex << std::showbase << segment_offset;
output << ", ";
output << symbol_name;
output << ", library_ordinal=";
output << (library_ordinal > 0 ? libraries[library_ordinal - 1].name() : std::to_string(library_ordinal));
output << ", addend=";
output << std::dec << addend;
output << ", is_weak_import=";
output << std::boolalpha << is_weak_import;
output << ")" << '\n';
uint64_t v = 0;
if (auto res = bind_stream.read_uleb128()) {
v = *res;
} else {
LIEF_ERR("Can't read BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB val");
break;
}
segment_offset += v + pint_v;
output << tab << "Segment Offset += " << std::hex << std::showbase << v + pint_v << " (" << segment_offset << ")" << '\n';
break;
}
case BIND_OPCODES::DO_BIND_ADD_ADDR_IMM_SCALED:
{
output << "[" << to_string(opcode) << "]" << '\n';
output << tab;
output << "bind(";
output << to_string(DyldBindingInfo::TYPE(type));
output << ", ";
output << segments[segment_idx].name();
output << ", ";
output << std::hex << std::showbase << segment_offset;
output << ", ";
output << symbol_name;
output << ", library_ordinal=";
output << (library_ordinal > 0 ? libraries[library_ordinal - 1].name() : std::to_string(library_ordinal));
output << ", addend=";
output << std::dec << addend;
output << ", is_weak_import=";
output << std::boolalpha << is_weak_import;
output << ")" << '\n';
segment_offset += imm * pint_v + pint_v;
output << tab << "Segment Offset += " << std::hex << std::showbase << (imm * pint_v + pint_v) << " (" << segment_offset << ")" << '\n';
break;
}
case BIND_OPCODES::DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
{
output << "[" << to_string(opcode) << "]" << '\n';
// Count
if (auto res = bind_stream.read_uleb128()) {
count = *res;
} else {
LIEF_ERR("Can't read BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB count");
break;
}
// Skip
if (auto res = bind_stream.read_uleb128()) {
skip = *res;
} else {
LIEF_ERR("Can't read BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB skip");
break;
}
output << tab << "for i in range(" << std::dec << static_cast<uint32_t>(count) << "):" << '\n';
for (size_t i = 0; i < count; ++i) {
output << tab << tab;
output << "bind(";
output << to_string(DyldBindingInfo::TYPE(type));
output << ", ";
output << segments[segment_idx].name();
output << ", ";
output << std::hex << std::showbase << segment_offset;
output << ", ";
output << symbol_name;
output << ", library_ordinal=";
output << (library_ordinal > 0 ? libraries[library_ordinal - 1].name() : std::to_string(library_ordinal));
output << ", addend=";
output << std::dec << addend;
output << ", is_weak_import=";
output << std::boolalpha << is_weak_import;
output << ")" << '\n';
segment_offset += skip + pint_v;
output << "Segment Offset += " << std::hex << std::showbase << skip << " + " << pint_v << " (" << segment_offset << ")";
output << '\n' << '\n';
}
break;
}
case BIND_OPCODES::THREADED:
{
const auto subopcode = static_cast<BIND_SUBOPCODE_THREADED>(imm);
output << std::string("[") + to_string(BIND_OPCODES::THREADED) + "]\n";
switch (subopcode) {
case BIND_SUBOPCODE_THREADED::APPLY:
{
output << tab << std::string("[") + to_string(subopcode) + "]\n";
uint64_t delta = 0;
const SegmentCommand& current_segment = segments[segment_idx];
do {
const uint64_t address = current_segment.virtual_address() + segment_offset;
span<const uint8_t> content = current_segment.content();
if (segment_offset >= content.size() ||
(segment_offset + sizeof(uint64_t)) >= content.size())
{
LIEF_WARN("Bad segment offset (0x{:x})", segment_offset);
break;
}
auto value = *reinterpret_cast<const uint64_t*>(content.data() + segment_offset);
bool is_rebase = (value & (static_cast<uint64_t>(1) << 62)) == 0;
if (is_rebase) {
output << tab << tab << fmt::format("rebase({}, {}, 0x{:x})\n",
"THREADED_REBASE", current_segment.name(), segment_offset);
} else {
uint16_t ordinal = value & 0xFFFF;
if (ordinal >= ordinal_table_size || ordinal >= ordinal_table.size()) {
LIEF_WARN("bind ordinal ({:d}) is out of range (max={:d}) for disk pointer 0x{:04x} in "
"segment '{}' (segment offset: 0x{:04x})", ordinal, ordinal_table_size, value,
current_segment.name(), segment_offset);
break;
}
if (address < current_segment.virtual_address() ||
address >= (current_segment.virtual_address() + current_segment.virtual_size())) {
LIEF_WARN("Bad binding address");
break;
}
const ThreadedBindData& th_bind_data = ordinal_table[ordinal];
const int64_t library_ordinal = th_bind_data.library_ordinal;
output << tab << tab << fmt::format("threaded_bind({}/{}, 0x{:x}, {}, {}, library_ordinal={}, "
"addend={}, is_weak_import={})\n",
"THREADED_BIND", to_string(DyldBindingInfo::TYPE(th_bind_data.type)), segment_offset,
current_segment.name(), th_bind_data.symbol_name,
library_ordinal > 0 ? libraries[library_ordinal - 1].name() : std::to_string(library_ordinal),
th_bind_data.addend, th_bind_data.symbol_flags);
}
// The delta is bits [51..61]
// And bit 62 is to tell us if we are a rebase (0) or bind (1)
value &= ~(1ull << 62);
delta = (value & 0x3FF8000000000000) >> 51;
segment_offset += delta * sizeof(pint_v);
output << tab << tab << fmt::format("Segment Offset += 0x{:x} (0x{:x})\n", delta * sizeof(pint_v), segment_offset);
} while (delta != 0);
break;
}
case BIND_SUBOPCODE_THREADED::SET_BIND_ORDINAL_TABLE_SIZE_ULEB:
{
output << tab << std::string("[") + to_string(subopcode) + "]\n";
if (auto res = bind_stream.read_uleb128()) {
count = *res;
} else {
LIEF_ERR("Can't read BIND_SUBOPCODE_THREADED_SET_BIND_ORDINAL_TABLE_SIZE_ULEB count");
break;
}
ordinal_table_size = count + 1; // the +1 comes from: 'ld64 wrote the wrong value here and we need to offset by 1 for now.'
use_threaded_rebase_bind = true;
ordinal_table.reserve(ordinal_table_size);
output << tab << tab << fmt::format("Ordinal table size := {:d}\n", count);
break;
}
}
break;
}
default:
{
LIEF_ERR("Unsupported opcode: 0x{:x}", static_cast<uint32_t>(opcode));
output << fmt::format("[UNKNOWN OP 0x{:x}]\n", static_cast<uint32_t>(opcode));
break;
}
}
}
}
// Weak Binding
// ============
void DyldInfo::weak_bind_opcodes(buffer_t raw) {
if (raw.size() > weak_bind_opcodes_.size()) {
LIEF_WARN("Can't update weak bind opcodes. The provided data is larger than the original ones");
return;
}
std::move(std::begin(raw), std::end(raw), weak_bind_opcodes_.data());
}
std::string DyldInfo::show_weak_bind_opcodes() const {
std::ostringstream output;
show_bindings(output, weak_bind_opcodes(), /* is_lazy = */ false);
return output.str();
}
// Lazy Binding
// ============
void DyldInfo::lazy_bind_opcodes(buffer_t raw) {
if (raw.size() > lazy_bind_opcodes_.size()) {
LIEF_WARN("Can't update lazy bind opcodes. The provided data is larger than the original ones");
return;
}
std::move(std::begin(raw), std::end(raw), lazy_bind_opcodes_.data());
}
std::string DyldInfo::show_lazy_bind_opcodes() const {
std::ostringstream output;
show_bindings(output, lazy_bind_opcodes(), true);
return output.str();
}
// Export Info
// ===========
std::string DyldInfo::show_export_trie() const {
if (binary_ == nullptr) {
LIEF_WARN("Can't print bind opcodes");
return "";
}
std::ostringstream output;
span<const uint8_t> buffer = export_trie();
SpanStream stream = buffer;
show_trie(output, "", stream, 0, buffer.size(), "");
return output.str();
}
void DyldInfo::show_trie(std::ostream& output, std::string output_prefix, BinaryStream& stream,
uint64_t start, uint64_t end, const std::string& prefix) const
{
MachO::show_trie(output, std::move(output_prefix), stream, start, end, prefix);
}
void DyldInfo::export_trie(buffer_t raw) {
if (raw.size() > export_trie_.size()) {
LIEF_WARN("Can't update the export trie. The provided data is larger than the original ones");
return;
}
std::move(std::begin(raw), std::end(raw), export_trie_.data());
}
void DyldInfo::accept(Visitor& visitor) const {
visitor.visit(*this);
}
bool operator==(uint8_t lhs, DyldInfo::REBASE_OPCODES rhs) {
return lhs == static_cast<uint8_t>(rhs);
}
bool operator!=(uint8_t lhs, DyldInfo::REBASE_OPCODES rhs) {
return lhs != static_cast<uint8_t>(rhs);
}
DyldInfo& DyldInfo::update_rebase_info(vector_iostream& stream) {
auto cmp = [] (const RelocationDyld* lhs, const RelocationDyld* rhs) {
return *lhs < *rhs;
};
// In recent version of dyld, relocations are melt with bindings
if (binding_encoding_version_ != BINDING_ENCODING_VERSION::V1) {
return *this;
}
std::set<RelocationDyld*, decltype(cmp)> rebases(cmp);
Binary::relocations_t relocations = binary_->relocations_list();
for (Relocation* r : relocations) {
if (r->origin() == Relocation::ORIGIN::DYLDINFO) {
rebases.insert(r->as<RelocationDyld>());
}
}
uint64_t current_segment_start = 0;
uint64_t current_segment_end = 0;
uint32_t current_segment_index = 0;
uint8_t type = 0;
auto address = static_cast<uint64_t>(-1);
std::vector<details::rebase_instruction> output;
for (RelocationDyld* rebase : rebases) {
if (type != rebase->type()) {
output.emplace_back(static_cast<uint8_t>(REBASE_OPCODES::SET_TYPE_IMM), rebase->type());
type = rebase->type();
}
if (address != rebase->address()) {
if (rebase->address() < current_segment_start || rebase->address() >= current_segment_end) {
SegmentCommand* segment = rebase->segment();
if (segment == nullptr) {
LIEF_ERR("No segment associated with the RebaseInfo. Can't update!");
return *this;
}
size_t index = segment->index();
current_segment_start = segment->virtual_address();
current_segment_end = segment->virtual_address() + segment->virtual_size();
current_segment_index = index;
output.emplace_back(static_cast<uint8_t>(REBASE_OPCODES::SET_SEGMENT_AND_OFFSET_ULEB), current_segment_index, rebase->address() - current_segment_start);
} else {
output.emplace_back(static_cast<uint8_t>(REBASE_OPCODES::ADD_ADDR_ULEB), rebase->address() - address);
}
address = rebase->address();
}
output.emplace_back(static_cast<uint8_t>(REBASE_OPCODES::DO_REBASE_ULEB_TIMES), 1);
address += binary_->pointer_size();
if (address >= current_segment_end) {
address = 0;
}
}
output.emplace_back(static_cast<uint8_t>(REBASE_OPCODES::DONE), 0);
// ===========================================
// 1. First optimization
// Compress packed runs of pointers
// Based on ld64-274.2/src/ld/LinkEdit.hpp:239
// ===========================================
auto dst = std::begin(output);
for (auto it = std::begin(output); it->opcode != REBASE_OPCODES::DONE; ++it) {
if (it->opcode == REBASE_OPCODES::DO_REBASE_ULEB_TIMES && it->op1 == 1) {
*dst = *it++;
while (it->opcode == REBASE_OPCODES::DO_REBASE_ULEB_TIMES) {
dst->op1 += it->op1;
++it;
}
--it;
++dst;
} else {
*dst++ = *it;
}
}
dst->opcode = static_cast<uint8_t>(REBASE_OPCODES::DONE);
// ===========================================
// 2. Second optimization
// Combine rebase/add pairs
// Base on ld64-274.2/src/ld/LinkEdit.hpp:257
// ===========================================
dst = std::begin(output);
for (auto it = std::begin(output); it->opcode != REBASE_OPCODES::DONE; ++it) {
if ((it->opcode == REBASE_OPCODES::DO_REBASE_ULEB_TIMES)
&& it->op1 == 1
&& it[1].opcode == REBASE_OPCODES::ADD_ADDR_ULEB) {
dst->opcode = static_cast<uint8_t>(REBASE_OPCODES::DO_REBASE_ADD_ADDR_ULEB);
dst->op1 = it[1].op1;
++it;
++dst;
} else {
*dst++ = *it;
}
}
dst->opcode = static_cast<uint8_t>(REBASE_OPCODES::DONE);
// ===========================================
// 3. Third optimization
// Base on ld64-274.2/src/ld/LinkEdit.hpp:274
// ===========================================
dst = std::begin(output);
for (auto it = std::begin(output); it->opcode != REBASE_OPCODES::DONE; ++it) {
uint64_t delta = it->op1;
if ((it->opcode == REBASE_OPCODES::DO_REBASE_ADD_ADDR_ULEB)
&& (it[1].opcode == REBASE_OPCODES::DO_REBASE_ADD_ADDR_ULEB)
&& (it[2].opcode == REBASE_OPCODES::DO_REBASE_ADD_ADDR_ULEB)
&& (it[1].op1 == delta)
&& (it[2].op1 == delta) ) {
dst->opcode = static_cast<uint8_t>(REBASE_OPCODES::DO_REBASE_ULEB_TIMES_SKIPPING_ULEB);
dst->op1 = 1;
dst->op2 = delta;
++it;
while (it->opcode == REBASE_OPCODES::DO_REBASE_ADD_ADDR_ULEB && it->op1 == delta) {
dst->op1++;
++it;
}
--it;
++dst;
} else {
*dst++ = *it;
}
}
dst->opcode = static_cast<uint8_t>(REBASE_OPCODES::DONE);
// ===========================================
// 4. Fourth optimization
// Use immediate encodings
// Base on ld64-274.2/src/ld/LinkEdit.hpp:303
// ===========================================
const size_t pint_size = binary_->pointer_size();
for (auto it = std::begin(output); it->opcode != REBASE_OPCODES::DONE; ++it) {
if (it->opcode == REBASE_OPCODES::ADD_ADDR_ULEB && it->op1 < (15 * pint_size) && (it->op1 % pint_size) == 0) {
it->opcode = static_cast<uint8_t>(REBASE_OPCODES::ADD_ADDR_IMM_SCALED);
it->op1 = it->op1 / pint_size;
} else if ( (it->opcode == REBASE_OPCODES::DO_REBASE_ULEB_TIMES) && (it->op1 < 15) ) {
it->opcode = static_cast<uint8_t>(REBASE_OPCODES::DO_REBASE_IMM_TIMES);
}
}