wined3d: Pass a texture and sub-resource index to wined3d_volume_download_data().
[wine.git] / dlls / dbghelp / dwarf.c
blobdcbcf0ab2cb094ed5db423dd7c0b1a7e9dee8ddc
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2011, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
25 #include "config.h"
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <stdio.h>
42 #include <assert.h>
43 #include <stdarg.h>
45 #ifdef HAVE_ZLIB
46 #include <zlib.h>
47 #endif
49 #include "windef.h"
50 #include "winternl.h"
51 #include "winbase.h"
52 #include "winuser.h"
53 #include "ole2.h"
54 #include "oleauto.h"
56 #include "dbghelp_private.h"
57 #include "image_private.h"
59 #include "wine/debug.h"
61 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
63 /* FIXME:
64 * - Functions:
65 * o unspecified parameters
66 * o inlined functions
67 * o Debug{Start|End}Point
68 * o CFA
69 * - Udt
70 * o proper types loading (nesting)
73 #if 0
74 static void dump(const void* ptr, unsigned len)
76 int i, j;
77 BYTE msg[128];
78 static const char hexof[] = "0123456789abcdef";
79 const BYTE* x = ptr;
81 for (i = 0; i < len; i += 16)
83 sprintf(msg, "%08x: ", i);
84 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
85 for (j = 0; j < min(16, len - i); j++)
87 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
88 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
89 msg[10 + 3 * j + 2] = ' ';
90 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
91 x[i + j] : '.';
93 msg[10 + 3 * 16] = ' ';
94 msg[10 + 3 * 16 + 1 + 16] = '\0';
95 TRACE("%s\n", msg);
98 #endif
102 * Main Specs:
103 * http://www.eagercon.com/dwarf/dwarf3std.htm
104 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
106 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
108 * example of projects who do dwarf2 parsing:
109 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
110 * http://elis.ugent.be/diota/log/ltrace_elf.c
112 #include "dwarf.h"
115 * Parsers
118 typedef struct dwarf2_abbrev_entry_attr_s
120 unsigned long attribute;
121 unsigned long form;
122 struct dwarf2_abbrev_entry_attr_s* next;
123 } dwarf2_abbrev_entry_attr_t;
125 typedef struct dwarf2_abbrev_entry_s
127 unsigned long entry_code;
128 unsigned long tag;
129 unsigned char have_child;
130 unsigned num_attr;
131 dwarf2_abbrev_entry_attr_t* attrs;
132 } dwarf2_abbrev_entry_t;
134 struct dwarf2_block
136 unsigned size;
137 const unsigned char* ptr;
140 struct attribute
142 unsigned long form;
143 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
144 union
146 unsigned long uvalue;
147 ULONGLONG lluvalue;
148 long svalue;
149 const char* string;
150 struct dwarf2_block block;
151 } u;
154 typedef struct dwarf2_debug_info_s
156 const dwarf2_abbrev_entry_t*abbrev;
157 struct symt* symt;
158 const unsigned char** data;
159 struct vector children;
160 struct dwarf2_debug_info_s* parent;
161 } dwarf2_debug_info_t;
163 typedef struct dwarf2_section_s
165 BOOL compressed;
166 const unsigned char* address;
167 unsigned size;
168 DWORD_PTR rva;
169 } dwarf2_section_t;
171 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
173 typedef struct dwarf2_traverse_context_s
175 const unsigned char* data;
176 const unsigned char* end_data;
177 unsigned char word_size;
178 } dwarf2_traverse_context_t;
180 /* symt_cache indexes */
181 #define sc_void 0
182 #define sc_int1 1
183 #define sc_int2 2
184 #define sc_int4 3
185 #define sc_num 4
187 typedef struct dwarf2_parse_context_s
189 const dwarf2_section_t* sections;
190 unsigned section;
191 struct pool pool;
192 struct module* module;
193 struct symt_compiland* compiland;
194 const struct elf_thunk_area*thunks;
195 struct sparse_array abbrev_table;
196 struct sparse_array debug_info_table;
197 unsigned long load_offset;
198 unsigned long ref_offset;
199 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
200 char* cpp_name;
201 } dwarf2_parse_context_t;
203 /* stored in the dbghelp's module internal structure for later reuse */
204 struct dwarf2_module_info_s
206 dwarf2_section_t debug_loc;
207 dwarf2_section_t debug_frame;
208 dwarf2_section_t eh_frame;
209 unsigned char word_size;
212 #define loc_dwarf2_location_list (loc_user + 0)
213 #define loc_dwarf2_block (loc_user + 1)
215 /* forward declarations */
216 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
218 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
220 return *ptr;
223 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
225 unsigned char uvalue = dwarf2_get_byte(ctx->data);
226 ctx->data += 1;
227 return uvalue;
230 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
232 return *(const UINT16*)ptr;
235 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
237 unsigned short uvalue = dwarf2_get_u2(ctx->data);
238 ctx->data += 2;
239 return uvalue;
242 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
244 return *(const UINT32*)ptr;
247 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
249 unsigned long uvalue = dwarf2_get_u4(ctx->data);
250 ctx->data += 4;
251 return uvalue;
254 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
256 return *(const UINT64*)ptr;
259 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
261 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
262 ctx->data += 8;
263 return uvalue;
266 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
268 unsigned long ret = 0;
269 unsigned char byte;
270 unsigned shift = 0;
274 byte = dwarf2_get_byte(ptr++);
275 ret |= (byte & 0x7f) << shift;
276 shift += 7;
277 } while (byte & 0x80);
279 if (end) *end = ptr;
280 return ret;
283 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
285 unsigned long ret;
287 assert(ctx);
289 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
291 return ret;
294 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
296 long ret = 0;
297 unsigned char byte;
298 unsigned shift = 0;
299 const unsigned size = sizeof(int) * 8;
303 byte = dwarf2_get_byte(ptr++);
304 ret |= (byte & 0x7f) << shift;
305 shift += 7;
306 } while (byte & 0x80);
307 if (end) *end = ptr;
309 /* as spec: sign bit of byte is 2nd high order bit (80x40)
310 * -> 0x80 is used as flag.
312 if ((shift < size) && (byte & 0x40))
314 ret |= - (1 << shift);
316 return ret;
319 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
321 long ret = 0;
323 assert(ctx);
325 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
326 return ret;
329 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
331 unsigned ret;
332 for (ret = 0; ctx->data[ret] & 0x80; ret++);
333 return ret + 1;
336 /******************************************************************
337 * dwarf2_get_addr
339 * Returns an address.
340 * We assume that in all cases word size from Dwarf matches the size of
341 * addresses in platform where the exec is compiled.
343 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
345 unsigned long ret;
347 switch (word_size)
349 case 4:
350 ret = dwarf2_get_u4(ptr);
351 break;
352 case 8:
353 ret = dwarf2_get_u8(ptr);
354 break;
355 default:
356 FIXME("Unsupported Word Size %u\n", word_size);
357 ret = 0;
359 return ret;
362 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
364 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
365 ctx->data += ctx->word_size;
366 return ret;
369 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
371 return wine_dbg_sprintf("ctx(%p)", ctx->data);
374 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
376 return wine_dbg_sprintf("ctx(%p,%s)",
377 ctx, debugstr_w(ctx->module->module.ModuleName));
380 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
382 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
383 di->abbrev, di->symt);
386 static dwarf2_abbrev_entry_t*
387 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
388 unsigned long entry_code)
390 assert( NULL != abbrev_table );
391 return sparse_array_find(abbrev_table, entry_code);
394 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
395 struct sparse_array* abbrev_table,
396 struct pool* pool)
398 unsigned long entry_code;
399 dwarf2_abbrev_entry_t* abbrev_entry;
400 dwarf2_abbrev_entry_attr_t* new = NULL;
401 dwarf2_abbrev_entry_attr_t* last = NULL;
402 unsigned long attribute;
403 unsigned long form;
405 assert( NULL != abbrev_ctx );
407 TRACE("%s, end at %p\n",
408 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
410 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
411 while (abbrev_ctx->data < abbrev_ctx->end_data)
413 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
414 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
415 TRACE("found entry_code %lu\n", entry_code);
416 if (!entry_code)
418 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
419 break;
421 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
422 assert( NULL != abbrev_entry );
424 abbrev_entry->entry_code = entry_code;
425 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
426 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
427 abbrev_entry->attrs = NULL;
428 abbrev_entry->num_attr = 0;
430 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
431 abbrev_table, sparse_array_length(abbrev_table),
432 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
434 last = NULL;
435 while (1)
437 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
438 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
439 if (!attribute) break;
441 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
442 assert(new);
444 new->attribute = attribute;
445 new->form = form;
446 new->next = NULL;
447 if (abbrev_entry->attrs) last->next = new;
448 else abbrev_entry->attrs = new;
449 last = new;
450 abbrev_entry->num_attr++;
453 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
456 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
457 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
459 unsigned step;
461 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
463 switch (abbrev_attr->form)
465 case DW_FORM_flag_present: step = 0; break;
466 case DW_FORM_ref_addr:
467 case DW_FORM_addr: step = ctx->word_size; break;
468 case DW_FORM_flag:
469 case DW_FORM_data1:
470 case DW_FORM_ref1: step = 1; break;
471 case DW_FORM_data2:
472 case DW_FORM_ref2: step = 2; break;
473 case DW_FORM_data4:
474 case DW_FORM_ref4:
475 case DW_FORM_strp: step = 4; break;
476 case DW_FORM_data8:
477 case DW_FORM_ref8: step = 8; break;
478 case DW_FORM_sdata:
479 case DW_FORM_ref_udata:
480 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
481 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
482 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
483 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
484 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
485 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
486 default:
487 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
488 return;
490 ctx->data += step;
493 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
494 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
495 const unsigned char* data,
496 struct attribute* attr)
498 attr->form = abbrev_attr->form;
499 switch (attr->form)
501 case DW_FORM_ref_addr:
502 case DW_FORM_addr:
503 attr->u.uvalue = dwarf2_get_addr(data,
504 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
505 TRACE("addr<0x%lx>\n", attr->u.uvalue);
506 break;
508 case DW_FORM_flag:
509 attr->u.uvalue = dwarf2_get_byte(data);
510 TRACE("flag<0x%lx>\n", attr->u.uvalue);
511 break;
513 case DW_FORM_flag_present:
514 attr->u.uvalue = 1;
515 TRACE("flag_present\n");
516 break;
518 case DW_FORM_data1:
519 attr->u.uvalue = dwarf2_get_byte(data);
520 TRACE("data1<%lu>\n", attr->u.uvalue);
521 break;
523 case DW_FORM_data2:
524 attr->u.uvalue = dwarf2_get_u2(data);
525 TRACE("data2<%lu>\n", attr->u.uvalue);
526 break;
528 case DW_FORM_data4:
529 attr->u.uvalue = dwarf2_get_u4(data);
530 TRACE("data4<%lu>\n", attr->u.uvalue);
531 break;
533 case DW_FORM_data8:
534 attr->u.lluvalue = dwarf2_get_u8(data);
535 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
536 break;
538 case DW_FORM_ref1:
539 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
540 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
541 break;
543 case DW_FORM_ref2:
544 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
545 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
546 break;
548 case DW_FORM_ref4:
549 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
550 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
551 break;
553 case DW_FORM_ref8:
554 FIXME("Unhandled 64-bit support\n");
555 break;
557 case DW_FORM_sdata:
558 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
559 break;
561 case DW_FORM_ref_udata:
562 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
563 break;
565 case DW_FORM_udata:
566 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
567 break;
569 case DW_FORM_string:
570 attr->u.string = (const char *)data;
571 TRACE("string<%s>\n", attr->u.string);
572 break;
574 case DW_FORM_strp:
576 unsigned long offset = dwarf2_get_u4(data);
577 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
579 TRACE("strp<%s>\n", attr->u.string);
580 break;
582 case DW_FORM_block:
583 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
584 break;
586 case DW_FORM_block1:
587 attr->u.block.size = dwarf2_get_byte(data);
588 attr->u.block.ptr = data + 1;
589 break;
591 case DW_FORM_block2:
592 attr->u.block.size = dwarf2_get_u2(data);
593 attr->u.block.ptr = data + 2;
594 break;
596 case DW_FORM_block4:
597 attr->u.block.size = dwarf2_get_u4(data);
598 attr->u.block.ptr = data + 4;
599 break;
601 default:
602 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
603 break;
607 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
608 const dwarf2_debug_info_t* di,
609 unsigned at, struct attribute* attr)
611 unsigned i, refidx = 0;
612 dwarf2_abbrev_entry_attr_t* abbrev_attr;
613 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
615 attr->gotten_from = attr_direct;
616 while (di)
618 ref_abbrev_attr = NULL;
619 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
621 if (abbrev_attr->attribute == at)
623 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
624 return TRUE;
626 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
627 abbrev_attr->attribute == DW_AT_specification) &&
628 at != DW_AT_sibling)
630 if (ref_abbrev_attr)
631 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
632 ref_abbrev_attr = abbrev_attr;
633 refidx = i;
634 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
635 attr_abstract_origin : attr_specification;
638 /* do we have either an abstract origin or a specification debug entry to look into ? */
639 if (!ref_abbrev_attr) break;
640 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
641 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
642 FIXME("Should have found the debug info entry\n");
644 return FALSE;
647 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
649 #define Wine_DW_no_register 0x7FFFFFFF
651 static unsigned dwarf2_map_register(int regno)
653 if (regno == Wine_DW_no_register)
655 FIXME("What the heck map reg 0x%x\n",regno);
656 return 0;
658 return dbghelp_current_cpu->map_dwarf_register(regno, FALSE);
661 static enum location_error
662 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
663 HANDLE hproc, const struct location* frame)
665 DWORD_PTR tmp, stack[64];
666 unsigned stk;
667 unsigned char op;
668 BOOL piece_found = FALSE;
670 stack[stk = 0] = 0;
672 loc->kind = loc_absolute;
673 loc->reg = Wine_DW_no_register;
675 while (ctx->data < ctx->end_data)
677 op = dwarf2_parse_byte(ctx);
679 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
680 stack[++stk] = op - DW_OP_lit0;
681 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
683 /* dbghelp APIs don't know how to cope with this anyway
684 * (for example 'long long' stored in two registers)
685 * FIXME: We should tell winedbg how to deal with it (sigh)
687 if (!piece_found)
689 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0);
690 if (loc->reg != Wine_DW_no_register)
691 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
692 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
693 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
694 loc->reg = cvreg;
696 loc->kind = loc_register;
698 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
700 /* dbghelp APIs don't know how to cope with this anyway
701 * (for example 'long long' stored in two registers)
702 * FIXME: We should tell winedbg how to deal with it (sigh)
704 if (!piece_found)
706 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0);
707 if (loc->reg != Wine_DW_no_register)
708 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
709 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
710 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
711 loc->reg = cvreg;
713 stack[++stk] = dwarf2_leb128_as_signed(ctx);
714 loc->kind = loc_regrel;
716 else switch (op)
718 case DW_OP_nop: break;
719 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
720 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
721 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
722 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
723 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
724 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
725 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
726 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
727 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
728 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
729 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
730 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
731 case DW_OP_drop: stk--; break;
732 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
733 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
734 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
735 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
736 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
737 case DW_OP_neg: stack[stk] = -stack[stk]; break;
738 case DW_OP_not: stack[stk] = ~stack[stk]; break;
739 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
740 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
741 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
742 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
743 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
744 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
745 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
746 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
747 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
748 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
749 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
750 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
751 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
752 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
753 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
754 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
755 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
756 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
757 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
758 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
759 case DW_OP_regx:
760 tmp = dwarf2_leb128_as_unsigned(ctx);
761 if (!piece_found)
763 if (loc->reg != Wine_DW_no_register)
764 FIXME("Only supporting one reg\n");
765 loc->reg = dwarf2_map_register(tmp);
767 loc->kind = loc_register;
768 break;
769 case DW_OP_bregx:
770 tmp = dwarf2_leb128_as_unsigned(ctx);
771 if (loc->reg != Wine_DW_no_register)
772 FIXME("Only supporting one regx\n");
773 loc->reg = dwarf2_map_register(tmp);
774 stack[++stk] = dwarf2_leb128_as_signed(ctx);
775 loc->kind = loc_regrel;
776 break;
777 case DW_OP_fbreg:
778 if (loc->reg != Wine_DW_no_register)
779 FIXME("Only supporting one reg (%s/%d -> -2)\n",
780 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
781 if (frame && frame->kind == loc_register)
783 loc->kind = loc_regrel;
784 loc->reg = frame->reg;
785 stack[++stk] = dwarf2_leb128_as_signed(ctx);
787 else if (frame && frame->kind == loc_regrel)
789 loc->kind = loc_regrel;
790 loc->reg = frame->reg;
791 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
793 else
795 /* FIXME: this could be later optimized by not recomputing
796 * this very location expression
798 loc->kind = loc_dwarf2_block;
799 stack[++stk] = dwarf2_leb128_as_signed(ctx);
801 break;
802 case DW_OP_piece:
804 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
805 WARN("Not handling OP_piece (size=%d)\n", sz);
806 piece_found = TRUE;
808 break;
809 case DW_OP_deref:
810 if (!stk)
812 FIXME("Unexpected empty stack\n");
813 return loc_err_internal;
815 if (loc->reg != Wine_DW_no_register)
817 WARN("Too complex expression for deref\n");
818 return loc_err_too_complex;
820 if (hproc)
822 DWORD_PTR addr = stack[stk--];
823 DWORD_PTR deref;
825 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
827 WARN("Couldn't read memory at %lx\n", addr);
828 return loc_err_cant_read;
830 stack[++stk] = deref;
832 else
834 loc->kind = loc_dwarf2_block;
836 break;
837 case DW_OP_deref_size:
838 if (!stk)
840 FIXME("Unexpected empty stack\n");
841 return loc_err_internal;
843 if (loc->reg != Wine_DW_no_register)
845 WARN("Too complex expression for deref\n");
846 return loc_err_too_complex;
848 if (hproc)
850 DWORD_PTR addr = stack[stk--];
851 BYTE derefsize = dwarf2_parse_byte(ctx);
852 DWORD64 deref;
854 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
856 WARN("Couldn't read memory at %lx\n", addr);
857 return loc_err_cant_read;
860 switch (derefsize)
862 case 1: stack[++stk] = *(unsigned char*)&deref; break;
863 case 2: stack[++stk] = *(unsigned short*)&deref; break;
864 case 4: stack[++stk] = *(DWORD*)&deref; break;
865 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
868 else
870 dwarf2_parse_byte(ctx);
871 loc->kind = loc_dwarf2_block;
873 break;
874 case DW_OP_stack_value:
875 /* Expected behaviour is that this is the last instruction of this
876 * expression and just the "top of stack" value should be put to loc->offset. */
877 break;
878 default:
879 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
880 FIXME("Unhandled attr op: %x\n", op);
881 /* FIXME else unhandled extension */
882 return loc_err_internal;
885 loc->offset = stack[stk];
886 return 0;
889 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
890 const dwarf2_debug_info_t* di,
891 unsigned long dw,
892 struct location* loc,
893 const struct location* frame)
895 struct attribute xloc;
897 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
899 switch (xloc.form)
901 case DW_FORM_data1: case DW_FORM_data2:
902 case DW_FORM_udata: case DW_FORM_sdata:
903 loc->kind = loc_absolute;
904 loc->reg = 0;
905 loc->offset = xloc.u.uvalue;
906 return TRUE;
907 case DW_FORM_data4: case DW_FORM_data8:
908 loc->kind = loc_dwarf2_location_list;
909 loc->reg = Wine_DW_no_register;
910 loc->offset = xloc.u.uvalue;
911 return TRUE;
912 case DW_FORM_block:
913 case DW_FORM_block1:
914 case DW_FORM_block2:
915 case DW_FORM_block4:
916 break;
917 default: FIXME("Unsupported yet form %lx\n", xloc.form);
918 return FALSE;
921 /* assume we have a block form */
923 if (xloc.u.block.size)
925 dwarf2_traverse_context_t lctx;
926 enum location_error err;
928 lctx.data = xloc.u.block.ptr;
929 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
930 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
932 err = compute_location(&lctx, loc, NULL, frame);
933 if (err < 0)
935 loc->kind = loc_error;
936 loc->reg = err;
938 else if (loc->kind == loc_dwarf2_block)
940 unsigned* ptr = pool_alloc(&ctx->module->pool,
941 sizeof(unsigned) + xloc.u.block.size);
942 *ptr = xloc.u.block.size;
943 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
944 loc->offset = (unsigned long)ptr;
947 return TRUE;
950 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
951 const dwarf2_debug_info_t* di)
953 struct attribute attr;
954 dwarf2_debug_info_t* type;
956 if (!dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
957 return NULL;
958 if (!(type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue)))
960 FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
961 return NULL;
963 if (!type->symt)
965 /* load the debug info entity */
966 dwarf2_load_one_entry(ctx, type);
967 if (!type->symt)
968 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
970 return type->symt;
973 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
975 char* last;
976 struct attribute diname;
977 struct attribute spec;
979 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
980 if (!ctx->cpp_name)
981 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
982 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
983 strcpy(last, name);
985 /* if the di is a definition, but has also a (previous) declaration, then scope must
986 * be gotten from declaration not definition
988 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
990 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
991 if (!di)
993 FIXME("Should have found the debug info entry\n");
994 return NULL;
998 for (di = di->parent; di; di = di->parent)
1000 switch (di->abbrev->tag)
1002 case DW_TAG_namespace:
1003 case DW_TAG_structure_type:
1004 case DW_TAG_class_type:
1005 case DW_TAG_interface_type:
1006 case DW_TAG_union_type:
1007 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
1009 size_t len = strlen(diname.u.string);
1010 last -= 2 + len;
1011 if (last < ctx->cpp_name) return NULL;
1012 memcpy(last, diname.u.string, len);
1013 last[len] = last[len + 1] = ':';
1015 break;
1016 default:
1017 break;
1020 return last;
1023 /******************************************************************
1024 * dwarf2_read_range
1026 * read a range for a given debug_info (either using AT_range attribute, in which
1027 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1028 * in all cases, range is relative to beginning of compilation unit
1030 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1031 unsigned long* plow, unsigned long* phigh)
1033 struct attribute range;
1035 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1037 dwarf2_traverse_context_t traverse;
1038 unsigned long low, high;
1040 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1041 traverse.end_data = ctx->sections[section_ranges].address +
1042 ctx->sections[section_ranges].size;
1043 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1045 *plow = ULONG_MAX;
1046 *phigh = 0;
1047 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1049 low = dwarf2_parse_addr(&traverse);
1050 high = dwarf2_parse_addr(&traverse);
1051 if (low == 0 && high == 0) break;
1052 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1053 if (low < *plow) *plow = low;
1054 if (high > *phigh) *phigh = high;
1056 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1057 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1059 return TRUE;
1061 else
1063 struct attribute low_pc;
1064 struct attribute high_pc;
1066 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1067 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1068 return FALSE;
1069 *plow = low_pc.u.uvalue;
1070 *phigh = high_pc.u.uvalue;
1071 return TRUE;
1075 /******************************************************************
1076 * dwarf2_read_one_debug_info
1078 * Loads into memory one debug info entry, and recursively its children (if any)
1080 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1081 dwarf2_traverse_context_t* traverse,
1082 dwarf2_debug_info_t* parent_di,
1083 dwarf2_debug_info_t** pdi)
1085 const dwarf2_abbrev_entry_t*abbrev;
1086 unsigned long entry_code;
1087 unsigned long offset;
1088 dwarf2_debug_info_t* di;
1089 dwarf2_debug_info_t* child;
1090 dwarf2_debug_info_t** where;
1091 dwarf2_abbrev_entry_attr_t* attr;
1092 unsigned i;
1093 struct attribute sibling;
1095 offset = traverse->data - ctx->sections[ctx->section].address;
1096 entry_code = dwarf2_leb128_as_unsigned(traverse);
1097 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1098 if (!entry_code)
1100 *pdi = NULL;
1101 return TRUE;
1103 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1104 if (!abbrev)
1106 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1107 return FALSE;
1109 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1110 if (!di) return FALSE;
1111 di->abbrev = abbrev;
1112 di->symt = NULL;
1113 di->parent = parent_di;
1115 if (abbrev->num_attr)
1117 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1118 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1120 di->data[i] = traverse->data;
1121 dwarf2_swallow_attribute(traverse, attr);
1124 else di->data = NULL;
1125 if (abbrev->have_child)
1127 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1128 while (traverse->data < traverse->end_data)
1130 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1131 if (!child) break;
1132 where = vector_add(&di->children, &ctx->pool);
1133 if (!where) return FALSE;
1134 *where = child;
1137 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1138 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1140 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1141 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1142 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1144 *pdi = di;
1145 return TRUE;
1148 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1149 dwarf2_debug_info_t* di)
1151 struct attribute spec;
1153 while (di)
1155 if (di->abbrev->have_child)
1156 return &di->children;
1157 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1158 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1159 FIXME("Should have found the debug info entry\n");
1161 return NULL;
1164 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1165 dwarf2_debug_info_t* di)
1167 struct attribute name;
1168 struct attribute size;
1169 struct attribute encoding;
1170 enum BasicType bt;
1171 int cache_idx = -1;
1172 if (di->symt) return di->symt;
1174 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1176 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1177 name.u.string = NULL;
1178 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1179 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1181 switch (encoding.u.uvalue)
1183 case DW_ATE_void: bt = btVoid; break;
1184 case DW_ATE_address: bt = btULong; break;
1185 case DW_ATE_boolean: bt = btBool; break;
1186 case DW_ATE_complex_float: bt = btComplex; break;
1187 case DW_ATE_float: bt = btFloat; break;
1188 case DW_ATE_signed: bt = btInt; break;
1189 case DW_ATE_unsigned: bt = btUInt; break;
1190 case DW_ATE_signed_char: bt = btChar; break;
1191 case DW_ATE_unsigned_char: bt = btChar; break;
1192 default: bt = btNoType; break;
1194 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1195 switch (bt)
1197 case btVoid:
1198 assert(size.u.uvalue == 0);
1199 cache_idx = sc_void;
1200 break;
1201 case btInt:
1202 switch (size.u.uvalue)
1204 case 1: cache_idx = sc_int1; break;
1205 case 2: cache_idx = sc_int2; break;
1206 case 4: cache_idx = sc_int4; break;
1208 break;
1209 default: break;
1211 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1212 ctx->symt_cache[cache_idx] = di->symt;
1214 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1215 return di->symt;
1218 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1219 dwarf2_debug_info_t* di)
1221 struct symt* ref_type;
1222 struct attribute name;
1224 if (di->symt) return di->symt;
1226 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1228 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1229 ref_type = dwarf2_lookup_type(ctx, di);
1231 if (name.u.string)
1232 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1233 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1234 return di->symt;
1237 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1238 dwarf2_debug_info_t* di)
1240 struct symt* ref_type;
1241 struct attribute size;
1243 if (di->symt) return di->symt;
1245 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1247 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1248 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1250 ref_type = ctx->symt_cache[sc_void];
1251 assert(ref_type);
1253 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1254 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1255 return di->symt;
1258 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1259 dwarf2_debug_info_t* di)
1261 struct symt* ref_type;
1262 struct symt* idx_type = NULL;
1263 struct attribute min, max, cnt;
1264 dwarf2_debug_info_t* child;
1265 unsigned int i;
1266 const struct vector* children;
1268 if (di->symt) return di->symt;
1270 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1272 ref_type = dwarf2_lookup_type(ctx, di);
1274 if (!(children = dwarf2_get_di_children(ctx, di)))
1276 /* fake an array with unknown size */
1277 /* FIXME: int4 even on 64bit machines??? */
1278 idx_type = ctx->symt_cache[sc_int4];
1279 min.u.uvalue = 0;
1280 max.u.uvalue = -1;
1282 else for (i = 0; i < vector_length(children); i++)
1284 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1285 switch (child->abbrev->tag)
1287 case DW_TAG_subrange_type:
1288 idx_type = dwarf2_lookup_type(ctx, child);
1289 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1290 min.u.uvalue = 0;
1291 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1292 max.u.uvalue = 0;
1293 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1294 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1295 break;
1296 default:
1297 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1298 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1299 break;
1302 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1303 return di->symt;
1306 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1307 dwarf2_debug_info_t* di)
1309 struct symt* ref_type;
1311 if (di->symt) return di->symt;
1313 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1315 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1317 ref_type = ctx->symt_cache[sc_void];
1318 assert(ref_type);
1320 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1321 di->symt = ref_type;
1323 return ref_type;
1326 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1327 dwarf2_debug_info_t* di)
1329 struct symt* ref_type;
1331 if (di->symt) return di->symt;
1333 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1335 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1337 ref_type = ctx->symt_cache[sc_void];
1338 assert(ref_type);
1340 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1341 di->symt = ref_type;
1343 return ref_type;
1346 static struct symt* dwarf2_parse_unspecified_type(dwarf2_parse_context_t* ctx,
1347 dwarf2_debug_info_t* di)
1349 struct attribute name;
1350 struct attribute size;
1351 struct symt_basic *basic;
1353 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1355 if (di->symt) return di->symt;
1357 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1358 name.u.string = "void";
1359 size.u.uvalue = sizeof(void *);
1361 basic = symt_new_basic(ctx->module, btVoid, name.u.string, size.u.uvalue);
1362 di->symt = &basic->symt;
1364 if (!ctx->symt_cache[sc_void])
1365 ctx->symt_cache[sc_void] = di->symt;
1367 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1368 return di->symt;
1371 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1372 dwarf2_debug_info_t* di)
1374 struct symt* ref_type = NULL;
1376 if (di->symt) return di->symt;
1378 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1380 ref_type = dwarf2_lookup_type(ctx, di);
1381 /* FIXME: for now, we hard-wire C++ references to pointers */
1382 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1384 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1386 return di->symt;
1389 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1390 dwarf2_debug_info_t* di,
1391 struct symt_udt* parent)
1393 struct symt* elt_type;
1394 struct attribute name;
1395 struct attribute bit_size;
1396 struct attribute bit_offset;
1397 struct location loc;
1399 assert(parent);
1401 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1403 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1404 elt_type = dwarf2_lookup_type(ctx, di);
1405 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1407 if (loc.kind != loc_absolute)
1409 FIXME("Found register, while not expecting it\n");
1410 loc.offset = 0;
1412 else
1413 TRACE("found member_location at %s -> %lu\n",
1414 dwarf2_debug_ctx(ctx), loc.offset);
1416 else
1417 loc.offset = 0;
1418 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1419 bit_size.u.uvalue = 0;
1420 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1422 /* FIXME: we should only do this when implementation is LSB (which is
1423 * the case on i386 processors)
1425 struct attribute nbytes;
1426 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1428 DWORD64 size;
1429 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1430 (unsigned long)size : 0;
1432 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1434 else bit_offset.u.uvalue = 0;
1435 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1436 (loc.offset << 3) + bit_offset.u.uvalue,
1437 bit_size.u.uvalue);
1439 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1442 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1443 dwarf2_debug_info_t* di);
1445 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1446 dwarf2_debug_info_t* di,
1447 enum UdtKind udt)
1449 struct attribute name;
1450 struct attribute size;
1451 struct vector* children;
1452 dwarf2_debug_info_t*child;
1453 unsigned int i;
1455 if (di->symt) return di->symt;
1457 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1459 /* quirk... FIXME provide real support for anonymous UDTs */
1460 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1461 name.u.string = "zz_anon_zz";
1462 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1464 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1465 size.u.uvalue, udt)->symt;
1467 children = dwarf2_get_di_children(ctx, di);
1468 if (children) for (i = 0; i < vector_length(children); i++)
1470 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1472 switch (child->abbrev->tag)
1474 case DW_TAG_array_type:
1475 dwarf2_parse_array_type(ctx, di);
1476 break;
1477 case DW_TAG_member:
1478 /* FIXME: should I follow the sibling stuff ?? */
1479 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1480 break;
1481 case DW_TAG_enumeration_type:
1482 dwarf2_parse_enumeration_type(ctx, child);
1483 break;
1484 case DW_TAG_subprogram:
1485 dwarf2_parse_subprogram(ctx, child);
1486 break;
1487 case DW_TAG_const_type:
1488 dwarf2_parse_const_type(ctx, child);
1489 break;
1490 case DW_TAG_structure_type:
1491 case DW_TAG_class_type:
1492 case DW_TAG_union_type:
1493 case DW_TAG_typedef:
1494 /* FIXME: we need to handle nested udt definitions */
1495 case DW_TAG_inheritance:
1496 case DW_TAG_template_type_param:
1497 case DW_TAG_template_value_param:
1498 case DW_TAG_variable:
1499 case DW_TAG_imported_declaration:
1500 case DW_TAG_ptr_to_member_type:
1501 case DW_TAG_GNU_template_parameter_pack:
1502 case DW_TAG_GNU_formal_parameter_pack:
1503 /* FIXME: some C++ related stuff */
1504 break;
1505 default:
1506 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1507 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1508 break;
1512 return di->symt;
1515 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1516 dwarf2_debug_info_t* di,
1517 struct symt_enum* parent)
1519 struct attribute name;
1520 struct attribute value;
1522 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1524 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1525 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1526 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1528 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1531 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1532 dwarf2_debug_info_t* di)
1534 struct attribute name;
1535 struct attribute size;
1536 struct symt_basic* basetype;
1537 struct vector* children;
1538 dwarf2_debug_info_t*child;
1539 unsigned int i;
1541 if (di->symt) return di->symt;
1543 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1545 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1546 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1548 switch (size.u.uvalue) /* FIXME: that's wrong */
1550 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1551 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1552 default:
1553 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1556 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1558 children = dwarf2_get_di_children(ctx, di);
1559 /* FIXME: should we use the sibling stuff ?? */
1560 if (children) for (i = 0; i < vector_length(children); i++)
1562 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1564 switch (child->abbrev->tag)
1566 case DW_TAG_enumerator:
1567 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1568 break;
1569 default:
1570 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1571 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1574 return di->symt;
1577 /* structure used to pass information around when parsing a subprogram */
1578 typedef struct dwarf2_subprogram_s
1580 dwarf2_parse_context_t* ctx;
1581 struct symt_function* func;
1582 BOOL non_computed_variable;
1583 struct location frame;
1584 } dwarf2_subprogram_t;
1586 /******************************************************************
1587 * dwarf2_parse_variable
1589 * Parses any variable (parameter, local/global variable)
1591 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1592 struct symt_block* block,
1593 dwarf2_debug_info_t* di)
1595 struct symt* param_type;
1596 struct attribute name, value;
1597 struct location loc;
1598 BOOL is_pmt;
1600 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1602 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1603 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1605 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1606 /* cannot do much without the name, the functions below won't like it. */
1607 return;
1609 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1610 &loc, &subpgm->frame))
1612 struct attribute ext;
1614 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1615 name.u.string, loc.kind, loc.offset, loc.reg,
1616 dwarf2_debug_ctx(subpgm->ctx));
1618 switch (loc.kind)
1620 case loc_error:
1621 break;
1622 case loc_absolute:
1623 /* it's a global variable */
1624 /* FIXME: we don't handle its scope yet */
1625 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1626 ext.u.uvalue = 0;
1627 loc.offset += subpgm->ctx->load_offset;
1628 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1629 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1630 loc, 0, param_type);
1631 break;
1632 default:
1633 subpgm->non_computed_variable = TRUE;
1634 /* fall through */
1635 case loc_register:
1636 case loc_regrel:
1637 /* either a pmt/variable relative to frame pointer or
1638 * pmt/variable in a register
1640 assert(subpgm->func);
1641 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1642 is_pmt ? DataIsParam : DataIsLocal,
1643 &loc, block, param_type, name.u.string);
1644 break;
1647 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1649 VARIANT v;
1650 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1651 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1652 switch (value.form)
1654 case DW_FORM_data1:
1655 case DW_FORM_data2:
1656 case DW_FORM_data4:
1657 case DW_FORM_udata:
1658 case DW_FORM_addr:
1659 v.n1.n2.vt = VT_UI4;
1660 v.n1.n2.n3.lVal = value.u.uvalue;
1661 break;
1663 case DW_FORM_data8:
1664 v.n1.n2.vt = VT_UI8;
1665 v.n1.n2.n3.llVal = value.u.lluvalue;
1666 break;
1668 case DW_FORM_sdata:
1669 v.n1.n2.vt = VT_I4;
1670 v.n1.n2.n3.lVal = value.u.svalue;
1671 break;
1673 case DW_FORM_strp:
1674 case DW_FORM_string:
1675 /* FIXME: native doesn't report const strings from here !!
1676 * however, the value of the string is in the code somewhere
1678 v.n1.n2.vt = VT_I1 | VT_BYREF;
1679 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1680 break;
1682 case DW_FORM_block:
1683 case DW_FORM_block1:
1684 case DW_FORM_block2:
1685 case DW_FORM_block4:
1686 v.n1.n2.vt = VT_I4;
1687 switch (value.u.block.size)
1689 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1690 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1691 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1692 default:
1693 v.n1.n2.vt = VT_I1 | VT_BYREF;
1694 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1695 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1697 break;
1699 default:
1700 FIXME("Unsupported form for const value %s (%lx)\n",
1701 name.u.string, value.form);
1702 v.n1.n2.vt = VT_EMPTY;
1704 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1705 name.u.string, param_type, &v)->symt;
1707 else
1709 /* variable has been optimized away... report anyway */
1710 loc.kind = loc_error;
1711 loc.reg = loc_err_no_location;
1712 if (subpgm->func)
1714 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1715 is_pmt ? DataIsParam : DataIsLocal,
1716 &loc, block, param_type, name.u.string);
1718 else
1720 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1723 if (is_pmt && subpgm->func && subpgm->func->type)
1724 symt_add_function_signature_parameter(subpgm->ctx->module,
1725 (struct symt_function_signature*)subpgm->func->type,
1726 param_type);
1728 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1731 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1732 const dwarf2_debug_info_t* di)
1734 struct attribute name;
1735 struct attribute low_pc;
1736 struct location loc;
1738 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1740 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1741 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1742 name.u.string = NULL;
1744 loc.kind = loc_absolute;
1745 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1746 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1747 &loc, name.u.string);
1750 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1751 struct symt_block* parent_block,
1752 dwarf2_debug_info_t* di);
1754 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1755 dwarf2_debug_info_t* di);
1757 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1758 struct symt_block* parent_block,
1759 dwarf2_debug_info_t* di)
1761 struct symt_block* block;
1762 unsigned long low_pc, high_pc;
1763 struct vector* children;
1764 dwarf2_debug_info_t*child;
1765 unsigned int i;
1767 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1769 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1771 FIXME("cannot read range\n");
1772 return;
1775 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1776 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1777 high_pc - low_pc);
1779 children = dwarf2_get_di_children(subpgm->ctx, di);
1780 if (children) for (i = 0; i < vector_length(children); i++)
1782 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1784 switch (child->abbrev->tag)
1786 case DW_TAG_formal_parameter:
1787 case DW_TAG_variable:
1788 dwarf2_parse_variable(subpgm, block, child);
1789 break;
1790 case DW_TAG_lexical_block:
1791 dwarf2_parse_subprogram_block(subpgm, block, child);
1792 break;
1793 case DW_TAG_inlined_subroutine:
1794 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1795 break;
1796 case DW_TAG_label:
1797 dwarf2_parse_subprogram_label(subpgm, child);
1798 break;
1799 case DW_TAG_GNU_call_site:
1800 /* this isn't properly supported by dbghelp interface. skip it for now */
1801 break;
1802 default:
1803 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1804 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1805 dwarf2_debug_di(di));
1808 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1811 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1812 struct symt_block* parent_block,
1813 dwarf2_debug_info_t* di)
1815 struct symt_block* block;
1816 unsigned long low_pc, high_pc;
1817 struct vector* children;
1818 dwarf2_debug_info_t*child;
1819 unsigned int i;
1821 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1823 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1825 FIXME("no range\n");
1826 return;
1829 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1830 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1831 high_pc - low_pc);
1833 children = dwarf2_get_di_children(subpgm->ctx, di);
1834 if (children) for (i = 0; i < vector_length(children); i++)
1836 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1838 switch (child->abbrev->tag)
1840 case DW_TAG_inlined_subroutine:
1841 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1842 break;
1843 case DW_TAG_variable:
1844 dwarf2_parse_variable(subpgm, block, child);
1845 break;
1846 case DW_TAG_pointer_type:
1847 dwarf2_parse_pointer_type(subpgm->ctx, di);
1848 break;
1849 case DW_TAG_subroutine_type:
1850 dwarf2_parse_subroutine_type(subpgm->ctx, di);
1851 break;
1852 case DW_TAG_const_type:
1853 dwarf2_parse_const_type(subpgm->ctx, di);
1854 break;
1855 case DW_TAG_lexical_block:
1856 dwarf2_parse_subprogram_block(subpgm, block, child);
1857 break;
1858 case DW_TAG_subprogram:
1859 /* FIXME: likely a declaration (to be checked)
1860 * skip it for now
1862 break;
1863 case DW_TAG_formal_parameter:
1864 /* FIXME: likely elements for exception handling (GCC flavor)
1865 * Skip it for now
1867 break;
1868 case DW_TAG_imported_module:
1869 /* C++ stuff to be silenced (for now) */
1870 break;
1871 case DW_TAG_GNU_call_site:
1872 /* this isn't properly supported by dbghelp interface. skip it for now */
1873 break;
1874 case DW_TAG_label:
1875 dwarf2_parse_subprogram_label(subpgm, child);
1876 break;
1877 case DW_TAG_class_type:
1878 case DW_TAG_structure_type:
1879 case DW_TAG_union_type:
1880 case DW_TAG_enumeration_type:
1881 case DW_TAG_typedef:
1882 /* the type referred to will be loaded when we need it, so skip it */
1883 break;
1884 default:
1885 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1886 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1890 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1893 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1894 dwarf2_debug_info_t* di)
1896 struct attribute name;
1897 unsigned long low_pc, high_pc;
1898 struct attribute is_decl;
1899 struct attribute inline_flags;
1900 struct symt* ret_type;
1901 struct symt_function_signature* sig_type;
1902 dwarf2_subprogram_t subpgm;
1903 struct vector* children;
1904 dwarf2_debug_info_t* child;
1905 unsigned int i;
1907 if (di->symt) return di->symt;
1909 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1911 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1913 WARN("No name for function... dropping function\n");
1914 return NULL;
1916 /* if it's an abstract representation of an inline function, there should be
1917 * a concrete object that we'll handle
1919 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1920 inline_flags.u.uvalue != DW_INL_not_inlined)
1922 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1923 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1924 return NULL;
1927 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1928 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1930 /* it's a real declaration, skip it */
1931 return NULL;
1933 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1935 WARN("cannot get range for %s\n", name.u.string);
1936 return NULL;
1938 /* As functions (defined as inline assembly) get debug info with dwarf
1939 * (not the case for stabs), we just drop Wine's thunks here...
1940 * Actual thunks will be created in elf_module from the symbol table
1942 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1943 return NULL;
1944 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1946 ret_type = ctx->symt_cache[sc_void];
1947 assert(ret_type);
1949 /* FIXME: assuming C source code */
1950 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1951 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1952 dwarf2_get_cpp_name(ctx, di, name.u.string),
1953 ctx->load_offset + low_pc, high_pc - low_pc,
1954 &sig_type->symt);
1955 di->symt = &subpgm.func->symt;
1956 subpgm.ctx = ctx;
1957 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1958 &subpgm.frame, NULL))
1960 /* on stack !! */
1961 subpgm.frame.kind = loc_regrel;
1962 subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
1963 subpgm.frame.offset = 0;
1965 subpgm.non_computed_variable = FALSE;
1967 children = dwarf2_get_di_children(ctx, di);
1968 if (children) for (i = 0; i < vector_length(children); i++)
1970 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1972 switch (child->abbrev->tag)
1974 case DW_TAG_variable:
1975 case DW_TAG_formal_parameter:
1976 dwarf2_parse_variable(&subpgm, NULL, child);
1977 break;
1978 case DW_TAG_lexical_block:
1979 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1980 break;
1981 case DW_TAG_inlined_subroutine:
1982 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1983 break;
1984 case DW_TAG_pointer_type:
1985 dwarf2_parse_pointer_type(subpgm.ctx, di);
1986 break;
1987 case DW_TAG_const_type:
1988 dwarf2_parse_const_type(subpgm.ctx, di);
1989 break;
1990 case DW_TAG_subprogram:
1991 /* FIXME: likely a declaration (to be checked)
1992 * skip it for now
1994 break;
1995 case DW_TAG_label:
1996 dwarf2_parse_subprogram_label(&subpgm, child);
1997 break;
1998 case DW_TAG_class_type:
1999 case DW_TAG_structure_type:
2000 case DW_TAG_union_type:
2001 case DW_TAG_enumeration_type:
2002 case DW_TAG_typedef:
2003 /* the type referred to will be loaded when we need it, so skip it */
2004 break;
2005 case DW_TAG_unspecified_parameters:
2006 case DW_TAG_template_type_param:
2007 case DW_TAG_template_value_param:
2008 case DW_TAG_GNU_call_site:
2009 case DW_TAG_GNU_template_parameter_pack:
2010 case DW_TAG_GNU_formal_parameter_pack:
2011 /* FIXME: no support in dbghelp's internals so far */
2012 break;
2013 default:
2014 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
2015 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2019 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2021 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
2022 &subpgm.frame, NULL);
2024 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
2026 return di->symt;
2029 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
2030 dwarf2_debug_info_t* di)
2032 struct symt* ret_type;
2033 struct symt_function_signature* sig_type;
2034 struct vector* children;
2035 dwarf2_debug_info_t* child;
2036 unsigned int i;
2038 if (di->symt) return di->symt;
2040 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2042 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
2044 ret_type = ctx->symt_cache[sc_void];
2045 assert(ret_type);
2048 /* FIXME: assuming C source code */
2049 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
2051 children = dwarf2_get_di_children(ctx, di);
2052 if (children) for (i = 0; i < vector_length(children); i++)
2054 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2056 switch (child->abbrev->tag)
2058 case DW_TAG_formal_parameter:
2059 symt_add_function_signature_parameter(ctx->module, sig_type,
2060 dwarf2_lookup_type(ctx, child));
2061 break;
2062 case DW_TAG_unspecified_parameters:
2063 WARN("Unsupported unspecified parameters\n");
2064 break;
2068 return di->symt = &sig_type->symt;
2071 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
2072 dwarf2_debug_info_t* di)
2074 struct vector* children;
2075 dwarf2_debug_info_t* child;
2076 unsigned int i;
2078 if (di->symt) return;
2080 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2082 di->symt = ctx->symt_cache[sc_void];
2084 children = dwarf2_get_di_children(ctx, di);
2085 if (children) for (i = 0; i < vector_length(children); i++)
2087 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2088 dwarf2_load_one_entry(ctx, child);
2092 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2093 dwarf2_debug_info_t* di)
2095 switch (di->abbrev->tag)
2097 case DW_TAG_typedef:
2098 dwarf2_parse_typedef(ctx, di);
2099 break;
2100 case DW_TAG_base_type:
2101 dwarf2_parse_base_type(ctx, di);
2102 break;
2103 case DW_TAG_pointer_type:
2104 dwarf2_parse_pointer_type(ctx, di);
2105 break;
2106 case DW_TAG_class_type:
2107 dwarf2_parse_udt_type(ctx, di, UdtClass);
2108 break;
2109 case DW_TAG_structure_type:
2110 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2111 break;
2112 case DW_TAG_union_type:
2113 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2114 break;
2115 case DW_TAG_array_type:
2116 dwarf2_parse_array_type(ctx, di);
2117 break;
2118 case DW_TAG_const_type:
2119 dwarf2_parse_const_type(ctx, di);
2120 break;
2121 case DW_TAG_volatile_type:
2122 dwarf2_parse_volatile_type(ctx, di);
2123 break;
2124 case DW_TAG_unspecified_type:
2125 dwarf2_parse_unspecified_type(ctx, di);
2126 break;
2127 case DW_TAG_reference_type:
2128 dwarf2_parse_reference_type(ctx, di);
2129 break;
2130 case DW_TAG_enumeration_type:
2131 dwarf2_parse_enumeration_type(ctx, di);
2132 break;
2133 case DW_TAG_subprogram:
2134 dwarf2_parse_subprogram(ctx, di);
2135 break;
2136 case DW_TAG_subroutine_type:
2137 dwarf2_parse_subroutine_type(ctx, di);
2138 break;
2139 case DW_TAG_variable:
2141 dwarf2_subprogram_t subpgm;
2143 subpgm.ctx = ctx;
2144 subpgm.func = NULL;
2145 subpgm.frame.kind = loc_absolute;
2146 subpgm.frame.offset = 0;
2147 subpgm.frame.reg = Wine_DW_no_register;
2148 dwarf2_parse_variable(&subpgm, NULL, di);
2150 break;
2151 case DW_TAG_namespace:
2152 dwarf2_parse_namespace(ctx, di);
2153 break;
2154 /* silence a couple of C++ defines */
2155 case DW_TAG_imported_module:
2156 case DW_TAG_imported_declaration:
2157 case DW_TAG_ptr_to_member_type:
2158 break;
2159 default:
2160 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2161 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2165 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2166 const struct vector* v, unsigned file, unsigned line)
2168 struct symt_function* func;
2169 struct symt_ht* symt;
2170 unsigned* psrc;
2172 if (!file || !(psrc = vector_at(v, file - 1))) return;
2174 TRACE("%s %lx %s %u\n",
2175 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2176 if (!(symt = symt_find_nearest(module, address)) ||
2177 symt->symt.tag != SymTagFunction) return;
2178 func = (struct symt_function*)symt;
2179 symt_add_func_line(module, func, *psrc, line, address - func->address);
2182 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2183 dwarf2_parse_context_t* ctx,
2184 const char* compile_dir,
2185 unsigned long offset)
2187 dwarf2_traverse_context_t traverse;
2188 unsigned long length;
2189 unsigned insn_size, default_stmt;
2190 unsigned line_range, opcode_base;
2191 int line_base;
2192 const unsigned char* opcode_len;
2193 struct vector dirs;
2194 struct vector files;
2195 const char** p;
2197 /* section with line numbers stripped */
2198 if (sections[section_line].address == IMAGE_NO_MAP)
2199 return FALSE;
2201 if (offset + 4 > sections[section_line].size)
2203 WARN("out of bounds offset\n");
2204 return FALSE;
2206 traverse.data = sections[section_line].address + offset;
2207 traverse.end_data = traverse.data + 4;
2208 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2210 length = dwarf2_parse_u4(&traverse);
2211 traverse.end_data = sections[section_line].address + offset + length;
2213 if (offset + 4 + length > sections[section_line].size)
2215 WARN("out of bounds header\n");
2216 return FALSE;
2218 dwarf2_parse_u2(&traverse); /* version */
2219 dwarf2_parse_u4(&traverse); /* header_len */
2220 insn_size = dwarf2_parse_byte(&traverse);
2221 default_stmt = dwarf2_parse_byte(&traverse);
2222 line_base = (signed char)dwarf2_parse_byte(&traverse);
2223 line_range = dwarf2_parse_byte(&traverse);
2224 opcode_base = dwarf2_parse_byte(&traverse);
2226 opcode_len = traverse.data;
2227 traverse.data += opcode_base - 1;
2229 vector_init(&dirs, sizeof(const char*), 4);
2230 p = vector_add(&dirs, &ctx->pool);
2231 *p = compile_dir ? compile_dir : ".";
2232 while (*traverse.data)
2234 const char* rel = (const char*)traverse.data;
2235 unsigned rellen = strlen(rel);
2236 TRACE("Got include %s\n", rel);
2237 traverse.data += rellen + 1;
2238 p = vector_add(&dirs, &ctx->pool);
2240 if (*rel == '/' || !compile_dir)
2241 *p = rel;
2242 else
2244 /* include directory relative to compile directory */
2245 unsigned baselen = strlen(compile_dir);
2246 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2247 strcpy(tmp, compile_dir);
2248 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2249 strcpy(&tmp[baselen], rel);
2250 *p = tmp;
2254 traverse.data++;
2256 vector_init(&files, sizeof(unsigned), 16);
2257 while (*traverse.data)
2259 unsigned int dir_index, mod_time;
2260 const char* name;
2261 const char* dir;
2262 unsigned* psrc;
2264 name = (const char*)traverse.data;
2265 traverse.data += strlen(name) + 1;
2266 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2267 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2268 length = dwarf2_leb128_as_unsigned(&traverse);
2269 dir = *(const char**)vector_at(&dirs, dir_index);
2270 TRACE("Got file %s/%s (%u,%lu)\n", dir, name, mod_time, length);
2271 psrc = vector_add(&files, &ctx->pool);
2272 *psrc = source_new(ctx->module, dir, name);
2274 traverse.data++;
2276 while (traverse.data < traverse.end_data)
2278 unsigned long address = 0;
2279 unsigned file = 1;
2280 unsigned line = 1;
2281 unsigned is_stmt = default_stmt;
2282 BOOL end_sequence = FALSE;
2283 unsigned opcode, extopcode, i;
2285 while (!end_sequence)
2287 opcode = dwarf2_parse_byte(&traverse);
2288 TRACE("Got opcode %x\n", opcode);
2290 if (opcode >= opcode_base)
2292 unsigned delta = opcode - opcode_base;
2294 address += (delta / line_range) * insn_size;
2295 line += line_base + (delta % line_range);
2296 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2298 else
2300 switch (opcode)
2302 case DW_LNS_copy:
2303 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2304 break;
2305 case DW_LNS_advance_pc:
2306 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2307 break;
2308 case DW_LNS_advance_line:
2309 line += dwarf2_leb128_as_signed(&traverse);
2310 break;
2311 case DW_LNS_set_file:
2312 file = dwarf2_leb128_as_unsigned(&traverse);
2313 break;
2314 case DW_LNS_set_column:
2315 dwarf2_leb128_as_unsigned(&traverse);
2316 break;
2317 case DW_LNS_negate_stmt:
2318 is_stmt = !is_stmt;
2319 break;
2320 case DW_LNS_set_basic_block:
2321 break;
2322 case DW_LNS_const_add_pc:
2323 address += ((255 - opcode_base) / line_range) * insn_size;
2324 break;
2325 case DW_LNS_fixed_advance_pc:
2326 address += dwarf2_parse_u2(&traverse);
2327 break;
2328 case DW_LNS_extended_op:
2329 dwarf2_leb128_as_unsigned(&traverse);
2330 extopcode = dwarf2_parse_byte(&traverse);
2331 switch (extopcode)
2333 case DW_LNE_end_sequence:
2334 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2335 end_sequence = TRUE;
2336 break;
2337 case DW_LNE_set_address:
2338 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2339 break;
2340 case DW_LNE_define_file:
2341 FIXME("not handled define file %s\n", traverse.data);
2342 traverse.data += strlen((const char *)traverse.data) + 1;
2343 dwarf2_leb128_as_unsigned(&traverse);
2344 dwarf2_leb128_as_unsigned(&traverse);
2345 dwarf2_leb128_as_unsigned(&traverse);
2346 break;
2347 case DW_LNE_set_discriminator:
2349 unsigned descr;
2351 descr = dwarf2_leb128_as_unsigned(&traverse);
2352 WARN("not handled discriminator %x\n", descr);
2354 break;
2355 default:
2356 FIXME("Unsupported extended opcode %x\n", extopcode);
2357 break;
2359 break;
2360 default:
2361 WARN("Unsupported opcode %x\n", opcode);
2362 for (i = 0; i < opcode_len[opcode]; i++)
2363 dwarf2_leb128_as_unsigned(&traverse);
2364 break;
2369 return TRUE;
2372 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2373 struct module* module,
2374 const struct elf_thunk_area* thunks,
2375 dwarf2_traverse_context_t* mod_ctx,
2376 unsigned long load_offset)
2378 dwarf2_parse_context_t ctx;
2379 dwarf2_traverse_context_t abbrev_ctx;
2380 dwarf2_debug_info_t* di;
2381 dwarf2_traverse_context_t cu_ctx;
2382 const unsigned char* comp_unit_start = mod_ctx->data;
2383 unsigned long cu_length;
2384 unsigned short cu_version;
2385 unsigned long cu_abbrev_offset;
2386 BOOL ret = FALSE;
2388 cu_length = dwarf2_parse_u4(mod_ctx);
2389 cu_ctx.data = mod_ctx->data;
2390 cu_ctx.end_data = mod_ctx->data + cu_length;
2391 mod_ctx->data += cu_length;
2392 cu_version = dwarf2_parse_u2(&cu_ctx);
2393 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2394 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2396 TRACE("Compilation Unit Header found at 0x%x:\n",
2397 (int)(comp_unit_start - sections[section_debug].address));
2398 TRACE("- length: %lu\n", cu_length);
2399 TRACE("- version: %u\n", cu_version);
2400 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2401 TRACE("- word_size: %u\n", cu_ctx.word_size);
2403 if (cu_version != 2)
2405 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2406 cu_version);
2407 return FALSE;
2410 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2411 mod_ctx->word_size = cu_ctx.word_size;
2413 pool_init(&ctx.pool, 65536);
2414 ctx.sections = sections;
2415 ctx.section = section_debug;
2416 ctx.module = module;
2417 ctx.thunks = thunks;
2418 ctx.load_offset = load_offset;
2419 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2420 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2421 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2422 ctx.cpp_name = NULL;
2424 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2425 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2426 abbrev_ctx.word_size = cu_ctx.word_size;
2427 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2429 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2430 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2432 if (di->abbrev->tag == DW_TAG_compile_unit)
2434 struct attribute name;
2435 struct vector* children;
2436 dwarf2_debug_info_t* child = NULL;
2437 unsigned int i;
2438 struct attribute stmt_list, low_pc;
2439 struct attribute comp_dir;
2441 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2442 name.u.string = NULL;
2444 /* get working directory of current compilation unit */
2445 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2446 comp_dir.u.string = NULL;
2448 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2449 low_pc.u.uvalue = 0;
2450 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2451 source_new(module, comp_dir.u.string, name.u.string));
2452 di->symt = &ctx.compiland->symt;
2453 children = dwarf2_get_di_children(&ctx, di);
2454 if (children) for (i = 0; i < vector_length(children); i++)
2456 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2457 dwarf2_load_one_entry(&ctx, child);
2459 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2461 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2462 module->module.LineNumbers = TRUE;
2464 ret = TRUE;
2466 else FIXME("Should have a compilation unit here\n");
2467 pool_destroy(&ctx.pool);
2468 return ret;
2471 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2472 unsigned long ip, dwarf2_traverse_context_t* lctx)
2474 DWORD_PTR beg, end;
2475 const BYTE* ptr = start;
2476 DWORD len;
2478 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2480 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2481 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2482 if (!beg && !end) break;
2483 len = dwarf2_get_u2(ptr); ptr += 2;
2485 if (beg <= ip && ip < end)
2487 lctx->data = ptr;
2488 lctx->end_data = ptr + len;
2489 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2490 return TRUE;
2492 ptr += len;
2494 WARN("Couldn't find ip in location list\n");
2495 return FALSE;
2498 static enum location_error loc_compute_frame(struct process* pcs,
2499 const struct module_format* modfmt,
2500 const struct symt_function* func,
2501 DWORD_PTR ip, struct location* frame)
2503 struct symt** psym = NULL;
2504 struct location* pframe;
2505 dwarf2_traverse_context_t lctx;
2506 enum location_error err;
2507 unsigned int i;
2509 for (i=0; i<vector_length(&func->vchildren); i++)
2511 psym = vector_at(&func->vchildren, i);
2512 if ((*psym)->tag == SymTagCustom)
2514 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2516 /* First, recompute the frame information, if needed */
2517 switch (pframe->kind)
2519 case loc_regrel:
2520 case loc_register:
2521 *frame = *pframe;
2522 break;
2523 case loc_dwarf2_location_list:
2524 WARN("Searching loclist for %s\n", func->hash_elt.name);
2525 if (!dwarf2_lookup_loclist(modfmt,
2526 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2527 ip, &lctx))
2528 return loc_err_out_of_scope;
2529 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2530 if (frame->kind >= loc_user)
2532 WARN("Couldn't compute runtime frame location\n");
2533 return loc_err_too_complex;
2535 break;
2536 default:
2537 WARN("Unsupported frame kind %d\n", pframe->kind);
2538 return loc_err_internal;
2540 return 0;
2543 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2544 return loc_err_internal;
2547 enum reg_rule
2549 RULE_UNSET, /* not set at all */
2550 RULE_UNDEFINED, /* undefined value */
2551 RULE_SAME, /* same value as previous frame */
2552 RULE_CFA_OFFSET, /* stored at cfa offset */
2553 RULE_OTHER_REG, /* stored in other register */
2554 RULE_EXPRESSION, /* address specified by expression */
2555 RULE_VAL_EXPRESSION /* value specified by expression */
2558 /* make it large enough for all CPUs */
2559 #define NB_FRAME_REGS 64
2560 #define MAX_SAVED_STATES 16
2562 struct frame_state
2564 ULONG_PTR cfa_offset;
2565 unsigned char cfa_reg;
2566 enum reg_rule cfa_rule;
2567 enum reg_rule rules[NB_FRAME_REGS];
2568 ULONG_PTR regs[NB_FRAME_REGS];
2571 struct frame_info
2573 ULONG_PTR ip;
2574 ULONG_PTR code_align;
2575 LONG_PTR data_align;
2576 unsigned char retaddr_reg;
2577 unsigned char fde_encoding;
2578 unsigned char lsda_encoding;
2579 unsigned char signal_frame;
2580 unsigned char aug_z_format;
2581 unsigned char state_sp;
2582 struct frame_state state;
2583 struct frame_state state_stack[MAX_SAVED_STATES];
2586 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2588 ULONG_PTR base;
2590 if (encoding == DW_EH_PE_omit) return 0;
2592 switch (encoding & 0xf0)
2594 case DW_EH_PE_abs:
2595 base = 0;
2596 break;
2597 case DW_EH_PE_pcrel:
2598 base = (ULONG_PTR)ctx->data;
2599 break;
2600 default:
2601 FIXME("unsupported encoding %02x\n", encoding);
2602 return 0;
2605 switch (encoding & 0x0f)
2607 case DW_EH_PE_native:
2608 return base + dwarf2_parse_addr(ctx);
2609 case DW_EH_PE_leb128:
2610 return base + dwarf2_leb128_as_unsigned(ctx);
2611 case DW_EH_PE_data2:
2612 return base + dwarf2_parse_u2(ctx);
2613 case DW_EH_PE_data4:
2614 return base + dwarf2_parse_u4(ctx);
2615 case DW_EH_PE_data8:
2616 return base + dwarf2_parse_u8(ctx);
2617 case DW_EH_PE_signed|DW_EH_PE_leb128:
2618 return base + dwarf2_leb128_as_signed(ctx);
2619 case DW_EH_PE_signed|DW_EH_PE_data2:
2620 return base + (signed short)dwarf2_parse_u2(ctx);
2621 case DW_EH_PE_signed|DW_EH_PE_data4:
2622 return base + (signed int)dwarf2_parse_u4(ctx);
2623 case DW_EH_PE_signed|DW_EH_PE_data8:
2624 return base + (LONG64)dwarf2_parse_u8(ctx);
2625 default:
2626 FIXME("unsupported encoding %02x\n", encoding);
2627 return 0;
2631 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2633 unsigned char version;
2634 const char* augmentation;
2635 const unsigned char* end;
2636 ULONG_PTR len;
2638 memset(info, 0, sizeof(*info));
2639 info->lsda_encoding = DW_EH_PE_omit;
2640 info->aug_z_format = 0;
2642 /* parse the CIE first */
2643 version = dwarf2_parse_byte(ctx);
2644 if (version != 1 && version != 3)
2646 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2647 return FALSE;
2649 augmentation = (const char*)ctx->data;
2650 ctx->data += strlen(augmentation) + 1;
2652 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2653 info->data_align = dwarf2_leb128_as_signed(ctx);
2654 if (version == 1)
2655 info->retaddr_reg = dwarf2_parse_byte(ctx);
2656 else
2657 info->retaddr_reg = dwarf2_leb128_as_unsigned(ctx);
2658 info->state.cfa_rule = RULE_CFA_OFFSET;
2660 end = NULL;
2661 TRACE("\tparsing augmentation %s\n", augmentation);
2662 if (*augmentation) do
2664 switch (*augmentation)
2666 case 'z':
2667 len = dwarf2_leb128_as_unsigned(ctx);
2668 end = ctx->data + len;
2669 info->aug_z_format = 1;
2670 continue;
2671 case 'L':
2672 info->lsda_encoding = dwarf2_parse_byte(ctx);
2673 continue;
2674 case 'P':
2676 unsigned char encoding = dwarf2_parse_byte(ctx);
2677 /* throw away the indirect bit, as we don't care for the result */
2678 encoding &= ~DW_EH_PE_indirect;
2679 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2680 continue;
2682 case 'R':
2683 info->fde_encoding = dwarf2_parse_byte(ctx);
2684 continue;
2685 case 'S':
2686 info->signal_frame = 1;
2687 continue;
2689 FIXME("unknown augmentation '%c'\n", *augmentation);
2690 if (!end) return FALSE;
2691 break;
2692 } while (*++augmentation);
2693 if (end) ctx->data = end;
2694 return TRUE;
2697 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2698 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2699 struct frame_info* info, BOOL in_eh_frame)
2701 const unsigned char* ptr_blk;
2702 const unsigned char* cie_ptr;
2703 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2704 unsigned len, id;
2705 unsigned long start, range;
2706 unsigned cie_id;
2707 const BYTE* start_data = fde_ctx->data;
2709 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2710 /* skip 0-padding at beginning of section (alignment) */
2711 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
2713 if (dwarf2_parse_u4(fde_ctx))
2715 fde_ctx->data -= 4;
2716 break;
2719 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2721 /* find the FDE for address addr (skip CIE) */
2722 len = dwarf2_parse_u4(fde_ctx);
2723 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2724 ptr_blk = fde_ctx->data + len;
2725 id = dwarf2_parse_u4(fde_ctx);
2726 if (id == cie_id)
2728 last_cie_ptr = fde_ctx->data - 8;
2729 /* we need some bits out of the CIE in order to parse all contents */
2730 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2731 cie_ctx->data = fde_ctx->data;
2732 cie_ctx->end_data = ptr_blk;
2733 cie_ctx->word_size = fde_ctx->word_size;
2734 continue;
2736 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2737 if (cie_ptr != last_cie_ptr)
2739 last_cie_ptr = cie_ptr;
2740 cie_ctx->data = cie_ptr;
2741 cie_ctx->word_size = fde_ctx->word_size;
2742 cie_ctx->end_data = cie_ptr + 4;
2743 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2744 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2746 FIXME("wrong CIE pointer at %x from FDE %x\n",
2747 (unsigned)(cie_ptr - start_data),
2748 (unsigned)(fde_ctx->data - start_data));
2749 return FALSE;
2751 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2753 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2754 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2756 if (addr >= start && addr < start + range)
2758 /* reset the FDE context */
2759 fde_ctx->end_data = ptr_blk;
2761 info->ip = start;
2762 return TRUE;
2765 return FALSE;
2768 static int valid_reg(ULONG_PTR reg)
2770 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2771 return (reg < NB_FRAME_REGS);
2774 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2775 ULONG_PTR last_ip, struct frame_info *info)
2777 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2779 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2781 if (op & 0xc0)
2783 switch (op & 0xc0)
2785 case DW_CFA_advance_loc:
2787 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2788 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2789 info->ip += offset;
2790 break;
2792 case DW_CFA_offset:
2794 ULONG_PTR reg = op & 0x3f;
2795 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2796 if (!valid_reg(reg)) break;
2797 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2798 info->ip,
2799 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2800 offset);
2801 info->state.regs[reg] = offset;
2802 info->state.rules[reg] = RULE_CFA_OFFSET;
2803 break;
2805 case DW_CFA_restore:
2807 ULONG_PTR reg = op & 0x3f;
2808 if (!valid_reg(reg)) break;
2809 TRACE("%lx: DW_CFA_restore %s\n",
2810 info->ip,
2811 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2812 info->state.rules[reg] = RULE_UNSET;
2813 break;
2817 else switch (op)
2819 case DW_CFA_nop:
2820 break;
2821 case DW_CFA_set_loc:
2823 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2824 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2825 info->ip = loc;
2826 break;
2828 case DW_CFA_advance_loc1:
2830 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2831 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2832 info->ip += offset;
2833 break;
2835 case DW_CFA_advance_loc2:
2837 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2838 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2839 info->ip += offset;
2840 break;
2842 case DW_CFA_advance_loc4:
2844 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2845 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2846 info->ip += offset;
2847 break;
2849 case DW_CFA_offset_extended:
2850 case DW_CFA_offset_extended_sf:
2852 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2853 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2854 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2855 if (!valid_reg(reg)) break;
2856 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2857 info->ip,
2858 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2859 offset);
2860 info->state.regs[reg] = offset;
2861 info->state.rules[reg] = RULE_CFA_OFFSET;
2862 break;
2864 case DW_CFA_restore_extended:
2866 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2867 if (!valid_reg(reg)) break;
2868 TRACE("%lx: DW_CFA_restore_extended %s\n",
2869 info->ip,
2870 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2871 info->state.rules[reg] = RULE_UNSET;
2872 break;
2874 case DW_CFA_undefined:
2876 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2877 if (!valid_reg(reg)) break;
2878 TRACE("%lx: DW_CFA_undefined %s\n",
2879 info->ip,
2880 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2881 info->state.rules[reg] = RULE_UNDEFINED;
2882 break;
2884 case DW_CFA_same_value:
2886 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2887 if (!valid_reg(reg)) break;
2888 TRACE("%lx: DW_CFA_same_value %s\n",
2889 info->ip,
2890 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2891 info->state.regs[reg] = reg;
2892 info->state.rules[reg] = RULE_SAME;
2893 break;
2895 case DW_CFA_register:
2897 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2898 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2899 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2900 TRACE("%lx: DW_CFA_register %s == %s\n",
2901 info->ip,
2902 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2903 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2, TRUE)));
2904 info->state.regs[reg] = reg2;
2905 info->state.rules[reg] = RULE_OTHER_REG;
2906 break;
2908 case DW_CFA_remember_state:
2909 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2910 if (info->state_sp >= MAX_SAVED_STATES)
2911 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2912 else
2913 info->state_stack[info->state_sp++] = info->state;
2914 break;
2915 case DW_CFA_restore_state:
2916 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2917 if (!info->state_sp)
2918 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2919 else
2920 info->state = info->state_stack[--info->state_sp];
2921 break;
2922 case DW_CFA_def_cfa:
2923 case DW_CFA_def_cfa_sf:
2925 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2926 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2927 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2928 if (!valid_reg(reg)) break;
2929 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2930 info->ip,
2931 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2932 offset);
2933 info->state.cfa_reg = reg;
2934 info->state.cfa_offset = offset;
2935 info->state.cfa_rule = RULE_CFA_OFFSET;
2936 break;
2938 case DW_CFA_def_cfa_register:
2940 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2941 if (!valid_reg(reg)) break;
2942 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2943 info->ip,
2944 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2945 info->state.cfa_reg = reg;
2946 info->state.cfa_rule = RULE_CFA_OFFSET;
2947 break;
2949 case DW_CFA_def_cfa_offset:
2950 case DW_CFA_def_cfa_offset_sf:
2952 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2953 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2954 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
2955 info->state.cfa_offset = offset;
2956 info->state.cfa_rule = RULE_CFA_OFFSET;
2957 break;
2959 case DW_CFA_def_cfa_expression:
2961 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2962 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2963 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2964 info->state.cfa_offset = expr;
2965 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2966 ctx->data += len;
2967 break;
2969 case DW_CFA_expression:
2970 case DW_CFA_val_expression:
2972 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2973 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2974 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2975 if (!valid_reg(reg)) break;
2976 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2977 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2978 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2979 expr, expr + len);
2980 info->state.regs[reg] = expr;
2981 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2982 ctx->data += len;
2983 break;
2985 case DW_CFA_GNU_args_size:
2986 /* FIXME: should check that GCC is the compiler for this CU */
2988 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2989 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2990 /* ignored */
2991 break;
2993 default:
2994 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2995 break;
3000 /* retrieve a context register from its dwarf number */
3001 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
3003 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg, TRUE), sz;
3004 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
3006 if (sz != sizeof(ULONG_PTR))
3008 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
3009 return 0;
3011 return *ptr;
3014 /* set a context register from its dwarf number */
3015 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
3016 ULONG_PTR val, BOOL isdebuggee)
3018 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg, TRUE), sz;
3019 ULONG_PTR* ptr = dbghelp_current_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(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
3051 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst, TRUE), szdst;
3052 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc, TRUE), szsrc;
3053 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3054 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3056 if (szdst != szsrc)
3058 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3059 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3060 return;
3062 memcpy(ptrdst, ptrsrc, szdst);
3065 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3066 const unsigned char* zp, CONTEXT *context)
3068 dwarf2_traverse_context_t ctx;
3069 ULONG_PTR reg, sz, tmp, stack[64];
3070 int sp = -1;
3071 ULONG_PTR len;
3073 ctx.data = zp;
3074 ctx.end_data = zp + 4;
3075 len = dwarf2_leb128_as_unsigned(&ctx);
3076 ctx.end_data = ctx.data + len;
3077 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3079 while (ctx.data < ctx.end_data)
3081 unsigned char opcode = dwarf2_parse_byte(&ctx);
3083 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3084 stack[++sp] = opcode - DW_OP_lit0;
3085 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3086 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
3087 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3088 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
3089 else switch (opcode)
3091 case DW_OP_nop: break;
3092 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
3093 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3094 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3095 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3096 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3097 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3098 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3099 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3100 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3101 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3102 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3103 case DW_OP_deref:
3104 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
3106 ERR("Couldn't read memory at %lx\n", stack[sp]);
3107 tmp = 0;
3109 stack[sp] = tmp;
3110 break;
3111 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3112 case DW_OP_drop: sp--; break;
3113 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3114 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3115 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3116 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3117 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3118 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3119 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3120 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3121 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3122 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3123 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3124 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3125 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3126 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3127 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3128 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3129 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3130 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3131 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3132 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3133 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3134 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3135 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3136 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3137 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3138 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3139 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3140 case DW_OP_GNU_encoded_addr:
3141 tmp = dwarf2_parse_byte(&ctx);
3142 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3143 break;
3144 case DW_OP_regx:
3145 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
3146 break;
3147 case DW_OP_bregx:
3148 reg = dwarf2_leb128_as_unsigned(&ctx);
3149 tmp = dwarf2_leb128_as_signed(&ctx);
3150 stack[++sp] = get_context_reg(context, reg) + tmp;
3151 break;
3152 case DW_OP_deref_size:
3153 sz = dwarf2_parse_byte(&ctx);
3154 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3156 ERR("Couldn't read memory at %lx\n", stack[sp]);
3157 tmp = 0;
3159 /* do integral promotion */
3160 switch (sz)
3162 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3163 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3164 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3165 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3166 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3168 break;
3169 default:
3170 FIXME("unhandled opcode %02x\n", opcode);
3173 return stack[sp];
3176 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3177 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3179 unsigned int i;
3180 ULONG_PTR value;
3181 CONTEXT new_context = *context;
3183 switch (state->cfa_rule)
3185 case RULE_EXPRESSION:
3186 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3187 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
3189 WARN("Couldn't read memory at %p\n", (void*)*cfa);
3190 return;
3192 break;
3193 case RULE_VAL_EXPRESSION:
3194 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3195 break;
3196 default:
3197 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3198 break;
3200 if (!*cfa) return;
3202 for (i = 0; i < NB_FRAME_REGS; i++)
3204 switch (state->rules[i])
3206 case RULE_UNSET:
3207 case RULE_UNDEFINED:
3208 case RULE_SAME:
3209 break;
3210 case RULE_CFA_OFFSET:
3211 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3212 break;
3213 case RULE_OTHER_REG:
3214 copy_context_reg(&new_context, i, context, state->regs[i]);
3215 break;
3216 case RULE_EXPRESSION:
3217 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3218 set_context_reg(csw, &new_context, i, value, TRUE);
3219 break;
3220 case RULE_VAL_EXPRESSION:
3221 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3222 set_context_reg(csw, &new_context, i, value, FALSE);
3223 break;
3226 *context = new_context;
3229 /***********************************************************************
3230 * dwarf2_virtual_unwind
3233 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3235 struct module_pair pair;
3236 struct frame_info info;
3237 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3238 struct module_format* modfmt;
3239 const unsigned char* end;
3240 DWORD_PTR delta;
3242 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3243 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3244 !module_get_debug(&pair))
3245 return FALSE;
3246 modfmt = pair.effective->format_info[DFI_DWARF];
3247 if (!modfmt) return FALSE;
3248 memset(&info, 0, sizeof(info));
3249 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3250 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3251 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3252 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3253 * in this process the IMAGE section at a different address as the one expected by
3254 * the image
3256 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3257 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3258 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3260 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3261 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3262 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3263 delta = pair.effective->reloc_delta;
3264 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3266 TRACE("Couldn't find information for %lx\n", ip);
3267 return FALSE;
3271 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3272 ip, info.ip, info.code_align, info.data_align,
3273 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg, TRUE)));
3275 /* if at very beginning of function, return and use default unwinder */
3276 if (ip == info.ip) return FALSE;
3277 execute_cfa_instructions(&cie_ctx, ip, &info);
3279 if (info.aug_z_format) /* get length of augmentation data */
3281 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3282 end = fde_ctx.data + len;
3284 else end = NULL;
3285 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3286 if (end) fde_ctx.data = end;
3288 execute_cfa_instructions(&fde_ctx, ip, &info);
3290 /* if there is no information about retaddr, use default unwinder */
3291 if (info.state.rules[info.retaddr_reg] == RULE_UNSET) return FALSE;
3293 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3295 return TRUE;
3298 static void dwarf2_location_compute(struct process* pcs,
3299 const struct module_format* modfmt,
3300 const struct symt_function* func,
3301 struct location* loc)
3303 struct location frame;
3304 DWORD_PTR ip;
3305 int err;
3306 dwarf2_traverse_context_t lctx;
3308 if (!func->container || func->container->tag != SymTagCompiland)
3310 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3311 err = loc_err_internal;
3313 else
3315 /* instruction pointer relative to compiland's start */
3316 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3318 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3320 switch (loc->kind)
3322 case loc_dwarf2_location_list:
3323 /* Then, if the variable has a location list, find it !! */
3324 if (dwarf2_lookup_loclist(modfmt,
3325 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3326 ip, &lctx))
3327 goto do_compute;
3328 err = loc_err_out_of_scope;
3329 break;
3330 case loc_dwarf2_block:
3331 /* or if we have a copy of an existing block, get ready for it */
3333 unsigned* ptr = (unsigned*)loc->offset;
3335 lctx.data = (const BYTE*)(ptr + 1);
3336 lctx.end_data = lctx.data + *ptr;
3337 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3339 do_compute:
3340 /* now get the variable */
3341 err = compute_location(&lctx, loc, pcs->handle, &frame);
3342 break;
3343 case loc_register:
3344 case loc_regrel:
3345 /* nothing to do */
3346 break;
3347 default:
3348 WARN("Unsupported local kind %d\n", loc->kind);
3349 err = loc_err_internal;
3353 if (err < 0)
3355 loc->kind = loc_register;
3356 loc->reg = err;
3360 #ifdef HAVE_ZLIB
3361 static void *zalloc(void *priv, uInt items, uInt sz)
3363 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3366 static void zfree(void *priv, void *addr)
3368 HeapFree(GetProcessHeap(), 0, addr);
3371 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3372 const char* zsectname,
3373 struct image_section_map* ism)
3375 z_stream z;
3376 LARGE_INTEGER li;
3377 int res;
3378 BOOL ret = FALSE;
3380 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3381 size_t sz = image_get_map_size(ism);
3383 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
3385 ERR("invalid compressed section %s\n", zsectname);
3386 goto out;
3389 #ifdef WORDS_BIGENDIAN
3390 li.u.HighPart = *(DWORD*)&sect[4];
3391 li.u.LowPart = *(DWORD*)&sect[8];
3392 #else
3393 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
3394 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
3395 #endif
3397 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
3398 if (!addr)
3399 goto out;
3401 z.next_in = &sect[12];
3402 z.avail_in = sz - 12;
3403 z.opaque = NULL;
3404 z.zalloc = zalloc;
3405 z.zfree = zfree;
3407 res = inflateInit(&z);
3408 if (res != Z_OK)
3410 FIXME("inflateInit failed with %i / %s\n", res, z.msg);
3411 goto out_free;
3414 do {
3415 z.next_out = addr + z.total_out;
3416 z.avail_out = li.QuadPart - z.total_out;
3417 res = inflate(&z, Z_FINISH);
3418 } while (z.avail_in && res == Z_STREAM_END);
3420 if (res != Z_STREAM_END)
3422 FIXME("Decompression failed with %i / %s\n", res, z.msg);
3423 goto out_end;
3426 ret = TRUE;
3427 section->compressed = TRUE;
3428 section->address = addr;
3429 section->rva = image_get_map_rva(ism);
3430 section->size = z.total_out;
3432 out_end:
3433 inflateEnd(&z);
3434 out_free:
3435 if (!ret)
3436 HeapFree(GetProcessHeap(), 0, addr);
3437 out:
3438 image_unmap_section(ism);
3439 return ret;
3442 #endif
3444 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3445 const char* sectname, const char* zsectname,
3446 struct image_section_map* ism)
3448 struct image_section_map local_ism;
3450 if (!ism) ism = &local_ism;
3452 section->compressed = FALSE;
3453 if (image_find_section(fmap, sectname, ism))
3455 section->address = (const BYTE*)image_map_section(ism);
3456 section->size = image_get_map_size(ism);
3457 section->rva = image_get_map_rva(ism);
3458 return TRUE;
3461 section->address = NULL;
3462 section->size = 0;
3463 section->rva = 0;
3465 if (zsectname && image_find_section(fmap, zsectname, ism))
3467 #ifdef HAVE_ZLIB
3468 return dwarf2_init_zsection(section, zsectname, ism);
3469 #else
3470 FIXME("dbghelp not built with zlib, but compressed section found\n" );
3471 #endif
3474 return FALSE;
3477 static inline void dwarf2_fini_section(dwarf2_section_t* section)
3479 if (section->compressed)
3480 HeapFree(GetProcessHeap(), 0, (void*)section->address);
3483 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3485 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
3486 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
3487 HeapFree(GetProcessHeap(), 0, modfmt);
3490 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3491 const struct elf_thunk_area* thunks,
3492 struct image_file_map* fmap)
3494 dwarf2_section_t eh_frame, section[section_max];
3495 dwarf2_traverse_context_t mod_ctx;
3496 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3497 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3498 BOOL ret = TRUE;
3499 struct module_format* dwarf2_modfmt;
3501 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect);
3502 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
3503 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
3504 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
3505 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
3506 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
3508 /* to do anything useful we need either .eh_frame or .debug_info */
3509 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3510 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3512 ret = FALSE;
3513 goto leave;
3516 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3518 /* debug info might have a different base address than .so file
3519 * when elf file is prelinked after splitting off debug info
3520 * adjust symbol base addresses accordingly
3522 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3525 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3527 mod_ctx.data = section[section_debug].address;
3528 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3529 mod_ctx.word_size = 0; /* will be correctly set later on */
3531 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3532 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3533 if (!dwarf2_modfmt)
3535 ret = FALSE;
3536 goto leave;
3538 dwarf2_modfmt->module = module;
3539 dwarf2_modfmt->remove = dwarf2_module_remove;
3540 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3541 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3542 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3543 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3545 /* As we'll need later some sections' content, we won't unmap these
3546 * sections upon existing this function
3548 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
3549 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
3550 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3552 while (mod_ctx.data < mod_ctx.end_data)
3554 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3556 dwarf2_modfmt->module->module.SymType = SymDia;
3557 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3558 /* FIXME: we could have a finer grain here */
3559 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3560 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3561 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3562 dwarf2_modfmt->module->module.Publics = TRUE;
3564 /* set the word_size for eh_frame parsing */
3565 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8;
3567 leave:
3568 dwarf2_fini_section(&section[section_debug]);
3569 dwarf2_fini_section(&section[section_abbrev]);
3570 dwarf2_fini_section(&section[section_string]);
3571 dwarf2_fini_section(&section[section_line]);
3572 dwarf2_fini_section(&section[section_ranges]);
3574 image_unmap_section(&debug_sect);
3575 image_unmap_section(&debug_abbrev_sect);
3576 image_unmap_section(&debug_str_sect);
3577 image_unmap_section(&debug_line_sect);
3578 image_unmap_section(&debug_ranges_sect);
3579 if (!ret) image_unmap_section(&eh_frame_sect);
3581 return ret;