summaryrefslogtreecommitdiffhomepage
path: root/include/perk/perk.h
blob: ca51be476be56aad3dfb6c293481a1923485ce0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#ifndef PERK_H
#define PERK_H

#include <stdint.h>
#include <stdio.h>

#include "perk_api.h"
#include "perk_consts.h"
#include "perk_structs.h"
#include "perk_meta.h"
#include "perk_output.h"

#ifdef __cplusplus
extern "C" {
#endif

/* pre-alpha */
#ifndef PERK_APP
#ifndef PERK_PRE_ALPHA
#error  libperk: pre-alpha: ABI is not final!
#error  to use the library, please pass -DPERK_PRE_ALPHA to the compiler.
#endif
#endif

/* status codes */
#define PERK_OK				0x00
#define PERK_USAGE			0x01
#define PERK_BAD_OPT			0x02
#define PERK_BAD_OPT_VAL		0x03
#define PERK_IO_ERROR			0xA0
#define PERK_MAP_ERROR			0xA1
#define PERK_BAD_DOS_HEADER		0xA2
#define PERK_BAD_COFF_HEADER		0xA3
#define PERK_BAD_IMAGE_TYPE		0xA4
#define PERK_MALFORMED_IMAGE		0xA5

/* driver flags */
#define PERK_DRIVER_VERBOSITY_NONE	0x0000
#define PERK_DRIVER_VERBOSITY_ERRORS	0x0001
#define PERK_DRIVER_VERBOSITY_STATUS	0x0002
#define PERK_DRIVER_VERBOSITY_USAGE	0x0004
#define PERK_DRIVER_CLONE_VECTOR	0x0008

#define PERK_DRIVER_FLAG_VERSION	0x0010
#define PERK_DRIVER_FLAG_DRY_RUN	0x0020

/* unit action flags */
#define PERK_ACTION_FLAG_MAP_READWRITE	0x0001

/* unit (text) output & format flags */
#define PERK_OUTPUT_FLAG_EXPORT_SYMS	0x0001

struct pe_raw_image {
	void *	addr;
	size_t	size;
};

struct pe_image_summary {
	uint32_t	num_of_export_syms;
	uint32_t	num_of_implibs;
	uint32_t	num_of_relocs;
};

struct pe_image_meta {
	struct pe_raw_image		image;
	struct pe_image_summary		summary;

	struct pe_meta_image_dos_hdr	dos;
	struct pe_meta_coff_file_hdr	coff;
	struct pe_meta_opt_hdr		opt;
	struct pe_meta_sec_hdr *	sectbl;

	struct pe_image_dos_hdr *	ados;
	struct pe_coff_file_hdr *	acoff;
	union  pe_opt_hdr *		aopt;
	struct pe_sec_hdr *		asectbl;

	struct pe_meta_export_hdr	edata;
	struct pe_export_hdr *		aedata;
	struct pe_meta_sec_hdr *	hedata;

	struct pe_meta_import_hdr *	idata;
	struct pe_import_hdr *		aidata;
	struct pe_meta_sec_hdr *	hidata;
};

struct pe_symbol_ctx {
	uint32_t			size;
	uint32_t			version;
	const char **			append;
	const char **			exclude;
};

struct pe_output_ctx {
	uint32_t			size;
	uint32_t			version;
	const char *			header;
	const char *			footer;
};

struct pe_linker_ctx {
	uint32_t			size;
	uint32_t			version;
};

struct pe_server_ctx {
	uint32_t			size;
	uint32_t			version;
};

struct pe_common_ctx {
	uint32_t			size;
	uint32_t			version;
	int				fdin;
	int				fdout;
	int				fderr;
	int				fdlog;
	int				fdsrc;
	int				fddst;
	int				fdtmp;
	int				prot;
	int				mode;
	int				status;
	uint64_t			drvflags;
	uint64_t			actflags;
	uint64_t			fmtflags;
	uint64_t			lnkflags;
	const char *			output;
	const char *			srcdir;
	const char *			dstdir;
	const char *			tmpdir;
	struct pe_symbol_ctx *		symctx;
	struct pe_output_ctx *		outctx;
	struct pe_linker_ctx *		lnkctx;
	struct pe_server_ctx *		srvctx;
};

struct pe_driver_ctx {
	const char **			units;
	const char *			program;
	const char *			module;
	struct pe_common_ctx		cctx;
};

struct pe_unit_ctx {
	const char *			path;
	struct pe_raw_image		map;
	struct pe_image_meta *		meta;
	struct pe_common_ctx		cctx;
};

/* driver api */
perk_api int pe_get_driver_ctx		(const char ** argv, const char ** envp, uint32_t flags, struct pe_driver_ctx **);
perk_api int pe_free_driver_ctx		(struct pe_driver_ctx *);

perk_api int pe_get_unit_ctx		(struct pe_driver_ctx *, const char * name, struct pe_unit_ctx **);
perk_api int pe_free_unit_ctx		(struct pe_unit_ctx *);

/* utility api */
perk_api int pe_output_export_symbols	(const struct pe_image_meta *, uint32_t flags, FILE *);

/* high-level api */
perk_api int pe_map_raw_image		(int fd, const char * name, int prot, struct pe_raw_image *);
perk_api int pe_unmap_raw_image		(struct pe_raw_image *);

perk_api int pe_get_image_meta		(const struct pe_raw_image *, struct pe_image_meta **);
perk_api int pe_free_image_meta		(struct pe_image_meta *);

perk_api int pe_get_named_section_index	(const struct pe_image_meta *, const char * name);
perk_api int pe_get_block_section_index	(const struct pe_image_meta *, const struct pe_block *);

/* low-level api */
perk_api int pe_read_dos_header		(const struct pe_image_dos_hdr *,	struct pe_meta_image_dos_hdr *);
perk_api int pe_read_coff_header	(const struct pe_coff_file_hdr *,	struct pe_meta_coff_file_hdr *);
perk_api int pe_read_optional_header	(const union  pe_opt_hdr *,		struct pe_meta_opt_hdr *);
perk_api int pe_read_section_header	(const struct pe_sec_hdr *,		struct pe_meta_sec_hdr *);
perk_api int pe_read_export_header	(const struct pe_export_hdr *,		struct pe_meta_export_hdr *);
perk_api int pe_read_import_header	(const struct pe_import_hdr *,		struct pe_meta_import_hdr *);
perk_api int pe_read_import_lookup_item	(const struct pe_import_lookup_item *,	struct pe_meta_import_lookup_item *, uint32_t magic);

#ifdef __cplusplus
}
#endif

#endif