dbghelp: Fix container for global variables (Dwarf).
[wine.git] / dlls / dbghelp / dwarf.c
blob13931c45d8b3a26805166e6e47b5f7c255726208
1 /*
2 * File dwarf.c - read dwarf2 information from the ELF modules
4 * Copyright (C) 2005, Raphael Junqueira
5 * Copyright (C) 2006-2011, Eric Pouech
6 * Copyright (C) 2010, Alexandre Julliard
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.
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.
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
23 #define NONAMELESSUNION
25 #include <sys/types.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <stdio.h>
29 #include <assert.h>
30 #include <stdarg.h>
31 #include <zlib.h>
33 #include "windef.h"
34 #include "winternl.h"
35 #include "winbase.h"
36 #include "winuser.h"
37 #include "ole2.h"
38 #include "oleauto.h"
40 #include "dbghelp_private.h"
41 #include "image_private.h"
43 #include "wine/debug.h"
45 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
47 /* FIXME:
48 * - Functions:
49 * o unspecified parameters
50 * o inlined functions
51 * o Debug{Start|End}Point
52 * o CFA
53 * - Udt
54 * o proper types loading (nesting)
57 #if 0
58 static void dump(const void* ptr, unsigned len)
60 int i, j;
61 BYTE msg[128];
62 static const char hexof[] = "0123456789abcdef";
63 const BYTE* x = ptr;
65 for (i = 0; i < len; i += 16)
67 sprintf(msg, "%08x: ", i);
68 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
69 for (j = 0; j < min(16, len - i); j++)
71 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
72 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
73 msg[10 + 3 * j + 2] = ' ';
74 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
75 x[i + j] : '.';
77 msg[10 + 3 * 16] = ' ';
78 msg[10 + 3 * 16 + 1 + 16] = '\0';
79 TRACE("%s\n", msg);
82 #endif
84 /**
86 * Main Specs:
87 * http://www.eagercon.com/dwarf/dwarf3std.htm
88 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
90 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
92 * example of projects who do dwarf2 parsing:
93 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
94 * http://elis.ugent.be/diota/log/ltrace_elf.c
96 #include "dwarf.h"
98 /**
99 * Parsers
102 typedef struct dwarf2_abbrev_entry_attr_s
104 ULONG_PTR attribute;
105 ULONG_PTR form;
106 struct dwarf2_abbrev_entry_attr_s* next;
107 } dwarf2_abbrev_entry_attr_t;
109 typedef struct dwarf2_abbrev_entry_s
111 ULONG_PTR entry_code;
112 ULONG_PTR tag;
113 unsigned char have_child;
114 unsigned num_attr;
115 dwarf2_abbrev_entry_attr_t* attrs;
116 } dwarf2_abbrev_entry_t;
118 struct dwarf2_block
120 unsigned size;
121 const unsigned char* ptr;
124 struct attribute
126 ULONG_PTR form;
127 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
128 union
130 ULONG_PTR uvalue;
131 ULONGLONG lluvalue;
132 LONG_PTR svalue;
133 const char* string;
134 struct dwarf2_block block;
135 } u;
136 const struct dwarf2_debug_info_s* debug_info;
139 typedef struct dwarf2_debug_info_s
141 const dwarf2_abbrev_entry_t*abbrev;
142 struct symt* symt;
143 const unsigned char** data;
144 struct vector children;
145 struct dwarf2_debug_info_s* parent;
146 struct dwarf2_parse_context_s* unit_ctx;
147 } dwarf2_debug_info_t;
149 typedef struct dwarf2_section_s
151 BOOL compressed;
152 const unsigned char* address;
153 unsigned size;
154 DWORD_PTR rva;
155 } dwarf2_section_t;
157 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
159 typedef struct dwarf2_traverse_context_s
161 const unsigned char* data;
162 const unsigned char* end_data;
163 } dwarf2_traverse_context_t;
165 typedef struct dwarf2_cuhead_s
167 unsigned char word_size; /* size of a word on target machine */
168 unsigned char version;
169 unsigned char offset_size; /* size of offset inside DWARF */
170 } dwarf2_cuhead_t;
172 typedef struct dwarf2_parse_module_context_s
174 ULONG_PTR load_offset;
175 const dwarf2_section_t* sections;
176 struct module* module;
177 const struct elf_thunk_area*thunks;
178 struct vector unit_contexts;
179 struct dwarf2_dwz_alternate_s* dwz;
180 DWORD cu_versions;
181 } dwarf2_parse_module_context_t;
183 typedef struct dwarf2_dwz_alternate_s
185 struct image_file_map* fmap;
186 dwarf2_section_t sections[section_max];
187 struct image_section_map sectmap[section_max];
188 dwarf2_parse_module_context_t module_ctx;
189 } dwarf2_dwz_alternate_t;
191 enum unit_status
193 UNIT_ERROR,
194 UNIT_NOTLOADED,
195 UNIT_LOADED,
196 UNIT_LOADED_FAIL,
197 UNIT_BEINGLOADED,
200 /* this is the context used for parsing a compilation unit
201 * inside an ELF/PE section (likely .debug_info)
203 typedef struct dwarf2_parse_context_s
205 dwarf2_parse_module_context_t* module_ctx;
206 unsigned section;
207 struct pool pool;
208 struct symt_compiland* compiland;
209 struct sparse_array abbrev_table;
210 struct sparse_array debug_info_table;
211 ULONG_PTR ref_offset;
212 char* cpp_name;
213 dwarf2_cuhead_t head;
214 enum unit_status status;
215 dwarf2_traverse_context_t traverse_DIE;
216 unsigned language;
217 } dwarf2_parse_context_t;
219 /* stored in the dbghelp's module internal structure for later reuse */
220 struct dwarf2_module_info_s
222 dwarf2_cuhead_t** cuheads;
223 unsigned num_cuheads;
224 dwarf2_section_t debug_loc;
225 dwarf2_section_t debug_frame;
226 dwarf2_section_t eh_frame;
227 unsigned char word_size;
230 #define loc_dwarf2_location_list (loc_user + 0)
231 #define loc_dwarf2_block (loc_user + 1)
232 #define loc_dwarf2_frame_cfa (loc_user + 2)
234 /* forward declarations */
235 static struct symt* dwarf2_parse_enumeration_type(dwarf2_debug_info_t* entry);
236 static BOOL dwarf2_parse_compilation_unit(dwarf2_parse_context_t* ctx);
237 static dwarf2_parse_context_t* dwarf2_locate_cu(dwarf2_parse_module_context_t* module_ctx, ULONG_PTR ref);
239 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
241 return *ptr;
244 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
246 unsigned char uvalue = dwarf2_get_byte(ctx->data);
247 ctx->data += 1;
248 return uvalue;
251 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
253 return *(const UINT16*)ptr;
256 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
258 unsigned short uvalue = dwarf2_get_u2(ctx->data);
259 ctx->data += 2;
260 return uvalue;
263 static ULONG_PTR dwarf2_get_u4(const unsigned char* ptr)
265 return *(const UINT32*)ptr;
268 static ULONG_PTR dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
270 ULONG_PTR uvalue = dwarf2_get_u4(ctx->data);
271 ctx->data += 4;
272 return uvalue;
275 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
277 return *(const UINT64*)ptr;
280 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
282 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
283 ctx->data += 8;
284 return uvalue;
287 static ULONG_PTR dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
289 ULONG_PTR ret = 0;
290 unsigned char byte;
291 unsigned shift = 0;
295 byte = dwarf2_get_byte(ptr++);
296 ret |= (byte & 0x7f) << shift;
297 shift += 7;
298 } while (byte & 0x80);
300 if (end) *end = ptr;
301 return ret;
304 static ULONG_PTR dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
306 ULONG_PTR ret;
308 assert(ctx);
310 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
312 return ret;
315 static LONG_PTR dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
317 LONG_PTR ret = 0;
318 unsigned char byte;
319 unsigned shift = 0;
320 const unsigned size = sizeof(int) * 8;
324 byte = dwarf2_get_byte(ptr++);
325 ret |= (byte & 0x7f) << shift;
326 shift += 7;
327 } while (byte & 0x80);
328 if (end) *end = ptr;
330 /* as spec: sign bit of byte is 2nd high order bit (80x40)
331 * -> 0x80 is used as flag.
333 if ((shift < size) && (byte & 0x40))
335 ret |= - (1 << shift);
337 return ret;
340 static LONG_PTR dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
342 LONG_PTR ret = 0;
344 assert(ctx);
346 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
347 return ret;
350 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
352 unsigned ret;
353 for (ret = 0; ctx->data[ret] & 0x80; ret++);
354 return ret + 1;
357 /******************************************************************
358 * dwarf2_get_addr
360 * Returns an address.
361 * We assume that in all cases word size from Dwarf matches the size of
362 * addresses in platform where the exec is compiled.
364 static ULONG_PTR dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
366 ULONG_PTR ret;
368 switch (word_size)
370 case 4:
371 ret = dwarf2_get_u4(ptr);
372 break;
373 case 8:
374 ret = dwarf2_get_u8(ptr);
375 break;
376 default:
377 FIXME("Unsupported Word Size %u\n", word_size);
378 ret = 0;
380 return ret;
383 static inline ULONG_PTR dwarf2_parse_addr(dwarf2_traverse_context_t* ctx, unsigned word_size)
385 ULONG_PTR ret = dwarf2_get_addr(ctx->data, word_size);
386 ctx->data += word_size;
387 return ret;
390 static inline ULONG_PTR dwarf2_parse_addr_head(dwarf2_traverse_context_t* ctx, const dwarf2_cuhead_t* head)
392 return dwarf2_parse_addr(ctx, head->word_size);
395 static ULONG_PTR dwarf2_parse_offset(dwarf2_traverse_context_t* ctx, unsigned char offset_size)
397 ULONG_PTR ret = dwarf2_get_addr(ctx->data, offset_size);
398 ctx->data += offset_size;
399 return ret;
402 static ULONG_PTR dwarf2_parse_3264(dwarf2_traverse_context_t* ctx, unsigned char* ofsz)
404 ULONG_PTR ret = dwarf2_parse_u4(ctx);
405 if (ret == 0xffffffff)
407 ret = dwarf2_parse_u8(ctx);
408 *ofsz = 8;
410 else *ofsz = 4;
411 return ret;
414 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
416 return wine_dbg_sprintf("ctx(%p)", ctx->data);
419 static const char* dwarf2_debug_unit_ctx(const dwarf2_parse_context_t* ctx)
421 return wine_dbg_sprintf("ctx(%p,%s)",
422 ctx, debugstr_w(ctx->module_ctx->module->modulename));
425 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
427 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p) in %s",
428 di->abbrev, di->symt, dwarf2_debug_unit_ctx(di->unit_ctx));
431 static dwarf2_abbrev_entry_t*
432 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
433 ULONG_PTR entry_code)
435 assert( NULL != abbrev_table );
436 return sparse_array_find(abbrev_table, entry_code);
439 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
440 struct sparse_array* abbrev_table,
441 struct pool* pool)
443 ULONG_PTR entry_code;
444 dwarf2_abbrev_entry_t* abbrev_entry;
445 dwarf2_abbrev_entry_attr_t* new = NULL;
446 dwarf2_abbrev_entry_attr_t* last = NULL;
447 ULONG_PTR attribute;
448 ULONG_PTR form;
450 assert( NULL != abbrev_ctx );
452 TRACE("%s, end at %p\n",
453 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
455 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
456 while (abbrev_ctx->data < abbrev_ctx->end_data)
458 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
459 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
460 TRACE("found entry_code %Iu\n", entry_code);
461 if (!entry_code)
463 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
464 break;
466 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
467 assert( NULL != abbrev_entry );
469 abbrev_entry->entry_code = entry_code;
470 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
471 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
472 abbrev_entry->attrs = NULL;
473 abbrev_entry->num_attr = 0;
475 TRACE("table:(%p,#%u) entry_code(%Iu) tag(0x%Ix) have_child(%u) -> %p\n",
476 abbrev_table, sparse_array_length(abbrev_table),
477 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
479 last = NULL;
480 while (1)
482 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
483 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
484 if (!attribute) break;
486 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
487 assert(new);
489 new->attribute = attribute;
490 new->form = form;
491 new->next = NULL;
492 if (abbrev_entry->attrs) last->next = new;
493 else abbrev_entry->attrs = new;
494 last = new;
495 abbrev_entry->num_attr++;
498 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
501 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
502 const dwarf2_cuhead_t* head,
503 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
505 unsigned step;
507 TRACE("(attr:0x%Ix,form:0x%Ix)\n", abbrev_attr->attribute, abbrev_attr->form);
509 switch (abbrev_attr->form)
511 case DW_FORM_flag_present: step = 0; break;
512 case DW_FORM_ref_addr: step = (head->version >= 3) ? head->offset_size : head->word_size; break;
513 case DW_FORM_addr: step = head->word_size; break;
514 case DW_FORM_flag:
515 case DW_FORM_data1:
516 case DW_FORM_ref1: step = 1; break;
517 case DW_FORM_data2:
518 case DW_FORM_ref2: step = 2; break;
519 case DW_FORM_data4:
520 case DW_FORM_ref4: step = 4; break;
521 case DW_FORM_data8:
522 case DW_FORM_ref8: step = 8; break;
523 case DW_FORM_sdata:
524 case DW_FORM_ref_udata:
525 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
526 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
527 case DW_FORM_exprloc:
528 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
529 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
530 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
531 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
532 case DW_FORM_sec_offset:
533 case DW_FORM_GNU_ref_alt:
534 case DW_FORM_GNU_strp_alt:
535 case DW_FORM_strp: step = head->offset_size; break;
536 default:
537 FIXME("Unhandled attribute form %Ix\n", abbrev_attr->form);
538 return;
540 ctx->data += step;
543 static BOOL dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
544 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
545 const unsigned char* data,
546 struct attribute* attr)
548 attr->form = abbrev_attr->form;
549 switch (attr->form)
551 case DW_FORM_ref_addr:
552 if (ctx->head.version >= 3)
553 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.offset_size);
554 else
555 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.word_size);
556 TRACE("addr<0x%Ix>\n", attr->u.uvalue);
557 break;
559 case DW_FORM_addr:
560 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.word_size);
561 TRACE("addr<0x%Ix>\n", attr->u.uvalue);
562 break;
564 case DW_FORM_flag:
565 attr->u.uvalue = dwarf2_get_byte(data);
566 TRACE("flag<0x%Ix>\n", attr->u.uvalue);
567 break;
569 case DW_FORM_flag_present:
570 attr->u.uvalue = 1;
571 TRACE("flag_present\n");
572 break;
574 case DW_FORM_data1:
575 attr->u.uvalue = dwarf2_get_byte(data);
576 TRACE("data1<%Iu>\n", attr->u.uvalue);
577 break;
579 case DW_FORM_data2:
580 attr->u.uvalue = dwarf2_get_u2(data);
581 TRACE("data2<%Iu>\n", attr->u.uvalue);
582 break;
584 case DW_FORM_data4:
585 attr->u.uvalue = dwarf2_get_u4(data);
586 TRACE("data4<%Iu>\n", attr->u.uvalue);
587 break;
589 case DW_FORM_data8:
590 attr->u.lluvalue = dwarf2_get_u8(data);
591 TRACE("data8<%s>\n", wine_dbgstr_longlong(attr->u.uvalue));
592 break;
594 case DW_FORM_ref1:
595 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
596 TRACE("ref1<0x%Ix>\n", attr->u.uvalue);
597 break;
599 case DW_FORM_ref2:
600 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
601 TRACE("ref2<0x%Ix>\n", attr->u.uvalue);
602 break;
604 case DW_FORM_ref4:
605 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
606 TRACE("ref4<0x%Ix>\n", attr->u.uvalue);
607 break;
609 case DW_FORM_ref8:
610 FIXME("Unhandled 64-bit support\n");
611 break;
613 case DW_FORM_sdata:
614 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
615 break;
617 case DW_FORM_ref_udata:
618 attr->u.uvalue = ctx->ref_offset + dwarf2_get_leb128_as_unsigned(data, NULL);
619 TRACE("ref_udata<0x%Ix>\n", attr->u.uvalue);
620 break;
622 case DW_FORM_udata:
623 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
624 TRACE("udata<0x%Ix>\n", attr->u.uvalue);
625 break;
627 case DW_FORM_string:
628 attr->u.string = (const char *)data;
629 TRACE("string<%s>\n", debugstr_a(attr->u.string));
630 break;
632 case DW_FORM_strp:
634 ULONG_PTR ofs = dwarf2_get_addr(data, ctx->head.offset_size);
635 if (ofs >= ctx->module_ctx->sections[section_string].size)
637 ERR("Out of bounds string offset (%08Ix)\n", ofs);
638 attr->u.string = "<<outofbounds-strp>>";
640 else
642 attr->u.string = (const char*)ctx->module_ctx->sections[section_string].address + ofs;
643 TRACE("strp<%s>\n", debugstr_a(attr->u.string));
646 break;
648 case DW_FORM_block:
649 case DW_FORM_exprloc:
650 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
651 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
652 break;
654 case DW_FORM_block1:
655 attr->u.block.size = dwarf2_get_byte(data);
656 attr->u.block.ptr = data + 1;
657 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
658 break;
660 case DW_FORM_block2:
661 attr->u.block.size = dwarf2_get_u2(data);
662 attr->u.block.ptr = data + 2;
663 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
664 break;
666 case DW_FORM_block4:
667 attr->u.block.size = dwarf2_get_u4(data);
668 attr->u.block.ptr = data + 4;
669 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
670 break;
672 case DW_FORM_sec_offset:
673 attr->u.lluvalue = dwarf2_get_addr(data, ctx->head.offset_size);
674 TRACE("sec_offset<%s>\n", wine_dbgstr_longlong(attr->u.lluvalue));
675 break;
677 case DW_FORM_GNU_ref_alt:
678 if (!ctx->module_ctx->dwz)
680 ERR("No DWZ file present for GNU_ref_alt in %s\n", debugstr_w(ctx->module_ctx->module->modulename));
681 attr->u.uvalue = 0;
682 return FALSE;
684 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.offset_size);
685 TRACE("ref_alt<0x%Ix>\n", attr->u.uvalue);
686 break;
688 case DW_FORM_GNU_strp_alt:
689 if (ctx->module_ctx->dwz)
691 ULONG_PTR ofs = dwarf2_get_addr(data, ctx->head.offset_size);
692 if (ofs < ctx->module_ctx->dwz->sections[section_string].size)
694 attr->u.string = (const char*)ctx->module_ctx->dwz->sections[section_string].address + ofs;
695 TRACE("strp_alt<%s>\n", debugstr_a(attr->u.string));
697 else
699 ERR("out of bounds strp_alt: 0x%Ix 0x%x (%u)\n", ofs, ctx->module_ctx->dwz->sections[section_string].size, ctx->head.offset_size);
700 attr->u.string = "<<outofbounds-strpalt>>";
703 else
705 ERR("No DWZ file present for GNU_strp_alt in %s\n", debugstr_w(ctx->module_ctx->module->modulename));
706 attr->u.string = "<<noDWZ-strpalt>>";
708 break;
710 default:
711 FIXME("Unhandled attribute form %Ix\n", abbrev_attr->form);
712 break;
714 return TRUE;
717 static dwarf2_debug_info_t* dwarf2_jump_to_debug_info(struct attribute* attr);
719 static BOOL dwarf2_find_attribute(const dwarf2_debug_info_t* di,
720 unsigned at, struct attribute* attr)
722 unsigned i, refidx = 0;
723 dwarf2_abbrev_entry_attr_t* abbrev_attr;
724 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
726 attr->gotten_from = attr_direct;
727 while (di)
729 ref_abbrev_attr = NULL;
730 attr->debug_info = di;
731 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
733 if (abbrev_attr->attribute == at)
735 return dwarf2_fill_attr(di->unit_ctx, abbrev_attr, di->data[i], attr);
737 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
738 abbrev_attr->attribute == DW_AT_specification) &&
739 at != DW_AT_sibling)
741 if (ref_abbrev_attr)
742 FIXME("two references %Ix and %Ix\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
743 ref_abbrev_attr = abbrev_attr;
744 refidx = i;
745 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
746 attr_abstract_origin : attr_specification;
749 /* do we have either an abstract origin or a specification debug entry to look into ? */
750 if (!ref_abbrev_attr || !dwarf2_fill_attr(di->unit_ctx, ref_abbrev_attr, di->data[refidx], attr))
751 break;
752 if (!(di = dwarf2_jump_to_debug_info(attr)))
754 FIXME("Should have found the debug info entry\n");
755 break;
758 return FALSE;
761 static dwarf2_debug_info_t* dwarf2_jump_to_debug_info(struct attribute* attr)
763 dwarf2_parse_context_t* ref_ctx = NULL;
764 BOOL with_other = TRUE;
765 dwarf2_debug_info_t* ret;
767 switch (attr->form)
769 case DW_FORM_ref_addr:
770 ref_ctx = dwarf2_locate_cu(attr->debug_info->unit_ctx->module_ctx, attr->u.uvalue);
771 break;
772 case DW_FORM_GNU_ref_alt:
773 if (attr->debug_info->unit_ctx->module_ctx->dwz)
774 ref_ctx = dwarf2_locate_cu(&attr->debug_info->unit_ctx->module_ctx->dwz->module_ctx, attr->u.uvalue);
775 break;
776 default:
777 with_other = FALSE;
778 ref_ctx = attr->debug_info->unit_ctx;
779 break;
781 if (!ref_ctx) return FALSE;
782 /* There are cases where we end up with a circular reference between two (or more)
783 * compilation units. Before this happens, try to see if we can refer to an already
784 * loaded debug_info in the target compilation unit (even if all the debug_info
785 * haven't been loaded yet).
787 if (ref_ctx->status == UNIT_BEINGLOADED &&
788 (ret = sparse_array_find(&ref_ctx->debug_info_table, attr->u.uvalue)))
789 return ret;
790 if (with_other)
792 /* ensure CU is fully loaded */
793 if (ref_ctx != attr->debug_info->unit_ctx && !dwarf2_parse_compilation_unit(ref_ctx))
794 return NULL;
796 return sparse_array_find(&ref_ctx->debug_info_table, attr->u.uvalue);
799 static void dwarf2_load_one_entry(dwarf2_debug_info_t*);
801 #define Wine_DW_no_register 0x7FFFFFFF
803 static unsigned dwarf2_map_register(int regno, const struct module* module)
805 if (regno == Wine_DW_no_register)
807 FIXME("What the heck map reg 0x%x\n",regno);
808 return 0;
810 return module->cpu->map_dwarf_register(regno, module, FALSE);
813 static enum location_error
814 compute_location(const struct module *module, const dwarf2_cuhead_t* head,
815 dwarf2_traverse_context_t* ctx, struct location* loc,
816 HANDLE hproc, const struct location* frame)
818 DWORD_PTR tmp, stack[64];
819 unsigned stk;
820 unsigned char op;
821 BOOL piece_found = FALSE;
823 stack[stk = 0] = 0;
825 loc->kind = loc_absolute;
826 loc->reg = Wine_DW_no_register;
828 while (ctx->data < ctx->end_data)
830 op = dwarf2_parse_byte(ctx);
832 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
833 stack[++stk] = op - DW_OP_lit0;
834 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
836 /* dbghelp APIs don't know how to cope with this anyway
837 * (for example 'long long' stored in two registers)
838 * FIXME: We should tell winedbg how to deal with it (sigh)
840 if (!piece_found)
842 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0, module);
843 if (loc->reg != Wine_DW_no_register)
844 FIXME("Only supporting one reg (%s/%d -> %s/%ld)\n",
845 module->cpu->fetch_regname(loc->reg), loc->reg,
846 module->cpu->fetch_regname(cvreg), cvreg);
847 loc->reg = cvreg;
849 loc->kind = loc_register;
851 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
853 /* dbghelp APIs don't know how to cope with this anyway
854 * (for example 'long long' stored in two registers)
855 * FIXME: We should tell winedbg how to deal with it (sigh)
857 if (!piece_found)
859 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0, module);
860 if (loc->reg != Wine_DW_no_register)
861 FIXME("Only supporting one breg (%s/%d -> %s/%ld)\n",
862 module->cpu->fetch_regname(loc->reg), loc->reg,
863 module->cpu->fetch_regname(cvreg), cvreg);
864 loc->reg = cvreg;
866 stack[++stk] = dwarf2_leb128_as_signed(ctx);
867 loc->kind = loc_regrel;
869 else switch (op)
871 case DW_OP_nop: break;
872 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr_head(ctx, head); break;
873 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
874 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
875 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
876 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
877 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
878 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
879 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
880 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
881 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
882 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
883 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
884 case DW_OP_drop: stk--; break;
885 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
886 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
887 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
888 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
889 case DW_OP_abs: stack[stk] = sizeof(stack[stk]) == 8 ? llabs((INT64)stack[stk]) : abs((INT32)stack[stk]); break;
890 case DW_OP_neg: stack[stk] = -stack[stk]; break;
891 case DW_OP_not: stack[stk] = ~stack[stk]; break;
892 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
893 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
894 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
895 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
896 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
897 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
898 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
899 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
900 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
901 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
902 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
903 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
904 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
905 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
906 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
907 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
908 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
909 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
910 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
911 case DW_OP_bra:
912 tmp = dwarf2_parse_u2(ctx);
913 if (!stack[stk--]) ctx->data += tmp;
914 break;
915 case DW_OP_regx:
916 tmp = dwarf2_leb128_as_unsigned(ctx);
917 if (!piece_found)
919 if (loc->reg != Wine_DW_no_register)
920 FIXME("Only supporting one reg\n");
921 loc->reg = dwarf2_map_register(tmp, module);
923 loc->kind = loc_register;
924 break;
925 case DW_OP_bregx:
926 tmp = dwarf2_leb128_as_unsigned(ctx);
927 if (loc->reg != Wine_DW_no_register)
928 FIXME("Only supporting one regx\n");
929 loc->reg = dwarf2_map_register(tmp, module);
930 stack[++stk] = dwarf2_leb128_as_signed(ctx);
931 loc->kind = loc_regrel;
932 break;
933 case DW_OP_fbreg:
934 if (loc->reg != Wine_DW_no_register)
935 FIXME("Only supporting one reg (%s/%d -> -2)\n",
936 module->cpu->fetch_regname(loc->reg), loc->reg);
937 if (frame && frame->kind == loc_register)
939 loc->kind = loc_regrel;
940 loc->reg = frame->reg;
941 stack[++stk] = dwarf2_leb128_as_signed(ctx);
943 else if (frame && frame->kind == loc_regrel)
945 loc->kind = loc_regrel;
946 loc->reg = frame->reg;
947 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
949 else
951 /* FIXME: this could be later optimized by not recomputing
952 * this very location expression
954 loc->kind = loc_dwarf2_block;
955 stack[++stk] = dwarf2_leb128_as_signed(ctx);
957 break;
958 case DW_OP_piece:
960 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
961 WARN("Not handling OP_piece (size=%d)\n", sz);
962 piece_found = TRUE;
964 break;
965 case DW_OP_deref:
966 if (!stk)
968 FIXME("Unexpected empty stack\n");
969 return loc_err_internal;
971 if (loc->reg != Wine_DW_no_register)
973 WARN("Too complex expression for deref\n");
974 return loc_err_too_complex;
976 if (hproc)
978 DWORD_PTR addr = stack[stk--];
979 DWORD_PTR deref = 0;
981 if (!ReadProcessMemory(hproc, (void*)addr, &deref, head->word_size, NULL))
983 WARN("Couldn't read memory at %Ix\n", addr);
984 return loc_err_cant_read;
986 stack[++stk] = deref;
988 else
990 loc->kind = loc_dwarf2_block;
992 break;
993 case DW_OP_deref_size:
994 if (!stk)
996 FIXME("Unexpected empty stack\n");
997 return loc_err_internal;
999 if (loc->reg != Wine_DW_no_register)
1001 WARN("Too complex expression for deref\n");
1002 return loc_err_too_complex;
1004 if (hproc)
1006 DWORD_PTR addr = stack[stk--];
1007 BYTE derefsize = dwarf2_parse_byte(ctx);
1008 DWORD64 deref;
1010 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
1012 WARN("Couldn't read memory at %Ix\n", addr);
1013 return loc_err_cant_read;
1016 switch (derefsize)
1018 case 1: stack[++stk] = *(unsigned char*)&deref; break;
1019 case 2: stack[++stk] = *(unsigned short*)&deref; break;
1020 case 4: stack[++stk] = *(DWORD*)&deref; break;
1021 case 8: if (head->word_size >= derefsize) stack[++stk] = deref; break;
1024 else
1026 dwarf2_parse_byte(ctx);
1027 loc->kind = loc_dwarf2_block;
1029 break;
1030 case DW_OP_stack_value:
1031 /* Expected behaviour is that this is the last instruction of this
1032 * expression and just the "top of stack" value should be put to loc->offset. */
1033 break;
1034 default:
1035 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
1036 FIXME("Unhandled attr op: %x\n", op);
1037 /* FIXME else unhandled extension */
1038 return loc_err_internal;
1041 loc->offset = stack[stk];
1042 return 0;
1045 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
1046 const dwarf2_debug_info_t* di,
1047 ULONG_PTR dw,
1048 struct location* loc,
1049 const struct location* frame)
1051 struct attribute xloc;
1053 if (!dwarf2_find_attribute(di, dw, &xloc)) return FALSE;
1055 switch (xloc.form)
1057 case DW_FORM_data4:
1058 if (ctx->head.version < 4)
1060 loc->kind = loc_dwarf2_location_list;
1061 loc->reg = Wine_DW_no_register;
1062 loc->offset = xloc.u.uvalue;
1063 return TRUE;
1065 /* fall through */
1066 case DW_FORM_data1: case DW_FORM_data2:
1067 case DW_FORM_udata: case DW_FORM_sdata:
1068 loc->kind = loc_absolute;
1069 loc->reg = 0;
1070 loc->offset = xloc.u.uvalue;
1071 return TRUE;
1072 case DW_FORM_data8:
1073 if (ctx->head.version >= 4)
1075 loc->kind = loc_absolute;
1076 loc->reg = 0;
1077 loc->offset = xloc.u.lluvalue;
1078 return TRUE;
1080 /* fall through */
1081 case DW_FORM_sec_offset:
1082 loc->kind = loc_dwarf2_location_list;
1083 loc->reg = Wine_DW_no_register;
1084 loc->offset = xloc.u.lluvalue;
1085 return TRUE;
1086 case DW_FORM_block:
1087 case DW_FORM_block1:
1088 case DW_FORM_block2:
1089 case DW_FORM_block4:
1090 case DW_FORM_exprloc:
1091 break;
1092 default: FIXME("Unsupported yet form %Ix\n", xloc.form);
1093 return FALSE;
1096 /* assume we have a block form */
1097 if (dw == DW_AT_frame_base && xloc.u.block.size == 1 && *xloc.u.block.ptr == DW_OP_call_frame_cfa)
1099 loc->kind = loc_dwarf2_frame_cfa;
1100 loc->reg = Wine_DW_no_register;
1101 loc->offset = 0;
1103 else if (xloc.u.block.size)
1105 dwarf2_traverse_context_t lctx;
1106 enum location_error err;
1108 lctx.data = xloc.u.block.ptr;
1109 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
1111 err = compute_location(ctx->module_ctx->module, &ctx->head, &lctx, loc, NULL, frame);
1112 if (err < 0)
1114 loc->kind = loc_error;
1115 loc->reg = err;
1117 else if (loc->kind == loc_dwarf2_block)
1119 unsigned* ptr = pool_alloc(&ctx->module_ctx->module->pool,
1120 sizeof(unsigned) + xloc.u.block.size);
1121 *ptr = xloc.u.block.size;
1122 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
1123 loc->offset = (ULONG_PTR)ptr;
1126 return TRUE;
1129 static struct symt* dwarf2_lookup_type(const dwarf2_debug_info_t* di)
1131 struct attribute attr;
1132 dwarf2_debug_info_t* type;
1134 if (!dwarf2_find_attribute(di, DW_AT_type, &attr))
1135 /* this is only valid if current language of CU is C or C++ */
1136 return &symt_get_basic(btVoid, 0)->symt;
1137 if (!(type = dwarf2_jump_to_debug_info(&attr)))
1138 return &symt_get_basic(btNoType, 0)->symt;
1140 if (type == di)
1142 FIXME("Reference to itself\n");
1143 return &symt_get_basic(btNoType, 0)->symt;
1145 if (!type->symt)
1147 /* load the debug info entity */
1148 dwarf2_load_one_entry(type);
1149 if (!type->symt)
1151 FIXME("Unable to load forward reference for tag %Ix\n", type->abbrev->tag);
1152 return &symt_get_basic(btNoType, 0)->symt;
1155 return type->symt;
1158 static const char* dwarf2_get_cpp_name(dwarf2_debug_info_t* di, const char* name)
1160 char* last;
1161 struct attribute diname;
1162 struct attribute spec;
1164 if (di->abbrev->tag == DW_TAG_compile_unit || di->abbrev->tag == DW_TAG_partial_unit) return name;
1166 /* if the di is a definition, but has also a (previous) declaration, then scope must
1167 * be gotten from declaration not definition
1169 if (dwarf2_find_attribute(di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
1171 di = dwarf2_jump_to_debug_info(&spec);
1172 if (!di)
1174 FIXME("Should have found the debug info entry\n");
1175 return NULL;
1179 if (!di->unit_ctx->cpp_name)
1180 di->unit_ctx->cpp_name = pool_alloc(&di->unit_ctx->pool, MAX_SYM_NAME);
1181 last = di->unit_ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
1182 strcpy(last, name);
1184 for (di = di->parent; di; di = di->parent)
1186 switch (di->abbrev->tag)
1188 case DW_TAG_namespace:
1189 case DW_TAG_structure_type:
1190 case DW_TAG_class_type:
1191 case DW_TAG_interface_type:
1192 case DW_TAG_union_type:
1193 if (dwarf2_find_attribute(di, DW_AT_name, &diname))
1195 size_t len = strlen(diname.u.string);
1196 last -= 2 + len;
1197 if (last < di->unit_ctx->cpp_name) return NULL;
1198 memcpy(last, diname.u.string, len);
1199 last[len] = last[len + 1] = ':';
1201 break;
1202 default:
1203 break;
1206 return last;
1209 /******************************************************************
1210 * dwarf2_read_range
1212 * read a range for a given debug_info (either using AT_range attribute, in which
1213 * case we don't return all the details, or using AT_low_pc & AT_high_pc attributes)
1214 * in all cases, range is relative to beginning of compilation unit
1216 static BOOL dwarf2_read_range(dwarf2_parse_context_t* ctx, const dwarf2_debug_info_t* di,
1217 ULONG_PTR* plow, ULONG_PTR* phigh)
1219 struct attribute range;
1221 if (dwarf2_find_attribute(di, DW_AT_ranges, &range))
1223 dwarf2_traverse_context_t traverse;
1224 ULONG_PTR low, high;
1225 const ULONG_PTR UMAX = ~(ULONG_PTR)0u;
1227 traverse.data = ctx->module_ctx->sections[section_ranges].address + range.u.uvalue;
1228 traverse.end_data = ctx->module_ctx->sections[section_ranges].address +
1229 ctx->module_ctx->sections[section_ranges].size;
1231 *plow = UMAX;
1232 *phigh = 0;
1233 while (traverse.data + 2 * ctx->head.word_size < traverse.end_data)
1235 low = dwarf2_parse_addr_head(&traverse, &ctx->head);
1236 high = dwarf2_parse_addr_head(&traverse, &ctx->head);
1237 if (low == 0 && high == 0) break;
1238 if (low == (ctx->head.word_size == 8 ? (~(DWORD64)0u) : (DWORD64)(~0u)))
1239 FIXME("unsupported yet (base address selection)\n");
1240 /* range values are relative to start of compilation unit */
1241 low += ctx->compiland->address - ctx->module_ctx->load_offset;
1242 high += ctx->compiland->address - ctx->module_ctx->load_offset;
1243 if (low < *plow) *plow = low;
1244 if (high > *phigh) *phigh = high;
1246 if (*plow == UMAX || *phigh == 0) {WARN("no entry found\n"); return FALSE;}
1247 if (*plow == *phigh) {WARN("entry found, but low=high %Ix %Ix\n", low, high); return FALSE;}
1249 return TRUE;
1251 else
1253 struct attribute low_pc;
1254 struct attribute high_pc;
1256 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc) ||
1257 !dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc))
1258 return FALSE;
1259 *plow = low_pc.u.uvalue;
1260 *phigh = high_pc.u.uvalue;
1261 if (ctx->head.version >= 4)
1262 switch (high_pc.form)
1264 case DW_FORM_addr:
1265 break;
1266 case DW_FORM_data1:
1267 case DW_FORM_data2:
1268 case DW_FORM_data4:
1269 case DW_FORM_data8:
1270 case DW_FORM_sdata:
1271 case DW_FORM_udata:
1272 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1273 *phigh += *plow;
1274 break;
1275 default:
1276 FIXME("Unsupported class for high_pc\n");
1277 break;
1279 return TRUE;
1283 static struct addr_range* dwarf2_get_ranges(const dwarf2_debug_info_t* di, unsigned* num_ranges)
1285 struct attribute range;
1286 struct addr_range* ranges;
1287 struct addr_range* new_ranges;
1289 if (dwarf2_find_attribute(di, DW_AT_ranges, &range))
1291 dwarf2_traverse_context_t traverse;
1292 unsigned alloc = 16;
1293 ranges = malloc(sizeof(struct addr_range) * alloc);
1294 if (!ranges) return NULL;
1295 *num_ranges = 0;
1297 traverse.data = di->unit_ctx->module_ctx->sections[section_ranges].address + range.u.uvalue;
1298 traverse.end_data = di->unit_ctx->module_ctx->sections[section_ranges].address +
1299 di->unit_ctx->module_ctx->sections[section_ranges].size;
1301 while (traverse.data + 2 * di->unit_ctx->head.word_size < traverse.end_data)
1303 ULONG_PTR low = dwarf2_parse_addr_head(&traverse, &di->unit_ctx->head);
1304 ULONG_PTR high = dwarf2_parse_addr_head(&traverse, &di->unit_ctx->head);
1305 if (low == 0 && high == 0) break;
1306 if (low == (di->unit_ctx->head.word_size == 8 ? (~(DWORD64)0u) : (DWORD64)(~0u)))
1307 FIXME("unsupported yet (base address selection)\n");
1308 if (*num_ranges >= alloc)
1310 alloc *= 2;
1311 new_ranges = realloc(ranges, sizeof(struct addr_range) * alloc);
1312 if (!new_ranges)
1314 free(ranges);
1315 return NULL;
1317 ranges = new_ranges;
1319 ranges[*num_ranges].low = di->unit_ctx->compiland->address + low;
1320 ranges[*num_ranges].high = di->unit_ctx->compiland->address + high;
1321 (*num_ranges)++;
1324 else
1326 struct attribute low_pc;
1327 struct attribute high_pc;
1329 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc) ||
1330 !dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc))
1331 return NULL;
1332 if (di->unit_ctx->head.version >= 4)
1333 switch (high_pc.form)
1335 case DW_FORM_addr:
1336 break;
1337 case DW_FORM_data1:
1338 case DW_FORM_data2:
1339 case DW_FORM_data4:
1340 case DW_FORM_data8:
1341 case DW_FORM_sdata:
1342 case DW_FORM_udata:
1343 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1344 high_pc.u.uvalue += low_pc.u.uvalue;
1345 break;
1346 default:
1347 FIXME("Unsupported class for high_pc\n");
1348 break;
1350 ranges = malloc(sizeof(struct addr_range));
1351 if (!ranges) return NULL;
1352 ranges[0].low = di->unit_ctx->module_ctx->load_offset + low_pc.u.uvalue;
1353 ranges[0].high = di->unit_ctx->module_ctx->load_offset + high_pc.u.uvalue;
1354 *num_ranges = 1;
1356 return ranges;
1359 /******************************************************************
1360 * dwarf2_read_one_debug_info
1362 * Loads into memory one debug info entry, and recursively its children (if any)
1364 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1365 dwarf2_traverse_context_t* traverse,
1366 dwarf2_debug_info_t* parent_di,
1367 dwarf2_debug_info_t** pdi)
1369 const dwarf2_abbrev_entry_t*abbrev;
1370 ULONG_PTR entry_code;
1371 ULONG_PTR offset;
1372 dwarf2_debug_info_t* di;
1373 dwarf2_debug_info_t* child;
1374 dwarf2_debug_info_t** where;
1375 dwarf2_abbrev_entry_attr_t* attr;
1376 unsigned i;
1377 struct attribute sibling;
1379 offset = traverse->data - ctx->module_ctx->sections[ctx->section].address;
1380 entry_code = dwarf2_leb128_as_unsigned(traverse);
1381 TRACE("found entry_code %Iu at 0x%Ix\n", entry_code, offset);
1382 if (!entry_code)
1384 *pdi = NULL;
1385 return TRUE;
1387 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1388 if (!abbrev)
1390 WARN("Cannot find abbrev entry for %Iu at 0x%Ix\n", entry_code, offset);
1391 return FALSE;
1393 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1394 if (!di) return FALSE;
1395 di->abbrev = abbrev;
1396 di->symt = NULL;
1397 di->parent = parent_di;
1398 di->unit_ctx = ctx;
1400 if (abbrev->num_attr)
1402 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1403 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1405 di->data[i] = traverse->data;
1406 dwarf2_swallow_attribute(traverse, &ctx->head, attr);
1409 else di->data = NULL;
1410 if (abbrev->have_child)
1412 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1413 while (traverse->data < traverse->end_data)
1415 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1416 if (!child) break;
1417 where = vector_add(&di->children, &ctx->pool);
1418 if (!where) return FALSE;
1419 *where = child;
1422 if (dwarf2_find_attribute(di, DW_AT_sibling, &sibling) &&
1423 traverse->data != ctx->module_ctx->sections[ctx->section].address + sibling.u.uvalue)
1425 if (sibling.u.uvalue >= ctx->module_ctx->sections[ctx->section].size)
1427 FIXME("cursor sibling after section end %s: 0x%Ix 0x%x\n",
1428 dwarf2_debug_unit_ctx(ctx), sibling.u.uvalue, ctx->module_ctx->sections[ctx->section].size);
1429 return FALSE;
1431 WARN("setting cursor for %s to next sibling <0x%Ix>\n",
1432 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1433 traverse->data = ctx->module_ctx->sections[ctx->section].address + sibling.u.uvalue;
1435 *pdi = di;
1436 return TRUE;
1439 static struct vector* dwarf2_get_di_children(dwarf2_debug_info_t* di)
1441 struct attribute spec;
1443 while (di)
1445 if (di->abbrev->have_child)
1446 return &di->children;
1447 if (!dwarf2_find_attribute(di, DW_AT_specification, &spec)) break;
1448 if (!(di = dwarf2_jump_to_debug_info(&spec)))
1449 FIXME("Should have found the debug info entry\n");
1451 return NULL;
1454 /* reconstruct whether integer is long (contains 'long' only once) */
1455 static BOOL is_long(const char* name)
1457 /* we assume name is made only of basic C keywords:
1458 * int long short unsigned signed void float double char _Bool _Complex
1460 const char* p = strstr(name, "long");
1461 return p && strstr(p + 4, "long") == NULL;
1464 static BOOL is_c_language(dwarf2_parse_context_t* unit_ctx)
1466 return unit_ctx->language == DW_LANG_C ||
1467 unit_ctx->language == DW_LANG_C89 ||
1468 unit_ctx->language == DW_LANG_C99;
1471 static BOOL is_cpp_language(dwarf2_parse_context_t* unit_ctx)
1473 return unit_ctx->language == DW_LANG_C_plus_plus;
1476 static struct symt* dwarf2_parse_base_type(dwarf2_debug_info_t* di)
1478 struct attribute name;
1479 struct attribute size;
1480 struct attribute encoding;
1481 enum BasicType bt;
1482 BOOL c_language, cpp_language;
1484 if (di->symt) return di->symt;
1486 TRACE("%s\n", dwarf2_debug_di(di));
1488 c_language = is_c_language(di->unit_ctx);
1489 cpp_language = is_cpp_language(di->unit_ctx);
1491 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1492 name.u.string = NULL;
1493 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1494 if (!dwarf2_find_attribute(di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1496 switch (encoding.u.uvalue)
1498 case DW_ATE_void: bt = btVoid; break;
1499 case DW_ATE_address: bt = btULong; break;
1500 case DW_ATE_boolean: bt = btBool; break;
1501 case DW_ATE_complex_float: bt = btComplex; break;
1502 case DW_ATE_float: bt = btFloat; break;
1503 case DW_ATE_signed: bt = ((c_language || cpp_language) && is_long(name.u.string)) ? btLong : btInt; break;
1504 case DW_ATE_unsigned:
1505 if ((c_language || cpp_language) && is_long(name.u.string)) bt = btULong;
1506 else if (cpp_language && !strcmp(name.u.string, "wchar_t")) bt = btWChar;
1507 else if (cpp_language && !strcmp(name.u.string, "char8_t")) bt = btChar8;
1508 else if (cpp_language && !strcmp(name.u.string, "char16_t")) bt = btChar16;
1509 else if (cpp_language && !strcmp(name.u.string, "char32_t")) bt = btChar32;
1510 else bt = btUInt;
1511 break;
1512 /* on Windows, in C, char == signed char, but not in C++ */
1513 case DW_ATE_signed_char: bt = (cpp_language && !strcmp(name.u.string, "signed char")) ? btInt : btChar; break;
1514 case DW_ATE_unsigned_char: bt = btUInt; break;
1515 case DW_ATE_UTF: bt = (size.u.uvalue == 1) ? btChar8 : (size.u.uvalue == 2 ? btChar16 : btChar32); break;
1516 default: bt = btNoType; break;
1518 di->symt = &symt_get_basic(bt, size.u.uvalue)->symt;
1520 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1521 return di->symt;
1524 static struct symt* dwarf2_parse_typedef(dwarf2_debug_info_t* di)
1526 struct symt* ref_type;
1527 struct attribute name;
1529 if (di->symt) return di->symt;
1531 TRACE("%s\n", dwarf2_debug_di(di));
1533 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1534 ref_type = dwarf2_lookup_type(di);
1536 if (name.u.string)
1538 /* Note: The MS C compiler has tweaks for WCHAR support.
1539 * Even if WCHAR is a typedef to wchar_t, wchar_t is emitted as btUInt/2 (it's defined as
1540 * unsigned short, so far so good), while WCHAR is emitted as btWChar/2).
1542 if ((is_c_language(di->unit_ctx) || is_cpp_language(di->unit_ctx)) && !strcmp(name.u.string, "WCHAR"))
1543 ref_type = &symt_get_basic(btWChar, 2)->symt;
1544 di->symt = &symt_new_typedef(di->unit_ctx->module_ctx->module, ref_type, name.u.string)->symt;
1546 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1547 return di->symt;
1550 static struct symt* dwarf2_parse_pointer_type(dwarf2_debug_info_t* di)
1552 struct symt* ref_type;
1553 struct attribute size;
1555 if (di->symt) return di->symt;
1557 TRACE("%s\n", dwarf2_debug_di(di));
1559 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = di->unit_ctx->module_ctx->module->cpu->word_size;
1560 ref_type = dwarf2_lookup_type(di);
1561 di->symt = &symt_new_pointer(di->unit_ctx->module_ctx->module, ref_type, size.u.uvalue)->symt;
1562 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1563 return di->symt;
1566 static struct symt* dwarf2_parse_subrange_type(dwarf2_debug_info_t* di)
1568 struct symt* ref_type;
1569 struct attribute name;
1570 struct attribute dummy;
1572 if (di->symt) return di->symt;
1574 TRACE("%s\n", dwarf2_debug_di(di));
1576 if (dwarf2_find_attribute(di, DW_AT_name, &name)) FIXME("Found name for subrange %s\n", name.u.string);
1577 if (dwarf2_find_attribute(di, DW_AT_byte_size, &dummy)) FIXME("Found byte_size %Iu\n", dummy.u.uvalue);
1578 if (dwarf2_find_attribute(di, DW_AT_bit_size, &dummy)) FIXME("Found bit_size %Iu\n", dummy.u.uvalue);
1579 /* for now, we don't support the byte_size nor bit_size about the subrange, and pretend the two
1580 * types are the same (FIXME)
1582 ref_type = dwarf2_lookup_type(di);
1583 di->symt = ref_type;
1584 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1585 return di->symt;
1588 static struct symt* dwarf2_parse_array_type(dwarf2_debug_info_t* di)
1590 struct symt* ref_type;
1591 struct symt* idx_type = NULL;
1592 struct symt* symt = NULL;
1593 struct attribute min, max, cnt;
1594 dwarf2_debug_info_t* child;
1595 unsigned int i, j;
1596 const struct vector* children;
1598 if (di->symt) return di->symt;
1600 TRACE("%s\n", dwarf2_debug_di(di));
1602 ref_type = dwarf2_lookup_type(di);
1604 if (!(children = dwarf2_get_di_children(di)))
1606 /* fake an array with unknown size */
1607 /* FIXME: int4 even on 64bit machines??? */
1608 idx_type = &symt_get_basic(btInt, 4)->symt;
1609 min.u.uvalue = 0;
1610 cnt.u.uvalue = 0;
1612 else for (i = 0; i < vector_length(children); i++)
1614 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1615 if (child->symt == &symt_get_basic(btNoType, 0)->symt) continue;
1616 switch (child->abbrev->tag)
1618 case DW_TAG_subrange_type:
1619 idx_type = dwarf2_lookup_type(child);
1620 if (!dwarf2_find_attribute(child, DW_AT_lower_bound, &min))
1621 min.u.uvalue = 0;
1622 if (dwarf2_find_attribute(child, DW_AT_upper_bound, &max))
1623 cnt.u.uvalue = max.u.uvalue + 1 - min.u.uvalue;
1624 else if (!dwarf2_find_attribute(child, DW_AT_count, &cnt))
1625 cnt.u.uvalue = 0;
1626 break;
1627 case DW_TAG_enumeration_type:
1628 symt = dwarf2_parse_enumeration_type(child);
1629 if (symt_check_tag(symt, SymTagEnum))
1631 struct symt_enum* enum_symt = (struct symt_enum*)symt;
1632 idx_type = enum_symt->base_type;
1633 min.u.uvalue = ~0U;
1634 max.u.uvalue = ~0U;
1635 for (j = 0; j < enum_symt->vchildren.num_elts; ++j)
1637 struct symt** pc = vector_at(&enum_symt->vchildren, j);
1638 if (pc && symt_check_tag(*pc, SymTagData))
1640 struct symt_data* elt = (struct symt_data*)(*pc);
1641 if (elt->u.value.n1.n2.n3.lVal < min.u.uvalue)
1642 min.u.uvalue = elt->u.value.n1.n2.n3.lVal;
1643 if (elt->u.value.n1.n2.n3.lVal > max.u.uvalue)
1644 max.u.uvalue = elt->u.value.n1.n2.n3.lVal;
1648 break;
1649 default:
1650 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1651 child->abbrev->tag, dwarf2_debug_di(di));
1652 break;
1655 di->symt = &symt_new_array(di->unit_ctx->module_ctx->module, min.u.uvalue, cnt.u.uvalue, ref_type, idx_type)->symt;
1656 return di->symt;
1659 static struct symt* dwarf2_parse_const_type(dwarf2_debug_info_t* di)
1661 struct symt* ref_type;
1663 if (di->symt) return di->symt;
1665 TRACE("%s\n", dwarf2_debug_di(di));
1667 ref_type = dwarf2_lookup_type(di);
1668 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1669 di->symt = ref_type;
1671 return ref_type;
1674 static struct symt* dwarf2_parse_volatile_type(dwarf2_debug_info_t* di)
1676 struct symt* ref_type;
1678 if (di->symt) return di->symt;
1680 TRACE("%s\n", dwarf2_debug_di(di));
1682 ref_type = dwarf2_lookup_type(di);
1683 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1684 di->symt = ref_type;
1686 return ref_type;
1689 static struct symt* dwarf2_parse_restrict_type(dwarf2_debug_info_t* di)
1691 struct symt* ref_type;
1693 if (di->symt) return di->symt;
1695 TRACE("%s\n", dwarf2_debug_di(di));
1697 ref_type = dwarf2_lookup_type(di);
1698 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1699 di->symt = ref_type;
1701 return ref_type;
1704 static struct symt* dwarf2_parse_unspecified_type(dwarf2_debug_info_t* di)
1706 struct attribute name;
1707 struct symt* basic;
1709 TRACE("%s\n", dwarf2_debug_di(di));
1711 if (di->symt) return di->symt;
1713 basic = &symt_get_basic(btVoid, 0)->symt;
1714 if (dwarf2_find_attribute(di, DW_AT_name, &name))
1715 /* define the missing type as a typedef to void... */
1716 di->symt = &symt_new_typedef(di->unit_ctx->module_ctx->module, basic, name.u.string)->symt;
1717 else /* or use void if it doesn't even have a name */
1718 di->symt = basic;
1720 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1721 return di->symt;
1724 static struct symt* dwarf2_parse_reference_type(dwarf2_debug_info_t* di)
1726 struct symt* ref_type = NULL;
1728 if (di->symt) return di->symt;
1730 TRACE("%s\n", dwarf2_debug_di(di));
1732 ref_type = dwarf2_lookup_type(di);
1733 /* FIXME: for now, we hard-wire C++ references to pointers */
1734 di->symt = &symt_new_pointer(di->unit_ctx->module_ctx->module, ref_type,
1735 di->unit_ctx->module_ctx->module->cpu->word_size)->symt;
1737 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1739 return di->symt;
1742 static void dwarf2_parse_udt_member(dwarf2_debug_info_t* di,
1743 struct symt_udt* parent)
1745 struct symt* elt_type;
1746 struct attribute name;
1747 struct attribute bit_size;
1748 struct attribute bit_offset;
1749 struct location loc;
1751 assert(parent);
1753 TRACE("%s\n", dwarf2_debug_di(di));
1755 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1756 elt_type = dwarf2_lookup_type(di);
1757 if (dwarf2_compute_location_attr(di->unit_ctx, di, DW_AT_data_member_location, &loc, NULL))
1759 if (loc.kind != loc_absolute)
1761 FIXME("Unexpected offset computation for member %s in %ls!%s\n",
1762 name.u.string, di->unit_ctx->module_ctx->module->modulename, parent->hash_elt.name);
1763 loc.offset = 0;
1765 else
1766 TRACE("found member_location at %s -> %Iu\n",
1767 dwarf2_debug_di(di), loc.offset);
1769 else
1770 loc.offset = 0;
1771 if (!dwarf2_find_attribute(di, DW_AT_bit_size, &bit_size))
1772 bit_size.u.uvalue = 0;
1773 if (dwarf2_find_attribute(di, DW_AT_bit_offset, &bit_offset))
1775 /* FIXME: we should only do this when implementation is LSB (which is
1776 * the case on i386 processors)
1778 struct attribute nbytes;
1779 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &nbytes))
1781 DWORD64 size;
1782 nbytes.u.uvalue = symt_get_info(di->unit_ctx->module_ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1783 (ULONG_PTR)size : 0;
1785 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1787 else bit_offset.u.uvalue = 0;
1788 symt_add_udt_element(di->unit_ctx->module_ctx->module, parent, name.u.string, elt_type,
1789 loc.offset, bit_offset.u.uvalue,
1790 bit_size.u.uvalue);
1792 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1795 static struct symt* dwarf2_parse_subprogram(dwarf2_debug_info_t* di);
1797 static struct symt* dwarf2_parse_udt_type(dwarf2_debug_info_t* di,
1798 enum UdtKind udt)
1800 struct attribute name;
1801 struct attribute size;
1802 struct vector* children;
1803 dwarf2_debug_info_t*child;
1804 unsigned int i;
1806 if (di->symt) return di->symt;
1808 TRACE("%s\n", dwarf2_debug_di(di));
1810 /* quirk... FIXME provide real support for anonymous UDTs */
1811 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1812 name.u.string = "zz_anon_zz";
1813 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1815 di->symt = &symt_new_udt(di->unit_ctx->module_ctx->module, dwarf2_get_cpp_name(di, name.u.string),
1816 size.u.uvalue, udt)->symt;
1818 children = dwarf2_get_di_children(di);
1819 if (children) for (i = 0; i < vector_length(children); i++)
1821 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1823 switch (child->abbrev->tag)
1825 case DW_TAG_array_type:
1826 dwarf2_parse_array_type(child);
1827 break;
1828 case DW_TAG_member:
1829 /* FIXME: should I follow the sibling stuff ?? */
1830 if (symt_check_tag(di->symt, SymTagUDT))
1831 dwarf2_parse_udt_member(child, (struct symt_udt*)di->symt);
1832 break;
1833 case DW_TAG_enumeration_type:
1834 dwarf2_parse_enumeration_type(child);
1835 break;
1836 case DW_TAG_subprogram:
1837 dwarf2_parse_subprogram(child);
1838 break;
1839 case DW_TAG_const_type:
1840 dwarf2_parse_const_type(child);
1841 break;
1842 case DW_TAG_volatile_type:
1843 dwarf2_parse_volatile_type(child);
1844 break;
1845 case DW_TAG_pointer_type:
1846 dwarf2_parse_pointer_type(child);
1847 break;
1848 case DW_TAG_subrange_type:
1849 dwarf2_parse_subrange_type(child);
1850 break;
1851 case DW_TAG_structure_type:
1852 case DW_TAG_class_type:
1853 case DW_TAG_union_type:
1854 case DW_TAG_typedef:
1855 /* FIXME: we need to handle nested udt definitions */
1856 case DW_TAG_inheritance:
1857 case DW_TAG_interface_type:
1858 case DW_TAG_template_type_param:
1859 case DW_TAG_template_value_param:
1860 case DW_TAG_variable:
1861 case DW_TAG_imported_declaration:
1862 case DW_TAG_ptr_to_member_type:
1863 case DW_TAG_GNU_template_template_param:
1864 case DW_TAG_GNU_template_parameter_pack:
1865 case DW_TAG_GNU_formal_parameter_pack:
1866 /* FIXME: some C++ related stuff */
1867 break;
1868 default:
1869 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1870 child->abbrev->tag, dwarf2_debug_di(di));
1871 break;
1875 return di->symt;
1878 static void dwarf2_parse_enumerator(dwarf2_debug_info_t* di,
1879 struct symt_enum* parent)
1881 struct attribute name;
1882 struct attribute value;
1884 TRACE("%s\n", dwarf2_debug_di(di));
1886 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) return;
1887 if (!dwarf2_find_attribute(di, DW_AT_const_value, &value)) value.u.svalue = 0;
1888 symt_add_enum_element(di->unit_ctx->module_ctx->module, parent, name.u.string, value.u.svalue);
1890 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1893 static struct symt* dwarf2_parse_enumeration_type(dwarf2_debug_info_t* di)
1895 struct attribute name;
1896 struct attribute attrtype;
1897 dwarf2_debug_info_t*ditype;
1898 struct symt* type;
1899 struct vector* children;
1900 dwarf2_debug_info_t*child;
1901 unsigned int i;
1903 if (di->symt) return di->symt;
1905 TRACE("%s\n", dwarf2_debug_di(di));
1907 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1908 if (dwarf2_find_attribute(di, DW_AT_type, &attrtype) && (ditype = dwarf2_jump_to_debug_info(&attrtype)) != NULL)
1909 type = ditype->symt;
1910 else /* no type found for this enumeration, construct it from size */
1912 struct attribute size;
1913 struct symt_basic* basetype;
1915 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1917 switch (size.u.uvalue) /* FIXME: that's wrong */
1919 case 1: basetype = symt_get_basic(btInt, 1); break;
1920 case 2: basetype = symt_get_basic(btInt, 2); break;
1921 default:
1922 case 4: basetype = symt_get_basic(btInt, 4); break;
1924 type = &basetype->symt;
1927 di->symt = &symt_new_enum(di->unit_ctx->module_ctx->module, name.u.string, type)->symt;
1928 children = dwarf2_get_di_children(di);
1930 if (children) for (i = 0; i < vector_length(children); i++)
1932 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1934 switch (child->abbrev->tag)
1936 case DW_TAG_enumerator:
1937 if (symt_check_tag(di->symt, SymTagEnum))
1938 dwarf2_parse_enumerator(child, (struct symt_enum*)di->symt);
1939 break;
1940 default:
1941 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1942 di->abbrev->tag, dwarf2_debug_di(di));
1945 return di->symt;
1948 /* structure used to pass information around when parsing a subprogram */
1949 typedef struct dwarf2_subprogram_s
1951 dwarf2_parse_context_t* ctx;
1952 struct symt_function* top_func;
1953 struct symt_function* current_func; /* either symt_function* or symt_inlinesite* */
1954 struct symt_block* current_block;
1955 BOOL non_computed_variable;
1956 struct location frame;
1957 } dwarf2_subprogram_t;
1959 /******************************************************************
1960 * dwarf2_parse_variable
1962 * Parses any variable (parameter, local/global variable)
1964 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1965 dwarf2_debug_info_t* di)
1967 struct symt* param_type;
1968 struct attribute name, value;
1969 struct location loc;
1970 BOOL is_pmt;
1972 TRACE("%s\n", dwarf2_debug_di(di));
1974 is_pmt = !subpgm->current_block && di->abbrev->tag == DW_TAG_formal_parameter;
1975 param_type = dwarf2_lookup_type(di);
1977 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) {
1978 /* cannot do much without the name, the functions below won't like it. */
1979 return;
1981 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1982 &loc, &subpgm->frame))
1984 struct attribute ext;
1986 TRACE("found parameter %s (kind=%d, offset=%Id, reg=%d) at %s\n",
1987 debugstr_a(name.u.string), loc.kind, loc.offset, loc.reg,
1988 dwarf2_debug_unit_ctx(subpgm->ctx));
1990 switch (loc.kind)
1992 case loc_error:
1993 break;
1994 case loc_absolute:
1995 /* it's a global variable */
1996 /* FIXME: we don't handle its scope yet */
1997 if (!dwarf2_find_attribute(di, DW_AT_external, &ext))
1998 ext.u.uvalue = 0;
1999 loc.offset += subpgm->ctx->module_ctx->load_offset;
2000 symt_new_global_variable(subpgm->ctx->module_ctx->module,
2001 ext.u.uvalue ? NULL : subpgm->ctx->compiland,
2002 dwarf2_get_cpp_name(di, name.u.string), !ext.u.uvalue,
2003 loc, 0, param_type);
2004 break;
2005 default:
2006 subpgm->non_computed_variable = TRUE;
2007 /* fall through */
2008 case loc_register:
2009 case loc_regrel:
2010 /* either a pmt/variable relative to frame pointer or
2011 * pmt/variable in a register
2013 if (subpgm->current_func)
2014 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->current_func,
2015 is_pmt ? DataIsParam : DataIsLocal,
2016 &loc, subpgm->current_block, param_type, name.u.string);
2017 break;
2020 else if (dwarf2_find_attribute(di, DW_AT_const_value, &value))
2022 VARIANT v;
2024 switch (value.form)
2026 case DW_FORM_data1:
2027 case DW_FORM_data2:
2028 case DW_FORM_data4:
2029 case DW_FORM_udata:
2030 case DW_FORM_addr:
2031 V_VT(&v) = VT_UI4;
2032 V_UI4(&v) = value.u.uvalue;
2033 break;
2035 case DW_FORM_data8:
2036 case DW_FORM_sec_offset:
2037 V_VT(&v) = VT_UI8;
2038 V_UI8(&v) = value.u.lluvalue;
2039 break;
2041 case DW_FORM_sdata:
2042 V_VT(&v) = VT_I4;
2043 V_I4(&v) = value.u.svalue;
2044 break;
2046 case DW_FORM_strp:
2047 case DW_FORM_string:
2048 /* FIXME: native doesn't report const strings from here !!
2049 * however, the value of the string is in the code somewhere
2051 V_VT(&v) = VT_BYREF;
2052 V_BYREF(&v) = pool_strdup(&subpgm->ctx->module_ctx->module->pool, value.u.string);
2053 break;
2055 case DW_FORM_block:
2056 case DW_FORM_block1:
2057 case DW_FORM_block2:
2058 case DW_FORM_block4:
2059 case DW_FORM_exprloc:
2060 V_VT(&v) = VT_I4;
2061 switch (value.u.block.size)
2063 case 1: V_I4(&v) = *(BYTE*)value.u.block.ptr; break;
2064 case 2: V_I4(&v) = *(USHORT*)value.u.block.ptr; break;
2065 case 4: V_I4(&v) = *(DWORD*)value.u.block.ptr; break;
2066 default:
2067 V_VT(&v) = VT_BYREF;
2068 V_BYREF(&v) = pool_alloc(&subpgm->ctx->module_ctx->module->pool, value.u.block.size);
2069 memcpy(V_BYREF(&v), value.u.block.ptr, value.u.block.size);
2071 break;
2073 default:
2074 FIXME("Unsupported form for const value %s (%Ix)\n",
2075 debugstr_a(name.u.string), value.form);
2076 V_VT(&v) = VT_EMPTY;
2078 if (subpgm->current_func)
2080 if (is_pmt) WARN("Constant parameter %s reported as local variable in function '%s'\n",
2081 debugstr_a(name.u.string), subpgm->current_func->hash_elt.name);
2082 di->symt = &symt_add_func_constant(subpgm->ctx->module_ctx->module,
2083 subpgm->current_func, subpgm->current_block,
2084 param_type, name.u.string, &v)->symt;
2086 else
2087 di->symt = &symt_new_constant(subpgm->ctx->module_ctx->module, subpgm->ctx->compiland,
2088 name.u.string, param_type, &v)->symt;
2090 else
2092 if (subpgm->current_func)
2094 /* local variable has been optimized away... report anyway */
2095 loc.kind = loc_error;
2096 loc.reg = loc_err_no_location;
2097 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->current_func,
2098 is_pmt ? DataIsParam : DataIsLocal,
2099 &loc, subpgm->current_block, param_type, name.u.string);
2101 else
2103 struct attribute is_decl;
2104 /* only warn when di doesn't represent a declaration */
2105 if (!dwarf2_find_attribute(di, DW_AT_declaration, &is_decl) ||
2106 !is_decl.u.uvalue || is_decl.gotten_from != attr_direct)
2107 WARN("dropping global variable %s which has been optimized away\n", debugstr_a(name.u.string));
2110 if (is_pmt && subpgm->current_func && symt_check_tag(subpgm->current_func->type, SymTagFunctionType))
2111 symt_add_function_signature_parameter(subpgm->ctx->module_ctx->module,
2112 (struct symt_function_signature*)subpgm->current_func->type,
2113 param_type);
2115 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
2118 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
2119 const dwarf2_debug_info_t* di)
2121 struct attribute name;
2122 struct attribute low_pc;
2123 struct location loc;
2125 TRACE("%s\n", dwarf2_debug_di(di));
2127 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2128 name.u.string = NULL;
2129 if (dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc))
2131 loc.kind = loc_absolute;
2132 loc.offset = subpgm->ctx->module_ctx->load_offset + low_pc.u.uvalue - subpgm->top_func->address;
2133 symt_add_function_point(subpgm->ctx->module_ctx->module, subpgm->top_func, SymTagLabel,
2134 &loc, name.u.string);
2136 else
2137 WARN("Label %s inside function %s doesn't have an address... don't register it\n",
2138 name.u.string, subpgm->top_func->hash_elt.name);
2141 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
2142 dwarf2_debug_info_t* di);
2144 static struct symt* dwarf2_parse_subroutine_type(dwarf2_debug_info_t* di);
2146 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
2147 dwarf2_debug_info_t* di)
2149 struct attribute name;
2150 ULONG_PTR low_pc, high_pc;
2151 struct symt* ret_type;
2152 struct symt_function_signature* sig_type;
2153 struct symt_inlinesite* inlined;
2154 struct vector* children;
2155 dwarf2_debug_info_t*child;
2156 unsigned int i;
2157 struct addr_range* adranges;
2158 unsigned num_adranges;
2160 TRACE("%s\n", dwarf2_debug_di(di));
2162 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
2164 WARN("cannot read range\n");
2165 return;
2167 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2169 FIXME("No name for function... dropping function\n");
2170 return;
2172 ret_type = dwarf2_lookup_type(di);
2174 /* FIXME: assuming C source code */
2175 sig_type = symt_new_function_signature(subpgm->ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2177 inlined = symt_new_inlinesite(subpgm->ctx->module_ctx->module,
2178 subpgm->top_func,
2179 subpgm->current_block ? &subpgm->current_block->symt : &subpgm->current_func->symt,
2180 dwarf2_get_cpp_name(di, name.u.string),
2181 subpgm->ctx->module_ctx->load_offset + low_pc,
2182 &sig_type->symt);
2183 subpgm->current_func = (struct symt_function*)inlined;
2184 subpgm->current_block = NULL;
2186 if ((adranges = dwarf2_get_ranges(di, &num_adranges)) != NULL)
2188 for (i = 0; i < num_adranges; ++i)
2189 symt_add_inlinesite_range(subpgm->ctx->module_ctx->module, inlined,
2190 adranges[i].low, adranges[i].high);
2191 free(adranges);
2193 else
2194 WARN("cannot read ranges\n");
2196 children = dwarf2_get_di_children(di);
2197 if (children) for (i = 0; i < vector_length(children); i++)
2199 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2201 switch (child->abbrev->tag)
2203 case DW_TAG_formal_parameter:
2204 case DW_TAG_variable:
2205 dwarf2_parse_variable(subpgm, child);
2206 break;
2207 case DW_TAG_lexical_block:
2208 dwarf2_parse_subprogram_block(subpgm, child);
2209 break;
2210 case DW_TAG_inlined_subroutine:
2211 dwarf2_parse_inlined_subroutine(subpgm, child);
2212 break;
2213 case DW_TAG_label:
2214 dwarf2_parse_subprogram_label(subpgm, child);
2215 break;
2216 case DW_TAG_GNU_call_site:
2217 /* this isn't properly supported by dbghelp interface. skip it for now */
2218 break;
2219 default:
2220 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2221 child->abbrev->tag, dwarf2_debug_di(di));
2224 subpgm->current_block = symt_check_tag(subpgm->current_func->container, SymTagBlock) ?
2225 (struct symt_block*)subpgm->current_func->container : NULL;
2226 subpgm->current_func = (struct symt_function*)symt_get_upper_inlined((struct symt_inlinesite*)subpgm->current_func);
2229 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
2230 dwarf2_debug_info_t* di)
2232 ULONG_PTR low_pc, high_pc;
2233 struct vector* children;
2234 dwarf2_debug_info_t*child;
2235 unsigned int i;
2237 TRACE("%s\n", dwarf2_debug_di(di));
2239 if (!dwarf2_read_range(subpgm->ctx, di, &low_pc, &high_pc))
2241 WARN("no range\n");
2242 return;
2245 subpgm->current_block = symt_open_func_block(subpgm->ctx->module_ctx->module, subpgm->current_func, subpgm->current_block,
2246 subpgm->ctx->module_ctx->load_offset + low_pc - subpgm->current_func->address,
2247 high_pc - low_pc);
2249 children = dwarf2_get_di_children(di);
2250 if (children) for (i = 0; i < vector_length(children); i++)
2252 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2254 switch (child->abbrev->tag)
2256 case DW_TAG_inlined_subroutine:
2257 dwarf2_parse_inlined_subroutine(subpgm, child);
2258 break;
2259 case DW_TAG_variable:
2260 dwarf2_parse_variable(subpgm, child);
2261 break;
2262 case DW_TAG_pointer_type:
2263 dwarf2_parse_pointer_type(child);
2264 break;
2265 case DW_TAG_subroutine_type:
2266 dwarf2_parse_subroutine_type(child);
2267 break;
2268 case DW_TAG_const_type:
2269 dwarf2_parse_const_type(child);
2270 break;
2271 case DW_TAG_lexical_block:
2272 dwarf2_parse_subprogram_block(subpgm, child);
2273 break;
2274 case DW_TAG_subprogram:
2275 /* FIXME: likely a declaration (to be checked)
2276 * skip it for now
2278 break;
2279 case DW_TAG_formal_parameter:
2280 /* FIXME: likely elements for exception handling (GCC flavor)
2281 * Skip it for now
2283 break;
2284 case DW_TAG_imported_module:
2285 /* C++ stuff to be silenced (for now) */
2286 break;
2287 case DW_TAG_GNU_call_site:
2288 /* this isn't properly supported by dbghelp interface. skip it for now */
2289 break;
2290 case DW_TAG_label:
2291 dwarf2_parse_subprogram_label(subpgm, child);
2292 break;
2293 case DW_TAG_class_type:
2294 case DW_TAG_structure_type:
2295 case DW_TAG_union_type:
2296 case DW_TAG_enumeration_type:
2297 case DW_TAG_typedef:
2298 /* the type referred to will be loaded when we need it, so skip it */
2299 break;
2300 default:
2301 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2302 child->abbrev->tag, dwarf2_debug_di(di));
2306 symt_close_func_block(subpgm->ctx->module_ctx->module, subpgm->current_func, subpgm->current_block, 0);
2307 subpgm->current_block = symt_check_tag(subpgm->current_block->container, SymTagBlock) ?
2308 (struct symt_block*)subpgm->current_block->container : NULL;
2311 static struct symt* dwarf2_parse_subprogram(dwarf2_debug_info_t* di)
2313 struct attribute name;
2314 struct addr_range* addr_ranges;
2315 unsigned num_addr_ranges;
2316 struct attribute is_decl;
2317 struct attribute inline_flags;
2318 struct symt* ret_type;
2319 struct symt_function_signature* sig_type;
2320 dwarf2_subprogram_t subpgm;
2321 struct vector* children;
2322 dwarf2_debug_info_t* child;
2323 unsigned int i;
2325 if (di->symt) return di->symt;
2327 TRACE("%s\n", dwarf2_debug_di(di));
2329 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2331 WARN("No name for function... dropping function\n");
2332 return NULL;
2334 /* if it's an abstract representation of an inline function, there should be
2335 * a concrete object that we'll handle
2337 if (dwarf2_find_attribute(di, DW_AT_inline, &inline_flags) &&
2338 inline_flags.gotten_from == attr_direct &&
2339 inline_flags.u.uvalue != DW_INL_not_inlined)
2341 TRACE("Function %s declared as inlined (%Id)... skipping\n",
2342 debugstr_a(name.u.string), inline_flags.u.uvalue);
2343 return NULL;
2346 if (dwarf2_find_attribute(di, DW_AT_declaration, &is_decl) &&
2347 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
2349 /* it's a real declaration, skip it */
2350 return NULL;
2352 if ((addr_ranges = dwarf2_get_ranges(di, &num_addr_ranges)) == NULL)
2354 WARN("cannot get range for %s\n", debugstr_a(name.u.string));
2355 return NULL;
2357 /* As functions (defined as inline assembly) get debug info with dwarf
2358 * (not the case for stabs), we just drop Wine's thunks here...
2359 * Actual thunks will be created in elf_module from the symbol table
2361 if (elf_is_in_thunk_area(di->unit_ctx->module_ctx->load_offset + addr_ranges[0].low, di->unit_ctx->module_ctx->thunks) >= 0)
2363 free(addr_ranges);
2364 return NULL;
2366 ret_type = dwarf2_lookup_type(di);
2368 /* FIXME: assuming C source code */
2369 sig_type = symt_new_function_signature(di->unit_ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2370 subpgm.top_func = symt_new_function(di->unit_ctx->module_ctx->module, di->unit_ctx->compiland,
2371 dwarf2_get_cpp_name(di, name.u.string),
2372 addr_ranges[0].low, addr_ranges[0].high - addr_ranges[0].low, &sig_type->symt);
2373 if (num_addr_ranges > 1)
2374 WARN("Function %s has multiple address ranges, only using the first one\n", name.u.string);
2375 free(addr_ranges);
2377 subpgm.current_func = subpgm.top_func;
2378 di->symt = &subpgm.top_func->symt;
2379 subpgm.ctx = di->unit_ctx;
2380 if (!dwarf2_compute_location_attr(di->unit_ctx, di, DW_AT_frame_base,
2381 &subpgm.frame, NULL))
2383 /* on stack !! */
2384 subpgm.frame.kind = loc_regrel;
2385 subpgm.frame.reg = di->unit_ctx->module_ctx->module->cpu->frame_regno;
2386 subpgm.frame.offset = 0;
2388 subpgm.non_computed_variable = FALSE;
2389 subpgm.current_block = NULL;
2391 children = dwarf2_get_di_children(di);
2392 if (children) for (i = 0; i < vector_length(children); i++)
2394 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2396 switch (child->abbrev->tag)
2398 case DW_TAG_variable:
2399 case DW_TAG_formal_parameter:
2400 dwarf2_parse_variable(&subpgm, child);
2401 break;
2402 case DW_TAG_lexical_block:
2403 dwarf2_parse_subprogram_block(&subpgm, child);
2404 break;
2405 case DW_TAG_inlined_subroutine:
2406 dwarf2_parse_inlined_subroutine(&subpgm, child);
2407 break;
2408 case DW_TAG_pointer_type:
2409 dwarf2_parse_pointer_type(child);
2410 break;
2411 case DW_TAG_const_type:
2412 dwarf2_parse_const_type(child);
2413 break;
2414 case DW_TAG_subprogram:
2415 /* FIXME: likely a declaration (to be checked)
2416 * skip it for now
2418 break;
2419 case DW_TAG_label:
2420 dwarf2_parse_subprogram_label(&subpgm, child);
2421 break;
2422 case DW_TAG_class_type:
2423 case DW_TAG_structure_type:
2424 case DW_TAG_union_type:
2425 case DW_TAG_enumeration_type:
2426 case DW_TAG_typedef:
2427 /* the type referred to will be loaded when we need it, so skip it */
2428 break;
2429 case DW_TAG_unspecified_parameters:
2430 case DW_TAG_template_type_param:
2431 case DW_TAG_template_value_param:
2432 case DW_TAG_GNU_call_site:
2433 case DW_TAG_GNU_template_parameter_pack:
2434 case DW_TAG_GNU_formal_parameter_pack:
2435 /* FIXME: no support in dbghelp's internals so far */
2436 break;
2437 default:
2438 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2439 child->abbrev->tag, dwarf2_debug_di(di));
2443 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2445 symt_add_function_point(di->unit_ctx->module_ctx->module, subpgm.top_func, SymTagCustom,
2446 &subpgm.frame, NULL);
2449 return di->symt;
2452 static struct symt* dwarf2_parse_subroutine_type(dwarf2_debug_info_t* di)
2454 struct symt* ret_type;
2455 struct symt_function_signature* sig_type;
2456 struct vector* children;
2457 dwarf2_debug_info_t* child;
2458 unsigned int i;
2460 if (di->symt) return di->symt;
2462 TRACE("%s\n", dwarf2_debug_di(di));
2464 ret_type = dwarf2_lookup_type(di);
2466 /* FIXME: assuming C source code */
2467 sig_type = symt_new_function_signature(di->unit_ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2469 children = dwarf2_get_di_children(di);
2470 if (children) for (i = 0; i < vector_length(children); i++)
2472 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2474 switch (child->abbrev->tag)
2476 case DW_TAG_formal_parameter:
2477 symt_add_function_signature_parameter(di->unit_ctx->module_ctx->module, sig_type,
2478 dwarf2_lookup_type(child));
2479 break;
2480 case DW_TAG_unspecified_parameters:
2481 WARN("Unsupported unspecified parameters\n");
2482 break;
2486 return di->symt = &sig_type->symt;
2489 static void dwarf2_parse_namespace(dwarf2_debug_info_t* di)
2491 struct vector* children;
2492 dwarf2_debug_info_t* child;
2493 unsigned int i;
2495 if (di->symt) return;
2497 TRACE("%s\n", dwarf2_debug_di(di));
2499 di->symt = &symt_get_basic(btVoid, 0)->symt;
2501 children = dwarf2_get_di_children(di);
2502 if (children) for (i = 0; i < vector_length(children); i++)
2504 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2505 dwarf2_load_one_entry(child);
2509 static void dwarf2_parse_imported_unit(dwarf2_debug_info_t* di)
2511 struct attribute imp;
2513 if (di->symt) return;
2515 TRACE("%s\n", dwarf2_debug_di(di));
2517 if (dwarf2_find_attribute(di, DW_AT_import, &imp))
2519 dwarf2_debug_info_t* jmp = dwarf2_jump_to_debug_info(&imp);
2520 if (jmp) di->symt = jmp->symt;
2521 else FIXME("Couldn't load imported CU\n");
2523 else
2524 FIXME("Couldn't find import attribute\n");
2527 static void dwarf2_load_one_entry(dwarf2_debug_info_t* di)
2529 switch (di->abbrev->tag)
2531 case DW_TAG_typedef:
2532 dwarf2_parse_typedef(di);
2533 break;
2534 case DW_TAG_base_type:
2535 dwarf2_parse_base_type(di);
2536 break;
2537 case DW_TAG_pointer_type:
2538 dwarf2_parse_pointer_type(di);
2539 break;
2540 case DW_TAG_class_type:
2541 dwarf2_parse_udt_type(di, UdtClass);
2542 break;
2543 case DW_TAG_structure_type:
2544 dwarf2_parse_udt_type(di, UdtStruct);
2545 break;
2546 case DW_TAG_union_type:
2547 dwarf2_parse_udt_type(di, UdtUnion);
2548 break;
2549 case DW_TAG_array_type:
2550 dwarf2_parse_array_type(di);
2551 break;
2552 case DW_TAG_const_type:
2553 dwarf2_parse_const_type(di);
2554 break;
2555 case DW_TAG_volatile_type:
2556 dwarf2_parse_volatile_type(di);
2557 break;
2558 case DW_TAG_restrict_type:
2559 dwarf2_parse_restrict_type(di);
2560 break;
2561 case DW_TAG_unspecified_type:
2562 dwarf2_parse_unspecified_type(di);
2563 break;
2564 case DW_TAG_reference_type:
2565 case DW_TAG_rvalue_reference_type:
2566 dwarf2_parse_reference_type(di);
2567 break;
2568 case DW_TAG_enumeration_type:
2569 dwarf2_parse_enumeration_type(di);
2570 break;
2571 case DW_TAG_subprogram:
2572 dwarf2_parse_subprogram(di);
2573 break;
2574 case DW_TAG_subroutine_type:
2575 dwarf2_parse_subroutine_type(di);
2576 break;
2577 case DW_TAG_variable:
2579 dwarf2_subprogram_t subpgm;
2581 subpgm.ctx = di->unit_ctx;
2582 subpgm.top_func = subpgm.current_func = NULL;
2583 subpgm.current_block = NULL;
2584 subpgm.frame.kind = loc_absolute;
2585 subpgm.frame.offset = 0;
2586 subpgm.frame.reg = Wine_DW_no_register;
2587 dwarf2_parse_variable(&subpgm, di);
2589 break;
2590 case DW_TAG_namespace:
2591 dwarf2_parse_namespace(di);
2592 break;
2593 case DW_TAG_subrange_type:
2594 dwarf2_parse_subrange_type(di);
2595 break;
2596 case DW_TAG_imported_unit:
2597 dwarf2_parse_imported_unit(di);
2598 break;
2599 /* keep it silent until we need DW_OP_call_xxx support */
2600 case DW_TAG_dwarf_procedure:
2601 /* silence a couple of non-C language defines (mainly C++ but others too) */
2602 case DW_TAG_imported_module:
2603 case DW_TAG_imported_declaration:
2604 case DW_TAG_interface_type:
2605 case DW_TAG_module:
2606 case DW_TAG_ptr_to_member_type:
2607 break;
2608 default:
2609 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2610 di->abbrev->tag, dwarf2_debug_di(di));
2614 static void dwarf2_set_line_number(struct module* module, ULONG_PTR address,
2615 const struct vector* v, unsigned file, unsigned line)
2617 struct symt_function* func;
2618 struct symt_inlinesite* inlined;
2619 struct symt_ht* symt;
2620 unsigned* psrc;
2622 if (!file || !(psrc = vector_at(v, file - 1))) return;
2624 TRACE("%s %Ix %s %u\n",
2625 debugstr_w(module->modulename), address, debugstr_a(source_get(module, *psrc)), line);
2626 symt = symt_find_symbol_at(module, address);
2627 if (symt_check_tag(&symt->symt, SymTagFunction))
2629 func = (struct symt_function*)symt;
2630 for (inlined = func->next_inlinesite; inlined; inlined = inlined->func.next_inlinesite)
2632 int i;
2633 for (i = 0; i < inlined->vranges.num_elts; ++i)
2635 struct addr_range* ar = (struct addr_range*)vector_at(&inlined->vranges, i);
2636 if (ar->low <= address && address < ar->high)
2638 symt_add_func_line(module, &inlined->func, *psrc, line, address);
2639 return; /* only add to lowest matching inline site */
2643 symt_add_func_line(module, func, *psrc, line, address);
2647 static BOOL dwarf2_parse_line_numbers(dwarf2_parse_context_t* ctx,
2648 const char* compile_dir,
2649 ULONG_PTR offset)
2651 dwarf2_traverse_context_t traverse;
2652 ULONG_PTR length;
2653 unsigned insn_size, version, default_stmt;
2654 unsigned line_range, opcode_base;
2655 int line_base;
2656 unsigned char offset_size;
2657 const unsigned char* opcode_len;
2658 struct vector dirs;
2659 struct vector files;
2660 const char** p;
2662 /* section with line numbers stripped */
2663 if (ctx->module_ctx->sections[section_line].address == IMAGE_NO_MAP)
2664 return FALSE;
2666 if (offset + 4 > ctx->module_ctx->sections[section_line].size)
2668 WARN("out of bounds offset\n");
2669 return FALSE;
2671 traverse.data = ctx->module_ctx->sections[section_line].address + offset;
2672 traverse.end_data = ctx->module_ctx->sections[section_line].address + ctx->module_ctx->sections[section_line].size;
2674 length = dwarf2_parse_3264(&traverse, &offset_size);
2675 if (offset_size != ctx->head.offset_size)
2677 WARN("Mismatch in 32/64 bit format\n");
2678 return FALSE;
2680 traverse.end_data = traverse.data + length;
2682 if (traverse.end_data > ctx->module_ctx->sections[section_line].address + ctx->module_ctx->sections[section_line].size)
2684 WARN("out of bounds header\n");
2685 return FALSE;
2687 version = dwarf2_parse_u2(&traverse);
2688 dwarf2_parse_offset(&traverse, offset_size); /* header_len */
2689 insn_size = dwarf2_parse_byte(&traverse);
2690 if (version >= 4)
2691 dwarf2_parse_byte(&traverse); /* max_operations_per_instructions */
2692 default_stmt = dwarf2_parse_byte(&traverse);
2693 line_base = (signed char)dwarf2_parse_byte(&traverse);
2694 line_range = dwarf2_parse_byte(&traverse);
2695 opcode_base = dwarf2_parse_byte(&traverse);
2697 opcode_len = traverse.data;
2698 traverse.data += opcode_base - 1;
2700 vector_init(&dirs, sizeof(const char*), 4);
2701 p = vector_add(&dirs, &ctx->pool);
2702 *p = compile_dir ? compile_dir : ".";
2703 while (traverse.data < traverse.end_data && *traverse.data)
2705 const char* rel = (const char*)traverse.data;
2706 unsigned rellen = strlen(rel);
2707 TRACE("Got include %s\n", debugstr_a(rel));
2708 traverse.data += rellen + 1;
2709 p = vector_add(&dirs, &ctx->pool);
2711 if (*rel == '/' || !compile_dir || !*compile_dir)
2712 *p = rel;
2713 else
2715 /* include directory relative to compile directory */
2716 unsigned baselen = strlen(compile_dir);
2717 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2718 strcpy(tmp, compile_dir);
2719 if (baselen && tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2720 strcpy(&tmp[baselen], rel);
2721 *p = tmp;
2725 traverse.data++;
2727 vector_init(&files, sizeof(unsigned), 16);
2728 while (traverse.data < traverse.end_data && *traverse.data)
2730 unsigned int dir_index, mod_time;
2731 const char* name;
2732 const char* dir;
2733 unsigned* psrc;
2735 name = (const char*)traverse.data;
2736 traverse.data += strlen(name) + 1;
2737 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2738 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2739 length = dwarf2_leb128_as_unsigned(&traverse);
2740 dir = *(const char**)vector_at(&dirs, dir_index);
2741 TRACE("Got file %s/%s (%u,%Iu)\n", debugstr_a(dir), debugstr_a(name), mod_time, length);
2742 psrc = vector_add(&files, &ctx->pool);
2743 *psrc = source_new(ctx->module_ctx->module, dir, name);
2745 traverse.data++;
2747 while (traverse.data < traverse.end_data && *traverse.data)
2749 ULONG_PTR address = 0;
2750 unsigned file = 1;
2751 unsigned line = 1;
2752 unsigned is_stmt = default_stmt;
2753 BOOL end_sequence = FALSE;
2754 unsigned opcode, extopcode, i;
2756 while (!end_sequence)
2758 opcode = dwarf2_parse_byte(&traverse);
2759 TRACE("Got opcode %x\n", opcode);
2761 if (opcode >= opcode_base)
2763 unsigned delta = opcode - opcode_base;
2765 address += (delta / line_range) * insn_size;
2766 line += line_base + (delta % line_range);
2767 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2769 else
2771 switch (opcode)
2773 case DW_LNS_copy:
2774 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2775 break;
2776 case DW_LNS_advance_pc:
2777 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2778 break;
2779 case DW_LNS_advance_line:
2780 line += dwarf2_leb128_as_signed(&traverse);
2781 break;
2782 case DW_LNS_set_file:
2783 file = dwarf2_leb128_as_unsigned(&traverse);
2784 break;
2785 case DW_LNS_set_column:
2786 dwarf2_leb128_as_unsigned(&traverse);
2787 break;
2788 case DW_LNS_negate_stmt:
2789 is_stmt = !is_stmt;
2790 break;
2791 case DW_LNS_set_basic_block:
2792 break;
2793 case DW_LNS_const_add_pc:
2794 address += ((255 - opcode_base) / line_range) * insn_size;
2795 break;
2796 case DW_LNS_fixed_advance_pc:
2797 address += dwarf2_parse_u2(&traverse);
2798 break;
2799 case DW_LNS_extended_op:
2800 dwarf2_leb128_as_unsigned(&traverse);
2801 extopcode = dwarf2_parse_byte(&traverse);
2802 switch (extopcode)
2804 case DW_LNE_end_sequence:
2805 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2806 end_sequence = TRUE;
2807 break;
2808 case DW_LNE_set_address:
2809 address = ctx->module_ctx->load_offset + dwarf2_parse_addr_head(&traverse, &ctx->head);
2810 break;
2811 case DW_LNE_define_file:
2812 FIXME("not handled define file %s\n", debugstr_a((char *)traverse.data));
2813 traverse.data += strlen((const char *)traverse.data) + 1;
2814 dwarf2_leb128_as_unsigned(&traverse);
2815 dwarf2_leb128_as_unsigned(&traverse);
2816 dwarf2_leb128_as_unsigned(&traverse);
2817 break;
2818 case DW_LNE_set_discriminator:
2820 unsigned descr;
2822 descr = dwarf2_leb128_as_unsigned(&traverse);
2823 WARN("not handled discriminator %x\n", descr);
2825 break;
2826 default:
2827 FIXME("Unsupported extended opcode %x\n", extopcode);
2828 break;
2830 break;
2831 default:
2832 WARN("Unsupported opcode %x\n", opcode);
2833 for (i = 0; i < opcode_len[opcode]; i++)
2834 dwarf2_leb128_as_unsigned(&traverse);
2835 break;
2840 return TRUE;
2843 unsigned dwarf2_cache_cuhead(struct dwarf2_module_info_s* module, struct symt_compiland* c, const dwarf2_cuhead_t* head)
2845 dwarf2_cuhead_t* ah;
2846 unsigned i;
2847 for (i = 0; i < module->num_cuheads; ++i)
2849 if (memcmp(module->cuheads[i], head, sizeof(*head)) == 0)
2851 c->user = module->cuheads[i];
2852 return TRUE;
2855 if (!(ah = pool_alloc(&c->container->module->pool, sizeof(*head)))) return FALSE;
2856 memcpy(ah, head, sizeof(*head));
2857 module->cuheads = realloc(module->cuheads, ++module->num_cuheads * sizeof(head));
2858 module->cuheads[module->num_cuheads - 1] = ah;
2859 c->user = ah;
2860 return TRUE;
2863 static dwarf2_parse_context_t* dwarf2_locate_cu(dwarf2_parse_module_context_t* module_ctx, ULONG_PTR ref)
2865 unsigned i;
2866 dwarf2_parse_context_t* ctx;
2867 const BYTE* where;
2868 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
2870 ctx = vector_at(&module_ctx->unit_contexts, i);
2871 where = module_ctx->sections[ctx->section].address + ref;
2872 if (where >= ctx->traverse_DIE.data && where < ctx->traverse_DIE.end_data)
2873 return ctx;
2875 FIXME("Couldn't find ref 0x%Ix inside sect\n", ref);
2876 return NULL;
2879 static BOOL dwarf2_parse_compilation_unit_head(dwarf2_parse_context_t* ctx,
2880 dwarf2_traverse_context_t* mod_ctx)
2882 dwarf2_traverse_context_t abbrev_ctx;
2883 const unsigned char* comp_unit_start = mod_ctx->data;
2884 ULONG_PTR cu_length;
2885 ULONG_PTR cu_abbrev_offset;
2886 /* FIXME this is a temporary configuration while adding support for dwarf3&4 bits */
2887 static LONG max_supported_dwarf_version = 0;
2889 cu_length = dwarf2_parse_3264(mod_ctx, &ctx->head.offset_size);
2891 ctx->traverse_DIE.data = mod_ctx->data;
2892 ctx->traverse_DIE.end_data = mod_ctx->data + cu_length;
2893 mod_ctx->data += cu_length;
2894 ctx->head.version = dwarf2_parse_u2(&ctx->traverse_DIE);
2895 cu_abbrev_offset = dwarf2_parse_offset(&ctx->traverse_DIE, ctx->head.offset_size);
2896 ctx->head.word_size = dwarf2_parse_byte(&ctx->traverse_DIE);
2897 ctx->status = UNIT_ERROR;
2899 TRACE("Compilation Unit Header found at 0x%x:\n",
2900 (int)(comp_unit_start - ctx->module_ctx->sections[section_debug].address));
2901 TRACE("- length: %Iu\n", cu_length);
2902 TRACE("- version: %u\n", ctx->head.version);
2903 TRACE("- abbrev_offset: %Iu\n", cu_abbrev_offset);
2904 TRACE("- word_size: %u\n", ctx->head.word_size);
2905 TRACE("- offset_size: %u\n", ctx->head.offset_size);
2907 if (ctx->head.version >= 2)
2908 ctx->module_ctx->cu_versions |= 1 << (ctx->head.version - 2);
2909 if (max_supported_dwarf_version == 0)
2911 char* env = getenv("DBGHELP_DWARF_VERSION");
2912 LONG v = env ? atol(env) : 4;
2913 max_supported_dwarf_version = (v >= 2 && v <= 4) ? v : 4;
2916 if (ctx->head.version < 2 || ctx->head.version > max_supported_dwarf_version)
2918 WARN("DWARF version %d isn't supported. Wine dbghelp only supports DWARF 2 up to %lu.\n",
2919 ctx->head.version, max_supported_dwarf_version);
2920 return FALSE;
2923 pool_init(&ctx->pool, 65536);
2924 ctx->section = section_debug;
2925 ctx->ref_offset = comp_unit_start - ctx->module_ctx->sections[section_debug].address;
2926 ctx->cpp_name = NULL;
2927 ctx->status = UNIT_NOTLOADED;
2929 abbrev_ctx.data = ctx->module_ctx->sections[section_abbrev].address + cu_abbrev_offset;
2930 abbrev_ctx.end_data = ctx->module_ctx->sections[section_abbrev].address + ctx->module_ctx->sections[section_abbrev].size;
2931 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx->abbrev_table, &ctx->pool);
2933 sparse_array_init(&ctx->debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2934 return TRUE;
2937 static BOOL dwarf2_parse_compilation_unit(dwarf2_parse_context_t* ctx)
2939 dwarf2_debug_info_t* di;
2940 dwarf2_traverse_context_t cu_ctx = ctx->traverse_DIE;
2941 BOOL ret = FALSE;
2943 switch (ctx->status)
2945 case UNIT_ERROR: return FALSE;
2946 case UNIT_BEINGLOADED:
2947 FIXME("Circular deps on CU\n");
2948 /* fall through */
2949 case UNIT_LOADED:
2950 case UNIT_LOADED_FAIL:
2951 return TRUE;
2952 case UNIT_NOTLOADED: break;
2955 ctx->status = UNIT_BEINGLOADED;
2956 if (dwarf2_read_one_debug_info(ctx, &cu_ctx, NULL, &di))
2958 if (di->abbrev->tag == DW_TAG_compile_unit || di->abbrev->tag == DW_TAG_partial_unit)
2960 struct attribute name;
2961 struct vector* children;
2962 dwarf2_debug_info_t* child = NULL;
2963 unsigned int i;
2964 struct attribute stmt_list, low_pc;
2965 struct attribute comp_dir;
2966 struct attribute language;
2968 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2969 name.u.string = NULL;
2971 /* get working directory of current compilation unit */
2972 if (!dwarf2_find_attribute(di, DW_AT_comp_dir, &comp_dir))
2973 comp_dir.u.string = NULL;
2975 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc))
2976 low_pc.u.uvalue = 0;
2978 if (!dwarf2_find_attribute(di, DW_AT_language, &language))
2979 language.u.uvalue = DW_LANG_C;
2981 ctx->language = language.u.uvalue;
2983 ctx->compiland = symt_new_compiland(ctx->module_ctx->module, ctx->module_ctx->load_offset + low_pc.u.uvalue,
2984 source_new(ctx->module_ctx->module, comp_dir.u.string, name.u.string));
2985 dwarf2_cache_cuhead(ctx->module_ctx->module->format_info[DFI_DWARF]->u.dwarf2_info, ctx->compiland, &ctx->head);
2986 di->symt = &ctx->compiland->symt;
2987 children = dwarf2_get_di_children(di);
2988 if (children) for (i = 0; i < vector_length(children); i++)
2990 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2991 dwarf2_load_one_entry(child);
2993 if (dwarf2_find_attribute(di, DW_AT_stmt_list, &stmt_list))
2995 if (dwarf2_parse_line_numbers(ctx, comp_dir.u.string, stmt_list.u.uvalue))
2996 ctx->module_ctx->module->module.LineNumbers = TRUE;
2998 ctx->status = UNIT_LOADED;
2999 ret = TRUE;
3001 else FIXME("Should have a compilation unit here %Iu\n", di->abbrev->tag);
3003 if (ctx->status == UNIT_BEINGLOADED) ctx->status = UNIT_LOADED_FAIL;
3004 return ret;
3007 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const dwarf2_cuhead_t* head,
3008 const BYTE* start, ULONG_PTR ip, dwarf2_traverse_context_t* lctx)
3010 DWORD_PTR beg, end;
3011 const BYTE* ptr = start;
3012 DWORD len;
3014 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
3016 beg = dwarf2_get_addr(ptr, head->word_size); ptr += head->word_size;
3017 end = dwarf2_get_addr(ptr, head->word_size); ptr += head->word_size;
3018 if (!beg && !end) break;
3019 len = dwarf2_get_u2(ptr); ptr += 2;
3021 if (beg <= ip && ip < end)
3023 lctx->data = ptr;
3024 lctx->end_data = ptr + len;
3025 return TRUE;
3027 ptr += len;
3029 WARN("Couldn't find ip in location list\n");
3030 return FALSE;
3033 static const dwarf2_cuhead_t* get_cuhead_from_func(const struct symt_function* func)
3035 if (symt_check_tag(&func->symt, SymTagInlineSite))
3036 func = symt_get_function_from_inlined((struct symt_inlinesite*)func);
3037 if (symt_check_tag(&func->symt, SymTagFunction) && symt_check_tag(func->container, SymTagCompiland))
3039 struct symt_compiland* c = (struct symt_compiland*)func->container;
3040 return (const dwarf2_cuhead_t*)c->user;
3042 FIXME("Should have a compilation unit head\n");
3043 return NULL;
3046 static BOOL compute_call_frame_cfa(struct module* module, ULONG_PTR ip, struct location* frame);
3048 static enum location_error loc_compute_frame(struct process* pcs,
3049 const struct module_format* modfmt,
3050 const struct symt_function* func,
3051 DWORD_PTR ip, const dwarf2_cuhead_t* head,
3052 struct location* frame)
3054 struct symt** psym = NULL;
3055 struct location* pframe;
3056 dwarf2_traverse_context_t lctx;
3057 enum location_error err;
3058 unsigned int i;
3060 for (i=0; i<vector_length(&func->vchildren); i++)
3062 psym = vector_at(&func->vchildren, i);
3063 if (psym && symt_check_tag(*psym, SymTagCustom))
3065 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
3067 /* First, recompute the frame information, if needed */
3068 switch (pframe->kind)
3070 case loc_regrel:
3071 case loc_register:
3072 *frame = *pframe;
3073 break;
3074 case loc_dwarf2_location_list:
3075 WARN("Searching loclist for %s\n", debugstr_a(func->hash_elt.name));
3076 if (!dwarf2_lookup_loclist(modfmt, head,
3077 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
3078 ip, &lctx))
3079 return loc_err_out_of_scope;
3080 if ((err = compute_location(modfmt->module, head,
3081 &lctx, frame, pcs->handle, NULL)) < 0) return err;
3082 if (frame->kind >= loc_user)
3084 WARN("Couldn't compute runtime frame location\n");
3085 return loc_err_too_complex;
3087 break;
3088 case loc_dwarf2_frame_cfa:
3089 if (!compute_call_frame_cfa(modfmt->module, ip + ((struct symt_compiland*)func->container)->address, frame)) return loc_err_internal;
3090 break;
3091 default:
3092 WARN("Unsupported frame kind %d\n", pframe->kind);
3093 return loc_err_internal;
3095 return 0;
3098 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
3099 return loc_err_internal;
3102 enum reg_rule
3104 RULE_UNSET, /* not set at all */
3105 RULE_UNDEFINED, /* undefined value */
3106 RULE_SAME, /* same value as previous frame */
3107 RULE_CFA_OFFSET, /* stored at cfa offset */
3108 RULE_OTHER_REG, /* stored in other register */
3109 RULE_EXPRESSION, /* address specified by expression */
3110 RULE_VAL_EXPRESSION /* value specified by expression */
3113 /* make it large enough for all CPUs */
3114 #define NB_FRAME_REGS 64
3115 #define MAX_SAVED_STATES 16
3117 struct frame_state
3119 ULONG_PTR cfa_offset;
3120 unsigned char cfa_reg;
3121 enum reg_rule cfa_rule;
3122 enum reg_rule rules[NB_FRAME_REGS];
3123 ULONG_PTR regs[NB_FRAME_REGS];
3126 struct frame_info
3128 ULONG_PTR ip;
3129 ULONG_PTR code_align;
3130 LONG_PTR data_align;
3131 unsigned char retaddr_reg;
3132 unsigned char fde_encoding;
3133 unsigned char lsda_encoding;
3134 unsigned char signal_frame;
3135 unsigned char aug_z_format;
3136 unsigned char state_sp;
3137 struct frame_state state;
3138 struct frame_state state_stack[MAX_SAVED_STATES];
3141 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding, unsigned char word_size)
3143 ULONG_PTR base;
3145 if (encoding == DW_EH_PE_omit) return 0;
3147 switch (encoding & 0xf0)
3149 case DW_EH_PE_abs:
3150 base = 0;
3151 break;
3152 case DW_EH_PE_pcrel:
3153 base = (ULONG_PTR)ctx->data;
3154 break;
3155 default:
3156 FIXME("unsupported encoding %02x\n", encoding);
3157 return 0;
3160 switch (encoding & 0x0f)
3162 case DW_EH_PE_native:
3163 return base + dwarf2_parse_addr(ctx, word_size);
3164 case DW_EH_PE_leb128:
3165 return base + dwarf2_leb128_as_unsigned(ctx);
3166 case DW_EH_PE_data2:
3167 return base + dwarf2_parse_u2(ctx);
3168 case DW_EH_PE_data4:
3169 return base + dwarf2_parse_u4(ctx);
3170 case DW_EH_PE_data8:
3171 return base + dwarf2_parse_u8(ctx);
3172 case DW_EH_PE_signed|DW_EH_PE_leb128:
3173 return base + dwarf2_leb128_as_signed(ctx);
3174 case DW_EH_PE_signed|DW_EH_PE_data2:
3175 return base + (signed short)dwarf2_parse_u2(ctx);
3176 case DW_EH_PE_signed|DW_EH_PE_data4:
3177 return base + (signed int)dwarf2_parse_u4(ctx);
3178 case DW_EH_PE_signed|DW_EH_PE_data8:
3179 return base + (LONG64)dwarf2_parse_u8(ctx);
3180 default:
3181 FIXME("unsupported encoding %02x\n", encoding);
3182 return 0;
3186 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info, unsigned char word_size)
3188 unsigned char version;
3189 const char* augmentation;
3190 const unsigned char* end;
3191 ULONG_PTR len;
3193 memset(info, 0, sizeof(*info));
3194 info->lsda_encoding = DW_EH_PE_omit;
3195 info->aug_z_format = 0;
3197 /* parse the CIE first */
3198 version = dwarf2_parse_byte(ctx);
3199 if (version != 1 && version != 3 && version != 4)
3201 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
3202 return FALSE;
3204 augmentation = (const char*)ctx->data;
3205 ctx->data += strlen(augmentation) + 1;
3207 switch (version)
3209 case 4:
3210 /* skip 'address_size' and 'segment_size' */
3211 ctx->data += 2;
3212 /* fallthrough */
3213 case 1:
3214 case 3:
3215 info->code_align = dwarf2_leb128_as_unsigned(ctx);
3216 info->data_align = dwarf2_leb128_as_signed(ctx);
3217 info->retaddr_reg = version == 1 ? dwarf2_parse_byte(ctx) :dwarf2_leb128_as_unsigned(ctx);
3218 break;
3219 default:
3222 info->state.cfa_rule = RULE_CFA_OFFSET;
3224 end = NULL;
3225 TRACE("\tparsing augmentation %s\n", debugstr_a(augmentation));
3226 if (*augmentation) do
3228 switch (*augmentation)
3230 case 'z':
3231 len = dwarf2_leb128_as_unsigned(ctx);
3232 end = ctx->data + len;
3233 info->aug_z_format = 1;
3234 continue;
3235 case 'L':
3236 info->lsda_encoding = dwarf2_parse_byte(ctx);
3237 continue;
3238 case 'P':
3240 unsigned char encoding = dwarf2_parse_byte(ctx);
3241 /* throw away the indirect bit, as we don't care for the result */
3242 encoding &= ~DW_EH_PE_indirect;
3243 dwarf2_parse_augmentation_ptr(ctx, encoding, word_size); /* handler */
3244 continue;
3246 case 'R':
3247 info->fde_encoding = dwarf2_parse_byte(ctx);
3248 continue;
3249 case 'S':
3250 info->signal_frame = 1;
3251 continue;
3253 FIXME("unknown augmentation '%c'\n", *augmentation);
3254 if (!end) return FALSE;
3255 break;
3256 } while (*++augmentation);
3257 if (end) ctx->data = end;
3258 return TRUE;
3261 static BOOL dwarf2_get_cie(ULONG_PTR addr, struct module* module, DWORD_PTR delta,
3262 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
3263 struct frame_info* info, BOOL in_eh_frame)
3265 const unsigned char* ptr_blk;
3266 const unsigned char* cie_ptr;
3267 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
3268 ULONG_PTR len, id;
3269 ULONG_PTR start, range;
3270 ULONG_PTR cie_id;
3271 const BYTE* start_data = fde_ctx->data;
3272 unsigned char word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3273 unsigned char offset_size;
3275 /* skip 0-padding at beginning of section (alignment) */
3276 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
3278 if (dwarf2_parse_u4(fde_ctx))
3280 fde_ctx->data -= 4;
3281 break;
3284 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
3286 const unsigned char* st = fde_ctx->data;
3287 /* find the FDE for address addr (skip CIE) */
3288 len = dwarf2_parse_3264(fde_ctx, &offset_size);
3289 cie_id = in_eh_frame ? 0 : (offset_size == 4 ? DW_CIE_ID : (ULONG_PTR)DW64_CIE_ID);
3290 ptr_blk = fde_ctx->data + len;
3291 id = dwarf2_parse_offset(fde_ctx, offset_size);
3292 if (id == cie_id)
3294 last_cie_ptr = st;
3295 /* we need some bits out of the CIE in order to parse all contents */
3296 if (!parse_cie_details(fde_ctx, info, word_size)) return FALSE;
3297 cie_ctx->data = fde_ctx->data;
3298 cie_ctx->end_data = ptr_blk;
3299 continue;
3301 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
3302 if (cie_ptr != last_cie_ptr)
3304 last_cie_ptr = cie_ptr;
3305 cie_ctx->data = cie_ptr;
3306 cie_ctx->end_data = cie_ptr + (offset_size == 4 ? 4 : 4 + 8);
3307 cie_ctx->end_data += dwarf2_parse_3264(cie_ctx, &offset_size);
3309 if (dwarf2_parse_offset(cie_ctx, in_eh_frame ? word_size : offset_size) != cie_id)
3311 FIXME("wrong CIE pointer at %x from FDE %x\n",
3312 (unsigned)(cie_ptr - start_data),
3313 (unsigned)(fde_ctx->data - start_data));
3314 return FALSE;
3316 if (!parse_cie_details(cie_ctx, info, word_size)) return FALSE;
3318 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding, word_size);
3319 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F, word_size);
3321 if (addr >= start && addr < start + range)
3323 /* reset the FDE context */
3324 fde_ctx->end_data = ptr_blk;
3326 info->ip = start;
3327 return TRUE;
3330 return FALSE;
3333 static int valid_reg(ULONG_PTR reg)
3335 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %Ix\n", reg);
3336 return (reg < NB_FRAME_REGS);
3339 static void execute_cfa_instructions(struct module* module, dwarf2_traverse_context_t* ctx,
3340 ULONG_PTR last_ip, struct frame_info *info)
3342 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
3344 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
3346 if (op & 0xc0)
3348 switch (op & 0xc0)
3350 case DW_CFA_advance_loc:
3352 ULONG_PTR offset = (op & 0x3f) * info->code_align;
3353 TRACE("%Ix: DW_CFA_advance_loc %Iu\n", info->ip, offset);
3354 info->ip += offset;
3355 break;
3357 case DW_CFA_offset:
3359 ULONG_PTR reg = op & 0x3f;
3360 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
3361 if (!valid_reg(reg)) break;
3362 TRACE("%Ix: DW_CFA_offset %s, %Id\n",
3363 info->ip,
3364 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3365 offset);
3366 info->state.regs[reg] = offset;
3367 info->state.rules[reg] = RULE_CFA_OFFSET;
3368 break;
3370 case DW_CFA_restore:
3372 ULONG_PTR reg = op & 0x3f;
3373 if (!valid_reg(reg)) break;
3374 TRACE("%Ix: DW_CFA_restore %s\n",
3375 info->ip,
3376 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3377 info->state.rules[reg] = RULE_UNSET;
3378 break;
3382 else switch (op)
3384 case DW_CFA_nop:
3385 break;
3386 case DW_CFA_set_loc:
3388 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding,
3389 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
3390 TRACE("%Ix: DW_CFA_set_loc %Ix\n", info->ip, loc);
3391 info->ip = loc;
3392 break;
3394 case DW_CFA_advance_loc1:
3396 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
3397 TRACE("%Ix: DW_CFA_advance_loc1 %Iu\n", info->ip, offset);
3398 info->ip += offset;
3399 break;
3401 case DW_CFA_advance_loc2:
3403 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
3404 TRACE("%Ix: DW_CFA_advance_loc2 %Iu\n", info->ip, offset);
3405 info->ip += offset;
3406 break;
3408 case DW_CFA_advance_loc4:
3410 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
3411 TRACE("%Ix: DW_CFA_advance_loc4 %Iu\n", info->ip, offset);
3412 info->ip += offset;
3413 break;
3415 case DW_CFA_offset_extended:
3416 case DW_CFA_offset_extended_sf:
3418 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3419 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
3420 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3421 if (!valid_reg(reg)) break;
3422 TRACE("%Ix: DW_CFA_offset_extended %s, %Id\n",
3423 info->ip,
3424 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3425 offset);
3426 info->state.regs[reg] = offset;
3427 info->state.rules[reg] = RULE_CFA_OFFSET;
3428 break;
3430 case DW_CFA_restore_extended:
3432 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3433 if (!valid_reg(reg)) break;
3434 TRACE("%Ix: DW_CFA_restore_extended %s\n",
3435 info->ip,
3436 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3437 info->state.rules[reg] = RULE_UNSET;
3438 break;
3440 case DW_CFA_undefined:
3442 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3443 if (!valid_reg(reg)) break;
3444 TRACE("%Ix: DW_CFA_undefined %s\n",
3445 info->ip,
3446 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3447 info->state.rules[reg] = RULE_UNDEFINED;
3448 break;
3450 case DW_CFA_same_value:
3452 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3453 if (!valid_reg(reg)) break;
3454 TRACE("%Ix: DW_CFA_same_value %s\n",
3455 info->ip,
3456 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3457 info->state.regs[reg] = reg;
3458 info->state.rules[reg] = RULE_SAME;
3459 break;
3461 case DW_CFA_register:
3463 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3464 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
3465 if (!valid_reg(reg) || !valid_reg(reg2)) break;
3466 TRACE("%Ix: DW_CFA_register %s == %s\n",
3467 info->ip,
3468 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3469 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg2, module, TRUE)));
3470 info->state.regs[reg] = reg2;
3471 info->state.rules[reg] = RULE_OTHER_REG;
3472 break;
3474 case DW_CFA_remember_state:
3475 TRACE("%Ix: DW_CFA_remember_state\n", info->ip);
3476 if (info->state_sp >= MAX_SAVED_STATES)
3477 FIXME("%Ix: DW_CFA_remember_state too many nested saves\n", info->ip);
3478 else
3479 info->state_stack[info->state_sp++] = info->state;
3480 break;
3481 case DW_CFA_restore_state:
3482 TRACE("%Ix: DW_CFA_restore_state\n", info->ip);
3483 if (!info->state_sp)
3484 FIXME("%Ix: DW_CFA_restore_state without corresponding save\n", info->ip);
3485 else
3486 info->state = info->state_stack[--info->state_sp];
3487 break;
3488 case DW_CFA_def_cfa:
3489 case DW_CFA_def_cfa_sf:
3491 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3492 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
3493 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3494 if (!valid_reg(reg)) break;
3495 TRACE("%Ix: DW_CFA_def_cfa %s, %Id\n",
3496 info->ip,
3497 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3498 offset);
3499 info->state.cfa_reg = reg;
3500 info->state.cfa_offset = offset;
3501 info->state.cfa_rule = RULE_CFA_OFFSET;
3502 break;
3504 case DW_CFA_def_cfa_register:
3506 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3507 if (!valid_reg(reg)) break;
3508 TRACE("%Ix: DW_CFA_def_cfa_register %s\n",
3509 info->ip,
3510 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3511 info->state.cfa_reg = reg;
3512 info->state.cfa_rule = RULE_CFA_OFFSET;
3513 break;
3515 case DW_CFA_def_cfa_offset:
3516 case DW_CFA_def_cfa_offset_sf:
3518 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
3519 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3520 TRACE("%Ix: DW_CFA_def_cfa_offset %Id\n", info->ip, offset);
3521 info->state.cfa_offset = offset;
3522 info->state.cfa_rule = RULE_CFA_OFFSET;
3523 break;
3525 case DW_CFA_def_cfa_expression:
3527 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3528 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3529 TRACE("%Ix: DW_CFA_def_cfa_expression %Ix-%Ix\n", info->ip, expr, expr+len);
3530 info->state.cfa_offset = expr;
3531 info->state.cfa_rule = RULE_VAL_EXPRESSION;
3532 ctx->data += len;
3533 break;
3535 case DW_CFA_expression:
3536 case DW_CFA_val_expression:
3538 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3539 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3540 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3541 if (!valid_reg(reg)) break;
3542 TRACE("%Ix: DW_CFA_%sexpression %s %Ix-%Ix\n",
3543 info->ip, (op == DW_CFA_expression) ? "" : "val_",
3544 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3545 expr, expr + len);
3546 info->state.regs[reg] = expr;
3547 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
3548 ctx->data += len;
3549 break;
3551 case DW_CFA_GNU_args_size:
3552 /* FIXME: should check that GCC is the compiler for this CU */
3554 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
3555 TRACE("%Ix: DW_CFA_GNU_args_size %Iu\n", info->ip, args);
3556 /* ignored */
3557 break;
3559 default:
3560 FIXME("%Ix: unknown CFA opcode %02x\n", info->ip, op);
3561 break;
3566 /* retrieve a context register from its dwarf number */
3567 static DWORD64 get_context_reg(const struct module* module, struct cpu_stack_walk *csw, union ctx *context,
3568 ULONG_PTR dw_reg)
3570 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3571 void* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3573 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3574 if (sz == 8)
3575 return *(DWORD64 *)ptr;
3576 else if (sz == 4)
3577 return *(DWORD *)ptr;
3579 FIXME("unhandled size %d\n", sz);
3580 return 0;
3583 /* set a context register from its dwarf number */
3584 static void set_context_reg(const struct module* module, struct cpu_stack_walk* csw, union ctx *context,
3585 ULONG_PTR dw_reg, ULONG_PTR val, BOOL isdebuggee)
3587 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3588 ULONG_PTR* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3590 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3591 if (isdebuggee)
3593 char tmp[16];
3595 if (sz > sizeof(tmp))
3597 FIXME("register %Iu/%u size is too wide: %u\n", dw_reg, regno, sz);
3598 return;
3600 if (!sw_read_mem(csw, val, tmp, sz))
3602 WARN("Couldn't read memory at %p\n", (void*)val);
3603 return;
3605 memcpy(ptr, tmp, sz);
3607 else
3609 if (sz != sizeof(ULONG_PTR))
3611 FIXME("assigning to register %Iu/%u of wrong size %u\n", dw_reg, regno, sz);
3612 return;
3614 *ptr = val;
3618 /* copy a register from one context to another using dwarf number */
3619 static void copy_context_reg(const struct module* module, struct cpu_stack_walk *csw,
3620 union ctx *dstcontext, ULONG_PTR dwregdst,
3621 union ctx *srccontext, ULONG_PTR dwregsrc)
3623 unsigned regdstno = csw->cpu->map_dwarf_register(dwregdst, module, TRUE), szdst;
3624 unsigned regsrcno = csw->cpu->map_dwarf_register(dwregsrc, module, TRUE), szsrc;
3625 ULONG_PTR* ptrdst = csw->cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3626 ULONG_PTR* ptrsrc = csw->cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3628 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3629 if (szdst != szsrc)
3631 FIXME("Cannot copy register %Iu/%u => %Iu/%u because of size mismatch (%u => %u)\n",
3632 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3633 return;
3635 memcpy(ptrdst, ptrsrc, szdst);
3638 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3639 const unsigned char* zp, union ctx *context)
3641 dwarf2_traverse_context_t ctx;
3642 ULONG_PTR reg, sz, tmp;
3643 DWORD64 stack[64];
3644 int sp = -1;
3645 ULONG_PTR len;
3647 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3648 ctx.data = zp;
3649 ctx.end_data = zp + 4;
3650 len = dwarf2_leb128_as_unsigned(&ctx);
3651 ctx.end_data = ctx.data + len;
3653 while (ctx.data < ctx.end_data)
3655 unsigned char opcode = dwarf2_parse_byte(&ctx);
3657 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3658 stack[++sp] = opcode - DW_OP_lit0;
3659 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3660 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_reg0);
3661 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3662 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_breg0)
3663 + dwarf2_leb128_as_signed(&ctx);
3664 else switch (opcode)
3666 case DW_OP_nop: break;
3667 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size); break;
3668 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3669 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3670 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3671 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3672 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3673 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3674 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3675 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3676 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3677 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3678 case DW_OP_deref:
3679 tmp = 0;
3680 if (!sw_read_mem(csw, stack[sp], &tmp, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size))
3682 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3683 tmp = 0;
3685 stack[sp] = tmp;
3686 break;
3687 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3688 case DW_OP_drop: sp--; break;
3689 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3690 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3691 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3692 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3693 case DW_OP_abs: stack[sp] = sizeof(stack[sp]) == 8 ? llabs((INT64)stack[sp]) : abs((INT32)stack[sp]); break;
3694 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3695 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3696 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3697 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3698 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3699 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3700 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3701 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3702 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3703 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3704 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3705 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3706 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3707 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3708 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3709 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3710 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3711 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3712 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3713 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3714 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3715 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3716 case DW_OP_GNU_encoded_addr:
3717 tmp = dwarf2_parse_byte(&ctx);
3718 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
3719 break;
3720 case DW_OP_regx:
3721 stack[++sp] = get_context_reg(module, csw, context, dwarf2_leb128_as_unsigned(&ctx));
3722 break;
3723 case DW_OP_bregx:
3724 reg = dwarf2_leb128_as_unsigned(&ctx);
3725 tmp = dwarf2_leb128_as_signed(&ctx);
3726 stack[++sp] = get_context_reg(module, csw, context, reg) + tmp;
3727 break;
3728 case DW_OP_deref_size:
3729 sz = dwarf2_parse_byte(&ctx);
3730 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3732 ERR("Couldn't read memory at %s\n", wine_dbgstr_longlong(stack[sp]));
3733 tmp = 0;
3735 /* do integral promotion */
3736 switch (sz)
3738 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3739 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3740 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3741 case 8: stack[sp] = tmp; break; /* FIXME: won't work on 32bit platform */
3742 default: FIXME("Unknown size for deref 0x%Ix\n", sz);
3744 break;
3745 default:
3746 FIXME("unhandled opcode %02x\n", opcode);
3749 return stack[sp];
3752 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3753 union ctx *context, struct frame_state *state, DWORD64 *cfa)
3755 unsigned int i;
3756 ULONG_PTR value;
3757 union ctx new_context = *context;
3759 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3760 switch (state->cfa_rule)
3762 case RULE_EXPRESSION:
3763 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3764 if (!sw_read_mem(csw, *cfa, cfa, csw->cpu->word_size))
3766 WARN("Couldn't read memory at %s\n", wine_dbgstr_longlong(*cfa));
3767 return;
3769 break;
3770 case RULE_VAL_EXPRESSION:
3771 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3772 break;
3773 default:
3774 *cfa = get_context_reg(module, csw, context, state->cfa_reg) + state->cfa_offset;
3775 break;
3777 if (!*cfa) return;
3779 for (i = 0; i < NB_FRAME_REGS; i++)
3781 switch (state->rules[i])
3783 case RULE_UNSET:
3784 case RULE_UNDEFINED:
3785 case RULE_SAME:
3786 break;
3787 case RULE_CFA_OFFSET:
3788 set_context_reg(module, csw, &new_context, i, *cfa + state->regs[i], TRUE);
3789 break;
3790 case RULE_OTHER_REG:
3791 copy_context_reg(module, csw, &new_context, i, context, state->regs[i]);
3792 break;
3793 case RULE_EXPRESSION:
3794 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3795 set_context_reg(module, csw, &new_context, i, value, TRUE);
3796 break;
3797 case RULE_VAL_EXPRESSION:
3798 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3799 set_context_reg(module, csw, &new_context, i, value, FALSE);
3800 break;
3803 *context = new_context;
3806 static BOOL dwarf2_fetch_frame_info(struct module* module, struct cpu* cpu, LONG_PTR ip, struct frame_info* info)
3808 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3809 struct module_format* modfmt;
3810 const unsigned char* end;
3811 DWORD_PTR delta;
3813 modfmt = module->format_info[DFI_DWARF];
3814 if (!modfmt) return FALSE;
3815 memset(info, 0, sizeof(*info));
3816 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3817 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3818 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3819 * in this process the IMAGE section at a different address as the one expected by
3820 * the image
3822 delta = module->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3823 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3824 if (!dwarf2_get_cie(ip, module, delta, &fde_ctx, &cie_ctx, info, TRUE))
3826 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3827 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3828 delta = module->reloc_delta;
3829 if (modfmt->u.dwarf2_info->debug_frame.address == IMAGE_NO_MAP ||
3830 !dwarf2_get_cie(ip, module, delta, &fde_ctx, &cie_ctx, info, FALSE))
3832 TRACE("Couldn't find information for %Ix\n", ip);
3833 return FALSE;
3837 TRACE("function %Ix/%Ix code_align %Iu data_align %Id retaddr %s\n",
3838 ip, info->ip, info->code_align, info->data_align,
3839 cpu->fetch_regname(cpu->map_dwarf_register(info->retaddr_reg, module, TRUE)));
3841 if (ip != info->ip)
3843 execute_cfa_instructions(module, &cie_ctx, ip, info);
3845 if (info->aug_z_format) /* get length of augmentation data */
3847 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3848 end = fde_ctx.data + len;
3850 else end = NULL;
3851 dwarf2_parse_augmentation_ptr(&fde_ctx, info->lsda_encoding, modfmt->u.dwarf2_info->word_size); /* handler_data */
3852 if (end) fde_ctx.data = end;
3854 execute_cfa_instructions(module, &fde_ctx, ip, info);
3856 return TRUE;
3859 /***********************************************************************
3860 * dwarf2_virtual_unwind
3863 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk *csw, ULONG_PTR ip,
3864 union ctx *context, DWORD64 *cfa)
3866 struct module_pair pair;
3867 struct frame_info info;
3869 if (!module_init_pair(&pair, csw->hProcess, ip)) return FALSE;
3870 if (csw->cpu != pair.effective->cpu) FIXME("mismatch in cpu\n");
3871 if (!dwarf2_fetch_frame_info(pair.effective, csw->cpu, ip, &info)) return FALSE;
3873 /* if at very beginning of function, return and use default unwinder */
3874 if (ip == info.ip) return FALSE;
3876 /* if there is no information about retaddr, use default unwinder */
3877 if (info.state.rules[info.retaddr_reg] == RULE_UNSET) return FALSE;
3879 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3881 return TRUE;
3884 static BOOL compute_call_frame_cfa(struct module* module, ULONG_PTR ip, struct location* frame)
3886 struct frame_info info;
3888 if (!dwarf2_fetch_frame_info(module, module->cpu, ip, &info)) return FALSE;
3890 /* beginning of function, or no available dwarf information ? */
3891 if (ip == info.ip || info.state.rules[info.retaddr_reg] == RULE_UNSET)
3893 /* fake the default unwinder */
3894 frame->kind = loc_regrel;
3895 frame->reg = module->cpu->frame_regno;
3896 frame->offset = module->cpu->word_size; /* FIXME stack direction */
3898 else
3900 /* we expect to translate the call_frame_cfa into a regrel location...
3901 * that should cover most of the cases
3903 switch (info.state.cfa_rule)
3905 case RULE_EXPRESSION:
3906 FIXME("Too complex expression for frame_CFA resolution (RULE_EXPRESSION)\n");
3907 return FALSE;
3908 case RULE_VAL_EXPRESSION:
3909 FIXME("Too complex expression for frame_CFA resolution (RULE_VAL_EXPRESSION)\n");
3910 return FALSE;
3911 default:
3912 frame->kind = loc_regrel;
3913 frame->reg = module->cpu->map_dwarf_register(info.state.cfa_reg, module, TRUE);
3914 frame->offset = info.state.cfa_offset;
3915 break;
3918 return TRUE;
3921 static void dwarf2_location_compute(struct process* pcs,
3922 const struct module_format* modfmt,
3923 const struct symt_function* func,
3924 struct location* loc)
3926 struct location frame;
3927 DWORD_PTR ip;
3928 int err;
3929 dwarf2_traverse_context_t lctx;
3930 const dwarf2_cuhead_t* head = get_cuhead_from_func(func);
3932 if (!head)
3934 WARN("We'd expect function %s's container to be a valid compiland with dwarf inforamation\n",
3935 debugstr_a(func->hash_elt.name));
3936 err = loc_err_internal;
3938 else
3940 /* instruction pointer relative to compiland's start */
3941 ip = pcs->localscope_pc - ((struct symt_compiland*)func->container)->address;
3943 if ((err = loc_compute_frame(pcs, modfmt, func, ip, head, &frame)) == 0)
3945 switch (loc->kind)
3947 case loc_dwarf2_location_list:
3948 /* Then, if the variable has a location list, find it !! */
3949 if (dwarf2_lookup_loclist(modfmt, head,
3950 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3951 ip, &lctx))
3952 goto do_compute;
3953 err = loc_err_out_of_scope;
3954 break;
3955 case loc_dwarf2_block:
3956 /* or if we have a copy of an existing block, get ready for it */
3958 unsigned* ptr = (unsigned*)loc->offset;
3960 lctx.data = (const BYTE*)(ptr + 1);
3961 lctx.end_data = lctx.data + *ptr;
3963 do_compute:
3964 /* now get the variable */
3965 err = compute_location(modfmt->module, head,
3966 &lctx, loc, pcs->handle, &frame);
3967 break;
3968 case loc_register:
3969 case loc_regrel:
3970 /* nothing to do */
3971 break;
3972 default:
3973 WARN("Unsupported local kind %d\n", loc->kind);
3974 err = loc_err_internal;
3978 if (err < 0)
3980 loc->kind = loc_register;
3981 loc->reg = err;
3985 static void *zalloc(void *priv, uInt items, uInt sz)
3987 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3990 static void zfree(void *priv, void *addr)
3992 HeapFree(GetProcessHeap(), 0, addr);
3995 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3996 const char* zsectname,
3997 struct image_section_map* ism)
3999 z_stream z;
4000 LARGE_INTEGER li;
4001 int res;
4002 BOOL ret = FALSE;
4004 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
4005 size_t sz = image_get_map_size(ism);
4007 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
4009 ERR("invalid compressed section %s\n", debugstr_a(zsectname));
4010 goto out;
4013 #ifdef WORDS_BIGENDIAN
4014 li.u.HighPart = *(DWORD*)&sect[4];
4015 li.u.LowPart = *(DWORD*)&sect[8];
4016 #else
4017 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
4018 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
4019 #endif
4021 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
4022 if (!addr)
4023 goto out;
4025 z.next_in = &sect[12];
4026 z.avail_in = sz - 12;
4027 z.opaque = NULL;
4028 z.zalloc = zalloc;
4029 z.zfree = zfree;
4031 res = inflateInit(&z);
4032 if (res != Z_OK)
4034 FIXME("inflateInit failed with %i / %s\n", res, debugstr_a(z.msg));
4035 goto out_free;
4038 do {
4039 z.next_out = addr + z.total_out;
4040 z.avail_out = li.QuadPart - z.total_out;
4041 res = inflate(&z, Z_FINISH);
4042 } while (z.avail_in && res == Z_STREAM_END);
4044 if (res != Z_STREAM_END)
4046 FIXME("Decompression failed with %i / %s\n", res, debugstr_a(z.msg));
4047 goto out_end;
4050 ret = TRUE;
4051 section->compressed = TRUE;
4052 section->address = addr;
4053 section->rva = image_get_map_rva(ism);
4054 section->size = z.total_out;
4056 out_end:
4057 inflateEnd(&z);
4058 out_free:
4059 if (!ret)
4060 HeapFree(GetProcessHeap(), 0, addr);
4061 out:
4062 image_unmap_section(ism);
4063 return ret;
4066 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
4067 const char* sectname, const char* zsectname,
4068 struct image_section_map* ism)
4070 struct image_section_map local_ism;
4072 if (!ism) ism = &local_ism;
4074 section->compressed = FALSE;
4075 if (image_find_section(fmap, sectname, ism))
4077 section->address = (const BYTE*)image_map_section(ism);
4078 section->size = image_get_map_size(ism);
4079 section->rva = image_get_map_rva(ism);
4080 return TRUE;
4083 section->address = NULL;
4084 section->size = 0;
4085 section->rva = 0;
4087 if (zsectname && image_find_section(fmap, zsectname, ism))
4089 return dwarf2_init_zsection(section, zsectname, ism);
4092 return FALSE;
4095 static inline void dwarf2_fini_section(dwarf2_section_t* section)
4097 if (section->compressed)
4098 HeapFree(GetProcessHeap(), 0, (void*)section->address);
4101 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
4103 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
4104 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
4105 free(modfmt->u.dwarf2_info->cuheads);
4106 HeapFree(GetProcessHeap(), 0, modfmt);
4109 static BOOL dwarf2_load_CU_module(dwarf2_parse_module_context_t* module_ctx, struct module* module,
4110 dwarf2_section_t* sections, ULONG_PTR load_offset,
4111 const struct elf_thunk_area* thunks, BOOL is_dwz)
4113 dwarf2_traverse_context_t mod_ctx;
4114 unsigned i;
4116 module_ctx->sections = sections;
4117 module_ctx->module = module;
4118 module_ctx->thunks = thunks;
4119 module_ctx->load_offset = load_offset;
4120 vector_init(&module_ctx->unit_contexts, sizeof(dwarf2_parse_context_t), 16);
4121 module_ctx->cu_versions = 0;
4123 /* phase I: parse all CU heads */
4124 mod_ctx.data = sections[section_debug].address;
4125 mod_ctx.end_data = mod_ctx.data + sections[section_debug].size;
4126 while (mod_ctx.data < mod_ctx.end_data)
4128 dwarf2_parse_context_t* unit_ctx = vector_add(&module_ctx->unit_contexts, &module_ctx->module->pool);
4130 unit_ctx->module_ctx = module_ctx;
4131 dwarf2_parse_compilation_unit_head(unit_ctx, &mod_ctx);
4134 /* phase2: load content of all CU
4135 * If this is a DWZ alternate module, don't load all debug_info at once
4136 * wait for main module to ask for them (it's likely it won't need them all)
4137 * Doing this can lead to a huge performance improvement.
4139 if (!is_dwz)
4140 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
4141 dwarf2_parse_compilation_unit((dwarf2_parse_context_t*)vector_at(&module_ctx->unit_contexts, i));
4143 return TRUE;
4146 static dwarf2_dwz_alternate_t* dwarf2_load_dwz(struct image_file_map* fmap, struct module* module)
4148 struct image_file_map* fmap_dwz;
4149 dwarf2_dwz_alternate_t* dwz;
4151 fmap_dwz = image_load_debugaltlink(fmap, module);
4152 if (!fmap_dwz) return NULL;
4153 if (!(dwz = HeapAlloc(GetProcessHeap(), 0, sizeof(*dwz))))
4155 image_unmap_file(fmap_dwz);
4156 HeapFree(GetProcessHeap(), 0, fmap_dwz);
4157 return NULL;
4160 dwz->fmap = fmap_dwz;
4161 dwarf2_init_section(&dwz->sections[section_debug], fmap_dwz, ".debug_info", ".zdebug_info", &dwz->sectmap[section_debug]);
4162 dwarf2_init_section(&dwz->sections[section_abbrev], fmap_dwz, ".debug_abbrev", ".zdebug_abbrev", &dwz->sectmap[section_abbrev]);
4163 dwarf2_init_section(&dwz->sections[section_string], fmap_dwz, ".debug_str", ".zdebug_str", &dwz->sectmap[section_string]);
4164 dwarf2_init_section(&dwz->sections[section_line], fmap_dwz, ".debug_line", ".zdebug_line", &dwz->sectmap[section_line]);
4165 dwarf2_init_section(&dwz->sections[section_ranges], fmap_dwz, ".debug_ranges", ".zdebug_ranges", &dwz->sectmap[section_ranges]);
4167 dwz->module_ctx.dwz = NULL;
4168 dwarf2_load_CU_module(&dwz->module_ctx, module, dwz->sections, 0/*FIXME*/, NULL, TRUE);
4169 return dwz;
4172 static void dwarf2_unload_dwz(dwarf2_dwz_alternate_t* dwz)
4174 if (!dwz) return;
4175 dwarf2_fini_section(&dwz->sections[section_debug]);
4176 dwarf2_fini_section(&dwz->sections[section_abbrev]);
4177 dwarf2_fini_section(&dwz->sections[section_string]);
4178 dwarf2_fini_section(&dwz->sections[section_line]);
4179 dwarf2_fini_section(&dwz->sections[section_ranges]);
4181 image_unmap_section(&dwz->sectmap[section_debug]);
4182 image_unmap_section(&dwz->sectmap[section_abbrev]);
4183 image_unmap_section(&dwz->sectmap[section_string]);
4184 image_unmap_section(&dwz->sectmap[section_line]);
4185 image_unmap_section(&dwz->sectmap[section_ranges]);
4187 HeapFree(GetProcessHeap(), 0, dwz);
4190 static BOOL dwarf2_unload_CU_module(dwarf2_parse_module_context_t* module_ctx)
4192 unsigned i;
4193 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
4195 dwarf2_parse_context_t* unit = vector_at(&module_ctx->unit_contexts, i);
4196 if (unit->status != UNIT_ERROR)
4197 pool_destroy(&unit->pool);
4199 dwarf2_unload_dwz(module_ctx->dwz);
4200 return TRUE;
4203 BOOL dwarf2_parse(struct module* module, ULONG_PTR load_offset,
4204 const struct elf_thunk_area* thunks,
4205 struct image_file_map* fmap)
4207 dwarf2_section_t eh_frame, section[section_max];
4208 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
4209 debug_line_sect, debug_ranges_sect, eh_frame_sect;
4210 BOOL ret = TRUE;
4211 struct module_format* dwarf2_modfmt;
4212 dwarf2_parse_module_context_t module_ctx;
4214 if (!dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect))
4215 /* lld produces .eh_fram to avoid generating a long name */
4216 dwarf2_init_section(&eh_frame, fmap, ".eh_fram", NULL, &eh_frame_sect);
4217 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
4218 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
4219 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
4220 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
4221 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
4223 /* to do anything useful we need either .eh_frame or .debug_info */
4224 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
4225 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
4227 ret = FALSE;
4228 goto leave;
4231 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
4233 /* debug info might have a different base address than .so file
4234 * when elf file is prelinked after splitting off debug info
4235 * adjust symbol base addresses accordingly
4237 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
4240 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->modulename));
4242 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
4243 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
4244 if (!dwarf2_modfmt)
4246 ret = FALSE;
4247 goto leave;
4249 dwarf2_modfmt->module = module;
4250 dwarf2_modfmt->remove = dwarf2_module_remove;
4251 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
4252 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
4253 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8; /* set the word_size for eh_frame parsing */
4254 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
4256 /* As we'll need later some sections' content, we won't unmap these
4257 * sections upon existing this function
4259 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
4260 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
4261 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
4262 dwarf2_modfmt->u.dwarf2_info->cuheads = NULL;
4263 dwarf2_modfmt->u.dwarf2_info->num_cuheads = 0;
4265 module_ctx.dwz = dwarf2_load_dwz(fmap, module);
4266 dwarf2_load_CU_module(&module_ctx, module, section, load_offset, thunks, FALSE);
4268 dwarf2_modfmt->module->module.SymType = SymDia;
4269 /* hide dwarf versions in CVSig
4270 * bits 24-31 will be set according to found dwarf version
4271 * different CU can have different dwarf version, so use a bit per version (version 2 => b24)
4273 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('F' << 16) | ((module_ctx.cu_versions & 0xFF) << 24);
4274 /* FIXME: we could have a finer grain here */
4275 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
4276 dwarf2_modfmt->module->module.TypeInfo = TRUE;
4277 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
4278 dwarf2_modfmt->module->module.Publics = TRUE;
4280 dwarf2_unload_CU_module(&module_ctx);
4281 leave:
4283 dwarf2_fini_section(&section[section_debug]);
4284 dwarf2_fini_section(&section[section_abbrev]);
4285 dwarf2_fini_section(&section[section_string]);
4286 dwarf2_fini_section(&section[section_line]);
4287 dwarf2_fini_section(&section[section_ranges]);
4289 image_unmap_section(&debug_sect);
4290 image_unmap_section(&debug_abbrev_sect);
4291 image_unmap_section(&debug_str_sect);
4292 image_unmap_section(&debug_line_sect);
4293 image_unmap_section(&debug_ranges_sect);
4294 if (!ret) image_unmap_section(&eh_frame_sect);
4296 return ret;