push da91f2e13455bbb7c30984ecc6c6cc3528d45c7b
[wine/hacks.git] / dlls / dbghelp / dwarf.c
blob3667c6769cc0bf2afa54957f8f8c4943fe36978a
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 = (const BYTE*)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 FIXME("Unhandled 64bits support\n");
494 break;
496 case DW_FORM_ref1:
497 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
498 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
499 break;
501 case DW_FORM_ref2:
502 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
503 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
504 break;
506 case DW_FORM_ref4:
507 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
508 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
509 break;
511 case DW_FORM_ref8:
512 FIXME("Unhandled 64 bit support\n");
513 break;
515 case DW_FORM_sdata:
516 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
517 break;
519 case DW_FORM_ref_udata:
520 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
521 break;
523 case DW_FORM_udata:
524 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
525 break;
527 case DW_FORM_string:
528 attr->u.string = (const char *)data;
529 TRACE("string<%s>\n", attr->u.string);
530 break;
532 case DW_FORM_strp:
534 unsigned long offset = dwarf2_get_u4(data);
535 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
537 TRACE("strp<%s>\n", attr->u.string);
538 break;
540 case DW_FORM_block:
541 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
542 break;
544 case DW_FORM_block1:
545 attr->u.block.size = dwarf2_get_byte(data);
546 attr->u.block.ptr = data + 1;
547 break;
549 case DW_FORM_block2:
550 attr->u.block.size = dwarf2_get_u2(data);
551 attr->u.block.ptr = data + 2;
552 break;
554 case DW_FORM_block4:
555 attr->u.block.size = dwarf2_get_u4(data);
556 attr->u.block.ptr = data + 4;
557 break;
559 default:
560 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
561 break;
565 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
566 const dwarf2_debug_info_t* di,
567 unsigned at, struct attribute* attr)
569 unsigned i, ai = 0;
570 dwarf2_abbrev_entry_attr_t* abbrev_attr;
571 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
573 while (di)
575 abstract_abbrev_attr = NULL;
576 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
578 if (abbrev_attr->attribute == at)
580 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
581 return TRUE;
583 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
584 at != DW_AT_sibling)
586 abstract_abbrev_attr = abbrev_attr;
587 ai = i;
590 /* do we have an abstract origin debug entry to look into ? */
591 if (!abstract_abbrev_attr) break;
592 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
593 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
594 FIXME("Should have found the debug info entry\n");
596 return FALSE;
599 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
600 struct symt_compiland*);
602 #define Wine_DW_no_register 0x7FFFFFFF
604 static unsigned dwarf2_map_register(int regno)
606 unsigned reg;
608 switch (regno)
610 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
611 case 0: reg = CV_REG_EAX; break;
612 case 1: reg = CV_REG_ECX; break;
613 case 2: reg = CV_REG_EDX; break;
614 case 3: reg = CV_REG_EBX; break;
615 case 4: reg = CV_REG_ESP; break;
616 case 5: reg = CV_REG_EBP; break;
617 case 6: reg = CV_REG_ESI; break;
618 case 7: reg = CV_REG_EDI; break;
619 case 8: reg = CV_REG_EIP; break;
620 case 9: reg = CV_REG_EFLAGS; break;
621 case 10: reg = CV_REG_CS; break;
622 case 11: reg = CV_REG_SS; break;
623 case 12: reg = CV_REG_DS; break;
624 case 13: reg = CV_REG_ES; break;
625 case 14: reg = CV_REG_FS; break;
626 case 15: reg = CV_REG_GS; break;
627 case 16: case 17: case 18: case 19:
628 case 20: case 21: case 22: case 23:
629 reg = CV_REG_ST0 + regno - 16; break;
630 case 24: reg = CV_REG_CTRL; break;
631 case 25: reg = CV_REG_STAT; break;
632 case 26: reg = CV_REG_TAG; break;
634 reg: fiseg 27
635 reg: fioff 28
636 reg: foseg 29
637 reg: fooff 30
638 reg: fop 31
640 case 32: case 33: case 34: case 35:
641 case 36: case 37: case 38: case 39:
642 reg = CV_REG_XMM0 + regno - 32; break;
643 case 40: reg = CV_REG_MXCSR; break;
644 default:
645 FIXME("Don't know how to map register %d\n", regno);
646 return 0;
648 return reg;
651 static enum location_error
652 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
653 HANDLE hproc, const struct location* frame)
655 unsigned long stack[64];
656 unsigned stk;
657 unsigned char op;
658 BOOL piece_found = FALSE;
660 stack[stk = 0] = 0;
662 loc->kind = loc_absolute;
663 loc->reg = Wine_DW_no_register;
665 while (ctx->data < ctx->end_data)
667 op = dwarf2_parse_byte(ctx);
668 switch (op)
670 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
671 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
672 case DW_OP_const1s: stack[++stk] = (long)(signed char)dwarf2_parse_byte(ctx); break;
673 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
674 case DW_OP_const2s: stack[++stk] = (long)(short)dwarf2_parse_u2(ctx); break;
675 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
676 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
677 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
678 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
679 case DW_OP_plus_uconst:
680 stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
681 case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
682 case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
683 case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
684 case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
685 case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
686 case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
687 case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
688 case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
689 /* dbghelp APIs don't know how to cope with this anyway
690 * (for example 'long long' stored in two registers)
691 * FIXME: We should tell winedbg how to deal with it (sigh)
693 if (!piece_found)
695 if (loc->reg != Wine_DW_no_register)
696 FIXME("Only supporting one reg (%d -> %d)\n",
697 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
698 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
700 loc->kind = loc_register;
701 break;
702 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
703 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
704 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
705 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
706 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
707 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
708 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
709 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:
710 /* dbghelp APIs don't know how to cope with this anyway
711 * (for example 'long long' stored in two registers)
712 * FIXME: We should tell winedbg how to deal with it (sigh)
714 if (!piece_found)
716 if (loc->reg != Wine_DW_no_register)
717 FIXME("Only supporting one reg (%d -> %d)\n",
718 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
719 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
721 stack[++stk] = dwarf2_leb128_as_signed(ctx);
722 loc->kind = loc_regrel;
723 break;
724 case DW_OP_fbreg:
725 if (loc->reg != Wine_DW_no_register)
726 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
727 if (frame && frame->kind == loc_register)
729 loc->kind = loc_regrel;
730 loc->reg = frame->reg;
731 stack[++stk] = dwarf2_leb128_as_signed(ctx);
733 else if (frame && frame->kind == loc_regrel)
735 loc->kind = loc_regrel;
736 loc->reg = frame->reg;
737 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
739 else
741 /* FIXME: this could be later optimized by not recomputing
742 * this very location expression
744 loc->kind = loc_dwarf2_block;
745 stack[++stk] = dwarf2_leb128_as_signed(ctx);
747 break;
748 case DW_OP_piece:
750 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
751 WARN("Not handling OP_piece (size=%d)\n", sz);
752 piece_found = TRUE;
754 break;
755 case DW_OP_deref:
756 if (!stk)
758 FIXME("Unexpected empty stack\n");
759 return loc_err_internal;
761 if (loc->reg != Wine_DW_no_register)
763 WARN("Too complex expression for deref\n");
764 return loc_err_too_complex;
766 if (hproc)
768 DWORD addr = stack[stk--];
769 DWORD deref;
771 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
773 WARN("Couldn't read memory at %x\n", addr);
774 return loc_err_cant_read;
776 stack[++stk] = deref;
778 else
780 loc->kind = loc_dwarf2_block;
782 break;
783 default:
784 FIXME("Unhandled attr op: %x\n", op);
785 return loc_err_internal;
788 loc->offset = stack[stk];
789 return 0;
792 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
793 const dwarf2_debug_info_t* di,
794 unsigned long dw,
795 struct location* loc,
796 const struct location* frame)
798 struct attribute xloc;
800 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
802 switch (xloc.form)
804 case DW_FORM_data1: case DW_FORM_data2:
805 case DW_FORM_udata: case DW_FORM_sdata:
806 loc->kind = loc_absolute;
807 loc->reg = 0;
808 loc->offset = xloc.u.uvalue;
809 return TRUE;
810 case DW_FORM_data4: case DW_FORM_data8:
811 loc->kind = loc_dwarf2_location_list;
812 loc->reg = Wine_DW_no_register;
813 loc->offset = xloc.u.uvalue;
814 return TRUE;
817 /* assume we have a block form */
819 if (xloc.u.block.size)
821 dwarf2_traverse_context_t lctx;
822 enum location_error err;
824 lctx.data = xloc.u.block.ptr;
825 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
826 lctx.word_size = ctx->word_size;
828 err = compute_location(&lctx, loc, NULL, frame);
829 if (err < 0)
831 loc->kind = loc_error;
832 loc->reg = err;
834 else if (loc->kind == loc_dwarf2_block)
836 unsigned* ptr = pool_alloc(&ctx->module->pool,
837 sizeof(unsigned) + xloc.u.block.size);
838 *ptr = xloc.u.block.size;
839 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
840 loc->offset = (unsigned long)ptr;
843 return TRUE;
846 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
847 const dwarf2_debug_info_t* di)
849 struct attribute attr;
851 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
853 dwarf2_debug_info_t* type;
855 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
856 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
857 if (!type->symt)
859 /* load the debug info entity */
860 dwarf2_load_one_entry(ctx, type, NULL);
862 return type->symt;
864 return NULL;
867 /******************************************************************
868 * dwarf2_read_one_debug_info
870 * Loads into memory one debug info entry, and recursively its children (if any)
872 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
873 dwarf2_traverse_context_t* traverse,
874 dwarf2_debug_info_t** pdi)
876 const dwarf2_abbrev_entry_t*abbrev;
877 unsigned long entry_code;
878 unsigned long offset;
879 dwarf2_debug_info_t* di;
880 dwarf2_debug_info_t* child;
881 dwarf2_debug_info_t** where;
882 dwarf2_abbrev_entry_attr_t* attr;
883 unsigned i;
884 struct attribute sibling;
886 offset = traverse->data - ctx->sections[ctx->section].address;
887 entry_code = dwarf2_leb128_as_unsigned(traverse);
888 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
889 if (!entry_code)
891 *pdi = NULL;
892 return TRUE;
894 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
895 if (!abbrev)
897 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
898 return FALSE;
900 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
901 if (!di) return FALSE;
902 di->abbrev = abbrev;
903 di->symt = NULL;
905 if (abbrev->num_attr)
907 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
908 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
910 di->data[i] = traverse->data;
911 dwarf2_swallow_attribute(traverse, attr);
914 else di->data = NULL;
915 if (abbrev->have_child)
917 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
918 while (traverse->data < traverse->end_data)
920 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
921 if (!child) break;
922 where = vector_add(&di->children, &ctx->pool);
923 if (!where) return FALSE;
924 *where = child;
927 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
928 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
930 WARN("setting cursor for %s to next sibling <0x%lx>\n",
931 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
932 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
934 *pdi = di;
935 return TRUE;
938 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
939 dwarf2_debug_info_t* di)
941 struct attribute name;
942 struct attribute size;
943 struct attribute encoding;
944 enum BasicType bt;
945 int cache_idx = -1;
946 if (di->symt) return di->symt;
948 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
950 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
951 name.u.string = NULL;
952 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
953 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
955 switch (encoding.u.uvalue)
957 case DW_ATE_void: bt = btVoid; break;
958 case DW_ATE_address: bt = btULong; break;
959 case DW_ATE_boolean: bt = btBool; break;
960 case DW_ATE_complex_float: bt = btComplex; break;
961 case DW_ATE_float: bt = btFloat; break;
962 case DW_ATE_signed: bt = btInt; break;
963 case DW_ATE_unsigned: bt = btUInt; break;
964 case DW_ATE_signed_char: bt = btChar; break;
965 case DW_ATE_unsigned_char: bt = btChar; break;
966 default: bt = btNoType; break;
968 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
969 switch (bt)
971 case btVoid:
972 assert(size.u.uvalue == 0);
973 cache_idx = sc_void;
974 break;
975 case btInt:
976 switch (size.u.uvalue)
978 case 1: cache_idx = sc_int1; break;
979 case 2: cache_idx = sc_int2; break;
980 case 4: cache_idx = sc_int4; break;
982 break;
983 default: break;
985 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
986 ctx->symt_cache[cache_idx] = di->symt;
988 if (di->abbrev->have_child) FIXME("Unsupported children\n");
989 return di->symt;
992 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
993 dwarf2_debug_info_t* di)
995 struct symt* ref_type;
996 struct attribute name;
998 if (di->symt) return di->symt;
1000 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1002 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1003 ref_type = dwarf2_lookup_type(ctx, di);
1005 if (name.u.string)
1006 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1007 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1008 return di->symt;
1011 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1012 dwarf2_debug_info_t* di)
1014 struct symt* ref_type;
1015 struct attribute size;
1017 if (di->symt) return di->symt;
1019 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1021 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1022 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1024 ref_type = ctx->symt_cache[sc_void];
1025 assert(ref_type);
1027 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1028 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1029 return di->symt;
1032 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1033 dwarf2_debug_info_t* di)
1035 struct symt* ref_type;
1036 struct symt* idx_type = NULL;
1037 struct attribute min, max, cnt;
1038 dwarf2_debug_info_t* child;
1039 unsigned int i;
1041 if (di->symt) return di->symt;
1043 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1045 if (!di->abbrev->have_child)
1047 FIXME("array without range information\n");
1048 return NULL;
1050 ref_type = dwarf2_lookup_type(ctx, di);
1052 for (i=0; i<vector_length(&di->children); i++)
1054 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1055 switch (child->abbrev->tag)
1057 case DW_TAG_subrange_type:
1058 idx_type = dwarf2_lookup_type(ctx, child);
1059 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1060 min.u.uvalue = 0;
1061 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1062 max.u.uvalue = 0;
1063 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1064 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1065 break;
1066 default:
1067 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1068 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1069 break;
1072 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1073 return di->symt;
1076 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1077 dwarf2_debug_info_t* di)
1079 struct symt* ref_type;
1081 if (di->symt) return di->symt;
1083 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1085 ref_type = dwarf2_lookup_type(ctx, di);
1086 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1087 di->symt = ref_type;
1089 return ref_type;
1092 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1093 dwarf2_debug_info_t* di)
1095 struct symt* ref_type;
1097 if (di->symt) return di->symt;
1099 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1101 ref_type = dwarf2_lookup_type(ctx, di);
1102 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1103 di->symt = ref_type;
1105 return ref_type;
1108 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1109 dwarf2_debug_info_t* di)
1111 struct symt* ref_type = NULL;
1113 if (di->symt) return di->symt;
1115 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1117 ref_type = dwarf2_lookup_type(ctx, di);
1118 /* FIXME: for now, we hard-wire C++ references to pointers */
1119 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1121 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1123 return di->symt;
1126 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1127 const dwarf2_debug_info_t* di,
1128 struct symt_udt* parent)
1130 struct symt* elt_type;
1131 struct attribute name;
1132 struct attribute bit_size;
1133 struct attribute bit_offset;
1134 struct location loc;
1136 assert(parent);
1138 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1140 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1141 elt_type = dwarf2_lookup_type(ctx, di);
1142 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1144 if (loc.kind != loc_absolute)
1146 FIXME("Found register, while not expecting it\n");
1147 loc.offset = 0;
1149 else
1150 TRACE("found member_location at %s -> %lu\n",
1151 dwarf2_debug_ctx(ctx), loc.offset);
1153 else
1154 loc.offset = 0;
1155 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1156 bit_size.u.uvalue = 0;
1157 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1159 /* FIXME: we should only do this when implementation is LSB (which is
1160 * the case on i386 processors)
1162 struct attribute nbytes;
1163 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1165 DWORD64 size;
1166 nbytes.u.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
1168 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1170 else bit_offset.u.uvalue = 0;
1171 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1172 (loc.offset << 3) + bit_offset.u.uvalue,
1173 bit_size.u.uvalue);
1175 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1178 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1179 dwarf2_debug_info_t* di,
1180 enum UdtKind udt)
1182 struct attribute name;
1183 struct attribute size;
1185 if (di->symt) return di->symt;
1187 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1189 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1190 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1192 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1194 if (di->abbrev->have_child) /** any interest to not have child ? */
1196 dwarf2_debug_info_t* child;
1197 unsigned int i;
1199 for (i=0; i<vector_length(&di->children); i++)
1201 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1203 switch (child->abbrev->tag)
1205 case DW_TAG_member:
1206 /* FIXME: should I follow the sibling stuff ?? */
1207 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1208 break;
1209 case DW_TAG_enumeration_type:
1210 dwarf2_parse_enumeration_type(ctx, child);
1211 break;
1212 case DW_TAG_structure_type:
1213 case DW_TAG_class_type:
1214 case DW_TAG_union_type:
1215 case DW_TAG_typedef:
1216 /* FIXME: we need to handle nested udt definitions */
1217 break;
1218 default:
1219 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1220 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1221 break;
1226 return di->symt;
1229 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1230 const dwarf2_debug_info_t* di,
1231 struct symt_enum* parent)
1233 struct attribute name;
1234 struct attribute value;
1236 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1238 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1239 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1240 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1242 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1245 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1246 dwarf2_debug_info_t* di)
1248 struct attribute name;
1249 struct attribute size;
1250 struct symt_basic* basetype;
1252 if (di->symt) return di->symt;
1254 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1256 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1257 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1259 switch (size.u.uvalue) /* FIXME: that's wrong */
1261 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1262 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1263 default:
1264 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1267 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1269 if (di->abbrev->have_child) /* any interest to not have child ? */
1271 dwarf2_debug_info_t* child;
1272 unsigned int i;
1274 /* FIXME: should we use the sibling stuff ?? */
1275 for (i=0; i<vector_length(&di->children); i++)
1277 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1279 switch (child->abbrev->tag)
1281 case DW_TAG_enumerator:
1282 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1283 break;
1284 default:
1285 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1286 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1290 return di->symt;
1293 /* structure used to pass information around when parsing a subprogram */
1294 typedef struct dwarf2_subprogram_s
1296 dwarf2_parse_context_t* ctx;
1297 struct symt_compiland* compiland;
1298 struct symt_function* func;
1299 BOOL non_computed_variable;
1300 struct location frame;
1301 } dwarf2_subprogram_t;
1303 /******************************************************************
1304 * dwarf2_parse_variable
1306 * Parses any variable (parameter, local/global variable)
1308 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1309 struct symt_block* block,
1310 dwarf2_debug_info_t* di)
1312 struct symt* param_type;
1313 struct attribute name, value;
1314 struct location loc;
1315 BOOL is_pmt;
1317 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1319 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1320 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1322 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1323 /* cannot do much without the name, the functions below won't like it. */
1324 return;
1326 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1327 &loc, &subpgm->frame))
1329 struct attribute ext;
1331 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1332 name.u.string, loc.kind, loc.offset, loc.reg,
1333 dwarf2_debug_ctx(subpgm->ctx));
1335 switch (loc.kind)
1337 case loc_absolute:
1338 /* it's a global variable */
1339 /* FIXME: we don't handle its scope yet */
1340 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1341 ext.u.uvalue = 0;
1342 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1343 name.u.string, !ext.u.uvalue,
1344 subpgm->ctx->load_offset + loc.offset,
1345 0, param_type);
1346 break;
1347 default:
1348 subpgm->non_computed_variable = TRUE;
1349 /* fall through */
1350 case loc_register:
1351 case loc_regrel:
1352 /* either a pmt/variable relative to frame pointer or
1353 * pmt/variable in a register
1355 assert(subpgm->func);
1356 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1357 is_pmt ? DataIsParam : DataIsLocal,
1358 &loc, block, param_type, name.u.string);
1359 break;
1362 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1364 VARIANT v;
1365 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1366 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1367 switch (value.form)
1369 case DW_FORM_data1:
1370 case DW_FORM_data2:
1371 case DW_FORM_data4:
1372 case DW_FORM_udata:
1373 case DW_FORM_addr:
1374 v.n1.n2.vt = VT_UI4;
1375 v.n1.n2.n3.lVal = value.u.uvalue;
1376 break;
1378 case DW_FORM_sdata:
1379 v.n1.n2.vt = VT_I4;
1380 v.n1.n2.n3.lVal = value.u.svalue;
1381 break;
1383 case DW_FORM_strp:
1384 case DW_FORM_string:
1385 /* FIXME: native doesn't report const strings from here !!
1386 * however, the value of the string is in the code somewhere
1388 v.n1.n2.vt = VT_I1 | VT_BYREF;
1389 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1390 break;
1392 case DW_FORM_data8:
1393 case DW_FORM_block:
1394 case DW_FORM_block1:
1395 case DW_FORM_block2:
1396 case DW_FORM_block4:
1398 default:
1399 FIXME("Unsupported form for const value %s (%lx)\n",
1400 name.u.string, value.form);
1401 v.n1.n2.vt = VT_EMPTY;
1403 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1404 name.u.string, param_type, &v)->symt;
1406 if (is_pmt && subpgm->func && subpgm->func->type)
1407 symt_add_function_signature_parameter(subpgm->ctx->module,
1408 (struct symt_function_signature*)subpgm->func->type,
1409 param_type);
1411 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1414 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1415 const dwarf2_debug_info_t* di)
1417 struct attribute name;
1418 struct attribute low_pc;
1419 struct location loc;
1421 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1423 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1424 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1425 name.u.string = NULL;
1427 loc.kind = loc_absolute;
1428 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1429 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1430 &loc, name.u.string);
1433 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1434 struct symt_block* parent_block,
1435 const dwarf2_debug_info_t* di);
1437 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1438 struct symt_block* parent_block,
1439 const dwarf2_debug_info_t* di)
1441 struct symt_block* block;
1442 struct attribute low_pc;
1443 struct attribute high_pc;
1445 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1447 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1448 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1450 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1451 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1452 high_pc.u.uvalue - low_pc.u.uvalue);
1454 if (di->abbrev->have_child) /** any interest to not have child ? */
1456 dwarf2_debug_info_t* child;
1457 unsigned int i;
1459 for (i=0; i<vector_length(&di->children); i++)
1461 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1463 switch (child->abbrev->tag)
1465 case DW_TAG_formal_parameter:
1466 case DW_TAG_variable:
1467 dwarf2_parse_variable(subpgm, block, child);
1468 break;
1469 case DW_TAG_lexical_block:
1470 dwarf2_parse_subprogram_block(subpgm, block, child);
1471 break;
1472 case DW_TAG_inlined_subroutine:
1473 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1474 break;
1475 case DW_TAG_label:
1476 dwarf2_parse_subprogram_label(subpgm, child);
1477 break;
1478 default:
1479 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1480 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1481 dwarf2_debug_di(di));
1485 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1488 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1489 struct symt_block* parent_block,
1490 const dwarf2_debug_info_t* di)
1492 struct symt_block* block;
1493 struct attribute low_pc;
1494 struct attribute high_pc;
1496 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1498 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1499 low_pc.u.uvalue = 0;
1500 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1501 high_pc.u.uvalue = 0;
1503 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1504 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1505 high_pc.u.uvalue - low_pc.u.uvalue);
1507 if (di->abbrev->have_child) /** any interest to not have child ? */
1509 dwarf2_debug_info_t* child;
1510 unsigned int i;
1512 for (i=0; i<vector_length(&di->children); i++)
1514 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1516 switch (child->abbrev->tag)
1518 case DW_TAG_inlined_subroutine:
1519 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1520 break;
1521 case DW_TAG_variable:
1522 dwarf2_parse_variable(subpgm, block, child);
1523 break;
1524 case DW_TAG_lexical_block:
1525 dwarf2_parse_subprogram_block(subpgm, block, child);
1526 break;
1527 case DW_TAG_subprogram:
1528 /* FIXME: likely a declaration (to be checked)
1529 * skip it for now
1531 break;
1532 case DW_TAG_formal_parameter:
1533 /* FIXME: likely elements for exception handling (GCC flavor)
1534 * Skip it for now
1536 break;
1537 case DW_TAG_label:
1538 dwarf2_parse_subprogram_label(subpgm, child);
1539 break;
1540 case DW_TAG_class_type:
1541 case DW_TAG_structure_type:
1542 case DW_TAG_union_type:
1543 case DW_TAG_enumeration_type:
1544 case DW_TAG_typedef:
1545 /* the type referred to will be loaded when we need it, so skip it */
1546 break;
1547 default:
1548 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1549 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1554 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1557 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1558 dwarf2_debug_info_t* di,
1559 struct symt_compiland* compiland)
1561 struct attribute name;
1562 struct attribute low_pc;
1563 struct attribute high_pc;
1564 struct attribute is_decl;
1565 struct attribute inline_flags;
1566 struct symt* ret_type;
1567 struct symt_function_signature* sig_type;
1568 dwarf2_subprogram_t subpgm;
1570 if (di->symt) return di->symt;
1572 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1574 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1576 WARN("No name for function... dropping function\n");
1577 return NULL;
1579 /* if it's an abstract representation of an inline function, there should be
1580 * a concrete object that we'll handle
1582 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1584 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1585 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1586 return NULL;
1589 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1590 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1591 /* As functions (defined as inline assembly) get debug info with dwarf
1592 * (not the case for stabs), we just drop Wine's thunks here...
1593 * Actual thunks will be created in elf_module from the symbol table
1595 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1596 ctx->thunks) >= 0)
1597 return NULL;
1598 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1599 is_decl.u.uvalue = 0;
1601 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1603 ret_type = ctx->symt_cache[sc_void];
1604 assert(ret_type);
1607 /* FIXME: assuming C source code */
1608 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1609 if (!is_decl.u.uvalue)
1611 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1612 ctx->load_offset + low_pc.u.uvalue,
1613 high_pc.u.uvalue - low_pc.u.uvalue,
1614 &sig_type->symt);
1615 di->symt = &subpgm.func->symt;
1617 else subpgm.func = NULL;
1619 subpgm.ctx = ctx;
1620 subpgm.compiland = compiland;
1621 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1622 &subpgm.frame, NULL))
1624 /* on stack !! */
1625 subpgm.frame.kind = loc_regrel;
1626 subpgm.frame.reg = 0;
1627 subpgm.frame.offset = 0;
1629 subpgm.non_computed_variable = FALSE;
1631 if (di->abbrev->have_child) /** any interest to not have child ? */
1633 dwarf2_debug_info_t* child;
1634 unsigned int i;
1636 for (i=0; i<vector_length(&di->children); i++)
1638 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1640 switch (child->abbrev->tag)
1642 case DW_TAG_variable:
1643 case DW_TAG_formal_parameter:
1644 dwarf2_parse_variable(&subpgm, NULL, child);
1645 break;
1646 case DW_TAG_lexical_block:
1647 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1648 break;
1649 case DW_TAG_inlined_subroutine:
1650 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1651 break;
1652 case DW_TAG_subprogram:
1653 /* FIXME: likely a declaration (to be checked)
1654 * skip it for now
1656 break;
1657 case DW_TAG_label:
1658 dwarf2_parse_subprogram_label(&subpgm, child);
1659 break;
1660 case DW_TAG_class_type:
1661 case DW_TAG_structure_type:
1662 case DW_TAG_union_type:
1663 case DW_TAG_enumeration_type:
1664 case DW_TAG_typedef:
1665 /* the type referred to will be loaded when we need it, so skip it */
1666 break;
1667 case DW_TAG_unspecified_parameters:
1668 /* FIXME: no support in dbghelp's internals so far */
1669 break;
1670 default:
1671 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1672 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1677 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1679 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1680 &subpgm.frame, NULL);
1682 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1684 return di->symt;
1687 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1688 dwarf2_debug_info_t* di)
1690 struct symt* ret_type;
1691 struct symt_function_signature* sig_type;
1693 if (di->symt) return di->symt;
1695 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1697 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1699 ret_type = ctx->symt_cache[sc_void];
1700 assert(ret_type);
1703 /* FIXME: assuming C source code */
1704 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1706 if (di->abbrev->have_child) /** any interest to not have child ? */
1708 dwarf2_debug_info_t* child;
1709 unsigned int i;
1711 for (i=0; i<vector_length(&di->children); i++)
1713 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1715 switch (child->abbrev->tag)
1717 case DW_TAG_formal_parameter:
1718 symt_add_function_signature_parameter(ctx->module, sig_type,
1719 dwarf2_lookup_type(ctx, child));
1720 break;
1721 case DW_TAG_unspecified_parameters:
1722 WARN("Unsupported unspecified parameters\n");
1723 break;
1728 return di->symt = &sig_type->symt;
1731 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1732 dwarf2_debug_info_t* di,
1733 struct symt_compiland* compiland)
1735 switch (di->abbrev->tag)
1737 case DW_TAG_typedef:
1738 dwarf2_parse_typedef(ctx, di);
1739 break;
1740 case DW_TAG_base_type:
1741 dwarf2_parse_base_type(ctx, di);
1742 break;
1743 case DW_TAG_pointer_type:
1744 dwarf2_parse_pointer_type(ctx, di);
1745 break;
1746 case DW_TAG_class_type:
1747 dwarf2_parse_udt_type(ctx, di, UdtClass);
1748 break;
1749 case DW_TAG_structure_type:
1750 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1751 break;
1752 case DW_TAG_union_type:
1753 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1754 break;
1755 case DW_TAG_array_type:
1756 dwarf2_parse_array_type(ctx, di);
1757 break;
1758 case DW_TAG_const_type:
1759 dwarf2_parse_const_type(ctx, di);
1760 break;
1761 case DW_TAG_volatile_type:
1762 dwarf2_parse_volatile_type(ctx, di);
1763 break;
1764 case DW_TAG_reference_type:
1765 dwarf2_parse_reference_type(ctx, di);
1766 break;
1767 case DW_TAG_enumeration_type:
1768 dwarf2_parse_enumeration_type(ctx, di);
1769 break;
1770 case DW_TAG_subprogram:
1771 dwarf2_parse_subprogram(ctx, di, compiland);
1772 break;
1773 case DW_TAG_subroutine_type:
1774 dwarf2_parse_subroutine_type(ctx, di);
1775 break;
1776 case DW_TAG_variable:
1778 dwarf2_subprogram_t subpgm;
1780 subpgm.ctx = ctx;
1781 subpgm.compiland = compiland;
1782 subpgm.func = NULL;
1783 subpgm.frame.kind = loc_absolute;
1784 subpgm.frame.offset = 0;
1785 subpgm.frame.reg = Wine_DW_no_register;
1786 dwarf2_parse_variable(&subpgm, NULL, di);
1788 break;
1789 default:
1790 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1791 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1795 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1796 const struct vector* v, unsigned file, unsigned line)
1798 struct symt_function* func;
1799 struct symt_ht* symt;
1800 unsigned* psrc;
1802 if (!file || !(psrc = vector_at(v, file - 1))) return;
1804 TRACE("%s %lx %s %u\n",
1805 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1806 if (!(symt = symt_find_nearest(module, address)) ||
1807 symt->symt.tag != SymTagFunction) return;
1808 func = (struct symt_function*)symt;
1809 symt_add_func_line(module, func, *psrc, line, address - func->address);
1812 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1813 dwarf2_parse_context_t* ctx,
1814 const char* compile_dir,
1815 unsigned long offset)
1817 dwarf2_traverse_context_t traverse;
1818 unsigned long length;
1819 unsigned version, header_len, insn_size, default_stmt;
1820 unsigned line_range, opcode_base;
1821 int line_base;
1822 const unsigned char* opcode_len;
1823 struct vector dirs;
1824 struct vector files;
1825 const char** p;
1827 /* section with line numbers stripped */
1828 if (sections[section_line].address == ELF_NO_MAP)
1829 return FALSE;
1831 traverse.data = sections[section_line].address + offset;
1832 traverse.start_data = traverse.data;
1833 traverse.end_data = traverse.data + 4;
1834 traverse.word_size = ctx->word_size;
1836 length = dwarf2_parse_u4(&traverse);
1837 traverse.end_data = traverse.start_data + length;
1839 version = dwarf2_parse_u2(&traverse);
1840 header_len = dwarf2_parse_u4(&traverse);
1841 insn_size = dwarf2_parse_byte(&traverse);
1842 default_stmt = dwarf2_parse_byte(&traverse);
1843 line_base = (signed char)dwarf2_parse_byte(&traverse);
1844 line_range = dwarf2_parse_byte(&traverse);
1845 opcode_base = dwarf2_parse_byte(&traverse);
1847 opcode_len = traverse.data;
1848 traverse.data += opcode_base - 1;
1850 vector_init(&dirs, sizeof(const char*), 4);
1851 p = vector_add(&dirs, &ctx->pool);
1852 *p = compile_dir ? compile_dir : ".";
1853 while (*traverse.data)
1855 const char* rel = (const char*)traverse.data;
1856 unsigned rellen = strlen(rel);
1857 TRACE("Got include %s\n", rel);
1858 traverse.data += rellen + 1;
1859 p = vector_add(&dirs, &ctx->pool);
1861 if (*rel == '/' || !compile_dir)
1862 *p = rel;
1863 else
1865 /* include directory relative to compile directory */
1866 unsigned baselen = strlen(compile_dir);
1867 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1868 strcpy(tmp, compile_dir);
1869 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1870 strcpy(&tmp[baselen], rel);
1871 *p = tmp;
1875 traverse.data++;
1877 vector_init(&files, sizeof(unsigned), 16);
1878 while (*traverse.data)
1880 unsigned int dir_index, mod_time, length;
1881 const char* name;
1882 const char* dir;
1883 unsigned* psrc;
1885 name = (const char*)traverse.data;
1886 traverse.data += strlen(name) + 1;
1887 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1888 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1889 length = dwarf2_leb128_as_unsigned(&traverse);
1890 dir = *(const char**)vector_at(&dirs, dir_index);
1891 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1892 psrc = vector_add(&files, &ctx->pool);
1893 *psrc = source_new(ctx->module, dir, name);
1895 traverse.data++;
1897 while (traverse.data < traverse.end_data)
1899 unsigned long address = 0;
1900 unsigned file = 1;
1901 unsigned line = 1;
1902 unsigned is_stmt = default_stmt;
1903 BOOL basic_block = FALSE, end_sequence = FALSE;
1904 unsigned opcode, extopcode, i;
1906 while (!end_sequence)
1908 opcode = dwarf2_parse_byte(&traverse);
1909 TRACE("Got opcode %x\n", opcode);
1911 if (opcode >= opcode_base)
1913 unsigned delta = opcode - opcode_base;
1915 address += (delta / line_range) * insn_size;
1916 line += line_base + (delta % line_range);
1917 basic_block = TRUE;
1918 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1920 else
1922 switch (opcode)
1924 case DW_LNS_copy:
1925 basic_block = FALSE;
1926 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1927 break;
1928 case DW_LNS_advance_pc:
1929 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1930 break;
1931 case DW_LNS_advance_line:
1932 line += dwarf2_leb128_as_signed(&traverse);
1933 break;
1934 case DW_LNS_set_file:
1935 file = dwarf2_leb128_as_unsigned(&traverse);
1936 break;
1937 case DW_LNS_set_column:
1938 dwarf2_leb128_as_unsigned(&traverse);
1939 break;
1940 case DW_LNS_negate_stmt:
1941 is_stmt = !is_stmt;
1942 break;
1943 case DW_LNS_set_basic_block:
1944 basic_block = 1;
1945 break;
1946 case DW_LNS_const_add_pc:
1947 address += ((255 - opcode_base) / line_range) * insn_size;
1948 break;
1949 case DW_LNS_fixed_advance_pc:
1950 address += dwarf2_parse_u2(&traverse);
1951 break;
1952 case DW_LNS_extended_op:
1953 dwarf2_leb128_as_unsigned(&traverse);
1954 extopcode = dwarf2_parse_byte(&traverse);
1955 switch (extopcode)
1957 case DW_LNE_end_sequence:
1958 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1959 end_sequence = TRUE;
1960 break;
1961 case DW_LNE_set_address:
1962 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
1963 break;
1964 case DW_LNE_define_file:
1965 FIXME("not handled %s\n", traverse.data);
1966 traverse.data += strlen((const char *)traverse.data) + 1;
1967 dwarf2_leb128_as_unsigned(&traverse);
1968 dwarf2_leb128_as_unsigned(&traverse);
1969 dwarf2_leb128_as_unsigned(&traverse);
1970 break;
1971 default:
1972 FIXME("Unsupported extended opcode %x\n", extopcode);
1973 break;
1975 break;
1976 default:
1977 WARN("Unsupported opcode %x\n", opcode);
1978 for (i = 0; i < opcode_len[opcode]; i++)
1979 dwarf2_leb128_as_unsigned(&traverse);
1980 break;
1985 return TRUE;
1988 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1989 struct module* module,
1990 const struct elf_thunk_area* thunks,
1991 dwarf2_traverse_context_t* mod_ctx,
1992 unsigned long load_offset)
1994 dwarf2_parse_context_t ctx;
1995 dwarf2_traverse_context_t abbrev_ctx;
1996 dwarf2_debug_info_t* di;
1997 dwarf2_traverse_context_t cu_ctx;
1998 const unsigned char* comp_unit_start = mod_ctx->data;
1999 unsigned long cu_length;
2000 unsigned short cu_version;
2001 unsigned long cu_abbrev_offset;
2002 BOOL ret = FALSE;
2004 cu_length = dwarf2_parse_u4(mod_ctx);
2005 cu_ctx.data = cu_ctx.start_data = mod_ctx->data;
2006 cu_ctx.end_data = mod_ctx->data + cu_length;
2007 mod_ctx->data += cu_length;
2008 cu_version = dwarf2_parse_u2(&cu_ctx);
2009 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2010 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2012 TRACE("Compilation Unit Header found at 0x%x:\n",
2013 comp_unit_start - sections[section_debug].address);
2014 TRACE("- length: %lu\n", cu_length);
2015 TRACE("- version: %u\n", cu_version);
2016 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2017 TRACE("- word_size: %u\n", cu_ctx.word_size);
2019 if (cu_version != 2)
2021 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2022 cu_version);
2023 return FALSE;
2026 pool_init(&ctx.pool, 65536);
2027 ctx.sections = sections;
2028 ctx.section = section_debug;
2029 ctx.module = module;
2030 ctx.word_size = cu_ctx.word_size;
2031 ctx.thunks = thunks;
2032 ctx.load_offset = load_offset;
2033 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2034 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2035 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2037 abbrev_ctx.start_data = sections[section_abbrev].address + cu_abbrev_offset;
2038 abbrev_ctx.data = abbrev_ctx.start_data;
2039 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2040 abbrev_ctx.word_size = cu_ctx.word_size;
2041 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2043 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2044 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2046 if (di->abbrev->tag == DW_TAG_compile_unit)
2048 struct attribute name;
2049 dwarf2_debug_info_t** pdi = NULL;
2050 struct attribute stmt_list, low_pc;
2051 struct attribute comp_dir;
2053 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2054 name.u.string = NULL;
2056 /* get working directory of current compilation unit */
2057 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2058 comp_dir.u.string = NULL;
2060 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2061 low_pc.u.uvalue = 0;
2062 di->symt = &symt_new_compiland(module,
2063 ctx.load_offset + low_pc.u.uvalue,
2064 source_new(module, comp_dir.u.string, name.u.string))->symt;
2066 if (di->abbrev->have_child)
2068 unsigned int i;
2069 for (i=0; i<vector_length(&di->children); i++)
2071 pdi = vector_at(&di->children, i);
2072 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2075 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2077 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2078 module->module.LineNumbers = TRUE;
2080 ret = TRUE;
2082 else FIXME("Should have a compilation unit here\n");
2083 pool_destroy(&ctx.pool);
2084 return ret;
2087 static BOOL dwarf2_lookup_loclist(const struct module* module, const BYTE* start,
2088 unsigned long ip,
2089 dwarf2_traverse_context_t* lctx)
2091 DWORD beg, end;
2092 const BYTE* ptr = start;
2093 DWORD len;
2095 while (ptr < module->dwarf2_info->debug_loc.address + module->dwarf2_info->debug_loc.size)
2097 beg = dwarf2_get_u4(ptr); ptr += 4;
2098 end = dwarf2_get_u4(ptr); ptr += 4;
2099 if (!beg && !end) break;
2100 len = dwarf2_get_u2(ptr); ptr += 2;
2102 if (beg <= ip && ip < end)
2104 lctx->data = ptr;
2105 lctx->end_data = ptr + len;
2106 lctx->word_size = 4; /* FIXME word size !!! */
2107 return TRUE;
2109 ptr += len;
2111 WARN("Couldn't find ip in location list\n");
2112 return FALSE;
2115 static enum location_error loc_compute_frame(struct process* pcs,
2116 const struct module* module,
2117 const struct symt_function* func,
2118 DWORD ip, struct location* frame)
2120 struct symt** psym = NULL;
2121 struct location* pframe;
2122 dwarf2_traverse_context_t lctx;
2123 enum location_error err;
2124 unsigned int i;
2126 for (i=0; i<vector_length(&func->vchildren); i++)
2128 psym = vector_at(&func->vchildren, i);
2129 if ((*psym)->tag == SymTagCustom)
2131 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2133 /* First, recompute the frame information, if needed */
2134 switch (pframe->kind)
2136 case loc_regrel:
2137 case loc_register:
2138 *frame = *pframe;
2139 break;
2140 case loc_dwarf2_location_list:
2141 WARN("Searching loclist for %s\n", func->hash_elt.name);
2142 if (!dwarf2_lookup_loclist(module,
2143 module->dwarf2_info->debug_loc.address + pframe->offset,
2144 ip, &lctx))
2145 return loc_err_out_of_scope;
2146 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2147 if (frame->kind >= loc_user)
2149 WARN("Couldn't compute runtime frame location\n");
2150 return loc_err_too_complex;
2152 break;
2153 default:
2154 WARN("Unsupported frame kind %d\n", pframe->kind);
2155 return loc_err_internal;
2157 return 0;
2160 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2161 return loc_err_internal;
2164 static void dwarf2_location_compute(struct process* pcs,
2165 const struct module* module,
2166 const struct symt_function* func,
2167 struct location* loc)
2169 struct location frame;
2170 DWORD ip;
2171 int err;
2172 dwarf2_traverse_context_t lctx;
2174 if (!func->container || func->container->tag != SymTagCompiland)
2176 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2177 err = loc_err_internal;
2179 else
2181 /* instruction pointer relative to compiland's start */
2182 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2184 if ((err = loc_compute_frame(pcs, module, func, ip, &frame)) == 0)
2186 switch (loc->kind)
2188 case loc_dwarf2_location_list:
2189 /* Then, if the variable has a location list, find it !! */
2190 if (dwarf2_lookup_loclist(module,
2191 module->dwarf2_info->debug_loc.address + loc->offset,
2192 ip, &lctx))
2193 goto do_compute;
2194 err = loc_err_out_of_scope;
2195 break;
2196 case loc_dwarf2_block:
2197 /* or if we have a copy of an existing block, get ready for it */
2199 unsigned* ptr = (unsigned*)loc->offset;
2201 lctx.data = (const BYTE*)(ptr + 1);
2202 lctx.end_data = lctx.data + *ptr;
2203 lctx.word_size = 4; /* FIXME !! */
2205 do_compute:
2206 /* now get the variable */
2207 err = compute_location(&lctx, loc, pcs->handle, &frame);
2208 break;
2209 case loc_register:
2210 case loc_regrel:
2211 /* nothing to do */
2212 break;
2213 default:
2214 WARN("Unsupported local kind %d\n", loc->kind);
2215 err = loc_err_internal;
2219 if (err < 0)
2221 loc->kind = loc_register;
2222 loc->reg = err;
2226 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2227 const struct elf_thunk_area* thunks,
2228 const unsigned char* debug, unsigned int debug_size,
2229 const unsigned char* abbrev, unsigned int abbrev_size,
2230 const unsigned char* str, unsigned int str_size,
2231 const unsigned char* line, unsigned int line_size,
2232 const unsigned char* loclist, unsigned int loclist_size)
2234 dwarf2_section_t section[section_max];
2235 unsigned char* ptr;
2236 dwarf2_traverse_context_t mod_ctx;
2238 mod_ctx.start_data = mod_ctx.data = debug;
2239 mod_ctx.end_data = debug + debug_size;
2241 module->loc_compute = dwarf2_location_compute;
2243 section[section_debug].address = debug;
2244 section[section_debug].size = debug_size;
2245 section[section_abbrev].address = abbrev;
2246 section[section_abbrev].size = abbrev_size;
2247 section[section_string].address = str;
2248 section[section_string].size = str_size;
2249 section[section_line].address = line;
2250 section[section_line].size = line_size;
2252 if (loclist_size)
2254 /* initialize the dwarf2 specific info block for this module.
2255 * As we'll need later on the .debug_loc section content, we copy it in
2256 * the module structure for later reuse
2258 module->dwarf2_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*module->dwarf2_info) + loclist_size);
2259 if (!module->dwarf2_info) return FALSE;
2260 ptr = (unsigned char*)(module->dwarf2_info + 1);
2261 memcpy(ptr, loclist, loclist_size);
2262 module->dwarf2_info->debug_loc.address = ptr;
2263 module->dwarf2_info->debug_loc.size = loclist_size;
2266 while (mod_ctx.data < mod_ctx.end_data)
2268 dwarf2_parse_compilation_unit(section, module, thunks, &mod_ctx, load_offset);
2270 module->module.SymType = SymDia;
2271 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2272 /* FIXME: we could have a finer grain here */
2273 module->module.GlobalSymbols = TRUE;
2274 module->module.TypeInfo = TRUE;
2275 module->module.SourceIndexed = TRUE;
2276 module->module.Publics = TRUE;
2277 return TRUE;