makefiles: Bypass the normal substitution mechanism for the makefile dependencies.
[wine/multimedia.git] / dlls / dbghelp / dwarf.c
blobe3c9ed9d1be010c6e39b5a2975da5783718d4a87
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
24 #include "config.h"
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
31 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #endif
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <stdio.h>
41 #ifndef PATH_MAX
42 #define PATH_MAX MAX_PATH
43 #endif
44 #include <assert.h>
45 #include <stdarg.h>
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winuser.h"
50 #include "ole2.h"
51 #include "oleauto.h"
53 #include "dbghelp_private.h"
54 #include "image_private.h"
56 #include "wine/debug.h"
58 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
60 /* FIXME:
61 * - Functions:
62 * o unspecified parameters
63 * o inlined functions
64 * o Debug{Start|End}Point
65 * o CFA
66 * - Udt
67 * o proper types loading (nesting)
70 #if 0
71 static void dump(const void* ptr, unsigned len)
73 int i, j;
74 BYTE msg[128];
75 static const char hexof[] = "0123456789abcdef";
76 const BYTE* x = ptr;
78 for (i = 0; i < len; i += 16)
80 sprintf(msg, "%08x: ", i);
81 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
82 for (j = 0; j < min(16, len - i); j++)
84 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
85 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
86 msg[10 + 3 * j + 2] = ' ';
87 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
88 x[i + j] : '.';
90 msg[10 + 3 * 16] = ' ';
91 msg[10 + 3 * 16 + 1 + 16] = '\0';
92 TRACE("%s\n", msg);
95 #endif
97 /**
99 * Main Specs:
100 * http://www.eagercon.com/dwarf/dwarf3std.htm
101 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
103 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
105 * example of projects who do dwarf2 parsing:
106 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
107 * http://elis.ugent.be/diota/log/ltrace_elf.c
109 #include "dwarf.h"
112 * Parsers
115 typedef struct dwarf2_abbrev_entry_attr_s
117 unsigned long attribute;
118 unsigned long form;
119 struct dwarf2_abbrev_entry_attr_s* next;
120 } dwarf2_abbrev_entry_attr_t;
122 typedef struct dwarf2_abbrev_entry_s
124 unsigned long entry_code;
125 unsigned long tag;
126 unsigned char have_child;
127 unsigned num_attr;
128 dwarf2_abbrev_entry_attr_t* attrs;
129 } dwarf2_abbrev_entry_t;
131 struct dwarf2_block
133 unsigned size;
134 const unsigned char* ptr;
137 struct attribute
139 unsigned long form;
140 union
142 unsigned long uvalue;
143 ULONGLONG lluvalue;
144 long svalue;
145 const char* string;
146 struct dwarf2_block block;
147 } u;
150 typedef struct dwarf2_debug_info_s
152 const dwarf2_abbrev_entry_t*abbrev;
153 struct symt* symt;
154 const unsigned char** data;
155 struct vector children;
156 } dwarf2_debug_info_t;
158 typedef struct dwarf2_section_s
160 const unsigned char* address;
161 unsigned size;
162 DWORD_PTR rva;
163 } dwarf2_section_t;
165 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
167 typedef struct dwarf2_traverse_context_s
169 const unsigned char* data;
170 const unsigned char* end_data;
171 unsigned char word_size;
172 } dwarf2_traverse_context_t;
174 /* symt_cache indexes */
175 #define sc_void 0
176 #define sc_int1 1
177 #define sc_int2 2
178 #define sc_int4 3
179 #define sc_num 4
181 typedef struct dwarf2_parse_context_s
183 const dwarf2_section_t* sections;
184 unsigned section;
185 struct pool pool;
186 struct module* module;
187 const struct elf_thunk_area*thunks;
188 struct sparse_array abbrev_table;
189 struct sparse_array debug_info_table;
190 unsigned long load_offset;
191 unsigned long ref_offset;
192 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
193 } dwarf2_parse_context_t;
195 /* stored in the dbghelp's module internal structure for later reuse */
196 struct dwarf2_module_info_s
198 dwarf2_section_t debug_loc;
199 unsigned char word_size;
202 #define loc_dwarf2_location_list (loc_user + 0)
203 #define loc_dwarf2_block (loc_user + 1)
205 /* forward declarations */
206 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
208 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
210 return *ptr;
213 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
215 unsigned char uvalue = dwarf2_get_byte(ctx->data);
216 ctx->data += 1;
217 return uvalue;
220 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
222 return *(const UINT16*)ptr;
225 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
227 unsigned short uvalue = dwarf2_get_u2(ctx->data);
228 ctx->data += 2;
229 return uvalue;
232 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
234 return *(const UINT32*)ptr;
237 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
239 unsigned long uvalue = dwarf2_get_u4(ctx->data);
240 ctx->data += 4;
241 return uvalue;
244 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
246 return *(const UINT64*)ptr;
249 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
251 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
252 ctx->data += 8;
253 return uvalue;
256 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
258 unsigned long ret = 0;
259 unsigned char byte;
260 unsigned shift = 0;
264 byte = dwarf2_get_byte(ptr++);
265 ret |= (byte & 0x7f) << shift;
266 shift += 7;
267 } while (byte & 0x80);
269 if (end) *end = ptr;
270 return ret;
273 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
275 unsigned long ret;
277 assert(ctx);
279 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
281 return ret;
284 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
286 long ret = 0;
287 unsigned char byte;
288 unsigned shift = 0;
289 const unsigned size = sizeof(int) * 8;
293 byte = dwarf2_get_byte(ptr++);
294 ret |= (byte & 0x7f) << shift;
295 shift += 7;
296 } while (byte & 0x80);
297 if (end) *end = ptr;
299 /* as spec: sign bit of byte is 2nd high order bit (80x40)
300 * -> 0x80 is used as flag.
302 if ((shift < size) && (byte & 0x40))
304 ret |= - (1 << shift);
306 return ret;
309 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
311 long ret = 0;
313 assert(ctx);
315 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
316 return ret;
319 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
321 unsigned ret;
322 for (ret = 0; ctx->data[ret] & 0x80; ret++);
323 return ret + 1;
326 /******************************************************************
327 * dwarf2_get_addr
329 * Returns an address.
330 * We assume that in all cases word size from Dwarf matches the size of
331 * addresses in platform where the exec is compiled.
333 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
335 unsigned long ret;
337 switch (word_size)
339 case 4:
340 ret = dwarf2_get_u4(ptr);
341 break;
342 case 8:
343 ret = dwarf2_get_u8(ptr);
344 break;
345 default:
346 FIXME("Unsupported Word Size %u\n", word_size);
347 ret = 0;
349 return ret;
352 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
354 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
355 ctx->data += ctx->word_size;
356 return ret;
359 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
361 return wine_dbg_sprintf("ctx(%p)", ctx->data);
364 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
366 return wine_dbg_sprintf("ctx(%p,%s)",
367 ctx, debugstr_w(ctx->module->module.ModuleName));
370 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
372 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
373 di->abbrev, di->symt);
376 static dwarf2_abbrev_entry_t*
377 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
378 unsigned long entry_code)
380 assert( NULL != abbrev_table );
381 return sparse_array_find(abbrev_table, entry_code);
384 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
385 struct sparse_array* abbrev_table,
386 struct pool* pool)
388 unsigned long entry_code;
389 dwarf2_abbrev_entry_t* abbrev_entry;
390 dwarf2_abbrev_entry_attr_t* new = NULL;
391 dwarf2_abbrev_entry_attr_t* last = NULL;
392 unsigned long attribute;
393 unsigned long form;
395 assert( NULL != abbrev_ctx );
397 TRACE("%s, end at %p\n",
398 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
400 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
401 while (abbrev_ctx->data < abbrev_ctx->end_data)
403 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
404 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
405 TRACE("found entry_code %lu\n", entry_code);
406 if (!entry_code)
408 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
409 break;
411 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
412 assert( NULL != abbrev_entry );
414 abbrev_entry->entry_code = entry_code;
415 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
416 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
417 abbrev_entry->attrs = NULL;
418 abbrev_entry->num_attr = 0;
420 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
421 abbrev_table, sparse_array_length(abbrev_table),
422 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
424 last = NULL;
425 while (1)
427 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
428 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
429 if (!attribute) break;
431 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
432 assert(new);
434 new->attribute = attribute;
435 new->form = form;
436 new->next = NULL;
437 if (abbrev_entry->attrs) last->next = new;
438 else abbrev_entry->attrs = new;
439 last = new;
440 abbrev_entry->num_attr++;
443 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
446 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
447 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
449 unsigned step;
451 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
453 switch (abbrev_attr->form)
455 case DW_FORM_ref_addr:
456 case DW_FORM_addr: step = ctx->word_size; break;
457 case DW_FORM_flag:
458 case DW_FORM_data1:
459 case DW_FORM_ref1: step = 1; break;
460 case DW_FORM_data2:
461 case DW_FORM_ref2: step = 2; break;
462 case DW_FORM_data4:
463 case DW_FORM_ref4:
464 case DW_FORM_strp: step = 4; break;
465 case DW_FORM_data8:
466 case DW_FORM_ref8: step = 8; break;
467 case DW_FORM_sdata:
468 case DW_FORM_ref_udata:
469 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
470 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
471 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
472 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
473 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
474 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
475 default:
476 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
477 return;
479 ctx->data += step;
482 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
483 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
484 const unsigned char* data,
485 struct attribute* attr)
487 attr->form = abbrev_attr->form;
488 switch (attr->form)
490 case DW_FORM_ref_addr:
491 case DW_FORM_addr:
492 attr->u.uvalue = dwarf2_get_addr(data,
493 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
494 TRACE("addr<0x%lx>\n", attr->u.uvalue);
495 break;
497 case DW_FORM_flag:
498 attr->u.uvalue = dwarf2_get_byte(data);
499 TRACE("flag<0x%lx>\n", attr->u.uvalue);
500 break;
502 case DW_FORM_data1:
503 attr->u.uvalue = dwarf2_get_byte(data);
504 TRACE("data1<%lu>\n", attr->u.uvalue);
505 break;
507 case DW_FORM_data2:
508 attr->u.uvalue = dwarf2_get_u2(data);
509 TRACE("data2<%lu>\n", attr->u.uvalue);
510 break;
512 case DW_FORM_data4:
513 attr->u.uvalue = dwarf2_get_u4(data);
514 TRACE("data4<%lu>\n", attr->u.uvalue);
515 break;
517 case DW_FORM_data8:
518 attr->u.lluvalue = dwarf2_get_u8(data);
519 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
520 break;
522 case DW_FORM_ref1:
523 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
524 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
525 break;
527 case DW_FORM_ref2:
528 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
529 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
530 break;
532 case DW_FORM_ref4:
533 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
534 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
535 break;
537 case DW_FORM_ref8:
538 FIXME("Unhandled 64 bit support\n");
539 break;
541 case DW_FORM_sdata:
542 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
543 break;
545 case DW_FORM_ref_udata:
546 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
547 break;
549 case DW_FORM_udata:
550 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
551 break;
553 case DW_FORM_string:
554 attr->u.string = (const char *)data;
555 TRACE("string<%s>\n", attr->u.string);
556 break;
558 case DW_FORM_strp:
560 unsigned long offset = dwarf2_get_u4(data);
561 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
563 TRACE("strp<%s>\n", attr->u.string);
564 break;
566 case DW_FORM_block:
567 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
568 break;
570 case DW_FORM_block1:
571 attr->u.block.size = dwarf2_get_byte(data);
572 attr->u.block.ptr = data + 1;
573 break;
575 case DW_FORM_block2:
576 attr->u.block.size = dwarf2_get_u2(data);
577 attr->u.block.ptr = data + 2;
578 break;
580 case DW_FORM_block4:
581 attr->u.block.size = dwarf2_get_u4(data);
582 attr->u.block.ptr = data + 4;
583 break;
585 default:
586 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
587 break;
591 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
592 const dwarf2_debug_info_t* di,
593 unsigned at, struct attribute* attr)
595 unsigned i, ai = 0;
596 dwarf2_abbrev_entry_attr_t* abbrev_attr;
597 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
599 while (di)
601 abstract_abbrev_attr = NULL;
602 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
604 if (abbrev_attr->attribute == at)
606 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
607 return TRUE;
609 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
610 at != DW_AT_sibling)
612 abstract_abbrev_attr = abbrev_attr;
613 ai = i;
616 /* do we have an abstract origin debug entry to look into ? */
617 if (!abstract_abbrev_attr) break;
618 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
619 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
620 FIXME("Should have found the debug info entry\n");
622 return FALSE;
625 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
626 struct symt_compiland*);
628 #define Wine_DW_no_register 0x7FFFFFFF
630 static unsigned dwarf2_map_register(int regno)
632 if (regno == Wine_DW_no_register)
634 FIXME("What the heck map reg 0x%x\n",regno);
635 return 0;
637 return dbghelp_current_cpu->map_dwarf_register(regno);
640 static enum location_error
641 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
642 HANDLE hproc, const struct location* frame)
644 DWORD_PTR tmp, stack[64];
645 unsigned stk;
646 unsigned char op;
647 BOOL piece_found = FALSE;
649 stack[stk = 0] = 0;
651 loc->kind = loc_absolute;
652 loc->reg = Wine_DW_no_register;
654 while (ctx->data < ctx->end_data)
656 op = dwarf2_parse_byte(ctx);
658 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
659 stack[++stk] = op - DW_OP_lit0;
660 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
662 /* dbghelp APIs don't know how to cope with this anyway
663 * (for example 'long long' stored in two registers)
664 * FIXME: We should tell winedbg how to deal with it (sigh)
666 if (!piece_found)
668 if (loc->reg != Wine_DW_no_register)
669 FIXME("Only supporting one reg (%d -> %d)\n",
670 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
671 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
673 loc->kind = loc_register;
675 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
677 /* dbghelp APIs don't know how to cope with this anyway
678 * (for example 'long long' stored in two registers)
679 * FIXME: We should tell winedbg how to deal with it (sigh)
681 if (!piece_found)
683 if (loc->reg != Wine_DW_no_register)
684 FIXME("Only supporting one breg (%d -> %d)\n",
685 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
686 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
688 stack[++stk] = dwarf2_leb128_as_signed(ctx);
689 loc->kind = loc_regrel;
690 break;
692 else switch (op)
694 case DW_OP_nop: break;
695 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
696 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
697 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
698 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
699 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
700 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
701 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
702 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
703 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
704 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
705 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
706 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
707 case DW_OP_drop: stk--; break;
708 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
709 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
710 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
711 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
712 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
713 case DW_OP_neg: stack[stk] = -stack[stk]; break;
714 case DW_OP_not: stack[stk] = ~stack[stk]; break;
715 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
716 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
717 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
718 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
719 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
720 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
721 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
722 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
723 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
724 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
725 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
726 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
727 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
728 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
729 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
730 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
731 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
732 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
733 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
734 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
735 case DW_OP_regx:
736 if (loc->reg != Wine_DW_no_register)
737 FIXME("Only supporting one regx\n");
738 loc->reg = dwarf2_map_register(dwarf2_leb128_as_unsigned(ctx));
739 loc->kind = loc_register;
740 break;
741 case DW_OP_bregx:
742 tmp = dwarf2_leb128_as_unsigned(ctx);
743 ctx->data++;
744 if (loc->reg != Wine_DW_no_register)
745 FIXME("Only supporting one regx\n");
746 loc->reg = dwarf2_map_register(tmp) + dwarf2_leb128_as_signed(ctx);
747 loc->kind = loc_register;
748 break;
749 case DW_OP_fbreg:
750 if (loc->reg != Wine_DW_no_register)
751 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
752 if (frame && frame->kind == loc_register)
754 loc->kind = loc_regrel;
755 loc->reg = frame->reg;
756 stack[++stk] = dwarf2_leb128_as_signed(ctx);
758 else if (frame && frame->kind == loc_regrel)
760 loc->kind = loc_regrel;
761 loc->reg = frame->reg;
762 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
764 else
766 /* FIXME: this could be later optimized by not recomputing
767 * this very location expression
769 loc->kind = loc_dwarf2_block;
770 stack[++stk] = dwarf2_leb128_as_signed(ctx);
772 break;
773 case DW_OP_piece:
775 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
776 WARN("Not handling OP_piece (size=%d)\n", sz);
777 piece_found = TRUE;
779 break;
780 case DW_OP_deref:
781 if (!stk)
783 FIXME("Unexpected empty stack\n");
784 return loc_err_internal;
786 if (loc->reg != Wine_DW_no_register)
788 WARN("Too complex expression for deref\n");
789 return loc_err_too_complex;
791 if (hproc)
793 DWORD_PTR addr = stack[stk--];
794 DWORD_PTR deref;
796 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
798 WARN("Couldn't read memory at %lx\n", addr);
799 return loc_err_cant_read;
801 stack[++stk] = deref;
803 else
805 loc->kind = loc_dwarf2_block;
807 break;
808 case DW_OP_deref_size:
809 if (!stk)
811 FIXME("Unexpected empty stack\n");
812 return loc_err_internal;
814 if (loc->reg != Wine_DW_no_register)
816 WARN("Too complex expression for deref\n");
817 return loc_err_too_complex;
819 if (hproc)
821 DWORD_PTR addr = stack[stk--];
822 BYTE derefsize = dwarf2_parse_byte(ctx);
823 DWORD64 deref;
825 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
827 WARN("Couldn't read memory at %lx\n", addr);
828 return loc_err_cant_read;
831 switch (derefsize)
833 case 1: stack[++stk] = *(unsigned char*)&deref; break;
834 case 2: stack[++stk] = *(unsigned short*)&deref; break;
835 case 4: stack[++stk] = *(DWORD*)&deref; break;
836 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
839 else
841 loc->kind = loc_dwarf2_block;
843 break;
844 default:
845 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
846 FIXME("Unhandled attr op: %x\n", op);
847 /* FIXME else unhandled extension */
848 return loc_err_internal;
851 loc->offset = stack[stk];
852 return 0;
855 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
856 const dwarf2_debug_info_t* di,
857 unsigned long dw,
858 struct location* loc,
859 const struct location* frame)
861 struct attribute xloc;
863 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
865 switch (xloc.form)
867 case DW_FORM_data1: case DW_FORM_data2:
868 case DW_FORM_udata: case DW_FORM_sdata:
869 loc->kind = loc_absolute;
870 loc->reg = 0;
871 loc->offset = xloc.u.uvalue;
872 return TRUE;
873 case DW_FORM_data4: case DW_FORM_data8:
874 loc->kind = loc_dwarf2_location_list;
875 loc->reg = Wine_DW_no_register;
876 loc->offset = xloc.u.uvalue;
877 return TRUE;
880 /* assume we have a block form */
882 if (xloc.u.block.size)
884 dwarf2_traverse_context_t lctx;
885 enum location_error err;
887 lctx.data = xloc.u.block.ptr;
888 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
889 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
891 err = compute_location(&lctx, loc, NULL, frame);
892 if (err < 0)
894 loc->kind = loc_error;
895 loc->reg = err;
897 else if (loc->kind == loc_dwarf2_block)
899 unsigned* ptr = pool_alloc(&ctx->module->pool,
900 sizeof(unsigned) + xloc.u.block.size);
901 *ptr = xloc.u.block.size;
902 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
903 loc->offset = (unsigned long)ptr;
906 return TRUE;
909 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
910 const dwarf2_debug_info_t* di)
912 struct attribute attr;
914 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
916 dwarf2_debug_info_t* type;
918 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
919 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
920 if (!type->symt)
922 /* load the debug info entity */
923 dwarf2_load_one_entry(ctx, type, NULL);
925 return type->symt;
927 return NULL;
930 /******************************************************************
931 * dwarf2_read_one_debug_info
933 * Loads into memory one debug info entry, and recursively its children (if any)
935 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
936 dwarf2_traverse_context_t* traverse,
937 dwarf2_debug_info_t** pdi)
939 const dwarf2_abbrev_entry_t*abbrev;
940 unsigned long entry_code;
941 unsigned long offset;
942 dwarf2_debug_info_t* di;
943 dwarf2_debug_info_t* child;
944 dwarf2_debug_info_t** where;
945 dwarf2_abbrev_entry_attr_t* attr;
946 unsigned i;
947 struct attribute sibling;
949 offset = traverse->data - ctx->sections[ctx->section].address;
950 entry_code = dwarf2_leb128_as_unsigned(traverse);
951 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
952 if (!entry_code)
954 *pdi = NULL;
955 return TRUE;
957 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
958 if (!abbrev)
960 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
961 return FALSE;
963 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
964 if (!di) return FALSE;
965 di->abbrev = abbrev;
966 di->symt = NULL;
968 if (abbrev->num_attr)
970 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
971 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
973 di->data[i] = traverse->data;
974 dwarf2_swallow_attribute(traverse, attr);
977 else di->data = NULL;
978 if (abbrev->have_child)
980 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
981 while (traverse->data < traverse->end_data)
983 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
984 if (!child) break;
985 where = vector_add(&di->children, &ctx->pool);
986 if (!where) return FALSE;
987 *where = child;
990 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
991 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
993 WARN("setting cursor for %s to next sibling <0x%lx>\n",
994 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
995 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
997 *pdi = di;
998 return TRUE;
1001 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1002 dwarf2_debug_info_t* di)
1004 struct attribute name;
1005 struct attribute size;
1006 struct attribute encoding;
1007 enum BasicType bt;
1008 int cache_idx = -1;
1009 if (di->symt) return di->symt;
1011 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1013 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1014 name.u.string = NULL;
1015 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1016 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1018 switch (encoding.u.uvalue)
1020 case DW_ATE_void: bt = btVoid; break;
1021 case DW_ATE_address: bt = btULong; break;
1022 case DW_ATE_boolean: bt = btBool; break;
1023 case DW_ATE_complex_float: bt = btComplex; break;
1024 case DW_ATE_float: bt = btFloat; break;
1025 case DW_ATE_signed: bt = btInt; break;
1026 case DW_ATE_unsigned: bt = btUInt; break;
1027 case DW_ATE_signed_char: bt = btChar; break;
1028 case DW_ATE_unsigned_char: bt = btChar; break;
1029 default: bt = btNoType; break;
1031 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1032 switch (bt)
1034 case btVoid:
1035 assert(size.u.uvalue == 0);
1036 cache_idx = sc_void;
1037 break;
1038 case btInt:
1039 switch (size.u.uvalue)
1041 case 1: cache_idx = sc_int1; break;
1042 case 2: cache_idx = sc_int2; break;
1043 case 4: cache_idx = sc_int4; break;
1045 break;
1046 default: break;
1048 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1049 ctx->symt_cache[cache_idx] = di->symt;
1051 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1052 return di->symt;
1055 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1056 dwarf2_debug_info_t* di)
1058 struct symt* ref_type;
1059 struct attribute name;
1061 if (di->symt) return di->symt;
1063 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1065 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1066 ref_type = dwarf2_lookup_type(ctx, di);
1068 if (name.u.string)
1069 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1070 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1071 return di->symt;
1074 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1075 dwarf2_debug_info_t* di)
1077 struct symt* ref_type;
1078 struct attribute size;
1080 if (di->symt) return di->symt;
1082 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1084 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1085 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1087 ref_type = ctx->symt_cache[sc_void];
1088 assert(ref_type);
1090 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1091 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1092 return di->symt;
1095 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1096 dwarf2_debug_info_t* di)
1098 struct symt* ref_type;
1099 struct symt* idx_type = NULL;
1100 struct attribute min, max, cnt;
1101 dwarf2_debug_info_t* child;
1102 unsigned int i;
1104 if (di->symt) return di->symt;
1106 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1108 if (!di->abbrev->have_child)
1110 FIXME("array without range information\n");
1111 return NULL;
1113 ref_type = dwarf2_lookup_type(ctx, di);
1115 for (i=0; i<vector_length(&di->children); i++)
1117 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1118 switch (child->abbrev->tag)
1120 case DW_TAG_subrange_type:
1121 idx_type = dwarf2_lookup_type(ctx, child);
1122 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1123 min.u.uvalue = 0;
1124 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1125 max.u.uvalue = 0;
1126 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1127 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1128 break;
1129 default:
1130 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1131 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1132 break;
1135 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1136 return di->symt;
1139 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1140 dwarf2_debug_info_t* di)
1142 struct symt* ref_type;
1144 if (di->symt) return di->symt;
1146 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1148 ref_type = dwarf2_lookup_type(ctx, di);
1149 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1150 di->symt = ref_type;
1152 return ref_type;
1155 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1156 dwarf2_debug_info_t* di)
1158 struct symt* ref_type;
1160 if (di->symt) return di->symt;
1162 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1164 ref_type = dwarf2_lookup_type(ctx, di);
1165 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1166 di->symt = ref_type;
1168 return ref_type;
1171 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1172 dwarf2_debug_info_t* di)
1174 struct symt* ref_type = NULL;
1176 if (di->symt) return di->symt;
1178 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1180 ref_type = dwarf2_lookup_type(ctx, di);
1181 /* FIXME: for now, we hard-wire C++ references to pointers */
1182 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1184 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1186 return di->symt;
1189 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1190 const dwarf2_debug_info_t* di,
1191 struct symt_udt* parent)
1193 struct symt* elt_type;
1194 struct attribute name;
1195 struct attribute bit_size;
1196 struct attribute bit_offset;
1197 struct location loc;
1199 assert(parent);
1201 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1203 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1204 elt_type = dwarf2_lookup_type(ctx, di);
1205 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1207 if (loc.kind != loc_absolute)
1209 FIXME("Found register, while not expecting it\n");
1210 loc.offset = 0;
1212 else
1213 TRACE("found member_location at %s -> %lu\n",
1214 dwarf2_debug_ctx(ctx), loc.offset);
1216 else
1217 loc.offset = 0;
1218 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1219 bit_size.u.uvalue = 0;
1220 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1222 /* FIXME: we should only do this when implementation is LSB (which is
1223 * the case on i386 processors)
1225 struct attribute nbytes;
1226 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1228 DWORD64 size;
1229 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1230 (unsigned long)size : 0;
1232 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1234 else bit_offset.u.uvalue = 0;
1235 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1236 (loc.offset << 3) + bit_offset.u.uvalue,
1237 bit_size.u.uvalue);
1239 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1242 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1243 dwarf2_debug_info_t* di,
1244 enum UdtKind udt)
1246 struct attribute name;
1247 struct attribute size;
1249 if (di->symt) return di->symt;
1251 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1253 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1254 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1256 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1258 if (di->abbrev->have_child) /** any interest to not have child ? */
1260 dwarf2_debug_info_t* child;
1261 unsigned int i;
1263 for (i=0; i<vector_length(&di->children); i++)
1265 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1267 switch (child->abbrev->tag)
1269 case DW_TAG_member:
1270 /* FIXME: should I follow the sibling stuff ?? */
1271 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1272 break;
1273 case DW_TAG_enumeration_type:
1274 dwarf2_parse_enumeration_type(ctx, child);
1275 break;
1276 case DW_TAG_structure_type:
1277 case DW_TAG_class_type:
1278 case DW_TAG_union_type:
1279 case DW_TAG_typedef:
1280 /* FIXME: we need to handle nested udt definitions */
1281 case DW_TAG_inheritance:
1282 case DW_TAG_subprogram:
1283 case DW_TAG_variable:
1284 /* FIXME: some C++ related stuff */
1285 break;
1286 default:
1287 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1288 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1289 break;
1294 return di->symt;
1297 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1298 const dwarf2_debug_info_t* di,
1299 struct symt_enum* parent)
1301 struct attribute name;
1302 struct attribute value;
1304 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1306 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1307 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1308 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1310 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1313 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1314 dwarf2_debug_info_t* di)
1316 struct attribute name;
1317 struct attribute size;
1318 struct symt_basic* basetype;
1320 if (di->symt) return di->symt;
1322 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1324 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1325 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1327 switch (size.u.uvalue) /* FIXME: that's wrong */
1329 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1330 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1331 default:
1332 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1335 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1337 if (di->abbrev->have_child) /* any interest to not have child ? */
1339 dwarf2_debug_info_t* child;
1340 unsigned int i;
1342 /* FIXME: should we use the sibling stuff ?? */
1343 for (i=0; i<vector_length(&di->children); i++)
1345 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1347 switch (child->abbrev->tag)
1349 case DW_TAG_enumerator:
1350 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1351 break;
1352 default:
1353 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1354 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1358 return di->symt;
1361 /* structure used to pass information around when parsing a subprogram */
1362 typedef struct dwarf2_subprogram_s
1364 dwarf2_parse_context_t* ctx;
1365 struct symt_compiland* compiland;
1366 struct symt_function* func;
1367 BOOL non_computed_variable;
1368 struct location frame;
1369 } dwarf2_subprogram_t;
1371 /******************************************************************
1372 * dwarf2_parse_variable
1374 * Parses any variable (parameter, local/global variable)
1376 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1377 struct symt_block* block,
1378 dwarf2_debug_info_t* di)
1380 struct symt* param_type;
1381 struct attribute name, value;
1382 struct location loc;
1383 BOOL is_pmt;
1385 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1387 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1388 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1390 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1391 /* cannot do much without the name, the functions below won't like it. */
1392 return;
1394 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1395 &loc, &subpgm->frame))
1397 struct attribute ext;
1399 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1400 name.u.string, loc.kind, loc.offset, loc.reg,
1401 dwarf2_debug_ctx(subpgm->ctx));
1403 switch (loc.kind)
1405 case loc_error:
1406 break;
1407 case loc_absolute:
1408 /* it's a global variable */
1409 /* FIXME: we don't handle its scope yet */
1410 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1411 ext.u.uvalue = 0;
1412 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1413 name.u.string, !ext.u.uvalue,
1414 subpgm->ctx->load_offset + loc.offset,
1415 0, param_type);
1416 break;
1417 default:
1418 subpgm->non_computed_variable = TRUE;
1419 /* fall through */
1420 case loc_register:
1421 case loc_regrel:
1422 /* either a pmt/variable relative to frame pointer or
1423 * pmt/variable in a register
1425 assert(subpgm->func);
1426 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1427 is_pmt ? DataIsParam : DataIsLocal,
1428 &loc, block, param_type, name.u.string);
1429 break;
1432 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1434 VARIANT v;
1435 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1436 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1437 switch (value.form)
1439 case DW_FORM_data1:
1440 case DW_FORM_data2:
1441 case DW_FORM_data4:
1442 case DW_FORM_udata:
1443 case DW_FORM_addr:
1444 v.n1.n2.vt = VT_UI4;
1445 v.n1.n2.n3.lVal = value.u.uvalue;
1446 break;
1448 case DW_FORM_data8:
1449 v.n1.n2.vt = VT_UI8;
1450 v.n1.n2.n3.llVal = value.u.lluvalue;
1451 break;
1453 case DW_FORM_sdata:
1454 v.n1.n2.vt = VT_I4;
1455 v.n1.n2.n3.lVal = value.u.svalue;
1456 break;
1458 case DW_FORM_strp:
1459 case DW_FORM_string:
1460 /* FIXME: native doesn't report const strings from here !!
1461 * however, the value of the string is in the code somewhere
1463 v.n1.n2.vt = VT_I1 | VT_BYREF;
1464 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1465 break;
1467 case DW_FORM_block:
1468 case DW_FORM_block1:
1469 case DW_FORM_block2:
1470 case DW_FORM_block4:
1471 v.n1.n2.vt = VT_I4;
1472 switch (value.u.block.size)
1474 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1475 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1476 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1477 default:
1478 v.n1.n2.vt = VT_I1 | VT_BYREF;
1479 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1480 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1482 break;
1484 default:
1485 FIXME("Unsupported form for const value %s (%lx)\n",
1486 name.u.string, value.form);
1487 v.n1.n2.vt = VT_EMPTY;
1489 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1490 name.u.string, param_type, &v)->symt;
1492 if (is_pmt && subpgm->func && subpgm->func->type)
1493 symt_add_function_signature_parameter(subpgm->ctx->module,
1494 (struct symt_function_signature*)subpgm->func->type,
1495 param_type);
1497 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1500 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1501 const dwarf2_debug_info_t* di)
1503 struct attribute name;
1504 struct attribute low_pc;
1505 struct location loc;
1507 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1509 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1510 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1511 name.u.string = NULL;
1513 loc.kind = loc_absolute;
1514 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1515 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1516 &loc, name.u.string);
1519 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1520 struct symt_block* parent_block,
1521 const dwarf2_debug_info_t* di);
1523 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1524 struct symt_block* parent_block,
1525 const dwarf2_debug_info_t* di)
1527 struct symt_block* block;
1528 struct attribute low_pc;
1529 struct attribute high_pc;
1531 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1533 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1534 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1536 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1537 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1538 high_pc.u.uvalue - low_pc.u.uvalue);
1540 if (di->abbrev->have_child) /** any interest to not have child ? */
1542 dwarf2_debug_info_t* child;
1543 unsigned int i;
1545 for (i=0; i<vector_length(&di->children); i++)
1547 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1549 switch (child->abbrev->tag)
1551 case DW_TAG_formal_parameter:
1552 case DW_TAG_variable:
1553 dwarf2_parse_variable(subpgm, block, child);
1554 break;
1555 case DW_TAG_lexical_block:
1556 dwarf2_parse_subprogram_block(subpgm, block, child);
1557 break;
1558 case DW_TAG_inlined_subroutine:
1559 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1560 break;
1561 case DW_TAG_label:
1562 dwarf2_parse_subprogram_label(subpgm, child);
1563 break;
1564 default:
1565 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1566 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1567 dwarf2_debug_di(di));
1571 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1574 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1575 struct symt_block* parent_block,
1576 const dwarf2_debug_info_t* di)
1578 struct symt_block* block;
1579 struct attribute low_pc;
1580 struct attribute high_pc;
1582 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1584 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1585 low_pc.u.uvalue = 0;
1586 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1587 high_pc.u.uvalue = 0;
1589 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1590 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1591 high_pc.u.uvalue - low_pc.u.uvalue);
1593 if (di->abbrev->have_child) /** any interest to not have child ? */
1595 dwarf2_debug_info_t* child;
1596 unsigned int i;
1598 for (i=0; i<vector_length(&di->children); i++)
1600 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1602 switch (child->abbrev->tag)
1604 case DW_TAG_inlined_subroutine:
1605 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1606 break;
1607 case DW_TAG_variable:
1608 dwarf2_parse_variable(subpgm, block, child);
1609 break;
1610 case DW_TAG_lexical_block:
1611 dwarf2_parse_subprogram_block(subpgm, block, child);
1612 break;
1613 case DW_TAG_subprogram:
1614 /* FIXME: likely a declaration (to be checked)
1615 * skip it for now
1617 break;
1618 case DW_TAG_formal_parameter:
1619 /* FIXME: likely elements for exception handling (GCC flavor)
1620 * Skip it for now
1622 break;
1623 case DW_TAG_label:
1624 dwarf2_parse_subprogram_label(subpgm, child);
1625 break;
1626 case DW_TAG_class_type:
1627 case DW_TAG_structure_type:
1628 case DW_TAG_union_type:
1629 case DW_TAG_enumeration_type:
1630 case DW_TAG_typedef:
1631 /* the type referred to will be loaded when we need it, so skip it */
1632 break;
1633 default:
1634 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1635 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1640 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1643 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1644 dwarf2_debug_info_t* di,
1645 struct symt_compiland* compiland)
1647 struct attribute name;
1648 struct attribute low_pc;
1649 struct attribute high_pc;
1650 struct attribute is_decl;
1651 struct attribute inline_flags;
1652 struct symt* ret_type;
1653 struct symt_function_signature* sig_type;
1654 dwarf2_subprogram_t subpgm;
1656 if (di->symt) return di->symt;
1658 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1660 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1662 WARN("No name for function... dropping function\n");
1663 return NULL;
1665 /* if it's an abstract representation of an inline function, there should be
1666 * a concrete object that we'll handle
1668 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1670 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1671 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1672 return NULL;
1675 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1676 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1677 /* As functions (defined as inline assembly) get debug info with dwarf
1678 * (not the case for stabs), we just drop Wine's thunks here...
1679 * Actual thunks will be created in elf_module from the symbol table
1681 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1682 ctx->thunks) >= 0)
1683 return NULL;
1684 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1685 is_decl.u.uvalue = 0;
1687 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1689 ret_type = ctx->symt_cache[sc_void];
1690 assert(ret_type);
1693 /* FIXME: assuming C source code */
1694 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1695 if (!is_decl.u.uvalue)
1697 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1698 ctx->load_offset + low_pc.u.uvalue,
1699 high_pc.u.uvalue - low_pc.u.uvalue,
1700 &sig_type->symt);
1701 di->symt = &subpgm.func->symt;
1703 else subpgm.func = NULL;
1705 subpgm.ctx = ctx;
1706 subpgm.compiland = compiland;
1707 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1708 &subpgm.frame, NULL))
1710 /* on stack !! */
1711 subpgm.frame.kind = loc_regrel;
1712 subpgm.frame.reg = 0;
1713 subpgm.frame.offset = 0;
1715 subpgm.non_computed_variable = FALSE;
1717 if (di->abbrev->have_child) /** any interest to not have child ? */
1719 dwarf2_debug_info_t* child;
1720 unsigned int i;
1722 for (i=0; i<vector_length(&di->children); i++)
1724 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1726 switch (child->abbrev->tag)
1728 case DW_TAG_variable:
1729 case DW_TAG_formal_parameter:
1730 dwarf2_parse_variable(&subpgm, NULL, child);
1731 break;
1732 case DW_TAG_lexical_block:
1733 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1734 break;
1735 case DW_TAG_inlined_subroutine:
1736 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1737 break;
1738 case DW_TAG_subprogram:
1739 /* FIXME: likely a declaration (to be checked)
1740 * skip it for now
1742 break;
1743 case DW_TAG_label:
1744 dwarf2_parse_subprogram_label(&subpgm, child);
1745 break;
1746 case DW_TAG_class_type:
1747 case DW_TAG_structure_type:
1748 case DW_TAG_union_type:
1749 case DW_TAG_enumeration_type:
1750 case DW_TAG_typedef:
1751 /* the type referred to will be loaded when we need it, so skip it */
1752 break;
1753 case DW_TAG_unspecified_parameters:
1754 /* FIXME: no support in dbghelp's internals so far */
1755 break;
1756 default:
1757 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1758 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1763 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1765 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1766 &subpgm.frame, NULL);
1768 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1770 return di->symt;
1773 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1774 dwarf2_debug_info_t* di)
1776 struct symt* ret_type;
1777 struct symt_function_signature* sig_type;
1779 if (di->symt) return di->symt;
1781 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1783 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1785 ret_type = ctx->symt_cache[sc_void];
1786 assert(ret_type);
1789 /* FIXME: assuming C source code */
1790 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1792 if (di->abbrev->have_child) /** any interest to not have child ? */
1794 dwarf2_debug_info_t* child;
1795 unsigned int i;
1797 for (i=0; i<vector_length(&di->children); i++)
1799 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1801 switch (child->abbrev->tag)
1803 case DW_TAG_formal_parameter:
1804 symt_add_function_signature_parameter(ctx->module, sig_type,
1805 dwarf2_lookup_type(ctx, child));
1806 break;
1807 case DW_TAG_unspecified_parameters:
1808 WARN("Unsupported unspecified parameters\n");
1809 break;
1814 return di->symt = &sig_type->symt;
1817 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1818 dwarf2_debug_info_t* di,
1819 struct symt_compiland* compiland)
1821 switch (di->abbrev->tag)
1823 case DW_TAG_typedef:
1824 dwarf2_parse_typedef(ctx, di);
1825 break;
1826 case DW_TAG_base_type:
1827 dwarf2_parse_base_type(ctx, di);
1828 break;
1829 case DW_TAG_pointer_type:
1830 dwarf2_parse_pointer_type(ctx, di);
1831 break;
1832 case DW_TAG_class_type:
1833 dwarf2_parse_udt_type(ctx, di, UdtClass);
1834 break;
1835 case DW_TAG_structure_type:
1836 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1837 break;
1838 case DW_TAG_union_type:
1839 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1840 break;
1841 case DW_TAG_array_type:
1842 dwarf2_parse_array_type(ctx, di);
1843 break;
1844 case DW_TAG_const_type:
1845 dwarf2_parse_const_type(ctx, di);
1846 break;
1847 case DW_TAG_volatile_type:
1848 dwarf2_parse_volatile_type(ctx, di);
1849 break;
1850 case DW_TAG_reference_type:
1851 dwarf2_parse_reference_type(ctx, di);
1852 break;
1853 case DW_TAG_enumeration_type:
1854 dwarf2_parse_enumeration_type(ctx, di);
1855 break;
1856 case DW_TAG_subprogram:
1857 dwarf2_parse_subprogram(ctx, di, compiland);
1858 break;
1859 case DW_TAG_subroutine_type:
1860 dwarf2_parse_subroutine_type(ctx, di);
1861 break;
1862 case DW_TAG_variable:
1864 dwarf2_subprogram_t subpgm;
1866 subpgm.ctx = ctx;
1867 subpgm.compiland = compiland;
1868 subpgm.func = NULL;
1869 subpgm.frame.kind = loc_absolute;
1870 subpgm.frame.offset = 0;
1871 subpgm.frame.reg = Wine_DW_no_register;
1872 dwarf2_parse_variable(&subpgm, NULL, di);
1874 break;
1875 /* silence a couple of C++ defines */
1876 case DW_TAG_namespace:
1877 case DW_TAG_imported_module:
1878 case DW_TAG_imported_declaration:
1879 break;
1880 default:
1881 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1882 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1886 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1887 const struct vector* v, unsigned file, unsigned line)
1889 struct symt_function* func;
1890 struct symt_ht* symt;
1891 unsigned* psrc;
1893 if (!file || !(psrc = vector_at(v, file - 1))) return;
1895 TRACE("%s %lx %s %u\n",
1896 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1897 if (!(symt = symt_find_nearest(module, address)) ||
1898 symt->symt.tag != SymTagFunction) return;
1899 func = (struct symt_function*)symt;
1900 symt_add_func_line(module, func, *psrc, line, address - func->address);
1903 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1904 dwarf2_parse_context_t* ctx,
1905 const char* compile_dir,
1906 unsigned long offset)
1908 dwarf2_traverse_context_t traverse;
1909 unsigned long length;
1910 unsigned version, header_len, insn_size, default_stmt;
1911 unsigned line_range, opcode_base;
1912 int line_base;
1913 const unsigned char* opcode_len;
1914 struct vector dirs;
1915 struct vector files;
1916 const char** p;
1918 /* section with line numbers stripped */
1919 if (sections[section_line].address == IMAGE_NO_MAP)
1920 return FALSE;
1922 traverse.data = sections[section_line].address + offset;
1923 traverse.end_data = traverse.data + 4;
1924 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1926 length = dwarf2_parse_u4(&traverse);
1927 traverse.end_data = sections[section_line].address + offset + length;
1929 version = dwarf2_parse_u2(&traverse);
1930 header_len = dwarf2_parse_u4(&traverse);
1931 insn_size = dwarf2_parse_byte(&traverse);
1932 default_stmt = dwarf2_parse_byte(&traverse);
1933 line_base = (signed char)dwarf2_parse_byte(&traverse);
1934 line_range = dwarf2_parse_byte(&traverse);
1935 opcode_base = dwarf2_parse_byte(&traverse);
1937 opcode_len = traverse.data;
1938 traverse.data += opcode_base - 1;
1940 vector_init(&dirs, sizeof(const char*), 4);
1941 p = vector_add(&dirs, &ctx->pool);
1942 *p = compile_dir ? compile_dir : ".";
1943 while (*traverse.data)
1945 const char* rel = (const char*)traverse.data;
1946 unsigned rellen = strlen(rel);
1947 TRACE("Got include %s\n", rel);
1948 traverse.data += rellen + 1;
1949 p = vector_add(&dirs, &ctx->pool);
1951 if (*rel == '/' || !compile_dir)
1952 *p = rel;
1953 else
1955 /* include directory relative to compile directory */
1956 unsigned baselen = strlen(compile_dir);
1957 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1958 strcpy(tmp, compile_dir);
1959 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1960 strcpy(&tmp[baselen], rel);
1961 *p = tmp;
1965 traverse.data++;
1967 vector_init(&files, sizeof(unsigned), 16);
1968 while (*traverse.data)
1970 unsigned int dir_index, mod_time, length;
1971 const char* name;
1972 const char* dir;
1973 unsigned* psrc;
1975 name = (const char*)traverse.data;
1976 traverse.data += strlen(name) + 1;
1977 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1978 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1979 length = dwarf2_leb128_as_unsigned(&traverse);
1980 dir = *(const char**)vector_at(&dirs, dir_index);
1981 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1982 psrc = vector_add(&files, &ctx->pool);
1983 *psrc = source_new(ctx->module, dir, name);
1985 traverse.data++;
1987 while (traverse.data < traverse.end_data)
1989 unsigned long address = 0;
1990 unsigned file = 1;
1991 unsigned line = 1;
1992 unsigned is_stmt = default_stmt;
1993 BOOL basic_block = FALSE, end_sequence = FALSE;
1994 unsigned opcode, extopcode, i;
1996 while (!end_sequence)
1998 opcode = dwarf2_parse_byte(&traverse);
1999 TRACE("Got opcode %x\n", opcode);
2001 if (opcode >= opcode_base)
2003 unsigned delta = opcode - opcode_base;
2005 address += (delta / line_range) * insn_size;
2006 line += line_base + (delta % line_range);
2007 basic_block = TRUE;
2008 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2010 else
2012 switch (opcode)
2014 case DW_LNS_copy:
2015 basic_block = FALSE;
2016 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2017 break;
2018 case DW_LNS_advance_pc:
2019 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2020 break;
2021 case DW_LNS_advance_line:
2022 line += dwarf2_leb128_as_signed(&traverse);
2023 break;
2024 case DW_LNS_set_file:
2025 file = dwarf2_leb128_as_unsigned(&traverse);
2026 break;
2027 case DW_LNS_set_column:
2028 dwarf2_leb128_as_unsigned(&traverse);
2029 break;
2030 case DW_LNS_negate_stmt:
2031 is_stmt = !is_stmt;
2032 break;
2033 case DW_LNS_set_basic_block:
2034 basic_block = 1;
2035 break;
2036 case DW_LNS_const_add_pc:
2037 address += ((255 - opcode_base) / line_range) * insn_size;
2038 break;
2039 case DW_LNS_fixed_advance_pc:
2040 address += dwarf2_parse_u2(&traverse);
2041 break;
2042 case DW_LNS_extended_op:
2043 dwarf2_leb128_as_unsigned(&traverse);
2044 extopcode = dwarf2_parse_byte(&traverse);
2045 switch (extopcode)
2047 case DW_LNE_end_sequence:
2048 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2049 end_sequence = TRUE;
2050 break;
2051 case DW_LNE_set_address:
2052 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2053 break;
2054 case DW_LNE_define_file:
2055 FIXME("not handled %s\n", traverse.data);
2056 traverse.data += strlen((const char *)traverse.data) + 1;
2057 dwarf2_leb128_as_unsigned(&traverse);
2058 dwarf2_leb128_as_unsigned(&traverse);
2059 dwarf2_leb128_as_unsigned(&traverse);
2060 break;
2061 default:
2062 FIXME("Unsupported extended opcode %x\n", extopcode);
2063 break;
2065 break;
2066 default:
2067 WARN("Unsupported opcode %x\n", opcode);
2068 for (i = 0; i < opcode_len[opcode]; i++)
2069 dwarf2_leb128_as_unsigned(&traverse);
2070 break;
2075 return TRUE;
2078 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2079 struct module* module,
2080 const struct elf_thunk_area* thunks,
2081 dwarf2_traverse_context_t* mod_ctx,
2082 unsigned long load_offset)
2084 dwarf2_parse_context_t ctx;
2085 dwarf2_traverse_context_t abbrev_ctx;
2086 dwarf2_debug_info_t* di;
2087 dwarf2_traverse_context_t cu_ctx;
2088 const unsigned char* comp_unit_start = mod_ctx->data;
2089 unsigned long cu_length;
2090 unsigned short cu_version;
2091 unsigned long cu_abbrev_offset;
2092 BOOL ret = FALSE;
2094 cu_length = dwarf2_parse_u4(mod_ctx);
2095 cu_ctx.data = mod_ctx->data;
2096 cu_ctx.end_data = mod_ctx->data + cu_length;
2097 mod_ctx->data += cu_length;
2098 cu_version = dwarf2_parse_u2(&cu_ctx);
2099 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2100 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2102 TRACE("Compilation Unit Header found at 0x%x:\n",
2103 (int)(comp_unit_start - sections[section_debug].address));
2104 TRACE("- length: %lu\n", cu_length);
2105 TRACE("- version: %u\n", cu_version);
2106 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2107 TRACE("- word_size: %u\n", cu_ctx.word_size);
2109 if (cu_version != 2)
2111 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2112 cu_version);
2113 return FALSE;
2116 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2117 mod_ctx->word_size = cu_ctx.word_size;
2119 pool_init(&ctx.pool, 65536);
2120 ctx.sections = sections;
2121 ctx.section = section_debug;
2122 ctx.module = module;
2123 ctx.thunks = thunks;
2124 ctx.load_offset = load_offset;
2125 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2126 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2127 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2129 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2130 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2131 abbrev_ctx.word_size = cu_ctx.word_size;
2132 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2134 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2135 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2137 if (di->abbrev->tag == DW_TAG_compile_unit)
2139 struct attribute name;
2140 dwarf2_debug_info_t** pdi = NULL;
2141 struct attribute stmt_list, low_pc;
2142 struct attribute comp_dir;
2144 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2145 name.u.string = NULL;
2147 /* get working directory of current compilation unit */
2148 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2149 comp_dir.u.string = NULL;
2151 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2152 low_pc.u.uvalue = 0;
2153 di->symt = &symt_new_compiland(module,
2154 ctx.load_offset + low_pc.u.uvalue,
2155 source_new(module, comp_dir.u.string, name.u.string))->symt;
2157 if (di->abbrev->have_child)
2159 unsigned int i;
2160 for (i=0; i<vector_length(&di->children); i++)
2162 pdi = vector_at(&di->children, i);
2163 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2166 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2168 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2169 module->module.LineNumbers = TRUE;
2171 ret = TRUE;
2173 else FIXME("Should have a compilation unit here\n");
2174 pool_destroy(&ctx.pool);
2175 return ret;
2178 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2179 unsigned long ip, dwarf2_traverse_context_t* lctx)
2181 DWORD_PTR beg, end;
2182 const BYTE* ptr = start;
2183 DWORD len;
2185 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2187 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2188 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2189 if (!beg && !end) break;
2190 len = dwarf2_get_u2(ptr); ptr += 2;
2192 if (beg <= ip && ip < end)
2194 lctx->data = ptr;
2195 lctx->end_data = ptr + len;
2196 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2197 return TRUE;
2199 ptr += len;
2201 WARN("Couldn't find ip in location list\n");
2202 return FALSE;
2205 static enum location_error loc_compute_frame(struct process* pcs,
2206 const struct module_format* modfmt,
2207 const struct symt_function* func,
2208 DWORD_PTR ip, struct location* frame)
2210 struct symt** psym = NULL;
2211 struct location* pframe;
2212 dwarf2_traverse_context_t lctx;
2213 enum location_error err;
2214 unsigned int i;
2216 for (i=0; i<vector_length(&func->vchildren); i++)
2218 psym = vector_at(&func->vchildren, i);
2219 if ((*psym)->tag == SymTagCustom)
2221 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2223 /* First, recompute the frame information, if needed */
2224 switch (pframe->kind)
2226 case loc_regrel:
2227 case loc_register:
2228 *frame = *pframe;
2229 break;
2230 case loc_dwarf2_location_list:
2231 WARN("Searching loclist for %s\n", func->hash_elt.name);
2232 if (!dwarf2_lookup_loclist(modfmt,
2233 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2234 ip, &lctx))
2235 return loc_err_out_of_scope;
2236 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2237 if (frame->kind >= loc_user)
2239 WARN("Couldn't compute runtime frame location\n");
2240 return loc_err_too_complex;
2242 break;
2243 default:
2244 WARN("Unsupported frame kind %d\n", pframe->kind);
2245 return loc_err_internal;
2247 return 0;
2250 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2251 return loc_err_internal;
2254 static void dwarf2_location_compute(struct process* pcs,
2255 const struct module_format* modfmt,
2256 const struct symt_function* func,
2257 struct location* loc)
2259 struct location frame;
2260 DWORD_PTR ip;
2261 int err;
2262 dwarf2_traverse_context_t lctx;
2264 if (!func->container || func->container->tag != SymTagCompiland)
2266 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2267 err = loc_err_internal;
2269 else
2271 /* instruction pointer relative to compiland's start */
2272 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2274 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
2276 switch (loc->kind)
2278 case loc_dwarf2_location_list:
2279 /* Then, if the variable has a location list, find it !! */
2280 if (dwarf2_lookup_loclist(modfmt,
2281 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
2282 ip, &lctx))
2283 goto do_compute;
2284 err = loc_err_out_of_scope;
2285 break;
2286 case loc_dwarf2_block:
2287 /* or if we have a copy of an existing block, get ready for it */
2289 unsigned* ptr = (unsigned*)loc->offset;
2291 lctx.data = (const BYTE*)(ptr + 1);
2292 lctx.end_data = lctx.data + *ptr;
2293 lctx.word_size = modfmt->u.dwarf2_info->word_size;
2295 do_compute:
2296 /* now get the variable */
2297 err = compute_location(&lctx, loc, pcs->handle, &frame);
2298 break;
2299 case loc_register:
2300 case loc_regrel:
2301 /* nothing to do */
2302 break;
2303 default:
2304 WARN("Unsupported local kind %d\n", loc->kind);
2305 err = loc_err_internal;
2309 if (err < 0)
2311 loc->kind = loc_register;
2312 loc->reg = err;
2316 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
2318 HeapFree(GetProcessHeap(), 0, modfmt);
2321 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
2322 const char* sectname, struct image_section_map* ism)
2324 struct image_section_map local_ism;
2326 if (!ism) ism = &local_ism;
2327 if (!image_find_section(fmap, sectname, ism))
2329 section->address = NULL;
2330 section->size = 0;
2331 section->rva = 0;
2332 return FALSE;
2335 section->address = (const BYTE*)image_map_section(ism);
2336 section->size = image_get_map_size(ism);
2337 section->rva = image_get_map_rva(ism);
2338 return TRUE;
2341 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2342 const struct elf_thunk_area* thunks,
2343 struct image_file_map* fmap)
2345 dwarf2_section_t section[section_max];
2346 dwarf2_traverse_context_t mod_ctx;
2347 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
2348 debug_line_sect;
2349 BOOL ret = TRUE;
2350 struct module_format* dwarf2_modfmt;
2352 if (!dwarf2_init_section(&section[section_debug], fmap, ".debug_info", &debug_sect))
2354 /* no Dwarf debug info here, so there's no error */
2355 return TRUE;
2357 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
2358 dwarf2_init_section(&section[section_string], fmap, ".debug_str", &debug_str_sect);
2359 dwarf2_init_section(&section[section_line], fmap, ".debug_line", &debug_line_sect);
2361 if (section[section_debug].address == IMAGE_NO_MAP ||
2362 section[section_abbrev].address == IMAGE_NO_MAP ||
2363 section[section_string].address == IMAGE_NO_MAP)
2365 ret = FALSE;
2366 goto leave;
2369 if (fmap->modtype == DMT_ELF)
2371 /* debug info might have a different base address than .so file
2372 * when elf file is prelinked after splitting off debug info
2373 * adjust symbol base addresses accordingly
2375 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
2378 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
2380 mod_ctx.data = section[section_debug].address;
2381 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
2382 mod_ctx.word_size = 0; /* will be correctly set later on */
2384 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
2385 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
2386 if (!dwarf2_modfmt)
2388 ret = FALSE;
2389 goto leave;
2391 dwarf2_modfmt->module = module;
2392 dwarf2_modfmt->remove = dwarf2_module_remove;
2393 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
2394 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
2395 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
2396 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
2398 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", NULL);
2400 while (mod_ctx.data < mod_ctx.end_data)
2402 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
2404 dwarf2_modfmt->module->module.SymType = SymDia;
2405 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2406 /* FIXME: we could have a finer grain here */
2407 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
2408 dwarf2_modfmt->module->module.TypeInfo = TRUE;
2409 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
2410 dwarf2_modfmt->module->module.Publics = TRUE;
2412 leave:
2413 image_unmap_section(&debug_sect);
2414 image_unmap_section(&debug_abbrev_sect);
2415 image_unmap_section(&debug_str_sect);
2416 image_unmap_section(&debug_line_sect);
2418 return ret;