Blame include/perk/perk.h

c0fbae
#ifndef PERK_H
c0fbae
#define PERK_H
c0fbae
c0fbae
#include <stdint.h>
c0fbae
#include <stdio.h>
c0fbae
1ef003
#include "perk_api.h"
c65ad9
#include "perk_consts.h"
c65ad9
#include "perk_structs.h"
c65ad9
#include "perk_meta.h"
c0fbae
c0fbae
#ifdef __cplusplus
c0fbae
extern "C" {
c0fbae
#endif
c0fbae
c0fbae
/* pre-alpha */
c0fbae
#ifndef PERK_APP
c0fbae
#ifndef PERK_PRE_ALPHA
c0fbae
#error  libperk: pre-alpha: ABI is not final!
c0fbae
#error  to use the library, please pass -DPERK_PRE_ALPHA to the compiler.
c0fbae
#endif
c0fbae
#endif
c0fbae
a3de1c
/* status codes */
a3de1c
#define PERK_OK				0x00
a3de1c
#define PERK_USAGE			0x01
b0d024
#define PERK_ERROR			0x02
a3de1c
a3de1c
/* driver flags */
a3de1c
#define PERK_DRIVER_VERBOSITY_NONE	0x0000
a3de1c
#define PERK_DRIVER_VERBOSITY_ERRORS	0x0001
a3de1c
#define PERK_DRIVER_VERBOSITY_STATUS	0x0002
a3de1c
#define PERK_DRIVER_VERBOSITY_USAGE	0x0004
a3de1c
#define PERK_DRIVER_CLONE_VECTOR	0x0008
a3de1c
8fd1f5
#define PERK_DRIVER_VERSION		0x0010
8fd1f5
#define PERK_DRIVER_DRY_RUN		0x0020
a3de1c
68f5d9
#define PERK_DRIVER_ANNOTATE_ALWAYS	0x1000
68f5d9
#define PERK_DRIVER_ANNOTATE_NEVER	0x2000
68f5d9
#define PERK_DRIVER_ANNOTATE_FULL	0x4000
68f5d9
a3de1c
/* unit action flags */
8fd1f5
#define PERK_ACTION_MAP_READWRITE	0x0001
a3de1c
e8769a
/* error flags */
e8769a
#define PERK_ERROR_TOP_LEVEL		0x0001
e8769a
#define PERK_ERROR_NESTED		0x0002
e8769a
#define PERK_ERROR_CHILD		0x0004
e8769a
#define PERK_ERROR_CUSTOM		0x0008
e8769a
ff121d
enum pe_custom_error {
ff121d
	PERK_ERR_FLOW_ERROR,
ff121d
	PERK_ERR_FLEE_ERROR,
ff121d
	PERK_ERR_NULL_CONTEXT,
ff121d
	PERK_ERR_NULL_IMAGE,
ff121d
	PERK_ERR_INVALID_CONTEXT,
ff121d
	PERK_ERR_INVALID_IMAGE,
ff121d
	PERK_ERR_IMAGE_SIZE_ZERO,
ff121d
	PERK_ERR_IMAGE_MALFORMED,
ff121d
	PERK_ERR_BAD_DOS_HEADER,
ff121d
	PERK_ERR_BAD_COFF_HEADER,
ff121d
	PERK_ERR_BAD_IMAGE_TYPE,
ff121d
	PERK_ERR_CAP,
ff121d
};
ff121d
b7025c
struct pe_source_version {
b7025c
	int		major;
b7025c
	int		minor;
b7025c
	int		revision;
b7025c
	const char *	commit;
b7025c
};
b7025c
c0fbae
struct pe_raw_image {
664d5b
	void *	addr;
c0fbae
	size_t	size;
c0fbae
};
c0fbae
cf4adc
struct pe_expsym {
cf4adc
	const char *	name;
cf4adc
	void *		eaddr;
cf4adc
	void *		maddr;
cf4adc
	uint32_t	roffset;
cf4adc
};
cf4adc
014adf
struct pe_meta_summary {
c3f597
	int32_t		nexpsyms;
c3f597
	int32_t		nimplibs;
c3f597
	int32_t		nrelocs;
c0fbae
};
c0fbae
c0fbae
struct pe_image_meta {
014adf
	struct pe_meta_summary		summary;
c0fbae
	struct pe_meta_image_dos_hdr	dos;
c0fbae
	struct pe_meta_coff_file_hdr	coff;
c0fbae
	struct pe_meta_opt_hdr		opt;
c0fbae
	struct pe_meta_sec_hdr *	sectbl;
c0fbae
c004d0
	struct pe_meta_export_hdr	edata;
c004d0
	struct pe_meta_sec_hdr *	hedata;
c004d0
c004d0
	struct pe_meta_import_hdr *	idata;
c004d0
	struct pe_meta_sec_hdr *	hidata;
c004d0
c004d0
	struct pe_raw_image		image;
99c452
	struct pe_raw_image_dos_hdr *	ados;
11fb1c
	struct pe_raw_coff_file_hdr *	acoff;
8cb34f
	union  pe_raw_opt_hdr *		aopt;
a09b56
	struct pe_raw_sec_hdr *		asectbl;
c004d0
8dd726
	struct pe_raw_export_hdr *	aedata;
34ee9e
	struct pe_raw_import_hdr *	aidata;
c0fbae
};
c0fbae
e8769a
struct pe_error_info {
f006c4
	const struct pe_driver_ctx *	edctx;
f006c4
	const struct pe_unit_ctx *	euctx;
f006c4
	const char *			eunit;
f006c4
	int				esyscode;
f006c4
	int				elibcode;
f006c4
	const char *			efunction;
f006c4
	int				eline;
f006c4
	unsigned			eflags;
f006c4
	void *				eany;
e8769a
};
e8769a
76aabf
struct pe_common_ctx {
a3de1c
	uint64_t			drvflags;
a3de1c
	uint64_t			actflags;
a3de1c
	uint64_t			fmtflags;
a3de1c
};
a3de1c
a3de1c
struct pe_driver_ctx {
a3de1c
	const char **			units;
a3de1c
	const char *			program;
a3de1c
	const char *			module;
c61328
	const struct pe_common_ctx *	cctx;
e8769a
	struct pe_error_info **		errv;
85119e
	void *				any;
a3de1c
};
a3de1c
a3de1c
struct pe_unit_ctx {
d7ed3e
	const char * const *		path;
d7ed3e
	const struct pe_raw_image *	map;
d7ed3e
	const struct pe_image_meta *	meta;
85119e
	void *				any;
a3de1c
};
a3de1c
bf447f
struct pe_info_string {
bf447f
	char				buffer[128];
bf447f
};
bf447f
b7025c
/* package info */
b7025c
perk_api				const struct pe_source_version * pe_source_version(void);
b7025c
a3de1c
/* driver api */
7d4640
perk_api int  pe_get_driver_ctx		(char ** argv, char ** envp, uint32_t flags, struct pe_driver_ctx **);
fa22b4
perk_api void pe_free_driver_ctx	(struct pe_driver_ctx *);
a3de1c
a9788e
perk_api int  pe_get_unit_ctx		(const struct pe_driver_ctx *, const char * path, struct pe_unit_ctx **);
fa22b4
perk_api void pe_free_unit_ctx		(struct pe_unit_ctx *);
a3de1c
c0fbae
/* utility api */
6fe7c1
perk_api int  pe_main			(int, char **, char **);
204e71
perk_api int  pe_output_image_category	(const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *);
437706
perk_api int  pe_output_image_sections	(const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *);
f779e3
perk_api int  pe_output_image_symbols	(const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *);
0d4d47
perk_api int  pe_output_image_strings	(const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *);
f64a5b
perk_api int  pe_output_export_symbols	(const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *);
251206
perk_api int  pe_output_import_libraries(const struct pe_driver_ctx *, const struct pe_image_meta *, FILE *);
27496e
27496e
/* error trace api */
68f5d9
perk_api int  pe_output_error_record	(const struct pe_driver_ctx *, const struct pe_error_info *);
68f5d9
perk_api int  pe_output_error_vector	(const struct pe_driver_ctx *);
c0fbae
c0fbae
/* high-level api */
9a46b6
perk_api int  pe_map_raw_image		(const struct pe_driver_ctx *, int fd, const char * path, int prot, struct pe_raw_image *);
2b7f36
perk_api int  pe_unmap_raw_image	(struct pe_raw_image *);
c0fbae
0e3fd6
perk_api int  pe_get_image_meta		(const struct pe_driver_ctx *, const struct pe_raw_image *, struct pe_image_meta **);
2b7f36
perk_api void pe_free_image_meta	(struct pe_image_meta *);
c0fbae
2b7f36
perk_api int  pe_get_named_section_index(const struct pe_image_meta *, const char * name);
2b7f36
perk_api int  pe_get_block_section_index(const struct pe_image_meta *, const struct pe_block *);
62b9db
62b9db
perk_api int  pe_get_roffset_from_rva	(const struct pe_image_meta *, uint32_t rva, uint32_t * roffset);
62b9db
perk_api int  pe_get_rva_from_roffset	(const struct pe_image_meta *, uint32_t roffset, uint32_t * rva);
62b9db
cf4adc
perk_api int  pe_get_expsym_by_name	(const struct pe_image_meta *, const char * name, struct pe_expsym * optional);
cf4adc
perk_api int  pe_get_expsym_by_index	(const struct pe_image_meta *, unsigned index, struct pe_expsym * optional);
c7ca52
bf447f
/* info api */
8c7b6a
perk_api int  pe_get_image_abi		(const struct pe_image_meta *, struct pe_info_string * optional);
6aec96
perk_api int  pe_get_image_subtype	(const struct pe_image_meta *, struct pe_info_string * optional);
049a18
perk_api int  pe_get_image_subsystem	(const struct pe_image_meta *, struct pe_info_string * optional);
bf447f
perk_api int  pe_get_image_framework	(const struct pe_image_meta *, struct pe_info_string * optional);
bf447f
c0fbae
/* low-level api */
99c452
perk_api int  pe_read_dos_header	(const struct pe_raw_image_dos_hdr *,	struct pe_meta_image_dos_hdr *);
11fb1c
perk_api int  pe_read_coff_header	(const struct pe_raw_coff_file_hdr *,	struct pe_meta_coff_file_hdr *);
8cb34f
perk_api int  pe_read_optional_header	(const union  pe_raw_opt_hdr *,		struct pe_meta_opt_hdr *);
a09b56
perk_api int  pe_read_section_header	(const struct pe_raw_sec_hdr *,		struct pe_meta_sec_hdr *);
8dd726
perk_api int  pe_read_export_header	(const struct pe_raw_export_hdr *,	struct pe_meta_export_hdr *);
34ee9e
perk_api int  pe_read_import_header	(const struct pe_raw_import_hdr *,	struct pe_meta_import_hdr *);
c0fbae
e75572
perk_api int  pe_read_coff_symbol	(const struct pe_raw_coff_symbol *,	struct pe_meta_coff_symbol *,
edaa40
					 const struct pe_meta_coff_file_hdr *,	void * base);
edaa40
34ee9e
perk_api int  pe_read_import_lookup	(const union  pe_raw_import_lookup *,	struct pe_meta_import_lookup *,
381efa
					 uint32_t magic);
381efa
c0fbae
#ifdef __cplusplus
c0fbae
}
c0fbae
#endif
c0fbae
c0fbae
#endif