summaryrefslogtreecommitdiffhomepage
path: root/src/output/pe_output_mdso_libraries.c
blob: aa70727189085b1fe070f94b331ed46f65761829 (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
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2017  Z. Gilboa                        */
/*  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_output.h>
#include "perk_reader_impl.h"
#include "perk_errinfo_impl.h"

static int pretty_header(const struct pe_common_ctx * cctx, FILE * fout)
{
	return (cctx->fmtflags & PERK_PRETTY_YAML)
		? fputs("dsolibs:\n",fout)
		: 0;
}

static int pretty_dsolib_header(const struct pe_common_ctx * cctx, const char * name, FILE * fout)
{
	if ((cctx->fmtflags & PERK_PRETTY_YAML) && (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS))
		return fprintf(fout,"  %s:\n",name);
	else if (cctx->fmtflags & PERK_PRETTY_YAML)
		return fprintf(fout,"- %s:\n",name);
	else if (cctx->fmtflags & PERK_OUTPUT_MDSO_SYMS)
		return fprintf(fout,"%s:\n",name);
	else
		return fprintf(fout,"%s\n",name);
}

static int pretty_dsolib_item(const struct pe_common_ctx * cctx, const char * name, FILE * fout)
{
	if (cctx->fmtflags & PERK_PRETTY_YAML)
		return fprintf(fout,"  - %s\n",name);
	else
		return fprintf(fout,"%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)
{
	uintptr_t			va;
	uint64_t			rva;
	uint32_t			roffset;
	struct mdso_raw_sym_entry_m32 *	sym32;
	struct mdso_raw_sym_entry_m64 *	sym64;

	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   = va - m->m_opt.oh_mem.coh_image_base;

		if (rva >> 32)
			return 0;
	} else {
		sym32 = (struct mdso_raw_sym_entry_m32 *)m->r_dsosyms + j;
		va    = pe_read_long(sym32->msym_meta);
		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 (unsigned char *)m->r_image.map_addr + roffset;
}

static char * dsosym_string(const struct pe_image_meta * m, int j)
{
	uintptr_t			va;
	uint64_t			rva;
	uint32_t			roffset;
	struct mdso_raw_sym_entry_m32 *	sym32;
	struct mdso_raw_sym_entry_m64 *	sym64;

	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   = va - m->m_opt.oh_mem.coh_image_base;

		if (rva >> 32)
			return 0;
	} else {
		sym32 = (struct mdso_raw_sym_entry_m32 *)m->r_dsosyms + j;
		va    = pe_read_long(sym32->msym_string);
		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;
}

int pe_output_mdso_libraries(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	m,
	FILE *				fout)
{
	int				i,j;
	char *				name;
	unsigned char *			symmeta;
	unsigned char *			dsometa;
	char *				symstr;
	const struct pe_common_ctx *	cctx = dctx->cctx;

	if (!m->m_stats.t_ndsolibs)
		return 0;

	if (!fout)
		fout = stdout;

	if (pe_get_image_abi(m,0) == PE_ABI_UNSUPPORTED)
		return PERK_CUSTOM_ERROR(
			dctx,PERK_ERR_UNSUPPORTED_ABI);

	if ((pretty_header(cctx,fout)) < 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(cctx,name,fout)) < 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(cctx,symstr,fout)) < 0)
						return PERK_FILE_ERROR(dctx);
			}
		}
	}

	return 0;
}