diff --git a/libr/bin/format/coff/coff.c b/libr/bin/format/coff/coff.c new file mode 100644 index 0000000000000..1304797d25b92 --- /dev/null +++ b/libr/bin/format/coff/coff.c @@ -0,0 +1,287 @@ +#include + +#include "coff.h" + +int coff_supported_arch(const ut8 *buf) +{ + ut16 arch = *(ut16*)buf; + int ret; + + switch (arch) { + case IMAGE_FILE_MACHINE_AMD64: + case IMAGE_FILE_MACHINE_I386: + case IMAGE_FILE_MACHINE_H8300: + case IMAGE_FILE_TI_COFF: + ret = R_TRUE; + break; + default: + ret = R_FALSE; + } + + return ret; +} + +static int r_bin_coff_init_hdr(struct r_bin_coff_obj *obj) +{ + size_t offset = 0; + + obj->hdr.machine = *(ut16*)obj->b->buf; + + switch(obj->hdr.machine) { + case IMAGE_FILE_MACHINE_H8300: + obj->endian = !LIL_ENDIAN; + break; + default: + obj->endian = LIL_ENDIAN; + } + + offset += sizeof(ut16); + + r_mem_copyendian((ut8*)&(obj->hdr.sections_num), obj->b->buf + offset, + sizeof(ut16), obj->endian); + + offset += sizeof(ut16); + + r_mem_copyendian((ut8*)&obj->hdr.timestamp, obj->b->buf + offset, + sizeof(ut32), obj->endian); + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&obj->hdr.symtable_offset, obj->b->buf + offset, + sizeof(ut32), obj->endian); + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->hdr.symbols_num), obj->b->buf + offset, + sizeof(ut32), obj->endian); + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->hdr.opt_hdr_size), obj->b->buf + offset, + sizeof(ut16), obj->endian); + offset += sizeof(ut16); + + r_mem_copyendian((ut8*)&(obj->hdr.flags), obj->b->buf + offset, + sizeof(ut16), obj->endian); + + if (obj->hdr.flags & IMAGE_FLAGS_TI_F_LITTLE) { + obj->endian = LIL_ENDIAN; + } + offset += sizeof(ut16); + + if (obj->hdr.machine == IMAGE_FILE_TI_COFF) { + r_mem_copyendian((ut8*)&(obj->hdr.target_id), obj->b->buf + offset, + sizeof(ut16), obj->endian); + } + + return R_TRUE; +} + +static int r_bin_coff_init_opt_hdr(struct r_bin_coff_obj *obj) +{ + size_t offset = 20; + + if (obj->hdr.opt_hdr_size == 0) + return 0; + + r_mem_copyendian((ut8*)&(obj->opt_hdr.magic), obj->b->buf + offset, + sizeof(ut16), obj->endian); + + offset += sizeof(ut16); + + r_mem_copyendian((ut8*)&(obj->opt_hdr.major_linker_version), + obj->b->buf + offset, sizeof(ut8), obj->endian); + + offset += sizeof(ut8); + + r_mem_copyendian((ut8*)&(obj->opt_hdr.minor_linker_version), + obj->b->buf + offset, sizeof(ut8), obj->endian); + + offset += sizeof(ut8); + + r_mem_copyendian((ut8*)&(obj->opt_hdr.size_of_code), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->opt_hdr.size_of_init_data), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->opt_hdr.size_of_uninit_data), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->opt_hdr.entry_point), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->opt_hdr.base_of_code), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + return 0; +} + +static int r_bin_coff_init_scn_hdr(struct r_bin_coff_obj *obj) +{ + size_t i, offset = obj->hdr.opt_hdr_size + 20; + + if (obj->hdr.machine == IMAGE_FILE_TI_COFF) { + offset += 2; + } + + obj->scn_hdrs = calloc(obj->hdr.sections_num, + sizeof(struct coff_scn_hdr)); + + for (i = 0; i < obj->hdr.sections_num; i++) { + strncpy(obj->scn_hdrs[i].name, (char*)(obj->b->buf + offset), 8); + + offset += 8; + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].virtual_size), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].virtual_addr), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].raw_data_size), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].raw_data_pointer), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].reloc_pointer), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].linenum_pointer), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].reloc_num), + obj->b->buf + offset, sizeof(ut16), obj->endian); + + offset += sizeof(ut16); + + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].linenum_num), + obj->b->buf + offset, sizeof(ut16), obj->endian); + + offset += sizeof(ut16); + + r_mem_copyendian((ut8*)&(obj->scn_hdrs[i].flags), + obj->b->buf + offset, sizeof(ut32), obj->endian); + + + offset += sizeof(ut32); + } + + return 0; +} + +static int r_bin_coff_init_symtable(struct r_bin_coff_obj *obj) +{ + size_t i, offset = obj->hdr.symtable_offset; + ut32 short_name, ofst; + + obj->symbols = calloc(obj->hdr.symbols_num, + sizeof(struct coff_symbol)); + + for (i = 0; i < obj->hdr.symbols_num; i++) { + r_mem_copyendian((ut8*)&short_name, obj->b->buf + offset, + sizeof(ut32), obj->endian); + + if (short_name) { + obj->symbols[i].name = malloc(sizeof(char) * 9); + strncpy(obj->symbols[i].name, + (char*)(obj->b->buf + offset), 8); + obj->symbols[i].name[8] = '\0'; + offset += 8; + } else { + offset += sizeof(ut32); + r_mem_copyendian((ut8*)&ofst, obj->b->buf + offset, + sizeof(ut32), obj->endian); + + obj->symbols[i].name = strdup((char*)(obj->b->buf + + obj->hdr.symtable_offset + ofst + + obj->hdr.symbols_num * 18)); + offset += sizeof(ut32); + } + + r_mem_copyendian((ut8*)&(obj->symbols[i].value), + obj->b->buf + offset, + sizeof(ut32), obj->endian); + + offset += sizeof(ut32); + + r_mem_copyendian((ut8*)&(obj->symbols[i].scn_num), + obj->b->buf + offset, + sizeof(ut16), obj->endian); + + offset += sizeof(ut16); + + r_mem_copyendian((ut8*)&(obj->symbols[i].type), + obj->b->buf + offset, + sizeof(ut16), obj->endian); + + offset += sizeof(ut16); + + r_mem_copyendian((ut8*)&(obj->symbols[i].storage_class), + obj->b->buf + offset, + sizeof(ut8), obj->endian); + + offset += sizeof(ut8); + + r_mem_copyendian((ut8*)&(obj->symbols[i].aux_sym_num), + obj->b->buf + offset, + sizeof(ut8), obj->endian); + + offset += sizeof(ut8); + } + + return 0; +} + +static int r_bin_coff_init(struct r_bin_coff_obj *obj, struct r_buf_t *buf) +{ + obj->b = buf; + obj->size = buf->length; + + r_bin_coff_init_hdr(obj); + r_bin_coff_init_opt_hdr(obj); + + r_bin_coff_init_scn_hdr(obj); + r_bin_coff_init_symtable(obj); + + return R_TRUE; +} + +void r_bin_coff_free(struct r_bin_coff_obj *obj) +{ + if (obj->scn_hdrs) + free(obj->scn_hdrs); + + if (obj->symbols) + free(obj->symbols); + + free(obj); +} + +struct r_bin_coff_obj* r_bin_coff_new_buf(struct r_buf_t *buf) +{ + struct r_bin_coff_obj* bin = R_NEW0(struct r_bin_coff_obj); + + r_bin_coff_init(bin, buf); + + return bin; +} diff --git a/libr/bin/format/coff/coff.h b/libr/bin/format/coff/coff.h new file mode 100644 index 0000000000000..151939af22957 --- /dev/null +++ b/libr/bin/format/coff/coff.h @@ -0,0 +1,48 @@ +/* radare - LGPL - Copyright 2014 Fedor Sakharov */ + +#ifndef COFF_H +#define COFF_H + +#include +#include + +#include "coff_specs.h" + +struct coff_scn_hdr { + char name[9]; + ut32 virtual_size; + ut32 virtual_addr; + ut32 raw_data_size; + ut32 raw_data_pointer; + ut32 reloc_pointer; + ut32 linenum_pointer; + ut16 reloc_num; + ut16 linenum_num; + ut32 flags; +}; + +struct coff_symbol { + char *name; + ut32 value; + ut16 scn_num; + ut16 type; + ut8 storage_class; + ut8 aux_sym_num; +}; + +struct r_bin_coff_obj { + struct coff_hdr hdr; + struct coff_opt_hdr opt_hdr; + struct coff_scn_hdr *scn_hdrs; + struct coff_symbol *symbols; + + struct r_buf_t *b; + size_t size; + ut8 endian; +}; + +int coff_supported_arch(const ut8 *buf); +struct r_bin_coff_obj* r_bin_coff_new_buf(struct r_buf_t *buf); +void r_bin_coff_free(struct r_bin_coff_obj *obj); + +#endif /* COFF_H */ diff --git a/libr/bin/format/coff/coff_specs.h b/libr/bin/format/coff/coff_specs.h new file mode 100644 index 0000000000000..efd54804b1e1d --- /dev/null +++ b/libr/bin/format/coff/coff_specs.h @@ -0,0 +1,154 @@ +/* radare - LGPL - Copyright 2014 - Fedor Sakharov */ +#ifndef COFF_SPECS_H +#define COFF_SPECS_H + +#include + +#define IMAGE_FILE_MACHINE_UNKNOWN 0x0 +#define IMAGE_FILE_MACHINE_AM33 0x1d3 +#define IMAGE_FILE_MACHINE_AMD64 0x8664 +#define IMAGE_FILE_MACHINE_ARM 0x1c0 +#define IMAGE_FILE_MACHINE_ARMNT 0x1c4 +#define IMAGE_FILE_MACHINE_ARM64 0xaa64 +#define IMAGE_FILE_MACHINE_EBC 0xebc +#define IMAGE_FILE_MACHINE_I386 0x14c +#define IMAGE_FILE_MACHINE_IA64 0x200 +#define IMAGE_FILE_MACHINE_M32R 0x9041 +#define IMAGE_FILE_MACHINE_MIPS16 0x266 +#define IMAGE_FILE_MACHINE_MIPSFPU 0x366 +#define IMAGE_FILE_MACHINE_MIPSFPU16 0x466 +#define IMAGE_FILE_MACHINE_POWERPC 0x1f0 +#define IMAGE_FILE_MACHINE_POWERPCFP 0x1f1 +#define IMAGE_FILE_MACHINE_R4000 0x166 +#define IMAGE_FILE_MACHINE_SH3 0x1a2 +#define IMAGE_FILE_MACHINE_SH3DSP 0x1a3 +#define IMAGE_FILE_MACHINE_SH4 0x1a6 +#define IMAGE_FILE_MACHINE_SH5 0x1a8 +#define IMAGE_FILE_MACHINE_THUMB 0x1c2 +#define IMAGE_FILE_MACHINE_WCEMIPSV2 0x169 +#define IMAGE_FILE_MACHINE_H8300 0x0083 + +#define IMAGE_FILE_TI_COFF 0xc1 +#define IMAGE_FILE_MACHINE_TMS470 0x0097 +#define IMAGE_FILE_MACHINE_TMS320C54 0x0098 +#define IMAGE_FILE_MACHINE_TMS320C60 0x0099 +#define IMAGE_FILE_MACHINE_TMS320C55 0x009C +#define IMAGE_FILE_MACHINE_TMS320C28 0x009D +#define IMAGE_FILE_MACHINE_MSP430 0x00A0 +#define IMAGE_FILE_MACHINE_TMS320C55PLUS 0x00A1 + +#define IMAGE_FLAGS_TI_F_RELFLG 0x0001 +#define IMAGE_FLAGS_TI_F_EXEC 0x0002 +#define IMAGE_FLAGS_TI_F_LNNO 0x0004 +#define IMAGE_FLAGS_TI_F_LSYMS 0x0008 +#define IMAGE_FLAGS_TI_F_BIG 0x0200 +#define IMAGE_FLAGS_TI_F_LITTLE 0x0100 + +#define IMAGE_SCN_TYPE_NO_PAD 0x00000008 +#define IMAGE_SCN_CNT_CODE 0x00000020 +#define IMAGE_SCN_CNT_INIT_DATA 0x00000040 +#define IMAGE_SCN_LNK_OTHER 0x00000100 +#define IMAGE_SCN_LNK_INFO 0x00000200 +#define IMAGE_SCN_LNK_REMOVE 0x00000800 +#define IMAGE_SCN_LNK_COMDAT 0x00001000 +#define IMAGE_SCN_GPREL 0x00008000 +#define IMAGE_SCN_MEM_PURGEABLE 0x00010000 +#define IMAGE_SCN_MEM_16BIT 0x00020000 +#define IMAGE_SCN_MEM_LOCKED 0x00040000 +#define IMAGE_SCN_MEM_PRELOAD 0x00080000 +#define IMAGE_SCN_ALIGN_1BYTES 0x00100000 +#define IMAGE_SCN_ALIGN_2BYTES 0x00200000 +#define IMAGE_SCN_ALIGN_4BYTES 0x00300000 +#define IMAGE_SCN_ALIGN_8BYTES 0x00400000 +#define IMAGE_SCN_ALIGN_16BYTES 0x00500000 +#define IMAGE_SCN_ALIGN_32BYTES 0x00600000 +#define IMAGE_SCN_ALIGN_64BYTES 0x00700000 +#define IMAGE_SCN_ALIGN_128BYTES 0x00800000 +#define IMAGE_SCN_ALIGN_256BYTES 0x00900000 +#define IMAGE_SCN_ALIGN_512BYTES 0x00A00000 +#define IMAGE_SCN_ALIGN_1024BYTES 0x00B00000 +#define IMAGE_SCN_ALIGN_2048BYTES 0x00C00000 +#define IMAGE_SCN_ALIGN_4096BYTES 0x00D00000 +#define IMAGE_SCN_ALIGN_8192BYTES 0x00E00000 +#define IMAGE_SCN_LNK_NRELOC_OVFL 0x01000000 +#define IMAGE_SCN_MEM_DISCARDABLE 0x02000000 +#define IMAGE_SCN_MEM_NOT_CACHED 0x04000000 +#define IMAGE_SCN_MEM_NOT_PAGED 0x08000000 +#define IMAGE_SCN_MEM_SHARED 0x10000000 +#define IMAGE_SCN_MEM_EXECUTE 0x20000000 +#define IMAGE_SCN_MEM_READ 0x40000000 +#define IMAGE_SCN_MEM_WRITE 0x80000000 + +#define IMAGE_SYM_TYPE_NULL 0 +#define IMAGE_SYM_TYPE_VOID 1 +#define IMAGE_SYM_TYPE_CHAR 2 +#define IMAGE_SYM_TYPE_SHORT 3 +#define IMAGE_SYM_TYPE_INT 4 +#define IMAGE_SYM_TYPE_LONG 5 +#define IMAGE_SYM_TYPE_FLOAT 6 +#define IMAGE_SYM_TYPE_DOUBLE 7 +#define IMAGE_SYM_TYPE_STRUCT 8 +#define IMAGE_SYM_TYPE_UNION 9 +#define IMAGE_SYM_TYPE_ENUM 10 +#define IMAGE_SYM_TYPE_MOE 11 +#define IMAGE_SYM_TYPE_BYTE 12 +#define IMAGE_SYM_TYPE_WORD 13 +#define IMAGE_SYM_TYPE_UINT 14 +#define IMAGE_SYM_TYPE_DWORD 15 + +#define IMAGE_SYM_DTYPE_NULL 0 +#define IMAGE_SYM_DTYPE_POINTER 1 +#define IMAGE_SYM_DTYPE_FUNCTION 2 +#define IMAGE_SYM_DTYPE_ARRAY 3 + +#define IMAGE_SYM_CLASS_END_OF_FUNCTION 0xFF +#define IMAGE_SYM_CLASS_NULL 0 +#define IMAGE_SYM_CLASS_AUTOMATIC 1 +#define IMAGE_SYM_CLASS_EXTERNAL 2 +#define IMAGE_SYM_CLASS_STATIC 3 +#define IMAGE_SYM_CLASS_REGISTER 4 +#define IMAGE_SYM_CLASS_EXTERNAL_DEF 5 +#define IMAGE_SYM_CLASS_LABEL 6 +#define IMAGE_SYM_CLASS_UNDEFINED_LABEL 7 +#define IMAGE_SYM_CLASS_MEMBER_OF_STRUCT 8 +#define IMAGE_SYM_CLASS_ARGUMENT 9 +#define IMAGE_SYM_CLASS_STRUCT_TAG 10 +#define IMAGE_SYM_CLASS_MEMBER_OF_UNION 11 +#define IMAGE_SYM_CLASS_UNION_TAG 12 +#define IMAGE_SYM_CLASS_TYPE_DEFINITION 13 +#define IMAGE_SYM_CLASS_UNDEFINED_STATIC 14 +#define IMAGE_SYM_CLASS_ENUM_TAG 15 +#define IMAGE_SYM_CLASS_MEMBER_OF_ENUM 16 +#define IMAGE_SYM_CLASS_REGISTER_PARAM 17 +#define IMAGE_SYM_CLASS_BIT_FIELD 18 +#define IMAGE_SYM_CLASS_BLOCK 100 +#define IMAGE_SYM_CLASS_FUNCTION 101 +#define IMAGE_SYM_CLASS_END_OF_STRUCT 102 +#define IMAGE_SYM_CLASS_FILE 103 +#define IMAGE_SYM_CLASS_SECTION 104 +#define IMAGE_SYM_CLASS_WEAK_EXTERNAL 105 +#define IMAGE_SYM_CLASS_CLR_TOKEN 107 + +struct coff_hdr { + ut16 machine; + ut16 sections_num; + ut32 timestamp; + ut32 symtable_offset; + ut32 symbols_num; + ut16 opt_hdr_size; + ut16 flags; + ut16 target_id; // TI COFF specific +}; + +struct coff_opt_hdr { + ut16 magic; + ut8 major_linker_version; + ut8 minor_linker_version; + ut32 size_of_code; + ut32 size_of_init_data; + ut32 size_of_uninit_data; + ut32 entry_point; + ut32 base_of_code; +}; + +#endif /* COFF_SPECS_H */ diff --git a/libr/bin/p/Makefile b/libr/bin/p/Makefile index 618c11c962de6..4f7d85449585b 100644 --- a/libr/bin/p/Makefile +++ b/libr/bin/p/Makefile @@ -9,7 +9,7 @@ foo: all ALL_TARGETS= FORMATS=any.mk elf.mk elf64.mk pe.mk pe64.mk te.mk mach0.mk FORMATS+=bios.mk mach064.mk fatmach0.mk dyldcache.mk java.mk -FORMATS+=dex.mk fs.mk ningb.mk +FORMATS+=dex.mk fs.mk ningb.mk coff.mk include $(FORMATS) all: ${ALL_TARGETS} diff --git a/libr/bin/p/bin_coff.c b/libr/bin/p/bin_coff.c new file mode 100644 index 0000000000000..bd68fa126a0c7 --- /dev/null +++ b/libr/bin/p/bin_coff.c @@ -0,0 +1,254 @@ +/* radare - LGPL - Copyright 2014 - Fedor Sakharov */ + +#include +#include +#include +#include + +#include "coff/coff.h" + +static int load(RBinFile *arch) +{ + if (!(arch->o->bin_obj = r_bin_coff_new_buf(arch->buf))) + return R_FALSE; + return R_TRUE; +} + +static int destroy(RBinFile *arch) +{ + r_bin_coff_free((struct r_bin_coff_obj*)arch->o->bin_obj); + return R_TRUE; +} + +static ut64 baddr(RBinFile *arch) +{ + return 0; +} + +static RBinAddr *binsym(RBinFile *arch, int sym) +{ + return NULL; +} + +static RList *entries(RBinFile *arch) +{ + size_t i; + RList *ret; + RBinAddr *ptr = NULL; + struct r_bin_coff_obj *obj = (struct r_bin_coff_obj*)arch->o->bin_obj; + + if (!(ret = r_list_new ())) + return NULL; + + ret->free = free; + + if (!(ptr = R_NEW (RBinAddr))) + return ret; + + memset (ptr, '\0', sizeof (RBinAddr)); + + if (obj->hdr.opt_hdr_size) { + ptr->offset = ptr->rva = obj->opt_hdr.entry_point; + } else { + for (i = 0; i < obj->hdr.sections_num; i++) { + if (!strcmp(obj->scn_hdrs[i].name, ".text")) { + ptr->offset = obj->scn_hdrs[i].virtual_addr; + ptr->rva = obj->scn_hdrs[i].virtual_addr; + break; + } + } + } + + r_list_append(ret, ptr); + + return ret; +} + +static RList *sections(RBinFile *arch) +{ + size_t i; + RList *ret = NULL; + RBinSection *ptr = NULL; + struct r_bin_coff_obj *obj = (struct r_bin_coff_obj*)arch->o->bin_obj; + + ret = r_list_new(); + + if (!ret) + return NULL; + + for (i = 0; i < obj->hdr.sections_num; i++) { + ptr = R_NEW0 (RBinSection); + + strncpy(ptr->name, obj->scn_hdrs[i].name, R_BIN_SIZEOF_STRINGS); + + ptr->size = obj->scn_hdrs[i].raw_data_size; + ptr->vsize = obj->scn_hdrs[i].virtual_size; + ptr->offset = obj->scn_hdrs[i].raw_data_pointer; + ptr->rva = obj->scn_hdrs[i].virtual_addr; + + r_list_append (ret, ptr); + } + + return ret; +} + +static RList *symbols(RBinFile *arch) +{ + size_t i; + RList *ret = NULL; + RBinSymbol *ptr = NULL; + + struct r_bin_coff_obj *obj= (struct r_bin_coff_obj*)arch->o->bin_obj; + + if (!(ret = r_list_new())) + return ret; + + ret->free = free; + + for (i = 0; i < obj->hdr.symbols_num; i++) { + if (!(ptr = R_NEW0 (RBinSymbol))) + break; + + strncpy (ptr->name, obj->symbols[i].name, + R_BIN_SIZEOF_STRINGS); + strncpy (ptr->forwarder, "NONE", + R_BIN_SIZEOF_STRINGS); + strncpy (ptr->bind, "", R_BIN_SIZEOF_STRINGS); + strncpy (ptr->type, "UNKNOWN", R_BIN_SIZEOF_STRINGS); + ptr->rva = obj->symbols[i].value; + ptr->offset = obj->symbols[i].value; + ptr->size = 0; + ptr->ordinal = 0; + + r_list_append (ret, ptr); + + i += obj->symbols[i].aux_sym_num; + } + + return ret; +} + +static RList *imports(RBinFile *arch) +{ + return NULL; +} + +static RList *libs(RBinFile *arch) +{ + return NULL; +} + +static RList *relocs(RBinFile *arch) +{ + return NULL; +} + +static RBinInfo *info(RBinFile *arch) +{ + RBinInfo *ret = R_NEW0(RBinInfo); + ret->has_va = 1; + struct r_bin_coff_obj *obj = (struct r_bin_coff_obj*)arch->o->bin_obj; + + strncpy (ret->file, arch->file, R_BIN_SIZEOF_STRINGS); + strncpy (ret->rpath, "NONE", R_BIN_SIZEOF_STRINGS); + ret->big_endian = obj->endian; + ret->dbg_info = 0; + + switch (obj->hdr.machine) { + case IMAGE_FILE_MACHINE_I386: + strncpy(ret->machine, "i386", R_BIN_SIZEOF_STRINGS); + strncpy(ret->arch, "x86", R_BIN_SIZEOF_STRINGS); + ret->bits = 32; + break; + case IMAGE_FILE_MACHINE_AMD64: + strncpy(ret->machine, "AMD 64", R_BIN_SIZEOF_STRINGS); + strncpy(ret->arch, "x86", R_BIN_SIZEOF_STRINGS); + ret->bits = 64; + break; + case IMAGE_FILE_MACHINE_H8300: + strncpy(ret->machine, "H8300", R_BIN_SIZEOF_STRINGS); + strncpy(ret->arch, "h8300", R_BIN_SIZEOF_STRINGS); + ret->bits = 16; + break; + + case IMAGE_FILE_TI_COFF: + if (obj->hdr.target_id == IMAGE_FILE_MACHINE_TMS320C54) { + strncpy(ret->machine, "c54x", R_BIN_SIZEOF_STRINGS); + strncpy(ret->arch, "tms320", R_BIN_SIZEOF_STRINGS); + ret->bits = 32; + } else if (obj->hdr.target_id == IMAGE_FILE_MACHINE_TMS320C55) { + strncpy(ret->machine, "c55x", R_BIN_SIZEOF_STRINGS); + strncpy(ret->arch, "tms320", R_BIN_SIZEOF_STRINGS); + ret->bits = 32; + } else if (obj->hdr.target_id == IMAGE_FILE_MACHINE_TMS320C55PLUS) { + strncpy(ret->machine, "c55x+", R_BIN_SIZEOF_STRINGS); + strncpy(ret->arch, "tms320", R_BIN_SIZEOF_STRINGS); + ret->bits = 32; + } + break; + default: + strncpy(ret->machine, "unknown", R_BIN_SIZEOF_STRINGS); + } + + return ret; +} + +static RList *fields(RBinFile *arch) +{ + return NULL; +} + +static RBuffer *create(RBin *bin, const ut8 *code, int codelen, + const ut8 *data, int datalen) +{ + return NULL; +} + +static int size(RBinFile *arch) +{ + return 0; +} + +static int check(RBinFile *arch) +{ + if (arch && arch->buf && arch->buf->buf) { + if (coff_supported_arch(arch->buf->buf)) + return R_TRUE; + } + return R_FALSE; +} + +RBinPlugin r_bin_plugin_coff = { + .name = "coff", + .desc = "COFF format r_bin plugin", + .license = "LGPL3", + .init = NULL, + .fini = NULL, + .load = &load, + .destroy = &destroy, + .check = &check, + .baddr = &baddr, + .boffset = NULL, + .binsym = &binsym, + .entries = &entries, + .sections = §ions, + .symbols = &symbols, + .imports = &imports, + .strings = NULL, + .info = &info, + .fields = &fields, + .size = &size, + .libs = &libs, + .relocs = &relocs, + .meta = NULL, + .create = &create, + .write = NULL, + .get_vaddr = NULL, +}; + +#ifndef CORELIB +struct r_lib_struct_t radare_plugin = { + .type = R_LIB_TYPE_BIN, + .data = &r_bin_plugin_coff +}; +#endif diff --git a/libr/bin/p/coff.mk b/libr/bin/p/coff.mk new file mode 100644 index 0000000000000..3914508f55e94 --- /dev/null +++ b/libr/bin/p/coff.mk @@ -0,0 +1,10 @@ +OBJ_COFF=bin_coff.o +OBJ_COFF+=../format/coff/coff.o + +STATIC_OBJ+=${OBJ_COFF} +TARGET_COFF=bin_coff.${EXT_SO} + +ALL_TARGETS+=${TARGET_COFF} + +${TARGET_COFF}: ${OBJ_COFF} + ${CC} $(call libname,bin_coff) ${CFLAGS} $(LDFLAGS) ${OBJ_COFF} diff --git a/libr/include/r_bin.h b/libr/include/r_bin.h index 77ffca11c3b70..762d713a9237c 100644 --- a/libr/include/r_bin.h +++ b/libr/include/r_bin.h @@ -384,6 +384,7 @@ extern RBinPlugin r_bin_plugin_dex; extern RBinPlugin r_bin_plugin_dummy; extern RBinPlugin r_bin_plugin_rar; extern RBinPlugin r_bin_plugin_ningb; +extern RBinPlugin r_bin_plugin_coff; extern RBinXtrPlugin r_bin_xtr_plugin_fatmach0; extern RBinXtrPlugin r_bin_xtr_plugin_dyldcache; diff --git a/plugins.def.cfg b/plugins.def.cfg index bdfea0db81bf3..cab2b82fd447a 100644 --- a/plugins.def.cfg +++ b/plugins.def.cfg @@ -71,6 +71,7 @@ bin.pe bin.mz bin.pe64 bin.te +bin.coff bin.mach0 bin.mach064 bin.ningb