summaryrefslogtreecommitdiff
path: root/src/output/pe_output_image_symbols.c
blob: 8f9abe6cf29930ea302067f2c3485082e6c19d73 (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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2025  SysDeer Technologies, LLC        */
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
/***************************************************************/

#include <stdio.h>
#include <string.h>
#include <inttypes.h>

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

#define PPRIX64 "%"PRIx64

static const char * pe_sym_type_desc_msb[0x10] = {
	[PE_IMAGE_SYM_DTYPE_NULL]     = "scalar variable, ",
	[PE_IMAGE_SYM_DTYPE_POINTER]  = "pointer to ",
	[PE_IMAGE_SYM_DTYPE_FUNCTION] = "function returning ",
	[PE_IMAGE_SYM_DTYPE_ARRAY]    = "array of ",
};

static const char * pe_sym_type_desc_lsb[0x10] = {
	[PE_IMAGE_SYM_TYPE_NULL]      = "unknown type",
	[PE_IMAGE_SYM_TYPE_VOID]      = "void",
	[PE_IMAGE_SYM_TYPE_CHAR]      = "char",
	[PE_IMAGE_SYM_TYPE_SHORT]     = "short",
	[PE_IMAGE_SYM_TYPE_INT]       = "int",
	[PE_IMAGE_SYM_TYPE_LONG]      = "long",
	[PE_IMAGE_SYM_TYPE_FLOAT]     = "float",
	[PE_IMAGE_SYM_TYPE_DOUBLE]    = "double",
	[PE_IMAGE_SYM_TYPE_STRUCT]    = "struct",
	[PE_IMAGE_SYM_TYPE_UNION]     = "union",
	[PE_IMAGE_SYM_TYPE_ENUM]      = "enum",
	[PE_IMAGE_SYM_TYPE_MOE]       = "enum member",
	[PE_IMAGE_SYM_TYPE_BYTE]      = "byte",
	[PE_IMAGE_SYM_TYPE_WORD]      = "word",
	[PE_IMAGE_SYM_TYPE_UINT]      = "uint",
	[PE_IMAGE_SYM_TYPE_DWORD]     = "dword",
};

static const char * pe_sym_storage_class_desc[256] = {
	[PE_IMAGE_SYM_CLASS_NULL]             = "storage class not assigned",
	[PE_IMAGE_SYM_CLASS_AUTOMATIC]        = "automatic (stack) variable",
	[PE_IMAGE_SYM_CLASS_EXTERNAL]         = "external symbol",
	[PE_IMAGE_SYM_CLASS_STATIC]           = "static variable",
	[PE_IMAGE_SYM_CLASS_REGISTER]         = "register variable",
	[PE_IMAGE_SYM_CLASS_EXTERNAL_DEF]     = "externally defined variable",
	[PE_IMAGE_SYM_CLASS_LABEL]            = "label",
	[PE_IMAGE_SYM_CLASS_UNDEFINED_LABEL]  = "undefined label",
	[PE_IMAGE_SYM_CLASS_MEMBER_OF_STRUCT] = "struct member",
	[PE_IMAGE_SYM_CLASS_ARGUMENT]         = "formal function parameter",
	[PE_IMAGE_SYM_CLASS_STRUCT_TAG]       = "struct tag",
	[PE_IMAGE_SYM_CLASS_MEMBER_OF_UNION]  = "union member",
	[PE_IMAGE_SYM_CLASS_UNION_TAG]        = "union tag",
	[PE_IMAGE_SYM_CLASS_TYPE_DEFINITION]  = "type definition",
	[PE_IMAGE_SYM_CLASS_UNDEFINED_STATIC] = "undefined (.bss) static data",
	[PE_IMAGE_SYM_CLASS_ENUM_TAG]         = "enum tag",
	[PE_IMAGE_SYM_CLASS_MEMBER_OF_ENUM]   = "enum member",
	[PE_IMAGE_SYM_CLASS_REGISTER_PARAM]   = "register parameter",
	[PE_IMAGE_SYM_CLASS_BIT_FIELD]        = "bit field",

	[PE_IMAGE_SYM_CLASS_BLOCK]            = "a beginning-of-block or end-of-block record",
	[PE_IMAGE_SYM_CLASS_FUNCTION]         = "function record",
	[PE_IMAGE_SYM_CLASS_END_OF_STRUCT]    = "end-of-struct",
	[PE_IMAGE_SYM_CLASS_FILE]             = "file",
	[PE_IMAGE_SYM_CLASS_SECTION]          = "section definition",
	[PE_IMAGE_SYM_CLASS_WEAK_EXTERN]      = "weak external",

	[PE_IMAGE_SYM_CLASS_CLR_TOKEN]        = "CLR token",

	[PE_IMAGE_SYM_CLASS_END_OF_FUNC]      = "end-of-function"
};

static int pe_output_symbol_names(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	struct pe_meta_coff_symbol *	symrec;

	for (symrec=meta->m_symtbl; symrec->cs_name; symrec++)
		if (pe_dprintf(fdout,"%s\n",symrec->cs_name) < 0)
			return PERK_FILE_ERROR(dctx);

	return 0;
}

static int pe_output_symbol_names_yaml(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	struct pe_meta_coff_symbol *	symrec;

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

	for (symrec=meta->m_symtbl; symrec->cs_name; symrec++)
		if (pe_dprintf(fdout,"    - [ symbol: %s ]\n",symrec->cs_name) < 0)
			return PERK_FILE_ERROR(dctx);

	return 0;
}

static int pe_output_symbol_records_yaml(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	struct pe_meta_coff_symbol *	symrec;
	struct pe_meta_sec_hdr *        sechdr;
	const char *                    secname;
	const char *                    classdesc;
	const char *                    typedesc[2];

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

	for (symrec=meta->m_symtbl; symrec->cs_name; symrec++) {
		switch (symrec->cs_section_number) {
			case PE_IMAGE_SYM_UNDEFINED:
				secname = "NULL (section is not yet defined)";
				break;

			case PE_IMAGE_SYM_ABSOLUTE:
				secname = "N/A (symbol is an absolute value)";
				break;

			case PE_IMAGE_SYM_DEBUG:
				secname = "N/A (debug symbol only)";
				break;

			default:
				sechdr  = &meta->m_sectbl[symrec->cs_section_number - 1];
				secname = sechdr->sh_long_name
					? sechdr->sh_long_name
					: sechdr->sh_name;
		}

		typedesc[0] = pe_sym_type_desc_msb[(symrec->cs_type >> 4) & 0x03];
		typedesc[1] = pe_sym_type_desc_lsb[(symrec->cs_type >> 0) & 0x0f];

		if (!(classdesc = pe_sym_storage_class_desc[symrec->cs_storage_class]))
			classdesc = "UNRECOGNIZED DATA";

		if (pe_dprintf(fdout,
				"    - symbol:\n"
				"      - [ name:       %s ]\n"
				"      - [ value:      0x%08x ]\n"
				"      - [ crc32:      0x%08x ]\n"
				"      - [ crc64:      0x"PPRIX64" ]\n"
				"      - [ secnum:     (%d) ]\n"
				"      - [ secname:    %s ]\n"
				"      - [ type:       0x%02X ]\n"
				"      - [ typedesc:   %s%s ]\n"
				"      - [ class:      %d ]\n"
				"      - [ classname:  %s ]\n"
				"      - [ aux-recs:   %d ]\n"
				"\n",
				symrec->cs_name,
				symrec->cs_value,
				symrec->cs_crc32,
				symrec->cs_crc64,
				symrec->cs_section_number,
				secname,
				symrec->cs_type,
				typedesc[0],typedesc[1],
				symrec->cs_storage_class,
				classdesc,
				symrec->cs_num_of_aux_recs) < 0)
			return PERK_FILE_ERROR(dctx);
	}

	return 0;
}

static int pe_output_image_symbols_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_symbol_records_yaml(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);
	} else {
		if (pe_output_symbol_names_yaml(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);
	}

	return 0;
}

int pe_output_image_symbols(
	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_symbols_yaml(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);

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

	return 0;
}