pe_var.h revision 139743
1139743Simp/*-
2123474Swpaul * Copyright (c) 2003
3123474Swpaul *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
4123474Swpaul *
5123474Swpaul * Redistribution and use in source and binary forms, with or without
6123474Swpaul * modification, are permitted provided that the following conditions
7123474Swpaul * are met:
8123474Swpaul * 1. Redistributions of source code must retain the above copyright
9123474Swpaul *    notice, this list of conditions and the following disclaimer.
10123474Swpaul * 2. Redistributions in binary form must reproduce the above copyright
11123474Swpaul *    notice, this list of conditions and the following disclaimer in the
12123474Swpaul *    documentation and/or other materials provided with the distribution.
13123474Swpaul * 3. All advertising materials mentioning features or use of this software
14123474Swpaul *    must display the following acknowledgement:
15123474Swpaul *	This product includes software developed by Bill Paul.
16123474Swpaul * 4. Neither the name of the author nor the names of any co-contributors
17123474Swpaul *    may be used to endorse or promote products derived from this software
18123474Swpaul *    without specific prior written permission.
19123474Swpaul *
20123474Swpaul * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21123474Swpaul * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22123474Swpaul * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23123474Swpaul * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24123474Swpaul * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25123474Swpaul * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26123474Swpaul * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27123474Swpaul * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28123474Swpaul * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29123474Swpaul * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30123474Swpaul * THE POSSIBILITY OF SUCH DAMAGE.
31123474Swpaul *
32123474Swpaul * $FreeBSD: head/sys/compat/ndis/pe_var.h 139743 2005-01-05 22:34:37Z imp $
33123474Swpaul */
34123474Swpaul
35123474Swpaul#ifndef _PE_VAR_H_
36123474Swpaul#define _PE_VAR_H_
37123474Swpaul
38123474Swpaul/*
39123474Swpaul *  Image Format
40123474Swpaul */
41123474Swpaul
42123474Swpaul#define IMAGE_DOS_SIGNATURE                 0x5A4D      /* MZ */
43123474Swpaul#define IMAGE_OS2_SIGNATURE                 0x454E      /* NE */
44123474Swpaul#define IMAGE_OS2_SIGNATURE_LE              0x454C      /* LE */
45123474Swpaul#define IMAGE_VXD_SIGNATURE                 0x454C      /* LE */
46123474Swpaul#define IMAGE_NT_SIGNATURE                  0x00004550  /* PE00 */
47123474Swpaul
48123474Swpaul/*
49123474Swpaul * All PE files have one of these, just so if you attempt to
50123474Swpaul * run them, they'll print out a message telling you they can
51123474Swpaul * only be run in Windows.
52123474Swpaul */
53123474Swpaul
54123474Swpaulstruct image_dos_header {
55123474Swpaul	uint16_t	idh_magic;	/* Magic number */
56123474Swpaul	uint16_t	idh_cblp;	/* Bytes on last page of file */
57123474Swpaul	uint16_t	idh_cp;		/* Pages in file */
58123474Swpaul	uint16_t	idh_crlc;	/* Relocations */
59123474Swpaul	uint16_t	idh_cparhdr;	/* Size of header in paragraphs */
60123474Swpaul	uint16_t	idh_minalloc;	/* Minimum extra paragraphs needed */
61123474Swpaul	uint16_t	idh_maxalloc;	/* Maximum extra paragraphs needed */
62123474Swpaul	uint16_t	idh_ss;		/* Initial (relative) SS value */
63123474Swpaul	uint16_t	idh_sp;		/* Initial SP value */
64123474Swpaul	uint16_t	idh_csum;	/* Checksum */
65123474Swpaul	uint16_t	idh_ip;		/* Initial IP value */
66123474Swpaul	uint16_t	idh_cs;		/* Initial (relative) CS value */
67123474Swpaul	uint16_t	idh_lfarlc;	/* File address of relocation table */
68123474Swpaul	uint16_t	idh_ovno;	/* Overlay number */
69123474Swpaul	uint16_t	idh_rsvd1[4];	/* Reserved words */
70123474Swpaul	uint16_t	idh_oemid;	/* OEM identifier (for idh_oeminfo) */
71123474Swpaul	uint16_t	idh_oeminfo;	/* OEM information; oemid specific */
72123474Swpaul	uint16_t	idh_rsvd2[10];	/* Reserved words */
73123474Swpaul	uint32_t	idh_lfanew;	/* File address of new exe header */
74123474Swpaul};
75123474Swpaul
76123474Swpaultypedef struct image_dos_header image_dos_header;
77123474Swpaul
78123474Swpaul/*
79123474Swpaul * File header format.
80123474Swpaul */
81123474Swpaul
82123474Swpaulstruct image_file_header {
83123474Swpaul	uint16_t	ifh_machine;		/* Machine type */
84123474Swpaul	uint16_t	ifh_numsections;	/* # of sections */
85123474Swpaul	uint32_t	ifh_timestamp;		/* Date/time stamp */
86123474Swpaul	uint32_t	ifh_symtblptr;		/* Offset to symbol table */
87123474Swpaul	uint32_t	ifh_numsyms; 		/* # of symbols */
88123474Swpaul	uint16_t	ifh_optionalhdrlen;	/* Size of optional header */
89123474Swpaul	uint16_t	ifh_characteristics;	/* Characteristics */
90123474Swpaul};
91123474Swpaul
92123474Swpaultypedef struct image_file_header image_file_header;
93123474Swpaul
94123474Swpaul/* Machine types */
95123474Swpaul
96123474Swpaul#define IMAGE_FILE_MACHINE_UNKNOWN      0
97123474Swpaul#define IMAGE_FILE_MACHINE_I860         0x014d
98123474Swpaul#define IMAGE_FILE_MACHINE_I386         0x014c
99123474Swpaul#define IMAGE_FILE_MACHINE_R3000        0x0162
100123474Swpaul#define IMAGE_FILE_MACHINE_R4000        0x0166
101123474Swpaul#define IMAGE_FILE_MACHINE_R10000       0x0168
102123474Swpaul#define IMAGE_FILE_MACHINE_WCEMIPSV2    0x0169
103123474Swpaul#define IMAGE_FILE_MACHINE_ALPHA        0x0184
104123474Swpaul#define IMAGE_FILE_MACHINE_SH3          0x01a2
105123474Swpaul#define IMAGE_FILE_MACHINE_SH3DSP       0x01a3
106123474Swpaul#define IMAGE_FILE_MACHINE_SH3E         0x01a4
107123474Swpaul#define IMAGE_FILE_MACHINE_SH4          0x01a6
108123474Swpaul#define IMAGE_FILE_MACHINE_SH5          0x01a8
109123474Swpaul#define IMAGE_FILE_MACHINE_ARM          0x01c0
110123474Swpaul#define IMAGE_FILE_MACHINE_THUMB        0x01c2
111123474Swpaul#define IMAGE_FILE_MACHINE_AM33         0x01d3
112123474Swpaul#define IMAGE_FILE_MACHINE_POWERPC      0x01f0
113123474Swpaul#define IMAGE_FILE_MACHINE_POWERPCFP    0x01f1
114123474Swpaul#define IMAGE_FILE_MACHINE_IA64         0x0200
115123474Swpaul#define IMAGE_FILE_MACHINE_MIPS16       0x0266
116123474Swpaul#define IMAGE_FILE_MACHINE_ALPHA64      0x0284
117123474Swpaul#define IMAGE_FILE_MACHINE_MIPSFPU      0x0366
118123474Swpaul#define IMAGE_FILE_MACHINE_MIPSFPU16    0x0466
119123474Swpaul#define IMAGE_FILE_MACHINE_AXP64        IMAGE_FILE_MACHINE_ALPHA64
120123474Swpaul#define IMAGE_FILE_MACHINE_TRICORE      0x0520
121123474Swpaul#define IMAGE_FILE_MACHINE_CEF          0x0cef
122123474Swpaul#define IMAGE_FILE_MACHINE_EBC          0x0ebc
123123474Swpaul#define IMAGE_FILE_MACHINE_AMD64        0x8664
124123474Swpaul#define IMAGE_FILE_MACHINE_M32R         0x9041
125123474Swpaul#define IMAGE_FILE_MACHINE_CEE          0xc0ee
126123474Swpaul
127123474Swpaul/* Characteristics */
128123474Swpaul
129123474Swpaul#define IMAGE_FILE_RELOCS_STRIPPED      0x0001 /* No relocation info */
130123474Swpaul#define IMAGE_FILE_EXECUTABLE_IMAGE     0x0002
131123474Swpaul#define IMAGE_FILE_LINE_NUMS_STRIPPED   0x0004
132123474Swpaul#define IMAGE_FILE_LOCAL_SYMS_STRIPPED  0x0008
133123474Swpaul#define IMAGE_FILE_AGGRESIVE_WS_TRIM    0x0010
134123474Swpaul#define IMAGE_FILE_LARGE_ADDRESS_AWARE  0x0020
135123474Swpaul#define IMAGE_FILE_16BIT_MACHINE        0x0040
136123474Swpaul#define IMAGE_FILE_BYTES_REVERSED_LO    0x0080
137123474Swpaul#define IMAGE_FILE_32BIT_MACHINE        0x0100
138123474Swpaul#define IMAGE_FILE_DEBUG_STRIPPED       0x0200
139123474Swpaul#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP      0x0400
140123474Swpaul#define IMAGE_FILE_NET_RUN_FROM_SWAP    0x0800
141123474Swpaul#define IMAGE_FILE_SYSTEM               0x1000
142123474Swpaul#define IMAGE_FILE_DLL                  0x2000
143123474Swpaul#define IMAGE_FILE_UP_SYSTEM_ONLY       0x4000
144123474Swpaul#define IMAGE_FILE_BYTES_REVERSED_HI    0x8000
145123474Swpaul
146123474Swpaul#define IMAGE_SIZEOF_FILE_HEADER             20
147123474Swpaul
148123474Swpaul/*
149123474Swpaul * Directory format.
150123474Swpaul */
151123474Swpaul
152123474Swpaulstruct image_data_directory {
153123474Swpaul	uint32_t		idd_vaddr;	/* virtual address */
154123474Swpaul	uint32_t		idd_size;	/* size */
155123474Swpaul};
156123474Swpaul
157123474Swpaultypedef struct image_data_directory image_data_directory;
158123474Swpaul
159123474Swpaul#define IMAGE_DIRECTORY_ENTRIES_MAX    16
160123474Swpaul
161123474Swpaul/*
162123474Swpaul * Optional header format.
163123474Swpaul */
164123474Swpaul
165123474Swpaulstruct image_optional_header {
166123474Swpaul
167123474Swpaul	/* Standard fields */
168123474Swpaul
169123474Swpaul	uint16_t	ioh_magic;
170123474Swpaul	uint8_t		ioh_linkerver_major;
171123474Swpaul	uint8_t		ioh_linkerver_minor;
172123474Swpaul	uint32_t	ioh_codesize;
173123474Swpaul	uint32_t	ioh_datasize;
174123474Swpaul	uint32_t	ioh_bsssize;
175123474Swpaul	uint32_t	ioh_entryaddr;
176123474Swpaul	uint32_t	ioh_codebaseaddr;
177123474Swpaul	uint32_t	ioh_databaseaddr;
178123474Swpaul
179123474Swpaul	/* NT-specific fields */
180123474Swpaul
181123474Swpaul	uint32_t	ioh_imagebase;
182123474Swpaul	uint32_t	ioh_sectalign;
183123474Swpaul	uint32_t	ioh_filealign;
184123474Swpaul	uint16_t	ioh_osver_major;
185123474Swpaul	uint16_t	ioh_osver_minor;
186123474Swpaul	uint16_t	ioh_imagever_major;
187123474Swpaul	uint16_t	ioh_imagever_minor;
188123474Swpaul	uint16_t	ioh_subsys_major;
189123474Swpaul	uint16_t	ioh_subsys_minor;
190123474Swpaul	uint32_t	ioh_win32ver;
191123474Swpaul	uint32_t	ioh_imagesize;
192123474Swpaul	uint32_t	ioh_headersize;
193123474Swpaul	uint32_t	ioh_csum;
194123474Swpaul	uint16_t	ioh_subsys;
195123474Swpaul	uint16_t	ioh_dll_characteristics;
196123474Swpaul	uint32_t	ioh_stackreservesize;
197123474Swpaul	uint32_t	ioh_stackcommitsize;
198123474Swpaul	uint32_t	ioh_heapreservesize;
199123474Swpaul	uint32_t	ioh_heapcommitsize;
200123474Swpaul	uint16_t	ioh_loaderflags;
201123474Swpaul	uint32_t	ioh_rva_size_cnt;
202123474Swpaul	image_data_directory	ioh_datadir[IMAGE_DIRECTORY_ENTRIES_MAX];
203123474Swpaul};
204123474Swpaul
205123474Swpaultypedef struct image_optional_header image_optional_header;
206123474Swpaul
207123474Swpaulstruct image_nt_header {
208123474Swpaul	uint32_t		inh_signature;
209123474Swpaul	image_file_header	inh_filehdr;
210123474Swpaul	image_optional_header	inh_optionalhdr;
211123474Swpaul};
212123474Swpaul
213123474Swpaultypedef struct image_nt_header image_nt_header;
214123474Swpaul
215123474Swpaul/* Directory Entries */
216123474Swpaul
217123474Swpaul#define IMAGE_DIRECTORY_ENTRY_EXPORT         0   /* Export Directory */
218123474Swpaul#define IMAGE_DIRECTORY_ENTRY_IMPORT         1   /* Import Directory */
219123474Swpaul#define IMAGE_DIRECTORY_ENTRY_RESOURCE       2   /* Resource Directory */
220123474Swpaul#define IMAGE_DIRECTORY_ENTRY_EXCEPTION      3   /* Exception Directory */
221123474Swpaul#define IMAGE_DIRECTORY_ENTRY_SECURITY       4   /* Security Directory */
222123474Swpaul#define IMAGE_DIRECTORY_ENTRY_BASERELOC      5   /* Base Relocation Table */
223123474Swpaul#define IMAGE_DIRECTORY_ENTRY_DEBUG          6   /* Debug Directory */
224123474Swpaul#define IMAGE_DIRECTORY_ENTRY_COPYRIGHT      7   /* Description String */
225123474Swpaul#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR      8   /* Machine Value (MIPS GP) */
226123474Swpaul#define IMAGE_DIRECTORY_ENTRY_TLS            9   /* TLS Directory */
227123474Swpaul#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG   10   /* Load Configuration Directory */
228123474Swpaul#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT  11   /* Bound Import Directory in headers */
229123474Swpaul#define IMAGE_DIRECTORY_ENTRY_IAT           12   /* Import Address Table */
230123474Swpaul#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT      13
231123474Swpaul#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR    14
232123474Swpaul
233124165Swpaul/* Resource types */
234124165Swpaul
235124165Swpaul#define RT_CURSOR	1
236124165Swpaul#define RT_BITMAP	2
237124165Swpaul#define RT_ICON		3
238124165Swpaul#define RT_MENU		4
239124165Swpaul#define RT_DIALOG	5
240124165Swpaul#define RT_STRING	6
241124165Swpaul#define RT_FONTDIR	7
242124165Swpaul#define RT_FONT		8
243124165Swpaul#define RT_ACCELERATOR	9
244124165Swpaul#define RT_RCDATA	10
245124165Swpaul#define RT_MESSAGETABLE	11
246124165Swpaul#define RT_GROUP_CURSOR	12
247124165Swpaul#define RT_GROUP_ICON	14
248124165Swpaul#define RT_VERSION	16
249124165Swpaul#define RT_DLGINCLUDE	17
250124165Swpaul#define RT_PLUGPLAY	19
251124165Swpaul#define RT_VXD		20
252124165Swpaul#define RT_ANICURSOR	21
253124165Swpaul#define RT_ANIICON	22
254124165Swpaul#define RT_HTML		23
255124165Swpaul
256123474Swpaul/*
257123474Swpaul * Section header format.
258123474Swpaul */
259123474Swpaul
260123474Swpaul#define IMAGE_SHORT_NAME_LEN			8
261123474Swpaul
262123474Swpaulstruct image_section_header {
263123474Swpaul	uint8_t		ish_name[IMAGE_SHORT_NAME_LEN];
264123474Swpaul	union {
265123474Swpaul		uint32_t	ish_paddr;
266123474Swpaul		uint32_t	ish_vsize;
267123474Swpaul	} ish_misc;
268123474Swpaul	uint32_t	ish_vaddr;
269123474Swpaul	uint32_t	ish_rawdatasize;
270123474Swpaul	uint32_t	ish_rawdataaddr;
271123474Swpaul	uint32_t	ish_relocaddr;
272123474Swpaul	uint32_t	ish_linenumaddr;
273123474Swpaul	uint16_t	ish_numrelocs;
274123474Swpaul	uint16_t	ish_numlinenums;
275123474Swpaul	uint32_t	ish_characteristics;
276123474Swpaul};
277123474Swpaul
278123474Swpaultypedef struct image_section_header image_section_header;
279123474Swpaul
280123474Swpaul#define IMAGE_SIZEOF_SECTION_HEADER          40
281123474Swpaul
282123474Swpaul/*
283123474Swpaul * Import format
284123474Swpaul */
285123474Swpaul
286123474Swpaulstruct image_import_by_name {
287123474Swpaul	uint16_t	iibn_hint;
288123474Swpaul	u_int8_t	iibn_name[1];
289123474Swpaul};
290123474Swpaul
291123474Swpaul#define IMAGE_ORDINAL_FLAG 0x80000000
292123474Swpaul#define IMAGE_ORDINAL(Ordinal) (Ordinal & 0xffff)
293123474Swpaul
294123474Swpaulstruct image_import_descriptor {
295123474Swpaul	uint32_t	iid_import_name_table_addr;
296123474Swpaul	uint32_t	iid_timestamp;
297123474Swpaul	uint32_t	iid_forwardchain;
298123474Swpaul	uint32_t	iid_nameaddr;
299123474Swpaul	uint32_t	iid_import_address_table_addr;
300123474Swpaul};
301123474Swpaul
302123474Swpaultypedef struct image_import_descriptor image_import_descriptor;
303123474Swpaul
304123474Swpaulstruct image_base_reloc {
305123474Swpaul	uint32_t	ibr_vaddr;
306123474Swpaul	uint32_t	ibr_blocksize;
307123474Swpaul	uint16_t	ibr_rel[1];
308123474Swpaul};
309123474Swpaul
310123474Swpaultypedef struct image_base_reloc image_base_reloc;
311123474Swpaul
312123474Swpaul#define IMR_RELTYPE(x)		((x >> 12) & 0xF)
313123474Swpaul#define IMR_RELOFFSET(x)	(x & 0xFFF)
314123474Swpaul
315123474Swpaul/* generic relocation types */
316123474Swpaul#define IMAGE_REL_BASED_ABSOLUTE                0
317123474Swpaul#define IMAGE_REL_BASED_HIGH                    1
318123474Swpaul#define IMAGE_REL_BASED_LOW                     2
319123474Swpaul#define IMAGE_REL_BASED_HIGHLOW                 3
320123474Swpaul#define IMAGE_REL_BASED_HIGHADJ                 4
321123474Swpaul#define IMAGE_REL_BASED_MIPS_JMPADDR            5
322123474Swpaul#define IMAGE_REL_BASED_SECTION                 6
323123474Swpaul#define IMAGE_REL_BASED_REL                     7
324123474Swpaul#define IMAGE_REL_BASED_MIPS_JMPADDR16          9
325123474Swpaul#define IMAGE_REL_BASED_IA64_IMM64              9 /* yes, 9 too */
326123474Swpaul#define IMAGE_REL_BASED_DIR64                   10
327123474Swpaul#define IMAGE_REL_BASED_HIGH3ADJ                11
328123474Swpaul
329124165Swpaulstruct image_resource_directory_entry {
330124165Swpaul	uint32_t		irde_name;
331124165Swpaul	uint32_t		irde_dataoff;
332124165Swpaul};
333123474Swpaul
334124165Swpaultypedef struct image_resource_directory_entry image_resource_directory_entry;
335124165Swpaul
336124165Swpaul#define RESOURCE_NAME_STR	0x80000000
337124165Swpaul#define RESOURCE_DIR_FLAG	0x80000000
338124165Swpaul
339124165Swpaulstruct image_resource_directory {
340124165Swpaul	uint32_t		ird_characteristics;
341124165Swpaul	uint32_t		ird_timestamp;
342124165Swpaul	uint16_t		ird_majorver;
343124165Swpaul	uint16_t		ird_minorver;
344124165Swpaul	uint16_t		ird_named_entries;
345124165Swpaul	uint16_t		ird_id_entries;
346124165Swpaul#ifdef notdef
347124165Swpaul	image_resource_directory_entry	ird_entries[1];
348124165Swpaul#endif
349124165Swpaul};
350124165Swpaul
351124165Swpaultypedef struct image_resource_directory image_resource_directory;
352124165Swpaul
353124165Swpaulstruct image_resource_directory_string {
354124165Swpaul	uint16_t		irds_len;
355124165Swpaul	char			irds_name[1];
356124165Swpaul};
357124165Swpaul
358124165Swpaultypedef struct image_resource_directory_string image_resource_directory_string;
359124165Swpaul
360124165Swpaulstruct image_resource_directory_string_u {
361124165Swpaul	uint16_t		irds_len;
362124165Swpaul	char			irds_name[1];
363124165Swpaul};
364124165Swpaul
365124165Swpaultypedef struct image_resource_directory_string_u
366124165Swpaul	image_resource_directory_string_u;
367124165Swpaul
368124165Swpaulstruct image_resource_data_entry {
369124165Swpaul	uint32_t		irde_offset;
370124165Swpaul	uint32_t		irde_size;
371124165Swpaul	uint32_t		irde_codepage;
372124165Swpaul	uint32_t		irde_rsvd;
373124165Swpaul};
374124165Swpaul
375124165Swpaultypedef struct image_resource_data_entry image_resource_data_entry;
376124165Swpaul
377124165Swpaulstruct message_resource_data {
378124165Swpaul	uint32_t		mrd_numblocks;
379124165Swpaul#ifdef notdef
380124165Swpaul	message_resource_block	mrd_blocks[1];
381124165Swpaul#endif
382124165Swpaul};
383124165Swpaul
384124165Swpaultypedef struct message_resource_data message_resource_data;
385124165Swpaul
386124165Swpaulstruct message_resource_block {
387124165Swpaul	uint32_t		mrb_lowid;
388124165Swpaul	uint32_t		mrb_highid;
389124165Swpaul	uint32_t		mrb_entryoff;
390124165Swpaul};
391124165Swpaul
392124165Swpaultypedef struct message_resource_block message_resource_block;
393124165Swpaul
394124165Swpaulstruct message_resource_entry {
395124165Swpaul	uint16_t		mre_len;
396124165Swpaul	uint16_t		mre_flags;
397124165Swpaul	char			mre_text[];
398124165Swpaul};
399124165Swpaul
400124165Swpaultypedef struct message_resource_entry message_resource_entry;
401124165Swpaul
402124165Swpaul#define MESSAGE_RESOURCE_UNICODE	0x0001
403124165Swpaul
404123474Swpaulstruct image_patch_table {
405123474Swpaul	char		*ipt_name;
406123474Swpaul	void		(*ipt_func)(void);
407123474Swpaul};
408123474Swpaul
409123474Swpaultypedef struct image_patch_table image_patch_table;
410123474Swpaul
411124504Sobrien/*
412124504Sobrien * Note: Windows uses the _stdcall calling convention. This means
413124504Sobrien * that the callback functions provided in the function table must
414124504Sobrien * be declared using __attribute__((__stdcall__)), otherwise the
415124504Sobrien * Windows code will likely screw up the %esp register and cause
416124504Sobrien * us to jump to an invalid address when it returns.
417124504Sobrien */
418124504Sobrien
419124504Sobrien#ifdef __amd64__
420124574Sobrien#define	__stdcall
421132973Swpaul#define __regcall
422132973Swpaul#define __fastcall
423132973Swpaul#define REGARGS1(decl1)		decl1
424132973Swpaul#define REGARGS2(decl1, decl2)	decl1, decl2
425132973Swpaul#define REGCALL1(arg1)		arg1
426132973Swpaul#define REGCALL2(arg1, arg2)	arg1, arg2
427124504Sobrien#else
428124574Sobrien#define	__stdcall __attribute__((__stdcall__))
429132973Swpaul#define __regcall __attribute__((__regparm__(3)))
430132973Swpaul#define __fastcall __stdcall __regcall
431132973Swpaul#define REGARGS1(decl1)		int dummy1, int dummy2, decl1
432132973Swpaul#define REGARGS2(decl1, decl2)	int dummy1, decl2, decl1
433132973Swpaul#define REGCALL1(arg1)		0, 0, arg1
434132973Swpaul#define REGCALL2(arg1, arg2)	0, arg2, arg1
435124504Sobrien#endif
436124504Sobrien
437128229Swpaul
438128229Swpaul/*
439128229Swpaul * This mess allows us to call a _fastcall style routine with our
440128229Swpaul * version of gcc, which lacks __attribute__((__fastcall__)). Only
441128229Swpaul * has meaning on x86; everywhere else, it's a no-op.
442128229Swpaul */
443128229Swpaul
444128229Swpaul#ifdef __i386__
445132973Swpaultypedef __fastcall int (*fcall1)(REGARGS1(uint32_t));
446132973Swpaultypedef __fastcall int (*fcall2)(REGARGS2(uint32_t, uint32_t));
447132973Swpaultypedef __fastcall int (*fcall3)(REGARGS2(uint32_t, uint32_t), uint32_t);
448132973Swpaul
449128229Swpaulstatic __inline uint32_t
450132973Swpaulfastcall1(fcall1 f, uint32_t a)
451128229Swpaul{
452132973Swpaul	return(f(REGCALL1(a)));
453128229Swpaul}
454128229Swpaul
455128229Swpaulstatic __inline uint32_t
456132973Swpaulfastcall2(fcall2 f, uint32_t a, uint32_t b)
457128229Swpaul{
458132973Swpaul	return(f(REGCALL2(a, b)));
459128229Swpaul}
460128229Swpaul
461128229Swpaulstatic __inline uint32_t
462132973Swpaulfastcall3(fcall3 f, uint32_t a, uint32_t b, uint32_t c)
463128229Swpaul{
464132973Swpaul	return(f(REGCALL2(a, b), c));
465128229Swpaul}
466128229Swpaul
467128229Swpaul#define FASTCALL1(f, a)		\
468132973Swpaul	fastcall1((fcall1)(f), (uint32_t)(a))
469128229Swpaul#define FASTCALL2(f, a, b)	\
470132973Swpaul	fastcall2((fcall2)(f), (uint32_t)(a), (uint32_t)(b))
471128229Swpaul#define FASTCALL3(f, a, b, c)	\
472132973Swpaul	fastcall3((fcall3)(f), (uint32_t)(a), (uint32_t)(b), (uint32_t)(c))
473128229Swpaul#else
474128229Swpaul#define FASTCALL1(f, a) (f)((a))
475128229Swpaul#define FASTCALL2(f, a, b) (f)((a), (b))
476128229Swpaul#define FASTCALL3(f, a, b, c) (f)((a), (b), (c))
477128229Swpaul#endif /* __i386__ */
478128229Swpaul
479123474Swpaul__BEGIN_DECLS
480123474Swpaulextern int pe_get_dos_header(vm_offset_t, image_dos_header *);
481123474Swpaulextern int pe_is_nt_image(vm_offset_t);
482123474Swpaulextern int pe_get_optional_header(vm_offset_t, image_optional_header *);
483123474Swpaulextern int pe_get_file_header(vm_offset_t, image_file_header *);
484123474Swpaulextern int pe_get_section_header(vm_offset_t, image_section_header *);
485123474Swpaulextern int pe_numsections(vm_offset_t);
486123474Swpaulextern vm_offset_t pe_imagebase(vm_offset_t);
487123474Swpaulextern vm_offset_t pe_directory_offset(vm_offset_t, uint32_t);
488123474Swpaulextern vm_offset_t pe_translate_addr (vm_offset_t, uint32_t);
489123474Swpaulextern int pe_get_section(vm_offset_t, image_section_header *, const char *);
490123474Swpaulextern int pe_relocate(vm_offset_t);
491123474Swpaulextern int pe_get_import_descriptor(vm_offset_t, image_import_descriptor *, char *);
492123474Swpaulextern int pe_patch_imports(vm_offset_t, char *, image_patch_table *);
493124173Swpaulextern int pe_get_messagetable(vm_offset_t, message_resource_data **);
494124173Swpaulextern int pe_get_message(vm_offset_t, uint32_t, char **, int *, uint16_t *);
495123474Swpaul__END_DECLS
496123474Swpaul
497123474Swpaul#endif /* _PE_VAR_H_ */
498