Blame src/output/pe_output_image_sections.c

437706
/***************************************************************/
437706
/*  perk: PE Resource Kit                                      */
6a5301
/*  Copyright (C) 2015--2025  SysDeer Technologies, LLC        */
437706
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
437706
/***************************************************************/
437706
437706
#include <stdio.h>
437706
437706
#include <perk/perk.h>
437706
#include <perk/perk_output.h>
67bba7
#include "perk_driver_impl.h"
67bba7
#include "perk_dprintf_impl.h"
437706
#include "perk_errinfo_impl.h"
437706
72d820
static int pe_output_section_names(
437706
	const struct pe_driver_ctx *	dctx,
72d820
	const struct pe_image_meta *	meta,
72d820
	int                             fdout)
437706
{
72d820
	int i;
437706
72d820
	for (i=0; i<meta->m_coff.cfh_num_of_sections; i++)
72d820
		if (pe_dprintf(fdout,"%s\n",meta->m_sectbl[i].sh_name) < 0)
437706
			return PERK_FILE_ERROR(dctx);
437706
72d820
	return 0;
72d820
}
72d820
72d820
static int pe_output_section_names_yaml(
72d820
	const struct pe_driver_ctx *	dctx,
72d820
	const struct pe_image_meta *	meta,
72d820
	int                             fdout)
72d820
{
72d820
	int i;
72d820
72d820
	if (pe_dprintf(fdout,"  - Sections:\n") < 0)
72d820
		return PERK_FILE_ERROR(dctx);
437706
58ced9
	for (i=0; i<meta->m_coff.cfh_num_of_sections; i++)
72d820
		if (pe_dprintf(fdout,"    - section: %s\n",meta->m_sectbl[i].sh_name) < 0)
437706
			return PERK_FILE_ERROR(dctx);
437706
437706
	return 0;
437706
}
72d820
97dd39
static int pe_output_section_record_yaml(
97dd39
	int                             fdout,
97dd39
	const struct pe_driver_ctx *	dctx,
97dd39
	const struct pe_meta_sec_hdr *  s)
97dd39
{
97dd39
	if (pe_dprintf(fdout,
97dd39
			"    - section:\n"
97dd39
			"      - [ name:              %s ]\n"
97dd39
			"      - [ virtual-size:      0x%08x ]\n"
97dd39
			"      - [ virtual-addr:      0x%08x ]\n"
97dd39
			"      - [ size-of-raw-data:  0x%08x ]\n"
97dd39
			"      - [ ptr-to-raw-data:   0x%08x ]\n"
97dd39
			"      - [ ptr-to-relocs:     0x%08x ]\n"
97dd39
			"      - [ ptr-to-line-nums:  0x%08x ]\n"
97dd39
			"      - [ num-of-relocs:     %u ]\n"
97dd39
			"      - [ num-of-line-nums:  %u ]\n"
97dd39
			"      - [ characteristics:   0x%08x ]\n"
97dd39
			"\n",
97dd39
			s->sh_name,
97dd39
			s->sh_virtual_size,
97dd39
			s->sh_virtual_addr,
97dd39
			s->sh_size_of_raw_data,
97dd39
			s->sh_ptr_to_raw_data,
97dd39
			s->sh_ptr_to_relocs,
97dd39
			s->sh_ptr_to_line_nums,
97dd39
			s->sh_num_of_relocs,
97dd39
			s->sh_num_of_line_nums,
97dd39
			s->sh_characteristics) < 0)
97dd39
		return PERK_FILE_ERROR(dctx);
97dd39
97dd39
	return 0;
97dd39
}
97dd39
72d820
static int pe_output_section_records_yaml(
72d820
	const struct pe_driver_ctx *	dctx,
72d820
	const struct pe_image_meta *	meta,
72d820
	int                             fdout)
72d820
{
97dd39
	int i;
97dd39
97dd39
	if (pe_dprintf(fdout,"  - Sections:\n") < 0)
97dd39
		return PERK_FILE_ERROR(dctx);
97dd39
97dd39
	for (i=0; i<meta->m_coff.cfh_num_of_sections; i++)
97dd39
		if (pe_output_section_record_yaml(fdout,dctx,&meta->m_sectbl[i]) < 0)
97dd39
			return PERK_NESTED_ERROR(dctx);
97dd39
72d820
	return 0;
72d820
}
72d820
72d820
static int pe_output_image_sections_yaml(
72d820
	const struct pe_driver_ctx *	dctx,
72d820
	const struct pe_image_meta *	meta,
72d820
	int                             fdout)
72d820
{
72d820
	if (dctx->cctx->fmtflags & PERK_PRETTY_VERBOSE) {
72d820
		if (pe_output_section_records_yaml(dctx,meta,fdout) < 0)
72d820
			return PERK_NESTED_ERROR(dctx);
72d820
	} else {
72d820
		if (pe_output_section_names_yaml(dctx,meta,fdout) < 0)
72d820
			return PERK_NESTED_ERROR(dctx);
72d820
	}
72d820
72d820
	return 0;
72d820
}
72d820
72d820
int pe_output_image_sections(
72d820
	const struct pe_driver_ctx *	dctx,
72d820
	const struct pe_image_meta *	meta)
72d820
{
72d820
	int fdout = pe_driver_fdout(dctx);
72d820
72d820
	if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) {
72d820
		if (pe_output_image_sections_yaml(dctx,meta,fdout) < 0)
72d820
			return PERK_NESTED_ERROR(dctx);
72d820
72d820
	} else {
72d820
		if (pe_output_section_names(dctx,meta,fdout) < 0)
72d820
			return PERK_NESTED_ERROR(dctx);
72d820
	}
72d820
72d820
	return 0;
72d820
}