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
210
211
212
213
214
215
216
|
/***************************************************************/
/* perk: PE Resource Kit */
/* Copyright (C) 2015--2021 SysDeer Technologies, LLC */
/* Released under GPLv2 and GPLv3; see COPYING.PERK. */
/***************************************************************/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <mdso/mdso_specs.h>
#include <mdso/mdso_structs.h>
#include <perk/perk.h>
#include <perk/perk_consts.h>
#include <perk/perk_output.h>
#include "perk_reader_impl.h"
#include "perk_driver_impl.h"
#include "perk_dprintf_impl.h"
#include "perk_errinfo_impl.h"
static int pretty_header(
int fdout,
const struct pe_common_ctx * cctx)
{
return (cctx->fmtflags & PERK_PRETTY_YAML)
? pe_dprintf(fdout,"dsolibs:\n")
: 0;
}
static int pretty_dsolib_header(
int fdout,
const struct pe_common_ctx * cctx,
const char * name)
{
if ((cctx->fmtflags & PERK_PRETTY_YAML) && (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS))
return pe_dprintf(fdout," %s:\n",name);
else if (cctx->fmtflags & PERK_PRETTY_YAML)
return pe_dprintf(fdout,"- %s:\n",name);
else if (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS)
return pe_dprintf(fdout,"%s:\n",name);
else
return pe_dprintf(fdout,"%s\n",name);
}
static int pretty_dsolib_item(
int fdout,
const struct pe_common_ctx * cctx,
const char * name)
{
if (cctx->fmtflags & PERK_PRETTY_YAML)
return pe_dprintf(fdout," - %s\n",name);
else
return pe_dprintf(fdout,"%s\n",name);
}
static unsigned char * dsolib_meta(const struct pe_image_meta * m, int i)
{
struct mdso_raw_meta_record_m32 * mdso32;
struct mdso_raw_meta_record_m64 * mdso64;
if (m->m_opt.oh_std.coh_magic == PE_MAGIC_PE32_PLUS) {
mdso64 = (struct mdso_raw_meta_record_m64 *)m->r_dsometa + i;
return mdso64->mrec_base;
} else {
mdso32 = (struct mdso_raw_meta_record_m32 *)m->r_dsometa + i;
return mdso32->mrec_base;
}
}
static char * dsolib_name(const struct pe_image_meta * m, int i)
{
uintptr_t va;
uint64_t rva;
uint32_t roffset;
struct mdso_raw_meta_record_m32 * mdso32;
struct mdso_raw_meta_record_m64 * mdso64;
if (m->m_opt.oh_std.coh_magic == PE_MAGIC_PE32_PLUS) {
mdso64 = (struct mdso_raw_meta_record_m64 *)m->r_dsometa + i;
va = pe_read_quad(mdso64->mrec_name);
rva = va - m->m_opt.oh_mem.coh_image_base;
if (rva >> 32)
return 0;
} else {
mdso32 = (struct mdso_raw_meta_record_m32 *)m->r_dsometa + i;
va = pe_read_long(mdso32->mrec_name);
rva = va - m->m_opt.oh_mem.coh_image_base;
}
if (pe_get_roffset_from_rva(m,(uint32_t)rva,&roffset) < 0)
return 0;
return m->r_image.map_addr + roffset;
}
static unsigned char * dsosym_meta(const struct pe_image_meta * m, int j)
{
uint64_t va;
uint32_t rva;
uint32_t roffset;
struct mdso_raw_sym_entry_m32 * sym32;
struct mdso_raw_sym_entry_m64 * sym64;
int idx;
if (m->m_opt.oh_std.coh_magic == PE_MAGIC_PE32_PLUS) {
sym64 = (struct mdso_raw_sym_entry_m64 *)m->r_dsosyms + j;
va = pe_read_quad(sym64->msym_meta);
rva = pe_read_long(sym64->msym_meta);
} else {
sym32 = (struct mdso_raw_sym_entry_m32 *)m->r_dsosyms + j;
va = pe_read_long(sym32->msym_meta);
rva = va;
}
if ((idx = pe_get_named_section_index(m,MDSO_META_SECTION)) >= 0)
if (idx != pe_get_block_section_index(m,&(struct pe_block){rva,0}))
return (unsigned char *)(-1);
if (va > rva)
return 0;
if (pe_get_roffset_from_rva(m,rva,&roffset) < 0)
return 0;
return (unsigned char *)m->r_image.map_addr + roffset;
}
static char * dsosym_string(const struct pe_image_meta * m, int j)
{
uint64_t va;
uint32_t rva;
uint32_t roffset;
struct mdso_raw_sym_entry_m32 * sym32;
struct mdso_raw_sym_entry_m64 * sym64;
int idx;
if (m->m_opt.oh_std.coh_magic == PE_MAGIC_PE32_PLUS) {
sym64 = (struct mdso_raw_sym_entry_m64 *)m->r_dsosyms + j;
va = pe_read_quad(sym64->msym_string);
rva = pe_read_long(sym64->msym_string);
} else {
sym32 = (struct mdso_raw_sym_entry_m32 *)m->r_dsosyms + j;
va = pe_read_long(sym32->msym_string);
rva = va;
}
if ((idx = pe_get_named_section_index(m,MDSO_STRS_SECTION)) >= 0)
if (idx != pe_get_block_section_index(m,&(struct pe_block){rva,0}))
return (char *)(-2);
if (va > rva)
return 0;
if (pe_get_roffset_from_rva(m,rva,&roffset) < 0)
return 0;
return m->r_image.map_addr + roffset;
}
int pe_output_mdso_libraries(
const struct pe_driver_ctx * dctx,
const struct pe_image_meta * m)
{
int i,j;
int fdout;
char * name;
unsigned char * symmeta;
unsigned char * dsometa;
char * symstr;
const struct pe_common_ctx * cctx = dctx->cctx;
fdout = pe_driver_fdout(dctx);
if (!m->m_stats.t_ndsolibs)
return 0;
if (pe_get_image_abi(m,0) == PE_ABI_UNSUPPORTED)
return PERK_CUSTOM_ERROR(
dctx,PERK_ERR_UNSUPPORTED_ABI);
if ((pretty_header(fdout,cctx)) < 0)
return PERK_FILE_ERROR(dctx);
for (i=0; i<m->m_stats.t_ndsolibs; i++) {
dsometa = dsolib_meta(m,i);
if (!(name = dsolib_name(m,i)))
return PERK_CUSTOM_ERROR(
dctx,PERK_ERR_IMAGE_MALFORMED);
if ((pretty_dsolib_header(fdout,cctx,name)) < 0)
return PERK_FILE_ERROR(dctx);
if (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS) {
for (j=0; j<m->m_stats.t_ndsosyms; j++) {
if (!(symmeta = dsosym_meta(m,j)))
return PERK_CUSTOM_ERROR(
dctx,PERK_ERR_IMAGE_MALFORMED);
if (!(symstr = dsosym_string(m,j)))
return PERK_CUSTOM_ERROR(
dctx,PERK_ERR_IMAGE_MALFORMED);
if (symmeta == dsometa)
if ((pretty_dsolib_item(fdout,cctx,symstr)) < 0)
return PERK_FILE_ERROR(dctx);
}
}
}
return 0;
}
|