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
c0fbae
struct pe_image_summary {
c3f597
	int32_t		nexpsyms;
c3f597
	int32_t		nimplibs;
c3f597
	int32_t		nrelocs;
c0fbae
};
c0fbae
c0fbae
struct pe_image_meta {
c0fbae
	struct pe_raw_image		image;
c0fbae
	struct pe_image_summary		summary;
c0fbae
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
c0fbae
	struct pe_image_dos_hdr *	ados;
c0fbae
	struct pe_coff_file_hdr *	acoff;
c0fbae
	union  pe_opt_hdr *		aopt;
c0fbae
	struct pe_sec_hdr *		asectbl;
c0fbae
c0fbae
	struct pe_meta_export_hdr	edata;
c0fbae
	struct pe_export_hdr *		aedata;
c0fbae
	struct pe_meta_sec_hdr *	hedata;
c0fbae
c0fbae
	struct pe_meta_import_hdr *	idata;
c0fbae
	struct pe_import_hdr *		aidata;
c0fbae
	struct pe_meta_sec_hdr *	hidata;
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
	const char *			output;
a3de1c
	const char *			srcdir;
a3de1c
	const char *			dstdir;
a3de1c
	const char *			tmpdir;
88e832
	const struct pe_symbol_ctx *	symctx;
88e832
	const struct pe_output_ctx *	outctx;
88e832
	const struct pe_linker_ctx *	lnkctx;
88e832
	const struct pe_server_ctx *	srvctx;
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
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 **);
665ac3
perk_api int  pe_output_image_type	(const struct pe_driver_ctx *, const struct pe_unit_ctx *, 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 *);
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
c0fbae
/* low-level api */
2b7f36
perk_api int  pe_read_dos_header	(const struct pe_image_dos_hdr *,	struct pe_meta_image_dos_hdr *);
2b7f36
perk_api int  pe_read_coff_header	(const struct pe_coff_file_hdr *,	struct pe_meta_coff_file_hdr *);
2b7f36
perk_api int  pe_read_optional_header	(const union  pe_opt_hdr *,		struct pe_meta_opt_hdr *);
2b7f36
perk_api int  pe_read_section_header	(const struct pe_sec_hdr *,		struct pe_meta_sec_hdr *);
2b7f36
perk_api int  pe_read_export_header	(const struct pe_export_hdr *,		struct pe_meta_export_hdr *);
2b7f36
perk_api int  pe_read_import_header	(const struct pe_import_hdr *,		struct pe_meta_import_hdr *);
7d4d3d
perk_api int  pe_read_import_lookup_item(const union  pe_import_lookup_item *,	struct pe_meta_import_lookup_item *, uint32_t magic);
c0fbae
c0fbae
#ifdef __cplusplus
c0fbae
}
c0fbae
#endif
c0fbae
c0fbae
#endif