2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
26 #include <sys/types.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
31 #ifdef HAVE_SYS_MMAN_H
42 #define PATH_MAX MAX_PATH
53 #include "dbghelp_private.h"
54 #include "image_private.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf
);
62 * o unspecified parameters
64 * o Debug{Start|End}Point
67 * o proper types loading (nesting)
71 static void dump(const void* ptr
, unsigned len
)
75 static const char hexof
[] = "0123456789abcdef";
78 for (i
= 0; i
< len
; i
+= 16)
80 sprintf(msg
, "%08x: ", i
);
81 memset(msg
+ 10, ' ', 3 * 16 + 1 + 16);
82 for (j
= 0; j
< min(16, len
- i
); j
++)
84 msg
[10 + 3 * j
+ 0] = hexof
[x
[i
+ j
] >> 4];
85 msg
[10 + 3 * j
+ 1] = hexof
[x
[i
+ j
] & 15];
86 msg
[10 + 3 * j
+ 2] = ' ';
87 msg
[10 + 3 * 16 + 1 + j
] = (x
[i
+ j
] >= 0x20 && x
[i
+ j
] < 0x7f) ?
90 msg
[10 + 3 * 16] = ' ';
91 msg
[10 + 3 * 16 + 1 + 16] = '\0';
100 * http://www.eagercon.com/dwarf/dwarf3std.htm
101 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
103 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
105 * example of projects who do dwarf2 parsing:
106 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
107 * http://elis.ugent.be/diota/log/ltrace_elf.c
115 typedef struct dwarf2_abbrev_entry_attr_s
117 unsigned long attribute
;
119 struct dwarf2_abbrev_entry_attr_s
* next
;
120 } dwarf2_abbrev_entry_attr_t
;
122 typedef struct dwarf2_abbrev_entry_s
124 unsigned long entry_code
;
126 unsigned char have_child
;
128 dwarf2_abbrev_entry_attr_t
* attrs
;
129 } dwarf2_abbrev_entry_t
;
134 const unsigned char* ptr
;
142 unsigned long uvalue
;
146 struct dwarf2_block block
;
150 typedef struct dwarf2_debug_info_s
152 const dwarf2_abbrev_entry_t
*abbrev
;
154 const unsigned char** data
;
155 struct vector children
;
156 } dwarf2_debug_info_t
;
158 typedef struct dwarf2_section_s
160 const unsigned char* address
;
165 enum dwarf2_sections
{section_debug
, section_string
, section_abbrev
, section_line
, section_max
};
167 typedef struct dwarf2_traverse_context_s
169 const unsigned char* data
;
170 const unsigned char* end_data
;
171 unsigned char word_size
;
172 } dwarf2_traverse_context_t
;
174 /* symt_cache indexes */
181 typedef struct dwarf2_parse_context_s
183 const dwarf2_section_t
* sections
;
186 struct module
* module
;
187 const struct elf_thunk_area
*thunks
;
188 struct sparse_array abbrev_table
;
189 struct sparse_array debug_info_table
;
190 unsigned long load_offset
;
191 unsigned long ref_offset
;
192 struct symt
* symt_cache
[sc_num
]; /* void, int1, int2, int4 */
193 } dwarf2_parse_context_t
;
195 /* stored in the dbghelp's module internal structure for later reuse */
196 struct dwarf2_module_info_s
198 dwarf2_section_t debug_loc
;
199 unsigned char word_size
;
202 #define loc_dwarf2_location_list (loc_user + 0)
203 #define loc_dwarf2_block (loc_user + 1)
205 /* forward declarations */
206 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_parse_context_t
* ctx
, dwarf2_debug_info_t
* entry
);
208 static unsigned char dwarf2_get_byte(const unsigned char* ptr
)
213 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t
* ctx
)
215 unsigned char uvalue
= dwarf2_get_byte(ctx
->data
);
220 static unsigned short dwarf2_get_u2(const unsigned char* ptr
)
222 return *(const UINT16
*)ptr
;
225 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t
* ctx
)
227 unsigned short uvalue
= dwarf2_get_u2(ctx
->data
);
232 static unsigned long dwarf2_get_u4(const unsigned char* ptr
)
234 return *(const UINT32
*)ptr
;
237 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t
* ctx
)
239 unsigned long uvalue
= dwarf2_get_u4(ctx
->data
);
244 static DWORD64
dwarf2_get_u8(const unsigned char* ptr
)
246 return *(const UINT64
*)ptr
;
249 static DWORD64
dwarf2_parse_u8(dwarf2_traverse_context_t
* ctx
)
251 DWORD64 uvalue
= dwarf2_get_u8(ctx
->data
);
256 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr
, const unsigned char** end
)
258 unsigned long ret
= 0;
264 byte
= dwarf2_get_byte(ptr
++);
265 ret
|= (byte
& 0x7f) << shift
;
267 } while (byte
& 0x80);
273 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t
* ctx
)
279 ret
= dwarf2_get_leb128_as_unsigned(ctx
->data
, &ctx
->data
);
284 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr
, const unsigned char** end
)
289 const unsigned size
= sizeof(int) * 8;
293 byte
= dwarf2_get_byte(ptr
++);
294 ret
|= (byte
& 0x7f) << shift
;
296 } while (byte
& 0x80);
299 /* as spec: sign bit of byte is 2nd high order bit (80x40)
300 * -> 0x80 is used as flag.
302 if ((shift
< size
) && (byte
& 0x40))
304 ret
|= - (1 << shift
);
309 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t
* ctx
)
315 ret
= dwarf2_get_leb128_as_signed(ctx
->data
, &ctx
->data
);
319 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t
* ctx
)
322 for (ret
= 0; ctx
->data
[ret
] & 0x80; ret
++);
326 /******************************************************************
329 * Returns an address.
330 * We assume that in all cases word size from Dwarf matches the size of
331 * addresses in platform where the exec is compiled.
333 static unsigned long dwarf2_get_addr(const unsigned char* ptr
, unsigned word_size
)
340 ret
= dwarf2_get_u4(ptr
);
343 ret
= dwarf2_get_u8(ptr
);
346 FIXME("Unsupported Word Size %u\n", word_size
);
352 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t
* ctx
)
354 unsigned long ret
= dwarf2_get_addr(ctx
->data
, ctx
->word_size
);
355 ctx
->data
+= ctx
->word_size
;
359 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t
* ctx
)
361 return wine_dbg_sprintf("ctx(%p)", ctx
->data
);
364 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t
* ctx
)
366 return wine_dbg_sprintf("ctx(%p,%s)",
367 ctx
, debugstr_w(ctx
->module
->module
.ModuleName
));
370 static const char* dwarf2_debug_di(const dwarf2_debug_info_t
* di
)
372 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
373 di
->abbrev
, di
->symt
);
376 static dwarf2_abbrev_entry_t
*
377 dwarf2_abbrev_table_find_entry(const struct sparse_array
* abbrev_table
,
378 unsigned long entry_code
)
380 assert( NULL
!= abbrev_table
);
381 return sparse_array_find(abbrev_table
, entry_code
);
384 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t
* abbrev_ctx
,
385 struct sparse_array
* abbrev_table
,
388 unsigned long entry_code
;
389 dwarf2_abbrev_entry_t
* abbrev_entry
;
390 dwarf2_abbrev_entry_attr_t
* new = NULL
;
391 dwarf2_abbrev_entry_attr_t
* last
= NULL
;
392 unsigned long attribute
;
395 assert( NULL
!= abbrev_ctx
);
397 TRACE("%s, end at %p\n",
398 dwarf2_debug_traverse_ctx(abbrev_ctx
), abbrev_ctx
->end_data
);
400 sparse_array_init(abbrev_table
, sizeof(dwarf2_abbrev_entry_t
), 32);
401 while (abbrev_ctx
->data
< abbrev_ctx
->end_data
)
403 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
404 entry_code
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
405 TRACE("found entry_code %lu\n", entry_code
);
408 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx
));
411 abbrev_entry
= sparse_array_add(abbrev_table
, entry_code
, pool
);
412 assert( NULL
!= abbrev_entry
);
414 abbrev_entry
->entry_code
= entry_code
;
415 abbrev_entry
->tag
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
416 abbrev_entry
->have_child
= dwarf2_parse_byte(abbrev_ctx
);
417 abbrev_entry
->attrs
= NULL
;
418 abbrev_entry
->num_attr
= 0;
420 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
421 abbrev_table
, sparse_array_length(abbrev_table
),
422 entry_code
, abbrev_entry
->tag
, abbrev_entry
->have_child
, abbrev_entry
);
427 attribute
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
428 form
= dwarf2_leb128_as_unsigned(abbrev_ctx
);
429 if (!attribute
) break;
431 new = pool_alloc(pool
, sizeof(dwarf2_abbrev_entry_attr_t
));
434 new->attribute
= attribute
;
437 if (abbrev_entry
->attrs
) last
->next
= new;
438 else abbrev_entry
->attrs
= new;
440 abbrev_entry
->num_attr
++;
443 TRACE("found %u entries\n", sparse_array_length(abbrev_table
));
446 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t
* ctx
,
447 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
)
451 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr
->attribute
, abbrev_attr
->form
);
453 switch (abbrev_attr
->form
)
455 case DW_FORM_ref_addr
:
456 case DW_FORM_addr
: step
= ctx
->word_size
; break;
459 case DW_FORM_ref1
: step
= 1; break;
461 case DW_FORM_ref2
: step
= 2; break;
464 case DW_FORM_strp
: step
= 4; break;
466 case DW_FORM_ref8
: step
= 8; break;
468 case DW_FORM_ref_udata
:
469 case DW_FORM_udata
: step
= dwarf2_leb128_length(ctx
); break;
470 case DW_FORM_string
: step
= strlen((const char*)ctx
->data
) + 1; break;
471 case DW_FORM_block
: step
= dwarf2_leb128_as_unsigned(ctx
); break;
472 case DW_FORM_block1
: step
= dwarf2_parse_byte(ctx
); break;
473 case DW_FORM_block2
: step
= dwarf2_parse_u2(ctx
); break;
474 case DW_FORM_block4
: step
= dwarf2_parse_u4(ctx
); break;
476 FIXME("Unhandled attribute form %lx\n", abbrev_attr
->form
);
482 static void dwarf2_fill_attr(const dwarf2_parse_context_t
* ctx
,
483 const dwarf2_abbrev_entry_attr_t
* abbrev_attr
,
484 const unsigned char* data
,
485 struct attribute
* attr
)
487 attr
->form
= abbrev_attr
->form
;
490 case DW_FORM_ref_addr
:
492 attr
->u
.uvalue
= dwarf2_get_addr(data
,
493 ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
);
494 TRACE("addr<0x%lx>\n", attr
->u
.uvalue
);
498 attr
->u
.uvalue
= dwarf2_get_byte(data
);
499 TRACE("flag<0x%lx>\n", attr
->u
.uvalue
);
503 attr
->u
.uvalue
= dwarf2_get_byte(data
);
504 TRACE("data1<%lu>\n", attr
->u
.uvalue
);
508 attr
->u
.uvalue
= dwarf2_get_u2(data
);
509 TRACE("data2<%lu>\n", attr
->u
.uvalue
);
513 attr
->u
.uvalue
= dwarf2_get_u4(data
);
514 TRACE("data4<%lu>\n", attr
->u
.uvalue
);
518 attr
->u
.lluvalue
= dwarf2_get_u8(data
);
519 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr
->u
.uvalue
));
523 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_byte(data
);
524 TRACE("ref1<0x%lx>\n", attr
->u
.uvalue
);
528 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u2(data
);
529 TRACE("ref2<0x%lx>\n", attr
->u
.uvalue
);
533 attr
->u
.uvalue
= ctx
->ref_offset
+ dwarf2_get_u4(data
);
534 TRACE("ref4<0x%lx>\n", attr
->u
.uvalue
);
538 FIXME("Unhandled 64 bit support\n");
542 attr
->u
.svalue
= dwarf2_get_leb128_as_signed(data
, NULL
);
545 case DW_FORM_ref_udata
:
546 attr
->u
.uvalue
= dwarf2_get_leb128_as_unsigned(data
, NULL
);
550 attr
->u
.uvalue
= dwarf2_get_leb128_as_unsigned(data
, NULL
);
554 attr
->u
.string
= (const char *)data
;
555 TRACE("string<%s>\n", attr
->u
.string
);
560 unsigned long offset
= dwarf2_get_u4(data
);
561 attr
->u
.string
= (const char*)ctx
->sections
[section_string
].address
+ offset
;
563 TRACE("strp<%s>\n", attr
->u
.string
);
567 attr
->u
.block
.size
= dwarf2_get_leb128_as_unsigned(data
, &attr
->u
.block
.ptr
);
571 attr
->u
.block
.size
= dwarf2_get_byte(data
);
572 attr
->u
.block
.ptr
= data
+ 1;
576 attr
->u
.block
.size
= dwarf2_get_u2(data
);
577 attr
->u
.block
.ptr
= data
+ 2;
581 attr
->u
.block
.size
= dwarf2_get_u4(data
);
582 attr
->u
.block
.ptr
= data
+ 4;
586 FIXME("Unhandled attribute form %lx\n", abbrev_attr
->form
);
591 static BOOL
dwarf2_find_attribute(const dwarf2_parse_context_t
* ctx
,
592 const dwarf2_debug_info_t
* di
,
593 unsigned at
, struct attribute
* attr
)
596 dwarf2_abbrev_entry_attr_t
* abbrev_attr
;
597 dwarf2_abbrev_entry_attr_t
* abstract_abbrev_attr
;
601 abstract_abbrev_attr
= NULL
;
602 for (i
= 0, abbrev_attr
= di
->abbrev
->attrs
; abbrev_attr
; i
++, abbrev_attr
= abbrev_attr
->next
)
604 if (abbrev_attr
->attribute
== at
)
606 dwarf2_fill_attr(ctx
, abbrev_attr
, di
->data
[i
], attr
);
609 if (abbrev_attr
->attribute
== DW_AT_abstract_origin
&&
612 abstract_abbrev_attr
= abbrev_attr
;
616 /* do we have an abstract origin debug entry to look into ? */
617 if (!abstract_abbrev_attr
) break;
618 dwarf2_fill_attr(ctx
, abstract_abbrev_attr
, di
->data
[ai
], attr
);
619 if (!(di
= sparse_array_find(&ctx
->debug_info_table
, attr
->u
.uvalue
)))
620 FIXME("Should have found the debug info entry\n");
625 static void dwarf2_load_one_entry(dwarf2_parse_context_t
*, dwarf2_debug_info_t
*,
626 struct symt_compiland
*);
628 #define Wine_DW_no_register 0x7FFFFFFF
630 static unsigned dwarf2_map_register(int regno
)
632 if (regno
== Wine_DW_no_register
)
634 FIXME("What the heck map reg 0x%x\n",regno
);
637 return dbghelp_current_cpu
->map_dwarf_register(regno
);
640 static enum location_error
641 compute_location(dwarf2_traverse_context_t
* ctx
, struct location
* loc
,
642 HANDLE hproc
, const struct location
* frame
)
644 DWORD_PTR tmp
, stack
[64];
647 BOOL piece_found
= FALSE
;
651 loc
->kind
= loc_absolute
;
652 loc
->reg
= Wine_DW_no_register
;
654 while (ctx
->data
< ctx
->end_data
)
656 op
= dwarf2_parse_byte(ctx
);
658 if (op
>= DW_OP_lit0
&& op
<= DW_OP_lit31
)
659 stack
[++stk
] = op
- DW_OP_lit0
;
660 else if (op
>= DW_OP_reg0
&& op
<= DW_OP_reg31
)
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 if (loc
->reg
!= Wine_DW_no_register
)
669 FIXME("Only supporting one reg (%d -> %d)\n",
670 loc
->reg
, dwarf2_map_register(op
- DW_OP_reg0
));
671 loc
->reg
= dwarf2_map_register(op
- DW_OP_reg0
);
673 loc
->kind
= loc_register
;
675 else if (op
>= DW_OP_breg0
&& op
<= DW_OP_breg31
)
677 /* dbghelp APIs don't know how to cope with this anyway
678 * (for example 'long long' stored in two registers)
679 * FIXME: We should tell winedbg how to deal with it (sigh)
683 if (loc
->reg
!= Wine_DW_no_register
)
684 FIXME("Only supporting one breg (%d -> %d)\n",
685 loc
->reg
, dwarf2_map_register(op
- DW_OP_breg0
));
686 loc
->reg
= dwarf2_map_register(op
- DW_OP_breg0
);
688 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
689 loc
->kind
= loc_regrel
;
694 case DW_OP_nop
: break;
695 case DW_OP_addr
: stack
[++stk
] = dwarf2_parse_addr(ctx
); break;
696 case DW_OP_const1u
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
697 case DW_OP_const1s
: stack
[++stk
] = dwarf2_parse_byte(ctx
); break;
698 case DW_OP_const2u
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
699 case DW_OP_const2s
: stack
[++stk
] = dwarf2_parse_u2(ctx
); break;
700 case DW_OP_const4u
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
701 case DW_OP_const4s
: stack
[++stk
] = dwarf2_parse_u4(ctx
); break;
702 case DW_OP_const8u
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
703 case DW_OP_const8s
: stack
[++stk
] = dwarf2_parse_u8(ctx
); break;
704 case DW_OP_constu
: stack
[++stk
] = dwarf2_leb128_as_unsigned(ctx
); break;
705 case DW_OP_consts
: stack
[++stk
] = dwarf2_leb128_as_signed(ctx
); break;
706 case DW_OP_dup
: stack
[stk
+ 1] = stack
[stk
]; stk
++; break;
707 case DW_OP_drop
: stk
--; break;
708 case DW_OP_over
: stack
[stk
+ 1] = stack
[stk
- 1]; stk
++; break;
709 case DW_OP_pick
: stack
[stk
+ 1] = stack
[stk
- dwarf2_parse_byte(ctx
)]; stk
++; break;
710 case DW_OP_swap
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = tmp
; break;
711 case DW_OP_rot
: tmp
= stack
[stk
]; stack
[stk
] = stack
[stk
-1]; stack
[stk
-1] = stack
[stk
-2]; stack
[stk
-2] = tmp
; break;
712 case DW_OP_abs
: stack
[stk
] = labs(stack
[stk
]); break;
713 case DW_OP_neg
: stack
[stk
] = -stack
[stk
]; break;
714 case DW_OP_not
: stack
[stk
] = ~stack
[stk
]; break;
715 case DW_OP_and
: stack
[stk
-1] &= stack
[stk
]; stk
--; break;
716 case DW_OP_or
: stack
[stk
-1] |= stack
[stk
]; stk
--; break;
717 case DW_OP_minus
: stack
[stk
-1] -= stack
[stk
]; stk
--; break;
718 case DW_OP_mul
: stack
[stk
-1] *= stack
[stk
]; stk
--; break;
719 case DW_OP_plus
: stack
[stk
-1] += stack
[stk
]; stk
--; break;
720 case DW_OP_xor
: stack
[stk
-1] ^= stack
[stk
]; stk
--; break;
721 case DW_OP_shl
: stack
[stk
-1] <<= stack
[stk
]; stk
--; break;
722 case DW_OP_shr
: stack
[stk
-1] >>= stack
[stk
]; stk
--; break;
723 case DW_OP_plus_uconst
: stack
[stk
] += dwarf2_leb128_as_unsigned(ctx
); break;
724 case DW_OP_shra
: stack
[stk
-1] = stack
[stk
-1] / (1 << stack
[stk
]); stk
--; break;
725 case DW_OP_div
: stack
[stk
-1] = stack
[stk
-1] / stack
[stk
]; stk
--; break;
726 case DW_OP_mod
: stack
[stk
-1] = stack
[stk
-1] % stack
[stk
]; stk
--; break;
727 case DW_OP_ge
: stack
[stk
-1] = (stack
[stk
-1] >= stack
[stk
]); stk
--; break;
728 case DW_OP_gt
: stack
[stk
-1] = (stack
[stk
-1] > stack
[stk
]); stk
--; break;
729 case DW_OP_le
: stack
[stk
-1] = (stack
[stk
-1] <= stack
[stk
]); stk
--; break;
730 case DW_OP_lt
: stack
[stk
-1] = (stack
[stk
-1] < stack
[stk
]); stk
--; break;
731 case DW_OP_eq
: stack
[stk
-1] = (stack
[stk
-1] == stack
[stk
]); stk
--; break;
732 case DW_OP_ne
: stack
[stk
-1] = (stack
[stk
-1] != stack
[stk
]); stk
--; break;
733 case DW_OP_skip
: tmp
= dwarf2_parse_u2(ctx
); ctx
->data
+= tmp
; break;
734 case DW_OP_bra
: tmp
= dwarf2_parse_u2(ctx
); if (!stack
[stk
--]) ctx
->data
+= tmp
; break;
736 if (loc
->reg
!= Wine_DW_no_register
)
737 FIXME("Only supporting one regx\n");
738 loc
->reg
= dwarf2_map_register(dwarf2_leb128_as_unsigned(ctx
));
739 loc
->kind
= loc_register
;
742 tmp
= dwarf2_leb128_as_unsigned(ctx
);
744 if (loc
->reg
!= Wine_DW_no_register
)
745 FIXME("Only supporting one regx\n");
746 loc
->reg
= dwarf2_map_register(tmp
) + dwarf2_leb128_as_signed(ctx
);
747 loc
->kind
= loc_register
;
750 if (loc
->reg
!= Wine_DW_no_register
)
751 FIXME("Only supporting one reg (%d -> -2)\n", loc
->reg
);
752 if (frame
&& frame
->kind
== loc_register
)
754 loc
->kind
= loc_regrel
;
755 loc
->reg
= frame
->reg
;
756 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
758 else if (frame
&& frame
->kind
== loc_regrel
)
760 loc
->kind
= loc_regrel
;
761 loc
->reg
= frame
->reg
;
762 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
) + frame
->offset
;
766 /* FIXME: this could be later optimized by not recomputing
767 * this very location expression
769 loc
->kind
= loc_dwarf2_block
;
770 stack
[++stk
] = dwarf2_leb128_as_signed(ctx
);
775 unsigned sz
= dwarf2_leb128_as_unsigned(ctx
);
776 WARN("Not handling OP_piece (size=%d)\n", sz
);
783 FIXME("Unexpected empty stack\n");
784 return loc_err_internal
;
786 if (loc
->reg
!= Wine_DW_no_register
)
788 WARN("Too complex expression for deref\n");
789 return loc_err_too_complex
;
793 DWORD_PTR addr
= stack
[stk
--];
796 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, sizeof(deref
), NULL
))
798 WARN("Couldn't read memory at %lx\n", addr
);
799 return loc_err_cant_read
;
801 stack
[++stk
] = deref
;
805 loc
->kind
= loc_dwarf2_block
;
808 case DW_OP_deref_size
:
811 FIXME("Unexpected empty stack\n");
812 return loc_err_internal
;
814 if (loc
->reg
!= Wine_DW_no_register
)
816 WARN("Too complex expression for deref\n");
817 return loc_err_too_complex
;
821 DWORD_PTR addr
= stack
[stk
--];
822 BYTE derefsize
= dwarf2_parse_byte(ctx
);
825 if (!ReadProcessMemory(hproc
, (void*)addr
, &deref
, derefsize
, NULL
))
827 WARN("Couldn't read memory at %lx\n", addr
);
828 return loc_err_cant_read
;
833 case 1: stack
[++stk
] = *(unsigned char*)&deref
; break;
834 case 2: stack
[++stk
] = *(unsigned short*)&deref
; break;
835 case 4: stack
[++stk
] = *(DWORD
*)&deref
; break;
836 case 8: if (ctx
->word_size
>= derefsize
) stack
[++stk
] = deref
; break;
841 loc
->kind
= loc_dwarf2_block
;
845 if (op
< DW_OP_lo_user
) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
846 FIXME("Unhandled attr op: %x\n", op
);
847 /* FIXME else unhandled extension */
848 return loc_err_internal
;
851 loc
->offset
= stack
[stk
];
855 static BOOL
dwarf2_compute_location_attr(dwarf2_parse_context_t
* ctx
,
856 const dwarf2_debug_info_t
* di
,
858 struct location
* loc
,
859 const struct location
* frame
)
861 struct attribute xloc
;
863 if (!dwarf2_find_attribute(ctx
, di
, dw
, &xloc
)) return FALSE
;
867 case DW_FORM_data1
: case DW_FORM_data2
:
868 case DW_FORM_udata
: case DW_FORM_sdata
:
869 loc
->kind
= loc_absolute
;
871 loc
->offset
= xloc
.u
.uvalue
;
873 case DW_FORM_data4
: case DW_FORM_data8
:
874 loc
->kind
= loc_dwarf2_location_list
;
875 loc
->reg
= Wine_DW_no_register
;
876 loc
->offset
= xloc
.u
.uvalue
;
880 /* assume we have a block form */
882 if (xloc
.u
.block
.size
)
884 dwarf2_traverse_context_t lctx
;
885 enum location_error err
;
887 lctx
.data
= xloc
.u
.block
.ptr
;
888 lctx
.end_data
= xloc
.u
.block
.ptr
+ xloc
.u
.block
.size
;
889 lctx
.word_size
= ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
891 err
= compute_location(&lctx
, loc
, NULL
, frame
);
894 loc
->kind
= loc_error
;
897 else if (loc
->kind
== loc_dwarf2_block
)
899 unsigned* ptr
= pool_alloc(&ctx
->module
->pool
,
900 sizeof(unsigned) + xloc
.u
.block
.size
);
901 *ptr
= xloc
.u
.block
.size
;
902 memcpy(ptr
+ 1, xloc
.u
.block
.ptr
, xloc
.u
.block
.size
);
903 loc
->offset
= (unsigned long)ptr
;
909 static struct symt
* dwarf2_lookup_type(dwarf2_parse_context_t
* ctx
,
910 const dwarf2_debug_info_t
* di
)
912 struct attribute attr
;
914 if (dwarf2_find_attribute(ctx
, di
, DW_AT_type
, &attr
))
916 dwarf2_debug_info_t
* type
;
918 type
= sparse_array_find(&ctx
->debug_info_table
, attr
.u
.uvalue
);
919 if (!type
) FIXME("Unable to find back reference to type %lx\n", attr
.u
.uvalue
);
922 /* load the debug info entity */
923 dwarf2_load_one_entry(ctx
, type
, NULL
);
930 /******************************************************************
931 * dwarf2_read_one_debug_info
933 * Loads into memory one debug info entry, and recursively its children (if any)
935 static BOOL
dwarf2_read_one_debug_info(dwarf2_parse_context_t
* ctx
,
936 dwarf2_traverse_context_t
* traverse
,
937 dwarf2_debug_info_t
** pdi
)
939 const dwarf2_abbrev_entry_t
*abbrev
;
940 unsigned long entry_code
;
941 unsigned long offset
;
942 dwarf2_debug_info_t
* di
;
943 dwarf2_debug_info_t
* child
;
944 dwarf2_debug_info_t
** where
;
945 dwarf2_abbrev_entry_attr_t
* attr
;
947 struct attribute sibling
;
949 offset
= traverse
->data
- ctx
->sections
[ctx
->section
].address
;
950 entry_code
= dwarf2_leb128_as_unsigned(traverse
);
951 TRACE("found entry_code %lu at 0x%lx\n", entry_code
, offset
);
957 abbrev
= dwarf2_abbrev_table_find_entry(&ctx
->abbrev_table
, entry_code
);
960 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code
, offset
);
963 di
= sparse_array_add(&ctx
->debug_info_table
, offset
, &ctx
->pool
);
964 if (!di
) return FALSE
;
968 if (abbrev
->num_attr
)
970 di
->data
= pool_alloc(&ctx
->pool
, abbrev
->num_attr
* sizeof(const char*));
971 for (i
= 0, attr
= abbrev
->attrs
; attr
; i
++, attr
= attr
->next
)
973 di
->data
[i
] = traverse
->data
;
974 dwarf2_swallow_attribute(traverse
, attr
);
977 else di
->data
= NULL
;
978 if (abbrev
->have_child
)
980 vector_init(&di
->children
, sizeof(dwarf2_debug_info_t
*), 16);
981 while (traverse
->data
< traverse
->end_data
)
983 if (!dwarf2_read_one_debug_info(ctx
, traverse
, &child
)) return FALSE
;
985 where
= vector_add(&di
->children
, &ctx
->pool
);
986 if (!where
) return FALSE
;
990 if (dwarf2_find_attribute(ctx
, di
, DW_AT_sibling
, &sibling
) &&
991 traverse
->data
!= ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
)
993 WARN("setting cursor for %s to next sibling <0x%lx>\n",
994 dwarf2_debug_traverse_ctx(traverse
), sibling
.u
.uvalue
);
995 traverse
->data
= ctx
->sections
[ctx
->section
].address
+ sibling
.u
.uvalue
;
1001 static struct symt
* dwarf2_parse_base_type(dwarf2_parse_context_t
* ctx
,
1002 dwarf2_debug_info_t
* di
)
1004 struct attribute name
;
1005 struct attribute size
;
1006 struct attribute encoding
;
1009 if (di
->symt
) return di
->symt
;
1011 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1013 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1014 name
.u
.string
= NULL
;
1015 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1016 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_encoding
, &encoding
)) encoding
.u
.uvalue
= DW_ATE_void
;
1018 switch (encoding
.u
.uvalue
)
1020 case DW_ATE_void
: bt
= btVoid
; break;
1021 case DW_ATE_address
: bt
= btULong
; break;
1022 case DW_ATE_boolean
: bt
= btBool
; break;
1023 case DW_ATE_complex_float
: bt
= btComplex
; break;
1024 case DW_ATE_float
: bt
= btFloat
; break;
1025 case DW_ATE_signed
: bt
= btInt
; break;
1026 case DW_ATE_unsigned
: bt
= btUInt
; break;
1027 case DW_ATE_signed_char
: bt
= btChar
; break;
1028 case DW_ATE_unsigned_char
: bt
= btChar
; break;
1029 default: bt
= btNoType
; break;
1031 di
->symt
= &symt_new_basic(ctx
->module
, bt
, name
.u
.string
, size
.u
.uvalue
)->symt
;
1035 assert(size
.u
.uvalue
== 0);
1036 cache_idx
= sc_void
;
1039 switch (size
.u
.uvalue
)
1041 case 1: cache_idx
= sc_int1
; break;
1042 case 2: cache_idx
= sc_int2
; break;
1043 case 4: cache_idx
= sc_int4
; break;
1048 if (cache_idx
!= -1 && !ctx
->symt_cache
[cache_idx
])
1049 ctx
->symt_cache
[cache_idx
] = di
->symt
;
1051 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1055 static struct symt
* dwarf2_parse_typedef(dwarf2_parse_context_t
* ctx
,
1056 dwarf2_debug_info_t
* di
)
1058 struct symt
* ref_type
;
1059 struct attribute name
;
1061 if (di
->symt
) return di
->symt
;
1063 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx
), di
->abbrev
->entry_code
);
1065 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1066 ref_type
= dwarf2_lookup_type(ctx
, di
);
1069 di
->symt
= &symt_new_typedef(ctx
->module
, ref_type
, name
.u
.string
)->symt
;
1070 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1074 static struct symt
* dwarf2_parse_pointer_type(dwarf2_parse_context_t
* ctx
,
1075 dwarf2_debug_info_t
* di
)
1077 struct symt
* ref_type
;
1078 struct attribute size
;
1080 if (di
->symt
) return di
->symt
;
1082 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1084 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1085 if (!(ref_type
= dwarf2_lookup_type(ctx
, di
)))
1087 ref_type
= ctx
->symt_cache
[sc_void
];
1090 di
->symt
= &symt_new_pointer(ctx
->module
, ref_type
)->symt
;
1091 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1095 static struct symt
* dwarf2_parse_array_type(dwarf2_parse_context_t
* ctx
,
1096 dwarf2_debug_info_t
* di
)
1098 struct symt
* ref_type
;
1099 struct symt
* idx_type
= NULL
;
1100 struct attribute min
, max
, cnt
;
1101 dwarf2_debug_info_t
* child
;
1104 if (di
->symt
) return di
->symt
;
1106 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1108 if (!di
->abbrev
->have_child
)
1110 FIXME("array without range information\n");
1113 ref_type
= dwarf2_lookup_type(ctx
, di
);
1115 for (i
=0; i
<vector_length(&di
->children
); i
++)
1117 child
= *(dwarf2_debug_info_t
**)vector_at(&di
->children
, i
);
1118 switch (child
->abbrev
->tag
)
1120 case DW_TAG_subrange_type
:
1121 idx_type
= dwarf2_lookup_type(ctx
, child
);
1122 if (!dwarf2_find_attribute(ctx
, child
, DW_AT_lower_bound
, &min
))
1124 if (!dwarf2_find_attribute(ctx
, child
, DW_AT_upper_bound
, &max
))
1126 if (dwarf2_find_attribute(ctx
, child
, DW_AT_count
, &cnt
))
1127 max
.u
.uvalue
= min
.u
.uvalue
+ cnt
.u
.uvalue
;
1130 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1131 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1135 di
->symt
= &symt_new_array(ctx
->module
, min
.u
.uvalue
, max
.u
.uvalue
, ref_type
, idx_type
)->symt
;
1139 static struct symt
* dwarf2_parse_const_type(dwarf2_parse_context_t
* ctx
,
1140 dwarf2_debug_info_t
* di
)
1142 struct symt
* ref_type
;
1144 if (di
->symt
) return di
->symt
;
1146 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1148 ref_type
= dwarf2_lookup_type(ctx
, di
);
1149 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1150 di
->symt
= ref_type
;
1155 static struct symt
* dwarf2_parse_volatile_type(dwarf2_parse_context_t
* ctx
,
1156 dwarf2_debug_info_t
* di
)
1158 struct symt
* ref_type
;
1160 if (di
->symt
) return di
->symt
;
1162 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1164 ref_type
= dwarf2_lookup_type(ctx
, di
);
1165 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1166 di
->symt
= ref_type
;
1171 static struct symt
* dwarf2_parse_reference_type(dwarf2_parse_context_t
* ctx
,
1172 dwarf2_debug_info_t
* di
)
1174 struct symt
* ref_type
= NULL
;
1176 if (di
->symt
) return di
->symt
;
1178 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1180 ref_type
= dwarf2_lookup_type(ctx
, di
);
1181 /* FIXME: for now, we hard-wire C++ references to pointers */
1182 di
->symt
= &symt_new_pointer(ctx
->module
, ref_type
)->symt
;
1184 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1189 static void dwarf2_parse_udt_member(dwarf2_parse_context_t
* ctx
,
1190 const dwarf2_debug_info_t
* di
,
1191 struct symt_udt
* parent
)
1193 struct symt
* elt_type
;
1194 struct attribute name
;
1195 struct attribute bit_size
;
1196 struct attribute bit_offset
;
1197 struct location loc
;
1201 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1203 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1204 elt_type
= dwarf2_lookup_type(ctx
, di
);
1205 if (dwarf2_compute_location_attr(ctx
, di
, DW_AT_data_member_location
, &loc
, NULL
))
1207 if (loc
.kind
!= loc_absolute
)
1209 FIXME("Found register, while not expecting it\n");
1213 TRACE("found member_location at %s -> %lu\n",
1214 dwarf2_debug_ctx(ctx
), loc
.offset
);
1218 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_bit_size
, &bit_size
))
1219 bit_size
.u
.uvalue
= 0;
1220 if (dwarf2_find_attribute(ctx
, di
, DW_AT_bit_offset
, &bit_offset
))
1222 /* FIXME: we should only do this when implementation is LSB (which is
1223 * the case on i386 processors)
1225 struct attribute nbytes
;
1226 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &nbytes
))
1229 nbytes
.u
.uvalue
= symt_get_info(ctx
->module
, elt_type
, TI_GET_LENGTH
, &size
) ?
1230 (unsigned long)size
: 0;
1232 bit_offset
.u
.uvalue
= nbytes
.u
.uvalue
* 8 - bit_offset
.u
.uvalue
- bit_size
.u
.uvalue
;
1234 else bit_offset
.u
.uvalue
= 0;
1235 symt_add_udt_element(ctx
->module
, parent
, name
.u
.string
, elt_type
,
1236 (loc
.offset
<< 3) + bit_offset
.u
.uvalue
,
1239 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1242 static struct symt
* dwarf2_parse_udt_type(dwarf2_parse_context_t
* ctx
,
1243 dwarf2_debug_info_t
* di
,
1246 struct attribute name
;
1247 struct attribute size
;
1249 if (di
->symt
) return di
->symt
;
1251 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1253 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1254 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 0;
1256 di
->symt
= &symt_new_udt(ctx
->module
, name
.u
.string
, size
.u
.uvalue
, udt
)->symt
;
1258 if (di
->abbrev
->have_child
) /** any interest to not have child ? */
1260 dwarf2_debug_info_t
* child
;
1263 for (i
=0; i
<vector_length(&di
->children
); i
++)
1265 child
= *(dwarf2_debug_info_t
**)vector_at(&di
->children
, i
);
1267 switch (child
->abbrev
->tag
)
1270 /* FIXME: should I follow the sibling stuff ?? */
1271 dwarf2_parse_udt_member(ctx
, child
, (struct symt_udt
*)di
->symt
);
1273 case DW_TAG_enumeration_type
:
1274 dwarf2_parse_enumeration_type(ctx
, child
);
1276 case DW_TAG_structure_type
:
1277 case DW_TAG_class_type
:
1278 case DW_TAG_union_type
:
1279 case DW_TAG_typedef
:
1280 /* FIXME: we need to handle nested udt definitions */
1281 case DW_TAG_inheritance
:
1282 case DW_TAG_subprogram
:
1283 case DW_TAG_variable
:
1284 /* FIXME: some C++ related stuff */
1287 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1288 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1297 static void dwarf2_parse_enumerator(dwarf2_parse_context_t
* ctx
,
1298 const dwarf2_debug_info_t
* di
,
1299 struct symt_enum
* parent
)
1301 struct attribute name
;
1302 struct attribute value
;
1304 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1306 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) return;
1307 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_const_value
, &value
)) value
.u
.svalue
= 0;
1308 symt_add_enum_element(ctx
->module
, parent
, name
.u
.string
, value
.u
.svalue
);
1310 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1313 static struct symt
* dwarf2_parse_enumeration_type(dwarf2_parse_context_t
* ctx
,
1314 dwarf2_debug_info_t
* di
)
1316 struct attribute name
;
1317 struct attribute size
;
1318 struct symt_basic
* basetype
;
1320 if (di
->symt
) return di
->symt
;
1322 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1324 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
)) name
.u
.string
= NULL
;
1325 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_byte_size
, &size
)) size
.u
.uvalue
= 4;
1327 switch (size
.u
.uvalue
) /* FIXME: that's wrong */
1329 case 1: basetype
= symt_new_basic(ctx
->module
, btInt
, "char", 1); break;
1330 case 2: basetype
= symt_new_basic(ctx
->module
, btInt
, "short", 2); break;
1332 case 4: basetype
= symt_new_basic(ctx
->module
, btInt
, "int", 4); break;
1335 di
->symt
= &symt_new_enum(ctx
->module
, name
.u
.string
, &basetype
->symt
)->symt
;
1337 if (di
->abbrev
->have_child
) /* any interest to not have child ? */
1339 dwarf2_debug_info_t
* child
;
1342 /* FIXME: should we use the sibling stuff ?? */
1343 for (i
=0; i
<vector_length(&di
->children
); i
++)
1345 child
= *(dwarf2_debug_info_t
**)vector_at(&di
->children
, i
);
1347 switch (child
->abbrev
->tag
)
1349 case DW_TAG_enumerator
:
1350 dwarf2_parse_enumerator(ctx
, child
, (struct symt_enum
*)di
->symt
);
1353 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1354 di
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1361 /* structure used to pass information around when parsing a subprogram */
1362 typedef struct dwarf2_subprogram_s
1364 dwarf2_parse_context_t
* ctx
;
1365 struct symt_compiland
* compiland
;
1366 struct symt_function
* func
;
1367 BOOL non_computed_variable
;
1368 struct location frame
;
1369 } dwarf2_subprogram_t
;
1371 /******************************************************************
1372 * dwarf2_parse_variable
1374 * Parses any variable (parameter, local/global variable)
1376 static void dwarf2_parse_variable(dwarf2_subprogram_t
* subpgm
,
1377 struct symt_block
* block
,
1378 dwarf2_debug_info_t
* di
)
1380 struct symt
* param_type
;
1381 struct attribute name
, value
;
1382 struct location loc
;
1385 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1387 is_pmt
= !block
&& di
->abbrev
->tag
== DW_TAG_formal_parameter
;
1388 param_type
= dwarf2_lookup_type(subpgm
->ctx
, di
);
1390 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_name
, &name
)) {
1391 /* cannot do much without the name, the functions below won't like it. */
1394 if (dwarf2_compute_location_attr(subpgm
->ctx
, di
, DW_AT_location
,
1395 &loc
, &subpgm
->frame
))
1397 struct attribute ext
;
1399 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1400 name
.u
.string
, loc
.kind
, loc
.offset
, loc
.reg
,
1401 dwarf2_debug_ctx(subpgm
->ctx
));
1408 /* it's a global variable */
1409 /* FIXME: we don't handle its scope yet */
1410 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_external
, &ext
))
1412 symt_new_global_variable(subpgm
->ctx
->module
, subpgm
->compiland
,
1413 name
.u
.string
, !ext
.u
.uvalue
,
1414 subpgm
->ctx
->load_offset
+ loc
.offset
,
1418 subpgm
->non_computed_variable
= TRUE
;
1422 /* either a pmt/variable relative to frame pointer or
1423 * pmt/variable in a register
1425 assert(subpgm
->func
);
1426 symt_add_func_local(subpgm
->ctx
->module
, subpgm
->func
,
1427 is_pmt
? DataIsParam
: DataIsLocal
,
1428 &loc
, block
, param_type
, name
.u
.string
);
1432 else if (dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_const_value
, &value
))
1435 if (subpgm
->func
) WARN("Unsupported constant %s in function\n", name
.u
.string
);
1436 if (is_pmt
) FIXME("Unsupported constant (parameter) %s in function\n", name
.u
.string
);
1444 v
.n1
.n2
.vt
= VT_UI4
;
1445 v
.n1
.n2
.n3
.lVal
= value
.u
.uvalue
;
1449 v
.n1
.n2
.vt
= VT_UI8
;
1450 v
.n1
.n2
.n3
.llVal
= value
.u
.lluvalue
;
1455 v
.n1
.n2
.n3
.lVal
= value
.u
.svalue
;
1459 case DW_FORM_string
:
1460 /* FIXME: native doesn't report const strings from here !!
1461 * however, the value of the string is in the code somewhere
1463 v
.n1
.n2
.vt
= VT_I1
| VT_BYREF
;
1464 v
.n1
.n2
.n3
.byref
= pool_strdup(&subpgm
->ctx
->module
->pool
, value
.u
.string
);
1468 case DW_FORM_block1
:
1469 case DW_FORM_block2
:
1470 case DW_FORM_block4
:
1472 switch (value
.u
.block
.size
)
1474 case 1: v
.n1
.n2
.n3
.lVal
= *(BYTE
*)value
.u
.block
.ptr
; break;
1475 case 2: v
.n1
.n2
.n3
.lVal
= *(USHORT
*)value
.u
.block
.ptr
; break;
1476 case 4: v
.n1
.n2
.n3
.lVal
= *(DWORD
*)value
.u
.block
.ptr
; break;
1478 v
.n1
.n2
.vt
= VT_I1
| VT_BYREF
;
1479 v
.n1
.n2
.n3
.byref
= pool_alloc(&subpgm
->ctx
->module
->pool
, value
.u
.block
.size
);
1480 memcpy(v
.n1
.n2
.n3
.byref
, value
.u
.block
.ptr
, value
.u
.block
.size
);
1485 FIXME("Unsupported form for const value %s (%lx)\n",
1486 name
.u
.string
, value
.form
);
1487 v
.n1
.n2
.vt
= VT_EMPTY
;
1489 di
->symt
= &symt_new_constant(subpgm
->ctx
->module
, subpgm
->compiland
,
1490 name
.u
.string
, param_type
, &v
)->symt
;
1492 if (is_pmt
&& subpgm
->func
&& subpgm
->func
->type
)
1493 symt_add_function_signature_parameter(subpgm
->ctx
->module
,
1494 (struct symt_function_signature
*)subpgm
->func
->type
,
1497 if (di
->abbrev
->have_child
) FIXME("Unsupported children\n");
1500 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t
* subpgm
,
1501 const dwarf2_debug_info_t
* di
)
1503 struct attribute name
;
1504 struct attribute low_pc
;
1505 struct location loc
;
1507 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1509 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_low_pc
, &low_pc
)) low_pc
.u
.uvalue
= 0;
1510 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_name
, &name
))
1511 name
.u
.string
= NULL
;
1513 loc
.kind
= loc_absolute
;
1514 loc
.offset
= subpgm
->ctx
->load_offset
+ low_pc
.u
.uvalue
;
1515 symt_add_function_point(subpgm
->ctx
->module
, subpgm
->func
, SymTagLabel
,
1516 &loc
, name
.u
.string
);
1519 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
1520 struct symt_block
* parent_block
,
1521 const dwarf2_debug_info_t
* di
);
1523 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t
* subpgm
,
1524 struct symt_block
* parent_block
,
1525 const dwarf2_debug_info_t
* di
)
1527 struct symt_block
* block
;
1528 struct attribute low_pc
;
1529 struct attribute high_pc
;
1531 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1533 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_low_pc
, &low_pc
)) low_pc
.u
.uvalue
= 0;
1534 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_high_pc
, &high_pc
)) high_pc
.u
.uvalue
= 0;
1536 block
= symt_open_func_block(subpgm
->ctx
->module
, subpgm
->func
, parent_block
,
1537 subpgm
->ctx
->load_offset
+ low_pc
.u
.uvalue
- subpgm
->func
->address
,
1538 high_pc
.u
.uvalue
- low_pc
.u
.uvalue
);
1540 if (di
->abbrev
->have_child
) /** any interest to not have child ? */
1542 dwarf2_debug_info_t
* child
;
1545 for (i
=0; i
<vector_length(&di
->children
); i
++)
1547 child
= *(dwarf2_debug_info_t
**)vector_at(&di
->children
, i
);
1549 switch (child
->abbrev
->tag
)
1551 case DW_TAG_formal_parameter
:
1552 case DW_TAG_variable
:
1553 dwarf2_parse_variable(subpgm
, block
, child
);
1555 case DW_TAG_lexical_block
:
1556 dwarf2_parse_subprogram_block(subpgm
, block
, child
);
1558 case DW_TAG_inlined_subroutine
:
1559 dwarf2_parse_inlined_subroutine(subpgm
, block
, child
);
1562 dwarf2_parse_subprogram_label(subpgm
, child
);
1565 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1566 child
->abbrev
->tag
, dwarf2_debug_ctx(subpgm
->ctx
),
1567 dwarf2_debug_di(di
));
1571 symt_close_func_block(subpgm
->ctx
->module
, subpgm
->func
, block
, 0);
1574 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t
* subpgm
,
1575 struct symt_block
* parent_block
,
1576 const dwarf2_debug_info_t
* di
)
1578 struct symt_block
* block
;
1579 struct attribute low_pc
;
1580 struct attribute high_pc
;
1582 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1584 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_low_pc
, &low_pc
))
1585 low_pc
.u
.uvalue
= 0;
1586 if (!dwarf2_find_attribute(subpgm
->ctx
, di
, DW_AT_high_pc
, &high_pc
))
1587 high_pc
.u
.uvalue
= 0;
1589 block
= symt_open_func_block(subpgm
->ctx
->module
, subpgm
->func
, parent_block
,
1590 subpgm
->ctx
->load_offset
+ low_pc
.u
.uvalue
- subpgm
->func
->address
,
1591 high_pc
.u
.uvalue
- low_pc
.u
.uvalue
);
1593 if (di
->abbrev
->have_child
) /** any interest to not have child ? */
1595 dwarf2_debug_info_t
* child
;
1598 for (i
=0; i
<vector_length(&di
->children
); i
++)
1600 child
= *(dwarf2_debug_info_t
**)vector_at(&di
->children
, i
);
1602 switch (child
->abbrev
->tag
)
1604 case DW_TAG_inlined_subroutine
:
1605 dwarf2_parse_inlined_subroutine(subpgm
, block
, child
);
1607 case DW_TAG_variable
:
1608 dwarf2_parse_variable(subpgm
, block
, child
);
1610 case DW_TAG_lexical_block
:
1611 dwarf2_parse_subprogram_block(subpgm
, block
, child
);
1613 case DW_TAG_subprogram
:
1614 /* FIXME: likely a declaration (to be checked)
1618 case DW_TAG_formal_parameter
:
1619 /* FIXME: likely elements for exception handling (GCC flavor)
1624 dwarf2_parse_subprogram_label(subpgm
, child
);
1626 case DW_TAG_class_type
:
1627 case DW_TAG_structure_type
:
1628 case DW_TAG_union_type
:
1629 case DW_TAG_enumeration_type
:
1630 case DW_TAG_typedef
:
1631 /* the type referred to will be loaded when we need it, so skip it */
1634 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1635 child
->abbrev
->tag
, dwarf2_debug_ctx(subpgm
->ctx
), dwarf2_debug_di(di
));
1640 symt_close_func_block(subpgm
->ctx
->module
, subpgm
->func
, block
, 0);
1643 static struct symt
* dwarf2_parse_subprogram(dwarf2_parse_context_t
* ctx
,
1644 dwarf2_debug_info_t
* di
,
1645 struct symt_compiland
* compiland
)
1647 struct attribute name
;
1648 struct attribute low_pc
;
1649 struct attribute high_pc
;
1650 struct attribute is_decl
;
1651 struct attribute inline_flags
;
1652 struct symt
* ret_type
;
1653 struct symt_function_signature
* sig_type
;
1654 dwarf2_subprogram_t subpgm
;
1656 if (di
->symt
) return di
->symt
;
1658 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1660 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_name
, &name
))
1662 WARN("No name for function... dropping function\n");
1665 /* if it's an abstract representation of an inline function, there should be
1666 * a concrete object that we'll handle
1668 if (dwarf2_find_attribute(ctx
, di
, DW_AT_inline
, &inline_flags
))
1670 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1671 name
.u
.string
? name
.u
.string
: "(null)", inline_flags
.u
.uvalue
);
1675 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_low_pc
, &low_pc
)) low_pc
.u
.uvalue
= 0;
1676 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_high_pc
, &high_pc
)) high_pc
.u
.uvalue
= 0;
1677 /* As functions (defined as inline assembly) get debug info with dwarf
1678 * (not the case for stabs), we just drop Wine's thunks here...
1679 * Actual thunks will be created in elf_module from the symbol table
1681 if (elf_is_in_thunk_area(ctx
->load_offset
+ low_pc
.u
.uvalue
,
1684 if (!dwarf2_find_attribute(ctx
, di
, DW_AT_declaration
, &is_decl
))
1685 is_decl
.u
.uvalue
= 0;
1687 if (!(ret_type
= dwarf2_lookup_type(ctx
, di
)))
1689 ret_type
= ctx
->symt_cache
[sc_void
];
1693 /* FIXME: assuming C source code */
1694 sig_type
= symt_new_function_signature(ctx
->module
, ret_type
, CV_CALL_FAR_C
);
1695 if (!is_decl
.u
.uvalue
)
1697 subpgm
.func
= symt_new_function(ctx
->module
, compiland
, name
.u
.string
,
1698 ctx
->load_offset
+ low_pc
.u
.uvalue
,
1699 high_pc
.u
.uvalue
- low_pc
.u
.uvalue
,
1701 di
->symt
= &subpgm
.func
->symt
;
1703 else subpgm
.func
= NULL
;
1706 subpgm
.compiland
= compiland
;
1707 if (!dwarf2_compute_location_attr(ctx
, di
, DW_AT_frame_base
,
1708 &subpgm
.frame
, NULL
))
1711 subpgm
.frame
.kind
= loc_regrel
;
1712 subpgm
.frame
.reg
= 0;
1713 subpgm
.frame
.offset
= 0;
1715 subpgm
.non_computed_variable
= FALSE
;
1717 if (di
->abbrev
->have_child
) /** any interest to not have child ? */
1719 dwarf2_debug_info_t
* child
;
1722 for (i
=0; i
<vector_length(&di
->children
); i
++)
1724 child
= *(dwarf2_debug_info_t
**)vector_at(&di
->children
, i
);
1726 switch (child
->abbrev
->tag
)
1728 case DW_TAG_variable
:
1729 case DW_TAG_formal_parameter
:
1730 dwarf2_parse_variable(&subpgm
, NULL
, child
);
1732 case DW_TAG_lexical_block
:
1733 dwarf2_parse_subprogram_block(&subpgm
, NULL
, child
);
1735 case DW_TAG_inlined_subroutine
:
1736 dwarf2_parse_inlined_subroutine(&subpgm
, NULL
, child
);
1738 case DW_TAG_subprogram
:
1739 /* FIXME: likely a declaration (to be checked)
1744 dwarf2_parse_subprogram_label(&subpgm
, child
);
1746 case DW_TAG_class_type
:
1747 case DW_TAG_structure_type
:
1748 case DW_TAG_union_type
:
1749 case DW_TAG_enumeration_type
:
1750 case DW_TAG_typedef
:
1751 /* the type referred to will be loaded when we need it, so skip it */
1753 case DW_TAG_unspecified_parameters
:
1754 /* FIXME: no support in dbghelp's internals so far */
1757 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1758 child
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1763 if (subpgm
.non_computed_variable
|| subpgm
.frame
.kind
>= loc_user
)
1765 symt_add_function_point(ctx
->module
, subpgm
.func
, SymTagCustom
,
1766 &subpgm
.frame
, NULL
);
1768 if (subpgm
.func
) symt_normalize_function(subpgm
.ctx
->module
, subpgm
.func
);
1773 static struct symt
* dwarf2_parse_subroutine_type(dwarf2_parse_context_t
* ctx
,
1774 dwarf2_debug_info_t
* di
)
1776 struct symt
* ret_type
;
1777 struct symt_function_signature
* sig_type
;
1779 if (di
->symt
) return di
->symt
;
1781 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx
), dwarf2_debug_di(di
));
1783 if (!(ret_type
= dwarf2_lookup_type(ctx
, di
)))
1785 ret_type
= ctx
->symt_cache
[sc_void
];
1789 /* FIXME: assuming C source code */
1790 sig_type
= symt_new_function_signature(ctx
->module
, ret_type
, CV_CALL_FAR_C
);
1792 if (di
->abbrev
->have_child
) /** any interest to not have child ? */
1794 dwarf2_debug_info_t
* child
;
1797 for (i
=0; i
<vector_length(&di
->children
); i
++)
1799 child
= *(dwarf2_debug_info_t
**)vector_at(&di
->children
, i
);
1801 switch (child
->abbrev
->tag
)
1803 case DW_TAG_formal_parameter
:
1804 symt_add_function_signature_parameter(ctx
->module
, sig_type
,
1805 dwarf2_lookup_type(ctx
, child
));
1807 case DW_TAG_unspecified_parameters
:
1808 WARN("Unsupported unspecified parameters\n");
1814 return di
->symt
= &sig_type
->symt
;
1817 static void dwarf2_load_one_entry(dwarf2_parse_context_t
* ctx
,
1818 dwarf2_debug_info_t
* di
,
1819 struct symt_compiland
* compiland
)
1821 switch (di
->abbrev
->tag
)
1823 case DW_TAG_typedef
:
1824 dwarf2_parse_typedef(ctx
, di
);
1826 case DW_TAG_base_type
:
1827 dwarf2_parse_base_type(ctx
, di
);
1829 case DW_TAG_pointer_type
:
1830 dwarf2_parse_pointer_type(ctx
, di
);
1832 case DW_TAG_class_type
:
1833 dwarf2_parse_udt_type(ctx
, di
, UdtClass
);
1835 case DW_TAG_structure_type
:
1836 dwarf2_parse_udt_type(ctx
, di
, UdtStruct
);
1838 case DW_TAG_union_type
:
1839 dwarf2_parse_udt_type(ctx
, di
, UdtUnion
);
1841 case DW_TAG_array_type
:
1842 dwarf2_parse_array_type(ctx
, di
);
1844 case DW_TAG_const_type
:
1845 dwarf2_parse_const_type(ctx
, di
);
1847 case DW_TAG_volatile_type
:
1848 dwarf2_parse_volatile_type(ctx
, di
);
1850 case DW_TAG_reference_type
:
1851 dwarf2_parse_reference_type(ctx
, di
);
1853 case DW_TAG_enumeration_type
:
1854 dwarf2_parse_enumeration_type(ctx
, di
);
1856 case DW_TAG_subprogram
:
1857 dwarf2_parse_subprogram(ctx
, di
, compiland
);
1859 case DW_TAG_subroutine_type
:
1860 dwarf2_parse_subroutine_type(ctx
, di
);
1862 case DW_TAG_variable
:
1864 dwarf2_subprogram_t subpgm
;
1867 subpgm
.compiland
= compiland
;
1869 subpgm
.frame
.kind
= loc_absolute
;
1870 subpgm
.frame
.offset
= 0;
1871 subpgm
.frame
.reg
= Wine_DW_no_register
;
1872 dwarf2_parse_variable(&subpgm
, NULL
, di
);
1875 /* silence a couple of C++ defines */
1876 case DW_TAG_namespace
:
1877 case DW_TAG_imported_module
:
1878 case DW_TAG_imported_declaration
:
1881 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1882 di
->abbrev
->tag
, dwarf2_debug_ctx(ctx
), di
->abbrev
->entry_code
);
1886 static void dwarf2_set_line_number(struct module
* module
, unsigned long address
,
1887 const struct vector
* v
, unsigned file
, unsigned line
)
1889 struct symt_function
* func
;
1890 struct symt_ht
* symt
;
1893 if (!file
|| !(psrc
= vector_at(v
, file
- 1))) return;
1895 TRACE("%s %lx %s %u\n",
1896 debugstr_w(module
->module
.ModuleName
), address
, source_get(module
, *psrc
), line
);
1897 if (!(symt
= symt_find_nearest(module
, address
)) ||
1898 symt
->symt
.tag
!= SymTagFunction
) return;
1899 func
= (struct symt_function
*)symt
;
1900 symt_add_func_line(module
, func
, *psrc
, line
, address
- func
->address
);
1903 static BOOL
dwarf2_parse_line_numbers(const dwarf2_section_t
* sections
,
1904 dwarf2_parse_context_t
* ctx
,
1905 const char* compile_dir
,
1906 unsigned long offset
)
1908 dwarf2_traverse_context_t traverse
;
1909 unsigned long length
;
1910 unsigned version
, header_len
, insn_size
, default_stmt
;
1911 unsigned line_range
, opcode_base
;
1913 const unsigned char* opcode_len
;
1915 struct vector files
;
1918 /* section with line numbers stripped */
1919 if (sections
[section_line
].address
== IMAGE_NO_MAP
)
1922 traverse
.data
= sections
[section_line
].address
+ offset
;
1923 traverse
.end_data
= traverse
.data
+ 4;
1924 traverse
.word_size
= ctx
->module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
;
1926 length
= dwarf2_parse_u4(&traverse
);
1927 traverse
.end_data
= sections
[section_line
].address
+ offset
+ length
;
1929 version
= dwarf2_parse_u2(&traverse
);
1930 header_len
= dwarf2_parse_u4(&traverse
);
1931 insn_size
= dwarf2_parse_byte(&traverse
);
1932 default_stmt
= dwarf2_parse_byte(&traverse
);
1933 line_base
= (signed char)dwarf2_parse_byte(&traverse
);
1934 line_range
= dwarf2_parse_byte(&traverse
);
1935 opcode_base
= dwarf2_parse_byte(&traverse
);
1937 opcode_len
= traverse
.data
;
1938 traverse
.data
+= opcode_base
- 1;
1940 vector_init(&dirs
, sizeof(const char*), 4);
1941 p
= vector_add(&dirs
, &ctx
->pool
);
1942 *p
= compile_dir
? compile_dir
: ".";
1943 while (*traverse
.data
)
1945 const char* rel
= (const char*)traverse
.data
;
1946 unsigned rellen
= strlen(rel
);
1947 TRACE("Got include %s\n", rel
);
1948 traverse
.data
+= rellen
+ 1;
1949 p
= vector_add(&dirs
, &ctx
->pool
);
1951 if (*rel
== '/' || !compile_dir
)
1955 /* include directory relative to compile directory */
1956 unsigned baselen
= strlen(compile_dir
);
1957 char* tmp
= pool_alloc(&ctx
->pool
, baselen
+ 1 + rellen
+ 1);
1958 strcpy(tmp
, compile_dir
);
1959 if (tmp
[baselen
- 1] != '/') tmp
[baselen
++] = '/';
1960 strcpy(&tmp
[baselen
], rel
);
1967 vector_init(&files
, sizeof(unsigned), 16);
1968 while (*traverse
.data
)
1970 unsigned int dir_index
, mod_time
, length
;
1975 name
= (const char*)traverse
.data
;
1976 traverse
.data
+= strlen(name
) + 1;
1977 dir_index
= dwarf2_leb128_as_unsigned(&traverse
);
1978 mod_time
= dwarf2_leb128_as_unsigned(&traverse
);
1979 length
= dwarf2_leb128_as_unsigned(&traverse
);
1980 dir
= *(const char**)vector_at(&dirs
, dir_index
);
1981 TRACE("Got file %s/%s (%u,%u)\n", dir
, name
, mod_time
, length
);
1982 psrc
= vector_add(&files
, &ctx
->pool
);
1983 *psrc
= source_new(ctx
->module
, dir
, name
);
1987 while (traverse
.data
< traverse
.end_data
)
1989 unsigned long address
= 0;
1992 unsigned is_stmt
= default_stmt
;
1993 BOOL basic_block
= FALSE
, end_sequence
= FALSE
;
1994 unsigned opcode
, extopcode
, i
;
1996 while (!end_sequence
)
1998 opcode
= dwarf2_parse_byte(&traverse
);
1999 TRACE("Got opcode %x\n", opcode
);
2001 if (opcode
>= opcode_base
)
2003 unsigned delta
= opcode
- opcode_base
;
2005 address
+= (delta
/ line_range
) * insn_size
;
2006 line
+= line_base
+ (delta
% line_range
);
2008 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2015 basic_block
= FALSE
;
2016 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2018 case DW_LNS_advance_pc
:
2019 address
+= insn_size
* dwarf2_leb128_as_unsigned(&traverse
);
2021 case DW_LNS_advance_line
:
2022 line
+= dwarf2_leb128_as_signed(&traverse
);
2024 case DW_LNS_set_file
:
2025 file
= dwarf2_leb128_as_unsigned(&traverse
);
2027 case DW_LNS_set_column
:
2028 dwarf2_leb128_as_unsigned(&traverse
);
2030 case DW_LNS_negate_stmt
:
2033 case DW_LNS_set_basic_block
:
2036 case DW_LNS_const_add_pc
:
2037 address
+= ((255 - opcode_base
) / line_range
) * insn_size
;
2039 case DW_LNS_fixed_advance_pc
:
2040 address
+= dwarf2_parse_u2(&traverse
);
2042 case DW_LNS_extended_op
:
2043 dwarf2_leb128_as_unsigned(&traverse
);
2044 extopcode
= dwarf2_parse_byte(&traverse
);
2047 case DW_LNE_end_sequence
:
2048 dwarf2_set_line_number(ctx
->module
, address
, &files
, file
, line
);
2049 end_sequence
= TRUE
;
2051 case DW_LNE_set_address
:
2052 address
= ctx
->load_offset
+ dwarf2_parse_addr(&traverse
);
2054 case DW_LNE_define_file
:
2055 FIXME("not handled %s\n", traverse
.data
);
2056 traverse
.data
+= strlen((const char *)traverse
.data
) + 1;
2057 dwarf2_leb128_as_unsigned(&traverse
);
2058 dwarf2_leb128_as_unsigned(&traverse
);
2059 dwarf2_leb128_as_unsigned(&traverse
);
2062 FIXME("Unsupported extended opcode %x\n", extopcode
);
2067 WARN("Unsupported opcode %x\n", opcode
);
2068 for (i
= 0; i
< opcode_len
[opcode
]; i
++)
2069 dwarf2_leb128_as_unsigned(&traverse
);
2078 static BOOL
dwarf2_parse_compilation_unit(const dwarf2_section_t
* sections
,
2079 struct module
* module
,
2080 const struct elf_thunk_area
* thunks
,
2081 dwarf2_traverse_context_t
* mod_ctx
,
2082 unsigned long load_offset
)
2084 dwarf2_parse_context_t ctx
;
2085 dwarf2_traverse_context_t abbrev_ctx
;
2086 dwarf2_debug_info_t
* di
;
2087 dwarf2_traverse_context_t cu_ctx
;
2088 const unsigned char* comp_unit_start
= mod_ctx
->data
;
2089 unsigned long cu_length
;
2090 unsigned short cu_version
;
2091 unsigned long cu_abbrev_offset
;
2094 cu_length
= dwarf2_parse_u4(mod_ctx
);
2095 cu_ctx
.data
= mod_ctx
->data
;
2096 cu_ctx
.end_data
= mod_ctx
->data
+ cu_length
;
2097 mod_ctx
->data
+= cu_length
;
2098 cu_version
= dwarf2_parse_u2(&cu_ctx
);
2099 cu_abbrev_offset
= dwarf2_parse_u4(&cu_ctx
);
2100 cu_ctx
.word_size
= dwarf2_parse_byte(&cu_ctx
);
2102 TRACE("Compilation Unit Header found at 0x%x:\n",
2103 (int)(comp_unit_start
- sections
[section_debug
].address
));
2104 TRACE("- length: %lu\n", cu_length
);
2105 TRACE("- version: %u\n", cu_version
);
2106 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset
);
2107 TRACE("- word_size: %u\n", cu_ctx
.word_size
);
2109 if (cu_version
!= 2)
2111 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2116 module
->format_info
[DFI_DWARF
]->u
.dwarf2_info
->word_size
= cu_ctx
.word_size
;
2117 mod_ctx
->word_size
= cu_ctx
.word_size
;
2119 pool_init(&ctx
.pool
, 65536);
2120 ctx
.sections
= sections
;
2121 ctx
.section
= section_debug
;
2122 ctx
.module
= module
;
2123 ctx
.thunks
= thunks
;
2124 ctx
.load_offset
= load_offset
;
2125 ctx
.ref_offset
= comp_unit_start
- sections
[section_debug
].address
;
2126 memset(ctx
.symt_cache
, 0, sizeof(ctx
.symt_cache
));
2127 ctx
.symt_cache
[sc_void
] = &symt_new_basic(module
, btVoid
, "void", 0)->symt
;
2129 abbrev_ctx
.data
= sections
[section_abbrev
].address
+ cu_abbrev_offset
;
2130 abbrev_ctx
.end_data
= sections
[section_abbrev
].address
+ sections
[section_abbrev
].size
;
2131 abbrev_ctx
.word_size
= cu_ctx
.word_size
;
2132 dwarf2_parse_abbrev_set(&abbrev_ctx
, &ctx
.abbrev_table
, &ctx
.pool
);
2134 sparse_array_init(&ctx
.debug_info_table
, sizeof(dwarf2_debug_info_t
), 128);
2135 dwarf2_read_one_debug_info(&ctx
, &cu_ctx
, &di
);
2137 if (di
->abbrev
->tag
== DW_TAG_compile_unit
)
2139 struct attribute name
;
2140 dwarf2_debug_info_t
** pdi
= NULL
;
2141 struct attribute stmt_list
, low_pc
;
2142 struct attribute comp_dir
;
2144 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_name
, &name
))
2145 name
.u
.string
= NULL
;
2147 /* get working directory of current compilation unit */
2148 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_comp_dir
, &comp_dir
))
2149 comp_dir
.u
.string
= NULL
;
2151 if (!dwarf2_find_attribute(&ctx
, di
, DW_AT_low_pc
, &low_pc
))
2152 low_pc
.u
.uvalue
= 0;
2153 di
->symt
= &symt_new_compiland(module
,
2154 ctx
.load_offset
+ low_pc
.u
.uvalue
,
2155 source_new(module
, comp_dir
.u
.string
, name
.u
.string
))->symt
;
2157 if (di
->abbrev
->have_child
)
2160 for (i
=0; i
<vector_length(&di
->children
); i
++)
2162 pdi
= vector_at(&di
->children
, i
);
2163 dwarf2_load_one_entry(&ctx
, *pdi
, (struct symt_compiland
*)di
->symt
);
2166 if (dwarf2_find_attribute(&ctx
, di
, DW_AT_stmt_list
, &stmt_list
))
2168 if (dwarf2_parse_line_numbers(sections
, &ctx
, comp_dir
.u
.string
, stmt_list
.u
.uvalue
))
2169 module
->module
.LineNumbers
= TRUE
;
2173 else FIXME("Should have a compilation unit here\n");
2174 pool_destroy(&ctx
.pool
);
2178 static BOOL
dwarf2_lookup_loclist(const struct module_format
* modfmt
, const BYTE
* start
,
2179 unsigned long ip
, dwarf2_traverse_context_t
* lctx
)
2182 const BYTE
* ptr
= start
;
2185 while (ptr
< modfmt
->u
.dwarf2_info
->debug_loc
.address
+ modfmt
->u
.dwarf2_info
->debug_loc
.size
)
2187 beg
= dwarf2_get_addr(ptr
, modfmt
->u
.dwarf2_info
->word_size
); ptr
+= modfmt
->u
.dwarf2_info
->word_size
;
2188 end
= dwarf2_get_addr(ptr
, modfmt
->u
.dwarf2_info
->word_size
); ptr
+= modfmt
->u
.dwarf2_info
->word_size
;
2189 if (!beg
&& !end
) break;
2190 len
= dwarf2_get_u2(ptr
); ptr
+= 2;
2192 if (beg
<= ip
&& ip
< end
)
2195 lctx
->end_data
= ptr
+ len
;
2196 lctx
->word_size
= modfmt
->u
.dwarf2_info
->word_size
;
2201 WARN("Couldn't find ip in location list\n");
2205 static enum location_error
loc_compute_frame(struct process
* pcs
,
2206 const struct module_format
* modfmt
,
2207 const struct symt_function
* func
,
2208 DWORD_PTR ip
, struct location
* frame
)
2210 struct symt
** psym
= NULL
;
2211 struct location
* pframe
;
2212 dwarf2_traverse_context_t lctx
;
2213 enum location_error err
;
2216 for (i
=0; i
<vector_length(&func
->vchildren
); i
++)
2218 psym
= vector_at(&func
->vchildren
, i
);
2219 if ((*psym
)->tag
== SymTagCustom
)
2221 pframe
= &((struct symt_hierarchy_point
*)*psym
)->loc
;
2223 /* First, recompute the frame information, if needed */
2224 switch (pframe
->kind
)
2230 case loc_dwarf2_location_list
:
2231 WARN("Searching loclist for %s\n", func
->hash_elt
.name
);
2232 if (!dwarf2_lookup_loclist(modfmt
,
2233 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ pframe
->offset
,
2235 return loc_err_out_of_scope
;
2236 if ((err
= compute_location(&lctx
, frame
, pcs
->handle
, NULL
)) < 0) return err
;
2237 if (frame
->kind
>= loc_user
)
2239 WARN("Couldn't compute runtime frame location\n");
2240 return loc_err_too_complex
;
2244 WARN("Unsupported frame kind %d\n", pframe
->kind
);
2245 return loc_err_internal
;
2250 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2251 return loc_err_internal
;
2254 static void dwarf2_location_compute(struct process
* pcs
,
2255 const struct module_format
* modfmt
,
2256 const struct symt_function
* func
,
2257 struct location
* loc
)
2259 struct location frame
;
2262 dwarf2_traverse_context_t lctx
;
2264 if (!func
->container
|| func
->container
->tag
!= SymTagCompiland
)
2266 WARN("We'd expect function %s's container to exist and be a compiland\n", func
->hash_elt
.name
);
2267 err
= loc_err_internal
;
2271 /* instruction pointer relative to compiland's start */
2272 ip
= pcs
->ctx_frame
.InstructionOffset
- ((struct symt_compiland
*)func
->container
)->address
;
2274 if ((err
= loc_compute_frame(pcs
, modfmt
, func
, ip
, &frame
)) == 0)
2278 case loc_dwarf2_location_list
:
2279 /* Then, if the variable has a location list, find it !! */
2280 if (dwarf2_lookup_loclist(modfmt
,
2281 modfmt
->u
.dwarf2_info
->debug_loc
.address
+ loc
->offset
,
2284 err
= loc_err_out_of_scope
;
2286 case loc_dwarf2_block
:
2287 /* or if we have a copy of an existing block, get ready for it */
2289 unsigned* ptr
= (unsigned*)loc
->offset
;
2291 lctx
.data
= (const BYTE
*)(ptr
+ 1);
2292 lctx
.end_data
= lctx
.data
+ *ptr
;
2293 lctx
.word_size
= modfmt
->u
.dwarf2_info
->word_size
;
2296 /* now get the variable */
2297 err
= compute_location(&lctx
, loc
, pcs
->handle
, &frame
);
2304 WARN("Unsupported local kind %d\n", loc
->kind
);
2305 err
= loc_err_internal
;
2311 loc
->kind
= loc_register
;
2316 static void dwarf2_module_remove(struct process
* pcs
, struct module_format
* modfmt
)
2318 HeapFree(GetProcessHeap(), 0, modfmt
);
2321 static inline BOOL
dwarf2_init_section(dwarf2_section_t
* section
, struct image_file_map
* fmap
,
2322 const char* sectname
, struct image_section_map
* ism
)
2324 struct image_section_map local_ism
;
2326 if (!ism
) ism
= &local_ism
;
2327 if (!image_find_section(fmap
, sectname
, ism
))
2329 section
->address
= NULL
;
2335 section
->address
= (const BYTE
*)image_map_section(ism
);
2336 section
->size
= image_get_map_size(ism
);
2337 section
->rva
= image_get_map_rva(ism
);
2341 BOOL
dwarf2_parse(struct module
* module
, unsigned long load_offset
,
2342 const struct elf_thunk_area
* thunks
,
2343 struct image_file_map
* fmap
)
2345 dwarf2_section_t section
[section_max
];
2346 dwarf2_traverse_context_t mod_ctx
;
2347 struct image_section_map debug_sect
, debug_str_sect
, debug_abbrev_sect
,
2350 struct module_format
* dwarf2_modfmt
;
2352 if (!dwarf2_init_section(§ion
[section_debug
], fmap
, ".debug_info", &debug_sect
))
2354 /* no Dwarf debug info here, so there's no error */
2357 dwarf2_init_section(§ion
[section_abbrev
], fmap
, ".debug_abbrev", &debug_abbrev_sect
);
2358 dwarf2_init_section(§ion
[section_string
], fmap
, ".debug_str", &debug_str_sect
);
2359 dwarf2_init_section(§ion
[section_line
], fmap
, ".debug_line", &debug_line_sect
);
2361 if (section
[section_debug
].address
== IMAGE_NO_MAP
||
2362 section
[section_abbrev
].address
== IMAGE_NO_MAP
||
2363 section
[section_string
].address
== IMAGE_NO_MAP
)
2369 if (fmap
->modtype
== DMT_ELF
)
2371 /* debug info might have a different base address than .so file
2372 * when elf file is prelinked after splitting off debug info
2373 * adjust symbol base addresses accordingly
2375 load_offset
+= fmap
->u
.elf
.elf_start
- debug_sect
.fmap
->u
.elf
.elf_start
;
2378 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module
->module
.ModuleName
));
2380 mod_ctx
.data
= section
[section_debug
].address
;
2381 mod_ctx
.end_data
= mod_ctx
.data
+ section
[section_debug
].size
;
2382 mod_ctx
.word_size
= 0; /* will be correctly set later on */
2384 dwarf2_modfmt
= HeapAlloc(GetProcessHeap(), 0,
2385 sizeof(*dwarf2_modfmt
) + sizeof(*dwarf2_modfmt
->u
.dwarf2_info
));
2391 dwarf2_modfmt
->module
= module
;
2392 dwarf2_modfmt
->remove
= dwarf2_module_remove
;
2393 dwarf2_modfmt
->loc_compute
= dwarf2_location_compute
;
2394 dwarf2_modfmt
->u
.dwarf2_info
= (struct dwarf2_module_info_s
*)(dwarf2_modfmt
+ 1);
2395 dwarf2_modfmt
->u
.dwarf2_info
->word_size
= 0; /* will be correctly set later on */
2396 dwarf2_modfmt
->module
->format_info
[DFI_DWARF
] = dwarf2_modfmt
;
2398 dwarf2_init_section(&dwarf2_modfmt
->u
.dwarf2_info
->debug_loc
, fmap
, ".debug_loc", NULL
);
2400 while (mod_ctx
.data
< mod_ctx
.end_data
)
2402 dwarf2_parse_compilation_unit(section
, dwarf2_modfmt
->module
, thunks
, &mod_ctx
, load_offset
);
2404 dwarf2_modfmt
->module
->module
.SymType
= SymDia
;
2405 dwarf2_modfmt
->module
->module
.CVSig
= 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2406 /* FIXME: we could have a finer grain here */
2407 dwarf2_modfmt
->module
->module
.GlobalSymbols
= TRUE
;
2408 dwarf2_modfmt
->module
->module
.TypeInfo
= TRUE
;
2409 dwarf2_modfmt
->module
->module
.SourceIndexed
= TRUE
;
2410 dwarf2_modfmt
->module
->module
.Publics
= TRUE
;
2413 image_unmap_section(&debug_sect
);
2414 image_unmap_section(&debug_abbrev_sect
);
2415 image_unmap_section(&debug_str_sect
);
2416 image_unmap_section(&debug_line_sect
);