pe_var.h revision 124165
16934SN/A/*
26934SN/A * Copyright (c) 2003
36934SN/A *	Bill Paul <wpaul@windriver.com>.  All rights reserved.
46934SN/A *
56934SN/A * Redistribution and use in source and binary forms, with or without
66934SN/A * modification, are permitted provided that the following conditions
76934SN/A * are met:
86934SN/A * 1. Redistributions of source code must retain the above copyright
96934SN/A *    notice, this list of conditions and the following disclaimer.
106934SN/A * 2. Redistributions in binary form must reproduce the above copyright
116934SN/A *    notice, this list of conditions and the following disclaimer in the
126934SN/A *    documentation and/or other materials provided with the distribution.
136934SN/A * 3. All advertising materials mentioning features or use of this software
146934SN/A *    must display the following acknowledgement:
156934SN/A *	This product includes software developed by Bill Paul.
166934SN/A * 4. Neither the name of the author nor the names of any co-contributors
176934SN/A *    may be used to endorse or promote products derived from this software
186934SN/A *    without specific prior written permission.
196934SN/A *
206934SN/A * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
216934SN/A * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
226934SN/A * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
236934SN/A * ARE DISCLAIMED.  IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
246934SN/A * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
256934SN/A * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
266934SN/A * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
276934SN/A * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
286934SN/A * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
296934SN/A * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
306934SN/A * THE POSSIBILITY OF SUCH DAMAGE.
316934SN/A *
326934SN/A * $FreeBSD: head/sys/compat/ndis/pe_var.h 124165 2004-01-06 07:09:26Z wpaul $
336934SN/A */
346934SN/A
356934SN/A#ifndef _PE_VAR_H_
366934SN/A#define _PE_VAR_H_
376934SN/A
386934SN/A/*
396934SN/A *  Image Format
406934SN/A */
416934SN/A
426934SN/A#define IMAGE_DOS_SIGNATURE                 0x5A4D      /* MZ */
436934SN/A#define IMAGE_OS2_SIGNATURE                 0x454E      /* NE */
446934SN/A#define IMAGE_OS2_SIGNATURE_LE              0x454C      /* LE */
456936SN/A#define IMAGE_VXD_SIGNATURE                 0x454C      /* LE */
466936SN/A#define IMAGE_NT_SIGNATURE                  0x00004550  /* PE00 */
476934SN/A
486936SN/A/*
496934SN/A * All PE files have one of these, just so if you attempt to
506936SN/A * run them, they'll print out a message telling you they can
516936SN/A * only be run in Windows.
526936SN/A */
536936SN/A
546936SN/Astruct image_dos_header {
556936SN/A	uint16_t	idh_magic;	/* Magic number */
566936SN/A	uint16_t	idh_cblp;	/* Bytes on last page of file */
576936SN/A	uint16_t	idh_cp;		/* Pages in file */
586936SN/A	uint16_t	idh_crlc;	/* Relocations */
596936SN/A	uint16_t	idh_cparhdr;	/* Size of header in paragraphs */
606936SN/A	uint16_t	idh_minalloc;	/* Minimum extra paragraphs needed */
616936SN/A	uint16_t	idh_maxalloc;	/* Maximum extra paragraphs needed */
6212375Sprr	uint16_t	idh_ss;		/* Initial (relative) SS value */
6312375Sprr	uint16_t	idh_sp;		/* Initial SP value */
6412375Sprr	uint16_t	idh_csum;	/* Checksum */
6512375Sprr	uint16_t	idh_ip;		/* Initial IP value */
666936SN/A	uint16_t	idh_cs;		/* Initial (relative) CS value */
676936SN/A	uint16_t	idh_lfarlc;	/* File address of relocation table */
686934SN/A	uint16_t	idh_ovno;	/* Overlay number */
696934SN/A	uint16_t	idh_rsvd1[4];	/* Reserved words */
706934SN/A	uint16_t	idh_oemid;	/* OEM identifier (for idh_oeminfo) */
716936SN/A	uint16_t	idh_oeminfo;	/* OEM information; oemid specific */
726936SN/A	uint16_t	idh_rsvd2[10];	/* Reserved words */
736934SN/A	uint32_t	idh_lfanew;	/* File address of new exe header */
746936SN/A};
756934SN/A
766934SN/Atypedef struct image_dos_header image_dos_header;
776934SN/A
786936SN/A/*
796936SN/A * File header format.
806934SN/A */
816934SN/A
826936SN/Astruct image_file_header {
836934SN/A	uint16_t	ifh_machine;		/* Machine type */
846934SN/A	uint16_t	ifh_numsections;	/* # of sections */
856934SN/A	uint32_t	ifh_timestamp;		/* Date/time stamp */
866934SN/A	uint32_t	ifh_symtblptr;		/* Offset to symbol table */
876934SN/A	uint32_t	ifh_numsyms; 		/* # of symbols */
886934SN/A	uint16_t	ifh_optionalhdrlen;	/* Size of optional header */
896934SN/A	uint16_t	ifh_characteristics;	/* Characteristics */
906934SN/A};
916934SN/A
926934SN/Atypedef struct image_file_header image_file_header;
936934SN/A
946934SN/A/* Machine types */
956934SN/A
966934SN/A#define IMAGE_FILE_MACHINE_UNKNOWN      0
976934SN/A#define IMAGE_FILE_MACHINE_I860         0x014d
986934SN/A#define IMAGE_FILE_MACHINE_I386         0x014c
996934SN/A#define IMAGE_FILE_MACHINE_R3000        0x0162
1006934SN/A#define IMAGE_FILE_MACHINE_R4000        0x0166
1016934SN/A#define IMAGE_FILE_MACHINE_R10000       0x0168
1026934SN/A#define IMAGE_FILE_MACHINE_WCEMIPSV2    0x0169
1036934SN/A#define IMAGE_FILE_MACHINE_ALPHA        0x0184
1046934SN/A#define IMAGE_FILE_MACHINE_SH3          0x01a2
1056934SN/A#define IMAGE_FILE_MACHINE_SH3DSP       0x01a3
1066934SN/A#define IMAGE_FILE_MACHINE_SH3E         0x01a4
1076934SN/A#define IMAGE_FILE_MACHINE_SH4          0x01a6
1086934SN/A#define IMAGE_FILE_MACHINE_SH5          0x01a8
1096934SN/A#define IMAGE_FILE_MACHINE_ARM          0x01c0
1106934SN/A#define IMAGE_FILE_MACHINE_THUMB        0x01c2
1116934SN/A#define IMAGE_FILE_MACHINE_AM33         0x01d3
1126934SN/A#define IMAGE_FILE_MACHINE_POWERPC      0x01f0
1136934SN/A#define IMAGE_FILE_MACHINE_POWERPCFP    0x01f1
1146934SN/A#define IMAGE_FILE_MACHINE_IA64         0x0200
1156934SN/A#define IMAGE_FILE_MACHINE_MIPS16       0x0266
1166934SN/A#define IMAGE_FILE_MACHINE_ALPHA64      0x0284
1176934SN/A#define IMAGE_FILE_MACHINE_MIPSFPU      0x0366
1186934SN/A#define IMAGE_FILE_MACHINE_MIPSFPU16    0x0466
1196934SN/A#define IMAGE_FILE_MACHINE_AXP64        IMAGE_FILE_MACHINE_ALPHA64
1206934SN/A#define IMAGE_FILE_MACHINE_TRICORE      0x0520
1216934SN/A#define IMAGE_FILE_MACHINE_CEF          0x0cef
1226934SN/A#define IMAGE_FILE_MACHINE_EBC          0x0ebc
1236934SN/A#define IMAGE_FILE_MACHINE_AMD64        0x8664
1246934SN/A#define IMAGE_FILE_MACHINE_M32R         0x9041
1256934SN/A#define IMAGE_FILE_MACHINE_CEE          0xc0ee
1266934SN/A
1276934SN/A/* Characteristics */
1286934SN/A
1296934SN/A#define IMAGE_FILE_RELOCS_STRIPPED      0x0001 /* No relocation info */
1306934SN/A#define IMAGE_FILE_EXECUTABLE_IMAGE     0x0002
1316934SN/A#define IMAGE_FILE_LINE_NUMS_STRIPPED   0x0004
1326934SN/A#define IMAGE_FILE_LOCAL_SYMS_STRIPPED  0x0008
1336934SN/A#define IMAGE_FILE_AGGRESIVE_WS_TRIM    0x0010
1346934SN/A#define IMAGE_FILE_LARGE_ADDRESS_AWARE  0x0020
1356934SN/A#define IMAGE_FILE_16BIT_MACHINE        0x0040
1366934SN/A#define IMAGE_FILE_BYTES_REVERSED_LO    0x0080
1376934SN/A#define IMAGE_FILE_32BIT_MACHINE        0x0100
1386934SN/A#define IMAGE_FILE_DEBUG_STRIPPED       0x0200
1396934SN/A#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP      0x0400
1406934SN/A#define IMAGE_FILE_NET_RUN_FROM_SWAP    0x0800
1416934SN/A#define IMAGE_FILE_SYSTEM               0x1000
1426934SN/A#define IMAGE_FILE_DLL                  0x2000
1436934SN/A#define IMAGE_FILE_UP_SYSTEM_ONLY       0x4000
1446934SN/A#define IMAGE_FILE_BYTES_REVERSED_HI    0x8000
1456934SN/A
1466934SN/A#define IMAGE_SIZEOF_FILE_HEADER             20
1476934SN/A
1486934SN/A/*
1496934SN/A * Directory format.
1506934SN/A */
1516934SN/A
1526934SN/Astruct image_data_directory {
1536934SN/A	uint32_t		idd_vaddr;	/* virtual address */
1546934SN/A	uint32_t		idd_size;	/* size */
1556934SN/A};
1566934SN/A
1576934SN/Atypedef struct image_data_directory image_data_directory;
1586934SN/A
1596934SN/A#define IMAGE_DIRECTORY_ENTRIES_MAX    16
1606934SN/A
1616934SN/A/*
1626934SN/A * Optional header format.
1636934SN/A */
1646934SN/A
1656934SN/Astruct image_optional_header {
1666934SN/A
1676934SN/A	/* Standard fields */
1686934SN/A
1696934SN/A	uint16_t	ioh_magic;
1706934SN/A	uint8_t		ioh_linkerver_major;
1716934SN/A	uint8_t		ioh_linkerver_minor;
1726934SN/A	uint32_t	ioh_codesize;
1736934SN/A	uint32_t	ioh_datasize;
1746934SN/A	uint32_t	ioh_bsssize;
1756934SN/A	uint32_t	ioh_entryaddr;
1766934SN/A	uint32_t	ioh_codebaseaddr;
1776934SN/A	uint32_t	ioh_databaseaddr;
1786934SN/A
1796934SN/A	/* NT-specific fields */
1806934SN/A
1816934SN/A	uint32_t	ioh_imagebase;
1826934SN/A	uint32_t	ioh_sectalign;
1836934SN/A	uint32_t	ioh_filealign;
1846934SN/A	uint16_t	ioh_osver_major;
1856934SN/A	uint16_t	ioh_osver_minor;
1866934SN/A	uint16_t	ioh_imagever_major;
1876934SN/A	uint16_t	ioh_imagever_minor;
1886934SN/A	uint16_t	ioh_subsys_major;
1896934SN/A	uint16_t	ioh_subsys_minor;
1906934SN/A	uint32_t	ioh_win32ver;
1916934SN/A	uint32_t	ioh_imagesize;
1926936SN/A	uint32_t	ioh_headersize;
1936936SN/A	uint32_t	ioh_csum;
1946936SN/A	uint16_t	ioh_subsys;
19512375Sprr	uint16_t	ioh_dll_characteristics;
19612375Sprr	uint32_t	ioh_stackreservesize;
19712375Sprr	uint32_t	ioh_stackcommitsize;
19812375Sprr	uint32_t	ioh_heapreservesize;
1996936SN/A	uint32_t	ioh_heapcommitsize;
20012949Sprr	uint16_t	ioh_loaderflags;
2016936SN/A	uint32_t	ioh_rva_size_cnt;
2026934SN/A	image_data_directory	ioh_datadir[IMAGE_DIRECTORY_ENTRIES_MAX];
2036934SN/A};
2046934SN/A
2056934SN/Atypedef struct image_optional_header image_optional_header;
2066936SN/A
2076934SN/Astruct image_nt_header {
2086934SN/A	uint32_t		inh_signature;
2096934SN/A	image_file_header	inh_filehdr;
2106934SN/A	image_optional_header	inh_optionalhdr;
2116934SN/A};
2126936SN/A
2136934SN/Atypedef struct image_nt_header image_nt_header;
2146934SN/A
2156934SN/A/* Directory Entries */
21612949Sprr
21712949Sprr#define IMAGE_DIRECTORY_ENTRY_EXPORT         0   /* Export Directory */
2186934SN/A#define IMAGE_DIRECTORY_ENTRY_IMPORT         1   /* Import Directory */
2196934SN/A#define IMAGE_DIRECTORY_ENTRY_RESOURCE       2   /* Resource Directory */
2206934SN/A#define IMAGE_DIRECTORY_ENTRY_EXCEPTION      3   /* Exception Directory */
2216936SN/A#define IMAGE_DIRECTORY_ENTRY_SECURITY       4   /* Security Directory */
2226934SN/A#define IMAGE_DIRECTORY_ENTRY_BASERELOC      5   /* Base Relocation Table */
2236934SN/A#define IMAGE_DIRECTORY_ENTRY_DEBUG          6   /* Debug Directory */
2246934SN/A#define IMAGE_DIRECTORY_ENTRY_COPYRIGHT      7   /* Description String */
2256936SN/A#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR      8   /* Machine Value (MIPS GP) */
2266934SN/A#define IMAGE_DIRECTORY_ENTRY_TLS            9   /* TLS Directory */
2276934SN/A#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG   10   /* Load Configuration Directory */
2286934SN/A#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT  11   /* Bound Import Directory in headers */
2296936SN/A#define IMAGE_DIRECTORY_ENTRY_IAT           12   /* Import Address Table */
2306934SN/A#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT      13
2316934SN/A#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR    14
2326934SN/A
2336934SN/A/* Resource types */
2346934SN/A
2356934SN/A#define RT_CURSOR	1
2366936SN/A#define RT_BITMAP	2
2376934SN/A#define RT_ICON		3
2386934SN/A#define RT_MENU		4
2396934SN/A#define RT_DIALOG	5
2406934SN/A#define RT_STRING	6
2416936SN/A#define RT_FONTDIR	7
2426934SN/A#define RT_FONT		8
2436934SN/A#define RT_ACCELERATOR	9
2446934SN/A#define RT_RCDATA	10
2456936SN/A#define RT_MESSAGETABLE	11
2466936SN/A#define RT_GROUP_CURSOR	12
2476934SN/A#define RT_GROUP_ICON	14
2486934SN/A#define RT_VERSION	16
2496934SN/A#define RT_DLGINCLUDE	17
2506936SN/A#define RT_PLUGPLAY	19
2516934SN/A#define RT_VXD		20
2526936SN/A#define RT_ANICURSOR	21
2536936SN/A#define RT_ANIICON	22
2546936SN/A#define RT_HTML		23
2556936SN/A
2566934SN/A/*
2576934SN/A * Section header format.
2586934SN/A */
2596934SN/A
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__BEGIN_DECLS
412extern int pe_get_dos_header(vm_offset_t, image_dos_header *);
413extern int pe_is_nt_image(vm_offset_t);
414extern int pe_get_optional_header(vm_offset_t, image_optional_header *);
415extern int pe_get_file_header(vm_offset_t, image_file_header *);
416extern int pe_get_section_header(vm_offset_t, image_section_header *);
417extern int pe_numsections(vm_offset_t);
418extern vm_offset_t pe_imagebase(vm_offset_t);
419extern vm_offset_t pe_directory_offset(vm_offset_t, uint32_t);
420extern vm_offset_t pe_translate_addr (vm_offset_t, uint32_t);
421extern int pe_get_section(vm_offset_t, image_section_header *, const char *);
422extern int pe_relocate(vm_offset_t);
423extern int pe_get_import_descriptor(vm_offset_t, image_import_descriptor *, char *);
424extern int pe_patch_imports(vm_offset_t, char *, image_patch_table *);
425#ifdef _KERNEL
426extern int pe_get_messagetable(vm_offset_t, message_resource_data  **);
427extern int pe_get_message(vm_offset_t, uint32_t, char **, int *);
428#endif
429__END_DECLS
430
431#endif /* _PE_VAR_H_ */
432