summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authormidipix <writeonce@midipix.org>2016-11-16 18:35:06 -0500
committermidipix <writeonce@midipix.org>2016-11-16 18:35:06 -0500
commitf779e375ef1f739f7ab515a8d853f9f53ac9ec5a (patch)
tree81fd1b677a2cb201151cda8cec17fd0abfe65709
parent016925a048e34c3eeb2815d6830d61771f0ed999 (diff)
downloadperk-f779e375ef1f739f7ab515a8d853f9f53ac9ec5a.tar.bz2
perk-f779e375ef1f739f7ab515a8d853f9f53ac9ec5a.tar.xz
output: pe_output_image_symbols(): initial implementation and integration.
-rw-r--r--include/perk/perk.h1
-rw-r--r--project/common.mk1
-rw-r--r--src/driver/pe_amain.c3
-rw-r--r--src/output/pe_output_image_symbols.c80
4 files changed, 85 insertions, 0 deletions
diff --git a/include/perk/perk.h b/include/perk/perk.h
index 017f19b..8324f66 100644
--- a/include/perk/perk.h
+++ b/include/perk/perk.h
@@ -164,6 +164,7 @@ perk_api void pe_free_unit_ctx (struct pe_unit_ctx *);
perk_api int pe_main (int, char **, char **);
perk_api int pe_output_image_category (const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *);
perk_api int pe_output_image_sections (const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *);
+perk_api int pe_output_image_symbols (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_error_record (const struct pe_driver_ctx *, const struct pe_error_info *);
diff --git a/project/common.mk b/project/common.mk
index adca9d5..e84caa2 100644
--- a/project/common.mk
+++ b/project/common.mk
@@ -12,6 +12,7 @@ API_SRCS = \
src/output/pe_output_export_symbols.c \
src/output/pe_output_image_category.c \
src/output/pe_output_image_sections.c \
+ src/output/pe_output_image_symbols.c \
src/output/pe_output_import_libraries.c \
src/reader/pe_read_coff_header.c \
src/reader/pe_read_dos_header.c \
diff --git a/src/driver/pe_amain.c b/src/driver/pe_amain.c
index 59cb8f2..e9167fb 100644
--- a/src/driver/pe_amain.c
+++ b/src/driver/pe_amain.c
@@ -58,6 +58,9 @@ static void pe_perform_unit_actions(
if (flags & PERK_OUTPUT_IMAGE_SECTIONS)
pe_output_image_sections(dctx,uctx->meta,0);
+ if (flags & PERK_OUTPUT_IMAGE_SYMBOLS)
+ pe_output_image_symbols(dctx,uctx->meta,0);
+
if (flags & PERK_OUTPUT_EXPORT_SYMS)
pe_output_export_symbols(dctx,uctx->meta,0);
diff --git a/src/output/pe_output_image_symbols.c b/src/output/pe_output_image_symbols.c
new file mode 100644
index 0000000..da4bf8c
--- /dev/null
+++ b/src/output/pe_output_image_symbols.c
@@ -0,0 +1,80 @@
+/***************************************************************/
+/* perk: PE Resource Kit */
+/* Copyright (C) 2015--2016 Z. Gilboa */
+/* Released under GPLv2 and GPLv3; see COPYING.PERK. */
+/***************************************************************/
+
+#include <stdio.h>
+#include <string.h>
+
+#include <perk/perk.h>
+#include <perk/perk_output.h>
+#include "perk_reader_impl.h"
+#include "perk_errinfo_impl.h"
+
+int pe_output_image_symbols(
+ const struct pe_driver_ctx * dctx,
+ const struct pe_image_meta * meta,
+ FILE * fout)
+{
+ unsigned i,bias;
+ uint32_t roffset;
+ struct pe_coff_sym_entry * symtbl;
+ char buf[24];
+ char * mark;
+ const char * name;
+ const char * dash = "";
+
+ if (!fout)
+ fout = stdout;
+
+ if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) {
+ if (fputs("symbols:\n",fout) < 0)
+ return PERK_FILE_ERROR(dctx);
+
+ dash = "- ";
+ }
+
+ mark = (char *)meta->image.addr;
+ symtbl = (struct pe_coff_sym_entry *)(mark + meta->coff.ptr_to_sym_tbl);
+
+ for (i=0,bias=0; i<meta->coff.num_of_syms; i++,bias=0) {
+ if (symtbl[i].storage_class[0] == PE_IMAGE_SYM_CLASS_FILE)
+ if (symtbl[i].num_of_aux_symbols[0])
+ if (!symtbl[i+1].value[0])
+ bias = 1;
+
+ i += bias;
+
+ if (!bias && (symtbl[i].storage_class[0] == PE_IMAGE_SYM_CLASS_FILE)
+ && symtbl[i].num_of_aux_symbols[0]) {
+ memset(buf,0,sizeof(buf));
+ memcpy(buf,symtbl[i+1].name,sizeof(*symtbl));
+ name = buf;
+
+ } else if (symtbl[i].name[0]) {
+ memset(buf,0,sizeof(buf));
+ memcpy(buf,symtbl[i].name,sizeof(symtbl->name));
+ name = buf;
+
+ } else if (!symtbl[i].name[1] && !symtbl[i].name[2] && !symtbl[i].name[3]) {
+ roffset = pe_read_long(&symtbl[i].name[4]);
+ name = (roffset < meta->coff.size_of_string_tbl)
+ ? mark + meta->coff.ptr_to_string_tbl + roffset
+ : 0;
+ } else {
+ name = 0;
+ }
+
+ i -= bias;
+ i += symtbl[i].num_of_aux_symbols[0];
+
+
+ if (name) {
+ if (fprintf(fout,"%s%s\n",dash,name) < 0)
+ return PERK_FILE_ERROR(dctx);
+ }
+ }
+
+ return 0;
+}