msi: Change an ERR to a WARN.
[wine.git] / dlls / dbghelp / dwarf.c
blob01250a5f8641d3db1a5b57f9d4ce231d4e042c6e
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 loc;
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 if (dwarf2_find_attribute(di, DW_AT_location, &loc))
1061 union attribute name;
1062 union attribute ext;
1063 long offset;
1064 int in_reg;
1066 dwarf2_find_name(subpgm->ctx, di, &name, "parameter");
1067 offset = dwarf2_compute_location(subpgm->ctx, loc.block, &in_reg);
1068 TRACE("found parameter %s/%ld (reg=%d) at %s\n",
1069 name.string, offset, in_reg, dwarf2_debug_ctx(subpgm->ctx));
1070 switch (in_reg)
1072 case Wine_DW_no_register:
1073 /* it's a global variable */
1074 /* FIXME: we don't handle it's scope yet */
1075 if (!dwarf2_find_attribute(di, DW_AT_external, &ext))
1076 ext.uvalue = 0;
1077 symt_new_global_variable(subpgm->ctx->module, subpgm->compiland,
1078 name.string, !ext.uvalue,
1079 subpgm->ctx->module->module.BaseOfImage + offset,
1080 0, param_type);
1081 break;
1082 case Wine_DW_frame_register:
1083 in_reg = subpgm->frame_reg;
1084 offset += subpgm->frame_offset;
1085 /* fall through */
1086 default:
1087 /* either a pmt/variable relative to frame pointer or
1088 * pmt/variable in a register
1090 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1091 is_pmt ? DataIsParam : DataIsLocal,
1092 dwarf2_map_register(in_reg), offset,
1093 block, param_type, name.string);
1094 break;
1097 if (is_pmt && subpgm->func && subpgm->func->type)
1098 symt_add_function_signature_parameter(subpgm->ctx->module,
1099 (struct symt_function_signature*)subpgm->func->type,
1100 param_type);
1102 if (di->abbrev->have_child) FIXME("Unsupported children\n");
1105 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1106 dwarf2_debug_info_t* di)
1108 union attribute name;
1109 union attribute low_pc;
1111 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1113 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1114 dwarf2_find_name(subpgm->ctx, di, &name, "label");
1116 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1117 subpgm->ctx->module->module.BaseOfImage + low_pc.uvalue, name.string);
1120 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1121 struct symt_block* block_parent,
1122 dwarf2_debug_info_t* di);
1124 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1125 dwarf2_debug_info_t* di)
1127 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1129 /* FIXME: attributes to handle:
1130 DW_AT_low_pc:
1131 DW_AT_high_pc:
1132 DW_AT_name:
1135 if (di->abbrev->have_child) /** any interest to not have child ? */
1137 dwarf2_debug_info_t** pchild = NULL;
1138 dwarf2_debug_info_t* child;
1140 while ((pchild = vector_iter_up(&di->children, pchild)))
1142 child = *pchild;
1144 switch (child->abbrev->tag)
1146 case DW_TAG_formal_parameter:
1147 /* FIXME: this is not properly supported yet
1148 * dwarf2_parse_subprogram_parameter(ctx, child, NULL);
1150 break;
1151 case DW_TAG_variable:
1152 /* FIXME:
1153 * dwarf2_parse_variable(ctx, child);
1155 break;
1156 case DW_TAG_lexical_block:
1157 /* FIXME:
1158 dwarf2_parse_subprogram_block(ctx, child, func);
1160 break;
1161 case DW_TAG_inlined_subroutine:
1162 /* FIXME */
1163 dwarf2_parse_inlined_subroutine(subpgm, child);
1164 break;
1165 case DW_TAG_label:
1166 dwarf2_parse_subprogram_label(subpgm, child);
1167 break;
1168 default:
1169 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1170 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1171 dwarf2_debug_di(di));
1177 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1178 struct symt_block* parent_block,
1179 dwarf2_debug_info_t* di)
1181 struct symt_block* block;
1182 union attribute low_pc;
1183 union attribute high_pc;
1185 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1187 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1188 if (!dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc)) high_pc.uvalue = 0;
1190 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1191 low_pc.uvalue, high_pc.uvalue - low_pc.uvalue);
1193 if (di->abbrev->have_child) /** any interest to not have child ? */
1195 dwarf2_debug_info_t** pchild = NULL;
1196 dwarf2_debug_info_t* child;
1198 while ((pchild = vector_iter_up(&di->children, pchild)))
1200 child = *pchild;
1202 switch (child->abbrev->tag)
1204 case DW_TAG_inlined_subroutine:
1205 dwarf2_parse_inlined_subroutine(subpgm, child);
1206 break;
1207 case DW_TAG_variable:
1208 dwarf2_parse_variable(subpgm, block, child);
1209 break;
1210 case DW_TAG_lexical_block:
1211 dwarf2_parse_subprogram_block(subpgm, block, child);
1212 break;
1213 case DW_TAG_subprogram:
1214 /* FIXME: likely a declaration (to be checked)
1215 * skip it for now
1217 break;
1218 case DW_TAG_formal_parameter:
1219 /* FIXME: likely elements for exception handling (GCC flavor)
1220 * Skip it for now
1222 break;
1223 case DW_TAG_class_type:
1224 case DW_TAG_structure_type:
1225 case DW_TAG_union_type:
1226 case DW_TAG_enumeration_type:
1227 /* the type referred to will be loaded when we need it, so skip it */
1228 break;
1229 default:
1230 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1231 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1236 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1239 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1240 dwarf2_debug_info_t* di,
1241 struct symt_compiland* compiland)
1243 union attribute name;
1244 union attribute low_pc;
1245 union attribute high_pc;
1246 union attribute is_decl;
1247 union attribute inline_flags;
1248 union attribute frame;
1249 struct symt* ret_type;
1250 struct symt_function_signature* sig_type;
1251 dwarf2_subprogram_t subpgm;
1253 if (di->symt) return di->symt;
1255 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1257 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc)) low_pc.uvalue = 0;
1258 if (!dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc)) high_pc.uvalue = 0;
1259 /* As functions (defined as inline assembly) get debug info with dwarf
1260 * (not the case for stabs), we just drop Wine's thunks here...
1261 * Actual thunks will be created in elf_module from the symbol table
1263 if (elf_is_in_thunk_area(ctx->module->module.BaseOfImage + low_pc.uvalue,
1264 ctx->thunks) >= 0)
1265 return NULL;
1266 if (!dwarf2_find_attribute(di, DW_AT_declaration, &is_decl)) is_decl.uvalue = 0;
1267 if (!dwarf2_find_attribute(di, DW_AT_inline, &inline_flags)) inline_flags.uvalue = 0;
1268 dwarf2_find_name(ctx, di, &name, "subprogram");
1269 ret_type = dwarf2_lookup_type(ctx, di);
1271 /* FIXME: assuming C source code */
1272 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1273 if (!is_decl.uvalue)
1275 subpgm.func = symt_new_function(ctx->module, compiland, name.string,
1276 ctx->module->module.BaseOfImage + low_pc.uvalue,
1277 high_pc.uvalue - low_pc.uvalue,
1278 &sig_type->symt);
1279 di->symt = &subpgm.func->symt;
1281 else subpgm.func = NULL;
1283 subpgm.ctx = ctx;
1284 subpgm.compiland = compiland;
1285 if (dwarf2_find_attribute(di, DW_AT_frame_base, &frame))
1287 subpgm.frame_offset = dwarf2_compute_location(ctx, frame.block, &subpgm.frame_reg);
1288 TRACE("For %s got %ld/%d\n", name.string, subpgm.frame_offset, subpgm.frame_reg);
1290 else /* on stack !! */
1292 subpgm.frame_reg = 0;
1293 subpgm.frame_offset = 0;
1296 if (di->abbrev->have_child) /** any interest to not have child ? */
1298 dwarf2_debug_info_t** pchild = NULL;
1299 dwarf2_debug_info_t* child;
1301 while ((pchild = vector_iter_up(&di->children, pchild)))
1303 child = *pchild;
1305 switch (child->abbrev->tag)
1307 case DW_TAG_variable:
1308 case DW_TAG_formal_parameter:
1309 dwarf2_parse_variable(&subpgm, NULL, child);
1310 break;
1311 case DW_TAG_lexical_block:
1312 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1313 break;
1314 case DW_TAG_inlined_subroutine:
1315 dwarf2_parse_inlined_subroutine(&subpgm, child);
1316 break;
1317 case DW_TAG_subprogram:
1318 /* FIXME: likely a declaration (to be checked)
1319 * skip it for now
1321 break;
1322 case DW_TAG_label:
1323 dwarf2_parse_subprogram_label(&subpgm, child);
1324 break;
1325 case DW_TAG_class_type:
1326 case DW_TAG_structure_type:
1327 case DW_TAG_union_type:
1328 case DW_TAG_enumeration_type:
1329 case DW_TAG_typedef:
1330 /* the type referred to will be loaded when we need it, so skip it */
1331 break;
1332 case DW_TAG_unspecified_parameters:
1333 /* FIXME: no support in dbghelp's internals so far */
1334 break;
1335 default:
1336 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1337 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1342 symt_normalize_function(subpgm.ctx->module, subpgm.func);
1344 return di->symt;
1347 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
1348 dwarf2_debug_info_t* di,
1349 struct symt_compiland* compiland)
1351 switch (di->abbrev->tag)
1353 case DW_TAG_typedef:
1354 dwarf2_parse_typedef(ctx, di);
1355 break;
1356 case DW_TAG_base_type:
1357 dwarf2_parse_base_type(ctx, di);
1358 break;
1359 case DW_TAG_pointer_type:
1360 dwarf2_parse_pointer_type(ctx, di);
1361 break;
1362 case DW_TAG_class_type:
1363 dwarf2_parse_udt_type(ctx, di, UdtClass);
1364 break;
1365 case DW_TAG_structure_type:
1366 dwarf2_parse_udt_type(ctx, di, UdtStruct);
1367 break;
1368 case DW_TAG_union_type:
1369 dwarf2_parse_udt_type(ctx, di, UdtUnion);
1370 break;
1371 case DW_TAG_array_type:
1372 dwarf2_parse_array_type(ctx, di);
1373 break;
1374 case DW_TAG_const_type:
1375 dwarf2_parse_const_type(ctx, di);
1376 break;
1377 case DW_TAG_reference_type:
1378 dwarf2_parse_reference_type(ctx, di);
1379 break;
1380 case DW_TAG_enumeration_type:
1381 dwarf2_parse_enumeration_type(ctx, di);
1382 break;
1383 case DW_TAG_subprogram:
1384 dwarf2_parse_subprogram(ctx, di, compiland);
1385 break;
1386 default:
1387 WARN("Unhandled Tag type 0x%lx at %s, for %lu\n",
1388 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1392 static void dwarf2_set_line_number(struct module* module, unsigned long address,
1393 struct vector* v, unsigned file, unsigned line)
1395 struct symt_function* func;
1396 int idx;
1397 unsigned* psrc;
1399 if (!file || !(psrc = vector_at(v, file - 1))) return;
1401 TRACE("%s %lx %s %u\n", module->module.ModuleName, address, source_get(module, *psrc), line);
1402 if ((idx = symt_find_nearest(module, address)) == -1 ||
1403 module->addr_sorttab[idx]->symt.tag != SymTagFunction) return;
1404 func = (struct symt_function*)module->addr_sorttab[idx];
1405 symt_add_func_line(module, func, *psrc, line, address - func->address);
1408 static void dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
1409 dwarf2_parse_context_t* ctx,
1410 unsigned long offset)
1412 dwarf2_traverse_context_t traverse;
1413 unsigned long length;
1414 unsigned version, header_len, insn_size, default_stmt;
1415 unsigned line_range, opcode_base;
1416 int line_base;
1417 const unsigned char* opcode_len;
1418 struct vector dirs;
1419 struct vector files;
1420 const char** p;
1422 traverse.sections = sections;
1423 traverse.section = section_line;
1424 traverse.data = sections[section_line].address + offset;
1425 traverse.start_data = traverse.data;
1426 traverse.end_data = traverse.data + 4;
1427 traverse.offset = offset;
1428 traverse.word_size = ctx->word_size;
1430 length = dwarf2_parse_u4(&traverse);
1431 traverse.end_data = traverse.start_data + length;
1433 version = dwarf2_parse_u2(&traverse);
1434 header_len = dwarf2_parse_u4(&traverse);
1435 insn_size = dwarf2_parse_byte(&traverse);
1436 default_stmt = dwarf2_parse_byte(&traverse);
1437 line_base = (signed char)dwarf2_parse_byte(&traverse);
1438 line_range = dwarf2_parse_byte(&traverse);
1439 opcode_base = dwarf2_parse_byte(&traverse);
1441 opcode_len = traverse.data;
1442 traverse.data += opcode_base - 1;
1444 vector_init(&dirs, sizeof(const char*), 4);
1445 p = vector_add(&dirs, &ctx->pool);
1446 *p = ".";
1447 while (*traverse.data)
1449 TRACE("Got include %s\n", (const char*)traverse.data);
1450 p = vector_add(&dirs, &ctx->pool);
1451 *p = (const char *)traverse.data;
1452 traverse.data += strlen((const char *)traverse.data) + 1;
1454 traverse.data++;
1456 vector_init(&files, sizeof(unsigned), 16);
1457 while (*traverse.data)
1459 unsigned int dir_index, mod_time, length;
1460 const char* name;
1461 const char* dir;
1462 unsigned* psrc;
1464 name = (const char*)traverse.data;
1465 traverse.data += strlen(name) + 1;
1466 dir_index = dwarf2_leb128_as_unsigned(&traverse);
1467 mod_time = dwarf2_leb128_as_unsigned(&traverse);
1468 length = dwarf2_leb128_as_unsigned(&traverse);
1469 dir = *(const char**)vector_at(&dirs, dir_index);
1470 TRACE("Got file %s/%s (%u,%u)\n", dir, name, mod_time, length);
1471 psrc = vector_add(&files, &ctx->pool);
1472 *psrc = source_new(ctx->module, dir, name);
1474 traverse.data++;
1476 while (traverse.data < traverse.end_data)
1478 unsigned long address = 0;
1479 unsigned file = 1;
1480 unsigned line = 1;
1481 unsigned is_stmt = default_stmt;
1482 BOOL basic_block = FALSE, end_sequence = FALSE;
1483 unsigned opcode, extopcode, i;
1485 while (!end_sequence)
1487 opcode = dwarf2_parse_byte(&traverse);
1488 TRACE("Got opcode %x\n", opcode);
1490 if (opcode >= opcode_base)
1492 unsigned delta = opcode - opcode_base;
1494 address += (delta / line_range) * insn_size;
1495 line += line_base + (delta % line_range);
1496 basic_block = TRUE;
1497 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1499 else
1501 switch (opcode)
1503 case DW_LNS_copy:
1504 basic_block = FALSE;
1505 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1506 break;
1507 case DW_LNS_advance_pc:
1508 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
1509 break;
1510 case DW_LNS_advance_line:
1511 line += dwarf2_leb128_as_signed(&traverse);
1512 break;
1513 case DW_LNS_set_file:
1514 file = dwarf2_leb128_as_unsigned(&traverse);
1515 break;
1516 case DW_LNS_set_column:
1517 dwarf2_leb128_as_unsigned(&traverse);
1518 break;
1519 case DW_LNS_negate_stmt:
1520 is_stmt = !is_stmt;
1521 break;
1522 case DW_LNS_set_basic_block:
1523 basic_block = 1;
1524 break;
1525 case DW_LNS_const_add_pc:
1526 address += ((255 - opcode_base) / line_range) * insn_size;
1527 break;
1528 case DW_LNS_fixed_advance_pc:
1529 address += dwarf2_parse_u2(&traverse);
1530 break;
1531 case DW_LNS_extended_op:
1532 dwarf2_leb128_as_unsigned(&traverse);
1533 extopcode = dwarf2_parse_byte(&traverse);
1534 switch (extopcode)
1536 case DW_LNE_end_sequence:
1537 dwarf2_set_line_number(ctx->module, address, &files, file, line);
1538 end_sequence = TRUE;
1539 break;
1540 case DW_LNE_set_address:
1541 address = ctx->module->module.BaseOfImage + dwarf2_parse_addr(&traverse);
1542 break;
1543 case DW_LNE_define_file:
1544 FIXME("not handled %s\n", traverse.data);
1545 traverse.data += strlen((const char *)traverse.data) + 1;
1546 dwarf2_leb128_as_unsigned(&traverse);
1547 dwarf2_leb128_as_unsigned(&traverse);
1548 dwarf2_leb128_as_unsigned(&traverse);
1549 break;
1550 default:
1551 FIXME("Unsupported extended opcode %x\n", extopcode);
1552 break;
1554 break;
1555 default:
1556 WARN("Unsupported opcode %x\n", opcode);
1557 for (i = 0; i < opcode_len[opcode]; i++)
1558 dwarf2_leb128_as_unsigned(&traverse);
1559 break;
1566 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
1567 const dwarf2_comp_unit_t* comp_unit,
1568 struct module* module,
1569 const struct elf_thunk_area* thunks,
1570 const unsigned char* comp_unit_cursor)
1572 dwarf2_parse_context_t ctx;
1573 dwarf2_traverse_context_t traverse;
1574 dwarf2_traverse_context_t abbrev_ctx;
1575 dwarf2_debug_info_t* di;
1576 BOOL ret = FALSE;
1578 TRACE("Compilation Unit Header found at 0x%x:\n",
1579 comp_unit_cursor - sections[section_debug].address);
1580 TRACE("- length: %lu\n", comp_unit->length);
1581 TRACE("- version: %u\n", comp_unit->version);
1582 TRACE("- abbrev_offset: %lu\n", comp_unit->abbrev_offset);
1583 TRACE("- word_size: %u\n", comp_unit->word_size);
1585 if (comp_unit->version != 2)
1587 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
1588 comp_unit->version);
1589 return FALSE;
1592 pool_init(&ctx.pool, 65536);
1593 ctx.module = module;
1594 ctx.word_size = comp_unit->word_size;
1595 ctx.thunks = thunks;
1597 traverse.sections = sections;
1598 traverse.section = section_debug;
1599 traverse.start_data = comp_unit_cursor + sizeof(dwarf2_comp_unit_stream_t);
1600 traverse.data = traverse.start_data;
1601 traverse.offset = comp_unit_cursor - sections[section_debug].address;
1602 traverse.word_size = comp_unit->word_size;
1603 traverse.end_data = comp_unit_cursor + comp_unit->length + sizeof(unsigned);
1605 abbrev_ctx.sections = sections;
1606 abbrev_ctx.section = section_abbrev;
1607 abbrev_ctx.start_data = sections[section_abbrev].address + comp_unit->abbrev_offset;
1608 abbrev_ctx.data = abbrev_ctx.start_data;
1609 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
1610 abbrev_ctx.offset = comp_unit->abbrev_offset;
1611 abbrev_ctx.word_size = comp_unit->word_size;
1612 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
1614 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
1615 dwarf2_read_one_debug_info(&ctx, &traverse, &di);
1617 if (di->abbrev->tag == DW_TAG_compile_unit)
1619 union attribute name;
1620 dwarf2_debug_info_t** pdi = NULL;
1621 union attribute stmt_list;
1623 TRACE("beginning at 0x%lx, for %lu\n", di->offset, di->abbrev->entry_code);
1625 dwarf2_find_name(&ctx, di, &name, "compiland");
1626 di->symt = &symt_new_compiland(module, source_new(module, NULL, name.string))->symt;
1628 if (di->abbrev->have_child)
1630 while ((pdi = vector_iter_up(&di->children, pdi)))
1632 dwarf2_load_one_entry(&ctx, *pdi, (struct symt_compiland*)di->symt);
1635 if (dwarf2_find_attribute(di, DW_AT_stmt_list, &stmt_list))
1637 dwarf2_parse_line_numbers(sections, &ctx, stmt_list.uvalue);
1639 ret = TRUE;
1641 else FIXME("Should have a compilation unit here\n");
1642 pool_destroy(&ctx.pool);
1643 return ret;
1646 BOOL dwarf2_parse(struct module* module, unsigned long load_offset,
1647 const struct elf_thunk_area* thunks,
1648 const unsigned char* debug, unsigned int debug_size,
1649 const unsigned char* abbrev, unsigned int abbrev_size,
1650 const unsigned char* str, unsigned int str_size,
1651 const unsigned char* line, unsigned int line_size)
1653 dwarf2_section_t section[section_max];
1654 const unsigned char*comp_unit_cursor = debug;
1655 const unsigned char*end_debug = debug + debug_size;
1657 section[section_debug].address = debug;
1658 section[section_debug].size = debug_size;
1659 section[section_abbrev].address = abbrev;
1660 section[section_abbrev].size = abbrev_size;
1661 section[section_string].address = str;
1662 section[section_string].size = str_size;
1663 section[section_line].address = line;
1664 section[section_line].size = line_size;
1666 while (comp_unit_cursor < end_debug)
1668 const dwarf2_comp_unit_stream_t* comp_unit_stream;
1669 dwarf2_comp_unit_t comp_unit;
1671 comp_unit_stream = (const dwarf2_comp_unit_stream_t*) comp_unit_cursor;
1672 comp_unit.length = *(unsigned long*) comp_unit_stream->length;
1673 comp_unit.version = *(unsigned short*) comp_unit_stream->version;
1674 comp_unit.abbrev_offset = *(unsigned long*) comp_unit_stream->abbrev_offset;
1675 comp_unit.word_size = *(unsigned char*) comp_unit_stream->word_size;
1677 dwarf2_parse_compilation_unit(section, &comp_unit, module,
1678 thunks, comp_unit_cursor);
1679 comp_unit_cursor += comp_unit.length + sizeof(unsigned);
1681 module->module.SymType = SymDia;
1682 module->module.CVSig = 'D' | ('W' << 8) | ('A' << 16) | ('R' << 24);
1683 /* FIXME: we could have a finer grain here */
1684 module->module.LineNumbers = TRUE;
1685 module->module.GlobalSymbols = TRUE;
1686 module->module.TypeInfo = TRUE;
1687 module->module.SourceIndexed = TRUE;
1688 module->module.Publics = TRUE;
1689 return TRUE;