wined3d: Print real unhandled D3DCMPFUNC value.
[wine/hacks.git] / dlls / dbghelp / dwarf.c
blobe129d99738b523c45bac63eff380eda6301cc9d4
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 {
112 unsigned long attribute;
113 unsigned long form;
114 struct dwarf2_abbrev_entry_attr_s* next;
115 } dwarf2_abbrev_entry_attr_t;
117 typedef struct dwarf2_abbrev_entry_s
119 unsigned long entry_code;
120 unsigned long tag;
121 unsigned char have_child;
122 unsigned num_attr;
123 dwarf2_abbrev_entry_attr_t* attrs;
124 } dwarf2_abbrev_entry_t;
126 struct dwarf2_block
128 unsigned size;
129 const unsigned char* ptr;
132 union attribute
134 unsigned long uvalue;
135 long svalue;
136 const char* string;
137 struct dwarf2_block* block;
140 typedef struct dwarf2_debug_info_s
142 unsigned long offset;
143 const dwarf2_abbrev_entry_t*abbrev;
144 struct symt* symt;
145 union attribute* attributes;
146 struct vector children;
147 } dwarf2_debug_info_t;
150 typedef struct dwarf2_section_s
152 const unsigned char* address;
153 unsigned size;
154 } dwarf2_section_t;
156 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_max};
158 typedef struct dwarf2_traverse_context_s
160 const dwarf2_section_t* sections;
161 unsigned section;
162 const unsigned char* data;
163 const unsigned char* start_data;
164 const unsigned char* end_data;
165 unsigned long offset;
166 unsigned char word_size;
167 } dwarf2_traverse_context_t;
169 typedef struct dwarf2_parse_context_s
171 struct pool pool;
172 struct module* module;
173 const struct elf_thunk_area*thunks;
174 struct sparse_array abbrev_table;
175 struct sparse_array debug_info_table;
176 unsigned char word_size;
177 } dwarf2_parse_context_t;
179 /* forward declarations */
180 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
182 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
184 unsigned char uvalue = *(const unsigned char*) ctx->data;
185 ctx->data += 1;
186 return uvalue;
189 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
191 unsigned short uvalue = *(const unsigned short*) ctx->data;
192 ctx->data += 2;
193 return uvalue;
196 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
198 unsigned long uvalue = *(const unsigned int*) ctx->data;
199 ctx->data += 4;
200 return uvalue;
203 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
205 unsigned long ret = 0;
206 unsigned char byte;
207 unsigned shift = 0;
209 assert( NULL != ctx );
213 byte = dwarf2_parse_byte(ctx);
214 ret |= (byte & 0x7f) << shift;
215 shift += 7;
216 } while (byte & 0x80);
218 return ret;
221 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
223 long ret = 0;
224 unsigned char byte;
225 unsigned shift = 0;
226 const unsigned size = sizeof(int) * 8;
228 assert( NULL != ctx );
232 byte = dwarf2_parse_byte(ctx);
233 ret |= (byte & 0x7f) << shift;
234 shift += 7;
235 } while (byte & 0x80);
237 /* as spec: sign bit of byte is 2nd high order bit (80x40)
238 * -> 0x80 is used as flag.
240 if ((shift < size) && (byte & 0x40))
242 ret |= - (1 << shift);
244 return ret;
247 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
249 unsigned long ret;
251 switch (ctx->word_size)
253 case 4:
254 ret = dwarf2_parse_u4(ctx);
255 break;
256 default:
257 FIXME("Unsupported Word Size %u\n", ctx->word_size);
258 ret = 0;
260 return ret;
263 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
265 return wine_dbg_sprintf("ctx(0x%x)", ctx->data - ctx->sections[ctx->section].address);
268 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
270 return wine_dbg_sprintf("ctx(%p,%s)", ctx, ctx->module->module.ModuleName);
273 static const char* dwarf2_debug_di(dwarf2_debug_info_t* di)
275 return wine_dbg_sprintf("debug_info(offset:0x%lx,abbrev:%p,symt:%p)",
276 di->offset, di->abbrev, di->symt);
279 static dwarf2_abbrev_entry_t*
280 dwarf2_abbrev_table_find_entry(struct sparse_array* abbrev_table,
281 unsigned long entry_code)
283 assert( NULL != abbrev_table );
284 return sparse_array_find(abbrev_table, entry_code);
287 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
288 struct sparse_array* abbrev_table,
289 struct pool* pool)
291 unsigned long entry_code;
292 dwarf2_abbrev_entry_t* abbrev_entry;
293 dwarf2_abbrev_entry_attr_t* new = NULL;
294 dwarf2_abbrev_entry_attr_t* last = NULL;
295 unsigned long attribute;
296 unsigned long form;
298 assert( NULL != abbrev_ctx );
300 TRACE("%s, end at %p\n",
301 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
303 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
304 while (abbrev_ctx->data < abbrev_ctx->end_data)
306 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
307 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
308 TRACE("found entry_code %lu\n", entry_code);
309 if (!entry_code)
311 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
312 break;
314 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
315 assert( NULL != abbrev_entry );
317 abbrev_entry->entry_code = entry_code;
318 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
319 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
320 abbrev_entry->attrs = NULL;
321 abbrev_entry->num_attr = 0;
323 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
324 abbrev_table, sparse_array_length(abbrev_table),
325 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
327 last = NULL;
328 while (1)
330 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
331 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
332 if (!attribute) break;
334 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
335 assert(new);
337 new->attribute = attribute;
338 new->form = form;
339 new->next = NULL;
340 if (abbrev_entry->attrs) last->next = new;
341 else abbrev_entry->attrs = new;
342 last = new;
343 abbrev_entry->num_attr++;
346 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
349 static void dwarf2_parse_attr_into_di(struct pool* pool,
350 dwarf2_traverse_context_t* ctx,
351 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
352 union attribute* attr)
355 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
357 switch (abbrev_attr->form) {
358 case DW_FORM_ref_addr:
359 case DW_FORM_addr:
360 attr->uvalue = dwarf2_parse_addr(ctx);
361 TRACE("addr<0x%lx>\n", attr->uvalue);
362 break;
364 case DW_FORM_flag:
365 attr->uvalue = dwarf2_parse_byte(ctx);
366 TRACE("flag<0x%lx>\n", attr->uvalue);
367 break;
369 case DW_FORM_data1:
370 attr->uvalue = dwarf2_parse_byte(ctx);
371 TRACE("data1<%lu>\n", attr->uvalue);
372 break;
374 case DW_FORM_data2:
375 attr->uvalue = dwarf2_parse_u2(ctx);
376 TRACE("data2<%lu>\n", attr->uvalue);
377 break;
379 case DW_FORM_data4:
380 attr->uvalue = dwarf2_parse_u4(ctx);
381 TRACE("data4<%lu>\n", attr->uvalue);
382 break;
384 case DW_FORM_data8:
385 FIXME("Unhandled 64bits support\n");
386 ctx->data += 8;
387 break;
389 case DW_FORM_ref1:
390 attr->uvalue = ctx->offset + dwarf2_parse_byte(ctx);
391 TRACE("ref1<0x%lx>\n", attr->uvalue);
392 break;
394 case DW_FORM_ref2:
395 attr->uvalue = ctx->offset + dwarf2_parse_u2(ctx);
396 TRACE("ref2<0x%lx>\n", attr->uvalue);
397 break;
399 case DW_FORM_ref4:
400 attr->uvalue = ctx->offset + dwarf2_parse_u4(ctx);
401 TRACE("ref4<0x%lx>\n", attr->uvalue);
402 break;
404 case DW_FORM_ref8:
405 FIXME("Unhandled 64 bit support\n");
406 ctx->data += 8;
407 break;
409 case DW_FORM_sdata:
410 attr->svalue = dwarf2_leb128_as_signed(ctx);
411 break;
413 case DW_FORM_ref_udata:
414 attr->uvalue = dwarf2_leb128_as_unsigned(ctx);
415 break;
417 case DW_FORM_udata:
418 attr->uvalue = dwarf2_leb128_as_unsigned(ctx);
419 break;
421 case DW_FORM_string:
422 attr->string = (const char*)ctx->data;
423 ctx->data += strlen(attr->string) + 1;
424 TRACE("string<%s>\n", attr->string);
425 break;
427 case DW_FORM_strp:
429 unsigned long offset = dwarf2_parse_u4(ctx);
430 attr->string = (const char*)ctx->sections[section_string].address + offset;
432 TRACE("strp<%s>\n", attr->string);
433 break;
434 case DW_FORM_block:
435 attr->block = pool_alloc(pool, sizeof(struct dwarf2_block));
436 attr->block->size = dwarf2_leb128_as_unsigned(ctx);
437 attr->block->ptr = ctx->data;
438 ctx->data += attr->block->size;
439 break;
441 case DW_FORM_block1:
442 attr->block = pool_alloc(pool, sizeof(struct dwarf2_block));
443 attr->block->size = dwarf2_parse_byte(ctx);
444 attr->block->ptr = ctx->data;
445 ctx->data += attr->block->size;
446 break;
448 case DW_FORM_block2:
449 attr->block = pool_alloc(pool, sizeof(struct dwarf2_block));
450 attr->block->size = dwarf2_parse_u2(ctx);
451 attr->block->ptr = ctx->data;
452 ctx->data += attr->block->size;
453 break;
455 case DW_FORM_block4:
456 attr->block = pool_alloc(pool, sizeof(struct dwarf2_block));
457 attr->block->size = dwarf2_parse_u4(ctx);
458 attr->block->ptr = ctx->data;
459 ctx->data += attr->block->size;
460 break;
462 default:
463 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
464 break;
468 static BOOL dwarf2_find_attribute(const dwarf2_debug_info_t* di,
469 unsigned at, union attribute* attr)
471 unsigned i;
472 dwarf2_abbrev_entry_attr_t* abbrev_attr;
474 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
476 if (abbrev_attr->attribute == at)
478 *attr = di->attributes[i];
479 return TRUE;
482 return FALSE;
485 static void dwarf2_find_name(dwarf2_parse_context_t* ctx,
486 const dwarf2_debug_info_t* di,
487 union attribute* attr, const char* pfx)
489 static int index;
491 if (!dwarf2_find_attribute(di, DW_AT_name, attr))
493 char* tmp = pool_alloc(&ctx->pool, strlen(pfx) + 16);
494 if (tmp) sprintf(tmp, "%s_%d", pfx, index++);
495 attr->string = tmp;
499 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*,
500 struct symt_compiland*);
502 #define Wine_DW_no_register -1
503 #define Wine_DW_frame_register -2
505 static unsigned long dwarf2_compute_location(dwarf2_parse_context_t* ctx,
506 struct dwarf2_block* block,
507 int* in_register)
509 unsigned long loc[64];
510 unsigned stk;
512 if (in_register) *in_register = Wine_DW_no_register;
513 loc[stk = 0] = 0;
515 if (block->size)
517 dwarf2_traverse_context_t lctx;
518 unsigned char op;
519 BOOL piece_found = FALSE;
521 lctx.data = block->ptr;
522 lctx.end_data = block->ptr + block->size;
523 lctx.word_size = ctx->word_size;
525 while (lctx.data < lctx.end_data)
527 op = dwarf2_parse_byte(&lctx);
528 switch (op)
530 case DW_OP_addr: loc[++stk] = dwarf2_parse_addr(&lctx); break;
531 case DW_OP_const1u: loc[++stk] = dwarf2_parse_byte(&lctx); break;
532 case DW_OP_const1s: loc[++stk] = (long)(signed char)dwarf2_parse_byte(&lctx); break;
533 case DW_OP_const2u: loc[++stk] = dwarf2_parse_u2(&lctx); break;
534 case DW_OP_const2s: loc[++stk] = (long)(short)dwarf2_parse_u2(&lctx); break;
535 case DW_OP_const4u: loc[++stk] = dwarf2_parse_u4(&lctx); break;
536 case DW_OP_const4s: loc[++stk] = dwarf2_parse_u4(&lctx); break;
537 case DW_OP_constu: loc[++stk] = dwarf2_leb128_as_unsigned(&lctx); break;
538 case DW_OP_consts: loc[++stk] = dwarf2_leb128_as_signed(&lctx); break;
539 case DW_OP_plus_uconst:
540 loc[stk] += dwarf2_leb128_as_unsigned(&lctx); break;
541 case DW_OP_reg0: case DW_OP_reg1: case DW_OP_reg2: case DW_OP_reg3:
542 case DW_OP_reg4: case DW_OP_reg5: case DW_OP_reg6: case DW_OP_reg7:
543 case DW_OP_reg8: case DW_OP_reg9: case DW_OP_reg10: case DW_OP_reg11:
544 case DW_OP_reg12: case DW_OP_reg13: case DW_OP_reg14: case DW_OP_reg15:
545 case DW_OP_reg16: case DW_OP_reg17: case DW_OP_reg18: case DW_OP_reg19:
546 case DW_OP_reg20: case DW_OP_reg21: case DW_OP_reg22: case DW_OP_reg23:
547 case DW_OP_reg24: case DW_OP_reg25: case DW_OP_reg26: case DW_OP_reg27:
548 case DW_OP_reg28: case DW_OP_reg29: case DW_OP_reg30: case DW_OP_reg31:
549 if (in_register)
551 /* dbghelp APIs don't know how to cope with this anyway
552 * (for example 'long long' stored in two registers)
553 * FIXME: We should tell winedbg how to deal with it (sigh)
555 if (!piece_found || (op - DW_OP_reg0 != *in_register + 1))
557 if (*in_register != Wine_DW_no_register)
558 FIXME("Only supporting one reg (%d -> %d)\n",
559 *in_register, op - DW_OP_reg0);
560 *in_register = op - DW_OP_reg0;
563 else FIXME("Found register, while not expecting it\n");
564 break;
565 case DW_OP_fbreg:
566 if (in_register)
568 if (*in_register != Wine_DW_no_register)
569 FIXME("Only supporting one reg (%d -> -2)\n", *in_register);
570 *in_register = Wine_DW_frame_register;
572 else FIXME("Found register, while not expecting it\n");
573 loc[++stk] = dwarf2_leb128_as_signed(&lctx);
574 break;
575 case DW_OP_piece:
577 unsigned sz = dwarf2_leb128_as_unsigned(&lctx);
578 WARN("Not handling OP_piece directly (size=%d)\n", sz);
579 piece_found = TRUE;
581 break;
582 default:
583 FIXME("Unhandled attr op: %x\n", op);
584 return loc[stk];
588 return loc[stk];
591 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
592 const dwarf2_debug_info_t* di)
594 union attribute attr;
596 if (dwarf2_find_attribute(di, DW_AT_type, &attr))
598 dwarf2_debug_info_t* type;
600 type = sparse_array_find(&ctx->debug_info_table, attr.uvalue);
601 if (!type) FIXME("Unable to find back reference to type %lx\n", attr.uvalue);
602 if (!type->symt)
604 /* load the debug info entity */
605 dwarf2_load_one_entry(ctx, type, NULL);
607 return type->symt;
609 return NULL;
612 /******************************************************************
613 * dwarf2_read_one_debug_info
615 * Loads into memory one debug info entry, and recursively its children (if any)
617 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
618 dwarf2_traverse_context_t* traverse,
619 dwarf2_debug_info_t** pdi)
621 const dwarf2_abbrev_entry_t*abbrev;
622 unsigned long entry_code;
623 unsigned long offset;
624 dwarf2_debug_info_t* di;
625 dwarf2_debug_info_t* child;
626 dwarf2_debug_info_t** where;
627 dwarf2_abbrev_entry_attr_t* attr;
628 unsigned i;
629 union attribute sibling;
631 offset = traverse->data - traverse->sections[traverse->section].address;
632 entry_code = dwarf2_leb128_as_unsigned(traverse);
633 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
634 if (!entry_code)
636 *pdi = NULL;
637 return TRUE;
639 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
640 if (!abbrev)
642 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
643 return FALSE;
645 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
646 if (!di) return FALSE;
647 di->offset = offset;
648 di->abbrev = abbrev;
649 di->symt = NULL;
651 if (abbrev->num_attr)
653 di->attributes = pool_alloc(&ctx->pool,
654 abbrev->num_attr * sizeof(union attribute));
655 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
657 dwarf2_parse_attr_into_di(&ctx->pool, traverse, attr, &di->attributes[i]);
660 else di->attributes = NULL;
661 if (abbrev->have_child)
663 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
664 while (traverse->data < traverse->end_data)
666 if (!dwarf2_read_one_debug_info(ctx, traverse, &child)) return FALSE;
667 if (!child) break;
668 where = vector_add(&di->children, &ctx->pool);
669 if (!where) return FALSE;
670 *where = child;
673 if (dwarf2_find_attribute(di, DW_AT_sibling, &sibling) &&
674 traverse->data != traverse->sections[traverse->section].address + sibling.uvalue)
676 WARN("setting cursor for %s to next sibling <0x%lx>\n",
677 dwarf2_debug_traverse_ctx(traverse), sibling.uvalue);
678 traverse->data = traverse->sections[traverse->section].address + sibling.uvalue;
680 *pdi = di;
681 return TRUE;
684 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
685 dwarf2_debug_info_t* di)
687 union attribute name;
688 union attribute size;
689 union attribute encoding;
690 enum BasicType bt;
692 if (di->symt) return di->symt;
694 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
696 dwarf2_find_name(ctx, di, &name, "base_type");
697 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.uvalue = 0;
698 if (!dwarf2_find_attribute(di, DW_AT_encoding, &encoding)) encoding.uvalue = DW_ATE_void;
700 switch (encoding.uvalue)
702 case DW_ATE_void: bt = btVoid; break;
703 case DW_ATE_address: bt = btULong; break;
704 case DW_ATE_boolean: bt = btBool; break;
705 case DW_ATE_complex_float: bt = btComplex; break;
706 case DW_ATE_float: bt = btFloat; break;
707 case DW_ATE_signed: bt = btInt; break;
708 case DW_ATE_unsigned: bt = btUInt; break;
709 case DW_ATE_signed_char: bt = btChar; break;
710 case DW_ATE_unsigned_char: bt = btChar; break;
711 default: bt = btNoType; break;
713 di->symt = &symt_new_basic(ctx->module, bt, name.string, size.uvalue)->symt;
714 if (di->abbrev->have_child) FIXME("Unsupported children\n");
715 return di->symt;
718 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
719 dwarf2_debug_info_t* di)
721 struct symt* ref_type;
722 union attribute name;
724 if (di->symt) return di->symt;
726 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
728 dwarf2_find_name(ctx, di, &name, "typedef");
729 ref_type = dwarf2_lookup_type(ctx, di);
731 if (name.string)
733 di->symt = &symt_new_typedef(ctx->module, ref_type, name.string)->symt;
735 if (di->abbrev->have_child) FIXME("Unsupported children\n");
736 return di->symt;
739 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
740 dwarf2_debug_info_t* di)
742 struct symt* ref_type;
743 union attribute size;
745 if (di->symt) return di->symt;
747 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
749 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.uvalue = 0;
750 ref_type = dwarf2_lookup_type(ctx, di);
752 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
753 if (di->abbrev->have_child) FIXME("Unsupported children\n");
754 return di->symt;
757 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
758 dwarf2_debug_info_t* di)
760 struct symt* ref_type;
761 struct symt* idx_type = NULL;
762 union attribute min, max, cnt;
763 dwarf2_debug_info_t** pchild = NULL;
764 dwarf2_debug_info_t* child;
766 if (di->symt) return di->symt;
768 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
770 if (!di->abbrev->have_child)
772 FIXME("array without range information\n");
773 return NULL;
775 ref_type = dwarf2_lookup_type(ctx, di);
777 while ((pchild = vector_iter_up(&di->children, pchild)))
779 child = *pchild;
780 switch (child->abbrev->tag)
782 case DW_TAG_subrange_type:
783 idx_type = dwarf2_lookup_type(ctx, child);
784 if (!dwarf2_find_attribute(child, DW_AT_lower_bound, &min))
785 min.uvalue = 0;
786 if (!dwarf2_find_attribute(child, DW_AT_upper_bound, &max))
787 max.uvalue = 0;
788 if (dwarf2_find_attribute(child, DW_AT_count, &cnt))
789 max.uvalue = min.uvalue + cnt.uvalue;
790 break;
791 default:
792 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
793 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
794 break;
797 di->symt = &symt_new_array(ctx->module, min.uvalue, max.uvalue, ref_type, idx_type)->symt;
798 return di->symt;
801 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
802 dwarf2_debug_info_t* di)
804 struct symt* ref_type;
806 if (di->symt) return di->symt;
808 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
810 ref_type = dwarf2_lookup_type(ctx, di);
811 if (di->abbrev->have_child) FIXME("Unsupported children\n");
812 di->symt = ref_type;
814 return ref_type;
817 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
818 dwarf2_debug_info_t* di)
820 struct symt* ref_type = NULL;
822 if (di->symt) return di->symt;
824 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
826 ref_type = dwarf2_lookup_type(ctx, di);
827 /* FIXME: for now, we hard-wire C++ references to pointers */
828 di->symt = &symt_new_pointer(ctx->module, ref_type)->symt;
830 if (di->abbrev->have_child) FIXME("Unsupported children\n");
832 return di->symt;
835 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
836 dwarf2_debug_info_t* di,
837 struct symt_udt* parent)
839 struct symt* elt_type;
840 union attribute name;
841 union attribute loc;
842 unsigned long offset = 0;
843 union attribute bit_size;
844 union attribute bit_offset;
846 assert(parent);
848 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
850 dwarf2_find_name(ctx, di, &name, "udt_member");
851 elt_type = dwarf2_lookup_type(ctx, di);
852 if (dwarf2_find_attribute(di, DW_AT_data_member_location, &loc))
854 TRACE("found member_location at %s\n", dwarf2_debug_ctx(ctx));
855 offset = dwarf2_compute_location(ctx, loc.block, NULL);
856 TRACE("found offset:%lu\n", offset);
858 if (!dwarf2_find_attribute(di, DW_AT_bit_size, &bit_size)) bit_size.uvalue = 0;
859 if (dwarf2_find_attribute(di, DW_AT_bit_offset, &bit_offset))
861 /* FIXME: we should only do this when implementation is LSB (which is
862 * the case on i386 processors)
864 union attribute nbytes;
865 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &nbytes))
867 DWORD64 size;
868 nbytes.uvalue = symt_get_info(elt_type, TI_GET_LENGTH, &size) ? (unsigned long)size : 0;
870 bit_offset.uvalue = nbytes.uvalue * 8 - bit_offset.uvalue - bit_size.uvalue;
872 else bit_offset.uvalue = 0;
873 symt_add_udt_element(ctx->module, parent, name.string, elt_type,
874 (offset << 3) + bit_offset.uvalue, bit_size.uvalue);
876 if (di->abbrev->have_child) FIXME("Unsupported children\n");
879 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
880 dwarf2_debug_info_t* di,
881 enum UdtKind udt)
883 union attribute name;
884 union attribute size;
886 if (di->symt) return di->symt;
888 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
890 dwarf2_find_name(ctx, di, &name, "udt");
891 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.uvalue = 0;
893 di->symt = &symt_new_udt(ctx->module, name.string, size.uvalue, udt)->symt;
895 if (di->abbrev->have_child) /** any interest to not have child ? */
897 dwarf2_debug_info_t** pchild = NULL;
898 dwarf2_debug_info_t* child;
900 while ((pchild = vector_iter_up(&di->children, pchild)))
902 child = *pchild;
904 switch (child->abbrev->tag)
906 case DW_TAG_member:
907 /* FIXME: should I follow the sibling stuff ?? */
908 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
909 break;
910 case DW_TAG_enumeration_type:
911 dwarf2_parse_enumeration_type(ctx, child);
912 break;
913 case DW_TAG_structure_type:
914 case DW_TAG_class_type:
915 case DW_TAG_union_type:
916 /* FIXME: we need to handle nested udt definitions */
917 break;
918 default:
919 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
920 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
921 break;
926 return di->symt;
929 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
930 dwarf2_debug_info_t* di,
931 struct symt_enum* parent)
933 union attribute name;
934 union attribute value;
936 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
938 dwarf2_find_name(ctx, di, &name, "enum_value");
939 if (!dwarf2_find_attribute(di, DW_AT_const_value, &value)) value.svalue = 0;
940 symt_add_enum_element(ctx->module, parent, name.string, value.svalue);
942 if (di->abbrev->have_child) FIXME("Unsupported children\n");
945 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
946 dwarf2_debug_info_t* di)
948 union attribute name;
949 union attribute size;
951 if (di->symt) return di->symt;
953 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
955 dwarf2_find_name(ctx, di, &name, "enum");
956 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.uvalue = 0;
958 di->symt = &symt_new_enum(ctx->module, name.string)->symt;
960 if (di->abbrev->have_child) /* any interest to not have child ? */
962 dwarf2_debug_info_t** pchild = NULL;
963 dwarf2_debug_info_t* child;
965 /* FIXME: should we use the sibling stuff ?? */
966 while ((pchild = vector_iter_up(&di->children, pchild)))
968 child = *pchild;
970 switch (child->abbrev->tag)
972 case DW_TAG_enumerator:
973 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
974 break;
975 default:
976 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
977 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
981 return di->symt;
984 static unsigned dwarf2_map_register(int regno)
986 unsigned reg;
988 switch (regno)
990 case Wine_DW_no_register: FIXME("What the heck\n"); reg = 0; break;
991 /* FIXME: this is a dirty hack */
992 case Wine_DW_frame_register: reg = 0; break;
993 case 0: reg = CV_REG_EAX; break;
994 case 1: reg = CV_REG_ECX; break;
995 case 2: reg = CV_REG_EDX; break;
996 case 3: reg = CV_REG_EBX; break;
997 case 4: reg = CV_REG_ESP; break;
998 case 5: reg = CV_REG_EBP; break;
999 case 6: reg = CV_REG_ESI; break;
1000 case 7: reg = CV_REG_EDI; break;
1001 case 8: reg = CV_REG_EIP; break;
1002 case 9: reg = CV_REG_EFLAGS; break;
1003 case 10: reg = CV_REG_CS; break;
1004 case 11: reg = CV_REG_SS; break;
1005 case 12: reg = CV_REG_DS; break;
1006 case 13: reg = CV_REG_ES; break;
1007 case 14: reg = CV_REG_FS; break;
1008 case 15: reg = CV_REG_GS; break;
1009 case 16: case 17: case 18: case 19:
1010 case 20: case 21: case 22: case 23:
1011 reg = CV_REG_ST0 + regno - 16; break;
1012 case 24: reg = CV_REG_CTRL; break;
1013 case 25: reg = CV_REG_STAT; break;
1014 case 26: reg = CV_REG_TAG; break;
1016 reg: fiseg 27
1017 reg: fioff 28
1018 reg: foseg 29
1019 reg: fooff 30
1020 reg: fop 31
1022 case 32: case 33: case 34: case 35:
1023 case 36: case 37: case 38: case 39:
1024 reg = CV_REG_XMM0 + regno - 32; break;
1025 case 40: reg = CV_REG_MXCSR; break;
1026 default:
1027 FIXME("Don't know how to map register %d\n", regno);
1028 return 0;
1030 return reg;
1033 /* structure used to pass information around when parsing a subprogram */
1034 typedef struct dwarf2_subprogram_s
1036 dwarf2_parse_context_t* ctx;
1037 struct symt_compiland* compiland;
1038 struct symt_function* func;
1039 long frame_offset;
1040 int frame_reg;
1041 } dwarf2_subprogram_t;
1043 /******************************************************************
1044 * dwarf2_parse_variable
1046 * Parses any variable (parameter, local/global variable)
1048 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1049 struct symt_block* block,
1050 dwarf2_debug_info_t* di)
1052 struct symt* param_type;
1053 union attribute name, loc, value;
1054 BOOL is_pmt = di->abbrev->tag == DW_TAG_formal_parameter;
1056 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1058 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1059 dwarf2_find_name(subpgm->ctx, di, &name, "parameter");
1060 if (dwarf2_find_attribute(di, DW_AT_location, &loc) && loc.block)
1062 union attribute ext;
1063 long offset;
1064 int in_reg;
1066 offset = dwarf2_compute_location(subpgm->ctx, loc.block, &in_reg);
1067 TRACE("found parameter %s/%ld (reg=%d) at %s\n",
1068 name.string, offset, in_reg, dwarf2_debug_ctx(subpgm->ctx));
1069 switch (in_reg)
1071 case Wine_DW_no_register:
1072 /* it's a global variable */
1073 /* FIXME: we don't handle it's scope yet */
1074 if (!dwarf2_find_attribute(di, DW_AT_external, &ext))
1075 ext.uvalue = 0;
1076 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1077 name.string, !ext.uvalue,
1078 subpgm->ctx->module->module.BaseOfImage + offset,
1079 0, param_type);
1080 break;
1081 case Wine_DW_frame_register:
1082 in_reg = subpgm->frame_reg;
1083 offset += subpgm->frame_offset;
1084 /* fall through */
1085 default:
1086 /* either a pmt/variable relative to frame pointer or
1087 * pmt/variable in a register
1089 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1090 is_pmt ? DataIsParam : DataIsLocal,
1091 dwarf2_map_register(in_reg), offset,
1092 block, param_type, name.string);
1093 break;
1096 if (dwarf2_find_attribute(di, DW_AT_const_value, &value))
1098 FIXME("NIY: const value %08lx for %s\n", value.uvalue, name.string);
1100 if (is_pmt && subpgm->func && subpgm->func->type)
1101 symt_add_function_signature_parameter(subpgm->ctx->module,
1102 (struct symt_function_signature*)subpgm->func->type,
1103 param_type);
1105 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1108 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1109 dwarf2_debug_info_t* di)
1111 union attribute name;
1112 union attribute low_pc;
1114 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1116 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1117 dwarf2_find_name(subpgm->ctx, di, &name, "label");
1119 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1120 subpgm->ctx->module->module.BaseOfImage + low_pc.uvalue, name.string);
1123 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1124 struct symt_block* block_parent,
1125 dwarf2_debug_info_t* di);
1127 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1128 dwarf2_debug_info_t* di)
1130 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1132 /* FIXME: attributes to handle:
1133 DW_AT_low_pc:
1134 DW_AT_high_pc:
1135 DW_AT_name:
1138 if (di->abbrev->have_child) /** any interest to not have child ? */
1140 dwarf2_debug_info_t** pchild = NULL;
1141 dwarf2_debug_info_t* child;
1143 while ((pchild = vector_iter_up(&di->children, pchild)))
1145 child = *pchild;
1147 switch (child->abbrev->tag)
1149 case DW_TAG_formal_parameter:
1150 /* FIXME: this is not properly supported yet
1151 * dwarf2_parse_subprogram_parameter(ctx, child, NULL);
1153 break;
1154 case DW_TAG_variable:
1155 /* FIXME:
1156 * dwarf2_parse_variable(ctx, child);
1158 break;
1159 case DW_TAG_lexical_block:
1160 /* FIXME:
1161 dwarf2_parse_subprogram_block(ctx, child, func);
1163 break;
1164 case DW_TAG_inlined_subroutine:
1165 /* FIXME */
1166 dwarf2_parse_inlined_subroutine(subpgm, child);
1167 break;
1168 case DW_TAG_label:
1169 dwarf2_parse_subprogram_label(subpgm, child);
1170 break;
1171 default:
1172 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1173 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1174 dwarf2_debug_di(di));
1180 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1181 struct symt_block* parent_block,
1182 dwarf2_debug_info_t* di)
1184 struct symt_block* block;
1185 union attribute low_pc;
1186 union attribute high_pc;
1188 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1190 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1191 if (!dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc)) high_pc.uvalue = 0;
1193 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1194 low_pc.uvalue, high_pc.uvalue - low_pc.uvalue);
1196 if (di->abbrev->have_child) /** any interest to not have child ? */
1198 dwarf2_debug_info_t** pchild = NULL;
1199 dwarf2_debug_info_t* child;
1201 while ((pchild = vector_iter_up(&di->children, pchild)))
1203 child = *pchild;
1205 switch (child->abbrev->tag)
1207 case DW_TAG_inlined_subroutine:
1208 dwarf2_parse_inlined_subroutine(subpgm, child);
1209 break;
1210 case DW_TAG_variable:
1211 dwarf2_parse_variable(subpgm, block, child);
1212 break;
1213 case DW_TAG_lexical_block:
1214 dwarf2_parse_subprogram_block(subpgm, block, child);
1215 break;
1216 case DW_TAG_subprogram:
1217 /* FIXME: likely a declaration (to be checked)
1218 * skip it for now
1220 break;
1221 case DW_TAG_formal_parameter:
1222 /* FIXME: likely elements for exception handling (GCC flavor)
1223 * Skip it for now
1225 break;
1226 case DW_TAG_class_type:
1227 case DW_TAG_structure_type:
1228 case DW_TAG_union_type:
1229 case DW_TAG_enumeration_type:
1230 /* the type referred to will be loaded when we need it, so skip it */
1231 break;
1232 default:
1233 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1234 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1239 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1242 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1243 dwarf2_debug_info_t* di,
1244 struct symt_compiland* compiland)
1246 union attribute name;
1247 union attribute low_pc;
1248 union attribute high_pc;
1249 union attribute is_decl;
1250 union attribute inline_flags;
1251 union attribute frame;
1252 struct symt* ret_type;
1253 struct symt_function_signature* sig_type;
1254 dwarf2_subprogram_t subpgm;
1256 if (di->symt) return di->symt;
1258 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1260 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1261 if (!dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc)) high_pc.uvalue = 0;
1262 /* As functions (defined as inline assembly) get debug info with dwarf
1263 * (not the case for stabs), we just drop Wine's thunks here...
1264 * Actual thunks will be created in elf_module from the symbol table
1266 if (elf_is_in_thunk_area(ctx->module->module.BaseOfImage + low_pc.uvalue,
1267 ctx->thunks) >= 0)
1268 return NULL;
1269 if (!dwarf2_find_attribute(di, DW_AT_declaration, &is_decl)) is_decl.uvalue = 0;
1270 if (!dwarf2_find_attribute(di, DW_AT_inline, &inline_flags)) inline_flags.uvalue = 0;
1271 dwarf2_find_name(ctx, di, &name, "subprogram");
1272 ret_type = dwarf2_lookup_type(ctx, di);
1274 /* FIXME: assuming C source code */
1275 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1276 if (!is_decl.uvalue)
1278 subpgm.func = symt_new_function(ctx->module, compiland, name.string,
1279 ctx->module->module.BaseOfImage + low_pc.uvalue,
1280 high_pc.uvalue - low_pc.uvalue,
1281 &sig_type->symt);
1282 di->symt = &subpgm.func->symt;
1284 else subpgm.func = NULL;
1286 subpgm.ctx = ctx;
1287 subpgm.compiland = compiland;
1288 if (dwarf2_find_attribute(di, DW_AT_frame_base, &frame))
1290 subpgm.frame_offset = dwarf2_compute_location(ctx, frame.block, &subpgm.frame_reg);
1291 TRACE("For %s got %ld/%d\n", name.string, subpgm.frame_offset, subpgm.frame_reg);
1293 else /* on stack !! */
1295 subpgm.frame_reg = 0;
1296 subpgm.frame_offset = 0;
1299 if (di->abbrev->have_child) /** any interest to not have child ? */
1301 dwarf2_debug_info_t** pchild = NULL;
1302 dwarf2_debug_info_t* child;
1304 while ((pchild = vector_iter_up(&di->children, pchild)))
1306 child = *pchild;
1308 switch (child->abbrev->tag)
1310 case DW_TAG_variable:
1311 case DW_TAG_formal_parameter:
1312 dwarf2_parse_variable(&subpgm, NULL, child);
1313 break;
1314 case DW_TAG_lexical_block:
1315 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1316 break;
1317 case DW_TAG_inlined_subroutine:
1318 dwarf2_parse_inlined_subroutine(&subpgm, child);
1319 break;
1320 case DW_TAG_subprogram:
1321 /* FIXME: likely a declaration (to be checked)
1322 * skip it for now
1324 break;
1325 case DW_TAG_label:
1326 dwarf2_parse_subprogram_label(&subpgm, child);
1327 break;
1328 case DW_TAG_class_type:
1329 case DW_TAG_structure_type:
1330 case DW_TAG_union_type:
1331 case DW_TAG_enumeration_type:
1332 case DW_TAG_typedef:
1333 /* the type referred to will be loaded when we need it, so skip it */
1334 break;
1335 case DW_TAG_unspecified_parameters:
1336 /* FIXME: no support in dbghelp's internals so far */
1337 break;
1338 default:
1339 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1340 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1345 symt_normalize_function(subpgm.ctx->module, subpgm.func);
1347 return di->symt;
1350 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1351 dwarf2_debug_info_t* di,
1352 struct symt_compiland* compiland)
1354 switch (di->abbrev->tag)
1356 case DW_TAG_typedef:
1357 dwarf2_parse_typedef(ctx, di);
1358 break;
1359 case DW_TAG_base_type:
1360 dwarf2_parse_base_type(ctx, di);
1361 break;
1362 case DW_TAG_pointer_type:
1363 dwarf2_parse_pointer_type(ctx, di);
1364 break;
1365 case DW_TAG_class_type:
1366 dwarf2_parse_udt_type(ctx, di, UdtClass);
1367 break;
1368 case DW_TAG_structure_type:
1369 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1370 break;
1371 case DW_TAG_union_type:
1372 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1373 break;
1374 case DW_TAG_array_type:
1375 dwarf2_parse_array_type(ctx, di);
1376 break;
1377 case DW_TAG_const_type:
1378 dwarf2_parse_const_type(ctx, di);
1379 break;
1380 case DW_TAG_reference_type:
1381 dwarf2_parse_reference_type(ctx, di);
1382 break;
1383 case DW_TAG_enumeration_type:
1384 dwarf2_parse_enumeration_type(ctx, di);
1385 break;
1386 case DW_TAG_subprogram:
1387 dwarf2_parse_subprogram(ctx, di, compiland);
1388 break;
1389 default:
1390 WARN("Unhandled Tag type 0x%lx at %s, for %lu\n",
1391 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1395 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1396 struct vector* v, unsigned file, unsigned line)
1398 struct symt_function* func;
1399 int idx;
1400 unsigned* psrc;
1402 if (!file || !(psrc = vector_at(v, file - 1))) return;
1404 TRACE("%s %lx %s %u\n", module->module.ModuleName, address, source_get(module, *psrc), line);
1405 if ((idx = symt_find_nearest(module, address)) == -1 ||
1406 module->addr_sorttab[idx]->symt.tag != SymTagFunction) return;
1407 func = (struct symt_function*)module->addr_sorttab[idx];
1408 symt_add_func_line(module, func, *psrc, line, address - func->address);
1411 static void dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1412 dwarf2_parse_context_t* ctx,
1413 unsigned long offset)
1415 dwarf2_traverse_context_t traverse;
1416 unsigned long length;
1417 unsigned version, header_len, insn_size, default_stmt;
1418 unsigned line_range, opcode_base;
1419 int line_base;
1420 const unsigned char* opcode_len;
1421 struct vector dirs;
1422 struct vector files;
1423 const char** p;
1425 traverse.sections = sections;
1426 traverse.section = section_line;
1427 traverse.data = sections[section_line].address + offset;
1428 traverse.start_data = traverse.data;
1429 traverse.end_data = traverse.data + 4;
1430 traverse.offset = offset;
1431 traverse.word_size = ctx->word_size;
1433 length = dwarf2_parse_u4(&traverse);
1434 traverse.end_data = traverse.start_data + length;
1436 version = dwarf2_parse_u2(&traverse);
1437 header_len = dwarf2_parse_u4(&traverse);
1438 insn_size = dwarf2_parse_byte(&traverse);
1439 default_stmt = dwarf2_parse_byte(&traverse);
1440 line_base = (signed char)dwarf2_parse_byte(&traverse);
1441 line_range = dwarf2_parse_byte(&traverse);
1442 opcode_base = dwarf2_parse_byte(&traverse);
1444 opcode_len = traverse.data;
1445 traverse.data += opcode_base - 1;
1447 vector_init(&dirs, sizeof(const char*), 4);
1448 p = vector_add(&dirs, &ctx->pool);
1449 *p = ".";
1450 while (*traverse.data)
1452 TRACE("Got include %s\n", (const char*)traverse.data);
1453 p = vector_add(&dirs, &ctx->pool);
1454 *p = (const char *)traverse.data;
1455 traverse.data += strlen((const char *)traverse.data) + 1;
1457 traverse.data++;
1459 vector_init(&files, sizeof(unsigned), 16);
1460 while (*traverse.data)
1462 unsigned int dir_index, mod_time, length;
1463 const char* name;
1464 const char* dir;
1465 unsigned* psrc;
1467 name = (const char*)traverse.data;
1468 traverse.data += strlen(name) + 1;
1469 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1470 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1471 length = dwarf2_leb128_as_unsigned(&traverse);
1472 dir = *(const char**)vector_at(&dirs, dir_index);
1473 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1474 psrc = vector_add(&files, &ctx->pool);
1475 *psrc = source_new(ctx->module, dir, name);
1477 traverse.data++;
1479 while (traverse.data < traverse.end_data)
1481 unsigned long address = 0;
1482 unsigned file = 1;
1483 unsigned line = 1;
1484 unsigned is_stmt = default_stmt;
1485 BOOL basic_block = FALSE, end_sequence = FALSE;
1486 unsigned opcode, extopcode, i;
1488 while (!end_sequence)
1490 opcode = dwarf2_parse_byte(&traverse);
1491 TRACE("Got opcode %x\n", opcode);
1493 if (opcode >= opcode_base)
1495 unsigned delta = opcode - opcode_base;
1497 address += (delta / line_range) * insn_size;
1498 line += line_base + (delta % line_range);
1499 basic_block = TRUE;
1500 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1502 else
1504 switch (opcode)
1506 case DW_LNS_copy:
1507 basic_block = FALSE;
1508 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1509 break;
1510 case DW_LNS_advance_pc:
1511 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1512 break;
1513 case DW_LNS_advance_line:
1514 line += dwarf2_leb128_as_signed(&traverse);
1515 break;
1516 case DW_LNS_set_file:
1517 file = dwarf2_leb128_as_unsigned(&traverse);
1518 break;
1519 case DW_LNS_set_column:
1520 dwarf2_leb128_as_unsigned(&traverse);
1521 break;
1522 case DW_LNS_negate_stmt:
1523 is_stmt = !is_stmt;
1524 break;
1525 case DW_LNS_set_basic_block:
1526 basic_block = 1;
1527 break;
1528 case DW_LNS_const_add_pc:
1529 address += ((255 - opcode_base) / line_range) * insn_size;
1530 break;
1531 case DW_LNS_fixed_advance_pc:
1532 address += dwarf2_parse_u2(&traverse);
1533 break;
1534 case DW_LNS_extended_op:
1535 dwarf2_leb128_as_unsigned(&traverse);
1536 extopcode = dwarf2_parse_byte(&traverse);
1537 switch (extopcode)
1539 case DW_LNE_end_sequence:
1540 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1541 end_sequence = TRUE;
1542 break;
1543 case DW_LNE_set_address:
1544 address = ctx->module->module.BaseOfImage + dwarf2_parse_addr(&traverse);
1545 break;
1546 case DW_LNE_define_file:
1547 FIXME("not handled %s\n", traverse.data);
1548 traverse.data += strlen((const char *)traverse.data) + 1;
1549 dwarf2_leb128_as_unsigned(&traverse);
1550 dwarf2_leb128_as_unsigned(&traverse);
1551 dwarf2_leb128_as_unsigned(&traverse);
1552 break;
1553 default:
1554 FIXME("Unsupported extended opcode %x\n", extopcode);
1555 break;
1557 break;
1558 default:
1559 WARN("Unsupported opcode %x\n", opcode);
1560 for (i = 0; i < opcode_len[opcode]; i++)
1561 dwarf2_leb128_as_unsigned(&traverse);
1562 break;
1569 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1570 const dwarf2_comp_unit_t* comp_unit,
1571 struct module* module,
1572 const struct elf_thunk_area* thunks,
1573 const unsigned char* comp_unit_cursor)
1575 dwarf2_parse_context_t ctx;
1576 dwarf2_traverse_context_t traverse;
1577 dwarf2_traverse_context_t abbrev_ctx;
1578 dwarf2_debug_info_t* di;
1579 BOOL ret = FALSE;
1581 TRACE("Compilation Unit Header found at 0x%x:\n",
1582 comp_unit_cursor - sections[section_debug].address);
1583 TRACE("- length: %lu\n", comp_unit->length);
1584 TRACE("- version: %u\n", comp_unit->version);
1585 TRACE("- abbrev_offset: %lu\n", comp_unit->abbrev_offset);
1586 TRACE("- word_size: %u\n", comp_unit->word_size);
1588 if (comp_unit->version != 2)
1590 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
1591 comp_unit->version);
1592 return FALSE;
1595 pool_init(&ctx.pool, 65536);
1596 ctx.module = module;
1597 ctx.word_size = comp_unit->word_size;
1598 ctx.thunks = thunks;
1600 traverse.sections = sections;
1601 traverse.section = section_debug;
1602 traverse.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
1603 traverse.data = traverse.start_data;
1604 traverse.offset = comp_unit_cursor - sections[section_debug].address;
1605 traverse.word_size = comp_unit->word_size;
1606 traverse.end_data = comp_unit_cursor + comp_unit->length + sizeof(unsigned);
1608 abbrev_ctx.sections = sections;
1609 abbrev_ctx.section = section_abbrev;
1610 abbrev_ctx.start_data = sections[section_abbrev].address + comp_unit->abbrev_offset;
1611 abbrev_ctx.data = abbrev_ctx.start_data;
1612 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
1613 abbrev_ctx.offset = comp_unit->abbrev_offset;
1614 abbrev_ctx.word_size = comp_unit->word_size;
1615 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
1617 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
1618 dwarf2_read_one_debug_info(&ctx, &traverse, &di);
1620 if (di->abbrev->tag == DW_TAG_compile_unit)
1622 union attribute name;
1623 dwarf2_debug_info_t** pdi = NULL;
1624 union attribute stmt_list;
1626 TRACE("beginning at 0x%lx, for %lu\n", di->offset, di->abbrev->entry_code);
1628 dwarf2_find_name(&ctx, di, &name, "compiland");
1629 di->symt = &symt_new_compiland(module, source_new(module, NULL, name.string))->symt;
1631 if (di->abbrev->have_child)
1633 while ((pdi = vector_iter_up(&di->children, pdi)))
1635 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
1638 if (dwarf2_find_attribute(di, DW_AT_stmt_list, &stmt_list))
1640 dwarf2_parse_line_numbers(sections, &ctx, stmt_list.uvalue);
1642 ret = TRUE;
1644 else FIXME("Should have a compilation unit here\n");
1645 pool_destroy(&ctx.pool);
1646 return ret;
1649 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
1650 const struct elf_thunk_area* thunks,
1651 const unsigned char* debug, unsigned int debug_size,
1652 const unsigned char* abbrev, unsigned int abbrev_size,
1653 const unsigned char* str, unsigned int str_size,
1654 const unsigned char* line, unsigned int line_size)
1656 dwarf2_section_t section[section_max];
1657 const unsigned char*comp_unit_cursor = debug;
1658 const unsigned char*end_debug = debug + debug_size;
1660 section[section_debug].address = debug;
1661 section[section_debug].size = debug_size;
1662 section[section_abbrev].address = abbrev;
1663 section[section_abbrev].size = abbrev_size;
1664 section[section_string].address = str;
1665 section[section_string].size = str_size;
1666 section[section_line].address = line;
1667 section[section_line].size = line_size;
1669 while (comp_unit_cursor < end_debug)
1671 const dwarf2_comp_unit_stream_t* comp_unit_stream;
1672 dwarf2_comp_unit_t comp_unit;
1674 comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
1675 comp_unit.length = *(unsigned long*) comp_unit_stream->length;
1676 comp_unit.version = *(unsigned short*) comp_unit_stream->version;
1677 comp_unit.abbrev_offset = *(unsigned long*) comp_unit_stream->abbrev_offset;
1678 comp_unit.word_size = *(unsigned char*) comp_unit_stream->word_size;
1680 dwarf2_parse_compilation_unit(section, &comp_unit, module,
1681 thunks, comp_unit_cursor);
1682 comp_unit_cursor += comp_unit.length + sizeof(unsigned);
1684 module->module.SymType = SymDia;
1685 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
1686 /* FIXME: we could have a finer grain here */
1687 module->module.LineNumbers = TRUE;
1688 module->module.GlobalSymbols = TRUE;
1689 module->module.TypeInfo = TRUE;
1690 module->module.SourceIndexed = TRUE;
1691 module->module.Publics = TRUE;
1692 return TRUE;