pe_var.h revision 142037
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 142037 2005-02-18 04:33:34Z wpaul $
33123474Swpaul */
34123474Swpaul
35123474Swpaul#ifndef _PE_VAR_H_
36123474Swpaul#define _PE_VAR_H_
37123474Swpaul
38123474Swpaul/*
39123474Swpaul *  Image Format
40123474Swpaul */
41123474Swpaul
42141963Swpaul#define IMAGE_DOS_SIGNATURE			0x5A4D      /* MZ */
43141963Swpaul#define IMAGE_OS2_SIGNATURE			0x454E      /* NE */
44141963Swpaul#define IMAGE_OS2_SIGNATURE_LE			0x454C      /* LE */
45141963Swpaul#define IMAGE_VXD_SIGNATURE			0x454C      /* LE */
46141963Swpaul#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;
177141963Swpaul#ifndef __amd64__
178123474Swpaul	uint32_t	ioh_databaseaddr;
179141963Swpaul#endif
180123474Swpaul
181123474Swpaul	/* NT-specific fields */
182123474Swpaul
183141963Swpaul	uintptr_t	ioh_imagebase;
184123474Swpaul	uint32_t	ioh_sectalign;
185123474Swpaul	uint32_t	ioh_filealign;
186123474Swpaul	uint16_t	ioh_osver_major;
187123474Swpaul	uint16_t	ioh_osver_minor;
188123474Swpaul	uint16_t	ioh_imagever_major;
189123474Swpaul	uint16_t	ioh_imagever_minor;
190123474Swpaul	uint16_t	ioh_subsys_major;
191123474Swpaul	uint16_t	ioh_subsys_minor;
192123474Swpaul	uint32_t	ioh_win32ver;
193123474Swpaul	uint32_t	ioh_imagesize;
194123474Swpaul	uint32_t	ioh_headersize;
195123474Swpaul	uint32_t	ioh_csum;
196123474Swpaul	uint16_t	ioh_subsys;
197123474Swpaul	uint16_t	ioh_dll_characteristics;
198141963Swpaul	uintptr_t	ioh_stackreservesize;
199141963Swpaul	uintptr_t	ioh_stackcommitsize;
200141963Swpaul	uintptr_t	ioh_heapreservesize;
201141963Swpaul	uintptr_t	ioh_heapcommitsize;
202123474Swpaul	uint16_t	ioh_loaderflags;
203123474Swpaul	uint32_t	ioh_rva_size_cnt;
204123474Swpaul	image_data_directory	ioh_datadir[IMAGE_DIRECTORY_ENTRIES_MAX];
205123474Swpaul};
206123474Swpaul
207123474Swpaultypedef struct image_optional_header image_optional_header;
208123474Swpaul
209123474Swpaulstruct image_nt_header {
210123474Swpaul	uint32_t		inh_signature;
211123474Swpaul	image_file_header	inh_filehdr;
212123474Swpaul	image_optional_header	inh_optionalhdr;
213123474Swpaul};
214123474Swpaul
215123474Swpaultypedef struct image_nt_header image_nt_header;
216123474Swpaul
217123474Swpaul/* Directory Entries */
218123474Swpaul
219123474Swpaul#define IMAGE_DIRECTORY_ENTRY_EXPORT         0   /* Export Directory */
220123474Swpaul#define IMAGE_DIRECTORY_ENTRY_IMPORT         1   /* Import Directory */
221123474Swpaul#define IMAGE_DIRECTORY_ENTRY_RESOURCE       2   /* Resource Directory */
222123474Swpaul#define IMAGE_DIRECTORY_ENTRY_EXCEPTION      3   /* Exception Directory */
223123474Swpaul#define IMAGE_DIRECTORY_ENTRY_SECURITY       4   /* Security Directory */
224123474Swpaul#define IMAGE_DIRECTORY_ENTRY_BASERELOC      5   /* Base Relocation Table */
225123474Swpaul#define IMAGE_DIRECTORY_ENTRY_DEBUG          6   /* Debug Directory */
226123474Swpaul#define IMAGE_DIRECTORY_ENTRY_COPYRIGHT      7   /* Description String */
227123474Swpaul#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR      8   /* Machine Value (MIPS GP) */
228123474Swpaul#define IMAGE_DIRECTORY_ENTRY_TLS            9   /* TLS Directory */
229123474Swpaul#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG   10   /* Load Configuration Directory */
230123474Swpaul#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT  11   /* Bound Import Directory in headers */
231123474Swpaul#define IMAGE_DIRECTORY_ENTRY_IAT           12   /* Import Address Table */
232123474Swpaul#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT      13
233123474Swpaul#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR    14
234123474Swpaul
235124165Swpaul/* Resource types */
236124165Swpaul
237124165Swpaul#define RT_CURSOR	1
238124165Swpaul#define RT_BITMAP	2
239124165Swpaul#define RT_ICON		3
240124165Swpaul#define RT_MENU		4
241124165Swpaul#define RT_DIALOG	5
242124165Swpaul#define RT_STRING	6
243124165Swpaul#define RT_FONTDIR	7
244124165Swpaul#define RT_FONT		8
245124165Swpaul#define RT_ACCELERATOR	9
246124165Swpaul#define RT_RCDATA	10
247124165Swpaul#define RT_MESSAGETABLE	11
248124165Swpaul#define RT_GROUP_CURSOR	12
249124165Swpaul#define RT_GROUP_ICON	14
250124165Swpaul#define RT_VERSION	16
251124165Swpaul#define RT_DLGINCLUDE	17
252124165Swpaul#define RT_PLUGPLAY	19
253124165Swpaul#define RT_VXD		20
254124165Swpaul#define RT_ANICURSOR	21
255124165Swpaul#define RT_ANIICON	22
256124165Swpaul#define RT_HTML		23
257124165Swpaul
258123474Swpaul/*
259123474Swpaul * Section header format.
260123474Swpaul */
261123474Swpaul
262123474Swpaul#define IMAGE_SHORT_NAME_LEN			8
263123474Swpaul
264123474Swpaulstruct image_section_header {
265123474Swpaul	uint8_t		ish_name[IMAGE_SHORT_NAME_LEN];
266123474Swpaul	union {
267123474Swpaul		uint32_t	ish_paddr;
268123474Swpaul		uint32_t	ish_vsize;
269123474Swpaul	} ish_misc;
270123474Swpaul	uint32_t	ish_vaddr;
271123474Swpaul	uint32_t	ish_rawdatasize;
272123474Swpaul	uint32_t	ish_rawdataaddr;
273123474Swpaul	uint32_t	ish_relocaddr;
274123474Swpaul	uint32_t	ish_linenumaddr;
275123474Swpaul	uint16_t	ish_numrelocs;
276123474Swpaul	uint16_t	ish_numlinenums;
277123474Swpaul	uint32_t	ish_characteristics;
278123474Swpaul};
279123474Swpaul
280123474Swpaultypedef struct image_section_header image_section_header;
281123474Swpaul
282123474Swpaul#define IMAGE_SIZEOF_SECTION_HEADER          40
283123474Swpaul
284123474Swpaul/*
285123474Swpaul * Import format
286123474Swpaul */
287123474Swpaul
288123474Swpaulstruct image_import_by_name {
289123474Swpaul	uint16_t	iibn_hint;
290142037Swpaul	uint8_t		iibn_name[1];
291123474Swpaul};
292123474Swpaul
293123474Swpaul#define IMAGE_ORDINAL_FLAG 0x80000000
294123474Swpaul#define IMAGE_ORDINAL(Ordinal) (Ordinal & 0xffff)
295123474Swpaul
296123474Swpaulstruct image_import_descriptor {
297123474Swpaul	uint32_t	iid_import_name_table_addr;
298123474Swpaul	uint32_t	iid_timestamp;
299123474Swpaul	uint32_t	iid_forwardchain;
300123474Swpaul	uint32_t	iid_nameaddr;
301123474Swpaul	uint32_t	iid_import_address_table_addr;
302123474Swpaul};
303123474Swpaul
304123474Swpaultypedef struct image_import_descriptor image_import_descriptor;
305123474Swpaul
306123474Swpaulstruct image_base_reloc {
307123474Swpaul	uint32_t	ibr_vaddr;
308123474Swpaul	uint32_t	ibr_blocksize;
309123474Swpaul	uint16_t	ibr_rel[1];
310123474Swpaul};
311123474Swpaul
312123474Swpaultypedef struct image_base_reloc image_base_reloc;
313123474Swpaul
314123474Swpaul#define IMR_RELTYPE(x)		((x >> 12) & 0xF)
315123474Swpaul#define IMR_RELOFFSET(x)	(x & 0xFFF)
316123474Swpaul
317123474Swpaul/* generic relocation types */
318123474Swpaul#define IMAGE_REL_BASED_ABSOLUTE                0
319123474Swpaul#define IMAGE_REL_BASED_HIGH                    1
320123474Swpaul#define IMAGE_REL_BASED_LOW                     2
321123474Swpaul#define IMAGE_REL_BASED_HIGHLOW                 3
322123474Swpaul#define IMAGE_REL_BASED_HIGHADJ                 4
323123474Swpaul#define IMAGE_REL_BASED_MIPS_JMPADDR            5
324123474Swpaul#define IMAGE_REL_BASED_SECTION                 6
325123474Swpaul#define IMAGE_REL_BASED_REL                     7
326123474Swpaul#define IMAGE_REL_BASED_MIPS_JMPADDR16          9
327123474Swpaul#define IMAGE_REL_BASED_IA64_IMM64              9 /* yes, 9 too */
328123474Swpaul#define IMAGE_REL_BASED_DIR64                   10
329123474Swpaul#define IMAGE_REL_BASED_HIGH3ADJ                11
330123474Swpaul
331124165Swpaulstruct image_resource_directory_entry {
332124165Swpaul	uint32_t		irde_name;
333124165Swpaul	uint32_t		irde_dataoff;
334124165Swpaul};
335123474Swpaul
336124165Swpaultypedef struct image_resource_directory_entry image_resource_directory_entry;
337124165Swpaul
338124165Swpaul#define RESOURCE_NAME_STR	0x80000000
339124165Swpaul#define RESOURCE_DIR_FLAG	0x80000000
340124165Swpaul
341124165Swpaulstruct image_resource_directory {
342124165Swpaul	uint32_t		ird_characteristics;
343124165Swpaul	uint32_t		ird_timestamp;
344124165Swpaul	uint16_t		ird_majorver;
345124165Swpaul	uint16_t		ird_minorver;
346124165Swpaul	uint16_t		ird_named_entries;
347124165Swpaul	uint16_t		ird_id_entries;
348124165Swpaul#ifdef notdef
349124165Swpaul	image_resource_directory_entry	ird_entries[1];
350124165Swpaul#endif
351124165Swpaul};
352124165Swpaul
353124165Swpaultypedef struct image_resource_directory image_resource_directory;
354124165Swpaul
355124165Swpaulstruct image_resource_directory_string {
356124165Swpaul	uint16_t		irds_len;
357124165Swpaul	char			irds_name[1];
358124165Swpaul};
359124165Swpaul
360124165Swpaultypedef struct image_resource_directory_string image_resource_directory_string;
361124165Swpaul
362124165Swpaulstruct image_resource_directory_string_u {
363124165Swpaul	uint16_t		irds_len;
364124165Swpaul	char			irds_name[1];
365124165Swpaul};
366124165Swpaul
367124165Swpaultypedef struct image_resource_directory_string_u
368124165Swpaul	image_resource_directory_string_u;
369124165Swpaul
370124165Swpaulstruct image_resource_data_entry {
371124165Swpaul	uint32_t		irde_offset;
372124165Swpaul	uint32_t		irde_size;
373124165Swpaul	uint32_t		irde_codepage;
374124165Swpaul	uint32_t		irde_rsvd;
375124165Swpaul};
376124165Swpaul
377124165Swpaultypedef struct image_resource_data_entry image_resource_data_entry;
378124165Swpaul
379124165Swpaulstruct message_resource_data {
380124165Swpaul	uint32_t		mrd_numblocks;
381124165Swpaul#ifdef notdef
382124165Swpaul	message_resource_block	mrd_blocks[1];
383124165Swpaul#endif
384124165Swpaul};
385124165Swpaul
386124165Swpaultypedef struct message_resource_data message_resource_data;
387124165Swpaul
388124165Swpaulstruct message_resource_block {
389124165Swpaul	uint32_t		mrb_lowid;
390124165Swpaul	uint32_t		mrb_highid;
391124165Swpaul	uint32_t		mrb_entryoff;
392124165Swpaul};
393124165Swpaul
394124165Swpaultypedef struct message_resource_block message_resource_block;
395124165Swpaul
396124165Swpaulstruct message_resource_entry {
397124165Swpaul	uint16_t		mre_len;
398124165Swpaul	uint16_t		mre_flags;
399124165Swpaul	char			mre_text[];
400124165Swpaul};
401124165Swpaul
402124165Swpaultypedef struct message_resource_entry message_resource_entry;
403124165Swpaul
404124165Swpaul#define MESSAGE_RESOURCE_UNICODE	0x0001
405124165Swpaul
406123474Swpaulstruct image_patch_table {
407123474Swpaul	char		*ipt_name;
408123474Swpaul	void		(*ipt_func)(void);
409141963Swpaul	void		(*ipt_wrap)(void);
410123474Swpaul};
411123474Swpaul
412123474Swpaultypedef struct image_patch_table image_patch_table;
413123474Swpaul
414124504Sobrien/*
415124504Sobrien * Note: Windows uses the _stdcall calling convention. This means
416124504Sobrien * that the callback functions provided in the function table must
417124504Sobrien * be declared using __attribute__((__stdcall__)), otherwise the
418124504Sobrien * Windows code will likely screw up the %esp register and cause
419124504Sobrien * us to jump to an invalid address when it returns.
420124504Sobrien */
421124504Sobrien
422124504Sobrien#ifdef __amd64__
423124574Sobrien#define	__stdcall
424132973Swpaul#define __regcall
425132973Swpaul#define __fastcall
426132973Swpaul#define REGARGS1(decl1)		decl1
427132973Swpaul#define REGARGS2(decl1, decl2)	decl1, decl2
428132973Swpaul#define REGCALL1(arg1)		arg1
429132973Swpaul#define REGCALL2(arg1, arg2)	arg1, arg2
430124504Sobrien#else
431124574Sobrien#define	__stdcall __attribute__((__stdcall__))
432132973Swpaul#define __regcall __attribute__((__regparm__(3)))
433132973Swpaul#define __fastcall __stdcall __regcall
434132973Swpaul#define REGARGS1(decl1)		int dummy1, int dummy2, decl1
435132973Swpaul#define REGARGS2(decl1, decl2)	int dummy1, decl2, decl1
436132973Swpaul#define REGCALL1(arg1)		0, 0, arg1
437132973Swpaul#define REGCALL2(arg1, arg2)	0, arg2, arg1
438124504Sobrien#endif
439124504Sobrien
440128229Swpaul
441128229Swpaul/*
442128229Swpaul * This mess allows us to call a _fastcall style routine with our
443128229Swpaul * version of gcc, which lacks __attribute__((__fastcall__)). Only
444128229Swpaul * has meaning on x86; everywhere else, it's a no-op.
445128229Swpaul */
446128229Swpaul
447128229Swpaul#ifdef __i386__
448132973Swpaultypedef __fastcall int (*fcall1)(REGARGS1(uint32_t));
449132973Swpaultypedef __fastcall int (*fcall2)(REGARGS2(uint32_t, uint32_t));
450132973Swpaultypedef __fastcall int (*fcall3)(REGARGS2(uint32_t, uint32_t), uint32_t);
451132973Swpaul
452128229Swpaulstatic __inline uint32_t
453132973Swpaulfastcall1(fcall1 f, uint32_t a)
454128229Swpaul{
455132973Swpaul	return(f(REGCALL1(a)));
456128229Swpaul}
457128229Swpaul
458128229Swpaulstatic __inline uint32_t
459132973Swpaulfastcall2(fcall2 f, uint32_t a, uint32_t b)
460128229Swpaul{
461132973Swpaul	return(f(REGCALL2(a, b)));
462128229Swpaul}
463128229Swpaul
464128229Swpaulstatic __inline uint32_t
465132973Swpaulfastcall3(fcall3 f, uint32_t a, uint32_t b, uint32_t c)
466128229Swpaul{
467132973Swpaul	return(f(REGCALL2(a, b), c));
468128229Swpaul}
469128229Swpaul
470128229Swpaul#define FASTCALL1(f, a)		\
471132973Swpaul	fastcall1((fcall1)(f), (uint32_t)(a))
472128229Swpaul#define FASTCALL2(f, a, b)	\
473132973Swpaul	fastcall2((fcall2)(f), (uint32_t)(a), (uint32_t)(b))
474128229Swpaul#define FASTCALL3(f, a, b, c)	\
475132973Swpaul	fastcall3((fcall3)(f), (uint32_t)(a), (uint32_t)(b), (uint32_t)(c))
476128229Swpaul#else
477128229Swpaul#define FASTCALL1(f, a) (f)((a))
478128229Swpaul#define FASTCALL2(f, a, b) (f)((a), (b))
479128229Swpaul#define FASTCALL3(f, a, b, c) (f)((a), (b), (c))
480128229Swpaul#endif /* __i386__ */
481128229Swpaul
482141963Swpaul
483141963Swpaul/*
484141963Swpaul * AMD64 support. Microsoft uses a different calling convention
485141963Swpaul * than everyone else on the amd64 platform. Sadly, gcc has no
486141963Swpaul * built-in support for it (yet).
487141963Swpaul *
488141963Swpaul * The three major differences we're concerned with are:
489141963Swpaul *
490141963Swpaul * - The first 4 register-sized arguments are passed in the
491141963Swpaul *   %rcx, %rdx, %r8 and %r9 registers, and the rest are pushed
492141963Swpaul *   onto the stack. (The ELF ABI uses 6 registers, not 4).
493141963Swpaul *
494141963Swpaul * - The caller must reserve space on the stack for the 4
495141963Swpaul *   register arguments in case the callee has to spill them.
496141963Swpaul *
497141963Swpaul * - The stack myst be 16-byte aligned by the time the callee
498141963Swpaul *   executes. A call instruction implicitly pushes an 8 byte
499141963Swpaul *   return address onto the stack. We have to make sure that
500141963Swpaul *   the amount of space we consume, plus the return address,
501141963Swpaul *   is a multiple of 16 bytes in size. This means that in
502141963Swpaul *   some cases, we may need to chew up an extra 8 bytes on
503141963Swpaul *   the stack that will be unused.
504141963Swpaul *
505141963Swpaul * On the bright side, Microsoft seems to be using just the one
506141963Swpaul * calling convention for all functions on amd64, unlike x86 where
507141963Swpaul * they use a mix of _stdcall, _fastcall and _cdecl.
508141963Swpaul */
509141963Swpaul
510141963Swpaul#ifdef __amd64__
511141963Swpaul
512141963Swpaulextern uint64_t x86_64_call1(void *, uint64_t);
513141963Swpaulextern uint64_t x86_64_call2(void *, uint64_t, uint64_t);
514141963Swpaulextern uint64_t x86_64_call3(void *, uint64_t, uint64_t, uint64_t);
515141963Swpaulextern uint64_t x86_64_call4(void *, uint64_t, uint64_t, uint64_t, uint64_t);
516141963Swpaulextern uint64_t x86_64_call5(void *, uint64_t, uint64_t, uint64_t, uint64_t,
517141963Swpaul	uint64_t);
518141963Swpaulextern uint64_t x86_64_call6(void *, uint64_t, uint64_t, uint64_t, uint64_t,
519141963Swpaul	uint64_t, uint64_t);
520141963Swpaul
521141963Swpaul
522141963Swpaul#define MSCALL1(fn, a)						\
523141963Swpaul	x86_64_call1((fn), (uint64_t)(a))
524141963Swpaul#define MSCALL2(fn, a, b)					\
525141963Swpaul	x86_64_call2((fn), (uint64_t)(a), (uint64_t)(b))
526141963Swpaul#define MSCALL3(fn, a, b, c)					\
527141963Swpaul	x86_64_call3((fn), (uint64_t)(a), (uint64_t)(b),		\
528141963Swpaul	(uint64_t)(c))
529141963Swpaul#define MSCALL4(fn, a, b, c, d)					\
530141963Swpaul	x86_64_call4((fn), (uint64_t)(a), (uint64_t)(b),		\
531141963Swpaul	(uint64_t)(c), (uint64_t)(d))
532141963Swpaul#define MSCALL5(fn, a, b, c, d, e)				\
533141963Swpaul	x86_64_call5((fn), (uint64_t)(a), (uint64_t)(b),		\
534141963Swpaul	(uint64_t)(c), (uint64_t)(d), (uint64_t)(e))
535141963Swpaul#define MSCALL6(fn, a, b, c, d, e, f)				\
536141963Swpaul	x86_64_call6((fn), (uint64_t)(a), (uint64_t)(b),		\
537141963Swpaul	(uint64_t)(c), (uint64_t)(d), (uint64_t)(e), (uint64_t)(f))
538141963Swpaul
539141963Swpaul#else /* __amd64__ */
540141963Swpaul
541141963Swpaul#define MSCALL1(fn, a)			(fn)((a))
542141963Swpaul#define MSCALL2(fn, a, b)		(fn)((a), (b))
543141963Swpaul#define MSCALL3(fn, a, b, c)		(fn)((a), (b), (c))
544141963Swpaul#define MSCALL4(fn, a, b, c, d)		(fn)((a), (b), (c), (d))
545141963Swpaul#define MSCALL5(fn, a, b, c, d, e)	(fn)((a), (b), (c), (d), (e))
546141963Swpaul#define MSCALL6(fn, a, b, c, d, e, f)	(fn)((a), (b), (c), (d), (e), (f))
547141963Swpaul
548141963Swpaul#endif /* __amd64__ */
549141963Swpaul
550141963Swpaul
551140751Swpaul#define FUNC void(*)(void)
552141963Swpaul#define IMPORT_FUNC(x)		{ #x, (FUNC)x, NULL }
553141963Swpaul#define IMPORT_FUNC_MAP(x, y)	{ #x, (FUNC)y, NULL }
554140751Swpaul
555123474Swpaul__BEGIN_DECLS
556123474Swpaulextern int pe_get_dos_header(vm_offset_t, image_dos_header *);
557123474Swpaulextern int pe_is_nt_image(vm_offset_t);
558123474Swpaulextern int pe_get_optional_header(vm_offset_t, image_optional_header *);
559123474Swpaulextern int pe_get_file_header(vm_offset_t, image_file_header *);
560123474Swpaulextern int pe_get_section_header(vm_offset_t, image_section_header *);
561123474Swpaulextern int pe_numsections(vm_offset_t);
562123474Swpaulextern vm_offset_t pe_imagebase(vm_offset_t);
563123474Swpaulextern vm_offset_t pe_directory_offset(vm_offset_t, uint32_t);
564141963Swpaulextern vm_offset_t pe_translate_addr (vm_offset_t, vm_offset_t);
565123474Swpaulextern int pe_get_section(vm_offset_t, image_section_header *, const char *);
566123474Swpaulextern int pe_relocate(vm_offset_t);
567123474Swpaulextern int pe_get_import_descriptor(vm_offset_t, image_import_descriptor *, char *);
568123474Swpaulextern int pe_patch_imports(vm_offset_t, char *, image_patch_table *);
569124173Swpaulextern int pe_get_messagetable(vm_offset_t, message_resource_data **);
570124173Swpaulextern int pe_get_message(vm_offset_t, uint32_t, char **, int *, uint16_t *);
571123474Swpaul__END_DECLS
572123474Swpaul
573123474Swpaul#endif /* _PE_VAR_H_ */
574