dbghelp: Silence typedefs children in sub program block.
[wine/gsoc_dplay.git] / dlls / dbghelp / dwarf.c
blobcd66c0e1838d89a75f0dcd401569247ff37f4517
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <stdio.h>
39 #ifndef PATH_MAX
40 #define PATH_MAX MAX_PATH
41 #endif
42 #include <assert.h>
43 #include <stdarg.h>
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winreg.h"
48 #include "winnls.h"
50 #include "dbghelp_private.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
56 /* FIXME:
57 * - Functions:
58 * o unspecified parameters
59 * o inlined functions
60 * o Debug{Start|End}Point
61 * o CFA
62 * - Udt
63 * o proper types loading (nesting)
66 #if 0
67 static void dump(const void* ptr, unsigned len)
69 int i, j;
70 BYTE msg[128];
71 static const char hexof[] = "0123456789abcdef";
72 const BYTE* x = (const BYTE*)ptr;
74 for (i = 0; i < len; i += 16)
76 sprintf(msg, "%08x: ", i);
77 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
78 for (j = 0; j < min(16, len - i); j++)
80 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
81 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
82 msg[10 + 3 * j + 2] = ' ';
83 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
84 x[i + j] : '.';
86 msg[10 + 3 * 16] = ' ';
87 msg[10 + 3 * 16 + 1 + 16] = '\0';
88 TRACE("%s\n", msg);
91 #endif
93 /**
95 * Main Specs:
96 * http://www.eagercon.com/dwarf/dwarf3std.htm
97 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
99 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
101 * example of projects who do dwarf2 parsing:
102 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
103 * http://elis.ugent.be/diota/log/ltrace_elf.c
105 #include "dwarf.h"
108 * Parsers
111 typedef struct dwarf2_abbrev_entry_attr_s
113 unsigned long attribute;
114 unsigned long form;
115 struct dwarf2_abbrev_entry_attr_s* next;
116 } dwarf2_abbrev_entry_attr_t;
118 typedef struct dwarf2_abbrev_entry_s
120 unsigned long entry_code;
121 unsigned long tag;
122 unsigned char have_child;
123 unsigned num_attr;
124 dwarf2_abbrev_entry_attr_t* attrs;
125 } dwarf2_abbrev_entry_t;
127 struct dwarf2_block
129 unsigned size;
130 const unsigned char* ptr;
133 struct attribute
135 unsigned long form;
136 union
138 unsigned long uvalue;
139 long svalue;
140 const char* string;
141 struct dwarf2_block block;
142 } u;
145 typedef struct dwarf2_debug_info_s
147 const dwarf2_abbrev_entry_t*abbrev;
148 struct symt* symt;
149 const unsigned char** data;
150 struct vector children;
151 } dwarf2_debug_info_t;
153 typedef struct dwarf2_section_s
155 const unsigned char* address;
156 unsigned size;
157 } dwarf2_section_t;
159 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
161 typedef struct dwarf2_traverse_context_s
163 const unsigned char* data;
164 const unsigned char* start_data;
165 const unsigned char* end_data;
166 unsigned char word_size;
167 } dwarf2_traverse_context_t;
169 typedef struct dwarf2_parse_context_s
171 const dwarf2_section_t* sections;
172 unsigned section;
173 struct pool pool;
174 struct module* module;
175 const struct elf_thunk_area*thunks;
176 struct sparse_array abbrev_table;
177 struct sparse_array debug_info_table;
178 unsigned long ref_offset;
179 unsigned char word_size;
180 } dwarf2_parse_context_t;
182 /* stored in the dbghelp's module internal structure for later reuse */
183 struct dwarf2_module_info_s
185 dwarf2_section_t debug_loc;
188 #define loc_dwarf2_location_list (loc_user + 0)
189 #define loc_dwarf2_block (loc_user + 1)
191 /* forward declarations */
192 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
194 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
196 return *ptr;
199 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
201 unsigned char uvalue = dwarf2_get_byte(ctx->data);
202 ctx->data += 1;
203 return uvalue;
206 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
208 return *(const unsigned short*)ptr;
211 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
213 unsigned short uvalue = dwarf2_get_u2(ctx->data);
214 ctx->data += 2;
215 return uvalue;
218 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
220 return *(const unsigned long*)ptr;
223 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
225 unsigned long uvalue = dwarf2_get_u4(ctx->data);
226 ctx->data += 4;
227 return uvalue;
230 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
232 unsigned long ret = 0;
233 unsigned char byte;
234 unsigned shift = 0;
238 byte = dwarf2_get_byte(ptr++);
239 ret |= (byte & 0x7f) << shift;
240 shift += 7;
241 } while (byte & 0x80);
243 if (end) *end = ptr;
244 return ret;
247 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
249 unsigned long ret;
251 assert(ctx);
253 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
255 return ret;
258 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
260 long ret = 0;
261 unsigned char byte;
262 unsigned shift = 0;
263 const unsigned size = sizeof(int) * 8;
267 byte = dwarf2_get_byte(ptr++);
268 ret |= (byte & 0x7f) << shift;
269 shift += 7;
270 } while (byte & 0x80);
271 if (end) *end = ptr;
273 /* as spec: sign bit of byte is 2nd high order bit (80x40)
274 * -> 0x80 is used as flag.
276 if ((shift < size) && (byte & 0x40))
278 ret |= - (1 << shift);
280 return ret;
283 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
285 long ret = 0;
287 assert(ctx);
289 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
290 return ret;
293 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
295 unsigned ret;
296 for (ret = 0; ctx->data[ret] & 0x80; ret++);
297 return ret + 1;
300 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
302 unsigned long ret;
304 switch (word_size)
306 case 4:
307 ret = dwarf2_get_u4(ptr);
308 break;
309 default:
310 FIXME("Unsupported Word Size %u\n", word_size);
311 ret = 0;
313 return ret;
316 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
318 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
319 ctx->data += ctx->word_size;
320 return ret;
323 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
325 return wine_dbg_sprintf("ctx(%p)", ctx->data);
328 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
330 return wine_dbg_sprintf("ctx(%p,%s)", ctx, ctx->module->module.ModuleName);
333 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
335 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
336 di->abbrev, di->symt);
339 static dwarf2_abbrev_entry_t*
340 dwarf2_abbrev_table_find_entry(struct sparse_array* abbrev_table,
341 unsigned long entry_code)
343 assert( NULL != abbrev_table );
344 return sparse_array_find(abbrev_table, entry_code);
347 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
348 struct sparse_array* abbrev_table,
349 struct pool* pool)
351 unsigned long entry_code;
352 dwarf2_abbrev_entry_t* abbrev_entry;
353 dwarf2_abbrev_entry_attr_t* new = NULL;
354 dwarf2_abbrev_entry_attr_t* last = NULL;
355 unsigned long attribute;
356 unsigned long form;
358 assert( NULL != abbrev_ctx );
360 TRACE("%s, end at %p\n",
361 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
363 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
364 while (abbrev_ctx->data < abbrev_ctx->end_data)
366 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
367 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
368 TRACE("found entry_code %lu\n", entry_code);
369 if (!entry_code)
371 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
372 break;
374 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
375 assert( NULL != abbrev_entry );
377 abbrev_entry->entry_code = entry_code;
378 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
379 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
380 abbrev_entry->attrs = NULL;
381 abbrev_entry->num_attr = 0;
383 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
384 abbrev_table, sparse_array_length(abbrev_table),
385 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
387 last = NULL;
388 while (1)
390 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
391 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
392 if (!attribute) break;
394 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
395 assert(new);
397 new->attribute = attribute;
398 new->form = form;
399 new->next = NULL;
400 if (abbrev_entry->attrs) last->next = new;
401 else abbrev_entry->attrs = new;
402 last = new;
403 abbrev_entry->num_attr++;
406 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
409 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
410 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
412 unsigned step;
414 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
416 switch (abbrev_attr->form)
418 case DW_FORM_ref_addr:
419 case DW_FORM_addr: step = ctx->word_size; break;
420 case DW_FORM_flag:
421 case DW_FORM_data1:
422 case DW_FORM_ref1: step = 1; break;
423 case DW_FORM_data2:
424 case DW_FORM_ref2: step = 2; break;
425 case DW_FORM_data4:
426 case DW_FORM_ref4:
427 case DW_FORM_strp: step = 4; break;
428 case DW_FORM_data8:
429 case DW_FORM_ref8: step = 8; break;
430 case DW_FORM_sdata:
431 case DW_FORM_ref_udata:
432 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
433 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
434 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
435 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
436 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
437 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
438 default:
439 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
440 return;
442 ctx->data += step;
445 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
446 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
447 const unsigned char* data,
448 struct attribute* attr)
450 attr->form = abbrev_attr->form;
451 switch (attr->form)
453 case DW_FORM_ref_addr:
454 case DW_FORM_addr:
455 attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
456 TRACE("addr<0x%lx>\n", attr->u.uvalue);
457 break;
459 case DW_FORM_flag:
460 attr->u.uvalue = dwarf2_get_byte(data);
461 TRACE("flag<0x%lx>\n", attr->u.uvalue);
462 break;
464 case DW_FORM_data1:
465 attr->u.uvalue = dwarf2_get_byte(data);
466 TRACE("data1<%lu>\n", attr->u.uvalue);
467 break;
469 case DW_FORM_data2:
470 attr->u.uvalue = dwarf2_get_u2(data);
471 TRACE("data2<%lu>\n", attr->u.uvalue);
472 break;
474 case DW_FORM_data4:
475 attr->u.uvalue = dwarf2_get_u4(data);
476 TRACE("data4<%lu>\n", attr->u.uvalue);
477 break;
479 case DW_FORM_data8:
480 FIXME("Unhandled 64bits support\n");
481 break;
483 case DW_FORM_ref1:
484 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
485 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
486 break;
488 case DW_FORM_ref2:
489 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
490 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
491 break;
493 case DW_FORM_ref4:
494 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
495 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
496 break;
498 case DW_FORM_ref8:
499 FIXME("Unhandled 64 bit support\n");
500 break;
502 case DW_FORM_sdata:
503 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
504 break;
506 case DW_FORM_ref_udata:
507 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
508 break;
510 case DW_FORM_udata:
511 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
512 break;
514 case DW_FORM_string:
515 attr->u.string = (const char *)data;
516 TRACE("string<%s>\n", attr->u.string);
517 break;
519 case DW_FORM_strp:
521 unsigned long offset = dwarf2_get_u4(data);
522 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
524 TRACE("strp<%s>\n", attr->u.string);
525 break;
527 case DW_FORM_block:
528 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
529 break;
531 case DW_FORM_block1:
532 attr->u.block.size = dwarf2_get_byte(data);
533 attr->u.block.ptr = data + 1;
534 break;
536 case DW_FORM_block2:
537 attr->u.block.size = dwarf2_get_u2(data);
538 attr->u.block.ptr = data + 2;
539 break;
541 case DW_FORM_block4:
542 attr->u.block.size = dwarf2_get_u4(data);
543 attr->u.block.ptr = data + 4;
544 break;
546 default:
547 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
548 break;
552 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
553 const dwarf2_debug_info_t* di,
554 unsigned at, struct attribute* attr)
556 unsigned i, ai = 0;
557 dwarf2_abbrev_entry_attr_t* abbrev_attr;
558 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
560 while (di)
562 abstract_abbrev_attr = NULL;
563 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
565 if (abbrev_attr->attribute == at)
567 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
568 return TRUE;
570 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
571 at != DW_AT_sibling)
573 abstract_abbrev_attr = abbrev_attr;
574 ai = i;
577 /* do we have an abstract origin debug entry to look into ? */
578 if (!abstract_abbrev_attr) break;
579 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
580 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
581 FIXME("Should have found the debug info entry\n");
583 return FALSE;
586 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
587 struct symt_compiland*);
589 #define Wine_DW_no_register 0x7FFFFFFF
591 static unsigned dwarf2_map_register(int regno)
593 unsigned reg;
595 switch (regno)
597 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
598 case 0: reg = CV_REG_EAX; break;
599 case 1: reg = CV_REG_ECX; break;
600 case 2: reg = CV_REG_EDX; break;
601 case 3: reg = CV_REG_EBX; break;
602 case 4: reg = CV_REG_ESP; break;
603 case 5: reg = CV_REG_EBP; break;
604 case 6: reg = CV_REG_ESI; break;
605 case 7: reg = CV_REG_EDI; break;
606 case 8: reg = CV_REG_EIP; break;
607 case 9: reg = CV_REG_EFLAGS; break;
608 case 10: reg = CV_REG_CS; break;
609 case 11: reg = CV_REG_SS; break;
610 case 12: reg = CV_REG_DS; break;
611 case 13: reg = CV_REG_ES; break;
612 case 14: reg = CV_REG_FS; break;
613 case 15: reg = CV_REG_GS; break;
614 case 16: case 17: case 18: case 19:
615 case 20: case 21: case 22: case 23:
616 reg = CV_REG_ST0 + regno - 16; break;
617 case 24: reg = CV_REG_CTRL; break;
618 case 25: reg = CV_REG_STAT; break;
619 case 26: reg = CV_REG_TAG; break;
621 reg: fiseg 27
622 reg: fioff 28
623 reg: foseg 29
624 reg: fooff 30
625 reg: fop 31
627 case 32: case 33: case 34: case 35:
628 case 36: case 37: case 38: case 39:
629 reg = CV_REG_XMM0 + regno - 32; break;
630 case 40: reg = CV_REG_MXCSR; break;
631 default:
632 FIXME("Don't know how to map register %d\n", regno);
633 return 0;
635 return reg;
638 static enum location_error
639 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
640 HANDLE hproc, const struct location* frame)
642 unsigned long stack[64];
643 unsigned stk;
644 unsigned char op;
645 BOOL piece_found = FALSE;
647 stack[stk = 0] = 0;
649 loc->kind = loc_absolute;
650 loc->reg = Wine_DW_no_register;
652 while (ctx->data < ctx->end_data)
654 op = dwarf2_parse_byte(ctx);
655 switch (op)
657 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
658 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
659 case DW_OP_const1s: stack[++stk] = (long)(signed char)dwarf2_parse_byte(ctx); break;
660 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
661 case DW_OP_const2s: stack[++stk] = (long)(short)dwarf2_parse_u2(ctx); break;
662 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
663 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
664 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
665 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
666 case DW_OP_plus_uconst:
667 stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
668 case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
669 case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
670 case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
671 case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
672 case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
673 case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
674 case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
675 case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
676 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
677 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
678 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
679 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
680 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
681 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
682 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
683 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:
684 /* dbghelp APIs don't know how to cope with this anyway
685 * (for example 'long long' stored in two registers)
686 * FIXME: We should tell winedbg how to deal with it (sigh)
688 if (!piece_found)
690 if (loc->reg != Wine_DW_no_register)
691 FIXME("Only supporting one reg (%d -> %d)\n",
692 loc->reg, op - DW_OP_reg0);
693 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
695 if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
697 stack[++stk] = dwarf2_leb128_as_signed(ctx);
698 loc->kind = loc_regrel;
700 else
701 loc->kind = loc_register;
702 break;
703 case DW_OP_fbreg:
704 if (loc->reg != Wine_DW_no_register)
705 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
706 if (frame && frame->kind == loc_register)
708 loc->kind = loc_regrel;
709 loc->reg = frame->reg;
710 stack[++stk] = dwarf2_leb128_as_signed(ctx);
712 else if (frame && frame->kind == loc_regrel)
714 loc->kind = loc_regrel;
715 loc->reg = frame->reg;
716 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
718 else
720 /* FIXME: this could be later optimized by not recomputing
721 * this very location expression
723 loc->kind = loc_dwarf2_block;
724 stack[++stk] = dwarf2_leb128_as_signed(ctx);
726 break;
727 case DW_OP_piece:
729 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
730 WARN("Not handling OP_piece (size=%d)\n", sz);
731 piece_found = TRUE;
733 break;
734 case DW_OP_deref:
735 if (!stk)
737 FIXME("Unexpected empty stack\n");
738 return loc_err_internal;
740 if (loc->reg != Wine_DW_no_register)
742 WARN("Too complex expression for deref\n");
743 return loc_err_too_complex;
745 if (hproc)
747 DWORD addr = stack[stk--];
748 DWORD deref;
750 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
752 WARN("Couldn't read memory at %lx\n", addr);
753 return loc_err_cant_read;
755 stack[++stk] = deref;
757 else
759 loc->kind = loc_dwarf2_block;
761 break;
762 default:
763 FIXME("Unhandled attr op: %x\n", op);
764 return loc_err_internal;
767 loc->offset = stack[stk];
768 return 0;
771 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
772 dwarf2_debug_info_t* di,
773 unsigned long dw,
774 struct location* loc,
775 const struct location* frame)
777 struct attribute xloc;
779 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
781 switch (xloc.form)
783 case DW_FORM_data1: case DW_FORM_data2:
784 case DW_FORM_udata: case DW_FORM_sdata:
785 loc->kind = loc_absolute;
786 loc->reg = 0;
787 loc->offset = xloc.u.uvalue;
788 return TRUE;
789 case DW_FORM_data4: case DW_FORM_data8:
790 loc->kind = loc_dwarf2_location_list;
791 loc->reg = Wine_DW_no_register;
792 loc->offset = xloc.u.uvalue;
793 return TRUE;
796 /* assume we have a block form */
798 if (xloc.u.block.size)
800 dwarf2_traverse_context_t lctx;
801 enum location_error err;
803 lctx.data = xloc.u.block.ptr;
804 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
805 lctx.word_size = ctx->word_size;
807 err = compute_location(&lctx, loc, NULL, frame);
808 if (err < 0)
810 loc->kind = loc_error;
811 loc->reg = err;
813 else if (loc->kind == loc_dwarf2_block)
815 unsigned* ptr = pool_alloc(&ctx->module->pool,
816 sizeof(unsigned) + xloc.u.block.size);
817 *ptr = xloc.u.block.size;
818 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
819 loc->offset = (unsigned long)ptr;
822 return TRUE;
825 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
826 const dwarf2_debug_info_t* di)
828 struct attribute attr;
830 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
832 dwarf2_debug_info_t* type;
834 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
835 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
836 if (!type->symt)
838 /* load the debug info entity */
839 dwarf2_load_one_entry(ctx, type, NULL);
841 return type->symt;
843 return NULL;
846 /******************************************************************
847 * dwarf2_read_one_debug_info
849 * Loads into memory one debug info entry, and recursively its children (if any)
851 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
852 dwarf2_traverse_context_t* traverse,
853 dwarf2_debug_info_t** pdi)
855 const dwarf2_abbrev_entry_t*abbrev;
856 unsigned long entry_code;
857 unsigned long offset;
858 dwarf2_debug_info_t* di;
859 dwarf2_debug_info_t* child;
860 dwarf2_debug_info_t** where;
861 dwarf2_abbrev_entry_attr_t* attr;
862 unsigned i;
863 struct attribute sibling;
865 offset = traverse->data - ctx->sections[ctx->section].address;
866 entry_code = dwarf2_leb128_as_unsigned(traverse);
867 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
868 if (!entry_code)
870 *pdi = NULL;
871 return TRUE;
873 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
874 if (!abbrev)
876 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
877 return FALSE;
879 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
880 if (!di) return FALSE;
881 di->abbrev = abbrev;
882 di->symt = NULL;
884 if (abbrev->num_attr)
886 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
887 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
889 di->data[i] = traverse->data;
890 dwarf2_swallow_attribute(traverse, attr);
893 else di->data = NULL;
894 if (abbrev->have_child)
896 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
897 while (traverse->data < traverse->end_data)
899 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
900 if (!child) break;
901 where = vector_add(&di->children, &ctx->pool);
902 if (!where) return FALSE;
903 *where = child;
906 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
907 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
909 WARN("setting cursor for %s to next sibling <0x%lx>\n",
910 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
911 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
913 *pdi = di;
914 return TRUE;
917 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
918 dwarf2_debug_info_t* di)
920 struct attribute name;
921 struct attribute size;
922 struct attribute encoding;
923 enum BasicType bt;
925 if (di->symt) return di->symt;
927 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
929 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
930 name.u.string = NULL;
931 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
932 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
934 switch (encoding.u.uvalue)
936 case DW_ATE_void: bt = btVoid; break;
937 case DW_ATE_address: bt = btULong; break;
938 case DW_ATE_boolean: bt = btBool; break;
939 case DW_ATE_complex_float: bt = btComplex; break;
940 case DW_ATE_float: bt = btFloat; break;
941 case DW_ATE_signed: bt = btInt; break;
942 case DW_ATE_unsigned: bt = btUInt; break;
943 case DW_ATE_signed_char: bt = btChar; break;
944 case DW_ATE_unsigned_char: bt = btChar; break;
945 default: bt = btNoType; break;
947 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
948 if (di->abbrev->have_child) FIXME("Unsupported children\n");
949 return di->symt;
952 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
953 dwarf2_debug_info_t* di)
955 struct symt* ref_type;
956 struct attribute name;
958 if (di->symt) return di->symt;
960 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
962 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
963 ref_type = dwarf2_lookup_type(ctx, di);
965 if (name.u.string)
966 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
967 if (di->abbrev->have_child) FIXME("Unsupported children\n");
968 return di->symt;
971 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
972 dwarf2_debug_info_t* di)
974 struct symt* ref_type;
975 struct attribute size;
977 if (di->symt) return di->symt;
979 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
981 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
982 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
983 ref_type = &symt_new_basic(ctx->module, btVoid, "void", 0)->symt;
985 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
986 if (di->abbrev->have_child) FIXME("Unsupported children\n");
987 return di->symt;
990 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
991 dwarf2_debug_info_t* di)
993 struct symt* ref_type;
994 struct symt* idx_type = NULL;
995 struct attribute min, max, cnt;
996 dwarf2_debug_info_t** pchild = NULL;
997 dwarf2_debug_info_t* child;
999 if (di->symt) return di->symt;
1001 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1003 if (!di->abbrev->have_child)
1005 FIXME("array without range information\n");
1006 return NULL;
1008 ref_type = dwarf2_lookup_type(ctx, di);
1010 while ((pchild = vector_iter_up(&di->children, pchild)))
1012 child = *pchild;
1013 switch (child->abbrev->tag)
1015 case DW_TAG_subrange_type:
1016 idx_type = dwarf2_lookup_type(ctx, child);
1017 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1018 min.u.uvalue = 0;
1019 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1020 max.u.uvalue = 0;
1021 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1022 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1023 break;
1024 default:
1025 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1026 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1027 break;
1030 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1031 return di->symt;
1034 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1035 dwarf2_debug_info_t* di)
1037 struct symt* ref_type;
1039 if (di->symt) return di->symt;
1041 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1043 ref_type = dwarf2_lookup_type(ctx, di);
1044 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1045 di->symt = ref_type;
1047 return ref_type;
1050 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1051 dwarf2_debug_info_t* di)
1053 struct symt* ref_type;
1055 if (di->symt) return di->symt;
1057 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1059 ref_type = dwarf2_lookup_type(ctx, di);
1060 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1061 di->symt = ref_type;
1063 return ref_type;
1066 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1067 dwarf2_debug_info_t* di)
1069 struct symt* ref_type = NULL;
1071 if (di->symt) return di->symt;
1073 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1075 ref_type = dwarf2_lookup_type(ctx, di);
1076 /* FIXME: for now, we hard-wire C++ references to pointers */
1077 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1079 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1081 return di->symt;
1084 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1085 dwarf2_debug_info_t* di,
1086 struct symt_udt* parent)
1088 struct symt* elt_type;
1089 struct attribute name;
1090 struct attribute bit_size;
1091 struct attribute bit_offset;
1092 struct location loc;
1094 assert(parent);
1096 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1098 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1099 elt_type = dwarf2_lookup_type(ctx, di);
1100 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1102 if (loc.kind != loc_absolute)
1104 FIXME("Found register, while not expecting it\n");
1105 loc.offset = 0;
1107 else
1108 TRACE("found member_location at %s -> %lu\n",
1109 dwarf2_debug_ctx(ctx), loc.offset);
1111 else
1112 loc.offset = 0;
1113 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1114 bit_size.u.uvalue = 0;
1115 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1117 /* FIXME: we should only do this when implementation is LSB (which is
1118 * the case on i386 processors)
1120 struct attribute nbytes;
1121 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1123 DWORD64 size;
1124 nbytes.u.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
1126 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1128 else bit_offset.u.uvalue = 0;
1129 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1130 (loc.offset << 3) + bit_offset.u.uvalue,
1131 bit_size.u.uvalue);
1133 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1136 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1137 dwarf2_debug_info_t* di,
1138 enum UdtKind udt)
1140 struct attribute name;
1141 struct attribute size;
1143 if (di->symt) return di->symt;
1145 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1147 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1148 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1150 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1152 if (di->abbrev->have_child) /** any interest to not have child ? */
1154 dwarf2_debug_info_t** pchild = NULL;
1155 dwarf2_debug_info_t* child;
1157 while ((pchild = vector_iter_up(&di->children, pchild)))
1159 child = *pchild;
1161 switch (child->abbrev->tag)
1163 case DW_TAG_member:
1164 /* FIXME: should I follow the sibling stuff ?? */
1165 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1166 break;
1167 case DW_TAG_enumeration_type:
1168 dwarf2_parse_enumeration_type(ctx, child);
1169 break;
1170 case DW_TAG_structure_type:
1171 case DW_TAG_class_type:
1172 case DW_TAG_union_type:
1173 /* FIXME: we need to handle nested udt definitions */
1174 break;
1175 default:
1176 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1177 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1178 break;
1183 return di->symt;
1186 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1187 dwarf2_debug_info_t* di,
1188 struct symt_enum* parent)
1190 struct attribute name;
1191 struct attribute value;
1193 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1195 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1196 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1197 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1199 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1202 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1203 dwarf2_debug_info_t* di)
1205 struct attribute name;
1206 struct attribute size;
1208 if (di->symt) return di->symt;
1210 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1212 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1213 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1215 di->symt = &symt_new_enum(ctx->module, name.u.string)->symt;
1217 if (di->abbrev->have_child) /* any interest to not have child ? */
1219 dwarf2_debug_info_t** pchild = NULL;
1220 dwarf2_debug_info_t* child;
1222 /* FIXME: should we use the sibling stuff ?? */
1223 while ((pchild = vector_iter_up(&di->children, pchild)))
1225 child = *pchild;
1227 switch (child->abbrev->tag)
1229 case DW_TAG_enumerator:
1230 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1231 break;
1232 default:
1233 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1234 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1238 return di->symt;
1241 /* structure used to pass information around when parsing a subprogram */
1242 typedef struct dwarf2_subprogram_s
1244 dwarf2_parse_context_t* ctx;
1245 struct symt_compiland* compiland;
1246 struct symt_function* func;
1247 BOOL non_computed_variable;
1248 struct location frame;
1249 } dwarf2_subprogram_t;
1251 /******************************************************************
1252 * dwarf2_parse_variable
1254 * Parses any variable (parameter, local/global variable)
1256 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1257 struct symt_block* block,
1258 dwarf2_debug_info_t* di)
1260 struct symt* param_type;
1261 struct attribute name, value;
1262 struct location loc;
1263 BOOL is_pmt;
1265 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1267 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1268 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1270 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1271 name.u.string = NULL;
1272 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1273 &loc, &subpgm->frame))
1275 struct attribute ext;
1277 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1278 name.u.string, loc.kind, loc.offset, loc.reg,
1279 dwarf2_debug_ctx(subpgm->ctx));
1281 switch (loc.kind)
1283 case loc_absolute:
1284 /* it's a global variable */
1285 /* FIXME: we don't handle its scope yet */
1286 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1287 ext.u.uvalue = 0;
1288 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1289 name.u.string, !ext.u.uvalue,
1290 subpgm->ctx->module->module.BaseOfImage + loc.offset,
1291 0, param_type);
1292 break;
1293 default:
1294 subpgm->non_computed_variable = TRUE;
1295 /* fall through */
1296 case loc_register:
1297 case loc_regrel:
1298 /* either a pmt/variable relative to frame pointer or
1299 * pmt/variable in a register
1301 assert(subpgm->func);
1302 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1303 is_pmt ? DataIsParam : DataIsLocal,
1304 &loc, block, param_type, name.u.string);
1305 break;
1308 if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1310 FIXME("NIY: const value %08lx for %s\n", value.u.uvalue, name.u.string);
1312 if (is_pmt && subpgm->func && subpgm->func->type)
1313 symt_add_function_signature_parameter(subpgm->ctx->module,
1314 (struct symt_function_signature*)subpgm->func->type,
1315 param_type);
1317 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1320 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1321 dwarf2_debug_info_t* di)
1323 struct attribute name;
1324 struct attribute low_pc;
1325 struct location loc;
1327 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1329 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1330 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1331 name.u.string = NULL;
1333 loc.kind = loc_absolute;
1334 loc.offset = subpgm->ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1335 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1336 &loc, name.u.string);
1339 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1340 struct symt_block* parent_block,
1341 dwarf2_debug_info_t* di);
1343 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1344 struct symt_block* parent_block,
1345 dwarf2_debug_info_t* di)
1347 struct symt_block* block;
1348 struct attribute low_pc;
1349 struct attribute high_pc;
1351 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1353 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1354 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1356 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1357 subpgm->ctx->module->module.BaseOfImage + low_pc.u.uvalue - subpgm->func->address,
1358 high_pc.u.uvalue - low_pc.u.uvalue);
1360 if (di->abbrev->have_child) /** any interest to not have child ? */
1362 dwarf2_debug_info_t** pchild = NULL;
1363 dwarf2_debug_info_t* child;
1365 while ((pchild = vector_iter_up(&di->children, pchild)))
1367 child = *pchild;
1369 switch (child->abbrev->tag)
1371 case DW_TAG_formal_parameter:
1372 case DW_TAG_variable:
1373 dwarf2_parse_variable(subpgm, block, child);
1374 break;
1375 case DW_TAG_lexical_block:
1376 dwarf2_parse_subprogram_block(subpgm, block, child);
1377 break;
1378 case DW_TAG_inlined_subroutine:
1379 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1380 break;
1381 case DW_TAG_label:
1382 dwarf2_parse_subprogram_label(subpgm, child);
1383 break;
1384 default:
1385 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1386 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1387 dwarf2_debug_di(di));
1391 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1394 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1395 struct symt_block* parent_block,
1396 dwarf2_debug_info_t* di)
1398 struct symt_block* block;
1399 struct attribute low_pc;
1400 struct attribute high_pc;
1402 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1404 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1405 low_pc.u.uvalue = 0;
1406 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1407 high_pc.u.uvalue = 0;
1409 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1410 subpgm->ctx->module->module.BaseOfImage + low_pc.u.uvalue - subpgm->func->address,
1411 high_pc.u.uvalue - low_pc.u.uvalue);
1413 if (di->abbrev->have_child) /** any interest to not have child ? */
1415 dwarf2_debug_info_t** pchild = NULL;
1416 dwarf2_debug_info_t* child;
1418 while ((pchild = vector_iter_up(&di->children, pchild)))
1420 child = *pchild;
1422 switch (child->abbrev->tag)
1424 case DW_TAG_inlined_subroutine:
1425 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1426 break;
1427 case DW_TAG_variable:
1428 dwarf2_parse_variable(subpgm, block, child);
1429 break;
1430 case DW_TAG_lexical_block:
1431 dwarf2_parse_subprogram_block(subpgm, block, child);
1432 break;
1433 case DW_TAG_subprogram:
1434 /* FIXME: likely a declaration (to be checked)
1435 * skip it for now
1437 break;
1438 case DW_TAG_formal_parameter:
1439 /* FIXME: likely elements for exception handling (GCC flavor)
1440 * Skip it for now
1442 break;
1443 case DW_TAG_label:
1444 dwarf2_parse_subprogram_label(subpgm, child);
1445 break;
1446 case DW_TAG_class_type:
1447 case DW_TAG_structure_type:
1448 case DW_TAG_union_type:
1449 case DW_TAG_enumeration_type:
1450 case DW_TAG_typedef:
1451 /* the type referred to will be loaded when we need it, so skip it */
1452 break;
1453 default:
1454 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1455 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1460 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1463 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1464 dwarf2_debug_info_t* di,
1465 struct symt_compiland* compiland)
1467 struct attribute name;
1468 struct attribute low_pc;
1469 struct attribute high_pc;
1470 struct attribute is_decl;
1471 struct attribute inline_flags;
1472 struct symt* ret_type;
1473 struct symt_function_signature* sig_type;
1474 dwarf2_subprogram_t subpgm;
1476 if (di->symt) return di->symt;
1478 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1480 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1481 /* if it's an abstract representation of an inline function, there should be
1482 * a concrete object that we'll handle
1484 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1486 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1487 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1488 return NULL;
1491 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1492 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1493 /* As functions (defined as inline assembly) get debug info with dwarf
1494 * (not the case for stabs), we just drop Wine's thunks here...
1495 * Actual thunks will be created in elf_module from the symbol table
1497 if (elf_is_in_thunk_area(ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1498 ctx->thunks) >= 0)
1499 return NULL;
1500 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1501 is_decl.u.uvalue = 0;
1503 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1504 ret_type = &symt_new_basic(ctx->module, btVoid, "void", 0)->symt;
1506 /* FIXME: assuming C source code */
1507 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1508 if (!is_decl.u.uvalue)
1510 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1511 ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1512 high_pc.u.uvalue - low_pc.u.uvalue,
1513 &sig_type->symt);
1514 di->symt = &subpgm.func->symt;
1516 else subpgm.func = NULL;
1518 subpgm.ctx = ctx;
1519 subpgm.compiland = compiland;
1520 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1521 &subpgm.frame, NULL))
1523 /* on stack !! */
1524 subpgm.frame.kind = loc_regrel;
1525 subpgm.frame.reg = 0;
1526 subpgm.frame.offset = 0;
1528 subpgm.non_computed_variable = FALSE;
1530 if (di->abbrev->have_child) /** any interest to not have child ? */
1532 dwarf2_debug_info_t** pchild = NULL;
1533 dwarf2_debug_info_t* child;
1535 while ((pchild = vector_iter_up(&di->children, pchild)))
1537 child = *pchild;
1539 switch (child->abbrev->tag)
1541 case DW_TAG_variable:
1542 case DW_TAG_formal_parameter:
1543 dwarf2_parse_variable(&subpgm, NULL, child);
1544 break;
1545 case DW_TAG_lexical_block:
1546 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1547 break;
1548 case DW_TAG_inlined_subroutine:
1549 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1550 break;
1551 case DW_TAG_subprogram:
1552 /* FIXME: likely a declaration (to be checked)
1553 * skip it for now
1555 break;
1556 case DW_TAG_label:
1557 dwarf2_parse_subprogram_label(&subpgm, child);
1558 break;
1559 case DW_TAG_class_type:
1560 case DW_TAG_structure_type:
1561 case DW_TAG_union_type:
1562 case DW_TAG_enumeration_type:
1563 case DW_TAG_typedef:
1564 /* the type referred to will be loaded when we need it, so skip it */
1565 break;
1566 case DW_TAG_unspecified_parameters:
1567 /* FIXME: no support in dbghelp's internals so far */
1568 break;
1569 default:
1570 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1571 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1576 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1578 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1579 &subpgm.frame, NULL);
1581 symt_normalize_function(subpgm.ctx->module, subpgm.func);
1583 return di->symt;
1586 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1587 dwarf2_debug_info_t* di)
1589 struct symt* ret_type;
1590 struct symt_function_signature* sig_type;
1592 if (di->symt) return di->symt;
1594 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1596 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1597 ret_type = &symt_new_basic(ctx->module, btVoid, "void", 0)->symt;
1599 /* FIXME: assuming C source code */
1600 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1602 if (di->abbrev->have_child) /** any interest to not have child ? */
1604 dwarf2_debug_info_t** pchild = NULL;
1605 dwarf2_debug_info_t* child;
1607 while ((pchild = vector_iter_up(&di->children, pchild)))
1609 child = *pchild;
1611 switch (child->abbrev->tag)
1613 case DW_TAG_formal_parameter:
1614 symt_add_function_signature_parameter(ctx->module, sig_type,
1615 dwarf2_lookup_type(ctx, child));
1616 break;
1617 case DW_TAG_unspecified_parameters:
1618 WARN("Unsupported unspecified parameters\n");
1619 break;
1624 return di->symt = &sig_type->symt;
1627 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1628 dwarf2_debug_info_t* di,
1629 struct symt_compiland* compiland)
1631 switch (di->abbrev->tag)
1633 case DW_TAG_typedef:
1634 dwarf2_parse_typedef(ctx, di);
1635 break;
1636 case DW_TAG_base_type:
1637 dwarf2_parse_base_type(ctx, di);
1638 break;
1639 case DW_TAG_pointer_type:
1640 dwarf2_parse_pointer_type(ctx, di);
1641 break;
1642 case DW_TAG_class_type:
1643 dwarf2_parse_udt_type(ctx, di, UdtClass);
1644 break;
1645 case DW_TAG_structure_type:
1646 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1647 break;
1648 case DW_TAG_union_type:
1649 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1650 break;
1651 case DW_TAG_array_type:
1652 dwarf2_parse_array_type(ctx, di);
1653 break;
1654 case DW_TAG_const_type:
1655 dwarf2_parse_const_type(ctx, di);
1656 break;
1657 case DW_TAG_volatile_type:
1658 dwarf2_parse_volatile_type(ctx, di);
1659 break;
1660 case DW_TAG_reference_type:
1661 dwarf2_parse_reference_type(ctx, di);
1662 break;
1663 case DW_TAG_enumeration_type:
1664 dwarf2_parse_enumeration_type(ctx, di);
1665 break;
1666 case DW_TAG_subprogram:
1667 dwarf2_parse_subprogram(ctx, di, compiland);
1668 break;
1669 case DW_TAG_subroutine_type:
1670 dwarf2_parse_subroutine_type(ctx, di);
1671 break;
1672 case DW_TAG_variable:
1674 dwarf2_subprogram_t subpgm;
1676 subpgm.ctx = ctx;
1677 subpgm.compiland = compiland;
1678 subpgm.func = NULL;
1679 subpgm.frame.kind = loc_absolute;
1680 subpgm.frame.offset = 0;
1681 subpgm.frame.reg = Wine_DW_no_register;
1682 dwarf2_parse_variable(&subpgm, NULL, di);
1684 break;
1685 default:
1686 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1687 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1691 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1692 struct vector* v, unsigned file, unsigned line)
1694 struct symt_function* func;
1695 int idx;
1696 unsigned* psrc;
1698 if (!file || !(psrc = vector_at(v, file - 1))) return;
1700 TRACE("%s %lx %s %u\n", module->module.ModuleName, address, source_get(module, *psrc), line);
1701 if ((idx = symt_find_nearest(module, address)) == -1 ||
1702 module->addr_sorttab[idx]->symt.tag != SymTagFunction) return;
1703 func = (struct symt_function*)module->addr_sorttab[idx];
1704 symt_add_func_line(module, func, *psrc, line, address - func->address);
1707 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1708 dwarf2_parse_context_t* ctx,
1709 const char* compile_dir,
1710 unsigned long offset)
1712 dwarf2_traverse_context_t traverse;
1713 unsigned long length;
1714 unsigned version, header_len, insn_size, default_stmt;
1715 unsigned line_range, opcode_base;
1716 int line_base;
1717 const unsigned char* opcode_len;
1718 struct vector dirs;
1719 struct vector files;
1720 const char** p;
1722 /* section with line numbers stripped */
1723 if (sections[section_line].address == ELF_NO_MAP)
1724 return FALSE;
1726 traverse.data = sections[section_line].address + offset;
1727 traverse.start_data = traverse.data;
1728 traverse.end_data = traverse.data + 4;
1729 traverse.word_size = ctx->word_size;
1731 length = dwarf2_parse_u4(&traverse);
1732 traverse.end_data = traverse.start_data + length;
1734 version = dwarf2_parse_u2(&traverse);
1735 header_len = dwarf2_parse_u4(&traverse);
1736 insn_size = dwarf2_parse_byte(&traverse);
1737 default_stmt = dwarf2_parse_byte(&traverse);
1738 line_base = (signed char)dwarf2_parse_byte(&traverse);
1739 line_range = dwarf2_parse_byte(&traverse);
1740 opcode_base = dwarf2_parse_byte(&traverse);
1742 opcode_len = traverse.data;
1743 traverse.data += opcode_base - 1;
1745 vector_init(&dirs, sizeof(const char*), 4);
1746 p = vector_add(&dirs, &ctx->pool);
1747 *p = compile_dir ? compile_dir : ".";
1748 while (*traverse.data)
1750 const char* rel = (const char*)traverse.data;
1751 unsigned rellen = strlen(rel);
1752 TRACE("Got include %s\n", rel);
1753 traverse.data += rellen + 1;
1754 p = vector_add(&dirs, &ctx->pool);
1756 if (*rel == '/' || !compile_dir)
1757 *p = rel;
1758 else
1760 /* include directory relative to compile directory */
1761 unsigned baselen = strlen(compile_dir);
1762 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1763 strcpy(tmp, compile_dir);
1764 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1765 strcpy(&tmp[baselen], rel);
1766 *p = tmp;
1770 traverse.data++;
1772 vector_init(&files, sizeof(unsigned), 16);
1773 while (*traverse.data)
1775 unsigned int dir_index, mod_time, length;
1776 const char* name;
1777 const char* dir;
1778 unsigned* psrc;
1780 name = (const char*)traverse.data;
1781 traverse.data += strlen(name) + 1;
1782 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1783 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1784 length = dwarf2_leb128_as_unsigned(&traverse);
1785 dir = *(const char**)vector_at(&dirs, dir_index);
1786 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1787 psrc = vector_add(&files, &ctx->pool);
1788 *psrc = source_new(ctx->module, dir, name);
1790 traverse.data++;
1792 while (traverse.data < traverse.end_data)
1794 unsigned long address = 0;
1795 unsigned file = 1;
1796 unsigned line = 1;
1797 unsigned is_stmt = default_stmt;
1798 BOOL basic_block = FALSE, end_sequence = FALSE;
1799 unsigned opcode, extopcode, i;
1801 while (!end_sequence)
1803 opcode = dwarf2_parse_byte(&traverse);
1804 TRACE("Got opcode %x\n", opcode);
1806 if (opcode >= opcode_base)
1808 unsigned delta = opcode - opcode_base;
1810 address += (delta / line_range) * insn_size;
1811 line += line_base + (delta % line_range);
1812 basic_block = TRUE;
1813 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1815 else
1817 switch (opcode)
1819 case DW_LNS_copy:
1820 basic_block = FALSE;
1821 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1822 break;
1823 case DW_LNS_advance_pc:
1824 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1825 break;
1826 case DW_LNS_advance_line:
1827 line += dwarf2_leb128_as_signed(&traverse);
1828 break;
1829 case DW_LNS_set_file:
1830 file = dwarf2_leb128_as_unsigned(&traverse);
1831 break;
1832 case DW_LNS_set_column:
1833 dwarf2_leb128_as_unsigned(&traverse);
1834 break;
1835 case DW_LNS_negate_stmt:
1836 is_stmt = !is_stmt;
1837 break;
1838 case DW_LNS_set_basic_block:
1839 basic_block = 1;
1840 break;
1841 case DW_LNS_const_add_pc:
1842 address += ((255 - opcode_base) / line_range) * insn_size;
1843 break;
1844 case DW_LNS_fixed_advance_pc:
1845 address += dwarf2_parse_u2(&traverse);
1846 break;
1847 case DW_LNS_extended_op:
1848 dwarf2_leb128_as_unsigned(&traverse);
1849 extopcode = dwarf2_parse_byte(&traverse);
1850 switch (extopcode)
1852 case DW_LNE_end_sequence:
1853 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1854 end_sequence = TRUE;
1855 break;
1856 case DW_LNE_set_address:
1857 address = ctx->module->module.BaseOfImage + dwarf2_parse_addr(&traverse);
1858 break;
1859 case DW_LNE_define_file:
1860 FIXME("not handled %s\n", traverse.data);
1861 traverse.data += strlen((const char *)traverse.data) + 1;
1862 dwarf2_leb128_as_unsigned(&traverse);
1863 dwarf2_leb128_as_unsigned(&traverse);
1864 dwarf2_leb128_as_unsigned(&traverse);
1865 break;
1866 default:
1867 FIXME("Unsupported extended opcode %x\n", extopcode);
1868 break;
1870 break;
1871 default:
1872 WARN("Unsupported opcode %x\n", opcode);
1873 for (i = 0; i < opcode_len[opcode]; i++)
1874 dwarf2_leb128_as_unsigned(&traverse);
1875 break;
1880 return TRUE;
1883 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1884 const dwarf2_comp_unit_t* comp_unit,
1885 struct module* module,
1886 const struct elf_thunk_area* thunks,
1887 const unsigned char* comp_unit_cursor)
1889 dwarf2_parse_context_t ctx;
1890 dwarf2_traverse_context_t traverse;
1891 dwarf2_traverse_context_t abbrev_ctx;
1892 dwarf2_debug_info_t* di;
1893 BOOL ret = FALSE;
1895 TRACE("Compilation Unit Header found at 0x%x:\n",
1896 comp_unit_cursor - sections[section_debug].address);
1897 TRACE("- length: %lu\n", comp_unit->length);
1898 TRACE("- version: %u\n", comp_unit->version);
1899 TRACE("- abbrev_offset: %lu\n", comp_unit->abbrev_offset);
1900 TRACE("- word_size: %u\n", comp_unit->word_size);
1902 if (comp_unit->version != 2)
1904 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
1905 comp_unit->version);
1906 return FALSE;
1909 pool_init(&ctx.pool, 65536);
1910 ctx.sections = sections;
1911 ctx.section = section_debug;
1912 ctx.module = module;
1913 ctx.word_size = comp_unit->word_size;
1914 ctx.thunks = thunks;
1915 ctx.ref_offset = comp_unit_cursor - sections[section_debug].address;
1917 traverse.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
1918 traverse.data = traverse.start_data;
1919 traverse.word_size = comp_unit->word_size;
1920 traverse.end_data = comp_unit_cursor + comp_unit->length + sizeof(unsigned);
1922 abbrev_ctx.start_data = sections[section_abbrev].address + comp_unit->abbrev_offset;
1923 abbrev_ctx.data = abbrev_ctx.start_data;
1924 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
1925 abbrev_ctx.word_size = comp_unit->word_size;
1926 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
1928 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
1929 dwarf2_read_one_debug_info(&ctx, &traverse, &di);
1931 if (di->abbrev->tag == DW_TAG_compile_unit)
1933 struct attribute name;
1934 dwarf2_debug_info_t** pdi = NULL;
1935 struct attribute stmt_list, low_pc;
1936 struct attribute comp_dir;
1938 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
1939 name.u.string = NULL;
1941 /* get working directory of current compilation unit */
1942 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
1943 comp_dir.u.string = NULL;
1945 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
1946 low_pc.u.uvalue = 0;
1947 di->symt = &symt_new_compiland(module,
1948 module->module.BaseOfImage + low_pc.u.uvalue,
1949 source_new(module, comp_dir.u.string, name.u.string))->symt;
1951 if (di->abbrev->have_child)
1953 while ((pdi = vector_iter_up(&di->children, pdi)))
1955 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
1958 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
1960 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
1961 module->module.LineNumbers = TRUE;
1963 ret = TRUE;
1965 else FIXME("Should have a compilation unit here\n");
1966 pool_destroy(&ctx.pool);
1967 return ret;
1970 static BOOL dwarf2_lookup_loclist(const struct module* module, const BYTE* start,
1971 unsigned long ip,
1972 dwarf2_traverse_context_t* lctx)
1974 DWORD beg, end;
1975 const BYTE* ptr = start;
1976 DWORD len;
1978 while (ptr < module->dwarf2_info->debug_loc.address + module->dwarf2_info->debug_loc.size)
1980 beg = dwarf2_get_u4(ptr); ptr += 4;
1981 end = dwarf2_get_u4(ptr); ptr += 4;
1982 if (!beg && !end) break;
1983 len = dwarf2_get_u2(ptr); ptr += 2;
1985 if (beg <= ip && ip < end)
1987 lctx->data = ptr;
1988 lctx->end_data = ptr + len;
1989 lctx->word_size = 4; /* FIXME word size !!! */
1990 return TRUE;
1992 ptr += len;
1994 WARN("Couldn't find ip in location list\n");
1995 return FALSE;
1998 static enum location_error loc_compute_frame(struct process* pcs,
1999 const struct module* module,
2000 const struct symt_function* func,
2001 DWORD ip, struct location* frame)
2003 struct symt** psym = NULL;
2004 struct location* pframe;
2005 dwarf2_traverse_context_t lctx;
2006 enum location_error err;
2008 while ((psym = vector_iter_up(&func->vchildren, psym)))
2010 if ((*psym)->tag == SymTagCustom)
2012 pframe = &((struct symt_function_point*)*psym)->loc;
2014 /* First, recompute the frame information, if needed */
2015 switch (pframe->kind)
2017 case loc_regrel:
2018 case loc_register:
2019 *frame = *pframe;
2020 break;
2021 case loc_dwarf2_location_list:
2022 WARN("Searching loclist for %s\n", func->hash_elt.name);
2023 if (!dwarf2_lookup_loclist(module,
2024 module->dwarf2_info->debug_loc.address + pframe->offset,
2025 ip, &lctx))
2026 return loc_err_out_of_scope;
2027 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2028 if (frame->kind >= loc_user)
2030 WARN("Couldn't compute runtime frame location\n");
2031 return loc_err_too_complex;
2033 break;
2034 default:
2035 WARN("Unsupported frame kind %d\n", pframe->kind);
2036 return loc_err_internal;
2038 return 0;
2041 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2042 return loc_err_internal;
2045 static void dwarf2_location_compute(struct process* pcs,
2046 const struct module* module,
2047 const struct symt_function* func,
2048 struct location* loc)
2050 struct location frame;
2051 DWORD ip;
2052 int err;
2053 dwarf2_traverse_context_t lctx;
2055 if (!func->container || func->container->tag != SymTagCompiland)
2057 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2058 err = loc_err_internal;
2060 else
2062 /* instruction pointer relative to compiland's start */
2063 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2065 if ((err = loc_compute_frame(pcs, module, func, ip, &frame)) == 0)
2067 switch (loc->kind)
2069 case loc_dwarf2_location_list:
2070 /* Then, if the variable has a location list, find it !! */
2071 if (dwarf2_lookup_loclist(module,
2072 module->dwarf2_info->debug_loc.address + loc->offset,
2073 ip, &lctx))
2074 goto do_compute;
2075 err = loc_err_out_of_scope;
2076 break;
2077 case loc_dwarf2_block:
2078 /* or if we have a copy of an existing block, get ready for it */
2080 unsigned* ptr = (unsigned*)loc->offset;
2082 lctx.data = (const BYTE*)(ptr + 1);
2083 lctx.end_data = lctx.data + *ptr;
2084 lctx.word_size = 4; /* FIXME !! */
2086 do_compute:
2087 /* now get the variable */
2088 err = compute_location(&lctx, loc, pcs->handle, &frame);
2089 break;
2090 case loc_register:
2091 case loc_regrel:
2092 /* nothing to do */
2093 break;
2094 default:
2095 WARN("Unsupported local kind %d\n", loc->kind);
2096 err = loc_err_internal;
2100 if (err < 0)
2102 loc->kind = loc_register;
2103 loc->reg = err;
2107 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2108 const struct elf_thunk_area* thunks,
2109 const unsigned char* debug, unsigned int debug_size,
2110 const unsigned char* abbrev, unsigned int abbrev_size,
2111 const unsigned char* str, unsigned int str_size,
2112 const unsigned char* line, unsigned int line_size,
2113 const unsigned char* loclist, unsigned int loclist_size)
2115 dwarf2_section_t section[section_max];
2116 unsigned char* ptr;
2117 const unsigned char*comp_unit_cursor = debug;
2118 const unsigned char*end_debug = debug + debug_size;
2120 module->loc_compute = dwarf2_location_compute;
2122 section[section_debug].address = debug;
2123 section[section_debug].size = debug_size;
2124 section[section_abbrev].address = abbrev;
2125 section[section_abbrev].size = abbrev_size;
2126 section[section_string].address = str;
2127 section[section_string].size = str_size;
2128 section[section_line].address = line;
2129 section[section_line].size = line_size;
2131 if (loclist_size)
2133 /* initialize the dwarf2 specific info block for this module.
2134 * As we'll need later on the .debug_loc section content, we copy it in
2135 * the module structure for later reuse
2137 module->dwarf2_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*module->dwarf2_info) + loclist_size);
2138 if (!module->dwarf2_info) return FALSE;
2139 ptr = (unsigned char*)(module->dwarf2_info + 1);
2140 memcpy(ptr, loclist, loclist_size);
2141 module->dwarf2_info->debug_loc.address = ptr;
2142 module->dwarf2_info->debug_loc.size = loclist_size;
2145 while (comp_unit_cursor < end_debug)
2147 const dwarf2_comp_unit_stream_t* comp_unit_stream;
2148 dwarf2_comp_unit_t comp_unit;
2150 comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
2151 comp_unit.length = *(unsigned long*) comp_unit_stream->length;
2152 comp_unit.version = *(unsigned short*) comp_unit_stream->version;
2153 comp_unit.abbrev_offset = *(unsigned long*) comp_unit_stream->abbrev_offset;
2154 comp_unit.word_size = *(unsigned char*) comp_unit_stream->word_size;
2156 dwarf2_parse_compilation_unit(section, &comp_unit, module,
2157 thunks, comp_unit_cursor);
2158 comp_unit_cursor += comp_unit.length + sizeof(unsigned);
2160 module->module.SymType = SymDia;
2161 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2162 /* FIXME: we could have a finer grain here */
2163 module->module.GlobalSymbols = TRUE;
2164 module->module.TypeInfo = TRUE;
2165 module->module.SourceIndexed = TRUE;
2166 module->module.Publics = TRUE;
2167 return TRUE;