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

#include <psxtypes/psxtypes.h>
#include <pemagine/pe_consts.h>
#include <pemagine/pe_structs.h>
#include <pemagine/pemagine.h>
#include "pe_impl.h"

static __inline__ int pe_addr_within_bounds(void * addr, void * bottom, void * top)
{
	return (((uintptr_t)addr >= (uintptr_t)bottom) && ((uintptr_t)addr < (uintptr_t)top));
}

static void * pe_resolve_forwarder_rva(char * forwarder)
{
	uint16_t	soname[512];
	uint16_t *	wch;
	char *		mark;
	char *		dot;
	void *		base;

	for (mark=forwarder; *mark; mark++)
		(void)0;

	if (mark - forwarder + 5 > 512)
		return 0;

	for (dot=&mark[-1]; dot>forwarder && *dot!='.'; dot--)
		(void)0;

	if (*dot != '.')
		return 0;

	mark = forwarder;
	wch  = soname;

	for (; mark<dot && (unsigned)*mark<0x80; )
		*wch++ = *mark++;

	if (mark < dot)
		return 0;

	*wch++ = '.';
	*wch++ = 'd';
	*wch++ = 'l';
	*wch++ = 'l';
	*wch++ = 0;

	if (!(base = pe_get_module_handle(soname)))
		return 0;

	return pe_get_procedure_address(base,++dot);
}

int pe_get_export_symbol_info(
	const void *		base,
	const char *		name,
	struct pe_export_sym *	sym)
{
	struct pe_raw_export_hdr * exp_hdr;

	ssize_t		len;
	uint32_t *	rva_offset;
	uintptr_t *	addr_offset;
	uintptr_t	addr;

	uint32_t	sec_size;
	void *		sec_addr_cap;

	uint32_t *	fn_addr;
	uint32_t *	fn_names;
	uint16_t *	fn_ordinals;
	uint32_t *	addr_entries;
	uint32_t *	name_entries;

	char *		exp_name;
	const char *	src;
	const char *	dst;
	ssize_t		match;

	uint32_t	lower;
	uint32_t	upper;
	uint32_t	idx;

	/* init */
	sym->ordinal_base	= 0;
	sym->ordinal		= 0;
	sym->addr		= 0;
	sym->forwarder_rva	= 0;
	sym->name		= 0;
	sym->status		= 0;

	if (!(exp_hdr = pe_get_image_export_hdr_addr(base,&sec_size)))
		return -1;

	sec_addr_cap = pe_va_from_rva(exp_hdr,sec_size);
	rva_offset   = (uint32_t *)(exp_hdr->eh_export_addr_tbl_rva);
	fn_addr      = (uint32_t *)pe_va_from_rva(base,*rva_offset);

	rva_offset   = (uint32_t *)(exp_hdr->eh_name_ptr_rva);
	fn_names     = (uint32_t *)pe_va_from_rva(base,*rva_offset);

	rva_offset   = (uint32_t *)(exp_hdr->eh_ordinal_tbl_rva);
	fn_ordinals  = (uint16_t *)pe_va_from_rva(base,*rva_offset);

	addr_entries = (uint32_t *)exp_hdr->eh_addr_tbl_entries;
	name_entries = (uint32_t *)exp_hdr->eh_num_of_name_ptrs;


	/* by ordinal? */
	if ((intptr_t)name < 0x10000) {
		sym->ordinal_base = (uint32_t *)exp_hdr->eh_ordinal_base;

		/* the array is zero-based, but ordinals are normally one-based... */
		if (((intptr_t)name - *sym->ordinal_base + 1) > *addr_entries)
			return -1;

		addr        = ((uintptr_t)name - *sym->ordinal_base) * sizeof(uint32_t);
		rva_offset  = (uint32_t *)pe_va_from_rva(fn_addr,addr);
		addr_offset = (uintptr_t *)pe_va_from_rva(base,*rva_offset);

		if (pe_addr_within_bounds(addr_offset,exp_hdr,sec_addr_cap)) {
			sym->forwarder_rva = addr_offset;
			sym->addr = pe_resolve_forwarder_rva((char *)addr_offset);
		} else {
			sym->forwarder_rva = 0;
			sym->addr = addr_offset;
		}

		return 0;
	}

	if ((len = pe_impl_strlen_ansi(name)) < 0)
		return -1;

	len++;
	lower  = 0;
	upper  = *name_entries;

	while (lower < upper) {
		idx        = (lower + upper) / 2;
		rva_offset = (uint32_t *)pe_va_from_rva(fn_names,idx*sizeof(uint32_t));
		exp_name   = (char *)pe_va_from_rva(base,*rva_offset);

		src = name;
		dst = exp_name;

		for (match=0; (match<len) && (*src==*dst); match++) {
				src++;
				dst++;
		}

		if (match == len) {
			sym->ordinal_base = (uint32_t *)exp_hdr->eh_ordinal_base;
			sym->ordinal      = (uint16_t *)pe_va_from_rva(fn_ordinals,idx*sizeof(uint16_t));

			rva_offset  = (uint32_t *)pe_va_from_rva(fn_addr,(*sym->ordinal)*sizeof(uint32_t));
			addr_offset = (uintptr_t *)pe_va_from_rva(base,*rva_offset);

			if (pe_addr_within_bounds(addr_offset,exp_hdr,sec_addr_cap)) {
				sym->forwarder_rva = addr_offset;
				sym->addr = pe_resolve_forwarder_rva((char *)addr_offset);
			} else {
				sym->forwarder_rva = 0;
				sym->addr = addr_offset;
			}

			return 0;
		}

		else {
			if (*src > *dst)
				lower = idx + 1;
			else
				upper = idx;
		}
	}

	return -1;
}