dbghelp/dwarf: Add current block as a field in dwarf2_subprogram_t.
[wine.git] / dlls / dbghelp / dwarf.c
blobef86e6bffe0ab1982b0efbec923b7675b29e5034
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2011, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
25 #include <sys/types.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <stdarg.h>
31 #include <zlib.h>
33 #include "windef.h"
34 #include "winternl.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "ole2.h"
38 #include "oleauto.h"
40 #include "dbghelp_private.h"
41 #include "image_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
47 /* FIXME:
48 * - Functions:
49 * o unspecified parameters
50 * o inlined functions
51 * o Debug{Start|End}Point
52 * o CFA
53 * - Udt
54 * o proper types loading (nesting)
57 #if 0
58 static void dump(const void* ptr, unsigned len)
60 int i, j;
61 BYTE msg[128];
62 static const char hexof[] = "0123456789abcdef";
63 const BYTE* x = ptr;
65 for (i = 0; i < len; i += 16)
67 sprintf(msg, "%08x: ", i);
68 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
69 for (j = 0; j < min(16, len - i); j++)
71 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
72 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
73 msg[10 + 3 * j + 2] = ' ';
74 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
75 x[i + j] : '.';
77 msg[10 + 3 * 16] = ' ';
78 msg[10 + 3 * 16 + 1 + 16] = '\0';
79 TRACE("%s\n", msg);
82 #endif
84 /**
86 * Main Specs:
87 * http://www.eagercon.com/dwarf/dwarf3std.htm
88 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
90 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
92 * example of projects who do dwarf2 parsing:
93 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
94 * http://elis.ugent.be/diota/log/ltrace_elf.c
96 #include "dwarf.h"
98 /**
99 * Parsers
102 typedef struct dwarf2_abbrev_entry_attr_s
104 ULONG_PTR attribute;
105 ULONG_PTR form;
106 struct dwarf2_abbrev_entry_attr_s* next;
107 } dwarf2_abbrev_entry_attr_t;
109 typedef struct dwarf2_abbrev_entry_s
111 ULONG_PTR entry_code;
112 ULONG_PTR tag;
113 unsigned char have_child;
114 unsigned num_attr;
115 dwarf2_abbrev_entry_attr_t* attrs;
116 } dwarf2_abbrev_entry_t;
118 struct dwarf2_block
120 unsigned size;
121 const unsigned char* ptr;
124 struct attribute
126 ULONG_PTR form;
127 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
128 union
130 ULONG_PTR uvalue;
131 ULONGLONG lluvalue;
132 LONG_PTR svalue;
133 const char* string;
134 struct dwarf2_block block;
135 } u;
136 const struct dwarf2_debug_info_s* debug_info;
139 typedef struct dwarf2_debug_info_s
141 const dwarf2_abbrev_entry_t*abbrev;
142 struct symt* symt;
143 const unsigned char** data;
144 struct vector children;
145 struct dwarf2_debug_info_s* parent;
146 struct dwarf2_parse_context_s* unit_ctx;
147 } dwarf2_debug_info_t;
149 typedef struct dwarf2_section_s
151 BOOL compressed;
152 const unsigned char* address;
153 unsigned size;
154 DWORD_PTR rva;
155 } dwarf2_section_t;
157 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
159 typedef struct dwarf2_traverse_context_s
161 const unsigned char* data;
162 const unsigned char* end_data;
163 } dwarf2_traverse_context_t;
165 /* symt_cache indexes */
166 #define sc_void 0
167 #define sc_unknown 1
168 #define sc_num 2
170 typedef struct dwarf2_cuhead_s
172 unsigned char word_size; /* size of a word on target machine */
173 unsigned char version;
174 unsigned char offset_size; /* size of offset inside DWARF */
175 } dwarf2_cuhead_t;
177 typedef struct dwarf2_parse_module_context_s
179 ULONG_PTR load_offset;
180 const dwarf2_section_t* sections;
181 struct module* module;
182 const struct elf_thunk_area*thunks;
183 struct symt* symt_cache[sc_num]; /* void, unknown */
184 struct vector unit_contexts;
185 struct dwarf2_dwz_alternate_s* dwz;
186 DWORD cu_versions;
187 } dwarf2_parse_module_context_t;
189 typedef struct dwarf2_dwz_alternate_s
191 struct image_file_map* fmap;
192 dwarf2_section_t sections[section_max];
193 struct image_section_map sectmap[section_max];
194 dwarf2_parse_module_context_t module_ctx;
195 } dwarf2_dwz_alternate_t;
197 enum unit_status
199 UNIT_ERROR,
200 UNIT_NOTLOADED,
201 UNIT_LOADED,
202 UNIT_LOADED_FAIL,
203 UNIT_BEINGLOADED,
206 /* this is the context used for parsing a compilation unit
207 * inside an ELF/PE section (likely .debug_info)
209 typedef struct dwarf2_parse_context_s
211 dwarf2_parse_module_context_t* module_ctx;
212 unsigned section;
213 struct pool pool;
214 struct symt_compiland* compiland;
215 struct sparse_array abbrev_table;
216 struct sparse_array debug_info_table;
217 ULONG_PTR ref_offset;
218 char* cpp_name;
219 dwarf2_cuhead_t head;
220 enum unit_status status;
221 dwarf2_traverse_context_t traverse_DIE;
222 } dwarf2_parse_context_t;
224 /* stored in the dbghelp's module internal structure for later reuse */
225 struct dwarf2_module_info_s
227 dwarf2_cuhead_t** cuheads;
228 unsigned num_cuheads;
229 dwarf2_section_t debug_loc;
230 dwarf2_section_t debug_frame;
231 dwarf2_section_t eh_frame;
232 unsigned char word_size;
235 #define loc_dwarf2_location_list (loc_user + 0)
236 #define loc_dwarf2_block (loc_user + 1)
237 #define loc_dwarf2_frame_cfa (loc_user + 2)
239 /* forward declarations */
240 static struct symt* dwarf2_parse_enumeration_type(dwarf2_debug_info_t* entry);
241 static BOOL dwarf2_parse_compilation_unit(dwarf2_parse_context_t* ctx);
242 static dwarf2_parse_context_t* dwarf2_locate_cu(dwarf2_parse_module_context_t* module_ctx, ULONG_PTR ref);
244 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
246 return *ptr;
249 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
251 unsigned char uvalue = dwarf2_get_byte(ctx->data);
252 ctx->data += 1;
253 return uvalue;
256 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
258 return *(const UINT16*)ptr;
261 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
263 unsigned short uvalue = dwarf2_get_u2(ctx->data);
264 ctx->data += 2;
265 return uvalue;
268 static ULONG_PTR dwarf2_get_u4(const unsigned char* ptr)
270 return *(const UINT32*)ptr;
273 static ULONG_PTR dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
275 ULONG_PTR uvalue = dwarf2_get_u4(ctx->data);
276 ctx->data += 4;
277 return uvalue;
280 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
282 return *(const UINT64*)ptr;
285 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
287 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
288 ctx->data += 8;
289 return uvalue;
292 static ULONG_PTR dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
294 ULONG_PTR ret = 0;
295 unsigned char byte;
296 unsigned shift = 0;
300 byte = dwarf2_get_byte(ptr++);
301 ret |= (byte & 0x7f) << shift;
302 shift += 7;
303 } while (byte & 0x80);
305 if (end) *end = ptr;
306 return ret;
309 static ULONG_PTR dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
311 ULONG_PTR ret;
313 assert(ctx);
315 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
317 return ret;
320 static LONG_PTR dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
322 LONG_PTR ret = 0;
323 unsigned char byte;
324 unsigned shift = 0;
325 const unsigned size = sizeof(int) * 8;
329 byte = dwarf2_get_byte(ptr++);
330 ret |= (byte & 0x7f) << shift;
331 shift += 7;
332 } while (byte & 0x80);
333 if (end) *end = ptr;
335 /* as spec: sign bit of byte is 2nd high order bit (80x40)
336 * -> 0x80 is used as flag.
338 if ((shift < size) && (byte & 0x40))
340 ret |= - (1 << shift);
342 return ret;
345 static LONG_PTR dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
347 LONG_PTR ret = 0;
349 assert(ctx);
351 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
352 return ret;
355 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
357 unsigned ret;
358 for (ret = 0; ctx->data[ret] & 0x80; ret++);
359 return ret + 1;
362 /******************************************************************
363 * dwarf2_get_addr
365 * Returns an address.
366 * We assume that in all cases word size from Dwarf matches the size of
367 * addresses in platform where the exec is compiled.
369 static ULONG_PTR dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
371 ULONG_PTR ret;
373 switch (word_size)
375 case 4:
376 ret = dwarf2_get_u4(ptr);
377 break;
378 case 8:
379 ret = dwarf2_get_u8(ptr);
380 break;
381 default:
382 FIXME("Unsupported Word Size %u\n", word_size);
383 ret = 0;
385 return ret;
388 static inline ULONG_PTR dwarf2_parse_addr(dwarf2_traverse_context_t* ctx, unsigned word_size)
390 ULONG_PTR ret = dwarf2_get_addr(ctx->data, word_size);
391 ctx->data += word_size;
392 return ret;
395 static inline ULONG_PTR dwarf2_parse_addr_head(dwarf2_traverse_context_t* ctx, const dwarf2_cuhead_t* head)
397 return dwarf2_parse_addr(ctx, head->word_size);
400 static ULONG_PTR dwarf2_parse_offset(dwarf2_traverse_context_t* ctx, unsigned char offset_size)
402 ULONG_PTR ret = dwarf2_get_addr(ctx->data, offset_size);
403 ctx->data += offset_size;
404 return ret;
407 static ULONG_PTR dwarf2_parse_3264(dwarf2_traverse_context_t* ctx, unsigned char* ofsz)
409 ULONG_PTR ret = dwarf2_parse_u4(ctx);
410 if (ret == 0xffffffff)
412 ret = dwarf2_parse_u8(ctx);
413 *ofsz = 8;
415 else *ofsz = 4;
416 return ret;
419 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
421 return wine_dbg_sprintf("ctx(%p)", ctx->data);
424 static const char* dwarf2_debug_unit_ctx(const dwarf2_parse_context_t* ctx)
426 return wine_dbg_sprintf("ctx(%p,%s)",
427 ctx, debugstr_w(ctx->module_ctx->module->modulename));
430 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
432 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p) in %s",
433 di->abbrev, di->symt, dwarf2_debug_unit_ctx(di->unit_ctx));
436 static dwarf2_abbrev_entry_t*
437 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
438 ULONG_PTR entry_code)
440 assert( NULL != abbrev_table );
441 return sparse_array_find(abbrev_table, entry_code);
444 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
445 struct sparse_array* abbrev_table,
446 struct pool* pool)
448 ULONG_PTR entry_code;
449 dwarf2_abbrev_entry_t* abbrev_entry;
450 dwarf2_abbrev_entry_attr_t* new = NULL;
451 dwarf2_abbrev_entry_attr_t* last = NULL;
452 ULONG_PTR attribute;
453 ULONG_PTR form;
455 assert( NULL != abbrev_ctx );
457 TRACE("%s, end at %p\n",
458 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
460 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
461 while (abbrev_ctx->data < abbrev_ctx->end_data)
463 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
464 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
465 TRACE("found entry_code %lu\n", entry_code);
466 if (!entry_code)
468 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
469 break;
471 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
472 assert( NULL != abbrev_entry );
474 abbrev_entry->entry_code = entry_code;
475 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
476 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
477 abbrev_entry->attrs = NULL;
478 abbrev_entry->num_attr = 0;
480 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
481 abbrev_table, sparse_array_length(abbrev_table),
482 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
484 last = NULL;
485 while (1)
487 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
488 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
489 if (!attribute) break;
491 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
492 assert(new);
494 new->attribute = attribute;
495 new->form = form;
496 new->next = NULL;
497 if (abbrev_entry->attrs) last->next = new;
498 else abbrev_entry->attrs = new;
499 last = new;
500 abbrev_entry->num_attr++;
503 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
506 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
507 const dwarf2_cuhead_t* head,
508 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
510 unsigned step;
512 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
514 switch (abbrev_attr->form)
516 case DW_FORM_flag_present: step = 0; break;
517 case DW_FORM_ref_addr: step = (head->version >= 3) ? head->offset_size : head->word_size; break;
518 case DW_FORM_addr: step = head->word_size; break;
519 case DW_FORM_flag:
520 case DW_FORM_data1:
521 case DW_FORM_ref1: step = 1; break;
522 case DW_FORM_data2:
523 case DW_FORM_ref2: step = 2; break;
524 case DW_FORM_data4:
525 case DW_FORM_ref4: step = 4; break;
526 case DW_FORM_data8:
527 case DW_FORM_ref8: step = 8; break;
528 case DW_FORM_sdata:
529 case DW_FORM_ref_udata:
530 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
531 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
532 case DW_FORM_exprloc:
533 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
534 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
535 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
536 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
537 case DW_FORM_sec_offset:
538 case DW_FORM_GNU_ref_alt:
539 case DW_FORM_GNU_strp_alt:
540 case DW_FORM_strp: step = head->offset_size; break;
541 default:
542 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
543 return;
545 ctx->data += step;
548 static BOOL dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
549 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
550 const unsigned char* data,
551 struct attribute* attr)
553 attr->form = abbrev_attr->form;
554 switch (attr->form)
556 case DW_FORM_ref_addr:
557 if (ctx->head.version >= 3)
558 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.offset_size);
559 else
560 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.word_size);
561 TRACE("addr<0x%lx>\n", attr->u.uvalue);
562 break;
564 case DW_FORM_addr:
565 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.word_size);
566 TRACE("addr<0x%lx>\n", attr->u.uvalue);
567 break;
569 case DW_FORM_flag:
570 attr->u.uvalue = dwarf2_get_byte(data);
571 TRACE("flag<0x%lx>\n", attr->u.uvalue);
572 break;
574 case DW_FORM_flag_present:
575 attr->u.uvalue = 1;
576 TRACE("flag_present\n");
577 break;
579 case DW_FORM_data1:
580 attr->u.uvalue = dwarf2_get_byte(data);
581 TRACE("data1<%lu>\n", attr->u.uvalue);
582 break;
584 case DW_FORM_data2:
585 attr->u.uvalue = dwarf2_get_u2(data);
586 TRACE("data2<%lu>\n", attr->u.uvalue);
587 break;
589 case DW_FORM_data4:
590 attr->u.uvalue = dwarf2_get_u4(data);
591 TRACE("data4<%lu>\n", attr->u.uvalue);
592 break;
594 case DW_FORM_data8:
595 attr->u.lluvalue = dwarf2_get_u8(data);
596 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
597 break;
599 case DW_FORM_ref1:
600 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
601 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
602 break;
604 case DW_FORM_ref2:
605 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
606 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
607 break;
609 case DW_FORM_ref4:
610 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
611 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
612 break;
614 case DW_FORM_ref8:
615 FIXME("Unhandled 64-bit support\n");
616 break;
618 case DW_FORM_sdata:
619 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
620 break;
622 case DW_FORM_ref_udata:
623 attr->u.uvalue = ctx->ref_offset + dwarf2_get_leb128_as_unsigned(data, NULL);
624 TRACE("ref_udata<0x%lx>\n", attr->u.uvalue);
625 break;
627 case DW_FORM_udata:
628 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
629 TRACE("udata<0x%lx>\n", attr->u.uvalue);
630 break;
632 case DW_FORM_string:
633 attr->u.string = (const char *)data;
634 TRACE("string<%s>\n", debugstr_a(attr->u.string));
635 break;
637 case DW_FORM_strp:
639 ULONG_PTR ofs = dwarf2_get_addr(data, ctx->head.offset_size);
640 if (ofs >= ctx->module_ctx->sections[section_string].size)
642 ERR("Out of bounds string offset (%08lx)\n", ofs);
643 attr->u.string = "<<outofbounds-strp>>";
645 else
647 attr->u.string = (const char*)ctx->module_ctx->sections[section_string].address + ofs;
648 TRACE("strp<%s>\n", debugstr_a(attr->u.string));
651 break;
653 case DW_FORM_block:
654 case DW_FORM_exprloc:
655 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
656 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
657 break;
659 case DW_FORM_block1:
660 attr->u.block.size = dwarf2_get_byte(data);
661 attr->u.block.ptr = data + 1;
662 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
663 break;
665 case DW_FORM_block2:
666 attr->u.block.size = dwarf2_get_u2(data);
667 attr->u.block.ptr = data + 2;
668 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
669 break;
671 case DW_FORM_block4:
672 attr->u.block.size = dwarf2_get_u4(data);
673 attr->u.block.ptr = data + 4;
674 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
675 break;
677 case DW_FORM_sec_offset:
678 attr->u.lluvalue = dwarf2_get_addr(data, ctx->head.offset_size);
679 TRACE("sec_offset<%s>\n", wine_dbgstr_longlong(attr->u.lluvalue));
680 break;
682 case DW_FORM_GNU_ref_alt:
683 if (!ctx->module_ctx->dwz)
685 ERR("No DWZ file present for GNU_ref_alt in %s\n", debugstr_w(ctx->module_ctx->module->modulename));
686 attr->u.uvalue = 0;
687 return FALSE;
689 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.offset_size);
690 TRACE("ref_alt<0x%lx>\n", attr->u.uvalue);
691 break;
693 case DW_FORM_GNU_strp_alt:
694 if (ctx->module_ctx->dwz)
696 ULONG_PTR ofs = dwarf2_get_addr(data, ctx->head.offset_size);
697 if (ofs < ctx->module_ctx->dwz->sections[section_string].size)
699 attr->u.string = (const char*)ctx->module_ctx->dwz->sections[section_string].address + ofs;
700 TRACE("strp_alt<%s>\n", debugstr_a(attr->u.string));
702 else
704 ERR("out of bounds strp_alt: 0x%lx 0x%x (%u)\n", ofs, ctx->module_ctx->dwz->sections[section_string].size, ctx->head.offset_size);
705 attr->u.string = "<<outofbounds-strpalt>>";
708 else
710 ERR("No DWZ file present for GNU_strp_alt in %s\n", debugstr_w(ctx->module_ctx->module->modulename));
711 attr->u.string = "<<noDWZ-strpalt>>";
713 break;
715 default:
716 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
717 break;
719 return TRUE;
722 static dwarf2_debug_info_t* dwarf2_jump_to_debug_info(struct attribute* attr);
724 static BOOL dwarf2_find_attribute(const dwarf2_debug_info_t* di,
725 unsigned at, struct attribute* attr)
727 unsigned i, refidx = 0;
728 dwarf2_abbrev_entry_attr_t* abbrev_attr;
729 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
731 attr->gotten_from = attr_direct;
732 while (di)
734 ref_abbrev_attr = NULL;
735 attr->debug_info = di;
736 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
738 if (abbrev_attr->attribute == at)
740 return dwarf2_fill_attr(di->unit_ctx, abbrev_attr, di->data[i], attr);
742 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
743 abbrev_attr->attribute == DW_AT_specification) &&
744 at != DW_AT_sibling)
746 if (ref_abbrev_attr)
747 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
748 ref_abbrev_attr = abbrev_attr;
749 refidx = i;
750 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
751 attr_abstract_origin : attr_specification;
754 /* do we have either an abstract origin or a specification debug entry to look into ? */
755 if (!ref_abbrev_attr || !dwarf2_fill_attr(di->unit_ctx, ref_abbrev_attr, di->data[refidx], attr))
756 break;
757 if (!(di = dwarf2_jump_to_debug_info(attr)))
759 FIXME("Should have found the debug info entry\n");
760 break;
763 return FALSE;
766 static dwarf2_debug_info_t* dwarf2_jump_to_debug_info(struct attribute* attr)
768 dwarf2_parse_context_t* ref_ctx = NULL;
769 BOOL with_other = TRUE;
770 dwarf2_debug_info_t* ret;
772 switch (attr->form)
774 case DW_FORM_ref_addr:
775 ref_ctx = dwarf2_locate_cu(attr->debug_info->unit_ctx->module_ctx, attr->u.uvalue);
776 break;
777 case DW_FORM_GNU_ref_alt:
778 if (attr->debug_info->unit_ctx->module_ctx->dwz)
779 ref_ctx = dwarf2_locate_cu(&attr->debug_info->unit_ctx->module_ctx->dwz->module_ctx, attr->u.uvalue);
780 break;
781 default:
782 with_other = FALSE;
783 ref_ctx = attr->debug_info->unit_ctx;
784 break;
786 if (!ref_ctx) return FALSE;
787 /* There are cases where we end up with a circular reference between two (or more)
788 * compilation units. Before this happens, try to see if we can refer to an already
789 * loaded debug_info in the target compilation unit (even if all the debug_info
790 * haven't been loaded yet).
792 if (ref_ctx->status == UNIT_BEINGLOADED &&
793 (ret = sparse_array_find(&ref_ctx->debug_info_table, attr->u.uvalue)))
794 return ret;
795 if (with_other)
797 /* ensure CU is fully loaded */
798 if (ref_ctx != attr->debug_info->unit_ctx && !dwarf2_parse_compilation_unit(ref_ctx))
799 return NULL;
801 return sparse_array_find(&ref_ctx->debug_info_table, attr->u.uvalue);
804 static void dwarf2_load_one_entry(dwarf2_debug_info_t*);
806 #define Wine_DW_no_register 0x7FFFFFFF
808 static unsigned dwarf2_map_register(int regno, const struct module* module)
810 if (regno == Wine_DW_no_register)
812 FIXME("What the heck map reg 0x%x\n",regno);
813 return 0;
815 return dbghelp_current_cpu->map_dwarf_register(regno, module, FALSE);
818 static enum location_error
819 compute_location(const struct module *module, const dwarf2_cuhead_t* head,
820 dwarf2_traverse_context_t* ctx, struct location* loc,
821 HANDLE hproc, const struct location* frame)
823 DWORD_PTR tmp, stack[64];
824 unsigned stk;
825 unsigned char op;
826 BOOL piece_found = FALSE;
828 stack[stk = 0] = 0;
830 loc->kind = loc_absolute;
831 loc->reg = Wine_DW_no_register;
833 while (ctx->data < ctx->end_data)
835 op = dwarf2_parse_byte(ctx);
837 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
838 stack[++stk] = op - DW_OP_lit0;
839 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
841 /* dbghelp APIs don't know how to cope with this anyway
842 * (for example 'long long' stored in two registers)
843 * FIXME: We should tell winedbg how to deal with it (sigh)
845 if (!piece_found)
847 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0, module);
848 if (loc->reg != Wine_DW_no_register)
849 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
850 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
851 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
852 loc->reg = cvreg;
854 loc->kind = loc_register;
856 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
858 /* dbghelp APIs don't know how to cope with this anyway
859 * (for example 'long long' stored in two registers)
860 * FIXME: We should tell winedbg how to deal with it (sigh)
862 if (!piece_found)
864 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0, module);
865 if (loc->reg != Wine_DW_no_register)
866 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
867 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
868 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
869 loc->reg = cvreg;
871 stack[++stk] = dwarf2_leb128_as_signed(ctx);
872 loc->kind = loc_regrel;
874 else switch (op)
876 case DW_OP_nop: break;
877 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr_head(ctx, head); break;
878 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
879 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
880 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
881 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
882 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
883 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
884 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
885 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
886 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
887 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
888 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
889 case DW_OP_drop: stk--; break;
890 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
891 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
892 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
893 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
894 case DW_OP_abs: stack[stk] = sizeof(stack[stk]) == 8 ? llabs((INT64)stack[stk]) : abs((INT32)stack[stk]); break;
895 case DW_OP_neg: stack[stk] = -stack[stk]; break;
896 case DW_OP_not: stack[stk] = ~stack[stk]; break;
897 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
898 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
899 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
900 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
901 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
902 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
903 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
904 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
905 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
906 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
907 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
908 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
909 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
910 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
911 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
912 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
913 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
914 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
915 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
916 case DW_OP_bra:
917 tmp = dwarf2_parse_u2(ctx);
918 if (!stack[stk--]) ctx->data += tmp;
919 break;
920 case DW_OP_regx:
921 tmp = dwarf2_leb128_as_unsigned(ctx);
922 if (!piece_found)
924 if (loc->reg != Wine_DW_no_register)
925 FIXME("Only supporting one reg\n");
926 loc->reg = dwarf2_map_register(tmp, module);
928 loc->kind = loc_register;
929 break;
930 case DW_OP_bregx:
931 tmp = dwarf2_leb128_as_unsigned(ctx);
932 if (loc->reg != Wine_DW_no_register)
933 FIXME("Only supporting one regx\n");
934 loc->reg = dwarf2_map_register(tmp, module);
935 stack[++stk] = dwarf2_leb128_as_signed(ctx);
936 loc->kind = loc_regrel;
937 break;
938 case DW_OP_fbreg:
939 if (loc->reg != Wine_DW_no_register)
940 FIXME("Only supporting one reg (%s/%d -> -2)\n",
941 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
942 if (frame && frame->kind == loc_register)
944 loc->kind = loc_regrel;
945 loc->reg = frame->reg;
946 stack[++stk] = dwarf2_leb128_as_signed(ctx);
948 else if (frame && frame->kind == loc_regrel)
950 loc->kind = loc_regrel;
951 loc->reg = frame->reg;
952 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
954 else
956 /* FIXME: this could be later optimized by not recomputing
957 * this very location expression
959 loc->kind = loc_dwarf2_block;
960 stack[++stk] = dwarf2_leb128_as_signed(ctx);
962 break;
963 case DW_OP_piece:
965 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
966 WARN("Not handling OP_piece (size=%d)\n", sz);
967 piece_found = TRUE;
969 break;
970 case DW_OP_deref:
971 if (!stk)
973 FIXME("Unexpected empty stack\n");
974 return loc_err_internal;
976 if (loc->reg != Wine_DW_no_register)
978 WARN("Too complex expression for deref\n");
979 return loc_err_too_complex;
981 if (hproc)
983 DWORD_PTR addr = stack[stk--];
984 DWORD_PTR deref = 0;
986 if (!ReadProcessMemory(hproc, (void*)addr, &deref, head->word_size, NULL))
988 WARN("Couldn't read memory at %lx\n", addr);
989 return loc_err_cant_read;
991 stack[++stk] = deref;
993 else
995 loc->kind = loc_dwarf2_block;
997 break;
998 case DW_OP_deref_size:
999 if (!stk)
1001 FIXME("Unexpected empty stack\n");
1002 return loc_err_internal;
1004 if (loc->reg != Wine_DW_no_register)
1006 WARN("Too complex expression for deref\n");
1007 return loc_err_too_complex;
1009 if (hproc)
1011 DWORD_PTR addr = stack[stk--];
1012 BYTE derefsize = dwarf2_parse_byte(ctx);
1013 DWORD64 deref;
1015 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
1017 WARN("Couldn't read memory at %lx\n", addr);
1018 return loc_err_cant_read;
1021 switch (derefsize)
1023 case 1: stack[++stk] = *(unsigned char*)&deref; break;
1024 case 2: stack[++stk] = *(unsigned short*)&deref; break;
1025 case 4: stack[++stk] = *(DWORD*)&deref; break;
1026 case 8: if (head->word_size >= derefsize) stack[++stk] = deref; break;
1029 else
1031 dwarf2_parse_byte(ctx);
1032 loc->kind = loc_dwarf2_block;
1034 break;
1035 case DW_OP_stack_value:
1036 /* Expected behaviour is that this is the last instruction of this
1037 * expression and just the "top of stack" value should be put to loc->offset. */
1038 break;
1039 default:
1040 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
1041 FIXME("Unhandled attr op: %x\n", op);
1042 /* FIXME else unhandled extension */
1043 return loc_err_internal;
1046 loc->offset = stack[stk];
1047 return 0;
1050 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
1051 const dwarf2_debug_info_t* di,
1052 ULONG_PTR dw,
1053 struct location* loc,
1054 const struct location* frame)
1056 struct attribute xloc;
1058 if (!dwarf2_find_attribute(di, dw, &xloc)) return FALSE;
1060 switch (xloc.form)
1062 case DW_FORM_data1: case DW_FORM_data2:
1063 case DW_FORM_udata: case DW_FORM_sdata:
1064 loc->kind = loc_absolute;
1065 loc->reg = 0;
1066 loc->offset = xloc.u.uvalue;
1067 return TRUE;
1068 case DW_FORM_data4:
1069 loc->kind = loc_dwarf2_location_list;
1070 loc->reg = Wine_DW_no_register;
1071 loc->offset = xloc.u.uvalue;
1072 return TRUE;
1073 case DW_FORM_data8: case DW_FORM_sec_offset:
1074 loc->kind = loc_dwarf2_location_list;
1075 loc->reg = Wine_DW_no_register;
1076 loc->offset = xloc.u.lluvalue;
1077 return TRUE;
1078 case DW_FORM_block:
1079 case DW_FORM_block1:
1080 case DW_FORM_block2:
1081 case DW_FORM_block4:
1082 case DW_FORM_exprloc:
1083 break;
1084 default: FIXME("Unsupported yet form %lx\n", xloc.form);
1085 return FALSE;
1088 /* assume we have a block form */
1089 if (dw == DW_AT_frame_base && xloc.u.block.size == 1 && *xloc.u.block.ptr == DW_OP_call_frame_cfa)
1091 loc->kind = loc_dwarf2_frame_cfa;
1092 loc->reg = Wine_DW_no_register;
1093 loc->offset = 0;
1095 else if (xloc.u.block.size)
1097 dwarf2_traverse_context_t lctx;
1098 enum location_error err;
1100 lctx.data = xloc.u.block.ptr;
1101 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
1103 err = compute_location(ctx->module_ctx->module, &ctx->head, &lctx, loc, NULL, frame);
1104 if (err < 0)
1106 loc->kind = loc_error;
1107 loc->reg = err;
1109 else if (loc->kind == loc_dwarf2_block)
1111 unsigned* ptr = pool_alloc(&ctx->module_ctx->module->pool,
1112 sizeof(unsigned) + xloc.u.block.size);
1113 *ptr = xloc.u.block.size;
1114 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
1115 loc->offset = (ULONG_PTR)ptr;
1118 return TRUE;
1121 static struct symt* dwarf2_lookup_type(const dwarf2_debug_info_t* di)
1123 struct attribute attr;
1124 dwarf2_debug_info_t* type;
1126 if (!dwarf2_find_attribute(di, DW_AT_type, &attr))
1127 /* this is only valid if current language of CU is C or C++ */
1128 return di->unit_ctx->module_ctx->symt_cache[sc_void];
1129 if (!(type = dwarf2_jump_to_debug_info(&attr)))
1130 return di->unit_ctx->module_ctx->symt_cache[sc_unknown];
1132 if (type == di)
1134 FIXME("Reference to itself\n");
1135 return di->unit_ctx->module_ctx->symt_cache[sc_unknown];
1137 if (!type->symt)
1139 /* load the debug info entity */
1140 dwarf2_load_one_entry(type);
1141 if (!type->symt)
1143 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
1144 return di->unit_ctx->module_ctx->symt_cache[sc_unknown];
1147 return type->symt;
1150 static const char* dwarf2_get_cpp_name(dwarf2_debug_info_t* di, const char* name)
1152 char* last;
1153 struct attribute diname;
1154 struct attribute spec;
1156 if (di->abbrev->tag == DW_TAG_compile_unit || di->abbrev->tag == DW_TAG_partial_unit) return name;
1158 /* if the di is a definition, but has also a (previous) declaration, then scope must
1159 * be gotten from declaration not definition
1161 if (dwarf2_find_attribute(di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
1163 di = dwarf2_jump_to_debug_info(&spec);
1164 if (!di)
1166 FIXME("Should have found the debug info entry\n");
1167 return NULL;
1171 if (!di->unit_ctx->cpp_name)
1172 di->unit_ctx->cpp_name = pool_alloc(&di->unit_ctx->pool, MAX_SYM_NAME);
1173 last = di->unit_ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
1174 strcpy(last, name);
1176 for (di = di->parent; di; di = di->parent)
1178 switch (di->abbrev->tag)
1180 case DW_TAG_namespace:
1181 case DW_TAG_structure_type:
1182 case DW_TAG_class_type:
1183 case DW_TAG_interface_type:
1184 case DW_TAG_union_type:
1185 if (dwarf2_find_attribute(di, DW_AT_name, &diname))
1187 size_t len = strlen(diname.u.string);
1188 last -= 2 + len;
1189 if (last < di->unit_ctx->cpp_name) return NULL;
1190 memcpy(last, diname.u.string, len);
1191 last[len] = last[len + 1] = ':';
1193 break;
1194 default:
1195 break;
1198 return last;
1201 /******************************************************************
1202 * dwarf2_read_range
1204 * read a range for a given debug_info (either using AT_range attribute, in which
1205 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1206 * in all cases, range is relative to beginning of compilation unit
1208 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1209 ULONG_PTR* plow, ULONG_PTR* phigh)
1211 struct attribute range;
1213 if (dwarf2_find_attribute(di, DW_AT_ranges, &range))
1215 dwarf2_traverse_context_t traverse;
1216 ULONG_PTR low, high;
1217 const ULONG_PTR UMAX = ~(ULONG_PTR)0u;
1219 traverse.data = ctx->module_ctx->sections[section_ranges].address + range.u.uvalue;
1220 traverse.end_data = ctx->module_ctx->sections[section_ranges].address +
1221 ctx->module_ctx->sections[section_ranges].size;
1223 *plow = UMAX;
1224 *phigh = 0;
1225 while (traverse.data + 2 * ctx->head.word_size < traverse.end_data)
1227 low = dwarf2_parse_addr_head(&traverse, &ctx->head);
1228 high = dwarf2_parse_addr_head(&traverse, &ctx->head);
1229 if (low == 0 && high == 0) break;
1230 if (low == (ctx->head.word_size == 8 ? (~(DWORD64)0u) : (DWORD64)(~0u)))
1231 FIXME("unsupported yet (base address selection)\n");
1232 /* range values are relative to start of compilation unit */
1233 low += ctx->compiland->address - ctx->module_ctx->load_offset;
1234 high += ctx->compiland->address - ctx->module_ctx->load_offset;
1235 if (low < *plow) *plow = low;
1236 if (high > *phigh) *phigh = high;
1238 if (*plow == UMAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1239 if (*plow == *phigh) {WARN("entry found, but low=high %lx %lx\n", low, high); return FALSE;}
1241 return TRUE;
1243 else
1245 struct attribute low_pc;
1246 struct attribute high_pc;
1248 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc) ||
1249 !dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc))
1250 return FALSE;
1251 *plow = low_pc.u.uvalue;
1252 *phigh = high_pc.u.uvalue;
1253 if (ctx->head.version >= 4)
1254 switch (high_pc.form)
1256 case DW_FORM_addr:
1257 break;
1258 case DW_FORM_data1:
1259 case DW_FORM_data2:
1260 case DW_FORM_data4:
1261 case DW_FORM_data8:
1262 case DW_FORM_sdata:
1263 case DW_FORM_udata:
1264 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1265 *phigh += *plow;
1266 break;
1267 default:
1268 FIXME("Unsupported class for high_pc\n");
1269 break;
1271 return TRUE;
1275 /******************************************************************
1276 * dwarf2_read_one_debug_info
1278 * Loads into memory one debug info entry, and recursively its children (if any)
1280 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1281 dwarf2_traverse_context_t* traverse,
1282 dwarf2_debug_info_t* parent_di,
1283 dwarf2_debug_info_t** pdi)
1285 const dwarf2_abbrev_entry_t*abbrev;
1286 ULONG_PTR entry_code;
1287 ULONG_PTR offset;
1288 dwarf2_debug_info_t* di;
1289 dwarf2_debug_info_t* child;
1290 dwarf2_debug_info_t** where;
1291 dwarf2_abbrev_entry_attr_t* attr;
1292 unsigned i;
1293 struct attribute sibling;
1295 offset = traverse->data - ctx->module_ctx->sections[ctx->section].address;
1296 entry_code = dwarf2_leb128_as_unsigned(traverse);
1297 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1298 if (!entry_code)
1300 *pdi = NULL;
1301 return TRUE;
1303 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1304 if (!abbrev)
1306 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1307 return FALSE;
1309 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1310 if (!di) return FALSE;
1311 di->abbrev = abbrev;
1312 di->symt = NULL;
1313 di->parent = parent_di;
1314 di->unit_ctx = ctx;
1316 if (abbrev->num_attr)
1318 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1319 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1321 di->data[i] = traverse->data;
1322 dwarf2_swallow_attribute(traverse, &ctx->head, attr);
1325 else di->data = NULL;
1326 if (abbrev->have_child)
1328 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1329 while (traverse->data < traverse->end_data)
1331 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1332 if (!child) break;
1333 where = vector_add(&di->children, &ctx->pool);
1334 if (!where) return FALSE;
1335 *where = child;
1338 if (dwarf2_find_attribute(di, DW_AT_sibling, &sibling) &&
1339 traverse->data != ctx->module_ctx->sections[ctx->section].address + sibling.u.uvalue)
1341 if (sibling.u.uvalue >= ctx->module_ctx->sections[ctx->section].size)
1343 FIXME("cursor sibling after section end %s: 0x%lx 0x%x\n",
1344 dwarf2_debug_unit_ctx(ctx), sibling.u.uvalue, ctx->module_ctx->sections[ctx->section].size);
1345 return FALSE;
1347 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1348 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1349 traverse->data = ctx->module_ctx->sections[ctx->section].address + sibling.u.uvalue;
1351 *pdi = di;
1352 return TRUE;
1355 static struct vector* dwarf2_get_di_children(dwarf2_debug_info_t* di)
1357 struct attribute spec;
1359 while (di)
1361 if (di->abbrev->have_child)
1362 return &di->children;
1363 if (!dwarf2_find_attribute(di, DW_AT_specification, &spec)) break;
1364 if (!(di = dwarf2_jump_to_debug_info(&spec)))
1365 FIXME("Should have found the debug info entry\n");
1367 return NULL;
1370 static struct symt* dwarf2_parse_base_type(dwarf2_debug_info_t* di)
1372 struct attribute name;
1373 struct attribute size;
1374 struct attribute encoding;
1375 enum BasicType bt;
1376 if (di->symt) return di->symt;
1378 TRACE("%s\n", dwarf2_debug_di(di));
1380 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1381 name.u.string = NULL;
1382 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1383 if (!dwarf2_find_attribute(di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1385 switch (encoding.u.uvalue)
1387 case DW_ATE_void: bt = btVoid; break;
1388 case DW_ATE_address: bt = btULong; break;
1389 case DW_ATE_boolean: bt = btBool; break;
1390 case DW_ATE_complex_float: bt = btComplex; break;
1391 case DW_ATE_float: bt = btFloat; break;
1392 case DW_ATE_signed: bt = btInt; break;
1393 case DW_ATE_unsigned: bt = btUInt; break;
1394 case DW_ATE_signed_char: bt = btChar; break;
1395 case DW_ATE_unsigned_char: bt = btChar; break;
1396 default: bt = btNoType; break;
1398 di->symt = &symt_new_basic(di->unit_ctx->module_ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1400 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1401 return di->symt;
1404 static struct symt* dwarf2_parse_typedef(dwarf2_debug_info_t* di)
1406 struct symt* ref_type;
1407 struct attribute name;
1409 if (di->symt) return di->symt;
1411 TRACE("%s\n", dwarf2_debug_di(di));
1413 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1414 ref_type = dwarf2_lookup_type(di);
1416 if (name.u.string)
1417 di->symt = &symt_new_typedef(di->unit_ctx->module_ctx->module, ref_type, name.u.string)->symt;
1418 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1419 return di->symt;
1422 static struct symt* dwarf2_parse_pointer_type(dwarf2_debug_info_t* di)
1424 struct symt* ref_type;
1425 struct attribute size;
1427 if (di->symt) return di->symt;
1429 TRACE("%s\n", dwarf2_debug_di(di));
1431 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1432 ref_type = dwarf2_lookup_type(di);
1433 di->symt = &symt_new_pointer(di->unit_ctx->module_ctx->module, ref_type, size.u.uvalue)->symt;
1434 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1435 return di->symt;
1438 static struct symt* dwarf2_parse_subrange_type(dwarf2_debug_info_t* di)
1440 struct symt* ref_type;
1441 struct attribute name;
1442 struct attribute dummy;
1444 if (di->symt) return di->symt;
1446 TRACE("%s\n", dwarf2_debug_di(di));
1448 if (dwarf2_find_attribute(di, DW_AT_name, &name)) FIXME("Found name for subrange %s\n", name.u.string);
1449 if (dwarf2_find_attribute(di, DW_AT_byte_size, &dummy)) FIXME("Found byte_size %lu\n", dummy.u.uvalue);
1450 if (dwarf2_find_attribute(di, DW_AT_bit_size, &dummy)) FIXME("Found bit_size %lu\n", dummy.u.uvalue);
1451 /* for now, we don't support the byte_size nor bit_size about the subrange, and pretend the two
1452 * types are the same (FIXME)
1454 ref_type = dwarf2_lookup_type(di);
1455 di->symt = ref_type;
1456 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1457 return di->symt;
1460 static struct symt* dwarf2_parse_array_type(dwarf2_debug_info_t* di)
1462 struct symt* ref_type;
1463 struct symt* idx_type = NULL;
1464 struct symt* symt = NULL;
1465 struct attribute min, max, cnt;
1466 dwarf2_debug_info_t* child;
1467 unsigned int i, j;
1468 const struct vector* children;
1470 if (di->symt) return di->symt;
1472 TRACE("%s\n", dwarf2_debug_di(di));
1474 ref_type = dwarf2_lookup_type(di);
1476 if (!(children = dwarf2_get_di_children(di)))
1478 /* fake an array with unknown size */
1479 /* FIXME: int4 even on 64bit machines??? */
1480 idx_type = &symt_new_basic(di->unit_ctx->module_ctx->module, btInt, "int", 4)->symt;
1481 min.u.uvalue = 0;
1482 cnt.u.uvalue = 0;
1484 else for (i = 0; i < vector_length(children); i++)
1486 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1487 if (child->symt == di->unit_ctx->module_ctx->symt_cache[sc_unknown]) continue;
1488 switch (child->abbrev->tag)
1490 case DW_TAG_subrange_type:
1491 idx_type = dwarf2_lookup_type(child);
1492 if (!dwarf2_find_attribute(child, DW_AT_lower_bound, &min))
1493 min.u.uvalue = 0;
1494 if (dwarf2_find_attribute(child, DW_AT_upper_bound, &max))
1495 cnt.u.uvalue = max.u.uvalue + 1 - min.u.uvalue;
1496 else if (!dwarf2_find_attribute(child, DW_AT_count, &cnt))
1497 cnt.u.uvalue = 0;
1498 break;
1499 case DW_TAG_enumeration_type:
1500 symt = dwarf2_parse_enumeration_type(child);
1501 if (symt_check_tag(symt, SymTagEnum))
1503 struct symt_enum* enum_symt = (struct symt_enum*)symt;
1504 idx_type = enum_symt->base_type;
1505 min.u.uvalue = ~0U;
1506 max.u.uvalue = ~0U;
1507 for (j = 0; j < enum_symt->vchildren.num_elts; ++j)
1509 struct symt** pc = vector_at(&enum_symt->vchildren, j);
1510 if (pc && symt_check_tag(*pc, SymTagData))
1512 struct symt_data* elt = (struct symt_data*)(*pc);
1513 if (elt->u.value.n1.n2.n3.lVal < min.u.uvalue)
1514 min.u.uvalue = elt->u.value.n1.n2.n3.lVal;
1515 if (elt->u.value.n1.n2.n3.lVal > max.u.uvalue)
1516 max.u.uvalue = elt->u.value.n1.n2.n3.lVal;
1520 break;
1521 default:
1522 FIXME("Unhandled Tag type 0x%lx at %s\n",
1523 child->abbrev->tag, dwarf2_debug_di(di));
1524 break;
1527 di->symt = &symt_new_array(di->unit_ctx->module_ctx->module, min.u.uvalue, cnt.u.uvalue, ref_type, idx_type)->symt;
1528 return di->symt;
1531 static struct symt* dwarf2_parse_const_type(dwarf2_debug_info_t* di)
1533 struct symt* ref_type;
1535 if (di->symt) return di->symt;
1537 TRACE("%s\n", dwarf2_debug_di(di));
1539 ref_type = dwarf2_lookup_type(di);
1540 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1541 di->symt = ref_type;
1543 return ref_type;
1546 static struct symt* dwarf2_parse_volatile_type(dwarf2_debug_info_t* di)
1548 struct symt* ref_type;
1550 if (di->symt) return di->symt;
1552 TRACE("%s\n", dwarf2_debug_di(di));
1554 ref_type = dwarf2_lookup_type(di);
1555 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1556 di->symt = ref_type;
1558 return ref_type;
1561 static struct symt* dwarf2_parse_restrict_type(dwarf2_debug_info_t* di)
1563 struct symt* ref_type;
1565 if (di->symt) return di->symt;
1567 TRACE("%s\n", dwarf2_debug_di(di));
1569 ref_type = dwarf2_lookup_type(di);
1570 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1571 di->symt = ref_type;
1573 return ref_type;
1576 static struct symt* dwarf2_parse_unspecified_type(dwarf2_debug_info_t* di)
1578 struct attribute name;
1579 struct attribute size;
1580 struct symt_basic *basic;
1582 TRACE("%s\n", dwarf2_debug_di(di));
1584 if (di->symt) return di->symt;
1586 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1587 name.u.string = "void";
1588 size.u.uvalue = sizeof(void *);
1590 basic = symt_new_basic(di->unit_ctx->module_ctx->module, btVoid, name.u.string, size.u.uvalue);
1591 di->symt = &basic->symt;
1593 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1594 return di->symt;
1597 static struct symt* dwarf2_parse_reference_type(dwarf2_debug_info_t* di)
1599 struct symt* ref_type = NULL;
1601 if (di->symt) return di->symt;
1603 TRACE("%s\n", dwarf2_debug_di(di));
1605 ref_type = dwarf2_lookup_type(di);
1606 /* FIXME: for now, we hard-wire C++ references to pointers */
1607 di->symt = &symt_new_pointer(di->unit_ctx->module_ctx->module, ref_type, sizeof(void *))->symt;
1609 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1611 return di->symt;
1614 static void dwarf2_parse_udt_member(dwarf2_debug_info_t* di,
1615 struct symt_udt* parent)
1617 struct symt* elt_type;
1618 struct attribute name;
1619 struct attribute bit_size;
1620 struct attribute bit_offset;
1621 struct location loc;
1623 assert(parent);
1625 TRACE("%s\n", dwarf2_debug_di(di));
1627 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1628 elt_type = dwarf2_lookup_type(di);
1629 if (dwarf2_compute_location_attr(di->unit_ctx, di, DW_AT_data_member_location, &loc, NULL))
1631 if (loc.kind != loc_absolute)
1633 FIXME("Found register, while not expecting it\n");
1634 loc.offset = 0;
1636 else
1637 TRACE("found member_location at %s -> %lu\n",
1638 dwarf2_debug_di(di), loc.offset);
1640 else
1641 loc.offset = 0;
1642 if (!dwarf2_find_attribute(di, DW_AT_bit_size, &bit_size))
1643 bit_size.u.uvalue = 0;
1644 if (dwarf2_find_attribute(di, DW_AT_bit_offset, &bit_offset))
1646 /* FIXME: we should only do this when implementation is LSB (which is
1647 * the case on i386 processors)
1649 struct attribute nbytes;
1650 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &nbytes))
1652 DWORD64 size;
1653 nbytes.u.uvalue = symt_get_info(di->unit_ctx->module_ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1654 (ULONG_PTR)size : 0;
1656 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1658 else bit_offset.u.uvalue = 0;
1659 symt_add_udt_element(di->unit_ctx->module_ctx->module, parent, name.u.string, elt_type,
1660 loc.offset, bit_offset.u.uvalue,
1661 bit_size.u.uvalue);
1663 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1666 static struct symt* dwarf2_parse_subprogram(dwarf2_debug_info_t* di);
1668 static struct symt* dwarf2_parse_udt_type(dwarf2_debug_info_t* di,
1669 enum UdtKind udt)
1671 struct attribute name;
1672 struct attribute size;
1673 struct vector* children;
1674 dwarf2_debug_info_t*child;
1675 unsigned int i;
1677 if (di->symt) return di->symt;
1679 TRACE("%s\n", dwarf2_debug_di(di));
1681 /* quirk... FIXME provide real support for anonymous UDTs */
1682 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1683 name.u.string = "zz_anon_zz";
1684 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1686 di->symt = &symt_new_udt(di->unit_ctx->module_ctx->module, dwarf2_get_cpp_name(di, name.u.string),
1687 size.u.uvalue, udt)->symt;
1689 children = dwarf2_get_di_children(di);
1690 if (children) for (i = 0; i < vector_length(children); i++)
1692 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1694 switch (child->abbrev->tag)
1696 case DW_TAG_array_type:
1697 dwarf2_parse_array_type(child);
1698 break;
1699 case DW_TAG_member:
1700 /* FIXME: should I follow the sibling stuff ?? */
1701 if (symt_check_tag(di->symt, SymTagUDT))
1702 dwarf2_parse_udt_member(child, (struct symt_udt*)di->symt);
1703 break;
1704 case DW_TAG_enumeration_type:
1705 dwarf2_parse_enumeration_type(child);
1706 break;
1707 case DW_TAG_subprogram:
1708 dwarf2_parse_subprogram(child);
1709 break;
1710 case DW_TAG_const_type:
1711 dwarf2_parse_const_type(child);
1712 break;
1713 case DW_TAG_volatile_type:
1714 dwarf2_parse_volatile_type(child);
1715 break;
1716 case DW_TAG_pointer_type:
1717 dwarf2_parse_pointer_type(child);
1718 break;
1719 case DW_TAG_subrange_type:
1720 dwarf2_parse_subrange_type(child);
1721 break;
1722 case DW_TAG_structure_type:
1723 case DW_TAG_class_type:
1724 case DW_TAG_union_type:
1725 case DW_TAG_typedef:
1726 /* FIXME: we need to handle nested udt definitions */
1727 case DW_TAG_inheritance:
1728 case DW_TAG_interface_type:
1729 case DW_TAG_template_type_param:
1730 case DW_TAG_template_value_param:
1731 case DW_TAG_variable:
1732 case DW_TAG_imported_declaration:
1733 case DW_TAG_ptr_to_member_type:
1734 case DW_TAG_GNU_template_template_param:
1735 case DW_TAG_GNU_template_parameter_pack:
1736 case DW_TAG_GNU_formal_parameter_pack:
1737 /* FIXME: some C++ related stuff */
1738 break;
1739 default:
1740 FIXME("Unhandled Tag type 0x%lx at %s\n",
1741 child->abbrev->tag, dwarf2_debug_di(di));
1742 break;
1746 return di->symt;
1749 static void dwarf2_parse_enumerator(dwarf2_debug_info_t* di,
1750 struct symt_enum* parent)
1752 struct attribute name;
1753 struct attribute value;
1755 TRACE("%s\n", dwarf2_debug_di(di));
1757 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) return;
1758 if (!dwarf2_find_attribute(di, DW_AT_const_value, &value)) value.u.svalue = 0;
1759 symt_add_enum_element(di->unit_ctx->module_ctx->module, parent, name.u.string, value.u.svalue);
1761 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1764 static struct symt* dwarf2_parse_enumeration_type(dwarf2_debug_info_t* di)
1766 struct attribute name;
1767 struct attribute size;
1768 struct symt_basic* basetype;
1769 struct vector* children;
1770 dwarf2_debug_info_t*child;
1771 unsigned int i;
1773 if (di->symt) return di->symt;
1775 TRACE("%s\n", dwarf2_debug_di(di));
1777 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1778 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1780 switch (size.u.uvalue) /* FIXME: that's wrong */
1782 case 1: basetype = symt_new_basic(di->unit_ctx->module_ctx->module, btInt, "char", 1); break;
1783 case 2: basetype = symt_new_basic(di->unit_ctx->module_ctx->module, btInt, "short", 2); break;
1784 default:
1785 case 4: basetype = symt_new_basic(di->unit_ctx->module_ctx->module, btInt, "int", 4); break;
1788 di->symt = &symt_new_enum(di->unit_ctx->module_ctx->module, name.u.string, &basetype->symt)->symt;
1790 children = dwarf2_get_di_children(di);
1791 /* FIXME: should we use the sibling stuff ?? */
1792 if (children) for (i = 0; i < vector_length(children); i++)
1794 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1796 switch (child->abbrev->tag)
1798 case DW_TAG_enumerator:
1799 if (symt_check_tag(di->symt, SymTagEnum))
1800 dwarf2_parse_enumerator(child, (struct symt_enum*)di->symt);
1801 break;
1802 default:
1803 FIXME("Unhandled Tag type 0x%lx at %s\n",
1804 di->abbrev->tag, dwarf2_debug_di(di));
1807 return di->symt;
1810 /* structure used to pass information around when parsing a subprogram */
1811 typedef struct dwarf2_subprogram_s
1813 dwarf2_parse_context_t* ctx;
1814 struct symt_function* func;
1815 struct symt_block* current_block;
1816 BOOL non_computed_variable;
1817 struct location frame;
1818 } dwarf2_subprogram_t;
1820 /******************************************************************
1821 * dwarf2_parse_variable
1823 * Parses any variable (parameter, local/global variable)
1825 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1826 dwarf2_debug_info_t* di)
1828 struct symt* param_type;
1829 struct attribute name, value;
1830 struct location loc;
1831 BOOL is_pmt;
1833 TRACE("%s\n", dwarf2_debug_di(di));
1835 is_pmt = !subpgm->current_block && di->abbrev->tag == DW_TAG_formal_parameter;
1836 param_type = dwarf2_lookup_type(di);
1838 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) {
1839 /* cannot do much without the name, the functions below won't like it. */
1840 return;
1842 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1843 &loc, &subpgm->frame))
1845 struct attribute ext;
1847 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1848 debugstr_a(name.u.string), loc.kind, loc.offset, loc.reg,
1849 dwarf2_debug_unit_ctx(subpgm->ctx));
1851 switch (loc.kind)
1853 case loc_error:
1854 break;
1855 case loc_absolute:
1856 /* it's a global variable */
1857 /* FIXME: we don't handle its scope yet */
1858 if (!dwarf2_find_attribute(di, DW_AT_external, &ext))
1859 ext.u.uvalue = 0;
1860 loc.offset += subpgm->ctx->module_ctx->load_offset;
1861 symt_new_global_variable(subpgm->ctx->module_ctx->module, subpgm->ctx->compiland,
1862 dwarf2_get_cpp_name(di, name.u.string), !ext.u.uvalue,
1863 loc, 0, param_type);
1864 break;
1865 default:
1866 subpgm->non_computed_variable = TRUE;
1867 /* fall through */
1868 case loc_register:
1869 case loc_regrel:
1870 /* either a pmt/variable relative to frame pointer or
1871 * pmt/variable in a register
1873 if (subpgm->func)
1874 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->func,
1875 is_pmt ? DataIsParam : DataIsLocal,
1876 &loc, subpgm->current_block, param_type, name.u.string);
1877 break;
1880 else if (dwarf2_find_attribute(di, DW_AT_const_value, &value))
1882 VARIANT v;
1884 switch (value.form)
1886 case DW_FORM_data1:
1887 case DW_FORM_data2:
1888 case DW_FORM_data4:
1889 case DW_FORM_udata:
1890 case DW_FORM_addr:
1891 V_VT(&v) = VT_UI4;
1892 V_UI4(&v) = value.u.uvalue;
1893 break;
1895 case DW_FORM_data8:
1896 case DW_FORM_sec_offset:
1897 V_VT(&v) = VT_UI8;
1898 V_UI8(&v) = value.u.lluvalue;
1899 break;
1901 case DW_FORM_sdata:
1902 V_VT(&v) = VT_I4;
1903 V_I4(&v) = value.u.svalue;
1904 break;
1906 case DW_FORM_strp:
1907 case DW_FORM_string:
1908 /* FIXME: native doesn't report const strings from here !!
1909 * however, the value of the string is in the code somewhere
1911 V_VT(&v) = VT_BYREF;
1912 V_BYREF(&v) = pool_strdup(&subpgm->ctx->module_ctx->module->pool, value.u.string);
1913 break;
1915 case DW_FORM_block:
1916 case DW_FORM_block1:
1917 case DW_FORM_block2:
1918 case DW_FORM_block4:
1919 case DW_FORM_exprloc:
1920 V_VT(&v) = VT_I4;
1921 switch (value.u.block.size)
1923 case 1: V_I4(&v) = *(BYTE*)value.u.block.ptr; break;
1924 case 2: V_I4(&v) = *(USHORT*)value.u.block.ptr; break;
1925 case 4: V_I4(&v) = *(DWORD*)value.u.block.ptr; break;
1926 default:
1927 V_VT(&v) = VT_BYREF;
1928 V_BYREF(&v) = pool_alloc(&subpgm->ctx->module_ctx->module->pool, value.u.block.size);
1929 memcpy(V_BYREF(&v), value.u.block.ptr, value.u.block.size);
1931 break;
1933 default:
1934 FIXME("Unsupported form for const value %s (%lx)\n",
1935 debugstr_a(name.u.string), value.form);
1936 V_VT(&v) = VT_EMPTY;
1938 if (subpgm->func)
1940 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function '%s'\n", debugstr_a(name.u.string), subpgm->func->hash_elt.name);
1941 di->symt = &symt_add_func_constant(subpgm->ctx->module_ctx->module,
1942 subpgm->func, subpgm->current_block,
1943 param_type, name.u.string, &v)->symt;
1945 else
1946 di->symt = &symt_new_constant(subpgm->ctx->module_ctx->module, subpgm->ctx->compiland,
1947 name.u.string, param_type, &v)->symt;
1949 else
1951 if (subpgm->func)
1953 /* local variable has been optimized away... report anyway */
1954 loc.kind = loc_error;
1955 loc.reg = loc_err_no_location;
1956 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->func,
1957 is_pmt ? DataIsParam : DataIsLocal,
1958 &loc, subpgm->current_block, param_type, name.u.string);
1960 else
1962 struct attribute is_decl;
1963 /* only warn when di doesn't represent a declaration */
1964 if (!dwarf2_find_attribute(di, DW_AT_declaration, &is_decl) ||
1965 !is_decl.u.uvalue || is_decl.gotten_from != attr_direct)
1966 WARN("dropping global variable %s which has been optimized away\n", debugstr_a(name.u.string));
1969 if (is_pmt && subpgm->func && symt_check_tag(subpgm->func->type, SymTagFunctionType))
1970 symt_add_function_signature_parameter(subpgm->ctx->module_ctx->module,
1971 (struct symt_function_signature*)subpgm->func->type,
1972 param_type);
1974 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1977 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1978 const dwarf2_debug_info_t* di)
1980 struct attribute name;
1981 struct attribute low_pc;
1982 struct location loc;
1984 TRACE("%s\n", dwarf2_debug_di(di));
1986 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1987 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1988 name.u.string = NULL;
1990 loc.kind = loc_absolute;
1991 loc.offset = subpgm->ctx->module_ctx->load_offset + low_pc.u.uvalue - subpgm->func->address;
1992 symt_add_function_point(subpgm->ctx->module_ctx->module, subpgm->func, SymTagLabel,
1993 &loc, name.u.string);
1996 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1997 dwarf2_debug_info_t* di);
1999 static struct symt* dwarf2_parse_subroutine_type(dwarf2_debug_info_t* di);
2001 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
2002 dwarf2_debug_info_t* di)
2004 ULONG_PTR low_pc, high_pc;
2005 struct vector* children;
2006 dwarf2_debug_info_t*child;
2007 unsigned int i;
2009 TRACE("%s\n", dwarf2_debug_di(di));
2011 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
2013 WARN("cannot read range\n");
2014 return;
2017 subpgm->current_block = symt_open_func_block(subpgm->ctx->module_ctx->module, subpgm->func, subpgm->current_block,
2018 subpgm->ctx->module_ctx->load_offset + low_pc - subpgm->func->address,
2019 high_pc - low_pc);
2021 children = dwarf2_get_di_children(di);
2022 if (children) for (i = 0; i < vector_length(children); i++)
2024 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2026 switch (child->abbrev->tag)
2028 case DW_TAG_formal_parameter:
2029 case DW_TAG_variable:
2030 dwarf2_parse_variable(subpgm, child);
2031 break;
2032 case DW_TAG_lexical_block:
2033 dwarf2_parse_subprogram_block(subpgm, child);
2034 break;
2035 case DW_TAG_inlined_subroutine:
2036 dwarf2_parse_inlined_subroutine(subpgm, child);
2037 break;
2038 case DW_TAG_label:
2039 dwarf2_parse_subprogram_label(subpgm, child);
2040 break;
2041 case DW_TAG_GNU_call_site:
2042 /* this isn't properly supported by dbghelp interface. skip it for now */
2043 break;
2044 default:
2045 FIXME("Unhandled Tag type 0x%lx at %s\n",
2046 child->abbrev->tag, dwarf2_debug_di(di));
2049 symt_close_func_block(subpgm->ctx->module_ctx->module, subpgm->func, subpgm->current_block, 0);
2050 subpgm->current_block = symt_check_tag(subpgm->current_block->container, SymTagBlock) ?
2051 (struct symt_block*)subpgm->current_block->container : NULL;
2054 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
2055 dwarf2_debug_info_t* di)
2057 ULONG_PTR low_pc, high_pc;
2058 struct vector* children;
2059 dwarf2_debug_info_t*child;
2060 unsigned int i;
2062 TRACE("%s\n", dwarf2_debug_di(di));
2064 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
2066 WARN("no range\n");
2067 return;
2070 subpgm->current_block = symt_open_func_block(subpgm->ctx->module_ctx->module, subpgm->func, subpgm->current_block,
2071 subpgm->ctx->module_ctx->load_offset + low_pc - subpgm->func->address,
2072 high_pc - low_pc);
2074 children = dwarf2_get_di_children(di);
2075 if (children) for (i = 0; i < vector_length(children); i++)
2077 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2079 switch (child->abbrev->tag)
2081 case DW_TAG_inlined_subroutine:
2082 dwarf2_parse_inlined_subroutine(subpgm, child);
2083 break;
2084 case DW_TAG_variable:
2085 dwarf2_parse_variable(subpgm, child);
2086 break;
2087 case DW_TAG_pointer_type:
2088 dwarf2_parse_pointer_type(child);
2089 break;
2090 case DW_TAG_subroutine_type:
2091 dwarf2_parse_subroutine_type(child);
2092 break;
2093 case DW_TAG_const_type:
2094 dwarf2_parse_const_type(child);
2095 break;
2096 case DW_TAG_lexical_block:
2097 dwarf2_parse_subprogram_block(subpgm, child);
2098 break;
2099 case DW_TAG_subprogram:
2100 /* FIXME: likely a declaration (to be checked)
2101 * skip it for now
2103 break;
2104 case DW_TAG_formal_parameter:
2105 /* FIXME: likely elements for exception handling (GCC flavor)
2106 * Skip it for now
2108 break;
2109 case DW_TAG_imported_module:
2110 /* C++ stuff to be silenced (for now) */
2111 break;
2112 case DW_TAG_GNU_call_site:
2113 /* this isn't properly supported by dbghelp interface. skip it for now */
2114 break;
2115 case DW_TAG_label:
2116 dwarf2_parse_subprogram_label(subpgm, child);
2117 break;
2118 case DW_TAG_class_type:
2119 case DW_TAG_structure_type:
2120 case DW_TAG_union_type:
2121 case DW_TAG_enumeration_type:
2122 case DW_TAG_typedef:
2123 /* the type referred to will be loaded when we need it, so skip it */
2124 break;
2125 default:
2126 FIXME("Unhandled Tag type 0x%lx at %s\n",
2127 child->abbrev->tag, dwarf2_debug_di(di));
2131 symt_close_func_block(subpgm->ctx->module_ctx->module, subpgm->func, subpgm->current_block, 0);
2132 subpgm->current_block = symt_check_tag(subpgm->current_block->container, SymTagBlock) ?
2133 (struct symt_block*)subpgm->current_block->container : NULL;
2136 static struct symt* dwarf2_parse_subprogram(dwarf2_debug_info_t* di)
2138 struct attribute name;
2139 ULONG_PTR low_pc, high_pc;
2140 struct attribute is_decl;
2141 struct attribute inline_flags;
2142 struct symt* ret_type;
2143 struct symt_function_signature* sig_type;
2144 dwarf2_subprogram_t subpgm;
2145 struct vector* children;
2146 dwarf2_debug_info_t* child;
2147 unsigned int i;
2149 if (di->symt) return di->symt;
2151 TRACE("%s\n", dwarf2_debug_di(di));
2153 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2155 WARN("No name for function... dropping function\n");
2156 return NULL;
2158 /* if it's an abstract representation of an inline function, there should be
2159 * a concrete object that we'll handle
2161 if (dwarf2_find_attribute(di, DW_AT_inline, &inline_flags) &&
2162 inline_flags.u.uvalue != DW_INL_not_inlined)
2164 TRACE("Function %s declared as inlined (%ld)... skipping\n",
2165 debugstr_a(name.u.string), inline_flags.u.uvalue);
2166 return NULL;
2169 if (dwarf2_find_attribute(di, DW_AT_declaration, &is_decl) &&
2170 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
2172 /* it's a real declaration, skip it */
2173 return NULL;
2175 if (!dwarf2_read_range(di->unit_ctx, di, &low_pc, &high_pc))
2177 WARN("cannot get range for %s\n", debugstr_a(name.u.string));
2178 return NULL;
2180 /* As functions (defined as inline assembly) get debug info with dwarf
2181 * (not the case for stabs), we just drop Wine's thunks here...
2182 * Actual thunks will be created in elf_module from the symbol table
2184 if (elf_is_in_thunk_area(di->unit_ctx->module_ctx->load_offset + low_pc, di->unit_ctx->module_ctx->thunks) >= 0)
2185 return NULL;
2186 ret_type = dwarf2_lookup_type(di);
2188 /* FIXME: assuming C source code */
2189 sig_type = symt_new_function_signature(di->unit_ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2190 subpgm.func = symt_new_function(di->unit_ctx->module_ctx->module, di->unit_ctx->compiland,
2191 dwarf2_get_cpp_name(di, name.u.string),
2192 di->unit_ctx->module_ctx->load_offset + low_pc, high_pc - low_pc,
2193 &sig_type->symt);
2194 di->symt = &subpgm.func->symt;
2195 subpgm.ctx = di->unit_ctx;
2196 if (!dwarf2_compute_location_attr(di->unit_ctx, di, DW_AT_frame_base,
2197 &subpgm.frame, NULL))
2199 /* on stack !! */
2200 subpgm.frame.kind = loc_regrel;
2201 subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
2202 subpgm.frame.offset = 0;
2204 subpgm.non_computed_variable = FALSE;
2205 subpgm.current_block = NULL;
2207 children = dwarf2_get_di_children(di);
2208 if (children) for (i = 0; i < vector_length(children); i++)
2210 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2212 switch (child->abbrev->tag)
2214 case DW_TAG_variable:
2215 case DW_TAG_formal_parameter:
2216 dwarf2_parse_variable(&subpgm, child);
2217 break;
2218 case DW_TAG_lexical_block:
2219 dwarf2_parse_subprogram_block(&subpgm, child);
2220 break;
2221 case DW_TAG_inlined_subroutine:
2222 dwarf2_parse_inlined_subroutine(&subpgm, child);
2223 break;
2224 case DW_TAG_pointer_type:
2225 dwarf2_parse_pointer_type(child);
2226 break;
2227 case DW_TAG_const_type:
2228 dwarf2_parse_const_type(child);
2229 break;
2230 case DW_TAG_subprogram:
2231 /* FIXME: likely a declaration (to be checked)
2232 * skip it for now
2234 break;
2235 case DW_TAG_label:
2236 dwarf2_parse_subprogram_label(&subpgm, child);
2237 break;
2238 case DW_TAG_class_type:
2239 case DW_TAG_structure_type:
2240 case DW_TAG_union_type:
2241 case DW_TAG_enumeration_type:
2242 case DW_TAG_typedef:
2243 /* the type referred to will be loaded when we need it, so skip it */
2244 break;
2245 case DW_TAG_unspecified_parameters:
2246 case DW_TAG_template_type_param:
2247 case DW_TAG_template_value_param:
2248 case DW_TAG_GNU_call_site:
2249 case DW_TAG_GNU_template_parameter_pack:
2250 case DW_TAG_GNU_formal_parameter_pack:
2251 /* FIXME: no support in dbghelp's internals so far */
2252 break;
2253 default:
2254 FIXME("Unhandled Tag type 0x%lx at %s\n",
2255 child->abbrev->tag, dwarf2_debug_di(di));
2259 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2261 symt_add_function_point(di->unit_ctx->module_ctx->module, subpgm.func, SymTagCustom,
2262 &subpgm.frame, NULL);
2265 return di->symt;
2268 static struct symt* dwarf2_parse_subroutine_type(dwarf2_debug_info_t* di)
2270 struct symt* ret_type;
2271 struct symt_function_signature* sig_type;
2272 struct vector* children;
2273 dwarf2_debug_info_t* child;
2274 unsigned int i;
2276 if (di->symt) return di->symt;
2278 TRACE("%s\n", dwarf2_debug_di(di));
2280 ret_type = dwarf2_lookup_type(di);
2282 /* FIXME: assuming C source code */
2283 sig_type = symt_new_function_signature(di->unit_ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2285 children = dwarf2_get_di_children(di);
2286 if (children) for (i = 0; i < vector_length(children); i++)
2288 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2290 switch (child->abbrev->tag)
2292 case DW_TAG_formal_parameter:
2293 symt_add_function_signature_parameter(di->unit_ctx->module_ctx->module, sig_type,
2294 dwarf2_lookup_type(child));
2295 break;
2296 case DW_TAG_unspecified_parameters:
2297 WARN("Unsupported unspecified parameters\n");
2298 break;
2302 return di->symt = &sig_type->symt;
2305 static void dwarf2_parse_namespace(dwarf2_debug_info_t* di)
2307 struct vector* children;
2308 dwarf2_debug_info_t* child;
2309 unsigned int i;
2311 if (di->symt) return;
2313 TRACE("%s\n", dwarf2_debug_di(di));
2315 di->symt = di->unit_ctx->module_ctx->symt_cache[sc_void];
2317 children = dwarf2_get_di_children(di);
2318 if (children) for (i = 0; i < vector_length(children); i++)
2320 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2321 dwarf2_load_one_entry(child);
2325 static void dwarf2_parse_imported_unit(dwarf2_debug_info_t* di)
2327 struct attribute imp;
2329 if (di->symt) return;
2331 TRACE("%s\n", dwarf2_debug_di(di));
2333 if (dwarf2_find_attribute(di, DW_AT_import, &imp))
2335 dwarf2_debug_info_t* jmp = dwarf2_jump_to_debug_info(&imp);
2336 if (jmp) di->symt = jmp->symt;
2337 else FIXME("Couldn't load imported CU\n");
2339 else
2340 FIXME("Couldn't find import attribute\n");
2343 static void dwarf2_load_one_entry(dwarf2_debug_info_t* di)
2345 switch (di->abbrev->tag)
2347 case DW_TAG_typedef:
2348 dwarf2_parse_typedef(di);
2349 break;
2350 case DW_TAG_base_type:
2351 dwarf2_parse_base_type(di);
2352 break;
2353 case DW_TAG_pointer_type:
2354 dwarf2_parse_pointer_type(di);
2355 break;
2356 case DW_TAG_class_type:
2357 dwarf2_parse_udt_type(di, UdtClass);
2358 break;
2359 case DW_TAG_structure_type:
2360 dwarf2_parse_udt_type(di, UdtStruct);
2361 break;
2362 case DW_TAG_union_type:
2363 dwarf2_parse_udt_type(di, UdtUnion);
2364 break;
2365 case DW_TAG_array_type:
2366 dwarf2_parse_array_type(di);
2367 break;
2368 case DW_TAG_const_type:
2369 dwarf2_parse_const_type(di);
2370 break;
2371 case DW_TAG_volatile_type:
2372 dwarf2_parse_volatile_type(di);
2373 break;
2374 case DW_TAG_restrict_type:
2375 dwarf2_parse_restrict_type(di);
2376 break;
2377 case DW_TAG_unspecified_type:
2378 dwarf2_parse_unspecified_type(di);
2379 break;
2380 case DW_TAG_reference_type:
2381 case DW_TAG_rvalue_reference_type:
2382 dwarf2_parse_reference_type(di);
2383 break;
2384 case DW_TAG_enumeration_type:
2385 dwarf2_parse_enumeration_type(di);
2386 break;
2387 case DW_TAG_subprogram:
2388 dwarf2_parse_subprogram(di);
2389 break;
2390 case DW_TAG_subroutine_type:
2391 dwarf2_parse_subroutine_type(di);
2392 break;
2393 case DW_TAG_variable:
2395 dwarf2_subprogram_t subpgm;
2397 subpgm.ctx = di->unit_ctx;
2398 subpgm.func = NULL;
2399 subpgm.current_block = NULL;
2400 subpgm.frame.kind = loc_absolute;
2401 subpgm.frame.offset = 0;
2402 subpgm.frame.reg = Wine_DW_no_register;
2403 dwarf2_parse_variable(&subpgm, di);
2405 break;
2406 case DW_TAG_namespace:
2407 dwarf2_parse_namespace(di);
2408 break;
2409 case DW_TAG_subrange_type:
2410 dwarf2_parse_subrange_type(di);
2411 break;
2412 case DW_TAG_imported_unit:
2413 dwarf2_parse_imported_unit(di);
2414 break;
2415 /* keep it silent until we need DW_OP_call_xxx support */
2416 case DW_TAG_dwarf_procedure:
2417 /* silence a couple of non-C language defines (mainly C++ but others too) */
2418 case DW_TAG_imported_module:
2419 case DW_TAG_imported_declaration:
2420 case DW_TAG_interface_type:
2421 case DW_TAG_module:
2422 case DW_TAG_ptr_to_member_type:
2423 break;
2424 default:
2425 FIXME("Unhandled Tag type 0x%lx at %s\n",
2426 di->abbrev->tag, dwarf2_debug_di(di));
2430 static void dwarf2_set_line_number(struct module* module, ULONG_PTR address,
2431 const struct vector* v, unsigned file, unsigned line)
2433 struct symt_function* func;
2434 struct symt_ht* symt;
2435 unsigned* psrc;
2437 if (!file || !(psrc = vector_at(v, file - 1))) return;
2439 TRACE("%s %lx %s %u\n",
2440 debugstr_w(module->modulename), address, debugstr_a(source_get(module, *psrc)), line);
2441 symt = symt_find_nearest(module, address);
2442 if (symt_check_tag(&symt->symt, SymTagFunction))
2444 func = (struct symt_function*)symt;
2445 symt_add_func_line(module, func, *psrc, line, address - func->address);
2449 static BOOL dwarf2_parse_line_numbers(dwarf2_parse_context_t* ctx,
2450 const char* compile_dir,
2451 ULONG_PTR offset)
2453 dwarf2_traverse_context_t traverse;
2454 ULONG_PTR length;
2455 unsigned insn_size, version, default_stmt;
2456 unsigned line_range, opcode_base;
2457 int line_base;
2458 unsigned char offset_size;
2459 const unsigned char* opcode_len;
2460 struct vector dirs;
2461 struct vector files;
2462 const char** p;
2464 /* section with line numbers stripped */
2465 if (ctx->module_ctx->sections[section_line].address == IMAGE_NO_MAP)
2466 return FALSE;
2468 if (offset + 4 > ctx->module_ctx->sections[section_line].size)
2470 WARN("out of bounds offset\n");
2471 return FALSE;
2473 traverse.data = ctx->module_ctx->sections[section_line].address + offset;
2474 traverse.end_data = ctx->module_ctx->sections[section_line].address + ctx->module_ctx->sections[section_line].size;
2476 length = dwarf2_parse_3264(&traverse, &offset_size);
2477 if (offset_size != ctx->head.offset_size)
2479 WARN("Mismatch in 32/64 bit format\n");
2480 return FALSE;
2482 traverse.end_data = traverse.data + length;
2484 if (traverse.end_data > ctx->module_ctx->sections[section_line].address + ctx->module_ctx->sections[section_line].size)
2486 WARN("out of bounds header\n");
2487 return FALSE;
2489 version = dwarf2_parse_u2(&traverse);
2490 dwarf2_parse_offset(&traverse, offset_size); /* header_len */
2491 insn_size = dwarf2_parse_byte(&traverse);
2492 if (version >= 4)
2493 dwarf2_parse_byte(&traverse); /* max_operations_per_instructions */
2494 default_stmt = dwarf2_parse_byte(&traverse);
2495 line_base = (signed char)dwarf2_parse_byte(&traverse);
2496 line_range = dwarf2_parse_byte(&traverse);
2497 opcode_base = dwarf2_parse_byte(&traverse);
2499 opcode_len = traverse.data;
2500 traverse.data += opcode_base - 1;
2502 vector_init(&dirs, sizeof(const char*), 4);
2503 p = vector_add(&dirs, &ctx->pool);
2504 *p = compile_dir ? compile_dir : ".";
2505 while (traverse.data < traverse.end_data && *traverse.data)
2507 const char* rel = (const char*)traverse.data;
2508 unsigned rellen = strlen(rel);
2509 TRACE("Got include %s\n", debugstr_a(rel));
2510 traverse.data += rellen + 1;
2511 p = vector_add(&dirs, &ctx->pool);
2513 if (*rel == '/' || !compile_dir || !*compile_dir)
2514 *p = rel;
2515 else
2517 /* include directory relative to compile directory */
2518 unsigned baselen = strlen(compile_dir);
2519 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2520 strcpy(tmp, compile_dir);
2521 if (baselen && tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2522 strcpy(&tmp[baselen], rel);
2523 *p = tmp;
2527 traverse.data++;
2529 vector_init(&files, sizeof(unsigned), 16);
2530 while (traverse.data < traverse.end_data && *traverse.data)
2532 unsigned int dir_index, mod_time;
2533 const char* name;
2534 const char* dir;
2535 unsigned* psrc;
2537 name = (const char*)traverse.data;
2538 traverse.data += strlen(name) + 1;
2539 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2540 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2541 length = dwarf2_leb128_as_unsigned(&traverse);
2542 dir = *(const char**)vector_at(&dirs, dir_index);
2543 TRACE("Got file %s/%s (%u,%lu)\n", debugstr_a(dir), debugstr_a(name), mod_time, length);
2544 psrc = vector_add(&files, &ctx->pool);
2545 *psrc = source_new(ctx->module_ctx->module, dir, name);
2547 traverse.data++;
2549 while (traverse.data < traverse.end_data && *traverse.data)
2551 ULONG_PTR address = 0;
2552 unsigned file = 1;
2553 unsigned line = 1;
2554 unsigned is_stmt = default_stmt;
2555 BOOL end_sequence = FALSE;
2556 unsigned opcode, extopcode, i;
2558 while (!end_sequence)
2560 opcode = dwarf2_parse_byte(&traverse);
2561 TRACE("Got opcode %x\n", opcode);
2563 if (opcode >= opcode_base)
2565 unsigned delta = opcode - opcode_base;
2567 address += (delta / line_range) * insn_size;
2568 line += line_base + (delta % line_range);
2569 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2571 else
2573 switch (opcode)
2575 case DW_LNS_copy:
2576 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2577 break;
2578 case DW_LNS_advance_pc:
2579 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2580 break;
2581 case DW_LNS_advance_line:
2582 line += dwarf2_leb128_as_signed(&traverse);
2583 break;
2584 case DW_LNS_set_file:
2585 file = dwarf2_leb128_as_unsigned(&traverse);
2586 break;
2587 case DW_LNS_set_column:
2588 dwarf2_leb128_as_unsigned(&traverse);
2589 break;
2590 case DW_LNS_negate_stmt:
2591 is_stmt = !is_stmt;
2592 break;
2593 case DW_LNS_set_basic_block:
2594 break;
2595 case DW_LNS_const_add_pc:
2596 address += ((255 - opcode_base) / line_range) * insn_size;
2597 break;
2598 case DW_LNS_fixed_advance_pc:
2599 address += dwarf2_parse_u2(&traverse);
2600 break;
2601 case DW_LNS_extended_op:
2602 dwarf2_leb128_as_unsigned(&traverse);
2603 extopcode = dwarf2_parse_byte(&traverse);
2604 switch (extopcode)
2606 case DW_LNE_end_sequence:
2607 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2608 end_sequence = TRUE;
2609 break;
2610 case DW_LNE_set_address:
2611 address = ctx->module_ctx->load_offset + dwarf2_parse_addr_head(&traverse, &ctx->head);
2612 break;
2613 case DW_LNE_define_file:
2614 FIXME("not handled define file %s\n", debugstr_a((char *)traverse.data));
2615 traverse.data += strlen((const char *)traverse.data) + 1;
2616 dwarf2_leb128_as_unsigned(&traverse);
2617 dwarf2_leb128_as_unsigned(&traverse);
2618 dwarf2_leb128_as_unsigned(&traverse);
2619 break;
2620 case DW_LNE_set_discriminator:
2622 unsigned descr;
2624 descr = dwarf2_leb128_as_unsigned(&traverse);
2625 WARN("not handled discriminator %x\n", descr);
2627 break;
2628 default:
2629 FIXME("Unsupported extended opcode %x\n", extopcode);
2630 break;
2632 break;
2633 default:
2634 WARN("Unsupported opcode %x\n", opcode);
2635 for (i = 0; i < opcode_len[opcode]; i++)
2636 dwarf2_leb128_as_unsigned(&traverse);
2637 break;
2642 return TRUE;
2645 unsigned dwarf2_cache_cuhead(struct dwarf2_module_info_s* module, struct symt_compiland* c, const dwarf2_cuhead_t* head)
2647 dwarf2_cuhead_t* ah;
2648 unsigned i;
2649 for (i = 0; i < module->num_cuheads; ++i)
2651 if (memcmp(module->cuheads[i], head, sizeof(*head)) == 0)
2653 c->user = module->cuheads[i];
2654 return TRUE;
2657 if (!(ah = pool_alloc(&c->container->module->pool, sizeof(*head)))) return FALSE;
2658 memcpy(ah, head, sizeof(*head));
2659 module->cuheads = realloc(module->cuheads, ++module->num_cuheads * sizeof(head));
2660 module->cuheads[module->num_cuheads - 1] = ah;
2661 c->user = ah;
2662 return TRUE;
2665 static dwarf2_parse_context_t* dwarf2_locate_cu(dwarf2_parse_module_context_t* module_ctx, ULONG_PTR ref)
2667 unsigned i;
2668 dwarf2_parse_context_t* ctx;
2669 const BYTE* where;
2670 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
2672 ctx = vector_at(&module_ctx->unit_contexts, i);
2673 where = module_ctx->sections[ctx->section].address + ref;
2674 if (where >= ctx->traverse_DIE.data && where < ctx->traverse_DIE.end_data)
2675 return ctx;
2677 FIXME("Couldn't find ref 0x%lx inside sect\n", ref);
2678 return NULL;
2681 static BOOL dwarf2_parse_compilation_unit_head(dwarf2_parse_context_t* ctx,
2682 dwarf2_traverse_context_t* mod_ctx)
2684 dwarf2_traverse_context_t abbrev_ctx;
2685 const unsigned char* comp_unit_start = mod_ctx->data;
2686 ULONG_PTR cu_length;
2687 ULONG_PTR cu_abbrev_offset;
2688 /* FIXME this is a temporary configuration while adding support for dwarf3&4 bits */
2689 static LONG max_supported_dwarf_version = 0;
2691 cu_length = dwarf2_parse_3264(mod_ctx, &ctx->head.offset_size);
2693 ctx->traverse_DIE.data = mod_ctx->data;
2694 ctx->traverse_DIE.end_data = mod_ctx->data + cu_length;
2695 mod_ctx->data += cu_length;
2696 ctx->head.version = dwarf2_parse_u2(&ctx->traverse_DIE);
2697 cu_abbrev_offset = dwarf2_parse_offset(&ctx->traverse_DIE, ctx->head.offset_size);
2698 ctx->head.word_size = dwarf2_parse_byte(&ctx->traverse_DIE);
2699 ctx->status = UNIT_ERROR;
2701 TRACE("Compilation Unit Header found at 0x%x:\n",
2702 (int)(comp_unit_start - ctx->module_ctx->sections[section_debug].address));
2703 TRACE("- length: %lu\n", cu_length);
2704 TRACE("- version: %u\n", ctx->head.version);
2705 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2706 TRACE("- word_size: %u\n", ctx->head.word_size);
2707 TRACE("- offset_size: %u\n", ctx->head.offset_size);
2709 if (ctx->head.version >= 2)
2710 ctx->module_ctx->cu_versions |= 1 << (ctx->head.version - 2);
2711 if (max_supported_dwarf_version == 0)
2713 char* env = getenv("DBGHELP_DWARF_VERSION");
2714 LONG v = env ? atol(env) : 4;
2715 max_supported_dwarf_version = (v >= 2 && v <= 4) ? v : 4;
2718 if (ctx->head.version < 2 || ctx->head.version > max_supported_dwarf_version)
2720 WARN("DWARF version %d isn't supported. Wine dbghelp only supports DWARF 2 up to %u.\n",
2721 ctx->head.version, max_supported_dwarf_version);
2722 return FALSE;
2725 pool_init(&ctx->pool, 65536);
2726 ctx->section = section_debug;
2727 ctx->ref_offset = comp_unit_start - ctx->module_ctx->sections[section_debug].address;
2728 ctx->cpp_name = NULL;
2729 ctx->status = UNIT_NOTLOADED;
2731 abbrev_ctx.data = ctx->module_ctx->sections[section_abbrev].address + cu_abbrev_offset;
2732 abbrev_ctx.end_data = ctx->module_ctx->sections[section_abbrev].address + ctx->module_ctx->sections[section_abbrev].size;
2733 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx->abbrev_table, &ctx->pool);
2735 sparse_array_init(&ctx->debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2736 return TRUE;
2739 static BOOL dwarf2_parse_compilation_unit(dwarf2_parse_context_t* ctx)
2741 dwarf2_debug_info_t* di;
2742 dwarf2_traverse_context_t cu_ctx = ctx->traverse_DIE;
2743 BOOL ret = FALSE;
2745 switch (ctx->status)
2747 case UNIT_ERROR: return FALSE;
2748 case UNIT_BEINGLOADED:
2749 FIXME("Circular deps on CU\n");
2750 /* fall through */
2751 case UNIT_LOADED:
2752 case UNIT_LOADED_FAIL:
2753 return TRUE;
2754 case UNIT_NOTLOADED: break;
2757 ctx->status = UNIT_BEINGLOADED;
2758 if (dwarf2_read_one_debug_info(ctx, &cu_ctx, NULL, &di))
2760 if (di->abbrev->tag == DW_TAG_compile_unit || di->abbrev->tag == DW_TAG_partial_unit)
2762 struct attribute name;
2763 struct vector* children;
2764 dwarf2_debug_info_t* child = NULL;
2765 unsigned int i;
2766 struct attribute stmt_list, low_pc;
2767 struct attribute comp_dir;
2769 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2770 name.u.string = NULL;
2772 /* get working directory of current compilation unit */
2773 if (!dwarf2_find_attribute(di, DW_AT_comp_dir, &comp_dir))
2774 comp_dir.u.string = NULL;
2776 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc))
2777 low_pc.u.uvalue = 0;
2778 ctx->compiland = symt_new_compiland(ctx->module_ctx->module, ctx->module_ctx->load_offset + low_pc.u.uvalue,
2779 source_new(ctx->module_ctx->module, comp_dir.u.string, name.u.string));
2780 dwarf2_cache_cuhead(ctx->module_ctx->module->format_info[DFI_DWARF]->u.dwarf2_info, ctx->compiland, &ctx->head);
2781 di->symt = &ctx->compiland->symt;
2782 children = dwarf2_get_di_children(di);
2783 if (children) for (i = 0; i < vector_length(children); i++)
2785 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2786 dwarf2_load_one_entry(child);
2788 if (dwarf2_find_attribute(di, DW_AT_stmt_list, &stmt_list))
2790 if (dwarf2_parse_line_numbers(ctx, comp_dir.u.string, stmt_list.u.uvalue))
2791 ctx->module_ctx->module->module.LineNumbers = TRUE;
2793 ctx->status = UNIT_LOADED;
2794 ret = TRUE;
2796 else FIXME("Should have a compilation unit here %lu\n", di->abbrev->tag);
2798 if (ctx->status == UNIT_BEINGLOADED) ctx->status = UNIT_LOADED_FAIL;
2799 return ret;
2802 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const dwarf2_cuhead_t* head,
2803 const BYTE* start, ULONG_PTR ip, dwarf2_traverse_context_t* lctx)
2805 DWORD_PTR beg, end;
2806 const BYTE* ptr = start;
2807 DWORD len;
2809 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2811 beg = dwarf2_get_addr(ptr, head->word_size); ptr += head->word_size;
2812 end = dwarf2_get_addr(ptr, head->word_size); ptr += head->word_size;
2813 if (!beg && !end) break;
2814 len = dwarf2_get_u2(ptr); ptr += 2;
2816 if (beg <= ip && ip < end)
2818 lctx->data = ptr;
2819 lctx->end_data = ptr + len;
2820 return TRUE;
2822 ptr += len;
2824 WARN("Couldn't find ip in location list\n");
2825 return FALSE;
2828 static const dwarf2_cuhead_t* get_cuhead_from_func(const struct symt_function* func)
2830 if (func && symt_check_tag(func->container, SymTagCompiland))
2832 struct symt_compiland* c = (struct symt_compiland*)func->container;
2833 return (const dwarf2_cuhead_t*)c->user;
2835 FIXME("Should have a compilation unit head\n");
2836 return NULL;
2839 static BOOL compute_call_frame_cfa(struct module* module, ULONG_PTR ip, struct location* frame);
2841 static enum location_error loc_compute_frame(struct process* pcs,
2842 const struct module_format* modfmt,
2843 const struct symt_function* func,
2844 DWORD_PTR ip, const dwarf2_cuhead_t* head,
2845 struct location* frame)
2847 struct symt** psym = NULL;
2848 struct location* pframe;
2849 dwarf2_traverse_context_t lctx;
2850 enum location_error err;
2851 unsigned int i;
2853 for (i=0; i<vector_length(&func->vchildren); i++)
2855 psym = vector_at(&func->vchildren, i);
2856 if (psym && symt_check_tag(*psym, SymTagCustom))
2858 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2860 /* First, recompute the frame information, if needed */
2861 switch (pframe->kind)
2863 case loc_regrel:
2864 case loc_register:
2865 *frame = *pframe;
2866 break;
2867 case loc_dwarf2_location_list:
2868 WARN("Searching loclist for %s\n", debugstr_a(func->hash_elt.name));
2869 if (!dwarf2_lookup_loclist(modfmt, head,
2870 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2871 ip, &lctx))
2872 return loc_err_out_of_scope;
2873 if ((err = compute_location(modfmt->module, head,
2874 &lctx, frame, pcs->handle, NULL)) < 0) return err;
2875 if (frame->kind >= loc_user)
2877 WARN("Couldn't compute runtime frame location\n");
2878 return loc_err_too_complex;
2880 break;
2881 case loc_dwarf2_frame_cfa:
2882 if (!compute_call_frame_cfa(modfmt->module, ip + ((struct symt_compiland*)func->container)->address, frame)) return loc_err_internal;
2883 break;
2884 default:
2885 WARN("Unsupported frame kind %d\n", pframe->kind);
2886 return loc_err_internal;
2888 return 0;
2891 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2892 return loc_err_internal;
2895 enum reg_rule
2897 RULE_UNSET, /* not set at all */
2898 RULE_UNDEFINED, /* undefined value */
2899 RULE_SAME, /* same value as previous frame */
2900 RULE_CFA_OFFSET, /* stored at cfa offset */
2901 RULE_OTHER_REG, /* stored in other register */
2902 RULE_EXPRESSION, /* address specified by expression */
2903 RULE_VAL_EXPRESSION /* value specified by expression */
2906 /* make it large enough for all CPUs */
2907 #define NB_FRAME_REGS 64
2908 #define MAX_SAVED_STATES 16
2910 struct frame_state
2912 ULONG_PTR cfa_offset;
2913 unsigned char cfa_reg;
2914 enum reg_rule cfa_rule;
2915 enum reg_rule rules[NB_FRAME_REGS];
2916 ULONG_PTR regs[NB_FRAME_REGS];
2919 struct frame_info
2921 ULONG_PTR ip;
2922 ULONG_PTR code_align;
2923 LONG_PTR data_align;
2924 unsigned char retaddr_reg;
2925 unsigned char fde_encoding;
2926 unsigned char lsda_encoding;
2927 unsigned char signal_frame;
2928 unsigned char aug_z_format;
2929 unsigned char state_sp;
2930 struct frame_state state;
2931 struct frame_state state_stack[MAX_SAVED_STATES];
2934 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding, unsigned char word_size)
2936 ULONG_PTR base;
2938 if (encoding == DW_EH_PE_omit) return 0;
2940 switch (encoding & 0xf0)
2942 case DW_EH_PE_abs:
2943 base = 0;
2944 break;
2945 case DW_EH_PE_pcrel:
2946 base = (ULONG_PTR)ctx->data;
2947 break;
2948 default:
2949 FIXME("unsupported encoding %02x\n", encoding);
2950 return 0;
2953 switch (encoding & 0x0f)
2955 case DW_EH_PE_native:
2956 return base + dwarf2_parse_addr(ctx, word_size);
2957 case DW_EH_PE_leb128:
2958 return base + dwarf2_leb128_as_unsigned(ctx);
2959 case DW_EH_PE_data2:
2960 return base + dwarf2_parse_u2(ctx);
2961 case DW_EH_PE_data4:
2962 return base + dwarf2_parse_u4(ctx);
2963 case DW_EH_PE_data8:
2964 return base + dwarf2_parse_u8(ctx);
2965 case DW_EH_PE_signed|DW_EH_PE_leb128:
2966 return base + dwarf2_leb128_as_signed(ctx);
2967 case DW_EH_PE_signed|DW_EH_PE_data2:
2968 return base + (signed short)dwarf2_parse_u2(ctx);
2969 case DW_EH_PE_signed|DW_EH_PE_data4:
2970 return base + (signed int)dwarf2_parse_u4(ctx);
2971 case DW_EH_PE_signed|DW_EH_PE_data8:
2972 return base + (LONG64)dwarf2_parse_u8(ctx);
2973 default:
2974 FIXME("unsupported encoding %02x\n", encoding);
2975 return 0;
2979 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info, unsigned char word_size)
2981 unsigned char version;
2982 const char* augmentation;
2983 const unsigned char* end;
2984 ULONG_PTR len;
2986 memset(info, 0, sizeof(*info));
2987 info->lsda_encoding = DW_EH_PE_omit;
2988 info->aug_z_format = 0;
2990 /* parse the CIE first */
2991 version = dwarf2_parse_byte(ctx);
2992 if (version != 1 && version != 3 && version != 4)
2994 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2995 return FALSE;
2997 augmentation = (const char*)ctx->data;
2998 ctx->data += strlen(augmentation) + 1;
3000 switch (version)
3002 case 4:
3003 /* skip 'address_size' and 'segment_size' */
3004 ctx->data += 2;
3005 /* fallthrough */
3006 case 1:
3007 case 3:
3008 info->code_align = dwarf2_leb128_as_unsigned(ctx);
3009 info->data_align = dwarf2_leb128_as_signed(ctx);
3010 info->retaddr_reg = version == 1 ? dwarf2_parse_byte(ctx) :dwarf2_leb128_as_unsigned(ctx);
3011 break;
3012 default:
3015 info->state.cfa_rule = RULE_CFA_OFFSET;
3017 end = NULL;
3018 TRACE("\tparsing augmentation %s\n", debugstr_a(augmentation));
3019 if (*augmentation) do
3021 switch (*augmentation)
3023 case 'z':
3024 len = dwarf2_leb128_as_unsigned(ctx);
3025 end = ctx->data + len;
3026 info->aug_z_format = 1;
3027 continue;
3028 case 'L':
3029 info->lsda_encoding = dwarf2_parse_byte(ctx);
3030 continue;
3031 case 'P':
3033 unsigned char encoding = dwarf2_parse_byte(ctx);
3034 /* throw away the indirect bit, as we don't care for the result */
3035 encoding &= ~DW_EH_PE_indirect;
3036 dwarf2_parse_augmentation_ptr(ctx, encoding, word_size); /* handler */
3037 continue;
3039 case 'R':
3040 info->fde_encoding = dwarf2_parse_byte(ctx);
3041 continue;
3042 case 'S':
3043 info->signal_frame = 1;
3044 continue;
3046 FIXME("unknown augmentation '%c'\n", *augmentation);
3047 if (!end) return FALSE;
3048 break;
3049 } while (*++augmentation);
3050 if (end) ctx->data = end;
3051 return TRUE;
3054 static BOOL dwarf2_get_cie(ULONG_PTR addr, struct module* module, DWORD_PTR delta,
3055 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
3056 struct frame_info* info, BOOL in_eh_frame)
3058 const unsigned char* ptr_blk;
3059 const unsigned char* cie_ptr;
3060 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
3061 ULONG_PTR len, id;
3062 ULONG_PTR start, range;
3063 ULONG_PTR cie_id;
3064 const BYTE* start_data = fde_ctx->data;
3065 unsigned char word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3066 unsigned char offset_size;
3068 /* skip 0-padding at beginning of section (alignment) */
3069 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
3071 if (dwarf2_parse_u4(fde_ctx))
3073 fde_ctx->data -= 4;
3074 break;
3077 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
3079 const unsigned char* st = fde_ctx->data;
3080 /* find the FDE for address addr (skip CIE) */
3081 len = dwarf2_parse_3264(fde_ctx, &offset_size);
3082 cie_id = in_eh_frame ? 0 : (offset_size == 4 ? DW_CIE_ID : (ULONG_PTR)DW64_CIE_ID);
3083 ptr_blk = fde_ctx->data + len;
3084 id = dwarf2_parse_offset(fde_ctx, offset_size);
3085 if (id == cie_id)
3087 last_cie_ptr = st;
3088 /* we need some bits out of the CIE in order to parse all contents */
3089 if (!parse_cie_details(fde_ctx, info, word_size)) return FALSE;
3090 cie_ctx->data = fde_ctx->data;
3091 cie_ctx->end_data = ptr_blk;
3092 continue;
3094 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
3095 if (cie_ptr != last_cie_ptr)
3097 last_cie_ptr = cie_ptr;
3098 cie_ctx->data = cie_ptr;
3099 cie_ctx->end_data = cie_ptr + (offset_size == 4 ? 4 : 4 + 8);
3100 cie_ctx->end_data += dwarf2_parse_3264(cie_ctx, &offset_size);
3102 if (dwarf2_parse_offset(cie_ctx, in_eh_frame ? word_size : offset_size) != cie_id)
3104 FIXME("wrong CIE pointer at %x from FDE %x\n",
3105 (unsigned)(cie_ptr - start_data),
3106 (unsigned)(fde_ctx->data - start_data));
3107 return FALSE;
3109 if (!parse_cie_details(cie_ctx, info, word_size)) return FALSE;
3111 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding, word_size);
3112 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F, word_size);
3114 if (addr >= start && addr < start + range)
3116 /* reset the FDE context */
3117 fde_ctx->end_data = ptr_blk;
3119 info->ip = start;
3120 return TRUE;
3123 return FALSE;
3126 static int valid_reg(ULONG_PTR reg)
3128 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
3129 return (reg < NB_FRAME_REGS);
3132 static void execute_cfa_instructions(struct module* module, dwarf2_traverse_context_t* ctx,
3133 ULONG_PTR last_ip, struct frame_info *info)
3135 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
3137 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
3139 if (op & 0xc0)
3141 switch (op & 0xc0)
3143 case DW_CFA_advance_loc:
3145 ULONG_PTR offset = (op & 0x3f) * info->code_align;
3146 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
3147 info->ip += offset;
3148 break;
3150 case DW_CFA_offset:
3152 ULONG_PTR reg = op & 0x3f;
3153 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
3154 if (!valid_reg(reg)) break;
3155 TRACE("%lx: DW_CFA_offset %s, %ld\n",
3156 info->ip,
3157 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
3158 offset);
3159 info->state.regs[reg] = offset;
3160 info->state.rules[reg] = RULE_CFA_OFFSET;
3161 break;
3163 case DW_CFA_restore:
3165 ULONG_PTR reg = op & 0x3f;
3166 if (!valid_reg(reg)) break;
3167 TRACE("%lx: DW_CFA_restore %s\n",
3168 info->ip,
3169 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
3170 info->state.rules[reg] = RULE_UNSET;
3171 break;
3175 else switch (op)
3177 case DW_CFA_nop:
3178 break;
3179 case DW_CFA_set_loc:
3181 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding,
3182 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
3183 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
3184 info->ip = loc;
3185 break;
3187 case DW_CFA_advance_loc1:
3189 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
3190 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
3191 info->ip += offset;
3192 break;
3194 case DW_CFA_advance_loc2:
3196 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
3197 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
3198 info->ip += offset;
3199 break;
3201 case DW_CFA_advance_loc4:
3203 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
3204 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
3205 info->ip += offset;
3206 break;
3208 case DW_CFA_offset_extended:
3209 case DW_CFA_offset_extended_sf:
3211 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3212 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
3213 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3214 if (!valid_reg(reg)) break;
3215 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
3216 info->ip,
3217 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
3218 offset);
3219 info->state.regs[reg] = offset;
3220 info->state.rules[reg] = RULE_CFA_OFFSET;
3221 break;
3223 case DW_CFA_restore_extended:
3225 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3226 if (!valid_reg(reg)) break;
3227 TRACE("%lx: DW_CFA_restore_extended %s\n",
3228 info->ip,
3229 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
3230 info->state.rules[reg] = RULE_UNSET;
3231 break;
3233 case DW_CFA_undefined:
3235 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3236 if (!valid_reg(reg)) break;
3237 TRACE("%lx: DW_CFA_undefined %s\n",
3238 info->ip,
3239 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
3240 info->state.rules[reg] = RULE_UNDEFINED;
3241 break;
3243 case DW_CFA_same_value:
3245 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3246 if (!valid_reg(reg)) break;
3247 TRACE("%lx: DW_CFA_same_value %s\n",
3248 info->ip,
3249 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
3250 info->state.regs[reg] = reg;
3251 info->state.rules[reg] = RULE_SAME;
3252 break;
3254 case DW_CFA_register:
3256 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3257 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
3258 if (!valid_reg(reg) || !valid_reg(reg2)) break;
3259 TRACE("%lx: DW_CFA_register %s == %s\n",
3260 info->ip,
3261 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
3262 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2, module, TRUE)));
3263 info->state.regs[reg] = reg2;
3264 info->state.rules[reg] = RULE_OTHER_REG;
3265 break;
3267 case DW_CFA_remember_state:
3268 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
3269 if (info->state_sp >= MAX_SAVED_STATES)
3270 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
3271 else
3272 info->state_stack[info->state_sp++] = info->state;
3273 break;
3274 case DW_CFA_restore_state:
3275 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
3276 if (!info->state_sp)
3277 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
3278 else
3279 info->state = info->state_stack[--info->state_sp];
3280 break;
3281 case DW_CFA_def_cfa:
3282 case DW_CFA_def_cfa_sf:
3284 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3285 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
3286 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3287 if (!valid_reg(reg)) break;
3288 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
3289 info->ip,
3290 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
3291 offset);
3292 info->state.cfa_reg = reg;
3293 info->state.cfa_offset = offset;
3294 info->state.cfa_rule = RULE_CFA_OFFSET;
3295 break;
3297 case DW_CFA_def_cfa_register:
3299 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3300 if (!valid_reg(reg)) break;
3301 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
3302 info->ip,
3303 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)));
3304 info->state.cfa_reg = reg;
3305 info->state.cfa_rule = RULE_CFA_OFFSET;
3306 break;
3308 case DW_CFA_def_cfa_offset:
3309 case DW_CFA_def_cfa_offset_sf:
3311 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
3312 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3313 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
3314 info->state.cfa_offset = offset;
3315 info->state.cfa_rule = RULE_CFA_OFFSET;
3316 break;
3318 case DW_CFA_def_cfa_expression:
3320 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3321 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3322 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
3323 info->state.cfa_offset = expr;
3324 info->state.cfa_rule = RULE_VAL_EXPRESSION;
3325 ctx->data += len;
3326 break;
3328 case DW_CFA_expression:
3329 case DW_CFA_val_expression:
3331 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3332 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3333 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3334 if (!valid_reg(reg)) break;
3335 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
3336 info->ip, (op == DW_CFA_expression) ? "" : "val_",
3337 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, module, TRUE)),
3338 expr, expr + len);
3339 info->state.regs[reg] = expr;
3340 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
3341 ctx->data += len;
3342 break;
3344 case DW_CFA_GNU_args_size:
3345 /* FIXME: should check that GCC is the compiler for this CU */
3347 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
3348 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
3349 /* ignored */
3350 break;
3352 default:
3353 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
3354 break;
3359 /* retrieve a context register from its dwarf number */
3360 static DWORD64 get_context_reg(const struct module* module, struct cpu_stack_walk *csw, union ctx *context,
3361 ULONG_PTR dw_reg)
3363 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3364 void* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3366 if (sz == 8)
3367 return *(DWORD64 *)ptr;
3368 else if (sz == 4)
3369 return *(DWORD *)ptr;
3371 FIXME("unhandled size %d\n", sz);
3372 return 0;
3375 /* set a context register from its dwarf number */
3376 static void set_context_reg(const struct module* module, struct cpu_stack_walk* csw, union ctx *context,
3377 ULONG_PTR dw_reg, ULONG_PTR val, BOOL isdebuggee)
3379 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3380 ULONG_PTR* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3382 if (isdebuggee)
3384 char tmp[16];
3386 if (sz > sizeof(tmp))
3388 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
3389 return;
3391 if (!sw_read_mem(csw, val, tmp, sz))
3393 WARN("Couldn't read memory at %p\n", (void*)val);
3394 return;
3396 memcpy(ptr, tmp, sz);
3398 else
3400 if (sz != sizeof(ULONG_PTR))
3402 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
3403 return;
3405 *ptr = val;
3409 /* copy a register from one context to another using dwarf number */
3410 static void copy_context_reg(const struct module* module, struct cpu_stack_walk *csw,
3411 union ctx *dstcontext, ULONG_PTR dwregdst,
3412 union ctx *srccontext, ULONG_PTR dwregsrc)
3414 unsigned regdstno = csw->cpu->map_dwarf_register(dwregdst, module, TRUE), szdst;
3415 unsigned regsrcno = csw->cpu->map_dwarf_register(dwregsrc, module, TRUE), szsrc;
3416 ULONG_PTR* ptrdst = csw->cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3417 ULONG_PTR* ptrsrc = csw->cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3419 if (szdst != szsrc)
3421 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3422 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3423 return;
3425 memcpy(ptrdst, ptrsrc, szdst);
3428 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3429 const unsigned char* zp, union ctx *context)
3431 dwarf2_traverse_context_t ctx;
3432 ULONG_PTR reg, sz, tmp;
3433 DWORD64 stack[64];
3434 int sp = -1;
3435 ULONG_PTR len;
3437 ctx.data = zp;
3438 ctx.end_data = zp + 4;
3439 len = dwarf2_leb128_as_unsigned(&ctx);
3440 ctx.end_data = ctx.data + len;
3442 while (ctx.data < ctx.end_data)
3444 unsigned char opcode = dwarf2_parse_byte(&ctx);
3446 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3447 stack[++sp] = opcode - DW_OP_lit0;
3448 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3449 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_reg0);
3450 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3451 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_breg0)
3452 + dwarf2_leb128_as_signed(&ctx);
3453 else switch (opcode)
3455 case DW_OP_nop: break;
3456 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size); break;
3457 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3458 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3459 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3460 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3461 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3462 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3463 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3464 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3465 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3466 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3467 case DW_OP_deref:
3468 tmp = 0;
3469 if (!sw_read_mem(csw, stack[sp], &tmp, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size))
3471 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3472 tmp = 0;
3474 stack[sp] = tmp;
3475 break;
3476 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3477 case DW_OP_drop: sp--; break;
3478 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3479 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3480 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3481 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3482 case DW_OP_abs: stack[sp] = sizeof(stack[sp]) == 8 ? llabs((INT64)stack[sp]) : abs((INT32)stack[sp]); break;
3483 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3484 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3485 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3486 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3487 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3488 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3489 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3490 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3491 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3492 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3493 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3494 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3495 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3496 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3497 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3498 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3499 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3500 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3501 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3502 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3503 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3504 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3505 case DW_OP_GNU_encoded_addr:
3506 tmp = dwarf2_parse_byte(&ctx);
3507 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
3508 break;
3509 case DW_OP_regx:
3510 stack[++sp] = get_context_reg(module, csw, context, dwarf2_leb128_as_unsigned(&ctx));
3511 break;
3512 case DW_OP_bregx:
3513 reg = dwarf2_leb128_as_unsigned(&ctx);
3514 tmp = dwarf2_leb128_as_signed(&ctx);
3515 stack[++sp] = get_context_reg(module, csw, context, reg) + tmp;
3516 break;
3517 case DW_OP_deref_size:
3518 sz = dwarf2_parse_byte(&ctx);
3519 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3521 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3522 tmp = 0;
3524 /* do integral promotion */
3525 switch (sz)
3527 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3528 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3529 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3530 case 8: stack[sp] = tmp; break; /* FIXME: won't work on 32bit platform */
3531 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3533 break;
3534 default:
3535 FIXME("unhandled opcode %02x\n", opcode);
3538 return stack[sp];
3541 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3542 union ctx *context, struct frame_state *state, DWORD64 *cfa)
3544 unsigned int i;
3545 ULONG_PTR value;
3546 union ctx new_context = *context;
3548 switch (state->cfa_rule)
3550 case RULE_EXPRESSION:
3551 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3552 if (!sw_read_mem(csw, *cfa, cfa, csw->cpu->word_size))
3554 WARN("Couldn't read memory at %s\n", wine_dbgstr_longlong(*cfa));
3555 return;
3557 break;
3558 case RULE_VAL_EXPRESSION:
3559 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3560 break;
3561 default:
3562 *cfa = get_context_reg(module, csw, context, state->cfa_reg) + state->cfa_offset;
3563 break;
3565 if (!*cfa) return;
3567 for (i = 0; i < NB_FRAME_REGS; i++)
3569 switch (state->rules[i])
3571 case RULE_UNSET:
3572 case RULE_UNDEFINED:
3573 case RULE_SAME:
3574 break;
3575 case RULE_CFA_OFFSET:
3576 set_context_reg(module, csw, &new_context, i, *cfa + state->regs[i], TRUE);
3577 break;
3578 case RULE_OTHER_REG:
3579 copy_context_reg(module, csw, &new_context, i, context, state->regs[i]);
3580 break;
3581 case RULE_EXPRESSION:
3582 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3583 set_context_reg(module, csw, &new_context, i, value, TRUE);
3584 break;
3585 case RULE_VAL_EXPRESSION:
3586 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3587 set_context_reg(module, csw, &new_context, i, value, FALSE);
3588 break;
3591 *context = new_context;
3594 static BOOL dwarf2_fetch_frame_info(struct module* module, struct cpu* cpu, LONG_PTR ip, struct frame_info* info)
3596 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3597 struct module_format* modfmt;
3598 const unsigned char* end;
3599 DWORD_PTR delta;
3601 modfmt = module->format_info[DFI_DWARF];
3602 if (!modfmt) return FALSE;
3603 memset(info, 0, sizeof(*info));
3604 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3605 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3606 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3607 * in this process the IMAGE section at a different address as the one expected by
3608 * the image
3610 delta = module->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3611 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3612 if (!dwarf2_get_cie(ip, module, delta, &fde_ctx, &cie_ctx, info, TRUE))
3614 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3615 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3616 delta = module->reloc_delta;
3617 if (!dwarf2_get_cie(ip, module, delta, &fde_ctx, &cie_ctx, info, FALSE))
3619 TRACE("Couldn't find information for %lx\n", ip);
3620 return FALSE;
3624 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3625 ip, info->ip, info->code_align, info->data_align,
3626 cpu->fetch_regname(cpu->map_dwarf_register(info->retaddr_reg, module, TRUE)));
3628 if (ip != info->ip)
3630 execute_cfa_instructions(module, &cie_ctx, ip, info);
3632 if (info->aug_z_format) /* get length of augmentation data */
3634 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3635 end = fde_ctx.data + len;
3637 else end = NULL;
3638 dwarf2_parse_augmentation_ptr(&fde_ctx, info->lsda_encoding, modfmt->u.dwarf2_info->word_size); /* handler_data */
3639 if (end) fde_ctx.data = end;
3641 execute_cfa_instructions(module, &fde_ctx, ip, info);
3643 return TRUE;
3646 /***********************************************************************
3647 * dwarf2_virtual_unwind
3650 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk *csw, ULONG_PTR ip,
3651 union ctx *context, DWORD64 *cfa)
3653 struct module_pair pair;
3654 struct frame_info info;
3656 if (!module_init_pair(&pair, csw->hProcess, ip)) return FALSE;
3657 if (!dwarf2_fetch_frame_info(pair.effective, csw->cpu, ip, &info)) return FALSE;
3659 /* if at very beginning of function, return and use default unwinder */
3660 if (ip == info.ip) return FALSE;
3662 /* if there is no information about retaddr, use default unwinder */
3663 if (info.state.rules[info.retaddr_reg] == RULE_UNSET) return FALSE;
3665 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3667 return TRUE;
3670 static BOOL compute_call_frame_cfa(struct module* module, ULONG_PTR ip, struct location* frame)
3672 struct frame_info info;
3674 if (!dwarf2_fetch_frame_info(module, dbghelp_current_cpu, ip, &info)) return FALSE;
3676 /* beginning of function, or no available dwarf information ? */
3677 if (ip == info.ip || info.state.rules[info.retaddr_reg] == RULE_UNSET)
3679 /* fake the default unwinder */
3680 frame->kind = loc_regrel;
3681 frame->reg = dbghelp_current_cpu->frame_regno;
3682 frame->offset = dbghelp_current_cpu->word_size; /* FIXME stack direction */
3684 else
3686 /* we expect to translate the call_frame_cfa into a regrel location...
3687 * that should cover most of the cases
3689 switch (info.state.cfa_rule)
3691 case RULE_EXPRESSION:
3692 FIXME("Too complex expression for frame_CFA resolution (RULE_EXPRESSION)\n");
3693 return FALSE;
3694 case RULE_VAL_EXPRESSION:
3695 FIXME("Too complex expression for frame_CFA resolution (RULE_VAL_EXPRESSION)\n");
3696 return FALSE;
3697 default:
3698 frame->kind = loc_regrel;
3699 frame->reg = dbghelp_current_cpu->map_dwarf_register(info.state.cfa_reg, module, TRUE);
3700 frame->offset = info.state.cfa_offset;
3701 break;
3704 return TRUE;
3707 static void dwarf2_location_compute(struct process* pcs,
3708 const struct module_format* modfmt,
3709 const struct symt_function* func,
3710 struct location* loc)
3712 struct location frame;
3713 DWORD_PTR ip;
3714 int err;
3715 dwarf2_traverse_context_t lctx;
3716 const dwarf2_cuhead_t* head = get_cuhead_from_func(func);
3718 if (!head)
3720 WARN("We'd expect function %s's container to be a valid compiland with dwarf inforamation\n",
3721 debugstr_a(func->hash_elt.name));
3722 err = loc_err_internal;
3724 else
3726 /* instruction pointer relative to compiland's start */
3727 ip = pcs->localscope_pc - ((struct symt_compiland*)func->container)->address;
3729 if ((err = loc_compute_frame(pcs, modfmt, func, ip, head, &frame)) == 0)
3731 switch (loc->kind)
3733 case loc_dwarf2_location_list:
3734 /* Then, if the variable has a location list, find it !! */
3735 if (dwarf2_lookup_loclist(modfmt, head,
3736 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3737 ip, &lctx))
3738 goto do_compute;
3739 err = loc_err_out_of_scope;
3740 break;
3741 case loc_dwarf2_block:
3742 /* or if we have a copy of an existing block, get ready for it */
3744 unsigned* ptr = (unsigned*)loc->offset;
3746 lctx.data = (const BYTE*)(ptr + 1);
3747 lctx.end_data = lctx.data + *ptr;
3749 do_compute:
3750 /* now get the variable */
3751 err = compute_location(modfmt->module, head,
3752 &lctx, loc, pcs->handle, &frame);
3753 break;
3754 case loc_register:
3755 case loc_regrel:
3756 /* nothing to do */
3757 break;
3758 default:
3759 WARN("Unsupported local kind %d\n", loc->kind);
3760 err = loc_err_internal;
3764 if (err < 0)
3766 loc->kind = loc_register;
3767 loc->reg = err;
3771 static void *zalloc(void *priv, uInt items, uInt sz)
3773 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3776 static void zfree(void *priv, void *addr)
3778 HeapFree(GetProcessHeap(), 0, addr);
3781 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3782 const char* zsectname,
3783 struct image_section_map* ism)
3785 z_stream z;
3786 LARGE_INTEGER li;
3787 int res;
3788 BOOL ret = FALSE;
3790 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3791 size_t sz = image_get_map_size(ism);
3793 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
3795 ERR("invalid compressed section %s\n", debugstr_a(zsectname));
3796 goto out;
3799 #ifdef WORDS_BIGENDIAN
3800 li.u.HighPart = *(DWORD*)&sect[4];
3801 li.u.LowPart = *(DWORD*)&sect[8];
3802 #else
3803 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
3804 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
3805 #endif
3807 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
3808 if (!addr)
3809 goto out;
3811 z.next_in = &sect[12];
3812 z.avail_in = sz - 12;
3813 z.opaque = NULL;
3814 z.zalloc = zalloc;
3815 z.zfree = zfree;
3817 res = inflateInit(&z);
3818 if (res != Z_OK)
3820 FIXME("inflateInit failed with %i / %s\n", res, debugstr_a(z.msg));
3821 goto out_free;
3824 do {
3825 z.next_out = addr + z.total_out;
3826 z.avail_out = li.QuadPart - z.total_out;
3827 res = inflate(&z, Z_FINISH);
3828 } while (z.avail_in && res == Z_STREAM_END);
3830 if (res != Z_STREAM_END)
3832 FIXME("Decompression failed with %i / %s\n", res, debugstr_a(z.msg));
3833 goto out_end;
3836 ret = TRUE;
3837 section->compressed = TRUE;
3838 section->address = addr;
3839 section->rva = image_get_map_rva(ism);
3840 section->size = z.total_out;
3842 out_end:
3843 inflateEnd(&z);
3844 out_free:
3845 if (!ret)
3846 HeapFree(GetProcessHeap(), 0, addr);
3847 out:
3848 image_unmap_section(ism);
3849 return ret;
3852 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3853 const char* sectname, const char* zsectname,
3854 struct image_section_map* ism)
3856 struct image_section_map local_ism;
3858 if (!ism) ism = &local_ism;
3860 section->compressed = FALSE;
3861 if (image_find_section(fmap, sectname, ism))
3863 section->address = (const BYTE*)image_map_section(ism);
3864 section->size = image_get_map_size(ism);
3865 section->rva = image_get_map_rva(ism);
3866 return TRUE;
3869 section->address = NULL;
3870 section->size = 0;
3871 section->rva = 0;
3873 if (zsectname && image_find_section(fmap, zsectname, ism))
3875 return dwarf2_init_zsection(section, zsectname, ism);
3878 return FALSE;
3881 static inline void dwarf2_fini_section(dwarf2_section_t* section)
3883 if (section->compressed)
3884 HeapFree(GetProcessHeap(), 0, (void*)section->address);
3887 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3889 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
3890 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
3891 free(modfmt->u.dwarf2_info->cuheads);
3892 HeapFree(GetProcessHeap(), 0, modfmt);
3895 static BOOL dwarf2_load_CU_module(dwarf2_parse_module_context_t* module_ctx, struct module* module,
3896 dwarf2_section_t* sections, ULONG_PTR load_offset,
3897 const struct elf_thunk_area* thunks, BOOL is_dwz)
3899 dwarf2_traverse_context_t mod_ctx;
3900 unsigned i;
3902 module_ctx->sections = sections;
3903 module_ctx->module = module;
3904 module_ctx->thunks = thunks;
3905 module_ctx->load_offset = load_offset;
3906 memset(module_ctx->symt_cache, 0, sizeof(module_ctx->symt_cache));
3907 module_ctx->symt_cache[sc_void] = &symt_new_basic(module_ctx->module, btVoid, "void", 0)->symt;
3908 module_ctx->symt_cache[sc_unknown] = &symt_new_basic(module_ctx->module, btNoType, "# unknown", 0)->symt;
3909 vector_init(&module_ctx->unit_contexts, sizeof(dwarf2_parse_context_t), 16);
3910 module_ctx->cu_versions = 0;
3912 /* phase I: parse all CU heads */
3913 mod_ctx.data = sections[section_debug].address;
3914 mod_ctx.end_data = mod_ctx.data + sections[section_debug].size;
3915 while (mod_ctx.data < mod_ctx.end_data)
3917 dwarf2_parse_context_t* unit_ctx = vector_add(&module_ctx->unit_contexts, &module_ctx->module->pool);
3919 unit_ctx->module_ctx = module_ctx;
3920 dwarf2_parse_compilation_unit_head(unit_ctx, &mod_ctx);
3923 /* phase2: load content of all CU
3924 * If this is a DWZ alternate module, don't load all debug_info at once
3925 * wait for main module to ask for them (it's likely it won't need them all)
3926 * Doing this can lead to a huge performance improvement.
3928 if (!is_dwz)
3929 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
3930 dwarf2_parse_compilation_unit((dwarf2_parse_context_t*)vector_at(&module_ctx->unit_contexts, i));
3932 return TRUE;
3935 static dwarf2_dwz_alternate_t* dwarf2_load_dwz(struct image_file_map* fmap, struct module* module)
3937 struct image_file_map* fmap_dwz;
3938 dwarf2_dwz_alternate_t* dwz;
3940 fmap_dwz = image_load_debugaltlink(fmap, module);
3941 if (!fmap_dwz) return NULL;
3942 if (!(dwz = HeapAlloc(GetProcessHeap(), 0, sizeof(*dwz))))
3944 image_unmap_file(fmap_dwz);
3945 HeapFree(GetProcessHeap(), 0, fmap_dwz);
3946 return NULL;
3949 dwz->fmap = fmap_dwz;
3950 dwarf2_init_section(&dwz->sections[section_debug], fmap_dwz, ".debug_info", ".zdebug_info", &dwz->sectmap[section_debug]);
3951 dwarf2_init_section(&dwz->sections[section_abbrev], fmap_dwz, ".debug_abbrev", ".zdebug_abbrev", &dwz->sectmap[section_abbrev]);
3952 dwarf2_init_section(&dwz->sections[section_string], fmap_dwz, ".debug_str", ".zdebug_str", &dwz->sectmap[section_string]);
3953 dwarf2_init_section(&dwz->sections[section_line], fmap_dwz, ".debug_line", ".zdebug_line", &dwz->sectmap[section_line]);
3954 dwarf2_init_section(&dwz->sections[section_ranges], fmap_dwz, ".debug_ranges", ".zdebug_ranges", &dwz->sectmap[section_ranges]);
3956 dwz->module_ctx.dwz = NULL;
3957 dwarf2_load_CU_module(&dwz->module_ctx, module, dwz->sections, 0/*FIXME*/, NULL, TRUE);
3958 return dwz;
3961 static void dwarf2_unload_dwz(dwarf2_dwz_alternate_t* dwz)
3963 if (!dwz) return;
3964 dwarf2_fini_section(&dwz->sections[section_debug]);
3965 dwarf2_fini_section(&dwz->sections[section_abbrev]);
3966 dwarf2_fini_section(&dwz->sections[section_string]);
3967 dwarf2_fini_section(&dwz->sections[section_line]);
3968 dwarf2_fini_section(&dwz->sections[section_ranges]);
3970 image_unmap_section(&dwz->sectmap[section_debug]);
3971 image_unmap_section(&dwz->sectmap[section_abbrev]);
3972 image_unmap_section(&dwz->sectmap[section_string]);
3973 image_unmap_section(&dwz->sectmap[section_line]);
3974 image_unmap_section(&dwz->sectmap[section_ranges]);
3976 image_unmap_file(dwz->fmap);
3977 HeapFree(GetProcessHeap(), 0, dwz);
3980 static BOOL dwarf2_unload_CU_module(dwarf2_parse_module_context_t* module_ctx)
3982 unsigned i;
3983 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
3985 dwarf2_parse_context_t* unit = vector_at(&module_ctx->unit_contexts, i);
3986 if (unit->status != UNIT_ERROR)
3987 pool_destroy(&unit->pool);
3989 dwarf2_unload_dwz(module_ctx->dwz);
3990 return TRUE;
3993 BOOL dwarf2_parse(struct module* module, ULONG_PTR load_offset,
3994 const struct elf_thunk_area* thunks,
3995 struct image_file_map* fmap)
3997 dwarf2_section_t eh_frame, section[section_max];
3998 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3999 debug_line_sect, debug_ranges_sect, eh_frame_sect;
4000 BOOL ret = TRUE;
4001 struct module_format* dwarf2_modfmt;
4002 dwarf2_parse_module_context_t module_ctx;
4004 if (!dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect))
4005 /* lld produces .eh_fram to avoid generating a long name */
4006 dwarf2_init_section(&eh_frame, fmap, ".eh_fram", NULL, &eh_frame_sect);
4007 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
4008 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
4009 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
4010 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
4011 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
4013 /* to do anything useful we need either .eh_frame or .debug_info */
4014 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
4015 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
4017 ret = FALSE;
4018 goto leave;
4021 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
4023 /* debug info might have a different base address than .so file
4024 * when elf file is prelinked after splitting off debug info
4025 * adjust symbol base addresses accordingly
4027 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
4030 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->modulename));
4032 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
4033 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
4034 if (!dwarf2_modfmt)
4036 ret = FALSE;
4037 goto leave;
4039 dwarf2_modfmt->module = module;
4040 dwarf2_modfmt->remove = dwarf2_module_remove;
4041 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
4042 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
4043 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8; /* set the word_size for eh_frame parsing */
4044 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
4046 /* As we'll need later some sections' content, we won't unmap these
4047 * sections upon existing this function
4049 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
4050 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
4051 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
4052 dwarf2_modfmt->u.dwarf2_info->cuheads = NULL;
4053 dwarf2_modfmt->u.dwarf2_info->num_cuheads = 0;
4055 module_ctx.dwz = dwarf2_load_dwz(fmap, module);
4056 dwarf2_load_CU_module(&module_ctx, module, section, load_offset, thunks, FALSE);
4058 dwarf2_modfmt->module->module.SymType = SymDia;
4059 /* hide dwarf versions in CVSig
4060 * bits 24-31 will be set according to found dwarf version
4061 * different CU can have different dwarf version, so use a bit per version (version 2 => b24)
4063 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('F' << 16) | ((module_ctx.cu_versions & 0xFF) << 24);
4064 /* FIXME: we could have a finer grain here */
4065 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
4066 dwarf2_modfmt->module->module.TypeInfo = TRUE;
4067 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
4068 dwarf2_modfmt->module->module.Publics = TRUE;
4070 dwarf2_unload_CU_module(&module_ctx);
4071 leave:
4073 dwarf2_fini_section(&section[section_debug]);
4074 dwarf2_fini_section(&section[section_abbrev]);
4075 dwarf2_fini_section(&section[section_string]);
4076 dwarf2_fini_section(&section[section_line]);
4077 dwarf2_fini_section(&section[section_ranges]);
4079 image_unmap_section(&debug_sect);
4080 image_unmap_section(&debug_abbrev_sect);
4081 image_unmap_section(&debug_str_sect);
4082 image_unmap_section(&debug_line_sect);
4083 image_unmap_section(&debug_ranges_sect);
4084 if (!ret) image_unmap_section(&eh_frame_sect);
4086 return ret;