Blame src/hdrdump/pe_hdrdump_opt_hdr.c

382719
/***************************************************************/
382719
/*  perk: PE Resource Kit                                      */
382719
/*  Copyright (C) 2015--2017  Z. Gilboa                        */
382719
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
382719
/***************************************************************/
382719
382719
#include <stdio.h>
382719
382719
#include <perk/perk.h>
d6e02b
#include <perk/perk_consts.h>
d6e02b
#include <perk/perk_structs.h>
382719
#include <perk/perk_output.h>
382719
#include "perk_driver_impl.h"
382719
#include "perk_hdrdump_impl.h"
382719
#include "perk_dprintf_impl.h"
382719
#include "perk_errinfo_impl.h"
382719
382719
#define PE_ADDR       &meta->r_opt->opt_hdr_32
382719
#define PE_RAW_STRUCT opt_hdr_32
382719
#define PE_OUTPUT(x)  PE_OUTPUT_TABLE(x)
382719
382719
static int pe_hdrdump_opt_hdr_32(
382719
	const struct pe_driver_ctx *	dctx,
382719
	const struct pe_image_meta *	meta)
382719
{
382719
	uint64_t	faddr;
382719
	uint64_t	vaddr;
382719
	char *		ch;
382719
	char		buf[8192];
382719
382719
	if (!PE_ADDR)
382719
		return 0;
382719
382719
	faddr = (char *)(PE_ADDR) - (char *)meta->r_image.map_addr;
382719
	vaddr = meta->m_opt.oh_mem.coh_image_base + faddr;
382719
382719
	ch  = buf;
382719
	ch += pe_output_hex_header(
382719
		ch,
382719
		"struct pe_raw_opt_hdr_32",
382719
		faddr,vaddr,32);
382719
382719
	ch += PE_OUTPUT(coh_magic);
382719
	ch += PE_OUTPUT(coh_major_linker_ver);
382719
	ch += PE_OUTPUT(coh_minor_linker_ver);
382719
	ch += PE_OUTPUT(coh_size_of_code);
382719
	ch += PE_OUTPUT(coh_size_of_inited_data);
382719
	ch += PE_OUTPUT(coh_size_of_uninited_data);
382719
	ch += PE_OUTPUT(coh_entry_point);
382719
	ch += PE_OUTPUT(coh_base_of_code);
382719
	ch += PE_OUTPUT(coh_base_of_data);
382719
	ch += PE_OUTPUT(coh_image_base);
382719
	ch += PE_OUTPUT(coh_section_align);
382719
	ch += PE_OUTPUT(coh_file_align);
382719
	ch += PE_OUTPUT(coh_major_os_ver);
382719
	ch += PE_OUTPUT(coh_minor_os_ver);
382719
	ch += PE_OUTPUT(coh_major_image_ver);
382719
	ch += PE_OUTPUT(coh_minor_image_ver);
382719
	ch += PE_OUTPUT(coh_major_subsys_ver);
382719
	ch += PE_OUTPUT(coh_minor_subsys_ver);
382719
	ch += PE_OUTPUT(coh_win32_ver);
382719
	ch += PE_OUTPUT(coh_size_of_image);
382719
	ch += PE_OUTPUT(coh_size_of_headers);
382719
	ch += PE_OUTPUT(coh_checksum);
382719
	ch += PE_OUTPUT(coh_subsystem);
382719
	ch += PE_OUTPUT(coh_dll_characteristics);
382719
	ch += PE_OUTPUT(coh_size_of_stack_reserve);
382719
	ch += PE_OUTPUT(coh_size_of_stack_commit);
382719
	ch += PE_OUTPUT(coh_size_of_heap_reserve);
382719
	ch += PE_OUTPUT(coh_size_of_heap_commit);
382719
	ch += PE_OUTPUT(coh_loader_flags);
382719
	ch += PE_OUTPUT(coh_rva_and_sizes);
382719
	ch += PE_OUTPUT(coh_export_tbl);
382719
	ch += PE_OUTPUT(coh_import_tbl);
382719
	ch += PE_OUTPUT(coh_resource_tbl);
382719
	ch += PE_OUTPUT(coh_exception_tbl);
382719
	ch += PE_OUTPUT(coh_certificate_tbl);
382719
	ch += PE_OUTPUT(coh_base_reloc_tbl);
382719
	ch += PE_OUTPUT(coh_debug);
382719
	ch += PE_OUTPUT(coh_arch);
382719
	ch += PE_OUTPUT(coh_global_ptr);
382719
	ch += PE_OUTPUT(coh_tls_tbl);
382719
	ch += PE_OUTPUT(coh_load_config_tbl);
382719
	ch += PE_OUTPUT(coh_bound_import);
382719
	ch += PE_OUTPUT(coh_iat);
382719
	ch += PE_OUTPUT(coh_delay_import_descriptor);
382719
	ch += PE_OUTPUT(coh_clr_runtime_hdr);
382719
	ch += PE_OUTPUT(coh_reserved);
382719
382719
	ch += pe_output_hex_footer(ch);
382719
382719
	*ch = 0;
382719
382719
	if (pe_dprintf(pe_driver_fdout(dctx),"%s",buf) < 0)
382719
		return PERK_FILE_ERROR(dctx);
382719
382719
	return 0;
382719
}
382719
382719
#undef  PE_ADDR
382719
#undef  PE_RAW_STRUCT
382719
#undef  PE_OUTPUT
382719
382719
#define PE_ADDR       &meta->r_opt->opt_hdr_64
382719
#define PE_RAW_STRUCT opt_hdr_64
382719
#define PE_OUTPUT(x)  PE_OUTPUT_TABLE(x)
382719
382719
static int pe_hdrdump_opt_hdr_64(
382719
	const struct pe_driver_ctx *	dctx,
382719
	const struct pe_image_meta *	meta)
