dbghelp: Use Dwarf debug info even when part of it is missing.
[wine.git] / dlls / dbghelp / dwarf.c
blobe02e654c4e774e421b3e73acb6f6adb43836c44d
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2010, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #define NONAMELESSUNION
25 #include "config.h"
27 #include <sys/types.h>
28 #include <fcntl.h>
29 #ifdef HAVE_SYS_STAT_H
30 # include <sys/stat.h>
31 #endif
32 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/mman.h>
34 #endif
35 #include <limits.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #ifdef HAVE_UNISTD_H
39 # include <unistd.h>
40 #endif
41 #include <stdio.h>
42 #ifndef PATH_MAX
43 #define PATH_MAX MAX_PATH
44 #endif
45 #include <assert.h>
46 #include <stdarg.h>
48 #include "windef.h"
49 #include "winbase.h"
50 #include "winuser.h"
51 #include "ole2.h"
52 #include "oleauto.h"
54 #include "dbghelp_private.h"
55 #include "image_private.h"
57 #include "wine/debug.h"
59 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
61 /* FIXME:
62 * - Functions:
63 * o unspecified parameters
64 * o inlined functions
65 * o Debug{Start|End}Point
66 * o CFA
67 * - Udt
68 * o proper types loading (nesting)
71 #if 0
72 static void dump(const void* ptr, unsigned len)
74 int i, j;
75 BYTE msg[128];
76 static const char hexof[] = "0123456789abcdef";
77 const BYTE* x = ptr;
79 for (i = 0; i < len; i += 16)
81 sprintf(msg, "%08x: ", i);
82 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
83 for (j = 0; j < min(16, len - i); j++)
85 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
86 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
87 msg[10 + 3 * j + 2] = ' ';
88 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
89 x[i + j] : '.';
91 msg[10 + 3 * 16] = ' ';
92 msg[10 + 3 * 16 + 1 + 16] = '\0';
93 TRACE("%s\n", msg);
96 #endif
98 /**
100 * Main Specs:
101 * http://www.eagercon.com/dwarf/dwarf3std.htm
102 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
104 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
106 * example of projects who do dwarf2 parsing:
107 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
108 * http://elis.ugent.be/diota/log/ltrace_elf.c
110 #include "dwarf.h"
113 * Parsers
116 typedef struct dwarf2_abbrev_entry_attr_s
118 unsigned long attribute;
119 unsigned long form;
120 struct dwarf2_abbrev_entry_attr_s* next;
121 } dwarf2_abbrev_entry_attr_t;
123 typedef struct dwarf2_abbrev_entry_s
125 unsigned long entry_code;
126 unsigned long tag;
127 unsigned char have_child;
128 unsigned num_attr;
129 dwarf2_abbrev_entry_attr_t* attrs;
130 } dwarf2_abbrev_entry_t;
132 struct dwarf2_block
134 unsigned size;
135 const unsigned char* ptr;
138 struct attribute
140 unsigned long form;
141 union
143 unsigned long uvalue;
144 ULONGLONG lluvalue;
145 long svalue;
146 const char* string;
147 struct dwarf2_block block;
148 } u;
151 typedef struct dwarf2_debug_info_s
153 const dwarf2_abbrev_entry_t*abbrev;
154 struct symt* symt;
155 const unsigned char** data;
156 struct vector children;
157 } dwarf2_debug_info_t;
159 typedef struct dwarf2_section_s
161 const unsigned char* address;
162 unsigned size;
163 DWORD_PTR rva;
164 } dwarf2_section_t;
166 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
168 typedef struct dwarf2_traverse_context_s
170 const unsigned char* data;
171 const unsigned char* end_data;
172 unsigned char word_size;
173 } dwarf2_traverse_context_t;
175 /* symt_cache indexes */
176 #define sc_void 0
177 #define sc_int1 1
178 #define sc_int2 2
179 #define sc_int4 3
180 #define sc_num 4
182 typedef struct dwarf2_parse_context_s
184 const dwarf2_section_t* sections;
185 unsigned section;
186 struct pool pool;
187 struct module* module;
188 const struct elf_thunk_area*thunks;
189 struct sparse_array abbrev_table;
190 struct sparse_array debug_info_table;
191 unsigned long load_offset;
192 unsigned long ref_offset;
193 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
194 } dwarf2_parse_context_t;
196 /* stored in the dbghelp's module internal structure for later reuse */
197 struct dwarf2_module_info_s
199 dwarf2_section_t debug_loc;
200 dwarf2_section_t debug_frame;
201 dwarf2_section_t eh_frame;
202 unsigned char word_size;
205 #define loc_dwarf2_location_list (loc_user + 0)
206 #define loc_dwarf2_block (loc_user + 1)
208 /* forward declarations */
209 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
211 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
213 return *ptr;
216 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
218 unsigned char uvalue = dwarf2_get_byte(ctx->data);
219 ctx->data += 1;
220 return uvalue;
223 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
225 return *(const UINT16*)ptr;
228 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
230 unsigned short uvalue = dwarf2_get_u2(ctx->data);
231 ctx->data += 2;
232 return uvalue;
235 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
237 return *(const UINT32*)ptr;
240 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
242 unsigned long uvalue = dwarf2_get_u4(ctx->data);
243 ctx->data += 4;
244 return uvalue;
247 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
249 return *(const UINT64*)ptr;
252 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
254 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
255 ctx->data += 8;
256 return uvalue;
259 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
261 unsigned long ret = 0;
262 unsigned char byte;
263 unsigned shift = 0;
267 byte = dwarf2_get_byte(ptr++);
268 ret |= (byte & 0x7f) << shift;
269 shift += 7;
270 } while (byte & 0x80);
272 if (end) *end = ptr;
273 return ret;
276 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
278 unsigned long ret;
280 assert(ctx);
282 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
284 return ret;
287 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
289 long ret = 0;
290 unsigned char byte;
291 unsigned shift = 0;
292 const unsigned size = sizeof(int) * 8;
296 byte = dwarf2_get_byte(ptr++);
297 ret |= (byte & 0x7f) << shift;
298 shift += 7;
299 } while (byte & 0x80);
300 if (end) *end = ptr;
302 /* as spec: sign bit of byte is 2nd high order bit (80x40)
303 * -> 0x80 is used as flag.
305 if ((shift < size) && (byte & 0x40))
307 ret |= - (1 << shift);
309 return ret;
312 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
314 long ret = 0;
316 assert(ctx);
318 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
319 return ret;
322 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
324 unsigned ret;
325 for (ret = 0; ctx->data[ret] & 0x80; ret++);
326 return ret + 1;
329 /******************************************************************
330 * dwarf2_get_addr
332 * Returns an address.
333 * We assume that in all cases word size from Dwarf matches the size of
334 * addresses in platform where the exec is compiled.
336 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
338 unsigned long ret;
340 switch (word_size)
342 case 4:
343 ret = dwarf2_get_u4(ptr);
344 break;
345 case 8:
346 ret = dwarf2_get_u8(ptr);
347 break;
348 default:
349 FIXME("Unsupported Word Size %u\n", word_size);
350 ret = 0;
352 return ret;
355 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
357 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
358 ctx->data += ctx->word_size;
359 return ret;
362 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
364 return wine_dbg_sprintf("ctx(%p)", ctx->data);
367 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
369 return wine_dbg_sprintf("ctx(%p,%s)",
370 ctx, debugstr_w(ctx->module->module.ModuleName));
373 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
375 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
376 di->abbrev, di->symt);
379 static dwarf2_abbrev_entry_t*
380 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
381 unsigned long entry_code)
383 assert( NULL != abbrev_table );
384 return sparse_array_find(abbrev_table, entry_code);
387 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
388 struct sparse_array* abbrev_table,
389 struct pool* pool)
391 unsigned long entry_code;
392 dwarf2_abbrev_entry_t* abbrev_entry;
393 dwarf2_abbrev_entry_attr_t* new = NULL;
394 dwarf2_abbrev_entry_attr_t* last = NULL;
395 unsigned long attribute;
396 unsigned long form;
398 assert( NULL != abbrev_ctx );
400 TRACE("%s, end at %p\n",
401 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
403 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
404 while (abbrev_ctx->data < abbrev_ctx->end_data)
406 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
407 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
408 TRACE("found entry_code %lu\n", entry_code);
409 if (!entry_code)
411 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
412 break;
414 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
415 assert( NULL != abbrev_entry );
417 abbrev_entry->entry_code = entry_code;
418 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
419 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
420 abbrev_entry->attrs = NULL;
421 abbrev_entry->num_attr = 0;
423 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
424 abbrev_table, sparse_array_length(abbrev_table),
425 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
427 last = NULL;
428 while (1)
430 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
431 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
432 if (!attribute) break;
434 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
435 assert(new);
437 new->attribute = attribute;
438 new->form = form;
439 new->next = NULL;
440 if (abbrev_entry->attrs) last->next = new;
441 else abbrev_entry->attrs = new;
442 last = new;
443 abbrev_entry->num_attr++;
446 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
449 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
450 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
452 unsigned step;
454 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
456 switch (abbrev_attr->form)
458 case DW_FORM_ref_addr:
459 case DW_FORM_addr: step = ctx->word_size; break;
460 case DW_FORM_flag:
461 case DW_FORM_data1:
462 case DW_FORM_ref1: step = 1; break;
463 case DW_FORM_data2:
464 case DW_FORM_ref2: step = 2; break;
465 case DW_FORM_data4:
466 case DW_FORM_ref4:
467 case DW_FORM_strp: step = 4; break;
468 case DW_FORM_data8:
469 case DW_FORM_ref8: step = 8; break;
470 case DW_FORM_sdata:
471 case DW_FORM_ref_udata:
472 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
473 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
474 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
475 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
476 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
477 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
478 default:
479 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
480 return;
482 ctx->data += step;
485 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
486 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
487 const unsigned char* data,
488 struct attribute* attr)
490 attr->form = abbrev_attr->form;
491 switch (attr->form)
493 case DW_FORM_ref_addr:
494 case DW_FORM_addr:
495 attr->u.uvalue = dwarf2_get_addr(data,
496 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
497 TRACE("addr<0x%lx>\n", attr->u.uvalue);
498 break;
500 case DW_FORM_flag:
501 attr->u.uvalue = dwarf2_get_byte(data);
502 TRACE("flag<0x%lx>\n", attr->u.uvalue);
503 break;
505 case DW_FORM_data1:
506 attr->u.uvalue = dwarf2_get_byte(data);
507 TRACE("data1<%lu>\n", attr->u.uvalue);
508 break;
510 case DW_FORM_data2:
511 attr->u.uvalue = dwarf2_get_u2(data);
512 TRACE("data2<%lu>\n", attr->u.uvalue);
513 break;
515 case DW_FORM_data4:
516 attr->u.uvalue = dwarf2_get_u4(data);
517 TRACE("data4<%lu>\n", attr->u.uvalue);
518 break;
520 case DW_FORM_data8:
521 attr->u.lluvalue = dwarf2_get_u8(data);
522 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
523 break;
525 case DW_FORM_ref1:
526 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
527 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
528 break;
530 case DW_FORM_ref2:
531 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
532 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
533 break;
535 case DW_FORM_ref4:
536 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
537 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
538 break;
540 case DW_FORM_ref8:
541 FIXME("Unhandled 64 bit support\n");
542 break;
544 case DW_FORM_sdata:
545 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
546 break;
548 case DW_FORM_ref_udata:
549 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
550 break;
552 case DW_FORM_udata:
553 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
554 break;
556 case DW_FORM_string:
557 attr->u.string = (const char *)data;
558 TRACE("string<%s>\n", attr->u.string);
559 break;
561 case DW_FORM_strp:
563 unsigned long offset = dwarf2_get_u4(data);
564 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
566 TRACE("strp<%s>\n", attr->u.string);
567 break;
569 case DW_FORM_block:
570 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
571 break;
573 case DW_FORM_block1:
574 attr->u.block.size = dwarf2_get_byte(data);
575 attr->u.block.ptr = data + 1;
576 break;
578 case DW_FORM_block2:
579 attr->u.block.size = dwarf2_get_u2(data);
580 attr->u.block.ptr = data + 2;
581 break;
583 case DW_FORM_block4:
584 attr->u.block.size = dwarf2_get_u4(data);
585 attr->u.block.ptr = data + 4;
586 break;
588 default:
589 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
590 break;
594 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
595 const dwarf2_debug_info_t* di,
596 unsigned at, struct attribute* attr)
598 unsigned i, ai = 0;
599 dwarf2_abbrev_entry_attr_t* abbrev_attr;
600 dwarf2_abbrev_entry_attr_t* abstract_abbrev_attr;
602 while (di)
604 abstract_abbrev_attr = NULL;
605 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
607 if (abbrev_attr->attribute == at)
609 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
610 return TRUE;
612 if (abbrev_attr->attribute == DW_AT_abstract_origin &&
613 at != DW_AT_sibling)
615 abstract_abbrev_attr = abbrev_attr;
616 ai = i;
619 /* do we have an abstract origin debug entry to look into ? */
620 if (!abstract_abbrev_attr) break;
621 dwarf2_fill_attr(ctx, abstract_abbrev_attr, di->data[ai], attr);
622 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
623 FIXME("Should have found the debug info entry\n");
625 return FALSE;
628 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
629 struct symt_compiland*);
631 #define Wine_DW_no_register 0x7FFFFFFF
633 static unsigned dwarf2_map_register(int regno)
635 if (regno == Wine_DW_no_register)
637 FIXME("What the heck map reg 0x%x\n",regno);
638 return 0;
640 return dbghelp_current_cpu->map_dwarf_register(regno);
643 static enum location_error
644 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
645 HANDLE hproc, const struct location* frame)
647 DWORD_PTR tmp, stack[64];
648 unsigned stk;
649 unsigned char op;
650 BOOL piece_found = FALSE;
652 stack[stk = 0] = 0;
654 loc->kind = loc_absolute;
655 loc->reg = Wine_DW_no_register;
657 while (ctx->data < ctx->end_data)
659 op = dwarf2_parse_byte(ctx);
661 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
662 stack[++stk] = op - DW_OP_lit0;
663 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
665 /* dbghelp APIs don't know how to cope with this anyway
666 * (for example 'long long' stored in two registers)
667 * FIXME: We should tell winedbg how to deal with it (sigh)
669 if (!piece_found)
671 if (loc->reg != Wine_DW_no_register)
672 FIXME("Only supporting one reg (%d -> %d)\n",
673 loc->reg, dwarf2_map_register(op - DW_OP_reg0));
674 loc->reg = dwarf2_map_register(op - DW_OP_reg0);
676 loc->kind = loc_register;
678 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
680 /* dbghelp APIs don't know how to cope with this anyway
681 * (for example 'long long' stored in two registers)
682 * FIXME: We should tell winedbg how to deal with it (sigh)
684 if (!piece_found)
686 if (loc->reg != Wine_DW_no_register)
687 FIXME("Only supporting one breg (%d -> %d)\n",
688 loc->reg, dwarf2_map_register(op - DW_OP_breg0));
689 loc->reg = dwarf2_map_register(op - DW_OP_breg0);
691 stack[++stk] = dwarf2_leb128_as_signed(ctx);
692 loc->kind = loc_regrel;
694 else switch (op)
696 case DW_OP_nop: break;
697 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
698 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
699 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
700 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
701 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
702 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
703 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
704 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
705 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
706 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
707 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
708 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
709 case DW_OP_drop: stk--; break;
710 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
711 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
712 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
713 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
714 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
715 case DW_OP_neg: stack[stk] = -stack[stk]; break;
716 case DW_OP_not: stack[stk] = ~stack[stk]; break;
717 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
718 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
719 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
720 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
721 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
722 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
723 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
724 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
725 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
726 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
727 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
728 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
729 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
730 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
731 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
732 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
733 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
734 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
735 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
736 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
737 case DW_OP_regx:
738 tmp = dwarf2_leb128_as_unsigned(ctx);
739 if (!piece_found)
741 if (loc->reg != Wine_DW_no_register)
742 FIXME("Only supporting one reg\n");
743 loc->reg = dwarf2_map_register(tmp);
745 loc->kind = loc_register;
746 break;
747 case DW_OP_bregx:
748 tmp = dwarf2_leb128_as_unsigned(ctx);
749 if (loc->reg != Wine_DW_no_register)
750 FIXME("Only supporting one regx\n");
751 loc->reg = dwarf2_map_register(tmp);
752 stack[++stk] = dwarf2_leb128_as_signed(ctx);
753 loc->kind = loc_regrel;
754 break;
755 case DW_OP_fbreg:
756 if (loc->reg != Wine_DW_no_register)
757 FIXME("Only supporting one reg (%d -> -2)\n", loc->reg);
758 if (frame && frame->kind == loc_register)
760 loc->kind = loc_regrel;
761 loc->reg = frame->reg;
762 stack[++stk] = dwarf2_leb128_as_signed(ctx);
764 else if (frame && frame->kind == loc_regrel)
766 loc->kind = loc_regrel;
767 loc->reg = frame->reg;
768 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
770 else
772 /* FIXME: this could be later optimized by not recomputing
773 * this very location expression
775 loc->kind = loc_dwarf2_block;
776 stack[++stk] = dwarf2_leb128_as_signed(ctx);
778 break;
779 case DW_OP_piece:
781 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
782 WARN("Not handling OP_piece (size=%d)\n", sz);
783 piece_found = TRUE;
785 break;
786 case DW_OP_deref:
787 if (!stk)
789 FIXME("Unexpected empty stack\n");
790 return loc_err_internal;
792 if (loc->reg != Wine_DW_no_register)
794 WARN("Too complex expression for deref\n");
795 return loc_err_too_complex;
797 if (hproc)
799 DWORD_PTR addr = stack[stk--];
800 DWORD_PTR deref;
802 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
804 WARN("Couldn't read memory at %lx\n", addr);
805 return loc_err_cant_read;
807 stack[++stk] = deref;
809 else
811 loc->kind = loc_dwarf2_block;
813 break;
814 case DW_OP_deref_size:
815 if (!stk)
817 FIXME("Unexpected empty stack\n");
818 return loc_err_internal;
820 if (loc->reg != Wine_DW_no_register)
822 WARN("Too complex expression for deref\n");
823 return loc_err_too_complex;
825 if (hproc)
827 DWORD_PTR addr = stack[stk--];
828 BYTE derefsize = dwarf2_parse_byte(ctx);
829 DWORD64 deref;
831 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
833 WARN("Couldn't read memory at %lx\n", addr);
834 return loc_err_cant_read;
837 switch (derefsize)
839 case 1: stack[++stk] = *(unsigned char*)&deref; break;
840 case 2: stack[++stk] = *(unsigned short*)&deref; break;
841 case 4: stack[++stk] = *(DWORD*)&deref; break;
842 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
845 else
847 loc->kind = loc_dwarf2_block;
849 break;
850 case DW_OP_stack_value:
851 /* Expected behaviour is that this is the last instruction of this
852 * expression and just the "top of stack" value should be put to loc->offset. */
853 break;
854 default:
855 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
856 FIXME("Unhandled attr op: %x\n", op);
857 /* FIXME else unhandled extension */
858 return loc_err_internal;
861 loc->offset = stack[stk];
862 return 0;
865 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
866 const dwarf2_debug_info_t* di,
867 unsigned long dw,
868 struct location* loc,
869 const struct location* frame)
871 struct attribute xloc;
873 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
875 switch (xloc.form)
877 case DW_FORM_data1: case DW_FORM_data2:
878 case DW_FORM_udata: case DW_FORM_sdata:
879 loc->kind = loc_absolute;
880 loc->reg = 0;
881 loc->offset = xloc.u.uvalue;
882 return TRUE;
883 case DW_FORM_data4: case DW_FORM_data8:
884 loc->kind = loc_dwarf2_location_list;
885 loc->reg = Wine_DW_no_register;
886 loc->offset = xloc.u.uvalue;
887 return TRUE;
888 case DW_FORM_block:
889 case DW_FORM_block1:
890 case DW_FORM_block2:
891 case DW_FORM_block4:
892 break;
893 default: FIXME("Unsupported yet form %lx\n", xloc.form);
894 return FALSE;
897 /* assume we have a block form */
899 if (xloc.u.block.size)
901 dwarf2_traverse_context_t lctx;
902 enum location_error err;
904 lctx.data = xloc.u.block.ptr;
905 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
906 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
908 err = compute_location(&lctx, loc, NULL, frame);
909 if (err < 0)
911 loc->kind = loc_error;
912 loc->reg = err;
914 else if (loc->kind == loc_dwarf2_block)
916 unsigned* ptr = pool_alloc(&ctx->module->pool,
917 sizeof(unsigned) + xloc.u.block.size);
918 *ptr = xloc.u.block.size;
919 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
920 loc->offset = (unsigned long)ptr;
923 return TRUE;
926 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
927 const dwarf2_debug_info_t* di)
929 struct attribute attr;
931 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
933 dwarf2_debug_info_t* type;
935 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
936 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
937 if (!type->symt)
939 /* load the debug info entity */
940 dwarf2_load_one_entry(ctx, type, NULL);
942 return type->symt;
944 return NULL;
947 /******************************************************************
948 * dwarf2_read_range
950 * read a range for a given debug_info (either using AT_range attribute, in which
951 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
952 * in all cases, range is relative to beginning of compilation unit
954 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
955 unsigned long* plow, unsigned long* phigh)
957 struct attribute range;
959 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
961 dwarf2_traverse_context_t traverse;
962 unsigned long low, high;
964 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
965 traverse.end_data = ctx->sections[section_ranges].address +
966 ctx->sections[section_ranges].size;
967 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
969 *plow = ULONG_MAX;
970 *phigh = 0;
971 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
973 low = dwarf2_parse_addr(&traverse);
974 high = dwarf2_parse_addr(&traverse);
975 if (low == 0 && high == 0) break;
976 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
977 if (low < *plow) *plow = low;
978 if (high > *phigh) *phigh = high;
980 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
981 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
983 return TRUE;
985 else
987 struct attribute low_pc;
988 struct attribute high_pc;
990 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
991 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
993 FIXME("missing low or high value\n");
994 return FALSE;
996 *plow = low_pc.u.uvalue;
997 *phigh = high_pc.u.uvalue;
998 return TRUE;
1002 /******************************************************************
1003 * dwarf2_read_one_debug_info
1005 * Loads into memory one debug info entry, and recursively its children (if any)
1007 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1008 dwarf2_traverse_context_t* traverse,
1009 dwarf2_debug_info_t** pdi)
1011 const dwarf2_abbrev_entry_t*abbrev;
1012 unsigned long entry_code;
1013 unsigned long offset;
1014 dwarf2_debug_info_t* di;
1015 dwarf2_debug_info_t* child;
1016 dwarf2_debug_info_t** where;
1017 dwarf2_abbrev_entry_attr_t* attr;
1018 unsigned i;
1019 struct attribute sibling;
1021 offset = traverse->data - ctx->sections[ctx->section].address;
1022 entry_code = dwarf2_leb128_as_unsigned(traverse);
1023 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1024 if (!entry_code)
1026 *pdi = NULL;
1027 return TRUE;
1029 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1030 if (!abbrev)
1032 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1033 return FALSE;
1035 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1036 if (!di) return FALSE;
1037 di->abbrev = abbrev;
1038 di->symt = NULL;
1040 if (abbrev->num_attr)
1042 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1043 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1045 di->data[i] = traverse->data;
1046 dwarf2_swallow_attribute(traverse, attr);
1049 else di->data = NULL;
1050 if (abbrev->have_child)
1052 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1053 while (traverse->data < traverse->end_data)
1055 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
1056 if (!child) break;
1057 where = vector_add(&di->children, &ctx->pool);
1058 if (!where) return FALSE;
1059 *where = child;
1062 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1063 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1065 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1066 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1067 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1069 *pdi = di;
1070 return TRUE;
1073 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1074 dwarf2_debug_info_t* di)
1076 struct attribute name;
1077 struct attribute size;
1078 struct attribute encoding;
1079 enum BasicType bt;
1080 int cache_idx = -1;
1081 if (di->symt) return di->symt;
1083 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1085 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1086 name.u.string = NULL;
1087 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1088 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1090 switch (encoding.u.uvalue)
1092 case DW_ATE_void: bt = btVoid; break;
1093 case DW_ATE_address: bt = btULong; break;
1094 case DW_ATE_boolean: bt = btBool; break;
1095 case DW_ATE_complex_float: bt = btComplex; break;
1096 case DW_ATE_float: bt = btFloat; break;
1097 case DW_ATE_signed: bt = btInt; break;
1098 case DW_ATE_unsigned: bt = btUInt; break;
1099 case DW_ATE_signed_char: bt = btChar; break;
1100 case DW_ATE_unsigned_char: bt = btChar; break;
1101 default: bt = btNoType; break;
1103 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1104 switch (bt)
1106 case btVoid:
1107 assert(size.u.uvalue == 0);
1108 cache_idx = sc_void;
1109 break;
1110 case btInt:
1111 switch (size.u.uvalue)
1113 case 1: cache_idx = sc_int1; break;
1114 case 2: cache_idx = sc_int2; break;
1115 case 4: cache_idx = sc_int4; break;
1117 break;
1118 default: break;
1120 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1121 ctx->symt_cache[cache_idx] = di->symt;
1123 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1124 return di->symt;
1127 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1128 dwarf2_debug_info_t* di)
1130 struct symt* ref_type;
1131 struct attribute name;
1133 if (di->symt) return di->symt;
1135 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1137 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1138 ref_type = dwarf2_lookup_type(ctx, di);
1140 if (name.u.string)
1141 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1142 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1143 return di->symt;
1146 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1147 dwarf2_debug_info_t* di)
1149 struct symt* ref_type;
1150 struct attribute size;
1152 if (di->symt) return di->symt;
1154 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1156 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1157 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1159 ref_type = ctx->symt_cache[sc_void];
1160 assert(ref_type);
1162 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1163 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1164 return di->symt;
1167 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1168 dwarf2_debug_info_t* di)
1170 struct symt* ref_type;
1171 struct symt* idx_type = NULL;
1172 struct attribute min, max, cnt;
1173 dwarf2_debug_info_t* child;
1174 unsigned int i;
1176 if (di->symt) return di->symt;
1178 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1180 ref_type = dwarf2_lookup_type(ctx, di);
1182 if (!di->abbrev->have_child)
1184 /* fake an array with unknown size */
1185 /* FIXME: int4 even on 64bit machines??? */
1186 idx_type = ctx->symt_cache[sc_int4];
1187 min.u.uvalue = 0;
1188 max.u.uvalue = -1;
1190 else for (i = 0; i < vector_length(&di->children); i++)
1192 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1193 switch (child->abbrev->tag)
1195 case DW_TAG_subrange_type:
1196 idx_type = dwarf2_lookup_type(ctx, child);
1197 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1198 min.u.uvalue = 0;
1199 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1200 max.u.uvalue = 0;
1201 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1202 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1203 break;
1204 default:
1205 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1206 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1207 break;
1210 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1211 return di->symt;
1214 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1215 dwarf2_debug_info_t* di)
1217 struct symt* ref_type;
1219 if (di->symt) return di->symt;
1221 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1223 ref_type = dwarf2_lookup_type(ctx, di);
1224 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1225 di->symt = ref_type;
1227 return ref_type;
1230 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1231 dwarf2_debug_info_t* di)
1233 struct symt* ref_type;
1235 if (di->symt) return di->symt;
1237 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1239 ref_type = dwarf2_lookup_type(ctx, di);
1240 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1241 di->symt = ref_type;
1243 return ref_type;
1246 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1247 dwarf2_debug_info_t* di)
1249 struct symt* ref_type = NULL;
1251 if (di->symt) return di->symt;
1253 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1255 ref_type = dwarf2_lookup_type(ctx, di);
1256 /* FIXME: for now, we hard-wire C++ references to pointers */
1257 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1259 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1261 return di->symt;
1264 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1265 const dwarf2_debug_info_t* di,
1266 struct symt_udt* parent)
1268 struct symt* elt_type;
1269 struct attribute name;
1270 struct attribute bit_size;
1271 struct attribute bit_offset;
1272 struct location loc;
1274 assert(parent);
1276 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1278 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1279 elt_type = dwarf2_lookup_type(ctx, di);
1280 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1282 if (loc.kind != loc_absolute)
1284 FIXME("Found register, while not expecting it\n");
1285 loc.offset = 0;
1287 else
1288 TRACE("found member_location at %s -> %lu\n",
1289 dwarf2_debug_ctx(ctx), loc.offset);
1291 else
1292 loc.offset = 0;
1293 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1294 bit_size.u.uvalue = 0;
1295 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1297 /* FIXME: we should only do this when implementation is LSB (which is
1298 * the case on i386 processors)
1300 struct attribute nbytes;
1301 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1303 DWORD64 size;
1304 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1305 (unsigned long)size : 0;
1307 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1309 else bit_offset.u.uvalue = 0;
1310 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1311 (loc.offset << 3) + bit_offset.u.uvalue,
1312 bit_size.u.uvalue);
1314 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1317 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1318 dwarf2_debug_info_t* di,
1319 enum UdtKind udt)
1321 struct attribute name;
1322 struct attribute size;
1324 if (di->symt) return di->symt;
1326 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1328 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1329 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1331 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1333 if (di->abbrev->have_child) /** any interest to not have child ? */
1335 dwarf2_debug_info_t* child;
1336 unsigned int i;
1338 for (i=0; i<vector_length(&di->children); i++)
1340 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1342 switch (child->abbrev->tag)
1344 case DW_TAG_member:
1345 /* FIXME: should I follow the sibling stuff ?? */
1346 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1347 break;
1348 case DW_TAG_enumeration_type:
1349 dwarf2_parse_enumeration_type(ctx, child);
1350 break;
1351 case DW_TAG_structure_type:
1352 case DW_TAG_class_type:
1353 case DW_TAG_union_type:
1354 case DW_TAG_typedef:
1355 /* FIXME: we need to handle nested udt definitions */
1356 case DW_TAG_inheritance:
1357 case DW_TAG_subprogram:
1358 case DW_TAG_template_type_param:
1359 case DW_TAG_template_value_param:
1360 case DW_TAG_variable:
1361 /* FIXME: some C++ related stuff */
1362 break;
1363 default:
1364 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1365 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1366 break;
1371 return di->symt;
1374 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1375 const dwarf2_debug_info_t* di,
1376 struct symt_enum* parent)
1378 struct attribute name;
1379 struct attribute value;
1381 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1383 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1384 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1385 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1387 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1390 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1391 dwarf2_debug_info_t* di)
1393 struct attribute name;
1394 struct attribute size;
1395 struct symt_basic* basetype;
1397 if (di->symt) return di->symt;
1399 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1401 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1402 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1404 switch (size.u.uvalue) /* FIXME: that's wrong */
1406 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1407 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1408 default:
1409 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1412 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1414 if (di->abbrev->have_child) /* any interest to not have child ? */
1416 dwarf2_debug_info_t* child;
1417 unsigned int i;
1419 /* FIXME: should we use the sibling stuff ?? */
1420 for (i=0; i<vector_length(&di->children); i++)
1422 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1424 switch (child->abbrev->tag)
1426 case DW_TAG_enumerator:
1427 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1428 break;
1429 default:
1430 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1431 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1435 return di->symt;
1438 /* structure used to pass information around when parsing a subprogram */
1439 typedef struct dwarf2_subprogram_s
1441 dwarf2_parse_context_t* ctx;
1442 struct symt_compiland* compiland;
1443 struct symt_function* func;
1444 BOOL non_computed_variable;
1445 struct location frame;
1446 } dwarf2_subprogram_t;
1448 /******************************************************************
1449 * dwarf2_parse_variable
1451 * Parses any variable (parameter, local/global variable)
1453 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1454 struct symt_block* block,
1455 dwarf2_debug_info_t* di)
1457 struct symt* param_type;
1458 struct attribute name, value;
1459 struct location loc;
1460 BOOL is_pmt;
1462 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1464 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1465 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1467 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1468 /* cannot do much without the name, the functions below won't like it. */
1469 return;
1471 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1472 &loc, &subpgm->frame))
1474 struct attribute ext;
1476 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1477 name.u.string, loc.kind, loc.offset, loc.reg,
1478 dwarf2_debug_ctx(subpgm->ctx));
1480 switch (loc.kind)
1482 case loc_error:
1483 break;
1484 case loc_absolute:
1485 /* it's a global variable */
1486 /* FIXME: we don't handle its scope yet */
1487 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1488 ext.u.uvalue = 0;
1489 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1490 name.u.string, !ext.u.uvalue,
1491 subpgm->ctx->load_offset + loc.offset,
1492 0, param_type);
1493 break;
1494 default:
1495 subpgm->non_computed_variable = TRUE;
1496 /* fall through */
1497 case loc_register:
1498 case loc_regrel:
1499 /* either a pmt/variable relative to frame pointer or
1500 * pmt/variable in a register
1502 assert(subpgm->func);
1503 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1504 is_pmt ? DataIsParam : DataIsLocal,
1505 &loc, block, param_type, name.u.string);
1506 break;
1509 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1511 VARIANT v;
1512 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1513 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1514 switch (value.form)
1516 case DW_FORM_data1:
1517 case DW_FORM_data2:
1518 case DW_FORM_data4:
1519 case DW_FORM_udata:
1520 case DW_FORM_addr:
1521 v.n1.n2.vt = VT_UI4;
1522 v.n1.n2.n3.lVal = value.u.uvalue;
1523 break;
1525 case DW_FORM_data8:
1526 v.n1.n2.vt = VT_UI8;
1527 v.n1.n2.n3.llVal = value.u.lluvalue;
1528 break;
1530 case DW_FORM_sdata:
1531 v.n1.n2.vt = VT_I4;
1532 v.n1.n2.n3.lVal = value.u.svalue;
1533 break;
1535 case DW_FORM_strp:
1536 case DW_FORM_string:
1537 /* FIXME: native doesn't report const strings from here !!
1538 * however, the value of the string is in the code somewhere
1540 v.n1.n2.vt = VT_I1 | VT_BYREF;
1541 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1542 break;
1544 case DW_FORM_block:
1545 case DW_FORM_block1:
1546 case DW_FORM_block2:
1547 case DW_FORM_block4:
1548 v.n1.n2.vt = VT_I4;
1549 switch (value.u.block.size)
1551 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1552 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1553 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1554 default:
1555 v.n1.n2.vt = VT_I1 | VT_BYREF;
1556 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1557 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1559 break;
1561 default:
1562 FIXME("Unsupported form for const value %s (%lx)\n",
1563 name.u.string, value.form);
1564 v.n1.n2.vt = VT_EMPTY;
1566 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->compiland,
1567 name.u.string, param_type, &v)->symt;
1569 else
1571 /* variable has been optimiezd away... report anyway */
1572 loc.kind = loc_error;
1573 loc.reg = loc_err_no_location;
1574 if (subpgm->func)
1576 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1577 is_pmt ? DataIsParam : DataIsLocal,
1578 &loc, block, param_type, name.u.string);
1580 else
1582 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1585 if (is_pmt && subpgm->func && subpgm->func->type)
1586 symt_add_function_signature_parameter(subpgm->ctx->module,
1587 (struct symt_function_signature*)subpgm->func->type,
1588 param_type);
1590 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1593 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1594 const dwarf2_debug_info_t* di)
1596 struct attribute name;
1597 struct attribute low_pc;
1598 struct location loc;
1600 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1602 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1603 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1604 name.u.string = NULL;
1606 loc.kind = loc_absolute;
1607 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1608 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1609 &loc, name.u.string);
1612 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1613 struct symt_block* parent_block,
1614 const dwarf2_debug_info_t* di);
1616 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1617 struct symt_block* parent_block,
1618 const dwarf2_debug_info_t* di)
1620 struct symt_block* block;
1621 unsigned long low_pc, high_pc;
1623 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1625 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1627 FIXME("cannot read range\n");
1628 return;
1631 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1632 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1633 high_pc - low_pc);
1635 if (di->abbrev->have_child) /** any interest to not have child ? */
1637 dwarf2_debug_info_t* child;
1638 unsigned int i;
1640 for (i=0; i<vector_length(&di->children); i++)
1642 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1644 switch (child->abbrev->tag)
1646 case DW_TAG_formal_parameter:
1647 case DW_TAG_variable:
1648 dwarf2_parse_variable(subpgm, block, child);
1649 break;
1650 case DW_TAG_lexical_block:
1651 dwarf2_parse_subprogram_block(subpgm, block, child);
1652 break;
1653 case DW_TAG_inlined_subroutine:
1654 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1655 break;
1656 case DW_TAG_label:
1657 dwarf2_parse_subprogram_label(subpgm, child);
1658 break;
1659 default:
1660 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1661 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1662 dwarf2_debug_di(di));
1666 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1669 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1670 struct symt_block* parent_block,
1671 const dwarf2_debug_info_t* di)
1673 struct symt_block* block;
1674 unsigned long low_pc, high_pc;
1676 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1678 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1680 FIXME("no range\n");
1681 return;
1684 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1685 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1686 high_pc - low_pc);
1688 if (di->abbrev->have_child) /** any interest to not have child ? */
1690 dwarf2_debug_info_t* child;
1691 unsigned int i;
1693 for (i=0; i<vector_length(&di->children); i++)
1695 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1697 switch (child->abbrev->tag)
1699 case DW_TAG_inlined_subroutine:
1700 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1701 break;
1702 case DW_TAG_variable:
1703 dwarf2_parse_variable(subpgm, block, child);
1704 break;
1705 case DW_TAG_lexical_block:
1706 dwarf2_parse_subprogram_block(subpgm, block, child);
1707 break;
1708 case DW_TAG_subprogram:
1709 /* FIXME: likely a declaration (to be checked)
1710 * skip it for now
1712 break;
1713 case DW_TAG_formal_parameter:
1714 /* FIXME: likely elements for exception handling (GCC flavor)
1715 * Skip it for now
1717 break;
1718 case DW_TAG_label:
1719 dwarf2_parse_subprogram_label(subpgm, child);
1720 break;
1721 case DW_TAG_class_type:
1722 case DW_TAG_structure_type:
1723 case DW_TAG_union_type:
1724 case DW_TAG_enumeration_type:
1725 case DW_TAG_typedef:
1726 /* the type referred to will be loaded when we need it, so skip it */
1727 break;
1728 default:
1729 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1730 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1735 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1738 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1739 dwarf2_debug_info_t* di,
1740 struct symt_compiland* compiland)
1742 struct attribute name;
1743 unsigned long low_pc, high_pc;
1744 struct attribute is_decl;
1745 struct attribute inline_flags;
1746 struct symt* ret_type;
1747 struct symt_function_signature* sig_type;
1748 dwarf2_subprogram_t subpgm;
1750 if (di->symt) return di->symt;
1752 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1754 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1756 WARN("No name for function... dropping function\n");
1757 return NULL;
1759 /* if it's an abstract representation of an inline function, there should be
1760 * a concrete object that we'll handle
1762 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags))
1764 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1765 name.u.string ? name.u.string : "(null)", inline_flags.u.uvalue);
1766 return NULL;
1769 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1771 FIXME("cannot get range\n");
1772 return NULL;
1775 /* As functions (defined as inline assembly) get debug info with dwarf
1776 * (not the case for stabs), we just drop Wine's thunks here...
1777 * Actual thunks will be created in elf_module from the symbol table
1779 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1780 return NULL;
1781 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl))
1782 is_decl.u.uvalue = 0;
1784 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1786 ret_type = ctx->symt_cache[sc_void];
1787 assert(ret_type);
1790 /* FIXME: assuming C source code */
1791 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1792 if (!is_decl.u.uvalue)
1794 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1795 ctx->load_offset + low_pc, high_pc - low_pc,
1796 &sig_type->symt);
1797 di->symt = &subpgm.func->symt;
1799 else subpgm.func = NULL;
1801 subpgm.ctx = ctx;
1802 subpgm.compiland = compiland;
1803 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1804 &subpgm.frame, NULL))
1806 /* on stack !! */
1807 subpgm.frame.kind = loc_regrel;
1808 subpgm.frame.reg = 0;
1809 subpgm.frame.offset = 0;
1811 subpgm.non_computed_variable = FALSE;
1813 if (di->abbrev->have_child) /** any interest to not have child ? */
1815 dwarf2_debug_info_t* child;
1816 unsigned int i;
1818 for (i=0; i<vector_length(&di->children); i++)
1820 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1822 switch (child->abbrev->tag)
1824 case DW_TAG_variable:
1825 case DW_TAG_formal_parameter:
1826 dwarf2_parse_variable(&subpgm, NULL, child);
1827 break;
1828 case DW_TAG_lexical_block:
1829 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1830 break;
1831 case DW_TAG_inlined_subroutine:
1832 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1833 break;
1834 case DW_TAG_subprogram:
1835 /* FIXME: likely a declaration (to be checked)
1836 * skip it for now
1838 break;
1839 case DW_TAG_label:
1840 dwarf2_parse_subprogram_label(&subpgm, child);
1841 break;
1842 case DW_TAG_class_type:
1843 case DW_TAG_structure_type:
1844 case DW_TAG_union_type:
1845 case DW_TAG_enumeration_type:
1846 case DW_TAG_typedef:
1847 /* the type referred to will be loaded when we need it, so skip it */
1848 break;
1849 case DW_TAG_unspecified_parameters:
1850 case DW_TAG_template_type_param:
1851 case DW_TAG_template_value_param:
1852 /* FIXME: no support in dbghelp's internals so far */
1853 break;
1854 default:
1855 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1856 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1861 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
1863 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
1864 &subpgm.frame, NULL);
1866 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
1868 return di->symt;
1871 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1872 dwarf2_debug_info_t* di)
1874 struct symt* ret_type;
1875 struct symt_function_signature* sig_type;
1877 if (di->symt) return di->symt;
1879 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1881 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1883 ret_type = ctx->symt_cache[sc_void];
1884 assert(ret_type);
1887 /* FIXME: assuming C source code */
1888 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1890 if (di->abbrev->have_child) /** any interest to not have child ? */
1892 dwarf2_debug_info_t* child;
1893 unsigned int i;
1895 for (i=0; i<vector_length(&di->children); i++)
1897 child = *(dwarf2_debug_info_t**)vector_at(&di->children, i);
1899 switch (child->abbrev->tag)
1901 case DW_TAG_formal_parameter:
1902 symt_add_function_signature_parameter(ctx->module, sig_type,
1903 dwarf2_lookup_type(ctx, child));
1904 break;
1905 case DW_TAG_unspecified_parameters:
1906 WARN("Unsupported unspecified parameters\n");
1907 break;
1912 return di->symt = &sig_type->symt;
1915 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1916 dwarf2_debug_info_t* di,
1917 struct symt_compiland* compiland)
1919 switch (di->abbrev->tag)
1921 case DW_TAG_typedef:
1922 dwarf2_parse_typedef(ctx, di);
1923 break;
1924 case DW_TAG_base_type:
1925 dwarf2_parse_base_type(ctx, di);
1926 break;
1927 case DW_TAG_pointer_type:
1928 dwarf2_parse_pointer_type(ctx, di);
1929 break;
1930 case DW_TAG_class_type:
1931 dwarf2_parse_udt_type(ctx, di, UdtClass);
1932 break;
1933 case DW_TAG_structure_type:
1934 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1935 break;
1936 case DW_TAG_union_type:
1937 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1938 break;
1939 case DW_TAG_array_type:
1940 dwarf2_parse_array_type(ctx, di);
1941 break;
1942 case DW_TAG_const_type:
1943 dwarf2_parse_const_type(ctx, di);
1944 break;
1945 case DW_TAG_volatile_type:
1946 dwarf2_parse_volatile_type(ctx, di);
1947 break;
1948 case DW_TAG_reference_type:
1949 dwarf2_parse_reference_type(ctx, di);
1950 break;
1951 case DW_TAG_enumeration_type:
1952 dwarf2_parse_enumeration_type(ctx, di);
1953 break;
1954 case DW_TAG_subprogram:
1955 dwarf2_parse_subprogram(ctx, di, compiland);
1956 break;
1957 case DW_TAG_subroutine_type:
1958 dwarf2_parse_subroutine_type(ctx, di);
1959 break;
1960 case DW_TAG_variable:
1962 dwarf2_subprogram_t subpgm;
1964 subpgm.ctx = ctx;
1965 subpgm.compiland = compiland;
1966 subpgm.func = NULL;
1967 subpgm.frame.kind = loc_absolute;
1968 subpgm.frame.offset = 0;
1969 subpgm.frame.reg = Wine_DW_no_register;
1970 dwarf2_parse_variable(&subpgm, NULL, di);
1972 break;
1973 /* silence a couple of C++ defines */
1974 case DW_TAG_namespace:
1975 case DW_TAG_imported_module:
1976 case DW_TAG_imported_declaration:
1977 break;
1978 default:
1979 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1980 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1984 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1985 const struct vector* v, unsigned file, unsigned line)
1987 struct symt_function* func;
1988 struct symt_ht* symt;
1989 unsigned* psrc;
1991 if (!file || !(psrc = vector_at(v, file - 1))) return;
1993 TRACE("%s %lx %s %u\n",
1994 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
1995 if (!(symt = symt_find_nearest(module, address)) ||
1996 symt->symt.tag != SymTagFunction) return;
1997 func = (struct symt_function*)symt;
1998 symt_add_func_line(module, func, *psrc, line, address - func->address);
2001 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2002 dwarf2_parse_context_t* ctx,
2003 const char* compile_dir,
2004 unsigned long offset)
2006 dwarf2_traverse_context_t traverse;
2007 unsigned long length;
2008 unsigned version, header_len, insn_size, default_stmt;
2009 unsigned line_range, opcode_base;
2010 int line_base;
2011 const unsigned char* opcode_len;
2012 struct vector dirs;
2013 struct vector files;
2014 const char** p;
2016 /* section with line numbers stripped */
2017 if (sections[section_line].address == IMAGE_NO_MAP)
2018 return FALSE;
2020 traverse.data = sections[section_line].address + offset;
2021 traverse.end_data = traverse.data + 4;
2022 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2024 length = dwarf2_parse_u4(&traverse);
2025 traverse.end_data = sections[section_line].address + offset + length;
2027 version = dwarf2_parse_u2(&traverse);
2028 header_len = dwarf2_parse_u4(&traverse);
2029 insn_size = dwarf2_parse_byte(&traverse);
2030 default_stmt = dwarf2_parse_byte(&traverse);
2031 line_base = (signed char)dwarf2_parse_byte(&traverse);
2032 line_range = dwarf2_parse_byte(&traverse);
2033 opcode_base = dwarf2_parse_byte(&traverse);
2035 opcode_len = traverse.data;
2036 traverse.data += opcode_base - 1;
2038 vector_init(&dirs, sizeof(const char*), 4);
2039 p = vector_add(&dirs, &ctx->pool);
2040 *p = compile_dir ? compile_dir : ".";
2041 while (*traverse.data)
2043 const char* rel = (const char*)traverse.data;
2044 unsigned rellen = strlen(rel);
2045 TRACE("Got include %s\n", rel);
2046 traverse.data += rellen + 1;
2047 p = vector_add(&dirs, &ctx->pool);
2049 if (*rel == '/' || !compile_dir)
2050 *p = rel;
2051 else
2053 /* include directory relative to compile directory */
2054 unsigned baselen = strlen(compile_dir);
2055 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2056 strcpy(tmp, compile_dir);
2057 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2058 strcpy(&tmp[baselen], rel);
2059 *p = tmp;
2063 traverse.data++;
2065 vector_init(&files, sizeof(unsigned), 16);
2066 while (*traverse.data)
2068 unsigned int dir_index, mod_time, length;
2069 const char* name;
2070 const char* dir;
2071 unsigned* psrc;
2073 name = (const char*)traverse.data;
2074 traverse.data += strlen(name) + 1;
2075 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2076 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2077 length = dwarf2_leb128_as_unsigned(&traverse);
2078 dir = *(const char**)vector_at(&dirs, dir_index);
2079 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
2080 psrc = vector_add(&files, &ctx->pool);
2081 *psrc = source_new(ctx->module, dir, name);
2083 traverse.data++;
2085 while (traverse.data < traverse.end_data)
2087 unsigned long address = 0;
2088 unsigned file = 1;
2089 unsigned line = 1;
2090 unsigned is_stmt = default_stmt;
2091 BOOL end_sequence = FALSE;
2092 unsigned opcode, extopcode, i;
2094 while (!end_sequence)
2096 opcode = dwarf2_parse_byte(&traverse);
2097 TRACE("Got opcode %x\n", opcode);
2099 if (opcode >= opcode_base)
2101 unsigned delta = opcode - opcode_base;
2103 address += (delta / line_range) * insn_size;
2104 line += line_base + (delta % line_range);
2105 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2107 else
2109 switch (opcode)
2111 case DW_LNS_copy:
2112 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2113 break;
2114 case DW_LNS_advance_pc:
2115 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2116 break;
2117 case DW_LNS_advance_line:
2118 line += dwarf2_leb128_as_signed(&traverse);
2119 break;
2120 case DW_LNS_set_file:
2121 file = dwarf2_leb128_as_unsigned(&traverse);
2122 break;
2123 case DW_LNS_set_column:
2124 dwarf2_leb128_as_unsigned(&traverse);
2125 break;
2126 case DW_LNS_negate_stmt:
2127 is_stmt = !is_stmt;
2128 break;
2129 case DW_LNS_set_basic_block:
2130 break;
2131 case DW_LNS_const_add_pc:
2132 address += ((255 - opcode_base) / line_range) * insn_size;
2133 break;
2134 case DW_LNS_fixed_advance_pc:
2135 address += dwarf2_parse_u2(&traverse);
2136 break;
2137 case DW_LNS_extended_op:
2138 dwarf2_leb128_as_unsigned(&traverse);
2139 extopcode = dwarf2_parse_byte(&traverse);
2140 switch (extopcode)
2142 case DW_LNE_end_sequence:
2143 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2144 end_sequence = TRUE;
2145 break;
2146 case DW_LNE_set_address:
2147 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2148 break;
2149 case DW_LNE_define_file:
2150 FIXME("not handled %s\n", traverse.data);
2151 traverse.data += strlen((const char *)traverse.data) + 1;
2152 dwarf2_leb128_as_unsigned(&traverse);
2153 dwarf2_leb128_as_unsigned(&traverse);
2154 dwarf2_leb128_as_unsigned(&traverse);
2155 break;
2156 case DW_LNE_set_discriminator:
2157 WARN("not handled %s\n", traverse.data);
2158 dwarf2_leb128_as_unsigned(&traverse);
2159 break;
2160 default:
2161 FIXME("Unsupported extended opcode %x\n", extopcode);
2162 break;
2164 break;
2165 default:
2166 WARN("Unsupported opcode %x\n", opcode);
2167 for (i = 0; i < opcode_len[opcode]; i++)
2168 dwarf2_leb128_as_unsigned(&traverse);
2169 break;
2174 return TRUE;
2177 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2178 struct module* module,
2179 const struct elf_thunk_area* thunks,
2180 dwarf2_traverse_context_t* mod_ctx,
2181 unsigned long load_offset)
2183 dwarf2_parse_context_t ctx;
2184 dwarf2_traverse_context_t abbrev_ctx;
2185 dwarf2_debug_info_t* di;
2186 dwarf2_traverse_context_t cu_ctx;
2187 const unsigned char* comp_unit_start = mod_ctx->data;
2188 unsigned long cu_length;
2189 unsigned short cu_version;
2190 unsigned long cu_abbrev_offset;
2191 BOOL ret = FALSE;
2193 cu_length = dwarf2_parse_u4(mod_ctx);
2194 cu_ctx.data = mod_ctx->data;
2195 cu_ctx.end_data = mod_ctx->data + cu_length;
2196 mod_ctx->data += cu_length;
2197 cu_version = dwarf2_parse_u2(&cu_ctx);
2198 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2199 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2201 TRACE("Compilation Unit Header found at 0x%x:\n",
2202 (int)(comp_unit_start - sections[section_debug].address));
2203 TRACE("- length: %lu\n", cu_length);
2204 TRACE("- version: %u\n", cu_version);
2205 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2206 TRACE("- word_size: %u\n", cu_ctx.word_size);
2208 if (cu_version != 2)
2210 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2211 cu_version);
2212 return FALSE;
2215 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2216 mod_ctx->word_size = cu_ctx.word_size;
2218 pool_init(&ctx.pool, 65536);
2219 ctx.sections = sections;
2220 ctx.section = section_debug;
2221 ctx.module = module;
2222 ctx.thunks = thunks;
2223 ctx.load_offset = load_offset;
2224 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2225 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2226 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2228 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2229 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2230 abbrev_ctx.word_size = cu_ctx.word_size;
2231 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2233 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2234 dwarf2_read_one_debug_info(&ctx, &cu_ctx, &di);
2236 if (di->abbrev->tag == DW_TAG_compile_unit)
2238 struct attribute name;
2239 dwarf2_debug_info_t** pdi = NULL;
2240 struct attribute stmt_list, low_pc;
2241 struct attribute comp_dir;
2243 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2244 name.u.string = NULL;
2246 /* get working directory of current compilation unit */
2247 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2248 comp_dir.u.string = NULL;
2250 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2251 low_pc.u.uvalue = 0;
2252 di->symt = &symt_new_compiland(module,
2253 ctx.load_offset + low_pc.u.uvalue,
2254 source_new(module, comp_dir.u.string, name.u.string))->symt;
2256 if (di->abbrev->have_child)
2258 unsigned int i;
2259 for (i=0; i<vector_length(&di->children); i++)
2261 pdi = vector_at(&di->children, i);
2262 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
2265 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2267 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2268 module->module.LineNumbers = TRUE;
2270 ret = TRUE;
2272 else FIXME("Should have a compilation unit here\n");
2273 pool_destroy(&ctx.pool);
2274 return ret;
2277 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2278 unsigned long ip, dwarf2_traverse_context_t* lctx)
2280 DWORD_PTR beg, end;
2281 const BYTE* ptr = start;
2282 DWORD len;
2284 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2286 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2287 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2288 if (!beg && !end) break;
2289 len = dwarf2_get_u2(ptr); ptr += 2;
2291 if (beg <= ip && ip < end)
2293 lctx->data = ptr;
2294 lctx->end_data = ptr + len;
2295 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2296 return TRUE;
2298 ptr += len;
2300 WARN("Couldn't find ip in location list\n");
2301 return FALSE;
2304 static enum location_error loc_compute_frame(struct process* pcs,
2305 const struct module_format* modfmt,
2306 const struct symt_function* func,
2307 DWORD_PTR ip, struct location* frame)
2309 struct symt** psym = NULL;
2310 struct location* pframe;
2311 dwarf2_traverse_context_t lctx;
2312 enum location_error err;
2313 unsigned int i;
2315 for (i=0; i<vector_length(&func->vchildren); i++)
2317 psym = vector_at(&func->vchildren, i);
2318 if ((*psym)->tag == SymTagCustom)
2320 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2322 /* First, recompute the frame information, if needed */
2323 switch (pframe->kind)
2325 case loc_regrel:
2326 case loc_register:
2327 *frame = *pframe;
2328 break;
2329 case loc_dwarf2_location_list:
2330 WARN("Searching loclist for %s\n", func->hash_elt.name);
2331 if (!dwarf2_lookup_loclist(modfmt,
2332 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2333 ip, &lctx))
2334 return loc_err_out_of_scope;
2335 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2336 if (frame->kind >= loc_user)
2338 WARN("Couldn't compute runtime frame location\n");
2339 return loc_err_too_complex;
2341 break;
2342 default:
2343 WARN("Unsupported frame kind %d\n", pframe->kind);
2344 return loc_err_internal;
2346 return 0;
2349 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2350 return loc_err_internal;
2353 enum reg_rule
2355 RULE_UNSET, /* not set at all */
2356 RULE_UNDEFINED, /* undefined value */
2357 RULE_SAME, /* same value as previous frame */
2358 RULE_CFA_OFFSET, /* stored at cfa offset */
2359 RULE_OTHER_REG, /* stored in other register */
2360 RULE_EXPRESSION, /* address specified by expression */
2361 RULE_VAL_EXPRESSION /* value specified by expression */
2364 /* make it large enough for all CPUs */
2365 #define NB_FRAME_REGS 64
2366 #define MAX_SAVED_STATES 16
2368 struct frame_state
2370 ULONG_PTR cfa_offset;
2371 unsigned char cfa_reg;
2372 enum reg_rule cfa_rule;
2373 enum reg_rule rules[NB_FRAME_REGS];
2374 ULONG_PTR regs[NB_FRAME_REGS];
2377 struct frame_info
2379 ULONG_PTR ip;
2380 ULONG_PTR code_align;
2381 LONG_PTR data_align;
2382 unsigned char retaddr_reg;
2383 unsigned char fde_encoding;
2384 unsigned char lsda_encoding;
2385 unsigned char signal_frame;
2386 unsigned char aug_z_format;
2387 unsigned char state_sp;
2388 struct frame_state state;
2389 struct frame_state state_stack[MAX_SAVED_STATES];
2392 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2394 ULONG_PTR base;
2396 if (encoding == DW_EH_PE_omit) return 0;
2398 switch (encoding & 0xf0)
2400 case DW_EH_PE_abs:
2401 base = 0;
2402 break;
2403 case DW_EH_PE_pcrel:
2404 base = (ULONG_PTR)ctx->data;
2405 break;
2406 default:
2407 FIXME("unsupported encoding %02x\n", encoding);
2408 return 0;
2411 switch (encoding & 0x0f)
2413 case DW_EH_PE_native:
2414 return base + dwarf2_parse_addr(ctx);
2415 case DW_EH_PE_leb128:
2416 return base + dwarf2_leb128_as_unsigned(ctx);
2417 case DW_EH_PE_data2:
2418 return base + dwarf2_parse_u2(ctx);
2419 case DW_EH_PE_data4:
2420 return base + dwarf2_parse_u4(ctx);
2421 case DW_EH_PE_data8:
2422 return base + dwarf2_parse_u8(ctx);
2423 case DW_EH_PE_signed|DW_EH_PE_leb128:
2424 return base + dwarf2_leb128_as_signed(ctx);
2425 case DW_EH_PE_signed|DW_EH_PE_data2:
2426 return base + (signed short)dwarf2_parse_u2(ctx);
2427 case DW_EH_PE_signed|DW_EH_PE_data4:
2428 return base + (signed int)dwarf2_parse_u4(ctx);
2429 case DW_EH_PE_signed|DW_EH_PE_data8:
2430 return base + (LONG64)dwarf2_parse_u8(ctx);
2431 default:
2432 FIXME("unsupported encoding %02x\n", encoding);
2433 return 0;
2437 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2439 unsigned char version;
2440 const char* augmentation;
2441 const unsigned char* end;
2442 ULONG_PTR len;
2444 memset(info, 0, sizeof(*info));
2445 info->lsda_encoding = DW_EH_PE_omit;
2446 info->aug_z_format = 0;
2448 /* parse the CIE first */
2449 version = dwarf2_parse_byte(ctx);
2450 if (version != 1)
2452 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2453 return FALSE;
2455 augmentation = (const char*)ctx->data;
2456 ctx->data += strlen(augmentation) + 1;
2458 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2459 info->data_align = dwarf2_leb128_as_signed(ctx);
2460 info->retaddr_reg = dwarf2_parse_byte(ctx);
2461 info->state.cfa_rule = RULE_CFA_OFFSET;
2463 end = NULL;
2464 TRACE("\tparsing augmentation %s\n", augmentation);
2465 if (*augmentation) do
2467 switch (*augmentation)
2469 case 'z':
2470 len = dwarf2_leb128_as_unsigned(ctx);
2471 end = ctx->data + len;
2472 info->aug_z_format = 1;
2473 continue;
2474 case 'L':
2475 info->lsda_encoding = dwarf2_parse_byte(ctx);
2476 continue;
2477 case 'P':
2479 unsigned char encoding = dwarf2_parse_byte(ctx);
2480 /* throw away the indirect bit, as we don't care for the result */
2481 encoding &= ~DW_EH_PE_indirect;
2482 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2483 continue;
2485 case 'R':
2486 info->fde_encoding = dwarf2_parse_byte(ctx);
2487 continue;
2488 case 'S':
2489 info->signal_frame = 1;
2490 continue;
2492 FIXME("unknown augmentation '%c'\n", *augmentation);
2493 if (!end) return FALSE;
2494 break;
2495 } while (*++augmentation);
2496 if (end) ctx->data = end;
2497 return TRUE;
2500 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2501 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2502 struct frame_info* info, BOOL in_eh_frame)
2504 const unsigned char* ptr_blk;
2505 const unsigned char* cie_ptr;
2506 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2507 unsigned len, id;
2508 unsigned long start, range;
2509 unsigned cie_id;
2510 const BYTE* start_data = fde_ctx->data;
2512 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2513 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2515 /* find the FDE for address addr (skip CIE) */
2516 len = dwarf2_parse_u4(fde_ctx);
2517 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2518 ptr_blk = fde_ctx->data + len;
2519 id = dwarf2_parse_u4(fde_ctx);
2520 if (id == cie_id)
2522 last_cie_ptr = fde_ctx->data - 8;
2523 /* we need some bits out of the CIE in order to parse all contents */
2524 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2525 cie_ctx->data = fde_ctx->data;
2526 cie_ctx->end_data = ptr_blk;
2527 cie_ctx->word_size = fde_ctx->word_size;
2528 continue;
2530 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2531 if (cie_ptr != last_cie_ptr)
2533 last_cie_ptr = cie_ptr;
2534 cie_ctx->data = cie_ptr;
2535 cie_ctx->word_size = fde_ctx->word_size;
2536 cie_ctx->end_data = cie_ptr + 4;
2537 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2538 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2540 FIXME("wrong CIE pointer\n");
2541 return FALSE;
2543 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2545 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2546 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2548 if (addr >= start && addr < start + range)
2550 /* reset the FDE context */
2551 fde_ctx->end_data = ptr_blk;
2553 info->ip = start;
2554 return TRUE;
2557 return FALSE;
2560 static int valid_reg(ULONG_PTR reg)
2562 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2563 return (reg < NB_FRAME_REGS);
2566 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2567 ULONG_PTR last_ip, struct frame_info *info)
2569 while (ctx->data < ctx->end_data && info->ip < last_ip + info->signal_frame)
2571 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2573 if (op & 0xc0)
2575 switch (op & 0xc0)
2577 case DW_CFA_advance_loc:
2579 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2580 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2581 info->ip += offset;
2582 break;
2584 case DW_CFA_offset:
2586 ULONG_PTR reg = op & 0x3f;
2587 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2588 if (!valid_reg(reg)) break;
2589 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2590 info->ip,
2591 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2592 offset);
2593 info->state.regs[reg] = offset;
2594 info->state.rules[reg] = RULE_CFA_OFFSET;
2595 break;
2597 case DW_CFA_restore:
2599 ULONG_PTR reg = op & 0x3f;
2600 if (!valid_reg(reg)) break;
2601 TRACE("%lx: DW_CFA_restore %s\n",
2602 info->ip,
2603 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2604 info->state.rules[reg] = RULE_UNSET;
2605 break;
2609 else switch (op)
2611 case DW_CFA_nop:
2612 break;
2613 case DW_CFA_set_loc:
2615 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2616 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2617 info->ip = loc;
2618 break;
2620 case DW_CFA_advance_loc1:
2622 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2623 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2624 info->ip += offset;
2625 break;
2627 case DW_CFA_advance_loc2:
2629 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2630 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2631 info->ip += offset;
2632 break;
2634 case DW_CFA_advance_loc4:
2636 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2637 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2638 info->ip += offset;
2639 break;
2641 case DW_CFA_offset_extended:
2642 case DW_CFA_offset_extended_sf:
2644 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2645 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2646 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2647 if (!valid_reg(reg)) break;
2648 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2649 info->ip,
2650 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2651 offset);
2652 info->state.regs[reg] = offset;
2653 info->state.rules[reg] = RULE_CFA_OFFSET;
2654 break;
2656 case DW_CFA_restore_extended:
2658 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2659 if (!valid_reg(reg)) break;
2660 TRACE("%lx: DW_CFA_restore_extended %s\n",
2661 info->ip,
2662 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2663 info->state.rules[reg] = RULE_UNSET;
2664 break;
2666 case DW_CFA_undefined:
2668 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2669 if (!valid_reg(reg)) break;
2670 TRACE("%lx: DW_CFA_undefined %s\n",
2671 info->ip,
2672 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2673 info->state.rules[reg] = RULE_UNDEFINED;
2674 break;
2676 case DW_CFA_same_value:
2678 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2679 if (!valid_reg(reg)) break;
2680 TRACE("%lx: DW_CFA_same_value %s\n",
2681 info->ip,
2682 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2683 info->state.regs[reg] = reg;
2684 info->state.rules[reg] = RULE_SAME;
2685 break;
2687 case DW_CFA_register:
2689 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2690 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2691 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2692 TRACE("%lx: DW_CFA_register %s == %s\n",
2693 info->ip,
2694 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2695 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2)));
2696 info->state.regs[reg] = reg2;
2697 info->state.rules[reg] = RULE_OTHER_REG;
2698 break;
2700 case DW_CFA_remember_state:
2701 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2702 if (info->state_sp >= MAX_SAVED_STATES)
2703 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2704 else
2705 info->state_stack[info->state_sp++] = info->state;
2706 break;
2707 case DW_CFA_restore_state:
2708 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2709 if (!info->state_sp)
2710 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2711 else
2712 info->state = info->state_stack[--info->state_sp];
2713 break;
2714 case DW_CFA_def_cfa:
2715 case DW_CFA_def_cfa_sf:
2717 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2718 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2719 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2720 if (!valid_reg(reg)) break;
2721 TRACE("%lx: DW_CFA_def_cfa %s, %lu\n",
2722 info->ip,
2723 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2724 offset);
2725 info->state.cfa_reg = reg;
2726 info->state.cfa_offset = offset;
2727 info->state.cfa_rule = RULE_CFA_OFFSET;
2728 break;
2730 case DW_CFA_def_cfa_register:
2732 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2733 if (!valid_reg(reg)) break;
2734 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2735 info->ip,
2736 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)));
2737 info->state.cfa_reg = reg;
2738 info->state.cfa_rule = RULE_CFA_OFFSET;
2739 break;
2741 case DW_CFA_def_cfa_offset:
2742 case DW_CFA_def_cfa_offset_sf:
2744 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2745 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2746 TRACE("%lx: DW_CFA_def_cfa_offset %lu\n", info->ip, offset);
2747 info->state.cfa_offset = offset;
2748 info->state.cfa_rule = RULE_CFA_OFFSET;
2749 break;
2751 case DW_CFA_def_cfa_expression:
2753 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2754 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2755 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2756 info->state.cfa_offset = expr;
2757 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2758 ctx->data += len;
2759 break;
2761 case DW_CFA_expression:
2762 case DW_CFA_val_expression:
2764 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2765 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2766 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2767 if (!valid_reg(reg)) break;
2768 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
2769 info->ip, (op == DW_CFA_expression) ? "" : "val_",
2770 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg)),
2771 expr, expr + len);
2772 info->state.regs[reg] = expr;
2773 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
2774 ctx->data += len;
2775 break;
2777 case DW_CFA_GNU_args_size:
2778 /* FIXME: should check that GCC is the compiler for this CU */
2780 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
2781 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
2782 /* ignored */
2783 break;
2785 default:
2786 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
2787 break;
2792 /* retrieve a context register from its dwarf number */
2793 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
2795 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2796 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2798 if (sz != sizeof(ULONG_PTR))
2800 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2801 return 0;
2803 return *ptr;
2806 /* set a context register from its dwarf number */
2807 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
2808 ULONG_PTR val, BOOL isdebuggee)
2810 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg), sz;
2811 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
2813 if (isdebuggee)
2815 char tmp[16];
2817 if (sz > sizeof(tmp))
2819 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
2820 return;
2822 if (!sw_read_mem(csw, val, tmp, sz))
2824 WARN("Couldn't read memory at %p\n", (void*)val);
2825 return;
2827 memcpy(ptr, tmp, sz);
2829 else
2831 if (sz != sizeof(ULONG_PTR))
2833 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
2834 return;
2836 *ptr = val;
2840 /* copy a register from one context to another using dwarf number */
2841 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
2843 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst), szdst;
2844 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc), szsrc;
2845 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
2846 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
2848 if (szdst != szsrc)
2850 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
2851 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
2852 return;
2854 memcpy(ptrdst, ptrsrc, szdst);
2857 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
2858 const unsigned char* zp, CONTEXT *context)
2860 dwarf2_traverse_context_t ctx;
2861 ULONG_PTR reg, sz, tmp, stack[64];
2862 int sp = -1;
2863 ULONG_PTR len;
2865 ctx.data = zp;
2866 ctx.end_data = zp + 4;
2867 len = dwarf2_leb128_as_unsigned(&ctx);
2868 ctx.end_data = ctx.data + len;
2869 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2871 while (ctx.data < ctx.end_data)
2873 unsigned char opcode = dwarf2_parse_byte(&ctx);
2875 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
2876 stack[++sp] = opcode - DW_OP_lit0;
2877 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
2878 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
2879 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
2880 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
2881 else switch (opcode)
2883 case DW_OP_nop: break;
2884 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
2885 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
2886 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
2887 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
2888 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
2889 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
2890 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
2891 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
2892 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
2893 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
2894 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
2895 case DW_OP_deref:
2896 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
2898 ERR("Couldn't read memory at %lx\n", stack[sp]);
2899 tmp = 0;
2901 stack[sp] = tmp;
2902 break;
2903 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
2904 case DW_OP_drop: sp--; break;
2905 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
2906 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
2907 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
2908 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
2909 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
2910 case DW_OP_neg: stack[sp] = -stack[sp]; break;
2911 case DW_OP_not: stack[sp] = ~stack[sp]; break;
2912 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
2913 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
2914 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
2915 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
2916 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
2917 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
2918 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
2919 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
2920 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
2921 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
2922 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
2923 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
2924 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
2925 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
2926 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
2927 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
2928 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
2929 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
2930 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
2931 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
2932 case DW_OP_GNU_encoded_addr:
2933 tmp = dwarf2_parse_byte(&ctx);
2934 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
2935 break;
2936 case DW_OP_regx:
2937 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
2938 break;
2939 case DW_OP_bregx:
2940 reg = dwarf2_leb128_as_unsigned(&ctx);
2941 tmp = dwarf2_leb128_as_signed(&ctx);
2942 stack[++sp] = get_context_reg(context, reg) + tmp;
2943 break;
2944 case DW_OP_deref_size:
2945 sz = dwarf2_parse_byte(&ctx);
2946 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
2948 ERR("Couldn't read memory at %lx\n", stack[sp]);
2949 tmp = 0;
2951 /* do integral promotion */
2952 switch (sz)
2954 case 1: stack[sp] = *(unsigned char*)&tmp; break;
2955 case 2: stack[sp] = *(unsigned short*)&tmp; break;
2956 case 4: stack[sp] = *(unsigned int*)&tmp; break;
2957 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
2958 default: FIXME("Unknown size for deref 0x%lx\n", sz);
2960 break;
2961 default:
2962 FIXME("unhandled opcode %02x\n", opcode);
2965 return stack[sp];
2968 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
2969 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
2971 unsigned int i;
2972 ULONG_PTR value;
2973 CONTEXT new_context = *context;
2975 switch (state->cfa_rule)
2977 case RULE_EXPRESSION:
2978 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
2979 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
2981 WARN("Couldn't read memory at %p\n", (void*)*cfa);
2982 return;
2984 break;
2985 case RULE_VAL_EXPRESSION:
2986 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
2987 break;
2988 default:
2989 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
2990 break;
2992 if (!*cfa) return;
2994 for (i = 0; i < NB_FRAME_REGS; i++)
2996 switch (state->rules[i])
2998 case RULE_UNSET:
2999 case RULE_UNDEFINED:
3000 case RULE_SAME:
3001 break;
3002 case RULE_CFA_OFFSET:
3003 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3004 break;
3005 case RULE_OTHER_REG:
3006 copy_context_reg(&new_context, i, context, state->regs[i]);
3007 break;
3008 case RULE_EXPRESSION:
3009 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3010 set_context_reg(csw, &new_context, i, value, TRUE);
3011 break;
3012 case RULE_VAL_EXPRESSION:
3013 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3014 set_context_reg(csw, &new_context, i, value, FALSE);
3015 break;
3018 *context = new_context;
3021 /***********************************************************************
3022 * dwarf2_virtual_unwind
3025 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3027 struct module_pair pair;
3028 struct frame_info info;
3029 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3030 struct module_format* modfmt;
3031 const unsigned char* end;
3032 DWORD_PTR delta;
3034 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3035 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3036 !module_get_debug(&pair))
3037 return FALSE;
3038 modfmt = pair.effective->format_info[DFI_DWARF];
3039 if (!modfmt) return FALSE;
3040 memset(&info, 0, sizeof(info));
3041 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3042 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3043 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3044 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3045 * in this process the IMAGE section at a different address as the one expected by
3046 * the image
3048 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3049 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3050 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3052 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3053 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3054 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3055 delta = pair.effective->reloc_delta;
3056 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3058 TRACE("Couldn't find information for %lx\n", ip);
3059 return FALSE;
3063 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3064 ip, info.ip, info.code_align, info.data_align,
3065 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg)));
3067 /* if at very beginning of function, return and use default unwinder */
3068 if (ip == info.ip) return FALSE;
3069 execute_cfa_instructions(&cie_ctx, ip, &info);
3071 if (info.aug_z_format) /* get length of augmentation data */
3073 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3074 end = fde_ctx.data + len;
3076 else end = NULL;
3077 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3078 if (end) fde_ctx.data = end;
3080 execute_cfa_instructions(&fde_ctx, ip, &info);
3081 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3083 return TRUE;
3086 static void dwarf2_location_compute(struct process* pcs,
3087 const struct module_format* modfmt,
3088 const struct symt_function* func,
3089 struct location* loc)
3091 struct location frame;
3092 DWORD_PTR ip;
3093 int err;
3094 dwarf2_traverse_context_t lctx;
3096 if (!func->container || func->container->tag != SymTagCompiland)
3098 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3099 err = loc_err_internal;
3101 else
3103 /* instruction pointer relative to compiland's start */
3104 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3106 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3108 switch (loc->kind)
3110 case loc_dwarf2_location_list:
3111 /* Then, if the variable has a location list, find it !! */
3112 if (dwarf2_lookup_loclist(modfmt,
3113 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3114 ip, &lctx))
3115 goto do_compute;
3116 err = loc_err_out_of_scope;
3117 break;
3118 case loc_dwarf2_block:
3119 /* or if we have a copy of an existing block, get ready for it */
3121 unsigned* ptr = (unsigned*)loc->offset;
3123 lctx.data = (const BYTE*)(ptr + 1);
3124 lctx.end_data = lctx.data + *ptr;
3125 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3127 do_compute:
3128 /* now get the variable */
3129 err = compute_location(&lctx, loc, pcs->handle, &frame);
3130 break;
3131 case loc_register:
3132 case loc_regrel:
3133 /* nothing to do */
3134 break;
3135 default:
3136 WARN("Unsupported local kind %d\n", loc->kind);
3137 err = loc_err_internal;
3141 if (err < 0)
3143 loc->kind = loc_register;
3144 loc->reg = err;
3148 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
3150 HeapFree(GetProcessHeap(), 0, modfmt);
3153 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
3154 const char* sectname, struct image_section_map* ism)
3156 struct image_section_map local_ism;
3158 if (!ism) ism = &local_ism;
3159 if (!image_find_section(fmap, sectname, ism))
3161 section->address = NULL;
3162 section->size = 0;
3163 section->rva = 0;
3164 return FALSE;
3167 section->address = (const BYTE*)image_map_section(ism);
3168 section->size = image_get_map_size(ism);
3169 section->rva = image_get_map_rva(ism);
3170 return TRUE;
3173 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
3174 const struct elf_thunk_area* thunks,
3175 struct image_file_map* fmap)
3177 dwarf2_section_t eh_frame, section[section_max];
3178 dwarf2_traverse_context_t mod_ctx;
3179 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
3180 debug_line_sect, debug_ranges_sect, eh_frame_sect;
3181 BOOL ret = TRUE;
3182 struct module_format* dwarf2_modfmt;
3184 dwarf2_init_section(&eh_frame, fmap, ".eh_frame", &eh_frame_sect);
3185 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", &debug_sect);
3186 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", &debug_abbrev_sect);
3187 dwarf2_init_section(&section[section_string], fmap, ".debug_str", &debug_str_sect);
3188 dwarf2_init_section(&section[section_line], fmap, ".debug_line", &debug_line_sect);
3189 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", &debug_ranges_sect);
3191 /* to do anything useful we need either .eh_frame or .debug_info */
3192 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
3193 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
3195 ret = FALSE;
3196 goto leave;
3199 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
3201 /* debug info might have a different base address than .so file
3202 * when elf file is prelinked after splitting off debug info
3203 * adjust symbol base addresses accordingly
3205 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
3208 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->module.ModuleName));
3210 mod_ctx.data = section[section_debug].address;
3211 mod_ctx.end_data = mod_ctx.data + section[section_debug].size;
3212 mod_ctx.word_size = 0; /* will be correctly set later on */
3214 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
3215 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
3216 if (!dwarf2_modfmt)
3218 ret = FALSE;
3219 goto leave;
3221 dwarf2_modfmt->module = module;
3222 dwarf2_modfmt->remove = dwarf2_module_remove;
3223 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
3224 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
3225 dwarf2_modfmt->u.dwarf2_info->word_size = 0; /* will be correctly set later on */
3226 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
3228 /* As we'll need later some sections' content, we won't unmap these
3229 * sections upon existing this function
3231 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", NULL);
3232 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", NULL);
3233 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
3235 while (mod_ctx.data < mod_ctx.end_data)
3237 dwarf2_parse_compilation_unit(section, dwarf2_modfmt->module, thunks, &mod_ctx, load_offset);
3239 dwarf2_modfmt->module->module.SymType = SymDia;
3240 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
3241 /* FIXME: we could have a finer grain here */
3242 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
3243 dwarf2_modfmt->module->module.TypeInfo = TRUE;
3244 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
3245 dwarf2_modfmt->module->module.Publics = TRUE;
3247 leave:
3248 image_unmap_section(&debug_sect);
3249 image_unmap_section(&debug_abbrev_sect);
3250 image_unmap_section(&debug_str_sect);
3251 image_unmap_section(&debug_line_sect);
3252 image_unmap_section(&debug_ranges_sect);
3253 if (!ret) image_unmap_section(&eh_frame_sect);
3255 return ret;