0d517929186956cc9004fa14643802272bf623c2
[reactos.git] / dll / win32 / dbghelp / dwarf.c
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
3 *
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2011, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 */
22
23 #ifndef DBGHELP_STATIC_LIB
24
25 #define NONAMELESSUNION
26
27 #include "config.h"
28
29 #include <sys/types.h>
30 #include <fcntl.h>
31 #ifdef HAVE_SYS_STAT_H
32 # include <sys/stat.h>
33 #endif
34 #ifdef HAVE_SYS_MMAN_H
35 #include <sys/mman.h>
36 #endif
37 #include <limits.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #ifdef HAVE_UNISTD_H
41 # include <unistd.h>
42 #endif
43 #include <stdio.h>
44 #include <assert.h>
45 #include <stdarg.h>
46
47 #ifdef HAVE_ZLIB
48 #include <zlib.h>
49 #endif
50
51 #include "windef.h"
52 #include "winternl.h"
53 #include "winbase.h"
54 #include "winuser.h"
55 #include "ole2.h"
56 #include "oleauto.h"
57
58 #include "dbghelp_private.h"
59 #include "image_private.h"
60
61 #include "wine/debug.h"
62
63 #else
64 #include "dbghelp_private.h"
65 #include "image_private.h"
66 #endif /* !DBGHELP_STATIC_LIB */
67
68 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
69
70 /* FIXME:
71 * - Functions:
72 * o unspecified parameters
73 * o inlined functions
74 * o Debug{Start|End}Point
75 * o CFA
76 * - Udt
77 * o proper types loading (nesting)
78 */
79
80 #if 0
81 static void dump(const void* ptr, unsigned len)
82 {
83 int i, j;
84 BYTE msg[128];
85 static const char hexof[] = "0123456789abcdef";
86 const BYTE* x = ptr;
87
88 for (i = 0; i < len; i += 16)
89 {
90 sprintf(msg, "%08x: ", i);
91 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
92 for (j = 0; j < min(16, len - i); j++)
93 {
94 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
95 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
96 msg[10 + 3 * j + 2] = ' ';
97 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
98 x[i + j] : '.';
99 }
100 msg[10 + 3 * 16] = ' ';
101 msg[10 + 3 * 16 + 1 + 16] = '\0';
102 TRACE("%s\n", msg);
103 }
104 }
105 #endif
106
107 /**
108 *
109 * Main Specs:
110 * http://www.eagercon.com/dwarf/dwarf3std.htm
111 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
112 *
113 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
114 *
115 * example of projects who do dwarf2 parsing:
116 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
117 * http://elis.ugent.be/diota/log/ltrace_elf.c
118 */
119 #include "dwarf.h"
120
121 /**
122 * Parsers
123 */
124
125 typedef struct dwarf2_abbrev_entry_attr_s
126 {
127 unsigned long attribute;
128 unsigned long form;
129 struct dwarf2_abbrev_entry_attr_s* next;
130 } dwarf2_abbrev_entry_attr_t;
131
132 typedef struct dwarf2_abbrev_entry_s
133 {
134 unsigned long entry_code;
135 unsigned long tag;
136 unsigned char have_child;
137 unsigned num_attr;
138 dwarf2_abbrev_entry_attr_t* attrs;
139 } dwarf2_abbrev_entry_t;
140
141 struct dwarf2_block
142 {
143 unsigned size;
144 const unsigned char* ptr;
145 };
146
147 struct attribute
148 {
149 unsigned long form;
150 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
151 union
152 {
153 unsigned long uvalue;
154 ULONGLONG lluvalue;
155 long svalue;
156 const char* string;
157 struct dwarf2_block block;
158 } u;
159 };
160
161 typedef struct dwarf2_debug_info_s
162 {
163 const dwarf2_abbrev_entry_t*abbrev;
164 struct symt* symt;
165 const unsigned char** data;
166 struct vector children;
167 struct dwarf2_debug_info_s* parent;
168 } dwarf2_debug_info_t;
169
170 typedef struct dwarf2_section_s
171 {
172 BOOL compressed;
173 const unsigned char* address;
174 unsigned size;
175 DWORD_PTR rva;
176 } dwarf2_section_t;
177
178 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
179
180 typedef struct dwarf2_traverse_context_s
181 {
182 const unsigned char* data;
183 const unsigned char* end_data;
184 unsigned char word_size;
185 } dwarf2_traverse_context_t;
186
187 /* symt_cache indexes */
188 #define sc_void 0
189 #define sc_int1 1
190 #define sc_int2 2
191 #define sc_int4 3
192 #define sc_num 4
193
194 typedef struct dwarf2_parse_context_s
195 {
196 const dwarf2_section_t* sections;
197 unsigned section;
198 struct pool pool;
199 struct module* module;
200 struct symt_compiland* compiland;
201 const struct elf_thunk_area*thunks;
202 struct sparse_array abbrev_table;
203 struct sparse_array debug_info_table;
204 unsigned long load_offset;
205 unsigned long ref_offset;
206 struct symt* symt_cache[sc_num]; /* void, int1, int2, int4 */
207 char* cpp_name;
208 } dwarf2_parse_context_t;
209
210 /* stored in the dbghelp's module internal structure for later reuse */
211 struct dwarf2_module_info_s
212 {
213 dwarf2_section_t debug_loc;
214 dwarf2_section_t debug_frame;
215 dwarf2_section_t eh_frame;
216 unsigned char word_size;
217 };
218
219 #define loc_dwarf2_location_list (loc_user + 0)
220 #define loc_dwarf2_block (loc_user + 1)
221
222 /* forward declarations */
223 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* entry);
224
225 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
226 {
227 return *ptr;
228 }
229
230 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
231 {
232 unsigned char uvalue = dwarf2_get_byte(ctx->data);
233 ctx->data += 1;
234 return uvalue;
235 }
236
237 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
238 {
239 return *(const UINT16*)ptr;
240 }
241
242 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
243 {
244 unsigned short uvalue = dwarf2_get_u2(ctx->data);
245 ctx->data += 2;
246 return uvalue;
247 }
248
249 static unsigned long dwarf2_get_u4(const unsigned char* ptr)
250 {
251 return *(const UINT32*)ptr;
252 }
253
254 static unsigned long dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
255 {
256 unsigned long uvalue = dwarf2_get_u4(ctx->data);
257 ctx->data += 4;
258 return uvalue;
259 }
260
261 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
262 {
263 return *(const UINT64*)ptr;
264 }
265
266 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
267 {
268 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
269 ctx->data += 8;
270 return uvalue;
271 }
272
273 static unsigned long dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
274 {
275 unsigned long ret = 0;
276 unsigned char byte;
277 unsigned shift = 0;
278
279 do
280 {
281 byte = dwarf2_get_byte(ptr++);
282 ret |= (byte & 0x7f) << shift;
283 shift += 7;
284 } while (byte & 0x80);
285
286 if (end) *end = ptr;
287 return ret;
288 }
289
290 static unsigned long dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
291 {
292 unsigned long ret;
293
294 assert(ctx);
295
296 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
297
298 return ret;
299 }
300
301 static long dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
302 {
303 long ret = 0;
304 unsigned char byte;
305 unsigned shift = 0;
306 const unsigned size = sizeof(int) * 8;
307
308 do
309 {
310 byte = dwarf2_get_byte(ptr++);
311 ret |= (byte & 0x7f) << shift;
312 shift += 7;
313 } while (byte & 0x80);
314 if (end) *end = ptr;
315
316 /* as spec: sign bit of byte is 2nd high order bit (80x40)
317 * -> 0x80 is used as flag.
318 */
319 if ((shift < size) && (byte & 0x40))
320 {
321 ret |= - (1 << shift);
322 }
323 return ret;
324 }
325
326 static long dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
327 {
328 long ret = 0;
329
330 assert(ctx);
331
332 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
333 return ret;
334 }
335
336 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
337 {
338 unsigned ret;
339 for (ret = 0; ctx->data[ret] & 0x80; ret++);
340 return ret + 1;
341 }
342
343 /******************************************************************
344 * dwarf2_get_addr
345 *
346 * Returns an address.
347 * We assume that in all cases word size from Dwarf matches the size of
348 * addresses in platform where the exec is compiled.
349 */
350 static unsigned long dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
351 {
352 unsigned long ret;
353
354 switch (word_size)
355 {
356 case 4:
357 ret = dwarf2_get_u4(ptr);
358 break;
359 case 8:
360 ret = dwarf2_get_u8(ptr);
361 break;
362 default:
363 FIXME("Unsupported Word Size %u\n", word_size);
364 ret = 0;
365 }
366 return ret;
367 }
368
369 static unsigned long dwarf2_parse_addr(dwarf2_traverse_context_t* ctx)
370 {
371 unsigned long ret = dwarf2_get_addr(ctx->data, ctx->word_size);
372 ctx->data += ctx->word_size;
373 return ret;
374 }
375
376 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
377 {
378 return wine_dbg_sprintf("ctx(%p)", ctx->data);
379 }
380
381 static const char* dwarf2_debug_ctx(const dwarf2_parse_context_t* ctx)
382 {
383 return wine_dbg_sprintf("ctx(%p,%s)",
384 ctx, debugstr_w(ctx->module->module.ModuleName));
385 }
386
387 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
388 {
389 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p)",
390 di->abbrev, di->symt);
391 }
392
393 static dwarf2_abbrev_entry_t*
394 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
395 unsigned long entry_code)
396 {
397 assert( NULL != abbrev_table );
398 return sparse_array_find(abbrev_table, entry_code);
399 }
400
401 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
402 struct sparse_array* abbrev_table,
403 struct pool* pool)
404 {
405 unsigned long entry_code;
406 dwarf2_abbrev_entry_t* abbrev_entry;
407 dwarf2_abbrev_entry_attr_t* new = NULL;
408 dwarf2_abbrev_entry_attr_t* last = NULL;
409 unsigned long attribute;
410 unsigned long form;
411
412 assert( NULL != abbrev_ctx );
413
414 TRACE("%s, end at %p\n",
415 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
416
417 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
418 while (abbrev_ctx->data < abbrev_ctx->end_data)
419 {
420 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
421 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
422 TRACE("found entry_code %lu\n", entry_code);
423 if (!entry_code)
424 {
425 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
426 break;
427 }
428 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
429 assert( NULL != abbrev_entry );
430
431 abbrev_entry->entry_code = entry_code;
432 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
433 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
434 abbrev_entry->attrs = NULL;
435 abbrev_entry->num_attr = 0;
436
437 TRACE("table:(%p,#%u) entry_code(%lu) tag(0x%lx) have_child(%u) -> %p\n",
438 abbrev_table, sparse_array_length(abbrev_table),
439 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
440
441 last = NULL;
442 while (1)
443 {
444 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
445 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
446 if (!attribute) break;
447
448 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
449 assert(new);
450
451 new->attribute = attribute;
452 new->form = form;
453 new->next = NULL;
454 if (abbrev_entry->attrs) last->next = new;
455 else abbrev_entry->attrs = new;
456 last = new;
457 abbrev_entry->num_attr++;
458 }
459 }
460 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
461 }
462
463 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
464 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
465 {
466 unsigned step;
467
468 TRACE("(attr:0x%lx,form:0x%lx)\n", abbrev_attr->attribute, abbrev_attr->form);
469
470 switch (abbrev_attr->form)
471 {
472 case DW_FORM_flag_present: step = 0; break;
473 case DW_FORM_ref_addr:
474 case DW_FORM_addr: step = ctx->word_size; break;
475 case DW_FORM_flag:
476 case DW_FORM_data1:
477 case DW_FORM_ref1: step = 1; break;
478 case DW_FORM_data2:
479 case DW_FORM_ref2: step = 2; break;
480 case DW_FORM_data4:
481 case DW_FORM_ref4:
482 case DW_FORM_strp: step = 4; break;
483 case DW_FORM_data8:
484 case DW_FORM_ref8: step = 8; break;
485 case DW_FORM_sdata:
486 case DW_FORM_ref_udata:
487 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
488 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
489 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
490 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
491 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
492 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
493 default:
494 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
495 return;
496 }
497 ctx->data += step;
498 }
499
500 static void dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
501 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
502 const unsigned char* data,
503 struct attribute* attr)
504 {
505 attr->form = abbrev_attr->form;
506 switch (attr->form)
507 {
508 case DW_FORM_ref_addr:
509 case DW_FORM_addr:
510 attr->u.uvalue = dwarf2_get_addr(data,
511 ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
512 TRACE("addr<0x%lx>\n", attr->u.uvalue);
513 break;
514
515 case DW_FORM_flag:
516 attr->u.uvalue = dwarf2_get_byte(data);
517 TRACE("flag<0x%lx>\n", attr->u.uvalue);
518 break;
519
520 case DW_FORM_flag_present:
521 attr->u.uvalue = 1;
522 TRACE("flag_present\n");
523 break;
524
525 case DW_FORM_data1:
526 attr->u.uvalue = dwarf2_get_byte(data);
527 TRACE("data1<%lu>\n", attr->u.uvalue);
528 break;
529
530 case DW_FORM_data2:
531 attr->u.uvalue = dwarf2_get_u2(data);
532 TRACE("data2<%lu>\n", attr->u.uvalue);
533 break;
534
535 case DW_FORM_data4:
536 attr->u.uvalue = dwarf2_get_u4(data);
537 TRACE("data4<%lu>\n", attr->u.uvalue);
538 break;
539
540 case DW_FORM_data8:
541 attr->u.lluvalue = dwarf2_get_u8(data);
542 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
543 break;
544
545 case DW_FORM_ref1:
546 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
547 TRACE("ref1<0x%lx>\n", attr->u.uvalue);
548 break;
549
550 case DW_FORM_ref2:
551 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
552 TRACE("ref2<0x%lx>\n", attr->u.uvalue);
553 break;
554
555 case DW_FORM_ref4:
556 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
557 TRACE("ref4<0x%lx>\n", attr->u.uvalue);
558 break;
559
560 case DW_FORM_ref8:
561 FIXME("Unhandled 64-bit support\n");
562 break;
563
564 case DW_FORM_sdata:
565 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
566 break;
567
568 case DW_FORM_ref_udata:
569 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
570 break;
571
572 case DW_FORM_udata:
573 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
574 break;
575
576 case DW_FORM_string:
577 attr->u.string = (const char *)data;
578 TRACE("string<%s>\n", attr->u.string);
579 break;
580
581 case DW_FORM_strp:
582 {
583 unsigned long offset = dwarf2_get_u4(data);
584 attr->u.string = (const char*)ctx->sections[section_string].address + offset;
585 }
586 TRACE("strp<%s>\n", attr->u.string);
587 break;
588
589 case DW_FORM_block:
590 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
591 break;
592
593 case DW_FORM_block1:
594 attr->u.block.size = dwarf2_get_byte(data);
595 attr->u.block.ptr = data + 1;
596 break;
597
598 case DW_FORM_block2:
599 attr->u.block.size = dwarf2_get_u2(data);
600 attr->u.block.ptr = data + 2;
601 break;
602
603 case DW_FORM_block4:
604 attr->u.block.size = dwarf2_get_u4(data);
605 attr->u.block.ptr = data + 4;
606 break;
607
608 default:
609 FIXME("Unhandled attribute form %lx\n", abbrev_attr->form);
610 break;
611 }
612 }
613
614 static BOOL dwarf2_find_attribute(const dwarf2_parse_context_t* ctx,
615 const dwarf2_debug_info_t* di,
616 unsigned at, struct attribute* attr)
617 {
618 unsigned i, refidx = 0;
619 dwarf2_abbrev_entry_attr_t* abbrev_attr;
620 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
621
622 attr->gotten_from = attr_direct;
623 while (di)
624 {
625 ref_abbrev_attr = NULL;
626 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
627 {
628 if (abbrev_attr->attribute == at)
629 {
630 dwarf2_fill_attr(ctx, abbrev_attr, di->data[i], attr);
631 return TRUE;
632 }
633 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
634 abbrev_attr->attribute == DW_AT_specification) &&
635 at != DW_AT_sibling)
636 {
637 if (ref_abbrev_attr)
638 FIXME("two references %lx and %lx\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
639 ref_abbrev_attr = abbrev_attr;
640 refidx = i;
641 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
642 attr_abstract_origin : attr_specification;
643 }
644 }
645 /* do we have either an abstract origin or a specification debug entry to look into ? */
646 if (!ref_abbrev_attr) break;
647 dwarf2_fill_attr(ctx, ref_abbrev_attr, di->data[refidx], attr);
648 if (!(di = sparse_array_find(&ctx->debug_info_table, attr->u.uvalue)))
649 FIXME("Should have found the debug info entry\n");
650 }
651 return FALSE;
652 }
653
654 static void dwarf2_load_one_entry(dwarf2_parse_context_t*, dwarf2_debug_info_t*);
655
656 #define Wine_DW_no_register 0x7FFFFFFF
657
658 static unsigned dwarf2_map_register(int regno)
659 {
660 if (regno == Wine_DW_no_register)
661 {
662 FIXME("What the heck map reg 0x%x\n",regno);
663 return 0;
664 }
665 return dbghelp_current_cpu->map_dwarf_register(regno, FALSE);
666 }
667
668 static enum location_error
669 compute_location(dwarf2_traverse_context_t* ctx, struct location* loc,
670 HANDLE hproc, const struct location* frame)
671 {
672 DWORD_PTR tmp, stack[64];
673 unsigned stk;
674 unsigned char op;
675 BOOL piece_found = FALSE;
676
677 stack[stk = 0] = 0;
678
679 loc->kind = loc_absolute;
680 loc->reg = Wine_DW_no_register;
681
682 while (ctx->data < ctx->end_data)
683 {
684 op = dwarf2_parse_byte(ctx);
685
686 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
687 stack[++stk] = op - DW_OP_lit0;
688 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
689 {
690 /* dbghelp APIs don't know how to cope with this anyway
691 * (for example 'long long' stored in two registers)
692 * FIXME: We should tell winedbg how to deal with it (sigh)
693 */
694 if (!piece_found)
695 {
696 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0);
697 if (loc->reg != Wine_DW_no_register)
698 FIXME("Only supporting one reg (%s/%d -> %s/%d)\n",
699 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
700 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
701 loc->reg = cvreg;
702 }
703 loc->kind = loc_register;
704 }
705 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
706 {
707 /* dbghelp APIs don't know how to cope with this anyway
708 * (for example 'long long' stored in two registers)
709 * FIXME: We should tell winedbg how to deal with it (sigh)
710 */
711 if (!piece_found)
712 {
713 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0);
714 if (loc->reg != Wine_DW_no_register)
715 FIXME("Only supporting one breg (%s/%d -> %s/%d)\n",
716 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg,
717 dbghelp_current_cpu->fetch_regname(cvreg), cvreg);
718 loc->reg = cvreg;
719 }
720 stack[++stk] = dwarf2_leb128_as_signed(ctx);
721 loc->kind = loc_regrel;
722 }
723 else switch (op)
724 {
725 case DW_OP_nop: break;
726 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr(ctx); break;
727 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
728 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
729 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
730 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
731 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
732 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
733 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
734 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
735 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
736 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
737 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
738 case DW_OP_drop: stk--; break;
739 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
740 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
741 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
742 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
743 case DW_OP_abs: stack[stk] = labs(stack[stk]); break;
744 case DW_OP_neg: stack[stk] = -stack[stk]; break;
745 case DW_OP_not: stack[stk] = ~stack[stk]; break;
746 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
747 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
748 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
749 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
750 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
751 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
752 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
753 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
754 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
755 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
756 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
757 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
758 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
759 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
760 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
761 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
762 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
763 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
764 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
765 case DW_OP_bra: tmp = dwarf2_parse_u2(ctx); if (!stack[stk--]) ctx->data += tmp; break;
766 case DW_OP_regx:
767 tmp = dwarf2_leb128_as_unsigned(ctx);
768 if (!piece_found)
769 {
770 if (loc->reg != Wine_DW_no_register)
771 FIXME("Only supporting one reg\n");
772 loc->reg = dwarf2_map_register(tmp);
773 }
774 loc->kind = loc_register;
775 break;
776 case DW_OP_bregx:
777 tmp = dwarf2_leb128_as_unsigned(ctx);
778 if (loc->reg != Wine_DW_no_register)
779 FIXME("Only supporting one regx\n");
780 loc->reg = dwarf2_map_register(tmp);
781 stack[++stk] = dwarf2_leb128_as_signed(ctx);
782 loc->kind = loc_regrel;
783 break;
784 case DW_OP_fbreg:
785 if (loc->reg != Wine_DW_no_register)
786 FIXME("Only supporting one reg (%s/%d -> -2)\n",
787 dbghelp_current_cpu->fetch_regname(loc->reg), loc->reg);
788 if (frame && frame->kind == loc_register)
789 {
790 loc->kind = loc_regrel;
791 loc->reg = frame->reg;
792 stack[++stk] = dwarf2_leb128_as_signed(ctx);
793 }
794 else if (frame && frame->kind == loc_regrel)
795 {
796 loc->kind = loc_regrel;
797 loc->reg = frame->reg;
798 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
799 }
800 else
801 {
802 /* FIXME: this could be later optimized by not recomputing
803 * this very location expression
804 */
805 loc->kind = loc_dwarf2_block;
806 stack[++stk] = dwarf2_leb128_as_signed(ctx);
807 }
808 break;
809 case DW_OP_piece:
810 {
811 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
812 WARN("Not handling OP_piece (size=%d)\n", sz);
813 piece_found = TRUE;
814 }
815 break;
816 case DW_OP_deref:
817 if (!stk)
818 {
819 FIXME("Unexpected empty stack\n");
820 return loc_err_internal;
821 }
822 if (loc->reg != Wine_DW_no_register)
823 {
824 WARN("Too complex expression for deref\n");
825 return loc_err_too_complex;
826 }
827 if (hproc)
828 {
829 DWORD_PTR addr = stack[stk--];
830 DWORD_PTR deref;
831
832 if (!ReadProcessMemory(hproc, (void*)addr, &deref, sizeof(deref), NULL))
833 {
834 WARN("Couldn't read memory at %lx\n", addr);
835 return loc_err_cant_read;
836 }
837 stack[++stk] = deref;
838 }
839 else
840 {
841 loc->kind = loc_dwarf2_block;
842 }
843 break;
844 case DW_OP_deref_size:
845 if (!stk)
846 {
847 FIXME("Unexpected empty stack\n");
848 return loc_err_internal;
849 }
850 if (loc->reg != Wine_DW_no_register)
851 {
852 WARN("Too complex expression for deref\n");
853 return loc_err_too_complex;
854 }
855 if (hproc)
856 {
857 DWORD_PTR addr = stack[stk--];
858 BYTE derefsize = dwarf2_parse_byte(ctx);
859 DWORD64 deref;
860
861 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
862 {
863 WARN("Couldn't read memory at %lx\n", addr);
864 return loc_err_cant_read;
865 }
866
867 switch (derefsize)
868 {
869 case 1: stack[++stk] = *(unsigned char*)&deref; break;
870 case 2: stack[++stk] = *(unsigned short*)&deref; break;
871 case 4: stack[++stk] = *(DWORD*)&deref; break;
872 case 8: if (ctx->word_size >= derefsize) stack[++stk] = deref; break;
873 }
874 }
875 else
876 {
877 dwarf2_parse_byte(ctx);
878 loc->kind = loc_dwarf2_block;
879 }
880 break;
881 case DW_OP_stack_value:
882 /* Expected behaviour is that this is the last instruction of this
883 * expression and just the "top of stack" value should be put to loc->offset. */
884 break;
885 default:
886 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
887 FIXME("Unhandled attr op: %x\n", op);
888 /* FIXME else unhandled extension */
889 return loc_err_internal;
890 }
891 }
892 loc->offset = stack[stk];
893 return 0;
894 }
895
896 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
897 const dwarf2_debug_info_t* di,
898 unsigned long dw,
899 struct location* loc,
900 const struct location* frame)
901 {
902 struct attribute xloc;
903
904 if (!dwarf2_find_attribute(ctx, di, dw, &xloc)) return FALSE;
905
906 switch (xloc.form)
907 {
908 case DW_FORM_data1: case DW_FORM_data2:
909 case DW_FORM_udata: case DW_FORM_sdata:
910 loc->kind = loc_absolute;
911 loc->reg = 0;
912 loc->offset = xloc.u.uvalue;
913 return TRUE;
914 case DW_FORM_data4: case DW_FORM_data8:
915 loc->kind = loc_dwarf2_location_list;
916 loc->reg = Wine_DW_no_register;
917 loc->offset = xloc.u.uvalue;
918 return TRUE;
919 case DW_FORM_block:
920 case DW_FORM_block1:
921 case DW_FORM_block2:
922 case DW_FORM_block4:
923 break;
924 default: FIXME("Unsupported yet form %lx\n", xloc.form);
925 return FALSE;
926 }
927
928 /* assume we have a block form */
929
930 if (xloc.u.block.size)
931 {
932 dwarf2_traverse_context_t lctx;
933 enum location_error err;
934
935 lctx.data = xloc.u.block.ptr;
936 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
937 lctx.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
938
939 err = compute_location(&lctx, loc, NULL, frame);
940 if (err < 0)
941 {
942 loc->kind = loc_error;
943 loc->reg = err;
944 }
945 else if (loc->kind == loc_dwarf2_block)
946 {
947 unsigned* ptr = pool_alloc(&ctx->module->pool,
948 sizeof(unsigned) + xloc.u.block.size);
949 *ptr = xloc.u.block.size;
950 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
951 loc->offset = (unsigned long)ptr;
952 }
953 }
954 return TRUE;
955 }
956
957 static struct symt* dwarf2_lookup_type(dwarf2_parse_context_t* ctx,
958 const dwarf2_debug_info_t* di)
959 {
960 struct attribute attr;
961 dwarf2_debug_info_t* type;
962
963 if (!dwarf2_find_attribute(ctx, di, DW_AT_type, &attr))
964 return NULL;
965 if (!(type = sparse_array_find(&ctx->debug_info_table, attr.u.uvalue)))
966 {
967 FIXME("Unable to find back reference to type %lx\n", attr.u.uvalue);
968 return NULL;
969 }
970 if (!type->symt)
971 {
972 /* load the debug info entity */
973 dwarf2_load_one_entry(ctx, type);
974 if (!type->symt)
975 FIXME("Unable to load forward reference for tag %lx\n", type->abbrev->tag);
976 }
977 return type->symt;
978 }
979
980 static const char* dwarf2_get_cpp_name(dwarf2_parse_context_t* ctx, dwarf2_debug_info_t* di, const char* name)
981 {
982 char* last;
983 struct attribute diname;
984 struct attribute spec;
985
986 if (di->abbrev->tag == DW_TAG_compile_unit) return name;
987 if (!ctx->cpp_name)
988 ctx->cpp_name = pool_alloc(&ctx->pool, MAX_SYM_NAME);
989 last = ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
990 strcpy(last, name);
991
992 /* if the di is a definition, but has also a (previous) declaration, then scope must
993 * be gotten from declaration not definition
994 */
995 if (dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
996 {
997 di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue);
998 if (!di)
999 {
1000 FIXME("Should have found the debug info entry\n");
1001 return NULL;
1002 }
1003 }
1004
1005 for (di = di->parent; di; di = di->parent)
1006 {
1007 switch (di->abbrev->tag)
1008 {
1009 case DW_TAG_namespace:
1010 case DW_TAG_structure_type:
1011 case DW_TAG_class_type:
1012 case DW_TAG_interface_type:
1013 case DW_TAG_union_type:
1014 if (dwarf2_find_attribute(ctx, di, DW_AT_name, &diname))
1015 {
1016 size_t len = strlen(diname.u.string);
1017 last -= 2 + len;
1018 if (last < ctx->cpp_name) return NULL;
1019 memcpy(last, diname.u.string, len);
1020 last[len] = last[len + 1] = ':';
1021 }
1022 break;
1023 default:
1024 break;
1025 }
1026 }
1027 return last;
1028 }
1029
1030 /******************************************************************
1031 * dwarf2_read_range
1032 *
1033 * read a range for a given debug_info (either using AT_range attribute, in which
1034 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1035 * in all cases, range is relative to beginning of compilation unit
1036 */
1037 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1038 unsigned long* plow, unsigned long* phigh)
1039 {
1040 struct attribute range;
1041
1042 if (dwarf2_find_attribute(ctx, di, DW_AT_ranges, &range))
1043 {
1044 dwarf2_traverse_context_t traverse;
1045 unsigned long low, high;
1046
1047 traverse.data = ctx->sections[section_ranges].address + range.u.uvalue;
1048 traverse.end_data = ctx->sections[section_ranges].address +
1049 ctx->sections[section_ranges].size;
1050 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
1051
1052 *plow = ULONG_MAX;
1053 *phigh = 0;
1054 while (traverse.data + 2 * traverse.word_size < traverse.end_data)
1055 {
1056 low = dwarf2_parse_addr(&traverse);
1057 high = dwarf2_parse_addr(&traverse);
1058 if (low == 0 && high == 0) break;
1059 if (low == ULONG_MAX) FIXME("unsupported yet (base address selection)\n");
1060 if (low < *plow) *plow = low;
1061 if (high > *phigh) *phigh = high;
1062 }
1063 if (*plow == ULONG_MAX || *phigh == 0) {FIXME("no entry found\n"); return FALSE;}
1064 if (*plow == *phigh) {FIXME("entry found, but low=high\n"); return FALSE;}
1065
1066 return TRUE;
1067 }
1068 else
1069 {
1070 struct attribute low_pc;
1071 struct attribute high_pc;
1072
1073 if (!dwarf2_find_attribute(ctx, di, DW_AT_low_pc, &low_pc) ||
1074 !dwarf2_find_attribute(ctx, di, DW_AT_high_pc, &high_pc))
1075 return FALSE;
1076 *plow = low_pc.u.uvalue;
1077 *phigh = high_pc.u.uvalue;
1078 return TRUE;
1079 }
1080 }
1081
1082 /******************************************************************
1083 * dwarf2_read_one_debug_info
1084 *
1085 * Loads into memory one debug info entry, and recursively its children (if any)
1086 */
1087 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1088 dwarf2_traverse_context_t* traverse,
1089 dwarf2_debug_info_t* parent_di,
1090 dwarf2_debug_info_t** pdi)
1091 {
1092 const dwarf2_abbrev_entry_t*abbrev;
1093 unsigned long entry_code;
1094 unsigned long offset;
1095 dwarf2_debug_info_t* di;
1096 dwarf2_debug_info_t* child;
1097 dwarf2_debug_info_t** where;
1098 dwarf2_abbrev_entry_attr_t* attr;
1099 unsigned i;
1100 struct attribute sibling;
1101
1102 offset = traverse->data - ctx->sections[ctx->section].address;
1103 entry_code = dwarf2_leb128_as_unsigned(traverse);
1104 TRACE("found entry_code %lu at 0x%lx\n", entry_code, offset);
1105 if (!entry_code)
1106 {
1107 *pdi = NULL;
1108 return TRUE;
1109 }
1110 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1111 if (!abbrev)
1112 {
1113 WARN("Cannot find abbrev entry for %lu at 0x%lx\n", entry_code, offset);
1114 return FALSE;
1115 }
1116 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1117 if (!di) return FALSE;
1118 di->abbrev = abbrev;
1119 di->symt = NULL;
1120 di->parent = parent_di;
1121
1122 if (abbrev->num_attr)
1123 {
1124 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1125 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1126 {
1127 di->data[i] = traverse->data;
1128 dwarf2_swallow_attribute(traverse, attr);
1129 }
1130 }
1131 else di->data = NULL;
1132 if (abbrev->have_child)
1133 {
1134 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1135 while (traverse->data < traverse->end_data)
1136 {
1137 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1138 if (!child) break;
1139 where = vector_add(&di->children, &ctx->pool);
1140 if (!where) return FALSE;
1141 *where = child;
1142 }
1143 }
1144 if (dwarf2_find_attribute(ctx, di, DW_AT_sibling, &sibling) &&
1145 traverse->data != ctx->sections[ctx->section].address + sibling.u.uvalue)
1146 {
1147 WARN("setting cursor for %s to next sibling <0x%lx>\n",
1148 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1149 traverse->data = ctx->sections[ctx->section].address + sibling.u.uvalue;
1150 }
1151 *pdi = di;
1152 return TRUE;
1153 }
1154
1155 static struct vector* dwarf2_get_di_children(dwarf2_parse_context_t* ctx,
1156 dwarf2_debug_info_t* di)
1157 {
1158 struct attribute spec;
1159
1160 while (di)
1161 {
1162 if (di->abbrev->have_child)
1163 return &di->children;
1164 if (!dwarf2_find_attribute(ctx, di, DW_AT_specification, &spec)) break;
1165 if (!(di = sparse_array_find(&ctx->debug_info_table, spec.u.uvalue)))
1166 FIXME("Should have found the debug info entry\n");
1167 }
1168 return NULL;
1169 }
1170
1171 static struct symt* dwarf2_parse_base_type(dwarf2_parse_context_t* ctx,
1172 dwarf2_debug_info_t* di)
1173 {
1174 struct attribute name;
1175 struct attribute size;
1176 struct attribute encoding;
1177 enum BasicType bt;
1178 int cache_idx = -1;
1179 if (di->symt) return di->symt;
1180
1181 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1182
1183 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1184 name.u.string = NULL;
1185 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1186 if (!dwarf2_find_attribute(ctx, di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1187
1188 switch (encoding.u.uvalue)
1189 {
1190 case DW_ATE_void: bt = btVoid; break;
1191 case DW_ATE_address: bt = btULong; break;
1192 case DW_ATE_boolean: bt = btBool; break;
1193 case DW_ATE_complex_float: bt = btComplex; break;
1194 case DW_ATE_float: bt = btFloat; break;
1195 case DW_ATE_signed: bt = btInt; break;
1196 case DW_ATE_unsigned: bt = btUInt; break;
1197 case DW_ATE_signed_char: bt = btChar; break;
1198 case DW_ATE_unsigned_char: bt = btChar; break;
1199 default: bt = btNoType; break;
1200 }
1201 di->symt = &symt_new_basic(ctx->module, bt, name.u.string, size.u.uvalue)->symt;
1202 switch (bt)
1203 {
1204 case btVoid:
1205 assert(size.u.uvalue == 0);
1206 cache_idx = sc_void;
1207 break;
1208 case btInt:
1209 switch (size.u.uvalue)
1210 {
1211 case 1: cache_idx = sc_int1; break;
1212 case 2: cache_idx = sc_int2; break;
1213 case 4: cache_idx = sc_int4; break;
1214 }
1215 break;
1216 default: break;
1217 }
1218 if (cache_idx != -1 && !ctx->symt_cache[cache_idx])
1219 ctx->symt_cache[cache_idx] = di->symt;
1220
1221 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1222 return di->symt;
1223 }
1224
1225 static struct symt* dwarf2_parse_typedef(dwarf2_parse_context_t* ctx,
1226 dwarf2_debug_info_t* di)
1227 {
1228 struct symt* ref_type;
1229 struct attribute name;
1230
1231 if (di->symt) return di->symt;
1232
1233 TRACE("%s, for %lu\n", dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
1234
1235 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1236 ref_type = dwarf2_lookup_type(ctx, di);
1237
1238 if (name.u.string)
1239 di->symt = &symt_new_typedef(ctx->module, ref_type, name.u.string)->symt;
1240 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1241 return di->symt;
1242 }
1243
1244 static struct symt* dwarf2_parse_pointer_type(dwarf2_parse_context_t* ctx,
1245 dwarf2_debug_info_t* di)
1246 {
1247 struct symt* ref_type;
1248 struct attribute size;
1249
1250 if (di->symt) return di->symt;
1251
1252 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1253
1254 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = sizeof(void *);
1255 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1256 {
1257 ref_type = ctx->symt_cache[sc_void];
1258 assert(ref_type);
1259 }
1260 di->symt = &symt_new_pointer(ctx->module, ref_type, size.u.uvalue)->symt;
1261 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1262 return di->symt;
1263 }
1264
1265 static struct symt* dwarf2_parse_array_type(dwarf2_parse_context_t* ctx,
1266 dwarf2_debug_info_t* di)
1267 {
1268 struct symt* ref_type;
1269 struct symt* idx_type = NULL;
1270 struct attribute min, max, cnt;
1271 dwarf2_debug_info_t* child;
1272 unsigned int i;
1273 const struct vector* children;
1274
1275 if (di->symt) return di->symt;
1276
1277 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1278
1279 ref_type = dwarf2_lookup_type(ctx, di);
1280
1281 if (!(children = dwarf2_get_di_children(ctx, di)))
1282 {
1283 /* fake an array with unknown size */
1284 /* FIXME: int4 even on 64bit machines??? */
1285 idx_type = ctx->symt_cache[sc_int4];
1286 min.u.uvalue = 0;
1287 max.u.uvalue = -1;
1288 }
1289 else for (i = 0; i < vector_length(children); i++)
1290 {
1291 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1292 switch (child->abbrev->tag)
1293 {
1294 case DW_TAG_subrange_type:
1295 idx_type = dwarf2_lookup_type(ctx, child);
1296 if (!dwarf2_find_attribute(ctx, child, DW_AT_lower_bound, &min))
1297 min.u.uvalue = 0;
1298 if (!dwarf2_find_attribute(ctx, child, DW_AT_upper_bound, &max))
1299 max.u.uvalue = 0;
1300 if (dwarf2_find_attribute(ctx, child, DW_AT_count, &cnt))
1301 max.u.uvalue = min.u.uvalue + cnt.u.uvalue;
1302 break;
1303 default:
1304 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1305 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1306 break;
1307 }
1308 }
1309 di->symt = &symt_new_array(ctx->module, min.u.uvalue, max.u.uvalue, ref_type, idx_type)->symt;
1310 return di->symt;
1311 }
1312
1313 static struct symt* dwarf2_parse_const_type(dwarf2_parse_context_t* ctx,
1314 dwarf2_debug_info_t* di)
1315 {
1316 struct symt* ref_type;
1317
1318 if (di->symt) return di->symt;
1319
1320 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1321
1322 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1323 {
1324 ref_type = ctx->symt_cache[sc_void];
1325 assert(ref_type);
1326 }
1327 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1328 di->symt = ref_type;
1329
1330 return ref_type;
1331 }
1332
1333 static struct symt* dwarf2_parse_volatile_type(dwarf2_parse_context_t* ctx,
1334 dwarf2_debug_info_t* di)
1335 {
1336 struct symt* ref_type;
1337
1338 if (di->symt) return di->symt;
1339
1340 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1341
1342 if (!(ref_type = dwarf2_lookup_type(ctx, di)))
1343 {
1344 ref_type = ctx->symt_cache[sc_void];
1345 assert(ref_type);
1346 }
1347 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1348 di->symt = ref_type;
1349
1350 return ref_type;
1351 }
1352
1353 static struct symt* dwarf2_parse_unspecified_type(dwarf2_parse_context_t* ctx,
1354 dwarf2_debug_info_t* di)
1355 {
1356 struct attribute name;
1357 struct attribute size;
1358 struct symt_basic *basic;
1359
1360 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1361
1362 if (di->symt) return di->symt;
1363
1364 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1365 name.u.string = "void";
1366 size.u.uvalue = sizeof(void *);
1367
1368 basic = symt_new_basic(ctx->module, btVoid, name.u.string, size.u.uvalue);
1369 di->symt = &basic->symt;
1370
1371 if (!ctx->symt_cache[sc_void])
1372 ctx->symt_cache[sc_void] = di->symt;
1373
1374 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1375 return di->symt;
1376 }
1377
1378 static struct symt* dwarf2_parse_reference_type(dwarf2_parse_context_t* ctx,
1379 dwarf2_debug_info_t* di)
1380 {
1381 struct symt* ref_type = NULL;
1382
1383 if (di->symt) return di->symt;
1384
1385 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1386
1387 ref_type = dwarf2_lookup_type(ctx, di);
1388 /* FIXME: for now, we hard-wire C++ references to pointers */
1389 di->symt = &symt_new_pointer(ctx->module, ref_type, sizeof(void *))->symt;
1390
1391 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1392
1393 return di->symt;
1394 }
1395
1396 static void dwarf2_parse_udt_member(dwarf2_parse_context_t* ctx,
1397 dwarf2_debug_info_t* di,
1398 struct symt_udt* parent)
1399 {
1400 struct symt* elt_type;
1401 struct attribute name;
1402 struct attribute bit_size;
1403 struct attribute bit_offset;
1404 struct location loc;
1405
1406 assert(parent);
1407
1408 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1409
1410 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1411 elt_type = dwarf2_lookup_type(ctx, di);
1412 if (dwarf2_compute_location_attr(ctx, di, DW_AT_data_member_location, &loc, NULL))
1413 {
1414 if (loc.kind != loc_absolute)
1415 {
1416 FIXME("Found register, while not expecting it\n");
1417 loc.offset = 0;
1418 }
1419 else
1420 TRACE("found member_location at %s -> %lu\n",
1421 dwarf2_debug_ctx(ctx), loc.offset);
1422 }
1423 else
1424 loc.offset = 0;
1425 if (!dwarf2_find_attribute(ctx, di, DW_AT_bit_size, &bit_size))
1426 bit_size.u.uvalue = 0;
1427 if (dwarf2_find_attribute(ctx, di, DW_AT_bit_offset, &bit_offset))
1428 {
1429 /* FIXME: we should only do this when implementation is LSB (which is
1430 * the case on i386 processors)
1431 */
1432 struct attribute nbytes;
1433 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &nbytes))
1434 {
1435 DWORD64 size;
1436 nbytes.u.uvalue = symt_get_info(ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1437 (unsigned long)size : 0;
1438 }
1439 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1440 }
1441 else bit_offset.u.uvalue = 0;
1442 symt_add_udt_element(ctx->module, parent, name.u.string, elt_type,
1443 (loc.offset << 3) + bit_offset.u.uvalue,
1444 bit_size.u.uvalue);
1445
1446 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1447 }
1448
1449 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1450 dwarf2_debug_info_t* di);
1451
1452 static struct symt* dwarf2_parse_udt_type(dwarf2_parse_context_t* ctx,
1453 dwarf2_debug_info_t* di,
1454 enum UdtKind udt)
1455 {
1456 struct attribute name;
1457 struct attribute size;
1458 struct vector* children;
1459 dwarf2_debug_info_t*child;
1460 unsigned int i;
1461
1462 if (di->symt) return di->symt;
1463
1464 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1465
1466 /* quirk... FIXME provide real support for anonymous UDTs */
1467 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1468 name.u.string = "zz_anon_zz";
1469 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1470
1471 di->symt = &symt_new_udt(ctx->module, dwarf2_get_cpp_name(ctx, di, name.u.string),
1472 size.u.uvalue, udt)->symt;
1473
1474 children = dwarf2_get_di_children(ctx, di);
1475 if (children) for (i = 0; i < vector_length(children); i++)
1476 {
1477 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1478
1479 switch (child->abbrev->tag)
1480 {
1481 case DW_TAG_array_type:
1482 dwarf2_parse_array_type(ctx, di);
1483 break;
1484 case DW_TAG_member:
1485 /* FIXME: should I follow the sibling stuff ?? */
1486 dwarf2_parse_udt_member(ctx, child, (struct symt_udt*)di->symt);
1487 break;
1488 case DW_TAG_enumeration_type:
1489 dwarf2_parse_enumeration_type(ctx, child);
1490 break;
1491 case DW_TAG_subprogram:
1492 dwarf2_parse_subprogram(ctx, child);
1493 break;
1494 case DW_TAG_const_type:
1495 dwarf2_parse_const_type(ctx, child);
1496 break;
1497 case DW_TAG_structure_type:
1498 case DW_TAG_class_type:
1499 case DW_TAG_union_type:
1500 case DW_TAG_typedef:
1501 /* FIXME: we need to handle nested udt definitions */
1502 case DW_TAG_inheritance:
1503 case DW_TAG_template_type_param:
1504 case DW_TAG_template_value_param:
1505 case DW_TAG_variable:
1506 case DW_TAG_imported_declaration:
1507 case DW_TAG_ptr_to_member_type:
1508 case DW_TAG_GNU_template_parameter_pack:
1509 case DW_TAG_GNU_formal_parameter_pack:
1510 /* FIXME: some C++ related stuff */
1511 break;
1512 default:
1513 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1514 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1515 break;
1516 }
1517 }
1518
1519 return di->symt;
1520 }
1521
1522 static void dwarf2_parse_enumerator(dwarf2_parse_context_t* ctx,
1523 dwarf2_debug_info_t* di,
1524 struct symt_enum* parent)
1525 {
1526 struct attribute name;
1527 struct attribute value;
1528
1529 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1530
1531 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) return;
1532 if (!dwarf2_find_attribute(ctx, di, DW_AT_const_value, &value)) value.u.svalue = 0;
1533 symt_add_enum_element(ctx->module, parent, name.u.string, value.u.svalue);
1534
1535 if (dwarf2_get_di_children(ctx, di)) FIXME("Unsupported children\n");
1536 }
1537
1538 static struct symt* dwarf2_parse_enumeration_type(dwarf2_parse_context_t* ctx,
1539 dwarf2_debug_info_t* di)
1540 {
1541 struct attribute name;
1542 struct attribute size;
1543 struct symt_basic* basetype;
1544 struct vector* children;
1545 dwarf2_debug_info_t*child;
1546 unsigned int i;
1547
1548 if (di->symt) return di->symt;
1549
1550 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1551
1552 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name)) name.u.string = NULL;
1553 if (!dwarf2_find_attribute(ctx, di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1554
1555 switch (size.u.uvalue) /* FIXME: that's wrong */
1556 {
1557 case 1: basetype = symt_new_basic(ctx->module, btInt, "char", 1); break;
1558 case 2: basetype = symt_new_basic(ctx->module, btInt, "short", 2); break;
1559 default:
1560 case 4: basetype = symt_new_basic(ctx->module, btInt, "int", 4); break;
1561 }
1562
1563 di->symt = &symt_new_enum(ctx->module, name.u.string, &basetype->symt)->symt;
1564
1565 children = dwarf2_get_di_children(ctx, di);
1566 /* FIXME: should we use the sibling stuff ?? */
1567 if (children) for (i = 0; i < vector_length(children); i++)
1568 {
1569 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1570
1571 switch (child->abbrev->tag)
1572 {
1573 case DW_TAG_enumerator:
1574 dwarf2_parse_enumerator(ctx, child, (struct symt_enum*)di->symt);
1575 break;
1576 default:
1577 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1578 di->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1579 }
1580 }
1581 return di->symt;
1582 }
1583
1584 /* structure used to pass information around when parsing a subprogram */
1585 typedef struct dwarf2_subprogram_s
1586 {
1587 dwarf2_parse_context_t* ctx;
1588 struct symt_function* func;
1589 BOOL non_computed_variable;
1590 struct location frame;
1591 } dwarf2_subprogram_t;
1592
1593 /******************************************************************
1594 * dwarf2_parse_variable
1595 *
1596 * Parses any variable (parameter, local/global variable)
1597 */
1598 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1599 struct symt_block* block,
1600 dwarf2_debug_info_t* di)
1601 {
1602 struct symt* param_type;
1603 struct attribute name, value;
1604 struct location loc;
1605 BOOL is_pmt;
1606
1607 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1608
1609 is_pmt = !block && di->abbrev->tag == DW_TAG_formal_parameter;
1610 param_type = dwarf2_lookup_type(subpgm->ctx, di);
1611
1612 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name)) {
1613 /* cannot do much without the name, the functions below won't like it. */
1614 return;
1615 }
1616 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1617 &loc, &subpgm->frame))
1618 {
1619 struct attribute ext;
1620
1621 TRACE("found parameter %s (kind=%d, offset=%ld, reg=%d) at %s\n",
1622 name.u.string, loc.kind, loc.offset, loc.reg,
1623 dwarf2_debug_ctx(subpgm->ctx));
1624
1625 switch (loc.kind)
1626 {
1627 case loc_error:
1628 break;
1629 case loc_absolute:
1630 /* it's a global variable */
1631 /* FIXME: we don't handle its scope yet */
1632 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_external, &ext))
1633 ext.u.uvalue = 0;
1634 loc.offset += subpgm->ctx->load_offset;
1635 symt_new_global_variable(subpgm->ctx->module, subpgm->ctx->compiland,
1636 dwarf2_get_cpp_name(subpgm->ctx, di, name.u.string), !ext.u.uvalue,
1637 loc, 0, param_type);
1638 break;
1639 default:
1640 subpgm->non_computed_variable = TRUE;
1641 /* fall through */
1642 case loc_register:
1643 case loc_regrel:
1644 /* either a pmt/variable relative to frame pointer or
1645 * pmt/variable in a register
1646 */
1647 assert(subpgm->func);
1648 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1649 is_pmt ? DataIsParam : DataIsLocal,
1650 &loc, block, param_type, name.u.string);
1651 break;
1652 }
1653 }
1654 else if (dwarf2_find_attribute(subpgm->ctx, di, DW_AT_const_value, &value))
1655 {
1656 VARIANT v;
1657 if (subpgm->func) WARN("Unsupported constant %s in function\n", name.u.string);
1658 if (is_pmt) FIXME("Unsupported constant (parameter) %s in function\n", name.u.string);
1659 switch (value.form)
1660 {
1661 case DW_FORM_data1:
1662 case DW_FORM_data2:
1663 case DW_FORM_data4:
1664 case DW_FORM_udata:
1665 case DW_FORM_addr:
1666 v.n1.n2.vt = VT_UI4;
1667 v.n1.n2.n3.lVal = value.u.uvalue;
1668 break;
1669
1670 case DW_FORM_data8:
1671 v.n1.n2.vt = VT_UI8;
1672 v.n1.n2.n3.llVal = value.u.lluvalue;
1673 break;
1674
1675 case DW_FORM_sdata:
1676 v.n1.n2.vt = VT_I4;
1677 v.n1.n2.n3.lVal = value.u.svalue;
1678 break;
1679
1680 case DW_FORM_strp:
1681 case DW_FORM_string:
1682 /* FIXME: native doesn't report const strings from here !!
1683 * however, the value of the string is in the code somewhere
1684 */
1685 v.n1.n2.vt = VT_I1 | VT_BYREF;
1686 v.n1.n2.n3.byref = pool_strdup(&subpgm->ctx->module->pool, value.u.string);
1687 break;
1688
1689 case DW_FORM_block:
1690 case DW_FORM_block1:
1691 case DW_FORM_block2:
1692 case DW_FORM_block4:
1693 v.n1.n2.vt = VT_I4;
1694 switch (value.u.block.size)
1695 {
1696 case 1: v.n1.n2.n3.lVal = *(BYTE*)value.u.block.ptr; break;
1697 case 2: v.n1.n2.n3.lVal = *(USHORT*)value.u.block.ptr; break;
1698 case 4: v.n1.n2.n3.lVal = *(DWORD*)value.u.block.ptr; break;
1699 default:
1700 v.n1.n2.vt = VT_I1 | VT_BYREF;
1701 v.n1.n2.n3.byref = pool_alloc(&subpgm->ctx->module->pool, value.u.block.size);
1702 memcpy(v.n1.n2.n3.byref, value.u.block.ptr, value.u.block.size);
1703 }
1704 break;
1705
1706 default:
1707 FIXME("Unsupported form for const value %s (%lx)\n",
1708 name.u.string, value.form);
1709 v.n1.n2.vt = VT_EMPTY;
1710 }
1711 di->symt = &symt_new_constant(subpgm->ctx->module, subpgm->ctx->compiland,
1712 name.u.string, param_type, &v)->symt;
1713 }
1714 else
1715 {
1716 /* variable has been optimized away... report anyway */
1717 loc.kind = loc_error;
1718 loc.reg = loc_err_no_location;
1719 if (subpgm->func)
1720 {
1721 symt_add_func_local(subpgm->ctx->module, subpgm->func,
1722 is_pmt ? DataIsParam : DataIsLocal,
1723 &loc, block, param_type, name.u.string);
1724 }
1725 else
1726 {
1727 WARN("dropping global variable %s which has been optimized away\n", name.u.string);
1728 }
1729 }
1730 if (is_pmt && subpgm->func && subpgm->func->type)
1731 symt_add_function_signature_parameter(subpgm->ctx->module,
1732 (struct symt_function_signature*)subpgm->func->type,
1733 param_type);
1734
1735 if (dwarf2_get_di_children(subpgm->ctx, di)) FIXME("Unsupported children\n");
1736 }
1737
1738 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
1739 const dwarf2_debug_info_t* di)
1740 {
1741 struct attribute name;
1742 struct attribute low_pc;
1743 struct location loc;
1744
1745 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1746
1747 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_low_pc, &low_pc)) low_pc.u.uvalue = 0;
1748 if (!dwarf2_find_attribute(subpgm->ctx, di, DW_AT_name, &name))
1749 name.u.string = NULL;
1750
1751 loc.kind = loc_absolute;
1752 loc.offset = subpgm->ctx->load_offset + low_pc.u.uvalue;
1753 symt_add_function_point(subpgm->ctx->module, subpgm->func, SymTagLabel,
1754 &loc, name.u.string);
1755 }
1756
1757 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1758 struct symt_block* parent_block,
1759 dwarf2_debug_info_t* di);
1760
1761 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
1762 dwarf2_debug_info_t* di);
1763
1764 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
1765 struct symt_block* parent_block,
1766 dwarf2_debug_info_t* di)
1767 {
1768 struct symt_block* block;
1769 unsigned long low_pc, high_pc;
1770 struct vector* children;
1771 dwarf2_debug_info_t*child;
1772 unsigned int i;
1773
1774 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1775
1776 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1777 {
1778 FIXME("cannot read range\n");
1779 return;
1780 }
1781
1782 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1783 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1784 high_pc - low_pc);
1785
1786 children = dwarf2_get_di_children(subpgm->ctx, di);
1787 if (children) for (i = 0; i < vector_length(children); i++)
1788 {
1789 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1790
1791 switch (child->abbrev->tag)
1792 {
1793 case DW_TAG_formal_parameter:
1794 case DW_TAG_variable:
1795 dwarf2_parse_variable(subpgm, block, child);
1796 break;
1797 case DW_TAG_lexical_block:
1798 dwarf2_parse_subprogram_block(subpgm, block, child);
1799 break;
1800 case DW_TAG_inlined_subroutine:
1801 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1802 break;
1803 case DW_TAG_label:
1804 dwarf2_parse_subprogram_label(subpgm, child);
1805 break;
1806 case DW_TAG_GNU_call_site:
1807 /* this isn't properly supported by dbghelp interface. skip it for now */
1808 break;
1809 default:
1810 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1811 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx),
1812 dwarf2_debug_di(di));
1813 }
1814 }
1815 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1816 }
1817
1818 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
1819 struct symt_block* parent_block,
1820 dwarf2_debug_info_t* di)
1821 {
1822 struct symt_block* block;
1823 unsigned long low_pc, high_pc;
1824 struct vector* children;
1825 dwarf2_debug_info_t*child;
1826 unsigned int i;
1827
1828 TRACE("%s, for %s\n", dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1829
1830 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
1831 {
1832 WARN("no range\n");
1833 return;
1834 }
1835
1836 block = symt_open_func_block(subpgm->ctx->module, subpgm->func, parent_block,
1837 subpgm->ctx->load_offset + low_pc - subpgm->func->address,
1838 high_pc - low_pc);
1839
1840 children = dwarf2_get_di_children(subpgm->ctx, di);
1841 if (children) for (i = 0; i < vector_length(children); i++)
1842 {
1843 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1844
1845 switch (child->abbrev->tag)
1846 {
1847 case DW_TAG_inlined_subroutine:
1848 dwarf2_parse_inlined_subroutine(subpgm, block, child);
1849 break;
1850 case DW_TAG_variable:
1851 dwarf2_parse_variable(subpgm, block, child);
1852 break;
1853 case DW_TAG_pointer_type:
1854 dwarf2_parse_pointer_type(subpgm->ctx, di);
1855 break;
1856 case DW_TAG_subroutine_type:
1857 dwarf2_parse_subroutine_type(subpgm->ctx, di);
1858 break;
1859 case DW_TAG_const_type:
1860 dwarf2_parse_const_type(subpgm->ctx, di);
1861 break;
1862 case DW_TAG_lexical_block:
1863 dwarf2_parse_subprogram_block(subpgm, block, child);
1864 break;
1865 case DW_TAG_subprogram:
1866 /* FIXME: likely a declaration (to be checked)
1867 * skip it for now
1868 */
1869 break;
1870 case DW_TAG_formal_parameter:
1871 /* FIXME: likely elements for exception handling (GCC flavor)
1872 * Skip it for now
1873 */
1874 break;
1875 case DW_TAG_imported_module:
1876 /* C++ stuff to be silenced (for now) */
1877 break;
1878 case DW_TAG_GNU_call_site:
1879 /* this isn't properly supported by dbghelp interface. skip it for now */
1880 break;
1881 case DW_TAG_label:
1882 dwarf2_parse_subprogram_label(subpgm, child);
1883 break;
1884 case DW_TAG_class_type:
1885 case DW_TAG_structure_type:
1886 case DW_TAG_union_type:
1887 case DW_TAG_enumeration_type:
1888 case DW_TAG_typedef:
1889 /* the type referred to will be loaded when we need it, so skip it */
1890 break;
1891 default:
1892 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
1893 child->abbrev->tag, dwarf2_debug_ctx(subpgm->ctx), dwarf2_debug_di(di));
1894 }
1895 }
1896
1897 symt_close_func_block(subpgm->ctx->module, subpgm->func, block, 0);
1898 }
1899
1900 static struct symt* dwarf2_parse_subprogram(dwarf2_parse_context_t* ctx,
1901 dwarf2_debug_info_t* di)
1902 {
1903 struct attribute name;
1904 unsigned long low_pc, high_pc;
1905 struct attribute is_decl;
1906 struct attribute inline_flags;
1907 struct symt* ret_type;
1908 struct symt_function_signature* sig_type;
1909 dwarf2_subprogram_t subpgm;
1910 struct vector* children;
1911 dwarf2_debug_info_t* child;
1912 unsigned int i;
1913
1914 if (di->symt) return di->symt;
1915
1916 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
1917
1918 if (!dwarf2_find_attribute(ctx, di, DW_AT_name, &name))
1919 {
1920 WARN("No name for function... dropping function\n");
1921 return NULL;
1922 }
1923 /* if it's an abstract representation of an inline function, there should be
1924 * a concrete object that we'll handle
1925 */
1926 if (dwarf2_find_attribute(ctx, di, DW_AT_inline, &inline_flags) &&
1927 inline_flags.u.uvalue != DW_INL_not_inlined)
1928 {
1929 TRACE("Function %s declared as inlined (%ld)... skipping\n",
1930 debugstr_a(name.u.string), inline_flags.u.uvalue);
1931 return NULL;
1932 }
1933
1934 if (dwarf2_find_attribute(ctx, di, DW_AT_declaration, &is_decl) &&
1935 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
1936 {
1937 /* it's a real declaration, skip it */
1938 return NULL;
1939 }
1940 if (!dwarf2_read_range(ctx, di, &low_pc, &high_pc))
1941 {
1942 WARN("cannot get range for %s\n", name.u.string);
1943 return NULL;
1944 }
1945 /* As functions (defined as inline assembly) get debug info with dwarf
1946 * (not the case for stabs), we just drop Wine's thunks here...
1947 * Actual thunks will be created in elf_module from the symbol table
1948 */
1949 #ifndef DBGHELP_STATIC_LIB
1950 if (elf_is_in_thunk_area(ctx->load_offset + low_pc, ctx->thunks) >= 0)
1951 return NULL;
1952 #endif
1953 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
1954 {
1955 ret_type = ctx->symt_cache[sc_void];
1956 assert(ret_type);
1957 }
1958 /* FIXME: assuming C source code */
1959 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
1960 subpgm.func = symt_new_function(ctx->module, ctx->compiland,
1961 dwarf2_get_cpp_name(ctx, di, name.u.string),
1962 ctx->load_offset + low_pc, high_pc - low_pc,
1963 &sig_type->symt);
1964 di->symt = &subpgm.func->symt;
1965 subpgm.ctx = ctx;
1966 if (!dwarf2_compute_location_attr(ctx, di, DW_AT_frame_base,
1967 &subpgm.frame, NULL))
1968 {
1969 /* on stack !! */
1970 subpgm.frame.kind = loc_regrel;
1971 subpgm.frame.reg = dbghelp_current_cpu->frame_regno;
1972 subpgm.frame.offset = 0;
1973 }
1974 subpgm.non_computed_variable = FALSE;
1975
1976 children = dwarf2_get_di_children(ctx, di);
1977 if (children) for (i = 0; i < vector_length(children); i++)
1978 {
1979 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1980
1981 switch (child->abbrev->tag)
1982 {
1983 case DW_TAG_variable:
1984 case DW_TAG_formal_parameter:
1985 dwarf2_parse_variable(&subpgm, NULL, child);
1986 break;
1987 case DW_TAG_lexical_block:
1988 dwarf2_parse_subprogram_block(&subpgm, NULL, child);
1989 break;
1990 case DW_TAG_inlined_subroutine:
1991 dwarf2_parse_inlined_subroutine(&subpgm, NULL, child);
1992 break;
1993 case DW_TAG_pointer_type:
1994 dwarf2_parse_pointer_type(subpgm.ctx, di);
1995 break;
1996 case DW_TAG_const_type:
1997 dwarf2_parse_const_type(subpgm.ctx, di);
1998 break;
1999 case DW_TAG_subprogram:
2000 /* FIXME: likely a declaration (to be checked)
2001 * skip it for now
2002 */
2003 break;
2004 case DW_TAG_label:
2005 dwarf2_parse_subprogram_label(&subpgm, child);
2006 break;
2007 case DW_TAG_class_type:
2008 case DW_TAG_structure_type:
2009 case DW_TAG_union_type:
2010 case DW_TAG_enumeration_type:
2011 case DW_TAG_typedef:
2012 /* the type referred to will be loaded when we need it, so skip it */
2013 break;
2014 case DW_TAG_unspecified_parameters:
2015 case DW_TAG_template_type_param:
2016 case DW_TAG_template_value_param:
2017 case DW_TAG_GNU_call_site:
2018 case DW_TAG_GNU_template_parameter_pack:
2019 case DW_TAG_GNU_formal_parameter_pack:
2020 /* FIXME: no support in dbghelp's internals so far */
2021 break;
2022 default:
2023 FIXME("Unhandled Tag type 0x%lx at %s, for %s\n",
2024 child->abbrev->tag, dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2025 }
2026 }
2027
2028 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2029 {
2030 symt_add_function_point(ctx->module, subpgm.func, SymTagCustom,
2031 &subpgm.frame, NULL);
2032 }
2033 if (subpgm.func) symt_normalize_function(subpgm.ctx->module, subpgm.func);
2034
2035 return di->symt;
2036 }
2037
2038 static struct symt* dwarf2_parse_subroutine_type(dwarf2_parse_context_t* ctx,
2039 dwarf2_debug_info_t* di)
2040 {
2041 struct symt* ret_type;
2042 struct symt_function_signature* sig_type;
2043 struct vector* children;
2044 dwarf2_debug_info_t* child;
2045 unsigned int i;
2046
2047 if (di->symt) return di->symt;
2048
2049 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2050
2051 if (!(ret_type = dwarf2_lookup_type(ctx, di)))
2052 {
2053 ret_type = ctx->symt_cache[sc_void];
2054 assert(ret_type);
2055 }
2056
2057 /* FIXME: assuming C source code */
2058 sig_type = symt_new_function_signature(ctx->module, ret_type, CV_CALL_FAR_C);
2059
2060 children = dwarf2_get_di_children(ctx, di);
2061 if (children) for (i = 0; i < vector_length(children); i++)
2062 {
2063 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2064
2065 switch (child->abbrev->tag)
2066 {
2067 case DW_TAG_formal_parameter:
2068 symt_add_function_signature_parameter(ctx->module, sig_type,
2069 dwarf2_lookup_type(ctx, child));
2070 break;
2071 case DW_TAG_unspecified_parameters:
2072 WARN("Unsupported unspecified parameters\n");
2073 break;
2074 }
2075 }
2076
2077 return di->symt = &sig_type->symt;
2078 }
2079
2080 static void dwarf2_parse_namespace(dwarf2_parse_context_t* ctx,
2081 dwarf2_debug_info_t* di)
2082 {
2083 struct vector* children;
2084 dwarf2_debug_info_t* child;
2085 unsigned int i;
2086
2087 if (di->symt) return;
2088
2089 TRACE("%s, for %s\n", dwarf2_debug_ctx(ctx), dwarf2_debug_di(di));
2090
2091 di->symt = ctx->symt_cache[sc_void];
2092
2093 children = dwarf2_get_di_children(ctx, di);
2094 if (children) for (i = 0; i < vector_length(children); i++)
2095 {
2096 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2097 dwarf2_load_one_entry(ctx, child);
2098 }
2099 }
2100
2101 static void dwarf2_load_one_entry(dwarf2_parse_context_t* ctx,
2102 dwarf2_debug_info_t* di)
2103 {
2104 switch (di->abbrev->tag)
2105 {
2106 case DW_TAG_typedef:
2107 dwarf2_parse_typedef(ctx, di);
2108 break;
2109 case DW_TAG_base_type:
2110 dwarf2_parse_base_type(ctx, di);
2111 break;
2112 case DW_TAG_pointer_type:
2113 dwarf2_parse_pointer_type(ctx, di);
2114 break;
2115 case DW_TAG_class_type:
2116 dwarf2_parse_udt_type(ctx, di, UdtClass);
2117 break;
2118 case DW_TAG_structure_type:
2119 dwarf2_parse_udt_type(ctx, di, UdtStruct);
2120 break;
2121 case DW_TAG_union_type:
2122 dwarf2_parse_udt_type(ctx, di, UdtUnion);
2123 break;
2124 case DW_TAG_array_type:
2125 dwarf2_parse_array_type(ctx, di);
2126 break;
2127 case DW_TAG_const_type:
2128 dwarf2_parse_const_type(ctx, di);
2129 break;
2130 case DW_TAG_volatile_type:
2131 dwarf2_parse_volatile_type(ctx, di);
2132 break;
2133 case DW_TAG_unspecified_type:
2134 dwarf2_parse_unspecified_type(ctx, di);
2135 break;
2136 case DW_TAG_reference_type:
2137 dwarf2_parse_reference_type(ctx, di);
2138 break;
2139 case DW_TAG_enumeration_type:
2140 dwarf2_parse_enumeration_type(ctx, di);
2141 break;
2142 case DW_TAG_subprogram:
2143 dwarf2_parse_subprogram(ctx, di);
2144 break;
2145 case DW_TAG_subroutine_type:
2146 dwarf2_parse_subroutine_type(ctx, di);
2147 break;
2148 case DW_TAG_variable:
2149 {
2150 dwarf2_subprogram_t subpgm;
2151
2152 subpgm.ctx = ctx;
2153 subpgm.func = NULL;
2154 subpgm.frame.kind = loc_absolute;
2155 subpgm.frame.offset = 0;
2156 subpgm.frame.reg = Wine_DW_no_register;
2157 dwarf2_parse_variable(&subpgm, NULL, di);
2158 }
2159 break;
2160 case DW_TAG_namespace:
2161 dwarf2_parse_namespace(ctx, di);
2162 break;
2163 /* silence a couple of C++ defines */
2164 case DW_TAG_imported_module:
2165 case DW_TAG_imported_declaration:
2166 case DW_TAG_ptr_to_member_type:
2167 break;
2168 default:
2169 FIXME("Unhandled Tag type 0x%lx at %s, for %lu\n",
2170 di->abbrev->tag, dwarf2_debug_ctx(ctx), di->abbrev->entry_code);
2171 }
2172 }
2173
2174 static void dwarf2_set_line_number(struct module* module, unsigned long address,
2175 const struct vector* v, unsigned file, unsigned line)
2176 {
2177 struct symt_function* func;
2178 struct symt_ht* symt;
2179 unsigned* psrc;
2180
2181 if (!file || !(psrc = vector_at(v, file - 1))) return;
2182
2183 TRACE("%s %lx %s %u\n",
2184 debugstr_w(module->module.ModuleName), address, source_get(module, *psrc), line);
2185 if (!(symt = symt_find_nearest(module, address)) ||
2186 symt->symt.tag != SymTagFunction) return;
2187 func = (struct symt_function*)symt;
2188 symt_add_func_line(module, func, *psrc, line, address - func->address);
2189 }
2190
2191 static BOOL dwarf2_parse_line_numbers(const dwarf2_section_t* sections,
2192 dwarf2_parse_context_t* ctx,
2193 const char* compile_dir,
2194 unsigned long offset)
2195 {
2196 dwarf2_traverse_context_t traverse;
2197 unsigned long length;
2198 unsigned insn_size, default_stmt;
2199 unsigned line_range, opcode_base;
2200 int line_base;
2201 const unsigned char* opcode_len;
2202 struct vector dirs;
2203 struct vector files;
2204 const char** p;
2205
2206 /* section with line numbers stripped */
2207 if (sections[section_line].address == IMAGE_NO_MAP)
2208 return FALSE;
2209
2210 if (offset + 4 > sections[section_line].size)
2211 {
2212 WARN("out of bounds offset\n");
2213 return FALSE;
2214 }
2215 traverse.data = sections[section_line].address + offset;
2216 traverse.end_data = traverse.data + 4;
2217 traverse.word_size = ctx->module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
2218
2219 length = dwarf2_parse_u4(&traverse);
2220 traverse.end_data = sections[section_line].address + offset + length;
2221
2222 if (offset + 4 + length > sections[section_line].size)
2223 {
2224 WARN("out of bounds header\n");
2225 return FALSE;
2226 }
2227 dwarf2_parse_u2(&traverse); /* version */
2228 dwarf2_parse_u4(&traverse); /* header_len */
2229 insn_size = dwarf2_parse_byte(&traverse);
2230 default_stmt = dwarf2_parse_byte(&traverse);
2231 line_base = (signed char)dwarf2_parse_byte(&traverse);
2232 line_range = dwarf2_parse_byte(&traverse);
2233 opcode_base = dwarf2_parse_byte(&traverse);
2234
2235 opcode_len = traverse.data;
2236 traverse.data += opcode_base - 1;
2237
2238 vector_init(&dirs, sizeof(const char*), 4);
2239 p = vector_add(&dirs, &ctx->pool);
2240 *p = compile_dir ? compile_dir : ".";
2241 while (*traverse.data)
2242 {
2243 const char* rel = (const char*)traverse.data;
2244 unsigned rellen = strlen(rel);
2245 TRACE("Got include %s\n", rel);
2246 traverse.data += rellen + 1;
2247 p = vector_add(&dirs, &ctx->pool);
2248
2249 if (*rel == '/' || !compile_dir)
2250 *p = rel;
2251 else
2252 {
2253 /* include directory relative to compile directory */
2254 unsigned baselen = strlen(compile_dir);
2255 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2256 strcpy(tmp, compile_dir);
2257 if (tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2258 strcpy(&tmp[baselen], rel);
2259 *p = tmp;
2260 }
2261
2262 }
2263 traverse.data++;
2264
2265 vector_init(&files, sizeof(unsigned), 16);
2266 while (*traverse.data)
2267 {
2268 unsigned int dir_index, mod_time;
2269 const char* name;
2270 const char* dir;
2271 unsigned* psrc;
2272
2273 name = (const char*)traverse.data;
2274 traverse.data += strlen(name) + 1;
2275 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2276 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2277 length = dwarf2_leb128_as_unsigned(&traverse);
2278 dir = *(const char**)vector_at(&dirs, dir_index);
2279 TRACE("Got file %s/%s (%u,%lu)\n", dir, name, mod_time, length);
2280 psrc = vector_add(&files, &ctx->pool);
2281 *psrc = source_new(ctx->module, dir, name);
2282 }
2283 traverse.data++;
2284
2285 while (traverse.data < traverse.end_data)
2286 {
2287 unsigned long address = 0;
2288 unsigned file = 1;
2289 unsigned line = 1;
2290 unsigned is_stmt = default_stmt;
2291 BOOL end_sequence = FALSE;
2292 unsigned opcode, extopcode, i;
2293
2294 while (!end_sequence)
2295 {
2296 opcode = dwarf2_parse_byte(&traverse);
2297 TRACE("Got opcode %x\n", opcode);
2298
2299 if (opcode >= opcode_base)
2300 {
2301 unsigned delta = opcode - opcode_base;
2302
2303 address += (delta / line_range) * insn_size;
2304 line += line_base + (delta % line_range);
2305 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2306 }
2307 else
2308 {
2309 switch (opcode)
2310 {
2311 case DW_LNS_copy:
2312 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2313 break;
2314 case DW_LNS_advance_pc:
2315 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2316 break;
2317 case DW_LNS_advance_line:
2318 line += dwarf2_leb128_as_signed(&traverse);
2319 break;
2320 case DW_LNS_set_file:
2321 file = dwarf2_leb128_as_unsigned(&traverse);
2322 break;
2323 case DW_LNS_set_column:
2324 dwarf2_leb128_as_unsigned(&traverse);
2325 break;
2326 case DW_LNS_negate_stmt:
2327 is_stmt = !is_stmt;
2328 break;
2329 case DW_LNS_set_basic_block:
2330 break;
2331 case DW_LNS_const_add_pc:
2332 address += ((255 - opcode_base) / line_range) * insn_size;
2333 break;
2334 case DW_LNS_fixed_advance_pc:
2335 address += dwarf2_parse_u2(&traverse);
2336 break;
2337 case DW_LNS_extended_op:
2338 dwarf2_leb128_as_unsigned(&traverse);
2339 extopcode = dwarf2_parse_byte(&traverse);
2340 switch (extopcode)
2341 {
2342 case DW_LNE_end_sequence:
2343 dwarf2_set_line_number(ctx->module, address, &files, file, line);
2344 end_sequence = TRUE;
2345 break;
2346 case DW_LNE_set_address:
2347 address = ctx->load_offset + dwarf2_parse_addr(&traverse);
2348 break;
2349 case DW_LNE_define_file:
2350 FIXME("not handled define file %s\n", traverse.data);
2351 traverse.data += strlen((const char *)traverse.data) + 1;
2352 dwarf2_leb128_as_unsigned(&traverse);
2353 dwarf2_leb128_as_unsigned(&traverse);
2354 dwarf2_leb128_as_unsigned(&traverse);
2355 break;
2356 case DW_LNE_set_discriminator:
2357 {
2358 unsigned descr;
2359
2360 descr = dwarf2_leb128_as_unsigned(&traverse);
2361 WARN("not handled discriminator %x\n", descr);
2362 }
2363 break;
2364 default:
2365 FIXME("Unsupported extended opcode %x\n", extopcode);
2366 break;
2367 }
2368 break;
2369 default:
2370 WARN("Unsupported opcode %x\n", opcode);
2371 for (i = 0; i < opcode_len[opcode]; i++)
2372 dwarf2_leb128_as_unsigned(&traverse);
2373 break;
2374 }
2375 }
2376 }
2377 }
2378 return TRUE;
2379 }
2380
2381 static BOOL dwarf2_parse_compilation_unit(const dwarf2_section_t* sections,
2382 struct module* module,
2383 const struct elf_thunk_area* thunks,
2384 dwarf2_traverse_context_t* mod_ctx,
2385 unsigned long load_offset)
2386 {
2387 dwarf2_parse_context_t ctx;
2388 dwarf2_traverse_context_t abbrev_ctx;
2389 dwarf2_debug_info_t* di;
2390 dwarf2_traverse_context_t cu_ctx;
2391 const unsigned char* comp_unit_start = mod_ctx->data;
2392 unsigned long cu_length;
2393 unsigned short cu_version;
2394 unsigned long cu_abbrev_offset;
2395 BOOL ret = FALSE;
2396
2397 cu_length = dwarf2_parse_u4(mod_ctx);
2398 cu_ctx.data = mod_ctx->data;
2399 cu_ctx.end_data = mod_ctx->data + cu_length;
2400 mod_ctx->data += cu_length;
2401 cu_version = dwarf2_parse_u2(&cu_ctx);
2402 cu_abbrev_offset = dwarf2_parse_u4(&cu_ctx);
2403 cu_ctx.word_size = dwarf2_parse_byte(&cu_ctx);
2404
2405 TRACE("Compilation Unit Header found at 0x%x:\n",
2406 (int)(comp_unit_start - sections[section_debug].address));
2407 TRACE("- length: %lu\n", cu_length);
2408 TRACE("- version: %u\n", cu_version);
2409 TRACE("- abbrev_offset: %lu\n", cu_abbrev_offset);
2410 TRACE("- word_size: %u\n", cu_ctx.word_size);
2411
2412 if (cu_version != 2)
2413 {
2414 WARN("%u DWARF version unsupported. Wine dbghelp only support DWARF 2.\n",
2415 cu_version);
2416 return FALSE;
2417 }
2418
2419 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size = cu_ctx.word_size;
2420 mod_ctx->word_size = cu_ctx.word_size;
2421
2422 pool_init(&ctx.pool, 65536);
2423 ctx.sections = sections;
2424 ctx.section = section_debug;
2425 ctx.module = module;
2426 ctx.thunks = thunks;
2427 ctx.load_offset = load_offset;
2428 ctx.ref_offset = comp_unit_start - sections[section_debug].address;
2429 memset(ctx.symt_cache, 0, sizeof(ctx.symt_cache));
2430 ctx.symt_cache[sc_void] = &symt_new_basic(module, btVoid, "void", 0)->symt;
2431 ctx.cpp_name = NULL;
2432
2433 abbrev_ctx.data = sections[section_abbrev].address + cu_abbrev_offset;
2434 abbrev_ctx.end_data = sections[section_abbrev].address + sections[section_abbrev].size;
2435 abbrev_ctx.word_size = cu_ctx.word_size;
2436 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx.abbrev_table, &ctx.pool);
2437
2438 sparse_array_init(&ctx.debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2439 dwarf2_read_one_debug_info(&ctx, &cu_ctx, NULL, &di);
2440
2441 if (di->abbrev->tag == DW_TAG_compile_unit)
2442 {
2443 struct attribute name;
2444 struct vector* children;
2445 dwarf2_debug_info_t* child = NULL;
2446 unsigned int i;
2447 struct attribute stmt_list, low_pc;
2448 struct attribute comp_dir;
2449
2450 if (!dwarf2_find_attribute(&ctx, di, DW_AT_name, &name))
2451 name.u.string = NULL;
2452
2453 /* get working directory of current compilation unit */
2454 if (!dwarf2_find_attribute(&ctx, di, DW_AT_comp_dir, &comp_dir))
2455 comp_dir.u.string = NULL;
2456
2457 if (!dwarf2_find_attribute(&ctx, di, DW_AT_low_pc, &low_pc))
2458 low_pc.u.uvalue = 0;
2459 ctx.compiland = symt_new_compiland(module, ctx.load_offset + low_pc.u.uvalue,
2460 source_new(module, comp_dir.u.string, name.u.string));
2461 di->symt = &ctx.compiland->symt;
2462 children = dwarf2_get_di_children(&ctx, di);
2463 if (children) for (i = 0; i < vector_length(children); i++)
2464 {
2465 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2466 dwarf2_load_one_entry(&ctx, child);
2467 }
2468 if (dwarf2_find_attribute(&ctx, di, DW_AT_stmt_list, &stmt_list))
2469 {
2470 #if defined(__REACTOS__) && defined(__clang__)
2471 unsigned long stmt_list_val = stmt_list.u.uvalue;
2472 if (stmt_list_val > module->module.BaseOfImage)
2473 {
2474 /* FIXME: Clang is recording this as an address, not an offset */
2475 stmt_list_val -= module->module.BaseOfImage + sections[section_line].rva;
2476 }
2477 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list_val))
2478 #else
2479 if (dwarf2_parse_line_numbers(sections, &ctx, comp_dir.u.string, stmt_list.u.uvalue))
2480 #endif
2481 module->module.LineNumbers = TRUE;
2482 }
2483 ret = TRUE;
2484 }
2485 else FIXME("Should have a compilation unit here\n");
2486 pool_destroy(&ctx.pool);
2487 return ret;
2488 }
2489
2490 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const BYTE* start,
2491 unsigned long ip, dwarf2_traverse_context_t* lctx)
2492 {
2493 DWORD_PTR beg, end;
2494 const BYTE* ptr = start;
2495 DWORD len;
2496
2497 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
2498 {
2499 beg = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2500 end = dwarf2_get_addr(ptr, modfmt->u.dwarf2_info->word_size); ptr += modfmt->u.dwarf2_info->word_size;
2501 if (!beg && !end) break;
2502 len = dwarf2_get_u2(ptr); ptr += 2;
2503
2504 if (beg <= ip && ip < end)
2505 {
2506 lctx->data = ptr;
2507 lctx->end_data = ptr + len;
2508 lctx->word_size = modfmt->u.dwarf2_info->word_size;
2509 return TRUE;
2510 }
2511 ptr += len;
2512 }
2513 WARN("Couldn't find ip in location list\n");
2514 return FALSE;
2515 }
2516
2517 static enum location_error loc_compute_frame(struct process* pcs,
2518 const struct module_format* modfmt,
2519 const struct symt_function* func,
2520 DWORD_PTR ip, struct location* frame)
2521 {
2522 struct symt** psym = NULL;
2523 struct location* pframe;
2524 dwarf2_traverse_context_t lctx;
2525 enum location_error err;
2526 unsigned int i;
2527
2528 for (i=0; i<vector_length(&func->vchildren); i++)
2529 {
2530 psym = vector_at(&func->vchildren, i);
2531 if ((*psym)->tag == SymTagCustom)
2532 {
2533 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
2534
2535 /* First, recompute the frame information, if needed */
2536 switch (pframe->kind)
2537 {
2538 case loc_regrel:
2539 case loc_register:
2540 *frame = *pframe;
2541 break;
2542 case loc_dwarf2_location_list:
2543 WARN("Searching loclist for %s\n", func->hash_elt.name);
2544 if (!dwarf2_lookup_loclist(modfmt,
2545 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
2546 ip, &lctx))
2547 return loc_err_out_of_scope;
2548 if ((err = compute_location(&lctx, frame, pcs->handle, NULL)) < 0) return err;
2549 if (frame->kind >= loc_user)
2550 {
2551 WARN("Couldn't compute runtime frame location\n");
2552 return loc_err_too_complex;
2553 }
2554 break;
2555 default:
2556 WARN("Unsupported frame kind %d\n", pframe->kind);
2557 return loc_err_internal;
2558 }
2559 return 0;
2560 }
2561 }
2562 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
2563 return loc_err_internal;
2564 }
2565
2566 enum reg_rule
2567 {
2568 RULE_UNSET, /* not set at all */
2569 RULE_UNDEFINED, /* undefined value */
2570 RULE_SAME, /* same value as previous frame */
2571 RULE_CFA_OFFSET, /* stored at cfa offset */
2572 RULE_OTHER_REG, /* stored in other register */
2573 RULE_EXPRESSION, /* address specified by expression */
2574 RULE_VAL_EXPRESSION /* value specified by expression */
2575 };
2576
2577 /* make it large enough for all CPUs */
2578 #define NB_FRAME_REGS 64
2579 #define MAX_SAVED_STATES 16
2580
2581 struct frame_state
2582 {
2583 ULONG_PTR cfa_offset;
2584 unsigned char cfa_reg;
2585 enum reg_rule cfa_rule;
2586 enum reg_rule rules[NB_FRAME_REGS];
2587 ULONG_PTR regs[NB_FRAME_REGS];
2588 };
2589
2590 struct frame_info
2591 {
2592 ULONG_PTR ip;
2593 ULONG_PTR code_align;
2594 LONG_PTR data_align;
2595 unsigned char retaddr_reg;
2596 unsigned char fde_encoding;
2597 unsigned char lsda_encoding;
2598 unsigned char signal_frame;
2599 unsigned char aug_z_format;
2600 unsigned char state_sp;
2601 struct frame_state state;
2602 struct frame_state state_stack[MAX_SAVED_STATES];
2603 };
2604
2605 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding)
2606 {
2607 ULONG_PTR base;
2608
2609 if (encoding == DW_EH_PE_omit) return 0;
2610
2611 switch (encoding & 0xf0)
2612 {
2613 case DW_EH_PE_abs:
2614 base = 0;
2615 break;
2616 case DW_EH_PE_pcrel:
2617 base = (ULONG_PTR)ctx->data;
2618 break;
2619 default:
2620 FIXME("unsupported encoding %02x\n", encoding);
2621 return 0;
2622 }
2623
2624 switch (encoding & 0x0f)
2625 {
2626 case DW_EH_PE_native:
2627 return base + dwarf2_parse_addr(ctx);
2628 case DW_EH_PE_leb128:
2629 return base + dwarf2_leb128_as_unsigned(ctx);
2630 case DW_EH_PE_data2:
2631 return base + dwarf2_parse_u2(ctx);
2632 case DW_EH_PE_data4:
2633 return base + dwarf2_parse_u4(ctx);
2634 case DW_EH_PE_data8:
2635 return base + dwarf2_parse_u8(ctx);
2636 case DW_EH_PE_signed|DW_EH_PE_leb128:
2637 return base + dwarf2_leb128_as_signed(ctx);
2638 case DW_EH_PE_signed|DW_EH_PE_data2:
2639 return base + (signed short)dwarf2_parse_u2(ctx);
2640 case DW_EH_PE_signed|DW_EH_PE_data4:
2641 return base + (signed int)dwarf2_parse_u4(ctx);
2642 case DW_EH_PE_signed|DW_EH_PE_data8:
2643 return base + (LONG64)dwarf2_parse_u8(ctx);
2644 default:
2645 FIXME("unsupported encoding %02x\n", encoding);
2646 return 0;
2647 }
2648 }
2649
2650 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info)
2651 {
2652 unsigned char version;
2653 const char* augmentation;
2654 const unsigned char* end;
2655 ULONG_PTR len;
2656
2657 memset(info, 0, sizeof(*info));
2658 info->lsda_encoding = DW_EH_PE_omit;
2659 info->aug_z_format = 0;
2660
2661 /* parse the CIE first */
2662 version = dwarf2_parse_byte(ctx);
2663 if (version != 1 && version != 3 && version != 4)
2664 {
2665 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
2666 return FALSE;
2667 }
2668 augmentation = (const char*)ctx->data;
2669 ctx->data += strlen(augmentation) + 1;
2670
2671 switch (version)
2672 {
2673 case 4:
2674 /* skip 'address_size' and 'segment_size' */
2675 ctx->data += 2;
2676 /* fallthrough */
2677 case 1:
2678 case 3:
2679 info->code_align = dwarf2_leb128_as_unsigned(ctx);
2680 info->data_align = dwarf2_leb128_as_signed(ctx);
2681 info->retaddr_reg = version == 1 ? dwarf2_parse_byte(ctx) :dwarf2_leb128_as_unsigned(ctx);
2682 break;
2683 default:
2684 ;
2685 }
2686 info->state.cfa_rule = RULE_CFA_OFFSET;
2687
2688 end = NULL;
2689 TRACE("\tparsing augmentation %s\n", augmentation);
2690 if (*augmentation) do
2691 {
2692 switch (*augmentation)
2693 {
2694 case 'z':
2695 len = dwarf2_leb128_as_unsigned(ctx);
2696 end = ctx->data + len;
2697 info->aug_z_format = 1;
2698 continue;
2699 case 'L':
2700 info->lsda_encoding = dwarf2_parse_byte(ctx);
2701 continue;
2702 case 'P':
2703 {
2704 unsigned char encoding = dwarf2_parse_byte(ctx);
2705 /* throw away the indirect bit, as we don't care for the result */
2706 encoding &= ~DW_EH_PE_indirect;
2707 dwarf2_parse_augmentation_ptr(ctx, encoding); /* handler */
2708 continue;
2709 }
2710 case 'R':
2711 info->fde_encoding = dwarf2_parse_byte(ctx);
2712 continue;
2713 case 'S':
2714 info->signal_frame = 1;
2715 continue;
2716 }
2717 FIXME("unknown augmentation '%c'\n", *augmentation);
2718 if (!end) return FALSE;
2719 break;
2720 } while (*++augmentation);
2721 if (end) ctx->data = end;
2722 return TRUE;
2723 }
2724
2725 static BOOL dwarf2_get_cie(unsigned long addr, struct module* module, DWORD_PTR delta,
2726 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
2727 struct frame_info* info, BOOL in_eh_frame)
2728 {
2729 const unsigned char* ptr_blk;
2730 const unsigned char* cie_ptr;
2731 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
2732 unsigned len, id;
2733 unsigned long start, range;
2734 unsigned cie_id;
2735 const BYTE* start_data = fde_ctx->data;
2736
2737 cie_id = in_eh_frame ? 0 : DW_CIE_ID;
2738 /* skip 0-padding at beginning of section (alignment) */
2739 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
2740 {
2741 if (dwarf2_parse_u4(fde_ctx))
2742 {
2743 fde_ctx->data -= 4;
2744 break;
2745 }
2746 }
2747 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
2748 {
2749 /* find the FDE for address addr (skip CIE) */
2750 len = dwarf2_parse_u4(fde_ctx);
2751 if (len == 0xffffffff) FIXME("Unsupported yet 64-bit CIEs\n");
2752 ptr_blk = fde_ctx->data + len;
2753 id = dwarf2_parse_u4(fde_ctx);
2754 if (id == cie_id)
2755 {
2756 last_cie_ptr = fde_ctx->data - 8;
2757 /* we need some bits out of the CIE in order to parse all contents */
2758 if (!parse_cie_details(fde_ctx, info)) return FALSE;
2759 cie_ctx->data = fde_ctx->data;
2760 cie_ctx->end_data = ptr_blk;
2761 cie_ctx->word_size = fde_ctx->word_size;
2762 continue;
2763 }
2764 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
2765 if (cie_ptr != last_cie_ptr)
2766 {
2767 last_cie_ptr = cie_ptr;
2768 cie_ctx->data = cie_ptr;
2769 cie_ctx->word_size = fde_ctx->word_size;
2770 cie_ctx->end_data = cie_ptr + 4;
2771 cie_ctx->end_data = cie_ptr + 4 + dwarf2_parse_u4(cie_ctx);
2772 if (dwarf2_parse_u4(cie_ctx) != cie_id)
2773 {
2774 FIXME("wrong CIE pointer at %x from FDE %x\n",
2775 (unsigned)(cie_ptr - start_data),
2776 (unsigned)(fde_ctx->data - start_data));
2777 return FALSE;
2778 }
2779 if (!parse_cie_details(cie_ctx, info)) return FALSE;
2780 }
2781 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding);
2782 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F);
2783
2784 if (addr >= start && addr < start + range)
2785 {
2786 /* reset the FDE context */
2787 fde_ctx->end_data = ptr_blk;
2788
2789 info->ip = start;
2790 return TRUE;
2791 }
2792 }
2793 return FALSE;
2794 }
2795
2796 static int valid_reg(ULONG_PTR reg)
2797 {
2798 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %lx\n", reg);
2799 return (reg < NB_FRAME_REGS);
2800 }
2801
2802 static void execute_cfa_instructions(dwarf2_traverse_context_t* ctx,
2803 ULONG_PTR last_ip, struct frame_info *info)
2804 {
2805 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
2806 {
2807 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
2808
2809 if (op & 0xc0)
2810 {
2811 switch (op & 0xc0)
2812 {
2813 case DW_CFA_advance_loc:
2814 {
2815 ULONG_PTR offset = (op & 0x3f) * info->code_align;
2816 TRACE("%lx: DW_CFA_advance_loc %lu\n", info->ip, offset);
2817 info->ip += offset;
2818 break;
2819 }
2820 case DW_CFA_offset:
2821 {
2822 ULONG_PTR reg = op & 0x3f;
2823 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
2824 if (!valid_reg(reg)) break;
2825 TRACE("%lx: DW_CFA_offset %s, %ld\n",
2826 info->ip,
2827 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2828 offset);
2829 info->state.regs[reg] = offset;
2830 info->state.rules[reg] = RULE_CFA_OFFSET;
2831 break;
2832 }
2833 case DW_CFA_restore:
2834 {
2835 ULONG_PTR reg = op & 0x3f;
2836 if (!valid_reg(reg)) break;
2837 TRACE("%lx: DW_CFA_restore %s\n",
2838 info->ip,
2839 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2840 info->state.rules[reg] = RULE_UNSET;
2841 break;
2842 }
2843 }
2844 }
2845 else switch (op)
2846 {
2847 case DW_CFA_nop:
2848 break;
2849 case DW_CFA_set_loc:
2850 {
2851 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding);
2852 TRACE("%lx: DW_CFA_set_loc %lx\n", info->ip, loc);
2853 info->ip = loc;
2854 break;
2855 }
2856 case DW_CFA_advance_loc1:
2857 {
2858 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
2859 TRACE("%lx: DW_CFA_advance_loc1 %lu\n", info->ip, offset);
2860 info->ip += offset;
2861 break;
2862 }
2863 case DW_CFA_advance_loc2:
2864 {
2865 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
2866 TRACE("%lx: DW_CFA_advance_loc2 %lu\n", info->ip, offset);
2867 info->ip += offset;
2868 break;
2869 }
2870 case DW_CFA_advance_loc4:
2871 {
2872 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
2873 TRACE("%lx: DW_CFA_advance_loc4 %lu\n", info->ip, offset);
2874 info->ip += offset;
2875 break;
2876 }
2877 case DW_CFA_offset_extended:
2878 case DW_CFA_offset_extended_sf:
2879 {
2880 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2881 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
2882 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2883 if (!valid_reg(reg)) break;
2884 TRACE("%lx: DW_CFA_offset_extended %s, %ld\n",
2885 info->ip,
2886 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2887 offset);
2888 info->state.regs[reg] = offset;
2889 info->state.rules[reg] = RULE_CFA_OFFSET;
2890 break;
2891 }
2892 case DW_CFA_restore_extended:
2893 {
2894 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2895 if (!valid_reg(reg)) break;
2896 TRACE("%lx: DW_CFA_restore_extended %s\n",
2897 info->ip,
2898 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2899 info->state.rules[reg] = RULE_UNSET;
2900 break;
2901 }
2902 case DW_CFA_undefined:
2903 {
2904 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2905 if (!valid_reg(reg)) break;
2906 TRACE("%lx: DW_CFA_undefined %s\n",
2907 info->ip,
2908 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2909 info->state.rules[reg] = RULE_UNDEFINED;
2910 break;
2911 }
2912 case DW_CFA_same_value:
2913 {
2914 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2915 if (!valid_reg(reg)) break;
2916 TRACE("%lx: DW_CFA_same_value %s\n",
2917 info->ip,
2918 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2919 info->state.regs[reg] = reg;
2920 info->state.rules[reg] = RULE_SAME;
2921 break;
2922 }
2923 case DW_CFA_register:
2924 {
2925 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2926 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
2927 if (!valid_reg(reg) || !valid_reg(reg2)) break;
2928 TRACE("%lx: DW_CFA_register %s == %s\n",
2929 info->ip,
2930 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2931 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg2, TRUE)));
2932 info->state.regs[reg] = reg2;
2933 info->state.rules[reg] = RULE_OTHER_REG;
2934 break;
2935 }
2936 case DW_CFA_remember_state:
2937 TRACE("%lx: DW_CFA_remember_state\n", info->ip);
2938 if (info->state_sp >= MAX_SAVED_STATES)
2939 FIXME("%lx: DW_CFA_remember_state too many nested saves\n", info->ip);
2940 else
2941 info->state_stack[info->state_sp++] = info->state;
2942 break;
2943 case DW_CFA_restore_state:
2944 TRACE("%lx: DW_CFA_restore_state\n", info->ip);
2945 if (!info->state_sp)
2946 FIXME("%lx: DW_CFA_restore_state without corresponding save\n", info->ip);
2947 else
2948 info->state = info->state_stack[--info->state_sp];
2949 break;
2950 case DW_CFA_def_cfa:
2951 case DW_CFA_def_cfa_sf:
2952 {
2953 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2954 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
2955 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2956 if (!valid_reg(reg)) break;
2957 TRACE("%lx: DW_CFA_def_cfa %s, %ld\n",
2958 info->ip,
2959 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
2960 offset);
2961 info->state.cfa_reg = reg;
2962 info->state.cfa_offset = offset;
2963 info->state.cfa_rule = RULE_CFA_OFFSET;
2964 break;
2965 }
2966 case DW_CFA_def_cfa_register:
2967 {
2968 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
2969 if (!valid_reg(reg)) break;
2970 TRACE("%lx: DW_CFA_def_cfa_register %s\n",
2971 info->ip,
2972 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)));
2973 info->state.cfa_reg = reg;
2974 info->state.cfa_rule = RULE_CFA_OFFSET;
2975 break;
2976 }
2977 case DW_CFA_def_cfa_offset:
2978 case DW_CFA_def_cfa_offset_sf:
2979 {
2980 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
2981 : dwarf2_leb128_as_signed(ctx) * info->data_align;
2982 TRACE("%lx: DW_CFA_def_cfa_offset %ld\n", info->ip, offset);
2983 info->state.cfa_offset = offset;
2984 info->state.cfa_rule = RULE_CFA_OFFSET;
2985 break;
2986 }
2987 case DW_CFA_def_cfa_expression:
2988 {
2989 ULONG_PTR expr = (ULONG_PTR)ctx->data;
2990 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
2991 TRACE("%lx: DW_CFA_def_cfa_expression %lx-%lx\n", info->ip, expr, expr+len);
2992 info->state.cfa_offset = expr;
2993 info->state.cfa_rule = RULE_VAL_EXPRESSION;
2994 ctx->data += len;
2995 break;
2996 }
2997 case DW_CFA_expression:
2998 case DW_CFA_val_expression:
2999 {
3000 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3001 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3002 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3003 if (!valid_reg(reg)) break;
3004 TRACE("%lx: DW_CFA_%sexpression %s %lx-%lx\n",
3005 info->ip, (op == DW_CFA_expression) ? "" : "val_",
3006 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(reg, TRUE)),
3007 expr, expr + len);
3008 info->state.regs[reg] = expr;
3009 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
3010 ctx->data += len;
3011 break;
3012 }
3013 case DW_CFA_GNU_args_size:
3014 /* FIXME: should check that GCC is the compiler for this CU */
3015 {
3016 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
3017 TRACE("%lx: DW_CFA_GNU_args_size %lu\n", info->ip, args);
3018 /* ignored */
3019 break;
3020 }
3021 default:
3022 FIXME("%lx: unknown CFA opcode %02x\n", info->ip, op);
3023 break;
3024 }
3025 }
3026 }
3027
3028 /* retrieve a context register from its dwarf number */
3029 static ULONG_PTR get_context_reg(CONTEXT *context, ULONG_PTR dw_reg)
3030 {
3031 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg, TRUE), sz;
3032 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
3033
3034 if (sz != sizeof(ULONG_PTR))
3035 {
3036 FIXME("reading register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
3037 return 0;
3038 }
3039 return *ptr;
3040 }
3041
3042 /* set a context register from its dwarf number */
3043 static void set_context_reg(struct cpu_stack_walk* csw, CONTEXT *context, ULONG_PTR dw_reg,
3044 ULONG_PTR val, BOOL isdebuggee)
3045 {
3046 unsigned regno = dbghelp_current_cpu->map_dwarf_register(dw_reg, TRUE), sz;
3047 ULONG_PTR* ptr = dbghelp_current_cpu->fetch_context_reg(context, regno, &sz);
3048
3049 if (isdebuggee)
3050 {
3051 char tmp[16];
3052
3053 if (sz > sizeof(tmp))
3054 {
3055 FIXME("register %lu/%u size is too wide: %u\n", dw_reg, regno, sz);
3056 return;
3057 }
3058 if (!sw_read_mem(csw, val, tmp, sz))
3059 {
3060 WARN("Couldn't read memory at %p\n", (void*)val);
3061 return;
3062 }
3063 memcpy(ptr, tmp, sz);
3064 }
3065 else
3066 {
3067 if (sz != sizeof(ULONG_PTR))
3068 {
3069 FIXME("assigning to register %lu/%u of wrong size %u\n", dw_reg, regno, sz);
3070 return;
3071 }
3072 *ptr = val;
3073 }
3074 }
3075
3076 /* copy a register from one context to another using dwarf number */
3077 static void copy_context_reg(CONTEXT *dstcontext, ULONG_PTR dwregdst, CONTEXT* srccontext, ULONG_PTR dwregsrc)
3078 {
3079 unsigned regdstno = dbghelp_current_cpu->map_dwarf_register(dwregdst, TRUE), szdst;
3080 unsigned regsrcno = dbghelp_current_cpu->map_dwarf_register(dwregsrc, TRUE), szsrc;
3081 ULONG_PTR* ptrdst = dbghelp_current_cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3082 ULONG_PTR* ptrsrc = dbghelp_current_cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3083
3084 if (szdst != szsrc)
3085 {
3086 FIXME("Cannot copy register %lu/%u => %lu/%u because of size mismatch (%u => %u)\n",
3087 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3088 return;
3089 }
3090 memcpy(ptrdst, ptrsrc, szdst);
3091 }
3092
3093 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3094 const unsigned char* zp, CONTEXT *context)
3095 {
3096 dwarf2_traverse_context_t ctx;
3097 ULONG_PTR reg, sz, tmp, stack[64];
3098 int sp = -1;
3099 ULONG_PTR len;
3100
3101 ctx.data = zp;
3102 ctx.end_data = zp + 4;
3103 len = dwarf2_leb128_as_unsigned(&ctx);
3104 ctx.end_data = ctx.data + len;
3105 ctx.word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3106
3107 while (ctx.data < ctx.end_data)
3108 {
3109 unsigned char opcode = dwarf2_parse_byte(&ctx);
3110
3111 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3112 stack[++sp] = opcode - DW_OP_lit0;
3113 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3114 stack[++sp] = get_context_reg(context, opcode - DW_OP_reg0);
3115 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3116 stack[++sp] = get_context_reg(context, opcode - DW_OP_breg0) + dwarf2_leb128_as_signed(&ctx);
3117 else switch (opcode)
3118 {
3119 case DW_OP_nop: break;
3120 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx); break;
3121 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3122 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3123 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3124 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3125 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3126 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3127 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3128 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3129 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3130 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3131 case DW_OP_deref:
3132 if (!sw_read_mem(csw, stack[sp], &tmp, sizeof(tmp)))
3133 {
3134 ERR("Couldn't read memory at %lx\n", stack[sp]);
3135 tmp = 0;
3136 }
3137 stack[sp] = tmp;
3138 break;
3139 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3140 case DW_OP_drop: sp--; break;
3141 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3142 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3143 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3144 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3145 case DW_OP_abs: stack[sp] = labs(stack[sp]); break;
3146 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3147 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3148 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3149 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3150 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3151 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3152 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3153 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3154 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3155 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3156 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3157 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3158 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3159 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3160 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3161 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3162 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3163 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3164 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3165 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3166 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3167 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3168 case DW_OP_GNU_encoded_addr:
3169 tmp = dwarf2_parse_byte(&ctx);
3170 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp);
3171 break;
3172 case DW_OP_regx:
3173 stack[++sp] = get_context_reg(context, dwarf2_leb128_as_unsigned(&ctx));
3174 break;
3175 case DW_OP_bregx:
3176 reg = dwarf2_leb128_as_unsigned(&ctx);
3177 tmp = dwarf2_leb128_as_signed(&ctx);
3178 stack[++sp] = get_context_reg(context, reg) + tmp;
3179 break;
3180 case DW_OP_deref_size:
3181 sz = dwarf2_parse_byte(&ctx);
3182 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3183 {
3184 ERR("Couldn't read memory at %lx\n", stack[sp]);
3185 tmp = 0;
3186 }
3187 /* do integral promotion */
3188 switch (sz)
3189 {
3190 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3191 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3192 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3193 case 8: stack[sp] = *(ULONG_PTR*)&tmp; break; /* FIXME: won't work on 32bit platform */
3194 default: FIXME("Unknown size for deref 0x%lx\n", sz);
3195 }
3196 break;
3197 default:
3198 FIXME("unhandled opcode %02x\n", opcode);
3199 }
3200 }
3201 return stack[sp];
3202 }
3203
3204 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3205 CONTEXT *context, struct frame_state *state, ULONG_PTR* cfa)
3206 {
3207 unsigned int i;
3208 ULONG_PTR value;
3209 CONTEXT new_context = *context;
3210
3211 switch (state->cfa_rule)
3212 {
3213 case RULE_EXPRESSION:
3214 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3215 if (!sw_read_mem(csw, *cfa, cfa, sizeof(*cfa)))
3216 {
3217 WARN("Couldn't read memory at %p\n", (void*)*cfa);
3218 return;
3219 }
3220 break;
3221 case RULE_VAL_EXPRESSION:
3222 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3223 break;
3224 default:
3225 *cfa = get_context_reg(context, state->cfa_reg) + state->cfa_offset;
3226 break;
3227 }
3228 if (!*cfa) return;
3229
3230 for (i = 0; i < NB_FRAME_REGS; i++)
3231 {
3232 switch (state->rules[i])
3233 {
3234 case RULE_UNSET:
3235 case RULE_UNDEFINED:
3236 case RULE_SAME:
3237 break;
3238 case RULE_CFA_OFFSET:
3239 set_context_reg(csw, &new_context, i, *cfa + state->regs[i], TRUE);
3240 break;
3241 case RULE_OTHER_REG:
3242 copy_context_reg(&new_context, i, context, state->regs[i]);
3243 break;
3244 case RULE_EXPRESSION:
3245 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3246 set_context_reg(csw, &new_context, i, value, TRUE);
3247 break;
3248 case RULE_VAL_EXPRESSION:
3249 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3250 set_context_reg(csw, &new_context, i, value, FALSE);
3251 break;
3252 }
3253 }
3254 *context = new_context;
3255 }
3256
3257 /***********************************************************************
3258 * dwarf2_virtual_unwind
3259 *
3260 */
3261 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk* csw, ULONG_PTR ip, CONTEXT* context, ULONG_PTR* cfa)
3262 {
3263 struct module_pair pair;
3264 struct frame_info info;
3265 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3266 struct module_format* modfmt;
3267 const unsigned char* end;
3268 DWORD_PTR delta;
3269
3270 if (!(pair.pcs = process_find_by_handle(csw->hProcess)) ||
3271 !(pair.requested = module_find_by_addr(pair.pcs, ip, DMT_UNKNOWN)) ||
3272 !module_get_debug(&pair))
3273 return FALSE;
3274 modfmt = pair.effective->format_info[DFI_DWARF];
3275 if (!modfmt) return FALSE;
3276 memset(&info, 0, sizeof(info));
3277 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3278 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3279 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3280 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3281 * in this process the IMAGE section at a different address as the one expected by
3282 * the image
3283 */
3284 delta = pair.effective->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3285 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3286 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, TRUE))
3287 {
3288 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3289 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3290 fde_ctx.word_size = modfmt->u.dwarf2_info->word_size;
3291 delta = pair.effective->reloc_delta;
3292 if (!dwarf2_get_cie(ip, pair.effective, delta, &fde_ctx, &cie_ctx, &info, FALSE))
3293 {
3294 TRACE("Couldn't find information for %lx\n", ip);
3295 return FALSE;
3296 }
3297 }
3298
3299 TRACE("function %lx/%lx code_align %lu data_align %ld retaddr %s\n",
3300 ip, info.ip, info.code_align, info.data_align,
3301 dbghelp_current_cpu->fetch_regname(dbghelp_current_cpu->map_dwarf_register(info.retaddr_reg, TRUE)));
3302
3303 /* if at very beginning of function, return and use default unwinder */
3304 if (ip == info.ip) return FALSE;
3305 execute_cfa_instructions(&cie_ctx, ip, &info);
3306
3307 if (info.aug_z_format) /* get length of augmentation data */
3308 {
3309 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3310 end = fde_ctx.data + len;
3311 }
3312 else end = NULL;
3313 dwarf2_parse_augmentation_ptr(&fde_ctx, info.lsda_encoding); /* handler_data */
3314 if (end) fde_ctx.data = end;
3315
3316 execute_cfa_instructions(&fde_ctx, ip, &info);
3317
3318 /* if there is no information about retaddr, use default unwinder */
3319 if (info.state.rules[info.retaddr_reg] == RULE_UNSET) return FALSE;
3320
3321 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3322
3323 return TRUE;
3324 }
3325
3326 static void dwarf2_location_compute(struct process* pcs,
3327 const struct module_format* modfmt,
3328 const struct symt_function* func,
3329 struct location* loc)
3330 {
3331 struct location frame;
3332 DWORD_PTR ip;
3333 int err;
3334 dwarf2_traverse_context_t lctx;
3335
3336 if (!func->container || func->container->tag != SymTagCompiland)
3337 {
3338 WARN("We'd expect function %s's container to exist and be a compiland\n", func->hash_elt.name);
3339 err = loc_err_internal;
3340 }
3341 else
3342 {
3343 /* instruction pointer relative to compiland's start */
3344 ip = pcs->ctx_frame.InstructionOffset - ((struct symt_compiland*)func->container)->address;
3345
3346 if ((err = loc_compute_frame(pcs, modfmt, func, ip, &frame)) == 0)
3347 {
3348 switch (loc->kind)
3349 {
3350 case loc_dwarf2_location_list:
3351 /* Then, if the variable has a location list, find it !! */
3352 if (dwarf2_lookup_loclist(modfmt,
3353 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3354 ip, &lctx))
3355 goto do_compute;
3356 err = loc_err_out_of_scope;
3357 break;
3358 case loc_dwarf2_block:
3359 /* or if we have a copy of an existing block, get ready for it */
3360 {
3361 unsigned* ptr = (unsigned*)loc->offset;
3362
3363 lctx.data = (const BYTE*)(ptr + 1);
3364 lctx.end_data = lctx.data + *ptr;
3365 lctx.word_size = modfmt->u.dwarf2_info->word_size;
3366 }
3367 do_compute:
3368 /* now get the variable */
3369 err = compute_location(&lctx, loc, pcs->handle, &frame);
3370 break;
3371 case loc_register:
3372 case loc_regrel:
3373 /* nothing to do */
3374 break;
3375 default:
3376 WARN("Unsupported local kind %d\n", loc->kind);
3377 err = loc_err_internal;
3378 }
3379 }
3380 }
3381 if (err < 0)
3382 {
3383 loc->kind = loc_register;
3384 loc->reg = err;
3385 }
3386 }
3387
3388 #ifdef HAVE_ZLIB
3389 static void *zalloc(void *priv, uInt items, uInt sz)
3390 {
3391 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3392 }
3393
3394 static void zfree(void *priv, void *addr)
3395 {
3396 HeapFree(GetProcessHeap(), 0, addr);
3397 }
3398
3399 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3400 const char* zsectname,
3401 struct image_section_map* ism)
3402 {
3403 z_stream z;
3404 LARGE_INTEGER li;
3405 int res;
3406 BOOL ret = FALSE;
3407
3408 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3409 size_t sz = image_get_map_size(ism);
3410
3411 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
3412 {
3413 ERR("invalid compressed section %s\n", zsectname);
3414 goto out;
3415 }
3416
3417 #ifdef WORDS_BIGENDIAN
3418 li.u.HighPart = *(DWORD*)&sect[4];
3419 li.u.LowPart = *(DWORD*)&sect[8];
3420 #else
3421 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
3422 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
3423 #endif
3424
3425 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
3426 if (!addr)
3427 goto out;
3428
3429 z.next_in = &sect[12];
3430 z.avail_in = sz - 12;
3431 z.opaque = NULL;
3432 z.zalloc = zalloc;
3433 z.zfree = zfree;
3434
3435 res = inflateInit(&z);
3436 if (res != Z_OK)
3437 {
3438 FIXME("inflateInit failed with %i / %s\n", res, z.msg);
3439 goto out_free;
3440 }
3441
3442 do {
3443 z.next_out = addr + z.total_out;
3444 z.avail_out = li.QuadPart - z.total_out;
3445 res = inflate(&z, Z_FINISH);
3446 } while (z.avail_in && res == Z_STREAM_END);
3447
3448 if