1 #ifndef R2_BIN_H 2 #define R2_BIN_H 3 4 #include <r_util.h> 5 #include <r_types.h> 6 #include <r_io.h> 7 #include <r_cons.h> 8 #include <r_list.h> 9 10 typedef struct r_bin_t RBin; 11 12 #include <r_bin_dwarf.h> 13 #include <r_pdb.h> 14 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 R_LIB_VERSION_HEADER (r_bin); 20 21 #define R_BIN_DBG_STRIPPED 0x01 22 #define R_BIN_DBG_STATIC 0x02 23 #define R_BIN_DBG_LINENUMS 0x04 24 #define R_BIN_DBG_SYMS 0x08 25 #define R_BIN_DBG_RELOCS 0x10 26 27 #define R_BIN_ENTRY_TYPE_PROGRAM 0 28 #define R_BIN_ENTRY_TYPE_MAIN 1 29 #define R_BIN_ENTRY_TYPE_INIT 2 30 #define R_BIN_ENTRY_TYPE_FINI 3 31 #define R_BIN_ENTRY_TYPE_TLS 4 32 #define R_BIN_ENTRY_TYPE_PREINIT 5 33 34 #define R_BIN_SIZEOF_STRINGS 512 35 #define R_BIN_MAX_ARCH 1024 36 37 #define R_BIN_REQ_ALL UT64_MAX 38 #define R_BIN_REQ_UNK 0x000000 39 #define R_BIN_REQ_ENTRIES 0x000001 40 #define R_BIN_REQ_IMPORTS 0x000002 41 #define R_BIN_REQ_SYMBOLS 0x000004 42 #define R_BIN_REQ_SECTIONS 0x000008 43 #define R_BIN_REQ_INFO 0x000010 44 #define R_BIN_REQ_OPERATION 0x000020 45 #define R_BIN_REQ_HELP 0x000040 46 #define R_BIN_REQ_STRINGS 0x000080 47 #define R_BIN_REQ_FIELDS 0x000100 48 #define R_BIN_REQ_LIBS 0x000200 49 #define R_BIN_REQ_SRCLINE 0x000400 50 #define R_BIN_REQ_MAIN 0x000800 51 #define R_BIN_REQ_EXTRACT 0x001000 52 #define R_BIN_REQ_RELOCS 0x002000 53 #define R_BIN_REQ_LISTARCHS 0x004000 54 #define R_BIN_REQ_CREATE 0x008000 55 #define R_BIN_REQ_CLASSES 0x010000 56 #define R_BIN_REQ_DWARF 0x020000 57 #define R_BIN_REQ_SIZE 0x040000 58 #define R_BIN_REQ_PDB 0x080000 59 #define R_BIN_REQ_PDB_DWNLD 0x100000 60 #define R_BIN_REQ_DLOPEN 0x200000 61 #define R_BIN_REQ_EXPORTS 0x400000 62 #define R_BIN_REQ_VERSIONINFO 0x800000 63 #define R_BIN_REQ_PACKAGE 0x1000000 64 #define R_BIN_REQ_HEADER 0x2000000 65 #define R_BIN_REQ_LISTPLUGINS 0x4000000 66 #define R_BIN_REQ_RESOURCES 0x8000000 67 #define R_BIN_REQ_INITFINI 0x10000000 68 #define R_BIN_REQ_SEGMENTS 0x20000000 69 #define R_BIN_REQ_HASHES 0x40000000 70 #define R_BIN_REQ_SIGNATURE 0x80000000 71 #define R_BIN_REQ_TRYCATCH 0x100000000 72 #define R_BIN_REQ_SECTIONS_MAPPING 0x200000000 73 74 /* RBinSymbol->method_flags : */ 75 #define R_BIN_METH_CLASS 0x0000000000000001L 76 #define R_BIN_METH_STATIC 0x0000000000000002L 77 #define R_BIN_METH_PUBLIC 0x0000000000000004L 78 #define R_BIN_METH_PRIVATE 0x0000000000000008L 79 #define R_BIN_METH_PROTECTED 0x0000000000000010L 80 #define R_BIN_METH_INTERNAL 0x0000000000000020L 81 #define R_BIN_METH_OPEN 0x0000000000000040L 82 #define R_BIN_METH_FILEPRIVATE 0x0000000000000080L 83 #define R_BIN_METH_FINAL 0x0000000000000100L 84 #define R_BIN_METH_VIRTUAL 0x0000000000000200L 85 #define R_BIN_METH_CONST 0x0000000000000400L 86 #define R_BIN_METH_MUTATING 0x0000000000000800L 87 #define R_BIN_METH_ABSTRACT 0x0000000000001000L 88 #define R_BIN_METH_SYNCHRONIZED 0x0000000000002000L 89 #define R_BIN_METH_NATIVE 0x0000000000004000L 90 #define R_BIN_METH_BRIDGE 0x0000000000008000L 91 #define R_BIN_METH_VARARGS 0x0000000000010000L 92 #define R_BIN_METH_SYNTHETIC 0x0000000000020000L 93 #define R_BIN_METH_STRICT 0x0000000000040000L 94 #define R_BIN_METH_MIRANDA 0x0000000000080000L 95 #define R_BIN_METH_CONSTRUCTOR 0x0000000000100000L 96 #define R_BIN_METH_DECLARED_SYNCHRONIZED 0x0000000000200000L 97 98 #define R_BIN_BIND_LOCAL_STR "LOCAL" 99 #define R_BIN_BIND_GLOBAL_STR "GLOBAL" 100 #define R_BIN_BIND_WEAK_STR "WEAK" 101 #define R_BIN_BIND_NUM_STR "NUM" 102 #define R_BIN_BIND_LOOS_STR "LOOS" 103 #define R_BIN_BIND_HIOS_STR "HIOS" 104 #define R_BIN_BIND_LOPROC_STR "LOPROC" 105 #define R_BIN_BIND_HIPROC_STR "HIPROC" 106 #define R_BIN_BIND_UNKNOWN_STR "UNKNOWN" 107 108 #define R_BIN_TYPE_NOTYPE_STR "NOTYPE" 109 #define R_BIN_TYPE_OBJECT_STR "OBJ" 110 #define R_BIN_TYPE_FUNC_STR "FUNC" 111 #define R_BIN_TYPE_METH_STR "METH" 112 #define R_BIN_TYPE_STATIC_STR "STATIC" 113 #define R_BIN_TYPE_SECTION_STR "SECT" 114 #define R_BIN_TYPE_FILE_STR "FILE" 115 #define R_BIN_TYPE_COMMON_STR "COMMON" 116 #define R_BIN_TYPE_TLS_STR "TLS" 117 #define R_BIN_TYPE_NUM_STR "NUM" 118 #define R_BIN_TYPE_LOOS_STR "LOOS" 119 #define R_BIN_TYPE_HIOS_STR "HIOS" 120 #define R_BIN_TYPE_LOPROC_STR "LOPROC" 121 #define R_BIN_TYPE_HIPROC_STR "HIPROC" 122 #define R_BIN_TYPE_SPECIAL_SYM_STR "SPCL" 123 #define R_BIN_TYPE_UNKNOWN_STR "UNK" 124 125 enum { 126 R_BIN_SYM_ENTRY, 127 R_BIN_SYM_INIT, 128 R_BIN_SYM_MAIN, 129 R_BIN_SYM_FINI, 130 R_BIN_SYM_LAST 131 }; 132 133 // name mangling types 134 // TODO: Rename to R_BIN_LANG_ 135 enum { 136 R_BIN_NM_NONE = 0, 137 R_BIN_NM_JAVA = 1, 138 R_BIN_NM_C = 1<<1, 139 R_BIN_NM_GO = 1<<2, 140 R_BIN_NM_CXX = 1<<3, 141 R_BIN_NM_OBJC = 1<<4, 142 R_BIN_NM_SWIFT = 1<<5, 143 R_BIN_NM_DLANG = 1<<6, 144 R_BIN_NM_MSVC = 1<<7, 145 R_BIN_NM_RUST = 1<<8, 146 R_BIN_NM_KOTLIN = 1<<9, 147 R_BIN_NM_BLOCKS = 1<<31, 148 R_BIN_NM_ANY = -1, 149 }; 150 151 enum { 152 R_STRING_TYPE_DETECT = '?', 153 R_STRING_TYPE_ASCII = 'a', 154 R_STRING_TYPE_UTF8 = 'u', 155 R_STRING_TYPE_WIDE = 'w', // utf16 / widechar string 156 R_STRING_TYPE_WIDE32 = 'W', // utf32 157 R_STRING_TYPE_BASE64 = 'b', 158 }; 159 160 enum { 161 R_BIN_CLASS_PRIVATE, 162 R_BIN_CLASS_PUBLIC, 163 R_BIN_CLASS_FRIENDLY, 164 R_BIN_CLASS_PROTECTED, 165 }; 166 167 enum { 168 R_BIN_RELOC_8 = 8, 169 R_BIN_RELOC_16 = 16, 170 R_BIN_RELOC_32 = 32, 171 R_BIN_RELOC_64 = 64 172 }; 173 174 enum { 175 R_BIN_TYPE_DEFAULT = 0, 176 R_BIN_TYPE_CORE = 1 177 }; 178 179 typedef struct r_bin_addr_t { 180 ut64 vaddr; 181 ut64 paddr; 182 ut64 hvaddr; 183 ut64 hpaddr; 184 int type; 185 int bits; 186 } RBinAddr; 187 188 typedef struct r_bin_hash_t { 189 const char *type; 190 ut64 addr; 191 int len; 192 ut64 from; 193 ut64 to; 194 ut8 buf[32]; 195 const char *cmd; 196 } RBinHash; 197 198 typedef struct r_bin_file_hash_t { 199 const char *type; 200 const char *hex; 201 } RBinFileHash; 202 203 typedef struct r_bin_info_t { 204 char *file; 205 char *type; 206 char *bclass; 207 char *rclass; 208 char *arch; 209 char *cpu; 210 char *machine; 211 char *head_flag; 212 char *features; 213 char *os; 214 char *subsystem; 215 char *rpath; 216 char *guid; 217 char *debug_file_name; 218 const char *lang; 219 char *default_cc; 220 RList/*<RBinFileHash>*/ *file_hashes; 221 int bits; 222 int has_va; 223 int has_pi; // pic/pie 224 int has_canary; 225 int has_retguard; 226 int has_sanitizers; 227 int has_crypto; 228 int has_nx; 229 int big_endian; 230 bool has_lit; 231 char *actual_checksum; 232 char *claimed_checksum; 233 int pe_overlay; 234 bool signature; 235 ut64 dbg_info; 236 RBinHash sum[3]; 237 ut64 baddr; 238 char *intrp; 239 char *compiler; 240 } RBinInfo; 241 242 typedef struct r_bin_object_t { 243 ut64 baddr; 244 st64 baddr_shift; 245 ut64 loadaddr; 246 ut64 boffset; 247 ut64 size; 248 ut64 obj_size; 249 RList/*<RBinSection>*/ *sections; 250 RList/*<RBinImport>*/ *imports; 251 RList/*<RBinSymbol>*/ *symbols; 252 RList/*<??>*/ *entries; 253 RList/*<??>*/ *fields; 254 RList/*<??>*/ *libs; 255 RBNode/*<RBinReloc>*/ *relocs; 256 RList/*<??>*/ *strings; 257 RList/*<RBinClass>*/ *classes; 258 HtPP *classes_ht; 259 HtPP *methods_ht; 260 RList/*<RBinDwarfRow>*/ *lines; 261 HtUP *strings_db; 262 RList/*<??>*/ *mem; //RBinMem maybe? 263 RList/*<BinMap*/ *maps; 264 char *regstate; 265 RBinInfo *info; 266 RBinAddr *binsym[R_BIN_SYM_LAST]; 267 struct r_bin_plugin_t *plugin; 268 int lang; 269 Sdb *kv; 270 HtUP *addr2klassmethod; 271 void *bin_obj; // internal pointer used by formats 272 } RBinObject; 273 274 // XXX: RbinFile may hold more than one RBinObject 275 /// XX curplugin == o->plugin 276 typedef struct r_bin_file_t { 277 char *file; 278 int fd; 279 int size; 280 int rawstr; 281 int strmode; 282 ut32 id; 283 RBuffer *buf; 284 ut64 offset; 285 RBinObject *o; 286 void *xtr_obj; 287 ut64 loadaddr; 288 /* values used when searching the strings */ 289 int minstrlen; 290 int maxstrlen; 291 int narch; 292 struct r_bin_xtr_plugin_t *curxtr; 293 // struct r_bin_plugin_t *curplugin; // use o->plugin 294 RList *xtr_data; 295 Sdb *sdb; 296 Sdb *sdb_info; 297 Sdb *sdb_addrinfo; 298 struct r_bin_t *rbin; 299 } RBinFile; 300 301 typedef struct r_bin_file_options_t { 302 int rawstr; 303 ut64 baddr; // base address 304 ut64 laddr; // load address 305 ut64 paddr; // offset 306 const char *plugname; // force a plugin? why do i need this? 307 // const char *xtrname; 308 } RBinFileOptions; 309 310 struct r_bin_t { 311 const char *file; 312 RBinFile *cur; // TODO: deprecate 313 int narch; 314 void *user; 315 /* preconfigured values */ 316 int debase64; 317 int minstrlen; 318 int maxstrlen; 319 ut64 maxstrbuf; 320 int rawstr; 321 Sdb *sdb; 322 RIDStorage *ids; 323 RList/*<RBinPlugin>*/ *plugins; 324 RList/*<RBinXtrPlugin>*/ *binxtrs; 325 RList/*<RBinLdrPlugin>*/ *binldrs; 326 RList/*<RBinFile>*/ *binfiles; 327 PrintfCallback cb_printf; 328 int loadany; 329 RIOBind iob; 330 RConsBind consb; 331 char *force; 332 bool want_dbginfo; 333 int filter; // symbol filtering 334 char strfilter; // string filtering 335 char *strpurge; // purge false positive strings 336 char *srcdir; // dir.source 337 char *prefix; // bin.prefix 338 char *strenc; 339 ut64 filter_rules; 340 bool demanglercmd; 341 bool verbose; 342 bool use_xtr; // use extract plugins when loading a file? 343 bool use_ldr; // use loader plugins when loading a file? 344 RStrConstPool constpool; 345 bool is_reloc_patched; // used to indicate whether relocations were patched or not 346 }; 347 348 typedef struct r_bin_xtr_metadata_t { 349 char *arch; 350 int bits; 351 char *libname; 352 char *machine; 353 char *type; 354 const char *xtr_type; 355 } RBinXtrMetadata; 356 357 typedef int (*FREE_XTR)(void *xtr_obj); 358 typedef struct r_bin_xtr_extract_t { 359 char *file; 360 RBuffer *buf; 361 ut64 size; 362 ut64 offset; 363 ut64 baddr; 364 ut64 laddr; 365 int file_count; 366 int loaded; 367 RBinXtrMetadata *metadata; 368 } RBinXtrData; 369 370 R_API RBinXtrData *r_bin_xtrdata_new(RBuffer *buf, ut64 offset, ut64 size, ut32 file_count, RBinXtrMetadata *metadata); 371 R_API void r_bin_xtrdata_free(void /*RBinXtrData*/ *data); 372 373 typedef struct r_bin_xtr_plugin_t { 374 char *name; 375 char *desc; 376 char *license; 377 int (*init)(void *user); 378 int (*fini)(void *user); 379 bool (*check_buffer)(RBuffer *b); 380 381 RBinXtrData *(*extract_from_bytes)(RBin *bin, const ut8 *buf, ut64 size, int idx); 382 RBinXtrData *(*extract_from_buffer)(RBin *bin, RBuffer *buf, int idx); 383 RList *(*extractall_from_bytes)(RBin *bin, const ut8 *buf, ut64 size); 384 RList *(*extractall_from_buffer)(RBin *bin, RBuffer *buf); 385 RBinXtrData *(*extract)(RBin *bin, int idx); 386 RList *(*extractall)(RBin *bin); 387 388 bool (*load)(RBin *bin); 389 int (*size)(RBin *bin); 390 void (*destroy)(RBin *bin); 391 void (*free_xtr)(void *xtr_obj); 392 } RBinXtrPlugin; 393 394 typedef struct r_bin_ldr_plugin_t { 395 char *name; 396 char *desc; 397 char *license; 398 int (*init)(void *user); 399 int (*fini)(void *user); 400 bool (*load)(RBin *bin); 401 } RBinLdrPlugin; 402 403 typedef struct r_bin_arch_options_t { 404 const char *arch; 405 int bits; 406 } RBinArchOptions; 407 408 typedef struct r_bin_trycatch_t { 409 ut64 source; 410 ut64 from; 411 ut64 to; 412 ut64 handler; 413 ut64 filter; 414 // TODO: add type/name of exception 415 } RBinTrycatch; 416 417 R_API RBinTrycatch *r_bin_trycatch_new(ut64 source, ut64 from, ut64 to, ut64 handler, ut64 filter); 418 R_API void r_bin_trycatch_free(RBinTrycatch *tc); 419 420 typedef struct r_bin_plugin_t { 421 char *name; 422 char *desc; 423 char *author; 424 char *version; 425 char *license; 426 int (*init)(void *user); 427 int (*fini)(void *user); 428 Sdb * (*get_sdb)(RBinFile *obj); 429 bool (*load_buffer)(RBinFile *bf, void **bin_obj, RBuffer *buf, ut64 loadaddr, Sdb *sdb); 430 ut64 (*size)(RBinFile *bin); // return ut64 maybe? meh 431 void (*destroy)(RBinFile *bf); 432 bool (*check_bytes)(const ut8 *buf, ut64 length); 433 bool (*check_buffer)(RBuffer *buf); 434 ut64 (*baddr)(RBinFile *bf); 435 ut64 (*boffset)(RBinFile *bf); 436 RBinAddr* (*binsym)(RBinFile *bf, int num); 437 RList/*<RBinAddr>*/* (*entries)(RBinFile *bf); 438 RList/*<RBinSection>*/* (*sections)(RBinFile *bf); 439 R_BORROW RList/*<RBinDwarfRow>*/* (*lines)(RBinFile *bf); 440 RList/*<RBinSymbol>*/* (*symbols)(RBinFile *bf); 441 RList/*<RBinImport>*/* (*imports)(RBinFile *bf); 442 RList/*<RBinString>*/* (*strings)(RBinFile *bf); 443 RBinInfo/*<RBinInfo>*/* (*info)(RBinFile *bf); 444 RList/*<RBinField>*/* (*fields)(RBinFile *bf); 445 RList/*<char *>*/* (*libs)(RBinFile *bf); 446 RList/*<RBinReloc>*/* (*relocs)(RBinFile *bf); 447 RList/*<RBinTrycatch>*/* (*trycatch)(RBinFile *bf); 448 RList/*<RBinClass>*/* (*classes)(RBinFile *bf); 449 RList/*<RBinMem>*/* (*mem)(RBinFile *bf); 450 RList/*<RBinReloc>*/* (*patch_relocs)(RBin *bin); 451 RList/*<RBinMap>*/* (*maps)(RBinFile *bf); 452 RList/*<RBinFileHash>*/* (*hashes)(RBinFile *bf); 453 void (*header)(RBinFile *bf); 454 char* (*signature)(RBinFile *bf, bool json); 455 int (*demangle_type)(const char *str); 456 struct r_bin_dbginfo_t *dbginfo; 457 struct r_bin_write_t *write; 458 int (*get_offset)(RBinFile *bf, int type, int idx); 459 const char* (*get_name)(RBinFile *bf, int type, int idx, bool simplified); 460 ut64 (*get_vaddr)(RBinFile *bf, ut64 baddr, ut64 paddr, ut64 vaddr); 461 RBuffer* (*create)(RBin *bin, const ut8 *code, int codelen, const ut8 *data, int datalen, RBinArchOptions *opt); 462 char* (*demangle)(const char *str); 463 char* (*regstate)(RBinFile *bf); 464 int (*file_type)(RBinFile *bf); 465 /* default value if not specified by user */ 466 int minstrlen; 467 char strfilter; 468 void *user; 469 } RBinPlugin; 470 471 typedef void (*RBinSymbollCallback)(RBinObject *obj, void *symbol); 472 473 typedef struct r_bin_section_t { 474 char *name; 475 ut64 size; 476 ut64 vsize; 477 ut64 vaddr; 478 ut64 paddr; 479 ut32 perm; 480 // per section platform info 481 const char *arch; 482 char *format; 483 int bits; 484 bool has_strings; 485 bool add; // indicates when you want to add the section to io `S` command 486 bool is_data; 487 bool is_segment; 488 } RBinSection; 489 490 typedef struct r_bin_class_t { 491 char *name; 492 // TODO: char *module; 493 char *super; 494 char *visibility_str; // XXX only used by java 495 int index; 496 ut64 addr; 497 RList *methods; // <RBinSymbol> 498 RList *fields; // <RBinField> 499 // RList *interfaces; // <char *> 500 int visibility; 501 } RBinClass; 502 503 #define RBinSectionName r_offsetof(RBinSection, name) 504 #define RBinSectionOffset r_offsetof(RBinSection, offset) 505 506 #define REBASE_PADDR(o, l, type_t)\ 507 do { \ 508 RListIter *_it;\ 509 type_t *_el;\ 510 r_list_foreach ((l), _it, _el) { \ 511 _el->paddr += (o)->loadaddr;\ 512 }\ 513 } while (0) 514 515 516 typedef struct r_bin_symbol_t { 517 /* heap-allocated */ 518 char *name; 519 char *dname; 520 char *libname; 521 char *classname; 522 /* const-unique-strings */ 523 const char *forwarder; 524 const char *bind; 525 const char *type; 526 const char *rtype; 527 bool is_imported; 528 /* only used by java */ 529 const char *visibility_str; 530 // ---------------- 531 //char descriptor[R_BIN_SIZEOF_STRINGS+1]; 532 ut64 vaddr; 533 ut64 paddr; 534 ut32 size; 535 ut32 ordinal; 536 ut32 visibility; 537 int bits; 538 /* see R_BIN_METH_* constants */ 539 ut64 method_flags; 540 int dup_count; 541 } RBinSymbol; 542 543 typedef struct r_bin_import_t { 544 char *name; 545 char *libname; 546 const char *bind; 547 const char *type; 548 char *classname; 549 char *descriptor; 550 ut32 ordinal; 551 ut32 visibility; 552 } RBinImport; 553 554 typedef struct r_bin_reloc_t { 555 ut8 type; 556 ut8 additive; 557 RBinSymbol *symbol; 558 RBinImport *import; 559 st64 addend; 560 ut64 vaddr; 561 ut64 paddr; 562 ut32 visibility; 563 /* is_ifunc: indirect function, `addend` points to a resolver function 564 * that returns the actual relocation value, e.g. chooses 565 * an optimized version depending on the CPU. 566 * cf. https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html 567 */ 568 bool is_ifunc; 569 RBNode vrb; 570 } RBinReloc; 571 572 typedef struct r_bin_string_t { 573 // TODO: rename string->name (avoid colisions) 574 char *string; 575 ut64 vaddr; 576 ut64 paddr; 577 ut32 ordinal; 578 ut32 size; // size of buffer containing the string in bytes 579 ut32 length; // length of string in chars 580 char type; // Ascii Wide cp850 utf8 base64 ... 581 } RBinString; 582 583 typedef struct r_bin_field_t { 584 ut64 vaddr; 585 ut64 paddr; 586 int size; 587 int offset; 588 ut32 visibility; 589 char *name; 590 char *type; 591 char *comment; 592 char *format; 593 bool format_named; // whether format is the name of a format or a raw pf format string 594 ut64 flags; 595 } RBinField; 596 597 R_API RBinField *r_bin_field_new(ut64 paddr, ut64 vaddr, int size, const char *name, const char *comment, const char *format, bool format_named); 598 R_API void r_bin_field_free(void *); 599 600 typedef struct r_bin_mem_t { 601 char *name; 602 ut64 addr; 603 int size; 604 int perms; 605 RList *mirrors; //for mirror access; stuff here should only create new maps not new fds 606 } RBinMem; 607 608 typedef struct r_bin_map_t { 609 ut64 addr; 610 ut64 offset; 611 int size; 612 int perms; 613 char *file; 614 } RBinMap; 615 616 typedef struct r_bin_dbginfo_t { 617 bool (*get_line)(RBinFile *arch, ut64 addr, char *file, int len, int *line); 618 } RBinDbgInfo; 619 620 typedef struct r_bin_write_t { 621 ut64 (*scn_resize)(RBinFile *bf, const char *name, ut64 size); 622 bool (*scn_perms)(RBinFile *bf, const char *name, int perms); 623 int (*rpath_del)(RBinFile *bf); 624 bool (*entry)(RBinFile *bf, ut64 addr); 625 bool (*addlib)(RBinFile *bf, const char *lib); 626 } RBinWrite; 627 628 // TODO: deprecate r_bin_is_big_endian 629 // TODO: has_dbg_syms... maybe flags? 630 631 typedef int (*RBinGetOffset)(RBin *bin, int type, int idx); 632 typedef const char *(*RBinGetName)(RBin *bin, int type, int idx, bool sd); 633 typedef RList *(*RBinGetSections)(RBin *bin); 634 typedef RBinSection *(*RBinGetSectionAt)(RBin *bin, ut64 addr); 635 typedef char *(*RBinDemangle)(RBinFile *bf, const char *def, const char *str, ut64 vaddr, bool libs); 636 637 typedef struct r_bin_bind_t { 638 RBin *bin; 639 RBinGetOffset get_offset; 640 RBinGetName get_name; 641 RBinGetSections get_sections; 642 RBinGetSectionAt get_vsect_at; 643 RBinDemangle demangle; 644 ut32 visibility; 645 } RBinBind; 646 647 R_IPI RBinSection *r_bin_section_new(const char *name); 648 R_IPI void r_bin_section_free(RBinSection *bs); 649 R_API void r_bin_info_free(RBinInfo *rb); 650 R_API void r_bin_import_free(void *_imp); 651 R_API void r_bin_symbol_free(void *_sym); 652 R_API RBinSymbol *r_bin_symbol_new(const char *name, ut64 paddr, ut64 vaddr); 653 R_API void r_bin_string_free(void *_str); 654 655 #ifdef R_API 656 657 typedef struct r_bin_options_t { 658 const char *pluginname; 659 ut64 baseaddr; // where the linker maps the binary in memory 660 ut64 loadaddr; // starting physical address to read from the target file 661 ut64 sz; 662 int xtr_idx; // load Nth binary 663 int rawstr; 664 int fd; 665 const char *filename; 666 } RBinOptions; 667 668 R_API RBinImport *r_bin_import_clone(RBinImport *o); 669 R_API const char *r_bin_symbol_name(RBinSymbol *s); 670 typedef void (*RBinSymbolCallback)(RBinObject *obj, RBinSymbol *symbol); 671 672 // options functions 673 R_API void r_bin_options_init(RBinOptions *opt, int fd, ut64 baseaddr, ut64 loadaddr, int rawstr); 674 R_API void r_bin_arch_options_init(RBinArchOptions *opt, const char *arch, int bits); 675 676 // open/close/reload functions 677 R_API RBin *r_bin_new(void); 678 R_API void r_bin_free(RBin *bin); 679 R_API bool r_bin_open(RBin *bin, const char *file, RBinOptions *opt); 680 R_API bool r_bin_open_io(RBin *bin, RBinOptions *opt); 681 R_API bool r_bin_open_buf(RBin *bin, RBuffer *buf, RBinOptions *opt); 682 R_API bool r_bin_reload(RBin *bin, ut32 bf_id, ut64 baseaddr); 683 684 // plugins/bind functions 685 R_API void r_bin_bind(RBin *b, RBinBind *bnd); 686 R_API bool r_bin_add(RBin *bin, RBinPlugin *foo); 687 R_API bool r_bin_xtr_add(RBin *bin, RBinXtrPlugin *foo); 688 R_API bool r_bin_ldr_add(RBin *bin, RBinLdrPlugin *foo); 689 R_API void r_bin_list(RBin *bin, PJ *pj, int format); 690 R_API bool r_bin_list_plugin(RBin *bin, const char *name, PJ *pj, int json); 691 R_API RBinPlugin *r_bin_get_binplugin_by_bytes(RBin *bin, const ut8 *bytes, ut64 sz); 692 R_API RBinPlugin *r_bin_get_binplugin_by_buffer(RBin *bin, RBuffer *buf); 693 R_API void r_bin_force_plugin(RBin *bin, const char *pname); 694 695 // get/set various bin information 696 R_API ut64 r_bin_get_baddr(RBin *bin); 697 R_API ut64 r_bin_file_get_baddr(RBinFile *bf); 698 R_API void r_bin_set_user_ptr(RBin *bin, void *user); 699 R_API RBinInfo *r_bin_get_info(RBin *bin); 700 R_API void r_bin_set_baddr(RBin *bin, ut64 baddr); 701 R_API ut64 r_bin_get_laddr(RBin *bin); 702 R_API ut64 r_bin_get_size(RBin *bin); 703 R_API RBinAddr *r_bin_get_sym(RBin *bin, int sym); 704 R_API RList *r_bin_raw_strings(RBinFile *a, int min); 705 R_API RList *r_bin_dump_strings(RBinFile *a, int min, int raw); 706 707 // use RBinFile instead 708 R_API RList *r_bin_get_entries(RBin *bin); 709 R_API RList *r_bin_get_fields(RBin *bin); 710 R_API RList *r_bin_get_imports(RBin *bin); 711 R_API RList *r_bin_get_libs(RBin *bin); 712 R_API RBNode *r_bin_patch_relocs(RBin *bin); 713 R_API RList *r_bin_patch_relocs_list(RBin *bin); 714 R_API RBNode *r_bin_get_relocs(RBin *bin); 715 R_API RList *r_bin_get_relocs_list(RBin *bin); 716 R_API RList *r_bin_get_sections(RBin *bin); 717 R_API RList *r_bin_get_classes(RBin *bin); 718 R_API RList *r_bin_get_strings(RBin *bin); 719 R_API RList *r_bin_file_get_trycatch(RBinFile *bf); 720 R_API RList *r_bin_get_symbols(RBin *bin); 721 R_API RList *r_bin_reset_strings(RBin *bin); 722 R_API int r_bin_is_string(RBin *bin, ut64 va); 723 R_API int r_bin_is_big_endian(RBin *bin); 724 R_API int r_bin_is_static(RBin *bin); 725 R_API ut64 r_bin_get_vaddr(RBin *bin, ut64 paddr, ut64 vaddr); 726 R_API ut64 r_bin_file_get_vaddr(RBinFile *bf, ut64 paddr, ut64 vaddr); 727 R_API ut64 r_bin_a2b(RBin *bin, ut64 addr); 728 729 R_API int r_bin_load_languages(RBinFile *binfile); 730 R_API RBinFile *r_bin_cur(RBin *bin); 731 R_API RBinObject *r_bin_cur_object(RBin *bin); 732 733 // select/list binfiles functions 734 R_API bool r_bin_select(RBin *bin, const char *arch, int bits, const char *name); 735 R_API bool r_bin_select_bfid(RBin *bin, ut32 bf_id); 736 R_API bool r_bin_use_arch(RBin *bin, const char *arch, int bits, const char *name); 737 R_API void r_bin_list_archs(RBin *bin, PJ *pj, int mode); 738 R_API RBuffer *r_bin_create(RBin *bin, const char *plugin_name, const ut8 *code, int codelen, const ut8 *data, int datalen, RBinArchOptions *opt); 739 R_API RBuffer *r_bin_package(RBin *bin, const char *type, const char *file, RList *files); 740 741 R_API const char *r_bin_string_type(int type); 742 R_API const char *r_bin_entry_type_string(int etype); 743 744 R_API bool r_bin_file_object_new_from_xtr_data(RBin *bin, RBinFile *bf, ut64 baseaddr, ut64 loadaddr, RBinXtrData *data); 745 746 747 // RBinFile lifecycle 748 // R_IPI RBinFile *r_bin_file_new(RBin *bin, const char *file, ut64 file_sz, int rawstr, int fd, const char *xtrname, Sdb *sdb, bool steal_ptr); 749 R_API bool r_bin_file_close(RBin *bin, int bd); 750 R_API void r_bin_file_free(void /*RBinFile*/ *bf_); 751 // RBinFile.get 752 R_API RBinFile *r_bin_file_at(RBin *bin, ut64 addr); 753 R_API RBinFile *r_bin_file_find_by_object_id(RBin *bin, ut32 binobj_id); 754 R_API RList *r_bin_file_get_symbols(RBinFile *bf); 755 // 756 R_API ut64 r_bin_file_get_vaddr(RBinFile *bf, ut64 paddr, ut64 vaddr); 757 // RBinFile.add 758 R_API RBinClass *r_bin_file_add_class(RBinFile *binfile, const char *name, const char *super, int view); 759 R_API RBinSymbol *r_bin_file_add_method(RBinFile *bf, const char *classname, const char *name, int nargs); 760 R_API RBinField *r_bin_file_add_field(RBinFile *binfile, const char *classname, const char *name); 761 // RBinFile.find 762 R_API RBinFile *r_bin_file_find_by_arch_bits(RBin *bin, const char *arch, int bits); 763 R_API RBinFile *r_bin_file_find_by_id(RBin *bin, ut32 bin_id); 764 R_API RBinFile *r_bin_file_find_by_fd(RBin *bin, ut32 bin_fd); 765 R_API RBinFile *r_bin_file_find_by_name(RBin *bin, const char *name); 766 767 R_API bool r_bin_file_set_cur_binfile(RBin *bin, RBinFile *bf); 768 R_API bool r_bin_file_set_cur_by_name(RBin *bin, const char *name); 769 R_API bool r_bin_file_deref(RBin *bin, RBinFile *a); 770 R_API bool r_bin_file_set_cur_by_fd(RBin *bin, ut32 bin_fd); 771 R_API bool r_bin_file_set_cur_by_id(RBin *bin, ut32 bin_id); 772 R_API bool r_bin_file_set_cur_by_name(RBin *bin, const char *name); 773 R_API ut64 r_bin_file_delete_all(RBin *bin); 774 R_API bool r_bin_file_delete(RBin *bin, ut32 bin_id); 775 R_API RList *r_bin_file_compute_hashes(RBin *bin, ut64 limit); 776 R_API RList *r_bin_file_set_hashes(RBin *bin, RList *new_hashes); 777 R_API RBinPlugin *r_bin_file_cur_plugin(RBinFile *binfile); 778 R_API void r_bin_file_hash_free(RBinFileHash *fhash); 779 780 // binobject functions 781 R_API int r_bin_object_set_items(RBinFile *binfile, RBinObject *o); 782 R_API bool r_bin_object_delete(RBin *bin, ut32 binfile_id); 783 R_API void r_bin_mem_free(void *data); 784 785 // demangle functions 786 R_API char *r_bin_demangle(RBinFile *binfile, const char *lang, const char *str, ut64 vaddr, bool libs); 787 R_API char *r_bin_demangle_java(const char *str); 788 R_API char *r_bin_demangle_cxx(RBinFile *binfile, const char *str, ut64 vaddr); 789 R_API char *r_bin_demangle_msvc(const char *str); 790 R_API char *r_bin_demangle_swift(const char *s, bool syscmd); 791 R_API char *r_bin_demangle_objc(RBinFile *binfile, const char *sym); 792 R_API char *r_bin_demangle_rust(RBinFile *binfile, const char *str, ut64 vaddr); 793 R_API int r_bin_demangle_type(const char *str); 794 R_API void r_bin_demangle_list(RBin *bin); 795 R_API char *r_bin_demangle_plugin(RBin *bin, const char *name, const char *str); 796 R_API const char *r_bin_get_meth_flag_string(ut64 flag, bool compact); 797 798 R_API RBinSection *r_bin_get_section_at(RBinObject *o, ut64 off, int va); 799 800 /* dbginfo.c */ 801 R_API bool r_bin_addr2line(RBin *bin, ut64 addr, char *file, int len, int *line); 802 R_API char *r_bin_addr2text(RBin *bin, ut64 addr, int origin); 803 R_API char *r_bin_addr2fileline(RBin *bin, ut64 addr); 804 /* bin_write.c */ 805 R_API bool r_bin_wr_addlib(RBin *bin, const char *lib); 806 R_API ut64 r_bin_wr_scn_resize(RBin *bin, const char *name, ut64 size); 807 R_API bool r_bin_wr_scn_perms(RBin *bin, const char *name, int perms); 808 R_API bool r_bin_wr_rpath_del(RBin *bin); 809 R_API bool r_bin_wr_entry(RBin *bin, ut64 addr); 810 R_API bool r_bin_wr_output(RBin *bin, const char *filename); 811 812 R_API RList *r_bin_get_mem(RBin *bin); 813 814 /* filter.c */ 815 R_API void r_bin_load_filter(RBin *bin, ut64 rules); 816 R_API void r_bin_filter_symbols(RBinFile *bf, RList *list); 817 R_API void r_bin_filter_sections(RBinFile *bf, RList *list); 818 R_API char *r_bin_filter_name(RBinFile *bf, Sdb *db, ut64 addr, char *name); 819 R_API void r_bin_filter_sym(RBinFile *bf, HtPP *ht, ut64 vaddr, RBinSymbol *sym); 820 R_API bool r_bin_strpurge(RBin *bin, const char *str, ut64 addr); 821 R_API bool r_bin_string_filter(RBin *bin, const char *str, ut64 addr); 822 823 /* plugin pointers */ 824 extern RBinPlugin r_bin_plugin_any; 825 extern RBinPlugin r_bin_plugin_fs; 826 extern RBinPlugin r_bin_plugin_cgc; 827 extern RBinPlugin r_bin_plugin_elf; 828 extern RBinPlugin r_bin_plugin_elf64; 829 extern RBinPlugin r_bin_plugin_p9; 830 extern RBinPlugin r_bin_plugin_ne; 831 extern RBinPlugin r_bin_plugin_le; 832 extern RBinPlugin r_bin_plugin_pe; 833 extern RBinPlugin r_bin_plugin_mz; 834 extern RBinPlugin r_bin_plugin_pe64; 835 extern RBinPlugin r_bin_plugin_pebble; 836 extern RBinPlugin r_bin_plugin_bios; 837 extern RBinPlugin r_bin_plugin_bf; 838 extern RBinPlugin r_bin_plugin_te; 839 extern RBinPlugin r_bin_plugin_symbols; 840 extern RBinPlugin r_bin_plugin_mach0; 841 extern RBinPlugin r_bin_plugin_mach064; 842 extern RBinPlugin r_bin_plugin_mdmp; 843 extern RBinPlugin r_bin_plugin_java; 844 extern RBinPlugin r_bin_plugin_dex; 845 extern RBinPlugin r_bin_plugin_coff; 846 extern RBinPlugin r_bin_plugin_ningb; 847 extern RBinPlugin r_bin_plugin_ningba; 848 extern RBinPlugin r_bin_plugin_ninds; 849 extern RBinPlugin r_bin_plugin_nin3ds; 850 extern RBinPlugin r_bin_plugin_xbe; 851 extern RBinPlugin r_bin_plugin_bflt; 852 extern RBinXtrPlugin r_bin_xtr_plugin_xtr_fatmach0; 853 extern RBinXtrPlugin r_bin_xtr_plugin_xtr_dyldcache; 854 extern RBinXtrPlugin r_bin_xtr_plugin_xtr_pemixed; 855 extern RBinXtrPlugin r_bin_xtr_plugin_xtr_sep64; 856 extern RBinLdrPlugin r_bin_ldr_plugin_ldr_linux; 857 extern RBinPlugin r_bin_plugin_zimg; 858 extern RBinPlugin r_bin_plugin_omf; 859 extern RBinPlugin r_bin_plugin_art; 860 extern RBinPlugin r_bin_plugin_bootimg; 861 extern RBinPlugin r_bin_plugin_dol; 862 extern RBinPlugin r_bin_plugin_nes; 863 extern RBinPlugin r_bin_plugin_qnx; 864 extern RBinPlugin r_bin_plugin_mbn; 865 extern RBinPlugin r_bin_plugin_smd; 866 extern RBinPlugin r_bin_plugin_sms; 867 extern RBinPlugin r_bin_plugin_psxexe; 868 extern RBinPlugin r_bin_plugin_vsf; 869 extern RBinPlugin r_bin_plugin_dyldcache; 870 extern RBinPlugin r_bin_plugin_xnu_kernelcache; 871 extern RBinPlugin r_bin_plugin_avr; 872 extern RBinPlugin r_bin_plugin_menuet; 873 extern RBinPlugin r_bin_plugin_wasm; 874 extern RBinPlugin r_bin_plugin_nro; 875 extern RBinPlugin r_bin_plugin_nso; 876 extern RBinPlugin r_bin_plugin_sfc; 877 extern RBinPlugin r_bin_plugin_z64; 878 extern RBinPlugin r_bin_plugin_prg; 879 extern RBinPlugin r_bin_plugin_dmp64; 880 extern RBinPlugin r_bin_plugin_pyc; 881 882 #ifdef __cplusplus 883 } 884 #endif 885 886 #endif 887 #endif 888