summaryrefslogtreecommitdiffhomepage
path: root/src/meta/pe_get_symbol_name.c
blob: f558c2f1c32bbea3e3806b5efbc8a70543e87d42 (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
/*****************************************************************************/
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
/*  Copyright (C) 2013--2020  SysDeer Technologies, LLC                      */
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
/*****************************************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pemagine.h>


struct pe_symbol_name_ctx {
	const void *	addr;
	char *		name;
};

static int pe_enum_exports_callback(
	const void *			base,
	struct pe_raw_export_hdr *	exp_hdr,
	struct pe_export_sym *		sym,
	enum pe_callback_reason		reason,
	void *				context);

#if (__SIZEOF_POINTER__ == 4)
static char * pe_get_imported_symbol_info_32(
	const void *			sym_addr,
	struct pe_ldr_tbl_entry **	ldr_tbl_entry);
#endif

#if (__SIZEOF_POINTER__ == 8)
static char * pe_get_imported_symbol_info_64(
	const void *			sym_addr,
	struct pe_ldr_tbl_entry **	ldr_tbl_entry);
#endif


char * pe_get_symbol_name(const void * base, const void * sym_addr)
{
	struct pe_export_sym		exp_item;
	struct pe_symbol_name_ctx	ctx;

	ctx.name = 0;
	ctx.addr = sym_addr;

	pe_enum_image_exports(
		base,
		pe_enum_exports_callback,
		&exp_item,
		&ctx);

	return ctx.name;
}


char * pe_get_import_symbol_info(
	const void *			sym_addr,
	struct pe_ldr_tbl_entry **	ldr_tbl_entry)
{
	#if (__SIZEOF_POINTER__ == 4)
		return pe_get_imported_symbol_info_32(
			sym_addr,
			ldr_tbl_entry);
	#elif (__SIZEOF_POINTER__ == 8)
		return pe_get_imported_symbol_info_64(
			sym_addr,
			ldr_tbl_entry);
	#endif
}

static int pe_enum_exports_callback(
	const void *			base,
	struct pe_raw_export_hdr *	exp_hdr,
	struct pe_export_sym  *		sym,
	enum pe_callback_reason		reason,
	void *				context)
{
	struct pe_symbol_name_ctx * ctx;

	(void)base;
	(void)exp_hdr;

	if (reason != PE_CALLBACK_REASON_ITEM)
		return 1;

	ctx = (struct pe_symbol_name_ctx *)context;

	if (sym->addr == ctx->addr) {
		ctx->name = sym->name;
		return 0;
	} else
		return 1;
}


#if (__SIZEOF_POINTER__ == 4)
static char * pe_get_imported_symbol_info_32(
	const void *			sym_addr,
	struct pe_ldr_tbl_entry **	ldr_tbl_entry)
{
	struct symbol {
		unsigned char	call;
		unsigned char	ds;
		unsigned char	sym_addr[4];
		unsigned char	padding[2];
	};

	char *				fn_name;
	struct pe_ldr_tbl_entry *	mod_info;
	void *				mod_base;
	uint32_t ***			sym_redirected_addr;
	struct symbol *			sym;

	fn_name  = 0;
	sym = (struct symbol *)sym_addr;

	if ((sym->call == 0xff) && (sym->ds == 0x25))
		if ((sym_redirected_addr = (uint32_t ***)sym->sym_addr))
			if ((mod_info = pe_get_symbol_module_info(
					**sym_redirected_addr)))
				if ((mod_base = mod_info->dll_base))
					fn_name = pe_get_symbol_name(
						mod_base,
						**sym_redirected_addr);
	if (fn_name && ldr_tbl_entry)
		*ldr_tbl_entry = mod_info;

	return fn_name;
}
#endif


#if (__SIZEOF_POINTER__ == 8)
static char * pe_get_imported_symbol_info_64(
	const void *			sym_addr,
	struct pe_ldr_tbl_entry **	ldr_tbl_entry)
{
	struct symbol {
		unsigned char	call;
		unsigned char	ds;
		unsigned char	sym_addr[4];
		unsigned char	padding[2];
	};

	char *				fn_name;
	struct pe_ldr_tbl_entry *	mod_info;
	void *				mod_base;
	uint32_t *			sym_offset;
	uint32_t			offset;
	struct symbol *			sym;

	fn_name  = 0;
	mod_info = 0;
	sym = (struct symbol *)sym_addr;

	if ((sym->call == 0xff) && (sym->ds == 0x25)) {
		if ((sym_offset = (uint32_t *)sym->sym_addr)) {
			offset   = *sym_offset;
			sym_addr = *(void **)(offset + (uintptr_t)(++sym_offset));
		} else
			sym_addr = 0;

		if (sym_addr && (mod_info = pe_get_symbol_module_info(sym_addr)))
			if ((mod_base = mod_info->dll_base))
				fn_name = pe_get_symbol_name(
					mod_base,
					sym_addr);
	}

	if (fn_name && ldr_tbl_entry)
		*ldr_tbl_entry = mod_info;

	return fn_name;
}
#endif