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 typedef struct dwarf2_cuhead_s
167 unsigned char word_size
; /* size of a word on target machine */
168 unsigned char version
;
169 unsigned char offset_size
; /* size of offset inside DWARF */
172 typedef struct dwarf2_parse_module_context_s
174 ULONG_PTR load_offset
;
175 const dwarf2_section_t
* sections
;
176 struct module
* module
;
177 const struct elf_thunk_area
*thunks
;
178 struct vector unit_contexts
;
179 struct dwarf2_dwz_alternate_s
* dwz
;
181 } dwarf2_parse_module_context_t
;
183 typedef struct dwarf2_dwz_alternate_s
185 struct image_file_map
* fmap
;
186 dwarf2_section_t sections
[section_max
];
187 struct image_section_map sectmap
[section_max
];
188 dwarf2_parse_module_context_t module_ctx
;
189 } dwarf2_dwz_alternate_t
;
200 /* this is the context used for parsing a compilation unit
201 * inside an ELF/PE section (likely .debug_info)
203 typedef struct dwarf2_parse_context_s
205 dwarf2_parse_module_context_t
* module_ctx
;
208 struct symt_compiland
* compiland
;
209 struct sparse_array abbrev_table
;
210 struct sparse_array debug_info_table
;
211 ULONG_PTR ref_offset
;
213 dwarf2_cuhead_t head
;
214 enum unit_status status
;
215 dwarf2_traverse_context_t traverse_DIE
;
217 } dwarf2_parse_context_t
;
219 /* stored in the dbghelp's module internal structure for later reuse */
220 struct dwarf2_module_info_s
222 dwarf2_cuhead_t
** cuheads
;
223 unsigned num_cuheads
;
224 dwarf2_section_t debug_loc
;
225 dwarf2_section_t debug_frame
;
226 dwarf2_section_t eh_frame
;
227 unsigned char word_size
;
230 #define loc_dwarf2_location_list (loc_user + 0)
231 #define loc_dwarf2_block (loc_user + 1)
232 #define loc_dwarf2_frame_cfa (loc_user + 2)
234 /* forward declarations */
235 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_debug_info_t
* entry
);
236 static BOOL
dwarf2_parse_compilation_unit(dwarf2_parse_context_t
* ctx
);
237 static dwarf2_parse_context_t
* dwarf2_locate_cu(dwarf2_parse_module_context_t
* module_ctx
, ULONG_PTR ref
);
239 static unsigned char dwarf2_get_byte(const unsigned char* ptr
)
244 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t
* ctx
)
246 unsigned char uvalue
= dwarf2_get_byte(ctx
->data
);
251 static unsigned short dwarf2_get_u2(const unsigned char* ptr
)
253 return *(const UINT16
*)ptr
;
256 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t
* ctx
)
258 unsigned short uvalue
= dwarf2_get_u2(ctx
->data
);
263 static ULONG_PTR
dwarf2_get_u4(const unsigned char* ptr
)
265 return *(const UINT32
*)ptr
;
268 static ULONG_PTR
dwarf2_parse_u4(dwarf2_traverse_context_t
* ctx
)
270 ULONG_PTR uvalue
= dwarf2_get_u4(ctx
->data
);
275 static DWORD64
dwarf2_get_u8(const unsigned char* ptr
)
277 return *(const UINT64
*)ptr
;
280 static DWORD64
dwarf2_parse_u8(dwarf2_traverse_context_t
* ctx
)
282 DWORD64 uvalue
= dwarf2_get_u8(ctx
->data
);
287 static ULONG_PTR
dwarf2_get_leb128_as_unsigned(const unsigned char* ptr
, const unsigned char** end
)
295 byte
= dwarf2_get_byte(ptr
++);
296 ret
|= (byte
& 0x7f) << shift
;
298 } while (byte
& 0x80);
304 static ULONG_PTR
dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t
* ctx
)
310 ret
= dwarf2_get_leb128_as_unsigned(ctx
->data
, &ctx
->data
);
315 static LONG_PTR
dwarf2_get_leb128_as_signed(const unsigned char* ptr
, const unsigned char** end
)
320 const unsigned size
= sizeof(int) * 8;
324 byte
= dwarf2_get_byte(ptr
++);
325 ret
|= (byte
& 0x7f) << shift
;
327 } while (byte
& 0x80);
330 /* as spec: sign bit of byte is 2nd high order bit (80x40)
331 * -> 0x80 is used as flag.
333 if ((shift
< size
) && (byte
& 0x40))
335 ret
|= - (1 << shift
);
340 static LONG_PTR
dwarf2_leb128_as_signed(dwarf2_traverse_context_t
* ctx
)
346 ret
= dwarf2_get_leb128_as_signed(ctx
->data
, &ctx
->data
);
350 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t
* ctx
)
353 for (ret
= 0; ctx
->data
[ret
] & 0x80; ret
++);
357 /******************************************************************
360 * Returns an address.
361 * We assume that in all cases word size from Dwarf matches the size of
362 * addresses in platform where the exec is compiled.
364 static ULONG_PTR
dwarf2_get_addr(const unsigned char* ptr
, unsigned word_size
)
371 ret
= dwarf2_get_u4(ptr
);
374 ret
= dwarf2_get_u8(ptr
);
377 FIXME("Unsupported Word Size %u\n", word_size
);
383 static inline ULONG_PTR
dwarf2_parse_addr(dwarf2_traverse_context_t
* ctx
, unsigned word_size
)
385 ULONG_PTR ret
= dwarf2_get_addr(ctx
->data
, word_size
);
386 ctx
->data
+= word_size
;
390 static inline ULONG_PTR
dwarf2_parse_addr_head(dwarf2_traverse_context_t
* ctx
, const dwarf2_cuhead_t
* head
)
392 return dwarf2_parse_addr(ctx
, head
->word_size
);
395 static ULONG_PTR
dwarf2_parse_offset(dwarf2_traverse_context_t
* ctx
, unsigned char offset_size
)
397 ULONG_PTR ret
= dwarf2_get_addr(ctx
->data
, offset_size
);
398 ctx
->data
+= offset_size
;
402 static ULONG_PTR
dwarf2_parse_3264(dwarf2_traverse_context_t
* ctx
, unsigned char* ofsz
)
404 ULONG_PTR ret
= dwarf2_parse_u4(ctx
);
405 if (ret
== 0xffffffff)
407 ret
= dwarf2_parse_u8(ctx
);
414 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t
* ctx
)
416 return wine_dbg_sprintf("ctx(%p)", ctx
->data
);
419 static const char* dwarf2_debug_unit_ctx(const dwarf2_parse_context_t
* ctx
)
421 return wine_dbg_sprintf("ctx(%p,%s)",
422 ctx
, debugstr_w(ctx
->module_ctx
->module
->modulename
));
425 static const char* dwarf2_debug_di(const dwarf2_debug_info_t
* di
)
427 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p) in %s",
428 di
->abbrev
, di
->symt
, dwarf2_debug_unit_ctx(di
->unit_ctx
));
431 static dwarf2_abbrev_entry_t
*
432 dwarf2_abbrev_table_find_entry(const struct sparse_array
* abbrev_table
,
433 ULONG_PTR entry_code
)
435 assert( NULL
!= abbrev_table
);
436 return sparse_array_find(abbrev_table
, entry_code
);
439 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t
* abbrev_ctx
,
440 struct sparse_array
* abbrev_table
,
443 ULONG_PTR entry_code
;
444 dwarf2_abbrev_entry_t
* abbrev_entry
;
445 dwarf2_abbrev_entry_attr_t
* new = NULL
;
446 dwarf2_abbrev_entry_attr_t
* last
= NULL
;
450 assert( NULL
!= abbrev_ctx
);
452 TRACE("%s, end at %p\n",
453 dwarf2_debug_traverse_ctx(abbrev_ctx
), abbrev_ctx
->end_data
);
455 sparse_array_init(abbrev_table
, sizeof(dwarf2_abbrev_entry_t
), 32);
456 while (abbrev_ctx
->data
< abbrev_ctx
->end_data
)
458 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
459 entry_code
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
460 TRACE("found entry_code %Iu\n", entry_code
);
463 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
466 abbrev_entry
= sparse_array_add(abbrev_table
, entry_code
, pool
);
467 assert( NULL
!= abbrev_entry
);
469 abbrev_entry
->entry_code
= entry_code
;
470 abbrev_entry
->tag
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
471 abbrev_entry
->have_child
= dwarf2_parse_byte(abbrev_ctx
);
472 abbrev_entry
->attrs
= NULL
;
473 abbrev_entry
->num_attr
= 0;
475 TRACE("table:(%p,#%u) entry_code(%Iu) tag(0x%Ix) have_child(%u) -> %p\n",
476 abbrev_table
, sparse_array_length(abbrev_table
),
477 entry_code
, abbrev_entry
->tag
, abbrev_entry
->have_child
, abbrev_entry
);
482 attribute
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
483 form
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
484 if (!attribute
) break;
486 new = pool_alloc(pool
, sizeof(dwarf2_abbrev_entry_attr_t
));
489 new->attribute
= attribute
;
492 if (abbrev_entry
->attrs
) last
->next
= new;
493 else abbrev_entry
->attrs
= new;
495 abbrev_entry
->num_attr
++;
498 TRACE("found %u entries\n", sparse_array_length(abbrev_table
));
501 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t
* ctx
,
502 const dwarf2_cuhead_t
* head
,
503 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
)
507 TRACE("(attr:0x%Ix,form:0x%Ix)\n", abbrev_attr
->attribute
, abbrev_attr
->form
);
509 switch (abbrev_attr
->form
)
511 case DW_FORM_flag_present
: step
= 0; break;
512 case DW_FORM_ref_addr
: step
= (head
->version
>= 3) ? head
->offset_size
: head
->word_size
; break;
513 case DW_FORM_addr
: step
= head
->word_size
; break;
516 case DW_FORM_ref1
: step
= 1; break;
518 case DW_FORM_ref2
: step
= 2; break;
520 case DW_FORM_ref4
: step
= 4; break;
522 case DW_FORM_ref8
: step
= 8; break;
524 case DW_FORM_ref_udata
:
525 case DW_FORM_udata
: step
= dwarf2_leb128_length(ctx
); break;
526 case DW_FORM_string
: step
= strlen((const char*)ctx
->data
) + 1; break;
527 case DW_FORM_exprloc
:
528 case DW_FORM_block
: step
= dwarf2_leb128_as_unsigned(ctx
); break;
529 case DW_FORM_block1
: step
= dwarf2_parse_byte(ctx
); break;
530 case DW_FORM_block2
: step
= dwarf2_parse_u2(ctx
); break;
531 case DW_FORM_block4
: step
= dwarf2_parse_u4(ctx
); break;
532 case DW_FORM_sec_offset
:
533 case DW_FORM_GNU_ref_alt
:
534 case DW_FORM_GNU_strp_alt
:
535 case DW_FORM_strp
: step
= head
->offset_size
; break;
537 FIXME("Unhandled attribute form %Ix\n", abbrev_attr
->form
);
543 static BOOL
dwarf2_fill_attr(const dwarf2_parse_context_t
* ctx
,
544 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
,
545 const unsigned char* data
,
546 struct attribute
* attr
)
548 attr
->form
= abbrev_attr
->form
;
551 case DW_FORM_ref_addr
:
552 if (ctx
->head
.version
>= 3)
553 attr
->u
.uvalue
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
555 attr
->u
.uvalue
= dwarf2_get_addr(data
, ctx
->head
.word_size
);
556 TRACE("addr<0x%Ix>\n", attr
->u
.uvalue
);
560 attr
->u
.uvalue
= dwarf2_get_addr(data
, ctx
->head
.word_size
);
561 TRACE("addr<0x%Ix>\n", attr
->u
.uvalue
);
565 attr
->u
.uvalue
= dwarf2_get_byte(data
);
566 TRACE("flag<0x%Ix>\n", attr
->u
.uvalue
);
569 case DW_FORM_flag_present
:
571 TRACE("flag_present\n");
575 attr
->u
.uvalue
= dwarf2_get_byte(data
);
576 TRACE("data1<%Iu>\n", attr
->u
.uvalue
);
580 attr
->u
.uvalue
= dwarf2_get_u2(data
);
581 TRACE("data2<%Iu>\n", attr
->u
.uvalue
);
585 attr
->u
.uvalue
= dwarf2_get_u4(data
);
586 TRACE("data4<%Iu>\n", attr
->u
.uvalue
);
590 attr
->u
.lluvalue
= dwarf2_get_u8(data
);
591 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr
->u
.uvalue
));
595 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_byte(data
);
596 TRACE("ref1<0x%Ix>\n", attr
->u
.uvalue
);
600 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u2(data
);
601 TRACE("ref2<0x%Ix>\n", attr
->u
.uvalue
);
605 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u4(data
);
606 TRACE("ref4<0x%Ix>\n", attr
->u
.uvalue
);
610 FIXME("Unhandled 64-bit support\n");
614 attr
->u
.svalue
= dwarf2_get_leb128_as_signed(data
, NULL
);
617 case DW_FORM_ref_udata
:
618 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_leb128_as_unsigned(data
, NULL
);
619 TRACE("ref_udata<0x%Ix>\n", attr
->u
.uvalue
);
623 attr
->u
.uvalue
= dwarf2_get_leb128_as_unsigned(data
, NULL
);
624 TRACE("udata<0x%Ix>\n", attr
->u
.uvalue
);
628 attr
->u
.string
= (const char *)data
;
629 TRACE("string<%s>\n", debugstr_a(attr
->u
.string
));
634 ULONG_PTR ofs
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
635 if (ofs
>= ctx
->module_ctx
->sections
[section_string
].size
)
637 ERR("Out of bounds string offset (%08Ix)\n", ofs
);
638 attr
->u
.string
= "<<outofbounds-strp>>";
642 attr
->u
.string
= (const char*)ctx
->module_ctx
->sections
[section_string
].address
+ ofs
;
643 TRACE("strp<%s>\n", debugstr_a(attr
->u
.string
));
649 case DW_FORM_exprloc
:
650 attr
->u
.block
.size
= dwarf2_get_leb128_as_unsigned(data
, &attr
->u
.block
.ptr
);
651 TRACE("block<%p,%u>\n", attr
->u
.block
.ptr
, attr
->u
.block
.size
);
655 attr
->u
.block
.size
= dwarf2_get_byte(data
);
656 attr
->u
.block
.ptr
= data
+ 1;
657 TRACE("block<%p,%u>\n", attr
->u
.block
.ptr
, attr
->u
.block
.size
);
661 attr
->u
.block
.size
= dwarf2_get_u2(data
);
662 attr
->u
.block
.ptr
= data
+ 2;
663 TRACE("block<%p,%u>\n", attr
->u
.block
.ptr
, attr
->u
.block
.size
);
667 attr
->u
.block
.size
= dwarf2_get_u4(data
);
668 attr
->u
.block
.ptr
= data
+ 4;
669 TRACE("block<%p,%u>\n", attr
->u
.block
.ptr
, attr
->u
.block
.size
);
672 case DW_FORM_sec_offset
:
673 attr
->u
.lluvalue
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
674 TRACE("sec_offset<%s>\n", wine_dbgstr_longlong(attr
->u
.lluvalue
));
677 case DW_FORM_GNU_ref_alt
:
678 if (!ctx
->module_ctx
->dwz
)
680 ERR("No DWZ file present for GNU_ref_alt in %s\n", debugstr_w(ctx
->module_ctx
->module
->modulename
));
684 attr
->u
.uvalue
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
685 TRACE("ref_alt<0x%Ix>\n", attr
->u
.uvalue
);
688 case DW_FORM_GNU_strp_alt
:
689 if (ctx
->module_ctx
->dwz
)
691 ULONG_PTR ofs
= dwarf2_get_addr(data
, ctx
->head
.offset_size
);
692 if (ofs
< ctx
->module_ctx
->dwz
->sections
[section_string
].size
)
694 attr
->u
.string
= (const char*)ctx
->module_ctx
->dwz
->sections
[section_string
].address
+ ofs
;
695 TRACE("strp_alt<%s>\n", debugstr_a(attr
->u
.string
));
699 ERR("out of bounds strp_alt: 0x%Ix 0x%x (%u)\n", ofs
, ctx
->module_ctx
->dwz
->sections
[section_string
].size
, ctx
->head
.offset_size
);
700 attr
->u
.string
= "<<outofbounds-strpalt>>";
705 ERR("No DWZ file present for GNU_strp_alt in %s\n", debugstr_w(ctx
->module_ctx
->module
->modulename
));
706 attr
->u
.string
= "<<noDWZ-strpalt>>";
711 FIXME("Unhandled attribute form %Ix\n", abbrev_attr
->form
);
717 static dwarf2_debug_info_t
* dwarf2_jump_to_debug_info(struct attribute
* attr
);
719 static BOOL
dwarf2_find_attribute(const dwarf2_debug_info_t
* di
,
720 unsigned at
, struct attribute
* attr
)
722 unsigned i
, refidx
= 0;
723 dwarf2_abbrev_entry_attr_t
* abbrev_attr
;
724 dwarf2_abbrev_entry_attr_t
* ref_abbrev_attr
= NULL
;
726 attr
->gotten_from
= attr_direct
;
729 ref_abbrev_attr
= NULL
;
730 attr
->debug_info
= di
;
731 for (i
= 0, abbrev_attr
= di
->abbrev
->attrs
; abbrev_attr
; i
++, abbrev_attr
= abbrev_attr
->next
)
733 if (abbrev_attr
->attribute
== at
)
735 return dwarf2_fill_attr(di
->unit_ctx
, abbrev_attr
, di
->data
[i
], attr
);
737 if ((abbrev_attr
->attribute
== DW_AT_abstract_origin
||
738 abbrev_attr
->attribute
== DW_AT_specification
) &&
742 FIXME("two references %Ix and %Ix\n", ref_abbrev_attr
->attribute
, abbrev_attr
->attribute
);
743 ref_abbrev_attr
= abbrev_attr
;
745 attr
->gotten_from
= (abbrev_attr
->attribute
== DW_AT_abstract_origin
) ?
746 attr_abstract_origin
: attr_specification
;
749 /* do we have either an abstract origin or a specification debug entry to look into ? */
750 if (!ref_abbrev_attr
|| !dwarf2_fill_attr(di
->unit_ctx
, ref_abbrev_attr
, di
->data
[refidx
], attr
))
752 if (!(di
= dwarf2_jump_to_debug_info(attr
)))
754 FIXME("Should have found the debug info entry\n");
761 static dwarf2_debug_info_t
* dwarf2_jump_to_debug_info(struct attribute
* attr
)
763 dwarf2_parse_context_t
* ref_ctx
= NULL
;
764 BOOL with_other
= TRUE
;
765 dwarf2_debug_info_t
* ret
;
769 case DW_FORM_ref_addr
:
770 ref_ctx
= dwarf2_locate_cu(attr
->debug_info
->unit_ctx
->module_ctx
, attr
->u
.uvalue
);
772 case DW_FORM_GNU_ref_alt
:
773 if (attr
->debug_info
->unit_ctx
->module_ctx
->dwz
)
774 ref_ctx
= dwarf2_locate_cu(&attr
->debug_info
->unit_ctx
->module_ctx
->dwz
->module_ctx
, attr
->u
.uvalue
);
778 ref_ctx
= attr
->debug_info
->unit_ctx
;
781 if (!ref_ctx
) return FALSE
;
782 /* There are cases where we end up with a circular reference between two (or more)
783 * compilation units. Before this happens, try to see if we can refer to an already
784 * loaded debug_info in the target compilation unit (even if all the debug_info
785 * haven't been loaded yet).
787 if (ref_ctx
->status
== UNIT_BEINGLOADED
&&
788 (ret
= sparse_array_find(&ref_ctx
->debug_info_table
, attr
->u
.uvalue
)))
792 /* ensure CU is fully loaded */
793 if (ref_ctx
!= attr
->debug_info
->unit_ctx
&& !dwarf2_parse_compilation_unit(ref_ctx
))
796 return sparse_array_find(&ref_ctx
->debug_info_table
, attr
->u
.uvalue
);
799 static void dwarf2_load_one_entry(dwarf2_debug_info_t
*);
801 #define Wine_DW_no_register 0x7FFFFFFF
803 static unsigned dwarf2_map_register(int regno
, const struct module
* module
)
805 if (regno
== Wine_DW_no_register
)
807 FIXME("What the heck map reg 0x%x\n",regno
);
810 return module
->cpu
->map_dwarf_register(regno
, module
, FALSE
);
813 static enum location_error
814 compute_location(const struct module
*module
, const dwarf2_cuhead_t
* head
,
815 dwarf2_traverse_context_t
* ctx
, struct location
* loc
,
816 HANDLE hproc
, const struct location
* frame
)
818 DWORD_PTR tmp
, stack
[64];
821 BOOL piece_found
= FALSE
;
825 loc
->kind
= loc_absolute
;
826 loc
->reg
= Wine_DW_no_register
;
828 while (ctx
->data
< ctx
->end_data
)
830 op
= dwarf2_parse_byte(ctx
);
832 if (op
>= DW_OP_lit0
&& op
<= DW_OP_lit31
)
833 stack
[++stk
] = op
- DW_OP_lit0
;
834 else if (op
>= DW_OP_reg0
&& op
<= DW_OP_reg31
)
836 /* dbghelp APIs don't know how to cope with this anyway
837 * (for example 'long long' stored in two registers)
838 * FIXME: We should tell winedbg how to deal with it (sigh)
842 DWORD cvreg
= dwarf2_map_register(op
- DW_OP_reg0
, module
);
843 if (loc
->reg
!= Wine_DW_no_register
)
844 FIXME("Only supporting one reg (%s/%d -> %s/%ld)\n",
845 module
->cpu
->fetch_regname(loc
->reg
), loc
->reg
,
846 module
->cpu
->fetch_regname(cvreg
), cvreg
);
849 loc
->kind
= loc_register
;
851 else if (op
>= DW_OP_breg0
&& op
<= DW_OP_breg31
)
853 /* dbghelp APIs don't know how to cope with this anyway
854 * (for example 'long long' stored in two registers)
855 * FIXME: We should tell winedbg how to deal with it (sigh)
859 DWORD cvreg
= dwarf2_map_register(op
- DW_OP_breg0
, module
);
860 if (loc
->reg
!= Wine_DW_no_register
)
861 FIXME("Only supporting one breg (%s/%d -> %s/%ld)\n",
862 module
->cpu
->fetch_regname(loc
->reg
), loc
->reg
,
863 module
->cpu
->fetch_regname(cvreg
), cvreg
);
866 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
867 loc
->kind
= loc_regrel
;
871 case DW_OP_nop
: break;
872 case DW_OP_addr
: stack
[++stk
] = dwarf2_parse_addr_head(ctx
, head
); break;
873 case DW_OP_const1u
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
874 case DW_OP_const1s
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
875 case DW_OP_const2u
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
876 case DW_OP_const2s
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
877 case DW_OP_const4u
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
878 case DW_OP_const4s
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
879 case DW_OP_const8u
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
880 case DW_OP_const8s
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
881 case DW_OP_constu
: stack
[++stk
] = dwarf2_leb128_as_unsigned(ctx
); break;
882 case DW_OP_consts
: stack
[++stk
] = dwarf2_leb128_as_signed(ctx
); break;
883 case DW_OP_dup
: stack
[stk
+ 1] = stack
[stk
]; stk
++; break;
884 case DW_OP_drop
: stk
--; break;
885 case DW_OP_over
: stack
[stk
+ 1] = stack
[stk
- 1]; stk
++; break;
886 case DW_OP_pick
: stack
[stk
+ 1] = stack
[stk
- dwarf2_parse_byte(ctx
)]; stk
++; break;
887 case DW_OP_swap
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = tmp
; break;
888 case DW_OP_rot
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = stack
[stk
-2]; stack
[stk
-2] = tmp
; break;
889 case DW_OP_abs
: stack
[stk
] = sizeof(stack
[stk
]) == 8 ? llabs((INT64
)stack
[stk
]) : abs((INT32
)stack
[stk
]); break;
890 case DW_OP_neg
: stack
[stk
] = -stack
[stk
]; break;
891 case DW_OP_not
: stack
[stk
] = ~stack
[stk
]; break;
892 case DW_OP_and
: stack
[stk
-1] &= stack
[stk
]; stk
--; break;
893 case DW_OP_or
: stack
[stk
-1] |= stack
[stk
]; stk
--; break;
894 case DW_OP_minus
: stack
[stk
-1] -= stack
[stk
]; stk
--; break;
895 case DW_OP_mul
: stack
[stk
-1] *= stack
[stk
]; stk
--; break;
896 case DW_OP_plus
: stack
[stk
-1] += stack
[stk
]; stk
--; break;
897 case DW_OP_xor
: stack
[stk
-1] ^= stack
[stk
]; stk
--; break;
898 case DW_OP_shl
: stack
[stk
-1] <<= stack
[stk
]; stk
--; break;
899 case DW_OP_shr
: stack
[stk
-1] >>= stack
[stk
]; stk
--; break;
900 case DW_OP_plus_uconst
: stack
[stk
] += dwarf2_leb128_as_unsigned(ctx
); break;
901 case DW_OP_shra
: stack
[stk
-1] = stack
[stk
-1] / (1 << stack
[stk
]); stk
--; break;
902 case DW_OP_div
: stack
[stk
-1] = stack
[stk
-1] / stack
[stk
]; stk
--; break;
903 case DW_OP_mod
: stack
[stk
-1] = stack
[stk
-1] % stack
[stk
]; stk
--; break;
904 case DW_OP_ge
: stack
[stk
-1] = (stack
[stk
-1] >= stack
[stk
]); stk
--; break;
905 case DW_OP_gt
: stack
[stk
-1] = (stack
[stk
-1] > stack
[stk
]); stk
--; break;
906 case DW_OP_le
: stack
[stk
-1] = (stack
[stk
-1] <= stack
[stk
]); stk
--; break;
907 case DW_OP_lt
: stack
[stk
-1] = (stack
[stk
-1] < stack
[stk
]); stk
--; break;
908 case DW_OP_eq
: stack
[stk
-1] = (stack
[stk
-1] == stack
[stk
]); stk
--; break;
909 case DW_OP_ne
: stack
[stk
-1] = (stack
[stk
-1] != stack
[stk
]); stk
--; break;
910 case DW_OP_skip
: tmp
= dwarf2_parse_u2(ctx
); ctx
->data
+= tmp
; break;
912 tmp
= dwarf2_parse_u2(ctx
);
913 if (!stack
[stk
--]) ctx
->data
+= tmp
;
916 tmp
= dwarf2_leb128_as_unsigned(ctx
);
919 if (loc
->reg
!= Wine_DW_no_register
)
920 FIXME("Only supporting one reg\n");
921 loc
->reg
= dwarf2_map_register(tmp
, module
);
923 loc
->kind
= loc_register
;
926 tmp
= dwarf2_leb128_as_unsigned(ctx
);
927 if (loc
->reg
!= Wine_DW_no_register
)
928 FIXME("Only supporting one regx\n");
929 loc
->reg
= dwarf2_map_register(tmp
, module
);
930 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
931 loc
->kind
= loc_regrel
;
934 if (loc
->reg
!= Wine_DW_no_register
)
935 FIXME("Only supporting one reg (%s/%d -> -2)\n",
936 module
->cpu
->fetch_regname(loc
->reg
), loc
->reg
);
937 if (frame
&& frame
->kind
== loc_register
)
939 loc
->kind
= loc_regrel
;
940 loc
->reg
= frame
->reg
;
941 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
943 else if (frame
&& frame
->kind
== loc_regrel
)
945 loc
->kind
= loc_regrel
;
946 loc
->reg
= frame
->reg
;
947 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
) + frame
->offset
;
951 /* FIXME: this could be later optimized by not recomputing
952 * this very location expression
954 loc
->kind
= loc_dwarf2_block
;
955 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
960 unsigned sz
= dwarf2_leb128_as_unsigned(ctx
);
961 WARN("Not handling OP_piece (size=%d)\n", sz
);
968 FIXME("Unexpected empty stack\n");
969 return loc_err_internal
;
971 if (loc
->reg
!= Wine_DW_no_register
)
973 WARN("Too complex expression for deref\n");
974 return loc_err_too_complex
;
978 DWORD_PTR addr
= stack
[stk
--];
981 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, head
->word_size
, NULL
))
983 WARN("Couldn't read memory at %Ix\n", addr
);
984 return loc_err_cant_read
;
986 stack
[++stk
] = deref
;
990 loc
->kind
= loc_dwarf2_block
;
993 case DW_OP_deref_size
:
996 FIXME("Unexpected empty stack\n");
997 return loc_err_internal
;
999 if (loc
->reg
!= Wine_DW_no_register
)
1001 WARN("Too complex expression for deref\n");
1002 return loc_err_too_complex
;
1006 DWORD_PTR addr
= stack
[stk
--];
1007 BYTE derefsize
= dwarf2_parse_byte(ctx
);
1010 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, derefsize
, NULL
))
1012 WARN("Couldn't read memory at %Ix\n", addr
);
1013 return loc_err_cant_read
;
1018 case 1: stack
[++stk
] = *(unsigned char*)&deref
; break;
1019 case 2: stack
[++stk
] = *(unsigned short*)&deref
; break;
1020 case 4: stack
[++stk
] = *(DWORD
*)&deref
; break;
1021 case 8: if (head
->word_size
>= derefsize
) stack
[++stk
] = deref
; break;
1026 dwarf2_parse_byte(ctx
);
1027 loc
->kind
= loc_dwarf2_block
;
1030 case DW_OP_stack_value
:
1031 /* Expected behaviour is that this is the last instruction of this
1032 * expression and just the "top of stack" value should be put to loc->offset. */
1035 if (op
< DW_OP_lo_user
) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
1036 FIXME("Unhandled attr op: %x\n", op
);
1037 /* FIXME else unhandled extension */
1038 return loc_err_internal
;
1041 loc
->offset
= stack
[stk
];
1045 static BOOL
dwarf2_compute_location_attr(dwarf2_parse_context_t
* ctx
,
1046 const dwarf2_debug_info_t
* di
,
1048 struct location
* loc
,
1049 const struct location
* frame
)
1051 struct attribute xloc
;
1053 if (!dwarf2_find_attribute(di
, dw
, &xloc
)) return FALSE
;
1058 if (ctx
->head
.version
< 4)
1060 loc
->kind
= loc_dwarf2_location_list
;
1061 loc
->reg
= Wine_DW_no_register
;
1062 loc
->offset
= xloc
.u
.uvalue
;
1066 case DW_FORM_data1
: case DW_FORM_data2
:
1067 case DW_FORM_udata
: case DW_FORM_sdata
:
1068 loc
->kind
= loc_absolute
;
1070 loc
->offset
= xloc
.u
.uvalue
;
1073 if (ctx
->head
.version
>= 4)
1075 loc
->kind
= loc_absolute
;
1077 loc
->offset
= xloc
.u
.lluvalue
;
1081 case DW_FORM_sec_offset
:
1082 loc
->kind
= loc_dwarf2_location_list
;
1083 loc
->reg
= Wine_DW_no_register
;
1084 loc
->offset
= xloc
.u
.lluvalue
;
1087 case DW_FORM_block1
:
1088 case DW_FORM_block2
:
1089 case DW_FORM_block4
:
1090 case DW_FORM_exprloc
:
1092 default: FIXME("Unsupported yet form %Ix\n", xloc
.form
);
1096 /* assume we have a block form */
1097 if (dw
== DW_AT_frame_base
&& xloc
.u
.block
.size
== 1 && *xloc
.u
.block
.ptr
== DW_OP_call_frame_cfa
)
1099 loc
->kind
= loc_dwarf2_frame_cfa
;
1100 loc
->reg
= Wine_DW_no_register
;
1103 else if (xloc
.u
.block
.size
)
1105 dwarf2_traverse_context_t lctx
;
1106 enum location_error err
;
1108 lctx
.data
= xloc
.u
.block
.ptr
;
1109 lctx
.end_data
= xloc
.u
.block
.ptr
+ xloc
.u
.block
.size
;
1111 err
= compute_location(ctx
->module_ctx
->module
, &ctx
->head
, &lctx
, loc
, NULL
, frame
);
1114 loc
->kind
= loc_error
;
1117 else if (loc
->kind
== loc_dwarf2_block
)
1119 unsigned* ptr
= pool_alloc(&ctx
->module_ctx
->module
->pool
,
1120 sizeof(unsigned) + xloc
.u
.block
.size
);
1121 *ptr
= xloc
.u
.block
.size
;
1122 memcpy(ptr
+ 1, xloc
.u
.block
.ptr
, xloc
.u
.block
.size
);
1123 loc
->offset
= (ULONG_PTR
)ptr
;
1129 static struct symt
* dwarf2_lookup_type(const dwarf2_debug_info_t
* di
)
1131 struct attribute attr
;
1132 dwarf2_debug_info_t
* type
;
1134 if (!dwarf2_find_attribute(di
, DW_AT_type
, &attr
))
1135 /* this is only valid if current language of CU is C or C++ */
1136 return &symt_get_basic(btVoid
, 0)->symt
;
1137 if (!(type
= dwarf2_jump_to_debug_info(&attr
)))
1138 return &symt_get_basic(btNoType
, 0)->symt
;
1142 FIXME("Reference to itself\n");
1143 return &symt_get_basic(btNoType
, 0)->symt
;
1147 /* load the debug info entity */
1148 dwarf2_load_one_entry(type
);
1151 FIXME("Unable to load forward reference for tag %Ix\n", type
->abbrev
->tag
);
1152 return &symt_get_basic(btNoType
, 0)->symt
;
1158 static const char* dwarf2_get_cpp_name(dwarf2_debug_info_t
* di
, const char* name
)
1161 struct attribute diname
;
1162 struct attribute spec
;
1164 if (di
->abbrev
->tag
== DW_TAG_compile_unit
|| di
->abbrev
->tag
== DW_TAG_partial_unit
) return name
;
1166 /* if the di is a definition, but has also a (previous) declaration, then scope must
1167 * be gotten from declaration not definition
1169 if (dwarf2_find_attribute(di
, DW_AT_specification
, &spec
) && spec
.gotten_from
== attr_direct
)
1171 di
= dwarf2_jump_to_debug_info(&spec
);
1174 FIXME("Should have found the debug info entry\n");
1179 if (!di
->unit_ctx
->cpp_name
)
1180 di
->unit_ctx
->cpp_name
= pool_alloc(&di
->unit_ctx
->pool
, MAX_SYM_NAME
);
1181 last
= di
->unit_ctx
->cpp_name
+ MAX_SYM_NAME
- strlen(name
) - 1;
1184 for (di
= di
->parent
; di
; di
= di
->parent
)
1186 switch (di
->abbrev
->tag
)
1188 case DW_TAG_namespace
:
1189 case DW_TAG_structure_type
:
1190 case DW_TAG_class_type
:
1191 case DW_TAG_interface_type
:
1192 case DW_TAG_union_type
:
1193 if (dwarf2_find_attribute(di
, DW_AT_name
, &diname
))
1195 size_t len
= strlen(diname
.u
.string
);
1197 if (last
< di
->unit_ctx
->cpp_name
) return NULL
;
1198 memcpy(last
, diname
.u
.string
, len
);
1199 last
[len
] = last
[len
+ 1] = ':';
1209 /******************************************************************
1212 * read a range for a given debug_info (either using AT_range attribute, in which
1213 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1214 * in all cases, range is relative to beginning of compilation unit
1216 static BOOL
dwarf2_read_range(dwarf2_parse_context_t
* ctx
, const dwarf2_debug_info_t
* di
,
1217 ULONG_PTR
* plow
, ULONG_PTR
* phigh
)
1219 struct attribute range
;
1221 if (dwarf2_find_attribute(di
, DW_AT_ranges
, &range
))
1223 dwarf2_traverse_context_t traverse
;
1224 ULONG_PTR low
, high
;
1225 const ULONG_PTR UMAX
= ~(ULONG_PTR
)0u;
1227 traverse
.data
= ctx
->module_ctx
->sections
[section_ranges
].address
+ range
.u
.uvalue
;
1228 traverse
.end_data
= ctx
->module_ctx
->sections
[section_ranges
].address
+
1229 ctx
->module_ctx
->sections
[section_ranges
].size
;
1233 while (traverse
.data
+ 2 * ctx
->head
.word_size
< traverse
.end_data
)
1235 low
= dwarf2_parse_addr_head(&traverse
, &ctx
->head
);
1236 high
= dwarf2_parse_addr_head(&traverse
, &ctx
->head
);
1237 if (low
== 0 && high
== 0) break;
1238 if (low
== (ctx
->head
.word_size
== 8 ? (~(DWORD64
)0u) : (DWORD64
)(~0u)))
1239 FIXME("unsupported yet (base address selection)\n");
1240 /* range values are relative to start of compilation unit */
1241 low
+= ctx
->compiland
->address
- ctx
->module_ctx
->load_offset
;
1242 high
+= ctx
->compiland
->address
- ctx
->module_ctx
->load_offset
;
1243 if (low
< *plow
) *plow
= low
;
1244 if (high
> *phigh
) *phigh
= high
;
1246 if (*plow
== UMAX
|| *phigh
== 0) {WARN("no entry found\n"); return FALSE
;}
1247 if (*plow
== *phigh
) {WARN("entry found, but low=high %Ix %Ix\n", low
, high
); return FALSE
;}
1253 struct attribute low_pc
;
1254 struct attribute high_pc
;
1256 if (!dwarf2_find_attribute(di
, DW_AT_low_pc
, &low_pc
) ||
1257 !dwarf2_find_attribute(di
, DW_AT_high_pc
, &high_pc
))
1259 *plow
= low_pc
.u
.uvalue
;
1260 *phigh
= high_pc
.u
.uvalue
;
1261 if (ctx
->head
.version
>= 4)
1262 switch (high_pc
.form
)
1272 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1276 FIXME("Unsupported class for high_pc\n");
1283 static struct addr_range
* dwarf2_get_ranges(const dwarf2_debug_info_t
* di
, unsigned* num_ranges
)
1285 struct attribute range
;
1286 struct addr_range
* ranges
;
1287 struct addr_range
* new_ranges
;
1289 if (dwarf2_find_attribute(di
, DW_AT_ranges
, &range
))
1291 dwarf2_traverse_context_t traverse
;
1292 unsigned alloc
= 16;
1293 ranges
= malloc(sizeof(struct addr_range
) * alloc
);
1294 if (!ranges
) return NULL
;
1297 traverse
.data
= di
->unit_ctx
->module_ctx
->sections
[section_ranges
].address
+ range
.u
.uvalue
;
1298 traverse
.end_data
= di
->unit_ctx
->module_ctx
->sections
[section_ranges
].address
+
1299 di
->unit_ctx
->module_ctx
->sections
[section_ranges
].size
;
1301 while (traverse
.data
+ 2 * di
->unit_ctx
->head
.word_size
< traverse
.end_data
)
1303 ULONG_PTR low
= dwarf2_parse_addr_head(&traverse
, &di
->unit_ctx
->head
);
1304 ULONG_PTR high
= dwarf2_parse_addr_head(&traverse
, &di
->unit_ctx
->head
);
1305 if (low
== 0 && high
== 0) break;
1306 if (low
== (di
->unit_ctx
->head
.word_size
== 8 ? (~(DWORD64
)0u) : (DWORD64
)(~0u)))
1307 FIXME("unsupported yet (base address selection)\n");
1308 if (*num_ranges
>= alloc
)
1311 new_ranges
= realloc(ranges
, sizeof(struct addr_range
) * alloc
);
1317 ranges
= new_ranges
;
1319 ranges
[*num_ranges
].low
= di
->unit_ctx
->compiland
->address
+ low
;
1320 ranges
[*num_ranges
].high
= di
->unit_ctx
->compiland
->address
+ high
;
1326 struct attribute low_pc
;
1327 struct attribute high_pc
;
1329 if (!dwarf2_find_attribute(di
, DW_AT_low_pc
, &low_pc
) ||
1330 !dwarf2_find_attribute(di
, DW_AT_high_pc
, &high_pc
))
1332 if (di
->unit_ctx
->head
.version
>= 4)
1333 switch (high_pc
.form
)
1343 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1344 high_pc
.u
.uvalue
+= low_pc
.u
.uvalue
;
1347 FIXME("Unsupported class for high_pc\n");
1350 ranges
= malloc(sizeof(struct addr_range
));
1351 if (!ranges
) return NULL
;
1352 ranges
[0].low
= di
->unit_ctx
->module_ctx
->load_offset
+ low_pc
.u
.uvalue
;
1353 ranges
[0].high
= di
->unit_ctx
->module_ctx
->load_offset
+ high_pc
.u
.uvalue
;
1359 /******************************************************************
1360 * dwarf2_read_one_debug_info
1362 * Loads into memory one debug info entry, and recursively its children (if any)
1364 static BOOL
dwarf2_read_one_debug_info(dwarf2_parse_context_t
* ctx
,
1365 dwarf2_traverse_context_t
* traverse
,
1366 dwarf2_debug_info_t
* parent_di
,
1367 dwarf2_debug_info_t
** pdi
)
1369 const dwarf2_abbrev_entry_t
*abbrev
;
1370 ULONG_PTR entry_code
;
1372 dwarf2_debug_info_t
* di
;
1373 dwarf2_debug_info_t
* child
;
1374 dwarf2_debug_info_t
** where
;
1375 dwarf2_abbrev_entry_attr_t
* attr
;
1377 struct attribute sibling
;
1379 offset
= traverse
->data
- ctx
->module_ctx
->sections
[ctx
->section
].address
;
1380 entry_code
= dwarf2_leb128_as_unsigned(traverse
);
1381 TRACE("found entry_code %Iu at 0x%Ix\n", entry_code
, offset
);
1387 abbrev
= dwarf2_abbrev_table_find_entry(&ctx
->abbrev_table
, entry_code
);
1390 WARN("Cannot find abbrev entry for %Iu at 0x%Ix\n", entry_code
, offset
);
1393 di
= sparse_array_add(&ctx
->debug_info_table
, offset
, &ctx
->pool
);
1394 if (!di
) return FALSE
;
1395 di
->abbrev
= abbrev
;
1397 di
->parent
= parent_di
;
1400 if (abbrev
->num_attr
)
1402 di
->data
= pool_alloc(&ctx
->pool
, abbrev
->num_attr
* sizeof(const char*));
1403 for (i
= 0, attr
= abbrev
->attrs
; attr
; i
++, attr
= attr
->next
)
1405 di
->data
[i
] = traverse
->data
;
1406 dwarf2_swallow_attribute(traverse
, &ctx
->head
, attr
);
1409 else di
->data
= NULL
;
1410 if (abbrev
->have_child
)
1412 vector_init(&di
->children
, sizeof(dwarf2_debug_info_t
*), 16);
1413 while (traverse
->data
< traverse
->end_data
)
1415 if (!dwarf2_read_one_debug_info(ctx
, traverse
, di
, &child
)) return FALSE
;
1417 where
= vector_add(&di
->children
, &ctx
->pool
);
1418 if (!where
) return FALSE
;
1422 if (dwarf2_find_attribute(di
, DW_AT_sibling
, &sibling
) &&
1423 traverse
->data
!= ctx
->module_ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
)
1425 if (sibling
.u
.uvalue
>= ctx
->module_ctx
->sections
[ctx
->section
].size
)
1427 FIXME("cursor sibling after section end %s: 0x%Ix 0x%x\n",
1428 dwarf2_debug_unit_ctx(ctx
), sibling
.u
.uvalue
, ctx
->module_ctx
->sections
[ctx
->section
].size
);
1431 WARN("setting cursor for %s to next sibling <0x%Ix>\n",
1432 dwarf2_debug_traverse_ctx(traverse
), sibling
.u
.uvalue
);
1433 traverse
->data
= ctx
->module_ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
;
1439 static struct vector
* dwarf2_get_di_children(dwarf2_debug_info_t
* di
)
1441 struct attribute spec
;
1445 if (di
->abbrev
->have_child
)
1446 return &di
->children
;
1447 if (!dwarf2_find_attribute(di
, DW_AT_specification
, &spec
)) break;
1448 if (!(di
= dwarf2_jump_to_debug_info(&spec
)))
1449 FIXME("Should have found the debug info entry\n");
1454 /* reconstruct whether integer is long (contains 'long' only once) */
1455 static BOOL
is_long(const char* name
)
1457 /* we assume name is made only of basic C keywords:
1458 * int long short unsigned signed void float double char _Bool _Complex
1460 const char* p
= strstr(name
, "long");
1461 return p
&& strstr(p
+ 4, "long") == NULL
;
1464 static BOOL
is_c_language(dwarf2_parse_context_t
* unit_ctx
)
1466 return unit_ctx
->language
== DW_LANG_C
||
1467 unit_ctx
->language
== DW_LANG_C89
||
1468 unit_ctx
->language
== DW_LANG_C99
;
1471 static BOOL
is_cpp_language(dwarf2_parse_context_t
* unit_ctx
)
1473 return unit_ctx
->language
== DW_LANG_C_plus_plus
;
1476 static struct symt
* dwarf2_parse_base_type(dwarf2_debug_info_t
* di
)
1478 struct attribute name
;
1479 struct attribute size
;
1480 struct attribute encoding
;
1482 BOOL c_language
, cpp_language
;
1484 if (di
->symt
) return di
->symt
;
1486 TRACE("%s\n", dwarf2_debug_di(di
));
1488 c_language
= is_c_language(di
->unit_ctx
);
1489 cpp_language
= is_cpp_language(di
->unit_ctx
);
1491 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
1492 name
.u
.string
= NULL
;
1493 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1494 if (!dwarf2_find_attribute(di
, DW_AT_encoding
, &encoding
)) encoding
.u
.uvalue
= DW_ATE_void
;
1496 switch (encoding
.u
.uvalue
)
1498 case DW_ATE_void
: bt
= btVoid
; break;
1499 case DW_ATE_address
: bt
= btULong
; break;
1500 case DW_ATE_boolean
: bt
= btBool
; break;
1501 case DW_ATE_complex_float
: bt
= btComplex
; break;
1502 case DW_ATE_float
: bt
= btFloat
; break;
1503 case DW_ATE_signed
: bt
= ((c_language
|| cpp_language
) && is_long(name
.u
.string
)) ? btLong
: btInt
; break;
1504 case DW_ATE_unsigned
:
1505 if ((c_language
|| cpp_language
) && is_long(name
.u
.string
)) bt
= btULong
;
1506 else if (cpp_language
&& !strcmp(name
.u
.string
, "wchar_t")) bt
= btWChar
;
1507 else if (cpp_language
&& !strcmp(name
.u
.string
, "char8_t")) bt
= btChar8
;
1508 else if (cpp_language
&& !strcmp(name
.u
.string
, "char16_t")) bt
= btChar16
;
1509 else if (cpp_language
&& !strcmp(name
.u
.string
, "char32_t")) bt
= btChar32
;
1512 /* on Windows, in C, char == signed char, but not in C++ */
1513 case DW_ATE_signed_char
: bt
= (cpp_language
&& !strcmp(name
.u
.string
, "signed char")) ? btInt
: btChar
; break;
1514 case DW_ATE_unsigned_char
: bt
= btUInt
; break;
1515 case DW_ATE_UTF
: bt
= (size
.u
.uvalue
== 1) ? btChar8
: (size
.u
.uvalue
== 2 ? btChar16
: btChar32
); break;
1516 default: bt
= btNoType
; break;
1518 di
->symt
= &symt_get_basic(bt
, size
.u
.uvalue
)->symt
;
1520 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1524 static struct symt
* dwarf2_parse_typedef(dwarf2_debug_info_t
* di
)
1526 struct symt
* ref_type
;
1527 struct attribute name
;
1529 if (di
->symt
) return di
->symt
;
1531 TRACE("%s\n", dwarf2_debug_di(di
));
1533 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1534 ref_type
= dwarf2_lookup_type(di
);
1538 /* Note: The MS C compiler has tweaks for WCHAR support.
1539 * Even if WCHAR is a typedef to wchar_t, wchar_t is emitted as btUInt/2 (it's defined as
1540 * unsigned short, so far so good), while WCHAR is emitted as btWChar/2).
1542 if ((is_c_language(di
->unit_ctx
) || is_cpp_language(di
->unit_ctx
)) && !strcmp(name
.u
.string
, "WCHAR"))
1543 ref_type
= &symt_get_basic(btWChar
, 2)->symt
;
1544 di
->symt
= &symt_new_typedef(di
->unit_ctx
->module_ctx
->module
, ref_type
, name
.u
.string
)->symt
;
1546 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1550 static struct symt
* dwarf2_parse_pointer_type(dwarf2_debug_info_t
* di
)
1552 struct symt
* ref_type
;
1553 struct attribute size
;
1555 if (di
->symt
) return di
->symt
;
1557 TRACE("%s\n", dwarf2_debug_di(di
));
1559 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= di
->unit_ctx
->module_ctx
->module
->cpu
->word_size
;
1560 ref_type
= dwarf2_lookup_type(di
);
1561 di
->symt
= &symt_new_pointer(di
->unit_ctx
->module_ctx
->module
, ref_type
, size
.u
.uvalue
)->symt
;
1562 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1566 static struct symt
* dwarf2_parse_subrange_type(dwarf2_debug_info_t
* di
)
1568 struct symt
* ref_type
;
1569 struct attribute name
;
1570 struct attribute dummy
;
1572 if (di
->symt
) return di
->symt
;
1574 TRACE("%s\n", dwarf2_debug_di(di
));
1576 if (dwarf2_find_attribute(di
, DW_AT_name
, &name
)) FIXME("Found name for subrange %s\n", name
.u
.string
);
1577 if (dwarf2_find_attribute(di
, DW_AT_byte_size
, &dummy
)) FIXME("Found byte_size %Iu\n", dummy
.u
.uvalue
);
1578 if (dwarf2_find_attribute(di
, DW_AT_bit_size
, &dummy
)) FIXME("Found bit_size %Iu\n", dummy
.u
.uvalue
);
1579 /* for now, we don't support the byte_size nor bit_size about the subrange, and pretend the two
1580 * types are the same (FIXME)
1582 ref_type
= dwarf2_lookup_type(di
);
1583 di
->symt
= ref_type
;
1584 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1588 static struct symt
* dwarf2_parse_array_type(dwarf2_debug_info_t
* di
)
1590 struct symt
* ref_type
;
1591 struct symt
* idx_type
= NULL
;
1592 struct symt
* symt
= NULL
;
1593 struct attribute min
, max
, cnt
;
1594 dwarf2_debug_info_t
* child
;
1596 const struct vector
* children
;
1598 if (di
->symt
) return di
->symt
;
1600 TRACE("%s\n", dwarf2_debug_di(di
));
1602 ref_type
= dwarf2_lookup_type(di
);
1604 if (!(children
= dwarf2_get_di_children(di
)))
1606 /* fake an array with unknown size */
1607 /* FIXME: int4 even on 64bit machines??? */
1608 idx_type
= &symt_get_basic(btInt
, 4)->symt
;
1612 else for (i
= 0; i
< vector_length(children
); i
++)
1614 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1615 if (child
->symt
== &symt_get_basic(btNoType
, 0)->symt
) continue;
1616 switch (child
->abbrev
->tag
)
1618 case DW_TAG_subrange_type
:
1619 idx_type
= dwarf2_lookup_type(child
);
1620 if (!dwarf2_find_attribute(child
, DW_AT_lower_bound
, &min
))
1622 if (dwarf2_find_attribute(child
, DW_AT_upper_bound
, &max
))
1623 cnt
.u
.uvalue
= max
.u
.uvalue
+ 1 - min
.u
.uvalue
;
1624 else if (!dwarf2_find_attribute(child
, DW_AT_count
, &cnt
))
1627 case DW_TAG_enumeration_type
:
1628 symt
= dwarf2_parse_enumeration_type(child
);
1629 if (symt_check_tag(symt
, SymTagEnum
))
1631 struct symt_enum
* enum_symt
= (struct symt_enum
*)symt
;
1632 idx_type
= enum_symt
->base_type
;
1635 for (j
= 0; j
< enum_symt
->vchildren
.num_elts
; ++j
)
1637 struct symt
** pc
= vector_at(&enum_symt
->vchildren
, j
);
1638 if (pc
&& symt_check_tag(*pc
, SymTagData
))
1640 struct symt_data
* elt
= (struct symt_data
*)(*pc
);
1641 if (elt
->u
.value
.n1
.n2
.n3
.lVal
< min
.u
.uvalue
)
1642 min
.u
.uvalue
= elt
->u
.value
.n1
.n2
.n3
.lVal
;
1643 if (elt
->u
.value
.n1
.n2
.n3
.lVal
> max
.u
.uvalue
)
1644 max
.u
.uvalue
= elt
->u
.value
.n1
.n2
.n3
.lVal
;
1650 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1651 child
->abbrev
->tag
, dwarf2_debug_di(di
));
1655 di
->symt
= &symt_new_array(di
->unit_ctx
->module_ctx
->module
, min
.u
.uvalue
, cnt
.u
.uvalue
, ref_type
, idx_type
)->symt
;
1659 static struct symt
* dwarf2_parse_const_type(dwarf2_debug_info_t
* di
)
1661 struct symt
* ref_type
;
1663 if (di
->symt
) return di
->symt
;
1665 TRACE("%s\n", dwarf2_debug_di(di
));
1667 ref_type
= dwarf2_lookup_type(di
);
1668 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1669 di
->symt
= ref_type
;
1674 static struct symt
* dwarf2_parse_volatile_type(dwarf2_debug_info_t
* di
)
1676 struct symt
* ref_type
;
1678 if (di
->symt
) return di
->symt
;
1680 TRACE("%s\n", dwarf2_debug_di(di
));
1682 ref_type
= dwarf2_lookup_type(di
);
1683 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1684 di
->symt
= ref_type
;
1689 static struct symt
* dwarf2_parse_restrict_type(dwarf2_debug_info_t
* di
)
1691 struct symt
* ref_type
;
1693 if (di
->symt
) return di
->symt
;
1695 TRACE("%s\n", dwarf2_debug_di(di
));
1697 ref_type
= dwarf2_lookup_type(di
);
1698 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1699 di
->symt
= ref_type
;
1704 static struct symt
* dwarf2_parse_unspecified_type(dwarf2_debug_info_t
* di
)
1706 struct attribute name
;
1709 TRACE("%s\n", dwarf2_debug_di(di
));
1711 if (di
->symt
) return di
->symt
;
1713 basic
= &symt_get_basic(btVoid
, 0)->symt
;
1714 if (dwarf2_find_attribute(di
, DW_AT_name
, &name
))
1715 /* define the missing type as a typedef to void... */
1716 di
->symt
= &symt_new_typedef(di
->unit_ctx
->module_ctx
->module
, basic
, name
.u
.string
)->symt
;
1717 else /* or use void if it doesn't even have a name */
1720 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1724 static struct symt
* dwarf2_parse_reference_type(dwarf2_debug_info_t
* di
)
1726 struct symt
* ref_type
= NULL
;
1728 if (di
->symt
) return di
->symt
;
1730 TRACE("%s\n", dwarf2_debug_di(di
));
1732 ref_type
= dwarf2_lookup_type(di
);
1733 /* FIXME: for now, we hard-wire C++ references to pointers */
1734 di
->symt
= &symt_new_pointer(di
->unit_ctx
->module_ctx
->module
, ref_type
,
1735 di
->unit_ctx
->module_ctx
->module
->cpu
->word_size
)->symt
;
1737 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1742 static void dwarf2_parse_udt_member(dwarf2_debug_info_t
* di
,
1743 struct symt_udt
* parent
)
1745 struct symt
* elt_type
;
1746 struct attribute name
;
1747 struct attribute bit_size
;
1748 struct attribute bit_offset
;
1749 struct location loc
;
1753 TRACE("%s\n", dwarf2_debug_di(di
));
1755 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1756 elt_type
= dwarf2_lookup_type(di
);
1757 if (dwarf2_compute_location_attr(di
->unit_ctx
, di
, DW_AT_data_member_location
, &loc
, NULL
))
1759 if (loc
.kind
!= loc_absolute
)
1761 FIXME("Unexpected offset computation for member %s in %ls!%s\n",
1762 name
.u
.string
, di
->unit_ctx
->module_ctx
->module
->modulename
, parent
->hash_elt
.name
);
1766 TRACE("found member_location at %s -> %Iu\n",
1767 dwarf2_debug_di(di
), loc
.offset
);
1771 if (!dwarf2_find_attribute(di
, DW_AT_bit_size
, &bit_size
))
1772 bit_size
.u
.uvalue
= 0;
1773 if (dwarf2_find_attribute(di
, DW_AT_bit_offset
, &bit_offset
))
1775 /* FIXME: we should only do this when implementation is LSB (which is
1776 * the case on i386 processors)
1778 struct attribute nbytes
;
1779 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &nbytes
))
1782 nbytes
.u
.uvalue
= symt_get_info(di
->unit_ctx
->module_ctx
->module
, elt_type
, TI_GET_LENGTH
, &size
) ?
1783 (ULONG_PTR
)size
: 0;
1785 bit_offset
.u
.uvalue
= nbytes
.u
.uvalue
* 8 - bit_offset
.u
.uvalue
- bit_size
.u
.uvalue
;
1787 else bit_offset
.u
.uvalue
= 0;
1788 symt_add_udt_element(di
->unit_ctx
->module_ctx
->module
, parent
, name
.u
.string
, elt_type
,
1789 loc
.offset
, bit_offset
.u
.uvalue
,
1792 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1795 static struct symt
* dwarf2_parse_subprogram(dwarf2_debug_info_t
* di
);
1797 static struct symt
* dwarf2_parse_udt_type(dwarf2_debug_info_t
* di
,
1800 struct attribute name
;
1801 struct attribute size
;
1802 struct vector
* children
;
1803 dwarf2_debug_info_t
*child
;
1806 if (di
->symt
) return di
->symt
;
1808 TRACE("%s\n", dwarf2_debug_di(di
));
1810 /* quirk... FIXME provide real support for anonymous UDTs */
1811 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
1812 name
.u
.string
= "zz_anon_zz";
1813 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1815 di
->symt
= &symt_new_udt(di
->unit_ctx
->module_ctx
->module
, dwarf2_get_cpp_name(di
, name
.u
.string
),
1816 size
.u
.uvalue
, udt
)->symt
;
1818 children
= dwarf2_get_di_children(di
);
1819 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1821 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1823 switch (child
->abbrev
->tag
)
1825 case DW_TAG_array_type
:
1826 dwarf2_parse_array_type(child
);
1829 /* FIXME: should I follow the sibling stuff ?? */
1830 if (symt_check_tag(di
->symt
, SymTagUDT
))
1831 dwarf2_parse_udt_member(child
, (struct symt_udt
*)di
->symt
);
1833 case DW_TAG_enumeration_type
:
1834 dwarf2_parse_enumeration_type(child
);
1836 case DW_TAG_subprogram
:
1837 dwarf2_parse_subprogram(child
);
1839 case DW_TAG_const_type
:
1840 dwarf2_parse_const_type(child
);
1842 case DW_TAG_volatile_type
:
1843 dwarf2_parse_volatile_type(child
);
1845 case DW_TAG_pointer_type
:
1846 dwarf2_parse_pointer_type(child
);
1848 case DW_TAG_subrange_type
:
1849 dwarf2_parse_subrange_type(child
);
1851 case DW_TAG_structure_type
:
1852 case DW_TAG_class_type
:
1853 case DW_TAG_union_type
:
1854 case DW_TAG_typedef
:
1855 /* FIXME: we need to handle nested udt definitions */
1856 case DW_TAG_inheritance
:
1857 case DW_TAG_interface_type
:
1858 case DW_TAG_template_type_param
:
1859 case DW_TAG_template_value_param
:
1860 case DW_TAG_variable
:
1861 case DW_TAG_imported_declaration
:
1862 case DW_TAG_ptr_to_member_type
:
1863 case DW_TAG_GNU_template_template_param
:
1864 case DW_TAG_GNU_template_parameter_pack
:
1865 case DW_TAG_GNU_formal_parameter_pack
:
1866 /* FIXME: some C++ related stuff */
1869 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1870 child
->abbrev
->tag
, dwarf2_debug_di(di
));
1878 static void dwarf2_parse_enumerator(dwarf2_debug_info_t
* di
,
1879 struct symt_enum
* parent
)
1881 struct attribute name
;
1882 struct attribute value
;
1884 TRACE("%s\n", dwarf2_debug_di(di
));
1886 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) return;
1887 if (!dwarf2_find_attribute(di
, DW_AT_const_value
, &value
)) value
.u
.svalue
= 0;
1888 symt_add_enum_element(di
->unit_ctx
->module_ctx
->module
, parent
, name
.u
.string
, value
.u
.svalue
);
1890 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
1893 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_debug_info_t
* di
)
1895 struct attribute name
;
1896 struct attribute attrtype
;
1897 dwarf2_debug_info_t
*ditype
;
1899 struct vector
* children
;
1900 dwarf2_debug_info_t
*child
;
1903 if (di
->symt
) return di
->symt
;
1905 TRACE("%s\n", dwarf2_debug_di(di
));
1907 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1908 if (dwarf2_find_attribute(di
, DW_AT_type
, &attrtype
) && (ditype
= dwarf2_jump_to_debug_info(&attrtype
)) != NULL
)
1909 type
= ditype
->symt
;
1910 else /* no type found for this enumeration, construct it from size */
1912 struct attribute size
;
1913 struct symt_basic
* basetype
;
1915 if (!dwarf2_find_attribute(di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 4;
1917 switch (size
.u
.uvalue
) /* FIXME: that's wrong */
1919 case 1: basetype
= symt_get_basic(btInt
, 1); break;
1920 case 2: basetype
= symt_get_basic(btInt
, 2); break;
1922 case 4: basetype
= symt_get_basic(btInt
, 4); break;
1924 type
= &basetype
->symt
;
1927 di
->symt
= &symt_new_enum(di
->unit_ctx
->module_ctx
->module
, name
.u
.string
, type
)->symt
;
1928 children
= dwarf2_get_di_children(di
);
1930 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1932 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1934 switch (child
->abbrev
->tag
)
1936 case DW_TAG_enumerator
:
1937 if (symt_check_tag(di
->symt
, SymTagEnum
))
1938 dwarf2_parse_enumerator(child
, (struct symt_enum
*)di
->symt
);
1941 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1942 di
->abbrev
->tag
, dwarf2_debug_di(di
));
1948 /* structure used to pass information around when parsing a subprogram */
1949 typedef struct dwarf2_subprogram_s
1951 dwarf2_parse_context_t
* ctx
;
1952 struct symt_function
* top_func
;
1953 struct symt_function
* current_func
; /* either symt_function* or symt_inlinesite* */
1954 struct symt_block
* current_block
;
1955 BOOL non_computed_variable
;
1956 struct location frame
;
1957 } dwarf2_subprogram_t
;
1959 /******************************************************************
1960 * dwarf2_parse_variable
1962 * Parses any variable (parameter, local/global variable)
1964 static void dwarf2_parse_variable(dwarf2_subprogram_t
* subpgm
,
1965 dwarf2_debug_info_t
* di
)
1967 struct symt
* param_type
;
1968 struct attribute name
, value
;
1969 struct location loc
;
1972 TRACE("%s\n", dwarf2_debug_di(di
));
1974 is_pmt
= !subpgm
->current_block
&& di
->abbrev
->tag
== DW_TAG_formal_parameter
;
1975 param_type
= dwarf2_lookup_type(di
);
1977 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
)) {
1978 /* cannot do much without the name, the functions below won't like it. */
1981 if (dwarf2_compute_location_attr(subpgm
->ctx
, di
, DW_AT_location
,
1982 &loc
, &subpgm
->frame
))
1984 struct attribute ext
;
1986 TRACE("found parameter %s (kind=%d, offset=%Id, reg=%d) at %s\n",
1987 debugstr_a(name
.u
.string
), loc
.kind
, loc
.offset
, loc
.reg
,
1988 dwarf2_debug_unit_ctx(subpgm
->ctx
));
1995 /* it's a global variable */
1996 /* FIXME: we don't handle its scope yet */
1997 if (!dwarf2_find_attribute(di
, DW_AT_external
, &ext
))
1999 loc
.offset
+= subpgm
->ctx
->module_ctx
->load_offset
;
2000 symt_new_global_variable(subpgm
->ctx
->module_ctx
->module
,
2001 ext
.u
.uvalue
? NULL
: subpgm
->ctx
->compiland
,
2002 dwarf2_get_cpp_name(di
, name
.u
.string
), !ext
.u
.uvalue
,
2003 loc
, 0, param_type
);
2006 subpgm
->non_computed_variable
= TRUE
;
2010 /* either a pmt/variable relative to frame pointer or
2011 * pmt/variable in a register
2013 if (subpgm
->current_func
)
2014 symt_add_func_local(subpgm
->ctx
->module_ctx
->module
, subpgm
->current_func
,
2015 is_pmt
? DataIsParam
: DataIsLocal
,
2016 &loc
, subpgm
->current_block
, param_type
, name
.u
.string
);
2020 else if (dwarf2_find_attribute(di
, DW_AT_const_value
, &value
))
2032 V_UI4(&v
) = value
.u
.uvalue
;
2036 case DW_FORM_sec_offset
:
2038 V_UI8(&v
) = value
.u
.lluvalue
;
2043 V_I4(&v
) = value
.u
.svalue
;
2047 case DW_FORM_string
:
2048 /* FIXME: native doesn't report const strings from here !!
2049 * however, the value of the string is in the code somewhere
2051 V_VT(&v
) = VT_BYREF
;
2052 V_BYREF(&v
) = pool_strdup(&subpgm
->ctx
->module_ctx
->module
->pool
, value
.u
.string
);
2056 case DW_FORM_block1
:
2057 case DW_FORM_block2
:
2058 case DW_FORM_block4
:
2059 case DW_FORM_exprloc
:
2061 switch (value
.u
.block
.size
)
2063 case 1: V_I4(&v
) = *(BYTE
*)value
.u
.block
.ptr
; break;
2064 case 2: V_I4(&v
) = *(USHORT
*)value
.u
.block
.ptr
; break;
2065 case 4: V_I4(&v
) = *(DWORD
*)value
.u
.block
.ptr
; break;
2067 V_VT(&v
) = VT_BYREF
;
2068 V_BYREF(&v
) = pool_alloc(&subpgm
->ctx
->module_ctx
->module
->pool
, value
.u
.block
.size
);
2069 memcpy(V_BYREF(&v
), value
.u
.block
.ptr
, value
.u
.block
.size
);
2074 FIXME("Unsupported form for const value %s (%Ix)\n",
2075 debugstr_a(name
.u
.string
), value
.form
);
2076 V_VT(&v
) = VT_EMPTY
;
2078 if (subpgm
->current_func
)
2080 if (is_pmt
) WARN("Constant parameter %s reported as local variable in function '%s'\n",
2081 debugstr_a(name
.u
.string
), subpgm
->current_func
->hash_elt
.name
);
2082 di
->symt
= &symt_add_func_constant(subpgm
->ctx
->module_ctx
->module
,
2083 subpgm
->current_func
, subpgm
->current_block
,
2084 param_type
, name
.u
.string
, &v
)->symt
;
2087 di
->symt
= &symt_new_constant(subpgm
->ctx
->module_ctx
->module
, subpgm
->ctx
->compiland
,
2088 name
.u
.string
, param_type
, &v
)->symt
;
2092 if (subpgm
->current_func
)
2094 /* local variable has been optimized away... report anyway */
2095 loc
.kind
= loc_error
;
2096 loc
.reg
= loc_err_no_location
;
2097 symt_add_func_local(subpgm
->ctx
->module_ctx
->module
, subpgm
->current_func
,
2098 is_pmt
? DataIsParam
: DataIsLocal
,
2099 &loc
, subpgm
->current_block
, param_type
, name
.u
.string
);
2103 struct attribute is_decl
;
2104 /* only warn when di doesn't represent a declaration */
2105 if (!dwarf2_find_attribute(di
, DW_AT_declaration
, &is_decl
) ||
2106 !is_decl
.u
.uvalue
|| is_decl
.gotten_from
!= attr_direct
)
2107 WARN("dropping global variable %s which has been optimized away\n", debugstr_a(name
.u
.string
));
2110 if (is_pmt
&& subpgm
->current_func
&& symt_check_tag(subpgm
->current_func
->type
, SymTagFunctionType
))
2111 symt_add_function_signature_parameter(subpgm
->ctx
->module_ctx
->module
,
2112 (struct symt_function_signature
*)subpgm
->current_func
->type
,
2115 if (dwarf2_get_di_children(di
)) FIXME("Unsupported children\n");
2118 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t
* subpgm
,
2119 const dwarf2_debug_info_t
* di
)
2121 struct attribute name
;
2122 struct attribute low_pc
;
2123 struct location loc
;
2125 TRACE("%s\n", dwarf2_debug_di(di
));
2127 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
2128 name
.u
.string
= NULL
;
2129 if (dwarf2_find_attribute(di
, DW_AT_low_pc
, &low_pc
))
2131 loc
.kind
= loc_absolute
;
2132 loc
.offset
= subpgm
->ctx
->module_ctx
->load_offset
+ low_pc
.u
.uvalue
- subpgm
->top_func
->address
;
2133 symt_add_function_point(subpgm
->ctx
->module_ctx
->module
, subpgm
->top_func
, SymTagLabel
,
2134 &loc
, name
.u
.string
);
2137 WARN("Label %s inside function %s doesn't have an address... don't register it\n",
2138 name
.u
.string
, subpgm
->top_func
->hash_elt
.name
);
2141 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
2142 dwarf2_debug_info_t
* di
);
2144 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_debug_info_t
* di
);
2146 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t
* subpgm
,
2147 dwarf2_debug_info_t
* di
)
2149 struct attribute name
;
2150 ULONG_PTR low_pc
, high_pc
;
2151 struct symt
* ret_type
;
2152 struct symt_function_signature
* sig_type
;
2153 struct symt_inlinesite
* inlined
;
2154 struct vector
* children
;
2155 dwarf2_debug_info_t
*child
;
2157 struct addr_range
* adranges
;
2158 unsigned num_adranges
;
2160 TRACE("%s\n", dwarf2_debug_di(di
));
2162 if (!dwarf2_read_range(subpgm
->ctx
, di
, &low_pc
, &high_pc
))
2164 WARN("cannot read range\n");
2167 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
2169 FIXME("No name for function... dropping function\n");
2172 ret_type
= dwarf2_lookup_type(di
);
2174 /* FIXME: assuming C source code */
2175 sig_type
= symt_new_function_signature(subpgm
->ctx
->module_ctx
->module
, ret_type
, CV_CALL_FAR_C
);
2177 inlined
= symt_new_inlinesite(subpgm
->ctx
->module_ctx
->module
,
2179 subpgm
->current_block
? &subpgm
->current_block
->symt
: &subpgm
->current_func
->symt
,
2180 dwarf2_get_cpp_name(di
, name
.u
.string
),
2181 subpgm
->ctx
->module_ctx
->load_offset
+ low_pc
,
2183 subpgm
->current_func
= (struct symt_function
*)inlined
;
2184 subpgm
->current_block
= NULL
;
2186 if ((adranges
= dwarf2_get_ranges(di
, &num_adranges
)) != NULL
)
2188 for (i
= 0; i
< num_adranges
; ++i
)
2189 symt_add_inlinesite_range(subpgm
->ctx
->module_ctx
->module
, inlined
,
2190 adranges
[i
].low
, adranges
[i
].high
);
2194 WARN("cannot read ranges\n");
2196 children
= dwarf2_get_di_children(di
);
2197 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2199 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2201 switch (child
->abbrev
->tag
)
2203 case DW_TAG_formal_parameter
:
2204 case DW_TAG_variable
:
2205 dwarf2_parse_variable(subpgm
, child
);
2207 case DW_TAG_lexical_block
:
2208 dwarf2_parse_subprogram_block(subpgm
, child
);
2210 case DW_TAG_inlined_subroutine
:
2211 dwarf2_parse_inlined_subroutine(subpgm
, child
);
2214 dwarf2_parse_subprogram_label(subpgm
, child
);
2216 case DW_TAG_GNU_call_site
:
2217 /* this isn't properly supported by dbghelp interface. skip it for now */
2220 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2221 child
->abbrev
->tag
, dwarf2_debug_di(di
));
2224 subpgm
->current_block
= symt_check_tag(subpgm
->current_func
->container
, SymTagBlock
) ?
2225 (struct symt_block
*)subpgm
->current_func
->container
: NULL
;
2226 subpgm
->current_func
= (struct symt_function
*)symt_get_upper_inlined((struct symt_inlinesite
*)subpgm
->current_func
);
2229 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
2230 dwarf2_debug_info_t
* di
)
2232 ULONG_PTR low_pc
, high_pc
;
2233 struct vector
* children
;
2234 dwarf2_debug_info_t
*child
;
2237 TRACE("%s\n", dwarf2_debug_di(di
));
2239 if (!dwarf2_read_range(subpgm
->ctx
, di
, &low_pc
, &high_pc
))
2245 subpgm
->current_block
= symt_open_func_block(subpgm
->ctx
->module_ctx
->module
, subpgm
->current_func
, subpgm
->current_block
,
2246 subpgm
->ctx
->module_ctx
->load_offset
+ low_pc
- subpgm
->current_func
->address
,
2249 children
= dwarf2_get_di_children(di
);
2250 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2252 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2254 switch (child
->abbrev
->tag
)
2256 case DW_TAG_inlined_subroutine
:
2257 dwarf2_parse_inlined_subroutine(subpgm
, child
);
2259 case DW_TAG_variable
:
2260 dwarf2_parse_variable(subpgm
, child
);
2262 case DW_TAG_pointer_type
:
2263 dwarf2_parse_pointer_type(child
);
2265 case DW_TAG_subroutine_type
:
2266 dwarf2_parse_subroutine_type(child
);
2268 case DW_TAG_const_type
:
2269 dwarf2_parse_const_type(child
);
2271 case DW_TAG_lexical_block
:
2272 dwarf2_parse_subprogram_block(subpgm
, child
);
2274 case DW_TAG_subprogram
:
2275 /* FIXME: likely a declaration (to be checked)
2279 case DW_TAG_formal_parameter
:
2280 /* FIXME: likely elements for exception handling (GCC flavor)
2284 case DW_TAG_imported_module
:
2285 /* C++ stuff to be silenced (for now) */
2287 case DW_TAG_GNU_call_site
:
2288 /* this isn't properly supported by dbghelp interface. skip it for now */
2291 dwarf2_parse_subprogram_label(subpgm
, child
);
2293 case DW_TAG_class_type
:
2294 case DW_TAG_structure_type
:
2295 case DW_TAG_union_type
:
2296 case DW_TAG_enumeration_type
:
2297 case DW_TAG_typedef
:
2298 /* the type referred to will be loaded when we need it, so skip it */
2301 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2302 child
->abbrev
->tag
, dwarf2_debug_di(di
));
2306 symt_close_func_block(subpgm
->ctx
->module_ctx
->module
, subpgm
->current_func
, subpgm
->current_block
, 0);
2307 subpgm
->current_block
= symt_check_tag(subpgm
->current_block
->container
, SymTagBlock
) ?
2308 (struct symt_block
*)subpgm
->current_block
->container
: NULL
;
2311 static struct symt
* dwarf2_parse_subprogram(dwarf2_debug_info_t
* di
)
2313 struct attribute name
;
2314 struct addr_range
* addr_ranges
;
2315 unsigned num_addr_ranges
;
2316 struct attribute is_decl
;
2317 struct attribute inline_flags
;
2318 struct symt
* ret_type
;
2319 struct symt_function_signature
* sig_type
;
2320 dwarf2_subprogram_t subpgm
;
2321 struct vector
* children
;
2322 dwarf2_debug_info_t
* child
;
2325 if (di
->symt
) return di
->symt
;
2327 TRACE("%s\n", dwarf2_debug_di(di
));
2329 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
2331 WARN("No name for function... dropping function\n");
2334 /* if it's an abstract representation of an inline function, there should be
2335 * a concrete object that we'll handle
2337 if (dwarf2_find_attribute(di
, DW_AT_inline
, &inline_flags
) &&
2338 inline_flags
.gotten_from
== attr_direct
&&
2339 inline_flags
.u
.uvalue
!= DW_INL_not_inlined
)
2341 TRACE("Function %s declared as inlined (%Id)... skipping\n",
2342 debugstr_a(name
.u
.string
), inline_flags
.u
.uvalue
);
2346 if (dwarf2_find_attribute(di
, DW_AT_declaration
, &is_decl
) &&
2347 is_decl
.u
.uvalue
&& is_decl
.gotten_from
== attr_direct
)
2349 /* it's a real declaration, skip it */
2352 if ((addr_ranges
= dwarf2_get_ranges(di
, &num_addr_ranges
)) == NULL
)
2354 WARN("cannot get range for %s\n", debugstr_a(name
.u
.string
));
2357 /* As functions (defined as inline assembly) get debug info with dwarf
2358 * (not the case for stabs), we just drop Wine's thunks here...
2359 * Actual thunks will be created in elf_module from the symbol table
2361 if (elf_is_in_thunk_area(di
->unit_ctx
->module_ctx
->load_offset
+ addr_ranges
[0].low
, di
->unit_ctx
->module_ctx
->thunks
) >= 0)
2366 ret_type
= dwarf2_lookup_type(di
);
2368 /* FIXME: assuming C source code */
2369 sig_type
= symt_new_function_signature(di
->unit_ctx
->module_ctx
->module
, ret_type
, CV_CALL_FAR_C
);
2370 subpgm
.top_func
= symt_new_function(di
->unit_ctx
->module_ctx
->module
, di
->unit_ctx
->compiland
,
2371 dwarf2_get_cpp_name(di
, name
.u
.string
),
2372 addr_ranges
[0].low
, addr_ranges
[0].high
- addr_ranges
[0].low
, &sig_type
->symt
);
2373 if (num_addr_ranges
> 1)
2374 WARN("Function %s has multiple address ranges, only using the first one\n", name
.u
.string
);
2377 subpgm
.current_func
= subpgm
.top_func
;
2378 di
->symt
= &subpgm
.top_func
->symt
;
2379 subpgm
.ctx
= di
->unit_ctx
;
2380 if (!dwarf2_compute_location_attr(di
->unit_ctx
, di
, DW_AT_frame_base
,
2381 &subpgm
.frame
, NULL
))
2384 subpgm
.frame
.kind
= loc_regrel
;
2385 subpgm
.frame
.reg
= di
->unit_ctx
->module_ctx
->module
->cpu
->frame_regno
;
2386 subpgm
.frame
.offset
= 0;
2388 subpgm
.non_computed_variable
= FALSE
;
2389 subpgm
.current_block
= NULL
;
2391 children
= dwarf2_get_di_children(di
);
2392 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2394 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2396 switch (child
->abbrev
->tag
)
2398 case DW_TAG_variable
:
2399 case DW_TAG_formal_parameter
:
2400 dwarf2_parse_variable(&subpgm
, child
);
2402 case DW_TAG_lexical_block
:
2403 dwarf2_parse_subprogram_block(&subpgm
, child
);
2405 case DW_TAG_inlined_subroutine
:
2406 dwarf2_parse_inlined_subroutine(&subpgm
, child
);
2408 case DW_TAG_pointer_type
:
2409 dwarf2_parse_pointer_type(child
);
2411 case DW_TAG_const_type
:
2412 dwarf2_parse_const_type(child
);
2414 case DW_TAG_subprogram
:
2415 /* FIXME: likely a declaration (to be checked)
2420 dwarf2_parse_subprogram_label(&subpgm
, child
);
2422 case DW_TAG_class_type
:
2423 case DW_TAG_structure_type
:
2424 case DW_TAG_union_type
:
2425 case DW_TAG_enumeration_type
:
2426 case DW_TAG_typedef
:
2427 /* the type referred to will be loaded when we need it, so skip it */
2429 case DW_TAG_unspecified_parameters
:
2430 case DW_TAG_template_type_param
:
2431 case DW_TAG_template_value_param
:
2432 case DW_TAG_GNU_call_site
:
2433 case DW_TAG_GNU_template_parameter_pack
:
2434 case DW_TAG_GNU_formal_parameter_pack
:
2435 /* FIXME: no support in dbghelp's internals so far */
2438 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2439 child
->abbrev
->tag
, dwarf2_debug_di(di
));
2443 if (subpgm
.non_computed_variable
|| subpgm
.frame
.kind
>= loc_user
)
2445 symt_add_function_point(di
->unit_ctx
->module_ctx
->module
, subpgm
.top_func
, SymTagCustom
,
2446 &subpgm
.frame
, NULL
);
2452 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_debug_info_t
* di
)
2454 struct symt
* ret_type
;
2455 struct symt_function_signature
* sig_type
;
2456 struct vector
* children
;
2457 dwarf2_debug_info_t
* child
;
2460 if (di
->symt
) return di
->symt
;
2462 TRACE("%s\n", dwarf2_debug_di(di
));
2464 ret_type
= dwarf2_lookup_type(di
);
2466 /* FIXME: assuming C source code */
2467 sig_type
= symt_new_function_signature(di
->unit_ctx
->module_ctx
->module
, ret_type
, CV_CALL_FAR_C
);
2469 children
= dwarf2_get_di_children(di
);
2470 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2472 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2474 switch (child
->abbrev
->tag
)
2476 case DW_TAG_formal_parameter
:
2477 symt_add_function_signature_parameter(di
->unit_ctx
->module_ctx
->module
, sig_type
,
2478 dwarf2_lookup_type(child
));
2480 case DW_TAG_unspecified_parameters
:
2481 WARN("Unsupported unspecified parameters\n");
2486 return di
->symt
= &sig_type
->symt
;
2489 static void dwarf2_parse_namespace(dwarf2_debug_info_t
* di
)
2491 struct vector
* children
;
2492 dwarf2_debug_info_t
* child
;
2495 if (di
->symt
) return;
2497 TRACE("%s\n", dwarf2_debug_di(di
));
2499 di
->symt
= &symt_get_basic(btVoid
, 0)->symt
;
2501 children
= dwarf2_get_di_children(di
);
2502 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2504 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2505 dwarf2_load_one_entry(child
);
2509 static void dwarf2_parse_imported_unit(dwarf2_debug_info_t
* di
)
2511 struct attribute imp
;
2513 if (di
->symt
) return;
2515 TRACE("%s\n", dwarf2_debug_di(di
));
2517 if (dwarf2_find_attribute(di
, DW_AT_import
, &imp
))
2519 dwarf2_debug_info_t
* jmp
= dwarf2_jump_to_debug_info(&imp
);
2520 if (jmp
) di
->symt
= jmp
->symt
;
2521 else FIXME("Couldn't load imported CU\n");
2524 FIXME("Couldn't find import attribute\n");
2527 static void dwarf2_load_one_entry(dwarf2_debug_info_t
* di
)
2529 switch (di
->abbrev
->tag
)
2531 case DW_TAG_typedef
:
2532 dwarf2_parse_typedef(di
);
2534 case DW_TAG_base_type
:
2535 dwarf2_parse_base_type(di
);
2537 case DW_TAG_pointer_type
:
2538 dwarf2_parse_pointer_type(di
);
2540 case DW_TAG_class_type
:
2541 dwarf2_parse_udt_type(di
, UdtClass
);
2543 case DW_TAG_structure_type
:
2544 dwarf2_parse_udt_type(di
, UdtStruct
);
2546 case DW_TAG_union_type
:
2547 dwarf2_parse_udt_type(di
, UdtUnion
);
2549 case DW_TAG_array_type
:
2550 dwarf2_parse_array_type(di
);
2552 case DW_TAG_const_type
:
2553 dwarf2_parse_const_type(di
);
2555 case DW_TAG_volatile_type
:
2556 dwarf2_parse_volatile_type(di
);
2558 case DW_TAG_restrict_type
:
2559 dwarf2_parse_restrict_type(di
);
2561 case DW_TAG_unspecified_type
:
2562 dwarf2_parse_unspecified_type(di
);
2564 case DW_TAG_reference_type
:
2565 case DW_TAG_rvalue_reference_type
:
2566 dwarf2_parse_reference_type(di
);
2568 case DW_TAG_enumeration_type
:
2569 dwarf2_parse_enumeration_type(di
);
2571 case DW_TAG_subprogram
:
2572 dwarf2_parse_subprogram(di
);
2574 case DW_TAG_subroutine_type
:
2575 dwarf2_parse_subroutine_type(di
);
2577 case DW_TAG_variable
:
2579 dwarf2_subprogram_t subpgm
;
2581 subpgm
.ctx
= di
->unit_ctx
;
2582 subpgm
.top_func
= subpgm
.current_func
= NULL
;
2583 subpgm
.current_block
= NULL
;
2584 subpgm
.frame
.kind
= loc_absolute
;
2585 subpgm
.frame
.offset
= 0;
2586 subpgm
.frame
.reg
= Wine_DW_no_register
;
2587 dwarf2_parse_variable(&subpgm
, di
);
2590 case DW_TAG_namespace
:
2591 dwarf2_parse_namespace(di
);
2593 case DW_TAG_subrange_type
:
2594 dwarf2_parse_subrange_type(di
);
2596 case DW_TAG_imported_unit
:
2597 dwarf2_parse_imported_unit(di
);
2599 /* keep it silent until we need DW_OP_call_xxx support */
2600 case DW_TAG_dwarf_procedure
:
2601 /* silence a couple of non-C language defines (mainly C++ but others too) */
2602 case DW_TAG_imported_module
:
2603 case DW_TAG_imported_declaration
:
2604 case DW_TAG_interface_type
:
2606 case DW_TAG_ptr_to_member_type
:
2609 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2610 di
->abbrev
->tag
, dwarf2_debug_di(di
));
2614 static void dwarf2_set_line_number(struct module
* module
, ULONG_PTR address
,
2615 const struct vector
* v
, unsigned file
, unsigned line
)
2617 struct symt_function
* func
;
2618 struct symt_inlinesite
* inlined
;
2619 struct symt_ht
* symt
;
2622 if (!file
|| !(psrc
= vector_at(v
, file
- 1))) return;
2624 TRACE("%s %Ix %s %u\n",
2625 debugstr_w(module
->modulename
), address
, debugstr_a(source_get(module
, *psrc
)), line
);
2626 symt
= symt_find_symbol_at(module
, address
);
2627 if (symt_check_tag(&symt
->symt
, SymTagFunction
))
2629 func
= (struct symt_function
*)symt
;
2630 for (inlined
= func
->next_inlinesite
; inlined
; inlined
= inlined
->func
.next_inlinesite
)
2633 for (i
= 0; i
< inlined
->vranges
.num_elts
; ++i
)
2635 struct addr_range
* ar
= (struct addr_range
*)vector_at(&inlined
->vranges
, i
);
2636 if (ar
->low
<= address
&& address
< ar
->high
)
2638 symt_add_func_line(module
, &inlined
->func
, *psrc
, line
, address
);
2639 return; /* only add to lowest matching inline site */
2643 symt_add_func_line(module
, func
, *psrc
, line
, address
);
2647 static BOOL
dwarf2_parse_line_numbers(dwarf2_parse_context_t
* ctx
,
2648 const char* compile_dir
,
2651 dwarf2_traverse_context_t traverse
;
2653 unsigned insn_size
, version
, default_stmt
;
2654 unsigned line_range
, opcode_base
;
2656 unsigned char offset_size
;
2657 const unsigned char* opcode_len
;
2659 struct vector files
;
2662 /* section with line numbers stripped */
2663 if (ctx
->module_ctx
->sections
[section_line
].address
== IMAGE_NO_MAP
)
2666 if (offset
+ 4 > ctx
->module_ctx
->sections
[section_line
].size
)
2668 WARN("out of bounds offset\n");
2671 traverse
.data
= ctx
->module_ctx
->sections
[section_line
].address
+ offset
;
2672 traverse
.end_data
= ctx
->module_ctx
->sections
[section_line
].address
+ ctx
->module_ctx
->sections
[section_line
].size
;
2674 length
= dwarf2_parse_3264(&traverse
, &offset_size
);
2675 if (offset_size
!= ctx
->head
.offset_size
)
2677 WARN("Mismatch in 32/64 bit format\n");
2680 traverse
.end_data
= traverse
.data
+ length
;
2682 if (traverse
.end_data
> ctx
->module_ctx
->sections
[section_line
].address
+ ctx
->module_ctx
->sections
[section_line
].size
)
2684 WARN("out of bounds header\n");
2687 version
= dwarf2_parse_u2(&traverse
);
2688 dwarf2_parse_offset(&traverse
, offset_size
); /* header_len */
2689 insn_size
= dwarf2_parse_byte(&traverse
);
2691 dwarf2_parse_byte(&traverse
); /* max_operations_per_instructions */
2692 default_stmt
= dwarf2_parse_byte(&traverse
);
2693 line_base
= (signed char)dwarf2_parse_byte(&traverse
);
2694 line_range
= dwarf2_parse_byte(&traverse
);
2695 opcode_base
= dwarf2_parse_byte(&traverse
);
2697 opcode_len
= traverse
.data
;
2698 traverse
.data
+= opcode_base
- 1;
2700 vector_init(&dirs
, sizeof(const char*), 4);
2701 p
= vector_add(&dirs
, &ctx
->pool
);
2702 *p
= compile_dir
? compile_dir
: ".";
2703 while (traverse
.data
< traverse
.end_data
&& *traverse
.data
)
2705 const char* rel
= (const char*)traverse
.data
;
2706 unsigned rellen
= strlen(rel
);
2707 TRACE("Got include %s\n", debugstr_a(rel
));
2708 traverse
.data
+= rellen
+ 1;
2709 p
= vector_add(&dirs
, &ctx
->pool
);
2711 if (*rel
== '/' || !compile_dir
|| !*compile_dir
)
2715 /* include directory relative to compile directory */
2716 unsigned baselen
= strlen(compile_dir
);
2717 char* tmp
= pool_alloc(&ctx
->pool
, baselen
+ 1 + rellen
+ 1);
2718 strcpy(tmp
, compile_dir
);
2719 if (baselen
&& tmp
[baselen
- 1] != '/') tmp
[baselen
++] = '/';
2720 strcpy(&tmp
[baselen
], rel
);
2727 vector_init(&files
, sizeof(unsigned), 16);
2728 while (traverse
.data
< traverse
.end_data
&& *traverse
.data
)
2730 unsigned int dir_index
, mod_time
;
2735 name
= (const char*)traverse
.data
;
2736 traverse
.data
+= strlen(name
) + 1;
2737 dir_index
= dwarf2_leb128_as_unsigned(&traverse
);
2738 mod_time
= dwarf2_leb128_as_unsigned(&traverse
);
2739 length
= dwarf2_leb128_as_unsigned(&traverse
);
2740 dir
= *(const char**)vector_at(&dirs
, dir_index
);
2741 TRACE("Got file %s/%s (%u,%Iu)\n", debugstr_a(dir
), debugstr_a(name
), mod_time
, length
);
2742 psrc
= vector_add(&files
, &ctx
->pool
);
2743 *psrc
= source_new(ctx
->module_ctx
->module
, dir
, name
);
2747 while (traverse
.data
< traverse
.end_data
&& *traverse
.data
)
2749 ULONG_PTR address
= 0;
2752 unsigned is_stmt
= default_stmt
;
2753 BOOL end_sequence
= FALSE
;
2754 unsigned opcode
, extopcode
, i
;
2756 while (!end_sequence
)
2758 opcode
= dwarf2_parse_byte(&traverse
);
2759 TRACE("Got opcode %x\n", opcode
);
2761 if (opcode
>= opcode_base
)
2763 unsigned delta
= opcode
- opcode_base
;
2765 address
+= (delta
/ line_range
) * insn_size
;
2766 line
+= line_base
+ (delta
% line_range
);
2767 dwarf2_set_line_number(ctx
->module_ctx
->module
, address
, &files
, file
, line
);
2774 dwarf2_set_line_number(ctx
->module_ctx
->module
, address
, &files
, file
, line
);
2776 case DW_LNS_advance_pc
:
2777 address
+= insn_size
* dwarf2_leb128_as_unsigned(&traverse
);
2779 case DW_LNS_advance_line
:
2780 line
+= dwarf2_leb128_as_signed(&traverse
);
2782 case DW_LNS_set_file
:
2783 file
= dwarf2_leb128_as_unsigned(&traverse
);
2785 case DW_LNS_set_column
:
2786 dwarf2_leb128_as_unsigned(&traverse
);
2788 case DW_LNS_negate_stmt
:
2791 case DW_LNS_set_basic_block
:
2793 case DW_LNS_const_add_pc
:
2794 address
+= ((255 - opcode_base
) / line_range
) * insn_size
;
2796 case DW_LNS_fixed_advance_pc
:
2797 address
+= dwarf2_parse_u2(&traverse
);
2799 case DW_LNS_extended_op
:
2800 dwarf2_leb128_as_unsigned(&traverse
);
2801 extopcode
= dwarf2_parse_byte(&traverse
);
2804 case DW_LNE_end_sequence
:
2805 dwarf2_set_line_number(ctx
->module_ctx
->module
, address
, &files
, file
, line
);
2806 end_sequence
= TRUE
;
2808 case DW_LNE_set_address
:
2809 address
= ctx
->module_ctx
->load_offset
+ dwarf2_parse_addr_head(&traverse
, &ctx
->head
);
2811 case DW_LNE_define_file
:
2812 FIXME("not handled define file %s\n", debugstr_a((char *)traverse
.data
));
2813 traverse
.data
+= strlen((const char *)traverse
.data
) + 1;
2814 dwarf2_leb128_as_unsigned(&traverse
);
2815 dwarf2_leb128_as_unsigned(&traverse
);
2816 dwarf2_leb128_as_unsigned(&traverse
);
2818 case DW_LNE_set_discriminator
:
2822 descr
= dwarf2_leb128_as_unsigned(&traverse
);
2823 WARN("not handled discriminator %x\n", descr
);
2827 FIXME("Unsupported extended opcode %x\n", extopcode
);
2832 WARN("Unsupported opcode %x\n", opcode
);
2833 for (i
= 0; i
< opcode_len
[opcode
]; i
++)
2834 dwarf2_leb128_as_unsigned(&traverse
);
2843 unsigned dwarf2_cache_cuhead(struct dwarf2_module_info_s
* module
, struct symt_compiland
* c
, const dwarf2_cuhead_t
* head
)
2845 dwarf2_cuhead_t
* ah
;
2847 for (i
= 0; i
< module
->num_cuheads
; ++i
)
2849 if (memcmp(module
->cuheads
[i
], head
, sizeof(*head
)) == 0)
2851 c
->user
= module
->cuheads
[i
];
2855 if (!(ah
= pool_alloc(&c
->container
->module
->pool
, sizeof(*head
)))) return FALSE
;
2856 memcpy(ah
, head
, sizeof(*head
));
2857 module
->cuheads
= realloc(module
->cuheads
, ++module
->num_cuheads
* sizeof(head
));
2858 module
->cuheads
[module
->num_cuheads
- 1] = ah
;
2863 static dwarf2_parse_context_t
* dwarf2_locate_cu(dwarf2_parse_module_context_t
* module_ctx
, ULONG_PTR ref
)
2866 dwarf2_parse_context_t
* ctx
;
2868 for (i
= 0; i
< module_ctx
->unit_contexts
.num_elts
; ++i
)
2870 ctx
= vector_at(&module_ctx
->unit_contexts
, i
);
2871 where
= module_ctx
->sections
[ctx
->section
].address
+ ref
;
2872 if (where
>= ctx
->traverse_DIE
.data
&& where
< ctx
->traverse_DIE
.end_data
)
2875 FIXME("Couldn't find ref 0x%Ix inside sect\n", ref
);
2879 static BOOL
dwarf2_parse_compilation_unit_head(dwarf2_parse_context_t
* ctx
,
2880 dwarf2_traverse_context_t
* mod_ctx
)
2882 dwarf2_traverse_context_t abbrev_ctx
;
2883 const unsigned char* comp_unit_start
= mod_ctx
->data
;
2884 ULONG_PTR cu_length
;
2885 ULONG_PTR cu_abbrev_offset
;
2886 /* FIXME this is a temporary configuration while adding support for dwarf3&4 bits */
2887 static LONG max_supported_dwarf_version
= 0;
2889 cu_length
= dwarf2_parse_3264(mod_ctx
, &ctx
->head
.offset_size
);
2891 ctx
->traverse_DIE
.data
= mod_ctx
->data
;
2892 ctx
->traverse_DIE
.end_data
= mod_ctx
->data
+ cu_length
;
2893 mod_ctx
->data
+= cu_length
;
2894 ctx
->head
.version
= dwarf2_parse_u2(&ctx
->traverse_DIE
);
2895 cu_abbrev_offset
= dwarf2_parse_offset(&ctx
->traverse_DIE
, ctx
->head
.offset_size
);
2896 ctx
->head
.word_size
= dwarf2_parse_byte(&ctx
->traverse_DIE
);
2897 ctx
->status
= UNIT_ERROR
;
2899 TRACE("Compilation Unit Header found at 0x%x:\n",
2900 (int)(comp_unit_start
- ctx
->module_ctx
->sections
[section_debug
].address
));
2901 TRACE("- length: %Iu\n", cu_length
);
2902 TRACE("- version: %u\n", ctx
->head
.version
);
2903 TRACE("- abbrev_offset: %Iu\n", cu_abbrev_offset
);
2904 TRACE("- word_size: %u\n", ctx
->head
.word_size
);
2905 TRACE("- offset_size: %u\n", ctx
->head
.offset_size
);
2907 if (ctx
->head
.version
>= 2)
2908 ctx
->module_ctx
->cu_versions
|= 1 << (ctx
->head
.version
- 2);
2909 if (max_supported_dwarf_version
== 0)
2911 char* env
= getenv("DBGHELP_DWARF_VERSION");
2912 LONG v
= env
? atol(env
) : 4;
2913 max_supported_dwarf_version
= (v
>= 2 && v
<= 4) ? v
: 4;
2916 if (ctx
->head
.version
< 2 || ctx
->head
.version
> max_supported_dwarf_version
)
2918 WARN("DWARF version %d isn't supported. Wine dbghelp only supports DWARF 2 up to %lu.\n",
2919 ctx
->head
.version
, max_supported_dwarf_version
);
2923 pool_init(&ctx
->pool
, 65536);
2924 ctx
->section
= section_debug
;
2925 ctx
->ref_offset
= comp_unit_start
- ctx
->module_ctx
->sections
[section_debug
].address
;
2926 ctx
->cpp_name
= NULL
;
2927 ctx
->status
= UNIT_NOTLOADED
;
2929 abbrev_ctx
.data
= ctx
->module_ctx
->sections
[section_abbrev
].address
+ cu_abbrev_offset
;
2930 abbrev_ctx
.end_data
= ctx
->module_ctx
->sections
[section_abbrev
].address
+ ctx
->module_ctx
->sections
[section_abbrev
].size
;
2931 dwarf2_parse_abbrev_set(&abbrev_ctx
, &ctx
->abbrev_table
, &ctx
->pool
);
2933 sparse_array_init(&ctx
->debug_info_table
, sizeof(dwarf2_debug_info_t
), 128);
2937 static BOOL
dwarf2_parse_compilation_unit(dwarf2_parse_context_t
* ctx
)
2939 dwarf2_debug_info_t
* di
;
2940 dwarf2_traverse_context_t cu_ctx
= ctx
->traverse_DIE
;
2943 switch (ctx
->status
)
2945 case UNIT_ERROR
: return FALSE
;
2946 case UNIT_BEINGLOADED
:
2947 FIXME("Circular deps on CU\n");
2950 case UNIT_LOADED_FAIL
:
2952 case UNIT_NOTLOADED
: break;
2955 ctx
->status
= UNIT_BEINGLOADED
;
2956 if (dwarf2_read_one_debug_info(ctx
, &cu_ctx
, NULL
, &di
))
2958 if (di
->abbrev
->tag
== DW_TAG_compile_unit
|| di
->abbrev
->tag
== DW_TAG_partial_unit
)
2960 struct attribute name
;
2961 struct vector
* children
;
2962 dwarf2_debug_info_t
* child
= NULL
;
2964 struct attribute stmt_list
, low_pc
;
2965 struct attribute comp_dir
;
2966 struct attribute language
;
2968 if (!dwarf2_find_attribute(di
, DW_AT_name
, &name
))
2969 name
.u
.string
= NULL
;
2971 /* get working directory of current compilation unit */
2972 if (!dwarf2_find_attribute(di
, DW_AT_comp_dir
, &comp_dir
))
2973 comp_dir
.u
.string
= NULL
;
2975 if (!dwarf2_find_attribute(di
, DW_AT_low_pc
, &low_pc
))
2976 low_pc
.u
.uvalue
= 0;
2978 if (!dwarf2_find_attribute(di
, DW_AT_language
, &language
))
2979 language
.u
.uvalue
= DW_LANG_C
;
2981 ctx
->language
= language
.u
.uvalue
;
2983 ctx
->compiland
= symt_new_compiland(ctx
->module_ctx
->module
, ctx
->module_ctx
->load_offset
+ low_pc
.u
.uvalue
,
2984 source_new(ctx
->module_ctx
->module
, comp_dir
.u
.string
, name
.u
.string
));
2985 dwarf2_cache_cuhead(ctx
->module_ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
, ctx
->compiland
, &ctx
->head
);
2986 di
->symt
= &ctx
->compiland
->symt
;
2987 children
= dwarf2_get_di_children(di
);
2988 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2990 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2991 dwarf2_load_one_entry(child
);
2993 if (dwarf2_find_attribute(di
, DW_AT_stmt_list
, &stmt_list
))
2995 if (dwarf2_parse_line_numbers(ctx
, comp_dir
.u
.string
, stmt_list
.u
.uvalue
))
2996 ctx
->module_ctx
->module
->module
.LineNumbers
= TRUE
;
2998 ctx
->status
= UNIT_LOADED
;
3001 else FIXME("Should have a compilation unit here %Iu\n", di
->abbrev
->tag
);
3003 if (ctx
->status
== UNIT_BEINGLOADED
) ctx
->status
= UNIT_LOADED_FAIL
;
3007 static BOOL
dwarf2_lookup_loclist(const struct module_format
* modfmt
, const dwarf2_cuhead_t
* head
,
3008 const BYTE
* start
, ULONG_PTR ip
, dwarf2_traverse_context_t
* lctx
)
3011 const BYTE
* ptr
= start
;
3014 while (ptr
< modfmt
->u
.dwarf2_info
->debug_loc
.address
+ modfmt
->u
.dwarf2_info
->debug_loc
.size
)
3016 beg
= dwarf2_get_addr(ptr
, head
->word_size
); ptr
+= head
->word_size
;
3017 end
= dwarf2_get_addr(ptr
, head
->word_size
); ptr
+= head
->word_size
;
3018 if (!beg
&& !end
) break;
3019 len
= dwarf2_get_u2(ptr
); ptr
+= 2;
3021 if (beg
<= ip
&& ip
< end
)
3024 lctx
->end_data
= ptr
+ len
;
3029 WARN("Couldn't find ip in location list\n");
3033 static const dwarf2_cuhead_t
* get_cuhead_from_func(const struct symt_function
* func
)
3035 if (symt_check_tag(&func
->symt
, SymTagInlineSite
))
3036 func
= symt_get_function_from_inlined((struct symt_inlinesite
*)func
);
3037 if (symt_check_tag(&func
->symt
, SymTagFunction
) && symt_check_tag(func
->container
, SymTagCompiland
))
3039 struct symt_compiland
* c
= (struct symt_compiland
*)func
->container
;
3040 return (const dwarf2_cuhead_t
*)c
->user
;
3042 FIXME("Should have a compilation unit head\n");
3046 static BOOL
compute_call_frame_cfa(struct module
* module
, ULONG_PTR ip
, struct location
* frame
);
3048 static enum location_error
loc_compute_frame(struct process
* pcs
,
3049 const struct module_format
* modfmt
,
3050 const struct symt_function
* func
,
3051 DWORD_PTR ip
, const dwarf2_cuhead_t
* head
,
3052 struct location
* frame
)
3054 struct symt
** psym
= NULL
;
3055 struct location
* pframe
;
3056 dwarf2_traverse_context_t lctx
;
3057 enum location_error err
;
3060 for (i
=0; i
<vector_length(&func
->vchildren
); i
++)
3062 psym
= vector_at(&func
->vchildren
, i
);
3063 if (psym
&& symt_check_tag(*psym
, SymTagCustom
))
3065 pframe
= &((struct symt_hierarchy_point
*)*psym
)->loc
;
3067 /* First, recompute the frame information, if needed */
3068 switch (pframe
->kind
)
3074 case loc_dwarf2_location_list
:
3075 WARN("Searching loclist for %s\n", debugstr_a(func
->hash_elt
.name
));
3076 if (!dwarf2_lookup_loclist(modfmt
, head
,
3077 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ pframe
->offset
,
3079 return loc_err_out_of_scope
;
3080 if ((err
= compute_location(modfmt
->module
, head
,
3081 &lctx
, frame
, pcs
->handle
, NULL
)) < 0) return err
;
3082 if (frame
->kind
>= loc_user
)
3084 WARN("Couldn't compute runtime frame location\n");
3085 return loc_err_too_complex
;
3088 case loc_dwarf2_frame_cfa
:
3089 if (!compute_call_frame_cfa(modfmt
->module
, ip
+ ((struct symt_compiland
*)func
->container
)->address
, frame
)) return loc_err_internal
;
3092 WARN("Unsupported frame kind %d\n", pframe
->kind
);
3093 return loc_err_internal
;
3098 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
3099 return loc_err_internal
;
3104 RULE_UNSET
, /* not set at all */
3105 RULE_UNDEFINED
, /* undefined value */
3106 RULE_SAME
, /* same value as previous frame */
3107 RULE_CFA_OFFSET
, /* stored at cfa offset */
3108 RULE_OTHER_REG
, /* stored in other register */
3109 RULE_EXPRESSION
, /* address specified by expression */
3110 RULE_VAL_EXPRESSION
/* value specified by expression */
3113 /* make it large enough for all CPUs */
3114 #define NB_FRAME_REGS 64
3115 #define MAX_SAVED_STATES 16
3119 ULONG_PTR cfa_offset
;
3120 unsigned char cfa_reg
;
3121 enum reg_rule cfa_rule
;
3122 enum reg_rule rules
[NB_FRAME_REGS
];
3123 ULONG_PTR regs
[NB_FRAME_REGS
];
3129 ULONG_PTR code_align
;
3130 LONG_PTR data_align
;
3131 unsigned char retaddr_reg
;
3132 unsigned char fde_encoding
;
3133 unsigned char lsda_encoding
;
3134 unsigned char signal_frame
;
3135 unsigned char aug_z_format
;
3136 unsigned char state_sp
;
3137 struct frame_state state
;
3138 struct frame_state state_stack
[MAX_SAVED_STATES
];
3141 static ULONG_PTR
dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t
* ctx
, unsigned char encoding
, unsigned char word_size
)
3145 if (encoding
== DW_EH_PE_omit
) return 0;
3147 switch (encoding
& 0xf0)
3152 case DW_EH_PE_pcrel
:
3153 base
= (ULONG_PTR
)ctx
->data
;
3156 FIXME("unsupported encoding %02x\n", encoding
);
3160 switch (encoding
& 0x0f)
3162 case DW_EH_PE_native
:
3163 return base
+ dwarf2_parse_addr(ctx
, word_size
);
3164 case DW_EH_PE_leb128
:
3165 return base
+ dwarf2_leb128_as_unsigned(ctx
);
3166 case DW_EH_PE_data2
:
3167 return base
+ dwarf2_parse_u2(ctx
);
3168 case DW_EH_PE_data4
:
3169 return base
+ dwarf2_parse_u4(ctx
);
3170 case DW_EH_PE_data8
:
3171 return base
+ dwarf2_parse_u8(ctx
);
3172 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
3173 return base
+ dwarf2_leb128_as_signed(ctx
);
3174 case DW_EH_PE_signed
|DW_EH_PE_data2
:
3175 return base
+ (signed short)dwarf2_parse_u2(ctx
);
3176 case DW_EH_PE_signed
|DW_EH_PE_data4
:
3177 return base
+ (signed int)dwarf2_parse_u4(ctx
);
3178 case DW_EH_PE_signed
|DW_EH_PE_data8
:
3179 return base
+ (LONG64
)dwarf2_parse_u8(ctx
);
3181 FIXME("unsupported encoding %02x\n", encoding
);
3186 static BOOL
parse_cie_details(dwarf2_traverse_context_t
* ctx
, struct frame_info
* info
, unsigned char word_size
)
3188 unsigned char version
;
3189 const char* augmentation
;
3190 const unsigned char* end
;
3193 memset(info
, 0, sizeof(*info
));
3194 info
->lsda_encoding
= DW_EH_PE_omit
;
3195 info
->aug_z_format
= 0;
3197 /* parse the CIE first */
3198 version
= dwarf2_parse_byte(ctx
);
3199 if (version
!= 1 && version
!= 3 && version
!= 4)
3201 FIXME("unknown CIE version %u at %p\n", version
, ctx
->data
- 1);
3204 augmentation
= (const char*)ctx
->data
;
3205 ctx
->data
+= strlen(augmentation
) + 1;
3210 /* skip 'address_size' and 'segment_size' */
3215 info
->code_align
= dwarf2_leb128_as_unsigned(ctx
);
3216 info
->data_align
= dwarf2_leb128_as_signed(ctx
);
3217 info
->retaddr_reg
= version
== 1 ? dwarf2_parse_byte(ctx
) :dwarf2_leb128_as_unsigned(ctx
);
3222 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
3225 TRACE("\tparsing augmentation %s\n", debugstr_a(augmentation
));
3226 if (*augmentation
) do
3228 switch (*augmentation
)
3231 len
= dwarf2_leb128_as_unsigned(ctx
);
3232 end
= ctx
->data
+ len
;
3233 info
->aug_z_format
= 1;
3236 info
->lsda_encoding
= dwarf2_parse_byte(ctx
);
3240 unsigned char encoding
= dwarf2_parse_byte(ctx
);
3241 /* throw away the indirect bit, as we don't care for the result */
3242 encoding
&= ~DW_EH_PE_indirect
;
3243 dwarf2_parse_augmentation_ptr(ctx
, encoding
, word_size
); /* handler */
3247 info
->fde_encoding
= dwarf2_parse_byte(ctx
);
3250 info
->signal_frame
= 1;
3253 FIXME("unknown augmentation '%c'\n", *augmentation
);
3254 if (!end
) return FALSE
;
3256 } while (*++augmentation
);
3257 if (end
) ctx
->data
= end
;
3261 static BOOL
dwarf2_get_cie(ULONG_PTR addr
, struct module
* module
, DWORD_PTR delta
,
3262 dwarf2_traverse_context_t
* fde_ctx
, dwarf2_traverse_context_t
* cie_ctx
,
3263 struct frame_info
* info
, BOOL in_eh_frame
)
3265 const unsigned char* ptr_blk
;
3266 const unsigned char* cie_ptr
;
3267 const unsigned char* last_cie_ptr
= (const unsigned char*)~0;
3269 ULONG_PTR start
, range
;
3271 const BYTE
* start_data
= fde_ctx
->data
;
3272 unsigned char word_size
= module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
3273 unsigned char offset_size
;
3275 /* skip 0-padding at beginning of section (alignment) */
3276 while (fde_ctx
->data
+ 2 * 4 < fde_ctx
->end_data
)
3278 if (dwarf2_parse_u4(fde_ctx
))
3284 for (; fde_ctx
->data
+ 2 * 4 < fde_ctx
->end_data
; fde_ctx
->data
= ptr_blk
)
3286 const unsigned char* st
= fde_ctx
->data
;
3287 /* find the FDE for address addr (skip CIE) */
3288 len
= dwarf2_parse_3264(fde_ctx
, &offset_size
);
3289 cie_id
= in_eh_frame
? 0 : (offset_size
== 4 ? DW_CIE_ID
: (ULONG_PTR
)DW64_CIE_ID
);
3290 ptr_blk
= fde_ctx
->data
+ len
;
3291 id
= dwarf2_parse_offset(fde_ctx
, offset_size
);
3295 /* we need some bits out of the CIE in order to parse all contents */
3296 if (!parse_cie_details(fde_ctx
, info
, word_size
)) return FALSE
;
3297 cie_ctx
->data
= fde_ctx
->data
;
3298 cie_ctx
->end_data
= ptr_blk
;
3301 cie_ptr
= (in_eh_frame
) ? fde_ctx
->data
- id
- 4 : start_data
+ id
;
3302 if (cie_ptr
!= last_cie_ptr
)
3304 last_cie_ptr
= cie_ptr
;
3305 cie_ctx
->data
= cie_ptr
;
3306 cie_ctx
->end_data
= cie_ptr
+ (offset_size
== 4 ? 4 : 4 + 8);
3307 cie_ctx
->end_data
+= dwarf2_parse_3264(cie_ctx
, &offset_size
);
3309 if (dwarf2_parse_offset(cie_ctx
, in_eh_frame
? word_size
: offset_size
) != cie_id
)
3311 FIXME("wrong CIE pointer at %x from FDE %x\n",
3312 (unsigned)(cie_ptr
- start_data
),
3313 (unsigned)(fde_ctx
->data
- start_data
));
3316 if (!parse_cie_details(cie_ctx
, info
, word_size
)) return FALSE
;
3318 start
= delta
+ dwarf2_parse_augmentation_ptr(fde_ctx
, info
->fde_encoding
, word_size
);
3319 range
= dwarf2_parse_augmentation_ptr(fde_ctx
, info
->fde_encoding
& 0x0F, word_size
);
3321 if (addr
>= start
&& addr
< start
+ range
)
3323 /* reset the FDE context */
3324 fde_ctx
->end_data
= ptr_blk
;
3333 static int valid_reg(ULONG_PTR reg
)
3335 if (reg
>= NB_FRAME_REGS
) FIXME("unsupported reg %Ix\n", reg
);
3336 return (reg
< NB_FRAME_REGS
);
3339 static void execute_cfa_instructions(struct module
* module
, dwarf2_traverse_context_t
* ctx
,
3340 ULONG_PTR last_ip
, struct frame_info
*info
)
3342 while (ctx
->data
< ctx
->end_data
&& info
->ip
<= last_ip
+ info
->signal_frame
)
3344 enum dwarf_call_frame_info op
= dwarf2_parse_byte(ctx
);
3350 case DW_CFA_advance_loc
:
3352 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
3353 TRACE("%Ix: DW_CFA_advance_loc %Iu\n", info
->ip
, offset
);
3359 ULONG_PTR reg
= op
& 0x3f;
3360 LONG_PTR offset
= dwarf2_leb128_as_unsigned(ctx
) * info
->data_align
;
3361 if (!valid_reg(reg
)) break;
3362 TRACE("%Ix: DW_CFA_offset %s, %Id\n",
3364 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3366 info
->state
.regs
[reg
] = offset
;
3367 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
3370 case DW_CFA_restore
:
3372 ULONG_PTR reg
= op
& 0x3f;
3373 if (!valid_reg(reg
)) break;
3374 TRACE("%Ix: DW_CFA_restore %s\n",
3376 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3377 info
->state
.rules
[reg
] = RULE_UNSET
;
3386 case DW_CFA_set_loc
:
3388 ULONG_PTR loc
= dwarf2_parse_augmentation_ptr(ctx
, info
->fde_encoding
,
3389 module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
);
3390 TRACE("%Ix: DW_CFA_set_loc %Ix\n", info
->ip
, loc
);
3394 case DW_CFA_advance_loc1
:
3396 ULONG_PTR offset
= dwarf2_parse_byte(ctx
) * info
->code_align
;
3397 TRACE("%Ix: DW_CFA_advance_loc1 %Iu\n", info
->ip
, offset
);
3401 case DW_CFA_advance_loc2
:
3403 ULONG_PTR offset
= dwarf2_parse_u2(ctx
) * info
->code_align
;
3404 TRACE("%Ix: DW_CFA_advance_loc2 %Iu\n", info
->ip
, offset
);
3408 case DW_CFA_advance_loc4
:
3410 ULONG_PTR offset
= dwarf2_parse_u4(ctx
) * info
->code_align
;
3411 TRACE("%Ix: DW_CFA_advance_loc4 %Iu\n", info
->ip
, offset
);
3415 case DW_CFA_offset_extended
:
3416 case DW_CFA_offset_extended_sf
:
3418 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3419 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf2_leb128_as_unsigned(ctx
) * info
->data_align
3420 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
3421 if (!valid_reg(reg
)) break;
3422 TRACE("%Ix: DW_CFA_offset_extended %s, %Id\n",
3424 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3426 info
->state
.regs
[reg
] = offset
;
3427 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
3430 case DW_CFA_restore_extended
:
3432 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3433 if (!valid_reg(reg
)) break;
3434 TRACE("%Ix: DW_CFA_restore_extended %s\n",
3436 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3437 info
->state
.rules
[reg
] = RULE_UNSET
;
3440 case DW_CFA_undefined
:
3442 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3443 if (!valid_reg(reg
)) break;
3444 TRACE("%Ix: DW_CFA_undefined %s\n",
3446 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3447 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
3450 case DW_CFA_same_value
:
3452 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3453 if (!valid_reg(reg
)) break;
3454 TRACE("%Ix: DW_CFA_same_value %s\n",
3456 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3457 info
->state
.regs
[reg
] = reg
;
3458 info
->state
.rules
[reg
] = RULE_SAME
;
3461 case DW_CFA_register
:
3463 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3464 ULONG_PTR reg2
= dwarf2_leb128_as_unsigned(ctx
);
3465 if (!valid_reg(reg
) || !valid_reg(reg2
)) break;
3466 TRACE("%Ix: DW_CFA_register %s == %s\n",
3468 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3469 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg2
, module
, TRUE
)));
3470 info
->state
.regs
[reg
] = reg2
;
3471 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
3474 case DW_CFA_remember_state
:
3475 TRACE("%Ix: DW_CFA_remember_state\n", info
->ip
);
3476 if (info
->state_sp
>= MAX_SAVED_STATES
)
3477 FIXME("%Ix: DW_CFA_remember_state too many nested saves\n", info
->ip
);
3479 info
->state_stack
[info
->state_sp
++] = info
->state
;
3481 case DW_CFA_restore_state
:
3482 TRACE("%Ix: DW_CFA_restore_state\n", info
->ip
);
3483 if (!info
->state_sp
)
3484 FIXME("%Ix: DW_CFA_restore_state without corresponding save\n", info
->ip
);
3486 info
->state
= info
->state_stack
[--info
->state_sp
];
3488 case DW_CFA_def_cfa
:
3489 case DW_CFA_def_cfa_sf
:
3491 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3492 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf2_leb128_as_unsigned(ctx
)
3493 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
3494 if (!valid_reg(reg
)) break;
3495 TRACE("%Ix: DW_CFA_def_cfa %s, %Id\n",
3497 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3499 info
->state
.cfa_reg
= reg
;
3500 info
->state
.cfa_offset
= offset
;
3501 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
3504 case DW_CFA_def_cfa_register
:
3506 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3507 if (!valid_reg(reg
)) break;
3508 TRACE("%Ix: DW_CFA_def_cfa_register %s\n",
3510 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)));
3511 info
->state
.cfa_reg
= reg
;
3512 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
3515 case DW_CFA_def_cfa_offset
:
3516 case DW_CFA_def_cfa_offset_sf
:
3518 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf2_leb128_as_unsigned(ctx
)
3519 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
3520 TRACE("%Ix: DW_CFA_def_cfa_offset %Id\n", info
->ip
, offset
);
3521 info
->state
.cfa_offset
= offset
;
3522 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
3525 case DW_CFA_def_cfa_expression
:
3527 ULONG_PTR expr
= (ULONG_PTR
)ctx
->data
;
3528 ULONG_PTR len
= dwarf2_leb128_as_unsigned(ctx
);
3529 TRACE("%Ix: DW_CFA_def_cfa_expression %Ix-%Ix\n", info
->ip
, expr
, expr
+len
);
3530 info
->state
.cfa_offset
= expr
;
3531 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
3535 case DW_CFA_expression
:
3536 case DW_CFA_val_expression
:
3538 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3539 ULONG_PTR expr
= (ULONG_PTR
)ctx
->data
;
3540 ULONG_PTR len
= dwarf2_leb128_as_unsigned(ctx
);
3541 if (!valid_reg(reg
)) break;
3542 TRACE("%Ix: DW_CFA_%sexpression %s %Ix-%Ix\n",
3543 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_",
3544 module
->cpu
->fetch_regname(module
->cpu
->map_dwarf_register(reg
, module
, TRUE
)),
3546 info
->state
.regs
[reg
] = expr
;
3547 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
3551 case DW_CFA_GNU_args_size
:
3552 /* FIXME: should check that GCC is the compiler for this CU */
3554 ULONG_PTR args
= dwarf2_leb128_as_unsigned(ctx
);
3555 TRACE("%Ix: DW_CFA_GNU_args_size %Iu\n", info
->ip
, args
);
3560 FIXME("%Ix: unknown CFA opcode %02x\n", info
->ip
, op
);
3566 /* retrieve a context register from its dwarf number */
3567 static DWORD64
get_context_reg(const struct module
* module
, struct cpu_stack_walk
*csw
, union ctx
*context
,
3570 unsigned regno
= csw
->cpu
->map_dwarf_register(dw_reg
, module
, TRUE
), sz
;
3571 void* ptr
= csw
->cpu
->fetch_context_reg(context
, regno
, &sz
);
3573 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3575 return *(DWORD64
*)ptr
;
3577 return *(DWORD
*)ptr
;
3579 FIXME("unhandled size %d\n", sz
);
3583 /* set a context register from its dwarf number */
3584 static void set_context_reg(const struct module
* module
, struct cpu_stack_walk
* csw
, union ctx
*context
,
3585 ULONG_PTR dw_reg
, ULONG_PTR val
, BOOL isdebuggee
)
3587 unsigned regno
= csw
->cpu
->map_dwarf_register(dw_reg
, module
, TRUE
), sz
;
3588 ULONG_PTR
* ptr
= csw
->cpu
->fetch_context_reg(context
, regno
, &sz
);
3590 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3595 if (sz
> sizeof(tmp
))
3597 FIXME("register %Iu/%u size is too wide: %u\n", dw_reg
, regno
, sz
);
3600 if (!sw_read_mem(csw
, val
, tmp
, sz
))
3602 WARN("Couldn't read memory at %p\n", (void*)val
);
3605 memcpy(ptr
, tmp
, sz
);
3609 if (sz
!= sizeof(ULONG_PTR
))
3611 FIXME("assigning to register %Iu/%u of wrong size %u\n", dw_reg
, regno
, sz
);
3618 /* copy a register from one context to another using dwarf number */
3619 static void copy_context_reg(const struct module
* module
, struct cpu_stack_walk
*csw
,
3620 union ctx
*dstcontext
, ULONG_PTR dwregdst
,
3621 union ctx
*srccontext
, ULONG_PTR dwregsrc
)
3623 unsigned regdstno
= csw
->cpu
->map_dwarf_register(dwregdst
, module
, TRUE
), szdst
;
3624 unsigned regsrcno
= csw
->cpu
->map_dwarf_register(dwregsrc
, module
, TRUE
), szsrc
;
3625 ULONG_PTR
* ptrdst
= csw
->cpu
->fetch_context_reg(dstcontext
, regdstno
, &szdst
);
3626 ULONG_PTR
* ptrsrc
= csw
->cpu
->fetch_context_reg(srccontext
, regsrcno
, &szsrc
);
3628 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3631 FIXME("Cannot copy register %Iu/%u => %Iu/%u because of size mismatch (%u => %u)\n",
3632 dwregsrc
, regsrcno
, dwregdst
, regdstno
, szsrc
, szdst
);
3635 memcpy(ptrdst
, ptrsrc
, szdst
);
3638 static ULONG_PTR
eval_expression(const struct module
* module
, struct cpu_stack_walk
* csw
,
3639 const unsigned char* zp
, union ctx
*context
)
3641 dwarf2_traverse_context_t ctx
;
3642 ULONG_PTR reg
, sz
, tmp
;
3647 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3649 ctx
.end_data
= zp
+ 4;
3650 len
= dwarf2_leb128_as_unsigned(&ctx
);
3651 ctx
.end_data
= ctx
.data
+ len
;
3653 while (ctx
.data
< ctx
.end_data
)
3655 unsigned char opcode
= dwarf2_parse_byte(&ctx
);
3657 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
3658 stack
[++sp
] = opcode
- DW_OP_lit0
;
3659 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
3660 stack
[++sp
] = get_context_reg(module
, csw
, context
, opcode
- DW_OP_reg0
);
3661 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
3662 stack
[++sp
] = get_context_reg(module
, csw
, context
, opcode
- DW_OP_breg0
)
3663 + dwarf2_leb128_as_signed(&ctx
);
3664 else switch (opcode
)
3666 case DW_OP_nop
: break;
3667 case DW_OP_addr
: stack
[++sp
] = dwarf2_parse_addr(&ctx
, module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
); break;
3668 case DW_OP_const1u
: stack
[++sp
] = dwarf2_parse_byte(&ctx
); break;
3669 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf2_parse_byte(&ctx
); break;
3670 case DW_OP_const2u
: stack
[++sp
] = dwarf2_parse_u2(&ctx
); break;
3671 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf2_parse_u2(&ctx
); break;
3672 case DW_OP_const4u
: stack
[++sp
] = dwarf2_parse_u4(&ctx
); break;
3673 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf2_parse_u4(&ctx
); break;
3674 case DW_OP_const8u
: stack
[++sp
] = dwarf2_parse_u8(&ctx
); break;
3675 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf2_parse_u8(&ctx
); break;
3676 case DW_OP_constu
: stack
[++sp
] = dwarf2_leb128_as_unsigned(&ctx
); break;
3677 case DW_OP_consts
: stack
[++sp
] = dwarf2_leb128_as_signed(&ctx
); break;
3680 if (!sw_read_mem(csw
, stack
[sp
], &tmp
, module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
))
3682 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack
[sp
]));
3687 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
3688 case DW_OP_drop
: sp
--; break;
3689 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
3690 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf2_parse_byte(&ctx
)]; sp
++; break;
3691 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
3692 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
3693 case DW_OP_abs
: stack
[sp
] = sizeof(stack
[sp
]) == 8 ? llabs((INT64
)stack
[sp
]) : abs((INT32
)stack
[sp
]); break;
3694 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
3695 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
3696 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
3697 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
3698 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
3699 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
3700 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
3701 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
3702 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
3703 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
3704 case DW_OP_plus_uconst
: stack
[sp
] += dwarf2_leb128_as_unsigned(&ctx
); break;
3705 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
3706 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
3707 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
3708 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
3709 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
3710 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
3711 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
3712 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
3713 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
3714 case DW_OP_skip
: tmp
= (short)dwarf2_parse_u2(&ctx
); ctx
.data
+= tmp
; break;
3715 case DW_OP_bra
: tmp
= (short)dwarf2_parse_u2(&ctx
); if (!stack
[sp
--]) ctx
.data
+= tmp
; break;
3716 case DW_OP_GNU_encoded_addr
:
3717 tmp
= dwarf2_parse_byte(&ctx
);
3718 stack
[++sp
] = dwarf2_parse_augmentation_ptr(&ctx
, tmp
, module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
);
3721 stack
[++sp
] = get_context_reg(module
, csw
, context
, dwarf2_leb128_as_unsigned(&ctx
));
3724 reg
= dwarf2_leb128_as_unsigned(&ctx
);
3725 tmp
= dwarf2_leb128_as_signed(&ctx
);
3726 stack
[++sp
] = get_context_reg(module
, csw
, context
, reg
) + tmp
;
3728 case DW_OP_deref_size
:
3729 sz
= dwarf2_parse_byte(&ctx
);
3730 if (!sw_read_mem(csw
, stack
[sp
], &tmp
, sz
))
3732 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack
[sp
]));
3735 /* do integral promotion */
3738 case 1: stack
[sp
] = *(unsigned char*)&tmp
; break;
3739 case 2: stack
[sp
] = *(unsigned short*)&tmp
; break;
3740 case 4: stack
[sp
] = *(unsigned int*)&tmp
; break;
3741 case 8: stack
[sp
] = tmp
; break; /* FIXME: won't work on 32bit platform */
3742 default: FIXME("Unknown size for deref 0x%Ix\n", sz
);
3746 FIXME("unhandled opcode %02x\n", opcode
);
3752 static void apply_frame_state(const struct module
* module
, struct cpu_stack_walk
* csw
,
3753 union ctx
*context
, struct frame_state
*state
, DWORD64
*cfa
)
3757 union ctx new_context
= *context
;
3759 if (csw
->cpu
!= module
->cpu
) FIXME("mismatch in cpu\n");
3760 switch (state
->cfa_rule
)
3762 case RULE_EXPRESSION
:
3763 *cfa
= eval_expression(module
, csw
, (const unsigned char*)state
->cfa_offset
, context
);
3764 if (!sw_read_mem(csw
, *cfa
, cfa
, csw
->cpu
->word_size
))
3766 WARN("Couldn't read memory at %s\n", wine_dbgstr_longlong(*cfa
));
3770 case RULE_VAL_EXPRESSION
:
3771 *cfa
= eval_expression(module
, csw
, (const unsigned char*)state
->cfa_offset
, context
);
3774 *cfa
= get_context_reg(module
, csw
, context
, state
->cfa_reg
) + state
->cfa_offset
;
3779 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
3781 switch (state
->rules
[i
])
3784 case RULE_UNDEFINED
:
3787 case RULE_CFA_OFFSET
:
3788 set_context_reg(module
, csw
, &new_context
, i
, *cfa
+ state
->regs
[i
], TRUE
);
3790 case RULE_OTHER_REG
:
3791 copy_context_reg(module
, csw
, &new_context
, i
, context
, state
->regs
[i
]);
3793 case RULE_EXPRESSION
:
3794 value
= eval_expression(module
, csw
, (const unsigned char*)state
->regs
[i
], context
);
3795 set_context_reg(module
, csw
, &new_context
, i
, value
, TRUE
);
3797 case RULE_VAL_EXPRESSION
:
3798 value
= eval_expression(module
, csw
, (const unsigned char*)state
->regs
[i
], context
);
3799 set_context_reg(module
, csw
, &new_context
, i
, value
, FALSE
);
3803 *context
= new_context
;
3806 static BOOL
dwarf2_fetch_frame_info(struct module
* module
, struct cpu
* cpu
, LONG_PTR ip
, struct frame_info
* info
)
3808 dwarf2_traverse_context_t cie_ctx
, fde_ctx
;
3809 struct module_format
* modfmt
;
3810 const unsigned char* end
;
3813 modfmt
= module
->format_info
[DFI_DWARF
];
3814 if (!modfmt
) return FALSE
;
3815 memset(info
, 0, sizeof(*info
));
3816 fde_ctx
.data
= modfmt
->u
.dwarf2_info
->eh_frame
.address
;
3817 fde_ctx
.end_data
= fde_ctx
.data
+ modfmt
->u
.dwarf2_info
->eh_frame
.size
;
3818 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3819 * in this process the IMAGE section at a different address as the one expected by
3822 delta
= module
->module
.BaseOfImage
+ modfmt
->u
.dwarf2_info
->eh_frame
.rva
-
3823 (DWORD_PTR
)modfmt
->u
.dwarf2_info
->eh_frame
.address
;
3824 if (!dwarf2_get_cie(ip
, module
, delta
, &fde_ctx
, &cie_ctx
, info
, TRUE
))
3826 fde_ctx
.data
= modfmt
->u
.dwarf2_info
->debug_frame
.address
;
3827 fde_ctx
.end_data
= fde_ctx
.data
+ modfmt
->u
.dwarf2_info
->debug_frame
.size
;
3828 delta
= module
->reloc_delta
;
3829 if (modfmt
->u
.dwarf2_info
->debug_frame
.address
== IMAGE_NO_MAP
||
3830 !dwarf2_get_cie(ip
, module
, delta
, &fde_ctx
, &cie_ctx
, info
, FALSE
))
3832 TRACE("Couldn't find information for %Ix\n", ip
);
3837 TRACE("function %Ix/%Ix code_align %Iu data_align %Id retaddr %s\n",
3838 ip
, info
->ip
, info
->code_align
, info
->data_align
,
3839 cpu
->fetch_regname(cpu
->map_dwarf_register(info
->retaddr_reg
, module
, TRUE
)));
3843 execute_cfa_instructions(module
, &cie_ctx
, ip
, info
);
3845 if (info
->aug_z_format
) /* get length of augmentation data */
3847 ULONG_PTR len
= dwarf2_leb128_as_unsigned(&fde_ctx
);
3848 end
= fde_ctx
.data
+ len
;
3851 dwarf2_parse_augmentation_ptr(&fde_ctx
, info
->lsda_encoding
, modfmt
->u
.dwarf2_info
->word_size
); /* handler_data */
3852 if (end
) fde_ctx
.data
= end
;
3854 execute_cfa_instructions(module
, &fde_ctx
, ip
, info
);
3859 /***********************************************************************
3860 * dwarf2_virtual_unwind
3863 BOOL
dwarf2_virtual_unwind(struct cpu_stack_walk
*csw
, ULONG_PTR ip
,
3864 union ctx
*context
, DWORD64
*cfa
)
3866 struct module_pair pair
;
3867 struct frame_info info
;
3869 if (!module_init_pair(&pair
, csw
->hProcess
, ip
)) return FALSE
;
3870 if (csw
->cpu
!= pair
.effective
->cpu
) FIXME("mismatch in cpu\n");
3871 if (!dwarf2_fetch_frame_info(pair
.effective
, csw
->cpu
, ip
, &info
)) return FALSE
;
3873 /* if at very beginning of function, return and use default unwinder */
3874 if (ip
== info
.ip
) return FALSE
;
3876 /* if there is no information about retaddr, use default unwinder */
3877 if (info
.state
.rules
[info
.retaddr_reg
] == RULE_UNSET
) return FALSE
;
3879 apply_frame_state(pair
.effective
, csw
, context
, &info
.state
, cfa
);
3884 static BOOL
compute_call_frame_cfa(struct module
* module
, ULONG_PTR ip
, struct location
* frame
)
3886 struct frame_info info
;
3888 if (!dwarf2_fetch_frame_info(module
, module
->cpu
, ip
, &info
)) return FALSE
;
3890 /* beginning of function, or no available dwarf information ? */
3891 if (ip
== info
.ip
|| info
.state
.rules
[info
.retaddr_reg
] == RULE_UNSET
)
3893 /* fake the default unwinder */
3894 frame
->kind
= loc_regrel
;
3895 frame
->reg
= module
->cpu
->frame_regno
;
3896 frame
->offset
= module
->cpu
->word_size
; /* FIXME stack direction */
3900 /* we expect to translate the call_frame_cfa into a regrel location...
3901 * that should cover most of the cases
3903 switch (info
.state
.cfa_rule
)
3905 case RULE_EXPRESSION
:
3906 FIXME("Too complex expression for frame_CFA resolution (RULE_EXPRESSION)\n");
3908 case RULE_VAL_EXPRESSION
:
3909 FIXME("Too complex expression for frame_CFA resolution (RULE_VAL_EXPRESSION)\n");
3912 frame
->kind
= loc_regrel
;
3913 frame
->reg
= module
->cpu
->map_dwarf_register(info
.state
.cfa_reg
, module
, TRUE
);
3914 frame
->offset
= info
.state
.cfa_offset
;
3921 static void dwarf2_location_compute(struct process
* pcs
,
3922 const struct module_format
* modfmt
,
3923 const struct symt_function
* func
,
3924 struct location
* loc
)
3926 struct location frame
;
3929 dwarf2_traverse_context_t lctx
;
3930 const dwarf2_cuhead_t
* head
= get_cuhead_from_func(func
);
3934 WARN("We'd expect function %s's container to be a valid compiland with dwarf inforamation\n",
3935 debugstr_a(func
->hash_elt
.name
));
3936 err
= loc_err_internal
;
3940 /* instruction pointer relative to compiland's start */
3941 ip
= pcs
->localscope_pc
- ((struct symt_compiland
*)func
->container
)->address
;
3943 if ((err
= loc_compute_frame(pcs
, modfmt
, func
, ip
, head
, &frame
)) == 0)
3947 case loc_dwarf2_location_list
:
3948 /* Then, if the variable has a location list, find it !! */
3949 if (dwarf2_lookup_loclist(modfmt
, head
,
3950 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ loc
->offset
,
3953 err
= loc_err_out_of_scope
;
3955 case loc_dwarf2_block
:
3956 /* or if we have a copy of an existing block, get ready for it */
3958 unsigned* ptr
= (unsigned*)loc
->offset
;
3960 lctx
.data
= (const BYTE
*)(ptr
+ 1);
3961 lctx
.end_data
= lctx
.data
+ *ptr
;
3964 /* now get the variable */
3965 err
= compute_location(modfmt
->module
, head
,
3966 &lctx
, loc
, pcs
->handle
, &frame
);
3973 WARN("Unsupported local kind %d\n", loc
->kind
);
3974 err
= loc_err_internal
;
3980 loc
->kind
= loc_register
;
3985 static void *zalloc(void *priv
, uInt items
, uInt sz
)
3987 return HeapAlloc(GetProcessHeap(), 0, items
* sz
);
3990 static void zfree(void *priv
, void *addr
)
3992 HeapFree(GetProcessHeap(), 0, addr
);
3995 static inline BOOL
dwarf2_init_zsection(dwarf2_section_t
* section
,
3996 const char* zsectname
,
3997 struct image_section_map
* ism
)
4004 BYTE
*addr
, *sect
= (BYTE
*)image_map_section(ism
);
4005 size_t sz
= image_get_map_size(ism
);
4007 if (sz
<= 12 || memcmp(sect
, "ZLIB", 4))
4009 ERR("invalid compressed section %s\n", debugstr_a(zsectname
));
4013 #ifdef WORDS_BIGENDIAN
4014 li
.u
.HighPart
= *(DWORD
*)§
[4];
4015 li
.u
.LowPart
= *(DWORD
*)§
[8];
4017 li
.u
.HighPart
= RtlUlongByteSwap(*(DWORD
*)§
[4]);
4018 li
.u
.LowPart
= RtlUlongByteSwap(*(DWORD
*)§
[8]);
4021 addr
= HeapAlloc(GetProcessHeap(), 0, li
.QuadPart
);
4025 z
.next_in
= §
[12];
4026 z
.avail_in
= sz
- 12;
4031 res
= inflateInit(&z
);
4034 FIXME("inflateInit failed with %i / %s\n", res
, debugstr_a(z
.msg
));
4039 z
.next_out
= addr
+ z
.total_out
;
4040 z
.avail_out
= li
.QuadPart
- z
.total_out
;
4041 res
= inflate(&z
, Z_FINISH
);
4042 } while (z
.avail_in
&& res
== Z_STREAM_END
);
4044 if (res
!= Z_STREAM_END
)
4046 FIXME("Decompression failed with %i / %s\n", res
, debugstr_a(z
.msg
));
4051 section
->compressed
= TRUE
;
4052 section
->address
= addr
;
4053 section
->rva
= image_get_map_rva(ism
);
4054 section
->size
= z
.total_out
;
4060 HeapFree(GetProcessHeap(), 0, addr
);
4062 image_unmap_section(ism
);
4066 static inline BOOL
dwarf2_init_section(dwarf2_section_t
* section
, struct image_file_map
* fmap
,
4067 const char* sectname
, const char* zsectname
,
4068 struct image_section_map
* ism
)
4070 struct image_section_map local_ism
;
4072 if (!ism
) ism
= &local_ism
;
4074 section
->compressed
= FALSE
;
4075 if (image_find_section(fmap
, sectname
, ism
))
4077 section
->address
= (const BYTE
*)image_map_section(ism
);
4078 section
->size
= image_get_map_size(ism
);
4079 section
->rva
= image_get_map_rva(ism
);
4083 section
->address
= NULL
;
4087 if (zsectname
&& image_find_section(fmap
, zsectname
, ism
))
4089 return dwarf2_init_zsection(section
, zsectname
, ism
);
4095 static inline void dwarf2_fini_section(dwarf2_section_t
* section
)
4097 if (section
->compressed
)
4098 HeapFree(GetProcessHeap(), 0, (void*)section
->address
);
4101 static void dwarf2_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
4103 dwarf2_fini_section(&modfmt
->u
.dwarf2_info
->debug_loc
);
4104 dwarf2_fini_section(&modfmt
->u
.dwarf2_info
->debug_frame
);
4105 free(modfmt
->u
.dwarf2_info
->cuheads
);
4106 HeapFree(GetProcessHeap(), 0, modfmt
);
4109 static BOOL
dwarf2_load_CU_module(dwarf2_parse_module_context_t
* module_ctx
, struct module
* module
,
4110 dwarf2_section_t
* sections
, ULONG_PTR load_offset
,
4111 const struct elf_thunk_area
* thunks
, BOOL is_dwz
)
4113 dwarf2_traverse_context_t mod_ctx
;
4116 module_ctx
->sections
= sections
;
4117 module_ctx
->module
= module
;
4118 module_ctx
->thunks
= thunks
;
4119 module_ctx
->load_offset
= load_offset
;
4120 vector_init(&module_ctx
->unit_contexts
, sizeof(dwarf2_parse_context_t
), 16);
4121 module_ctx
->cu_versions
= 0;
4123 /* phase I: parse all CU heads */
4124 mod_ctx
.data
= sections
[section_debug
].address
;
4125 mod_ctx
.end_data
= mod_ctx
.data
+ sections
[section_debug
].size
;
4126 while (mod_ctx
.data
< mod_ctx
.end_data
)
4128 dwarf2_parse_context_t
* unit_ctx
= vector_add(&module_ctx
->unit_contexts
, &module_ctx
->module
->pool
);
4130 unit_ctx
->module_ctx
= module_ctx
;
4131 dwarf2_parse_compilation_unit_head(unit_ctx
, &mod_ctx
);
4134 /* phase2: load content of all CU
4135 * If this is a DWZ alternate module, don't load all debug_info at once
4136 * wait for main module to ask for them (it's likely it won't need them all)
4137 * Doing this can lead to a huge performance improvement.
4140 for (i
= 0; i
< module_ctx
->unit_contexts
.num_elts
; ++i
)
4141 dwarf2_parse_compilation_unit((dwarf2_parse_context_t
*)vector_at(&module_ctx
->unit_contexts
, i
));
4146 static dwarf2_dwz_alternate_t
* dwarf2_load_dwz(struct image_file_map
* fmap
, struct module
* module
)
4148 struct image_file_map
* fmap_dwz
;
4149 dwarf2_dwz_alternate_t
* dwz
;
4151 fmap_dwz
= image_load_debugaltlink(fmap
, module
);
4152 if (!fmap_dwz
) return NULL
;
4153 if (!(dwz
= HeapAlloc(GetProcessHeap(), 0, sizeof(*dwz
))))
4155 image_unmap_file(fmap_dwz
);
4156 HeapFree(GetProcessHeap(), 0, fmap_dwz
);
4160 dwz
->fmap
= fmap_dwz
;
4161 dwarf2_init_section(&dwz
->sections
[section_debug
], fmap_dwz
, ".debug_info", ".zdebug_info", &dwz
->sectmap
[section_debug
]);
4162 dwarf2_init_section(&dwz
->sections
[section_abbrev
], fmap_dwz
, ".debug_abbrev", ".zdebug_abbrev", &dwz
->sectmap
[section_abbrev
]);
4163 dwarf2_init_section(&dwz
->sections
[section_string
], fmap_dwz
, ".debug_str", ".zdebug_str", &dwz
->sectmap
[section_string
]);
4164 dwarf2_init_section(&dwz
->sections
[section_line
], fmap_dwz
, ".debug_line", ".zdebug_line", &dwz
->sectmap
[section_line
]);
4165 dwarf2_init_section(&dwz
->sections
[section_ranges
], fmap_dwz
, ".debug_ranges", ".zdebug_ranges", &dwz
->sectmap
[section_ranges
]);
4167 dwz
->module_ctx
.dwz
= NULL
;
4168 dwarf2_load_CU_module(&dwz
->module_ctx
, module
, dwz
->sections
, 0/*FIXME*/, NULL
, TRUE
);
4172 static void dwarf2_unload_dwz(dwarf2_dwz_alternate_t
* dwz
)
4175 dwarf2_fini_section(&dwz
->sections
[section_debug
]);
4176 dwarf2_fini_section(&dwz
->sections
[section_abbrev
]);
4177 dwarf2_fini_section(&dwz
->sections
[section_string
]);
4178 dwarf2_fini_section(&dwz
->sections
[section_line
]);
4179 dwarf2_fini_section(&dwz
->sections
[section_ranges
]);
4181 image_unmap_section(&dwz
->sectmap
[section_debug
]);
4182 image_unmap_section(&dwz
->sectmap
[section_abbrev
]);
4183 image_unmap_section(&dwz
->sectmap
[section_string
]);
4184 image_unmap_section(&dwz
->sectmap
[section_line
]);
4185 image_unmap_section(&dwz
->sectmap
[section_ranges
]);
4187 HeapFree(GetProcessHeap(), 0, dwz
);
4190 static BOOL
dwarf2_unload_CU_module(dwarf2_parse_module_context_t
* module_ctx
)
4193 for (i
= 0; i
< module_ctx
->unit_contexts
.num_elts
; ++i
)
4195 dwarf2_parse_context_t
* unit
= vector_at(&module_ctx
->unit_contexts
, i
);
4196 if (unit
->status
!= UNIT_ERROR
)
4197 pool_destroy(&unit
->pool
);
4199 dwarf2_unload_dwz(module_ctx
->dwz
);
4203 BOOL
dwarf2_parse(struct module
* module
, ULONG_PTR load_offset
,
4204 const struct elf_thunk_area
* thunks
,
4205 struct image_file_map
* fmap
)
4207 dwarf2_section_t eh_frame
, section
[section_max
];
4208 struct image_section_map debug_sect
, debug_str_sect
, debug_abbrev_sect
,
4209 debug_line_sect
, debug_ranges_sect
, eh_frame_sect
;
4211 struct module_format
* dwarf2_modfmt
;
4212 dwarf2_parse_module_context_t module_ctx
;
4214 if (!dwarf2_init_section(&eh_frame
, fmap
, ".eh_frame", NULL
, &eh_frame_sect
))
4215 /* lld produces .eh_fram to avoid generating a long name */
4216 dwarf2_init_section(&eh_frame
, fmap
, ".eh_fram", NULL
, &eh_frame_sect
);
4217 dwarf2_init_section(§ion
[section_debug
], fmap
, ".debug_info", ".zdebug_info", &debug_sect
);
4218 dwarf2_init_section(§ion
[section_abbrev
], fmap
, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect
);
4219 dwarf2_init_section(§ion
[section_string
], fmap
, ".debug_str", ".zdebug_str", &debug_str_sect
);
4220 dwarf2_init_section(§ion
[section_line
], fmap
, ".debug_line", ".zdebug_line", &debug_line_sect
);
4221 dwarf2_init_section(§ion
[section_ranges
], fmap
, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect
);
4223 /* to do anything useful we need either .eh_frame or .debug_info */
4224 if ((!eh_frame
.address
|| eh_frame
.address
== IMAGE_NO_MAP
) &&
4225 (!section
[section_debug
].address
|| section
[section_debug
].address
== IMAGE_NO_MAP
))
4231 if (fmap
->modtype
== DMT_ELF
&& debug_sect
.fmap
)
4233 /* debug info might have a different base address than .so file
4234 * when elf file is prelinked after splitting off debug info
4235 * adjust symbol base addresses accordingly
4237 load_offset
+= fmap
->u
.elf
.elf_start
- debug_sect
.fmap
->u
.elf
.elf_start
;
4240 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module
->modulename
));
4242 dwarf2_modfmt
= HeapAlloc(GetProcessHeap(), 0,
4243 sizeof(*dwarf2_modfmt
) + sizeof(*dwarf2_modfmt
->u
.dwarf2_info
));
4249 dwarf2_modfmt
->module
= module
;
4250 dwarf2_modfmt
->remove
= dwarf2_module_remove
;
4251 dwarf2_modfmt
->loc_compute
= dwarf2_location_compute
;
4252 dwarf2_modfmt
->u
.dwarf2_info
= (struct dwarf2_module_info_s
*)(dwarf2_modfmt
+ 1);
4253 dwarf2_modfmt
->u
.dwarf2_info
->word_size
= fmap
->addr_size
/ 8; /* set the word_size for eh_frame parsing */
4254 dwarf2_modfmt
->module
->format_info
[DFI_DWARF
] = dwarf2_modfmt
;
4256 /* As we'll need later some sections' content, we won't unmap these
4257 * sections upon existing this function
4259 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_loc
, fmap
, ".debug_loc", ".zdebug_loc", NULL
);
4260 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_frame
, fmap
, ".debug_frame", ".zdebug_frame", NULL
);
4261 dwarf2_modfmt
->u
.dwarf2_info
->eh_frame
= eh_frame
;
4262 dwarf2_modfmt
->u
.dwarf2_info
->cuheads
= NULL
;
4263 dwarf2_modfmt
->u
.dwarf2_info
->num_cuheads
= 0;
4265 module_ctx
.dwz
= dwarf2_load_dwz(fmap
, module
);
4266 dwarf2_load_CU_module(&module_ctx
, module
, section
, load_offset
, thunks
, FALSE
);
4268 dwarf2_modfmt
->module
->module
.SymType
= SymDia
;
4269 /* hide dwarf versions in CVSig
4270 * bits 24-31 will be set according to found dwarf version
4271 * different CU can have different dwarf version, so use a bit per version (version 2 => b24)
4273 dwarf2_modfmt
->module
->module
.CVSig
= 'D' | ('W' << 8) | ('F' << 16) | ((module_ctx
.cu_versions
& 0xFF) << 24);
4274 /* FIXME: we could have a finer grain here */
4275 dwarf2_modfmt
->module
->module
.GlobalSymbols
= TRUE
;
4276 dwarf2_modfmt
->module
->module
.TypeInfo
= TRUE
;
4277 dwarf2_modfmt
->module
->module
.SourceIndexed
= TRUE
;
4278 dwarf2_modfmt
->module
->module
.Publics
= TRUE
;
4280 dwarf2_unload_CU_module(&module_ctx
);
4283 dwarf2_fini_section(§ion
[section_debug
]);
4284 dwarf2_fini_section(§ion
[section_abbrev
]);
4285 dwarf2_fini_section(§ion
[section_string
]);
4286 dwarf2_fini_section(§ion
[section_line
]);
4287 dwarf2_fini_section(§ion
[section_ranges
]);
4289 image_unmap_section(&debug_sect
);
4290 image_unmap_section(&debug_abbrev_sect
);
4291 image_unmap_section(&debug_str_sect
);
4292 image_unmap_section(&debug_line_sect
);
4293 image_unmap_section(&debug_ranges_sect
);
4294 if (!ret
) image_unmap_section(&eh_frame_sect
);