1 /*
2  * w32dll-local.h -- w32dll.c internal header file
3  * Written by Andrew Church <achurch@achurch.org>
4  *
5  * This file is part of transcode, a video stream processing tool.
6  * transcode is free software, distributable under the terms of the GNU
7  * General Public License (version 2 or later).  See the file COPYING
8  * for details.
9  */
10 
11 #ifndef W32DLL_LOCAL_H
12 #define W32DLL_LOCAL_H
13 
14 /*************************************************************************/
15 
16 /* Win-PE executable file structures. */
17 
18 /************************************/
19 
20 /* DOS executable header */
21 
22 struct dos_header {
23     uint16_t signature;         /* DOS_EXE_SIGNATURE */
24     uint16_t len_bytes;         /* 1..512 */
25     uint16_t len_sectors;
26     uint16_t reloc_count;
27     uint16_t len_header;
28     uint16_t min_extra_mem;
29     uint16_t max_extra_mem;
30     uint16_t init_ss;
31     uint16_t init_sp;
32     uint16_t checksum;
33     uint16_t init_cs;
34     uint16_t init_ip;
35     uint16_t reloc_offset;
36     uint16_t overlay_num;
37     uint8_t ignore1C[0x20];     /* 0x1C..0x3B */
38     uint16_t winheader;
39     uint16_t ignore3E;
40 };
41 
42 #define DOS_EXE_SIGNATURE 0x5A4D  /* 'MZ' */
43 
44 /************************************/
45 
46 /* Win-PE executable headers (base and optional) */
47 
48 enum rva_index {
49     RVA_EXPORT = 0,
50     RVA_IMPORT,
51     RVA_RESOURCE,
52     RVA_EXCEPTION,
53     RVA_CERTIFICATE,
54     RVA_BASE_RELOC,
55     RVA_DEBUG,
56     RVA_ARCH,
57     RVA_GLOBAL_PTR,
58     RVA_TLS_TABLE,
59     RVA_LOAD_CONFIG,
60     RVA_BOUND_IMPORT,
61     RVA_IMPORT_ADDR,
62     RVA_DELAY_IMPORT,
63     RVA_MAX
64 };
65 
66 struct pe_header {
67     uint32_t signature;         /* WIN_PE_SIGNATURE */
68     uint16_t arch;              /* WIN_PE_ARCH_xxx */
69     uint16_t nsections;
70     uint32_t timestamp;
71     uint32_t sym_table_offset;
72     uint32_t nsyms;
73     uint16_t opt_header_size;
74     uint16_t flags;             /* WIN_PE_FLAG_xxx */
75 };
76 struct pe_ext_header {
77     uint16_t magic;             /* WIN_PE_OPT_MAGIC_xxx */
78     uint8_t linkver_major;
79     uint8_t linkver_minor;
80     uint32_t code_size;
81     uint32_t data_size;
82     uint32_t bss_size;
83     uint32_t entry_point;
84     uint32_t code_base;
85     uint32_t data_base;
86     uint32_t image_base;        /* Code assumes it's loaded at this address */
87     uint32_t section_align;
88     uint32_t file_align;
89     uint16_t osver_major;
90     uint16_t osver_minor;
91     uint16_t imagever_major;
92     uint16_t imagever_minor;
93     uint16_t subsysver_major;
94     uint16_t subsysver_minor;
95     uint32_t win32_ver;
96     uint32_t image_size;
97     uint32_t header_size;
98     uint32_t checksum;
99     uint16_t subsystem;
100     uint16_t dll_flags;         /* Ignored here */
101     uint32_t stack_reserve;
102     uint32_t stack_commit;
103     uint32_t heap_reserve;
104     uint32_t heap_commit;
105     uint32_t loader_flags;
106     uint32_t nrva;
107     struct {
108         uint32_t address;
109         uint32_t size;
110     } rva[RVA_MAX];
111 };
112 
113 #define WIN_PE_SIGNATURE    0x00004550  /* 'PE\0\0' */
114 
115 #define WIN_PE_ARCH_X86         0x014C  /* Ignore the lower 2 bits for this */
116 #define WIN_PE_ARCH_IA64        0x0200
117 #define WIN_PE_ARCH_X86_64      0x8664
118 
119 #define WIN_PE_FLAG_DLL         0x2000
120 
121 #define WIN_PE_OPT_MAGIC_32     0x010B  /* 32-bit code */
122 #define WIN_PE_OPT_MAGIC_64     0x020B  /* 64-bit code */
123 
124 /************************************/
125 
126 /* Section table */
127 
128 struct pe_section_header {
129     char name[8];
130     uint32_t virtsize;
131     uint32_t virtaddr;
132     uint32_t filesize;
133     uint32_t fileaddr;
134     uint32_t reloc_table;
135     uint32_t linenum_table;
136     uint16_t nrelocs;
137     uint16_t nlinenums;
138     uint32_t flags;             /* SECTION_FLAG_xxx */
139 };
140 
141 #define SECTION_FLAG_CODE   0x00000020
142 #define SECTION_FLAG_DATA   0x00000040
143 #define SECTION_FLAG_BSS    0x00000080
144 #define SECTION_FLAG_EXEC   0x20000000
145 #define SECTION_FLAG_READ   0x40000000
146 #define SECTION_FLAG_WRITE  0x80000000
147 
148 /************************************/
149 
150 /* Export directory */
151 
152 struct export_directory {
153     uint32_t flags;
154     uint32_t timestamp;
155     uint16_t version_major;
156     uint16_t version_minor;
157     uint32_t name;              /* Address of name in section */
158     uint32_t ordinal_base;
159     uint32_t nfuncs;
160     uint32_t nnames;
161     uint32_t func_table;
162     uint32_t name_table;
163     uint32_t name_ordinal_table;
164 };
165 
166 /************************************/
167 
168 /* Import directory */
169 
170 struct import_directory {
171     uint32_t import_table;
172     uint32_t timestamp;
173     uint32_t forward;
174     uint32_t module_name;
175     uint32_t import_addr_table;
176 };
177 
178 struct import_name_entry {
179     uint16_t hint;
180     char name[1];       /* As long as necessary, null-terminated */
181 };
182 
183 /*************************************************************************/
184 
185 /* Constants for the DllMain() function (entry point). */
186 #define DLL_PROCESS_DETACH      0
187 #define DLL_PROCESS_ATTACH      1
188 #define DLL_THREAD_ATTACH       2
189 #define DLL_THREAD_DETACH       3
190 
191 /* Handle value for "this module" (FIXME: assumes only one module). */
192 #define HANDLE_DEFAULT          1
193 
194 /*************************************************************************/
195 
196 /* Internal function prototypes. */
197 
198 extern void *w32dll_emu_import_by_name(const char *module,
199                                        const struct import_name_entry *name);
200 extern void *w32dll_emu_import_by_ordinal(const char *module,
201                                           uint32_t ordinal);
202 
203 /*************************************************************************/
204 
205 #endif  /* W32DLL_LOCAL_H */
206 
207 /*
208  * Local variables:
209  *   c-file-style: "stroustrup"
210  *   c-file-offsets: ((case-label . *) (statement-case-intro . *))
211  *   indent-tabs-mode: nil
212  * End:
213  *
214  * vim: expandtab shiftwidth=4:
215  */
216