push 149f0a5527ac85057a8ef03858d34d91c36f97e8
[wine/hacks.git] / dlls / dbghelp / dwarf.c
blob60062028a8172f3a3f8897ce53415fb9dc5e3a6f
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"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
59 /* FIXME:
60 * - Functions:
61 * o unspecified parameters
62 * o inlined functions
63 * o Debug{Start|End}Point
64 * o CFA
65 * - Udt
66 * o proper types loading (nesting)
69 #if 0
70 static void dump(const void* ptr, unsigned len)
72 int i, j;
73 BYTE msg[128];
74 static const char hexof[] = "0123456789abcdef";
75 const BYTE* x = ptr;
77 for (i = 0; i < len; i += 16)
79 sprintf(msg, "%08x: ", i);
80 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
81 for (j = 0; j < min(16, len - i); j++)
83 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
84 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
85 msg[10 + 3 * j + 2] = ' ';
86 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
87 x[i + j] : '.';
89 msg[10 + 3 * 16] = ' ';
90 msg[10 + 3 * 16 + 1 + 16] = '\0';
91 TRACE("%s\n", msg);
94 #endif
96 /**
98 * Main Specs:
99 * http://www.eagercon.com/dwarf/dwarf3std.htm
100 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
102 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
104 * example of projects who do dwarf2 parsing:
105 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
106 * http://elis.ugent.be/diota/log/ltrace_elf.c
108 #include "dwarf.h"
111 * Parsers
114 typedef struct dwarf2_abbrev_entry_attr_s
116 unsigned long attribute;
117 unsigned long form;
118 struct dwarf2_abbrev_entry_attr_s* next;
119 } dwarf2_abbrev_entry_attr_t;
121 typedef struct dwarf2_abbrev_entry_s
123 unsigned long entry_code;
124 unsigned long tag;
125 unsigned char have_child;
126 unsigned num_attr;
127 dwarf2_abbrev_entry_attr_t* attrs;
128 } dwarf2_abbrev_entry_t;
130 struct dwarf2_block
132 unsigned size;
133 const unsigned char* ptr;
136 struct attribute
138 unsigned long form;
139 union
141 unsigned long uvalue;
142 long svalue;
143 const char* string;
144 struct dwarf2_block block;
145 } u;
148 typedef struct dwarf2_debug_info_s
150 const dwarf2_abbrev_entry_t*abbrev;
151 struct symt* symt;
152 const unsigned char** data;
153 struct vector children;
154 } dwarf2_debug_info_t;
156 typedef struct dwarf2_section_s
158 const unsigned char* address;
159 unsigned size;
160 } dwarf2_section_t;
162 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
164 typedef struct dwarf2_traverse_context_s
166 const unsigned char* data;
167 const unsigned char* start_data;
168 const unsigned char* end_data;
169 unsigned char word_size;
170 } dwarf2_traverse_context_t;
172 /* symt_cache indexes */
173 #define sc_void 0
174 #define sc_int1 1
175 #define sc_int2 2
176 #define sc_int4 3
177 #define sc_num 4
179 typedef struct dwarf2_parse_context_s
181 const dwarf2_section_t* sections;
182 unsigned section;
183 struct pool pool;
184 struct module* module;
185 const struct elf_thunk_area*thunks;
186 struct sparse_array abbrev_table;
187 struct sparse_array debug_info_table;
188 unsigned long load_offset;
189 unsigned long ref_offset;
190 unsigned char word_size;
191 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
192 } dwarf2_parse_context_t;
194 /* stored in the dbghelp's module internal structure for later reuse */
195 struct dwarf2_module_info_s
197 dwarf2_section_t debug_loc;
200 #define loc_dwarf2_location_list (loc_user + 0)
201 #define loc_dwarf2_block (loc_user + 1)
203 /* forward declarations */
204 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
206 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
208 return *ptr;
211 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
213 unsigned char uvalue = dwarf2_get_byte(ctx->data);
214 ctx->data += 1;
215 return uvalue;
218 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
220 return *(const unsigned short*)ptr;
223 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
225 unsigned short uvalue = dwarf2_get_u2(ctx->data);
226 ctx->data += 2;
227 return uvalue;
230 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
232 return *(const unsigned long*)ptr;
235 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
237 unsigned long uvalue = dwarf2_get_u4(ctx->data);
238 ctx->data += 4;
239 return uvalue;
242 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
244 unsigned long ret = 0;
245 unsigned char byte;
246 unsigned shift = 0;
250 byte = dwarf2_get_byte(ptr++);
251 ret |= (byte & 0x7f) << shift;
252 shift += 7;
253 } while (byte & 0x80);
255 if (end) *end = ptr;
256 return ret;
259 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
261 unsigned long ret;
263 assert(ctx);
265 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
267 return ret;
270 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
272 long ret = 0;
273 unsigned char byte;
274 unsigned shift = 0;
275 const unsigned size = sizeof(int) * 8;
279 byte = dwarf2_get_byte(ptr++);
280 ret |= (byte & 0x7f) << shift;
281 shift += 7;
282 } while (byte & 0x80);
283 if (end) *end = ptr;
285 /* as spec: sign bit of byte is 2nd high order bit (80x40)
286 * -> 0x80 is used as flag.
288 if ((shift < size) && (byte & 0x40))
290 ret |= - (1 << shift);
292 return ret;
295 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
297 long ret = 0;
299 assert(ctx);
301 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
302 return ret;
305 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
307 unsigned ret;
308 for (ret = 0; ctx->data[ret] & 0x80; ret++);
309 return ret + 1;
312 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
314 unsigned long ret;
316 switch (word_size)
318 case 4:
319 ret = dwarf2_get_u4(ptr);
320 break;
321 default:
322 FIXME("Unsupported Word Size %u\n", word_size);
323 ret = 0;
325 return ret;
328 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
330 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
331 ctx->data += ctx->word_size;
332 return ret;
335 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
337 return wine_dbg_sprintf("ctx(%p)", ctx->data);
340 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
342 return wine_dbg_sprintf("ctx(%p,%s)",
343 ctx, debugstr_w(ctx->module->module.ModuleName));
346 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
348 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
349 di->abbrev, di->symt);
352 static dwarf2_abbrev_entry_t*
353 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
354 unsigned long entry_code)
356 assert( NULL != abbrev_table );
357 return sparse_array_find(abbrev_table, entry_code);
360 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
361 struct sparse_array* abbrev_table,
362 struct pool* pool)
364 unsigned long entry_code;
365 dwarf2_abbrev_entry_t* abbrev_entry;
366 dwarf2_abbrev_entry_attr_t* new = NULL;
367 dwarf2_abbrev_entry_attr_t* last = NULL;
368 unsigned long attribute;
369 unsigned long form;
371 assert( NULL != abbrev_ctx );
373 TRACE("%s, end at %p\n",
374 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
376 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
377 while (abbrev_ctx->data < abbrev_ctx->end_data)
379 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
380 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
381 TRACE("found entry_code %lu\n", entry_code);
382 if (!entry_code)
384 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
385 break;
387 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
388 assert( NULL != abbrev_entry );
390 abbrev_entry->entry_code = entry_code;
391 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
392 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
393 abbrev_entry->attrs = NULL;
394 abbrev_entry->num_attr = 0;
396 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
397 abbrev_table, sparse_array_length(abbrev_table),
398 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
400 last = NULL;
401 while (1)
403 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
404 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
405 if (!attribute) break;
407 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
408 assert(new);
410 new->attribute = attribute;
411 new->form = form;
412 new->next = NULL;
413 if (abbrev_entry->attrs) last->next = new;
414 else abbrev_entry->attrs = new;
415 last = new;
416 abbrev_entry->num_attr++;
419 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
422 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
423 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
425 unsigned step;
427 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
429 switch (abbrev_attr->form)
431 case DW_FORM_ref_addr:
432 case DW_FORM_addr: step = ctx->word_size; break;
433 case DW_FORM_flag:
434 case DW_FORM_data1:
435 case DW_FORM_ref1: step = 1; break;
436 case DW_FORM_data2:
437 case DW_FORM_ref2: step = 2; break;
438 case DW_FORM_data4:
439 case DW_FORM_ref4:
440 case DW_FORM_strp: step = 4; break;
441 case DW_FORM_data8:
442 case DW_FORM_ref8: step = 8; break;
443 case DW_FORM_sdata:
444 case DW_FORM_ref_udata:
445 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
446 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
447 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
448 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
449 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
450 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
451 default:
452 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
453 return;
455 ctx->data += step;
458 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
459 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
460 const unsigned char* data,
461 struct attribute* attr)
463 attr->form = abbrev_attr->form;
464 switch (attr->form)
466 case DW_FORM_ref_addr:
467 case DW_FORM_addr:
468 attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
469 TRACE("addr<0x%lx>\n", attr->u.uvalue);
470 break;
472 case DW_FORM_flag:
473 attr->u.uvalue = dwarf2_get_byte(data);
474 TRACE("flag<0x%lx>\n", attr->u.uvalue);
475 break;
477 case DW_FORM_data1:
478 attr->u.uvalue = dwarf2_get_byte(data);
479 TRACE("data1<%lu>\n", attr->u.uvalue);
480 break;
482 case DW_FORM_data2:
483 attr->u.uvalue = dwarf2_get_u2(data);
484 TRACE("data2<%lu>\n", attr->u.uvalue);
485 break;
487 case DW_FORM_data4:
488 attr->u.uvalue = dwarf2_get_u4(data);
489 TRACE("data4<%lu>\n", attr->u.uvalue);
490 break;
492 case DW_FORM_data8:
493 attr->u.block.size = 8;
494 attr->u.block.ptr = data;
495 data += 8;
496 break;
498 case DW_FORM_ref1:
499 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
500 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
501 break;
503 case DW_FORM_ref2:
504 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
505 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
506 break;
508 case DW_FORM_ref4:
509 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
510 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
511 break;
513 case DW_FORM_ref8:
514 FIXME("Unhandled 64 bit support\n");
515 break;
517 case DW_FORM_sdata:
518 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
519 break;
521 case DW_FORM_ref_udata:
522 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
523 break;
525 case DW_FORM_udata:
526 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
527 break;
529 case DW_FORM_string:
530 attr->u.string = (const char *)data;
531 TRACE("string<%s>\n", attr->u.string);
532 break;
534 case DW_FORM_strp:
536 unsigned long offset = dwarf2_get_u4(data);
537 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
539 TRACE("strp<%s>\n", attr->u.string);
540 break;
542 case DW_FORM_block:
543 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
544 break;
546 case DW_FORM_block1:
547 attr->u.block.size = dwarf2_get_byte(data);
548 attr->u.block.ptr = data + 1;
549 break;
551 case DW_FORM_block2:
552 attr->u.block.size = dwarf2_get_u2(data);
553 attr->u.block.ptr = data + 2;
554 break;
556 case DW_FORM_block4:
557 attr->u.block.size = dwarf2_get_u4(data);
558 attr->u.block.ptr = data + 4;
559 break;
561 default:
562 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
563 break;
567 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
568 const dwarf2_debug_info_t* di,
569 unsigned at, struct attribute* attr)
571 unsigned i, ai = 0;
572 dwarf2_abbrev_entry_attr_t* abbrev_attr;
573 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
575 while (di)
577 abstract_abbrev_attr = NULL;
578 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
580 if (abbrev_attr->attribute == at)
582 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
583 return TRUE;
585 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
586 at != DW_AT_sibling)
588 abstract_abbrev_attr = abbrev_attr;
589 ai = i;
592 /* do we have an abstract origin debug entry to look into ? */
593 if (!abstract_abbrev_attr) break;
594 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
595 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
596 FIXME("Should have found the debug info entry\n");
598 return FALSE;
601 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
602 struct symt_compiland*);
604 #define Wine_DW_no_register 0x7FFFFFFF
606 static unsigned dwarf2_map_register(int regno)
608 unsigned reg;
610 switch (regno)
612 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
613 case 0: reg = CV_REG_EAX; break;
614 case 1: reg = CV_REG_ECX; break;
615 case 2: reg = CV_REG_EDX; break;
616 case 3: reg = CV_REG_EBX; break;
617 case 4: reg = CV_REG_ESP; break;
618 case 5: reg = CV_REG_EBP; break;
619 case 6: reg = CV_REG_ESI; break;
620 case 7: reg = CV_REG_EDI; break;
621 case 8: reg = CV_REG_EIP; break;
622 case 9: reg = CV_REG_EFLAGS; break;
623 case 10: reg = CV_REG_CS; break;
624 case 11: reg = CV_REG_SS; break;
625 case 12: reg = CV_REG_DS; break;
626 case 13: reg = CV_REG_ES; break;
627 case 14: reg = CV_REG_FS; break;
628 case 15: reg = CV_REG_GS; break;
629 case 16: case 17: case 18: case 19:
630 case 20: case 21: case 22: case 23:
631 reg = CV_REG_ST0 + regno - 16; break;
632 case 24: reg = CV_REG_CTRL; break;
633 case 25: reg = CV_REG_STAT; break;
634 case 26: reg = CV_REG_TAG; break;
636 reg: fiseg 27
637 reg: fioff 28
638 reg: foseg 29
639 reg: fooff 30
640 reg: fop 31
642 case 32: case 33: case 34: case 35:
643 case 36: case 37: case 38: case 39:
644 reg = CV_REG_XMM0 + regno - 32; break;
645 case 40: reg = CV_REG_MXCSR; break;
646 default:
647 FIXME("Don't know how to map register %d\n", regno);
648 return 0;
650 return reg;
653 static enum location_error
654 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
655 HANDLE hproc, const struct location* frame)
657 unsigned long stack[64];
658 unsigned stk;
659 unsigned char op;
660 BOOL piece_found = FALSE;
662 stack[stk = 0] = 0;
664 loc->kind = loc_absolute;
665 loc->reg = Wine_DW_no_register;
667 while (ctx->data < ctx->end_data)
669 op = dwarf2_parse_byte(ctx);
670 switch (op)
672 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
673 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
674 case DW_OP_const1s: stack[++stk] = (long)(signed char)dwarf2_parse_byte(ctx); break;
675 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
676 case DW_OP_const2s: stack[++stk] = (long)(short)dwarf2_parse_u2(ctx); break;
677 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
678 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
679 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
680 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
681 case DW_OP_plus_uconst:
682 stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
683 case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
684 case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
685 case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
686 case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
687 case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
688 case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
689 case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
690 case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
691 /* dbghelp APIs don't know how to cope with this anyway
692 * (for example 'long long' stored in two registers)
693 * FIXME: We should tell winedbg how to deal with it (sigh)
695 if (!piece_found)
697 if (loc->reg != Wine_DW_no_register)
698 FIXME("Only supporting one reg (%d -> %d)\n",
699 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
700 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
702 loc->kind = loc_register;
703 break;
704 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
705 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
706 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
707 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
708 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
709 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
710 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
711 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:
712 /* dbghelp APIs don't know how to cope with this anyway
713 * (for example 'long long' stored in two registers)
714 * FIXME: We should tell winedbg how to deal with it (sigh)
716 if (!piece_found)
718 if (loc->reg != Wine_DW_no_register)
719 FIXME("Only supporting one reg (%d -> %d)\n",
720 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
721 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
723 stack[++stk] = dwarf2_leb128_as_signed(ctx);
724 loc->kind = loc_regrel;
725 break;
726 case DW_OP_fbreg:
727 if (loc->reg != Wine_DW_no_register)
728 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
729 if (frame && frame->kind == loc_register)
731 loc->kind = loc_regrel;
732 loc->reg = frame->reg;
733 stack[++stk] = dwarf2_leb128_as_signed(ctx);
735 else if (frame && frame->kind == loc_regrel)
737 loc->kind = loc_regrel;
738 loc->reg = frame->reg;
739 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
741 else
743 /* FIXME: this could be later optimized by not recomputing
744 * this very location expression
746 loc->kind = loc_dwarf2_block;
747 stack[++stk] = dwarf2_leb128_as_signed(ctx);
749 break;
750 case DW_OP_piece:
752 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
753 WARN("Not handling OP_piece (size=%d)\n", sz);
754 piece_found = TRUE;
756 break;
757 case DW_OP_deref:
758 if (!stk)
760 FIXME("Unexpected empty stack\n");
761 return loc_err_internal;
763 if (loc->reg != Wine_DW_no_register)
765 WARN("Too complex expression for deref\n");
766 return loc_err_too_complex;
768 if (hproc)
770 DWORD addr = stack[stk--];
771 DWORD deref;
773 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
775 WARN("Couldn't read memory at %x\n", addr);
776 return loc_err_cant_read;
778 stack[++stk] = deref;
780 else
782 loc->kind = loc_dwarf2_block;
784 break;
785 default:
786 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
787 FIXME("Unhandled attr op: %x\n", op);
788 /* FIXME else unhandled extension */
789 return loc_err_internal;
792 loc->offset = stack[stk];
793 return 0;
796 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
797 const dwarf2_debug_info_t* di,
798 unsigned long dw,
799 struct location* loc,
800 const struct location* frame)
802 struct attribute xloc;
804 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
806 switch (xloc.form)
808 case DW_FORM_data1: case DW_FORM_data2:
809 case DW_FORM_udata: case DW_FORM_sdata:
810 loc->kind = loc_absolute;
811 loc->reg = 0;
812 loc->offset = xloc.u.uvalue;
813 return TRUE;
814 case DW_FORM_data4: case DW_FORM_data8:
815 loc->kind = loc_dwarf2_location_list;
816 loc->reg = Wine_DW_no_register;
817 loc->offset = xloc.u.uvalue;
818 return TRUE;
821 /* assume we have a block form */
823 if (xloc.u.block.size)
825 dwarf2_traverse_context_t lctx;
826 enum location_error err;
828 lctx.data = xloc.u.block.ptr;
829 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
830 lctx.word_size = ctx->word_size;
832 err = compute_location(&lctx, loc, NULL, frame);
833 if (err < 0)
835 loc->kind = loc_error;
836 loc->reg = err;
838 else if (loc->kind == loc_dwarf2_block)
840 unsigned* ptr = pool_alloc(&ctx->module->pool,
841 sizeof(unsigned) + xloc.u.block.size);
842 *ptr = xloc.u.block.size;
843 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
844 loc->offset = (unsigned long)ptr;
847 return TRUE;
850 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
851 const dwarf2_debug_info_t* di)
853 struct attribute attr;
855 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
857 dwarf2_debug_info_t* type;
859 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
860 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
861 if (!type->symt)
863 /* load the debug info entity */
864 dwarf2_load_one_entry(ctx, type, NULL);
866 return type->symt;
868 return NULL;
871 /******************************************************************
872 * dwarf2_read_one_debug_info
874 * Loads into memory one debug info entry, and recursively its children (if any)
876 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
877 dwarf2_traverse_context_t* traverse,
878 dwarf2_debug_info_t** pdi)
880 const dwarf2_abbrev_entry_t*abbrev;
881 unsigned long entry_code;
882 unsigned long offset;
883 dwarf2_debug_info_t* di;
884 dwarf2_debug_info_t* child;
885 dwarf2_debug_info_t** where;
886 dwarf2_abbrev_entry_attr_t* attr;
887 unsigned i;
888 struct attribute sibling;
890 offset = traverse->data - ctx->sections[ctx->section].address;
891 entry_code = dwarf2_leb128_as_unsigned(traverse);
892 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
893 if (!entry_code)
895 *pdi = NULL;
896 return TRUE;
898 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
899 if (!abbrev)
901 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
902 return FALSE;
904 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
905 if (!di) return FALSE;
906 di->abbrev = abbrev;
907 di->symt = NULL;
909 if (abbrev->num_attr)
911 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
912 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
914 di->data[i] = traverse->data;
915 dwarf2_swallow_attribute(traverse, attr);
918 else di->data = NULL;
919 if (abbrev->have_child)
921 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
922 while (traverse->data < traverse->end_data)
924 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
925 if (!child) break;
926 where = vector_add(&di->children, &ctx->pool);
927 if (!where) return FALSE;
928 *where = child;
931 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
932 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
934 WARN("setting cursor for %s to next sibling <0x%lx>\n",
935 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
936 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
938 *pdi = di;
939 return TRUE;
942 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
943 dwarf2_debug_info_t* di)
945 struct attribute name;
946 struct attribute size;
947 struct attribute encoding;
948 enum BasicType bt;
949 int cache_idx = -1;
950 if (di->symt) return di->symt;
952 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
954 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
955 name.u.string = NULL;
956 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
957 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
959 switch (encoding.u.uvalue)
961 case DW_ATE_void: bt = btVoid; break;
962 case DW_ATE_address: bt = btULong; break;
963 case DW_ATE_boolean: bt = btBool; break;
964 case DW_ATE_complex_float: bt = btComplex; break;
965 case DW_ATE_float: bt = btFloat; break;
966 case DW_ATE_signed: bt = btInt; break;
967 case DW_ATE_unsigned: bt = btUInt; break;
968 case DW_ATE_signed_char: bt = btChar; break;
969 case DW_ATE_unsigned_char: bt = btChar; break;
970 default: bt = btNoType; break;
972 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
973 switch (bt)
975 case btVoid:
976 assert(size.u.uvalue == 0);
977 cache_idx = sc_void;
978 break;
979 case btInt:
980 switch (size.u.uvalue)
982 case 1: cache_idx = sc_int1; break;
983 case 2: cache_idx = sc_int2; break;
984 case 4: cache_idx = sc_int4; break;
986 break;
987 default: break;
989 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
990 ctx->symt_cache[cache_idx] = di->symt;
992 if (di->abbrev->have_child) FIXME("Unsupported children\n");
993 return di->symt;
996 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
997 dwarf2_debug_info_t* di)
999 struct symt* ref_type;
1000 struct attribute name;
1002 if (di->symt) return di->symt;
1004 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1006 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1007 ref_type = dwarf2_lookup_type(ctx, di);
1009 if (name.u.string)
1010 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1011 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1012 return di->symt;
1015 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1016 dwarf2_debug_info_t* di)
1018 struct symt* ref_type;
1019 struct attribute size;
1021 if (di->symt) return di->symt;
1023 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1025 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1026 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1028 ref_type = ctx->symt_cache[sc_void];
1029 assert(ref_type);
1031 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1032 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1033 return di->symt;
1036 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1037 dwarf2_debug_info_t* di)
1039 struct symt* ref_type;
1040 struct symt* idx_type = NULL;
1041 struct attribute min, max, cnt;
1042 dwarf2_debug_info_t* child;
1043 unsigned int i;
1045 if (di->symt) return di->symt;
1047 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1049 if (!di->abbrev->have_child)
1051 FIXME("array without range information\n");
1052 return NULL;
1054 ref_type = dwarf2_lookup_type(ctx, di);
1056 for (i=0; i<vector_length(&di->children); i++)
1058 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1059 switch (child->abbrev->tag)
1061 case DW_TAG_subrange_type:
1062 idx_type = dwarf2_lookup_type(ctx, child);
1063 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1064 min.u.uvalue = 0;
1065 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1066 max.u.uvalue = 0;
1067 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1068 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1069 break;
1070 default:
1071 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1072 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1073 break;
1076 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1077 return di->symt;
1080 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1081 dwarf2_debug_info_t* di)
1083 struct symt* ref_type;
1085 if (di->symt) return di->symt;
1087 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1089 ref_type = dwarf2_lookup_type(ctx, di);
1090 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1091 di->symt = ref_type;
1093 return ref_type;
1096 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1097 dwarf2_debug_info_t* di)
1099 struct symt* ref_type;
1101 if (di->symt) return di->symt;
1103 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1105 ref_type = dwarf2_lookup_type(ctx, di);
1106 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1107 di->symt = ref_type;
1109 return ref_type;
1112 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1113 dwarf2_debug_info_t* di)
1115 struct symt* ref_type = NULL;
1117 if (di->symt) return di->symt;
1119 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1121 ref_type = dwarf2_lookup_type(ctx, di);
1122 /* FIXME: for now, we hard-wire C++ references to pointers */
1123 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1125 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1127 return di->symt;
1130 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1131 const dwarf2_debug_info_t* di,
1132 struct symt_udt* parent)
1134 struct symt* elt_type;
1135 struct attribute name;
1136 struct attribute bit_size;
1137 struct attribute bit_offset;
1138 struct location loc;
1140 assert(parent);
1142 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1144 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1145 elt_type = dwarf2_lookup_type(ctx, di);
1146 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1148 if (loc.kind != loc_absolute)
1150 FIXME("Found register, while not expecting it\n");
1151 loc.offset = 0;
1153 else
1154 TRACE("found member_location at %s -> %lu\n",
1155 dwarf2_debug_ctx(ctx), loc.offset);
1157 else
1158 loc.offset = 0;
1159 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1160 bit_size.u.uvalue = 0;
1161 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1163 /* FIXME: we should only do this when implementation is LSB (which is
1164 * the case on i386 processors)
1166 struct attribute nbytes;
1167 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1169 DWORD64 size;
1170 nbytes.u.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
1172 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1174 else bit_offset.u.uvalue = 0;
1175 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1176 (loc.offset << 3) + bit_offset.u.uvalue,
1177 bit_size.u.uvalue);
1179 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1182 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1183 dwarf2_debug_info_t* di,
1184 enum UdtKind udt)
1186 struct attribute name;
1187 struct attribute size;
1189 if (di->symt) return di->symt;
1191 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1193 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1194 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1196 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1198 if (di->abbrev->have_child) /** any interest to not have child ? */
1200 dwarf2_debug_info_t* child;
1201 unsigned int i;
1203 for (i=0; i<vector_length(&di->children); i++)
1205 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1207 switch (child->abbrev->tag)
1209 case DW_TAG_member:
1210 /* FIXME: should I follow the sibling stuff ?? */
1211 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1212 break;
1213 case DW_TAG_enumeration_type:
1214 dwarf2_parse_enumeration_type(ctx, child);
1215 break;
1216 case DW_TAG_structure_type:
1217 case DW_TAG_class_type:
1218 case DW_TAG_union_type:
1219 case DW_TAG_typedef:
1220 /* FIXME: we need to handle nested udt definitions */
1221 break;
1222 default:
1223 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1224 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1225 break;
1230 return di->symt;
1233 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1234 const dwarf2_debug_info_t* di,
1235 struct symt_enum* parent)
1237 struct attribute name;
1238 struct attribute value;
1240 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1242 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1243 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1244 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1246 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1249 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1250 dwarf2_debug_info_t* di)
1252 struct attribute name;
1253 struct attribute size;
1254 struct symt_basic* basetype;
1256 if (di->symt) return di->symt;
1258 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1260 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1261 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1263 switch (size.u.uvalue) /* FIXME: that's wrong */
1265 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1266 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1267 default:
1268 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1271 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1273 if (di->abbrev->have_child) /* any interest to not have child ? */
1275 dwarf2_debug_info_t* child;
1276 unsigned int i;
1278 /* FIXME: should we use the sibling stuff ?? */
1279 for (i=0; i<vector_length(&di->children); i++)
1281 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1283 switch (child->abbrev->tag)
1285 case DW_TAG_enumerator:
1286 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1287 break;
1288 default:
1289 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1290 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1294 return di->symt;
1297 /* structure used to pass information around when parsing a subprogram */
1298 typedef struct dwarf2_subprogram_s
1300 dwarf2_parse_context_t* ctx;
1301 struct symt_compiland* compiland;
1302 struct symt_function* func;
1303 BOOL non_computed_variable;
1304 struct location frame;
1305 } dwarf2_subprogram_t;
1307 /******************************************************************
1308 * dwarf2_parse_variable
1310 * Parses any variable (parameter, local/global variable)
1312 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1313 struct symt_block* block,
1314 dwarf2_debug_info_t* di)
1316 struct symt* param_type;
1317 struct attribute name, value;
1318 struct location loc;
1319 BOOL is_pmt;
1321 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1323 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1324 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1326 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1327 /* cannot do much without the name, the functions below won't like it. */
1328 return;
1330 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1331 &loc, &subpgm->frame))
1333 struct attribute ext;
1335 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1336 name.u.string, loc.kind, loc.offset, loc.reg,
1337 dwarf2_debug_ctx(subpgm->ctx));
1339 switch (loc.kind)
1341 case loc_error:
1342 break;
1343 case loc_absolute:
1344 /* it's a global variable */
1345 /* FIXME: we don't handle its scope yet */
1346 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1347 ext.u.uvalue = 0;
1348 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1349 name.u.string, !ext.u.uvalue,
1350 subpgm->ctx->load_offset + loc.offset,
1351 0, param_type);
1352 break;
1353 default:
1354 subpgm->non_computed_variable = TRUE;
1355 /* fall through */
1356 case loc_register:
1357 case loc_regrel:
1358 /* either a pmt/variable relative to frame pointer or
1359 * pmt/variable in a register
1361 assert(subpgm->func);
1362 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1363 is_pmt ? DataIsParam : DataIsLocal,
1364 &loc, block, param_type, name.u.string);
1365 break;
1368 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1370 VARIANT v;
1371 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1372 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1373 switch (value.form)
1375 case DW_FORM_data1:
1376 case DW_FORM_data2:
1377 case DW_FORM_data4:
1378 case DW_FORM_udata:
1379 case DW_FORM_addr:
1380 v.n1.n2.vt = VT_UI4;
1381 v.n1.n2.n3.lVal = value.u.uvalue;
1382 break;
1384 case DW_FORM_sdata:
1385 v.n1.n2.vt = VT_I4;
1386 v.n1.n2.n3.lVal = value.u.svalue;
1387 break;
1389 case DW_FORM_strp:
1390 case DW_FORM_string:
1391 /* FIXME: native doesn't report const strings from here !!
1392 * however, the value of the string is in the code somewhere
1394 v.n1.n2.vt = VT_I1 | VT_BYREF;
1395 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1396 break;
1398 case DW_FORM_block:
1399 case DW_FORM_block1:
1400 case DW_FORM_block2:
1401 case DW_FORM_block4:
1402 v.n1.n2.vt = VT_I4;
1403 switch (value.u.block.size)
1405 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1406 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1407 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1408 default:
1409 v.n1.n2.vt = VT_I1 | VT_BYREF;
1410 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1411 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1413 break;
1415 case DW_FORM_data8:
1416 v.n1.n2.vt = VT_I1 | VT_BYREF;
1417 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1418 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1419 break;
1421 default:
1422 FIXME("Unsupported form for const value %s (%lx)\n",
1423 name.u.string, value.form);
1424 v.n1.n2.vt = VT_EMPTY;
1426 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1427 name.u.string, param_type, &v)->symt;
1429 if (is_pmt && subpgm->func && subpgm->func->type)
1430 symt_add_function_signature_parameter(subpgm->ctx->module,
1431 (struct symt_function_signature*)subpgm->func->type,
1432 param_type);
1434 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1437 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1438 const dwarf2_debug_info_t* di)
1440 struct attribute name;
1441 struct attribute low_pc;
1442 struct location loc;
1444 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1446 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1447 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1448 name.u.string = NULL;
1450 loc.kind = loc_absolute;
1451 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1452 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1453 &loc, name.u.string);
1456 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1457 struct symt_block* parent_block,
1458 const dwarf2_debug_info_t* di);
1460 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1461 struct symt_block* parent_block,
1462 const dwarf2_debug_info_t* di)
1464 struct symt_block* block;
1465 struct attribute low_pc;
1466 struct attribute high_pc;
1468 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1470 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1471 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1473 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1474 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1475 high_pc.u.uvalue - low_pc.u.uvalue);
1477 if (di->abbrev->have_child) /** any interest to not have child ? */
1479 dwarf2_debug_info_t* child;
1480 unsigned int i;
1482 for (i=0; i<vector_length(&di->children); i++)
1484 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1486 switch (child->abbrev->tag)
1488 case DW_TAG_formal_parameter:
1489 case DW_TAG_variable:
1490 dwarf2_parse_variable(subpgm, block, child);
1491 break;
1492 case DW_TAG_lexical_block:
1493 dwarf2_parse_subprogram_block(subpgm, block, child);
1494 break;
1495 case DW_TAG_inlined_subroutine:
1496 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1497 break;
1498 case DW_TAG_label:
1499 dwarf2_parse_subprogram_label(subpgm, child);
1500 break;
1501 default:
1502 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1503 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1504 dwarf2_debug_di(di));
1508 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1511 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1512 struct symt_block* parent_block,
1513 const dwarf2_debug_info_t* di)
1515 struct symt_block* block;
1516 struct attribute low_pc;
1517 struct attribute high_pc;
1519 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1521 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1522 low_pc.u.uvalue = 0;
1523 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1524 high_pc.u.uvalue = 0;
1526 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1527 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1528 high_pc.u.uvalue - low_pc.u.uvalue);
1530 if (di->abbrev->have_child) /** any interest to not have child ? */
1532 dwarf2_debug_info_t* child;
1533 unsigned int i;
1535 for (i=0; i<vector_length(&di->children); i++)
1537 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1539 switch (child->abbrev->tag)
1541 case DW_TAG_inlined_subroutine:
1542 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1543 break;
1544 case DW_TAG_variable:
1545 dwarf2_parse_variable(subpgm, block, child);
1546 break;
1547 case DW_TAG_lexical_block:
1548 dwarf2_parse_subprogram_block(subpgm, block, child);
1549 break;
1550 case DW_TAG_subprogram:
1551 /* FIXME: likely a declaration (to be checked)
1552 * skip it for now
1554 break;
1555 case DW_TAG_formal_parameter:
1556 /* FIXME: likely elements for exception handling (GCC flavor)
1557 * Skip it for now
1559 break;
1560 case DW_TAG_label:
1561 dwarf2_parse_subprogram_label(subpgm, child);
1562 break;
1563 case DW_TAG_class_type:
1564 case DW_TAG_structure_type:
1565 case DW_TAG_union_type:
1566 case DW_TAG_enumeration_type:
1567 case DW_TAG_typedef:
1568 /* the type referred to will be loaded when we need it, so skip it */
1569 break;
1570 default:
1571 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1572 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1577 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1580 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1581 dwarf2_debug_info_t* di,
1582 struct symt_compiland* compiland)
1584 struct attribute name;
1585 struct attribute low_pc;
1586 struct attribute high_pc;
1587 struct attribute is_decl;
1588 struct attribute inline_flags;
1589 struct symt* ret_type;
1590 struct symt_function_signature* sig_type;
1591 dwarf2_subprogram_t subpgm;
1593 if (di->symt) return di->symt;
1595 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1597 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1599 WARN("No name for function... dropping function\n");
1600 return NULL;
1602 /* if it's an abstract representation of an inline function, there should be
1603 * a concrete object that we'll handle
1605 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1607 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1608 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1609 return NULL;
1612 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1613 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1614 /* As functions (defined as inline assembly) get debug info with dwarf
1615 * (not the case for stabs), we just drop Wine's thunks here...
1616 * Actual thunks will be created in elf_module from the symbol table
1618 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1619 ctx->thunks) >= 0)
1620 return NULL;
1621 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1622 is_decl.u.uvalue = 0;
1624 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1626 ret_type = ctx->symt_cache[sc_void];
1627 assert(ret_type);
1630 /* FIXME: assuming C source code */
1631 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1632 if (!is_decl.u.uvalue)
1634 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1635 ctx->load_offset + low_pc.u.uvalue,
1636 high_pc.u.uvalue - low_pc.u.uvalue,
1637 &sig_type->symt);
1638 di->symt = &subpgm.func->symt;
1640 else subpgm.func = NULL;
1642 subpgm.ctx = ctx;
1643 subpgm.compiland = compiland;
1644 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1645 &subpgm.frame, NULL))
1647 /* on stack !! */
1648 subpgm.frame.kind = loc_regrel;
1649 subpgm.frame.reg = 0;
1650 subpgm.frame.offset = 0;
1652 subpgm.non_computed_variable = FALSE;
1654 if (di->abbrev->have_child) /** any interest to not have child ? */
1656 dwarf2_debug_info_t* child;
1657 unsigned int i;
1659 for (i=0; i<vector_length(&di->children); i++)
1661 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1663 switch (child->abbrev->tag)
1665 case DW_TAG_variable:
1666 case DW_TAG_formal_parameter:
1667 dwarf2_parse_variable(&subpgm, NULL, child);
1668 break;
1669 case DW_TAG_lexical_block:
1670 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1671 break;
1672 case DW_TAG_inlined_subroutine:
1673 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1674 break;
1675 case DW_TAG_subprogram:
1676 /* FIXME: likely a declaration (to be checked)
1677 * skip it for now
1679 break;
1680 case DW_TAG_label:
1681 dwarf2_parse_subprogram_label(&subpgm, child);
1682 break;
1683 case DW_TAG_class_type:
1684 case DW_TAG_structure_type:
1685 case DW_TAG_union_type:
1686 case DW_TAG_enumeration_type:
1687 case DW_TAG_typedef:
1688 /* the type referred to will be loaded when we need it, so skip it */
1689 break;
1690 case DW_TAG_unspecified_parameters:
1691 /* FIXME: no support in dbghelp's internals so far */
1692 break;
1693 default:
1694 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1695 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1700 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1702 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1703 &subpgm.frame, NULL);
1705 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1707 return di->symt;
1710 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1711 dwarf2_debug_info_t* di)
1713 struct symt* ret_type;
1714 struct symt_function_signature* sig_type;
1716 if (di->symt) return di->symt;
1718 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1720 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1722 ret_type = ctx->symt_cache[sc_void];
1723 assert(ret_type);
1726 /* FIXME: assuming C source code */
1727 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1729 if (di->abbrev->have_child) /** any interest to not have child ? */
1731 dwarf2_debug_info_t* child;
1732 unsigned int i;
1734 for (i=0; i<vector_length(&di->children); i++)
1736 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1738 switch (child->abbrev->tag)
1740 case DW_TAG_formal_parameter:
1741 symt_add_function_signature_parameter(ctx->module, sig_type,
1742 dwarf2_lookup_type(ctx, child));
1743 break;
1744 case DW_TAG_unspecified_parameters:
1745 WARN("Unsupported unspecified parameters\n");
1746 break;
1751 return di->symt = &sig_type->symt;
1754 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1755 dwarf2_debug_info_t* di,
1756 struct symt_compiland* compiland)
1758 switch (di->abbrev->tag)
1760 case DW_TAG_typedef:
1761 dwarf2_parse_typedef(ctx, di);
1762 break;
1763 case DW_TAG_base_type:
1764 dwarf2_parse_base_type(ctx, di);
1765 break;
1766 case DW_TAG_pointer_type:
1767 dwarf2_parse_pointer_type(ctx, di);
1768 break;
1769 case DW_TAG_class_type:
1770 dwarf2_parse_udt_type(ctx, di, UdtClass);
1771 break;
1772 case DW_TAG_structure_type:
1773 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1774 break;
1775 case DW_TAG_union_type:
1776 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1777 break;
1778 case DW_TAG_array_type:
1779 dwarf2_parse_array_type(ctx, di);
1780 break;
1781 case DW_TAG_const_type:
1782 dwarf2_parse_const_type(ctx, di);
1783 break;
1784 case DW_TAG_volatile_type:
1785 dwarf2_parse_volatile_type(ctx, di);
1786 break;
1787 case DW_TAG_reference_type:
1788 dwarf2_parse_reference_type(ctx, di);
1789 break;
1790 case DW_TAG_enumeration_type:
1791 dwarf2_parse_enumeration_type(ctx, di);
1792 break;
1793 case DW_TAG_subprogram:
1794 dwarf2_parse_subprogram(ctx, di, compiland);
1795 break;
1796 case DW_TAG_subroutine_type:
1797 dwarf2_parse_subroutine_type(ctx, di);
1798 break;
1799 case DW_TAG_variable:
1801 dwarf2_subprogram_t subpgm;
1803 subpgm.ctx = ctx;
1804 subpgm.compiland = compiland;
1805 subpgm.func = NULL;
1806 subpgm.frame.kind = loc_absolute;
1807 subpgm.frame.offset = 0;
1808 subpgm.frame.reg = Wine_DW_no_register;
1809 dwarf2_parse_variable(&subpgm, NULL, di);
1811 break;
1812 default:
1813 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1814 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1818 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1819 const struct vector* v, unsigned file, unsigned line)
1821 struct symt_function* func;
1822 struct symt_ht* symt;
1823 unsigned* psrc;
1825 if (!file || !(psrc = vector_at(v, file - 1))) return;
1827 TRACE("%s %lx %s %u\n",
1828 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1829 if (!(symt = symt_find_nearest(module, address)) ||
1830 symt->symt.tag != SymTagFunction) return;
1831 func = (struct symt_function*)symt;
1832 symt_add_func_line(module, func, *psrc, line, address - func->address);
1835 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1836 dwarf2_parse_context_t* ctx,
1837 const char* compile_dir,
1838 unsigned long offset)
1840 dwarf2_traverse_context_t traverse;
1841 unsigned long length;
1842 unsigned version, header_len, insn_size, default_stmt;
1843 unsigned line_range, opcode_base;
1844 int line_base;
1845 const unsigned char* opcode_len;
1846 struct vector dirs;
1847 struct vector files;
1848 const char** p;
1850 /* section with line numbers stripped */
1851 if (sections[section_line].address == ELF_NO_MAP)
1852 return FALSE;
1854 traverse.data = sections[section_line].address + offset;
1855 traverse.start_data = traverse.data;
1856 traverse.end_data = traverse.data + 4;
1857 traverse.word_size = ctx->word_size;
1859 length = dwarf2_parse_u4(&traverse);
1860 traverse.end_data = traverse.start_data + length;
1862 version = dwarf2_parse_u2(&traverse);
1863 header_len = dwarf2_parse_u4(&traverse);
1864 insn_size = dwarf2_parse_byte(&traverse);
1865 default_stmt = dwarf2_parse_byte(&traverse);
1866 line_base = (signed char)dwarf2_parse_byte(&traverse);
1867 line_range = dwarf2_parse_byte(&traverse);
1868 opcode_base = dwarf2_parse_byte(&traverse);
1870 opcode_len = traverse.data;
1871 traverse.data += opcode_base - 1;
1873 vector_init(&dirs, sizeof(const char*), 4);
1874 p = vector_add(&dirs, &ctx->pool);
1875 *p = compile_dir ? compile_dir : ".";
1876 while (*traverse.data)
1878 const char* rel = (const char*)traverse.data;
1879 unsigned rellen = strlen(rel);
1880 TRACE("Got include %s\n", rel);
1881 traverse.data += rellen + 1;
1882 p = vector_add(&dirs, &ctx->pool);
1884 if (*rel == '/' || !compile_dir)
1885 *p = rel;
1886 else
1888 /* include directory relative to compile directory */
1889 unsigned baselen = strlen(compile_dir);
1890 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1891 strcpy(tmp, compile_dir);
1892 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1893 strcpy(&tmp[baselen], rel);
1894 *p = tmp;
1898 traverse.data++;
1900 vector_init(&files, sizeof(unsigned), 16);
1901 while (*traverse.data)
1903 unsigned int dir_index, mod_time, length;
1904 const char* name;
1905 const char* dir;
1906 unsigned* psrc;
1908 name = (const char*)traverse.data;
1909 traverse.data += strlen(name) + 1;
1910 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1911 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1912 length = dwarf2_leb128_as_unsigned(&traverse);
1913 dir = *(const char**)vector_at(&dirs, dir_index);
1914 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1915 psrc = vector_add(&files, &ctx->pool);
1916 *psrc = source_new(ctx->module, dir, name);
1918 traverse.data++;
1920 while (traverse.data < traverse.end_data)
1922 unsigned long address = 0;
1923 unsigned file = 1;
1924 unsigned line = 1;
1925 unsigned is_stmt = default_stmt;
1926 BOOL basic_block = FALSE, end_sequence = FALSE;
1927 unsigned opcode, extopcode, i;
1929 while (!end_sequence)
1931 opcode = dwarf2_parse_byte(&traverse);
1932 TRACE("Got opcode %x\n", opcode);
1934 if (opcode >= opcode_base)
1936 unsigned delta = opcode - opcode_base;
1938 address += (delta / line_range) * insn_size;
1939 line += line_base + (delta % line_range);
1940 basic_block = TRUE;
1941 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1943 else
1945 switch (opcode)
1947 case DW_LNS_copy:
1948 basic_block = FALSE;
1949 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1950 break;
1951 case DW_LNS_advance_pc:
1952 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1953 break;
1954 case DW_LNS_advance_line:
1955 line += dwarf2_leb128_as_signed(&traverse);
1956 break;
1957 case DW_LNS_set_file:
1958 file = dwarf2_leb128_as_unsigned(&traverse);
1959 break;
1960 case DW_LNS_set_column:
1961 dwarf2_leb128_as_unsigned(&traverse);
1962 break;
1963 case DW_LNS_negate_stmt:
1964 is_stmt = !is_stmt;
1965 break;
1966 case DW_LNS_set_basic_block:
1967 basic_block = 1;
1968 break;
1969 case DW_LNS_const_add_pc:
1970 address += ((255 - opcode_base) / line_range) * insn_size;
1971 break;
1972 case DW_LNS_fixed_advance_pc:
1973 address += dwarf2_parse_u2(&traverse);
1974 break;
1975 case DW_LNS_extended_op:
1976 dwarf2_leb128_as_unsigned(&traverse);
1977 extopcode = dwarf2_parse_byte(&traverse);
1978 switch (extopcode)
1980 case DW_LNE_end_sequence:
1981 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1982 end_sequence = TRUE;
1983 break;
1984 case DW_LNE_set_address:
1985 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
1986 break;
1987 case DW_LNE_define_file:
1988 FIXME("not handled %s\n", traverse.data);
1989 traverse.data += strlen((const char *)traverse.data) + 1;
1990 dwarf2_leb128_as_unsigned(&traverse);
1991 dwarf2_leb128_as_unsigned(&traverse);
1992 dwarf2_leb128_as_unsigned(&traverse);
1993 break;
1994 default:
1995 FIXME("Unsupported extended opcode %x\n", extopcode);
1996 break;
1998 break;
1999 default:
2000 WARN("Unsupported opcode %x\n", opcode);
2001 for (i = 0; i < opcode_len[opcode]; i++)
2002 dwarf2_leb128_as_unsigned(&traverse);
2003 break;
2008 return TRUE;
2011 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2012 struct module* module,
2013 const struct elf_thunk_area* thunks,
2014 dwarf2_traverse_context_t* mod_ctx,
2015 unsigned long load_offset)
2017 dwarf2_parse_context_t ctx;
2018 dwarf2_traverse_context_t abbrev_ctx;
2019 dwarf2_debug_info_t* di;
2020 dwarf2_traverse_context_t cu_ctx;
2021 const unsigned char* comp_unit_start = mod_ctx->data;
2022 unsigned long cu_length;
2023 unsigned short cu_version;
2024 unsigned long cu_abbrev_offset;
2025 BOOL ret = FALSE;
2027 cu_length = dwarf2_parse_u4(mod_ctx);
2028 cu_ctx.data = cu_ctx.start_data = mod_ctx->data;
2029 cu_ctx.end_data = mod_ctx->data + cu_length;
2030 mod_ctx->data += cu_length;
2031 cu_version = dwarf2_parse_u2(&cu_ctx);
2032 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2033 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2035 TRACE("Compilation Unit Header found at 0x%x:\n",
2036 comp_unit_start - sections[section_debug].address);
2037 TRACE("- length: %lu\n", cu_length);
2038 TRACE("- version: %u\n", cu_version);
2039 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2040 TRACE("- word_size: %u\n", cu_ctx.word_size);
2042 if (cu_version != 2)
2044 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2045 cu_version);
2046 return FALSE;
2049 pool_init(&ctx.pool, 65536);
2050 ctx.sections = sections;
2051 ctx.section = section_debug;
2052 ctx.module = module;
2053 ctx.word_size = cu_ctx.word_size;
2054 ctx.thunks = thunks;
2055 ctx.load_offset = load_offset;
2056 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2057 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2058 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2060 abbrev_ctx.start_data = sections[section_abbrev].address + cu_abbrev_offset;
2061 abbrev_ctx.data = abbrev_ctx.start_data;
2062 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2063 abbrev_ctx.word_size = cu_ctx.word_size;
2064 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2066 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2067 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2069 if (di->abbrev->tag == DW_TAG_compile_unit)
2071 struct attribute name;
2072 dwarf2_debug_info_t** pdi = NULL;
2073 struct attribute stmt_list, low_pc;
2074 struct attribute comp_dir;
2076 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2077 name.u.string = NULL;
2079 /* get working directory of current compilation unit */
2080 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2081 comp_dir.u.string = NULL;
2083 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2084 low_pc.u.uvalue = 0;
2085 di->symt = &symt_new_compiland(module,
2086 ctx.load_offset + low_pc.u.uvalue,
2087 source_new(module, comp_dir.u.string, name.u.string))->symt;
2089 if (di->abbrev->have_child)
2091 unsigned int i;
2092 for (i=0; i<vector_length(&di->children); i++)
2094 pdi = vector_at(&di->children, i);
2095 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2098 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2100 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2101 module->module.LineNumbers = TRUE;
2103 ret = TRUE;
2105 else FIXME("Should have a compilation unit here\n");
2106 pool_destroy(&ctx.pool);
2107 return ret;
2110 static BOOL dwarf2_lookup_loclist(const struct module* module, const BYTE* start,
2111 unsigned long ip,
2112 dwarf2_traverse_context_t* lctx)
2114 DWORD beg, end;
2115 const BYTE* ptr = start;
2116 DWORD len;
2118 while (ptr < module->dwarf2_info->debug_loc.address + module->dwarf2_info->debug_loc.size)
2120 beg = dwarf2_get_u4(ptr); ptr += 4;
2121 end = dwarf2_get_u4(ptr); ptr += 4;
2122 if (!beg && !end) break;
2123 len = dwarf2_get_u2(ptr); ptr += 2;
2125 if (beg <= ip && ip < end)
2127 lctx->data = ptr;
2128 lctx->end_data = ptr + len;
2129 lctx->word_size = 4; /* FIXME word size !!! */
2130 return TRUE;
2132 ptr += len;
2134 WARN("Couldn't find ip in location list\n");
2135 return FALSE;
2138 static enum location_error loc_compute_frame(struct process* pcs,
2139 const struct module* module,
2140 const struct symt_function* func,
2141 DWORD ip, struct location* frame)
2143 struct symt** psym = NULL;
2144 struct location* pframe;
2145 dwarf2_traverse_context_t lctx;
2146 enum location_error err;
2147 unsigned int i;
2149 for (i=0; i<vector_length(&func->vchildren); i++)
2151 psym = vector_at(&func->vchildren, i);
2152 if ((*psym)->tag == SymTagCustom)
2154 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2156 /* First, recompute the frame information, if needed */
2157 switch (pframe->kind)
2159 case loc_regrel:
2160 case loc_register:
2161 *frame = *pframe;
2162 break;
2163 case loc_dwarf2_location_list:
2164 WARN("Searching loclist for %s\n", func->hash_elt.name);
2165 if (!dwarf2_lookup_loclist(module,
2166 module->dwarf2_info->debug_loc.address + pframe->offset,
2167 ip, &lctx))
2168 return loc_err_out_of_scope;
2169 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2170 if (frame->kind >= loc_user)
2172 WARN("Couldn't compute runtime frame location\n");
2173 return loc_err_too_complex;
2175 break;
2176 default:
2177 WARN("Unsupported frame kind %d\n", pframe->kind);
2178 return loc_err_internal;
2180 return 0;
2183 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2184 return loc_err_internal;
2187 static void dwarf2_location_compute(struct process* pcs,
2188 const struct module* module,
2189 const struct symt_function* func,
2190 struct location* loc)
2192 struct location frame;
2193 DWORD ip;
2194 int err;
2195 dwarf2_traverse_context_t lctx;
2197 if (!func->container || func->container->tag != SymTagCompiland)
2199 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2200 err = loc_err_internal;
2202 else
2204 /* instruction pointer relative to compiland's start */
2205 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2207 if ((err = loc_compute_frame(pcs, module, func, ip, &frame)) == 0)
2209 switch (loc->kind)
2211 case loc_dwarf2_location_list:
2212 /* Then, if the variable has a location list, find it !! */
2213 if (dwarf2_lookup_loclist(module,
2214 module->dwarf2_info->debug_loc.address + loc->offset,
2215 ip, &lctx))
2216 goto do_compute;
2217 err = loc_err_out_of_scope;
2218 break;
2219 case loc_dwarf2_block:
2220 /* or if we have a copy of an existing block, get ready for it */
2222 unsigned* ptr = (unsigned*)loc->offset;
2224 lctx.data = (const BYTE*)(ptr + 1);
2225 lctx.end_data = lctx.data + *ptr;
2226 lctx.word_size = 4; /* FIXME !! */
2228 do_compute:
2229 /* now get the variable */
2230 err = compute_location(&lctx, loc, pcs->handle, &frame);
2231 break;
2232 case loc_register:
2233 case loc_regrel:
2234 /* nothing to do */
2235 break;
2236 default:
2237 WARN("Unsupported local kind %d\n", loc->kind);
2238 err = loc_err_internal;
2242 if (err < 0)
2244 loc->kind = loc_register;
2245 loc->reg = err;
2249 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2250 const struct elf_thunk_area* thunks,
2251 const unsigned char* debug, unsigned int debug_size,
2252 const unsigned char* abbrev, unsigned int abbrev_size,
2253 const unsigned char* str, unsigned int str_size,
2254 const unsigned char* line, unsigned int line_size,
2255 const unsigned char* loclist, unsigned int loclist_size)
2257 dwarf2_section_t section[section_max];
2258 unsigned char* ptr;
2259 dwarf2_traverse_context_t mod_ctx;
2261 mod_ctx.start_data = mod_ctx.data = debug;
2262 mod_ctx.end_data = debug + debug_size;
2264 module->loc_compute = dwarf2_location_compute;
2266 section[section_debug].address = debug;
2267 section[section_debug].size = debug_size;
2268 section[section_abbrev].address = abbrev;
2269 section[section_abbrev].size = abbrev_size;
2270 section[section_string].address = str;
2271 section[section_string].size = str_size;
2272 section[section_line].address = line;
2273 section[section_line].size = line_size;
2275 if (loclist_size)
2277 /* initialize the dwarf2 specific info block for this module.
2278 * As we'll need later on the .debug_loc section content, we copy it in
2279 * the module structure for later reuse
2281 module->dwarf2_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*module->dwarf2_info) + loclist_size);
2282 if (!module->dwarf2_info) return FALSE;
2283 ptr = (unsigned char*)(module->dwarf2_info + 1);
2284 memcpy(ptr, loclist, loclist_size);
2285 module->dwarf2_info->debug_loc.address = ptr;
2286 module->dwarf2_info->debug_loc.size = loclist_size;
2289 while (mod_ctx.data < mod_ctx.end_data)
2291 dwarf2_parse_compilation_unit(section, module, thunks, &mod_ctx, load_offset);
2293 module->module.SymType = SymDia;
2294 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2295 /* FIXME: we could have a finer grain here */
2296 module->module.GlobalSymbols = TRUE;
2297 module->module.TypeInfo = TRUE;
2298 module->module.SourceIndexed = TRUE;
2299 module->module.Publics = TRUE;
2300 return TRUE;