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

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

/* private args structure */
struct pe_symbol_name_ctx {
	const void *	addr;
	char *		name;
};

/* private forward declarations */
static int pe_enum_exports_callback(
	const void *			base,
	struct pe_export_hdr *		exp_hdr,
	struct pe_export_sym *		sym,
	enum pe_callback_reason		reason,
	void *				context);

#if defined (__NT32)
static char * pe_get_imported_symbol_info_32(
	const void *			sym_addr,
	void **				sym_image_addr,
	char **				sym_name,
	struct pe_ldr_tbl_entry **	ldr_tbl_entry);
#endif

#if defined (__NT64)
static char * pe_get_imported_symbol_info_64(
	const void *			sym_addr,
	void **				sym_image_addr,
	char **				sym_name,
	struct pe_ldr_tbl_entry **	ldr_tbl_entry);
;
#endif

pe_api
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;
}


pe_api
char * pe_get_import_symbol_info(
	const void *			sym_addr,
	void **				sym_image_addr,
	char **				sym_name,
	struct pe_ldr_tbl_entry **	ldr_tbl_entry)
{
	#if defined(__NT32)
		return pe_get_imported_symbol_info_32(
			sym_addr,
			sym_image_addr,
			sym_name,
			ldr_tbl_entry);
	#elif defined (__NT64)
		return pe_get_imported_symbol_info_64(
			sym_addr,
			sym_image_addr,
			sym_name,
			ldr_tbl_entry);
	#endif
}

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

	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;
}


#ifdef __NT32
static char * pe_get_imported_symbol_info_32(
	const void *			sym_addr,
	void **				sym_image_addr,
	char **				sym_name,
	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;
	mod_info = 0;
	sym = (struct symbol *)sym_addr;

	if ((sym->call == 0xff) && (sym->ds == 0x25)) {
		sym_redirected_addr = (uint32_t ***)sym->sym_addr;

		if (sym_redirected_addr)
			mod_info = pe_get_symbol_module_info(**sym_redirected_addr);

		if (mod_info)
			mod_base = mod_info->dll_base;
		else
			mod_base = (void *)0;

		if (mod_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


#ifdef __NT64
static char * pe_get_imported_symbol_info_64(
	const void *			sym_addr,
	void **				sym_image_addr,
	char **				sym_name,
	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)) {
		sym_offset = (uint32_t *)sym->sym_addr;

		if (sym_offset) {
			offset		= *sym_offset;
			sym_addr	= *(void **)(offset + (uintptr_t)(++sym_offset));
			mod_info	= pe_get_symbol_module_info(sym_addr);
		}

		if (mod_info)
			mod_base = mod_info->dll_base;
		else
			mod_base = (void *)0;

		if (mod_base)
			fn_name = pe_get_symbol_name(mod_base,sym_addr);
	}

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

		if (sym_image_addr)
			*sym_image_addr = (void *)sym_addr;

		if (sym_name)
			*sym_name = fn_name;
	}

	return fn_name;
}
#endif