dbghelp/dwarf: Don't unmap the fmap of a DWZ module twice.
[wine.git] / dlls / dbghelp / dwarf.c
blob67440e97fe8794007f1beaf70bba2e140eee5887
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 module->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 module->cpu->fetch_regname(loc->reg), loc->reg,
851 module->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 module->cpu->fetch_regname(loc->reg), loc->reg,
868 module->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 module->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_data4:
1063 if (ctx->head.version < 4)
1065 loc->kind = loc_dwarf2_location_list;
1066 loc->reg = Wine_DW_no_register;
1067 loc->offset = xloc.u.uvalue;
1068 return TRUE;
1070 /* fall through */
1071 case DW_FORM_data1: case DW_FORM_data2:
1072 case DW_FORM_udata: case DW_FORM_sdata:
1073 loc->kind = loc_absolute;
1074 loc->reg = 0;
1075 loc->offset = xloc.u.uvalue;
1076 return TRUE;
1077 case DW_FORM_data8:
1078 if (ctx->head.version >= 4)
1080 loc->kind = loc_absolute;
1081 loc->reg = 0;
1082 loc->offset = xloc.u.lluvalue;
1083 return TRUE;
1085 /* fall through */
1086 case DW_FORM_sec_offset:
1087 loc->kind = loc_dwarf2_location_list;
1088 loc->reg = Wine_DW_no_register;
1089 loc->offset = xloc.u.lluvalue;
1090 return TRUE;
1091 case DW_FORM_block:
1092 case DW_FORM_block1:
1093 case DW_FORM_block2:
1094 case DW_FORM_block4:
1095 case DW_FORM_exprloc:
1096 break;
1097 default: FIXME("Unsupported yet form %lx\n", xloc.form);
1098 return FALSE;
1101 /* assume we have a block form */
1102 if (dw == DW_AT_frame_base && xloc.u.block.size == 1 && *xloc.u.block.ptr == DW_OP_call_frame_cfa)
1104 loc->kind = loc_dwarf2_frame_cfa;
1105 loc->reg = Wine_DW_no_register;
1106 loc->offset = 0;
1108 else if (xloc.u.block.size)
1110 dwarf2_traverse_context_t lctx;
1111 enum location_error err;
1113 lctx.data = xloc.u.block.ptr;
1114 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
1116 err = compute_location(ctx->module_ctx->module, &ctx->head, &lctx, loc, NULL, frame);
1117 if (err < 0)
1119 loc->kind = loc_error;
1120 loc->reg = err;
1122 else if (loc->kind == loc_dwarf2_block)
1124 unsigned* ptr = pool_alloc(&ctx->module_ctx->module->pool,
1125 sizeof(unsigned) + xloc.u.block.size);
1126 *ptr = xloc.u.block.size;
1127 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
1128 loc->offset = (ULONG_PTR)ptr;
1131 return TRUE;
1134 static struct symt* dwarf2_lookup_type(const dwarf2_debug_info_t* di)
1136 struct attribute attr;
1137 dwarf2_debug_info_t* type;
1139 if (!dwarf2_find_attribute(di, DW_AT_type, &attr))
1140 /* this is only valid if current language of CU is C or C++ */
1141 return di->unit_ctx->module_ctx->symt_cache[sc_void];
1142 if (!(type = dwarf2_jump_to_debug_info(&attr)))
1143 return di->unit_ctx->module_ctx->symt_cache[sc_unknown];
1145 if (type == di)
1147 FIXME("Reference to itself\n");
1148 return di->unit_ctx->module_ctx->symt_cache[sc_unknown];
1150 if (!type->symt)
1152 /* load the debug info entity */
1153 dwarf2_load_one_entry(type);
1154 if (!type->symt)
1156 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
1157 return di->unit_ctx->module_ctx->symt_cache[sc_unknown];
1160 return type->symt;
1163 static const char* dwarf2_get_cpp_name(dwarf2_debug_info_t* di, const char* name)
1165 char* last;
1166 struct attribute diname;
1167 struct attribute spec;
1169 if (di->abbrev->tag == DW_TAG_compile_unit || di->abbrev->tag == DW_TAG_partial_unit) return name;
1171 /* if the di is a definition, but has also a (previous) declaration, then scope must
1172 * be gotten from declaration not definition
1174 if (dwarf2_find_attribute(di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
1176 di = dwarf2_jump_to_debug_info(&spec);
1177 if (!di)
1179 FIXME("Should have found the debug info entry\n");
1180 return NULL;
1184 if (!di->unit_ctx->cpp_name)
1185 di->unit_ctx->cpp_name = pool_alloc(&di->unit_ctx->pool, MAX_SYM_NAME);
1186 last = di->unit_ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
1187 strcpy(last, name);
1189 for (di = di->parent; di; di = di->parent)
1191 switch (di->abbrev->tag)
1193 case DW_TAG_namespace:
1194 case DW_TAG_structure_type:
1195 case DW_TAG_class_type:
1196 case DW_TAG_interface_type:
1197 case DW_TAG_union_type:
1198 if (dwarf2_find_attribute(di, DW_AT_name, &diname))
1200 size_t len = strlen(diname.u.string);
1201 last -= 2 + len;
1202 if (last < di->unit_ctx->cpp_name) return NULL;
1203 memcpy(last, diname.u.string, len);
1204 last[len] = last[len + 1] = ':';
1206 break;
1207 default:
1208 break;
1211 return last;
1214 /******************************************************************
1215 * dwarf2_read_range
1217 * read a range for a given debug_info (either using AT_range attribute, in which
1218 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1219 * in all cases, range is relative to beginning of compilation unit
1221 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1222 ULONG_PTR* plow, ULONG_PTR* phigh)
1224 struct attribute range;
1226 if (dwarf2_find_attribute(di, DW_AT_ranges, &range))
1228 dwarf2_traverse_context_t traverse;
1229 ULONG_PTR low, high;
1230 const ULONG_PTR UMAX = ~(ULONG_PTR)0u;
1232 traverse.data = ctx->module_ctx->sections[section_ranges].address + range.u.uvalue;
1233 traverse.end_data = ctx->module_ctx->sections[section_ranges].address +
1234 ctx->module_ctx->sections[section_ranges].size;
1236 *plow = UMAX;
1237 *phigh = 0;
1238 while (traverse.data + 2 * ctx->head.word_size < traverse.end_data)
1240 low = dwarf2_parse_addr_head(&traverse, &ctx->head);
1241 high = dwarf2_parse_addr_head(&traverse, &ctx->head);
1242 if (low == 0 && high == 0) break;
1243 if (low == (ctx->head.word_size == 8 ? (~(DWORD64)0u) : (DWORD64)(~0u)))
1244 FIXME("unsupported yet (base address selection)\n");
1245 /* range values are relative to start of compilation unit */
1246 low += ctx->compiland->address - ctx->module_ctx->load_offset;
1247 high += ctx->compiland->address - ctx->module_ctx->load_offset;
1248 if (low < *plow) *plow = low;
1249 if (high > *phigh) *phigh = high;
1251 if (*plow == UMAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1252 if (*plow == *phigh) {WARN("entry found, but low=high %lx %lx\n", low, high); return FALSE;}
1254 return TRUE;
1256 else
1258 struct attribute low_pc;
1259 struct attribute high_pc;
1261 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc) ||
1262 !dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc))
1263 return FALSE;
1264 *plow = low_pc.u.uvalue;
1265 *phigh = high_pc.u.uvalue;
1266 if (ctx->head.version >= 4)
1267 switch (high_pc.form)
1269 case DW_FORM_addr:
1270 break;
1271 case DW_FORM_data1:
1272 case DW_FORM_data2:
1273 case DW_FORM_data4:
1274 case DW_FORM_data8:
1275 case DW_FORM_sdata:
1276 case DW_FORM_udata:
1277 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1278 *phigh += *plow;
1279 break;
1280 default:
1281 FIXME("Unsupported class for high_pc\n");
1282 break;
1284 return TRUE;
1288 static struct addr_range* dwarf2_get_ranges(const dwarf2_debug_info_t* di, unsigned* num_ranges)
1290 struct attribute range;
1291 struct addr_range* ranges;
1293 if (dwarf2_find_attribute(di, DW_AT_ranges, &range))
1295 dwarf2_traverse_context_t traverse;
1296 unsigned alloc = 16;
1297 ranges = malloc(sizeof(struct addr_range) * alloc);
1298 if (!ranges) return NULL;
1299 *num_ranges = 0;
1301 traverse.data = di->unit_ctx->module_ctx->sections[section_ranges].address + range.u.uvalue;
1302 traverse.end_data = di->unit_ctx->module_ctx->sections[section_ranges].address +
1303 di->unit_ctx->module_ctx->sections[section_ranges].size;
1305 while (traverse.data + 2 * di->unit_ctx->head.word_size < traverse.end_data)
1307 ULONG_PTR low = dwarf2_parse_addr_head(&traverse, &di->unit_ctx->head);
1308 ULONG_PTR high = dwarf2_parse_addr_head(&traverse, &di->unit_ctx->head);
1309 if (low == 0 && high == 0) break;
1310 if (low == (di->unit_ctx->head.word_size == 8 ? (~(DWORD64)0u) : (DWORD64)(~0u)))
1311 FIXME("unsupported yet (base address selection)\n");
1312 if (*num_ranges >= alloc)
1314 alloc *= 2;
1315 ranges = realloc(ranges, sizeof(struct addr_range) * alloc);
1316 if (!ranges) return NULL;
1318 ranges[*num_ranges].low = di->unit_ctx->compiland->address + low;
1319 ranges[*num_ranges].high = di->unit_ctx->compiland->address + high;
1320 (*num_ranges)++;
1323 else
1325 struct attribute low_pc;
1326 struct attribute high_pc;
1328 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc) ||
1329 !dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc))
1330 return NULL;
1331 if (di->unit_ctx->head.version >= 4)
1332 switch (high_pc.form)
1334 case DW_FORM_addr:
1335 break;
1336 case DW_FORM_data1:
1337 case DW_FORM_data2:
1338 case DW_FORM_data4:
1339 case DW_FORM_data8:
1340 case DW_FORM_sdata:
1341 case DW_FORM_udata:
1342 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1343 high_pc.u.uvalue += low_pc.u.uvalue;
1344 break;
1345 default:
1346 FIXME("Unsupported class for high_pc\n");
1347 break;
1349 ranges = malloc(sizeof(struct addr_range));
1350 if (!ranges) return NULL;
1351 ranges[0].low = di->unit_ctx->module_ctx->load_offset + low_pc.u.uvalue;
1352 ranges[0].high = di->unit_ctx->module_ctx->load_offset + high_pc.u.uvalue;
1353 *num_ranges = 1;
1355 return ranges;
1358 /******************************************************************
1359 * dwarf2_read_one_debug_info
1361 * Loads into memory one debug info entry, and recursively its children (if any)
1363 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1364 dwarf2_traverse_context_t* traverse,
1365 dwarf2_debug_info_t* parent_di,
1366 dwarf2_debug_info_t** pdi)
1368 const dwarf2_abbrev_entry_t*abbrev;
1369 ULONG_PTR entry_code;
1370 ULONG_PTR offset;
1371 dwarf2_debug_info_t* di;
1372 dwarf2_debug_info_t* child;
1373 dwarf2_debug_info_t** where;
1374 dwarf2_abbrev_entry_attr_t* attr;
1375 unsigned i;
1376 struct attribute sibling;
1378 offset = traverse->data - ctx->module_ctx->sections[ctx->section].address;
1379 entry_code = dwarf2_leb128_as_unsigned(traverse);
1380 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1381 if (!entry_code)
1383 *pdi = NULL;
1384 return TRUE;
1386 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1387 if (!abbrev)
1389 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1390 return FALSE;
1392 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1393 if (!di) return FALSE;
1394 di->abbrev = abbrev;
1395 di->symt = NULL;
1396 di->parent = parent_di;
1397 di->unit_ctx = ctx;
1399 if (abbrev->num_attr)
1401 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1402 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1404 di->data[i] = traverse->data;
1405 dwarf2_swallow_attribute(traverse, &ctx->head, attr);
1408 else di->data = NULL;
1409 if (abbrev->have_child)
1411 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1412 while (traverse->data < traverse->end_data)
1414 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1415 if (!child) break;
1416 where = vector_add(&di->children, &ctx->pool);
1417 if (!where) return FALSE;
1418 *where = child;
1421 if (dwarf2_find_attribute(di, DW_AT_sibling, &sibling) &&
1422 traverse->data != ctx->module_ctx->sections[ctx->section].address + sibling.u.uvalue)
1424 if (sibling.u.uvalue >= ctx->module_ctx->sections[ctx->section].size)
1426 FIXME("cursor sibling after section end %s: 0x%lx 0x%x\n",
1427 dwarf2_debug_unit_ctx(ctx), sibling.u.uvalue, ctx->module_ctx->sections[ctx->section].size);
1428 return FALSE;
1430 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1431 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1432 traverse->data = ctx->module_ctx->sections[ctx->section].address + sibling.u.uvalue;
1434 *pdi = di;
1435 return TRUE;
1438 static struct vector* dwarf2_get_di_children(dwarf2_debug_info_t* di)
1440 struct attribute spec;
1442 while (di)
1444 if (di->abbrev->have_child)
1445 return &di->children;
1446 if (!dwarf2_find_attribute(di, DW_AT_specification, &spec)) break;
1447 if (!(di = dwarf2_jump_to_debug_info(&spec)))
1448 FIXME("Should have found the debug info entry\n");
1450 return NULL;
1453 static struct symt* dwarf2_parse_base_type(dwarf2_debug_info_t* di)
1455 struct attribute name;
1456 struct attribute size;
1457 struct attribute encoding;
1458 enum BasicType bt;
1459 if (di->symt) return di->symt;
1461 TRACE("%s\n", dwarf2_debug_di(di));
1463 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1464 name.u.string = NULL;
1465 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1466 if (!dwarf2_find_attribute(di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1468 switch (encoding.u.uvalue)
1470 case DW_ATE_void: bt = btVoid; break;
1471 case DW_ATE_address: bt = btULong; break;
1472 case DW_ATE_boolean: bt = btBool; break;
1473 case DW_ATE_complex_float: bt = btComplex; break;
1474 case DW_ATE_float: bt = btFloat; break;
1475 case DW_ATE_signed: bt = btInt; break;
1476 case DW_ATE_unsigned: bt = btUInt; break;
1477 case DW_ATE_signed_char: bt = btChar; break;
1478 case DW_ATE_unsigned_char: bt = btChar; break;
1479 default: bt = btNoType; break;
1481 di->symt = &symt_new_basic(di->unit_ctx->module_ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1483 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1484 return di->symt;
1487 static struct symt* dwarf2_parse_typedef(dwarf2_debug_info_t* di)
1489 struct symt* ref_type;
1490 struct attribute name;
1492 if (di->symt) return di->symt;
1494 TRACE("%s\n", dwarf2_debug_di(di));
1496 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1497 ref_type = dwarf2_lookup_type(di);
1499 if (name.u.string)
1500 di->symt = &symt_new_typedef(di->unit_ctx->module_ctx->module, ref_type, name.u.string)->symt;
1501 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1502 return di->symt;
1505 static struct symt* dwarf2_parse_pointer_type(dwarf2_debug_info_t* di)
1507 struct symt* ref_type;
1508 struct attribute size;
1510 if (di->symt) return di->symt;
1512 TRACE("%s\n", dwarf2_debug_di(di));
1514 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = di->unit_ctx->module_ctx->module->cpu->word_size;
1515 ref_type = dwarf2_lookup_type(di);
1516 di->symt = &symt_new_pointer(di->unit_ctx->module_ctx->module, ref_type, size.u.uvalue)->symt;
1517 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1518 return di->symt;
1521 static struct symt* dwarf2_parse_subrange_type(dwarf2_debug_info_t* di)
1523 struct symt* ref_type;
1524 struct attribute name;
1525 struct attribute dummy;
1527 if (di->symt) return di->symt;
1529 TRACE("%s\n", dwarf2_debug_di(di));
1531 if (dwarf2_find_attribute(di, DW_AT_name, &name)) FIXME("Found name for subrange %s\n", name.u.string);
1532 if (dwarf2_find_attribute(di, DW_AT_byte_size, &dummy)) FIXME("Found byte_size %lu\n", dummy.u.uvalue);
1533 if (dwarf2_find_attribute(di, DW_AT_bit_size, &dummy)) FIXME("Found bit_size %lu\n", dummy.u.uvalue);
1534 /* for now, we don't support the byte_size nor bit_size about the subrange, and pretend the two
1535 * types are the same (FIXME)
1537 ref_type = dwarf2_lookup_type(di);
1538 di->symt = ref_type;
1539 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1540 return di->symt;
1543 static struct symt* dwarf2_parse_array_type(dwarf2_debug_info_t* di)
1545 struct symt* ref_type;
1546 struct symt* idx_type = NULL;
1547 struct symt* symt = NULL;
1548 struct attribute min, max, cnt;
1549 dwarf2_debug_info_t* child;
1550 unsigned int i, j;
1551 const struct vector* children;
1553 if (di->symt) return di->symt;
1555 TRACE("%s\n", dwarf2_debug_di(di));
1557 ref_type = dwarf2_lookup_type(di);
1559 if (!(children = dwarf2_get_di_children(di)))
1561 /* fake an array with unknown size */
1562 /* FIXME: int4 even on 64bit machines??? */
1563 idx_type = &symt_new_basic(di->unit_ctx->module_ctx->module, btInt, "int", 4)->symt;
1564 min.u.uvalue = 0;
1565 cnt.u.uvalue = 0;
1567 else for (i = 0; i < vector_length(children); i++)
1569 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1570 if (child->symt == di->unit_ctx->module_ctx->symt_cache[sc_unknown]) continue;
1571 switch (child->abbrev->tag)
1573 case DW_TAG_subrange_type:
1574 idx_type = dwarf2_lookup_type(child);
1575 if (!dwarf2_find_attribute(child, DW_AT_lower_bound, &min))
1576 min.u.uvalue = 0;
1577 if (dwarf2_find_attribute(child, DW_AT_upper_bound, &max))
1578 cnt.u.uvalue = max.u.uvalue + 1 - min.u.uvalue;
1579 else if (!dwarf2_find_attribute(child, DW_AT_count, &cnt))
1580 cnt.u.uvalue = 0;
1581 break;
1582 case DW_TAG_enumeration_type:
1583 symt = dwarf2_parse_enumeration_type(child);
1584 if (symt_check_tag(symt, SymTagEnum))
1586 struct symt_enum* enum_symt = (struct symt_enum*)symt;
1587 idx_type = enum_symt->base_type;
1588 min.u.uvalue = ~0U;
1589 max.u.uvalue = ~0U;
1590 for (j = 0; j < enum_symt->vchildren.num_elts; ++j)
1592 struct symt** pc = vector_at(&enum_symt->vchildren, j);
1593 if (pc && symt_check_tag(*pc, SymTagData))
1595 struct symt_data* elt = (struct symt_data*)(*pc);
1596 if (elt->u.value.n1.n2.n3.lVal < min.u.uvalue)
1597 min.u.uvalue = elt->u.value.n1.n2.n3.lVal;
1598 if (elt->u.value.n1.n2.n3.lVal > max.u.uvalue)
1599 max.u.uvalue = elt->u.value.n1.n2.n3.lVal;
1603 break;
1604 default:
1605 FIXME("Unhandled Tag type 0x%lx at %s\n",
1606 child->abbrev->tag, dwarf2_debug_di(di));
1607 break;
1610 di->symt = &symt_new_array(di->unit_ctx->module_ctx->module, min.u.uvalue, cnt.u.uvalue, ref_type, idx_type)->symt;
1611 return di->symt;
1614 static struct symt* dwarf2_parse_const_type(dwarf2_debug_info_t* di)
1616 struct symt* ref_type;
1618 if (di->symt) return di->symt;
1620 TRACE("%s\n", dwarf2_debug_di(di));
1622 ref_type = dwarf2_lookup_type(di);
1623 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1624 di->symt = ref_type;
1626 return ref_type;
1629 static struct symt* dwarf2_parse_volatile_type(dwarf2_debug_info_t* di)
1631 struct symt* ref_type;
1633 if (di->symt) return di->symt;
1635 TRACE("%s\n", dwarf2_debug_di(di));
1637 ref_type = dwarf2_lookup_type(di);
1638 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1639 di->symt = ref_type;
1641 return ref_type;
1644 static struct symt* dwarf2_parse_restrict_type(dwarf2_debug_info_t* di)
1646 struct symt* ref_type;
1648 if (di->symt) return di->symt;
1650 TRACE("%s\n", dwarf2_debug_di(di));
1652 ref_type = dwarf2_lookup_type(di);
1653 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1654 di->symt = ref_type;
1656 return ref_type;
1659 static struct symt* dwarf2_parse_unspecified_type(dwarf2_debug_info_t* di)
1661 struct attribute name;
1662 struct attribute size;
1663 struct symt_basic *basic;
1665 TRACE("%s\n", dwarf2_debug_di(di));
1667 if (di->symt) return di->symt;
1669 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1670 name.u.string = "void";
1671 size.u.uvalue = di->unit_ctx->module_ctx->module->cpu->word_size;
1673 basic = symt_new_basic(di->unit_ctx->module_ctx->module, btVoid, name.u.string, size.u.uvalue);
1674 di->symt = &basic->symt;
1676 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1677 return di->symt;
1680 static struct symt* dwarf2_parse_reference_type(dwarf2_debug_info_t* di)
1682 struct symt* ref_type = NULL;
1684 if (di->symt) return di->symt;
1686 TRACE("%s\n", dwarf2_debug_di(di));
1688 ref_type = dwarf2_lookup_type(di);
1689 /* FIXME: for now, we hard-wire C++ references to pointers */
1690 di->symt = &symt_new_pointer(di->unit_ctx->module_ctx->module, ref_type,
1691 di->unit_ctx->module_ctx->module->cpu->word_size)->symt;
1693 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1695 return di->symt;
1698 static void dwarf2_parse_udt_member(dwarf2_debug_info_t* di,
1699 struct symt_udt* parent)
1701 struct symt* elt_type;
1702 struct attribute name;
1703 struct attribute bit_size;
1704 struct attribute bit_offset;
1705 struct location loc;
1707 assert(parent);
1709 TRACE("%s\n", dwarf2_debug_di(di));
1711 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1712 elt_type = dwarf2_lookup_type(di);
1713 if (dwarf2_compute_location_attr(di->unit_ctx, di, DW_AT_data_member_location, &loc, NULL))
1715 if (loc.kind != loc_absolute)
1717 FIXME("Unexpected offset computation for member %s in %ls!%s\n",
1718 name.u.string, di->unit_ctx->module_ctx->module->modulename, parent->hash_elt.name);
1719 loc.offset = 0;
1721 else
1722 TRACE("found member_location at %s -> %lu\n",
1723 dwarf2_debug_di(di), loc.offset);
1725 else
1726 loc.offset = 0;
1727 if (!dwarf2_find_attribute(di, DW_AT_bit_size, &bit_size))
1728 bit_size.u.uvalue = 0;
1729 if (dwarf2_find_attribute(di, DW_AT_bit_offset, &bit_offset))
1731 /* FIXME: we should only do this when implementation is LSB (which is
1732 * the case on i386 processors)
1734 struct attribute nbytes;
1735 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &nbytes))
1737 DWORD64 size;
1738 nbytes.u.uvalue = symt_get_info(di->unit_ctx->module_ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1739 (ULONG_PTR)size : 0;
1741 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1743 else bit_offset.u.uvalue = 0;
1744 symt_add_udt_element(di->unit_ctx->module_ctx->module, parent, name.u.string, elt_type,
1745 loc.offset, bit_offset.u.uvalue,
1746 bit_size.u.uvalue);
1748 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1751 static struct symt* dwarf2_parse_subprogram(dwarf2_debug_info_t* di);
1753 static struct symt* dwarf2_parse_udt_type(dwarf2_debug_info_t* di,
1754 enum UdtKind udt)
1756 struct attribute name;
1757 struct attribute size;
1758 struct vector* children;
1759 dwarf2_debug_info_t*child;
1760 unsigned int i;
1762 if (di->symt) return di->symt;
1764 TRACE("%s\n", dwarf2_debug_di(di));
1766 /* quirk... FIXME provide real support for anonymous UDTs */
1767 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1768 name.u.string = "zz_anon_zz";
1769 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1771 di->symt = &symt_new_udt(di->unit_ctx->module_ctx->module, dwarf2_get_cpp_name(di, name.u.string),
1772 size.u.uvalue, udt)->symt;
1774 children = dwarf2_get_di_children(di);
1775 if (children) for (i = 0; i < vector_length(children); i++)
1777 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1779 switch (child->abbrev->tag)
1781 case DW_TAG_array_type:
1782 dwarf2_parse_array_type(child);
1783 break;
1784 case DW_TAG_member:
1785 /* FIXME: should I follow the sibling stuff ?? */
1786 if (symt_check_tag(di->symt, SymTagUDT))
1787 dwarf2_parse_udt_member(child, (struct symt_udt*)di->symt);
1788 break;
1789 case DW_TAG_enumeration_type:
1790 dwarf2_parse_enumeration_type(child);
1791 break;
1792 case DW_TAG_subprogram:
1793 dwarf2_parse_subprogram(child);
1794 break;
1795 case DW_TAG_const_type:
1796 dwarf2_parse_const_type(child);
1797 break;
1798 case DW_TAG_volatile_type:
1799 dwarf2_parse_volatile_type(child);
1800 break;
1801 case DW_TAG_pointer_type:
1802 dwarf2_parse_pointer_type(child);
1803 break;
1804 case DW_TAG_subrange_type:
1805 dwarf2_parse_subrange_type(child);
1806 break;
1807 case DW_TAG_structure_type:
1808 case DW_TAG_class_type:
1809 case DW_TAG_union_type:
1810 case DW_TAG_typedef:
1811 /* FIXME: we need to handle nested udt definitions */
1812 case DW_TAG_inheritance:
1813 case DW_TAG_interface_type:
1814 case DW_TAG_template_type_param:
1815 case DW_TAG_template_value_param:
1816 case DW_TAG_variable:
1817 case DW_TAG_imported_declaration:
1818 case DW_TAG_ptr_to_member_type:
1819 case DW_TAG_GNU_template_template_param:
1820 case DW_TAG_GNU_template_parameter_pack:
1821 case DW_TAG_GNU_formal_parameter_pack:
1822 /* FIXME: some C++ related stuff */
1823 break;
1824 default:
1825 FIXME("Unhandled Tag type 0x%lx at %s\n",
1826 child->abbrev->tag, dwarf2_debug_di(di));
1827 break;
1831 return di->symt;
1834 static void dwarf2_parse_enumerator(dwarf2_debug_info_t* di,
1835 struct symt_enum* parent)
1837 struct attribute name;
1838 struct attribute value;
1840 TRACE("%s\n", dwarf2_debug_di(di));
1842 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) return;
1843 if (!dwarf2_find_attribute(di, DW_AT_const_value, &value)) value.u.svalue = 0;
1844 symt_add_enum_element(di->unit_ctx->module_ctx->module, parent, name.u.string, value.u.svalue);
1846 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1849 static struct symt* dwarf2_parse_enumeration_type(dwarf2_debug_info_t* di)
1851 struct attribute name;
1852 struct attribute attrtype;
1853 dwarf2_debug_info_t*ditype;
1854 struct symt* type;
1855 struct vector* children;
1856 dwarf2_debug_info_t*child;
1857 unsigned int i;
1859 if (di->symt) return di->symt;
1861 TRACE("%s\n", dwarf2_debug_di(di));
1863 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1864 if (dwarf2_find_attribute(di, DW_AT_type, &attrtype) && (ditype = dwarf2_jump_to_debug_info(&attrtype)) != NULL)
1865 type = ditype->symt;
1866 else /* no type found for this enumeration, construct it from size */
1868 struct attribute size;
1869 struct symt_basic* basetype;
1871 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1873 switch (size.u.uvalue) /* FIXME: that's wrong */
1875 case 1: basetype = symt_new_basic(di->unit_ctx->module_ctx->module, btInt, "char", 1); break;
1876 case 2: basetype = symt_new_basic(di->unit_ctx->module_ctx->module, btInt, "short", 2); break;
1877 default:
1878 case 4: basetype = symt_new_basic(di->unit_ctx->module_ctx->module, btInt, "int", 4); break;
1880 type = &basetype->symt;
1883 di->symt = &symt_new_enum(di->unit_ctx->module_ctx->module, name.u.string, type)->symt;
1884 children = dwarf2_get_di_children(di);
1886 if (children) for (i = 0; i < vector_length(children); i++)
1888 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1890 switch (child->abbrev->tag)
1892 case DW_TAG_enumerator:
1893 if (symt_check_tag(di->symt, SymTagEnum))
1894 dwarf2_parse_enumerator(child, (struct symt_enum*)di->symt);
1895 break;
1896 default:
1897 FIXME("Unhandled Tag type 0x%lx at %s\n",
1898 di->abbrev->tag, dwarf2_debug_di(di));
1901 return di->symt;
1904 /* structure used to pass information around when parsing a subprogram */
1905 typedef struct dwarf2_subprogram_s
1907 dwarf2_parse_context_t* ctx;
1908 struct symt_function* top_func;
1909 struct symt_function* current_func; /* either symt_function* or symt_inlinesite* */
1910 struct symt_block* current_block;
1911 BOOL non_computed_variable;
1912 struct location frame;
1913 } dwarf2_subprogram_t;
1915 /******************************************************************
1916 * dwarf2_parse_variable
1918 * Parses any variable (parameter, local/global variable)
1920 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1921 dwarf2_debug_info_t* di)
1923 struct symt* param_type;
1924 struct attribute name, value;
1925 struct location loc;
1926 BOOL is_pmt;
1928 TRACE("%s\n", dwarf2_debug_di(di));
1930 is_pmt = !subpgm->current_block && di->abbrev->tag == DW_TAG_formal_parameter;
1931 param_type = dwarf2_lookup_type(di);
1933 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) {
1934 /* cannot do much without the name, the functions below won't like it. */
1935 return;
1937 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1938 &loc, &subpgm->frame))
1940 struct attribute ext;
1942 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1943 debugstr_a(name.u.string), loc.kind, loc.offset, loc.reg,
1944 dwarf2_debug_unit_ctx(subpgm->ctx));
1946 switch (loc.kind)
1948 case loc_error:
1949 break;
1950 case loc_absolute:
1951 /* it's a global variable */
1952 /* FIXME: we don't handle its scope yet */
1953 if (!dwarf2_find_attribute(di, DW_AT_external, &ext))
1954 ext.u.uvalue = 0;
1955 loc.offset += subpgm->ctx->module_ctx->load_offset;
1956 symt_new_global_variable(subpgm->ctx->module_ctx->module, subpgm->ctx->compiland,
1957 dwarf2_get_cpp_name(di, name.u.string), !ext.u.uvalue,
1958 loc, 0, param_type);
1959 break;
1960 default:
1961 subpgm->non_computed_variable = TRUE;
1962 /* fall through */
1963 case loc_register:
1964 case loc_regrel:
1965 /* either a pmt/variable relative to frame pointer or
1966 * pmt/variable in a register
1968 if (subpgm->current_func)
1969 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->current_func,
1970 is_pmt ? DataIsParam : DataIsLocal,
1971 &loc, subpgm->current_block, param_type, name.u.string);
1972 break;
1975 else if (dwarf2_find_attribute(di, DW_AT_const_value, &value))
1977 VARIANT v;
1979 switch (value.form)
1981 case DW_FORM_data1:
1982 case DW_FORM_data2:
1983 case DW_FORM_data4:
1984 case DW_FORM_udata:
1985 case DW_FORM_addr:
1986 V_VT(&v) = VT_UI4;
1987 V_UI4(&v) = value.u.uvalue;
1988 break;
1990 case DW_FORM_data8:
1991 case DW_FORM_sec_offset:
1992 V_VT(&v) = VT_UI8;
1993 V_UI8(&v) = value.u.lluvalue;
1994 break;
1996 case DW_FORM_sdata:
1997 V_VT(&v) = VT_I4;
1998 V_I4(&v) = value.u.svalue;
1999 break;
2001 case DW_FORM_strp:
2002 case DW_FORM_string:
2003 /* FIXME: native doesn't report const strings from here !!
2004 * however, the value of the string is in the code somewhere
2006 V_VT(&v) = VT_BYREF;
2007 V_BYREF(&v) = pool_strdup(&subpgm->ctx->module_ctx->module->pool, value.u.string);
2008 break;
2010 case DW_FORM_block:
2011 case DW_FORM_block1:
2012 case DW_FORM_block2:
2013 case DW_FORM_block4:
2014 case DW_FORM_exprloc:
2015 V_VT(&v) = VT_I4;
2016 switch (value.u.block.size)
2018 case 1: V_I4(&v) = *(BYTE*)value.u.block.ptr; break;
2019 case 2: V_I4(&v) = *(USHORT*)value.u.block.ptr; break;
2020 case 4: V_I4(&v) = *(DWORD*)value.u.block.ptr; break;
2021 default:
2022 V_VT(&v) = VT_BYREF;
2023 V_BYREF(&v) = pool_alloc(&subpgm->ctx->module_ctx->module->pool, value.u.block.size);
2024 memcpy(V_BYREF(&v), value.u.block.ptr, value.u.block.size);
2026 break;
2028 default:
2029 FIXME("Unsupported form for const value %s (%lx)\n",
2030 debugstr_a(name.u.string), value.form);
2031 V_VT(&v) = VT_EMPTY;
2033 if (subpgm->current_func)
2035 if (is_pmt) WARN("Constant parameter %s reported as local variable in function '%s'\n",
2036 debugstr_a(name.u.string), subpgm->current_func->hash_elt.name);
2037 di->symt = &symt_add_func_constant(subpgm->ctx->module_ctx->module,
2038 subpgm->current_func, subpgm->current_block,
2039 param_type, name.u.string, &v)->symt;
2041 else
2042 di->symt = &symt_new_constant(subpgm->ctx->module_ctx->module, subpgm->ctx->compiland,
2043 name.u.string, param_type, &v)->symt;
2045 else
2047 if (subpgm->current_func)
2049 /* local variable has been optimized away... report anyway */
2050 loc.kind = loc_error;
2051 loc.reg = loc_err_no_location;
2052 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->current_func,
2053 is_pmt ? DataIsParam : DataIsLocal,
2054 &loc, subpgm->current_block, param_type, name.u.string);
2056 else
2058 struct attribute is_decl;
2059 /* only warn when di doesn't represent a declaration */
2060 if (!dwarf2_find_attribute(di, DW_AT_declaration, &is_decl) ||
2061 !is_decl.u.uvalue || is_decl.gotten_from != attr_direct)
2062 WARN("dropping global variable %s which has been optimized away\n", debugstr_a(name.u.string));
2065 if (is_pmt && subpgm->current_func && symt_check_tag(subpgm->current_func->type, SymTagFunctionType))
2066 symt_add_function_signature_parameter(subpgm->ctx->module_ctx->module,
2067 (struct symt_function_signature*)subpgm->current_func->type,
2068 param_type);
2070 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
2073 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
2074 const dwarf2_debug_info_t* di)
2076 struct attribute name;
2077 struct attribute low_pc;
2078 struct location loc;
2080 TRACE("%s\n", dwarf2_debug_di(di));
2082 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2083 name.u.string = NULL;
2084 if (dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc))
2086 loc.kind = loc_absolute;
2087 loc.offset = subpgm->ctx->module_ctx->load_offset + low_pc.u.uvalue - subpgm->top_func->address;
2088 symt_add_function_point(subpgm->ctx->module_ctx->module, subpgm->top_func, SymTagLabel,
2089 &loc, name.u.string);
2091 else
2092 WARN("Label %s inside function %s doesn't have an address... don't register it\n",
2093 name.u.string, subpgm->top_func->hash_elt.name);
2096 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
2097 dwarf2_debug_info_t* di);
2099 static struct symt* dwarf2_parse_subroutine_type(dwarf2_debug_info_t* di);
2101 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
2102 dwarf2_debug_info_t* di)
2104 struct attribute name;
2105 ULONG_PTR low_pc, high_pc;
2106 struct symt* ret_type;
2107 struct symt_function_signature* sig_type;
2108 struct symt_inlinesite* inlined;
2109 struct vector* children;
2110 dwarf2_debug_info_t*child;
2111 unsigned int i;
2112 struct addr_range* adranges;
2113 unsigned num_adranges;
2115 TRACE("%s\n", dwarf2_debug_di(di));
2117 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
2119 WARN("cannot read range\n");
2120 return;
2122 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2124 FIXME("No name for function... dropping function\n");
2125 return;
2127 ret_type = dwarf2_lookup_type(di);
2129 /* FIXME: assuming C source code */
2130 sig_type = symt_new_function_signature(subpgm->ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2132 inlined = symt_new_inlinesite(subpgm->ctx->module_ctx->module,
2133 subpgm->top_func,
2134 subpgm->current_block ? &subpgm->current_block->symt : &subpgm->current_func->symt,
2135 dwarf2_get_cpp_name(di, name.u.string),
2136 subpgm->ctx->module_ctx->load_offset + low_pc,
2137 &sig_type->symt);
2138 subpgm->current_func = (struct symt_function*)inlined;
2139 subpgm->current_block = NULL;
2141 if ((adranges = dwarf2_get_ranges(di, &num_adranges)) != NULL)
2143 for (i = 0; i < num_adranges; ++i)
2144 symt_add_inlinesite_range(subpgm->ctx->module_ctx->module, inlined,
2145 adranges[i].low, adranges[i].high);
2146 free(adranges);
2148 else
2149 WARN("cannot read ranges\n");
2151 children = dwarf2_get_di_children(di);
2152 if (children) for (i = 0; i < vector_length(children); i++)
2154 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2156 switch (child->abbrev->tag)
2158 case DW_TAG_formal_parameter:
2159 case DW_TAG_variable:
2160 dwarf2_parse_variable(subpgm, child);
2161 break;
2162 case DW_TAG_lexical_block:
2163 dwarf2_parse_subprogram_block(subpgm, child);
2164 break;
2165 case DW_TAG_inlined_subroutine:
2166 dwarf2_parse_inlined_subroutine(subpgm, child);
2167 break;
2168 case DW_TAG_label:
2169 dwarf2_parse_subprogram_label(subpgm, child);
2170 break;
2171 case DW_TAG_GNU_call_site:
2172 /* this isn't properly supported by dbghelp interface. skip it for now */
2173 break;
2174 default:
2175 FIXME("Unhandled Tag type 0x%lx at %s\n",
2176 child->abbrev->tag, dwarf2_debug_di(di));
2179 subpgm->current_block = symt_check_tag(subpgm->current_func->container, SymTagBlock) ?
2180 (struct symt_block*)subpgm->current_func->container : NULL;
2181 subpgm->current_func = (struct symt_function*)symt_get_upper_inlined((struct symt_inlinesite*)subpgm->current_func);
2184 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
2185 dwarf2_debug_info_t* di)
2187 ULONG_PTR low_pc, high_pc;
2188 struct vector* children;
2189 dwarf2_debug_info_t*child;
2190 unsigned int i;
2192 TRACE("%s\n", dwarf2_debug_di(di));
2194 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
2196 WARN("no range\n");
2197 return;
2200 subpgm->current_block = symt_open_func_block(subpgm->ctx->module_ctx->module, subpgm->current_func, subpgm->current_block,
2201 subpgm->ctx->module_ctx->load_offset + low_pc - subpgm->current_func->address,
2202 high_pc - low_pc);
2204 children = dwarf2_get_di_children(di);
2205 if (children) for (i = 0; i < vector_length(children); i++)
2207 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2209 switch (child->abbrev->tag)
2211 case DW_TAG_inlined_subroutine:
2212 dwarf2_parse_inlined_subroutine(subpgm, child);
2213 break;
2214 case DW_TAG_variable:
2215 dwarf2_parse_variable(subpgm, child);
2216 break;
2217 case DW_TAG_pointer_type:
2218 dwarf2_parse_pointer_type(child);
2219 break;
2220 case DW_TAG_subroutine_type:
2221 dwarf2_parse_subroutine_type(child);
2222 break;
2223 case DW_TAG_const_type:
2224 dwarf2_parse_const_type(child);
2225 break;
2226 case DW_TAG_lexical_block:
2227 dwarf2_parse_subprogram_block(subpgm, child);
2228 break;
2229 case DW_TAG_subprogram:
2230 /* FIXME: likely a declaration (to be checked)
2231 * skip it for now
2233 break;
2234 case DW_TAG_formal_parameter:
2235 /* FIXME: likely elements for exception handling (GCC flavor)
2236 * Skip it for now
2238 break;
2239 case DW_TAG_imported_module:
2240 /* C++ stuff to be silenced (for now) */
2241 break;
2242 case DW_TAG_GNU_call_site:
2243 /* this isn't properly supported by dbghelp interface. skip it for now */
2244 break;
2245 case DW_TAG_label:
2246 dwarf2_parse_subprogram_label(subpgm, child);
2247 break;
2248 case DW_TAG_class_type:
2249 case DW_TAG_structure_type:
2250 case DW_TAG_union_type:
2251 case DW_TAG_enumeration_type:
2252 case DW_TAG_typedef:
2253 /* the type referred to will be loaded when we need it, so skip it */
2254 break;
2255 default:
2256 FIXME("Unhandled Tag type 0x%lx at %s\n",
2257 child->abbrev->tag, dwarf2_debug_di(di));
2261 symt_close_func_block(subpgm->ctx->module_ctx->module, subpgm->current_func, subpgm->current_block, 0);
2262 subpgm->current_block = symt_check_tag(subpgm->current_block->container, SymTagBlock) ?
2263 (struct symt_block*)subpgm->current_block->container : NULL;
2266 static struct symt* dwarf2_parse_subprogram(dwarf2_debug_info_t* di)
2268 struct attribute name;
2269 struct addr_range* addr_ranges;
2270 unsigned num_addr_ranges;
2271 struct attribute is_decl;
2272 struct attribute inline_flags;
2273 struct symt* ret_type;
2274 struct symt_function_signature* sig_type;
2275 dwarf2_subprogram_t subpgm;
2276 struct vector* children;
2277 dwarf2_debug_info_t* child;
2278 unsigned int i;
2280 if (di->symt) return di->symt;
2282 TRACE("%s\n", dwarf2_debug_di(di));
2284 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2286 WARN("No name for function... dropping function\n");
2287 return NULL;
2289 /* if it's an abstract representation of an inline function, there should be
2290 * a concrete object that we'll handle
2292 if (dwarf2_find_attribute(di, DW_AT_inline, &inline_flags) &&
2293 inline_flags.gotten_from == attr_direct &&
2294 inline_flags.u.uvalue != DW_INL_not_inlined)
2296 TRACE("Function %s declared as inlined (%ld)... skipping\n",
2297 debugstr_a(name.u.string), inline_flags.u.uvalue);
2298 return NULL;
2301 if (dwarf2_find_attribute(di, DW_AT_declaration, &is_decl) &&
2302 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
2304 /* it's a real declaration, skip it */
2305 return NULL;
2307 if ((addr_ranges = dwarf2_get_ranges(di, &num_addr_ranges)) == NULL)
2309 WARN("cannot get range for %s\n", debugstr_a(name.u.string));
2310 return NULL;
2312 /* As functions (defined as inline assembly) get debug info with dwarf
2313 * (not the case for stabs), we just drop Wine's thunks here...
2314 * Actual thunks will be created in elf_module from the symbol table
2316 if (elf_is_in_thunk_area(di->unit_ctx->module_ctx->load_offset + addr_ranges[0].low, di->unit_ctx->module_ctx->thunks) >= 0)
2317 return NULL;
2318 ret_type = dwarf2_lookup_type(di);
2320 /* FIXME: assuming C source code */
2321 sig_type = symt_new_function_signature(di->unit_ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2322 subpgm.top_func = symt_new_function(di->unit_ctx->module_ctx->module, di->unit_ctx->compiland,
2323 dwarf2_get_cpp_name(di, name.u.string),
2324 addr_ranges[0].low, addr_ranges[0].high - addr_ranges[0].low, &sig_type->symt);
2325 if (num_addr_ranges > 1)
2326 WARN("Function %s has multiple address ranges, only using the first one\n", name.u.string);
2327 free(addr_ranges);
2329 subpgm.current_func = subpgm.top_func;
2330 di->symt = &subpgm.top_func->symt;
2331 subpgm.ctx = di->unit_ctx;
2332 if (!dwarf2_compute_location_attr(di->unit_ctx, di, DW_AT_frame_base,
2333 &subpgm.frame, NULL))
2335 /* on stack !! */
2336 subpgm.frame.kind = loc_regrel;
2337 subpgm.frame.reg = di->unit_ctx->module_ctx->module->cpu->frame_regno;
2338 subpgm.frame.offset = 0;
2340 subpgm.non_computed_variable = FALSE;
2341 subpgm.current_block = NULL;
2343 children = dwarf2_get_di_children(di);
2344 if (children) for (i = 0; i < vector_length(children); i++)
2346 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2348 switch (child->abbrev->tag)
2350 case DW_TAG_variable:
2351 case DW_TAG_formal_parameter:
2352 dwarf2_parse_variable(&subpgm, child);
2353 break;
2354 case DW_TAG_lexical_block:
2355 dwarf2_parse_subprogram_block(&subpgm, child);
2356 break;
2357 case DW_TAG_inlined_subroutine:
2358 dwarf2_parse_inlined_subroutine(&subpgm, child);
2359 break;
2360 case DW_TAG_pointer_type:
2361 dwarf2_parse_pointer_type(child);
2362 break;
2363 case DW_TAG_const_type:
2364 dwarf2_parse_const_type(child);
2365 break;
2366 case DW_TAG_subprogram:
2367 /* FIXME: likely a declaration (to be checked)
2368 * skip it for now
2370 break;
2371 case DW_TAG_label:
2372 dwarf2_parse_subprogram_label(&subpgm, child);
2373 break;
2374 case DW_TAG_class_type:
2375 case DW_TAG_structure_type:
2376 case DW_TAG_union_type:
2377 case DW_TAG_enumeration_type:
2378 case DW_TAG_typedef:
2379 /* the type referred to will be loaded when we need it, so skip it */
2380 break;
2381 case DW_TAG_unspecified_parameters:
2382 case DW_TAG_template_type_param:
2383 case DW_TAG_template_value_param:
2384 case DW_TAG_GNU_call_site:
2385 case DW_TAG_GNU_template_parameter_pack:
2386 case DW_TAG_GNU_formal_parameter_pack:
2387 /* FIXME: no support in dbghelp's internals so far */
2388 break;
2389 default:
2390 FIXME("Unhandled Tag type 0x%lx at %s\n",
2391 child->abbrev->tag, dwarf2_debug_di(di));
2395 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2397 symt_add_function_point(di->unit_ctx->module_ctx->module, subpgm.top_func, SymTagCustom,
2398 &subpgm.frame, NULL);
2401 return di->symt;
2404 static struct symt* dwarf2_parse_subroutine_type(dwarf2_debug_info_t* di)
2406 struct symt* ret_type;
2407 struct symt_function_signature* sig_type;
2408 struct vector* children;
2409 dwarf2_debug_info_t* child;
2410 unsigned int i;
2412 if (di->symt) return di->symt;
2414 TRACE("%s\n", dwarf2_debug_di(di));
2416 ret_type = dwarf2_lookup_type(di);
2418 /* FIXME: assuming C source code */
2419 sig_type = symt_new_function_signature(di->unit_ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2421 children = dwarf2_get_di_children(di);
2422 if (children) for (i = 0; i < vector_length(children); i++)
2424 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2426 switch (child->abbrev->tag)
2428 case DW_TAG_formal_parameter:
2429 symt_add_function_signature_parameter(di->unit_ctx->module_ctx->module, sig_type,
2430 dwarf2_lookup_type(child));
2431 break;
2432 case DW_TAG_unspecified_parameters:
2433 WARN("Unsupported unspecified parameters\n");
2434 break;
2438 return di->symt = &sig_type->symt;
2441 static void dwarf2_parse_namespace(dwarf2_debug_info_t* di)
2443 struct vector* children;
2444 dwarf2_debug_info_t* child;
2445 unsigned int i;
2447 if (di->symt) return;
2449 TRACE("%s\n", dwarf2_debug_di(di));
2451 di->symt = di->unit_ctx->module_ctx->symt_cache[sc_void];
2453 children = dwarf2_get_di_children(di);
2454 if (children) for (i = 0; i < vector_length(children); i++)
2456 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2457 dwarf2_load_one_entry(child);
2461 static void dwarf2_parse_imported_unit(dwarf2_debug_info_t* di)
2463 struct attribute imp;
2465 if (di->symt) return;
2467 TRACE("%s\n", dwarf2_debug_di(di));
2469 if (dwarf2_find_attribute(di, DW_AT_import, &imp))
2471 dwarf2_debug_info_t* jmp = dwarf2_jump_to_debug_info(&imp);
2472 if (jmp) di->symt = jmp->symt;
2473 else FIXME("Couldn't load imported CU\n");
2475 else
2476 FIXME("Couldn't find import attribute\n");
2479 static void dwarf2_load_one_entry(dwarf2_debug_info_t* di)
2481 switch (di->abbrev->tag)
2483 case DW_TAG_typedef:
2484 dwarf2_parse_typedef(di);
2485 break;
2486 case DW_TAG_base_type:
2487 dwarf2_parse_base_type(di);
2488 break;
2489 case DW_TAG_pointer_type:
2490 dwarf2_parse_pointer_type(di);
2491 break;
2492 case DW_TAG_class_type:
2493 dwarf2_parse_udt_type(di, UdtClass);
2494 break;
2495 case DW_TAG_structure_type:
2496 dwarf2_parse_udt_type(di, UdtStruct);
2497 break;
2498 case DW_TAG_union_type:
2499 dwarf2_parse_udt_type(di, UdtUnion);
2500 break;
2501 case DW_TAG_array_type:
2502 dwarf2_parse_array_type(di);
2503 break;
2504 case DW_TAG_const_type:
2505 dwarf2_parse_const_type(di);
2506 break;
2507 case DW_TAG_volatile_type:
2508 dwarf2_parse_volatile_type(di);
2509 break;
2510 case DW_TAG_restrict_type:
2511 dwarf2_parse_restrict_type(di);
2512 break;
2513 case DW_TAG_unspecified_type:
2514 dwarf2_parse_unspecified_type(di);
2515 break;
2516 case DW_TAG_reference_type:
2517 case DW_TAG_rvalue_reference_type:
2518 dwarf2_parse_reference_type(di);
2519 break;
2520 case DW_TAG_enumeration_type:
2521 dwarf2_parse_enumeration_type(di);
2522 break;
2523 case DW_TAG_subprogram:
2524 dwarf2_parse_subprogram(di);
2525 break;
2526 case DW_TAG_subroutine_type:
2527 dwarf2_parse_subroutine_type(di);
2528 break;
2529 case DW_TAG_variable:
2531 dwarf2_subprogram_t subpgm;
2533 subpgm.ctx = di->unit_ctx;
2534 subpgm.top_func = subpgm.current_func = NULL;
2535 subpgm.current_block = NULL;
2536 subpgm.frame.kind = loc_absolute;
2537 subpgm.frame.offset = 0;
2538 subpgm.frame.reg = Wine_DW_no_register;
2539 dwarf2_parse_variable(&subpgm, di);
2541 break;
2542 case DW_TAG_namespace:
2543 dwarf2_parse_namespace(di);
2544 break;
2545 case DW_TAG_subrange_type:
2546 dwarf2_parse_subrange_type(di);
2547 break;
2548 case DW_TAG_imported_unit:
2549 dwarf2_parse_imported_unit(di);
2550 break;
2551 /* keep it silent until we need DW_OP_call_xxx support */
2552 case DW_TAG_dwarf_procedure:
2553 /* silence a couple of non-C language defines (mainly C++ but others too) */
2554 case DW_TAG_imported_module:
2555 case DW_TAG_imported_declaration:
2556 case DW_TAG_interface_type:
2557 case DW_TAG_module:
2558 case DW_TAG_ptr_to_member_type:
2559 break;
2560 default:
2561 FIXME("Unhandled Tag type 0x%lx at %s\n",
2562 di->abbrev->tag, dwarf2_debug_di(di));
2566 static void dwarf2_set_line_number(struct module* module, ULONG_PTR address,
2567 const struct vector* v, unsigned file, unsigned line)
2569 struct symt_function* func;
2570 struct symt_inlinesite* inlined;
2571 struct symt_ht* symt;
2572 unsigned* psrc;
2574 if (!file || !(psrc = vector_at(v, file - 1))) return;
2576 TRACE("%s %lx %s %u\n",
2577 debugstr_w(module->modulename), address, debugstr_a(source_get(module, *psrc)), line);
2578 symt = symt_find_nearest(module, address);
2579 if (symt_check_tag(&symt->symt, SymTagFunction))
2581 func = (struct symt_function*)symt;
2582 for (inlined = func->next_inlinesite; inlined; inlined = inlined->func.next_inlinesite)
2584 int i;
2585 for (i = 0; i < inlined->vranges.num_elts; ++i)
2587 struct addr_range* ar = (struct addr_range*)vector_at(&inlined->vranges, i);
2588 if (ar->low <= address && address < ar->high)
2590 symt_add_func_line(module, &inlined->func, *psrc, line, address);
2591 return; /* only add to lowest matching inline site */
2595 symt_add_func_line(module, func, *psrc, line, address);
2599 static BOOL dwarf2_parse_line_numbers(dwarf2_parse_context_t* ctx,
2600 const char* compile_dir,
2601 ULONG_PTR offset)
2603 dwarf2_traverse_context_t traverse;
2604 ULONG_PTR length;
2605 unsigned insn_size, version, default_stmt;
2606 unsigned line_range, opcode_base;
2607 int line_base;
2608 unsigned char offset_size;
2609 const unsigned char* opcode_len;
2610 struct vector dirs;
2611 struct vector files;
2612 const char** p;
2614 /* section with line numbers stripped */
2615 if (ctx->module_ctx->sections[section_line].address == IMAGE_NO_MAP)
2616 return FALSE;
2618 if (offset + 4 > ctx->module_ctx->sections[section_line].size)
2620 WARN("out of bounds offset\n");
2621 return FALSE;
2623 traverse.data = ctx->module_ctx->sections[section_line].address + offset;
2624 traverse.end_data = ctx->module_ctx->sections[section_line].address + ctx->module_ctx->sections[section_line].size;
2626 length = dwarf2_parse_3264(&traverse, &offset_size);
2627 if (offset_size != ctx->head.offset_size)
2629 WARN("Mismatch in 32/64 bit format\n");
2630 return FALSE;
2632 traverse.end_data = traverse.data + length;
2634 if (traverse.end_data > ctx->module_ctx->sections[section_line].address + ctx->module_ctx->sections[section_line].size)
2636 WARN("out of bounds header\n");
2637 return FALSE;
2639 version = dwarf2_parse_u2(&traverse);
2640 dwarf2_parse_offset(&traverse, offset_size); /* header_len */
2641 insn_size = dwarf2_parse_byte(&traverse);
2642 if (version >= 4)
2643 dwarf2_parse_byte(&traverse); /* max_operations_per_instructions */
2644 default_stmt = dwarf2_parse_byte(&traverse);
2645 line_base = (signed char)dwarf2_parse_byte(&traverse);
2646 line_range = dwarf2_parse_byte(&traverse);
2647 opcode_base = dwarf2_parse_byte(&traverse);
2649 opcode_len = traverse.data;
2650 traverse.data += opcode_base - 1;
2652 vector_init(&dirs, sizeof(const char*), 4);
2653 p = vector_add(&dirs, &ctx->pool);
2654 *p = compile_dir ? compile_dir : ".";
2655 while (traverse.data < traverse.end_data && *traverse.data)
2657 const char* rel = (const char*)traverse.data;
2658 unsigned rellen = strlen(rel);
2659 TRACE("Got include %s\n", debugstr_a(rel));
2660 traverse.data += rellen + 1;
2661 p = vector_add(&dirs, &ctx->pool);
2663 if (*rel == '/' || !compile_dir || !*compile_dir)
2664 *p = rel;
2665 else
2667 /* include directory relative to compile directory */
2668 unsigned baselen = strlen(compile_dir);
2669 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2670 strcpy(tmp, compile_dir);
2671 if (baselen && tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2672 strcpy(&tmp[baselen], rel);
2673 *p = tmp;
2677 traverse.data++;
2679 vector_init(&files, sizeof(unsigned), 16);
2680 while (traverse.data < traverse.end_data && *traverse.data)
2682 unsigned int dir_index, mod_time;
2683 const char* name;
2684 const char* dir;
2685 unsigned* psrc;
2687 name = (const char*)traverse.data;
2688 traverse.data += strlen(name) + 1;
2689 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2690 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2691 length = dwarf2_leb128_as_unsigned(&traverse);
2692 dir = *(const char**)vector_at(&dirs, dir_index);
2693 TRACE("Got file %s/%s (%u,%lu)\n", debugstr_a(dir), debugstr_a(name), mod_time, length);
2694 psrc = vector_add(&files, &ctx->pool);
2695 *psrc = source_new(ctx->module_ctx->module, dir, name);
2697 traverse.data++;
2699 while (traverse.data < traverse.end_data && *traverse.data)
2701 ULONG_PTR address = 0;
2702 unsigned file = 1;
2703 unsigned line = 1;
2704 unsigned is_stmt = default_stmt;
2705 BOOL end_sequence = FALSE;
2706 unsigned opcode, extopcode, i;
2708 while (!end_sequence)
2710 opcode = dwarf2_parse_byte(&traverse);
2711 TRACE("Got opcode %x\n", opcode);
2713 if (opcode >= opcode_base)
2715 unsigned delta = opcode - opcode_base;
2717 address += (delta / line_range) * insn_size;
2718 line += line_base + (delta % line_range);
2719 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2721 else
2723 switch (opcode)
2725 case DW_LNS_copy:
2726 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2727 break;
2728 case DW_LNS_advance_pc:
2729 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2730 break;
2731 case DW_LNS_advance_line:
2732 line += dwarf2_leb128_as_signed(&traverse);
2733 break;
2734 case DW_LNS_set_file:
2735 file = dwarf2_leb128_as_unsigned(&traverse);
2736 break;
2737 case DW_LNS_set_column:
2738 dwarf2_leb128_as_unsigned(&traverse);
2739 break;
2740 case DW_LNS_negate_stmt:
2741 is_stmt = !is_stmt;
2742 break;
2743 case DW_LNS_set_basic_block:
2744 break;
2745 case DW_LNS_const_add_pc:
2746 address += ((255 - opcode_base) / line_range) * insn_size;
2747 break;
2748 case DW_LNS_fixed_advance_pc:
2749 address += dwarf2_parse_u2(&traverse);
2750 break;
2751 case DW_LNS_extended_op:
2752 dwarf2_leb128_as_unsigned(&traverse);
2753 extopcode = dwarf2_parse_byte(&traverse);
2754 switch (extopcode)
2756 case DW_LNE_end_sequence:
2757 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2758 end_sequence = TRUE;
2759 break;
2760 case DW_LNE_set_address:
2761 address = ctx->module_ctx->load_offset + dwarf2_parse_addr_head(&traverse, &ctx->head);
2762 break;
2763 case DW_LNE_define_file:
2764 FIXME("not handled define file %s\n", debugstr_a((char *)traverse.data));
2765 traverse.data += strlen((const char *)traverse.data) + 1;
2766 dwarf2_leb128_as_unsigned(&traverse);
2767 dwarf2_leb128_as_unsigned(&traverse);
2768 dwarf2_leb128_as_unsigned(&traverse);
2769 break;
2770 case DW_LNE_set_discriminator:
2772 unsigned descr;
2774 descr = dwarf2_leb128_as_unsigned(&traverse);
2775 WARN("not handled discriminator %x\n", descr);
2777 break;
2778 default:
2779 FIXME("Unsupported extended opcode %x\n", extopcode);
2780 break;
2782 break;
2783 default:
2784 WARN("Unsupported opcode %x\n", opcode);
2785 for (i = 0; i < opcode_len[opcode]; i++)
2786 dwarf2_leb128_as_unsigned(&traverse);
2787 break;
2792 return TRUE;
2795 unsigned dwarf2_cache_cuhead(struct dwarf2_module_info_s* module, struct symt_compiland* c, const dwarf2_cuhead_t* head)
2797 dwarf2_cuhead_t* ah;
2798 unsigned i;
2799 for (i = 0; i < module->num_cuheads; ++i)
2801 if (memcmp(module->cuheads[i], head, sizeof(*head)) == 0)
2803 c->user = module->cuheads[i];
2804 return TRUE;
2807 if (!(ah = pool_alloc(&c->container->module->pool, sizeof(*head)))) return FALSE;
2808 memcpy(ah, head, sizeof(*head));
2809 module->cuheads = realloc(module->cuheads, ++module->num_cuheads * sizeof(head));
2810 module->cuheads[module->num_cuheads - 1] = ah;
2811 c->user = ah;
2812 return TRUE;
2815 static dwarf2_parse_context_t* dwarf2_locate_cu(dwarf2_parse_module_context_t* module_ctx, ULONG_PTR ref)
2817 unsigned i;
2818 dwarf2_parse_context_t* ctx;
2819 const BYTE* where;
2820 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
2822 ctx = vector_at(&module_ctx->unit_contexts, i);
2823 where = module_ctx->sections[ctx->section].address + ref;
2824 if (where >= ctx->traverse_DIE.data && where < ctx->traverse_DIE.end_data)
2825 return ctx;
2827 FIXME("Couldn't find ref 0x%lx inside sect\n", ref);
2828 return NULL;
2831 static BOOL dwarf2_parse_compilation_unit_head(dwarf2_parse_context_t* ctx,
2832 dwarf2_traverse_context_t* mod_ctx)
2834 dwarf2_traverse_context_t abbrev_ctx;
2835 const unsigned char* comp_unit_start = mod_ctx->data;
2836 ULONG_PTR cu_length;
2837 ULONG_PTR cu_abbrev_offset;
2838 /* FIXME this is a temporary configuration while adding support for dwarf3&4 bits */
2839 static LONG max_supported_dwarf_version = 0;
2841 cu_length = dwarf2_parse_3264(mod_ctx, &ctx->head.offset_size);
2843 ctx->traverse_DIE.data = mod_ctx->data;
2844 ctx->traverse_DIE.end_data = mod_ctx->data + cu_length;
2845 mod_ctx->data += cu_length;
2846 ctx->head.version = dwarf2_parse_u2(&ctx->traverse_DIE);
2847 cu_abbrev_offset = dwarf2_parse_offset(&ctx->traverse_DIE, ctx->head.offset_size);
2848 ctx->head.word_size = dwarf2_parse_byte(&ctx->traverse_DIE);
2849 ctx->status = UNIT_ERROR;
2851 TRACE("Compilation Unit Header found at 0x%x:\n",
2852 (int)(comp_unit_start - ctx->module_ctx->sections[section_debug].address));
2853 TRACE("- length: %lu\n", cu_length);
2854 TRACE("- version: %u\n", ctx->head.version);
2855 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2856 TRACE("- word_size: %u\n", ctx->head.word_size);
2857 TRACE("- offset_size: %u\n", ctx->head.offset_size);
2859 if (ctx->head.version >= 2)
2860 ctx->module_ctx->cu_versions |= 1 << (ctx->head.version - 2);
2861 if (max_supported_dwarf_version == 0)
2863 char* env = getenv("DBGHELP_DWARF_VERSION");
2864 LONG v = env ? atol(env) : 4;
2865 max_supported_dwarf_version = (v >= 2 && v <= 4) ? v : 4;
2868 if (ctx->head.version < 2 || ctx->head.version > max_supported_dwarf_version)
2870 WARN("DWARF version %d isn't supported. Wine dbghelp only supports DWARF 2 up to %u.\n",
2871 ctx->head.version, max_supported_dwarf_version);
2872 return FALSE;
2875 pool_init(&ctx->pool, 65536);
2876 ctx->section = section_debug;
2877 ctx->ref_offset = comp_unit_start - ctx->module_ctx->sections[section_debug].address;
2878 ctx->cpp_name = NULL;
2879 ctx->status = UNIT_NOTLOADED;
2881 abbrev_ctx.data = ctx->module_ctx->sections[section_abbrev].address + cu_abbrev_offset;
2882 abbrev_ctx.end_data = ctx->module_ctx->sections[section_abbrev].address + ctx->module_ctx->sections[section_abbrev].size;
2883 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx->abbrev_table, &ctx->pool);
2885 sparse_array_init(&ctx->debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2886 return TRUE;
2889 static BOOL dwarf2_parse_compilation_unit(dwarf2_parse_context_t* ctx)
2891 dwarf2_debug_info_t* di;
2892 dwarf2_traverse_context_t cu_ctx = ctx->traverse_DIE;
2893 BOOL ret = FALSE;
2895 switch (ctx->status)
2897 case UNIT_ERROR: return FALSE;
2898 case UNIT_BEINGLOADED:
2899 FIXME("Circular deps on CU\n");
2900 /* fall through */
2901 case UNIT_LOADED:
2902 case UNIT_LOADED_FAIL:
2903 return TRUE;
2904 case UNIT_NOTLOADED: break;
2907 ctx->status = UNIT_BEINGLOADED;
2908 if (dwarf2_read_one_debug_info(ctx, &cu_ctx, NULL, &di))
2910 if (di->abbrev->tag == DW_TAG_compile_unit || di->abbrev->tag == DW_TAG_partial_unit)
2912 struct attribute name;
2913 struct vector* children;
2914 dwarf2_debug_info_t* child = NULL;
2915 unsigned int i;
2916 struct attribute stmt_list, low_pc;
2917 struct attribute comp_dir;
2919 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2920 name.u.string = NULL;
2922 /* get working directory of current compilation unit */
2923 if (!dwarf2_find_attribute(di, DW_AT_comp_dir, &comp_dir))
2924 comp_dir.u.string = NULL;
2926 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc))
2927 low_pc.u.uvalue = 0;
2928 ctx->compiland = symt_new_compiland(ctx->module_ctx->module, ctx->module_ctx->load_offset + low_pc.u.uvalue,
2929 source_new(ctx->module_ctx->module, comp_dir.u.string, name.u.string));
2930 dwarf2_cache_cuhead(ctx->module_ctx->module->format_info[DFI_DWARF]->u.dwarf2_info, ctx->compiland, &ctx->head);
2931 di->symt = &ctx->compiland->symt;
2932 children = dwarf2_get_di_children(di);
2933 if (children) for (i = 0; i < vector_length(children); i++)
2935 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2936 dwarf2_load_one_entry(child);
2938 if (dwarf2_find_attribute(di, DW_AT_stmt_list, &stmt_list))
2940 if (dwarf2_parse_line_numbers(ctx, comp_dir.u.string, stmt_list.u.uvalue))
2941 ctx->module_ctx->module->module.LineNumbers = TRUE;
2943 ctx->status = UNIT_LOADED;
2944 ret = TRUE;
2946 else FIXME("Should have a compilation unit here %lu\n", di->abbrev->tag);
2948 if (ctx->status == UNIT_BEINGLOADED) ctx->status = UNIT_LOADED_FAIL;
2949 return ret;
2952 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const dwarf2_cuhead_t* head,
2953 const BYTE* start, ULONG_PTR ip, dwarf2_traverse_context_t* lctx)
2955 DWORD_PTR beg, end;
2956 const BYTE* ptr = start;
2957 DWORD len;
2959 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2961 beg = dwarf2_get_addr(ptr, head->word_size); ptr += head->word_size;
2962 end = dwarf2_get_addr(ptr, head->word_size); ptr += head->word_size;
2963 if (!beg && !end) break;
2964 len = dwarf2_get_u2(ptr); ptr += 2;
2966 if (beg <= ip && ip < end)
2968 lctx->data = ptr;
2969 lctx->end_data = ptr + len;
2970 return TRUE;
2972 ptr += len;
2974 WARN("Couldn't find ip in location list\n");
2975 return FALSE;
2978 static const dwarf2_cuhead_t* get_cuhead_from_func(const struct symt_function* func)
2980 if (symt_check_tag(&func->symt, SymTagInlineSite))
2981 func = symt_get_function_from_inlined((struct symt_inlinesite*)func);
2982 if (symt_check_tag(&func->symt, SymTagFunction) && symt_check_tag(func->container, SymTagCompiland))
2984 struct symt_compiland* c = (struct symt_compiland*)func->container;
2985 return (const dwarf2_cuhead_t*)c->user;
2987 FIXME("Should have a compilation unit head\n");
2988 return NULL;
2991 static BOOL compute_call_frame_cfa(struct module* module, ULONG_PTR ip, struct location* frame);
2993 static enum location_error loc_compute_frame(struct process* pcs,
2994 const struct module_format* modfmt,
2995 const struct symt_function* func,
2996 DWORD_PTR ip, const dwarf2_cuhead_t* head,
2997 struct location* frame)
2999 struct symt** psym = NULL;
3000 struct location* pframe;
3001 dwarf2_traverse_context_t lctx;
3002 enum location_error err;
3003 unsigned int i;
3005 for (i=0; i<vector_length(&func->vchildren); i++)
3007 psym = vector_at(&func->vchildren, i);
3008 if (psym && symt_check_tag(*psym, SymTagCustom))
3010 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
3012 /* First, recompute the frame information, if needed */
3013 switch (pframe->kind)
3015 case loc_regrel:
3016 case loc_register:
3017 *frame = *pframe;
3018 break;
3019 case loc_dwarf2_location_list:
3020 WARN("Searching loclist for %s\n", debugstr_a(func->hash_elt.name));
3021 if (!dwarf2_lookup_loclist(modfmt, head,
3022 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
3023 ip, &lctx))
3024 return loc_err_out_of_scope;
3025 if ((err = compute_location(modfmt->module, head,
3026 &lctx, frame, pcs->handle, NULL)) < 0) return err;
3027 if (frame->kind >= loc_user)
3029 WARN("Couldn't compute runtime frame location\n");
3030 return loc_err_too_complex;
3032 break;
3033 case loc_dwarf2_frame_cfa:
3034 if (!compute_call_frame_cfa(modfmt->module, ip + ((struct symt_compiland*)func->container)->address, frame)) return loc_err_internal;
3035 break;
3036 default:
3037 WARN("Unsupported frame kind %d\n", pframe->kind);
3038 return loc_err_internal;
3040 return 0;
3043 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
3044 return loc_err_internal;
3047 enum reg_rule
3049 RULE_UNSET, /* not set at all */
3050 RULE_UNDEFINED, /* undefined value */
3051 RULE_SAME, /* same value as previous frame */
3052 RULE_CFA_OFFSET, /* stored at cfa offset */
3053 RULE_OTHER_REG, /* stored in other register */
3054 RULE_EXPRESSION, /* address specified by expression */
3055 RULE_VAL_EXPRESSION /* value specified by expression */
3058 /* make it large enough for all CPUs */
3059 #define NB_FRAME_REGS 64
3060 #define MAX_SAVED_STATES 16
3062 struct frame_state
3064 ULONG_PTR cfa_offset;
3065 unsigned char cfa_reg;
3066 enum reg_rule cfa_rule;
3067 enum reg_rule rules[NB_FRAME_REGS];
3068 ULONG_PTR regs[NB_FRAME_REGS];
3071 struct frame_info
3073 ULONG_PTR ip;
3074 ULONG_PTR code_align;
3075 LONG_PTR data_align;
3076 unsigned char retaddr_reg;
3077 unsigned char fde_encoding;
3078 unsigned char lsda_encoding;
3079 unsigned char signal_frame;
3080 unsigned char aug_z_format;
3081 unsigned char state_sp;
3082 struct frame_state state;
3083 struct frame_state state_stack[MAX_SAVED_STATES];
3086 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding, unsigned char word_size)
3088 ULONG_PTR base;
3090 if (encoding == DW_EH_PE_omit) return 0;
3092 switch (encoding & 0xf0)
3094 case DW_EH_PE_abs:
3095 base = 0;
3096 break;
3097 case DW_EH_PE_pcrel:
3098 base = (ULONG_PTR)ctx->data;
3099 break;
3100 default:
3101 FIXME("unsupported encoding %02x\n", encoding);
3102 return 0;
3105 switch (encoding & 0x0f)
3107 case DW_EH_PE_native:
3108 return base + dwarf2_parse_addr(ctx, word_size);
3109 case DW_EH_PE_leb128:
3110 return base + dwarf2_leb128_as_unsigned(ctx);
3111 case DW_EH_PE_data2:
3112 return base + dwarf2_parse_u2(ctx);
3113 case DW_EH_PE_data4:
3114 return base + dwarf2_parse_u4(ctx);
3115 case DW_EH_PE_data8:
3116 return base + dwarf2_parse_u8(ctx);
3117 case DW_EH_PE_signed|DW_EH_PE_leb128:
3118 return base + dwarf2_leb128_as_signed(ctx);
3119 case DW_EH_PE_signed|DW_EH_PE_data2:
3120 return base + (signed short)dwarf2_parse_u2(ctx);
3121 case DW_EH_PE_signed|DW_EH_PE_data4:
3122 return base + (signed int)dwarf2_parse_u4(ctx);
3123 case DW_EH_PE_signed|DW_EH_PE_data8:
3124 return base + (LONG64)dwarf2_parse_u8(ctx);
3125 default:
3126 FIXME("unsupported encoding %02x\n", encoding);
3127 return 0;
3131 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info, unsigned char word_size)
3133 unsigned char version;
3134 const char* augmentation;
3135 const unsigned char* end;
3136 ULONG_PTR len;
3138 memset(info, 0, sizeof(*info));
3139 info->lsda_encoding = DW_EH_PE_omit;
3140 info->aug_z_format = 0;
3142 /* parse the CIE first */
3143 version = dwarf2_parse_byte(ctx);
3144 if (version != 1 && version != 3 && version != 4)
3146 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
3147 return FALSE;
3149 augmentation = (const char*)ctx->data;
3150 ctx->data += strlen(augmentation) + 1;
3152 switch (version)
3154 case 4:
3155 /* skip 'address_size' and 'segment_size' */
3156 ctx->data += 2;
3157 /* fallthrough */
3158 case 1:
3159 case 3:
3160 info->code_align = dwarf2_leb128_as_unsigned(ctx);
3161 info->data_align = dwarf2_leb128_as_signed(ctx);
3162 info->retaddr_reg = version == 1 ? dwarf2_parse_byte(ctx) :dwarf2_leb128_as_unsigned(ctx);
3163 break;
3164 default:
3167 info->state.cfa_rule = RULE_CFA_OFFSET;
3169 end = NULL;
3170 TRACE("\tparsing augmentation %s\n", debugstr_a(augmentation));
3171 if (*augmentation) do
3173 switch (*augmentation)
3175 case 'z':
3176 len = dwarf2_leb128_as_unsigned(ctx);
3177 end = ctx->data + len;
3178 info->aug_z_format = 1;
3179 continue;
3180 case 'L':
3181 info->lsda_encoding = dwarf2_parse_byte(ctx);
3182 continue;
3183 case 'P':
3185 unsigned char encoding = dwarf2_parse_byte(ctx);
3186 /* throw away the indirect bit, as we don't care for the result */
3187 encoding &= ~DW_EH_PE_indirect;
3188 dwarf2_parse_augmentation_ptr(ctx, encoding, word_size); /* handler */
3189 continue;
3191 case 'R':
3192 info->fde_encoding = dwarf2_parse_byte(ctx);
3193 continue;
3194 case 'S':
3195 info->signal_frame = 1;
3196 continue;
3198 FIXME("unknown augmentation '%c'\n", *augmentation);
3199 if (!end) return FALSE;
3200 break;
3201 } while (*++augmentation);
3202 if (end) ctx->data = end;
3203 return TRUE;
3206 static BOOL dwarf2_get_cie(ULONG_PTR addr, struct module* module, DWORD_PTR delta,
3207 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
3208 struct frame_info* info, BOOL in_eh_frame)
3210 const unsigned char* ptr_blk;
3211 const unsigned char* cie_ptr;
3212 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
3213 ULONG_PTR len, id;
3214 ULONG_PTR start, range;
3215 ULONG_PTR cie_id;
3216 const BYTE* start_data = fde_ctx->data;
3217 unsigned char word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3218 unsigned char offset_size;
3220 /* skip 0-padding at beginning of section (alignment) */
3221 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
3223 if (dwarf2_parse_u4(fde_ctx))
3225 fde_ctx->data -= 4;
3226 break;
3229 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
3231 const unsigned char* st = fde_ctx->data;
3232 /* find the FDE for address addr (skip CIE) */
3233 len = dwarf2_parse_3264(fde_ctx, &offset_size);
3234 cie_id = in_eh_frame ? 0 : (offset_size == 4 ? DW_CIE_ID : (ULONG_PTR)DW64_CIE_ID);
3235 ptr_blk = fde_ctx->data + len;
3236 id = dwarf2_parse_offset(fde_ctx, offset_size);
3237 if (id == cie_id)
3239 last_cie_ptr = st;
3240 /* we need some bits out of the CIE in order to parse all contents */
3241 if (!parse_cie_details(fde_ctx, info, word_size)) return FALSE;
3242 cie_ctx->data = fde_ctx->data;
3243 cie_ctx->end_data = ptr_blk;
3244 continue;
3246 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
3247 if (cie_ptr != last_cie_ptr)
3249 last_cie_ptr = cie_ptr;
3250 cie_ctx->data = cie_ptr;
3251 cie_ctx->end_data = cie_ptr + (offset_size == 4 ? 4 : 4 + 8);
3252 cie_ctx->end_data += dwarf2_parse_3264(cie_ctx, &offset_size);
3254 if (dwarf2_parse_offset(cie_ctx, in_eh_frame ? word_size : offset_size) != cie_id)
3256 FIXME("wrong CIE pointer at %x from FDE %x\n",
3257 (unsigned)(cie_ptr - start_data),
3258 (unsigned)(fde_ctx->data - start_data));
3259 return FALSE;
3261 if (!parse_cie_details(cie_ctx, info, word_size)) return FALSE;
3263 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding, word_size);
3264 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F, word_size);
3266 if (addr >= start && addr < start + range)
3268 /* reset the FDE context */
3269 fde_ctx->end_data = ptr_blk;
3271 info->ip = start;
3272 return TRUE;
3275 return FALSE;
3278 static int valid_reg(ULONG_PTR reg)
3280 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
3281 return (reg < NB_FRAME_REGS);
3284 static void execute_cfa_instructions(struct module* module, dwarf2_traverse_context_t* ctx,
3285 ULONG_PTR last_ip, struct frame_info *info)
3287 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
3289 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
3291 if (op & 0xc0)
3293 switch (op & 0xc0)
3295 case DW_CFA_advance_loc:
3297 ULONG_PTR offset = (op & 0x3f) * info->code_align;
3298 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
3299 info->ip += offset;
3300 break;
3302 case DW_CFA_offset:
3304 ULONG_PTR reg = op & 0x3f;
3305 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
3306 if (!valid_reg(reg)) break;
3307 TRACE("%lx: DW_CFA_offset %s, %ld\n",
3308 info->ip,
3309 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3310 offset);
3311 info->state.regs[reg] = offset;
3312 info->state.rules[reg] = RULE_CFA_OFFSET;
3313 break;
3315 case DW_CFA_restore:
3317 ULONG_PTR reg = op & 0x3f;
3318 if (!valid_reg(reg)) break;
3319 TRACE("%lx: DW_CFA_restore %s\n",
3320 info->ip,
3321 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3322 info->state.rules[reg] = RULE_UNSET;
3323 break;
3327 else switch (op)
3329 case DW_CFA_nop:
3330 break;
3331 case DW_CFA_set_loc:
3333 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding,
3334 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
3335 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
3336 info->ip = loc;
3337 break;
3339 case DW_CFA_advance_loc1:
3341 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
3342 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
3343 info->ip += offset;
3344 break;
3346 case DW_CFA_advance_loc2:
3348 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
3349 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
3350 info->ip += offset;
3351 break;
3353 case DW_CFA_advance_loc4:
3355 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
3356 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
3357 info->ip += offset;
3358 break;
3360 case DW_CFA_offset_extended:
3361 case DW_CFA_offset_extended_sf:
3363 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3364 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
3365 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3366 if (!valid_reg(reg)) break;
3367 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
3368 info->ip,
3369 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3370 offset);
3371 info->state.regs[reg] = offset;
3372 info->state.rules[reg] = RULE_CFA_OFFSET;
3373 break;
3375 case DW_CFA_restore_extended:
3377 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3378 if (!valid_reg(reg)) break;
3379 TRACE("%lx: DW_CFA_restore_extended %s\n",
3380 info->ip,
3381 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3382 info->state.rules[reg] = RULE_UNSET;
3383 break;
3385 case DW_CFA_undefined:
3387 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3388 if (!valid_reg(reg)) break;
3389 TRACE("%lx: DW_CFA_undefined %s\n",
3390 info->ip,
3391 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3392 info->state.rules[reg] = RULE_UNDEFINED;
3393 break;
3395 case DW_CFA_same_value:
3397 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3398 if (!valid_reg(reg)) break;
3399 TRACE("%lx: DW_CFA_same_value %s\n",
3400 info->ip,
3401 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3402 info->state.regs[reg] = reg;
3403 info->state.rules[reg] = RULE_SAME;
3404 break;
3406 case DW_CFA_register:
3408 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3409 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
3410 if (!valid_reg(reg) || !valid_reg(reg2)) break;
3411 TRACE("%lx: DW_CFA_register %s == %s\n",
3412 info->ip,
3413 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3414 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg2, module, TRUE)));
3415 info->state.regs[reg] = reg2;
3416 info->state.rules[reg] = RULE_OTHER_REG;
3417 break;
3419 case DW_CFA_remember_state:
3420 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
3421 if (info->state_sp >= MAX_SAVED_STATES)
3422 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
3423 else
3424 info->state_stack[info->state_sp++] = info->state;
3425 break;
3426 case DW_CFA_restore_state:
3427 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
3428 if (!info->state_sp)
3429 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
3430 else
3431 info->state = info->state_stack[--info->state_sp];
3432 break;
3433 case DW_CFA_def_cfa:
3434 case DW_CFA_def_cfa_sf:
3436 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3437 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
3438 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3439 if (!valid_reg(reg)) break;
3440 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
3441 info->ip,
3442 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3443 offset);
3444 info->state.cfa_reg = reg;
3445 info->state.cfa_offset = offset;
3446 info->state.cfa_rule = RULE_CFA_OFFSET;
3447 break;
3449 case DW_CFA_def_cfa_register:
3451 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3452 if (!valid_reg(reg)) break;
3453 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
3454 info->ip,
3455 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3456 info->state.cfa_reg = reg;
3457 info->state.cfa_rule = RULE_CFA_OFFSET;
3458 break;
3460 case DW_CFA_def_cfa_offset:
3461 case DW_CFA_def_cfa_offset_sf:
3463 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
3464 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3465 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
3466 info->state.cfa_offset = offset;
3467 info->state.cfa_rule = RULE_CFA_OFFSET;
3468 break;
3470 case DW_CFA_def_cfa_expression:
3472 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3473 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3474 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
3475 info->state.cfa_offset = expr;
3476 info->state.cfa_rule = RULE_VAL_EXPRESSION;
3477 ctx->data += len;
3478 break;
3480 case DW_CFA_expression:
3481 case DW_CFA_val_expression:
3483 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3484 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3485 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3486 if (!valid_reg(reg)) break;
3487 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
3488 info->ip, (op == DW_CFA_expression) ? "" : "val_",
3489 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3490 expr, expr + len);
3491 info->state.regs[reg] = expr;
3492 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
3493 ctx->data += len;
3494 break;
3496 case DW_CFA_GNU_args_size:
3497 /* FIXME: should check that GCC is the compiler for this CU */
3499 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
3500 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
3501 /* ignored */
3502 break;
3504 default:
3505 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
3506 break;
3511 /* retrieve a context register from its dwarf number */
3512 static DWORD64 get_context_reg(const struct module* module, struct cpu_stack_walk *csw, union ctx *context,
3513 ULONG_PTR dw_reg)
3515 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3516 void* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3518 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3519 if (sz == 8)
3520 return *(DWORD64 *)ptr;
3521 else if (sz == 4)
3522 return *(DWORD *)ptr;
3524 FIXME("unhandled size %d\n", sz);
3525 return 0;
3528 /* set a context register from its dwarf number */
3529 static void set_context_reg(const struct module* module, struct cpu_stack_walk* csw, union ctx *context,
3530 ULONG_PTR dw_reg, ULONG_PTR val, BOOL isdebuggee)
3532 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3533 ULONG_PTR* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3535 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3536 if (isdebuggee)
3538 char tmp[16];
3540 if (sz > sizeof(tmp))
3542 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
3543 return;
3545 if (!sw_read_mem(csw, val, tmp, sz))
3547 WARN("Couldn't read memory at %p\n", (void*)val);
3548 return;
3550 memcpy(ptr, tmp, sz);
3552 else
3554 if (sz != sizeof(ULONG_PTR))
3556 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
3557 return;
3559 *ptr = val;
3563 /* copy a register from one context to another using dwarf number */
3564 static void copy_context_reg(const struct module* module, struct cpu_stack_walk *csw,
3565 union ctx *dstcontext, ULONG_PTR dwregdst,
3566 union ctx *srccontext, ULONG_PTR dwregsrc)
3568 unsigned regdstno = csw->cpu->map_dwarf_register(dwregdst, module, TRUE), szdst;
3569 unsigned regsrcno = csw->cpu->map_dwarf_register(dwregsrc, module, TRUE), szsrc;
3570 ULONG_PTR* ptrdst = csw->cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3571 ULONG_PTR* ptrsrc = csw->cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3573 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3574 if (szdst != szsrc)
3576 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3577 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3578 return;
3580 memcpy(ptrdst, ptrsrc, szdst);
3583 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3584 const unsigned char* zp, union ctx *context)
3586 dwarf2_traverse_context_t ctx;
3587 ULONG_PTR reg, sz, tmp;
3588 DWORD64 stack[64];
3589 int sp = -1;
3590 ULONG_PTR len;
3592 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3593 ctx.data = zp;
3594 ctx.end_data = zp + 4;
3595 len = dwarf2_leb128_as_unsigned(&ctx);
3596 ctx.end_data = ctx.data + len;
3598 while (ctx.data < ctx.end_data)
3600 unsigned char opcode = dwarf2_parse_byte(&ctx);
3602 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3603 stack[++sp] = opcode - DW_OP_lit0;
3604 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3605 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_reg0);
3606 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3607 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_breg0)
3608 + dwarf2_leb128_as_signed(&ctx);
3609 else switch (opcode)
3611 case DW_OP_nop: break;
3612 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size); break;
3613 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3614 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3615 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3616 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3617 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3618 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3619 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3620 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3621 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3622 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3623 case DW_OP_deref:
3624 tmp = 0;
3625 if (!sw_read_mem(csw, stack[sp], &tmp, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size))
3627 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3628 tmp = 0;
3630 stack[sp] = tmp;
3631 break;
3632 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3633 case DW_OP_drop: sp--; break;
3634 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3635 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3636 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3637 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3638 case DW_OP_abs: stack[sp] = sizeof(stack[sp]) == 8 ? llabs((INT64)stack[sp]) : abs((INT32)stack[sp]); break;
3639 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3640 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3641 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3642 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3643 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3644 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3645 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3646 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3647 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3648 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3649 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3650 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3651 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3652 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3653 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3654 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3655 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3656 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3657 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3658 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3659 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3660 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3661 case DW_OP_GNU_encoded_addr:
3662 tmp = dwarf2_parse_byte(&ctx);
3663 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
3664 break;
3665 case DW_OP_regx:
3666 stack[++sp] = get_context_reg(module, csw, context, dwarf2_leb128_as_unsigned(&ctx));
3667 break;
3668 case DW_OP_bregx:
3669 reg = dwarf2_leb128_as_unsigned(&ctx);
3670 tmp = dwarf2_leb128_as_signed(&ctx);
3671 stack[++sp] = get_context_reg(module, csw, context, reg) + tmp;
3672 break;
3673 case DW_OP_deref_size:
3674 sz = dwarf2_parse_byte(&ctx);
3675 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3677 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3678 tmp = 0;
3680 /* do integral promotion */
3681 switch (sz)
3683 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3684 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3685 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3686 case 8: stack[sp] = tmp; break; /* FIXME: won't work on 32bit platform */
3687 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3689 break;
3690 default:
3691 FIXME("unhandled opcode %02x\n", opcode);
3694 return stack[sp];
3697 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3698 union ctx *context, struct frame_state *state, DWORD64 *cfa)
3700 unsigned int i;
3701 ULONG_PTR value;
3702 union ctx new_context = *context;
3704 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3705 switch (state->cfa_rule)
3707 case RULE_EXPRESSION:
3708 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3709 if (!sw_read_mem(csw, *cfa, cfa, csw->cpu->word_size))
3711 WARN("Couldn't read memory at %s\n", wine_dbgstr_longlong(*cfa));
3712 return;
3714 break;
3715 case RULE_VAL_EXPRESSION:
3716 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3717 break;
3718 default:
3719 *cfa = get_context_reg(module, csw, context, state->cfa_reg) + state->cfa_offset;
3720 break;
3722 if (!*cfa) return;
3724 for (i = 0; i < NB_FRAME_REGS; i++)
3726 switch (state->rules[i])
3728 case RULE_UNSET:
3729 case RULE_UNDEFINED:
3730 case RULE_SAME:
3731 break;
3732 case RULE_CFA_OFFSET:
3733 set_context_reg(module, csw, &new_context, i, *cfa + state->regs[i], TRUE);
3734 break;
3735 case RULE_OTHER_REG:
3736 copy_context_reg(module, csw, &new_context, i, context, state->regs[i]);
3737 break;
3738 case RULE_EXPRESSION:
3739 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3740 set_context_reg(module, csw, &new_context, i, value, TRUE);
3741 break;
3742 case RULE_VAL_EXPRESSION:
3743 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3744 set_context_reg(module, csw, &new_context, i, value, FALSE);
3745 break;
3748 *context = new_context;
3751 static BOOL dwarf2_fetch_frame_info(struct module* module, struct cpu* cpu, LONG_PTR ip, struct frame_info* info)
3753 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3754 struct module_format* modfmt;
3755 const unsigned char* end;
3756 DWORD_PTR delta;
3758 modfmt = module->format_info[DFI_DWARF];
3759 if (!modfmt) return FALSE;
3760 memset(info, 0, sizeof(*info));
3761 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3762 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3763 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3764 * in this process the IMAGE section at a different address as the one expected by
3765 * the image
3767 delta = module->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3768 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3769 if (!dwarf2_get_cie(ip, module, delta, &fde_ctx, &cie_ctx, info, TRUE))
3771 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3772 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3773 delta = module->reloc_delta;
3774 if (!dwarf2_get_cie(ip, module, delta, &fde_ctx, &cie_ctx, info, FALSE))
3776 TRACE("Couldn't find information for %lx\n", ip);
3777 return FALSE;
3781 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3782 ip, info->ip, info->code_align, info->data_align,
3783 cpu->fetch_regname(cpu->map_dwarf_register(info->retaddr_reg, module, TRUE)));
3785 if (ip != info->ip)
3787 execute_cfa_instructions(module, &cie_ctx, ip, info);
3789 if (info->aug_z_format) /* get length of augmentation data */
3791 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3792 end = fde_ctx.data + len;
3794 else end = NULL;
3795 dwarf2_parse_augmentation_ptr(&fde_ctx, info->lsda_encoding, modfmt->u.dwarf2_info->word_size); /* handler_data */
3796 if (end) fde_ctx.data = end;
3798 execute_cfa_instructions(module, &fde_ctx, ip, info);
3800 return TRUE;
3803 /***********************************************************************
3804 * dwarf2_virtual_unwind
3807 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk *csw, ULONG_PTR ip,
3808 union ctx *context, DWORD64 *cfa)
3810 struct module_pair pair;
3811 struct frame_info info;
3813 if (!module_init_pair(&pair, csw->hProcess, ip)) return FALSE;
3814 if (csw->cpu != pair.effective->cpu) FIXME("mismatch in cpu\n");
3815 if (!dwarf2_fetch_frame_info(pair.effective, csw->cpu, ip, &info)) return FALSE;
3817 /* if at very beginning of function, return and use default unwinder */
3818 if (ip == info.ip) return FALSE;
3820 /* if there is no information about retaddr, use default unwinder */
3821 if (info.state.rules[info.retaddr_reg] == RULE_UNSET) return FALSE;
3823 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3825 return TRUE;
3828 static BOOL compute_call_frame_cfa(struct module* module, ULONG_PTR ip, struct location* frame)
3830 struct frame_info info;
3832 if (!dwarf2_fetch_frame_info(module, module->cpu, ip, &info)) return FALSE;
3834 /* beginning of function, or no available dwarf information ? */
3835 if (ip == info.ip || info.state.rules[info.retaddr_reg] == RULE_UNSET)
3837 /* fake the default unwinder */
3838 frame->kind = loc_regrel;
3839 frame->reg = module->cpu->frame_regno;
3840 frame->offset = module->cpu->word_size; /* FIXME stack direction */
3842 else
3844 /* we expect to translate the call_frame_cfa into a regrel location...
3845 * that should cover most of the cases
3847 switch (info.state.cfa_rule)
3849 case RULE_EXPRESSION:
3850 FIXME("Too complex expression for frame_CFA resolution (RULE_EXPRESSION)\n");
3851 return FALSE;
3852 case RULE_VAL_EXPRESSION:
3853 FIXME("Too complex expression for frame_CFA resolution (RULE_VAL_EXPRESSION)\n");
3854 return FALSE;
3855 default:
3856 frame->kind = loc_regrel;
3857 frame->reg = module->cpu->map_dwarf_register(info.state.cfa_reg, module, TRUE);
3858 frame->offset = info.state.cfa_offset;
3859 break;
3862 return TRUE;
3865 static void dwarf2_location_compute(struct process* pcs,
3866 const struct module_format* modfmt,
3867 const struct symt_function* func,
3868 struct location* loc)
3870 struct location frame;
3871 DWORD_PTR ip;
3872 int err;
3873 dwarf2_traverse_context_t lctx;
3874 const dwarf2_cuhead_t* head = get_cuhead_from_func(func);
3876 if (!head)
3878 WARN("We'd expect function %s's container to be a valid compiland with dwarf inforamation\n",
3879 debugstr_a(func->hash_elt.name));
3880 err = loc_err_internal;
3882 else
3884 /* instruction pointer relative to compiland's start */
3885 ip = pcs->localscope_pc - ((struct symt_compiland*)func->container)->address;
3887 if ((err = loc_compute_frame(pcs, modfmt, func, ip, head, &frame)) == 0)
3889 switch (loc->kind)
3891 case loc_dwarf2_location_list:
3892 /* Then, if the variable has a location list, find it !! */
3893 if (dwarf2_lookup_loclist(modfmt, head,
3894 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3895 ip, &lctx))
3896 goto do_compute;
3897 err = loc_err_out_of_scope;
3898 break;
3899 case loc_dwarf2_block:
3900 /* or if we have a copy of an existing block, get ready for it */
3902 unsigned* ptr = (unsigned*)loc->offset;
3904 lctx.data = (const BYTE*)(ptr + 1);
3905 lctx.end_data = lctx.data + *ptr;
3907 do_compute:
3908 /* now get the variable */
3909 err = compute_location(modfmt->module, head,
3910 &lctx, loc, pcs->handle, &frame);
3911 break;
3912 case loc_register:
3913 case loc_regrel:
3914 /* nothing to do */
3915 break;
3916 default:
3917 WARN("Unsupported local kind %d\n", loc->kind);
3918 err = loc_err_internal;
3922 if (err < 0)
3924 loc->kind = loc_register;
3925 loc->reg = err;
3929 static void *zalloc(void *priv, uInt items, uInt sz)
3931 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3934 static void zfree(void *priv, void *addr)
3936 HeapFree(GetProcessHeap(), 0, addr);
3939 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3940 const char* zsectname,
3941 struct image_section_map* ism)
3943 z_stream z;
3944 LARGE_INTEGER li;
3945 int res;
3946 BOOL ret = FALSE;
3948 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3949 size_t sz = image_get_map_size(ism);
3951 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
3953 ERR("invalid compressed section %s\n", debugstr_a(zsectname));
3954 goto out;
3957 #ifdef WORDS_BIGENDIAN
3958 li.u.HighPart = *(DWORD*)&sect[4];
3959 li.u.LowPart = *(DWORD*)&sect[8];
3960 #else
3961 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
3962 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
3963 #endif
3965 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
3966 if (!addr)
3967 goto out;
3969 z.next_in = &sect[12];
3970 z.avail_in = sz - 12;
3971 z.opaque = NULL;
3972 z.zalloc = zalloc;
3973 z.zfree = zfree;
3975 res = inflateInit(&z);
3976 if (res != Z_OK)
3978 FIXME("inflateInit failed with %i / %s\n", res, debugstr_a(z.msg));
3979 goto out_free;
3982 do {
3983 z.next_out = addr + z.total_out;
3984 z.avail_out = li.QuadPart - z.total_out;
3985 res = inflate(&z, Z_FINISH);
3986 } while (z.avail_in && res == Z_STREAM_END);
3988 if (res != Z_STREAM_END)
3990 FIXME("Decompression failed with %i / %s\n", res, debugstr_a(z.msg));
3991 goto out_end;
3994 ret = TRUE;
3995 section->compressed = TRUE;
3996 section->address = addr;
3997 section->rva = image_get_map_rva(ism);
3998 section->size = z.total_out;
4000 out_end:
4001 inflateEnd(&z);
4002 out_free:
4003 if (!ret)
4004 HeapFree(GetProcessHeap(), 0, addr);
4005 out:
4006 image_unmap_section(ism);
4007 return ret;
4010 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
4011 const char* sectname, const char* zsectname,
4012 struct image_section_map* ism)
4014 struct image_section_map local_ism;
4016 if (!ism) ism = &local_ism;
4018 section->compressed = FALSE;
4019 if (image_find_section(fmap, sectname, ism))
4021 section->address = (const BYTE*)image_map_section(ism);
4022 section->size = image_get_map_size(ism);
4023 section->rva = image_get_map_rva(ism);
4024 return TRUE;
4027 section->address = NULL;
4028 section->size = 0;
4029 section->rva = 0;
4031 if (zsectname && image_find_section(fmap, zsectname, ism))
4033 return dwarf2_init_zsection(section, zsectname, ism);
4036 return FALSE;
4039 static inline void dwarf2_fini_section(dwarf2_section_t* section)
4041 if (section->compressed)
4042 HeapFree(GetProcessHeap(), 0, (void*)section->address);
4045 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
4047 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
4048 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
4049 free(modfmt->u.dwarf2_info->cuheads);
4050 HeapFree(GetProcessHeap(), 0, modfmt);
4053 static BOOL dwarf2_load_CU_module(dwarf2_parse_module_context_t* module_ctx, struct module* module,
4054 dwarf2_section_t* sections, ULONG_PTR load_offset,
4055 const struct elf_thunk_area* thunks, BOOL is_dwz)
4057 dwarf2_traverse_context_t mod_ctx;
4058 unsigned i;
4060 module_ctx->sections = sections;
4061 module_ctx->module = module;
4062 module_ctx->thunks = thunks;
4063 module_ctx->load_offset = load_offset;
4064 memset(module_ctx->symt_cache, 0, sizeof(module_ctx->symt_cache));
4065 module_ctx->symt_cache[sc_void] = &symt_new_basic(module_ctx->module, btVoid, "void", 0)->symt;
4066 module_ctx->symt_cache[sc_unknown] = &symt_new_basic(module_ctx->module, btNoType, "# unknown", 0)->symt;
4067 vector_init(&module_ctx->unit_contexts, sizeof(dwarf2_parse_context_t), 16);
4068 module_ctx->cu_versions = 0;
4070 /* phase I: parse all CU heads */
4071 mod_ctx.data = sections[section_debug].address;
4072 mod_ctx.end_data = mod_ctx.data + sections[section_debug].size;
4073 while (mod_ctx.data < mod_ctx.end_data)
4075 dwarf2_parse_context_t* unit_ctx = vector_add(&module_ctx->unit_contexts, &module_ctx->module->pool);
4077 unit_ctx->module_ctx = module_ctx;
4078 dwarf2_parse_compilation_unit_head(unit_ctx, &mod_ctx);
4081 /* phase2: load content of all CU
4082 * If this is a DWZ alternate module, don't load all debug_info at once
4083 * wait for main module to ask for them (it's likely it won't need them all)
4084 * Doing this can lead to a huge performance improvement.
4086 if (!is_dwz)
4087 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
4088 dwarf2_parse_compilation_unit((dwarf2_parse_context_t*)vector_at(&module_ctx->unit_contexts, i));
4090 return TRUE;
4093 static dwarf2_dwz_alternate_t* dwarf2_load_dwz(struct image_file_map* fmap, struct module* module)
4095 struct image_file_map* fmap_dwz;
4096 dwarf2_dwz_alternate_t* dwz;
4098 fmap_dwz = image_load_debugaltlink(fmap, module);
4099 if (!fmap_dwz) return NULL;
4100 if (!(dwz = HeapAlloc(GetProcessHeap(), 0, sizeof(*dwz))))
4102 image_unmap_file(fmap_dwz);
4103 HeapFree(GetProcessHeap(), 0, fmap_dwz);
4104 return NULL;
4107 dwz->fmap = fmap_dwz;
4108 dwarf2_init_section(&dwz->sections[section_debug], fmap_dwz, ".debug_info", ".zdebug_info", &dwz->sectmap[section_debug]);
4109 dwarf2_init_section(&dwz->sections[section_abbrev], fmap_dwz, ".debug_abbrev", ".zdebug_abbrev", &dwz->sectmap[section_abbrev]);
4110 dwarf2_init_section(&dwz->sections[section_string], fmap_dwz, ".debug_str", ".zdebug_str", &dwz->sectmap[section_string]);
4111 dwarf2_init_section(&dwz->sections[section_line], fmap_dwz, ".debug_line", ".zdebug_line", &dwz->sectmap[section_line]);
4112 dwarf2_init_section(&dwz->sections[section_ranges], fmap_dwz, ".debug_ranges", ".zdebug_ranges", &dwz->sectmap[section_ranges]);
4114 dwz->module_ctx.dwz = NULL;
4115 dwarf2_load_CU_module(&dwz->module_ctx, module, dwz->sections, 0/*FIXME*/, NULL, TRUE);
4116 return dwz;
4119 static void dwarf2_unload_dwz(dwarf2_dwz_alternate_t* dwz)
4121 if (!dwz) return;
4122 dwarf2_fini_section(&dwz->sections[section_debug]);
4123 dwarf2_fini_section(&dwz->sections[section_abbrev]);
4124 dwarf2_fini_section(&dwz->sections[section_string]);
4125 dwarf2_fini_section(&dwz->sections[section_line]);
4126 dwarf2_fini_section(&dwz->sections[section_ranges]);
4128 image_unmap_section(&dwz->sectmap[section_debug]);
4129 image_unmap_section(&dwz->sectmap[section_abbrev]);
4130 image_unmap_section(&dwz->sectmap[section_string]);
4131 image_unmap_section(&dwz->sectmap[section_line]);
4132 image_unmap_section(&dwz->sectmap[section_ranges]);
4134 HeapFree(GetProcessHeap(), 0, dwz);
4137 static BOOL dwarf2_unload_CU_module(dwarf2_parse_module_context_t* module_ctx)
4139 unsigned i;
4140 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
4142 dwarf2_parse_context_t* unit = vector_at(&module_ctx->unit_contexts, i);
4143 if (unit->status != UNIT_ERROR)
4144 pool_destroy(&unit->pool);
4146 dwarf2_unload_dwz(module_ctx->dwz);
4147 return TRUE;
4150 BOOL dwarf2_parse(struct module* module, ULONG_PTR load_offset,
4151 const struct elf_thunk_area* thunks,
4152 struct image_file_map* fmap)
4154 dwarf2_section_t eh_frame, section[section_max];
4155 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
4156 debug_line_sect, debug_ranges_sect, eh_frame_sect;
4157 BOOL ret = TRUE;
4158 struct module_format* dwarf2_modfmt;
4159 dwarf2_parse_module_context_t module_ctx;
4161 if (!dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect))
4162 /* lld produces .eh_fram to avoid generating a long name */
4163 dwarf2_init_section(&eh_frame, fmap, ".eh_fram", NULL, &eh_frame_sect);
4164 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
4165 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
4166 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
4167 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
4168 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
4170 /* to do anything useful we need either .eh_frame or .debug_info */
4171 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
4172 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
4174 ret = FALSE;
4175 goto leave;
4178 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
4180 /* debug info might have a different base address than .so file
4181 * when elf file is prelinked after splitting off debug info
4182 * adjust symbol base addresses accordingly
4184 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
4187 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->modulename));
4189 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
4190 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
4191 if (!dwarf2_modfmt)
4193 ret = FALSE;
4194 goto leave;
4196 dwarf2_modfmt->module = module;
4197 dwarf2_modfmt->remove = dwarf2_module_remove;
4198 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
4199 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
4200 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8; /* set the word_size for eh_frame parsing */
4201 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
4203 /* As we'll need later some sections' content, we won't unmap these
4204 * sections upon existing this function
4206 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
4207 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
4208 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
4209 dwarf2_modfmt->u.dwarf2_info->cuheads = NULL;
4210 dwarf2_modfmt->u.dwarf2_info->num_cuheads = 0;
4212 module_ctx.dwz = dwarf2_load_dwz(fmap, module);
4213 dwarf2_load_CU_module(&module_ctx, module, section, load_offset, thunks, FALSE);
4215 dwarf2_modfmt->module->module.SymType = SymDia;
4216 /* hide dwarf versions in CVSig
4217 * bits 24-31 will be set according to found dwarf version
4218 * different CU can have different dwarf version, so use a bit per version (version 2 => b24)
4220 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('F' << 16) | ((module_ctx.cu_versions & 0xFF) << 24);
4221 /* FIXME: we could have a finer grain here */
4222 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
4223 dwarf2_modfmt->module->module.TypeInfo = TRUE;
4224 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
4225 dwarf2_modfmt->module->module.Publics = TRUE;
4227 dwarf2_unload_CU_module(&module_ctx);
4228 leave:
4230 dwarf2_fini_section(&section[section_debug]);
4231 dwarf2_fini_section(&section[section_abbrev]);
4232 dwarf2_fini_section(&section[section_string]);
4233 dwarf2_fini_section(&section[section_line]);
4234 dwarf2_fini_section(&section[section_ranges]);
4236 image_unmap_section(&debug_sect);
4237 image_unmap_section(&debug_abbrev_sect);
4238 image_unmap_section(&debug_str_sect);
4239 image_unmap_section(&debug_line_sect);
4240 image_unmap_section(&debug_ranges_sect);
4241 if (!ret) image_unmap_section(&eh_frame_sect);
4243 return ret;