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 #include "dbghelp_private.h"
29 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf
);
33 * o unspecified parameters
35 * o Debug{Start|End}Point
38 * o proper types loading (nesting)
42 static void dump(const void* ptr
, unsigned len
)
46 static const char hexof
[] = "0123456789abcdef";
49 for (i
= 0; i
< len
; i
+= 16)
51 sprintf(msg
, "%08x: ", i
);
52 memset(msg
+ 10, ' ', 3 * 16 + 1 + 16);
53 for (j
= 0; j
< min(16, len
- i
); j
++)
55 msg
[10 + 3 * j
+ 0] = hexof
[x
[i
+ j
] >> 4];
56 msg
[10 + 3 * j
+ 1] = hexof
[x
[i
+ j
] & 15];
57 msg
[10 + 3 * j
+ 2] = ' ';
58 msg
[10 + 3 * 16 + 1 + j
] = (x
[i
+ j
] >= 0x20 && x
[i
+ j
] < 0x7f) ?
61 msg
[10 + 3 * 16] = ' ';
62 msg
[10 + 3 * 16 + 1 + 16] = '\0';
71 * http://www.eagercon.com/dwarf/dwarf3std.htm
72 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
74 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
76 * example of projects who do dwarf2 parsing:
77 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
78 * http://elis.ugent.be/diota/log/ltrace_elf.c
86 typedef struct dwarf2_abbrev_entry_attr_s
88 unsigned long attribute
;
90 struct dwarf2_abbrev_entry_attr_s
* next
;
91 } dwarf2_abbrev_entry_attr_t
;
93 typedef struct dwarf2_abbrev_entry_s
95 unsigned long entry_code
;
97 unsigned char have_child
;
99 dwarf2_abbrev_entry_attr_t
* attrs
;
100 } dwarf2_abbrev_entry_t
;
105 const unsigned char* ptr
;
111 enum {attr_direct
, attr_abstract_origin
, attr_specification
} gotten_from
;
114 unsigned long uvalue
;
118 struct dwarf2_block block
;
122 typedef struct dwarf2_debug_info_s
124 const dwarf2_abbrev_entry_t
*abbrev
;
126 const unsigned char** data
;
127 struct vector children
;
128 struct dwarf2_debug_info_s
* parent
;
129 } dwarf2_debug_info_t
;
131 typedef struct dwarf2_section_s
134 const unsigned char* address
;
139 enum dwarf2_sections
{section_debug
, section_string
, section_abbrev
, section_line
, section_ranges
, section_max
};
141 typedef struct dwarf2_traverse_context_s
143 const unsigned char* data
;
144 const unsigned char* end_data
;
145 unsigned char word_size
;
146 } dwarf2_traverse_context_t
;
148 /* symt_cache indexes */
155 typedef struct dwarf2_parse_context_s
157 const dwarf2_section_t
* sections
;
160 struct module
* module
;
161 struct symt_compiland
* compiland
;
162 const struct elf_thunk_area
*thunks
;
163 struct sparse_array abbrev_table
;
164 struct sparse_array debug_info_table
;
165 unsigned long load_offset
;
166 unsigned long ref_offset
;
167 struct symt
* symt_cache
[sc_num
]; /* void, int1, int2, int4 */
169 } dwarf2_parse_context_t
;
171 /* stored in the dbghelp's module internal structure for later reuse */
172 struct dwarf2_module_info_s
174 dwarf2_section_t debug_loc
;
175 dwarf2_section_t debug_frame
;
176 dwarf2_section_t eh_frame
;
177 unsigned char word_size
;
180 #define loc_dwarf2_location_list (loc_user + 0)
181 #define loc_dwarf2_block (loc_user + 1)
183 /* forward declarations */
184 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_parse_context_t
* ctx
, dwarf2_debug_info_t
* entry
);
186 static unsigned char dwarf2_get_byte(const unsigned char* ptr
)
191 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t
* ctx
)
193 unsigned char uvalue
= dwarf2_get_byte(ctx
->data
);
198 static unsigned short dwarf2_get_u2(const unsigned char* ptr
)
200 return *(const UINT16
*)ptr
;
203 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t
* ctx
)
205 unsigned short uvalue
= dwarf2_get_u2(ctx
->data
);
210 static unsigned long dwarf2_get_u4(const unsigned char* ptr
)
212 return *(const UINT32
*)ptr
;
215 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t
* ctx
)
217 unsigned long uvalue
= dwarf2_get_u4(ctx
->data
);
222 static DWORD64
dwarf2_get_u8(const unsigned char* ptr
)
224 return *(const UINT64
*)ptr
;
227 static DWORD64
dwarf2_parse_u8(dwarf2_traverse_context_t
* ctx
)
229 DWORD64 uvalue
= dwarf2_get_u8(ctx
->data
);
234 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr
, const unsigned char** end
)
236 unsigned long ret
= 0;
242 byte
= dwarf2_get_byte(ptr
++);
243 ret
|= (byte
& 0x7f) << shift
;
245 } while (byte
& 0x80);
251 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t
* ctx
)
257 ret
= dwarf2_get_leb128_as_unsigned(ctx
->data
, &ctx
->data
);
262 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr
, const unsigned char** end
)
267 const unsigned size
= sizeof(int) * 8;
271 byte
= dwarf2_get_byte(ptr
++);
272 ret
|= (byte
& 0x7f) << shift
;
274 } while (byte
& 0x80);
277 /* as spec: sign bit of byte is 2nd high order bit (80x40)
278 * -> 0x80 is used as flag.
280 if ((shift
< size
) && (byte
& 0x40))
282 ret
|= - (1 << shift
);
287 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t
* ctx
)
293 ret
= dwarf2_get_leb128_as_signed(ctx
->data
, &ctx
->data
);
297 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t
* ctx
)
300 for (ret
= 0; ctx
->data
[ret
] & 0x80; ret
++);
304 /******************************************************************
307 * Returns an address.
308 * We assume that in all cases word size from Dwarf matches the size of
309 * addresses in platform where the exec is compiled.
311 static unsigned long dwarf2_get_addr(const unsigned char* ptr
, unsigned word_size
)
318 ret
= dwarf2_get_u4(ptr
);
321 ret
= dwarf2_get_u8(ptr
);
324 FIXME("Unsupported Word Size %u\n", word_size
);
330 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t
* ctx
)
332 unsigned long ret
= dwarf2_get_addr(ctx
->data
, ctx
->word_size
);
333 ctx
->data
+= ctx
->word_size
;
337 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t
* ctx
)
339 return wine_dbg_sprintf("ctx(%p)", ctx
->data
);
342 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t
* ctx
)
344 return wine_dbg_sprintf("ctx(%p,%s)",
345 ctx
, debugstr_w(ctx
->module
->module
.ModuleName
));
348 static const char* dwarf2_debug_di(const dwarf2_debug_info_t
* di
)
350 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
351 di
->abbrev
, di
->symt
);
354 static dwarf2_abbrev_entry_t
*
355 dwarf2_abbrev_table_find_entry(const struct sparse_array
* abbrev_table
,
356 unsigned long entry_code
)
358 assert( NULL
!= abbrev_table
);
359 return sparse_array_find(abbrev_table
, entry_code
);
362 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t
* abbrev_ctx
,
363 struct sparse_array
* abbrev_table
,
366 unsigned long entry_code
;
367 dwarf2_abbrev_entry_t
* abbrev_entry
;
368 dwarf2_abbrev_entry_attr_t
* new = NULL
;
369 dwarf2_abbrev_entry_attr_t
* last
= NULL
;
370 unsigned long attribute
;
373 assert( NULL
!= abbrev_ctx
);
375 TRACE("%s, end at %p\n",
376 dwarf2_debug_traverse_ctx(abbrev_ctx
), abbrev_ctx
->end_data
);
378 sparse_array_init(abbrev_table
, sizeof(dwarf2_abbrev_entry_t
), 32);
379 while (abbrev_ctx
->data
< abbrev_ctx
->end_data
)
381 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
382 entry_code
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
383 TRACE("found entry_code %lu\n", entry_code
);
386 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
389 abbrev_entry
= sparse_array_add(abbrev_table
, entry_code
, pool
);
390 assert( NULL
!= abbrev_entry
);
392 abbrev_entry
->entry_code
= entry_code
;
393 abbrev_entry
->tag
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
394 abbrev_entry
->have_child
= dwarf2_parse_byte(abbrev_ctx
);
395 abbrev_entry
->attrs
= NULL
;
396 abbrev_entry
->num_attr
= 0;
398 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
399 abbrev_table
, sparse_array_length(abbrev_table
),
400 entry_code
, abbrev_entry
->tag
, abbrev_entry
->have_child
, abbrev_entry
);
405 attribute
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
406 form
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
407 if (!attribute
) break;
409 new = pool_alloc(pool
, sizeof(dwarf2_abbrev_entry_attr_t
));
412 new->attribute
= attribute
;
415 if (abbrev_entry
->attrs
) last
->next
= new;
416 else abbrev_entry
->attrs
= new;
418 abbrev_entry
->num_attr
++;
421 TRACE("found %u entries\n", sparse_array_length(abbrev_table
));
424 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t
* ctx
,
425 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
)
429 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr
->attribute
, abbrev_attr
->form
);
431 switch (abbrev_attr
->form
)
433 case DW_FORM_ref_addr
:
434 case DW_FORM_addr
: step
= ctx
->word_size
; break;
437 case DW_FORM_ref1
: step
= 1; break;
439 case DW_FORM_ref2
: step
= 2; break;
442 case DW_FORM_strp
: step
= 4; break;
444 case DW_FORM_ref8
: step
= 8; break;
446 case DW_FORM_ref_udata
:
447 case DW_FORM_udata
: step
= dwarf2_leb128_length(ctx
); break;
448 case DW_FORM_string
: step
= strlen((const char*)ctx
->data
) + 1; break;
449 case DW_FORM_block
: step
= dwarf2_leb128_as_unsigned(ctx
); break;
450 case DW_FORM_block1
: step
= dwarf2_parse_byte(ctx
); break;
451 case DW_FORM_block2
: step
= dwarf2_parse_u2(ctx
); break;
452 case DW_FORM_block4
: step
= dwarf2_parse_u4(ctx
); break;
454 FIXME("Unhandled attribute form %lx\n", abbrev_attr
->form
);
460 static void dwarf2_fill_attr(const dwarf2_parse_context_t
* ctx
,
461 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
,
462 const unsigned char* data
,
463 struct attribute
* attr
)
465 attr
->form
= abbrev_attr
->form
;
468 case DW_FORM_ref_addr
:
470 attr
->u
.uvalue
= dwarf2_get_addr(data
,
471 ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
);
472 TRACE("addr<0x%lx>\n", attr
->u
.uvalue
);
476 attr
->u
.uvalue
= dwarf2_get_byte(data
);
477 TRACE("flag<0x%lx>\n", attr
->u
.uvalue
);
481 attr
->u
.uvalue
= dwarf2_get_byte(data
);
482 TRACE("data1<%lu>\n", attr
->u
.uvalue
);
486 attr
->u
.uvalue
= dwarf2_get_u2(data
);
487 TRACE("data2<%lu>\n", attr
->u
.uvalue
);
491 attr
->u
.uvalue
= dwarf2_get_u4(data
);
492 TRACE("data4<%lu>\n", attr
->u
.uvalue
);
496 attr
->u
.lluvalue
= dwarf2_get_u8(data
);
497 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr
->u
.uvalue
));
501 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_byte(data
);
502 TRACE("ref1<0x%lx>\n", attr
->u
.uvalue
);
506 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u2(data
);
507 TRACE("ref2<0x%lx>\n", attr
->u
.uvalue
);
511 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u4(data
);
512 TRACE("ref4<0x%lx>\n", attr
->u
.uvalue
);
516 FIXME("Unhandled 64-bit support\n");
520 attr
->u
.svalue
= dwarf2_get_leb128_as_signed(data
, NULL
);
523 case DW_FORM_ref_udata
:
524 attr
->u
.uvalue
= dwarf2_get_leb128_as_unsigned(data
, NULL
);
528 attr
->u
.uvalue
= dwarf2_get_leb128_as_unsigned(data
, NULL
);
532 attr
->u
.string
= (const char *)data
;
533 TRACE("string<%s>\n", attr
->u
.string
);
538 unsigned long offset
= dwarf2_get_u4(data
);
539 attr
->u
.string
= (const char*)ctx
->sections
[section_string
].address
+ offset
;
541 TRACE("strp<%s>\n", attr
->u
.string
);
545 attr
->u
.block
.size
= dwarf2_get_leb128_as_unsigned(data
, &attr
->u
.block
.ptr
);
549 attr
->u
.block
.size
= dwarf2_get_byte(data
);
550 attr
->u
.block
.ptr
= data
+ 1;
554 attr
->u
.block
.size
= dwarf2_get_u2(data
);
555 attr
->u
.block
.ptr
= data
+ 2;
559 attr
->u
.block
.size
= dwarf2_get_u4(data
);
560 attr
->u
.block
.ptr
= data
+ 4;
564 FIXME("Unhandled attribute form %lx\n", abbrev_attr
->form
);
569 static BOOL
dwarf2_find_attribute(const dwarf2_parse_context_t
* ctx
,
570 const dwarf2_debug_info_t
* di
,
571 unsigned at
, struct attribute
* attr
)
573 unsigned i
, refidx
= 0;
574 dwarf2_abbrev_entry_attr_t
* abbrev_attr
;
575 dwarf2_abbrev_entry_attr_t
* ref_abbrev_attr
= NULL
;
577 attr
->gotten_from
= attr_direct
;
580 ref_abbrev_attr
= NULL
;
581 for (i
= 0, abbrev_attr
= di
->abbrev
->attrs
; abbrev_attr
; i
++, abbrev_attr
= abbrev_attr
->next
)
583 if (abbrev_attr
->attribute
== at
)
585 dwarf2_fill_attr(ctx
, abbrev_attr
, di
->data
[i
], attr
);
588 if ((abbrev_attr
->attribute
== DW_AT_abstract_origin
||
589 abbrev_attr
->attribute
== DW_AT_specification
) &&
593 FIXME("two references %lx and %lx\n", ref_abbrev_attr
->attribute
, abbrev_attr
->attribute
);
594 ref_abbrev_attr
= abbrev_attr
;
596 attr
->gotten_from
= (abbrev_attr
->attribute
== DW_AT_abstract_origin
) ?
597 attr_abstract_origin
: attr_specification
;
600 /* do we have either an abstract origin or a specification debug entry to look into ? */
601 if (!ref_abbrev_attr
) break;
602 dwarf2_fill_attr(ctx
, ref_abbrev_attr
, di
->data
[refidx
], attr
);
603 if (!(di
= sparse_array_find(&ctx
->debug_info_table
, attr
->u
.uvalue
)))
604 FIXME("Should have found the debug info entry\n");
609 static void dwarf2_load_one_entry(dwarf2_parse_context_t
*, dwarf2_debug_info_t
*);
611 #define Wine_DW_no_register 0x7FFFFFFF
613 static unsigned dwarf2_map_register(int regno
)
615 if (regno
== Wine_DW_no_register
)
617 FIXME("What the heck map reg 0x%x\n",regno
);
620 return dbghelp_current_cpu
->map_dwarf_register(regno
, FALSE
);
623 static enum location_error
624 compute_location(dwarf2_traverse_context_t
* ctx
, struct location
* loc
,
625 HANDLE hproc
, const struct location
* frame
)
627 DWORD_PTR tmp
, stack
[64];
630 BOOL piece_found
= FALSE
;
634 loc
->kind
= loc_absolute
;
635 loc
->reg
= Wine_DW_no_register
;
637 while (ctx
->data
< ctx
->end_data
)
639 op
= dwarf2_parse_byte(ctx
);
641 if (op
>= DW_OP_lit0
&& op
<= DW_OP_lit31
)
642 stack
[++stk
] = op
- DW_OP_lit0
;
643 else if (op
>= DW_OP_reg0
&& op
<= DW_OP_reg31
)
645 /* dbghelp APIs don't know how to cope with this anyway
646 * (for example 'long long' stored in two registers)
647 * FIXME: We should tell winedbg how to deal with it (sigh)
651 DWORD cvreg
= dwarf2_map_register(op
- DW_OP_reg0
);
652 if (loc
->reg
!= Wine_DW_no_register
)
653 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
654 dbghelp_current_cpu
->fetch_regname(loc
->reg
), loc
->reg
,
655 dbghelp_current_cpu
->fetch_regname(cvreg
), cvreg
);
658 loc
->kind
= loc_register
;
660 else if (op
>= DW_OP_breg0
&& op
<= DW_OP_breg31
)
662 /* dbghelp APIs don't know how to cope with this anyway
663 * (for example 'long long' stored in two registers)
664 * FIXME: We should tell winedbg how to deal with it (sigh)
668 DWORD cvreg
= dwarf2_map_register(op
- DW_OP_breg0
);
669 if (loc
->reg
!= Wine_DW_no_register
)
670 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
671 dbghelp_current_cpu
->fetch_regname(loc
->reg
), loc
->reg
,
672 dbghelp_current_cpu
->fetch_regname(cvreg
), cvreg
);
675 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
676 loc
->kind
= loc_regrel
;
680 case DW_OP_nop
: break;
681 case DW_OP_addr
: stack
[++stk
] = dwarf2_parse_addr(ctx
); break;
682 case DW_OP_const1u
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
683 case DW_OP_const1s
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
684 case DW_OP_const2u
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
685 case DW_OP_const2s
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
686 case DW_OP_const4u
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
687 case DW_OP_const4s
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
688 case DW_OP_const8u
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
689 case DW_OP_const8s
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
690 case DW_OP_constu
: stack
[++stk
] = dwarf2_leb128_as_unsigned(ctx
); break;
691 case DW_OP_consts
: stack
[++stk
] = dwarf2_leb128_as_signed(ctx
); break;
692 case DW_OP_dup
: stack
[stk
+ 1] = stack
[stk
]; stk
++; break;
693 case DW_OP_drop
: stk
--; break;
694 case DW_OP_over
: stack
[stk
+ 1] = stack
[stk
- 1]; stk
++; break;
695 case DW_OP_pick
: stack
[stk
+ 1] = stack
[stk
- dwarf2_parse_byte(ctx
)]; stk
++; break;
696 case DW_OP_swap
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = tmp
; break;
697 case DW_OP_rot
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = stack
[stk
-2]; stack
[stk
-2] = tmp
; break;
698 case DW_OP_abs
: stack
[stk
] = labs(stack
[stk
]); break;
699 case DW_OP_neg
: stack
[stk
] = -stack
[stk
]; break;
700 case DW_OP_not
: stack
[stk
] = ~stack
[stk
]; break;
701 case DW_OP_and
: stack
[stk
-1] &= stack
[stk
]; stk
--; break;
702 case DW_OP_or
: stack
[stk
-1] |= stack
[stk
]; stk
--; break;
703 case DW_OP_minus
: stack
[stk
-1] -= stack
[stk
]; stk
--; break;
704 case DW_OP_mul
: stack
[stk
-1] *= stack
[stk
]; stk
--; break;
705 case DW_OP_plus
: stack
[stk
-1] += stack
[stk
]; stk
--; break;
706 case DW_OP_xor
: stack
[stk
-1] ^= stack
[stk
]; stk
--; break;
707 case DW_OP_shl
: stack
[stk
-1] <<= stack
[stk
]; stk
--; break;
708 case DW_OP_shr
: stack
[stk
-1] >>= stack
[stk
]; stk
--; break;
709 case DW_OP_plus_uconst
: stack
[stk
] += dwarf2_leb128_as_unsigned(ctx
); break;
710 case DW_OP_shra
: stack
[stk
-1] = stack
[stk
-1] / (1 << stack
[stk
]); stk
--; break;
711 case DW_OP_div
: stack
[stk
-1] = stack
[stk
-1] / stack
[stk
]; stk
--; break;
712 case DW_OP_mod
: stack
[stk
-1] = stack
[stk
-1] % stack
[stk
]; stk
--; break;
713 case DW_OP_ge
: stack
[stk
-1] = (stack
[stk
-1] >= stack
[stk
]); stk
--; break;
714 case DW_OP_gt
: stack
[stk
-1] = (stack
[stk
-1] > stack
[stk
]); stk
--; break;
715 case DW_OP_le
: stack
[stk
-1] = (stack
[stk
-1] <= stack
[stk
]); stk
--; break;
716 case DW_OP_lt
: stack
[stk
-1] = (stack
[stk
-1] < stack
[stk
]); stk
--; break;
717 case DW_OP_eq
: stack
[stk
-1] = (stack
[stk
-1] == stack
[stk
]); stk
--; break;
718 case DW_OP_ne
: stack
[stk
-1] = (stack
[stk
-1] != stack
[stk
]); stk
--; break;
719 case DW_OP_skip
: tmp
= dwarf2_parse_u2(ctx
); ctx
->data
+= tmp
; break;
720 case DW_OP_bra
: tmp
= dwarf2_parse_u2(ctx
); if (!stack
[stk
--]) ctx
->data
+= tmp
; break;
722 tmp
= dwarf2_leb128_as_unsigned(ctx
);
725 if (loc
->reg
!= Wine_DW_no_register
)
726 FIXME("Only supporting one reg\n");
727 loc
->reg
= dwarf2_map_register(tmp
);
729 loc
->kind
= loc_register
;
732 tmp
= dwarf2_leb128_as_unsigned(ctx
);
733 if (loc
->reg
!= Wine_DW_no_register
)
734 FIXME("Only supporting one regx\n");
735 loc
->reg
= dwarf2_map_register(tmp
);
736 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
737 loc
->kind
= loc_regrel
;
740 if (loc
->reg
!= Wine_DW_no_register
)
741 FIXME("Only supporting one reg (%s/%d -> -2)\n",
742 dbghelp_current_cpu
->fetch_regname(loc
->reg
), loc
->reg
);
743 if (frame
&& frame
->kind
== loc_register
)
745 loc
->kind
= loc_regrel
;
746 loc
->reg
= frame
->reg
;
747 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
749 else if (frame
&& frame
->kind
== loc_regrel
)
751 loc
->kind
= loc_regrel
;
752 loc
->reg
= frame
->reg
;
753 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
) + frame
->offset
;
757 /* FIXME: this could be later optimized by not recomputing
758 * this very location expression
760 loc
->kind
= loc_dwarf2_block
;
761 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
766 unsigned sz
= dwarf2_leb128_as_unsigned(ctx
);
767 WARN("Not handling OP_piece (size=%d)\n", sz
);
774 FIXME("Unexpected empty stack\n");
775 return loc_err_internal
;
777 if (loc
->reg
!= Wine_DW_no_register
)
779 WARN("Too complex expression for deref\n");
780 return loc_err_too_complex
;
784 DWORD_PTR addr
= stack
[stk
--];
787 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, sizeof(deref
), NULL
))
789 WARN("Couldn't read memory at %lx\n", addr
);
790 return loc_err_cant_read
;
792 stack
[++stk
] = deref
;
796 loc
->kind
= loc_dwarf2_block
;
799 case DW_OP_deref_size
:
802 FIXME("Unexpected empty stack\n");
803 return loc_err_internal
;
805 if (loc
->reg
!= Wine_DW_no_register
)
807 WARN("Too complex expression for deref\n");
808 return loc_err_too_complex
;
812 DWORD_PTR addr
= stack
[stk
--];
813 BYTE derefsize
= dwarf2_parse_byte(ctx
);
816 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, derefsize
, NULL
))
818 WARN("Couldn't read memory at %lx\n", addr
);
819 return loc_err_cant_read
;
824 case 1: stack
[++stk
] = *(unsigned char*)&deref
; break;
825 case 2: stack
[++stk
] = *(unsigned short*)&deref
; break;
826 case 4: stack
[++stk
] = *(DWORD
*)&deref
; break;
827 case 8: if (ctx
->word_size
>= derefsize
) stack
[++stk
] = deref
; break;
832 dwarf2_parse_byte(ctx
);
833 loc
->kind
= loc_dwarf2_block
;
836 case DW_OP_stack_value
:
837 /* Expected behaviour is that this is the last instruction of this
838 * expression and just the "top of stack" value should be put to loc->offset. */
841 if (op
< DW_OP_lo_user
) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
842 FIXME("Unhandled attr op: %x\n", op
);
843 /* FIXME else unhandled extension */
844 return loc_err_internal
;
847 loc
->offset
= stack
[stk
];
851 static BOOL
dwarf2_compute_location_attr(dwarf2_parse_context_t
* ctx
,
852 const dwarf2_debug_info_t
* di
,
854 struct location
* loc
,
855 const struct location
* frame
)
857 struct attribute xloc
;
859 if (!dwarf2_find_attribute(ctx
, di
, dw
, &xloc
)) return FALSE
;
863 case DW_FORM_data1
: case DW_FORM_data2
:
864 case DW_FORM_udata
: case DW_FORM_sdata
:
865 loc
->kind
= loc_absolute
;
867 loc
->offset
= xloc
.u
.uvalue
;
869 case DW_FORM_data4
: case DW_FORM_data8
:
870 loc
->kind
= loc_dwarf2_location_list
;
871 loc
->reg
= Wine_DW_no_register
;
872 loc
->offset
= xloc
.u
.uvalue
;
879 default: FIXME("Unsupported yet form %lx\n", xloc
.form
);
883 /* assume we have a block form */
885 if (xloc
.u
.block
.size
)
887 dwarf2_traverse_context_t lctx
;
888 enum location_error err
;
890 lctx
.data
= xloc
.u
.block
.ptr
;
891 lctx
.end_data
= xloc
.u
.block
.ptr
+ xloc
.u
.block
.size
;
892 lctx
.word_size
= ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
894 err
= compute_location(&lctx
, loc
, NULL
, frame
);
897 loc
->kind
= loc_error
;
900 else if (loc
->kind
== loc_dwarf2_block
)
902 unsigned* ptr
= pool_alloc(&ctx
->module
->pool
,
903 sizeof(unsigned) + xloc
.u
.block
.size
);
904 *ptr
= xloc
.u
.block
.size
;
905 memcpy(ptr
+ 1, xloc
.u
.block
.ptr
, xloc
.u
.block
.size
);
906 loc
->offset
= (unsigned long)ptr
;
912 static struct symt
* dwarf2_lookup_type(dwarf2_parse_context_t
* ctx
,
913 const dwarf2_debug_info_t
* di
)
915 struct attribute attr
;
916 dwarf2_debug_info_t
* type
;
918 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_type
, &attr
))
920 if (!(type
= sparse_array_find(&ctx
->debug_info_table
, attr
.u
.uvalue
)))
922 FIXME("Unable to find back reference to type %lx\n", attr
.u
.uvalue
);
927 /* load the debug info entity */
928 dwarf2_load_one_entry(ctx
, type
);
930 FIXME("Unable to load forward reference for tag %lx\n", type
->abbrev
->tag
);
935 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t
* ctx
, dwarf2_debug_info_t
* di
, const char* name
)
938 struct attribute diname
;
939 struct attribute spec
;
941 if (di
->abbrev
->tag
== DW_TAG_compile_unit
) return name
;
943 ctx
->cpp_name
= pool_alloc(&ctx
->pool
, MAX_SYM_NAME
);
944 last
= ctx
->cpp_name
+ MAX_SYM_NAME
- strlen(name
) - 1;
947 /* if the di is a definition, but has also a (previous) declaration, then scope must
948 * be gotten from declaration not definition
950 if (dwarf2_find_attribute(ctx
, di
, DW_AT_specification
, &spec
) && spec
.gotten_from
== attr_direct
)
952 di
= sparse_array_find(&ctx
->debug_info_table
, spec
.u
.uvalue
);
955 FIXME("Should have found the debug info entry\n");
960 for (di
= di
->parent
; di
; di
= di
->parent
)
962 switch (di
->abbrev
->tag
)
964 case DW_TAG_namespace
:
965 case DW_TAG_structure_type
:
966 case DW_TAG_class_type
:
967 case DW_TAG_interface_type
:
968 case DW_TAG_union_type
:
969 if (dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &diname
))
971 size_t len
= strlen(diname
.u
.string
);
973 if (last
< ctx
->cpp_name
) return NULL
;
974 memcpy(last
, diname
.u
.string
, len
);
975 last
[len
] = last
[len
+ 1] = ':';
985 /******************************************************************
988 * read a range for a given debug_info (either using AT_range attribute, in which
989 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
990 * in all cases, range is relative to beginning of compilation unit
992 static BOOL
dwarf2_read_range(dwarf2_parse_context_t
* ctx
, const dwarf2_debug_info_t
* di
,
993 unsigned long* plow
, unsigned long* phigh
)
995 struct attribute range
;
997 if (dwarf2_find_attribute(ctx
, di
, DW_AT_ranges
, &range
))
999 dwarf2_traverse_context_t traverse
;
1000 unsigned long low
, high
;
1002 traverse
.data
= ctx
->sections
[section_ranges
].address
+ range
.u
.uvalue
;
1003 traverse
.end_data
= ctx
->sections
[section_ranges
].address
+
1004 ctx
->sections
[section_ranges
].size
;
1005 traverse
.word_size
= ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
1009 while (traverse
.data
+ 2 * traverse
.word_size
< traverse
.end_data
)
1011 low
= dwarf2_parse_addr(&traverse
);
1012 high
= dwarf2_parse_addr(&traverse
);
1013 if (low
== 0 && high
== 0) break;
1014 if (low
== ULONG_MAX
) FIXME("unsupported yet (base address selection)\n");
1015 if (low
< *plow
) *plow
= low
;
1016 if (high
> *phigh
) *phigh
= high
;
1018 if (*plow
== ULONG_MAX
|| *phigh
== 0) {FIXME("no entry found\n"); return FALSE
;}
1019 if (*plow
== *phigh
) {FIXME("entry found, but low=high\n"); return FALSE
;}
1025 struct attribute low_pc
;
1026 struct attribute high_pc
;
1028 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_low_pc
, &low_pc
) ||
1029 !dwarf2_find_attribute(ctx
, di
, DW_AT_high_pc
, &high_pc
))
1031 *plow
= low_pc
.u
.uvalue
;
1032 *phigh
= high_pc
.u
.uvalue
;
1037 /******************************************************************
1038 * dwarf2_read_one_debug_info
1040 * Loads into memory one debug info entry, and recursively its children (if any)
1042 static BOOL
dwarf2_read_one_debug_info(dwarf2_parse_context_t
* ctx
,
1043 dwarf2_traverse_context_t
* traverse
,
1044 dwarf2_debug_info_t
* parent_di
,
1045 dwarf2_debug_info_t
** pdi
)
1047 const dwarf2_abbrev_entry_t
*abbrev
;
1048 unsigned long entry_code
;
1049 unsigned long offset
;
1050 dwarf2_debug_info_t
* di
;
1051 dwarf2_debug_info_t
* child
;
1052 dwarf2_debug_info_t
** where
;
1053 dwarf2_abbrev_entry_attr_t
* attr
;
1055 struct attribute sibling
;
1057 offset
= traverse
->data
- ctx
->sections
[ctx
->section
].address
;
1058 entry_code
= dwarf2_leb128_as_unsigned(traverse
);
1059 TRACE("found entry_code %lu at 0x%lx\n", entry_code
, offset
);
1065 abbrev
= dwarf2_abbrev_table_find_entry(&ctx
->abbrev_table
, entry_code
);
1068 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code
, offset
);
1071 di
= sparse_array_add(&ctx
->debug_info_table
, offset
, &ctx
->pool
);
1072 if (!di
) return FALSE
;
1073 di
->abbrev
= abbrev
;
1075 di
->parent
= parent_di
;
1077 if (abbrev
->num_attr
)
1079 di
->data
= pool_alloc(&ctx
->pool
, abbrev
->num_attr
* sizeof(const char*));
1080 for (i
= 0, attr
= abbrev
->attrs
; attr
; i
++, attr
= attr
->next
)
1082 di
->data
[i
] = traverse
->data
;
1083 dwarf2_swallow_attribute(traverse
, attr
);
1086 else di
->data
= NULL
;
1087 if (abbrev
->have_child
)
1089 vector_init(&di
->children
, sizeof(dwarf2_debug_info_t
*), 16);
1090 while (traverse
->data
< traverse
->end_data
)
1092 if (!dwarf2_read_one_debug_info(ctx
, traverse
, di
, &child
)) return FALSE
;
1094 where
= vector_add(&di
->children
, &ctx
->pool
);
1095 if (!where
) return FALSE
;
1099 if (dwarf2_find_attribute(ctx
, di
, DW_AT_sibling
, &sibling
) &&
1100 traverse
->data
!= ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
)
1102 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1103 dwarf2_debug_traverse_ctx(traverse
), sibling
.u
.uvalue
);
1104 traverse
->data
= ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
;
1110 static struct vector
* dwarf2_get_di_children(dwarf2_parse_context_t
* ctx
,
1111 dwarf2_debug_info_t
* di
)
1113 struct attribute spec
;
1117 if (di
->abbrev
->have_child
)
1118 return &di
->children
;
1119 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_specification
, &spec
)) break;
1120 if (!(di
= sparse_array_find(&ctx
->debug_info_table
, spec
.u
.uvalue
)))
1121 FIXME("Should have found the debug info entry\n");
1126 static struct symt
* dwarf2_parse_base_type(dwarf2_parse_context_t
* ctx
,
1127 dwarf2_debug_info_t
* di
)
1129 struct attribute name
;
1130 struct attribute size
;
1131 struct attribute encoding
;
1134 if (di
->symt
) return di
->symt
;
1136 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1138 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1139 name
.u
.string
= NULL
;
1140 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1141 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_encoding
, &encoding
)) encoding
.u
.uvalue
= DW_ATE_void
;
1143 switch (encoding
.u
.uvalue
)
1145 case DW_ATE_void
: bt
= btVoid
; break;
1146 case DW_ATE_address
: bt
= btULong
; break;
1147 case DW_ATE_boolean
: bt
= btBool
; break;
1148 case DW_ATE_complex_float
: bt
= btComplex
; break;
1149 case DW_ATE_float
: bt
= btFloat
; break;
1150 case DW_ATE_signed
: bt
= btInt
; break;
1151 case DW_ATE_unsigned
: bt
= btUInt
; break;
1152 case DW_ATE_signed_char
: bt
= btChar
; break;
1153 case DW_ATE_unsigned_char
: bt
= btChar
; break;
1154 default: bt
= btNoType
; break;
1156 di
->symt
= &symt_new_basic(ctx
->module
, bt
, name
.u
.string
, size
.u
.uvalue
)->symt
;
1160 assert(size
.u
.uvalue
== 0);
1161 cache_idx
= sc_void
;
1164 switch (size
.u
.uvalue
)
1166 case 1: cache_idx
= sc_int1
; break;
1167 case 2: cache_idx
= sc_int2
; break;
1168 case 4: cache_idx
= sc_int4
; break;
1173 if (cache_idx
!= -1 && !ctx
->symt_cache
[cache_idx
])
1174 ctx
->symt_cache
[cache_idx
] = di
->symt
;
1176 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1180 static struct symt
* dwarf2_parse_typedef(dwarf2_parse_context_t
* ctx
,
1181 dwarf2_debug_info_t
* di
)
1183 struct symt
* ref_type
;
1184 struct attribute name
;
1186 if (di
->symt
) return di
->symt
;
1188 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx
), di
->abbrev
->entry_code
);
1190 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1191 ref_type
= dwarf2_lookup_type(ctx
, di
);
1194 di
->symt
= &symt_new_typedef(ctx
->module
, ref_type
, name
.u
.string
)->symt
;
1195 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1199 static struct symt
* dwarf2_parse_pointer_type(dwarf2_parse_context_t
* ctx
,
1200 dwarf2_debug_info_t
* di
)
1202 struct symt
* ref_type
;
1203 struct attribute size
;
1205 if (di
->symt
) return di
->symt
;
1207 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1209 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= sizeof(void *);
1210 if (!(ref_type
= dwarf2_lookup_type(ctx
, di
)))
1212 ref_type
= ctx
->symt_cache
[sc_void
];
1215 di
->symt
= &symt_new_pointer(ctx
->module
, ref_type
, size
.u
.uvalue
)->symt
;
1216 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1220 static struct symt
* dwarf2_parse_array_type(dwarf2_parse_context_t
* ctx
,
1221 dwarf2_debug_info_t
* di
)
1223 struct symt
* ref_type
;
1224 struct symt
* idx_type
= NULL
;
1225 struct attribute min
, max
, cnt
;
1226 dwarf2_debug_info_t
* child
;
1228 const struct vector
* children
;
1230 if (di
->symt
) return di
->symt
;
1232 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1234 ref_type
= dwarf2_lookup_type(ctx
, di
);
1236 if (!(children
= dwarf2_get_di_children(ctx
, di
)))
1238 /* fake an array with unknown size */
1239 /* FIXME: int4 even on 64bit machines??? */
1240 idx_type
= ctx
->symt_cache
[sc_int4
];
1244 else for (i
= 0; i
< vector_length(children
); i
++)
1246 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1247 switch (child
->abbrev
->tag
)
1249 case DW_TAG_subrange_type
:
1250 idx_type
= dwarf2_lookup_type(ctx
, child
);
1251 if (!dwarf2_find_attribute(ctx
, child
, DW_AT_lower_bound
, &min
))
1253 if (!dwarf2_find_attribute(ctx
, child
, DW_AT_upper_bound
, &max
))
1255 if (dwarf2_find_attribute(ctx
, child
, DW_AT_count
, &cnt
))
1256 max
.u
.uvalue
= min
.u
.uvalue
+ cnt
.u
.uvalue
;
1259 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1260 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1264 di
->symt
= &symt_new_array(ctx
->module
, min
.u
.uvalue
, max
.u
.uvalue
, ref_type
, idx_type
)->symt
;
1268 static struct symt
* dwarf2_parse_const_type(dwarf2_parse_context_t
* ctx
,
1269 dwarf2_debug_info_t
* di
)
1271 struct symt
* ref_type
;
1273 if (di
->symt
) return di
->symt
;
1275 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1277 if (!(ref_type
= dwarf2_lookup_type(ctx
, di
)))
1279 ref_type
= ctx
->symt_cache
[sc_void
];
1282 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1283 di
->symt
= ref_type
;
1288 static struct symt
* dwarf2_parse_volatile_type(dwarf2_parse_context_t
* ctx
,
1289 dwarf2_debug_info_t
* di
)
1291 struct symt
* ref_type
;
1293 if (di
->symt
) return di
->symt
;
1295 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1297 if (!(ref_type
= dwarf2_lookup_type(ctx
, di
)))
1299 ref_type
= ctx
->symt_cache
[sc_void
];
1302 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1303 di
->symt
= ref_type
;
1308 static struct symt
* dwarf2_parse_unspecified_type(dwarf2_parse_context_t
* ctx
,
1309 dwarf2_debug_info_t
* di
)
1311 struct attribute name
;
1312 struct attribute size
;
1313 struct symt_basic
*basic
;
1315 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1317 if (di
->symt
) return di
->symt
;
1319 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1320 name
.u
.string
= "void";
1321 size
.u
.uvalue
= sizeof(void *);
1323 basic
= symt_new_basic(ctx
->module
, btVoid
, name
.u
.string
, size
.u
.uvalue
);
1324 di
->symt
= &basic
->symt
;
1326 if (!ctx
->symt_cache
[sc_void
])
1327 ctx
->symt_cache
[sc_void
] = di
->symt
;
1329 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1333 static struct symt
* dwarf2_parse_reference_type(dwarf2_parse_context_t
* ctx
,
1334 dwarf2_debug_info_t
* di
)
1336 struct symt
* ref_type
= NULL
;
1338 if (di
->symt
) return di
->symt
;
1340 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1342 ref_type
= dwarf2_lookup_type(ctx
, di
);
1343 /* FIXME: for now, we hard-wire C++ references to pointers */
1344 di
->symt
= &symt_new_pointer(ctx
->module
, ref_type
, sizeof(void *))->symt
;
1346 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1351 static void dwarf2_parse_udt_member(dwarf2_parse_context_t
* ctx
,
1352 dwarf2_debug_info_t
* di
,
1353 struct symt_udt
* parent
)
1355 struct symt
* elt_type
;
1356 struct attribute name
;
1357 struct attribute bit_size
;
1358 struct attribute bit_offset
;
1359 struct location loc
;
1363 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1365 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1366 elt_type
= dwarf2_lookup_type(ctx
, di
);
1367 if (dwarf2_compute_location_attr(ctx
, di
, DW_AT_data_member_location
, &loc
, NULL
))
1369 if (loc
.kind
!= loc_absolute
)
1371 FIXME("Found register, while not expecting it\n");
1375 TRACE("found member_location at %s -> %lu\n",
1376 dwarf2_debug_ctx(ctx
), loc
.offset
);
1380 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_bit_size
, &bit_size
))
1381 bit_size
.u
.uvalue
= 0;
1382 if (dwarf2_find_attribute(ctx
, di
, DW_AT_bit_offset
, &bit_offset
))
1384 /* FIXME: we should only do this when implementation is LSB (which is
1385 * the case on i386 processors)
1387 struct attribute nbytes
;
1388 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &nbytes
))
1391 nbytes
.u
.uvalue
= symt_get_info(ctx
->module
, elt_type
, TI_GET_LENGTH
, &size
) ?
1392 (unsigned long)size
: 0;
1394 bit_offset
.u
.uvalue
= nbytes
.u
.uvalue
* 8 - bit_offset
.u
.uvalue
- bit_size
.u
.uvalue
;
1396 else bit_offset
.u
.uvalue
= 0;
1397 symt_add_udt_element(ctx
->module
, parent
, name
.u
.string
, elt_type
,
1398 (loc
.offset
<< 3) + bit_offset
.u
.uvalue
,
1401 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1404 static struct symt
* dwarf2_parse_subprogram(dwarf2_parse_context_t
* ctx
,
1405 dwarf2_debug_info_t
* di
);
1407 static struct symt
* dwarf2_parse_udt_type(dwarf2_parse_context_t
* ctx
,
1408 dwarf2_debug_info_t
* di
,
1411 struct attribute name
;
1412 struct attribute size
;
1413 struct vector
* children
;
1414 dwarf2_debug_info_t
*child
;
1417 if (di
->symt
) return di
->symt
;
1419 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1421 /* quirk... FIXME provide real support for anonymous UDTs */
1422 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1423 name
.u
.string
= "zz_anon_zz";
1424 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1426 di
->symt
= &symt_new_udt(ctx
->module
, dwarf2_get_cpp_name(ctx
, di
, name
.u
.string
),
1427 size
.u
.uvalue
, udt
)->symt
;
1429 children
= dwarf2_get_di_children(ctx
, di
);
1430 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1432 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1434 switch (child
->abbrev
->tag
)
1436 case DW_TAG_array_type
:
1437 dwarf2_parse_array_type(ctx
, di
);
1440 /* FIXME: should I follow the sibling stuff ?? */
1441 dwarf2_parse_udt_member(ctx
, child
, (struct symt_udt
*)di
->symt
);
1443 case DW_TAG_enumeration_type
:
1444 dwarf2_parse_enumeration_type(ctx
, child
);
1446 case DW_TAG_subprogram
:
1447 dwarf2_parse_subprogram(ctx
, child
);
1449 case DW_TAG_const_type
:
1450 dwarf2_parse_const_type(ctx
, child
);
1452 case DW_TAG_structure_type
:
1453 case DW_TAG_class_type
:
1454 case DW_TAG_union_type
:
1455 case DW_TAG_typedef
:
1456 /* FIXME: we need to handle nested udt definitions */
1457 case DW_TAG_inheritance
:
1458 case DW_TAG_template_type_param
:
1459 case DW_TAG_template_value_param
:
1460 case DW_TAG_variable
:
1461 case DW_TAG_imported_declaration
:
1462 case DW_TAG_ptr_to_member_type
:
1463 case DW_TAG_GNU_template_parameter_pack
:
1464 case DW_TAG_GNU_formal_parameter_pack
:
1465 /* FIXME: some C++ related stuff */
1468 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1469 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1477 static void dwarf2_parse_enumerator(dwarf2_parse_context_t
* ctx
,
1478 dwarf2_debug_info_t
* di
,
1479 struct symt_enum
* parent
)
1481 struct attribute name
;
1482 struct attribute value
;
1484 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1486 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) return;
1487 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_const_value
, &value
)) value
.u
.svalue
= 0;
1488 symt_add_enum_element(ctx
->module
, parent
, name
.u
.string
, value
.u
.svalue
);
1490 if (dwarf2_get_di_children(ctx
, di
)) FIXME("Unsupported children\n");
1493 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_parse_context_t
* ctx
,
1494 dwarf2_debug_info_t
* di
)
1496 struct attribute name
;
1497 struct attribute size
;
1498 struct symt_basic
* basetype
;
1499 struct vector
* children
;
1500 dwarf2_debug_info_t
*child
;
1503 if (di
->symt
) return di
->symt
;
1505 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1507 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1508 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 4;
1510 switch (size
.u
.uvalue
) /* FIXME: that's wrong */
1512 case 1: basetype
= symt_new_basic(ctx
->module
, btInt
, "char", 1); break;
1513 case 2: basetype
= symt_new_basic(ctx
->module
, btInt
, "short", 2); break;
1515 case 4: basetype
= symt_new_basic(ctx
->module
, btInt
, "int", 4); break;
1518 di
->symt
= &symt_new_enum(ctx
->module
, name
.u
.string
, &basetype
->symt
)->symt
;
1520 children
= dwarf2_get_di_children(ctx
, di
);
1521 /* FIXME: should we use the sibling stuff ?? */
1522 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1524 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1526 switch (child
->abbrev
->tag
)
1528 case DW_TAG_enumerator
:
1529 dwarf2_parse_enumerator(ctx
, child
, (struct symt_enum
*)di
->symt
);
1532 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1533 di
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1539 /* structure used to pass information around when parsing a subprogram */
1540 typedef struct dwarf2_subprogram_s
1542 dwarf2_parse_context_t
* ctx
;
1543 struct symt_function
* func
;
1544 BOOL non_computed_variable
;
1545 struct location frame
;
1546 } dwarf2_subprogram_t
;
1548 /******************************************************************
1549 * dwarf2_parse_variable
1551 * Parses any variable (parameter, local/global variable)
1553 static void dwarf2_parse_variable(dwarf2_subprogram_t
* subpgm
,
1554 struct symt_block
* block
,
1555 dwarf2_debug_info_t
* di
)
1557 struct symt
* param_type
;
1558 struct attribute name
, value
;
1559 struct location loc
;
1562 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1564 is_pmt
= !block
&& di
->abbrev
->tag
== DW_TAG_formal_parameter
;
1565 param_type
= dwarf2_lookup_type(subpgm
->ctx
, di
);
1567 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_name
, &name
)) {
1568 /* cannot do much without the name, the functions below won't like it. */
1571 if (dwarf2_compute_location_attr(subpgm
->ctx
, di
, DW_AT_location
,
1572 &loc
, &subpgm
->frame
))
1574 struct attribute ext
;
1576 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1577 name
.u
.string
, loc
.kind
, loc
.offset
, loc
.reg
,
1578 dwarf2_debug_ctx(subpgm
->ctx
));
1585 /* it's a global variable */
1586 /* FIXME: we don't handle its scope yet */
1587 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_external
, &ext
))
1589 loc
.offset
+= subpgm
->ctx
->load_offset
;
1590 symt_new_global_variable(subpgm
->ctx
->module
, subpgm
->ctx
->compiland
,
1591 dwarf2_get_cpp_name(subpgm
->ctx
, di
, name
.u
.string
), !ext
.u
.uvalue
,
1592 loc
, 0, param_type
);
1595 subpgm
->non_computed_variable
= TRUE
;
1599 /* either a pmt/variable relative to frame pointer or
1600 * pmt/variable in a register
1602 assert(subpgm
->func
);
1603 symt_add_func_local(subpgm
->ctx
->module
, subpgm
->func
,
1604 is_pmt
? DataIsParam
: DataIsLocal
,
1605 &loc
, block
, param_type
, name
.u
.string
);
1609 else if (dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_const_value
, &value
))
1612 if (subpgm
->func
) WARN("Unsupported constant %s in function\n", name
.u
.string
);
1613 if (is_pmt
) FIXME("Unsupported constant (parameter) %s in function\n", name
.u
.string
);
1621 v
.n1
.n2
.vt
= VT_UI4
;
1622 v
.n1
.n2
.n3
.lVal
= value
.u
.uvalue
;
1626 v
.n1
.n2
.vt
= VT_UI8
;
1627 v
.n1
.n2
.n3
.llVal
= value
.u
.lluvalue
;
1632 v
.n1
.n2
.n3
.lVal
= value
.u
.svalue
;
1636 case DW_FORM_string
:
1637 /* FIXME: native doesn't report const strings from here !!
1638 * however, the value of the string is in the code somewhere
1640 v
.n1
.n2
.vt
= VT_I1
| VT_BYREF
;
1641 v
.n1
.n2
.n3
.byref
= pool_strdup(&subpgm
->ctx
->module
->pool
, value
.u
.string
);
1645 case DW_FORM_block1
:
1646 case DW_FORM_block2
:
1647 case DW_FORM_block4
:
1649 switch (value
.u
.block
.size
)
1651 case 1: v
.n1
.n2
.n3
.lVal
= *(BYTE
*)value
.u
.block
.ptr
; break;
1652 case 2: v
.n1
.n2
.n3
.lVal
= *(USHORT
*)value
.u
.block
.ptr
; break;
1653 case 4: v
.n1
.n2
.n3
.lVal
= *(DWORD
*)value
.u
.block
.ptr
; break;
1655 v
.n1
.n2
.vt
= VT_I1
| VT_BYREF
;
1656 v
.n1
.n2
.n3
.byref
= pool_alloc(&subpgm
->ctx
->module
->pool
, value
.u
.block
.size
);
1657 memcpy(v
.n1
.n2
.n3
.byref
, value
.u
.block
.ptr
, value
.u
.block
.size
);
1662 FIXME("Unsupported form for const value %s (%lx)\n",
1663 name
.u
.string
, value
.form
);
1664 v
.n1
.n2
.vt
= VT_EMPTY
;
1666 di
->symt
= &symt_new_constant(subpgm
->ctx
->module
, subpgm
->ctx
->compiland
,
1667 name
.u
.string
, param_type
, &v
)->symt
;
1671 /* variable has been optimized away... report anyway */
1672 loc
.kind
= loc_error
;
1673 loc
.reg
= loc_err_no_location
;
1676 symt_add_func_local(subpgm
->ctx
->module
, subpgm
->func
,
1677 is_pmt
? DataIsParam
: DataIsLocal
,
1678 &loc
, block
, param_type
, name
.u
.string
);
1682 WARN("dropping global variable %s which has been optimized away\n", name
.u
.string
);
1685 if (is_pmt
&& subpgm
->func
&& subpgm
->func
->type
)
1686 symt_add_function_signature_parameter(subpgm
->ctx
->module
,
1687 (struct symt_function_signature
*)subpgm
->func
->type
,
1690 if (dwarf2_get_di_children(subpgm
->ctx
, di
)) FIXME("Unsupported children\n");
1693 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t
* subpgm
,
1694 const dwarf2_debug_info_t
* di
)
1696 struct attribute name
;
1697 struct attribute low_pc
;
1698 struct location loc
;
1700 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1702 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_low_pc
, &low_pc
)) low_pc
.u
.uvalue
= 0;
1703 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_name
, &name
))
1704 name
.u
.string
= NULL
;
1706 loc
.kind
= loc_absolute
;
1707 loc
.offset
= subpgm
->ctx
->load_offset
+ low_pc
.u
.uvalue
;
1708 symt_add_function_point(subpgm
->ctx
->module
, subpgm
->func
, SymTagLabel
,
1709 &loc
, name
.u
.string
);
1712 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
1713 struct symt_block
* parent_block
,
1714 dwarf2_debug_info_t
* di
);
1716 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_parse_context_t
* ctx
,
1717 dwarf2_debug_info_t
* di
);
1719 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t
* subpgm
,
1720 struct symt_block
* parent_block
,
1721 dwarf2_debug_info_t
* di
)
1723 struct symt_block
* block
;
1724 unsigned long low_pc
, high_pc
;
1725 struct vector
* children
;
1726 dwarf2_debug_info_t
*child
;
1729 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1731 if (!dwarf2_read_range(subpgm
->ctx
, di
, &low_pc
, &high_pc
))
1733 FIXME("cannot read range\n");
1737 block
= symt_open_func_block(subpgm
->ctx
->module
, subpgm
->func
, parent_block
,
1738 subpgm
->ctx
->load_offset
+ low_pc
- subpgm
->func
->address
,
1741 children
= dwarf2_get_di_children(subpgm
->ctx
, di
);
1742 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1744 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1746 switch (child
->abbrev
->tag
)
1748 case DW_TAG_formal_parameter
:
1749 case DW_TAG_variable
:
1750 dwarf2_parse_variable(subpgm
, block
, child
);
1752 case DW_TAG_lexical_block
:
1753 dwarf2_parse_subprogram_block(subpgm
, block
, child
);
1755 case DW_TAG_inlined_subroutine
:
1756 dwarf2_parse_inlined_subroutine(subpgm
, block
, child
);
1759 dwarf2_parse_subprogram_label(subpgm
, child
);
1761 case DW_TAG_GNU_call_site
:
1762 /* this isn't properly supported by dbghelp interface. skip it for now */
1765 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1766 child
->abbrev
->tag
, dwarf2_debug_ctx(subpgm
->ctx
),
1767 dwarf2_debug_di(di
));
1770 symt_close_func_block(subpgm
->ctx
->module
, subpgm
->func
, block
, 0);
1773 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
1774 struct symt_block
* parent_block
,
1775 dwarf2_debug_info_t
* di
)
1777 struct symt_block
* block
;
1778 unsigned long low_pc
, high_pc
;
1779 struct vector
* children
;
1780 dwarf2_debug_info_t
*child
;
1783 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1785 if (!dwarf2_read_range(subpgm
->ctx
, di
, &low_pc
, &high_pc
))
1787 FIXME("no range\n");
1791 block
= symt_open_func_block(subpgm
->ctx
->module
, subpgm
->func
, parent_block
,
1792 subpgm
->ctx
->load_offset
+ low_pc
- subpgm
->func
->address
,
1795 children
= dwarf2_get_di_children(subpgm
->ctx
, di
);
1796 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1798 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1800 switch (child
->abbrev
->tag
)
1802 case DW_TAG_inlined_subroutine
:
1803 dwarf2_parse_inlined_subroutine(subpgm
, block
, child
);
1805 case DW_TAG_variable
:
1806 dwarf2_parse_variable(subpgm
, block
, child
);
1808 case DW_TAG_pointer_type
:
1809 dwarf2_parse_pointer_type(subpgm
->ctx
, di
);
1811 case DW_TAG_subroutine_type
:
1812 dwarf2_parse_subroutine_type(subpgm
->ctx
, di
);
1814 case DW_TAG_const_type
:
1815 dwarf2_parse_const_type(subpgm
->ctx
, di
);
1817 case DW_TAG_lexical_block
:
1818 dwarf2_parse_subprogram_block(subpgm
, block
, child
);
1820 case DW_TAG_subprogram
:
1821 /* FIXME: likely a declaration (to be checked)
1825 case DW_TAG_formal_parameter
:
1826 /* FIXME: likely elements for exception handling (GCC flavor)
1830 case DW_TAG_imported_module
:
1831 /* C++ stuff to be silenced (for now) */
1833 case DW_TAG_GNU_call_site
:
1834 /* this isn't properly supported by dbghelp interface. skip it for now */
1837 dwarf2_parse_subprogram_label(subpgm
, child
);
1839 case DW_TAG_class_type
:
1840 case DW_TAG_structure_type
:
1841 case DW_TAG_union_type
:
1842 case DW_TAG_enumeration_type
:
1843 case DW_TAG_typedef
:
1844 /* the type referred to will be loaded when we need it, so skip it */
1847 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1848 child
->abbrev
->tag
, dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1852 symt_close_func_block(subpgm
->ctx
->module
, subpgm
->func
, block
, 0);
1855 static struct symt
* dwarf2_parse_subprogram(dwarf2_parse_context_t
* ctx
,
1856 dwarf2_debug_info_t
* di
)
1858 struct attribute name
;
1859 unsigned long low_pc
, high_pc
;
1860 struct attribute is_decl
;
1861 struct attribute inline_flags
;
1862 struct symt
* ret_type
;
1863 struct symt_function_signature
* sig_type
;
1864 dwarf2_subprogram_t subpgm
;
1865 struct vector
* children
;
1866 dwarf2_debug_info_t
* child
;
1869 if (di
->symt
) return di
->symt
;
1871 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1873 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1875 WARN("No name for function... dropping function\n");
1878 /* if it's an abstract representation of an inline function, there should be
1879 * a concrete object that we'll handle
1881 if (dwarf2_find_attribute(ctx
, di
, DW_AT_inline
, &inline_flags
) &&
1882 inline_flags
.u
.uvalue
!= DW_INL_not_inlined
)
1884 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1885 name
.u
.string
? name
.u
.string
: "(null)", inline_flags
.u
.uvalue
);
1889 if (dwarf2_find_attribute(ctx
, di
, DW_AT_declaration
, &is_decl
) &&
1890 is_decl
.u
.uvalue
&& is_decl
.gotten_from
== attr_direct
)
1892 /* it's a real declaration, skip it */
1895 if (!dwarf2_read_range(ctx
, di
, &low_pc
, &high_pc
))
1897 WARN("cannot get range for %s\n", name
.u
.string
);
1900 /* As functions (defined as inline assembly) get debug info with dwarf
1901 * (not the case for stabs), we just drop Wine's thunks here...
1902 * Actual thunks will be created in elf_module from the symbol table
1904 #ifndef DBGHELP_STATIC_LIB
1905 if (elf_is_in_thunk_area(ctx
->load_offset
+ low_pc
, ctx
->thunks
) >= 0)
1908 if (!(ret_type
= dwarf2_lookup_type(ctx
, di
)))
1910 ret_type
= ctx
->symt_cache
[sc_void
];
1913 /* FIXME: assuming C source code */
1914 sig_type
= symt_new_function_signature(ctx
->module
, ret_type
, CV_CALL_FAR_C
);
1915 subpgm
.func
= symt_new_function(ctx
->module
, ctx
->compiland
,
1916 dwarf2_get_cpp_name(ctx
, di
, name
.u
.string
),
1917 ctx
->load_offset
+ low_pc
, high_pc
- low_pc
,
1919 di
->symt
= &subpgm
.func
->symt
;
1921 if (!dwarf2_compute_location_attr(ctx
, di
, DW_AT_frame_base
,
1922 &subpgm
.frame
, NULL
))
1925 subpgm
.frame
.kind
= loc_regrel
;
1926 subpgm
.frame
.reg
= dbghelp_current_cpu
->frame_regno
;
1927 subpgm
.frame
.offset
= 0;
1929 subpgm
.non_computed_variable
= FALSE
;
1931 children
= dwarf2_get_di_children(ctx
, di
);
1932 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
1934 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
1936 switch (child
->abbrev
->tag
)
1938 case DW_TAG_variable
:
1939 case DW_TAG_formal_parameter
:
1940 dwarf2_parse_variable(&subpgm
, NULL
, child
);
1942 case DW_TAG_lexical_block
:
1943 dwarf2_parse_subprogram_block(&subpgm
, NULL
, child
);
1945 case DW_TAG_inlined_subroutine
:
1946 dwarf2_parse_inlined_subroutine(&subpgm
, NULL
, child
);
1948 case DW_TAG_pointer_type
:
1949 dwarf2_parse_pointer_type(subpgm
.ctx
, di
);
1951 case DW_TAG_const_type
:
1952 dwarf2_parse_const_type(subpgm
.ctx
, di
);
1954 case DW_TAG_subprogram
:
1955 /* FIXME: likely a declaration (to be checked)
1960 dwarf2_parse_subprogram_label(&subpgm
, child
);
1962 case DW_TAG_class_type
:
1963 case DW_TAG_structure_type
:
1964 case DW_TAG_union_type
:
1965 case DW_TAG_enumeration_type
:
1966 case DW_TAG_typedef
:
1967 /* the type referred to will be loaded when we need it, so skip it */
1969 case DW_TAG_unspecified_parameters
:
1970 case DW_TAG_template_type_param
:
1971 case DW_TAG_template_value_param
:
1972 case DW_TAG_GNU_call_site
:
1973 case DW_TAG_GNU_template_parameter_pack
:
1974 case DW_TAG_GNU_formal_parameter_pack
:
1975 /* FIXME: no support in dbghelp's internals so far */
1978 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1979 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1983 if (subpgm
.non_computed_variable
|| subpgm
.frame
.kind
>= loc_user
)
1985 symt_add_function_point(ctx
->module
, subpgm
.func
, SymTagCustom
,
1986 &subpgm
.frame
, NULL
);
1988 if (subpgm
.func
) symt_normalize_function(subpgm
.ctx
->module
, subpgm
.func
);
1993 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_parse_context_t
* ctx
,
1994 dwarf2_debug_info_t
* di
)
1996 struct symt
* ret_type
;
1997 struct symt_function_signature
* sig_type
;
1998 struct vector
* children
;
1999 dwarf2_debug_info_t
* child
;
2002 if (di
->symt
) return di
->symt
;
2004 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
2006 if (!(ret_type
= dwarf2_lookup_type(ctx
, di
)))
2008 ret_type
= ctx
->symt_cache
[sc_void
];
2012 /* FIXME: assuming C source code */
2013 sig_type
= symt_new_function_signature(ctx
->module
, ret_type
, CV_CALL_FAR_C
);
2015 children
= dwarf2_get_di_children(ctx
, di
);
2016 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2018 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2020 switch (child
->abbrev
->tag
)
2022 case DW_TAG_formal_parameter
:
2023 symt_add_function_signature_parameter(ctx
->module
, sig_type
,
2024 dwarf2_lookup_type(ctx
, child
));
2026 case DW_TAG_unspecified_parameters
:
2027 WARN("Unsupported unspecified parameters\n");
2032 return di
->symt
= &sig_type
->symt
;
2035 static void dwarf2_parse_namespace(dwarf2_parse_context_t
* ctx
,
2036 dwarf2_debug_info_t
* di
)
2038 struct vector
* children
;
2039 dwarf2_debug_info_t
* child
;
2042 if (di
->symt
) return;
2044 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
2046 di
->symt
= ctx
->symt_cache
[sc_void
];
2048 children
= dwarf2_get_di_children(ctx
, di
);
2049 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2051 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2052 dwarf2_load_one_entry(ctx
, child
);
2056 static void dwarf2_load_one_entry(dwarf2_parse_context_t
* ctx
,
2057 dwarf2_debug_info_t
* di
)
2059 switch (di
->abbrev
->tag
)
2061 case DW_TAG_typedef
:
2062 dwarf2_parse_typedef(ctx
, di
);
2064 case DW_TAG_base_type
:
2065 dwarf2_parse_base_type(ctx
, di
);
2067 case DW_TAG_pointer_type
:
2068 dwarf2_parse_pointer_type(ctx
, di
);
2070 case DW_TAG_class_type
:
2071 dwarf2_parse_udt_type(ctx
, di
, UdtClass
);
2073 case DW_TAG_structure_type
:
2074 dwarf2_parse_udt_type(ctx
, di
, UdtStruct
);
2076 case DW_TAG_union_type
:
2077 dwarf2_parse_udt_type(ctx
, di
, UdtUnion
);
2079 case DW_TAG_array_type
:
2080 dwarf2_parse_array_type(ctx
, di
);
2082 case DW_TAG_const_type
:
2083 dwarf2_parse_const_type(ctx
, di
);
2085 case DW_TAG_volatile_type
:
2086 dwarf2_parse_volatile_type(ctx
, di
);
2088 case DW_TAG_unspecified_type
:
2089 dwarf2_parse_unspecified_type(ctx
, di
);
2091 case DW_TAG_reference_type
:
2092 dwarf2_parse_reference_type(ctx
, di
);
2094 case DW_TAG_enumeration_type
:
2095 dwarf2_parse_enumeration_type(ctx
, di
);
2097 case DW_TAG_subprogram
:
2098 dwarf2_parse_subprogram(ctx
, di
);
2100 case DW_TAG_subroutine_type
:
2101 dwarf2_parse_subroutine_type(ctx
, di
);
2103 case DW_TAG_variable
:
2105 dwarf2_subprogram_t subpgm
;
2109 subpgm
.frame
.kind
= loc_absolute
;
2110 subpgm
.frame
.offset
= 0;
2111 subpgm
.frame
.reg
= Wine_DW_no_register
;
2112 dwarf2_parse_variable(&subpgm
, NULL
, di
);
2115 case DW_TAG_namespace
:
2116 dwarf2_parse_namespace(ctx
, di
);
2118 /* silence a couple of C++ defines */
2119 case DW_TAG_imported_module
:
2120 case DW_TAG_imported_declaration
:
2121 case DW_TAG_ptr_to_member_type
:
2124 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2125 di
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), di
->abbrev
->entry_code
);
2129 static void dwarf2_set_line_number(struct module
* module
, unsigned long address
,
2130 const struct vector
* v
, unsigned file
, unsigned line
)
2132 struct symt_function
* func
;
2133 struct symt_ht
* symt
;
2136 if (!file
|| !(psrc
= vector_at(v
, file
- 1))) return;
2138 TRACE("%s %lx %s %u\n",
2139 debugstr_w(module
->module
.ModuleName
), address
, source_get(module
, *psrc
), line
);
2140 if (!(symt
= symt_find_nearest(module
, address
)) ||
2141 symt
->symt
.tag
!= SymTagFunction
) return;
2142 func
= (struct symt_function
*)symt
;
2143 symt_add_func_line(module
, func
, *psrc
, line
, address
- func
->address
);
2146 static BOOL
dwarf2_parse_line_numbers(const dwarf2_section_t
* sections
,
2147 dwarf2_parse_context_t
* ctx
,
2148 const char* compile_dir
,
2149 unsigned long offset
)
2151 dwarf2_traverse_context_t traverse
;
2152 unsigned long length
;
2153 unsigned insn_size
, default_stmt
;
2154 unsigned line_range
, opcode_base
;
2156 const unsigned char* opcode_len
;
2158 struct vector files
;
2161 /* section with line numbers stripped */
2162 if (sections
[section_line
].address
== IMAGE_NO_MAP
)
2165 if (offset
+ 4 > sections
[section_line
].size
)
2167 WARN("out of bounds offset\n");
2170 traverse
.data
= sections
[section_line
].address
+ offset
;
2171 traverse
.end_data
= traverse
.data
+ 4;
2172 traverse
.word_size
= ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
2174 length
= dwarf2_parse_u4(&traverse
);
2175 traverse
.end_data
= sections
[section_line
].address
+ offset
+ length
;
2177 if (offset
+ 4 + length
> sections
[section_line
].size
)
2179 WARN("out of bounds header\n");
2182 dwarf2_parse_u2(&traverse
); /* version */
2183 dwarf2_parse_u4(&traverse
); /* header_len */
2184 insn_size
= dwarf2_parse_byte(&traverse
);
2185 default_stmt
= dwarf2_parse_byte(&traverse
);
2186 line_base
= (signed char)dwarf2_parse_byte(&traverse
);
2187 line_range
= dwarf2_parse_byte(&traverse
);
2188 opcode_base
= dwarf2_parse_byte(&traverse
);
2190 opcode_len
= traverse
.data
;
2191 traverse
.data
+= opcode_base
- 1;
2193 vector_init(&dirs
, sizeof(const char*), 4);
2194 p
= vector_add(&dirs
, &ctx
->pool
);
2195 *p
= compile_dir
? compile_dir
: ".";
2196 while (*traverse
.data
)
2198 const char* rel
= (const char*)traverse
.data
;
2199 unsigned rellen
= strlen(rel
);
2200 TRACE("Got include %s\n", rel
);
2201 traverse
.data
+= rellen
+ 1;
2202 p
= vector_add(&dirs
, &ctx
->pool
);
2204 if (*rel
== '/' || !compile_dir
)
2208 /* include directory relative to compile directory */
2209 unsigned baselen
= strlen(compile_dir
);
2210 char* tmp
= pool_alloc(&ctx
->pool
, baselen
+ 1 + rellen
+ 1);
2211 strcpy(tmp
, compile_dir
);
2212 if (tmp
[baselen
- 1] != '/') tmp
[baselen
++] = '/';
2213 strcpy(&tmp
[baselen
], rel
);
2220 vector_init(&files
, sizeof(unsigned), 16);
2221 while (*traverse
.data
)
2223 unsigned int dir_index
, mod_time
;
2228 name
= (const char*)traverse
.data
;
2229 traverse
.data
+= strlen(name
) + 1;
2230 dir_index
= dwarf2_leb128_as_unsigned(&traverse
);
2231 mod_time
= dwarf2_leb128_as_unsigned(&traverse
);
2232 length
= dwarf2_leb128_as_unsigned(&traverse
);
2233 dir
= *(const char**)vector_at(&dirs
, dir_index
);
2234 TRACE("Got file %s/%s (%u,%lu)\n", dir
, name
, mod_time
, length
);
2235 psrc
= vector_add(&files
, &ctx
->pool
);
2236 *psrc
= source_new(ctx
->module
, dir
, name
);
2240 while (traverse
.data
< traverse
.end_data
)
2242 unsigned long address
= 0;
2245 unsigned is_stmt
= default_stmt
;
2246 BOOL end_sequence
= FALSE
;
2247 unsigned opcode
, extopcode
, i
;
2249 while (!end_sequence
)
2251 opcode
= dwarf2_parse_byte(&traverse
);
2252 TRACE("Got opcode %x\n", opcode
);
2254 if (opcode
>= opcode_base
)
2256 unsigned delta
= opcode
- opcode_base
;
2258 address
+= (delta
/ line_range
) * insn_size
;
2259 line
+= line_base
+ (delta
% line_range
);
2260 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2267 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2269 case DW_LNS_advance_pc
:
2270 address
+= insn_size
* dwarf2_leb128_as_unsigned(&traverse
);
2272 case DW_LNS_advance_line
:
2273 line
+= dwarf2_leb128_as_signed(&traverse
);
2275 case DW_LNS_set_file
:
2276 file
= dwarf2_leb128_as_unsigned(&traverse
);
2278 case DW_LNS_set_column
:
2279 dwarf2_leb128_as_unsigned(&traverse
);
2281 case DW_LNS_negate_stmt
:
2284 case DW_LNS_set_basic_block
:
2286 case DW_LNS_const_add_pc
:
2287 address
+= ((255 - opcode_base
) / line_range
) * insn_size
;
2289 case DW_LNS_fixed_advance_pc
:
2290 address
+= dwarf2_parse_u2(&traverse
);
2292 case DW_LNS_extended_op
:
2293 dwarf2_leb128_as_unsigned(&traverse
);
2294 extopcode
= dwarf2_parse_byte(&traverse
);
2297 case DW_LNE_end_sequence
:
2298 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2299 end_sequence
= TRUE
;
2301 case DW_LNE_set_address
:
2302 address
= ctx
->load_offset
+ dwarf2_parse_addr(&traverse
);
2304 case DW_LNE_define_file
:
2305 FIXME("not handled define file %s\n", traverse
.data
);
2306 traverse
.data
+= strlen((const char *)traverse
.data
) + 1;
2307 dwarf2_leb128_as_unsigned(&traverse
);
2308 dwarf2_leb128_as_unsigned(&traverse
);
2309 dwarf2_leb128_as_unsigned(&traverse
);
2311 case DW_LNE_set_discriminator
:
2315 descr
= dwarf2_leb128_as_unsigned(&traverse
);
2316 WARN("not handled discriminator %x\n", descr
);
2320 FIXME("Unsupported extended opcode %x\n", extopcode
);
2325 WARN("Unsupported opcode %x\n", opcode
);
2326 for (i
= 0; i
< opcode_len
[opcode
]; i
++)
2327 dwarf2_leb128_as_unsigned(&traverse
);
2336 static BOOL
dwarf2_parse_compilation_unit(const dwarf2_section_t
* sections
,
2337 struct module
* module
,
2338 const struct elf_thunk_area
* thunks
,
2339 dwarf2_traverse_context_t
* mod_ctx
,
2340 unsigned long load_offset
)
2342 dwarf2_parse_context_t ctx
;
2343 dwarf2_traverse_context_t abbrev_ctx
;
2344 dwarf2_debug_info_t
* di
;
2345 dwarf2_traverse_context_t cu_ctx
;
2346 const unsigned char* comp_unit_start
= mod_ctx
->data
;
2347 unsigned long cu_length
;
2348 unsigned short cu_version
;
2349 unsigned long cu_abbrev_offset
;
2352 cu_length
= dwarf2_parse_u4(mod_ctx
);
2353 cu_ctx
.data
= mod_ctx
->data
;
2354 cu_ctx
.end_data
= mod_ctx
->data
+ cu_length
;
2355 mod_ctx
->data
+= cu_length
;
2356 cu_version
= dwarf2_parse_u2(&cu_ctx
);
2357 cu_abbrev_offset
= dwarf2_parse_u4(&cu_ctx
);
2358 cu_ctx
.word_size
= dwarf2_parse_byte(&cu_ctx
);
2360 TRACE("Compilation Unit Header found at 0x%x:\n",
2361 (int)(comp_unit_start
- sections
[section_debug
].address
));
2362 TRACE("- length: %lu\n", cu_length
);
2363 TRACE("- version: %u\n", cu_version
);
2364 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset
);
2365 TRACE("- word_size: %u\n", cu_ctx
.word_size
);
2367 if (cu_version
!= 2)
2369 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2374 module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
= cu_ctx
.word_size
;
2375 mod_ctx
->word_size
= cu_ctx
.word_size
;
2377 pool_init(&ctx
.pool
, 65536);
2378 ctx
.sections
= sections
;
2379 ctx
.section
= section_debug
;
2380 ctx
.module
= module
;
2381 ctx
.thunks
= thunks
;
2382 ctx
.load_offset
= load_offset
;
2383 ctx
.ref_offset
= comp_unit_start
- sections
[section_debug
].address
;
2384 memset(ctx
.symt_cache
, 0, sizeof(ctx
.symt_cache
));
2385 ctx
.symt_cache
[sc_void
] = &symt_new_basic(module
, btVoid
, "void", 0)->symt
;
2386 ctx
.cpp_name
= NULL
;
2388 abbrev_ctx
.data
= sections
[section_abbrev
].address
+ cu_abbrev_offset
;
2389 abbrev_ctx
.end_data
= sections
[section_abbrev
].address
+ sections
[section_abbrev
].size
;
2390 abbrev_ctx
.word_size
= cu_ctx
.word_size
;
2391 dwarf2_parse_abbrev_set(&abbrev_ctx
, &ctx
.abbrev_table
, &ctx
.pool
);
2393 sparse_array_init(&ctx
.debug_info_table
, sizeof(dwarf2_debug_info_t
), 128);
2394 dwarf2_read_one_debug_info(&ctx
, &cu_ctx
, NULL
, &di
);
2396 if (di
->abbrev
->tag
== DW_TAG_compile_unit
)
2398 struct attribute name
;
2399 struct vector
* children
;
2400 dwarf2_debug_info_t
* child
= NULL
;
2402 struct attribute stmt_list
, low_pc
;
2403 struct attribute comp_dir
;
2405 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_name
, &name
))
2406 name
.u
.string
= NULL
;
2408 /* get working directory of current compilation unit */
2409 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_comp_dir
, &comp_dir
))
2410 comp_dir
.u
.string
= NULL
;
2412 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_low_pc
, &low_pc
))
2413 low_pc
.u
.uvalue
= 0;
2414 ctx
.compiland
= symt_new_compiland(module
, ctx
.load_offset
+ low_pc
.u
.uvalue
,
2415 source_new(module
, comp_dir
.u
.string
, name
.u
.string
));
2416 di
->symt
= &ctx
.compiland
->symt
;
2417 children
= dwarf2_get_di_children(&ctx
, di
);
2418 if (children
) for (i
= 0; i
< vector_length(children
); i
++)
2420 child
= *(dwarf2_debug_info_t
**)vector_at(children
, i
);
2421 dwarf2_load_one_entry(&ctx
, child
);
2423 if (dwarf2_find_attribute(&ctx
, di
, DW_AT_stmt_list
, &stmt_list
))
2425 #if defined(__REACTOS__) && defined(__clang__)
2426 unsigned long stmt_list_val
= stmt_list
.u
.uvalue
;
2427 if (stmt_list_val
> module
->module
.BaseOfImage
)
2429 /* FIXME: Clang is recording this as an address, not an offset */
2430 stmt_list_val
-= module
->module
.BaseOfImage
+ sections
[section_line
].rva
;
2432 if (dwarf2_parse_line_numbers(sections
, &ctx
, comp_dir
.u
.string
, stmt_list_val
))
2434 if (dwarf2_parse_line_numbers(sections
, &ctx
, comp_dir
.u
.string
, stmt_list
.u
.uvalue
))
2436 module
->module
.LineNumbers
= TRUE
;
2440 else FIXME("Should have a compilation unit here\n");
2441 pool_destroy(&ctx
.pool
);
2445 static BOOL
dwarf2_lookup_loclist(const struct module_format
* modfmt
, const BYTE
* start
,
2446 unsigned long ip
, dwarf2_traverse_context_t
* lctx
)
2449 const BYTE
* ptr
= start
;
2452 while (ptr
< modfmt
->u
.dwarf2_info
->debug_loc
.address
+ modfmt
->u
.dwarf2_info
->debug_loc
.size
)
2454 beg
= dwarf2_get_addr(ptr
, modfmt
->u
.dwarf2_info
->word_size
); ptr
+= modfmt
->u
.dwarf2_info
->word_size
;
2455 end
= dwarf2_get_addr(ptr
, modfmt
->u
.dwarf2_info
->word_size
); ptr
+= modfmt
->u
.dwarf2_info
->word_size
;
2456 if (!beg
&& !end
) break;
2457 len
= dwarf2_get_u2(ptr
); ptr
+= 2;
2459 if (beg
<= ip
&& ip
< end
)
2462 lctx
->end_data
= ptr
+ len
;
2463 lctx
->word_size
= modfmt
->u
.dwarf2_info
->word_size
;
2468 WARN("Couldn't find ip in location list\n");
2472 static enum location_error
loc_compute_frame(struct process
* pcs
,
2473 const struct module_format
* modfmt
,
2474 const struct symt_function
* func
,
2475 DWORD_PTR ip
, struct location
* frame
)
2477 struct symt
** psym
= NULL
;
2478 struct location
* pframe
;
2479 dwarf2_traverse_context_t lctx
;
2480 enum location_error err
;
2483 for (i
=0; i
<vector_length(&func
->vchildren
); i
++)
2485 psym
= vector_at(&func
->vchildren
, i
);
2486 if ((*psym
)->tag
== SymTagCustom
)
2488 pframe
= &((struct symt_hierarchy_point
*)*psym
)->loc
;
2490 /* First, recompute the frame information, if needed */
2491 switch (pframe
->kind
)
2497 case loc_dwarf2_location_list
:
2498 WARN("Searching loclist for %s\n", func
->hash_elt
.name
);
2499 if (!dwarf2_lookup_loclist(modfmt
,
2500 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ pframe
->offset
,
2502 return loc_err_out_of_scope
;
2503 if ((err
= compute_location(&lctx
, frame
, pcs
->handle
, NULL
)) < 0) return err
;
2504 if (frame
->kind
>= loc_user
)
2506 WARN("Couldn't compute runtime frame location\n");
2507 return loc_err_too_complex
;
2511 WARN("Unsupported frame kind %d\n", pframe
->kind
);
2512 return loc_err_internal
;
2517 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2518 return loc_err_internal
;
2523 RULE_UNSET
, /* not set at all */
2524 RULE_UNDEFINED
, /* undefined value */
2525 RULE_SAME
, /* same value as previous frame */
2526 RULE_CFA_OFFSET
, /* stored at cfa offset */
2527 RULE_OTHER_REG
, /* stored in other register */
2528 RULE_EXPRESSION
, /* address specified by expression */
2529 RULE_VAL_EXPRESSION
/* value specified by expression */
2532 /* make it large enough for all CPUs */
2533 #define NB_FRAME_REGS 64
2534 #define MAX_SAVED_STATES 16
2538 ULONG_PTR cfa_offset
;
2539 unsigned char cfa_reg
;
2540 enum reg_rule cfa_rule
;
2541 enum reg_rule rules
[NB_FRAME_REGS
];
2542 ULONG_PTR regs
[NB_FRAME_REGS
];
2548 ULONG_PTR code_align
;
2549 LONG_PTR data_align
;
2550 unsigned char retaddr_reg
;
2551 unsigned char fde_encoding
;
2552 unsigned char lsda_encoding
;
2553 unsigned char signal_frame
;
2554 unsigned char aug_z_format
;
2555 unsigned char state_sp
;
2556 struct frame_state state
;
2557 struct frame_state state_stack
[MAX_SAVED_STATES
];
2560 static ULONG_PTR
dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t
* ctx
, unsigned char encoding
)
2564 if (encoding
== DW_EH_PE_omit
) return 0;
2566 switch (encoding
& 0xf0)
2571 case DW_EH_PE_pcrel
:
2572 base
= (ULONG_PTR
)ctx
->data
;
2575 FIXME("unsupported encoding %02x\n", encoding
);
2579 switch (encoding
& 0x0f)
2581 case DW_EH_PE_native
:
2582 return base
+ dwarf2_parse_addr(ctx
);
2583 case DW_EH_PE_leb128
:
2584 return base
+ dwarf2_leb128_as_unsigned(ctx
);
2585 case DW_EH_PE_data2
:
2586 return base
+ dwarf2_parse_u2(ctx
);
2587 case DW_EH_PE_data4
:
2588 return base
+ dwarf2_parse_u4(ctx
);
2589 case DW_EH_PE_data8
:
2590 return base
+ dwarf2_parse_u8(ctx
);
2591 case DW_EH_PE_signed
|DW_EH_PE_leb128
:
2592 return base
+ dwarf2_leb128_as_signed(ctx
);
2593 case DW_EH_PE_signed
|DW_EH_PE_data2
:
2594 return base
+ (signed short)dwarf2_parse_u2(ctx
);
2595 case DW_EH_PE_signed
|DW_EH_PE_data4
:
2596 return base
+ (signed int)dwarf2_parse_u4(ctx
);
2597 case DW_EH_PE_signed
|DW_EH_PE_data8
:
2598 return base
+ (LONG64
)dwarf2_parse_u8(ctx
);
2600 FIXME("unsupported encoding %02x\n", encoding
);
2605 static BOOL
parse_cie_details(dwarf2_traverse_context_t
* ctx
, struct frame_info
* info
)
2607 unsigned char version
;
2608 const char* augmentation
;
2609 const unsigned char* end
;
2612 memset(info
, 0, sizeof(*info
));
2613 info
->lsda_encoding
= DW_EH_PE_omit
;
2614 info
->aug_z_format
= 0;
2616 /* parse the CIE first */
2617 version
= dwarf2_parse_byte(ctx
);
2618 if (version
!= 1 && version
!= 3)
2620 FIXME("unknown CIE version %u at %p\n", version
, ctx
->data
- 1);
2623 augmentation
= (const char*)ctx
->data
;
2624 ctx
->data
+= strlen(augmentation
) + 1;
2626 info
->code_align
= dwarf2_leb128_as_unsigned(ctx
);
2627 info
->data_align
= dwarf2_leb128_as_signed(ctx
);
2629 info
->retaddr_reg
= dwarf2_parse_byte(ctx
);
2631 info
->retaddr_reg
= dwarf2_leb128_as_unsigned(ctx
);
2632 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
2635 TRACE("\tparsing augmentation %s\n", augmentation
);
2636 if (*augmentation
) do
2638 switch (*augmentation
)
2641 len
= dwarf2_leb128_as_unsigned(ctx
);
2642 end
= ctx
->data
+ len
;
2643 info
->aug_z_format
= 1;
2646 info
->lsda_encoding
= dwarf2_parse_byte(ctx
);
2650 unsigned char encoding
= dwarf2_parse_byte(ctx
);
2651 /* throw away the indirect bit, as we don't care for the result */
2652 encoding
&= ~DW_EH_PE_indirect
;
2653 dwarf2_parse_augmentation_ptr(ctx
, encoding
); /* handler */
2657 info
->fde_encoding
= dwarf2_parse_byte(ctx
);
2660 info
->signal_frame
= 1;
2663 FIXME("unknown augmentation '%c'\n", *augmentation
);
2664 if (!end
) return FALSE
;
2666 } while (*++augmentation
);
2667 if (end
) ctx
->data
= end
;
2671 static BOOL
dwarf2_get_cie(unsigned long addr
, struct module
* module
, DWORD_PTR delta
,
2672 dwarf2_traverse_context_t
* fde_ctx
, dwarf2_traverse_context_t
* cie_ctx
,
2673 struct frame_info
* info
, BOOL in_eh_frame
)
2675 const unsigned char* ptr_blk
;
2676 const unsigned char* cie_ptr
;
2677 const unsigned char* last_cie_ptr
= (const unsigned char*)~0;
2679 unsigned long start
, range
;
2681 const BYTE
* start_data
= fde_ctx
->data
;
2683 cie_id
= in_eh_frame
? 0 : DW_CIE_ID
;
2684 /* skip 0-padding at beginning of section (alignment) */
2685 while (fde_ctx
->data
+ 2 * 4 < fde_ctx
->end_data
)
2687 if (dwarf2_parse_u4(fde_ctx
))
2693 for (; fde_ctx
->data
+ 2 * 4 < fde_ctx
->end_data
; fde_ctx
->data
= ptr_blk
)
2695 /* find the FDE for address addr (skip CIE) */
2696 len
= dwarf2_parse_u4(fde_ctx
);
2697 if (len
== 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2698 ptr_blk
= fde_ctx
->data
+ len
;
2699 id
= dwarf2_parse_u4(fde_ctx
);
2702 last_cie_ptr
= fde_ctx
->data
- 8;
2703 /* we need some bits out of the CIE in order to parse all contents */
2704 if (!parse_cie_details(fde_ctx
, info
)) return FALSE
;
2705 cie_ctx
->data
= fde_ctx
->data
;
2706 cie_ctx
->end_data
= ptr_blk
;
2707 cie_ctx
->word_size
= fde_ctx
->word_size
;
2710 cie_ptr
= (in_eh_frame
) ? fde_ctx
->data
- id
- 4 : start_data
+ id
;
2711 if (cie_ptr
!= last_cie_ptr
)
2713 last_cie_ptr
= cie_ptr
;
2714 cie_ctx
->data
= cie_ptr
;
2715 cie_ctx
->word_size
= fde_ctx
->word_size
;
2716 cie_ctx
->end_data
= cie_ptr
+ 4;
2717 cie_ctx
->end_data
= cie_ptr
+ 4 + dwarf2_parse_u4(cie_ctx
);
2718 if (dwarf2_parse_u4(cie_ctx
) != cie_id
)
2720 FIXME("wrong CIE pointer at %x from FDE %x\n",
2721 (unsigned)(cie_ptr
- start_data
),
2722 (unsigned)(fde_ctx
->data
- start_data
));
2725 if (!parse_cie_details(cie_ctx
, info
)) return FALSE
;
2727 start
= delta
+ dwarf2_parse_augmentation_ptr(fde_ctx
, info
->fde_encoding
);
2728 range
= dwarf2_parse_augmentation_ptr(fde_ctx
, info
->fde_encoding
& 0x0F);
2730 if (addr
>= start
&& addr
< start
+ range
)
2732 /* reset the FDE context */
2733 fde_ctx
->end_data
= ptr_blk
;
2742 static int valid_reg(ULONG_PTR reg
)
2744 if (reg
>= NB_FRAME_REGS
) FIXME("unsupported reg %lx\n", reg
);
2745 return (reg
< NB_FRAME_REGS
);
2748 static void execute_cfa_instructions(dwarf2_traverse_context_t
* ctx
,
2749 ULONG_PTR last_ip
, struct frame_info
*info
)
2751 while (ctx
->data
< ctx
->end_data
&& info
->ip
<= last_ip
+ info
->signal_frame
)
2753 enum dwarf_call_frame_info op
= dwarf2_parse_byte(ctx
);
2759 case DW_CFA_advance_loc
:
2761 ULONG_PTR offset
= (op
& 0x3f) * info
->code_align
;
2762 TRACE("%lx: DW_CFA_advance_loc %lu\n", info
->ip
, offset
);
2768 ULONG_PTR reg
= op
& 0x3f;
2769 LONG_PTR offset
= dwarf2_leb128_as_unsigned(ctx
) * info
->data_align
;
2770 if (!valid_reg(reg
)) break;
2771 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2773 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2775 info
->state
.regs
[reg
] = offset
;
2776 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
2779 case DW_CFA_restore
:
2781 ULONG_PTR reg
= op
& 0x3f;
2782 if (!valid_reg(reg
)) break;
2783 TRACE("%lx: DW_CFA_restore %s\n",
2785 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2786 info
->state
.rules
[reg
] = RULE_UNSET
;
2795 case DW_CFA_set_loc
:
2797 ULONG_PTR loc
= dwarf2_parse_augmentation_ptr(ctx
, info
->fde_encoding
);
2798 TRACE("%lx: DW_CFA_set_loc %lx\n", info
->ip
, loc
);
2802 case DW_CFA_advance_loc1
:
2804 ULONG_PTR offset
= dwarf2_parse_byte(ctx
) * info
->code_align
;
2805 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info
->ip
, offset
);
2809 case DW_CFA_advance_loc2
:
2811 ULONG_PTR offset
= dwarf2_parse_u2(ctx
) * info
->code_align
;
2812 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info
->ip
, offset
);
2816 case DW_CFA_advance_loc4
:
2818 ULONG_PTR offset
= dwarf2_parse_u4(ctx
) * info
->code_align
;
2819 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info
->ip
, offset
);
2823 case DW_CFA_offset_extended
:
2824 case DW_CFA_offset_extended_sf
:
2826 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2827 LONG_PTR offset
= (op
== DW_CFA_offset_extended
) ? dwarf2_leb128_as_unsigned(ctx
) * info
->data_align
2828 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
2829 if (!valid_reg(reg
)) break;
2830 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2832 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2834 info
->state
.regs
[reg
] = offset
;
2835 info
->state
.rules
[reg
] = RULE_CFA_OFFSET
;
2838 case DW_CFA_restore_extended
:
2840 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2841 if (!valid_reg(reg
)) break;
2842 TRACE("%lx: DW_CFA_restore_extended %s\n",
2844 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2845 info
->state
.rules
[reg
] = RULE_UNSET
;
2848 case DW_CFA_undefined
:
2850 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2851 if (!valid_reg(reg
)) break;
2852 TRACE("%lx: DW_CFA_undefined %s\n",
2854 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2855 info
->state
.rules
[reg
] = RULE_UNDEFINED
;
2858 case DW_CFA_same_value
:
2860 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2861 if (!valid_reg(reg
)) break;
2862 TRACE("%lx: DW_CFA_same_value %s\n",
2864 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2865 info
->state
.regs
[reg
] = reg
;
2866 info
->state
.rules
[reg
] = RULE_SAME
;
2869 case DW_CFA_register
:
2871 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2872 ULONG_PTR reg2
= dwarf2_leb128_as_unsigned(ctx
);
2873 if (!valid_reg(reg
) || !valid_reg(reg2
)) break;
2874 TRACE("%lx: DW_CFA_register %s == %s\n",
2876 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2877 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg2
, TRUE
)));
2878 info
->state
.regs
[reg
] = reg2
;
2879 info
->state
.rules
[reg
] = RULE_OTHER_REG
;
2882 case DW_CFA_remember_state
:
2883 TRACE("%lx: DW_CFA_remember_state\n", info
->ip
);
2884 if (info
->state_sp
>= MAX_SAVED_STATES
)
2885 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info
->ip
);
2887 info
->state_stack
[info
->state_sp
++] = info
->state
;
2889 case DW_CFA_restore_state
:
2890 TRACE("%lx: DW_CFA_restore_state\n", info
->ip
);
2891 if (!info
->state_sp
)
2892 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info
->ip
);
2894 info
->state
= info
->state_stack
[--info
->state_sp
];
2896 case DW_CFA_def_cfa
:
2897 case DW_CFA_def_cfa_sf
:
2899 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2900 ULONG_PTR offset
= (op
== DW_CFA_def_cfa
) ? dwarf2_leb128_as_unsigned(ctx
)
2901 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
2902 if (!valid_reg(reg
)) break;
2903 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2905 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2907 info
->state
.cfa_reg
= reg
;
2908 info
->state
.cfa_offset
= offset
;
2909 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
2912 case DW_CFA_def_cfa_register
:
2914 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2915 if (!valid_reg(reg
)) break;
2916 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2918 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)));
2919 info
->state
.cfa_reg
= reg
;
2920 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
2923 case DW_CFA_def_cfa_offset
:
2924 case DW_CFA_def_cfa_offset_sf
:
2926 ULONG_PTR offset
= (op
== DW_CFA_def_cfa_offset
) ? dwarf2_leb128_as_unsigned(ctx
)
2927 : dwarf2_leb128_as_signed(ctx
) * info
->data_align
;
2928 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info
->ip
, offset
);
2929 info
->state
.cfa_offset
= offset
;
2930 info
->state
.cfa_rule
= RULE_CFA_OFFSET
;
2933 case DW_CFA_def_cfa_expression
:
2935 ULONG_PTR expr
= (ULONG_PTR
)ctx
->data
;
2936 ULONG_PTR len
= dwarf2_leb128_as_unsigned(ctx
);
2937 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info
->ip
, expr
, expr
+len
);
2938 info
->state
.cfa_offset
= expr
;
2939 info
->state
.cfa_rule
= RULE_VAL_EXPRESSION
;
2943 case DW_CFA_expression
:
2944 case DW_CFA_val_expression
:
2946 ULONG_PTR reg
= dwarf2_leb128_as_unsigned(ctx
);
2947 ULONG_PTR expr
= (ULONG_PTR
)ctx
->data
;
2948 ULONG_PTR len
= dwarf2_leb128_as_unsigned(ctx
);
2949 if (!valid_reg(reg
)) break;
2950 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2951 info
->ip
, (op
== DW_CFA_expression
) ? "" : "val_",
2952 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(reg
, TRUE
)),
2954 info
->state
.regs
[reg
] = expr
;
2955 info
->state
.rules
[reg
] = (op
== DW_CFA_expression
) ? RULE_EXPRESSION
: RULE_VAL_EXPRESSION
;
2959 case DW_CFA_GNU_args_size
:
2960 /* FIXME: should check that GCC is the compiler for this CU */
2962 ULONG_PTR args
= dwarf2_leb128_as_unsigned(ctx
);
2963 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info
->ip
, args
);
2968 FIXME("%lx: unknown CFA opcode %02x\n", info
->ip
, op
);
2974 /* retrieve a context register from its dwarf number */
2975 static ULONG_PTR
get_context_reg(CONTEXT
*context
, ULONG_PTR dw_reg
)
2977 unsigned regno
= dbghelp_current_cpu
->map_dwarf_register(dw_reg
, TRUE
), sz
;
2978 ULONG_PTR
* ptr
= dbghelp_current_cpu
->fetch_context_reg(context
, regno
, &sz
);
2980 if (sz
!= sizeof(ULONG_PTR
))
2982 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg
, regno
, sz
);
2988 /* set a context register from its dwarf number */
2989 static void set_context_reg(struct cpu_stack_walk
* csw
, CONTEXT
*context
, ULONG_PTR dw_reg
,
2990 ULONG_PTR val
, BOOL isdebuggee
)
2992 unsigned regno
= dbghelp_current_cpu
->map_dwarf_register(dw_reg
, TRUE
), sz
;
2993 ULONG_PTR
* ptr
= dbghelp_current_cpu
->fetch_context_reg(context
, regno
, &sz
);
2999 if (sz
> sizeof(tmp
))
3001 FIXME("register %lu/%u size is too wide: %u\n", dw_reg
, regno
, sz
);
3004 if (!sw_read_mem(csw
, val
, tmp
, sz
))
3006 WARN("Couldn't read memory at %p\n", (void*)val
);
3009 memcpy(ptr
, tmp
, sz
);
3013 if (sz
!= sizeof(ULONG_PTR
))
3015 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg
, regno
, sz
);
3022 /* copy a register from one context to another using dwarf number */
3023 static void copy_context_reg(CONTEXT
*dstcontext
, ULONG_PTR dwregdst
, CONTEXT
* srccontext
, ULONG_PTR dwregsrc
)
3025 unsigned regdstno
= dbghelp_current_cpu
->map_dwarf_register(dwregdst
, TRUE
), szdst
;
3026 unsigned regsrcno
= dbghelp_current_cpu
->map_dwarf_register(dwregsrc
, TRUE
), szsrc
;
3027 ULONG_PTR
* ptrdst
= dbghelp_current_cpu
->fetch_context_reg(dstcontext
, regdstno
, &szdst
);
3028 ULONG_PTR
* ptrsrc
= dbghelp_current_cpu
->fetch_context_reg(srccontext
, regsrcno
, &szsrc
);
3032 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3033 dwregsrc
, regsrcno
, dwregdst
, regdstno
, szsrc
, szdst
);
3036 memcpy(ptrdst
, ptrsrc
, szdst
);
3039 static ULONG_PTR
eval_expression(const struct module
* module
, struct cpu_stack_walk
* csw
,
3040 const unsigned char* zp
, CONTEXT
*context
)
3042 dwarf2_traverse_context_t ctx
;
3043 ULONG_PTR reg
, sz
, tmp
, stack
[64];
3048 ctx
.end_data
= zp
+ 4;
3049 len
= dwarf2_leb128_as_unsigned(&ctx
);
3050 ctx
.end_data
= ctx
.data
+ len
;
3051 ctx
.word_size
= module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
3053 while (ctx
.data
< ctx
.end_data
)
3055 unsigned char opcode
= dwarf2_parse_byte(&ctx
);
3057 if (opcode
>= DW_OP_lit0
&& opcode
<= DW_OP_lit31
)
3058 stack
[++sp
] = opcode
- DW_OP_lit0
;
3059 else if (opcode
>= DW_OP_reg0
&& opcode
<= DW_OP_reg31
)
3060 stack
[++sp
] = get_context_reg(context
, opcode
- DW_OP_reg0
);
3061 else if (opcode
>= DW_OP_breg0
&& opcode
<= DW_OP_breg31
)
3062 stack
[++sp
] = get_context_reg(context
, opcode
- DW_OP_breg0
) + dwarf2_leb128_as_signed(&ctx
);
3063 else switch (opcode
)
3065 case DW_OP_nop
: break;
3066 case DW_OP_addr
: stack
[++sp
] = dwarf2_parse_addr(&ctx
); break;
3067 case DW_OP_const1u
: stack
[++sp
] = dwarf2_parse_byte(&ctx
); break;
3068 case DW_OP_const1s
: stack
[++sp
] = (signed char)dwarf2_parse_byte(&ctx
); break;
3069 case DW_OP_const2u
: stack
[++sp
] = dwarf2_parse_u2(&ctx
); break;
3070 case DW_OP_const2s
: stack
[++sp
] = (short)dwarf2_parse_u2(&ctx
); break;
3071 case DW_OP_const4u
: stack
[++sp
] = dwarf2_parse_u4(&ctx
); break;
3072 case DW_OP_const4s
: stack
[++sp
] = (signed int)dwarf2_parse_u4(&ctx
); break;
3073 case DW_OP_const8u
: stack
[++sp
] = dwarf2_parse_u8(&ctx
); break;
3074 case DW_OP_const8s
: stack
[++sp
] = (LONG_PTR
)dwarf2_parse_u8(&ctx
); break;
3075 case DW_OP_constu
: stack
[++sp
] = dwarf2_leb128_as_unsigned(&ctx
); break;
3076 case DW_OP_consts
: stack
[++sp
] = dwarf2_leb128_as_signed(&ctx
); break;
3078 if (!sw_read_mem(csw
, stack
[sp
], &tmp
, sizeof(tmp
)))
3080 ERR("Couldn't read memory at %lx\n", stack
[sp
]);
3085 case DW_OP_dup
: stack
[sp
+ 1] = stack
[sp
]; sp
++; break;
3086 case DW_OP_drop
: sp
--; break;
3087 case DW_OP_over
: stack
[sp
+ 1] = stack
[sp
- 1]; sp
++; break;
3088 case DW_OP_pick
: stack
[sp
+ 1] = stack
[sp
- dwarf2_parse_byte(&ctx
)]; sp
++; break;
3089 case DW_OP_swap
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = tmp
; break;
3090 case DW_OP_rot
: tmp
= stack
[sp
]; stack
[sp
] = stack
[sp
-1]; stack
[sp
-1] = stack
[sp
-2]; stack
[sp
-2] = tmp
; break;
3091 case DW_OP_abs
: stack
[sp
] = labs(stack
[sp
]); break;
3092 case DW_OP_neg
: stack
[sp
] = -stack
[sp
]; break;
3093 case DW_OP_not
: stack
[sp
] = ~stack
[sp
]; break;
3094 case DW_OP_and
: stack
[sp
-1] &= stack
[sp
]; sp
--; break;
3095 case DW_OP_or
: stack
[sp
-1] |= stack
[sp
]; sp
--; break;
3096 case DW_OP_minus
: stack
[sp
-1] -= stack
[sp
]; sp
--; break;
3097 case DW_OP_mul
: stack
[sp
-1] *= stack
[sp
]; sp
--; break;
3098 case DW_OP_plus
: stack
[sp
-1] += stack
[sp
]; sp
--; break;
3099 case DW_OP_xor
: stack
[sp
-1] ^= stack
[sp
]; sp
--; break;
3100 case DW_OP_shl
: stack
[sp
-1] <<= stack
[sp
]; sp
--; break;
3101 case DW_OP_shr
: stack
[sp
-1] >>= stack
[sp
]; sp
--; break;
3102 case DW_OP_plus_uconst
: stack
[sp
] += dwarf2_leb128_as_unsigned(&ctx
); break;
3103 case DW_OP_shra
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (1 << stack
[sp
]); sp
--; break;
3104 case DW_OP_div
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] / (LONG_PTR
)stack
[sp
]; sp
--; break;
3105 case DW_OP_mod
: stack
[sp
-1] = (LONG_PTR
)stack
[sp
-1] % (LONG_PTR
)stack
[sp
]; sp
--; break;
3106 case DW_OP_ge
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] >= (LONG_PTR
)stack
[sp
]); sp
--; break;
3107 case DW_OP_gt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] > (LONG_PTR
)stack
[sp
]); sp
--; break;
3108 case DW_OP_le
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] <= (LONG_PTR
)stack
[sp
]); sp
--; break;
3109 case DW_OP_lt
: stack
[sp
-1] = ((LONG_PTR
)stack
[sp
-1] < (LONG_PTR
)stack
[sp
]); sp
--; break;
3110 case DW_OP_eq
: stack
[sp
-1] = (stack
[sp
-1] == stack
[sp
]); sp
--; break;
3111 case DW_OP_ne
: stack
[sp
-1] = (stack
[sp
-1] != stack
[sp
]); sp
--; break;
3112 case DW_OP_skip
: tmp
= (short)dwarf2_parse_u2(&ctx
); ctx
.data
+= tmp
; break;
3113 case DW_OP_bra
: tmp
= (short)dwarf2_parse_u2(&ctx
); if (!stack
[sp
--]) ctx
.data
+= tmp
; break;
3114 case DW_OP_GNU_encoded_addr
:
3115 tmp
= dwarf2_parse_byte(&ctx
);
3116 stack
[++sp
] = dwarf2_parse_augmentation_ptr(&ctx
, tmp
);
3119 stack
[++sp
] = get_context_reg(context
, dwarf2_leb128_as_unsigned(&ctx
));
3122 reg
= dwarf2_leb128_as_unsigned(&ctx
);
3123 tmp
= dwarf2_leb128_as_signed(&ctx
);
3124 stack
[++sp
] = get_context_reg(context
, reg
) + tmp
;
3126 case DW_OP_deref_size
:
3127 sz
= dwarf2_parse_byte(&ctx
);
3128 if (!sw_read_mem(csw
, stack
[sp
], &tmp
, sz
))
3130 ERR("Couldn't read memory at %lx\n", stack
[sp
]);
3133 /* do integral promotion */
3136 case 1: stack
[sp
] = *(unsigned char*)&tmp
; break;
3137 case 2: stack
[sp
] = *(unsigned short*)&tmp
; break;
3138 case 4: stack
[sp
] = *(unsigned int*)&tmp
; break;
3139 case 8: stack
[sp
] = *(ULONG_PTR
*)&tmp
; break; /* FIXME: won't work on 32bit platform */
3140 default: FIXME("Unknown size for deref 0x%lx\n", sz
);
3144 FIXME("unhandled opcode %02x\n", opcode
);
3150 static void apply_frame_state(const struct module
* module
, struct cpu_stack_walk
* csw
,
3151 CONTEXT
*context
, struct frame_state
*state
, ULONG_PTR
* cfa
)
3155 CONTEXT new_context
= *context
;
3157 switch (state
->cfa_rule
)
3159 case RULE_EXPRESSION
:
3160 *cfa
= eval_expression(module
, csw
, (const unsigned char*)state
->cfa_offset
, context
);
3161 if (!sw_read_mem(csw
, *cfa
, cfa
, sizeof(*cfa
)))
3163 WARN("Couldn't read memory at %p\n", (void*)*cfa
);
3167 case RULE_VAL_EXPRESSION
:
3168 *cfa
= eval_expression(module
, csw
, (const unsigned char*)state
->cfa_offset
, context
);
3171 *cfa
= get_context_reg(context
, state
->cfa_reg
) + state
->cfa_offset
;
3176 for (i
= 0; i
< NB_FRAME_REGS
; i
++)
3178 switch (state
->rules
[i
])
3181 case RULE_UNDEFINED
:
3184 case RULE_CFA_OFFSET
:
3185 set_context_reg(csw
, &new_context
, i
, *cfa
+ state
->regs
[i
], TRUE
);
3187 case RULE_OTHER_REG
:
3188 copy_context_reg(&new_context
, i
, context
, state
->regs
[i
]);
3190 case RULE_EXPRESSION
:
3191 value
= eval_expression(module
, csw
, (const unsigned char*)state
->regs
[i
], context
);
3192 set_context_reg(csw
, &new_context
, i
, value
, TRUE
);
3194 case RULE_VAL_EXPRESSION
:
3195 value
= eval_expression(module
, csw
, (const unsigned char*)state
->regs
[i
], context
);
3196 set_context_reg(csw
, &new_context
, i
, value
, FALSE
);
3200 *context
= new_context
;
3203 /***********************************************************************
3204 * dwarf2_virtual_unwind
3207 BOOL
dwarf2_virtual_unwind(struct cpu_stack_walk
* csw
, ULONG_PTR ip
, CONTEXT
* context
, ULONG_PTR
* cfa
)
3209 struct module_pair pair
;
3210 struct frame_info info
;
3211 dwarf2_traverse_context_t cie_ctx
, fde_ctx
;
3212 struct module_format
* modfmt
;
3213 const unsigned char* end
;
3216 if (!(pair
.pcs
= process_find_by_handle(csw
->hProcess
)) ||
3217 !(pair
.requested
= module_find_by_addr(pair
.pcs
, ip
, DMT_UNKNOWN
)) ||
3218 !module_get_debug(&pair
))
3220 modfmt
= pair
.effective
->format_info
[DFI_DWARF
];
3221 if (!modfmt
) return FALSE
;
3222 memset(&info
, 0, sizeof(info
));
3223 fde_ctx
.data
= modfmt
->u
.dwarf2_info
->eh_frame
.address
;
3224 fde_ctx
.end_data
= fde_ctx
.data
+ modfmt
->u
.dwarf2_info
->eh_frame
.size
;
3225 fde_ctx
.word_size
= modfmt
->u
.dwarf2_info
->word_size
;
3226 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3227 * in this process the IMAGE section at a different address as the one expected by
3230 delta
= pair
.effective
->module
.BaseOfImage
+ modfmt
->u
.dwarf2_info
->eh_frame
.rva
-
3231 (DWORD_PTR
)modfmt
->u
.dwarf2_info
->eh_frame
.address
;
3232 if (!dwarf2_get_cie(ip
, pair
.effective
, delta
, &fde_ctx
, &cie_ctx
, &info
, TRUE
))
3234 fde_ctx
.data
= modfmt
->u
.dwarf2_info
->debug_frame
.address
;
3235 fde_ctx
.end_data
= fde_ctx
.data
+ modfmt
->u
.dwarf2_info
->debug_frame
.size
;
3236 fde_ctx
.word_size
= modfmt
->u
.dwarf2_info
->word_size
;
3237 delta
= pair
.effective
->reloc_delta
;
3238 if (!dwarf2_get_cie(ip
, pair
.effective
, delta
, &fde_ctx
, &cie_ctx
, &info
, FALSE
))
3240 TRACE("Couldn't find information for %lx\n", ip
);
3245 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3246 ip
, info
.ip
, info
.code_align
, info
.data_align
,
3247 dbghelp_current_cpu
->fetch_regname(dbghelp_current_cpu
->map_dwarf_register(info
.retaddr_reg
, TRUE
)));
3249 /* if at very beginning of function, return and use default unwinder */
3250 if (ip
== info
.ip
) return FALSE
;
3251 execute_cfa_instructions(&cie_ctx
, ip
, &info
);
3253 if (info
.aug_z_format
) /* get length of augmentation data */
3255 ULONG_PTR len
= dwarf2_leb128_as_unsigned(&fde_ctx
);
3256 end
= fde_ctx
.data
+ len
;
3259 dwarf2_parse_augmentation_ptr(&fde_ctx
, info
.lsda_encoding
); /* handler_data */
3260 if (end
) fde_ctx
.data
= end
;
3262 execute_cfa_instructions(&fde_ctx
, ip
, &info
);
3264 /* if there is no information about retaddr, use default unwinder */
3265 if (info
.state
.rules
[info
.retaddr_reg
] == RULE_UNSET
) return FALSE
;
3267 apply_frame_state(pair
.effective
, csw
, context
, &info
.state
, cfa
);
3272 static void dwarf2_location_compute(struct process
* pcs
,
3273 const struct module_format
* modfmt
,
3274 const struct symt_function
* func
,
3275 struct location
* loc
)
3277 struct location frame
;
3280 dwarf2_traverse_context_t lctx
;
3282 if (!func
->container
|| func
->container
->tag
!= SymTagCompiland
)
3284 WARN("We'd expect function %s's container to exist and be a compiland\n", func
->hash_elt
.name
);
3285 err
= loc_err_internal
;
3289 /* instruction pointer relative to compiland's start */
3290 ip
= pcs
->ctx_frame
.InstructionOffset
- ((struct symt_compiland
*)func
->container
)->address
;
3292 if ((err
= loc_compute_frame(pcs
, modfmt
, func
, ip
, &frame
)) == 0)
3296 case loc_dwarf2_location_list
:
3297 /* Then, if the variable has a location list, find it !! */
3298 if (dwarf2_lookup_loclist(modfmt
,
3299 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ loc
->offset
,
3302 err
= loc_err_out_of_scope
;
3304 case loc_dwarf2_block
:
3305 /* or if we have a copy of an existing block, get ready for it */
3307 unsigned* ptr
= (unsigned*)loc
->offset
;
3309 lctx
.data
= (const BYTE
*)(ptr
+ 1);
3310 lctx
.end_data
= lctx
.data
+ *ptr
;
3311 lctx
.word_size
= modfmt
->u
.dwarf2_info
->word_size
;
3314 /* now get the variable */
3315 err
= compute_location(&lctx
, loc
, pcs
->handle
, &frame
);
3322 WARN("Unsupported local kind %d\n", loc
->kind
);
3323 err
= loc_err_internal
;
3329 loc
->kind
= loc_register
;
3335 static void *zalloc(void *priv
, uInt items
, uInt sz
)
3337 return HeapAlloc(GetProcessHeap(), 0, items
* sz
);
3340 static void zfree(void *priv
, void *addr
)
3342 HeapFree(GetProcessHeap(), 0, addr
);
3345 static inline BOOL
dwarf2_init_zsection(dwarf2_section_t
* section
,
3346 const char* zsectname
,
3347 struct image_section_map
* ism
)
3354 BYTE
*addr
, *sect
= (BYTE
*)image_map_section(ism
);
3355 size_t sz
= image_get_map_size(ism
);
3357 if (sz
<= 12 || memcmp(sect
, "ZLIB", 4))
3359 ERR("invalid compressed section %s\n", zsectname
);
3363 #ifdef WORDS_BIGENDIAN
3364 li
.u
.HighPart
= *(DWORD
*)§
[4];
3365 li
.u
.LowPart
= *(DWORD
*)§
[8];
3367 li
.u
.HighPart
= RtlUlongByteSwap(*(DWORD
*)§
[4]);
3368 li
.u
.LowPart
= RtlUlongByteSwap(*(DWORD
*)§
[8]);
3371 addr
= HeapAlloc(GetProcessHeap(), 0, li
.QuadPart
);
3375 z
.next_in
= §
[12];
3376 z
.avail_in
= sz
- 12;
3381 res
= inflateInit(&z
);
3384 FIXME("inflateInit failed with %i / %s\n", res
, z
.msg
);
3389 z
.next_out
= addr
+ z
.total_out
;
3390 z
.avail_out
= li
.QuadPart
- z
.total_out
;
3391 res
= inflate(&z
, Z_FINISH
);
3392 } while (z
.avail_in
&& res
== Z_STREAM_END
);
3394 if (res
!= Z_STREAM_END
)
3396 FIXME("Decompression failed with %i / %s\n", res
, z
.msg
);
3401 section
->compressed
= TRUE
;
3402 section
->address
= addr
;
3403 section
->rva
= image_get_map_rva(ism
);
3404 section
->size
= z
.total_out
;
3410 HeapFree(GetProcessHeap(), 0, addr
);
3412 image_unmap_section(ism
);
3418 static inline BOOL
dwarf2_init_section(dwarf2_section_t
* section
, struct image_file_map
* fmap
,
3419 const char* sectname
, const char* zsectname
,
3420 struct image_section_map
* ism
)
3422 struct image_section_map local_ism
;
3424 if (!ism
) ism
= &local_ism
;
3426 section
->compressed
= FALSE
;
3427 if (image_find_section(fmap
, sectname
, ism
))
3429 section
->address
= (const BYTE
*)image_map_section(ism
);
3430 section
->size
= image_get_map_size(ism
);
3431 section
->rva
= image_get_map_rva(ism
);
3435 section
->address
= NULL
;
3439 if (zsectname
&& image_find_section(fmap
, zsectname
, ism
))
3442 return dwarf2_init_zsection(section
, zsectname
, ism
);
3444 FIXME("dbghelp not built with zlib, but compressed section found\n" );
3451 static inline void dwarf2_fini_section(dwarf2_section_t
* section
)
3453 if (section
->compressed
)
3454 HeapFree(GetProcessHeap(), 0, (void*)section
->address
);
3457 static void dwarf2_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
3459 dwarf2_fini_section(&modfmt
->u
.dwarf2_info
->debug_loc
);
3460 dwarf2_fini_section(&modfmt
->u
.dwarf2_info
->debug_frame
);
3461 HeapFree(GetProcessHeap(), 0, modfmt
);
3464 BOOL
dwarf2_parse(struct module
* module
, unsigned long load_offset
,
3465 const struct elf_thunk_area
* thunks
,
3466 struct image_file_map
* fmap
)
3468 dwarf2_section_t eh_frame
, section
[section_max
];
3469 dwarf2_traverse_context_t mod_ctx
;
3470 struct image_section_map debug_sect
, debug_str_sect
, debug_abbrev_sect
,
3471 debug_line_sect
, debug_ranges_sect
, eh_frame_sect
;
3473 struct module_format
* dwarf2_modfmt
;
3475 dwarf2_init_section(&eh_frame
, fmap
, ".eh_frame", NULL
, &eh_frame_sect
);
3476 dwarf2_init_section(§ion
[section_debug
], fmap
, ".debug_info", ".zdebug_info", &debug_sect
);
3477 dwarf2_init_section(§ion
[section_abbrev
], fmap
, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect
);
3478 dwarf2_init_section(§ion
[section_string
], fmap
, ".debug_str", ".zdebug_str", &debug_str_sect
);
3479 dwarf2_init_section(§ion
[section_line
], fmap
, ".debug_line", ".zdebug_line", &debug_line_sect
);
3480 dwarf2_init_section(§ion
[section_ranges
], fmap
, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect
);
3482 /* to do anything useful we need either .eh_frame or .debug_info */
3483 if ((!eh_frame
.address
|| eh_frame
.address
== IMAGE_NO_MAP
) &&
3484 (!section
[section_debug
].address
|| section
[section_debug
].address
== IMAGE_NO_MAP
))
3490 if (fmap
->modtype
== DMT_ELF
&& debug_sect
.fmap
)
3492 /* debug info might have a different base address than .so file
3493 * when elf file is prelinked after splitting off debug info
3494 * adjust symbol base addresses accordingly
3496 load_offset
+= fmap
->u
.elf
.elf_start
- debug_sect
.fmap
->u
.elf
.elf_start
;
3499 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module
->module
.ModuleName
));
3501 mod_ctx
.data
= section
[section_debug
].address
;
3502 mod_ctx
.end_data
= mod_ctx
.data
+ section
[section_debug
].size
;
3503 mod_ctx
.word_size
= 0; /* will be correctly set later on */
3505 dwarf2_modfmt
= HeapAlloc(GetProcessHeap(), 0,
3506 sizeof(*dwarf2_modfmt
) + sizeof(*dwarf2_modfmt
->u
.dwarf2_info
));
3512 dwarf2_modfmt
->module
= module
;
3513 dwarf2_modfmt
->remove
= dwarf2_module_remove
;
3514 dwarf2_modfmt
->loc_compute
= dwarf2_location_compute
;
3515 dwarf2_modfmt
->u
.dwarf2_info
= (struct dwarf2_module_info_s
*)(dwarf2_modfmt
+ 1);
3516 dwarf2_modfmt
->u
.dwarf2_info
->word_size
= 0; /* will be correctly set later on */
3517 dwarf2_modfmt
->module
->format_info
[DFI_DWARF
] = dwarf2_modfmt
;
3519 /* As we'll need later some sections' content, we won't unmap these
3520 * sections upon existing this function
3522 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_loc
, fmap
, ".debug_loc", ".zdebug_loc", NULL
);
3523 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_frame
, fmap
, ".debug_frame", ".zdebug_frame", NULL
);
3524 dwarf2_modfmt
->u
.dwarf2_info
->eh_frame
= eh_frame
;
3526 while (mod_ctx
.data
< mod_ctx
.end_data
)
3528 dwarf2_parse_compilation_unit(section
, dwarf2_modfmt
->module
, thunks
, &mod_ctx
, load_offset
);
3530 dwarf2_modfmt
->module
->module
.SymType
= SymDia
;
3531 dwarf2_modfmt
->module
->module
.CVSig
= 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3532 /* FIXME: we could have a finer grain here */
3533 dwarf2_modfmt
->module
->module
.GlobalSymbols
= TRUE
;
3534 dwarf2_modfmt
->module
->module
.TypeInfo
= TRUE
;
3535 dwarf2_modfmt
->module
->module
.SourceIndexed
= TRUE
;
3536 dwarf2_modfmt
->module
->module
.Publics
= TRUE
;
3538 /* set the word_size for eh_frame parsing */
3539 dwarf2_modfmt
->u
.dwarf2_info
->word_size
= fmap
->addr_size
/ 8;
3542 dwarf2_fini_section(§ion
[section_debug
]);
3543 dwarf2_fini_section(§ion
[section_abbrev
]);
3544 dwarf2_fini_section(§ion
[section_string
]);
3545 dwarf2_fini_section(§ion
[section_line
]);
3546 dwarf2_fini_section(§ion
[section_ranges
]);
3548 image_unmap_section(&debug_sect
);
3549 image_unmap_section(&debug_abbrev_sect
);
3550 image_unmap_section(&debug_str_sect
);
3551 image_unmap_section(&debug_line_sect
);
3552 image_unmap_section(&debug_ranges_sect
);
3553 if (!ret
) image_unmap_section(&eh_frame_sect
);