-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
bin.c
1698 lines (1568 loc) · 42.2 KB
/
bin.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
/* radare2 - LGPL - Copyright 2009-2018 - pancake, nibble, dso */
#include <r_bin.h>
#include <r_types.h>
#include <r_util.h>
#include <r_lib.h>
#include <r_io.h>
#include <config.h>
R_LIB_VERSION (r_bin);
#define DB a->sdb;
#define RBINLISTFREE(x)\
if (x) { \
r_list_free (x);\
x = NULL;\
}
#define ARCHS_KEY "archs"
#if !defined(R_BIN_STATIC_PLUGINS)
#define R_BIN_STATIC_PLUGINS 0
#endif
#if !defined(R_BIN_XTR_STATIC_PLUGINS)
#define R_BIN_XTR_STATIC_PLUGINS 0
#endif
#if !defined(R_BIN_LDR_STATIC_PLUGINS)
#define R_BIN_LDR_STATIC_PLUGINS 0
#endif
static RBinPlugin *bin_static_plugins[] = { R_BIN_STATIC_PLUGINS, NULL };
static RBinXtrPlugin *bin_xtr_static_plugins[] = { R_BIN_XTR_STATIC_PLUGINS, NULL };
static RBinLdrPlugin *bin_ldr_static_plugins[] = { R_BIN_LDR_STATIC_PLUGINS, NULL };
static bool r_bin_load_io_at_offset_as(RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx, ut64 offset, const char *name);
static int getoffset(RBin *bin, int type, int idx) {
RBinFile *a = r_bin_cur (bin);
RBinPlugin *plugin = r_bin_file_cur_plugin (a);
if (plugin && plugin->get_offset) {
return plugin->get_offset (a, type, idx);
}
return -1;
}
static const char *getname(RBin *bin, int type, int idx) {
RBinFile *a = r_bin_cur (bin);
RBinPlugin *plugin = r_bin_file_cur_plugin (a);
if (plugin && plugin->get_name) {
return plugin->get_name (a, type, idx);
}
return NULL;
}
static ut64 binobj_a2b(RBinObject *o, ut64 addr) {
return addr + (o? o->baddr_shift: 0);
}
R_API void r_bin_iobind(RBin *bin, RIO *io) {
r_io_bind (io, &bin->iob);
}
// TODO: move these two function do a different file
R_API RBinXtrData *r_bin_xtrdata_new(RBuffer *buf, ut64 offset, ut64 size,
ut32 file_count,
RBinXtrMetadata *metadata) {
RBinXtrData *data = R_NEW0 (RBinXtrData);
if (!data) {
return NULL;
}
data->offset = offset;
data->size = size;
data->file_count = file_count;
data->metadata = metadata;
data->loaded = 0;
// TODO: USE RBuffer *buf inside RBinXtrData*
data->buffer = malloc (size + 1);
// data->laddr = 0; /// XXX
if (!data->buffer) {
free (data);
return NULL;
}
// XXX unnecessary memcpy, this is slow
memcpy (data->buffer, r_buf_buffer (buf), size);
data->buffer[size] = 0;
return data;
}
R_API const char *r_bin_string_type (int type) {
switch (type) {
case 'a': return "ascii";
case 'u': return "utf8";
case 'w': return "utf16le";
case 'W': return "utf32le";
case 'b': return "base64";
}
return "ascii"; // XXX
}
R_API void r_bin_xtrdata_free(void /*RBinXtrData*/ *data_) {
RBinXtrData *data = data_;
if (data) {
if (data->metadata) {
free (data->metadata->libname);
free (data->metadata->arch);
free (data->metadata->machine);
free (data->metadata);
}
free (data->file);
free (data->buffer);
free (data);
}
}
R_API RList* r_bin_raw_strings(RBinFile *bf, int min) {
RList *l = NULL;
if (bf) {
int tmp = bf->rawstr;
bf->rawstr = 2;
l = r_bin_file_get_strings (bf, min, 0);
bf->rawstr = tmp;
}
return l;
}
R_API int r_bin_dump_strings(RBinFile *a, int min) {
r_bin_file_get_strings (a, min, 1);
return 0;
}
/* This is very slow if there are lot of symbols */
R_API int r_bin_load_languages(RBinFile *binfile) {
if (r_bin_lang_rust (binfile)) {
return R_BIN_NM_RUST;
}
if (r_bin_lang_swift (binfile)) {
return R_BIN_NM_SWIFT;
}
if (r_bin_lang_objc (binfile)) {
return R_BIN_NM_OBJC;
}
if (r_bin_lang_cxx (binfile)) {
return R_BIN_NM_CXX;
}
if (r_bin_lang_dlang (binfile)) {
return R_BIN_NM_DLANG;
}
if (r_bin_lang_msvc (binfile)) {
return R_BIN_NM_MSVC;
}
return R_BIN_NM_NONE;
}
R_API void r_bin_info_free(RBinInfo *rb) {
if (!rb) {
return;
}
free (rb->intrp);
free (rb->file);
free (rb->type);
free (rb->bclass);
free (rb->rclass);
free (rb->arch);
free (rb->cpu);
free (rb->machine);
free (rb->os);
free (rb->subsystem);
free (rb->rpath);
free (rb->guid);
free (rb->debug_file_name);
free (rb);
}
R_API void r_bin_import_free(void *_imp) {
RBinImport *imp = (RBinImport *)_imp;
if (imp) {
R_FREE (imp->name);
R_FREE (imp->classname);
R_FREE (imp->descriptor);
free (imp);
}
}
R_API void r_bin_symbol_free(void *_sym) {
RBinSymbol *sym = (RBinSymbol *)_sym;
free (sym->name);
free (sym->classname);
free (sym);
}
R_API void r_bin_string_free(void *_str) {
RBinString *str = (RBinString *)_str;
free (str->string);
free (str);
}
// XXX - change this to RBinObject instead of RBinFile
// makes no sense to pass in a binfile and set the RBinObject
// kinda a clunky functions
// XXX - this is a rather hacky way to do things, there may need to be a better
// way.
R_API int r_bin_load(RBin *bin, const char *file, ut64 baseaddr, ut64 loadaddr, int xtr_idx, int fd, int rawstr) {
if (!bin) {
return false;
}
// ALIAS? return r_bin_load_as (bin, file, baseaddr, loadaddr,
// xtr_idx, fd, rawstr, 0, file);
RIOBind *iob = &(bin->iob);
if (!iob) {
return false;
}
if (!iob->io) {
iob->io = r_io_new (); //wtf
if (!iob->io) {
return false;
}
bin->io_owned = true;
r_io_bind (iob->io, &bin->iob); //memleak?
iob = &bin->iob;
}
if (!iob->desc_get (iob->io, fd)) {
fd = iob->fd_open (iob->io, file, R_IO_READ, 0644);
}
bin->rawstr = rawstr;
// Use the current RIODesc otherwise r_io_map_select can swap them later on
if (fd < 0) {
if (bin->io_owned) {
r_io_free (iob->io);
memset (&bin->iob, 0, sizeof (bin->iob));
bin->io_owned = false;
}
return false;
}
//Use the current RIODesc otherwise r_io_map_select can swap them later on
return r_bin_load_io (bin, fd, baseaddr, loadaddr, xtr_idx);
}
R_API int r_bin_load_as(RBin *bin, const char *file, ut64 baseaddr,
ut64 loadaddr, int xtr_idx, int fd, int rawstr,
int fileoffset, const char *name) {
RIOBind *iob = &(bin->iob);
if (!iob || !iob->io) {
return false;
}
if (fd < 0) {
fd = iob->fd_open (iob->io, file, R_IO_READ, 0644);
}
if (fd < 0) {
return false;
}
return r_bin_load_io_at_offset_as (bin, fd, baseaddr, loadaddr,
xtr_idx, fileoffset, name);
}
R_API int r_bin_reload(RBin *bin, int fd, ut64 baseaddr) {
RIOBind *iob = &(bin->iob);
RList *the_obj_list = NULL;
int res = false;
RBinFile *bf = NULL;
ut8 *buf_bytes = NULL;
ut64 sz = UT64_MAX;
if (!iob || !iob->io) {
res = false;
goto error;
}
const char *name = iob->fd_get_name (iob->io, fd);
bf = r_bin_file_find_by_name (bin, name);
if (!bf) {
res = false;
goto error;
}
the_obj_list = bf->objs;
bf->objs = r_list_newf ((RListFree)r_bin_object_free);
// invalidate current object reference
bf->o = NULL;
sz = iob->fd_size (iob->io, fd);
if (sz == UT64_MAX) { // || sz > (64 * 1024 * 1024)) {
// too big, probably wrong
eprintf ("Too big\n");
res = false;
goto error;
}
// TODO: deprecate, the code in the else should be enough
#if 1
if (sz == UT64_MAX && iob->fd_is_dbg (iob->io, fd)) {
// attempt a local open and read
// This happens when a plugin like debugger does not have a
// fixed size.
// if there is no fixed size or its MAXED, there is no way to
// definitively
// load the bin-properly. Many of the plugins require all
// content and are not
// stream based loaders
int tfd = iob->fd_open (iob->io, name, R_IO_READ, 0);
if (tfd < 0) {
res = false;
goto error;
}
sz = iob->fd_size (iob->io, tfd);
if (sz == UT64_MAX) {
iob->fd_close (iob->io, tfd);
res = false;
goto error;
}
// OMG NOES we want to use io, not this shit.
buf_bytes = calloc (1, sz + 1);
if (!buf_bytes) {
iob->fd_close (iob->io, tfd);
res = false;
goto error;
}
if (!iob->read_at (iob->io, 0LL, buf_bytes, sz)) {
free (buf_bytes);
iob->fd_close (iob->io, tfd);
res = false;
goto error;
}
iob->fd_close (iob->io, tfd);
} else {
buf_bytes = calloc (1, sz + 1);
if (!buf_bytes) {
res = false;
goto error;
}
if (!iob->fd_read_at (iob->io, fd, 0LL, buf_bytes, sz)) {
free (buf_bytes);
res = false;
goto error;
}
}
bool yes_plz_steal_ptr = true;
r_bin_file_set_bytes (bf, buf_bytes, sz, yes_plz_steal_ptr);
#else
bf->buf = r_buf_new_with_io (iob, fd);
#endif
if (r_list_length (the_obj_list) == 1) {
RBinObject *old_o = (RBinObject *)r_list_get_n (the_obj_list, 0);
res = r_bin_load_io_at_offset_as (bin, fd, baseaddr,
old_o->loadaddr, 0, old_o->boffset, NULL);
} else {
RListIter *iter = NULL;
RBinObject *old_o;
r_list_foreach (the_obj_list, iter, old_o) {
// XXX - naive. do we need a way to prevent multiple "anys" from being opened?
res = r_bin_load_io_at_offset_as (bin, fd, baseaddr,
old_o->loadaddr, 0, old_o->boffset, old_o->plugin->name);
}
}
bf->o = r_list_get_n (bf->objs, 0);
error:
r_list_free (the_obj_list);
return res;
}
R_API int r_bin_load_io(RBin *bin, int fd, ut64 baseaddr, ut64 loadaddr, int xtr_idx) {
return r_bin_load_io_at_offset_as (bin, fd, baseaddr, loadaddr, xtr_idx, 0, NULL);
}
R_API int r_bin_load_io_at_offset_as_sz(RBin *bin, int fd, ut64 baseaddr,
ut64 loadaddr, int xtr_idx, ut64 offset, const char *name, ut64 sz) {
RIOBind *iob = &(bin->iob);
RIO *io = iob? iob->io: NULL;
RListIter *it;
ut8 *buf_bytes = NULL;
RBinXtrPlugin *xtr;
ut64 file_sz = UT64_MAX;
RBinFile *binfile = NULL;
int tfd = -1;
if (!io || (fd < 0) || (st64)sz < 0) {
return false;
}
bool is_debugger = iob->fd_is_dbg (io, fd);
const char *fname = iob->fd_get_name (io, fd);
if (loadaddr == UT64_MAX) {
loadaddr = 0;
}
file_sz = iob->fd_size (io, fd);
// file_sz = UT64_MAX happens when attaching to frida:// and other non-debugger io plugins which results in double opening
if (is_debugger && file_sz == UT64_MAX) {
tfd = iob->fd_open (io, fname, R_IO_READ, 0644);
if (tfd >= 1) {
file_sz = iob->fd_size (io, tfd);
}
}
if (!sz) {
sz = file_sz;
}
// check if blockdevice?
if (sz >= UT32_MAX) {
sz = 1024 * 32;
}
bin->file = fname;
sz = R_MIN (file_sz, sz);
if (!r_list_length (bin->binfiles)) {
if (is_debugger) {
//use the temporal RIODesc to read the content of the file instead
//from the memory
if (tfd >= 0) {
buf_bytes = calloc (1, sz + 1);
iob->fd_read_at (io, tfd, 0, buf_bytes, sz);
// iob->fd_close (io, tfd);
}
}
}
#if 0
if (!buf_bytes) {
if (sz < 1) {
eprintf ("Cannot allocate %d bytes\n", sz + 1);
return false;
}
buf_bytes = calloc (1, sz + 1);
if (!buf_bytes) {
eprintf ("Cannot allocate %d bytes.\n", sz + 1);
return false;
}
ut64 seekaddr = is_debugger? baseaddr: loadaddr;
if (!iob->fd_read_at (io, fd, seekaddr, buf_bytes, sz)) {
sz = 0LL;
}
}
#else
// this thing works for 2GB ELF core from vbox
if (!buf_bytes) {
if (sz < 1) {
eprintf ("Cannot allocate %d bytes\n", (int)(sz));
return false;
}
buf_bytes = calloc (1, sz);
if (!buf_bytes) {
eprintf ("Cannot allocate %d bytes.\n", (int)(sz + 1));
return false;
}
ut64 seekaddr = is_debugger? baseaddr: loadaddr;
if (!iob->fd_read_at (io, fd, seekaddr, buf_bytes, sz)) {
sz = 0LL;
}
}
#endif
if (bin->use_xtr && !name && (st64)sz > 0) {
// XXX - for the time being this is fine, but we may want to
// change the name to something like
// <xtr_name>:<bin_type_name>
r_list_foreach (bin->binxtrs, it, xtr) {
if (xtr && xtr->check_bytes (buf_bytes, sz)) {
if (xtr && (xtr->extract_from_bytes || xtr->extractall_from_bytes)) {
if (is_debugger && sz != file_sz) {
R_FREE (buf_bytes);
if (tfd < 0) {
tfd = iob->fd_open (io, fname, R_IO_READ, 0);
}
sz = iob->fd_size (io, tfd);
if (sz != UT64_MAX) {
buf_bytes = calloc (1, sz + 1);
if (buf_bytes) {
(void) iob->fd_read_at (io, tfd, 0, buf_bytes, sz);
}
}
// DOUBLECLOSE UAF : iob->fd_close (io, tfd);
tfd = -1; // marking it closed
} else if (sz != file_sz) {
(void) iob->read_at (io, 0LL, buf_bytes, sz);
}
binfile = r_bin_file_xtr_load_bytes (bin, xtr,
fname, buf_bytes, sz, file_sz,
baseaddr, loadaddr, xtr_idx,
fd, bin->rawstr);
}
xtr = NULL;
}
}
}
if (!binfile) {
if (true) {
binfile = r_bin_file_new_from_bytes (
bin, fname, buf_bytes, sz, file_sz, bin->rawstr,
baseaddr, loadaddr, fd, name, NULL, offset, true);
} else {
binfile = r_bin_file_new_from_fd (bin, tfd, NULL);
}
}
return binfile? r_bin_file_set_cur_binfile (bin, binfile): false;
}
static bool r_bin_load_io_at_offset_as(RBin *bin, int fd, ut64 baseaddr,
ut64 loadaddr, int xtr_idx, ut64 offset, const char *name) {
// adding file_sz to help reduce the performance impact on the system
// in this case the number of bytes read will be limited to 2MB
// (MIN_LOAD_SIZE)
// if it fails, the whole file is loaded.
const ut64 MAX_LOAD_SIZE = 0; // 0xfffff; //128 * (1 << 10 << 10);
int res = r_bin_load_io_at_offset_as_sz (bin, fd, baseaddr,
loadaddr, xtr_idx, offset, name, MAX_LOAD_SIZE);
if (!res) {
res = r_bin_load_io_at_offset_as_sz (bin, fd, baseaddr,
loadaddr, xtr_idx, offset, name, UT64_MAX);
}
return res;
}
R_API RBinPlugin *r_bin_get_binplugin_by_name(RBin *bin, const char *name) {
RBinPlugin *plugin;
RListIter *it;
if (bin && name) {
r_list_foreach (bin->plugins, it, plugin) {
if (!strcmp (plugin->name, name)) {
return plugin;
}
}
}
return NULL;
}
R_API RBinPlugin *r_bin_get_binplugin_by_bytes(RBin *bin, const ut8 *bytes, ut64 sz) {
RBinPlugin *plugin;
RListIter *it;
if (!bin || !bytes) {
return NULL;
}
r_list_foreach (bin->plugins, it, plugin) {
if (plugin->check_bytes && plugin->check_bytes (bytes, sz)) {
return plugin;
}
}
return NULL;
}
R_API RBinXtrPlugin *r_bin_get_xtrplugin_by_name(RBin *bin, const char *name) {
RBinXtrPlugin *xtr;
RListIter *it;
if (!bin || !name) {
return NULL;
}
// TODO: use a hashtable here
r_list_foreach (bin->binxtrs, it, xtr) {
if (!strcmp (xtr->name, name)) {
return xtr;
}
// must be set to null
xtr = NULL;
}
return NULL;
}
R_API RBinPlugin *r_bin_get_binplugin_any(RBin *bin) {
return r_bin_get_binplugin_by_name (bin, "any");
}
static void r_bin_plugin_free(RBinPlugin *p) {
if (p && p->fini) {
p->fini (NULL);
}
R_FREE (p);
}
// rename to r_bin_plugin_add like the rest
R_API bool r_bin_add(RBin *bin, RBinPlugin *foo) {
RListIter *it;
RBinPlugin *plugin;
if (foo->init) {
foo->init (bin->user);
}
r_list_foreach (bin->plugins, it, plugin) {
if (!strcmp (plugin->name, foo->name)) {
return false;
}
}
plugin = R_NEW0 (RBinPlugin);
memcpy (plugin, foo, sizeof (RBinPlugin));
r_list_append (bin->plugins, plugin);
return true;
}
R_API bool r_bin_ldr_add(RBin *bin, RBinLdrPlugin *foo) {
RListIter *it;
RBinLdrPlugin *ldr;
if (foo->init) {
foo->init (bin->user);
}
// avoid duplicates
r_list_foreach (bin->binldrs, it, ldr) {
if (!strcmp (ldr->name, foo->name)) {
return false;
}
}
r_list_append (bin->binldrs, foo);
return true;
}
R_API bool r_bin_xtr_add(RBin *bin, RBinXtrPlugin *foo) {
RListIter *it;
RBinXtrPlugin *xtr;
if (foo->init) {
foo->init (bin->user);
}
// avoid duplicates
r_list_foreach (bin->binxtrs, it, xtr) {
if (!strcmp (xtr->name, foo->name)) {
return false;
}
}
r_list_append (bin->binxtrs, foo);
return true;
}
R_API void *r_bin_free(RBin *bin) {
if (!bin) {
return NULL;
}
if (bin->io_owned) {
r_io_free (bin->iob.io);
}
bin->file = NULL;
free (bin->force);
free (bin->srcdir);
//r_bin_free_bin_files (bin);
r_list_free (bin->binfiles);
r_list_free (bin->binxtrs);
r_list_free (bin->plugins);
r_list_free (bin->binldrs);
sdb_free (bin->sdb);
r_id_pool_free (bin->file_ids);
memset (bin, 0, sizeof (RBin));
free (bin);
return NULL;
}
static bool r_bin_print_plugin_details(RBin *bin, RBinPlugin *bp, int json) {
if (json == 'q') {
bin->cb_printf ("%s\n", bp->name);
} else if (json) {
bin->cb_printf (
"{\"name\":\"%s\",\"description\":\"%s\","
"\"license\":\"%s\"}\n",
bp->name, bp->desc, bp->license? bp->license: "???");
} else {
bin->cb_printf ("Name: %s\n", bp->name);
bin->cb_printf ("Description: %s\n", bp->desc);
if (bp->license) {
bin->cb_printf ("License: %s\n", bp->license);
}
if (bp->version) {
bin->cb_printf ("Version: %s\n", bp->version);
}
if (bp->author) {
bin->cb_printf ("Author: %s\n", bp->author);
}
}
return true;
}
static int r_bin_print_xtrplugin_details(RBin *bin, RBinXtrPlugin *bx, int json) {
if (json == 'q') {
bin->cb_printf ("%s\n", bx->name);
} else if (json) {
bin->cb_printf (
"{\"name\":\"%s\",\"description\":\"%s\","
"\"license\":\"%s\"}\n",
bx->name, bx->desc, bx->license? bx->license: "???");
} else {
bin->cb_printf ("Name: %s\n", bx->name);
bin->cb_printf ("Description: %s\n", bx->desc);
if (bx->license) {
bin->cb_printf ("License: %s\n", bx->license);
}
}
return true;
}
R_API int r_bin_list(RBin *bin, int json) {
RListIter *it;
RBinPlugin *bp;
RBinXtrPlugin *bx;
RBinLdrPlugin *ld;
if (json == 'q') {
r_list_foreach (bin->plugins, it, bp) {
bin->cb_printf ("%s\n", bp->name);
}
r_list_foreach (bin->binxtrs, it, bx) {
bin->cb_printf ("%s\n", bx->name);
}
} else if (json) {
int i;
i = 0;
bin->cb_printf ("{\"bin\":[");
r_list_foreach (bin->plugins, it, bp) {
bin->cb_printf (
"%s{\"name\":\"%s\",\"description\":\"%s\","
"\"license\":\"%s\"}",
i? ",": "", bp->name, bp->desc, bp->license? bp->license: "???");
i++;
}
i = 0;
bin->cb_printf ("],\"xtr\":[");
r_list_foreach (bin->binxtrs, it, bx) {
bin->cb_printf (
"%s{\"name\":\"%s\",\"description\":\"%s\","
"\"license\":\"%s\"}",
i? ",": "", bx->name, bx->desc, bx->license? bx->license: "???");
i++;
}
i = 0;
bin->cb_printf ("],\"ldr\":[");
r_list_foreach (bin->binxtrs, it, ld) {
bin->cb_printf (
"%s{\"name\":\"%s\",\"description\":\"%s\","
"\"license\":\"%s\"}",
i? ",": "", ld->name, ld->desc, ld->license? ld->license: "???");
i++;
}
bin->cb_printf ("]}\n");
} else {
r_list_foreach (bin->plugins, it, bp) {
bin->cb_printf ("bin %-11s %s (%s) %s %s\n",
bp->name, bp->desc, bp->license? bp->license: "???",
bp->version? bp->version: "",
bp->author? bp->author: "");
}
r_list_foreach (bin->binxtrs, it, bx) {
const char *name = strncmp (bx->name, "xtr.", 4)? bx->name : bx->name + 3;
bin->cb_printf ("xtr %-11s %s (%s)\n", name,
bx->desc, bx->license? bx->license: "???");
}
r_list_foreach (bin->binldrs, it, ld) {
const char *name = strncmp (ld->name, "ldr.", 4)? ld->name : ld->name + 3;
bin->cb_printf ("ldr %-11s %s (%s)\n", name,
ld->desc, ld->license? ld->license: "???");
}
}
return false;
}
R_API int r_bin_list_plugin(RBin *bin, const char* name, int json) {
RListIter *it;
RBinPlugin *bp;
RBinXtrPlugin *bx;
r_list_foreach (bin->plugins, it, bp) {
if (!r_str_cmp (name, bp->name, strlen (name))) {
continue;
}
return r_bin_print_plugin_details (bin, bp, json);
}
r_list_foreach (bin->binxtrs, it, bx) {
if (!r_str_cmp (name, bx->name, strlen (name))) {
continue;
}
return r_bin_print_xtrplugin_details (bin, bx, json);
}
eprintf ("cannot find plugin %s\n", name);
return false;
}
/* returns the base address of bin or UT64_MAX in case of errors */
R_API ut64 r_bin_get_baddr(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return r_bin_object_get_baddr (o);
}
/* returns the load address of bin or UT64_MAX in case of errors */
R_API ut64 r_bin_get_laddr(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->loadaddr: UT64_MAX;
}
R_API void r_bin_set_baddr(RBin *bin, ut64 baddr) {
RBinObject *o = r_bin_cur_object (bin);
r_bin_object_set_baddr (o, baddr);
// XXX - update all the infos?
}
R_API ut64 r_bin_get_boffset(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->boffset: UT64_MAX;
}
R_API RBinAddr *r_bin_get_sym(RBin *bin, int sym) {
RBinObject *o = r_bin_cur_object (bin);
if (sym < 0 || sym >= R_BIN_SYM_LAST) {
return NULL;
}
return o? o->binsym[sym]: NULL;
}
// XXX: those accessors are redundant
R_API RList *r_bin_get_entries(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->entries: NULL;
}
R_API RList *r_bin_get_fields(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->fields: NULL;
}
R_API RList *r_bin_get_imports(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->imports: NULL;
}
R_API RBinInfo *r_bin_get_info(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->info: NULL;
}
R_API RList *r_bin_get_libs(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->libs: NULL;
}
R_API RList * r_bin_patch_relocs(RBin *bin) {
static bool first = true;
RBinObject *o = r_bin_cur_object (bin);
if (!o) {
return NULL;
}
// r_bin_object_set_items set o->relocs but there we don't have access
// to io
// so we need to be run from bin_relocs, free the previous reloc and get
// the patched ones
if (first && o->plugin && o->plugin->patch_relocs) {
RList *tmp = o->plugin->patch_relocs (bin);
first = false;
if (!tmp) {
return o->relocs;
}
r_list_free (o->relocs);
o->relocs = tmp;
REBASE_PADDR (o, o->relocs, RBinReloc);
first = false;
return o->relocs;
}
return o->relocs;
}
R_API RList *r_bin_get_relocs(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->relocs: NULL;
}
R_API RList *r_bin_get_sections(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->sections: NULL;
}
// TODO: Move into section.c and rename it to r_io_section_get_at ()
R_API RBinSection *r_bin_get_section_at(RBinObject *o, ut64 off, int va) {
RBinSection *section;
RListIter *iter;
ut64 from, to;
if (o) {
// TODO: must be O(1) .. use sdb here
r_list_foreach (o->sections, iter, section) {
from = va? binobj_a2b (o, section->vaddr): section->paddr;
to = va? (binobj_a2b (o, section->vaddr) + section->vsize) :
(section->paddr + section->size);
if (off >= from && off < to) {
return section;
}
}
}
return NULL;
}
R_API RList *r_bin_reset_strings(RBin *bin) {
RBinFile *a = r_bin_cur (bin);
RBinObject *o = r_bin_cur_object (bin);
RBinPlugin *plugin = r_bin_file_cur_plugin (a);
if (!a || !o) {
return NULL;
}
if (o->strings) {
r_list_free (o->strings);
o->strings = NULL;
}
if (bin->minstrlen <= 0) {
return NULL;
}
a->rawstr = bin->rawstr;
if (plugin && plugin->strings) {
o->strings = plugin->strings (a);
} else {
o->strings = r_bin_file_get_strings (a, bin->minstrlen, 0);
}
if (bin->debase64) {
r_bin_object_filter_strings (o);
}
return o->strings;
}
R_API RList *r_bin_get_strings(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->strings: NULL;
}
R_API int r_bin_is_string(RBin *bin, ut64 va) {
RBinString *string;
RListIter *iter;
RList *list;
if (!(list = r_bin_get_strings (bin))) {
return false;
}
r_list_foreach (list, iter, string) {
if (string->vaddr == va) {
return true;
}
if (string->vaddr > va) {
return false;
}
}
return false;
}
//callee must not free the symbol
R_API RBinSymbol *r_bin_get_symbol_at_vaddr(RBin *bin, ut64 addr) {
//use skiplist here
RList *symbols = r_bin_get_symbols (bin);
RListIter *iter;
RBinSymbol *symbol;
r_list_foreach (symbols, iter, symbol) {
if (symbol->vaddr == addr) {
return symbol;
}
}
return NULL;
}
//callee must not free the symbol
R_API RBinSymbol *r_bin_get_symbol_at_paddr(RBin *bin, ut64 addr) {
//use skiplist here
RList *symbols = r_bin_get_symbols (bin);
RListIter *iter;
RBinSymbol *symbol;
r_list_foreach (symbols, iter, symbol) {
if (symbol->paddr == addr) {
return symbol;
}
}
return NULL;
}
R_API RList *r_bin_get_symbols(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->symbols: NULL;
}
R_API RList *r_bin_get_mem(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? o->mem: NULL;
}
R_API int r_bin_is_big_endian(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return (o && o->info)? o->info->big_endian: -1;
}
R_API int r_bin_is_stripped(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? (R_BIN_DBG_STRIPPED & o->info->dbg_info): 1;
}
R_API int r_bin_is_static(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
if (o && r_list_length (o->libs) > 0)
return R_BIN_DBG_STATIC & o->info->dbg_info;
return true;
}
// TODO: Integrate with r_bin_dbg */
R_API int r_bin_has_dbg_linenums(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? (R_BIN_DBG_LINENUMS & o->info->dbg_info): false;
}
R_API int r_bin_has_dbg_syms(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? (R_BIN_DBG_SYMS & o->info->dbg_info): false;
}
R_API int r_bin_has_dbg_relocs(RBin *bin) {
RBinObject *o = r_bin_cur_object (bin);
return o? (R_BIN_DBG_RELOCS & o->info->dbg_info): false;