Blame src/meta/pe_get_symbol_module_info.c

feffc7
/*****************************************************************************/
feffc7
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
b6ea9a
/*  Copyright (C) 2013--2017  Z. Gilboa                                      */
feffc7
/*  Released under GPLv2 and GPLv3; see COPYING.PEMAGINE.                    */
feffc7
/*****************************************************************************/
feffc7
feffc7
#include <psxtypes/psxtypes.h>
feffc7
#include <pemagine/pemagine.h>
feffc7
feffc7
feffc7
struct pe_module_info_ctx {
feffc7
	uintptr_t			sym_addr;
feffc7
	struct pe_ldr_tbl_entry *	image_ldr_tbl_entry;
feffc7
};
feffc7
feffc7
feffc7
feffc7
static int pe_get_symbol_module_info_callback(
feffc7
	struct pe_ldr_tbl_entry *	ldr_tbl_entry,
feffc7
	enum pe_callback_reason		reason,
feffc7
	void *				context)
feffc7
{
feffc7
	uintptr_t			image_base;
feffc7
	uintptr_t			image_size;
feffc7
	struct pe_module_info_ctx *	ctx;
feffc7
feffc7
	if (reason != PE_CALLBACK_REASON_ITEM)
feffc7
		return 1;
feffc7
feffc7
	ctx = (struct pe_module_info_ctx *)context;
feffc7
	image_base = (uintptr_t)(ldr_tbl_entry->dll_base);
feffc7
	image_size = (uintptr_t)(ldr_tbl_entry->size_of_image);
feffc7
feffc7
	if ((ctx->sym_addr > image_base) && (ctx->sym_addr < image_base + image_size)) {
feffc7
		/* within bounds */
feffc7
		ctx->image_ldr_tbl_entry = ldr_tbl_entry;
feffc7
		return 0;
feffc7
	} else
feffc7
		return 1;
feffc7
}
feffc7
feffc7
feffc7
struct pe_ldr_tbl_entry * pe_get_symbol_module_info(const void * sym_addr)
feffc7
{
feffc7
	struct pe_module_info_ctx ctx;
feffc7
feffc7
	ctx.sym_addr = (uintptr_t)sym_addr;
feffc7
	ctx.image_ldr_tbl_entry = 0;
feffc7
feffc7
	pe_enum_modules_in_load_order(
feffc7
		pe_get_symbol_module_info_callback,
feffc7
		&ctx;;
feffc7
feffc7
	return ctx.image_ldr_tbl_entry;
feffc7
}