3 * Bill Paul <wpaul@windriver.com>. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Bill Paul.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY Bill Paul AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL Bill Paul OR THE VOICES IN HIS HEAD
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
32 * $FreeBSD: src/sys/compat/ndis/pe_var.h,v 1.8.2.2 2005/02/18 16:30:09 wpaul Exp $
46 #define IMAGE_DOS_SIGNATURE 0x5A4D /* MZ */
47 #define IMAGE_OS2_SIGNATURE 0x454E /* NE */
48 #define IMAGE_OS2_SIGNATURE_LE 0x454C /* LE */
49 #define IMAGE_VXD_SIGNATURE 0x454C /* LE */
50 #define IMAGE_NT_SIGNATURE 0x00004550 /* PE00 */
53 * All PE files have one of these, just so if you attempt to
54 * run them, they'll print out a message telling you they can
55 * only be run in Windows.
58 struct image_dos_header
{
59 uint16_t idh_magic
; /* Magic number */
60 uint16_t idh_cblp
; /* Bytes on last page of file */
61 uint16_t idh_cp
; /* Pages in file */
62 uint16_t idh_crlc
; /* Relocations */
63 uint16_t idh_cparhdr
; /* Size of header in paragraphs */
64 uint16_t idh_minalloc
; /* Minimum extra paragraphs needed */
65 uint16_t idh_maxalloc
; /* Maximum extra paragraphs needed */
66 uint16_t idh_ss
; /* Initial (relative) SS value */
67 uint16_t idh_sp
; /* Initial SP value */
68 uint16_t idh_csum
; /* Checksum */
69 uint16_t idh_ip
; /* Initial IP value */
70 uint16_t idh_cs
; /* Initial (relative) CS value */
71 uint16_t idh_lfarlc
; /* File address of relocation table */
72 uint16_t idh_ovno
; /* Overlay number */
73 uint16_t idh_rsvd1
[4]; /* Reserved words */
74 uint16_t idh_oemid
; /* OEM identifier (for idh_oeminfo) */
75 uint16_t idh_oeminfo
; /* OEM information; oemid specific */
76 uint16_t idh_rsvd2
[10]; /* Reserved words */
77 uint32_t idh_lfanew
; /* File address of new exe header */
80 typedef struct image_dos_header image_dos_header
;
86 struct image_file_header
{
87 uint16_t ifh_machine
; /* Machine type */
88 uint16_t ifh_numsections
; /* # of sections */
89 uint32_t ifh_timestamp
; /* Date/time stamp */
90 uint32_t ifh_symtblptr
; /* Offset to symbol table */
91 uint32_t ifh_numsyms
; /* # of symbols */
92 uint16_t ifh_optionalhdrlen
; /* Size of optional header */
93 uint16_t ifh_characteristics
; /* Characteristics */
96 typedef struct image_file_header image_file_header
;
100 #define IMAGE_FILE_MACHINE_UNKNOWN 0
101 #define IMAGE_FILE_MACHINE_I860 0x014d
102 #define IMAGE_FILE_MACHINE_I386 0x014c
103 #define IMAGE_FILE_MACHINE_R3000 0x0162
104 #define IMAGE_FILE_MACHINE_R4000 0x0166
105 #define IMAGE_FILE_MACHINE_R10000 0x0168
106 #define IMAGE_FILE_MACHINE_WCEMIPSV2 0x0169
107 #define IMAGE_FILE_MACHINE_ALPHA 0x0184
108 #define IMAGE_FILE_MACHINE_SH3 0x01a2
109 #define IMAGE_FILE_MACHINE_SH3DSP 0x01a3
110 #define IMAGE_FILE_MACHINE_SH3E 0x01a4
111 #define IMAGE_FILE_MACHINE_SH4 0x01a6
112 #define IMAGE_FILE_MACHINE_SH5 0x01a8
113 #define IMAGE_FILE_MACHINE_ARM 0x01c0
114 #define IMAGE_FILE_MACHINE_THUMB 0x01c2
115 #define IMAGE_FILE_MACHINE_AM33 0x01d3
116 #define IMAGE_FILE_MACHINE_POWERPC 0x01f0
117 #define IMAGE_FILE_MACHINE_POWERPCFP 0x01f1
118 #define IMAGE_FILE_MACHINE_IA64 0x0200
119 #define IMAGE_FILE_MACHINE_MIPS16 0x0266
120 #define IMAGE_FILE_MACHINE_ALPHA64 0x0284
121 #define IMAGE_FILE_MACHINE_MIPSFPU 0x0366
122 #define IMAGE_FILE_MACHINE_MIPSFPU16 0x0466
123 #define IMAGE_FILE_MACHINE_AXP64 IMAGE_FILE_MACHINE_ALPHA64
124 #define IMAGE_FILE_MACHINE_TRICORE 0x0520
125 #define IMAGE_FILE_MACHINE_CEF 0x0cef
126 #define IMAGE_FILE_MACHINE_EBC 0x0ebc
127 #define IMAGE_FILE_MACHINE_AMD64 0x8664
128 #define IMAGE_FILE_MACHINE_M32R 0x9041
129 #define IMAGE_FILE_MACHINE_CEE 0xc0ee
131 /* Characteristics */
133 #define IMAGE_FILE_RELOCS_STRIPPED 0x0001 /* No relocation info */
134 #define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002
135 #define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004
136 #define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008
137 #define IMAGE_FILE_AGGRESIVE_WS_TRIM 0x0010
138 #define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020
139 #define IMAGE_FILE_16BIT_MACHINE 0x0040
140 #define IMAGE_FILE_BYTES_REVERSED_LO 0x0080
141 #define IMAGE_FILE_32BIT_MACHINE 0x0100
142 #define IMAGE_FILE_DEBUG_STRIPPED 0x0200
143 #define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400
144 #define IMAGE_FILE_NET_RUN_FROM_SWAP 0x0800
145 #define IMAGE_FILE_SYSTEM 0x1000
146 #define IMAGE_FILE_DLL 0x2000
147 #define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000
148 #define IMAGE_FILE_BYTES_REVERSED_HI 0x8000
150 #define IMAGE_SIZEOF_FILE_HEADER 20
156 struct image_data_directory
{
157 uint32_t idd_vaddr
; /* virtual address */
158 uint32_t idd_size
; /* size */
161 typedef struct image_data_directory image_data_directory
;
163 #define IMAGE_DIRECTORY_ENTRIES_MAX 16
166 * Optional header format.
169 struct image_optional_header
{
171 /* Standard fields */
174 uint8_t ioh_linkerver_major
;
175 uint8_t ioh_linkerver_minor
;
176 uint32_t ioh_codesize
;
177 uint32_t ioh_datasize
;
178 uint32_t ioh_bsssize
;
179 uint32_t ioh_entryaddr
;
180 uint32_t ioh_codebaseaddr
;
182 uint32_t ioh_databaseaddr
;
185 /* NT-specific fields */
187 uintptr_t ioh_imagebase
;
188 uint32_t ioh_sectalign
;
189 uint32_t ioh_filealign
;
190 uint16_t ioh_osver_major
;
191 uint16_t ioh_osver_minor
;
192 uint16_t ioh_imagever_major
;
193 uint16_t ioh_imagever_minor
;
194 uint16_t ioh_subsys_major
;
195 uint16_t ioh_subsys_minor
;
196 uint32_t ioh_win32ver
;
197 uint32_t ioh_imagesize
;
198 uint32_t ioh_headersize
;
201 uint16_t ioh_dll_characteristics
;
202 uintptr_t ioh_stackreservesize
;
203 uintptr_t ioh_stackcommitsize
;
204 uintptr_t ioh_heapreservesize
;
205 uintptr_t ioh_heapcommitsize
;
206 uint16_t ioh_loaderflags
;
207 uint32_t ioh_rva_size_cnt
;
208 image_data_directory ioh_datadir
[IMAGE_DIRECTORY_ENTRIES_MAX
];
211 typedef struct image_optional_header image_optional_header
;
213 struct image_nt_header
{
214 uint32_t inh_signature
;
215 image_file_header inh_filehdr
;
216 image_optional_header inh_optionalhdr
;
219 typedef struct image_nt_header image_nt_header
;
221 /* Directory Entries */
223 #define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */
224 #define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */
225 #define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */
226 #define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */
227 #define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */
228 #define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */
229 #define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */
230 #define IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 /* Description String */
231 #define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* Machine Value (MIPS GP) */
232 #define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */
233 #define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */
234 #define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */
235 #define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */
236 #define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13
237 #define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14
249 #define RT_ACCELERATOR 9
251 #define RT_MESSAGETABLE 11
252 #define RT_GROUP_CURSOR 12
253 #define RT_GROUP_ICON 14
254 #define RT_VERSION 16
255 #define RT_DLGINCLUDE 17
256 #define RT_PLUGPLAY 19
258 #define RT_ANICURSOR 21
259 #define RT_ANIICON 22
263 * Section header format.
266 #define IMAGE_SHORT_NAME_LEN 8
268 struct image_section_header
{
269 uint8_t ish_name
[IMAGE_SHORT_NAME_LEN
];
275 uint32_t ish_rawdatasize
;
276 uint32_t ish_rawdataaddr
;
277 uint32_t ish_relocaddr
;
278 uint32_t ish_linenumaddr
;
279 uint16_t ish_numrelocs
;
280 uint16_t ish_numlinenums
;
281 uint32_t ish_characteristics
;
284 typedef struct image_section_header image_section_header
;
286 #define IMAGE_SIZEOF_SECTION_HEADER 40
292 struct image_import_by_name
{
294 uint8_t iibn_name
[1];
297 #define IMAGE_ORDINAL_FLAG 0x80000000
298 #define IMAGE_ORDINAL(Ordinal) (Ordinal & 0xffff)
300 struct image_import_descriptor
{
301 uint32_t iid_import_name_table_addr
;
302 uint32_t iid_timestamp
;
303 uint32_t iid_forwardchain
;
304 uint32_t iid_nameaddr
;
305 uint32_t iid_import_address_table_addr
;
308 typedef struct image_import_descriptor image_import_descriptor
;
310 struct image_base_reloc
{
312 uint32_t ibr_blocksize
;
316 typedef struct image_base_reloc image_base_reloc
;
318 #define IMR_RELTYPE(x) ((x >> 12) & 0xF)
319 #define IMR_RELOFFSET(x) (x & 0xFFF)
321 /* generic relocation types */
322 #define IMAGE_REL_BASED_ABSOLUTE 0
323 #define IMAGE_REL_BASED_HIGH 1
324 #define IMAGE_REL_BASED_LOW 2
325 #define IMAGE_REL_BASED_HIGHLOW 3
326 #define IMAGE_REL_BASED_HIGHADJ 4
327 #define IMAGE_REL_BASED_MIPS_JMPADDR 5
328 #define IMAGE_REL_BASED_SECTION 6
329 #define IMAGE_REL_BASED_REL 7
330 #define IMAGE_REL_BASED_MIPS_JMPADDR16 9
331 #define IMAGE_REL_BASED_IA64_IMM64 9 /* yes, 9 too */
332 #define IMAGE_REL_BASED_DIR64 10
333 #define IMAGE_REL_BASED_HIGH3ADJ 11
335 struct image_resource_directory_entry
{
337 uint32_t irde_dataoff
;
340 typedef struct image_resource_directory_entry image_resource_directory_entry
;
342 #define RESOURCE_NAME_STR 0x80000000
343 #define RESOURCE_DIR_FLAG 0x80000000
345 struct image_resource_directory
{
346 uint32_t ird_characteristics
;
347 uint32_t ird_timestamp
;
348 uint16_t ird_majorver
;
349 uint16_t ird_minorver
;
350 uint16_t ird_named_entries
;
351 uint16_t ird_id_entries
;
353 image_resource_directory_entry ird_entries
[1];
357 typedef struct image_resource_directory image_resource_directory
;
359 struct image_resource_directory_string
{
364 typedef struct image_resource_directory_string image_resource_directory_string
;
366 struct image_resource_directory_string_u
{
371 typedef struct image_resource_directory_string_u
372 image_resource_directory_string_u
;
374 struct image_resource_data_entry
{
375 uint32_t irde_offset
;
377 uint32_t irde_codepage
;
381 typedef struct image_resource_data_entry image_resource_data_entry
;
383 struct message_resource_data
{
384 uint32_t mrd_numblocks
;
386 message_resource_block mrd_blocks
[1];
390 typedef struct message_resource_data message_resource_data
;
392 struct message_resource_block
{
395 uint32_t mrb_entryoff
;
398 typedef struct message_resource_block message_resource_block
;
400 struct message_resource_entry
{
406 typedef struct message_resource_entry message_resource_entry
;
408 #define MESSAGE_RESOURCE_UNICODE 0x0001
410 struct image_patch_table
{
411 const char *ipt_name
;
412 void (*ipt_func
)(void);
413 void (*ipt_wrap
)(void);
416 typedef struct image_patch_table image_patch_table
;
419 * Note: Windows uses the _stdcall calling convention. This means
420 * that the callback functions provided in the function table must
421 * be declared using __attribute__((__stdcall__)), otherwise the
422 * Windows code will likely screw up the %esp register and cause
423 * us to jump to an invalid address when it returns.
430 #define REGARGS1(decl1) decl1
431 #define REGARGS2(decl1, decl2) decl1, decl2
432 #define REGCALL1(arg1) arg1
433 #define REGCALL2(arg1, arg2) arg1, arg2
435 #define __stdcall __attribute__((__stdcall__))
436 #define __regcall __attribute__((__regparm__(3)))
437 #define __fastcall __stdcall __regcall
438 #define REGARGS1(decl1) int dummy1, int dummy2, decl1
439 #define REGARGS2(decl1, decl2) int dummy1, decl2, decl1
440 #define REGCALL1(arg1) 0, 0, arg1
441 #define REGCALL2(arg1, arg2) 0, arg2, arg1
446 * This mess allows us to call a _fastcall style routine with our
447 * version of gcc, which lacks __attribute__((__fastcall__)). Only
448 * has meaning on x86; everywhere else, it's a no-op.
452 typedef __fastcall
int (*fcall1
)(REGARGS1(uint32_t));
453 typedef __fastcall
int (*fcall2
)(REGARGS2(uint32_t, uint32_t));
454 typedef __fastcall
int (*fcall3
)(REGARGS2(uint32_t, uint32_t), uint32_t);
456 static __inline
uint32_t
457 fastcall1(fcall1 f
, uint32_t a
)
459 return(f(REGCALL1(a
)));
462 static __inline
uint32_t
463 fastcall2(fcall2 f
, uint32_t a
, uint32_t b
)
465 return(f(REGCALL2(a
, b
)));
468 static __inline
uint32_t
469 fastcall3(fcall3 f
, uint32_t a
, uint32_t b
, uint32_t c
)
471 return(f(REGCALL2(a
, b
), c
));
474 #define FASTCALL1(f, a) \
475 fastcall1((fcall1)(f), (uint32_t)(a))
476 #define FASTCALL2(f, a, b) \
477 fastcall2((fcall2)(f), (uint32_t)(a), (uint32_t)(b))
478 #define FASTCALL3(f, a, b, c) \
479 fastcall3((fcall3)(f), (uint32_t)(a), (uint32_t)(b), (uint32_t)(c))
481 #define FASTCALL1(f, a) (f)((a))
482 #define FASTCALL2(f, a, b) (f)((a), (b))
483 #define FASTCALL3(f, a, b, c) (f)((a), (b), (c))
484 #endif /* __i386__ */
488 * AMD64 support. Microsoft uses a different calling convention
489 * than everyone else on the amd64 platform. Sadly, gcc has no
490 * built-in support for it (yet).
492 * The three major differences we're concerned with are:
494 * - The first 4 register-sized arguments are passed in the
495 * %rcx, %rdx, %r8 and %r9 registers, and the rest are pushed
496 * onto the stack. (The ELF ABI uses 6 registers, not 4).
498 * - The caller must reserve space on the stack for the 4
499 * register arguments in case the callee has to spill them.
501 * - The stack myst be 16-byte aligned by the time the callee
502 * executes. A call instruction implicitly pushes an 8 byte
503 * return address onto the stack. We have to make sure that
504 * the amount of space we consume, plus the return address,
505 * is a multiple of 16 bytes in size. This means that in
506 * some cases, we may need to chew up an extra 8 bytes on
507 * the stack that will be unused.
509 * On the bright side, Microsoft seems to be using just the one
510 * calling convention for all functions on amd64, unlike x86 where
511 * they use a mix of _stdcall, _fastcall and _cdecl.
516 extern uint64_t x86_64_call1(void *, uint64_t);
517 extern uint64_t x86_64_call2(void *, uint64_t, uint64_t);
518 extern uint64_t x86_64_call3(void *, uint64_t, uint64_t, uint64_t);
519 extern uint64_t x86_64_call4(void *, uint64_t, uint64_t, uint64_t, uint64_t);
520 extern uint64_t x86_64_call5(void *, uint64_t, uint64_t, uint64_t, uint64_t,
522 extern uint64_t x86_64_call6(void *, uint64_t, uint64_t, uint64_t, uint64_t,
526 #define MSCALL1(fn, a) \
527 x86_64_call1((fn), (uint64_t)(a))
528 #define MSCALL2(fn, a, b) \
529 x86_64_call2((fn), (uint64_t)(a), (uint64_t)(b))
530 #define MSCALL3(fn, a, b, c) \
531 x86_64_call3((fn), (uint64_t)(a), (uint64_t)(b), \
533 #define MSCALL4(fn, a, b, c, d) \
534 x86_64_call4((fn), (uint64_t)(a), (uint64_t)(b), \
535 (uint64_t)(c), (uint64_t)(d))
536 #define MSCALL5(fn, a, b, c, d, e) \
537 x86_64_call5((fn), (uint64_t)(a), (uint64_t)(b), \
538 (uint64_t)(c), (uint64_t)(d), (uint64_t)(e))
539 #define MSCALL6(fn, a, b, c, d, e, f) \
540 x86_64_call6((fn), (uint64_t)(a), (uint64_t)(b), \
541 (uint64_t)(c), (uint64_t)(d), (uint64_t)(e), (uint64_t)(f))
543 #else /* __amd64__ */
545 #define MSCALL1(fn, a) (fn)((a))
546 #define MSCALL2(fn, a, b) (fn)((a), (b))
547 #define MSCALL3(fn, a, b, c) (fn)((a), (b), (c))
548 #define MSCALL4(fn, a, b, c, d) (fn)((a), (b), (c), (d))
549 #define MSCALL5(fn, a, b, c, d, e) (fn)((a), (b), (c), (d), (e))
550 #define MSCALL6(fn, a, b, c, d, e, f) (fn)((a), (b), (c), (d), (e), (f))
552 #endif /* __amd64__ */
555 #define FUNC void(*)(void)
556 #define IMPORT_FUNC(x) { #x, (FUNC)x, NULL }
557 #define IMPORT_FUNC_MAP(x, y) { #x, (FUNC)y, NULL }
560 extern int pe_get_dos_header(vm_offset_t
, image_dos_header
*);
561 extern int pe_is_nt_image(vm_offset_t
);
562 extern int pe_get_optional_header(vm_offset_t
, image_optional_header
*);
563 extern int pe_get_file_header(vm_offset_t
, image_file_header
*);
564 extern int pe_get_section_header(vm_offset_t
, image_section_header
*);
565 extern int pe_numsections(vm_offset_t
);
566 extern vm_offset_t
pe_imagebase(vm_offset_t
);
567 extern vm_offset_t
pe_directory_offset(vm_offset_t
, uint32_t);
568 extern vm_offset_t
pe_translate_addr (vm_offset_t
, vm_offset_t
);
569 extern int pe_get_section(vm_offset_t
, image_section_header
*, const char *);
570 extern int pe_relocate(vm_offset_t
);
571 extern int pe_get_import_descriptor(vm_offset_t
, image_import_descriptor
*, const char *);
572 extern int pe_patch_imports(vm_offset_t
, const char *, image_patch_table
*);
573 extern int pe_get_messagetable(vm_offset_t
, message_resource_data
**);
574 extern int pe_get_message(vm_offset_t
, uint32_t, char **, int *, uint16_t *);
577 #endif /* _PE_VAR_H_ */