summaryrefslogtreecommitdiffhomepage
path: root/src/meta/pe_get_image_stack_heap_info.c
blob: 67a6de9ff084fdf298e51c3a726946ab5ef8b4f1 (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
/*****************************************************************************/
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
/*  Copyright (C) 2013--2017  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>

static uint32_t pe_read_raw_data_32(unsigned char * addr)
{
	uint32_t * p32;

	p32 = (uint32_t *)addr;
	return *p32;
}

static uint64_t pe_read_raw_data_64(unsigned char * addr)
{
	uint64_t * p64;

	p64 = (uint64_t *)addr;
	return *p64;
}

int pe_get_image_stack_heap_info(const void * base, struct pe_stack_heap_info * stack_heap_info)
{
	uint16_t *		magic;
	union pe_raw_opt_hdr *	hdr;

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

	magic = (uint16_t *)hdr;

	switch (*magic) {
		case PE_MAGIC_PE32:
			stack_heap_info->size_of_stack_reserve = pe_read_raw_data_32(hdr->opt_hdr_32.coh_size_of_stack_reserve);
			stack_heap_info->size_of_stack_commit  = pe_read_raw_data_32(hdr->opt_hdr_32.coh_size_of_stack_commit);
			stack_heap_info->size_of_heap_reserve  = pe_read_raw_data_32(hdr->opt_hdr_32.coh_size_of_heap_reserve);
			stack_heap_info->size_of_heap_commit   = pe_read_raw_data_32(hdr->opt_hdr_32.coh_size_of_heap_commit);
			break;

		case PE_MAGIC_PE32_PLUS:
			stack_heap_info->size_of_stack_reserve = pe_read_raw_data_64(hdr->opt_hdr_64.coh_size_of_stack_reserve);
			stack_heap_info->size_of_stack_commit  = pe_read_raw_data_64(hdr->opt_hdr_64.coh_size_of_stack_commit);
			stack_heap_info->size_of_heap_reserve  = pe_read_raw_data_64(hdr->opt_hdr_64.coh_size_of_heap_reserve);
			stack_heap_info->size_of_heap_commit   = pe_read_raw_data_64(hdr->opt_hdr_64.coh_size_of_heap_commit);
			break;

		default:
			return -1;
	}

	return 0;
}