summaryrefslogtreecommitdiff
path: root/src/output
diff options
context:
space:
mode:
Diffstat (limited to 'src/output')
-rw-r--r--src/output/pe_output_error.c1
-rw-r--r--src/output/pe_output_mdso_libraries.c196
2 files changed, 197 insertions, 0 deletions
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;
+}