Blame src/exports/pe_enum_image_exports.c

feffc7
/*****************************************************************************/
feffc7
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
feffc7
/*  Copyright (C) 2013,2014,2015  Z. Gilboa                                  */
feffc7
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
feffc7
/*****************************************************************************/
feffc7
feffc7
#include <psxtypes/psxtypes.h>
feffc7
#include <pemagine/pe_consts.h>
feffc7
#include <pemagine/pe_structs.h>
feffc7
#include <pemagine/pemagine.h>
feffc7
feffc7
int pe_enum_image_exports(
feffc7
	const void *				base,
feffc7
	pe_enum_image_exports_callback *	callback,
feffc7
	struct pe_export_sym  *			sym,
feffc7
	void *					ctx)
feffc7
{
d70e62
	int				ret;
d70e62
	uint32_t			idx;
d70e62
	uint32_t *			offset;
d70e62
	struct pe_raw_export_hdr *	exp_hdr;
d70e62
	uint32_t *			count;
d70e62
	uint32_t *			fn_addr;
d70e62
	uint32_t *			fn_names;
d70e62
	uint16_t *			fn_ordinals;
feffc7
feffc7
	if (!(exp_hdr = pe_get_image_export_hdr_addr(base,0))) {
feffc7
		callback(base,0,0,PE_CALLBACK_REASON_ERROR,ctx);
feffc7
		return -1;
feffc7
	}
feffc7
feffc7
	offset  = (uint32_t *)(exp_hdr->export_addr_tbl_rva);
feffc7
	fn_addr = (uint32_t *)pe_va_from_rva(base,*offset);
feffc7
feffc7
	offset   = (uint32_t *)(exp_hdr->name_ptr_rva);
feffc7
	fn_names = (uint32_t *)pe_va_from_rva(base,*offset);
feffc7
feffc7
	offset      = (uint32_t *)(exp_hdr->ordinal_tbl_rva);
feffc7
	fn_ordinals = (uint16_t *)pe_va_from_rva(base,*offset);
feffc7
feffc7
	if ((ret = callback(base,exp_hdr,0,PE_CALLBACK_REASON_INIT,ctx)) <= 0)
feffc7
		return ret;
feffc7
feffc7
	count = (uint32_t *)exp_hdr->num_of_name_ptrs;
feffc7
	sym->ordinal_base = (uint32_t *)exp_hdr->ordinal_base;
feffc7
feffc7
	for (idx=0; idx<*count; idx++) {
feffc7
		offset = (uint32_t *)pe_va_from_rva(fn_names,idx*sizeof(uint32_t));
feffc7
		sym->name = (char *)pe_va_from_rva(base,*offset);
feffc7
		sym->ordinal = (uint16_t *)pe_va_from_rva(fn_ordinals,idx*sizeof(uint16_t));
feffc7
feffc7
		offset  = (uint32_t *)pe_va_from_rva(fn_addr,(*sym->ordinal)*sizeof(uint32_t));
feffc7
		sym->addr = pe_va_from_rva(base,*offset);
feffc7
feffc7
		if ((ret = callback(base,exp_hdr,sym,PE_CALLBACK_REASON_ITEM,ctx)) <= 0)
feffc7
			return ret;
feffc7
	}
feffc7
feffc7
	return callback(base,exp_hdr,sym,PE_CALLBACK_REASON_DONE,ctx);
feffc7
}