From 67bba74b591b7d000eb68013334ba923ca4f216c Mon Sep 17 00:00:00 2001 From: midipix Date: Tue, 24 Jul 2018 03:29:42 -0400 Subject: output interfaces: revised API and implementation to use fdctx and pure fdio. --- include/perk/perk.h | 14 +++++----- src/driver/pe_amain.c | 14 +++++----- src/output/pe_output_export_symbols.c | 35 ++++++++++++++---------- src/output/pe_output_idata_libraries.c | 50 ++++++++++++++++++++-------------- src/output/pe_output_image_category.c | 17 ++++++++---- src/output/pe_output_image_sections.c | 15 ++++++---- src/output/pe_output_image_strings.c | 13 +++++---- src/output/pe_output_image_symbols.c | 15 ++++++---- src/output/pe_output_mdso_libraries.c | 45 ++++++++++++++++++------------ 9 files changed, 127 insertions(+), 91 deletions(-) diff --git a/include/perk/perk.h b/include/perk/perk.h index 675f437..2860c53 100644 --- a/include/perk/perk.h +++ b/include/perk/perk.h @@ -191,13 +191,13 @@ perk_api int pe_set_driver_fdctx (struct pe_driver_ctx *, const struct pe /* utility api */ perk_api int pe_main (int, char **, char **, const struct pe_fd_ctx *); -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_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 *); +perk_api int pe_output_image_category (const struct pe_driver_ctx *, const struct pe_image_meta *); +perk_api int pe_output_image_sections (const struct pe_driver_ctx *, const struct pe_image_meta *); +perk_api int pe_output_image_symbols (const struct pe_driver_ctx *, const struct pe_image_meta *); +perk_api int pe_output_image_strings (const struct pe_driver_ctx *, const struct pe_image_meta *); +perk_api int pe_output_export_symbols (const struct pe_driver_ctx *, const struct pe_image_meta *); +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 *); /* error trace api */ perk_api int pe_output_error_record (const struct pe_driver_ctx *, const struct pe_error_info *); diff --git a/src/driver/pe_amain.c b/src/driver/pe_amain.c index 4ea62ef..bc8afce 100644 --- a/src/driver/pe_amain.c +++ b/src/driver/pe_amain.c @@ -55,25 +55,25 @@ static void pe_perform_unit_actions( uint64_t flags = dctx->cctx->fmtflags; if (flags & PERK_OUTPUT_IMAGE_CATEGORY) - pe_output_image_category(dctx,uctx->meta,0); + pe_output_image_category(dctx,uctx->meta); if (flags & PERK_OUTPUT_IMAGE_SECTIONS) - pe_output_image_sections(dctx,uctx->meta,0); + pe_output_image_sections(dctx,uctx->meta); if (flags & PERK_OUTPUT_IMAGE_SYMBOLS) - pe_output_image_symbols(dctx,uctx->meta,0); + pe_output_image_symbols(dctx,uctx->meta); if (flags & PERK_OUTPUT_IMAGE_STRINGS) - pe_output_image_strings(dctx,uctx->meta,0); + pe_output_image_strings(dctx,uctx->meta); if (flags & PERK_OUTPUT_EXPORT_SYMS) - pe_output_export_symbols(dctx,uctx->meta,0); + pe_output_export_symbols(dctx,uctx->meta); if ((flags & PERK_OUTPUT_IMPORT_LIBS) || (flags & PERK_OUTPUT_IMPORT_SYMS)) - pe_output_import_libraries(dctx,uctx->meta,0); + pe_output_import_libraries(dctx,uctx->meta); if ((flags & PERK_OUTPUT_MDSO_LIBS) || (flags & PERK_OUTPUT_MDSO_SYMS)) - pe_output_mdso_libraries(dctx,uctx->meta,0); + pe_output_mdso_libraries(dctx,uctx->meta); } static int pe_exit(struct pe_driver_ctx * dctx, int ret) diff --git a/src/output/pe_output_export_symbols.c b/src/output/pe_output_export_symbols.c index 700822c..475ae3f 100644 --- a/src/output/pe_output_export_symbols.c +++ b/src/output/pe_output_export_symbols.c @@ -12,43 +12,49 @@ #include #include +#include "perk_driver_impl.h" +#include "perk_dprintf_impl.h" #include "perk_errinfo_impl.h" -static int pretty_header(const struct pe_common_ctx * cctx, FILE * fout) +static int pretty_header( + int fdout, + const struct pe_common_ctx * cctx) { if (cctx->fmtflags & PERK_PRETTY_YAML) - return fputs("exports:\n",fout); + return pe_dprintf(fdout,"exports:\n"); else if (cctx->fmtflags & PERK_PRETTY_DLLTOOL) - return fputs("EXPORTS\n",fout); + return pe_dprintf(fdout,"EXPORTS\n"); return 0; } -static int pretty_export_item(const struct pe_common_ctx * cctx, const char * name, FILE * fout) +static int pretty_export_item( + int fdout, + const struct pe_common_ctx * cctx, + const char * name) { if (cctx->fmtflags & PERK_PRETTY_YAML) - return fprintf(fout,"- %s\n",name); + return pe_dprintf(fdout,"- %s\n",name); else - return fprintf(fout,"%s\n",name); + return pe_dprintf(fdout,"%s\n",name); } int pe_output_export_symbols( const struct pe_driver_ctx * dctx, - const struct pe_image_meta * m, - FILE * fout) + const struct pe_image_meta * m) { + int fdout; char * mark; uint32_t offset; uint32_t * symrva; unsigned i; const struct pe_common_ctx * cctx = dctx->cctx; + fdout = pe_driver_fdout(dctx); + if (!m->h_edata) return 0; - if (!fout) - fout = stdout; - - if ((pretty_header(cctx,fout)) < 0) + if ((pretty_header(fdout,cctx)) < 0) return PERK_FILE_ERROR(dctx); mark = m->r_image.map_addr; @@ -57,9 +63,8 @@ int pe_output_export_symbols( for (i=0; im_edata.eh_num_of_name_ptrs; i++) if ((pretty_export_item( - cctx, - &mark[symrva[i] - offset], - fout)) < 0) + fdout,cctx, + &mark[symrva[i] - offset])) < 0) return PERK_FILE_ERROR(dctx); return 0; diff --git a/src/output/pe_output_idata_libraries.c b/src/output/pe_output_idata_libraries.c index a5c49bf..b68f77f 100644 --- a/src/output/pe_output_idata_libraries.c +++ b/src/output/pe_output_idata_libraries.c @@ -12,64 +12,74 @@ #include #include +#include "perk_driver_impl.h" +#include "perk_dprintf_impl.h" #include "perk_errinfo_impl.h" -static int pretty_header(const struct pe_common_ctx * cctx, FILE * fout) +static int pretty_header( + int fdout, + const struct pe_common_ctx * cctx) { return (cctx->fmtflags & PERK_PRETTY_YAML) - ? fputs("imports:\n",fout) + ? pe_dprintf(fdout,"imports:\n") : 0; } -static int pretty_implib_header(const struct pe_common_ctx * cctx, const char * name, FILE * fout) +static int pretty_implib_header( + int fdout, + const struct pe_common_ctx * cctx, + const char * name) { if ((cctx->fmtflags & PERK_PRETTY_YAML) && (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS)) - return fprintf(fout," %s:\n",name); + return pe_dprintf(fdout," %s:\n",name); else if (cctx->fmtflags & PERK_PRETTY_YAML) - return fprintf(fout,"- %s:\n",name); + return pe_dprintf(fdout,"- %s:\n",name); else if (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS) - return fprintf(fout,"%s:\n",name); + return pe_dprintf(fdout,"%s:\n",name); else - return fprintf(fout,"%s\n",name); + return pe_dprintf(fdout,"%s\n",name); } -static int pretty_implib_item(const struct pe_common_ctx * cctx, const char * name, FILE * fout) +static int pretty_implib_item( + int fdout, + const struct pe_common_ctx * cctx, + const char * name) { if (cctx->fmtflags & PERK_PRETTY_YAML) - return fprintf(fout," - %s\n",name); + return pe_dprintf(fdout," - %s\n",name); else - return fprintf(fout,"%s\n",name); + return pe_dprintf(fdout,"%s\n",name); } int pe_output_import_libraries( const struct pe_driver_ctx * dctx, - const struct pe_image_meta * m, - FILE * fout) + const struct pe_image_meta * m) { + int fdout; int i; unsigned j; const struct pe_common_ctx * cctx = dctx->cctx; + fdout = pe_driver_fdout(dctx); + if (!m->m_stats.t_nimplibs) return 0; - if (!fout) - fout = stdout; - - if ((pretty_header(cctx,fout)) < 0) + if ((pretty_header(fdout,cctx)) < 0) return PERK_FILE_ERROR(dctx); for (i=0; im_stats.t_nimplibs; i++) { - if ((pretty_implib_header(cctx,m->m_idata[i].ih_name,fout)) < 0) + if ((pretty_implib_header( + fdout,cctx, + m->m_idata[i].ih_name)) < 0) return PERK_FILE_ERROR(dctx); if (cctx->fmtflags & PERK_OUTPUT_IMPORT_SYMS) for (j=0; jm_idata[i].ih_count; j++) if (m->m_idata[i].ih_items[j].ii_name) if ((pretty_implib_item( - cctx, - m->m_idata[i].ih_items[j].ii_name, - fout)) < 0) + fdout,cctx, + m->m_idata[i].ih_items[j].ii_name)) < 0) return PERK_FILE_ERROR(dctx); } diff --git a/src/output/pe_output_image_category.c b/src/output/pe_output_image_category.c index 9e24481..c4d5f37 100644 --- a/src/output/pe_output_image_category.c +++ b/src/output/pe_output_image_category.c @@ -8,20 +8,21 @@ #include #include +#include "perk_driver_impl.h" +#include "perk_dprintf_impl.h" #include "perk_errinfo_impl.h" int pe_output_image_category( const struct pe_driver_ctx * dctx, - const struct pe_image_meta * meta, - FILE * fout) + const struct pe_image_meta * meta) { + int fdout; struct pe_info_string abi; struct pe_info_string subtype; struct pe_info_string subsystem; struct pe_info_string framework; - if (!fout) - fout = stdout; + fdout = pe_driver_fdout(dctx); pe_get_image_abi (meta,&abi); pe_get_image_subtype (meta,&subtype); @@ -29,14 +30,18 @@ int pe_output_image_category( pe_get_image_framework(meta,&framework); if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) { - if (fprintf(fout,"category:\n- %s:\n- %s:\n- %s:\n- %s:\n", + if (pe_dprintf( + fdout, + "category:\n- %s:\n- %s:\n- %s:\n- %s:\n", abi.buffer, subtype.buffer, subsystem.buffer, framework.buffer) < 0) return PERK_FILE_ERROR(dctx); } else { - if (fprintf(fout,"%s-%s-%s-%s\n", + if (pe_dprintf( + fdout, + "%s-%s-%s-%s\n", abi.buffer, subtype.buffer, subsystem.buffer, diff --git a/src/output/pe_output_image_sections.c b/src/output/pe_output_image_sections.c index 461cf5c..e6155fb 100644 --- a/src/output/pe_output_image_sections.c +++ b/src/output/pe_output_image_sections.c @@ -8,28 +8,31 @@ #include #include +#include "perk_driver_impl.h" +#include "perk_dprintf_impl.h" #include "perk_errinfo_impl.h" int pe_output_image_sections( const struct pe_driver_ctx * dctx, - const struct pe_image_meta * meta, - FILE * fout) + const struct pe_image_meta * meta) { int i; + int fdout; const char * dash = ""; - if (!fout) - fout = stdout; + fdout = pe_driver_fdout(dctx); if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) { - if (fputs("sections:\n",fout) < 0) + if (pe_dprintf(fdout,"sections:\n") < 0) return PERK_FILE_ERROR(dctx); dash = "- "; } for (i=0; im_coff.cfh_num_of_sections; i++) - if (fprintf(fout,"%s%s\n", + if (pe_dprintf( + fdout, + "%s%s\n", dash, meta->m_sectbl[i].sh_long_name ? meta->m_sectbl[i].sh_long_name diff --git a/src/output/pe_output_image_strings.c b/src/output/pe_output_image_strings.c index 51c7ef9..36bf9a6 100644 --- a/src/output/pe_output_image_strings.c +++ b/src/output/pe_output_image_strings.c @@ -10,23 +10,24 @@ #include #include #include "perk_reader_impl.h" +#include "perk_driver_impl.h" +#include "perk_dprintf_impl.h" #include "perk_errinfo_impl.h" int pe_output_image_strings( const struct pe_driver_ctx * dctx, - const struct pe_image_meta * meta, - FILE * fout) + const struct pe_image_meta * meta) { + int fdout; const char * ch; const char * mark; const char * cap; const char * dash = ""; - if (!fout) - fout = stdout; + fdout = pe_driver_fdout(dctx); if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) { - if (fputs("strings:\n",fout) < 0) + if (pe_dprintf(fdout,"strings:\n") < 0) return PERK_FILE_ERROR(dctx); dash = "- "; @@ -39,7 +40,7 @@ int pe_output_image_strings( mark += sizeof(uint32_t); for (ch=mark; ch #include #include "perk_reader_impl.h" +#include "perk_driver_impl.h" +#include "perk_dprintf_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) + const struct pe_image_meta * meta) { unsigned i; + int fdout; char * mark; struct pe_raw_coff_symbol * symtbl; struct pe_meta_coff_symbol symrec; const char * dash = ""; - if (!fout) - fout = stdout; + fdout = pe_driver_fdout(dctx); if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) { - if (fputs("symbols:\n",fout) < 0) + if (pe_dprintf(fdout,"symbols:\n") < 0) return PERK_FILE_ERROR(dctx); dash = "- "; @@ -41,7 +42,9 @@ int pe_output_image_symbols( &symtbl[i],&symrec, &meta->m_coff,meta->r_image.map_addr); - if (fprintf(fout,"%s%s\n", + if (pe_dprintf( + fdout, + "%s%s\n", dash, symrec.cs_long_name ? symrec.cs_long_name diff --git a/src/output/pe_output_mdso_libraries.c b/src/output/pe_output_mdso_libraries.c index aa70727..ba72665 100644 --- a/src/output/pe_output_mdso_libraries.c +++ b/src/output/pe_output_mdso_libraries.c @@ -16,33 +16,43 @@ #include #include #include "perk_reader_impl.h" +#include "perk_driver_impl.h" +#include "perk_dprintf_impl.h" #include "perk_errinfo_impl.h" -static int pretty_header(const struct pe_common_ctx * cctx, FILE * fout) +static int pretty_header( + int fdout, + const struct pe_common_ctx * cctx) { return (cctx->fmtflags & PERK_PRETTY_YAML) - ? fputs("dsolibs:\n",fout) + ? pe_dprintf(fdout,"dsolibs:\n") : 0; } -static int pretty_dsolib_header(const struct pe_common_ctx * cctx, const char * name, FILE * fout) +static int pretty_dsolib_header( + int fdout, + const struct pe_common_ctx * cctx, + const char * name) { if ((cctx->fmtflags & PERK_PRETTY_YAML) && (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS)) - return fprintf(fout," %s:\n",name); + return pe_dprintf(fdout," %s:\n",name); else if (cctx->fmtflags & PERK_PRETTY_YAML) - return fprintf(fout,"- %s:\n",name); + return pe_dprintf(fdout,"- %s:\n",name); else if (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS) - return fprintf(fout,"%s:\n",name); + return pe_dprintf(fdout,"%s:\n",name); else - return fprintf(fout,"%s\n",name); + return pe_dprintf(fdout,"%s\n",name); } -static int pretty_dsolib_item(const struct pe_common_ctx * cctx, const char * name, FILE * fout) +static int pretty_dsolib_item( + int fdout, + const struct pe_common_ctx * cctx, + const char * name) { if (cctx->fmtflags & PERK_PRETTY_YAML) - return fprintf(fout," - %s\n",name); + return pe_dprintf(fdout," - %s\n",name); else - return fprintf(fout,"%s\n",name); + return pe_dprintf(fdout,"%s\n",name); } static unsigned char * dsolib_meta(const struct pe_image_meta * m, int i) @@ -142,27 +152,26 @@ static char * dsosym_string(const struct pe_image_meta * m, int j) int pe_output_mdso_libraries( const struct pe_driver_ctx * dctx, - const struct pe_image_meta * m, - FILE * fout) + const struct pe_image_meta * m) { int i,j; + int fdout; char * name; unsigned char * symmeta; unsigned char * dsometa; char * symstr; const struct pe_common_ctx * cctx = dctx->cctx; + fdout = pe_driver_fdout(dctx); + 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) + if ((pretty_header(fdout,cctx)) < 0) return PERK_FILE_ERROR(dctx); for (i=0; im_stats.t_ndsolibs; i++) { @@ -172,7 +181,7 @@ int pe_output_mdso_libraries( return PERK_CUSTOM_ERROR( dctx,PERK_ERR_IMAGE_MALFORMED); - if ((pretty_dsolib_header(cctx,name,fout)) < 0) + if ((pretty_dsolib_header(fdout,cctx,name)) < 0) return PERK_FILE_ERROR(dctx); if (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS) { @@ -186,7 +195,7 @@ int pe_output_mdso_libraries( dctx,PERK_ERR_IMAGE_MALFORMED); if (symmeta == dsometa) - if ((pretty_dsolib_item(cctx,symstr,fout)) < 0) + if ((pretty_dsolib_item(fdout,cctx,symstr)) < 0) return PERK_FILE_ERROR(dctx); } } -- cgit v1.2.3