382719
{
382719
	uint64_t	faddr;
382719
	uint64_t	vaddr;
382719
	char *		ch;
382719
	char		buf[8192];
382719
382719
	if (!PE_ADDR)
382719
		return 0;
382719
382719
	faddr = (char *)(PE_ADDR) - (char *)meta->r_image.map_addr;
382719
	vaddr = meta->m_opt.oh_mem.coh_image_base + faddr;
382719
382719
	ch  = buf;
382719
	ch += pe_output_hex_header(
382719
		ch,
382719
		"struct pe_raw_opt_hdr_64",
382719
		faddr,vaddr,64);
382719
382719
	ch += PE_OUTPUT(coh_magic);
382719
	ch += PE_OUTPUT(coh_major_linker_ver);
382719
	ch += PE_OUTPUT(coh_minor_linker_ver);
382719
	ch += PE_OUTPUT(coh_size_of_code);
382719
	ch += PE_OUTPUT(coh_size_of_inited_data);
382719
	ch += PE_OUTPUT(coh_size_of_uninited_data);
382719
	ch += PE_OUTPUT(coh_entry_point);
382719
	ch += PE_OUTPUT(coh_base_of_code);
382719
	ch += PE_OUTPUT(coh_image_base);
382719
	ch += PE_OUTPUT(coh_section_align);
382719
	ch += PE_OUTPUT(coh_file_align);
382719
	ch += PE_OUTPUT(coh_major_os_ver);
382719
	ch += PE_OUTPUT(coh_minor_os_ver);
382719
	ch += PE_OUTPUT(coh_major_image_ver);
382719
	ch += PE_OUTPUT(coh_minor_image_ver);
382719
	ch += PE_OUTPUT(coh_major_subsys_ver);
382719
	ch += PE_OUTPUT(coh_minor_subsys_ver);
382719
	ch += PE_OUTPUT(coh_win32_ver);
382719
	ch += PE_OUTPUT(coh_size_of_image);
382719
	ch += PE_OUTPUT(coh_size_of_headers);
382719
	ch += PE_OUTPUT(coh_checksum);
382719
	ch += PE_OUTPUT(coh_subsystem);
382719
	ch += PE_OUTPUT(coh_dll_characteristics);
382719
	ch += PE_OUTPUT(coh_size_of_stack_reserve);
382719
	ch += PE_OUTPUT(coh_size_of_stack_commit);
382719
	ch += PE_OUTPUT(coh_size_of_heap_reserve);
382719
	ch += PE_OUTPUT(coh_size_of_heap_commit);
382719
	ch += PE_OUTPUT(coh_loader_flags);
382719
	ch += PE_OUTPUT(coh_rva_and_sizes);
382719
	ch += PE_OUTPUT(coh_export_tbl);
382719
	ch += PE_OUTPUT(coh_import_tbl);
382719
	ch += PE_OUTPUT(coh_resource_tbl);
382719
	ch += PE_OUTPUT(coh_exception_tbl);
382719
	ch += PE_OUTPUT(coh_certificate_tbl);
382719
	ch += PE_OUTPUT(coh_base_reloc_tbl);
382719
	ch += PE_OUTPUT(coh_debug);
382719
	ch += PE_OUTPUT(coh_arch);
382719
	ch += PE_OUTPUT(coh_global_ptr);
382719
	ch += PE_OUTPUT(coh_tls_tbl);
382719
	ch += PE_OUTPUT(coh_load_config_tbl);
382719
	ch += PE_OUTPUT(coh_bound_import);
382719
	ch += PE_OUTPUT(coh_iat);
382719
	ch += PE_OUTPUT(coh_delay_import_descriptor);
382719
	ch += PE_OUTPUT(coh_clr_runtime_hdr);
382719
	ch += PE_OUTPUT(coh_reserved);
382719
382719
	ch += pe_output_hex_footer(ch);
382719
382719
	*ch = 0;
382719
382719
	if (pe_dprintf(pe_driver_fdout(dctx),"%s",buf) < 0)
382719
		return PERK_FILE_ERROR(dctx);
382719
382719
	return 0;
382719
}
382719
382719
int pe_hdrdump_opt_hdr(
382719
	const struct pe_driver_ctx *	dctx,
382719
	const struct pe_image_meta *	meta)
382719
{
382719
	switch (pe_image_bits(meta)) {
382719
		case 32:
382719
			return pe_hdrdump_opt_hdr_32(dctx,meta);
382719
382719
		case 64:
382719
			return pe_hdrdump_opt_hdr_64(dctx,meta);
382719
382719
		default:
382719
			return PERK_CUSTOM_ERROR(
382719
				dctx,PERK_ERR_UNSUPPORTED_ABI);
382719
	}
382719
}