Blame src/reader/pe_read_optional_header.c

e2e2c2
/***************************************************************/
e2e2c2
/*  perk: PE Resource Kit                                      */
e2e2c2
/*  Copyright (C) 2015--2016  Z. Gilboa                        */
e2e2c2
/*  Released under GPLv2 and GPLv3; see COPYING.PERK.          */
e2e2c2
/***************************************************************/
e2e2c2
c0fbae
#include <string.h>
c0fbae
c0fbae
#include <perk/perk.h>
2d6c77
#include "perk_endian_impl.h"
413f56
#include "perk_reader_impl.h"
c0fbae
8cb34f
static int pe_read_optional_header_structs(const union pe_raw_opt_hdr * p, struct pe_meta_opt_hdr * m)
2d6c77
{
659299
	unsigned int			i;
659299
	struct pe_block *		pdir;
659299
	const  unsigned char *		mark;
659299
8cb34f
	struct pe_raw_opt_hdr_std *	astd;
8cb34f
	struct pe_raw_opt_hdr_vers *	avers;
8cb34f
	struct pe_raw_opt_hdr_align *	aalign;
8cb34f
	struct pe_raw_opt_hdr_img *	aimg;
8cb34f
	struct pe_raw_opt_hdr_ldr *	aldr;
b5f7f5
d34ab8
	m->std.coh_magic = pe_read_short(p->opt_hdr_32.magic);
2d6c77
d34ab8
	switch (m->std.coh_magic) {
c0fbae
		case PE_MAGIC_PE32:
8cb34f
			astd	= (struct pe_raw_opt_hdr_std *)p;
8cb34f
			avers	= (struct pe_raw_opt_hdr_vers *)&p->opt_hdr_32.major_os_ver;
8cb34f
			aalign	= (struct pe_raw_opt_hdr_align *)&p->opt_hdr_32.section_align;
8cb34f
			aimg	= (struct pe_raw_opt_hdr_img *)&p->opt_hdr_32.size_of_image;
8cb34f
			aldr	= (struct pe_raw_opt_hdr_ldr *)&p->opt_hdr_32.loader_flags;
c0fbae
			break;
c0fbae
c0fbae
		case PE_MAGIC_PE32_PLUS:
8cb34f
			astd	= (struct pe_raw_opt_hdr_std *)p;
8cb34f
			avers	= (struct pe_raw_opt_hdr_vers *)&p->opt_hdr_64.major_os_ver;
8cb34f
			aalign	= (struct pe_raw_opt_hdr_align *)&p->opt_hdr_64.section_align;
8cb34f
			aimg	= (struct pe_raw_opt_hdr_img *)&p->opt_hdr_64.size_of_image;
8cb34f
			aldr	= (struct pe_raw_opt_hdr_ldr *)&p->opt_hdr_64.loader_flags;
c0fbae
			break;
c0fbae
c0fbae
		default:
ff121d
			return PERK_ERR_BAD_IMAGE_TYPE;
c0fbae
	}
c0fbae
c0fbae
	/* std */
d34ab8
	m->std.coh_major_linker_ver		= astd->coh_major_linker_ver[0];
d34ab8
	m->std.coh_minor_linker_ver		= astd->coh_minor_linker_ver[0];
c0fbae
d34ab8
	m->std.coh_size_of_code			= pe_read_long(astd->coh_size_of_code);
d34ab8
	m->std.coh_size_of_inited_data		= pe_read_long(astd->coh_size_of_inited_data);
d34ab8
	m->std.coh_size_of_uninited_data	= pe_read_long(astd->coh_size_of_uninited_data);
d34ab8
	m->std.coh_entry_point			= pe_read_long(astd->coh_entry_point);
d34ab8
	m->std.coh_base_of_code			= pe_read_long(astd->coh_base_of_code);
c0fbae
c0fbae
	/* vers */
9fdefd
	m->vers.coh_major_os_ver		= pe_read_short(avers->coh_major_os_ver);
9fdefd
	m->vers.coh_minor_os_ver		= pe_read_short(avers->coh_minor_os_ver);
9fdefd
	m->vers.coh_major_image_ver		= pe_read_short(avers->coh_major_image_ver);
9fdefd
	m->vers.coh_minor_image_ver		= pe_read_short(avers->coh_minor_image_ver);
9fdefd
	m->vers.coh_major_subsys_ver		= pe_read_short(avers->coh_major_subsys_ver);
9fdefd
	m->vers.coh_minor_subsys_ver		= pe_read_short(avers->coh_minor_subsys_ver);
c0fbae
9fdefd
	m->vers.coh_win32_ver			= pe_read_long(avers->coh_win32_ver);
c0fbae
c0fbae
	/* align */
ca9b3d
	m->align.coh_section_align		= pe_read_long(aalign->coh_section_align);
ca9b3d
	m->align.coh_file_align			= pe_read_long(aalign->coh_file_align);
c0fbae
c0fbae
	/* img */
b6ce09
	m->img.coh_size_of_image		= pe_read_long(aimg->coh_size_of_image);
b6ce09
	m->img.coh_size_of_headers		= pe_read_long(aimg->coh_size_of_headers);
b6ce09
	m->img.coh_checksum			= pe_read_long(aimg->coh_checksum);
c0fbae
b6ce09
	m->img.coh_subsystem			= pe_read_short(aimg->coh_subsystem);
b6ce09
	m->img.coh_dll_characteristics		= pe_read_short(aimg->coh_dll_characteristics);
c0fbae
c0fbae
	/* ldr */
01f370
	m->ldr.coh_loader_flags			= pe_read_long(aldr->coh_loader_flags);
01f370
	m->ldr.coh_rva_and_sizes		= pe_read_long(aldr->coh_rva_and_sizes);
c0fbae
659299
	/* dirs */
01f370
	if (m->ldr.coh_rva_and_sizes > 0x10)
659299
		return PERK_ERR_BAD_IMAGE_TYPE;
659299
01f370
	if (m->ldr.coh_rva_and_sizes < 0x10)
659299
		memset(&m->dirs,0,sizeof(m->dirs));
659299
659299
	mark = p->opt_hdr_64.export_tbl;
0d33f1
	pdir = &m->dirs.coh_export_tbl;
659299
01f370
	for (i=0; i<m->ldr.coh_rva_and_sizes; i++) {
a402c4
		pdir[i].dh_rva  = pe_read_long(&mark[i*8]);
a402c4
		pdir[i].dh_size = pe_read_long(&mark[i*8+4]);
659299
	}
659299
2d6c77
	return 0;
2d6c77
}
2d6c77
8cb34f
int pe_read_optional_header(const union pe_raw_opt_hdr * p, struct pe_meta_opt_hdr * m)
2d6c77
{
2d6c77
	int ret;
2d6c77
56cb69
	if ((ret = pe_read_optional_header_structs(p,m)))
56cb69
		return ret;
c0fbae
d34ab8
	switch (m->std.coh_magic) {
c0fbae
		case PE_MAGIC_PE32:
012431
			m->mem.coh_base_of_data             = pe_read_long(p->opt_hdr_32.base_of_data);
012431
			m->mem.coh_image_base               = pe_read_long(p->opt_hdr_32.image_base);
012431
			m->mem.coh_size_of_stack_reserve    = pe_read_long(p->opt_hdr_32.size_of_stack_reserve);
012431
			m->mem.coh_size_of_stack_commit     = pe_read_long(p->opt_hdr_32.size_of_stack_commit);
012431
			m->mem.coh_size_of_heap_reserve     = pe_read_long(p->opt_hdr_32.size_of_heap_reserve);
012431
			m->mem.coh_size_of_heap_commit      = pe_read_long(p->opt_hdr_32.size_of_heap_commit);
c0fbae
			break;
c0fbae
c0fbae
		case PE_MAGIC_PE32_PLUS:
012431
			m->mem.coh_base_of_data             = (uint64_t)-1;
012431
			m->mem.coh_image_base               = pe_read_quad(p->opt_hdr_64.image_base);
012431
			m->mem.coh_size_of_stack_reserve    = pe_read_quad(p->opt_hdr_64.size_of_stack_reserve);
012431
			m->mem.coh_size_of_stack_commit     = pe_read_quad(p->opt_hdr_64.size_of_stack_commit);
012431
			m->mem.coh_size_of_heap_reserve     = pe_read_quad(p->opt_hdr_64.size_of_heap_reserve);
012431
			m->mem.coh_size_of_heap_commit      = pe_read_quad(p->opt_hdr_64.size_of_heap_commit);
c0fbae
			break;
c7189e
	}
c0fbae
c0fbae
	return 0;
b5f7f5
}