rpcrt4: Use I_Rpc{Allocate, Free} instead of Heap{Alloc, Free} as that
[wine/testsucceed.git] / dlls / dbghelp / dwarf.c
blobb1c8572eb13ecef21f6f6f809f11c92dc1e3f977
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <stdio.h>
39 #ifndef PATH_MAX
40 #define PATH_MAX MAX_PATH
41 #endif
42 #include <assert.h>
43 #include <stdarg.h>
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winreg.h"
48 #include "winnls.h"
50 #include "dbghelp_private.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
56 /* FIXME:
57 * - Functions:
58 * o unspecified parameters
59 * o inlined functions
60 * o Debug{Start|End}Point
61 * o CFA
62 * - Udt
63 * o proper types loading (nesting)
66 #if 0
67 static void dump(const void* ptr, unsigned len)
69 int i, j;
70 BYTE msg[128];
71 static const char hexof[] = "0123456789abcdef";
72 const BYTE* x = (const BYTE*)ptr;
74 for (i = 0; i < len; i += 16)
76 sprintf(msg, "%08x: ", i);
77 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
78 for (j = 0; j < min(16, len - i); j++)
80 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
81 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
82 msg[10 + 3 * j + 2] = ' ';
83 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
84 x[i + j] : '.';
86 msg[10 + 3 * 16] = ' ';
87 msg[10 + 3 * 16 + 1 + 16] = '\0';
88 TRACE("%s\n", msg);
91 #endif
93 /**
95 * Main Specs:
96 * http://www.eagercon.com/dwarf/dwarf3std.htm
97 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
99 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
101 * example of projects who do dwarf2 parsing:
102 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
103 * http://elis.ugent.be/diota/log/ltrace_elf.c
105 #include "dwarf.h"
108 * Parsers
111 typedef struct dwarf2_abbrev_entry_attr_s
113 unsigned long attribute;
114 unsigned long form;
115 struct dwarf2_abbrev_entry_attr_s* next;
116 } dwarf2_abbrev_entry_attr_t;
118 typedef struct dwarf2_abbrev_entry_s
120 unsigned long entry_code;
121 unsigned long tag;
122 unsigned char have_child;
123 unsigned num_attr;
124 dwarf2_abbrev_entry_attr_t* attrs;
125 } dwarf2_abbrev_entry_t;
127 struct dwarf2_block
129 unsigned size;
130 const unsigned char* ptr;
133 struct attribute
135 unsigned long form;
136 union
138 unsigned long uvalue;
139 long svalue;
140 const char* string;
141 struct dwarf2_block block;
142 } u;
145 typedef struct dwarf2_debug_info_s
147 const dwarf2_abbrev_entry_t*abbrev;
148 struct symt* symt;
149 const unsigned char** data;
150 struct vector children;
151 } dwarf2_debug_info_t;
154 typedef struct dwarf2_section_s
156 const unsigned char* address;
157 unsigned size;
158 } dwarf2_section_t;
160 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
162 typedef struct dwarf2_traverse_context_s
164 const unsigned char* data;
165 const unsigned char* start_data;
166 const unsigned char* end_data;
167 unsigned char word_size;
168 } dwarf2_traverse_context_t;
170 typedef struct dwarf2_parse_context_s
172 const dwarf2_section_t* sections;
173 unsigned section;
174 struct pool pool;
175 struct module* module;
176 const struct elf_thunk_area*thunks;
177 struct sparse_array abbrev_table;
178 struct sparse_array debug_info_table;
179 unsigned long ref_offset;
180 unsigned char word_size;
181 } dwarf2_parse_context_t;
183 /* forward declarations */
184 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
186 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
188 return *ptr;
191 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
193 unsigned char uvalue = dwarf2_get_byte(ctx->data);
194 ctx->data += 1;
195 return uvalue;
198 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
200 return *(const unsigned short*)ptr;
203 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
205 unsigned short uvalue = dwarf2_get_u2(ctx->data);
206 ctx->data += 2;
207 return uvalue;
210 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
212 return *(const unsigned long*)ptr;
215 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
217 unsigned long uvalue = dwarf2_get_u4(ctx->data);
218 ctx->data += 4;
219 return uvalue;
222 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
224 unsigned long ret = 0;
225 unsigned char byte;
226 unsigned shift = 0;
230 byte = dwarf2_get_byte(ptr++);
231 ret |= (byte & 0x7f) << shift;
232 shift += 7;
233 } while (byte & 0x80);
235 if (end) *end = ptr;
236 return ret;
239 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
241 unsigned long ret;
243 assert(ctx);
245 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
247 return ret;
250 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
252 long ret = 0;
253 unsigned char byte;
254 unsigned shift = 0;
255 const unsigned size = sizeof(int) * 8;
259 byte = dwarf2_get_byte(ptr++);
260 ret |= (byte & 0x7f) << shift;
261 shift += 7;
262 } while (byte & 0x80);
263 if (end) *end = ptr;
265 /* as spec: sign bit of byte is 2nd high order bit (80x40)
266 * -> 0x80 is used as flag.
268 if ((shift < size) && (byte & 0x40))
270 ret |= - (1 << shift);
272 return ret;
275 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
277 long ret = 0;
279 assert(ctx);
281 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
282 return ret;
285 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
287 unsigned ret;
288 for (ret = 0; ctx->data[ret] & 0x80; ret++);
289 return ret + 1;
292 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
294 unsigned long ret;
296 switch (word_size)
298 case 4:
299 ret = dwarf2_get_u4(ptr);
300 break;
301 default:
302 FIXME("Unsupported Word Size %u\n", word_size);
303 ret = 0;
305 return ret;
308 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
310 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
311 ctx->data += ctx->word_size;
312 return ret;
315 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
317 return wine_dbg_sprintf("ctx(%p)", ctx->data);
320 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
322 return wine_dbg_sprintf("ctx(%p,%s)", ctx, ctx->module->module.ModuleName);
325 static const char* dwarf2_debug_di(dwarf2_debug_info_t* di)
327 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
328 di->abbrev, di->symt);
331 static dwarf2_abbrev_entry_t*
332 dwarf2_abbrev_table_find_entry(struct sparse_array* abbrev_table,
333 unsigned long entry_code)
335 assert( NULL != abbrev_table );
336 return sparse_array_find(abbrev_table, entry_code);
339 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
340 struct sparse_array* abbrev_table,
341 struct pool* pool)
343 unsigned long entry_code;
344 dwarf2_abbrev_entry_t* abbrev_entry;
345 dwarf2_abbrev_entry_attr_t* new = NULL;
346 dwarf2_abbrev_entry_attr_t* last = NULL;
347 unsigned long attribute;
348 unsigned long form;
350 assert( NULL != abbrev_ctx );
352 TRACE("%s, end at %p\n",
353 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
355 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
356 while (abbrev_ctx->data < abbrev_ctx->end_data)
358 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
359 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
360 TRACE("found entry_code %lu\n", entry_code);
361 if (!entry_code)
363 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
364 break;
366 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
367 assert( NULL != abbrev_entry );
369 abbrev_entry->entry_code = entry_code;
370 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
371 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
372 abbrev_entry->attrs = NULL;
373 abbrev_entry->num_attr = 0;
375 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
376 abbrev_table, sparse_array_length(abbrev_table),
377 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
379 last = NULL;
380 while (1)
382 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
383 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
384 if (!attribute) break;
386 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
387 assert(new);
389 new->attribute = attribute;
390 new->form = form;
391 new->next = NULL;
392 if (abbrev_entry->attrs) last->next = new;
393 else abbrev_entry->attrs = new;
394 last = new;
395 abbrev_entry->num_attr++;
398 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
401 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
402 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
404 unsigned step;
406 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
408 switch (abbrev_attr->form)
410 case DW_FORM_ref_addr:
411 case DW_FORM_addr: step = ctx->word_size; break;
412 case DW_FORM_flag:
413 case DW_FORM_data1:
414 case DW_FORM_ref1: step = 1; break;
415 case DW_FORM_data2:
416 case DW_FORM_ref2: step = 2; break;
417 case DW_FORM_data4:
418 case DW_FORM_ref4:
419 case DW_FORM_strp: step = 4; break;
420 case DW_FORM_data8:
421 case DW_FORM_ref8: step = 8; break;
422 case DW_FORM_sdata:
423 case DW_FORM_ref_udata:
424 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
425 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
426 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
427 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
428 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
429 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
430 default:
431 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
432 return;
434 ctx->data += step;
437 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
438 const dwarf2_debug_info_t* di,
439 unsigned at, struct attribute* attr)
441 unsigned i;
442 dwarf2_abbrev_entry_attr_t* abbrev_attr;
444 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
446 if (abbrev_attr->attribute == at)
448 attr->form = abbrev_attr->form;
449 switch (attr->form)
451 case DW_FORM_ref_addr:
452 case DW_FORM_addr:
453 attr->u.uvalue = dwarf2_get_addr(di->data[i], ctx->word_size);
454 TRACE("addr<0x%lx>\n", attr->u.uvalue);
455 break;
457 case DW_FORM_flag:
458 attr->u.uvalue = dwarf2_get_byte(di->data[i]);
459 TRACE("flag<0x%lx>\n", attr->u.uvalue);
460 break;
462 case DW_FORM_data1:
463 attr->u.uvalue = dwarf2_get_byte(di->data[i]);
464 TRACE("data1<%lu>\n", attr->u.uvalue);
465 break;
467 case DW_FORM_data2:
468 attr->u.uvalue = dwarf2_get_u2(di->data[i]);
469 TRACE("data2<%lu>\n", attr->u.uvalue);
470 break;
472 case DW_FORM_data4:
473 attr->u.uvalue = dwarf2_get_u4(di->data[i]);
474 TRACE("data4<%lu>\n", attr->u.uvalue);
475 break;
477 case DW_FORM_data8:
478 FIXME("Unhandled 64bits support\n");
479 break;
481 case DW_FORM_ref1:
482 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(di->data[i]);
483 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
484 break;
486 case DW_FORM_ref2:
487 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(di->data[i]);
488 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
489 break;
491 case DW_FORM_ref4:
492 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(di->data[i]);
493 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
494 break;
496 case DW_FORM_ref8:
497 FIXME("Unhandled 64 bit support\n");
498 break;
500 case DW_FORM_sdata:
501 attr->u.svalue = dwarf2_get_leb128_as_signed(di->data[i], NULL);
502 break;
504 case DW_FORM_ref_udata:
505 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(di->data[i], NULL);
506 break;
508 case DW_FORM_udata:
509 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(di->data[i], NULL);
510 break;
512 case DW_FORM_string:
513 attr->u.string = (const char *)di->data[i];
514 TRACE("string<%s>\n", attr->u.string);
515 break;
517 case DW_FORM_strp:
519 unsigned long offset = dwarf2_get_u4(di->data[i]);
520 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
522 TRACE("strp<%s>\n", attr->u.string);
523 break;
524 case DW_FORM_block:
525 attr->u.block.size = dwarf2_get_leb128_as_unsigned(di->data[i], &attr->u.block.ptr);
526 break;
528 case DW_FORM_block1:
529 attr->u.block.size = dwarf2_get_byte(di->data[i]);
530 attr->u.block.ptr = di->data[i] + 1;
531 break;
533 case DW_FORM_block2:
534 attr->u.block.size = dwarf2_get_u2(di->data[i]);
535 attr->u.block.ptr = di->data[i] + 2;
536 break;
538 case DW_FORM_block4:
539 attr->u.block.size = dwarf2_get_u4(di->data[i]);
540 attr->u.block.ptr = di->data[i] + 4;
541 break;
543 default:
544 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
545 break;
547 return TRUE;
550 return FALSE;
553 static void dwarf2_find_name(dwarf2_parse_context_t* ctx,
554 const dwarf2_debug_info_t* di,
555 struct attribute* attr, const char* pfx)
557 static int index;
559 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, attr))
561 char* tmp = pool_alloc(&ctx->pool, strlen(pfx) + 16);
562 if (tmp) sprintf(tmp, "%s_%d", pfx, index++);
563 attr->u.string = tmp;
567 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
568 struct symt_compiland*);
570 #define Wine_DW_no_register 0x7FFFFFFF
571 #define Wine_DW_frame_register 0x7FFFFFFE
572 #define Wine_DW_register_deref 0x80000000
574 static BOOL dwarf2_compute_location(dwarf2_parse_context_t* ctx,
575 dwarf2_debug_info_t* di,
576 unsigned long dw,
577 unsigned long* offset, int* in_register)
579 unsigned long loc[64];
580 unsigned stk;
581 struct attribute xloc;
583 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
585 if (in_register) *in_register = Wine_DW_no_register;
587 switch (xloc.form)
589 case DW_FORM_data1: case DW_FORM_data2:
590 case DW_FORM_data4: case DW_FORM_data8:
591 case DW_FORM_udata: case DW_FORM_sdata:
592 /* we've got a constant */
593 *offset = xloc.u.uvalue;
594 return TRUE;
596 /* assume we have a block form */
597 loc[stk = 0] = 0;
599 if (xloc.u.block.size)
601 dwarf2_traverse_context_t lctx;
602 unsigned char op;
603 BOOL piece_found = FALSE;
605 lctx.data = xloc.u.block.ptr;
606 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
607 lctx.word_size = ctx->word_size;
609 while (lctx.data < lctx.end_data)
611 op = dwarf2_parse_byte(&lctx);
612 switch (op)
614 case DW_OP_addr: loc[++stk] = dwarf2_parse_addr(&lctx); break;
615 case DW_OP_const1u: loc[++stk] = dwarf2_parse_byte(&lctx); break;
616 case DW_OP_const1s: loc[++stk] = (long)(signed char)dwarf2_parse_byte(&lctx); break;
617 case DW_OP_const2u: loc[++stk] = dwarf2_parse_u2(&lctx); break;
618 case DW_OP_const2s: loc[++stk] = (long)(short)dwarf2_parse_u2(&lctx); break;
619 case DW_OP_const4u: loc[++stk] = dwarf2_parse_u4(&lctx); break;
620 case DW_OP_const4s: loc[++stk] = dwarf2_parse_u4(&lctx); break;
621 case DW_OP_constu: loc[++stk] = dwarf2_leb128_as_unsigned(&lctx); break;
622 case DW_OP_consts: loc[++stk] = dwarf2_leb128_as_signed(&lctx); break;
623 case DW_OP_plus_uconst:
624 loc[stk] += dwarf2_leb128_as_unsigned(&lctx); break;
625 case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
626 case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
627 case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
628 case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
629 case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
630 case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
631 case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
632 case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
633 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
634 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
635 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
636 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
637 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
638 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
639 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
640 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:
641 if (in_register)
643 /* dbghelp APIs don't know how to cope with this anyway
644 * (for example 'long long' stored in two registers)
645 * FIXME: We should tell winedbg how to deal with it (sigh)
647 if (!piece_found || (op - DW_OP_reg0 != *in_register + 1))
649 if (*in_register != Wine_DW_no_register)
650 FIXME("Only supporting one reg (%d -> %d)\n",
651 *in_register, op - DW_OP_reg0);
652 *in_register = op - DW_OP_reg0;
655 else FIXME("Found register, while not expecting it\n");
656 if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
658 loc[++stk] = dwarf2_leb128_as_signed(&lctx);
659 if (in_register) *in_register |= Wine_DW_register_deref;
661 break;
662 case DW_OP_fbreg:
663 if (in_register)
665 if (*in_register != Wine_DW_no_register)
666 FIXME("Only supporting one reg (%d -> -2)\n", *in_register);
667 *in_register = Wine_DW_frame_register | Wine_DW_register_deref;
669 else FIXME("Found register, while not expecting it\n");
670 loc[++stk] = dwarf2_leb128_as_signed(&lctx);
671 break;
672 case DW_OP_piece:
674 unsigned sz = dwarf2_leb128_as_unsigned(&lctx);
675 WARN("Not handling OP_piece directly (size=%d)\n", sz);
676 piece_found = TRUE;
678 break;
679 default:
680 FIXME("Unhandled attr op: %x\n", op);
681 return loc[stk];
685 *offset = loc[stk];
686 return TRUE;
689 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
690 const dwarf2_debug_info_t* di)
692 struct attribute attr;
694 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
696 dwarf2_debug_info_t* type;
698 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
699 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
700 if (!type->symt)
702 /* load the debug info entity */
703 dwarf2_load_one_entry(ctx, type, NULL);
705 return type->symt;
707 return NULL;
710 /******************************************************************
711 * dwarf2_read_one_debug_info
713 * Loads into memory one debug info entry, and recursively its children (if any)
715 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
716 dwarf2_traverse_context_t* traverse,
717 dwarf2_debug_info_t** pdi)
719 const dwarf2_abbrev_entry_t*abbrev;
720 unsigned long entry_code;
721 unsigned long offset;
722 dwarf2_debug_info_t* di;
723 dwarf2_debug_info_t* child;
724 dwarf2_debug_info_t** where;
725 dwarf2_abbrev_entry_attr_t* attr;
726 unsigned i;
727 struct attribute sibling;
729 offset = traverse->data - ctx->sections[ctx->section].address;
730 entry_code = dwarf2_leb128_as_unsigned(traverse);
731 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
732 if (!entry_code)
734 *pdi = NULL;
735 return TRUE;
737 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
738 if (!abbrev)
740 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
741 return FALSE;
743 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
744 if (!di) return FALSE;
745 di->abbrev = abbrev;
746 di->symt = NULL;
748 if (abbrev->num_attr)
750 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
751 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
753 di->data[i] = traverse->data;
754 dwarf2_swallow_attribute(traverse, attr);
757 else di->data = NULL;
758 if (abbrev->have_child)
760 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
761 while (traverse->data < traverse->end_data)
763 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
764 if (!child) break;
765 where = vector_add(&di->children, &ctx->pool);
766 if (!where) return FALSE;
767 *where = child;
770 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
771 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
773 WARN("setting cursor for %s to next sibling <0x%lx>\n",
774 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
775 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
777 *pdi = di;
778 return TRUE;
781 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
782 dwarf2_debug_info_t* di)
784 struct attribute name;
785 struct attribute size;
786 struct attribute encoding;
787 enum BasicType bt;
789 if (di->symt) return di->symt;
791 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
793 dwarf2_find_name(ctx, di, &name, "base_type");
794 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
795 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
797 switch (encoding.u.uvalue)
799 case DW_ATE_void: bt = btVoid; break;
800 case DW_ATE_address: bt = btULong; break;
801 case DW_ATE_boolean: bt = btBool; break;
802 case DW_ATE_complex_float: bt = btComplex; break;
803 case DW_ATE_float: bt = btFloat; break;
804 case DW_ATE_signed: bt = btInt; break;
805 case DW_ATE_unsigned: bt = btUInt; break;
806 case DW_ATE_signed_char: bt = btChar; break;
807 case DW_ATE_unsigned_char: bt = btChar; break;
808 default: bt = btNoType; break;
810 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
811 if (di->abbrev->have_child) FIXME("Unsupported children\n");
812 return di->symt;
815 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
816 dwarf2_debug_info_t* di)
818 struct symt* ref_type;
819 struct attribute name;
821 if (di->symt) return di->symt;
823 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
825 dwarf2_find_name(ctx, di, &name, "typedef");
826 ref_type = dwarf2_lookup_type(ctx, di);
828 if (name.u.string)
830 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
832 if (di->abbrev->have_child) FIXME("Unsupported children\n");
833 return di->symt;
836 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
837 dwarf2_debug_info_t* di)
839 struct symt* ref_type;
840 struct attribute size;
842 if (di->symt) return di->symt;
844 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
846 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
847 ref_type = dwarf2_lookup_type(ctx, di);
849 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
850 if (di->abbrev->have_child) FIXME("Unsupported children\n");
851 return di->symt;
854 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
855 dwarf2_debug_info_t* di)
857 struct symt* ref_type;
858 struct symt* idx_type = NULL;
859 struct attribute min, max, cnt;
860 dwarf2_debug_info_t** pchild = NULL;
861 dwarf2_debug_info_t* child;
863 if (di->symt) return di->symt;
865 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
867 if (!di->abbrev->have_child)
869 FIXME("array without range information\n");
870 return NULL;
872 ref_type = dwarf2_lookup_type(ctx, di);
874 while ((pchild = vector_iter_up(&di->children, pchild)))
876 child = *pchild;
877 switch (child->abbrev->tag)
879 case DW_TAG_subrange_type:
880 idx_type = dwarf2_lookup_type(ctx, child);
881 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
882 min.u.uvalue = 0;
883 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
884 max.u.uvalue = 0;
885 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
886 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
887 break;
888 default:
889 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
890 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
891 break;
894 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
895 return di->symt;
898 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
899 dwarf2_debug_info_t* di)
901 struct symt* ref_type;
903 if (di->symt) return di->symt;
905 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
907 ref_type = dwarf2_lookup_type(ctx, di);
908 if (di->abbrev->have_child) FIXME("Unsupported children\n");
909 di->symt = ref_type;
911 return ref_type;
914 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
915 dwarf2_debug_info_t* di)
917 struct symt* ref_type;
919 if (di->symt) return di->symt;
921 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
923 ref_type = dwarf2_lookup_type(ctx, di);
924 if (di->abbrev->have_child) FIXME("Unsupported children\n");
925 di->symt = ref_type;
927 return ref_type;
930 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
931 dwarf2_debug_info_t* di)
933 struct symt* ref_type = NULL;
935 if (di->symt) return di->symt;
937 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
939 ref_type = dwarf2_lookup_type(ctx, di);
940 /* FIXME: for now, we hard-wire C++ references to pointers */
941 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
943 if (di->abbrev->have_child) FIXME("Unsupported children\n");
945 return di->symt;
948 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
949 dwarf2_debug_info_t* di,
950 struct symt_udt* parent)
952 struct symt* elt_type;
953 struct attribute name;
954 unsigned long offset = 0;
955 struct attribute bit_size;
956 struct attribute bit_offset;
958 assert(parent);
960 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
962 dwarf2_find_name(ctx, di, &name, "udt_member");
963 elt_type = dwarf2_lookup_type(ctx, di);
964 if (dwarf2_compute_location(ctx, di, DW_AT_data_member_location, &offset, NULL))
965 TRACE("found member_location at %s -> %lu\n", dwarf2_debug_ctx(ctx), offset);
966 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size)) bit_size.u.uvalue = 0;
967 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
969 /* FIXME: we should only do this when implementation is LSB (which is
970 * the case on i386 processors)
972 struct attribute nbytes;
973 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
975 DWORD64 size;
976 nbytes.u.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
978 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
980 else bit_offset.u.uvalue = 0;
981 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
982 (offset << 3) + bit_offset.u.uvalue, bit_size.u.uvalue);
984 if (di->abbrev->have_child) FIXME("Unsupported children\n");
987 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
988 dwarf2_debug_info_t* di,
989 enum UdtKind udt)
991 struct attribute name;
992 struct attribute size;
994 if (di->symt) return di->symt;
996 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
998 dwarf2_find_name(ctx, di, &name, "udt");
999 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1001 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1003 if (di->abbrev->have_child) /** any interest to not have child ? */
1005 dwarf2_debug_info_t** pchild = NULL;
1006 dwarf2_debug_info_t* child;
1008 while ((pchild = vector_iter_up(&di->children, pchild)))
1010 child = *pchild;
1012 switch (child->abbrev->tag)
1014 case DW_TAG_member:
1015 /* FIXME: should I follow the sibling stuff ?? */
1016 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1017 break;
1018 case DW_TAG_enumeration_type:
1019 dwarf2_parse_enumeration_type(ctx, child);
1020 break;
1021 case DW_TAG_structure_type:
1022 case DW_TAG_class_type:
1023 case DW_TAG_union_type:
1024 /* FIXME: we need to handle nested udt definitions */
1025 break;
1026 default:
1027 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1028 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1029 break;
1034 return di->symt;
1037 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1038 dwarf2_debug_info_t* di,
1039 struct symt_enum* parent)
1041 struct attribute name;
1042 struct attribute value;
1044 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1046 dwarf2_find_name(ctx, di, &name, "enum_value");
1047 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1048 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1050 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1053 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1054 dwarf2_debug_info_t* di)
1056 struct attribute name;
1057 struct attribute size;
1059 if (di->symt) return di->symt;
1061 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1063 dwarf2_find_name(ctx, di, &name, "enum");
1064 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1066 di->symt = &symt_new_enum(ctx->module, name.u.string)->symt;
1068 if (di->abbrev->have_child) /* any interest to not have child ? */
1070 dwarf2_debug_info_t** pchild = NULL;
1071 dwarf2_debug_info_t* child;
1073 /* FIXME: should we use the sibling stuff ?? */
1074 while ((pchild = vector_iter_up(&di->children, pchild)))
1076 child = *pchild;
1078 switch (child->abbrev->tag)
1080 case DW_TAG_enumerator:
1081 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1082 break;
1083 default:
1084 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1085 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1089 return di->symt;
1092 static unsigned dwarf2_map_register(int regno)
1094 unsigned reg;
1096 switch (regno)
1098 case Wine_DW_no_register: FIXME("What the heck\n"); reg = 0; break;
1099 /* FIXME: this is a dirty hack */
1100 case Wine_DW_frame_register: reg = 0; break;
1101 case 0: reg = CV_REG_EAX; break;
1102 case 1: reg = CV_REG_ECX; break;
1103 case 2: reg = CV_REG_EDX; break;
1104 case 3: reg = CV_REG_EBX; break;
1105 case 4: reg = CV_REG_ESP; break;
1106 case 5: reg = CV_REG_EBP; break;
1107 case 6: reg = CV_REG_ESI; break;
1108 case 7: reg = CV_REG_EDI; break;
1109 case 8: reg = CV_REG_EIP; break;
1110 case 9: reg = CV_REG_EFLAGS; break;
1111 case 10: reg = CV_REG_CS; break;
1112 case 11: reg = CV_REG_SS; break;
1113 case 12: reg = CV_REG_DS; break;
1114 case 13: reg = CV_REG_ES; break;
1115 case 14: reg = CV_REG_FS; break;
1116 case 15: reg = CV_REG_GS; break;
1117 case 16: case 17: case 18: case 19:
1118 case 20: case 21: case 22: case 23:
1119 reg = CV_REG_ST0 + regno - 16; break;
1120 case 24: reg = CV_REG_CTRL; break;
1121 case 25: reg = CV_REG_STAT; break;
1122 case 26: reg = CV_REG_TAG; break;
1124 reg: fiseg 27
1125 reg: fioff 28
1126 reg: foseg 29
1127 reg: fooff 30
1128 reg: fop 31
1130 case 32: case 33: case 34: case 35:
1131 case 36: case 37: case 38: case 39:
1132 reg = CV_REG_XMM0 + regno - 32; break;
1133 case 40: reg = CV_REG_MXCSR; break;
1134 default:
1135 FIXME("Don't know how to map register %d\n", regno);
1136 return 0;
1138 return reg;
1141 /* structure used to pass information around when parsing a subprogram */
1142 typedef struct dwarf2_subprogram_s
1144 dwarf2_parse_context_t* ctx;
1145 struct symt_compiland* compiland;
1146 struct symt_function* func;
1147 unsigned long frame_offset;
1148 int frame_reg;
1149 } dwarf2_subprogram_t;
1151 /******************************************************************
1152 * dwarf2_parse_variable
1154 * Parses any variable (parameter, local/global variable)
1156 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1157 struct symt_block* block,
1158 dwarf2_debug_info_t* di)
1160 struct symt* param_type;
1161 struct attribute name, value;
1162 unsigned long offset;
1163 int in_reg;
1164 BOOL is_pmt = di->abbrev->tag == DW_TAG_formal_parameter;
1166 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1168 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1169 dwarf2_find_name(subpgm->ctx, di, &name, "parameter");
1170 if (dwarf2_compute_location(subpgm->ctx, di, DW_AT_location, &offset, &in_reg))
1172 struct attribute ext;
1174 TRACE("found parameter %s/%ld (reg=%d) at %s\n",
1175 name.u.string, offset, in_reg, dwarf2_debug_ctx(subpgm->ctx));
1176 switch (in_reg & ~Wine_DW_register_deref)
1178 case Wine_DW_no_register:
1179 /* it's a global variable */
1180 /* FIXME: we don't handle it's scope yet */
1181 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1182 ext.u.uvalue = 0;
1183 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1184 name.u.string, !ext.u.uvalue,
1185 subpgm->ctx->module->module.BaseOfImage + offset,
1186 0, param_type);
1187 break;
1188 case Wine_DW_frame_register:
1189 in_reg = subpgm->frame_reg | Wine_DW_register_deref;
1190 offset += subpgm->frame_offset;
1191 /* fall through */
1192 default:
1193 /* either a pmt/variable relative to frame pointer or
1194 * pmt/variable in a register
1196 assert(subpgm->func);
1197 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1198 is_pmt ? DataIsParam : DataIsLocal,
1199 dwarf2_map_register(in_reg & ~Wine_DW_register_deref),
1200 in_reg & Wine_DW_register_deref,
1201 offset, block, param_type, name.u.string);
1202 break;
1205 if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1207 FIXME("NIY: const value %08lx for %s\n", value.u.uvalue, name.u.string);
1209 if (is_pmt && subpgm->func && subpgm->func->type)
1210 symt_add_function_signature_parameter(subpgm->ctx->module,
1211 (struct symt_function_signature*)subpgm->func->type,
1212 param_type);
1214 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1217 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1218 dwarf2_debug_info_t* di)
1220 struct attribute name;
1221 struct attribute low_pc;
1223 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1225 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1226 dwarf2_find_name(subpgm->ctx, di, &name, "label");
1228 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1229 subpgm->ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1230 name.u.string);
1233 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1234 struct symt_block* block_parent,
1235 dwarf2_debug_info_t* di);
1237 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1238 dwarf2_debug_info_t* di)
1240 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1242 /* FIXME: attributes to handle:
1243 DW_AT_low_pc:
1244 DW_AT_high_pc:
1245 DW_AT_name:
1248 if (di->abbrev->have_child) /** any interest to not have child ? */
1250 dwarf2_debug_info_t** pchild = NULL;
1251 dwarf2_debug_info_t* child;
1253 while ((pchild = vector_iter_up(&di->children, pchild)))
1255 child = *pchild;
1257 switch (child->abbrev->tag)
1259 case DW_TAG_formal_parameter:
1260 /* FIXME: this is not properly supported yet
1261 * dwarf2_parse_subprogram_parameter(ctx, child, NULL);
1263 break;
1264 case DW_TAG_variable:
1265 /* FIXME:
1266 * dwarf2_parse_variable(ctx, child);
1268 break;
1269 case DW_TAG_lexical_block:
1270 /* FIXME:
1271 dwarf2_parse_subprogram_block(ctx, child, func);
1273 break;
1274 case DW_TAG_inlined_subroutine:
1275 /* FIXME */
1276 dwarf2_parse_inlined_subroutine(subpgm, child);
1277 break;
1278 case DW_TAG_label:
1279 dwarf2_parse_subprogram_label(subpgm, child);
1280 break;
1281 default:
1282 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1283 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1284 dwarf2_debug_di(di));
1290 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1291 struct symt_block* parent_block,
1292 dwarf2_debug_info_t* di)
1294 struct symt_block* block;
1295 struct attribute low_pc;
1296 struct attribute high_pc;
1298 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1300 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1301 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1303 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1304 low_pc.u.uvalue, high_pc.u.uvalue - low_pc.u.uvalue);
1306 if (di->abbrev->have_child) /** any interest to not have child ? */
1308 dwarf2_debug_info_t** pchild = NULL;
1309 dwarf2_debug_info_t* child;
1311 while ((pchild = vector_iter_up(&di->children, pchild)))
1313 child = *pchild;
1315 switch (child->abbrev->tag)
1317 case DW_TAG_inlined_subroutine:
1318 dwarf2_parse_inlined_subroutine(subpgm, child);
1319 break;
1320 case DW_TAG_variable:
1321 dwarf2_parse_variable(subpgm, block, child);
1322 break;
1323 case DW_TAG_lexical_block:
1324 dwarf2_parse_subprogram_block(subpgm, block, child);
1325 break;
1326 case DW_TAG_subprogram:
1327 /* FIXME: likely a declaration (to be checked)
1328 * skip it for now
1330 break;
1331 case DW_TAG_formal_parameter:
1332 /* FIXME: likely elements for exception handling (GCC flavor)
1333 * Skip it for now
1335 break;
1336 case DW_TAG_class_type:
1337 case DW_TAG_structure_type:
1338 case DW_TAG_union_type:
1339 case DW_TAG_enumeration_type:
1340 /* the type referred to will be loaded when we need it, so skip it */
1341 break;
1342 default:
1343 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1344 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1349 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1352 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1353 dwarf2_debug_info_t* di,
1354 struct symt_compiland* compiland)
1356 struct attribute name;
1357 struct attribute low_pc;
1358 struct attribute high_pc;
1359 struct attribute is_decl;
1360 struct attribute inline_flags;
1361 struct symt* ret_type;
1362 struct symt_function_signature* sig_type;
1363 dwarf2_subprogram_t subpgm;
1365 if (di->symt) return di->symt;
1367 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1369 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1370 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1371 /* As functions (defined as inline assembly) get debug info with dwarf
1372 * (not the case for stabs), we just drop Wine's thunks here...
1373 * Actual thunks will be created in elf_module from the symbol table
1375 if (elf_is_in_thunk_area(ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1376 ctx->thunks) >= 0)
1377 return NULL;
1378 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl)) is_decl.u.uvalue = 0;
1379 if (!dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags)) inline_flags.u.uvalue = 0;
1380 dwarf2_find_name(ctx, di, &name, "subprogram");
1381 ret_type = dwarf2_lookup_type(ctx, di);
1383 /* FIXME: assuming C source code */
1384 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1385 if (!is_decl.u.uvalue)
1387 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1388 ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1389 high_pc.u.uvalue - low_pc.u.uvalue,
1390 &sig_type->symt);
1391 di->symt = &subpgm.func->symt;
1393 else subpgm.func = NULL;
1395 subpgm.ctx = ctx;
1396 subpgm.compiland = compiland;
1397 if (dwarf2_compute_location(ctx, di, DW_AT_frame_base, &subpgm.frame_offset, &subpgm.frame_reg))
1398 TRACE("For %s got %ld/%d\n", name.u.string, subpgm.frame_offset, subpgm.frame_reg);
1399 else /* on stack !! */
1401 subpgm.frame_reg = 0;
1402 subpgm.frame_offset = 0;
1405 if (di->abbrev->have_child) /** any interest to not have child ? */
1407 dwarf2_debug_info_t** pchild = NULL;
1408 dwarf2_debug_info_t* child;
1410 while ((pchild = vector_iter_up(&di->children, pchild)))
1412 child = *pchild;
1414 switch (child->abbrev->tag)
1416 case DW_TAG_variable:
1417 case DW_TAG_formal_parameter:
1418 dwarf2_parse_variable(&subpgm, NULL, child);
1419 break;
1420 case DW_TAG_lexical_block:
1421 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1422 break;
1423 case DW_TAG_inlined_subroutine:
1424 dwarf2_parse_inlined_subroutine(&subpgm, child);
1425 break;
1426 case DW_TAG_subprogram:
1427 /* FIXME: likely a declaration (to be checked)
1428 * skip it for now
1430 break;
1431 case DW_TAG_label:
1432 dwarf2_parse_subprogram_label(&subpgm, child);
1433 break;
1434 case DW_TAG_class_type:
1435 case DW_TAG_structure_type:
1436 case DW_TAG_union_type:
1437 case DW_TAG_enumeration_type:
1438 case DW_TAG_typedef:
1439 /* the type referred to will be loaded when we need it, so skip it */
1440 break;
1441 case DW_TAG_unspecified_parameters:
1442 /* FIXME: no support in dbghelp's internals so far */
1443 break;
1444 default:
1445 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1446 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1451 symt_normalize_function(subpgm.ctx->module, subpgm.func);
1453 return di->symt;
1456 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1457 dwarf2_debug_info_t* di)
1459 struct symt* ret_type;
1460 struct symt_function_signature* sig_type;
1462 if (di->symt) return di->symt;
1464 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1466 ret_type = dwarf2_lookup_type(ctx, di);
1468 /* FIXME: assuming C source code */
1469 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1471 if (di->abbrev->have_child) /** any interest to not have child ? */
1473 dwarf2_debug_info_t** pchild = NULL;
1474 dwarf2_debug_info_t* child;
1476 while ((pchild = vector_iter_up(&di->children, pchild)))
1478 child = *pchild;
1480 switch (child->abbrev->tag)
1482 case DW_TAG_formal_parameter:
1483 symt_add_function_signature_parameter(ctx->module, sig_type,
1484 dwarf2_lookup_type(ctx, child));
1485 break;
1486 case DW_TAG_unspecified_parameters:
1487 WARN("Unsupported unspecified parameters\n");
1488 break;
1493 return di->symt = &sig_type->symt;
1496 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1497 dwarf2_debug_info_t* di,
1498 struct symt_compiland* compiland)
1500 switch (di->abbrev->tag)
1502 case DW_TAG_typedef:
1503 dwarf2_parse_typedef(ctx, di);
1504 break;
1505 case DW_TAG_base_type:
1506 dwarf2_parse_base_type(ctx, di);
1507 break;
1508 case DW_TAG_pointer_type:
1509 dwarf2_parse_pointer_type(ctx, di);
1510 break;
1511 case DW_TAG_class_type:
1512 dwarf2_parse_udt_type(ctx, di, UdtClass);
1513 break;
1514 case DW_TAG_structure_type:
1515 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1516 break;
1517 case DW_TAG_union_type:
1518 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1519 break;
1520 case DW_TAG_array_type:
1521 dwarf2_parse_array_type(ctx, di);
1522 break;
1523 case DW_TAG_const_type:
1524 dwarf2_parse_const_type(ctx, di);
1525 break;
1526 case DW_TAG_volatile_type:
1527 dwarf2_parse_volatile_type(ctx, di);
1528 break;
1529 case DW_TAG_reference_type:
1530 dwarf2_parse_reference_type(ctx, di);
1531 break;
1532 case DW_TAG_enumeration_type:
1533 dwarf2_parse_enumeration_type(ctx, di);
1534 break;
1535 case DW_TAG_subprogram:
1536 dwarf2_parse_subprogram(ctx, di, compiland);
1537 break;
1538 case DW_TAG_subroutine_type:
1539 dwarf2_parse_subroutine_type(ctx, di);
1540 break;
1541 case DW_TAG_variable:
1543 dwarf2_subprogram_t subpgm;
1545 subpgm.ctx = ctx;
1546 subpgm.compiland = compiland;
1547 subpgm.func = NULL;
1548 subpgm.frame_offset = 0;
1549 subpgm.frame_reg = 0;
1550 dwarf2_parse_variable(&subpgm, NULL, di);
1552 break;
1553 default:
1554 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1555 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1559 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1560 struct vector* v, unsigned file, unsigned line)
1562 struct symt_function* func;
1563 int idx;
1564 unsigned* psrc;
1566 if (!file || !(psrc = vector_at(v, file - 1))) return;
1568 TRACE("%s %lx %s %u\n", module->module.ModuleName, address, source_get(module, *psrc), line);
1569 if ((idx = symt_find_nearest(module, address)) == -1 ||
1570 module->addr_sorttab[idx]->symt.tag != SymTagFunction) return;
1571 func = (struct symt_function*)module->addr_sorttab[idx];
1572 symt_add_func_line(module, func, *psrc, line, address - func->address);
1575 static void dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1576 dwarf2_parse_context_t* ctx,
1577 unsigned long offset)
1579 dwarf2_traverse_context_t traverse;
1580 unsigned long length;
1581 unsigned version, header_len, insn_size, default_stmt;
1582 unsigned line_range, opcode_base;
1583 int line_base;
1584 const unsigned char* opcode_len;
1585 struct vector dirs;
1586 struct vector files;
1587 const char** p;
1589 traverse.data = sections[section_line].address + offset;
1590 traverse.start_data = traverse.data;
1591 traverse.end_data = traverse.data + 4;
1592 traverse.word_size = ctx->word_size;
1594 length = dwarf2_parse_u4(&traverse);
1595 traverse.end_data = traverse.start_data + length;
1597 version = dwarf2_parse_u2(&traverse);
1598 header_len = dwarf2_parse_u4(&traverse);
1599 insn_size = dwarf2_parse_byte(&traverse);
1600 default_stmt = dwarf2_parse_byte(&traverse);
1601 line_base = (signed char)dwarf2_parse_byte(&traverse);
1602 line_range = dwarf2_parse_byte(&traverse);
1603 opcode_base = dwarf2_parse_byte(&traverse);
1605 opcode_len = traverse.data;
1606 traverse.data += opcode_base - 1;
1608 vector_init(&dirs, sizeof(const char*), 4);
1609 p = vector_add(&dirs, &ctx->pool);
1610 *p = ".";
1611 while (*traverse.data)
1613 TRACE("Got include %s\n", (const char*)traverse.data);
1614 p = vector_add(&dirs, &ctx->pool);
1615 *p = (const char *)traverse.data;
1616 traverse.data += strlen((const char *)traverse.data) + 1;
1618 traverse.data++;
1620 vector_init(&files, sizeof(unsigned), 16);
1621 while (*traverse.data)
1623 unsigned int dir_index, mod_time, length;
1624 const char* name;
1625 const char* dir;
1626 unsigned* psrc;
1628 name = (const char*)traverse.data;
1629 traverse.data += strlen(name) + 1;
1630 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1631 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1632 length = dwarf2_leb128_as_unsigned(&traverse);
1633 dir = *(const char**)vector_at(&dirs, dir_index);
1634 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1635 psrc = vector_add(&files, &ctx->pool);
1636 *psrc = source_new(ctx->module, dir, name);
1638 traverse.data++;
1640 while (traverse.data < traverse.end_data)
1642 unsigned long address = 0;
1643 unsigned file = 1;
1644 unsigned line = 1;
1645 unsigned is_stmt = default_stmt;
1646 BOOL basic_block = FALSE, end_sequence = FALSE;
1647 unsigned opcode, extopcode, i;
1649 while (!end_sequence)
1651 opcode = dwarf2_parse_byte(&traverse);
1652 TRACE("Got opcode %x\n", opcode);
1654 if (opcode >= opcode_base)
1656 unsigned delta = opcode - opcode_base;
1658 address += (delta / line_range) * insn_size;
1659 line += line_base + (delta % line_range);
1660 basic_block = TRUE;
1661 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1663 else
1665 switch (opcode)
1667 case DW_LNS_copy:
1668 basic_block = FALSE;
1669 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1670 break;
1671 case DW_LNS_advance_pc:
1672 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1673 break;
1674 case DW_LNS_advance_line:
1675 line += dwarf2_leb128_as_signed(&traverse);
1676 break;
1677 case DW_LNS_set_file:
1678 file = dwarf2_leb128_as_unsigned(&traverse);
1679 break;
1680 case DW_LNS_set_column:
1681 dwarf2_leb128_as_unsigned(&traverse);
1682 break;
1683 case DW_LNS_negate_stmt:
1684 is_stmt = !is_stmt;
1685 break;
1686 case DW_LNS_set_basic_block:
1687 basic_block = 1;
1688 break;
1689 case DW_LNS_const_add_pc:
1690 address += ((255 - opcode_base) / line_range) * insn_size;
1691 break;
1692 case DW_LNS_fixed_advance_pc:
1693 address += dwarf2_parse_u2(&traverse);
1694 break;
1695 case DW_LNS_extended_op:
1696 dwarf2_leb128_as_unsigned(&traverse);
1697 extopcode = dwarf2_parse_byte(&traverse);
1698 switch (extopcode)
1700 case DW_LNE_end_sequence:
1701 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1702 end_sequence = TRUE;
1703 break;
1704 case DW_LNE_set_address:
1705 address = ctx->module->module.BaseOfImage + dwarf2_parse_addr(&traverse);
1706 break;
1707 case DW_LNE_define_file:
1708 FIXME("not handled %s\n", traverse.data);
1709 traverse.data += strlen((const char *)traverse.data) + 1;
1710 dwarf2_leb128_as_unsigned(&traverse);
1711 dwarf2_leb128_as_unsigned(&traverse);
1712 dwarf2_leb128_as_unsigned(&traverse);
1713 break;
1714 default:
1715 FIXME("Unsupported extended opcode %x\n", extopcode);
1716 break;
1718 break;
1719 default:
1720 WARN("Unsupported opcode %x\n", opcode);
1721 for (i = 0; i < opcode_len[opcode]; i++)
1722 dwarf2_leb128_as_unsigned(&traverse);
1723 break;
1730 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1731 const dwarf2_comp_unit_t* comp_unit,
1732 struct module* module,
1733 const struct elf_thunk_area* thunks,
1734 const unsigned char* comp_unit_cursor)
1736 dwarf2_parse_context_t ctx;
1737 dwarf2_traverse_context_t traverse;
1738 dwarf2_traverse_context_t abbrev_ctx;
1739 dwarf2_debug_info_t* di;
1740 BOOL ret = FALSE;
1742 TRACE("Compilation Unit Header found at 0x%x:\n",
1743 comp_unit_cursor - sections[section_debug].address);
1744 TRACE("- length: %lu\n", comp_unit->length);
1745 TRACE("- version: %u\n", comp_unit->version);
1746 TRACE("- abbrev_offset: %lu\n", comp_unit->abbrev_offset);
1747 TRACE("- word_size: %u\n", comp_unit->word_size);
1749 if (comp_unit->version != 2)
1751 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
1752 comp_unit->version);
1753 return FALSE;
1756 pool_init(&ctx.pool, 65536);
1757 ctx.sections = sections;
1758 ctx.section = section_debug;
1759 ctx.module = module;
1760 ctx.word_size = comp_unit->word_size;
1761 ctx.thunks = thunks;
1762 ctx.ref_offset = comp_unit_cursor - sections[section_debug].address;
1764 traverse.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
1765 traverse.data = traverse.start_data;
1766 traverse.word_size = comp_unit->word_size;
1767 traverse.end_data = comp_unit_cursor + comp_unit->length + sizeof(unsigned);
1769 abbrev_ctx.start_data = sections[section_abbrev].address + comp_unit->abbrev_offset;
1770 abbrev_ctx.data = abbrev_ctx.start_data;
1771 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
1772 abbrev_ctx.word_size = comp_unit->word_size;
1773 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
1775 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
1776 dwarf2_read_one_debug_info(&ctx, &traverse, &di);
1778 if (di->abbrev->tag == DW_TAG_compile_unit)
1780 struct attribute name;
1781 dwarf2_debug_info_t** pdi = NULL;
1782 struct attribute stmt_list;
1784 dwarf2_find_name(&ctx, di, &name, "compiland");
1785 di->symt = &symt_new_compiland(module, source_new(module, NULL, name.u.string))->symt;
1787 if (di->abbrev->have_child)
1789 while ((pdi = vector_iter_up(&di->children, pdi)))
1791 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
1794 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
1796 dwarf2_parse_line_numbers(sections, &ctx, stmt_list.u.uvalue);
1798 ret = TRUE;
1800 else FIXME("Should have a compilation unit here\n");
1801 pool_destroy(&ctx.pool);
1802 return ret;
1805 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
1806 const struct elf_thunk_area* thunks,
1807 const unsigned char* debug, unsigned int debug_size,
1808 const unsigned char* abbrev, unsigned int abbrev_size,
1809 const unsigned char* str, unsigned int str_size,
1810 const unsigned char* line, unsigned int line_size)
1812 dwarf2_section_t section[section_max];
1813 const unsigned char*comp_unit_cursor = debug;
1814 const unsigned char*end_debug = debug + debug_size;
1816 section[section_debug].address = debug;
1817 section[section_debug].size = debug_size;
1818 section[section_abbrev].address = abbrev;
1819 section[section_abbrev].size = abbrev_size;
1820 section[section_string].address = str;
1821 section[section_string].size = str_size;
1822 section[section_line].address = line;
1823 section[section_line].size = line_size;
1825 while (comp_unit_cursor < end_debug)
1827 const dwarf2_comp_unit_stream_t* comp_unit_stream;
1828 dwarf2_comp_unit_t comp_unit;
1830 comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
1831 comp_unit.length = *(unsigned long*) comp_unit_stream->length;
1832 comp_unit.version = *(unsigned short*) comp_unit_stream->version;
1833 comp_unit.abbrev_offset = *(unsigned long*) comp_unit_stream->abbrev_offset;
1834 comp_unit.word_size = *(unsigned char*) comp_unit_stream->word_size;
1836 dwarf2_parse_compilation_unit(section, &comp_unit, module,
1837 thunks, comp_unit_cursor);
1838 comp_unit_cursor += comp_unit.length + sizeof(unsigned);
1840 module->module.SymType = SymDia;
1841 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
1842 /* FIXME: we could have a finer grain here */
1843 module->module.LineNumbers = TRUE;
1844 module->module.GlobalSymbols = TRUE;
1845 module->module.TypeInfo = TRUE;
1846 module->module.SourceIndexed = TRUE;
1847 module->module.Publics = TRUE;
1848 return TRUE;