summaryrefslogtreecommitdiffhomepage
path: root/src/output/pe_output_image_sections.c
blob: 9233fefb6291477560404866fa2977036ff620a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2025  SysDeer Technologies, LLC        */
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
/***************************************************************/

#include <stdio.h>

#include <perk/perk.h>
#include <perk/perk_output.h>
#include <perk/perk_structs.h>
#include "perk_driver_impl.h"
#include "perk_dprintf_impl.h"
#include "perk_errinfo_impl.h"

static int pe_output_section_names(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	int i;

	for (i=0; i<meta->m_coff.cfh_num_of_sections; i++)
		if (pe_dprintf(fdout,"%s\n",meta->m_sectbl[i].sh_name) < 0)
			return PERK_FILE_ERROR(dctx);

	return 0;
}

static int pe_output_section_names_yaml(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	int i;

	if (pe_dprintf(fdout,"  - Sections:\n") < 0)
		return PERK_FILE_ERROR(dctx);

	for (i=0; i<meta->m_coff.cfh_num_of_sections; i++)
		if (pe_dprintf(fdout,"    - section: %s\n",meta->m_sectbl[i].sh_name) < 0)
			return PERK_FILE_ERROR(dctx);

	return 0;
}

static int pe_output_section_record_yaml(
	int                             fdout,
	const struct pe_driver_ctx *	dctx,
	const struct pe_meta_sec_hdr *  s,
	const unsigned char *           base)
{
	int                               i;
	const struct pe_raw_coff_reloc *  r;
	struct pe_meta_coff_reloc         m;

	if (pe_dprintf(fdout,
			"    - section:\n"
			"      - [ name:              %s ]\n"
			"      - [ virtual-size:      0x%08x ]\n"
			"      - [ virtual-addr:      0x%08x ]\n"
			"      - [ size-of-raw-data:  0x%08x ]\n"
			"      - [ ptr-to-raw-data:   0x%08x ]\n"
			"      - [ ptr-to-relocs:     0x%08x ]\n"
			"      - [ ptr-to-line-nums:  0x%08x ]\n"
			"      - [ num-of-relocs:     %u ]\n"
			"      - [ num-of-line-nums:  %u ]\n"
			"      - [ characteristics:   0x%08x ]\n"
			"\n",
			s->sh_name,
			s->sh_virtual_size,
			s->sh_virtual_addr,
			s->sh_size_of_raw_data,
			s->sh_ptr_to_raw_data,
			s->sh_ptr_to_relocs,
			s->sh_ptr_to_line_nums,
			s->sh_num_of_relocs,
			s->sh_num_of_line_nums,
			s->sh_characteristics) < 0)
		return PERK_FILE_ERROR(dctx);

	if (s->sh_num_of_relocs == 0)
		return 0;

	if (pe_dprintf(fdout,"      - relocation-records:\n") < 0)
		return PERK_FILE_ERROR(dctx);

	r = (const struct pe_raw_coff_reloc *)&base[s->sh_ptr_to_relocs];

	for (i=0; i<s->sh_num_of_relocs; i++) {
		pe_read_coff_reloc(&r[i],&m);

		if (pe_dprintf(
				fdout,
				"        - reloction-record:\n"
				"          - [ rva:  0x%08x ]\n"
				"          - [ sym:  0x%08x ]\n"
				"          - [ type: %d ]\n"
				"\n",
				m.rel_rva,
				m.rel_sym,
				m.rel_type) < 0)
			return PERK_FILE_ERROR(dctx);
	}

	return 0;
}

static int pe_output_section_records_yaml(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	int i;

	if (pe_dprintf(fdout,"  - Sections:\n") < 0)
		return PERK_FILE_ERROR(dctx);

	for (i=0; i<meta->m_coff.cfh_num_of_sections; i++)
		if (pe_output_section_record_yaml(
				fdout,dctx,
				&meta->m_sectbl[i],
				meta->r_image.map_addr) < 0)
			return PERK_NESTED_ERROR(dctx);

	return 0;
}

static int pe_output_image_sections_yaml(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	if (dctx->cctx->fmtflags & PERK_PRETTY_VERBOSE) {
		if (pe_output_section_records_yaml(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);
	} else {
		if (pe_output_section_names_yaml(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);
	}

	return 0;
}

int pe_output_image_sections(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta)
{
	int fdout = pe_driver_fdout(dctx);

	if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) {
		if (pe_output_image_sections_yaml(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);

	} else {
		if (pe_output_section_names(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);
	}

	return 0;
}