pe_var.h revision 124574
144603Sdcs/*
244603Sdcs * Copyright (c) 2003
344603Sdcs *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
444603Sdcs *
544603Sdcs * Redistribution and use in source and binary forms, with or without
644603Sdcs * modification, are permitted provided that the following conditions
744603Sdcs * are met:
844603Sdcs * 1. Redistributions of source code must retain the above copyright
944603Sdcs *    notice, this list of conditions and the following disclaimer.
1044603Sdcs * 2. Redistributions in binary form must reproduce the above copyright
1144603Sdcs *    notice, this list of conditions and the following disclaimer in the
1244603Sdcs *    documentation and/or other materials provided with the distribution.
1344603Sdcs * 3. All advertising materials mentioning features or use of this software
1444603Sdcs *    must display the following acknowledgement:
1544603Sdcs *	This product includes software developed by Bill Paul.
1644603Sdcs * 4. Neither the name of the author nor the names of any co-contributors
1744603Sdcs *    may be used to endorse or promote products derived from this software
1844603Sdcs *    without specific prior written permission.
1944603Sdcs *
2044603Sdcs * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
2144603Sdcs * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2244603Sdcs * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2344603Sdcs * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
2444603Sdcs * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2550477Speter * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2644603Sdcs * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2761694Sdcs * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2861694Sdcs * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2987636Sjhb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
3087636Sjhb * THE POSSIBILITY OF SUCH DAMAGE.
3161694Sdcs *
3261694Sdcs * $FreeBSD: head/sys/compat/ndis/pe_var.h 124574 2004-01-15 19:34:56Z obrien $
3361694Sdcs */
3461694Sdcs
3561694Sdcs#ifndef _PE_VAR_H_
3661694Sdcs#define _PE_VAR_H_
3761694Sdcs
3861379Sdcs/*
3977444Sdcs *  Image Format
4077444Sdcs */
4177444Sdcs
4244603Sdcs#define IMAGE_DOS_SIGNATURE                 0x5A4D      /* MZ */
4344603Sdcs#define IMAGE_OS2_SIGNATURE                 0x454E      /* NE */
4444603Sdcs#define IMAGE_OS2_SIGNATURE_LE              0x454C      /* LE */
4544603Sdcs#define IMAGE_VXD_SIGNATURE                 0x454C      /* LE */
4644603Sdcs#define IMAGE_NT_SIGNATURE                  0x00004550  /* PE00 */
4744603Sdcs
4865883Sdcs/*
4961376Sdcs * All PE files have one of these, just so if you attempt to
5065883Sdcs * run them, they'll print out a message telling you they can
5165949Sdcs * only be run in Windows.
5261376Sdcs */
5365630Sdcs
5465883Sdcsstruct image_dos_header {
5565883Sdcs	uint16_t	idh_magic;	/* Magic number */
5665630Sdcs	uint16_t	idh_cblp;	/* Bytes on last page of file */
5765630Sdcs	uint16_t	idh_cp;		/* Pages in file */
5865630Sdcs	uint16_t	idh_crlc;	/* Relocations */
5966871Sdcs	uint16_t	idh_cparhdr;	/* Size of header in paragraphs */
6066871Sdcs	uint16_t	idh_minalloc;	/* Minimum extra paragraphs needed */
6165938Sdcs	uint16_t	idh_maxalloc;	/* Maximum extra paragraphs needed */
6266871Sdcs	uint16_t	idh_ss;		/* Initial (relative) SS value */
6366871Sdcs	uint16_t	idh_sp;		/* Initial SP value */
6466346Sdcs	uint16_t	idh_csum;	/* Checksum */
6565630Sdcs	uint16_t	idh_ip;		/* Initial IP value */
6665630Sdcs	uint16_t	idh_cs;		/* Initial (relative) CS value */
6766871Sdcs	uint16_t	idh_lfarlc;	/* File address of relocation table */
6866871Sdcs	uint16_t	idh_ovno;	/* Overlay number */
6965938Sdcs	uint16_t	idh_rsvd1[4];	/* Reserved words */
7066871Sdcs	uint16_t	idh_oemid;	/* OEM identifier (for idh_oeminfo) */
7166871Sdcs	uint16_t	idh_oeminfo;	/* OEM information; oemid specific */
7266346Sdcs	uint16_t	idh_rsvd2[10];	/* Reserved words */
7365630Sdcs	uint32_t	idh_lfanew;	/* File address of new exe header */
7465945Sdcs};
7565621Sdcs
7665621Sdcstypedef struct image_dos_header image_dos_header;
7765621Sdcs
7865621Sdcs/*
7965949Sdcs * File header format.
8065630Sdcs */
8165945Sdcs
8265621Sdcsstruct image_file_header {
8365621Sdcs	uint16_t	ifh_machine;		/* Machine type */
8465621Sdcs	uint16_t	ifh_numsections;	/* # of sections */
8565621Sdcs	uint32_t	ifh_timestamp;		/* Date/time stamp */
8665883Sdcs	uint32_t	ifh_symtblptr;		/* Offset to symbol table */
8765621Sdcs	uint32_t	ifh_numsyms; 		/* # of symbols */
8861376Sdcs	uint16_t	ifh_optionalhdrlen;	/* Size of optional header */
8965883Sdcs	uint16_t	ifh_characteristics;	/* Characteristics */
9061376Sdcs};
9161376Sdcs
92222417Sjuliantypedef struct image_file_header image_file_header;
9353672Sdcs
9444603Sdcs/* Machine types */
9544603Sdcs
9644603Sdcs#define IMAGE_FILE_MACHINE_UNKNOWN      0
9744603Sdcs#define IMAGE_FILE_MACHINE_I860         0x014d
9844603Sdcs#define IMAGE_FILE_MACHINE_I386         0x014c
9944603Sdcs#define IMAGE_FILE_MACHINE_R3000        0x0162
10044603Sdcs#define IMAGE_FILE_MACHINE_R4000        0x0166
10144603Sdcs#define IMAGE_FILE_MACHINE_R10000       0x0168
10244603Sdcs#define IMAGE_FILE_MACHINE_WCEMIPSV2    0x0169
10397201Sgordon#define IMAGE_FILE_MACHINE_ALPHA        0x0184
10444603Sdcs#define IMAGE_FILE_MACHINE_SH3          0x01a2
10544603Sdcs#define IMAGE_FILE_MACHINE_SH3DSP       0x01a3
10644603Sdcs#define IMAGE_FILE_MACHINE_SH3E         0x01a4
10744603Sdcs#define IMAGE_FILE_MACHINE_SH4          0x01a6
10844603Sdcs#define IMAGE_FILE_MACHINE_SH5          0x01a8
10944603Sdcs#define IMAGE_FILE_MACHINE_ARM          0x01c0
11044603Sdcs#define IMAGE_FILE_MACHINE_THUMB        0x01c2
11144603Sdcs#define IMAGE_FILE_MACHINE_AM33         0x01d3
11247198Sdcs#define IMAGE_FILE_MACHINE_POWERPC      0x01f0
11347198Sdcs#define IMAGE_FILE_MACHINE_POWERPCFP    0x01f1
11447198Sdcs#define IMAGE_FILE_MACHINE_IA64         0x0200
11547198Sdcs#define IMAGE_FILE_MACHINE_MIPS16       0x0266
11647198Sdcs#define IMAGE_FILE_MACHINE_ALPHA64      0x0284
11747198Sdcs#define IMAGE_FILE_MACHINE_MIPSFPU      0x0366
11847198Sdcs#define IMAGE_FILE_MACHINE_MIPSFPU16    0x0466
11947198Sdcs#define IMAGE_FILE_MACHINE_AXP64        IMAGE_FILE_MACHINE_ALPHA64
12047198Sdcs#define IMAGE_FILE_MACHINE_TRICORE      0x0520
12197201Sgordon#define IMAGE_FILE_MACHINE_CEF          0x0cef
12247198Sdcs#define IMAGE_FILE_MACHINE_EBC          0x0ebc
12347198Sdcs#define IMAGE_FILE_MACHINE_AMD64        0x8664
12447198Sdcs#define IMAGE_FILE_MACHINE_M32R         0x9041
12544603Sdcs#define IMAGE_FILE_MACHINE_CEE          0xc0ee
12644603Sdcs
12744603Sdcs/* Characteristics */
12844603Sdcs
12944603Sdcs#define IMAGE_FILE_RELOCS_STRIPPED      0x0001 /* No relocation info */
13044603Sdcs#define IMAGE_FILE_EXECUTABLE_IMAGE     0x0002
131186789Sluigi#define IMAGE_FILE_LINE_NUMS_STRIPPED   0x0004
13244603Sdcs#define IMAGE_FILE_LOCAL_SYMS_STRIPPED  0x0008
13344603Sdcs#define IMAGE_FILE_AGGRESIVE_WS_TRIM    0x0010
13444603Sdcs#define IMAGE_FILE_LARGE_ADDRESS_AWARE  0x0020
13544603Sdcs#define IMAGE_FILE_16BIT_MACHINE        0x0040
13644603Sdcs#define IMAGE_FILE_BYTES_REVERSED_LO    0x0080
13744603Sdcs#define IMAGE_FILE_32BIT_MACHINE        0x0100
13844603Sdcs#define IMAGE_FILE_DEBUG_STRIPPED       0x0200
13944603Sdcs#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP      0x0400
14044603Sdcs#define IMAGE_FILE_NET_RUN_FROM_SWAP    0x0800
14144603Sdcs#define IMAGE_FILE_SYSTEM               0x1000
14244603Sdcs#define IMAGE_FILE_DLL                  0x2000
14344603Sdcs#define IMAGE_FILE_UP_SYSTEM_ONLY       0x4000
14444603Sdcs#define IMAGE_FILE_BYTES_REVERSED_HI    0x8000
145186789Sluigi
146186789Sluigi#define IMAGE_SIZEOF_FILE_HEADER             20
14746005Sdcs
148186789Sluigi/*
149186789Sluigi * Directory format.
150186789Sluigi */
151186789Sluigi
15246005Sdcsstruct image_data_directory {
15346005Sdcs	uint32_t		idd_vaddr;	/* virtual address */
154186789Sluigi	uint32_t		idd_size;	/* size */
15546005Sdcs};
156186789Sluigi
15746005Sdcstypedef struct image_data_directory image_data_directory;
158186789Sluigi
15946005Sdcs#define IMAGE_DIRECTORY_ENTRIES_MAX    16
16044603Sdcs
16144603Sdcs/*
16244603Sdcs * Optional header format.
16344603Sdcs */
164186789Sluigi
16544603Sdcsstruct image_optional_header {
16644603Sdcs
16744603Sdcs	/* Standard fields */
16844603Sdcs
16944603Sdcs	uint16_t	ioh_magic;
17044603Sdcs	uint8_t		ioh_linkerver_major;
17144603Sdcs	uint8_t		ioh_linkerver_minor;
17244603Sdcs	uint32_t	ioh_codesize;
17365949Sdcs	uint32_t	ioh_datasize;
17465949Sdcs	uint32_t	ioh_bsssize;
17565949Sdcs	uint32_t	ioh_entryaddr;
17665949Sdcs	uint32_t	ioh_codebaseaddr;
17765949Sdcs	uint32_t	ioh_databaseaddr;
17865949Sdcs
17965949Sdcs	/* NT-specific fields */
18065949Sdcs
18165949Sdcs	uint32_t	ioh_imagebase;
18265949Sdcs	uint32_t	ioh_sectalign;
18365949Sdcs	uint32_t	ioh_filealign;
18465949Sdcs	uint16_t	ioh_osver_major;
18565949Sdcs	uint16_t	ioh_osver_minor;
18665949Sdcs	uint16_t	ioh_imagever_major;
18765949Sdcs	uint16_t	ioh_imagever_minor;
18865949Sdcs	uint16_t	ioh_subsys_major;
18965949Sdcs	uint16_t	ioh_subsys_minor;
19065949Sdcs	uint32_t	ioh_win32ver;
19144603Sdcs	uint32_t	ioh_imagesize;
19247198Sdcs	uint32_t	ioh_headersize;
193	uint32_t	ioh_csum;
194	uint16_t	ioh_subsys;
195	uint16_t	ioh_dll_characteristics;
196	uint32_t	ioh_stackreservesize;
197	uint32_t	ioh_stackcommitsize;
198	uint32_t	ioh_heapreservesize;
199	uint32_t	ioh_heapcommitsize;
200	uint16_t	ioh_loaderflags;
201	uint32_t	ioh_rva_size_cnt;
202	image_data_directory	ioh_datadir[IMAGE_DIRECTORY_ENTRIES_MAX];
203};
204
205typedef struct image_optional_header image_optional_header;
206
207struct image_nt_header {
208	uint32_t		inh_signature;
209	image_file_header	inh_filehdr;
210	image_optional_header	inh_optionalhdr;
211};
212
213typedef struct image_nt_header image_nt_header;
214
215/* Directory Entries */
216
217#define IMAGE_DIRECTORY_ENTRY_EXPORT         0   /* Export Directory */
218#define IMAGE_DIRECTORY_ENTRY_IMPORT         1   /* Import Directory */
219#define IMAGE_DIRECTORY_ENTRY_RESOURCE       2   /* Resource Directory */
220#define IMAGE_DIRECTORY_ENTRY_EXCEPTION      3   /* Exception Directory */
221#define IMAGE_DIRECTORY_ENTRY_SECURITY       4   /* Security Directory */
222#define IMAGE_DIRECTORY_ENTRY_BASERELOC      5   /* Base Relocation Table */
223#define IMAGE_DIRECTORY_ENTRY_DEBUG          6   /* Debug Directory */
224#define IMAGE_DIRECTORY_ENTRY_COPYRIGHT      7   /* Description String */
225#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR      8   /* Machine Value (MIPS GP) */
226#define IMAGE_DIRECTORY_ENTRY_TLS            9   /* TLS Directory */
227#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG   10   /* Load Configuration Directory */
228#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT  11   /* Bound Import Directory in headers */
229#define IMAGE_DIRECTORY_ENTRY_IAT           12   /* Import Address Table */
230#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT      13
231#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR    14
232
233/* Resource types */
234
235#define RT_CURSOR	1
236#define RT_BITMAP	2
237#define RT_ICON		3
238#define RT_MENU		4
239#define RT_DIALOG	5
240#define RT_STRING	6
241#define RT_FONTDIR	7
242#define RT_FONT		8
243#define RT_ACCELERATOR	9
244#define RT_RCDATA	10
245#define RT_MESSAGETABLE	11
246#define RT_GROUP_CURSOR	12
247#define RT_GROUP_ICON	14
248#define RT_VERSION	16
249#define RT_DLGINCLUDE	17
250#define RT_PLUGPLAY	19
251#define RT_VXD		20
252#define RT_ANICURSOR	21
253#define RT_ANIICON	22
254#define RT_HTML		23
255
256/*
257 * Section header format.
258 */
259
260#define IMAGE_SHORT_NAME_LEN			8
261
262struct image_section_header {
263	uint8_t		ish_name[IMAGE_SHORT_NAME_LEN];
264	union {
265		uint32_t	ish_paddr;
266		uint32_t	ish_vsize;
267	} ish_misc;
268	uint32_t	ish_vaddr;
269	uint32_t	ish_rawdatasize;
270	uint32_t	ish_rawdataaddr;
271	uint32_t	ish_relocaddr;
272	uint32_t	ish_linenumaddr;
273	uint16_t	ish_numrelocs;
274	uint16_t	ish_numlinenums;
275	uint32_t	ish_characteristics;
276};
277
278typedef struct image_section_header image_section_header;
279
280#define IMAGE_SIZEOF_SECTION_HEADER          40
281
282/*
283 * Import format
284 */
285
286struct image_import_by_name {
287	uint16_t	iibn_hint;
288	u_int8_t	iibn_name[1];
289};
290
291#define IMAGE_ORDINAL_FLAG 0x80000000
292#define IMAGE_ORDINAL(Ordinal) (Ordinal & 0xffff)
293
294struct image_import_descriptor {
295	uint32_t	iid_import_name_table_addr;
296	uint32_t	iid_timestamp;
297	uint32_t	iid_forwardchain;
298	uint32_t	iid_nameaddr;
299	uint32_t	iid_import_address_table_addr;
300};
301
302typedef struct image_import_descriptor image_import_descriptor;
303
304struct image_base_reloc {
305	uint32_t	ibr_vaddr;
306	uint32_t	ibr_blocksize;
307	uint16_t	ibr_rel[1];
308};
309
310typedef struct image_base_reloc image_base_reloc;
311
312#define IMR_RELTYPE(x)		((x >> 12) & 0xF)
313#define IMR_RELOFFSET(x)	(x & 0xFFF)
314
315/* generic relocation types */
316#define IMAGE_REL_BASED_ABSOLUTE                0
317#define IMAGE_REL_BASED_HIGH                    1
318#define IMAGE_REL_BASED_LOW                     2
319#define IMAGE_REL_BASED_HIGHLOW                 3
320#define IMAGE_REL_BASED_HIGHADJ                 4
321#define IMAGE_REL_BASED_MIPS_JMPADDR            5
322#define IMAGE_REL_BASED_SECTION                 6
323#define IMAGE_REL_BASED_REL                     7
324#define IMAGE_REL_BASED_MIPS_JMPADDR16          9
325#define IMAGE_REL_BASED_IA64_IMM64              9 /* yes, 9 too */
326#define IMAGE_REL_BASED_DIR64                   10
327#define IMAGE_REL_BASED_HIGH3ADJ                11
328
329struct image_resource_directory_entry {
330	uint32_t		irde_name;
331	uint32_t		irde_dataoff;
332};
333
334typedef struct image_resource_directory_entry image_resource_directory_entry;
335
336#define RESOURCE_NAME_STR	0x80000000
337#define RESOURCE_DIR_FLAG	0x80000000
338
339struct image_resource_directory {
340	uint32_t		ird_characteristics;
341	uint32_t		ird_timestamp;
342	uint16_t		ird_majorver;
343	uint16_t		ird_minorver;
344	uint16_t		ird_named_entries;
345	uint16_t		ird_id_entries;
346#ifdef notdef
347	image_resource_directory_entry	ird_entries[1];
348#endif
349};
350
351typedef struct image_resource_directory image_resource_directory;
352
353struct image_resource_directory_string {
354	uint16_t		irds_len;
355	char			irds_name[1];
356};
357
358typedef struct image_resource_directory_string image_resource_directory_string;
359
360struct image_resource_directory_string_u {
361	uint16_t		irds_len;
362	char			irds_name[1];
363};
364
365typedef struct image_resource_directory_string_u
366	image_resource_directory_string_u;
367
368struct image_resource_data_entry {
369	uint32_t		irde_offset;
370	uint32_t		irde_size;
371	uint32_t		irde_codepage;
372	uint32_t		irde_rsvd;
373};
374
375typedef struct image_resource_data_entry image_resource_data_entry;
376
377struct message_resource_data {
378	uint32_t		mrd_numblocks;
379#ifdef notdef
380	message_resource_block	mrd_blocks[1];
381#endif
382};
383
384typedef struct message_resource_data message_resource_data;
385
386struct message_resource_block {
387	uint32_t		mrb_lowid;
388	uint32_t		mrb_highid;
389	uint32_t		mrb_entryoff;
390};
391
392typedef struct message_resource_block message_resource_block;
393
394struct message_resource_entry {
395	uint16_t		mre_len;
396	uint16_t		mre_flags;
397	char			mre_text[];
398};
399
400typedef struct message_resource_entry message_resource_entry;
401
402#define MESSAGE_RESOURCE_UNICODE	0x0001
403
404struct image_patch_table {
405	char		*ipt_name;
406	void		(*ipt_func)(void);
407};
408
409typedef struct image_patch_table image_patch_table;
410
411/*
412 * Note: Windows uses the _stdcall calling convention. This means
413 * that the callback functions provided in the function table must
414 * be declared using __attribute__((__stdcall__)), otherwise the
415 * Windows code will likely screw up the %esp register and cause
416 * us to jump to an invalid address when it returns.
417 */
418
419#ifdef __amd64__
420#define	__stdcall
421#define	NDIS_BUS_SPACE_IO	AMD64_BUS_SPACE_IO
422#define	NDIS_BUS_SPACE_MEM	AMD64_BUS_SPACE_MEM
423#else
424#define	__stdcall __attribute__((__stdcall__))
425#define	NDIS_BUS_SPACE_IO	I386_BUS_SPACE_IO
426#define	NDIS_BUS_SPACE_MEM	I386_BUS_SPACE_MEM
427#endif
428
429__BEGIN_DECLS
430extern int pe_get_dos_header(vm_offset_t, image_dos_header *);
431extern int pe_is_nt_image(vm_offset_t);
432extern int pe_get_optional_header(vm_offset_t, image_optional_header *);
433extern int pe_get_file_header(vm_offset_t, image_file_header *);
434extern int pe_get_section_header(vm_offset_t, image_section_header *);
435extern int pe_numsections(vm_offset_t);
436extern vm_offset_t pe_imagebase(vm_offset_t);
437extern vm_offset_t pe_directory_offset(vm_offset_t, uint32_t);
438extern vm_offset_t pe_translate_addr (vm_offset_t, uint32_t);
439extern int pe_get_section(vm_offset_t, image_section_header *, const char *);
440extern int pe_relocate(vm_offset_t);
441extern int pe_get_import_descriptor(vm_offset_t, image_import_descriptor *, char *);
442extern int pe_patch_imports(vm_offset_t, char *, image_patch_table *);
443extern int pe_get_messagetable(vm_offset_t, message_resource_data **);
444extern int pe_get_message(vm_offset_t, uint32_t, char **, int *, uint16_t *);
445__END_DECLS
446
447#endif /* _PE_VAR_H_ */
448