wined3d: Use a separate STATE_VDECL state handler in the GLSL pipeline.
[wine/multimedia.git] / dlls / dbghelp / dwarf.c
blobc54bfd305bf6ab1241fe54dc39841432fad6ca9a
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_ref_addr:
466 case DW_FORM_addr: step = ctx->word_size; break;
467 case DW_FORM_flag:
468 case DW_FORM_data1:
469 case DW_FORM_ref1: step = 1; break;
470 case DW_FORM_data2:
471 case DW_FORM_ref2: step = 2; break;
472 case DW_FORM_data4:
473 case DW_FORM_ref4:
474 case DW_FORM_strp: step = 4; break;
475 case DW_FORM_data8:
476 case DW_FORM_ref8: step = 8; break;
477 case DW_FORM_sdata:
478 case DW_FORM_ref_udata:
479 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
480 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
481 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
482 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
483 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
484 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
485 default:
486 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
487 return;
489 ctx->data += step;
492 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
493 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
494 const unsigned char* data,
495 struct attribute* attr)
497 attr->form = abbrev_attr->form;
498 switch (attr->form)
500 case DW_FORM_ref_addr:
501 case DW_FORM_addr:
502 attr->u.uvalue = dwarf2_get_addr(data,
503 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
504 TRACE("addr<0x%lx>\n", attr->u.uvalue);
505 break;
507 case DW_FORM_flag:
508 attr->u.uvalue = dwarf2_get_byte(data);
509 TRACE("flag<0x%lx>\n", attr->u.uvalue);
510 break;
512 case DW_FORM_data1:
513 attr->u.uvalue = dwarf2_get_byte(data);
514 TRACE("data1<%lu>\n", attr->u.uvalue);
515 break;
517 case DW_FORM_data2:
518 attr->u.uvalue = dwarf2_get_u2(data);
519 TRACE("data2<%lu>\n", attr->u.uvalue);
520 break;
522 case DW_FORM_data4:
523 attr->u.uvalue = dwarf2_get_u4(data);
524 TRACE("data4<%lu>\n", attr->u.uvalue);
525 break;
527 case DW_FORM_data8:
528 attr->u.lluvalue = dwarf2_get_u8(data);
529 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
530 break;
532 case DW_FORM_ref1:
533 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
534 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
535 break;
537 case DW_FORM_ref2:
538 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
539 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
540 break;
542 case DW_FORM_ref4:
543 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
544 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
545 break;
547 case DW_FORM_ref8:
548 FIXME("Unhandled 64-bit support\n");
549 break;
551 case DW_FORM_sdata:
552 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
553 break;
555 case DW_FORM_ref_udata:
556 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
557 break;
559 case DW_FORM_udata:
560 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
561 break;
563 case DW_FORM_string:
564 attr->u.string = (const char *)data;
565 TRACE("string<%s>\n", attr->u.string);
566 break;
568 case DW_FORM_strp:
570 unsigned long offset = dwarf2_get_u4(data);
571 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
573 TRACE("strp<%s>\n", attr->u.string);
574 break;
576 case DW_FORM_block:
577 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
578 break;
580 case DW_FORM_block1:
581 attr->u.block.size = dwarf2_get_byte(data);
582 attr->u.block.ptr = data + 1;
583 break;
585 case DW_FORM_block2:
586 attr->u.block.size = dwarf2_get_u2(data);
587 attr->u.block.ptr = data + 2;
588 break;
590 case DW_FORM_block4:
591 attr->u.block.size = dwarf2_get_u4(data);
592 attr->u.block.ptr = data + 4;
593 break;
595 default:
596 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
597 break;
601 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
602 const dwarf2_debug_info_t* di,
603 unsigned at, struct attribute* attr)
605 unsigned i, refidx = 0;
606 dwarf2_abbrev_entry_attr_t* abbrev_attr;
607 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
609 attr->gotten_from = attr_direct;
610 while (di)
612 ref_abbrev_attr = NULL;
613 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
615 if (abbrev_attr->attribute == at)
617 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
618 return TRUE;
620 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
621 abbrev_attr->attribute == DW_AT_specification) &&
622 at != DW_AT_sibling)
624 if (ref_abbrev_attr)
625 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
626 ref_abbrev_attr = abbrev_attr;
627 refidx = i;
628 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
629 attr_abstract_origin : attr_specification;
632 /* do we have either an abstract origin or a specification debug entry to look into ? */
633 if (!ref_abbrev_attr) break;
634 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
635 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
636 FIXME("Should have found the debug info entry\n");
638 return FALSE;
641 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
643 #define Wine_DW_no_register 0x7FFFFFFF
645 static unsigned dwarf2_map_register(int regno)
647 if (regno == Wine_DW_no_register)
649 FIXME("What the heck map reg 0x%x\n",regno);
650 return 0;
652 return dbghelp_current_cpu->map_dwarf_register(regno);
655 static enum location_error
656 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
657 HANDLE hproc, const struct location* frame)
659 DWORD_PTR tmp, stack[64];
660 unsigned stk;
661 unsigned char op;
662 BOOL piece_found = FALSE;
664 stack[stk = 0] = 0;
666 loc->kind = loc_absolute;
667 loc->reg = Wine_DW_no_register;
669 while (ctx->data < ctx->end_data)
671 op = dwarf2_parse_byte(ctx);
673 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
674 stack[++stk] = op - DW_OP_lit0;
675 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
677 /* dbghelp APIs don't know how to cope with this anyway
678 * (for example 'long long' stored in two registers)
679 * FIXME: We should tell winedbg how to deal with it (sigh)
681 if (!piece_found)
683 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0);
684 if (loc->reg != Wine_DW_no_register)
685 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
686 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
687 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
688 loc->reg = cvreg;
690 loc->kind = loc_register;
692 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
694 /* dbghelp APIs don't know how to cope with this anyway
695 * (for example 'long long' stored in two registers)
696 * FIXME: We should tell winedbg how to deal with it (sigh)
698 if (!piece_found)
700 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0);
701 if (loc->reg != Wine_DW_no_register)
702 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
703 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
704 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
705 loc->reg = cvreg;
707 stack[++stk] = dwarf2_leb128_as_signed(ctx);
708 loc->kind = loc_regrel;
710 else switch (op)
712 case DW_OP_nop: break;
713 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
714 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
715 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
716 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
717 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
718 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
719 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
720 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
721 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
722 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
723 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
724 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
725 case DW_OP_drop: stk--; break;
726 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
727 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
728 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
729 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
730 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
731 case DW_OP_neg: stack[stk] = -stack[stk]; break;
732 case DW_OP_not: stack[stk] = ~stack[stk]; break;
733 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
734 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
735 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
736 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
737 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
738 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
739 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
740 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
741 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
742 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
743 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
744 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
745 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
746 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
747 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
748 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
749 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
750 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
751 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
752 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
753 case DW_OP_regx:
754 tmp = dwarf2_leb128_as_unsigned(ctx);
755 if (!piece_found)
757 if (loc->reg != Wine_DW_no_register)
758 FIXME("Only supporting one reg\n");
759 loc->reg = dwarf2_map_register(tmp);
761 loc->kind = loc_register;
762 break;
763 case DW_OP_bregx:
764 tmp = dwarf2_leb128_as_unsigned(ctx);
765 if (loc->reg != Wine_DW_no_register)
766 FIXME("Only supporting one regx\n");
767 loc->reg = dwarf2_map_register(tmp);
768 stack[++stk] = dwarf2_leb128_as_signed(ctx);
769 loc->kind = loc_regrel;
770 break;
771 case DW_OP_fbreg:
772 if (loc->reg != Wine_DW_no_register)
773 FIXME("Only supporting one reg (%s/%d -> -2)\n",
774 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
775 if (frame && frame->kind == loc_register)
777 loc->kind = loc_regrel;
778 loc->reg = frame->reg;
779 stack[++stk] = dwarf2_leb128_as_signed(ctx);
781 else if (frame && frame->kind == loc_regrel)
783 loc->kind = loc_regrel;
784 loc->reg = frame->reg;
785 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
787 else
789 /* FIXME: this could be later optimized by not recomputing
790 * this very location expression
792 loc->kind = loc_dwarf2_block;
793 stack[++stk] = dwarf2_leb128_as_signed(ctx);
795 break;
796 case DW_OP_piece:
798 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
799 WARN("Not handling OP_piece (size=%d)\n", sz);
800 piece_found = TRUE;
802 break;
803 case DW_OP_deref:
804 if (!stk)
806 FIXME("Unexpected empty stack\n");
807 return loc_err_internal;
809 if (loc->reg != Wine_DW_no_register)
811 WARN("Too complex expression for deref\n");
812 return loc_err_too_complex;
814 if (hproc)
816 DWORD_PTR addr = stack[stk--];
817 DWORD_PTR deref;
819 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
821 WARN("Couldn't read memory at %lx\n", addr);
822 return loc_err_cant_read;
824 stack[++stk] = deref;
826 else
828 loc->kind = loc_dwarf2_block;
830 break;
831 case DW_OP_deref_size:
832 if (!stk)
834 FIXME("Unexpected empty stack\n");
835 return loc_err_internal;
837 if (loc->reg != Wine_DW_no_register)
839 WARN("Too complex expression for deref\n");
840 return loc_err_too_complex;
842 if (hproc)
844 DWORD_PTR addr = stack[stk--];
845 BYTE derefsize = dwarf2_parse_byte(ctx);
846 DWORD64 deref;
848 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
850 WARN("Couldn't read memory at %lx\n", addr);
851 return loc_err_cant_read;
854 switch (derefsize)
856 case 1: stack[++stk] = *(unsigned char*)&deref; break;
857 case 2: stack[++stk] = *(unsigned short*)&deref; break;
858 case 4: stack[++stk] = *(DWORD*)&deref; break;
859 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
862 else
864 dwarf2_parse_byte(ctx);
865 loc->kind = loc_dwarf2_block;
867 break;
868 case DW_OP_stack_value:
869 /* Expected behaviour is that this is the last instruction of this
870 * expression and just the "top of stack" value should be put to loc->offset. */
871 break;
872 default:
873 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
874 FIXME("Unhandled attr op: %x\n", op);
875 /* FIXME else unhandled extension */
876 return loc_err_internal;
879 loc->offset = stack[stk];
880 return 0;
883 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
884 const dwarf2_debug_info_t* di,
885 unsigned long dw,
886 struct location* loc,
887 const struct location* frame)
889 struct attribute xloc;
891 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
893 switch (xloc.form)
895 case DW_FORM_data1: case DW_FORM_data2:
896 case DW_FORM_udata: case DW_FORM_sdata:
897 loc->kind = loc_absolute;
898 loc->reg = 0;
899 loc->offset = xloc.u.uvalue;
900 return TRUE;
901 case DW_FORM_data4: case DW_FORM_data8:
902 loc->kind = loc_dwarf2_location_list;
903 loc->reg = Wine_DW_no_register;
904 loc->offset = xloc.u.uvalue;
905 return TRUE;
906 case DW_FORM_block:
907 case DW_FORM_block1:
908 case DW_FORM_block2:
909 case DW_FORM_block4:
910 break;
911 default: FIXME("Unsupported yet form %lx\n", xloc.form);
912 return FALSE;
915 /* assume we have a block form */
917 if (xloc.u.block.size)
919 dwarf2_traverse_context_t lctx;
920 enum location_error err;
922 lctx.data = xloc.u.block.ptr;
923 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
924 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
926 err = compute_location(&lctx, loc, NULL, frame);
927 if (err < 0)
929 loc->kind = loc_error;
930 loc->reg = err;
932 else if (loc->kind == loc_dwarf2_block)
934 unsigned* ptr = pool_alloc(&ctx->module->pool,
935 sizeof(unsigned) + xloc.u.block.size);
936 *ptr = xloc.u.block.size;
937 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
938 loc->offset = (unsigned long)ptr;
941 return TRUE;
944 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
945 const dwarf2_debug_info_t* di)
947 struct attribute attr;
948 dwarf2_debug_info_t* type;
950 if (!dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
951 return NULL;
952 if (!(type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue)))
954 FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
955 return NULL;
957 if (!type->symt)
959 /* load the debug info entity */
960 dwarf2_load_one_entry(ctx, type);
961 if (!type->symt)
962 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
964 return type->symt;
967 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
969 char* last;
970 struct attribute diname;
971 struct attribute spec;
973 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
974 if (!ctx->cpp_name)
975 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
976 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
977 strcpy(last, name);
979 /* if the di is a definition, but has also a (previous) declaration, then scope must
980 * be gotten from declaration not definition
982 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
984 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
985 if (!di)
987 FIXME("Should have found the debug info entry\n");
988 return NULL;
992 for (di = di->parent; di; di = di->parent)
994 switch (di->abbrev->tag)
996 case DW_TAG_namespace:
997 case DW_TAG_structure_type:
998 case DW_TAG_class_type:
999 case DW_TAG_interface_type:
1000 case DW_TAG_union_type:
1001 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
1003 size_t len = strlen(diname.u.string);
1004 last -= 2 + len;
1005 if (last < ctx->cpp_name) return NULL;
1006 memcpy(last, diname.u.string, len);
1007 last[len] = last[len + 1] = ':';
1009 break;
1010 default:
1011 break;
1014 return last;
1017 /******************************************************************
1018 * dwarf2_read_range
1020 * read a range for a given debug_info (either using AT_range attribute, in which
1021 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1022 * in all cases, range is relative to beginning of compilation unit
1024 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1025 unsigned long* plow, unsigned long* phigh)
1027 struct attribute range;
1029 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1031 dwarf2_traverse_context_t traverse;
1032 unsigned long low, high;
1034 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1035 traverse.end_data = ctx->sections[section_ranges].address +
1036 ctx->sections[section_ranges].size;
1037 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1039 *plow = ULONG_MAX;
1040 *phigh = 0;
1041 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1043 low = dwarf2_parse_addr(&traverse);
1044 high = dwarf2_parse_addr(&traverse);
1045 if (low == 0 && high == 0) break;
1046 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1047 if (low < *plow) *plow = low;
1048 if (high > *phigh) *phigh = high;
1050 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1051 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1053 return TRUE;
1055 else
1057 struct attribute low_pc;
1058 struct attribute high_pc;
1060 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1061 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1062 return FALSE;
1063 *plow = low_pc.u.uvalue;
1064 *phigh = high_pc.u.uvalue;
1065 return TRUE;
1069 /******************************************************************
1070 * dwarf2_read_one_debug_info
1072 * Loads into memory one debug info entry, and recursively its children (if any)
1074 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1075 dwarf2_traverse_context_t* traverse,
1076 dwarf2_debug_info_t* parent_di,
1077 dwarf2_debug_info_t** pdi)
1079 const dwarf2_abbrev_entry_t*abbrev;
1080 unsigned long entry_code;
1081 unsigned long offset;
1082 dwarf2_debug_info_t* di;
1083 dwarf2_debug_info_t* child;
1084 dwarf2_debug_info_t** where;
1085 dwarf2_abbrev_entry_attr_t* attr;
1086 unsigned i;
1087 struct attribute sibling;
1089 offset = traverse->data - ctx->sections[ctx->section].address;
1090 entry_code = dwarf2_leb128_as_unsigned(traverse);
1091 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1092 if (!entry_code)
1094 *pdi = NULL;
1095 return TRUE;
1097 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1098 if (!abbrev)
1100 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1101 return FALSE;
1103 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1104 if (!di) return FALSE;
1105 di->abbrev = abbrev;
1106 di->symt = NULL;
1107 di->parent = parent_di;
1109 if (abbrev->num_attr)
1111 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1112 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1114 di->data[i] = traverse->data;
1115 dwarf2_swallow_attribute(traverse, attr);
1118 else di->data = NULL;
1119 if (abbrev->have_child)
1121 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1122 while (traverse->data < traverse->end_data)
1124 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1125 if (!child) break;
1126 where = vector_add(&di->children, &ctx->pool);
1127 if (!where) return FALSE;
1128 *where = child;
1131 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1132 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1134 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1135 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1136 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1138 *pdi = di;
1139 return TRUE;
1142 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1143 dwarf2_debug_info_t* di)
1145 struct attribute spec;
1147 while (di)
1149 if (di->abbrev->have_child)
1150 return &di->children;
1151 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1152 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1153 FIXME("Should have found the debug info entry\n");
1155 return NULL;
1158 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1159 dwarf2_debug_info_t* di)
1161 struct attribute name;
1162 struct attribute size;
1163 struct attribute encoding;
1164 enum BasicType bt;
1165 int cache_idx = -1;
1166 if (di->symt) return di->symt;
1168 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1170 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1171 name.u.string = NULL;
1172 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1173 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1175 switch (encoding.u.uvalue)
1177 case DW_ATE_void: bt = btVoid; break;
1178 case DW_ATE_address: bt = btULong; break;
1179 case DW_ATE_boolean: bt = btBool; break;
1180 case DW_ATE_complex_float: bt = btComplex; break;
1181 case DW_ATE_float: bt = btFloat; break;
1182 case DW_ATE_signed: bt = btInt; break;
1183 case DW_ATE_unsigned: bt = btUInt; break;
1184 case DW_ATE_signed_char: bt = btChar; break;
1185 case DW_ATE_unsigned_char: bt = btChar; break;
1186 default: bt = btNoType; break;
1188 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1189 switch (bt)
1191 case btVoid:
1192 assert(size.u.uvalue == 0);
1193 cache_idx = sc_void;
1194 break;
1195 case btInt:
1196 switch (size.u.uvalue)
1198 case 1: cache_idx = sc_int1; break;
1199 case 2: cache_idx = sc_int2; break;
1200 case 4: cache_idx = sc_int4; break;
1202 break;
1203 default: break;
1205 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1206 ctx->symt_cache[cache_idx] = di->symt;
1208 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1209 return di->symt;
1212 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1213 dwarf2_debug_info_t* di)
1215 struct symt* ref_type;
1216 struct attribute name;
1218 if (di->symt) return di->symt;
1220 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1222 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1223 ref_type = dwarf2_lookup_type(ctx, di);
1225 if (name.u.string)
1226 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1227 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1228 return di->symt;
1231 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1232 dwarf2_debug_info_t* di)
1234 struct symt* ref_type;
1235 struct attribute size;
1237 if (di->symt) return di->symt;
1239 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1241 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1242 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1244 ref_type = ctx->symt_cache[sc_void];
1245 assert(ref_type);
1247 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1248 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1249 return di->symt;
1252 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1253 dwarf2_debug_info_t* di)
1255 struct symt* ref_type;
1256 struct symt* idx_type = NULL;
1257 struct attribute min, max, cnt;
1258 dwarf2_debug_info_t* child;
1259 unsigned int i;
1260 const struct vector* children;
1262 if (di->symt) return di->symt;
1264 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1266 ref_type = dwarf2_lookup_type(ctx, di);
1268 if (!(children = dwarf2_get_di_children(ctx, di)))
1270 /* fake an array with unknown size */
1271 /* FIXME: int4 even on 64bit machines??? */
1272 idx_type = ctx->symt_cache[sc_int4];
1273 min.u.uvalue = 0;
1274 max.u.uvalue = -1;
1276 else for (i = 0; i < vector_length(children); i++)
1278 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1279 switch (child->abbrev->tag)
1281 case DW_TAG_subrange_type:
1282 idx_type = dwarf2_lookup_type(ctx, child);
1283 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1284 min.u.uvalue = 0;
1285 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1286 max.u.uvalue = 0;
1287 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1288 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1289 break;
1290 default:
1291 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1292 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1293 break;
1296 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1297 return di->symt;
1300 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1301 dwarf2_debug_info_t* di)
1303 struct symt* ref_type;
1305 if (di->symt) return di->symt;
1307 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1309 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1311 ref_type = ctx->symt_cache[sc_void];
1312 assert(ref_type);
1314 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1315 di->symt = ref_type;
1317 return ref_type;
1320 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1321 dwarf2_debug_info_t* di)
1323 struct symt* ref_type;
1325 if (di->symt) return di->symt;
1327 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1329 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1331 ref_type = ctx->symt_cache[sc_void];
1332 assert(ref_type);
1334 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1335 di->symt = ref_type;
1337 return ref_type;
1340 static struct symt* dwarf2_parse_unspecified_type(dwarf2_parse_context_t* ctx,
1341 dwarf2_debug_info_t* di)
1343 struct attribute name;
1344 struct attribute size;
1345 struct symt_basic *basic;
1347 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1349 if (di->symt) return di->symt;
1351 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1352 name.u.string = "void";
1353 size.u.uvalue = sizeof(void *);
1355 basic = symt_new_basic(ctx->module, btVoid, name.u.string, size.u.uvalue);
1356 di->symt = &basic->symt;
1358 if (!ctx->symt_cache[sc_void])
1359 ctx->symt_cache[sc_void] = di->symt;
1361 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1362 return di->symt;
1365 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1366 dwarf2_debug_info_t* di)
1368 struct symt* ref_type = NULL;
1370 if (di->symt) return di->symt;
1372 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1374 ref_type = dwarf2_lookup_type(ctx, di);
1375 /* FIXME: for now, we hard-wire C++ references to pointers */
1376 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1378 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1380 return di->symt;
1383 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1384 dwarf2_debug_info_t* di,
1385 struct symt_udt* parent)
1387 struct symt* elt_type;
1388 struct attribute name;
1389 struct attribute bit_size;
1390 struct attribute bit_offset;
1391 struct location loc;
1393 assert(parent);
1395 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1397 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1398 elt_type = dwarf2_lookup_type(ctx, di);
1399 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1401 if (loc.kind != loc_absolute)
1403 FIXME("Found register, while not expecting it\n");
1404 loc.offset = 0;
1406 else
1407 TRACE("found member_location at %s -> %lu\n",
1408 dwarf2_debug_ctx(ctx), loc.offset);
1410 else
1411 loc.offset = 0;
1412 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1413 bit_size.u.uvalue = 0;
1414 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1416 /* FIXME: we should only do this when implementation is LSB (which is
1417 * the case on i386 processors)
1419 struct attribute nbytes;
1420 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1422 DWORD64 size;
1423 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1424 (unsigned long)size : 0;
1426 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1428 else bit_offset.u.uvalue = 0;
1429 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1430 (loc.offset << 3) + bit_offset.u.uvalue,
1431 bit_size.u.uvalue);
1433 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1436 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1437 dwarf2_debug_info_t* di);
1439 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1440 dwarf2_debug_info_t* di,
1441 enum UdtKind udt)
1443 struct attribute name;
1444 struct attribute size;
1445 struct vector* children;
1446 dwarf2_debug_info_t*child;
1447 unsigned int i;
1449 if (di->symt) return di->symt;
1451 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1453 /* quirk... FIXME provide real support for anonymous UDTs */
1454 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1455 name.u.string = "zz_anon_zz";
1456 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1458 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1459 size.u.uvalue, udt)->symt;
1461 children = dwarf2_get_di_children(ctx, di);
1462 if (children) for (i = 0; i < vector_length(children); i++)
1464 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1466 switch (child->abbrev->tag)
1468 case DW_TAG_array_type:
1469 dwarf2_parse_array_type(ctx, di);
1470 break;
1471 case DW_TAG_member:
1472 /* FIXME: should I follow the sibling stuff ?? */
1473 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1474 break;
1475 case DW_TAG_enumeration_type:
1476 dwarf2_parse_enumeration_type(ctx, child);
1477 break;
1478 case DW_TAG_subprogram:
1479 dwarf2_parse_subprogram(ctx, child);
1480 break;
1481 case DW_TAG_const_type:
1482 dwarf2_parse_const_type(ctx, child);
1483 break;
1484 case DW_TAG_structure_type:
1485 case DW_TAG_class_type:
1486 case DW_TAG_union_type:
1487 case DW_TAG_typedef:
1488 /* FIXME: we need to handle nested udt definitions */
1489 case DW_TAG_inheritance:
1490 case DW_TAG_template_type_param:
1491 case DW_TAG_template_value_param:
1492 case DW_TAG_variable:
1493 case DW_TAG_imported_declaration:
1494 case DW_TAG_ptr_to_member_type:
1495 case DW_TAG_GNU_template_parameter_pack:
1496 case DW_TAG_GNU_formal_parameter_pack:
1497 /* FIXME: some C++ related stuff */
1498 break;
1499 default:
1500 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1501 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1502 break;
1506 return di->symt;
1509 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1510 dwarf2_debug_info_t* di,
1511 struct symt_enum* parent)
1513 struct attribute name;
1514 struct attribute value;
1516 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1518 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1519 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1520 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1522 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1525 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1526 dwarf2_debug_info_t* di)
1528 struct attribute name;
1529 struct attribute size;
1530 struct symt_basic* basetype;
1531 struct vector* children;
1532 dwarf2_debug_info_t*child;
1533 unsigned int i;
1535 if (di->symt) return di->symt;
1537 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1539 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1540 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1542 switch (size.u.uvalue) /* FIXME: that's wrong */
1544 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1545 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1546 default:
1547 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1550 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1552 children = dwarf2_get_di_children(ctx, di);
1553 /* FIXME: should we use the sibling stuff ?? */
1554 if (children) for (i = 0; i < vector_length(children); i++)
1556 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1558 switch (child->abbrev->tag)
1560 case DW_TAG_enumerator:
1561 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1562 break;
1563 default:
1564 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1565 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1568 return di->symt;
1571 /* structure used to pass information around when parsing a subprogram */
1572 typedef struct dwarf2_subprogram_s
1574 dwarf2_parse_context_t* ctx;
1575 struct symt_function* func;
1576 BOOL non_computed_variable;
1577 struct location frame;
1578 } dwarf2_subprogram_t;
1580 /******************************************************************
1581 * dwarf2_parse_variable
1583 * Parses any variable (parameter, local/global variable)
1585 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1586 struct symt_block* block,
1587 dwarf2_debug_info_t* di)
1589 struct symt* param_type;
1590 struct attribute name, value;
1591 struct location loc;
1592 BOOL is_pmt;
1594 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1596 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1597 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1599 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1600 /* cannot do much without the name, the functions below won't like it. */
1601 return;
1603 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1604 &loc, &subpgm->frame))
1606 struct attribute ext;
1608 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1609 name.u.string, loc.kind, loc.offset, loc.reg,
1610 dwarf2_debug_ctx(subpgm->ctx));
1612 switch (loc.kind)
1614 case loc_error:
1615 break;
1616 case loc_absolute:
1617 /* it's a global variable */
1618 /* FIXME: we don't handle its scope yet */
1619 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1620 ext.u.uvalue = 0;
1621 loc.offset += subpgm->ctx->load_offset;
1622 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1623 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1624 loc, 0, param_type);
1625 break;
1626 default:
1627 subpgm->non_computed_variable = TRUE;
1628 /* fall through */
1629 case loc_register:
1630 case loc_regrel:
1631 /* either a pmt/variable relative to frame pointer or
1632 * pmt/variable in a register
1634 assert(subpgm->func);
1635 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1636 is_pmt ? DataIsParam : DataIsLocal,
1637 &loc, block, param_type, name.u.string);
1638 break;
1641 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1643 VARIANT v;
1644 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1645 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1646 switch (value.form)
1648 case DW_FORM_data1:
1649 case DW_FORM_data2:
1650 case DW_FORM_data4:
1651 case DW_FORM_udata:
1652 case DW_FORM_addr:
1653 v.n1.n2.vt = VT_UI4;
1654 v.n1.n2.n3.lVal = value.u.uvalue;
1655 break;
1657 case DW_FORM_data8:
1658 v.n1.n2.vt = VT_UI8;
1659 v.n1.n2.n3.llVal = value.u.lluvalue;
1660 break;
1662 case DW_FORM_sdata:
1663 v.n1.n2.vt = VT_I4;
1664 v.n1.n2.n3.lVal = value.u.svalue;
1665 break;
1667 case DW_FORM_strp:
1668 case DW_FORM_string:
1669 /* FIXME: native doesn't report const strings from here !!
1670 * however, the value of the string is in the code somewhere
1672 v.n1.n2.vt = VT_I1 | VT_BYREF;
1673 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1674 break;
1676 case DW_FORM_block:
1677 case DW_FORM_block1:
1678 case DW_FORM_block2:
1679 case DW_FORM_block4:
1680 v.n1.n2.vt = VT_I4;
1681 switch (value.u.block.size)
1683 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1684 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1685 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1686 default:
1687 v.n1.n2.vt = VT_I1 | VT_BYREF;
1688 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1689 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1691 break;
1693 default:
1694 FIXME("Unsupported form for const value %s (%lx)\n",
1695 name.u.string, value.form);
1696 v.n1.n2.vt = VT_EMPTY;
1698 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1699 name.u.string, param_type, &v)->symt;
1701 else
1703 /* variable has been optimized away... report anyway */
1704 loc.kind = loc_error;
1705 loc.reg = loc_err_no_location;
1706 if (subpgm->func)
1708 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1709 is_pmt ? DataIsParam : DataIsLocal,
1710 &loc, block, param_type, name.u.string);
1712 else
1714 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1717 if (is_pmt && subpgm->func && subpgm->func->type)
1718 symt_add_function_signature_parameter(subpgm->ctx->module,
1719 (struct symt_function_signature*)subpgm->func->type,
1720 param_type);
1722 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1725 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1726 const dwarf2_debug_info_t* di)
1728 struct attribute name;
1729 struct attribute low_pc;
1730 struct location loc;
1732 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1734 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1735 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1736 name.u.string = NULL;
1738 loc.kind = loc_absolute;
1739 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1740 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1741 &loc, name.u.string);
1744 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1745 struct symt_block* parent_block,
1746 dwarf2_debug_info_t* di);
1748 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1749 dwarf2_debug_info_t* di);
1751 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1752 struct symt_block* parent_block,
1753 dwarf2_debug_info_t* di)
1755 struct symt_block* block;
1756 unsigned long low_pc, high_pc;
1757 struct vector* children;
1758 dwarf2_debug_info_t*child;
1759 unsigned int i;
1761 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1763 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1765 FIXME("cannot read range\n");
1766 return;
1769 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1770 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1771 high_pc - low_pc);
1773 children = dwarf2_get_di_children(subpgm->ctx, di);
1774 if (children) for (i = 0; i < vector_length(children); i++)
1776 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1778 switch (child->abbrev->tag)
1780 case DW_TAG_formal_parameter:
1781 case DW_TAG_variable:
1782 dwarf2_parse_variable(subpgm, block, child);
1783 break;
1784 case DW_TAG_lexical_block:
1785 dwarf2_parse_subprogram_block(subpgm, block, child);
1786 break;
1787 case DW_TAG_inlined_subroutine:
1788 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1789 break;
1790 case DW_TAG_label:
1791 dwarf2_parse_subprogram_label(subpgm, child);
1792 break;
1793 case DW_TAG_GNU_call_site:
1794 /* this isn't properly supported by dbghelp interface. skip it for now */
1795 break;
1796 default:
1797 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1798 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1799 dwarf2_debug_di(di));
1802 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1805 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1806 struct symt_block* parent_block,
1807 dwarf2_debug_info_t* di)
1809 struct symt_block* block;
1810 unsigned long low_pc, high_pc;
1811 struct vector* children;
1812 dwarf2_debug_info_t*child;
1813 unsigned int i;
1815 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1817 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1819 FIXME("no range\n");
1820 return;
1823 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1824 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1825 high_pc - low_pc);
1827 children = dwarf2_get_di_children(subpgm->ctx, di);
1828 if (children) for (i = 0; i < vector_length(children); i++)
1830 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1832 switch (child->abbrev->tag)
1834 case DW_TAG_inlined_subroutine:
1835 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1836 break;
1837 case DW_TAG_variable:
1838 dwarf2_parse_variable(subpgm, block, child);
1839 break;
1840 case DW_TAG_pointer_type:
1841 dwarf2_parse_pointer_type(subpgm->ctx, di);
1842 break;
1843 case DW_TAG_subroutine_type:
1844 dwarf2_parse_subroutine_type(subpgm->ctx, di);
1845 break;
1846 case DW_TAG_lexical_block:
1847 dwarf2_parse_subprogram_block(subpgm, block, child);
1848 break;
1849 case DW_TAG_subprogram:
1850 /* FIXME: likely a declaration (to be checked)
1851 * skip it for now
1853 break;
1854 case DW_TAG_formal_parameter:
1855 /* FIXME: likely elements for exception handling (GCC flavor)
1856 * Skip it for now
1858 break;
1859 case DW_TAG_imported_module:
1860 /* C++ stuff to be silenced (for now) */
1861 break;
1862 case DW_TAG_GNU_call_site:
1863 /* this isn't properly supported by dbghelp interface. skip it for now */
1864 break;
1865 case DW_TAG_label:
1866 dwarf2_parse_subprogram_label(subpgm, child);
1867 break;
1868 case DW_TAG_class_type:
1869 case DW_TAG_structure_type:
1870 case DW_TAG_union_type:
1871 case DW_TAG_enumeration_type:
1872 case DW_TAG_typedef:
1873 /* the type referred to will be loaded when we need it, so skip it */
1874 break;
1875 default:
1876 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1877 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1881 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1884 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1885 dwarf2_debug_info_t* di)
1887 struct attribute name;
1888 unsigned long low_pc, high_pc;
1889 struct attribute is_decl;
1890 struct attribute inline_flags;
1891 struct symt* ret_type;
1892 struct symt_function_signature* sig_type;
1893 dwarf2_subprogram_t subpgm;
1894 struct vector* children;
1895 dwarf2_debug_info_t* child;
1896 unsigned int i;
1898 if (di->symt) return di->symt;
1900 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1902 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1904 WARN("No name for function... dropping function\n");
1905 return NULL;
1907 /* if it's an abstract representation of an inline function, there should be
1908 * a concrete object that we'll handle
1910 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1911 inline_flags.u.uvalue != DW_INL_not_inlined)
1913 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1914 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1915 return NULL;
1918 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1919 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1921 /* it's a real declaration, skip it */
1922 return NULL;
1924 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1926 WARN("cannot get range for %s\n", name.u.string);
1927 return NULL;
1929 /* As functions (defined as inline assembly) get debug info with dwarf
1930 * (not the case for stabs), we just drop Wine's thunks here...
1931 * Actual thunks will be created in elf_module from the symbol table
1933 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1934 return NULL;
1935 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1937 ret_type = ctx->symt_cache[sc_void];
1938 assert(ret_type);
1940 /* FIXME: assuming C source code */
1941 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1942 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1943 dwarf2_get_cpp_name(ctx, di, name.u.string),
1944 ctx->load_offset + low_pc, high_pc - low_pc,
1945 &sig_type->symt);
1946 di->symt = &subpgm.func->symt;
1947 subpgm.ctx = ctx;
1948 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1949 &subpgm.frame, NULL))
1951 /* on stack !! */
1952 subpgm.frame.kind = loc_regrel;
1953 subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
1954 subpgm.frame.offset = 0;
1956 subpgm.non_computed_variable = FALSE;
1958 children = dwarf2_get_di_children(ctx, di);
1959 if (children) for (i = 0; i < vector_length(children); i++)
1961 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1963 switch (child->abbrev->tag)
1965 case DW_TAG_variable:
1966 case DW_TAG_formal_parameter:
1967 dwarf2_parse_variable(&subpgm, NULL, child);
1968 break;
1969 case DW_TAG_lexical_block:
1970 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1971 break;
1972 case DW_TAG_inlined_subroutine:
1973 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1974 break;
1975 case DW_TAG_pointer_type:
1976 dwarf2_parse_pointer_type(subpgm.ctx, di);
1977 break;
1978 case DW_TAG_subprogram:
1979 /* FIXME: likely a declaration (to be checked)
1980 * skip it for now
1982 break;
1983 case DW_TAG_label:
1984 dwarf2_parse_subprogram_label(&subpgm, child);
1985 break;
1986 case DW_TAG_class_type:
1987 case DW_TAG_structure_type:
1988 case DW_TAG_union_type:
1989 case DW_TAG_enumeration_type:
1990 case DW_TAG_typedef:
1991 /* the type referred to will be loaded when we need it, so skip it */
1992 break;
1993 case DW_TAG_unspecified_parameters:
1994 case DW_TAG_template_type_param:
1995 case DW_TAG_template_value_param:
1996 case DW_TAG_GNU_call_site:
1997 case DW_TAG_GNU_template_parameter_pack:
1998 case DW_TAG_GNU_formal_parameter_pack:
1999 /* FIXME: no support in dbghelp's internals so far */
2000 break;
2001 default:
2002 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
2003 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2007 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2009 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
2010 &subpgm.frame, NULL);
2012 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
2014 return di->symt;
2017 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
2018 dwarf2_debug_info_t* di)
2020 struct symt* ret_type;
2021 struct symt_function_signature* sig_type;
2022 struct vector* children;
2023 dwarf2_debug_info_t* child;
2024 unsigned int i;
2026 if (di->symt) return di->symt;
2028 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2030 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
2032 ret_type = ctx->symt_cache[sc_void];
2033 assert(ret_type);
2036 /* FIXME: assuming C source code */
2037 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
2039 children = dwarf2_get_di_children(ctx, di);
2040 if (children) for (i = 0; i < vector_length(children); i++)
2042 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2044 switch (child->abbrev->tag)
2046 case DW_TAG_formal_parameter:
2047 symt_add_function_signature_parameter(ctx->module, sig_type,
2048 dwarf2_lookup_type(ctx, child));
2049 break;
2050 case DW_TAG_unspecified_parameters:
2051 WARN("Unsupported unspecified parameters\n");
2052 break;
2056 return di->symt = &sig_type->symt;
2059 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
2060 dwarf2_debug_info_t* di)
2062 struct vector* children;
2063 dwarf2_debug_info_t* child;
2064 unsigned int i;
2066 if (di->symt) return;
2068 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2070 di->symt = ctx->symt_cache[sc_void];
2072 children = dwarf2_get_di_children(ctx, di);
2073 if (children) for (i = 0; i < vector_length(children); i++)
2075 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2076 dwarf2_load_one_entry(ctx, child);
2080 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2081 dwarf2_debug_info_t* di)
2083 switch (di->abbrev->tag)
2085 case DW_TAG_typedef:
2086 dwarf2_parse_typedef(ctx, di);
2087 break;
2088 case DW_TAG_base_type:
2089 dwarf2_parse_base_type(ctx, di);
2090 break;
2091 case DW_TAG_pointer_type:
2092 dwarf2_parse_pointer_type(ctx, di);
2093 break;
2094 case DW_TAG_class_type:
2095 dwarf2_parse_udt_type(ctx, di, UdtClass);
2096 break;
2097 case DW_TAG_structure_type:
2098 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2099 break;
2100 case DW_TAG_union_type:
2101 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2102 break;
2103 case DW_TAG_array_type:
2104 dwarf2_parse_array_type(ctx, di);
2105 break;
2106 case DW_TAG_const_type:
2107 dwarf2_parse_const_type(ctx, di);
2108 break;
2109 case DW_TAG_volatile_type:
2110 dwarf2_parse_volatile_type(ctx, di);
2111 break;
2112 case DW_TAG_unspecified_type:
2113 dwarf2_parse_unspecified_type(ctx, di);
2114 break;
2115 case DW_TAG_reference_type:
2116 dwarf2_parse_reference_type(ctx, di);
2117 break;
2118 case DW_TAG_enumeration_type:
2119 dwarf2_parse_enumeration_type(ctx, di);
2120 break;
2121 case DW_TAG_subprogram:
2122 dwarf2_parse_subprogram(ctx, di);
2123 break;
2124 case DW_TAG_subroutine_type:
2125 dwarf2_parse_subroutine_type(ctx, di);
2126 break;
2127 case DW_TAG_variable:
2129 dwarf2_subprogram_t subpgm;
2131 subpgm.ctx = ctx;
2132 subpgm.func = NULL;
2133 subpgm.frame.kind = loc_absolute;
2134 subpgm.frame.offset = 0;
2135 subpgm.frame.reg = Wine_DW_no_register;
2136 dwarf2_parse_variable(&subpgm, NULL, di);
2138 break;
2139 case DW_TAG_namespace:
2140 dwarf2_parse_namespace(ctx, di);
2141 break;
2142 /* silence a couple of C++ defines */
2143 case DW_TAG_imported_module:
2144 case DW_TAG_imported_declaration:
2145 case DW_TAG_ptr_to_member_type:
2146 break;
2147 default:
2148 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2149 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2153 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2154 const struct vector* v, unsigned file, unsigned line)
2156 struct symt_function* func;
2157 struct symt_ht* symt;
2158 unsigned* psrc;
2160 if (!file || !(psrc = vector_at(v, file - 1))) return;
2162 TRACE("%s %lx %s %u\n",
2163 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2164 if (!(symt = symt_find_nearest(module, address)) ||
2165 symt->symt.tag != SymTagFunction) return;
2166 func = (struct symt_function*)symt;
2167 symt_add_func_line(module, func, *psrc, line, address - func->address);
2170 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2171 dwarf2_parse_context_t* ctx,
2172 const char* compile_dir,
2173 unsigned long offset)
2175 dwarf2_traverse_context_t traverse;
2176 unsigned long length;
2177 unsigned insn_size, default_stmt;
2178 unsigned line_range, opcode_base;
2179 int line_base;
2180 const unsigned char* opcode_len;
2181 struct vector dirs;
2182 struct vector files;
2183 const char** p;
2185 /* section with line numbers stripped */
2186 if (sections[section_line].address == IMAGE_NO_MAP)
2187 return FALSE;
2189 if (offset + 4 > sections[section_line].size)
2191 WARN("out of bounds offset\n");
2192 return FALSE;
2194 traverse.data = sections[section_line].address + offset;
2195 traverse.end_data = traverse.data + 4;
2196 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2198 length = dwarf2_parse_u4(&traverse);
2199 traverse.end_data = sections[section_line].address + offset + length;
2201 if (offset + 4 + length > sections[section_line].size)
2203 WARN("out of bounds header\n");
2204 return FALSE;
2206 dwarf2_parse_u2(&traverse); /* version */
2207 dwarf2_parse_u4(&traverse); /* header_len */
2208 insn_size = dwarf2_parse_byte(&traverse);
2209 default_stmt = dwarf2_parse_byte(&traverse);
2210 line_base = (signed char)dwarf2_parse_byte(&traverse);
2211 line_range = dwarf2_parse_byte(&traverse);
2212 opcode_base = dwarf2_parse_byte(&traverse);
2214 opcode_len = traverse.data;
2215 traverse.data += opcode_base - 1;
2217 vector_init(&dirs, sizeof(const char*), 4);
2218 p = vector_add(&dirs, &ctx->pool);
2219 *p = compile_dir ? compile_dir : ".";
2220 while (*traverse.data)
2222 const char* rel = (const char*)traverse.data;
2223 unsigned rellen = strlen(rel);
2224 TRACE("Got include %s\n", rel);
2225 traverse.data += rellen + 1;
2226 p = vector_add(&dirs, &ctx->pool);
2228 if (*rel == '/' || !compile_dir)
2229 *p = rel;
2230 else
2232 /* include directory relative to compile directory */
2233 unsigned baselen = strlen(compile_dir);
2234 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2235 strcpy(tmp, compile_dir);
2236 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2237 strcpy(&tmp[baselen], rel);
2238 *p = tmp;
2242 traverse.data++;
2244 vector_init(&files, sizeof(unsigned), 16);
2245 while (*traverse.data)
2247 unsigned int dir_index, mod_time;
2248 const char* name;
2249 const char* dir;
2250 unsigned* psrc;
2252 name = (const char*)traverse.data;
2253 traverse.data += strlen(name) + 1;
2254 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2255 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2256 length = dwarf2_leb128_as_unsigned(&traverse);
2257 dir = *(const char**)vector_at(&dirs, dir_index);
2258 TRACE("Got file %s/%s (%u,%lu)\n", dir, name, mod_time, length);
2259 psrc = vector_add(&files, &ctx->pool);
2260 *psrc = source_new(ctx->module, dir, name);
2262 traverse.data++;
2264 while (traverse.data < traverse.end_data)
2266 unsigned long address = 0;
2267 unsigned file = 1;
2268 unsigned line = 1;
2269 unsigned is_stmt = default_stmt;
2270 BOOL end_sequence = FALSE;
2271 unsigned opcode, extopcode, i;
2273 while (!end_sequence)
2275 opcode = dwarf2_parse_byte(&traverse);
2276 TRACE("Got opcode %x\n", opcode);
2278 if (opcode >= opcode_base)
2280 unsigned delta = opcode - opcode_base;
2282 address += (delta / line_range) * insn_size;
2283 line += line_base + (delta % line_range);
2284 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2286 else
2288 switch (opcode)
2290 case DW_LNS_copy:
2291 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2292 break;
2293 case DW_LNS_advance_pc:
2294 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2295 break;
2296 case DW_LNS_advance_line:
2297 line += dwarf2_leb128_as_signed(&traverse);
2298 break;
2299 case DW_LNS_set_file:
2300 file = dwarf2_leb128_as_unsigned(&traverse);
2301 break;
2302 case DW_LNS_set_column:
2303 dwarf2_leb128_as_unsigned(&traverse);
2304 break;
2305 case DW_LNS_negate_stmt:
2306 is_stmt = !is_stmt;
2307 break;
2308 case DW_LNS_set_basic_block:
2309 break;
2310 case DW_LNS_const_add_pc:
2311 address += ((255 - opcode_base) / line_range) * insn_size;
2312 break;
2313 case DW_LNS_fixed_advance_pc:
2314 address += dwarf2_parse_u2(&traverse);
2315 break;
2316 case DW_LNS_extended_op:
2317 dwarf2_leb128_as_unsigned(&traverse);
2318 extopcode = dwarf2_parse_byte(&traverse);
2319 switch (extopcode)
2321 case DW_LNE_end_sequence:
2322 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2323 end_sequence = TRUE;
2324 break;
2325 case DW_LNE_set_address:
2326 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2327 break;
2328 case DW_LNE_define_file:
2329 FIXME("not handled define file %s\n", traverse.data);
2330 traverse.data += strlen((const char *)traverse.data) + 1;
2331 dwarf2_leb128_as_unsigned(&traverse);
2332 dwarf2_leb128_as_unsigned(&traverse);
2333 dwarf2_leb128_as_unsigned(&traverse);
2334 break;
2335 case DW_LNE_set_discriminator:
2337 unsigned descr;
2339 descr = dwarf2_leb128_as_unsigned(&traverse);
2340 WARN("not handled discriminator %x\n", descr);
2342 break;
2343 default:
2344 FIXME("Unsupported extended opcode %x\n", extopcode);
2345 break;
2347 break;
2348 default:
2349 WARN("Unsupported opcode %x\n", opcode);
2350 for (i = 0; i < opcode_len[opcode]; i++)
2351 dwarf2_leb128_as_unsigned(&traverse);
2352 break;
2357 return TRUE;
2360 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2361 struct module* module,
2362 const struct elf_thunk_area* thunks,
2363 dwarf2_traverse_context_t* mod_ctx,
2364 unsigned long load_offset)
2366 dwarf2_parse_context_t ctx;
2367 dwarf2_traverse_context_t abbrev_ctx;
2368 dwarf2_debug_info_t* di;
2369 dwarf2_traverse_context_t cu_ctx;
2370 const unsigned char* comp_unit_start = mod_ctx->data;
2371 unsigned long cu_length;
2372 unsigned short cu_version;
2373 unsigned long cu_abbrev_offset;
2374 BOOL ret = FALSE;
2376 cu_length = dwarf2_parse_u4(mod_ctx);
2377 cu_ctx.data = mod_ctx->data;
2378 cu_ctx.end_data = mod_ctx->data + cu_length;
2379 mod_ctx->data += cu_length;
2380 cu_version = dwarf2_parse_u2(&cu_ctx);
2381 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2382 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2384 TRACE("Compilation Unit Header found at 0x%x:\n",
2385 (int)(comp_unit_start - sections[section_debug].address));
2386 TRACE("- length: %lu\n", cu_length);
2387 TRACE("- version: %u\n", cu_version);
2388 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2389 TRACE("- word_size: %u\n", cu_ctx.word_size);
2391 if (cu_version != 2)
2393 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2394 cu_version);
2395 return FALSE;
2398 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2399 mod_ctx->word_size = cu_ctx.word_size;
2401 pool_init(&ctx.pool, 65536);
2402 ctx.sections = sections;
2403 ctx.section = section_debug;
2404 ctx.module = module;
2405 ctx.thunks = thunks;
2406 ctx.load_offset = load_offset;
2407 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2408 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2409 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2410 ctx.cpp_name = NULL;
2412 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2413 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2414 abbrev_ctx.word_size = cu_ctx.word_size;
2415 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2417 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2418 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2420 if (di->abbrev->tag == DW_TAG_compile_unit)
2422 struct attribute name;
2423 struct vector* children;
2424 dwarf2_debug_info_t* child = NULL;
2425 unsigned int i;
2426 struct attribute stmt_list, low_pc;
2427 struct attribute comp_dir;
2429 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2430 name.u.string = NULL;
2432 /* get working directory of current compilation unit */
2433 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2434 comp_dir.u.string = NULL;
2436 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2437 low_pc.u.uvalue = 0;
2438 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2439 source_new(module, comp_dir.u.string, name.u.string));
2440 di->symt = &ctx.compiland->symt;
2441 children = dwarf2_get_di_children(&ctx, di);
2442 if (children) for (i = 0; i < vector_length(children); i++)
2444 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2445 dwarf2_load_one_entry(&ctx, child);
2447 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2449 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2450 module->module.LineNumbers = TRUE;
2452 ret = TRUE;
2454 else FIXME("Should have a compilation unit here\n");
2455 pool_destroy(&ctx.pool);
2456 return ret;
2459 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2460 unsigned long ip, dwarf2_traverse_context_t* lctx)
2462 DWORD_PTR beg, end;
2463 const BYTE* ptr = start;
2464 DWORD len;
2466 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2468 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2469 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2470 if (!beg && !end) break;
2471 len = dwarf2_get_u2(ptr); ptr += 2;
2473 if (beg <= ip && ip < end)
2475 lctx->data = ptr;
2476 lctx->end_data = ptr + len;
2477 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2478 return TRUE;
2480 ptr += len;
2482 WARN("Couldn't find ip in location list\n");
2483 return FALSE;
2486 static enum location_error loc_compute_frame(struct process* pcs,
2487 const struct module_format* modfmt,
2488 const struct symt_function* func,
2489 DWORD_PTR ip, struct location* frame)
2491 struct symt** psym = NULL;
2492 struct location* pframe;
2493 dwarf2_traverse_context_t lctx;
2494 enum location_error err;
2495 unsigned int i;
2497 for (i=0; i<vector_length(&func->vchildren); i++)
2499 psym = vector_at(&func->vchildren, i);
2500 if ((*psym)->tag == SymTagCustom)
2502 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2504 /* First, recompute the frame information, if needed */
2505 switch (pframe->kind)
2507 case loc_regrel:
2508 case loc_register:
2509 *frame = *pframe;
2510 break;
2511 case loc_dwarf2_location_list:
2512 WARN("Searching loclist for %s\n", func->hash_elt.name);
2513 if (!dwarf2_lookup_loclist(modfmt,
2514 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2515 ip, &lctx))
2516 return loc_err_out_of_scope;
2517 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2518 if (frame->kind >= loc_user)
2520 WARN("Couldn't compute runtime frame location\n");
2521 return loc_err_too_complex;
2523 break;
2524 default:
2525 WARN("Unsupported frame kind %d\n", pframe->kind);
2526 return loc_err_internal;
2528 return 0;
2531 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2532 return loc_err_internal;
2535 enum reg_rule
2537 RULE_UNSET, /* not set at all */
2538 RULE_UNDEFINED, /* undefined value */
2539 RULE_SAME, /* same value as previous frame */
2540 RULE_CFA_OFFSET, /* stored at cfa offset */
2541 RULE_OTHER_REG, /* stored in other register */
2542 RULE_EXPRESSION, /* address specified by expression */
2543 RULE_VAL_EXPRESSION /* value specified by expression */
2546 /* make it large enough for all CPUs */
2547 #define NB_FRAME_REGS 64
2548 #define MAX_SAVED_STATES 16
2550 struct frame_state
2552 ULONG_PTR cfa_offset;
2553 unsigned char cfa_reg;
2554 enum reg_rule cfa_rule;
2555 enum reg_rule rules[NB_FRAME_REGS];
2556 ULONG_PTR regs[NB_FRAME_REGS];
2559 struct frame_info
2561 ULONG_PTR ip;
2562 ULONG_PTR code_align;
2563 LONG_PTR data_align;
2564 unsigned char retaddr_reg;
2565 unsigned char fde_encoding;
2566 unsigned char lsda_encoding;
2567 unsigned char signal_frame;
2568 unsigned char aug_z_format;
2569 unsigned char state_sp;
2570 struct frame_state state;
2571 struct frame_state state_stack[MAX_SAVED_STATES];
2574 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2576 ULONG_PTR base;
2578 if (encoding == DW_EH_PE_omit) return 0;
2580 switch (encoding & 0xf0)
2582 case DW_EH_PE_abs:
2583 base = 0;
2584 break;
2585 case DW_EH_PE_pcrel:
2586 base = (ULONG_PTR)ctx->data;
2587 break;
2588 default:
2589 FIXME("unsupported encoding %02x\n", encoding);
2590 return 0;
2593 switch (encoding & 0x0f)
2595 case DW_EH_PE_native:
2596 return base + dwarf2_parse_addr(ctx);
2597 case DW_EH_PE_leb128:
2598 return base + dwarf2_leb128_as_unsigned(ctx);
2599 case DW_EH_PE_data2:
2600 return base + dwarf2_parse_u2(ctx);
2601 case DW_EH_PE_data4:
2602 return base + dwarf2_parse_u4(ctx);
2603 case DW_EH_PE_data8:
2604 return base + dwarf2_parse_u8(ctx);
2605 case DW_EH_PE_signed|DW_EH_PE_leb128:
2606 return base + dwarf2_leb128_as_signed(ctx);
2607 case DW_EH_PE_signed|DW_EH_PE_data2:
2608 return base + (signed short)dwarf2_parse_u2(ctx);
2609 case DW_EH_PE_signed|DW_EH_PE_data4:
2610 return base + (signed int)dwarf2_parse_u4(ctx);
2611 case DW_EH_PE_signed|DW_EH_PE_data8:
2612 return base + (LONG64)dwarf2_parse_u8(ctx);
2613 default:
2614 FIXME("unsupported encoding %02x\n", encoding);
2615 return 0;
2619 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2621 unsigned char version;
2622 const char* augmentation;
2623 const unsigned char* end;
2624 ULONG_PTR len;
2626 memset(info, 0, sizeof(*info));
2627 info->lsda_encoding = DW_EH_PE_omit;
2628 info->aug_z_format = 0;
2630 /* parse the CIE first */
2631 version = dwarf2_parse_byte(ctx);
2632 if (version != 1)
2634 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2635 return FALSE;
2637 augmentation = (const char*)ctx->data;
2638 ctx->data += strlen(augmentation) + 1;
2640 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2641 info->data_align = dwarf2_leb128_as_signed(ctx);
2642 info->retaddr_reg = dwarf2_parse_byte(ctx);
2643 info->state.cfa_rule = RULE_CFA_OFFSET;
2645 end = NULL;
2646 TRACE("\tparsing augmentation %s\n", augmentation);
2647 if (*augmentation) do
2649 switch (*augmentation)
2651 case 'z':
2652 len = dwarf2_leb128_as_unsigned(ctx);
2653 end = ctx->data + len;
2654 info->aug_z_format = 1;
2655 continue;
2656 case 'L':
2657 info->lsda_encoding = dwarf2_parse_byte(ctx);
2658 continue;
2659 case 'P':
2661 unsigned char encoding = dwarf2_parse_byte(ctx);
2662 /* throw away the indirect bit, as we don't care for the result */
2663 encoding &= ~DW_EH_PE_indirect;
2664 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2665 continue;
2667 case 'R':
2668 info->fde_encoding = dwarf2_parse_byte(ctx);
2669 continue;
2670 case 'S':
2671 info->signal_frame = 1;
2672 continue;
2674 FIXME("unknown augmentation '%c'\n", *augmentation);
2675 if (!end) return FALSE;
2676 break;
2677 } while (*++augmentation);
2678 if (end) ctx->data = end;
2679 return TRUE;
2682 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2683 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2684 struct frame_info* info, BOOL in_eh_frame)
2686 const unsigned char* ptr_blk;
2687 const unsigned char* cie_ptr;
2688 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2689 unsigned len, id;
2690 unsigned long start, range;
2691 unsigned cie_id;
2692 const BYTE* start_data = fde_ctx->data;
2694 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2695 /* skip 0-padding at beginning of section (alignment) */
2696 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
2698 if (dwarf2_parse_u4(fde_ctx))
2700 fde_ctx->data -= 4;
2701 break;
2704 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2706 /* find the FDE for address addr (skip CIE) */
2707 len = dwarf2_parse_u4(fde_ctx);
2708 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2709 ptr_blk = fde_ctx->data + len;
2710 id = dwarf2_parse_u4(fde_ctx);
2711 if (id == cie_id)
2713 last_cie_ptr = fde_ctx->data - 8;
2714 /* we need some bits out of the CIE in order to parse all contents */
2715 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2716 cie_ctx->data = fde_ctx->data;
2717 cie_ctx->end_data = ptr_blk;
2718 cie_ctx->word_size = fde_ctx->word_size;
2719 continue;
2721 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2722 if (cie_ptr != last_cie_ptr)
2724 last_cie_ptr = cie_ptr;
2725 cie_ctx->data = cie_ptr;
2726 cie_ctx->word_size = fde_ctx->word_size;
2727 cie_ctx->end_data = cie_ptr + 4;
2728 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2729 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2731 FIXME("wrong CIE pointer at %x from FDE %x\n",
2732 (unsigned)(cie_ptr - start_data),
2733 (unsigned)(fde_ctx->data - start_data));
2734 return FALSE;
2736 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2738 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2739 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2741 if (addr >= start && addr < start + range)
2743 /* reset the FDE context */
2744 fde_ctx->end_data = ptr_blk;
2746 info->ip = start;
2747 return TRUE;
2750 return FALSE;
2753 static int valid_reg(ULONG_PTR reg)
2755 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2756 return (reg < NB_FRAME_REGS);
2759 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2760 ULONG_PTR last_ip, struct frame_info *info)
2762 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2764 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2766 if (op & 0xc0)
2768 switch (op & 0xc0)
2770 case DW_CFA_advance_loc:
2772 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2773 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2774 info->ip += offset;
2775 break;
2777 case DW_CFA_offset:
2779 ULONG_PTR reg = op & 0x3f;
2780 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2781 if (!valid_reg(reg)) break;
2782 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2783 info->ip,
2784 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2785 offset);
2786 info->state.regs[reg] = offset;
2787 info->state.rules[reg] = RULE_CFA_OFFSET;
2788 break;
2790 case DW_CFA_restore:
2792 ULONG_PTR reg = op & 0x3f;
2793 if (!valid_reg(reg)) break;
2794 TRACE("%lx: DW_CFA_restore %s\n",
2795 info->ip,
2796 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2797 info->state.rules[reg] = RULE_UNSET;
2798 break;
2802 else switch (op)
2804 case DW_CFA_nop:
2805 break;
2806 case DW_CFA_set_loc:
2808 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2809 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2810 info->ip = loc;
2811 break;
2813 case DW_CFA_advance_loc1:
2815 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2816 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2817 info->ip += offset;
2818 break;
2820 case DW_CFA_advance_loc2:
2822 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2823 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2824 info->ip += offset;
2825 break;
2827 case DW_CFA_advance_loc4:
2829 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2830 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2831 info->ip += offset;
2832 break;
2834 case DW_CFA_offset_extended:
2835 case DW_CFA_offset_extended_sf:
2837 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2838 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2839 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2840 if (!valid_reg(reg)) break;
2841 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2842 info->ip,
2843 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2844 offset);
2845 info->state.regs[reg] = offset;
2846 info->state.rules[reg] = RULE_CFA_OFFSET;
2847 break;
2849 case DW_CFA_restore_extended:
2851 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2852 if (!valid_reg(reg)) break;
2853 TRACE("%lx: DW_CFA_restore_extended %s\n",
2854 info->ip,
2855 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2856 info->state.rules[reg] = RULE_UNSET;
2857 break;
2859 case DW_CFA_undefined:
2861 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2862 if (!valid_reg(reg)) break;
2863 TRACE("%lx: DW_CFA_undefined %s\n",
2864 info->ip,
2865 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2866 info->state.rules[reg] = RULE_UNDEFINED;
2867 break;
2869 case DW_CFA_same_value:
2871 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2872 if (!valid_reg(reg)) break;
2873 TRACE("%lx: DW_CFA_same_value %s\n",
2874 info->ip,
2875 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2876 info->state.regs[reg] = reg;
2877 info->state.rules[reg] = RULE_SAME;
2878 break;
2880 case DW_CFA_register:
2882 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2883 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2884 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2885 TRACE("%lx: DW_CFA_register %s == %s\n",
2886 info->ip,
2887 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2888 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2889 info->state.regs[reg] = reg2;
2890 info->state.rules[reg] = RULE_OTHER_REG;
2891 break;
2893 case DW_CFA_remember_state:
2894 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2895 if (info->state_sp >= MAX_SAVED_STATES)
2896 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2897 else
2898 info->state_stack[info->state_sp++] = info->state;
2899 break;
2900 case DW_CFA_restore_state:
2901 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2902 if (!info->state_sp)
2903 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2904 else
2905 info->state = info->state_stack[--info->state_sp];
2906 break;
2907 case DW_CFA_def_cfa:
2908 case DW_CFA_def_cfa_sf:
2910 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2911 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2912 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2913 if (!valid_reg(reg)) break;
2914 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2915 info->ip,
2916 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2917 offset);
2918 info->state.cfa_reg = reg;
2919 info->state.cfa_offset = offset;
2920 info->state.cfa_rule = RULE_CFA_OFFSET;
2921 break;
2923 case DW_CFA_def_cfa_register:
2925 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2926 if (!valid_reg(reg)) break;
2927 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2928 info->ip,
2929 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2930 info->state.cfa_reg = reg;
2931 info->state.cfa_rule = RULE_CFA_OFFSET;
2932 break;
2934 case DW_CFA_def_cfa_offset:
2935 case DW_CFA_def_cfa_offset_sf:
2937 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2938 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2939 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
2940 info->state.cfa_offset = offset;
2941 info->state.cfa_rule = RULE_CFA_OFFSET;
2942 break;
2944 case DW_CFA_def_cfa_expression:
2946 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2947 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2948 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2949 info->state.cfa_offset = expr;
2950 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2951 ctx->data += len;
2952 break;
2954 case DW_CFA_expression:
2955 case DW_CFA_val_expression:
2957 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2958 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2959 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2960 if (!valid_reg(reg)) break;
2961 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2962 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2963 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2964 expr, expr + len);
2965 info->state.regs[reg] = expr;
2966 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2967 ctx->data += len;
2968 break;
2970 case DW_CFA_GNU_args_size:
2971 /* FIXME: should check that GCC is the compiler for this CU */
2973 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2974 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2975 /* ignored */
2976 break;
2978 default:
2979 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2980 break;
2985 /* retrieve a context register from its dwarf number */
2986 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2988 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2989 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2991 if (sz != sizeof(ULONG_PTR))
2993 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2994 return 0;
2996 return *ptr;
2999 /* set a context register from its dwarf number */
3000 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
3001 ULONG_PTR val, BOOL isdebuggee)
3003 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
3004 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
3006 if (isdebuggee)
3008 char tmp[16];
3010 if (sz > sizeof(tmp))
3012 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
3013 return;
3015 if (!sw_read_mem(csw, val, tmp, sz))
3017 WARN("Couldn't read memory at %p\n", (void*)val);
3018 return;
3020 memcpy(ptr, tmp, sz);
3022 else
3024 if (sz != sizeof(ULONG_PTR))
3026 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
3027 return;
3029 *ptr = val;
3033 /* copy a register from one context to another using dwarf number */
3034 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
3036 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
3037 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
3038 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3039 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3041 if (szdst != szsrc)
3043 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3044 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3045 return;
3047 memcpy(ptrdst, ptrsrc, szdst);
3050 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3051 const unsigned char* zp, CONTEXT *context)
3053 dwarf2_traverse_context_t ctx;
3054 ULONG_PTR reg, sz, tmp, stack[64];
3055 int sp = -1;
3056 ULONG_PTR len;
3058 ctx.data = zp;
3059 ctx.end_data = zp + 4;
3060 len = dwarf2_leb128_as_unsigned(&ctx);
3061 ctx.end_data = ctx.data + len;
3062 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3064 while (ctx.data < ctx.end_data)
3066 unsigned char opcode = dwarf2_parse_byte(&ctx);
3068 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3069 stack[++sp] = opcode - DW_OP_lit0;
3070 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3071 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
3072 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3073 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
3074 else switch (opcode)
3076 case DW_OP_nop: break;
3077 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
3078 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3079 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3080 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3081 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3082 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3083 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3084 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3085 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3086 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3087 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3088 case DW_OP_deref:
3089 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
3091 ERR("Couldn't read memory at %lx\n", stack[sp]);
3092 tmp = 0;
3094 stack[sp] = tmp;
3095 break;
3096 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3097 case DW_OP_drop: sp--; break;
3098 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3099 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3100 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3101 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3102 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3103 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3104 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3105 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3106 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3107 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3108 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3109 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3110 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3111 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3112 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3113 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3114 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3115 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3116 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3117 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3118 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3119 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3120 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3121 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3122 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3123 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3124 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3125 case DW_OP_GNU_encoded_addr:
3126 tmp = dwarf2_parse_byte(&ctx);
3127 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3128 break;
3129 case DW_OP_regx:
3130 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
3131 break;
3132 case DW_OP_bregx:
3133 reg = dwarf2_leb128_as_unsigned(&ctx);
3134 tmp = dwarf2_leb128_as_signed(&ctx);
3135 stack[++sp] = get_context_reg(context, reg) + tmp;
3136 break;
3137 case DW_OP_deref_size:
3138 sz = dwarf2_parse_byte(&ctx);
3139 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3141 ERR("Couldn't read memory at %lx\n", stack[sp]);
3142 tmp = 0;
3144 /* do integral promotion */
3145 switch (sz)
3147 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3148 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3149 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3150 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3151 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3153 break;
3154 default:
3155 FIXME("unhandled opcode %02x\n", opcode);
3158 return stack[sp];
3161 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3162 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3164 unsigned int i;
3165 ULONG_PTR value;
3166 CONTEXT new_context = *context;
3168 switch (state->cfa_rule)
3170 case RULE_EXPRESSION:
3171 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3172 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
3174 WARN("Couldn't read memory at %p\n", (void*)*cfa);
3175 return;
3177 break;
3178 case RULE_VAL_EXPRESSION:
3179 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3180 break;
3181 default:
3182 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3183 break;
3185 if (!*cfa) return;
3187 for (i = 0; i < NB_FRAME_REGS; i++)
3189 switch (state->rules[i])
3191 case RULE_UNSET:
3192 case RULE_UNDEFINED:
3193 case RULE_SAME:
3194 break;
3195 case RULE_CFA_OFFSET:
3196 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3197 break;
3198 case RULE_OTHER_REG:
3199 copy_context_reg(&new_context, i, context, state->regs[i]);
3200 break;
3201 case RULE_EXPRESSION:
3202 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3203 set_context_reg(csw, &new_context, i, value, TRUE);
3204 break;
3205 case RULE_VAL_EXPRESSION:
3206 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3207 set_context_reg(csw, &new_context, i, value, FALSE);
3208 break;
3211 *context = new_context;
3214 /***********************************************************************
3215 * dwarf2_virtual_unwind
3218 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3220 struct module_pair pair;
3221 struct frame_info info;
3222 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3223 struct module_format* modfmt;
3224 const unsigned char* end;
3225 DWORD_PTR delta;
3227 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3228 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3229 !module_get_debug(&pair))
3230 return FALSE;
3231 modfmt = pair.effective->format_info[DFI_DWARF];
3232 if (!modfmt) return FALSE;
3233 memset(&info, 0, sizeof(info));
3234 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3235 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3236 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3237 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3238 * in this process the IMAGE section at a different address as the one expected by
3239 * the image
3241 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3242 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3243 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3245 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3246 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3247 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3248 delta = pair.effective->reloc_delta;
3249 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3251 TRACE("Couldn't find information for %lx\n", ip);
3252 return FALSE;
3256 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3257 ip, info.ip, info.code_align, info.data_align,
3258 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
3260 /* if at very beginning of function, return and use default unwinder */
3261 if (ip == info.ip) return FALSE;
3262 execute_cfa_instructions(&cie_ctx, ip, &info);
3264 if (info.aug_z_format) /* get length of augmentation data */
3266 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3267 end = fde_ctx.data + len;
3269 else end = NULL;
3270 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3271 if (end) fde_ctx.data = end;
3273 execute_cfa_instructions(&fde_ctx, ip, &info);
3274 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3276 return TRUE;
3279 static void dwarf2_location_compute(struct process* pcs,
3280 const struct module_format* modfmt,
3281 const struct symt_function* func,
3282 struct location* loc)
3284 struct location frame;
3285 DWORD_PTR ip;
3286 int err;
3287 dwarf2_traverse_context_t lctx;
3289 if (!func->container || func->container->tag != SymTagCompiland)
3291 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3292 err = loc_err_internal;
3294 else
3296 /* instruction pointer relative to compiland's start */
3297 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3299 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3301 switch (loc->kind)
3303 case loc_dwarf2_location_list:
3304 /* Then, if the variable has a location list, find it !! */
3305 if (dwarf2_lookup_loclist(modfmt,
3306 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3307 ip, &lctx))
3308 goto do_compute;
3309 err = loc_err_out_of_scope;
3310 break;
3311 case loc_dwarf2_block:
3312 /* or if we have a copy of an existing block, get ready for it */
3314 unsigned* ptr = (unsigned*)loc->offset;
3316 lctx.data = (const BYTE*)(ptr + 1);
3317 lctx.end_data = lctx.data + *ptr;
3318 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3320 do_compute:
3321 /* now get the variable */
3322 err = compute_location(&lctx, loc, pcs->handle, &frame);
3323 break;
3324 case loc_register:
3325 case loc_regrel:
3326 /* nothing to do */
3327 break;
3328 default:
3329 WARN("Unsupported local kind %d\n", loc->kind);
3330 err = loc_err_internal;
3334 if (err < 0)
3336 loc->kind = loc_register;
3337 loc->reg = err;
3341 #ifdef HAVE_ZLIB
3342 static void *zalloc(void *priv, uInt items, uInt sz)
3344 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3347 static void zfree(void *priv, void *addr)
3349 HeapFree(GetProcessHeap(), 0, addr);
3352 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3353 const char* zsectname,
3354 struct image_section_map* ism)
3356 z_stream z;
3357 LARGE_INTEGER li;
3358 int res;
3359 BOOL ret = FALSE;
3361 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3362 size_t sz = image_get_map_size(ism);
3364 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
3366 ERR("invalid compressed section %s\n", zsectname);
3367 goto out;
3370 #ifdef WORDS_BIGENDIAN
3371 li.u.HighPart = *(DWORD*)&sect[4];
3372 li.u.LowPart = *(DWORD*)&sect[8];
3373 #else
3374 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
3375 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
3376 #endif
3378 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
3379 if (!addr)
3380 goto out;
3382 z.next_in = &sect[12];
3383 z.avail_in = sz - 12;
3384 z.opaque = NULL;
3385 z.zalloc = zalloc;
3386 z.zfree = zfree;
3388 res = inflateInit(&z);
3389 if (res != Z_OK)
3391 FIXME("inflateInit failed with %i / %s\n", res, z.msg);
3392 goto out_free;
3395 do {
3396 z.next_out = addr + z.total_out;
3397 z.avail_out = li.QuadPart - z.total_out;
3398 res = inflate(&z, Z_FINISH);
3399 } while (z.avail_in && res == Z_STREAM_END);
3401 if (res != Z_STREAM_END)
3403 FIXME("Decompression failed with %i / %s\n", res, z.msg);
3404 goto out_end;
3407 ret = TRUE;
3408 section->compressed = TRUE;
3409 section->address = addr;
3410 section->rva = image_get_map_rva(ism);
3411 section->size = z.total_out;
3413 out_end:
3414 inflateEnd(&z);
3415 out_free:
3416 if (!ret)
3417 HeapFree(GetProcessHeap(), 0, addr);
3418 out:
3419 image_unmap_section(ism);
3420 return ret;
3423 #endif
3425 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3426 const char* sectname, const char* zsectname,
3427 struct image_section_map* ism)
3429 struct image_section_map local_ism;
3431 if (!ism) ism = &local_ism;
3433 section->compressed = FALSE;
3434 if (image_find_section(fmap, sectname, ism))
3436 section->address = (const BYTE*)image_map_section(ism);
3437 section->size = image_get_map_size(ism);
3438 section->rva = image_get_map_rva(ism);
3439 return TRUE;
3442 section->address = NULL;
3443 section->size = 0;
3444 section->rva = 0;
3446 if (zsectname && image_find_section(fmap, zsectname, ism))
3448 #ifdef HAVE_ZLIB
3449 return dwarf2_init_zsection(section, zsectname, ism);
3450 #else
3451 FIXME("dbghelp not built with zlib, but compressed section found\n" );
3452 #endif
3455 return FALSE;
3458 static inline void dwarf2_fini_section(dwarf2_section_t* section)
3460 if (section->compressed)
3461 HeapFree(GetProcessHeap(), 0, (void*)section->address);
3464 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3466 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
3467 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
3468 HeapFree(GetProcessHeap(), 0, modfmt);
3471 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3472 const struct elf_thunk_area* thunks,
3473 struct image_file_map* fmap)
3475 dwarf2_section_t eh_frame, section[section_max];
3476 dwarf2_traverse_context_t mod_ctx;
3477 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3478 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3479 BOOL ret = TRUE;
3480 struct module_format* dwarf2_modfmt;
3482 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect);
3483 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
3484 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
3485 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
3486 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
3487 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
3489 /* to do anything useful we need either .eh_frame or .debug_info */
3490 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3491 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3493 ret = FALSE;
3494 goto leave;
3497 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3499 /* debug info might have a different base address than .so file
3500 * when elf file is prelinked after splitting off debug info
3501 * adjust symbol base addresses accordingly
3503 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3506 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3508 mod_ctx.data = section[section_debug].address;
3509 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3510 mod_ctx.word_size = 0; /* will be correctly set later on */
3512 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3513 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3514 if (!dwarf2_modfmt)
3516 ret = FALSE;
3517 goto leave;
3519 dwarf2_modfmt->module = module;
3520 dwarf2_modfmt->remove = dwarf2_module_remove;
3521 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3522 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3523 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3524 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3526 /* As we'll need later some sections' content, we won't unmap these
3527 * sections upon existing this function
3529 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
3530 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
3531 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3533 while (mod_ctx.data < mod_ctx.end_data)
3535 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3537 dwarf2_modfmt->module->module.SymType = SymDia;
3538 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3539 /* FIXME: we could have a finer grain here */
3540 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3541 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3542 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3543 dwarf2_modfmt->module->module.Publics = TRUE;
3545 /* set the word_size for eh_frame parsing */
3546 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8;
3548 leave:
3549 dwarf2_fini_section(&section[section_debug]);
3550 dwarf2_fini_section(&section[section_abbrev]);
3551 dwarf2_fini_section(&section[section_string]);
3552 dwarf2_fini_section(&section[section_line]);
3553 dwarf2_fini_section(&section[section_ranges]);
3555 image_unmap_section(&debug_sect);
3556 image_unmap_section(&debug_abbrev_sect);
3557 image_unmap_section(&debug_str_sect);
3558 image_unmap_section(&debug_line_sect);
3559 image_unmap_section(&debug_ranges_sect);
3560 if (!ret) image_unmap_section(&eh_frame_sect);
3562 return ret;