-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbira.cpp
1004 lines (876 loc) · 37.2 KB
/
bira.cpp
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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <vector>
#include <string>
#include "codec.h"
#include "utils/cxxopts.hpp"
#include <LIEF/ELF.hpp>
#include <LIEF/enums.hpp>
#include "BPatch.h"
#include "Symtab.h"
#include "Function.h"
#include "Variable.h"
#include "Module.h"
#include "BPatch_binaryEdit.h"
#include "BPatch_addressSpace.h"
#include "BPatch_function.h"
#include "BPatch_image.h"
#include "BPatch_point.h"
#include "BPatch_process.h"
#include "BPatch_thread.h"
#include "BPatch_type.h"
using namespace LIEF::ELF;
using namespace std;
using namespace Dyninst;
// to-do: replace LIEF with dyninst.
vector<instr_t*>
decode(LIEF::span<const unsigned char> &text_content, uint file_offset, vector<uint> &ins_code_list)
{
vector<instr_t*> decode_list;
for(int i = 0; i < text_content.size(); i+=4) {
instr_t *ins = instr_create();
instr_init(ins);
byte* pc = &text_content[i];
decode_common(pc, file_offset + i, ins);
if(ins->opcode == OP_udf) {
printf("udf: %u\n", *(uint*)pc);
}
/* can do some modify on ins!*/
ins_code_list.push_back(*(uint*)pc);
decode_list.push_back(ins);
}
return decode_list;
}
void
encode(vector<instr_t*> &instr_list, uint file_offset, vector<uint> &ins_code_list) {
int count = 0;
for(auto ins : instr_list) {
byte* pc = ins_code_list[count];
decode_info_t di;
uint tmp = encode_common(count*4 + file_offset, ins, &di);
if(tmp == 0xffffffff) {
printf("opcode = %d, udf: %u\n", ins->opcode, pc);
}
ins_code_list[count] = tmp;
count ++;
}
}
vector<uint8_t>
getByteslist(vector<instr_t*> &instr_list, vector<uint> &ins_code_list)
{
vector<uint8_t> data;
int count = 0;
for(auto i : instr_list) {
byte* pc = (byte*)&ins_code_list[count];
data.push_back(*pc);
data.push_back(*(pc+1));
data.push_back(*(pc+2));
data.push_back(*(pc+3));
count ++;
}
return data;
}
static Address base, entry_func, exit_func;
uint8_t params_num;
bool trace_params;
vector<vector<reg_id_t>> entry_exit_used_regs;
reg_id_t DR_REG_ST1 = DR_REG_X18;
void
save_regs(vector<instr_t*> &ins_list, vector<uint> &ins_code_list, vector<reg_id_t> ®s)
{
for(int i = 0; i < regs.size() - 1; i+=2) {
// stp reg0, reg1, [sp, #-0x10]!
// printf("%d, %d\n", regs[i], regs[i+1]);
instr_t * stp1_ins = INSTR_CREATE_stp(opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, -16, OPSZ_16), opnd_create_reg(regs[i]), opnd_create_reg(regs[i+1]));
ins_list.push_back(stp1_ins);
ins_code_list.push_back(0);
// sub sp sp 16
instr_t* sub1_ins = INSTR_CREATE_sub(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(16));
ins_list.push_back(sub1_ins);
ins_code_list.push_back(0);
}
}
void
restore_regs(vector<instr_t*> &ins_list, vector<uint> &ins_code_list, vector<reg_id_t> ®s)
{
for(int i = regs.size() - 1; i >=0 ; i-=2) {
// ldp reg1, reg0, [sp]
instr_t * ldp4_ins = INSTR_CREATE_ldp(opnd_create_reg(regs[i-1]), opnd_create_reg(regs[i]), opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, 0, OPSZ_16));
ins_list.push_back(ldp4_ins);
ins_code_list.push_back(0);
// add sp sp 16
instr_t* add4_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(16));
ins_list.push_back(add4_ins);
ins_code_list.push_back(0);
}
}
// save NZCV FPCR FPSR
void
save_aflgs(vector<uint> &ins_code_list, vector<instr_t*> &ins_list, int stack_off)
{
instr_t * mrs_ins1 = INSTR_CREATE_mrs(opnd_create_reg(DR_REG_ST1), opnd_create_reg(DR_REG_NZCV));
ins_list.push_back(mrs_ins1);
ins_code_list.push_back(0);
instr_t * mrs_ins2 = INSTR_CREATE_mrs(opnd_create_reg(DR_REG_X30), opnd_create_reg(DR_REG_FPCR));
ins_list.push_back(mrs_ins2);
ins_code_list.push_back(0);
instr_t * stp_aflgs_ins = INSTR_CREATE_stp(opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, stack_off, OPSZ_16), opnd_create_reg(DR_REG_ST1), opnd_create_reg(DR_REG_X30));
ins_list.push_back(stp_aflgs_ins);
ins_code_list.push_back(0);
instr_t * mrs_ins3 = INSTR_CREATE_mrs(opnd_create_reg(DR_REG_ST1), opnd_create_reg(DR_REG_FPSR));
ins_list.push_back(mrs_ins3);
ins_code_list.push_back(0);
instr_t * stp_aflgs_ins2 = INSTR_CREATE_stp(opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, stack_off - 16, OPSZ_16), opnd_create_reg(DR_REG_ST1), opnd_create_reg(DR_REG_X30));
ins_list.push_back(stp_aflgs_ins2);
ins_code_list.push_back(0);
}
// restore NZCV FPCR FPSR
void
restore_aflgs(vector<uint> &ins_code_list, vector<instr_t*> &ins_list)
{
// add sp here
instr_t* add_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(32));
ins_list.push_back(add_ins);
ins_code_list.push_back(0);
instr_t * ldp_aflgs_ins2 = INSTR_CREATE_ldp(opnd_create_reg(DR_REG_ST1), opnd_create_reg(DR_REG_X30), opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0,-32, OPSZ_16));
ins_list.push_back(ldp_aflgs_ins2);
ins_code_list.push_back(0);
instr_t * msr_ins3 = INSTR_CREATE_msr(opnd_create_reg(DR_REG_FPSR), opnd_create_reg(DR_REG_ST1));
ins_list.push_back(msr_ins3);
ins_code_list.push_back(0);
instr_t * ldp_aflgs_ins = INSTR_CREATE_ldp(opnd_create_reg(DR_REG_X30), opnd_create_reg(DR_REG_X30), opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, -16, OPSZ_16));
ins_list.push_back(ldp_aflgs_ins);
ins_code_list.push_back(0);
instr_t * msr_ins2 = INSTR_CREATE_msr(opnd_create_reg(DR_REG_FPCR), opnd_create_reg(DR_REG_X30));
ins_list.push_back(msr_ins2);
ins_code_list.push_back(0);
instr_t * msr_ins1 = INSTR_CREATE_msr(opnd_create_reg(DR_REG_NZCV), opnd_create_reg(DR_REG_ST1));
ins_list.push_back(msr_ins1);
ins_code_list.push_back(0);
}
void
restore_replaced_ins(vector<instr_t *> &replaced_instrs, vector<uint> &ins_code_list, vector<instr_t*> &ins_list, bool isBlr)
{
// modify sp?
int i = (isBlr) ? 1 : 0;
for(; i < replaced_instrs.size(); i++) {
auto ins = replaced_instrs[i];
int opcode = instr_get_opcode(ins);
if(opcode == OP_bl) {
// to-do use adrp and blr to replace
uint target = opnd_get_pc(instr_get_target(ins));
instr_t * adrp_ins2 = INSTR_CREATE_adrp(opnd_create_reg(DR_REG_X30), OPND_CREATE_ABSMEM((void *)((target >> 12) << 12), OPSZ_0));
ins_list.push_back(adrp_ins2);
ins_code_list.push_back(0);
instr_t* add_off_ins2 = INSTR_CREATE_add(opnd_create_reg(DR_REG_X30), opnd_create_reg(DR_REG_X30), OPND_CREATE_INT32((target << 20) >> 20));
ins_list.push_back(add_off_ins2);
ins_code_list.push_back(0);
instr_t * blr_ins2 = INSTR_CREATE_blr(opnd_create_reg(DR_REG_X30));
ins_list.push_back(blr_ins2);
ins_code_list.push_back(0);
} else {
// if the third ins is cmp, maybe ldp and add will change arithmetic flags?
ins_list.push_back(ins);
ins_code_list.push_back(0);
}
}
}
// add blr support(blr flg)
// compare the blr reg with func_addr(adrp add to a reg)
// if match, call hook blr hook
// else blr and ret
//
// adrp
// add
// cmp reg1 reg2
// if equal continue hook
// else blr and ret
void
handleBlr(vector<instr_t *> &instrs, uint func_addr, int bOffset, vector<uint> &ins_code_list, vector<instr_t*> &ins_list, bool replace_before)
{
instr_t * adrp_ins = INSTR_CREATE_adrp(opnd_create_reg(DR_REG_X30), OPND_CREATE_ABSMEM((void *)((func_addr >> 12) << 12), OPSZ_0));
ins_list.push_back(adrp_ins);
ins_code_list.push_back(0);
instr_t* add_off_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_X30), opnd_create_reg(DR_REG_X30), OPND_CREATE_INT32(func_addr & 0xfff));
ins_list.push_back(add_off_ins);
ins_code_list.push_back(0);
instr_t* cmp_ins = INSTR_CREATE_cmp(opnd_create_reg(DR_REG_X30), opnd_create_reg(opnd_get_reg(instr_get_src(instrs[0], 0))));
ins_list.push_back(cmp_ins);
ins_code_list.push_back(0);
// if eq, jump to continue after ret
app_pc pc = replace_before ? bOffset+8*4:bOffset+(8 + instrs.size())*4;
instr_t* beq_ins = XINST_CREATE_jump_cond(DR_PRED_EQ, opnd_create_pc(pc));
ins_list.push_back(beq_ins);
ins_code_list.push_back(0);
// call func
ins_list.push_back(instrs[0]);
ins_code_list.push_back(0);
// aflgs attention!
// add sp sp 16
instr_t* add_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(16));
ins_list.push_back(add_ins);
ins_code_list.push_back(0);
// ldp x29, x30, [sp, #0x10]
instr_t * ldp_ins = INSTR_CREATE_ldp(opnd_create_reg(DR_REG_ST1), opnd_create_reg(DR_REG_X30), opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, -16, OPSZ_16));
ins_list.push_back(ldp_ins);
ins_code_list.push_back(0);
// restore the replace ins.
if(!replace_before) {
restore_replaced_ins(instrs, ins_code_list, ins_list, true);
}
// ret
instr_t* ret_ins = INSTR_CREATE_ret(opnd_create_reg(DR_REG_X30));
ins_list.push_back(ret_ins);
ins_code_list.push_back(0);
}
static uint special_offset;
SymtabAPI::Region *special_section;
void
call_external_func(vector<uint> &ins_code_list, vector<instr_t*> &ins_list, uint external_func_addr)
{
// ldr hook addr and blr hook
instr_t * hook_adrp_ins = INSTR_CREATE_adrp(opnd_create_reg(DR_REG_X30), OPND_CREATE_ABSMEM((void *)((external_func_addr >> 12) << 12), OPSZ_0));
ins_list.push_back(hook_adrp_ins);
ins_code_list.push_back(0);
instr_t* hook_add_off_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_X30), opnd_create_reg(DR_REG_X30), OPND_CREATE_INT32(external_func_addr & 0xfff));
ins_list.push_back(hook_add_off_ins);
ins_code_list.push_back(0);
instr_t* ldr_ins = INSTR_CREATE_ldr(opnd_create_reg(DR_REG_X30),
opnd_create_base_disp_aarch64(DR_REG_X30, DR_REG_NULL, 0, false,
0, 0, OPSZ_8));
ins_list.push_back(ldr_ins);
ins_code_list.push_back(0);
instr_t * hook_blr_ins = INSTR_CREATE_blr(opnd_create_reg(DR_REG_X30));
ins_list.push_back(hook_blr_ins);
ins_code_list.push_back(0);
}
// TODO:push_params_below_save_regs
void
push_params_below_save_regs(vector<uint> &ins_code_list, vector<instr_t*> &ins_list, int stack_params_num, int save_regs_off)
{
if(stack_params_num < 0) return;
uint old_stack_off = stack_params_num * 16 + save_regs_off;
for(int i = 0; i < stack_params_num; i++) {
int count = (i + 1) * -16;
instr_t * ldp_ins = INSTR_CREATE_ldp(opnd_create_reg(DR_REG_X9), opnd_create_reg(DR_REG_X10), opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, old_stack_off + count, OPSZ_16));
ins_list.push_back(ldp_ins);
ins_code_list.push_back(0);
instr_t * stp_ins = INSTR_CREATE_stp(opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, count, OPSZ_16), opnd_create_reg(DR_REG_X9), opnd_create_reg(DR_REG_X10));
ins_list.push_back(stp_ins);
ins_code_list.push_back(0);
}
}
// TODO
// x0: lr, x1: params_num, x2: x0, ... x7: x5
void
mov_params_for_trace(vector<uint> &ins_code_list, vector<instr_t*> &ins_list)
{
if(params_num >= 7) {
instr_t * stp_x6_x7_ins = INSTR_CREATE_stp(opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, -16, OPSZ_16), opnd_create_reg(DR_REG_X6), opnd_create_reg(DR_REG_X7));
ins_list.push_back(stp_x6_x7_ins);
ins_code_list.push_back(0);
instr_t* sub_ins1 = INSTR_CREATE_sub(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(16));
ins_list.push_back(sub_ins1);
ins_code_list.push_back(0);
}
if(params_num >= 6) {
instr_t * mov_x7_x5 = INSTR_CREATE_add(opnd_create_reg(DR_REG_X7), opnd_create_reg(DR_REG_X5), OPND_CREATE_INT32(0));
ins_list.push_back(mov_x7_x5);
ins_code_list.push_back(0);
}
if(params_num >= 5) {
instr_t * mov_x6_x4 = INSTR_CREATE_add(opnd_create_reg(DR_REG_X6), opnd_create_reg(DR_REG_X4), OPND_CREATE_INT32(0));
ins_list.push_back(mov_x6_x4);
ins_code_list.push_back(0);
}
if(params_num >= 4) {
instr_t * mov_x5_x3 = INSTR_CREATE_add(opnd_create_reg(DR_REG_X5), opnd_create_reg(DR_REG_X3), OPND_CREATE_INT32(0));
ins_list.push_back(mov_x5_x3);
ins_code_list.push_back(0);
}
if(params_num >= 3) {
instr_t * mov_x4_x2 = INSTR_CREATE_add(opnd_create_reg(DR_REG_X4), opnd_create_reg(DR_REG_X2), OPND_CREATE_INT32(0));
ins_list.push_back(mov_x4_x2);
ins_code_list.push_back(0);
}
if(params_num >= 2) {
instr_t * mov_x3_x1 = INSTR_CREATE_add(opnd_create_reg(DR_REG_X3), opnd_create_reg(DR_REG_X1), OPND_CREATE_INT32(0));
ins_list.push_back(mov_x3_x1);
ins_code_list.push_back(0);
}
if(params_num >= 1) {
instr_t * mov_x2_x0 = INSTR_CREATE_add(opnd_create_reg(DR_REG_X2), opnd_create_reg(DR_REG_X0), OPND_CREATE_INT32(0));
ins_list.push_back(mov_x2_x0);
ins_code_list.push_back(0);
}
instr_t * mov_x1_params_num = INSTR_CREATE_movz(opnd_create_reg(DR_REG_X1), OPND_CREATE_INT8(params_num), OPND_CREATE_INT32(0));
ins_list.push_back(mov_x1_params_num);
ins_code_list.push_back(0);
}
bool
restore_replaced_ins_before(int stack_params_num, vector<instr_t *> &replaced_instrs, vector<uint> &ins_code_list, vector<instr_t*> &ins_list, bool isBlr)
{
bool use_sp = false;
for(auto i : replaced_instrs) {
if(i->opcode == OP_str || i->opcode == OP_stp) {
int op_num = instr_num_dsts(i);
// printf("opcode: %d\n", i->opcode);
for(int j = 0; j < op_num; j++) {
// printf("x3 == %d, sp == %d, opnd_get_reg(opnd): %d, op kind: %d\n", DR_REG_X3, DR_REG_XSP, opnd_get_reg(instr_get_dst(i, j)), instr_get_dst(i, j).kind);
if(opnd_uses_reg(instr_get_dst(i, j), DR_REG_XSP)) {
// printf("opcode: %d\n", i->opcode);
use_sp = true;
break;
}
}
} else {
int op_num = instr_num_srcs(i);
for(int j = 0; j < op_num; j++) {
if(opnd_uses_reg(instr_get_src(i, j), DR_REG_XSP)) {
use_sp = true;
break;
}
}
}
if(use_sp) break;
}
// if replaced ins == OP_bl, we must mov sp first.
if(!use_sp) {
// sub sp sp 16
int stack_sub_off = 16;
if(params_num - 8 > 0) stack_sub_off += (stack_params_num << 4);
instr_t* sub_aflg_ins1 = INSTR_CREATE_sub(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(stack_sub_off));
ins_list.push_back(sub_aflg_ins1);
ins_code_list.push_back(0);
}
restore_replaced_ins(replaced_instrs, ins_code_list, ins_list, isBlr);
return use_sp;
}
// append new ins into .special
// if isBlr, instrs[0] == blr
vector<uint8_t>
appendCode(vector<instr_t *> &instrs, uint func_addr, bool newSection, bool isBlr, bool replace_before) {
uint file_offset;
if(newSection)
file_offset = base;
else
file_offset = special_section->getDiskOffset() + special_offset;
// file_offset = special_section->getDiskOffset();
vector<uint> ins_code_list;
vector<instr_t*> ins_list;
int stack_params_num = (params_num - 8) / 2 + (params_num - 8) % 2;
if(replace_before)
restore_replaced_ins(instrs, ins_code_list, ins_list, isBlr);
// stp x29, x30, [sp, #-0x10]!
instr_t * stp_ins1 = INSTR_CREATE_stp(opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, -16, OPSZ_16), opnd_create_reg(DR_REG_X29), opnd_create_reg(DR_REG_X30));
ins_list.push_back(stp_ins1);
ins_code_list.push_back(0);
instr_t* sub_ins1 = INSTR_CREATE_sub(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(16));
ins_list.push_back(sub_ins1);
ins_code_list.push_back(0);
instr_t * mov_x29_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_X29), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(0));
ins_list.push_back(mov_x29_ins);
ins_code_list.push_back(0);
if(isBlr) {
// use ins_list size to cal ins nums and update the offset!!!
// restore the sp after move params!!!
if(newSection)
handleBlr(instrs, func_addr, file_offset + 4 * ins_list.size(), ins_code_list, ins_list, replace_before);
else
handleBlr(instrs, func_addr, file_offset + 4 * ins_list.size(), ins_code_list, ins_list, replace_before);
}
save_regs(ins_list, ins_code_list, entry_exit_used_regs[0]);
if(trace_params) {
push_params_below_save_regs(ins_code_list, ins_list, stack_params_num, entry_exit_used_regs[0].size() * 8 + 16/*save general and x29, x30*/);
if(stack_params_num > 0) {
instr_t* sub_params_ins = INSTR_CREATE_sub(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(stack_params_num*16));
ins_list.push_back(sub_params_ins);
ins_code_list.push_back(0);
}
mov_params_for_trace(ins_code_list, ins_list);
}
instr_t * save_x30_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_X0), opnd_create_reg(DR_REG_X30), OPND_CREATE_INT32(0));
ins_list.push_back(save_x30_ins);
ins_code_list.push_back(0);
call_external_func(ins_code_list, ins_list, entry_func);
if(trace_params) {
if(stack_params_num > 0) {
instr_t* add_params_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32((stack_params_num+1)*16));
ins_list.push_back(add_params_ins);
ins_code_list.push_back(0);
} else if(params_num >= 7) {
instr_t* add_params_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(16));
ins_list.push_back(add_params_ins);
ins_code_list.push_back(0);
}
}
restore_regs(ins_list, ins_code_list, entry_exit_used_regs[0]);
instr_t * ldp_ins1 = INSTR_CREATE_ldp(opnd_create_reg(DR_REG_X29), opnd_create_reg(DR_REG_X30), opnd_create_base_disp(DR_REG_XSP, DR_REG_NULL, 0, 0, OPSZ_16));
ins_list.push_back(ldp_ins1);
ins_code_list.push_back(0);
instr_t* add_ins1 = INSTR_CREATE_add(opnd_create_reg(DR_REG_XSP), opnd_create_reg(DR_REG_XSP), OPND_CREATE_INT32(16));
ins_list.push_back(add_ins1);
ins_code_list.push_back(0);
if(isBlr) {
ins_list.push_back(instrs[0]);
ins_code_list.push_back(0);
} else {
// blr func
instr_t * adrp_ins = INSTR_CREATE_adrp(opnd_create_reg(DR_REG_X30), OPND_CREATE_ABSMEM((void *)((func_addr >> 12) << 12), OPSZ_0));
ins_list.push_back(adrp_ins);
ins_code_list.push_back(0);
instr_t* add_off_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_X30), opnd_create_reg(DR_REG_X30), OPND_CREATE_INT32((func_addr << 20) >> 20));
ins_list.push_back(add_off_ins);
ins_code_list.push_back(0);
instr_t * blr_ins = INSTR_CREATE_blr(opnd_create_reg(DR_REG_X30));
ins_list.push_back(blr_ins);
ins_code_list.push_back(0);
}
save_regs(ins_list, ins_code_list, entry_exit_used_regs[1]);
call_external_func(ins_code_list, ins_list, exit_func);
instr_t * restore_x30_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_X30), opnd_create_reg(DR_REG_X0), OPND_CREATE_INT32(0));
ins_list.push_back(restore_x30_ins);
ins_code_list.push_back(0);
restore_regs(ins_list, ins_code_list, entry_exit_used_regs[1]);
if(!replace_before) {
restore_replaced_ins(instrs, ins_code_list, ins_list, isBlr);
}
// ret
instr_t* ret_ins = INSTR_CREATE_ret(opnd_create_reg(DR_REG_X30));
ins_list.push_back(ret_ins);
ins_code_list.push_back(0);
encode(ins_list, file_offset, ins_code_list);
vector<uint8_t> data = getByteslist(ins_list, ins_code_list);
return data;
}
void
encode_section(SymtabAPI::Symtab *symTab, uint &wrapper_addr, vector<instr_t *> &instrs, uint func_addr, std::string &secName, bool newSection, bool isBlr, bool replaced_before)
{
// save context (i.e. lr)
// get page offset in lr: and lr lr 0xfff
// subs lr lr (pc_offset & 0xfff)
// b.ne XINST_CREATE_jump_cond(DR_PRED_NE, opnd_create_pc(ret addr))
// blr (to .special accordingly)
// ret
vector<uint8_t> data;
if(newSection) {
// instrs.size() == 1, we need to replace 2 ins at all.
// add blr flg
unsigned char empty[] = {};
int section_size = 1 << 12;// + regs.size() * 16;// + regs.size() * 32; // is BLR + x!!
symTab->addRegion(base, (void*)(empty), section_size, secName.c_str(), SymtabAPI::Region::RT_TEXT, true);
SymtabAPI::Region *new_section;
if(!symTab->findRegion(new_section, secName.c_str()))
cout << "findRegion err" << endl;
wrapper_addr = new_section->getMemOffset();
// data = encode_new_section(instrs, func_addr, isBlr, replaced_before);
data = appendCode(instrs, func_addr, newSection, isBlr, replaced_before);
base += section_size;
unsigned char* rawData = new unsigned char[data.size()];
for(int i = 0; i < data.size(); i++)
rawData[i] = data[i];
if(!new_section->setPtrToRawData((void*)rawData, data.size()))
cout << "setPtrToRawData err!" << endl;
} else {
// instrs.size() == 2, we need to replace 3 ins at all.
// no jumptable needed, just using adrp, add off, blr
// pc_offset == i+2 th ins offset
// (i+2)*4 + text_offset?
wrapper_addr = special_section->getDiskOffset() + special_offset;
// data = appendCode(func_addr, instrs, isBlr, replaced_before);
data = appendCode(instrs, func_addr, newSection, isBlr, replaced_before);
uint special_code_size = data.size();
unsigned char* rawData = new unsigned char[data.size()];
for(int i = 0; i < data.size(); i++)
rawData[i] = data[i];
if(!special_section->patchData(special_offset, (void*)rawData, special_code_size))
cout << "special_section patchData err!" << endl;
special_offset += special_code_size;
}
// printf("wrapper_addr = %llx\n", wrapper_addr);
}
vector<instr_t*>
decode(vector<uint8_t> &text_content, uint file_offset)
{
vector<instr_t*> decode_list;
for(int i = 0; i < text_content.size(); i+=4) {
instr_t *ins = instr_create();
instr_init(ins);
byte* pc = &text_content[i];
decode_common(pc, file_offset + i, ins);
decode_list.push_back(ins);
}
return decode_list;
}
vector<reg_id_t>
getEntryExitFuncUsedRegs(std::unique_ptr<const Binary> &lib, string &trace_func_name)
{
Symbol * trace_func = lib->get_symbol(trace_func_name);
// printf("trace_func va = %d, size = %d\n", trace_func->value(), trace_func->size());
vector<uint8_t> trace_func_content = lib->get_content_from_virtual_address(trace_func->value(), trace_func->size());
vector<uint> ins_code_list;
vector<instr_t*> decode_list = decode(trace_func_content, 0);
set<reg_id_t> reg_set;
for(int i = 0; i < decode_list.size(); i++) {
int dst_num = instr_num_dsts(decode_list[i]);
for(int j = 0; j < dst_num; j++) {
// reg_get_size
if(!opnd_is_reg(instr_get_dst(decode_list[i], j))) continue;
reg_id_t reg = opnd_get_reg(instr_get_dst(decode_list[i], j));
if(reg >= DR_REG_W0 && reg < DR_REG_W29) reg -= (DR_REG_W0 - DR_REG_X0);
if(reg >= DR_REG_X0 && reg < DR_REG_ST1) reg_set.insert(reg);
}
}
reg_set.insert(DR_REG_X0);
reg_set.insert(DR_REG_X1);
reg_set.insert(DR_REG_X2);
reg_set.insert(DR_REG_X3);
reg_set.insert(DR_REG_X4);
reg_set.insert(DR_REG_X5);
reg_set.insert(DR_REG_X6);
reg_set.insert(DR_REG_X7);
reg_set.insert(DR_REG_X8);
reg_set.insert(DR_REG_X9);
reg_set.insert(DR_REG_X10);
vector<reg_id_t> regs;
for(auto reg : reg_set) regs.push_back(reg);
if(regs.size() % 2 != 0) regs.push_back(DR_REG_X29);
return regs;
}
bool
check_opcode(int opcode)
{
switch(opcode) {
case OP_bcond:
case OP_cbnz:
case OP_tbnz:
case OP_cbz:
case OP_tbz:
case OP_b:
case OP_bl:
case OP_ret:
#ifdef KYLIN
case OP_adrp:
#endif
return true;
default: return false;
}
return false;
}
// stp x29, x30, [sp, #16]!
// can not replace it
bool check_stp_x30(instr_t * ins)
{
if(ins->opcode != OP_stp) return false;
for(int j = 0; j < 2; j++) {
if(opnd_uses_reg(instr_get_src(ins, j), DR_REG_X30)) {
return true;
}
}
return false;
}
void
modify_text_dyn(std::unique_ptr<const Binary> &binary, uint func_addr, SymtabAPI::Symtab *symTab)
{
LIEF::ELF::Section* section = binary->text_section();
LIEF::span<const unsigned char> text_content = section->content();
uint file_offset = section->file_offset();
cout << ".text offset = " << file_offset << endl;
vector<uint> ins_code_list;
vector<instr_t*> decode_list = decode(text_content, file_offset, ins_code_list);
set<uint> branch_addrs;
int count = 0;
for(int i = 0; i < decode_list.size(); i++) {
if(check_opcode(decode_list[i]->opcode)) {
branch_addrs.emplace(opnd_get_pc(instr_get_target(decode_list[i])));
}
}
for(int i = 0; i < decode_list.size(); i++) {
bool isBlr = false;
// if(decode_list[i]->opcode == OP_blr) {
// isBlr = true;
// }
#ifndef KYLIN
if((isBlr || (decode_list[i]->opcode == OP_bl && opnd_get_pc(instr_get_target(decode_list[i])) == func_addr))) {
#else
if((isBlr || (decode_list[i]->opcode == OP_bl && opnd_get_pc(instr_get_target(decode_list[i])) == func_addr - 0x400000))) {
#endif
bool can_replace_before = (branch_addrs.find(file_offset + (i)*4) == branch_addrs.end());
bool can_replace_after = (branch_addrs.find(file_offset + (i+1)*4) == branch_addrs.end());
if(!can_replace_before && !can_replace_after) {
printf("skip1!\n");
continue;
}
printf("Got it!\n");
uint wrapper_addr;
vector<instr_t *> instrs;
if(isBlr) instrs.push_back(decode_list[i]);
uint opcode_ins_3 = instr_get_opcode(decode_list[i-3]);
uint opcode_ins_2 = instr_get_opcode(decode_list[i-2]);
uint opcode_ins_1 = instr_get_opcode(decode_list[i-1]);
reg_id_t DR_REG_BR = DR_REG_X30;
instr_t * blr_ins = INSTR_CREATE_blr(opnd_create_reg(DR_REG_X30));
// instr_t * blr_ins = INSTR_CREATE_br(opnd_create_reg(DR_REG_BR));
// try to replace ins after bl
if(check_opcode(opcode_ins_1) || !can_replace_before) {
uint opcode_ins1 = instr_get_opcode(decode_list[i+1]);
uint opcode_ins2 = instr_get_opcode(decode_list[i+2]);
if(check_opcode(opcode_ins1) || !can_replace_after) {
printf("skip2!\n");
continue;
}
instrs.push_back(decode_list[i+1]);
#ifndef KYLIN
if((opcode_ins2 != OP_b && check_opcode(opcode_ins2)) || branch_addrs.find(file_offset + (i+2)*4) != branch_addrs.end()) {
std::string secName = ".mysection" + std::to_string(i);
encode_section(symTab, wrapper_addr, instrs, func_addr, secName, true, isBlr, false);
instr_t * adrp_ins = INSTR_CREATE_adrp(opnd_create_reg(DR_REG_BR), OPND_CREATE_ABSMEM((void *)((wrapper_addr >> 12) << 12), OPSZ_0));
decode_list[i] = adrp_ins;
decode_list[i+1] = blr_ins;
i++;
#else
if(check_opcode(opcode_ins2) || branch_addrs.find(file_offset + (i+2)*4) != branch_addrs.end()) {
continue;
#endif
} else {
instrs.push_back(decode_list[i+2]);
std::string secName = ".special";
encode_section(symTab, wrapper_addr, instrs, func_addr, secName, false, isBlr, false);
#ifndef KYLIN
instr_t * adrp_ins = INSTR_CREATE_adrp(opnd_create_reg(DR_REG_BR), OPND_CREATE_ABSMEM((void *)((wrapper_addr >> 12) << 12), OPSZ_0));
instr_t* add_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_BR), opnd_create_reg(DR_REG_BR), OPND_CREATE_INT32((wrapper_addr << 20) >> 20));
decode_list[i] = adrp_ins;
decode_list[i+1] = add_ins;
#else
instr_t * movz_ins = INSTR_CREATE_movz(opnd_create_reg(DR_REG_X30), OPND_CREATE_INT16(wrapper_addr & 0xffff), OPND_CREATE_INT32(0));
instr_t * movk_ins = INSTR_CREATE_movk(opnd_create_reg(DR_REG_X30), OPND_CREATE_INT16((wrapper_addr >> 16) & 0xffff), OPND_CREATE_INT32(16));
decode_list[i] = movz_ins;
decode_list[i+1] = movk_ins;
#endif
decode_list[i+2] = blr_ins;
i+=2;
}
continue;
}
if(check_stp_x30(decode_list[i-2]) || check_opcode(opcode_ins_2) || branch_addrs.find(file_offset + (i-1)*4) != branch_addrs.end()) {
#ifndef KYLIN
instrs.push_back(decode_list[i-1]);
std::string secName = ".mysection" + std::to_string(i);
encode_section(symTab, wrapper_addr, instrs, func_addr, secName, true, isBlr, true);
instr_t * adrp_ins = INSTR_CREATE_adrp(opnd_create_reg(DR_REG_BR), OPND_CREATE_ABSMEM((void *)((wrapper_addr >> 12) << 12), OPSZ_0));
decode_list[i-1] = adrp_ins;
decode_list[i] = blr_ins;
#else
continue;
#endif
} else {
instrs.push_back(decode_list[i-2]);
instrs.push_back(decode_list[i-1]);
std::string secName = ".special";
encode_section(symTab, wrapper_addr, instrs, func_addr, secName, false, isBlr, true);
#ifndef KYLIN
instr_t * adrp_ins = INSTR_CREATE_adrp(opnd_create_reg(DR_REG_BR), OPND_CREATE_ABSMEM((void *)((wrapper_addr >> 12) << 12), OPSZ_0));
instr_t* add_ins = INSTR_CREATE_add(opnd_create_reg(DR_REG_BR), opnd_create_reg(DR_REG_BR), OPND_CREATE_INT32((wrapper_addr << 20) >> 20));
decode_list[i-2] = adrp_ins;
decode_list[i-1] = add_ins;
#else
instr_t * movz_ins = INSTR_CREATE_movz(opnd_create_reg(DR_REG_X30), OPND_CREATE_INT16(wrapper_addr & 0xffff), OPND_CREATE_INT32(0));
instr_t * movk_ins = INSTR_CREATE_movk(opnd_create_reg(DR_REG_X30), OPND_CREATE_INT16((wrapper_addr >> 16) & 0xffff), OPND_CREATE_INT32(16));
decode_list[i-2] = movz_ins;
decode_list[i-1] = movk_ins;
#endif
decode_list[i] = blr_ins;
}
}
count ++;
}
encode(decode_list, file_offset, ins_code_list);
SymtabAPI::Region *text_section;
if(!symTab->findRegion(text_section, ".text"))
cout << "findRegion .text err" << endl;
vector<uint8_t> data = getByteslist(decode_list, ins_code_list);
unsigned char* rawData = new unsigned char[data.size()];
for(int i = 0; i < data.size(); i++)
rawData[i] = data[i];
if(!text_section->setPtrToRawData((void*)rawData, data.size()))
cout << ".text setPtrToRawData err!" << endl;
}
void addSpecialSectionPages(SymtabAPI::Symtab *symTab) {
static unsigned char empty[] = {};
int pageSize = 1 << 12;
base = symTab->getFreeOffset(50*1024*1024);
base += (1024*1024);
base -= (base & (1024*1024-1));
symTab->addRegion(base, (void*)(empty), 48, ".extsym", SymtabAPI::Region::RT_TEXTDATA, true);
SymtabAPI::Region *extsym_section;
if(!symTab->findRegion(extsym_section, ".extsym"))
cout << "findRegion .extsym err" << endl;
base += 48;
symTab->addRegion(base, (void*)(empty), 4 * pageSize - 48, ".special", SymtabAPI::Region::RT_TEXT, true);
if(!symTab->findRegion(special_section, ".special"))
cout << "findRegion .special err" << endl;
base += 4 * pageSize - 48;
}
static int reloc_num = 0;
Address addExternalFuncSymbol(SymtabAPI::Symtab *symTab, SymtabAPI::Symtab *lib, string func_name) {
vector<SymtabAPI::Function*> ret;
if(!lib->findFunctionsByName(ret, move(func_name))) {
cout << "cannot find " << endl;
return false;
}
vector<SymtabAPI::Symbol *> syms;
if(!ret[0]->getSymbols(syms)) {
cout << "cannot find symbols that refer " << endl;
return false;
}
// try to find a dynamic symbol
// (take first static symbol if none are found)
SymtabAPI::Symbol *referring = syms[0];
for (unsigned k=0; k<syms.size(); k++) {
if (syms[k]->isInDynSymtab()) {
referring = syms[k];
break;
}
}
unsigned int jump_slot_size;
switch (symTab->getAddressWidth()) {
case 4: jump_slot_size = 4; break; // l: not needed
case 8:
jump_slot_size = 24;
break;
default: assert(0 && "Encountered unknown address width");
}
Address relocation_address = base - 4 * (1 << 12) + reloc_num * jump_slot_size;// add into .extsym
reloc_num++;
Dyninst::SymtabAPI::Region *extsym = NULL;
symTab->findRegion(extsym, ".extsym");
assert(extsym);
// Create the relocationEntry
Dyninst::SymtabAPI::relocationEntry localRel(relocation_address, referring->getMangledName(), referring,
Dyninst::SymtabAPI::relocationEntry::getGlobalRelType(symTab->getAddressWidth(), referring));
symTab->addExternalSymbolReference(referring, extsym, localRel);
return relocation_address;
}
std::string input_file;
std::string func_name;
std::string trace_lib;
// TODO: add -tp,--trace_params option to enable params tracing
void parse(int argc, char *argv[]) {
cxxopts::Options options("bira", "Binary rewriting tool for arm.");
options.add_options()
("f,func", "Target function name", cxxopts::value<std::string>())
("h,help", "Print help")
("i,input", "Input binary file", cxxopts::value<std::string>())
("l,link", "External shared library for linking",cxxopts::value<std::string>())
("tp", "Enable parameters tracing")
;
try {
auto result = options.parse(argc, argv);
// print help message if configured
if (result.count("help")) {
std::cout << options.help() << std::endl;
exit(1);
}
if (result.count("input")>=1) {
if(result.count("input")>2) {
std::cout << "Warning: multiple input binary file configured. Only use the last one!" << std::endl;
}
input_file = result["input"].as<std::string>();
} else {
std::cout << "-i or --input must be specified!" << std::endl;
exit(1);
}
if (result.count("func")>=1) {
if(result.count("func")>2) {
std::cout << "Warning: multiple function configured. Only use the last one!" << std::endl;
}
func_name = result["func"].as<std::string>();
} else {
std::cout << "-f or --func must be specified!" << std::endl;
exit(1);
}
if (result.count("link")>=1) {
if(result.count("link")>2) {
std::cout << "Warning: multiple library configured. Only use the last one!" << std::endl;
}
trace_lib = result["link"].as<std::string>();
} else {
std::cout << "-l or --link must be specified!" << std::endl;
exit(1);
}
std::cout << "Configured: " << std::endl;
std::cout << "\t" << "Input: " << input_file << std::endl;
std::cout << "\t" << "Function: " << func_name << std::endl;
std::cout << "\t" << "Trace Library: " << trace_lib << std::endl;
if (result.count("tp")>=1) {
trace_params = true;
} else {
trace_params = false;
}
} catch (cxxopts::exceptions::exception e) {
std::cout << "Error: " << e.what() << std::endl;
std::cout << options.help() << std::endl;
exit(1);
}
}
/* agrv[1] == <exe> argv[2] == <lib> argv[3] == <func>*/
int main(int argc, char** argv) {
parse(argc, argv);
std::unique_ptr<const Binary> binary{Parser::parse(input_file)};
std::unique_ptr<const Binary> lib{Parser::parse(trace_lib)};
SymtabAPI::Symtab *symTab, *libhook;
if(!SymtabAPI::Symtab::openFile(libhook, trace_lib)) {
cerr << "error: file cannot be parsed";
return -1;
}
if(!SymtabAPI::Symtab::openFile(symTab, input_file)) {
cerr << "error: file cannot be parsed";
return -1;
}
vector<SymtabAPI::Function*> target_func;
if(!symTab->findFunctionsByName(target_func, func_name)) {
cout << "cannot find " << func_name << endl;
return -1;
}
uint func_addr = target_func[0]->getOffset();
// printf("target_func offset: %llx\n", func_addr);
vector<SymtabAPI::localVar *> params;
if(!target_func[0]->getParams(params)) {
params_num = 0;
// cout << "cannot getParams for " << argv[3] << endl;
//return -1;
}
params_num = params.size();
printf("params_num: %d\n", params_num);
addSpecialSectionPages(symTab);
string entry_func_name("trace_entry_func");
entry_func = addExternalFuncSymbol(symTab, libhook, entry_func_name);
assert(entry_func);
// printf("entry_func = %llx\n", entry_func);
vector<reg_id_t> regs0 = getEntryExitFuncUsedRegs(lib, entry_func_name);
// printf("X0 = %u\n", DR_REG_X0);
// for(auto reg : regs0) printf("%u\n", reg);
string exit_func_name("trace_exit_func");
exit_func = addExternalFuncSymbol(symTab, libhook, exit_func_name);
assert(exit_func);
// printf("exit_func = %llx\n", exit_func);
vector<reg_id_t> regs1 = getEntryExitFuncUsedRegs(lib, exit_func_name);
// printf("X0 = %u\n", DR_REG_X0);
// for(auto reg : regs1) printf("%u\n", reg);
entry_exit_used_regs.push_back(regs0);
entry_exit_used_regs.push_back(regs1);
modify_text_dyn(binary, func_addr, symTab);
if(!symTab->addLibraryPrereq(trace_lib)) {
cerr << "error: add library";
return -1;
}
string outfile(input_file);
outfile += ".dyn";
if (!symTab->emit(outfile)) {
cout << "emit new binary error!\n";
return -1;