/
disasm.c
5957 lines (5720 loc) · 162 KB
/
disasm.c
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
/* radare - LGPL - Copyright 2009-2018 - nibble, pancake, dso */
#include "r_core.h"
#include "r_cons.h"
#define HASRETRY 1
#define HAVE_LOCALS 1
#define DEFAULT_NARGS 4
#define R_MIDFLAGS_SHOW 1
#define R_MIDFLAGS_REALIGN 2
#define R_MIDFLAGS_SYMALIGN 3
#define COLOR(ds, field) (ds->show_color ? ds->field : "")
#define COLOR_CONST(ds, color) (ds->show_color ? Color_ ## color : "")
#define COLOR_RESET(ds) COLOR_CONST(ds, RESET)
// ugly globals but meh
static ut64 emustack_min = 0LL;
static ut64 emustack_max = 0LL;
static const char* r_vline_a[] = {
"|", // LINE_VERT
"|-", // LINE_CROSS
"-", // LINE_HORIZ
":", // LINE_UP
",", // LUP_CORNER
"\\", // RDWN_CORNER
"/", // RUP_CORNER
"`", // LDWN_CORNER
"->", // ARROW_RIGHT
"=<", // ARROW_LEFT
};
static const char* r_vline_u[] = {
"│", // LINE_VERT
"├", // LINE_CROSS
"─", // LINE_HORIZ
"⁝", // LINE_UP
// "↑", // LINE_UP
//"┌", // LUP_CORNER
"┘", // LUP_CORNER
"└", // RDWN_CORNER
"┌", // RUP_CORNER
"┐", // LDWN_CORNER
">", // ARROW_RIGHT
"<", // ARROW_LEFT
};
static const char* r_vline_uc[] = {
"│", // LINE_VERT
"├", // LINE_CROSS
"─", // LINE_HORIZ
// "↑", // LINE_UP
"⋮", // LINE_UP
// "≀", // LINE_UP
//"┌", // LUP_CORNER
"╯", // LUP_CORNER
"╰", // RDWN_CORNER
"╭", // RUP_CORNER
"╮", // LDWN_CORNER
">", // ARROW_RIGHT
"<", // ARROW_LEFT
};
#define DS_PRE_NONE 0
#define DS_PRE_EMPTY 1
#define DS_PRE_FCN_HEAD 2
#define DS_PRE_FCN_MIDDLE 3
#define DS_PRE_FCN_TAIL 4
// TODO: what about using bit shifting and enum for keys? see libr/util/bitmap.c
// the problem of this is that the fields will be more opaque to bindings, but we will earn some bits
typedef struct {
RCore *core;
char str[1024], strsub[1024];
bool immtrim;
bool immstr;
bool use_esil;
bool show_color;
bool show_color_bytes;
int colorop;
int acase;
bool capitalize;
bool show_flgoff;
bool hasMidflag;
int atabs;
int atabsonce;
int atabsoff;
int decode;
bool pseudo;
int filter;
int interactive;
bool jmpsub;
bool varsub;
bool show_lines;
bool show_lines_bb;
bool show_lines_ret;
bool show_lines_call;
int linesright;
int tracespace;
int cyclespace;
int cmtfold;
int show_indent;
bool show_dwarf;
bool show_size;
bool show_trace;
bool show_family;
bool asm_describe;
int linesout;
int adistrick;
bool asm_meta;
int asm_demangle;
bool show_offset;
bool show_offdec; // dupe for r_print->flags
bool show_bbline;
bool show_emu;
bool pre_emu;
bool show_emu_str;
bool show_emu_stroff;
bool show_emu_strinv;
bool show_emu_strflag;
bool show_emu_stack;
bool show_emu_write;
bool show_section;
int show_section_col;
bool show_symbols;
int show_symbols_col;
bool show_offseg;
bool show_flags;
bool bblined;
bool show_bytes;
bool show_reloff;
bool show_reloff_flags;
bool show_comments;
bool show_jmphints;
bool show_leahints;
bool show_slow;
int cmtcol;
bool show_lines_fcn;
bool show_calls;
bool show_cmtflgrefs;
bool show_cycles;
bool show_stackptr;
int stackFd;
bool show_xrefs;
bool show_cmtrefs;
const char *show_cmtoff;
bool show_functions;
bool show_hints;
bool show_marks;
bool show_asciidot;
RStrEnc strenc;
int cursor;
int show_comment_right_default;
int flagspace_ports;
int show_flag_in_bytes;
int lbytes;
int show_comment_right;
int pre;
char *ocomment;
int linesopts;
int lastfail;
int ocols;
int lcols;
int nb, nbytes;
int show_utf8;
int lines;
int oplen;
bool show_varaccess;
bool show_vars;
int show_varsum;
int midflags;
bool midcursor;
bool show_noisy_comments;
ut64 asm_highlight;
const char *pal_comment;
const char *color_comment;
const char *color_usrcmt;
const char *color_fname;
const char *color_floc;
const char *color_fline;
const char *color_flow;
const char *color_flow2;
const char *color_flag;
const char *color_label;
const char *color_other;
const char *color_nop;
const char *color_bin;
const char *color_math;
const char *color_btext;
const char *color_jmp;
const char *color_cjmp;
const char *color_call;
const char *color_cmp;
const char *color_swi;
const char *color_trap;
const char *color_ret;
const char *color_push;
const char *color_pop;
const char *color_reg;
const char *color_num;
const char *color_mov;
const char *color_invalid;
const char *color_gui_cflow;
const char *color_gui_dataoffset;
const char *color_gui_background;
const char *color_gui_alt_background;
const char *color_gui_border;
const char *color_linehl;
RFlagItem *lastflag;
RAnalHint *hint;
RPrint *print;
ut64 esil_old_pc;
ut8* esil_regstate;
bool esil_likely;
int l;
int middle;
int indent_level;
int indent_space;
char *line;
char *refline, *refline2;
char *comment;
char *opstr;
char *osl, *sl;
int stackptr, ostackptr;
int index;
ut64 at, vat, addr, dest;
int tries, cbytes, idx;
char chref;
bool retry;
bool mi_found;
RAsmOp asmop;
RAnalOp analop;
RAnalFunction *fcn;
RAnalFunction *pdf;
const ut8 *buf;
int len;
int maxrefs;
int foldxrefs;
char *prev_ins;
bool prev_ins_eq;
int prev_ins_count;
bool show_nodup;
bool has_description;
// caches
char *_tabsbuf;
int _tabsoff;
bool dwarfFile;
bool dwarfAbspath;
bool showpayloads;
bool showrelocs;
int cmtcount;
int shortcut_pos;
bool asm_anal;
ut64 printed_str_addr;
ut64 printed_flag_addr;
ut64 min_ref_addr;
bool use_json;
bool first_line;
const char *strip;
} RDisasmState;
static void ds_setup_print_pre(RDisasmState *ds, bool tail, bool middle);
static void ds_setup_pre(RDisasmState *ds, bool tail, bool middle);
static void ds_print_pre(RDisasmState *ds);
static void ds_beginline(RDisasmState *ds);
static void ds_begin_json_line(RDisasmState *ds);
static void ds_newline(RDisasmState *ds);
static void ds_print_esil_anal(RDisasmState *ds);
static void ds_reflines_init(RDisasmState *ds);
static void ds_align_comment(RDisasmState *ds);
static RDisasmState * ds_init(RCore * core);
static void ds_build_op_str(RDisasmState *ds, bool print_color);
static void ds_pre_xrefs(RDisasmState *ds, bool no_fcnlines);
static void ds_show_xrefs(RDisasmState *ds);
static void ds_atabs_option(RDisasmState *ds);
static void ds_show_functions(RDisasmState *ds);
static void ds_show_comments_right(RDisasmState *ds);
static void ds_show_flags(RDisasmState *ds);
static void ds_update_ref_lines(RDisasmState *ds);
static int ds_disassemble(RDisasmState *ds, ut8 *buf, int len);
static void ds_control_flow_comments(RDisasmState *ds);
static void ds_print_lines_right(RDisasmState *ds);
static void ds_print_lines_left(RDisasmState *ds);
static void ds_print_cycles(RDisasmState *ds);
static void ds_print_family(RDisasmState *ds);
static void ds_print_stackptr(RDisasmState *ds);
static void ds_print_offset(RDisasmState *ds);
static void ds_print_op_size(RDisasmState *ds);
static void ds_print_trace(RDisasmState *ds);
static void ds_adistrick_comments(RDisasmState *ds);
static int ds_print_meta_infos(RDisasmState *ds, ut8* buf, int len, int idx );
static void ds_print_opstr(RDisasmState *ds);
static void ds_print_color_reset(RDisasmState *ds);
static int ds_print_middle(RDisasmState *ds, int ret);
static bool ds_print_labels(RDisasmState *ds, RAnalFunction *f);
static void ds_print_import_name(RDisasmState *ds);
static void ds_print_sysregs(RDisasmState *ds);
static void ds_print_fcn_name(RDisasmState *ds);
static void ds_print_as_string(RDisasmState *ds);
static void ds_print_core_vmode(RDisasmState *ds, int pos);
static void ds_print_dwarf(RDisasmState *ds);
static void ds_print_asmop_payload(RDisasmState *ds, const ut8 *buf);
static char *ds_esc_str(RDisasmState *ds, const char *str, int len, const char **prefix_out);
static void ds_print_comments_right(RDisasmState *ds);
static void ds_print_ptr(RDisasmState *ds, int len, int idx);
static void ds_print_str(RDisasmState *ds, const char *str, int len, ut64 refaddr);
static char *ds_sub_jumps(RDisasmState *ds, char *str);
static void ds_start_line_highlight(RDisasmState *ds);
static void ds_end_line_highlight(RDisasmState *ds);
static bool line_highlighted(RDisasmState *ds);
R_API ut64 r_core_pava (RCore *core, ut64 addr) {
if (core->pava) {
RIOMap *map = r_io_map_get_paddr (core->io, addr);
if (map) {
return addr - map->delta + map->itv.addr;
}
}
return addr;
}
static RAnalFunction *fcnIn(RDisasmState *ds, ut64 at, int type) {
if (ds->fcn && r_tinyrange_in (&ds->fcn->bbr, at)) {
return ds->fcn;
}
return r_anal_get_fcn_in (ds->core->anal, at, type);
}
static int cmpaddr(const void *_a, const void *_b) {
const RAnalBlock *a = _a, *b = _b;
return (a->addr > b->addr);
}
static void get_bits_comment(RCore *core, RAnalFunction *f, char *cmt, int cmt_size) {
if (core && f && cmt && cmt_size > 0 && f->bits && f->bits != core->assembler->bits) {
const char *asm_arch = r_config_get (core->config, "asm.arch");
if (asm_arch && *asm_arch && strstr (asm_arch, "arm")) {
switch (f->bits) {
case 16: strcpy (cmt, " (thumb)"); break;
case 32: strcpy (cmt, " (arm)"); break;
case 64: strcpy (cmt, " (aarch64)"); break;
}
} else {
snprintf (cmt, cmt_size, " (%d bits)", f->bits);
}
} else {
if (cmt) {
cmt[0] = 0;
}
}
}
static const char * get_section_name(RCore *core, ut64 addr) {
static char section[128] = "";
static ut64 oaddr = UT64_MAX;
RIOSection *s;
if (oaddr == addr) {
return section;
}
s = r_io_section_vget (core->io, addr);
if (s) {
snprintf (section, sizeof (section)-1, "%10s ", s->name);
} else {
RListIter *iter;
RDebugMap *map;
*section = 0;
r_list_foreach (core->dbg->maps, iter, map) {
if (addr >= map->addr && addr < map->addr_end) {
const char *mn = r_str_lchr (map->name, '/');
if (mn) {
strncpy (section, mn + 1, sizeof (section) - 1);
} else {
strncpy (section, map->name, sizeof (section) - 1);
}
break;
}
}
}
oaddr = addr;
return section;
}
// up means if this lines go up, it controls whether to insert `_
// nl if we have to insert new line, it controls whether to insert \n
static void _ds_comment_align_(RDisasmState *ds, bool up, bool nl) {
ds->cmtcount ++;
if (ds->show_comment_right) {
if (ds->show_color) {
r_cons_printf (ds->pal_comment);
}
return;
}
//XXX fix this generate many dupes with section name
const char *sn = ds->show_section ? get_section_name (ds->core, ds->at) : "";
// if (ds->cmtcount == 0) {
ds_align_comment (ds);
// }
sn = ds->show_section ? get_section_name (ds->core, ds->at) : "";
ds_align_comment (ds);
r_cons_print (COLOR_RESET (ds));
ds_print_pre (ds);
r_cons_printf ("%s%s%s%s%s %s %s", nl? "\n": "", sn,
COLOR (ds, color_flow),ds->refline, COLOR_RESET (ds),
up? "": ".-", COLOR (ds, color_comment));
#if 0
// r_cons_printf ("(%d)", ds->cmtcount);
if (ds->cmtcount == 1) {
ds_align_comment (ds);
r_cons_printf ("%s%s%s%s%s%s%s %s %s", nl? "\n": "",
COLOR_RESET (ds), COLOR (ds, color_fline),
ds->pre, sn, ds->refline, COLOR_RESET (ds),
up? "": ".v", COLOR (ds, color_comment));
} else {
r_cons_printf ("%s%s", COLOR (ds, color_comment), " "); //nl? "\n": "");
}
#if 0
if (!up || ds->cmtcount > 1) {
r_cons_printf ("%s%s", COLOR (ds, color_comment), nl? "\n": "");
} else {
ds_align_comment (ds);
r_cons_printf ("%s%s%s%s%s%s%s %s %s", nl? "\n": "",
COLOR_RESET (ds), COLOR (ds, color_fline),
ds->pre, sn, ds->refline, COLOR_RESET (ds),
up? "": "`-", COLOR (ds, color_comment));
}
#endif
#endif
}
#define _ALIGN _ds_comment_align_ (ds, true, false)
static void ds_comment_lineup(RDisasmState *ds) {
_ALIGN;
}
static void ds_begin_comment(RDisasmState *ds) {
if (ds->show_comment_right) {
_ALIGN;
} else {
if (ds->use_json) {
ds_begin_json_line (ds);
}
ds_pre_xrefs (ds, false);
}
}
static void ds_comment(RDisasmState *ds, bool align, const char *format, ...) {
va_list ap;
va_start (ap, format);
if (ds->show_comments) {
if (ds->show_comment_right && align) {
ds_align_comment (ds);
} else if (!ds->use_json) {
r_cons_printf ("%s", COLOR (ds, color_comment));
}
}
if (!ds->use_json) {
r_cons_printf_list (format, ap);
} else {
char buffer[4096];
vsnprintf (buffer, sizeof(buffer), format, ap);
char *escstr = r_str_escape_latin1 (buffer, false, true);
if (escstr) {
r_cons_printf ("%s", escstr);
free (escstr);
}
}
if (!ds->show_comment_right && align) {
ds_newline (ds);
}
va_end (ap);
}
static void ds_comment_esil(RDisasmState *ds, bool up, bool end, const char *format, ...) {
va_list ap;
va_start (ap, format);
if (ds->show_comments && up) {
ds->show_comment_right ? ds_align_comment (ds) : ds_comment_lineup (ds);
}
r_cons_printf_list (format, ap);
va_end (ap);
if (ds->show_comments && !ds->show_comment_right) {
if (end) {
ds_newline (ds);
}
}
}
static void ds_print_esil_anal_fini(RDisasmState *ds) {
RCore *core = ds->core;
if (ds->show_emu && ds->esil_regstate) {
RCore* core = ds->core;
core->anal->last_disasm_reg = r_reg_arena_peek (core->anal->reg);
const char *pc = r_reg_get_name (core->anal->reg, R_REG_NAME_PC);
r_reg_arena_poke (core->anal->reg, ds->esil_regstate);
r_reg_setv (core->anal->reg, pc, ds->esil_old_pc);
R_FREE (ds->esil_regstate);
}
if (core && core->anal && core->anal->esil) {
// make sure to remove reference to ds to avoid UAF
core->anal->esil->user = NULL;
}
}
static RDisasmState * ds_init(RCore *core) {
RDisasmState *ds = R_NEW0 (RDisasmState);
if (!ds) {
return NULL;
}
ds->shortcut_pos = r_config_get_i (core->config, "asm.shortcut");
ds->core = core;
ds->strip = r_config_get (core->config, "asm.strip");
ds->pal_comment = core->cons->pal.comment;
#define P(x) (core->cons && core->cons->pal.x)? core->cons->pal.x
ds->color_comment = P(comment): Color_CYAN;
ds->color_usrcmt = P(usercomment): Color_CYAN;
ds->color_fname = P(fname): Color_RED;
ds->color_floc = P(floc): Color_MAGENTA;
ds->color_fline = P(fline): Color_CYAN;
ds->color_flow = P(flow): Color_CYAN;
ds->color_flow2 = P(flow2): Color_CYAN;
ds->color_flag = P(flag): Color_CYAN;
ds->color_label = P(label): Color_CYAN;
ds->color_other = P(other): Color_WHITE;
ds->color_nop = P(nop): Color_BLUE;
ds->color_bin = P(bin): Color_YELLOW;
ds->color_math = P(math): Color_YELLOW;
ds->color_btext = P(btext): Color_YELLOW;
ds->color_jmp = P(jmp): Color_GREEN;
ds->color_cjmp = P(cjmp): Color_GREEN;
ds->color_call = P(call): Color_BGREEN;
ds->color_cmp = P(cmp): Color_MAGENTA;
ds->color_swi = P(swi): Color_MAGENTA;
ds->color_trap = P(trap): Color_BRED;
ds->color_ret = P(ret): Color_RED;
ds->color_push = P(push): Color_YELLOW;
ds->color_pop = P(pop): Color_BYELLOW;
ds->color_reg = P(reg): Color_YELLOW;
ds->color_num = P(num): Color_CYAN;
ds->color_mov = P(mov): Color_WHITE;
ds->color_invalid = P(invalid): Color_BRED;
ds->color_gui_cflow = P(gui_cflow): Color_YELLOW;
ds->color_gui_dataoffset = P(gui_dataoffset): Color_YELLOW;
ds->color_gui_background = P(gui_background): Color_BLACK;
ds->color_gui_alt_background = P(gui_alt_background): Color_GRAY;
ds->color_gui_border = P(gui_border): Color_BGGRAY;
ds->color_linehl = P(linehl): Color_BGBLUE;
ds->immstr = r_config_get_i (core->config, "asm.imm.str");
ds->immtrim = r_config_get_i (core->config, "asm.imm.trim");
ds->use_esil = r_config_get_i (core->config, "asm.esil");
ds->pre_emu = r_config_get_i (core->config, "emu.pre");
ds->show_flgoff = r_config_get_i (core->config, "asm.flags.offset");
ds->show_nodup = r_config_get_i (core->config, "asm.nodup");
{
const char *ah = r_config_get (core->config, "asm.highlight");
ds->asm_highlight = (ah && *ah)? r_num_math (core->num, ah): UT64_MAX;
}
ds->asm_anal = r_config_get_i (core->config, "asm.anal");
ds->show_color = r_config_get_i (core->config, "scr.color");
ds->show_color_bytes = r_config_get_i (core->config, "scr.color.bytes"); // maybe rename to asm.color.bytes
ds->colorop = r_config_get_i (core->config, "scr.color.ops"); // XXX confusing name // asm.color.inst (mnemonic + operands) ?
ds->show_utf8 = r_config_get_i (core->config, "scr.utf8");
ds->acase = r_config_get_i (core->config, "asm.ucase");
ds->capitalize = r_config_get_i (core->config, "asm.capitalize");
ds->atabs = r_config_get_i (core->config, "asm.tabs");
ds->atabsonce = r_config_get_i (core->config, "asm.tabs.once");
ds->atabsoff = r_config_get_i (core->config, "asm.tabs.off");
ds->midflags = r_config_get_i (core->config, "asm.flags.middle");
ds->midcursor = r_config_get_i (core->config, "asm.midcursor");
ds->decode = r_config_get_i (core->config, "asm.decode");
core->parser->pseudo = ds->pseudo = r_config_get_i (core->config, "asm.pseudo");
if (ds->pseudo) {
ds->atabs = 0;
}
ds->filter = r_config_get_i (core->config, "asm.filter");
ds->interactive = r_config_get_i (core->config, "scr.interactive");
ds->jmpsub = r_config_get_i (core->config, "asm.jmpsub");
ds->varsub = r_config_get_i (core->config, "asm.var.sub");
core->parser->relsub = r_config_get_i (core->config, "asm.relsub");
core->parser->localvar_only = r_config_get_i (core->config, "asm.var.subonly");
core->parser->retleave_asm = NULL;
ds->show_vars = r_config_get_i (core->config, "asm.var");
ds->show_varsum = r_config_get_i (core->config, "asm.var.summary");
ds->show_varaccess = r_config_get_i (core->config, "asm.var.access");
ds->maxrefs = r_config_get_i (core->config, "asm.xrefs.max");
ds->foldxrefs = r_config_get_i (core->config, "asm.xrefs.fold");
ds->show_lines = r_config_get_i (core->config, "asm.lines");
ds->show_lines_bb = ds->show_lines ? r_config_get_i (core->config, "asm.lines.bb") : false;
ds->linesright = r_config_get_i (core->config, "asm.lines.right");
ds->show_indent = r_config_get_i (core->config, "asm.indent");
ds->indent_space = r_config_get_i (core->config, "asm.indentspace");
ds->tracespace = r_config_get_i (core->config, "asm.tracespace");
ds->cyclespace = r_config_get_i (core->config, "asm.cyclespace");
ds->show_dwarf = r_config_get_i (core->config, "asm.dwarf");
ds->dwarfFile = r_config_get_i (ds->core->config, "asm.dwarf.file");
ds->dwarfAbspath = r_config_get_i (ds->core->config, "asm.dwarf.abspath");
ds->show_lines_call = ds->show_lines ? r_config_get_i (core->config, "asm.lines.call") : false;
ds->show_lines_ret = ds->show_lines ? r_config_get_i (core->config, "asm.lines.ret") : false;
ds->show_size = r_config_get_i (core->config, "asm.size");
ds->show_trace = r_config_get_i (core->config, "asm.trace");
ds->linesout = r_config_get_i (core->config, "asm.lines.out");
ds->adistrick = r_config_get_i (core->config, "asm.middle"); // TODO: find better name
ds->asm_demangle = r_config_get_i (core->config, "asm.demangle");
ds->asm_describe = r_config_get_i (core->config, "asm.describe");
ds->show_offset = r_config_get_i (core->config, "asm.offset");
ds->show_offdec = r_config_get_i (core->config, "asm.decoff");
ds->show_bbline = r_config_get_i (core->config, "asm.bbline");
ds->show_section = r_config_get_i (core->config, "asm.section");
ds->show_section_col = r_config_get_i (core->config, "asm.section.col");
ds->show_symbols = r_config_get_i (core->config, "asm.symbol");
ds->show_symbols_col = r_config_get_i (core->config, "asm.symbol.col");
ds->show_emu = r_config_get_i (core->config, "asm.emu");
ds->show_emu_str = r_config_get_i (core->config, "emu.str");
ds->show_emu_stroff = r_config_get_i (core->config, "emu.stroff");
ds->show_emu_strinv = r_config_get_i (core->config, "emu.strinv");
ds->show_emu_strflag = r_config_get_i (core->config, "emu.strflag");
ds->show_emu_write = r_config_get_i (core->config, "emu.write");
ds->show_emu_stack = r_config_get_i (core->config, "emu.stack");
ds->stackFd = -1;
if (ds->show_emu_stack) {
// TODO: initialize fake stack in here
const char *uri = "malloc://32K";
ut64 size = r_num_get (core->num, "32K");
ut64 addr = r_reg_getv (core->anal->reg, "SP") - (size / 2);
emustack_min = addr;
emustack_max = addr + size;
ds->stackFd = r_io_fd_open (core->io, uri, R_IO_RW, 0);
RIOMap *map = r_io_map_add (core->io, ds->stackFd, R_IO_RW, 0LL, addr, size, true);
if (!map) {
r_io_fd_close (core->io, ds->stackFd);
eprintf ("Cannot create map for tha stack, fd %d got closed again\n", ds->stackFd);
ds->stackFd = -1;
} else {
r_io_map_set_name (map, "fake.stack");
}
}
ds->stackptr = core->anal->stackptr;
ds->show_offseg = r_config_get_i (core->config, "asm.segoff");
ds->show_flags = r_config_get_i (core->config, "asm.flags");
ds->show_bytes = r_config_get_i (core->config, "asm.bytes");
ds->asm_meta = r_config_get_i (core->config, "asm.meta");
ds->show_reloff = r_config_get_i (core->config, "asm.reloff");
ds->show_reloff_flags = r_config_get_i (core->config, "asm.reloff.flags");
ds->show_lines_fcn = ds->show_lines ? r_config_get_i (core->config, "asm.lines.fcn") : false;
ds->show_comments = r_config_get_i (core->config, "asm.comments");
ds->show_jmphints = r_config_get_i (core->config, "asm.jmphints");
ds->show_leahints = r_config_get_i (core->config, "asm.leahints");
ds->show_slow = r_config_get_i (core->config, "asm.slow");
ds->show_calls = r_config_get_i (core->config, "asm.calls");
ds->show_family = r_config_get_i (core->config, "asm.family");
ds->cmtcol = r_config_get_i (core->config, "asm.cmt.col");
ds->show_cmtflgrefs = r_config_get_i (core->config, "asm.cmt.flgrefs");
ds->show_cycles = r_config_get_i (core->config, "asm.cycles");
ds->show_stackptr = r_config_get_i (core->config, "asm.stackptr");
ds->show_xrefs = r_config_get_i (core->config, "asm.xrefs");
ds->show_cmtrefs = r_config_get_i (core->config, "asm.cmt.refs");
ds->cmtfold = r_config_get_i (core->config, "asm.cmt.fold");
ds->show_cmtoff = r_config_get (core->config, "asm.cmt.off");
ds->show_functions = r_config_get_i (core->config, "asm.functions");
ds->nbytes = r_config_get_i (core->config, "asm.nbytes");
ds->show_asciidot = !strcmp (core->print->strconv_mode, "asciidot");
const char *strenc_str = r_config_get (core->config, "asm.strenc");
if (!strcmp (strenc_str, "latin1")) {
ds->strenc = R_STRING_ENC_LATIN1;
} else if (!strcmp (strenc_str, "utf8")) {
ds->strenc = R_STRING_ENC_UTF8;
} else if (!strcmp (strenc_str, "utf16le")) {
ds->strenc = R_STRING_ENC_UTF16LE;
} else if (!strcmp (strenc_str, "utf32le")) {
ds->strenc = R_STRING_ENC_UTF32LE;
} else {
ds->strenc = R_STRING_ENC_GUESS;
}
core->print->bytespace = r_config_get_i (core->config, "asm.bytespace");
ds->cursor = 0;
ds->nb = 0;
ds->flagspace_ports = r_flag_space_get (core->flags, "ports");
ds->lbytes = r_config_get_i (core->config, "asm.lbytes");
ds->show_comment_right_default = r_config_get_i (core->config, "asm.cmt.right");
ds->show_comment_right = ds->show_comment_right_default;
ds->show_flag_in_bytes = r_config_get_i (core->config, "asm.flags.inbytes");
ds->show_hints = r_config_get_i (core->config, "asm.hints");
ds->show_marks = r_config_get_i (core->config, "asm.marks");
ds->show_noisy_comments = r_config_get_i (core->config, "asm.noisy");
ds->pre = DS_PRE_NONE;
ds->ocomment = NULL;
ds->linesopts = 0;
ds->lastfail = 0;
ds->ocols = 0;
ds->lcols = 0;
ds->printed_str_addr = UT64_MAX;
ds->printed_flag_addr = UT64_MAX;
ds->esil_old_pc = UT64_MAX;
ds->esil_regstate = NULL;
ds->esil_likely = false;
ds->showpayloads = r_config_get_i (ds->core->config, "asm.payloads");
ds->showrelocs = r_config_get_i (core->config, "bin.relocs");
ds->min_ref_addr = r_config_get_i (core->config, "asm.var.submin");
if (ds->show_flag_in_bytes) {
ds->show_flags = 0;
}
if (r_config_get_i (core->config, "asm.lines.wide")) {
ds->linesopts |= R_ANAL_REFLINE_TYPE_WIDE;
}
if (core->cons->vline) {
if (ds->show_utf8) {
ds->linesopts |= R_ANAL_REFLINE_TYPE_UTF8;
}
}
if (ds->show_lines_bb) {
ds->ocols += 10; // XXX
}
if (ds->show_offset) {
ds->ocols += 14;
}
ds->lcols = ds->ocols + 2;
if (ds->show_bytes) {
ds->ocols += 20;
}
if (ds->show_trace) {
ds->ocols += 8;
}
if (ds->show_stackptr) {
ds->ocols += 4;
}
/* disasm */ ds->ocols += 20;
ds->nb = ds->nbytes? (1 + ds->nbytes * 2): 0;
ds->tries = 3;
if (core->print->cur_enabled) {
if (core->print->cur < 0) {
core->print->cur = 0;
}
ds->cursor = core->print->cur;
} else {
ds->cursor = -1;
}
if (r_config_get_i (core->config, "asm.lines.wide")) {
ds->linesopts |= R_ANAL_REFLINE_TYPE_WIDE;
}
if (core->cons->vline) {
if (ds->show_utf8) {
ds->linesopts |= R_ANAL_REFLINE_TYPE_UTF8;
}
}
return ds;
}
static ut64 lastaddr = UT64_MAX;
static void ds_reflines_fini(RDisasmState *ds) {
RAnal *anal = ds->core->anal;
r_list_free (anal->reflines);
r_list_free (anal->reflines2);
anal->reflines = NULL;
anal->reflines2 = NULL;
R_FREE (ds->refline);
R_FREE (ds->refline2);
}
static void ds_reflines_init(RDisasmState *ds) {
RAnal *anal = ds->core->anal;
lastaddr = UT64_MAX;
if (ds->show_lines_bb) {
ds_reflines_fini (ds);
anal->reflines = r_anal_reflines_get (anal,
ds->addr, ds->buf, ds->len, ds->l,
ds->linesout, ds->show_lines_call);
anal->reflines2 = r_anal_reflines_get (anal,
ds->addr, ds->buf, ds->len, ds->l,
ds->linesout, 1);
} else {
r_list_free (anal->reflines);
r_list_free (anal->reflines2);
anal->reflines = anal->reflines2 = NULL;
}
}
static void ds_reflines_fcn_init(RDisasmState *ds, RAnalFunction *fcn, const ut8* buf) {
RCore *core = ds->core;
RAnal *anal = core->anal;
if (ds->show_lines_bb) {
// TODO: make anal->reflines implicit
free (anal->reflines); // TODO: leak
anal->reflines = r_anal_reflines_fcn_get (anal, fcn, -1, ds->linesout, ds->show_lines_call);
free (anal->reflines2); // TODO: leak
anal->reflines2 = r_anal_reflines_fcn_get (anal, fcn, -1, ds->linesout, 1);
} else {
r_list_free (anal->reflines);
r_list_free (anal->reflines2);
anal->reflines = anal->reflines2 = NULL;
}
}
static void ds_free(RDisasmState *ds) {
if (!ds) {
return;
}
if (ds->show_emu_stack) {
// TODO: destroy fake stack in here
eprintf ("Free fake stack\n");
if (ds->stackFd != -1) {
r_io_fd_close (ds->core->io, ds->stackFd);
}
}
r_anal_op_fini (&ds->analop);
r_anal_hint_free (ds->hint);
ds_print_esil_anal_fini (ds);
ds_reflines_fini (ds);
ds_print_esil_anal_fini (ds);
free (ds->comment);
free (ds->line);
free (ds->refline);
free (ds->refline2);
free (ds->opstr);
free (ds->osl);
free (ds->sl);
free (ds->_tabsbuf);
R_FREE (ds);
}
/* XXX move to r_print */
static char *colorize_asm_string(RCore *core, RDisasmState *ds, bool print_color) {
char *spacer = NULL;
char *source = ds->opstr? ds->opstr: ds->asmop.buf_asm;
char *hlstr = r_meta_get_string (ds->core->anal, R_META_TYPE_HIGHLIGHT, ds->at);
bool partial_reset = line_highlighted (ds) ? true : ((hlstr && *hlstr) ? true : false);
if (!ds->show_color || !ds->colorop) {
return strdup (source);
}
if (print_color) {
r_cons_strcat (r_print_color_op_type (core->print, ds->analop.type));
}
// workaround dummy colorizer in case of paired commands (tms320 & friends)
spacer = strstr (source, "||");
if (spacer) {
char *scol1, *s1 = r_str_ndup (source, spacer - source);
char *scol2, *s2 = strdup (spacer + 2);
scol1 = r_print_colorize_opcode (ds->core->print, s1, ds->color_reg, ds->color_num, partial_reset);
free (s1);
scol2 = r_print_colorize_opcode (ds->core->print, s2, ds->color_reg, ds->color_num, partial_reset);
free (s2);
if (!scol1) {
scol1 = strdup ("");
}
if (!scol2) {
scol2 = strdup ("");
}
source = malloc (strlen (scol1) + strlen (scol2) + 2 + 1); // reuse source variable
sprintf (source, "%s||%s", scol1, scol2);
free (scol1);
free (scol2);
return source;
}
return r_print_colorize_opcode (ds->core->print, source, ds->color_reg, ds->color_num, partial_reset);
}
static bool ds_must_strip(RDisasmState *ds) {
if (ds && ds->strip && *ds->strip) {
const char * optype = r_anal_optype_to_string (ds->analop.type);
if (optype && *optype) {
return strstr (ds->strip, optype);
}
}
return false;
}
static void ds_highlight_word(RDisasmState * ds, char *word, char *color) {
char *source = ds->opstr? ds->opstr: ds->asmop.buf_asm;
const char *color_reset = line_highlighted (ds) ? ds->color_linehl : Color_RESET_BG;
char *asm_str = r_str_highlight (source, word, color, color_reset);
ds->opstr = asm_str? asm_str:source;
}
static void ds_build_op_str(RDisasmState *ds, bool print_color) {
RCore *core = ds->core;
if (!ds->opstr) {
ds->opstr = strdup (ds->asmop.buf_asm);
}
/* initialize */
core->parser->hint = ds->hint;
core->parser->relsub = r_config_get_i (core->config, "asm.relsub");
core->parser->relsub_addr = 0;
if (ds->varsub && ds->opstr) {
ut64 at = ds->vat;
RAnalFunction *f = fcnIn (ds, at, R_ANAL_FCN_TYPE_NULL);
core->parser->varlist = r_anal_var_list_dynamic;
r_parse_varsub (core->parser, f, at, ds->analop.size,
ds->opstr, ds->strsub, sizeof (ds->strsub));
if (*ds->strsub) {
free (ds->opstr);
ds->opstr = strdup (ds->strsub);
}
if (core->parser->relsub) {
RList *list = r_anal_refs_get (core->anal, at);
RListIter *iter;
RAnalRef *ref;
r_list_foreach (list, iter, ref) {
if ((ref->type == R_ANAL_REF_TYPE_DATA
|| ref->type == R_ANAL_REF_TYPE_STRING)
&& ds->analop.type == R_ANAL_OP_TYPE_LEA) {
core->parser->relsub_addr = ref->addr;
break;
}
}
r_list_free (list);
}
}
char *asm_str = colorize_asm_string (core, ds, print_color);
asm_str = ds_sub_jumps (ds, asm_str);
if (ds->immtrim) {
r_parse_immtrim (ds->opstr);
free (asm_str);
return;
}
if (ds->decode) {
char *tmpopstr = r_anal_op_to_string (core->anal, &ds->analop);
// TODO: Use data from code analysis..not raw ds->analop here
// if we want to get more information
ds->opstr = tmpopstr? tmpopstr: asm_str? strdup (asm_str): strdup ("");
} else {
if (ds->hint && ds->hint->opcode) {
free (ds->opstr);
ds->opstr = strdup (ds->hint->opcode);
}
if (ds->filter) {
int ofs = core->parser->flagspace;
int fs = ds->flagspace_ports;
if (ds->analop.type == R_ANAL_OP_TYPE_IO) {
core->parser->notin_flagspace = -1;
core->parser->flagspace = fs;
} else {
if (fs != -1) {
core->parser->notin_flagspace = fs;
core->parser->flagspace = fs;
} else {
core->parser->notin_flagspace = -1;
core->parser->flagspace = -1;
}
}
if (ds->analop.refptr) {
if (core->parser->relsub_addr == 0) {
ut64 killme = UT64_MAX;
const int be = core->assembler->big_endian;
r_io_read_i (core->io, ds->analop.ptr, &killme, ds->analop.refptr, be);
core->parser->relsub_addr = (int)killme;
}
}
r_parse_filter (core->parser, core->flags, asm_str,
ds->str, sizeof (ds->str), core->print->big_endian);
core->parser->flagspace = ofs;
free (ds->opstr);
ds->opstr = strdup (ds->str);
} else {
free (ds->opstr);
ds->opstr = strdup (asm_str? asm_str: "");
}
}
if (ds->show_color) {
int i = 0;
char *word = NULL;
char *bgcolor = NULL;
char *wcdata = r_meta_get_string (ds->core->anal, R_META_TYPE_HIGHLIGHT, ds->at);
int argc = 0;
char **wc_array = r_str_argv (wcdata, &argc);
for (i = 0; i < argc; i++) {
bgcolor = strchr (wc_array[i], '\x1b');
word = r_str_newlen (wc_array[i], bgcolor - wc_array[i]);
ds_highlight_word (ds, word, bgcolor);
}
}
if (ds->use_esil) {
if (*R_STRBUF_SAFEGET (&ds->analop.esil)) {
free (ds->opstr);
ds->opstr = strdup (R_STRBUF_SAFEGET (&ds->analop.esil));
} else {
char *p = malloc (strlen (ds->opstr) + 6); /* What's up '\0' ? */
if (p) {
strcpy (p, "TODO,");