dbghelp: When managing DW_OP_deref_size, always ensure we read the size parameter.
[wine/multimedia.git] / dlls / dbghelp / dwarf.c
blob5d6fd25b17c3fe5c6bf1aa0aa44fa778449e1f0f
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 #ifndef PATH_MAX
43 #define PATH_MAX MAX_PATH
44 #endif
45 #include <assert.h>
46 #include <stdarg.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winuser.h"
51 #include "ole2.h"
52 #include "oleauto.h"
54 #include "dbghelp_private.h"
55 #include "image_private.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
61 /* FIXME:
62 * - Functions:
63 * o unspecified parameters
64 * o inlined functions
65 * o Debug{Start|End}Point
66 * o CFA
67 * - Udt
68 * o proper types loading (nesting)
71 #if 0
72 static void dump(const void* ptr, unsigned len)
74 int i, j;
75 BYTE msg[128];
76 static const char hexof[] = "0123456789abcdef";
77 const BYTE* x = ptr;
79 for (i = 0; i < len; i += 16)
81 sprintf(msg, "%08x: ", i);
82 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
83 for (j = 0; j < min(16, len - i); j++)
85 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
86 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
87 msg[10 + 3 * j + 2] = ' ';
88 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
89 x[i + j] : '.';
91 msg[10 + 3 * 16] = ' ';
92 msg[10 + 3 * 16 + 1 + 16] = '\0';
93 TRACE("%s\n", msg);
96 #endif
98 /**
100 * Main Specs:
101 * http://www.eagercon.com/dwarf/dwarf3std.htm
102 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
104 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
106 * example of projects who do dwarf2 parsing:
107 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
108 * http://elis.ugent.be/diota/log/ltrace_elf.c
110 #include "dwarf.h"
113 * Parsers
116 typedef struct dwarf2_abbrev_entry_attr_s
118 unsigned long attribute;
119 unsigned long form;
120 struct dwarf2_abbrev_entry_attr_s* next;
121 } dwarf2_abbrev_entry_attr_t;
123 typedef struct dwarf2_abbrev_entry_s
125 unsigned long entry_code;
126 unsigned long tag;
127 unsigned char have_child;
128 unsigned num_attr;
129 dwarf2_abbrev_entry_attr_t* attrs;
130 } dwarf2_abbrev_entry_t;
132 struct dwarf2_block
134 unsigned size;
135 const unsigned char* ptr;
138 struct attribute
140 unsigned long form;
141 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
142 union
144 unsigned long uvalue;
145 ULONGLONG lluvalue;
146 long svalue;
147 const char* string;
148 struct dwarf2_block block;
149 } u;
152 typedef struct dwarf2_debug_info_s
154 const dwarf2_abbrev_entry_t*abbrev;
155 struct symt* symt;
156 const unsigned char** data;
157 struct vector children;
158 struct dwarf2_debug_info_s* parent;
159 } dwarf2_debug_info_t;
161 typedef struct dwarf2_section_s
163 const unsigned char* address;
164 unsigned size;
165 DWORD_PTR rva;
166 } dwarf2_section_t;
168 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
170 typedef struct dwarf2_traverse_context_s
172 const unsigned char* data;
173 const unsigned char* end_data;
174 unsigned char word_size;
175 } dwarf2_traverse_context_t;
177 /* symt_cache indexes */
178 #define sc_void 0
179 #define sc_int1 1
180 #define sc_int2 2
181 #define sc_int4 3
182 #define sc_num 4
184 typedef struct dwarf2_parse_context_s
186 const dwarf2_section_t* sections;
187 unsigned section;
188 struct pool pool;
189 struct module* module;
190 struct symt_compiland* compiland;
191 const struct elf_thunk_area*thunks;
192 struct sparse_array abbrev_table;
193 struct sparse_array debug_info_table;
194 unsigned long load_offset;
195 unsigned long ref_offset;
196 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
197 char* cpp_name;
198 } dwarf2_parse_context_t;
200 /* stored in the dbghelp's module internal structure for later reuse */
201 struct dwarf2_module_info_s
203 dwarf2_section_t debug_loc;
204 dwarf2_section_t debug_frame;
205 dwarf2_section_t eh_frame;
206 unsigned char word_size;
209 #define loc_dwarf2_location_list (loc_user + 0)
210 #define loc_dwarf2_block (loc_user + 1)
212 /* forward declarations */
213 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
215 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
217 return *ptr;
220 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
222 unsigned char uvalue = dwarf2_get_byte(ctx->data);
223 ctx->data += 1;
224 return uvalue;
227 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
229 return *(const UINT16*)ptr;
232 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
234 unsigned short uvalue = dwarf2_get_u2(ctx->data);
235 ctx->data += 2;
236 return uvalue;
239 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
241 return *(const UINT32*)ptr;
244 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
246 unsigned long uvalue = dwarf2_get_u4(ctx->data);
247 ctx->data += 4;
248 return uvalue;
251 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
253 return *(const UINT64*)ptr;
256 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
258 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
259 ctx->data += 8;
260 return uvalue;
263 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
265 unsigned long ret = 0;
266 unsigned char byte;
267 unsigned shift = 0;
271 byte = dwarf2_get_byte(ptr++);
272 ret |= (byte & 0x7f) << shift;
273 shift += 7;
274 } while (byte & 0x80);
276 if (end) *end = ptr;
277 return ret;
280 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
282 unsigned long ret;
284 assert(ctx);
286 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
288 return ret;
291 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
293 long ret = 0;
294 unsigned char byte;
295 unsigned shift = 0;
296 const unsigned size = sizeof(int) * 8;
300 byte = dwarf2_get_byte(ptr++);
301 ret |= (byte & 0x7f) << shift;
302 shift += 7;
303 } while (byte & 0x80);
304 if (end) *end = ptr;
306 /* as spec: sign bit of byte is 2nd high order bit (80x40)
307 * -> 0x80 is used as flag.
309 if ((shift < size) && (byte & 0x40))
311 ret |= - (1 << shift);
313 return ret;
316 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
318 long ret = 0;
320 assert(ctx);
322 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
323 return ret;
326 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
328 unsigned ret;
329 for (ret = 0; ctx->data[ret] & 0x80; ret++);
330 return ret + 1;
333 /******************************************************************
334 * dwarf2_get_addr
336 * Returns an address.
337 * We assume that in all cases word size from Dwarf matches the size of
338 * addresses in platform where the exec is compiled.
340 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
342 unsigned long ret;
344 switch (word_size)
346 case 4:
347 ret = dwarf2_get_u4(ptr);
348 break;
349 case 8:
350 ret = dwarf2_get_u8(ptr);
351 break;
352 default:
353 FIXME("Unsupported Word Size %u\n", word_size);
354 ret = 0;
356 return ret;
359 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
361 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
362 ctx->data += ctx->word_size;
363 return ret;
366 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
368 return wine_dbg_sprintf("ctx(%p)", ctx->data);
371 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
373 return wine_dbg_sprintf("ctx(%p,%s)",
374 ctx, debugstr_w(ctx->module->module.ModuleName));
377 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
379 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
380 di->abbrev, di->symt);
383 static dwarf2_abbrev_entry_t*
384 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
385 unsigned long entry_code)
387 assert( NULL != abbrev_table );
388 return sparse_array_find(abbrev_table, entry_code);
391 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
392 struct sparse_array* abbrev_table,
393 struct pool* pool)
395 unsigned long entry_code;
396 dwarf2_abbrev_entry_t* abbrev_entry;
397 dwarf2_abbrev_entry_attr_t* new = NULL;
398 dwarf2_abbrev_entry_attr_t* last = NULL;
399 unsigned long attribute;
400 unsigned long form;
402 assert( NULL != abbrev_ctx );
404 TRACE("%s, end at %p\n",
405 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
407 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
408 while (abbrev_ctx->data < abbrev_ctx->end_data)
410 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
411 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
412 TRACE("found entry_code %lu\n", entry_code);
413 if (!entry_code)
415 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
416 break;
418 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
419 assert( NULL != abbrev_entry );
421 abbrev_entry->entry_code = entry_code;
422 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
423 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
424 abbrev_entry->attrs = NULL;
425 abbrev_entry->num_attr = 0;
427 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
428 abbrev_table, sparse_array_length(abbrev_table),
429 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
431 last = NULL;
432 while (1)
434 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
435 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
436 if (!attribute) break;
438 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
439 assert(new);
441 new->attribute = attribute;
442 new->form = form;
443 new->next = NULL;
444 if (abbrev_entry->attrs) last->next = new;
445 else abbrev_entry->attrs = new;
446 last = new;
447 abbrev_entry->num_attr++;
450 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
453 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
454 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
456 unsigned step;
458 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
460 switch (abbrev_attr->form)
462 case DW_FORM_ref_addr:
463 case DW_FORM_addr: step = ctx->word_size; break;
464 case DW_FORM_flag:
465 case DW_FORM_data1:
466 case DW_FORM_ref1: step = 1; break;
467 case DW_FORM_data2:
468 case DW_FORM_ref2: step = 2; break;
469 case DW_FORM_data4:
470 case DW_FORM_ref4:
471 case DW_FORM_strp: step = 4; break;
472 case DW_FORM_data8:
473 case DW_FORM_ref8: step = 8; break;
474 case DW_FORM_sdata:
475 case DW_FORM_ref_udata:
476 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
477 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
478 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
479 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
480 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
481 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
482 default:
483 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
484 return;
486 ctx->data += step;
489 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
490 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
491 const unsigned char* data,
492 struct attribute* attr)
494 attr->form = abbrev_attr->form;
495 switch (attr->form)
497 case DW_FORM_ref_addr:
498 case DW_FORM_addr:
499 attr->u.uvalue = dwarf2_get_addr(data,
500 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
501 TRACE("addr<0x%lx>\n", attr->u.uvalue);
502 break;
504 case DW_FORM_flag:
505 attr->u.uvalue = dwarf2_get_byte(data);
506 TRACE("flag<0x%lx>\n", attr->u.uvalue);
507 break;
509 case DW_FORM_data1:
510 attr->u.uvalue = dwarf2_get_byte(data);
511 TRACE("data1<%lu>\n", attr->u.uvalue);
512 break;
514 case DW_FORM_data2:
515 attr->u.uvalue = dwarf2_get_u2(data);
516 TRACE("data2<%lu>\n", attr->u.uvalue);
517 break;
519 case DW_FORM_data4:
520 attr->u.uvalue = dwarf2_get_u4(data);
521 TRACE("data4<%lu>\n", attr->u.uvalue);
522 break;
524 case DW_FORM_data8:
525 attr->u.lluvalue = dwarf2_get_u8(data);
526 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
527 break;
529 case DW_FORM_ref1:
530 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
531 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
532 break;
534 case DW_FORM_ref2:
535 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
536 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
537 break;
539 case DW_FORM_ref4:
540 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
541 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
542 break;
544 case DW_FORM_ref8:
545 FIXME("Unhandled 64 bit support\n");
546 break;
548 case DW_FORM_sdata:
549 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
550 break;
552 case DW_FORM_ref_udata:
553 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
554 break;
556 case DW_FORM_udata:
557 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
558 break;
560 case DW_FORM_string:
561 attr->u.string = (const char *)data;
562 TRACE("string<%s>\n", attr->u.string);
563 break;
565 case DW_FORM_strp:
567 unsigned long offset = dwarf2_get_u4(data);
568 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
570 TRACE("strp<%s>\n", attr->u.string);
571 break;
573 case DW_FORM_block:
574 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
575 break;
577 case DW_FORM_block1:
578 attr->u.block.size = dwarf2_get_byte(data);
579 attr->u.block.ptr = data + 1;
580 break;
582 case DW_FORM_block2:
583 attr->u.block.size = dwarf2_get_u2(data);
584 attr->u.block.ptr = data + 2;
585 break;
587 case DW_FORM_block4:
588 attr->u.block.size = dwarf2_get_u4(data);
589 attr->u.block.ptr = data + 4;
590 break;
592 default:
593 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
594 break;
598 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
599 const dwarf2_debug_info_t* di,
600 unsigned at, struct attribute* attr)
602 unsigned i, refidx = 0;
603 dwarf2_abbrev_entry_attr_t* abbrev_attr;
604 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
606 attr->gotten_from = attr_direct;
607 while (di)
609 ref_abbrev_attr = NULL;
610 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
612 if (abbrev_attr->attribute == at)
614 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
615 return TRUE;
617 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
618 abbrev_attr->attribute == DW_AT_specification) &&
619 at != DW_AT_sibling)
621 if (ref_abbrev_attr)
622 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
623 ref_abbrev_attr = abbrev_attr;
624 refidx = i;
625 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
626 attr_abstract_origin : attr_specification;
629 /* do we have either an abstract origin or a specification debug entry to look into ? */
630 if (!ref_abbrev_attr) break;
631 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
632 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
633 FIXME("Should have found the debug info entry\n");
635 return FALSE;
638 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
640 #define Wine_DW_no_register 0x7FFFFFFF
642 static unsigned dwarf2_map_register(int regno)
644 if (regno == Wine_DW_no_register)
646 FIXME("What the heck map reg 0x%x\n",regno);
647 return 0;
649 return dbghelp_current_cpu->map_dwarf_register(regno);
652 static enum location_error
653 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
654 HANDLE hproc, const struct location* frame)
656 DWORD_PTR tmp, stack[64];
657 unsigned stk;
658 unsigned char op;
659 BOOL piece_found = FALSE;
661 stack[stk = 0] = 0;
663 loc->kind = loc_absolute;
664 loc->reg = Wine_DW_no_register;
666 while (ctx->data < ctx->end_data)
668 op = dwarf2_parse_byte(ctx);
670 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
671 stack[++stk] = op - DW_OP_lit0;
672 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
674 /* dbghelp APIs don't know how to cope with this anyway
675 * (for example 'long long' stored in two registers)
676 * FIXME: We should tell winedbg how to deal with it (sigh)
678 if (!piece_found)
680 if (loc->reg != Wine_DW_no_register)
681 FIXME("Only supporting one reg (%d -> %d)\n",
682 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
683 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
685 loc->kind = loc_register;
687 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
689 /* dbghelp APIs don't know how to cope with this anyway
690 * (for example 'long long' stored in two registers)
691 * FIXME: We should tell winedbg how to deal with it (sigh)
693 if (!piece_found)
695 if (loc->reg != Wine_DW_no_register)
696 FIXME("Only supporting one breg (%d -> %d)\n",
697 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
698 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
700 stack[++stk] = dwarf2_leb128_as_signed(ctx);
701 loc->kind = loc_regrel;
703 else switch (op)
705 case DW_OP_nop: break;
706 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
707 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
708 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
709 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
710 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
711 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
712 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
713 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
714 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
715 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
716 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
717 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
718 case DW_OP_drop: stk--; break;
719 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
720 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
721 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
722 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
723 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
724 case DW_OP_neg: stack[stk] = -stack[stk]; break;
725 case DW_OP_not: stack[stk] = ~stack[stk]; break;
726 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
727 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
728 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
729 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
730 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
731 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
732 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
733 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
734 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
735 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
736 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
737 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
738 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
739 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
740 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
741 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
742 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
743 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
744 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
745 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
746 case DW_OP_regx:
747 tmp = dwarf2_leb128_as_unsigned(ctx);
748 if (!piece_found)
750 if (loc->reg != Wine_DW_no_register)
751 FIXME("Only supporting one reg\n");
752 loc->reg = dwarf2_map_register(tmp);
754 loc->kind = loc_register;
755 break;
756 case DW_OP_bregx:
757 tmp = dwarf2_leb128_as_unsigned(ctx);
758 if (loc->reg != Wine_DW_no_register)
759 FIXME("Only supporting one regx\n");
760 loc->reg = dwarf2_map_register(tmp);
761 stack[++stk] = dwarf2_leb128_as_signed(ctx);
762 loc->kind = loc_regrel;
763 break;
764 case DW_OP_fbreg:
765 if (loc->reg != Wine_DW_no_register)
766 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
767 if (frame && frame->kind == loc_register)
769 loc->kind = loc_regrel;
770 loc->reg = frame->reg;
771 stack[++stk] = dwarf2_leb128_as_signed(ctx);
773 else if (frame && frame->kind == loc_regrel)
775 loc->kind = loc_regrel;
776 loc->reg = frame->reg;
777 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
779 else
781 /* FIXME: this could be later optimized by not recomputing
782 * this very location expression
784 loc->kind = loc_dwarf2_block;
785 stack[++stk] = dwarf2_leb128_as_signed(ctx);
787 break;
788 case DW_OP_piece:
790 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
791 WARN("Not handling OP_piece (size=%d)\n", sz);
792 piece_found = TRUE;
794 break;
795 case DW_OP_deref:
796 if (!stk)
798 FIXME("Unexpected empty stack\n");
799 return loc_err_internal;
801 if (loc->reg != Wine_DW_no_register)
803 WARN("Too complex expression for deref\n");
804 return loc_err_too_complex;
806 if (hproc)
808 DWORD_PTR addr = stack[stk--];
809 DWORD_PTR deref;
811 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
813 WARN("Couldn't read memory at %lx\n", addr);
814 return loc_err_cant_read;
816 stack[++stk] = deref;
818 else
820 loc->kind = loc_dwarf2_block;
822 break;
823 case DW_OP_deref_size:
824 if (!stk)
826 FIXME("Unexpected empty stack\n");
827 return loc_err_internal;
829 if (loc->reg != Wine_DW_no_register)
831 WARN("Too complex expression for deref\n");
832 return loc_err_too_complex;
834 if (hproc)
836 DWORD_PTR addr = stack[stk--];
837 BYTE derefsize = dwarf2_parse_byte(ctx);
838 DWORD64 deref;
840 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
842 WARN("Couldn't read memory at %lx\n", addr);
843 return loc_err_cant_read;
846 switch (derefsize)
848 case 1: stack[++stk] = *(unsigned char*)&deref; break;
849 case 2: stack[++stk] = *(unsigned short*)&deref; break;
850 case 4: stack[++stk] = *(DWORD*)&deref; break;
851 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
854 else
856 dwarf2_parse_byte(ctx);
857 loc->kind = loc_dwarf2_block;
859 break;
860 case DW_OP_stack_value:
861 /* Expected behaviour is that this is the last instruction of this
862 * expression and just the "top of stack" value should be put to loc->offset. */
863 break;
864 default:
865 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
866 FIXME("Unhandled attr op: %x\n", op);
867 /* FIXME else unhandled extension */
868 return loc_err_internal;
871 loc->offset = stack[stk];
872 return 0;
875 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
876 const dwarf2_debug_info_t* di,
877 unsigned long dw,
878 struct location* loc,
879 const struct location* frame)
881 struct attribute xloc;
883 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
885 switch (xloc.form)
887 case DW_FORM_data1: case DW_FORM_data2:
888 case DW_FORM_udata: case DW_FORM_sdata:
889 loc->kind = loc_absolute;
890 loc->reg = 0;
891 loc->offset = xloc.u.uvalue;
892 return TRUE;
893 case DW_FORM_data4: case DW_FORM_data8:
894 loc->kind = loc_dwarf2_location_list;
895 loc->reg = Wine_DW_no_register;
896 loc->offset = xloc.u.uvalue;
897 return TRUE;
898 case DW_FORM_block:
899 case DW_FORM_block1:
900 case DW_FORM_block2:
901 case DW_FORM_block4:
902 break;
903 default: FIXME("Unsupported yet form %lx\n", xloc.form);
904 return FALSE;
907 /* assume we have a block form */
909 if (xloc.u.block.size)
911 dwarf2_traverse_context_t lctx;
912 enum location_error err;
914 lctx.data = xloc.u.block.ptr;
915 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
916 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
918 err = compute_location(&lctx, loc, NULL, frame);
919 if (err < 0)
921 loc->kind = loc_error;
922 loc->reg = err;
924 else if (loc->kind == loc_dwarf2_block)
926 unsigned* ptr = pool_alloc(&ctx->module->pool,
927 sizeof(unsigned) + xloc.u.block.size);
928 *ptr = xloc.u.block.size;
929 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
930 loc->offset = (unsigned long)ptr;
933 return TRUE;
936 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
937 const dwarf2_debug_info_t* di)
939 struct attribute attr;
941 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
943 dwarf2_debug_info_t* type;
945 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
946 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
947 if (!type->symt)
949 /* load the debug info entity */
950 dwarf2_load_one_entry(ctx, type);
952 return type->symt;
954 return NULL;
957 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
959 char* last;
960 struct attribute diname;
961 struct attribute spec;
963 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
964 if (!ctx->cpp_name)
965 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
966 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
967 strcpy(last, name);
969 /* if the di is a definition, but has also a (previous) declaration, then scope must
970 * be gotten from declaration not definition
972 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
974 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
975 if (!di)
977 FIXME("Should have found the debug info entry\n");
978 return NULL;
982 for (di = di->parent; di; di = di->parent)
984 switch (di->abbrev->tag)
986 case DW_TAG_namespace:
987 case DW_TAG_structure_type:
988 case DW_TAG_class_type:
989 case DW_TAG_interface_type:
990 case DW_TAG_union_type:
991 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
993 size_t len = strlen(diname.u.string);
994 last -= 2 + len;
995 if (last < ctx->cpp_name) return NULL;
996 memcpy(last, diname.u.string, len);
997 last[len] = last[len + 1] = ':';
999 break;
1000 default:
1001 break;
1004 return last;
1007 /******************************************************************
1008 * dwarf2_read_range
1010 * read a range for a given debug_info (either using AT_range attribute, in which
1011 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1012 * in all cases, range is relative to beginning of compilation unit
1014 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1015 unsigned long* plow, unsigned long* phigh)
1017 struct attribute range;
1019 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1021 dwarf2_traverse_context_t traverse;
1022 unsigned long low, high;
1024 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1025 traverse.end_data = ctx->sections[section_ranges].address +
1026 ctx->sections[section_ranges].size;
1027 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1029 *plow = ULONG_MAX;
1030 *phigh = 0;
1031 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1033 low = dwarf2_parse_addr(&traverse);
1034 high = dwarf2_parse_addr(&traverse);
1035 if (low == 0 && high == 0) break;
1036 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1037 if (low < *plow) *plow = low;
1038 if (high > *phigh) *phigh = high;
1040 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1041 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1043 return TRUE;
1045 else
1047 struct attribute low_pc;
1048 struct attribute high_pc;
1050 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1051 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1052 return FALSE;
1053 *plow = low_pc.u.uvalue;
1054 *phigh = high_pc.u.uvalue;
1055 return TRUE;
1059 /******************************************************************
1060 * dwarf2_read_one_debug_info
1062 * Loads into memory one debug info entry, and recursively its children (if any)
1064 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1065 dwarf2_traverse_context_t* traverse,
1066 dwarf2_debug_info_t* parent_di,
1067 dwarf2_debug_info_t** pdi)
1069 const dwarf2_abbrev_entry_t*abbrev;
1070 unsigned long entry_code;
1071 unsigned long offset;
1072 dwarf2_debug_info_t* di;
1073 dwarf2_debug_info_t* child;
1074 dwarf2_debug_info_t** where;
1075 dwarf2_abbrev_entry_attr_t* attr;
1076 unsigned i;
1077 struct attribute sibling;
1079 offset = traverse->data - ctx->sections[ctx->section].address;
1080 entry_code = dwarf2_leb128_as_unsigned(traverse);
1081 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1082 if (!entry_code)
1084 *pdi = NULL;
1085 return TRUE;
1087 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1088 if (!abbrev)
1090 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1091 return FALSE;
1093 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1094 if (!di) return FALSE;
1095 di->abbrev = abbrev;
1096 di->symt = NULL;
1097 di->parent = parent_di;
1099 if (abbrev->num_attr)
1101 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1102 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1104 di->data[i] = traverse->data;
1105 dwarf2_swallow_attribute(traverse, attr);
1108 else di->data = NULL;
1109 if (abbrev->have_child)
1111 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1112 while (traverse->data < traverse->end_data)
1114 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1115 if (!child) break;
1116 where = vector_add(&di->children, &ctx->pool);
1117 if (!where) return FALSE;
1118 *where = child;
1121 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1122 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1124 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1125 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1126 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1128 *pdi = di;
1129 return TRUE;
1132 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1133 dwarf2_debug_info_t* di)
1135 struct attribute spec;
1137 while (di)
1139 if (di->abbrev->have_child)
1140 return &di->children;
1141 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1142 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1143 FIXME("Should have found the debug info entry\n");
1145 return NULL;
1148 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1149 dwarf2_debug_info_t* di)
1151 struct attribute name;
1152 struct attribute size;
1153 struct attribute encoding;
1154 enum BasicType bt;
1155 int cache_idx = -1;
1156 if (di->symt) return di->symt;
1158 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1160 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1161 name.u.string = NULL;
1162 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1163 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1165 switch (encoding.u.uvalue)
1167 case DW_ATE_void: bt = btVoid; break;
1168 case DW_ATE_address: bt = btULong; break;
1169 case DW_ATE_boolean: bt = btBool; break;
1170 case DW_ATE_complex_float: bt = btComplex; break;
1171 case DW_ATE_float: bt = btFloat; break;
1172 case DW_ATE_signed: bt = btInt; break;
1173 case DW_ATE_unsigned: bt = btUInt; break;
1174 case DW_ATE_signed_char: bt = btChar; break;
1175 case DW_ATE_unsigned_char: bt = btChar; break;
1176 default: bt = btNoType; break;
1178 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1179 switch (bt)
1181 case btVoid:
1182 assert(size.u.uvalue == 0);
1183 cache_idx = sc_void;
1184 break;
1185 case btInt:
1186 switch (size.u.uvalue)
1188 case 1: cache_idx = sc_int1; break;
1189 case 2: cache_idx = sc_int2; break;
1190 case 4: cache_idx = sc_int4; break;
1192 break;
1193 default: break;
1195 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1196 ctx->symt_cache[cache_idx] = di->symt;
1198 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1199 return di->symt;
1202 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1203 dwarf2_debug_info_t* di)
1205 struct symt* ref_type;
1206 struct attribute name;
1208 if (di->symt) return di->symt;
1210 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1212 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1213 ref_type = dwarf2_lookup_type(ctx, di);
1215 if (name.u.string)
1216 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1217 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1218 return di->symt;
1221 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1222 dwarf2_debug_info_t* di)
1224 struct symt* ref_type;
1225 struct attribute size;
1227 if (di->symt) return di->symt;
1229 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1231 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1232 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1234 ref_type = ctx->symt_cache[sc_void];
1235 assert(ref_type);
1237 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1238 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1239 return di->symt;
1242 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1243 dwarf2_debug_info_t* di)
1245 struct symt* ref_type;
1246 struct symt* idx_type = NULL;
1247 struct attribute min, max, cnt;
1248 dwarf2_debug_info_t* child;
1249 unsigned int i;
1250 const struct vector* children;
1252 if (di->symt) return di->symt;
1254 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1256 ref_type = dwarf2_lookup_type(ctx, di);
1258 if (!(children = dwarf2_get_di_children(ctx, di)))
1260 /* fake an array with unknown size */
1261 /* FIXME: int4 even on 64bit machines??? */
1262 idx_type = ctx->symt_cache[sc_int4];
1263 min.u.uvalue = 0;
1264 max.u.uvalue = -1;
1266 else for (i = 0; i < vector_length(children); i++)
1268 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1269 switch (child->abbrev->tag)
1271 case DW_TAG_subrange_type:
1272 idx_type = dwarf2_lookup_type(ctx, child);
1273 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1274 min.u.uvalue = 0;
1275 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1276 max.u.uvalue = 0;
1277 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1278 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1279 break;
1280 default:
1281 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1282 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1283 break;
1286 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1287 return di->symt;
1290 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1291 dwarf2_debug_info_t* di)
1293 struct symt* ref_type;
1295 if (di->symt) return di->symt;
1297 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1299 ref_type = dwarf2_lookup_type(ctx, di);
1300 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1301 di->symt = ref_type;
1303 return ref_type;
1306 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1307 dwarf2_debug_info_t* di)
1309 struct symt* ref_type;
1311 if (di->symt) return di->symt;
1313 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1315 ref_type = dwarf2_lookup_type(ctx, di);
1316 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1317 di->symt = ref_type;
1319 return ref_type;
1322 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1323 dwarf2_debug_info_t* di)
1325 struct symt* ref_type = NULL;
1327 if (di->symt) return di->symt;
1329 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1331 ref_type = dwarf2_lookup_type(ctx, di);
1332 /* FIXME: for now, we hard-wire C++ references to pointers */
1333 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1335 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1337 return di->symt;
1340 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1341 dwarf2_debug_info_t* di,
1342 struct symt_udt* parent)
1344 struct symt* elt_type;
1345 struct attribute name;
1346 struct attribute bit_size;
1347 struct attribute bit_offset;
1348 struct location loc;
1350 assert(parent);
1352 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1354 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1355 elt_type = dwarf2_lookup_type(ctx, di);
1356 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1358 if (loc.kind != loc_absolute)
1360 FIXME("Found register, while not expecting it\n");
1361 loc.offset = 0;
1363 else
1364 TRACE("found member_location at %s -> %lu\n",
1365 dwarf2_debug_ctx(ctx), loc.offset);
1367 else
1368 loc.offset = 0;
1369 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1370 bit_size.u.uvalue = 0;
1371 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1373 /* FIXME: we should only do this when implementation is LSB (which is
1374 * the case on i386 processors)
1376 struct attribute nbytes;
1377 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1379 DWORD64 size;
1380 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1381 (unsigned long)size : 0;
1383 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1385 else bit_offset.u.uvalue = 0;
1386 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1387 (loc.offset << 3) + bit_offset.u.uvalue,
1388 bit_size.u.uvalue);
1390 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1393 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1394 dwarf2_debug_info_t* di);
1396 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1397 dwarf2_debug_info_t* di,
1398 enum UdtKind udt)
1400 struct attribute name;
1401 struct attribute size;
1402 struct vector* children;
1403 dwarf2_debug_info_t*child;
1404 unsigned int i;
1406 if (di->symt) return di->symt;
1408 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1410 /* quirk... FIXME provide real support for anonymous UDTs */
1411 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1412 name.u.string = "zz_anon_zz";
1413 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1415 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1416 size.u.uvalue, udt)->symt;
1418 children = dwarf2_get_di_children(ctx, di);
1419 if (children) for (i = 0; i < vector_length(children); i++)
1421 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1423 switch (child->abbrev->tag)
1425 case DW_TAG_member:
1426 /* FIXME: should I follow the sibling stuff ?? */
1427 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1428 break;
1429 case DW_TAG_enumeration_type:
1430 dwarf2_parse_enumeration_type(ctx, child);
1431 break;
1432 case DW_TAG_subprogram:
1433 dwarf2_parse_subprogram(ctx, child);
1434 break;
1435 case DW_TAG_structure_type:
1436 case DW_TAG_class_type:
1437 case DW_TAG_union_type:
1438 case DW_TAG_typedef:
1439 /* FIXME: we need to handle nested udt definitions */
1440 case DW_TAG_inheritance:
1441 case DW_TAG_template_type_param:
1442 case DW_TAG_template_value_param:
1443 case DW_TAG_variable:
1444 case DW_TAG_imported_declaration:
1445 case DW_TAG_ptr_to_member_type:
1446 /* FIXME: some C++ related stuff */
1447 break;
1448 default:
1449 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1450 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1451 break;
1455 return di->symt;
1458 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1459 dwarf2_debug_info_t* di,
1460 struct symt_enum* parent)
1462 struct attribute name;
1463 struct attribute value;
1465 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1467 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1468 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1469 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1471 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1474 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1475 dwarf2_debug_info_t* di)
1477 struct attribute name;
1478 struct attribute size;
1479 struct symt_basic* basetype;
1480 struct vector* children;
1481 dwarf2_debug_info_t*child;
1482 unsigned int i;
1484 if (di->symt) return di->symt;
1486 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1488 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1489 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1491 switch (size.u.uvalue) /* FIXME: that's wrong */
1493 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1494 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1495 default:
1496 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1499 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1501 children = dwarf2_get_di_children(ctx, di);
1502 /* FIXME: should we use the sibling stuff ?? */
1503 if (children) for (i = 0; i < vector_length(children); i++)
1505 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1507 switch (child->abbrev->tag)
1509 case DW_TAG_enumerator:
1510 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1511 break;
1512 default:
1513 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1514 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1517 return di->symt;
1520 /* structure used to pass information around when parsing a subprogram */
1521 typedef struct dwarf2_subprogram_s
1523 dwarf2_parse_context_t* ctx;
1524 struct symt_function* func;
1525 BOOL non_computed_variable;
1526 struct location frame;
1527 } dwarf2_subprogram_t;
1529 /******************************************************************
1530 * dwarf2_parse_variable
1532 * Parses any variable (parameter, local/global variable)
1534 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1535 struct symt_block* block,
1536 dwarf2_debug_info_t* di)
1538 struct symt* param_type;
1539 struct attribute name, value;
1540 struct location loc;
1541 BOOL is_pmt;
1543 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1545 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1546 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1548 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1549 /* cannot do much without the name, the functions below won't like it. */
1550 return;
1552 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1553 &loc, &subpgm->frame))
1555 struct attribute ext;
1557 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1558 name.u.string, loc.kind, loc.offset, loc.reg,
1559 dwarf2_debug_ctx(subpgm->ctx));
1561 switch (loc.kind)
1563 case loc_error:
1564 break;
1565 case loc_absolute:
1566 /* it's a global variable */
1567 /* FIXME: we don't handle its scope yet */
1568 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1569 ext.u.uvalue = 0;
1570 loc.offset += subpgm->ctx->load_offset;
1571 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1572 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1573 loc, 0, param_type);
1574 break;
1575 default:
1576 subpgm->non_computed_variable = TRUE;
1577 /* fall through */
1578 case loc_register:
1579 case loc_regrel:
1580 /* either a pmt/variable relative to frame pointer or
1581 * pmt/variable in a register
1583 assert(subpgm->func);
1584 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1585 is_pmt ? DataIsParam : DataIsLocal,
1586 &loc, block, param_type, name.u.string);
1587 break;
1590 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1592 VARIANT v;
1593 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1594 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1595 switch (value.form)
1597 case DW_FORM_data1:
1598 case DW_FORM_data2:
1599 case DW_FORM_data4:
1600 case DW_FORM_udata:
1601 case DW_FORM_addr:
1602 v.n1.n2.vt = VT_UI4;
1603 v.n1.n2.n3.lVal = value.u.uvalue;
1604 break;
1606 case DW_FORM_data8:
1607 v.n1.n2.vt = VT_UI8;
1608 v.n1.n2.n3.llVal = value.u.lluvalue;
1609 break;
1611 case DW_FORM_sdata:
1612 v.n1.n2.vt = VT_I4;
1613 v.n1.n2.n3.lVal = value.u.svalue;
1614 break;
1616 case DW_FORM_strp:
1617 case DW_FORM_string:
1618 /* FIXME: native doesn't report const strings from here !!
1619 * however, the value of the string is in the code somewhere
1621 v.n1.n2.vt = VT_I1 | VT_BYREF;
1622 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1623 break;
1625 case DW_FORM_block:
1626 case DW_FORM_block1:
1627 case DW_FORM_block2:
1628 case DW_FORM_block4:
1629 v.n1.n2.vt = VT_I4;
1630 switch (value.u.block.size)
1632 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1633 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1634 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1635 default:
1636 v.n1.n2.vt = VT_I1 | VT_BYREF;
1637 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1638 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1640 break;
1642 default:
1643 FIXME("Unsupported form for const value %s (%lx)\n",
1644 name.u.string, value.form);
1645 v.n1.n2.vt = VT_EMPTY;
1647 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1648 name.u.string, param_type, &v)->symt;
1650 else
1652 /* variable has been optimiezd away... report anyway */
1653 loc.kind = loc_error;
1654 loc.reg = loc_err_no_location;
1655 if (subpgm->func)
1657 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1658 is_pmt ? DataIsParam : DataIsLocal,
1659 &loc, block, param_type, name.u.string);
1661 else
1663 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1666 if (is_pmt && subpgm->func && subpgm->func->type)
1667 symt_add_function_signature_parameter(subpgm->ctx->module,
1668 (struct symt_function_signature*)subpgm->func->type,
1669 param_type);
1671 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1674 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1675 const dwarf2_debug_info_t* di)
1677 struct attribute name;
1678 struct attribute low_pc;
1679 struct location loc;
1681 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1683 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1684 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1685 name.u.string = NULL;
1687 loc.kind = loc_absolute;
1688 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1689 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1690 &loc, name.u.string);
1693 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1694 struct symt_block* parent_block,
1695 dwarf2_debug_info_t* di);
1697 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1698 struct symt_block* parent_block,
1699 dwarf2_debug_info_t* di)
1701 struct symt_block* block;
1702 unsigned long low_pc, high_pc;
1703 struct vector* children;
1704 dwarf2_debug_info_t*child;
1705 unsigned int i;
1707 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1709 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1711 FIXME("cannot read range\n");
1712 return;
1715 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1716 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1717 high_pc - low_pc);
1719 children = dwarf2_get_di_children(subpgm->ctx, di);
1720 if (children) for (i = 0; i < vector_length(children); i++)
1722 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1724 switch (child->abbrev->tag)
1726 case DW_TAG_formal_parameter:
1727 case DW_TAG_variable:
1728 dwarf2_parse_variable(subpgm, block, child);
1729 break;
1730 case DW_TAG_lexical_block:
1731 dwarf2_parse_subprogram_block(subpgm, block, child);
1732 break;
1733 case DW_TAG_inlined_subroutine:
1734 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1735 break;
1736 case DW_TAG_label:
1737 dwarf2_parse_subprogram_label(subpgm, child);
1738 break;
1739 default:
1740 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1741 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1742 dwarf2_debug_di(di));
1745 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1748 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1749 struct symt_block* parent_block,
1750 dwarf2_debug_info_t* di)
1752 struct symt_block* block;
1753 unsigned long low_pc, high_pc;
1754 struct vector* children;
1755 dwarf2_debug_info_t*child;
1756 unsigned int i;
1758 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1760 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1762 FIXME("no range\n");
1763 return;
1766 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1767 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1768 high_pc - low_pc);
1770 children = dwarf2_get_di_children(subpgm->ctx, di);
1771 if (children) for (i = 0; i < vector_length(children); i++)
1773 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1775 switch (child->abbrev->tag)
1777 case DW_TAG_inlined_subroutine:
1778 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1779 break;
1780 case DW_TAG_variable:
1781 dwarf2_parse_variable(subpgm, block, child);
1782 break;
1783 case DW_TAG_lexical_block:
1784 dwarf2_parse_subprogram_block(subpgm, block, child);
1785 break;
1786 case DW_TAG_subprogram:
1787 /* FIXME: likely a declaration (to be checked)
1788 * skip it for now
1790 break;
1791 case DW_TAG_formal_parameter:
1792 /* FIXME: likely elements for exception handling (GCC flavor)
1793 * Skip it for now
1795 break;
1796 case DW_TAG_imported_module:
1797 /* C++ stuff to be silenced (for now) */
1798 break;
1799 case DW_TAG_label:
1800 dwarf2_parse_subprogram_label(subpgm, child);
1801 break;
1802 case DW_TAG_class_type:
1803 case DW_TAG_structure_type:
1804 case DW_TAG_union_type:
1805 case DW_TAG_enumeration_type:
1806 case DW_TAG_typedef:
1807 /* the type referred to will be loaded when we need it, so skip it */
1808 break;
1809 default:
1810 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1811 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1815 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1818 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1819 dwarf2_debug_info_t* di)
1821 struct attribute name;
1822 unsigned long low_pc, high_pc;
1823 struct attribute is_decl;
1824 struct attribute inline_flags;
1825 struct symt* ret_type;
1826 struct symt_function_signature* sig_type;
1827 dwarf2_subprogram_t subpgm;
1828 struct vector* children;
1829 dwarf2_debug_info_t* child;
1830 unsigned int i;
1832 if (di->symt) return di->symt;
1834 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1836 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1838 WARN("No name for function... dropping function\n");
1839 return NULL;
1841 /* if it's an abstract representation of an inline function, there should be
1842 * a concrete object that we'll handle
1844 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1845 inline_flags.u.uvalue != DW_INL_not_inlined)
1847 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1848 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1849 return NULL;
1852 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1853 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1855 /* it's a real declaration, skip it */
1856 return NULL;
1858 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1860 WARN("cannot get range for %s\n", name.u.string);
1861 return NULL;
1863 /* As functions (defined as inline assembly) get debug info with dwarf
1864 * (not the case for stabs), we just drop Wine's thunks here...
1865 * Actual thunks will be created in elf_module from the symbol table
1867 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1868 return NULL;
1869 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1871 ret_type = ctx->symt_cache[sc_void];
1872 assert(ret_type);
1874 /* FIXME: assuming C source code */
1875 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1876 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1877 dwarf2_get_cpp_name(ctx, di, name.u.string),
1878 ctx->load_offset + low_pc, high_pc - low_pc,
1879 &sig_type->symt);
1880 di->symt = &subpgm.func->symt;
1881 subpgm.ctx = ctx;
1882 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1883 &subpgm.frame, NULL))
1885 /* on stack !! */
1886 subpgm.frame.kind = loc_regrel;
1887 subpgm.frame.reg = 0;
1888 subpgm.frame.offset = 0;
1890 subpgm.non_computed_variable = FALSE;
1892 children = dwarf2_get_di_children(ctx, di);
1893 if (children) for (i = 0; i < vector_length(children); i++)
1895 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1897 switch (child->abbrev->tag)
1899 case DW_TAG_variable:
1900 case DW_TAG_formal_parameter:
1901 dwarf2_parse_variable(&subpgm, NULL, child);
1902 break;
1903 case DW_TAG_lexical_block:
1904 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1905 break;
1906 case DW_TAG_inlined_subroutine:
1907 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1908 break;
1909 case DW_TAG_subprogram:
1910 /* FIXME: likely a declaration (to be checked)
1911 * skip it for now
1913 break;
1914 case DW_TAG_label:
1915 dwarf2_parse_subprogram_label(&subpgm, child);
1916 break;
1917 case DW_TAG_class_type:
1918 case DW_TAG_structure_type:
1919 case DW_TAG_union_type:
1920 case DW_TAG_enumeration_type:
1921 case DW_TAG_typedef:
1922 /* the type referred to will be loaded when we need it, so skip it */
1923 break;
1924 case DW_TAG_unspecified_parameters:
1925 case DW_TAG_template_type_param:
1926 case DW_TAG_template_value_param:
1927 /* FIXME: no support in dbghelp's internals so far */
1928 break;
1929 default:
1930 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1931 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1935 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1937 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1938 &subpgm.frame, NULL);
1940 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1942 return di->symt;
1945 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1946 dwarf2_debug_info_t* di)
1948 struct symt* ret_type;
1949 struct symt_function_signature* sig_type;
1950 struct vector* children;
1951 dwarf2_debug_info_t* child;
1952 unsigned int i;
1954 if (di->symt) return di->symt;
1956 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1958 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1960 ret_type = ctx->symt_cache[sc_void];
1961 assert(ret_type);
1964 /* FIXME: assuming C source code */
1965 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1967 children = dwarf2_get_di_children(ctx, di);
1968 if (children) for (i = 0; i < vector_length(children); i++)
1970 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1972 switch (child->abbrev->tag)
1974 case DW_TAG_formal_parameter:
1975 symt_add_function_signature_parameter(ctx->module, sig_type,
1976 dwarf2_lookup_type(ctx, child));
1977 break;
1978 case DW_TAG_unspecified_parameters:
1979 WARN("Unsupported unspecified parameters\n");
1980 break;
1984 return di->symt = &sig_type->symt;
1987 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
1988 dwarf2_debug_info_t* di)
1990 struct vector* children;
1991 dwarf2_debug_info_t* child;
1992 unsigned int i;
1994 if (di->symt) return;
1996 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1998 di->symt = ctx->symt_cache[sc_void];
2000 children = dwarf2_get_di_children(ctx, di);
2001 if (children) for (i = 0; i < vector_length(children); i++)
2003 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2004 dwarf2_load_one_entry(ctx, child);
2008 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2009 dwarf2_debug_info_t* di)
2011 switch (di->abbrev->tag)
2013 case DW_TAG_typedef:
2014 dwarf2_parse_typedef(ctx, di);
2015 break;
2016 case DW_TAG_base_type:
2017 dwarf2_parse_base_type(ctx, di);
2018 break;
2019 case DW_TAG_pointer_type:
2020 dwarf2_parse_pointer_type(ctx, di);
2021 break;
2022 case DW_TAG_class_type:
2023 dwarf2_parse_udt_type(ctx, di, UdtClass);
2024 break;
2025 case DW_TAG_structure_type:
2026 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2027 break;
2028 case DW_TAG_union_type:
2029 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2030 break;
2031 case DW_TAG_array_type:
2032 dwarf2_parse_array_type(ctx, di);
2033 break;
2034 case DW_TAG_const_type:
2035 dwarf2_parse_const_type(ctx, di);
2036 break;
2037 case DW_TAG_volatile_type:
2038 dwarf2_parse_volatile_type(ctx, di);
2039 break;
2040 case DW_TAG_reference_type:
2041 dwarf2_parse_reference_type(ctx, di);
2042 break;
2043 case DW_TAG_enumeration_type:
2044 dwarf2_parse_enumeration_type(ctx, di);
2045 break;
2046 case DW_TAG_subprogram:
2047 dwarf2_parse_subprogram(ctx, di);
2048 break;
2049 case DW_TAG_subroutine_type:
2050 dwarf2_parse_subroutine_type(ctx, di);
2051 break;
2052 case DW_TAG_variable:
2054 dwarf2_subprogram_t subpgm;
2056 subpgm.ctx = ctx;
2057 subpgm.func = NULL;
2058 subpgm.frame.kind = loc_absolute;
2059 subpgm.frame.offset = 0;
2060 subpgm.frame.reg = Wine_DW_no_register;
2061 dwarf2_parse_variable(&subpgm, NULL, di);
2063 break;
2064 case DW_TAG_namespace:
2065 dwarf2_parse_namespace(ctx, di);
2066 break;
2067 /* silence a couple of C++ defines */
2068 case DW_TAG_imported_module:
2069 case DW_TAG_imported_declaration:
2070 case DW_TAG_ptr_to_member_type:
2071 break;
2072 default:
2073 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2074 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2078 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2079 const struct vector* v, unsigned file, unsigned line)
2081 struct symt_function* func;
2082 struct symt_ht* symt;
2083 unsigned* psrc;
2085 if (!file || !(psrc = vector_at(v, file - 1))) return;
2087 TRACE("%s %lx %s %u\n",
2088 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2089 if (!(symt = symt_find_nearest(module, address)) ||
2090 symt->symt.tag != SymTagFunction) return;
2091 func = (struct symt_function*)symt;
2092 symt_add_func_line(module, func, *psrc, line, address - func->address);
2095 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2096 dwarf2_parse_context_t* ctx,
2097 const char* compile_dir,
2098 unsigned long offset)
2100 dwarf2_traverse_context_t traverse;
2101 unsigned long length;
2102 unsigned version, header_len, insn_size, default_stmt;
2103 unsigned line_range, opcode_base;
2104 int line_base;
2105 const unsigned char* opcode_len;
2106 struct vector dirs;
2107 struct vector files;
2108 const char** p;
2110 /* section with line numbers stripped */
2111 if (sections[section_line].address == IMAGE_NO_MAP)
2112 return FALSE;
2114 traverse.data = sections[section_line].address + offset;
2115 traverse.end_data = traverse.data + 4;
2116 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2118 length = dwarf2_parse_u4(&traverse);
2119 traverse.end_data = sections[section_line].address + offset + length;
2121 version = dwarf2_parse_u2(&traverse);
2122 header_len = dwarf2_parse_u4(&traverse);
2123 insn_size = dwarf2_parse_byte(&traverse);
2124 default_stmt = dwarf2_parse_byte(&traverse);
2125 line_base = (signed char)dwarf2_parse_byte(&traverse);
2126 line_range = dwarf2_parse_byte(&traverse);
2127 opcode_base = dwarf2_parse_byte(&traverse);
2129 opcode_len = traverse.data;
2130 traverse.data += opcode_base - 1;
2132 vector_init(&dirs, sizeof(const char*), 4);
2133 p = vector_add(&dirs, &ctx->pool);
2134 *p = compile_dir ? compile_dir : ".";
2135 while (*traverse.data)
2137 const char* rel = (const char*)traverse.data;
2138 unsigned rellen = strlen(rel);
2139 TRACE("Got include %s\n", rel);
2140 traverse.data += rellen + 1;
2141 p = vector_add(&dirs, &ctx->pool);
2143 if (*rel == '/' || !compile_dir)
2144 *p = rel;
2145 else
2147 /* include directory relative to compile directory */
2148 unsigned baselen = strlen(compile_dir);
2149 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2150 strcpy(tmp, compile_dir);
2151 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2152 strcpy(&tmp[baselen], rel);
2153 *p = tmp;
2157 traverse.data++;
2159 vector_init(&files, sizeof(unsigned), 16);
2160 while (*traverse.data)
2162 unsigned int dir_index, mod_time, length;
2163 const char* name;
2164 const char* dir;
2165 unsigned* psrc;
2167 name = (const char*)traverse.data;
2168 traverse.data += strlen(name) + 1;
2169 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2170 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2171 length = dwarf2_leb128_as_unsigned(&traverse);
2172 dir = *(const char**)vector_at(&dirs, dir_index);
2173 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
2174 psrc = vector_add(&files, &ctx->pool);
2175 *psrc = source_new(ctx->module, dir, name);
2177 traverse.data++;
2179 while (traverse.data < traverse.end_data)
2181 unsigned long address = 0;
2182 unsigned file = 1;
2183 unsigned line = 1;
2184 unsigned is_stmt = default_stmt;
2185 BOOL end_sequence = FALSE;
2186 unsigned opcode, extopcode, i;
2188 while (!end_sequence)
2190 opcode = dwarf2_parse_byte(&traverse);
2191 TRACE("Got opcode %x\n", opcode);
2193 if (opcode >= opcode_base)
2195 unsigned delta = opcode - opcode_base;
2197 address += (delta / line_range) * insn_size;
2198 line += line_base + (delta % line_range);
2199 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2201 else
2203 switch (opcode)
2205 case DW_LNS_copy:
2206 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2207 break;
2208 case DW_LNS_advance_pc:
2209 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2210 break;
2211 case DW_LNS_advance_line:
2212 line += dwarf2_leb128_as_signed(&traverse);
2213 break;
2214 case DW_LNS_set_file:
2215 file = dwarf2_leb128_as_unsigned(&traverse);
2216 break;
2217 case DW_LNS_set_column:
2218 dwarf2_leb128_as_unsigned(&traverse);
2219 break;
2220 case DW_LNS_negate_stmt:
2221 is_stmt = !is_stmt;
2222 break;
2223 case DW_LNS_set_basic_block:
2224 break;
2225 case DW_LNS_const_add_pc:
2226 address += ((255 - opcode_base) / line_range) * insn_size;
2227 break;
2228 case DW_LNS_fixed_advance_pc:
2229 address += dwarf2_parse_u2(&traverse);
2230 break;
2231 case DW_LNS_extended_op:
2232 dwarf2_leb128_as_unsigned(&traverse);
2233 extopcode = dwarf2_parse_byte(&traverse);
2234 switch (extopcode)
2236 case DW_LNE_end_sequence:
2237 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2238 end_sequence = TRUE;
2239 break;
2240 case DW_LNE_set_address:
2241 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2242 break;
2243 case DW_LNE_define_file:
2244 FIXME("not handled %s\n", traverse.data);
2245 traverse.data += strlen((const char *)traverse.data) + 1;
2246 dwarf2_leb128_as_unsigned(&traverse);
2247 dwarf2_leb128_as_unsigned(&traverse);
2248 dwarf2_leb128_as_unsigned(&traverse);
2249 break;
2250 case DW_LNE_set_discriminator:
2251 WARN("not handled %s\n", traverse.data);
2252 dwarf2_leb128_as_unsigned(&traverse);
2253 break;
2254 default:
2255 FIXME("Unsupported extended opcode %x\n", extopcode);
2256 break;
2258 break;
2259 default:
2260 WARN("Unsupported opcode %x\n", opcode);
2261 for (i = 0; i < opcode_len[opcode]; i++)
2262 dwarf2_leb128_as_unsigned(&traverse);
2263 break;
2268 return TRUE;
2271 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2272 struct module* module,
2273 const struct elf_thunk_area* thunks,
2274 dwarf2_traverse_context_t* mod_ctx,
2275 unsigned long load_offset)
2277 dwarf2_parse_context_t ctx;
2278 dwarf2_traverse_context_t abbrev_ctx;
2279 dwarf2_debug_info_t* di;
2280 dwarf2_traverse_context_t cu_ctx;
2281 const unsigned char* comp_unit_start = mod_ctx->data;
2282 unsigned long cu_length;
2283 unsigned short cu_version;
2284 unsigned long cu_abbrev_offset;
2285 BOOL ret = FALSE;
2287 cu_length = dwarf2_parse_u4(mod_ctx);
2288 cu_ctx.data = mod_ctx->data;
2289 cu_ctx.end_data = mod_ctx->data + cu_length;
2290 mod_ctx->data += cu_length;
2291 cu_version = dwarf2_parse_u2(&cu_ctx);
2292 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2293 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2295 TRACE("Compilation Unit Header found at 0x%x:\n",
2296 (int)(comp_unit_start - sections[section_debug].address));
2297 TRACE("- length: %lu\n", cu_length);
2298 TRACE("- version: %u\n", cu_version);
2299 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2300 TRACE("- word_size: %u\n", cu_ctx.word_size);
2302 if (cu_version != 2)
2304 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2305 cu_version);
2306 return FALSE;
2309 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2310 mod_ctx->word_size = cu_ctx.word_size;
2312 pool_init(&ctx.pool, 65536);
2313 ctx.sections = sections;
2314 ctx.section = section_debug;
2315 ctx.module = module;
2316 ctx.thunks = thunks;
2317 ctx.load_offset = load_offset;
2318 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2319 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2320 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2321 ctx.cpp_name = NULL;
2323 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2324 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2325 abbrev_ctx.word_size = cu_ctx.word_size;
2326 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2328 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2329 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2331 if (di->abbrev->tag == DW_TAG_compile_unit)
2333 struct attribute name;
2334 struct vector* children;
2335 dwarf2_debug_info_t* child = NULL;
2336 unsigned int i;
2337 struct attribute stmt_list, low_pc;
2338 struct attribute comp_dir;
2340 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2341 name.u.string = NULL;
2343 /* get working directory of current compilation unit */
2344 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2345 comp_dir.u.string = NULL;
2347 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2348 low_pc.u.uvalue = 0;
2349 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2350 source_new(module, comp_dir.u.string, name.u.string));
2351 di->symt = &ctx.compiland->symt;
2352 children = dwarf2_get_di_children(&ctx, di);
2353 if (children) for (i = 0; i < vector_length(children); i++)
2355 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2356 dwarf2_load_one_entry(&ctx, child);
2358 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2360 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2361 module->module.LineNumbers = TRUE;
2363 ret = TRUE;
2365 else FIXME("Should have a compilation unit here\n");
2366 pool_destroy(&ctx.pool);
2367 return ret;
2370 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2371 unsigned long ip, dwarf2_traverse_context_t* lctx)
2373 DWORD_PTR beg, end;
2374 const BYTE* ptr = start;
2375 DWORD len;
2377 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2379 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2380 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2381 if (!beg && !end) break;
2382 len = dwarf2_get_u2(ptr); ptr += 2;
2384 if (beg <= ip && ip < end)
2386 lctx->data = ptr;
2387 lctx->end_data = ptr + len;
2388 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2389 return TRUE;
2391 ptr += len;
2393 WARN("Couldn't find ip in location list\n");
2394 return FALSE;
2397 static enum location_error loc_compute_frame(struct process* pcs,
2398 const struct module_format* modfmt,
2399 const struct symt_function* func,
2400 DWORD_PTR ip, struct location* frame)
2402 struct symt** psym = NULL;
2403 struct location* pframe;
2404 dwarf2_traverse_context_t lctx;
2405 enum location_error err;
2406 unsigned int i;
2408 for (i=0; i<vector_length(&func->vchildren); i++)
2410 psym = vector_at(&func->vchildren, i);
2411 if ((*psym)->tag == SymTagCustom)
2413 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2415 /* First, recompute the frame information, if needed */
2416 switch (pframe->kind)
2418 case loc_regrel:
2419 case loc_register:
2420 *frame = *pframe;
2421 break;
2422 case loc_dwarf2_location_list:
2423 WARN("Searching loclist for %s\n", func->hash_elt.name);
2424 if (!dwarf2_lookup_loclist(modfmt,
2425 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2426 ip, &lctx))
2427 return loc_err_out_of_scope;
2428 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2429 if (frame->kind >= loc_user)
2431 WARN("Couldn't compute runtime frame location\n");
2432 return loc_err_too_complex;
2434 break;
2435 default:
2436 WARN("Unsupported frame kind %d\n", pframe->kind);
2437 return loc_err_internal;
2439 return 0;
2442 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2443 return loc_err_internal;
2446 enum reg_rule
2448 RULE_UNSET, /* not set at all */
2449 RULE_UNDEFINED, /* undefined value */
2450 RULE_SAME, /* same value as previous frame */
2451 RULE_CFA_OFFSET, /* stored at cfa offset */
2452 RULE_OTHER_REG, /* stored in other register */
2453 RULE_EXPRESSION, /* address specified by expression */
2454 RULE_VAL_EXPRESSION /* value specified by expression */
2457 /* make it large enough for all CPUs */
2458 #define NB_FRAME_REGS 64
2459 #define MAX_SAVED_STATES 16
2461 struct frame_state
2463 ULONG_PTR cfa_offset;
2464 unsigned char cfa_reg;
2465 enum reg_rule cfa_rule;
2466 enum reg_rule rules[NB_FRAME_REGS];
2467 ULONG_PTR regs[NB_FRAME_REGS];
2470 struct frame_info
2472 ULONG_PTR ip;
2473 ULONG_PTR code_align;
2474 LONG_PTR data_align;
2475 unsigned char retaddr_reg;
2476 unsigned char fde_encoding;
2477 unsigned char lsda_encoding;
2478 unsigned char signal_frame;
2479 unsigned char aug_z_format;
2480 unsigned char state_sp;
2481 struct frame_state state;
2482 struct frame_state state_stack[MAX_SAVED_STATES];
2485 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2487 ULONG_PTR base;
2489 if (encoding == DW_EH_PE_omit) return 0;
2491 switch (encoding & 0xf0)
2493 case DW_EH_PE_abs:
2494 base = 0;
2495 break;
2496 case DW_EH_PE_pcrel:
2497 base = (ULONG_PTR)ctx->data;
2498 break;
2499 default:
2500 FIXME("unsupported encoding %02x\n", encoding);
2501 return 0;
2504 switch (encoding & 0x0f)
2506 case DW_EH_PE_native:
2507 return base + dwarf2_parse_addr(ctx);
2508 case DW_EH_PE_leb128:
2509 return base + dwarf2_leb128_as_unsigned(ctx);
2510 case DW_EH_PE_data2:
2511 return base + dwarf2_parse_u2(ctx);
2512 case DW_EH_PE_data4:
2513 return base + dwarf2_parse_u4(ctx);
2514 case DW_EH_PE_data8:
2515 return base + dwarf2_parse_u8(ctx);
2516 case DW_EH_PE_signed|DW_EH_PE_leb128:
2517 return base + dwarf2_leb128_as_signed(ctx);
2518 case DW_EH_PE_signed|DW_EH_PE_data2:
2519 return base + (signed short)dwarf2_parse_u2(ctx);
2520 case DW_EH_PE_signed|DW_EH_PE_data4:
2521 return base + (signed int)dwarf2_parse_u4(ctx);
2522 case DW_EH_PE_signed|DW_EH_PE_data8:
2523 return base + (LONG64)dwarf2_parse_u8(ctx);
2524 default:
2525 FIXME("unsupported encoding %02x\n", encoding);
2526 return 0;
2530 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2532 unsigned char version;
2533 const char* augmentation;
2534 const unsigned char* end;
2535 ULONG_PTR len;
2537 memset(info, 0, sizeof(*info));
2538 info->lsda_encoding = DW_EH_PE_omit;
2539 info->aug_z_format = 0;
2541 /* parse the CIE first */
2542 version = dwarf2_parse_byte(ctx);
2543 if (version != 1)
2545 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2546 return FALSE;
2548 augmentation = (const char*)ctx->data;
2549 ctx->data += strlen(augmentation) + 1;
2551 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2552 info->data_align = dwarf2_leb128_as_signed(ctx);
2553 info->retaddr_reg = dwarf2_parse_byte(ctx);
2554 info->state.cfa_rule = RULE_CFA_OFFSET;
2556 end = NULL;
2557 TRACE("\tparsing augmentation %s\n", augmentation);
2558 if (*augmentation) do
2560 switch (*augmentation)
2562 case 'z':
2563 len = dwarf2_leb128_as_unsigned(ctx);
2564 end = ctx->data + len;
2565 info->aug_z_format = 1;
2566 continue;
2567 case 'L':
2568 info->lsda_encoding = dwarf2_parse_byte(ctx);
2569 continue;
2570 case 'P':
2572 unsigned char encoding = dwarf2_parse_byte(ctx);
2573 /* throw away the indirect bit, as we don't care for the result */
2574 encoding &= ~DW_EH_PE_indirect;
2575 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2576 continue;
2578 case 'R':
2579 info->fde_encoding = dwarf2_parse_byte(ctx);
2580 continue;
2581 case 'S':
2582 info->signal_frame = 1;
2583 continue;
2585 FIXME("unknown augmentation '%c'\n", *augmentation);
2586 if (!end) return FALSE;
2587 break;
2588 } while (*++augmentation);
2589 if (end) ctx->data = end;
2590 return TRUE;
2593 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2594 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2595 struct frame_info* info, BOOL in_eh_frame)
2597 const unsigned char* ptr_blk;
2598 const unsigned char* cie_ptr;
2599 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2600 unsigned len, id;
2601 unsigned long start, range;
2602 unsigned cie_id;
2603 const BYTE* start_data = fde_ctx->data;
2605 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2606 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2608 /* find the FDE for address addr (skip CIE) */
2609 len = dwarf2_parse_u4(fde_ctx);
2610 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2611 ptr_blk = fde_ctx->data + len;
2612 id = dwarf2_parse_u4(fde_ctx);
2613 if (id == cie_id)
2615 last_cie_ptr = fde_ctx->data - 8;
2616 /* we need some bits out of the CIE in order to parse all contents */
2617 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2618 cie_ctx->data = fde_ctx->data;
2619 cie_ctx->end_data = ptr_blk;
2620 cie_ctx->word_size = fde_ctx->word_size;
2621 continue;
2623 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2624 if (cie_ptr != last_cie_ptr)
2626 last_cie_ptr = cie_ptr;
2627 cie_ctx->data = cie_ptr;
2628 cie_ctx->word_size = fde_ctx->word_size;
2629 cie_ctx->end_data = cie_ptr + 4;
2630 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2631 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2633 FIXME("wrong CIE pointer\n");
2634 return FALSE;
2636 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2638 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2639 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2641 if (addr >= start && addr < start + range)
2643 /* reset the FDE context */
2644 fde_ctx->end_data = ptr_blk;
2646 info->ip = start;
2647 return TRUE;
2650 return FALSE;
2653 static int valid_reg(ULONG_PTR reg)
2655 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2656 return (reg < NB_FRAME_REGS);
2659 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2660 ULONG_PTR last_ip, struct frame_info *info)
2662 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2664 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2666 if (op & 0xc0)
2668 switch (op & 0xc0)
2670 case DW_CFA_advance_loc:
2672 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2673 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2674 info->ip += offset;
2675 break;
2677 case DW_CFA_offset:
2679 ULONG_PTR reg = op & 0x3f;
2680 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2681 if (!valid_reg(reg)) break;
2682 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2683 info->ip,
2684 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2685 offset);
2686 info->state.regs[reg] = offset;
2687 info->state.rules[reg] = RULE_CFA_OFFSET;
2688 break;
2690 case DW_CFA_restore:
2692 ULONG_PTR reg = op & 0x3f;
2693 if (!valid_reg(reg)) break;
2694 TRACE("%lx: DW_CFA_restore %s\n",
2695 info->ip,
2696 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2697 info->state.rules[reg] = RULE_UNSET;
2698 break;
2702 else switch (op)
2704 case DW_CFA_nop:
2705 break;
2706 case DW_CFA_set_loc:
2708 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2709 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2710 info->ip = loc;
2711 break;
2713 case DW_CFA_advance_loc1:
2715 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2716 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2717 info->ip += offset;
2718 break;
2720 case DW_CFA_advance_loc2:
2722 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2723 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2724 info->ip += offset;
2725 break;
2727 case DW_CFA_advance_loc4:
2729 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2730 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2731 info->ip += offset;
2732 break;
2734 case DW_CFA_offset_extended:
2735 case DW_CFA_offset_extended_sf:
2737 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2738 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2739 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2740 if (!valid_reg(reg)) break;
2741 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2742 info->ip,
2743 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2744 offset);
2745 info->state.regs[reg] = offset;
2746 info->state.rules[reg] = RULE_CFA_OFFSET;
2747 break;
2749 case DW_CFA_restore_extended:
2751 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2752 if (!valid_reg(reg)) break;
2753 TRACE("%lx: DW_CFA_restore_extended %s\n",
2754 info->ip,
2755 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2756 info->state.rules[reg] = RULE_UNSET;
2757 break;
2759 case DW_CFA_undefined:
2761 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2762 if (!valid_reg(reg)) break;
2763 TRACE("%lx: DW_CFA_undefined %s\n",
2764 info->ip,
2765 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2766 info->state.rules[reg] = RULE_UNDEFINED;
2767 break;
2769 case DW_CFA_same_value:
2771 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2772 if (!valid_reg(reg)) break;
2773 TRACE("%lx: DW_CFA_same_value %s\n",
2774 info->ip,
2775 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2776 info->state.regs[reg] = reg;
2777 info->state.rules[reg] = RULE_SAME;
2778 break;
2780 case DW_CFA_register:
2782 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2783 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2784 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2785 TRACE("%lx: DW_CFA_register %s == %s\n",
2786 info->ip,
2787 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2788 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2789 info->state.regs[reg] = reg2;
2790 info->state.rules[reg] = RULE_OTHER_REG;
2791 break;
2793 case DW_CFA_remember_state:
2794 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2795 if (info->state_sp >= MAX_SAVED_STATES)
2796 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2797 else
2798 info->state_stack[info->state_sp++] = info->state;
2799 break;
2800 case DW_CFA_restore_state:
2801 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2802 if (!info->state_sp)
2803 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2804 else
2805 info->state = info->state_stack[--info->state_sp];
2806 break;
2807 case DW_CFA_def_cfa:
2808 case DW_CFA_def_cfa_sf:
2810 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2811 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2812 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2813 if (!valid_reg(reg)) break;
2814 TRACE("%lx: DW_CFA_def_cfa %s, %lu\n",
2815 info->ip,
2816 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2817 offset);
2818 info->state.cfa_reg = reg;
2819 info->state.cfa_offset = offset;
2820 info->state.cfa_rule = RULE_CFA_OFFSET;
2821 break;
2823 case DW_CFA_def_cfa_register:
2825 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2826 if (!valid_reg(reg)) break;
2827 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2828 info->ip,
2829 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2830 info->state.cfa_reg = reg;
2831 info->state.cfa_rule = RULE_CFA_OFFSET;
2832 break;
2834 case DW_CFA_def_cfa_offset:
2835 case DW_CFA_def_cfa_offset_sf:
2837 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2838 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2839 TRACE("%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset);
2840 info->state.cfa_offset = offset;
2841 info->state.cfa_rule = RULE_CFA_OFFSET;
2842 break;
2844 case DW_CFA_def_cfa_expression:
2846 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2847 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2848 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2849 info->state.cfa_offset = expr;
2850 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2851 ctx->data += len;
2852 break;
2854 case DW_CFA_expression:
2855 case DW_CFA_val_expression:
2857 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2858 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2859 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2860 if (!valid_reg(reg)) break;
2861 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2862 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2863 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2864 expr, expr + len);
2865 info->state.regs[reg] = expr;
2866 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2867 ctx->data += len;
2868 break;
2870 case DW_CFA_GNU_args_size:
2871 /* FIXME: should check that GCC is the compiler for this CU */
2873 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2874 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2875 /* ignored */
2876 break;
2878 default:
2879 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2880 break;
2885 /* retrieve a context register from its dwarf number */
2886 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2888 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2889 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2891 if (sz != sizeof(ULONG_PTR))
2893 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2894 return 0;
2896 return *ptr;
2899 /* set a context register from its dwarf number */
2900 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2901 ULONG_PTR val, BOOL isdebuggee)
2903 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2904 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2906 if (isdebuggee)
2908 char tmp[16];
2910 if (sz > sizeof(tmp))
2912 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2913 return;
2915 if (!sw_read_mem(csw, val, tmp, sz))
2917 WARN("Couldn't read memory at %p\n", (void*)val);
2918 return;
2920 memcpy(ptr, tmp, sz);
2922 else
2924 if (sz != sizeof(ULONG_PTR))
2926 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2927 return;
2929 *ptr = val;
2933 /* copy a register from one context to another using dwarf number */
2934 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2936 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2937 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2938 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2939 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2941 if (szdst != szsrc)
2943 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2944 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2945 return;
2947 memcpy(ptrdst, ptrsrc, szdst);
2950 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
2951 const unsigned char* zp, CONTEXT *context)
2953 dwarf2_traverse_context_t ctx;
2954 ULONG_PTR reg, sz, tmp, stack[64];
2955 int sp = -1;
2956 ULONG_PTR len;
2958 ctx.data = zp;
2959 ctx.end_data = zp + 4;
2960 len = dwarf2_leb128_as_unsigned(&ctx);
2961 ctx.end_data = ctx.data + len;
2962 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2964 while (ctx.data < ctx.end_data)
2966 unsigned char opcode = dwarf2_parse_byte(&ctx);
2968 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
2969 stack[++sp] = opcode - DW_OP_lit0;
2970 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
2971 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
2972 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
2973 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
2974 else switch (opcode)
2976 case DW_OP_nop: break;
2977 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
2978 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
2979 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
2980 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
2981 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
2982 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
2983 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
2984 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
2985 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
2986 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
2987 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
2988 case DW_OP_deref:
2989 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
2991 ERR("Couldn't read memory at %lx\n", stack[sp]);
2992 tmp = 0;
2994 stack[sp] = tmp;
2995 break;
2996 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
2997 case DW_OP_drop: sp--; break;
2998 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
2999 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3000 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3001 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3002 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3003 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3004 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3005 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3006 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3007 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3008 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3009 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3010 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3011 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3012 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3013 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3014 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3015 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3016 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3017 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3018 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3019 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3020 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3021 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3022 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3023 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3024 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3025 case DW_OP_GNU_encoded_addr:
3026 tmp = dwarf2_parse_byte(&ctx);
3027 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3028 break;
3029 case DW_OP_regx:
3030 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
3031 break;
3032 case DW_OP_bregx:
3033 reg = dwarf2_leb128_as_unsigned(&ctx);
3034 tmp = dwarf2_leb128_as_signed(&ctx);
3035 stack[++sp] = get_context_reg(context, reg) + tmp;
3036 break;
3037 case DW_OP_deref_size:
3038 sz = dwarf2_parse_byte(&ctx);
3039 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3041 ERR("Couldn't read memory at %lx\n", stack[sp]);
3042 tmp = 0;
3044 /* do integral promotion */
3045 switch (sz)
3047 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3048 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3049 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3050 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3051 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3053 break;
3054 default:
3055 FIXME("unhandled opcode %02x\n", opcode);
3058 return stack[sp];
3061 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3062 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3064 unsigned int i;
3065 ULONG_PTR value;
3066 CONTEXT new_context = *context;
3068 switch (state->cfa_rule)
3070 case RULE_EXPRESSION:
3071 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3072 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
3074 WARN("Couldn't read memory at %p\n", (void*)*cfa);
3075 return;
3077 break;
3078 case RULE_VAL_EXPRESSION:
3079 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3080 break;
3081 default:
3082 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3083 break;
3085 if (!*cfa) return;
3087 for (i = 0; i < NB_FRAME_REGS; i++)
3089 switch (state->rules[i])
3091 case RULE_UNSET:
3092 case RULE_UNDEFINED:
3093 case RULE_SAME:
3094 break;
3095 case RULE_CFA_OFFSET:
3096 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3097 break;
3098 case RULE_OTHER_REG:
3099 copy_context_reg(&new_context, i, context, state->regs[i]);
3100 break;
3101 case RULE_EXPRESSION:
3102 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3103 set_context_reg(csw, &new_context, i, value, TRUE);
3104 break;
3105 case RULE_VAL_EXPRESSION:
3106 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3107 set_context_reg(csw, &new_context, i, value, FALSE);
3108 break;
3111 *context = new_context;
3114 /***********************************************************************
3115 * dwarf2_virtual_unwind
3118 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3120 struct module_pair pair;
3121 struct frame_info info;
3122 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3123 struct module_format* modfmt;
3124 const unsigned char* end;
3125 DWORD_PTR delta;
3127 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3128 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3129 !module_get_debug(&pair))
3130 return FALSE;
3131 modfmt = pair.effective->format_info[DFI_DWARF];
3132 if (!modfmt) return FALSE;
3133 memset(&info, 0, sizeof(info));
3134 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3135 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3136 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3137 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3138 * in this process the IMAGE section at a different address as the one expected by
3139 * the image
3141 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3142 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3143 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3145 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3146 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3147 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3148 delta = pair.effective->reloc_delta;
3149 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3151 TRACE("Couldn't find information for %lx\n", ip);
3152 return FALSE;
3156 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3157 ip, info.ip, info.code_align, info.data_align,
3158 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
3160 /* if at very beginning of function, return and use default unwinder */
3161 if (ip == info.ip) return FALSE;
3162 execute_cfa_instructions(&cie_ctx, ip, &info);
3164 if (info.aug_z_format) /* get length of augmentation data */
3166 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3167 end = fde_ctx.data + len;
3169 else end = NULL;
3170 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3171 if (end) fde_ctx.data = end;
3173 execute_cfa_instructions(&fde_ctx, ip, &info);
3174 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3176 return TRUE;
3179 static void dwarf2_location_compute(struct process* pcs,
3180 const struct module_format* modfmt,
3181 const struct symt_function* func,
3182 struct location* loc)
3184 struct location frame;
3185 DWORD_PTR ip;
3186 int err;
3187 dwarf2_traverse_context_t lctx;
3189 if (!func->container || func->container->tag != SymTagCompiland)
3191 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3192 err = loc_err_internal;
3194 else
3196 /* instruction pointer relative to compiland's start */
3197 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3199 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3201 switch (loc->kind)
3203 case loc_dwarf2_location_list:
3204 /* Then, if the variable has a location list, find it !! */
3205 if (dwarf2_lookup_loclist(modfmt,
3206 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3207 ip, &lctx))
3208 goto do_compute;
3209 err = loc_err_out_of_scope;
3210 break;
3211 case loc_dwarf2_block:
3212 /* or if we have a copy of an existing block, get ready for it */
3214 unsigned* ptr = (unsigned*)loc->offset;
3216 lctx.data = (const BYTE*)(ptr + 1);
3217 lctx.end_data = lctx.data + *ptr;
3218 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3220 do_compute:
3221 /* now get the variable */
3222 err = compute_location(&lctx, loc, pcs->handle, &frame);
3223 break;
3224 case loc_register:
3225 case loc_regrel:
3226 /* nothing to do */
3227 break;
3228 default:
3229 WARN("Unsupported local kind %d\n", loc->kind);
3230 err = loc_err_internal;
3234 if (err < 0)
3236 loc->kind = loc_register;
3237 loc->reg = err;
3241 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3243 HeapFree(GetProcessHeap(), 0, modfmt);
3246 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3247 const char* sectname, struct image_section_map* ism)
3249 struct image_section_map local_ism;
3251 if (!ism) ism = &local_ism;
3252 if (!image_find_section(fmap, sectname, ism))
3254 section->address = NULL;
3255 section->size = 0;
3256 section->rva = 0;
3257 return FALSE;
3260 section->address = (const BYTE*)image_map_section(ism);
3261 section->size = image_get_map_size(ism);
3262 section->rva = image_get_map_rva(ism);
3263 return TRUE;
3266 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3267 const struct elf_thunk_area* thunks,
3268 struct image_file_map* fmap)
3270 dwarf2_section_t eh_frame, section[section_max];
3271 dwarf2_traverse_context_t mod_ctx;
3272 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3273 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3274 BOOL ret = TRUE;
3275 struct module_format* dwarf2_modfmt;
3277 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", &eh_frame_sect);
3278 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", &debug_sect);
3279 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3280 dwarf2_init_section(&section[section_string], fmap, ".debug_str", &debug_str_sect);
3281 dwarf2_init_section(&section[section_line], fmap, ".debug_line", &debug_line_sect);
3282 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", &debug_ranges_sect);
3284 /* to do anything useful we need either .eh_frame or .debug_info */
3285 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3286 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3288 ret = FALSE;
3289 goto leave;
3292 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3294 /* debug info might have a different base address than .so file
3295 * when elf file is prelinked after splitting off debug info
3296 * adjust symbol base addresses accordingly
3298 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3301 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3303 mod_ctx.data = section[section_debug].address;
3304 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3305 mod_ctx.word_size = 0; /* will be correctly set later on */
3307 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3308 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3309 if (!dwarf2_modfmt)
3311 ret = FALSE;
3312 goto leave;
3314 dwarf2_modfmt->module = module;
3315 dwarf2_modfmt->remove = dwarf2_module_remove;
3316 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3317 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3318 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3319 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3321 /* As we'll need later some sections' content, we won't unmap these
3322 * sections upon existing this function
3324 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", NULL);
3325 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3326 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3328 while (mod_ctx.data < mod_ctx.end_data)
3330 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3332 dwarf2_modfmt->module->module.SymType = SymDia;
3333 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3334 /* FIXME: we could have a finer grain here */
3335 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3336 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3337 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3338 dwarf2_modfmt->module->module.Publics = TRUE;
3340 leave:
3341 image_unmap_section(&debug_sect);
3342 image_unmap_section(&debug_abbrev_sect);
3343 image_unmap_section(&debug_str_sect);
3344 image_unmap_section(&debug_line_sect);
3345 image_unmap_section(&debug_ranges_sect);
3346 if (!ret) image_unmap_section(&eh_frame_sect);
3348 return ret;