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