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: releng/11.0/sys/compat/ndis/pe_var.h 268351 2014-07-07 00:27:09Z marcel $ 33123474Swpaul */ 34123474Swpaul 35123474Swpaul#ifndef _PE_VAR_H_ 36189488Sweongyo#define _PE_VAR_H_ 37123474Swpaul 38123474Swpaul/* 39123474Swpaul * Image Format 40123474Swpaul */ 41123474Swpaul 42189488Sweongyo#define IMAGE_DOS_SIGNATURE 0x5A4D /* MZ */ 43189488Sweongyo#define IMAGE_OS2_SIGNATURE 0x454E /* NE */ 44189488Sweongyo#define IMAGE_OS2_SIGNATURE_LE 0x454C /* LE */ 45189488Sweongyo#define IMAGE_VXD_SIGNATURE 0x454C /* LE */ 46189488Sweongyo#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 */ 87189488Sweongyo 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 96189488Sweongyo#define IMAGE_FILE_MACHINE_UNKNOWN 0 97189488Sweongyo#define IMAGE_FILE_MACHINE_I860 0x014d 98189488Sweongyo#define IMAGE_FILE_MACHINE_I386 0x014c 99189488Sweongyo#define IMAGE_FILE_MACHINE_R3000 0x0162 100189488Sweongyo#define IMAGE_FILE_MACHINE_R4000 0x0166 101189488Sweongyo#define IMAGE_FILE_MACHINE_R10000 0x0168 102189488Sweongyo#define IMAGE_FILE_MACHINE_WCEMIPSV2 0x0169 103189488Sweongyo#define IMAGE_FILE_MACHINE_ALPHA 0x0184 104189488Sweongyo#define IMAGE_FILE_MACHINE_SH3 0x01a2 105189488Sweongyo#define IMAGE_FILE_MACHINE_SH3DSP 0x01a3 106189488Sweongyo#define IMAGE_FILE_MACHINE_SH3E 0x01a4 107189488Sweongyo#define IMAGE_FILE_MACHINE_SH4 0x01a6 108189488Sweongyo#define IMAGE_FILE_MACHINE_SH5 0x01a8 109189488Sweongyo#define IMAGE_FILE_MACHINE_ARM 0x01c0 110189488Sweongyo#define IMAGE_FILE_MACHINE_THUMB 0x01c2 111189488Sweongyo#define IMAGE_FILE_MACHINE_AM33 0x01d3 112189488Sweongyo#define IMAGE_FILE_MACHINE_POWERPC 0x01f0 113189488Sweongyo#define IMAGE_FILE_MACHINE_POWERPCFP 0x01f1 114189488Sweongyo#define IMAGE_FILE_MACHINE_MIPS16 0x0266 115189488Sweongyo#define IMAGE_FILE_MACHINE_ALPHA64 0x0284 116189488Sweongyo#define IMAGE_FILE_MACHINE_MIPSFPU 0x0366 117189488Sweongyo#define IMAGE_FILE_MACHINE_MIPSFPU16 0x0466 118189488Sweongyo#define IMAGE_FILE_MACHINE_AXP64 IMAGE_FILE_MACHINE_ALPHA64 119189488Sweongyo#define IMAGE_FILE_MACHINE_TRICORE 0x0520 120189488Sweongyo#define IMAGE_FILE_MACHINE_CEF 0x0cef 121189488Sweongyo#define IMAGE_FILE_MACHINE_EBC 0x0ebc 122189488Sweongyo#define IMAGE_FILE_MACHINE_AMD64 0x8664 123189488Sweongyo#define IMAGE_FILE_MACHINE_M32R 0x9041 124189488Sweongyo#define IMAGE_FILE_MACHINE_CEE 0xc0ee 125123474Swpaul 126123474Swpaul/* Characteristics */ 127123474Swpaul 128189488Sweongyo#define IMAGE_FILE_RELOCS_STRIPPED 0x0001 /* No relocation info */ 129189488Sweongyo#define IMAGE_FILE_EXECUTABLE_IMAGE 0x0002 130189488Sweongyo#define IMAGE_FILE_LINE_NUMS_STRIPPED 0x0004 131189488Sweongyo#define IMAGE_FILE_LOCAL_SYMS_STRIPPED 0x0008 132189488Sweongyo#define IMAGE_FILE_AGGRESIVE_WS_TRIM 0x0010 133189488Sweongyo#define IMAGE_FILE_LARGE_ADDRESS_AWARE 0x0020 134189488Sweongyo#define IMAGE_FILE_16BIT_MACHINE 0x0040 135189488Sweongyo#define IMAGE_FILE_BYTES_REVERSED_LO 0x0080 136189488Sweongyo#define IMAGE_FILE_32BIT_MACHINE 0x0100 137189488Sweongyo#define IMAGE_FILE_DEBUG_STRIPPED 0x0200 138189488Sweongyo#define IMAGE_FILE_REMOVABLE_RUN_FROM_SWAP 0x0400 139189488Sweongyo#define IMAGE_FILE_NET_RUN_FROM_SWAP 0x0800 140189488Sweongyo#define IMAGE_FILE_SYSTEM 0x1000 141189488Sweongyo#define IMAGE_FILE_DLL 0x2000 142189488Sweongyo#define IMAGE_FILE_UP_SYSTEM_ONLY 0x4000 143189488Sweongyo#define IMAGE_FILE_BYTES_REVERSED_HI 0x8000 144123474Swpaul 145189488Sweongyo#define IMAGE_SIZEOF_FILE_HEADER 20 146123474Swpaul 147123474Swpaul/* 148123474Swpaul * Directory format. 149123474Swpaul */ 150123474Swpaul 151123474Swpaulstruct image_data_directory { 152123474Swpaul uint32_t idd_vaddr; /* virtual address */ 153123474Swpaul uint32_t idd_size; /* size */ 154123474Swpaul}; 155123474Swpaul 156123474Swpaultypedef struct image_data_directory image_data_directory; 157123474Swpaul 158189488Sweongyo#define IMAGE_DIRECTORY_ENTRIES_MAX 16 159123474Swpaul 160123474Swpaul/* 161123474Swpaul * Optional header format. 162123474Swpaul */ 163123474Swpaul 164123474Swpaulstruct image_optional_header { 165123474Swpaul 166123474Swpaul /* Standard fields */ 167123474Swpaul 168123474Swpaul uint16_t ioh_magic; 169123474Swpaul uint8_t ioh_linkerver_major; 170123474Swpaul uint8_t ioh_linkerver_minor; 171123474Swpaul uint32_t ioh_codesize; 172123474Swpaul uint32_t ioh_datasize; 173123474Swpaul uint32_t ioh_bsssize; 174123474Swpaul uint32_t ioh_entryaddr; 175123474Swpaul uint32_t ioh_codebaseaddr; 176141963Swpaul#ifndef __amd64__ 177123474Swpaul uint32_t ioh_databaseaddr; 178141963Swpaul#endif 179123474Swpaul 180123474Swpaul /* NT-specific fields */ 181123474Swpaul 182141963Swpaul uintptr_t ioh_imagebase; 183123474Swpaul uint32_t ioh_sectalign; 184123474Swpaul uint32_t ioh_filealign; 185123474Swpaul uint16_t ioh_osver_major; 186123474Swpaul uint16_t ioh_osver_minor; 187123474Swpaul uint16_t ioh_imagever_major; 188123474Swpaul uint16_t ioh_imagever_minor; 189123474Swpaul uint16_t ioh_subsys_major; 190123474Swpaul uint16_t ioh_subsys_minor; 191123474Swpaul uint32_t ioh_win32ver; 192123474Swpaul uint32_t ioh_imagesize; 193123474Swpaul uint32_t ioh_headersize; 194123474Swpaul uint32_t ioh_csum; 195123474Swpaul uint16_t ioh_subsys; 196123474Swpaul uint16_t ioh_dll_characteristics; 197141963Swpaul uintptr_t ioh_stackreservesize; 198141963Swpaul uintptr_t ioh_stackcommitsize; 199141963Swpaul uintptr_t ioh_heapreservesize; 200141963Swpaul uintptr_t ioh_heapcommitsize; 201123474Swpaul uint16_t ioh_loaderflags; 202123474Swpaul uint32_t ioh_rva_size_cnt; 203123474Swpaul image_data_directory ioh_datadir[IMAGE_DIRECTORY_ENTRIES_MAX]; 204123474Swpaul}; 205123474Swpaul 206123474Swpaultypedef struct image_optional_header image_optional_header; 207123474Swpaul 208123474Swpaulstruct image_nt_header { 209123474Swpaul uint32_t inh_signature; 210123474Swpaul image_file_header inh_filehdr; 211123474Swpaul image_optional_header inh_optionalhdr; 212123474Swpaul}; 213123474Swpaul 214123474Swpaultypedef struct image_nt_header image_nt_header; 215123474Swpaul 216151703Swpaul#define IMAGE_SIZEOF_NT_HEADER(nthdr) \ 217151703Swpaul (offsetof(image_nt_header, inh_optionalhdr) + \ 218151703Swpaul ((image_nt_header *)(nthdr))->inh_filehdr.ifh_optionalhdrlen) 219151703Swpaul 220123474Swpaul/* Directory Entries */ 221123474Swpaul 222189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_EXPORT 0 /* Export Directory */ 223189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_IMPORT 1 /* Import Directory */ 224189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_RESOURCE 2 /* Resource Directory */ 225189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_EXCEPTION 3 /* Exception Directory */ 226189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_SECURITY 4 /* Security Directory */ 227189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_BASERELOC 5 /* Base Relocation Table */ 228189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_DEBUG 6 /* Debug Directory */ 229189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_COPYRIGHT 7 /* Description String */ 230189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_GLOBALPTR 8 /* Machine Value (MIPS GP) */ 231189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_TLS 9 /* TLS Directory */ 232189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_LOAD_CONFIG 10 /* Load Configuration Directory */ 233189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT 11 /* Bound Import Directory in headers */ 234189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_IAT 12 /* Import Address Table */ 235189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_DELAY_IMPORT 13 236189488Sweongyo#define IMAGE_DIRECTORY_ENTRY_COM_DESCRIPTOR 14 237123474Swpaul 238124165Swpaul/* Resource types */ 239124165Swpaul 240189488Sweongyo#define RT_CURSOR 1 241189488Sweongyo#define RT_BITMAP 2 242189488Sweongyo#define RT_ICON 3 243189488Sweongyo#define RT_MENU 4 244189488Sweongyo#define RT_DIALOG 5 245189488Sweongyo#define RT_STRING 6 246189488Sweongyo#define RT_FONTDIR 7 247189488Sweongyo#define RT_FONT 8 248189488Sweongyo#define RT_ACCELERATOR 9 249189488Sweongyo#define RT_RCDATA 10 250189488Sweongyo#define RT_MESSAGETABLE 11 251189488Sweongyo#define RT_GROUP_CURSOR 12 252189488Sweongyo#define RT_GROUP_ICON 14 253189488Sweongyo#define RT_VERSION 16 254189488Sweongyo#define RT_DLGINCLUDE 17 255189488Sweongyo#define RT_PLUGPLAY 19 256189488Sweongyo#define RT_VXD 20 257189488Sweongyo#define RT_ANICURSOR 21 258189488Sweongyo#define RT_ANIICON 22 259189488Sweongyo#define RT_HTML 23 260124165Swpaul 261123474Swpaul/* 262123474Swpaul * Section header format. 263123474Swpaul */ 264123474Swpaul 265189488Sweongyo#define IMAGE_SHORT_NAME_LEN 8 266123474Swpaul 267123474Swpaulstruct image_section_header { 268123474Swpaul uint8_t ish_name[IMAGE_SHORT_NAME_LEN]; 269123474Swpaul union { 270123474Swpaul uint32_t ish_paddr; 271123474Swpaul uint32_t ish_vsize; 272123474Swpaul } ish_misc; 273123474Swpaul uint32_t ish_vaddr; 274123474Swpaul uint32_t ish_rawdatasize; 275123474Swpaul uint32_t ish_rawdataaddr; 276123474Swpaul uint32_t ish_relocaddr; 277123474Swpaul uint32_t ish_linenumaddr; 278123474Swpaul uint16_t ish_numrelocs; 279123474Swpaul uint16_t ish_numlinenums; 280123474Swpaul uint32_t ish_characteristics; 281123474Swpaul}; 282123474Swpaul 283123474Swpaultypedef struct image_section_header image_section_header; 284123474Swpaul 285189488Sweongyo#define IMAGE_SIZEOF_SECTION_HEADER 40 286123474Swpaul 287189488Sweongyo#define IMAGE_FIRST_SECTION(nthdr) \ 288151703Swpaul ((image_section_header *)((vm_offset_t)(nthdr) + \ 289151703Swpaul offsetof(image_nt_header, inh_optionalhdr) + \ 290151703Swpaul ((image_nt_header *)(nthdr))->inh_filehdr.ifh_optionalhdrlen)) 291151703Swpaul 292123474Swpaul/* 293123474Swpaul * Import format 294123474Swpaul */ 295123474Swpaul 296123474Swpaulstruct image_import_by_name { 297123474Swpaul uint16_t iibn_hint; 298142037Swpaul uint8_t iibn_name[1]; 299123474Swpaul}; 300123474Swpaul 301189488Sweongyo#define IMAGE_ORDINAL_FLAG 0x80000000 302189488Sweongyo#define IMAGE_ORDINAL(Ordinal) (Ordinal & 0xffff) 303123474Swpaul 304123474Swpaulstruct image_import_descriptor { 305123474Swpaul uint32_t iid_import_name_table_addr; 306123474Swpaul uint32_t iid_timestamp; 307123474Swpaul uint32_t iid_forwardchain; 308123474Swpaul uint32_t iid_nameaddr; 309123474Swpaul uint32_t iid_import_address_table_addr; 310123474Swpaul}; 311123474Swpaul 312123474Swpaultypedef struct image_import_descriptor image_import_descriptor; 313123474Swpaul 314123474Swpaulstruct image_base_reloc { 315123474Swpaul uint32_t ibr_vaddr; 316123474Swpaul uint32_t ibr_blocksize; 317123474Swpaul uint16_t ibr_rel[1]; 318123474Swpaul}; 319123474Swpaul 320123474Swpaultypedef struct image_base_reloc image_base_reloc; 321123474Swpaul 322189488Sweongyo#define IMR_RELTYPE(x) ((x >> 12) & 0xF) 323189488Sweongyo#define IMR_RELOFFSET(x) (x & 0xFFF) 324123474Swpaul 325123474Swpaul/* generic relocation types */ 326189488Sweongyo#define IMAGE_REL_BASED_ABSOLUTE 0 327189488Sweongyo#define IMAGE_REL_BASED_HIGH 1 328189488Sweongyo#define IMAGE_REL_BASED_LOW 2 329189488Sweongyo#define IMAGE_REL_BASED_HIGHLOW 3 330189488Sweongyo#define IMAGE_REL_BASED_HIGHADJ 4 331189488Sweongyo#define IMAGE_REL_BASED_MIPS_JMPADDR 5 332189488Sweongyo#define IMAGE_REL_BASED_SECTION 6 333189488Sweongyo#define IMAGE_REL_BASED_REL 7 334189488Sweongyo#define IMAGE_REL_BASED_MIPS_JMPADDR16 9 335189488Sweongyo#define IMAGE_REL_BASED_DIR64 10 336189488Sweongyo#define IMAGE_REL_BASED_HIGH3ADJ 11 337123474Swpaul 338124165Swpaulstruct image_resource_directory_entry { 339124165Swpaul uint32_t irde_name; 340124165Swpaul uint32_t irde_dataoff; 341124165Swpaul}; 342123474Swpaul 343124165Swpaultypedef struct image_resource_directory_entry image_resource_directory_entry; 344124165Swpaul 345189488Sweongyo#define RESOURCE_NAME_STR 0x80000000 346189488Sweongyo#define RESOURCE_DIR_FLAG 0x80000000 347124165Swpaul 348124165Swpaulstruct image_resource_directory { 349124165Swpaul uint32_t ird_characteristics; 350124165Swpaul uint32_t ird_timestamp; 351124165Swpaul uint16_t ird_majorver; 352124165Swpaul uint16_t ird_minorver; 353124165Swpaul uint16_t ird_named_entries; 354124165Swpaul uint16_t ird_id_entries; 355124165Swpaul#ifdef notdef 356124165Swpaul image_resource_directory_entry ird_entries[1]; 357124165Swpaul#endif 358124165Swpaul}; 359124165Swpaul 360124165Swpaultypedef struct image_resource_directory image_resource_directory; 361124165Swpaul 362124165Swpaulstruct image_resource_directory_string { 363124165Swpaul uint16_t irds_len; 364124165Swpaul char irds_name[1]; 365124165Swpaul}; 366124165Swpaul 367124165Swpaultypedef struct image_resource_directory_string image_resource_directory_string; 368124165Swpaul 369124165Swpaulstruct image_resource_directory_string_u { 370124165Swpaul uint16_t irds_len; 371124165Swpaul char irds_name[1]; 372124165Swpaul}; 373124165Swpaul 374124165Swpaultypedef struct image_resource_directory_string_u 375124165Swpaul image_resource_directory_string_u; 376124165Swpaul 377124165Swpaulstruct image_resource_data_entry { 378124165Swpaul uint32_t irde_offset; 379124165Swpaul uint32_t irde_size; 380124165Swpaul uint32_t irde_codepage; 381124165Swpaul uint32_t irde_rsvd; 382124165Swpaul}; 383124165Swpaul 384124165Swpaultypedef struct image_resource_data_entry image_resource_data_entry; 385124165Swpaul 386124165Swpaulstruct message_resource_data { 387124165Swpaul uint32_t mrd_numblocks; 388124165Swpaul#ifdef notdef 389124165Swpaul message_resource_block mrd_blocks[1]; 390124165Swpaul#endif 391124165Swpaul}; 392124165Swpaul 393124165Swpaultypedef struct message_resource_data message_resource_data; 394124165Swpaul 395124165Swpaulstruct message_resource_block { 396124165Swpaul uint32_t mrb_lowid; 397124165Swpaul uint32_t mrb_highid; 398124165Swpaul uint32_t mrb_entryoff; 399124165Swpaul}; 400124165Swpaul 401124165Swpaultypedef struct message_resource_block message_resource_block; 402124165Swpaul 403124165Swpaulstruct message_resource_entry { 404124165Swpaul uint16_t mre_len; 405124165Swpaul uint16_t mre_flags; 406124165Swpaul char mre_text[]; 407124165Swpaul}; 408124165Swpaul 409124165Swpaultypedef struct message_resource_entry message_resource_entry; 410124165Swpaul 411189488Sweongyo#define MESSAGE_RESOURCE_UNICODE 0x0001 412124165Swpaul 413123474Swpaulstruct image_patch_table { 414123474Swpaul char *ipt_name; 415123474Swpaul void (*ipt_func)(void); 416141963Swpaul void (*ipt_wrap)(void); 417144888Swpaul int ipt_argcnt; 418144888Swpaul int ipt_ftype; 419123474Swpaul}; 420123474Swpaul 421123474Swpaultypedef struct image_patch_table image_patch_table; 422123474Swpaul 423124504Sobrien/* 424141963Swpaul * AMD64 support. Microsoft uses a different calling convention 425141963Swpaul * than everyone else on the amd64 platform. Sadly, gcc has no 426141963Swpaul * built-in support for it (yet). 427141963Swpaul * 428141963Swpaul * The three major differences we're concerned with are: 429141963Swpaul * 430141963Swpaul * - The first 4 register-sized arguments are passed in the 431141963Swpaul * %rcx, %rdx, %r8 and %r9 registers, and the rest are pushed 432141963Swpaul * onto the stack. (The ELF ABI uses 6 registers, not 4). 433141963Swpaul * 434141963Swpaul * - The caller must reserve space on the stack for the 4 435141963Swpaul * register arguments in case the callee has to spill them. 436141963Swpaul * 437141963Swpaul * - The stack myst be 16-byte aligned by the time the callee 438141963Swpaul * executes. A call instruction implicitly pushes an 8 byte 439141963Swpaul * return address onto the stack. We have to make sure that 440141963Swpaul * the amount of space we consume, plus the return address, 441141963Swpaul * is a multiple of 16 bytes in size. This means that in 442141963Swpaul * some cases, we may need to chew up an extra 8 bytes on 443141963Swpaul * the stack that will be unused. 444141963Swpaul * 445141963Swpaul * On the bright side, Microsoft seems to be using just the one 446141963Swpaul * calling convention for all functions on amd64, unlike x86 where 447141963Swpaul * they use a mix of _stdcall, _fastcall and _cdecl. 448141963Swpaul */ 449141963Swpaul 450141963Swpaul#ifdef __amd64__ 451141963Swpaul 452141963Swpaulextern uint64_t x86_64_call1(void *, uint64_t); 453141963Swpaulextern uint64_t x86_64_call2(void *, uint64_t, uint64_t); 454141963Swpaulextern uint64_t x86_64_call3(void *, uint64_t, uint64_t, uint64_t); 455141963Swpaulextern uint64_t x86_64_call4(void *, uint64_t, uint64_t, uint64_t, uint64_t); 456141963Swpaulextern uint64_t x86_64_call5(void *, uint64_t, uint64_t, uint64_t, uint64_t, 457141963Swpaul uint64_t); 458141963Swpaulextern uint64_t x86_64_call6(void *, uint64_t, uint64_t, uint64_t, uint64_t, 459141963Swpaul uint64_t, uint64_t); 460141963Swpaul 461141963Swpaul 462189488Sweongyo#define MSCALL1(fn, a) \ 463141963Swpaul x86_64_call1((fn), (uint64_t)(a)) 464189488Sweongyo#define MSCALL2(fn, a, b) \ 465141963Swpaul x86_64_call2((fn), (uint64_t)(a), (uint64_t)(b)) 466189488Sweongyo#define MSCALL3(fn, a, b, c) \ 467141963Swpaul x86_64_call3((fn), (uint64_t)(a), (uint64_t)(b), \ 468141963Swpaul (uint64_t)(c)) 469189488Sweongyo#define MSCALL4(fn, a, b, c, d) \ 470141963Swpaul x86_64_call4((fn), (uint64_t)(a), (uint64_t)(b), \ 471141963Swpaul (uint64_t)(c), (uint64_t)(d)) 472189488Sweongyo#define MSCALL5(fn, a, b, c, d, e) \ 473141963Swpaul x86_64_call5((fn), (uint64_t)(a), (uint64_t)(b), \ 474141963Swpaul (uint64_t)(c), (uint64_t)(d), (uint64_t)(e)) 475189488Sweongyo#define MSCALL6(fn, a, b, c, d, e, f) \ 476141963Swpaul x86_64_call6((fn), (uint64_t)(a), (uint64_t)(b), \ 477141963Swpaul (uint64_t)(c), (uint64_t)(d), (uint64_t)(e), (uint64_t)(f)) 478141963Swpaul 479144888Swpaul#endif /* __amd64__ */ 480141963Swpaul 481144888Swpaul#ifdef __i386__ 482141963Swpaul 483144888Swpaulextern uint32_t x86_stdcall_call(void *, int, ...); 484141963Swpaul 485189488Sweongyo#define MSCALL1(fn, a) x86_stdcall_call(fn, 1, (a)) 486189488Sweongyo#define MSCALL2(fn, a, b) x86_stdcall_call(fn, 2, (a), (b)) 487189488Sweongyo#define MSCALL3(fn, a, b, c) x86_stdcall_call(fn, 3, (a), (b), (c)) 488189488Sweongyo#define MSCALL4(fn, a, b, c, d) x86_stdcall_call(fn, 4, (a), (b), (c), (d)) 489189488Sweongyo#define MSCALL5(fn, a, b, c, d, e) \ 490144888Swpaul x86_stdcall_call(fn, 5, (a), (b), (c), (d), (e)) 491189488Sweongyo#define MSCALL6(fn, a, b, c, d, e, f) \ 492144888Swpaul x86_stdcall_call(fn, 6, (a), (b), (c), (d), (e), (f)) 493141963Swpaul 494144888Swpaul#endif /* __i386__ */ 495144888Swpaul 496144888Swpaul 497189488Sweongyo#define FUNC void(*)(void) 498140751Swpaul 499144888Swpaul#ifdef __i386__ 500189488Sweongyo#define IMPORT_SFUNC(x, y) { #x, (FUNC)x, NULL, y, WINDRV_WRAP_STDCALL } 501189488Sweongyo#define IMPORT_SFUNC_MAP(x, y, z) \ 502144888Swpaul { #x, (FUNC)y, NULL, z, WINDRV_WRAP_STDCALL } 503189488Sweongyo#define IMPORT_FFUNC(x, y) { #x, (FUNC)x, NULL, y, WINDRV_WRAP_FASTCALL } 504189488Sweongyo#define IMPORT_FFUNC_MAP(x, y, z) \ 505144888Swpaul { #x, (FUNC)y, NULL, z, WINDRV_WRAP_FASTCALL } 506189488Sweongyo#define IMPORT_RFUNC(x, y) { #x, (FUNC)x, NULL, y, WINDRV_WRAP_REGPARM } 507189488Sweongyo#define IMPORT_RFUNC_MAP(x, y, z) \ 508144888Swpaul { #x, (FUNC)y, NULL, z, WINDRV_WRAP_REGPARM } 509189488Sweongyo#define IMPORT_CFUNC(x, y) { #x, (FUNC)x, NULL, y, WINDRV_WRAP_CDECL } 510189488Sweongyo#define IMPORT_CFUNC_MAP(x, y, z) \ 511144888Swpaul { #x, (FUNC)y, NULL, z, WINDRV_WRAP_CDECL } 512144888Swpaul#endif /* __i386__ */ 513144888Swpaul 514144888Swpaul#ifdef __amd64__ 515189488Sweongyo#define IMPORT_SFUNC(x, y) { #x, (FUNC)x, NULL, y, WINDRV_WRAP_AMD64 } 516189488Sweongyo#define IMPORT_SFUNC_MAP(x, y, z) \ 517144888Swpaul { #x, (FUNC)y, NULL, z, WINDRV_WRAP_AMD64 } 518189488Sweongyo#define IMPORT_FFUNC(x, y) { #x, (FUNC)x, NULL, y, WINDRV_WRAP_AMD64 } 519189488Sweongyo#define IMPORT_FFUNC_MAP(x, y, z) \ 520144888Swpaul { #x, (FUNC)y, NULL, z, WINDRV_WRAP_AMD64 } 521189488Sweongyo#define IMPORT_RFUNC(x, y) { #x, (FUNC)x, NULL, y, WINDRV_WRAP_AMD64 } 522189488Sweongyo#define IMPORT_RFUNC_MAP(x, y, z) \ 523144888Swpaul { #x, (FUNC)y, NULL, z, WINDRV_WRAP_AMD64 } 524189488Sweongyo#define IMPORT_CFUNC(x, y) { #x, (FUNC)x, NULL, y, WINDRV_WRAP_AMD64 } 525189488Sweongyo#define IMPORT_CFUNC_MAP(x, y, z) \ 526144888Swpaul { #x, (FUNC)y, NULL, z, WINDRV_WRAP_AMD64 } 527144888Swpaul#endif /* __amd64__ */ 528144888Swpaul 529123474Swpaul__BEGIN_DECLS 530123474Swpaulextern int pe_get_dos_header(vm_offset_t, image_dos_header *); 531123474Swpaulextern int pe_is_nt_image(vm_offset_t); 532123474Swpaulextern int pe_get_optional_header(vm_offset_t, image_optional_header *); 533123474Swpaulextern int pe_get_file_header(vm_offset_t, image_file_header *); 534123474Swpaulextern int pe_get_section_header(vm_offset_t, image_section_header *); 535123474Swpaulextern int pe_numsections(vm_offset_t); 536123474Swpaulextern vm_offset_t pe_imagebase(vm_offset_t); 537123474Swpaulextern vm_offset_t pe_directory_offset(vm_offset_t, uint32_t); 538141963Swpaulextern vm_offset_t pe_translate_addr (vm_offset_t, vm_offset_t); 539123474Swpaulextern int pe_get_section(vm_offset_t, image_section_header *, const char *); 540123474Swpaulextern int pe_relocate(vm_offset_t); 541123474Swpaulextern int pe_get_import_descriptor(vm_offset_t, image_import_descriptor *, char *); 542123474Swpaulextern int pe_patch_imports(vm_offset_t, char *, image_patch_table *); 543124173Swpaulextern int pe_get_messagetable(vm_offset_t, message_resource_data **); 544124173Swpaulextern int pe_get_message(vm_offset_t, uint32_t, char **, int *, uint16_t *); 545123474Swpaul__END_DECLS 546123474Swpaul 547123474Swpaul#endif /* _PE_VAR_H_ */ 548