summaryrefslogtreecommitdiffhomepage
path: root/src/output/pe_output_base_relocs.c
blob: ea398af14361a41a7afec9d745a5515805c8814a (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
/***************************************************************/
/*  perk: PE Resource Kit                                      */
/*  Copyright (C) 2015--2025  SysDeer Technologies, LLC        */
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
/***************************************************************/

#include <stdio.h>

#include <perk/perk.h>
#include <perk/perk_output.h>
#include <perk/perk_consts.h>
#include <perk/perk_structs.h>
#include "perk_driver_impl.h"
#include "perk_endian_impl.h"
#include "perk_reader_impl.h"
#include "perk_dprintf_impl.h"
#include "perk_errinfo_impl.h"

static const char * pe_base_reloc_type_desc[0x10] = {
	[PE_IMAGE_REL_BASED_ABSOLUTE]       = "ABSOLUTE",
	[PE_IMAGE_REL_BASED_HIGH]           = "HIGH",
	[PE_IMAGE_REL_BASED_LOW]            = "LOW",
	[PE_IMAGE_REL_BASED_HIGHLOW]        = "HIGHLOW",
	[PE_IMAGE_REL_BASED_HIGHADJ]        = "HIGHADJ",
	[PE_IMAGE_REL_BASED_RESERVED]       = "RESERVED",
	[PE_IMAGE_REL_BASED_DIR64]          = "DIR64",
};

static int pe_output_base_reloc_records(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	struct pe_raw_base_reloc_blk *  r;
	int                             i;
	unsigned                        j;
	uint32_t                        rva;
	uint32_t                        size;
	uint32_t                        nents;
	uint16_t                        fixup;
	uint32_t                        offset;
	const char *                    desc;

	for (i=0; i<meta->m_stats.t_nrelblks; i++) {
		r = meta->r_reltbl[i];

		rva  = pe_read_long(r->blk_rva);
		size = pe_read_long(r->blk_size);

		nents  = size - offsetof(struct pe_raw_base_reloc_blk,blk_data);
		nents /= sizeof(uint16_t);

		if (pe_dprintf(fdout,
				"Virtual Address: %08x, "
				"Chunk Size: %u (0x%x), "
				"Number of Fixups: %u\n",
				rva,size,size,nents) < 0)
			return PERK_FILE_ERROR(dctx);

		for (j=0; j<nents; j++) {
			fixup  = pe_read_short(r->blk_data[j]);
			desc   = pe_base_reloc_type_desc[fixup >> 12];
			offset = fixup & 0xfff;

			if (pe_dprintf(fdout,
					"\t"
					"reloc %4d "
					"offset %4x [%x] %s\n",
					j,offset,offset+rva,
					desc) < 0)
				return PERK_FILE_ERROR(dctx);
		}

		if (pe_dprintf(fdout,"\n") < 0)
			return PERK_FILE_ERROR(dctx);
	}

	return 0;
}

static int pe_output_base_relocs_yaml(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta,
	int                             fdout)
{
	struct pe_raw_base_reloc_blk *  r;
	int                             i;
	unsigned                        j;
	uint32_t                        rva;
	uint32_t                        size;
	uint32_t                        nents;
	uint16_t                        fixup;
	uint32_t                        offset;
	const char *                    desc;

	if (pe_dprintf(fdout," - Base relocation blocks:\n") < 0)
		return PERK_FILE_ERROR(dctx);

	for (i=0; i<meta->m_stats.t_nrelblks; i++) {
		r = meta->r_reltbl[i];

		rva  = pe_read_long(r->blk_rva);
		size = pe_read_long(r->blk_size);

		nents  = size - offsetof(struct pe_raw_base_reloc_blk,blk_data);
		nents /= sizeof(uint16_t);

		if (pe_dprintf(fdout,
				"    - base relocation block:\n"
				"      [ page-rva:      0x%08x ]\n"
				"      [ block-size:    0x%08x ]\n"
				"      [ fixup-entries: %u ]\n\n"
				"      - page fixups:\n",
				rva,size,nents) < 0)
			return PERK_FILE_ERROR(dctx);

		for (j=0; j<nents; j++) {
			fixup  = pe_read_short(r->blk_data[j]);
			desc   = pe_base_reloc_type_desc[fixup >> 12];
			offset = fixup & 0xfff;

			if (pe_dprintf(fdout,
					"        [ type   = %s ]\n"
					"        [ offset = 0x%03x ]\n\n",
					desc,offset) < 0)
				return PERK_FILE_ERROR(dctx);
		}
	}

	return 0;
}

int pe_output_base_relocs(
	const struct pe_driver_ctx *	dctx,
	const struct pe_image_meta *	meta)
{
	int fdout = pe_driver_fdout(dctx);

	if (dctx->cctx->fmtflags & PERK_PRETTY_YAML) {
		if (pe_output_base_relocs_yaml(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);

	} else {
		if (pe_output_base_reloc_records(dctx,meta,fdout) < 0)
			return PERK_NESTED_ERROR(dctx);
	}

	return 0;
}