rpcrt4/tests: Replace || by |.
[wine/multimedia.git] / dlls / dbghelp / dwarf.c
blob4253413ea5e935e3c03f4d2aa55b3f06b05084e2
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006, Eric Pouech
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <sys/types.h>
25 #include <fcntl.h>
26 #ifdef HAVE_SYS_STAT_H
27 # include <sys/stat.h>
28 #endif
29 #ifdef HAVE_SYS_MMAN_H
30 #include <sys/mman.h>
31 #endif
32 #include <limits.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #ifdef HAVE_UNISTD_H
36 # include <unistd.h>
37 #endif
38 #include <stdio.h>
39 #ifndef PATH_MAX
40 #define PATH_MAX MAX_PATH
41 #endif
42 #include <assert.h>
43 #include <stdarg.h>
45 #include "windef.h"
46 #include "winbase.h"
47 #include "winreg.h"
48 #include "winnls.h"
50 #include "dbghelp_private.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
56 /* FIXME:
57 * - Functions:
58 * o unspecified parameters
59 * o inlined functions
60 * o Debug{Start|End}Point
61 * o CFA
62 * - Udt
63 * o proper types loading (nesting)
66 #if 0
67 static void dump(const void* ptr, unsigned len)
69 int i, j;
70 BYTE msg[128];
71 static const char hexof[] = "0123456789abcdef";
72 const BYTE* x = (const BYTE*)ptr;
74 for (i = 0; i < len; i += 16)
76 sprintf(msg, "%08x: ", i);
77 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
78 for (j = 0; j < min(16, len - i); j++)
80 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
81 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
82 msg[10 + 3 * j + 2] = ' ';
83 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
84 x[i + j] : '.';
86 msg[10 + 3 * 16] = ' ';
87 msg[10 + 3 * 16 + 1 + 16] = '\0';
88 TRACE("%s\n", msg);
91 #endif
93 /**
95 * Main Specs:
96 * http://www.eagercon.com/dwarf/dwarf3std.htm
97 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
99 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
101 * example of projects who do dwarf2 parsing:
102 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
103 * http://elis.ugent.be/diota/log/ltrace_elf.c
105 #include "dwarf.h"
108 * Parsers
111 typedef struct dwarf2_abbrev_entry_attr_s
113 unsigned long attribute;
114 unsigned long form;
115 struct dwarf2_abbrev_entry_attr_s* next;
116 } dwarf2_abbrev_entry_attr_t;
118 typedef struct dwarf2_abbrev_entry_s
120 unsigned long entry_code;
121 unsigned long tag;
122 unsigned char have_child;
123 unsigned num_attr;
124 dwarf2_abbrev_entry_attr_t* attrs;
125 } dwarf2_abbrev_entry_t;
127 struct dwarf2_block
129 unsigned size;
130 const unsigned char* ptr;
133 struct attribute
135 unsigned long form;
136 union
138 unsigned long uvalue;
139 long svalue;
140 const char* string;
141 struct dwarf2_block block;
142 } u;
145 typedef struct dwarf2_debug_info_s
147 const dwarf2_abbrev_entry_t*abbrev;
148 struct symt* symt;
149 const unsigned char** data;
150 struct vector children;
151 } dwarf2_debug_info_t;
153 #define NO_MAP ((const void*)0xffffffff)
155 typedef struct dwarf2_section_s
157 const unsigned char* address;
158 unsigned size;
159 } dwarf2_section_t;
161 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
163 typedef struct dwarf2_traverse_context_s
165 const unsigned char* data;
166 const unsigned char* start_data;
167 const unsigned char* end_data;
168 unsigned char word_size;
169 } dwarf2_traverse_context_t;
171 typedef struct dwarf2_parse_context_s
173 const dwarf2_section_t* sections;
174 unsigned section;
175 struct pool pool;
176 struct module* module;
177 const struct elf_thunk_area*thunks;
178 struct sparse_array abbrev_table;
179 struct sparse_array debug_info_table;
180 unsigned long ref_offset;
181 unsigned char word_size;
182 } dwarf2_parse_context_t;
184 /* forward declarations */
185 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
187 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
189 return *ptr;
192 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
194 unsigned char uvalue = dwarf2_get_byte(ctx->data);
195 ctx->data += 1;
196 return uvalue;
199 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
201 return *(const unsigned short*)ptr;
204 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
206 unsigned short uvalue = dwarf2_get_u2(ctx->data);
207 ctx->data += 2;
208 return uvalue;
211 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
213 return *(const unsigned long*)ptr;
216 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
218 unsigned long uvalue = dwarf2_get_u4(ctx->data);
219 ctx->data += 4;
220 return uvalue;
223 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
225 unsigned long ret = 0;
226 unsigned char byte;
227 unsigned shift = 0;
231 byte = dwarf2_get_byte(ptr++);
232 ret |= (byte & 0x7f) << shift;
233 shift += 7;
234 } while (byte & 0x80);
236 if (end) *end = ptr;
237 return ret;
240 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
242 unsigned long ret;
244 assert(ctx);
246 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
248 return ret;
251 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
253 long ret = 0;
254 unsigned char byte;
255 unsigned shift = 0;
256 const unsigned size = sizeof(int) * 8;
260 byte = dwarf2_get_byte(ptr++);
261 ret |= (byte & 0x7f) << shift;
262 shift += 7;
263 } while (byte & 0x80);
264 if (end) *end = ptr;
266 /* as spec: sign bit of byte is 2nd high order bit (80x40)
267 * -> 0x80 is used as flag.
269 if ((shift < size) && (byte & 0x40))
271 ret |= - (1 << shift);
273 return ret;
276 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
278 long ret = 0;
280 assert(ctx);
282 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
283 return ret;
286 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
288 unsigned ret;
289 for (ret = 0; ctx->data[ret] & 0x80; ret++);
290 return ret + 1;
293 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
295 unsigned long ret;
297 switch (word_size)
299 case 4:
300 ret = dwarf2_get_u4(ptr);
301 break;
302 default:
303 FIXME("Unsupported Word Size %u\n", word_size);
304 ret = 0;
306 return ret;
309 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
311 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
312 ctx->data += ctx->word_size;
313 return ret;
316 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
318 return wine_dbg_sprintf("ctx(%p)", ctx->data);
321 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
323 return wine_dbg_sprintf("ctx(%p,%s)", ctx, ctx->module->module.ModuleName);
326 static const char* dwarf2_debug_di(dwarf2_debug_info_t* di)
328 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
329 di->abbrev, di->symt);
332 static dwarf2_abbrev_entry_t*
333 dwarf2_abbrev_table_find_entry(struct sparse_array* abbrev_table,
334 unsigned long entry_code)
336 assert( NULL != abbrev_table );
337 return sparse_array_find(abbrev_table, entry_code);
340 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
341 struct sparse_array* abbrev_table,
342 struct pool* pool)
344 unsigned long entry_code;
345 dwarf2_abbrev_entry_t* abbrev_entry;
346 dwarf2_abbrev_entry_attr_t* new = NULL;
347 dwarf2_abbrev_entry_attr_t* last = NULL;
348 unsigned long attribute;
349 unsigned long form;
351 assert( NULL != abbrev_ctx );
353 TRACE("%s, end at %p\n",
354 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
356 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
357 while (abbrev_ctx->data < abbrev_ctx->end_data)
359 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
360 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
361 TRACE("found entry_code %lu\n", entry_code);
362 if (!entry_code)
364 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
365 break;
367 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
368 assert( NULL != abbrev_entry );
370 abbrev_entry->entry_code = entry_code;
371 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
372 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
373 abbrev_entry->attrs = NULL;
374 abbrev_entry->num_attr = 0;
376 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
377 abbrev_table, sparse_array_length(abbrev_table),
378 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
380 last = NULL;
381 while (1)
383 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
384 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
385 if (!attribute) break;
387 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
388 assert(new);
390 new->attribute = attribute;
391 new->form = form;
392 new->next = NULL;
393 if (abbrev_entry->attrs) last->next = new;
394 else abbrev_entry->attrs = new;
395 last = new;
396 abbrev_entry->num_attr++;
399 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
402 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
403 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
405 unsigned step;
407 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
409 switch (abbrev_attr->form)
411 case DW_FORM_ref_addr:
412 case DW_FORM_addr: step = ctx->word_size; break;
413 case DW_FORM_flag:
414 case DW_FORM_data1:
415 case DW_FORM_ref1: step = 1; break;
416 case DW_FORM_data2:
417 case DW_FORM_ref2: step = 2; break;
418 case DW_FORM_data4:
419 case DW_FORM_ref4:
420 case DW_FORM_strp: step = 4; break;
421 case DW_FORM_data8:
422 case DW_FORM_ref8: step = 8; break;
423 case DW_FORM_sdata:
424 case DW_FORM_ref_udata:
425 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
426 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
427 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
428 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
429 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
430 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
431 default:
432 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
433 return;
435 ctx->data += step;
438 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
439 const dwarf2_debug_info_t* di,
440 unsigned at, struct attribute* attr)
442 unsigned i;
443 dwarf2_abbrev_entry_attr_t* abbrev_attr;
445 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
447 if (abbrev_attr->attribute == at)
449 attr->form = abbrev_attr->form;
450 switch (attr->form)
452 case DW_FORM_ref_addr:
453 case DW_FORM_addr:
454 attr->u.uvalue = dwarf2_get_addr(di->data[i], ctx->word_size);
455 TRACE("addr<0x%lx>\n", attr->u.uvalue);
456 break;
458 case DW_FORM_flag:
459 attr->u.uvalue = dwarf2_get_byte(di->data[i]);
460 TRACE("flag<0x%lx>\n", attr->u.uvalue);
461 break;
463 case DW_FORM_data1:
464 attr->u.uvalue = dwarf2_get_byte(di->data[i]);
465 TRACE("data1<%lu>\n", attr->u.uvalue);
466 break;
468 case DW_FORM_data2:
469 attr->u.uvalue = dwarf2_get_u2(di->data[i]);
470 TRACE("data2<%lu>\n", attr->u.uvalue);
471 break;
473 case DW_FORM_data4:
474 attr->u.uvalue = dwarf2_get_u4(di->data[i]);
475 TRACE("data4<%lu>\n", attr->u.uvalue);
476 break;
478 case DW_FORM_data8:
479 FIXME("Unhandled 64bits support\n");
480 break;
482 case DW_FORM_ref1:
483 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(di->data[i]);
484 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
485 break;
487 case DW_FORM_ref2:
488 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(di->data[i]);
489 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
490 break;
492 case DW_FORM_ref4:
493 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(di->data[i]);
494 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
495 break;
497 case DW_FORM_ref8:
498 FIXME("Unhandled 64 bit support\n");
499 break;
501 case DW_FORM_sdata:
502 attr->u.svalue = dwarf2_get_leb128_as_signed(di->data[i], NULL);
503 break;
505 case DW_FORM_ref_udata:
506 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(di->data[i], NULL);
507 break;
509 case DW_FORM_udata:
510 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(di->data[i], NULL);
511 break;
513 case DW_FORM_string:
514 attr->u.string = (const char *)di->data[i];
515 TRACE("string<%s>\n", attr->u.string);
516 break;
518 case DW_FORM_strp:
520 unsigned long offset = dwarf2_get_u4(di->data[i]);
521 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
523 TRACE("strp<%s>\n", attr->u.string);
524 break;
525 case DW_FORM_block:
526 attr->u.block.size = dwarf2_get_leb128_as_unsigned(di->data[i], &attr->u.block.ptr);
527 break;
529 case DW_FORM_block1:
530 attr->u.block.size = dwarf2_get_byte(di->data[i]);
531 attr->u.block.ptr = di->data[i] + 1;
532 break;
534 case DW_FORM_block2:
535 attr->u.block.size = dwarf2_get_u2(di->data[i]);
536 attr->u.block.ptr = di->data[i] + 2;
537 break;
539 case DW_FORM_block4:
540 attr->u.block.size = dwarf2_get_u4(di->data[i]);
541 attr->u.block.ptr = di->data[i] + 4;
542 break;
544 default:
545 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
546 break;
548 return TRUE;
551 return FALSE;
554 static void dwarf2_find_name(dwarf2_parse_context_t* ctx,
555 const dwarf2_debug_info_t* di,
556 struct attribute* attr, const char* pfx)
558 static int index;
560 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, attr))
562 char* tmp = pool_alloc(&ctx->pool, strlen(pfx) + 16);
563 if (tmp) sprintf(tmp, "%s_%d", pfx, index++);
564 attr->u.string = tmp;
568 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
569 struct symt_compiland*);
571 #define Wine_DW_no_register 0x7FFFFFFF
572 #define Wine_DW_frame_register 0x7FFFFFFE
573 #define Wine_DW_register_deref 0x80000000
575 static BOOL dwarf2_compute_location(dwarf2_parse_context_t* ctx,
576 dwarf2_debug_info_t* di,
577 unsigned long dw,
578 unsigned long* offset, int* in_register)
580 unsigned long loc[64];
581 unsigned stk;
582 struct attribute xloc;
584 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
586 if (in_register) *in_register = Wine_DW_no_register;
588 switch (xloc.form)
590 case DW_FORM_data1: case DW_FORM_data2:
591 case DW_FORM_data4: case DW_FORM_data8:
592 case DW_FORM_udata: case DW_FORM_sdata:
593 /* we've got a constant */
594 *offset = xloc.u.uvalue;
595 return TRUE;
597 /* assume we have a block form */
598 loc[stk = 0] = 0;
600 if (xloc.u.block.size)
602 dwarf2_traverse_context_t lctx;
603 unsigned char op;
604 BOOL piece_found = FALSE;
606 lctx.data = xloc.u.block.ptr;
607 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
608 lctx.word_size = ctx->word_size;
610 while (lctx.data < lctx.end_data)
612 op = dwarf2_parse_byte(&lctx);
613 switch (op)
615 case DW_OP_addr: loc[++stk] = dwarf2_parse_addr(&lctx); break;
616 case DW_OP_const1u: loc[++stk] = dwarf2_parse_byte(&lctx); break;
617 case DW_OP_const1s: loc[++stk] = (long)(signed char)dwarf2_parse_byte(&lctx); break;
618 case DW_OP_const2u: loc[++stk] = dwarf2_parse_u2(&lctx); break;
619 case DW_OP_const2s: loc[++stk] = (long)(short)dwarf2_parse_u2(&lctx); break;
620 case DW_OP_const4u: loc[++stk] = dwarf2_parse_u4(&lctx); break;
621 case DW_OP_const4s: loc[++stk] = dwarf2_parse_u4(&lctx); break;
622 case DW_OP_constu: loc[++stk] = dwarf2_leb128_as_unsigned(&lctx); break;
623 case DW_OP_consts: loc[++stk] = dwarf2_leb128_as_signed(&lctx); break;
624 case DW_OP_plus_uconst:
625 loc[stk] += dwarf2_leb128_as_unsigned(&lctx); break;
626 case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
627 case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
628 case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
629 case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
630 case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
631 case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
632 case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
633 case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
634 case DW_OP_breg0: case DW_OP_breg1: case DW_OP_breg2: case DW_OP_breg3:
635 case DW_OP_breg4: case DW_OP_breg5: case DW_OP_breg6: case DW_OP_breg7:
636 case DW_OP_breg8: case DW_OP_breg9: case DW_OP_breg10: case DW_OP_breg11:
637 case DW_OP_breg12: case DW_OP_breg13: case DW_OP_breg14: case DW_OP_breg15:
638 case DW_OP_breg16: case DW_OP_breg17: case DW_OP_breg18: case DW_OP_breg19:
639 case DW_OP_breg20: case DW_OP_breg21: case DW_OP_breg22: case DW_OP_breg23:
640 case DW_OP_breg24: case DW_OP_breg25: case DW_OP_breg26: case DW_OP_breg27:
641 case DW_OP_breg28: case DW_OP_breg29: case DW_OP_breg30: case DW_OP_breg31:
642 if (in_register)
644 /* dbghelp APIs don't know how to cope with this anyway
645 * (for example 'long long' stored in two registers)
646 * FIXME: We should tell winedbg how to deal with it (sigh)
648 if (!piece_found || (op - DW_OP_reg0 != *in_register + 1))
650 if (*in_register != Wine_DW_no_register)
651 FIXME("Only supporting one reg (%d -> %d)\n",
652 *in_register, op - DW_OP_reg0);
653 *in_register = op - DW_OP_reg0;
656 else FIXME("Found register, while not expecting it\n");
657 if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
659 loc[++stk] = dwarf2_leb128_as_signed(&lctx);
660 if (in_register) *in_register |= Wine_DW_register_deref;
662 break;
663 case DW_OP_fbreg:
664 if (in_register)
666 if (*in_register != Wine_DW_no_register)
667 FIXME("Only supporting one reg (%d -> -2)\n", *in_register);
668 *in_register = Wine_DW_frame_register | Wine_DW_register_deref;
670 else FIXME("Found register, while not expecting it\n");
671 loc[++stk] = dwarf2_leb128_as_signed(&lctx);
672 break;
673 case DW_OP_piece:
675 unsigned sz = dwarf2_leb128_as_unsigned(&lctx);
676 WARN("Not handling OP_piece directly (size=%d)\n", sz);
677 piece_found = TRUE;
679 break;
680 default:
681 FIXME("Unhandled attr op: %x\n", op);
682 return loc[stk];
686 *offset = loc[stk];
687 return TRUE;
690 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
691 const dwarf2_debug_info_t* di)
693 struct attribute attr;
695 if (dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
697 dwarf2_debug_info_t* type;
699 type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue);
700 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
701 if (!type->symt)
703 /* load the debug info entity */
704 dwarf2_load_one_entry(ctx, type, NULL);
706 return type->symt;
708 return NULL;
711 /******************************************************************
712 * dwarf2_read_one_debug_info
714 * Loads into memory one debug info entry, and recursively its children (if any)
716 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
717 dwarf2_traverse_context_t* traverse,
718 dwarf2_debug_info_t** pdi)
720 const dwarf2_abbrev_entry_t*abbrev;
721 unsigned long entry_code;
722 unsigned long offset;
723 dwarf2_debug_info_t* di;
724 dwarf2_debug_info_t* child;
725 dwarf2_debug_info_t** where;
726 dwarf2_abbrev_entry_attr_t* attr;
727 unsigned i;
728 struct attribute sibling;
730 offset = traverse->data - ctx->sections[ctx->section].address;
731 entry_code = dwarf2_leb128_as_unsigned(traverse);
732 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
733 if (!entry_code)
735 *pdi = NULL;
736 return TRUE;
738 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
739 if (!abbrev)
741 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
742 return FALSE;
744 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
745 if (!di) return FALSE;
746 di->abbrev = abbrev;
747 di->symt = NULL;
749 if (abbrev->num_attr)
751 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
752 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
754 di->data[i] = traverse->data;
755 dwarf2_swallow_attribute(traverse, attr);
758 else di->data = NULL;
759 if (abbrev->have_child)
761 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
762 while (traverse->data < traverse->end_data)
764 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
765 if (!child) break;
766 where = vector_add(&di->children, &ctx->pool);
767 if (!where) return FALSE;
768 *where = child;
771 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
772 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
774 WARN("setting cursor for %s to next sibling <0x%lx>\n",
775 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
776 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
778 *pdi = di;
779 return TRUE;
782 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
783 dwarf2_debug_info_t* di)
785 struct attribute name;
786 struct attribute size;
787 struct attribute encoding;
788 enum BasicType bt;
790 if (di->symt) return di->symt;
792 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
794 dwarf2_find_name(ctx, di, &name, "base_type");
795 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
796 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
798 switch (encoding.u.uvalue)
800 case DW_ATE_void: bt = btVoid; break;
801 case DW_ATE_address: bt = btULong; break;
802 case DW_ATE_boolean: bt = btBool; break;
803 case DW_ATE_complex_float: bt = btComplex; break;
804 case DW_ATE_float: bt = btFloat; break;
805 case DW_ATE_signed: bt = btInt; break;
806 case DW_ATE_unsigned: bt = btUInt; break;
807 case DW_ATE_signed_char: bt = btChar; break;
808 case DW_ATE_unsigned_char: bt = btChar; break;
809 default: bt = btNoType; break;
811 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
812 if (di->abbrev->have_child) FIXME("Unsupported children\n");
813 return di->symt;
816 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
817 dwarf2_debug_info_t* di)
819 struct symt* ref_type;
820 struct attribute name;
822 if (di->symt) return di->symt;
824 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
826 dwarf2_find_name(ctx, di, &name, "typedef");
827 ref_type = dwarf2_lookup_type(ctx, di);
829 if (name.u.string)
831 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
833 if (di->abbrev->have_child) FIXME("Unsupported children\n");
834 return di->symt;
837 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
838 dwarf2_debug_info_t* di)
840 struct symt* ref_type;
841 struct attribute size;
843 if (di->symt) return di->symt;
845 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
847 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
848 ref_type = dwarf2_lookup_type(ctx, di);
850 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
851 if (di->abbrev->have_child) FIXME("Unsupported children\n");
852 return di->symt;
855 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
856 dwarf2_debug_info_t* di)
858 struct symt* ref_type;
859 struct symt* idx_type = NULL;
860 struct attribute min, max, cnt;
861 dwarf2_debug_info_t** pchild = NULL;
862 dwarf2_debug_info_t* child;
864 if (di->symt) return di->symt;
866 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
868 if (!di->abbrev->have_child)
870 FIXME("array without range information\n");
871 return NULL;
873 ref_type = dwarf2_lookup_type(ctx, di);
875 while ((pchild = vector_iter_up(&di->children, pchild)))
877 child = *pchild;
878 switch (child->abbrev->tag)
880 case DW_TAG_subrange_type:
881 idx_type = dwarf2_lookup_type(ctx, child);
882 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
883 min.u.uvalue = 0;
884 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
885 max.u.uvalue = 0;
886 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
887 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
888 break;
889 default:
890 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
891 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
892 break;
895 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
896 return di->symt;
899 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
900 dwarf2_debug_info_t* di)
902 struct symt* ref_type;
904 if (di->symt) return di->symt;
906 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
908 ref_type = dwarf2_lookup_type(ctx, di);
909 if (di->abbrev->have_child) FIXME("Unsupported children\n");
910 di->symt = ref_type;
912 return ref_type;
915 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
916 dwarf2_debug_info_t* di)
918 struct symt* ref_type;
920 if (di->symt) return di->symt;
922 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
924 ref_type = dwarf2_lookup_type(ctx, di);
925 if (di->abbrev->have_child) FIXME("Unsupported children\n");
926 di->symt = ref_type;
928 return ref_type;
931 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
932 dwarf2_debug_info_t* di)
934 struct symt* ref_type = NULL;
936 if (di->symt) return di->symt;
938 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
940 ref_type = dwarf2_lookup_type(ctx, di);
941 /* FIXME: for now, we hard-wire C++ references to pointers */
942 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
944 if (di->abbrev->have_child) FIXME("Unsupported children\n");
946 return di->symt;
949 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
950 dwarf2_debug_info_t* di,
951 struct symt_udt* parent)
953 struct symt* elt_type;
954 struct attribute name;
955 unsigned long offset = 0;
956 struct attribute bit_size;
957 struct attribute bit_offset;
959 assert(parent);
961 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
963 dwarf2_find_name(ctx, di, &name, "udt_member");
964 elt_type = dwarf2_lookup_type(ctx, di);
965 if (dwarf2_compute_location(ctx, di, DW_AT_data_member_location, &offset, NULL))
966 TRACE("found member_location at %s -> %lu\n", dwarf2_debug_ctx(ctx), offset);
967 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size)) bit_size.u.uvalue = 0;
968 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
970 /* FIXME: we should only do this when implementation is LSB (which is
971 * the case on i386 processors)
973 struct attribute nbytes;
974 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
976 DWORD64 size;
977 nbytes.u.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
979 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
981 else bit_offset.u.uvalue = 0;
982 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
983 (offset << 3) + bit_offset.u.uvalue, bit_size.u.uvalue);
985 if (di->abbrev->have_child) FIXME("Unsupported children\n");
988 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
989 dwarf2_debug_info_t* di,
990 enum UdtKind udt)
992 struct attribute name;
993 struct attribute size;
995 if (di->symt) return di->symt;
997 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
999 dwarf2_find_name(ctx, di, &name, "udt");
1000 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1002 di->symt = &symt_new_udt(ctx->module, name.u.string, size.u.uvalue, udt)->symt;
1004 if (di->abbrev->have_child) /** any interest to not have child ? */
1006 dwarf2_debug_info_t** pchild = NULL;
1007 dwarf2_debug_info_t* child;
1009 while ((pchild = vector_iter_up(&di->children, pchild)))
1011 child = *pchild;
1013 switch (child->abbrev->tag)
1015 case DW_TAG_member:
1016 /* FIXME: should I follow the sibling stuff ?? */
1017 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1018 break;
1019 case DW_TAG_enumeration_type:
1020 dwarf2_parse_enumeration_type(ctx, child);
1021 break;
1022 case DW_TAG_structure_type:
1023 case DW_TAG_class_type:
1024 case DW_TAG_union_type:
1025 /* FIXME: we need to handle nested udt definitions */
1026 break;
1027 default:
1028 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1029 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1030 break;
1035 return di->symt;
1038 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1039 dwarf2_debug_info_t* di,
1040 struct symt_enum* parent)
1042 struct attribute name;
1043 struct attribute value;
1045 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1047 dwarf2_find_name(ctx, di, &name, "enum_value");
1048 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1049 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1051 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1054 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1055 dwarf2_debug_info_t* di)
1057 struct attribute name;
1058 struct attribute size;
1060 if (di->symt) return di->symt;
1062 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1064 dwarf2_find_name(ctx, di, &name, "enum");
1065 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1067 di->symt = &symt_new_enum(ctx->module, name.u.string)->symt;
1069 if (di->abbrev->have_child) /* any interest to not have child ? */
1071 dwarf2_debug_info_t** pchild = NULL;
1072 dwarf2_debug_info_t* child;
1074 /* FIXME: should we use the sibling stuff ?? */
1075 while ((pchild = vector_iter_up(&di->children, pchild)))
1077 child = *pchild;
1079 switch (child->abbrev->tag)
1081 case DW_TAG_enumerator:
1082 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1083 break;
1084 default:
1085 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1086 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1090 return di->symt;
1093 static unsigned dwarf2_map_register(int regno)
1095 unsigned reg;
1097 switch (regno)
1099 case Wine_DW_no_register: FIXME("What the heck map reg 0x%x\n",regno); reg = 0; break;
1100 /* FIXME: this is a dirty hack */
1101 case Wine_DW_frame_register: reg = 0; break;
1102 case 0: reg = CV_REG_EAX; break;
1103 case 1: reg = CV_REG_ECX; break;
1104 case 2: reg = CV_REG_EDX; break;
1105 case 3: reg = CV_REG_EBX; break;
1106 case 4: reg = CV_REG_ESP; break;
1107 case 5: reg = CV_REG_EBP; break;
1108 case 6: reg = CV_REG_ESI; break;
1109 case 7: reg = CV_REG_EDI; break;
1110 case 8: reg = CV_REG_EIP; break;
1111 case 9: reg = CV_REG_EFLAGS; break;
1112 case 10: reg = CV_REG_CS; break;
1113 case 11: reg = CV_REG_SS; break;
1114 case 12: reg = CV_REG_DS; break;
1115 case 13: reg = CV_REG_ES; break;
1116 case 14: reg = CV_REG_FS; break;
1117 case 15: reg = CV_REG_GS; break;
1118 case 16: case 17: case 18: case 19:
1119 case 20: case 21: case 22: case 23:
1120 reg = CV_REG_ST0 + regno - 16; break;
1121 case 24: reg = CV_REG_CTRL; break;
1122 case 25: reg = CV_REG_STAT; break;
1123 case 26: reg = CV_REG_TAG; break;
1125 reg: fiseg 27
1126 reg: fioff 28
1127 reg: foseg 29
1128 reg: fooff 30
1129 reg: fop 31
1131 case 32: case 33: case 34: case 35:
1132 case 36: case 37: case 38: case 39:
1133 reg = CV_REG_XMM0 + regno - 32; break;
1134 case 40: reg = CV_REG_MXCSR; break;
1135 default:
1136 FIXME("Don't know how to map register %d\n", regno);
1137 return 0;
1139 return reg;
1142 /* structure used to pass information around when parsing a subprogram */
1143 typedef struct dwarf2_subprogram_s
1145 dwarf2_parse_context_t* ctx;
1146 struct symt_compiland* compiland;
1147 struct symt_function* func;
1148 unsigned long frame_offset;
1149 int frame_reg;
1150 } dwarf2_subprogram_t;
1152 /******************************************************************
1153 * dwarf2_parse_variable
1155 * Parses any variable (parameter, local/global variable)
1157 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1158 struct symt_block* block,
1159 dwarf2_debug_info_t* di)
1161 struct symt* param_type;
1162 struct attribute name, value;
1163 unsigned long offset;
1164 int in_reg;
1165 BOOL is_pmt = di->abbrev->tag == DW_TAG_formal_parameter;
1167 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1169 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1170 dwarf2_find_name(subpgm->ctx, di, &name, "parameter");
1171 if (dwarf2_compute_location(subpgm->ctx, di, DW_AT_location, &offset, &in_reg))
1173 struct attribute ext;
1175 TRACE("found parameter %s/%ld (reg=%d) at %s\n",
1176 name.u.string, offset, in_reg, dwarf2_debug_ctx(subpgm->ctx));
1177 switch (in_reg & ~Wine_DW_register_deref)
1179 case Wine_DW_no_register:
1180 /* it's a global variable */
1181 /* FIXME: we don't handle it's scope yet */
1182 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1183 ext.u.uvalue = 0;
1184 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1185 name.u.string, !ext.u.uvalue,
1186 subpgm->ctx->module->module.BaseOfImage + offset,
1187 0, param_type);
1188 break;
1189 case Wine_DW_frame_register:
1190 in_reg = subpgm->frame_reg | Wine_DW_register_deref;
1191 offset += subpgm->frame_offset;
1192 /* fall through */
1193 default:
1194 /* either a pmt/variable relative to frame pointer or
1195 * pmt/variable in a register
1197 assert(subpgm->func);
1198 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1199 is_pmt ? DataIsParam : DataIsLocal,
1200 dwarf2_map_register(in_reg & ~Wine_DW_register_deref),
1201 in_reg & Wine_DW_register_deref,
1202 offset, block, param_type, name.u.string);
1203 break;
1206 if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1208 FIXME("NIY: const value %08lx for %s\n", value.u.uvalue, name.u.string);
1210 if (is_pmt && subpgm->func && subpgm->func->type)
1211 symt_add_function_signature_parameter(subpgm->ctx->module,
1212 (struct symt_function_signature*)subpgm->func->type,
1213 param_type);
1215 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1218 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1219 dwarf2_debug_info_t* di)
1221 struct attribute name;
1222 struct attribute low_pc;
1224 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1226 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1227 dwarf2_find_name(subpgm->ctx, di, &name, "label");
1229 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1230 subpgm->ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1231 name.u.string);
1234 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1235 struct symt_block* block_parent,
1236 dwarf2_debug_info_t* di);
1238 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1239 dwarf2_debug_info_t* di)
1241 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1243 /* FIXME: attributes to handle:
1244 DW_AT_low_pc:
1245 DW_AT_high_pc:
1246 DW_AT_name:
1249 if (di->abbrev->have_child) /** any interest to not have child ? */
1251 dwarf2_debug_info_t** pchild = NULL;
1252 dwarf2_debug_info_t* child;
1254 while ((pchild = vector_iter_up(&di->children, pchild)))
1256 child = *pchild;
1258 switch (child->abbrev->tag)
1260 case DW_TAG_formal_parameter:
1261 /* FIXME: this is not properly supported yet
1262 * dwarf2_parse_subprogram_parameter(ctx, child, NULL);
1264 break;
1265 case DW_TAG_variable:
1266 /* FIXME:
1267 * dwarf2_parse_variable(ctx, child);
1269 break;
1270 case DW_TAG_lexical_block:
1271 /* FIXME:
1272 dwarf2_parse_subprogram_block(ctx, child, func);
1274 break;
1275 case DW_TAG_inlined_subroutine:
1276 /* FIXME */
1277 dwarf2_parse_inlined_subroutine(subpgm, child);
1278 break;
1279 case DW_TAG_label:
1280 dwarf2_parse_subprogram_label(subpgm, child);
1281 break;
1282 default:
1283 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1284 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1285 dwarf2_debug_di(di));
1291 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1292 struct symt_block* parent_block,
1293 dwarf2_debug_info_t* di)
1295 struct symt_block* block;
1296 struct attribute low_pc;
1297 struct attribute high_pc;
1299 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1301 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1302 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1304 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1305 low_pc.u.uvalue, high_pc.u.uvalue - low_pc.u.uvalue);
1307 if (di->abbrev->have_child) /** any interest to not have child ? */
1309 dwarf2_debug_info_t** pchild = NULL;
1310 dwarf2_debug_info_t* child;
1312 while ((pchild = vector_iter_up(&di->children, pchild)))
1314 child = *pchild;
1316 switch (child->abbrev->tag)
1318 case DW_TAG_inlined_subroutine:
1319 dwarf2_parse_inlined_subroutine(subpgm, child);
1320 break;
1321 case DW_TAG_variable:
1322 dwarf2_parse_variable(subpgm, block, child);
1323 break;
1324 case DW_TAG_lexical_block:
1325 dwarf2_parse_subprogram_block(subpgm, block, child);
1326 break;
1327 case DW_TAG_subprogram:
1328 /* FIXME: likely a declaration (to be checked)
1329 * skip it for now
1331 break;
1332 case DW_TAG_formal_parameter:
1333 /* FIXME: likely elements for exception handling (GCC flavor)
1334 * Skip it for now
1336 break;
1337 case DW_TAG_class_type:
1338 case DW_TAG_structure_type:
1339 case DW_TAG_union_type:
1340 case DW_TAG_enumeration_type:
1341 /* the type referred to will be loaded when we need it, so skip it */
1342 break;
1343 default:
1344 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1345 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1350 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1353 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1354 dwarf2_debug_info_t* di,
1355 struct symt_compiland* compiland)
1357 struct attribute name;
1358 struct attribute low_pc;
1359 struct attribute high_pc;
1360 struct attribute is_decl;
1361 struct attribute inline_flags;
1362 struct symt* ret_type;
1363 struct symt_function_signature* sig_type;
1364 dwarf2_subprogram_t subpgm;
1366 if (di->symt) return di->symt;
1368 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1370 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1371 if (!dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc)) high_pc.u.uvalue = 0;
1372 /* As functions (defined as inline assembly) get debug info with dwarf
1373 * (not the case for stabs), we just drop Wine's thunks here...
1374 * Actual thunks will be created in elf_module from the symbol table
1376 if (elf_is_in_thunk_area(ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1377 ctx->thunks) >= 0)
1378 return NULL;
1379 if (!dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl)) is_decl.u.uvalue = 0;
1380 if (!dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags)) inline_flags.u.uvalue = 0;
1381 dwarf2_find_name(ctx, di, &name, "subprogram");
1382 ret_type = dwarf2_lookup_type(ctx, di);
1384 /* FIXME: assuming C source code */
1385 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1386 if (!is_decl.u.uvalue)
1388 subpgm.func = symt_new_function(ctx->module, compiland, name.u.string,
1389 ctx->module->module.BaseOfImage + low_pc.u.uvalue,
1390 high_pc.u.uvalue - low_pc.u.uvalue,
1391 &sig_type->symt);
1392 di->symt = &subpgm.func->symt;
1394 else subpgm.func = NULL;
1396 subpgm.ctx = ctx;
1397 subpgm.compiland = compiland;
1398 if (dwarf2_compute_location(ctx, di, DW_AT_frame_base, &subpgm.frame_offset, &subpgm.frame_reg)) {
1399 TRACE("For %s got %ld/%d\n", name.u.string, subpgm.frame_offset, subpgm.frame_reg);
1400 if (subpgm.frame_reg == Wine_DW_no_register) {
1401 /* Likely a constant, meaning a location list offset.
1402 We do not handle those yet. */
1403 /*FIXME("need to handle location lists\n"); */
1404 subpgm.frame_reg = 0;
1405 subpgm.frame_offset = 0;
1408 else /* on stack !! */
1410 subpgm.frame_reg = 0;
1411 subpgm.frame_offset = 0;
1414 if (di->abbrev->have_child) /** any interest to not have child ? */
1416 dwarf2_debug_info_t** pchild = NULL;
1417 dwarf2_debug_info_t* child;
1419 while ((pchild = vector_iter_up(&di->children, pchild)))
1421 child = *pchild;
1423 switch (child->abbrev->tag)
1425 case DW_TAG_variable:
1426 case DW_TAG_formal_parameter:
1427 dwarf2_parse_variable(&subpgm, NULL, child);
1428 break;
1429 case DW_TAG_lexical_block:
1430 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1431 break;
1432 case DW_TAG_inlined_subroutine:
1433 dwarf2_parse_inlined_subroutine(&subpgm, child);
1434 break;
1435 case DW_TAG_subprogram:
1436 /* FIXME: likely a declaration (to be checked)
1437 * skip it for now
1439 break;
1440 case DW_TAG_label:
1441 dwarf2_parse_subprogram_label(&subpgm, child);
1442 break;
1443 case DW_TAG_class_type:
1444 case DW_TAG_structure_type:
1445 case DW_TAG_union_type:
1446 case DW_TAG_enumeration_type:
1447 case DW_TAG_typedef:
1448 /* the type referred to will be loaded when we need it, so skip it */
1449 break;
1450 case DW_TAG_unspecified_parameters:
1451 /* FIXME: no support in dbghelp's internals so far */
1452 break;
1453 default:
1454 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1455 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1460 symt_normalize_function(subpgm.ctx->module, subpgm.func);
1462 return di->symt;
1465 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1466 dwarf2_debug_info_t* di)
1468 struct symt* ret_type;
1469 struct symt_function_signature* sig_type;
1471 if (di->symt) return di->symt;
1473 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1475 ret_type = dwarf2_lookup_type(ctx, di);
1477 /* FIXME: assuming C source code */
1478 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1480 if (di->abbrev->have_child) /** any interest to not have child ? */
1482 dwarf2_debug_info_t** pchild = NULL;
1483 dwarf2_debug_info_t* child;
1485 while ((pchild = vector_iter_up(&di->children, pchild)))
1487 child = *pchild;
1489 switch (child->abbrev->tag)
1491 case DW_TAG_formal_parameter:
1492 symt_add_function_signature_parameter(ctx->module, sig_type,
1493 dwarf2_lookup_type(ctx, child));
1494 break;
1495 case DW_TAG_unspecified_parameters:
1496 WARN("Unsupported unspecified parameters\n");
1497 break;
1502 return di->symt = &sig_type->symt;
1505 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1506 dwarf2_debug_info_t* di,
1507 struct symt_compiland* compiland)
1509 switch (di->abbrev->tag)
1511 case DW_TAG_typedef:
1512 dwarf2_parse_typedef(ctx, di);
1513 break;
1514 case DW_TAG_base_type:
1515 dwarf2_parse_base_type(ctx, di);
1516 break;
1517 case DW_TAG_pointer_type:
1518 dwarf2_parse_pointer_type(ctx, di);
1519 break;
1520 case DW_TAG_class_type:
1521 dwarf2_parse_udt_type(ctx, di, UdtClass);
1522 break;
1523 case DW_TAG_structure_type:
1524 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1525 break;
1526 case DW_TAG_union_type:
1527 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1528 break;
1529 case DW_TAG_array_type:
1530 dwarf2_parse_array_type(ctx, di);
1531 break;
1532 case DW_TAG_const_type:
1533 dwarf2_parse_const_type(ctx, di);
1534 break;
1535 case DW_TAG_volatile_type:
1536 dwarf2_parse_volatile_type(ctx, di);
1537 break;
1538 case DW_TAG_reference_type:
1539 dwarf2_parse_reference_type(ctx, di);
1540 break;
1541 case DW_TAG_enumeration_type:
1542 dwarf2_parse_enumeration_type(ctx, di);
1543 break;
1544 case DW_TAG_subprogram:
1545 dwarf2_parse_subprogram(ctx, di, compiland);
1546 break;
1547 case DW_TAG_subroutine_type:
1548 dwarf2_parse_subroutine_type(ctx, di);
1549 break;
1550 case DW_TAG_variable:
1552 dwarf2_subprogram_t subpgm;
1554 subpgm.ctx = ctx;
1555 subpgm.compiland = compiland;
1556 subpgm.func = NULL;
1557 subpgm.frame_offset = 0;
1558 subpgm.frame_reg = 0;
1559 dwarf2_parse_variable(&subpgm, NULL, di);
1561 break;
1562 default:
1563 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
1564 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1568 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1569 struct vector* v, unsigned file, unsigned line)
1571 struct symt_function* func;
1572 int idx;
1573 unsigned* psrc;
1575 if (!file || !(psrc = vector_at(v, file - 1))) return;
1577 TRACE("%s %lx %s %u\n", module->module.ModuleName, address, source_get(module, *psrc), line);
1578 if ((idx = symt_find_nearest(module, address)) == -1 ||
1579 module->addr_sorttab[idx]->symt.tag != SymTagFunction) return;
1580 func = (struct symt_function*)module->addr_sorttab[idx];
1581 symt_add_func_line(module, func, *psrc, line, address - func->address);
1584 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1585 dwarf2_parse_context_t* ctx,
1586 const char* compile_dir,
1587 unsigned long offset)
1589 dwarf2_traverse_context_t traverse;
1590 unsigned long length;
1591 unsigned version, header_len, insn_size, default_stmt;
1592 unsigned line_range, opcode_base;
1593 int line_base;
1594 const unsigned char* opcode_len;
1595 struct vector dirs;
1596 struct vector files;
1597 const char** p;
1599 /* section with line numbers stripped */
1600 if (sections[section_line].address == NO_MAP)
1601 return FALSE;
1603 traverse.data = sections[section_line].address + offset;
1604 traverse.start_data = traverse.data;
1605 traverse.end_data = traverse.data + 4;
1606 traverse.word_size = ctx->word_size;
1608 length = dwarf2_parse_u4(&traverse);
1609 traverse.end_data = traverse.start_data + length;
1611 version = dwarf2_parse_u2(&traverse);
1612 header_len = dwarf2_parse_u4(&traverse);
1613 insn_size = dwarf2_parse_byte(&traverse);
1614 default_stmt = dwarf2_parse_byte(&traverse);
1615 line_base = (signed char)dwarf2_parse_byte(&traverse);
1616 line_range = dwarf2_parse_byte(&traverse);
1617 opcode_base = dwarf2_parse_byte(&traverse);
1619 opcode_len = traverse.data;
1620 traverse.data += opcode_base - 1;
1622 vector_init(&dirs, sizeof(const char*), 4);
1623 p = vector_add(&dirs, &ctx->pool);
1624 *p = compile_dir ? compile_dir : ".";
1625 while (*traverse.data)
1627 const char* rel = (const char*)traverse.data;
1628 unsigned rellen = strlen(rel);
1629 TRACE("Got include %s\n", rel);
1630 traverse.data += rellen + 1;
1631 p = vector_add(&dirs, &ctx->pool);
1633 if (*rel == '/' || !compile_dir)
1634 *p = rel;
1635 else
1637 /* include directory relative to compile directory */
1638 unsigned baselen = strlen(compile_dir);
1639 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
1640 strcpy(tmp, compile_dir);
1641 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
1642 strcpy(&tmp[baselen], rel);
1643 *p = tmp;
1647 traverse.data++;
1649 vector_init(&files, sizeof(unsigned), 16);
1650 while (*traverse.data)
1652 unsigned int dir_index, mod_time, length;
1653 const char* name;
1654 const char* dir;
1655 unsigned* psrc;
1657 name = (const char*)traverse.data;
1658 traverse.data += strlen(name) + 1;
1659 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1660 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1661 length = dwarf2_leb128_as_unsigned(&traverse);
1662 dir = *(const char**)vector_at(&dirs, dir_index);
1663 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1664 psrc = vector_add(&files, &ctx->pool);
1665 *psrc = source_new(ctx->module, dir, name);
1667 traverse.data++;
1669 while (traverse.data < traverse.end_data)
1671 unsigned long address = 0;
1672 unsigned file = 1;
1673 unsigned line = 1;
1674 unsigned is_stmt = default_stmt;
1675 BOOL basic_block = FALSE, end_sequence = FALSE;
1676 unsigned opcode, extopcode, i;
1678 while (!end_sequence)
1680 opcode = dwarf2_parse_byte(&traverse);
1681 TRACE("Got opcode %x\n", opcode);
1683 if (opcode >= opcode_base)
1685 unsigned delta = opcode - opcode_base;
1687 address += (delta / line_range) * insn_size;
1688 line += line_base + (delta % line_range);
1689 basic_block = TRUE;
1690 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1692 else
1694 switch (opcode)
1696 case DW_LNS_copy:
1697 basic_block = FALSE;
1698 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1699 break;
1700 case DW_LNS_advance_pc:
1701 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1702 break;
1703 case DW_LNS_advance_line:
1704 line += dwarf2_leb128_as_signed(&traverse);
1705 break;
1706 case DW_LNS_set_file:
1707 file = dwarf2_leb128_as_unsigned(&traverse);
1708 break;
1709 case DW_LNS_set_column:
1710 dwarf2_leb128_as_unsigned(&traverse);
1711 break;
1712 case DW_LNS_negate_stmt:
1713 is_stmt = !is_stmt;
1714 break;
1715 case DW_LNS_set_basic_block:
1716 basic_block = 1;
1717 break;
1718 case DW_LNS_const_add_pc:
1719 address += ((255 - opcode_base) / line_range) * insn_size;
1720 break;
1721 case DW_LNS_fixed_advance_pc:
1722 address += dwarf2_parse_u2(&traverse);
1723 break;
1724 case DW_LNS_extended_op:
1725 dwarf2_leb128_as_unsigned(&traverse);
1726 extopcode = dwarf2_parse_byte(&traverse);
1727 switch (extopcode)
1729 case DW_LNE_end_sequence:
1730 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1731 end_sequence = TRUE;
1732 break;
1733 case DW_LNE_set_address:
1734 address = ctx->module->module.BaseOfImage + dwarf2_parse_addr(&traverse);
1735 break;
1736 case DW_LNE_define_file:
1737 FIXME("not handled %s\n", traverse.data);
1738 traverse.data += strlen((const char *)traverse.data) + 1;
1739 dwarf2_leb128_as_unsigned(&traverse);
1740 dwarf2_leb128_as_unsigned(&traverse);
1741 dwarf2_leb128_as_unsigned(&traverse);
1742 break;
1743 default:
1744 FIXME("Unsupported extended opcode %x\n", extopcode);
1745 break;
1747 break;
1748 default:
1749 WARN("Unsupported opcode %x\n", opcode);
1750 for (i = 0; i < opcode_len[opcode]; i++)
1751 dwarf2_leb128_as_unsigned(&traverse);
1752 break;
1757 return TRUE;
1760 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1761 const dwarf2_comp_unit_t* comp_unit,
1762 struct module* module,
1763 const struct elf_thunk_area* thunks,
1764 const unsigned char* comp_unit_cursor)
1766 dwarf2_parse_context_t ctx;
1767 dwarf2_traverse_context_t traverse;
1768 dwarf2_traverse_context_t abbrev_ctx;
1769 dwarf2_debug_info_t* di;
1770 BOOL ret = FALSE;
1772 TRACE("Compilation Unit Header found at 0x%x:\n",
1773 comp_unit_cursor - sections[section_debug].address);
1774 TRACE("- length: %lu\n", comp_unit->length);
1775 TRACE("- version: %u\n", comp_unit->version);
1776 TRACE("- abbrev_offset: %lu\n", comp_unit->abbrev_offset);
1777 TRACE("- word_size: %u\n", comp_unit->word_size);
1779 if (comp_unit->version != 2)
1781 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
1782 comp_unit->version);
1783 return FALSE;
1786 pool_init(&ctx.pool, 65536);
1787 ctx.sections = sections;
1788 ctx.section = section_debug;
1789 ctx.module = module;
1790 ctx.word_size = comp_unit->word_size;
1791 ctx.thunks = thunks;
1792 ctx.ref_offset = comp_unit_cursor - sections[section_debug].address;
1794 traverse.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
1795 traverse.data = traverse.start_data;
1796 traverse.word_size = comp_unit->word_size;
1797 traverse.end_data = comp_unit_cursor + comp_unit->length + sizeof(unsigned);
1799 abbrev_ctx.start_data = sections[section_abbrev].address + comp_unit->abbrev_offset;
1800 abbrev_ctx.data = abbrev_ctx.start_data;
1801 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
1802 abbrev_ctx.word_size = comp_unit->word_size;
1803 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
1805 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
1806 dwarf2_read_one_debug_info(&ctx, &traverse, &di);
1808 if (di->abbrev->tag == DW_TAG_compile_unit)
1810 struct attribute name;
1811 dwarf2_debug_info_t** pdi = NULL;
1812 struct attribute stmt_list;
1813 struct attribute comp_dir;
1815 dwarf2_find_name(&ctx, di, &name, "compiland");
1817 /* get working directory of current compilation unit */
1818 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
1819 comp_dir.u.string = NULL;
1821 di->symt = &symt_new_compiland(module, source_new(module, comp_dir.u.string, name.u.string))->symt;
1823 if (di->abbrev->have_child)
1825 while ((pdi = vector_iter_up(&di->children, pdi)))
1827 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
1830 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
1832 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
1833 module->module.LineNumbers = TRUE;
1835 ret = TRUE;
1837 else FIXME("Should have a compilation unit here\n");
1838 pool_destroy(&ctx.pool);
1839 return ret;
1842 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
1843 const struct elf_thunk_area* thunks,
1844 const unsigned char* debug, unsigned int debug_size,
1845 const unsigned char* abbrev, unsigned int abbrev_size,
1846 const unsigned char* str, unsigned int str_size,
1847 const unsigned char* line, unsigned int line_size)
1849 dwarf2_section_t section[section_max];
1850 const unsigned char*comp_unit_cursor = debug;
1851 const unsigned char*end_debug = debug + debug_size;
1853 section[section_debug].address = debug;
1854 section[section_debug].size = debug_size;
1855 section[section_abbrev].address = abbrev;
1856 section[section_abbrev].size = abbrev_size;
1857 section[section_string].address = str;
1858 section[section_string].size = str_size;
1859 section[section_line].address = line;
1860 section[section_line].size = line_size;
1862 while (comp_unit_cursor < end_debug)
1864 const dwarf2_comp_unit_stream_t* comp_unit_stream;
1865 dwarf2_comp_unit_t comp_unit;
1867 comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
1868 comp_unit.length = *(unsigned long*) comp_unit_stream->length;
1869 comp_unit.version = *(unsigned short*) comp_unit_stream->version;
1870 comp_unit.abbrev_offset = *(unsigned long*) comp_unit_stream->abbrev_offset;
1871 comp_unit.word_size = *(unsigned char*) comp_unit_stream->word_size;
1873 dwarf2_parse_compilation_unit(section, &comp_unit, module,
1874 thunks, comp_unit_cursor);
1875 comp_unit_cursor += comp_unit.length + sizeof(unsigned);
1877 module->module.SymType = SymDia;
1878 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
1879 /* FIXME: we could have a finer grain here */
1880 module->module.GlobalSymbols = TRUE;
1881 module->module.TypeInfo = TRUE;
1882 module->module.SourceIndexed = TRUE;
1883 module->module.Publics = TRUE;
1884 return TRUE;