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