Merge pull request #4444 from lateralusX/jlorenss/windows-unwind-info
[mono.git] / mono / mini / dwarfwriter.c
1 /*
2  * dwarfwriter.c: Creation of DWARF debug information
3  *
4  * Author:
5  *   Zoltan Varga (vargaz@gmail.com)
6  *
7  * (C) 2008-2009 Novell, Inc.
8  */
9
10 #include "config.h"
11 #include <mono/utils/mono-compiler.h>
12
13 #if !defined(DISABLE_AOT) && !defined(DISABLE_JIT)
14 #include "dwarfwriter.h"
15
16 #include <sys/types.h>
17 #include <ctype.h>
18 #include <string.h>
19 #ifdef HAVE_STDINT_H
20 #include <stdint.h>
21 #endif
22
23 #include <mono/metadata/mono-endian.h>
24 #include <mono/metadata/debug-internals.h>
25
26 #ifndef HOST_WIN32
27 #include <mono/utils/freebsd-elf32.h>
28 #include <mono/utils/freebsd-elf64.h>
29 #endif
30
31 #include <mono/utils/freebsd-dwarf.h>
32
33 #define DW_AT_MIPS_linkage_name 0x2007
34 #define DW_LNE_set_prologue_end 0x0a
35
36 typedef struct {
37         MonoMethod *method;
38         char *start_symbol, *end_symbol;
39         guint8 *code;
40         guint32 code_size;
41 } MethodLineNumberInfo;
42
43 struct _MonoDwarfWriter
44 {
45         MonoImageWriter *w;
46         GHashTable *class_to_die, *class_to_vtype_die, *class_to_pointer_die;
47         GHashTable *class_to_reference_die;
48         int fde_index, tdie_index, line_number_file_index, line_number_dir_index;
49         GHashTable *file_to_index, *index_to_file, *dir_to_index;
50         FILE *il_file;
51         int il_file_line_index, loclist_index;
52         GSList *cie_program;
53         FILE *fp;
54         const char *temp_prefix;
55         gboolean emit_line;
56         GSList *line_info;
57         int cur_file_index;
58 };
59
60 static void
61 emit_line_number_info (MonoDwarfWriter *w, MonoMethod *method, 
62                                            char *start_symbol, char *end_symbol,
63                                            guint8 *code, guint32 code_size,
64                                            MonoDebugMethodJitInfo *debug_info);
65
66 /*
67  * mono_dwarf_writer_create:
68  *
69  *   Create a DWARF writer object. WRITER is the underlying image writer this 
70  * writer will emit to. IL_FILE is the file where IL code will be dumped to for
71  * methods which have no line number info. It can be NULL.
72  */
73 MonoDwarfWriter*
74 mono_dwarf_writer_create (MonoImageWriter *writer, FILE *il_file, int il_file_start_line, gboolean emit_line_numbers)
75 {
76         MonoDwarfWriter *w = g_new0 (MonoDwarfWriter, 1);
77
78         w->w = writer;
79         w->il_file = il_file;
80         w->il_file_line_index = il_file_start_line;
81
82         w->emit_line = emit_line_numbers;
83
84         w->fp = mono_img_writer_get_fp (w->w);
85         w->temp_prefix = mono_img_writer_get_temp_label_prefix (w->w);
86
87         w->class_to_die = g_hash_table_new (NULL, NULL);
88         w->class_to_vtype_die = g_hash_table_new (NULL, NULL);
89         w->class_to_pointer_die = g_hash_table_new (NULL, NULL);
90         w->class_to_reference_die = g_hash_table_new (NULL, NULL);
91         w->cur_file_index = -1;
92
93         return w;
94 }
95
96 void
97 mono_dwarf_writer_destroy (MonoDwarfWriter *w)
98 {
99         g_free (w);
100 }
101
102 int
103 mono_dwarf_writer_get_il_file_line_index (MonoDwarfWriter *w)
104 {
105         return w->il_file_line_index;
106 }
107
108 /* Wrappers around the image writer functions */
109
110 static inline void
111 emit_section_change (MonoDwarfWriter *w, const char *section_name, int subsection_index)
112 {
113         mono_img_writer_emit_section_change (w->w, section_name, subsection_index);
114 }
115
116 static inline void
117 emit_push_section (MonoDwarfWriter *w, const char *section_name, int subsection)
118 {
119         mono_img_writer_emit_push_section (w->w, section_name, subsection);
120 }
121
122 static inline void
123 emit_pop_section (MonoDwarfWriter *w)
124 {
125         mono_img_writer_emit_pop_section (w->w);
126 }
127
128 static inline void
129 emit_label (MonoDwarfWriter *w, const char *name) 
130
131         mono_img_writer_emit_label (w->w, name); 
132 }
133
134 static inline void
135 emit_bytes (MonoDwarfWriter *w, const guint8* buf, int size) 
136
137         mono_img_writer_emit_bytes (w->w, buf, size); 
138 }
139
140 static inline void
141 emit_string (MonoDwarfWriter *w, const char *value) 
142
143         mono_img_writer_emit_string (w->w, value); 
144 }
145
146 static inline void
147 emit_line (MonoDwarfWriter *w) 
148
149         mono_img_writer_emit_line (w->w); 
150 }
151
152 static inline void
153 emit_alignment (MonoDwarfWriter *w, int size) 
154
155         mono_img_writer_emit_alignment (w->w, size); 
156 }
157
158 static inline void
159 emit_pointer_unaligned (MonoDwarfWriter *w, const char *target) 
160
161         mono_img_writer_emit_pointer_unaligned (w->w, target); 
162 }
163
164 static inline void
165 emit_pointer (MonoDwarfWriter *w, const char *target) 
166
167         mono_img_writer_emit_pointer (w->w, target); 
168 }
169
170 static inline void
171 emit_int16 (MonoDwarfWriter *w, int value) 
172
173         mono_img_writer_emit_int16 (w->w, value); 
174 }
175
176 static inline void
177 emit_int32 (MonoDwarfWriter *w, int value) 
178
179         mono_img_writer_emit_int32 (w->w, value); 
180 }
181
182 static inline void
183 emit_symbol_diff (MonoDwarfWriter *w, const char *end, const char* start, int offset) 
184
185         mono_img_writer_emit_symbol_diff (w->w, end, start, offset); 
186 }
187
188 static inline void
189 emit_byte (MonoDwarfWriter *w, guint8 val) 
190
191         mono_img_writer_emit_byte (w->w, val); 
192 }
193
194 static void
195 emit_escaped_string (MonoDwarfWriter *w, char *value)
196 {
197         int i, len;
198
199         len = strlen (value);
200         for (i = 0; i < len; ++i) {
201                 char c = value [i];
202                 if (!(isalnum (c))) {
203                         switch (c) {
204                         case '_':
205                         case '-':
206                         case ':':
207                         case '.':
208                         case ',':
209                         case '/':
210                         case '<':
211                         case '>':
212                         case '`':
213                         case '(':
214                         case ')':
215                         case '[':
216                         case ']':
217                                 break;
218                         default:
219                                 value [i] = '_';
220                                 break;
221                         }
222                 }
223         }
224         mono_img_writer_emit_string (w->w, value);
225 }
226
227 static G_GNUC_UNUSED void
228 emit_uleb128 (MonoDwarfWriter *w, guint32 value)
229 {
230         do {
231                 guint8 b = value & 0x7f;
232                 value >>= 7;
233                 if (value != 0) /* more bytes to come */
234                         b |= 0x80;
235                 emit_byte (w, b);
236         } while (value);
237 }
238
239 static G_GNUC_UNUSED void
240 emit_sleb128 (MonoDwarfWriter *w, gint64 value)
241 {
242         gboolean more = 1;
243         gboolean negative = (value < 0);
244         guint32 size = 64;
245         guint8 byte;
246
247         while (more) {
248                 byte = value & 0x7f;
249                 value >>= 7;
250                 /* the following is unnecessary if the
251                  * implementation of >>= uses an arithmetic rather
252                  * than logical shift for a signed left operand
253                  */
254                 if (negative)
255                         /* sign extend */
256                         value |= - ((gint64)1 <<(size - 7));
257                 /* sign bit of byte is second high order bit (0x40) */
258                 if ((value == 0 && !(byte & 0x40)) ||
259                         (value == -1 && (byte & 0x40)))
260                         more = 0;
261                 else
262                         byte |= 0x80;
263                 emit_byte (w, byte);
264         }
265 }
266
267 static G_GNUC_UNUSED void
268 encode_uleb128 (guint32 value, guint8 *buf, guint8 **endbuf)
269 {
270         guint8 *p = buf;
271
272         do {
273                 guint8 b = value & 0x7f;
274                 value >>= 7;
275                 if (value != 0) /* more bytes to come */
276                         b |= 0x80;
277                 *p ++ = b;
278         } while (value);
279
280         *endbuf = p;
281 }
282
283 static G_GNUC_UNUSED void
284 encode_sleb128 (gint32 value, guint8 *buf, guint8 **endbuf)
285 {
286         gboolean more = 1;
287         gboolean negative = (value < 0);
288         guint32 size = 32;
289         guint8 byte;
290         guint8 *p = buf;
291
292         while (more) {
293                 byte = value & 0x7f;
294                 value >>= 7;
295                 /* the following is unnecessary if the
296                  * implementation of >>= uses an arithmetic rather
297                  * than logical shift for a signed left operand
298                  */
299                 if (negative)
300                         /* sign extend */
301                         value |= - (1 <<(size - 7));
302                 /* sign bit of byte is second high order bit (0x40) */
303                 if ((value == 0 && !(byte & 0x40)) ||
304                         (value == -1 && (byte & 0x40)))
305                         more = 0;
306                 else
307                         byte |= 0x80;
308                 *p ++= byte;
309         }
310
311         *endbuf = p;
312 }
313
314 static void
315 emit_dwarf_abbrev (MonoDwarfWriter *w, int code, int tag, gboolean has_child,
316                                    int *attrs, int attrs_len)
317 {
318         int i;
319
320         emit_uleb128 (w, code);
321         emit_uleb128 (w, tag);
322         emit_byte (w, has_child);
323
324         for (i = 0; i < attrs_len; i++)
325                 emit_uleb128 (w, attrs [i]);
326         emit_uleb128 (w, 0);
327         emit_uleb128 (w, 0);
328 }
329
330 static void
331 emit_cie (MonoDwarfWriter *w)
332 {
333         emit_section_change (w, ".debug_frame", 0);
334
335         emit_alignment (w, 8);
336
337         /* Emit a CIE */
338         emit_symbol_diff (w, ".Lcie0_end", ".Lcie0_start", 0); /* length */
339         emit_label (w, ".Lcie0_start");
340         emit_int32 (w, 0xffffffff); /* CIE id */
341         emit_byte (w, 3); /* version */
342         emit_string (w, ""); /* augmention */
343         emit_sleb128 (w, 1); /* code alignment factor */
344         emit_sleb128 (w, mono_unwind_get_dwarf_data_align ()); /* data alignment factor */
345         emit_uleb128 (w, mono_unwind_get_dwarf_pc_reg ());
346
347         w->cie_program = w->cie_program;
348         if (w->cie_program) {
349                 guint32 uw_info_len;
350                 guint8 *uw_info = mono_unwind_ops_encode (w->cie_program, &uw_info_len);
351                 emit_bytes (w, uw_info, uw_info_len);
352                 g_free (uw_info);
353         }
354
355         emit_alignment (w, sizeof (gpointer));
356         emit_label (w, ".Lcie0_end");
357 }
358
359 static void
360 emit_pointer_value (MonoDwarfWriter *w, gpointer ptr)
361 {
362         gssize val = (gssize)ptr;
363         emit_bytes (w, (guint8*)&val, sizeof (gpointer));
364 }
365
366 static void
367 emit_fde (MonoDwarfWriter *w, int fde_index, char *start_symbol, char *end_symbol,
368                   guint8 *code, guint32 code_size, GSList *unwind_ops, gboolean use_cie)
369 {
370         char symbol1 [128];
371         char symbol2 [128];
372         GSList *l;
373         guint8 *uw_info;
374         guint32 uw_info_len;
375
376         emit_section_change (w, ".debug_frame", 0);
377
378         sprintf (symbol1, ".Lfde%d_start", fde_index);
379         sprintf (symbol2, ".Lfde%d_end", fde_index);
380         emit_symbol_diff (w, symbol2, symbol1, 0); /* length */
381         emit_label (w, symbol1);
382         emit_int32 (w, 0); /* CIE_pointer */
383         if (start_symbol) {
384                 emit_pointer (w, start_symbol); /* initial_location */
385                 if (end_symbol)
386                         emit_symbol_diff (w, end_symbol, start_symbol, 0); /* address_range */
387                 else {
388                         g_assert (code_size);
389                         emit_int32 (w, code_size);
390                 }
391         } else {
392                 emit_pointer_value (w, code);
393                 emit_int32 (w, code_size);
394         }
395 #if SIZEOF_VOID_P == 8
396         /* Upper 32 bits of code size */
397         emit_int32 (w, 0);
398 #endif
399
400         l = unwind_ops;
401         if (w->cie_program) {
402                 // FIXME: Check that the ops really begin with the CIE program */
403                 int i;
404
405                 for (i = 0; i < g_slist_length (w->cie_program); ++i)
406                         if (l)
407                                 l = l->next;
408         }
409
410         /* Convert the list of MonoUnwindOps to the format used by DWARF */     
411         uw_info = mono_unwind_ops_encode_full (l, &uw_info_len, FALSE);
412         emit_bytes (w, uw_info, uw_info_len);
413         g_free (uw_info);
414
415         emit_alignment (w, sizeof (mgreg_t));
416         emit_label (w, symbol2);
417 }
418
419 /* Abbrevations */
420 #define ABBREV_COMPILE_UNIT 1
421 #define ABBREV_SUBPROGRAM 2
422 #define ABBREV_PARAM 3
423 #define ABBREV_BASE_TYPE 4
424 #define ABBREV_STRUCT_TYPE 5
425 #define ABBREV_DATA_MEMBER 6
426 #define ABBREV_TYPEDEF 7
427 #define ABBREV_ENUM_TYPE 8
428 #define ABBREV_ENUMERATOR 9
429 #define ABBREV_NAMESPACE 10
430 #define ABBREV_VARIABLE 11
431 #define ABBREV_VARIABLE_LOCLIST 12
432 #define ABBREV_POINTER_TYPE 13
433 #define ABBREV_REFERENCE_TYPE 14
434 #define ABBREV_PARAM_LOCLIST 15
435 #define ABBREV_INHERITANCE 16
436 #define ABBREV_STRUCT_TYPE_NOCHILDREN 17
437 #define ABBREV_TRAMP_SUBPROGRAM 18
438
439 static int compile_unit_attr [] = {
440         DW_AT_producer     ,DW_FORM_string,
441     DW_AT_name         ,DW_FORM_string,
442     DW_AT_comp_dir     ,DW_FORM_string,
443         DW_AT_language     ,DW_FORM_data1,
444     DW_AT_low_pc       ,DW_FORM_addr,
445     DW_AT_high_pc      ,DW_FORM_addr,
446         DW_AT_stmt_list    ,DW_FORM_data4
447 };
448
449 static int subprogram_attr [] = {
450         DW_AT_name         , DW_FORM_string,
451         DW_AT_MIPS_linkage_name, DW_FORM_string,
452         DW_AT_decl_file    , DW_FORM_udata,
453         DW_AT_decl_line    , DW_FORM_udata,
454 #ifndef TARGET_IOS
455         DW_AT_description  , DW_FORM_string,
456 #endif
457     DW_AT_low_pc       , DW_FORM_addr,
458     DW_AT_high_pc      , DW_FORM_addr,
459         DW_AT_frame_base   , DW_FORM_block1
460 };
461
462 static int tramp_subprogram_attr [] = {
463         DW_AT_name         , DW_FORM_string,
464     DW_AT_low_pc       , DW_FORM_addr,
465     DW_AT_high_pc      , DW_FORM_addr,
466 };
467
468 static int param_attr [] = {
469         DW_AT_name,     DW_FORM_string,
470         DW_AT_type,     DW_FORM_ref4,
471         DW_AT_location, DW_FORM_block1
472 };
473
474 static int param_loclist_attr [] = {
475         DW_AT_name,     DW_FORM_string,
476         DW_AT_type,     DW_FORM_ref4,
477         DW_AT_location, DW_FORM_data4
478 };
479
480 static int base_type_attr [] = {
481         DW_AT_byte_size,   DW_FORM_data1,
482         DW_AT_encoding,    DW_FORM_data1,
483         DW_AT_name,        DW_FORM_string
484 };
485
486 static int struct_type_attr [] = {
487         DW_AT_name,        DW_FORM_string,
488         DW_AT_byte_size,   DW_FORM_udata,
489 };
490
491 static int data_member_attr [] = {
492         DW_AT_name,        DW_FORM_string,
493         DW_AT_type,        DW_FORM_ref4,
494         DW_AT_data_member_location, DW_FORM_block1
495 };
496
497 static int typedef_attr [] = {
498         DW_AT_name,        DW_FORM_string,
499         DW_AT_type,        DW_FORM_ref4
500 };
501
502 static int pointer_type_attr [] = {
503         DW_AT_type,        DW_FORM_ref4,
504 };
505
506 static int reference_type_attr [] = {
507         DW_AT_type,        DW_FORM_ref4,
508 };
509
510 static int enum_type_attr [] = {
511         DW_AT_name,        DW_FORM_string,
512         DW_AT_byte_size,   DW_FORM_udata,
513         DW_AT_type,        DW_FORM_ref4,
514 };
515
516 static int enumerator_attr [] = {
517         DW_AT_name,        DW_FORM_string,
518         DW_AT_const_value, DW_FORM_sdata,
519 };
520
521 static int namespace_attr [] = {
522         DW_AT_name,        DW_FORM_string,
523 };
524
525 static int variable_attr [] = {
526         DW_AT_name,     DW_FORM_string,
527         DW_AT_type,     DW_FORM_ref4,
528         DW_AT_location, DW_FORM_block1
529 };
530
531 static int variable_loclist_attr [] = {
532         DW_AT_name,     DW_FORM_string,
533         DW_AT_type,     DW_FORM_ref4,
534         DW_AT_location, DW_FORM_data4
535 };
536  
537 static int inheritance_attr [] = {
538         DW_AT_type,        DW_FORM_ref4,
539         DW_AT_data_member_location, DW_FORM_block1
540 };
541
542 typedef struct DwarfBasicType {
543         const char *die_name, *name;
544         int type;
545         int size;
546         int encoding;
547 } DwarfBasicType;
548
549 static DwarfBasicType basic_types [] = {
550         { ".LDIE_I1", "sbyte", MONO_TYPE_I1, 1, DW_ATE_signed },
551         { ".LDIE_U1", "byte", MONO_TYPE_U1, 1, DW_ATE_unsigned },
552         { ".LDIE_I2", "short", MONO_TYPE_I2, 2, DW_ATE_signed },
553         { ".LDIE_U2", "ushort", MONO_TYPE_U2, 2, DW_ATE_unsigned },
554         { ".LDIE_I4", "int", MONO_TYPE_I4, 4, DW_ATE_signed },
555         { ".LDIE_U4", "uint", MONO_TYPE_U4, 4, DW_ATE_unsigned },
556         { ".LDIE_I8", "long", MONO_TYPE_I8, 8, DW_ATE_signed },
557         { ".LDIE_U8", "ulong", MONO_TYPE_U8, 8, DW_ATE_unsigned },
558         { ".LDIE_I", "intptr", MONO_TYPE_I, SIZEOF_VOID_P, DW_ATE_signed },
559         { ".LDIE_U", "uintptr", MONO_TYPE_U, SIZEOF_VOID_P, DW_ATE_unsigned },
560         { ".LDIE_R4", "float", MONO_TYPE_R4, 4, DW_ATE_float },
561         { ".LDIE_R8", "double", MONO_TYPE_R8, 8, DW_ATE_float },
562         { ".LDIE_BOOLEAN", "boolean", MONO_TYPE_BOOLEAN, 1, DW_ATE_boolean },
563         { ".LDIE_CHAR", "char", MONO_TYPE_CHAR, 2, DW_ATE_unsigned_char },
564         { ".LDIE_STRING", "string", MONO_TYPE_STRING, sizeof (gpointer), DW_ATE_address },
565         { ".LDIE_OBJECT", "object", MONO_TYPE_OBJECT, sizeof (gpointer), DW_ATE_address },
566         { ".LDIE_SZARRAY", "object", MONO_TYPE_SZARRAY, sizeof (gpointer), DW_ATE_address },
567 };
568
569 /* Constants for encoding line number special opcodes */
570 #define OPCODE_BASE 13
571 #define LINE_BASE -5
572 #define LINE_RANGE 14
573
574 static int
575 get_line_number_file_name (MonoDwarfWriter *w, const char *name)
576 {
577         int index;
578
579         g_assert (w->file_to_index);
580         index = GPOINTER_TO_UINT (g_hash_table_lookup (w->file_to_index, name));
581         g_assert (index > 0);
582         return index - 1;
583 }
584
585 static int
586 add_line_number_file_name (MonoDwarfWriter *w, const char *name,
587                                                    gint64 last_mod_time, gint64 file_size)
588 {
589         int index;
590         char *copy;
591
592         if (!w->file_to_index) {
593                 w->file_to_index = g_hash_table_new (g_str_hash, g_str_equal);
594                 w->index_to_file = g_hash_table_new (NULL, NULL);
595         }
596
597         index = GPOINTER_TO_UINT (g_hash_table_lookup (w->file_to_index, name));
598         if (index > 0)
599                 return index - 1;
600         index = w->line_number_file_index;
601         w->line_number_file_index ++;
602         copy = g_strdup (name);
603         g_hash_table_insert (w->file_to_index, copy, GUINT_TO_POINTER (index + 1));
604         g_hash_table_insert (w->index_to_file, GUINT_TO_POINTER (index + 1), copy);
605
606         return index;
607 }
608
609 char *
610 mono_dwarf_escape_path (const char *name)
611 {
612         if (strchr (name, '\\')) {
613                 char *s;
614                 int len, i, j;
615
616                 len = strlen (name);
617                 s = (char *)g_malloc0 ((len + 1) * 2);
618                 j = 0;
619                 for (i = 0; i < len; ++i) {
620                         if (name [i] == '\\') {
621                                 s [j ++] = '\\';
622                                 s [j ++] = '\\';
623                         } else {
624                                 s [j ++] = name [i];
625                         }
626                 }
627                 return s;
628         }
629         return g_strdup (name);
630 }
631
632 static void
633 emit_all_line_number_info (MonoDwarfWriter *w)
634 {
635         int i;
636         GHashTable *dir_to_index, *index_to_dir;
637         GSList *l;
638         GSList *info_list;
639
640         add_line_number_file_name (w, "<unknown>", 0, 0);
641
642         /* Collect files */
643         info_list = g_slist_reverse (w->line_info);
644         for (l = info_list; l; l = l->next) {
645                 MethodLineNumberInfo *info = (MethodLineNumberInfo *)l->data;
646                 MonoDebugMethodInfo *minfo;
647                 GPtrArray *source_file_list;
648
649                 // FIXME: Free stuff
650                 minfo = mono_debug_lookup_method (info->method);
651                 if (!minfo)
652                         continue;
653
654                 mono_debug_get_seq_points (minfo, NULL, &source_file_list, NULL, NULL, NULL);
655                 for (i = 0; i < source_file_list->len; ++i) {
656                         MonoDebugSourceInfo *sinfo = (MonoDebugSourceInfo *)g_ptr_array_index (source_file_list, i);
657                         add_line_number_file_name (w, sinfo->source_file, 0, 0);
658                 }
659         }               
660
661         /* Preprocess files */
662         dir_to_index = g_hash_table_new (g_str_hash, g_str_equal);
663         index_to_dir = g_hash_table_new (NULL, NULL);
664         for (i = 0; i < w->line_number_file_index; ++i) {
665                 char *name = (char *)g_hash_table_lookup (w->index_to_file, GUINT_TO_POINTER (i + 1));
666                 char *copy;
667                 int dir_index = 0;
668
669                 if (g_path_is_absolute (name)) {
670                         char *dir = g_path_get_dirname (name);
671
672                         dir_index = GPOINTER_TO_UINT (g_hash_table_lookup (dir_to_index, dir));
673                         if (dir_index == 0) {
674                                 dir_index = w->line_number_dir_index;
675                                 w->line_number_dir_index ++;
676                                 copy = g_strdup (dir);
677                                 g_hash_table_insert (dir_to_index, copy, GUINT_TO_POINTER (dir_index + 1));
678                                 g_hash_table_insert (index_to_dir, GUINT_TO_POINTER (dir_index + 1), copy);
679                         } else {
680                                 dir_index --;
681                         }
682
683                         g_free (dir);
684                 }
685         }
686
687         /* Line number info header */
688         emit_section_change (w, ".debug_line", 0);
689         emit_label (w, ".Ldebug_line_section_start");
690         emit_label (w, ".Ldebug_line_start");
691         emit_symbol_diff (w, ".Ldebug_line_end", ".", -4); /* length */
692         emit_int16 (w, 0x2); /* version */
693         emit_symbol_diff (w, ".Ldebug_line_header_end", ".", -4); /* header_length */
694         emit_byte (w, 1); /* minimum_instruction_length */
695         emit_byte (w, 1); /* default_is_stmt */
696         emit_byte (w, LINE_BASE); /* line_base */
697         emit_byte (w, LINE_RANGE); /* line_range */
698         emit_byte (w, OPCODE_BASE); /* opcode_base */
699         emit_byte (w, 0); /* standard_opcode_lengths */
700         emit_byte (w, 1);
701         emit_byte (w, 1);
702         emit_byte (w, 1);
703         emit_byte (w, 1);
704         emit_byte (w, 0);
705         emit_byte (w, 0);
706         emit_byte (w, 0);
707         emit_byte (w, 1);
708         emit_byte (w, 0);
709         emit_byte (w, 0);
710         emit_byte (w, 1);
711
712         /* Includes */
713         emit_section_change (w, ".debug_line", 0);
714         for (i = 0; i < w->line_number_dir_index; ++i) {
715                 char *dir = (char *)g_hash_table_lookup (index_to_dir, GUINT_TO_POINTER (i + 1));
716
717                 emit_string (w, mono_dwarf_escape_path (dir));
718         }
719         /* End of Includes */
720         emit_byte (w, 0);
721
722         /* Files */
723         for (i = 0; i < w->line_number_file_index; ++i) {
724                 char *name = (char *)g_hash_table_lookup (w->index_to_file, GUINT_TO_POINTER (i + 1));
725                 char *basename = NULL, *dir;
726                 int dir_index = 0;
727
728                 if (g_path_is_absolute (name)) {
729                         dir = g_path_get_dirname (name);
730
731                         dir_index = GPOINTER_TO_UINT (g_hash_table_lookup (dir_to_index, dir));
732                         basename = g_path_get_basename (name);
733                 }
734                                                                                          
735                 if (basename)
736                         emit_string (w, basename);
737                 else
738                         emit_string (w, mono_dwarf_escape_path (name));
739                 emit_uleb128 (w, dir_index);
740                 emit_byte (w, 0);
741                 emit_byte (w, 0);
742         }
743
744         /* End of Files */
745         emit_byte (w, 0);
746
747         emit_label (w, ".Ldebug_line_header_end");
748
749         /* Emit line number table */
750         for (l = info_list; l; l = l->next) {
751                 MethodLineNumberInfo *info = (MethodLineNumberInfo *)l->data;
752                 MonoDebugMethodJitInfo *dmji;
753
754                 dmji = mono_debug_find_method (info->method, mono_domain_get ());
755                 if (!dmji)
756                         continue;
757                 emit_line_number_info (w, info->method, info->start_symbol, info->end_symbol, info->code, info->code_size, dmji);
758                 mono_debug_free_method_jit_info (dmji);
759         }
760         g_slist_free (info_list);
761
762         emit_byte (w, 0);
763         emit_byte (w, 1);
764         emit_byte (w, DW_LNE_end_sequence);
765
766         emit_label (w, ".Ldebug_line_end");
767 }
768
769 /*
770  * Some assemblers like apple's do not support subsections, so we can't place
771  * .Ldebug_info_end at the end of the section using subsections. Instead, we 
772  * define it every time something gets added to the .debug_info section.
773  * The apple assember seems to use the last definition.
774  */
775 static void
776 emit_debug_info_end (MonoDwarfWriter *w)
777 {
778         /* This doesn't seem to work/required with recent iphone sdk versions */
779 #if 0
780         if (!mono_img_writer_subsections_supported (w->w))
781                 fprintf (w->fp, "\n.set %sdebug_info_end,.\n", w->temp_prefix);
782 #endif
783 }
784
785 void
786 mono_dwarf_writer_emit_base_info (MonoDwarfWriter *w, const char *cu_name, GSList *base_unwind_program)
787 {
788         char *s, *build_info;
789         int i;
790
791         if (!w->emit_line) {
792                 emit_section_change (w, ".debug_line", 0);
793                 emit_label (w, ".Ldebug_line_section_start");
794                 emit_label (w, ".Ldebug_line_start");
795         }
796
797         w->cie_program = base_unwind_program;
798
799         emit_section_change (w, ".debug_abbrev", 0);
800         emit_dwarf_abbrev (w, ABBREV_COMPILE_UNIT, DW_TAG_compile_unit, TRUE, 
801                                            compile_unit_attr, G_N_ELEMENTS (compile_unit_attr));
802         emit_dwarf_abbrev (w, ABBREV_SUBPROGRAM, DW_TAG_subprogram, TRUE, 
803                                            subprogram_attr, G_N_ELEMENTS (subprogram_attr));
804         emit_dwarf_abbrev (w, ABBREV_PARAM, DW_TAG_formal_parameter, FALSE, 
805                                            param_attr, G_N_ELEMENTS (param_attr));
806         emit_dwarf_abbrev (w, ABBREV_PARAM_LOCLIST, DW_TAG_formal_parameter, FALSE, 
807                                            param_loclist_attr, G_N_ELEMENTS (param_loclist_attr));
808         emit_dwarf_abbrev (w, ABBREV_BASE_TYPE, DW_TAG_base_type, FALSE, 
809                                            base_type_attr, G_N_ELEMENTS (base_type_attr));
810         emit_dwarf_abbrev (w, ABBREV_STRUCT_TYPE, DW_TAG_class_type, TRUE, 
811                                            struct_type_attr, G_N_ELEMENTS (struct_type_attr));
812         emit_dwarf_abbrev (w, ABBREV_STRUCT_TYPE_NOCHILDREN, DW_TAG_class_type, FALSE, 
813                                            struct_type_attr, G_N_ELEMENTS (struct_type_attr));
814         emit_dwarf_abbrev (w, ABBREV_DATA_MEMBER, DW_TAG_member, FALSE, 
815                                            data_member_attr, G_N_ELEMENTS (data_member_attr));
816         emit_dwarf_abbrev (w, ABBREV_TYPEDEF, DW_TAG_typedef, FALSE, 
817                                            typedef_attr, G_N_ELEMENTS (typedef_attr));
818         emit_dwarf_abbrev (w, ABBREV_ENUM_TYPE, DW_TAG_enumeration_type, TRUE,
819                                            enum_type_attr, G_N_ELEMENTS (enum_type_attr));
820         emit_dwarf_abbrev (w, ABBREV_ENUMERATOR, DW_TAG_enumerator, FALSE,
821                                            enumerator_attr, G_N_ELEMENTS (enumerator_attr));
822         emit_dwarf_abbrev (w, ABBREV_NAMESPACE, DW_TAG_namespace, TRUE,
823                                            namespace_attr, G_N_ELEMENTS (namespace_attr));
824         emit_dwarf_abbrev (w, ABBREV_VARIABLE, DW_TAG_variable, FALSE,
825                                            variable_attr, G_N_ELEMENTS (variable_attr));
826         emit_dwarf_abbrev (w, ABBREV_VARIABLE_LOCLIST, DW_TAG_variable, FALSE,
827                                            variable_loclist_attr, G_N_ELEMENTS (variable_loclist_attr));
828         emit_dwarf_abbrev (w, ABBREV_POINTER_TYPE, DW_TAG_pointer_type, FALSE,
829                                            pointer_type_attr, G_N_ELEMENTS (pointer_type_attr));
830         emit_dwarf_abbrev (w, ABBREV_REFERENCE_TYPE, DW_TAG_reference_type, FALSE,
831                                            reference_type_attr, G_N_ELEMENTS (reference_type_attr));
832         emit_dwarf_abbrev (w, ABBREV_INHERITANCE, DW_TAG_inheritance, FALSE,
833                                            inheritance_attr, G_N_ELEMENTS (inheritance_attr));
834         emit_dwarf_abbrev (w, ABBREV_TRAMP_SUBPROGRAM, DW_TAG_subprogram, FALSE,
835                                            tramp_subprogram_attr, G_N_ELEMENTS (tramp_subprogram_attr));
836         emit_byte (w, 0);
837
838         emit_section_change (w, ".debug_info", 0);
839         emit_label (w, ".Ldebug_info_start");
840         emit_symbol_diff (w, ".Ldebug_info_end", ".Ldebug_info_begin", 0); /* length */
841         emit_label (w, ".Ldebug_info_begin");
842         emit_int16 (w, 0x2); /* DWARF version 2 */
843         emit_int32 (w, 0); /* .debug_abbrev offset */
844         emit_byte (w, sizeof (gpointer)); /* address size */
845
846         /* Compilation unit */
847         emit_uleb128 (w, ABBREV_COMPILE_UNIT);
848         build_info = mono_get_runtime_build_info ();
849         s = g_strdup_printf ("Mono AOT Compiler %s", build_info);
850         emit_string (w, s);
851         g_free (build_info);
852         g_free (s);
853         emit_string (w, cu_name);
854         emit_string (w, "");
855         emit_byte (w, DW_LANG_C);
856         emit_pointer_value (w, 0);
857         emit_pointer_value (w, 0);
858         /* offset into .debug_line section */
859         emit_symbol_diff (w, ".Ldebug_line_start", ".Ldebug_line_section_start", 0);
860
861         /* Base types */
862         for (i = 0; i < G_N_ELEMENTS (basic_types); ++i) {
863                 emit_label (w, basic_types [i].die_name);
864                 emit_uleb128 (w, ABBREV_BASE_TYPE);
865                 emit_byte (w, basic_types [i].size);
866                 emit_byte (w, basic_types [i].encoding);
867                 emit_string (w, basic_types [i].name);
868         }
869
870         emit_debug_info_end (w);
871
872         /* debug_loc section */
873         emit_section_change (w, ".debug_loc", 0);
874         emit_label (w, ".Ldebug_loc_start");
875
876         emit_cie (w);
877 }
878
879 /*
880  * mono_dwarf_writer_close:
881  *
882  *   Finalize the emitted debugging info.
883  */
884 void
885 mono_dwarf_writer_close (MonoDwarfWriter *w)
886 {
887         emit_section_change (w, ".debug_info", 0);
888         emit_byte (w, 0); /* close COMPILE_UNIT */
889         emit_label (w, ".Ldebug_info_end");
890
891         if (w->emit_line)
892                 emit_all_line_number_info (w);
893 }
894
895 static void emit_type (MonoDwarfWriter *w, MonoType *t);
896 static const char* get_type_die (MonoDwarfWriter *w, MonoType *t);
897
898 static const char*
899 get_class_die (MonoDwarfWriter *w, MonoClass *klass, gboolean vtype)
900 {
901         GHashTable *cache;
902
903         if (vtype)
904                 cache = w->class_to_vtype_die;
905         else
906                 cache = w->class_to_die;
907
908         return (const char *)g_hash_table_lookup (cache, klass);
909 }
910
911 /* Returns the local symbol pointing to the emitted debug info */
912 static char*
913 emit_class_dwarf_info (MonoDwarfWriter *w, MonoClass *klass, gboolean vtype)
914 {
915         char *die, *pointer_die, *reference_die;
916         char *full_name, *p;
917         gpointer iter;
918         MonoClassField *field;
919         const char *fdie;
920         int k;
921         gboolean emit_namespace = FALSE, has_children;
922         GHashTable *cache;
923
924         if (vtype)
925                 cache = w->class_to_vtype_die;
926         else
927                 cache = w->class_to_die;
928
929         die = (char *)g_hash_table_lookup (cache, klass);
930         if (die)
931                 return die;
932
933         if (!((klass->byval_arg.type == MONO_TYPE_CLASS) || (klass->byval_arg.type == MONO_TYPE_OBJECT) || klass->byval_arg.type == MONO_TYPE_GENERICINST || klass->enumtype || (klass->byval_arg.type == MONO_TYPE_VALUETYPE && vtype) ||
934                   (klass->byval_arg.type >= MONO_TYPE_BOOLEAN && klass->byval_arg.type <= MONO_TYPE_R8 && !vtype)))
935                 return NULL;
936
937         /*
938          * FIXME: gdb can't handle namespaces in languages it doesn't know about.
939          */
940         /*
941         if (klass->name_space && klass->name_space [0] != '\0')
942                 emit_namespace = TRUE;
943         */
944         if (emit_namespace) {
945                 emit_uleb128 (w, ABBREV_NAMESPACE);
946                 emit_string (w, klass->name_space);
947         }
948
949         full_name = g_strdup_printf ("%s%s%s", klass->name_space, klass->name_space ? "." : "", klass->name);
950         /* 
951          * gdb doesn't support namespaces for non-C++ dwarf objects, so use _
952          * to separate components.
953          */
954         for (p = full_name; *p; p ++)
955                 if (*p == '.')
956                         *p = '_';
957
958         die = g_strdup_printf (".LTDIE_%d", w->tdie_index);
959         pointer_die = g_strdup_printf (".LTDIE_%d_POINTER", w->tdie_index);
960         reference_die = g_strdup_printf (".LTDIE_%d_REFERENCE", w->tdie_index);
961         w->tdie_index ++;
962
963         g_hash_table_insert (w->class_to_pointer_die, klass, pointer_die);
964         g_hash_table_insert (w->class_to_reference_die, klass, reference_die);
965         g_hash_table_insert (cache, klass, die);
966
967         if (klass->enumtype) {
968                 int size = mono_class_value_size (mono_class_from_mono_type (mono_class_enum_basetype (klass)), NULL);
969
970                 emit_label (w, die);
971
972                 emit_uleb128 (w, ABBREV_ENUM_TYPE);
973                 emit_string (w, full_name);
974                 emit_uleb128 (w, size);
975                 for (k = 0; k < G_N_ELEMENTS (basic_types); ++k)
976                         if (basic_types [k].type == mono_class_enum_basetype (klass)->type)
977                                 break;
978                 g_assert (k < G_N_ELEMENTS (basic_types));
979                 emit_symbol_diff (w, basic_types [k].die_name, ".Ldebug_info_start", 0);
980
981                 /* Emit enum values */
982                 iter = NULL;
983                 while ((field = mono_class_get_fields (klass, &iter))) {
984                         const char *p;
985                         MonoTypeEnum def_type;
986
987                         if (!(field->type->attrs & FIELD_ATTRIBUTE_STATIC))
988                                 continue;
989                         if (mono_field_is_deleted (field))
990                                 continue;
991
992                         emit_uleb128 (w, ABBREV_ENUMERATOR);
993                         emit_string (w, mono_field_get_name (field));
994
995                         p = mono_class_get_field_default_value (field, &def_type);
996                         /* len = */ mono_metadata_decode_blob_size (p, &p);
997                         switch (mono_class_enum_basetype (klass)->type) {
998                         case MONO_TYPE_U1:
999                         case MONO_TYPE_I1:
1000                         case MONO_TYPE_BOOLEAN:
1001                                 emit_sleb128 (w, *p);
1002                                 break;
1003                         case MONO_TYPE_U2:
1004                         case MONO_TYPE_I2:
1005                         case MONO_TYPE_CHAR:
1006                                 emit_sleb128 (w, read16 (p));
1007                                 break;
1008                         case MONO_TYPE_U4:
1009                         case MONO_TYPE_I4:
1010                                 emit_sleb128 (w, read32 (p));
1011                                 break;
1012                         case MONO_TYPE_U8:
1013                         case MONO_TYPE_I8:
1014                                 emit_sleb128 (w, read64 (p));
1015                                 break;
1016                         case MONO_TYPE_I:
1017                         case MONO_TYPE_U:
1018 #if SIZEOF_VOID_P == 8
1019                                 emit_sleb128 (w, read64 (p));
1020 #else
1021                                 emit_sleb128 (w, read32 (p));
1022 #endif
1023                                 break;
1024                         default:
1025                                 g_assert_not_reached ();
1026                         }
1027                 }
1028
1029                 has_children = TRUE;
1030         } else {
1031                 guint8 buf [128];
1032                 guint8 *p;
1033                 char *parent_die;
1034
1035                 if (klass->parent)
1036                         parent_die = emit_class_dwarf_info (w, klass->parent, FALSE);
1037                 else
1038                         parent_die = NULL;
1039
1040                 /* Emit field types */
1041                 iter = NULL;
1042                 while ((field = mono_class_get_fields (klass, &iter))) {
1043                         if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1044                                 continue;
1045
1046                         emit_type (w, field->type);
1047                 }
1048
1049                 iter = NULL;
1050                 has_children = parent_die || mono_class_get_fields (klass, &iter);
1051
1052                 emit_label (w, die);
1053
1054                 emit_uleb128 (w, has_children ? ABBREV_STRUCT_TYPE : ABBREV_STRUCT_TYPE_NOCHILDREN);
1055                 emit_string (w, full_name);
1056                 emit_uleb128 (w, klass->instance_size);
1057
1058                 if (parent_die) {
1059                         emit_uleb128 (w, ABBREV_INHERITANCE);
1060                         emit_symbol_diff (w, parent_die, ".Ldebug_info_start", 0);
1061
1062                         p = buf;
1063                         *p ++= DW_OP_plus_uconst;
1064                         encode_uleb128 (0, p, &p);
1065                         emit_byte (w, p - buf);
1066                         emit_bytes (w, buf, p - buf);
1067                 }
1068
1069                 /* Emit fields */
1070                 iter = NULL;
1071                 while ((field = mono_class_get_fields (klass, &iter))) {
1072                         if (field->type->attrs & FIELD_ATTRIBUTE_STATIC)
1073                                 continue;
1074
1075                         fdie = get_type_die (w, field->type);
1076                         if (fdie) {
1077                                 emit_uleb128 (w, ABBREV_DATA_MEMBER);
1078                                 emit_string (w, field->name);
1079                                 emit_symbol_diff (w, fdie, ".Ldebug_info_start", 0);
1080                                 /* location */
1081                                 p = buf;
1082                                 *p ++= DW_OP_plus_uconst;
1083                                 if (klass->valuetype && vtype)
1084                                         encode_uleb128 (field->offset - sizeof (MonoObject), p, &p);
1085                                 else
1086                                         encode_uleb128 (field->offset, p, &p);
1087
1088                                 emit_byte (w, p - buf);
1089                                 emit_bytes (w, buf, p - buf);
1090                         }
1091                 }
1092         }
1093
1094         /* Type end */
1095         if (has_children)
1096                 emit_uleb128 (w, 0x0);
1097
1098         /* Add a typedef, so we can reference the type without a 'struct' in gdb */
1099         emit_uleb128 (w, ABBREV_TYPEDEF);
1100         emit_string (w, full_name);
1101         emit_symbol_diff (w, die, ".Ldebug_info_start", 0);
1102
1103         /* Add a pointer type */
1104         emit_label (w, pointer_die);
1105
1106         emit_uleb128 (w, ABBREV_POINTER_TYPE);
1107         emit_symbol_diff (w, die, ".Ldebug_info_start", 0);
1108
1109         /* Add a reference type */
1110         emit_label (w, reference_die);
1111
1112         emit_uleb128 (w, ABBREV_REFERENCE_TYPE);
1113         emit_symbol_diff (w, die, ".Ldebug_info_start", 0);
1114
1115         g_free (full_name);
1116
1117         if (emit_namespace) {
1118                 /* Namespace end */
1119                 emit_uleb128 (w, 0x0);
1120         }
1121
1122         return die;
1123 }
1124
1125 static gboolean base_types_emitted [64];
1126
1127 static const char*
1128 get_type_die (MonoDwarfWriter *w, MonoType *t)
1129 {
1130         MonoClass *klass = mono_class_from_mono_type (t);
1131         int j;
1132         const char *tdie;
1133
1134         if (t->byref) {
1135                 if (t->type == MONO_TYPE_VALUETYPE) {
1136                         tdie = (const char *)g_hash_table_lookup (w->class_to_pointer_die, klass);
1137                 }
1138                 else {
1139                         tdie = get_class_die (w, klass, FALSE);
1140                         /* Should return a pointer type to a reference */
1141                 }
1142                 // FIXME:
1143                 t = &mono_defaults.int_class->byval_arg;
1144         }
1145         for (j = 0; j < G_N_ELEMENTS (basic_types); ++j)
1146                 if (basic_types [j].type == t->type)
1147                         break;
1148         if (j < G_N_ELEMENTS (basic_types)) {
1149                 tdie = basic_types [j].die_name;
1150         } else {
1151                 switch (t->type) {
1152                 case MONO_TYPE_CLASS:
1153                         tdie = (const char *)g_hash_table_lookup (w->class_to_reference_die, klass);
1154                         //tdie = ".LDIE_OBJECT";
1155                         break;
1156                 case MONO_TYPE_ARRAY:
1157                         tdie = ".LDIE_OBJECT";
1158                         break;
1159                 case MONO_TYPE_VALUETYPE:
1160                         if (klass->enumtype)
1161                                 tdie = get_class_die (w, klass, FALSE);
1162                         else
1163                                 tdie = ".LDIE_I4";
1164                         break;
1165                 case MONO_TYPE_GENERICINST:
1166                         if (!MONO_TYPE_ISSTRUCT (t)) {
1167                                 tdie = (const char *)g_hash_table_lookup (w->class_to_reference_die, klass);
1168                         } else {
1169                                 tdie = ".LDIE_I4";
1170                         }
1171                         break;
1172                 case MONO_TYPE_PTR:
1173                         tdie = ".LDIE_I";
1174                         break;
1175                 default:
1176                         tdie = ".LDIE_I4";
1177                         break;
1178                 }
1179         }
1180
1181         g_assert (tdie);
1182
1183         return tdie;
1184 }
1185
1186 static void
1187 emit_type (MonoDwarfWriter *w, MonoType *t)
1188 {
1189         MonoClass *klass = mono_class_from_mono_type (t);
1190         int j;
1191         const char *tdie;
1192
1193         if (t->byref) {
1194                 if (t->type == MONO_TYPE_VALUETYPE) {
1195                         tdie = emit_class_dwarf_info (w, klass, TRUE);
1196                         if (tdie)
1197                                 return;
1198                 }
1199                 else {
1200                         emit_class_dwarf_info (w, klass, FALSE);
1201                 }
1202                 // FIXME:
1203                 t = &mono_defaults.int_class->byval_arg;
1204         }
1205         for (j = 0; j < G_N_ELEMENTS (basic_types); ++j)
1206                 if (basic_types [j].type == t->type)
1207                         break;
1208         if (j < G_N_ELEMENTS (basic_types)) {
1209                 /* Emit a boxed version of base types */
1210                 if (j < 64 && !base_types_emitted [j]) {
1211                         emit_class_dwarf_info (w, klass, FALSE);
1212                         base_types_emitted [j] = TRUE;
1213                 }
1214         } else {
1215                 switch (t->type) {
1216                 case MONO_TYPE_CLASS:
1217                         emit_class_dwarf_info (w, klass, FALSE);
1218                         break;
1219                 case MONO_TYPE_ARRAY:
1220                         break;
1221                 case MONO_TYPE_VALUETYPE:
1222                         if (klass->enumtype)
1223                                 emit_class_dwarf_info (w, klass, FALSE);
1224                         break;
1225                 case MONO_TYPE_GENERICINST:
1226                         if (!MONO_TYPE_ISSTRUCT (t))
1227                                 emit_class_dwarf_info (w, klass, FALSE);
1228                         break;
1229                 case MONO_TYPE_PTR:
1230                         break;
1231                 default:
1232                         break;
1233                 }
1234         }
1235 }
1236
1237 static void
1238 emit_var_type (MonoDwarfWriter *w, MonoType *t)
1239 {
1240         const char *tdie;
1241
1242         tdie = get_type_die (w, t);
1243
1244         emit_symbol_diff (w, tdie, ".Ldebug_info_start", 0);
1245 }
1246
1247 static void
1248 encode_var_location (MonoDwarfWriter *w, MonoInst *ins, guint8 *p, guint8 **endp)
1249 {
1250         /* location */
1251         /* FIXME: This needs a location list, since the args can go from reg->stack */
1252         if (!ins || ins->flags & MONO_INST_IS_DEAD) {
1253                 /* gdb treats this as optimized out */
1254         } else if (ins->opcode == OP_REGVAR) {
1255                 *p = DW_OP_reg0 + mono_hw_reg_to_dwarf_reg (ins->dreg);
1256                 p ++;
1257         } else if (ins->opcode == OP_REGOFFSET) {
1258                 *p ++= DW_OP_breg0 + mono_hw_reg_to_dwarf_reg (ins->inst_basereg);
1259                 encode_sleb128 (ins->inst_offset, p, &p);
1260         } else {
1261                 // FIXME:
1262                 *p ++ = DW_OP_reg0;
1263         }
1264
1265         *endp = p;
1266 }
1267
1268 static void
1269 emit_loclist (MonoDwarfWriter *w, MonoInst *ins,
1270                           guint8 *loclist_begin_addr, guint8 *loclist_end_addr,
1271                           guint8 *expr, guint32 expr_len)
1272 {
1273         char label [128];
1274
1275         emit_push_section (w, ".debug_loc", 0);
1276         sprintf (label, ".Lloclist_%d", w->loclist_index ++ );
1277         emit_label (w, label);
1278
1279         emit_pointer_value (w, loclist_begin_addr);
1280         emit_pointer_value (w, loclist_end_addr);
1281         emit_byte (w, expr_len % 256);
1282         emit_byte (w, expr_len / 256);
1283         emit_bytes (w, expr, expr_len);
1284
1285         emit_pointer_value (w, NULL);
1286         emit_pointer_value (w, NULL);
1287
1288         emit_pop_section (w);
1289         emit_symbol_diff (w, label, ".Ldebug_loc_start", 0);
1290 }
1291
1292 /* 
1293  * MonoDisHelper->tokener doesn't take an IP argument, and we can't add one since 
1294  * it is a public header.
1295  */
1296 static const guint8 *token_handler_ip;
1297
1298 static char*
1299 token_handler (MonoDisHelper *dh, MonoMethod *method, guint32 token)
1300 {
1301         MonoError error;
1302         char *res, *desc;
1303         MonoMethod *cmethod;
1304         MonoClass *klass;
1305         MonoClassField *field;
1306         gpointer data = NULL;
1307
1308         if (method->wrapper_type)
1309                 data = mono_method_get_wrapper_data (method, token);
1310
1311         switch (*token_handler_ip) {
1312         case CEE_ISINST:
1313         case CEE_CASTCLASS:
1314         case CEE_LDELEMA:
1315                 if (method->wrapper_type) {
1316                         klass = (MonoClass *)data;
1317                 } else {
1318                         klass = mono_class_get_checked (method->klass->image, token, &error);
1319                         g_assert (mono_error_ok (&error)); /* FIXME error handling */
1320                 }
1321                 res = g_strdup_printf ("<%s>", klass->name);
1322                 break;
1323         case CEE_NEWOBJ:
1324         case CEE_CALL:
1325         case CEE_CALLVIRT:
1326                 if (method->wrapper_type) {
1327                         cmethod = (MonoMethod *)data;
1328                 } else {
1329                         MonoError error;
1330                         cmethod = mono_get_method_checked (method->klass->image, token, NULL, NULL, &error);
1331                         if (!cmethod)
1332                                 g_error ("Could not load method due to %s", mono_error_get_message (&error)); /* FIXME don't swallow the error */
1333                 }
1334                 desc = mono_method_full_name (cmethod, TRUE);
1335                 res = g_strdup_printf ("<%s>", desc);
1336                 g_free (desc);
1337                 break;
1338         case CEE_CALLI:
1339                 if (method->wrapper_type) {
1340                         desc = mono_signature_get_desc ((MonoMethodSignature *)data, FALSE);
1341                         res = g_strdup_printf ("<%s>", desc);
1342                         g_free (desc);
1343                 } else {
1344                         res = g_strdup_printf ("<0x%08x>", token);
1345                 }
1346                 break;
1347         case CEE_LDFLD:
1348         case CEE_LDSFLD:
1349         case CEE_STFLD:
1350         case CEE_STSFLD:
1351                 if (method->wrapper_type) {
1352                         field = (MonoClassField *)data;
1353                 } else {
1354                         field = mono_field_from_token_checked (method->klass->image, token, &klass, NULL,  &error);
1355                         g_assert (mono_error_ok (&error)); /* FIXME error handling */
1356                 }
1357                 desc = mono_field_full_name (field);
1358                 res = g_strdup_printf ("<%s>", desc);
1359                 g_free (desc);
1360                 break;
1361         default:
1362                 res = g_strdup_printf ("<0x%08x>", token);
1363                 break;
1364         }
1365
1366         return res;
1367 }
1368
1369 /*
1370  * disasm_ins:
1371  *
1372  *   Produce a disassembled form of the IL instruction at IP. This is an extension
1373  * of mono_disasm_code_one () which can disasm tokens, handle wrapper methods, and
1374  * CEE_MONO_ opcodes.
1375  */
1376 static char*
1377 disasm_ins (MonoMethod *method, const guchar *ip, const guint8 **endip)
1378 {
1379         MonoError error;
1380         char *dis;
1381         MonoDisHelper dh;
1382         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
1383         mono_error_assert_ok (&error); /* FIXME don't swallow the error */
1384
1385         memset (&dh, 0, sizeof (dh));
1386         dh.newline = "";
1387         dh.label_format = "IL_%04x: ";
1388         dh.label_target = "IL_%04x";
1389         dh.tokener = token_handler;
1390
1391         token_handler_ip = ip;
1392         if (*ip == MONO_CUSTOM_PREFIX) {
1393                 guint32 token;
1394                 gpointer data;
1395
1396                 switch (ip [1]) {
1397                 case CEE_MONO_ICALL: {
1398                         MonoJitICallInfo *info;
1399
1400                         token = read32 (ip + 2);
1401                         data = mono_method_get_wrapper_data (method, token);
1402                         info = mono_find_jit_icall_by_addr (data);
1403                         g_assert (info);
1404
1405                         dis = g_strdup_printf ("IL_%04x: mono_icall <%s>", (int)(ip - header->code), info->name);
1406                         ip += 6;
1407                         break;
1408                 }
1409                 case CEE_MONO_CLASSCONST: {
1410                         token = read32 (ip + 2);
1411                         data = mono_method_get_wrapper_data (method, token);
1412
1413                         dis = g_strdup_printf ("IL_%04x: mono_classconst <%s>", (int)(ip - header->code), ((MonoClass*)data)->name);
1414                         ip += 6;
1415                         break;
1416                 }
1417                 default:
1418                         dis = mono_disasm_code_one (&dh, method, ip, &ip);
1419                 }
1420         } else {
1421                 dis = mono_disasm_code_one (&dh, method, ip, &ip);
1422         }
1423         token_handler_ip = NULL;
1424
1425         *endip = ip;
1426         mono_metadata_free_mh (header);
1427         return dis;
1428 }
1429
1430 static gint32
1431 il_offset_from_address (MonoMethod *method, MonoDebugMethodJitInfo *jit, 
1432                                                 guint32 native_offset)
1433 {
1434         int i;
1435
1436         if (!jit->line_numbers)
1437                 return -1;
1438
1439         for (i = jit->num_line_numbers - 1; i >= 0; i--) {
1440                 MonoDebugLineNumberEntry lne = jit->line_numbers [i];
1441
1442                 if (lne.native_offset <= native_offset)
1443                         return lne.il_offset;
1444         }
1445
1446         return -1;
1447 }
1448
1449 static int max_special_addr_diff = 0;
1450
1451 static inline void
1452 emit_advance_op (MonoDwarfWriter *w, int line_diff, int addr_diff)
1453 {
1454         gint64 opcode = 0;
1455
1456         /* Use a special opcode if possible */
1457         if (line_diff - LINE_BASE >= 0 && line_diff - LINE_BASE < LINE_RANGE) {
1458                 if (max_special_addr_diff == 0)
1459                         max_special_addr_diff = (255 - OPCODE_BASE) / LINE_RANGE;
1460
1461                 if (addr_diff > max_special_addr_diff && (addr_diff < 2 * max_special_addr_diff)) {
1462                         emit_byte (w, DW_LNS_const_add_pc);
1463                         addr_diff -= max_special_addr_diff;
1464                 }
1465
1466                 opcode = (line_diff - LINE_BASE) + (LINE_RANGE * addr_diff) + OPCODE_BASE;
1467                 if (opcode > 255)
1468                         opcode = 0;
1469         }
1470
1471         if (opcode != 0) {
1472                 emit_byte (w, opcode);
1473         } else {
1474                 //printf ("large: %d %d %d\n", line_diff, addr_diff, max_special_addr_diff);
1475                 emit_byte (w, DW_LNS_advance_line);
1476                 emit_sleb128 (w, line_diff);
1477                 emit_byte (w, DW_LNS_advance_pc);
1478                 emit_sleb128 (w, addr_diff);
1479                 emit_byte (w, DW_LNS_copy);
1480         }
1481 }
1482
1483 static gint
1484 compare_lne (MonoDebugLineNumberEntry *a, MonoDebugLineNumberEntry *b)
1485 {
1486         if (a->native_offset == b->native_offset)
1487                 return a->il_offset - b->il_offset;
1488         else
1489                 return a->native_offset - b->native_offset;
1490 }
1491
1492 static void
1493 emit_line_number_info (MonoDwarfWriter *w, MonoMethod *method, 
1494                                            char *start_symbol, char *end_symbol,
1495                                            guint8 *code, guint32 code_size,
1496                                            MonoDebugMethodJitInfo *debug_info)
1497 {
1498         MonoError error;
1499         guint32 prev_line = 0;
1500         guint32 prev_native_offset = 0;
1501         int i, file_index, il_offset, prev_il_offset;
1502         gboolean first = TRUE;
1503         MonoDebugSourceLocation *loc;
1504         char *prev_file_name = NULL;
1505         MonoMethodHeader *header = mono_method_get_header_checked (method, &error);
1506         MonoDebugMethodInfo *minfo;
1507         MonoDebugLineNumberEntry *ln_array;
1508         int *native_to_il_offset = NULL;
1509         
1510         mono_error_assert_ok (&error); /* FIXME don't swallow the error */
1511
1512         if (!w->emit_line) {
1513                 mono_metadata_free_mh (header);
1514                 return;
1515         }
1516
1517         minfo = mono_debug_lookup_method (method);
1518
1519         /* Compute the native->IL offset mapping */
1520
1521         g_assert (code_size);
1522
1523         ln_array = g_new0 (MonoDebugLineNumberEntry, debug_info->num_line_numbers);
1524         memcpy (ln_array, debug_info->line_numbers, debug_info->num_line_numbers * sizeof (MonoDebugLineNumberEntry));
1525
1526         qsort (ln_array, debug_info->num_line_numbers, sizeof (MonoDebugLineNumberEntry), (int (*)(const void *, const void *))compare_lne);
1527
1528         native_to_il_offset = g_new0 (int, code_size + 1);
1529
1530         for (i = 0; i < debug_info->num_line_numbers; ++i) {
1531                 int j;
1532                 MonoDebugLineNumberEntry *lne = &ln_array [i];
1533
1534                 if (i == 0) {
1535                         for (j = 0; j < lne->native_offset; ++j)
1536                                 native_to_il_offset [j] = -1;
1537                 }
1538
1539                 if (i < debug_info->num_line_numbers - 1) {
1540                         MonoDebugLineNumberEntry *lne_next = &ln_array [i + 1];
1541
1542                         for (j = lne->native_offset; j < lne_next->native_offset; ++j)
1543                                 native_to_il_offset [j] = lne->il_offset;
1544                 } else {
1545                         for (j = lne->native_offset; j < code_size; ++j)
1546                                 native_to_il_offset [j] = lne->il_offset;
1547                 }
1548         }
1549         g_free (ln_array);
1550
1551         prev_line = 1;
1552         prev_il_offset = -1;
1553
1554         w->cur_file_index = -1;
1555         for (i = 0; i < code_size; ++i) {
1556                 int line_diff, addr_diff;
1557
1558                 if (!minfo)
1559                         continue;
1560
1561                 if (!debug_info->line_numbers)
1562                         continue;
1563
1564                 if (native_to_il_offset)
1565                         il_offset = native_to_il_offset [i];
1566                 else
1567                         il_offset = il_offset_from_address (method, debug_info, i);
1568                 /*
1569                 il_offset = il_offset_from_address (method, debug_info, i);
1570
1571                 g_assert (il_offset == native_to_il_offset [i]);
1572                 */
1573
1574                 il_offset = native_to_il_offset [i];
1575                 if (il_offset < 0)
1576                         continue;
1577
1578                 if (il_offset == prev_il_offset)
1579                         continue;
1580
1581                 prev_il_offset = il_offset;
1582
1583                 loc = mono_debug_method_lookup_location (minfo, il_offset);
1584                 if (!loc)
1585                         continue;
1586                 if (!loc->source_file) {
1587                         mono_debug_free_source_location (loc);
1588                         continue;
1589                 }
1590
1591                 line_diff = (gint32)loc->row - (gint32)prev_line;
1592                 addr_diff = i - prev_native_offset;
1593
1594                 if (first) {    
1595                         emit_section_change (w, ".debug_line", 0);
1596
1597                         emit_byte (w, 0);
1598                         emit_byte (w, sizeof (gpointer) + 1);
1599                         emit_byte (w, DW_LNE_set_address);
1600                         if (start_symbol)
1601                                 emit_pointer_unaligned (w, start_symbol);
1602                         else
1603                                 emit_pointer_value (w, code);
1604                         first = FALSE;
1605                 }
1606
1607                 if (loc->row != prev_line) {
1608                         if (!prev_file_name || strcmp (loc->source_file, prev_file_name) != 0) {
1609                                 /* Add an entry to the file table */
1610                                 /* FIXME: Avoid duplicates */
1611                                 file_index = get_line_number_file_name (w, loc->source_file) + 1;
1612                                 g_free (prev_file_name);
1613                                 prev_file_name = g_strdup (loc->source_file);
1614
1615                                 if (w->cur_file_index != file_index) {
1616                                         emit_byte (w, DW_LNS_set_file);
1617                                         emit_uleb128 (w, file_index);
1618                                         emit_byte (w, DW_LNS_copy);
1619                                         w->cur_file_index = file_index;
1620                                 }                                       
1621                         }
1622                 }
1623
1624                 if (loc->row != prev_line) {
1625                         if (prev_native_offset == 0)
1626                                 emit_byte (w, DW_LNE_set_prologue_end);
1627
1628                         //printf ("X: %p(+0x%x) %d %s:%d(+%d)\n", code + i, addr_diff, loc->il_offset, loc->source_file, loc->row, line_diff);
1629                         emit_advance_op (w, line_diff, addr_diff);
1630
1631                         prev_line = loc->row;
1632                         prev_native_offset = i;
1633                 }
1634
1635                 mono_debug_free_source_location (loc);
1636                 first = FALSE;
1637         }
1638
1639         g_free (native_to_il_offset);
1640         g_free (prev_file_name);
1641
1642         if (!first) {
1643                 emit_byte (w, DW_LNS_advance_pc);
1644                 emit_sleb128 (w, code_size - prev_native_offset);
1645                 emit_byte (w, DW_LNS_copy);
1646
1647                 emit_byte (w, 0);
1648                 emit_byte (w, 1);
1649                 emit_byte (w, DW_LNE_end_sequence);
1650         } else if (!start_symbol) {
1651                 /* No debug info, XDEBUG mode */
1652                 char *name, *dis;
1653                 const guint8 *ip = header->code;
1654                 int prev_line, prev_native_offset;
1655                 int *il_to_line;
1656
1657                 /*
1658                  * Emit the IL code into a temporary file and emit line number info
1659                  * referencing that file.
1660                  */
1661
1662                 name = mono_method_full_name (method, TRUE);
1663                 fprintf (w->il_file, "// %s\n", name);
1664                 w->il_file_line_index ++;
1665                 g_free (name);
1666
1667                 il_to_line = g_new0 (int, header->code_size);
1668
1669                 emit_section_change (w, ".debug_line", 0);
1670                 emit_byte (w, 0);
1671                 emit_byte (w, sizeof (gpointer) + 1);
1672                 emit_byte (w, DW_LNE_set_address);
1673                 emit_pointer_value (w, code);
1674
1675                 // FIXME: Optimize this
1676                 while (ip < header->code + header->code_size) {
1677                         int il_offset = ip - header->code;
1678
1679                         /* Emit IL */
1680                         w->il_file_line_index ++;
1681
1682                         dis = disasm_ins (method, ip, &ip);
1683                         fprintf (w->il_file, "%s\n", dis);
1684                         g_free (dis);
1685
1686                         il_to_line [il_offset] = w->il_file_line_index;
1687                 }
1688
1689                 /* Emit line number info */
1690                 prev_line = 1;
1691                 prev_native_offset = 0;
1692                 for (i = 0; i < debug_info->num_line_numbers; ++i) {
1693                         MonoDebugLineNumberEntry *lne = &debug_info->line_numbers [i];
1694                         int line;
1695
1696                         if (lne->il_offset >= header->code_size)
1697                                 continue;
1698                         line = il_to_line [lne->il_offset];
1699                         if (!line) {
1700                                 /* 
1701                                  * This seems to happen randomly, it looks like il_offset points
1702                                  * into the middle of an instruction.
1703                                  */
1704                                 continue;
1705                                 /*
1706                                 printf ("%s\n", mono_method_full_name (method, TRUE));
1707                                 printf ("%d %d\n", lne->il_offset, header->code_size);
1708                                 g_assert (line);
1709                                 */
1710                         }
1711
1712                         if (line - prev_line != 0) {
1713                                 emit_advance_op (w, line - prev_line, (gint32)lne->native_offset - prev_native_offset);
1714
1715                                 prev_line = line;
1716                                 prev_native_offset = lne->native_offset;
1717                         }
1718                 }
1719
1720                 emit_byte (w, DW_LNS_advance_pc);
1721                 emit_sleb128 (w, code_size - prev_native_offset);
1722                 emit_byte (w, DW_LNS_copy);
1723
1724                 emit_byte (w, 0);
1725                 emit_byte (w, 1);
1726                 emit_byte (w, DW_LNE_end_sequence);
1727
1728                 fflush (w->il_file);
1729                 g_free (il_to_line);
1730         }
1731         mono_metadata_free_mh (header);
1732 }
1733
1734 static MonoMethodVar*
1735 find_vmv (MonoCompile *cfg, MonoInst *ins)
1736 {
1737         int j;
1738
1739         if (cfg->varinfo) {
1740                 for (j = 0; j < cfg->num_varinfo; ++j) {
1741                         if (cfg->varinfo [j] == ins)
1742                                 break;
1743                 }
1744
1745                 if (j < cfg->num_varinfo) {
1746                         return MONO_VARINFO (cfg, j);
1747                 }
1748         }
1749
1750         return NULL;
1751 }
1752
1753 void
1754 mono_dwarf_writer_emit_method (MonoDwarfWriter *w, MonoCompile *cfg, MonoMethod *method, char *start_symbol, char *end_symbol, char *linkage_name,
1755                                                            guint8 *code, guint32 code_size, MonoInst **args, MonoInst **locals, GSList *unwind_info, MonoDebugMethodJitInfo *debug_info)
1756 {
1757         MonoError error;
1758         char *name;
1759         MonoMethodSignature *sig;
1760         MonoMethodHeader *header;
1761         char **names;
1762         MonoDebugLocalsInfo *locals_info;
1763         MonoDebugMethodInfo *minfo;
1764         MonoDebugSourceLocation *loc = NULL;
1765         int i;
1766         guint8 buf [128];
1767         guint8 *p;
1768
1769         emit_section_change (w, ".debug_info", 0);
1770
1771         sig = mono_method_signature (method);
1772         header = mono_method_get_header_checked (method, &error);
1773         mono_error_assert_ok (&error); /* FIXME don't swallow the error */
1774
1775         /* Parameter types */
1776         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1777                 MonoType *t;
1778
1779                 if (i == 0 && sig->hasthis) {
1780                         if (method->klass->valuetype)
1781                                 t = &method->klass->this_arg;
1782                         else
1783                                 t = &method->klass->byval_arg;
1784                 } else {
1785                         t = sig->params [i - sig->hasthis];
1786                 }
1787
1788                 emit_type (w, t);
1789         }
1790         //emit_type (w, &mono_defaults.int32_class->byval_arg);
1791
1792         /* Local types */
1793         for (i = 0; i < header->num_locals; ++i) {
1794                 emit_type (w, header->locals [i]);
1795         }
1796
1797         minfo = mono_debug_lookup_method (method);
1798         if (minfo)
1799                 loc = mono_debug_method_lookup_location (minfo, 0);
1800
1801         /* Subprogram */
1802         names = g_new0 (char *, sig->param_count);
1803         mono_method_get_param_names (method, (const char **) names);
1804
1805         emit_uleb128 (w, ABBREV_SUBPROGRAM);
1806         /* DW_AT_name */
1807         name = mono_method_full_name (method, FALSE);
1808         emit_escaped_string (w, name);
1809         /* DW_AT_MIPS_linkage_name */
1810         if (linkage_name)
1811                 emit_string (w, linkage_name);
1812         else
1813                 emit_string (w, "");
1814         /* DW_AT_decl_file/DW_AT_decl_line */
1815         if (loc) {
1816                 int file_index = add_line_number_file_name (w, loc->source_file, 0, 0);
1817                 emit_uleb128 (w, file_index + 1);
1818                 emit_uleb128 (w, loc->row);
1819
1820                 mono_debug_free_source_location (loc);
1821                 loc = NULL;
1822         } else {
1823                 emit_uleb128 (w, 0);
1824                 emit_uleb128 (w, 0);
1825         }
1826 #ifndef TARGET_IOS
1827         emit_string (w, name);
1828 #endif
1829         g_free (name);
1830         if (start_symbol) {
1831                 emit_pointer_unaligned (w, start_symbol);
1832                 emit_pointer_unaligned (w, end_symbol);
1833         } else {
1834                 emit_pointer_value (w, code);
1835                 emit_pointer_value (w, code + code_size);
1836         }
1837         /* frame_base */
1838         emit_byte (w, 2);
1839         emit_byte (w, DW_OP_breg6);
1840         emit_byte (w, 16);
1841
1842         /* Parameters */
1843         for (i = 0; i < sig->param_count + sig->hasthis; ++i) {
1844                 MonoInst *arg = args ? args [i] : NULL;
1845                 MonoType *t;
1846                 const char *pname;
1847                 char pname_buf [128];
1848                 MonoMethodVar *vmv = NULL;
1849                 gboolean need_loclist = FALSE;
1850
1851                 vmv = find_vmv (cfg, arg);
1852                 if (code && vmv && (vmv->live_range_start || vmv->live_range_end))
1853                         need_loclist = TRUE;
1854
1855                 if (i == 0 && sig->hasthis) {
1856                         if (method->klass->valuetype)
1857                                 t = &method->klass->this_arg;
1858                         else
1859                                 t = &method->klass->byval_arg;
1860                         pname = "this";
1861                 } else {
1862                         t = sig->params [i - sig->hasthis];
1863                         pname = names [i - sig->hasthis];
1864                 }
1865
1866                 emit_uleb128 (w, need_loclist ? ABBREV_PARAM_LOCLIST : ABBREV_PARAM);
1867                 /* name */
1868                 if (pname[0] == '\0') {
1869                         sprintf (pname_buf, "param%d", i - sig->hasthis);
1870                         pname = pname_buf;
1871                 }
1872                 emit_string (w, pname);
1873                 /* type */
1874                 if (!arg || arg->flags & MONO_INST_IS_DEAD)
1875                         emit_var_type (w, &mono_defaults.int32_class->byval_arg);
1876                 else
1877                         emit_var_type (w, t);
1878
1879                 p = buf;
1880                 encode_var_location (w, arg, p, &p);
1881                 if (need_loclist) {
1882                         vmv->live_range_start = 0;
1883                         if (vmv->live_range_end == 0)
1884                                 /* FIXME: Uses made in calls are not recorded */
1885                                 vmv->live_range_end = code_size;
1886                         emit_loclist (w, arg, code + vmv->live_range_start, code + vmv->live_range_end, buf, p - buf);
1887                 } else {
1888                         emit_byte (w, p - buf);
1889                         emit_bytes (w, buf, p - buf);
1890                 }
1891         }               
1892         g_free (names);
1893
1894         /* Locals */
1895         locals_info = mono_debug_lookup_locals (method);
1896
1897         for (i = 0; i < header->num_locals; ++i) {
1898                 MonoInst *ins = locals [i];
1899                 char name_buf [128];
1900                 int j;
1901                 MonoMethodVar *vmv = NULL;
1902                 gboolean need_loclist = FALSE;
1903                 char *lname;
1904
1905                 /* ins->dreg no longer contains the original vreg */
1906                 vmv = find_vmv (cfg, ins);
1907                 if (code && vmv) {
1908                         if (vmv->live_range_start) {
1909                                 /* This variable has a precise live range */
1910                                 need_loclist = TRUE;
1911                         }
1912                 }
1913
1914                 emit_uleb128 (w, need_loclist ? ABBREV_VARIABLE_LOCLIST : ABBREV_VARIABLE);
1915                 /* name */
1916                 lname = NULL;
1917                 if (locals_info) {
1918                         for (j = 0; j < locals_info->num_locals; ++j)
1919                                 if (locals_info->locals [j].index == i)
1920                                         break;
1921                         if (j < locals_info->num_locals)
1922                                 lname = locals_info->locals [j].name;
1923                 }
1924                 if (lname) {
1925                         emit_string (w, lname);
1926                 } else {
1927                         sprintf (name_buf, "V_%d", i);
1928                         emit_string (w, name_buf);
1929                 }
1930                 /* type */
1931                 if (!ins || ins->flags & MONO_INST_IS_DEAD)
1932                         emit_var_type (w, &mono_defaults.int32_class->byval_arg);
1933                 else
1934                         emit_var_type (w, header->locals [i]);
1935
1936                 p = buf;
1937                 encode_var_location (w, ins, p, &p);
1938
1939                 if (need_loclist) {
1940                         if (vmv->live_range_end == 0)
1941                                 /* FIXME: Uses made in calls are not recorded */
1942                                 vmv->live_range_end = code_size;
1943                         emit_loclist (w, ins, code + vmv->live_range_start, code + vmv->live_range_end, buf, p - buf);
1944                 } else {
1945                         emit_byte (w, p - buf);
1946                         emit_bytes (w, buf, p - buf);
1947                 }
1948         }
1949
1950         if (locals_info)
1951                 mono_debug_free_locals (locals_info);
1952
1953         /* Subprogram end */
1954         emit_uleb128 (w, 0x0);
1955
1956         emit_line (w);
1957
1958         emit_debug_info_end (w);
1959
1960         /* Emit unwind info */
1961         if (unwind_info) {
1962                 emit_fde (w, w->fde_index, start_symbol, end_symbol, code, code_size, unwind_info, TRUE);
1963                 w->fde_index ++;
1964         }
1965
1966         /* Save the information needed to emit the line number info later at once */
1967         /* != could happen when using --regression */
1968         if (debug_info && (debug_info->code_start == code)) {
1969                 MethodLineNumberInfo *info;
1970
1971                 info = g_new0 (MethodLineNumberInfo, 1);
1972                 info->method = method;
1973                 info->start_symbol = g_strdup (start_symbol);
1974                 info->end_symbol = g_strdup (end_symbol);
1975                 info->code = code;
1976                 info->code_size = code_size;
1977                 w->line_info = g_slist_prepend (w->line_info, info);
1978         }
1979
1980         emit_line (w);
1981         mono_metadata_free_mh (header);
1982 }
1983
1984 void
1985 mono_dwarf_writer_emit_trampoline (MonoDwarfWriter *w, const char *tramp_name, char *start_symbol, char *end_symbol, guint8 *code, guint32 code_size, GSList *unwind_info)
1986 {
1987         emit_section_change (w, ".debug_info", 0);
1988
1989         /* Subprogram */
1990         emit_uleb128 (w, ABBREV_TRAMP_SUBPROGRAM);
1991         emit_string (w, tramp_name);
1992         emit_pointer_value (w, code);
1993         emit_pointer_value (w, code + code_size);
1994
1995         /* Subprogram end */
1996         emit_uleb128 (w, 0x0);
1997
1998         emit_debug_info_end (w);
1999
2000         /* Emit unwind info */
2001         emit_fde (w, w->fde_index, start_symbol, end_symbol, code, code_size, unwind_info, FALSE);
2002         w->fde_index ++;
2003 }
2004
2005 #else /* !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */
2006
2007 MONO_EMPTY_SOURCE_FILE (dwarfwriter);
2008
2009 #endif /* End of: !defined(DISABLE_AOT) && !defined(DISABLE_JIT) */