user32/tests: Test pending redraw state with owner-drawn list box.
[wine.git] / dlls / dbghelp / dwarf.c
blob53274c4f5978f94986cf4ab8489a7830c3503f1e
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 <sys/types.h>
26 #include <limits.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <stdio.h>
30 #include <assert.h>
31 #include <stdarg.h>
33 #include "windef.h"
34 #include "winternl.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "ole2.h"
38 #include "oleauto.h"
40 #include "dbghelp_private.h"
41 #include "image_private.h"
42 #include "zlib.h"
44 #include "wine/debug.h"
46 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
48 /* FIXME:
49 * - Functions:
50 * o unspecified parameters
51 * o inlined functions
52 * o Debug{Start|End}Point
53 * o CFA
54 * - Udt
55 * o proper types loading (nesting)
58 #if 0
59 static void dump(const void* ptr, unsigned len)
61 int i, j;
62 BYTE msg[128];
63 static const char hexof[] = "0123456789abcdef";
64 const BYTE* x = ptr;
66 for (i = 0; i < len; i += 16)
68 sprintf(msg, "%08x: ", i);
69 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
70 for (j = 0; j < min(16, len - i); j++)
72 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
73 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
74 msg[10 + 3 * j + 2] = ' ';
75 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
76 x[i + j] : '.';
78 msg[10 + 3 * 16] = ' ';
79 msg[10 + 3 * 16 + 1 + 16] = '\0';
80 TRACE("%s\n", msg);
83 #endif
85 /**
87 * Main Specs:
88 * http://www.eagercon.com/dwarf/dwarf3std.htm
89 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
91 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
93 * example of projects who do dwarf2 parsing:
94 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
95 * http://elis.ugent.be/diota/log/ltrace_elf.c
97 #include "dwarf.h"
99 /**
100 * Parsers
103 typedef struct dwarf2_abbrev_entry_attr_s
105 ULONG_PTR attribute;
106 ULONG_PTR form;
107 struct dwarf2_abbrev_entry_attr_s* next;
108 } dwarf2_abbrev_entry_attr_t;
110 typedef struct dwarf2_abbrev_entry_s
112 ULONG_PTR entry_code;
113 ULONG_PTR tag;
114 unsigned char have_child;
115 unsigned num_attr;
116 dwarf2_abbrev_entry_attr_t* attrs;
117 } dwarf2_abbrev_entry_t;
119 struct dwarf2_block
121 unsigned size;
122 const unsigned char* ptr;
125 struct attribute
127 ULONG_PTR form;
128 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
129 union
131 ULONG_PTR uvalue;
132 ULONGLONG lluvalue;
133 LONG_PTR svalue;
134 const char* string;
135 struct dwarf2_block block;
136 } u;
139 typedef struct dwarf2_debug_info_s
141 const dwarf2_abbrev_entry_t*abbrev;
142 struct symt* symt;
143 const unsigned char** data;
144 struct vector children;
145 struct dwarf2_debug_info_s* parent;
146 } dwarf2_debug_info_t;
148 typedef struct dwarf2_section_s
150 BOOL compressed;
151 const unsigned char* address;
152 unsigned size;
153 DWORD_PTR rva;
154 } dwarf2_section_t;
156 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
158 typedef struct dwarf2_traverse_context_s
160 const unsigned char* data;
161 const unsigned char* end_data;
162 unsigned char word_size;
163 } dwarf2_traverse_context_t;
165 /* symt_cache indexes */
166 #define sc_void 0
167 #define sc_int1 1
168 #define sc_int2 2
169 #define sc_int4 3
170 #define sc_num 4
172 typedef struct dwarf2_parse_context_s
174 const dwarf2_section_t* sections;
175 unsigned section;
176 struct pool pool;
177 struct module* module;
178 struct symt_compiland* compiland;
179 const struct elf_thunk_area*thunks;
180 struct sparse_array abbrev_table;
181 struct sparse_array debug_info_table;
182 ULONG_PTR load_offset;
183 ULONG_PTR ref_offset;
184 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
185 char* cpp_name;
186 } dwarf2_parse_context_t;
188 /* stored in the dbghelp's module internal structure for later reuse */
189 struct dwarf2_module_info_s
191 dwarf2_section_t debug_loc;
192 dwarf2_section_t debug_frame;
193 dwarf2_section_t eh_frame;
194 unsigned char word_size;
197 #define loc_dwarf2_location_list (loc_user + 0)
198 #define loc_dwarf2_block (loc_user + 1)
200 /* forward declarations */
201 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
203 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
205 return *ptr;
208 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
210 unsigned char uvalue = dwarf2_get_byte(ctx->data);
211 ctx->data += 1;
212 return uvalue;
215 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
217 return *(const UINT16*)ptr;
220 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
222 unsigned short uvalue = dwarf2_get_u2(ctx->data);
223 ctx->data += 2;
224 return uvalue;
227 static ULONG_PTR dwarf2_get_u4(const unsigned char* ptr)
229 return *(const UINT32*)ptr;
232 static ULONG_PTR dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
234 ULONG_PTR uvalue = dwarf2_get_u4(ctx->data);
235 ctx->data += 4;
236 return uvalue;
239 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
241 return *(const UINT64*)ptr;
244 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
246 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
247 ctx->data += 8;
248 return uvalue;
251 static ULONG_PTR dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
253 ULONG_PTR ret = 0;
254 unsigned char byte;
255 unsigned shift = 0;
259 byte = dwarf2_get_byte(ptr++);
260 ret |= (byte & 0x7f) << shift;
261 shift += 7;
262 } while (byte & 0x80);
264 if (end) *end = ptr;
265 return ret;
268 static ULONG_PTR dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
270 ULONG_PTR ret;
272 assert(ctx);
274 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
276 return ret;
279 static LONG_PTR dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
281 LONG_PTR ret = 0;
282 unsigned char byte;
283 unsigned shift = 0;
284 const unsigned size = sizeof(int) * 8;
288 byte = dwarf2_get_byte(ptr++);
289 ret |= (byte & 0x7f) << shift;
290 shift += 7;
291 } while (byte & 0x80);
292 if (end) *end = ptr;
294 /* as spec: sign bit of byte is 2nd high order bit (80x40)
295 * -> 0x80 is used as flag.
297 if ((shift < size) && (byte & 0x40))
299 ret |= - (1 << shift);
301 return ret;
304 static LONG_PTR dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
306 LONG_PTR ret = 0;
308 assert(ctx);
310 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
311 return ret;
314 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
316 unsigned ret;
317 for (ret = 0; ctx->data[ret] & 0x80; ret++);
318 return ret + 1;
321 /******************************************************************
322 * dwarf2_get_addr
324 * Returns an address.
325 * We assume that in all cases word size from Dwarf matches the size of
326 * addresses in platform where the exec is compiled.
328 static ULONG_PTR dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
330 ULONG_PTR ret;
332 switch (word_size)
334 case 4:
335 ret = dwarf2_get_u4(ptr);
336 break;
337 case 8:
338 ret = dwarf2_get_u8(ptr);
339 break;
340 default:
341 FIXME("Unsupported Word Size %u\n", word_size);
342 ret = 0;
344 return ret;
347 static ULONG_PTR dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
349 ULONG_PTR ret = dwarf2_get_addr(ctx->data, ctx->word_size);
350 ctx->data += ctx->word_size;
351 return ret;
354 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
356 return wine_dbg_sprintf("ctx(%p)", ctx->data);
359 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
361 return wine_dbg_sprintf("ctx(%p,%s)",
362 ctx, debugstr_w(ctx->module->module.ModuleName));
365 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
367 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
368 di->abbrev, di->symt);
371 static dwarf2_abbrev_entry_t*
372 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
373 ULONG_PTR entry_code)
375 assert( NULL != abbrev_table );
376 return sparse_array_find(abbrev_table, entry_code);
379 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
380 struct sparse_array* abbrev_table,
381 struct pool* pool)
383 ULONG_PTR entry_code;
384 dwarf2_abbrev_entry_t* abbrev_entry;
385 dwarf2_abbrev_entry_attr_t* new = NULL;
386 dwarf2_abbrev_entry_attr_t* last = NULL;
387 ULONG_PTR attribute;
388 ULONG_PTR form;
390 assert( NULL != abbrev_ctx );
392 TRACE("%s, end at %p\n",
393 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
395 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
396 while (abbrev_ctx->data < abbrev_ctx->end_data)
398 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
399 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
400 TRACE("found entry_code %lu\n", entry_code);
401 if (!entry_code)
403 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
404 break;
406 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
407 assert( NULL != abbrev_entry );
409 abbrev_entry->entry_code = entry_code;
410 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
411 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
412 abbrev_entry->attrs = NULL;
413 abbrev_entry->num_attr = 0;
415 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
416 abbrev_table, sparse_array_length(abbrev_table),
417 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
419 last = NULL;
420 while (1)
422 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
423 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
424 if (!attribute) break;
426 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
427 assert(new);
429 new->attribute = attribute;
430 new->form = form;
431 new->next = NULL;
432 if (abbrev_entry->attrs) last->next = new;
433 else abbrev_entry->attrs = new;
434 last = new;
435 abbrev_entry->num_attr++;
438 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
441 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
442 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
444 unsigned step;
446 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
448 switch (abbrev_attr->form)
450 case DW_FORM_flag_present: step = 0; break;
451 case DW_FORM_ref_addr:
452 case DW_FORM_addr: step = ctx->word_size; break;
453 case DW_FORM_flag:
454 case DW_FORM_data1:
455 case DW_FORM_ref1: step = 1; break;
456 case DW_FORM_data2:
457 case DW_FORM_ref2: step = 2; break;
458 case DW_FORM_data4:
459 case DW_FORM_ref4:
460 case DW_FORM_strp: step = 4; break;
461 case DW_FORM_data8:
462 case DW_FORM_ref8: step = 8; break;
463 case DW_FORM_sdata:
464 case DW_FORM_ref_udata:
465 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
466 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
467 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
468 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
469 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
470 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
471 default:
472 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
473 return;
475 ctx->data += step;
478 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
479 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
480 const unsigned char* data,
481 struct attribute* attr)
483 attr->form = abbrev_attr->form;
484 switch (attr->form)
486 case DW_FORM_ref_addr:
487 case DW_FORM_addr:
488 attr->u.uvalue = dwarf2_get_addr(data,
489 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
490 TRACE("addr<0x%lx>\n", attr->u.uvalue);
491 break;
493 case DW_FORM_flag:
494 attr->u.uvalue = dwarf2_get_byte(data);
495 TRACE("flag<0x%lx>\n", attr->u.uvalue);
496 break;
498 case DW_FORM_flag_present:
499 attr->u.uvalue = 1;
500 TRACE("flag_present\n");
501 break;
503 case DW_FORM_data1:
504 attr->u.uvalue = dwarf2_get_byte(data);
505 TRACE("data1<%lu>\n", attr->u.uvalue);
506 break;
508 case DW_FORM_data2:
509 attr->u.uvalue = dwarf2_get_u2(data);
510 TRACE("data2<%lu>\n", attr->u.uvalue);
511 break;
513 case DW_FORM_data4:
514 attr->u.uvalue = dwarf2_get_u4(data);
515 TRACE("data4<%lu>\n", attr->u.uvalue);
516 break;
518 case DW_FORM_data8:
519 attr->u.lluvalue = dwarf2_get_u8(data);
520 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
521 break;
523 case DW_FORM_ref1:
524 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
525 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
526 break;
528 case DW_FORM_ref2:
529 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
530 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
531 break;
533 case DW_FORM_ref4:
534 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
535 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
536 break;
538 case DW_FORM_ref8:
539 FIXME("Unhandled 64-bit support\n");
540 break;
542 case DW_FORM_sdata:
543 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
544 break;
546 case DW_FORM_ref_udata:
547 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
548 break;
550 case DW_FORM_udata:
551 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
552 break;
554 case DW_FORM_string:
555 attr->u.string = (const char *)data;
556 TRACE("string<%s>\n", debugstr_a(attr->u.string));
557 break;
559 case DW_FORM_strp:
561 ULONG_PTR offset = dwarf2_get_u4(data);
562 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
564 TRACE("strp<%s>\n", debugstr_a(attr->u.string));
565 break;
567 case DW_FORM_block:
568 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
569 break;
571 case DW_FORM_block1:
572 attr->u.block.size = dwarf2_get_byte(data);
573 attr->u.block.ptr = data + 1;
574 break;
576 case DW_FORM_block2:
577 attr->u.block.size = dwarf2_get_u2(data);
578 attr->u.block.ptr = data + 2;
579 break;
581 case DW_FORM_block4:
582 attr->u.block.size = dwarf2_get_u4(data);
583 attr->u.block.ptr = data + 4;
584 break;
586 default:
587 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
588 break;
592 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
593 const dwarf2_debug_info_t* di,
594 unsigned at, struct attribute* attr)
596 unsigned i, refidx = 0;
597 dwarf2_abbrev_entry_attr_t* abbrev_attr;
598 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
600 attr->gotten_from = attr_direct;
601 while (di)
603 ref_abbrev_attr = NULL;
604 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
606 if (abbrev_attr->attribute == at)
608 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
609 return TRUE;
611 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
612 abbrev_attr->attribute == DW_AT_specification) &&
613 at != DW_AT_sibling)
615 if (ref_abbrev_attr)
616 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
617 ref_abbrev_attr = abbrev_attr;
618 refidx = i;
619 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
620 attr_abstract_origin : attr_specification;
623 /* do we have either an abstract origin or a specification debug entry to look into ? */
624 if (!ref_abbrev_attr) break;
625 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
626 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
627 FIXME("Should have found the debug info entry\n");
629 return FALSE;
632 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
634 #define Wine_DW_no_register 0x7FFFFFFF
636 static unsigned dwarf2_map_register(int regno, const struct module* module)
638 if (regno == Wine_DW_no_register)
640 FIXME("What the heck map reg 0x%x\n",regno);
641 return 0;
643 return dbghelp_current_cpu->map_dwarf_register(regno, module, FALSE);
646 static enum location_error
647 compute_location(const struct module *module, dwarf2_traverse_context_t* ctx, struct location* loc,
648 HANDLE hproc, const struct location* frame)
650 DWORD_PTR tmp, stack[64];
651 unsigned stk;
652 unsigned char op;
653 BOOL piece_found = FALSE;
655 stack[stk = 0] = 0;
657 loc->kind = loc_absolute;
658 loc->reg = Wine_DW_no_register;
660 while (ctx->data < ctx->end_data)
662 op = dwarf2_parse_byte(ctx);
664 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
665 stack[++stk] = op - DW_OP_lit0;
666 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
668 /* dbghelp APIs don't know how to cope with this anyway
669 * (for example 'long long' stored in two registers)
670 * FIXME: We should tell winedbg how to deal with it (sigh)
672 if (!piece_found)
674 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0, module);
675 if (loc->reg != Wine_DW_no_register)
676 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
677 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
678 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
679 loc->reg = cvreg;
681 loc->kind = loc_register;
683 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
685 /* dbghelp APIs don't know how to cope with this anyway
686 * (for example 'long long' stored in two registers)
687 * FIXME: We should tell winedbg how to deal with it (sigh)
689 if (!piece_found)
691 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0, module);
692 if (loc->reg != Wine_DW_no_register)
693 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
694 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
695 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
696 loc->reg = cvreg;
698 stack[++stk] = dwarf2_leb128_as_signed(ctx);
699 loc->kind = loc_regrel;
701 else switch (op)
703 case DW_OP_nop: break;
704 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
705 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
706 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
707 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
708 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
709 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
710 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
711 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
712 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
713 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
714 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
715 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
716 case DW_OP_drop: stk--; break;
717 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
718 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
719 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
720 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
721 case DW_OP_abs: stack[stk] = sizeof(stack[stk]) == 8 ? llabs((INT64)stack[stk]) : abs((INT32)stack[stk]); break;
722 case DW_OP_neg: stack[stk] = -stack[stk]; break;
723 case DW_OP_not: stack[stk] = ~stack[stk]; break;
724 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
725 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
726 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
727 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
728 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
729 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
730 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
731 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
732 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
733 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
734 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
735 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
736 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
737 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
738 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
739 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
740 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
741 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
742 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
743 case DW_OP_bra:
744 tmp = dwarf2_parse_u2(ctx);
745 if (!stack[stk--]) ctx->data += tmp;
746 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, module);
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, module);
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 = 0;
813 if (!ReadProcessMemory(hproc, (void*)addr, &deref, ctx->word_size, 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 ULONG_PTR 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(ctx->module, &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 = (ULONG_PTR)ptr;
933 compute_location(ctx->module, &lctx, loc, NULL, frame);
936 return TRUE;
939 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
940 const dwarf2_debug_info_t* di)
942 struct attribute attr;
943 dwarf2_debug_info_t* type;
945 if (!dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
946 return NULL;
947 if (!(type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue)))
949 FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
950 return NULL;
952 if (!type->symt)
954 /* load the debug info entity */
955 dwarf2_load_one_entry(ctx, type);
956 if (!type->symt)
957 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
959 return type->symt;
962 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
964 char* last;
965 struct attribute diname;
966 struct attribute spec;
968 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
969 if (!ctx->cpp_name)
970 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
971 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
972 strcpy(last, name);
974 /* if the di is a definition, but has also a (previous) declaration, then scope must
975 * be gotten from declaration not definition
977 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
979 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
980 if (!di)
982 FIXME("Should have found the debug info entry\n");
983 return NULL;
987 for (di = di->parent; di; di = di->parent)
989 switch (di->abbrev->tag)
991 case DW_TAG_namespace:
992 case DW_TAG_structure_type:
993 case DW_TAG_class_type:
994 case DW_TAG_interface_type:
995 case DW_TAG_union_type:
996 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
998 size_t len = strlen(diname.u.string);
999 last -= 2 + len;
1000 if (last < ctx->cpp_name) return NULL;
1001 memcpy(last, diname.u.string, len);
1002 last[len] = last[len + 1] = ':';
1004 break;
1005 default:
1006 break;
1009 return last;
1012 /******************************************************************
1013 * dwarf2_read_range
1015 * read a range for a given debug_info (either using AT_range attribute, in which
1016 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1017 * in all cases, range is relative to beginning of compilation unit
1019 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1020 ULONG_PTR* plow, ULONG_PTR* phigh)
1022 struct attribute range;
1024 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1026 dwarf2_traverse_context_t traverse;
1027 ULONG_PTR low, high;
1029 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1030 traverse.end_data = ctx->sections[section_ranges].address +
1031 ctx->sections[section_ranges].size;
1032 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1034 *plow = ULONG_MAX;
1035 *phigh = 0;
1036 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1038 low = dwarf2_parse_addr(&traverse);
1039 high = dwarf2_parse_addr(&traverse);
1040 if (low == 0 && high == 0) break;
1041 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1042 if (low < *plow) *plow = low;
1043 if (high > *phigh) *phigh = high;
1045 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1046 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1048 return TRUE;
1050 else
1052 struct attribute low_pc;
1053 struct attribute high_pc;
1055 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1056 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1057 return FALSE;
1058 *plow = low_pc.u.uvalue;
1059 *phigh = high_pc.u.uvalue;
1060 return TRUE;
1064 /******************************************************************
1065 * dwarf2_read_one_debug_info
1067 * Loads into memory one debug info entry, and recursively its children (if any)
1069 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1070 dwarf2_traverse_context_t* traverse,
1071 dwarf2_debug_info_t* parent_di,
1072 dwarf2_debug_info_t** pdi)
1074 const dwarf2_abbrev_entry_t*abbrev;
1075 ULONG_PTR entry_code;
1076 ULONG_PTR offset;
1077 dwarf2_debug_info_t* di;
1078 dwarf2_debug_info_t* child;
1079 dwarf2_debug_info_t** where;
1080 dwarf2_abbrev_entry_attr_t* attr;
1081 unsigned i;
1082 struct attribute sibling;
1084 offset = traverse->data - ctx->sections[ctx->section].address;
1085 entry_code = dwarf2_leb128_as_unsigned(traverse);
1086 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1087 if (!entry_code)
1089 *pdi = NULL;
1090 return TRUE;
1092 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1093 if (!abbrev)
1095 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1096 return FALSE;
1098 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1099 if (!di) return FALSE;
1100 di->abbrev = abbrev;
1101 di->symt = NULL;
1102 di->parent = parent_di;
1104 if (abbrev->num_attr)
1106 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1107 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1109 di->data[i] = traverse->data;
1110 dwarf2_swallow_attribute(traverse, attr);
1113 else di->data = NULL;
1114 if (abbrev->have_child)
1116 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1117 while (traverse->data < traverse->end_data)
1119 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1120 if (!child) break;
1121 where = vector_add(&di->children, &ctx->pool);
1122 if (!where) return FALSE;
1123 *where = child;
1126 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1127 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1129 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1130 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1131 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1133 *pdi = di;
1134 return TRUE;
1137 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1138 dwarf2_debug_info_t* di)
1140 struct attribute spec;
1142 while (di)
1144 if (di->abbrev->have_child)
1145 return &di->children;
1146 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1147 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1148 FIXME("Should have found the debug info entry\n");
1150 return NULL;
1153 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1154 dwarf2_debug_info_t* di)
1156 struct attribute name;
1157 struct attribute size;
1158 struct attribute encoding;
1159 enum BasicType bt;
1160 int cache_idx = -1;
1161 if (di->symt) return di->symt;
1163 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1165 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1166 name.u.string = NULL;
1167 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1168 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1170 switch (encoding.u.uvalue)
1172 case DW_ATE_void: bt = btVoid; break;
1173 case DW_ATE_address: bt = btULong; break;
1174 case DW_ATE_boolean: bt = btBool; break;
1175 case DW_ATE_complex_float: bt = btComplex; break;
1176 case DW_ATE_float: bt = btFloat; break;
1177 case DW_ATE_signed: bt = btInt; break;
1178 case DW_ATE_unsigned: bt = btUInt; break;
1179 case DW_ATE_signed_char: bt = btChar; break;
1180 case DW_ATE_unsigned_char: bt = btChar; break;
1181 default: bt = btNoType; break;
1183 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1184 switch (bt)
1186 case btVoid:
1187 assert(size.u.uvalue == 0);
1188 cache_idx = sc_void;
1189 break;
1190 case btInt:
1191 switch (size.u.uvalue)
1193 case 1: cache_idx = sc_int1; break;
1194 case 2: cache_idx = sc_int2; break;
1195 case 4: cache_idx = sc_int4; break;
1197 break;
1198 default: break;
1200 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1201 ctx->symt_cache[cache_idx] = di->symt;
1203 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1204 return di->symt;
1207 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1208 dwarf2_debug_info_t* di)
1210 struct symt* ref_type;
1211 struct attribute name;
1213 if (di->symt) return di->symt;
1215 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1217 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1218 ref_type = dwarf2_lookup_type(ctx, di);
1220 if (name.u.string)
1221 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1222 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1223 return di->symt;
1226 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1227 dwarf2_debug_info_t* di)
1229 struct symt* ref_type;
1230 struct attribute size;
1232 if (di->symt) return di->symt;
1234 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1236 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1237 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1239 ref_type = ctx->symt_cache[sc_void];
1240 assert(ref_type);
1242 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1243 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1244 return di->symt;
1247 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1248 dwarf2_debug_info_t* di)
1250 struct symt* ref_type;
1251 struct symt* idx_type = NULL;
1252 struct attribute min, max, cnt;
1253 dwarf2_debug_info_t* child;
1254 unsigned int i;
1255 const struct vector* children;
1257 if (di->symt) return di->symt;
1259 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1261 ref_type = dwarf2_lookup_type(ctx, di);
1263 if (!(children = dwarf2_get_di_children(ctx, di)))
1265 /* fake an array with unknown size */
1266 /* FIXME: int4 even on 64bit machines??? */
1267 idx_type = ctx->symt_cache[sc_int4];
1268 min.u.uvalue = 0;
1269 max.u.uvalue = -1;
1271 else for (i = 0; i < vector_length(children); i++)
1273 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1274 switch (child->abbrev->tag)
1276 case DW_TAG_subrange_type:
1277 idx_type = dwarf2_lookup_type(ctx, child);
1278 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1279 min.u.uvalue = 0;
1280 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1281 max.u.uvalue = 0;
1282 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1283 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1284 break;
1285 default:
1286 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1287 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1288 break;
1291 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1292 return di->symt;
1295 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1296 dwarf2_debug_info_t* di)
1298 struct symt* ref_type;
1300 if (di->symt) return di->symt;
1302 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1304 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1306 ref_type = ctx->symt_cache[sc_void];
1307 assert(ref_type);
1309 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1310 di->symt = ref_type;
1312 return ref_type;
1315 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1316 dwarf2_debug_info_t* di)
1318 struct symt* ref_type;
1320 if (di->symt) return di->symt;
1322 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1324 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1326 ref_type = ctx->symt_cache[sc_void];
1327 assert(ref_type);
1329 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1330 di->symt = ref_type;
1332 return ref_type;
1335 static struct symt* dwarf2_parse_unspecified_type(dwarf2_parse_context_t* ctx,
1336 dwarf2_debug_info_t* di)
1338 struct attribute name;
1339 struct attribute size;
1340 struct symt_basic *basic;
1342 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1344 if (di->symt) return di->symt;
1346 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1347 name.u.string = "void";
1348 size.u.uvalue = sizeof(void *);
1350 basic = symt_new_basic(ctx->module, btVoid, name.u.string, size.u.uvalue);
1351 di->symt = &basic->symt;
1353 if (!ctx->symt_cache[sc_void])
1354 ctx->symt_cache[sc_void] = di->symt;
1356 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1357 return di->symt;
1360 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1361 dwarf2_debug_info_t* di)
1363 struct symt* ref_type = NULL;
1365 if (di->symt) return di->symt;
1367 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1369 ref_type = dwarf2_lookup_type(ctx, di);
1370 /* FIXME: for now, we hard-wire C++ references to pointers */
1371 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1373 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1375 return di->symt;
1378 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1379 dwarf2_debug_info_t* di,
1380 struct symt_udt* parent)
1382 struct symt* elt_type;
1383 struct attribute name;
1384 struct attribute bit_size;
1385 struct attribute bit_offset;
1386 struct location loc;
1388 assert(parent);
1390 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1392 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1393 elt_type = dwarf2_lookup_type(ctx, di);
1394 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1396 if (loc.kind != loc_absolute)
1398 FIXME("Found register, while not expecting it\n");
1399 loc.offset = 0;
1401 else
1402 TRACE("found member_location at %s -> %lu\n",
1403 dwarf2_debug_ctx(ctx), loc.offset);
1405 else
1406 loc.offset = 0;
1407 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1408 bit_size.u.uvalue = 0;
1409 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1411 /* FIXME: we should only do this when implementation is LSB (which is
1412 * the case on i386 processors)
1414 struct attribute nbytes;
1415 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1417 DWORD64 size;
1418 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1419 (ULONG_PTR)size : 0;
1421 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1423 else bit_offset.u.uvalue = 0;
1424 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1425 (loc.offset << 3) + bit_offset.u.uvalue,
1426 bit_size.u.uvalue);
1428 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1431 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1432 dwarf2_debug_info_t* di);
1434 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1435 dwarf2_debug_info_t* di,
1436 enum UdtKind udt)
1438 struct attribute name;
1439 struct attribute size;
1440 struct vector* children;
1441 dwarf2_debug_info_t*child;
1442 unsigned int i;
1444 if (di->symt) return di->symt;
1446 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1448 /* quirk... FIXME provide real support for anonymous UDTs */
1449 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1450 name.u.string = "zz_anon_zz";
1451 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1453 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1454 size.u.uvalue, udt)->symt;
1456 children = dwarf2_get_di_children(ctx, di);
1457 if (children) for (i = 0; i < vector_length(children); i++)
1459 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1461 switch (child->abbrev->tag)
1463 case DW_TAG_array_type:
1464 dwarf2_parse_array_type(ctx, di);
1465 break;
1466 case DW_TAG_member:
1467 /* FIXME: should I follow the sibling stuff ?? */
1468 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1469 break;
1470 case DW_TAG_enumeration_type:
1471 dwarf2_parse_enumeration_type(ctx, child);
1472 break;
1473 case DW_TAG_subprogram:
1474 dwarf2_parse_subprogram(ctx, child);
1475 break;
1476 case DW_TAG_const_type:
1477 dwarf2_parse_const_type(ctx, child);
1478 break;
1479 case DW_TAG_structure_type:
1480 case DW_TAG_class_type:
1481 case DW_TAG_union_type:
1482 case DW_TAG_typedef:
1483 /* FIXME: we need to handle nested udt definitions */
1484 case DW_TAG_inheritance:
1485 case DW_TAG_template_type_param:
1486 case DW_TAG_template_value_param:
1487 case DW_TAG_variable:
1488 case DW_TAG_imported_declaration:
1489 case DW_TAG_ptr_to_member_type:
1490 case DW_TAG_GNU_template_parameter_pack:
1491 case DW_TAG_GNU_formal_parameter_pack:
1492 /* FIXME: some C++ related stuff */
1493 break;
1494 default:
1495 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1496 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1497 break;
1501 return di->symt;
1504 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1505 dwarf2_debug_info_t* di,
1506 struct symt_enum* parent)
1508 struct attribute name;
1509 struct attribute value;
1511 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1513 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1514 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1515 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1517 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1520 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1521 dwarf2_debug_info_t* di)
1523 struct attribute name;
1524 struct attribute size;
1525 struct symt_basic* basetype;
1526 struct vector* children;
1527 dwarf2_debug_info_t*child;
1528 unsigned int i;
1530 if (di->symt) return di->symt;
1532 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1534 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1535 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1537 switch (size.u.uvalue) /* FIXME: that's wrong */
1539 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1540 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1541 default:
1542 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1545 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1547 children = dwarf2_get_di_children(ctx, di);
1548 /* FIXME: should we use the sibling stuff ?? */
1549 if (children) for (i = 0; i < vector_length(children); i++)
1551 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1553 switch (child->abbrev->tag)
1555 case DW_TAG_enumerator:
1556 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1557 break;
1558 default:
1559 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1560 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1563 return di->symt;
1566 /* structure used to pass information around when parsing a subprogram */
1567 typedef struct dwarf2_subprogram_s
1569 dwarf2_parse_context_t* ctx;
1570 struct symt_function* func;
1571 BOOL non_computed_variable;
1572 struct location frame;
1573 } dwarf2_subprogram_t;
1575 /******************************************************************
1576 * dwarf2_parse_variable
1578 * Parses any variable (parameter, local/global variable)
1580 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1581 struct symt_block* block,
1582 dwarf2_debug_info_t* di)
1584 struct symt* param_type;
1585 struct attribute name, value;
1586 struct location loc;
1587 BOOL is_pmt;
1589 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1591 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1592 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1594 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1595 /* cannot do much without the name, the functions below won't like it. */
1596 return;
1598 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1599 &loc, &subpgm->frame))
1601 struct attribute ext;
1603 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1604 debugstr_a(name.u.string), loc.kind, loc.offset, loc.reg,
1605 dwarf2_debug_ctx(subpgm->ctx));
1607 switch (loc.kind)
1609 case loc_error:
1610 break;
1611 case loc_absolute:
1612 /* it's a global variable */
1613 /* FIXME: we don't handle its scope yet */
1614 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1615 ext.u.uvalue = 0;
1616 loc.offset += subpgm->ctx->load_offset;
1617 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1618 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1619 loc, 0, param_type);
1620 break;
1621 default:
1622 subpgm->non_computed_variable = TRUE;
1623 /* fall through */
1624 case loc_register:
1625 case loc_regrel:
1626 /* either a pmt/variable relative to frame pointer or
1627 * pmt/variable in a register
1629 assert(subpgm->func);
1630 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1631 is_pmt ? DataIsParam : DataIsLocal,
1632 &loc, block, param_type, name.u.string);
1633 break;
1636 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1638 VARIANT v;
1639 if (subpgm->func) WARN("Unsupported constant %s in function\n", debugstr_a(name.u.string));
1640 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", debugstr_a(name.u.string));
1641 switch (value.form)
1643 case DW_FORM_data1:
1644 case DW_FORM_data2:
1645 case DW_FORM_data4:
1646 case DW_FORM_udata:
1647 case DW_FORM_addr:
1648 v.n1.n2.vt = VT_UI4;
1649 v.n1.n2.n3.lVal = value.u.uvalue;
1650 break;
1652 case DW_FORM_data8:
1653 v.n1.n2.vt = VT_UI8;
1654 v.n1.n2.n3.llVal = value.u.lluvalue;
1655 break;
1657 case DW_FORM_sdata:
1658 v.n1.n2.vt = VT_I4;
1659 v.n1.n2.n3.lVal = value.u.svalue;
1660 break;
1662 case DW_FORM_strp:
1663 case DW_FORM_string:
1664 /* FIXME: native doesn't report const strings from here !!
1665 * however, the value of the string is in the code somewhere
1667 v.n1.n2.vt = VT_I1 | VT_BYREF;
1668 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1669 break;
1671 case DW_FORM_block:
1672 case DW_FORM_block1:
1673 case DW_FORM_block2:
1674 case DW_FORM_block4:
1675 v.n1.n2.vt = VT_I4;
1676 switch (value.u.block.size)
1678 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1679 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1680 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1681 default:
1682 v.n1.n2.vt = VT_I1 | VT_BYREF;
1683 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1684 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1686 break;
1688 default:
1689 FIXME("Unsupported form for const value %s (%lx)\n",
1690 debugstr_a(name.u.string), value.form);
1691 v.n1.n2.vt = VT_EMPTY;
1693 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1694 name.u.string, param_type, &v)->symt;
1696 else
1698 /* variable has been optimized away... report anyway */
1699 loc.kind = loc_error;
1700 loc.reg = loc_err_no_location;
1701 if (subpgm->func)
1703 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1704 is_pmt ? DataIsParam : DataIsLocal,
1705 &loc, block, param_type, name.u.string);
1707 else
1709 WARN("dropping global variable %s which has been optimized away\n", debugstr_a(name.u.string));
1712 if (is_pmt && subpgm->func && subpgm->func->type)
1713 symt_add_function_signature_parameter(subpgm->ctx->module,
1714 (struct symt_function_signature*)subpgm->func->type,
1715 param_type);
1717 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1720 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1721 const dwarf2_debug_info_t* di)
1723 struct attribute name;
1724 struct attribute low_pc;
1725 struct location loc;
1727 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1729 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1730 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1731 name.u.string = NULL;
1733 loc.kind = loc_absolute;
1734 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1735 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1736 &loc, name.u.string);
1739 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1740 struct symt_block* parent_block,
1741 dwarf2_debug_info_t* di);
1743 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1744 dwarf2_debug_info_t* di);
1746 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1747 struct symt_block* parent_block,
1748 dwarf2_debug_info_t* di)
1750 struct symt_block* block;
1751 ULONG_PTR low_pc, high_pc;
1752 struct vector* children;
1753 dwarf2_debug_info_t*child;
1754 unsigned int i;
1756 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1758 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1760 FIXME("cannot read range\n");
1761 return;
1764 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1765 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1766 high_pc - low_pc);
1768 children = dwarf2_get_di_children(subpgm->ctx, di);
1769 if (children) for (i = 0; i < vector_length(children); i++)
1771 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1773 switch (child->abbrev->tag)
1775 case DW_TAG_formal_parameter:
1776 case DW_TAG_variable:
1777 dwarf2_parse_variable(subpgm, block, child);
1778 break;
1779 case DW_TAG_lexical_block:
1780 dwarf2_parse_subprogram_block(subpgm, block, child);
1781 break;
1782 case DW_TAG_inlined_subroutine:
1783 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1784 break;
1785 case DW_TAG_label:
1786 dwarf2_parse_subprogram_label(subpgm, child);
1787 break;
1788 case DW_TAG_GNU_call_site:
1789 /* this isn't properly supported by dbghelp interface. skip it for now */
1790 break;
1791 default:
1792 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1793 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1794 dwarf2_debug_di(di));
1797 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1800 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1801 struct symt_block* parent_block,
1802 dwarf2_debug_info_t* di)
1804 struct symt_block* block;
1805 ULONG_PTR low_pc, high_pc;
1806 struct vector* children;
1807 dwarf2_debug_info_t*child;
1808 unsigned int i;
1810 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1812 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1814 WARN("no range\n");
1815 return;
1818 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1819 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1820 high_pc - low_pc);
1822 children = dwarf2_get_di_children(subpgm->ctx, di);
1823 if (children) for (i = 0; i < vector_length(children); i++)
1825 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1827 switch (child->abbrev->tag)
1829 case DW_TAG_inlined_subroutine:
1830 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1831 break;
1832 case DW_TAG_variable:
1833 dwarf2_parse_variable(subpgm, block, child);
1834 break;
1835 case DW_TAG_pointer_type:
1836 dwarf2_parse_pointer_type(subpgm->ctx, di);
1837 break;
1838 case DW_TAG_subroutine_type:
1839 dwarf2_parse_subroutine_type(subpgm->ctx, di);
1840 break;
1841 case DW_TAG_const_type:
1842 dwarf2_parse_const_type(subpgm->ctx, di);
1843 break;
1844 case DW_TAG_lexical_block:
1845 dwarf2_parse_subprogram_block(subpgm, block, child);
1846 break;
1847 case DW_TAG_subprogram:
1848 /* FIXME: likely a declaration (to be checked)
1849 * skip it for now
1851 break;
1852 case DW_TAG_formal_parameter:
1853 /* FIXME: likely elements for exception handling (GCC flavor)
1854 * Skip it for now
1856 break;
1857 case DW_TAG_imported_module:
1858 /* C++ stuff to be silenced (for now) */
1859 break;
1860 case DW_TAG_GNU_call_site:
1861 /* this isn't properly supported by dbghelp interface. skip it for now */
1862 break;
1863 case DW_TAG_label:
1864 dwarf2_parse_subprogram_label(subpgm, child);
1865 break;
1866 case DW_TAG_class_type:
1867 case DW_TAG_structure_type:
1868 case DW_TAG_union_type:
1869 case DW_TAG_enumeration_type:
1870 case DW_TAG_typedef:
1871 /* the type referred to will be loaded when we need it, so skip it */
1872 break;
1873 default:
1874 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1875 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1879 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1882 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1883 dwarf2_debug_info_t* di)
1885 struct attribute name;
1886 ULONG_PTR low_pc, high_pc;
1887 struct attribute is_decl;
1888 struct attribute inline_flags;
1889 struct symt* ret_type;
1890 struct symt_function_signature* sig_type;
1891 dwarf2_subprogram_t subpgm;
1892 struct vector* children;
1893 dwarf2_debug_info_t* child;
1894 unsigned int i;
1896 if (di->symt) return di->symt;
1898 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1900 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1902 WARN("No name for function... dropping function\n");
1903 return NULL;
1905 /* if it's an abstract representation of an inline function, there should be
1906 * a concrete object that we'll handle
1908 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1909 inline_flags.u.uvalue != DW_INL_not_inlined)
1911 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1912 debugstr_a(name.u.string), inline_flags.u.uvalue);
1913 return NULL;
1916 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1917 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1919 /* it's a real declaration, skip it */
1920 return NULL;
1922 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1924 WARN("cannot get range for %s\n", debugstr_a(name.u.string));
1925 return NULL;
1927 /* As functions (defined as inline assembly) get debug info with dwarf
1928 * (not the case for stabs), we just drop Wine's thunks here...
1929 * Actual thunks will be created in elf_module from the symbol table
1931 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1932 return NULL;
1933 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1935 ret_type = ctx->symt_cache[sc_void];
1936 assert(ret_type);
1938 /* FIXME: assuming C source code */
1939 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1940 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1941 dwarf2_get_cpp_name(ctx, di, name.u.string),
1942 ctx->load_offset + low_pc, high_pc - low_pc,
1943 &sig_type->symt);
1944 di->symt = &subpgm.func->symt;
1945 subpgm.ctx = ctx;
1946 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1947 &subpgm.frame, NULL))
1949 /* on stack !! */
1950 subpgm.frame.kind = loc_regrel;
1951 subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
1952 subpgm.frame.offset = 0;
1954 subpgm.non_computed_variable = FALSE;
1956 children = dwarf2_get_di_children(ctx, di);
1957 if (children) for (i = 0; i < vector_length(children); i++)
1959 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1961 switch (child->abbrev->tag)
1963 case DW_TAG_variable:
1964 case DW_TAG_formal_parameter:
1965 dwarf2_parse_variable(&subpgm, NULL, child);
1966 break;
1967 case DW_TAG_lexical_block:
1968 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1969 break;
1970 case DW_TAG_inlined_subroutine:
1971 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1972 break;
1973 case DW_TAG_pointer_type:
1974 dwarf2_parse_pointer_type(subpgm.ctx, di);
1975 break;
1976 case DW_TAG_const_type:
1977 dwarf2_parse_const_type(subpgm.ctx, di);
1978 break;
1979 case DW_TAG_subprogram:
1980 /* FIXME: likely a declaration (to be checked)
1981 * skip it for now
1983 break;
1984 case DW_TAG_label:
1985 dwarf2_parse_subprogram_label(&subpgm, child);
1986 break;
1987 case DW_TAG_class_type:
1988 case DW_TAG_structure_type:
1989 case DW_TAG_union_type:
1990 case DW_TAG_enumeration_type:
1991 case DW_TAG_typedef:
1992 /* the type referred to will be loaded when we need it, so skip it */
1993 break;
1994 case DW_TAG_unspecified_parameters:
1995 case DW_TAG_template_type_param:
1996 case DW_TAG_template_value_param:
1997 case DW_TAG_GNU_call_site:
1998 case DW_TAG_GNU_template_parameter_pack:
1999 case DW_TAG_GNU_formal_parameter_pack:
2000 /* FIXME: no support in dbghelp's internals so far */
2001 break;
2002 default:
2003 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
2004 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2008 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2010 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
2011 &subpgm.frame, NULL);
2013 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
2015 return di->symt;
2018 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
2019 dwarf2_debug_info_t* di)
2021 struct symt* ret_type;
2022 struct symt_function_signature* sig_type;
2023 struct vector* children;
2024 dwarf2_debug_info_t* child;
2025 unsigned int i;
2027 if (di->symt) return di->symt;
2029 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2031 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
2033 ret_type = ctx->symt_cache[sc_void];
2034 assert(ret_type);
2037 /* FIXME: assuming C source code */
2038 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
2040 children = dwarf2_get_di_children(ctx, di);
2041 if (children) for (i = 0; i < vector_length(children); i++)
2043 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2045 switch (child->abbrev->tag)
2047 case DW_TAG_formal_parameter:
2048 symt_add_function_signature_parameter(ctx->module, sig_type,
2049 dwarf2_lookup_type(ctx, child));
2050 break;
2051 case DW_TAG_unspecified_parameters:
2052 WARN("Unsupported unspecified parameters\n");
2053 break;
2057 return di->symt = &sig_type->symt;
2060 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
2061 dwarf2_debug_info_t* di)
2063 struct vector* children;
2064 dwarf2_debug_info_t* child;
2065 unsigned int i;
2067 if (di->symt) return;
2069 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2071 di->symt = ctx->symt_cache[sc_void];
2073 children = dwarf2_get_di_children(ctx, di);
2074 if (children) for (i = 0; i < vector_length(children); i++)
2076 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2077 dwarf2_load_one_entry(ctx, child);
2081 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2082 dwarf2_debug_info_t* di)
2084 switch (di->abbrev->tag)
2086 case DW_TAG_typedef:
2087 dwarf2_parse_typedef(ctx, di);
2088 break;
2089 case DW_TAG_base_type:
2090 dwarf2_parse_base_type(ctx, di);
2091 break;
2092 case DW_TAG_pointer_type:
2093 dwarf2_parse_pointer_type(ctx, di);
2094 break;
2095 case DW_TAG_class_type:
2096 dwarf2_parse_udt_type(ctx, di, UdtClass);
2097 break;
2098 case DW_TAG_structure_type:
2099 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2100 break;
2101 case DW_TAG_union_type:
2102 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2103 break;
2104 case DW_TAG_array_type:
2105 dwarf2_parse_array_type(ctx, di);
2106 break;
2107 case DW_TAG_const_type:
2108 dwarf2_parse_const_type(ctx, di);
2109 break;
2110 case DW_TAG_volatile_type:
2111 dwarf2_parse_volatile_type(ctx, di);
2112 break;
2113 case DW_TAG_unspecified_type:
2114 dwarf2_parse_unspecified_type(ctx, di);
2115 break;
2116 case DW_TAG_reference_type:
2117 dwarf2_parse_reference_type(ctx, di);
2118 break;
2119 case DW_TAG_enumeration_type:
2120 dwarf2_parse_enumeration_type(ctx, di);
2121 break;
2122 case DW_TAG_subprogram:
2123 dwarf2_parse_subprogram(ctx, di);
2124 break;
2125 case DW_TAG_subroutine_type:
2126 dwarf2_parse_subroutine_type(ctx, di);
2127 break;
2128 case DW_TAG_variable:
2130 dwarf2_subprogram_t subpgm;
2132 subpgm.ctx = ctx;
2133 subpgm.func = NULL;
2134 subpgm.frame.kind = loc_absolute;
2135 subpgm.frame.offset = 0;
2136 subpgm.frame.reg = Wine_DW_no_register;
2137 dwarf2_parse_variable(&subpgm, NULL, di);
2139 break;
2140 case DW_TAG_namespace:
2141 dwarf2_parse_namespace(ctx, di);
2142 break;
2143 /* silence a couple of C++ defines */
2144 case DW_TAG_imported_module:
2145 case DW_TAG_imported_declaration:
2146 case DW_TAG_ptr_to_member_type:
2147 break;
2148 default:
2149 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2150 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2154 static void dwarf2_set_line_number(struct module* module, ULONG_PTR address,
2155 const struct vector* v, unsigned file, unsigned line)
2157 struct symt_function* func;
2158 struct symt_ht* symt;
2159 unsigned* psrc;
2161 if (!file || !(psrc = vector_at(v, file - 1))) return;
2163 TRACE("%s %lx %s %u\n",
2164 debugstr_w(module->module.ModuleName), address, debugstr_a(source_get(module, *psrc)), line);
2165 if (!(symt = symt_find_nearest(module, address)) ||
2166 symt->symt.tag != SymTagFunction) return;
2167 func = (struct symt_function*)symt;
2168 symt_add_func_line(module, func, *psrc, line, address - func->address);
2171 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2172 dwarf2_parse_context_t* ctx,
2173 const char* compile_dir,
2174 ULONG_PTR offset)
2176 dwarf2_traverse_context_t traverse;
2177 ULONG_PTR length;
2178 unsigned insn_size, default_stmt;
2179 unsigned line_range, opcode_base;
2180 int line_base;
2181 const unsigned char* opcode_len;
2182 struct vector dirs;
2183 struct vector files;
2184 const char** p;
2186 /* section with line numbers stripped */
2187 if (sections[section_line].address == IMAGE_NO_MAP)
2188 return FALSE;
2190 if (offset + 4 > sections[section_line].size)
2192 WARN("out of bounds offset\n");
2193 return FALSE;
2195 traverse.data = sections[section_line].address + offset;
2196 traverse.end_data = traverse.data + 4;
2197 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2199 length = dwarf2_parse_u4(&traverse);
2200 traverse.end_data = sections[section_line].address + offset + length;
2202 if (offset + 4 + length > sections[section_line].size)
2204 WARN("out of bounds header\n");
2205 return FALSE;
2207 dwarf2_parse_u2(&traverse); /* version */
2208 dwarf2_parse_u4(&traverse); /* header_len */
2209 insn_size = dwarf2_parse_byte(&traverse);
2210 default_stmt = dwarf2_parse_byte(&traverse);
2211 line_base = (signed char)dwarf2_parse_byte(&traverse);
2212 line_range = dwarf2_parse_byte(&traverse);
2213 opcode_base = dwarf2_parse_byte(&traverse);
2215 opcode_len = traverse.data;
2216 traverse.data += opcode_base - 1;
2218 vector_init(&dirs, sizeof(const char*), 4);
2219 p = vector_add(&dirs, &ctx->pool);
2220 *p = compile_dir ? compile_dir : ".";
2221 while (*traverse.data)
2223 const char* rel = (const char*)traverse.data;
2224 unsigned rellen = strlen(rel);
2225 TRACE("Got include %s\n", debugstr_a(rel));
2226 traverse.data += rellen + 1;
2227 p = vector_add(&dirs, &ctx->pool);
2229 if (*rel == '/' || !compile_dir)
2230 *p = rel;
2231 else
2233 /* include directory relative to compile directory */
2234 unsigned baselen = strlen(compile_dir);
2235 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2236 strcpy(tmp, compile_dir);
2237 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2238 strcpy(&tmp[baselen], rel);
2239 *p = tmp;
2243 traverse.data++;
2245 vector_init(&files, sizeof(unsigned), 16);
2246 while (*traverse.data)
2248 unsigned int dir_index, mod_time;
2249 const char* name;
2250 const char* dir;
2251 unsigned* psrc;
2253 name = (const char*)traverse.data;
2254 traverse.data += strlen(name) + 1;
2255 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2256 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2257 length = dwarf2_leb128_as_unsigned(&traverse);
2258 dir = *(const char**)vector_at(&dirs, dir_index);
2259 TRACE("Got file %s/%s (%u,%lu)\n", debugstr_a(dir), debugstr_a(name), mod_time, length);
2260 psrc = vector_add(&files, &ctx->pool);
2261 *psrc = source_new(ctx->module, dir, name);
2263 traverse.data++;
2265 while (traverse.data < traverse.end_data)
2267 ULONG_PTR address = 0;
2268 unsigned file = 1;
2269 unsigned line = 1;
2270 unsigned is_stmt = default_stmt;
2271 BOOL end_sequence = FALSE;
2272 unsigned opcode, extopcode, i;
2274 while (!end_sequence)
2276 opcode = dwarf2_parse_byte(&traverse);
2277 TRACE("Got opcode %x\n", opcode);
2279 if (opcode >= opcode_base)
2281 unsigned delta = opcode - opcode_base;
2283 address += (delta / line_range) * insn_size;
2284 line += line_base + (delta % line_range);
2285 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2287 else
2289 switch (opcode)
2291 case DW_LNS_copy:
2292 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2293 break;
2294 case DW_LNS_advance_pc:
2295 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2296 break;
2297 case DW_LNS_advance_line:
2298 line += dwarf2_leb128_as_signed(&traverse);
2299 break;
2300 case DW_LNS_set_file:
2301 file = dwarf2_leb128_as_unsigned(&traverse);
2302 break;
2303 case DW_LNS_set_column:
2304 dwarf2_leb128_as_unsigned(&traverse);
2305 break;
2306 case DW_LNS_negate_stmt:
2307 is_stmt = !is_stmt;
2308 break;
2309 case DW_LNS_set_basic_block:
2310 break;
2311 case DW_LNS_const_add_pc:
2312 address += ((255 - opcode_base) / line_range) * insn_size;
2313 break;
2314 case DW_LNS_fixed_advance_pc:
2315 address += dwarf2_parse_u2(&traverse);
2316 break;
2317 case DW_LNS_extended_op:
2318 dwarf2_leb128_as_unsigned(&traverse);
2319 extopcode = dwarf2_parse_byte(&traverse);
2320 switch (extopcode)
2322 case DW_LNE_end_sequence:
2323 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2324 end_sequence = TRUE;
2325 break;
2326 case DW_LNE_set_address:
2327 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2328 break;
2329 case DW_LNE_define_file:
2330 FIXME("not handled define file %s\n", debugstr_a((char *)traverse.data));
2331 traverse.data += strlen((const char *)traverse.data) + 1;
2332 dwarf2_leb128_as_unsigned(&traverse);
2333 dwarf2_leb128_as_unsigned(&traverse);
2334 dwarf2_leb128_as_unsigned(&traverse);
2335 break;
2336 case DW_LNE_set_discriminator:
2338 unsigned descr;
2340 descr = dwarf2_leb128_as_unsigned(&traverse);
2341 WARN("not handled discriminator %x\n", descr);
2343 break;
2344 default:
2345 FIXME("Unsupported extended opcode %x\n", extopcode);
2346 break;
2348 break;
2349 default:
2350 WARN("Unsupported opcode %x\n", opcode);
2351 for (i = 0; i < opcode_len[opcode]; i++)
2352 dwarf2_leb128_as_unsigned(&traverse);
2353 break;
2358 return TRUE;
2361 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2362 struct module* module,
2363 const struct elf_thunk_area* thunks,
2364 dwarf2_traverse_context_t* mod_ctx,
2365 ULONG_PTR load_offset)
2367 dwarf2_parse_context_t ctx;
2368 dwarf2_traverse_context_t abbrev_ctx;
2369 dwarf2_debug_info_t* di;
2370 dwarf2_traverse_context_t cu_ctx;
2371 const unsigned char* comp_unit_start = mod_ctx->data;
2372 ULONG_PTR cu_length;
2373 unsigned short cu_version;
2374 ULONG_PTR cu_abbrev_offset;
2375 BOOL ret = FALSE;
2377 cu_length = dwarf2_parse_u4(mod_ctx);
2378 cu_ctx.data = mod_ctx->data;
2379 cu_ctx.end_data = mod_ctx->data + cu_length;
2380 mod_ctx->data += cu_length;
2381 cu_version = dwarf2_parse_u2(&cu_ctx);
2382 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2383 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2385 TRACE("Compilation Unit Header found at 0x%x:\n",
2386 (int)(comp_unit_start - sections[section_debug].address));
2387 TRACE("- length: %lu\n", cu_length);
2388 TRACE("- version: %u\n", cu_version);
2389 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2390 TRACE("- word_size: %u\n", cu_ctx.word_size);
2392 if (cu_version != 2)
2394 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2395 cu_version);
2396 return FALSE;
2399 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2400 mod_ctx->word_size = cu_ctx.word_size;
2402 pool_init(&ctx.pool, 65536);
2403 ctx.sections = sections;
2404 ctx.section = section_debug;
2405 ctx.module = module;
2406 ctx.thunks = thunks;
2407 ctx.load_offset = load_offset;
2408 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2409 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2410 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2411 ctx.cpp_name = NULL;
2413 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2414 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2415 abbrev_ctx.word_size = cu_ctx.word_size;
2416 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2418 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2419 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2421 if (di->abbrev->tag == DW_TAG_compile_unit)
2423 struct attribute name;
2424 struct vector* children;
2425 dwarf2_debug_info_t* child = NULL;
2426 unsigned int i;
2427 struct attribute stmt_list, low_pc;
2428 struct attribute comp_dir;
2430 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2431 name.u.string = NULL;
2433 /* get working directory of current compilation unit */
2434 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2435 comp_dir.u.string = NULL;
2437 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2438 low_pc.u.uvalue = 0;
2439 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2440 source_new(module, comp_dir.u.string, name.u.string));
2441 di->symt = &ctx.compiland->symt;
2442 children = dwarf2_get_di_children(&ctx, di);
2443 if (children) for (i = 0; i < vector_length(children); i++)
2445 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2446 dwarf2_load_one_entry(&ctx, child);
2448 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2450 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2451 module->module.LineNumbers = TRUE;
2453 ret = TRUE;
2455 else FIXME("Should have a compilation unit here\n");
2456 pool_destroy(&ctx.pool);
2457 return ret;
2460 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2461 ULONG_PTR ip, dwarf2_traverse_context_t* lctx)
2463 DWORD_PTR beg, end;
2464 const BYTE* ptr = start;
2465 DWORD len;
2467 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2469 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2470 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2471 if (!beg && !end) break;
2472 len = dwarf2_get_u2(ptr); ptr += 2;
2474 if (beg <= ip && ip < end)
2476 lctx->data = ptr;
2477 lctx->end_data = ptr + len;
2478 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2479 return TRUE;
2481 ptr += len;
2483 WARN("Couldn't find ip in location list\n");
2484 return FALSE;
2487 static enum location_error loc_compute_frame(struct process* pcs,
2488 const struct module_format* modfmt,
2489 const struct symt_function* func,
2490 DWORD_PTR ip, struct location* frame)
2492 struct symt** psym = NULL;
2493 struct location* pframe;
2494 dwarf2_traverse_context_t lctx;
2495 enum location_error err;
2496 unsigned int i;
2498 for (i=0; i<vector_length(&func->vchildren); i++)
2500 psym = vector_at(&func->vchildren, i);
2501 if ((*psym)->tag == SymTagCustom)
2503 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2505 /* First, recompute the frame information, if needed */
2506 switch (pframe->kind)
2508 case loc_regrel:
2509 case loc_register:
2510 *frame = *pframe;
2511 break;
2512 case loc_dwarf2_location_list:
2513 WARN("Searching loclist for %s\n", debugstr_a(func->hash_elt.name));
2514 if (!dwarf2_lookup_loclist(modfmt,
2515 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2516 ip, &lctx))
2517 return loc_err_out_of_scope;
2518 if ((err = compute_location(modfmt->module, &lctx, frame, pcs->handle, NULL)) < 0) return err;
2519 if (frame->kind >= loc_user)
2521 WARN("Couldn't compute runtime frame location\n");
2522 return loc_err_too_complex;
2524 break;
2525 default:
2526 WARN("Unsupported frame kind %d\n", pframe->kind);
2527 return loc_err_internal;
2529 return 0;
2532 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2533 return loc_err_internal;
2536 enum reg_rule
2538 RULE_UNSET, /* not set at all */
2539 RULE_UNDEFINED, /* undefined value */
2540 RULE_SAME, /* same value as previous frame */
2541 RULE_CFA_OFFSET, /* stored at cfa offset */
2542 RULE_OTHER_REG, /* stored in other register */
2543 RULE_EXPRESSION, /* address specified by expression */
2544 RULE_VAL_EXPRESSION /* value specified by expression */
2547 /* make it large enough for all CPUs */
2548 #define NB_FRAME_REGS 64
2549 #define MAX_SAVED_STATES 16
2551 struct frame_state
2553 ULONG_PTR cfa_offset;
2554 unsigned char cfa_reg;
2555 enum reg_rule cfa_rule;
2556 enum reg_rule rules[NB_FRAME_REGS];
2557 ULONG_PTR regs[NB_FRAME_REGS];
2560 struct frame_info
2562 ULONG_PTR ip;
2563 ULONG_PTR code_align;
2564 LONG_PTR data_align;
2565 unsigned char retaddr_reg;
2566 unsigned char fde_encoding;
2567 unsigned char lsda_encoding;
2568 unsigned char signal_frame;
2569 unsigned char aug_z_format;
2570 unsigned char state_sp;
2571 struct frame_state state;
2572 struct frame_state state_stack[MAX_SAVED_STATES];
2575 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2577 ULONG_PTR base;
2579 if (encoding == DW_EH_PE_omit) return 0;
2581 switch (encoding & 0xf0)
2583 case DW_EH_PE_abs:
2584 base = 0;
2585 break;
2586 case DW_EH_PE_pcrel:
2587 base = (ULONG_PTR)ctx->data;
2588 break;
2589 default:
2590 FIXME("unsupported encoding %02x\n", encoding);
2591 return 0;
2594 switch (encoding & 0x0f)
2596 case DW_EH_PE_native:
2597 return base + dwarf2_parse_addr(ctx);
2598 case DW_EH_PE_leb128:
2599 return base + dwarf2_leb128_as_unsigned(ctx);
2600 case DW_EH_PE_data2:
2601 return base + dwarf2_parse_u2(ctx);
2602 case DW_EH_PE_data4:
2603 return base + dwarf2_parse_u4(ctx);
2604 case DW_EH_PE_data8:
2605 return base + dwarf2_parse_u8(ctx);
2606 case DW_EH_PE_signed|DW_EH_PE_leb128:
2607 return base + dwarf2_leb128_as_signed(ctx);
2608 case DW_EH_PE_signed|DW_EH_PE_data2:
2609 return base + (signed short)dwarf2_parse_u2(ctx);
2610 case DW_EH_PE_signed|DW_EH_PE_data4:
2611 return base + (signed int)dwarf2_parse_u4(ctx);
2612 case DW_EH_PE_signed|DW_EH_PE_data8:
2613 return base + (LONG64)dwarf2_parse_u8(ctx);
2614 default:
2615 FIXME("unsupported encoding %02x\n", encoding);
2616 return 0;
2620 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2622 unsigned char version;
2623 const char* augmentation;
2624 const unsigned char* end;
2625 ULONG_PTR len;
2627 memset(info, 0, sizeof(*info));
2628 info->lsda_encoding = DW_EH_PE_omit;
2629 info->aug_z_format = 0;
2631 /* parse the CIE first */
2632 version = dwarf2_parse_byte(ctx);
2633 if (version != 1 && version != 3 && version != 4)
2635 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2636 return FALSE;
2638 augmentation = (const char*)ctx->data;
2639 ctx->data += strlen(augmentation) + 1;
2641 switch (version)
2643 case 4:
2644 /* skip 'address_size' and 'segment_size' */
2645 ctx->data += 2;
2646 /* fallthrough */
2647 case 1:
2648 case 3:
2649 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2650 info->data_align = dwarf2_leb128_as_signed(ctx);
2651 info->retaddr_reg = version == 1 ? dwarf2_parse_byte(ctx) :dwarf2_leb128_as_unsigned(ctx);
2652 break;
2653 default:
2656 info->state.cfa_rule = RULE_CFA_OFFSET;
2658 end = NULL;
2659 TRACE("\tparsing augmentation %s\n", debugstr_a(augmentation));
2660 if (*augmentation) do
2662 switch (*augmentation)
2664 case 'z':
2665 len = dwarf2_leb128_as_unsigned(ctx);
2666 end = ctx->data + len;
2667 info->aug_z_format = 1;
2668 continue;
2669 case 'L':
2670 info->lsda_encoding = dwarf2_parse_byte(ctx);
2671 continue;
2672 case 'P':
2674 unsigned char encoding = dwarf2_parse_byte(ctx);
2675 /* throw away the indirect bit, as we don't care for the result */
2676 encoding &= ~DW_EH_PE_indirect;
2677 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2678 continue;
2680 case 'R':
2681 info->fde_encoding = dwarf2_parse_byte(ctx);
2682 continue;
2683 case 'S':
2684 info->signal_frame = 1;
2685 continue;
2687 FIXME("unknown augmentation '%c'\n", *augmentation);
2688 if (!end) return FALSE;
2689 break;
2690 } while (*++augmentation);
2691 if (end) ctx->data = end;
2692 return TRUE;
2695 static BOOL dwarf2_get_cie(ULONG_PTR addr, struct module* module, DWORD_PTR delta,
2696 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2697 struct frame_info* info, BOOL in_eh_frame)
2699 const unsigned char* ptr_blk;
2700 const unsigned char* cie_ptr;
2701 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2702 unsigned len, id;
2703 ULONG_PTR start, range;
2704 unsigned cie_id;
2705 const BYTE* start_data = fde_ctx->data;
2707 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2708 /* skip 0-padding at beginning of section (alignment) */
2709 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
2711 if (dwarf2_parse_u4(fde_ctx))
2713 fde_ctx->data -= 4;
2714 break;
2717 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2719 /* find the FDE for address addr (skip CIE) */
2720 len = dwarf2_parse_u4(fde_ctx);
2721 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2722 ptr_blk = fde_ctx->data + len;
2723 id = dwarf2_parse_u4(fde_ctx);
2724 if (id == cie_id)
2726 last_cie_ptr = fde_ctx->data - 8;
2727 /* we need some bits out of the CIE in order to parse all contents */
2728 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2729 cie_ctx->data = fde_ctx->data;
2730 cie_ctx->end_data = ptr_blk;
2731 cie_ctx->word_size = fde_ctx->word_size;
2732 continue;
2734 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2735 if (cie_ptr != last_cie_ptr)
2737 last_cie_ptr = cie_ptr;
2738 cie_ctx->data = cie_ptr;
2739 cie_ctx->word_size = fde_ctx->word_size;
2740 cie_ctx->end_data = cie_ptr + 4;
2741 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2742 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2744 FIXME("wrong CIE pointer at %x from FDE %x\n",
2745 (unsigned)(cie_ptr - start_data),
2746 (unsigned)(fde_ctx->data - start_data));
2747 return FALSE;
2749 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2751 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2752 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2754 if (addr >= start && addr < start + range)
2756 /* reset the FDE context */
2757 fde_ctx->end_data = ptr_blk;
2759 info->ip = start;
2760 return TRUE;
2763 return FALSE;
2766 static int valid_reg(ULONG_PTR reg)
2768 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2769 return (reg < NB_FRAME_REGS);
2772 static void execute_cfa_instructions(struct module* module, dwarf2_traverse_context_t* ctx,
2773 ULONG_PTR last_ip, struct frame_info *info)
2775 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2777 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2779 if (op & 0xc0)
2781 switch (op & 0xc0)
2783 case DW_CFA_advance_loc:
2785 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2786 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2787 info->ip += offset;
2788 break;
2790 case DW_CFA_offset:
2792 ULONG_PTR reg = op & 0x3f;
2793 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2794 if (!valid_reg(reg)) break;
2795 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2796 info->ip,
2797 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
2798 offset);
2799 info->state.regs[reg] = offset;
2800 info->state.rules[reg] = RULE_CFA_OFFSET;
2801 break;
2803 case DW_CFA_restore:
2805 ULONG_PTR reg = op & 0x3f;
2806 if (!valid_reg(reg)) break;
2807 TRACE("%lx: DW_CFA_restore %s\n",
2808 info->ip,
2809 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
2810 info->state.rules[reg] = RULE_UNSET;
2811 break;
2815 else switch (op)
2817 case DW_CFA_nop:
2818 break;
2819 case DW_CFA_set_loc:
2821 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2822 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2823 info->ip = loc;
2824 break;
2826 case DW_CFA_advance_loc1:
2828 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2829 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2830 info->ip += offset;
2831 break;
2833 case DW_CFA_advance_loc2:
2835 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2836 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2837 info->ip += offset;
2838 break;
2840 case DW_CFA_advance_loc4:
2842 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2843 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2844 info->ip += offset;
2845 break;
2847 case DW_CFA_offset_extended:
2848 case DW_CFA_offset_extended_sf:
2850 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2851 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2852 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2853 if (!valid_reg(reg)) break;
2854 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2855 info->ip,
2856 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
2857 offset);
2858 info->state.regs[reg] = offset;
2859 info->state.rules[reg] = RULE_CFA_OFFSET;
2860 break;
2862 case DW_CFA_restore_extended:
2864 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2865 if (!valid_reg(reg)) break;
2866 TRACE("%lx: DW_CFA_restore_extended %s\n",
2867 info->ip,
2868 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
2869 info->state.rules[reg] = RULE_UNSET;
2870 break;
2872 case DW_CFA_undefined:
2874 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2875 if (!valid_reg(reg)) break;
2876 TRACE("%lx: DW_CFA_undefined %s\n",
2877 info->ip,
2878 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
2879 info->state.rules[reg] = RULE_UNDEFINED;
2880 break;
2882 case DW_CFA_same_value:
2884 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2885 if (!valid_reg(reg)) break;
2886 TRACE("%lx: DW_CFA_same_value %s\n",
2887 info->ip,
2888 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
2889 info->state.regs[reg] = reg;
2890 info->state.rules[reg] = RULE_SAME;
2891 break;
2893 case DW_CFA_register:
2895 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2896 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2897 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2898 TRACE("%lx: DW_CFA_register %s == %s\n",
2899 info->ip,
2900 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
2901 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2, module, TRUE)));
2902 info->state.regs[reg] = reg2;
2903 info->state.rules[reg] = RULE_OTHER_REG;
2904 break;
2906 case DW_CFA_remember_state:
2907 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2908 if (info->state_sp >= MAX_SAVED_STATES)
2909 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2910 else
2911 info->state_stack[info->state_sp++] = info->state;
2912 break;
2913 case DW_CFA_restore_state:
2914 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2915 if (!info->state_sp)
2916 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2917 else
2918 info->state = info->state_stack[--info->state_sp];
2919 break;
2920 case DW_CFA_def_cfa:
2921 case DW_CFA_def_cfa_sf:
2923 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2924 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2925 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2926 if (!valid_reg(reg)) break;
2927 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2928 info->ip,
2929 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
2930 offset);
2931 info->state.cfa_reg = reg;
2932 info->state.cfa_offset = offset;
2933 info->state.cfa_rule = RULE_CFA_OFFSET;
2934 break;
2936 case DW_CFA_def_cfa_register:
2938 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2939 if (!valid_reg(reg)) break;
2940 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2941 info->ip,
2942 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
2943 info->state.cfa_reg = reg;
2944 info->state.cfa_rule = RULE_CFA_OFFSET;
2945 break;
2947 case DW_CFA_def_cfa_offset:
2948 case DW_CFA_def_cfa_offset_sf:
2950 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2951 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2952 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
2953 info->state.cfa_offset = offset;
2954 info->state.cfa_rule = RULE_CFA_OFFSET;
2955 break;
2957 case DW_CFA_def_cfa_expression:
2959 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2960 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2961 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2962 info->state.cfa_offset = expr;
2963 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2964 ctx->data += len;
2965 break;
2967 case DW_CFA_expression:
2968 case DW_CFA_val_expression:
2970 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2971 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2972 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2973 if (!valid_reg(reg)) break;
2974 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2975 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2976 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
2977 expr, expr + len);
2978 info->state.regs[reg] = expr;
2979 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2980 ctx->data += len;
2981 break;
2983 case DW_CFA_GNU_args_size:
2984 /* FIXME: should check that GCC is the compiler for this CU */
2986 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2987 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2988 /* ignored */
2989 break;
2991 default:
2992 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2993 break;
2998 /* retrieve a context register from its dwarf number */
2999 static DWORD64 get_context_reg(const struct module* module, struct cpu_stack_walk *csw, union ctx *context,
3000 ULONG_PTR dw_reg)
3002 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3003 void* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3005 if (sz == 8)
3006 return *(DWORD64 *)ptr;
3007 else if (sz == 4)
3008 return *(DWORD *)ptr;
3010 FIXME("unhandled size %d\n", sz);
3011 return 0;
3014 /* set a context register from its dwarf number */
3015 static void set_context_reg(const struct module* module, struct cpu_stack_walk* csw, union ctx *context,
3016 ULONG_PTR dw_reg, ULONG_PTR val, BOOL isdebuggee)
3018 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3019 ULONG_PTR* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3021 if (isdebuggee)
3023 char tmp[16];
3025 if (sz > sizeof(tmp))
3027 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
3028 return;
3030 if (!sw_read_mem(csw, val, tmp, sz))
3032 WARN("Couldn't read memory at %p\n", (void*)val);
3033 return;
3035 memcpy(ptr, tmp, sz);
3037 else
3039 if (sz != sizeof(ULONG_PTR))
3041 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
3042 return;
3044 *ptr = val;
3048 /* copy a register from one context to another using dwarf number */
3049 static void copy_context_reg(const struct module* module, struct cpu_stack_walk *csw,
3050 union ctx *dstcontext, ULONG_PTR dwregdst,
3051 union ctx *srccontext, ULONG_PTR dwregsrc)
3053 unsigned regdstno = csw->cpu->map_dwarf_register(dwregdst, module, TRUE), szdst;
3054 unsigned regsrcno = csw->cpu->map_dwarf_register(dwregsrc, module, TRUE), szsrc;
3055 ULONG_PTR* ptrdst = csw->cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3056 ULONG_PTR* ptrsrc = csw->cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3058 if (szdst != szsrc)
3060 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3061 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3062 return;
3064 memcpy(ptrdst, ptrsrc, szdst);
3067 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3068 const unsigned char* zp, union ctx *context)
3070 dwarf2_traverse_context_t ctx;
3071 ULONG_PTR reg, sz, tmp;
3072 DWORD64 stack[64];
3073 int sp = -1;
3074 ULONG_PTR len;
3076 ctx.data = zp;
3077 ctx.end_data = zp + 4;
3078 len = dwarf2_leb128_as_unsigned(&ctx);
3079 ctx.end_data = ctx.data + len;
3080 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3082 while (ctx.data < ctx.end_data)
3084 unsigned char opcode = dwarf2_parse_byte(&ctx);
3086 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3087 stack[++sp] = opcode - DW_OP_lit0;
3088 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3089 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_reg0);
3090 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3091 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_breg0)
3092 + dwarf2_leb128_as_signed(&ctx);
3093 else switch (opcode)
3095 case DW_OP_nop: break;
3096 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
3097 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3098 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3099 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3100 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3101 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3102 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3103 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3104 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3105 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3106 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3107 case DW_OP_deref:
3108 tmp = 0;
3109 if (!sw_read_mem(csw, stack[sp], &tmp, ctx.word_size))
3111 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3112 tmp = 0;
3114 stack[sp] = tmp;
3115 break;
3116 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3117 case DW_OP_drop: sp--; break;
3118 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3119 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3120 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3121 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3122 case DW_OP_abs: stack[sp] = sizeof(stack[sp]) == 8 ? llabs((INT64)stack[sp]) : abs((INT32)stack[sp]); break;
3123 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3124 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3125 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3126 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3127 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3128 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3129 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3130 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3131 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3132 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3133 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3134 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3135 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3136 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3137 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3138 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3139 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3140 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3141 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3142 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3143 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3144 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3145 case DW_OP_GNU_encoded_addr:
3146 tmp = dwarf2_parse_byte(&ctx);
3147 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3148 break;
3149 case DW_OP_regx:
3150 stack[++sp] = get_context_reg(module, csw, context, dwarf2_leb128_as_unsigned(&ctx));
3151 break;
3152 case DW_OP_bregx:
3153 reg = dwarf2_leb128_as_unsigned(&ctx);
3154 tmp = dwarf2_leb128_as_signed(&ctx);
3155 stack[++sp] = get_context_reg(module, csw, context, reg) + tmp;
3156 break;
3157 case DW_OP_deref_size:
3158 sz = dwarf2_parse_byte(&ctx);
3159 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3161 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3162 tmp = 0;
3164 /* do integral promotion */
3165 switch (sz)
3167 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3168 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3169 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3170 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3171 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3173 break;
3174 default:
3175 FIXME("unhandled opcode %02x\n", opcode);
3178 return stack[sp];
3181 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3182 union ctx *context, struct frame_state *state, DWORD64 *cfa)
3184 unsigned int i;
3185 ULONG_PTR value;
3186 union ctx new_context = *context;
3188 switch (state->cfa_rule)
3190 case RULE_EXPRESSION:
3191 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3192 if (!sw_read_mem(csw, *cfa, cfa, csw->cpu->word_size))
3194 WARN("Couldn't read memory at %s\n", wine_dbgstr_longlong(*cfa));
3195 return;
3197 break;
3198 case RULE_VAL_EXPRESSION:
3199 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3200 break;
3201 default:
3202 *cfa = get_context_reg(module, csw, context, state->cfa_reg) + state->cfa_offset;
3203 break;
3205 if (!*cfa) return;
3207 for (i = 0; i < NB_FRAME_REGS; i++)
3209 switch (state->rules[i])
3211 case RULE_UNSET:
3212 case RULE_UNDEFINED:
3213 case RULE_SAME:
3214 break;
3215 case RULE_CFA_OFFSET:
3216 set_context_reg(module, csw, &new_context, i, *cfa + state->regs[i], TRUE);
3217 break;
3218 case RULE_OTHER_REG:
3219 copy_context_reg(module, csw, &new_context, i, context, state->regs[i]);
3220 break;
3221 case RULE_EXPRESSION:
3222 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3223 set_context_reg(module, csw, &new_context, i, value, TRUE);
3224 break;
3225 case RULE_VAL_EXPRESSION:
3226 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3227 set_context_reg(module, csw, &new_context, i, value, FALSE);
3228 break;
3231 *context = new_context;
3234 /***********************************************************************
3235 * dwarf2_virtual_unwind
3238 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk *csw, ULONG_PTR ip,
3239 union ctx *context, DWORD64 *cfa)
3241 struct module_pair pair;
3242 struct frame_info info;
3243 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3244 struct module_format* modfmt;
3245 const unsigned char* end;
3246 DWORD_PTR delta;
3248 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3249 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3250 !module_get_debug(&pair))
3251 return FALSE;
3252 modfmt = pair.effective->format_info[DFI_DWARF];
3253 if (!modfmt) return FALSE;
3254 memset(&info, 0, sizeof(info));
3255 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3256 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3257 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3258 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3259 * in this process the IMAGE section at a different address as the one expected by
3260 * the image
3262 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3263 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3264 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3266 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3267 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3268 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3269 delta = pair.effective->reloc_delta;
3270 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3272 TRACE("Couldn't find information for %lx\n", ip);
3273 return FALSE;
3277 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3278 ip, info.ip, info.code_align, info.data_align,
3279 csw->cpu->fetch_regname(csw->cpu->map_dwarf_register(info.retaddr_reg, pair.effective, TRUE)));
3281 /* if at very beginning of function, return and use default unwinder */
3282 if (ip == info.ip) return FALSE;
3283 execute_cfa_instructions(pair.effective, &cie_ctx, ip, &info);
3285 if (info.aug_z_format) /* get length of augmentation data */
3287 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3288 end = fde_ctx.data + len;
3290 else end = NULL;
3291 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3292 if (end) fde_ctx.data = end;
3294 execute_cfa_instructions(pair.effective, &fde_ctx, ip, &info);
3296 /* if there is no information about retaddr, use default unwinder */
3297 if (info.state.rules[info.retaddr_reg] == RULE_UNSET) return FALSE;
3299 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3301 return TRUE;
3304 static void dwarf2_location_compute(struct process* pcs,
3305 const struct module_format* modfmt,
3306 const struct symt_function* func,
3307 struct location* loc)
3309 struct location frame;
3310 DWORD_PTR ip;
3311 int err;
3312 dwarf2_traverse_context_t lctx;
3314 if (!func->container || func->container->tag != SymTagCompiland)
3316 WARN("We'd expect function %s's container to exist and be a compiland\n", debugstr_a(func->hash_elt.name));
3317 err = loc_err_internal;
3319 else
3321 /* instruction pointer relative to compiland's start */
3322 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3324 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3326 switch (loc->kind)
3328 case loc_dwarf2_location_list:
3329 /* Then, if the variable has a location list, find it !! */
3330 if (dwarf2_lookup_loclist(modfmt,
3331 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3332 ip, &lctx))
3333 goto do_compute;
3334 err = loc_err_out_of_scope;
3335 break;
3336 case loc_dwarf2_block:
3337 /* or if we have a copy of an existing block, get ready for it */
3339 unsigned* ptr = (unsigned*)loc->offset;
3341 lctx.data = (const BYTE*)(ptr + 1);
3342 lctx.end_data = lctx.data + *ptr;
3343 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3345 do_compute:
3346 /* now get the variable */
3347 err = compute_location(modfmt->module, &lctx, loc, pcs->handle, &frame);
3348 break;
3349 case loc_register:
3350 case loc_regrel:
3351 /* nothing to do */
3352 break;
3353 default:
3354 WARN("Unsupported local kind %d\n", loc->kind);
3355 err = loc_err_internal;
3359 if (err < 0)
3361 loc->kind = loc_register;
3362 loc->reg = err;
3366 static void *zalloc(void *priv, uInt items, uInt sz)
3368 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3371 static void zfree(void *priv, void *addr)
3373 HeapFree(GetProcessHeap(), 0, addr);
3376 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3377 const char* zsectname,
3378 struct image_section_map* ism)
3380 z_stream z;
3381 LARGE_INTEGER li;
3382 int res;
3383 BOOL ret = FALSE;
3385 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3386 size_t sz = image_get_map_size(ism);
3388 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
3390 ERR("invalid compressed section %s\n", debugstr_a(zsectname));
3391 goto out;
3394 #ifdef WORDS_BIGENDIAN
3395 li.u.HighPart = *(DWORD*)&sect[4];
3396 li.u.LowPart = *(DWORD*)&sect[8];
3397 #else
3398 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
3399 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
3400 #endif
3402 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
3403 if (!addr)
3404 goto out;
3406 z.next_in = &sect[12];
3407 z.avail_in = sz - 12;
3408 z.opaque = NULL;
3409 z.zalloc = zalloc;
3410 z.zfree = zfree;
3412 res = inflateInit(&z);
3413 if (res != Z_OK)
3415 FIXME("inflateInit failed with %i / %s\n", res, debugstr_a(z.msg));
3416 goto out_free;
3419 do {
3420 z.next_out = addr + z.total_out;
3421 z.avail_out = li.QuadPart - z.total_out;
3422 res = inflate(&z, Z_FINISH);
3423 } while (z.avail_in && res == Z_STREAM_END);
3425 if (res != Z_STREAM_END)
3427 FIXME("Decompression failed with %i / %s\n", res, debugstr_a(z.msg));
3428 goto out_end;
3431 ret = TRUE;
3432 section->compressed = TRUE;
3433 section->address = addr;
3434 section->rva = image_get_map_rva(ism);
3435 section->size = z.total_out;
3437 out_end:
3438 inflateEnd(&z);
3439 out_free:
3440 if (!ret)
3441 HeapFree(GetProcessHeap(), 0, addr);
3442 out:
3443 image_unmap_section(ism);
3444 return ret;
3447 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3448 const char* sectname, const char* zsectname,
3449 struct image_section_map* ism)
3451 struct image_section_map local_ism;
3453 if (!ism) ism = &local_ism;
3455 section->compressed = FALSE;
3456 if (image_find_section(fmap, sectname, ism))
3458 section->address = (const BYTE*)image_map_section(ism);
3459 section->size = image_get_map_size(ism);
3460 section->rva = image_get_map_rva(ism);
3461 return TRUE;
3464 section->address = NULL;
3465 section->size = 0;
3466 section->rva = 0;
3468 if (zsectname && image_find_section(fmap, zsectname, ism))
3470 return dwarf2_init_zsection(section, zsectname, ism);
3473 return FALSE;
3476 static inline void dwarf2_fini_section(dwarf2_section_t* section)
3478 if (section->compressed)
3479 HeapFree(GetProcessHeap(), 0, (void*)section->address);
3482 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3484 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
3485 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
3486 HeapFree(GetProcessHeap(), 0, modfmt);
3489 BOOL dwarf2_parse(struct module* module, ULONG_PTR load_offset,
3490 const struct elf_thunk_area* thunks,
3491 struct image_file_map* fmap)
3493 dwarf2_section_t eh_frame, section[section_max];
3494 dwarf2_traverse_context_t mod_ctx;
3495 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3496 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3497 BOOL ret = TRUE;
3498 struct module_format* dwarf2_modfmt;
3500 if (!dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect))
3501 /* lld produces .eh_fram to avoid generating a long name */
3502 dwarf2_init_section(&eh_frame, fmap, ".eh_fram", NULL, &eh_frame_sect);
3503 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
3504 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
3505 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
3506 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
3507 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
3509 /* to do anything useful we need either .eh_frame or .debug_info */
3510 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3511 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3513 ret = FALSE;
3514 goto leave;
3517 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3519 /* debug info might have a different base address than .so file
3520 * when elf file is prelinked after splitting off debug info
3521 * adjust symbol base addresses accordingly
3523 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3526 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3528 mod_ctx.data = section[section_debug].address;
3529 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3530 mod_ctx.word_size = 0; /* will be correctly set later on */
3532 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3533 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3534 if (!dwarf2_modfmt)
3536 ret = FALSE;
3537 goto leave;
3539 dwarf2_modfmt->module = module;
3540 dwarf2_modfmt->remove = dwarf2_module_remove;
3541 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3542 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3543 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3544 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3546 /* As we'll need later some sections' content, we won't unmap these
3547 * sections upon existing this function
3549 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
3550 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
3551 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3553 while (mod_ctx.data < mod_ctx.end_data)
3555 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3557 dwarf2_modfmt->module->module.SymType = SymDia;
3558 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3559 /* FIXME: we could have a finer grain here */
3560 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3561 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3562 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3563 dwarf2_modfmt->module->module.Publics = TRUE;
3565 /* set the word_size for eh_frame parsing */
3566 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8;
3568 leave:
3569 dwarf2_fini_section(&section[section_debug]);
3570 dwarf2_fini_section(&section[section_abbrev]);
3571 dwarf2_fini_section(&section[section_string]);
3572 dwarf2_fini_section(&section[section_line]);
3573 dwarf2_fini_section(&section[section_ranges]);
3575 image_unmap_section(&debug_sect);
3576 image_unmap_section(&debug_abbrev_sect);
3577 image_unmap_section(&debug_str_sect);
3578 image_unmap_section(&debug_line_sect);
3579 image_unmap_section(&debug_ranges_sect);
3580 if (!ret) image_unmap_section(&eh_frame_sect);
3582 return ret;