cmd: DIR command outputs free space for the path.
[wine.git] / dlls / dbghelp / dwarf.c
blob2a4f9a1622800e61b54fb2f14eddd78243fa6b9e
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 #include <sys/types.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <stdio.h>
27 #include <assert.h>
28 #include <stdarg.h>
29 #include <zlib.h>
31 #include "windef.h"
32 #include "winternl.h"
33 #include "winbase.h"
34 #include "winuser.h"
35 #include "ole2.h"
36 #include "oleauto.h"
38 #include "dbghelp_private.h"
39 #include "image_private.h"
41 #include "wine/debug.h"
43 WINE_DEFAULT_DEBUG_CHANNEL(dbghelp_dwarf);
45 /* FIXME:
46 * - Functions:
47 * o unspecified parameters
48 * o inlined functions
49 * o Debug{Start|End}Point
50 * o CFA
51 * - Udt
52 * o proper types loading (nesting)
55 #if 0
56 static void dump(const void* ptr, unsigned len)
58 int i, j;
59 BYTE msg[128];
60 static const char hexof[] = "0123456789abcdef";
61 const BYTE* x = ptr;
63 for (i = 0; i < len; i += 16)
65 sprintf(msg, "%08x: ", i);
66 memset(msg + 10, ' ', 3 * 16 + 1 + 16);
67 for (j = 0; j < min(16, len - i); j++)
69 msg[10 + 3 * j + 0] = hexof[x[i + j] >> 4];
70 msg[10 + 3 * j + 1] = hexof[x[i + j] & 15];
71 msg[10 + 3 * j + 2] = ' ';
72 msg[10 + 3 * 16 + 1 + j] = (x[i + j] >= 0x20 && x[i + j] < 0x7f) ?
73 x[i + j] : '.';
75 msg[10 + 3 * 16] = ' ';
76 msg[10 + 3 * 16 + 1 + 16] = '\0';
77 TRACE("%s\n", msg);
80 #endif
82 /**
84 * Main Specs:
85 * http://www.eagercon.com/dwarf/dwarf3std.htm
86 * http://www.eagercon.com/dwarf/dwarf-2.0.0.pdf
88 * dwarf2.h: http://www.hakpetzna.com/b/binutils/dwarf2_8h-source.html
90 * example of projects who do dwarf2 parsing:
91 * http://www.x86-64.org/cgi-bin/cvsweb.cgi/binutils.dead/binutils/readelf.c?rev=1.1.1.2
92 * http://elis.ugent.be/diota/log/ltrace_elf.c
94 #include "dwarf.h"
96 /**
97 * Parsers
100 typedef struct dwarf2_abbrev_entry_attr_s
102 ULONG_PTR attribute;
103 ULONG_PTR form;
104 struct dwarf2_abbrev_entry_attr_s* next;
105 } dwarf2_abbrev_entry_attr_t;
107 typedef struct dwarf2_abbrev_entry_s
109 ULONG_PTR entry_code;
110 ULONG_PTR tag;
111 unsigned char have_child;
112 unsigned num_attr;
113 dwarf2_abbrev_entry_attr_t* attrs;
114 } dwarf2_abbrev_entry_t;
116 struct dwarf2_block
118 unsigned size;
119 const unsigned char* ptr;
122 struct attribute
124 ULONG_PTR form;
125 enum {attr_direct, attr_abstract_origin, attr_specification} gotten_from;
126 union
128 ULONG_PTR uvalue;
129 ULONGLONG lluvalue;
130 LONG_PTR svalue;
131 const char* string;
132 struct dwarf2_block block;
133 } u;
134 const struct dwarf2_debug_info_s* debug_info;
137 typedef struct dwarf2_debug_info_s
139 const dwarf2_abbrev_entry_t*abbrev;
140 struct symt* symt;
141 const unsigned char** data;
142 struct vector children;
143 struct dwarf2_debug_info_s* parent;
144 struct dwarf2_parse_context_s* unit_ctx;
145 } dwarf2_debug_info_t;
147 typedef struct dwarf2_section_s
149 BOOL compressed;
150 const unsigned char* address;
151 unsigned size;
152 DWORD_PTR rva;
153 } dwarf2_section_t;
155 enum dwarf2_sections {section_debug, section_string, section_abbrev, section_line, section_ranges, section_max};
157 typedef struct dwarf2_traverse_context_s
159 const unsigned char* data;
160 const unsigned char* end_data;
161 } dwarf2_traverse_context_t;
163 typedef struct dwarf2_cuhead_s
165 unsigned char word_size; /* size of a word on target machine */
166 unsigned char version;
167 unsigned char offset_size; /* size of offset inside DWARF */
168 } dwarf2_cuhead_t;
170 typedef struct dwarf2_parse_module_context_s
172 ULONG_PTR load_offset;
173 const dwarf2_section_t* sections;
174 struct module* module;
175 const struct elf_thunk_area*thunks;
176 struct vector unit_contexts;
177 struct dwarf2_dwz_alternate_s* dwz;
178 DWORD cu_versions;
179 } dwarf2_parse_module_context_t;
181 typedef struct dwarf2_dwz_alternate_s
183 struct image_file_map* fmap;
184 dwarf2_section_t sections[section_max];
185 struct image_section_map sectmap[section_max];
186 dwarf2_parse_module_context_t module_ctx;
187 } dwarf2_dwz_alternate_t;
189 enum unit_status
191 UNIT_ERROR,
192 UNIT_NOTLOADED,
193 UNIT_LOADED,
194 UNIT_LOADED_FAIL,
195 UNIT_BEINGLOADED,
198 /* this is the context used for parsing a compilation unit
199 * inside an ELF/PE section (likely .debug_info)
201 typedef struct dwarf2_parse_context_s
203 dwarf2_parse_module_context_t* module_ctx;
204 unsigned section;
205 struct pool pool;
206 struct symt_compiland* compiland;
207 struct sparse_array abbrev_table;
208 struct sparse_array debug_info_table;
209 ULONG_PTR ref_offset;
210 char* cpp_name;
211 dwarf2_cuhead_t head;
212 enum unit_status status;
213 dwarf2_traverse_context_t traverse_DIE;
214 unsigned language;
215 } dwarf2_parse_context_t;
217 /* stored in the dbghelp's module internal structure for later reuse */
218 struct dwarf2_module_info_s
220 dwarf2_cuhead_t** cuheads;
221 unsigned num_cuheads;
222 dwarf2_section_t debug_loc;
223 dwarf2_section_t debug_frame;
224 dwarf2_section_t eh_frame;
225 unsigned char word_size;
228 #define loc_dwarf2_location_list (loc_user + 0)
229 #define loc_dwarf2_block (loc_user + 1)
230 #define loc_dwarf2_frame_cfa (loc_user + 2)
232 /* forward declarations */
233 static struct symt* dwarf2_parse_enumeration_type(dwarf2_debug_info_t* entry);
234 static BOOL dwarf2_parse_compilation_unit(dwarf2_parse_context_t* ctx);
235 static dwarf2_parse_context_t* dwarf2_locate_cu(dwarf2_parse_module_context_t* module_ctx, ULONG_PTR ref);
237 static unsigned char dwarf2_get_byte(const unsigned char* ptr)
239 return *ptr;
242 static unsigned char dwarf2_parse_byte(dwarf2_traverse_context_t* ctx)
244 unsigned char uvalue = dwarf2_get_byte(ctx->data);
245 ctx->data += 1;
246 return uvalue;
249 static unsigned short dwarf2_get_u2(const unsigned char* ptr)
251 return *(const UINT16*)ptr;
254 static unsigned short dwarf2_parse_u2(dwarf2_traverse_context_t* ctx)
256 unsigned short uvalue = dwarf2_get_u2(ctx->data);
257 ctx->data += 2;
258 return uvalue;
261 static ULONG_PTR dwarf2_get_u4(const unsigned char* ptr)
263 return *(const UINT32*)ptr;
266 static ULONG_PTR dwarf2_parse_u4(dwarf2_traverse_context_t* ctx)
268 ULONG_PTR uvalue = dwarf2_get_u4(ctx->data);
269 ctx->data += 4;
270 return uvalue;
273 static DWORD64 dwarf2_get_u8(const unsigned char* ptr)
275 return *(const UINT64*)ptr;
278 static DWORD64 dwarf2_parse_u8(dwarf2_traverse_context_t* ctx)
280 DWORD64 uvalue = dwarf2_get_u8(ctx->data);
281 ctx->data += 8;
282 return uvalue;
285 static ULONG_PTR dwarf2_get_leb128_as_unsigned(const unsigned char* ptr, const unsigned char** end)
287 ULONG_PTR ret = 0;
288 unsigned char byte;
289 unsigned shift = 0;
293 byte = dwarf2_get_byte(ptr++);
294 ret |= (byte & 0x7f) << shift;
295 shift += 7;
296 } while (byte & 0x80);
298 if (end) *end = ptr;
299 return ret;
302 static ULONG_PTR dwarf2_leb128_as_unsigned(dwarf2_traverse_context_t* ctx)
304 ULONG_PTR ret;
306 assert(ctx);
308 ret = dwarf2_get_leb128_as_unsigned(ctx->data, &ctx->data);
310 return ret;
313 static LONG_PTR dwarf2_get_leb128_as_signed(const unsigned char* ptr, const unsigned char** end)
315 LONG_PTR ret = 0;
316 unsigned char byte;
317 unsigned shift = 0;
318 const unsigned size = sizeof(int) * 8;
322 byte = dwarf2_get_byte(ptr++);
323 ret |= (byte & 0x7f) << shift;
324 shift += 7;
325 } while (byte & 0x80);
326 if (end) *end = ptr;
328 /* as spec: sign bit of byte is 2nd high order bit (80x40)
329 * -> 0x80 is used as flag.
331 if ((shift < size) && (byte & 0x40))
333 ret |= - (1 << shift);
335 return ret;
338 static LONG_PTR dwarf2_leb128_as_signed(dwarf2_traverse_context_t* ctx)
340 LONG_PTR ret = 0;
342 assert(ctx);
344 ret = dwarf2_get_leb128_as_signed(ctx->data, &ctx->data);
345 return ret;
348 static unsigned dwarf2_leb128_length(const dwarf2_traverse_context_t* ctx)
350 unsigned ret;
351 for (ret = 0; ctx->data[ret] & 0x80; ret++);
352 return ret + 1;
355 /******************************************************************
356 * dwarf2_get_addr
358 * Returns an address.
359 * We assume that in all cases word size from Dwarf matches the size of
360 * addresses in platform where the exec is compiled.
362 static ULONG_PTR dwarf2_get_addr(const unsigned char* ptr, unsigned word_size)
364 ULONG_PTR ret;
366 switch (word_size)
368 case 4:
369 ret = dwarf2_get_u4(ptr);
370 break;
371 case 8:
372 ret = dwarf2_get_u8(ptr);
373 break;
374 default:
375 FIXME("Unsupported Word Size %u\n", word_size);
376 ret = 0;
378 return ret;
381 static inline ULONG_PTR dwarf2_parse_addr(dwarf2_traverse_context_t* ctx, unsigned word_size)
383 ULONG_PTR ret = dwarf2_get_addr(ctx->data, word_size);
384 ctx->data += word_size;
385 return ret;
388 static inline ULONG_PTR dwarf2_parse_addr_head(dwarf2_traverse_context_t* ctx, const dwarf2_cuhead_t* head)
390 return dwarf2_parse_addr(ctx, head->word_size);
393 static ULONG_PTR dwarf2_parse_offset(dwarf2_traverse_context_t* ctx, unsigned char offset_size)
395 ULONG_PTR ret = dwarf2_get_addr(ctx->data, offset_size);
396 ctx->data += offset_size;
397 return ret;
400 static ULONG_PTR dwarf2_parse_3264(dwarf2_traverse_context_t* ctx, unsigned char* ofsz)
402 ULONG_PTR ret = dwarf2_parse_u4(ctx);
403 if (ret == 0xffffffff)
405 ret = dwarf2_parse_u8(ctx);
406 *ofsz = 8;
408 else *ofsz = 4;
409 return ret;
412 static const char* dwarf2_debug_traverse_ctx(const dwarf2_traverse_context_t* ctx)
414 return wine_dbg_sprintf("ctx(%p)", ctx->data);
417 static const char* dwarf2_debug_unit_ctx(const dwarf2_parse_context_t* ctx)
419 return wine_dbg_sprintf("ctx(%p,%s)",
420 ctx, debugstr_w(ctx->module_ctx->module->modulename));
423 static const char* dwarf2_debug_di(const dwarf2_debug_info_t* di)
425 return wine_dbg_sprintf("debug_info(abbrev:%p,symt:%p) in %s",
426 di->abbrev, di->symt, dwarf2_debug_unit_ctx(di->unit_ctx));
429 static dwarf2_abbrev_entry_t*
430 dwarf2_abbrev_table_find_entry(const struct sparse_array* abbrev_table,
431 ULONG_PTR entry_code)
433 assert( NULL != abbrev_table );
434 return sparse_array_find(abbrev_table, entry_code);
437 static void dwarf2_parse_abbrev_set(dwarf2_traverse_context_t* abbrev_ctx,
438 struct sparse_array* abbrev_table,
439 struct pool* pool)
441 ULONG_PTR entry_code;
442 dwarf2_abbrev_entry_t* abbrev_entry;
443 dwarf2_abbrev_entry_attr_t* new = NULL;
444 dwarf2_abbrev_entry_attr_t* last = NULL;
445 ULONG_PTR attribute;
446 ULONG_PTR form;
448 assert( NULL != abbrev_ctx );
450 TRACE("%s, end at %p\n",
451 dwarf2_debug_traverse_ctx(abbrev_ctx), abbrev_ctx->end_data);
453 sparse_array_init(abbrev_table, sizeof(dwarf2_abbrev_entry_t), 32);
454 while (abbrev_ctx->data < abbrev_ctx->end_data)
456 TRACE("now at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
457 entry_code = dwarf2_leb128_as_unsigned(abbrev_ctx);
458 TRACE("found entry_code %Iu\n", entry_code);
459 if (!entry_code)
461 TRACE("NULL entry code at %s\n", dwarf2_debug_traverse_ctx(abbrev_ctx));
462 break;
464 abbrev_entry = sparse_array_add(abbrev_table, entry_code, pool);
465 assert( NULL != abbrev_entry );
467 abbrev_entry->entry_code = entry_code;
468 abbrev_entry->tag = dwarf2_leb128_as_unsigned(abbrev_ctx);
469 abbrev_entry->have_child = dwarf2_parse_byte(abbrev_ctx);
470 abbrev_entry->attrs = NULL;
471 abbrev_entry->num_attr = 0;
473 TRACE("table:(%p,#%u) entry_code(%Iu) tag(0x%Ix) have_child(%u) -> %p\n",
474 abbrev_table, sparse_array_length(abbrev_table),
475 entry_code, abbrev_entry->tag, abbrev_entry->have_child, abbrev_entry);
477 last = NULL;
478 while (1)
480 attribute = dwarf2_leb128_as_unsigned(abbrev_ctx);
481 form = dwarf2_leb128_as_unsigned(abbrev_ctx);
482 if (!attribute) break;
484 new = pool_alloc(pool, sizeof(dwarf2_abbrev_entry_attr_t));
485 assert(new);
487 new->attribute = attribute;
488 new->form = form;
489 new->next = NULL;
490 if (abbrev_entry->attrs) last->next = new;
491 else abbrev_entry->attrs = new;
492 last = new;
493 abbrev_entry->num_attr++;
496 TRACE("found %u entries\n", sparse_array_length(abbrev_table));
499 static void dwarf2_swallow_attribute(dwarf2_traverse_context_t* ctx,
500 const dwarf2_cuhead_t* head,
501 const dwarf2_abbrev_entry_attr_t* abbrev_attr)
503 unsigned step;
505 TRACE("(attr:0x%Ix,form:0x%Ix)\n", abbrev_attr->attribute, abbrev_attr->form);
507 switch (abbrev_attr->form)
509 case DW_FORM_flag_present: step = 0; break;
510 case DW_FORM_ref_addr: step = (head->version >= 3) ? head->offset_size : head->word_size; break;
511 case DW_FORM_addr: step = head->word_size; break;
512 case DW_FORM_flag:
513 case DW_FORM_data1:
514 case DW_FORM_ref1: step = 1; break;
515 case DW_FORM_data2:
516 case DW_FORM_ref2: step = 2; break;
517 case DW_FORM_data4:
518 case DW_FORM_ref4: step = 4; break;
519 case DW_FORM_data8:
520 case DW_FORM_ref8: step = 8; break;
521 case DW_FORM_sdata:
522 case DW_FORM_ref_udata:
523 case DW_FORM_udata: step = dwarf2_leb128_length(ctx); break;
524 case DW_FORM_string: step = strlen((const char*)ctx->data) + 1; break;
525 case DW_FORM_exprloc:
526 case DW_FORM_block: step = dwarf2_leb128_as_unsigned(ctx); break;
527 case DW_FORM_block1: step = dwarf2_parse_byte(ctx); break;
528 case DW_FORM_block2: step = dwarf2_parse_u2(ctx); break;
529 case DW_FORM_block4: step = dwarf2_parse_u4(ctx); break;
530 case DW_FORM_sec_offset:
531 case DW_FORM_GNU_ref_alt:
532 case DW_FORM_GNU_strp_alt:
533 case DW_FORM_strp: step = head->offset_size; break;
534 default:
535 FIXME("Unhandled attribute form %Ix\n", abbrev_attr->form);
536 return;
538 ctx->data += step;
541 static BOOL dwarf2_fill_attr(const dwarf2_parse_context_t* ctx,
542 const dwarf2_abbrev_entry_attr_t* abbrev_attr,
543 const unsigned char* data,
544 struct attribute* attr)
546 attr->form = abbrev_attr->form;
547 switch (attr->form)
549 case DW_FORM_ref_addr:
550 if (ctx->head.version >= 3)
551 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.offset_size);
552 else
553 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.word_size);
554 TRACE("addr<0x%Ix>\n", attr->u.uvalue);
555 break;
557 case DW_FORM_addr:
558 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.word_size);
559 TRACE("addr<0x%Ix>\n", attr->u.uvalue);
560 break;
562 case DW_FORM_flag:
563 attr->u.uvalue = dwarf2_get_byte(data);
564 TRACE("flag<0x%Ix>\n", attr->u.uvalue);
565 break;
567 case DW_FORM_flag_present:
568 attr->u.uvalue = 1;
569 TRACE("flag_present\n");
570 break;
572 case DW_FORM_data1:
573 attr->u.uvalue = dwarf2_get_byte(data);
574 TRACE("data1<%Iu>\n", attr->u.uvalue);
575 break;
577 case DW_FORM_data2:
578 attr->u.uvalue = dwarf2_get_u2(data);
579 TRACE("data2<%Iu>\n", attr->u.uvalue);
580 break;
582 case DW_FORM_data4:
583 attr->u.uvalue = dwarf2_get_u4(data);
584 TRACE("data4<%Iu>\n", attr->u.uvalue);
585 break;
587 case DW_FORM_data8:
588 attr->u.lluvalue = dwarf2_get_u8(data);
589 TRACE("data8<%Ix>\n", attr->u.uvalue);
590 break;
592 case DW_FORM_ref1:
593 attr->u.uvalue = ctx->ref_offset + dwarf2_get_byte(data);
594 TRACE("ref1<0x%Ix>\n", attr->u.uvalue);
595 break;
597 case DW_FORM_ref2:
598 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u2(data);
599 TRACE("ref2<0x%Ix>\n", attr->u.uvalue);
600 break;
602 case DW_FORM_ref4:
603 attr->u.uvalue = ctx->ref_offset + dwarf2_get_u4(data);
604 TRACE("ref4<0x%Ix>\n", attr->u.uvalue);
605 break;
607 case DW_FORM_ref8:
608 FIXME("Unhandled 64-bit support\n");
609 break;
611 case DW_FORM_sdata:
612 attr->u.svalue = dwarf2_get_leb128_as_signed(data, NULL);
613 break;
615 case DW_FORM_ref_udata:
616 attr->u.uvalue = ctx->ref_offset + dwarf2_get_leb128_as_unsigned(data, NULL);
617 TRACE("ref_udata<0x%Ix>\n", attr->u.uvalue);
618 break;
620 case DW_FORM_udata:
621 attr->u.uvalue = dwarf2_get_leb128_as_unsigned(data, NULL);
622 TRACE("udata<0x%Ix>\n", attr->u.uvalue);
623 break;
625 case DW_FORM_string:
626 attr->u.string = (const char *)data;
627 TRACE("string<%s>\n", debugstr_a(attr->u.string));
628 break;
630 case DW_FORM_strp:
632 ULONG_PTR ofs = dwarf2_get_addr(data, ctx->head.offset_size);
633 if (ofs >= ctx->module_ctx->sections[section_string].size)
635 ERR("Out of bounds string offset (%08Ix)\n", ofs);
636 attr->u.string = "<<outofbounds-strp>>";
638 else
640 attr->u.string = (const char*)ctx->module_ctx->sections[section_string].address + ofs;
641 TRACE("strp<%s>\n", debugstr_a(attr->u.string));
644 break;
646 case DW_FORM_block:
647 case DW_FORM_exprloc:
648 attr->u.block.size = dwarf2_get_leb128_as_unsigned(data, &attr->u.block.ptr);
649 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
650 break;
652 case DW_FORM_block1:
653 attr->u.block.size = dwarf2_get_byte(data);
654 attr->u.block.ptr = data + 1;
655 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
656 break;
658 case DW_FORM_block2:
659 attr->u.block.size = dwarf2_get_u2(data);
660 attr->u.block.ptr = data + 2;
661 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
662 break;
664 case DW_FORM_block4:
665 attr->u.block.size = dwarf2_get_u4(data);
666 attr->u.block.ptr = data + 4;
667 TRACE("block<%p,%u>\n", attr->u.block.ptr, attr->u.block.size);
668 break;
670 case DW_FORM_sec_offset:
671 attr->u.lluvalue = dwarf2_get_addr(data, ctx->head.offset_size);
672 TRACE("sec_offset<%I64x>\n", attr->u.lluvalue);
673 break;
675 case DW_FORM_GNU_ref_alt:
676 if (!ctx->module_ctx->dwz)
678 ERR("No DWZ file present for GNU_ref_alt in %s\n", debugstr_w(ctx->module_ctx->module->modulename));
679 attr->u.uvalue = 0;
680 return FALSE;
682 attr->u.uvalue = dwarf2_get_addr(data, ctx->head.offset_size);
683 TRACE("ref_alt<0x%Ix>\n", attr->u.uvalue);
684 break;
686 case DW_FORM_GNU_strp_alt:
687 if (ctx->module_ctx->dwz)
689 ULONG_PTR ofs = dwarf2_get_addr(data, ctx->head.offset_size);
690 if (ofs < ctx->module_ctx->dwz->sections[section_string].size)
692 attr->u.string = (const char*)ctx->module_ctx->dwz->sections[section_string].address + ofs;
693 TRACE("strp_alt<%s>\n", debugstr_a(attr->u.string));
695 else
697 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);
698 attr->u.string = "<<outofbounds-strpalt>>";
701 else
703 ERR("No DWZ file present for GNU_strp_alt in %s\n", debugstr_w(ctx->module_ctx->module->modulename));
704 attr->u.string = "<<noDWZ-strpalt>>";
706 break;
708 default:
709 FIXME("Unhandled attribute form %Ix\n", abbrev_attr->form);
710 break;
712 return TRUE;
715 static dwarf2_debug_info_t* dwarf2_jump_to_debug_info(struct attribute* attr);
717 static BOOL dwarf2_find_attribute(const dwarf2_debug_info_t* di,
718 unsigned at, struct attribute* attr)
720 unsigned i, refidx = 0;
721 dwarf2_abbrev_entry_attr_t* abbrev_attr;
722 dwarf2_abbrev_entry_attr_t* ref_abbrev_attr = NULL;
724 attr->gotten_from = attr_direct;
725 while (di)
727 ref_abbrev_attr = NULL;
728 attr->debug_info = di;
729 for (i = 0, abbrev_attr = di->abbrev->attrs; abbrev_attr; i++, abbrev_attr = abbrev_attr->next)
731 if (abbrev_attr->attribute == at)
733 return dwarf2_fill_attr(di->unit_ctx, abbrev_attr, di->data[i], attr);
735 if ((abbrev_attr->attribute == DW_AT_abstract_origin ||
736 abbrev_attr->attribute == DW_AT_specification) &&
737 at != DW_AT_sibling)
739 if (ref_abbrev_attr)
740 FIXME("two references %Ix and %Ix\n", ref_abbrev_attr->attribute, abbrev_attr->attribute);
741 ref_abbrev_attr = abbrev_attr;
742 refidx = i;
743 attr->gotten_from = (abbrev_attr->attribute == DW_AT_abstract_origin) ?
744 attr_abstract_origin : attr_specification;
747 /* do we have either an abstract origin or a specification debug entry to look into ? */
748 if (!ref_abbrev_attr || !dwarf2_fill_attr(di->unit_ctx, ref_abbrev_attr, di->data[refidx], attr))
749 break;
750 if (!(di = dwarf2_jump_to_debug_info(attr)))
752 FIXME("Should have found the debug info entry\n");
753 break;
756 return FALSE;
759 static dwarf2_debug_info_t* dwarf2_jump_to_debug_info(struct attribute* attr)
761 dwarf2_parse_context_t* ref_ctx = NULL;
762 BOOL with_other = TRUE;
763 dwarf2_debug_info_t* ret;
765 switch (attr->form)
767 case DW_FORM_ref_addr:
768 ref_ctx = dwarf2_locate_cu(attr->debug_info->unit_ctx->module_ctx, attr->u.uvalue);
769 break;
770 case DW_FORM_GNU_ref_alt:
771 if (attr->debug_info->unit_ctx->module_ctx->dwz)
772 ref_ctx = dwarf2_locate_cu(&attr->debug_info->unit_ctx->module_ctx->dwz->module_ctx, attr->u.uvalue);
773 break;
774 default:
775 with_other = FALSE;
776 ref_ctx = attr->debug_info->unit_ctx;
777 break;
779 if (!ref_ctx) return FALSE;
780 /* There are cases where we end up with a circular reference between two (or more)
781 * compilation units. Before this happens, try to see if we can refer to an already
782 * loaded debug_info in the target compilation unit (even if all the debug_info
783 * haven't been loaded yet).
785 if (ref_ctx->status == UNIT_BEINGLOADED &&
786 (ret = sparse_array_find(&ref_ctx->debug_info_table, attr->u.uvalue)))
787 return ret;
788 if (with_other)
790 /* ensure CU is fully loaded */
791 if (ref_ctx != attr->debug_info->unit_ctx && !dwarf2_parse_compilation_unit(ref_ctx))
792 return NULL;
794 return sparse_array_find(&ref_ctx->debug_info_table, attr->u.uvalue);
797 static void dwarf2_load_one_entry(dwarf2_debug_info_t*);
799 #define Wine_DW_no_register 0x7FFFFFFF
801 static unsigned dwarf2_map_register(int regno, const struct module* module)
803 if (regno == Wine_DW_no_register)
805 FIXME("What the heck map reg 0x%x\n",regno);
806 return 0;
808 return module->cpu->map_dwarf_register(regno, module, FALSE);
811 static enum location_error
812 compute_location(const struct module *module, const dwarf2_cuhead_t* head,
813 dwarf2_traverse_context_t* ctx, struct location* loc,
814 HANDLE hproc, const struct location* frame)
816 DWORD_PTR tmp, stack[64];
817 unsigned stk;
818 unsigned char op;
819 BOOL piece_found = FALSE;
821 stack[stk = 0] = 0;
823 loc->kind = loc_absolute;
824 loc->reg = Wine_DW_no_register;
826 while (ctx->data < ctx->end_data)
828 op = dwarf2_parse_byte(ctx);
830 if (op >= DW_OP_lit0 && op <= DW_OP_lit31)
831 stack[++stk] = op - DW_OP_lit0;
832 else if (op >= DW_OP_reg0 && op <= DW_OP_reg31)
834 /* dbghelp APIs don't know how to cope with this anyway
835 * (for example 'long long' stored in two registers)
836 * FIXME: We should tell winedbg how to deal with it (sigh)
838 if (!piece_found)
840 DWORD cvreg = dwarf2_map_register(op - DW_OP_reg0, module);
841 if (loc->reg != Wine_DW_no_register)
842 FIXME("Only supporting one reg (%s/%d -> %s/%ld)\n",
843 module->cpu->fetch_regname(loc->reg), loc->reg,
844 module->cpu->fetch_regname(cvreg), cvreg);
845 loc->reg = cvreg;
847 loc->kind = loc_register;
849 else if (op >= DW_OP_breg0 && op <= DW_OP_breg31)
851 /* dbghelp APIs don't know how to cope with this anyway
852 * (for example 'long long' stored in two registers)
853 * FIXME: We should tell winedbg how to deal with it (sigh)
855 if (!piece_found)
857 DWORD cvreg = dwarf2_map_register(op - DW_OP_breg0, module);
858 if (loc->reg != Wine_DW_no_register)
859 FIXME("Only supporting one breg (%s/%d -> %s/%ld)\n",
860 module->cpu->fetch_regname(loc->reg), loc->reg,
861 module->cpu->fetch_regname(cvreg), cvreg);
862 loc->reg = cvreg;
864 stack[++stk] = dwarf2_leb128_as_signed(ctx);
865 loc->kind = loc_regrel;
867 else switch (op)
869 case DW_OP_nop: break;
870 case DW_OP_addr: stack[++stk] = dwarf2_parse_addr_head(ctx, head); break;
871 case DW_OP_const1u: stack[++stk] = dwarf2_parse_byte(ctx); break;
872 case DW_OP_const1s: stack[++stk] = dwarf2_parse_byte(ctx); break;
873 case DW_OP_const2u: stack[++stk] = dwarf2_parse_u2(ctx); break;
874 case DW_OP_const2s: stack[++stk] = dwarf2_parse_u2(ctx); break;
875 case DW_OP_const4u: stack[++stk] = dwarf2_parse_u4(ctx); break;
876 case DW_OP_const4s: stack[++stk] = dwarf2_parse_u4(ctx); break;
877 case DW_OP_const8u: stack[++stk] = dwarf2_parse_u8(ctx); break;
878 case DW_OP_const8s: stack[++stk] = dwarf2_parse_u8(ctx); break;
879 case DW_OP_constu: stack[++stk] = dwarf2_leb128_as_unsigned(ctx); break;
880 case DW_OP_consts: stack[++stk] = dwarf2_leb128_as_signed(ctx); break;
881 case DW_OP_dup: stack[stk + 1] = stack[stk]; stk++; break;
882 case DW_OP_drop: stk--; break;
883 case DW_OP_over: stack[stk + 1] = stack[stk - 1]; stk++; break;
884 case DW_OP_pick: stack[stk + 1] = stack[stk - dwarf2_parse_byte(ctx)]; stk++; break;
885 case DW_OP_swap: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = tmp; break;
886 case DW_OP_rot: tmp = stack[stk]; stack[stk] = stack[stk-1]; stack[stk-1] = stack[stk-2]; stack[stk-2] = tmp; break;
887 case DW_OP_abs: stack[stk] = sizeof(stack[stk]) == 8 ? llabs((INT64)stack[stk]) : abs((INT32)stack[stk]); break;
888 case DW_OP_neg: stack[stk] = -stack[stk]; break;
889 case DW_OP_not: stack[stk] = ~stack[stk]; break;
890 case DW_OP_and: stack[stk-1] &= stack[stk]; stk--; break;
891 case DW_OP_or: stack[stk-1] |= stack[stk]; stk--; break;
892 case DW_OP_minus: stack[stk-1] -= stack[stk]; stk--; break;
893 case DW_OP_mul: stack[stk-1] *= stack[stk]; stk--; break;
894 case DW_OP_plus: stack[stk-1] += stack[stk]; stk--; break;
895 case DW_OP_xor: stack[stk-1] ^= stack[stk]; stk--; break;
896 case DW_OP_shl: stack[stk-1] <<= stack[stk]; stk--; break;
897 case DW_OP_shr: stack[stk-1] >>= stack[stk]; stk--; break;
898 case DW_OP_plus_uconst: stack[stk] += dwarf2_leb128_as_unsigned(ctx); break;
899 case DW_OP_shra: stack[stk-1] = stack[stk-1] / (1 << stack[stk]); stk--; break;
900 case DW_OP_div: stack[stk-1] = stack[stk-1] / stack[stk]; stk--; break;
901 case DW_OP_mod: stack[stk-1] = stack[stk-1] % stack[stk]; stk--; break;
902 case DW_OP_ge: stack[stk-1] = (stack[stk-1] >= stack[stk]); stk--; break;
903 case DW_OP_gt: stack[stk-1] = (stack[stk-1] > stack[stk]); stk--; break;
904 case DW_OP_le: stack[stk-1] = (stack[stk-1] <= stack[stk]); stk--; break;
905 case DW_OP_lt: stack[stk-1] = (stack[stk-1] < stack[stk]); stk--; break;
906 case DW_OP_eq: stack[stk-1] = (stack[stk-1] == stack[stk]); stk--; break;
907 case DW_OP_ne: stack[stk-1] = (stack[stk-1] != stack[stk]); stk--; break;
908 case DW_OP_skip: tmp = dwarf2_parse_u2(ctx); ctx->data += tmp; break;
909 case DW_OP_bra:
910 tmp = dwarf2_parse_u2(ctx);
911 if (!stack[stk--]) ctx->data += tmp;
912 break;
913 case DW_OP_regx:
914 tmp = dwarf2_leb128_as_unsigned(ctx);
915 if (!piece_found)
917 if (loc->reg != Wine_DW_no_register)
918 FIXME("Only supporting one reg\n");
919 loc->reg = dwarf2_map_register(tmp, module);
921 loc->kind = loc_register;
922 break;
923 case DW_OP_bregx:
924 tmp = dwarf2_leb128_as_unsigned(ctx);
925 if (loc->reg != Wine_DW_no_register)
926 FIXME("Only supporting one regx\n");
927 loc->reg = dwarf2_map_register(tmp, module);
928 stack[++stk] = dwarf2_leb128_as_signed(ctx);
929 loc->kind = loc_regrel;
930 break;
931 case DW_OP_fbreg:
932 if (loc->reg != Wine_DW_no_register)
933 FIXME("Only supporting one reg (%s/%d -> -2)\n",
934 module->cpu->fetch_regname(loc->reg), loc->reg);
935 if (frame && frame->kind == loc_register)
937 loc->kind = loc_regrel;
938 loc->reg = frame->reg;
939 stack[++stk] = dwarf2_leb128_as_signed(ctx);
941 else if (frame && frame->kind == loc_regrel)
943 loc->kind = loc_regrel;
944 loc->reg = frame->reg;
945 stack[++stk] = dwarf2_leb128_as_signed(ctx) + frame->offset;
947 else
949 /* FIXME: this could be later optimized by not recomputing
950 * this very location expression
952 loc->kind = loc_dwarf2_block;
953 stack[++stk] = dwarf2_leb128_as_signed(ctx);
955 break;
956 case DW_OP_piece:
958 unsigned sz = dwarf2_leb128_as_unsigned(ctx);
959 WARN("Not handling OP_piece (size=%d)\n", sz);
960 piece_found = TRUE;
962 break;
963 case DW_OP_deref:
964 if (!stk)
966 FIXME("Unexpected empty stack\n");
967 return loc_err_internal;
969 if (loc->reg != Wine_DW_no_register)
971 WARN("Too complex expression for deref\n");
972 return loc_err_too_complex;
974 if (hproc)
976 DWORD_PTR addr = stack[stk--];
977 DWORD_PTR deref = 0;
979 if (!ReadProcessMemory(hproc, (void*)addr, &deref, head->word_size, NULL))
981 WARN("Couldn't read memory at %Ix\n", addr);
982 return loc_err_cant_read;
984 stack[++stk] = deref;
986 else
988 loc->kind = loc_dwarf2_block;
990 break;
991 case DW_OP_deref_size:
992 if (!stk)
994 FIXME("Unexpected empty stack\n");
995 return loc_err_internal;
997 if (loc->reg != Wine_DW_no_register)
999 WARN("Too complex expression for deref\n");
1000 return loc_err_too_complex;
1002 if (hproc)
1004 DWORD_PTR addr = stack[stk--];
1005 BYTE derefsize = dwarf2_parse_byte(ctx);
1006 DWORD64 deref;
1008 if (!ReadProcessMemory(hproc, (void*)addr, &deref, derefsize, NULL))
1010 WARN("Couldn't read memory at %Ix\n", addr);
1011 return loc_err_cant_read;
1014 switch (derefsize)
1016 case 1: stack[++stk] = *(unsigned char*)&deref; break;
1017 case 2: stack[++stk] = *(unsigned short*)&deref; break;
1018 case 4: stack[++stk] = *(DWORD*)&deref; break;
1019 case 8: if (head->word_size >= derefsize) stack[++stk] = deref; break;
1022 else
1024 dwarf2_parse_byte(ctx);
1025 loc->kind = loc_dwarf2_block;
1027 break;
1028 case DW_OP_stack_value:
1029 /* Expected behaviour is that this is the last instruction of this
1030 * expression and just the "top of stack" value should be put to loc->offset. */
1031 break;
1032 default:
1033 if (op < DW_OP_lo_user) /* as DW_OP_hi_user is 0xFF, we don't need to test against it */
1034 FIXME("Unhandled attr op: %x\n", op);
1035 /* FIXME else unhandled extension */
1036 return loc_err_internal;
1039 loc->offset = stack[stk];
1040 return 0;
1043 static BOOL dwarf2_compute_location_attr(dwarf2_parse_context_t* ctx,
1044 const dwarf2_debug_info_t* di,
1045 ULONG_PTR dw,
1046 struct location* loc,
1047 const struct location* frame)
1049 struct attribute xloc;
1051 if (!dwarf2_find_attribute(di, dw, &xloc)) return FALSE;
1053 switch (xloc.form)
1055 case DW_FORM_data4:
1056 if (ctx->head.version < 4)
1058 loc->kind = loc_dwarf2_location_list;
1059 loc->reg = Wine_DW_no_register;
1060 loc->offset = xloc.u.uvalue;
1061 return TRUE;
1063 /* fall through */
1064 case DW_FORM_data1: case DW_FORM_data2:
1065 case DW_FORM_udata: case DW_FORM_sdata:
1066 loc->kind = loc_absolute;
1067 loc->reg = 0;
1068 loc->offset = xloc.u.uvalue;
1069 return TRUE;
1070 case DW_FORM_data8:
1071 if (ctx->head.version >= 4)
1073 loc->kind = loc_absolute;
1074 loc->reg = 0;
1075 loc->offset = xloc.u.lluvalue;
1076 return TRUE;
1078 /* fall through */
1079 case DW_FORM_sec_offset:
1080 loc->kind = loc_dwarf2_location_list;
1081 loc->reg = Wine_DW_no_register;
1082 loc->offset = xloc.u.lluvalue;
1083 return TRUE;
1084 case DW_FORM_block:
1085 case DW_FORM_block1:
1086 case DW_FORM_block2:
1087 case DW_FORM_block4:
1088 case DW_FORM_exprloc:
1089 break;
1090 default: FIXME("Unsupported yet form %Ix\n", xloc.form);
1091 return FALSE;
1094 /* assume we have a block form */
1095 if (dw == DW_AT_frame_base && xloc.u.block.size == 1 && *xloc.u.block.ptr == DW_OP_call_frame_cfa)
1097 loc->kind = loc_dwarf2_frame_cfa;
1098 loc->reg = Wine_DW_no_register;
1099 loc->offset = 0;
1101 else if (xloc.u.block.size)
1103 dwarf2_traverse_context_t lctx;
1104 enum location_error err;
1106 lctx.data = xloc.u.block.ptr;
1107 lctx.end_data = xloc.u.block.ptr + xloc.u.block.size;
1109 err = compute_location(ctx->module_ctx->module, &ctx->head, &lctx, loc, NULL, frame);
1110 if (err < 0)
1112 loc->kind = loc_error;
1113 loc->reg = err;
1115 else if (loc->kind == loc_dwarf2_block)
1117 unsigned* ptr = pool_alloc(&ctx->module_ctx->module->pool,
1118 sizeof(unsigned) + xloc.u.block.size);
1119 *ptr = xloc.u.block.size;
1120 memcpy(ptr + 1, xloc.u.block.ptr, xloc.u.block.size);
1121 loc->offset = (ULONG_PTR)ptr;
1124 return TRUE;
1127 static struct symt* dwarf2_lookup_type(const dwarf2_debug_info_t* di)
1129 struct attribute attr;
1130 dwarf2_debug_info_t* type;
1132 if (!dwarf2_find_attribute(di, DW_AT_type, &attr))
1133 /* this is only valid if current language of CU is C or C++ */
1134 return &symt_get_basic(btVoid, 0)->symt;
1135 if (!(type = dwarf2_jump_to_debug_info(&attr)))
1136 return &symt_get_basic(btNoType, 0)->symt;
1138 if (type == di)
1140 FIXME("Reference to itself\n");
1141 return &symt_get_basic(btNoType, 0)->symt;
1143 if (!type->symt)
1145 /* load the debug info entity */
1146 dwarf2_load_one_entry(type);
1147 if (!type->symt)
1149 FIXME("Unable to load forward reference for tag %Ix\n", type->abbrev->tag);
1150 return &symt_get_basic(btNoType, 0)->symt;
1153 return type->symt;
1156 static const char* dwarf2_get_cpp_name(dwarf2_debug_info_t* di, const char* name)
1158 char* last;
1159 struct attribute diname;
1160 struct attribute spec;
1162 if (di->abbrev->tag == DW_TAG_compile_unit || di->abbrev->tag == DW_TAG_partial_unit) return name;
1164 /* if the di is a definition, but has also a (previous) declaration, then scope must
1165 * be gotten from declaration not definition
1167 if (dwarf2_find_attribute(di, DW_AT_specification, &spec) && spec.gotten_from == attr_direct)
1169 di = dwarf2_jump_to_debug_info(&spec);
1170 if (!di)
1172 FIXME("Should have found the debug info entry\n");
1173 return NULL;
1177 if (!di->unit_ctx->cpp_name)
1179 di->unit_ctx->cpp_name = pool_alloc(&di->unit_ctx->pool, MAX_SYM_NAME);
1180 if (!di->unit_ctx->cpp_name) return name;
1182 last = di->unit_ctx->cpp_name + MAX_SYM_NAME - strlen(name) - 1;
1183 strcpy(last, name);
1185 for (di = di->parent; di; di = di->parent)
1187 switch (di->abbrev->tag)
1189 case DW_TAG_namespace:
1190 case DW_TAG_structure_type:
1191 case DW_TAG_class_type:
1192 case DW_TAG_interface_type:
1193 case DW_TAG_union_type:
1194 if (dwarf2_find_attribute(di, DW_AT_name, &diname))
1196 size_t len = strlen(diname.u.string);
1197 last -= 2 + len;
1198 if (last < di->unit_ctx->cpp_name)
1200 WARN("Too long C++ qualified identifier for %s... using unqualified identifier\n", name);
1201 return name;
1203 memcpy(last, diname.u.string, len);
1204 last[len] = last[len + 1] = ':';
1206 break;
1207 default:
1208 break;
1211 return last;
1214 static unsigned dwarf2_get_num_ranges(const dwarf2_debug_info_t* di)
1216 struct attribute range;
1218 if (dwarf2_find_attribute(di, DW_AT_ranges, &range))
1220 dwarf2_traverse_context_t traverse;
1221 unsigned num_ranges = 0;
1223 traverse.data = di->unit_ctx->module_ctx->sections[section_ranges].address + range.u.uvalue;
1224 traverse.end_data = di->unit_ctx->module_ctx->sections[section_ranges].address +
1225 di->unit_ctx->module_ctx->sections[section_ranges].size;
1227 for (num_ranges = 0; traverse.data + 2 * di->unit_ctx->head.word_size < traverse.end_data; num_ranges++)
1229 ULONG_PTR low = dwarf2_parse_addr_head(&traverse, &di->unit_ctx->head);
1230 ULONG_PTR high = dwarf2_parse_addr_head(&traverse, &di->unit_ctx->head);
1231 if (low == 0 && high == 0) break;
1232 if (low == (di->unit_ctx->head.word_size == 8 ? (~(DWORD64)0u) : (DWORD64)(~0u)))
1233 FIXME("unsupported yet (base address selection)\n");
1235 return num_ranges;
1237 else
1239 struct attribute low_pc;
1240 struct attribute high_pc;
1242 return dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc) &&
1243 dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc) ? 1 : 0;
1247 /* nun_ranges must have been gotten from dwarf2_get_num_ranges() */
1248 static BOOL dwarf2_fill_ranges(const dwarf2_debug_info_t* di, struct addr_range* ranges, unsigned num_ranges)
1250 struct attribute range;
1252 if (dwarf2_find_attribute(di, DW_AT_ranges, &range))
1254 dwarf2_traverse_context_t traverse;
1255 unsigned index;
1257 traverse.data = di->unit_ctx->module_ctx->sections[section_ranges].address + range.u.uvalue;
1258 traverse.end_data = di->unit_ctx->module_ctx->sections[section_ranges].address +
1259 di->unit_ctx->module_ctx->sections[section_ranges].size;
1261 for (index = 0; traverse.data + 2 * di->unit_ctx->head.word_size < traverse.end_data; index++)
1263 ULONG_PTR low = dwarf2_parse_addr_head(&traverse, &di->unit_ctx->head);
1264 ULONG_PTR high = dwarf2_parse_addr_head(&traverse, &di->unit_ctx->head);
1265 if (low == 0 && high == 0) break;
1266 if (low == (di->unit_ctx->head.word_size == 8 ? (~(DWORD64)0u) : (DWORD64)(~0u)))
1267 FIXME("unsupported yet (base address selection)\n");
1268 if (index >= num_ranges) return FALSE; /* sanity check */
1269 ranges[index].low = di->unit_ctx->compiland->address + low;
1270 ranges[index].high = di->unit_ctx->compiland->address + high;
1272 return index == num_ranges; /* sanity check */
1274 else
1276 struct attribute low_pc;
1277 struct attribute high_pc;
1279 if (num_ranges != 1 || /* sanity check */
1280 !dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc) ||
1281 !dwarf2_find_attribute(di, DW_AT_high_pc, &high_pc))
1282 return FALSE;
1283 if (di->unit_ctx->head.version >= 4)
1284 switch (high_pc.form)
1286 case DW_FORM_addr:
1287 break;
1288 case DW_FORM_data1:
1289 case DW_FORM_data2:
1290 case DW_FORM_data4:
1291 case DW_FORM_data8:
1292 case DW_FORM_sdata:
1293 case DW_FORM_udata:
1294 /* From dwarf4 on, when FORM's class is constant, high_pc is an offset from low_pc */
1295 high_pc.u.uvalue += low_pc.u.uvalue;
1296 break;
1297 default:
1298 FIXME("Unsupported class for high_pc\n");
1299 break;
1301 ranges[0].low = di->unit_ctx->module_ctx->load_offset + low_pc.u.uvalue;
1302 ranges[0].high = di->unit_ctx->module_ctx->load_offset + high_pc.u.uvalue;
1304 return TRUE;
1307 static struct addr_range* dwarf2_get_ranges(const dwarf2_debug_info_t* di, unsigned* num_ranges)
1309 unsigned nr = dwarf2_get_num_ranges(di);
1310 struct addr_range* ranges;
1312 if (nr == 0) return NULL;
1313 ranges = malloc(nr * sizeof(ranges[0]));
1314 if (!ranges || !dwarf2_fill_ranges(di, ranges, nr)) return NULL;
1315 *num_ranges = nr;
1316 return ranges;
1319 /******************************************************************
1320 * dwarf2_read_one_debug_info
1322 * Loads into memory one debug info entry, and recursively its children (if any)
1324 static BOOL dwarf2_read_one_debug_info(dwarf2_parse_context_t* ctx,
1325 dwarf2_traverse_context_t* traverse,
1326 dwarf2_debug_info_t* parent_di,
1327 dwarf2_debug_info_t** pdi)
1329 const dwarf2_abbrev_entry_t*abbrev;
1330 ULONG_PTR entry_code;
1331 ULONG_PTR offset;
1332 dwarf2_debug_info_t* di;
1333 dwarf2_debug_info_t* child;
1334 dwarf2_debug_info_t** where;
1335 dwarf2_abbrev_entry_attr_t* attr;
1336 unsigned i;
1337 struct attribute sibling;
1339 offset = traverse->data - ctx->module_ctx->sections[ctx->section].address;
1340 entry_code = dwarf2_leb128_as_unsigned(traverse);
1341 TRACE("found entry_code %Iu at 0x%Ix\n", entry_code, offset);
1342 if (!entry_code)
1344 *pdi = NULL;
1345 return TRUE;
1347 abbrev = dwarf2_abbrev_table_find_entry(&ctx->abbrev_table, entry_code);
1348 if (!abbrev)
1350 WARN("Cannot find abbrev entry for %Iu at 0x%Ix\n", entry_code, offset);
1351 return FALSE;
1353 di = sparse_array_add(&ctx->debug_info_table, offset, &ctx->pool);
1354 if (!di) return FALSE;
1355 di->abbrev = abbrev;
1356 di->symt = NULL;
1357 di->parent = parent_di;
1358 di->unit_ctx = ctx;
1360 if (abbrev->num_attr)
1362 di->data = pool_alloc(&ctx->pool, abbrev->num_attr * sizeof(const char*));
1363 for (i = 0, attr = abbrev->attrs; attr; i++, attr = attr->next)
1365 di->data[i] = traverse->data;
1366 dwarf2_swallow_attribute(traverse, &ctx->head, attr);
1369 else di->data = NULL;
1370 if (abbrev->have_child)
1372 vector_init(&di->children, sizeof(dwarf2_debug_info_t*), 16);
1373 while (traverse->data < traverse->end_data)
1375 if (!dwarf2_read_one_debug_info(ctx, traverse, di, &child)) return FALSE;
1376 if (!child) break;
1377 where = vector_add(&di->children, &ctx->pool);
1378 if (!where) return FALSE;
1379 *where = child;
1382 if (dwarf2_find_attribute(di, DW_AT_sibling, &sibling) &&
1383 traverse->data != ctx->module_ctx->sections[ctx->section].address + sibling.u.uvalue)
1385 if (sibling.u.uvalue >= ctx->module_ctx->sections[ctx->section].size)
1387 FIXME("cursor sibling after section end %s: 0x%Ix 0x%x\n",
1388 dwarf2_debug_unit_ctx(ctx), sibling.u.uvalue, ctx->module_ctx->sections[ctx->section].size);
1389 return FALSE;
1391 WARN("setting cursor for %s to next sibling <0x%Ix>\n",
1392 dwarf2_debug_traverse_ctx(traverse), sibling.u.uvalue);
1393 traverse->data = ctx->module_ctx->sections[ctx->section].address + sibling.u.uvalue;
1395 *pdi = di;
1396 return TRUE;
1399 static struct vector* dwarf2_get_di_children(dwarf2_debug_info_t* di)
1401 struct attribute spec;
1403 while (di)
1405 if (di->abbrev->have_child)
1406 return &di->children;
1407 if (!dwarf2_find_attribute(di, DW_AT_specification, &spec)) break;
1408 if (!(di = dwarf2_jump_to_debug_info(&spec)))
1409 FIXME("Should have found the debug info entry\n");
1411 return NULL;
1414 /* reconstruct whether integer is long (contains 'long' only once) */
1415 static BOOL is_long(const char* name)
1417 /* we assume name is made only of basic C keywords:
1418 * int long short unsigned signed void float double char _Bool _Complex
1420 const char* p = strstr(name, "long");
1421 return p && strstr(p + 4, "long") == NULL;
1424 static BOOL is_c_language(dwarf2_parse_context_t* unit_ctx)
1426 return unit_ctx->language == DW_LANG_C ||
1427 unit_ctx->language == DW_LANG_C89 ||
1428 unit_ctx->language == DW_LANG_C99;
1431 static BOOL is_cpp_language(dwarf2_parse_context_t* unit_ctx)
1433 return unit_ctx->language == DW_LANG_C_plus_plus;
1436 static struct symt* dwarf2_parse_base_type(dwarf2_debug_info_t* di)
1438 struct attribute name;
1439 struct attribute size;
1440 struct attribute encoding;
1441 enum BasicType bt;
1442 BOOL c_language, cpp_language;
1444 if (di->symt) return di->symt;
1446 TRACE("%s\n", dwarf2_debug_di(di));
1448 c_language = is_c_language(di->unit_ctx);
1449 cpp_language = is_cpp_language(di->unit_ctx);
1451 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1452 name.u.string = NULL;
1453 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1454 if (!dwarf2_find_attribute(di, DW_AT_encoding, &encoding)) encoding.u.uvalue = DW_ATE_void;
1456 switch (encoding.u.uvalue)
1458 case DW_ATE_void: bt = btVoid; break;
1459 case DW_ATE_address: bt = btULong; break;
1460 case DW_ATE_boolean: bt = btBool; break;
1461 case DW_ATE_complex_float: bt = btComplex; break;
1462 case DW_ATE_float: bt = btFloat; break;
1463 case DW_ATE_signed: bt = ((c_language || cpp_language) && is_long(name.u.string)) ? btLong : btInt; break;
1464 case DW_ATE_unsigned:
1465 if ((c_language || cpp_language) && is_long(name.u.string)) bt = btULong;
1466 else if (cpp_language && !strcmp(name.u.string, "wchar_t")) bt = btWChar;
1467 else if (cpp_language && !strcmp(name.u.string, "char8_t")) bt = btChar8;
1468 else if (cpp_language && !strcmp(name.u.string, "char16_t")) bt = btChar16;
1469 else if (cpp_language && !strcmp(name.u.string, "char32_t")) bt = btChar32;
1470 else bt = btUInt;
1471 break;
1472 /* on Windows, in C, char == signed char, but not in C++ */
1473 case DW_ATE_signed_char: bt = (cpp_language && !strcmp(name.u.string, "signed char")) ? btInt : btChar; break;
1474 case DW_ATE_unsigned_char: bt = btUInt; break;
1475 case DW_ATE_UTF: bt = (size.u.uvalue == 1) ? btChar8 : (size.u.uvalue == 2 ? btChar16 : btChar32); break;
1476 default: bt = btNoType; break;
1478 di->symt = &symt_get_basic(bt, size.u.uvalue)->symt;
1480 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1481 return di->symt;
1484 static struct symt* dwarf2_parse_typedef(dwarf2_debug_info_t* di)
1486 struct symt* ref_type;
1487 struct attribute name;
1489 if (di->symt) return di->symt;
1491 TRACE("%s\n", dwarf2_debug_di(di));
1493 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1494 ref_type = dwarf2_lookup_type(di);
1496 if (name.u.string)
1498 /* Note: The MS C compiler has tweaks for WCHAR support.
1499 * Even if WCHAR is a typedef to wchar_t, wchar_t is emitted as btUInt/2 (it's defined as
1500 * unsigned short, so far so good), while WCHAR is emitted as btWChar/2).
1502 if ((is_c_language(di->unit_ctx) || is_cpp_language(di->unit_ctx)) && !strcmp(name.u.string, "WCHAR"))
1503 ref_type = &symt_get_basic(btWChar, 2)->symt;
1504 di->symt = &symt_new_typedef(di->unit_ctx->module_ctx->module, ref_type, name.u.string)->symt;
1506 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1507 return di->symt;
1510 static struct symt* dwarf2_parse_pointer_type(dwarf2_debug_info_t* di)
1512 struct symt* ref_type;
1513 struct attribute size;
1515 if (di->symt) return di->symt;
1517 TRACE("%s\n", dwarf2_debug_di(di));
1519 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = di->unit_ctx->module_ctx->module->cpu->word_size;
1520 ref_type = dwarf2_lookup_type(di);
1521 di->symt = &symt_new_pointer(di->unit_ctx->module_ctx->module, ref_type, size.u.uvalue)->symt;
1522 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1523 return di->symt;
1526 static struct symt* dwarf2_parse_subrange_type(dwarf2_debug_info_t* di)
1528 struct symt* ref_type;
1529 struct attribute name;
1530 struct attribute dummy;
1532 if (di->symt) return di->symt;
1534 TRACE("%s\n", dwarf2_debug_di(di));
1536 if (dwarf2_find_attribute(di, DW_AT_name, &name)) FIXME("Found name for subrange %s\n", name.u.string);
1537 if (dwarf2_find_attribute(di, DW_AT_byte_size, &dummy)) FIXME("Found byte_size %Iu\n", dummy.u.uvalue);
1538 if (dwarf2_find_attribute(di, DW_AT_bit_size, &dummy)) FIXME("Found bit_size %Iu\n", dummy.u.uvalue);
1539 /* for now, we don't support the byte_size nor bit_size about the subrange, and pretend the two
1540 * types are the same (FIXME)
1542 ref_type = dwarf2_lookup_type(di);
1543 di->symt = ref_type;
1544 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1545 return di->symt;
1548 static struct symt* dwarf2_parse_array_type(dwarf2_debug_info_t* di)
1550 struct symt* ref_type;
1551 struct symt* idx_type = NULL;
1552 struct symt* symt = NULL;
1553 struct attribute min, max, cnt;
1554 dwarf2_debug_info_t* child;
1555 unsigned int i, j;
1556 const struct vector* children;
1558 if (di->symt) return di->symt;
1560 TRACE("%s\n", dwarf2_debug_di(di));
1562 ref_type = dwarf2_lookup_type(di);
1564 if (!(children = dwarf2_get_di_children(di)))
1566 /* fake an array with unknown size */
1567 /* FIXME: int4 even on 64bit machines??? */
1568 idx_type = &symt_get_basic(btInt, 4)->symt;
1569 min.u.uvalue = 0;
1570 cnt.u.uvalue = 0;
1572 else for (i = 0; i < vector_length(children); i++)
1574 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1575 if (child->symt == &symt_get_basic(btNoType, 0)->symt) continue;
1576 switch (child->abbrev->tag)
1578 case DW_TAG_subrange_type:
1579 idx_type = dwarf2_lookup_type(child);
1580 if (!dwarf2_find_attribute(child, DW_AT_lower_bound, &min))
1581 min.u.uvalue = 0;
1582 if (dwarf2_find_attribute(child, DW_AT_upper_bound, &max))
1583 cnt.u.uvalue = max.u.uvalue + 1 - min.u.uvalue;
1584 else if (!dwarf2_find_attribute(child, DW_AT_count, &cnt))
1585 cnt.u.uvalue = 0;
1586 break;
1587 case DW_TAG_enumeration_type:
1588 symt = dwarf2_parse_enumeration_type(child);
1589 if (symt_check_tag(symt, SymTagEnum))
1591 struct symt_enum* enum_symt = (struct symt_enum*)symt;
1592 idx_type = enum_symt->base_type;
1593 min.u.uvalue = ~0U;
1594 max.u.uvalue = ~0U;
1595 for (j = 0; j < enum_symt->vchildren.num_elts; ++j)
1597 struct symt** pc = vector_at(&enum_symt->vchildren, j);
1598 if (pc && symt_check_tag(*pc, SymTagData))
1600 struct symt_data* elt = (struct symt_data*)(*pc);
1601 if (elt->u.value.lVal < min.u.uvalue)
1602 min.u.uvalue = elt->u.value.lVal;
1603 if (elt->u.value.lVal > max.u.uvalue)
1604 max.u.uvalue = elt->u.value.lVal;
1608 break;
1609 default:
1610 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1611 child->abbrev->tag, dwarf2_debug_di(di));
1612 break;
1615 di->symt = &symt_new_array(di->unit_ctx->module_ctx->module, min.u.uvalue, cnt.u.uvalue, ref_type, idx_type)->symt;
1616 return di->symt;
1619 static struct symt* dwarf2_parse_const_type(dwarf2_debug_info_t* di)
1621 struct symt* ref_type;
1623 if (di->symt) return di->symt;
1625 TRACE("%s\n", dwarf2_debug_di(di));
1627 ref_type = dwarf2_lookup_type(di);
1628 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1629 di->symt = ref_type;
1631 return ref_type;
1634 static struct symt* dwarf2_parse_volatile_type(dwarf2_debug_info_t* di)
1636 struct symt* ref_type;
1638 if (di->symt) return di->symt;
1640 TRACE("%s\n", dwarf2_debug_di(di));
1642 ref_type = dwarf2_lookup_type(di);
1643 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1644 di->symt = ref_type;
1646 return ref_type;
1649 static struct symt* dwarf2_parse_restrict_type(dwarf2_debug_info_t* di)
1651 struct symt* ref_type;
1653 if (di->symt) return di->symt;
1655 TRACE("%s\n", dwarf2_debug_di(di));
1657 ref_type = dwarf2_lookup_type(di);
1658 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1659 di->symt = ref_type;
1661 return ref_type;
1664 static struct symt* dwarf2_parse_unspecified_type(dwarf2_debug_info_t* di)
1666 struct attribute name;
1667 struct symt* basic;
1669 TRACE("%s\n", dwarf2_debug_di(di));
1671 if (di->symt) return di->symt;
1673 basic = &symt_get_basic(btVoid, 0)->symt;
1674 if (dwarf2_find_attribute(di, DW_AT_name, &name))
1675 /* define the missing type as a typedef to void... */
1676 di->symt = &symt_new_typedef(di->unit_ctx->module_ctx->module, basic, name.u.string)->symt;
1677 else /* or use void if it doesn't even have a name */
1678 di->symt = basic;
1680 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1681 return di->symt;
1684 static struct symt* dwarf2_parse_reference_type(dwarf2_debug_info_t* di)
1686 struct symt* ref_type = NULL;
1688 if (di->symt) return di->symt;
1690 TRACE("%s\n", dwarf2_debug_di(di));
1692 ref_type = dwarf2_lookup_type(di);
1693 /* FIXME: for now, we hard-wire C++ references to pointers */
1694 di->symt = &symt_new_pointer(di->unit_ctx->module_ctx->module, ref_type,
1695 di->unit_ctx->module_ctx->module->cpu->word_size)->symt;
1697 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1699 return di->symt;
1702 static void dwarf2_parse_udt_member(dwarf2_debug_info_t* di,
1703 struct symt_udt* parent)
1705 struct symt* elt_type;
1706 struct attribute name;
1707 struct attribute bit_size;
1708 struct attribute bit_offset;
1709 struct location loc;
1711 assert(parent);
1713 TRACE("%s\n", dwarf2_debug_di(di));
1715 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1716 elt_type = dwarf2_lookup_type(di);
1717 if (dwarf2_compute_location_attr(di->unit_ctx, di, DW_AT_data_member_location, &loc, NULL))
1719 if (loc.kind != loc_absolute)
1721 FIXME("Unexpected offset computation for member %s in %ls!%s\n",
1722 name.u.string, di->unit_ctx->module_ctx->module->modulename, parent->hash_elt.name);
1723 loc.offset = 0;
1725 else
1726 TRACE("found member_location at %s -> %Iu\n",
1727 dwarf2_debug_di(di), loc.offset);
1729 else
1730 loc.offset = 0;
1731 if (!dwarf2_find_attribute(di, DW_AT_bit_size, &bit_size))
1732 bit_size.u.uvalue = 0;
1733 if (dwarf2_find_attribute(di, DW_AT_bit_offset, &bit_offset))
1735 /* FIXME: we should only do this when implementation is LSB (which is
1736 * the case on i386 processors)
1738 struct attribute nbytes;
1739 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &nbytes))
1741 DWORD64 size;
1742 nbytes.u.uvalue = symt_get_info(di->unit_ctx->module_ctx->module, elt_type, TI_GET_LENGTH, &size) ?
1743 (ULONG_PTR)size : 0;
1745 bit_offset.u.uvalue = nbytes.u.uvalue * 8 - bit_offset.u.uvalue - bit_size.u.uvalue;
1747 else bit_offset.u.uvalue = 0;
1748 symt_add_udt_element(di->unit_ctx->module_ctx->module, parent, name.u.string, elt_type,
1749 loc.offset, bit_offset.u.uvalue,
1750 bit_size.u.uvalue);
1752 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1755 static struct symt* dwarf2_parse_subprogram(dwarf2_debug_info_t* di);
1757 static struct symt* dwarf2_parse_udt_type(dwarf2_debug_info_t* di,
1758 enum UdtKind udt)
1760 struct attribute name;
1761 struct attribute size;
1762 struct vector* children;
1763 dwarf2_debug_info_t*child;
1764 unsigned int i;
1766 if (di->symt) return di->symt;
1768 TRACE("%s\n", dwarf2_debug_di(di));
1770 /* quirk... FIXME provide real support for anonymous UDTs */
1771 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
1772 name.u.string = "<unnamed-tag>";
1773 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 0;
1775 di->symt = &symt_new_udt(di->unit_ctx->module_ctx->module, dwarf2_get_cpp_name(di, name.u.string),
1776 size.u.uvalue, udt)->symt;
1778 children = dwarf2_get_di_children(di);
1779 if (children) for (i = 0; i < vector_length(children); i++)
1781 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1783 switch (child->abbrev->tag)
1785 case DW_TAG_array_type:
1786 dwarf2_parse_array_type(child);
1787 break;
1788 case DW_TAG_member:
1789 /* FIXME: should I follow the sibling stuff ?? */
1790 if (symt_check_tag(di->symt, SymTagUDT))
1791 dwarf2_parse_udt_member(child, (struct symt_udt*)di->symt);
1792 break;
1793 case DW_TAG_enumeration_type:
1794 dwarf2_parse_enumeration_type(child);
1795 break;
1796 case DW_TAG_subprogram:
1797 dwarf2_parse_subprogram(child);
1798 break;
1799 case DW_TAG_const_type:
1800 dwarf2_parse_const_type(child);
1801 break;
1802 case DW_TAG_volatile_type:
1803 dwarf2_parse_volatile_type(child);
1804 break;
1805 case DW_TAG_pointer_type:
1806 dwarf2_parse_pointer_type(child);
1807 break;
1808 case DW_TAG_subrange_type:
1809 dwarf2_parse_subrange_type(child);
1810 break;
1811 case DW_TAG_structure_type:
1812 case DW_TAG_class_type:
1813 case DW_TAG_union_type:
1814 case DW_TAG_typedef:
1815 /* FIXME: we need to handle nested udt definitions */
1816 case DW_TAG_inheritance:
1817 case DW_TAG_interface_type:
1818 case DW_TAG_template_type_param:
1819 case DW_TAG_template_value_param:
1820 case DW_TAG_variable:
1821 case DW_TAG_imported_declaration:
1822 case DW_TAG_ptr_to_member_type:
1823 case DW_TAG_GNU_template_template_param:
1824 case DW_TAG_GNU_template_parameter_pack:
1825 case DW_TAG_GNU_formal_parameter_pack:
1826 /* FIXME: some C++ related stuff */
1827 break;
1828 default:
1829 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1830 child->abbrev->tag, dwarf2_debug_di(di));
1831 break;
1835 return di->symt;
1838 static void dwarf2_parse_enumerator(dwarf2_debug_info_t* di,
1839 struct symt_enum* parent)
1841 struct attribute name;
1842 struct attribute value;
1844 TRACE("%s\n", dwarf2_debug_di(di));
1846 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) return;
1847 if (!dwarf2_find_attribute(di, DW_AT_const_value, &value)) value.u.svalue = 0;
1848 symt_add_enum_element(di->unit_ctx->module_ctx->module, parent, name.u.string, value.u.svalue);
1850 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
1853 static struct symt* dwarf2_parse_enumeration_type(dwarf2_debug_info_t* di)
1855 struct attribute name;
1856 struct attribute attrtype;
1857 dwarf2_debug_info_t*ditype;
1858 struct symt* type;
1859 struct vector* children;
1860 dwarf2_debug_info_t*child;
1861 unsigned int i;
1863 if (di->symt) return di->symt;
1865 TRACE("%s\n", dwarf2_debug_di(di));
1867 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) name.u.string = NULL;
1868 if (dwarf2_find_attribute(di, DW_AT_type, &attrtype) && (ditype = dwarf2_jump_to_debug_info(&attrtype)) != NULL)
1869 type = ditype->symt;
1870 else /* no type found for this enumeration, construct it from size */
1872 struct attribute size;
1873 struct symt_basic* basetype;
1875 if (!dwarf2_find_attribute(di, DW_AT_byte_size, &size)) size.u.uvalue = 4;
1877 switch (size.u.uvalue) /* FIXME: that's wrong */
1879 case 1: basetype = symt_get_basic(btInt, 1); break;
1880 case 2: basetype = symt_get_basic(btInt, 2); break;
1881 default:
1882 case 4: basetype = symt_get_basic(btInt, 4); break;
1884 type = &basetype->symt;
1887 di->symt = &symt_new_enum(di->unit_ctx->module_ctx->module, name.u.string, type)->symt;
1888 children = dwarf2_get_di_children(di);
1890 if (children) for (i = 0; i < vector_length(children); i++)
1892 child = *(dwarf2_debug_info_t**)vector_at(children, i);
1894 switch (child->abbrev->tag)
1896 case DW_TAG_enumerator:
1897 if (symt_check_tag(di->symt, SymTagEnum))
1898 dwarf2_parse_enumerator(child, (struct symt_enum*)di->symt);
1899 break;
1900 default:
1901 FIXME("Unhandled Tag type 0x%Ix at %s\n",
1902 di->abbrev->tag, dwarf2_debug_di(di));
1905 return di->symt;
1908 /* structure used to pass information around when parsing a subprogram */
1909 typedef struct dwarf2_subprogram_s
1911 dwarf2_parse_context_t* ctx;
1912 struct symt_function* top_func;
1913 struct symt_function* current_func; /* either symt_function* or symt_inlinesite* */
1914 struct symt_block* current_block;
1915 BOOL non_computed_variable;
1916 struct location frame;
1917 } dwarf2_subprogram_t;
1919 /******************************************************************
1920 * dwarf2_parse_variable
1922 * Parses any variable (parameter, local/global variable)
1924 static void dwarf2_parse_variable(dwarf2_subprogram_t* subpgm,
1925 dwarf2_debug_info_t* di)
1927 struct symt* param_type;
1928 struct attribute name, value;
1929 struct location loc;
1930 BOOL is_pmt;
1932 TRACE("%s\n", dwarf2_debug_di(di));
1934 is_pmt = !subpgm->current_block && di->abbrev->tag == DW_TAG_formal_parameter;
1935 param_type = dwarf2_lookup_type(di);
1937 if (!dwarf2_find_attribute(di, DW_AT_name, &name)) {
1938 /* cannot do much without the name, the functions below won't like it. */
1939 return;
1941 if (dwarf2_compute_location_attr(subpgm->ctx, di, DW_AT_location,
1942 &loc, &subpgm->frame))
1944 struct attribute ext;
1946 TRACE("found parameter %s (kind=%d, offset=%Id, reg=%d) at %s\n",
1947 debugstr_a(name.u.string), loc.kind, loc.offset, loc.reg,
1948 dwarf2_debug_unit_ctx(subpgm->ctx));
1950 switch (loc.kind)
1952 case loc_error:
1953 break;
1954 case loc_absolute:
1955 /* it's a global variable */
1956 if (!dwarf2_find_attribute(di, DW_AT_external, &ext))
1957 ext.u.uvalue = 0;
1958 loc.offset += subpgm->ctx->module_ctx->load_offset;
1959 if (subpgm->top_func)
1961 if (ext.u.uvalue) WARN("unexpected global inside a function\n");
1962 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->current_func,
1963 DataIsStaticLocal, &loc, subpgm->current_block,
1964 param_type, dwarf2_get_cpp_name(di, name.u.string));
1966 else
1968 symt_new_global_variable(subpgm->ctx->module_ctx->module,
1969 ext.u.uvalue ? NULL : subpgm->ctx->compiland,
1970 dwarf2_get_cpp_name(di, name.u.string), !ext.u.uvalue,
1971 loc, 0, param_type);
1973 break;
1974 default:
1975 subpgm->non_computed_variable = TRUE;
1976 /* fall through */
1977 case loc_register:
1978 case loc_regrel:
1979 /* either a pmt/variable relative to frame pointer or
1980 * pmt/variable in a register
1982 if (subpgm->current_func)
1983 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->current_func,
1984 is_pmt ? DataIsParam : DataIsLocal,
1985 &loc, subpgm->current_block, param_type, name.u.string);
1986 break;
1989 else if (dwarf2_find_attribute(di, DW_AT_const_value, &value))
1991 VARIANT v;
1993 switch (value.form)
1995 case DW_FORM_data1:
1996 case DW_FORM_data2:
1997 case DW_FORM_data4:
1998 case DW_FORM_udata:
1999 case DW_FORM_addr:
2000 V_VT(&v) = VT_UI4;
2001 V_UI4(&v) = value.u.uvalue;
2002 break;
2004 case DW_FORM_data8:
2005 case DW_FORM_sec_offset:
2006 V_VT(&v) = VT_UI8;
2007 V_UI8(&v) = value.u.lluvalue;
2008 break;
2010 case DW_FORM_sdata:
2011 V_VT(&v) = VT_I4;
2012 V_I4(&v) = value.u.svalue;
2013 break;
2015 case DW_FORM_strp:
2016 case DW_FORM_string:
2017 /* FIXME: native doesn't report const strings from here !!
2018 * however, the value of the string is in the code somewhere
2020 V_VT(&v) = VT_BYREF;
2021 V_BYREF(&v) = pool_strdup(&subpgm->ctx->module_ctx->module->pool, value.u.string);
2022 break;
2024 case DW_FORM_block:
2025 case DW_FORM_block1:
2026 case DW_FORM_block2:
2027 case DW_FORM_block4:
2028 case DW_FORM_exprloc:
2029 V_VT(&v) = VT_I4;
2030 switch (value.u.block.size)
2032 case 1: V_I4(&v) = *(BYTE*)value.u.block.ptr; break;
2033 case 2: V_I4(&v) = *(USHORT*)value.u.block.ptr; break;
2034 case 4: V_I4(&v) = *(DWORD*)value.u.block.ptr; break;
2035 default:
2036 V_VT(&v) = VT_BYREF;
2037 V_BYREF(&v) = pool_alloc(&subpgm->ctx->module_ctx->module->pool, value.u.block.size);
2038 memcpy(V_BYREF(&v), value.u.block.ptr, value.u.block.size);
2040 break;
2042 default:
2043 FIXME("Unsupported form for const value %s (%Ix)\n",
2044 debugstr_a(name.u.string), value.form);
2045 V_VT(&v) = VT_EMPTY;
2047 if (subpgm->current_func)
2049 if (is_pmt) WARN("Constant parameter %s reported as local variable in function '%s'\n",
2050 debugstr_a(name.u.string), subpgm->current_func->hash_elt.name);
2051 di->symt = &symt_add_func_constant(subpgm->ctx->module_ctx->module,
2052 subpgm->current_func, subpgm->current_block,
2053 param_type, name.u.string, &v)->symt;
2055 else
2056 di->symt = &symt_new_constant(subpgm->ctx->module_ctx->module, subpgm->ctx->compiland,
2057 name.u.string, param_type, &v)->symt;
2059 else
2061 if (subpgm->current_func)
2063 /* local variable has been optimized away... report anyway */
2064 loc.kind = loc_error;
2065 loc.reg = loc_err_no_location;
2066 symt_add_func_local(subpgm->ctx->module_ctx->module, subpgm->current_func,
2067 is_pmt ? DataIsParam : DataIsLocal,
2068 &loc, subpgm->current_block, param_type, name.u.string);
2070 else
2072 struct attribute is_decl;
2073 /* only warn when di doesn't represent a declaration */
2074 if (!dwarf2_find_attribute(di, DW_AT_declaration, &is_decl) ||
2075 !is_decl.u.uvalue || is_decl.gotten_from != attr_direct)
2076 WARN("dropping global variable %s which has been optimized away\n", debugstr_a(name.u.string));
2079 if (is_pmt && subpgm->current_func && symt_check_tag(subpgm->current_func->type, SymTagFunctionType))
2080 symt_add_function_signature_parameter(subpgm->ctx->module_ctx->module,
2081 (struct symt_function_signature*)subpgm->current_func->type,
2082 param_type);
2084 if (dwarf2_get_di_children(di)) FIXME("Unsupported children\n");
2087 static void dwarf2_parse_subprogram_label(dwarf2_subprogram_t* subpgm,
2088 const dwarf2_debug_info_t* di)
2090 struct attribute name;
2091 struct attribute low_pc;
2092 struct location loc;
2094 TRACE("%s\n", dwarf2_debug_di(di));
2096 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2097 name.u.string = NULL;
2098 if (dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc))
2100 loc.kind = loc_absolute;
2101 loc.offset = subpgm->ctx->module_ctx->load_offset + low_pc.u.uvalue - subpgm->top_func->ranges[0].low;
2102 symt_add_function_point(subpgm->ctx->module_ctx->module, subpgm->top_func, SymTagLabel,
2103 &loc, name.u.string);
2105 else
2106 WARN("Label %s inside function %s doesn't have an address... don't register it\n",
2107 name.u.string, subpgm->top_func->hash_elt.name);
2110 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
2111 dwarf2_debug_info_t* di);
2113 static struct symt* dwarf2_parse_subroutine_type(dwarf2_debug_info_t* di);
2115 static void dwarf2_parse_inlined_subroutine(dwarf2_subprogram_t* subpgm,
2116 dwarf2_debug_info_t* di)
2118 struct attribute name;
2119 struct symt* ret_type;
2120 struct symt_function_signature* sig_type;
2121 struct symt_function* inlined;
2122 struct vector* children;
2123 dwarf2_debug_info_t*child;
2124 unsigned int i;
2125 unsigned num_ranges;
2127 TRACE("%s\n", dwarf2_debug_di(di));
2129 if (!(num_ranges = dwarf2_get_num_ranges(di)))
2131 WARN("cannot read ranges\n");
2132 return;
2134 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2136 FIXME("No name for function... dropping function\n");
2137 return;
2139 ret_type = dwarf2_lookup_type(di);
2141 /* FIXME: assuming C source code */
2142 sig_type = symt_new_function_signature(subpgm->ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2144 inlined = symt_new_inlinesite(subpgm->ctx->module_ctx->module,
2145 subpgm->top_func,
2146 subpgm->current_block ? &subpgm->current_block->symt : &subpgm->current_func->symt,
2147 dwarf2_get_cpp_name(di, name.u.string),
2148 &sig_type->symt, num_ranges);
2149 subpgm->current_func = inlined;
2150 subpgm->current_block = NULL;
2152 if (!dwarf2_fill_ranges(di, inlined->ranges, num_ranges))
2154 FIXME("Unexpected situation\n");
2155 inlined->num_ranges = 0;
2158 children = dwarf2_get_di_children(di);
2159 if (children) for (i = 0; i < vector_length(children); i++)
2161 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2163 switch (child->abbrev->tag)
2165 case DW_TAG_formal_parameter:
2166 case DW_TAG_variable:
2167 dwarf2_parse_variable(subpgm, child);
2168 break;
2169 case DW_TAG_lexical_block:
2170 dwarf2_parse_subprogram_block(subpgm, child);
2171 break;
2172 case DW_TAG_inlined_subroutine:
2173 dwarf2_parse_inlined_subroutine(subpgm, child);
2174 break;
2175 case DW_TAG_label:
2176 dwarf2_parse_subprogram_label(subpgm, child);
2177 break;
2178 case DW_TAG_GNU_call_site:
2179 /* this isn't properly supported by dbghelp interface. skip it for now */
2180 break;
2181 default:
2182 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2183 child->abbrev->tag, dwarf2_debug_di(di));
2186 subpgm->current_block = symt_check_tag(subpgm->current_func->container, SymTagBlock) ?
2187 (struct symt_block*)subpgm->current_func->container : NULL;
2188 subpgm->current_func = (struct symt_function*)symt_get_upper_inlined(subpgm->current_func);
2191 static void dwarf2_parse_subprogram_block(dwarf2_subprogram_t* subpgm,
2192 dwarf2_debug_info_t* di)
2194 unsigned int num_ranges;
2195 struct vector* children;
2196 dwarf2_debug_info_t*child;
2197 unsigned int i;
2199 TRACE("%s\n", dwarf2_debug_di(di));
2201 num_ranges = dwarf2_get_num_ranges(di);
2202 if (!num_ranges)
2204 WARN("no ranges\n");
2205 return;
2208 /* Dwarf tends to keep the structure of the C/C++ program, and emits DW_TAG_lexical_block
2209 * for every block the in source program.
2210 * With inlining and other optimizations, code for a block no longer lies in a contiguous
2211 * chunk of memory. It's hence described with (potentially) multiple chunks of memory.
2212 * Then each variable of each block is attached to its block. (A)
2214 * PDB on the other hand no longer emits block information, and merge variable information
2215 * at function level (actually function and each inline site).
2216 * For example, if several variables of same name exist in different blocks of a function,
2217 * only one entry for that name will exist. The information stored in (A) will point
2218 * to the correct instance as defined by C/C++ scoping rules.
2220 * (A) in all cases, there is information telling for each address of the function if a
2221 * variable is accessible, and if so, how to get its value.
2223 * DbgHelp only exposes a contiguous chunk of memory for a block.
2225 * => Store internally all the ranges of addresses in a block, but only expose the size
2226 * of the first chunk of memory.
2227 * Otherwise, it would break the rule: blocks' chunks don't overlap.
2228 * Note: This could mislead some programs using the blocks' address and size information.
2229 * That's very unlikely to happen (most will use the APIs from DbgHelp, which will
2230 * hide this information to the caller).
2232 subpgm->current_block = symt_open_func_block(subpgm->ctx->module_ctx->module, subpgm->current_func,
2233 subpgm->current_block, num_ranges);
2234 if (!dwarf2_fill_ranges(di, subpgm->current_block->ranges, num_ranges))
2236 FIXME("Unexpected situation\n");
2237 subpgm->current_block->num_ranges = 0;
2240 children = dwarf2_get_di_children(di);
2241 if (children) for (i = 0; i < vector_length(children); i++)
2243 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2245 switch (child->abbrev->tag)
2247 case DW_TAG_inlined_subroutine:
2248 dwarf2_parse_inlined_subroutine(subpgm, child);
2249 break;
2250 case DW_TAG_variable:
2251 dwarf2_parse_variable(subpgm, child);
2252 break;
2253 case DW_TAG_pointer_type:
2254 dwarf2_parse_pointer_type(child);
2255 break;
2256 case DW_TAG_subroutine_type:
2257 dwarf2_parse_subroutine_type(child);
2258 break;
2259 case DW_TAG_const_type:
2260 dwarf2_parse_const_type(child);
2261 break;
2262 case DW_TAG_lexical_block:
2263 dwarf2_parse_subprogram_block(subpgm, child);
2264 break;
2265 case DW_TAG_subprogram:
2266 /* FIXME: likely a declaration (to be checked)
2267 * skip it for now
2269 break;
2270 case DW_TAG_formal_parameter:
2271 /* FIXME: likely elements for exception handling (GCC flavor)
2272 * Skip it for now
2274 break;
2275 case DW_TAG_imported_module:
2276 /* C++ stuff to be silenced (for now) */
2277 break;
2278 case DW_TAG_GNU_call_site:
2279 /* this isn't properly supported by dbghelp interface. skip it for now */
2280 break;
2281 case DW_TAG_label:
2282 dwarf2_parse_subprogram_label(subpgm, child);
2283 break;
2284 case DW_TAG_class_type:
2285 case DW_TAG_structure_type:
2286 case DW_TAG_union_type:
2287 case DW_TAG_enumeration_type:
2288 case DW_TAG_typedef:
2289 /* the type referred to will be loaded when we need it, so skip it */
2290 break;
2291 default:
2292 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2293 child->abbrev->tag, dwarf2_debug_di(di));
2297 subpgm->current_block = symt_close_func_block(subpgm->ctx->module_ctx->module, subpgm->current_func, subpgm->current_block);
2300 static struct symt* dwarf2_parse_subprogram(dwarf2_debug_info_t* di)
2302 struct attribute name;
2303 struct addr_range* addr_ranges;
2304 unsigned num_addr_ranges;
2305 struct attribute is_decl;
2306 struct attribute inline_flags;
2307 struct symt* ret_type;
2308 struct symt_function_signature* sig_type;
2309 dwarf2_subprogram_t subpgm;
2310 struct vector* children;
2311 dwarf2_debug_info_t* child;
2312 unsigned int i;
2314 if (di->symt) return di->symt;
2316 TRACE("%s\n", dwarf2_debug_di(di));
2318 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2320 WARN("No name for function... dropping function\n");
2321 return NULL;
2323 /* if it's an abstract representation of an inline function, there should be
2324 * a concrete object that we'll handle
2326 if (dwarf2_find_attribute(di, DW_AT_inline, &inline_flags) &&
2327 inline_flags.gotten_from == attr_direct &&
2328 inline_flags.u.uvalue != DW_INL_not_inlined)
2330 TRACE("Function %s declared as inlined (%Id)... skipping\n",
2331 debugstr_a(name.u.string), inline_flags.u.uvalue);
2332 return NULL;
2335 if (dwarf2_find_attribute(di, DW_AT_declaration, &is_decl) &&
2336 is_decl.u.uvalue && is_decl.gotten_from == attr_direct)
2338 /* it's a real declaration, skip it */
2339 return NULL;
2341 if ((addr_ranges = dwarf2_get_ranges(di, &num_addr_ranges)) == NULL)
2343 WARN("cannot get range for %s\n", debugstr_a(name.u.string));
2344 return NULL;
2346 /* As functions (defined as inline assembly) get debug info with dwarf
2347 * (not the case for stabs), we just drop Wine's thunks here...
2348 * Actual thunks will be created in elf_module from the symbol table
2350 if (elf_is_in_thunk_area(di->unit_ctx->module_ctx->load_offset + addr_ranges[0].low, di->unit_ctx->module_ctx->thunks) >= 0)
2352 free(addr_ranges);
2353 return NULL;
2355 ret_type = dwarf2_lookup_type(di);
2357 /* FIXME: assuming C source code */
2358 sig_type = symt_new_function_signature(di->unit_ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2359 subpgm.top_func = symt_new_function(di->unit_ctx->module_ctx->module, di->unit_ctx->compiland,
2360 dwarf2_get_cpp_name(di, name.u.string),
2361 addr_ranges[0].low, addr_ranges[0].high - addr_ranges[0].low, &sig_type->symt);
2362 if (num_addr_ranges > 1)
2363 WARN("Function %s has multiple address ranges, only using the first one\n", name.u.string);
2364 free(addr_ranges);
2366 subpgm.current_func = subpgm.top_func;
2367 di->symt = &subpgm.top_func->symt;
2368 subpgm.ctx = di->unit_ctx;
2369 if (!dwarf2_compute_location_attr(di->unit_ctx, di, DW_AT_frame_base,
2370 &subpgm.frame, NULL))
2372 /* on stack !! */
2373 subpgm.frame.kind = loc_regrel;
2374 subpgm.frame.reg = di->unit_ctx->module_ctx->module->cpu->frame_regno;
2375 subpgm.frame.offset = 0;
2377 subpgm.non_computed_variable = FALSE;
2378 subpgm.current_block = NULL;
2380 children = dwarf2_get_di_children(di);
2381 if (children) for (i = 0; i < vector_length(children); i++)
2383 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2385 switch (child->abbrev->tag)
2387 case DW_TAG_variable:
2388 case DW_TAG_formal_parameter:
2389 dwarf2_parse_variable(&subpgm, child);
2390 break;
2391 case DW_TAG_lexical_block:
2392 dwarf2_parse_subprogram_block(&subpgm, child);
2393 break;
2394 case DW_TAG_inlined_subroutine:
2395 dwarf2_parse_inlined_subroutine(&subpgm, child);
2396 break;
2397 case DW_TAG_pointer_type:
2398 dwarf2_parse_pointer_type(child);
2399 break;
2400 case DW_TAG_const_type:
2401 dwarf2_parse_const_type(child);
2402 break;
2403 case DW_TAG_subprogram:
2404 /* FIXME: likely a declaration (to be checked)
2405 * skip it for now
2407 break;
2408 case DW_TAG_label:
2409 dwarf2_parse_subprogram_label(&subpgm, child);
2410 break;
2411 case DW_TAG_class_type:
2412 case DW_TAG_structure_type:
2413 case DW_TAG_union_type:
2414 case DW_TAG_enumeration_type:
2415 case DW_TAG_typedef:
2416 /* the type referred to will be loaded when we need it, so skip it */
2417 break;
2418 case DW_TAG_unspecified_parameters:
2419 case DW_TAG_template_type_param:
2420 case DW_TAG_template_value_param:
2421 case DW_TAG_GNU_call_site:
2422 case DW_TAG_GNU_template_parameter_pack:
2423 case DW_TAG_GNU_formal_parameter_pack:
2424 /* FIXME: no support in dbghelp's internals so far */
2425 break;
2426 default:
2427 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2428 child->abbrev->tag, dwarf2_debug_di(di));
2432 if (subpgm.non_computed_variable || subpgm.frame.kind >= loc_user)
2434 symt_add_function_point(di->unit_ctx->module_ctx->module, subpgm.top_func, SymTagCustom,
2435 &subpgm.frame, NULL);
2438 return di->symt;
2441 static struct symt* dwarf2_parse_subroutine_type(dwarf2_debug_info_t* di)
2443 struct symt* ret_type;
2444 struct symt_function_signature* sig_type;
2445 struct vector* children;
2446 dwarf2_debug_info_t* child;
2447 unsigned int i;
2449 if (di->symt) return di->symt;
2451 TRACE("%s\n", dwarf2_debug_di(di));
2453 ret_type = dwarf2_lookup_type(di);
2455 /* FIXME: assuming C source code */
2456 sig_type = symt_new_function_signature(di->unit_ctx->module_ctx->module, ret_type, CV_CALL_FAR_C);
2458 children = dwarf2_get_di_children(di);
2459 if (children) for (i = 0; i < vector_length(children); i++)
2461 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2463 switch (child->abbrev->tag)
2465 case DW_TAG_formal_parameter:
2466 symt_add_function_signature_parameter(di->unit_ctx->module_ctx->module, sig_type,
2467 dwarf2_lookup_type(child));
2468 break;
2469 case DW_TAG_unspecified_parameters:
2470 WARN("Unsupported unspecified parameters\n");
2471 break;
2475 return di->symt = &sig_type->symt;
2478 static void dwarf2_parse_namespace(dwarf2_debug_info_t* di)
2480 struct vector* children;
2481 dwarf2_debug_info_t* child;
2482 unsigned int i;
2484 if (di->symt) return;
2486 TRACE("%s\n", dwarf2_debug_di(di));
2488 di->symt = &symt_get_basic(btVoid, 0)->symt;
2490 children = dwarf2_get_di_children(di);
2491 if (children) for (i = 0; i < vector_length(children); i++)
2493 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2494 dwarf2_load_one_entry(child);
2498 static void dwarf2_parse_imported_unit(dwarf2_debug_info_t* di)
2500 struct attribute imp;
2502 if (di->symt) return;
2504 TRACE("%s\n", dwarf2_debug_di(di));
2506 if (dwarf2_find_attribute(di, DW_AT_import, &imp))
2508 dwarf2_debug_info_t* jmp = dwarf2_jump_to_debug_info(&imp);
2509 if (jmp) di->symt = jmp->symt;
2510 else FIXME("Couldn't load imported CU\n");
2512 else
2513 FIXME("Couldn't find import attribute\n");
2516 static void dwarf2_load_one_entry(dwarf2_debug_info_t* di)
2518 switch (di->abbrev->tag)
2520 case DW_TAG_typedef:
2521 dwarf2_parse_typedef(di);
2522 break;
2523 case DW_TAG_base_type:
2524 dwarf2_parse_base_type(di);
2525 break;
2526 case DW_TAG_pointer_type:
2527 dwarf2_parse_pointer_type(di);
2528 break;
2529 case DW_TAG_class_type:
2530 dwarf2_parse_udt_type(di, UdtClass);
2531 break;
2532 case DW_TAG_structure_type:
2533 dwarf2_parse_udt_type(di, UdtStruct);
2534 break;
2535 case DW_TAG_union_type:
2536 dwarf2_parse_udt_type(di, UdtUnion);
2537 break;
2538 case DW_TAG_array_type:
2539 dwarf2_parse_array_type(di);
2540 break;
2541 case DW_TAG_const_type:
2542 dwarf2_parse_const_type(di);
2543 break;
2544 case DW_TAG_volatile_type:
2545 dwarf2_parse_volatile_type(di);
2546 break;
2547 case DW_TAG_restrict_type:
2548 dwarf2_parse_restrict_type(di);
2549 break;
2550 case DW_TAG_unspecified_type:
2551 dwarf2_parse_unspecified_type(di);
2552 break;
2553 case DW_TAG_reference_type:
2554 case DW_TAG_rvalue_reference_type:
2555 dwarf2_parse_reference_type(di);
2556 break;
2557 case DW_TAG_enumeration_type:
2558 dwarf2_parse_enumeration_type(di);
2559 break;
2560 case DW_TAG_subprogram:
2561 dwarf2_parse_subprogram(di);
2562 break;
2563 case DW_TAG_subroutine_type:
2564 dwarf2_parse_subroutine_type(di);
2565 break;
2566 case DW_TAG_variable:
2568 dwarf2_subprogram_t subpgm;
2570 subpgm.ctx = di->unit_ctx;
2571 subpgm.top_func = subpgm.current_func = NULL;
2572 subpgm.current_block = NULL;
2573 subpgm.frame.kind = loc_absolute;
2574 subpgm.frame.offset = 0;
2575 subpgm.frame.reg = Wine_DW_no_register;
2576 dwarf2_parse_variable(&subpgm, di);
2578 break;
2579 case DW_TAG_namespace:
2580 dwarf2_parse_namespace(di);
2581 break;
2582 case DW_TAG_subrange_type:
2583 dwarf2_parse_subrange_type(di);
2584 break;
2585 case DW_TAG_imported_unit:
2586 dwarf2_parse_imported_unit(di);
2587 break;
2588 /* keep it silent until we need DW_OP_call_xxx support */
2589 case DW_TAG_dwarf_procedure:
2590 /* silence a couple of non-C language defines (mainly C++ but others too) */
2591 case DW_TAG_imported_module:
2592 case DW_TAG_imported_declaration:
2593 case DW_TAG_interface_type:
2594 case DW_TAG_module:
2595 case DW_TAG_ptr_to_member_type:
2596 break;
2597 default:
2598 FIXME("Unhandled Tag type 0x%Ix at %s\n",
2599 di->abbrev->tag, dwarf2_debug_di(di));
2603 static void dwarf2_set_line_number(struct module* module, ULONG_PTR address,
2604 const struct vector* v, unsigned file, unsigned line)
2606 struct symt_function* func;
2607 struct symt_function* inlined;
2608 struct symt_ht* symt;
2609 unsigned* psrc;
2611 if (!file || !(psrc = vector_at(v, file - 1))) return;
2613 TRACE("%s %Ix %s %u\n",
2614 debugstr_w(module->modulename), address, debugstr_a(source_get(module, *psrc)), line);
2615 symt = symt_find_symbol_at(module, address);
2616 if (symt_check_tag(&symt->symt, SymTagFunction))
2618 func = (struct symt_function*)symt;
2619 for (inlined = func->next_inlinesite; inlined; inlined = inlined->next_inlinesite)
2621 int i;
2622 for (i = 0; i < inlined->num_ranges; ++i)
2624 if (inlined->ranges[i].low <= address && address < inlined->ranges[i].high)
2626 symt_add_func_line(module, inlined, *psrc, line, address);
2627 return; /* only add to lowest matching inline site */
2631 symt_add_func_line(module, func, *psrc, line, address);
2635 static BOOL dwarf2_parse_line_numbers(dwarf2_parse_context_t* ctx,
2636 const char* compile_dir,
2637 ULONG_PTR offset)
2639 dwarf2_traverse_context_t traverse;
2640 ULONG_PTR length;
2641 unsigned insn_size, version, default_stmt;
2642 unsigned line_range, opcode_base;
2643 int line_base;
2644 unsigned char offset_size;
2645 const unsigned char* opcode_len;
2646 struct vector dirs;
2647 struct vector files;
2648 const char** p;
2650 /* section with line numbers stripped */
2651 if (ctx->module_ctx->sections[section_line].address == IMAGE_NO_MAP)
2652 return FALSE;
2654 if (offset + 4 > ctx->module_ctx->sections[section_line].size)
2656 WARN("out of bounds offset\n");
2657 return FALSE;
2659 traverse.data = ctx->module_ctx->sections[section_line].address + offset;
2660 traverse.end_data = ctx->module_ctx->sections[section_line].address + ctx->module_ctx->sections[section_line].size;
2662 length = dwarf2_parse_3264(&traverse, &offset_size);
2663 if (offset_size != ctx->head.offset_size)
2665 WARN("Mismatch in 32/64 bit format\n");
2666 return FALSE;
2668 traverse.end_data = traverse.data + length;
2670 if (traverse.end_data > ctx->module_ctx->sections[section_line].address + ctx->module_ctx->sections[section_line].size)
2672 WARN("out of bounds header\n");
2673 return FALSE;
2675 version = dwarf2_parse_u2(&traverse);
2676 dwarf2_parse_offset(&traverse, offset_size); /* header_len */
2677 insn_size = dwarf2_parse_byte(&traverse);
2678 if (version >= 4)
2679 dwarf2_parse_byte(&traverse); /* max_operations_per_instructions */
2680 default_stmt = dwarf2_parse_byte(&traverse);
2681 line_base = (signed char)dwarf2_parse_byte(&traverse);
2682 line_range = dwarf2_parse_byte(&traverse);
2683 opcode_base = dwarf2_parse_byte(&traverse);
2685 opcode_len = traverse.data;
2686 traverse.data += opcode_base - 1;
2688 vector_init(&dirs, sizeof(const char*), 4);
2689 p = vector_add(&dirs, &ctx->pool);
2690 *p = compile_dir ? compile_dir : ".";
2691 while (traverse.data < traverse.end_data && *traverse.data)
2693 const char* rel = (const char*)traverse.data;
2694 unsigned rellen = strlen(rel);
2695 TRACE("Got include %s\n", debugstr_a(rel));
2696 traverse.data += rellen + 1;
2697 p = vector_add(&dirs, &ctx->pool);
2699 if (*rel == '/' || !compile_dir || !*compile_dir)
2700 *p = rel;
2701 else
2703 /* include directory relative to compile directory */
2704 unsigned baselen = strlen(compile_dir);
2705 char* tmp = pool_alloc(&ctx->pool, baselen + 1 + rellen + 1);
2706 strcpy(tmp, compile_dir);
2707 if (baselen && tmp[baselen - 1] != '/') tmp[baselen++] = '/';
2708 strcpy(&tmp[baselen], rel);
2709 *p = tmp;
2713 traverse.data++;
2715 vector_init(&files, sizeof(unsigned), 16);
2716 while (traverse.data < traverse.end_data && *traverse.data)
2718 unsigned int dir_index, mod_time;
2719 const char* name;
2720 const char* dir;
2721 unsigned* psrc;
2723 name = (const char*)traverse.data;
2724 traverse.data += strlen(name) + 1;
2725 dir_index = dwarf2_leb128_as_unsigned(&traverse);
2726 mod_time = dwarf2_leb128_as_unsigned(&traverse);
2727 length = dwarf2_leb128_as_unsigned(&traverse);
2728 dir = *(const char**)vector_at(&dirs, dir_index);
2729 TRACE("Got file %s/%s (%u,%Iu)\n", debugstr_a(dir), debugstr_a(name), mod_time, length);
2730 psrc = vector_add(&files, &ctx->pool);
2731 *psrc = source_new(ctx->module_ctx->module, dir, name);
2733 traverse.data++;
2735 while (traverse.data < traverse.end_data && *traverse.data)
2737 ULONG_PTR address = 0;
2738 unsigned file = 1;
2739 unsigned line = 1;
2740 unsigned is_stmt = default_stmt;
2741 BOOL end_sequence = FALSE;
2742 unsigned opcode, extopcode, i;
2744 while (!end_sequence)
2746 opcode = dwarf2_parse_byte(&traverse);
2747 TRACE("Got opcode %x\n", opcode);
2749 if (opcode >= opcode_base)
2751 unsigned delta = opcode - opcode_base;
2753 address += (delta / line_range) * insn_size;
2754 line += line_base + (delta % line_range);
2755 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2757 else
2759 switch (opcode)
2761 case DW_LNS_copy:
2762 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2763 break;
2764 case DW_LNS_advance_pc:
2765 address += insn_size * dwarf2_leb128_as_unsigned(&traverse);
2766 break;
2767 case DW_LNS_advance_line:
2768 line += dwarf2_leb128_as_signed(&traverse);
2769 break;
2770 case DW_LNS_set_file:
2771 file = dwarf2_leb128_as_unsigned(&traverse);
2772 break;
2773 case DW_LNS_set_column:
2774 dwarf2_leb128_as_unsigned(&traverse);
2775 break;
2776 case DW_LNS_negate_stmt:
2777 is_stmt = !is_stmt;
2778 break;
2779 case DW_LNS_set_basic_block:
2780 break;
2781 case DW_LNS_const_add_pc:
2782 address += ((255 - opcode_base) / line_range) * insn_size;
2783 break;
2784 case DW_LNS_fixed_advance_pc:
2785 address += dwarf2_parse_u2(&traverse);
2786 break;
2787 case DW_LNS_extended_op:
2788 dwarf2_leb128_as_unsigned(&traverse);
2789 extopcode = dwarf2_parse_byte(&traverse);
2790 switch (extopcode)
2792 case DW_LNE_end_sequence:
2793 dwarf2_set_line_number(ctx->module_ctx->module, address, &files, file, line);
2794 end_sequence = TRUE;
2795 break;
2796 case DW_LNE_set_address:
2797 address = ctx->module_ctx->load_offset + dwarf2_parse_addr_head(&traverse, &ctx->head);
2798 break;
2799 case DW_LNE_define_file:
2800 FIXME("not handled define file %s\n", debugstr_a((char *)traverse.data));
2801 traverse.data += strlen((const char *)traverse.data) + 1;
2802 dwarf2_leb128_as_unsigned(&traverse);
2803 dwarf2_leb128_as_unsigned(&traverse);
2804 dwarf2_leb128_as_unsigned(&traverse);
2805 break;
2806 case DW_LNE_set_discriminator:
2808 unsigned descr;
2810 descr = dwarf2_leb128_as_unsigned(&traverse);
2811 WARN("not handled discriminator %x\n", descr);
2813 break;
2814 default:
2815 FIXME("Unsupported extended opcode %x\n", extopcode);
2816 break;
2818 break;
2819 default:
2820 WARN("Unsupported opcode %x\n", opcode);
2821 for (i = 0; i < opcode_len[opcode]; i++)
2822 dwarf2_leb128_as_unsigned(&traverse);
2823 break;
2828 return TRUE;
2831 unsigned dwarf2_cache_cuhead(struct dwarf2_module_info_s* module, struct symt_compiland* c, const dwarf2_cuhead_t* head)
2833 dwarf2_cuhead_t* ah;
2834 unsigned i;
2835 for (i = 0; i < module->num_cuheads; ++i)
2837 if (memcmp(module->cuheads[i], head, sizeof(*head)) == 0)
2839 c->user = module->cuheads[i];
2840 return TRUE;
2843 if (!(ah = pool_alloc(&c->container->module->pool, sizeof(*head)))) return FALSE;
2844 memcpy(ah, head, sizeof(*head));
2845 module->cuheads = realloc(module->cuheads, ++module->num_cuheads * sizeof(head));
2846 module->cuheads[module->num_cuheads - 1] = ah;
2847 c->user = ah;
2848 return TRUE;
2851 static dwarf2_parse_context_t* dwarf2_locate_cu(dwarf2_parse_module_context_t* module_ctx, ULONG_PTR ref)
2853 unsigned i;
2854 dwarf2_parse_context_t* ctx;
2855 const BYTE* where;
2856 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
2858 ctx = vector_at(&module_ctx->unit_contexts, i);
2859 where = module_ctx->sections[ctx->section].address + ref;
2860 if (where >= ctx->traverse_DIE.data && where < ctx->traverse_DIE.end_data)
2861 return ctx;
2863 FIXME("Couldn't find ref 0x%Ix inside sect\n", ref);
2864 return NULL;
2867 static BOOL dwarf2_parse_compilation_unit_head(dwarf2_parse_context_t* ctx,
2868 dwarf2_traverse_context_t* mod_ctx)
2870 dwarf2_traverse_context_t abbrev_ctx;
2871 const unsigned char* comp_unit_start = mod_ctx->data;
2872 ULONG_PTR cu_length;
2873 ULONG_PTR cu_abbrev_offset;
2874 /* FIXME this is a temporary configuration while adding support for dwarf3&4 bits */
2875 static LONG max_supported_dwarf_version = 0;
2877 cu_length = dwarf2_parse_3264(mod_ctx, &ctx->head.offset_size);
2879 ctx->traverse_DIE.data = mod_ctx->data;
2880 ctx->traverse_DIE.end_data = mod_ctx->data + cu_length;
2881 mod_ctx->data += cu_length;
2882 ctx->head.version = dwarf2_parse_u2(&ctx->traverse_DIE);
2883 cu_abbrev_offset = dwarf2_parse_offset(&ctx->traverse_DIE, ctx->head.offset_size);
2884 ctx->head.word_size = dwarf2_parse_byte(&ctx->traverse_DIE);
2885 ctx->status = UNIT_ERROR;
2887 TRACE("Compilation Unit Header found at 0x%x:\n",
2888 (int)(comp_unit_start - ctx->module_ctx->sections[section_debug].address));
2889 TRACE("- length: %Iu\n", cu_length);
2890 TRACE("- version: %u\n", ctx->head.version);
2891 TRACE("- abbrev_offset: %Iu\n", cu_abbrev_offset);
2892 TRACE("- word_size: %u\n", ctx->head.word_size);
2893 TRACE("- offset_size: %u\n", ctx->head.offset_size);
2895 if (ctx->head.version >= 2)
2896 ctx->module_ctx->cu_versions |= 1 << (ctx->head.version - 2);
2897 if (max_supported_dwarf_version == 0)
2899 char* env = getenv("DBGHELP_DWARF_VERSION");
2900 LONG v = env ? atol(env) : 4;
2901 max_supported_dwarf_version = (v >= 2 && v <= 4) ? v : 4;
2904 if (ctx->head.version < 2 || ctx->head.version > max_supported_dwarf_version)
2906 WARN("DWARF version %d isn't supported. Wine dbghelp only supports DWARF 2 up to %lu.\n",
2907 ctx->head.version, max_supported_dwarf_version);
2908 return FALSE;
2911 pool_init(&ctx->pool, 65536);
2912 ctx->section = section_debug;
2913 ctx->ref_offset = comp_unit_start - ctx->module_ctx->sections[section_debug].address;
2914 ctx->cpp_name = NULL;
2915 ctx->status = UNIT_NOTLOADED;
2917 abbrev_ctx.data = ctx->module_ctx->sections[section_abbrev].address + cu_abbrev_offset;
2918 abbrev_ctx.end_data = ctx->module_ctx->sections[section_abbrev].address + ctx->module_ctx->sections[section_abbrev].size;
2919 dwarf2_parse_abbrev_set(&abbrev_ctx, &ctx->abbrev_table, &ctx->pool);
2921 sparse_array_init(&ctx->debug_info_table, sizeof(dwarf2_debug_info_t), 128);
2922 return TRUE;
2925 static BOOL dwarf2_parse_compilation_unit(dwarf2_parse_context_t* ctx)
2927 dwarf2_debug_info_t* di;
2928 dwarf2_traverse_context_t cu_ctx = ctx->traverse_DIE;
2929 BOOL ret = FALSE;
2931 switch (ctx->status)
2933 case UNIT_ERROR: return FALSE;
2934 case UNIT_BEINGLOADED:
2935 FIXME("Circular deps on CU\n");
2936 /* fall through */
2937 case UNIT_LOADED:
2938 case UNIT_LOADED_FAIL:
2939 return TRUE;
2940 case UNIT_NOTLOADED: break;
2943 ctx->status = UNIT_BEINGLOADED;
2944 if (dwarf2_read_one_debug_info(ctx, &cu_ctx, NULL, &di))
2946 if (di->abbrev->tag == DW_TAG_compile_unit || di->abbrev->tag == DW_TAG_partial_unit)
2948 struct attribute name;
2949 struct vector* children;
2950 dwarf2_debug_info_t* child = NULL;
2951 unsigned int i;
2952 struct attribute stmt_list, low_pc;
2953 struct attribute comp_dir;
2954 struct attribute language;
2956 if (!dwarf2_find_attribute(di, DW_AT_name, &name))
2957 name.u.string = NULL;
2959 /* get working directory of current compilation unit */
2960 if (!dwarf2_find_attribute(di, DW_AT_comp_dir, &comp_dir))
2961 comp_dir.u.string = NULL;
2963 if (!dwarf2_find_attribute(di, DW_AT_low_pc, &low_pc))
2964 low_pc.u.uvalue = 0;
2966 if (!dwarf2_find_attribute(di, DW_AT_language, &language))
2967 language.u.uvalue = DW_LANG_C;
2969 ctx->language = language.u.uvalue;
2971 ctx->compiland = symt_new_compiland(ctx->module_ctx->module,
2972 source_new(ctx->module_ctx->module, comp_dir.u.string, name.u.string));
2973 ctx->compiland->address = ctx->module_ctx->load_offset + low_pc.u.uvalue;
2974 dwarf2_cache_cuhead(ctx->module_ctx->module->format_info[DFI_DWARF]->u.dwarf2_info, ctx->compiland, &ctx->head);
2975 di->symt = &ctx->compiland->symt;
2976 children = dwarf2_get_di_children(di);
2977 if (children) for (i = 0; i < vector_length(children); i++)
2979 child = *(dwarf2_debug_info_t**)vector_at(children, i);
2980 dwarf2_load_one_entry(child);
2982 if (dwarf2_find_attribute(di, DW_AT_stmt_list, &stmt_list))
2984 if (dwarf2_parse_line_numbers(ctx, comp_dir.u.string, stmt_list.u.uvalue))
2985 ctx->module_ctx->module->module.LineNumbers = TRUE;
2987 ctx->status = UNIT_LOADED;
2988 ret = TRUE;
2990 else FIXME("Should have a compilation unit here %Iu\n", di->abbrev->tag);
2992 if (ctx->status == UNIT_BEINGLOADED) ctx->status = UNIT_LOADED_FAIL;
2993 return ret;
2996 static BOOL dwarf2_lookup_loclist(const struct module_format* modfmt, const dwarf2_cuhead_t* head,
2997 const BYTE* start, ULONG_PTR ip, dwarf2_traverse_context_t* lctx)
2999 DWORD_PTR beg, end;
3000 const BYTE* ptr = start;
3001 DWORD len;
3003 while (ptr < modfmt->u.dwarf2_info->debug_loc.address + modfmt->u.dwarf2_info->debug_loc.size)
3005 beg = dwarf2_get_addr(ptr, head->word_size); ptr += head->word_size;
3006 end = dwarf2_get_addr(ptr, head->word_size); ptr += head->word_size;
3007 if (!beg && !end) break;
3008 len = dwarf2_get_u2(ptr); ptr += 2;
3010 if (beg <= ip && ip < end)
3012 lctx->data = ptr;
3013 lctx->end_data = ptr + len;
3014 return TRUE;
3016 ptr += len;
3018 WARN("Couldn't find ip in location list\n");
3019 return FALSE;
3022 static const dwarf2_cuhead_t* get_cuhead_from_func(const struct symt_function* func)
3024 if (symt_check_tag(&func->symt, SymTagInlineSite))
3025 func = symt_get_function_from_inlined((struct symt_function*)func);
3026 if (symt_check_tag(&func->symt, SymTagFunction) && symt_check_tag(func->container, SymTagCompiland))
3028 struct symt_compiland* c = (struct symt_compiland*)func->container;
3029 return (const dwarf2_cuhead_t*)c->user;
3031 FIXME("Should have a compilation unit head\n");
3032 return NULL;
3035 static enum location_error compute_call_frame_cfa(struct module* module, ULONG_PTR ip, struct location* frame);
3037 static enum location_error loc_compute_frame(struct process* pcs,
3038 const struct module_format* modfmt,
3039 const struct symt_function* func,
3040 DWORD_PTR ip, const dwarf2_cuhead_t* head,
3041 struct location* frame)
3043 struct symt** psym = NULL;
3044 struct location* pframe;
3045 dwarf2_traverse_context_t lctx;
3046 enum location_error err;
3047 unsigned int i;
3049 for (i=0; i<vector_length(&func->vchildren); i++)
3051 psym = vector_at(&func->vchildren, i);
3052 if (psym && symt_check_tag(*psym, SymTagCustom))
3054 pframe = &((struct symt_hierarchy_point*)*psym)->loc;
3056 /* First, recompute the frame information, if needed */
3057 switch (pframe->kind)
3059 case loc_regrel:
3060 case loc_register:
3061 *frame = *pframe;
3062 break;
3063 case loc_dwarf2_location_list:
3064 WARN("Searching loclist for %s\n", debugstr_a(func->hash_elt.name));
3065 if (!dwarf2_lookup_loclist(modfmt, head,
3066 modfmt->u.dwarf2_info->debug_loc.address + pframe->offset,
3067 ip, &lctx))
3068 return loc_err_out_of_scope;
3069 if ((err = compute_location(modfmt->module, head,
3070 &lctx, frame, pcs->handle, NULL)) < 0) return err;
3071 if (frame->kind >= loc_user)
3073 WARN("Couldn't compute runtime frame location\n");
3074 return loc_err_too_complex;
3076 break;
3077 case loc_dwarf2_frame_cfa:
3078 err = compute_call_frame_cfa(modfmt->module, ip + ((struct symt_compiland*)func->container)->address, frame);
3079 if (err < 0) return err;
3080 break;
3081 default:
3082 WARN("Unsupported frame kind %d\n", pframe->kind);
3083 return loc_err_internal;
3085 return 0;
3088 WARN("Couldn't find Custom function point, whilst location list offset is searched\n");
3089 return loc_err_internal;
3092 enum reg_rule
3094 RULE_UNSET, /* not set at all */
3095 RULE_UNDEFINED, /* undefined value */
3096 RULE_SAME, /* same value as previous frame */
3097 RULE_CFA_OFFSET, /* stored at cfa offset */
3098 RULE_OTHER_REG, /* stored in other register */
3099 RULE_EXPRESSION, /* address specified by expression */
3100 RULE_VAL_EXPRESSION /* value specified by expression */
3103 /* make it large enough for all CPUs */
3104 #define NB_FRAME_REGS 64
3105 #define MAX_SAVED_STATES 16
3107 struct frame_state
3109 ULONG_PTR cfa_offset;
3110 unsigned char cfa_reg;
3111 enum reg_rule cfa_rule;
3112 enum reg_rule rules[NB_FRAME_REGS];
3113 ULONG_PTR regs[NB_FRAME_REGS];
3116 struct frame_info
3118 ULONG_PTR ip;
3119 ULONG_PTR code_align;
3120 LONG_PTR data_align;
3121 unsigned char retaddr_reg;
3122 unsigned char fde_encoding;
3123 unsigned char lsda_encoding;
3124 unsigned char signal_frame;
3125 unsigned char aug_z_format;
3126 unsigned char state_sp;
3127 struct frame_state state;
3128 struct frame_state state_stack[MAX_SAVED_STATES];
3131 static ULONG_PTR dwarf2_parse_augmentation_ptr(dwarf2_traverse_context_t* ctx, unsigned char encoding, unsigned char word_size)
3133 ULONG_PTR base;
3135 if (encoding == DW_EH_PE_omit) return 0;
3137 switch (encoding & 0xf0)
3139 case DW_EH_PE_abs:
3140 base = 0;
3141 break;
3142 case DW_EH_PE_pcrel:
3143 base = (ULONG_PTR)ctx->data;
3144 break;
3145 default:
3146 FIXME("unsupported encoding %02x\n", encoding);
3147 return 0;
3150 switch (encoding & 0x0f)
3152 case DW_EH_PE_native:
3153 return base + dwarf2_parse_addr(ctx, word_size);
3154 case DW_EH_PE_leb128:
3155 return base + dwarf2_leb128_as_unsigned(ctx);
3156 case DW_EH_PE_data2:
3157 return base + dwarf2_parse_u2(ctx);
3158 case DW_EH_PE_data4:
3159 return base + dwarf2_parse_u4(ctx);
3160 case DW_EH_PE_data8:
3161 return base + dwarf2_parse_u8(ctx);
3162 case DW_EH_PE_signed|DW_EH_PE_leb128:
3163 return base + dwarf2_leb128_as_signed(ctx);
3164 case DW_EH_PE_signed|DW_EH_PE_data2:
3165 return base + (signed short)dwarf2_parse_u2(ctx);
3166 case DW_EH_PE_signed|DW_EH_PE_data4:
3167 return base + (signed int)dwarf2_parse_u4(ctx);
3168 case DW_EH_PE_signed|DW_EH_PE_data8:
3169 return base + (LONG64)dwarf2_parse_u8(ctx);
3170 default:
3171 FIXME("unsupported encoding %02x\n", encoding);
3172 return 0;
3176 static BOOL parse_cie_details(dwarf2_traverse_context_t* ctx, struct frame_info* info, unsigned char word_size)
3178 unsigned char version;
3179 const char* augmentation;
3180 const unsigned char* end;
3181 ULONG_PTR len;
3183 memset(info, 0, sizeof(*info));
3184 info->lsda_encoding = DW_EH_PE_omit;
3185 info->aug_z_format = 0;
3187 /* parse the CIE first */
3188 version = dwarf2_parse_byte(ctx);
3189 if (version != 1 && version != 3 && version != 4)
3191 FIXME("unknown CIE version %u at %p\n", version, ctx->data - 1);
3192 return FALSE;
3194 augmentation = (const char*)ctx->data;
3195 ctx->data += strlen(augmentation) + 1;
3197 switch (version)
3199 case 4:
3200 /* skip 'address_size' and 'segment_size' */
3201 ctx->data += 2;
3202 /* fallthrough */
3203 case 1:
3204 case 3:
3205 info->code_align = dwarf2_leb128_as_unsigned(ctx);
3206 info->data_align = dwarf2_leb128_as_signed(ctx);
3207 info->retaddr_reg = version == 1 ? dwarf2_parse_byte(ctx) :dwarf2_leb128_as_unsigned(ctx);
3208 break;
3209 default:
3212 info->state.cfa_rule = RULE_CFA_OFFSET;
3214 end = NULL;
3215 TRACE("\tparsing augmentation %s\n", debugstr_a(augmentation));
3216 if (*augmentation) do
3218 switch (*augmentation)
3220 case 'z':
3221 len = dwarf2_leb128_as_unsigned(ctx);
3222 end = ctx->data + len;
3223 info->aug_z_format = 1;
3224 continue;
3225 case 'L':
3226 info->lsda_encoding = dwarf2_parse_byte(ctx);
3227 continue;
3228 case 'P':
3230 unsigned char encoding = dwarf2_parse_byte(ctx);
3231 /* throw away the indirect bit, as we don't care for the result */
3232 encoding &= ~DW_EH_PE_indirect;
3233 dwarf2_parse_augmentation_ptr(ctx, encoding, word_size); /* handler */
3234 continue;
3236 case 'R':
3237 info->fde_encoding = dwarf2_parse_byte(ctx);
3238 continue;
3239 case 'S':
3240 info->signal_frame = 1;
3241 continue;
3243 FIXME("unknown augmentation '%c'\n", *augmentation);
3244 if (!end) return FALSE;
3245 break;
3246 } while (*++augmentation);
3247 if (end) ctx->data = end;
3248 return TRUE;
3251 static BOOL dwarf2_get_cie(ULONG_PTR addr, struct module* module, DWORD_PTR delta,
3252 dwarf2_traverse_context_t* fde_ctx, dwarf2_traverse_context_t* cie_ctx,
3253 struct frame_info* info, BOOL in_eh_frame)
3255 const unsigned char* ptr_blk;
3256 const unsigned char* cie_ptr;
3257 const unsigned char* last_cie_ptr = (const unsigned char*)~0;
3258 ULONG_PTR len, id;
3259 ULONG_PTR start, range;
3260 ULONG_PTR cie_id;
3261 const BYTE* start_data = fde_ctx->data;
3262 unsigned char word_size = module->format_info[DFI_DWARF]->u.dwarf2_info->word_size;
3263 unsigned char offset_size;
3265 /* skip 0-padding at beginning of section (alignment) */
3266 while (fde_ctx->data + 2 * 4 < fde_ctx->end_data)
3268 if (dwarf2_parse_u4(fde_ctx))
3270 fde_ctx->data -= 4;
3271 break;
3274 for (; fde_ctx->data + 2 * 4 < fde_ctx->end_data; fde_ctx->data = ptr_blk)
3276 const unsigned char* st = fde_ctx->data;
3277 /* find the FDE for address addr (skip CIE) */
3278 len = dwarf2_parse_3264(fde_ctx, &offset_size);
3279 cie_id = in_eh_frame ? 0 : (offset_size == 4 ? DW_CIE_ID : (ULONG_PTR)DW64_CIE_ID);
3280 ptr_blk = fde_ctx->data + len;
3281 id = dwarf2_parse_offset(fde_ctx, offset_size);
3282 if (id == cie_id)
3284 last_cie_ptr = st;
3285 /* we need some bits out of the CIE in order to parse all contents */
3286 if (!parse_cie_details(fde_ctx, info, word_size)) return FALSE;
3287 cie_ctx->data = fde_ctx->data;
3288 cie_ctx->end_data = ptr_blk;
3289 continue;
3291 cie_ptr = (in_eh_frame) ? fde_ctx->data - id - 4 : start_data + id;
3292 if (cie_ptr != last_cie_ptr)
3294 last_cie_ptr = cie_ptr;
3295 cie_ctx->data = cie_ptr;
3296 cie_ctx->end_data = cie_ptr + (offset_size == 4 ? 4 : 4 + 8);
3297 cie_ctx->end_data += dwarf2_parse_3264(cie_ctx, &offset_size);
3299 if (dwarf2_parse_offset(cie_ctx, in_eh_frame ? word_size : offset_size) != cie_id)
3301 FIXME("wrong CIE pointer at %x from FDE %x\n",
3302 (unsigned)(cie_ptr - start_data),
3303 (unsigned)(fde_ctx->data - start_data));
3304 return FALSE;
3306 if (!parse_cie_details(cie_ctx, info, word_size)) return FALSE;
3308 start = delta + dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding, word_size);
3309 range = dwarf2_parse_augmentation_ptr(fde_ctx, info->fde_encoding & 0x0F, word_size);
3311 if (addr >= start && addr < start + range)
3313 /* reset the FDE context */
3314 fde_ctx->end_data = ptr_blk;
3316 info->ip = start;
3317 return TRUE;
3320 return FALSE;
3323 static int valid_reg(ULONG_PTR reg)
3325 if (reg >= NB_FRAME_REGS) FIXME("unsupported reg %Ix\n", reg);
3326 return (reg < NB_FRAME_REGS);
3329 static void execute_cfa_instructions(struct module* module, dwarf2_traverse_context_t* ctx,
3330 ULONG_PTR last_ip, struct frame_info *info)
3332 while (ctx->data < ctx->end_data && info->ip <= last_ip + info->signal_frame)
3334 enum dwarf_call_frame_info op = dwarf2_parse_byte(ctx);
3336 if (op & 0xc0)
3338 switch (op & 0xc0)
3340 case DW_CFA_advance_loc:
3342 ULONG_PTR offset = (op & 0x3f) * info->code_align;
3343 TRACE("%Ix: DW_CFA_advance_loc %Iu\n", info->ip, offset);
3344 info->ip += offset;
3345 break;
3347 case DW_CFA_offset:
3349 ULONG_PTR reg = op & 0x3f;
3350 LONG_PTR offset = dwarf2_leb128_as_unsigned(ctx) * info->data_align;
3351 if (!valid_reg(reg)) break;
3352 TRACE("%Ix: DW_CFA_offset %s, %Id\n",
3353 info->ip,
3354 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3355 offset);
3356 info->state.regs[reg] = offset;
3357 info->state.rules[reg] = RULE_CFA_OFFSET;
3358 break;
3360 case DW_CFA_restore:
3362 ULONG_PTR reg = op & 0x3f;
3363 if (!valid_reg(reg)) break;
3364 TRACE("%Ix: DW_CFA_restore %s\n",
3365 info->ip,
3366 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3367 info->state.rules[reg] = RULE_UNSET;
3368 break;
3372 else switch (op)
3374 case DW_CFA_nop:
3375 break;
3376 case DW_CFA_set_loc:
3378 ULONG_PTR loc = dwarf2_parse_augmentation_ptr(ctx, info->fde_encoding,
3379 module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
3380 TRACE("%Ix: DW_CFA_set_loc %Ix\n", info->ip, loc);
3381 info->ip = loc;
3382 break;
3384 case DW_CFA_advance_loc1:
3386 ULONG_PTR offset = dwarf2_parse_byte(ctx) * info->code_align;
3387 TRACE("%Ix: DW_CFA_advance_loc1 %Iu\n", info->ip, offset);
3388 info->ip += offset;
3389 break;
3391 case DW_CFA_advance_loc2:
3393 ULONG_PTR offset = dwarf2_parse_u2(ctx) * info->code_align;
3394 TRACE("%Ix: DW_CFA_advance_loc2 %Iu\n", info->ip, offset);
3395 info->ip += offset;
3396 break;
3398 case DW_CFA_advance_loc4:
3400 ULONG_PTR offset = dwarf2_parse_u4(ctx) * info->code_align;
3401 TRACE("%Ix: DW_CFA_advance_loc4 %Iu\n", info->ip, offset);
3402 info->ip += offset;
3403 break;
3405 case DW_CFA_offset_extended:
3406 case DW_CFA_offset_extended_sf:
3408 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3409 LONG_PTR offset = (op == DW_CFA_offset_extended) ? dwarf2_leb128_as_unsigned(ctx) * info->data_align
3410 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3411 if (!valid_reg(reg)) break;
3412 TRACE("%Ix: DW_CFA_offset_extended %s, %Id\n",
3413 info->ip,
3414 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3415 offset);
3416 info->state.regs[reg] = offset;
3417 info->state.rules[reg] = RULE_CFA_OFFSET;
3418 break;
3420 case DW_CFA_restore_extended:
3422 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3423 if (!valid_reg(reg)) break;
3424 TRACE("%Ix: DW_CFA_restore_extended %s\n",
3425 info->ip,
3426 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3427 info->state.rules[reg] = RULE_UNSET;
3428 break;
3430 case DW_CFA_undefined:
3432 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3433 if (!valid_reg(reg)) break;
3434 TRACE("%Ix: DW_CFA_undefined %s\n",
3435 info->ip,
3436 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3437 info->state.rules[reg] = RULE_UNDEFINED;
3438 break;
3440 case DW_CFA_same_value:
3442 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3443 if (!valid_reg(reg)) break;
3444 TRACE("%Ix: DW_CFA_same_value %s\n",
3445 info->ip,
3446 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3447 info->state.regs[reg] = reg;
3448 info->state.rules[reg] = RULE_SAME;
3449 break;
3451 case DW_CFA_register:
3453 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3454 ULONG_PTR reg2 = dwarf2_leb128_as_unsigned(ctx);
3455 if (!valid_reg(reg) || !valid_reg(reg2)) break;
3456 TRACE("%Ix: DW_CFA_register %s == %s\n",
3457 info->ip,
3458 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3459 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg2, module, TRUE)));
3460 info->state.regs[reg] = reg2;
3461 info->state.rules[reg] = RULE_OTHER_REG;
3462 break;
3464 case DW_CFA_remember_state:
3465 TRACE("%Ix: DW_CFA_remember_state\n", info->ip);
3466 if (info->state_sp >= MAX_SAVED_STATES)
3467 FIXME("%Ix: DW_CFA_remember_state too many nested saves\n", info->ip);
3468 else
3469 info->state_stack[info->state_sp++] = info->state;
3470 break;
3471 case DW_CFA_restore_state:
3472 TRACE("%Ix: DW_CFA_restore_state\n", info->ip);
3473 if (!info->state_sp)
3474 FIXME("%Ix: DW_CFA_restore_state without corresponding save\n", info->ip);
3475 else
3476 info->state = info->state_stack[--info->state_sp];
3477 break;
3478 case DW_CFA_def_cfa:
3479 case DW_CFA_def_cfa_sf:
3481 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3482 ULONG_PTR offset = (op == DW_CFA_def_cfa) ? dwarf2_leb128_as_unsigned(ctx)
3483 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3484 if (!valid_reg(reg)) break;
3485 TRACE("%Ix: DW_CFA_def_cfa %s, %Id\n",
3486 info->ip,
3487 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3488 offset);
3489 info->state.cfa_reg = reg;
3490 info->state.cfa_offset = offset;
3491 info->state.cfa_rule = RULE_CFA_OFFSET;
3492 break;
3494 case DW_CFA_def_cfa_register:
3496 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3497 if (!valid_reg(reg)) break;
3498 TRACE("%Ix: DW_CFA_def_cfa_register %s\n",
3499 info->ip,
3500 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)));
3501 info->state.cfa_reg = reg;
3502 info->state.cfa_rule = RULE_CFA_OFFSET;
3503 break;
3505 case DW_CFA_def_cfa_offset:
3506 case DW_CFA_def_cfa_offset_sf:
3508 ULONG_PTR offset = (op == DW_CFA_def_cfa_offset) ? dwarf2_leb128_as_unsigned(ctx)
3509 : dwarf2_leb128_as_signed(ctx) * info->data_align;
3510 TRACE("%Ix: DW_CFA_def_cfa_offset %Id\n", info->ip, offset);
3511 info->state.cfa_offset = offset;
3512 info->state.cfa_rule = RULE_CFA_OFFSET;
3513 break;
3515 case DW_CFA_def_cfa_expression:
3517 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3518 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3519 TRACE("%Ix: DW_CFA_def_cfa_expression %Ix-%Ix\n", info->ip, expr, expr+len);
3520 info->state.cfa_offset = expr;
3521 info->state.cfa_rule = RULE_VAL_EXPRESSION;
3522 ctx->data += len;
3523 break;
3525 case DW_CFA_expression:
3526 case DW_CFA_val_expression:
3528 ULONG_PTR reg = dwarf2_leb128_as_unsigned(ctx);
3529 ULONG_PTR expr = (ULONG_PTR)ctx->data;
3530 ULONG_PTR len = dwarf2_leb128_as_unsigned(ctx);
3531 if (!valid_reg(reg)) break;
3532 TRACE("%Ix: DW_CFA_%sexpression %s %Ix-%Ix\n",
3533 info->ip, (op == DW_CFA_expression) ? "" : "val_",
3534 module->cpu->fetch_regname(module->cpu->map_dwarf_register(reg, module, TRUE)),
3535 expr, expr + len);
3536 info->state.regs[reg] = expr;
3537 info->state.rules[reg] = (op == DW_CFA_expression) ? RULE_EXPRESSION : RULE_VAL_EXPRESSION;
3538 ctx->data += len;
3539 break;
3541 case DW_CFA_GNU_args_size:
3542 /* FIXME: should check that GCC is the compiler for this CU */
3544 ULONG_PTR args = dwarf2_leb128_as_unsigned(ctx);
3545 TRACE("%Ix: DW_CFA_GNU_args_size %Iu\n", info->ip, args);
3546 /* ignored */
3547 break;
3549 default:
3550 FIXME("%Ix: unknown CFA opcode %02x\n", info->ip, op);
3551 break;
3556 /* retrieve a context register from its dwarf number */
3557 static DWORD64 get_context_reg(const struct module* module, struct cpu_stack_walk *csw, union ctx *context,
3558 ULONG_PTR dw_reg)
3560 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3561 void* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3563 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3564 if (sz == 8)
3565 return *(DWORD64 *)ptr;
3566 else if (sz == 4)
3567 return *(DWORD *)ptr;
3569 FIXME("unhandled size %d\n", sz);
3570 return 0;
3573 /* set a context register from its dwarf number */
3574 static void set_context_reg(const struct module* module, struct cpu_stack_walk* csw, union ctx *context,
3575 ULONG_PTR dw_reg, ULONG_PTR val, BOOL isdebuggee)
3577 unsigned regno = csw->cpu->map_dwarf_register(dw_reg, module, TRUE), sz;
3578 ULONG_PTR* ptr = csw->cpu->fetch_context_reg(context, regno, &sz);
3580 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3581 if (isdebuggee)
3583 char tmp[16];
3585 if (sz > sizeof(tmp))
3587 FIXME("register %Iu/%u size is too wide: %u\n", dw_reg, regno, sz);
3588 return;
3590 if (!sw_read_mem(csw, val, tmp, sz))
3592 WARN("Couldn't read memory at %p\n", (void*)val);
3593 return;
3595 memcpy(ptr, tmp, sz);
3597 else
3599 if (sz != sizeof(ULONG_PTR))
3601 FIXME("assigning to register %Iu/%u of wrong size %u\n", dw_reg, regno, sz);
3602 return;
3604 *ptr = val;
3608 /* copy a register from one context to another using dwarf number */
3609 static void copy_context_reg(const struct module* module, struct cpu_stack_walk *csw,
3610 union ctx *dstcontext, ULONG_PTR dwregdst,
3611 union ctx *srccontext, ULONG_PTR dwregsrc)
3613 unsigned regdstno = csw->cpu->map_dwarf_register(dwregdst, module, TRUE), szdst;
3614 unsigned regsrcno = csw->cpu->map_dwarf_register(dwregsrc, module, TRUE), szsrc;
3615 ULONG_PTR* ptrdst = csw->cpu->fetch_context_reg(dstcontext, regdstno, &szdst);
3616 ULONG_PTR* ptrsrc = csw->cpu->fetch_context_reg(srccontext, regsrcno, &szsrc);
3618 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3619 if (szdst != szsrc)
3621 FIXME("Cannot copy register %Iu/%u => %Iu/%u because of size mismatch (%u => %u)\n",
3622 dwregsrc, regsrcno, dwregdst, regdstno, szsrc, szdst);
3623 return;
3625 memcpy(ptrdst, ptrsrc, szdst);
3628 static ULONG_PTR eval_expression(const struct module* module, struct cpu_stack_walk* csw,
3629 const unsigned char* zp, union ctx *context)
3631 dwarf2_traverse_context_t ctx;
3632 ULONG_PTR reg, sz, tmp;
3633 DWORD64 stack[64];
3634 int sp = -1;
3635 ULONG_PTR len;
3637 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3638 ctx.data = zp;
3639 ctx.end_data = zp + 4;
3640 len = dwarf2_leb128_as_unsigned(&ctx);
3641 ctx.end_data = ctx.data + len;
3643 while (ctx.data < ctx.end_data)
3645 unsigned char opcode = dwarf2_parse_byte(&ctx);
3647 if (opcode >= DW_OP_lit0 && opcode <= DW_OP_lit31)
3648 stack[++sp] = opcode - DW_OP_lit0;
3649 else if (opcode >= DW_OP_reg0 && opcode <= DW_OP_reg31)
3650 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_reg0);
3651 else if (opcode >= DW_OP_breg0 && opcode <= DW_OP_breg31)
3652 stack[++sp] = get_context_reg(module, csw, context, opcode - DW_OP_breg0)
3653 + dwarf2_leb128_as_signed(&ctx);
3654 else switch (opcode)
3656 case DW_OP_nop: break;
3657 case DW_OP_addr: stack[++sp] = dwarf2_parse_addr(&ctx, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size); break;
3658 case DW_OP_const1u: stack[++sp] = dwarf2_parse_byte(&ctx); break;
3659 case DW_OP_const1s: stack[++sp] = (signed char)dwarf2_parse_byte(&ctx); break;
3660 case DW_OP_const2u: stack[++sp] = dwarf2_parse_u2(&ctx); break;
3661 case DW_OP_const2s: stack[++sp] = (short)dwarf2_parse_u2(&ctx); break;
3662 case DW_OP_const4u: stack[++sp] = dwarf2_parse_u4(&ctx); break;
3663 case DW_OP_const4s: stack[++sp] = (signed int)dwarf2_parse_u4(&ctx); break;
3664 case DW_OP_const8u: stack[++sp] = dwarf2_parse_u8(&ctx); break;
3665 case DW_OP_const8s: stack[++sp] = (LONG_PTR)dwarf2_parse_u8(&ctx); break;
3666 case DW_OP_constu: stack[++sp] = dwarf2_leb128_as_unsigned(&ctx); break;
3667 case DW_OP_consts: stack[++sp] = dwarf2_leb128_as_signed(&ctx); break;
3668 case DW_OP_deref:
3669 tmp = 0;
3670 if (!sw_read_mem(csw, stack[sp], &tmp, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size))
3672 ERR("Couldn't read memory at %I64x\n", stack[sp]);
3673 tmp = 0;
3675 stack[sp] = tmp;
3676 break;
3677 case DW_OP_dup: stack[sp + 1] = stack[sp]; sp++; break;
3678 case DW_OP_drop: sp--; break;
3679 case DW_OP_over: stack[sp + 1] = stack[sp - 1]; sp++; break;
3680 case DW_OP_pick: stack[sp + 1] = stack[sp - dwarf2_parse_byte(&ctx)]; sp++; break;
3681 case DW_OP_swap: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = tmp; break;
3682 case DW_OP_rot: tmp = stack[sp]; stack[sp] = stack[sp-1]; stack[sp-1] = stack[sp-2]; stack[sp-2] = tmp; break;
3683 case DW_OP_abs: stack[sp] = sizeof(stack[sp]) == 8 ? llabs((INT64)stack[sp]) : abs((INT32)stack[sp]); break;
3684 case DW_OP_neg: stack[sp] = -stack[sp]; break;
3685 case DW_OP_not: stack[sp] = ~stack[sp]; break;
3686 case DW_OP_and: stack[sp-1] &= stack[sp]; sp--; break;
3687 case DW_OP_or: stack[sp-1] |= stack[sp]; sp--; break;
3688 case DW_OP_minus: stack[sp-1] -= stack[sp]; sp--; break;
3689 case DW_OP_mul: stack[sp-1] *= stack[sp]; sp--; break;
3690 case DW_OP_plus: stack[sp-1] += stack[sp]; sp--; break;
3691 case DW_OP_xor: stack[sp-1] ^= stack[sp]; sp--; break;
3692 case DW_OP_shl: stack[sp-1] <<= stack[sp]; sp--; break;
3693 case DW_OP_shr: stack[sp-1] >>= stack[sp]; sp--; break;
3694 case DW_OP_plus_uconst: stack[sp] += dwarf2_leb128_as_unsigned(&ctx); break;
3695 case DW_OP_shra: stack[sp-1] = (LONG_PTR)stack[sp-1] / (1 << stack[sp]); sp--; break;
3696 case DW_OP_div: stack[sp-1] = (LONG_PTR)stack[sp-1] / (LONG_PTR)stack[sp]; sp--; break;
3697 case DW_OP_mod: stack[sp-1] = (LONG_PTR)stack[sp-1] % (LONG_PTR)stack[sp]; sp--; break;
3698 case DW_OP_ge: stack[sp-1] = ((LONG_PTR)stack[sp-1] >= (LONG_PTR)stack[sp]); sp--; break;
3699 case DW_OP_gt: stack[sp-1] = ((LONG_PTR)stack[sp-1] > (LONG_PTR)stack[sp]); sp--; break;
3700 case DW_OP_le: stack[sp-1] = ((LONG_PTR)stack[sp-1] <= (LONG_PTR)stack[sp]); sp--; break;
3701 case DW_OP_lt: stack[sp-1] = ((LONG_PTR)stack[sp-1] < (LONG_PTR)stack[sp]); sp--; break;
3702 case DW_OP_eq: stack[sp-1] = (stack[sp-1] == stack[sp]); sp--; break;
3703 case DW_OP_ne: stack[sp-1] = (stack[sp-1] != stack[sp]); sp--; break;
3704 case DW_OP_skip: tmp = (short)dwarf2_parse_u2(&ctx); ctx.data += tmp; break;
3705 case DW_OP_bra: tmp = (short)dwarf2_parse_u2(&ctx); if (!stack[sp--]) ctx.data += tmp; break;
3706 case DW_OP_GNU_encoded_addr:
3707 tmp = dwarf2_parse_byte(&ctx);
3708 stack[++sp] = dwarf2_parse_augmentation_ptr(&ctx, tmp, module->format_info[DFI_DWARF]->u.dwarf2_info->word_size);
3709 break;
3710 case DW_OP_regx:
3711 stack[++sp] = get_context_reg(module, csw, context, dwarf2_leb128_as_unsigned(&ctx));
3712 break;
3713 case DW_OP_bregx:
3714 reg = dwarf2_leb128_as_unsigned(&ctx);
3715 tmp = dwarf2_leb128_as_signed(&ctx);
3716 stack[++sp] = get_context_reg(module, csw, context, reg) + tmp;
3717 break;
3718 case DW_OP_deref_size:
3719 sz = dwarf2_parse_byte(&ctx);
3720 if (!sw_read_mem(csw, stack[sp], &tmp, sz))
3722 ERR("Couldn't read memory at %I64x\n", stack[sp]);
3723 tmp = 0;
3725 /* do integral promotion */
3726 switch (sz)
3728 case 1: stack[sp] = *(unsigned char*)&tmp; break;
3729 case 2: stack[sp] = *(unsigned short*)&tmp; break;
3730 case 4: stack[sp] = *(unsigned int*)&tmp; break;
3731 case 8: stack[sp] = tmp; break; /* FIXME: won't work on 32bit platform */
3732 default: FIXME("Unknown size for deref 0x%Ix\n", sz);
3734 break;
3735 default:
3736 FIXME("unhandled opcode %02x\n", opcode);
3739 return stack[sp];
3742 static void apply_frame_state(const struct module* module, struct cpu_stack_walk* csw,
3743 union ctx *context, struct frame_state *state, DWORD64 *cfa)
3745 unsigned int i;
3746 ULONG_PTR value;
3747 union ctx new_context = *context;
3749 if (csw->cpu != module->cpu) FIXME("mismatch in cpu\n");
3750 switch (state->cfa_rule)
3752 case RULE_EXPRESSION:
3753 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3754 if (!sw_read_mem(csw, *cfa, cfa, csw->cpu->word_size))
3756 WARN("Couldn't read memory at %I64x\n", *cfa);
3757 return;
3759 break;
3760 case RULE_VAL_EXPRESSION:
3761 *cfa = eval_expression(module, csw, (const unsigned char*)state->cfa_offset, context);
3762 break;
3763 default:
3764 *cfa = get_context_reg(module, csw, context, state->cfa_reg) + (LONG_PTR)state->cfa_offset;
3765 break;
3767 if (!*cfa) return;
3769 for (i = 0; i < NB_FRAME_REGS; i++)
3771 switch (state->rules[i])
3773 case RULE_UNSET:
3774 case RULE_UNDEFINED:
3775 case RULE_SAME:
3776 break;
3777 case RULE_CFA_OFFSET:
3778 set_context_reg(module, csw, &new_context, i, *cfa + (LONG_PTR)state->regs[i], TRUE);
3779 break;
3780 case RULE_OTHER_REG:
3781 copy_context_reg(module, csw, &new_context, i, context, state->regs[i]);
3782 break;
3783 case RULE_EXPRESSION:
3784 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3785 set_context_reg(module, csw, &new_context, i, value, TRUE);
3786 break;
3787 case RULE_VAL_EXPRESSION:
3788 value = eval_expression(module, csw, (const unsigned char*)state->regs[i], context);
3789 set_context_reg(module, csw, &new_context, i, value, FALSE);
3790 break;
3793 *context = new_context;
3796 static BOOL dwarf2_fetch_frame_info(struct module* module, struct cpu* cpu, LONG_PTR ip, struct frame_info* info)
3798 dwarf2_traverse_context_t cie_ctx, fde_ctx;
3799 struct module_format* modfmt;
3800 const unsigned char* end;
3801 DWORD_PTR delta;
3803 modfmt = module->format_info[DFI_DWARF];
3804 if (!modfmt) return FALSE;
3805 memset(info, 0, sizeof(*info));
3806 fde_ctx.data = modfmt->u.dwarf2_info->eh_frame.address;
3807 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->eh_frame.size;
3808 /* let offsets relative to the eh_frame sections be correctly computed, as we'll map
3809 * in this process the IMAGE section at a different address as the one expected by
3810 * the image
3812 delta = module->module.BaseOfImage + modfmt->u.dwarf2_info->eh_frame.rva -
3813 (DWORD_PTR)modfmt->u.dwarf2_info->eh_frame.address;
3814 if (!dwarf2_get_cie(ip, module, delta, &fde_ctx, &cie_ctx, info, TRUE))
3816 fde_ctx.data = modfmt->u.dwarf2_info->debug_frame.address;
3817 fde_ctx.end_data = fde_ctx.data + modfmt->u.dwarf2_info->debug_frame.size;
3818 delta = module->reloc_delta;
3819 if (modfmt->u.dwarf2_info->debug_frame.address == IMAGE_NO_MAP ||
3820 !dwarf2_get_cie(ip, module, delta, &fde_ctx, &cie_ctx, info, FALSE))
3822 TRACE("Couldn't find information for %Ix\n", ip);
3823 return FALSE;
3827 TRACE("function %Ix/%Ix code_align %Iu data_align %Id retaddr %s\n",
3828 ip, info->ip, info->code_align, info->data_align,
3829 cpu->fetch_regname(cpu->map_dwarf_register(info->retaddr_reg, module, TRUE)));
3831 if (ip != info->ip)
3833 execute_cfa_instructions(module, &cie_ctx, ip, info);
3835 if (info->aug_z_format) /* get length of augmentation data */
3837 ULONG_PTR len = dwarf2_leb128_as_unsigned(&fde_ctx);
3838 end = fde_ctx.data + len;
3840 else end = NULL;
3841 dwarf2_parse_augmentation_ptr(&fde_ctx, info->lsda_encoding, modfmt->u.dwarf2_info->word_size); /* handler_data */
3842 if (end) fde_ctx.data = end;
3844 execute_cfa_instructions(module, &fde_ctx, ip, info);
3846 return TRUE;
3849 /***********************************************************************
3850 * dwarf2_virtual_unwind
3853 BOOL dwarf2_virtual_unwind(struct cpu_stack_walk *csw, ULONG_PTR ip,
3854 union ctx *context, DWORD64 *cfa)
3856 struct module_pair pair;
3857 struct frame_info info;
3859 if (!module_init_pair(&pair, csw->hProcess, ip)) return FALSE;
3860 if (csw->cpu != pair.effective->cpu) FIXME("mismatch in cpu\n");
3861 if (!dwarf2_fetch_frame_info(pair.effective, csw->cpu, ip, &info)) return FALSE;
3863 /* if at very beginning of function, return and use default unwinder */
3864 if (ip == info.ip) return FALSE;
3866 /* if there is no information about retaddr, use default unwinder */
3867 if (info.state.rules[info.retaddr_reg] == RULE_UNSET) return FALSE;
3869 apply_frame_state(pair.effective, csw, context, &info.state, cfa);
3871 return TRUE;
3874 static enum location_error compute_call_frame_cfa(struct module* module, ULONG_PTR ip, struct location* frame)
3876 struct frame_info info;
3878 if (!dwarf2_fetch_frame_info(module, module->cpu, ip, &info)) return loc_err_internal;
3880 /* beginning of function, or no available dwarf information ? */
3881 if (ip == info.ip || info.state.rules[info.retaddr_reg] == RULE_UNSET)
3883 /* fake the default unwinder */
3884 frame->kind = loc_regrel;
3885 frame->reg = module->cpu->frame_regno;
3886 frame->offset = module->cpu->word_size; /* FIXME stack direction */
3888 else
3890 /* we expect to translate the call_frame_cfa into a regrel location...
3891 * that should cover most of the cases
3893 switch (info.state.cfa_rule)
3895 case RULE_EXPRESSION:
3896 WARN("Too complex expression for frame_CFA resolution (RULE_EXPRESSION)\n");
3897 return loc_err_too_complex;
3898 case RULE_VAL_EXPRESSION:
3899 /* unfortunately, we've seen at least construct like:
3900 * cfa := 'breg_x + offset; deref'
3901 * which is an indirection too much for the DbgHelp API.
3903 WARN("Too complex expression for frame_CFA resolution (RULE_VAL_EXPRESSION)\n");
3904 return loc_err_too_complex;
3905 default:
3906 frame->kind = loc_regrel;
3907 frame->reg = module->cpu->map_dwarf_register(info.state.cfa_reg, module, TRUE);
3908 frame->offset = info.state.cfa_offset;
3909 break;
3912 return 0;
3915 static void dwarf2_location_compute(struct process* pcs,
3916 const struct module_format* modfmt,
3917 const struct symt_function* func,
3918 struct location* loc)
3920 struct location frame;
3921 DWORD_PTR ip;
3922 int err;
3923 dwarf2_traverse_context_t lctx;
3924 const dwarf2_cuhead_t* head = get_cuhead_from_func(func);
3926 if (!head)
3928 WARN("We'd expect function %s's container to be a valid compiland with dwarf inforamation\n",
3929 debugstr_a(func->hash_elt.name));
3930 err = loc_err_internal;
3932 else
3934 /* instruction pointer relative to compiland's start */
3935 ip = pcs->localscope_pc - ((struct symt_compiland*)func->container)->address;
3937 if ((err = loc_compute_frame(pcs, modfmt, func, ip, head, &frame)) == 0)
3939 switch (loc->kind)
3941 case loc_dwarf2_location_list:
3942 /* Then, if the variable has a location list, find it !! */
3943 if (dwarf2_lookup_loclist(modfmt, head,
3944 modfmt->u.dwarf2_info->debug_loc.address + loc->offset,
3945 ip, &lctx))
3946 goto do_compute;
3947 err = loc_err_out_of_scope;
3948 break;
3949 case loc_dwarf2_block:
3950 /* or if we have a copy of an existing block, get ready for it */
3952 unsigned* ptr = (unsigned*)loc->offset;
3954 lctx.data = (const BYTE*)(ptr + 1);
3955 lctx.end_data = lctx.data + *ptr;
3957 do_compute:
3958 /* now get the variable */
3959 err = compute_location(modfmt->module, head,
3960 &lctx, loc, pcs->handle, &frame);
3961 break;
3962 case loc_register:
3963 case loc_regrel:
3964 /* nothing to do */
3965 break;
3966 default:
3967 WARN("Unsupported local kind %d\n", loc->kind);
3968 err = loc_err_internal;
3972 if (err < 0)
3974 loc->kind = loc_register;
3975 loc->reg = err;
3979 static void *zalloc(void *priv, uInt items, uInt sz)
3981 return HeapAlloc(GetProcessHeap(), 0, items * sz);
3984 static void zfree(void *priv, void *addr)
3986 HeapFree(GetProcessHeap(), 0, addr);
3989 static inline BOOL dwarf2_init_zsection(dwarf2_section_t* section,
3990 const char* zsectname,
3991 struct image_section_map* ism)
3993 z_stream z;
3994 LARGE_INTEGER li;
3995 int res;
3996 BOOL ret = FALSE;
3998 BYTE *addr, *sect = (BYTE *)image_map_section(ism);
3999 size_t sz = image_get_map_size(ism);
4001 if (sz <= 12 || memcmp(sect, "ZLIB", 4))
4003 ERR("invalid compressed section %s\n", debugstr_a(zsectname));
4004 goto out;
4007 #ifdef WORDS_BIGENDIAN
4008 li.u.HighPart = *(DWORD*)&sect[4];
4009 li.u.LowPart = *(DWORD*)&sect[8];
4010 #else
4011 li.u.HighPart = RtlUlongByteSwap(*(DWORD*)&sect[4]);
4012 li.u.LowPart = RtlUlongByteSwap(*(DWORD*)&sect[8]);
4013 #endif
4015 addr = HeapAlloc(GetProcessHeap(), 0, li.QuadPart);
4016 if (!addr)
4017 goto out;
4019 z.next_in = &sect[12];
4020 z.avail_in = sz - 12;
4021 z.opaque = NULL;
4022 z.zalloc = zalloc;
4023 z.zfree = zfree;
4025 res = inflateInit(&z);
4026 if (res != Z_OK)
4028 FIXME("inflateInit failed with %i / %s\n", res, debugstr_a(z.msg));
4029 goto out_free;
4032 do {
4033 z.next_out = addr + z.total_out;
4034 z.avail_out = li.QuadPart - z.total_out;
4035 res = inflate(&z, Z_FINISH);
4036 } while (z.avail_in && res == Z_STREAM_END);
4038 if (res != Z_STREAM_END)
4040 FIXME("Decompression failed with %i / %s\n", res, debugstr_a(z.msg));
4041 goto out_end;
4044 ret = TRUE;
4045 section->compressed = TRUE;
4046 section->address = addr;
4047 section->rva = image_get_map_rva(ism);
4048 section->size = z.total_out;
4050 out_end:
4051 inflateEnd(&z);
4052 out_free:
4053 if (!ret)
4054 HeapFree(GetProcessHeap(), 0, addr);
4055 out:
4056 image_unmap_section(ism);
4057 return ret;
4060 static inline BOOL dwarf2_init_section(dwarf2_section_t* section, struct image_file_map* fmap,
4061 const char* sectname, const char* zsectname,
4062 struct image_section_map* ism)
4064 struct image_section_map local_ism;
4066 if (!ism) ism = &local_ism;
4068 section->compressed = FALSE;
4069 if (image_find_section(fmap, sectname, ism))
4071 section->address = (const BYTE*)image_map_section(ism);
4072 section->size = image_get_map_size(ism);
4073 section->rva = image_get_map_rva(ism);
4074 return TRUE;
4077 section->address = NULL;
4078 section->size = 0;
4079 section->rva = 0;
4081 if (zsectname && image_find_section(fmap, zsectname, ism))
4083 return dwarf2_init_zsection(section, zsectname, ism);
4086 return FALSE;
4089 static inline void dwarf2_fini_section(dwarf2_section_t* section)
4091 if (section->compressed)
4092 HeapFree(GetProcessHeap(), 0, (void*)section->address);
4095 static void dwarf2_module_remove(struct process* pcs, struct module_format* modfmt)
4097 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_loc);
4098 dwarf2_fini_section(&modfmt->u.dwarf2_info->debug_frame);
4099 free(modfmt->u.dwarf2_info->cuheads);
4100 HeapFree(GetProcessHeap(), 0, modfmt);
4103 static BOOL dwarf2_load_CU_module(dwarf2_parse_module_context_t* module_ctx, struct module* module,
4104 dwarf2_section_t* sections, ULONG_PTR load_offset,
4105 const struct elf_thunk_area* thunks, BOOL is_dwz)
4107 dwarf2_traverse_context_t mod_ctx;
4108 unsigned i;
4110 module_ctx->sections = sections;
4111 module_ctx->module = module;
4112 module_ctx->thunks = thunks;
4113 module_ctx->load_offset = load_offset;
4114 vector_init(&module_ctx->unit_contexts, sizeof(dwarf2_parse_context_t), 16);
4115 module_ctx->cu_versions = 0;
4117 /* phase I: parse all CU heads */
4118 mod_ctx.data = sections[section_debug].address;
4119 mod_ctx.end_data = mod_ctx.data + sections[section_debug].size;
4120 while (mod_ctx.data < mod_ctx.end_data)
4122 dwarf2_parse_context_t* unit_ctx = vector_add(&module_ctx->unit_contexts, &module_ctx->module->pool);
4124 unit_ctx->module_ctx = module_ctx;
4125 dwarf2_parse_compilation_unit_head(unit_ctx, &mod_ctx);
4128 /* phase2: load content of all CU
4129 * If this is a DWZ alternate module, don't load all debug_info at once
4130 * wait for main module to ask for them (it's likely it won't need them all)
4131 * Doing this can lead to a huge performance improvement.
4133 if (!is_dwz)
4134 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
4135 dwarf2_parse_compilation_unit((dwarf2_parse_context_t*)vector_at(&module_ctx->unit_contexts, i));
4137 return TRUE;
4140 static dwarf2_dwz_alternate_t* dwarf2_load_dwz(struct image_file_map* fmap, struct module* module)
4142 struct image_file_map* fmap_dwz;
4143 dwarf2_dwz_alternate_t* dwz;
4145 fmap_dwz = image_load_debugaltlink(fmap, module);
4146 if (!fmap_dwz) return NULL;
4147 if (!(dwz = HeapAlloc(GetProcessHeap(), 0, sizeof(*dwz))))
4149 image_unmap_file(fmap_dwz);
4150 HeapFree(GetProcessHeap(), 0, fmap_dwz);
4151 return NULL;
4154 dwz->fmap = fmap_dwz;
4155 dwarf2_init_section(&dwz->sections[section_debug], fmap_dwz, ".debug_info", ".zdebug_info", &dwz->sectmap[section_debug]);
4156 dwarf2_init_section(&dwz->sections[section_abbrev], fmap_dwz, ".debug_abbrev", ".zdebug_abbrev", &dwz->sectmap[section_abbrev]);
4157 dwarf2_init_section(&dwz->sections[section_string], fmap_dwz, ".debug_str", ".zdebug_str", &dwz->sectmap[section_string]);
4158 dwarf2_init_section(&dwz->sections[section_line], fmap_dwz, ".debug_line", ".zdebug_line", &dwz->sectmap[section_line]);
4159 dwarf2_init_section(&dwz->sections[section_ranges], fmap_dwz, ".debug_ranges", ".zdebug_ranges", &dwz->sectmap[section_ranges]);
4161 dwz->module_ctx.dwz = NULL;
4162 dwarf2_load_CU_module(&dwz->module_ctx, module, dwz->sections, 0/*FIXME*/, NULL, TRUE);
4163 return dwz;
4166 static void dwarf2_unload_dwz(dwarf2_dwz_alternate_t* dwz)
4168 if (!dwz) return;
4169 dwarf2_fini_section(&dwz->sections[section_debug]);
4170 dwarf2_fini_section(&dwz->sections[section_abbrev]);
4171 dwarf2_fini_section(&dwz->sections[section_string]);
4172 dwarf2_fini_section(&dwz->sections[section_line]);
4173 dwarf2_fini_section(&dwz->sections[section_ranges]);
4175 image_unmap_section(&dwz->sectmap[section_debug]);
4176 image_unmap_section(&dwz->sectmap[section_abbrev]);
4177 image_unmap_section(&dwz->sectmap[section_string]);
4178 image_unmap_section(&dwz->sectmap[section_line]);
4179 image_unmap_section(&dwz->sectmap[section_ranges]);
4181 HeapFree(GetProcessHeap(), 0, dwz);
4184 static BOOL dwarf2_unload_CU_module(dwarf2_parse_module_context_t* module_ctx)
4186 unsigned i;
4187 for (i = 0; i < module_ctx->unit_contexts.num_elts; ++i)
4189 dwarf2_parse_context_t* unit = vector_at(&module_ctx->unit_contexts, i);
4190 if (unit->status != UNIT_ERROR)
4191 pool_destroy(&unit->pool);
4193 dwarf2_unload_dwz(module_ctx->dwz);
4194 return TRUE;
4197 BOOL dwarf2_parse(struct module* module, ULONG_PTR load_offset,
4198 const struct elf_thunk_area* thunks,
4199 struct image_file_map* fmap)
4201 dwarf2_section_t eh_frame, section[section_max];
4202 struct image_section_map debug_sect, debug_str_sect, debug_abbrev_sect,
4203 debug_line_sect, debug_ranges_sect, eh_frame_sect;
4204 BOOL ret = TRUE;
4205 struct module_format* dwarf2_modfmt;
4206 dwarf2_parse_module_context_t module_ctx;
4208 if (!dwarf2_init_section(&eh_frame, fmap, ".eh_frame", NULL, &eh_frame_sect))
4209 /* lld produces .eh_fram to avoid generating a long name */
4210 dwarf2_init_section(&eh_frame, fmap, ".eh_fram", NULL, &eh_frame_sect);
4211 dwarf2_init_section(&section[section_debug], fmap, ".debug_info", ".zdebug_info", &debug_sect);
4212 dwarf2_init_section(&section[section_abbrev], fmap, ".debug_abbrev", ".zdebug_abbrev", &debug_abbrev_sect);
4213 dwarf2_init_section(&section[section_string], fmap, ".debug_str", ".zdebug_str", &debug_str_sect);
4214 dwarf2_init_section(&section[section_line], fmap, ".debug_line", ".zdebug_line", &debug_line_sect);
4215 dwarf2_init_section(&section[section_ranges], fmap, ".debug_ranges", ".zdebug_ranges", &debug_ranges_sect);
4217 /* to do anything useful we need either .eh_frame or .debug_info */
4218 if ((!eh_frame.address || eh_frame.address == IMAGE_NO_MAP) &&
4219 (!section[section_debug].address || section[section_debug].address == IMAGE_NO_MAP))
4221 ret = FALSE;
4222 goto leave;
4225 if (fmap->modtype == DMT_ELF && debug_sect.fmap)
4227 /* debug info might have a different base address than .so file
4228 * when elf file is prelinked after splitting off debug info
4229 * adjust symbol base addresses accordingly
4231 load_offset += fmap->u.elf.elf_start - debug_sect.fmap->u.elf.elf_start;
4234 TRACE("Loading Dwarf2 information for %s\n", debugstr_w(module->modulename));
4236 dwarf2_modfmt = HeapAlloc(GetProcessHeap(), 0,
4237 sizeof(*dwarf2_modfmt) + sizeof(*dwarf2_modfmt->u.dwarf2_info));
4238 if (!dwarf2_modfmt)
4240 ret = FALSE;
4241 goto leave;
4243 dwarf2_modfmt->module = module;
4244 dwarf2_modfmt->remove = dwarf2_module_remove;
4245 dwarf2_modfmt->loc_compute = dwarf2_location_compute;
4246 dwarf2_modfmt->u.dwarf2_info = (struct dwarf2_module_info_s*)(dwarf2_modfmt + 1);
4247 dwarf2_modfmt->u.dwarf2_info->word_size = fmap->addr_size / 8; /* set the word_size for eh_frame parsing */
4248 dwarf2_modfmt->module->format_info[DFI_DWARF] = dwarf2_modfmt;
4250 /* As we'll need later some sections' content, we won't unmap these
4251 * sections upon existing this function
4253 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_loc, fmap, ".debug_loc", ".zdebug_loc", NULL);
4254 dwarf2_init_section(&dwarf2_modfmt->u.dwarf2_info->debug_frame, fmap, ".debug_frame", ".zdebug_frame", NULL);
4255 dwarf2_modfmt->u.dwarf2_info->eh_frame = eh_frame;
4256 dwarf2_modfmt->u.dwarf2_info->cuheads = NULL;
4257 dwarf2_modfmt->u.dwarf2_info->num_cuheads = 0;
4259 module_ctx.dwz = dwarf2_load_dwz(fmap, module);
4260 dwarf2_load_CU_module(&module_ctx, module, section, load_offset, thunks, FALSE);
4262 dwarf2_modfmt->module->module.SymType = SymDia;
4263 /* hide dwarf versions in CVSig
4264 * bits 24-31 will be set according to found dwarf version
4265 * different CU can have different dwarf version, so use a bit per version (version 2 => b24)
4267 dwarf2_modfmt->module->module.CVSig = 'D' | ('W' << 8) | ('F' << 16) | ((module_ctx.cu_versions & 0xFF) << 24);
4268 /* FIXME: we could have a finer grain here */
4269 dwarf2_modfmt->module->module.GlobalSymbols = TRUE;
4270 dwarf2_modfmt->module->module.TypeInfo = TRUE;
4271 dwarf2_modfmt->module->module.SourceIndexed = TRUE;
4272 dwarf2_modfmt->module->module.Publics = TRUE;
4274 dwarf2_unload_CU_module(&module_ctx);
4275 leave:
4277 dwarf2_fini_section(&section[section_debug]);
4278 dwarf2_fini_section(&section[section_abbrev]);
4279 dwarf2_fini_section(&section[section_string]);
4280 dwarf2_fini_section(&section[section_line]);
4281 dwarf2_fini_section(&section[section_ranges]);
4283 image_unmap_section(&debug_sect);
4284 image_unmap_section(&debug_abbrev_sect);
4285 image_unmap_section(&debug_str_sect);
4286 image_unmap_section(&debug_line_sect);
4287 image_unmap_section(&debug_ranges_sect);
4288 if (!ret) image_unmap_section(&eh_frame_sect);
4290 return ret;