2008-09-12 Mark Probst <mark.probst@gmail.com>
[mono.git] / mono / metadata / cil-coff.h
1
2 #ifndef __MONO_CIL_COFF_H__
3 #define __MONO_CIL_COFF_H__
4
5 #include <mono/metadata/metadata.h>
6
7 /*
8  * 25.2.1: Method header type values
9  */
10 #define METHOD_HEADER_FORMAT_MASK   7
11 #define METHOD_HEADER_TINY_FORMAT   2
12 #define METHOD_HEADER_TINY_FORMAT1  6
13 #define METHOD_HEADER_FAT_FORMAT    3
14
15 /*
16  * 25.2.3.1: Flags for method headers
17  */
18 #define METHOD_HEADER_INIT_LOCALS   0x10
19 #define METHOD_HEADER_MORE_SECTS    0x08
20
21 /*
22  * For section data (25.3)
23  */
24 #define METHOD_HEADER_SECTION_RESERVED    0
25 #define METHOD_HEADER_SECTION_EHTABLE     1
26 #define METHOD_HEADER_SECTION_OPTIL_TABLE 2
27 #define METHOD_HEADER_SECTION_FAT_FORMAT  0x40
28 #define METHOD_HEADER_SECTION_MORE_SECTS  0x80
29
30 /* 128 bytes */
31 typedef struct {
32         char    msdos_sig [2];
33         guint16 nlast_page;
34         guint16 npages;
35         char    msdos_header [54];
36         guint32 pe_offset;
37         char    msdos_header2 [64];
38 } MonoMSDOSHeader;
39
40 /* 20 bytes */
41 typedef struct {
42         guint16  coff_machine;
43         guint16  coff_sections;
44         guint32  coff_time;
45         guint32  coff_symptr;
46         guint32  coff_symcount;
47         guint16  coff_opt_header_size;
48         guint16  coff_attributes;
49 } MonoCOFFHeader;
50
51 #define COFF_ATTRIBUTE_EXECUTABLE_IMAGE 0x0002
52 #define COFF_ATTRIBUTE_LIBRARY_IMAGE    0x2000
53
54 /* 28 bytes */
55 typedef struct {
56         guint16 pe_magic;
57         guchar  pe_major;
58         guchar  pe_minor;
59         guint32 pe_code_size;
60         guint32 pe_data_size;
61         guint32 pe_uninit_data_size;
62         guint32 pe_rva_entry_point;
63         guint32 pe_rva_code_base;
64         guint32 pe_rva_data_base;
65 } MonoPEHeader;
66
67 /* 24 bytes */
68 typedef struct {
69         guint16 pe_magic;
70         guchar  pe_major;
71         guchar  pe_minor;
72         guint32 pe_code_size;
73         guint32 pe_data_size;
74         guint32 pe_uninit_data_size;
75         guint32 pe_rva_entry_point;
76         guint32 pe_rva_code_base;
77 } MonoPEHeader64;
78
79 /* 68 bytes */
80 typedef struct {
81         guint32 pe_image_base;          /* must be 0x400000 */
82         guint32 pe_section_align;       /* must be 8192 */
83         guint32 pe_file_alignment;      /* must be 512 or 4096 */
84         guint16 pe_os_major;            /* must be 4 */
85         guint16 pe_os_minor;            /* must be 0 */
86         guint16 pe_user_major;
87         guint16 pe_user_minor;
88         guint16 pe_subsys_major;
89         guint16 pe_subsys_minor;
90         guint32 pe_reserved_1;
91         guint32 pe_image_size;
92         guint32 pe_header_size;
93         guint32 pe_checksum;
94         guint16 pe_subsys_required;
95         guint16 pe_dll_flags;
96         guint32 pe_stack_reserve;
97         guint32 pe_stack_commit;
98         guint32 pe_heap_reserve;
99         guint32 pe_heap_commit;
100         guint32 pe_loader_flags;
101         guint32 pe_data_dir_count;
102 } MonoPEHeaderNT;
103
104 /* 88 bytes */
105 typedef struct {
106         guint64 pe_image_base;
107         guint32 pe_section_align;       /* must be 8192 */
108         guint32 pe_file_alignment;      /* must be 512 or 4096 */
109         guint16 pe_os_major;            /* must be 4 */
110         guint16 pe_os_minor;            /* must be 0 */
111         guint16 pe_user_major;
112         guint16 pe_user_minor;
113         guint16 pe_subsys_major;
114         guint16 pe_subsys_minor;
115         guint32 pe_reserved_1;
116         guint32 pe_image_size;
117         guint32 pe_header_size;
118         guint32 pe_checksum;
119         guint16 pe_subsys_required;
120         guint16 pe_dll_flags;
121         guint64 pe_stack_reserve;
122         guint64 pe_stack_commit;
123         guint64 pe_heap_reserve;
124         guint64 pe_heap_commit;
125         guint32 pe_loader_flags;
126         guint32 pe_data_dir_count;
127 } MonoPEHeaderNT64;
128
129 typedef struct {
130         guint32 rde_data_offset;
131         guint32 rde_size;
132         guint32 rde_codepage;
133         guint32 rde_reserved;
134 } MonoPEResourceDataEntry;
135
136 #define MONO_PE_RESOURCE_ID_CURSOR      0x01
137 #define MONO_PE_RESOURCE_ID_BITMAP      0x02
138 #define MONO_PE_RESOURCE_ID_ICON        0x03
139 #define MONO_PE_RESOURCE_ID_MENU        0x04
140 #define MONO_PE_RESOURCE_ID_DIALOG      0x05
141 #define MONO_PE_RESOURCE_ID_STRING      0x06
142 #define MONO_PE_RESOURCE_ID_FONTDIR     0x07
143 #define MONO_PE_RESOURCE_ID_FONT        0x08
144 #define MONO_PE_RESOURCE_ID_ACCEL       0x09
145 #define MONO_PE_RESOURCE_ID_RCDATA      0x0a
146 #define MONO_PE_RESOURCE_ID_MESSAGETABLE        0x0b
147 #define MONO_PE_RESOURCE_ID_GROUP_CURSOR        0x0c
148 #define MONO_PE_RESOURCE_ID_GROUP_ICON  0x0d
149 #define MONO_PE_RESOURCE_ID_VERSION     0x10
150 #define MONO_PE_RESOURCE_ID_DLGINCLUDE  0x11
151 #define MONO_PE_RESOURCE_ID_PLUGPLAY    0x13
152 #define MONO_PE_RESOURCE_ID_VXD         0x14
153 #define MONO_PE_RESOURCE_ID_ANICURSOR   0x15
154 #define MONO_PE_RESOURCE_ID_ANIICON     0x16
155 #define MONO_PE_RESOURCE_ID_HTML        0x17
156
157 typedef struct {
158         /* If the MSB is set, then the other 31 bits store the RVA of
159          * the unicode string containing the name.  Otherwise, the
160          * other 31 bits contain the ID of this entry.
161          */
162         guint32 name;
163
164         /* If the MSB is set, then the other 31 bits store the RVA of
165          * another subdirectory.  Otherwise, the other 31 bits store
166          * the RVA of the resource data entry leaf node.
167          */
168         guint32 dir;
169 } MonoPEResourceDirEntry;
170
171 #define MONO_PE_RES_DIR_ENTRY_NAME_IS_STRING(d) (GUINT32_FROM_LE((d).name) >> 31)
172 #define MONO_PE_RES_DIR_ENTRY_NAME_OFFSET(d)    (GUINT32_FROM_LE((d).name) & 0x7fffffff)
173 #define MONO_PE_RES_DIR_ENTRY_SET_NAME(d,i,o)   ((d).name = GUINT32_TO_LE(((guint32)((i)?1:0) << 31) | ((o) & 0x7fffffff)))
174
175 #define MONO_PE_RES_DIR_ENTRY_IS_DIR(d)         (GUINT32_FROM_LE((d).dir) >> 31)
176 #define MONO_PE_RES_DIR_ENTRY_DIR_OFFSET(d)     (GUINT32_FROM_LE((d).dir) & 0x7fffffff)
177 #define MONO_PE_RES_DIR_ENTRY_SET_DIR(d,i,o)    ((d).dir = GUINT32_TO_LE(((guint32)((i)?1:0) << 31) | ((o) & 0x7fffffff)))
178
179 typedef struct 
180 {
181         guint32 res_characteristics;
182         guint32 res_date_stamp;
183         guint16 res_major;
184         guint16 res_minor;
185         guint16 res_named_entries;
186         guint16 res_id_entries;
187         /* Directory entries follow on here.  The array is
188          * res_named_entries + res_id_entries long, containing all
189          * named entries first.
190          */
191 } MonoPEResourceDir;
192
193 typedef struct {
194         guint32 rva;
195         guint32 size;
196 } MonoPEDirEntry;
197
198 /* 128 bytes */
199 typedef struct {
200         MonoPEDirEntry pe_export_table;
201         MonoPEDirEntry pe_import_table;
202         MonoPEDirEntry pe_resource_table;
203         MonoPEDirEntry pe_exception_table;
204         MonoPEDirEntry pe_certificate_table;
205         MonoPEDirEntry pe_reloc_table;
206         MonoPEDirEntry pe_debug;
207         MonoPEDirEntry pe_copyright;
208         MonoPEDirEntry pe_global_ptr;
209         MonoPEDirEntry pe_tls_table;
210         MonoPEDirEntry pe_load_config_table;
211         MonoPEDirEntry pe_bound_import;
212         MonoPEDirEntry pe_iat;
213         MonoPEDirEntry pe_delay_import_desc;
214         MonoPEDirEntry pe_cli_header;
215         MonoPEDirEntry pe_reserved;
216 } MonoPEDatadir;
217
218 /* 248 bytes */
219 typedef struct {
220         char            pesig [4];
221         MonoCOFFHeader  coff;
222         MonoPEHeader    pe;
223         MonoPEHeaderNT  nt;
224         MonoPEDatadir   datadir;
225 } MonoDotNetHeader32;
226
227 /* 248 bytes */
228 typedef struct {
229         char            pesig [4];
230         MonoCOFFHeader  coff;
231         MonoPEHeader    pe;
232         MonoPEHeaderNT  nt;
233         MonoPEDatadir   datadir;
234 } MonoDotNetHeader;
235
236 /* XX248 bytes */
237 typedef struct {
238         char              pesig [4];
239         MonoCOFFHeader    coff;
240         MonoPEHeader64    pe;
241         MonoPEHeaderNT64  nt;
242         MonoPEDatadir     datadir;
243 } MonoDotNetHeader64;
244
245 #define VTFIXUP_TYPE_32BIT                            0x01
246 #define VTFIXUP_TYPE_64BIT                            0x02
247 #define VTFIXUP_TYPE_FROM_UNMANAGED                   0x04
248 #define VTFIXUP_TYPE_FROM_UNMANAGED_RETAIN_APPDOMAIN  0x08
249 #define VTFIXUP_TYPE_CALL_MOST_DERIVED                0x10
250
251 typedef struct {
252         guint32 rva;
253         guint16 count;
254         guint16 type;
255 } MonoVTableFixup;
256
257 typedef struct {
258         char    st_name [8];
259         guint32 st_virtual_size;
260         guint32 st_virtual_address;
261         guint32 st_raw_data_size;
262         guint32 st_raw_data_ptr;
263         guint32 st_reloc_ptr;
264         guint32 st_lineno_ptr;
265         guint16 st_reloc_count;
266         guint16 st_line_count;
267
268 #define SECT_FLAGS_HAS_CODE               0x20
269 #define SECT_FLAGS_HAS_INITIALIZED_DATA   0x40
270 #define SECT_FLAGS_HAS_UNINITIALIZED_DATA 0x80
271 #define SECT_FLAGS_MEM_DISCARDABLE        0x02000000
272 #define SECT_FLAGS_MEM_NOT_CACHED         0x04000000
273 #define SECT_FLAGS_MEM_NOT_PAGED          0x08000000
274 #define SECT_FLAGS_MEM_SHARED             0x10000000
275 #define SECT_FLAGS_MEM_EXECUTE            0x20000000
276 #define SECT_FLAGS_MEM_READ               0x40000000
277 #define SECT_FLAGS_MEM_WRITE              0x80000000
278         guint32 st_flags;
279
280 } MonoSectionTable;
281
282 typedef struct {
283         guint32        ch_size;
284         guint16        ch_runtime_major;
285         guint16        ch_runtime_minor;
286         MonoPEDirEntry ch_metadata;
287
288 #define CLI_FLAGS_ILONLY         0x01
289 #define CLI_FLAGS_32BITREQUIRED  0x02
290 #define CLI_FLAGS_STRONGNAMESIGNED 0x8
291 #define CLI_FLAGS_TRACKDEBUGDATA 0x00010000
292         guint32        ch_flags;
293
294         guint32        ch_entry_point;
295         MonoPEDirEntry ch_resources;
296         MonoPEDirEntry ch_strong_name;
297         MonoPEDirEntry ch_code_manager_table;
298         MonoPEDirEntry ch_vtable_fixups;
299         MonoPEDirEntry ch_export_address_table_jumps;
300
301         /* The following are zero in the current docs */
302         MonoPEDirEntry ch_eeinfo_table;
303         MonoPEDirEntry ch_helper_table;
304         MonoPEDirEntry ch_dynamic_info;
305         MonoPEDirEntry ch_delay_load_info;
306         MonoPEDirEntry ch_module_image;
307         MonoPEDirEntry ch_external_fixups;
308         MonoPEDirEntry ch_ridmap;
309         MonoPEDirEntry ch_debug_map;
310         MonoPEDirEntry ch_ip_map;
311 } MonoCLIHeader;
312
313 /* This is not an on-disk structure */
314 typedef struct {
315         MonoDotNetHeader  cli_header;
316         int               cli_section_count;
317         MonoSectionTable  *cli_section_tables;
318         void            **cli_sections;
319         MonoCLIHeader     cli_cli_header;
320 } MonoCLIImageInfo;
321
322 guint32       mono_cli_rva_image_map (MonoImage *image, guint32 rva);
323
324 #endif /* __MONO_CIL_COFF_H__ */