svn path=/trunk/mcs/; revision=104772
[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 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
163         guint32 name_offset:31;
164         guint32 name_is_string:1;
165 #else
166         guint32 name_is_string:1;
167         guint32 name_offset:31;
168 #endif
169         
170         /* If the MSB is set, then the other 31 bits store the RVA of
171          * another subdirectory.  Otherwise, the other 31 bits store
172          * the RVA of the resource data entry leaf node.
173          */
174 #if G_BYTE_ORDER == G_LITTLE_ENDIAN
175         guint32 dir_offset:31;
176         guint32 is_dir:1;
177 #else
178         guint32 is_dir:1;
179         guint32 dir_offset:31;
180 #endif
181 } MonoPEResourceDirEntry;
182
183 typedef struct 
184 {
185         guint32 res_characteristics;
186         guint32 res_date_stamp;
187         guint16 res_major;
188         guint16 res_minor;
189         guint16 res_named_entries;
190         guint16 res_id_entries;
191         /* Directory entries follow on here.  The array is
192          * res_named_entries + res_id_entries long, containing all
193          * named entries first.
194          */
195 } MonoPEResourceDir;
196
197 typedef struct {
198         guint32 rva;
199         guint32 size;
200 } MonoPEDirEntry;
201
202 /* 128 bytes */
203 typedef struct {
204         MonoPEDirEntry pe_export_table;
205         MonoPEDirEntry pe_import_table;
206         MonoPEDirEntry pe_resource_table;
207         MonoPEDirEntry pe_exception_table;
208         MonoPEDirEntry pe_certificate_table;
209         MonoPEDirEntry pe_reloc_table;
210         MonoPEDirEntry pe_debug;
211         MonoPEDirEntry pe_copyright;
212         MonoPEDirEntry pe_global_ptr;
213         MonoPEDirEntry pe_tls_table;
214         MonoPEDirEntry pe_load_config_table;
215         MonoPEDirEntry pe_bound_import;
216         MonoPEDirEntry pe_iat;
217         MonoPEDirEntry pe_delay_import_desc;
218         MonoPEDirEntry pe_cli_header;
219         MonoPEDirEntry pe_reserved;
220 } MonoPEDatadir;
221
222 /* 248 bytes */
223 typedef struct {
224         char            pesig [4];
225         MonoCOFFHeader  coff;
226         MonoPEHeader    pe;
227         MonoPEHeaderNT  nt;
228         MonoPEDatadir   datadir;
229 } MonoDotNetHeader32;
230
231 /* 248 bytes */
232 typedef struct {
233         char            pesig [4];
234         MonoCOFFHeader  coff;
235         MonoPEHeader    pe;
236         MonoPEHeaderNT  nt;
237         MonoPEDatadir   datadir;
238 } MonoDotNetHeader;
239
240 /* XX248 bytes */
241 typedef struct {
242         char              pesig [4];
243         MonoCOFFHeader    coff;
244         MonoPEHeader64    pe;
245         MonoPEHeaderNT64  nt;
246         MonoPEDatadir     datadir;
247 } MonoDotNetHeader64;
248
249 #define VTFIXUP_TYPE_32BIT                            0x01
250 #define VTFIXUP_TYPE_64BIT                            0x02
251 #define VTFIXUP_TYPE_FROM_UNMANAGED                   0x04
252 #define VTFIXUP_TYPE_FROM_UNMANAGED_RETAIN_APPDOMAIN  0x08
253 #define VTFIXUP_TYPE_CALL_MOST_DERIVED                0x10
254
255 typedef struct {
256         guint32 rva;
257         guint16 count;
258         guint16 type;
259 } MonoVTableFixup;
260
261 typedef struct {
262         char    st_name [8];
263         guint32 st_virtual_size;
264         guint32 st_virtual_address;
265         guint32 st_raw_data_size;
266         guint32 st_raw_data_ptr;
267         guint32 st_reloc_ptr;
268         guint32 st_lineno_ptr;
269         guint16 st_reloc_count;
270         guint16 st_line_count;
271
272 #define SECT_FLAGS_HAS_CODE               0x20
273 #define SECT_FLAGS_HAS_INITIALIZED_DATA   0x40
274 #define SECT_FLAGS_HAS_UNINITIALIZED_DATA 0x80
275 #define SECT_FLAGS_MEM_DISCARDABLE        0x02000000
276 #define SECT_FLAGS_MEM_NOT_CACHED         0x04000000
277 #define SECT_FLAGS_MEM_NOT_PAGED          0x08000000
278 #define SECT_FLAGS_MEM_SHARED             0x10000000
279 #define SECT_FLAGS_MEM_EXECUTE            0x20000000
280 #define SECT_FLAGS_MEM_READ               0x40000000
281 #define SECT_FLAGS_MEM_WRITE              0x80000000
282         guint32 st_flags;
283
284 } MonoSectionTable;
285
286 typedef struct {
287         guint32        ch_size;
288         guint16        ch_runtime_major;
289         guint16        ch_runtime_minor;
290         MonoPEDirEntry ch_metadata;
291
292 #define CLI_FLAGS_ILONLY         0x01
293 #define CLI_FLAGS_32BITREQUIRED  0x02
294 #define CLI_FLAGS_STRONGNAMESIGNED 0x8
295 #define CLI_FLAGS_TRACKDEBUGDATA 0x00010000
296         guint32        ch_flags;
297
298         guint32        ch_entry_point;
299         MonoPEDirEntry ch_resources;
300         MonoPEDirEntry ch_strong_name;
301         MonoPEDirEntry ch_code_manager_table;
302         MonoPEDirEntry ch_vtable_fixups;
303         MonoPEDirEntry ch_export_address_table_jumps;
304
305         /* The following are zero in the current docs */
306         MonoPEDirEntry ch_eeinfo_table;
307         MonoPEDirEntry ch_helper_table;
308         MonoPEDirEntry ch_dynamic_info;
309         MonoPEDirEntry ch_delay_load_info;
310         MonoPEDirEntry ch_module_image;
311         MonoPEDirEntry ch_external_fixups;
312         MonoPEDirEntry ch_ridmap;
313         MonoPEDirEntry ch_debug_map;
314         MonoPEDirEntry ch_ip_map;
315 } MonoCLIHeader;
316
317 /* This is not an on-disk structure */
318 typedef struct {
319         MonoDotNetHeader  cli_header;
320         int               cli_section_count;
321         MonoSectionTable  *cli_section_tables;
322         void            **cli_sections;
323         MonoCLIHeader     cli_cli_header;
324 } MonoCLIImageInfo;
325
326 guint32       mono_cli_rva_image_map (MonoImage *image, guint32 rva);
327
328 #endif /* __MONO_CIL_COFF_H__ */