push 87b6981010d7405c33b14cddcceec21b47729eba
[wine/hacks.git] / dlls / dbghelp / dwarf.c
blob433dca88ff44911e57881cac1ab62b76cb015a8e
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #define NONAMELESSUNION
24 #include "config.h"
26 #include <sys/types.h>
27 #include <fcntl.h>
28 #ifdef HAVE_SYS_STAT_H
29 # include <sys/stat.h>
30 #endif
31 #ifdef HAVE_SYS_MMAN_H
32 #include <sys/mman.h>
33 #endif
34 #include <limits.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #ifdef HAVE_UNISTD_H
38 # include <unistd.h>
39 #endif
40 #include <stdio.h>
41 #ifndef PATH_MAX
42 #define PATH_MAX MAX_PATH
43 #endif
44 #include <assert.h>
45 #include <stdarg.h>
47 #include "windef.h"
48 #include "winbase.h"
49 #include "winuser.h"
50 #include "ole2.h"
51 #include "oleauto.h"
53 #include "dbghelp_private.h"
55 #include "wine/debug.h"
57 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
59 /* FIXME:
60 * - Functions:
61 * o unspecified parameters
62 * o inlined functions
63 * o Debug{Start|End}Point
64 * o CFA
65 * - Udt
66 * o proper types loading (nesting)
69 #if 0
70 static void dump(const void* ptr, unsigned len)
72 int i, j;
73 BYTE msg[128];
74 static const char hexof[] = "0123456789abcdef";
75 const BYTE* x = ptr;
77 for (i = 0; i < len; i += 16)
79 sprintf(msg, "%08x: ", i);
80 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
81 for (j = 0; j < min(16, len - i); j++)
83 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
84 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
85 msg[10 + 3 * j + 2] = ' ';
86 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
87 x[i + j] : '.';
89 msg[10 + 3 * 16] = ' ';
90 msg[10 + 3 * 16 + 1 + 16] = '\0';
91 TRACE("%s\n", msg);
94 #endif
96 /**
98 * Main Specs:
99 * http://www.eagercon.com/dwarf/dwarf3std.htm
100 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
102 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
104 * example of projects who do dwarf2 parsing:
105 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
106 * http://elis.ugent.be/diota/log/ltrace_elf.c
108 #include "dwarf.h"
111 * Parsers
114 typedef struct dwarf2_abbrev_entry_attr_s
116 unsigned long attribute;
117 unsigned long form;
118 struct dwarf2_abbrev_entry_attr_s* next;
119 } dwarf2_abbrev_entry_attr_t;
121 typedef struct dwarf2_abbrev_entry_s
123 unsigned long entry_code;
124 unsigned long tag;
125 unsigned char have_child;
126 unsigned num_attr;
127 dwarf2_abbrev_entry_attr_t* attrs;
128 } dwarf2_abbrev_entry_t;
130 struct dwarf2_block
132 unsigned size;
133 const unsigned char* ptr;
136 struct attribute
138 unsigned long form;
139 union
141 unsigned long uvalue;
142 long svalue;
143 const char* string;
144 struct dwarf2_block block;
145 } u;
148 typedef struct dwarf2_debug_info_s
150 const dwarf2_abbrev_entry_t*abbrev;
151 struct symt* symt;
152 const unsigned char** data;
153 struct vector children;
154 } dwarf2_debug_info_t;
156 typedef struct dwarf2_section_s
158 const unsigned char* address;
159 unsigned size;
160 } dwarf2_section_t;
162 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
164 typedef struct dwarf2_traverse_context_s
166 const unsigned char* data;
167 const unsigned char* start_data;
168 const unsigned char* end_data;
169 unsigned char word_size;
170 } dwarf2_traverse_context_t;
172 /* symt_cache indexes */
173 #define sc_void 0
174 #define sc_int1 1
175 #define sc_int2 2
176 #define sc_int4 3
177 #define sc_num 4
179 typedef struct dwarf2_parse_context_s
181 const dwarf2_section_t* sections;
182 unsigned section;
183 struct pool pool;
184 struct module* module;
185 const struct elf_thunk_area*thunks;
186 struct sparse_array abbrev_table;
187 struct sparse_array debug_info_table;
188 unsigned long load_offset;
189 unsigned long ref_offset;
190 unsigned char word_size;
191 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
192 } dwarf2_parse_context_t;
194 /* stored in the dbghelp's module internal structure for later reuse */
195 struct dwarf2_module_info_s
197 dwarf2_section_t debug_loc;
200 #define loc_dwarf2_location_list (loc_user + 0)
201 #define loc_dwarf2_block (loc_user + 1)
203 /* forward declarations */
204 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
206 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
208 return *ptr;
211 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
213 unsigned char uvalue = dwarf2_get_byte(ctx->data);
214 ctx->data += 1;
215 return uvalue;
218 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
220 return *(const UINT16*)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 UINT32*)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 DWORD64 dwarf2_get_u8(const unsigned char* ptr)
244 return *(const UINT64*)ptr;
247 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
249 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
250 ctx->data += 8;
251 return uvalue;
254 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
256 unsigned long ret = 0;
257 unsigned char byte;
258 unsigned shift = 0;
262 byte = dwarf2_get_byte(ptr++);
263 ret |= (byte & 0x7f) << shift;
264 shift += 7;
265 } while (byte & 0x80);
267 if (end) *end = ptr;
268 return ret;
271 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
273 unsigned long ret;
275 assert(ctx);
277 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
279 return ret;
282 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
284 long ret = 0;
285 unsigned char byte;
286 unsigned shift = 0;
287 const unsigned size = sizeof(int) * 8;
291 byte = dwarf2_get_byte(ptr++);
292 ret |= (byte & 0x7f) << shift;
293 shift += 7;
294 } while (byte & 0x80);
295 if (end) *end = ptr;
297 /* as spec: sign bit of byte is 2nd high order bit (80x40)
298 * -> 0x80 is used as flag.
300 if ((shift < size) && (byte & 0x40))
302 ret |= - (1 << shift);
304 return ret;
307 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
309 long ret = 0;
311 assert(ctx);
313 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
314 return ret;
317 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
319 unsigned ret;
320 for (ret = 0; ctx->data[ret] & 0x80; ret++);
321 return ret + 1;
324 /******************************************************************
325 * dwarf2_get_addr
327 * Returns an address.
328 * We assume that in all cases word size from Dwarf matches the size of
329 * addresses in platform where the exec is compiled.
331 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
333 unsigned long ret;
335 switch (word_size)
337 case 4:
338 ret = dwarf2_get_u4(ptr);
339 break;
340 case 8:
341 ret = dwarf2_get_u8(ptr);
342 break;
343 default:
344 FIXME("Unsupported Word Size %u\n", word_size);
345 ret = 0;
347 return ret;
350 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
352 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
353 ctx->data += ctx->word_size;
354 return ret;
357 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
359 return wine_dbg_sprintf("ctx(%p)", ctx->data);
362 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
364 return wine_dbg_sprintf("ctx(%p,%s)",
365 ctx, debugstr_w(ctx->module->module.ModuleName));
368 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
370 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
371 di->abbrev, di->symt);
374 static dwarf2_abbrev_entry_t*
375 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
376 unsigned long entry_code)
378 assert( NULL != abbrev_table );
379 return sparse_array_find(abbrev_table, entry_code);
382 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
383 struct sparse_array* abbrev_table,
384 struct pool* pool)
386 unsigned long entry_code;
387 dwarf2_abbrev_entry_t* abbrev_entry;
388 dwarf2_abbrev_entry_attr_t* new = NULL;
389 dwarf2_abbrev_entry_attr_t* last = NULL;
390 unsigned long attribute;
391 unsigned long form;
393 assert( NULL != abbrev_ctx );
395 TRACE("%s, end at %p\n",
396 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
398 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
399 while (abbrev_ctx->data < abbrev_ctx->end_data)
401 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
402 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
403 TRACE("found entry_code %lu\n", entry_code);
404 if (!entry_code)
406 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
407 break;
409 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
410 assert( NULL != abbrev_entry );
412 abbrev_entry->entry_code = entry_code;
413 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
414 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
415 abbrev_entry->attrs = NULL;
416 abbrev_entry->num_attr = 0;
418 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
419 abbrev_table, sparse_array_length(abbrev_table),
420 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
422 last = NULL;
423 while (1)
425 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
426 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
427 if (!attribute) break;
429 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
430 assert(new);
432 new->attribute = attribute;
433 new->form = form;
434 new->next = NULL;
435 if (abbrev_entry->attrs) last->next = new;
436 else abbrev_entry->attrs = new;
437 last = new;
438 abbrev_entry->num_attr++;
441 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
444 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
445 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
447 unsigned step;
449 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
451 switch (abbrev_attr->form)
453 case DW_FORM_ref_addr:
454 case DW_FORM_addr: step = ctx->word_size; break;
455 case DW_FORM_flag:
456 case DW_FORM_data1:
457 case DW_FORM_ref1: step = 1; break;
458 case DW_FORM_data2:
459 case DW_FORM_ref2: step = 2; break;
460 case DW_FORM_data4:
461 case DW_FORM_ref4:
462 case DW_FORM_strp: step = 4; break;
463 case DW_FORM_data8:
464 case DW_FORM_ref8: step = 8; break;
465 case DW_FORM_sdata:
466 case DW_FORM_ref_udata:
467 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
468 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
469 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
470 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
471 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
472 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
473 default:
474 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
475 return;
477 ctx->data += step;
480 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
481 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
482 const unsigned char* data,
483 struct attribute* attr)
485 attr->form = abbrev_attr->form;
486 switch (attr->form)
488 case DW_FORM_ref_addr:
489 case DW_FORM_addr:
490 attr->u.uvalue = dwarf2_get_addr(data, ctx->word_size);
491 TRACE("addr<0x%lx>\n", attr->u.uvalue);
492 break;
494 case DW_FORM_flag:
495 attr->u.uvalue = dwarf2_get_byte(data);
496 TRACE("flag<0x%lx>\n", attr->u.uvalue);
497 break;
499 case DW_FORM_data1:
500 attr->u.uvalue = dwarf2_get_byte(data);
501 TRACE("data1<%lu>\n", attr->u.uvalue);
502 break;
504 case DW_FORM_data2:
505 attr->u.uvalue = dwarf2_get_u2(data);
506 TRACE("data2<%lu>\n", attr->u.uvalue);
507 break;
509 case DW_FORM_data4:
510 attr->u.uvalue = dwarf2_get_u4(data);
511 TRACE("data4<%lu>\n", attr->u.uvalue);
512 break;
514 case DW_FORM_data8:
515 attr->u.block.size = 8;
516 attr->u.block.ptr = data;
517 data += 8;
518 break;
520 case DW_FORM_ref1:
521 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
522 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
523 break;
525 case DW_FORM_ref2:
526 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
527 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
528 break;
530 case DW_FORM_ref4:
531 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
532 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
533 break;
535 case DW_FORM_ref8:
536 FIXME("Unhandled 64 bit support\n");
537 break;
539 case DW_FORM_sdata:
540 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
541 break;
543 case DW_FORM_ref_udata:
544 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
545 break;
547 case DW_FORM_udata:
548 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
549 break;
551 case DW_FORM_string:
552 attr->u.string = (const char *)data;
553 TRACE("string<%s>\n", attr->u.string);
554 break;
556 case DW_FORM_strp:
558 unsigned long offset = dwarf2_get_u4(data);
559 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
561 TRACE("strp<%s>\n", attr->u.string);
562 break;
564 case DW_FORM_block:
565 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
566 break;
568 case DW_FORM_block1:
569 attr->u.block.size = dwarf2_get_byte(data);
570 attr->u.block.ptr = data + 1;
571 break;
573 case DW_FORM_block2:
574 attr->u.block.size = dwarf2_get_u2(data);
575 attr->u.block.ptr = data + 2;
576 break;
578 case DW_FORM_block4:
579 attr->u.block.size = dwarf2_get_u4(data);
580 attr->u.block.ptr = data + 4;
581 break;
583 default:
584 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
585 break;
589 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
590 const dwarf2_debug_info_t* di,
591 unsigned at, struct attribute* attr)
593 unsigned i, ai = 0;
594 dwarf2_abbrev_entry_attr_t* abbrev_attr;
595 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
597 while (di)
599 abstract_abbrev_attr = NULL;
600 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
602 if (abbrev_attr->attribute == at)
604 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
605 return TRUE;
607 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
608 at != DW_AT_sibling)
610 abstract_abbrev_attr = abbrev_attr;
611 ai = i;
614 /* do we have an abstract origin debug entry to look into ? */
615 if (!abstract_abbrev_attr) break;
616 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
617 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
618 FIXME("Should have found the debug info entry\n");
620 return FALSE;
623 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
624 struct symt_compiland*);
626 #define Wine_DW_no_register 0x7FFFFFFF
628 static unsigned dwarf2_map_register(int regno)
630 unsigned reg;
632 switch (regno)
634 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
635 case 0: reg = CV_REG_EAX; break;
636 case 1: reg = CV_REG_ECX; break;
637 case 2: reg = CV_REG_EDX; break;
638 case 3: reg = CV_REG_EBX; break;
639 case 4: reg = CV_REG_ESP; break;
640 case 5: reg = CV_REG_EBP; break;
641 case 6: reg = CV_REG_ESI; break;
642 case 7: reg = CV_REG_EDI; break;
643 case 8: reg = CV_REG_EIP; break;
644 case 9: reg = CV_REG_EFLAGS; break;
645 case 10: reg = CV_REG_CS; break;
646 case 11: reg = CV_REG_SS; break;
647 case 12: reg = CV_REG_DS; break;
648 case 13: reg = CV_REG_ES; break;
649 case 14: reg = CV_REG_FS; break;
650 case 15: reg = CV_REG_GS; break;
651 case 16: case 17: case 18: case 19:
652 case 20: case 21: case 22: case 23:
653 reg = CV_REG_ST0 + regno - 16; break;
654 case 24: reg = CV_REG_CTRL; break;
655 case 25: reg = CV_REG_STAT; break;
656 case 26: reg = CV_REG_TAG; break;
658 reg: fiseg 27
659 reg: fioff 28
660 reg: foseg 29
661 reg: fooff 30
662 reg: fop 31
664 case 32: case 33: case 34: case 35:
665 case 36: case 37: case 38: case 39:
666 reg = CV_REG_XMM0 + regno - 32; break;
667 case 40: reg = CV_REG_MXCSR; break;
668 default:
669 FIXME("Don't know how to map register %d\n", regno);
670 return 0;
672 return reg;
675 static enum location_error
676 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
677 HANDLE hproc, const struct location* frame)
679 DWORD_PTR tmp, stack[64];
680 unsigned stk;
681 unsigned char op;
682 BOOL piece_found = FALSE;
684 stack[stk = 0] = 0;
686 loc->kind = loc_absolute;
687 loc->reg = Wine_DW_no_register;
689 while (ctx->data < ctx->end_data)
691 op = dwarf2_parse_byte(ctx);
693 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
694 stack[++stk] = op - DW_OP_lit0;
695 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
697 /* dbghelp APIs don't know how to cope with this anyway
698 * (for example 'long long' stored in two registers)
699 * FIXME: We should tell winedbg how to deal with it (sigh)
701 if (!piece_found)
703 if (loc->reg != Wine_DW_no_register)
704 FIXME("Only supporting one reg (%d -> %d)\n",
705 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
706 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
708 loc->kind = loc_register;
710 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
712 /* dbghelp APIs don't know how to cope with this anyway
713 * (for example 'long long' stored in two registers)
714 * FIXME: We should tell winedbg how to deal with it (sigh)
716 if (!piece_found)
718 if (loc->reg != Wine_DW_no_register)
719 FIXME("Only supporting one breg (%d -> %d)\n",
720 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
721 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
723 stack[++stk] = dwarf2_leb128_as_signed(ctx);
724 loc->kind = loc_regrel;
725 break;
727 else switch (op)
729 case DW_OP_nop: break;
730 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
731 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
732 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
733 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
734 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
735 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
736 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
737 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
738 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
739 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
740 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
741 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
742 case DW_OP_drop: stk--; break;
743 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
744 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
745 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
746 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
747 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
748 case DW_OP_neg: stack[stk] = -stack[stk]; break;
749 case DW_OP_not: stack[stk] = ~stack[stk]; break;
750 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
751 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
752 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
753 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
754 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
755 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
756 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
757 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
758 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
759 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
760 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
761 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
762 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
763 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
764 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
765 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
766 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
767 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
768 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
769 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
770 case DW_OP_regx:
771 if (loc->reg != Wine_DW_no_register)
772 FIXME("Only supporting one regx\n");
773 loc->reg = dwarf2_map_register(dwarf2_leb128_as_unsigned(ctx));
774 loc->kind = loc_register;
775 break;
776 case DW_OP_bregx:
777 tmp = dwarf2_leb128_as_unsigned(ctx);
778 ctx->data++;
779 if (loc->reg != Wine_DW_no_register)
780 FIXME("Only supporting one regx\n");
781 loc->reg = dwarf2_map_register(tmp) + dwarf2_leb128_as_signed(ctx);
782 loc->kind = loc_register;
783 break;
784 case DW_OP_fbreg:
785 if (loc->reg != Wine_DW_no_register)
786 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
787 if (frame && frame->kind == loc_register)
789 loc->kind = loc_regrel;
790 loc->reg = frame->reg;
791 stack[++stk] = dwarf2_leb128_as_signed(ctx);
793 else if (frame && frame->kind == loc_regrel)
795 loc->kind = loc_regrel;
796 loc->reg = frame->reg;
797 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
799 else
801 /* FIXME: this could be later optimized by not recomputing
802 * this very location expression
804 loc->kind = loc_dwarf2_block;
805 stack[++stk] = dwarf2_leb128_as_signed(ctx);
807 break;
808 case DW_OP_piece:
810 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
811 WARN("Not handling OP_piece (size=%d)\n", sz);
812 piece_found = TRUE;
814 break;
815 case DW_OP_deref:
816 if (!stk)
818 FIXME("Unexpected empty stack\n");
819 return loc_err_internal;
821 if (loc->reg != Wine_DW_no_register)
823 WARN("Too complex expression for deref\n");
824 return loc_err_too_complex;
826 if (hproc)
828 DWORD_PTR addr = stack[stk--];
829 DWORD_PTR deref;
831 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
833 WARN("Couldn't read memory at %lx\n", addr);
834 return loc_err_cant_read;
836 stack[++stk] = deref;
838 else
840 loc->kind = loc_dwarf2_block;
842 break;
843 case DW_OP_deref_size:
844 if (!stk)
846 FIXME("Unexpected empty stack\n");
847 return loc_err_internal;
849 if (loc->reg != Wine_DW_no_register)
851 WARN("Too complex expression for deref\n");
852 return loc_err_too_complex;
854 if (hproc)
856 DWORD_PTR addr = stack[stk--];
857 BYTE derefsize = dwarf2_parse_byte(ctx);
858 DWORD64 deref;
860 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
862 WARN("Couldn't read memory at %lx\n", addr);
863 return loc_err_cant_read;
866 switch (derefsize)
868 case 1: stack[++stk] = *(unsigned char*)&deref; break;
869 case 2: stack[++stk] = *(unsigned short*)&deref; break;
870 case 4: stack[++stk] = *(DWORD*)&deref; break;
871 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
874 else
876 loc->kind = loc_dwarf2_block;
878 break;
879 default:
880 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
881 FIXME("Unhandled attr op: %x\n", op);
882 /* FIXME else unhandled extension */
883 return loc_err_internal;
886 loc->offset = stack[stk];
887 return 0;
890 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
891 const dwarf2_debug_info_t* di,
892 unsigned long dw,
893 struct location* loc,
894 const struct location* frame)
896 struct attribute xloc;
898 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
900 switch (xloc.form)
902 case DW_FORM_data1: case DW_FORM_data2:
903 case DW_FORM_udata: case DW_FORM_sdata:
904 loc->kind = loc_absolute;
905 loc->reg = 0;
906 loc->offset = xloc.u.uvalue;
907 return TRUE;
908 case DW_FORM_data4: case DW_FORM_data8:
909 loc->kind = loc_dwarf2_location_list;
910 loc->reg = Wine_DW_no_register;
911 loc->offset = xloc.u.uvalue;
912 return TRUE;
915 /* assume we have a block form */
917 if (xloc.u.block.size)
919 dwarf2_traverse_context_t lctx;
920 enum location_error err;
922 lctx.data = xloc.u.block.ptr;
923 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
924 lctx.word_size = ctx->word_size;
926 err = compute_location(&lctx, loc, NULL, frame);
927 if (err < 0)
929 loc->kind = loc_error;
930 loc->reg = err;
932 else if (loc->kind == loc_dwarf2_block)
934 unsigned* ptr = pool_alloc(&ctx->module->pool,
935 sizeof(unsigned) + xloc.u.block.size);
936 *ptr = xloc.u.block.size;
937 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
938 loc->offset = (unsigned long)ptr;
941 return TRUE;
944 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
945 const dwarf2_debug_info_t* di)
947 struct attribute attr;
949 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
951 dwarf2_debug_info_t* type;
953 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
954 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
955 if (!type->symt)
957 /* load the debug info entity */
958 dwarf2_load_one_entry(ctx, type, NULL);
960 return type->symt;
962 return NULL;
965 /******************************************************************
966 * dwarf2_read_one_debug_info
968 * Loads into memory one debug info entry, and recursively its children (if any)
970 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
971 dwarf2_traverse_context_t* traverse,
972 dwarf2_debug_info_t** pdi)
974 const dwarf2_abbrev_entry_t*abbrev;
975 unsigned long entry_code;
976 unsigned long offset;
977 dwarf2_debug_info_t* di;
978 dwarf2_debug_info_t* child;
979 dwarf2_debug_info_t** where;
980 dwarf2_abbrev_entry_attr_t* attr;
981 unsigned i;
982 struct attribute sibling;
984 offset = traverse->data - ctx->sections[ctx->section].address;
985 entry_code = dwarf2_leb128_as_unsigned(traverse);
986 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
987 if (!entry_code)
989 *pdi = NULL;
990 return TRUE;
992 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
993 if (!abbrev)
995 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
996 return FALSE;
998 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
999 if (!di) return FALSE;
1000 di->abbrev = abbrev;
1001 di->symt = NULL;
1003 if (abbrev->num_attr)
1005 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1006 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1008 di->data[i] = traverse->data;
1009 dwarf2_swallow_attribute(traverse, attr);
1012 else di->data = NULL;
1013 if (abbrev->have_child)
1015 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1016 while (traverse->data < traverse->end_data)
1018 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
1019 if (!child) break;
1020 where = vector_add(&di->children, &ctx->pool);
1021 if (!where) return FALSE;
1022 *where = child;
1025 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1026 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1028 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1029 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1030 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1032 *pdi = di;
1033 return TRUE;
1036 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1037 dwarf2_debug_info_t* di)
1039 struct attribute name;
1040 struct attribute size;
1041 struct attribute encoding;
1042 enum BasicType bt;
1043 int cache_idx = -1;
1044 if (di->symt) return di->symt;
1046 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1048 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1049 name.u.string = NULL;
1050 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1051 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1053 switch (encoding.u.uvalue)
1055 case DW_ATE_void: bt = btVoid; break;
1056 case DW_ATE_address: bt = btULong; break;
1057 case DW_ATE_boolean: bt = btBool; break;
1058 case DW_ATE_complex_float: bt = btComplex; break;
1059 case DW_ATE_float: bt = btFloat; break;
1060 case DW_ATE_signed: bt = btInt; break;
1061 case DW_ATE_unsigned: bt = btUInt; break;
1062 case DW_ATE_signed_char: bt = btChar; break;
1063 case DW_ATE_unsigned_char: bt = btChar; break;
1064 default: bt = btNoType; break;
1066 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1067 switch (bt)
1069 case btVoid:
1070 assert(size.u.uvalue == 0);
1071 cache_idx = sc_void;
1072 break;
1073 case btInt:
1074 switch (size.u.uvalue)
1076 case 1: cache_idx = sc_int1; break;
1077 case 2: cache_idx = sc_int2; break;
1078 case 4: cache_idx = sc_int4; break;
1080 break;
1081 default: break;
1083 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1084 ctx->symt_cache[cache_idx] = di->symt;
1086 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1087 return di->symt;
1090 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1091 dwarf2_debug_info_t* di)
1093 struct symt* ref_type;
1094 struct attribute name;
1096 if (di->symt) return di->symt;
1098 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1100 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1101 ref_type = dwarf2_lookup_type(ctx, di);
1103 if (name.u.string)
1104 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1105 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1106 return di->symt;
1109 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1110 dwarf2_debug_info_t* di)
1112 struct symt* ref_type;
1113 struct attribute size;
1115 if (di->symt) return di->symt;
1117 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1119 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1120 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1122 ref_type = ctx->symt_cache[sc_void];
1123 assert(ref_type);
1125 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1126 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1127 return di->symt;
1130 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1131 dwarf2_debug_info_t* di)
1133 struct symt* ref_type;
1134 struct symt* idx_type = NULL;
1135 struct attribute min, max, cnt;
1136 dwarf2_debug_info_t* child;
1137 unsigned int i;
1139 if (di->symt) return di->symt;
1141 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1143 if (!di->abbrev->have_child)
1145 FIXME("array without range information\n");
1146 return NULL;
1148 ref_type = dwarf2_lookup_type(ctx, di);
1150 for (i=0; i<vector_length(&di->children); i++)
1152 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1153 switch (child->abbrev->tag)
1155 case DW_TAG_subrange_type:
1156 idx_type = dwarf2_lookup_type(ctx, child);
1157 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1158 min.u.uvalue = 0;
1159 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1160 max.u.uvalue = 0;
1161 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1162 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1163 break;
1164 default:
1165 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1166 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1167 break;
1170 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1171 return di->symt;
1174 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1175 dwarf2_debug_info_t* di)
1177 struct symt* ref_type;
1179 if (di->symt) return di->symt;
1181 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1183 ref_type = dwarf2_lookup_type(ctx, di);
1184 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1185 di->symt = ref_type;
1187 return ref_type;
1190 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1191 dwarf2_debug_info_t* di)
1193 struct symt* ref_type;
1195 if (di->symt) return di->symt;
1197 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1199 ref_type = dwarf2_lookup_type(ctx, di);
1200 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1201 di->symt = ref_type;
1203 return ref_type;
1206 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1207 dwarf2_debug_info_t* di)
1209 struct symt* ref_type = NULL;
1211 if (di->symt) return di->symt;
1213 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1215 ref_type = dwarf2_lookup_type(ctx, di);
1216 /* FIXME: for now, we hard-wire C++ references to pointers */
1217 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
1219 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1221 return di->symt;
1224 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1225 const dwarf2_debug_info_t* di,
1226 struct symt_udt* parent)
1228 struct symt* elt_type;
1229 struct attribute name;
1230 struct attribute bit_size;
1231 struct attribute bit_offset;
1232 struct location loc;
1234 assert(parent);
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)) name.u.string = NULL;
1239 elt_type = dwarf2_lookup_type(ctx, di);
1240 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1242 if (loc.kind != loc_absolute)
1244 FIXME("Found register, while not expecting it\n");
1245 loc.offset = 0;
1247 else
1248 TRACE("found member_location at %s -> %lu\n",
1249 dwarf2_debug_ctx(ctx), loc.offset);
1251 else
1252 loc.offset = 0;
1253 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1254 bit_size.u.uvalue = 0;
1255 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1257 /* FIXME: we should only do this when implementation is LSB (which is
1258 * the case on i386 processors)
1260 struct attribute nbytes;
1261 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1263 DWORD64 size;
1264 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1265 (unsigned long)size : 0;
1267 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1269 else bit_offset.u.uvalue = 0;
1270 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1271 (loc.offset << 3) + bit_offset.u.uvalue,
1272 bit_size.u.uvalue);
1274 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1277 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1278 dwarf2_debug_info_t* di,
1279 enum UdtKind udt)
1281 struct attribute name;
1282 struct attribute size;
1284 if (di->symt) return di->symt;
1286 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1288 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1289 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1291 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1293 if (di->abbrev->have_child) /** any interest to not have child ? */
1295 dwarf2_debug_info_t* child;
1296 unsigned int i;
1298 for (i=0; i<vector_length(&di->children); i++)
1300 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1302 switch (child->abbrev->tag)
1304 case DW_TAG_member:
1305 /* FIXME: should I follow the sibling stuff ?? */
1306 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1307 break;
1308 case DW_TAG_enumeration_type:
1309 dwarf2_parse_enumeration_type(ctx, child);
1310 break;
1311 case DW_TAG_structure_type:
1312 case DW_TAG_class_type:
1313 case DW_TAG_union_type:
1314 case DW_TAG_typedef:
1315 /* FIXME: we need to handle nested udt definitions */
1316 case DW_TAG_inheritance:
1317 case DW_TAG_subprogram:
1318 case DW_TAG_variable:
1319 /* FIXME: some C++ related stuff */
1320 break;
1321 default:
1322 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1323 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1324 break;
1329 return di->symt;
1332 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1333 const dwarf2_debug_info_t* di,
1334 struct symt_enum* parent)
1336 struct attribute name;
1337 struct attribute value;
1339 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1341 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1342 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1343 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1345 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1348 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1349 dwarf2_debug_info_t* di)
1351 struct attribute name;
1352 struct attribute size;
1353 struct symt_basic* basetype;
1355 if (di->symt) return di->symt;
1357 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1359 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1360 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1362 switch (size.u.uvalue) /* FIXME: that's wrong */
1364 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1365 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1366 default:
1367 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1370 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1372 if (di->abbrev->have_child) /* any interest to not have child ? */
1374 dwarf2_debug_info_t* child;
1375 unsigned int i;
1377 /* FIXME: should we use the sibling stuff ?? */
1378 for (i=0; i<vector_length(&di->children); i++)
1380 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1382 switch (child->abbrev->tag)
1384 case DW_TAG_enumerator:
1385 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1386 break;
1387 default:
1388 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1389 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1393 return di->symt;
1396 /* structure used to pass information around when parsing a subprogram */
1397 typedef struct dwarf2_subprogram_s
1399 dwarf2_parse_context_t* ctx;
1400 struct symt_compiland* compiland;
1401 struct symt_function* func;
1402 BOOL non_computed_variable;
1403 struct location frame;
1404 } dwarf2_subprogram_t;
1406 /******************************************************************
1407 * dwarf2_parse_variable
1409 * Parses any variable (parameter, local/global variable)
1411 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1412 struct symt_block* block,
1413 dwarf2_debug_info_t* di)
1415 struct symt* param_type;
1416 struct attribute name, value;
1417 struct location loc;
1418 BOOL is_pmt;
1420 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1422 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1423 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1425 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1426 /* cannot do much without the name, the functions below won't like it. */
1427 return;
1429 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1430 &loc, &subpgm->frame))
1432 struct attribute ext;
1434 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1435 name.u.string, loc.kind, loc.offset, loc.reg,
1436 dwarf2_debug_ctx(subpgm->ctx));
1438 switch (loc.kind)
1440 case loc_error:
1441 break;
1442 case loc_absolute:
1443 /* it's a global variable */
1444 /* FIXME: we don't handle its scope yet */
1445 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1446 ext.u.uvalue = 0;
1447 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1448 name.u.string, !ext.u.uvalue,
1449 subpgm->ctx->load_offset + loc.offset,
1450 0, param_type);
1451 break;
1452 default:
1453 subpgm->non_computed_variable = TRUE;
1454 /* fall through */
1455 case loc_register:
1456 case loc_regrel:
1457 /* either a pmt/variable relative to frame pointer or
1458 * pmt/variable in a register
1460 assert(subpgm->func);
1461 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1462 is_pmt ? DataIsParam : DataIsLocal,
1463 &loc, block, param_type, name.u.string);
1464 break;
1467 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1469 VARIANT v;
1470 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1471 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1472 switch (value.form)
1474 case DW_FORM_data1:
1475 case DW_FORM_data2:
1476 case DW_FORM_data4:
1477 case DW_FORM_udata:
1478 case DW_FORM_addr:
1479 v.n1.n2.vt = VT_UI4;
1480 v.n1.n2.n3.lVal = value.u.uvalue;
1481 break;
1483 case DW_FORM_sdata:
1484 v.n1.n2.vt = VT_I4;
1485 v.n1.n2.n3.lVal = value.u.svalue;
1486 break;
1488 case DW_FORM_strp:
1489 case DW_FORM_string:
1490 /* FIXME: native doesn't report const strings from here !!
1491 * however, the value of the string is in the code somewhere
1493 v.n1.n2.vt = VT_I1 | VT_BYREF;
1494 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1495 break;
1497 case DW_FORM_block:
1498 case DW_FORM_block1:
1499 case DW_FORM_block2:
1500 case DW_FORM_block4:
1501 v.n1.n2.vt = VT_I4;
1502 switch (value.u.block.size)
1504 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1505 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1506 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1507 default:
1508 v.n1.n2.vt = VT_I1 | VT_BYREF;
1509 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1510 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1512 break;
1514 case DW_FORM_data8:
1515 v.n1.n2.vt = VT_I1 | VT_BYREF;
1516 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1517 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1518 break;
1520 default:
1521 FIXME("Unsupported form for const value %s (%lx)\n",
1522 name.u.string, value.form);
1523 v.n1.n2.vt = VT_EMPTY;
1525 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1526 name.u.string, param_type, &v)->symt;
1528 if (is_pmt && subpgm->func && subpgm->func->type)
1529 symt_add_function_signature_parameter(subpgm->ctx->module,
1530 (struct symt_function_signature*)subpgm->func->type,
1531 param_type);
1533 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1536 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1537 const dwarf2_debug_info_t* di)
1539 struct attribute name;
1540 struct attribute low_pc;
1541 struct location loc;
1543 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1545 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1546 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1547 name.u.string = NULL;
1549 loc.kind = loc_absolute;
1550 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1551 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1552 &loc, name.u.string);
1555 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1556 struct symt_block* parent_block,
1557 const dwarf2_debug_info_t* di);
1559 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1560 struct symt_block* parent_block,
1561 const dwarf2_debug_info_t* di)
1563 struct symt_block* block;
1564 struct attribute low_pc;
1565 struct attribute high_pc;
1567 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1569 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1570 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1572 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1573 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1574 high_pc.u.uvalue - low_pc.u.uvalue);
1576 if (di->abbrev->have_child) /** any interest to not have child ? */
1578 dwarf2_debug_info_t* child;
1579 unsigned int i;
1581 for (i=0; i<vector_length(&di->children); i++)
1583 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1585 switch (child->abbrev->tag)
1587 case DW_TAG_formal_parameter:
1588 case DW_TAG_variable:
1589 dwarf2_parse_variable(subpgm, block, child);
1590 break;
1591 case DW_TAG_lexical_block:
1592 dwarf2_parse_subprogram_block(subpgm, block, child);
1593 break;
1594 case DW_TAG_inlined_subroutine:
1595 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1596 break;
1597 case DW_TAG_label:
1598 dwarf2_parse_subprogram_label(subpgm, child);
1599 break;
1600 default:
1601 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1602 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1603 dwarf2_debug_di(di));
1607 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1610 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1611 struct symt_block* parent_block,
1612 const dwarf2_debug_info_t* di)
1614 struct symt_block* block;
1615 struct attribute low_pc;
1616 struct attribute high_pc;
1618 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1620 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc))
1621 low_pc.u.uvalue = 0;
1622 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc))
1623 high_pc.u.uvalue = 0;
1625 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1626 subpgm->ctx->load_offset + low_pc.u.uvalue - subpgm->func->address,
1627 high_pc.u.uvalue - low_pc.u.uvalue);
1629 if (di->abbrev->have_child) /** any interest to not have child ? */
1631 dwarf2_debug_info_t* child;
1632 unsigned int i;
1634 for (i=0; i<vector_length(&di->children); i++)
1636 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1638 switch (child->abbrev->tag)
1640 case DW_TAG_inlined_subroutine:
1641 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1642 break;
1643 case DW_TAG_variable:
1644 dwarf2_parse_variable(subpgm, block, child);
1645 break;
1646 case DW_TAG_lexical_block:
1647 dwarf2_parse_subprogram_block(subpgm, block, child);
1648 break;
1649 case DW_TAG_subprogram:
1650 /* FIXME: likely a declaration (to be checked)
1651 * skip it for now
1653 break;
1654 case DW_TAG_formal_parameter:
1655 /* FIXME: likely elements for exception handling (GCC flavor)
1656 * Skip it for now
1658 break;
1659 case DW_TAG_label:
1660 dwarf2_parse_subprogram_label(subpgm, child);
1661 break;
1662 case DW_TAG_class_type:
1663 case DW_TAG_structure_type:
1664 case DW_TAG_union_type:
1665 case DW_TAG_enumeration_type:
1666 case DW_TAG_typedef:
1667 /* the type referred to will be loaded when we need it, so skip it */
1668 break;
1669 default:
1670 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1671 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1676 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1679 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1680 dwarf2_debug_info_t* di,
1681 struct symt_compiland* compiland)
1683 struct attribute name;
1684 struct attribute low_pc;
1685 struct attribute high_pc;
1686 struct attribute is_decl;
1687 struct attribute inline_flags;
1688 struct symt* ret_type;
1689 struct symt_function_signature* sig_type;
1690 dwarf2_subprogram_t subpgm;
1692 if (di->symt) return di->symt;
1694 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1696 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1698 WARN("No name for function... dropping function\n");
1699 return NULL;
1701 /* if it's an abstract representation of an inline function, there should be
1702 * a concrete object that we'll handle
1704 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1706 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1707 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1708 return NULL;
1711 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1712 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1713 /* As functions (defined as inline assembly) get debug info with dwarf
1714 * (not the case for stabs), we just drop Wine's thunks here...
1715 * Actual thunks will be created in elf_module from the symbol table
1717 if (elf_is_in_thunk_area(ctx->load_offset + low_pc.u.uvalue,
1718 ctx->thunks) >= 0)
1719 return NULL;
1720 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1721 is_decl.u.uvalue = 0;
1723 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1725 ret_type = ctx->symt_cache[sc_void];
1726 assert(ret_type);
1729 /* FIXME: assuming C source code */
1730 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1731 if (!is_decl.u.uvalue)
1733 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1734 ctx->load_offset + low_pc.u.uvalue,
1735 high_pc.u.uvalue - low_pc.u.uvalue,
1736 &sig_type->symt);
1737 di->symt = &subpgm.func->symt;
1739 else subpgm.func = NULL;
1741 subpgm.ctx = ctx;
1742 subpgm.compiland = compiland;
1743 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1744 &subpgm.frame, NULL))
1746 /* on stack !! */
1747 subpgm.frame.kind = loc_regrel;
1748 subpgm.frame.reg = 0;
1749 subpgm.frame.offset = 0;
1751 subpgm.non_computed_variable = FALSE;
1753 if (di->abbrev->have_child) /** any interest to not have child ? */
1755 dwarf2_debug_info_t* child;
1756 unsigned int i;
1758 for (i=0; i<vector_length(&di->children); i++)
1760 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1762 switch (child->abbrev->tag)
1764 case DW_TAG_variable:
1765 case DW_TAG_formal_parameter:
1766 dwarf2_parse_variable(&subpgm, NULL, child);
1767 break;
1768 case DW_TAG_lexical_block:
1769 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1770 break;
1771 case DW_TAG_inlined_subroutine:
1772 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1773 break;
1774 case DW_TAG_subprogram:
1775 /* FIXME: likely a declaration (to be checked)
1776 * skip it for now
1778 break;
1779 case DW_TAG_label:
1780 dwarf2_parse_subprogram_label(&subpgm, child);
1781 break;
1782 case DW_TAG_class_type:
1783 case DW_TAG_structure_type:
1784 case DW_TAG_union_type:
1785 case DW_TAG_enumeration_type:
1786 case DW_TAG_typedef:
1787 /* the type referred to will be loaded when we need it, so skip it */
1788 break;
1789 case DW_TAG_unspecified_parameters:
1790 /* FIXME: no support in dbghelp's internals so far */
1791 break;
1792 default:
1793 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1794 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1799 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1801 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1802 &subpgm.frame, NULL);
1804 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1806 return di->symt;
1809 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1810 dwarf2_debug_info_t* di)
1812 struct symt* ret_type;
1813 struct symt_function_signature* sig_type;
1815 if (di->symt) return di->symt;
1817 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1819 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1821 ret_type = ctx->symt_cache[sc_void];
1822 assert(ret_type);
1825 /* FIXME: assuming C source code */
1826 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1828 if (di->abbrev->have_child) /** any interest to not have child ? */
1830 dwarf2_debug_info_t* child;
1831 unsigned int i;
1833 for (i=0; i<vector_length(&di->children); i++)
1835 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1837 switch (child->abbrev->tag)
1839 case DW_TAG_formal_parameter:
1840 symt_add_function_signature_parameter(ctx->module, sig_type,
1841 dwarf2_lookup_type(ctx, child));
1842 break;
1843 case DW_TAG_unspecified_parameters:
1844 WARN("Unsupported unspecified parameters\n");
1845 break;
1850 return di->symt = &sig_type->symt;
1853 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1854 dwarf2_debug_info_t* di,
1855 struct symt_compiland* compiland)
1857 switch (di->abbrev->tag)
1859 case DW_TAG_typedef:
1860 dwarf2_parse_typedef(ctx, di);
1861 break;
1862 case DW_TAG_base_type:
1863 dwarf2_parse_base_type(ctx, di);
1864 break;
1865 case DW_TAG_pointer_type:
1866 dwarf2_parse_pointer_type(ctx, di);
1867 break;
1868 case DW_TAG_class_type:
1869 dwarf2_parse_udt_type(ctx, di, UdtClass);
1870 break;
1871 case DW_TAG_structure_type:
1872 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1873 break;
1874 case DW_TAG_union_type:
1875 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1876 break;
1877 case DW_TAG_array_type:
1878 dwarf2_parse_array_type(ctx, di);
1879 break;
1880 case DW_TAG_const_type:
1881 dwarf2_parse_const_type(ctx, di);
1882 break;
1883 case DW_TAG_volatile_type:
1884 dwarf2_parse_volatile_type(ctx, di);
1885 break;
1886 case DW_TAG_reference_type:
1887 dwarf2_parse_reference_type(ctx, di);
1888 break;
1889 case DW_TAG_enumeration_type:
1890 dwarf2_parse_enumeration_type(ctx, di);
1891 break;
1892 case DW_TAG_subprogram:
1893 dwarf2_parse_subprogram(ctx, di, compiland);
1894 break;
1895 case DW_TAG_subroutine_type:
1896 dwarf2_parse_subroutine_type(ctx, di);
1897 break;
1898 case DW_TAG_variable:
1900 dwarf2_subprogram_t subpgm;
1902 subpgm.ctx = ctx;
1903 subpgm.compiland = compiland;
1904 subpgm.func = NULL;
1905 subpgm.frame.kind = loc_absolute;
1906 subpgm.frame.offset = 0;
1907 subpgm.frame.reg = Wine_DW_no_register;
1908 dwarf2_parse_variable(&subpgm, NULL, di);
1910 break;
1911 /* silence a couple of C++ defines */
1912 case DW_TAG_namespace:
1913 case DW_TAG_imported_module:
1914 case DW_TAG_imported_declaration:
1915 break;
1916 default:
1917 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1918 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1922 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1923 const struct vector* v, unsigned file, unsigned line)
1925 struct symt_function* func;
1926 struct symt_ht* symt;
1927 unsigned* psrc;
1929 if (!file || !(psrc = vector_at(v, file - 1))) return;
1931 TRACE("%s %lx %s %u\n",
1932 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1933 if (!(symt = symt_find_nearest(module, address)) ||
1934 symt->symt.tag != SymTagFunction) return;
1935 func = (struct symt_function*)symt;
1936 symt_add_func_line(module, func, *psrc, line, address - func->address);
1939 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1940 dwarf2_parse_context_t* ctx,
1941 const char* compile_dir,
1942 unsigned long offset)
1944 dwarf2_traverse_context_t traverse;
1945 unsigned long length;
1946 unsigned version, header_len, insn_size, default_stmt;
1947 unsigned line_range, opcode_base;
1948 int line_base;
1949 const unsigned char* opcode_len;
1950 struct vector dirs;
1951 struct vector files;
1952 const char** p;
1954 /* section with line numbers stripped */
1955 if (sections[section_line].address == ELF_NO_MAP)
1956 return FALSE;
1958 traverse.data = sections[section_line].address + offset;
1959 traverse.start_data = traverse.data;
1960 traverse.end_data = traverse.data + 4;
1961 traverse.word_size = ctx->word_size;
1963 length = dwarf2_parse_u4(&traverse);
1964 traverse.end_data = traverse.start_data + length;
1966 version = dwarf2_parse_u2(&traverse);
1967 header_len = dwarf2_parse_u4(&traverse);
1968 insn_size = dwarf2_parse_byte(&traverse);
1969 default_stmt = dwarf2_parse_byte(&traverse);
1970 line_base = (signed char)dwarf2_parse_byte(&traverse);
1971 line_range = dwarf2_parse_byte(&traverse);
1972 opcode_base = dwarf2_parse_byte(&traverse);
1974 opcode_len = traverse.data;
1975 traverse.data += opcode_base - 1;
1977 vector_init(&dirs, sizeof(const char*), 4);
1978 p = vector_add(&dirs, &ctx->pool);
1979 *p = compile_dir ? compile_dir : ".";
1980 while (*traverse.data)
1982 const char* rel = (const char*)traverse.data;
1983 unsigned rellen = strlen(rel);
1984 TRACE("Got include %s\n", rel);
1985 traverse.data += rellen + 1;
1986 p = vector_add(&dirs, &ctx->pool);
1988 if (*rel == '/' || !compile_dir)
1989 *p = rel;
1990 else
1992 /* include directory relative to compile directory */
1993 unsigned baselen = strlen(compile_dir);
1994 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1995 strcpy(tmp, compile_dir);
1996 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1997 strcpy(&tmp[baselen], rel);
1998 *p = tmp;
2002 traverse.data++;
2004 vector_init(&files, sizeof(unsigned), 16);
2005 while (*traverse.data)
2007 unsigned int dir_index, mod_time, length;
2008 const char* name;
2009 const char* dir;
2010 unsigned* psrc;
2012 name = (const char*)traverse.data;
2013 traverse.data += strlen(name) + 1;
2014 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2015 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2016 length = dwarf2_leb128_as_unsigned(&traverse);
2017 dir = *(const char**)vector_at(&dirs, dir_index);
2018 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
2019 psrc = vector_add(&files, &ctx->pool);
2020 *psrc = source_new(ctx->module, dir, name);
2022 traverse.data++;
2024 while (traverse.data < traverse.end_data)
2026 unsigned long address = 0;
2027 unsigned file = 1;
2028 unsigned line = 1;
2029 unsigned is_stmt = default_stmt;
2030 BOOL basic_block = FALSE, end_sequence = FALSE;
2031 unsigned opcode, extopcode, i;
2033 while (!end_sequence)
2035 opcode = dwarf2_parse_byte(&traverse);
2036 TRACE("Got opcode %x\n", opcode);
2038 if (opcode >= opcode_base)
2040 unsigned delta = opcode - opcode_base;
2042 address += (delta / line_range) * insn_size;
2043 line += line_base + (delta % line_range);
2044 basic_block = TRUE;
2045 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2047 else
2049 switch (opcode)
2051 case DW_LNS_copy:
2052 basic_block = FALSE;
2053 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2054 break;
2055 case DW_LNS_advance_pc:
2056 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2057 break;
2058 case DW_LNS_advance_line:
2059 line += dwarf2_leb128_as_signed(&traverse);
2060 break;
2061 case DW_LNS_set_file:
2062 file = dwarf2_leb128_as_unsigned(&traverse);
2063 break;
2064 case DW_LNS_set_column:
2065 dwarf2_leb128_as_unsigned(&traverse);
2066 break;
2067 case DW_LNS_negate_stmt:
2068 is_stmt = !is_stmt;
2069 break;
2070 case DW_LNS_set_basic_block:
2071 basic_block = 1;
2072 break;
2073 case DW_LNS_const_add_pc:
2074 address += ((255 - opcode_base) / line_range) * insn_size;
2075 break;
2076 case DW_LNS_fixed_advance_pc:
2077 address += dwarf2_parse_u2(&traverse);
2078 break;
2079 case DW_LNS_extended_op:
2080 dwarf2_leb128_as_unsigned(&traverse);
2081 extopcode = dwarf2_parse_byte(&traverse);
2082 switch (extopcode)
2084 case DW_LNE_end_sequence:
2085 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2086 end_sequence = TRUE;
2087 break;
2088 case DW_LNE_set_address:
2089 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2090 break;
2091 case DW_LNE_define_file:
2092 FIXME("not handled %s\n", traverse.data);
2093 traverse.data += strlen((const char *)traverse.data) + 1;
2094 dwarf2_leb128_as_unsigned(&traverse);
2095 dwarf2_leb128_as_unsigned(&traverse);
2096 dwarf2_leb128_as_unsigned(&traverse);
2097 break;
2098 default:
2099 FIXME("Unsupported extended opcode %x\n", extopcode);
2100 break;
2102 break;
2103 default:
2104 WARN("Unsupported opcode %x\n", opcode);
2105 for (i = 0; i < opcode_len[opcode]; i++)
2106 dwarf2_leb128_as_unsigned(&traverse);
2107 break;
2112 return TRUE;
2115 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2116 struct module* module,
2117 const struct elf_thunk_area* thunks,
2118 dwarf2_traverse_context_t* mod_ctx,
2119 unsigned long load_offset)
2121 dwarf2_parse_context_t ctx;
2122 dwarf2_traverse_context_t abbrev_ctx;
2123 dwarf2_debug_info_t* di;
2124 dwarf2_traverse_context_t cu_ctx;
2125 const unsigned char* comp_unit_start = mod_ctx->data;
2126 unsigned long cu_length;
2127 unsigned short cu_version;
2128 unsigned long cu_abbrev_offset;
2129 BOOL ret = FALSE;
2131 cu_length = dwarf2_parse_u4(mod_ctx);
2132 cu_ctx.data = cu_ctx.start_data = mod_ctx->data;
2133 cu_ctx.end_data = mod_ctx->data + cu_length;
2134 mod_ctx->data += cu_length;
2135 cu_version = dwarf2_parse_u2(&cu_ctx);
2136 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2137 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2139 TRACE("Compilation Unit Header found at 0x%x:\n",
2140 (int)(comp_unit_start - sections[section_debug].address));
2141 TRACE("- length: %lu\n", cu_length);
2142 TRACE("- version: %u\n", cu_version);
2143 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2144 TRACE("- word_size: %u\n", cu_ctx.word_size);
2146 if (cu_version != 2)
2148 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2149 cu_version);
2150 return FALSE;
2153 pool_init(&ctx.pool, 65536);
2154 ctx.sections = sections;
2155 ctx.section = section_debug;
2156 ctx.module = module;
2157 ctx.word_size = cu_ctx.word_size;
2158 ctx.thunks = thunks;
2159 ctx.load_offset = load_offset;
2160 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2161 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2162 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2164 abbrev_ctx.start_data = sections[section_abbrev].address + cu_abbrev_offset;
2165 abbrev_ctx.data = abbrev_ctx.start_data;
2166 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2167 abbrev_ctx.word_size = cu_ctx.word_size;
2168 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2170 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2171 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2173 if (di->abbrev->tag == DW_TAG_compile_unit)
2175 struct attribute name;
2176 dwarf2_debug_info_t** pdi = NULL;
2177 struct attribute stmt_list, low_pc;
2178 struct attribute comp_dir;
2180 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2181 name.u.string = NULL;
2183 /* get working directory of current compilation unit */
2184 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2185 comp_dir.u.string = NULL;
2187 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2188 low_pc.u.uvalue = 0;
2189 di->symt = &symt_new_compiland(module,
2190 ctx.load_offset + low_pc.u.uvalue,
2191 source_new(module, comp_dir.u.string, name.u.string))->symt;
2193 if (di->abbrev->have_child)
2195 unsigned int i;
2196 for (i=0; i<vector_length(&di->children); i++)
2198 pdi = vector_at(&di->children, i);
2199 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2202 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2204 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2205 module->module.LineNumbers = TRUE;
2207 ret = TRUE;
2209 else FIXME("Should have a compilation unit here\n");
2210 pool_destroy(&ctx.pool);
2211 return ret;
2214 static BOOL dwarf2_lookup_loclist(const struct module* module, const BYTE* start,
2215 unsigned long ip,
2216 dwarf2_traverse_context_t* lctx)
2218 DWORD beg, end;
2219 const BYTE* ptr = start;
2220 DWORD len;
2222 while (ptr < module->dwarf2_info->debug_loc.address + module->dwarf2_info->debug_loc.size)
2224 beg = dwarf2_get_u4(ptr); ptr += 4;
2225 end = dwarf2_get_u4(ptr); ptr += 4;
2226 if (!beg && !end) break;
2227 len = dwarf2_get_u2(ptr); ptr += 2;
2229 if (beg <= ip && ip < end)
2231 lctx->data = ptr;
2232 lctx->end_data = ptr + len;
2233 lctx->word_size = 4; /* FIXME word size !!! */
2234 return TRUE;
2236 ptr += len;
2238 WARN("Couldn't find ip in location list\n");
2239 return FALSE;
2242 static enum location_error loc_compute_frame(struct process* pcs,
2243 const struct module* module,
2244 const struct symt_function* func,
2245 DWORD ip, struct location* frame)
2247 struct symt** psym = NULL;
2248 struct location* pframe;
2249 dwarf2_traverse_context_t lctx;
2250 enum location_error err;
2251 unsigned int i;
2253 for (i=0; i<vector_length(&func->vchildren); i++)
2255 psym = vector_at(&func->vchildren, i);
2256 if ((*psym)->tag == SymTagCustom)
2258 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2260 /* First, recompute the frame information, if needed */
2261 switch (pframe->kind)
2263 case loc_regrel:
2264 case loc_register:
2265 *frame = *pframe;
2266 break;
2267 case loc_dwarf2_location_list:
2268 WARN("Searching loclist for %s\n", func->hash_elt.name);
2269 if (!dwarf2_lookup_loclist(module,
2270 module->dwarf2_info->debug_loc.address + pframe->offset,
2271 ip, &lctx))
2272 return loc_err_out_of_scope;
2273 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2274 if (frame->kind >= loc_user)
2276 WARN("Couldn't compute runtime frame location\n");
2277 return loc_err_too_complex;
2279 break;
2280 default:
2281 WARN("Unsupported frame kind %d\n", pframe->kind);
2282 return loc_err_internal;
2284 return 0;
2287 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2288 return loc_err_internal;
2291 static void dwarf2_location_compute(struct process* pcs,
2292 const struct module* module,
2293 const struct symt_function* func,
2294 struct location* loc)
2296 struct location frame;
2297 DWORD ip;
2298 int err;
2299 dwarf2_traverse_context_t lctx;
2301 if (!func->container || func->container->tag != SymTagCompiland)
2303 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
2304 err = loc_err_internal;
2306 else
2308 /* instruction pointer relative to compiland's start */
2309 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
2311 if ((err = loc_compute_frame(pcs, module, func, ip, &frame)) == 0)
2313 switch (loc->kind)
2315 case loc_dwarf2_location_list:
2316 /* Then, if the variable has a location list, find it !! */
2317 if (dwarf2_lookup_loclist(module,
2318 module->dwarf2_info->debug_loc.address + loc->offset,
2319 ip, &lctx))
2320 goto do_compute;
2321 err = loc_err_out_of_scope;
2322 break;
2323 case loc_dwarf2_block:
2324 /* or if we have a copy of an existing block, get ready for it */
2326 unsigned* ptr = (unsigned*)loc->offset;
2328 lctx.data = (const BYTE*)(ptr + 1);
2329 lctx.end_data = lctx.data + *ptr;
2330 lctx.word_size = 4; /* FIXME !! */
2332 do_compute:
2333 /* now get the variable */
2334 err = compute_location(&lctx, loc, pcs->handle, &frame);
2335 break;
2336 case loc_register:
2337 case loc_regrel:
2338 /* nothing to do */
2339 break;
2340 default:
2341 WARN("Unsupported local kind %d\n", loc->kind);
2342 err = loc_err_internal;
2346 if (err < 0)
2348 loc->kind = loc_register;
2349 loc->reg = err;
2353 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
2354 const struct elf_thunk_area* thunks,
2355 const unsigned char* debug, unsigned int debug_size,
2356 const unsigned char* abbrev, unsigned int abbrev_size,
2357 const unsigned char* str, unsigned int str_size,
2358 const unsigned char* line, unsigned int line_size,
2359 const unsigned char* loclist, unsigned int loclist_size)
2361 dwarf2_section_t section[section_max];
2362 unsigned char* ptr;
2363 dwarf2_traverse_context_t mod_ctx;
2365 mod_ctx.start_data = mod_ctx.data = debug;
2366 mod_ctx.end_data = debug + debug_size;
2368 module->loc_compute = dwarf2_location_compute;
2370 section[section_debug].address = debug;
2371 section[section_debug].size = debug_size;
2372 section[section_abbrev].address = abbrev;
2373 section[section_abbrev].size = abbrev_size;
2374 section[section_string].address = str;
2375 section[section_string].size = str_size;
2376 section[section_line].address = line;
2377 section[section_line].size = line_size;
2379 if (loclist_size)
2381 /* initialize the dwarf2 specific info block for this module.
2382 * As we'll need later on the .debug_loc section content, we copy it in
2383 * the module structure for later reuse
2385 module->dwarf2_info = HeapAlloc(GetProcessHeap(), 0, sizeof(*module->dwarf2_info) + loclist_size);
2386 if (!module->dwarf2_info) return FALSE;
2387 ptr = (unsigned char*)(module->dwarf2_info + 1);
2388 memcpy(ptr, loclist, loclist_size);
2389 module->dwarf2_info->debug_loc.address = ptr;
2390 module->dwarf2_info->debug_loc.size = loclist_size;
2393 while (mod_ctx.data < mod_ctx.end_data)
2395 dwarf2_parse_compilation_unit(section, module, thunks, &mod_ctx, load_offset);
2397 module->module.SymType = SymDia;
2398 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
2399 /* FIXME: we could have a finer grain here */
2400 module->module.GlobalSymbols = TRUE;
2401 module->module.TypeInfo = TRUE;
2402 module->module.SourceIndexed = TRUE;
2403 module->module.Publics = TRUE;
2404 return TRUE;