forked from lief-project/LIEF
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BinaryParser.tcc
1513 lines (1215 loc) · 43.9 KB
/
BinaryParser.tcc
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 R. Thomas
* Copyright 2017 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 "LIEF/logging++.hpp"
#include "LIEF/MachO/DyldInfo.hpp"
#include "LIEF/MachO/FunctionStarts.hpp"
#include "LIEF/MachO/SourceVersion.hpp"
#include "LIEF/MachO/VersionMin.hpp"
#include "LIEF/MachO/Relocation.hpp"
#include "LIEF/MachO/RelocationObject.hpp"
#include "LIEF/MachO/RelocationDyld.hpp"
namespace LIEF {
namespace MachO {
template<class MACHO_T>
void BinaryParser::parse(void) {
this->parse_header<MACHO_T>();
if (this->binary_->header().nb_cmds() > 0) {
this->parse_load_commands<MACHO_T>();
}
for (Section& section : this->binary_->sections()) {
try {
this->parse_relocations<MACHO_T>(section);
} catch (const exception& e) {
LOG(WARNING) << e.what();
}
}
if (this->binary_->has_dyld_info() and this->config_.parse_dyldinfo_deeply()) {
try {
this->parse_dyldinfo_binds<MACHO_T>();
} catch (const exception& e) {
LOG(WARNING) << e.what();
}
try {
this->parse_dyldinfo_export();
} catch (const exception& e) {
LOG(WARNING) << e.what();
}
try {
this->parse_dyldinfo_rebases<MACHO_T>();
} catch (const exception& e) {
LOG(WARNING) << e.what();
}
}
}
template<class MACHO_T>
void BinaryParser::parse_header(void) {
using header_t = typename MACHO_T::header;
this->binary_->header_ = {reinterpret_cast<const header_t*>(this->stream_->read(0, sizeof(header_t)))};
}
template<class MACHO_T>
void BinaryParser::parse_load_commands(void) {
using header_t = typename MACHO_T::header;
using segment_command_t = typename MACHO_T::segment_command;
using section_t = typename MACHO_T::section;
VLOG(VDEBUG) << "[+] Building Load commands";
const Header& header = this->binary_->header();
uint64_t loadcommands_offset = sizeof(header_t);
if ((loadcommands_offset + header.sizeof_cmds()) > this->stream_->size()) {
throw corrupted("Commands are corrupted");
}
size_t nbcmds = header.nb_cmds();
if (header.nb_cmds() > BinaryParser::MAX_COMMANDS) {
nbcmds = BinaryParser::MAX_COMMANDS;
LOG(WARNING) << "Only the first " << std::dec << nbcmds << " will be parsed";
}
for (size_t i = 0; i < nbcmds; ++i) {
const load_command* command = reinterpret_cast<const load_command*>(
this->stream_->read(loadcommands_offset, sizeof(load_command)));
std::unique_ptr<LoadCommand> load_command{nullptr};
switch (command->cmd) {
// ===============
// Segment command
// ===============
case LOAD_COMMAND_TYPES::LC_SEGMENT_64:
case LOAD_COMMAND_TYPES::LC_SEGMENT:
{
uint64_t local_offset = loadcommands_offset;
load_command = std::unique_ptr<SegmentCommand>{new SegmentCommand{
reinterpret_cast<const segment_command_t*>(
this->stream_->read(loadcommands_offset, sizeof(segment_command_t)))}};
local_offset += sizeof(segment_command_t);
SegmentCommand* segment = dynamic_cast<SegmentCommand*>(load_command.get());
const uint8_t* content = static_cast<const uint8_t*>(
this->stream_->read(segment->file_offset(), segment->file_size()));
segment->content({
content,
content + segment->file_size()
});
// --------
// Sections
// --------
for (size_t j = 0; j < segment->numberof_sections(); ++j) {
const section_t* section_header = reinterpret_cast<const section_t*>(this->stream_->read(local_offset, sizeof(section_t)));
std::unique_ptr<Section> section{new Section{section_header}};
section->segment_ = segment;
segment->sections_.push_back(section.release());
local_offset += sizeof(section_t);
}
break;
}
// =============
// DyLib Command
// =============
case LOAD_COMMAND_TYPES::LC_LOAD_WEAK_DYLIB:
case LOAD_COMMAND_TYPES::LC_ID_DYLIB:
case LOAD_COMMAND_TYPES::LC_LOAD_DYLIB:
{
const dylib_command* cmd =
reinterpret_cast<const dylib_command*>(
this->stream_->read(loadcommands_offset, sizeof(dylib_command)));
load_command = std::unique_ptr<DylibCommand>{new DylibCommand{cmd}};
const uint32_t str_name_offset = cmd->dylib.name;
std::string name = this->stream_->get_string(loadcommands_offset + str_name_offset);
dynamic_cast<DylibCommand*>(load_command.get())->name(name);
break;
}
// =============
// RPath Command
// =============
case LOAD_COMMAND_TYPES::LC_RPATH:
{
const rpath_command* cmd =
reinterpret_cast<const rpath_command*>(
this->stream_->read(loadcommands_offset, sizeof(rpath_command)));
load_command = std::unique_ptr<RPathCommand>{new RPathCommand{cmd}};
const uint32_t str_path_offset = cmd->path;
std::string path = this->stream_->get_string(loadcommands_offset + str_path_offset);
dynamic_cast<RPathCommand*>(load_command.get())->path(path);
break;
}
// ====
// UUID
// ====
case LOAD_COMMAND_TYPES::LC_UUID:
{
VLOG(VDEBUG) << "[+] Building UUID";
const uuid_command* cmd =
reinterpret_cast<const uuid_command*>(
this->stream_->read(loadcommands_offset, sizeof(uuid_command)));
load_command = std::unique_ptr<UUIDCommand>{new UUIDCommand{cmd}};
break;
}
// ==============
// Dynamic Linker
// ==============
case LOAD_COMMAND_TYPES::LC_LOAD_DYLINKER:
case LOAD_COMMAND_TYPES::LC_ID_DYLINKER:
{
const dylinker_command* cmd =
reinterpret_cast<const dylinker_command*>(
this->stream_->read(loadcommands_offset, sizeof(dylinker_command)));
const uint32_t linker_name_offset = cmd->name;
std::string name = this->stream_->get_string(
loadcommands_offset +
linker_name_offset);
load_command = std::unique_ptr<DylinkerCommand>{new DylinkerCommand{cmd}};
dynamic_cast<DylinkerCommand*>(load_command.get())->name(name);
break;
}
// ==============
// Prebound Dylib
// ==============
case LOAD_COMMAND_TYPES::LC_PREBOUND_DYLIB:
{
VLOG(VDEBUG) << "[+] Parsing LC_PREBOUND_DYLIB";
load_command = std::unique_ptr<LoadCommand>{new LoadCommand{command}};
const prebound_dylib_command* cmd =
reinterpret_cast<const prebound_dylib_command*>(
this->stream_->read(loadcommands_offset, sizeof(prebound_dylib_command)));
std::string name = this->stream_->get_string(
loadcommands_offset +
cmd->name);
//uint32_t sizeof_linked_modules = (cmd->nmodules / 8) + (cmd->nmodules % 8);
break;
}
// ======
// Thread
// ======
case LOAD_COMMAND_TYPES::LC_THREAD:
case LOAD_COMMAND_TYPES::LC_UNIXTHREAD:
{
VLOG(VDEBUG) << "[+] Parsing LC_THREAD";
const thread_command* cmd =
reinterpret_cast<const thread_command*>(
this->stream_->read(loadcommands_offset, sizeof(thread_command)));
load_command = std::unique_ptr<ThreadCommand>{new ThreadCommand{cmd}};
ThreadCommand* thread = dynamic_cast<ThreadCommand*>(load_command.get());
thread->architecture_ = this->binary_->header().cpu_type();
VLOG(VDEBUG) << "FLAVOR: " << cmd->flavor << std::endl
<< "COUNT: " << cmd->count;
switch(this->binary_->header().cpu_type()) {
case CPU_TYPES::CPU_TYPE_X86:
{
const uint8_t* pstart = reinterpret_cast<const uint8_t*>(
this->stream_->read(loadcommands_offset + sizeof(thread_command), sizeof(x86_thread_state_t)));
thread->state_ = {pstart, pstart + sizeof(x86_thread_state_t)};
break;
}
case CPU_TYPES::CPU_TYPE_X86_64:
{
const uint8_t* pstart = reinterpret_cast<const uint8_t*>(
this->stream_->read(loadcommands_offset + sizeof(thread_command), sizeof(x86_thread_state64_t)));
thread->state_ = {pstart, pstart + sizeof(x86_thread_state64_t)};
break;
}
case CPU_TYPES::CPU_TYPE_ARM:
{
const uint8_t* pstart = reinterpret_cast<const uint8_t*>(
this->stream_->read(loadcommands_offset + sizeof(thread_command), sizeof(arm_thread_state_t)));
thread->state_ = {pstart, pstart + sizeof(arm_thread_state_t)};
break;
}
case CPU_TYPES::CPU_TYPE_ARM64:
{
const uint8_t* pstart = reinterpret_cast<const uint8_t*>(
this->stream_->read(loadcommands_offset + sizeof(thread_command), sizeof(arm_thread_state64_t)));
thread->state_ = {pstart, pstart + sizeof(arm_thread_state64_t)};
break;
}
default:
{
LOG(ERROR) << "Unknown architecture";
}
}
break;
}
// ===============
// Routine command
// ===============
case LOAD_COMMAND_TYPES::LC_ROUTINES:
case LOAD_COMMAND_TYPES::LC_ROUTINES_64:
{
VLOG(VDEBUG) << "[+] Parsing LC_ROUTINE";
load_command = std::unique_ptr<LoadCommand>{new LoadCommand{command}};
break;
}
// =============
// Symbols table
// =============
case LOAD_COMMAND_TYPES::LC_SYMTAB:
{
using nlist_t = typename MACHO_T::nlist;
VLOG(VDEBUG) << "[+] Parsing symbols";
const symtab_command* cmd =
reinterpret_cast<const symtab_command*>(
this->stream_->read(loadcommands_offset, sizeof(symtab_command)));
load_command = std::unique_ptr<SymbolCommand>{new SymbolCommand{cmd}};
const nlist_t* nlist = reinterpret_cast<const nlist_t*>(
this->stream_->read(cmd->symoff, sizeof(nlist_t)));
for (size_t j = 0; j < cmd->nsyms; ++j) {
std::unique_ptr<Symbol> symbol{new Symbol{&nlist[j]}};
uint32_t idx = nlist[j].n_strx;
if (idx > 0) {
symbol->name(
this->stream_->get_string(cmd->stroff + idx));
}
this->binary_->symbols_.push_back(symbol.release());
}
break;
}
// ===============
// Dynamic Symbols
// ===============
case LOAD_COMMAND_TYPES::LC_DYSYMTAB:
{
VLOG(VDEBUG) << "[+] Parsing dynamic symbols";
const dysymtab_command* cmd =
reinterpret_cast<const dysymtab_command*>(
this->stream_->read(loadcommands_offset, sizeof(dysymtab_command)));
load_command = std::unique_ptr<DynamicSymbolCommand>{new DynamicSymbolCommand{cmd}};
break;
}
// ===============
// Dyd Info
// ===============
case LOAD_COMMAND_TYPES::LC_DYLD_INFO:
case LOAD_COMMAND_TYPES::LC_DYLD_INFO_ONLY:
{
VLOG(VDEBUG) << "[+] Parsing dyld information";
const dyld_info_command* cmd =
reinterpret_cast<const dyld_info_command*>(
this->stream_->read(loadcommands_offset, sizeof(dyld_info_command)));
load_command = std::unique_ptr<DyldInfo>{new DyldInfo{cmd}};
break;
}
// ===============
// Source Version
// ===============
case LOAD_COMMAND_TYPES::LC_SOURCE_VERSION:
{
VLOG(VDEBUG) << "[+] Parsing LC_SOURCE_VERSION";
const source_version_command* cmd =
reinterpret_cast<const source_version_command*>(
this->stream_->read(loadcommands_offset, sizeof(version_min_command)));
load_command = std::unique_ptr<SourceVersion>{new SourceVersion{cmd}};
VLOG(VDEBUG) << "Version: " << std::hex << cmd->version;
break;
}
case LOAD_COMMAND_TYPES::LC_VERSION_MIN_MACOSX:
case LOAD_COMMAND_TYPES::LC_VERSION_MIN_IPHONEOS:
{
VLOG(VDEBUG) << "[+] Parsing " << to_string(static_cast<LOAD_COMMAND_TYPES>(command->cmd));
const version_min_command* cmd =
reinterpret_cast<const version_min_command*>(
this->stream_->read(loadcommands_offset, sizeof(version_min_command)));
VLOG(VDEBUG) << "Version: " << std::hex << cmd->version;
VLOG(VDEBUG) << "SDK: " << std::hex << cmd->sdk;
load_command = std::unique_ptr<VersionMin>{new VersionMin{cmd}};
break;
}
//case LOAD_COMMAND_TYPES::LC_TWOLEVEL_HINTS:
// {
// VLOG(VDEBUG) << "[+] Parsing LC_TWOLEVEL_HINTS";
// load_command = new LoadCommand{command};
// break;
// }
//case LOAD_COMMAND_TYPES::LC_SUB_FRAMEWORK:
// {
// VLOG(VDEBUG) << "[+] Parsing LC_SUB_FRAMEWORK";
// load_command = new LoadCommand{command};
// break;
// }
//case LOAD_COMMAND_TYPES::LC_SUB_UMBRELLA:
// {
// VLOG(VDEBUG) << "[+] Parsing LC_SUB_UMBRELLA";
// load_command = new LoadCommand{command};
// break;
// }
//case LOAD_COMMAND_TYPES::LC_SUB_LIBRARY:
// {
// VLOG(VDEBUG) << "[+] Parsing LC_SUB_LIBRARY";
// load_command = new LoadCommand{command};
// break;
// }
//case LOAD_COMMAND_TYPES::LC_SUB_CLIENT:
// {
// VLOG(VDEBUG) << "[+] Parsing LC_SUB_CLIENT";
// load_command = new LoadCommand{command};
// break;
// }
// =======
// LC_MAIN
// =======
case LOAD_COMMAND_TYPES::LC_MAIN:
{
VLOG(VDEBUG) << "[+] Parsing LC_MAIN";
const entry_point_command* cmd =
reinterpret_cast<const entry_point_command*>(
this->stream_->read(loadcommands_offset, sizeof(entry_point_command)));
load_command = std::unique_ptr<MainCommand>{new MainCommand{cmd}};
break;
}
// ==================
// LC_FUNCTION_STARTS
// ==================
case LOAD_COMMAND_TYPES::LC_FUNCTION_STARTS:
{
VLOG(VDEBUG) << "[+] Parsing LC_FUNCTION_STARTS";
const linkedit_data_command* cmd =
reinterpret_cast<const linkedit_data_command*>(
this->stream_->read(loadcommands_offset, sizeof(linkedit_data_command)));
load_command = std::unique_ptr<FunctionStarts>{new FunctionStarts{cmd}};
uint64_t offset = cmd->dataoff;
std::pair<uint64_t, uint64_t> value_delta;
uint64_t value = 0;
FunctionStarts* fstart = dynamic_cast<FunctionStarts*>(load_command.get());
do {
value_delta = this->stream_->read_uleb128(offset);
if (std::get<0>(value_delta) == 0) {
break;
}
value += std::get<0>(value_delta);
offset += std::get<1>(value_delta);
VLOG(VDEBUG) << "Value: " << std::hex << value;
fstart->add_function(value);
} while(offset < (cmd->dataoff + cmd->datasize) and std::get<0>(value_delta) > 0);
break;
}
//case LOAD_COMMAND_TYPES::LC_CODE_SIGNATURE:
// {
// VLOG(VDEBUG) << "[+] Parsing LC_CODE_SIGNATURE";
// load_command = new LoadCommand{command};
// break;
// }
default:
{
LOG(WARNING) << "Command '" << to_string(static_cast<LOAD_COMMAND_TYPES>(command->cmd))
<< "' not parsed";
load_command = std::unique_ptr<LoadCommand>{new LoadCommand{command}};
}
}
if (load_command != nullptr) {
const uint8_t* content = static_cast<const uint8_t*>(
this->stream_->read(loadcommands_offset, command->cmdsize));
load_command->data({
content,
content + command->cmdsize
});
load_command->command_offset(loadcommands_offset);
this->binary_->commands_.push_back(load_command.release());
}
loadcommands_offset += command->cmdsize;
}
}
template<class MACHO_T>
void BinaryParser::parse_relocations(Section& section) {
if (section.numberof_relocations() == 0) {
VLOG(VDEBUG) << "No relocations in " << section.name();
return;
}
VLOG(VDEBUG) << "Parse '" << section.name() << "' relocations (" << std::dec << section.numberof_relocations() << ")";
uint64_t current_reloc_offset = section.relocation_offset();
size_t numberof_relocations = section.numberof_relocations();
if (section.numberof_relocations() > BinaryParser::MAX_RELOCATIONS) {
numberof_relocations = BinaryParser::MAX_RELOCATIONS;
LOG(WARNING) << "Huge number of relocations (" << std::dec << section.relocation_offset() << "). "
<< "Only the first " << std::dec << numberof_relocations << " will be parsed";
}
if (current_reloc_offset + numberof_relocations * 2 * sizeof(uint32_t) > this->stream_->size()) {
LOG(WARNING) << "Relocations corrupted";
return;
}
std::unique_ptr<RelocationObject> reloc{nullptr};
for (size_t i = 0; i < numberof_relocations; ++i) {
int32_t address = this->stream_->read_integer<int32_t>(current_reloc_offset);
bool is_scattered = static_cast<bool>(address & R_SCATTERED);
if (is_scattered) {
const scattered_relocation_info* reloc_info = reinterpret_cast<const scattered_relocation_info*>(
this->stream_->read(current_reloc_offset, sizeof(scattered_relocation_info)));
reloc = std::unique_ptr<RelocationObject>{new RelocationObject{reloc_info}};
reloc->section_ = §ion;
} else {
const relocation_info* reloc_info = reinterpret_cast<const relocation_info*>(
this->stream_->read(current_reloc_offset, sizeof(relocation_info)));
reloc = std::unique_ptr<RelocationObject>{new RelocationObject{reloc_info}};
reloc->section_ = §ion;
if (reloc_info->r_extern == 1 and reloc_info->r_symbolnum != R_ABS) {
if (reloc_info->r_symbolnum < this->binary_->symbols().size()) {
Symbol& symbol = this->binary_->symbols()[reloc_info->r_symbolnum];
reloc->symbol_ = &symbol;
VLOG(VDEBUG) << "Symbol: " << symbol.name();
} else {
LOG(WARNING) << "Relocation #" << std::dec << i << " of " << section.name() << " symbol index is out-of-bound";
}
}
if (reloc_info->r_extern == 0) {
if (reloc_info->r_symbolnum < this->binary_->sections().size()) {
Section& relsec = this->binary_->sections()[reloc_info->r_symbolnum];
reloc->section_ = &relsec;
VLOG(VDEBUG) << "Section: " << relsec.name();
} else {
LOG(WARNING) << "Relocation #" << std::dec << i << " of " << section.name() << " seems corrupted";
}
}
}
if (reloc) {
if (not reloc->has_section()) {
reloc->section_ = §ion;
}
reloc->architecture_ = this->binary_->header().cpu_type();
RelocationObject *r = reloc.release();
auto&& result = section.relocations_.emplace(r);
if (not result.second) { // Not inserted (Relocation already present)
delete r;
}
}
current_reloc_offset += 2 * sizeof(uint32_t);
}
}
template<class MACHO_T>
void BinaryParser::parse_dyldinfo_rebases() {
using pint_t = typename MACHO_T::uint;
DyldInfo& dyldinfo = this->binary_->dyld_info();
uint32_t offset = std::get<0>(dyldinfo.rebase());
uint32_t size = std::get<1>(dyldinfo.rebase());
if (offset == 0 or size == 0) {
return;
}
try {
const uint8_t* raw_rebase = reinterpret_cast<const uint8_t*>(this->stream_->read(offset, size));
dyldinfo.rebase_opcodes({raw_rebase, raw_rebase + size});
} catch (const exception& e) {
LOG(WARNING) << e.what();
}
uint64_t current_offset = offset;
uint64_t end_offset = offset + size;
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;
std::pair<uint64_t, uint64_t> value_delta = {0, 0};
while (not done and current_offset < end_offset) {
uint8_t imm = this->stream_->read_integer<uint8_t>(current_offset) & REBASE_IMMEDIATE_MASK;
uint8_t opcode = this->stream_->read_integer<uint8_t>(current_offset) & REBASE_OPCODE_MASK;
current_offset += sizeof(uint8_t);
switch(static_cast<REBASE_OPCODES>(opcode)) {
case REBASE_OPCODES::REBASE_OPCODE_DONE:
{
done = true;
break;
}
case REBASE_OPCODES::REBASE_OPCODE_SET_TYPE_IMM:
{
type = imm;
break;
}
case REBASE_OPCODES::REBASE_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
{
value_delta = this->stream_->read_uleb128(current_offset);
segment_index = imm;
segment_offset = std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);
break;
}
case REBASE_OPCODES::REBASE_OPCODE_ADD_ADDR_ULEB:
{
value_delta = this->stream_->read_uleb128(current_offset);
segment_offset += std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);
break;
}
case REBASE_OPCODES::REBASE_OPCODE_ADD_ADDR_IMM_SCALED:
{
segment_offset += (imm * sizeof(pint_t));
break;
}
case REBASE_OPCODES::REBASE_OPCODE_DO_REBASE_IMM_TIMES:
{
for (size_t i = 0; i < imm; ++i) {
this->do_rebase<MACHO_T>(type, segment_index, segment_offset);
segment_offset += sizeof(pint_t);
}
break;
}
case REBASE_OPCODES::REBASE_OPCODE_DO_REBASE_ULEB_TIMES:
{
value_delta = this->stream_->read_uleb128(current_offset);
count = std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);
for (size_t i = 0; i < count; ++i) {
this->do_rebase<MACHO_T>(type, segment_index, segment_offset);
segment_offset += sizeof(pint_t);
}
break;
}
case REBASE_OPCODES::REBASE_OPCODE_DO_REBASE_ADD_ADDR_ULEB:
{
this->do_rebase<MACHO_T>(type, segment_index, segment_offset);
value_delta = this->stream_->read_uleb128(current_offset);
segment_offset += std::get<0>(value_delta) + sizeof(pint_t);
current_offset += std::get<1>(value_delta);
break;
}
case REBASE_OPCODES::REBASE_OPCODE_DO_REBASE_ULEB_TIMES_SKIPPING_ULEB:
{
// Count
value_delta = this->stream_->read_uleb128(current_offset);
count += std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);
// Skip
value_delta = this->stream_->read_uleb128(current_offset);
skip += std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);
for (size_t i = 0; i < count; ++i) {
this->do_rebase<MACHO_T>(type, segment_index, segment_offset);
segment_offset += skip + sizeof(pint_t);
}
break;
}
default:
{
LOG(ERROR) << "Unsupported opcode: 0x" << std::hex << static_cast<uint32_t>(opcode);
break;
}
}
}
it_segments segments = this->binary_->segments();
// Tie segments and relocations
// The **OWNER**: Segment (destructor)
for (SegmentCommand& segment : segments) {
for (Relocation& relocation : segment.relocations()) {
relocation.segment_ = &segment;
}
}
// Tie sections and relocations
for (SegmentCommand& segment : segments) {
uint64_t offset = 0;
for (Relocation& relocation : segment.relocations()) {
if (not this->binary_->is_valid_addr(relocation.address())) {
continue;
}
try {
Section& section = this->binary_->section_from_virtual_address(relocation.address());
relocation.section_ = §ion;
} catch (const not_found& e) {
VLOG(VDEBUG) << "Unable to tie a section with dyld relocation at 0x" << std::hex << relocation.address() << " - 0x" << offset;
}
}
}
// Tie symbols and relocations
for (Relocation& relocation : this->binary_->relocations()) {
if (not this->binary_->is_valid_addr(relocation.address())) {
continue;
}
uint64_t address = relocation.address();
auto&& it_symbol = std::find_if(
std::begin(this->binary_->symbols_),
std::end(this->binary_->symbols_),
[&address] (const Symbol* sym) {
return sym->value() == address;
});
if (it_symbol != std::end(this->binary_->symbols_)) {
relocation.symbol_ = *it_symbol;
}
}
}
template<class MACHO_T>
void BinaryParser::parse_dyldinfo_binds() {
try {
this->parse_dyldinfo_generic_bind<MACHO_T>();
} catch (const exception& e) {
throw corrupted(e.what());
}
try {
this->parse_dyldinfo_weak_bind<MACHO_T>();
} catch (const exception& e) {
throw corrupted(e.what());
}
try {
this->parse_dyldinfo_lazy_bind<MACHO_T>();
} catch (const exception& e) {
throw corrupted(e.what());
}
}
// Generic bindings
// ================
template<class MACHO_T>
void BinaryParser::parse_dyldinfo_generic_bind() {
using pint_t = typename MACHO_T::uint;
DyldInfo& dyldinfo = this->binary_->dyld_info();
uint32_t offset = std::get<0>(dyldinfo.bind());
uint32_t size = std::get<1>(dyldinfo.bind());
if (offset == 0 or size == 0) {
return;
}
try {
const uint8_t* raw_binding = reinterpret_cast<const uint8_t*>(this->stream_->read(offset, size));
dyldinfo.bind_opcodes({raw_binding, raw_binding + size});
} catch (const exception& e) {
LOG(WARNING) << e.what();
}
uint64_t current_offset = offset;
uint64_t end_offset = offset + size;
uint8_t type = 0;
uint8_t segment_idx = 0;
uint64_t segment_offset = 0;
std::string symbol_name = "";
int library_ordinal = 0;
int64_t addend = 0;
uint32_t count = 0;
uint32_t skip = 0;
bool is_weak_import = false;
bool done = false;
std::pair<uint64_t, uint64_t> value_delta = {0, 0};
std::pair<int64_t, uint64_t> svalue_delta = {0, 0};
it_segments segments = this->binary_->segments();
while (not done and current_offset < end_offset) {
uint8_t imm = this->stream_->read_integer<uint8_t>(current_offset) & BIND_IMMEDIATE_MASK;
uint8_t opcode = this->stream_->read_integer<uint8_t>(current_offset) & BIND_OPCODE_MASK;
current_offset += sizeof(uint8_t);
switch (opcode) {
case BIND_OPCODE_DONE:
{
done = true;
break;
}
case BIND_OPCODE_SET_DYLIB_ORDINAL_IMM:
{
library_ordinal = imm;
break;
}
case BIND_OPCODE_SET_DYLIB_ORDINAL_ULEB:
{
value_delta = this->stream_->read_uleb128(current_offset);
library_ordinal = std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);
break;
}
case BIND_OPCODE_SET_DYLIB_SPECIAL_IMM:
{
// the special ordinals are negative numbers
if (imm == 0) {
library_ordinal = 0;
} else {
int8_t sign_extended = BIND_OPCODE_MASK | imm;
library_ordinal = sign_extended;
}
break;
}
case BIND_OPCODE_SET_SYMBOL_TRAILING_FLAGS_IMM:
{
symbol_name = this->stream_->get_string(current_offset);
current_offset += symbol_name.size() + 1;
if ((imm & BIND_SYMBOL_FLAGS_WEAK_IMPORT) != 0) {
is_weak_import = true;
} else {
is_weak_import = false;
}
break;
}
case BIND_OPCODE_SET_TYPE_IMM:
{
type = imm;
break;
}
case BIND_OPCODE_SET_ADDEND_SLEB:
{
svalue_delta = this->stream_->read_sleb128(current_offset);
addend = std::get<0>(svalue_delta);
current_offset += std::get<1>(svalue_delta);
break;
}
case BIND_OPCODE_SET_SEGMENT_AND_OFFSET_ULEB:
{
segment_idx = imm;
value_delta = this->stream_->read_uleb128(current_offset);
segment_offset = std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);
break;
}
case BIND_OPCODE_ADD_ADDR_ULEB:
{
value_delta = this->stream_->read_uleb128(current_offset);
segment_offset += std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);
break;
}
case BIND_OPCODE_DO_BIND:
{
this->do_bind<MACHO_T>(
BINDING_CLASS::BIND_CLASS_STANDARD,
type,
segment_idx,
segment_offset,
symbol_name,
library_ordinal,
addend,
is_weak_import,
segments);
segment_offset += sizeof(pint_t);
break;
}
case BIND_OPCODE_DO_BIND_ADD_ADDR_ULEB:
{
this->do_bind<MACHO_T>(
BINDING_CLASS::BIND_CLASS_STANDARD,
type,
segment_idx,
segment_offset,
symbol_name,
library_ordinal,
addend,
is_weak_import,
segments);
value_delta = this->stream_->read_uleb128(current_offset);
segment_offset += std::get<0>(value_delta) + sizeof(pint_t);
current_offset += std::get<1>(value_delta);
break;
}
case BIND_OPCODE_DO_BIND_ADD_ADDR_IMM_SCALED:
{
this->do_bind<MACHO_T>(
BINDING_CLASS::BIND_CLASS_STANDARD,
type,
segment_idx,
segment_offset,
symbol_name,
library_ordinal,
addend,
is_weak_import,
segments);
segment_offset += imm * sizeof(pint_t) + sizeof(pint_t);
break;
}
case BIND_OPCODE_DO_BIND_ULEB_TIMES_SKIPPING_ULEB:
{
// Count
value_delta = this->stream_->read_uleb128(current_offset);
count = std::get<0>(value_delta);
current_offset += std::get<1>(value_delta);