dmusic: Assign to structs instead of using memcpy.
[wine.git] / dlls / dbghelp / dwarf.c
blob81f3c7f420b0fc10214878c1f9f40aab5e681a24
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 #include "config.h"
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <stdio.h>
39 #ifndef PATH_MAX
40 #define PATH_MAX MAX_PATH
41 #endif
42 #include <assert.h>
43 #include <stdarg.h>
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winuser.h"
48 #include "ole2.h"
49 #include "oleauto.h"
51 #include "dbghelp_private.h"
53 #include "wine/debug.h"
55 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
57 /* FIXME:
58 * - Functions:
59 * o unspecified parameters
60 * o inlined functions
61 * o Debug{Start|End}Point
62 * o CFA
63 * - Udt
64 * o proper types loading (nesting)
67 #if 0
68 static void dump(const void* ptr, unsigned len)
70 int i, j;
71 BYTE msg[128];
72 static const char hexof[] = "0123456789abcdef";
73 const BYTE* x = (const BYTE*)ptr;
75 for (i = 0; i < len; i += 16)
77 sprintf(msg, "%08x: ", i);
78 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
79 for (j = 0; j < min(16, len - i); j++)
81 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
82 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
83 msg[10 + 3 * j + 2] = ' ';
84 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
85 x[i + j] : '.';
87 msg[10 + 3 * 16] = ' ';
88 msg[10 + 3 * 16 + 1 + 16] = '\0';
89 TRACE("%s\n", msg);
92 #endif
94 /**
96 * Main Specs:
97 * http://www.eagercon.com/dwarf/dwarf3std.htm
98 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
100 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
102 * example of projects who do dwarf2 parsing:
103 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
104 * http://elis.ugent.be/diota/log/ltrace_elf.c
106 #include "dwarf.h"
109 * Parsers
112 typedef struct dwarf2_abbrev_entry_attr_s
114 unsigned long attribute;
115 unsigned long form;
116 struct dwarf2_abbrev_entry_attr_s* next;
117 } dwarf2_abbrev_entry_attr_t;
119 typedef struct dwarf2_abbrev_entry_s
121 unsigned long entry_code;
122 unsigned long tag;
123 unsigned char have_child;
124 unsigned num_attr;
125 dwarf2_abbrev_entry_attr_t* attrs;
126 } dwarf2_abbrev_entry_t;
128 struct dwarf2_block
130 unsigned size;
131 const unsigned char* ptr;
134 struct attribute
136 unsigned long form;
137 union
139 unsigned long uvalue;
140 long svalue;
141 const char* string;
142 struct dwarf2_block block;
143 } u;
146 typedef struct dwarf2_debug_info_s
148 const dwarf2_abbrev_entry_t*abbrev;
149 struct symt* symt;
150 const unsigned char** data;
151 struct vector children;
152 } dwarf2_debug_info_t;
154 typedef struct dwarf2_section_s
156 const unsigned char* address;
157 unsigned size;
158 } dwarf2_section_t;
160 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
162 typedef struct dwarf2_traverse_context_s
164 const unsigned char* data;
165 const unsigned char* start_data;
166 const unsigned char* end_data;
167 unsigned char word_size;
168 } dwarf2_traverse_context_t;
170 /* symt_cache indexes */
171 #define sc_void 0
172 #define sc_int1 1
173 #define sc_int2 2
174 #define sc_int4 3
175 #define sc_num 4
177 typedef struct dwarf2_parse_context_s
179 const dwarf2_section_t* sections;
180 unsigned section;
181 struct pool pool;
182 struct module* module;
183 const struct elf_thunk_area*thunks;
184 struct sparse_array abbrev_table;
185 struct sparse_array debug_info_table;
186 unsigned long load_offset;
187 unsigned long ref_offset;
188 unsigned char word_size;
189 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
190 } dwarf2_parse_context_t;
192 /* stored in the dbghelp's module internal structure for later reuse */
193 struct dwarf2_module_info_s
195 dwarf2_section_t debug_loc;
198 #define loc_dwarf2_location_list (loc_user + 0)
199 #define loc_dwarf2_block (loc_user + 1)
201 /* forward declarations */
202 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
204 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
206 return *ptr;
209 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
211 unsigned char uvalue = dwarf2_get_byte(ctx->data);
212 ctx->data += 1;
213 return uvalue;
216 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
218 return *(const unsigned short*)ptr;
221 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
223 unsigned short uvalue = dwarf2_get_u2(ctx->data);
224 ctx->data += 2;
225 return uvalue;
228 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
230 return *(const unsigned long*)ptr;
233 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
235 unsigned long uvalue = dwarf2_get_u4(ctx->data);
236 ctx->data += 4;
237 return uvalue;
240 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
242 unsigned long ret = 0;
243 unsigned char byte;
244 unsigned shift = 0;
248 byte = dwarf2_get_byte(ptr++);
249 ret |= (byte & 0x7f) << shift;
250 shift += 7;
251 } while (byte & 0x80);
253 if (end) *end = ptr;
254 return ret;
257 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
259 unsigned long ret;
261 assert(ctx);
263 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
265 return ret;
268 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
270 long ret = 0;
271 unsigned char byte;
272 unsigned shift = 0;
273 const unsigned size = sizeof(int) * 8;
277 byte = dwarf2_get_byte(ptr++);
278 ret |= (byte & 0x7f) << shift;
279 shift += 7;
280 } while (byte & 0x80);
281 if (end) *end = ptr;
283 /* as spec: sign bit of byte is 2nd high order bit (80x40)
284 * -> 0x80 is used as flag.
286 if ((shift < size) && (byte & 0x40))
288 ret |= - (1 << shift);
290 return ret;
293 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
295 long ret = 0;
297 assert(ctx);
299 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
300 return ret;
303 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
305 unsigned ret;
306 for (ret = 0; ctx->data[ret] & 0x80; ret++);
307 return ret + 1;
310 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
312 unsigned long ret;
314 switch (word_size)
316 case 4:
317 ret = dwarf2_get_u4(ptr);
318 break;
319 default:
320 FIXME("Unsupported Word Size %u\n", word_size);
321 ret = 0;
323 return ret;
326 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
328 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
329 ctx->data += ctx->word_size;
330 return ret;
333 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
335 return wine_dbg_sprintf("ctx(%p)", ctx->data);
338 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
340 return wine_dbg_sprintf("ctx(%p,%s)",
341 ctx, debugstr_w(ctx->module->module.ModuleName));
344 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
346 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
347 di->abbrev, di->symt);
350 static dwarf2_abbrev_entry_t*
351 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
352 unsigned long entry_code)
354 assert( NULL != abbrev_table );
355 return sparse_array_find(abbrev_table, entry_code);
358 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
359 struct sparse_array* abbrev_table,
360 struct pool* pool)
362 unsigned long entry_code;
363 dwarf2_abbrev_entry_t* abbrev_entry;
364 dwarf2_abbrev_entry_attr_t* new = NULL;
365 dwarf2_abbrev_entry_attr_t* last = NULL;
366 unsigned long attribute;
367 unsigned long form;
369 assert( NULL != abbrev_ctx );
371 TRACE("%s, end at %p\n",
372 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
374 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
375 while (abbrev_ctx->data < abbrev_ctx->end_data)
377 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
378 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
379 TRACE("found entry_code %lu\n", entry_code);
380 if (!entry_code)
382 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
383 break;
385 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
386 assert( NULL != abbrev_entry );
388 abbrev_entry->entry_code = entry_code;
389 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
390 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
391 abbrev_entry->attrs = NULL;
392 abbrev_entry->num_attr = 0;
394 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
395 abbrev_table, sparse_array_length(abbrev_table),
396 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
398 last = NULL;
399 while (1)
401 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
402 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
403 if (!attribute) break;
405 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
406 assert(new);
408 new->attribute = attribute;
409 new->form = form;
410 new->next = NULL;
411 if (abbrev_entry->attrs) last->next = new;
412 else abbrev_entry->attrs = new;
413 last = new;
414 abbrev_entry->num_attr++;
417 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
420 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
421 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
423 unsigned step;
425 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
427 switch (abbrev_attr->form)
429 case DW_FORM_ref_addr:
430 case DW_FORM_addr: step = ctx->word_size; break;
431 case DW_FORM_flag:
432 case DW_FORM_data1:
433 case DW_FORM_ref1: step = 1; break;
434 case DW_FORM_data2:
435 case DW_FORM_ref2: step = 2; break;
436 case DW_FORM_data4:
437 case DW_FORM_ref4:
438 case DW_FORM_strp: step = 4; break;
439 case DW_FORM_data8:
440 case DW_FORM_ref8: step = 8; break;
441 case DW_FORM_sdata:
442 case DW_FORM_ref_udata:
443 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
444 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
445 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
446 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
447 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
448 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
449 default:
450 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
451 return;
453 ctx->data += step;
456 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
457 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
458 const unsigned char* data,
459 struct attribute* attr)
461 attr->form = abbrev_attr->form;
462 switch (attr->form)
464 case DW_FORM_ref_addr:
465 case DW_FORM_addr:
466 attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
467 TRACE("addr<0x%lx>\n", attr->u.uvalue);
468 break;
470 case DW_FORM_flag:
471 attr->u.uvalue = dwarf2_get_byte(data);
472 TRACE("flag<0x%lx>\n", attr->u.uvalue);
473 break;
475 case DW_FORM_data1:
476 attr->u.uvalue = dwarf2_get_byte(data);
477 TRACE("data1<%lu>\n", attr->u.uvalue);
478 break;
480 case DW_FORM_data2:
481 attr->u.uvalue = dwarf2_get_u2(data);
482 TRACE("data2<%lu>\n", attr->u.uvalue);
483 break;
485 case DW_FORM_data4:
486 attr->u.uvalue = dwarf2_get_u4(data);
487 TRACE("data4<%lu>\n", attr->u.uvalue);
488 break;
490 case DW_FORM_data8:
491 FIXME("Unhandled 64bits support\n");
492 break;
494 case DW_FORM_ref1:
495 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
496 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
497 break;
499 case DW_FORM_ref2:
500 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
501 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
502 break;
504 case DW_FORM_ref4:
505 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
506 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
507 break;
509 case DW_FORM_ref8:
510 FIXME("Unhandled 64 bit support\n");
511 break;
513 case DW_FORM_sdata:
514 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
515 break;
517 case DW_FORM_ref_udata:
518 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
519 break;
521 case DW_FORM_udata:
522 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
523 break;
525 case DW_FORM_string:
526 attr->u.string = (const char *)data;
527 TRACE("string<%s>\n", attr->u.string);
528 break;
530 case DW_FORM_strp:
532 unsigned long offset = dwarf2_get_u4(data);
533 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
535 TRACE("strp<%s>\n", attr->u.string);
536 break;
538 case DW_FORM_block:
539 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
540 break;
542 case DW_FORM_block1:
543 attr->u.block.size = dwarf2_get_byte(data);
544 attr->u.block.ptr = data + 1;
545 break;
547 case DW_FORM_block2:
548 attr->u.block.size = dwarf2_get_u2(data);
549 attr->u.block.ptr = data + 2;
550 break;
552 case DW_FORM_block4:
553 attr->u.block.size = dwarf2_get_u4(data);
554 attr->u.block.ptr = data + 4;
555 break;
557 default:
558 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
559 break;
563 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
564 const dwarf2_debug_info_t* di,
565 unsigned at, struct attribute* attr)
567 unsigned i, ai = 0;
568 dwarf2_abbrev_entry_attr_t* abbrev_attr;
569 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
571 while (di)
573 abstract_abbrev_attr = NULL;
574 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
576 if (abbrev_attr->attribute == at)
578 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
579 return TRUE;
581 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
582 at != DW_AT_sibling)
584 abstract_abbrev_attr = abbrev_attr;
585 ai = i;
588 /* do we have an abstract origin debug entry to look into ? */
589 if (!abstract_abbrev_attr) break;
590 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
591 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
592 FIXME("Should have found the debug info entry\n");
594 return FALSE;
597 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
598 struct symt_compiland*);
600 #define Wine_DW_no_register 0x7FFFFFFF
602 static unsigned dwarf2_map_register(int regno)
604 unsigned reg;
606 switch (regno)
608 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
609 case 0: reg = CV_REG_EAX; break;
610 case 1: reg = CV_REG_ECX; break;
611 case 2: reg = CV_REG_EDX; break;
612 case 3: reg = CV_REG_EBX; break;
613 case 4: reg = CV_REG_ESP; break;
614 case 5: reg = CV_REG_EBP; break;
615 case 6: reg = CV_REG_ESI; break;
616 case 7: reg = CV_REG_EDI; break;
617 case 8: reg = CV_REG_EIP; break;
618 case 9: reg = CV_REG_EFLAGS; break;
619 case 10: reg = CV_REG_CS; break;
620 case 11: reg = CV_REG_SS; break;
621 case 12: reg = CV_REG_DS; break;
622 case 13: reg = CV_REG_ES; break;
623 case 14: reg = CV_REG_FS; break;
624 case 15: reg = CV_REG_GS; break;
625 case 16: case 17: case 18: case 19:
626 case 20: case 21: case 22: case 23:
627 reg = CV_REG_ST0 + regno - 16; break;
628 case 24: reg = CV_REG_CTRL; break;
629 case 25: reg = CV_REG_STAT; break;
630 case 26: reg = CV_REG_TAG; break;
632 reg: fiseg 27
633 reg: fioff 28
634 reg: foseg 29
635 reg: fooff 30
636 reg: fop 31
638 case 32: case 33: case 34: case 35:
639 case 36: case 37: case 38: case 39:
640 reg = CV_REG_XMM0 + regno - 32; break;
641 case 40: reg = CV_REG_MXCSR; break;
642 default:
643 FIXME("Don't know how to map register %d\n", regno);
644 return 0;
646 return reg;
649 static enum location_error
650 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
651 HANDLE hproc, const struct location* frame)
653 unsigned long stack[64];
654 unsigned stk;
655 unsigned char op;
656 BOOL piece_found = FALSE;
658 stack[stk = 0] = 0;
660 loc->kind = loc_absolute;
661 loc->reg = Wine_DW_no_register;
663 while (ctx->data < ctx->end_data)
665 op = dwarf2_parse_byte(ctx);
666 switch (op)
668 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
669 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
670 case DW_OP_const1s: stack[++stk] = (long)(signed char)dwarf2_parse_byte(ctx); break;
671 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
672 case DW_OP_const2s: stack[++stk] = (long)(short)dwarf2_parse_u2(ctx); break;
673 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
674 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
675 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
676 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
677 case DW_OP_plus_uconst:
678 stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
679 case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
680 case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
681 case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
682 case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
683 case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
684 case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
685 case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
686 case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
687 /* dbghelp APIs don't know how to cope with this anyway
688 * (for example 'long long' stored in two registers)
689 * FIXME: We should tell winedbg how to deal with it (sigh)
691 if (!piece_found)
693 if (loc->reg != Wine_DW_no_register)
694 FIXME("Only supporting one reg (%d -> %d)\n",
695 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
696 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
698 loc->kind = loc_register;
699 break;
700 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
701 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
702 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
703 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
704 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
705 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
706 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
707 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:
708 /* dbghelp APIs don't know how to cope with this anyway
709 * (for example 'long long' stored in two registers)
710 * FIXME: We should tell winedbg how to deal with it (sigh)
712 if (!piece_found)
714 if (loc->reg != Wine_DW_no_register)
715 FIXME("Only supporting one reg (%d -> %d)\n",
716 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
717 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
719 stack[++stk] = dwarf2_leb128_as_signed(ctx);
720 loc->kind = loc_regrel;
721 break;
722 case DW_OP_fbreg:
723 if (loc->reg != Wine_DW_no_register)
724 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
725 if (frame && frame->kind == loc_register)
727 loc->kind = loc_regrel;
728 loc->reg = frame->reg;
729 stack[++stk] = dwarf2_leb128_as_signed(ctx);
731 else if (frame && frame->kind == loc_regrel)
733 loc->kind = loc_regrel;
734 loc->reg = frame->reg;
735 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
737 else
739 /* FIXME: this could be later optimized by not recomputing
740 * this very location expression
742 loc->kind = loc_dwarf2_block;
743 stack[++stk] = dwarf2_leb128_as_signed(ctx);
745 break;
746 case DW_OP_piece:
748 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
749 WARN("Not handling OP_piece (size=%d)\n", sz);
750 piece_found = TRUE;
752 break;
753 case DW_OP_deref:
754 if (!stk)
756 FIXME("Unexpected empty stack\n");
757 return loc_err_internal;
759 if (loc->reg != Wine_DW_no_register)
761 WARN("Too complex expression for deref\n");
762 return loc_err_too_complex;
764 if (hproc)
766 DWORD addr = stack[stk--];
767 DWORD deref;
769 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
771 WARN("Couldn't read memory at %x\n", addr);
772 return loc_err_cant_read;
774 stack[++stk] = deref;
776 else
778 loc->kind = loc_dwarf2_block;
780 break;
781 default:
782 FIXME("Unhandled attr op: %x\n", op);
783 return loc_err_internal;
786 loc->offset = stack[stk];
787 return 0;
790 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
791 const dwarf2_debug_info_t* di,
792 unsigned long dw,
793 struct location* loc,
794 const struct location* frame)
796 struct attribute xloc;
798 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
800 switch (xloc.form)
802 case DW_FORM_data1: case DW_FORM_data2:
803 case DW_FORM_udata: case DW_FORM_sdata:
804 loc->kind = loc_absolute;
805 loc->reg = 0;
806 loc->offset = xloc.u.uvalue;
807 return TRUE;
808 case DW_FORM_data4: case DW_FORM_data8:
809 loc->kind = loc_dwarf2_location_list;
810 loc->reg = Wine_DW_no_register;
811 loc->offset = xloc.u.uvalue;
812 return TRUE;
815 /* assume we have a block form */
817 if (xloc.u.block.size)
819 dwarf2_traverse_context_t lctx;
820 enum location_error err;
822 lctx.data = xloc.u.block.ptr;
823 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
824 lctx.word_size = ctx->word_size;
826 err = compute_location(&lctx, loc, NULL, frame);
827 if (err < 0)
829 loc->kind = loc_error;
830 loc->reg = err;
832 else if (loc->kind == loc_dwarf2_block)
834 unsigned* ptr = pool_alloc(&ctx->module->pool,
835 sizeof(unsigned) + xloc.u.block.size);
836 *ptr = xloc.u.block.size;
837 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
838 loc->offset = (unsigned long)ptr;
841 return TRUE;
844 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
845 const dwarf2_debug_info_t* di)
847 struct attribute attr;
849 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
851 dwarf2_debug_info_t* type;
853 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
854 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
855 if (!type->symt)
857 /* load the debug info entity */
858 dwarf2_load_one_entry(ctx, type, NULL);
860 return type->symt;
862 return NULL;
865 /******************************************************************
866 * dwarf2_read_one_debug_info
868 * Loads into memory one debug info entry, and recursively its children (if any)
870 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
871 dwarf2_traverse_context_t* traverse,
872 dwarf2_debug_info_t** pdi)
874 const dwarf2_abbrev_entry_t*abbrev;
875 unsigned long entry_code;
876 unsigned long offset;
877 dwarf2_debug_info_t* di;
878 dwarf2_debug_info_t* child;
879 dwarf2_debug_info_t** where;
880 dwarf2_abbrev_entry_attr_t* attr;
881 unsigned i;
882 struct attribute sibling;
884 offset = traverse->data - ctx->sections[ctx->section].address;
885 entry_code = dwarf2_leb128_as_unsigned(traverse);
886 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
887 if (!entry_code)
889 *pdi = NULL;
890 return TRUE;
892 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
893 if (!abbrev)
895 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
896 return FALSE;
898 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
899 if (!di) return FALSE;
900 di->abbrev = abbrev;
901 di->symt = NULL;
903 if (abbrev->num_attr)
905 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
906 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
908 di->data[i] = traverse->data;
909 dwarf2_swallow_attribute(traverse, attr);
912 else di->data = NULL;
913 if (abbrev->have_child)
915 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
916 while (traverse->data < traverse->end_data)
918 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
919 if (!child) break;
920 where = vector_add(&di->children, &ctx->pool);
921 if (!where) return FALSE;
922 *where = child;
925 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
926 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
928 WARN("setting cursor for %s to next sibling <0x%lx>\n",
929 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
930 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
932 *pdi = di;
933 return TRUE;
936 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
937 dwarf2_debug_info_t* di)
939 struct attribute name;
940 struct attribute size;
941 struct attribute encoding;
942 enum BasicType bt;
943 int cache_idx = -1;
944 if (di->symt) return di->symt;
946 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
948 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
949 name.u.string = NULL;
950 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
951 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
953 switch (encoding.u.uvalue)
955 case DW_ATE_void: bt = btVoid; break;
956 case DW_ATE_address: bt = btULong; break;
957 case DW_ATE_boolean: bt = btBool; break;
958 case DW_ATE_complex_float: bt = btComplex; break;
959 case DW_ATE_float: bt = btFloat; break;
960 case DW_ATE_signed: bt = btInt; break;
961 case DW_ATE_unsigned: bt = btUInt; break;
962 case DW_ATE_signed_char: bt = btChar; break;
963 case DW_ATE_unsigned_char: bt = btChar; break;
964 default: bt = btNoType; break;
966 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
967 switch (bt)
969 case btVoid:
970 assert(size.u.uvalue == 0);
971 cache_idx = sc_void;
972 break;
973 case btInt:
974 switch (size.u.uvalue)
976 case 1: cache_idx = sc_int1; break;
977 case 2: cache_idx = sc_int2; break;
978 case 4: cache_idx = sc_int4; break;
980 break;
981 default: break;
983 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
984 ctx->symt_cache[cache_idx] = di->symt;
986 if (di->abbrev->have_child) FIXME("Unsupported children\n");
987 return di->symt;
990 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
991 dwarf2_debug_info_t* di)
993 struct symt* ref_type;
994 struct attribute name;
996 if (di->symt) return di->symt;
998 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1000 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1001 ref_type = dwarf2_lookup_type(ctx, di);
1003 if (name.u.string)
1004 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1005 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1006 return di->symt;
1009 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1010 dwarf2_debug_info_t* di)
1012 struct symt* ref_type;
1013 struct attribute size;
1015 if (di->symt) return di->symt;
1017 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1019 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1020 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1022 ref_type = ctx->symt_cache[sc_void];
1023 assert(ref_type);
1025 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1026 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1027 return di->symt;
1030 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1031 dwarf2_debug_info_t* di)
1033 struct symt* ref_type;
1034 struct symt* idx_type = NULL;
1035 struct attribute min, max, cnt;
1036 dwarf2_debug_info_t* child;
1037 int i;
1039 if (di->symt) return di->symt;
1041 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1043 if (!di->abbrev->have_child)
1045 FIXME("array without range information\n");
1046 return NULL;
1048 ref_type = dwarf2_lookup_type(ctx, di);
1050 for (i=0; i<vector_length(&di->children); i++)
1052 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1053 switch (child->abbrev->tag)
1055 case DW_TAG_subrange_type:
1056 idx_type = dwarf2_lookup_type(ctx, child);
1057 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1058 min.u.uvalue = 0;
1059 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1060 max.u.uvalue = 0;
1061 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1062 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1063 break;
1064 default:
1065 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1066 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1067 break;
1070 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1071 return di->symt;
1074 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1075 dwarf2_debug_info_t* di)
1077 struct symt* ref_type;
1079 if (di->symt) return di->symt;
1081 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1083 ref_type = dwarf2_lookup_type(ctx, di);
1084 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1085 di->symt = ref_type;
1087 return ref_type;
1090 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1091 dwarf2_debug_info_t* di)
1093 struct symt* ref_type;
1095 if (di->symt) return di->symt;
1097 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1099 ref_type = dwarf2_lookup_type(ctx, di);
1100 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1101 di->symt = ref_type;
1103 return ref_type;
1106 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1107 dwarf2_debug_info_t* di)
1109 struct symt* ref_type = NULL;
1111 if (di->symt) return di->symt;
1113 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1115 ref_type = dwarf2_lookup_type(ctx, di);
1116 /* FIXME: for now, we hard-wire C++ references to pointers */
1117 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1119 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1121 return di->symt;
1124 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1125 const dwarf2_debug_info_t* di,
1126 struct symt_udt* parent)
1128 struct symt* elt_type;
1129 struct attribute name;
1130 struct attribute bit_size;
1131 struct attribute bit_offset;
1132 struct location loc;
1134 assert(parent);
1136 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1138 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1139 elt_type = dwarf2_lookup_type(ctx, di);
1140 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1142 if (loc.kind != loc_absolute)
1144 FIXME("Found register, while not expecting it\n");
1145 loc.offset = 0;
1147 else
1148 TRACE("found member_location at %s -> %lu\n",
1149 dwarf2_debug_ctx(ctx), loc.offset);
1151 else
1152 loc.offset = 0;
1153 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1154 bit_size.u.uvalue = 0;
1155 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1157 /* FIXME: we should only do this when implementation is LSB (which is
1158 * the case on i386 processors)
1160 struct attribute nbytes;
1161 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1163 DWORD64 size;
1164 nbytes.u.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
1166 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1168 else bit_offset.u.uvalue = 0;
1169 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1170 (loc.offset << 3) + bit_offset.u.uvalue,
1171 bit_size.u.uvalue);
1173 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1176 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1177 dwarf2_debug_info_t* di,
1178 enum UdtKind udt)
1180 struct attribute name;
1181 struct attribute size;
1183 if (di->symt) return di->symt;
1185 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1187 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1188 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1190 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1192 if (di->abbrev->have_child) /** any interest to not have child ? */
1194 dwarf2_debug_info_t* child;
1195 int i;
1197 for (i=0; i<vector_length(&di->children); i++)
1199 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1201 switch (child->abbrev->tag)
1203 case DW_TAG_member:
1204 /* FIXME: should I follow the sibling stuff ?? */
1205 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1206 break;
1207 case DW_TAG_enumeration_type:
1208 dwarf2_parse_enumeration_type(ctx, child);
1209 break;
1210 case DW_TAG_structure_type:
1211 case DW_TAG_class_type:
1212 case DW_TAG_union_type:
1213 case DW_TAG_typedef:
1214 /* FIXME: we need to handle nested udt definitions */
1215 break;
1216 default:
1217 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1218 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1219 break;
1224 return di->symt;
1227 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1228 const dwarf2_debug_info_t* di,
1229 struct symt_enum* parent)
1231 struct attribute name;
1232 struct attribute value;
1234 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1236 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1237 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1238 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1240 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1243 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1244 dwarf2_debug_info_t* di)
1246 struct attribute name;
1247 struct attribute size;
1248 struct symt_basic* basetype;
1250 if (di->symt) return di->symt;
1252 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1254 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1255 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1257 switch (size.u.uvalue) /* FIXME: that's wrong */
1259 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1260 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1261 default:
1262 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1265 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1267 if (di->abbrev->have_child) /* any interest to not have child ? */
1269 dwarf2_debug_info_t* child;
1270 int i;
1272 /* FIXME: should we use the sibling stuff ?? */
1273 for (i=0; i<vector_length(&di->children); i++)
1275 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1277 switch (child->abbrev->tag)
1279 case DW_TAG_enumerator:
1280 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1281 break;
1282 default:
1283 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1284 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1288 return di->symt;
1291 /* structure used to pass information around when parsing a subprogram */
1292 typedef struct dwarf2_subprogram_s
1294 dwarf2_parse_context_t* ctx;
1295 struct symt_compiland* compiland;
1296 struct symt_function* func;
1297 BOOL non_computed_variable;
1298 struct location frame;
1299 } dwarf2_subprogram_t;
1301 /******************************************************************
1302 * dwarf2_parse_variable
1304 * Parses any variable (parameter, local/global variable)
1306 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1307 struct symt_block* block,
1308 dwarf2_debug_info_t* di)
1310 struct symt* param_type;
1311 struct attribute name, value;
1312 struct location loc;
1313 BOOL is_pmt;
1315 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1317 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1318 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1320 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1321 /* cannot do much without the name, the functions below won't like it. */
1322 return;
1324 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1325 &loc, &subpgm->frame))
1327 struct attribute ext;
1329 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1330 name.u.string, loc.kind, loc.offset, loc.reg,
1331 dwarf2_debug_ctx(subpgm->ctx));
1333 switch (loc.kind)
1335 case loc_absolute:
1336 /* it's a global variable */
1337 /* FIXME: we don't handle its scope yet */
1338 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1339 ext.u.uvalue = 0;
1340 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1341 name.u.string, !ext.u.uvalue,
1342 subpgm->ctx->load_offset + loc.offset,
1343 0, param_type);
1344 break;
1345 default:
1346 subpgm->non_computed_variable = TRUE;
1347 /* fall through */
1348 case loc_register:
1349 case loc_regrel:
1350 /* either a pmt/variable relative to frame pointer or
1351 * pmt/variable in a register
1353 assert(subpgm->func);
1354 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1355 is_pmt ? DataIsParam : DataIsLocal,
1356 &loc, block, param_type, name.u.string);
1357 break;
1360 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1362 VARIANT v;
1363 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1364 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1365 switch (value.form)
1367 case DW_FORM_data1:
1368 case DW_FORM_data2:
1369 case DW_FORM_data4:
1370 case DW_FORM_udata:
1371 case DW_FORM_addr:
1372 v.n1.n2.vt = VT_UI4;
1373 v.n1.n2.n3.lVal = value.u.uvalue;
1374 break;
1376 case DW_FORM_sdata:
1377 v.n1.n2.vt = VT_I4;
1378 v.n1.n2.n3.lVal = value.u.svalue;
1379 break;
1381 case DW_FORM_strp:
1382 case DW_FORM_string:
1383 /* FIXME: native doesn't report const strings from here !!
1384 * however, the value of the string is in the code somewhere
1386 v.n1.n2.vt = VT_I1 | VT_BYREF;
1387 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1388 break;
1390 case DW_FORM_data8:
1391 case DW_FORM_block:
1392 case DW_FORM_block1:
1393 case DW_FORM_block2:
1394 case DW_FORM_block4:
1396 default:
1397 FIXME("Unsupported form for const value %s (%lx)\n",
1398 name.u.string, value.form);
1399 v.n1.n2.vt = VT_EMPTY;
1401 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1402 name.u.string, param_type, &v)->symt;
1404 if (is_pmt && subpgm->func && subpgm->func->type)
1405 symt_add_function_signature_parameter(subpgm->ctx->module,
1406 (struct symt_function_signature*)subpgm->func->type,
1407 param_type);
1409 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1412 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1413 const dwarf2_debug_info_t* di)
1415 struct attribute name;
1416 struct attribute low_pc;
1417 struct location loc;
1419 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1421 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1422 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1423 name.u.string = NULL;
1425 loc.kind = loc_absolute;
1426 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1427 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1428 &loc, name.u.string);
1431 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1432 struct symt_block* parent_block,
1433 const dwarf2_debug_info_t* di);
1435 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1436 struct symt_block* parent_block,
1437 const dwarf2_debug_info_t* di)
1439 struct symt_block* block;
1440 struct attribute low_pc;
1441 struct attribute high_pc;
1443 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1445 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1446 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1448 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1449 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1450 high_pc.u.uvalue - low_pc.u.uvalue);
1452 if (di->abbrev->have_child) /** any interest to not have child ? */
1454 dwarf2_debug_info_t* child;
1455 int i;
1457 for (i=0; i<vector_length(&di->children); i++)
1459 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1461 switch (child->abbrev->tag)
1463 case DW_TAG_formal_parameter:
1464 case DW_TAG_variable:
1465 dwarf2_parse_variable(subpgm, block, child);
1466 break;
1467 case DW_TAG_lexical_block:
1468 dwarf2_parse_subprogram_block(subpgm, block, child);
1469 break;
1470 case DW_TAG_inlined_subroutine:
1471 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1472 break;
1473 case DW_TAG_label:
1474 dwarf2_parse_subprogram_label(subpgm, child);
1475 break;
1476 default:
1477 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1478 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1479 dwarf2_debug_di(di));
1483 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1486 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1487 struct symt_block* parent_block,
1488 const dwarf2_debug_info_t* di)
1490 struct symt_block* block;
1491 struct attribute low_pc;
1492 struct attribute high_pc;
1494 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1496 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1497 low_pc.u.uvalue = 0;
1498 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1499 high_pc.u.uvalue = 0;
1501 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1502 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1503 high_pc.u.uvalue - low_pc.u.uvalue);
1505 if (di->abbrev->have_child) /** any interest to not have child ? */
1507 dwarf2_debug_info_t* child;
1508 int i;
1510 for (i=0; i<vector_length(&di->children); i++)
1512 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1514 switch (child->abbrev->tag)
1516 case DW_TAG_inlined_subroutine:
1517 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1518 break;
1519 case DW_TAG_variable:
1520 dwarf2_parse_variable(subpgm, block, child);
1521 break;
1522 case DW_TAG_lexical_block:
1523 dwarf2_parse_subprogram_block(subpgm, block, child);
1524 break;
1525 case DW_TAG_subprogram:
1526 /* FIXME: likely a declaration (to be checked)
1527 * skip it for now
1529 break;
1530 case DW_TAG_formal_parameter:
1531 /* FIXME: likely elements for exception handling (GCC flavor)
1532 * Skip it for now
1534 break;
1535 case DW_TAG_label:
1536 dwarf2_parse_subprogram_label(subpgm, child);
1537 break;
1538 case DW_TAG_class_type:
1539 case DW_TAG_structure_type:
1540 case DW_TAG_union_type:
1541 case DW_TAG_enumeration_type:
1542 case DW_TAG_typedef:
1543 /* the type referred to will be loaded when we need it, so skip it */
1544 break;
1545 default:
1546 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1547 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1552 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1555 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1556 dwarf2_debug_info_t* di,
1557 struct symt_compiland* compiland)
1559 struct attribute name;
1560 struct attribute low_pc;
1561 struct attribute high_pc;
1562 struct attribute is_decl;
1563 struct attribute inline_flags;
1564 struct symt* ret_type;
1565 struct symt_function_signature* sig_type;
1566 dwarf2_subprogram_t subpgm;
1568 if (di->symt) return di->symt;
1570 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1572 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1573 /* if it's an abstract representation of an inline function, there should be
1574 * a concrete object that we'll handle
1576 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1578 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1579 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1580 return NULL;
1583 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1584 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1585 /* As functions (defined as inline assembly) get debug info with dwarf
1586 * (not the case for stabs), we just drop Wine's thunks here...
1587 * Actual thunks will be created in elf_module from the symbol table
1589 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1590 ctx->thunks) >= 0)
1591 return NULL;
1592 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1593 is_decl.u.uvalue = 0;
1595 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1597 ret_type = ctx->symt_cache[sc_void];
1598 assert(ret_type);
1601 /* FIXME: assuming C source code */
1602 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1603 if (!is_decl.u.uvalue)
1605 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1606 ctx->load_offset + low_pc.u.uvalue,
1607 high_pc.u.uvalue - low_pc.u.uvalue,
1608 &sig_type->symt);
1609 di->symt = &subpgm.func->symt;
1611 else subpgm.func = NULL;
1613 subpgm.ctx = ctx;
1614 subpgm.compiland = compiland;
1615 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1616 &subpgm.frame, NULL))
1618 /* on stack !! */
1619 subpgm.frame.kind = loc_regrel;
1620 subpgm.frame.reg = 0;
1621 subpgm.frame.offset = 0;
1623 subpgm.non_computed_variable = FALSE;
1625 if (di->abbrev->have_child) /** any interest to not have child ? */
1627 dwarf2_debug_info_t* child;
1628 int i;
1630 for (i=0; i<vector_length(&di->children); i++)
1632 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1634 switch (child->abbrev->tag)
1636 case DW_TAG_variable:
1637 case DW_TAG_formal_parameter:
1638 dwarf2_parse_variable(&subpgm, NULL, child);
1639 break;
1640 case DW_TAG_lexical_block:
1641 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1642 break;
1643 case DW_TAG_inlined_subroutine:
1644 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1645 break;
1646 case DW_TAG_subprogram:
1647 /* FIXME: likely a declaration (to be checked)
1648 * skip it for now
1650 break;
1651 case DW_TAG_label:
1652 dwarf2_parse_subprogram_label(&subpgm, child);
1653 break;
1654 case DW_TAG_class_type:
1655 case DW_TAG_structure_type:
1656 case DW_TAG_union_type:
1657 case DW_TAG_enumeration_type:
1658 case DW_TAG_typedef:
1659 /* the type referred to will be loaded when we need it, so skip it */
1660 break;
1661 case DW_TAG_unspecified_parameters:
1662 /* FIXME: no support in dbghelp's internals so far */
1663 break;
1664 default:
1665 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1666 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1671 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1673 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1674 &subpgm.frame, NULL);
1676 symt_normalize_function(subpgm.ctx->module, subpgm.func);
1678 return di->symt;
1681 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1682 dwarf2_debug_info_t* di)
1684 struct symt* ret_type;
1685 struct symt_function_signature* sig_type;
1687 if (di->symt) return di->symt;
1689 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1691 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1693 ret_type = ctx->symt_cache[sc_void];
1694 assert(ret_type);
1697 /* FIXME: assuming C source code */
1698 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1700 if (di->abbrev->have_child) /** any interest to not have child ? */
1702 dwarf2_debug_info_t* child;
1703 int i;
1705 for (i=0; i<vector_length(&di->children); i++)
1707 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1709 switch (child->abbrev->tag)
1711 case DW_TAG_formal_parameter:
1712 symt_add_function_signature_parameter(ctx->module, sig_type,
1713 dwarf2_lookup_type(ctx, child));
1714 break;
1715 case DW_TAG_unspecified_parameters:
1716 WARN("Unsupported unspecified parameters\n");
1717 break;
1722 return di->symt = &sig_type->symt;
1725 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1726 dwarf2_debug_info_t* di,
1727 struct symt_compiland* compiland)
1729 switch (di->abbrev->tag)
1731 case DW_TAG_typedef:
1732 dwarf2_parse_typedef(ctx, di);
1733 break;
1734 case DW_TAG_base_type:
1735 dwarf2_parse_base_type(ctx, di);
1736 break;
1737 case DW_TAG_pointer_type:
1738 dwarf2_parse_pointer_type(ctx, di);
1739 break;
1740 case DW_TAG_class_type:
1741 dwarf2_parse_udt_type(ctx, di, UdtClass);
1742 break;
1743 case DW_TAG_structure_type:
1744 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1745 break;
1746 case DW_TAG_union_type:
1747 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1748 break;
1749 case DW_TAG_array_type:
1750 dwarf2_parse_array_type(ctx, di);
1751 break;
1752 case DW_TAG_const_type:
1753 dwarf2_parse_const_type(ctx, di);
1754 break;
1755 case DW_TAG_volatile_type:
1756 dwarf2_parse_volatile_type(ctx, di);
1757 break;
1758 case DW_TAG_reference_type:
1759 dwarf2_parse_reference_type(ctx, di);
1760 break;
1761 case DW_TAG_enumeration_type:
1762 dwarf2_parse_enumeration_type(ctx, di);
1763 break;
1764 case DW_TAG_subprogram:
1765 dwarf2_parse_subprogram(ctx, di, compiland);
1766 break;
1767 case DW_TAG_subroutine_type:
1768 dwarf2_parse_subroutine_type(ctx, di);
1769 break;
1770 case DW_TAG_variable:
1772 dwarf2_subprogram_t subpgm;
1774 subpgm.ctx = ctx;
1775 subpgm.compiland = compiland;
1776 subpgm.func = NULL;
1777 subpgm.frame.kind = loc_absolute;
1778 subpgm.frame.offset = 0;
1779 subpgm.frame.reg = Wine_DW_no_register;
1780 dwarf2_parse_variable(&subpgm, NULL, di);
1782 break;
1783 default:
1784 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1785 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1789 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1790 const struct vector* v, unsigned file, unsigned line)
1792 struct symt_function* func;
1793 struct symt_ht* symt;
1794 unsigned* psrc;
1796 if (!file || !(psrc = vector_at(v, file - 1))) return;
1798 TRACE("%s %lx %s %u\n",
1799 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1800 if (!(symt = symt_find_nearest(module, address)) ||
1801 symt->symt.tag != SymTagFunction) return;
1802 func = (struct symt_function*)symt;
1803 symt_add_func_line(module, func, *psrc, line, address - func->address);
1806 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1807 dwarf2_parse_context_t* ctx,
1808 const char* compile_dir,
1809 unsigned long offset)
1811 dwarf2_traverse_context_t traverse;
1812 unsigned long length;
1813 unsigned version, header_len, insn_size, default_stmt;
1814 unsigned line_range, opcode_base;
1815 int line_base;
1816 const unsigned char* opcode_len;
1817 struct vector dirs;
1818 struct vector files;
1819 const char** p;
1821 /* section with line numbers stripped */
1822 if (sections[section_line].address == ELF_NO_MAP)
1823 return FALSE;
1825 traverse.data = sections[section_line].address + offset;
1826 traverse.start_data = traverse.data;
1827 traverse.end_data = traverse.data + 4;
1828 traverse.word_size = ctx->word_size;
1830 length = dwarf2_parse_u4(&traverse);
1831 traverse.end_data = traverse.start_data + length;
1833 version = dwarf2_parse_u2(&traverse);
1834 header_len = dwarf2_parse_u4(&traverse);
1835 insn_size = dwarf2_parse_byte(&traverse);
1836 default_stmt = dwarf2_parse_byte(&traverse);
1837 line_base = (signed char)dwarf2_parse_byte(&traverse);
1838 line_range = dwarf2_parse_byte(&traverse);
1839 opcode_base = dwarf2_parse_byte(&traverse);
1841 opcode_len = traverse.data;
1842 traverse.data += opcode_base - 1;
1844 vector_init(&dirs, sizeof(const char*), 4);
1845 p = vector_add(&dirs, &ctx->pool);
1846 *p = compile_dir ? compile_dir : ".";
1847 while (*traverse.data)
1849 const char* rel = (const char*)traverse.data;
1850 unsigned rellen = strlen(rel);
1851 TRACE("Got include %s\n", rel);
1852 traverse.data += rellen + 1;
1853 p = vector_add(&dirs, &ctx->pool);
1855 if (*rel == '/' || !compile_dir)
1856 *p = rel;
1857 else
1859 /* include directory relative to compile directory */
1860 unsigned baselen = strlen(compile_dir);
1861 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1862 strcpy(tmp, compile_dir);
1863 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1864 strcpy(&tmp[baselen], rel);
1865 *p = tmp;
1869 traverse.data++;
1871 vector_init(&files, sizeof(unsigned), 16);
1872 while (*traverse.data)
1874 unsigned int dir_index, mod_time, length;
1875 const char* name;
1876 const char* dir;
1877 unsigned* psrc;
1879 name = (const char*)traverse.data;
1880 traverse.data += strlen(name) + 1;
1881 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1882 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1883 length = dwarf2_leb128_as_unsigned(&traverse);
1884 dir = *(const char**)vector_at(&dirs, dir_index);
1885 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1886 psrc = vector_add(&files, &ctx->pool);
1887 *psrc = source_new(ctx->module, dir, name);
1889 traverse.data++;
1891 while (traverse.data < traverse.end_data)
1893 unsigned long address = 0;
1894 unsigned file = 1;
1895 unsigned line = 1;
1896 unsigned is_stmt = default_stmt;
1897 BOOL basic_block = FALSE, end_sequence = FALSE;
1898 unsigned opcode, extopcode, i;
1900 while (!end_sequence)
1902 opcode = dwarf2_parse_byte(&traverse);
1903 TRACE("Got opcode %x\n", opcode);
1905 if (opcode >= opcode_base)
1907 unsigned delta = opcode - opcode_base;
1909 address += (delta / line_range) * insn_size;
1910 line += line_base + (delta % line_range);
1911 basic_block = TRUE;
1912 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1914 else
1916 switch (opcode)
1918 case DW_LNS_copy:
1919 basic_block = FALSE;
1920 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1921 break;
1922 case DW_LNS_advance_pc:
1923 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1924 break;
1925 case DW_LNS_advance_line:
1926 line += dwarf2_leb128_as_signed(&traverse);
1927 break;
1928 case DW_LNS_set_file:
1929 file = dwarf2_leb128_as_unsigned(&traverse);
1930 break;
1931 case DW_LNS_set_column:
1932 dwarf2_leb128_as_unsigned(&traverse);
1933 break;
1934 case DW_LNS_negate_stmt:
1935 is_stmt = !is_stmt;
1936 break;
1937 case DW_LNS_set_basic_block:
1938 basic_block = 1;
1939 break;
1940 case DW_LNS_const_add_pc:
1941 address += ((255 - opcode_base) / line_range) * insn_size;
1942 break;
1943 case DW_LNS_fixed_advance_pc:
1944 address += dwarf2_parse_u2(&traverse);
1945 break;
1946 case DW_LNS_extended_op:
1947 dwarf2_leb128_as_unsigned(&traverse);
1948 extopcode = dwarf2_parse_byte(&traverse);
1949 switch (extopcode)
1951 case DW_LNE_end_sequence:
1952 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1953 end_sequence = TRUE;
1954 break;
1955 case DW_LNE_set_address:
1956 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
1957 break;
1958 case DW_LNE_define_file:
1959 FIXME("not handled %s\n", traverse.data);
1960 traverse.data += strlen((const char *)traverse.data) + 1;
1961 dwarf2_leb128_as_unsigned(&traverse);
1962 dwarf2_leb128_as_unsigned(&traverse);
1963 dwarf2_leb128_as_unsigned(&traverse);
1964 break;
1965 default:
1966 FIXME("Unsupported extended opcode %x\n", extopcode);
1967 break;
1969 break;
1970 default:
1971 WARN("Unsupported opcode %x\n", opcode);
1972 for (i = 0; i < opcode_len[opcode]; i++)
1973 dwarf2_leb128_as_unsigned(&traverse);
1974 break;
1979 return TRUE;
1982 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1983 const dwarf2_comp_unit_t* comp_unit,
1984 struct module* module,
1985 const struct elf_thunk_area* thunks,
1986 const unsigned char* comp_unit_cursor,
1987 unsigned long load_offset)
1989 dwarf2_parse_context_t ctx;
1990 dwarf2_traverse_context_t traverse;
1991 dwarf2_traverse_context_t abbrev_ctx;
1992 dwarf2_debug_info_t* di;
1993 BOOL ret = FALSE;
1995 TRACE("Compilation Unit Header found at 0x%x:\n",
1996 comp_unit_cursor - sections[section_debug].address);
1997 TRACE("- length: %lu\n", comp_unit->length);
1998 TRACE("- version: %u\n", comp_unit->version);
1999 TRACE("- abbrev_offset: %lu\n", comp_unit->abbrev_offset);
2000 TRACE("- word_size: %u\n", comp_unit->word_size);
2002 if (comp_unit->version != 2)
2004 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2005 comp_unit->version);
2006 return FALSE;
2009 pool_init(&ctx.pool, 65536);
2010 ctx.sections = sections;
2011 ctx.section = section_debug;
2012 ctx.module = module;
2013 ctx.word_size = comp_unit->word_size;
2014 ctx.thunks = thunks;
2015 ctx.load_offset = load_offset;
2016 ctx.ref_offset = comp_unit_cursor - sections[section_debug].address;
2017 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2018 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2020 traverse.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
2021 traverse.data = traverse.start_data;
2022 traverse.word_size = comp_unit->word_size;
2023 traverse.end_data = comp_unit_cursor + comp_unit->length + sizeof(unsigned);
2025 abbrev_ctx.start_data = sections[section_abbrev].address + comp_unit->abbrev_offset;
2026 abbrev_ctx.data = abbrev_ctx.start_data;
2027 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2028 abbrev_ctx.word_size = comp_unit->word_size;
2029 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2031 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2032 dwarf2_read_one_debug_info(&ctx, &traverse, &di);
2034 if (di->abbrev->tag == DW_TAG_compile_unit)
2036 struct attribute name;
2037 dwarf2_debug_info_t** pdi = NULL;
2038 struct attribute stmt_list, low_pc;
2039 struct attribute comp_dir;
2041 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2042 name.u.string = NULL;
2044 /* get working directory of current compilation unit */
2045 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2046 comp_dir.u.string = NULL;
2048 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2049 low_pc.u.uvalue = 0;
2050 di->symt = &symt_new_compiland(module,
2051 ctx.load_offset + low_pc.u.uvalue,
2052 source_new(module, comp_dir.u.string, name.u.string))->symt;
2054 if (di->abbrev->have_child)
2056 int i;
2057 for (i=0; i<vector_length(&di->children); i++)
2059 pdi = vector_at(&di->children, i);
2060 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2063 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2065 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2066 module->module.LineNumbers = TRUE;
2068 ret = TRUE;
2070 else FIXME("Should have a compilation unit here\n");
2071 pool_destroy(&ctx.pool);
2072 return ret;
2075 static BOOL dwarf2_lookup_loclist(const struct module* module, const BYTE* start,
2076 unsigned long ip,
2077 dwarf2_traverse_context_t* lctx)
2079 DWORD beg, end;
2080 const BYTE* ptr = start;
2081 DWORD len;
2083 while (ptr < module->dwarf2_info->debug_loc.address + module->dwarf2_info->debug_loc.size)
2085 beg = dwarf2_get_u4(ptr); ptr += 4;
2086 end = dwarf2_get_u4(ptr); ptr += 4;
2087 if (!beg && !end) break;
2088 len = dwarf2_get_u2(ptr); ptr += 2;
2090 if (beg <= ip && ip < end)
2092 lctx->data = ptr;
2093 lctx->end_data = ptr + len;
2094 lctx->word_size = 4; /* FIXME word size !!! */
2095 return TRUE;
2097 ptr += len;
2099 WARN("Couldn't find ip in location list\n");
2100 return FALSE;
2103 static enum location_error loc_compute_frame(struct process* pcs,
2104 const struct module* module,
2105 const struct symt_function* func,
2106 DWORD ip, struct location* frame)
2108 struct symt** psym = NULL;
2109 struct location* pframe;
2110 dwarf2_traverse_context_t lctx;
2111 enum location_error err;
2112 int i;
2114 for (i=0; i<vector_length(&func->vchildren); i++)
2116 psym = vector_at(&func->vchildren, i);
2117 if ((*psym)->tag == SymTagCustom)
2119 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2121 /* First, recompute the frame information, if needed */
2122 switch (pframe->kind)
2124 case loc_regrel:
2125 case loc_register:
2126 *frame = *pframe;
2127 break;
2128 case loc_dwarf2_location_list:
2129 WARN("Searching loclist for %s\n", func->hash_elt.name);
2130 if (!dwarf2_lookup_loclist(module,
2131 module->dwarf2_info->debug_loc.address + pframe->offset,
2132 ip, &lctx))
2133 return loc_err_out_of_scope;
2134 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2135 if (frame->kind >= loc_user)
2137 WARN("Couldn't compute runtime frame location\n");
2138 return loc_err_too_complex;
2140 break;
2141 default:
2142 WARN("Unsupported frame kind %d\n", pframe->kind);
2143 return loc_err_internal;
2145 return 0;
2148 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2149 return loc_err_internal;
2152 static void dwarf2_location_compute(struct process* pcs,
2153 const struct module* module,
2154 const struct symt_function* func,
2155 struct location* loc)
2157 struct location frame;
2158 DWORD ip;
2159 int err;
2160 dwarf2_traverse_context_t lctx;
2162 if (!func->container || func->container->tag != SymTagCompiland)
2164 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2165 err = loc_err_internal;
2167 else
2169 /* instruction pointer relative to compiland's start */
2170 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2172 if ((err = loc_compute_frame(pcs, module, func, ip, &frame)) == 0)
2174 switch (loc->kind)
2176 case loc_dwarf2_location_list:
2177 /* Then, if the variable has a location list, find it !! */
2178 if (dwarf2_lookup_loclist(module,
2179 module->dwarf2_info->debug_loc.address + loc->offset,
2180 ip, &lctx))
2181 goto do_compute;
2182 err = loc_err_out_of_scope;
2183 break;
2184 case loc_dwarf2_block:
2185 /* or if we have a copy of an existing block, get ready for it */
2187 unsigned* ptr = (unsigned*)loc->offset;
2189 lctx.data = (const BYTE*)(ptr + 1);
2190 lctx.end_data = lctx.data + *ptr;
2191 lctx.word_size = 4; /* FIXME !! */
2193 do_compute:
2194 /* now get the variable */
2195 err = compute_location(&lctx, loc, pcs->handle, &frame);
2196 break;
2197 case loc_register:
2198 case loc_regrel:
2199 /* nothing to do */
2200 break;
2201 default:
2202 WARN("Unsupported local kind %d\n", loc->kind);
2203 err = loc_err_internal;
2207 if (err < 0)
2209 loc->kind = loc_register;
2210 loc->reg = err;
2214 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2215 const struct elf_thunk_area* thunks,
2216 const unsigned char* debug, unsigned int debug_size,
2217 const unsigned char* abbrev, unsigned int abbrev_size,
2218 const unsigned char* str, unsigned int str_size,
2219 const unsigned char* line, unsigned int line_size,
2220 const unsigned char* loclist, unsigned int loclist_size)
2222 dwarf2_section_t section[section_max];
2223 unsigned char* ptr;
2224 const unsigned char*comp_unit_cursor = debug;
2225 const unsigned char*end_debug = debug + debug_size;
2227 module->loc_compute = dwarf2_location_compute;
2229 section[section_debug].address = debug;
2230 section[section_debug].size = debug_size;
2231 section[section_abbrev].address = abbrev;
2232 section[section_abbrev].size = abbrev_size;
2233 section[section_string].address = str;
2234 section[section_string].size = str_size;
2235 section[section_line].address = line;
2236 section[section_line].size = line_size;
2238 if (loclist_size)
2240 /* initialize the dwarf2 specific info block for this module.
2241 * As we'll need later on the .debug_loc section content, we copy it in
2242 * the module structure for later reuse
2244 module->dwarf2_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*module->dwarf2_info) + loclist_size);
2245 if (!module->dwarf2_info) return FALSE;
2246 ptr = (unsigned char*)(module->dwarf2_info + 1);
2247 memcpy(ptr, loclist, loclist_size);
2248 module->dwarf2_info->debug_loc.address = ptr;
2249 module->dwarf2_info->debug_loc.size = loclist_size;
2252 while (comp_unit_cursor < end_debug)
2254 const dwarf2_comp_unit_stream_t* comp_unit_stream;
2255 dwarf2_comp_unit_t comp_unit;
2257 comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
2258 comp_unit.length = *(const unsigned long*) comp_unit_stream->length;
2259 comp_unit.version = *(const unsigned short*) comp_unit_stream->version;
2260 comp_unit.abbrev_offset = *(const unsigned long*) comp_unit_stream->abbrev_offset;
2261 comp_unit.word_size = *(const unsigned char*) comp_unit_stream->word_size;
2263 dwarf2_parse_compilation_unit(section, &comp_unit, module,
2264 thunks, comp_unit_cursor, load_offset);
2265 comp_unit_cursor += comp_unit.length + sizeof(unsigned);
2267 module->module.SymType = SymDia;
2268 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2269 /* FIXME: we could have a finer grain here */
2270 module->module.GlobalSymbols = TRUE;
2271 module->module.TypeInfo = TRUE;
2272 module->module.SourceIndexed = TRUE;
2273 module->module.Publics = TRUE;
2274 return TRUE;