Blame src/hdrdump/pe_hdrdump_sec_tbl.c

1ce710
/***************************************************************/
1ce710
/*  perk: PE Resource Kit                                      */
1ce710
/*  Copyright (C) 2015--2017  Z. Gilboa                        */
1ce710
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
1ce710
/***************************************************************/
1ce710
1ce710
#include <stdio.h>
1ce710
1ce710
#include <perk/perk.h>
1ce710
#include <perk/perk_output.h>
1ce710
#include "perk_driver_impl.h"
1ce710
#include "perk_hdrdump_impl.h"
1ce710
#include "perk_dprintf_impl.h"
1ce710
#include "perk_errinfo_impl.h"
1ce710
1ce710
#define PE_ADDR       &meta->r_sectbl[idx]
1ce710
#define PE_RAW_STRUCT sec_hdr
1ce710
#define PE_OUTPUT(x)  PE_OUTPUT_TABLE(x)
1ce710
1ce710
static int pe_hdrdump_sec_hdr_impl(
1ce710
	const struct pe_driver_ctx *	dctx,
1ce710
	const struct pe_image_meta *	meta,
1ce710
	uint32_t			idx)
1ce710
{
1ce710
	int		bits;
1ce710
	uint64_t	faddr;
1ce710
	uint64_t	vaddr;
1ce710
	char *		ch;
1ce710
	char		hdr[64];
1ce710
	char		buf[8192];
1ce710
1ce710
	if ((bits = pe_image_bits(meta)) < 0)
1ce710
		return PERK_CUSTOM_ERROR(
1ce710
			dctx,PERK_ERR_UNSUPPORTED_ABI);
1ce710
1ce710
	sprintf(
1ce710
		hdr,
1ce710
		"struct pe_raw_sec_hdr [%d]",
1ce710
		idx);
1ce710
1ce710
	faddr = (char *)(PE_ADDR) - (char *)meta->r_image.map_addr;
1ce710
	vaddr = meta->m_opt.oh_mem.coh_image_base + faddr;
1ce710
1ce710
	ch  = buf;
1ce710
	ch += pe_output_hex_header(
1ce710
		ch,hdr,
1ce710
		faddr,vaddr,bits);
1ce710
1ce710
	ch += PE_OUTPUT(sh_name);
1ce710
	ch += PE_OUTPUT(sh_virtual_size);
1ce710
	ch += PE_OUTPUT(sh_virtual_addr);
1ce710
	ch += PE_OUTPUT(sh_size_of_raw_data);
1ce710
	ch += PE_OUTPUT(sh_ptr_to_raw_data);
1ce710
	ch += PE_OUTPUT(sh_ptr_to_relocs);
1ce710
	ch += PE_OUTPUT(sh_ptr_to_line_nums);
1ce710
	ch += PE_OUTPUT(sh_num_of_relocs);
1ce710
	ch += PE_OUTPUT(sh_num_of_line_nums);
1ce710
	ch += PE_OUTPUT(sh_characteristics);
1ce710
1ce710
	ch += pe_output_hex_footer(ch);
1ce710
1ce710
	*ch = 0;
1ce710
1ce710
	if (pe_dprintf(pe_driver_fdout(dctx),"%s",buf) < 0)
1ce710
		return PERK_FILE_ERROR(dctx);
1ce710
1ce710
	return 0;
1ce710
}
1ce710
1ce710
int pe_hdrdump_sec_tbl(
1ce710
	const struct pe_driver_ctx *	dctx,
1ce710
	const struct pe_image_meta *	meta)
1ce710
{
1ce710
	uint32_t idx;
1ce710
1ce710
	for (idx=0; idx<meta->m_coff.cfh_num_of_sections; idx++)
1ce710
		if (pe_hdrdump_sec_hdr_impl(dctx,meta,idx) < 0)
1ce710
			return PERK_NESTED_ERROR(dctx);
1ce710
1ce710
	return 0;
1ce710
}