2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2011, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
25 #include <sys/types.h>
40 #include "dbghelp_private.h"
41 #include "image_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf
);
49 * o unspecified parameters
51 * o Debug{Start|End}Point
54 * o proper types loading (nesting)
58 static void dump(const void* ptr
, unsigned len
)
62 static const char hexof
[] = "0123456789abcdef";
65 for (i
= 0; i
< len
; i
+= 16)
67 sprintf(msg
, "%08x: ", i
);
68 memset(msg
+ 10, ' ', 3 * 16 + 1 + 16);
69 for (j
= 0; j
< min(16, len
- i
); j
++)
71 msg
[10 + 3 * j
+ 0] = hexof
[x
[i
+ j
] >> 4];
72 msg
[10 + 3 * j
+ 1] = hexof
[x
[i
+ j
] & 15];
73 msg
[10 + 3 * j
+ 2] = ' ';
74 msg
[10 + 3 * 16 + 1 + j
] = (x
[i
+ j
] >= 0x20 && x
[i
+ j
] < 0x7f) ?
77 msg
[10 + 3 * 16] = ' ';
78 msg
[10 + 3 * 16 + 1 + 16] = '\0';
87 * http://www.eagercon.com/dwarf/dwarf3std.htm
88 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
90 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
92 * example of projects who do dwarf2 parsing:
93 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
94 * http://elis.ugent.be/diota/log/ltrace_elf.c
102 typedef struct dwarf2_abbrev_entry_attr_s
106 struct dwarf2_abbrev_entry_attr_s
* next
;
107 } dwarf2_abbrev_entry_attr_t
;
109 typedef struct dwarf2_abbrev_entry_s
111 ULONG_PTR entry_code
;
113 unsigned char have_child
;
115 dwarf2_abbrev_entry_attr_t
* attrs
;
116 } dwarf2_abbrev_entry_t
;
121 const unsigned char* ptr
;
127 enum {attr_direct
, attr_abstract_origin
, attr_specification
} gotten_from
;
134 struct dwarf2_block block
;
136 const struct dwarf2_debug_info_s
* debug_info
;
139 typedef struct dwarf2_debug_info_s
141 const dwarf2_abbrev_entry_t
*abbrev
;
143 const unsigned char** data
;
144 struct vector children
;
145 struct dwarf2_debug_info_s
* parent
;
146 struct dwarf2_parse_context_s
* unit_ctx
;
147 } dwarf2_debug_info_t
;
149 typedef struct dwarf2_section_s
152 const unsigned char* address
;
157 enum dwarf2_sections
{section_debug
, section_string
, section_abbrev
, section_line
, section_ranges
, section_max
};
159 typedef struct dwarf2_traverse_context_s
161 const unsigned char* data
;
162 const unsigned char* end_data
;
163 } dwarf2_traverse_context_t
;
165 /* symt_cache indexes */
170 typedef struct dwarf2_cuhead_s
172 unsigned char word_size
; /* size of a word on target machine */
173 unsigned char version
;
174 unsigned char offset_size
; /* size of offset inside DWARF */
177 typedef struct dwarf2_parse_module_context_s
179 ULONG_PTR load_offset
;
180 const dwarf2_section_t
* sections
;
181 struct module
* module
;
182 const struct elf_thunk_area
*thunks
;
183 struct symt
* symt_cache
[sc_num
]; /* void, unknown */
184 struct vector unit_contexts
;
185 struct dwarf2_dwz_alternate_s
* dwz
;
187 } dwarf2_parse_module_context_t
;
189 typedef struct dwarf2_dwz_alternate_s
191 struct image_file_map
* fmap
;
192 dwarf2_section_t sections
[section_max
];
193 struct image_section_map sectmap
[section_max
];
194 dwarf2_parse_module_context_t module_ctx
;
195 } dwarf2_dwz_alternate_t
;
206 /* this is the context used for parsing a compilation unit
207 * inside an ELF/PE section (likely .debug_info)
209 typedef struct dwarf2_parse_context_s
211 dwarf2_parse_module_context_t
* module_ctx
;
214 struct symt_compiland
* compiland
;
215 struct sparse_array abbrev_table
;
216 struct sparse_array debug_info_table
;
217 ULONG_PTR ref_offset
;
219 dwarf2_cuhead_t head
;
220 enum unit_status status
;
221 dwarf2_traverse_context_t traverse_DIE
;
222 } dwarf2_parse_context_t
;
224 /* stored in the dbghelp's module internal structure for later reuse */
225 struct dwarf2_module_info_s
227 dwarf2_cuhead_t
** cuheads
;
228 unsigned num_cuheads
;
229 dwarf2_section_t debug_loc
;
230 dwarf2_section_t debug_frame
;
231 dwarf2_section_t eh_frame
;
232 unsigned char word_size
;
235 #define loc_dwarf2_location_list (loc_user + 0)
236 #define loc_dwarf2_block (loc_user + 1)
237 #define loc_dwarf2_frame_cfa (loc_user + 2)
239 /* forward declarations */
240 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_debug_info_t
* entry
);
241 static BOOL
dwarf2_parse_compilation_unit(dwarf2_parse_context_t
* ctx
);
242 static dwarf2_parse_context_t
* dwarf2_locate_cu(dwarf2_parse_module_context_t
* module_ctx
, ULONG_PTR ref
);
244 static unsigned char dwarf2_get_byte(const unsigned char* ptr
)
249 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t
* ctx
)
251 unsigned char uvalue
= dwarf2_get_byte(ctx
->data
);
256 static unsigned short dwarf2_get_u2(const unsigned char* ptr
)
258 return *(const UINT16
*)ptr
;
261 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t
* ctx
)
263 unsigned short uvalue
= dwarf2_get_u2(ctx
->data
);
268 static ULONG_PTR
dwarf2_get_u4(const unsigned char* ptr
)
270 return *(const UINT32
*)ptr
;
273 static ULONG_PTR
dwarf2_parse_u4(dwarf2_traverse_context_t
* ctx
)
275 ULONG_PTR uvalue
= dwarf2_get_u4(ctx
->data
);
280 static DWORD64
dwarf2_get_u8(const unsigned char* ptr
)
282 return *(const UINT64
*)ptr
;
285 static DWORD64
dwarf2_parse_u8(dwarf2_traverse_context_t
* ctx
)
287 DWORD64 uvalue
= dwarf2_get_u8(ctx
->data
);
292 static ULONG_PTR
dwarf2_get_leb128_as_unsigned(const unsigned char* ptr
, const unsigned char** end
)
300 byte
= dwarf2_get_byte(ptr
++);
301 ret
|= (byte
& 0x7f) << shift
;
303 } while (byte
& 0x80);
309 static ULONG_PTR
dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t
* ctx
)
315 ret
= dwarf2_get_leb128_as_unsigned(ctx
->data
, &ctx
->data
);
320 static LONG_PTR
dwarf2_get_leb128_as_signed(const unsigned char* ptr
, const unsigned char** end
)
325 const unsigned size
= sizeof(int) * 8;
329 byte
= dwarf2_get_byte(ptr
++);
330 ret
|= (byte
& 0x7f) << shift
;
332 } while (byte
& 0x80);
335 /* as spec: sign bit of byte is 2nd high order bit (80x40)
336 * -> 0x80 is used as flag.
338 if ((shift
< size
) && (byte
& 0x40))
340 ret
|= - (1 << shift
);
345 static LONG_PTR
dwarf2_leb128_as_signed(dwarf2_traverse_context_t
* ctx
)
351 ret
= dwarf2_get_leb128_as_signed(ctx
->data
, &ctx
->data
);
355 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t
* ctx
)
358 for (ret
= 0; ctx
->data
[ret
] & 0x80; ret
++);
362 /******************************************************************
365 * Returns an address.
366 * We assume that in all cases word size from Dwarf matches the size of
367 * addresses in platform where the exec is compiled.
369 static ULONG_PTR
dwarf2_get_addr(const unsigned char* ptr
, unsigned word_size
)
376 ret
= dwarf2_get_u4(ptr
);
379 ret
= dwarf2_get_u8(ptr
);
382 FIXME("Unsupported Word Size %u\n", word_size
);
388 static inline ULONG_PTR
dwarf2_parse_addr(dwarf2_traverse_context_t
* ctx
, unsigned word_size
)
390 ULONG_PTR ret
= dwarf2_get_addr(ctx
->data
, word_size
);
391 ctx
->data
+= word_size
;
395 static inline ULONG_PTR
dwarf2_parse_addr_head(dwarf2_traverse_context_t
* ctx
, const dwarf2_cuhead_t
* head
)
397 return dwarf2_parse_addr(ctx
, head
->word_size
);
400 static ULONG_PTR
dwarf2_parse_offset(dwarf2_traverse_context_t
* ctx
, unsigned char offset_size
)
402 ULONG_PTR ret
= dwarf2_get_addr(ctx
->data
, offset_size
);
403 ctx
->data
+= offset_size
;
407 static ULONG_PTR
dwarf2_parse_3264(dwarf2_traverse_context_t
* ctx
, unsigned char* ofsz
)
409 ULONG_PTR ret
= dwarf2_parse_u4(ctx
);
410 if (ret
== 0xffffffff)
412 ret
= dwarf2_parse_u8(ctx
);
419 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t
* ctx
)
421 return wine_dbg_sprintf("ctx(%p)", ctx
->data
);
424 static const char* dwarf2_debug_unit_ctx(const dwarf2_parse_context_t
* ctx
)
426 return wine_dbg_sprintf("ctx(%p,%s)",
427 ctx
, debugstr_w(ctx
->module_ctx
->module
->modulename
));
430 static const char* dwarf2_debug_di(const dwarf2_debug_info_t
* di
)
432 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p) in %s",
433 di
->abbrev
, di
->symt
, dwarf2_debug_unit_ctx(di
->unit_ctx
));
436 static dwarf2_abbrev_entry_t
*
437 dwarf2_abbrev_table_find_entry(const struct sparse_array
* abbrev_table
,
438 ULONG_PTR entry_code
)
440 assert( NULL
!= abbrev_table
);
441 return sparse_array_find(abbrev_table
, entry_code
);
444 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t
* abbrev_ctx
,
445 struct sparse_array
* abbrev_table
,
448 ULONG_PTR entry_code
;
449 dwarf2_abbrev_entry_t
* abbrev_entry
;
450 dwarf2_abbrev_entry_attr_t
* new = NULL
;
451 dwarf2_abbrev_entry_attr_t
* last
= NULL
;
455 assert( NULL
!= abbrev_ctx
);
457 TRACE("%s, end at %p\n",
458 dwarf2_debug_traverse_ctx(abbrev_ctx
), abbrev_ctx
->end_data
);
460 sparse_array_init(abbrev_table
, sizeof(dwarf2_abbrev_entry_t
), 32);
461 while (abbrev_ctx
->data
< abbrev_ctx
->end_data
)
463 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
464 entry_code
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
465 TRACE("found entry_code %lu\n", entry_code
);
468 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
471 abbrev_entry
= sparse_array_add(abbrev_table
, entry_code
, pool
);
472 assert( NULL
!= abbrev_entry
);
474 abbrev_entry
->entry_code
= entry_code
;
475 abbrev_entry
->tag
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
476 abbrev_entry
->have_child
= dwarf2_parse_byte(abbrev_ctx
);
477 abbrev_entry
->attrs
= NULL
;
478 abbrev_entry
->num_attr
= 0;
480 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
481 abbrev_table
, sparse_array_length(abbrev_table
),
482 entry_code
, abbrev_entry
->tag
, abbrev_entry
->have_child
, abbrev_entry
);
487 attribute
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
488 form
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
489 if (!attribute
) break;
491 new = pool_alloc(pool
, sizeof(dwarf2_abbrev_entry_attr_t
));
494 new->attribute
= attribute
;
497 if (abbrev_entry
->attrs
) last
->next
= new;
498 else abbrev_entry
->attrs
= new;
500 abbrev_entry
->num_attr
++;
503 TRACE("found %u entries\n", sparse_array_length(abbrev_table
));
506 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t
* ctx
,
507 const dwarf2_cuhead_t
* head
,
508 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
)
512 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr
->attribute
, abbrev_attr
->form
);
514 switch (abbrev_attr
->form
)
516 case DW_FORM_flag_present
: step
= 0; break;
517 case DW_FORM_ref_addr
: step
= (head
->version
>= 3) ? head
->offset_size
: head
->word_size
; break;
518 case DW_FORM_addr
: step
= head
->word_size
; break;
521 case DW_FORM_ref1
: step
= 1; break;
523 case DW_FORM_ref2
: step
= 2; break;
525 case DW_FORM_ref4
: step
= 4; break;
527 case DW_FORM_ref8
: step
= 8; break;
529 case DW_FORM_ref_udata
:
530 case DW_FORM_udata
: step
= dwarf2_leb128_length(ctx
); break;
531 case DW_FORM_string
: step
= strlen((const char*)ctx
->data
) + 1; break;
532 case DW_FORM_exprloc
:
533 case DW_FORM_block
: step
= dwarf2_leb128_as_unsigned(ctx
); break;
534 case DW_FORM_block1
: step
= dwarf2_parse_byte(ctx
); break;
535 case DW_FORM_block2
: step
= dwarf2_parse_u2(ctx
); break;
536 case DW_FORM_block4
: step
= dwarf2_parse_u4(ctx
); break;
537 case DW_FORM_sec_offset
:
538 case DW_FORM_GNU_ref_alt
:
539 case DW_FORM_GNU_strp_alt
:
540 case DW_FORM_strp
: step
= head
->offset_size
; break;
542 FIXME("Unhandled attribute form %lx\n", abbrev_attr
->form
);
548 static BOOL
dwarf2_fill_attr(const dwarf2_parse_context_t
* ctx
,
549 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
,
550 const unsigned char* data
,
551 struct attribute
* attr
)
553 attr
->form
= abbrev_attr
->form
;
556 case DW_FORM_ref_addr
:
557 if (ctx
->head
.version
>= 3)
558 attr
->u
.uvalue
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
560 attr
->u
.uvalue
= dwarf2_get_addr(data
, ctx
->head
.word_size
);
561 TRACE("addr<0x%lx>\n", attr
->u
.uvalue
);
565 attr
->u
.uvalue
= dwarf2_get_addr(data
, ctx
->head
.word_size
);
566 TRACE("addr<0x%lx>\n", attr
->u
.uvalue
);
570 attr
->u
.uvalue
= dwarf2_get_byte(data
);
571 TRACE("flag<0x%lx>\n", attr
->u
.uvalue
);
574 case DW_FORM_flag_present
:
576 TRACE("flag_present\n");
580 attr
->u
.uvalue
= dwarf2_get_byte(data
);
581 TRACE("data1<%lu>\n", attr
->u
.uvalue
);
585 attr
->u
.uvalue
= dwarf2_get_u2(data
);
586 TRACE("data2<%lu>\n", attr
->u
.uvalue
);
590 attr
->u
.uvalue
= dwarf2_get_u4(data
);
591 TRACE("data4<%lu>\n", attr
->u
.uvalue
);
595 attr
->u
.lluvalue
= dwarf2_get_u8(data
);
596 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr
->u
.uvalue
));
600 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_byte(data
);
601 TRACE("ref1<0x%lx>\n", attr
->u
.uvalue
);
605 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u2(data
);
606 TRACE("ref2<0x%lx>\n", attr
->u
.uvalue
);
610 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u4(data
);
611 TRACE("ref4<0x%lx>\n", attr
->u
.uvalue
);
615 FIXME("Unhandled 64-bit support\n");
619 attr
->u
.svalue
= dwarf2_get_leb128_as_signed(data
, NULL
);
622 case DW_FORM_ref_udata
:
623 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_leb128_as_unsigned(data
, NULL
);
624 TRACE("ref_udata<0x%lx>\n", attr
->u
.uvalue
);
628 attr
->u
.uvalue
= dwarf2_get_leb128_as_unsigned(data
, NULL
);
629 TRACE("udata<0x%lx>\n", attr
->u
.uvalue
);
633 attr
->u
.string
= (const char *)data
;
634 TRACE("string<%s>\n", debugstr_a(attr
->u
.string
));
639 ULONG_PTR ofs
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
640 if (ofs
>= ctx
->module_ctx
->sections
[section_string
].size
)
642 ERR("Out of bounds string offset (%08lx)\n", ofs
);
643 attr
->u
.string
= "<<outofbounds-strp>>";
647 attr
->u
.string
= (const char*)ctx
->module_ctx
->sections
[section_string
].address
+ ofs
;
648 TRACE("strp<%s>\n", debugstr_a(attr
->u
.string
));
654 case DW_FORM_exprloc
:
655 attr
->u
.block
.size
= dwarf2_get_leb128_as_unsigned(data
, &attr
->u
.block
.ptr
);
656 TRACE("block<%p,%u>\n", attr
->u
.block
.ptr
, attr
->u
.block
.size
);
660 attr
->u
.block
.size
= dwarf2_get_byte(data
);
661 attr
->u
.block
.ptr
= data
+ 1;
662 TRACE("block<%p,%u>\n", attr
->u
.block
.ptr
, attr
->u
.block
.size
);
666 attr
->u
.block
.size
= dwarf2_get_u2(data
);
667 attr
->u
.block
.ptr
= data
+ 2;
668 TRACE("block<%p,%u>\n", attr
->u
.block
.ptr
, attr
->u
.block
.size
);
672 attr
->u
.block
.size
= dwarf2_get_u4(data
);
673 attr
->u
.block
.ptr
= data
+ 4;
674 TRACE("block<%p,%u>\n", attr
->u
.block
.ptr
, attr
->u
.block
.size
);
677 case DW_FORM_sec_offset
:
678 attr
->u
.lluvalue
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
679 TRACE("sec_offset<%s>\n", wine_dbgstr_longlong(attr
->u
.lluvalue
));
682 case DW_FORM_GNU_ref_alt
:
683 if (!ctx
->module_ctx
->dwz
)
685 ERR("No DWZ file present for GNU_ref_alt in %s\n", debugstr_w(ctx
->module_ctx
->module
->modulename
));
689 attr
->u
.uvalue
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
690 TRACE("ref_alt<0x%lx>\n", attr
->u
.uvalue
);
693 case DW_FORM_GNU_strp_alt
:
694 if (ctx
->module_ctx
->dwz
)
696 ULONG_PTR ofs
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
697 if (ofs
< ctx
->module_ctx
->dwz
->sections
[section_string
].size
)
699 attr
->u
.string
= (const char*)ctx
->module_ctx
->dwz
->sections
[section_string
].address
+ ofs
;
700 TRACE("strp_alt<%s>\n", debugstr_a(attr
->u
.string
));
704 ERR("out of bounds strp_alt: 0x%lx 0x%x (%u)\n", ofs
, ctx
->module_ctx
->dwz
->sections
[section_string
].size
, ctx
->head
.offset_size
);
705 attr
->u
.string
= "<<outofbounds-strpalt>>";
710 ERR("No DWZ file present for GNU_strp_alt in %s\n", debugstr_w(ctx
->module_ctx
->module
->modulename
));
711 attr
->u
.string
= "<<noDWZ-strpalt>>";
716 FIXME("Unhandled attribute form %lx\n", abbrev_attr
->form
);
722 static dwarf2_debug_info_t
* dwarf2_jump_to_debug_info(struct attribute
* attr
);
724 static BOOL
dwarf2_find_attribute(const dwarf2_debug_info_t
* di
,
725 unsigned at
, struct attribute
* attr
)
727 unsigned i
, refidx
= 0;
728 dwarf2_abbrev_entry_attr_t
* abbrev_attr
;
729 dwarf2_abbrev_entry_attr_t
* ref_abbrev_attr
= NULL
;
731 attr
->gotten_from
= attr_direct
;
734 ref_abbrev_attr
= NULL
;
735 attr
->debug_info
= di
;
736 for (i
= 0, abbrev_attr
= di
->abbrev
->attrs
; abbrev_attr
; i
++, abbrev_attr
= abbrev_attr
->next
)
738 if (abbrev_attr
->attribute
== at
)
740 return dwarf2_fill_attr(di
->unit_ctx
, abbrev_attr
, di
->data
[i
], attr
);
742 if ((abbrev_attr
->attribute
== DW_AT_abstract_origin
||
743 abbrev_attr
->attribute
== DW_AT_specification
) &&
747 FIXME("two references %lx and %lx\n", ref_abbrev_attr
->attribute
, abbrev_attr
->attribute
);
748 ref_abbrev_attr
= abbrev_attr
;
750 attr
->gotten_from
= (abbrev_attr
->attribute
== DW_AT_abstract_origin
) ?
751 attr_abstract_origin
: attr_specification
;
754 /* do we have either an abstract origin or a specification debug entry to look into ? */
755 if (!ref_abbrev_attr
|| !dwarf2_fill_attr(di
->unit_ctx
, ref_abbrev_attr
, di
->data
[refidx
], attr
))
757 if (!(di
= dwarf2_jump_to_debug_info(attr
)))
759 FIXME("Should have found the debug info entry\n");
766 static dwarf2_debug_info_t
* dwarf2_jump_to_debug_info(struct attribute
* attr
)
768 dwarf2_parse_context_t
* ref_ctx
= NULL
;
769 BOOL with_other
= TRUE
;
770 dwarf2_debug_info_t
* ret
;
774 case DW_FORM_ref_addr
:
775 ref_ctx
= dwarf2_locate_cu(attr
->debug_info
->unit_ctx
->module_ctx
, attr
->u
.uvalue
);
777 case DW_FORM_GNU_ref_alt
:
778 if (attr
->debug_info
->unit_ctx
->module_ctx
->dwz
)
779 ref_ctx
= dwarf2_locate_cu(&attr
->debug_info
->unit_ctx
->module_ctx
->dwz
->module_ctx
, attr
->u
.uvalue
);
783 ref_ctx
= attr
->debug_info
->unit_ctx
;
786 if (!ref_ctx
) return FALSE
;
787 /* There are cases where we end up with a circular reference between two (or more)
788 * compilation units. Before this happens, try to see if we can refer to an already
789 * loaded debug_info in the target compilation unit (even if all the debug_info
790 * haven't been loaded yet).
792 if (ref_ctx
->status
== UNIT_BEINGLOADED
&&
793 (ret
= sparse_array_find(&ref_ctx
->debug_info_table
, attr
->u
.uvalue
)))
797 /* ensure CU is fully loaded */
798 if (ref_ctx
!= attr
->debug_info
->unit_ctx
&& !dwarf2_parse_compilation_unit(ref_ctx
))
801 return sparse_array_find(&ref_ctx
->debug_info_table
, attr
->u
.uvalue
);
804 static void dwarf2_load_one_entry(dwarf2_debug_info_t
*);
806 #define Wine_DW_no_register 0x7FFFFFFF
808 static unsigned dwarf2_map_register(int regno
, const struct module
* module
)
810 if (regno
== Wine_DW_no_register
)
812 FIXME("What the heck map reg 0x%x\n",regno
);
815 return module
->cpu
->map_dwarf_register(regno
, module
, FALSE
);
818 static enum location_error
819 compute_location(const struct module
*module
, const dwarf2_cuhead_t
* head
,
820 dwarf2_traverse_context_t
* ctx
, struct location
* loc
,
821 HANDLE hproc
, const struct location
* frame
)
823 DWORD_PTR tmp
, stack
[64];
826 BOOL piece_found
= FALSE
;
830 loc
->kind
= loc_absolute
;
831 loc
->reg
= Wine_DW_no_register
;
833 while (ctx
->data
< ctx
->end_data
)
835 op
= dwarf2_parse_byte(ctx
);
837 if (op
>= DW_OP_lit0
&& op
<= DW_OP_lit31
)
838 stack
[++stk
] = op
- DW_OP_lit0
;
839 else if (op
>= DW_OP_reg0
&& op
<= DW_OP_reg31
)
841 /* dbghelp APIs don't know how to cope with this anyway
842 * (for example 'long long' stored in two registers)
843 * FIXME: We should tell winedbg how to deal with it (sigh)
847 DWORD cvreg
= dwarf2_map_register(op
- DW_OP_reg0
, module
);
848 if (loc
->reg
!= Wine_DW_no_register
)
849 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
850 module
->cpu
->fetch_regname(loc
->reg
), loc
->reg
,
851 module
->cpu
->fetch_regname(cvreg
), cvreg
);
854 loc
->kind
= loc_register
;
856 else if (op
>= DW_OP_breg0
&& op
<= DW_OP_breg31
)
858 /* dbghelp APIs don't know how to cope with this anyway
859 * (for example 'long long' stored in two registers)
860 * FIXME: We should tell winedbg how to deal with it (sigh)
864 DWORD cvreg
= dwarf2_map_register(op
- DW_OP_breg0
, module
);
865 if (loc
->reg
!= Wine_DW_no_register
)
866 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
867 module
->cpu
->fetch_regname(loc
->reg
), loc
->reg
,
868 module
->cpu
->fetch_regname(cvreg
), cvreg
);
871 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
872 loc
->kind
= loc_regrel
;
876 case DW_OP_nop
: break;
877 case DW_OP_addr
: stack
[++stk
] = dwarf2_parse_addr_head(ctx
, head
); break;
878 case DW_OP_const1u
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
879 case DW_OP_const1s
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
880 case DW_OP_const2u
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
881 case DW_OP_const2s
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
882 case DW_OP_const4u
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
883 case DW_OP_const4s
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
884 case DW_OP_const8u
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
885 case DW_OP_const8s
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
886 case DW_OP_constu
: stack
[++stk
] = dwarf2_leb128_as_unsigned(ctx
); break;
887 case DW_OP_consts
: stack
[++stk
] = dwarf2_leb128_as_signed(ctx
); break;
888 case DW_OP_dup
: stack
[stk
+ 1] = stack
[stk
]; stk
++; break;
889 case DW_OP_drop
: stk
--; break;
890 case DW_OP_over
: stack
[stk
+ 1] = stack
[stk
- 1]; stk
++; break;
891 case DW_OP_pick
: stack
[stk
+ 1] = stack
[stk
- dwarf2_parse_byte(ctx
)]; stk
++; break;
892 case DW_OP_swap
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = tmp
; break;
893 case DW_OP_rot
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = stack
[stk
-2]; stack
[stk
-2] = tmp
; break;
894 case DW_OP_abs
: stack
[stk
] = sizeof(stack
[stk
]) == 8 ? llabs((INT64
)stack
[stk
]) : abs((INT32
)stack
[stk
]); break;
895 case DW_OP_neg
: stack
[stk
] = -stack
[stk
]; break;
896 case DW_OP_not
: stack
[stk
] = ~stack
[stk
]; break;
897 case DW_OP_and
: stack
[stk
-1] &= stack
[stk
]; stk
--; break;
898 case DW_OP_or
: stack
[stk
-1] |= stack
[stk
]; stk
--; break;
899 case DW_OP_minus
: stack
[stk
-1] -= stack
[stk
]; stk
--; break;
900 case DW_OP_mul
: stack
[stk
-1] *= stack
[stk
]; stk
--; break;
901 case DW_OP_plus
: stack
[stk
-1] += stack
[stk
]; stk
--; break;
902 case DW_OP_xor
: stack
[stk
-1] ^= stack
[stk
]; stk
--; break;
903 case DW_OP_shl
: stack
[stk
-1] <<= stack
[stk
]; stk
--; break;
904 case DW_OP_shr
: stack
[stk
-1] >>= stack
[stk
]; stk
--; break;
905 case DW_OP_plus_uconst
: stack
[stk
] += dwarf2_leb128_as_unsigned(ctx
); break;
906 case DW_OP_shra
: stack
[stk
-1] = stack
[stk
-1] / (1 << stack
[stk
]); stk
--; break;
907 case DW_OP_div
: stack
[stk
-1] = stack
[stk
-1] / stack
[stk
]; stk
--; break;
908 case DW_OP_mod
: stack
[stk
-1] = stack
[stk
-1] % stack
[stk
]; stk
--; break;
909 case DW_OP_ge
: stack
[stk
-1] = (stack
[stk
-1] >= stack
[stk
]); stk
--; break;
910 case DW_OP_gt
: stack
[stk
-1] = (stack
[stk
-1] > stack
[stk
]); stk
--; break;
911 case DW_OP_le
: stack
[stk
-1] = (stack
[stk
-1] <= stack
[stk
]); stk
--; break;
912 case DW_OP_lt
: stack
[stk
-1] = (stack
[stk
-1] < stack
[stk
]); stk
--; break;
913 case DW_OP_eq
: stack
[stk
-1] = (stack
[stk
-1] == stack
[stk
]); stk
--; break;
914 case DW_OP_ne
: stack
[stk
-1] = (stack
[stk
-1] != stack
[stk
]); stk
--; break;
915 case DW_OP_skip
: tmp
= dwarf2_parse_u2(ctx
); ctx
->data
+= tmp
; break;
917 tmp
= dwarf2_parse_u2(ctx
);
918 if (!stack
[stk
--]) ctx
->data
+= tmp
;
921 tmp
= dwarf2_leb128_as_unsigned(ctx
);
924 if (loc
->reg
!= Wine_DW_no_register
)
925 FIXME("Only supporting one reg\n");
926 loc
->reg
= dwarf2_map_register(tmp
, module
);
928 loc
->kind
= loc_register
;
931 tmp
= dwarf2_leb128_as_unsigned(ctx
);
932 if (loc
->reg
!= Wine_DW_no_register
)
933 FIXME("Only supporting one regx\n");
934 loc
->reg
= dwarf2_map_register(tmp
, module
);
935 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
936 loc
->kind
= loc_regrel
;
939 if (loc
->reg
!= Wine_DW_no_register
)
940 FIXME("Only supporting one reg (%s/%d -> -2)\n",
941 module
->cpu
->fetch_regname(loc
->reg
), loc
->reg
);
942 if (frame
&& frame
->kind
== loc_register
)
944 loc
->kind
= loc_regrel
;
945 loc
->reg
= frame
->reg
;
946 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
948 else if (frame
&& frame
->kind
== loc_regrel
)
950 loc
->kind
= loc_regrel
;
951 loc
->reg
= frame
->reg
;
952 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
) + frame
->offset
;
956 /* FIXME: this could be later optimized by not recomputing
957 * this very location expression
959 loc
->kind
= loc_dwarf2_block
;
960 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
965 unsigned sz
= dwarf2_leb128_as_unsigned(ctx
);
966 WARN("Not handling OP_piece (size=%d)\n", sz
);
973 FIXME("Unexpected empty stack\n");
974 return loc_err_internal
;
976 if (loc
->reg
!= Wine_DW_no_register
)
978 WARN("Too complex expression for deref\n");
979 return loc_err_too_complex
;
983 DWORD_PTR addr
= stack
[stk
--];
986 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, head
->word_size
, NULL
))
988 WARN("Couldn't read memory at %lx\n", addr
);
989 return loc_err_cant_read
;
991 stack
[++stk
] = deref
;
995 loc
->kind
= loc_dwarf2_block
;
998 case DW_OP_deref_size
:
1001 FIXME("Unexpected empty stack\n");
1002 return loc_err_internal
;
1004 if (loc
->reg
!= Wine_DW_no_register
)
1006 WARN("Too complex expression for deref\n");
1007 return loc_err_too_complex
;
1011 DWORD_PTR addr
= stack
[stk
--];
1012 BYTE derefsize
= dwarf2_parse_byte(ctx
);
1015 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, derefsize
, NULL
))
1017 WARN("Couldn't read memory at %lx\n", addr
);
1018 return loc_err_cant_read
;
1023 case 1: stack
[++stk
] = *(unsigned char*)&deref
; break;
1024 case 2: stack
[++stk
] = *(unsigned short*)&deref
; break;
1025 case 4: stack
[++stk
] = *(DWORD
*)&deref
; break;
1026 case 8: if (head
->word_size
>= derefsize
) stack
[++stk
] = deref
; break;
1031 dwarf2_parse_byte(ctx
);
1032 loc
->kind
= loc_dwarf2_block
;
1035 case DW_OP_stack_value
:
1036 /* Expected behaviour is that this is the last instruction of this
1037 * expression and just the "top of stack" value should be put to loc->offset. */
1040 if (op
< DW_OP_lo_user
) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
1041 FIXME("Unhandled attr op: %x\n", op
);
1042 /* FIXME else unhandled extension */
1043 return loc_err_internal
;
1046 loc
->offset
= stack
[stk
];
1050 static BOOL
dwarf2_compute_location_attr(dwarf2_parse_context_t
* ctx
,
1051 const dwarf2_debug_info_t
* di
,
1053 struct location
* loc
,
1054 const struct location
* frame
)
1056 struct attribute xloc
;
1058 if (!dwarf2_find_attribute(di
, dw
, &xloc
)) return FALSE
;
1063 if (ctx
->head
.version
< 4)
1065 loc
->kind
= loc_dwarf2_location_list
;
1066 loc
->reg
= Wine_DW_no_register
;
1067 loc
->offset
= xloc
.u
.uvalue
;
1071 case DW_FORM_data1
: case DW_FORM_data2
:
1072 case DW_FORM_udata
: case DW_FORM_sdata
:
1073 loc
->kind
= loc_absolute
;
1075 loc
->offset
= xloc
.u
.uvalue
;
1078 if (ctx
->head
.version
>= 4)
1080 loc
->kind
= loc_absolute
;
1082 loc
->offset
= xloc
.u
.lluvalue
;
1086 case DW_FORM_sec_offset
:
1087 loc
->kind
= loc_dwarf2_location_list
;
1088 loc
->reg
= Wine_DW_no_register
;
1089 loc
->offset
= xloc
.u
.lluvalue
;
1092 case DW_FORM_block1
:
1093 case DW_FORM_block2
:
1094 case DW_FORM_block4
:
1095 case DW_FORM_exprloc
:
1097 default: FIXME("Unsupported yet form %lx\n", xloc
.form
);
1101 /* assume we have a block form */
1102 if (dw
== DW_AT_frame_base
&& xloc
.u
.block
.size
== 1 && *xloc
.u
.block
.ptr
== DW_OP_call_frame_cfa
)
1104 loc
->kind
= loc_dwarf2_frame_cfa
;
1105 loc
->reg
= Wine_DW_no_register
;
1108 else if (xloc
.u
.block
.size
)
1110 dwarf2_traverse_context_t lctx
;
1111 enum location_error err
;
1113 lctx
.data
= xloc
.u
.block
.ptr
;
1114 lctx
.end_data
= xloc
.u
.block
.ptr
+ xloc
.u
.block
.size
;
1116 err
= compute_location(ctx
->module_ctx
->module
, &ctx
->head
, &lctx
, loc
, NULL
, frame
);
1119 loc
->kind
= loc_error
;
1122 else if (loc
->kind
== loc_dwarf2_block
)
1124 unsigned* ptr
= pool_alloc(&ctx
->module_ctx
->module
->pool
,
1125 sizeof(unsigned) + xloc
.u
.block
.size
);
1126 *ptr
= xloc
.u
.block
.size
;
1127 memcpy(ptr
+ 1, xloc
.u
.block
.ptr
, xloc
.u
.block
.size
);
1128 loc
->offset
= (ULONG_PTR
)ptr
;
1134 static struct symt
* dwarf2_lookup_type(const dwarf2_debug_info_t
* di
)
1136 struct attribute attr
;
1137 dwarf2_debug_info_t
* type
;
1139 if (!dwarf2_find_attribute(di
, DW_AT_type
, &attr
))
1140 /* this is only valid if current language of CU is C or C++ */
1141 return di
->unit_ctx
->module_ctx
->symt_cache
[sc_void
];
1142 if (!(type
= dwarf2_jump_to_debug_info(&attr
)))
1143 return di
->unit_ctx
->module_ctx
->symt_cache
[sc_unknown
];
1147 FIXME("Reference to itself\n");
1148 return di
->unit_ctx
->module_ctx
->symt_cache
[sc_unknown
];
1152 /* load the debug info entity */
1153 dwarf2_load_one_entry(type
);
1156 FIXME("Unable to load forward reference for tag %lx\n", type
->abbrev
->tag
);
1157 return di
->unit_ctx
->module_ctx
->symt_cache
[sc_unknown
];
1163 static const char* dwarf2_get_cpp_name(dwarf2_debug_info_t
* di
, const char* name
)
1166 struct attribute diname
;
1167 struct attribute spec
;
1169 if (di
->abbrev
->tag
== DW_TAG_compile_unit
|| di
->abbrev
->tag
== DW_TAG_partial_unit
) return name
;
1171 /* if the di is a definition, but has also a (previous) declaration, then scope must
1172 * be gotten from declaration not definition
1174 if (dwarf2_find_attribute(di
, DW_AT_specification
, &spec
) && spec
.gotten_from
== attr_direct
)
1176 di
= dwarf2_jump_to_debug_info(&spec
);
1179 FIXME("Should have found the debug info entry\n");
1184 if (!di
->unit_ctx
->cpp_name
)
1185 di
->unit_ctx
->cpp_name
= pool_alloc(&di
->unit_ctx
->pool
, MAX_SYM_NAME
);
1186 last
= di
->unit_ctx
->cpp_name
+ MAX_SYM_NAME
- strlen(name
) - 1;
1189 for (di
= di
->parent
; di
; di
= di
->parent
)
1191 switch (di
->abbrev
->tag
)
1193 case DW_TAG_namespace
:
1194 case DW_TAG_structure_type
:
1195 case DW_TAG_class_type
:
1196 case DW_TAG_interface_type
:
1197 case DW_TAG_union_type
:
1198 if (dwarf2_find_attribute(di
, DW_AT_name
, &diname
))
1200 size_t len
= strlen(diname
.u
.string
);
1202 if (last
< di
->unit_ctx
->cpp_name
) return NULL
;
1203 memcpy(last
, diname
.u
.string
, len
);
1204 last
[len
] = last
[len
+ 1] = ':';
1214 /******************************************************************
1217 * read a range for a given debug_info (either using AT_range attribute, in which
1218 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1219 * in all cases, range is relative to beginning of compilation unit
1221 static BOOL
dwarf2_read_range(dwarf2_parse_context_t
* ctx
, const dwarf2_debug_info_t
* di
,
1222 ULONG_PTR
* plow
, ULONG_PTR
* phigh
)
1224 struct attribute range
;
1226 if (dwarf2_find_attribute(di
, DW_AT_ranges
, &range
))
1228 dwarf2_traverse_context_t traverse
;
1229 ULONG_PTR low
, high
;
1230 const ULONG_PTR UMAX
= ~(ULONG_PTR
)0u;
1232 traverse
.data
= ctx
->module_ctx
->sections
[section_ranges
].address
+ range
.u
.uvalue
;
1233 traverse
.end_data
= ctx
->module_ctx
->sections
[section_ranges
].address
+
1234 ctx
->module_ctx
->sections
[section_ranges
].size
;
1238 while (traverse
.data
+ 2 * ctx
->head
.word_size
< traverse
.end_data
)
1240 low
= dwarf2_parse_addr_head(&traverse
, &ctx
->head
);
1241 high
= dwarf2_parse_addr_head(&traverse
, &ctx
->head
);
1242 if (low
== 0 && high
== 0) break;
1243 if (low
== (ctx
->head
.word_size
== 8 ? (~(DWORD64
)0u) : (DWORD64
)(~0u)))
1244 FIXME("unsupported yet (base address selection)\n");
1245 /* range values are relative to start of compilation unit */
1246 low
+= ctx
->compiland
->address
- ctx
->module_ctx
->load_offset
;
1247 high
+= ctx
->compiland
->address
- ctx
->module_ctx
->load_offset
;
1248 if (low
< *plow
) *plow
= low
;
1249 if (high
> *phigh
) *phigh
= high
;
1251 if (*plow
== UMAX
|| *phigh
== 0) {FIXME("no entry found\n"); return FALSE
;}
1252 if (*plow
== *phigh
) {WARN("entry found, but low=high %lx %lx\n", low
, high
); return FALSE
;}
1258 struct attribute low_pc
;
1259 struct attribute high_pc
;
1261 if (!dwarf2_find_attribute(di
, DW_AT_low_pc
, &low_pc
) ||
1262 !dwarf2_find_attribute(di
, DW_AT_high_pc
, &high_pc
))
1264 *plow
= low_pc
.u
.uvalue
;
1265 *phigh
= high_pc
.u
.uvalue
;
1266 if (ctx
->head
.version
>= 4)
1267 switch (high_pc
.form
)
1277 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1281 FIXME("Unsupported class for high_pc\n");
1288 static struct addr_range
* dwarf2_get_ranges(const dwarf2_debug_info_t
* di
, unsigned* num_ranges
)
1290 struct attribute range
;
1291 struct addr_range
* ranges
;
1293 if (dwarf2_find_attribute(di
, DW_AT_ranges
, &range
))
1295 dwarf2_traverse_context_t traverse
;
1296 unsigned alloc
= 16;
1297 ranges
= malloc(sizeof(struct addr_range
) * alloc
);
1298 if (!ranges
) return NULL
;
1301 traverse
.data
= di
->unit_ctx
->module_ctx
->sections
[section_ranges
].address
+ range
.u
.uvalue
;
1302 traverse
.end_data
= di
->unit_ctx
->module_ctx
->sections
[section_ranges
].address
+
1303 di
->unit_ctx
->module_ctx
->sections
[section_ranges
].size
;
1305 while (traverse
.data
+ 2 * di
->unit_ctx
->head
.word_size
< traverse
.end_data
)
1307 ULONG_PTR low
= dwarf2_parse_addr_head(&traverse
, &di
->unit_ctx
->head
);
1308 ULONG_PTR high
= dwarf2_parse_addr_head(&traverse
, &di
->unit_ctx
->head
);
1309 if (low
== 0 && high
== 0) break;
1310 if (low
== (di
->unit_ctx
->head
.word_size
== 8 ? (~(DWORD64
)0u) : (DWORD64
)(~0u)))
1311 FIXME("unsupported yet (base address selection)\n");
1312 if (*num_ranges
>= alloc
)
1315 ranges
= realloc(ranges
, sizeof(struct addr_range
) * alloc
);
1316 if (!ranges
) return NULL
;
1318 ranges
[*num_ranges
].low
= di
->unit_ctx
->compiland
->address
+ low
;
1319 ranges
[*num_ranges
].high
= di
->unit_ctx
->compiland
->address
+ high
;
1325 struct attribute low_pc
;
1326 struct attribute high_pc
;
1328 if (!dwarf2_find_attribute(di
, DW_AT_low_pc
, &low_pc
) ||
1329 !dwarf2_find_attribute(di
, DW_AT_high_pc
, &high_pc
))
1331 if (di
->unit_ctx
->head
.version
>= 4)
1332 switch (high_pc
.form
)
1342 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1343 high_pc
.u
.uvalue
+= low_pc
.u
.uvalue
;
1346 FIXME("Unsupported class for high_pc\n");
1349 ranges
= malloc(sizeof(struct addr_range
));
1350 if (!ranges
) return NULL
;
1351 ranges
[0].low
= di
->unit_ctx
->module_ctx
->load_offset
+ low_pc
.u
.uvalue
;
1352 ranges
[0].high
= di
->unit_ctx
->module_ctx
->load_offset
+ high_pc
.u
.uvalue
;
1358 /******************************************************************
1359 * dwarf2_read_one_debug_info
1361 * Loads into memory one debug info entry, and recursively its children (if any)
1363 static BOOL
dwarf2_read_one_debug_info(dwarf2_parse_context_t
* ctx
,
1364 dwarf2_traverse_context_t
* traverse
,
1365 dwarf2_debug_info_t
* parent_di
,
1366 dwarf2_debug_info_t
** pdi
)
1368 const dwarf2_abbrev_entry_t
*abbrev
;
1369 ULONG_PTR entry_code
;
1371 dwarf2_debug_info_t
* di
;
1372 dwarf2_debug_info_t
* child
;
1373 dwarf2_debug_info_t
** where
;
1374 dwarf2_abbrev_entry_attr_t
* attr
;
1376 struct attribute sibling
;
1378 offset
= traverse
->data
- ctx
->module_ctx
->sections
[ctx
->section
].address
;
1379 entry_code
= dwarf2_leb128_as_unsigned(traverse
);
1380 TRACE("found entry_code %lu at 0x%lx\n", entry_code
, offset
);
1386 abbrev
= dwarf2_abbrev_table_find_entry(&ctx
->abbrev_table
, entry_code
);
1389 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code
, offset
);
1392 di
= sparse_array_add(&ctx
->debug_info_table
, offset
, &ctx
->pool
);
1393 if (!di
) return FALSE
;
1394 di
->abbrev
= abbrev
;
1396 di
->parent
= parent_di
;
1399 if (abbrev
->num_attr
)
1401 di
->data
= pool_alloc(&ctx
->pool
, abbrev
->num_attr
* sizeof(const char*));
1402 for (i
= 0, attr
= abbrev
->attrs
; attr
; i
++, attr
= attr
->next
)
1404 di
->data
[i
] = traverse
->data
;
1405 dwarf2_swallow_attribute(traverse
, &ctx
->head
, attr
);
1408 else di
->data
= NULL
;
1409 if (abbrev
->have_child
)
1411 vector_init(&di
->children
, sizeof(dwarf2_debug_info_t
*), 16);
1412 while (traverse
->data
< traverse
->end_data
)
1414 if (!dwarf2_read_one_debug_info(ctx
, traverse
, di
, &child
)) return FALSE
;
1416 where
= vector_add(&di
->children
, &ctx
->pool
);
1417 if (!where
) return FALSE
;
1421 if (dwarf2_find_attribute(di
, DW_AT_sibling
, &sibling
) &&
1422 traverse
->data
!= ctx
->module_ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
)
1424 if (sibling
.u
.uvalue
>= ctx
->module_ctx
->sections
[ctx
->section
].size
)
1426 FIXME("cursor sibling after section end %s: 0x%lx 0x%x\n",
1427 dwarf2_debug_unit_ctx(ctx
), sibling
.u
.uvalue
, ctx
->module_ctx
->sections
[ctx
->section
].size
);
1430 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1431 dwarf2_debug_traverse_ctx(traverse
), sibling
.u
.uvalue
);
1432 traverse
->data
= ctx
->module_ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
;
1438 static struct vector
* dwarf2_get_di_children(dwarf2_debug_info_t
* di
)
1440 struct attribute spec
;
1444 if (di
->abbrev
->have_child
)
1445 return &di
->children
;
1446 if (!dwarf2_find_attribute(di
, DW_AT_specification
, &spec
)) break;
1447 if (!(di
= dwarf2_jump_to_debug_info(&spec
)))
1448 FIXME("Should have found the debug info entry\n");
1453 static struct symt
* dwarf2_parse_base_type(dwarf2_debug_info_t
* di
)
1455 struct attribute name
;
1456 struct attribute size
;
1457 struct attribute encoding
;
1459 if (di
->symt
) return di
->symt
;
1461 TRACE("%s\n", dwarf2_debug_di(di
));
1463 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
1464 name
.u
.string
= NULL
;
1465 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1466 if (!dwarf2_find_attribute(di
, DW_AT_encoding
, &encoding
)) encoding
.u
.uvalue
= DW_ATE_void
;
1468 switch (encoding
.u
.uvalue
)
1470 case DW_ATE_void
: bt
= btVoid
; break;
1471 case DW_ATE_address
: bt
= btULong
; break;
1472 case DW_ATE_boolean
: bt
= btBool
; break;
1473 case DW_ATE_complex_float
: bt
= btComplex
; break;
1474 case DW_ATE_float
: bt
= btFloat
; break;
1475 case DW_ATE_signed
: bt
= btInt
; break;
1476 case DW_ATE_unsigned
: bt
= btUInt
; break;
1477 case DW_ATE_signed_char
: bt
= btChar
; break;
1478 case DW_ATE_unsigned_char
: bt
= btChar
; break;
1479 default: bt
= btNoType
; break;
1481 di
->symt
= &symt_new_basic(di
->unit_ctx
->module_ctx
->module
, bt
, name
.u
.string
, size
.u
.uvalue
)->symt
;
1483 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1487 static struct symt
* dwarf2_parse_typedef(dwarf2_debug_info_t
* di
)
1489 struct symt
* ref_type
;
1490 struct attribute name
;
1492 if (di
->symt
) return di
->symt
;
1494 TRACE("%s\n", dwarf2_debug_di(di
));
1496 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1497 ref_type
= dwarf2_lookup_type(di
);
1500 di
->symt
= &symt_new_typedef(di
->unit_ctx
->module_ctx
->module
, ref_type
, name
.u
.string
)->symt
;
1501 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1505 static struct symt
* dwarf2_parse_pointer_type(dwarf2_debug_info_t
* di
)
1507 struct symt
* ref_type
;
1508 struct attribute size
;
1510 if (di
->symt
) return di
->symt
;
1512 TRACE("%s\n", dwarf2_debug_di(di
));
1514 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= di
->unit_ctx
->module_ctx
->module
->cpu
->word_size
;
1515 ref_type
= dwarf2_lookup_type(di
);
1516 di
->symt
= &symt_new_pointer(di
->unit_ctx
->module_ctx
->module
, ref_type
, size
.u
.uvalue
)->symt
;
1517 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1521 static struct symt
* dwarf2_parse_subrange_type(dwarf2_debug_info_t
* di
)
1523 struct symt
* ref_type
;
1524 struct attribute name
;
1525 struct attribute dummy
;
1527 if (di
->symt
) return di
->symt
;
1529 TRACE("%s\n", dwarf2_debug_di(di
));
1531 if (dwarf2_find_attribute(di
, DW_AT_name
, &name
)) FIXME("Found name for subrange %s\n", name
.u
.string
);
1532 if (dwarf2_find_attribute(di
, DW_AT_byte_size
, &dummy
)) FIXME("Found byte_size %lu\n", dummy
.u
.uvalue
);
1533 if (dwarf2_find_attribute(di
, DW_AT_bit_size
, &dummy
)) FIXME("Found bit_size %lu\n", dummy
.u
.uvalue
);
1534 /* for now, we don't support the byte_size nor bit_size about the subrange, and pretend the two
1535 * types are the same (FIXME)
1537 ref_type
= dwarf2_lookup_type(di
);
1538 di
->symt
= ref_type
;
1539 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1543 static struct symt
* dwarf2_parse_array_type(dwarf2_debug_info_t
* di
)
1545 struct symt
* ref_type
;
1546 struct symt
* idx_type
= NULL
;
1547 struct symt
* symt
= NULL
;
1548 struct attribute min
, max
, cnt
;
1549 dwarf2_debug_info_t
* child
;
1551 const struct vector
* children
;
1553 if (di
->symt
) return di
->symt
;
1555 TRACE("%s\n", dwarf2_debug_di(di
));
1557 ref_type
= dwarf2_lookup_type(di
);
1559 if (!(children
= dwarf2_get_di_children(di
)))
1561 /* fake an array with unknown size */
1562 /* FIXME: int4 even on 64bit machines??? */
1563 idx_type
= &symt_new_basic(di
->unit_ctx
->module_ctx
->module
, btInt
, "int", 4)->symt
;
1567 else for (i
= 0; i
< vector_length(children
); i
++)
1569 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1570 if (child
->symt
== di
->unit_ctx
->module_ctx
->symt_cache
[sc_unknown
]) continue;
1571 switch (child
->abbrev
->tag
)
1573 case DW_TAG_subrange_type
:
1574 idx_type
= dwarf2_lookup_type(child
);
1575 if (!dwarf2_find_attribute(child
, DW_AT_lower_bound
, &min
))
1577 if (dwarf2_find_attribute(child
, DW_AT_upper_bound
, &max
))
1578 cnt
.u
.uvalue
= max
.u
.uvalue
+ 1 - min
.u
.uvalue
;
1579 else if (!dwarf2_find_attribute(child
, DW_AT_count
, &cnt
))
1582 case DW_TAG_enumeration_type
:
1583 symt
= dwarf2_parse_enumeration_type(child
);
1584 if (symt_check_tag(symt
, SymTagEnum
))
1586 struct symt_enum
* enum_symt
= (struct symt_enum
*)symt
;
1587 idx_type
= enum_symt
->base_type
;
1590 for (j
= 0; j
< enum_symt
->vchildren
.num_elts
; ++j
)
1592 struct symt
** pc
= vector_at(&enum_symt
->vchildren
, j
);
1593 if (pc
&& symt_check_tag(*pc
, SymTagData
))
1595 struct symt_data
* elt
= (struct symt_data
*)(*pc
);
1596 if (elt
->u
.value
.n1
.n2
.n3
.lVal
< min
.u
.uvalue
)
1597 min
.u
.uvalue
= elt
->u
.value
.n1
.n2
.n3
.lVal
;
1598 if (elt
->u
.value
.n1
.n2
.n3
.lVal
> max
.u
.uvalue
)
1599 max
.u
.uvalue
= elt
->u
.value
.n1
.n2
.n3
.lVal
;
1605 FIXME("Unhandled Tag type 0x%lx at %s\n",
1606 child
->abbrev
->tag
, dwarf2_debug_di(di
));
1610 di
->symt
= &symt_new_array(di
->unit_ctx
->module_ctx
->module
, min
.u
.uvalue
, cnt
.u
.uvalue
, ref_type
, idx_type
)->symt
;
1614 static struct symt
* dwarf2_parse_const_type(dwarf2_debug_info_t
* di
)
1616 struct symt
* ref_type
;
1618 if (di
->symt
) return di
->symt
;
1620 TRACE("%s\n", dwarf2_debug_di(di
));
1622 ref_type
= dwarf2_lookup_type(di
);
1623 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1624 di
->symt
= ref_type
;
1629 static struct symt
* dwarf2_parse_volatile_type(dwarf2_debug_info_t
* di
)
1631 struct symt
* ref_type
;
1633 if (di
->symt
) return di
->symt
;
1635 TRACE("%s\n", dwarf2_debug_di(di
));
1637 ref_type
= dwarf2_lookup_type(di
);
1638 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1639 di
->symt
= ref_type
;
1644 static struct symt
* dwarf2_parse_restrict_type(dwarf2_debug_info_t
* di
)
1646 struct symt
* ref_type
;
1648 if (di
->symt
) return di
->symt
;
1650 TRACE("%s\n", dwarf2_debug_di(di
));
1652 ref_type
= dwarf2_lookup_type(di
);
1653 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1654 di
->symt
= ref_type
;
1659 static struct symt
* dwarf2_parse_unspecified_type(dwarf2_debug_info_t
* di
)
1661 struct attribute name
;
1662 struct attribute size
;
1663 struct symt_basic
*basic
;
1665 TRACE("%s\n", dwarf2_debug_di(di
));
1667 if (di
->symt
) return di
->symt
;
1669 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
1670 name
.u
.string
= "void";
1671 size
.u
.uvalue
= di
->unit_ctx
->module_ctx
->module
->cpu
->word_size
;
1673 basic
= symt_new_basic(di
->unit_ctx
->module_ctx
->module
, btVoid
, name
.u
.string
, size
.u
.uvalue
);
1674 di
->symt
= &basic
->symt
;
1676 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1680 static struct symt
* dwarf2_parse_reference_type(dwarf2_debug_info_t
* di
)
1682 struct symt
* ref_type
= NULL
;
1684 if (di
->symt
) return di
->symt
;
1686 TRACE("%s\n", dwarf2_debug_di(di
));
1688 ref_type
= dwarf2_lookup_type(di
);
1689 /* FIXME: for now, we hard-wire C++ references to pointers */
1690 di
->symt
= &symt_new_pointer(di
->unit_ctx
->module_ctx
->module
, ref_type
,
1691 di
->unit_ctx
->module_ctx
->module
->cpu
->word_size
)->symt
;
1693 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1698 static void dwarf2_parse_udt_member(dwarf2_debug_info_t
* di
,
1699 struct symt_udt
* parent
)
1701 struct symt
* elt_type
;
1702 struct attribute name
;
1703 struct attribute bit_size
;
1704 struct attribute bit_offset
;
1705 struct location loc
;
1709 TRACE("%s\n", dwarf2_debug_di(di
));
1711 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1712 elt_type
= dwarf2_lookup_type(di
);
1713 if (dwarf2_compute_location_attr(di
->unit_ctx
, di
, DW_AT_data_member_location
, &loc
, NULL
))
1715 if (loc
.kind
!= loc_absolute
)
1717 FIXME("Unexpected offset computation for member %s in %ls!%s\n",
1718 name
.u
.string
, di
->unit_ctx
->module_ctx
->module
->modulename
, parent
->hash_elt
.name
);
1722 TRACE("found member_location at %s -> %lu\n",
1723 dwarf2_debug_di(di
), loc
.offset
);
1727 if (!dwarf2_find_attribute(di
, DW_AT_bit_size
, &bit_size
))
1728 bit_size
.u
.uvalue
= 0;
1729 if (dwarf2_find_attribute(di
, DW_AT_bit_offset
, &bit_offset
))
1731 /* FIXME: we should only do this when implementation is LSB (which is
1732 * the case on i386 processors)
1734 struct attribute nbytes
;
1735 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &nbytes
))
1738 nbytes
.u
.uvalue
= symt_get_info(di
->unit_ctx
->module_ctx
->module
, elt_type
, TI_GET_LENGTH
, &size
) ?
1739 (ULONG_PTR
)size
: 0;
1741 bit_offset
.u
.uvalue
= nbytes
.u
.uvalue
* 8 - bit_offset
.u
.uvalue
- bit_size
.u
.uvalue
;
1743 else bit_offset
.u
.uvalue
= 0;
1744 symt_add_udt_element(di
->unit_ctx
->module_ctx
->module
, parent
, name
.u
.string
, elt_type
,
1745 loc
.offset
, bit_offset
.u
.uvalue
,
1748 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1751 static struct symt
* dwarf2_parse_subprogram(dwarf2_debug_info_t
* di
);
1753 static struct symt
* dwarf2_parse_udt_type(dwarf2_debug_info_t
* di
,
1756 struct attribute name
;
1757 struct attribute size
;
1758 struct vector
* children
;
1759 dwarf2_debug_info_t
*child
;
1762 if (di
->symt
) return di
->symt
;
1764 TRACE("%s\n", dwarf2_debug_di(di
));
1766 /* quirk... FIXME provide real support for anonymous UDTs */
1767 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
1768 name
.u
.string
= "zz_anon_zz";
1769 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1771 di
->symt
= &symt_new_udt(di
->unit_ctx
->module_ctx
->module
, dwarf2_get_cpp_name(di
, name
.u
.string
),
1772 size
.u
.uvalue
, udt
)->symt
;
1774 children
= dwarf2_get_di_children(di
);
1775 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1777 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1779 switch (child
->abbrev
->tag
)
1781 case DW_TAG_array_type
:
1782 dwarf2_parse_array_type(child
);
1785 /* FIXME: should I follow the sibling stuff ?? */
1786 if (symt_check_tag(di
->symt
, SymTagUDT
))
1787 dwarf2_parse_udt_member(child
, (struct symt_udt
*)di
->symt
);
1789 case DW_TAG_enumeration_type
:
1790 dwarf2_parse_enumeration_type(child
);
1792 case DW_TAG_subprogram
:
1793 dwarf2_parse_subprogram(child
);
1795 case DW_TAG_const_type
:
1796 dwarf2_parse_const_type(child
);
1798 case DW_TAG_volatile_type
:
1799 dwarf2_parse_volatile_type(child
);
1801 case DW_TAG_pointer_type
:
1802 dwarf2_parse_pointer_type(child
);
1804 case DW_TAG_subrange_type
:
1805 dwarf2_parse_subrange_type(child
);
1807 case DW_TAG_structure_type
:
1808 case DW_TAG_class_type
:
1809 case DW_TAG_union_type
:
1810 case DW_TAG_typedef
:
1811 /* FIXME: we need to handle nested udt definitions */
1812 case DW_TAG_inheritance
:
1813 case DW_TAG_interface_type
:
1814 case DW_TAG_template_type_param
:
1815 case DW_TAG_template_value_param
:
1816 case DW_TAG_variable
:
1817 case DW_TAG_imported_declaration
:
1818 case DW_TAG_ptr_to_member_type
:
1819 case DW_TAG_GNU_template_template_param
:
1820 case DW_TAG_GNU_template_parameter_pack
:
1821 case DW_TAG_GNU_formal_parameter_pack
:
1822 /* FIXME: some C++ related stuff */
1825 FIXME("Unhandled Tag type 0x%lx at %s\n",
1826 child
->abbrev
->tag
, dwarf2_debug_di(di
));
1834 static void dwarf2_parse_enumerator(dwarf2_debug_info_t
* di
,
1835 struct symt_enum
* parent
)
1837 struct attribute name
;
1838 struct attribute value
;
1840 TRACE("%s\n", dwarf2_debug_di(di
));
1842 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) return;
1843 if (!dwarf2_find_attribute(di
, DW_AT_const_value
, &value
)) value
.u
.svalue
= 0;
1844 symt_add_enum_element(di
->unit_ctx
->module_ctx
->module
, parent
, name
.u
.string
, value
.u
.svalue
);
1846 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1849 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_debug_info_t
* di
)
1851 struct attribute name
;
1852 struct attribute attrtype
;
1853 dwarf2_debug_info_t
*ditype
;
1855 struct vector
* children
;
1856 dwarf2_debug_info_t
*child
;
1859 if (di
->symt
) return di
->symt
;
1861 TRACE("%s\n", dwarf2_debug_di(di
));
1863 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1864 if (dwarf2_find_attribute(di
, DW_AT_type
, &attrtype
) && (ditype
= dwarf2_jump_to_debug_info(&attrtype
)) != NULL
)
1865 type
= ditype
->symt
;
1866 else /* no type found for this enumeration, construct it from size */
1868 struct attribute size
;
1869 struct symt_basic
* basetype
;
1871 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 4;
1873 switch (size
.u
.uvalue
) /* FIXME: that's wrong */
1875 case 1: basetype
= symt_new_basic(di
->unit_ctx
->module_ctx
->module
, btInt
, "char", 1); break;
1876 case 2: basetype
= symt_new_basic(di
->unit_ctx
->module_ctx
->module
, btInt
, "short", 2); break;
1878 case 4: basetype
= symt_new_basic(di
->unit_ctx
->module_ctx
->module
, btInt
, "int", 4); break;
1880 type
= &basetype
->symt
;
1883 di
->symt
= &symt_new_enum(di
->unit_ctx
->module_ctx
->module
, name
.u
.string
, type
)->symt
;
1884 children
= dwarf2_get_di_children(di
);
1886 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1888 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1890 switch (child
->abbrev
->tag
)
1892 case DW_TAG_enumerator
:
1893 if (symt_check_tag(di
->symt
, SymTagEnum
))
1894 dwarf2_parse_enumerator(child
, (struct symt_enum
*)di
->symt
);
1897 FIXME("Unhandled Tag type 0x%lx at %s\n",
1898 di
->abbrev
->tag
, dwarf2_debug_di(di
));
1904 /* structure used to pass information around when parsing a subprogram */
1905 typedef struct dwarf2_subprogram_s
1907 dwarf2_parse_context_t
* ctx
;
1908 struct symt_function
* top_func
;
1909 struct symt_function
* current_func
; /* either symt_function* or symt_inlinesite* */
1910 struct symt_block
* current_block
;
1911 BOOL non_computed_variable
;
1912 struct location frame
;
1913 } dwarf2_subprogram_t
;
1915 /******************************************************************
1916 * dwarf2_parse_variable
1918 * Parses any variable (parameter, local/global variable)
1920 static void dwarf2_parse_variable(dwarf2_subprogram_t
* subpgm
,
1921 dwarf2_debug_info_t
* di
)
1923 struct symt
* param_type
;
1924 struct attribute name
, value
;
1925 struct location loc
;
1928 TRACE("%s\n", dwarf2_debug_di(di
));
1930 is_pmt
= !subpgm
->current_block
&& di
->abbrev
->tag
== DW_TAG_formal_parameter
;
1931 param_type
= dwarf2_lookup_type(di
);
1933 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) {
1934 /* cannot do much without the name, the functions below won't like it. */
1937 if (dwarf2_compute_location_attr(subpgm
->ctx
, di
, DW_AT_location
,
1938 &loc
, &subpgm
->frame
))
1940 struct attribute ext
;
1942 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1943 debugstr_a(name
.u
.string
), loc
.kind
, loc
.offset
, loc
.reg
,
1944 dwarf2_debug_unit_ctx(subpgm
->ctx
));
1951 /* it's a global variable */
1952 /* FIXME: we don't handle its scope yet */
1953 if (!dwarf2_find_attribute(di
, DW_AT_external
, &ext
))
1955 loc
.offset
+= subpgm
->ctx
->module_ctx
->load_offset
;
1956 symt_new_global_variable(subpgm
->ctx
->module_ctx
->module
, subpgm
->ctx
->compiland
,
1957 dwarf2_get_cpp_name(di
, name
.u
.string
), !ext
.u
.uvalue
,
1958 loc
, 0, param_type
);
1961 subpgm
->non_computed_variable
= TRUE
;
1965 /* either a pmt/variable relative to frame pointer or
1966 * pmt/variable in a register
1968 if (subpgm
->current_func
)
1969 symt_add_func_local(subpgm
->ctx
->module_ctx
->module
, subpgm
->current_func
,
1970 is_pmt
? DataIsParam
: DataIsLocal
,
1971 &loc
, subpgm
->current_block
, param_type
, name
.u
.string
);
1975 else if (dwarf2_find_attribute(di
, DW_AT_const_value
, &value
))
1987 V_UI4(&v
) = value
.u
.uvalue
;
1991 case DW_FORM_sec_offset
:
1993 V_UI8(&v
) = value
.u
.lluvalue
;
1998 V_I4(&v
) = value
.u
.svalue
;
2002 case DW_FORM_string
:
2003 /* FIXME: native doesn't report const strings from here !!
2004 * however, the value of the string is in the code somewhere
2006 V_VT(&v
) = VT_BYREF
;
2007 V_BYREF(&v
) = pool_strdup(&subpgm
->ctx
->module_ctx
->module
->pool
, value
.u
.string
);
2011 case DW_FORM_block1
:
2012 case DW_FORM_block2
:
2013 case DW_FORM_block4
:
2014 case DW_FORM_exprloc
:
2016 switch (value
.u
.block
.size
)
2018 case 1: V_I4(&v
) = *(BYTE
*)value
.u
.block
.ptr
; break;
2019 case 2: V_I4(&v
) = *(USHORT
*)value
.u
.block
.ptr
; break;
2020 case 4: V_I4(&v
) = *(DWORD
*)value
.u
.block
.ptr
; break;
2022 V_VT(&v
) = VT_BYREF
;
2023 V_BYREF(&v
) = pool_alloc(&subpgm
->ctx
->module_ctx
->module
->pool
, value
.u
.block
.size
);
2024 memcpy(V_BYREF(&v
), value
.u
.block
.ptr
, value
.u
.block
.size
);
2029 FIXME("Unsupported form for const value %s (%lx)\n",
2030 debugstr_a(name
.u
.string
), value
.form
);
2031 V_VT(&v
) = VT_EMPTY
;
2033 if (subpgm
->current_func
)
2035 if (is_pmt
) WARN("Constant parameter %s reported as local variable in function '%s'\n",
2036 debugstr_a(name
.u
.string
), subpgm
->current_func
->hash_elt
.name
);
2037 di
->symt
= &symt_add_func_constant(subpgm
->ctx
->module_ctx
->module
,
2038 subpgm
->current_func
, subpgm
->current_block
,
2039 param_type
, name
.u
.string
, &v
)->symt
;
2042 di
->symt
= &symt_new_constant(subpgm
->ctx
->module_ctx
->module
, subpgm
->ctx
->compiland
,
2043 name
.u
.string
, param_type
, &v
)->symt
;
2047 if (subpgm
->current_func
)
2049 /* local variable has been optimized away... report anyway */
2050 loc
.kind
= loc_error
;
2051 loc
.reg
= loc_err_no_location
;
2052 symt_add_func_local(subpgm
->ctx
->module_ctx
->module
, subpgm
->current_func
,
2053 is_pmt
? DataIsParam
: DataIsLocal
,
2054 &loc
, subpgm
->current_block
, param_type
, name
.u
.string
);
2058 struct attribute is_decl
;
2059 /* only warn when di doesn't represent a declaration */
2060 if (!dwarf2_find_attribute(di
, DW_AT_declaration
, &is_decl
) ||
2061 !is_decl
.u
.uvalue
|| is_decl
.gotten_from
!= attr_direct
)
2062 WARN("dropping global variable %s which has been optimized away\n", debugstr_a(name
.u
.string
));
2065 if (is_pmt
&& subpgm
->current_func
&& symt_check_tag(subpgm
->current_func
->type
, SymTagFunctionType
))
2066 symt_add_function_signature_parameter(subpgm
->ctx
->module_ctx
->module
,
2067 (struct symt_function_signature
*)subpgm
->current_func
->type
,
2070 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
2073 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t
* subpgm
,
2074 const dwarf2_debug_info_t
* di
)
2076 struct attribute name
;
2077 struct attribute low_pc
;
2078 struct location loc
;
2080 TRACE("%s\n", dwarf2_debug_di(di
));
2082 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
2083 name
.u
.string
= NULL
;
2084 if (dwarf2_find_attribute(di
, DW_AT_low_pc
, &low_pc
))
2086 loc
.kind
= loc_absolute
;
2087 loc
.offset
= subpgm
->ctx
->module_ctx
->load_offset
+ low_pc
.u
.uvalue
- subpgm
->top_func
->address
;
2088 symt_add_function_point(subpgm
->ctx
->module_ctx
->module
, subpgm
->top_func
, SymTagLabel
,
2089 &loc
, name
.u
.string
);
2092 WARN("Label %s inside function %s doesn't have an address... don't register it\n",
2093 name
.u
.string
, subpgm
->top_func
->hash_elt
.name
);
2096 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
2097 dwarf2_debug_info_t
* di
);
2099 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_debug_info_t
* di
);
2101 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t
* subpgm
,
2102 dwarf2_debug_info_t
* di
)
2104 struct attribute name
;
2105 ULONG_PTR low_pc
, high_pc
;
2106 struct symt
* ret_type
;
2107 struct symt_function_signature
* sig_type
;
2108 struct symt_inlinesite
* inlined
;
2109 struct vector
* children
;
2110 dwarf2_debug_info_t
*child
;
2112 struct addr_range
* adranges
;
2113 unsigned num_adranges
;
2115 TRACE("%s\n", dwarf2_debug_di(di
));
2117 if (!dwarf2_read_range(subpgm
->ctx
, di
, &low_pc
, &high_pc
))
2119 WARN("cannot read range\n");
2122 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
2124 FIXME("No name for function... dropping function\n");
2127 ret_type
= dwarf2_lookup_type(di
);
2129 /* FIXME: assuming C source code */
2130 sig_type
= symt_new_function_signature(subpgm
->ctx
->module_ctx
->module
, ret_type
, CV_CALL_FAR_C
);
2132 inlined
= symt_new_inlinesite(subpgm
->ctx
->module_ctx
->module
,
2134 subpgm
->current_block
? &subpgm
->current_block
->symt
: &subpgm
->current_func
->symt
,
2135 dwarf2_get_cpp_name(di
, name
.u
.string
),
2136 subpgm
->ctx
->module_ctx
->load_offset
+ low_pc
,
2138 subpgm
->current_func
= (struct symt_function
*)inlined
;
2139 subpgm
->current_block
= NULL
;
2141 if ((adranges
= dwarf2_get_ranges(di
, &num_adranges
)) != NULL
)
2143 for (i
= 0; i
< num_adranges
; ++i
)
2144 symt_add_inlinesite_range(subpgm
->ctx
->module_ctx
->module
, inlined
,
2145 adranges
[i
].low
, adranges
[i
].high
);
2149 WARN("cannot read ranges\n");
2151 children
= dwarf2_get_di_children(di
);
2152 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2154 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2156 switch (child
->abbrev
->tag
)
2158 case DW_TAG_formal_parameter
:
2159 case DW_TAG_variable
:
2160 dwarf2_parse_variable(subpgm
, child
);
2162 case DW_TAG_lexical_block
:
2163 dwarf2_parse_subprogram_block(subpgm
, child
);
2165 case DW_TAG_inlined_subroutine
:
2166 dwarf2_parse_inlined_subroutine(subpgm
, child
);
2169 dwarf2_parse_subprogram_label(subpgm
, child
);
2171 case DW_TAG_GNU_call_site
:
2172 /* this isn't properly supported by dbghelp interface. skip it for now */
2175 FIXME("Unhandled Tag type 0x%lx at %s\n",
2176 child
->abbrev
->tag
, dwarf2_debug_di(di
));
2179 subpgm
->current_block
= symt_check_tag(subpgm
->current_func
->container
, SymTagBlock
) ?
2180 (struct symt_block
*)subpgm
->current_func
->container
: NULL
;
2181 subpgm
->current_func
= (struct symt_function
*)symt_get_upper_inlined((struct symt_inlinesite
*)subpgm
->current_func
);
2184 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
2185 dwarf2_debug_info_t
* di
)
2187 ULONG_PTR low_pc
, high_pc
;
2188 struct vector
* children
;
2189 dwarf2_debug_info_t
*child
;
2192 TRACE("%s\n", dwarf2_debug_di(di
));
2194 if (!dwarf2_read_range(subpgm
->ctx
, di
, &low_pc
, &high_pc
))
2200 subpgm
->current_block
= symt_open_func_block(subpgm
->ctx
->module_ctx
->module
, subpgm
->current_func
, subpgm
->current_block
,
2201 subpgm
->ctx
->module_ctx
->load_offset
+ low_pc
- subpgm
->current_func
->address
,
2204 children
= dwarf2_get_di_children(di
);
2205 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2207 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2209 switch (child
->abbrev
->tag
)
2211 case DW_TAG_inlined_subroutine
:
2212 dwarf2_parse_inlined_subroutine(subpgm
, child
);
2214 case DW_TAG_variable
:
2215 dwarf2_parse_variable(subpgm
, child
);
2217 case DW_TAG_pointer_type
:
2218 dwarf2_parse_pointer_type(child
);
2220 case DW_TAG_subroutine_type
:
2221 dwarf2_parse_subroutine_type(child
);
2223 case DW_TAG_const_type
:
2224 dwarf2_parse_const_type(child
);
2226 case DW_TAG_lexical_block
:
2227 dwarf2_parse_subprogram_block(subpgm
, child
);
2229 case DW_TAG_subprogram
:
2230 /* FIXME: likely a declaration (to be checked)
2234 case DW_TAG_formal_parameter
:
2235 /* FIXME: likely elements for exception handling (GCC flavor)
2239 case DW_TAG_imported_module
:
2240 /* C++ stuff to be silenced (for now) */
2242 case DW_TAG_GNU_call_site
:
2243 /* this isn't properly supported by dbghelp interface. skip it for now */
2246 dwarf2_parse_subprogram_label(subpgm
, child
);
2248 case DW_TAG_class_type
:
2249 case DW_TAG_structure_type
:
2250 case DW_TAG_union_type
:
2251 case DW_TAG_enumeration_type
:
2252 case DW_TAG_typedef
:
2253 /* the type referred to will be loaded when we need it, so skip it */
2256 FIXME("Unhandled Tag type 0x%lx at %s\n",
2257 child
->abbrev
->tag
, dwarf2_debug_di(di
));
2261 symt_close_func_block(subpgm
->ctx
->module_ctx
->module
, subpgm
->current_func
, subpgm
->current_block
, 0);
2262 subpgm
->current_block
= symt_check_tag(subpgm
->current_block
->container
, SymTagBlock
) ?
2263 (struct symt_block
*)subpgm
->current_block
->container
: NULL
;
2266 static struct symt
* dwarf2_parse_subprogram(dwarf2_debug_info_t
* di
)
2268 struct attribute name
;
2269 struct addr_range
* addr_ranges
;
2270 unsigned num_addr_ranges
;
2271 struct attribute is_decl
;
2272 struct attribute inline_flags
;
2273 struct symt
* ret_type
;
2274 struct symt_function_signature
* sig_type
;
2275 dwarf2_subprogram_t subpgm
;
2276 struct vector
* children
;
2277 dwarf2_debug_info_t
* child
;
2280 if (di
->symt
) return di
->symt
;
2282 TRACE("%s\n", dwarf2_debug_di(di
));
2284 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
2286 WARN("No name for function... dropping function\n");
2289 /* if it's an abstract representation of an inline function, there should be
2290 * a concrete object that we'll handle
2292 if (dwarf2_find_attribute(di
, DW_AT_inline
, &inline_flags
) &&
2293 inline_flags
.gotten_from
== attr_direct
&&
2294 inline_flags
.u
.uvalue
!= DW_INL_not_inlined
)
2296 TRACE("Function %s declared as inlined (%ld)... skipping\n",
2297 debugstr_a(name
.u
.string
), inline_flags
.u
.uvalue
);
2301 if (dwarf2_find_attribute(di
, DW_AT_declaration
, &is_decl
) &&
2302 is_decl
.u
.uvalue
&& is_decl
.gotten_from
== attr_direct
)
2304 /* it's a real declaration, skip it */
2307 if ((addr_ranges
= dwarf2_get_ranges(di
, &num_addr_ranges
)) == NULL
)
2309 WARN("cannot get range for %s\n", debugstr_a(name
.u
.string
));
2312 /* As functions (defined as inline assembly) get debug info with dwarf
2313 * (not the case for stabs), we just drop Wine's thunks here...
2314 * Actual thunks will be created in elf_module from the symbol table
2316 if (elf_is_in_thunk_area(di
->unit_ctx
->module_ctx
->load_offset
+ addr_ranges
[0].low
, di
->unit_ctx
->module_ctx
->thunks
) >= 0)
2318 ret_type
= dwarf2_lookup_type(di
);
2320 /* FIXME: assuming C source code */
2321 sig_type
= symt_new_function_signature(di
->unit_ctx
->module_ctx
->module
, ret_type
, CV_CALL_FAR_C
);
2322 subpgm
.top_func
= symt_new_function(di
->unit_ctx
->module_ctx
->module
, di
->unit_ctx
->compiland
,
2323 dwarf2_get_cpp_name(di
, name
.u
.string
),
2324 addr_ranges
[0].low
, addr_ranges
[0].high
- addr_ranges
[0].low
, &sig_type
->symt
);
2325 if (num_addr_ranges
> 1)
2326 WARN("Function %s has multiple address ranges, only using the first one\n", name
.u
.string
);
2329 subpgm
.current_func
= subpgm
.top_func
;
2330 di
->symt
= &subpgm
.top_func
->symt
;
2331 subpgm
.ctx
= di
->unit_ctx
;
2332 if (!dwarf2_compute_location_attr(di
->unit_ctx
, di
, DW_AT_frame_base
,
2333 &subpgm
.frame
, NULL
))
2336 subpgm
.frame
.kind
= loc_regrel
;
2337 subpgm
.frame
.reg
= di
->unit_ctx
->module_ctx
->module
->cpu
->frame_regno
;
2338 subpgm
.frame
.offset
= 0;
2340 subpgm
.non_computed_variable
= FALSE
;
2341 subpgm
.current_block
= NULL
;
2343 children
= dwarf2_get_di_children(di
);
2344 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2346 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2348 switch (child
->abbrev
->tag
)
2350 case DW_TAG_variable
:
2351 case DW_TAG_formal_parameter
:
2352 dwarf2_parse_variable(&subpgm
, child
);
2354 case DW_TAG_lexical_block
:
2355 dwarf2_parse_subprogram_block(&subpgm
, child
);
2357 case DW_TAG_inlined_subroutine
:
2358 dwarf2_parse_inlined_subroutine(&subpgm
, child
);
2360 case DW_TAG_pointer_type
:
2361 dwarf2_parse_pointer_type(child
);
2363 case DW_TAG_const_type
:
2364 dwarf2_parse_const_type(child
);
2366 case DW_TAG_subprogram
:
2367 /* FIXME: likely a declaration (to be checked)
2372 dwarf2_parse_subprogram_label(&subpgm
, child
);
2374 case DW_TAG_class_type
:
2375 case DW_TAG_structure_type
:
2376 case DW_TAG_union_type
:
2377 case DW_TAG_enumeration_type
:
2378 case DW_TAG_typedef
:
2379 /* the type referred to will be loaded when we need it, so skip it */
2381 case DW_TAG_unspecified_parameters
:
2382 case DW_TAG_template_type_param
:
2383 case DW_TAG_template_value_param
:
2384 case DW_TAG_GNU_call_site
:
2385 case DW_TAG_GNU_template_parameter_pack
:
2386 case DW_TAG_GNU_formal_parameter_pack
:
2387 /* FIXME: no support in dbghelp's internals so far */
2390 FIXME("Unhandled Tag type 0x%lx at %s\n",
2391 child
->abbrev
->tag
, dwarf2_debug_di(di
));
2395 if (subpgm
.non_computed_variable
|| subpgm
.frame
.kind
>= loc_user
)
2397 symt_add_function_point(di
->unit_ctx
->module_ctx
->module
, subpgm
.top_func
, SymTagCustom
,
2398 &subpgm
.frame
, NULL
);
2404 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_debug_info_t
* di
)
2406 struct symt
* ret_type
;
2407 struct symt_function_signature
* sig_type
;
2408 struct vector
* children
;
2409 dwarf2_debug_info_t
* child
;
2412 if (di
->symt
) return di
->symt
;
2414 TRACE("%s\n", dwarf2_debug_di(di
));
2416 ret_type
= dwarf2_lookup_type(di
);
2418 /* FIXME: assuming C source code */
2419 sig_type
= symt_new_function_signature(di
->unit_ctx
->module_ctx
->module
, ret_type
, CV_CALL_FAR_C
);
2421 children
= dwarf2_get_di_children(di
);
2422 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2424 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2426 switch (child
->abbrev
->tag
)
2428 case DW_TAG_formal_parameter
:
2429 symt_add_function_signature_parameter(di
->unit_ctx
->module_ctx
->module
, sig_type
,
2430 dwarf2_lookup_type(child
));
2432 case DW_TAG_unspecified_parameters
:
2433 WARN("Unsupported unspecified parameters\n");
2438 return di
->symt
= &sig_type
->symt
;
2441 static void dwarf2_parse_namespace(dwarf2_debug_info_t
* di
)
2443 struct vector
* children
;
2444 dwarf2_debug_info_t
* child
;
2447 if (di
->symt
) return;
2449 TRACE("%s\n", dwarf2_debug_di(di
));
2451 di
->symt
= di
->unit_ctx
->module_ctx
->symt_cache
[sc_void
];
2453 children
= dwarf2_get_di_children(di
);
2454 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2456 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2457 dwarf2_load_one_entry(child
);
2461 static void dwarf2_parse_imported_unit(dwarf2_debug_info_t
* di
)
2463 struct attribute imp
;
2465 if (di
->symt
) return;
2467 TRACE("%s\n", dwarf2_debug_di(di
));
2469 if (dwarf2_find_attribute(di
, DW_AT_import
, &imp
))
2471 dwarf2_debug_info_t
* jmp
= dwarf2_jump_to_debug_info(&imp
);
2472 if (jmp
) di
->symt
= jmp
->symt
;
2473 else FIXME("Couldn't load imported CU\n");
2476 FIXME("Couldn't find import attribute\n");
2479 static void dwarf2_load_one_entry(dwarf2_debug_info_t
* di
)
2481 switch (di
->abbrev
->tag
)
2483 case DW_TAG_typedef
:
2484 dwarf2_parse_typedef(di
);
2486 case DW_TAG_base_type
:
2487 dwarf2_parse_base_type(di
);
2489 case DW_TAG_pointer_type
:
2490 dwarf2_parse_pointer_type(di
);
2492 case DW_TAG_class_type
:
2493 dwarf2_parse_udt_type(di
, UdtClass
);
2495 case DW_TAG_structure_type
:
2496 dwarf2_parse_udt_type(di
, UdtStruct
);
2498 case DW_TAG_union_type
:
2499 dwarf2_parse_udt_type(di
, UdtUnion
);
2501 case DW_TAG_array_type
:
2502 dwarf2_parse_array_type(di
);
2504 case DW_TAG_const_type
:
2505 dwarf2_parse_const_type(di
);
2507 case DW_TAG_volatile_type
:
2508 dwarf2_parse_volatile_type(di
);
2510 case DW_TAG_restrict_type
:
2511 dwarf2_parse_restrict_type(di
);
2513 case DW_TAG_unspecified_type
:
2514 dwarf2_parse_unspecified_type(di
);
2516 case DW_TAG_reference_type
:
2517 case DW_TAG_rvalue_reference_type
:
2518 dwarf2_parse_reference_type(di
);
2520 case DW_TAG_enumeration_type
:
2521 dwarf2_parse_enumeration_type(di
);
2523 case DW_TAG_subprogram
:
2524 dwarf2_parse_subprogram(di
);
2526 case DW_TAG_subroutine_type
:
2527 dwarf2_parse_subroutine_type(di
);
2529 case DW_TAG_variable
:
2531 dwarf2_subprogram_t subpgm
;
2533 subpgm
.ctx
= di
->unit_ctx
;
2534 subpgm
.top_func
= subpgm
.current_func
= NULL
;
2535 subpgm
.current_block
= NULL
;
2536 subpgm
.frame
.kind
= loc_absolute
;
2537 subpgm
.frame
.offset
= 0;
2538 subpgm
.frame
.reg
= Wine_DW_no_register
;
2539 dwarf2_parse_variable(&subpgm
, di
);
2542 case DW_TAG_namespace
:
2543 dwarf2_parse_namespace(di
);
2545 case DW_TAG_subrange_type
:
2546 dwarf2_parse_subrange_type(di
);
2548 case DW_TAG_imported_unit
:
2549 dwarf2_parse_imported_unit(di
);
2551 /* keep it silent until we need DW_OP_call_xxx support */
2552 case DW_TAG_dwarf_procedure
:
2553 /* silence a couple of non-C language defines (mainly C++ but others too) */
2554 case DW_TAG_imported_module
:
2555 case DW_TAG_imported_declaration
:
2556 case DW_TAG_interface_type
:
2558 case DW_TAG_ptr_to_member_type
:
2561 FIXME("Unhandled Tag type 0x%lx at %s\n",
2562 di
->abbrev
->tag
, dwarf2_debug_di(di
));
2566 static void dwarf2_set_line_number(struct module
* module
, ULONG_PTR address
,
2567 const struct vector
* v
, unsigned file
, unsigned line
)
2569 struct symt_function
* func
;
2570 struct symt_inlinesite
* inlined
;
2571 struct symt_ht
* symt
;
2574 if (!file
|| !(psrc
= vector_at(v
, file
- 1))) return;
2576 TRACE("%s %lx %s %u\n",
2577 debugstr_w(module
->modulename
), address
, debugstr_a(source_get(module
, *psrc
)), line
);
2578 symt
= symt_find_nearest(module
, address
);
2579 if (symt_check_tag(&symt
->symt
, SymTagFunction
))
2581 func
= (struct symt_function
*)symt
;
2582 for (inlined
= func
->next_inlinesite
; inlined
; inlined
= inlined
->func
.next_inlinesite
)
2585 for (i
= 0; i
< inlined
->vranges
.num_elts
; ++i
)
2587 struct addr_range
* ar
= (struct addr_range
*)vector_at(&inlined
->vranges
, i
);
2588 if (ar
->low
<= address
&& address
< ar
->high
)
2590 symt_add_func_line(module
, &inlined
->func
, *psrc
, line
, address
);
2591 return; /* only add to lowest matching inline site */
2595 symt_add_func_line(module
, func
, *psrc
, line
, address
);
2599 static BOOL
dwarf2_parse_line_numbers(dwarf2_parse_context_t
* ctx
,
2600 const char* compile_dir
,
2603 dwarf2_traverse_context_t traverse
;
2605 unsigned insn_size
, version
, default_stmt
;
2606 unsigned line_range
, opcode_base
;
2608 unsigned char offset_size
;
2609 const unsigned char* opcode_len
;
2611 struct vector files
;
2614 /* section with line numbers stripped */
2615 if (ctx
->module_ctx
->sections
[section_line
].address
== IMAGE_NO_MAP
)
2618 if (offset
+ 4 > ctx
->module_ctx
->sections
[section_line
].size
)
2620 WARN("out of bounds offset\n");
2623 traverse
.data
= ctx
->module_ctx
->sections
[section_line
].address
+ offset
;
2624 traverse
.end_data
= ctx
->module_ctx
->sections
[section_line
].address
+ ctx
->module_ctx
->sections
[section_line
].size
;
2626 length
= dwarf2_parse_3264(&traverse
, &offset_size
);
2627 if (offset_size
!= ctx
->head
.offset_size
)
2629 WARN("Mismatch in 32/64 bit format\n");
2632 traverse
.end_data
= traverse
.data
+ length
;
2634 if (traverse
.end_data
> ctx
->module_ctx
->sections
[section_line
].address
+ ctx
->module_ctx
->sections
[section_line
].size
)
2636 WARN("out of bounds header\n");
2639 version
= dwarf2_parse_u2(&traverse
);
2640 dwarf2_parse_offset(&traverse
, offset_size
); /* header_len */
2641 insn_size
= dwarf2_parse_byte(&traverse
);
2643 dwarf2_parse_byte(&traverse
); /* max_operations_per_instructions */
2644 default_stmt
= dwarf2_parse_byte(&traverse
);
2645 line_base
= (signed char)dwarf2_parse_byte(&traverse
);
2646 line_range
= dwarf2_parse_byte(&traverse
);
2647 opcode_base
= dwarf2_parse_byte(&traverse
);
2649 opcode_len
= traverse
.data
;
2650 traverse
.data
+= opcode_base
- 1;
2652 vector_init(&dirs
, sizeof(const char*), 4);
2653 p
= vector_add(&dirs
, &ctx
->pool
);
2654 *p
= compile_dir
? compile_dir
: ".";
2655 while (traverse
.data
< traverse
.end_data
&& *traverse
.data
)
2657 const char* rel
= (const char*)traverse
.data
;
2658 unsigned rellen
= strlen(rel
);
2659 TRACE("Got include %s\n", debugstr_a(rel
));
2660 traverse
.data
+= rellen
+ 1;
2661 p
= vector_add(&dirs
, &ctx
->pool
);
2663 if (*rel
== '/' || !compile_dir
|| !*compile_dir
)
2667 /* include directory relative to compile directory */
2668 unsigned baselen
= strlen(compile_dir
);
2669 char* tmp
= pool_alloc(&ctx
->pool
, baselen
+ 1 + rellen
+ 1);
2670 strcpy(tmp
, compile_dir
);
2671 if (baselen
&& tmp
[baselen
- 1] != '/') tmp
[baselen
++] = '/';
2672 strcpy(&tmp
[baselen
], rel
);
2679 vector_init(&files
, sizeof(unsigned), 16);
2680 while (traverse
.data
< traverse
.end_data
&& *traverse
.data
)
2682 unsigned int dir_index
, mod_time
;
2687 name
= (const char*)traverse
.data
;
2688 traverse
.data
+= strlen(name
) + 1;
2689 dir_index
= dwarf2_leb128_as_unsigned(&traverse
);
2690 mod_time
= dwarf2_leb128_as_unsigned(&traverse
);
2691 length
= dwarf2_leb128_as_unsigned(&traverse
);
2692 dir
= *(const char**)vector_at(&dirs
, dir_index
);
2693 TRACE("Got file %s/%s (%u,%lu)\n", debugstr_a(dir
), debugstr_a(name
), mod_time
, length
);
2694 psrc
= vector_add(&files
, &ctx
->pool
);
2695 *psrc
= source_new(ctx
->module_ctx
->module
, dir
, name
);
2699 while (traverse
.data
< traverse
.end_data
&& *traverse
.data
)
2701 ULONG_PTR address
= 0;
2704 unsigned is_stmt
= default_stmt
;
2705 BOOL end_sequence
= FALSE
;
2706 unsigned opcode
, extopcode
, i
;
2708 while (!end_sequence
)
2710 opcode
= dwarf2_parse_byte(&traverse
);
2711 TRACE("Got opcode %x\n", opcode
);
2713 if (opcode
>= opcode_base
)
2715 unsigned delta
= opcode
- opcode_base
;
2717 address
+= (delta
/ line_range
) * insn_size
;
2718 line
+= line_base
+ (delta
% line_range
);
2719 dwarf2_set_line_number(ctx
->module_ctx
->module
, address
, &files
, file
, line
);
2726 dwarf2_set_line_number(ctx
->module_ctx
->module
, address
, &files
, file
, line
);
2728 case DW_LNS_advance_pc
:
2729 address
+= insn_size
* dwarf2_leb128_as_unsigned(&traverse
);
2731 case DW_LNS_advance_line
:
2732 line
+= dwarf2_leb128_as_signed(&traverse
);
2734 case DW_LNS_set_file
:
2735 file
= dwarf2_leb128_as_unsigned(&traverse
);
2737 case DW_LNS_set_column
:
2738 dwarf2_leb128_as_unsigned(&traverse
);
2740 case DW_LNS_negate_stmt
:
2743 case DW_LNS_set_basic_block
:
2745 case DW_LNS_const_add_pc
:
2746 address
+= ((255 - opcode_base
) / line_range
) * insn_size
;
2748 case DW_LNS_fixed_advance_pc
:
2749 address
+= dwarf2_parse_u2(&traverse
);
2751 case DW_LNS_extended_op
:
2752 dwarf2_leb128_as_unsigned(&traverse
);
2753 extopcode
= dwarf2_parse_byte(&traverse
);
2756 case DW_LNE_end_sequence
:
2757 dwarf2_set_line_number(ctx
->module_ctx
->module
, address
, &files
, file
, line
);
2758 end_sequence
= TRUE
;
2760 case DW_LNE_set_address
:
2761 address
= ctx
->module_ctx
->load_offset
+ dwarf2_parse_addr_head(&traverse
, &ctx
->head
);
2763 case DW_LNE_define_file
:
2764 FIXME("not handled define file %s\n", debugstr_a((char *)traverse
.data
));
2765 traverse
.data
+= strlen((const char *)traverse
.data
) + 1;
2766 dwarf2_leb128_as_unsigned(&traverse
);
2767 dwarf2_leb128_as_unsigned(&traverse
);
2768 dwarf2_leb128_as_unsigned(&traverse
);
2770 case DW_LNE_set_discriminator
:
2774 descr
= dwarf2_leb128_as_unsigned(&traverse
);
2775 WARN("not handled discriminator %x\n", descr
);
2779 FIXME("Unsupported extended opcode %x\n", extopcode
);
2784 WARN("Unsupported opcode %x\n", opcode
);
2785 for (i
= 0; i
< opcode_len
[opcode
]; i
++)
2786 dwarf2_leb128_as_unsigned(&traverse
);
2795 unsigned dwarf2_cache_cuhead(struct dwarf2_module_info_s
* module
, struct symt_compiland
* c
, const dwarf2_cuhead_t
* head
)
2797 dwarf2_cuhead_t
* ah
;
2799 for (i
= 0; i
< module
->num_cuheads
; ++i
)
2801 if (memcmp(module
->cuheads
[i
], head
, sizeof(*head
)) == 0)
2803 c
->user
= module
->cuheads
[i
];
2807 if (!(ah
= pool_alloc(&c
->container
->module
->pool
, sizeof(*head
)))) return FALSE
;
2808 memcpy(ah
, head
, sizeof(*head
));
2809 module
->cuheads
= realloc(module
->cuheads
, ++module
->num_cuheads
* sizeof(head
));
2810 module
->cuheads
[module
->num_cuheads
- 1] = ah
;
2815 static dwarf2_parse_context_t
* dwarf2_locate_cu(dwarf2_parse_module_context_t
* module_ctx
, ULONG_PTR ref
)
2818 dwarf2_parse_context_t
* ctx
;
2820 for (i
= 0; i
< module_ctx
->unit_contexts
.num_elts
; ++i
)
2822 ctx
= vector_at(&module_ctx
->unit_contexts
, i
);
2823 where
= module_ctx
->sections
[ctx
->section
].address
+ ref
;
2824 if (where
>= ctx
->traverse_DIE
.data
&& where
< ctx
->traverse_DIE
.end_data
)
2827 FIXME("Couldn't find ref 0x%lx inside sect\n", ref
);
2831 static BOOL
dwarf2_parse_compilation_unit_head(dwarf2_parse_context_t
* ctx
,
2832 dwarf2_traverse_context_t
* mod_ctx
)
2834 dwarf2_traverse_context_t abbrev_ctx
;
2835 const unsigned char* comp_unit_start
= mod_ctx
->data
;
2836 ULONG_PTR cu_length
;
2837 ULONG_PTR cu_abbrev_offset
;
2838 /* FIXME this is a temporary configuration while adding support for dwarf3&4 bits */
2839 static LONG max_supported_dwarf_version
= 0;
2841 cu_length
= dwarf2_parse_3264(mod_ctx
, &ctx
->head
.offset_size
);
2843 ctx
->traverse_DIE
.data
= mod_ctx
->data
;
2844 ctx
->traverse_DIE
.end_data
= mod_ctx
->data
+ cu_length
;
2845 mod_ctx
->data
+= cu_length
;
2846 ctx
->head
.version
= dwarf2_parse_u2(&ctx
->traverse_DIE
);
2847 cu_abbrev_offset
= dwarf2_parse_offset(&ctx
->traverse_DIE
, ctx
->head
.offset_size
);
2848 ctx
->head
.word_size
= dwarf2_parse_byte(&ctx
->traverse_DIE
);
2849 ctx
->status
= UNIT_ERROR
;
2851 TRACE("Compilation Unit Header found at 0x%x:\n",
2852 (int)(comp_unit_start
- ctx
->module_ctx
->sections
[section_debug
].address
));
2853 TRACE("- length: %lu\n", cu_length
);
2854 TRACE("- version: %u\n", ctx
->head
.version
);
2855 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset
);
2856 TRACE("- word_size: %u\n", ctx
->head
.word_size
);
2857 TRACE("- offset_size: %u\n", ctx
->head
.offset_size
);
2859 if (ctx
->head
.version
>= 2)
2860 ctx
->module_ctx
->cu_versions
|= 1 << (ctx
->head
.version
- 2);
2861 if (max_supported_dwarf_version
== 0)
2863 char* env
= getenv("DBGHELP_DWARF_VERSION");
2864 LONG v
= env
? atol(env
) : 4;
2865 max_supported_dwarf_version
= (v
>= 2 && v
<= 4) ? v
: 4;
2868 if (ctx
->head
.version
< 2 || ctx
->head
.version
> max_supported_dwarf_version
)
2870 WARN("DWARF version %d isn't supported. Wine dbghelp only supports DWARF 2 up to %u.\n",
2871 ctx
->head
.version
, max_supported_dwarf_version
);
2875 pool_init(&ctx
->pool
, 65536);
2876 ctx
->section
= section_debug
;
2877 ctx
->ref_offset
= comp_unit_start
- ctx
->module_ctx
->sections
[section_debug
].address
;
2878 ctx
->cpp_name
= NULL
;
2879 ctx
->status
= UNIT_NOTLOADED
;
2881 abbrev_ctx
.data
= ctx
->module_ctx
->sections
[section_abbrev
].address
+ cu_abbrev_offset
;
2882 abbrev_ctx
.end_data
= ctx
->module_ctx
->sections
[section_abbrev
].address
+ ctx
->module_ctx
->sections
[section_abbrev
].size
;
2883 dwarf2_parse_abbrev_set(&abbrev_ctx
, &ctx
->abbrev_table
, &ctx
->pool
);
2885 sparse_array_init(&ctx
->debug_info_table
, sizeof(dwarf2_debug_info_t
), 128);
2889 static BOOL
dwarf2_parse_compilation_unit(dwarf2_parse_context_t
* ctx
)
2891 dwarf2_debug_info_t
* di
;
2892 dwarf2_traverse_context_t cu_ctx
= ctx
->traverse_DIE
;
2895 switch (ctx
->status
)
2897 case UNIT_ERROR
: return FALSE
;
2898 case UNIT_BEINGLOADED
:
2899 FIXME("Circular deps on CU\n");
2902 case UNIT_LOADED_FAIL
:
2904 case UNIT_NOTLOADED
: break;
2907 ctx
->status
= UNIT_BEINGLOADED
;
2908 if (dwarf2_read_one_debug_info(ctx
, &cu_ctx
, NULL
, &di
))
2910 if (di
->abbrev
->tag
== DW_TAG_compile_unit
|| di
->abbrev
->tag
== DW_TAG_partial_unit
)
2912 struct attribute name
;
2913 struct vector
* children
;
2914 dwarf2_debug_info_t
* child
= NULL
;
2916 struct attribute stmt_list
, low_pc
;
2917 struct attribute comp_dir
;
2919 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
2920 name
.u
.string
= NULL
;
2922 /* get working directory of current compilation unit */
2923 if (!dwarf2_find_attribute(di
, DW_AT_comp_dir
, &comp_dir
))
2924 comp_dir
.u
.string
= NULL
;
2926 if (!dwarf2_find_attribute(di
, DW_AT_low_pc
, &low_pc
))
2927 low_pc
.u
.uvalue
= 0;
2928 ctx
->compiland
= symt_new_compiland(ctx
->module_ctx
->module
, ctx
->module_ctx
->load_offset
+ low_pc
.u
.uvalue
,
2929 source_new(ctx
->module_ctx
->module
, comp_dir
.u
.string
, name
.u
.string
));
2930 dwarf2_cache_cuhead(ctx
->module_ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
, ctx
->compiland
, &ctx
->head
);
2931 di
->symt
= &ctx
->compiland
->symt
;
2932 children
= dwarf2_get_di_children(di
);
2933 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2935 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2936 dwarf2_load_one_entry(child
);
2938 if (dwarf2_find_attribute(di
, DW_AT_stmt_list
, &stmt_list
))
2940 if (dwarf2_parse_line_numbers(ctx
, comp_dir
.u
.string
, stmt_list
.u
.uvalue
))
2941 ctx
->module_ctx
->module
->module
.LineNumbers
= TRUE
;
2943 ctx
->status
= UNIT_LOADED
;
2946 else FIXME("Should have a compilation unit here %lu\n", di
->abbrev
->tag
);
2948 if (ctx
->status
== UNIT_BEINGLOADED
) ctx
->status
= UNIT_LOADED_FAIL
;
2952 static BOOL
dwarf2_lookup_loclist(const struct module_format
* modfmt
, const dwarf2_cuhead_t
* head
,
2953 const BYTE
* start
, ULONG_PTR ip
, dwarf2_traverse_context_t
* lctx
)
2956 const BYTE
* ptr
= start
;
2959 while (ptr
< modfmt
->u
.dwarf2_info
->debug_loc
.address
+ modfmt
->u
.dwarf2_info
->debug_loc
.size
)
2961 beg
= dwarf2_get_addr(ptr
, head
->word_size
); ptr
+= head
->word_size
;
2962 end
= dwarf2_get_addr(ptr
, head
->word_size
); ptr
+= head
->word_size
;
2963 if (!beg
&& !end
) break;
2964 len
= dwarf2_get_u2(ptr
); ptr
+= 2;
2966 if (beg
<= ip
&& ip
< end
)
2969 lctx
->end_data
= ptr
+ len
;
2974 WARN("Couldn't find ip in location list\n");
2978 static const dwarf2_cuhead_t
* get_cuhead_from_func(const struct symt_function
* func
)
2980 if (symt_check_tag(&func
->symt
, SymTagInlineSite
))
2981 func
= symt_get_function_from_inlined((struct symt_inlinesite
*)func
);
2982 if (symt_check_tag(&func
->symt
, SymTagFunction
) && symt_check_tag(func
->container
, SymTagCompiland
))
2984 struct symt_compiland
* c
= (struct symt_compiland
*)func
->container
;
2985 return (const dwarf2_cuhead_t
*)c
->user
;
2987 FIXME("Should have a compilation unit head\n");
2991 static BOOL
compute_call_frame_cfa(struct module
* module
, ULONG_PTR ip
, struct location
* frame
);
2993 static enum location_error
loc_compute_frame(struct process
* pcs
,
2994 const struct module_format
* modfmt
,
2995 const struct symt_function
* func
,
2996 DWORD_PTR ip
, const dwarf2_cuhead_t
* head
,
2997 struct location
* frame
)
2999 struct symt
** psym
= NULL
;
3000 struct location
* pframe
;
3001 dwarf2_traverse_context_t lctx
;
3002 enum location_error err
;
3005 for (i
=0; i
<vector_length(&func
->vchildren
); i
++)
3007 psym
= vector_at(&func
->vchildren
, i
);
3008 if (psym
&& symt_check_tag(*psym
, SymTagCustom
))
3010 pframe
= &((struct symt_hierarchy_point
*)*psym
)->loc
;
3012 /* First, recompute the frame information, if needed */
3013 switch (pframe
->kind
)
3019 case loc_dwarf2_location_list
:
3020 WARN("Searching loclist for %s\n", debugstr_a(func
->hash_elt
.name
));
3021 if (!dwarf2_lookup_loclist(modfmt
, head
,
3022 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ pframe
->offset
,
3024 return loc_err_out_of_scope
;
3025 if ((err
= compute_location(modfmt
->module
, head
,
3026 &lctx
, frame
, pcs
->handle
, NULL
)) < 0) return err
;
3027 if (frame
->kind
>= loc_user
)
3029 WARN("Couldn't compute runtime frame location\n");
3030 return loc_err_too_complex
;
3033 case loc_dwarf2_frame_cfa
:
3034 if (!compute_call_frame_cfa(modfmt
->module
, ip
+ ((struct symt_compiland
*)func
->container
)->address
, frame
)) return loc_err_internal
;
3037 WARN("Unsupported frame kind %d\n", pframe
->kind
);
3038 return loc_err_internal
;
3043 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
3044 return loc_err_internal
;
3049 RULE_UNSET
, /* not set at all */
3050 RULE_UNDEFINED
, /* undefined value */
3051 RULE_SAME
, /* same value as previous frame */
3052 RULE_CFA_OFFSET
, /* stored at cfa offset */
3053 RULE_OTHER_REG
, /* stored in other register */
3054 RULE_EXPRESSION
, /* address specified by expression */
3055 RULE_VAL_EXPRESSION
/* value specified by expression */
3058 /* make it large enough for all CPUs */
3059 #define NB_FRAME_REGS 64
3060 #define MAX_SAVED_STATES 16
3064 ULONG_PTR cfa_offset
;
3065 unsigned char cfa_reg
;
3066 enum reg_rule cfa_rule
;
3067 enum reg_rule rules
[NB_FRAME_REGS
];
3068 ULONG_PTR regs
[NB_FRAME_REGS
];
3074 ULONG_PTR code_align
;
3075 LONG_PTR data_align
;
3076 unsigned char retaddr_reg
;
3077 unsigned char fde_encoding
;
3078 unsigned char lsda_encoding
;
3079 unsigned char signal_frame
;
3080 unsigned char aug_z_format
;
3081 unsigned char state_sp
;
3082 struct frame_state state
;
3083 struct frame_state state_stack
[MAX_SAVED_STATES
];
3086 static ULONG_PTR
dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t
* ctx
, unsigned char encoding
, unsigned char word_size
)
3090 if (encoding
== DW_EH_PE_omit
) return 0;
3092 switch (encoding
& 0xf0)
3097 case DW_EH_PE_pcrel
:
3098 base
= (ULONG_PTR
)ctx
->data
;
3101 FIXME("unsupported encoding %02x\n", encoding
);
3105 switch (encoding
& 0x0f)
3107 case DW_EH_PE_native
:
3108 return base
+ dwarf2_parse_addr(ctx
, word_size
);
3109 case DW_EH_PE_leb128
:
3110 return base
+ dwarf2_leb128_as_unsigned(ctx
);
3111 case DW_EH_PE_data2
:
3112 return base
+ dwarf2_parse_u2(ctx
);
3113 case DW_EH_PE_data4
:
3114 return base
+ dwarf2_parse_u4(ctx
);
3115 case DW_EH_PE_data8
:
3116 return base
+ dwarf2_parse_u8(ctx
);
3117 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
3118 return base
+ dwarf2_leb128_as_signed(ctx
);
3119 case DW_EH_PE_signed
|DW_EH_PE_data2
:
3120 return base
+ (signed short)dwarf2_parse_u2(ctx
);
3121 case DW_EH_PE_signed
|DW_EH_PE_data4
:
3122 return base
+ (signed int)dwarf2_parse_u4(ctx
);
3123 case DW_EH_PE_signed
|DW_EH_PE_data8
:
3124 return base
+ (LONG64
)dwarf2_parse_u8(ctx
);
3126 FIXME("unsupported encoding %02x\n", encoding
);
3131 static BOOL
parse_cie_details(dwarf2_traverse_context_t
* ctx
, struct frame_info
* info
, unsigned char word_size
)
3133 unsigned char version
;
3134 const char* augmentation
;
3135 const unsigned char* end
;
3138 memset(info
, 0, sizeof(*info
));
3139 info
->lsda_encoding
= DW_EH_PE_omit
;
3140 info
->aug_z_format
= 0;
3142 /* parse the CIE first */
3143 version
= dwarf2_parse_byte(ctx
);
3144 if (version
!= 1 && version
!= 3 && version
!= 4)
3146 FIXME("unknown CIE version %u at %p\n", version
, ctx
->data
- 1);
3149 augmentation
= (const char*)ctx
->data
;
3150 ctx
->data
+= strlen(augmentation
) + 1;
3155 /* skip 'address_size' and 'segment_size' */
3160 info
->code_align
= dwarf2_leb128_as_unsigned(ctx
);
3161 info
->data_align
= dwarf2_leb128_as_signed(ctx
);
3162 info
->retaddr_reg
= version
== 1 ? dwarf2_parse_byte(ctx
) :dwarf2_leb128_as_unsigned(ctx
);
3167 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
3170 TRACE("\tparsing augmentation %s\n", debugstr_a(augmentation
));
3171 if (*augmentation
) do
3173 switch (*augmentation
)
3176 len
= dwarf2_leb128_as_unsigned(ctx
);
3177 end
= ctx
->data
+ len
;
3178 info
->aug_z_format
= 1;
3181 info
->lsda_encoding
= dwarf2_parse_byte(ctx
);
3185 unsigned char encoding
= dwarf2_parse_byte(ctx
);
3186 /* throw away the indirect bit, as we don't care for the result */
3187 encoding
&= ~DW_EH_PE_indirect
;
3188 dwarf2_parse_augmentation_ptr(ctx
, encoding
, word_size
); /* handler */
3192 info
->fde_encoding
= dwarf2_parse_byte(ctx
);
3195 info
->signal_frame
= 1;
3198 FIXME("unknown augmentation '%c'\n", *augmentation
);
3199 if (!end
) return FALSE
;
3201 } while (*++augmentation
);
3202 if (end
) ctx
->data
= end
;
3206 static BOOL
dwarf2_get_cie(ULONG_PTR addr
, struct module
* module
, DWORD_PTR delta
,
3207 dwarf2_traverse_context_t
* fde_ctx
, dwarf2_traverse_context_t
* cie_ctx
,
3208 struct frame_info
* info
, BOOL in_eh_frame
)
3210 const unsigned char* ptr_blk
;
3211 const unsigned char* cie_ptr
;
3212 const unsigned char* last_cie_ptr
= (const unsigned char*)~0;
3214 ULONG_PTR start
, range
;
3216 const BYTE
* start_data
= fde_ctx
->data
;
3217 unsigned char word_size
= module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
3218 unsigned char offset_size
;
3220 /* skip 0-padding at beginning of section (alignment) */
3221 while (fde_ctx
->data
+ 2 * 4 < fde_ctx
->end_data
)
3223 if (dwarf2_parse_u4(fde_ctx
))
3229 for (; fde_ctx
->data
+ 2 * 4 < fde_ctx
->end_data
; fde_ctx
->data
= ptr_blk
)
3231 const unsigned char* st
= fde_ctx
->data
;
3232 /* find the FDE for address addr (skip CIE) */
3233 len
= dwarf2_parse_3264(fde_ctx
, &offset_size
);
3234 cie_id
= in_eh_frame
? 0 : (offset_size
== 4 ? DW_CIE_ID
: (ULONG_PTR
)DW64_CIE_ID
);
3235 ptr_blk
= fde_ctx
->data
+ len
;
3236 id
= dwarf2_parse_offset(fde_ctx
, offset_size
);
3240 /* we need some bits out of the CIE in order to parse all contents */
3241 if (!parse_cie_details(fde_ctx
, info
, word_size
)) return FALSE
;
3242 cie_ctx
->data
= fde_ctx
->data
;
3243 cie_ctx
->end_data
= ptr_blk
;
3246 cie_ptr
= (in_eh_frame
) ? fde_ctx
->data
- id
- 4 : start_data
+ id
;
3247 if (cie_ptr
!= last_cie_ptr
)
3249 last_cie_ptr
= cie_ptr
;
3250 cie_ctx
->data
= cie_ptr
;
3251 cie_ctx
->end_data
= cie_ptr
+ (offset_size
== 4 ? 4 : 4 + 8);
3252 cie_ctx
->end_data
+= dwarf2_parse_3264(cie_ctx
, &offset_size
);
3254 if (dwarf2_parse_offset(cie_ctx
, in_eh_frame
? word_size
: offset_size
) != cie_id
)
3256 FIXME("wrong CIE pointer at %x from FDE %x\n",
3257 (unsigned)(cie_ptr
- start_data
),
3258 (unsigned)(fde_ctx
->data
- start_data
));
3261 if (!parse_cie_details(cie_ctx
, info
, word_size
)) return FALSE
;
3263 start
= delta
+ dwarf2_parse_augmentation_ptr(fde_ctx
, info
->fde_encoding
, word_size
);
3264 range
= dwarf2_parse_augmentation_ptr(fde_ctx
, info
->fde_encoding
& 0x0F, word_size
);
3266 if (addr
>= start
&& addr
< start
+ range
)
3268 /* reset the FDE context */
3269 fde_ctx
->end_data
= ptr_blk
;
3278 static int valid_reg(ULONG_PTR reg
)
3280 if (reg
>= NB_FRAME_REGS
) FIXME("unsupported reg %lx\n", reg
);
3281 return (reg
< NB_FRAME_REGS
);
3284 static void execute_cfa_instructions(struct module
* module
, dwarf2_traverse_context_t
* ctx
,
3285 ULONG_PTR last_ip
, struct frame_info
*info
)
3287 while (ctx
->data
< ctx
->end_data
&& info
->ip
<= last_ip
+ info
->signal_frame
)
3289 enum dwarf_call_frame_info op
= dwarf2_parse_byte(ctx
);
3295 case DW_CFA_advance_loc
:
3297 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
3298 TRACE("%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
3304 ULONG_PTR reg
= op
& 0x3f;
3305 LONG_PTR offset
= dwarf2_leb128_as_unsigned(ctx
) * info
->data_align
;
3306 if (!valid_reg(reg
)) break;
3307 TRACE("%lx: DW_CFA_offset %s, %ld\n",
3309 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3311 info
->state
.regs
[reg
] = offset
;
3312 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
3315 case DW_CFA_restore
:
3317 ULONG_PTR reg
= op
& 0x3f;
3318 if (!valid_reg(reg
)) break;
3319 TRACE("%lx: DW_CFA_restore %s\n",
3321 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3322 info
->state
.rules
[reg
] = RULE_UNSET
;
3331 case DW_CFA_set_loc
:
3333 ULONG_PTR loc
= dwarf2_parse_augmentation_ptr(ctx
, info
->fde_encoding
,
3334 module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
);
3335 TRACE("%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
3339 case DW_CFA_advance_loc1
:
3341 ULONG_PTR offset
= dwarf2_parse_byte(ctx
) * info
->code_align
;
3342 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
3346 case DW_CFA_advance_loc2
:
3348 ULONG_PTR offset
= dwarf2_parse_u2(ctx
) * info
->code_align
;
3349 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
3353 case DW_CFA_advance_loc4
:
3355 ULONG_PTR offset
= dwarf2_parse_u4(ctx
) * info
->code_align
;
3356 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
3360 case DW_CFA_offset_extended
:
3361 case DW_CFA_offset_extended_sf
:
3363 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3364 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf2_leb128_as_unsigned(ctx
) * info
->data_align
3365 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
3366 if (!valid_reg(reg
)) break;
3367 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
3369 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3371 info
->state
.regs
[reg
] = offset
;
3372 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
3375 case DW_CFA_restore_extended
:
3377 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3378 if (!valid_reg(reg
)) break;
3379 TRACE("%lx: DW_CFA_restore_extended %s\n",
3381 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3382 info
->state
.rules
[reg
] = RULE_UNSET
;
3385 case DW_CFA_undefined
:
3387 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3388 if (!valid_reg(reg
)) break;
3389 TRACE("%lx: DW_CFA_undefined %s\n",
3391 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3392 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
3395 case DW_CFA_same_value
:
3397 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3398 if (!valid_reg(reg
)) break;
3399 TRACE("%lx: DW_CFA_same_value %s\n",
3401 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3402 info
->state
.regs
[reg
] = reg
;
3403 info
->state
.rules
[reg
] = RULE_SAME
;
3406 case DW_CFA_register
:
3408 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3409 ULONG_PTR reg2
= dwarf2_leb128_as_unsigned(ctx
);
3410 if (!valid_reg(reg
) || !valid_reg(reg2
)) break;
3411 TRACE("%lx: DW_CFA_register %s == %s\n",
3413 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3414 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg2
, module
, TRUE
)));
3415 info
->state
.regs
[reg
] = reg2
;
3416 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
3419 case DW_CFA_remember_state
:
3420 TRACE("%lx: DW_CFA_remember_state\n", info
->ip
);
3421 if (info
->state_sp
>= MAX_SAVED_STATES
)
3422 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info
->ip
);
3424 info
->state_stack
[info
->state_sp
++] = info
->state
;
3426 case DW_CFA_restore_state
:
3427 TRACE("%lx: DW_CFA_restore_state\n", info
->ip
);
3428 if (!info
->state_sp
)
3429 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info
->ip
);
3431 info
->state
= info
->state_stack
[--info
->state_sp
];
3433 case DW_CFA_def_cfa
:
3434 case DW_CFA_def_cfa_sf
:
3436 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3437 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf2_leb128_as_unsigned(ctx
)
3438 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
3439 if (!valid_reg(reg
)) break;
3440 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
3442 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3444 info
->state
.cfa_reg
= reg
;
3445 info
->state
.cfa_offset
= offset
;
3446 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
3449 case DW_CFA_def_cfa_register
:
3451 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3452 if (!valid_reg(reg
)) break;
3453 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
3455 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3456 info
->state
.cfa_reg
= reg
;
3457 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
3460 case DW_CFA_def_cfa_offset
:
3461 case DW_CFA_def_cfa_offset_sf
:
3463 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf2_leb128_as_unsigned(ctx
)
3464 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
3465 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info
->ip
, offset
);
3466 info
->state
.cfa_offset
= offset
;
3467 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
3470 case DW_CFA_def_cfa_expression
:
3472 ULONG_PTR expr
= (ULONG_PTR
)ctx
->data
;
3473 ULONG_PTR len
= dwarf2_leb128_as_unsigned(ctx
);
3474 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info
->ip
, expr
, expr
+len
);
3475 info
->state
.cfa_offset
= expr
;
3476 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
3480 case DW_CFA_expression
:
3481 case DW_CFA_val_expression
:
3483 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3484 ULONG_PTR expr
= (ULONG_PTR
)ctx
->data
;
3485 ULONG_PTR len
= dwarf2_leb128_as_unsigned(ctx
);
3486 if (!valid_reg(reg
)) break;
3487 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
3488 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_",
3489 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3491 info
->state
.regs
[reg
] = expr
;
3492 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
3496 case DW_CFA_GNU_args_size
:
3497 /* FIXME: should check that GCC is the compiler for this CU */
3499 ULONG_PTR args
= dwarf2_leb128_as_unsigned(ctx
);
3500 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info
->ip
, args
);
3505 FIXME("%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
3511 /* retrieve a context register from its dwarf number */
3512 static DWORD64
get_context_reg(const struct module
* module
, struct cpu_stack_walk
*csw
, union ctx
*context
,
3515 unsigned regno
= csw
->cpu
->map_dwarf_register(dw_reg
, module
, TRUE
), sz
;
3516 void* ptr
= csw
->cpu
->fetch_context_reg(context
, regno
, &sz
);
3518 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3520 return *(DWORD64
*)ptr
;
3522 return *(DWORD
*)ptr
;
3524 FIXME("unhandled size %d\n", sz
);
3528 /* set a context register from its dwarf number */
3529 static void set_context_reg(const struct module
* module
, struct cpu_stack_walk
* csw
, union ctx
*context
,
3530 ULONG_PTR dw_reg
, ULONG_PTR val
, BOOL isdebuggee
)
3532 unsigned regno
= csw
->cpu
->map_dwarf_register(dw_reg
, module
, TRUE
), sz
;
3533 ULONG_PTR
* ptr
= csw
->cpu
->fetch_context_reg(context
, regno
, &sz
);
3535 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3540 if (sz
> sizeof(tmp
))
3542 FIXME("register %lu/%u size is too wide: %u\n", dw_reg
, regno
, sz
);
3545 if (!sw_read_mem(csw
, val
, tmp
, sz
))
3547 WARN("Couldn't read memory at %p\n", (void*)val
);
3550 memcpy(ptr
, tmp
, sz
);
3554 if (sz
!= sizeof(ULONG_PTR
))
3556 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg
, regno
, sz
);
3563 /* copy a register from one context to another using dwarf number */
3564 static void copy_context_reg(const struct module
* module
, struct cpu_stack_walk
*csw
,
3565 union ctx
*dstcontext
, ULONG_PTR dwregdst
,
3566 union ctx
*srccontext
, ULONG_PTR dwregsrc
)
3568 unsigned regdstno
= csw
->cpu
->map_dwarf_register(dwregdst
, module
, TRUE
), szdst
;
3569 unsigned regsrcno
= csw
->cpu
->map_dwarf_register(dwregsrc
, module
, TRUE
), szsrc
;
3570 ULONG_PTR
* ptrdst
= csw
->cpu
->fetch_context_reg(dstcontext
, regdstno
, &szdst
);
3571 ULONG_PTR
* ptrsrc
= csw
->cpu
->fetch_context_reg(srccontext
, regsrcno
, &szsrc
);
3573 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3576 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3577 dwregsrc
, regsrcno
, dwregdst
, regdstno
, szsrc
, szdst
);
3580 memcpy(ptrdst
, ptrsrc
, szdst
);
3583 static ULONG_PTR
eval_expression(const struct module
* module
, struct cpu_stack_walk
* csw
,
3584 const unsigned char* zp
, union ctx
*context
)
3586 dwarf2_traverse_context_t ctx
;
3587 ULONG_PTR reg
, sz
, tmp
;
3592 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3594 ctx
.end_data
= zp
+ 4;
3595 len
= dwarf2_leb128_as_unsigned(&ctx
);
3596 ctx
.end_data
= ctx
.data
+ len
;
3598 while (ctx
.data
< ctx
.end_data
)
3600 unsigned char opcode
= dwarf2_parse_byte(&ctx
);
3602 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
3603 stack
[++sp
] = opcode
- DW_OP_lit0
;
3604 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
3605 stack
[++sp
] = get_context_reg(module
, csw
, context
, opcode
- DW_OP_reg0
);
3606 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
3607 stack
[++sp
] = get_context_reg(module
, csw
, context
, opcode
- DW_OP_breg0
)
3608 + dwarf2_leb128_as_signed(&ctx
);
3609 else switch (opcode
)
3611 case DW_OP_nop
: break;
3612 case DW_OP_addr
: stack
[++sp
] = dwarf2_parse_addr(&ctx
, module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
); break;
3613 case DW_OP_const1u
: stack
[++sp
] = dwarf2_parse_byte(&ctx
); break;
3614 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf2_parse_byte(&ctx
); break;
3615 case DW_OP_const2u
: stack
[++sp
] = dwarf2_parse_u2(&ctx
); break;
3616 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf2_parse_u2(&ctx
); break;
3617 case DW_OP_const4u
: stack
[++sp
] = dwarf2_parse_u4(&ctx
); break;
3618 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf2_parse_u4(&ctx
); break;
3619 case DW_OP_const8u
: stack
[++sp
] = dwarf2_parse_u8(&ctx
); break;
3620 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf2_parse_u8(&ctx
); break;
3621 case DW_OP_constu
: stack
[++sp
] = dwarf2_leb128_as_unsigned(&ctx
); break;
3622 case DW_OP_consts
: stack
[++sp
] = dwarf2_leb128_as_signed(&ctx
); break;
3625 if (!sw_read_mem(csw
, stack
[sp
], &tmp
, module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
))
3627 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack
[sp
]));
3632 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
3633 case DW_OP_drop
: sp
--; break;
3634 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
3635 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf2_parse_byte(&ctx
)]; sp
++; break;
3636 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
3637 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
3638 case DW_OP_abs
: stack
[sp
] = sizeof(stack
[sp
]) == 8 ? llabs((INT64
)stack
[sp
]) : abs((INT32
)stack
[sp
]); break;
3639 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
3640 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
3641 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
3642 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
3643 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
3644 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
3645 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
3646 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
3647 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
3648 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
3649 case DW_OP_plus_uconst
: stack
[sp
] += dwarf2_leb128_as_unsigned(&ctx
); break;
3650 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
3651 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
3652 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
3653 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
3654 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
3655 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
3656 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
3657 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
3658 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
3659 case DW_OP_skip
: tmp
= (short)dwarf2_parse_u2(&ctx
); ctx
.data
+= tmp
; break;
3660 case DW_OP_bra
: tmp
= (short)dwarf2_parse_u2(&ctx
); if (!stack
[sp
--]) ctx
.data
+= tmp
; break;
3661 case DW_OP_GNU_encoded_addr
:
3662 tmp
= dwarf2_parse_byte(&ctx
);
3663 stack
[++sp
] = dwarf2_parse_augmentation_ptr(&ctx
, tmp
, module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
);
3666 stack
[++sp
] = get_context_reg(module
, csw
, context
, dwarf2_leb128_as_unsigned(&ctx
));
3669 reg
= dwarf2_leb128_as_unsigned(&ctx
);
3670 tmp
= dwarf2_leb128_as_signed(&ctx
);
3671 stack
[++sp
] = get_context_reg(module
, csw
, context
, reg
) + tmp
;
3673 case DW_OP_deref_size
:
3674 sz
= dwarf2_parse_byte(&ctx
);
3675 if (!sw_read_mem(csw
, stack
[sp
], &tmp
, sz
))
3677 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack
[sp
]));
3680 /* do integral promotion */
3683 case 1: stack
[sp
] = *(unsigned char*)&tmp
; break;
3684 case 2: stack
[sp
] = *(unsigned short*)&tmp
; break;
3685 case 4: stack
[sp
] = *(unsigned int*)&tmp
; break;
3686 case 8: stack
[sp
] = tmp
; break; /* FIXME: won't work on 32bit platform */
3687 default: FIXME("Unknown size for deref 0x%lx\n", sz
);
3691 FIXME("unhandled opcode %02x\n", opcode
);
3697 static void apply_frame_state(const struct module
* module
, struct cpu_stack_walk
* csw
,
3698 union ctx
*context
, struct frame_state
*state
, DWORD64
*cfa
)
3702 union ctx new_context
= *context
;
3704 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3705 switch (state
->cfa_rule
)
3707 case RULE_EXPRESSION
:
3708 *cfa
= eval_expression(module
, csw
, (const unsigned char*)state
->cfa_offset
, context
);
3709 if (!sw_read_mem(csw
, *cfa
, cfa
, csw
->cpu
->word_size
))
3711 WARN("Couldn't read memory at %s\n", wine_dbgstr_longlong(*cfa
));
3715 case RULE_VAL_EXPRESSION
:
3716 *cfa
= eval_expression(module
, csw
, (const unsigned char*)state
->cfa_offset
, context
);
3719 *cfa
= get_context_reg(module
, csw
, context
, state
->cfa_reg
) + state
->cfa_offset
;
3724 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
3726 switch (state
->rules
[i
])
3729 case RULE_UNDEFINED
:
3732 case RULE_CFA_OFFSET
:
3733 set_context_reg(module
, csw
, &new_context
, i
, *cfa
+ state
->regs
[i
], TRUE
);
3735 case RULE_OTHER_REG
:
3736 copy_context_reg(module
, csw
, &new_context
, i
, context
, state
->regs
[i
]);
3738 case RULE_EXPRESSION
:
3739 value
= eval_expression(module
, csw
, (const unsigned char*)state
->regs
[i
], context
);
3740 set_context_reg(module
, csw
, &new_context
, i
, value
, TRUE
);
3742 case RULE_VAL_EXPRESSION
:
3743 value
= eval_expression(module
, csw
, (const unsigned char*)state
->regs
[i
], context
);
3744 set_context_reg(module
, csw
, &new_context
, i
, value
, FALSE
);
3748 *context
= new_context
;
3751 static BOOL
dwarf2_fetch_frame_info(struct module
* module
, struct cpu
* cpu
, LONG_PTR ip
, struct frame_info
* info
)
3753 dwarf2_traverse_context_t cie_ctx
, fde_ctx
;
3754 struct module_format
* modfmt
;
3755 const unsigned char* end
;
3758 modfmt
= module
->format_info
[DFI_DWARF
];
3759 if (!modfmt
) return FALSE
;
3760 memset(info
, 0, sizeof(*info
));
3761 fde_ctx
.data
= modfmt
->u
.dwarf2_info
->eh_frame
.address
;
3762 fde_ctx
.end_data
= fde_ctx
.data
+ modfmt
->u
.dwarf2_info
->eh_frame
.size
;
3763 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3764 * in this process the IMAGE section at a different address as the one expected by
3767 delta
= module
->module
.BaseOfImage
+ modfmt
->u
.dwarf2_info
->eh_frame
.rva
-
3768 (DWORD_PTR
)modfmt
->u
.dwarf2_info
->eh_frame
.address
;
3769 if (!dwarf2_get_cie(ip
, module
, delta
, &fde_ctx
, &cie_ctx
, info
, TRUE
))
3771 fde_ctx
.data
= modfmt
->u
.dwarf2_info
->debug_frame
.address
;
3772 fde_ctx
.end_data
= fde_ctx
.data
+ modfmt
->u
.dwarf2_info
->debug_frame
.size
;
3773 delta
= module
->reloc_delta
;
3774 if (!dwarf2_get_cie(ip
, module
, delta
, &fde_ctx
, &cie_ctx
, info
, FALSE
))
3776 TRACE("Couldn't find information for %lx\n", ip
);
3781 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3782 ip
, info
->ip
, info
->code_align
, info
->data_align
,
3783 cpu
->fetch_regname(cpu
->map_dwarf_register(info
->retaddr_reg
, module
, TRUE
)));
3787 execute_cfa_instructions(module
, &cie_ctx
, ip
, info
);
3789 if (info
->aug_z_format
) /* get length of augmentation data */
3791 ULONG_PTR len
= dwarf2_leb128_as_unsigned(&fde_ctx
);
3792 end
= fde_ctx
.data
+ len
;
3795 dwarf2_parse_augmentation_ptr(&fde_ctx
, info
->lsda_encoding
, modfmt
->u
.dwarf2_info
->word_size
); /* handler_data */
3796 if (end
) fde_ctx
.data
= end
;
3798 execute_cfa_instructions(module
, &fde_ctx
, ip
, info
);
3803 /***********************************************************************
3804 * dwarf2_virtual_unwind
3807 BOOL
dwarf2_virtual_unwind(struct cpu_stack_walk
*csw
, ULONG_PTR ip
,
3808 union ctx
*context
, DWORD64
*cfa
)
3810 struct module_pair pair
;
3811 struct frame_info info
;
3813 if (!module_init_pair(&pair
, csw
->hProcess
, ip
)) return FALSE
;
3814 if (csw
->cpu
!= pair
.effective
->cpu
) FIXME("mismatch in cpu\n");
3815 if (!dwarf2_fetch_frame_info(pair
.effective
, csw
->cpu
, ip
, &info
)) return FALSE
;
3817 /* if at very beginning of function, return and use default unwinder */
3818 if (ip
== info
.ip
) return FALSE
;
3820 /* if there is no information about retaddr, use default unwinder */
3821 if (info
.state
.rules
[info
.retaddr_reg
] == RULE_UNSET
) return FALSE
;
3823 apply_frame_state(pair
.effective
, csw
, context
, &info
.state
, cfa
);
3828 static BOOL
compute_call_frame_cfa(struct module
* module
, ULONG_PTR ip
, struct location
* frame
)
3830 struct frame_info info
;
3832 if (!dwarf2_fetch_frame_info(module
, module
->cpu
, ip
, &info
)) return FALSE
;
3834 /* beginning of function, or no available dwarf information ? */
3835 if (ip
== info
.ip
|| info
.state
.rules
[info
.retaddr_reg
] == RULE_UNSET
)
3837 /* fake the default unwinder */
3838 frame
->kind
= loc_regrel
;
3839 frame
->reg
= module
->cpu
->frame_regno
;
3840 frame
->offset
= module
->cpu
->word_size
; /* FIXME stack direction */
3844 /* we expect to translate the call_frame_cfa into a regrel location...
3845 * that should cover most of the cases
3847 switch (info
.state
.cfa_rule
)
3849 case RULE_EXPRESSION
:
3850 FIXME("Too complex expression for frame_CFA resolution (RULE_EXPRESSION)\n");
3852 case RULE_VAL_EXPRESSION
:
3853 FIXME("Too complex expression for frame_CFA resolution (RULE_VAL_EXPRESSION)\n");
3856 frame
->kind
= loc_regrel
;
3857 frame
->reg
= module
->cpu
->map_dwarf_register(info
.state
.cfa_reg
, module
, TRUE
);
3858 frame
->offset
= info
.state
.cfa_offset
;
3865 static void dwarf2_location_compute(struct process
* pcs
,
3866 const struct module_format
* modfmt
,
3867 const struct symt_function
* func
,
3868 struct location
* loc
)
3870 struct location frame
;
3873 dwarf2_traverse_context_t lctx
;
3874 const dwarf2_cuhead_t
* head
= get_cuhead_from_func(func
);
3878 WARN("We'd expect function %s's container to be a valid compiland with dwarf inforamation\n",
3879 debugstr_a(func
->hash_elt
.name
));
3880 err
= loc_err_internal
;
3884 /* instruction pointer relative to compiland's start */
3885 ip
= pcs
->localscope_pc
- ((struct symt_compiland
*)func
->container
)->address
;
3887 if ((err
= loc_compute_frame(pcs
, modfmt
, func
, ip
, head
, &frame
)) == 0)
3891 case loc_dwarf2_location_list
:
3892 /* Then, if the variable has a location list, find it !! */
3893 if (dwarf2_lookup_loclist(modfmt
, head
,
3894 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ loc
->offset
,
3897 err
= loc_err_out_of_scope
;
3899 case loc_dwarf2_block
:
3900 /* or if we have a copy of an existing block, get ready for it */
3902 unsigned* ptr
= (unsigned*)loc
->offset
;
3904 lctx
.data
= (const BYTE
*)(ptr
+ 1);
3905 lctx
.end_data
= lctx
.data
+ *ptr
;
3908 /* now get the variable */
3909 err
= compute_location(modfmt
->module
, head
,
3910 &lctx
, loc
, pcs
->handle
, &frame
);
3917 WARN("Unsupported local kind %d\n", loc
->kind
);
3918 err
= loc_err_internal
;
3924 loc
->kind
= loc_register
;
3929 static void *zalloc(void *priv
, uInt items
, uInt sz
)
3931 return HeapAlloc(GetProcessHeap(), 0, items
* sz
);
3934 static void zfree(void *priv
, void *addr
)
3936 HeapFree(GetProcessHeap(), 0, addr
);
3939 static inline BOOL
dwarf2_init_zsection(dwarf2_section_t
* section
,
3940 const char* zsectname
,
3941 struct image_section_map
* ism
)
3948 BYTE
*addr
, *sect
= (BYTE
*)image_map_section(ism
);
3949 size_t sz
= image_get_map_size(ism
);
3951 if (sz
<= 12 || memcmp(sect
, "ZLIB", 4))
3953 ERR("invalid compressed section %s\n", debugstr_a(zsectname
));
3957 #ifdef WORDS_BIGENDIAN
3958 li
.u
.HighPart
= *(DWORD
*)§
[4];
3959 li
.u
.LowPart
= *(DWORD
*)§
[8];
3961 li
.u
.HighPart
= RtlUlongByteSwap(*(DWORD
*)§
[4]);
3962 li
.u
.LowPart
= RtlUlongByteSwap(*(DWORD
*)§
[8]);
3965 addr
= HeapAlloc(GetProcessHeap(), 0, li
.QuadPart
);
3969 z
.next_in
= §
[12];
3970 z
.avail_in
= sz
- 12;
3975 res
= inflateInit(&z
);
3978 FIXME("inflateInit failed with %i / %s\n", res
, debugstr_a(z
.msg
));
3983 z
.next_out
= addr
+ z
.total_out
;
3984 z
.avail_out
= li
.QuadPart
- z
.total_out
;
3985 res
= inflate(&z
, Z_FINISH
);
3986 } while (z
.avail_in
&& res
== Z_STREAM_END
);
3988 if (res
!= Z_STREAM_END
)
3990 FIXME("Decompression failed with %i / %s\n", res
, debugstr_a(z
.msg
));
3995 section
->compressed
= TRUE
;
3996 section
->address
= addr
;
3997 section
->rva
= image_get_map_rva(ism
);
3998 section
->size
= z
.total_out
;
4004 HeapFree(GetProcessHeap(), 0, addr
);
4006 image_unmap_section(ism
);
4010 static inline BOOL
dwarf2_init_section(dwarf2_section_t
* section
, struct image_file_map
* fmap
,
4011 const char* sectname
, const char* zsectname
,
4012 struct image_section_map
* ism
)
4014 struct image_section_map local_ism
;
4016 if (!ism
) ism
= &local_ism
;
4018 section
->compressed
= FALSE
;
4019 if (image_find_section(fmap
, sectname
, ism
))
4021 section
->address
= (const BYTE
*)image_map_section(ism
);
4022 section
->size
= image_get_map_size(ism
);
4023 section
->rva
= image_get_map_rva(ism
);
4027 section
->address
= NULL
;
4031 if (zsectname
&& image_find_section(fmap
, zsectname
, ism
))
4033 return dwarf2_init_zsection(section
, zsectname
, ism
);
4039 static inline void dwarf2_fini_section(dwarf2_section_t
* section
)
4041 if (section
->compressed
)
4042 HeapFree(GetProcessHeap(), 0, (void*)section
->address
);
4045 static void dwarf2_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
4047 dwarf2_fini_section(&modfmt
->u
.dwarf2_info
->debug_loc
);
4048 dwarf2_fini_section(&modfmt
->u
.dwarf2_info
->debug_frame
);
4049 free(modfmt
->u
.dwarf2_info
->cuheads
);
4050 HeapFree(GetProcessHeap(), 0, modfmt
);
4053 static BOOL
dwarf2_load_CU_module(dwarf2_parse_module_context_t
* module_ctx
, struct module
* module
,
4054 dwarf2_section_t
* sections
, ULONG_PTR load_offset
,
4055 const struct elf_thunk_area
* thunks
, BOOL is_dwz
)
4057 dwarf2_traverse_context_t mod_ctx
;
4060 module_ctx
->sections
= sections
;
4061 module_ctx
->module
= module
;
4062 module_ctx
->thunks
= thunks
;
4063 module_ctx
->load_offset
= load_offset
;
4064 memset(module_ctx
->symt_cache
, 0, sizeof(module_ctx
->symt_cache
));
4065 module_ctx
->symt_cache
[sc_void
] = &symt_new_basic(module_ctx
->module
, btVoid
, "void", 0)->symt
;
4066 module_ctx
->symt_cache
[sc_unknown
] = &symt_new_basic(module_ctx
->module
, btNoType
, "# unknown", 0)->symt
;
4067 vector_init(&module_ctx
->unit_contexts
, sizeof(dwarf2_parse_context_t
), 16);
4068 module_ctx
->cu_versions
= 0;
4070 /* phase I: parse all CU heads */
4071 mod_ctx
.data
= sections
[section_debug
].address
;
4072 mod_ctx
.end_data
= mod_ctx
.data
+ sections
[section_debug
].size
;
4073 while (mod_ctx
.data
< mod_ctx
.end_data
)
4075 dwarf2_parse_context_t
* unit_ctx
= vector_add(&module_ctx
->unit_contexts
, &module_ctx
->module
->pool
);
4077 unit_ctx
->module_ctx
= module_ctx
;
4078 dwarf2_parse_compilation_unit_head(unit_ctx
, &mod_ctx
);
4081 /* phase2: load content of all CU
4082 * If this is a DWZ alternate module, don't load all debug_info at once
4083 * wait for main module to ask for them (it's likely it won't need them all)
4084 * Doing this can lead to a huge performance improvement.
4087 for (i
= 0; i
< module_ctx
->unit_contexts
.num_elts
; ++i
)
4088 dwarf2_parse_compilation_unit((dwarf2_parse_context_t
*)vector_at(&module_ctx
->unit_contexts
, i
));
4093 static dwarf2_dwz_alternate_t
* dwarf2_load_dwz(struct image_file_map
* fmap
, struct module
* module
)
4095 struct image_file_map
* fmap_dwz
;
4096 dwarf2_dwz_alternate_t
* dwz
;
4098 fmap_dwz
= image_load_debugaltlink(fmap
, module
);
4099 if (!fmap_dwz
) return NULL
;
4100 if (!(dwz
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dwz
))))
4102 image_unmap_file(fmap_dwz
);
4103 HeapFree(GetProcessHeap(), 0, fmap_dwz
);
4107 dwz
->fmap
= fmap_dwz
;
4108 dwarf2_init_section(&dwz
->sections
[section_debug
], fmap_dwz
, ".debug_info", ".zdebug_info", &dwz
->sectmap
[section_debug
]);
4109 dwarf2_init_section(&dwz
->sections
[section_abbrev
], fmap_dwz
, ".debug_abbrev", ".zdebug_abbrev", &dwz
->sectmap
[section_abbrev
]);
4110 dwarf2_init_section(&dwz
->sections
[section_string
], fmap_dwz
, ".debug_str", ".zdebug_str", &dwz
->sectmap
[section_string
]);
4111 dwarf2_init_section(&dwz
->sections
[section_line
], fmap_dwz
, ".debug_line", ".zdebug_line", &dwz
->sectmap
[section_line
]);
4112 dwarf2_init_section(&dwz
->sections
[section_ranges
], fmap_dwz
, ".debug_ranges", ".zdebug_ranges", &dwz
->sectmap
[section_ranges
]);
4114 dwz
->module_ctx
.dwz
= NULL
;
4115 dwarf2_load_CU_module(&dwz
->module_ctx
, module
, dwz
->sections
, 0/*FIXME*/, NULL
, TRUE
);
4119 static void dwarf2_unload_dwz(dwarf2_dwz_alternate_t
* dwz
)
4122 dwarf2_fini_section(&dwz
->sections
[section_debug
]);
4123 dwarf2_fini_section(&dwz
->sections
[section_abbrev
]);
4124 dwarf2_fini_section(&dwz
->sections
[section_string
]);
4125 dwarf2_fini_section(&dwz
->sections
[section_line
]);
4126 dwarf2_fini_section(&dwz
->sections
[section_ranges
]);
4128 image_unmap_section(&dwz
->sectmap
[section_debug
]);
4129 image_unmap_section(&dwz
->sectmap
[section_abbrev
]);
4130 image_unmap_section(&dwz
->sectmap
[section_string
]);
4131 image_unmap_section(&dwz
->sectmap
[section_line
]);
4132 image_unmap_section(&dwz
->sectmap
[section_ranges
]);
4134 image_unmap_file(dwz
->fmap
);
4135 HeapFree(GetProcessHeap(), 0, dwz
);
4138 static BOOL
dwarf2_unload_CU_module(dwarf2_parse_module_context_t
* module_ctx
)
4141 for (i
= 0; i
< module_ctx
->unit_contexts
.num_elts
; ++i
)
4143 dwarf2_parse_context_t
* unit
= vector_at(&module_ctx
->unit_contexts
, i
);
4144 if (unit
->status
!= UNIT_ERROR
)
4145 pool_destroy(&unit
->pool
);
4147 dwarf2_unload_dwz(module_ctx
->dwz
);
4151 BOOL
dwarf2_parse(struct module
* module
, ULONG_PTR load_offset
,
4152 const struct elf_thunk_area
* thunks
,
4153 struct image_file_map
* fmap
)
4155 dwarf2_section_t eh_frame
, section
[section_max
];
4156 struct image_section_map debug_sect
, debug_str_sect
, debug_abbrev_sect
,
4157 debug_line_sect
, debug_ranges_sect
, eh_frame_sect
;
4159 struct module_format
* dwarf2_modfmt
;
4160 dwarf2_parse_module_context_t module_ctx
;
4162 if (!dwarf2_init_section(&eh_frame
, fmap
, ".eh_frame", NULL
, &eh_frame_sect
))
4163 /* lld produces .eh_fram to avoid generating a long name */
4164 dwarf2_init_section(&eh_frame
, fmap
, ".eh_fram", NULL
, &eh_frame_sect
);
4165 dwarf2_init_section(§ion
[section_debug
], fmap
, ".debug_info", ".zdebug_info", &debug_sect
);
4166 dwarf2_init_section(§ion
[section_abbrev
], fmap
, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect
);
4167 dwarf2_init_section(§ion
[section_string
], fmap
, ".debug_str", ".zdebug_str", &debug_str_sect
);
4168 dwarf2_init_section(§ion
[section_line
], fmap
, ".debug_line", ".zdebug_line", &debug_line_sect
);
4169 dwarf2_init_section(§ion
[section_ranges
], fmap
, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect
);
4171 /* to do anything useful we need either .eh_frame or .debug_info */
4172 if ((!eh_frame
.address
|| eh_frame
.address
== IMAGE_NO_MAP
) &&
4173 (!section
[section_debug
].address
|| section
[section_debug
].address
== IMAGE_NO_MAP
))
4179 if (fmap
->modtype
== DMT_ELF
&& debug_sect
.fmap
)
4181 /* debug info might have a different base address than .so file
4182 * when elf file is prelinked after splitting off debug info
4183 * adjust symbol base addresses accordingly
4185 load_offset
+= fmap
->u
.elf
.elf_start
- debug_sect
.fmap
->u
.elf
.elf_start
;
4188 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module
->modulename
));
4190 dwarf2_modfmt
= HeapAlloc(GetProcessHeap(), 0,
4191 sizeof(*dwarf2_modfmt
) + sizeof(*dwarf2_modfmt
->u
.dwarf2_info
));
4197 dwarf2_modfmt
->module
= module
;
4198 dwarf2_modfmt
->remove
= dwarf2_module_remove
;
4199 dwarf2_modfmt
->loc_compute
= dwarf2_location_compute
;
4200 dwarf2_modfmt
->u
.dwarf2_info
= (struct dwarf2_module_info_s
*)(dwarf2_modfmt
+ 1);
4201 dwarf2_modfmt
->u
.dwarf2_info
->word_size
= fmap
->addr_size
/ 8; /* set the word_size for eh_frame parsing */
4202 dwarf2_modfmt
->module
->format_info
[DFI_DWARF
] = dwarf2_modfmt
;
4204 /* As we'll need later some sections' content, we won't unmap these
4205 * sections upon existing this function
4207 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_loc
, fmap
, ".debug_loc", ".zdebug_loc", NULL
);
4208 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_frame
, fmap
, ".debug_frame", ".zdebug_frame", NULL
);
4209 dwarf2_modfmt
->u
.dwarf2_info
->eh_frame
= eh_frame
;
4210 dwarf2_modfmt
->u
.dwarf2_info
->cuheads
= NULL
;
4211 dwarf2_modfmt
->u
.dwarf2_info
->num_cuheads
= 0;
4213 module_ctx
.dwz
= dwarf2_load_dwz(fmap
, module
);
4214 dwarf2_load_CU_module(&module_ctx
, module
, section
, load_offset
, thunks
, FALSE
);
4216 dwarf2_modfmt
->module
->module
.SymType
= SymDia
;
4217 /* hide dwarf versions in CVSig
4218 * bits 24-31 will be set according to found dwarf version
4219 * different CU can have different dwarf version, so use a bit per version (version 2 => b24)
4221 dwarf2_modfmt
->module
->module
.CVSig
= 'D' | ('W' << 8) | ('F' << 16) | ((module_ctx
.cu_versions
& 0xFF) << 24);
4222 /* FIXME: we could have a finer grain here */
4223 dwarf2_modfmt
->module
->module
.GlobalSymbols
= TRUE
;
4224 dwarf2_modfmt
->module
->module
.TypeInfo
= TRUE
;
4225 dwarf2_modfmt
->module
->module
.SourceIndexed
= TRUE
;
4226 dwarf2_modfmt
->module
->module
.Publics
= TRUE
;
4228 dwarf2_unload_CU_module(&module_ctx
);
4231 dwarf2_fini_section(§ion
[section_debug
]);
4232 dwarf2_fini_section(§ion
[section_abbrev
]);
4233 dwarf2_fini_section(§ion
[section_string
]);
4234 dwarf2_fini_section(§ion
[section_line
]);
4235 dwarf2_fini_section(§ion
[section_ranges
]);
4237 image_unmap_section(&debug_sect
);
4238 image_unmap_section(&debug_abbrev_sect
);
4239 image_unmap_section(&debug_str_sect
);
4240 image_unmap_section(&debug_line_sect
);
4241 image_unmap_section(&debug_ranges_sect
);
4242 if (!ret
) image_unmap_section(&eh_frame_sect
);