summaryrefslogtreecommitdiffhomepage
path: root/src/output/pe_output_image_type.c
blob: 1d0ff591ffbee685f3b8fdc39ed7004a5bf4c3d0 (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
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
/***************************************************************/

#include <stdint.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <perk/perk.h>
#include <perk/perk_output.h>
#include "perk_errinfo_impl.h"

static const char const * pe_subsystem_name[0x10] = {
	[PE_IMAGE_SUBSYSTEM_UNKNOWN]                  = "unknown",
	[PE_IMAGE_SUBSYSTEM_NATIVE]                   = "native",
	[PE_IMAGE_SUBSYSTEM_WINDOWS_GUI]              = "windows",
	[PE_IMAGE_SUBSYSTEM_WINDOWS_CUI]              = "console",
	[PE_IMAGE_SUBSYSTEM_POSIX_CUI]                = "posix",
	[PE_IMAGE_SUBSYSTEM_WINDOWS_CE_GUI]           = "wince",
	[PE_IMAGE_SUBSYSTEM_EFI_APPLICATION]          = "efi_app",
	[PE_IMAGE_SUBSYSTEM_EFI_BOOT_SERVICE_DRIVER]  = "efi_driver",
	[PE_IMAGE_SUBSYSTEM_EFI_ROM]                  = "efi_rom",
	[PE_IMAGE_SUBSYSTEM_XBOX]                     = "xbox"
};

static const char * pretty_abi(const struct pe_unit_ctx * uctx)
{
	switch (uctx->meta->opt.std.magic) {
		case PE_MAGIC_PE32:
			return "PE32";

		case PE_MAGIC_PE32_PLUS:
			return "PE64";

		default:
			return "INTERNAL_ERROR";
	}
}

static const char * pretty_type(const struct pe_unit_ctx * uctx)
{
	if (uctx->meta->coff.characteristics & PE_IMAGE_FILE_DLL)
		return "dll";
	else
		return "exe";
}

static const char * pretty_subsystem(const struct pe_unit_ctx * uctx)
{
	if (uctx->meta->opt.img.subsystem >= 0x10)
		return "UNEXPECTED_SUBSYSTEM";

	else if (!pe_subsystem_name[uctx->meta->opt.img.subsystem])
		return "UNEXPECTED_SUBSYSTEM";

	else
		return pe_subsystem_name[uctx->meta->opt.img.subsystem];
}

static bool pe_image_is_psxscl(const struct pe_unit_ctx * uctx)
{
	return (!uctx->meta->summary.nimplibs
		&& !pe_get_expsym_by_name(uctx->meta,"__psx_init",0));
}

static bool pe_image_is_cygwin(const struct pe_unit_ctx * uctx)
{
	int				i;
	const struct pe_image_meta *	meta = uctx->meta;

	for (i=0; i<uctx->meta->summary.nimplibs; i++)
		if (!(strcmp(meta->idata[i].name,"cygwin1.dll")))
			return true;

	return false;
}

static bool pe_image_is_msys(const struct pe_unit_ctx * uctx)
{
	int				i;
	const struct pe_image_meta *	meta = uctx->meta;

	for (i=0; i<uctx->meta->summary.nimplibs; i++)
		if (!(strcmp(meta->idata[i].name,"msys-2.0.dll")))
			return true;

	return false;
}

static bool pe_image_is_mingw(const struct pe_unit_ctx * uctx)
{
	return ((pe_get_named_section_index(uctx->meta,".CRT") >= 0)
		&& (pe_get_named_section_index(uctx->meta,".bss") >= 0)
		&& (pe_get_named_section_index(uctx->meta,".tls") >= 0));
}

static const char * pretty_framework(const struct pe_unit_ctx * uctx)
{
	if (pe_get_named_section_index(uctx->meta,".midipix") >= 0)
		return "midipix";

	else if (pe_get_named_section_index(uctx->meta,".freestd") >= 0)
		return "freestd";

	else if (pe_get_named_section_index(uctx->meta,".cygheap") >= 0)
		return "cygone";

	else if (pe_image_is_psxscl(uctx))
		return "psxscl";

	else if (pe_image_is_cygwin(uctx))
		return "cygwin";

	else if (pe_image_is_msys(uctx))
		return "msys";

	else if (pe_image_is_mingw(uctx))
		return "mingw";

	else if (uctx->meta->opt.img.subsystem == PE_IMAGE_SUBSYSTEM_POSIX_CUI)
		return "suacon";

	else if (uctx->meta->opt.img.subsystem == PE_IMAGE_SUBSYSTEM_WINDOWS_CUI)
		return "wincon";

	else if (uctx->meta->opt.img.subsystem == PE_IMAGE_SUBSYSTEM_WINDOWS_GUI)
		return "win32";

	else
		return "unknown";
}

int pe_output_image_type(
	const struct pe_driver_ctx *	dctx,
	const struct pe_unit_ctx *	uctx,
	FILE *				fout)
{
	if (!fout)
		fout = stdout;

	if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) {
		if (fprintf(fout,"%s:\n- %s:\n- %s:\n- %s:\n- %s:\n",
				*uctx->path,
				pretty_abi(uctx),
				pretty_type(uctx),
				pretty_subsystem(uctx),
				pretty_framework(uctx)) < 0)
			return PERK_FILE_ERROR(dctx);
	} else {
		if (fprintf(fout,"%s-%s-%s-%s\n",
				pretty_abi(uctx),
				pretty_type(uctx),
				pretty_subsystem(uctx),
				pretty_framework(uctx)) < 0)
			return PERK_FILE_ERROR(dctx);
	}

	return 0;
}