kernel32/tests: Add a test to check some fields in fake dlls.
[wine.git] / dlls / dbghelp / dwarf.c
blob26e2f42ba0d02da9113bdc302b8a44e44538ed92
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2011, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
25 #include "config.h"
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <stdio.h>
42 #include <assert.h>
43 #include <stdarg.h>
45 #ifdef HAVE_ZLIB
46 #include <zlib.h>
47 #endif
49 #include "windef.h"
50 #include "winternl.h"
51 #include "winbase.h"
52 #include "winuser.h"
53 #include "ole2.h"
54 #include "oleauto.h"
56 #include "dbghelp_private.h"
57 #include "image_private.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
63 /* FIXME:
64 * - Functions:
65 * o unspecified parameters
66 * o inlined functions
67 * o Debug{Start|End}Point
68 * o CFA
69 * - Udt
70 * o proper types loading (nesting)
73 #if 0
74 static void dump(const void* ptr, unsigned len)
76 int i, j;
77 BYTE msg[128];
78 static const char hexof[] = "0123456789abcdef";
79 const BYTE* x = ptr;
81 for (i = 0; i < len; i += 16)
83 sprintf(msg, "%08x: ", i);
84 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
85 for (j = 0; j < min(16, len - i); j++)
87 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
88 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
89 msg[10 + 3 * j + 2] = ' ';
90 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
91 x[i + j] : '.';
93 msg[10 + 3 * 16] = ' ';
94 msg[10 + 3 * 16 + 1 + 16] = '\0';
95 TRACE("%s\n", msg);
98 #endif
102 * Main Specs:
103 * http://www.eagercon.com/dwarf/dwarf3std.htm
104 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
106 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
108 * example of projects who do dwarf2 parsing:
109 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
110 * http://elis.ugent.be/diota/log/ltrace_elf.c
112 #include "dwarf.h"
115 * Parsers
118 typedef struct dwarf2_abbrev_entry_attr_s
120 unsigned long attribute;
121 unsigned long form;
122 struct dwarf2_abbrev_entry_attr_s* next;
123 } dwarf2_abbrev_entry_attr_t;
125 typedef struct dwarf2_abbrev_entry_s
127 unsigned long entry_code;
128 unsigned long tag;
129 unsigned char have_child;
130 unsigned num_attr;
131 dwarf2_abbrev_entry_attr_t* attrs;
132 } dwarf2_abbrev_entry_t;
134 struct dwarf2_block
136 unsigned size;
137 const unsigned char* ptr;
140 struct attribute
142 unsigned long form;
143 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
144 union
146 unsigned long uvalue;
147 ULONGLONG lluvalue;
148 long svalue;
149 const char* string;
150 struct dwarf2_block block;
151 } u;
154 typedef struct dwarf2_debug_info_s
156 const dwarf2_abbrev_entry_t*abbrev;
157 struct symt* symt;
158 const unsigned char** data;
159 struct vector children;
160 struct dwarf2_debug_info_s* parent;
161 } dwarf2_debug_info_t;
163 typedef struct dwarf2_section_s
165 BOOL compressed;
166 const unsigned char* address;
167 unsigned size;
168 DWORD_PTR rva;
169 } dwarf2_section_t;
171 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
173 typedef struct dwarf2_traverse_context_s
175 const unsigned char* data;
176 const unsigned char* end_data;
177 unsigned char word_size;
178 } dwarf2_traverse_context_t;
180 /* symt_cache indexes */
181 #define sc_void 0
182 #define sc_int1 1
183 #define sc_int2 2
184 #define sc_int4 3
185 #define sc_num 4
187 typedef struct dwarf2_parse_context_s
189 const dwarf2_section_t* sections;
190 unsigned section;
191 struct pool pool;
192 struct module* module;
193 struct symt_compiland* compiland;
194 const struct elf_thunk_area*thunks;
195 struct sparse_array abbrev_table;
196 struct sparse_array debug_info_table;
197 unsigned long load_offset;
198 unsigned long ref_offset;
199 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
200 char* cpp_name;
201 } dwarf2_parse_context_t;
203 /* stored in the dbghelp's module internal structure for later reuse */
204 struct dwarf2_module_info_s
206 dwarf2_section_t debug_loc;
207 dwarf2_section_t debug_frame;
208 dwarf2_section_t eh_frame;
209 unsigned char word_size;
212 #define loc_dwarf2_location_list (loc_user + 0)
213 #define loc_dwarf2_block (loc_user + 1)
215 /* forward declarations */
216 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
218 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
220 return *ptr;
223 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
225 unsigned char uvalue = dwarf2_get_byte(ctx->data);
226 ctx->data += 1;
227 return uvalue;
230 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
232 return *(const UINT16*)ptr;
235 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
237 unsigned short uvalue = dwarf2_get_u2(ctx->data);
238 ctx->data += 2;
239 return uvalue;
242 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
244 return *(const UINT32*)ptr;
247 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
249 unsigned long uvalue = dwarf2_get_u4(ctx->data);
250 ctx->data += 4;
251 return uvalue;
254 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
256 return *(const UINT64*)ptr;
259 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
261 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
262 ctx->data += 8;
263 return uvalue;
266 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
268 unsigned long ret = 0;
269 unsigned char byte;
270 unsigned shift = 0;
274 byte = dwarf2_get_byte(ptr++);
275 ret |= (byte & 0x7f) << shift;
276 shift += 7;
277 } while (byte & 0x80);
279 if (end) *end = ptr;
280 return ret;
283 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
285 unsigned long ret;
287 assert(ctx);
289 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
291 return ret;
294 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
296 long ret = 0;
297 unsigned char byte;
298 unsigned shift = 0;
299 const unsigned size = sizeof(int) * 8;
303 byte = dwarf2_get_byte(ptr++);
304 ret |= (byte & 0x7f) << shift;
305 shift += 7;
306 } while (byte & 0x80);
307 if (end) *end = ptr;
309 /* as spec: sign bit of byte is 2nd high order bit (80x40)
310 * -> 0x80 is used as flag.
312 if ((shift < size) && (byte & 0x40))
314 ret |= - (1 << shift);
316 return ret;
319 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
321 long ret = 0;
323 assert(ctx);
325 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
326 return ret;
329 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
331 unsigned ret;
332 for (ret = 0; ctx->data[ret] & 0x80; ret++);
333 return ret + 1;
336 /******************************************************************
337 * dwarf2_get_addr
339 * Returns an address.
340 * We assume that in all cases word size from Dwarf matches the size of
341 * addresses in platform where the exec is compiled.
343 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
345 unsigned long ret;
347 switch (word_size)
349 case 4:
350 ret = dwarf2_get_u4(ptr);
351 break;
352 case 8:
353 ret = dwarf2_get_u8(ptr);
354 break;
355 default:
356 FIXME("Unsupported Word Size %u\n", word_size);
357 ret = 0;
359 return ret;
362 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
364 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
365 ctx->data += ctx->word_size;
366 return ret;
369 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
371 return wine_dbg_sprintf("ctx(%p)", ctx->data);
374 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
376 return wine_dbg_sprintf("ctx(%p,%s)",
377 ctx, debugstr_w(ctx->module->module.ModuleName));
380 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
382 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
383 di->abbrev, di->symt);
386 static dwarf2_abbrev_entry_t*
387 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
388 unsigned long entry_code)
390 assert( NULL != abbrev_table );
391 return sparse_array_find(abbrev_table, entry_code);
394 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
395 struct sparse_array* abbrev_table,
396 struct pool* pool)
398 unsigned long entry_code;
399 dwarf2_abbrev_entry_t* abbrev_entry;
400 dwarf2_abbrev_entry_attr_t* new = NULL;
401 dwarf2_abbrev_entry_attr_t* last = NULL;
402 unsigned long attribute;
403 unsigned long form;
405 assert( NULL != abbrev_ctx );
407 TRACE("%s, end at %p\n",
408 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
410 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
411 while (abbrev_ctx->data < abbrev_ctx->end_data)
413 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
414 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
415 TRACE("found entry_code %lu\n", entry_code);
416 if (!entry_code)
418 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
419 break;
421 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
422 assert( NULL != abbrev_entry );
424 abbrev_entry->entry_code = entry_code;
425 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
426 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
427 abbrev_entry->attrs = NULL;
428 abbrev_entry->num_attr = 0;
430 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
431 abbrev_table, sparse_array_length(abbrev_table),
432 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
434 last = NULL;
435 while (1)
437 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
438 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
439 if (!attribute) break;
441 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
442 assert(new);
444 new->attribute = attribute;
445 new->form = form;
446 new->next = NULL;
447 if (abbrev_entry->attrs) last->next = new;
448 else abbrev_entry->attrs = new;
449 last = new;
450 abbrev_entry->num_attr++;
453 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
456 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
457 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
459 unsigned step;
461 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
463 switch (abbrev_attr->form)
465 case DW_FORM_flag_present: step = 0; break;
466 case DW_FORM_ref_addr:
467 case DW_FORM_addr: step = ctx->word_size; break;
468 case DW_FORM_flag:
469 case DW_FORM_data1:
470 case DW_FORM_ref1: step = 1; break;
471 case DW_FORM_data2:
472 case DW_FORM_ref2: step = 2; break;
473 case DW_FORM_data4:
474 case DW_FORM_ref4:
475 case DW_FORM_strp: step = 4; break;
476 case DW_FORM_data8:
477 case DW_FORM_ref8: step = 8; break;
478 case DW_FORM_sdata:
479 case DW_FORM_ref_udata:
480 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
481 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
482 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
483 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
484 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
485 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
486 default:
487 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
488 return;
490 ctx->data += step;
493 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
494 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
495 const unsigned char* data,
496 struct attribute* attr)
498 attr->form = abbrev_attr->form;
499 switch (attr->form)
501 case DW_FORM_ref_addr:
502 case DW_FORM_addr:
503 attr->u.uvalue = dwarf2_get_addr(data,
504 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
505 TRACE("addr<0x%lx>\n", attr->u.uvalue);
506 break;
508 case DW_FORM_flag:
509 attr->u.uvalue = dwarf2_get_byte(data);
510 TRACE("flag<0x%lx>\n", attr->u.uvalue);
511 break;
513 case DW_FORM_flag_present:
514 attr->u.uvalue = 1;
515 TRACE("flag_present\n");
516 break;
518 case DW_FORM_data1:
519 attr->u.uvalue = dwarf2_get_byte(data);
520 TRACE("data1<%lu>\n", attr->u.uvalue);
521 break;
523 case DW_FORM_data2:
524 attr->u.uvalue = dwarf2_get_u2(data);
525 TRACE("data2<%lu>\n", attr->u.uvalue);
526 break;
528 case DW_FORM_data4:
529 attr->u.uvalue = dwarf2_get_u4(data);
530 TRACE("data4<%lu>\n", attr->u.uvalue);
531 break;
533 case DW_FORM_data8:
534 attr->u.lluvalue = dwarf2_get_u8(data);
535 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
536 break;
538 case DW_FORM_ref1:
539 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
540 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
541 break;
543 case DW_FORM_ref2:
544 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
545 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
546 break;
548 case DW_FORM_ref4:
549 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
550 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
551 break;
553 case DW_FORM_ref8:
554 FIXME("Unhandled 64-bit support\n");
555 break;
557 case DW_FORM_sdata:
558 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
559 break;
561 case DW_FORM_ref_udata:
562 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
563 break;
565 case DW_FORM_udata:
566 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
567 break;
569 case DW_FORM_string:
570 attr->u.string = (const char *)data;
571 TRACE("string<%s>\n", debugstr_a(attr->u.string));
572 break;
574 case DW_FORM_strp:
576 unsigned long offset = dwarf2_get_u4(data);
577 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
579 TRACE("strp<%s>\n", debugstr_a(attr->u.string));
580 break;
582 case DW_FORM_block:
583 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
584 break;
586 case DW_FORM_block1:
587 attr->u.block.size = dwarf2_get_byte(data);
588 attr->u.block.ptr = data + 1;
589 break;
591 case DW_FORM_block2:
592 attr->u.block.size = dwarf2_get_u2(data);
593 attr->u.block.ptr = data + 2;
594 break;
596 case DW_FORM_block4:
597 attr->u.block.size = dwarf2_get_u4(data);
598 attr->u.block.ptr = data + 4;
599 break;
601 default:
602 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
603 break;
607 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
608 const dwarf2_debug_info_t* di,
609 unsigned at, struct attribute* attr)
611 unsigned i, refidx = 0;
612 dwarf2_abbrev_entry_attr_t* abbrev_attr;
613 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
615 attr->gotten_from = attr_direct;
616 while (di)
618 ref_abbrev_attr = NULL;
619 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
621 if (abbrev_attr->attribute == at)
623 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
624 return TRUE;
626 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
627 abbrev_attr->attribute == DW_AT_specification) &&
628 at != DW_AT_sibling)
630 if (ref_abbrev_attr)
631 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
632 ref_abbrev_attr = abbrev_attr;
633 refidx = i;
634 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
635 attr_abstract_origin : attr_specification;
638 /* do we have either an abstract origin or a specification debug entry to look into ? */
639 if (!ref_abbrev_attr) break;
640 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
641 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
642 FIXME("Should have found the debug info entry\n");
644 return FALSE;
647 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
649 #define Wine_DW_no_register 0x7FFFFFFF
651 static unsigned dwarf2_map_register(int regno)
653 if (regno == Wine_DW_no_register)
655 FIXME("What the heck map reg 0x%x\n",regno);
656 return 0;
658 return dbghelp_current_cpu->map_dwarf_register(regno, FALSE);
661 static enum location_error
662 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
663 HANDLE hproc, const struct location* frame)
665 DWORD_PTR tmp, stack[64];
666 unsigned stk;
667 unsigned char op;
668 BOOL piece_found = FALSE;
670 stack[stk = 0] = 0;
672 loc->kind = loc_absolute;
673 loc->reg = Wine_DW_no_register;
675 while (ctx->data < ctx->end_data)
677 op = dwarf2_parse_byte(ctx);
679 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
680 stack[++stk] = op - DW_OP_lit0;
681 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
683 /* dbghelp APIs don't know how to cope with this anyway
684 * (for example 'long long' stored in two registers)
685 * FIXME: We should tell winedbg how to deal with it (sigh)
687 if (!piece_found)
689 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0);
690 if (loc->reg != Wine_DW_no_register)
691 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
692 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
693 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
694 loc->reg = cvreg;
696 loc->kind = loc_register;
698 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
700 /* dbghelp APIs don't know how to cope with this anyway
701 * (for example 'long long' stored in two registers)
702 * FIXME: We should tell winedbg how to deal with it (sigh)
704 if (!piece_found)
706 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0);
707 if (loc->reg != Wine_DW_no_register)
708 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
709 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
710 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
711 loc->reg = cvreg;
713 stack[++stk] = dwarf2_leb128_as_signed(ctx);
714 loc->kind = loc_regrel;
716 else switch (op)
718 case DW_OP_nop: break;
719 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
720 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
721 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
722 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
723 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
724 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
725 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
726 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
727 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
728 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
729 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
730 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
731 case DW_OP_drop: stk--; break;
732 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
733 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
734 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
735 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
736 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
737 case DW_OP_neg: stack[stk] = -stack[stk]; break;
738 case DW_OP_not: stack[stk] = ~stack[stk]; break;
739 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
740 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
741 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
742 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
743 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
744 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
745 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
746 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
747 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
748 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
749 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
750 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
751 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
752 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
753 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
754 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
755 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
756 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
757 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
758 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
759 case DW_OP_regx:
760 tmp = dwarf2_leb128_as_unsigned(ctx);
761 if (!piece_found)
763 if (loc->reg != Wine_DW_no_register)
764 FIXME("Only supporting one reg\n");
765 loc->reg = dwarf2_map_register(tmp);
767 loc->kind = loc_register;
768 break;
769 case DW_OP_bregx:
770 tmp = dwarf2_leb128_as_unsigned(ctx);
771 if (loc->reg != Wine_DW_no_register)
772 FIXME("Only supporting one regx\n");
773 loc->reg = dwarf2_map_register(tmp);
774 stack[++stk] = dwarf2_leb128_as_signed(ctx);
775 loc->kind = loc_regrel;
776 break;
777 case DW_OP_fbreg:
778 if (loc->reg != Wine_DW_no_register)
779 FIXME("Only supporting one reg (%s/%d -> -2)\n",
780 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
781 if (frame && frame->kind == loc_register)
783 loc->kind = loc_regrel;
784 loc->reg = frame->reg;
785 stack[++stk] = dwarf2_leb128_as_signed(ctx);
787 else if (frame && frame->kind == loc_regrel)
789 loc->kind = loc_regrel;
790 loc->reg = frame->reg;
791 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
793 else
795 /* FIXME: this could be later optimized by not recomputing
796 * this very location expression
798 loc->kind = loc_dwarf2_block;
799 stack[++stk] = dwarf2_leb128_as_signed(ctx);
801 break;
802 case DW_OP_piece:
804 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
805 WARN("Not handling OP_piece (size=%d)\n", sz);
806 piece_found = TRUE;
808 break;
809 case DW_OP_deref:
810 if (!stk)
812 FIXME("Unexpected empty stack\n");
813 return loc_err_internal;
815 if (loc->reg != Wine_DW_no_register)
817 WARN("Too complex expression for deref\n");
818 return loc_err_too_complex;
820 if (hproc)
822 DWORD_PTR addr = stack[stk--];
823 DWORD_PTR deref = 0;
825 if (!ReadProcessMemory(hproc, (void*)addr, &deref, ctx->word_size, NULL))
827 WARN("Couldn't read memory at %lx\n", addr);
828 return loc_err_cant_read;
830 stack[++stk] = deref;
832 else
834 loc->kind = loc_dwarf2_block;
836 break;
837 case DW_OP_deref_size:
838 if (!stk)
840 FIXME("Unexpected empty stack\n");
841 return loc_err_internal;
843 if (loc->reg != Wine_DW_no_register)
845 WARN("Too complex expression for deref\n");
846 return loc_err_too_complex;
848 if (hproc)
850 DWORD_PTR addr = stack[stk--];
851 BYTE derefsize = dwarf2_parse_byte(ctx);
852 DWORD64 deref;
854 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
856 WARN("Couldn't read memory at %lx\n", addr);
857 return loc_err_cant_read;
860 switch (derefsize)
862 case 1: stack[++stk] = *(unsigned char*)&deref; break;
863 case 2: stack[++stk] = *(unsigned short*)&deref; break;
864 case 4: stack[++stk] = *(DWORD*)&deref; break;
865 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
868 else
870 dwarf2_parse_byte(ctx);
871 loc->kind = loc_dwarf2_block;
873 break;
874 case DW_OP_stack_value:
875 /* Expected behaviour is that this is the last instruction of this
876 * expression and just the "top of stack" value should be put to loc->offset. */
877 break;
878 default:
879 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
880 FIXME("Unhandled attr op: %x\n", op);
881 /* FIXME else unhandled extension */
882 return loc_err_internal;
885 loc->offset = stack[stk];
886 return 0;
889 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
890 const dwarf2_debug_info_t* di,
891 unsigned long dw,
892 struct location* loc,
893 const struct location* frame)
895 struct attribute xloc;
897 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
899 switch (xloc.form)
901 case DW_FORM_data1: case DW_FORM_data2:
902 case DW_FORM_udata: case DW_FORM_sdata:
903 loc->kind = loc_absolute;
904 loc->reg = 0;
905 loc->offset = xloc.u.uvalue;
906 return TRUE;
907 case DW_FORM_data4: case DW_FORM_data8:
908 loc->kind = loc_dwarf2_location_list;
909 loc->reg = Wine_DW_no_register;
910 loc->offset = xloc.u.uvalue;
911 return TRUE;
912 case DW_FORM_block:
913 case DW_FORM_block1:
914 case DW_FORM_block2:
915 case DW_FORM_block4:
916 break;
917 default: FIXME("Unsupported yet form %lx\n", xloc.form);
918 return FALSE;
921 /* assume we have a block form */
923 if (xloc.u.block.size)
925 dwarf2_traverse_context_t lctx;
926 enum location_error err;
928 lctx.data = xloc.u.block.ptr;
929 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
930 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
932 err = compute_location(&lctx, loc, NULL, frame);
933 if (err < 0)
935 loc->kind = loc_error;
936 loc->reg = err;
938 else if (loc->kind == loc_dwarf2_block)
940 unsigned* ptr = pool_alloc(&ctx->module->pool,
941 sizeof(unsigned) + xloc.u.block.size);
942 *ptr = xloc.u.block.size;
943 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
944 loc->offset = (unsigned long)ptr;
945 compute_location(&lctx, loc, NULL, frame);
948 return TRUE;
951 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
952 const dwarf2_debug_info_t* di)
954 struct attribute attr;
955 dwarf2_debug_info_t* type;
957 if (!dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
958 return NULL;
959 if (!(type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue)))
961 FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
962 return NULL;
964 if (!type->symt)
966 /* load the debug info entity */
967 dwarf2_load_one_entry(ctx, type);
968 if (!type->symt)
969 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
971 return type->symt;
974 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
976 char* last;
977 struct attribute diname;
978 struct attribute spec;
980 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
981 if (!ctx->cpp_name)
982 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
983 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
984 strcpy(last, name);
986 /* if the di is a definition, but has also a (previous) declaration, then scope must
987 * be gotten from declaration not definition
989 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
991 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
992 if (!di)
994 FIXME("Should have found the debug info entry\n");
995 return NULL;
999 for (di = di->parent; di; di = di->parent)
1001 switch (di->abbrev->tag)
1003 case DW_TAG_namespace:
1004 case DW_TAG_structure_type:
1005 case DW_TAG_class_type:
1006 case DW_TAG_interface_type:
1007 case DW_TAG_union_type:
1008 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
1010 size_t len = strlen(diname.u.string);
1011 last -= 2 + len;
1012 if (last < ctx->cpp_name) return NULL;
1013 memcpy(last, diname.u.string, len);
1014 last[len] = last[len + 1] = ':';
1016 break;
1017 default:
1018 break;
1021 return last;
1024 /******************************************************************
1025 * dwarf2_read_range
1027 * read a range for a given debug_info (either using AT_range attribute, in which
1028 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1029 * in all cases, range is relative to beginning of compilation unit
1031 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1032 unsigned long* plow, unsigned long* phigh)
1034 struct attribute range;
1036 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1038 dwarf2_traverse_context_t traverse;
1039 unsigned long low, high;
1041 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1042 traverse.end_data = ctx->sections[section_ranges].address +
1043 ctx->sections[section_ranges].size;
1044 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1046 *plow = ULONG_MAX;
1047 *phigh = 0;
1048 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1050 low = dwarf2_parse_addr(&traverse);
1051 high = dwarf2_parse_addr(&traverse);
1052 if (low == 0 && high == 0) break;
1053 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1054 if (low < *plow) *plow = low;
1055 if (high > *phigh) *phigh = high;
1057 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1058 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1060 return TRUE;
1062 else
1064 struct attribute low_pc;
1065 struct attribute high_pc;
1067 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1068 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1069 return FALSE;
1070 *plow = low_pc.u.uvalue;
1071 *phigh = high_pc.u.uvalue;
1072 return TRUE;
1076 /******************************************************************
1077 * dwarf2_read_one_debug_info
1079 * Loads into memory one debug info entry, and recursively its children (if any)
1081 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1082 dwarf2_traverse_context_t* traverse,
1083 dwarf2_debug_info_t* parent_di,
1084 dwarf2_debug_info_t** pdi)
1086 const dwarf2_abbrev_entry_t*abbrev;
1087 unsigned long entry_code;
1088 unsigned long offset;
1089 dwarf2_debug_info_t* di;
1090 dwarf2_debug_info_t* child;
1091 dwarf2_debug_info_t** where;
1092 dwarf2_abbrev_entry_attr_t* attr;
1093 unsigned i;
1094 struct attribute sibling;
1096 offset = traverse->data - ctx->sections[ctx->section].address;
1097 entry_code = dwarf2_leb128_as_unsigned(traverse);
1098 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1099 if (!entry_code)
1101 *pdi = NULL;
1102 return TRUE;
1104 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1105 if (!abbrev)
1107 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1108 return FALSE;
1110 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1111 if (!di) return FALSE;
1112 di->abbrev = abbrev;
1113 di->symt = NULL;
1114 di->parent = parent_di;
1116 if (abbrev->num_attr)
1118 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1119 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1121 di->data[i] = traverse->data;
1122 dwarf2_swallow_attribute(traverse, attr);
1125 else di->data = NULL;
1126 if (abbrev->have_child)
1128 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1129 while (traverse->data < traverse->end_data)
1131 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1132 if (!child) break;
1133 where = vector_add(&di->children, &ctx->pool);
1134 if (!where) return FALSE;
1135 *where = child;
1138 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1139 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1141 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1142 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1143 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1145 *pdi = di;
1146 return TRUE;
1149 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1150 dwarf2_debug_info_t* di)
1152 struct attribute spec;
1154 while (di)
1156 if (di->abbrev->have_child)
1157 return &di->children;
1158 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1159 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1160 FIXME("Should have found the debug info entry\n");
1162 return NULL;
1165 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1166 dwarf2_debug_info_t* di)
1168 struct attribute name;
1169 struct attribute size;
1170 struct attribute encoding;
1171 enum BasicType bt;
1172 int cache_idx = -1;
1173 if (di->symt) return di->symt;
1175 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1177 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1178 name.u.string = NULL;
1179 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1180 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1182 switch (encoding.u.uvalue)
1184 case DW_ATE_void: bt = btVoid; break;
1185 case DW_ATE_address: bt = btULong; break;
1186 case DW_ATE_boolean: bt = btBool; break;
1187 case DW_ATE_complex_float: bt = btComplex; break;
1188 case DW_ATE_float: bt = btFloat; break;
1189 case DW_ATE_signed: bt = btInt; break;
1190 case DW_ATE_unsigned: bt = btUInt; break;
1191 case DW_ATE_signed_char: bt = btChar; break;
1192 case DW_ATE_unsigned_char: bt = btChar; break;
1193 default: bt = btNoType; break;
1195 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1196 switch (bt)
1198 case btVoid:
1199 assert(size.u.uvalue == 0);
1200 cache_idx = sc_void;
1201 break;
1202 case btInt:
1203 switch (size.u.uvalue)
1205 case 1: cache_idx = sc_int1; break;
1206 case 2: cache_idx = sc_int2; break;
1207 case 4: cache_idx = sc_int4; break;
1209 break;
1210 default: break;
1212 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1213 ctx->symt_cache[cache_idx] = di->symt;
1215 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1216 return di->symt;
1219 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1220 dwarf2_debug_info_t* di)
1222 struct symt* ref_type;
1223 struct attribute name;
1225 if (di->symt) return di->symt;
1227 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1229 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1230 ref_type = dwarf2_lookup_type(ctx, di);
1232 if (name.u.string)
1233 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1234 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1235 return di->symt;
1238 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1239 dwarf2_debug_info_t* di)
1241 struct symt* ref_type;
1242 struct attribute size;
1244 if (di->symt) return di->symt;
1246 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1248 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1249 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1251 ref_type = ctx->symt_cache[sc_void];
1252 assert(ref_type);
1254 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1255 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1256 return di->symt;
1259 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1260 dwarf2_debug_info_t* di)
1262 struct symt* ref_type;
1263 struct symt* idx_type = NULL;
1264 struct attribute min, max, cnt;
1265 dwarf2_debug_info_t* child;
1266 unsigned int i;
1267 const struct vector* children;
1269 if (di->symt) return di->symt;
1271 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1273 ref_type = dwarf2_lookup_type(ctx, di);
1275 if (!(children = dwarf2_get_di_children(ctx, di)))
1277 /* fake an array with unknown size */
1278 /* FIXME: int4 even on 64bit machines??? */
1279 idx_type = ctx->symt_cache[sc_int4];
1280 min.u.uvalue = 0;
1281 max.u.uvalue = -1;
1283 else for (i = 0; i < vector_length(children); i++)
1285 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1286 switch (child->abbrev->tag)
1288 case DW_TAG_subrange_type:
1289 idx_type = dwarf2_lookup_type(ctx, child);
1290 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1291 min.u.uvalue = 0;
1292 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1293 max.u.uvalue = 0;
1294 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1295 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1296 break;
1297 default:
1298 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1299 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1300 break;
1303 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1304 return di->symt;
1307 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1308 dwarf2_debug_info_t* di)
1310 struct symt* ref_type;
1312 if (di->symt) return di->symt;
1314 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1316 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1318 ref_type = ctx->symt_cache[sc_void];
1319 assert(ref_type);
1321 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1322 di->symt = ref_type;
1324 return ref_type;
1327 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1328 dwarf2_debug_info_t* di)
1330 struct symt* ref_type;
1332 if (di->symt) return di->symt;
1334 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1336 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1338 ref_type = ctx->symt_cache[sc_void];
1339 assert(ref_type);
1341 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1342 di->symt = ref_type;
1344 return ref_type;
1347 static struct symt* dwarf2_parse_unspecified_type(dwarf2_parse_context_t* ctx,
1348 dwarf2_debug_info_t* di)
1350 struct attribute name;
1351 struct attribute size;
1352 struct symt_basic *basic;
1354 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1356 if (di->symt) return di->symt;
1358 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1359 name.u.string = "void";
1360 size.u.uvalue = sizeof(void *);
1362 basic = symt_new_basic(ctx->module, btVoid, name.u.string, size.u.uvalue);
1363 di->symt = &basic->symt;
1365 if (!ctx->symt_cache[sc_void])
1366 ctx->symt_cache[sc_void] = di->symt;
1368 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1369 return di->symt;
1372 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1373 dwarf2_debug_info_t* di)
1375 struct symt* ref_type = NULL;
1377 if (di->symt) return di->symt;
1379 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1381 ref_type = dwarf2_lookup_type(ctx, di);
1382 /* FIXME: for now, we hard-wire C++ references to pointers */
1383 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1385 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1387 return di->symt;
1390 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1391 dwarf2_debug_info_t* di,
1392 struct symt_udt* parent)
1394 struct symt* elt_type;
1395 struct attribute name;
1396 struct attribute bit_size;
1397 struct attribute bit_offset;
1398 struct location loc;
1400 assert(parent);
1402 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1404 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1405 elt_type = dwarf2_lookup_type(ctx, di);
1406 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1408 if (loc.kind != loc_absolute)
1410 FIXME("Found register, while not expecting it\n");
1411 loc.offset = 0;
1413 else
1414 TRACE("found member_location at %s -> %lu\n",
1415 dwarf2_debug_ctx(ctx), loc.offset);
1417 else
1418 loc.offset = 0;
1419 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1420 bit_size.u.uvalue = 0;
1421 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1423 /* FIXME: we should only do this when implementation is LSB (which is
1424 * the case on i386 processors)
1426 struct attribute nbytes;
1427 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1429 DWORD64 size;
1430 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1431 (unsigned long)size : 0;
1433 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1435 else bit_offset.u.uvalue = 0;
1436 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1437 (loc.offset << 3) + bit_offset.u.uvalue,
1438 bit_size.u.uvalue);
1440 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1443 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1444 dwarf2_debug_info_t* di);
1446 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1447 dwarf2_debug_info_t* di,
1448 enum UdtKind udt)
1450 struct attribute name;
1451 struct attribute size;
1452 struct vector* children;
1453 dwarf2_debug_info_t*child;
1454 unsigned int i;
1456 if (di->symt) return di->symt;
1458 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1460 /* quirk... FIXME provide real support for anonymous UDTs */
1461 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1462 name.u.string = "zz_anon_zz";
1463 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1465 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1466 size.u.uvalue, udt)->symt;
1468 children = dwarf2_get_di_children(ctx, di);
1469 if (children) for (i = 0; i < vector_length(children); i++)
1471 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1473 switch (child->abbrev->tag)
1475 case DW_TAG_array_type:
1476 dwarf2_parse_array_type(ctx, di);
1477 break;
1478 case DW_TAG_member:
1479 /* FIXME: should I follow the sibling stuff ?? */
1480 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1481 break;
1482 case DW_TAG_enumeration_type:
1483 dwarf2_parse_enumeration_type(ctx, child);
1484 break;
1485 case DW_TAG_subprogram:
1486 dwarf2_parse_subprogram(ctx, child);
1487 break;
1488 case DW_TAG_const_type:
1489 dwarf2_parse_const_type(ctx, child);
1490 break;
1491 case DW_TAG_structure_type:
1492 case DW_TAG_class_type:
1493 case DW_TAG_union_type:
1494 case DW_TAG_typedef:
1495 /* FIXME: we need to handle nested udt definitions */
1496 case DW_TAG_inheritance:
1497 case DW_TAG_template_type_param:
1498 case DW_TAG_template_value_param:
1499 case DW_TAG_variable:
1500 case DW_TAG_imported_declaration:
1501 case DW_TAG_ptr_to_member_type:
1502 case DW_TAG_GNU_template_parameter_pack:
1503 case DW_TAG_GNU_formal_parameter_pack:
1504 /* FIXME: some C++ related stuff */
1505 break;
1506 default:
1507 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1508 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1509 break;
1513 return di->symt;
1516 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1517 dwarf2_debug_info_t* di,
1518 struct symt_enum* parent)
1520 struct attribute name;
1521 struct attribute value;
1523 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1525 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1526 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1527 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1529 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1532 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1533 dwarf2_debug_info_t* di)
1535 struct attribute name;
1536 struct attribute size;
1537 struct symt_basic* basetype;
1538 struct vector* children;
1539 dwarf2_debug_info_t*child;
1540 unsigned int i;
1542 if (di->symt) return di->symt;
1544 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1546 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1547 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1549 switch (size.u.uvalue) /* FIXME: that's wrong */
1551 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1552 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1553 default:
1554 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1557 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1559 children = dwarf2_get_di_children(ctx, di);
1560 /* FIXME: should we use the sibling stuff ?? */
1561 if (children) for (i = 0; i < vector_length(children); i++)
1563 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1565 switch (child->abbrev->tag)
1567 case DW_TAG_enumerator:
1568 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1569 break;
1570 default:
1571 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1572 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1575 return di->symt;
1578 /* structure used to pass information around when parsing a subprogram */
1579 typedef struct dwarf2_subprogram_s
1581 dwarf2_parse_context_t* ctx;
1582 struct symt_function* func;
1583 BOOL non_computed_variable;
1584 struct location frame;
1585 } dwarf2_subprogram_t;
1587 /******************************************************************
1588 * dwarf2_parse_variable
1590 * Parses any variable (parameter, local/global variable)
1592 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1593 struct symt_block* block,
1594 dwarf2_debug_info_t* di)
1596 struct symt* param_type;
1597 struct attribute name, value;
1598 struct location loc;
1599 BOOL is_pmt;
1601 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1603 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1604 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1606 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1607 /* cannot do much without the name, the functions below won't like it. */
1608 return;
1610 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1611 &loc, &subpgm->frame))
1613 struct attribute ext;
1615 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1616 debugstr_a(name.u.string), loc.kind, loc.offset, loc.reg,
1617 dwarf2_debug_ctx(subpgm->ctx));
1619 switch (loc.kind)
1621 case loc_error:
1622 break;
1623 case loc_absolute:
1624 /* it's a global variable */
1625 /* FIXME: we don't handle its scope yet */
1626 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1627 ext.u.uvalue = 0;
1628 loc.offset += subpgm->ctx->load_offset;
1629 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1630 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1631 loc, 0, param_type);
1632 break;
1633 default:
1634 subpgm->non_computed_variable = TRUE;
1635 /* fall through */
1636 case loc_register:
1637 case loc_regrel:
1638 /* either a pmt/variable relative to frame pointer or
1639 * pmt/variable in a register
1641 assert(subpgm->func);
1642 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1643 is_pmt ? DataIsParam : DataIsLocal,
1644 &loc, block, param_type, name.u.string);
1645 break;
1648 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1650 VARIANT v;
1651 if (subpgm->func) WARN("Unsupported constant %s in function\n", debugstr_a(name.u.string));
1652 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", debugstr_a(name.u.string));
1653 switch (value.form)
1655 case DW_FORM_data1:
1656 case DW_FORM_data2:
1657 case DW_FORM_data4:
1658 case DW_FORM_udata:
1659 case DW_FORM_addr:
1660 v.n1.n2.vt = VT_UI4;
1661 v.n1.n2.n3.lVal = value.u.uvalue;
1662 break;
1664 case DW_FORM_data8:
1665 v.n1.n2.vt = VT_UI8;
1666 v.n1.n2.n3.llVal = value.u.lluvalue;
1667 break;
1669 case DW_FORM_sdata:
1670 v.n1.n2.vt = VT_I4;
1671 v.n1.n2.n3.lVal = value.u.svalue;
1672 break;
1674 case DW_FORM_strp:
1675 case DW_FORM_string:
1676 /* FIXME: native doesn't report const strings from here !!
1677 * however, the value of the string is in the code somewhere
1679 v.n1.n2.vt = VT_I1 | VT_BYREF;
1680 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1681 break;
1683 case DW_FORM_block:
1684 case DW_FORM_block1:
1685 case DW_FORM_block2:
1686 case DW_FORM_block4:
1687 v.n1.n2.vt = VT_I4;
1688 switch (value.u.block.size)
1690 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1691 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1692 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1693 default:
1694 v.n1.n2.vt = VT_I1 | VT_BYREF;
1695 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1696 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1698 break;
1700 default:
1701 FIXME("Unsupported form for const value %s (%lx)\n",
1702 debugstr_a(name.u.string), value.form);
1703 v.n1.n2.vt = VT_EMPTY;
1705 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1706 name.u.string, param_type, &v)->symt;
1708 else
1710 /* variable has been optimized away... report anyway */
1711 loc.kind = loc_error;
1712 loc.reg = loc_err_no_location;
1713 if (subpgm->func)
1715 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1716 is_pmt ? DataIsParam : DataIsLocal,
1717 &loc, block, param_type, name.u.string);
1719 else
1721 WARN("dropping global variable %s which has been optimized away\n", debugstr_a(name.u.string));
1724 if (is_pmt && subpgm->func && subpgm->func->type)
1725 symt_add_function_signature_parameter(subpgm->ctx->module,
1726 (struct symt_function_signature*)subpgm->func->type,
1727 param_type);
1729 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1732 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1733 const dwarf2_debug_info_t* di)
1735 struct attribute name;
1736 struct attribute low_pc;
1737 struct location loc;
1739 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1741 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1742 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1743 name.u.string = NULL;
1745 loc.kind = loc_absolute;
1746 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1747 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1748 &loc, name.u.string);
1751 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1752 struct symt_block* parent_block,
1753 dwarf2_debug_info_t* di);
1755 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1756 dwarf2_debug_info_t* di);
1758 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1759 struct symt_block* parent_block,
1760 dwarf2_debug_info_t* di)
1762 struct symt_block* block;
1763 unsigned long low_pc, high_pc;
1764 struct vector* children;
1765 dwarf2_debug_info_t*child;
1766 unsigned int i;
1768 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1770 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1772 FIXME("cannot read range\n");
1773 return;
1776 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1777 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1778 high_pc - low_pc);
1780 children = dwarf2_get_di_children(subpgm->ctx, di);
1781 if (children) for (i = 0; i < vector_length(children); i++)
1783 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1785 switch (child->abbrev->tag)
1787 case DW_TAG_formal_parameter:
1788 case DW_TAG_variable:
1789 dwarf2_parse_variable(subpgm, block, child);
1790 break;
1791 case DW_TAG_lexical_block:
1792 dwarf2_parse_subprogram_block(subpgm, block, child);
1793 break;
1794 case DW_TAG_inlined_subroutine:
1795 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1796 break;
1797 case DW_TAG_label:
1798 dwarf2_parse_subprogram_label(subpgm, child);
1799 break;
1800 case DW_TAG_GNU_call_site:
1801 /* this isn't properly supported by dbghelp interface. skip it for now */
1802 break;
1803 default:
1804 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1805 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1806 dwarf2_debug_di(di));
1809 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1812 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1813 struct symt_block* parent_block,
1814 dwarf2_debug_info_t* di)
1816 struct symt_block* block;
1817 unsigned long low_pc, high_pc;
1818 struct vector* children;
1819 dwarf2_debug_info_t*child;
1820 unsigned int i;
1822 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1824 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1826 WARN("no range\n");
1827 return;
1830 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1831 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1832 high_pc - low_pc);
1834 children = dwarf2_get_di_children(subpgm->ctx, di);
1835 if (children) for (i = 0; i < vector_length(children); i++)
1837 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1839 switch (child->abbrev->tag)
1841 case DW_TAG_inlined_subroutine:
1842 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1843 break;
1844 case DW_TAG_variable:
1845 dwarf2_parse_variable(subpgm, block, child);
1846 break;
1847 case DW_TAG_pointer_type:
1848 dwarf2_parse_pointer_type(subpgm->ctx, di);
1849 break;
1850 case DW_TAG_subroutine_type:
1851 dwarf2_parse_subroutine_type(subpgm->ctx, di);
1852 break;
1853 case DW_TAG_const_type:
1854 dwarf2_parse_const_type(subpgm->ctx, di);
1855 break;
1856 case DW_TAG_lexical_block:
1857 dwarf2_parse_subprogram_block(subpgm, block, child);
1858 break;
1859 case DW_TAG_subprogram:
1860 /* FIXME: likely a declaration (to be checked)
1861 * skip it for now
1863 break;
1864 case DW_TAG_formal_parameter:
1865 /* FIXME: likely elements for exception handling (GCC flavor)
1866 * Skip it for now
1868 break;
1869 case DW_TAG_imported_module:
1870 /* C++ stuff to be silenced (for now) */
1871 break;
1872 case DW_TAG_GNU_call_site:
1873 /* this isn't properly supported by dbghelp interface. skip it for now */
1874 break;
1875 case DW_TAG_label:
1876 dwarf2_parse_subprogram_label(subpgm, child);
1877 break;
1878 case DW_TAG_class_type:
1879 case DW_TAG_structure_type:
1880 case DW_TAG_union_type:
1881 case DW_TAG_enumeration_type:
1882 case DW_TAG_typedef:
1883 /* the type referred to will be loaded when we need it, so skip it */
1884 break;
1885 default:
1886 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1887 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1891 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1894 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1895 dwarf2_debug_info_t* di)
1897 struct attribute name;
1898 unsigned long low_pc, high_pc;
1899 struct attribute is_decl;
1900 struct attribute inline_flags;
1901 struct symt* ret_type;
1902 struct symt_function_signature* sig_type;
1903 dwarf2_subprogram_t subpgm;
1904 struct vector* children;
1905 dwarf2_debug_info_t* child;
1906 unsigned int i;
1908 if (di->symt) return di->symt;
1910 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1912 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1914 WARN("No name for function... dropping function\n");
1915 return NULL;
1917 /* if it's an abstract representation of an inline function, there should be
1918 * a concrete object that we'll handle
1920 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1921 inline_flags.u.uvalue != DW_INL_not_inlined)
1923 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1924 debugstr_a(name.u.string), inline_flags.u.uvalue);
1925 return NULL;
1928 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1929 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1931 /* it's a real declaration, skip it */
1932 return NULL;
1934 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1936 WARN("cannot get range for %s\n", debugstr_a(name.u.string));
1937 return NULL;
1939 /* As functions (defined as inline assembly) get debug info with dwarf
1940 * (not the case for stabs), we just drop Wine's thunks here...
1941 * Actual thunks will be created in elf_module from the symbol table
1943 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1944 return NULL;
1945 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1947 ret_type = ctx->symt_cache[sc_void];
1948 assert(ret_type);
1950 /* FIXME: assuming C source code */
1951 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1952 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1953 dwarf2_get_cpp_name(ctx, di, name.u.string),
1954 ctx->load_offset + low_pc, high_pc - low_pc,
1955 &sig_type->symt);
1956 di->symt = &subpgm.func->symt;
1957 subpgm.ctx = ctx;
1958 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1959 &subpgm.frame, NULL))
1961 /* on stack !! */
1962 subpgm.frame.kind = loc_regrel;
1963 subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
1964 subpgm.frame.offset = 0;
1966 subpgm.non_computed_variable = FALSE;
1968 children = dwarf2_get_di_children(ctx, di);
1969 if (children) for (i = 0; i < vector_length(children); i++)
1971 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1973 switch (child->abbrev->tag)
1975 case DW_TAG_variable:
1976 case DW_TAG_formal_parameter:
1977 dwarf2_parse_variable(&subpgm, NULL, child);
1978 break;
1979 case DW_TAG_lexical_block:
1980 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1981 break;
1982 case DW_TAG_inlined_subroutine:
1983 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1984 break;
1985 case DW_TAG_pointer_type:
1986 dwarf2_parse_pointer_type(subpgm.ctx, di);
1987 break;
1988 case DW_TAG_const_type:
1989 dwarf2_parse_const_type(subpgm.ctx, di);
1990 break;
1991 case DW_TAG_subprogram:
1992 /* FIXME: likely a declaration (to be checked)
1993 * skip it for now
1995 break;
1996 case DW_TAG_label:
1997 dwarf2_parse_subprogram_label(&subpgm, child);
1998 break;
1999 case DW_TAG_class_type:
2000 case DW_TAG_structure_type:
2001 case DW_TAG_union_type:
2002 case DW_TAG_enumeration_type:
2003 case DW_TAG_typedef:
2004 /* the type referred to will be loaded when we need it, so skip it */
2005 break;
2006 case DW_TAG_unspecified_parameters:
2007 case DW_TAG_template_type_param:
2008 case DW_TAG_template_value_param:
2009 case DW_TAG_GNU_call_site:
2010 case DW_TAG_GNU_template_parameter_pack:
2011 case DW_TAG_GNU_formal_parameter_pack:
2012 /* FIXME: no support in dbghelp's internals so far */
2013 break;
2014 default:
2015 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
2016 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2020 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2022 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
2023 &subpgm.frame, NULL);
2025 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
2027 return di->symt;
2030 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
2031 dwarf2_debug_info_t* di)
2033 struct symt* ret_type;
2034 struct symt_function_signature* sig_type;
2035 struct vector* children;
2036 dwarf2_debug_info_t* child;
2037 unsigned int i;
2039 if (di->symt) return di->symt;
2041 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2043 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
2045 ret_type = ctx->symt_cache[sc_void];
2046 assert(ret_type);
2049 /* FIXME: assuming C source code */
2050 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
2052 children = dwarf2_get_di_children(ctx, di);
2053 if (children) for (i = 0; i < vector_length(children); i++)
2055 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2057 switch (child->abbrev->tag)
2059 case DW_TAG_formal_parameter:
2060 symt_add_function_signature_parameter(ctx->module, sig_type,
2061 dwarf2_lookup_type(ctx, child));
2062 break;
2063 case DW_TAG_unspecified_parameters:
2064 WARN("Unsupported unspecified parameters\n");
2065 break;
2069 return di->symt = &sig_type->symt;
2072 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
2073 dwarf2_debug_info_t* di)
2075 struct vector* children;
2076 dwarf2_debug_info_t* child;
2077 unsigned int i;
2079 if (di->symt) return;
2081 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2083 di->symt = ctx->symt_cache[sc_void];
2085 children = dwarf2_get_di_children(ctx, di);
2086 if (children) for (i = 0; i < vector_length(children); i++)
2088 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2089 dwarf2_load_one_entry(ctx, child);
2093 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2094 dwarf2_debug_info_t* di)
2096 switch (di->abbrev->tag)
2098 case DW_TAG_typedef:
2099 dwarf2_parse_typedef(ctx, di);
2100 break;
2101 case DW_TAG_base_type:
2102 dwarf2_parse_base_type(ctx, di);
2103 break;
2104 case DW_TAG_pointer_type:
2105 dwarf2_parse_pointer_type(ctx, di);
2106 break;
2107 case DW_TAG_class_type:
2108 dwarf2_parse_udt_type(ctx, di, UdtClass);
2109 break;
2110 case DW_TAG_structure_type:
2111 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2112 break;
2113 case DW_TAG_union_type:
2114 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2115 break;
2116 case DW_TAG_array_type:
2117 dwarf2_parse_array_type(ctx, di);
2118 break;
2119 case DW_TAG_const_type:
2120 dwarf2_parse_const_type(ctx, di);
2121 break;
2122 case DW_TAG_volatile_type:
2123 dwarf2_parse_volatile_type(ctx, di);
2124 break;
2125 case DW_TAG_unspecified_type:
2126 dwarf2_parse_unspecified_type(ctx, di);
2127 break;
2128 case DW_TAG_reference_type:
2129 dwarf2_parse_reference_type(ctx, di);
2130 break;
2131 case DW_TAG_enumeration_type:
2132 dwarf2_parse_enumeration_type(ctx, di);
2133 break;
2134 case DW_TAG_subprogram:
2135 dwarf2_parse_subprogram(ctx, di);
2136 break;
2137 case DW_TAG_subroutine_type:
2138 dwarf2_parse_subroutine_type(ctx, di);
2139 break;
2140 case DW_TAG_variable:
2142 dwarf2_subprogram_t subpgm;
2144 subpgm.ctx = ctx;
2145 subpgm.func = NULL;
2146 subpgm.frame.kind = loc_absolute;
2147 subpgm.frame.offset = 0;
2148 subpgm.frame.reg = Wine_DW_no_register;
2149 dwarf2_parse_variable(&subpgm, NULL, di);
2151 break;
2152 case DW_TAG_namespace:
2153 dwarf2_parse_namespace(ctx, di);
2154 break;
2155 /* silence a couple of C++ defines */
2156 case DW_TAG_imported_module:
2157 case DW_TAG_imported_declaration:
2158 case DW_TAG_ptr_to_member_type:
2159 break;
2160 default:
2161 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2162 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2166 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2167 const struct vector* v, unsigned file, unsigned line)
2169 struct symt_function* func;
2170 struct symt_ht* symt;
2171 unsigned* psrc;
2173 if (!file || !(psrc = vector_at(v, file - 1))) return;
2175 TRACE("%s %lx %s %u\n",
2176 debugstr_w(module->module.ModuleName), address, debugstr_a(source_get(module, *psrc)), line);
2177 if (!(symt = symt_find_nearest(module, address)) ||
2178 symt->symt.tag != SymTagFunction) return;
2179 func = (struct symt_function*)symt;
2180 symt_add_func_line(module, func, *psrc, line, address - func->address);
2183 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2184 dwarf2_parse_context_t* ctx,
2185 const char* compile_dir,
2186 unsigned long offset)
2188 dwarf2_traverse_context_t traverse;
2189 unsigned long length;
2190 unsigned insn_size, default_stmt;
2191 unsigned line_range, opcode_base;
2192 int line_base;
2193 const unsigned char* opcode_len;
2194 struct vector dirs;
2195 struct vector files;
2196 const char** p;
2198 /* section with line numbers stripped */
2199 if (sections[section_line].address == IMAGE_NO_MAP)
2200 return FALSE;
2202 if (offset + 4 > sections[section_line].size)
2204 WARN("out of bounds offset\n");
2205 return FALSE;
2207 traverse.data = sections[section_line].address + offset;
2208 traverse.end_data = traverse.data + 4;
2209 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2211 length = dwarf2_parse_u4(&traverse);
2212 traverse.end_data = sections[section_line].address + offset + length;
2214 if (offset + 4 + length > sections[section_line].size)
2216 WARN("out of bounds header\n");
2217 return FALSE;
2219 dwarf2_parse_u2(&traverse); /* version */
2220 dwarf2_parse_u4(&traverse); /* header_len */
2221 insn_size = dwarf2_parse_byte(&traverse);
2222 default_stmt = dwarf2_parse_byte(&traverse);
2223 line_base = (signed char)dwarf2_parse_byte(&traverse);
2224 line_range = dwarf2_parse_byte(&traverse);
2225 opcode_base = dwarf2_parse_byte(&traverse);
2227 opcode_len = traverse.data;
2228 traverse.data += opcode_base - 1;
2230 vector_init(&dirs, sizeof(const char*), 4);
2231 p = vector_add(&dirs, &ctx->pool);
2232 *p = compile_dir ? compile_dir : ".";
2233 while (*traverse.data)
2235 const char* rel = (const char*)traverse.data;
2236 unsigned rellen = strlen(rel);
2237 TRACE("Got include %s\n", debugstr_a(rel));
2238 traverse.data += rellen + 1;
2239 p = vector_add(&dirs, &ctx->pool);
2241 if (*rel == '/' || !compile_dir)
2242 *p = rel;
2243 else
2245 /* include directory relative to compile directory */
2246 unsigned baselen = strlen(compile_dir);
2247 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2248 strcpy(tmp, compile_dir);
2249 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2250 strcpy(&tmp[baselen], rel);
2251 *p = tmp;
2255 traverse.data++;
2257 vector_init(&files, sizeof(unsigned), 16);
2258 while (*traverse.data)
2260 unsigned int dir_index, mod_time;
2261 const char* name;
2262 const char* dir;
2263 unsigned* psrc;
2265 name = (const char*)traverse.data;
2266 traverse.data += strlen(name) + 1;
2267 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2268 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2269 length = dwarf2_leb128_as_unsigned(&traverse);
2270 dir = *(const char**)vector_at(&dirs, dir_index);
2271 TRACE("Got file %s/%s (%u,%lu)\n", debugstr_a(dir), debugstr_a(name), mod_time, length);
2272 psrc = vector_add(&files, &ctx->pool);
2273 *psrc = source_new(ctx->module, dir, name);
2275 traverse.data++;
2277 while (traverse.data < traverse.end_data)
2279 unsigned long address = 0;
2280 unsigned file = 1;
2281 unsigned line = 1;
2282 unsigned is_stmt = default_stmt;
2283 BOOL end_sequence = FALSE;
2284 unsigned opcode, extopcode, i;
2286 while (!end_sequence)
2288 opcode = dwarf2_parse_byte(&traverse);
2289 TRACE("Got opcode %x\n", opcode);
2291 if (opcode >= opcode_base)
2293 unsigned delta = opcode - opcode_base;
2295 address += (delta / line_range) * insn_size;
2296 line += line_base + (delta % line_range);
2297 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2299 else
2301 switch (opcode)
2303 case DW_LNS_copy:
2304 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2305 break;
2306 case DW_LNS_advance_pc:
2307 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2308 break;
2309 case DW_LNS_advance_line:
2310 line += dwarf2_leb128_as_signed(&traverse);
2311 break;
2312 case DW_LNS_set_file:
2313 file = dwarf2_leb128_as_unsigned(&traverse);
2314 break;
2315 case DW_LNS_set_column:
2316 dwarf2_leb128_as_unsigned(&traverse);
2317 break;
2318 case DW_LNS_negate_stmt:
2319 is_stmt = !is_stmt;
2320 break;
2321 case DW_LNS_set_basic_block:
2322 break;
2323 case DW_LNS_const_add_pc:
2324 address += ((255 - opcode_base) / line_range) * insn_size;
2325 break;
2326 case DW_LNS_fixed_advance_pc:
2327 address += dwarf2_parse_u2(&traverse);
2328 break;
2329 case DW_LNS_extended_op:
2330 dwarf2_leb128_as_unsigned(&traverse);
2331 extopcode = dwarf2_parse_byte(&traverse);
2332 switch (extopcode)
2334 case DW_LNE_end_sequence:
2335 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2336 end_sequence = TRUE;
2337 break;
2338 case DW_LNE_set_address:
2339 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2340 break;
2341 case DW_LNE_define_file:
2342 FIXME("not handled define file %s\n", debugstr_a((char *)traverse.data));
2343 traverse.data += strlen((const char *)traverse.data) + 1;
2344 dwarf2_leb128_as_unsigned(&traverse);
2345 dwarf2_leb128_as_unsigned(&traverse);
2346 dwarf2_leb128_as_unsigned(&traverse);
2347 break;
2348 case DW_LNE_set_discriminator:
2350 unsigned descr;
2352 descr = dwarf2_leb128_as_unsigned(&traverse);
2353 WARN("not handled discriminator %x\n", descr);
2355 break;
2356 default:
2357 FIXME("Unsupported extended opcode %x\n", extopcode);
2358 break;
2360 break;
2361 default:
2362 WARN("Unsupported opcode %x\n", opcode);
2363 for (i = 0; i < opcode_len[opcode]; i++)
2364 dwarf2_leb128_as_unsigned(&traverse);
2365 break;
2370 return TRUE;
2373 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2374 struct module* module,
2375 const struct elf_thunk_area* thunks,
2376 dwarf2_traverse_context_t* mod_ctx,
2377 unsigned long load_offset)
2379 dwarf2_parse_context_t ctx;
2380 dwarf2_traverse_context_t abbrev_ctx;
2381 dwarf2_debug_info_t* di;
2382 dwarf2_traverse_context_t cu_ctx;
2383 const unsigned char* comp_unit_start = mod_ctx->data;
2384 unsigned long cu_length;
2385 unsigned short cu_version;
2386 unsigned long cu_abbrev_offset;
2387 BOOL ret = FALSE;
2389 cu_length = dwarf2_parse_u4(mod_ctx);
2390 cu_ctx.data = mod_ctx->data;
2391 cu_ctx.end_data = mod_ctx->data + cu_length;
2392 mod_ctx->data += cu_length;
2393 cu_version = dwarf2_parse_u2(&cu_ctx);
2394 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2395 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2397 TRACE("Compilation Unit Header found at 0x%x:\n",
2398 (int)(comp_unit_start - sections[section_debug].address));
2399 TRACE("- length: %lu\n", cu_length);
2400 TRACE("- version: %u\n", cu_version);
2401 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2402 TRACE("- word_size: %u\n", cu_ctx.word_size);
2404 if (cu_version != 2)
2406 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2407 cu_version);
2408 return FALSE;
2411 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2412 mod_ctx->word_size = cu_ctx.word_size;
2414 pool_init(&ctx.pool, 65536);
2415 ctx.sections = sections;
2416 ctx.section = section_debug;
2417 ctx.module = module;
2418 ctx.thunks = thunks;
2419 ctx.load_offset = load_offset;
2420 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2421 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2422 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2423 ctx.cpp_name = NULL;
2425 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2426 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2427 abbrev_ctx.word_size = cu_ctx.word_size;
2428 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2430 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2431 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2433 if (di->abbrev->tag == DW_TAG_compile_unit)
2435 struct attribute name;
2436 struct vector* children;
2437 dwarf2_debug_info_t* child = NULL;
2438 unsigned int i;
2439 struct attribute stmt_list, low_pc;
2440 struct attribute comp_dir;
2442 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2443 name.u.string = NULL;
2445 /* get working directory of current compilation unit */
2446 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2447 comp_dir.u.string = NULL;
2449 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2450 low_pc.u.uvalue = 0;
2451 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2452 source_new(module, comp_dir.u.string, name.u.string));
2453 di->symt = &ctx.compiland->symt;
2454 children = dwarf2_get_di_children(&ctx, di);
2455 if (children) for (i = 0; i < vector_length(children); i++)
2457 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2458 dwarf2_load_one_entry(&ctx, child);
2460 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2462 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2463 module->module.LineNumbers = TRUE;
2465 ret = TRUE;
2467 else FIXME("Should have a compilation unit here\n");
2468 pool_destroy(&ctx.pool);
2469 return ret;
2472 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2473 unsigned long ip, dwarf2_traverse_context_t* lctx)
2475 DWORD_PTR beg, end;
2476 const BYTE* ptr = start;
2477 DWORD len;
2479 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2481 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2482 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2483 if (!beg && !end) break;
2484 len = dwarf2_get_u2(ptr); ptr += 2;
2486 if (beg <= ip && ip < end)
2488 lctx->data = ptr;
2489 lctx->end_data = ptr + len;
2490 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2491 return TRUE;
2493 ptr += len;
2495 WARN("Couldn't find ip in location list\n");
2496 return FALSE;
2499 static enum location_error loc_compute_frame(struct process* pcs,
2500 const struct module_format* modfmt,
2501 const struct symt_function* func,
2502 DWORD_PTR ip, struct location* frame)
2504 struct symt** psym = NULL;
2505 struct location* pframe;
2506 dwarf2_traverse_context_t lctx;
2507 enum location_error err;
2508 unsigned int i;
2510 for (i=0; i<vector_length(&func->vchildren); i++)
2512 psym = vector_at(&func->vchildren, i);
2513 if ((*psym)->tag == SymTagCustom)
2515 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2517 /* First, recompute the frame information, if needed */
2518 switch (pframe->kind)
2520 case loc_regrel:
2521 case loc_register:
2522 *frame = *pframe;
2523 break;
2524 case loc_dwarf2_location_list:
2525 WARN("Searching loclist for %s\n", debugstr_a(func->hash_elt.name));
2526 if (!dwarf2_lookup_loclist(modfmt,
2527 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2528 ip, &lctx))
2529 return loc_err_out_of_scope;
2530 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2531 if (frame->kind >= loc_user)
2533 WARN("Couldn't compute runtime frame location\n");
2534 return loc_err_too_complex;
2536 break;
2537 default:
2538 WARN("Unsupported frame kind %d\n", pframe->kind);
2539 return loc_err_internal;
2541 return 0;
2544 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2545 return loc_err_internal;
2548 enum reg_rule
2550 RULE_UNSET, /* not set at all */
2551 RULE_UNDEFINED, /* undefined value */
2552 RULE_SAME, /* same value as previous frame */
2553 RULE_CFA_OFFSET, /* stored at cfa offset */
2554 RULE_OTHER_REG, /* stored in other register */
2555 RULE_EXPRESSION, /* address specified by expression */
2556 RULE_VAL_EXPRESSION /* value specified by expression */
2559 /* make it large enough for all CPUs */
2560 #define NB_FRAME_REGS 64
2561 #define MAX_SAVED_STATES 16
2563 struct frame_state
2565 ULONG_PTR cfa_offset;
2566 unsigned char cfa_reg;
2567 enum reg_rule cfa_rule;
2568 enum reg_rule rules[NB_FRAME_REGS];
2569 ULONG_PTR regs[NB_FRAME_REGS];
2572 struct frame_info
2574 ULONG_PTR ip;
2575 ULONG_PTR code_align;
2576 LONG_PTR data_align;
2577 unsigned char retaddr_reg;
2578 unsigned char fde_encoding;
2579 unsigned char lsda_encoding;
2580 unsigned char signal_frame;
2581 unsigned char aug_z_format;
2582 unsigned char state_sp;
2583 struct frame_state state;
2584 struct frame_state state_stack[MAX_SAVED_STATES];
2587 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2589 ULONG_PTR base;
2591 if (encoding == DW_EH_PE_omit) return 0;
2593 switch (encoding & 0xf0)
2595 case DW_EH_PE_abs:
2596 base = 0;
2597 break;
2598 case DW_EH_PE_pcrel:
2599 base = (ULONG_PTR)ctx->data;
2600 break;
2601 default:
2602 FIXME("unsupported encoding %02x\n", encoding);
2603 return 0;
2606 switch (encoding & 0x0f)
2608 case DW_EH_PE_native:
2609 return base + dwarf2_parse_addr(ctx);
2610 case DW_EH_PE_leb128:
2611 return base + dwarf2_leb128_as_unsigned(ctx);
2612 case DW_EH_PE_data2:
2613 return base + dwarf2_parse_u2(ctx);
2614 case DW_EH_PE_data4:
2615 return base + dwarf2_parse_u4(ctx);
2616 case DW_EH_PE_data8:
2617 return base + dwarf2_parse_u8(ctx);
2618 case DW_EH_PE_signed|DW_EH_PE_leb128:
2619 return base + dwarf2_leb128_as_signed(ctx);
2620 case DW_EH_PE_signed|DW_EH_PE_data2:
2621 return base + (signed short)dwarf2_parse_u2(ctx);
2622 case DW_EH_PE_signed|DW_EH_PE_data4:
2623 return base + (signed int)dwarf2_parse_u4(ctx);
2624 case DW_EH_PE_signed|DW_EH_PE_data8:
2625 return base + (LONG64)dwarf2_parse_u8(ctx);
2626 default:
2627 FIXME("unsupported encoding %02x\n", encoding);
2628 return 0;
2632 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2634 unsigned char version;
2635 const char* augmentation;
2636 const unsigned char* end;
2637 ULONG_PTR len;
2639 memset(info, 0, sizeof(*info));
2640 info->lsda_encoding = DW_EH_PE_omit;
2641 info->aug_z_format = 0;
2643 /* parse the CIE first */
2644 version = dwarf2_parse_byte(ctx);
2645 if (version != 1 && version != 3 && version != 4)
2647 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2648 return FALSE;
2650 augmentation = (const char*)ctx->data;
2651 ctx->data += strlen(augmentation) + 1;
2653 switch (version)
2655 case 4:
2656 /* skip 'address_size' and 'segment_size' */
2657 ctx->data += 2;
2658 /* fallthrough */
2659 case 1:
2660 case 3:
2661 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2662 info->data_align = dwarf2_leb128_as_signed(ctx);
2663 info->retaddr_reg = version == 1 ? dwarf2_parse_byte(ctx) :dwarf2_leb128_as_unsigned(ctx);
2664 break;
2665 default:
2668 info->state.cfa_rule = RULE_CFA_OFFSET;
2670 end = NULL;
2671 TRACE("\tparsing augmentation %s\n", debugstr_a(augmentation));
2672 if (*augmentation) do
2674 switch (*augmentation)
2676 case 'z':
2677 len = dwarf2_leb128_as_unsigned(ctx);
2678 end = ctx->data + len;
2679 info->aug_z_format = 1;
2680 continue;
2681 case 'L':
2682 info->lsda_encoding = dwarf2_parse_byte(ctx);
2683 continue;
2684 case 'P':
2686 unsigned char encoding = dwarf2_parse_byte(ctx);
2687 /* throw away the indirect bit, as we don't care for the result */
2688 encoding &= ~DW_EH_PE_indirect;
2689 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2690 continue;
2692 case 'R':
2693 info->fde_encoding = dwarf2_parse_byte(ctx);
2694 continue;
2695 case 'S':
2696 info->signal_frame = 1;
2697 continue;
2699 FIXME("unknown augmentation '%c'\n", *augmentation);
2700 if (!end) return FALSE;
2701 break;
2702 } while (*++augmentation);
2703 if (end) ctx->data = end;
2704 return TRUE;
2707 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2708 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2709 struct frame_info* info, BOOL in_eh_frame)
2711 const unsigned char* ptr_blk;
2712 const unsigned char* cie_ptr;
2713 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2714 unsigned len, id;
2715 unsigned long start, range;
2716 unsigned cie_id;
2717 const BYTE* start_data = fde_ctx->data;
2719 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2720 /* skip 0-padding at beginning of section (alignment) */
2721 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
2723 if (dwarf2_parse_u4(fde_ctx))
2725 fde_ctx->data -= 4;
2726 break;
2729 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2731 /* find the FDE for address addr (skip CIE) */
2732 len = dwarf2_parse_u4(fde_ctx);
2733 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2734 ptr_blk = fde_ctx->data + len;
2735 id = dwarf2_parse_u4(fde_ctx);
2736 if (id == cie_id)
2738 last_cie_ptr = fde_ctx->data - 8;
2739 /* we need some bits out of the CIE in order to parse all contents */
2740 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2741 cie_ctx->data = fde_ctx->data;
2742 cie_ctx->end_data = ptr_blk;
2743 cie_ctx->word_size = fde_ctx->word_size;
2744 continue;
2746 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2747 if (cie_ptr != last_cie_ptr)
2749 last_cie_ptr = cie_ptr;
2750 cie_ctx->data = cie_ptr;
2751 cie_ctx->word_size = fde_ctx->word_size;
2752 cie_ctx->end_data = cie_ptr + 4;
2753 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2754 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2756 FIXME("wrong CIE pointer at %x from FDE %x\n",
2757 (unsigned)(cie_ptr - start_data),
2758 (unsigned)(fde_ctx->data - start_data));
2759 return FALSE;
2761 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2763 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2764 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2766 if (addr >= start && addr < start + range)
2768 /* reset the FDE context */
2769 fde_ctx->end_data = ptr_blk;
2771 info->ip = start;
2772 return TRUE;
2775 return FALSE;
2778 static int valid_reg(ULONG_PTR reg)
2780 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2781 return (reg < NB_FRAME_REGS);
2784 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2785 ULONG_PTR last_ip, struct frame_info *info)
2787 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2789 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2791 if (op & 0xc0)
2793 switch (op & 0xc0)
2795 case DW_CFA_advance_loc:
2797 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2798 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2799 info->ip += offset;
2800 break;
2802 case DW_CFA_offset:
2804 ULONG_PTR reg = op & 0x3f;
2805 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2806 if (!valid_reg(reg)) break;
2807 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2808 info->ip,
2809 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2810 offset);
2811 info->state.regs[reg] = offset;
2812 info->state.rules[reg] = RULE_CFA_OFFSET;
2813 break;
2815 case DW_CFA_restore:
2817 ULONG_PTR reg = op & 0x3f;
2818 if (!valid_reg(reg)) break;
2819 TRACE("%lx: DW_CFA_restore %s\n",
2820 info->ip,
2821 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2822 info->state.rules[reg] = RULE_UNSET;
2823 break;
2827 else switch (op)
2829 case DW_CFA_nop:
2830 break;
2831 case DW_CFA_set_loc:
2833 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2834 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2835 info->ip = loc;
2836 break;
2838 case DW_CFA_advance_loc1:
2840 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2841 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2842 info->ip += offset;
2843 break;
2845 case DW_CFA_advance_loc2:
2847 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2848 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2849 info->ip += offset;
2850 break;
2852 case DW_CFA_advance_loc4:
2854 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2855 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2856 info->ip += offset;
2857 break;
2859 case DW_CFA_offset_extended:
2860 case DW_CFA_offset_extended_sf:
2862 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2863 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2864 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2865 if (!valid_reg(reg)) break;
2866 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2867 info->ip,
2868 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2869 offset);
2870 info->state.regs[reg] = offset;
2871 info->state.rules[reg] = RULE_CFA_OFFSET;
2872 break;
2874 case DW_CFA_restore_extended:
2876 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2877 if (!valid_reg(reg)) break;
2878 TRACE("%lx: DW_CFA_restore_extended %s\n",
2879 info->ip,
2880 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2881 info->state.rules[reg] = RULE_UNSET;
2882 break;
2884 case DW_CFA_undefined:
2886 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2887 if (!valid_reg(reg)) break;
2888 TRACE("%lx: DW_CFA_undefined %s\n",
2889 info->ip,
2890 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2891 info->state.rules[reg] = RULE_UNDEFINED;
2892 break;
2894 case DW_CFA_same_value:
2896 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2897 if (!valid_reg(reg)) break;
2898 TRACE("%lx: DW_CFA_same_value %s\n",
2899 info->ip,
2900 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2901 info->state.regs[reg] = reg;
2902 info->state.rules[reg] = RULE_SAME;
2903 break;
2905 case DW_CFA_register:
2907 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2908 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2909 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2910 TRACE("%lx: DW_CFA_register %s == %s\n",
2911 info->ip,
2912 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2913 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2, TRUE)));
2914 info->state.regs[reg] = reg2;
2915 info->state.rules[reg] = RULE_OTHER_REG;
2916 break;
2918 case DW_CFA_remember_state:
2919 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2920 if (info->state_sp >= MAX_SAVED_STATES)
2921 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2922 else
2923 info->state_stack[info->state_sp++] = info->state;
2924 break;
2925 case DW_CFA_restore_state:
2926 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2927 if (!info->state_sp)
2928 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2929 else
2930 info->state = info->state_stack[--info->state_sp];
2931 break;
2932 case DW_CFA_def_cfa:
2933 case DW_CFA_def_cfa_sf:
2935 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2936 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2937 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2938 if (!valid_reg(reg)) break;
2939 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2940 info->ip,
2941 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2942 offset);
2943 info->state.cfa_reg = reg;
2944 info->state.cfa_offset = offset;
2945 info->state.cfa_rule = RULE_CFA_OFFSET;
2946 break;
2948 case DW_CFA_def_cfa_register:
2950 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2951 if (!valid_reg(reg)) break;
2952 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2953 info->ip,
2954 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2955 info->state.cfa_reg = reg;
2956 info->state.cfa_rule = RULE_CFA_OFFSET;
2957 break;
2959 case DW_CFA_def_cfa_offset:
2960 case DW_CFA_def_cfa_offset_sf:
2962 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2963 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2964 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
2965 info->state.cfa_offset = offset;
2966 info->state.cfa_rule = RULE_CFA_OFFSET;
2967 break;
2969 case DW_CFA_def_cfa_expression:
2971 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2972 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2973 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2974 info->state.cfa_offset = expr;
2975 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2976 ctx->data += len;
2977 break;
2979 case DW_CFA_expression:
2980 case DW_CFA_val_expression:
2982 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2983 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2984 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2985 if (!valid_reg(reg)) break;
2986 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2987 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2988 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2989 expr, expr + len);
2990 info->state.regs[reg] = expr;
2991 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2992 ctx->data += len;
2993 break;
2995 case DW_CFA_GNU_args_size:
2996 /* FIXME: should check that GCC is the compiler for this CU */
2998 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2999 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
3000 /* ignored */
3001 break;
3003 default:
3004 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
3005 break;
3010 /* retrieve a context register from its dwarf number */
3011 static DWORD64 get_context_reg(struct cpu_stack_walk *csw, union ctx *context,
3012 ULONG_PTR dw_reg)
3014 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, TRUE), sz;
3015 void* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3017 if (sz == 8)
3018 return *(DWORD64 *)ptr;
3019 else if (sz == 4)
3020 return *(DWORD *)ptr;
3022 FIXME("unhandled size %d\n", sz);
3023 return 0;
3026 /* set a context register from its dwarf number */
3027 static void set_context_reg(struct cpu_stack_walk* csw, union ctx *context,
3028 ULONG_PTR dw_reg, ULONG_PTR val, BOOL isdebuggee)
3030 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, TRUE), sz;
3031 ULONG_PTR* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3033 if (isdebuggee)
3035 char tmp[16];
3037 if (sz > sizeof(tmp))
3039 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
3040 return;
3042 if (!sw_read_mem(csw, val, tmp, sz))
3044 WARN("Couldn't read memory at %p\n", (void*)val);
3045 return;
3047 memcpy(ptr, tmp, sz);
3049 else
3051 if (sz != sizeof(ULONG_PTR))
3053 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
3054 return;
3056 *ptr = val;
3060 /* copy a register from one context to another using dwarf number */
3061 static void copy_context_reg(struct cpu_stack_walk *csw,
3062 union ctx *dstcontext, ULONG_PTR dwregdst,
3063 union ctx *srccontext, ULONG_PTR dwregsrc)
3065 unsigned regdstno = csw->cpu->map_dwarf_register(dwregdst, TRUE), szdst;
3066 unsigned regsrcno = csw->cpu->map_dwarf_register(dwregsrc, TRUE), szsrc;
3067 ULONG_PTR* ptrdst = csw->cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3068 ULONG_PTR* ptrsrc = csw->cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3070 if (szdst != szsrc)
3072 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3073 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3074 return;
3076 memcpy(ptrdst, ptrsrc, szdst);
3079 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3080 const unsigned char* zp, union ctx *context)
3082 dwarf2_traverse_context_t ctx;
3083 ULONG_PTR reg, sz, tmp;
3084 DWORD64 stack[64];
3085 int sp = -1;
3086 ULONG_PTR len;
3088 ctx.data = zp;
3089 ctx.end_data = zp + 4;
3090 len = dwarf2_leb128_as_unsigned(&ctx);
3091 ctx.end_data = ctx.data + len;
3092 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3094 while (ctx.data < ctx.end_data)
3096 unsigned char opcode = dwarf2_parse_byte(&ctx);
3098 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3099 stack[++sp] = opcode - DW_OP_lit0;
3100 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3101 stack[++sp] = get_context_reg(csw, context, opcode - DW_OP_reg0);
3102 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3103 stack[++sp] = get_context_reg(csw, context, opcode - DW_OP_breg0)
3104 + dwarf2_leb128_as_signed(&ctx);
3105 else switch (opcode)
3107 case DW_OP_nop: break;
3108 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
3109 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3110 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3111 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3112 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3113 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3114 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3115 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3116 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3117 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3118 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3119 case DW_OP_deref:
3120 tmp = 0;
3121 if (!sw_read_mem(csw, stack[sp], &tmp, ctx.word_size))
3123 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3124 tmp = 0;
3126 stack[sp] = tmp;
3127 break;
3128 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3129 case DW_OP_drop: sp--; break;
3130 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3131 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3132 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3133 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3134 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3135 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3136 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3137 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3138 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3139 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3140 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3141 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3142 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3143 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3144 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3145 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3146 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3147 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3148 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3149 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3150 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3151 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3152 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3153 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3154 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3155 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3156 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3157 case DW_OP_GNU_encoded_addr:
3158 tmp = dwarf2_parse_byte(&ctx);
3159 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3160 break;
3161 case DW_OP_regx:
3162 stack[++sp] = get_context_reg(csw, context, dwarf2_leb128_as_unsigned(&ctx));
3163 break;
3164 case DW_OP_bregx:
3165 reg = dwarf2_leb128_as_unsigned(&ctx);
3166 tmp = dwarf2_leb128_as_signed(&ctx);
3167 stack[++sp] = get_context_reg(csw, context, reg) + tmp;
3168 break;
3169 case DW_OP_deref_size:
3170 sz = dwarf2_parse_byte(&ctx);
3171 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3173 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3174 tmp = 0;
3176 /* do integral promotion */
3177 switch (sz)
3179 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3180 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3181 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3182 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3183 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3185 break;
3186 default:
3187 FIXME("unhandled opcode %02x\n", opcode);
3190 return stack[sp];
3193 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3194 union ctx *context, struct frame_state *state, DWORD64 *cfa)
3196 unsigned int i;
3197 ULONG_PTR value;
3198 union ctx new_context = *context;
3200 switch (state->cfa_rule)
3202 case RULE_EXPRESSION:
3203 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3204 if (!sw_read_mem(csw, *cfa, cfa, csw->cpu->word_size))
3206 WARN("Couldn't read memory at %s\n", wine_dbgstr_longlong(*cfa));
3207 return;
3209 break;
3210 case RULE_VAL_EXPRESSION:
3211 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3212 break;
3213 default:
3214 *cfa = get_context_reg(csw, context, state->cfa_reg) + state->cfa_offset;
3215 break;
3217 if (!*cfa) return;
3219 for (i = 0; i < NB_FRAME_REGS; i++)
3221 switch (state->rules[i])
3223 case RULE_UNSET:
3224 case RULE_UNDEFINED:
3225 case RULE_SAME:
3226 break;
3227 case RULE_CFA_OFFSET:
3228 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3229 break;
3230 case RULE_OTHER_REG:
3231 copy_context_reg(csw, &new_context, i, context, state->regs[i]);
3232 break;
3233 case RULE_EXPRESSION:
3234 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3235 set_context_reg(csw, &new_context, i, value, TRUE);
3236 break;
3237 case RULE_VAL_EXPRESSION:
3238 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3239 set_context_reg(csw, &new_context, i, value, FALSE);
3240 break;
3243 *context = new_context;
3246 /***********************************************************************
3247 * dwarf2_virtual_unwind
3250 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk *csw, ULONG_PTR ip,
3251 union ctx *context, DWORD64 *cfa)
3253 struct module_pair pair;
3254 struct frame_info info;
3255 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3256 struct module_format* modfmt;
3257 const unsigned char* end;
3258 DWORD_PTR delta;
3260 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3261 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3262 !module_get_debug(&pair))
3263 return FALSE;
3264 modfmt = pair.effective->format_info[DFI_DWARF];
3265 if (!modfmt) return FALSE;
3266 memset(&info, 0, sizeof(info));
3267 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3268 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3269 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3270 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3271 * in this process the IMAGE section at a different address as the one expected by
3272 * the image
3274 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3275 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3276 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3278 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3279 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3280 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3281 delta = pair.effective->reloc_delta;
3282 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3284 TRACE("Couldn't find information for %lx\n", ip);
3285 return FALSE;
3289 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3290 ip, info.ip, info.code_align, info.data_align,
3291 csw->cpu->fetch_regname(csw->cpu->map_dwarf_register(info.retaddr_reg, TRUE)));
3293 /* if at very beginning of function, return and use default unwinder */
3294 if (ip == info.ip) return FALSE;
3295 execute_cfa_instructions(&cie_ctx, ip, &info);
3297 if (info.aug_z_format) /* get length of augmentation data */
3299 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3300 end = fde_ctx.data + len;
3302 else end = NULL;
3303 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3304 if (end) fde_ctx.data = end;
3306 execute_cfa_instructions(&fde_ctx, ip, &info);
3308 /* if there is no information about retaddr, use default unwinder */
3309 if (info.state.rules[info.retaddr_reg] == RULE_UNSET) return FALSE;
3311 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3313 return TRUE;
3316 static void dwarf2_location_compute(struct process* pcs,
3317 const struct module_format* modfmt,
3318 const struct symt_function* func,
3319 struct location* loc)
3321 struct location frame;
3322 DWORD_PTR ip;
3323 int err;
3324 dwarf2_traverse_context_t lctx;
3326 if (!func->container || func->container->tag != SymTagCompiland)
3328 WARN("We'd expect function %s's container to exist and be a compiland\n", debugstr_a(func->hash_elt.name));
3329 err = loc_err_internal;
3331 else
3333 /* instruction pointer relative to compiland's start */
3334 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3336 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3338 switch (loc->kind)
3340 case loc_dwarf2_location_list:
3341 /* Then, if the variable has a location list, find it !! */
3342 if (dwarf2_lookup_loclist(modfmt,
3343 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3344 ip, &lctx))
3345 goto do_compute;
3346 err = loc_err_out_of_scope;
3347 break;
3348 case loc_dwarf2_block:
3349 /* or if we have a copy of an existing block, get ready for it */
3351 unsigned* ptr = (unsigned*)loc->offset;
3353 lctx.data = (const BYTE*)(ptr + 1);
3354 lctx.end_data = lctx.data + *ptr;
3355 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3357 do_compute:
3358 /* now get the variable */
3359 err = compute_location(&lctx, loc, pcs->handle, &frame);
3360 break;
3361 case loc_register:
3362 case loc_regrel:
3363 /* nothing to do */
3364 break;
3365 default:
3366 WARN("Unsupported local kind %d\n", loc->kind);
3367 err = loc_err_internal;
3371 if (err < 0)
3373 loc->kind = loc_register;
3374 loc->reg = err;
3378 #ifdef HAVE_ZLIB
3379 static void *zalloc(void *priv, uInt items, uInt sz)
3381 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3384 static void zfree(void *priv, void *addr)
3386 HeapFree(GetProcessHeap(), 0, addr);
3389 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3390 const char* zsectname,
3391 struct image_section_map* ism)
3393 z_stream z;
3394 LARGE_INTEGER li;
3395 int res;
3396 BOOL ret = FALSE;
3398 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3399 size_t sz = image_get_map_size(ism);
3401 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
3403 ERR("invalid compressed section %s\n", debugstr_a(zsectname));
3404 goto out;
3407 #ifdef WORDS_BIGENDIAN
3408 li.u.HighPart = *(DWORD*)&sect[4];
3409 li.u.LowPart = *(DWORD*)&sect[8];
3410 #else
3411 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
3412 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
3413 #endif
3415 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
3416 if (!addr)
3417 goto out;
3419 z.next_in = &sect[12];
3420 z.avail_in = sz - 12;
3421 z.opaque = NULL;
3422 z.zalloc = zalloc;
3423 z.zfree = zfree;
3425 res = inflateInit(&z);
3426 if (res != Z_OK)
3428 FIXME("inflateInit failed with %i / %s\n", res, debugstr_a(z.msg));
3429 goto out_free;
3432 do {
3433 z.next_out = addr + z.total_out;
3434 z.avail_out = li.QuadPart - z.total_out;
3435 res = inflate(&z, Z_FINISH);
3436 } while (z.avail_in && res == Z_STREAM_END);
3438 if (res != Z_STREAM_END)
3440 FIXME("Decompression failed with %i / %s\n", res, debugstr_a(z.msg));
3441 goto out_end;
3444 ret = TRUE;
3445 section->compressed = TRUE;
3446 section->address = addr;
3447 section->rva = image_get_map_rva(ism);
3448 section->size = z.total_out;
3450 out_end:
3451 inflateEnd(&z);
3452 out_free:
3453 if (!ret)
3454 HeapFree(GetProcessHeap(), 0, addr);
3455 out:
3456 image_unmap_section(ism);
3457 return ret;
3460 #endif
3462 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3463 const char* sectname, const char* zsectname,
3464 struct image_section_map* ism)
3466 struct image_section_map local_ism;
3468 if (!ism) ism = &local_ism;
3470 section->compressed = FALSE;
3471 if (image_find_section(fmap, sectname, ism))
3473 section->address = (const BYTE*)image_map_section(ism);
3474 section->size = image_get_map_size(ism);
3475 section->rva = image_get_map_rva(ism);
3476 return TRUE;
3479 section->address = NULL;
3480 section->size = 0;
3481 section->rva = 0;
3483 if (zsectname && image_find_section(fmap, zsectname, ism))
3485 #ifdef HAVE_ZLIB
3486 return dwarf2_init_zsection(section, zsectname, ism);
3487 #else
3488 FIXME("dbghelp not built with zlib, but compressed section found\n" );
3489 #endif
3492 return FALSE;
3495 static inline void dwarf2_fini_section(dwarf2_section_t* section)
3497 if (section->compressed)
3498 HeapFree(GetProcessHeap(), 0, (void*)section->address);
3501 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3503 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
3504 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
3505 HeapFree(GetProcessHeap(), 0, modfmt);
3508 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3509 const struct elf_thunk_area* thunks,
3510 struct image_file_map* fmap)
3512 dwarf2_section_t eh_frame, section[section_max];
3513 dwarf2_traverse_context_t mod_ctx;
3514 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3515 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3516 BOOL ret = TRUE;
3517 struct module_format* dwarf2_modfmt;
3519 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect);
3520 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
3521 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
3522 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
3523 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
3524 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
3526 /* to do anything useful we need either .eh_frame or .debug_info */
3527 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3528 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3530 ret = FALSE;
3531 goto leave;
3534 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3536 /* debug info might have a different base address than .so file
3537 * when elf file is prelinked after splitting off debug info
3538 * adjust symbol base addresses accordingly
3540 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3543 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3545 mod_ctx.data = section[section_debug].address;
3546 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3547 mod_ctx.word_size = 0; /* will be correctly set later on */
3549 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3550 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3551 if (!dwarf2_modfmt)
3553 ret = FALSE;
3554 goto leave;
3556 dwarf2_modfmt->module = module;
3557 dwarf2_modfmt->remove = dwarf2_module_remove;
3558 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3559 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3560 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3561 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3563 /* As we'll need later some sections' content, we won't unmap these
3564 * sections upon existing this function
3566 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
3567 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
3568 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3570 while (mod_ctx.data < mod_ctx.end_data)
3572 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3574 dwarf2_modfmt->module->module.SymType = SymDia;
3575 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3576 /* FIXME: we could have a finer grain here */
3577 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3578 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3579 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3580 dwarf2_modfmt->module->module.Publics = TRUE;
3582 /* set the word_size for eh_frame parsing */
3583 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8;
3585 leave:
3586 dwarf2_fini_section(&section[section_debug]);
3587 dwarf2_fini_section(&section[section_abbrev]);
3588 dwarf2_fini_section(&section[section_string]);
3589 dwarf2_fini_section(&section[section_line]);
3590 dwarf2_fini_section(&section[section_ranges]);
3592 image_unmap_section(&debug_sect);
3593 image_unmap_section(&debug_abbrev_sect);
3594 image_unmap_section(&debug_str_sect);
3595 image_unmap_section(&debug_line_sect);
3596 image_unmap_section(&debug_ranges_sect);
3597 if (!ret) image_unmap_section(&eh_frame_sect);
3599 return ret;