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

#include <string.h>
#include <stdbool.h>

#include <perk/perk.h>
#include <perk/perk_meta.h>

static const char const * pe_framework_str[PE_FRAMEWORK_CAP] = {
	[PE_FRAMEWORK_UNKNOWN]  = "unknown",
	[PE_FRAMEWORK_FREESTD]  = "freestd",
	[PE_FRAMEWORK_PSXSCL]   = "psxscl",
	[PE_FRAMEWORK_MIDIPIX]  = "midipix",
	[PE_FRAMEWORK_CYGONE]   = "cygone",
	[PE_FRAMEWORK_CYGWIN]   = "cygwin",
	[PE_FRAMEWORK_MINGW]    = "mingw",
	[PE_FRAMEWORK_MSYS]     = "msys",
	[PE_FRAMEWORK_SUACON]   = "suacon",
	[PE_FRAMEWORK_WINCON]   = "wincon",
	[PE_FRAMEWORK_WINCLI]   = "wincli",
	[PE_FRAMEWORK_WIN32]    = "win32",
};

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

static bool pe_image_is_cygwin(const struct pe_image_meta * m)
{
	int i;

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

	return false;
}

static bool pe_image_is_msys(const struct pe_image_meta * m)
{
	int i;

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

	return false;
}

static bool pe_image_is_mingw(const struct pe_image_meta * m)
{
	return ((pe_get_named_section_index(m,".CRT") >= 0)
		&& (pe_get_named_section_index(m,".bss") >= 0)
		&& (pe_get_named_section_index(m,".tls") >= 0));
}

int pe_get_image_framework(const struct pe_image_meta * m, struct pe_info_string * infostr)
{
	int framework;

	if (pe_get_named_section_index(m,".midipix") >= 0)
		framework = PE_FRAMEWORK_MIDIPIX;

	else if (pe_get_named_section_index(m,".freestd") >= 0)
		framework = PE_FRAMEWORK_FREESTD;

	else if (pe_get_named_section_index(m,".cygheap") >= 0)
		framework = PE_FRAMEWORK_CYGONE;

	else if (pe_image_is_psxscl(m))
		framework = PE_FRAMEWORK_PSXSCL;

	else if (pe_image_is_cygwin(m))
		framework = PE_FRAMEWORK_CYGWIN;

	else if (pe_image_is_msys(m))
		framework = PE_FRAMEWORK_MSYS;

	else if (pe_image_is_mingw(m))
		framework = PE_FRAMEWORK_MINGW;

	else if (m->opt.img.subsystem == PE_IMAGE_SUBSYSTEM_POSIX_CUI)
		framework = PE_FRAMEWORK_SUACON;

	else if (m->opt.img.subsystem == PE_IMAGE_SUBSYSTEM_WINDOWS_CUI)
		framework = PE_FRAMEWORK_WINCON;

	else if (m->opt.img.subsystem == PE_IMAGE_SUBSYSTEM_WINDOWS_GUI)
		framework = PE_FRAMEWORK_WIN32;

	else
		framework = PE_FRAMEWORK_UNKNOWN;

	if (infostr)
		strcpy(infostr->buffer,pe_framework_str[framework]);

	return framework;
}