Blame src/hdrdump/pe_hdrdump_import_tbl.c

8f30d9
/***************************************************************/
8f30d9
/*  perk: PE Resource Kit                                      */
425fb8
/*  Copyright (C) 2015--2021  SysDeer Technologies, LLC        */
8f30d9
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
8f30d9
/***************************************************************/
8f30d9
8f30d9
#include <stdio.h>
8f30d9
8f30d9
#include <perk/perk.h>
d6e02b
#include <perk/perk_consts.h>
d6e02b
#include <perk/perk_structs.h>
8f30d9
#include <perk/perk_output.h>
8f30d9
#include "perk_driver_impl.h"
8f30d9
#include "perk_hdrdump_impl.h"
8f30d9
#include "perk_dprintf_impl.h"
8f30d9
#include "perk_errinfo_impl.h"
8f30d9
8f30d9
#define PE_ADDR       &meta->r_idata[idx]
8f30d9
#define PE_RAW_STRUCT import_hdr
8f30d9
#define PE_OUTPUT(x)  PE_OUTPUT_TABLE(x)
8f30d9
8f30d9
static int pe_hdrdump_import_hdr_impl(
8f30d9
	const struct pe_driver_ctx *	dctx,
8f30d9
	const struct pe_image_meta *	meta,
0a52ca
	int32_t				idx)
8f30d9
{
8f30d9
	int		bits;
8f30d9
	uint64_t	faddr;
8f30d9
	uint64_t	vaddr;
8f30d9
	char *		ch;
8f30d9
	char		hdr[64];
8f30d9
	char		buf[8192];
8f30d9
8f30d9
	if ((bits = pe_image_bits(meta)) < 0)
8f30d9
		return PERK_CUSTOM_ERROR(
8f30d9
			dctx,PERK_ERR_UNSUPPORTED_ABI);
8f30d9
0a52ca
	/* import header */
8f30d9
	sprintf(
8f30d9
		hdr,
8f30d9
		"struct pe_raw_import_hdr [%d]",
8f30d9
		idx);
8f30d9
8f30d9
	faddr = (char *)(PE_ADDR) - (char *)meta->r_image.map_addr;
8f30d9
	vaddr = meta->m_opt.oh_mem.coh_image_base + meta->m_idata[idx].ih_virtual_addr;
8f30d9
8f30d9
	ch  = buf;
8f30d9
	ch += pe_output_hex_header(
8f30d9
		ch,hdr,
8f30d9
		faddr,vaddr,bits);
8f30d9
8f30d9
	ch += PE_OUTPUT(ih_import_lookup_tbl_rva);
8f30d9
	ch += PE_OUTPUT(ih_time_date_stamp);
8f30d9
	ch += PE_OUTPUT(ih_forwarder_chain);
8f30d9
	ch += PE_OUTPUT(ih_name_rva);
8f30d9
	ch += PE_OUTPUT(ih_import_addr_tbl_rva);
8f30d9
8f30d9
	ch += pe_output_hex_footer(ch);
8f30d9
0a52ca
	/* import name for all but the null header */
0a52ca
	if (idx == meta->m_stats.t_nimplibs) {
0a52ca
		(void)0;
0a52ca
0a52ca
	} else if (meta->m_idata[idx].ih_name_rva) {
0a52ca
		sprintf(
0a52ca
			hdr,
0a52ca
			"struct pe_raw_import_name [%d]",
0a52ca
			idx);
0a52ca
0a52ca
		faddr = meta->m_idata[idx].ih_name - (char *)meta->r_image.map_addr;
0a52ca
		vaddr = meta->m_idata[idx].ih_name_rva;
0a52ca
0a52ca
		ch += pe_output_hex_header(
0a52ca
			ch,hdr,
0a52ca
			faddr,vaddr,bits);
0a52ca
0a52ca
		ch += pe_output_raw_element(
0a52ca
			ch,
0a52ca
			meta->m_idata[idx].ih_name,
0a52ca
			"ih_name",0,
0a52ca
			strlen(meta->m_idata[idx].ih_name) + 1);
0a52ca
0a52ca
		ch += pe_output_hex_footer(ch);
0a52ca
	}
0a52ca
8f30d9
	*ch = 0;
8f30d9
0a52ca
	/* fdout */
8f30d9
	if (pe_dprintf(pe_driver_fdout(dctx),"%s",buf) < 0)
8f30d9
		return PERK_FILE_ERROR(dctx);
8f30d9
8f30d9
	return 0;
8f30d9
}
8f30d9
8f30d9
int pe_hdrdump_import_tbl(
8f30d9
	const struct pe_driver_ctx *	dctx,
8f30d9
	const struct pe_image_meta *	meta)
8f30d9
{
8f30d9
	int idx;
8f30d9
44ba87
	if (meta->m_stats.t_nimplibs == 0)
44ba87
		return 0;
44ba87
0a52ca
	for (idx=0; idx<=meta->m_stats.t_nimplibs; idx++)
8f30d9
		if (pe_hdrdump_import_hdr_impl(dctx,meta,idx) < 0)
8f30d9
			return PERK_NESTED_ERROR(dctx);
8f30d9
8f30d9
	return 0;
8f30d9
}