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
|
/*****************************************************************************/
/* 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/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));
}
pe_api
int pe_get_export_symbol_info(
const void * base,
const char * name,
struct pe_export_sym * sym)
{
struct pe_export_hdr * exp_hdr;
ssize_t len;
uint32_t * rva_offset;
uintptr_t * addr_offset;
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;
size_t match;
uint32_t lower;
uint32_t upper;
uint32_t idx;
/* initialize pe_exp_item */
sym->ordinal_base = (uint32_t *)0;
sym->ordinal = (uint16_t *)0;
sym->addr = (void *)0;
sym->forwarder_rva = (void *)0;
sym->name = (char *)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->export_addr_tbl_rva);
fn_addr = (uint32_t *)pe_va_from_rva(base,*rva_offset);
rva_offset = (uint32_t *)(exp_hdr->name_ptr_rva);
fn_names = (uint32_t *)pe_va_from_rva(base,*rva_offset);
rva_offset = (uint32_t *)(exp_hdr->ordinal_tbl_rva);
fn_ordinals = (uint16_t *)pe_va_from_rva(base,*rva_offset);
addr_entries = (uint32_t *)exp_hdr->addr_tbl_entries;
name_entries = (uint32_t *)exp_hdr->num_of_name_ptrs;
/* by ordinal? */
if ((intptr_t)name < 0x10000) {
sym->ordinal_base = (uint32_t *)exp_hdr->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;
rva_offset = (uint32_t *)pe_va_from_rva(fn_addr,((uintptr_t)name-*sym->ordinal_base)*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 = 0;
sym->addr = addr_offset;
} else
/* todo: resolve forwarder address */
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->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)) {
/* todo: resolve forwarder address */
sym->forwarder_rva = 0;
sym->addr = 0;
} else {
sym->forwarder_rva = 0;
sym->addr = addr_offset;
}
return 0;
}
else {
if (*src > *dst)
lower = idx + 1;
else
upper = idx;
}
}
/* export name not found */
return -1;
}
|