summaryrefslogtreecommitdiffhomepage
path: root/src/headers/pe_get_image_section_tbl_addr.c
blob: d74b55db72268851a9b0149ac9595a9c8219690c (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
/*****************************************************************************/
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
/*  Copyright (C) 2013--2020  SysDeer Technologies, LLC                      */
/*  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"


struct pe_raw_sec_hdr * pe_get_image_section_tbl_addr(const void * base)
{
	struct pe_raw_coff_image_hdr *	coff;
	union  pe_raw_opt_hdr *		opt;
	unsigned char *			mark;

	if (!(coff = pe_get_image_coff_hdr_addr(base)))
		return 0;

	if (!(opt = pe_get_image_opt_hdr_addr(base)))
		return 0;

	mark  = opt->opt_hdr_32.coh_magic;
	mark += coff->cfh_size_of_opt_hdr[1] << 8;
	mark += coff->cfh_size_of_opt_hdr[0];

	return (struct pe_raw_sec_hdr *)mark;
}


struct pe_raw_sec_hdr * pe_get_image_named_section_addr(const void * base, const char * name)
{
	uint16_t			count;
	struct pe_raw_sec_hdr *		hdr;
	struct pe_raw_coff_image_hdr *	coff;
	char *				ch;
	size_t				len;
	uint32_t			pos;
	uint64_t			sname;
	uint64_t *			shname;

	if (!(hdr = pe_get_image_section_tbl_addr(base)))
		return 0;

	if (!(coff = pe_get_image_coff_hdr_addr(base)))
		return 0;

	count  = coff->cfh_num_of_sections[1] << 8;
	count += coff->cfh_num_of_sections[0];

	if ((len = pe_impl_strlen_ansi(name)) > 8) {
		/* todo: long name support */
		return 0;

	} else if (len == 0) {
		return 0;

	} else {
		sname = 0;
		ch    = (char *)&sname;

		for (pos=0; pos<len; pos++)
			ch[pos] = name[pos];

		for (; count; hdr++,count--) {
			shname = (uint64_t *)&hdr->sh_name[0];

			if (*shname == sname)
				return hdr;
		}
	}

	return 0;
}


struct pe_raw_sec_hdr * pe_get_image_block_section_addr(
	const void * base,
	uint32_t     blk_rva,
	uint32_t     blk_size)
{
	uint32_t			low,size;
	uint16_t			count;
	struct pe_raw_sec_hdr *		hdr;
	struct pe_raw_coff_image_hdr *	coff;

	if (!(hdr = pe_get_image_section_tbl_addr(base)))
		return 0;

	if (!(coff = pe_get_image_coff_hdr_addr(base)))
		return 0;

	count  = coff->cfh_num_of_sections[1] << 8;
	count += coff->cfh_num_of_sections[0];

	for (; count; hdr++,count--) {
		low   = hdr->sh_virtual_addr[3] << 24;
		low  += hdr->sh_virtual_addr[2] << 16;
		low  += hdr->sh_virtual_addr[1] << 8;
		low  += hdr->sh_virtual_addr[0];

		size  = hdr->sh_virtual_size[3] << 24;
		size += hdr->sh_virtual_size[2] << 16;
		size += hdr->sh_virtual_size[1] << 8;
		size += hdr->sh_virtual_size[0];

		if ((low <= blk_rva) && (blk_rva + blk_size <= low + size))
			return hdr;
	}

	return 0;
}