Add AppDefaults app selection to control panel
[wine/gsoc-2012-control.git] / dlls / dbghelp / dwarf.c
blob2ce26fab983d7b25f9db9e7d07cd89faab1cf2b0
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 #include "windef.h"
46 #include "winbase.h"
47 #include "winuser.h"
48 #include "ole2.h"
49 #include "oleauto.h"
51 #include "dbghelp_private.h"
52 #include "image_private.h"
54 #include "wine/debug.h"
56 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
58 /* FIXME:
59 * - Functions:
60 * o unspecified parameters
61 * o inlined functions
62 * o Debug{Start|End}Point
63 * o CFA
64 * - Udt
65 * o proper types loading (nesting)
68 #if 0
69 static void dump(const void* ptr, unsigned len)
71 int i, j;
72 BYTE msg[128];
73 static const char hexof[] = "0123456789abcdef";
74 const BYTE* x = ptr;
76 for (i = 0; i < len; i += 16)
78 sprintf(msg, "%08x: ", i);
79 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
80 for (j = 0; j < min(16, len - i); j++)
82 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
83 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
84 msg[10 + 3 * j + 2] = ' ';
85 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
86 x[i + j] : '.';
88 msg[10 + 3 * 16] = ' ';
89 msg[10 + 3 * 16 + 1 + 16] = '\0';
90 TRACE("%s\n", msg);
93 #endif
95 /**
97 * Main Specs:
98 * http://www.eagercon.com/dwarf/dwarf3std.htm
99 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
101 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
103 * example of projects who do dwarf2 parsing:
104 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
105 * http://elis.ugent.be/diota/log/ltrace_elf.c
107 #include "dwarf.h"
110 * Parsers
113 typedef struct dwarf2_abbrev_entry_attr_s
115 unsigned long attribute;
116 unsigned long form;
117 struct dwarf2_abbrev_entry_attr_s* next;
118 } dwarf2_abbrev_entry_attr_t;
120 typedef struct dwarf2_abbrev_entry_s
122 unsigned long entry_code;
123 unsigned long tag;
124 unsigned char have_child;
125 unsigned num_attr;
126 dwarf2_abbrev_entry_attr_t* attrs;
127 } dwarf2_abbrev_entry_t;
129 struct dwarf2_block
131 unsigned size;
132 const unsigned char* ptr;
135 struct attribute
137 unsigned long form;
138 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
139 union
141 unsigned long uvalue;
142 ULONGLONG lluvalue;
143 long svalue;
144 const char* string;
145 struct dwarf2_block block;
146 } u;
149 typedef struct dwarf2_debug_info_s
151 const dwarf2_abbrev_entry_t*abbrev;
152 struct symt* symt;
153 const unsigned char** data;
154 struct vector children;
155 struct dwarf2_debug_info_s* parent;
156 } dwarf2_debug_info_t;
158 typedef struct dwarf2_section_s
160 const unsigned char* address;
161 unsigned size;
162 DWORD_PTR rva;
163 } dwarf2_section_t;
165 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
167 typedef struct dwarf2_traverse_context_s
169 const unsigned char* data;
170 const unsigned char* end_data;
171 unsigned char word_size;
172 } dwarf2_traverse_context_t;
174 /* symt_cache indexes */
175 #define sc_void 0
176 #define sc_int1 1
177 #define sc_int2 2
178 #define sc_int4 3
179 #define sc_num 4
181 typedef struct dwarf2_parse_context_s
183 const dwarf2_section_t* sections;
184 unsigned section;
185 struct pool pool;
186 struct module* module;
187 struct symt_compiland* compiland;
188 const struct elf_thunk_area*thunks;
189 struct sparse_array abbrev_table;
190 struct sparse_array debug_info_table;
191 unsigned long load_offset;
192 unsigned long ref_offset;
193 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
194 char* cpp_name;
195 } dwarf2_parse_context_t;
197 /* stored in the dbghelp's module internal structure for later reuse */
198 struct dwarf2_module_info_s
200 dwarf2_section_t debug_loc;
201 dwarf2_section_t debug_frame;
202 dwarf2_section_t eh_frame;
203 unsigned char word_size;
206 #define loc_dwarf2_location_list (loc_user + 0)
207 #define loc_dwarf2_block (loc_user + 1)
209 /* forward declarations */
210 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
212 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
214 return *ptr;
217 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
219 unsigned char uvalue = dwarf2_get_byte(ctx->data);
220 ctx->data += 1;
221 return uvalue;
224 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
226 return *(const UINT16*)ptr;
229 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
231 unsigned short uvalue = dwarf2_get_u2(ctx->data);
232 ctx->data += 2;
233 return uvalue;
236 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
238 return *(const UINT32*)ptr;
241 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
243 unsigned long uvalue = dwarf2_get_u4(ctx->data);
244 ctx->data += 4;
245 return uvalue;
248 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
250 return *(const UINT64*)ptr;
253 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
255 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
256 ctx->data += 8;
257 return uvalue;
260 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
262 unsigned long ret = 0;
263 unsigned char byte;
264 unsigned shift = 0;
268 byte = dwarf2_get_byte(ptr++);
269 ret |= (byte & 0x7f) << shift;
270 shift += 7;
271 } while (byte & 0x80);
273 if (end) *end = ptr;
274 return ret;
277 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
279 unsigned long ret;
281 assert(ctx);
283 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
285 return ret;
288 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
290 long ret = 0;
291 unsigned char byte;
292 unsigned shift = 0;
293 const unsigned size = sizeof(int) * 8;
297 byte = dwarf2_get_byte(ptr++);
298 ret |= (byte & 0x7f) << shift;
299 shift += 7;
300 } while (byte & 0x80);
301 if (end) *end = ptr;
303 /* as spec: sign bit of byte is 2nd high order bit (80x40)
304 * -> 0x80 is used as flag.
306 if ((shift < size) && (byte & 0x40))
308 ret |= - (1 << shift);
310 return ret;
313 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
315 long ret = 0;
317 assert(ctx);
319 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
320 return ret;
323 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
325 unsigned ret;
326 for (ret = 0; ctx->data[ret] & 0x80; ret++);
327 return ret + 1;
330 /******************************************************************
331 * dwarf2_get_addr
333 * Returns an address.
334 * We assume that in all cases word size from Dwarf matches the size of
335 * addresses in platform where the exec is compiled.
337 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
339 unsigned long ret;
341 switch (word_size)
343 case 4:
344 ret = dwarf2_get_u4(ptr);
345 break;
346 case 8:
347 ret = dwarf2_get_u8(ptr);
348 break;
349 default:
350 FIXME("Unsupported Word Size %u\n", word_size);
351 ret = 0;
353 return ret;
356 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
358 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
359 ctx->data += ctx->word_size;
360 return ret;
363 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
365 return wine_dbg_sprintf("ctx(%p)", ctx->data);
368 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
370 return wine_dbg_sprintf("ctx(%p,%s)",
371 ctx, debugstr_w(ctx->module->module.ModuleName));
374 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
376 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
377 di->abbrev, di->symt);
380 static dwarf2_abbrev_entry_t*
381 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
382 unsigned long entry_code)
384 assert( NULL != abbrev_table );
385 return sparse_array_find(abbrev_table, entry_code);
388 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
389 struct sparse_array* abbrev_table,
390 struct pool* pool)
392 unsigned long entry_code;
393 dwarf2_abbrev_entry_t* abbrev_entry;
394 dwarf2_abbrev_entry_attr_t* new = NULL;
395 dwarf2_abbrev_entry_attr_t* last = NULL;
396 unsigned long attribute;
397 unsigned long form;
399 assert( NULL != abbrev_ctx );
401 TRACE("%s, end at %p\n",
402 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
404 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
405 while (abbrev_ctx->data < abbrev_ctx->end_data)
407 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
408 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
409 TRACE("found entry_code %lu\n", entry_code);
410 if (!entry_code)
412 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
413 break;
415 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
416 assert( NULL != abbrev_entry );
418 abbrev_entry->entry_code = entry_code;
419 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
420 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
421 abbrev_entry->attrs = NULL;
422 abbrev_entry->num_attr = 0;
424 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
425 abbrev_table, sparse_array_length(abbrev_table),
426 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
428 last = NULL;
429 while (1)
431 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
432 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
433 if (!attribute) break;
435 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
436 assert(new);
438 new->attribute = attribute;
439 new->form = form;
440 new->next = NULL;
441 if (abbrev_entry->attrs) last->next = new;
442 else abbrev_entry->attrs = new;
443 last = new;
444 abbrev_entry->num_attr++;
447 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
450 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
451 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
453 unsigned step;
455 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
457 switch (abbrev_attr->form)
459 case DW_FORM_ref_addr:
460 case DW_FORM_addr: step = ctx->word_size; break;
461 case DW_FORM_flag:
462 case DW_FORM_data1:
463 case DW_FORM_ref1: step = 1; break;
464 case DW_FORM_data2:
465 case DW_FORM_ref2: step = 2; break;
466 case DW_FORM_data4:
467 case DW_FORM_ref4:
468 case DW_FORM_strp: step = 4; break;
469 case DW_FORM_data8:
470 case DW_FORM_ref8: step = 8; break;
471 case DW_FORM_sdata:
472 case DW_FORM_ref_udata:
473 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
474 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
475 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
476 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
477 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
478 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
479 default:
480 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
481 return;
483 ctx->data += step;
486 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
487 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
488 const unsigned char* data,
489 struct attribute* attr)
491 attr->form = abbrev_attr->form;
492 switch (attr->form)
494 case DW_FORM_ref_addr:
495 case DW_FORM_addr:
496 attr->u.uvalue = dwarf2_get_addr(data,
497 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
498 TRACE("addr<0x%lx>\n", attr->u.uvalue);
499 break;
501 case DW_FORM_flag:
502 attr->u.uvalue = dwarf2_get_byte(data);
503 TRACE("flag<0x%lx>\n", attr->u.uvalue);
504 break;
506 case DW_FORM_data1:
507 attr->u.uvalue = dwarf2_get_byte(data);
508 TRACE("data1<%lu>\n", attr->u.uvalue);
509 break;
511 case DW_FORM_data2:
512 attr->u.uvalue = dwarf2_get_u2(data);
513 TRACE("data2<%lu>\n", attr->u.uvalue);
514 break;
516 case DW_FORM_data4:
517 attr->u.uvalue = dwarf2_get_u4(data);
518 TRACE("data4<%lu>\n", attr->u.uvalue);
519 break;
521 case DW_FORM_data8:
522 attr->u.lluvalue = dwarf2_get_u8(data);
523 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
524 break;
526 case DW_FORM_ref1:
527 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
528 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
529 break;
531 case DW_FORM_ref2:
532 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
533 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
534 break;
536 case DW_FORM_ref4:
537 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
538 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
539 break;
541 case DW_FORM_ref8:
542 FIXME("Unhandled 64 bit support\n");
543 break;
545 case DW_FORM_sdata:
546 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
547 break;
549 case DW_FORM_ref_udata:
550 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
551 break;
553 case DW_FORM_udata:
554 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
555 break;
557 case DW_FORM_string:
558 attr->u.string = (const char *)data;
559 TRACE("string<%s>\n", attr->u.string);
560 break;
562 case DW_FORM_strp:
564 unsigned long offset = dwarf2_get_u4(data);
565 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
567 TRACE("strp<%s>\n", attr->u.string);
568 break;
570 case DW_FORM_block:
571 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
572 break;
574 case DW_FORM_block1:
575 attr->u.block.size = dwarf2_get_byte(data);
576 attr->u.block.ptr = data + 1;
577 break;
579 case DW_FORM_block2:
580 attr->u.block.size = dwarf2_get_u2(data);
581 attr->u.block.ptr = data + 2;
582 break;
584 case DW_FORM_block4:
585 attr->u.block.size = dwarf2_get_u4(data);
586 attr->u.block.ptr = data + 4;
587 break;
589 default:
590 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
591 break;
595 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
596 const dwarf2_debug_info_t* di,
597 unsigned at, struct attribute* attr)
599 unsigned i, refidx = 0;
600 dwarf2_abbrev_entry_attr_t* abbrev_attr;
601 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
603 attr->gotten_from = attr_direct;
604 while (di)
606 ref_abbrev_attr = NULL;
607 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
609 if (abbrev_attr->attribute == at)
611 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
612 return TRUE;
614 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
615 abbrev_attr->attribute == DW_AT_specification) &&
616 at != DW_AT_sibling)
618 if (ref_abbrev_attr)
619 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
620 ref_abbrev_attr = abbrev_attr;
621 refidx = i;
622 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
623 attr_abstract_origin : attr_specification;
626 /* do we have either an abstract origin or a specification debug entry to look into ? */
627 if (!ref_abbrev_attr) break;
628 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
629 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
630 FIXME("Should have found the debug info entry\n");
632 return FALSE;
635 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
637 #define Wine_DW_no_register 0x7FFFFFFF
639 static unsigned dwarf2_map_register(int regno)
641 if (regno == Wine_DW_no_register)
643 FIXME("What the heck map reg 0x%x\n",regno);
644 return 0;
646 return dbghelp_current_cpu->map_dwarf_register(regno);
649 static enum location_error
650 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
651 HANDLE hproc, const struct location* frame)
653 DWORD_PTR tmp, stack[64];
654 unsigned stk;
655 unsigned char op;
656 BOOL piece_found = FALSE;
658 stack[stk = 0] = 0;
660 loc->kind = loc_absolute;
661 loc->reg = Wine_DW_no_register;
663 while (ctx->data < ctx->end_data)
665 op = dwarf2_parse_byte(ctx);
667 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
668 stack[++stk] = op - DW_OP_lit0;
669 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
671 /* dbghelp APIs don't know how to cope with this anyway
672 * (for example 'long long' stored in two registers)
673 * FIXME: We should tell winedbg how to deal with it (sigh)
675 if (!piece_found)
677 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0);
678 if (loc->reg != Wine_DW_no_register)
679 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
680 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
681 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
682 loc->reg = cvreg;
684 loc->kind = loc_register;
686 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
688 /* dbghelp APIs don't know how to cope with this anyway
689 * (for example 'long long' stored in two registers)
690 * FIXME: We should tell winedbg how to deal with it (sigh)
692 if (!piece_found)
694 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0);
695 if (loc->reg != Wine_DW_no_register)
696 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
697 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
698 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
699 loc->reg = cvreg;
701 stack[++stk] = dwarf2_leb128_as_signed(ctx);
702 loc->kind = loc_regrel;
704 else switch (op)
706 case DW_OP_nop: break;
707 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
708 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
709 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
710 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
711 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
712 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
713 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
714 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
715 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
716 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
717 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
718 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
719 case DW_OP_drop: stk--; break;
720 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
721 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
722 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
723 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
724 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
725 case DW_OP_neg: stack[stk] = -stack[stk]; break;
726 case DW_OP_not: stack[stk] = ~stack[stk]; break;
727 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
728 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
729 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
730 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
731 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
732 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
733 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
734 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
735 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
736 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
737 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
738 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
739 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
740 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
741 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
742 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
743 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
744 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
745 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
746 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
747 case DW_OP_regx:
748 tmp = dwarf2_leb128_as_unsigned(ctx);
749 if (!piece_found)
751 if (loc->reg != Wine_DW_no_register)
752 FIXME("Only supporting one reg\n");
753 loc->reg = dwarf2_map_register(tmp);
755 loc->kind = loc_register;
756 break;
757 case DW_OP_bregx:
758 tmp = dwarf2_leb128_as_unsigned(ctx);
759 if (loc->reg != Wine_DW_no_register)
760 FIXME("Only supporting one regx\n");
761 loc->reg = dwarf2_map_register(tmp);
762 stack[++stk] = dwarf2_leb128_as_signed(ctx);
763 loc->kind = loc_regrel;
764 break;
765 case DW_OP_fbreg:
766 if (loc->reg != Wine_DW_no_register)
767 FIXME("Only supporting one reg (%s/%d -> -2)\n",
768 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
769 if (frame && frame->kind == loc_register)
771 loc->kind = loc_regrel;
772 loc->reg = frame->reg;
773 stack[++stk] = dwarf2_leb128_as_signed(ctx);
775 else if (frame && frame->kind == loc_regrel)
777 loc->kind = loc_regrel;
778 loc->reg = frame->reg;
779 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
781 else
783 /* FIXME: this could be later optimized by not recomputing
784 * this very location expression
786 loc->kind = loc_dwarf2_block;
787 stack[++stk] = dwarf2_leb128_as_signed(ctx);
789 break;
790 case DW_OP_piece:
792 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
793 WARN("Not handling OP_piece (size=%d)\n", sz);
794 piece_found = TRUE;
796 break;
797 case DW_OP_deref:
798 if (!stk)
800 FIXME("Unexpected empty stack\n");
801 return loc_err_internal;
803 if (loc->reg != Wine_DW_no_register)
805 WARN("Too complex expression for deref\n");
806 return loc_err_too_complex;
808 if (hproc)
810 DWORD_PTR addr = stack[stk--];
811 DWORD_PTR deref;
813 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
815 WARN("Couldn't read memory at %lx\n", addr);
816 return loc_err_cant_read;
818 stack[++stk] = deref;
820 else
822 loc->kind = loc_dwarf2_block;
824 break;
825 case DW_OP_deref_size:
826 if (!stk)
828 FIXME("Unexpected empty stack\n");
829 return loc_err_internal;
831 if (loc->reg != Wine_DW_no_register)
833 WARN("Too complex expression for deref\n");
834 return loc_err_too_complex;
836 if (hproc)
838 DWORD_PTR addr = stack[stk--];
839 BYTE derefsize = dwarf2_parse_byte(ctx);
840 DWORD64 deref;
842 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
844 WARN("Couldn't read memory at %lx\n", addr);
845 return loc_err_cant_read;
848 switch (derefsize)
850 case 1: stack[++stk] = *(unsigned char*)&deref; break;
851 case 2: stack[++stk] = *(unsigned short*)&deref; break;
852 case 4: stack[++stk] = *(DWORD*)&deref; break;
853 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
856 else
858 dwarf2_parse_byte(ctx);
859 loc->kind = loc_dwarf2_block;
861 break;
862 case DW_OP_stack_value:
863 /* Expected behaviour is that this is the last instruction of this
864 * expression and just the "top of stack" value should be put to loc->offset. */
865 break;
866 default:
867 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
868 FIXME("Unhandled attr op: %x\n", op);
869 /* FIXME else unhandled extension */
870 return loc_err_internal;
873 loc->offset = stack[stk];
874 return 0;
877 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
878 const dwarf2_debug_info_t* di,
879 unsigned long dw,
880 struct location* loc,
881 const struct location* frame)
883 struct attribute xloc;
885 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
887 switch (xloc.form)
889 case DW_FORM_data1: case DW_FORM_data2:
890 case DW_FORM_udata: case DW_FORM_sdata:
891 loc->kind = loc_absolute;
892 loc->reg = 0;
893 loc->offset = xloc.u.uvalue;
894 return TRUE;
895 case DW_FORM_data4: case DW_FORM_data8:
896 loc->kind = loc_dwarf2_location_list;
897 loc->reg = Wine_DW_no_register;
898 loc->offset = xloc.u.uvalue;
899 return TRUE;
900 case DW_FORM_block:
901 case DW_FORM_block1:
902 case DW_FORM_block2:
903 case DW_FORM_block4:
904 break;
905 default: FIXME("Unsupported yet form %lx\n", xloc.form);
906 return FALSE;
909 /* assume we have a block form */
911 if (xloc.u.block.size)
913 dwarf2_traverse_context_t lctx;
914 enum location_error err;
916 lctx.data = xloc.u.block.ptr;
917 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
918 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
920 err = compute_location(&lctx, loc, NULL, frame);
921 if (err < 0)
923 loc->kind = loc_error;
924 loc->reg = err;
926 else if (loc->kind == loc_dwarf2_block)
928 unsigned* ptr = pool_alloc(&ctx->module->pool,
929 sizeof(unsigned) + xloc.u.block.size);
930 *ptr = xloc.u.block.size;
931 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
932 loc->offset = (unsigned long)ptr;
935 return TRUE;
938 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
939 const dwarf2_debug_info_t* di)
941 struct attribute attr;
942 dwarf2_debug_info_t* type;
944 if (!dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
945 return NULL;
946 if (!(type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue)))
948 FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
949 return NULL;
951 if (!type->symt)
953 /* load the debug info entity */
954 dwarf2_load_one_entry(ctx, type);
955 if (!type->symt)
956 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
958 return type->symt;
961 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
963 char* last;
964 struct attribute diname;
965 struct attribute spec;
967 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
968 if (!ctx->cpp_name)
969 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
970 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
971 strcpy(last, name);
973 /* if the di is a definition, but has also a (previous) declaration, then scope must
974 * be gotten from declaration not definition
976 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
978 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
979 if (!di)
981 FIXME("Should have found the debug info entry\n");
982 return NULL;
986 for (di = di->parent; di; di = di->parent)
988 switch (di->abbrev->tag)
990 case DW_TAG_namespace:
991 case DW_TAG_structure_type:
992 case DW_TAG_class_type:
993 case DW_TAG_interface_type:
994 case DW_TAG_union_type:
995 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
997 size_t len = strlen(diname.u.string);
998 last -= 2 + len;
999 if (last < ctx->cpp_name) return NULL;
1000 memcpy(last, diname.u.string, len);
1001 last[len] = last[len + 1] = ':';
1003 break;
1004 default:
1005 break;
1008 return last;
1011 /******************************************************************
1012 * dwarf2_read_range
1014 * read a range for a given debug_info (either using AT_range attribute, in which
1015 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1016 * in all cases, range is relative to beginning of compilation unit
1018 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1019 unsigned long* plow, unsigned long* phigh)
1021 struct attribute range;
1023 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1025 dwarf2_traverse_context_t traverse;
1026 unsigned long low, high;
1028 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1029 traverse.end_data = ctx->sections[section_ranges].address +
1030 ctx->sections[section_ranges].size;
1031 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1033 *plow = ULONG_MAX;
1034 *phigh = 0;
1035 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1037 low = dwarf2_parse_addr(&traverse);
1038 high = dwarf2_parse_addr(&traverse);
1039 if (low == 0 && high == 0) break;
1040 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1041 if (low < *plow) *plow = low;
1042 if (high > *phigh) *phigh = high;
1044 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1045 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1047 return TRUE;
1049 else
1051 struct attribute low_pc;
1052 struct attribute high_pc;
1054 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1055 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1056 return FALSE;
1057 *plow = low_pc.u.uvalue;
1058 *phigh = high_pc.u.uvalue;
1059 return TRUE;
1063 /******************************************************************
1064 * dwarf2_read_one_debug_info
1066 * Loads into memory one debug info entry, and recursively its children (if any)
1068 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1069 dwarf2_traverse_context_t* traverse,
1070 dwarf2_debug_info_t* parent_di,
1071 dwarf2_debug_info_t** pdi)
1073 const dwarf2_abbrev_entry_t*abbrev;
1074 unsigned long entry_code;
1075 unsigned long offset;
1076 dwarf2_debug_info_t* di;
1077 dwarf2_debug_info_t* child;
1078 dwarf2_debug_info_t** where;
1079 dwarf2_abbrev_entry_attr_t* attr;
1080 unsigned i;
1081 struct attribute sibling;
1083 offset = traverse->data - ctx->sections[ctx->section].address;
1084 entry_code = dwarf2_leb128_as_unsigned(traverse);
1085 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1086 if (!entry_code)
1088 *pdi = NULL;
1089 return TRUE;
1091 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1092 if (!abbrev)
1094 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1095 return FALSE;
1097 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1098 if (!di) return FALSE;
1099 di->abbrev = abbrev;
1100 di->symt = NULL;
1101 di->parent = parent_di;
1103 if (abbrev->num_attr)
1105 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1106 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1108 di->data[i] = traverse->data;
1109 dwarf2_swallow_attribute(traverse, attr);
1112 else di->data = NULL;
1113 if (abbrev->have_child)
1115 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1116 while (traverse->data < traverse->end_data)
1118 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1119 if (!child) break;
1120 where = vector_add(&di->children, &ctx->pool);
1121 if (!where) return FALSE;
1122 *where = child;
1125 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1126 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1128 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1129 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1130 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1132 *pdi = di;
1133 return TRUE;
1136 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1137 dwarf2_debug_info_t* di)
1139 struct attribute spec;
1141 while (di)
1143 if (di->abbrev->have_child)
1144 return &di->children;
1145 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1146 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1147 FIXME("Should have found the debug info entry\n");
1149 return NULL;
1152 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1153 dwarf2_debug_info_t* di)
1155 struct attribute name;
1156 struct attribute size;
1157 struct attribute encoding;
1158 enum BasicType bt;
1159 int cache_idx = -1;
1160 if (di->symt) return di->symt;
1162 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1164 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1165 name.u.string = NULL;
1166 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1167 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1169 switch (encoding.u.uvalue)
1171 case DW_ATE_void: bt = btVoid; break;
1172 case DW_ATE_address: bt = btULong; break;
1173 case DW_ATE_boolean: bt = btBool; break;
1174 case DW_ATE_complex_float: bt = btComplex; break;
1175 case DW_ATE_float: bt = btFloat; break;
1176 case DW_ATE_signed: bt = btInt; break;
1177 case DW_ATE_unsigned: bt = btUInt; break;
1178 case DW_ATE_signed_char: bt = btChar; break;
1179 case DW_ATE_unsigned_char: bt = btChar; break;
1180 default: bt = btNoType; break;
1182 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1183 switch (bt)
1185 case btVoid:
1186 assert(size.u.uvalue == 0);
1187 cache_idx = sc_void;
1188 break;
1189 case btInt:
1190 switch (size.u.uvalue)
1192 case 1: cache_idx = sc_int1; break;
1193 case 2: cache_idx = sc_int2; break;
1194 case 4: cache_idx = sc_int4; break;
1196 break;
1197 default: break;
1199 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1200 ctx->symt_cache[cache_idx] = di->symt;
1202 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1203 return di->symt;
1206 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1207 dwarf2_debug_info_t* di)
1209 struct symt* ref_type;
1210 struct attribute name;
1212 if (di->symt) return di->symt;
1214 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1216 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1217 ref_type = dwarf2_lookup_type(ctx, di);
1219 if (name.u.string)
1220 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1221 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1222 return di->symt;
1225 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1226 dwarf2_debug_info_t* di)
1228 struct symt* ref_type;
1229 struct attribute size;
1231 if (di->symt) return di->symt;
1233 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1235 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1236 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1238 ref_type = ctx->symt_cache[sc_void];
1239 assert(ref_type);
1241 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1242 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1243 return di->symt;
1246 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1247 dwarf2_debug_info_t* di)
1249 struct symt* ref_type;
1250 struct symt* idx_type = NULL;
1251 struct attribute min, max, cnt;
1252 dwarf2_debug_info_t* child;
1253 unsigned int i;
1254 const struct vector* children;
1256 if (di->symt) return di->symt;
1258 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1260 ref_type = dwarf2_lookup_type(ctx, di);
1262 if (!(children = dwarf2_get_di_children(ctx, di)))
1264 /* fake an array with unknown size */
1265 /* FIXME: int4 even on 64bit machines??? */
1266 idx_type = ctx->symt_cache[sc_int4];
1267 min.u.uvalue = 0;
1268 max.u.uvalue = -1;
1270 else for (i = 0; i < vector_length(children); i++)
1272 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1273 switch (child->abbrev->tag)
1275 case DW_TAG_subrange_type:
1276 idx_type = dwarf2_lookup_type(ctx, child);
1277 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1278 min.u.uvalue = 0;
1279 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1280 max.u.uvalue = 0;
1281 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1282 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1283 break;
1284 default:
1285 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1286 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1287 break;
1290 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1291 return di->symt;
1294 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1295 dwarf2_debug_info_t* di)
1297 struct symt* ref_type;
1299 if (di->symt) return di->symt;
1301 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1303 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1305 ref_type = ctx->symt_cache[sc_void];
1306 assert(ref_type);
1308 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1309 di->symt = ref_type;
1311 return ref_type;
1314 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1315 dwarf2_debug_info_t* di)
1317 struct symt* ref_type;
1319 if (di->symt) return di->symt;
1321 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1323 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1325 ref_type = ctx->symt_cache[sc_void];
1326 assert(ref_type);
1328 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1329 di->symt = ref_type;
1331 return ref_type;
1334 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1335 dwarf2_debug_info_t* di)
1337 struct symt* ref_type = NULL;
1339 if (di->symt) return di->symt;
1341 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1343 ref_type = dwarf2_lookup_type(ctx, di);
1344 /* FIXME: for now, we hard-wire C++ references to pointers */
1345 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1347 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1349 return di->symt;
1352 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1353 dwarf2_debug_info_t* di,
1354 struct symt_udt* parent)
1356 struct symt* elt_type;
1357 struct attribute name;
1358 struct attribute bit_size;
1359 struct attribute bit_offset;
1360 struct location loc;
1362 assert(parent);
1364 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1366 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1367 elt_type = dwarf2_lookup_type(ctx, di);
1368 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1370 if (loc.kind != loc_absolute)
1372 FIXME("Found register, while not expecting it\n");
1373 loc.offset = 0;
1375 else
1376 TRACE("found member_location at %s -> %lu\n",
1377 dwarf2_debug_ctx(ctx), loc.offset);
1379 else
1380 loc.offset = 0;
1381 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1382 bit_size.u.uvalue = 0;
1383 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1385 /* FIXME: we should only do this when implementation is LSB (which is
1386 * the case on i386 processors)
1388 struct attribute nbytes;
1389 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1391 DWORD64 size;
1392 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1393 (unsigned long)size : 0;
1395 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1397 else bit_offset.u.uvalue = 0;
1398 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1399 (loc.offset << 3) + bit_offset.u.uvalue,
1400 bit_size.u.uvalue);
1402 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1405 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1406 dwarf2_debug_info_t* di);
1408 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1409 dwarf2_debug_info_t* di,
1410 enum UdtKind udt)
1412 struct attribute name;
1413 struct attribute size;
1414 struct vector* children;
1415 dwarf2_debug_info_t*child;
1416 unsigned int i;
1418 if (di->symt) return di->symt;
1420 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1422 /* quirk... FIXME provide real support for anonymous UDTs */
1423 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1424 name.u.string = "zz_anon_zz";
1425 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1427 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1428 size.u.uvalue, udt)->symt;
1430 children = dwarf2_get_di_children(ctx, di);
1431 if (children) for (i = 0; i < vector_length(children); i++)
1433 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1435 switch (child->abbrev->tag)
1437 case DW_TAG_member:
1438 /* FIXME: should I follow the sibling stuff ?? */
1439 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1440 break;
1441 case DW_TAG_enumeration_type:
1442 dwarf2_parse_enumeration_type(ctx, child);
1443 break;
1444 case DW_TAG_subprogram:
1445 dwarf2_parse_subprogram(ctx, child);
1446 break;
1447 case DW_TAG_structure_type:
1448 case DW_TAG_class_type:
1449 case DW_TAG_union_type:
1450 case DW_TAG_typedef:
1451 /* FIXME: we need to handle nested udt definitions */
1452 case DW_TAG_inheritance:
1453 case DW_TAG_template_type_param:
1454 case DW_TAG_template_value_param:
1455 case DW_TAG_variable:
1456 case DW_TAG_imported_declaration:
1457 case DW_TAG_ptr_to_member_type:
1458 /* FIXME: some C++ related stuff */
1459 break;
1460 default:
1461 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1462 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1463 break;
1467 return di->symt;
1470 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1471 dwarf2_debug_info_t* di,
1472 struct symt_enum* parent)
1474 struct attribute name;
1475 struct attribute value;
1477 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1479 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1480 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1481 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1483 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1486 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1487 dwarf2_debug_info_t* di)
1489 struct attribute name;
1490 struct attribute size;
1491 struct symt_basic* basetype;
1492 struct vector* children;
1493 dwarf2_debug_info_t*child;
1494 unsigned int i;
1496 if (di->symt) return di->symt;
1498 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1500 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1501 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1503 switch (size.u.uvalue) /* FIXME: that's wrong */
1505 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1506 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1507 default:
1508 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1511 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1513 children = dwarf2_get_di_children(ctx, di);
1514 /* FIXME: should we use the sibling stuff ?? */
1515 if (children) for (i = 0; i < vector_length(children); i++)
1517 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1519 switch (child->abbrev->tag)
1521 case DW_TAG_enumerator:
1522 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1523 break;
1524 default:
1525 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1526 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1529 return di->symt;
1532 /* structure used to pass information around when parsing a subprogram */
1533 typedef struct dwarf2_subprogram_s
1535 dwarf2_parse_context_t* ctx;
1536 struct symt_function* func;
1537 BOOL non_computed_variable;
1538 struct location frame;
1539 } dwarf2_subprogram_t;
1541 /******************************************************************
1542 * dwarf2_parse_variable
1544 * Parses any variable (parameter, local/global variable)
1546 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1547 struct symt_block* block,
1548 dwarf2_debug_info_t* di)
1550 struct symt* param_type;
1551 struct attribute name, value;
1552 struct location loc;
1553 BOOL is_pmt;
1555 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1557 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1558 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1560 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1561 /* cannot do much without the name, the functions below won't like it. */
1562 return;
1564 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1565 &loc, &subpgm->frame))
1567 struct attribute ext;
1569 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1570 name.u.string, loc.kind, loc.offset, loc.reg,
1571 dwarf2_debug_ctx(subpgm->ctx));
1573 switch (loc.kind)
1575 case loc_error:
1576 break;
1577 case loc_absolute:
1578 /* it's a global variable */
1579 /* FIXME: we don't handle its scope yet */
1580 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1581 ext.u.uvalue = 0;
1582 loc.offset += subpgm->ctx->load_offset;
1583 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1584 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1585 loc, 0, param_type);
1586 break;
1587 default:
1588 subpgm->non_computed_variable = TRUE;
1589 /* fall through */
1590 case loc_register:
1591 case loc_regrel:
1592 /* either a pmt/variable relative to frame pointer or
1593 * pmt/variable in a register
1595 assert(subpgm->func);
1596 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1597 is_pmt ? DataIsParam : DataIsLocal,
1598 &loc, block, param_type, name.u.string);
1599 break;
1602 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1604 VARIANT v;
1605 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1606 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1607 switch (value.form)
1609 case DW_FORM_data1:
1610 case DW_FORM_data2:
1611 case DW_FORM_data4:
1612 case DW_FORM_udata:
1613 case DW_FORM_addr:
1614 v.n1.n2.vt = VT_UI4;
1615 v.n1.n2.n3.lVal = value.u.uvalue;
1616 break;
1618 case DW_FORM_data8:
1619 v.n1.n2.vt = VT_UI8;
1620 v.n1.n2.n3.llVal = value.u.lluvalue;
1621 break;
1623 case DW_FORM_sdata:
1624 v.n1.n2.vt = VT_I4;
1625 v.n1.n2.n3.lVal = value.u.svalue;
1626 break;
1628 case DW_FORM_strp:
1629 case DW_FORM_string:
1630 /* FIXME: native doesn't report const strings from here !!
1631 * however, the value of the string is in the code somewhere
1633 v.n1.n2.vt = VT_I1 | VT_BYREF;
1634 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1635 break;
1637 case DW_FORM_block:
1638 case DW_FORM_block1:
1639 case DW_FORM_block2:
1640 case DW_FORM_block4:
1641 v.n1.n2.vt = VT_I4;
1642 switch (value.u.block.size)
1644 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1645 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1646 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1647 default:
1648 v.n1.n2.vt = VT_I1 | VT_BYREF;
1649 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1650 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1652 break;
1654 default:
1655 FIXME("Unsupported form for const value %s (%lx)\n",
1656 name.u.string, value.form);
1657 v.n1.n2.vt = VT_EMPTY;
1659 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1660 name.u.string, param_type, &v)->symt;
1662 else
1664 /* variable has been optimized away... report anyway */
1665 loc.kind = loc_error;
1666 loc.reg = loc_err_no_location;
1667 if (subpgm->func)
1669 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1670 is_pmt ? DataIsParam : DataIsLocal,
1671 &loc, block, param_type, name.u.string);
1673 else
1675 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1678 if (is_pmt && subpgm->func && subpgm->func->type)
1679 symt_add_function_signature_parameter(subpgm->ctx->module,
1680 (struct symt_function_signature*)subpgm->func->type,
1681 param_type);
1683 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1686 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1687 const dwarf2_debug_info_t* di)
1689 struct attribute name;
1690 struct attribute low_pc;
1691 struct location loc;
1693 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1695 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1696 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1697 name.u.string = NULL;
1699 loc.kind = loc_absolute;
1700 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1701 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1702 &loc, name.u.string);
1705 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1706 struct symt_block* parent_block,
1707 dwarf2_debug_info_t* di);
1709 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1710 struct symt_block* parent_block,
1711 dwarf2_debug_info_t* di)
1713 struct symt_block* block;
1714 unsigned long low_pc, high_pc;
1715 struct vector* children;
1716 dwarf2_debug_info_t*child;
1717 unsigned int i;
1719 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1721 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1723 FIXME("cannot read range\n");
1724 return;
1727 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1728 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1729 high_pc - low_pc);
1731 children = dwarf2_get_di_children(subpgm->ctx, di);
1732 if (children) for (i = 0; i < vector_length(children); i++)
1734 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1736 switch (child->abbrev->tag)
1738 case DW_TAG_formal_parameter:
1739 case DW_TAG_variable:
1740 dwarf2_parse_variable(subpgm, block, child);
1741 break;
1742 case DW_TAG_lexical_block:
1743 dwarf2_parse_subprogram_block(subpgm, block, child);
1744 break;
1745 case DW_TAG_inlined_subroutine:
1746 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1747 break;
1748 case DW_TAG_label:
1749 dwarf2_parse_subprogram_label(subpgm, child);
1750 break;
1751 case DW_TAG_GNU_call_site:
1752 /* this isn't properly supported by dbghelp interface. skip it for now */
1753 break;
1754 default:
1755 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1756 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1757 dwarf2_debug_di(di));
1760 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1763 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1764 struct symt_block* parent_block,
1765 dwarf2_debug_info_t* di)
1767 struct symt_block* block;
1768 unsigned long low_pc, high_pc;
1769 struct vector* children;
1770 dwarf2_debug_info_t*child;
1771 unsigned int i;
1773 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1775 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1777 FIXME("no range\n");
1778 return;
1781 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1782 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1783 high_pc - low_pc);
1785 children = dwarf2_get_di_children(subpgm->ctx, di);
1786 if (children) for (i = 0; i < vector_length(children); i++)
1788 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1790 switch (child->abbrev->tag)
1792 case DW_TAG_inlined_subroutine:
1793 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1794 break;
1795 case DW_TAG_variable:
1796 dwarf2_parse_variable(subpgm, block, child);
1797 break;
1798 case DW_TAG_lexical_block:
1799 dwarf2_parse_subprogram_block(subpgm, block, child);
1800 break;
1801 case DW_TAG_subprogram:
1802 /* FIXME: likely a declaration (to be checked)
1803 * skip it for now
1805 break;
1806 case DW_TAG_formal_parameter:
1807 /* FIXME: likely elements for exception handling (GCC flavor)
1808 * Skip it for now
1810 break;
1811 case DW_TAG_imported_module:
1812 /* C++ stuff to be silenced (for now) */
1813 break;
1814 case DW_TAG_GNU_call_site:
1815 /* this isn't properly supported by dbghelp interface. skip it for now */
1816 break;
1817 case DW_TAG_label:
1818 dwarf2_parse_subprogram_label(subpgm, child);
1819 break;
1820 case DW_TAG_class_type:
1821 case DW_TAG_structure_type:
1822 case DW_TAG_union_type:
1823 case DW_TAG_enumeration_type:
1824 case DW_TAG_typedef:
1825 /* the type referred to will be loaded when we need it, so skip it */
1826 break;
1827 default:
1828 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1829 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1833 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1836 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1837 dwarf2_debug_info_t* di)
1839 struct attribute name;
1840 unsigned long low_pc, high_pc;
1841 struct attribute is_decl;
1842 struct attribute inline_flags;
1843 struct symt* ret_type;
1844 struct symt_function_signature* sig_type;
1845 dwarf2_subprogram_t subpgm;
1846 struct vector* children;
1847 dwarf2_debug_info_t* child;
1848 unsigned int i;
1850 if (di->symt) return di->symt;
1852 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1854 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1856 WARN("No name for function... dropping function\n");
1857 return NULL;
1859 /* if it's an abstract representation of an inline function, there should be
1860 * a concrete object that we'll handle
1862 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1863 inline_flags.u.uvalue != DW_INL_not_inlined)
1865 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1866 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1867 return NULL;
1870 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1871 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1873 /* it's a real declaration, skip it */
1874 return NULL;
1876 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1878 WARN("cannot get range for %s\n", name.u.string);
1879 return NULL;
1881 /* As functions (defined as inline assembly) get debug info with dwarf
1882 * (not the case for stabs), we just drop Wine's thunks here...
1883 * Actual thunks will be created in elf_module from the symbol table
1885 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1886 return NULL;
1887 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1889 ret_type = ctx->symt_cache[sc_void];
1890 assert(ret_type);
1892 /* FIXME: assuming C source code */
1893 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1894 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1895 dwarf2_get_cpp_name(ctx, di, name.u.string),
1896 ctx->load_offset + low_pc, high_pc - low_pc,
1897 &sig_type->symt);
1898 di->symt = &subpgm.func->symt;
1899 subpgm.ctx = ctx;
1900 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1901 &subpgm.frame, NULL))
1903 /* on stack !! */
1904 subpgm.frame.kind = loc_regrel;
1905 subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
1906 subpgm.frame.offset = 0;
1908 subpgm.non_computed_variable = FALSE;
1910 children = dwarf2_get_di_children(ctx, di);
1911 if (children) for (i = 0; i < vector_length(children); i++)
1913 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1915 switch (child->abbrev->tag)
1917 case DW_TAG_variable:
1918 case DW_TAG_formal_parameter:
1919 dwarf2_parse_variable(&subpgm, NULL, child);
1920 break;
1921 case DW_TAG_lexical_block:
1922 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1923 break;
1924 case DW_TAG_inlined_subroutine:
1925 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1926 break;
1927 case DW_TAG_subprogram:
1928 /* FIXME: likely a declaration (to be checked)
1929 * skip it for now
1931 break;
1932 case DW_TAG_label:
1933 dwarf2_parse_subprogram_label(&subpgm, child);
1934 break;
1935 case DW_TAG_class_type:
1936 case DW_TAG_structure_type:
1937 case DW_TAG_union_type:
1938 case DW_TAG_enumeration_type:
1939 case DW_TAG_typedef:
1940 /* the type referred to will be loaded when we need it, so skip it */
1941 break;
1942 case DW_TAG_unspecified_parameters:
1943 case DW_TAG_template_type_param:
1944 case DW_TAG_template_value_param:
1945 case DW_TAG_GNU_call_site:
1946 /* FIXME: no support in dbghelp's internals so far */
1947 break;
1948 default:
1949 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1950 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1954 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1956 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1957 &subpgm.frame, NULL);
1959 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1961 return di->symt;
1964 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1965 dwarf2_debug_info_t* di)
1967 struct symt* ret_type;
1968 struct symt_function_signature* sig_type;
1969 struct vector* children;
1970 dwarf2_debug_info_t* child;
1971 unsigned int i;
1973 if (di->symt) return di->symt;
1975 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1977 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1979 ret_type = ctx->symt_cache[sc_void];
1980 assert(ret_type);
1983 /* FIXME: assuming C source code */
1984 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1986 children = dwarf2_get_di_children(ctx, di);
1987 if (children) for (i = 0; i < vector_length(children); i++)
1989 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1991 switch (child->abbrev->tag)
1993 case DW_TAG_formal_parameter:
1994 symt_add_function_signature_parameter(ctx->module, sig_type,
1995 dwarf2_lookup_type(ctx, child));
1996 break;
1997 case DW_TAG_unspecified_parameters:
1998 WARN("Unsupported unspecified parameters\n");
1999 break;
2003 return di->symt = &sig_type->symt;
2006 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
2007 dwarf2_debug_info_t* di)
2009 struct vector* children;
2010 dwarf2_debug_info_t* child;
2011 unsigned int i;
2013 if (di->symt) return;
2015 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2017 di->symt = ctx->symt_cache[sc_void];
2019 children = dwarf2_get_di_children(ctx, di);
2020 if (children) for (i = 0; i < vector_length(children); i++)
2022 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2023 dwarf2_load_one_entry(ctx, child);
2027 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2028 dwarf2_debug_info_t* di)
2030 switch (di->abbrev->tag)
2032 case DW_TAG_typedef:
2033 dwarf2_parse_typedef(ctx, di);
2034 break;
2035 case DW_TAG_base_type:
2036 dwarf2_parse_base_type(ctx, di);
2037 break;
2038 case DW_TAG_pointer_type:
2039 dwarf2_parse_pointer_type(ctx, di);
2040 break;
2041 case DW_TAG_class_type:
2042 dwarf2_parse_udt_type(ctx, di, UdtClass);
2043 break;
2044 case DW_TAG_structure_type:
2045 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2046 break;
2047 case DW_TAG_union_type:
2048 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2049 break;
2050 case DW_TAG_array_type:
2051 dwarf2_parse_array_type(ctx, di);
2052 break;
2053 case DW_TAG_const_type:
2054 dwarf2_parse_const_type(ctx, di);
2055 break;
2056 case DW_TAG_volatile_type:
2057 dwarf2_parse_volatile_type(ctx, di);
2058 break;
2059 case DW_TAG_reference_type:
2060 dwarf2_parse_reference_type(ctx, di);
2061 break;
2062 case DW_TAG_enumeration_type:
2063 dwarf2_parse_enumeration_type(ctx, di);
2064 break;
2065 case DW_TAG_subprogram:
2066 dwarf2_parse_subprogram(ctx, di);
2067 break;
2068 case DW_TAG_subroutine_type:
2069 dwarf2_parse_subroutine_type(ctx, di);
2070 break;
2071 case DW_TAG_variable:
2073 dwarf2_subprogram_t subpgm;
2075 subpgm.ctx = ctx;
2076 subpgm.func = NULL;
2077 subpgm.frame.kind = loc_absolute;
2078 subpgm.frame.offset = 0;
2079 subpgm.frame.reg = Wine_DW_no_register;
2080 dwarf2_parse_variable(&subpgm, NULL, di);
2082 break;
2083 case DW_TAG_namespace:
2084 dwarf2_parse_namespace(ctx, di);
2085 break;
2086 /* silence a couple of C++ defines */
2087 case DW_TAG_imported_module:
2088 case DW_TAG_imported_declaration:
2089 case DW_TAG_ptr_to_member_type:
2090 break;
2091 default:
2092 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2093 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2097 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2098 const struct vector* v, unsigned file, unsigned line)
2100 struct symt_function* func;
2101 struct symt_ht* symt;
2102 unsigned* psrc;
2104 if (!file || !(psrc = vector_at(v, file - 1))) return;
2106 TRACE("%s %lx %s %u\n",
2107 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2108 if (!(symt = symt_find_nearest(module, address)) ||
2109 symt->symt.tag != SymTagFunction) return;
2110 func = (struct symt_function*)symt;
2111 symt_add_func_line(module, func, *psrc, line, address - func->address);
2114 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2115 dwarf2_parse_context_t* ctx,
2116 const char* compile_dir,
2117 unsigned long offset)
2119 dwarf2_traverse_context_t traverse;
2120 unsigned long length;
2121 unsigned insn_size, default_stmt;
2122 unsigned line_range, opcode_base;
2123 int line_base;
2124 const unsigned char* opcode_len;
2125 struct vector dirs;
2126 struct vector files;
2127 const char** p;
2129 /* section with line numbers stripped */
2130 if (sections[section_line].address == IMAGE_NO_MAP)
2131 return FALSE;
2133 if (offset + 4 > sections[section_line].size)
2135 WARN("out of bounds offset\n");
2136 return FALSE;
2138 traverse.data = sections[section_line].address + offset;
2139 traverse.end_data = traverse.data + 4;
2140 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2142 length = dwarf2_parse_u4(&traverse);
2143 traverse.end_data = sections[section_line].address + offset + length;
2145 if (offset + 4 + length > sections[section_line].size)
2147 WARN("out of bounds header\n");
2148 return FALSE;
2150 dwarf2_parse_u2(&traverse); /* version */
2151 dwarf2_parse_u4(&traverse); /* header_len */
2152 insn_size = dwarf2_parse_byte(&traverse);
2153 default_stmt = dwarf2_parse_byte(&traverse);
2154 line_base = (signed char)dwarf2_parse_byte(&traverse);
2155 line_range = dwarf2_parse_byte(&traverse);
2156 opcode_base = dwarf2_parse_byte(&traverse);
2158 opcode_len = traverse.data;
2159 traverse.data += opcode_base - 1;
2161 vector_init(&dirs, sizeof(const char*), 4);
2162 p = vector_add(&dirs, &ctx->pool);
2163 *p = compile_dir ? compile_dir : ".";
2164 while (*traverse.data)
2166 const char* rel = (const char*)traverse.data;
2167 unsigned rellen = strlen(rel);
2168 TRACE("Got include %s\n", rel);
2169 traverse.data += rellen + 1;
2170 p = vector_add(&dirs, &ctx->pool);
2172 if (*rel == '/' || !compile_dir)
2173 *p = rel;
2174 else
2176 /* include directory relative to compile directory */
2177 unsigned baselen = strlen(compile_dir);
2178 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2179 strcpy(tmp, compile_dir);
2180 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2181 strcpy(&tmp[baselen], rel);
2182 *p = tmp;
2186 traverse.data++;
2188 vector_init(&files, sizeof(unsigned), 16);
2189 while (*traverse.data)
2191 unsigned int dir_index, mod_time;
2192 const char* name;
2193 const char* dir;
2194 unsigned* psrc;
2196 name = (const char*)traverse.data;
2197 traverse.data += strlen(name) + 1;
2198 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2199 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2200 length = dwarf2_leb128_as_unsigned(&traverse);
2201 dir = *(const char**)vector_at(&dirs, dir_index);
2202 TRACE("Got file %s/%s (%u,%lu)\n", dir, name, mod_time, length);
2203 psrc = vector_add(&files, &ctx->pool);
2204 *psrc = source_new(ctx->module, dir, name);
2206 traverse.data++;
2208 while (traverse.data < traverse.end_data)
2210 unsigned long address = 0;
2211 unsigned file = 1;
2212 unsigned line = 1;
2213 unsigned is_stmt = default_stmt;
2214 BOOL end_sequence = FALSE;
2215 unsigned opcode, extopcode, i;
2217 while (!end_sequence)
2219 opcode = dwarf2_parse_byte(&traverse);
2220 TRACE("Got opcode %x\n", opcode);
2222 if (opcode >= opcode_base)
2224 unsigned delta = opcode - opcode_base;
2226 address += (delta / line_range) * insn_size;
2227 line += line_base + (delta % line_range);
2228 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2230 else
2232 switch (opcode)
2234 case DW_LNS_copy:
2235 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2236 break;
2237 case DW_LNS_advance_pc:
2238 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2239 break;
2240 case DW_LNS_advance_line:
2241 line += dwarf2_leb128_as_signed(&traverse);
2242 break;
2243 case DW_LNS_set_file:
2244 file = dwarf2_leb128_as_unsigned(&traverse);
2245 break;
2246 case DW_LNS_set_column:
2247 dwarf2_leb128_as_unsigned(&traverse);
2248 break;
2249 case DW_LNS_negate_stmt:
2250 is_stmt = !is_stmt;
2251 break;
2252 case DW_LNS_set_basic_block:
2253 break;
2254 case DW_LNS_const_add_pc:
2255 address += ((255 - opcode_base) / line_range) * insn_size;
2256 break;
2257 case DW_LNS_fixed_advance_pc:
2258 address += dwarf2_parse_u2(&traverse);
2259 break;
2260 case DW_LNS_extended_op:
2261 dwarf2_leb128_as_unsigned(&traverse);
2262 extopcode = dwarf2_parse_byte(&traverse);
2263 switch (extopcode)
2265 case DW_LNE_end_sequence:
2266 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2267 end_sequence = TRUE;
2268 break;
2269 case DW_LNE_set_address:
2270 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2271 break;
2272 case DW_LNE_define_file:
2273 FIXME("not handled define file %s\n", traverse.data);
2274 traverse.data += strlen((const char *)traverse.data) + 1;
2275 dwarf2_leb128_as_unsigned(&traverse);
2276 dwarf2_leb128_as_unsigned(&traverse);
2277 dwarf2_leb128_as_unsigned(&traverse);
2278 break;
2279 case DW_LNE_set_discriminator:
2281 unsigned descr;
2283 descr = dwarf2_leb128_as_unsigned(&traverse);
2284 WARN("not handled discriminator %x\n", descr);
2286 break;
2287 default:
2288 FIXME("Unsupported extended opcode %x\n", extopcode);
2289 break;
2291 break;
2292 default:
2293 WARN("Unsupported opcode %x\n", opcode);
2294 for (i = 0; i < opcode_len[opcode]; i++)
2295 dwarf2_leb128_as_unsigned(&traverse);
2296 break;
2301 return TRUE;
2304 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2305 struct module* module,
2306 const struct elf_thunk_area* thunks,
2307 dwarf2_traverse_context_t* mod_ctx,
2308 unsigned long load_offset)
2310 dwarf2_parse_context_t ctx;
2311 dwarf2_traverse_context_t abbrev_ctx;
2312 dwarf2_debug_info_t* di;
2313 dwarf2_traverse_context_t cu_ctx;
2314 const unsigned char* comp_unit_start = mod_ctx->data;
2315 unsigned long cu_length;
2316 unsigned short cu_version;
2317 unsigned long cu_abbrev_offset;
2318 BOOL ret = FALSE;
2320 cu_length = dwarf2_parse_u4(mod_ctx);
2321 cu_ctx.data = mod_ctx->data;
2322 cu_ctx.end_data = mod_ctx->data + cu_length;
2323 mod_ctx->data += cu_length;
2324 cu_version = dwarf2_parse_u2(&cu_ctx);
2325 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2326 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2328 TRACE("Compilation Unit Header found at 0x%x:\n",
2329 (int)(comp_unit_start - sections[section_debug].address));
2330 TRACE("- length: %lu\n", cu_length);
2331 TRACE("- version: %u\n", cu_version);
2332 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2333 TRACE("- word_size: %u\n", cu_ctx.word_size);
2335 if (cu_version != 2)
2337 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2338 cu_version);
2339 return FALSE;
2342 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2343 mod_ctx->word_size = cu_ctx.word_size;
2345 pool_init(&ctx.pool, 65536);
2346 ctx.sections = sections;
2347 ctx.section = section_debug;
2348 ctx.module = module;
2349 ctx.thunks = thunks;
2350 ctx.load_offset = load_offset;
2351 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2352 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2353 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2354 ctx.cpp_name = NULL;
2356 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2357 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2358 abbrev_ctx.word_size = cu_ctx.word_size;
2359 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2361 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2362 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2364 if (di->abbrev->tag == DW_TAG_compile_unit)
2366 struct attribute name;
2367 struct vector* children;
2368 dwarf2_debug_info_t* child = NULL;
2369 unsigned int i;
2370 struct attribute stmt_list, low_pc;
2371 struct attribute comp_dir;
2373 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2374 name.u.string = NULL;
2376 /* get working directory of current compilation unit */
2377 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2378 comp_dir.u.string = NULL;
2380 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2381 low_pc.u.uvalue = 0;
2382 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2383 source_new(module, comp_dir.u.string, name.u.string));
2384 di->symt = &ctx.compiland->symt;
2385 children = dwarf2_get_di_children(&ctx, di);
2386 if (children) for (i = 0; i < vector_length(children); i++)
2388 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2389 dwarf2_load_one_entry(&ctx, child);
2391 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2393 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2394 module->module.LineNumbers = TRUE;
2396 ret = TRUE;
2398 else FIXME("Should have a compilation unit here\n");
2399 pool_destroy(&ctx.pool);
2400 return ret;
2403 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2404 unsigned long ip, dwarf2_traverse_context_t* lctx)
2406 DWORD_PTR beg, end;
2407 const BYTE* ptr = start;
2408 DWORD len;
2410 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2412 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2413 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2414 if (!beg && !end) break;
2415 len = dwarf2_get_u2(ptr); ptr += 2;
2417 if (beg <= ip && ip < end)
2419 lctx->data = ptr;
2420 lctx->end_data = ptr + len;
2421 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2422 return TRUE;
2424 ptr += len;
2426 WARN("Couldn't find ip in location list\n");
2427 return FALSE;
2430 static enum location_error loc_compute_frame(struct process* pcs,
2431 const struct module_format* modfmt,
2432 const struct symt_function* func,
2433 DWORD_PTR ip, struct location* frame)
2435 struct symt** psym = NULL;
2436 struct location* pframe;
2437 dwarf2_traverse_context_t lctx;
2438 enum location_error err;
2439 unsigned int i;
2441 for (i=0; i<vector_length(&func->vchildren); i++)
2443 psym = vector_at(&func->vchildren, i);
2444 if ((*psym)->tag == SymTagCustom)
2446 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2448 /* First, recompute the frame information, if needed */
2449 switch (pframe->kind)
2451 case loc_regrel:
2452 case loc_register:
2453 *frame = *pframe;
2454 break;
2455 case loc_dwarf2_location_list:
2456 WARN("Searching loclist for %s\n", func->hash_elt.name);
2457 if (!dwarf2_lookup_loclist(modfmt,
2458 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2459 ip, &lctx))
2460 return loc_err_out_of_scope;
2461 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2462 if (frame->kind >= loc_user)
2464 WARN("Couldn't compute runtime frame location\n");
2465 return loc_err_too_complex;
2467 break;
2468 default:
2469 WARN("Unsupported frame kind %d\n", pframe->kind);
2470 return loc_err_internal;
2472 return 0;
2475 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2476 return loc_err_internal;
2479 enum reg_rule
2481 RULE_UNSET, /* not set at all */
2482 RULE_UNDEFINED, /* undefined value */
2483 RULE_SAME, /* same value as previous frame */
2484 RULE_CFA_OFFSET, /* stored at cfa offset */
2485 RULE_OTHER_REG, /* stored in other register */
2486 RULE_EXPRESSION, /* address specified by expression */
2487 RULE_VAL_EXPRESSION /* value specified by expression */
2490 /* make it large enough for all CPUs */
2491 #define NB_FRAME_REGS 64
2492 #define MAX_SAVED_STATES 16
2494 struct frame_state
2496 ULONG_PTR cfa_offset;
2497 unsigned char cfa_reg;
2498 enum reg_rule cfa_rule;
2499 enum reg_rule rules[NB_FRAME_REGS];
2500 ULONG_PTR regs[NB_FRAME_REGS];
2503 struct frame_info
2505 ULONG_PTR ip;
2506 ULONG_PTR code_align;
2507 LONG_PTR data_align;
2508 unsigned char retaddr_reg;
2509 unsigned char fde_encoding;
2510 unsigned char lsda_encoding;
2511 unsigned char signal_frame;
2512 unsigned char aug_z_format;
2513 unsigned char state_sp;
2514 struct frame_state state;
2515 struct frame_state state_stack[MAX_SAVED_STATES];
2518 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2520 ULONG_PTR base;
2522 if (encoding == DW_EH_PE_omit) return 0;
2524 switch (encoding & 0xf0)
2526 case DW_EH_PE_abs:
2527 base = 0;
2528 break;
2529 case DW_EH_PE_pcrel:
2530 base = (ULONG_PTR)ctx->data;
2531 break;
2532 default:
2533 FIXME("unsupported encoding %02x\n", encoding);
2534 return 0;
2537 switch (encoding & 0x0f)
2539 case DW_EH_PE_native:
2540 return base + dwarf2_parse_addr(ctx);
2541 case DW_EH_PE_leb128:
2542 return base + dwarf2_leb128_as_unsigned(ctx);
2543 case DW_EH_PE_data2:
2544 return base + dwarf2_parse_u2(ctx);
2545 case DW_EH_PE_data4:
2546 return base + dwarf2_parse_u4(ctx);
2547 case DW_EH_PE_data8:
2548 return base + dwarf2_parse_u8(ctx);
2549 case DW_EH_PE_signed|DW_EH_PE_leb128:
2550 return base + dwarf2_leb128_as_signed(ctx);
2551 case DW_EH_PE_signed|DW_EH_PE_data2:
2552 return base + (signed short)dwarf2_parse_u2(ctx);
2553 case DW_EH_PE_signed|DW_EH_PE_data4:
2554 return base + (signed int)dwarf2_parse_u4(ctx);
2555 case DW_EH_PE_signed|DW_EH_PE_data8:
2556 return base + (LONG64)dwarf2_parse_u8(ctx);
2557 default:
2558 FIXME("unsupported encoding %02x\n", encoding);
2559 return 0;
2563 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2565 unsigned char version;
2566 const char* augmentation;
2567 const unsigned char* end;
2568 ULONG_PTR len;
2570 memset(info, 0, sizeof(*info));
2571 info->lsda_encoding = DW_EH_PE_omit;
2572 info->aug_z_format = 0;
2574 /* parse the CIE first */
2575 version = dwarf2_parse_byte(ctx);
2576 if (version != 1)
2578 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2579 return FALSE;
2581 augmentation = (const char*)ctx->data;
2582 ctx->data += strlen(augmentation) + 1;
2584 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2585 info->data_align = dwarf2_leb128_as_signed(ctx);
2586 info->retaddr_reg = dwarf2_parse_byte(ctx);
2587 info->state.cfa_rule = RULE_CFA_OFFSET;
2589 end = NULL;
2590 TRACE("\tparsing augmentation %s\n", augmentation);
2591 if (*augmentation) do
2593 switch (*augmentation)
2595 case 'z':
2596 len = dwarf2_leb128_as_unsigned(ctx);
2597 end = ctx->data + len;
2598 info->aug_z_format = 1;
2599 continue;
2600 case 'L':
2601 info->lsda_encoding = dwarf2_parse_byte(ctx);
2602 continue;
2603 case 'P':
2605 unsigned char encoding = dwarf2_parse_byte(ctx);
2606 /* throw away the indirect bit, as we don't care for the result */
2607 encoding &= ~DW_EH_PE_indirect;
2608 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2609 continue;
2611 case 'R':
2612 info->fde_encoding = dwarf2_parse_byte(ctx);
2613 continue;
2614 case 'S':
2615 info->signal_frame = 1;
2616 continue;
2618 FIXME("unknown augmentation '%c'\n", *augmentation);
2619 if (!end) return FALSE;
2620 break;
2621 } while (*++augmentation);
2622 if (end) ctx->data = end;
2623 return TRUE;
2626 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2627 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2628 struct frame_info* info, BOOL in_eh_frame)
2630 const unsigned char* ptr_blk;
2631 const unsigned char* cie_ptr;
2632 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2633 unsigned len, id;
2634 unsigned long start, range;
2635 unsigned cie_id;
2636 const BYTE* start_data = fde_ctx->data;
2638 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2639 /* skip 0-padding at beginning of section (alignment) */
2640 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
2642 if (dwarf2_parse_u4(fde_ctx))
2644 fde_ctx->data -= 4;
2645 break;
2648 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2650 /* find the FDE for address addr (skip CIE) */
2651 len = dwarf2_parse_u4(fde_ctx);
2652 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2653 ptr_blk = fde_ctx->data + len;
2654 id = dwarf2_parse_u4(fde_ctx);
2655 if (id == cie_id)
2657 last_cie_ptr = fde_ctx->data - 8;
2658 /* we need some bits out of the CIE in order to parse all contents */
2659 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2660 cie_ctx->data = fde_ctx->data;
2661 cie_ctx->end_data = ptr_blk;
2662 cie_ctx->word_size = fde_ctx->word_size;
2663 continue;
2665 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2666 if (cie_ptr != last_cie_ptr)
2668 last_cie_ptr = cie_ptr;
2669 cie_ctx->data = cie_ptr;
2670 cie_ctx->word_size = fde_ctx->word_size;
2671 cie_ctx->end_data = cie_ptr + 4;
2672 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2673 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2675 FIXME("wrong CIE pointer at %x from FDE %x\n",
2676 (unsigned)(cie_ptr - start_data),
2677 (unsigned)(fde_ctx->data - start_data));
2678 return FALSE;
2680 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2682 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2683 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2685 if (addr >= start && addr < start + range)
2687 /* reset the FDE context */
2688 fde_ctx->end_data = ptr_blk;
2690 info->ip = start;
2691 return TRUE;
2694 return FALSE;
2697 static int valid_reg(ULONG_PTR reg)
2699 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2700 return (reg < NB_FRAME_REGS);
2703 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2704 ULONG_PTR last_ip, struct frame_info *info)
2706 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2708 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2710 if (op & 0xc0)
2712 switch (op & 0xc0)
2714 case DW_CFA_advance_loc:
2716 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2717 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2718 info->ip += offset;
2719 break;
2721 case DW_CFA_offset:
2723 ULONG_PTR reg = op & 0x3f;
2724 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2725 if (!valid_reg(reg)) break;
2726 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2727 info->ip,
2728 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2729 offset);
2730 info->state.regs[reg] = offset;
2731 info->state.rules[reg] = RULE_CFA_OFFSET;
2732 break;
2734 case DW_CFA_restore:
2736 ULONG_PTR reg = op & 0x3f;
2737 if (!valid_reg(reg)) break;
2738 TRACE("%lx: DW_CFA_restore %s\n",
2739 info->ip,
2740 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2741 info->state.rules[reg] = RULE_UNSET;
2742 break;
2746 else switch (op)
2748 case DW_CFA_nop:
2749 break;
2750 case DW_CFA_set_loc:
2752 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2753 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2754 info->ip = loc;
2755 break;
2757 case DW_CFA_advance_loc1:
2759 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2760 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2761 info->ip += offset;
2762 break;
2764 case DW_CFA_advance_loc2:
2766 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2767 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2768 info->ip += offset;
2769 break;
2771 case DW_CFA_advance_loc4:
2773 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2774 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2775 info->ip += offset;
2776 break;
2778 case DW_CFA_offset_extended:
2779 case DW_CFA_offset_extended_sf:
2781 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2782 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2783 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2784 if (!valid_reg(reg)) break;
2785 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2786 info->ip,
2787 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2788 offset);
2789 info->state.regs[reg] = offset;
2790 info->state.rules[reg] = RULE_CFA_OFFSET;
2791 break;
2793 case DW_CFA_restore_extended:
2795 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2796 if (!valid_reg(reg)) break;
2797 TRACE("%lx: DW_CFA_restore_extended %s\n",
2798 info->ip,
2799 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2800 info->state.rules[reg] = RULE_UNSET;
2801 break;
2803 case DW_CFA_undefined:
2805 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2806 if (!valid_reg(reg)) break;
2807 TRACE("%lx: DW_CFA_undefined %s\n",
2808 info->ip,
2809 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2810 info->state.rules[reg] = RULE_UNDEFINED;
2811 break;
2813 case DW_CFA_same_value:
2815 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2816 if (!valid_reg(reg)) break;
2817 TRACE("%lx: DW_CFA_same_value %s\n",
2818 info->ip,
2819 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2820 info->state.regs[reg] = reg;
2821 info->state.rules[reg] = RULE_SAME;
2822 break;
2824 case DW_CFA_register:
2826 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2827 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2828 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2829 TRACE("%lx: DW_CFA_register %s == %s\n",
2830 info->ip,
2831 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2832 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2833 info->state.regs[reg] = reg2;
2834 info->state.rules[reg] = RULE_OTHER_REG;
2835 break;
2837 case DW_CFA_remember_state:
2838 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2839 if (info->state_sp >= MAX_SAVED_STATES)
2840 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2841 else
2842 info->state_stack[info->state_sp++] = info->state;
2843 break;
2844 case DW_CFA_restore_state:
2845 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2846 if (!info->state_sp)
2847 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2848 else
2849 info->state = info->state_stack[--info->state_sp];
2850 break;
2851 case DW_CFA_def_cfa:
2852 case DW_CFA_def_cfa_sf:
2854 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2855 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2856 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2857 if (!valid_reg(reg)) break;
2858 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2859 info->ip,
2860 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2861 offset);
2862 info->state.cfa_reg = reg;
2863 info->state.cfa_offset = offset;
2864 info->state.cfa_rule = RULE_CFA_OFFSET;
2865 break;
2867 case DW_CFA_def_cfa_register:
2869 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2870 if (!valid_reg(reg)) break;
2871 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2872 info->ip,
2873 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2874 info->state.cfa_reg = reg;
2875 info->state.cfa_rule = RULE_CFA_OFFSET;
2876 break;
2878 case DW_CFA_def_cfa_offset:
2879 case DW_CFA_def_cfa_offset_sf:
2881 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2882 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2883 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
2884 info->state.cfa_offset = offset;
2885 info->state.cfa_rule = RULE_CFA_OFFSET;
2886 break;
2888 case DW_CFA_def_cfa_expression:
2890 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2891 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2892 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2893 info->state.cfa_offset = expr;
2894 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2895 ctx->data += len;
2896 break;
2898 case DW_CFA_expression:
2899 case DW_CFA_val_expression:
2901 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2902 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2903 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2904 if (!valid_reg(reg)) break;
2905 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2906 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2907 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2908 expr, expr + len);
2909 info->state.regs[reg] = expr;
2910 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2911 ctx->data += len;
2912 break;
2914 case DW_CFA_GNU_args_size:
2915 /* FIXME: should check that GCC is the compiler for this CU */
2917 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2918 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2919 /* ignored */
2920 break;
2922 default:
2923 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2924 break;
2929 /* retrieve a context register from its dwarf number */
2930 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2932 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2933 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2935 if (sz != sizeof(ULONG_PTR))
2937 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2938 return 0;
2940 return *ptr;
2943 /* set a context register from its dwarf number */
2944 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2945 ULONG_PTR val, BOOL isdebuggee)
2947 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2948 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2950 if (isdebuggee)
2952 char tmp[16];
2954 if (sz > sizeof(tmp))
2956 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2957 return;
2959 if (!sw_read_mem(csw, val, tmp, sz))
2961 WARN("Couldn't read memory at %p\n", (void*)val);
2962 return;
2964 memcpy(ptr, tmp, sz);
2966 else
2968 if (sz != sizeof(ULONG_PTR))
2970 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2971 return;
2973 *ptr = val;
2977 /* copy a register from one context to another using dwarf number */
2978 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2980 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2981 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2982 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2983 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2985 if (szdst != szsrc)
2987 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2988 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2989 return;
2991 memcpy(ptrdst, ptrsrc, szdst);
2994 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
2995 const unsigned char* zp, CONTEXT *context)
2997 dwarf2_traverse_context_t ctx;
2998 ULONG_PTR reg, sz, tmp, stack[64];
2999 int sp = -1;
3000 ULONG_PTR len;
3002 ctx.data = zp;
3003 ctx.end_data = zp + 4;
3004 len = dwarf2_leb128_as_unsigned(&ctx);
3005 ctx.end_data = ctx.data + len;
3006 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3008 while (ctx.data < ctx.end_data)
3010 unsigned char opcode = dwarf2_parse_byte(&ctx);
3012 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3013 stack[++sp] = opcode - DW_OP_lit0;
3014 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3015 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
3016 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3017 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
3018 else switch (opcode)
3020 case DW_OP_nop: break;
3021 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
3022 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3023 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3024 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3025 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3026 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3027 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3028 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3029 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3030 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3031 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3032 case DW_OP_deref:
3033 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
3035 ERR("Couldn't read memory at %lx\n", stack[sp]);
3036 tmp = 0;
3038 stack[sp] = tmp;
3039 break;
3040 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3041 case DW_OP_drop: sp--; break;
3042 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3043 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3044 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3045 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3046 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3047 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3048 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3049 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3050 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3051 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3052 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3053 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3054 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3055 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3056 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3057 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3058 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3059 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3060 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3061 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3062 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3063 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3064 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3065 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3066 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3067 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3068 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3069 case DW_OP_GNU_encoded_addr:
3070 tmp = dwarf2_parse_byte(&ctx);
3071 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3072 break;
3073 case DW_OP_regx:
3074 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
3075 break;
3076 case DW_OP_bregx:
3077 reg = dwarf2_leb128_as_unsigned(&ctx);
3078 tmp = dwarf2_leb128_as_signed(&ctx);
3079 stack[++sp] = get_context_reg(context, reg) + tmp;
3080 break;
3081 case DW_OP_deref_size:
3082 sz = dwarf2_parse_byte(&ctx);
3083 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3085 ERR("Couldn't read memory at %lx\n", stack[sp]);
3086 tmp = 0;
3088 /* do integral promotion */
3089 switch (sz)
3091 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3092 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3093 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3094 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3095 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3097 break;
3098 default:
3099 FIXME("unhandled opcode %02x\n", opcode);
3102 return stack[sp];
3105 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3106 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3108 unsigned int i;
3109 ULONG_PTR value;
3110 CONTEXT new_context = *context;
3112 switch (state->cfa_rule)
3114 case RULE_EXPRESSION:
3115 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3116 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
3118 WARN("Couldn't read memory at %p\n", (void*)*cfa);
3119 return;
3121 break;
3122 case RULE_VAL_EXPRESSION:
3123 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3124 break;
3125 default:
3126 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3127 break;
3129 if (!*cfa) return;
3131 for (i = 0; i < NB_FRAME_REGS; i++)
3133 switch (state->rules[i])
3135 case RULE_UNSET:
3136 case RULE_UNDEFINED:
3137 case RULE_SAME:
3138 break;
3139 case RULE_CFA_OFFSET:
3140 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3141 break;
3142 case RULE_OTHER_REG:
3143 copy_context_reg(&new_context, i, context, state->regs[i]);
3144 break;
3145 case RULE_EXPRESSION:
3146 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3147 set_context_reg(csw, &new_context, i, value, TRUE);
3148 break;
3149 case RULE_VAL_EXPRESSION:
3150 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3151 set_context_reg(csw, &new_context, i, value, FALSE);
3152 break;
3155 *context = new_context;
3158 /***********************************************************************
3159 * dwarf2_virtual_unwind
3162 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3164 struct module_pair pair;
3165 struct frame_info info;
3166 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3167 struct module_format* modfmt;
3168 const unsigned char* end;
3169 DWORD_PTR delta;
3171 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3172 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3173 !module_get_debug(&pair))
3174 return FALSE;
3175 modfmt = pair.effective->format_info[DFI_DWARF];
3176 if (!modfmt) return FALSE;
3177 memset(&info, 0, sizeof(info));
3178 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3179 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3180 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3181 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3182 * in this process the IMAGE section at a different address as the one expected by
3183 * the image
3185 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3186 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3187 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3189 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3190 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3191 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3192 delta = pair.effective->reloc_delta;
3193 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3195 TRACE("Couldn't find information for %lx\n", ip);
3196 return FALSE;
3200 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3201 ip, info.ip, info.code_align, info.data_align,
3202 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
3204 /* if at very beginning of function, return and use default unwinder */
3205 if (ip == info.ip) return FALSE;
3206 execute_cfa_instructions(&cie_ctx, ip, &info);
3208 if (info.aug_z_format) /* get length of augmentation data */
3210 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3211 end = fde_ctx.data + len;
3213 else end = NULL;
3214 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3215 if (end) fde_ctx.data = end;
3217 execute_cfa_instructions(&fde_ctx, ip, &info);
3218 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3220 return TRUE;
3223 static void dwarf2_location_compute(struct process* pcs,
3224 const struct module_format* modfmt,
3225 const struct symt_function* func,
3226 struct location* loc)
3228 struct location frame;
3229 DWORD_PTR ip;
3230 int err;
3231 dwarf2_traverse_context_t lctx;
3233 if (!func->container || func->container->tag != SymTagCompiland)
3235 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3236 err = loc_err_internal;
3238 else
3240 /* instruction pointer relative to compiland's start */
3241 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3243 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3245 switch (loc->kind)
3247 case loc_dwarf2_location_list:
3248 /* Then, if the variable has a location list, find it !! */
3249 if (dwarf2_lookup_loclist(modfmt,
3250 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3251 ip, &lctx))
3252 goto do_compute;
3253 err = loc_err_out_of_scope;
3254 break;
3255 case loc_dwarf2_block:
3256 /* or if we have a copy of an existing block, get ready for it */
3258 unsigned* ptr = (unsigned*)loc->offset;
3260 lctx.data = (const BYTE*)(ptr + 1);
3261 lctx.end_data = lctx.data + *ptr;
3262 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3264 do_compute:
3265 /* now get the variable */
3266 err = compute_location(&lctx, loc, pcs->handle, &frame);
3267 break;
3268 case loc_register:
3269 case loc_regrel:
3270 /* nothing to do */
3271 break;
3272 default:
3273 WARN("Unsupported local kind %d\n", loc->kind);
3274 err = loc_err_internal;
3278 if (err < 0)
3280 loc->kind = loc_register;
3281 loc->reg = err;
3285 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3287 HeapFree(GetProcessHeap(), 0, modfmt);
3290 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3291 const char* sectname, struct image_section_map* ism)
3293 struct image_section_map local_ism;
3295 if (!ism) ism = &local_ism;
3296 if (!image_find_section(fmap, sectname, ism))
3298 section->address = NULL;
3299 section->size = 0;
3300 section->rva = 0;
3301 return FALSE;
3304 section->address = (const BYTE*)image_map_section(ism);
3305 section->size = image_get_map_size(ism);
3306 section->rva = image_get_map_rva(ism);
3307 return TRUE;
3310 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3311 const struct elf_thunk_area* thunks,
3312 struct image_file_map* fmap)
3314 dwarf2_section_t eh_frame, section[section_max];
3315 dwarf2_traverse_context_t mod_ctx;
3316 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3317 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3318 BOOL ret = TRUE;
3319 struct module_format* dwarf2_modfmt;
3321 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", &eh_frame_sect);
3322 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", &debug_sect);
3323 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3324 dwarf2_init_section(&section[section_string], fmap, ".debug_str", &debug_str_sect);
3325 dwarf2_init_section(&section[section_line], fmap, ".debug_line", &debug_line_sect);
3326 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", &debug_ranges_sect);
3328 /* to do anything useful we need either .eh_frame or .debug_info */
3329 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3330 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3332 ret = FALSE;
3333 goto leave;
3336 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3338 /* debug info might have a different base address than .so file
3339 * when elf file is prelinked after splitting off debug info
3340 * adjust symbol base addresses accordingly
3342 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3345 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3347 mod_ctx.data = section[section_debug].address;
3348 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3349 mod_ctx.word_size = 0; /* will be correctly set later on */
3351 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3352 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3353 if (!dwarf2_modfmt)
3355 ret = FALSE;
3356 goto leave;
3358 dwarf2_modfmt->module = module;
3359 dwarf2_modfmt->remove = dwarf2_module_remove;
3360 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3361 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3362 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3363 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3365 /* As we'll need later some sections' content, we won't unmap these
3366 * sections upon existing this function
3368 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", NULL);
3369 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3370 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3372 while (mod_ctx.data < mod_ctx.end_data)
3374 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3376 dwarf2_modfmt->module->module.SymType = SymDia;
3377 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3378 /* FIXME: we could have a finer grain here */
3379 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3380 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3381 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3382 dwarf2_modfmt->module->module.Publics = TRUE;
3384 /* set the word_size for eh_frame parsing */
3385 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8;
3387 leave:
3388 image_unmap_section(&debug_sect);
3389 image_unmap_section(&debug_abbrev_sect);
3390 image_unmap_section(&debug_str_sect);
3391 image_unmap_section(&debug_line_sect);
3392 image_unmap_section(&debug_ranges_sect);
3393 if (!ret) image_unmap_section(&eh_frame_sect);
3395 return ret;