forked from radareorg/radare2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmd_open.c
1418 lines (1377 loc) · 36.9 KB
/
cmd_open.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-2017 - pancake */
#include "r_list.h"
#include "r_config.h"
#include "r_core.h"
#include "r_print.h"
#include "r_bin.h"
#include "r_debug.h"
#include "r_util/r_addr_interval.h"
static const char *help_msg_o[] = {
"Usage: o","[com- ] [file] ([offset])","",
"o","","list opened files",
"o"," 4","Switch to open file on fd 4",
"o","-1","close file descriptor 1",
"o-","!*","close all opened files",
"o--","","close all files, analysis, binfiles, flags, same as !r2 --",
"o"," [file]","open [file] file in read-only",
"o+"," [file]","open file in read-write mode",
"o"," [file] 0x4000 rwx", "map file at 0x4000",
"oa","[-] [A] [B] [filename]","Specify arch and bits for given file",
"oq","","list all open files",
"o*","","list opened files in r2 commands",
"o."," [len]","open a malloc://[len] copying the bytes from current offset",
"o=","","list opened files (ascii-art bars)",
"ob","[?] [lbdos] [...]","list opened binary files backed by fd",
"oc"," [file]","open core file, like relaunching r2",
"of"," [file]","open file and map it at addr 0 as read-only",
"oi","[-|idx]","alias for o, but using index instead of fd",
"oj","[?] ","list opened files in JSON format",
"oL","","list all IO plugins registered",
"om","[?]","create, list, remove IO maps",
"on"," [file] 0x4000","map raw file at 0x4000 (no r_bin involved)",
"oo","[?]","reopen current file (kill+fork in debugger)",
"oo","+","reopen current file in read-write",
"ood","[r] [args]","reopen in debugger mode (with args)",
"oo[bnm]"," [...]","see oo? for help",
"op"," [fd]", "priorize given fd (see also ob)",
"ox", " fd fdx", "exchange the descs of fd and fdx and keep the mapping",
NULL
};
static const char *help_msg_o_[] = {
"Usage: o-","[#!*]", "",
"o-*","","close all opened files",
"o-!","","close all files except the current one",
"o-3","","close fd=3",
NULL
};
static const char *help_msg_o_star[] = {
"Usage:", "o* [> files.r2]", "",
"o*", "", "list opened files in r2 commands", NULL
};
static const char *help_msg_oa[] = {
"Usage:", "oa [addr] ([filename])", " # load bininfo and update flags",
"oba", " [addr]", "Open bin info from the given address",
"oba", " [addr] [filename]", "Open file and load bin info at given address",
NULL
};
static const char *help_msg_ob[] = {
"Usage:", "ob", " # List open binary files backed by fd",
"ob", "", "List opened binary files and objid",
"ob*", "", "List opened binary files and objid (r2 commands)",
"ob", " [fd objid]", "Switch to open binary file by fd number and objid",
"oba", " [addr]", "Open bin info from the given address",
"oba", " [addr] [filename]", "Open file and load bin info at given address",
"obb", " [fd]", "Switch to open binfile by fd number",
"obj", "", "List opened binary files and objid (JSON format)",
"obr", " [baddr]", "Rebase current bin object",
"ob-", " [fd]", "Delete binfile by fd",
"obd", " [objid]", "Delete binary file by objid. Do nothing if only one loaded.",
"obo", " [objid]", "Switch to open binary file by objid",
NULL
};
static const char *help_msg_oj[] = {
"Usage:", "oj [~{}]", " # Use ~{} to indent the JSON",
"oj", "", "list opened files in JSON format", NULL
};
static const char *help_msg_om[] = {
"Usage:", "om[-] [arg]", " # map opened files",
"om", "", "list all defined IO maps",
"omq", "", "list all maps and their fds",
"om*", "", "list all maps in r2 commands format",
"om=", "", "list all maps in ascii art",
"omj", "", "list all maps in json format",
"om", " [fd]", "list all defined IO maps for a specific fd",
"om", "-mapid", "remove the map with corresponding id",
"om", " fd vaddr [size] [paddr] [rwx] [name]", "create new io map",
"omm"," [fd]", "create default map for given fd. (omm `oq`)",
"om.", "", "show map, that is mapped to current offset",
"omn", " mapid [name]", "set/delete name for map with mapid",
"omn.", "([-|name])", "show/set/delete name for current map",
"omf", " [mapid] rwx", "change flags/perms for current/given map",
"omfg", "[+-]rwx", "change flags/perms for all maps (global)",
"omb", " mapid addr", "relocate map with corresponding id",
"omb.", " addr", "relocate current map",
"omr", " mapid newsize", "resize map with corresponding id",
"omp", " mapid", "priorize map with corresponding id",
"ompf", "[fd]", "priorize map by fd",
"ompb", " binid", "priorize maps of mapped bin with binid",
"omps", " sectionid", "priorize maps of mapped section with sectionid",
"om*", "", "show r2 commands to restore mapaddr",
NULL
};
static const char *help_msg_oo[] = {
"Usage:", "oo[-] [arg]", " # map opened files",
"oo", "", "reopen current file",
"oo+", "", "reopen in read-write",
"oob", "", "reopen loading rbin info",
"ood", "", "reopen in debug mode",
"oom", "", "reopen in malloc://",
"oon", "", "reopen without loading rbin info",
"oon+", "", "reopen in read-write mode without loading rbin info",
"oonn", "", "reopen without loading rbin info, but with header flags",
"oonn+", "", "reopen in read-write mode without loading rbin info, but with",
NULL
};
static const char *help_msg_oo_plus[] = {
"oo+", "", "reopen in read-write",
NULL
};
static const char *help_msg_oob[] = {
"oob", "", "reopen loading rbin info",
NULL
};
static const char *help_msg_ood[] = {
"ood"," [args]","reopen in debugger mode (with args)",
"oodr"," [rarun2]","same as dor ..;ood",
NULL
};
static const char *help_msg_oon[] = {
"oon", "", "reopen without loading rbin info",
NULL
};
static const char *help_msg_oonn[] = {
"oonn", "", "reopen without loading rbin info, but with header flags",
NULL
};
static inline ut32 find_binfile_id_by_fd (RBin *bin, ut32 fd) {
RListIter *it;
RBinFile *bf;
r_list_foreach (bin->binfiles, it, bf) {
if (bf->fd == fd) {
return bf->id;
}
}
return UT32_MAX;
}
static void cmd_open_init(RCore *core) {
DEFINE_CMD_DESCRIPTOR (core, o);
DEFINE_CMD_DESCRIPTOR_SPECIAL (core, o*, o_star);
DEFINE_CMD_DESCRIPTOR (core, oa);
DEFINE_CMD_DESCRIPTOR (core, ob);
DEFINE_CMD_DESCRIPTOR (core, oj);
DEFINE_CMD_DESCRIPTOR (core, om);
DEFINE_CMD_DESCRIPTOR (core, oo);
DEFINE_CMD_DESCRIPTOR_SPECIAL (core, oo+, oo_plus);
DEFINE_CMD_DESCRIPTOR (core, oob);
DEFINE_CMD_DESCRIPTOR (core, ood);
DEFINE_CMD_DESCRIPTOR (core, oon);
DEFINE_CMD_DESCRIPTOR (core, oonn);
}
// very similiar to section list, must reuse more code
static void list_maps_visual(RIO *io, ut64 seek, ut64 len, int width, int use_color) {
ut64 mul, min = -1, max = 0;
SdbListIter *iter;
RIOMap *s;
int j, i;
width -= 80;
if (width < 1) {
width = 30;
}
// seek = (io->va || io->debug) ? r_io_section_vaddr_to_maddr_try (io, seek) : seek;
ls_foreach_prev (io->maps, iter, s) { //this must be prev, maps the previous map allways has lower priority
min = R_MIN (min, s->itv.addr);
max = R_MAX (max, r_itv_end (s->itv) - 1);
}
mul = (max - min) / width;
if (min != -1 && mul != 0) {
const char * color = "", *color_end = "";
i = 0;
ls_foreach_prev (io->maps, iter, s) {
if (use_color) {
color_end = Color_RESET;
if (s->flags & 1) { // exec bit
color = Color_GREEN;
} else if (s->flags & 2) { // write bit
color = Color_RED;
} else {
color = "";
color_end = "";
}
} else {
color = "";
color_end = "";
}
if (io->va) {
io->cb_printf ("%02d%c %s0x%08"PFMT64x"%s |", i,
r_itv_contain (s->itv, seek) ? '*' : ' ',
color, s->itv.addr, color_end);
} else {
io->cb_printf ("%02d%c %s0x%08"PFMT64x"%s |", i,
r_itv_contain (s->itv, seek) ? '*' : ' ',
color, s->itv.addr, color_end);
}
for (j = 0; j < width; j++) {
ut64 pos = min + j * mul;
ut64 npos = min + (j + 1) * mul;
// TODO trailing bytes
io->cb_printf (r_itv_overlap2 (s->itv, pos, npos - pos) ? "#" : "-");
}
io->cb_printf ("| %s0x%08"PFMT64x"%s %s %d %s\n",
color, r_itv_end (s->itv), color_end,
r_str_rwx_i (s->flags), s->fd, s->name);
i++;
}
/* current seek */
if (i > 0 && len != 0) {
if (seek == UT64_MAX) {
seek = 0;
}
//len = 8096;//r_io_size (io);
io->cb_printf ("=> 0x%08"PFMT64x" |", seek);
for (j=0;j<width;j++) {
io->cb_printf (
((j*mul)+min >= seek &&
(j*mul)+min <= seek+len)
?"^":"-");
}
io->cb_printf ("| 0x%08"PFMT64x"\n", seek+len);
}
}
}
static void cmd_open_bin(RCore *core, const char *input) {
const char *value = NULL;
ut32 binfile_num = -1, binobj_num = -1;
switch (input[1]) {
case '\0': // "ob"
case 'j': // "obj"
case '*': // "ob*"
r_core_bin_list (core, input[1]);
break;
case 'a': // "oba"
if ('?' == input[2]) {
r_core_cmd_help (core, help_msg_oa);
break;
}
if (input[3]) {
char *arg = strdup (input + 3);
char *filename = strchr (arg, ' ');
if (filename) {
RIODesc *desc = r_io_open (core->io, filename + 1, R_IO_READ, 0);
if (desc) {
*filename = 0;
ut64 addr = r_num_math (core->num, arg);
r_bin_load_io (core->bin, desc->fd, addr, 0, 0);
r_io_desc_close (desc);
r_core_cmd0 (core, ".is*");
} else {
eprintf ("Cannot open %s\n", filename + 1);
}
} else {
ut64 addr = r_num_math (core->num, input + 2);
RCoreFile *cf = r_core_file_cur (core);
if (cf) {
RIODesc *desc = r_io_desc_get (core->io, cf->fd);
if (desc) {
r_bin_load_io (core->bin, desc->fd, addr, 0, 0);
r_core_cmd0 (core, ".is*");
} else {
eprintf ("No file to load bin from?\n");
}
}
}
free (arg);
} else {
/* reload all bininfo */
RIODesc *desc;
RListIter *iter;
RCoreFile *file;
r_list_foreach (core->files, iter, file) {
desc = r_io_desc_get (core->io, file->fd);
r_bin_load_io (core->bin, desc->fd, core->offset, 0, 0);
r_core_cmd0 (core, ".is*");
break;
}
}
//r_bin_load_io_at_offset_as (core->bin, core->file->desc,
break;
case 'b': // "obb"
{
ut32 fd;
value = *(input + 3) ? input + 3 : NULL;
if (!value) {
eprintf ("Invalid fd number.");
break;
}
binfile_num = UT32_MAX;
fd = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
binfile_num = find_binfile_id_by_fd (core->bin, fd);
if (binfile_num == UT32_MAX) {
eprintf ("Invalid fd number.");
break;
}
r_core_bin_raise (core, binfile_num, -1);
}
break;
case ' ': // "ob "
{
ut32 fd;
int n;
const char *tmp;
char *v;
v = input[2] ? strdup (input + 2) : NULL;
if (!v) {
eprintf ("Invalid arguments");
break;
}
n = r_str_word_set0 (v);
if (n < 1 || n > 2) {
eprintf ("Invalid arguments\n");
eprintf ("usage: ob fd obj\n");
free (v);
break;
}
tmp = r_str_word_get0 (v, 0);
fd = *v && r_is_valid_input_num_value (core->num, tmp) ?
r_get_input_num_value (core->num, tmp) : UT32_MAX;
if (n == 2) {
tmp = r_str_word_get0 (v, 1);
binobj_num = *v && r_is_valid_input_num_value (core->num, tmp) ?
r_get_input_num_value (core->num, tmp) : UT32_MAX;
} else {
binfile_num = find_binfile_id_by_fd (core->bin, fd);
}
r_core_bin_raise (core, binfile_num, binobj_num);
free (v);
break;
}
case 'r': // "obr"
r_core_bin_rebase (core, r_num_math (core->num, input + 3));
r_core_cmd0 (core, ".is*");
break;
case 'o': // "obo"
value = input[3] ? input + 3 : NULL;
if (!value) {
eprintf ("Invalid argument");
break;
}
binobj_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binobj_num == UT32_MAX) {
eprintf ("Invalid binobj_num");
break;
}
r_core_bin_raise (core, -1, binobj_num);
break;
case '-': // "ob-"
//FIXME this command doesn't remove nothing
if (input[2] == '*') {
//FIXME this only delete from a list but it doesn't free any space
r_cons_printf ("[i] Deleted %d binfiles\n",
r_bin_file_delete_all (core->bin));
} else {
ut32 fd;
value = input[3] ? input + 3 : NULL;
if (!value) {
eprintf ("Invalid argument\n");
break;
}
fd = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
binfile_num = find_binfile_id_by_fd (core->bin, fd);
if (binfile_num == UT32_MAX) {
eprintf ("Invalid fd\n");
break;
}
if (r_core_bin_delete (core, binfile_num, -1)){
if (!r_bin_file_delete (core->bin, fd))
eprintf ("Cannot find an RBinFile associated with that fd.\n");
} else {
eprintf ("Couldn't erase because there must be 1 bin object loaded\n");
}
}
break;
case 'd': // "obd" backward compat, must be deleted
value = input[2] ? input + 2 : NULL;
if (!value) {
eprintf ("Invalid bin object number.");
break;
}
binobj_num = *value && r_is_valid_input_num_value (core->num, value) ?
r_get_input_num_value (core->num, value) : UT32_MAX;
if (binobj_num == UT32_MAX) {
eprintf ("Invalid bin object number.");
break;
}
r_core_bin_delete (core, -1, binobj_num);
break;
case '?': // "ob?"
r_core_cmd_help (core, help_msg_ob);
break;
}
}
// TODO: discuss the output format
static void map_list(RIO *io, int mode, RPrint *print, int fd) {
SdbListIter *iter;
RIOMap *map;
if (!io || !io->maps || !print || !print->cb_printf) {
return;
}
if (mode == 'j') {
print->cb_printf ("[");
}
bool first = true;
ls_foreach_prev (io->maps, iter, map) { //this must be prev
if (fd != -1 && map->fd != fd) {
continue;
}
switch (mode) {
case 'q':
print->cb_printf ("%d %d\n", map->fd, map->id);
break;
case 'j':
if (!first) {
print->cb_printf (",");
}
first = false;
print->cb_printf ("{\"map\":%i,\"fd\":%d,\"delta\":%"PFMT64u",\"from\":%"PFMT64u
",\"to\":%"PFMT64u",\"flags\":\"%s\",\"name\":\"%s\"}", map->id, map->fd,
map->delta, map->itv.addr, r_itv_end (map->itv),
r_str_rwx_i (map->flags), (map->name ? map->name : ""));
break;
case 1:
case '*':
case 'r':
print->cb_printf ("om %d 0x%"PFMT64x" 0x%"PFMT64x" 0x%"PFMT64x" %s%s%s\n", map->fd,
map->itv.addr, map->itv.size, map->delta, r_str_rwx_i(map->flags),
map->name ? " " : "", map->name ? map->name : "");
break;
default:
print->cb_printf ("%2d fd: %i +0x%08"PFMT64x" 0x%08"PFMT64x
" - 0x%08"PFMT64x" %s %s\n", map->id, map->fd,
map->delta, map->itv.addr, r_itv_end (map->itv) - 1,
r_str_rwx_i (map->flags), (map->name ? map->name : ""));
break;
}
}
if (mode == 'j') {
print->cb_printf ("]\n");
}
}
static void cmd_omfg (RCore *core, const char *input) {
SdbListIter *iter;
RIOMap *map;
input = r_str_chop_ro (input);
if (input) {
int flags = *input
? (*input == '+' || *input == '-')
? r_str_rwx (input + 1)
: r_str_rwx (input)
: 7;
switch (*input) {
case '+':
ls_foreach (core->io->maps, iter, map) {
map->flags |= flags;
}
break;
case '-':
ls_foreach (core->io->maps, iter, map) {
map->flags &= ~flags;
}
break;
default:
ls_foreach (core->io->maps, iter, map) {
map->flags = flags;
}
break;
}
}
}
static void cmd_omf (RCore *core, const char *input) {
SdbListIter *iter;
RIOMap *map;
char *arg = strdup (r_str_chop_ro (input));
if (!arg) {
return;
}
char *sp = strchr (arg, ' ');
if (sp) {
// change perms of Nth map
*sp++ = 0;
int id = r_num_math (core->num, arg);
int flags = (*sp)? r_str_rwx (sp): 7;
ls_foreach (core->io->maps, iter, map) {
if (map->id == id) {
map->flags = flags;
break;
}
}
} else {
// change perms of current map
int flags = (arg && *arg)? r_str_rwx (arg): 7;
ls_foreach (core->io->maps, iter, map) {
if (r_itv_contain (map->itv, core->offset)) {
map->flags = flags;
}
}
}
free (arg);
}
static void cmd_open_map(RCore *core, const char *input) {
ut64 fd = 0LL;
ut32 id = 0;
char *s = NULL, *p = NULL, *q = NULL;
ut64 new;
RIOMap *map = NULL;
const char *P;
switch (input[1]) {
case '.':
map = r_io_map_get (core->io, core->offset);
if (map) {
core->print->cb_printf ("map: %i fd: %i +0x%"PFMT64x" 0x%"PFMT64x
" - 0x%"PFMT64x" ; %s : %s\n", map->id, map->fd,
map->delta, map->itv.addr, r_itv_end (map->itv),
r_str_rwx_i (map->flags), map->name ? map->name : "");
}
break;
case 'r': // "omr"
if (input[2] != ' ') {
break;
}
P = strchr (input+3, ' ');
if (P) {
id = (ut32)r_num_math (core->num, input+3); //mapid
new = r_num_math (core->num, P+1);
r_io_map_resize (core->io, id, new);
}
break;
case 'b': // "omb"
if (input[2] == '.') {
RIOMap *map = r_io_map_get (core->io, core->offset);
if (map) {
ut64 dst = r_num_math (core->num, input + 3);
r_io_map_remap (core->io, map->id, dst);
}
} else {
if (input[2] != ' ') {
break;
}
P = strchr (input + 3, ' ');
if (P) {
id = (ut32)r_num_math (core->num, input+3); //mapid
new = r_num_math (core->num, P + 1);
r_io_map_remap (core->io, id, new);
}
}
break;
case 'p':
switch (input[2]) {
case 'f': // "ompf"
fd = r_num_math (core->num, input + 3);
if (!r_io_map_priorize_for_fd (core->io, (int)fd)) {
eprintf ("Cannot priorize any map for fd %d\n", (int)fd);
}
break;
case 'b': // "ompb"
id = (ut32)r_num_math (core->num, input + 4);
if (!r_io_section_priorize_bin (core->io, id)) {
eprintf ("Cannot priorize bin with binid %d\n", id);
}
break;
case 's': // "omps"
id = (ut32)r_num_math (core->num, input + 4);
if (!r_io_section_priorize (core->io, id)) {
eprintf ("Cannot priorize section with sectionid %d\n", id);
}
break;
case ' ': // "omp"
id = r_num_math (core->num, input + 3); //mapid
if (r_io_map_exists_for_id (core->io, id)) {
r_io_map_priorize (core->io, id);
} else {
eprintf ("Cannot find any map with mapid %d\n", id);
}
break;
}
break;
case ' ': // "om"
s = strdup (input + 2);
if (!s) {
break;
}
if (strchr (s, ' ')) {
int fd = 0, words = 0, rwx = 0;
ut64 size = 0, vaddr = 0, paddr = 0;
const char *name = NULL;
bool rwx_arg = false;
RIODesc *desc = NULL;
words = r_str_word_set0 (s);
switch (words) {
case 6:
name = r_str_word_get0 (s, 5);
case 5: //this sucks
rwx = r_str_rwx (r_str_word_get0 (s, 4));
rwx_arg = true;
case 4:
paddr = r_num_math (core->num, r_str_word_get0 (s, 3));
case 3:
size = r_num_math (core->num, r_str_word_get0 (s, 2));
case 2:
vaddr = r_num_math (core->num, r_str_word_get0 (s, 1));
case 1:
fd = r_num_math (core->num, r_str_word_get0 (s, 0));
}
if (fd < 3) {
eprintf ("wrong fd, it must be greater than 3\n");
break;
}
desc = r_io_desc_get (core->io, fd);
if (desc) {
if (!size) {
size = r_io_fd_size (core->io, fd);
}
map = r_io_map_add (core->io, fd, rwx_arg ? rwx : desc->flags, paddr, vaddr, size, true);
r_io_map_set_name (map, name);
}
} else {
int fd = r_io_fd_get_current (core->io);
if (r_io_desc_get (core->io, fd)) {
map_list (core->io, 0, core->print, fd);
} else {
eprintf ("Invalid fd %d\n", (int)fd);
}
}
R_FREE (s);
break;
case 'n': // "omn"
if (input[2] == '.') { // "omn."
RIOMap *map = r_io_map_get (core->io, core->offset);
if (map) {
switch (input[3]) {
case '-':
r_io_map_del_name (map);
break;
case 0:
r_cons_printf ("%s\n", map->name);
break;
default:
r_io_map_set_name (map, input + 3);
break;
}
}
} else {
if (!(s = strdup (&input[2]))) {
break;
}
p = s;
while (*s == ' ') {
s++;
}
if (*s == '\0') {
s = p;
break;
}
if (!(q = strchr (s, ' '))) {
id = (ut32)r_num_math (core->num, s);
map = r_io_map_get (core->io, id);
r_io_map_del_name (map);
s = p;
break;
}
*q = '\0';
q++;
id = (ut32)r_num_math (core->num, s);
map = r_io_map_get (core->io, id);
if (*q) {
r_io_map_set_name (map, q);
} else {
r_io_map_del_name (map);
}
s = p;
}
break;
case 'm': // "omm"
{
ut64 fd = input[3]? r_num_math (core->num, input + 3): UT64_MAX;
RIODesc *desc = r_io_desc_get (core->io, fd);
if (!desc) {
fd = r_io_fd_get_current (core->io);
desc = r_io_desc_get (core->io, fd);
}
if (desc) {
ut64 size = r_io_desc_size (desc);
map = r_io_map_add (core->io, fd, desc->flags, 0, 0, size, true);
r_io_map_set_name (map, desc->name);
} else {
eprintf ("Usage: omm [fd]\n");
}
}
break;
case '-': // "om-"
if (input[2] == '*') {
r_io_map_reset (core->io);
} else {
r_io_map_del (core->io, r_num_math (core->num, input + 2));
}
break;
case 'f': // "omf"
switch (input[2]) {
case 'g': // "omfg"
cmd_omfg (core, input + 3);
break;
case ' ': // "omf"
cmd_omf (core, input + 3);
break;
default:
r_core_cmd_help (core, help_msg_om);
break;
}
break;
case '\0': // "om"
case 'j': // "omj"
case '*': // "om*"
case 'q': // "omq"
map_list (core->io, input[1], core->print, -1);
break;
case '=': // "om="
list_maps_visual (core->io, core->offset, core->blocksize,
r_cons_get_size (NULL), r_config_get_i (core->config, "scr.color"));
break;
default:
case '?':
r_core_cmd_help (core, help_msg_om);
break;
}
R_FREE (s);
r_core_block_read (core);
}
R_API void r_core_file_reopen_in_malloc (RCore *core) {
RCoreFile *f;
RListIter *iter;
r_list_foreach (core->files, iter, f) {
ut64 sz = r_io_fd_size (core->io, f->fd);
ut8 *buf = calloc (sz, 1);
if (!buf) {
eprintf ("Cannot allocate %d\n", (int)sz);
continue;
}
(void)r_io_pread_at (core->io, 0, buf, sz);
char *url = r_str_newf ("malloc://%d", (int)sz);
RIODesc *desc = r_io_open (core->io, url, R_IO_READ | R_IO_WRITE, 0); //use r_io_desc_exchange pls
if (desc) {
r_io_fd_close (core->io, f->fd);
f->fd = desc->fd;
(void)r_io_write_at (core->io, 0, buf, sz);
} else {
eprintf ("Cannot open %s\n", url);
}
free (buf);
free (url);
break;
}
}
R_API void r_core_file_reopen_debug (RCore *core, const char *args) {
RCoreFile *ofile = core->file;
RBinFile *bf = NULL;
RIODesc *desc;
char *binpath = NULL;
if (!ofile || !(desc = r_io_desc_get (core->io, ofile->fd)) || !desc->uri) {
eprintf ("No file open?\n");
return;
}
bf = r_bin_file_find_by_fd (core->bin, ofile->fd);
binpath = bf ? strdup (bf->file) : NULL;
if (!binpath) {
if (r_file_exists (desc->name)) {
binpath = strdup (desc->name);
}
}
if (!binpath) {
/* fallback to oo */
(void)r_core_cmd0 (core, "oo");
return;
}
int bits = core->assembler->bits;
char *oldname = r_file_abspath (binpath);
char *newfile = r_str_newf ("dbg://%s %s", oldname, args);
char *newfile2 = strdup (newfile);
desc->uri = newfile;
desc->referer = NULL;
r_config_set_i (core->config, "asm.bits", bits);
r_config_set_i (core->config, "cfg.debug", true);
r_core_file_reopen (core, newfile, 0, 2);
newfile = newfile2;
#if !__WINDOWS__
ut64 new_baddr = r_debug_get_baddr (core->dbg, newfile);
ut64 old_baddr = r_config_get_i (core->config, "bin.baddr");
if (old_baddr != new_baddr) {
r_bin_set_baddr (core->bin, new_baddr);
r_config_set_i (core->config, "bin.baddr", new_baddr);
r_core_bin_rebase (core, new_baddr);
// r_core_bin_load (core, newfile, new_baddr);
// reload symbols with new baddr
r_core_cmd0 (core, ".is*");
r_core_cmd0 (core, ".ir*");
r_core_cmd0 (core, ".iz*");
r_core_cmd0 (core, ".iM*");
}
#endif
r_core_cmd0 (core, "sr PC");
free (oldname);
free (binpath);
free (newfile);
}
static int fdsz = 0;
static bool init_desc_list_visual_cb(void *user, void *data, ut32 id) {
RIODesc *desc = (RIODesc *)data;
ut64 sz = r_io_desc_size (desc);
if (sz > fdsz) {
fdsz = sz;
}
return true;
}
static bool desc_list_visual_cb(void *user, void *data, ut32 id) {
RPrint *p = (RPrint *)user;
RIODesc *desc = (RIODesc *)data;
ut64 sz = r_io_desc_size (desc);
r_cons_printf ("%2d %c %s 0x%08"PFMT64x" ", desc->fd,
(desc->io && (desc->io->desc == desc)) ? '*' : '-', r_str_rwx_i (desc->flags), sz);
int flags = p->flags;
p->flags &= ~R_PRINT_FLAGS_HEADER;
r_print_progressbar (p, sz * 100 / fdsz, r_cons_get_size (NULL) - 40);
p->flags = flags;
r_cons_printf (" %s\n", desc->uri);
#if 0
RIOMap *map;
SdbListIter *iter;
if (desc->io && desc->io->va && desc->io->maps) {
ls_foreach_prev (desc->io->maps, iter, map) {
if (map->fd == desc->fd) {
p->cb_printf (" +0x%"PFMT64x" 0x%"PFMT64x
" - 0x%"PFMT64x" : %s : %s : %s\n", map->delta,
map->from, map->to, r_str_rwx_i (map->flags), "",
map->name ? map->name : "");
}
}
}
#endif
return true;
}
static bool desc_list_quiet_cb(void *user, void *data, ut32 id) {
RPrint *p = (RPrint *)user;
RIODesc *desc = (RIODesc *)data;
p->cb_printf ("%d\n", desc->fd);
return true;
}
static bool desc_list_cb(void *user, void *data, ut32 id) {
RPrint *p = (RPrint *)user;
RIODesc *desc = (RIODesc *)data;
p->cb_printf ("%2d %c %s 0x%08"PFMT64x" %s\n", desc->fd,
(desc->io && (desc->io->desc == desc)) ? '*' : '-',
r_str_rwx_i (desc->flags), r_io_desc_size (desc), desc->uri);
return true;
}
static int cmd_open(void *data, const char *input) {
RCore *core = (RCore*)data;
int perms = R_IO_READ;
ut64 baddr = r_config_get_i (core->config, "bin.baddr"),
addr = 0LL;
int nowarn = r_config_get_i (core->config, "file.nowarn"),
argc, fd;
RCoreFile *file;
bool silence = false;
const char *ptr = NULL;
char **argv = NULL;
switch (*input) {
case 'a':
switch (input[1]) {
case '*': // "oa*"
{
RListIter *iter;
RBinFile *bf = NULL;
r_list_foreach (core->bin->binfiles, iter, bf) {
if (bf && bf->o && bf->o->info) {
eprintf ("oa %s %d %s\n", bf->o->info->arch, bf->o->info->bits, bf->file);
}
}
return 1;
}
case '?': // "oa?"
case ' ': // "oa "
{
int i;
char *ptr = strdup (input+2);
const char *arch = NULL;
ut16 bits = 0;
const char *filename = NULL;
i = r_str_word_set0 (ptr);
if (i < 2) {
eprintf ("Missing argument\n");
free (ptr);
return 0;
}
if (i == 3) {
filename = r_str_word_get0 (ptr, 2);
}
bits = r_num_math (core->num, r_str_word_get0 (ptr, 1));
arch = r_str_word_get0 (ptr, 0);
r_core_bin_set_arch_bits (core, filename, arch, bits);
RBinFile *file = r_bin_file_find_by_name (core->bin, filename);
if (!file) {
eprintf ("Cannot find file %s\n", filename);
free (ptr);
return 0;
}
if (file->o && file->o->info) {
file->o->info->arch = strdup(arch);
file->o->info->bits = bits;
r_core_bin_set_env (core, file);
}
free (ptr);
return 1;
}
break;
default:
eprintf ("Usage: oa[-][arch] [bits] [filename]\n");
return 0;
}
case 'n': // "on"
if (input[1] == '*') {
r_core_file_list (core, 'n');
return 0;
} else if (input[1] == 's') { // "ons"
silence = true;
if (input[2] != ' ') {
eprintf ("Usage: ons file [addr] [rwx]\n");
return 0;
}
ptr = input + 3;
} else if (input[1] == ' ') {
ptr = input + 2;
} else {
eprintf ("Usage: on file [addr] [rwx]\n");
return 0;
}
argv = r_str_argv (ptr, &argc);
if (!argc) {
if (silence) {
eprintf ("Usage: ons file [addr] [rwx]\n");
return 0;
} else {
eprintf ("Usage: on file [addr] [rwx]\n");
return 0;
}
r_str_argv_free (argv);
return 0;
} else {
ptr = argv[0];
}
if (argc == 2) {
if (r_num_is_valid_input (core->num, argv[1])) {
addr = r_num_math (core->num, argv[1]);
} else {
perms = r_str_rwx (argv[1]);