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 #ifndef DBGHELP_STATIC_LIB
25 #define NONAMELESSUNION
29 #include <sys/types.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
34 #ifdef HAVE_SYS_MMAN_H
58 #include "dbghelp_private.h"
59 #include "image_private.h"
61 #include "wine/debug.h"
64 #include "dbghelp_private.h"
65 #include "image_private.h"
66 #endif /* !DBGHELP_STATIC_LIB */
68 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf
);
72 * o unspecified parameters
74 * o Debug{Start|End}Point
77 * o proper types loading (nesting)
81 static void dump(const void* ptr
, unsigned len
)
85 static const char hexof
[] = "0123456789abcdef";
88 for (i
= 0; i
< len
; i
+= 16)
90 sprintf(msg
, "%08x: ", i
);
91 memset(msg
+ 10, ' ', 3 * 16 + 1 + 16);
92 for (j
= 0; j
< min(16, len
- i
); j
++)
94 msg
[10 + 3 * j
+ 0] = hexof
[x
[i
+ j
] >> 4];
95 msg
[10 + 3 * j
+ 1] = hexof
[x
[i
+ j
] & 15];
96 msg
[10 + 3 * j
+ 2] = ' ';
97 msg
[10 + 3 * 16 + 1 + j
] = (x
[i
+ j
] >= 0x20 && x
[i
+ j
] < 0x7f) ?
100 msg
[10 + 3 * 16] = ' ';
101 msg
[10 + 3 * 16 + 1 + 16] = '\0';
110 * http://www.eagercon.com/dwarf/dwarf3std.htm
111 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
113 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
115 * example of projects who do dwarf2 parsing:
116 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
117 * http://elis.ugent.be/diota/log/ltrace_elf.c
125 typedef struct dwarf2_abbrev_entry_attr_s
127 unsigned long attribute
;
129 struct dwarf2_abbrev_entry_attr_s
* next
;
130 } dwarf2_abbrev_entry_attr_t
;
132 typedef struct dwarf2_abbrev_entry_s
134 unsigned long entry_code
;
136 unsigned char have_child
;
138 dwarf2_abbrev_entry_attr_t
* attrs
;
139 } dwarf2_abbrev_entry_t
;
144 const unsigned char* ptr
;
150 enum {attr_direct
, attr_abstract_origin
, attr_specification
} gotten_from
;
153 unsigned long uvalue
;
157 struct dwarf2_block block
;
161 typedef struct dwarf2_debug_info_s
163 const dwarf2_abbrev_entry_t
*abbrev
;
165 const unsigned char** data
;
166 struct vector children
;
167 struct dwarf2_debug_info_s
* parent
;
168 } dwarf2_debug_info_t
;
170 typedef struct dwarf2_section_s
173 const unsigned char* address
;
178 enum dwarf2_sections
{section_debug
, section_string
, section_abbrev
, section_line
, section_ranges
, section_max
};
180 typedef struct dwarf2_traverse_context_s
182 const unsigned char* data
;
183 const unsigned char* end_data
;
184 unsigned char word_size
;
185 } dwarf2_traverse_context_t
;
187 /* symt_cache indexes */
194 typedef struct dwarf2_parse_context_s
196 const dwarf2_section_t
* sections
;
199 struct module
* module
;
200 struct symt_compiland
* compiland
;
201 const struct elf_thunk_area
*thunks
;
202 struct sparse_array abbrev_table
;
203 struct sparse_array debug_info_table
;
204 unsigned long load_offset
;
205 unsigned long ref_offset
;
206 struct symt
* symt_cache
[sc_num
]; /* void, int1, int2, int4 */
208 } dwarf2_parse_context_t
;
210 /* stored in the dbghelp's module internal structure for later reuse */
211 struct dwarf2_module_info_s
213 dwarf2_section_t debug_loc
;
214 dwarf2_section_t debug_frame
;
215 dwarf2_section_t eh_frame
;
216 unsigned char word_size
;
219 #define loc_dwarf2_location_list (loc_user + 0)
220 #define loc_dwarf2_block (loc_user + 1)
222 /* forward declarations */
223 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_parse_context_t
* ctx
, dwarf2_debug_info_t
* entry
);
225 static unsigned char dwarf2_get_byte(const unsigned char* ptr
)
230 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t
* ctx
)
232 unsigned char uvalue
= dwarf2_get_byte(ctx
->data
);
237 static unsigned short dwarf2_get_u2(const unsigned char* ptr
)
239 return *(const UINT16
*)ptr
;
242 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t
* ctx
)
244 unsigned short uvalue
= dwarf2_get_u2(ctx
->data
);
249 static unsigned long dwarf2_get_u4(const unsigned char* ptr
)
251 return *(const UINT32
*)ptr
;
254 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t
* ctx
)
256 unsigned long uvalue
= dwarf2_get_u4(ctx
->data
);
261 static DWORD64
dwarf2_get_u8(const unsigned char* ptr
)
263 return *(const UINT64
*)ptr
;
266 static DWORD64
dwarf2_parse_u8(dwarf2_traverse_context_t
* ctx
)
268 DWORD64 uvalue
= dwarf2_get_u8(ctx
->data
);
273 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr
, const unsigned char** end
)
275 unsigned long ret
= 0;
281 byte
= dwarf2_get_byte(ptr
++);
282 ret
|= (byte
& 0x7f) << shift
;
284 } while (byte
& 0x80);
290 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t
* ctx
)
296 ret
= dwarf2_get_leb128_as_unsigned(ctx
->data
, &ctx
->data
);
301 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr
, const unsigned char** end
)
306 const unsigned size
= sizeof(int) * 8;
310 byte
= dwarf2_get_byte(ptr
++);
311 ret
|= (byte
& 0x7f) << shift
;
313 } while (byte
& 0x80);
316 /* as spec: sign bit of byte is 2nd high order bit (80x40)
317 * -> 0x80 is used as flag.
319 if ((shift
< size
) && (byte
& 0x40))
321 ret
|= - (1 << shift
);
326 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t
* ctx
)
332 ret
= dwarf2_get_leb128_as_signed(ctx
->data
, &ctx
->data
);
336 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t
* ctx
)
339 for (ret
= 0; ctx
->data
[ret
] & 0x80; ret
++);
343 /******************************************************************
346 * Returns an address.
347 * We assume that in all cases word size from Dwarf matches the size of
348 * addresses in platform where the exec is compiled.
350 static unsigned long dwarf2_get_addr(const unsigned char* ptr
, unsigned word_size
)
357 ret
= dwarf2_get_u4(ptr
);
360 ret
= dwarf2_get_u8(ptr
);
363 FIXME("Unsupported Word Size %u\n", word_size
);
369 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t
* ctx
)
371 unsigned long ret
= dwarf2_get_addr(ctx
->data
, ctx
->word_size
);
372 ctx
->data
+= ctx
->word_size
;
376 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t
* ctx
)
378 return wine_dbg_sprintf("ctx(%p)", ctx
->data
);
381 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t
* ctx
)
383 return wine_dbg_sprintf("ctx(%p,%s)",
384 ctx
, debugstr_w(ctx
->module
->module
.ModuleName
));
387 static const char* dwarf2_debug_di(const dwarf2_debug_info_t
* di
)
389 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
390 di
->abbrev
, di
->symt
);
393 static dwarf2_abbrev_entry_t
*
394 dwarf2_abbrev_table_find_entry(const struct sparse_array
* abbrev_table
,
395 unsigned long entry_code
)
397 assert( NULL
!= abbrev_table
);
398 return sparse_array_find(abbrev_table
, entry_code
);
401 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t
* abbrev_ctx
,
402 struct sparse_array
* abbrev_table
,
405 unsigned long entry_code
;
406 dwarf2_abbrev_entry_t
* abbrev_entry
;
407 dwarf2_abbrev_entry_attr_t
* new = NULL
;
408 dwarf2_abbrev_entry_attr_t
* last
= NULL
;
409 unsigned long attribute
;
412 assert( NULL
!= abbrev_ctx
);
414 TRACE("%s, end at %p\n",
415 dwarf2_debug_traverse_ctx(abbrev_ctx
), abbrev_ctx
->end_data
);
417 sparse_array_init(abbrev_table
, sizeof(dwarf2_abbrev_entry_t
), 32);
418 while (abbrev_ctx
->data
< abbrev_ctx
->end_data
)
420 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
421 entry_code
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
422 TRACE("found entry_code %lu\n", entry_code
);
425 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
428 abbrev_entry
= sparse_array_add(abbrev_table
, entry_code
, pool
);
429 assert( NULL
!= abbrev_entry
);
431 abbrev_entry
->entry_code
= entry_code
;
432 abbrev_entry
->tag
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
433 abbrev_entry
->have_child
= dwarf2_parse_byte(abbrev_ctx
);
434 abbrev_entry
->attrs
= NULL
;
435 abbrev_entry
->num_attr
= 0;
437 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
438 abbrev_table
, sparse_array_length(abbrev_table
),
439 entry_code
, abbrev_entry
->tag
, abbrev_entry
->have_child
, abbrev_entry
);
444 attribute
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
445 form
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
446 if (!attribute
) break;
448 new = pool_alloc(pool
, sizeof(dwarf2_abbrev_entry_attr_t
));
451 new->attribute
= attribute
;
454 if (abbrev_entry
->attrs
) last
->next
= new;
455 else abbrev_entry
->attrs
= new;
457 abbrev_entry
->num_attr
++;
460 TRACE("found %u entries\n", sparse_array_length(abbrev_table
));
463 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t
* ctx
,
464 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
)
468 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr
->attribute
, abbrev_attr
->form
);
470 switch (abbrev_attr
->form
)
472 case DW_FORM_flag_present
: step
= 0; break;
473 case DW_FORM_ref_addr
:
474 case DW_FORM_addr
: step
= ctx
->word_size
; break;
477 case DW_FORM_ref1
: step
= 1; break;
479 case DW_FORM_ref2
: step
= 2; break;
482 case DW_FORM_strp
: step
= 4; break;
484 case DW_FORM_ref8
: step
= 8; break;
486 case DW_FORM_ref_udata
:
487 case DW_FORM_udata
: step
= dwarf2_leb128_length(ctx
); break;
488 case DW_FORM_string
: step
= strlen((const char*)ctx
->data
) + 1; break;
489 case DW_FORM_block
: step
= dwarf2_leb128_as_unsigned(ctx
); break;
490 case DW_FORM_block1
: step
= dwarf2_parse_byte(ctx
); break;
491 case DW_FORM_block2
: step
= dwarf2_parse_u2(ctx
); break;
492 case DW_FORM_block4
: step
= dwarf2_parse_u4(ctx
); break;
494 FIXME("Unhandled attribute form %lx\n", abbrev_attr
->form
);
500 static void dwarf2_fill_attr(const dwarf2_parse_context_t
* ctx
,
501 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
,
502 const unsigned char* data
,
503 struct attribute
* attr
)
505 attr
->form
= abbrev_attr
->form
;
508 case DW_FORM_ref_addr
:
510 attr
->u
.uvalue
= dwarf2_get_addr(data
,
511 ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
);
512 TRACE("addr<0x%lx>\n", attr
->u
.uvalue
);
516 attr
->u
.uvalue
= dwarf2_get_byte(data
);
517 TRACE("flag<0x%lx>\n", attr
->u
.uvalue
);
520 case DW_FORM_flag_present
:
522 TRACE("flag_present\n");
526 attr
->u
.uvalue
= dwarf2_get_byte(data
);
527 TRACE("data1<%lu>\n", attr
->u
.uvalue
);
531 attr
->u
.uvalue
= dwarf2_get_u2(data
);
532 TRACE("data2<%lu>\n", attr
->u
.uvalue
);
536 attr
->u
.uvalue
= dwarf2_get_u4(data
);
537 TRACE("data4<%lu>\n", attr
->u
.uvalue
);
541 attr
->u
.lluvalue
= dwarf2_get_u8(data
);
542 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr
->u
.uvalue
));
546 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_byte(data
);
547 TRACE("ref1<0x%lx>\n", attr
->u
.uvalue
);
551 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u2(data
);
552 TRACE("ref2<0x%lx>\n", attr
->u
.uvalue
);
556 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u4(data
);
557 TRACE("ref4<0x%lx>\n", attr
->u
.uvalue
);
561 FIXME("Unhandled 64-bit support\n");
565 attr
->u
.svalue
= dwarf2_get_leb128_as_signed(data
, NULL
);
568 case DW_FORM_ref_udata
:
569 attr
->u
.uvalue
= dwarf2_get_leb128_as_unsigned(data
, NULL
);
573 attr
->u
.uvalue
= dwarf2_get_leb128_as_unsigned(data
, NULL
);
577 attr
->u
.string
= (const char *)data
;
578 TRACE("string<%s>\n", attr
->u
.string
);
583 unsigned long offset
= dwarf2_get_u4(data
);
584 attr
->u
.string
= (const char*)ctx
->sections
[section_string
].address
+ offset
;
586 TRACE("strp<%s>\n", attr
->u
.string
);
590 attr
->u
.block
.size
= dwarf2_get_leb128_as_unsigned(data
, &attr
->u
.block
.ptr
);
594 attr
->u
.block
.size
= dwarf2_get_byte(data
);
595 attr
->u
.block
.ptr
= data
+ 1;
599 attr
->u
.block
.size
= dwarf2_get_u2(data
);
600 attr
->u
.block
.ptr
= data
+ 2;
604 attr
->u
.block
.size
= dwarf2_get_u4(data
);
605 attr
->u
.block
.ptr
= data
+ 4;
609 FIXME("Unhandled attribute form %lx\n", abbrev_attr
->form
);
614 static BOOL
dwarf2_find_attribute(const dwarf2_parse_context_t
* ctx
,
615 const dwarf2_debug_info_t
* di
,
616 unsigned at
, struct attribute
* attr
)
618 unsigned i
, refidx
= 0;
619 dwarf2_abbrev_entry_attr_t
* abbrev_attr
;
620 dwarf2_abbrev_entry_attr_t
* ref_abbrev_attr
= NULL
;
622 attr
->gotten_from
= attr_direct
;
625 ref_abbrev_attr
= NULL
;
626 for (i
= 0, abbrev_attr
= di
->abbrev
->attrs
; abbrev_attr
; i
++, abbrev_attr
= abbrev_attr
->next
)
628 if (abbrev_attr
->attribute
== at
)
630 dwarf2_fill_attr(ctx
, abbrev_attr
, di
->data
[i
], attr
);
633 if ((abbrev_attr
->attribute
== DW_AT_abstract_origin
||
634 abbrev_attr
->attribute
== DW_AT_specification
) &&
638 FIXME("two references %lx and %lx\n", ref_abbrev_attr
->attribute
, abbrev_attr
->attribute
);
639 ref_abbrev_attr
= abbrev_attr
;
641 attr
->gotten_from
= (abbrev_attr
->attribute
== DW_AT_abstract_origin
) ?
642 attr_abstract_origin
: attr_specification
;
645 /* do we have either an abstract origin or a specification debug entry to look into ? */
646 if (!ref_abbrev_attr
) break;
647 dwarf2_fill_attr(ctx
, ref_abbrev_attr
, di
->data
[refidx
], attr
);
648 if (!(di
= sparse_array_find(&ctx
->debug_info_table
, attr
->u
.uvalue
)))
649 FIXME("Should have found the debug info entry\n");
654 static void dwarf2_load_one_entry(dwarf2_parse_context_t
*, dwarf2_debug_info_t
*);
656 #define Wine_DW_no_register 0x7FFFFFFF
658 static unsigned dwarf2_map_register(int regno
)
660 if (regno
== Wine_DW_no_register
)
662 FIXME("What the heck map reg 0x%x\n",regno
);
665 return dbghelp_current_cpu
->map_dwarf_register(regno
, FALSE
);
668 static enum location_error
669 compute_location(dwarf2_traverse_context_t
* ctx
, struct location
* loc
,
670 HANDLE hproc
, const struct location
* frame
)
672 DWORD_PTR tmp
, stack
[64];
675 BOOL piece_found
= FALSE
;
679 loc
->kind
= loc_absolute
;
680 loc
->reg
= Wine_DW_no_register
;
682 while (ctx
->data
< ctx
->end_data
)
684 op
= dwarf2_parse_byte(ctx
);
686 if (op
>= DW_OP_lit0
&& op
<= DW_OP_lit31
)
687 stack
[++stk
] = op
- DW_OP_lit0
;
688 else if (op
>= DW_OP_reg0
&& op
<= DW_OP_reg31
)
690 /* dbghelp APIs don't know how to cope with this anyway
691 * (for example 'long long' stored in two registers)
692 * FIXME: We should tell winedbg how to deal with it (sigh)
696 DWORD cvreg
= dwarf2_map_register(op
- DW_OP_reg0
);
697 if (loc
->reg
!= Wine_DW_no_register
)
698 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
699 dbghelp_current_cpu
->fetch_regname(loc
->reg
), loc
->reg
,
700 dbghelp_current_cpu
->fetch_regname(cvreg
), cvreg
);
703 loc
->kind
= loc_register
;
705 else if (op
>= DW_OP_breg0
&& op
<= DW_OP_breg31
)
707 /* dbghelp APIs don't know how to cope with this anyway
708 * (for example 'long long' stored in two registers)
709 * FIXME: We should tell winedbg how to deal with it (sigh)
713 DWORD cvreg
= dwarf2_map_register(op
- DW_OP_breg0
);
714 if (loc
->reg
!= Wine_DW_no_register
)
715 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
716 dbghelp_current_cpu
->fetch_regname(loc
->reg
), loc
->reg
,
717 dbghelp_current_cpu
->fetch_regname(cvreg
), cvreg
);
720 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
721 loc
->kind
= loc_regrel
;
725 case DW_OP_nop
: break;
726 case DW_OP_addr
: stack
[++stk
] = dwarf2_parse_addr(ctx
); break;
727 case DW_OP_const1u
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
728 case DW_OP_const1s
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
729 case DW_OP_const2u
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
730 case DW_OP_const2s
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
731 case DW_OP_const4u
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
732 case DW_OP_const4s
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
733 case DW_OP_const8u
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
734 case DW_OP_const8s
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
735 case DW_OP_constu
: stack
[++stk
] = dwarf2_leb128_as_unsigned(ctx
); break;
736 case DW_OP_consts
: stack
[++stk
] = dwarf2_leb128_as_signed(ctx
); break;
737 case DW_OP_dup
: stack
[stk
+ 1] = stack
[stk
]; stk
++; break;
738 case DW_OP_drop
: stk
--; break;
739 case DW_OP_over
: stack
[stk
+ 1] = stack
[stk
- 1]; stk
++; break;
740 case DW_OP_pick
: stack
[stk
+ 1] = stack
[stk
- dwarf2_parse_byte(ctx
)]; stk
++; break;
741 case DW_OP_swap
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = tmp
; break;
742 case DW_OP_rot
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = stack
[stk
-2]; stack
[stk
-2] = tmp
; break;
743 case DW_OP_abs
: stack
[stk
] = labs(stack
[stk
]); break;
744 case DW_OP_neg
: stack
[stk
] = -stack
[stk
]; break;
745 case DW_OP_not
: stack
[stk
] = ~stack
[stk
]; break;
746 case DW_OP_and
: stack
[stk
-1] &= stack
[stk
]; stk
--; break;
747 case DW_OP_or
: stack
[stk
-1] |= stack
[stk
]; stk
--; break;
748 case DW_OP_minus
: stack
[stk
-1] -= stack
[stk
]; stk
--; break;
749 case DW_OP_mul
: stack
[stk
-1] *= stack
[stk
]; stk
--; break;
750 case DW_OP_plus
: stack
[stk
-1] += stack
[stk
]; stk
--; break;
751 case DW_OP_xor
: stack
[stk
-1] ^= stack
[stk
]; stk
--; break;
752 case DW_OP_shl
: stack
[stk
-1] <<= stack
[stk
]; stk
--; break;
753 case DW_OP_shr
: stack
[stk
-1] >>= stack
[stk
]; stk
--; break;
754 case DW_OP_plus_uconst
: stack
[stk
] += dwarf2_leb128_as_unsigned(ctx
); break;
755 case DW_OP_shra
: stack
[stk
-1] = stack
[stk
-1] / (1 << stack
[stk
]); stk
--; break;
756 case DW_OP_div
: stack
[stk
-1] = stack
[stk
-1] / stack
[stk
]; stk
--; break;
757 case DW_OP_mod
: stack
[stk
-1] = stack
[stk
-1] % stack
[stk
]; stk
--; break;
758 case DW_OP_ge
: stack
[stk
-1] = (stack
[stk
-1] >= stack
[stk
]); stk
--; break;
759 case DW_OP_gt
: stack
[stk
-1] = (stack
[stk
-1] > stack
[stk
]); stk
--; break;
760 case DW_OP_le
: stack
[stk
-1] = (stack
[stk
-1] <= stack
[stk
]); stk
--; break;
761 case DW_OP_lt
: stack
[stk
-1] = (stack
[stk
-1] < stack
[stk
]); stk
--; break;
762 case DW_OP_eq
: stack
[stk
-1] = (stack
[stk
-1] == stack
[stk
]); stk
--; break;
763 case DW_OP_ne
: stack
[stk
-1] = (stack
[stk
-1] != stack
[stk
]); stk
--; break;
764 case DW_OP_skip
: tmp
= dwarf2_parse_u2(ctx
); ctx
->data
+= tmp
; break;
765 case DW_OP_bra
: tmp
= dwarf2_parse_u2(ctx
); if (!stack
[stk
--]) ctx
->data
+= tmp
; break;
767 tmp
= dwarf2_leb128_as_unsigned(ctx
);
770 if (loc
->reg
!= Wine_DW_no_register
)
771 FIXME("Only supporting one reg\n");
772 loc
->reg
= dwarf2_map_register(tmp
);
774 loc
->kind
= loc_register
;
777 tmp
= dwarf2_leb128_as_unsigned(ctx
);
778 if (loc
->reg
!= Wine_DW_no_register
)
779 FIXME("Only supporting one regx\n");
780 loc
->reg
= dwarf2_map_register(tmp
);
781 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
782 loc
->kind
= loc_regrel
;
785 if (loc
->reg
!= Wine_DW_no_register
)
786 FIXME("Only supporting one reg (%s/%d -> -2)\n",
787 dbghelp_current_cpu
->fetch_regname(loc
->reg
), loc
->reg
);
788 if (frame
&& frame
->kind
== loc_register
)
790 loc
->kind
= loc_regrel
;
791 loc
->reg
= frame
->reg
;
792 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
794 else if (frame
&& frame
->kind
== loc_regrel
)
796 loc
->kind
= loc_regrel
;
797 loc
->reg
= frame
->reg
;
798 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
) + frame
->offset
;
802 /* FIXME: this could be later optimized by not recomputing
803 * this very location expression
805 loc
->kind
= loc_dwarf2_block
;
806 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
811 unsigned sz
= dwarf2_leb128_as_unsigned(ctx
);
812 WARN("Not handling OP_piece (size=%d)\n", sz
);
819 FIXME("Unexpected empty stack\n");
820 return loc_err_internal
;
822 if (loc
->reg
!= Wine_DW_no_register
)
824 WARN("Too complex expression for deref\n");
825 return loc_err_too_complex
;
829 DWORD_PTR addr
= stack
[stk
--];
832 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, sizeof(deref
), NULL
))
834 WARN("Couldn't read memory at %lx\n", addr
);
835 return loc_err_cant_read
;
837 stack
[++stk
] = deref
;
841 loc
->kind
= loc_dwarf2_block
;
844 case DW_OP_deref_size
:
847 FIXME("Unexpected empty stack\n");
848 return loc_err_internal
;
850 if (loc
->reg
!= Wine_DW_no_register
)
852 WARN("Too complex expression for deref\n");
853 return loc_err_too_complex
;
857 DWORD_PTR addr
= stack
[stk
--];
858 BYTE derefsize
= dwarf2_parse_byte(ctx
);
861 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, derefsize
, NULL
))
863 WARN("Couldn't read memory at %lx\n", addr
);
864 return loc_err_cant_read
;
869 case 1: stack
[++stk
] = *(unsigned char*)&deref
; break;
870 case 2: stack
[++stk
] = *(unsigned short*)&deref
; break;
871 case 4: stack
[++stk
] = *(DWORD
*)&deref
; break;
872 case 8: if (ctx
->word_size
>= derefsize
) stack
[++stk
] = deref
; break;
877 dwarf2_parse_byte(ctx
);
878 loc
->kind
= loc_dwarf2_block
;
881 case DW_OP_stack_value
:
882 /* Expected behaviour is that this is the last instruction of this
883 * expression and just the "top of stack" value should be put to loc->offset. */
886 if (op
< DW_OP_lo_user
) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
887 FIXME("Unhandled attr op: %x\n", op
);
888 /* FIXME else unhandled extension */
889 return loc_err_internal
;
892 loc
->offset
= stack
[stk
];
896 static BOOL
dwarf2_compute_location_attr(dwarf2_parse_context_t
* ctx
,
897 const dwarf2_debug_info_t
* di
,
899 struct location
* loc
,
900 const struct location
* frame
)
902 struct attribute xloc
;
904 if (!dwarf2_find_attribute(ctx
, di
, dw
, &xloc
)) return FALSE
;
908 case DW_FORM_data1
: case DW_FORM_data2
:
909 case DW_FORM_udata
: case DW_FORM_sdata
:
910 loc
->kind
= loc_absolute
;
912 loc
->offset
= xloc
.u
.uvalue
;
914 case DW_FORM_data4
: case DW_FORM_data8
:
915 loc
->kind
= loc_dwarf2_location_list
;
916 loc
->reg
= Wine_DW_no_register
;
917 loc
->offset
= xloc
.u
.uvalue
;
924 default: FIXME("Unsupported yet form %lx\n", xloc
.form
);
928 /* assume we have a block form */
930 if (xloc
.u
.block
.size
)
932 dwarf2_traverse_context_t lctx
;
933 enum location_error err
;
935 lctx
.data
= xloc
.u
.block
.ptr
;
936 lctx
.end_data
= xloc
.u
.block
.ptr
+ xloc
.u
.block
.size
;
937 lctx
.word_size
= ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
939 err
= compute_location(&lctx
, loc
, NULL
, frame
);
942 loc
->kind
= loc_error
;
945 else if (loc
->kind
== loc_dwarf2_block
)
947 unsigned* ptr
= pool_alloc(&ctx
->module
->pool
,
948 sizeof(unsigned) + xloc
.u
.block
.size
);
949 *ptr
= xloc
.u
.block
.size
;
950 memcpy(ptr
+ 1, xloc
.u
.block
.ptr
, xloc
.u
.block
.size
);
952 loc
->offset
= (unsigned long)ptr
;
954 loc
->offset
= (uintptr_t)ptr
;
961 static struct symt
* dwarf2_lookup_type(dwarf2_parse_context_t
* ctx
,
962 const dwarf2_debug_info_t
* di
)
964 struct attribute attr
;
965 dwarf2_debug_info_t
* type
;
967 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_type
, &attr
))
969 if (!(type
= sparse_array_find(&ctx
->debug_info_table
, attr
.u
.uvalue
)))
971 FIXME("Unable to find back reference to type %lx\n", attr
.u
.uvalue
);
976 /* load the debug info entity */
977 dwarf2_load_one_entry(ctx
, type
);
979 FIXME("Unable to load forward reference for tag %lx\n", type
->abbrev
->tag
);
984 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t
* ctx
, dwarf2_debug_info_t
* di
, const char* name
)
987 struct attribute diname
;
988 struct attribute spec
;
990 if (di
->abbrev
->tag
== DW_TAG_compile_unit
) return name
;
992 ctx
->cpp_name
= pool_alloc(&ctx
->pool
, MAX_SYM_NAME
);
993 last
= ctx
->cpp_name
+ MAX_SYM_NAME
- strlen(name
) - 1;
996 /* if the di is a definition, but has also a (previous) declaration, then scope must
997 * be gotten from declaration not definition
999 if (dwarf2_find_attribute(ctx
, di
, DW_AT_specification
, &spec
) && spec
.gotten_from
== attr_direct
)
1001 di
= sparse_array_find(&ctx
->debug_info_table
, spec
.u
.uvalue
);
1004 FIXME("Should have found the debug info entry\n");
1009 for (di
= di
->parent
; di
; di
= di
->parent
)
1011 switch (di
->abbrev
->tag
)
1013 case DW_TAG_namespace
:
1014 case DW_TAG_structure_type
:
1015 case DW_TAG_class_type
:
1016 case DW_TAG_interface_type
:
1017 case DW_TAG_union_type
:
1018 if (dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &diname
))
1020 size_t len
= strlen(diname
.u
.string
);
1022 if (last
< ctx
->cpp_name
) return NULL
;
1023 memcpy(last
, diname
.u
.string
, len
);
1024 last
[len
] = last
[len
+ 1] = ':';
1034 /******************************************************************
1037 * read a range for a given debug_info (either using AT_range attribute, in which
1038 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1039 * in all cases, range is relative to beginning of compilation unit
1041 static BOOL
dwarf2_read_range(dwarf2_parse_context_t
* ctx
, const dwarf2_debug_info_t
* di
,
1042 unsigned long* plow
, unsigned long* phigh
)
1044 struct attribute range
;
1046 if (dwarf2_find_attribute(ctx
, di
, DW_AT_ranges
, &range
))
1048 dwarf2_traverse_context_t traverse
;
1049 unsigned long low
, high
;
1051 traverse
.data
= ctx
->sections
[section_ranges
].address
+ range
.u
.uvalue
;
1052 traverse
.end_data
= ctx
->sections
[section_ranges
].address
+
1053 ctx
->sections
[section_ranges
].size
;
1054 traverse
.word_size
= ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
1058 while (traverse
.data
+ 2 * traverse
.word_size
< traverse
.end_data
)
1060 low
= dwarf2_parse_addr(&traverse
);
1061 high
= dwarf2_parse_addr(&traverse
);
1062 if (low
== 0 && high
== 0) break;
1063 if (low
== ULONG_MAX
) FIXME("unsupported yet (base address selection)\n");
1064 if (low
< *plow
) *plow
= low
;
1065 if (high
> *phigh
) *phigh
= high
;
1067 if (*plow
== ULONG_MAX
|| *phigh
== 0) {FIXME("no entry found\n"); return FALSE
;}
1068 if (*plow
== *phigh
) {FIXME("entry found, but low=high\n"); return FALSE
;}
1074 struct attribute low_pc
;
1075 struct attribute high_pc
;
1077 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_low_pc
, &low_pc
) ||
1078 !dwarf2_find_attribute(ctx
, di
, DW_AT_high_pc
, &high_pc
))
1080 *plow
= low_pc
.u
.uvalue
;
1081 *phigh
= high_pc
.u
.uvalue
;
1086 /******************************************************************
1087 * dwarf2_read_one_debug_info
1089 * Loads into memory one debug info entry, and recursively its children (if any)
1091 static BOOL
dwarf2_read_one_debug_info(dwarf2_parse_context_t
* ctx
,
1092 dwarf2_traverse_context_t
* traverse
,
1093 dwarf2_debug_info_t
* parent_di
,
1094 dwarf2_debug_info_t
** pdi
)
1096 const dwarf2_abbrev_entry_t
*abbrev
;
1097 unsigned long entry_code
;
1098 unsigned long offset
;
1099 dwarf2_debug_info_t
* di
;
1100 dwarf2_debug_info_t
* child
;
1101 dwarf2_debug_info_t
** where
;
1102 dwarf2_abbrev_entry_attr_t
* attr
;
1104 struct attribute sibling
;
1106 offset
= traverse
->data
- ctx
->sections
[ctx
->section
].address
;
1107 entry_code
= dwarf2_leb128_as_unsigned(traverse
);
1108 TRACE("found entry_code %lu at 0x%lx\n", entry_code
, offset
);
1114 abbrev
= dwarf2_abbrev_table_find_entry(&ctx
->abbrev_table
, entry_code
);
1117 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code
, offset
);
1120 di
= sparse_array_add(&ctx
->debug_info_table
, offset
, &ctx
->pool
);
1121 if (!di
) return FALSE
;
1122 di
->abbrev
= abbrev
;
1124 di
->parent
= parent_di
;
1126 if (abbrev
->num_attr
)
1128 di
->data
= pool_alloc(&ctx
->pool
, abbrev
->num_attr
* sizeof(const char*));
1129 for (i
= 0, attr
= abbrev
->attrs
; attr
; i
++, attr
= attr
->next
)
1131 di
->data
[i
] = traverse
->data
;
1132 dwarf2_swallow_attribute(traverse
, attr
);
1135 else di
->data
= NULL
;
1136 if (abbrev
->have_child
)
1138 vector_init(&di
->children
, sizeof(dwarf2_debug_info_t
*), 16);
1139 while (traverse
->data
< traverse
->end_data
)
1141 if (!dwarf2_read_one_debug_info(ctx
, traverse
, di
, &child
)) return FALSE
;
1143 where
= vector_add(&di
->children
, &ctx
->pool
);
1144 if (!where
) return FALSE
;
1148 if (dwarf2_find_attribute(ctx
, di
, DW_AT_sibling
, &sibling
) &&
1149 traverse
->data
!= ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
)
1151 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1152 dwarf2_debug_traverse_ctx(traverse
), sibling
.u
.uvalue
);
1153 traverse
->data
= ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
;
1159 static struct vector
* dwarf2_get_di_children(dwarf2_parse_context_t
* ctx
,
1160 dwarf2_debug_info_t
* di
)
1162 struct attribute spec
;
1166 if (di
->abbrev
->have_child
)
1167 return &di
->children
;
1168 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_specification
, &spec
)) break;
1169 if (!(di
= sparse_array_find(&ctx
->debug_info_table
, spec
.u
.uvalue
)))
1170 FIXME("Should have found the debug info entry\n");
1175 static struct symt
* dwarf2_parse_base_type(dwarf2_parse_context_t
* ctx
,
1176 dwarf2_debug_info_t
* di
)
1178 struct attribute name
;
1179 struct attribute size
;
1180 struct attribute encoding
;
1183 if (di
->symt
) return di
->symt
;
1185 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1187 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1188 name
.u
.string
= NULL
;
1189 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1190 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_encoding
, &encoding
)) encoding
.u
.uvalue
= DW_ATE_void
;
1192 switch (encoding
.u
.uvalue
)
1194 case DW_ATE_void
: bt
= btVoid
; break;
1195 case DW_ATE_address
: bt
= btULong
; break;
1196 case DW_ATE_boolean
: bt
= btBool
; break;
1197 case DW_ATE_complex_float
: bt
= btComplex
; break;
1198 case DW_ATE_float
: bt
= btFloat
; break;
1199 case DW_ATE_signed
: bt
= btInt
; break;
1200 case DW_ATE_unsigned
: bt
= btUInt
; break;
1201 case DW_ATE_signed_char
: bt
= btChar
; break;
1202 case DW_ATE_unsigned_char
: bt
= btChar
; break;
1203 default: bt
= btNoType
; break;
1205 di
->symt
= &symt_new_basic(ctx
->module
, bt
, name
.u
.string
, size
.u
.uvalue
)->symt
;
1209 assert(size
.u
.uvalue
== 0);
1210 cache_idx
= sc_void
;
1213 switch (size
.u
.uvalue
)
1215 case 1: cache_idx
= sc_int1
; break;
1216 case 2: cache_idx
= sc_int2
; break;
1217 case 4: cache_idx
= sc_int4
; break;
1222 if (cache_idx
!= -1 && !ctx
->symt_cache
[cache_idx
])
1223 ctx
->symt_cache
[cache_idx
] = di
->symt
;
1225 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1229 static struct symt
* dwarf2_parse_typedef(dwarf2_parse_context_t
* ctx
,
1230 dwarf2_debug_info_t
* di
)
1232 struct symt
* ref_type
;
1233 struct attribute name
;
1235 if (di
->symt
) return di
->symt
;
1237 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx
), di
->abbrev
->entry_code
);
1239 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1240 ref_type
= dwarf2_lookup_type(ctx
, di
);
1243 di
->symt
= &symt_new_typedef(ctx
->module
, ref_type
, name
.u
.string
)->symt
;
1244 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1248 static struct symt
* dwarf2_parse_pointer_type(dwarf2_parse_context_t
* ctx
,
1249 dwarf2_debug_info_t
* di
)
1251 struct symt
* ref_type
;
1252 struct attribute size
;
1254 if (di
->symt
) return di
->symt
;
1256 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1258 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= sizeof(void *);
1259 if (!(ref_type
= dwarf2_lookup_type(ctx
, di
)))
1261 ref_type
= ctx
->symt_cache
[sc_void
];
1264 di
->symt
= &symt_new_pointer(ctx
->module
, ref_type
, size
.u
.uvalue
)->symt
;
1265 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1269 static struct symt
* dwarf2_parse_array_type(dwarf2_parse_context_t
* ctx
,
1270 dwarf2_debug_info_t
* di
)
1272 struct symt
* ref_type
;
1273 struct symt
* idx_type
= NULL
;
1274 struct attribute min
, max
, cnt
;
1275 dwarf2_debug_info_t
* child
;
1277 const struct vector
* children
;
1279 if (di
->symt
) return di
->symt
;
1281 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1283 ref_type
= dwarf2_lookup_type(ctx
, di
);
1285 if (!(children
= dwarf2_get_di_children(ctx
, di
)))
1287 /* fake an array with unknown size */
1288 /* FIXME: int4 even on 64bit machines??? */
1289 idx_type
= ctx
->symt_cache
[sc_int4
];
1293 else for (i
= 0; i
< vector_length(children
); i
++)
1295 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1296 switch (child
->abbrev
->tag
)
1298 case DW_TAG_subrange_type
:
1299 idx_type
= dwarf2_lookup_type(ctx
, child
);
1300 if (!dwarf2_find_attribute(ctx
, child
, DW_AT_lower_bound
, &min
))
1302 if (!dwarf2_find_attribute(ctx
, child
, DW_AT_upper_bound
, &max
))
1304 if (dwarf2_find_attribute(ctx
, child
, DW_AT_count
, &cnt
))
1305 max
.u
.uvalue
= min
.u
.uvalue
+ cnt
.u
.uvalue
;
1308 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1309 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1313 di
->symt
= &symt_new_array(ctx
->module
, min
.u
.uvalue
, max
.u
.uvalue
, ref_type
, idx_type
)->symt
;
1317 static struct symt
* dwarf2_parse_const_type(dwarf2_parse_context_t
* ctx
,
1318 dwarf2_debug_info_t
* di
)
1320 struct symt
* ref_type
;
1322 if (di
->symt
) return di
->symt
;
1324 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1326 if (!(ref_type
= dwarf2_lookup_type(ctx
, di
)))
1328 ref_type
= ctx
->symt_cache
[sc_void
];
1331 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1332 di
->symt
= ref_type
;
1337 static struct symt
* dwarf2_parse_volatile_type(dwarf2_parse_context_t
* ctx
,
1338 dwarf2_debug_info_t
* di
)
1340 struct symt
* ref_type
;
1342 if (di
->symt
) return di
->symt
;
1344 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1346 if (!(ref_type
= dwarf2_lookup_type(ctx
, di
)))
1348 ref_type
= ctx
->symt_cache
[sc_void
];
1351 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1352 di
->symt
= ref_type
;
1357 static struct symt
* dwarf2_parse_unspecified_type(dwarf2_parse_context_t
* ctx
,
1358 dwarf2_debug_info_t
* di
)
1360 struct attribute name
;
1361 struct attribute size
;
1362 struct symt_basic
*basic
;
1364 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1366 if (di
->symt
) return di
->symt
;
1368 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1369 name
.u
.string
= "void";
1370 size
.u
.uvalue
= sizeof(void *);
1372 basic
= symt_new_basic(ctx
->module
, btVoid
, name
.u
.string
, size
.u
.uvalue
);
1373 di
->symt
= &basic
->symt
;
1375 if (!ctx
->symt_cache
[sc_void
])
1376 ctx
->symt_cache
[sc_void
] = di
->symt
;
1378 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1382 static struct symt
* dwarf2_parse_reference_type(dwarf2_parse_context_t
* ctx
,
1383 dwarf2_debug_info_t
* di
)
1385 struct symt
* ref_type
= NULL
;
1387 if (di
->symt
) return di
->symt
;
1389 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1391 ref_type
= dwarf2_lookup_type(ctx
, di
);
1392 /* FIXME: for now, we hard-wire C++ references to pointers */
1393 di
->symt
= &symt_new_pointer(ctx
->module
, ref_type
, sizeof(void *))->symt
;
1395 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1400 static void dwarf2_parse_udt_member(dwarf2_parse_context_t
* ctx
,
1401 dwarf2_debug_info_t
* di
,
1402 struct symt_udt
* parent
)
1404 struct symt
* elt_type
;
1405 struct attribute name
;
1406 struct attribute bit_size
;
1407 struct attribute bit_offset
;
1408 struct location loc
;
1412 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1414 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1415 elt_type
= dwarf2_lookup_type(ctx
, di
);
1416 if (dwarf2_compute_location_attr(ctx
, di
, DW_AT_data_member_location
, &loc
, NULL
))
1418 if (loc
.kind
!= loc_absolute
)
1420 FIXME("Found register, while not expecting it\n");
1424 TRACE("found member_location at %s -> %lu\n",
1425 dwarf2_debug_ctx(ctx
), loc
.offset
);
1429 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_bit_size
, &bit_size
))
1430 bit_size
.u
.uvalue
= 0;
1431 if (dwarf2_find_attribute(ctx
, di
, DW_AT_bit_offset
, &bit_offset
))
1433 /* FIXME: we should only do this when implementation is LSB (which is
1434 * the case on i386 processors)
1436 struct attribute nbytes
;
1437 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &nbytes
))
1440 nbytes
.u
.uvalue
= symt_get_info(ctx
->module
, elt_type
, TI_GET_LENGTH
, &size
) ?
1441 (unsigned long)size
: 0;
1443 bit_offset
.u
.uvalue
= nbytes
.u
.uvalue
* 8 - bit_offset
.u
.uvalue
- bit_size
.u
.uvalue
;
1445 else bit_offset
.u
.uvalue
= 0;
1446 symt_add_udt_element(ctx
->module
, parent
, name
.u
.string
, elt_type
,
1447 (loc
.offset
<< 3) + bit_offset
.u
.uvalue
,
1450 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1453 static struct symt
* dwarf2_parse_subprogram(dwarf2_parse_context_t
* ctx
,
1454 dwarf2_debug_info_t
* di
);
1456 static struct symt
* dwarf2_parse_udt_type(dwarf2_parse_context_t
* ctx
,
1457 dwarf2_debug_info_t
* di
,
1460 struct attribute name
;
1461 struct attribute size
;
1462 struct vector
* children
;
1463 dwarf2_debug_info_t
*child
;
1466 if (di
->symt
) return di
->symt
;
1468 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1470 /* quirk... FIXME provide real support for anonymous UDTs */
1471 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1472 name
.u
.string
= "zz_anon_zz";
1473 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1475 di
->symt
= &symt_new_udt(ctx
->module
, dwarf2_get_cpp_name(ctx
, di
, name
.u
.string
),
1476 size
.u
.uvalue
, udt
)->symt
;
1478 children
= dwarf2_get_di_children(ctx
, di
);
1479 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1481 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1483 switch (child
->abbrev
->tag
)
1485 case DW_TAG_array_type
:
1486 dwarf2_parse_array_type(ctx
, di
);
1489 /* FIXME: should I follow the sibling stuff ?? */
1490 dwarf2_parse_udt_member(ctx
, child
, (struct symt_udt
*)di
->symt
);
1492 case DW_TAG_enumeration_type
:
1493 dwarf2_parse_enumeration_type(ctx
, child
);
1495 case DW_TAG_subprogram
:
1496 dwarf2_parse_subprogram(ctx
, child
);
1498 case DW_TAG_const_type
:
1499 dwarf2_parse_const_type(ctx
, child
);
1501 case DW_TAG_structure_type
:
1502 case DW_TAG_class_type
:
1503 case DW_TAG_union_type
:
1504 case DW_TAG_typedef
:
1505 /* FIXME: we need to handle nested udt definitions */
1506 case DW_TAG_inheritance
:
1507 case DW_TAG_template_type_param
:
1508 case DW_TAG_template_value_param
:
1509 case DW_TAG_variable
:
1510 case DW_TAG_imported_declaration
:
1511 case DW_TAG_ptr_to_member_type
:
1512 case DW_TAG_GNU_template_parameter_pack
:
1513 case DW_TAG_GNU_formal_parameter_pack
:
1514 /* FIXME: some C++ related stuff */
1517 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1518 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1526 static void dwarf2_parse_enumerator(dwarf2_parse_context_t
* ctx
,
1527 dwarf2_debug_info_t
* di
,
1528 struct symt_enum
* parent
)
1530 struct attribute name
;
1531 struct attribute value
;
1533 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1535 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) return;
1536 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_const_value
, &value
)) value
.u
.svalue
= 0;
1537 symt_add_enum_element(ctx
->module
, parent
, name
.u
.string
, value
.u
.svalue
);
1539 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1542 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_parse_context_t
* ctx
,
1543 dwarf2_debug_info_t
* di
)
1545 struct attribute name
;
1546 struct attribute size
;
1547 struct symt_basic
* basetype
;
1548 struct vector
* children
;
1549 dwarf2_debug_info_t
*child
;
1552 if (di
->symt
) return di
->symt
;
1554 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1556 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1557 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 4;
1559 switch (size
.u
.uvalue
) /* FIXME: that's wrong */
1561 case 1: basetype
= symt_new_basic(ctx
->module
, btInt
, "char", 1); break;
1562 case 2: basetype
= symt_new_basic(ctx
->module
, btInt
, "short", 2); break;
1564 case 4: basetype
= symt_new_basic(ctx
->module
, btInt
, "int", 4); break;
1567 di
->symt
= &symt_new_enum(ctx
->module
, name
.u
.string
, &basetype
->symt
)->symt
;
1569 children
= dwarf2_get_di_children(ctx
, di
);
1570 /* FIXME: should we use the sibling stuff ?? */
1571 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1573 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1575 switch (child
->abbrev
->tag
)
1577 case DW_TAG_enumerator
:
1578 dwarf2_parse_enumerator(ctx
, child
, (struct symt_enum
*)di
->symt
);
1581 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1582 di
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1588 /* structure used to pass information around when parsing a subprogram */
1589 typedef struct dwarf2_subprogram_s
1591 dwarf2_parse_context_t
* ctx
;
1592 struct symt_function
* func
;
1593 BOOL non_computed_variable
;
1594 struct location frame
;
1595 } dwarf2_subprogram_t
;
1597 /******************************************************************
1598 * dwarf2_parse_variable
1600 * Parses any variable (parameter, local/global variable)
1602 static void dwarf2_parse_variable(dwarf2_subprogram_t
* subpgm
,
1603 struct symt_block
* block
,
1604 dwarf2_debug_info_t
* di
)
1606 struct symt
* param_type
;
1607 struct attribute name
, value
;
1608 struct location loc
;
1611 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1613 is_pmt
= !block
&& di
->abbrev
->tag
== DW_TAG_formal_parameter
;
1614 param_type
= dwarf2_lookup_type(subpgm
->ctx
, di
);
1616 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_name
, &name
)) {
1617 /* cannot do much without the name, the functions below won't like it. */
1620 if (dwarf2_compute_location_attr(subpgm
->ctx
, di
, DW_AT_location
,
1621 &loc
, &subpgm
->frame
))
1623 struct attribute ext
;
1625 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1626 name
.u
.string
, loc
.kind
, loc
.offset
, loc
.reg
,
1627 dwarf2_debug_ctx(subpgm
->ctx
));
1634 /* it's a global variable */
1635 /* FIXME: we don't handle its scope yet */
1636 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_external
, &ext
))
1638 loc
.offset
+= subpgm
->ctx
->load_offset
;
1639 symt_new_global_variable(subpgm
->ctx
->module
, subpgm
->ctx
->compiland
,
1640 dwarf2_get_cpp_name(subpgm
->ctx
, di
, name
.u
.string
), !ext
.u
.uvalue
,
1641 loc
, 0, param_type
);
1644 subpgm
->non_computed_variable
= TRUE
;
1648 /* either a pmt/variable relative to frame pointer or
1649 * pmt/variable in a register
1651 assert(subpgm
->func
);
1652 symt_add_func_local(subpgm
->ctx
->module
, subpgm
->func
,
1653 is_pmt
? DataIsParam
: DataIsLocal
,
1654 &loc
, block
, param_type
, name
.u
.string
);
1658 else if (dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_const_value
, &value
))
1661 if (subpgm
->func
) WARN("Unsupported constant %s in function\n", name
.u
.string
);
1662 if (is_pmt
) FIXME("Unsupported constant (parameter) %s in function\n", name
.u
.string
);
1670 v
.n1
.n2
.vt
= VT_UI4
;
1671 v
.n1
.n2
.n3
.lVal
= value
.u
.uvalue
;
1675 v
.n1
.n2
.vt
= VT_UI8
;
1676 v
.n1
.n2
.n3
.llVal
= value
.u
.lluvalue
;
1681 v
.n1
.n2
.n3
.lVal
= value
.u
.svalue
;
1685 case DW_FORM_string
:
1686 /* FIXME: native doesn't report const strings from here !!
1687 * however, the value of the string is in the code somewhere
1689 v
.n1
.n2
.vt
= VT_I1
| VT_BYREF
;
1690 v
.n1
.n2
.n3
.byref
= pool_strdup(&subpgm
->ctx
->module
->pool
, value
.u
.string
);
1694 case DW_FORM_block1
:
1695 case DW_FORM_block2
:
1696 case DW_FORM_block4
:
1698 switch (value
.u
.block
.size
)
1700 case 1: v
.n1
.n2
.n3
.lVal
= *(BYTE
*)value
.u
.block
.ptr
; break;
1701 case 2: v
.n1
.n2
.n3
.lVal
= *(USHORT
*)value
.u
.block
.ptr
; break;
1702 case 4: v
.n1
.n2
.n3
.lVal
= *(DWORD
*)value
.u
.block
.ptr
; break;
1704 v
.n1
.n2
.vt
= VT_I1
| VT_BYREF
;
1705 v
.n1
.n2
.n3
.byref
= pool_alloc(&subpgm
->ctx
->module
->pool
, value
.u
.block
.size
);
1706 memcpy(v
.n1
.n2
.n3
.byref
, value
.u
.block
.ptr
, value
.u
.block
.size
);
1711 FIXME("Unsupported form for const value %s (%lx)\n",
1712 name
.u
.string
, value
.form
);
1713 v
.n1
.n2
.vt
= VT_EMPTY
;
1715 di
->symt
= &symt_new_constant(subpgm
->ctx
->module
, subpgm
->ctx
->compiland
,
1716 name
.u
.string
, param_type
, &v
)->symt
;
1720 /* variable has been optimized away... report anyway */
1721 loc
.kind
= loc_error
;
1722 loc
.reg
= loc_err_no_location
;
1725 symt_add_func_local(subpgm
->ctx
->module
, subpgm
->func
,
1726 is_pmt
? DataIsParam
: DataIsLocal
,
1727 &loc
, block
, param_type
, name
.u
.string
);
1731 WARN("dropping global variable %s which has been optimized away\n", name
.u
.string
);
1734 if (is_pmt
&& subpgm
->func
&& subpgm
->func
->type
)
1735 symt_add_function_signature_parameter(subpgm
->ctx
->module
,
1736 (struct symt_function_signature
*)subpgm
->func
->type
,
1739 if (dwarf2_get_di_children(subpgm
->ctx
, di
)) FIXME("Unsupported children\n");
1742 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t
* subpgm
,
1743 const dwarf2_debug_info_t
* di
)
1745 struct attribute name
;
1746 struct attribute low_pc
;
1747 struct location loc
;
1749 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1751 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_low_pc
, &low_pc
)) low_pc
.u
.uvalue
= 0;
1752 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_name
, &name
))
1753 name
.u
.string
= NULL
;
1755 loc
.kind
= loc_absolute
;
1756 loc
.offset
= subpgm
->ctx
->load_offset
+ low_pc
.u
.uvalue
;
1757 symt_add_function_point(subpgm
->ctx
->module
, subpgm
->func
, SymTagLabel
,
1758 &loc
, name
.u
.string
);
1761 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
1762 struct symt_block
* parent_block
,
1763 dwarf2_debug_info_t
* di
);
1765 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_parse_context_t
* ctx
,
1766 dwarf2_debug_info_t
* di
);
1768 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t
* subpgm
,
1769 struct symt_block
* parent_block
,
1770 dwarf2_debug_info_t
* di
)
1772 struct symt_block
* block
;
1773 unsigned long low_pc
, high_pc
;
1774 struct vector
* children
;
1775 dwarf2_debug_info_t
*child
;
1778 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1780 if (!dwarf2_read_range(subpgm
->ctx
, di
, &low_pc
, &high_pc
))
1782 FIXME("cannot read range\n");
1786 block
= symt_open_func_block(subpgm
->ctx
->module
, subpgm
->func
, parent_block
,
1787 subpgm
->ctx
->load_offset
+ low_pc
- subpgm
->func
->address
,
1790 children
= dwarf2_get_di_children(subpgm
->ctx
, di
);
1791 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1793 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1795 switch (child
->abbrev
->tag
)
1797 case DW_TAG_formal_parameter
:
1798 case DW_TAG_variable
:
1799 dwarf2_parse_variable(subpgm
, block
, child
);
1801 case DW_TAG_lexical_block
:
1802 dwarf2_parse_subprogram_block(subpgm
, block
, child
);
1804 case DW_TAG_inlined_subroutine
:
1805 dwarf2_parse_inlined_subroutine(subpgm
, block
, child
);
1808 dwarf2_parse_subprogram_label(subpgm
, child
);
1810 case DW_TAG_GNU_call_site
:
1811 /* this isn't properly supported by dbghelp interface. skip it for now */
1814 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1815 child
->abbrev
->tag
, dwarf2_debug_ctx(subpgm
->ctx
),
1816 dwarf2_debug_di(di
));
1819 symt_close_func_block(subpgm
->ctx
->module
, subpgm
->func
, block
, 0);
1822 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
1823 struct symt_block
* parent_block
,
1824 dwarf2_debug_info_t
* di
)
1826 struct symt_block
* block
;
1827 unsigned long low_pc
, high_pc
;
1828 struct vector
* children
;
1829 dwarf2_debug_info_t
*child
;
1832 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1834 if (!dwarf2_read_range(subpgm
->ctx
, di
, &low_pc
, &high_pc
))
1840 block
= symt_open_func_block(subpgm
->ctx
->module
, subpgm
->func
, parent_block
,
1841 subpgm
->ctx
->load_offset
+ low_pc
- subpgm
->func
->address
,
1844 children
= dwarf2_get_di_children(subpgm
->ctx
, di
);
1845 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1847 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1849 switch (child
->abbrev
->tag
)
1851 case DW_TAG_inlined_subroutine
:
1852 dwarf2_parse_inlined_subroutine(subpgm
, block
, child
);
1854 case DW_TAG_variable
:
1855 dwarf2_parse_variable(subpgm
, block
, child
);
1857 case DW_TAG_pointer_type
:
1858 dwarf2_parse_pointer_type(subpgm
->ctx
, di
);
1860 case DW_TAG_subroutine_type
:
1861 dwarf2_parse_subroutine_type(subpgm
->ctx
, di
);
1863 case DW_TAG_const_type
:
1864 dwarf2_parse_const_type(subpgm
->ctx
, di
);
1866 case DW_TAG_lexical_block
:
1867 dwarf2_parse_subprogram_block(subpgm
, block
, child
);
1869 case DW_TAG_subprogram
:
1870 /* FIXME: likely a declaration (to be checked)
1874 case DW_TAG_formal_parameter
:
1875 /* FIXME: likely elements for exception handling (GCC flavor)
1879 case DW_TAG_imported_module
:
1880 /* C++ stuff to be silenced (for now) */
1882 case DW_TAG_GNU_call_site
:
1883 /* this isn't properly supported by dbghelp interface. skip it for now */
1886 dwarf2_parse_subprogram_label(subpgm
, child
);
1888 case DW_TAG_class_type
:
1889 case DW_TAG_structure_type
:
1890 case DW_TAG_union_type
:
1891 case DW_TAG_enumeration_type
:
1892 case DW_TAG_typedef
:
1893 /* the type referred to will be loaded when we need it, so skip it */
1896 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1897 child
->abbrev
->tag
, dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1901 symt_close_func_block(subpgm
->ctx
->module
, subpgm
->func
, block
, 0);
1904 static struct symt
* dwarf2_parse_subprogram(dwarf2_parse_context_t
* ctx
,
1905 dwarf2_debug_info_t
* di
)
1907 struct attribute name
;
1908 unsigned long low_pc
, high_pc
;
1909 struct attribute is_decl
;
1910 struct attribute inline_flags
;
1911 struct symt
* ret_type
;
1912 struct symt_function_signature
* sig_type
;
1913 dwarf2_subprogram_t subpgm
;
1914 struct vector
* children
;
1915 dwarf2_debug_info_t
* child
;
1918 if (di
->symt
) return di
->symt
;
1920 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1922 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1924 WARN("No name for function... dropping function\n");
1927 /* if it's an abstract representation of an inline function, there should be
1928 * a concrete object that we'll handle
1930 if (dwarf2_find_attribute(ctx
, di
, DW_AT_inline
, &inline_flags
) &&
1931 inline_flags
.u
.uvalue
!= DW_INL_not_inlined
)
1933 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1934 debugstr_a(name
.u
.string
), inline_flags
.u
.uvalue
);
1938 if (dwarf2_find_attribute(ctx
, di
, DW_AT_declaration
, &is_decl
) &&
1939 is_decl
.u
.uvalue
&& is_decl
.gotten_from
== attr_direct
)
1941 /* it's a real declaration, skip it */
1944 if (!dwarf2_read_range(ctx
, di
, &low_pc
, &high_pc
))
1946 WARN("cannot get range for %s\n", name
.u
.string
);
1949 /* As functions (defined as inline assembly) get debug info with dwarf
1950 * (not the case for stabs), we just drop Wine's thunks here...
1951 * Actual thunks will be created in elf_module from the symbol table
1953 #ifndef DBGHELP_STATIC_LIB
1954 if (elf_is_in_thunk_area(ctx
->load_offset
+ low_pc
, ctx
->thunks
) >= 0)
1957 if (!(ret_type
= dwarf2_lookup_type(ctx
, di
)))
1959 ret_type
= ctx
->symt_cache
[sc_void
];
1962 /* FIXME: assuming C source code */
1963 sig_type
= symt_new_function_signature(ctx
->module
, ret_type
, CV_CALL_FAR_C
);
1964 subpgm
.func
= symt_new_function(ctx
->module
, ctx
->compiland
,
1965 dwarf2_get_cpp_name(ctx
, di
, name
.u
.string
),
1966 ctx
->load_offset
+ low_pc
, high_pc
- low_pc
,
1968 di
->symt
= &subpgm
.func
->symt
;
1970 if (!dwarf2_compute_location_attr(ctx
, di
, DW_AT_frame_base
,
1971 &subpgm
.frame
, NULL
))
1974 subpgm
.frame
.kind
= loc_regrel
;
1975 subpgm
.frame
.reg
= dbghelp_current_cpu
->frame_regno
;
1976 subpgm
.frame
.offset
= 0;
1978 subpgm
.non_computed_variable
= FALSE
;
1980 children
= dwarf2_get_di_children(ctx
, di
);
1981 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1983 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1985 switch (child
->abbrev
->tag
)
1987 case DW_TAG_variable
:
1988 case DW_TAG_formal_parameter
:
1989 dwarf2_parse_variable(&subpgm
, NULL
, child
);
1991 case DW_TAG_lexical_block
:
1992 dwarf2_parse_subprogram_block(&subpgm
, NULL
, child
);
1994 case DW_TAG_inlined_subroutine
:
1995 dwarf2_parse_inlined_subroutine(&subpgm
, NULL
, child
);
1997 case DW_TAG_pointer_type
:
1998 dwarf2_parse_pointer_type(subpgm
.ctx
, di
);
2000 case DW_TAG_const_type
:
2001 dwarf2_parse_const_type(subpgm
.ctx
, di
);
2003 case DW_TAG_subprogram
:
2004 /* FIXME: likely a declaration (to be checked)
2009 dwarf2_parse_subprogram_label(&subpgm
, child
);
2011 case DW_TAG_class_type
:
2012 case DW_TAG_structure_type
:
2013 case DW_TAG_union_type
:
2014 case DW_TAG_enumeration_type
:
2015 case DW_TAG_typedef
:
2016 /* the type referred to will be loaded when we need it, so skip it */
2018 case DW_TAG_unspecified_parameters
:
2019 case DW_TAG_template_type_param
:
2020 case DW_TAG_template_value_param
:
2021 case DW_TAG_GNU_call_site
:
2022 case DW_TAG_GNU_template_parameter_pack
:
2023 case DW_TAG_GNU_formal_parameter_pack
:
2024 /* FIXME: no support in dbghelp's internals so far */
2027 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
2028 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
2032 if (subpgm
.non_computed_variable
|| subpgm
.frame
.kind
>= loc_user
)
2034 symt_add_function_point(ctx
->module
, subpgm
.func
, SymTagCustom
,
2035 &subpgm
.frame
, NULL
);
2037 if (subpgm
.func
) symt_normalize_function(subpgm
.ctx
->module
, subpgm
.func
);
2042 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_parse_context_t
* ctx
,
2043 dwarf2_debug_info_t
* di
)
2045 struct symt
* ret_type
;
2046 struct symt_function_signature
* sig_type
;
2047 struct vector
* children
;
2048 dwarf2_debug_info_t
* child
;
2051 if (di
->symt
) return di
->symt
;
2053 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
2055 if (!(ret_type
= dwarf2_lookup_type(ctx
, di
)))
2057 ret_type
= ctx
->symt_cache
[sc_void
];
2061 /* FIXME: assuming C source code */
2062 sig_type
= symt_new_function_signature(ctx
->module
, ret_type
, CV_CALL_FAR_C
);
2064 children
= dwarf2_get_di_children(ctx
, di
);
2065 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2067 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2069 switch (child
->abbrev
->tag
)
2071 case DW_TAG_formal_parameter
:
2072 symt_add_function_signature_parameter(ctx
->module
, sig_type
,
2073 dwarf2_lookup_type(ctx
, child
));
2075 case DW_TAG_unspecified_parameters
:
2076 WARN("Unsupported unspecified parameters\n");
2081 return di
->symt
= &sig_type
->symt
;
2084 static void dwarf2_parse_namespace(dwarf2_parse_context_t
* ctx
,
2085 dwarf2_debug_info_t
* di
)
2087 struct vector
* children
;
2088 dwarf2_debug_info_t
* child
;
2091 if (di
->symt
) return;
2093 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
2095 di
->symt
= ctx
->symt_cache
[sc_void
];
2097 children
= dwarf2_get_di_children(ctx
, di
);
2098 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2100 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2101 dwarf2_load_one_entry(ctx
, child
);
2105 static void dwarf2_load_one_entry(dwarf2_parse_context_t
* ctx
,
2106 dwarf2_debug_info_t
* di
)
2108 switch (di
->abbrev
->tag
)
2110 case DW_TAG_typedef
:
2111 dwarf2_parse_typedef(ctx
, di
);
2113 case DW_TAG_base_type
:
2114 dwarf2_parse_base_type(ctx
, di
);
2116 case DW_TAG_pointer_type
:
2117 dwarf2_parse_pointer_type(ctx
, di
);
2119 case DW_TAG_class_type
:
2120 dwarf2_parse_udt_type(ctx
, di
, UdtClass
);
2122 case DW_TAG_structure_type
:
2123 dwarf2_parse_udt_type(ctx
, di
, UdtStruct
);
2125 case DW_TAG_union_type
:
2126 dwarf2_parse_udt_type(ctx
, di
, UdtUnion
);
2128 case DW_TAG_array_type
:
2129 dwarf2_parse_array_type(ctx
, di
);
2131 case DW_TAG_const_type
:
2132 dwarf2_parse_const_type(ctx
, di
);
2134 case DW_TAG_volatile_type
:
2135 dwarf2_parse_volatile_type(ctx
, di
);
2137 case DW_TAG_unspecified_type
:
2138 dwarf2_parse_unspecified_type(ctx
, di
);
2140 case DW_TAG_reference_type
:
2141 dwarf2_parse_reference_type(ctx
, di
);
2143 case DW_TAG_enumeration_type
:
2144 dwarf2_parse_enumeration_type(ctx
, di
);
2146 case DW_TAG_subprogram
:
2147 dwarf2_parse_subprogram(ctx
, di
);
2149 case DW_TAG_subroutine_type
:
2150 dwarf2_parse_subroutine_type(ctx
, di
);
2152 case DW_TAG_variable
:
2154 dwarf2_subprogram_t subpgm
;
2158 subpgm
.frame
.kind
= loc_absolute
;
2159 subpgm
.frame
.offset
= 0;
2160 subpgm
.frame
.reg
= Wine_DW_no_register
;
2161 dwarf2_parse_variable(&subpgm
, NULL
, di
);
2164 case DW_TAG_namespace
:
2165 dwarf2_parse_namespace(ctx
, di
);
2167 /* silence a couple of C++ defines */
2168 case DW_TAG_imported_module
:
2169 case DW_TAG_imported_declaration
:
2170 case DW_TAG_ptr_to_member_type
:
2173 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2174 di
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), di
->abbrev
->entry_code
);
2178 static void dwarf2_set_line_number(struct module
* module
, unsigned long address
,
2179 const struct vector
* v
, unsigned file
, unsigned line
)
2181 struct symt_function
* func
;
2182 struct symt_ht
* symt
;
2185 if (!file
|| !(psrc
= vector_at(v
, file
- 1))) return;
2187 TRACE("%s %lx %s %u\n",
2188 debugstr_w(module
->module
.ModuleName
), address
, source_get(module
, *psrc
), line
);
2189 if (!(symt
= symt_find_nearest(module
, address
)) ||
2190 symt
->symt
.tag
!= SymTagFunction
) return;
2191 func
= (struct symt_function
*)symt
;
2192 symt_add_func_line(module
, func
, *psrc
, line
, address
- func
->address
);
2195 static BOOL
dwarf2_parse_line_numbers(const dwarf2_section_t
* sections
,
2196 dwarf2_parse_context_t
* ctx
,
2197 const char* compile_dir
,
2198 unsigned long offset
)
2200 dwarf2_traverse_context_t traverse
;
2201 unsigned long length
;
2202 unsigned insn_size
, default_stmt
;
2203 unsigned line_range
, opcode_base
;
2205 const unsigned char* opcode_len
;
2207 struct vector files
;
2210 /* section with line numbers stripped */
2211 if (sections
[section_line
].address
== IMAGE_NO_MAP
)
2214 if (offset
+ 4 > sections
[section_line
].size
)
2216 WARN("out of bounds offset\n");
2219 traverse
.data
= sections
[section_line
].address
+ offset
;
2220 traverse
.end_data
= traverse
.data
+ 4;
2221 traverse
.word_size
= ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
2223 length
= dwarf2_parse_u4(&traverse
);
2224 traverse
.end_data
= sections
[section_line
].address
+ offset
+ length
;
2226 if (offset
+ 4 + length
> sections
[section_line
].size
)
2228 WARN("out of bounds header\n");
2231 dwarf2_parse_u2(&traverse
); /* version */
2232 dwarf2_parse_u4(&traverse
); /* header_len */
2233 insn_size
= dwarf2_parse_byte(&traverse
);
2234 default_stmt
= dwarf2_parse_byte(&traverse
);
2235 line_base
= (signed char)dwarf2_parse_byte(&traverse
);
2236 line_range
= dwarf2_parse_byte(&traverse
);
2237 opcode_base
= dwarf2_parse_byte(&traverse
);
2239 opcode_len
= traverse
.data
;
2240 traverse
.data
+= opcode_base
- 1;
2242 vector_init(&dirs
, sizeof(const char*), 4);
2243 p
= vector_add(&dirs
, &ctx
->pool
);
2244 *p
= compile_dir
? compile_dir
: ".";
2245 while (*traverse
.data
)
2247 const char* rel
= (const char*)traverse
.data
;
2248 unsigned rellen
= strlen(rel
);
2249 TRACE("Got include %s\n", rel
);
2250 traverse
.data
+= rellen
+ 1;
2251 p
= vector_add(&dirs
, &ctx
->pool
);
2253 if (*rel
== '/' || !compile_dir
)
2257 /* include directory relative to compile directory */
2258 unsigned baselen
= strlen(compile_dir
);
2259 char* tmp
= pool_alloc(&ctx
->pool
, baselen
+ 1 + rellen
+ 1);
2260 strcpy(tmp
, compile_dir
);
2261 if (tmp
[baselen
- 1] != '/') tmp
[baselen
++] = '/';
2262 strcpy(&tmp
[baselen
], rel
);
2269 vector_init(&files
, sizeof(unsigned), 16);
2270 while (*traverse
.data
)
2272 unsigned int dir_index
, mod_time
;
2277 name
= (const char*)traverse
.data
;
2278 traverse
.data
+= strlen(name
) + 1;
2279 dir_index
= dwarf2_leb128_as_unsigned(&traverse
);
2280 mod_time
= dwarf2_leb128_as_unsigned(&traverse
);
2281 length
= dwarf2_leb128_as_unsigned(&traverse
);
2282 dir
= *(const char**)vector_at(&dirs
, dir_index
);
2283 TRACE("Got file %s/%s (%u,%lu)\n", dir
, name
, mod_time
, length
);
2284 psrc
= vector_add(&files
, &ctx
->pool
);
2285 *psrc
= source_new(ctx
->module
, dir
, name
);
2289 while (traverse
.data
< traverse
.end_data
)
2291 unsigned long address
= 0;
2294 unsigned is_stmt
= default_stmt
;
2295 BOOL end_sequence
= FALSE
;
2296 unsigned opcode
, extopcode
, i
;
2298 while (!end_sequence
)
2300 opcode
= dwarf2_parse_byte(&traverse
);
2301 TRACE("Got opcode %x\n", opcode
);
2303 if (opcode
>= opcode_base
)
2305 unsigned delta
= opcode
- opcode_base
;
2307 address
+= (delta
/ line_range
) * insn_size
;
2308 line
+= line_base
+ (delta
% line_range
);
2309 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2316 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2318 case DW_LNS_advance_pc
:
2319 address
+= insn_size
* dwarf2_leb128_as_unsigned(&traverse
);
2321 case DW_LNS_advance_line
:
2322 line
+= dwarf2_leb128_as_signed(&traverse
);
2324 case DW_LNS_set_file
:
2325 file
= dwarf2_leb128_as_unsigned(&traverse
);
2327 case DW_LNS_set_column
:
2328 dwarf2_leb128_as_unsigned(&traverse
);
2330 case DW_LNS_negate_stmt
:
2333 case DW_LNS_set_basic_block
:
2335 case DW_LNS_const_add_pc
:
2336 address
+= ((255 - opcode_base
) / line_range
) * insn_size
;
2338 case DW_LNS_fixed_advance_pc
:
2339 address
+= dwarf2_parse_u2(&traverse
);
2341 case DW_LNS_extended_op
:
2342 dwarf2_leb128_as_unsigned(&traverse
);
2343 extopcode
= dwarf2_parse_byte(&traverse
);
2346 case DW_LNE_end_sequence
:
2347 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2348 end_sequence
= TRUE
;
2350 case DW_LNE_set_address
:
2351 address
= ctx
->load_offset
+ dwarf2_parse_addr(&traverse
);
2353 case DW_LNE_define_file
:
2354 FIXME("not handled define file %s\n", traverse
.data
);
2355 traverse
.data
+= strlen((const char *)traverse
.data
) + 1;
2356 dwarf2_leb128_as_unsigned(&traverse
);
2357 dwarf2_leb128_as_unsigned(&traverse
);
2358 dwarf2_leb128_as_unsigned(&traverse
);
2360 case DW_LNE_set_discriminator
:
2364 descr
= dwarf2_leb128_as_unsigned(&traverse
);
2365 WARN("not handled discriminator %x\n", descr
);
2369 FIXME("Unsupported extended opcode %x\n", extopcode
);
2374 WARN("Unsupported opcode %x\n", opcode
);
2375 for (i
= 0; i
< opcode_len
[opcode
]; i
++)
2376 dwarf2_leb128_as_unsigned(&traverse
);
2385 static BOOL
dwarf2_parse_compilation_unit(const dwarf2_section_t
* sections
,
2386 struct module
* module
,
2387 const struct elf_thunk_area
* thunks
,
2388 dwarf2_traverse_context_t
* mod_ctx
,
2389 unsigned long load_offset
)
2391 dwarf2_parse_context_t ctx
;
2392 dwarf2_traverse_context_t abbrev_ctx
;
2393 dwarf2_debug_info_t
* di
;
2394 dwarf2_traverse_context_t cu_ctx
;
2395 const unsigned char* comp_unit_start
= mod_ctx
->data
;
2396 unsigned long cu_length
;
2397 unsigned short cu_version
;
2398 unsigned long cu_abbrev_offset
;
2401 cu_length
= dwarf2_parse_u4(mod_ctx
);
2402 cu_ctx
.data
= mod_ctx
->data
;
2403 cu_ctx
.end_data
= mod_ctx
->data
+ cu_length
;
2404 mod_ctx
->data
+= cu_length
;
2405 cu_version
= dwarf2_parse_u2(&cu_ctx
);
2406 cu_abbrev_offset
= dwarf2_parse_u4(&cu_ctx
);
2407 cu_ctx
.word_size
= dwarf2_parse_byte(&cu_ctx
);
2409 TRACE("Compilation Unit Header found at 0x%x:\n",
2410 (int)(comp_unit_start
- sections
[section_debug
].address
));
2411 TRACE("- length: %lu\n", cu_length
);
2412 TRACE("- version: %u\n", cu_version
);
2413 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset
);
2414 TRACE("- word_size: %u\n", cu_ctx
.word_size
);
2416 if (cu_version
!= 2)
2418 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2423 module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
= cu_ctx
.word_size
;
2424 mod_ctx
->word_size
= cu_ctx
.word_size
;
2426 pool_init(&ctx
.pool
, 65536);
2427 ctx
.sections
= sections
;
2428 ctx
.section
= section_debug
;
2429 ctx
.module
= module
;
2430 ctx
.thunks
= thunks
;
2431 ctx
.load_offset
= load_offset
;
2432 ctx
.ref_offset
= comp_unit_start
- sections
[section_debug
].address
;
2433 memset(ctx
.symt_cache
, 0, sizeof(ctx
.symt_cache
));
2434 ctx
.symt_cache
[sc_void
] = &symt_new_basic(module
, btVoid
, "void", 0)->symt
;
2435 ctx
.cpp_name
= NULL
;
2437 abbrev_ctx
.data
= sections
[section_abbrev
].address
+ cu_abbrev_offset
;
2438 abbrev_ctx
.end_data
= sections
[section_abbrev
].address
+ sections
[section_abbrev
].size
;
2439 abbrev_ctx
.word_size
= cu_ctx
.word_size
;
2440 dwarf2_parse_abbrev_set(&abbrev_ctx
, &ctx
.abbrev_table
, &ctx
.pool
);
2442 sparse_array_init(&ctx
.debug_info_table
, sizeof(dwarf2_debug_info_t
), 128);
2443 dwarf2_read_one_debug_info(&ctx
, &cu_ctx
, NULL
, &di
);
2445 if (di
->abbrev
->tag
== DW_TAG_compile_unit
)
2447 struct attribute name
;
2448 struct vector
* children
;
2449 dwarf2_debug_info_t
* child
= NULL
;
2451 struct attribute stmt_list
, low_pc
;
2452 struct attribute comp_dir
;
2454 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_name
, &name
))
2455 name
.u
.string
= NULL
;
2457 /* get working directory of current compilation unit */
2458 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_comp_dir
, &comp_dir
))
2459 comp_dir
.u
.string
= NULL
;
2461 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_low_pc
, &low_pc
))
2462 low_pc
.u
.uvalue
= 0;
2463 ctx
.compiland
= symt_new_compiland(module
, ctx
.load_offset
+ low_pc
.u
.uvalue
,
2464 source_new(module
, comp_dir
.u
.string
, name
.u
.string
));
2465 di
->symt
= &ctx
.compiland
->symt
;
2466 children
= dwarf2_get_di_children(&ctx
, di
);
2467 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2469 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2470 dwarf2_load_one_entry(&ctx
, child
);
2472 if (dwarf2_find_attribute(&ctx
, di
, DW_AT_stmt_list
, &stmt_list
))
2474 #if defined(__REACTOS__) && defined(__clang__)
2475 unsigned long stmt_list_val
= stmt_list
.u
.uvalue
;
2476 if (stmt_list_val
> module
->module
.BaseOfImage
)
2478 /* FIXME: Clang is recording this as an address, not an offset */
2479 stmt_list_val
-= module
->module
.BaseOfImage
+ sections
[section_line
].rva
;
2481 if (dwarf2_parse_line_numbers(sections
, &ctx
, comp_dir
.u
.string
, stmt_list_val
))
2483 if (dwarf2_parse_line_numbers(sections
, &ctx
, comp_dir
.u
.string
, stmt_list
.u
.uvalue
))
2485 module
->module
.LineNumbers
= TRUE
;
2489 else FIXME("Should have a compilation unit here\n");
2490 pool_destroy(&ctx
.pool
);
2494 static BOOL
dwarf2_lookup_loclist(const struct module_format
* modfmt
, const BYTE
* start
,
2495 unsigned long ip
, dwarf2_traverse_context_t
* lctx
)
2498 const BYTE
* ptr
= start
;
2501 while (ptr
< modfmt
->u
.dwarf2_info
->debug_loc
.address
+ modfmt
->u
.dwarf2_info
->debug_loc
.size
)
2503 beg
= dwarf2_get_addr(ptr
, modfmt
->u
.dwarf2_info
->word_size
); ptr
+= modfmt
->u
.dwarf2_info
->word_size
;
2504 end
= dwarf2_get_addr(ptr
, modfmt
->u
.dwarf2_info
->word_size
); ptr
+= modfmt
->u
.dwarf2_info
->word_size
;
2505 if (!beg
&& !end
) break;
2506 len
= dwarf2_get_u2(ptr
); ptr
+= 2;
2508 if (beg
<= ip
&& ip
< end
)
2511 lctx
->end_data
= ptr
+ len
;
2512 lctx
->word_size
= modfmt
->u
.dwarf2_info
->word_size
;
2517 WARN("Couldn't find ip in location list\n");
2521 static enum location_error
loc_compute_frame(struct process
* pcs
,
2522 const struct module_format
* modfmt
,
2523 const struct symt_function
* func
,
2524 DWORD_PTR ip
, struct location
* frame
)
2526 struct symt
** psym
= NULL
;
2527 struct location
* pframe
;
2528 dwarf2_traverse_context_t lctx
;
2529 enum location_error err
;
2532 for (i
=0; i
<vector_length(&func
->vchildren
); i
++)
2534 psym
= vector_at(&func
->vchildren
, i
);
2535 if ((*psym
)->tag
== SymTagCustom
)
2537 pframe
= &((struct symt_hierarchy_point
*)*psym
)->loc
;
2539 /* First, recompute the frame information, if needed */
2540 switch (pframe
->kind
)
2546 case loc_dwarf2_location_list
:
2547 WARN("Searching loclist for %s\n", func
->hash_elt
.name
);
2548 if (!dwarf2_lookup_loclist(modfmt
,
2549 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ pframe
->offset
,
2551 return loc_err_out_of_scope
;
2552 if ((err
= compute_location(&lctx
, frame
, pcs
->handle
, NULL
)) < 0) return err
;
2553 if (frame
->kind
>= loc_user
)
2555 WARN("Couldn't compute runtime frame location\n");
2556 return loc_err_too_complex
;
2560 WARN("Unsupported frame kind %d\n", pframe
->kind
);
2561 return loc_err_internal
;
2566 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2567 return loc_err_internal
;
2572 RULE_UNSET
, /* not set at all */
2573 RULE_UNDEFINED
, /* undefined value */
2574 RULE_SAME
, /* same value as previous frame */
2575 RULE_CFA_OFFSET
, /* stored at cfa offset */
2576 RULE_OTHER_REG
, /* stored in other register */
2577 RULE_EXPRESSION
, /* address specified by expression */
2578 RULE_VAL_EXPRESSION
/* value specified by expression */
2581 /* make it large enough for all CPUs */
2582 #define NB_FRAME_REGS 64
2583 #define MAX_SAVED_STATES 16
2587 ULONG_PTR cfa_offset
;
2588 unsigned char cfa_reg
;
2589 enum reg_rule cfa_rule
;
2590 enum reg_rule rules
[NB_FRAME_REGS
];
2591 ULONG_PTR regs
[NB_FRAME_REGS
];
2597 ULONG_PTR code_align
;
2598 LONG_PTR data_align
;
2599 unsigned char retaddr_reg
;
2600 unsigned char fde_encoding
;
2601 unsigned char lsda_encoding
;
2602 unsigned char signal_frame
;
2603 unsigned char aug_z_format
;
2604 unsigned char state_sp
;
2605 struct frame_state state
;
2606 struct frame_state state_stack
[MAX_SAVED_STATES
];
2609 static ULONG_PTR
dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t
* ctx
, unsigned char encoding
)
2613 if (encoding
== DW_EH_PE_omit
) return 0;
2615 switch (encoding
& 0xf0)
2620 case DW_EH_PE_pcrel
:
2621 base
= (ULONG_PTR
)ctx
->data
;
2624 FIXME("unsupported encoding %02x\n", encoding
);
2628 switch (encoding
& 0x0f)
2630 case DW_EH_PE_native
:
2631 return base
+ dwarf2_parse_addr(ctx
);
2632 case DW_EH_PE_leb128
:
2633 return base
+ dwarf2_leb128_as_unsigned(ctx
);
2634 case DW_EH_PE_data2
:
2635 return base
+ dwarf2_parse_u2(ctx
);
2636 case DW_EH_PE_data4
:
2637 return base
+ dwarf2_parse_u4(ctx
);
2638 case DW_EH_PE_data8
:
2639 return base
+ dwarf2_parse_u8(ctx
);
2640 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
2641 return base
+ dwarf2_leb128_as_signed(ctx
);
2642 case DW_EH_PE_signed
|DW_EH_PE_data2
:
2643 return base
+ (signed short)dwarf2_parse_u2(ctx
);
2644 case DW_EH_PE_signed
|DW_EH_PE_data4
:
2645 return base
+ (signed int)dwarf2_parse_u4(ctx
);
2646 case DW_EH_PE_signed
|DW_EH_PE_data8
:
2647 return base
+ (LONG64
)dwarf2_parse_u8(ctx
);
2649 FIXME("unsupported encoding %02x\n", encoding
);
2654 static BOOL
parse_cie_details(dwarf2_traverse_context_t
* ctx
, struct frame_info
* info
)
2656 unsigned char version
;
2657 const char* augmentation
;
2658 const unsigned char* end
;
2661 memset(info
, 0, sizeof(*info
));
2662 info
->lsda_encoding
= DW_EH_PE_omit
;
2663 info
->aug_z_format
= 0;
2665 /* parse the CIE first */
2666 version
= dwarf2_parse_byte(ctx
);
2667 if (version
!= 1 && version
!= 3 && version
!= 4)
2669 FIXME("unknown CIE version %u at %p\n", version
, ctx
->data
- 1);
2672 augmentation
= (const char*)ctx
->data
;
2673 ctx
->data
+= strlen(augmentation
) + 1;
2678 /* skip 'address_size' and 'segment_size' */
2683 info
->code_align
= dwarf2_leb128_as_unsigned(ctx
);
2684 info
->data_align
= dwarf2_leb128_as_signed(ctx
);
2685 info
->retaddr_reg
= version
== 1 ? dwarf2_parse_byte(ctx
) :dwarf2_leb128_as_unsigned(ctx
);
2690 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
2693 TRACE("\tparsing augmentation %s\n", augmentation
);
2694 if (*augmentation
) do
2696 switch (*augmentation
)
2699 len
= dwarf2_leb128_as_unsigned(ctx
);
2700 end
= ctx
->data
+ len
;
2701 info
->aug_z_format
= 1;
2704 info
->lsda_encoding
= dwarf2_parse_byte(ctx
);
2708 unsigned char encoding
= dwarf2_parse_byte(ctx
);
2709 /* throw away the indirect bit, as we don't care for the result */
2710 encoding
&= ~DW_EH_PE_indirect
;
2711 dwarf2_parse_augmentation_ptr(ctx
, encoding
); /* handler */
2715 info
->fde_encoding
= dwarf2_parse_byte(ctx
);
2718 info
->signal_frame
= 1;
2721 FIXME("unknown augmentation '%c'\n", *augmentation
);
2722 if (!end
) return FALSE
;
2724 } while (*++augmentation
);
2725 if (end
) ctx
->data
= end
;
2729 static BOOL
dwarf2_get_cie(unsigned long addr
, struct module
* module
, DWORD_PTR delta
,
2730 dwarf2_traverse_context_t
* fde_ctx
, dwarf2_traverse_context_t
* cie_ctx
,
2731 struct frame_info
* info
, BOOL in_eh_frame
)
2733 const unsigned char* ptr_blk
;
2734 const unsigned char* cie_ptr
;
2735 const unsigned char* last_cie_ptr
= (const unsigned char*)~0;
2737 unsigned long start
, range
;
2739 const BYTE
* start_data
= fde_ctx
->data
;
2741 cie_id
= in_eh_frame
? 0 : DW_CIE_ID
;
2742 /* skip 0-padding at beginning of section (alignment) */
2743 while (fde_ctx
->data
+ 2 * 4 < fde_ctx
->end_data
)
2745 if (dwarf2_parse_u4(fde_ctx
))
2751 for (; fde_ctx
->data
+ 2 * 4 < fde_ctx
->end_data
; fde_ctx
->data
= ptr_blk
)
2753 /* find the FDE for address addr (skip CIE) */
2754 len
= dwarf2_parse_u4(fde_ctx
);
2755 if (len
== 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2756 ptr_blk
= fde_ctx
->data
+ len
;
2757 id
= dwarf2_parse_u4(fde_ctx
);
2760 last_cie_ptr
= fde_ctx
->data
- 8;
2761 /* we need some bits out of the CIE in order to parse all contents */
2762 if (!parse_cie_details(fde_ctx
, info
)) return FALSE
;
2763 cie_ctx
->data
= fde_ctx
->data
;
2764 cie_ctx
->end_data
= ptr_blk
;
2765 cie_ctx
->word_size
= fde_ctx
->word_size
;
2768 cie_ptr
= (in_eh_frame
) ? fde_ctx
->data
- id
- 4 : start_data
+ id
;
2769 if (cie_ptr
!= last_cie_ptr
)
2771 last_cie_ptr
= cie_ptr
;
2772 cie_ctx
->data
= cie_ptr
;
2773 cie_ctx
->word_size
= fde_ctx
->word_size
;
2774 cie_ctx
->end_data
= cie_ptr
+ 4;
2775 cie_ctx
->end_data
= cie_ptr
+ 4 + dwarf2_parse_u4(cie_ctx
);
2776 if (dwarf2_parse_u4(cie_ctx
) != cie_id
)
2778 FIXME("wrong CIE pointer at %x from FDE %x\n",
2779 (unsigned)(cie_ptr
- start_data
),
2780 (unsigned)(fde_ctx
->data
- start_data
));
2783 if (!parse_cie_details(cie_ctx
, info
)) return FALSE
;
2785 start
= delta
+ dwarf2_parse_augmentation_ptr(fde_ctx
, info
->fde_encoding
);
2786 range
= dwarf2_parse_augmentation_ptr(fde_ctx
, info
->fde_encoding
& 0x0F);
2788 if (addr
>= start
&& addr
< start
+ range
)
2790 /* reset the FDE context */
2791 fde_ctx
->end_data
= ptr_blk
;
2800 static int valid_reg(ULONG_PTR reg
)
2802 if (reg
>= NB_FRAME_REGS
) FIXME("unsupported reg %lx\n", reg
);
2803 return (reg
< NB_FRAME_REGS
);
2806 static void execute_cfa_instructions(dwarf2_traverse_context_t
* ctx
,
2807 ULONG_PTR last_ip
, struct frame_info
*info
)
2809 while (ctx
->data
< ctx
->end_data
&& info
->ip
<= last_ip
+ info
->signal_frame
)
2811 enum dwarf_call_frame_info op
= dwarf2_parse_byte(ctx
);
2817 case DW_CFA_advance_loc
:
2819 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
2820 TRACE("%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
2826 ULONG_PTR reg
= op
& 0x3f;
2827 LONG_PTR offset
= dwarf2_leb128_as_unsigned(ctx
) * info
->data_align
;
2828 if (!valid_reg(reg
)) break;
2829 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2831 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2833 info
->state
.regs
[reg
] = offset
;
2834 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
2837 case DW_CFA_restore
:
2839 ULONG_PTR reg
= op
& 0x3f;
2840 if (!valid_reg(reg
)) break;
2841 TRACE("%lx: DW_CFA_restore %s\n",
2843 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2844 info
->state
.rules
[reg
] = RULE_UNSET
;
2853 case DW_CFA_set_loc
:
2855 ULONG_PTR loc
= dwarf2_parse_augmentation_ptr(ctx
, info
->fde_encoding
);
2856 TRACE("%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
2860 case DW_CFA_advance_loc1
:
2862 ULONG_PTR offset
= dwarf2_parse_byte(ctx
) * info
->code_align
;
2863 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
2867 case DW_CFA_advance_loc2
:
2869 ULONG_PTR offset
= dwarf2_parse_u2(ctx
) * info
->code_align
;
2870 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
2874 case DW_CFA_advance_loc4
:
2876 ULONG_PTR offset
= dwarf2_parse_u4(ctx
) * info
->code_align
;
2877 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
2881 case DW_CFA_offset_extended
:
2882 case DW_CFA_offset_extended_sf
:
2884 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2885 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf2_leb128_as_unsigned(ctx
) * info
->data_align
2886 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
2887 if (!valid_reg(reg
)) break;
2888 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2890 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2892 info
->state
.regs
[reg
] = offset
;
2893 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
2896 case DW_CFA_restore_extended
:
2898 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2899 if (!valid_reg(reg
)) break;
2900 TRACE("%lx: DW_CFA_restore_extended %s\n",
2902 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2903 info
->state
.rules
[reg
] = RULE_UNSET
;
2906 case DW_CFA_undefined
:
2908 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2909 if (!valid_reg(reg
)) break;
2910 TRACE("%lx: DW_CFA_undefined %s\n",
2912 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2913 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
2916 case DW_CFA_same_value
:
2918 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2919 if (!valid_reg(reg
)) break;
2920 TRACE("%lx: DW_CFA_same_value %s\n",
2922 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2923 info
->state
.regs
[reg
] = reg
;
2924 info
->state
.rules
[reg
] = RULE_SAME
;
2927 case DW_CFA_register
:
2929 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2930 ULONG_PTR reg2
= dwarf2_leb128_as_unsigned(ctx
);
2931 if (!valid_reg(reg
) || !valid_reg(reg2
)) break;
2932 TRACE("%lx: DW_CFA_register %s == %s\n",
2934 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2935 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg2
, TRUE
)));
2936 info
->state
.regs
[reg
] = reg2
;
2937 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
2940 case DW_CFA_remember_state
:
2941 TRACE("%lx: DW_CFA_remember_state\n", info
->ip
);
2942 if (info
->state_sp
>= MAX_SAVED_STATES
)
2943 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info
->ip
);
2945 info
->state_stack
[info
->state_sp
++] = info
->state
;
2947 case DW_CFA_restore_state
:
2948 TRACE("%lx: DW_CFA_restore_state\n", info
->ip
);
2949 if (!info
->state_sp
)
2950 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info
->ip
);
2952 info
->state
= info
->state_stack
[--info
->state_sp
];
2954 case DW_CFA_def_cfa
:
2955 case DW_CFA_def_cfa_sf
:
2957 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2958 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf2_leb128_as_unsigned(ctx
)
2959 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
2960 if (!valid_reg(reg
)) break;
2961 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2963 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2965 info
->state
.cfa_reg
= reg
;
2966 info
->state
.cfa_offset
= offset
;
2967 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
2970 case DW_CFA_def_cfa_register
:
2972 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2973 if (!valid_reg(reg
)) break;
2974 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2976 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2977 info
->state
.cfa_reg
= reg
;
2978 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
2981 case DW_CFA_def_cfa_offset
:
2982 case DW_CFA_def_cfa_offset_sf
:
2984 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf2_leb128_as_unsigned(ctx
)
2985 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
2986 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info
->ip
, offset
);
2987 info
->state
.cfa_offset
= offset
;
2988 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
2991 case DW_CFA_def_cfa_expression
:
2993 ULONG_PTR expr
= (ULONG_PTR
)ctx
->data
;
2994 ULONG_PTR len
= dwarf2_leb128_as_unsigned(ctx
);
2995 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info
->ip
, expr
, expr
+len
);
2996 info
->state
.cfa_offset
= expr
;
2997 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
3001 case DW_CFA_expression
:
3002 case DW_CFA_val_expression
:
3004 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
3005 ULONG_PTR expr
= (ULONG_PTR
)ctx
->data
;
3006 ULONG_PTR len
= dwarf2_leb128_as_unsigned(ctx
);
3007 if (!valid_reg(reg
)) break;
3008 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
3009 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_",
3010 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
3012 info
->state
.regs
[reg
] = expr
;
3013 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
3017 case DW_CFA_GNU_args_size
:
3018 /* FIXME: should check that GCC is the compiler for this CU */
3020 ULONG_PTR args
= dwarf2_leb128_as_unsigned(ctx
);
3021 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info
->ip
, args
);
3026 FIXME("%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
3032 /* retrieve a context register from its dwarf number */
3033 static ULONG_PTR
get_context_reg(CONTEXT
*context
, ULONG_PTR dw_reg
)
3035 unsigned regno
= dbghelp_current_cpu
->map_dwarf_register(dw_reg
, TRUE
), sz
;
3036 ULONG_PTR
* ptr
= dbghelp_current_cpu
->fetch_context_reg(context
, regno
, &sz
);
3038 if (sz
!= sizeof(ULONG_PTR
))
3040 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg
, regno
, sz
);
3046 /* set a context register from its dwarf number */
3047 static void set_context_reg(struct cpu_stack_walk
* csw
, CONTEXT
*context
, ULONG_PTR dw_reg
,
3048 ULONG_PTR val
, BOOL isdebuggee
)
3050 unsigned regno
= dbghelp_current_cpu
->map_dwarf_register(dw_reg
, TRUE
), sz
;
3051 ULONG_PTR
* ptr
= dbghelp_current_cpu
->fetch_context_reg(context
, regno
, &sz
);
3057 if (sz
> sizeof(tmp
))
3059 FIXME("register %lu/%u size is too wide: %u\n", dw_reg
, regno
, sz
);
3062 if (!sw_read_mem(csw
, val
, tmp
, sz
))
3064 WARN("Couldn't read memory at %p\n", (void*)val
);
3067 memcpy(ptr
, tmp
, sz
);
3071 if (sz
!= sizeof(ULONG_PTR
))
3073 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg
, regno
, sz
);
3080 /* copy a register from one context to another using dwarf number */
3081 static void copy_context_reg(CONTEXT
*dstcontext
, ULONG_PTR dwregdst
, CONTEXT
* srccontext
, ULONG_PTR dwregsrc
)
3083 unsigned regdstno
= dbghelp_current_cpu
->map_dwarf_register(dwregdst
, TRUE
), szdst
;
3084 unsigned regsrcno
= dbghelp_current_cpu
->map_dwarf_register(dwregsrc
, TRUE
), szsrc
;
3085 ULONG_PTR
* ptrdst
= dbghelp_current_cpu
->fetch_context_reg(dstcontext
, regdstno
, &szdst
);
3086 ULONG_PTR
* ptrsrc
= dbghelp_current_cpu
->fetch_context_reg(srccontext
, regsrcno
, &szsrc
);
3090 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3091 dwregsrc
, regsrcno
, dwregdst
, regdstno
, szsrc
, szdst
);
3094 memcpy(ptrdst
, ptrsrc
, szdst
);
3097 static ULONG_PTR
eval_expression(const struct module
* module
, struct cpu_stack_walk
* csw
,
3098 const unsigned char* zp
, CONTEXT
*context
)
3100 dwarf2_traverse_context_t ctx
;
3101 ULONG_PTR reg
, sz
, tmp
, stack
[64];
3106 ctx
.end_data
= zp
+ 4;
3107 len
= dwarf2_leb128_as_unsigned(&ctx
);
3108 ctx
.end_data
= ctx
.data
+ len
;
3109 ctx
.word_size
= module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
3111 while (ctx
.data
< ctx
.end_data
)
3113 unsigned char opcode
= dwarf2_parse_byte(&ctx
);
3115 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
3116 stack
[++sp
] = opcode
- DW_OP_lit0
;
3117 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
3118 stack
[++sp
] = get_context_reg(context
, opcode
- DW_OP_reg0
);
3119 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
3120 stack
[++sp
] = get_context_reg(context
, opcode
- DW_OP_breg0
) + dwarf2_leb128_as_signed(&ctx
);
3121 else switch (opcode
)
3123 case DW_OP_nop
: break;
3124 case DW_OP_addr
: stack
[++sp
] = dwarf2_parse_addr(&ctx
); break;
3125 case DW_OP_const1u
: stack
[++sp
] = dwarf2_parse_byte(&ctx
); break;
3126 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf2_parse_byte(&ctx
); break;
3127 case DW_OP_const2u
: stack
[++sp
] = dwarf2_parse_u2(&ctx
); break;
3128 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf2_parse_u2(&ctx
); break;
3129 case DW_OP_const4u
: stack
[++sp
] = dwarf2_parse_u4(&ctx
); break;
3130 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf2_parse_u4(&ctx
); break;
3131 case DW_OP_const8u
: stack
[++sp
] = dwarf2_parse_u8(&ctx
); break;
3132 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf2_parse_u8(&ctx
); break;
3133 case DW_OP_constu
: stack
[++sp
] = dwarf2_leb128_as_unsigned(&ctx
); break;
3134 case DW_OP_consts
: stack
[++sp
] = dwarf2_leb128_as_signed(&ctx
); break;
3136 if (!sw_read_mem(csw
, stack
[sp
], &tmp
, sizeof(tmp
)))
3138 ERR("Couldn't read memory at %lx\n", stack
[sp
]);
3143 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
3144 case DW_OP_drop
: sp
--; break;
3145 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
3146 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf2_parse_byte(&ctx
)]; sp
++; break;
3147 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
3148 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
3149 case DW_OP_abs
: stack
[sp
] = labs(stack
[sp
]); break;
3150 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
3151 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
3152 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
3153 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
3154 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
3155 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
3156 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
3157 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
3158 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
3159 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
3160 case DW_OP_plus_uconst
: stack
[sp
] += dwarf2_leb128_as_unsigned(&ctx
); break;
3161 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
3162 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
3163 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
3164 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
3165 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
3166 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
3167 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
3168 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
3169 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
3170 case DW_OP_skip
: tmp
= (short)dwarf2_parse_u2(&ctx
); ctx
.data
+= tmp
; break;
3171 case DW_OP_bra
: tmp
= (short)dwarf2_parse_u2(&ctx
); if (!stack
[sp
--]) ctx
.data
+= tmp
; break;
3172 case DW_OP_GNU_encoded_addr
:
3173 tmp
= dwarf2_parse_byte(&ctx
);
3174 stack
[++sp
] = dwarf2_parse_augmentation_ptr(&ctx
, tmp
);
3177 stack
[++sp
] = get_context_reg(context
, dwarf2_leb128_as_unsigned(&ctx
));
3180 reg
= dwarf2_leb128_as_unsigned(&ctx
);
3181 tmp
= dwarf2_leb128_as_signed(&ctx
);
3182 stack
[++sp
] = get_context_reg(context
, reg
) + tmp
;
3184 case DW_OP_deref_size
:
3185 sz
= dwarf2_parse_byte(&ctx
);
3186 if (!sw_read_mem(csw
, stack
[sp
], &tmp
, sz
))
3188 ERR("Couldn't read memory at %lx\n", stack
[sp
]);
3191 /* do integral promotion */
3194 case 1: stack
[sp
] = *(unsigned char*)&tmp
; break;
3195 case 2: stack
[sp
] = *(unsigned short*)&tmp
; break;
3196 case 4: stack
[sp
] = *(unsigned int*)&tmp
; break;
3197 case 8: stack
[sp
] = *(ULONG_PTR
*)&tmp
; break; /* FIXME: won't work on 32bit platform */
3198 default: FIXME("Unknown size for deref 0x%lx\n", sz
);
3202 FIXME("unhandled opcode %02x\n", opcode
);
3208 static void apply_frame_state(const struct module
* module
, struct cpu_stack_walk
* csw
,
3209 CONTEXT
*context
, struct frame_state
*state
, ULONG_PTR
* cfa
)
3213 CONTEXT new_context
= *context
;
3215 switch (state
->cfa_rule
)
3217 case RULE_EXPRESSION
:
3218 *cfa
= eval_expression(module
, csw
, (const unsigned char*)state
->cfa_offset
, context
);
3219 if (!sw_read_mem(csw
, *cfa
, cfa
, sizeof(*cfa
)))
3221 WARN("Couldn't read memory at %p\n", (void*)*cfa
);
3225 case RULE_VAL_EXPRESSION
:
3226 *cfa
= eval_expression(module
, csw
, (const unsigned char*)state
->cfa_offset
, context
);
3229 *cfa
= get_context_reg(context
, state
->cfa_reg
) + state
->cfa_offset
;
3234 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
3236 switch (state
->rules
[i
])
3239 case RULE_UNDEFINED
:
3242 case RULE_CFA_OFFSET
:
3243 set_context_reg(csw
, &new_context
, i
, *cfa
+ state
->regs
[i
], TRUE
);
3245 case RULE_OTHER_REG
:
3246 copy_context_reg(&new_context
, i
, context
, state
->regs
[i
]);
3248 case RULE_EXPRESSION
:
3249 value
= eval_expression(module
, csw
, (const unsigned char*)state
->regs
[i
], context
);
3250 set_context_reg(csw
, &new_context
, i
, value
, TRUE
);
3252 case RULE_VAL_EXPRESSION
:
3253 value
= eval_expression(module
, csw
, (const unsigned char*)state
->regs
[i
], context
);
3254 set_context_reg(csw
, &new_context
, i
, value
, FALSE
);
3258 *context
= new_context
;
3261 /***********************************************************************
3262 * dwarf2_virtual_unwind
3265 BOOL
dwarf2_virtual_unwind(struct cpu_stack_walk
* csw
, ULONG_PTR ip
, CONTEXT
* context
, ULONG_PTR
* cfa
)
3267 struct module_pair pair
;
3268 struct frame_info info
;
3269 dwarf2_traverse_context_t cie_ctx
, fde_ctx
;
3270 struct module_format
* modfmt
;
3271 const unsigned char* end
;
3274 if (!(pair
.pcs
= process_find_by_handle(csw
->hProcess
)) ||
3275 !(pair
.requested
= module_find_by_addr(pair
.pcs
, ip
, DMT_UNKNOWN
)) ||
3276 !module_get_debug(&pair
))
3278 modfmt
= pair
.effective
->format_info
[DFI_DWARF
];
3279 if (!modfmt
) return FALSE
;
3280 memset(&info
, 0, sizeof(info
));
3281 fde_ctx
.data
= modfmt
->u
.dwarf2_info
->eh_frame
.address
;
3282 fde_ctx
.end_data
= fde_ctx
.data
+ modfmt
->u
.dwarf2_info
->eh_frame
.size
;
3283 fde_ctx
.word_size
= modfmt
->u
.dwarf2_info
->word_size
;
3284 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3285 * in this process the IMAGE section at a different address as the one expected by
3288 delta
= pair
.effective
->module
.BaseOfImage
+ modfmt
->u
.dwarf2_info
->eh_frame
.rva
-
3289 (DWORD_PTR
)modfmt
->u
.dwarf2_info
->eh_frame
.address
;
3290 if (!dwarf2_get_cie(ip
, pair
.effective
, delta
, &fde_ctx
, &cie_ctx
, &info
, TRUE
))
3292 fde_ctx
.data
= modfmt
->u
.dwarf2_info
->debug_frame
.address
;
3293 fde_ctx
.end_data
= fde_ctx
.data
+ modfmt
->u
.dwarf2_info
->debug_frame
.size
;
3294 fde_ctx
.word_size
= modfmt
->u
.dwarf2_info
->word_size
;
3295 delta
= pair
.effective
->reloc_delta
;
3296 if (!dwarf2_get_cie(ip
, pair
.effective
, delta
, &fde_ctx
, &cie_ctx
, &info
, FALSE
))
3298 TRACE("Couldn't find information for %lx\n", ip
);
3303 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3304 ip
, info
.ip
, info
.code_align
, info
.data_align
,
3305 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(info
.retaddr_reg
, TRUE
)));
3307 /* if at very beginning of function, return and use default unwinder */
3308 if (ip
== info
.ip
) return FALSE
;
3309 execute_cfa_instructions(&cie_ctx
, ip
, &info
);
3311 if (info
.aug_z_format
) /* get length of augmentation data */
3313 ULONG_PTR len
= dwarf2_leb128_as_unsigned(&fde_ctx
);
3314 end
= fde_ctx
.data
+ len
;
3317 dwarf2_parse_augmentation_ptr(&fde_ctx
, info
.lsda_encoding
); /* handler_data */
3318 if (end
) fde_ctx
.data
= end
;
3320 execute_cfa_instructions(&fde_ctx
, ip
, &info
);
3322 /* if there is no information about retaddr, use default unwinder */
3323 if (info
.state
.rules
[info
.retaddr_reg
] == RULE_UNSET
) return FALSE
;
3325 apply_frame_state(pair
.effective
, csw
, context
, &info
.state
, cfa
);
3330 static void dwarf2_location_compute(struct process
* pcs
,
3331 const struct module_format
* modfmt
,
3332 const struct symt_function
* func
,
3333 struct location
* loc
)
3335 struct location frame
;
3338 dwarf2_traverse_context_t lctx
;
3340 if (!func
->container
|| func
->container
->tag
!= SymTagCompiland
)
3342 WARN("We'd expect function %s's container to exist and be a compiland\n", func
->hash_elt
.name
);
3343 err
= loc_err_internal
;
3347 /* instruction pointer relative to compiland's start */
3348 ip
= pcs
->ctx_frame
.InstructionOffset
- ((struct symt_compiland
*)func
->container
)->address
;
3350 if ((err
= loc_compute_frame(pcs
, modfmt
, func
, ip
, &frame
)) == 0)
3354 case loc_dwarf2_location_list
:
3355 /* Then, if the variable has a location list, find it !! */
3356 if (dwarf2_lookup_loclist(modfmt
,
3357 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ loc
->offset
,
3360 err
= loc_err_out_of_scope
;
3362 case loc_dwarf2_block
:
3363 /* or if we have a copy of an existing block, get ready for it */
3365 unsigned* ptr
= (unsigned*)loc
->offset
;
3367 lctx
.data
= (const BYTE
*)(ptr
+ 1);
3368 lctx
.end_data
= lctx
.data
+ *ptr
;
3369 lctx
.word_size
= modfmt
->u
.dwarf2_info
->word_size
;
3372 /* now get the variable */
3373 err
= compute_location(&lctx
, loc
, pcs
->handle
, &frame
);
3380 WARN("Unsupported local kind %d\n", loc
->kind
);
3381 err
= loc_err_internal
;
3387 loc
->kind
= loc_register
;
3393 static void *zalloc(void *priv
, uInt items
, uInt sz
)
3395 return HeapAlloc(GetProcessHeap(), 0, items
* sz
);
3398 static void zfree(void *priv
, void *addr
)
3400 HeapFree(GetProcessHeap(), 0, addr
);
3403 static inline BOOL
dwarf2_init_zsection(dwarf2_section_t
* section
,
3404 const char* zsectname
,
3405 struct image_section_map
* ism
)
3412 BYTE
*addr
, *sect
= (BYTE
*)image_map_section(ism
);
3413 size_t sz
= image_get_map_size(ism
);
3415 if (sz
<= 12 || memcmp(sect
, "ZLIB", 4))
3417 ERR("invalid compressed section %s\n", zsectname
);
3421 #ifdef WORDS_BIGENDIAN
3422 li
.u
.HighPart
= *(DWORD
*)§
[4];
3423 li
.u
.LowPart
= *(DWORD
*)§
[8];
3425 li
.u
.HighPart
= RtlUlongByteSwap(*(DWORD
*)§
[4]);
3426 li
.u
.LowPart
= RtlUlongByteSwap(*(DWORD
*)§
[8]);
3429 addr
= HeapAlloc(GetProcessHeap(), 0, li
.QuadPart
);
3433 z
.next_in
= §
[12];
3434 z
.avail_in
= sz
- 12;
3439 res
= inflateInit(&z
);
3442 FIXME("inflateInit failed with %i / %s\n", res
, z
.msg
);
3447 z
.next_out
= addr
+ z
.total_out
;
3448 z
.avail_out
= li
.QuadPart
- z
.total_out
;
3449 res
= inflate(&z
, Z_FINISH
);
3450 } while (z
.avail_in
&& res
== Z_STREAM_END
);
3452 if (res
!= Z_STREAM_END
)
3454 FIXME("Decompression failed with %i / %s\n", res
, z
.msg
);
3459 section
->compressed
= TRUE
;
3460 section
->address
= addr
;
3461 section
->rva
= image_get_map_rva(ism
);
3462 section
->size
= z
.total_out
;
3468 HeapFree(GetProcessHeap(), 0, addr
);
3470 image_unmap_section(ism
);
3476 static inline BOOL
dwarf2_init_section(dwarf2_section_t
* section
, struct image_file_map
* fmap
,
3477 const char* sectname
, const char* zsectname
,
3478 struct image_section_map
* ism
)
3480 struct image_section_map local_ism
;
3482 if (!ism
) ism
= &local_ism
;
3484 section
->compressed
= FALSE
;
3485 if (image_find_section(fmap
, sectname
, ism
))
3487 section
->address
= (const BYTE
*)image_map_section(ism
);
3488 section
->size
= image_get_map_size(ism
);
3489 section
->rva
= image_get_map_rva(ism
);
3493 section
->address
= NULL
;
3497 if (zsectname
&& image_find_section(fmap
, zsectname
, ism
))
3500 return dwarf2_init_zsection(section
, zsectname
, ism
);
3502 FIXME("dbghelp not built with zlib, but compressed section found\n" );
3509 static inline void dwarf2_fini_section(dwarf2_section_t
* section
)
3511 if (section
->compressed
)
3512 HeapFree(GetProcessHeap(), 0, (void*)section
->address
);
3515 static void dwarf2_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
3517 dwarf2_fini_section(&modfmt
->u
.dwarf2_info
->debug_loc
);
3518 dwarf2_fini_section(&modfmt
->u
.dwarf2_info
->debug_frame
);
3519 HeapFree(GetProcessHeap(), 0, modfmt
);
3522 BOOL
dwarf2_parse(struct module
* module
, unsigned long load_offset
,
3523 const struct elf_thunk_area
* thunks
,
3524 struct image_file_map
* fmap
)
3526 dwarf2_section_t eh_frame
, section
[section_max
];
3527 dwarf2_traverse_context_t mod_ctx
;
3528 struct image_section_map debug_sect
, debug_str_sect
, debug_abbrev_sect
,
3529 debug_line_sect
, debug_ranges_sect
, eh_frame_sect
;
3531 struct module_format
* dwarf2_modfmt
;
3533 dwarf2_init_section(&eh_frame
, fmap
, ".eh_frame", NULL
, &eh_frame_sect
);
3534 dwarf2_init_section(§ion
[section_debug
], fmap
, ".debug_info", ".zdebug_info", &debug_sect
);
3535 dwarf2_init_section(§ion
[section_abbrev
], fmap
, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect
);
3536 dwarf2_init_section(§ion
[section_string
], fmap
, ".debug_str", ".zdebug_str", &debug_str_sect
);
3537 dwarf2_init_section(§ion
[section_line
], fmap
, ".debug_line", ".zdebug_line", &debug_line_sect
);
3538 dwarf2_init_section(§ion
[section_ranges
], fmap
, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect
);
3540 /* to do anything useful we need either .eh_frame or .debug_info */
3541 if ((!eh_frame
.address
|| eh_frame
.address
== IMAGE_NO_MAP
) &&
3542 (!section
[section_debug
].address
|| section
[section_debug
].address
== IMAGE_NO_MAP
))
3548 if (fmap
->modtype
== DMT_ELF
&& debug_sect
.fmap
)
3550 /* debug info might have a different base address than .so file
3551 * when elf file is prelinked after splitting off debug info
3552 * adjust symbol base addresses accordingly
3554 load_offset
+= fmap
->u
.elf
.elf_start
- debug_sect
.fmap
->u
.elf
.elf_start
;
3557 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module
->module
.ModuleName
));
3559 mod_ctx
.data
= section
[section_debug
].address
;
3560 mod_ctx
.end_data
= mod_ctx
.data
+ section
[section_debug
].size
;
3561 mod_ctx
.word_size
= 0; /* will be correctly set later on */
3563 dwarf2_modfmt
= HeapAlloc(GetProcessHeap(), 0,
3564 sizeof(*dwarf2_modfmt
) + sizeof(*dwarf2_modfmt
->u
.dwarf2_info
));
3570 dwarf2_modfmt
->module
= module
;
3571 dwarf2_modfmt
->remove
= dwarf2_module_remove
;
3572 dwarf2_modfmt
->loc_compute
= dwarf2_location_compute
;
3573 dwarf2_modfmt
->u
.dwarf2_info
= (struct dwarf2_module_info_s
*)(dwarf2_modfmt
+ 1);
3574 dwarf2_modfmt
->u
.dwarf2_info
->word_size
= 0; /* will be correctly set later on */
3575 dwarf2_modfmt
->module
->format_info
[DFI_DWARF
] = dwarf2_modfmt
;
3577 /* As we'll need later some sections' content, we won't unmap these
3578 * sections upon existing this function
3580 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_loc
, fmap
, ".debug_loc", ".zdebug_loc", NULL
);
3581 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_frame
, fmap
, ".debug_frame", ".zdebug_frame", NULL
);
3582 dwarf2_modfmt
->u
.dwarf2_info
->eh_frame
= eh_frame
;
3584 while (mod_ctx
.data
< mod_ctx
.end_data
)
3586 dwarf2_parse_compilation_unit(section
, dwarf2_modfmt
->module
, thunks
, &mod_ctx
, load_offset
);
3588 dwarf2_modfmt
->module
->module
.SymType
= SymDia
;
3589 dwarf2_modfmt
->module
->module
.CVSig
= 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3590 /* FIXME: we could have a finer grain here */
3591 dwarf2_modfmt
->module
->module
.GlobalSymbols
= TRUE
;
3592 dwarf2_modfmt
->module
->module
.TypeInfo
= TRUE
;
3593 dwarf2_modfmt
->module
->module
.SourceIndexed
= TRUE
;
3594 dwarf2_modfmt
->module
->module
.Publics
= TRUE
;
3596 /* set the word_size for eh_frame parsing */
3597 dwarf2_modfmt
->u
.dwarf2_info
->word_size
= fmap
->addr_size
/ 8;
3600 dwarf2_fini_section(§ion
[section_debug
]);
3601 dwarf2_fini_section(§ion
[section_abbrev
]);
3602 dwarf2_fini_section(§ion
[section_string
]);
3603 dwarf2_fini_section(§ion
[section_line
]);
3604 dwarf2_fini_section(§ion
[section_ranges
]);
3606 image_unmap_section(&debug_sect
);
3607 image_unmap_section(&debug_abbrev_sect
);
3608 image_unmap_section(&debug_str_sect
);
3609 image_unmap_section(&debug_line_sect
);
3610 image_unmap_section(&debug_ranges_sect
);
3611 if (!ret
) image_unmap_section(&eh_frame_sect
);