Blame src/output/pe_output_export_symbols.c

e2e2c2
/***************************************************************/
e2e2c2
/*  perk: PE Resource Kit                                      */
e2e2c2
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
e2e2c2
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
e2e2c2
/***************************************************************/
e2e2c2
c0fbae
#include <stdint.h>
c0fbae
#include <stdlib.h>
c0fbae
#include <stdio.h>
c0fbae
#include <string.h>
c0fbae
#include <errno.h>
c0fbae
c0fbae
#include <perk/perk.h>
e343a6
#include <perk/perk_output.h>
f64a5b
#include "perk_errinfo_impl.h"
c0fbae
6025fb
static int pretty_header(const struct pe_common_ctx * cctx, FILE * fout)
e343a6
{
08a894
	if (cctx->fmtflags & PERK_PRETTY_YAML)
08a894
		return fputs("exports:\n",fout);
08a894
	else if (cctx->fmtflags & PERK_PRETTY_DLLTOOL)
08a894
		return fputs("EXPORTS\n",fout);
08a894
	return 0;
e343a6
}
e343a6
6025fb
static int pretty_export_item(const struct pe_common_ctx * cctx, const char * name, FILE * fout)
e343a6
{
e343a6
	if (cctx->fmtflags & PERK_PRETTY_YAML)
6025fb
		return fprintf(fout,"- %s\n",name);
e343a6
	else
6025fb
		return fprintf(fout,"%s\n",name);
e343a6
}
e343a6
2b7f36
int pe_output_export_symbols(
f64a5b
	const struct pe_driver_ctx *	dctx,
2b7f36
	const struct pe_image_meta *	m,
2b7f36
	FILE *				fout)
c0fbae
{
c3af45
	char *				mark;
f64a5b
	uint32_t 			offset;
f64a5b
	uint32_t *			symrva;
f64a5b
	unsigned			i;
f64a5b
	const struct pe_common_ctx *	cctx = dctx->cctx;
c0fbae
ab024a
	if (!m->hedata)
ab024a
		return 0;
7cd8bc
f11c1f
	if (!fout)
f11c1f
		fout = stdout;
c0fbae
6025fb
	if ((pretty_header(cctx,fout)) < 0)
f11c1f
		return PERK_FILE_ERROR(dctx);
e343a6
c3af45
	mark	= m->image.addr;
c0fbae
	offset	= m->hedata->virtual_addr - m->hedata->ptr_to_raw_data;
c3af45
	symrva	= (uint32_t *)(mark + m->edata.name_ptr_rva - offset);
c0fbae
c0fbae
	for (i=0; i<m->edata.num_of_name_ptrs; i++)
6025fb
		if ((pretty_export_item(
6025fb
				cctx,
c3af45
				&mark[symrva[i] - offset],
6025fb
				fout)) < 0)
f11c1f
			return PERK_FILE_ERROR(dctx);
ab024a
f11c1f
	return 0;
c0fbae
}