summaryrefslogtreecommitdiffhomepage
path: root/src/headers/pe_get_image_entry_point_addr.c
blob: 9e197b8d010e14391322b5a1ad6f789b540e084e (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
/*****************************************************************************/
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
/*  Copyright (C) 2013--2016  Z. Gilboa                                      */
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
/*****************************************************************************/

#include <psxtypes/psxtypes.h>
#include <pemagine/pe_structs.h>
#include <pemagine/pe_consts.h>
#include <pemagine/pemagine.h>


void * pe_get_image_entry_point_addr(const void * base)
{
	uint16_t *		magic;
	union pe_raw_opt_hdr *	hdr;
	uint32_t *		rva;

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

	magic = (uint16_t *)hdr;

	switch (*magic) {
		case PE_MAGIC_PE32:
			rva = (uint32_t *)hdr->opt_hdr_32.coh_entry_point;
			break;

		case PE_MAGIC_PE32_PLUS:
			rva = (uint32_t *)hdr->opt_hdr_64.coh_entry_point;
			break;

		default:
			return 0;
	}

	return pe_va_from_rva(base,*rva);
}