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