diff options
-rw-r--r-- | include/perk/perk.h | 2 | ||||
-rw-r--r-- | project/common.mk | 1 | ||||
-rw-r--r-- | src/driver/pe_amain.c | 3 | ||||
-rw-r--r-- | src/output/pe_output_error.c | 1 | ||||
-rw-r--r-- | src/output/pe_output_mdso_libraries.c | 196 |
5 files changed, 203 insertions, 0 deletions
diff --git a/include/perk/perk.h b/include/perk/perk.h index e012a96..45d2fc1 100644 --- a/include/perk/perk.h +++ b/include/perk/perk.h @@ -61,6 +61,7 @@ enum pe_custom_error { PERK_ERR_BAD_DOS_HEADER, PERK_ERR_BAD_COFF_HEADER, PERK_ERR_BAD_IMAGE_TYPE, + PERK_ERR_UNSUPPORTED_ABI, PERK_ERR_CAP, }; @@ -181,6 +182,7 @@ perk_api int pe_output_image_symbols (const struct pe_driver_ctx *, const struc perk_api int pe_output_image_strings (const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *); perk_api int pe_output_export_symbols (const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *); perk_api int pe_output_import_libraries(const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *); +perk_api int pe_output_mdso_libraries (const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *); /* error trace api */ perk_api int pe_output_error_record (const struct pe_driver_ctx *, const struct pe_error_info *); diff --git a/project/common.mk b/project/common.mk index 1dfe22c..1b513e8 100644 --- a/project/common.mk +++ b/project/common.mk @@ -15,6 +15,7 @@ API_SRCS = \ src/output/pe_output_image_strings.c \ src/output/pe_output_image_symbols.c \ src/output/pe_output_idata_libraries.c \ + src/output/pe_output_mdso_libraries.c \ src/reader/pe_read_coff_header.c \ src/reader/pe_read_coff_symbol.c \ src/reader/pe_read_dos_header.c \ diff --git a/src/driver/pe_amain.c b/src/driver/pe_amain.c index 0593a74..8fb1788 100644 --- a/src/driver/pe_amain.c +++ b/src/driver/pe_amain.c @@ -69,6 +69,9 @@ static void pe_perform_unit_actions( if ((flags & PERK_OUTPUT_IMPORT_LIBS) || (flags & PERK_OUTPUT_IMPORT_SYMS)) pe_output_import_libraries(dctx,uctx->meta,0); + + if ((flags & PERK_OUTPUT_MDSO_LIBS) || (flags & PERK_OUTPUT_MDSO_SYMS)) + pe_output_mdso_libraries(dctx,uctx->meta,0); } static int pe_exit(struct pe_driver_ctx * dctx, int ret) diff --git a/src/output/pe_output_error.c b/src/output/pe_output_error.c index cf9bd3f..60e8cf6 100644 --- a/src/output/pe_output_error.c +++ b/src/output/pe_output_error.c @@ -30,6 +30,7 @@ static const char * const pe_error_strings[PERK_ERR_CAP] = { [PERK_ERR_BAD_DOS_HEADER] = "bad DOS header", [PERK_ERR_BAD_COFF_HEADER] = "bad COFF header", [PERK_ERR_BAD_IMAGE_TYPE] = "bad PE image type", + [PERK_ERR_UNSUPPORTED_ABI] = "unsupported image abi", }; static const char * pe_output_error_header(const struct pe_error_info * erri) diff --git a/src/output/pe_output_mdso_libraries.c b/src/output/pe_output_mdso_libraries.c new file mode 100644 index 0000000..aa70727 --- /dev/null +++ b/src/output/pe_output_mdso_libraries.c @@ -0,0 +1,196 @@ +/***************************************************************/ +/* perk: PE Resource Kit */ +/* Copyright (C) 2015--2017 Z. Gilboa */ +/* Released under GPLv2 and GPLv3; see COPYING.PERK. */ +/***************************************************************/ + +#include <stdint.h> +#include <stdlib.h> +#include <stdio.h> +#include <string.h> +#include <errno.h> + +#include <mdso/mdso_specs.h> +#include <mdso/mdso_structs.h> + +#include <perk/perk.h> +#include <perk/perk_output.h> +#include "perk_reader_impl.h" +#include "perk_errinfo_impl.h" + +static int pretty_header(const struct pe_common_ctx * cctx, FILE * fout) +{ + return (cctx->fmtflags & PERK_PRETTY_YAML) + ? fputs("dsolibs:\n",fout) + : 0; +} + +static int pretty_dsolib_header(const struct pe_common_ctx * cctx, const char * name, FILE * fout) +{ + if ((cctx->fmtflags & PERK_PRETTY_YAML) && (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS)) + return fprintf(fout," %s:\n",name); + else if (cctx->fmtflags & PERK_PRETTY_YAML) + return fprintf(fout,"- %s:\n",name); + else if (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS) + return fprintf(fout,"%s:\n",name); + else + return fprintf(fout,"%s\n",name); +} + +static int pretty_dsolib_item(const struct pe_common_ctx * cctx, const char * name, FILE * fout) +{ + if (cctx->fmtflags & PERK_PRETTY_YAML) + return fprintf(fout," - %s\n",name); + else + return fprintf(fout,"%s\n",name); +} + +static unsigned char * dsolib_meta(const struct pe_image_meta * m, int i) +{ + struct mdso_raw_meta_record_m32 * mdso32; + struct mdso_raw_meta_record_m64 * mdso64; + + if (m->m_opt.oh_std.coh_magic == PE_MAGIC_PE32_PLUS) { + mdso64 = (struct mdso_raw_meta_record_m64 *)m->r_dsometa + i; + return mdso64->mrec_base; + } else { + mdso32 = (struct mdso_raw_meta_record_m32 *)m->r_dsometa + i; + return mdso32->mrec_base; + } +} + +static char * dsolib_name(const struct pe_image_meta * m, int i) +{ + uintptr_t va; + uint64_t rva; + uint32_t roffset; + struct mdso_raw_meta_record_m32 * mdso32; + struct mdso_raw_meta_record_m64 * mdso64; + + if (m->m_opt.oh_std.coh_magic == PE_MAGIC_PE32_PLUS) { + mdso64 = (struct mdso_raw_meta_record_m64 *)m->r_dsometa + i; + va = pe_read_quad(mdso64->mrec_name); + rva = va - m->m_opt.oh_mem.coh_image_base; + + if (rva >> 32) + return 0; + } else { + mdso32 = (struct mdso_raw_meta_record_m32 *)m->r_dsometa + i; + va = pe_read_long(mdso32->mrec_name); + rva = va - m->m_opt.oh_mem.coh_image_base; + } + + if (pe_get_roffset_from_rva(m,(uint32_t)rva,&roffset) < 0) + return 0; + + return m->r_image.map_addr + roffset; +} + +static unsigned char * dsosym_meta(const struct pe_image_meta * m, int j) +{ + uintptr_t va; + uint64_t rva; + uint32_t roffset; + struct mdso_raw_sym_entry_m32 * sym32; + struct mdso_raw_sym_entry_m64 * sym64; + + if (m->m_opt.oh_std.coh_magic == PE_MAGIC_PE32_PLUS) { + sym64 = (struct mdso_raw_sym_entry_m64 *)m->r_dsosyms + j; + va = pe_read_quad(sym64->msym_meta); + rva = va - m->m_opt.oh_mem.coh_image_base; + + if (rva >> 32) + return 0; + } else { + sym32 = (struct mdso_raw_sym_entry_m32 *)m->r_dsosyms + j; + va = pe_read_long(sym32->msym_meta); + rva = va - m->m_opt.oh_mem.coh_image_base; + } + + if (pe_get_roffset_from_rva(m,(uint32_t)rva,&roffset) < 0) + return 0; + + return (unsigned char *)m->r_image.map_addr + roffset; +} + +static char * dsosym_string(const struct pe_image_meta * m, int j) +{ + uintptr_t va; + uint64_t rva; + uint32_t roffset; + struct mdso_raw_sym_entry_m32 * sym32; + struct mdso_raw_sym_entry_m64 * sym64; + + if (m->m_opt.oh_std.coh_magic == PE_MAGIC_PE32_PLUS) { + sym64 = (struct mdso_raw_sym_entry_m64 *)m->r_dsosyms + j; + va = pe_read_quad(sym64->msym_string); + rva = va - m->m_opt.oh_mem.coh_image_base; + + if (rva >> 32) + return 0; + } else { + sym32 = (struct mdso_raw_sym_entry_m32 *)m->r_dsosyms + j; + va = pe_read_long(sym32->msym_string); + rva = va - m->m_opt.oh_mem.coh_image_base; + } + + if (pe_get_roffset_from_rva(m,(uint32_t)rva,&roffset) < 0) + return 0; + + return m->r_image.map_addr + roffset; +} + +int pe_output_mdso_libraries( + const struct pe_driver_ctx * dctx, + const struct pe_image_meta * m, + FILE * fout) +{ + int i,j; + char * name; + unsigned char * symmeta; + unsigned char * dsometa; + char * symstr; + const struct pe_common_ctx * cctx = dctx->cctx; + + if (!m->m_stats.t_ndsolibs) + return 0; + + if (!fout) + fout = stdout; + + if (pe_get_image_abi(m,0) == PE_ABI_UNSUPPORTED) + return PERK_CUSTOM_ERROR( + dctx,PERK_ERR_UNSUPPORTED_ABI); + + if ((pretty_header(cctx,fout)) < 0) + return PERK_FILE_ERROR(dctx); + + for (i=0; i<m->m_stats.t_ndsolibs; i++) { + dsometa = dsolib_meta(m,i); + + if (!(name = dsolib_name(m,i))) + return PERK_CUSTOM_ERROR( + dctx,PERK_ERR_IMAGE_MALFORMED); + + if ((pretty_dsolib_header(cctx,name,fout)) < 0) + return PERK_FILE_ERROR(dctx); + + if (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS) { + for (j=0; j<m->m_stats.t_ndsosyms; j++) { + if (!(symmeta = dsosym_meta(m,j))) + return PERK_CUSTOM_ERROR( + dctx,PERK_ERR_IMAGE_MALFORMED); + + if (!(symstr = dsosym_string(m,j))) + return PERK_CUSTOM_ERROR( + dctx,PERK_ERR_IMAGE_MALFORMED); + + if (symmeta == dsometa) + if ((pretty_dsolib_item(cctx,symstr,fout)) < 0) + return PERK_FILE_ERROR(dctx); + } + } + } + + return 0; +} |