Blame src/modules/pe_enum_modules.c

feffc7
/*****************************************************************************/
feffc7
/*  pemagination: a (virtual) tour into portable bits and executable bytes   */
59fd65
/*  Copyright (C) 2013--2016  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
#include "pe_impl.h"
feffc7
feffc7
static int pe_enum_modules(
feffc7
	pe_enum_modules_callback *	callback,
feffc7
	const uintptr_t			in_order_member_offset,
feffc7
	void *				context)
feffc7
{
feffc7
	int				ret;
feffc7
	uintptr_t 			peb_tbl_addr;
feffc7
	struct pe_peb_ldr_data *	peb_ldr_data;
feffc7
	struct pe_ldr_tbl_entry *	ldr_tbl_entry;
feffc7
	struct pe_list_entry *		plist_head;
feffc7
	struct pe_list_entry * 		plist_current;
feffc7
feffc7
	peb_ldr_data = (struct pe_peb_ldr_data *)pe_get_peb_ldr_data_address();
feffc7
	plist_head   = (struct pe_list_entry *)0;
feffc7
feffc7
	if ((intptr_t)peb_ldr_data == 0) {
feffc7
		callback(
feffc7
			0,
feffc7
			PE_CALLBACK_REASON_ERROR,
feffc7
			context);
feffc7
		return -1;
feffc7
	}
feffc7
feffc7
	ret = callback(
feffc7
		0,
feffc7
		PE_CALLBACK_REASON_INIT,
feffc7
		context);
feffc7
feffc7
	if (ret <= 0)
feffc7
		return ret;
feffc7
feffc7
	switch (in_order_member_offset) {
feffc7
		case IN_LOAD_ORDER_MODULE_LIST_OFFSET:
feffc7
			plist_head = peb_ldr_data->in_load_order_module_list.flink;
feffc7
			break;
feffc7
feffc7
		case IN_MEMORY_ORDER_MODULE_LIST_OFFSET:
feffc7
			plist_head = peb_ldr_data->in_memory_order_module_list.flink;
feffc7
			break;
feffc7
feffc7
		case IN_INITIALIZATION_ORDER_MODULE_LIST_OFFSET:
feffc7
			plist_head = peb_ldr_data->in_init_order_module_list.flink;
feffc7
			break;
feffc7
	}
feffc7
feffc7
	plist_current = plist_head;
feffc7
8a6eed
	do {
feffc7
		peb_tbl_addr	= (uintptr_t)plist_current - in_order_member_offset;
feffc7
		ldr_tbl_entry	= (struct pe_ldr_tbl_entry *)peb_tbl_addr;
feffc7
feffc7
		ret = callback(
feffc7
			ldr_tbl_entry,
feffc7
			PE_CALLBACK_REASON_ITEM,
feffc7
			context);
feffc7
feffc7
		if (ret <= 0)
feffc7
			return ret;
feffc7
		else
feffc7
			plist_current = plist_current->flink;
feffc7
feffc7
	} while (plist_current != plist_head);
feffc7
feffc7
	ret = callback(
feffc7
			ldr_tbl_entry,
feffc7
			PE_CALLBACK_REASON_DONE,
feffc7
			context);
feffc7
feffc7
	return ret;
feffc7
}
feffc7
feffc7
feffc7
int pe_enum_modules_in_load_order(pe_enum_modules_callback * fn, void * ctx)
feffc7
{
feffc7
	return pe_enum_modules(fn,IN_LOAD_ORDER_MODULE_LIST_OFFSET,ctx);
feffc7
}
feffc7
8a6eed
feffc7
int pe_enum_modules_in_memory_order(pe_enum_modules_callback * fn, void * ctx)
feffc7
{
feffc7
	return pe_enum_modules(fn,IN_MEMORY_ORDER_MODULE_LIST_OFFSET,ctx);
feffc7
}
feffc7
8a6eed
feffc7
int pe_enum_modules_in_init_order(pe_enum_modules_callback * fn, void * ctx)
feffc7
{
feffc7
	return pe_enum_modules(fn,IN_INITIALIZATION_ORDER_MODULE_LIST_OFFSET,ctx);
feffc7
}