diff --git a/include/perk/perk.h b/include/perk/perk.h index d5ede02..16b60c4 100644 --- a/include/perk/perk.h +++ b/include/perk/perk.h @@ -203,6 +203,9 @@ perk_api int pe_output_export_symbols (const struct pe_driver_ctx *, const str perk_api int pe_output_import_libraries(const struct pe_driver_ctx *, const struct pe_image_meta *); perk_api int pe_output_mdso_libraries (const struct pe_driver_ctx *, const struct pe_image_meta *); +/* header dump api */ +perk_api int pe_hdrdump_image_dos_hdr (const struct pe_driver_ctx *, const struct pe_image_meta *); + /* error trace api */ perk_api int pe_output_error_record (const struct pe_driver_ctx *, const struct pe_error_info *); perk_api int pe_output_error_vector (const struct pe_driver_ctx *); diff --git a/project/common.mk b/project/common.mk index d71c08b..b12a040 100644 --- a/project/common.mk +++ b/project/common.mk @@ -2,6 +2,7 @@ API_SRCS = \ src/driver/pe_amain.c \ src/driver/pe_driver_ctx.c \ src/driver/pe_unit_ctx.c \ + src/hdrdump/pe_hdrdump_image_dos_hdr.c \ src/info/pe_get_image_abi.c \ src/info/pe_get_image_framework.c \ src/info/pe_get_image_subsystem.c \ diff --git a/project/tree.mk b/project/tree.mk index 7606638..e33a0f5 100644 --- a/project/tree.mk +++ b/project/tree.mk @@ -1,6 +1,7 @@ tree.tag: mkdir -p src mkdir -p src/driver + mkdir -p src/hdrdump mkdir -p src/info mkdir -p src/internal mkdir -p src/logic diff --git a/src/hdrdump/pe_hdrdump_image_dos_hdr.c b/src/hdrdump/pe_hdrdump_image_dos_hdr.c new file mode 100644 index 0000000..b413565 --- /dev/null +++ b/src/hdrdump/pe_hdrdump_image_dos_hdr.c @@ -0,0 +1,74 @@ +/***************************************************************/ +/* perk: PE Resource Kit */ +/* Copyright (C) 2015--2017 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.PERK. */ +/***************************************************************/ + +#include + +#include +#include +#include "perk_driver_impl.h" +#include "perk_hdrdump_impl.h" +#include "perk_dprintf_impl.h" +#include "perk_errinfo_impl.h" + +#define PE_ADDR meta->r_dos +#define PE_RAW_STRUCT image_dos_hdr +#define PE_OUTPUT(x) PE_OUTPUT_TABLE(x) + +int pe_hdrdump_image_dos_hdr( + const struct pe_driver_ctx * dctx, + const struct pe_image_meta * meta) +{ + int bits; + uint64_t faddr; + uint64_t vaddr; + char * ch; + char buf[8192]; + + if (!PE_ADDR) + return 0; + + if ((bits = pe_image_bits(meta)) < 0) + return PERK_CUSTOM_ERROR( + dctx,PERK_ERR_UNSUPPORTED_ABI); + + faddr = 0; + vaddr = meta->m_opt.oh_mem.coh_image_base; + + ch = buf; + ch += pe_output_hex_header( + ch, + "struct pe_raw_image_dos_hdr", + faddr,vaddr,bits); + + ch += PE_OUTPUT(dos_magic); + ch += PE_OUTPUT(dos_cblp); + ch += PE_OUTPUT(dos_cp); + ch += PE_OUTPUT(dos_crlc); + ch += PE_OUTPUT(dos_cparhdr); + ch += PE_OUTPUT(dos_minalloc); + ch += PE_OUTPUT(dos_maxalloc); + ch += PE_OUTPUT(dos_ss); + ch += PE_OUTPUT(dos_sp); + ch += PE_OUTPUT(dos_csum); + ch += PE_OUTPUT(dos_ip); + ch += PE_OUTPUT(dos_cs); + ch += PE_OUTPUT(dos_lfarlc); + ch += PE_OUTPUT(dos_ovno); + ch += PE_OUTPUT(dos_res); + ch += PE_OUTPUT(dos_oemid); + ch += PE_OUTPUT(dos_oeminfo); + ch += PE_OUTPUT(dos_res2); + ch += PE_OUTPUT(dos_lfanew); + + ch += pe_output_hex_footer(ch); + + *ch = 0; + + if (pe_dprintf(pe_driver_fdout(dctx),"%s",buf) < 0) + return PERK_FILE_ERROR(dctx); + + return 0; +} diff --git a/src/internal/perk_hdrdump_impl.h b/src/internal/perk_hdrdump_impl.h index 41528ad..5adb63d 100644 --- a/src/internal/perk_hdrdump_impl.h +++ b/src/internal/perk_hdrdump_impl.h @@ -46,4 +46,18 @@ size_t pe_output_raw_element( uint32_t moffset, uint32_t msize); +static inline int pe_image_bits(const struct pe_image_meta * m) +{ + switch (m->m_abi) { + case PE_ABI_PE32: + return 32; + + case PE_ABI_PE64: + return 64; + + default: + return -1; + } +} + #endif