VirtualBox

source: vbox/trunk/src/VBox/Runtime/common/dbg/dbgmoddwarf.cpp@ 73531

Last change on this file since 73531 was 73531, checked in by vboxsync, 6 years ago

IPRT: Some work on stack unwinding using dwarf info. bugref:3897

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 200.3 KB
Line 
1/* $Id: dbgmoddwarf.cpp 73531 2018-08-06 17:08:57Z vboxsync $ */
2/** @file
3 * IPRT - Debug Info Reader For DWARF.
4 */
5
6/*
7 * Copyright (C) 2011-2017 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*********************************************************************************************************************************
29* Header Files *
30*********************************************************************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DBG_DWARF
32#include <iprt/dbg.h>
33#include "internal/iprt.h"
34
35#include <iprt/asm.h>
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/list.h>
39#include <iprt/log.h>
40#include <iprt/mem.h>
41#define RTDBGMODDWARF_WITH_MEM_CACHE
42#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
43# include <iprt/memcache.h>
44#endif
45#include <iprt/path.h>
46#include <iprt/string.h>
47#include <iprt/strcache.h>
48#include "internal/dbgmod.h"
49
50
51/*********************************************************************************************************************************
52* Defined Constants And Macros *
53*********************************************************************************************************************************/
54/** @name Standard DWARF Line Number Opcodes
55 * @{ */
56#define DW_LNS_extended UINT8_C(0x00)
57#define DW_LNS_copy UINT8_C(0x01)
58#define DW_LNS_advance_pc UINT8_C(0x02)
59#define DW_LNS_advance_line UINT8_C(0x03)
60#define DW_LNS_set_file UINT8_C(0x04)
61#define DW_LNS_set_column UINT8_C(0x05)
62#define DW_LNS_negate_stmt UINT8_C(0x06)
63#define DW_LNS_set_basic_block UINT8_C(0x07)
64#define DW_LNS_const_add_pc UINT8_C(0x08)
65#define DW_LNS_fixed_advance_pc UINT8_C(0x09)
66#define DW_LNS_set_prologue_end UINT8_C(0x0a)
67#define DW_LNS_set_epilogue_begin UINT8_C(0x0b)
68#define DW_LNS_set_isa UINT8_C(0x0c)
69#define DW_LNS_what_question_mark UINT8_C(0x0d)
70/** @} */
71
72
73/** @name Extended DWARF Line Number Opcodes
74 * @{ */
75#define DW_LNE_end_sequence UINT8_C(1)
76#define DW_LNE_set_address UINT8_C(2)
77#define DW_LNE_define_file UINT8_C(3)
78#define DW_LNE_set_descriminator UINT8_C(4)
79/** @} */
80
81/** @name DIE Tags.
82 * @{ */
83#define DW_TAG_array_type UINT16_C(0x0001)
84#define DW_TAG_class_type UINT16_C(0x0002)
85#define DW_TAG_entry_point UINT16_C(0x0003)
86#define DW_TAG_enumeration_type UINT16_C(0x0004)
87#define DW_TAG_formal_parameter UINT16_C(0x0005)
88#define DW_TAG_imported_declaration UINT16_C(0x0008)
89#define DW_TAG_label UINT16_C(0x000a)
90#define DW_TAG_lexical_block UINT16_C(0x000b)
91#define DW_TAG_member UINT16_C(0x000d)
92#define DW_TAG_pointer_type UINT16_C(0x000f)
93#define DW_TAG_reference_type UINT16_C(0x0010)
94#define DW_TAG_compile_unit UINT16_C(0x0011)
95#define DW_TAG_string_type UINT16_C(0x0012)
96#define DW_TAG_structure_type UINT16_C(0x0013)
97#define DW_TAG_subroutine_type UINT16_C(0x0015)
98#define DW_TAG_typedef UINT16_C(0x0016)
99#define DW_TAG_union_type UINT16_C(0x0017)
100#define DW_TAG_unspecified_parameters UINT16_C(0x0018)
101#define DW_TAG_variant UINT16_C(0x0019)
102#define DW_TAG_common_block UINT16_C(0x001a)
103#define DW_TAG_common_inclusion UINT16_C(0x001b)
104#define DW_TAG_inheritance UINT16_C(0x001c)
105#define DW_TAG_inlined_subroutine UINT16_C(0x001d)
106#define DW_TAG_module UINT16_C(0x001e)
107#define DW_TAG_ptr_to_member_type UINT16_C(0x001f)
108#define DW_TAG_set_type UINT16_C(0x0020)
109#define DW_TAG_subrange_type UINT16_C(0x0021)
110#define DW_TAG_with_stmt UINT16_C(0x0022)
111#define DW_TAG_access_declaration UINT16_C(0x0023)
112#define DW_TAG_base_type UINT16_C(0x0024)
113#define DW_TAG_catch_block UINT16_C(0x0025)
114#define DW_TAG_const_type UINT16_C(0x0026)
115#define DW_TAG_constant UINT16_C(0x0027)
116#define DW_TAG_enumerator UINT16_C(0x0028)
117#define DW_TAG_file_type UINT16_C(0x0029)
118#define DW_TAG_friend UINT16_C(0x002a)
119#define DW_TAG_namelist UINT16_C(0x002b)
120#define DW_TAG_namelist_item UINT16_C(0x002c)
121#define DW_TAG_packed_type UINT16_C(0x002d)
122#define DW_TAG_subprogram UINT16_C(0x002e)
123#define DW_TAG_template_type_parameter UINT16_C(0x002f)
124#define DW_TAG_template_value_parameter UINT16_C(0x0030)
125#define DW_TAG_thrown_type UINT16_C(0x0031)
126#define DW_TAG_try_block UINT16_C(0x0032)
127#define DW_TAG_variant_part UINT16_C(0x0033)
128#define DW_TAG_variable UINT16_C(0x0034)
129#define DW_TAG_volatile_type UINT16_C(0x0035)
130#define DW_TAG_dwarf_procedure UINT16_C(0x0036)
131#define DW_TAG_restrict_type UINT16_C(0x0037)
132#define DW_TAG_interface_type UINT16_C(0x0038)
133#define DW_TAG_namespace UINT16_C(0x0039)
134#define DW_TAG_imported_module UINT16_C(0x003a)
135#define DW_TAG_unspecified_type UINT16_C(0x003b)
136#define DW_TAG_partial_unit UINT16_C(0x003c)
137#define DW_TAG_imported_unit UINT16_C(0x003d)
138#define DW_TAG_condition UINT16_C(0x003f)
139#define DW_TAG_shared_type UINT16_C(0x0040)
140#define DW_TAG_type_unit UINT16_C(0x0041)
141#define DW_TAG_rvalue_reference_type UINT16_C(0x0042)
142#define DW_TAG_template_alias UINT16_C(0x0043)
143#define DW_TAG_lo_user UINT16_C(0x4080)
144#define DW_TAG_GNU_call_site UINT16_C(0x4109)
145#define DW_TAG_GNU_call_site_parameter UINT16_C(0x410a)
146#define DW_TAG_hi_user UINT16_C(0xffff)
147/** @} */
148
149
150/** @name DIE Attributes.
151 * @{ */
152#define DW_AT_sibling UINT16_C(0x0001)
153#define DW_AT_location UINT16_C(0x0002)
154#define DW_AT_name UINT16_C(0x0003)
155#define DW_AT_ordering UINT16_C(0x0009)
156#define DW_AT_byte_size UINT16_C(0x000b)
157#define DW_AT_bit_offset UINT16_C(0x000c)
158#define DW_AT_bit_size UINT16_C(0x000d)
159#define DW_AT_stmt_list UINT16_C(0x0010)
160#define DW_AT_low_pc UINT16_C(0x0011)
161#define DW_AT_high_pc UINT16_C(0x0012)
162#define DW_AT_language UINT16_C(0x0013)
163#define DW_AT_discr UINT16_C(0x0015)
164#define DW_AT_discr_value UINT16_C(0x0016)
165#define DW_AT_visibility UINT16_C(0x0017)
166#define DW_AT_import UINT16_C(0x0018)
167#define DW_AT_string_length UINT16_C(0x0019)
168#define DW_AT_common_reference UINT16_C(0x001a)
169#define DW_AT_comp_dir UINT16_C(0x001b)
170#define DW_AT_const_value UINT16_C(0x001c)
171#define DW_AT_containing_type UINT16_C(0x001d)
172#define DW_AT_default_value UINT16_C(0x001e)
173#define DW_AT_inline UINT16_C(0x0020)
174#define DW_AT_is_optional UINT16_C(0x0021)
175#define DW_AT_lower_bound UINT16_C(0x0022)
176#define DW_AT_producer UINT16_C(0x0025)
177#define DW_AT_prototyped UINT16_C(0x0027)
178#define DW_AT_return_addr UINT16_C(0x002a)
179#define DW_AT_start_scope UINT16_C(0x002c)
180#define DW_AT_bit_stride UINT16_C(0x002e)
181#define DW_AT_upper_bound UINT16_C(0x002f)
182#define DW_AT_abstract_origin UINT16_C(0x0031)
183#define DW_AT_accessibility UINT16_C(0x0032)
184#define DW_AT_address_class UINT16_C(0x0033)
185#define DW_AT_artificial UINT16_C(0x0034)
186#define DW_AT_base_types UINT16_C(0x0035)
187#define DW_AT_calling_convention UINT16_C(0x0036)
188#define DW_AT_count UINT16_C(0x0037)
189#define DW_AT_data_member_location UINT16_C(0x0038)
190#define DW_AT_decl_column UINT16_C(0x0039)
191#define DW_AT_decl_file UINT16_C(0x003a)
192#define DW_AT_decl_line UINT16_C(0x003b)
193#define DW_AT_declaration UINT16_C(0x003c)
194#define DW_AT_discr_list UINT16_C(0x003d)
195#define DW_AT_encoding UINT16_C(0x003e)
196#define DW_AT_external UINT16_C(0x003f)
197#define DW_AT_frame_base UINT16_C(0x0040)
198#define DW_AT_friend UINT16_C(0x0041)
199#define DW_AT_identifier_case UINT16_C(0x0042)
200#define DW_AT_macro_info UINT16_C(0x0043)
201#define DW_AT_namelist_item UINT16_C(0x0044)
202#define DW_AT_priority UINT16_C(0x0045)
203#define DW_AT_segment UINT16_C(0x0046)
204#define DW_AT_specification UINT16_C(0x0047)
205#define DW_AT_static_link UINT16_C(0x0048)
206#define DW_AT_type UINT16_C(0x0049)
207#define DW_AT_use_location UINT16_C(0x004a)
208#define DW_AT_variable_parameter UINT16_C(0x004b)
209#define DW_AT_virtuality UINT16_C(0x004c)
210#define DW_AT_vtable_elem_location UINT16_C(0x004d)
211#define DW_AT_allocated UINT16_C(0x004e)
212#define DW_AT_associated UINT16_C(0x004f)
213#define DW_AT_data_location UINT16_C(0x0050)
214#define DW_AT_byte_stride UINT16_C(0x0051)
215#define DW_AT_entry_pc UINT16_C(0x0052)
216#define DW_AT_use_UTF8 UINT16_C(0x0053)
217#define DW_AT_extension UINT16_C(0x0054)
218#define DW_AT_ranges UINT16_C(0x0055)
219#define DW_AT_trampoline UINT16_C(0x0056)
220#define DW_AT_call_column UINT16_C(0x0057)
221#define DW_AT_call_file UINT16_C(0x0058)
222#define DW_AT_call_line UINT16_C(0x0059)
223#define DW_AT_description UINT16_C(0x005a)
224#define DW_AT_binary_scale UINT16_C(0x005b)
225#define DW_AT_decimal_scale UINT16_C(0x005c)
226#define DW_AT_small UINT16_C(0x005d)
227#define DW_AT_decimal_sign UINT16_C(0x005e)
228#define DW_AT_digit_count UINT16_C(0x005f)
229#define DW_AT_picture_string UINT16_C(0x0060)
230#define DW_AT_mutable UINT16_C(0x0061)
231#define DW_AT_threads_scaled UINT16_C(0x0062)
232#define DW_AT_explicit UINT16_C(0x0063)
233#define DW_AT_object_pointer UINT16_C(0x0064)
234#define DW_AT_endianity UINT16_C(0x0065)
235#define DW_AT_elemental UINT16_C(0x0066)
236#define DW_AT_pure UINT16_C(0x0067)
237#define DW_AT_recursive UINT16_C(0x0068)
238#define DW_AT_signature UINT16_C(0x0069)
239#define DW_AT_main_subprogram UINT16_C(0x006a)
240#define DW_AT_data_bit_offset UINT16_C(0x006b)
241#define DW_AT_const_expr UINT16_C(0x006c)
242#define DW_AT_enum_class UINT16_C(0x006d)
243#define DW_AT_linkage_name UINT16_C(0x006e)
244#define DW_AT_lo_user UINT16_C(0x2000)
245/** Used by GCC and others, same as DW_AT_linkage_name. See http://wiki.dwarfstd.org/index.php?title=DW_AT_linkage_name*/
246#define DW_AT_MIPS_linkage_name UINT16_C(0x2007)
247#define DW_AT_hi_user UINT16_C(0x3fff)
248/** @} */
249
250/** @name DIE Forms.
251 * @{ */
252#define DW_FORM_addr UINT16_C(0x01)
253/* What was 0x02? */
254#define DW_FORM_block2 UINT16_C(0x03)
255#define DW_FORM_block4 UINT16_C(0x04)
256#define DW_FORM_data2 UINT16_C(0x05)
257#define DW_FORM_data4 UINT16_C(0x06)
258#define DW_FORM_data8 UINT16_C(0x07)
259#define DW_FORM_string UINT16_C(0x08)
260#define DW_FORM_block UINT16_C(0x09)
261#define DW_FORM_block1 UINT16_C(0x0a)
262#define DW_FORM_data1 UINT16_C(0x0b)
263#define DW_FORM_flag UINT16_C(0x0c)
264#define DW_FORM_sdata UINT16_C(0x0d)
265#define DW_FORM_strp UINT16_C(0x0e)
266#define DW_FORM_udata UINT16_C(0x0f)
267#define DW_FORM_ref_addr UINT16_C(0x10)
268#define DW_FORM_ref1 UINT16_C(0x11)
269#define DW_FORM_ref2 UINT16_C(0x12)
270#define DW_FORM_ref4 UINT16_C(0x13)
271#define DW_FORM_ref8 UINT16_C(0x14)
272#define DW_FORM_ref_udata UINT16_C(0x15)
273#define DW_FORM_indirect UINT16_C(0x16)
274#define DW_FORM_sec_offset UINT16_C(0x17)
275#define DW_FORM_exprloc UINT16_C(0x18)
276#define DW_FORM_flag_present UINT16_C(0x19)
277#define DW_FORM_ref_sig8 UINT16_C(0x20)
278/** @} */
279
280/** @name Address classes.
281 * @{ */
282#define DW_ADDR_none UINT8_C(0)
283#define DW_ADDR_i386_near16 UINT8_C(1)
284#define DW_ADDR_i386_far16 UINT8_C(2)
285#define DW_ADDR_i386_huge16 UINT8_C(3)
286#define DW_ADDR_i386_near32 UINT8_C(4)
287#define DW_ADDR_i386_far32 UINT8_C(5)
288/** @} */
289
290
291/** @name Location Expression Opcodes
292 * @{ */
293#define DW_OP_addr UINT8_C(0x03) /**< 1 operand, a constant address (size target specific). */
294#define DW_OP_deref UINT8_C(0x06) /**< 0 operands. */
295#define DW_OP_const1u UINT8_C(0x08) /**< 1 operand, a 1-byte constant. */
296#define DW_OP_const1s UINT8_C(0x09) /**< 1 operand, a 1-byte constant. */
297#define DW_OP_const2u UINT8_C(0x0a) /**< 1 operand, a 2-byte constant. */
298#define DW_OP_const2s UINT8_C(0x0b) /**< 1 operand, a 2-byte constant. */
299#define DW_OP_const4u UINT8_C(0x0c) /**< 1 operand, a 4-byte constant. */
300#define DW_OP_const4s UINT8_C(0x0d) /**< 1 operand, a 4-byte constant. */
301#define DW_OP_const8u UINT8_C(0x0e) /**< 1 operand, a 8-byte constant. */
302#define DW_OP_const8s UINT8_C(0x0f) /**< 1 operand, a 8-byte constant. */
303#define DW_OP_constu UINT8_C(0x10) /**< 1 operand, a ULEB128 constant. */
304#define DW_OP_consts UINT8_C(0x11) /**< 1 operand, a SLEB128 constant. */
305#define DW_OP_dup UINT8_C(0x12) /**< 0 operands. */
306#define DW_OP_drop UINT8_C(0x13) /**< 0 operands. */
307#define DW_OP_over UINT8_C(0x14) /**< 0 operands. */
308#define DW_OP_pick UINT8_C(0x15) /**< 1 operands, a 1-byte stack index. */
309#define DW_OP_swap UINT8_C(0x16) /**< 0 operands. */
310#define DW_OP_rot UINT8_C(0x17) /**< 0 operands. */
311#define DW_OP_xderef UINT8_C(0x18) /**< 0 operands. */
312#define DW_OP_abs UINT8_C(0x19) /**< 0 operands. */
313#define DW_OP_and UINT8_C(0x1a) /**< 0 operands. */
314#define DW_OP_div UINT8_C(0x1b) /**< 0 operands. */
315#define DW_OP_minus UINT8_C(0x1c) /**< 0 operands. */
316#define DW_OP_mod UINT8_C(0x1d) /**< 0 operands. */
317#define DW_OP_mul UINT8_C(0x1e) /**< 0 operands. */
318#define DW_OP_neg UINT8_C(0x1f) /**< 0 operands. */
319#define DW_OP_not UINT8_C(0x20) /**< 0 operands. */
320#define DW_OP_or UINT8_C(0x21) /**< 0 operands. */
321#define DW_OP_plus UINT8_C(0x22) /**< 0 operands. */
322#define DW_OP_plus_uconst UINT8_C(0x23) /**< 1 operands, a ULEB128 addend. */
323#define DW_OP_shl UINT8_C(0x24) /**< 0 operands. */
324#define DW_OP_shr UINT8_C(0x25) /**< 0 operands. */
325#define DW_OP_shra UINT8_C(0x26) /**< 0 operands. */
326#define DW_OP_xor UINT8_C(0x27) /**< 0 operands. */
327#define DW_OP_skip UINT8_C(0x2f) /**< 1 signed 2-byte constant. */
328#define DW_OP_bra UINT8_C(0x28) /**< 1 signed 2-byte constant. */
329#define DW_OP_eq UINT8_C(0x29) /**< 0 operands. */
330#define DW_OP_ge UINT8_C(0x2a) /**< 0 operands. */
331#define DW_OP_gt UINT8_C(0x2b) /**< 0 operands. */
332#define DW_OP_le UINT8_C(0x2c) /**< 0 operands. */
333#define DW_OP_lt UINT8_C(0x2d) /**< 0 operands. */
334#define DW_OP_ne UINT8_C(0x2e) /**< 0 operands. */
335#define DW_OP_lit0 UINT8_C(0x30) /**< 0 operands - literals 0..31 */
336#define DW_OP_lit31 UINT8_C(0x4f) /**< last litteral. */
337#define DW_OP_reg0 UINT8_C(0x50) /**< 0 operands - reg 0..31. */
338#define DW_OP_reg31 UINT8_C(0x6f) /**< last register. */
339#define DW_OP_breg0 UINT8_C(0x70) /**< 1 operand, a SLEB128 offset. */
340#define DW_OP_breg31 UINT8_C(0x8f) /**< last branch register. */
341#define DW_OP_regx UINT8_C(0x90) /**< 1 operand, a ULEB128 register. */
342#define DW_OP_fbreg UINT8_C(0x91) /**< 1 operand, a SLEB128 offset. */
343#define DW_OP_bregx UINT8_C(0x92) /**< 2 operands, a ULEB128 register followed by a SLEB128 offset. */
344#define DW_OP_piece UINT8_C(0x93) /**< 1 operand, a ULEB128 size of piece addressed. */
345#define DW_OP_deref_size UINT8_C(0x94) /**< 1 operand, a 1-byte size of data retrieved. */
346#define DW_OP_xderef_size UINT8_C(0x95) /**< 1 operand, a 1-byte size of data retrieved. */
347#define DW_OP_nop UINT8_C(0x96) /**< 0 operands. */
348#define DW_OP_lo_user UINT8_C(0xe0) /**< First user opcode */
349#define DW_OP_hi_user UINT8_C(0xff) /**< Last user opcode. */
350/** @} */
351
352/** @name Exception Handler Pointer Encodings (GCC/LSB).
353 * @{ */
354#define DW_EH_PE_FORMAT_MASK UINT8_C(0x0f) /**< Format mask. */
355#define DW_EH_PE_APPL_MASK UINT8_C(0x70) /**< Application mask. */
356#define DW_EH_PE_indirect UINT8_C(0x80) /**< Flag: Indirect pointer. */
357#define DW_EH_PE_omit UINT8_C(0xff) /**< Special value: Omitted. */
358#define DW_EH_PE_ptr UINT8_C(0x00) /**< Format: pointer sized, unsigned. */
359#define DW_EH_PE_uleb128 UINT8_C(0x01) /**< Format: unsigned LEB128. */
360#define DW_EH_PE_udata2 UINT8_C(0x02) /**< Format: unsigned 16-bit. */
361#define DW_EH_PE_udata4 UINT8_C(0x03) /**< Format: unsigned 32-bit. */
362#define DW_EH_PE_udata8 UINT8_C(0x04) /**< Format: unsigned 64-bit. */
363#define DW_EH_PE_sleb128 UINT8_C(0x09) /**< Format: signed LEB128. */
364#define DW_EH_PE_sdata2 UINT8_C(0x0a) /**< Format: signed 16-bit. */
365#define DW_EH_PE_sdata4 UINT8_C(0x0b) /**< Format: signed 32-bit. */
366#define DW_EH_PE_sdata8 UINT8_C(0x0c) /**< Format: signed 64-bit. */
367#define DW_EH_PE_absptr UINT8_C(0x00) /**< Application: Absolute */
368#define DW_EH_PE_pcrel UINT8_C(0x10) /**< Application: PC relative, i.e. relative pointer address. */
369#define DW_EH_PE_textrel UINT8_C(0x20) /**< Application: text section relative. */
370#define DW_EH_PE_datarel UINT8_C(0x30) /**< Application: data section relative. */
371#define DW_EH_PE_funcrel UINT8_C(0x40) /**< Application: relative to start of function. */
372#define DW_EH_PE_aligned UINT8_C(0x50) /**< Application: aligned pointer. */
373/** @} */
374
375
376/*********************************************************************************************************************************
377* Structures and Typedefs *
378*********************************************************************************************************************************/
379/** Pointer to a DWARF section reader. */
380typedef struct RTDWARFCURSOR *PRTDWARFCURSOR;
381/** Pointer to an attribute descriptor. */
382typedef struct RTDWARFATTRDESC const *PCRTDWARFATTRDESC;
383/** Pointer to a DIE. */
384typedef struct RTDWARFDIE *PRTDWARFDIE;
385/** Pointer to a const DIE. */
386typedef struct RTDWARFDIE const *PCRTDWARFDIE;
387
388/**
389 * DWARF sections.
390 */
391typedef enum krtDbgModDwarfSect
392{
393 krtDbgModDwarfSect_abbrev = 0,
394 krtDbgModDwarfSect_aranges,
395 krtDbgModDwarfSect_frame,
396 krtDbgModDwarfSect_info,
397 krtDbgModDwarfSect_inlined,
398 krtDbgModDwarfSect_line,
399 krtDbgModDwarfSect_loc,
400 krtDbgModDwarfSect_macinfo,
401 krtDbgModDwarfSect_pubnames,
402 krtDbgModDwarfSect_pubtypes,
403 krtDbgModDwarfSect_ranges,
404 krtDbgModDwarfSect_str,
405 krtDbgModDwarfSect_types,
406 /** End of valid parts (exclusive). */
407 krtDbgModDwarfSect_End
408} krtDbgModDwarfSect;
409
410/**
411 * Abbreviation cache entry.
412 */
413typedef struct RTDWARFABBREV
414{
415 /** Whether there are children or not. */
416 bool fChildren;
417 /** The tag. */
418 uint16_t uTag;
419 /** Offset into the abbrev section of the specification pairs. */
420 uint32_t offSpec;
421 /** The abbreviation table offset this is entry is valid for.
422 * UINT32_MAX if not valid. */
423 uint32_t offAbbrev;
424} RTDWARFABBREV;
425/** Pointer to an abbreviation cache entry. */
426typedef RTDWARFABBREV *PRTDWARFABBREV;
427/** Pointer to a const abbreviation cache entry. */
428typedef RTDWARFABBREV const *PCRTDWARFABBREV;
429
430/**
431 * Structure for gathering segment info.
432 */
433typedef struct RTDBGDWARFSEG
434{
435 /** The highest offset in the segment. */
436 uint64_t offHighest;
437 /** Calculated base address. */
438 uint64_t uBaseAddr;
439 /** Estimated The segment size. */
440 uint64_t cbSegment;
441 /** Segment number (RTLDRSEG::Sel16bit). */
442 RTSEL uSegment;
443} RTDBGDWARFSEG;
444/** Pointer to segment info. */
445typedef RTDBGDWARFSEG *PRTDBGDWARFSEG;
446
447
448/**
449 * The instance data of the DWARF reader.
450 */
451typedef struct RTDBGMODDWARF
452{
453 /** The debug container containing doing the real work. */
454 RTDBGMOD hCnt;
455 /** The image module (no reference). */
456 PRTDBGMODINT pImgMod;
457 /** The debug info module (no reference). */
458 PRTDBGMODINT pDbgInfoMod;
459 /** Nested image module (with reference ofc). */
460 PRTDBGMODINT pNestedMod;
461
462 /** DWARF debug info sections. */
463 struct
464 {
465 /** The file offset of the part. */
466 RTFOFF offFile;
467 /** The size of the part. */
468 size_t cb;
469 /** The memory mapping of the part. */
470 void const *pv;
471 /** Set if present. */
472 bool fPresent;
473 /** The debug info ordinal number in the image file. */
474 uint32_t iDbgInfo;
475 } aSections[krtDbgModDwarfSect_End];
476
477 /** The offset into the abbreviation section of the current cache. */
478 uint32_t offCachedAbbrev;
479 /** The number of cached abbreviations we've allocated space for. */
480 uint32_t cCachedAbbrevsAlloced;
481 /** Array of cached abbreviations, indexed by code. */
482 PRTDWARFABBREV paCachedAbbrevs;
483 /** Used by rtDwarfAbbrev_Lookup when the result is uncachable. */
484 RTDWARFABBREV LookupAbbrev;
485
486 /** The list of compilation units (RTDWARFDIE). */
487 RTLISTANCHOR CompileUnitList;
488
489 /** Set if we have to use link addresses because the module does not have
490 * fixups (mach_kernel). */
491 bool fUseLinkAddress;
492 /** This is set to -1 if we're doing everything in one pass.
493 * Otherwise it's 1 or 2:
494 * - In pass 1, we collect segment info.
495 * - In pass 2, we add debug info to the container.
496 * The two pass parsing is necessary for watcom generated symbol files as
497 * these contains no information about the code and data segments in the
498 * image. So we have to figure out some approximate stuff based on the
499 * segments and offsets we encounter in the debug info. */
500 int8_t iWatcomPass;
501 /** Segment index hint. */
502 uint16_t iSegHint;
503 /** The number of segments in paSegs.
504 * (During segment copying, this is abused to count useful segments.) */
505 uint32_t cSegs;
506 /** Pointer to segments if iWatcomPass isn't -1. */
507 PRTDBGDWARFSEG paSegs;
508#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
509 /** DIE allocators. */
510 struct
511 {
512 RTMEMCACHE hMemCache;
513 uint32_t cbMax;
514 } aDieAllocators[2];
515#endif
516} RTDBGMODDWARF;
517/** Pointer to instance data of the DWARF reader. */
518typedef RTDBGMODDWARF *PRTDBGMODDWARF;
519
520/**
521 * DWARF cursor for reading byte data.
522 */
523typedef struct RTDWARFCURSOR
524{
525 /** The current position. */
526 uint8_t const *pb;
527 /** The number of bytes left to read. */
528 size_t cbLeft;
529 /** The number of bytes left to read in the current unit. */
530 size_t cbUnitLeft;
531 /** The DWARF debug info reader instance. (Can be NULL for eh_frame.) */
532 PRTDBGMODDWARF pDwarfMod;
533 /** Set if this is 64-bit DWARF, clear if 32-bit. */
534 bool f64bitDwarf;
535 /** Set if the format endian is native, clear if endian needs to be
536 * inverted. */
537 bool fNativEndian;
538 /** The size of a native address. */
539 uint8_t cbNativeAddr;
540 /** The cursor status code. This is VINF_SUCCESS until some error
541 * occurs. */
542 int rc;
543 /** The start of the area covered by the cursor.
544 * Used for repositioning the cursor relative to the start of a section. */
545 uint8_t const *pbStart;
546 /** The section. */
547 krtDbgModDwarfSect enmSect;
548} RTDWARFCURSOR;
549
550
551/**
552 * DWARF line number program state.
553 */
554typedef struct RTDWARFLINESTATE
555{
556 /** Virtual Line Number Machine Registers. */
557 struct
558 {
559 uint64_t uAddress;
560 uint64_t idxOp;
561 uint32_t iFile;
562 uint32_t uLine;
563 uint32_t uColumn;
564 bool fIsStatement;
565 bool fBasicBlock;
566 bool fEndSequence;
567 bool fPrologueEnd;
568 bool fEpilogueBegin;
569 uint32_t uIsa;
570 uint32_t uDiscriminator;
571 RTSEL uSegment;
572 } Regs;
573 /** @} */
574
575 /** Header. */
576 struct
577 {
578 uint32_t uVer;
579 uint64_t offFirstOpcode;
580 uint8_t cbMinInstr;
581 uint8_t cMaxOpsPerInstr;
582 uint8_t u8DefIsStmt;
583 int8_t s8LineBase;
584 uint8_t u8LineRange;
585 uint8_t u8OpcodeBase;
586 uint8_t const *pacStdOperands;
587 } Hdr;
588
589 /** @name Include Path Table (0-based)
590 * @{ */
591 const char **papszIncPaths;
592 uint32_t cIncPaths;
593 /** @} */
594
595 /** @name File Name Table (0-based, dummy zero entry)
596 * @{ */
597 char **papszFileNames;
598 uint32_t cFileNames;
599 /** @} */
600
601 /** The DWARF debug info reader instance. */
602 PRTDBGMODDWARF pDwarfMod;
603} RTDWARFLINESTATE;
604/** Pointer to a DWARF line number program state. */
605typedef RTDWARFLINESTATE *PRTDWARFLINESTATE;
606
607
608/**
609 * Decodes an attribute and stores it in the specified DIE member field.
610 *
611 * @returns IPRT status code.
612 * @param pDie Pointer to the DIE structure.
613 * @param pbMember Pointer to the first byte in the member.
614 * @param pDesc The attribute descriptor.
615 * @param uForm The data form.
616 * @param pCursor The cursor to read data from.
617 */
618typedef DECLCALLBACK(int) FNRTDWARFATTRDECODER(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
619 uint32_t uForm, PRTDWARFCURSOR pCursor);
620/** Pointer to an attribute decoder callback. */
621typedef FNRTDWARFATTRDECODER *PFNRTDWARFATTRDECODER;
622
623/**
624 * Attribute descriptor.
625 */
626typedef struct RTDWARFATTRDESC
627{
628 /** The attribute. */
629 uint16_t uAttr;
630 /** The data member offset. */
631 uint16_t off;
632 /** The data member size and initialization method. */
633 uint8_t cbInit;
634 uint8_t bPadding[3]; /**< Alignment padding. */
635 /** The decoder function. */
636 PFNRTDWARFATTRDECODER pfnDecoder;
637} RTDWARFATTRDESC;
638
639/** Define a attribute entry. */
640#define ATTR_ENTRY(a_uAttr, a_Struct, a_Member, a_Init, a_pfnDecoder) \
641 { \
642 a_uAttr, \
643 (uint16_t)RT_OFFSETOF(a_Struct, a_Member), \
644 a_Init | ((uint8_t)RT_SIZEOFMEMB(a_Struct, a_Member) & ATTR_SIZE_MASK), \
645 { 0, 0, 0 }, \
646 a_pfnDecoder\
647 }
648
649/** @name Attribute size and init methods.
650 * @{ */
651#define ATTR_INIT_ZERO UINT8_C(0x00)
652#define ATTR_INIT_FFFS UINT8_C(0x80)
653#define ATTR_INIT_MASK UINT8_C(0x80)
654#define ATTR_SIZE_MASK UINT8_C(0x3f)
655#define ATTR_GET_SIZE(a_pAttrDesc) ((a_pAttrDesc)->cbInit & ATTR_SIZE_MASK)
656/** @} */
657
658
659/**
660 * DIE descriptor.
661 */
662typedef struct RTDWARFDIEDESC
663{
664 /** The size of the DIE. */
665 size_t cbDie;
666 /** The number of attributes. */
667 size_t cAttributes;
668 /** Pointer to the array of attributes. */
669 PCRTDWARFATTRDESC paAttributes;
670} RTDWARFDIEDESC;
671typedef struct RTDWARFDIEDESC const *PCRTDWARFDIEDESC;
672/** DIE descriptor initializer. */
673#define DIE_DESC_INIT(a_Type, a_aAttrs) { sizeof(a_Type), RT_ELEMENTS(a_aAttrs), &a_aAttrs[0] }
674
675
676/**
677 * DIE core structure, all inherits (starts with) this.
678 */
679typedef struct RTDWARFDIE
680{
681 /** Pointer to the parent node. NULL if root unit. */
682 struct RTDWARFDIE *pParent;
683 /** Our node in the sibling list. */
684 RTLISTNODE SiblingNode;
685 /** List of children. */
686 RTLISTNODE ChildList;
687 /** The number of attributes successfully decoded. */
688 uint8_t cDecodedAttrs;
689 /** The number of unknown or otherwise unhandled attributes. */
690 uint8_t cUnhandledAttrs;
691#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
692 /** The allocator index. */
693 uint8_t iAllocator;
694#endif
695 /** The die tag, indicating which union structure to use. */
696 uint16_t uTag;
697 /** Offset of the abbreviation specification (within debug_abbrev). */
698 uint32_t offSpec;
699} RTDWARFDIE;
700
701
702/**
703 * DWARF address structure.
704 */
705typedef struct RTDWARFADDR
706{
707 /** The address. */
708 uint64_t uAddress;
709} RTDWARFADDR;
710typedef RTDWARFADDR *PRTDWARFADDR;
711typedef RTDWARFADDR const *PCRTDWARFADDR;
712
713
714/**
715 * DWARF address range.
716 */
717typedef struct RTDWARFADDRRANGE
718{
719 uint64_t uLowAddress;
720 uint64_t uHighAddress;
721 uint8_t const *pbRanges; /* ?? */
722 uint8_t cAttrs : 2;
723 uint8_t fHaveLowAddress : 1;
724 uint8_t fHaveHighAddress : 1;
725 uint8_t fHaveHighIsAddress : 1;
726 uint8_t fHaveRanges : 1;
727} RTDWARFADDRRANGE;
728typedef RTDWARFADDRRANGE *PRTDWARFADDRRANGE;
729typedef RTDWARFADDRRANGE const *PCRTDWARFADDRRANGE;
730
731/** What a RTDWARFREF is relative to. */
732typedef enum krtDwarfRef
733{
734 krtDwarfRef_NotSet,
735 krtDwarfRef_LineSection,
736 krtDwarfRef_LocSection,
737 krtDwarfRef_RangesSection,
738 krtDwarfRef_InfoSection,
739 krtDwarfRef_SameUnit,
740 krtDwarfRef_TypeId64
741} krtDwarfRef;
742
743/**
744 * DWARF reference.
745 */
746typedef struct RTDWARFREF
747{
748 /** The offset. */
749 uint64_t off;
750 /** What the offset is relative to. */
751 krtDwarfRef enmWrt;
752} RTDWARFREF;
753typedef RTDWARFREF *PRTDWARFREF;
754typedef RTDWARFREF const *PCRTDWARFREF;
755
756
757/**
758 * DWARF Location state.
759 */
760typedef struct RTDWARFLOCST
761{
762 /** The input cursor. */
763 RTDWARFCURSOR Cursor;
764 /** Points to the current top of the stack. Initial value -1. */
765 int32_t iTop;
766 /** The value stack. */
767 uint64_t auStack[64];
768} RTDWARFLOCST;
769/** Pointer to location state. */
770typedef RTDWARFLOCST *PRTDWARFLOCST;
771
772
773
774/*********************************************************************************************************************************
775* Internal Functions *
776*********************************************************************************************************************************/
777static FNRTDWARFATTRDECODER rtDwarfDecode_Address;
778static FNRTDWARFATTRDECODER rtDwarfDecode_Bool;
779static FNRTDWARFATTRDECODER rtDwarfDecode_LowHighPc;
780static FNRTDWARFATTRDECODER rtDwarfDecode_Ranges;
781static FNRTDWARFATTRDECODER rtDwarfDecode_Reference;
782static FNRTDWARFATTRDECODER rtDwarfDecode_SectOff;
783static FNRTDWARFATTRDECODER rtDwarfDecode_String;
784static FNRTDWARFATTRDECODER rtDwarfDecode_UnsignedInt;
785static FNRTDWARFATTRDECODER rtDwarfDecode_SegmentLoc;
786
787
788/*********************************************************************************************************************************
789* Global Variables *
790*********************************************************************************************************************************/
791/** RTDWARFDIE description. */
792static const RTDWARFDIEDESC g_CoreDieDesc = { sizeof(RTDWARFDIE), 0, NULL };
793
794
795/**
796 * DW_TAG_compile_unit & DW_TAG_partial_unit.
797 */
798typedef struct RTDWARFDIECOMPILEUNIT
799{
800 /** The DIE core structure. */
801 RTDWARFDIE Core;
802 /** The unit name. */
803 const char *pszName;
804 /** The address range of the code belonging to this unit. */
805 RTDWARFADDRRANGE PcRange;
806 /** The language name. */
807 uint16_t uLanguage;
808 /** The identifier case. */
809 uint8_t uIdentifierCase;
810 /** String are UTF-8 encoded. If not set, the encoding is
811 * unknown. */
812 bool fUseUtf8;
813 /** The unit contains main() or equivalent. */
814 bool fMainFunction;
815 /** The line numbers for this unit. */
816 RTDWARFREF StmtListRef;
817 /** The macro information for this unit. */
818 RTDWARFREF MacroInfoRef;
819 /** Reference to the base types. */
820 RTDWARFREF BaseTypesRef;
821 /** Working directory for the unit. */
822 const char *pszCurDir;
823 /** The name of the compiler or whatever that produced this unit. */
824 const char *pszProducer;
825
826 /** @name From the unit header.
827 * @{ */
828 /** The offset into debug_info of this unit (for references). */
829 uint64_t offUnit;
830 /** The length of this unit. */
831 uint64_t cbUnit;
832 /** The offset into debug_abbrev of the abbreviation for this unit. */
833 uint64_t offAbbrev;
834 /** The native address size. */
835 uint8_t cbNativeAddr;
836 /** The DWARF version. */
837 uint8_t uDwarfVer;
838 /** @} */
839} RTDWARFDIECOMPILEUNIT;
840typedef RTDWARFDIECOMPILEUNIT *PRTDWARFDIECOMPILEUNIT;
841
842
843/** RTDWARFDIECOMPILEUNIT attributes. */
844static const RTDWARFATTRDESC g_aCompileUnitAttrs[] =
845{
846 ATTR_ENTRY(DW_AT_name, RTDWARFDIECOMPILEUNIT, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
847 ATTR_ENTRY(DW_AT_low_pc, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
848 ATTR_ENTRY(DW_AT_high_pc, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
849 ATTR_ENTRY(DW_AT_ranges, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_Ranges),
850 ATTR_ENTRY(DW_AT_language, RTDWARFDIECOMPILEUNIT, uLanguage, ATTR_INIT_ZERO, rtDwarfDecode_UnsignedInt),
851 ATTR_ENTRY(DW_AT_macro_info, RTDWARFDIECOMPILEUNIT, MacroInfoRef, ATTR_INIT_ZERO, rtDwarfDecode_SectOff),
852 ATTR_ENTRY(DW_AT_stmt_list, RTDWARFDIECOMPILEUNIT, StmtListRef, ATTR_INIT_ZERO, rtDwarfDecode_SectOff),
853 ATTR_ENTRY(DW_AT_comp_dir, RTDWARFDIECOMPILEUNIT, pszCurDir, ATTR_INIT_ZERO, rtDwarfDecode_String),
854 ATTR_ENTRY(DW_AT_producer, RTDWARFDIECOMPILEUNIT, pszProducer, ATTR_INIT_ZERO, rtDwarfDecode_String),
855 ATTR_ENTRY(DW_AT_identifier_case, RTDWARFDIECOMPILEUNIT, uIdentifierCase,ATTR_INIT_ZERO, rtDwarfDecode_UnsignedInt),
856 ATTR_ENTRY(DW_AT_base_types, RTDWARFDIECOMPILEUNIT, BaseTypesRef, ATTR_INIT_ZERO, rtDwarfDecode_Reference),
857 ATTR_ENTRY(DW_AT_use_UTF8, RTDWARFDIECOMPILEUNIT, fUseUtf8, ATTR_INIT_ZERO, rtDwarfDecode_Bool),
858 ATTR_ENTRY(DW_AT_main_subprogram, RTDWARFDIECOMPILEUNIT, fMainFunction, ATTR_INIT_ZERO, rtDwarfDecode_Bool)
859};
860
861/** RTDWARFDIECOMPILEUNIT description. */
862static const RTDWARFDIEDESC g_CompileUnitDesc = DIE_DESC_INIT(RTDWARFDIECOMPILEUNIT, g_aCompileUnitAttrs);
863
864
865/**
866 * DW_TAG_subprogram.
867 */
868typedef struct RTDWARFDIESUBPROGRAM
869{
870 /** The DIE core structure. */
871 RTDWARFDIE Core;
872 /** The name. */
873 const char *pszName;
874 /** The linkage name. */
875 const char *pszLinkageName;
876 /** The address range of the code belonging to this unit. */
877 RTDWARFADDRRANGE PcRange;
878 /** The first instruction in the function. */
879 RTDWARFADDR EntryPc;
880 /** Segment number (watcom). */
881 RTSEL uSegment;
882 /** Reference to the specification. */
883 RTDWARFREF SpecRef;
884} RTDWARFDIESUBPROGRAM;
885/** Pointer to a DW_TAG_subprogram DIE. */
886typedef RTDWARFDIESUBPROGRAM *PRTDWARFDIESUBPROGRAM;
887/** Pointer to a const DW_TAG_subprogram DIE. */
888typedef RTDWARFDIESUBPROGRAM const *PCRTDWARFDIESUBPROGRAM;
889
890
891/** RTDWARFDIESUBPROGRAM attributes. */
892static const RTDWARFATTRDESC g_aSubProgramAttrs[] =
893{
894 ATTR_ENTRY(DW_AT_name, RTDWARFDIESUBPROGRAM, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
895 ATTR_ENTRY(DW_AT_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
896 ATTR_ENTRY(DW_AT_MIPS_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
897 ATTR_ENTRY(DW_AT_low_pc, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
898 ATTR_ENTRY(DW_AT_high_pc, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
899 ATTR_ENTRY(DW_AT_ranges, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_Ranges),
900 ATTR_ENTRY(DW_AT_entry_pc, RTDWARFDIESUBPROGRAM, EntryPc, ATTR_INIT_ZERO, rtDwarfDecode_Address),
901 ATTR_ENTRY(DW_AT_segment, RTDWARFDIESUBPROGRAM, uSegment, ATTR_INIT_ZERO, rtDwarfDecode_SegmentLoc),
902 ATTR_ENTRY(DW_AT_specification, RTDWARFDIESUBPROGRAM, SpecRef, ATTR_INIT_ZERO, rtDwarfDecode_Reference)
903};
904
905/** RTDWARFDIESUBPROGRAM description. */
906static const RTDWARFDIEDESC g_SubProgramDesc = DIE_DESC_INIT(RTDWARFDIESUBPROGRAM, g_aSubProgramAttrs);
907
908
909/** RTDWARFDIESUBPROGRAM attributes for the specification hack. */
910static const RTDWARFATTRDESC g_aSubProgramSpecHackAttrs[] =
911{
912 ATTR_ENTRY(DW_AT_name, RTDWARFDIESUBPROGRAM, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
913 ATTR_ENTRY(DW_AT_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
914 ATTR_ENTRY(DW_AT_MIPS_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
915};
916
917/** RTDWARFDIESUBPROGRAM description for the specification hack. */
918static const RTDWARFDIEDESC g_SubProgramSpecHackDesc = DIE_DESC_INIT(RTDWARFDIESUBPROGRAM, g_aSubProgramSpecHackAttrs);
919
920
921/**
922 * DW_TAG_label.
923 */
924typedef struct RTDWARFDIELABEL
925{
926 /** The DIE core structure. */
927 RTDWARFDIE Core;
928 /** The name. */
929 const char *pszName;
930 /** The address of the first instruction. */
931 RTDWARFADDR Address;
932 /** Segment number (watcom). */
933 RTSEL uSegment;
934 /** Externally visible? */
935 bool fExternal;
936} RTDWARFDIELABEL;
937/** Pointer to a DW_TAG_label DIE. */
938typedef RTDWARFDIELABEL *PRTDWARFDIELABEL;
939/** Pointer to a const DW_TAG_label DIE. */
940typedef RTDWARFDIELABEL const *PCRTDWARFDIELABEL;
941
942
943/** RTDWARFDIESUBPROGRAM attributes. */
944static const RTDWARFATTRDESC g_aLabelAttrs[] =
945{
946 ATTR_ENTRY(DW_AT_name, RTDWARFDIELABEL, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
947 ATTR_ENTRY(DW_AT_low_pc, RTDWARFDIELABEL, Address, ATTR_INIT_ZERO, rtDwarfDecode_Address),
948 ATTR_ENTRY(DW_AT_segment, RTDWARFDIELABEL, uSegment, ATTR_INIT_ZERO, rtDwarfDecode_SegmentLoc),
949 ATTR_ENTRY(DW_AT_external, RTDWARFDIELABEL, fExternal, ATTR_INIT_ZERO, rtDwarfDecode_Bool)
950};
951
952/** RTDWARFDIESUBPROGRAM description. */
953static const RTDWARFDIEDESC g_LabelDesc = DIE_DESC_INIT(RTDWARFDIELABEL, g_aLabelAttrs);
954
955
956/**
957 * Tag names and descriptors.
958 */
959static const struct RTDWARFTAGDESC
960{
961 /** The tag value. */
962 uint16_t uTag;
963 /** The tag name as string. */
964 const char *pszName;
965 /** The DIE descriptor to use. */
966 PCRTDWARFDIEDESC pDesc;
967} g_aTagDescs[] =
968{
969#define TAGDESC(a_Name, a_pDesc) { DW_ ## a_Name, #a_Name, a_pDesc }
970#define TAGDESC_EMPTY() { 0, NULL, NULL }
971#define TAGDESC_CORE(a_Name) TAGDESC(a_Name, &g_CoreDieDesc)
972 TAGDESC_EMPTY(), /* 0x00 */
973 TAGDESC_CORE(TAG_array_type),
974 TAGDESC_CORE(TAG_class_type),
975 TAGDESC_CORE(TAG_entry_point),
976 TAGDESC_CORE(TAG_enumeration_type), /* 0x04 */
977 TAGDESC_CORE(TAG_formal_parameter),
978 TAGDESC_EMPTY(),
979 TAGDESC_EMPTY(),
980 TAGDESC_CORE(TAG_imported_declaration), /* 0x08 */
981 TAGDESC_EMPTY(),
982 TAGDESC(TAG_label, &g_LabelDesc),
983 TAGDESC_CORE(TAG_lexical_block),
984 TAGDESC_EMPTY(), /* 0x0c */
985 TAGDESC_CORE(TAG_member),
986 TAGDESC_EMPTY(),
987 TAGDESC_CORE(TAG_pointer_type),
988 TAGDESC_CORE(TAG_reference_type), /* 0x10 */
989 TAGDESC_CORE(TAG_compile_unit),
990 TAGDESC_CORE(TAG_string_type),
991 TAGDESC_CORE(TAG_structure_type),
992 TAGDESC_EMPTY(), /* 0x14 */
993 TAGDESC_CORE(TAG_subroutine_type),
994 TAGDESC_CORE(TAG_typedef),
995 TAGDESC_CORE(TAG_union_type),
996 TAGDESC_CORE(TAG_unspecified_parameters), /* 0x18 */
997 TAGDESC_CORE(TAG_variant),
998 TAGDESC_CORE(TAG_common_block),
999 TAGDESC_CORE(TAG_common_inclusion),
1000 TAGDESC_CORE(TAG_inheritance), /* 0x1c */
1001 TAGDESC_CORE(TAG_inlined_subroutine),
1002 TAGDESC_CORE(TAG_module),
1003 TAGDESC_CORE(TAG_ptr_to_member_type),
1004 TAGDESC_CORE(TAG_set_type), /* 0x20 */
1005 TAGDESC_CORE(TAG_subrange_type),
1006 TAGDESC_CORE(TAG_with_stmt),
1007 TAGDESC_CORE(TAG_access_declaration),
1008 TAGDESC_CORE(TAG_base_type), /* 0x24 */
1009 TAGDESC_CORE(TAG_catch_block),
1010 TAGDESC_CORE(TAG_const_type),
1011 TAGDESC_CORE(TAG_constant),
1012 TAGDESC_CORE(TAG_enumerator), /* 0x28 */
1013 TAGDESC_CORE(TAG_file_type),
1014 TAGDESC_CORE(TAG_friend),
1015 TAGDESC_CORE(TAG_namelist),
1016 TAGDESC_CORE(TAG_namelist_item), /* 0x2c */
1017 TAGDESC_CORE(TAG_packed_type),
1018 TAGDESC(TAG_subprogram, &g_SubProgramDesc),
1019 TAGDESC_CORE(TAG_template_type_parameter),
1020 TAGDESC_CORE(TAG_template_value_parameter), /* 0x30 */
1021 TAGDESC_CORE(TAG_thrown_type),
1022 TAGDESC_CORE(TAG_try_block),
1023 TAGDESC_CORE(TAG_variant_part),
1024 TAGDESC_CORE(TAG_variable), /* 0x34 */
1025 TAGDESC_CORE(TAG_volatile_type),
1026 TAGDESC_CORE(TAG_dwarf_procedure),
1027 TAGDESC_CORE(TAG_restrict_type),
1028 TAGDESC_CORE(TAG_interface_type), /* 0x38 */
1029 TAGDESC_CORE(TAG_namespace),
1030 TAGDESC_CORE(TAG_imported_module),
1031 TAGDESC_CORE(TAG_unspecified_type),
1032 TAGDESC_CORE(TAG_partial_unit), /* 0x3c */
1033 TAGDESC_CORE(TAG_imported_unit),
1034 TAGDESC_EMPTY(),
1035 TAGDESC_CORE(TAG_condition),
1036 TAGDESC_CORE(TAG_shared_type), /* 0x40 */
1037 TAGDESC_CORE(TAG_type_unit),
1038 TAGDESC_CORE(TAG_rvalue_reference_type),
1039 TAGDESC_CORE(TAG_template_alias)
1040#undef TAGDESC
1041#undef TAGDESC_EMPTY
1042#undef TAGDESC_CORE
1043};
1044
1045
1046/*********************************************************************************************************************************
1047* Internal Functions *
1048*********************************************************************************************************************************/
1049static int rtDwarfInfo_ParseDie(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc,
1050 PRTDWARFCURSOR pCursor, PCRTDWARFABBREV pAbbrev, bool fInitDie);
1051
1052
1053
1054#if defined(LOG_ENABLED) || defined(RT_STRICT)
1055
1056# if 0 /* unused */
1057/**
1058 * Turns a tag value into a string for logging purposes.
1059 *
1060 * @returns String name.
1061 * @param uTag The tag.
1062 */
1063static const char *rtDwarfLog_GetTagName(uint32_t uTag)
1064{
1065 if (uTag < RT_ELEMENTS(g_aTagDescs))
1066 {
1067 const char *pszTag = g_aTagDescs[uTag].pszName;
1068 if (pszTag)
1069 return pszTag;
1070 }
1071
1072 static char s_szStatic[32];
1073 RTStrPrintf(s_szStatic, sizeof(s_szStatic),"DW_TAG_%#x", uTag);
1074 return s_szStatic;
1075}
1076# endif
1077
1078
1079/**
1080 * Turns an attributevalue into a string for logging purposes.
1081 *
1082 * @returns String name.
1083 * @param uAttr The attribute.
1084 */
1085static const char *rtDwarfLog_AttrName(uint32_t uAttr)
1086{
1087 switch (uAttr)
1088 {
1089 RT_CASE_RET_STR(DW_AT_sibling);
1090 RT_CASE_RET_STR(DW_AT_location);
1091 RT_CASE_RET_STR(DW_AT_name);
1092 RT_CASE_RET_STR(DW_AT_ordering);
1093 RT_CASE_RET_STR(DW_AT_byte_size);
1094 RT_CASE_RET_STR(DW_AT_bit_offset);
1095 RT_CASE_RET_STR(DW_AT_bit_size);
1096 RT_CASE_RET_STR(DW_AT_stmt_list);
1097 RT_CASE_RET_STR(DW_AT_low_pc);
1098 RT_CASE_RET_STR(DW_AT_high_pc);
1099 RT_CASE_RET_STR(DW_AT_language);
1100 RT_CASE_RET_STR(DW_AT_discr);
1101 RT_CASE_RET_STR(DW_AT_discr_value);
1102 RT_CASE_RET_STR(DW_AT_visibility);
1103 RT_CASE_RET_STR(DW_AT_import);
1104 RT_CASE_RET_STR(DW_AT_string_length);
1105 RT_CASE_RET_STR(DW_AT_common_reference);
1106 RT_CASE_RET_STR(DW_AT_comp_dir);
1107 RT_CASE_RET_STR(DW_AT_const_value);
1108 RT_CASE_RET_STR(DW_AT_containing_type);
1109 RT_CASE_RET_STR(DW_AT_default_value);
1110 RT_CASE_RET_STR(DW_AT_inline);
1111 RT_CASE_RET_STR(DW_AT_is_optional);
1112 RT_CASE_RET_STR(DW_AT_lower_bound);
1113 RT_CASE_RET_STR(DW_AT_producer);
1114 RT_CASE_RET_STR(DW_AT_prototyped);
1115 RT_CASE_RET_STR(DW_AT_return_addr);
1116 RT_CASE_RET_STR(DW_AT_start_scope);
1117 RT_CASE_RET_STR(DW_AT_bit_stride);
1118 RT_CASE_RET_STR(DW_AT_upper_bound);
1119 RT_CASE_RET_STR(DW_AT_abstract_origin);
1120 RT_CASE_RET_STR(DW_AT_accessibility);
1121 RT_CASE_RET_STR(DW_AT_address_class);
1122 RT_CASE_RET_STR(DW_AT_artificial);
1123 RT_CASE_RET_STR(DW_AT_base_types);
1124 RT_CASE_RET_STR(DW_AT_calling_convention);
1125 RT_CASE_RET_STR(DW_AT_count);
1126 RT_CASE_RET_STR(DW_AT_data_member_location);
1127 RT_CASE_RET_STR(DW_AT_decl_column);
1128 RT_CASE_RET_STR(DW_AT_decl_file);
1129 RT_CASE_RET_STR(DW_AT_decl_line);
1130 RT_CASE_RET_STR(DW_AT_declaration);
1131 RT_CASE_RET_STR(DW_AT_discr_list);
1132 RT_CASE_RET_STR(DW_AT_encoding);
1133 RT_CASE_RET_STR(DW_AT_external);
1134 RT_CASE_RET_STR(DW_AT_frame_base);
1135 RT_CASE_RET_STR(DW_AT_friend);
1136 RT_CASE_RET_STR(DW_AT_identifier_case);
1137 RT_CASE_RET_STR(DW_AT_macro_info);
1138 RT_CASE_RET_STR(DW_AT_namelist_item);
1139 RT_CASE_RET_STR(DW_AT_priority);
1140 RT_CASE_RET_STR(DW_AT_segment);
1141 RT_CASE_RET_STR(DW_AT_specification);
1142 RT_CASE_RET_STR(DW_AT_static_link);
1143 RT_CASE_RET_STR(DW_AT_type);
1144 RT_CASE_RET_STR(DW_AT_use_location);
1145 RT_CASE_RET_STR(DW_AT_variable_parameter);
1146 RT_CASE_RET_STR(DW_AT_virtuality);
1147 RT_CASE_RET_STR(DW_AT_vtable_elem_location);
1148 RT_CASE_RET_STR(DW_AT_allocated);
1149 RT_CASE_RET_STR(DW_AT_associated);
1150 RT_CASE_RET_STR(DW_AT_data_location);
1151 RT_CASE_RET_STR(DW_AT_byte_stride);
1152 RT_CASE_RET_STR(DW_AT_entry_pc);
1153 RT_CASE_RET_STR(DW_AT_use_UTF8);
1154 RT_CASE_RET_STR(DW_AT_extension);
1155 RT_CASE_RET_STR(DW_AT_ranges);
1156 RT_CASE_RET_STR(DW_AT_trampoline);
1157 RT_CASE_RET_STR(DW_AT_call_column);
1158 RT_CASE_RET_STR(DW_AT_call_file);
1159 RT_CASE_RET_STR(DW_AT_call_line);
1160 RT_CASE_RET_STR(DW_AT_description);
1161 RT_CASE_RET_STR(DW_AT_binary_scale);
1162 RT_CASE_RET_STR(DW_AT_decimal_scale);
1163 RT_CASE_RET_STR(DW_AT_small);
1164 RT_CASE_RET_STR(DW_AT_decimal_sign);
1165 RT_CASE_RET_STR(DW_AT_digit_count);
1166 RT_CASE_RET_STR(DW_AT_picture_string);
1167 RT_CASE_RET_STR(DW_AT_mutable);
1168 RT_CASE_RET_STR(DW_AT_threads_scaled);
1169 RT_CASE_RET_STR(DW_AT_explicit);
1170 RT_CASE_RET_STR(DW_AT_object_pointer);
1171 RT_CASE_RET_STR(DW_AT_endianity);
1172 RT_CASE_RET_STR(DW_AT_elemental);
1173 RT_CASE_RET_STR(DW_AT_pure);
1174 RT_CASE_RET_STR(DW_AT_recursive);
1175 RT_CASE_RET_STR(DW_AT_signature);
1176 RT_CASE_RET_STR(DW_AT_main_subprogram);
1177 RT_CASE_RET_STR(DW_AT_data_bit_offset);
1178 RT_CASE_RET_STR(DW_AT_const_expr);
1179 RT_CASE_RET_STR(DW_AT_enum_class);
1180 RT_CASE_RET_STR(DW_AT_linkage_name);
1181 RT_CASE_RET_STR(DW_AT_MIPS_linkage_name);
1182 }
1183 static char s_szStatic[32];
1184 RTStrPrintf(s_szStatic, sizeof(s_szStatic),"DW_AT_%#x", uAttr);
1185 return s_szStatic;
1186}
1187
1188
1189/**
1190 * Turns a form value into a string for logging purposes.
1191 *
1192 * @returns String name.
1193 * @param uForm The form.
1194 */
1195static const char *rtDwarfLog_FormName(uint32_t uForm)
1196{
1197 switch (uForm)
1198 {
1199 RT_CASE_RET_STR(DW_FORM_addr);
1200 RT_CASE_RET_STR(DW_FORM_block2);
1201 RT_CASE_RET_STR(DW_FORM_block4);
1202 RT_CASE_RET_STR(DW_FORM_data2);
1203 RT_CASE_RET_STR(DW_FORM_data4);
1204 RT_CASE_RET_STR(DW_FORM_data8);
1205 RT_CASE_RET_STR(DW_FORM_string);
1206 RT_CASE_RET_STR(DW_FORM_block);
1207 RT_CASE_RET_STR(DW_FORM_block1);
1208 RT_CASE_RET_STR(DW_FORM_data1);
1209 RT_CASE_RET_STR(DW_FORM_flag);
1210 RT_CASE_RET_STR(DW_FORM_sdata);
1211 RT_CASE_RET_STR(DW_FORM_strp);
1212 RT_CASE_RET_STR(DW_FORM_udata);
1213 RT_CASE_RET_STR(DW_FORM_ref_addr);
1214 RT_CASE_RET_STR(DW_FORM_ref1);
1215 RT_CASE_RET_STR(DW_FORM_ref2);
1216 RT_CASE_RET_STR(DW_FORM_ref4);
1217 RT_CASE_RET_STR(DW_FORM_ref8);
1218 RT_CASE_RET_STR(DW_FORM_ref_udata);
1219 RT_CASE_RET_STR(DW_FORM_indirect);
1220 RT_CASE_RET_STR(DW_FORM_sec_offset);
1221 RT_CASE_RET_STR(DW_FORM_exprloc);
1222 RT_CASE_RET_STR(DW_FORM_flag_present);
1223 RT_CASE_RET_STR(DW_FORM_ref_sig8);
1224 }
1225 static char s_szStatic[32];
1226 RTStrPrintf(s_szStatic, sizeof(s_szStatic),"DW_FORM_%#x", uForm);
1227 return s_szStatic;
1228}
1229
1230#endif /* LOG_ENABLED || RT_STRICT */
1231
1232
1233
1234/** @callback_method_impl{FNRTLDRENUMSEGS} */
1235static DECLCALLBACK(int) rtDbgModDwarfScanSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
1236{
1237 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
1238 Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
1239 pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
1240 NOREF(hLdrMod);
1241
1242 /* Count relevant segments. */
1243 if (pSeg->RVA != NIL_RTLDRADDR)
1244 pThis->cSegs++;
1245
1246 return VINF_SUCCESS;
1247}
1248
1249
1250/** @callback_method_impl{FNRTLDRENUMSEGS} */
1251static DECLCALLBACK(int) rtDbgModDwarfAddSegmentsCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
1252{
1253 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
1254 Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx cbMapped=%#llx\n",
1255 pSeg->cchName, pSeg->pszName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb, pSeg->cbMapped));
1256 NOREF(hLdrMod);
1257 Assert(pSeg->cchName > 0);
1258 Assert(!pSeg->pszName[pSeg->cchName]);
1259
1260 /* If the segment doesn't have a mapping, just add a dummy so the indexing
1261 works out correctly (same as for the image). */
1262 if (pSeg->RVA == NIL_RTLDRADDR)
1263 return RTDbgModSegmentAdd(pThis->hCnt, 0, 0, pSeg->pszName, 0 /*fFlags*/, NULL);
1264
1265 /* The link address is 0 for all segments in a relocatable ELF image. */
1266 RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
1267 return RTDbgModSegmentAdd(pThis->hCnt, pSeg->RVA, cb, pSeg->pszName, 0 /*fFlags*/, NULL);
1268}
1269
1270
1271/**
1272 * Calls pfnSegmentAdd for each segment in the executable image.
1273 *
1274 * @returns IPRT status code.
1275 * @param pThis The DWARF instance.
1276 */
1277static int rtDbgModDwarfAddSegmentsFromImage(PRTDBGMODDWARF pThis)
1278{
1279 AssertReturn(pThis->pImgMod && pThis->pImgMod->pImgVt, VERR_INTERNAL_ERROR_2);
1280 Assert(!pThis->cSegs);
1281 int rc = pThis->pImgMod->pImgVt->pfnEnumSegments(pThis->pImgMod, rtDbgModDwarfScanSegmentsCallback, pThis);
1282 if (RT_SUCCESS(rc))
1283 {
1284 if (pThis->cSegs == 0)
1285 pThis->iWatcomPass = 1;
1286 else
1287 {
1288 pThis->cSegs = 0;
1289 pThis->iWatcomPass = -1;
1290 rc = pThis->pImgMod->pImgVt->pfnEnumSegments(pThis->pImgMod, rtDbgModDwarfAddSegmentsCallback, pThis);
1291 }
1292 }
1293
1294 return rc;
1295}
1296
1297
1298/**
1299 * Looks up a segment.
1300 *
1301 * @returns Pointer to the segment on success, NULL if not found.
1302 * @param pThis The DWARF instance.
1303 * @param uSeg The segment number / selector.
1304 */
1305static PRTDBGDWARFSEG rtDbgModDwarfFindSegment(PRTDBGMODDWARF pThis, RTSEL uSeg)
1306{
1307 uint32_t cSegs = pThis->cSegs;
1308 uint32_t iSeg = pThis->iSegHint;
1309 PRTDBGDWARFSEG paSegs = pThis->paSegs;
1310 if ( iSeg < cSegs
1311 && paSegs[iSeg].uSegment == uSeg)
1312 return &paSegs[iSeg];
1313
1314 for (iSeg = 0; iSeg < cSegs; iSeg++)
1315 if (uSeg == paSegs[iSeg].uSegment)
1316 {
1317 pThis->iSegHint = iSeg;
1318 return &paSegs[iSeg];
1319 }
1320
1321 AssertFailed();
1322 return NULL;
1323}
1324
1325
1326/**
1327 * Record a segment:offset during pass 1.
1328 *
1329 * @returns IPRT status code.
1330 * @param pThis The DWARF instance.
1331 * @param uSeg The segment number / selector.
1332 * @param offSeg The segment offset.
1333 */
1334static int rtDbgModDwarfRecordSegOffset(PRTDBGMODDWARF pThis, RTSEL uSeg, uint64_t offSeg)
1335{
1336 /* Look up the segment. */
1337 uint32_t cSegs = pThis->cSegs;
1338 uint32_t iSeg = pThis->iSegHint;
1339 PRTDBGDWARFSEG paSegs = pThis->paSegs;
1340 if ( iSeg >= cSegs
1341 || paSegs[iSeg].uSegment != uSeg)
1342 {
1343 for (iSeg = 0; iSeg < cSegs; iSeg++)
1344 if (uSeg <= paSegs[iSeg].uSegment)
1345 break;
1346 if ( iSeg >= cSegs
1347 || paSegs[iSeg].uSegment != uSeg)
1348 {
1349 /* Add */
1350 void *pvNew = RTMemRealloc(paSegs, (pThis->cSegs + 1) * sizeof(paSegs[0]));
1351 if (!pvNew)
1352 return VERR_NO_MEMORY;
1353 pThis->paSegs = paSegs = (PRTDBGDWARFSEG)pvNew;
1354 if (iSeg != cSegs)
1355 memmove(&paSegs[iSeg + 1], &paSegs[iSeg], (cSegs - iSeg) * sizeof(paSegs[0]));
1356 paSegs[iSeg].offHighest = offSeg;
1357 paSegs[iSeg].uBaseAddr = 0;
1358 paSegs[iSeg].cbSegment = 0;
1359 paSegs[iSeg].uSegment = uSeg;
1360 pThis->cSegs++;
1361 }
1362
1363 pThis->iSegHint = iSeg;
1364 }
1365
1366 /* Increase it's range? */
1367 if (paSegs[iSeg].offHighest < offSeg)
1368 {
1369 Log3(("rtDbgModDwarfRecordSegOffset: iSeg=%d uSeg=%#06x offSeg=%#llx\n", iSeg, uSeg, offSeg));
1370 paSegs[iSeg].offHighest = offSeg;
1371 }
1372
1373 return VINF_SUCCESS;
1374}
1375
1376
1377/**
1378 * Calls pfnSegmentAdd for each segment in the executable image.
1379 *
1380 * @returns IPRT status code.
1381 * @param pThis The DWARF instance.
1382 */
1383static int rtDbgModDwarfAddSegmentsFromPass1(PRTDBGMODDWARF pThis)
1384{
1385 AssertReturn(pThis->cSegs, VERR_DWARF_BAD_INFO);
1386 uint32_t const cSegs = pThis->cSegs;
1387 PRTDBGDWARFSEG paSegs = pThis->paSegs;
1388
1389 /*
1390 * Are the segments assigned more or less in numerical order?
1391 */
1392 if ( paSegs[0].uSegment < 16U
1393 && paSegs[cSegs - 1].uSegment - paSegs[0].uSegment + 1U <= cSegs + 16U)
1394 {
1395 /** @todo heuristics, plase. */
1396 AssertFailedReturn(VERR_DWARF_TODO);
1397
1398 }
1399 /*
1400 * Assume DOS segmentation.
1401 */
1402 else
1403 {
1404 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
1405 paSegs[iSeg].uBaseAddr = (uint32_t)paSegs[iSeg].uSegment << 16;
1406 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
1407 paSegs[iSeg].cbSegment = paSegs[iSeg].offHighest;
1408 }
1409
1410 /*
1411 * Add them.
1412 */
1413 for (uint32_t iSeg = 0; iSeg < cSegs; iSeg++)
1414 {
1415 Log3(("rtDbgModDwarfAddSegmentsFromPass1: Seg#%u: %#010llx LB %#llx uSegment=%#x\n",
1416 iSeg, paSegs[iSeg].uBaseAddr, paSegs[iSeg].cbSegment, paSegs[iSeg].uSegment));
1417 char szName[32];
1418 RTStrPrintf(szName, sizeof(szName), "seg-%#04xh", paSegs[iSeg].uSegment);
1419 int rc = RTDbgModSegmentAdd(pThis->hCnt, paSegs[iSeg].uBaseAddr, paSegs[iSeg].cbSegment,
1420 szName, 0 /*fFlags*/, NULL);
1421 if (RT_FAILURE(rc))
1422 return rc;
1423 }
1424
1425 return VINF_SUCCESS;
1426}
1427
1428
1429/**
1430 * Loads a DWARF section from the image file.
1431 *
1432 * @returns IPRT status code.
1433 * @param pThis The DWARF instance.
1434 * @param enmSect The section to load.
1435 */
1436static int rtDbgModDwarfLoadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
1437{
1438 /*
1439 * Don't load stuff twice.
1440 */
1441 if (pThis->aSections[enmSect].pv)
1442 return VINF_SUCCESS;
1443
1444 /*
1445 * Sections that are not present cannot be loaded, treat them like they
1446 * are empty
1447 */
1448 if (!pThis->aSections[enmSect].fPresent)
1449 {
1450 Assert(pThis->aSections[enmSect].cb);
1451 return VINF_SUCCESS;
1452 }
1453 if (!pThis->aSections[enmSect].cb)
1454 return VINF_SUCCESS;
1455
1456 /*
1457 * Sections must be readable with the current image interface.
1458 */
1459 if (pThis->aSections[enmSect].offFile < 0)
1460 return VERR_OUT_OF_RANGE;
1461
1462 /*
1463 * Do the job.
1464 */
1465 return pThis->pDbgInfoMod->pImgVt->pfnMapPart(pThis->pDbgInfoMod,
1466 pThis->aSections[enmSect].iDbgInfo,
1467 pThis->aSections[enmSect].offFile,
1468 pThis->aSections[enmSect].cb,
1469 &pThis->aSections[enmSect].pv);
1470}
1471
1472
1473#ifdef SOME_UNUSED_FUNCTION
1474/**
1475 * Unloads a DWARF section previously mapped by rtDbgModDwarfLoadSection.
1476 *
1477 * @returns IPRT status code.
1478 * @param pThis The DWARF instance.
1479 * @param enmSect The section to unload.
1480 */
1481static int rtDbgModDwarfUnloadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
1482{
1483 if (!pThis->aSections[enmSect].pv)
1484 return VINF_SUCCESS;
1485
1486 int rc = pThis->pDbgInfoMod->pImgVt->pfnUnmapPart(pThis->pDbgInfoMod, pThis->aSections[enmSect].cb, &pThis->aSections[enmSect].pv);
1487 AssertRC(rc);
1488 return rc;
1489}
1490#endif
1491
1492
1493/**
1494 * Converts to UTF-8 or otherwise makes sure it's valid UTF-8.
1495 *
1496 * @returns IPRT status code.
1497 * @param pThis The DWARF instance.
1498 * @param ppsz Pointer to the string pointer. May be
1499 * reallocated (RTStr*).
1500 */
1501static int rtDbgModDwarfStringToUtf8(PRTDBGMODDWARF pThis, char **ppsz)
1502{
1503 /** @todo DWARF & UTF-8. */
1504 NOREF(pThis);
1505 RTStrPurgeEncoding(*ppsz);
1506 return VINF_SUCCESS;
1507}
1508
1509
1510/**
1511 * Convers a link address into a segment+offset or RVA.
1512 *
1513 * @returns IPRT status code.
1514 * @param pThis The DWARF instance.
1515 * @param uSegment The segment, 0 if not applicable.
1516 * @param LinkAddress The address to convert..
1517 * @param piSeg The segment index.
1518 * @param poffSeg Where to return the segment offset.
1519 */
1520static int rtDbgModDwarfLinkAddressToSegOffset(PRTDBGMODDWARF pThis, RTSEL uSegment, uint64_t LinkAddress,
1521 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
1522{
1523 if (pThis->paSegs)
1524 {
1525 PRTDBGDWARFSEG pSeg = rtDbgModDwarfFindSegment(pThis, uSegment);
1526 if (pSeg)
1527 {
1528 *piSeg = pSeg - pThis->paSegs;
1529 *poffSeg = LinkAddress;
1530 return VINF_SUCCESS;
1531 }
1532 }
1533
1534 if (pThis->fUseLinkAddress)
1535 return pThis->pImgMod->pImgVt->pfnLinkAddressToSegOffset(pThis->pImgMod, LinkAddress, piSeg, poffSeg);
1536 return pThis->pImgMod->pImgVt->pfnRvaToSegOffset(pThis->pImgMod, LinkAddress, piSeg, poffSeg);
1537}
1538
1539
1540/**
1541 * Converts a segment+offset address into an RVA.
1542 *
1543 * @returns IPRT status code.
1544 * @param pThis The DWARF instance.
1545 * @param idxSegment The segment index.
1546 * @param offSegment The segment offset.
1547 * @param puRva Where to return the calculated RVA.
1548 */
1549static int rtDbgModDwarfSegOffsetToRva(PRTDBGMODDWARF pThis, RTDBGSEGIDX idxSegment, uint64_t offSegment, PRTUINTPTR puRva)
1550{
1551 if (pThis->paSegs)
1552 {
1553 PRTDBGDWARFSEG pSeg = rtDbgModDwarfFindSegment(pThis, idxSegment);
1554 if (pSeg)
1555 {
1556 *puRva = pSeg->uBaseAddr + offSegment;
1557 return VINF_SUCCESS;
1558 }
1559 }
1560
1561 RTUINTPTR uRva = RTDbgModSegmentRva(pThis->pImgMod, idxSegment);
1562 if (uRva != RTUINTPTR_MAX)
1563 {
1564 *puRva = uRva + offSegment;
1565 return VINF_SUCCESS;
1566 }
1567 return VERR_INVALID_POINTER;
1568}
1569
1570/**
1571 * Converts a segment+offset address into an RVA.
1572 *
1573 * @returns IPRT status code.
1574 * @param pThis The DWARF instance.
1575 * @param uRva The RVA to convert.
1576 * @param pidxSegment Where to return the segment index.
1577 * @param poffSegment Where to return the segment offset.
1578 */
1579static int rtDbgModDwarfRvaToSegOffset(PRTDBGMODDWARF pThis, RTUINTPTR uRva, RTDBGSEGIDX *pidxSegment, uint64_t *poffSegment)
1580{
1581 RTUINTPTR offSeg = 0;
1582 RTDBGSEGIDX idxSeg = RTDbgModRvaToSegOff(pThis->pImgMod, uRva, &offSeg);
1583 if (idxSeg != NIL_RTDBGSEGIDX)
1584 {
1585 *pidxSegment = idxSeg;
1586 *poffSegment = offSeg;
1587 return VINF_SUCCESS;
1588 }
1589 return VERR_INVALID_POINTER;
1590}
1591
1592
1593
1594/*
1595 *
1596 * DWARF Cursor.
1597 * DWARF Cursor.
1598 * DWARF Cursor.
1599 *
1600 */
1601
1602
1603/**
1604 * Reads a 8-bit unsigned integer and advances the cursor.
1605 *
1606 * @returns 8-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
1607 * uErrValue is returned.
1608 * @param pCursor The cursor.
1609 * @param uErrValue What to return on read error.
1610 */
1611static uint8_t rtDwarfCursor_GetU8(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
1612{
1613 if (pCursor->cbUnitLeft < 1)
1614 {
1615 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1616 return uErrValue;
1617 }
1618
1619 uint8_t u8 = pCursor->pb[0];
1620 pCursor->pb += 1;
1621 pCursor->cbUnitLeft -= 1;
1622 pCursor->cbLeft -= 1;
1623 return u8;
1624}
1625
1626
1627/**
1628 * Reads a 16-bit unsigned integer and advances the cursor.
1629 *
1630 * @returns 16-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
1631 * uErrValue is returned.
1632 * @param pCursor The cursor.
1633 * @param uErrValue What to return on read error.
1634 */
1635static uint16_t rtDwarfCursor_GetU16(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
1636{
1637 if (pCursor->cbUnitLeft < 2)
1638 {
1639 pCursor->pb += pCursor->cbUnitLeft;
1640 pCursor->cbLeft -= pCursor->cbUnitLeft;
1641 pCursor->cbUnitLeft = 0;
1642 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1643 return uErrValue;
1644 }
1645
1646 uint16_t u16 = RT_MAKE_U16(pCursor->pb[0], pCursor->pb[1]);
1647 pCursor->pb += 2;
1648 pCursor->cbUnitLeft -= 2;
1649 pCursor->cbLeft -= 2;
1650 if (!pCursor->fNativEndian)
1651 u16 = RT_BSWAP_U16(u16);
1652 return u16;
1653}
1654
1655
1656/**
1657 * Reads a 32-bit unsigned integer and advances the cursor.
1658 *
1659 * @returns 32-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
1660 * uErrValue is returned.
1661 * @param pCursor The cursor.
1662 * @param uErrValue What to return on read error.
1663 */
1664static uint32_t rtDwarfCursor_GetU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
1665{
1666 if (pCursor->cbUnitLeft < 4)
1667 {
1668 pCursor->pb += pCursor->cbUnitLeft;
1669 pCursor->cbLeft -= pCursor->cbUnitLeft;
1670 pCursor->cbUnitLeft = 0;
1671 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1672 return uErrValue;
1673 }
1674
1675 uint32_t u32 = RT_MAKE_U32_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3]);
1676 pCursor->pb += 4;
1677 pCursor->cbUnitLeft -= 4;
1678 pCursor->cbLeft -= 4;
1679 if (!pCursor->fNativEndian)
1680 u32 = RT_BSWAP_U32(u32);
1681 return u32;
1682}
1683
1684
1685/**
1686 * Reads a 64-bit unsigned integer and advances the cursor.
1687 *
1688 * @returns 64-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
1689 * uErrValue is returned.
1690 * @param pCursor The cursor.
1691 * @param uErrValue What to return on read error.
1692 */
1693static uint64_t rtDwarfCursor_GetU64(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1694{
1695 if (pCursor->cbUnitLeft < 8)
1696 {
1697 pCursor->pb += pCursor->cbUnitLeft;
1698 pCursor->cbLeft -= pCursor->cbUnitLeft;
1699 pCursor->cbUnitLeft = 0;
1700 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1701 return uErrValue;
1702 }
1703
1704 uint64_t u64 = RT_MAKE_U64_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3],
1705 pCursor->pb[4], pCursor->pb[5], pCursor->pb[6], pCursor->pb[7]);
1706 pCursor->pb += 8;
1707 pCursor->cbUnitLeft -= 8;
1708 pCursor->cbLeft -= 8;
1709 if (!pCursor->fNativEndian)
1710 u64 = RT_BSWAP_U64(u64);
1711 return u64;
1712}
1713
1714
1715/**
1716 * Reads an unsigned LEB128 encoded number.
1717 *
1718 * @returns unsigned 64-bit number. On error RTDWARFCURSOR::rc is set and @a
1719 * uErrValue is returned.
1720 * @param pCursor The cursor.
1721 * @param uErrValue The value to return on error.
1722 */
1723static uint64_t rtDwarfCursor_GetULeb128(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1724{
1725 if (pCursor->cbUnitLeft < 1)
1726 {
1727 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1728 return uErrValue;
1729 }
1730
1731 /*
1732 * Special case - single byte.
1733 */
1734 uint8_t b = pCursor->pb[0];
1735 if (!(b & 0x80))
1736 {
1737 pCursor->pb += 1;
1738 pCursor->cbUnitLeft -= 1;
1739 pCursor->cbLeft -= 1;
1740 return b;
1741 }
1742
1743 /*
1744 * Generic case.
1745 */
1746 /* Decode. */
1747 uint32_t off = 1;
1748 uint64_t u64Ret = b & 0x7f;
1749 do
1750 {
1751 if (off == pCursor->cbUnitLeft)
1752 {
1753 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1754 u64Ret = uErrValue;
1755 break;
1756 }
1757 b = pCursor->pb[off];
1758 u64Ret |= (b & 0x7f) << off * 7;
1759 off++;
1760 } while (b & 0x80);
1761
1762 /* Update the cursor. */
1763 pCursor->pb += off;
1764 pCursor->cbUnitLeft -= off;
1765 pCursor->cbLeft -= off;
1766
1767 /* Check the range. */
1768 uint32_t cBits = off * 7;
1769 if (cBits > 64)
1770 {
1771 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1772 u64Ret = uErrValue;
1773 }
1774
1775 return u64Ret;
1776}
1777
1778
1779/**
1780 * Reads a signed LEB128 encoded number.
1781 *
1782 * @returns signed 64-bit number. On error RTDWARFCURSOR::rc is set and @a
1783 * uErrValue is returned.
1784 * @param pCursor The cursor.
1785 * @param sErrValue The value to return on error.
1786 */
1787static int64_t rtDwarfCursor_GetSLeb128(PRTDWARFCURSOR pCursor, int64_t sErrValue)
1788{
1789 if (pCursor->cbUnitLeft < 1)
1790 {
1791 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1792 return sErrValue;
1793 }
1794
1795 /*
1796 * Special case - single byte.
1797 */
1798 uint8_t b = pCursor->pb[0];
1799 if (!(b & 0x80))
1800 {
1801 pCursor->pb += 1;
1802 pCursor->cbUnitLeft -= 1;
1803 pCursor->cbLeft -= 1;
1804 if (b & 0x40)
1805 b |= 0x80;
1806 return (int8_t)b;
1807 }
1808
1809 /*
1810 * Generic case.
1811 */
1812 /* Decode it. */
1813 uint32_t off = 1;
1814 uint64_t u64Ret = b & 0x7f;
1815 do
1816 {
1817 if (off == pCursor->cbUnitLeft)
1818 {
1819 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1820 u64Ret = (uint64_t)sErrValue;
1821 break;
1822 }
1823 b = pCursor->pb[off];
1824 u64Ret |= (b & 0x7f) << off * 7;
1825 off++;
1826 } while (b & 0x80);
1827
1828 /* Update cursor. */
1829 pCursor->pb += off;
1830 pCursor->cbUnitLeft -= off;
1831 pCursor->cbLeft -= off;
1832
1833 /* Check the range. */
1834 uint32_t cBits = off * 7;
1835 if (cBits > 64)
1836 {
1837 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1838 u64Ret = (uint64_t)sErrValue;
1839 }
1840 /* Sign extend the value. */
1841 else if (u64Ret & RT_BIT_64(cBits - 1))
1842 u64Ret |= ~(RT_BIT_64(cBits - 1) - 1);
1843
1844 return (int64_t)u64Ret;
1845}
1846
1847
1848/**
1849 * Reads an unsigned LEB128 encoded number, max 32-bit width.
1850 *
1851 * @returns unsigned 32-bit number. On error RTDWARFCURSOR::rc is set and @a
1852 * uErrValue is returned.
1853 * @param pCursor The cursor.
1854 * @param uErrValue The value to return on error.
1855 */
1856static uint32_t rtDwarfCursor_GetULeb128AsU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
1857{
1858 uint64_t u64 = rtDwarfCursor_GetULeb128(pCursor, uErrValue);
1859 if (u64 > UINT32_MAX)
1860 {
1861 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1862 return uErrValue;
1863 }
1864 return (uint32_t)u64;
1865}
1866
1867
1868/**
1869 * Reads a signed LEB128 encoded number, max 32-bit width.
1870 *
1871 * @returns signed 32-bit number. On error RTDWARFCURSOR::rc is set and @a
1872 * uErrValue is returned.
1873 * @param pCursor The cursor.
1874 * @param sErrValue The value to return on error.
1875 */
1876static int32_t rtDwarfCursor_GetSLeb128AsS32(PRTDWARFCURSOR pCursor, int32_t sErrValue)
1877{
1878 int64_t s64 = rtDwarfCursor_GetSLeb128(pCursor, sErrValue);
1879 if (s64 > INT32_MAX || s64 < INT32_MIN)
1880 {
1881 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1882 return sErrValue;
1883 }
1884 return (int32_t)s64;
1885}
1886
1887
1888/**
1889 * Skips a LEB128 encoded number.
1890 *
1891 * @returns IPRT status code.
1892 * @param pCursor The cursor.
1893 */
1894static int rtDwarfCursor_SkipLeb128(PRTDWARFCURSOR pCursor)
1895{
1896 if (RT_FAILURE(pCursor->rc))
1897 return pCursor->rc;
1898
1899 if (pCursor->cbUnitLeft < 1)
1900 return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1901
1902 uint32_t offSkip = 1;
1903 if (pCursor->pb[0] & 0x80)
1904 do
1905 {
1906 if (offSkip == pCursor->cbUnitLeft)
1907 {
1908 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1909 break;
1910 }
1911 } while (pCursor->pb[offSkip++] & 0x80);
1912
1913 pCursor->pb += offSkip;
1914 pCursor->cbUnitLeft -= offSkip;
1915 pCursor->cbLeft -= offSkip;
1916 return pCursor->rc;
1917}
1918
1919
1920/**
1921 * Advances the cursor a given number of bytes.
1922 *
1923 * @returns IPRT status code.
1924 * @param pCursor The cursor.
1925 * @param offSkip The number of bytes to advance.
1926 */
1927static int rtDwarfCursor_SkipBytes(PRTDWARFCURSOR pCursor, uint64_t offSkip)
1928{
1929 if (RT_FAILURE(pCursor->rc))
1930 return pCursor->rc;
1931 if (pCursor->cbUnitLeft < offSkip)
1932 return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1933
1934 size_t const offSkipSizeT = (size_t)offSkip;
1935 pCursor->cbUnitLeft -= offSkipSizeT;
1936 pCursor->cbLeft -= offSkipSizeT;
1937 pCursor->pb += offSkipSizeT;
1938
1939 return VINF_SUCCESS;
1940}
1941
1942
1943/**
1944 * Reads a zero terminated string, advancing the cursor beyond the terminator.
1945 *
1946 * @returns Pointer to the string.
1947 * @param pCursor The cursor.
1948 * @param pszErrValue What to return if the string isn't terminated
1949 * before the end of the unit.
1950 */
1951static const char *rtDwarfCursor_GetSZ(PRTDWARFCURSOR pCursor, const char *pszErrValue)
1952{
1953 const char *pszRet = (const char *)pCursor->pb;
1954 for (;;)
1955 {
1956 if (!pCursor->cbUnitLeft)
1957 {
1958 pCursor->rc = VERR_DWARF_BAD_STRING;
1959 return pszErrValue;
1960 }
1961 pCursor->cbUnitLeft--;
1962 pCursor->cbLeft--;
1963 if (!*pCursor->pb++)
1964 break;
1965 }
1966 return pszRet;
1967}
1968
1969
1970/**
1971 * Reads a 1, 2, 4 or 8 byte unsigned value.
1972 *
1973 * @returns 64-bit unsigned value.
1974 * @param pCursor The cursor.
1975 * @param cbValue The value size.
1976 * @param uErrValue The error value.
1977 */
1978static uint64_t rtDwarfCursor_GetVarSizedU(PRTDWARFCURSOR pCursor, size_t cbValue, uint64_t uErrValue)
1979{
1980 uint64_t u64Ret;
1981 switch (cbValue)
1982 {
1983 case 1: u64Ret = rtDwarfCursor_GetU8( pCursor, UINT8_MAX); break;
1984 case 2: u64Ret = rtDwarfCursor_GetU16(pCursor, UINT16_MAX); break;
1985 case 4: u64Ret = rtDwarfCursor_GetU32(pCursor, UINT32_MAX); break;
1986 case 8: u64Ret = rtDwarfCursor_GetU64(pCursor, UINT64_MAX); break;
1987 default:
1988 pCursor->rc = VERR_DWARF_BAD_INFO;
1989 return uErrValue;
1990 }
1991 if (RT_FAILURE(pCursor->rc))
1992 return uErrValue;
1993 return u64Ret;
1994}
1995
1996
1997#if 0 /* unused */
1998/**
1999 * Gets the pointer to a variable size block and advances the cursor.
2000 *
2001 * @returns Pointer to the block at the current cursor location. On error
2002 * RTDWARFCURSOR::rc is set and NULL returned.
2003 * @param pCursor The cursor.
2004 * @param cbBlock The block size.
2005 */
2006static const uint8_t *rtDwarfCursor_GetBlock(PRTDWARFCURSOR pCursor, uint32_t cbBlock)
2007{
2008 if (cbBlock > pCursor->cbUnitLeft)
2009 {
2010 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
2011 return NULL;
2012 }
2013
2014 uint8_t const *pb = &pCursor->pb[0];
2015 pCursor->pb += cbBlock;
2016 pCursor->cbUnitLeft -= cbBlock;
2017 pCursor->cbLeft -= cbBlock;
2018 return pb;
2019}
2020#endif
2021
2022
2023/**
2024 * Reads an unsigned DWARF half number.
2025 *
2026 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
2027 * uErrValue is returned.
2028 * @param pCursor The cursor.
2029 * @param uErrValue What to return on error.
2030 */
2031static uint16_t rtDwarfCursor_GetUHalf(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
2032{
2033 return rtDwarfCursor_GetU16(pCursor, uErrValue);
2034}
2035
2036
2037/**
2038 * Reads an unsigned DWARF byte number.
2039 *
2040 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
2041 * uErrValue is returned.
2042 * @param pCursor The cursor.
2043 * @param uErrValue What to return on error.
2044 */
2045static uint8_t rtDwarfCursor_GetUByte(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
2046{
2047 return rtDwarfCursor_GetU8(pCursor, uErrValue);
2048}
2049
2050
2051/**
2052 * Reads a signed DWARF byte number.
2053 *
2054 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
2055 * uErrValue is returned.
2056 * @param pCursor The cursor.
2057 * @param iErrValue What to return on error.
2058 */
2059static int8_t rtDwarfCursor_GetSByte(PRTDWARFCURSOR pCursor, int8_t iErrValue)
2060{
2061 return (int8_t)rtDwarfCursor_GetU8(pCursor, (uint8_t)iErrValue);
2062}
2063
2064
2065/**
2066 * Reads a unsigned DWARF offset value.
2067 *
2068 * @returns The value. On error RTDWARFCURSOR::rc is set and @a
2069 * uErrValue is returned.
2070 * @param pCursor The cursor.
2071 * @param uErrValue What to return on error.
2072 */
2073static uint64_t rtDwarfCursor_GetUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
2074{
2075 if (pCursor->f64bitDwarf)
2076 return rtDwarfCursor_GetU64(pCursor, uErrValue);
2077 return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
2078}
2079
2080
2081/**
2082 * Reads a unsigned DWARF native offset value.
2083 *
2084 * @returns The value. On error RTDWARFCURSOR::rc is set and @a
2085 * uErrValue is returned.
2086 * @param pCursor The cursor.
2087 * @param uErrValue What to return on error.
2088 */
2089static uint64_t rtDwarfCursor_GetNativeUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
2090{
2091 switch (pCursor->cbNativeAddr)
2092 {
2093 case 1: return rtDwarfCursor_GetU8(pCursor, (uint8_t )uErrValue);
2094 case 2: return rtDwarfCursor_GetU16(pCursor, (uint16_t)uErrValue);
2095 case 4: return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
2096 case 8: return rtDwarfCursor_GetU64(pCursor, uErrValue);
2097 default:
2098 pCursor->rc = VERR_INTERNAL_ERROR_2;
2099 return uErrValue;
2100 }
2101}
2102
2103
2104/**
2105 * Reads a 1, 2, 4 or 8 byte unsigned value.
2106 *
2107 * @returns 64-bit unsigned value.
2108 * @param pCursor The cursor.
2109 * @param bPtrEnc The pointer encoding.
2110 * @param uErrValue The error value.
2111 */
2112static uint64_t rtDwarfCursor_GetPtrEnc(PRTDWARFCURSOR pCursor, uint8_t bPtrEnc, uint64_t uErrValue)
2113{
2114 uint64_t u64Ret;
2115 switch (bPtrEnc & DW_EH_PE_FORMAT_MASK)
2116 {
2117 case DW_EH_PE_ptr:
2118 u64Ret = rtDwarfCursor_GetNativeUOff(pCursor, uErrValue);
2119 break;
2120 case DW_EH_PE_uleb128:
2121 u64Ret = rtDwarfCursor_GetULeb128(pCursor, uErrValue);
2122 break;
2123 case DW_EH_PE_udata2:
2124 u64Ret = rtDwarfCursor_GetU16(pCursor, UINT16_MAX);
2125 break;
2126 case DW_EH_PE_udata4:
2127 u64Ret = rtDwarfCursor_GetU32(pCursor, UINT32_MAX);
2128 break;
2129 case DW_EH_PE_udata8:
2130 u64Ret = rtDwarfCursor_GetU64(pCursor, UINT64_MAX);
2131 break;
2132 case DW_EH_PE_sleb128:
2133 u64Ret = rtDwarfCursor_GetSLeb128(pCursor, uErrValue);
2134 break;
2135 case DW_EH_PE_sdata2:
2136 u64Ret = (int64_t)(int16_t)rtDwarfCursor_GetU16(pCursor, UINT16_MAX);
2137 break;
2138 case DW_EH_PE_sdata4:
2139 u64Ret = (int64_t)(int32_t)rtDwarfCursor_GetU32(pCursor, UINT32_MAX);
2140 break;
2141 case DW_EH_PE_sdata8:
2142 u64Ret = rtDwarfCursor_GetU64(pCursor, UINT64_MAX);
2143 break;
2144 default:
2145 pCursor->rc = VERR_DWARF_BAD_INFO;
2146 return uErrValue;
2147 }
2148 if (RT_FAILURE(pCursor->rc))
2149 return uErrValue;
2150 return u64Ret;
2151}
2152
2153
2154/**
2155 * Gets the unit length, updating the unit length member and DWARF bitness
2156 * members of the cursor.
2157 *
2158 * @returns The unit length.
2159 * @param pCursor The cursor.
2160 */
2161static uint64_t rtDwarfCursor_GetInitalLength(PRTDWARFCURSOR pCursor)
2162{
2163 /*
2164 * Read the initial length.
2165 */
2166 pCursor->cbUnitLeft = pCursor->cbLeft;
2167 uint64_t cbUnit = rtDwarfCursor_GetU32(pCursor, 0);
2168 if (cbUnit != UINT32_C(0xffffffff))
2169 pCursor->f64bitDwarf = false;
2170 else
2171 {
2172 pCursor->f64bitDwarf = true;
2173 cbUnit = rtDwarfCursor_GetU64(pCursor, 0);
2174 }
2175
2176
2177 /*
2178 * Set the unit length, quitely fixing bad lengths.
2179 */
2180 pCursor->cbUnitLeft = (size_t)cbUnit;
2181 if ( pCursor->cbUnitLeft > pCursor->cbLeft
2182 || pCursor->cbUnitLeft != cbUnit)
2183 pCursor->cbUnitLeft = pCursor->cbLeft;
2184
2185 return cbUnit;
2186}
2187
2188
2189/**
2190 * Calculates the section offset corresponding to the current cursor position.
2191 *
2192 * @returns 32-bit section offset. If out of range, RTDWARFCURSOR::rc will be
2193 * set and UINT32_MAX returned.
2194 * @param pCursor The cursor.
2195 */
2196static uint32_t rtDwarfCursor_CalcSectOffsetU32(PRTDWARFCURSOR pCursor)
2197{
2198 size_t off = pCursor->pb - pCursor->pbStart;
2199 uint32_t offRet = (uint32_t)off;
2200 if (offRet != off)
2201 {
2202 AssertFailed();
2203 pCursor->rc = VERR_OUT_OF_RANGE;
2204 offRet = UINT32_MAX;
2205 }
2206 return offRet;
2207}
2208
2209
2210/**
2211 * Calculates an absolute cursor position from one relative to the current
2212 * cursor position.
2213 *
2214 * @returns The absolute cursor position.
2215 * @param pCursor The cursor.
2216 * @param offRelative The relative position. Must be a positive
2217 * offset.
2218 */
2219static uint8_t const *rtDwarfCursor_CalcPos(PRTDWARFCURSOR pCursor, size_t offRelative)
2220{
2221 if (offRelative > pCursor->cbUnitLeft)
2222 {
2223 Log(("rtDwarfCursor_CalcPos: bad position %#zx, cbUnitLeft=%#zu\n", offRelative, pCursor->cbUnitLeft));
2224 pCursor->rc = VERR_DWARF_BAD_POS;
2225 return NULL;
2226 }
2227 return pCursor->pb + offRelative;
2228}
2229
2230
2231/**
2232 * Advances the cursor to the given position.
2233 *
2234 * @returns IPRT status code.
2235 * @param pCursor The cursor.
2236 * @param pbNewPos The new position - returned by
2237 * rtDwarfCursor_CalcPos().
2238 */
2239static int rtDwarfCursor_AdvanceToPos(PRTDWARFCURSOR pCursor, uint8_t const *pbNewPos)
2240{
2241 if (RT_FAILURE(pCursor->rc))
2242 return pCursor->rc;
2243 AssertPtr(pbNewPos);
2244 if ((uintptr_t)pbNewPos < (uintptr_t)pCursor->pb)
2245 {
2246 Log(("rtDwarfCursor_AdvanceToPos: bad position %p, current %p\n", pbNewPos, pCursor->pb));
2247 return pCursor->rc = VERR_DWARF_BAD_POS;
2248 }
2249
2250 uintptr_t cbAdj = (uintptr_t)pbNewPos - (uintptr_t)pCursor->pb;
2251 if (RT_UNLIKELY(cbAdj > pCursor->cbUnitLeft))
2252 {
2253 AssertFailed();
2254 pCursor->rc = VERR_DWARF_BAD_POS;
2255 cbAdj = pCursor->cbUnitLeft;
2256 }
2257
2258 pCursor->cbUnitLeft -= cbAdj;
2259 pCursor->cbLeft -= cbAdj;
2260 pCursor->pb += cbAdj;
2261 return pCursor->rc;
2262}
2263
2264
2265/**
2266 * Check if the cursor is at the end of the current DWARF unit.
2267 *
2268 * @retval true if at the end or a cursor error is pending.
2269 * @retval false if not.
2270 * @param pCursor The cursor.
2271 */
2272static bool rtDwarfCursor_IsAtEndOfUnit(PRTDWARFCURSOR pCursor)
2273{
2274 return !pCursor->cbUnitLeft || RT_FAILURE(pCursor->rc);
2275}
2276
2277
2278/**
2279 * Skips to the end of the current unit.
2280 *
2281 * @returns IPRT status code.
2282 * @param pCursor The cursor.
2283 */
2284static int rtDwarfCursor_SkipUnit(PRTDWARFCURSOR pCursor)
2285{
2286 pCursor->pb += pCursor->cbUnitLeft;
2287 pCursor->cbLeft -= pCursor->cbUnitLeft;
2288 pCursor->cbUnitLeft = 0;
2289 return pCursor->rc;
2290}
2291
2292
2293/**
2294 * Check if the cursor is at the end of the section (or whatever the cursor is
2295 * processing).
2296 *
2297 * @retval true if at the end or a cursor error is pending.
2298 * @retval false if not.
2299 * @param pCursor The cursor.
2300 */
2301static bool rtDwarfCursor_IsAtEnd(PRTDWARFCURSOR pCursor)
2302{
2303 return !pCursor->cbLeft || RT_FAILURE(pCursor->rc);
2304}
2305
2306
2307/**
2308 * Initialize a section reader cursor.
2309 *
2310 * @returns IPRT status code.
2311 * @param pCursor The cursor.
2312 * @param pThis The dwarf module.
2313 * @param enmSect The name of the section to read.
2314 */
2315static int rtDwarfCursor_Init(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
2316{
2317 int rc = rtDbgModDwarfLoadSection(pThis, enmSect);
2318 if (RT_FAILURE(rc))
2319 return rc;
2320
2321 pCursor->enmSect = enmSect;
2322 pCursor->pbStart = (uint8_t const *)pThis->aSections[enmSect].pv;
2323 pCursor->pb = pCursor->pbStart;
2324 pCursor->cbLeft = pThis->aSections[enmSect].cb;
2325 pCursor->cbUnitLeft = pCursor->cbLeft;
2326 pCursor->pDwarfMod = pThis;
2327 pCursor->f64bitDwarf = false;
2328 /** @todo ask the image about the endian used as well as the address
2329 * width. */
2330 pCursor->fNativEndian = true;
2331 pCursor->cbNativeAddr = 4;
2332 pCursor->rc = VINF_SUCCESS;
2333
2334 return VINF_SUCCESS;
2335}
2336
2337
2338/**
2339 * Initialize a section reader cursor with an offset.
2340 *
2341 * @returns IPRT status code.
2342 * @param pCursor The cursor.
2343 * @param pThis The dwarf module.
2344 * @param enmSect The name of the section to read.
2345 * @param offSect The offset into the section.
2346 */
2347static int rtDwarfCursor_InitWithOffset(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis,
2348 krtDbgModDwarfSect enmSect, uint32_t offSect)
2349{
2350 if (offSect > pThis->aSections[enmSect].cb)
2351 {
2352 Log(("rtDwarfCursor_InitWithOffset: offSect=%#x cb=%#x enmSect=%d\n", offSect, pThis->aSections[enmSect].cb, enmSect));
2353 return VERR_DWARF_BAD_POS;
2354 }
2355
2356 int rc = rtDwarfCursor_Init(pCursor, pThis, enmSect);
2357 if (RT_SUCCESS(rc))
2358 {
2359 pCursor->pbStart += offSect;
2360 pCursor->pb += offSect;
2361 pCursor->cbLeft -= offSect;
2362 pCursor->cbUnitLeft -= offSect;
2363 }
2364
2365 return rc;
2366}
2367
2368
2369/**
2370 * Initialize a cursor for a block (subsection) retrieved from the given cursor.
2371 *
2372 * The parent cursor will be advanced past the block.
2373 *
2374 * @returns IPRT status code.
2375 * @param pCursor The cursor.
2376 * @param pParent The parent cursor. Will be moved by @a cbBlock.
2377 * @param cbBlock The size of the block the new cursor should
2378 * cover.
2379 */
2380static int rtDwarfCursor_InitForBlock(PRTDWARFCURSOR pCursor, PRTDWARFCURSOR pParent, uint32_t cbBlock)
2381{
2382 if (RT_FAILURE(pParent->rc))
2383 return pParent->rc;
2384 if (pParent->cbUnitLeft < cbBlock)
2385 {
2386 Log(("rtDwarfCursor_InitForBlock: cbUnitLeft=%#x < cbBlock=%#x \n", pParent->cbUnitLeft, cbBlock));
2387 return VERR_DWARF_BAD_POS;
2388 }
2389
2390 *pCursor = *pParent;
2391 pCursor->cbLeft = cbBlock;
2392 pCursor->cbUnitLeft = cbBlock;
2393
2394 pParent->pb += cbBlock;
2395 pParent->cbLeft -= cbBlock;
2396 pParent->cbUnitLeft -= cbBlock;
2397
2398 return VINF_SUCCESS;
2399}
2400
2401
2402/**
2403 * Initialize a reader cursor for a memory block (eh_frame).
2404 *
2405 * @returns IPRT status code.
2406 * @param pCursor The cursor.
2407 * @param pvMem The memory block.
2408 * @param cbMem The size of the memory block.
2409 */
2410static int rtDwarfCursor_InitForMem(PRTDWARFCURSOR pCursor, void const *pvMem, size_t cbMem)
2411{
2412 pCursor->enmSect = krtDbgModDwarfSect_End;
2413 pCursor->pbStart = (uint8_t const *)pvMem;
2414 pCursor->pb = (uint8_t const *)pvMem;
2415 pCursor->cbLeft = cbMem;
2416 pCursor->cbUnitLeft = cbMem;
2417 pCursor->pDwarfMod = NULL;
2418 pCursor->f64bitDwarf = false;
2419 /** @todo ask the image about the endian used as well as the address
2420 * width. */
2421 pCursor->fNativEndian = true;
2422 pCursor->cbNativeAddr = 4;
2423 pCursor->rc = VINF_SUCCESS;
2424
2425 return VINF_SUCCESS;
2426}
2427
2428
2429/**
2430 * Deletes a section reader initialized by rtDwarfCursor_Init.
2431 *
2432 * @returns @a rcOther or RTDWARCURSOR::rc.
2433 * @param pCursor The section reader.
2434 * @param rcOther Other error code to be returned if it indicates
2435 * error or if the cursor status is OK.
2436 */
2437static int rtDwarfCursor_Delete(PRTDWARFCURSOR pCursor, int rcOther)
2438{
2439 /* ... and a drop of poison. */
2440 pCursor->pb = NULL;
2441 pCursor->cbLeft = ~(size_t)0;
2442 pCursor->cbUnitLeft = ~(size_t)0;
2443 pCursor->pDwarfMod = NULL;
2444 if (RT_FAILURE(pCursor->rc) && RT_SUCCESS(rcOther))
2445 rcOther = pCursor->rc;
2446 pCursor->rc = VERR_INTERNAL_ERROR_4;
2447 return rcOther;
2448}
2449
2450
2451/*
2452 *
2453 * DWARF Frame Unwind Information.
2454 * DWARF Frame Unwind Information.
2455 * DWARF Frame Unwind Information.
2456 *
2457 */
2458
2459/**
2460 * Common information entry (CIE) information.
2461 */
2462typedef struct RTDWARFCIEINFO
2463{
2464 /** The segment location of the CIE. */
2465 uint64_t offCie;
2466 /** The DWARF version. */
2467 uint8_t uDwarfVer;
2468 /** The address pointer encoding. */
2469 uint8_t bAddressPtrEnc;
2470 /** The segment size (v4). */
2471 uint8_t cbSegment;
2472 /** The LSDA pointer encoding. */
2473 uint8_t bLsdaPtrEnc;
2474
2475 /** Set if the EH data field is present ('eh'). */
2476 bool fHasEhData : 1;
2477 /** Set if there is an augmentation data size ('z'). */
2478 bool fHasAugmentationSize : 1;
2479 /** Set if the augmentation data contains a LSDA (pointer size byte in CIE,
2480 * pointer in FDA) ('L'). */
2481 bool fHasLanguageSpecificDataArea : 1;
2482 /** Set if the augmentation data contains a personality routine
2483 * (pointer size + pointer) ('P'). */
2484 bool fHasPersonalityRoutine : 1;
2485 /** Set if the augmentation data contains the address encoding . */
2486 bool fHasAddressEnc : 1;
2487 /** Set if signal frame. */
2488 bool fIsSignalFrame : 1;
2489 /** Set if we've encountered unknown augmentation data. This
2490 * means the CIE is incomplete and cannot be used. */
2491 bool fHasUnknowAugmentation : 1;
2492
2493 /** Copy of the augmentation string. */
2494 const char *pszAugmentation;
2495
2496 /** Code alignment factor for the instruction. */
2497 uint64_t uCodeAlignFactor;
2498 /** Data alignment factor for the instructions. */
2499 int64_t iDataAlignFactor;
2500
2501 /** Pointer to the instruction sequence. */
2502 uint8_t const *pbInstructions;
2503 /** The length of the instruction sequence. */
2504 size_t cbInstructions;
2505} RTDWARFCIEINFO;
2506/** Pointer to CIE info. */
2507typedef RTDWARFCIEINFO *PRTDWARFCIEINFO;
2508/** Pointer to const CIE info. */
2509typedef RTDWARFCIEINFO const *PCRTDWARFCIEINFO;
2510
2511
2512/**
2513 * Processes a FDE, taking over after the PC range field.
2514 *
2515 * @returns IPRT status code.
2516 * @param pCursor The cursor.
2517 * @param pCie Information about the corresponding CIE.
2518 * @param uPcBegin The PC begin field value (sans segment).
2519 * @param cbPcRange The PC range from @a uPcBegin.
2520 * @param offInRange The offset into the range corresponding to
2521 * pState->uPc.
2522 * @param pState The unwind state to work.
2523 */
2524static int rtDwarfUnwind_ProcessFde(PRTDWARFCURSOR pCursor, PCRTDWARFCIEINFO pCie, uint64_t uPcBegin,
2525 uint64_t cbPcRange, uint64_t offInRange, PRTDBGUNWINDSTATE pState)
2526{
2527 /*
2528 * Deal with augmented data fields.
2529 */
2530 /* The size. */
2531 size_t cbInstr = ~(size_t)0;
2532 if (pCie->fHasAugmentationSize)
2533 {
2534 uint64_t cbAugData = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
2535 if (RT_FAILURE(pCursor->rc))
2536 return pCursor->rc;
2537 if (cbAugData > pCursor->cbUnitLeft)
2538 return VERR_DBG_MALFORMED_UNWIND_INFO;
2539 cbInstr = pCursor->cbUnitLeft - cbAugData;
2540 }
2541 else if (pCie->fHasUnknowAugmentation)
2542 return VERR_DBG_MALFORMED_UNWIND_INFO;
2543
2544 /* Parse the string and fetch FDE fields. */
2545 if (!pCie->fHasEhData)
2546 for (const char *pszAug = pCie->pszAugmentation; *pszAug != '\0'; pszAug++)
2547 switch (*pszAug)
2548 {
2549 case 'L':
2550 if (pCie->bLsdaPtrEnc != DW_EH_PE_omit)
2551 rtDwarfCursor_GetPtrEnc(pCursor, pCie->bLsdaPtrEnc, 0);
2552 break;
2553 }
2554
2555 /* Skip unconsumed bytes. */
2556 if ( cbInstr != ~(size_t)0
2557 && pCursor->cbUnitLeft > cbInstr)
2558 rtDwarfCursor_SkipBytes(pCursor, pCursor->cbUnitLeft - cbInstr);
2559 if (RT_FAILURE(pCursor->rc))
2560 return pCursor->rc;
2561
2562 /*
2563 * The crazy part. Table program execution.
2564 */
2565 if (pCie->cbInstructions > 0)
2566 {
2567 /** @todo continue here later. */
2568 }
2569
2570 RT_NOREF(pState, uPcBegin, cbPcRange, offInRange);
2571 return VINF_SUCCESS;
2572}
2573
2574
2575
2576/**
2577 * Load the information we need from a CIE.
2578 *
2579 * This starts after the initial length and CIE_pointer fields has
2580 * been processed.
2581 *
2582 * @returns IPRT status code.
2583 * @param pCursor The cursor.
2584 * @param pNewCie The structure to populate with parsed CIE info.
2585 * @param offUnit The unit offset.
2586 * @param bDefaultPtrEnc The default pointer encoding.
2587 */
2588static int rtDwarfUnwind_LoadCie(PRTDWARFCURSOR pCursor, PRTDWARFCIEINFO pNewCie, uint64_t offUnit, uint8_t bDefaultPtrEnc)
2589{
2590 /*
2591 * Initialize the CIE record and get the version.
2592 */
2593 RT_ZERO(*pNewCie);
2594 pNewCie->offCie = offUnit;
2595 pNewCie->bLsdaPtrEnc = DW_EH_PE_omit;
2596 pNewCie->bAddressPtrEnc = DW_EH_PE_omit; /* set later */
2597 pNewCie->uDwarfVer = rtDwarfCursor_GetUByte(pCursor, 0);
2598 if ( pNewCie->uDwarfVer >= 1 /* Note! Some GCC versions may emit v1 here. */
2599 && pNewCie->uDwarfVer <= 5)
2600 return VERR_VERSION_MISMATCH;
2601
2602 /*
2603 * The augmentation string.
2604 *
2605 * First deal with special "eh" string from oldish GCC (dwarf2out.c about 1997), specified in LSB:
2606 * https://refspecs.linuxfoundation.org/LSB_3.0.0/LSB-PDA/LSB-PDA/ehframechpt.html
2607 */
2608 pNewCie->pszAugmentation = rtDwarfCursor_GetSZ(pCursor, "");
2609 if ( pNewCie->pszAugmentation[0] == 'e'
2610 && pNewCie->pszAugmentation[1] == 'h'
2611 && pNewCie->pszAugmentation[2] == '\0')
2612 {
2613 pNewCie->fHasEhData = true;
2614 rtDwarfCursor_GetPtrEnc(pCursor, bDefaultPtrEnc, 0);
2615 }
2616 else
2617 {
2618 /* Regular augmentation string. */
2619 for (const char *pszAug = pNewCie->pszAugmentation; *pszAug != '\0'; pszAug++)
2620 switch (*pszAug)
2621 {
2622 case 'z':
2623 pNewCie->fHasAugmentationSize = true;
2624 break;
2625 case 'L':
2626 pNewCie->fHasLanguageSpecificDataArea = true;
2627 break;
2628 case 'P':
2629 pNewCie->fHasPersonalityRoutine = true;
2630 break;
2631 case 'R':
2632 pNewCie->fHasAddressEnc = true;
2633 break;
2634 case 'S':
2635 pNewCie->fIsSignalFrame = true;
2636 break;
2637 default:
2638 pNewCie->fHasUnknowAugmentation = true;
2639 break;
2640 }
2641 }
2642
2643 /*
2644 * More standard fields
2645 */
2646 uint8_t cbAddress = 0;
2647 if (pNewCie->uDwarfVer >= 4)
2648 {
2649 cbAddress = rtDwarfCursor_GetU8(pCursor, bDefaultPtrEnc == DW_EH_PE_udata8 ? 8 : 4);
2650 pNewCie->cbSegment = rtDwarfCursor_GetU8(pCursor, 0);
2651 }
2652 pNewCie->uCodeAlignFactor = rtDwarfCursor_GetULeb128(pCursor, 1);
2653 pNewCie->iDataAlignFactor = rtDwarfCursor_GetSLeb128(pCursor, 1);
2654
2655 /*
2656 * Augmentation data.
2657 */
2658 if (!pNewCie->fHasEhData)
2659 {
2660 /* The size. */
2661 size_t cbInstr = ~(size_t)0;
2662 if (pNewCie->fHasAugmentationSize)
2663 {
2664 uint64_t cbAugData = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
2665 if (RT_FAILURE(pCursor->rc))
2666 return pCursor->rc;
2667 if (cbAugData > pCursor->cbUnitLeft)
2668 return VERR_DBG_MALFORMED_UNWIND_INFO;
2669 cbInstr = pCursor->cbUnitLeft - cbAugData;
2670 }
2671 else if (pNewCie->fHasUnknowAugmentation)
2672 return VERR_DBG_MALFORMED_UNWIND_INFO;
2673
2674 /* Parse the string. */
2675 for (const char *pszAug = pNewCie->pszAugmentation; *pszAug != '\0'; pszAug++)
2676 switch (*pszAug)
2677 {
2678 case 'L':
2679 pNewCie->bLsdaPtrEnc = rtDwarfCursor_GetU8(pCursor, DW_EH_PE_omit);
2680 break;
2681 case 'P':
2682 rtDwarfCursor_GetPtrEnc(pCursor, rtDwarfCursor_GetU8(pCursor, DW_EH_PE_omit), 0);
2683 break;
2684 case 'R':
2685 pNewCie->bAddressPtrEnc = rtDwarfCursor_GetU8(pCursor, DW_EH_PE_omit);
2686 break;
2687 }
2688
2689 /* Skip unconsumed bytes. */
2690 if ( cbInstr != ~(size_t)0
2691 && pCursor->cbUnitLeft > cbInstr)
2692 rtDwarfCursor_SkipBytes(pCursor, pCursor->cbUnitLeft - cbInstr);
2693 }
2694
2695 /*
2696 * Note down where the instructions are.
2697 */
2698 pNewCie->pbInstructions = pCursor->pb;
2699 pNewCie->cbInstructions = pCursor->cbUnitLeft;
2700
2701 /*
2702 * Determine the target address encoding.
2703 */
2704 if (pNewCie->bAddressPtrEnc == DW_EH_PE_omit)
2705 switch (cbAddress)
2706 {
2707 case 2: pNewCie->bAddressPtrEnc = DW_EH_PE_udata2; break;
2708 case 4: pNewCie->bAddressPtrEnc = DW_EH_PE_udata4; break;
2709 case 8: pNewCie->bAddressPtrEnc = DW_EH_PE_udata8; break;
2710 default: pNewCie->bAddressPtrEnc = bDefaultPtrEnc; break;
2711 }
2712
2713 return VINF_SUCCESS;
2714}
2715
2716
2717/**
2718 * Does a slow unwind of a '.debug_frame' or '.eh_frame' section.
2719 *
2720 * @returns IPRT status code.
2721 * @param pCursor The cursor.
2722 * @param idxSeg The segment of the PC location.
2723 * @param offSeg The segment offset of the PC location.
2724 * @param uRva The RVA of the PC location.
2725 * @param pState The unwind state to work.
2726 * @param bDefaultPtrEnc The default pointer encoding.
2727 * @param fIsEhFrame Set if this is a '.eh_frame'. GCC generate these
2728 * with different CIE_pointer values.
2729 */
2730DECLHIDDEN(int) rtDwarfUnwind_Slow(PRTDWARFCURSOR pCursor, RTDBGSEGIDX idxSeg, RTUINTPTR offSeg, RTUINTPTR uRva,
2731 PRTDBGUNWINDSTATE pState, uint8_t bDefaultPtrEnc, bool fIsEhFrame)
2732{
2733 /*
2734 * CIE info we collect.
2735 */
2736 PRTDWARFCIEINFO paCies = NULL;
2737 uint32_t cCies = 0;
2738 PRTDWARFCIEINFO pCieHint = NULL;
2739
2740 /*
2741 * Do the scanning.
2742 */
2743 int rc = VERR_DBG_UNWIND_INFO_NOT_FOUND;
2744 while ( !rtDwarfCursor_IsAtEnd(pCursor)
2745 && RT_SUCCESS(rc))
2746 {
2747 uint64_t const offUnit = rtDwarfCursor_CalcSectOffsetU32(pCursor);
2748 if (rtDwarfCursor_GetInitalLength(pCursor) == 0)
2749 break;
2750
2751 uint64_t const offRelCie = rtDwarfCursor_GetUOff(pCursor, 0);
2752 if ( offRelCie != 0
2753 && offRelCie != (pCursor->f64bitDwarf ? UINT64_MAX : UINT32_MAX))
2754 {
2755 /*
2756 * Frame descriptor entry (FDE).
2757 */
2758 /* Locate the corresponding CIE. The CIE pointer is self relative
2759 in .eh_frame and section relative in .debug_frame. */
2760 PRTDWARFCIEINFO pCieForFde;
2761 uint64_t offCie = fIsEhFrame ? offUnit - offRelCie : offRelCie;
2762 if (pCieHint && pCieHint->offCie == offCie)
2763 pCieForFde = pCieHint;
2764 else
2765 {
2766 pCieForFde = NULL;
2767 uint32_t i = cCies;
2768 while (i-- > 0)
2769 if (paCies[i].offCie == offCie)
2770 {
2771 pCieHint = pCieForFde = &paCies[i];
2772 break;
2773 }
2774 }
2775 if (pCieForFde)
2776 {
2777 /* Read the PC range covered by this FDE (the fields are also
2778 known as initial_location & instructions). */
2779 RTDBGSEGIDX idxFdeSeg = RTDBGSEGIDX_RVA;
2780 if (pCieForFde->cbSegment)
2781 idxFdeSeg = rtDwarfCursor_GetVarSizedU(pCursor, pCieForFde->cbSegment, RTDBGSEGIDX_RVA);
2782 uint64_t uPcBegin = rtDwarfCursor_GetPtrEnc(pCursor, pCieForFde->bAddressPtrEnc, 0);
2783 uint64_t cbPcRange = rtDwarfCursor_GetPtrEnc(pCursor, pCieForFde->bAddressPtrEnc, 0);
2784
2785 /* Match it with what we're looking for. */
2786 bool fMatch = idxFdeSeg == RTDBGSEGIDX_RVA
2787 ? uRva - uPcBegin < cbPcRange
2788 : idxSeg == idxFdeSeg && offSeg - uPcBegin < cbPcRange;
2789 if (fMatch)
2790 {
2791 rc = rtDwarfUnwind_ProcessFde(pCursor, pCieForFde, uPcBegin, cbPcRange,
2792 idxFdeSeg == RTDBGSEGIDX_RVA ? uRva - uPcBegin : offSeg - uPcBegin,
2793 pState);
2794 break;
2795 }
2796 }
2797 }
2798 else
2799 {
2800 /*
2801 * Common information entry (CIE). Record the info we need about it.
2802 */
2803 if ((cCies & 8) == 0)
2804 {
2805 void *pvNew = RTMemRealloc(paCies, sizeof(paCies[0]) * (cCies + 8));
2806 if (pvNew)
2807 paCies = (PRTDWARFCIEINFO)pvNew;
2808 else
2809 {
2810 rc = VERR_NO_MEMORY;
2811 break;
2812 }
2813 }
2814 int rc2 = rtDwarfUnwind_LoadCie(pCursor, &paCies[cCies], offUnit, bDefaultPtrEnc);
2815 if (RT_SUCCESS(rc2))
2816 cCies++;
2817 }
2818 rtDwarfCursor_SkipUnit(pCursor);
2819 }
2820
2821 /*
2822 * Cleanup.
2823 */
2824 if (paCies)
2825 RTMemFree(paCies);
2826 return rc;
2827}
2828
2829
2830/**
2831 * Helper for translating a loader architecture value to a pointe encoding.
2832 *
2833 * @returns Pointer encoding.
2834 * @param enmLdrArch The loader architecture value to convert.
2835 */
2836static uint8_t rtDwarfUnwind_ArchToPtrEnc(RTLDRARCH enmLdrArch)
2837{
2838 switch (enmLdrArch)
2839 {
2840 case RTLDRARCH_AMD64:
2841 case RTLDRARCH_ARM64:
2842 return DW_EH_PE_udata8;
2843 case RTLDRARCH_X86_16:
2844 case RTLDRARCH_X86_32:
2845 case RTLDRARCH_ARM32:
2846 return DW_EH_PE_udata4;
2847 case RTLDRARCH_HOST:
2848 case RTLDRARCH_WHATEVER:
2849 case RTLDRARCH_INVALID:
2850 case RTLDRARCH_END:
2851 case RTLDRARCH_32BIT_HACK:
2852 break;
2853 }
2854 AssertFailed();
2855 return DW_EH_PE_udata4;
2856}
2857
2858
2859/**
2860 * Interface for the loader code.
2861 *
2862 * @returns IPRT status.
2863 * @param pvSection The '.eh_frame' section data.
2864 * @param cbSection The size of the '.eh_frame' section data.
2865 * @param idxSeg The segment of the PC location.
2866 * @param offSeg The segment offset of the PC location.
2867 * @param uRva The RVA of the PC location.
2868 * @param pState The unwind state to work.
2869 * @param enmArch The image architecture.
2870 */
2871DECLHIDDEN(int) rtDwarfUnwind_EhData(void const *pvSection, size_t cbSection, RTDBGSEGIDX idxSeg, RTUINTPTR offSeg,
2872 RTUINTPTR uRva, PRTDBGUNWINDSTATE pState, RTLDRARCH enmArch)
2873{
2874 RTDWARFCURSOR Cursor;
2875 rtDwarfCursor_InitForMem(&Cursor, pvSection, cbSection);
2876 int rc = rtDwarfUnwind_Slow(&Cursor, idxSeg, offSeg, uRva, pState, rtDwarfUnwind_ArchToPtrEnc(enmArch), true /*fIsEhFrame*/);
2877 return rtDwarfCursor_Delete(&Cursor, rc);
2878}
2879
2880
2881/*
2882 *
2883 * DWARF Line Numbers.
2884 * DWARF Line Numbers.
2885 * DWARF Line Numbers.
2886 *
2887 */
2888
2889
2890/**
2891 * Defines a file name.
2892 *
2893 * @returns IPRT status code.
2894 * @param pLnState The line number program state.
2895 * @param pszFilename The name of the file.
2896 * @param idxInc The include path index.
2897 */
2898static int rtDwarfLine_DefineFileName(PRTDWARFLINESTATE pLnState, const char *pszFilename, uint64_t idxInc)
2899{
2900 /*
2901 * Resize the array if necessary.
2902 */
2903 uint32_t iFileName = pLnState->cFileNames;
2904 if ((iFileName % 2) == 0)
2905 {
2906 void *pv = RTMemRealloc(pLnState->papszFileNames, sizeof(pLnState->papszFileNames[0]) * (iFileName + 2));
2907 if (!pv)
2908 return VERR_NO_MEMORY;
2909 pLnState->papszFileNames = (char **)pv;
2910 }
2911
2912 /*
2913 * Add the file name.
2914 */
2915 if ( pszFilename[0] == '/'
2916 || pszFilename[0] == '\\'
2917 || (RT_C_IS_ALPHA(pszFilename[0]) && pszFilename[1] == ':') )
2918 pLnState->papszFileNames[iFileName] = RTStrDup(pszFilename);
2919 else if (idxInc < pLnState->cIncPaths)
2920 pLnState->papszFileNames[iFileName] = RTPathJoinA(pLnState->papszIncPaths[idxInc], pszFilename);
2921 else
2922 return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
2923 if (!pLnState->papszFileNames[iFileName])
2924 return VERR_NO_STR_MEMORY;
2925 pLnState->cFileNames = iFileName + 1;
2926
2927 /*
2928 * Sanitize the name.
2929 */
2930 int rc = rtDbgModDwarfStringToUtf8(pLnState->pDwarfMod, &pLnState->papszFileNames[iFileName]);
2931 Log((" File #%02u = '%s'\n", iFileName, pLnState->papszFileNames[iFileName]));
2932 return rc;
2933}
2934
2935
2936/**
2937 * Adds a line to the table and resets parts of the state (DW_LNS_copy).
2938 *
2939 * @returns IPRT status code
2940 * @param pLnState The line number program state.
2941 * @param offOpCode The opcode offset (for logging
2942 * purposes).
2943 */
2944static int rtDwarfLine_AddLine(PRTDWARFLINESTATE pLnState, uint32_t offOpCode)
2945{
2946 PRTDBGMODDWARF pThis = pLnState->pDwarfMod;
2947 int rc;
2948 if (pThis->iWatcomPass == 1)
2949 rc = rtDbgModDwarfRecordSegOffset(pThis, pLnState->Regs.uSegment, pLnState->Regs.uAddress + 1);
2950 else
2951 {
2952 const char *pszFile = pLnState->Regs.iFile < pLnState->cFileNames
2953 ? pLnState->papszFileNames[pLnState->Regs.iFile]
2954 : "<bad file name index>";
2955 NOREF(offOpCode);
2956
2957 RTDBGSEGIDX iSeg;
2958 RTUINTPTR offSeg;
2959 rc = rtDbgModDwarfLinkAddressToSegOffset(pLnState->pDwarfMod, pLnState->Regs.uSegment, pLnState->Regs.uAddress,
2960 &iSeg, &offSeg); /*AssertRC(rc);*/
2961 if (RT_SUCCESS(rc))
2962 {
2963 Log2(("rtDwarfLine_AddLine: %x:%08llx (%#llx) %s(%d) [offOpCode=%08x]\n", iSeg, offSeg, pLnState->Regs.uAddress, pszFile, pLnState->Regs.uLine, offOpCode));
2964 rc = RTDbgModLineAdd(pLnState->pDwarfMod->hCnt, pszFile, pLnState->Regs.uLine, iSeg, offSeg, NULL);
2965
2966 /* Ignore address conflicts for now. */
2967 if (rc == VERR_DBG_ADDRESS_CONFLICT)
2968 rc = VINF_SUCCESS;
2969 }
2970 else
2971 rc = VINF_SUCCESS; /* ignore failure */
2972 }
2973
2974 pLnState->Regs.fBasicBlock = false;
2975 pLnState->Regs.fPrologueEnd = false;
2976 pLnState->Regs.fEpilogueBegin = false;
2977 pLnState->Regs.uDiscriminator = 0;
2978 return rc;
2979}
2980
2981
2982/**
2983 * Reset the program to the start-of-sequence state.
2984 *
2985 * @param pLnState The line number program state.
2986 */
2987static void rtDwarfLine_ResetState(PRTDWARFLINESTATE pLnState)
2988{
2989 pLnState->Regs.uAddress = 0;
2990 pLnState->Regs.idxOp = 0;
2991 pLnState->Regs.iFile = 1;
2992 pLnState->Regs.uLine = 1;
2993 pLnState->Regs.uColumn = 0;
2994 pLnState->Regs.fIsStatement = RT_BOOL(pLnState->Hdr.u8DefIsStmt);
2995 pLnState->Regs.fBasicBlock = false;
2996 pLnState->Regs.fEndSequence = false;
2997 pLnState->Regs.fPrologueEnd = false;
2998 pLnState->Regs.fEpilogueBegin = false;
2999 pLnState->Regs.uIsa = 0;
3000 pLnState->Regs.uDiscriminator = 0;
3001 pLnState->Regs.uSegment = 0;
3002}
3003
3004
3005/**
3006 * Runs the line number program.
3007 *
3008 * @returns IPRT status code.
3009 * @param pLnState The line number program state.
3010 * @param pCursor The cursor.
3011 */
3012static int rtDwarfLine_RunProgram(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
3013{
3014 LogFlow(("rtDwarfLine_RunProgram: cbUnitLeft=%zu\n", pCursor->cbUnitLeft));
3015
3016 int rc = VINF_SUCCESS;
3017 rtDwarfLine_ResetState(pLnState);
3018
3019 while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
3020 {
3021#ifdef LOG_ENABLED
3022 uint32_t const offOpCode = rtDwarfCursor_CalcSectOffsetU32(pCursor);
3023#else
3024 uint32_t const offOpCode = 0;
3025#endif
3026 uint8_t bOpCode = rtDwarfCursor_GetUByte(pCursor, DW_LNS_extended);
3027 if (bOpCode >= pLnState->Hdr.u8OpcodeBase)
3028 {
3029 /*
3030 * Special opcode.
3031 */
3032 uint8_t const bLogOpCode = bOpCode; NOREF(bLogOpCode);
3033 bOpCode -= pLnState->Hdr.u8OpcodeBase;
3034
3035 int32_t const cLineDelta = bOpCode % pLnState->Hdr.u8LineRange + (int32_t)pLnState->Hdr.s8LineBase;
3036 bOpCode /= pLnState->Hdr.u8LineRange;
3037
3038 uint64_t uTmp = bOpCode + pLnState->Regs.idxOp;
3039 uint64_t const cAddressDelta = uTmp / pLnState->Hdr.cMaxOpsPerInstr * pLnState->Hdr.cbMinInstr;
3040 uint64_t const cOpIndexDelta = uTmp % pLnState->Hdr.cMaxOpsPerInstr;
3041
3042 pLnState->Regs.uLine += cLineDelta;
3043 pLnState->Regs.uAddress += cAddressDelta;
3044 pLnState->Regs.idxOp += cOpIndexDelta;
3045 Log2(("%08x: DW Special Opcode %#04x: uLine + %d => %u; uAddress + %#llx => %#llx; idxOp + %#llx => %#llx\n",
3046 offOpCode, bLogOpCode, cLineDelta, pLnState->Regs.uLine, cAddressDelta, pLnState->Regs.uAddress,
3047 cOpIndexDelta, pLnState->Regs.idxOp));
3048
3049 /*
3050 * LLVM emits debug info for global constructors (_GLOBAL__I_a) which are not part of source
3051 * code but are inserted by the compiler: The resulting line number will be 0
3052 * because they are not part of the source file obviously (see https://reviews.llvm.org/rL205999),
3053 * so skip adding them when they are encountered.
3054 */
3055 if (pLnState->Regs.uLine)
3056 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
3057 }
3058 else
3059 {
3060 switch (bOpCode)
3061 {
3062 /*
3063 * Standard opcode.
3064 */
3065 case DW_LNS_copy:
3066 Log2(("%08x: DW_LNS_copy\n", offOpCode));
3067 /* See the comment about LLVM above. */
3068 if (pLnState->Regs.uLine)
3069 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
3070 break;
3071
3072 case DW_LNS_advance_pc:
3073 {
3074 uint64_t u64Adv = rtDwarfCursor_GetULeb128(pCursor, 0);
3075 pLnState->Regs.uAddress += (pLnState->Regs.idxOp + u64Adv) / pLnState->Hdr.cMaxOpsPerInstr
3076 * pLnState->Hdr.cbMinInstr;
3077 pLnState->Regs.idxOp += (pLnState->Regs.idxOp + u64Adv) % pLnState->Hdr.cMaxOpsPerInstr;
3078 Log2(("%08x: DW_LNS_advance_pc: u64Adv=%#llx (%lld) )\n", offOpCode, u64Adv, u64Adv));
3079 break;
3080 }
3081
3082 case DW_LNS_advance_line:
3083 {
3084 int32_t cLineDelta = rtDwarfCursor_GetSLeb128AsS32(pCursor, 0);
3085 pLnState->Regs.uLine += cLineDelta;
3086 Log2(("%08x: DW_LNS_advance_line: uLine + %d => %u\n", offOpCode, cLineDelta, pLnState->Regs.uLine));
3087 break;
3088 }
3089
3090 case DW_LNS_set_file:
3091 pLnState->Regs.iFile = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
3092 Log2(("%08x: DW_LNS_set_file: iFile=%u\n", offOpCode, pLnState->Regs.iFile));
3093 break;
3094
3095 case DW_LNS_set_column:
3096 pLnState->Regs.uColumn = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
3097 Log2(("%08x: DW_LNS_set_column\n", offOpCode));
3098 break;
3099
3100 case DW_LNS_negate_stmt:
3101 pLnState->Regs.fIsStatement = !pLnState->Regs.fIsStatement;
3102 Log2(("%08x: DW_LNS_negate_stmt\n", offOpCode));
3103 break;
3104
3105 case DW_LNS_set_basic_block:
3106 pLnState->Regs.fBasicBlock = true;
3107 Log2(("%08x: DW_LNS_set_basic_block\n", offOpCode));
3108 break;
3109
3110 case DW_LNS_const_add_pc:
3111 {
3112 uint8_t u8Adv = (255 - pLnState->Hdr.u8OpcodeBase) / pLnState->Hdr.u8LineRange;
3113 if (pLnState->Hdr.cMaxOpsPerInstr <= 1)
3114 pLnState->Regs.uAddress += (uint32_t)pLnState->Hdr.cbMinInstr * u8Adv;
3115 else
3116 {
3117 pLnState->Regs.uAddress += (pLnState->Regs.idxOp + u8Adv) / pLnState->Hdr.cMaxOpsPerInstr
3118 * pLnState->Hdr.cbMinInstr;
3119 pLnState->Regs.idxOp = (pLnState->Regs.idxOp + u8Adv) % pLnState->Hdr.cMaxOpsPerInstr;
3120 }
3121 Log2(("%08x: DW_LNS_const_add_pc\n", offOpCode));
3122 break;
3123 }
3124 case DW_LNS_fixed_advance_pc:
3125 pLnState->Regs.uAddress += rtDwarfCursor_GetUHalf(pCursor, 0);
3126 pLnState->Regs.idxOp = 0;
3127 Log2(("%08x: DW_LNS_fixed_advance_pc\n", offOpCode));
3128 break;
3129
3130 case DW_LNS_set_prologue_end:
3131 pLnState->Regs.fPrologueEnd = true;
3132 Log2(("%08x: DW_LNS_set_prologue_end\n", offOpCode));
3133 break;
3134
3135 case DW_LNS_set_epilogue_begin:
3136 pLnState->Regs.fEpilogueBegin = true;
3137 Log2(("%08x: DW_LNS_set_epilogue_begin\n", offOpCode));
3138 break;
3139
3140 case DW_LNS_set_isa:
3141 pLnState->Regs.uIsa = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
3142 Log2(("%08x: DW_LNS_set_isa %#x\n", offOpCode, pLnState->Regs.uIsa));
3143 break;
3144
3145 default:
3146 {
3147 unsigned cOpsToSkip = pLnState->Hdr.pacStdOperands[bOpCode - 1];
3148 Log(("rtDwarfLine_RunProgram: Unknown standard opcode %#x, %#x operands, at %08x.\n", bOpCode, cOpsToSkip, offOpCode));
3149 while (cOpsToSkip-- > 0)
3150 rc = rtDwarfCursor_SkipLeb128(pCursor);
3151 break;
3152 }
3153
3154 /*
3155 * Extended opcode.
3156 */
3157 case DW_LNS_extended:
3158 {
3159 /* The instruction has a length prefix. */
3160 uint64_t cbInstr = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
3161 if (RT_FAILURE(pCursor->rc))
3162 return pCursor->rc;
3163 if (cbInstr > pCursor->cbUnitLeft)
3164 return VERR_DWARF_BAD_LNE;
3165 uint8_t const * const pbEndOfInstr = rtDwarfCursor_CalcPos(pCursor, cbInstr);
3166
3167 /* Get the opcode and deal with it if we know it. */
3168 bOpCode = rtDwarfCursor_GetUByte(pCursor, 0);
3169 switch (bOpCode)
3170 {
3171 case DW_LNE_end_sequence:
3172#if 0 /* No need for this, I think. */
3173 pLnState->Regs.fEndSequence = true;
3174 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
3175#endif
3176 rtDwarfLine_ResetState(pLnState);
3177 Log2(("%08x: DW_LNE_end_sequence\n", offOpCode));
3178 break;
3179
3180 case DW_LNE_set_address:
3181 pLnState->Regs.uAddress = rtDwarfCursor_GetVarSizedU(pCursor, cbInstr - 1, UINT64_MAX);
3182 pLnState->Regs.idxOp = 0;
3183 Log2(("%08x: DW_LNE_set_address: %#llx\n", offOpCode, pLnState->Regs.uAddress));
3184 break;
3185
3186 case DW_LNE_define_file:
3187 {
3188 const char *pszFilename = rtDwarfCursor_GetSZ(pCursor, NULL);
3189 uint32_t idxInc = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
3190 rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
3191 rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
3192 Log2(("%08x: DW_LNE_define_file: {%d}/%s\n", offOpCode, idxInc, pszFilename));
3193
3194 rc = rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
3195 if (RT_SUCCESS(rc))
3196 rc = rtDwarfLine_DefineFileName(pLnState, pszFilename, idxInc);
3197 break;
3198 }
3199
3200 /*
3201 * Note! Was defined in DWARF 4. But... Watcom used it
3202 * for setting the segment in DWARF 2, creating
3203 * an incompatibility with the newer standard.
3204 */
3205 case DW_LNE_set_descriminator:
3206 if (pLnState->Hdr.uVer != 2)
3207 {
3208 Assert(pLnState->Hdr.uVer >= 4);
3209 pLnState->Regs.uDiscriminator = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
3210 Log2(("%08x: DW_LNE_set_descriminator: %u\n", offOpCode, pLnState->Regs.uDiscriminator));
3211 }
3212 else
3213 {
3214 uint64_t uSeg = rtDwarfCursor_GetVarSizedU(pCursor, cbInstr - 1, UINT64_MAX);
3215 Log2(("%08x: DW_LNE_set_segment: %#llx, cbInstr=%#x - Watcom Extension\n", offOpCode, uSeg, cbInstr));
3216 pLnState->Regs.uSegment = (RTSEL)uSeg;
3217 AssertStmt(pLnState->Regs.uSegment == uSeg, rc = VERR_DWARF_BAD_INFO);
3218 }
3219 break;
3220
3221 default:
3222 Log(("rtDwarfLine_RunProgram: Unknown extended opcode %#x, length %#x at %08x\n", bOpCode, cbInstr, offOpCode));
3223 break;
3224 }
3225
3226 /* Advance the cursor to the end of the instruction . */
3227 rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
3228 break;
3229 }
3230 }
3231 }
3232
3233 /*
3234 * Check the status before looping.
3235 */
3236 if (RT_FAILURE(rc))
3237 return rc;
3238 if (RT_FAILURE(pCursor->rc))
3239 return pCursor->rc;
3240 }
3241 return rc;
3242}
3243
3244
3245/**
3246 * Reads the include directories for a line number unit.
3247 *
3248 * @returns IPRT status code
3249 * @param pLnState The line number program state.
3250 * @param pCursor The cursor.
3251 */
3252static int rtDwarfLine_ReadFileNames(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
3253{
3254 int rc = rtDwarfLine_DefineFileName(pLnState, "/<bad-zero-file-name-entry>", 0);
3255 if (RT_FAILURE(rc))
3256 return rc;
3257
3258 for (;;)
3259 {
3260 const char *psz = rtDwarfCursor_GetSZ(pCursor, NULL);
3261 if (!*psz)
3262 break;
3263
3264 uint64_t idxInc = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
3265 rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
3266 rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
3267
3268 rc = rtDwarfLine_DefineFileName(pLnState, psz, idxInc);
3269 if (RT_FAILURE(rc))
3270 return rc;
3271 }
3272 return pCursor->rc;
3273}
3274
3275
3276/**
3277 * Reads the include directories for a line number unit.
3278 *
3279 * @returns IPRT status code
3280 * @param pLnState The line number program state.
3281 * @param pCursor The cursor.
3282 */
3283static int rtDwarfLine_ReadIncludePaths(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
3284{
3285 const char *psz = ""; /* The zeroth is the unit dir. */
3286 for (;;)
3287 {
3288 if ((pLnState->cIncPaths % 2) == 0)
3289 {
3290 void *pv = RTMemRealloc(pLnState->papszIncPaths, sizeof(pLnState->papszIncPaths[0]) * (pLnState->cIncPaths + 2));
3291 if (!pv)
3292 return VERR_NO_MEMORY;
3293 pLnState->papszIncPaths = (const char **)pv;
3294 }
3295 Log((" Path #%02u = '%s'\n", pLnState->cIncPaths, psz));
3296 pLnState->papszIncPaths[pLnState->cIncPaths] = psz;
3297 pLnState->cIncPaths++;
3298
3299 psz = rtDwarfCursor_GetSZ(pCursor, NULL);
3300 if (!*psz)
3301 break;
3302 }
3303
3304 return pCursor->rc;
3305}
3306
3307
3308/**
3309 * Explodes the line number table for a compilation unit.
3310 *
3311 * @returns IPRT status code
3312 * @param pThis The DWARF instance.
3313 * @param pCursor The cursor to read the line number information
3314 * via.
3315 */
3316static int rtDwarfLine_ExplodeUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor)
3317{
3318 RTDWARFLINESTATE LnState;
3319 RT_ZERO(LnState);
3320 LnState.pDwarfMod = pThis;
3321
3322 /*
3323 * Parse the header.
3324 */
3325 rtDwarfCursor_GetInitalLength(pCursor);
3326 LnState.Hdr.uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
3327 if ( LnState.Hdr.uVer < 2
3328 || LnState.Hdr.uVer > 4)
3329 return rtDwarfCursor_SkipUnit(pCursor);
3330
3331 LnState.Hdr.offFirstOpcode = rtDwarfCursor_GetUOff(pCursor, 0);
3332 uint8_t const * const pbFirstOpcode = rtDwarfCursor_CalcPos(pCursor, LnState.Hdr.offFirstOpcode);
3333
3334 LnState.Hdr.cbMinInstr = rtDwarfCursor_GetUByte(pCursor, 0);
3335 if (LnState.Hdr.uVer >= 4)
3336 LnState.Hdr.cMaxOpsPerInstr = rtDwarfCursor_GetUByte(pCursor, 0);
3337 else
3338 LnState.Hdr.cMaxOpsPerInstr = 1;
3339 LnState.Hdr.u8DefIsStmt = rtDwarfCursor_GetUByte(pCursor, 0);
3340 LnState.Hdr.s8LineBase = rtDwarfCursor_GetSByte(pCursor, 0);
3341 LnState.Hdr.u8LineRange = rtDwarfCursor_GetUByte(pCursor, 0);
3342 LnState.Hdr.u8OpcodeBase = rtDwarfCursor_GetUByte(pCursor, 0);
3343
3344 if ( !LnState.Hdr.u8OpcodeBase
3345 || !LnState.Hdr.cMaxOpsPerInstr
3346 || !LnState.Hdr.u8LineRange
3347 || LnState.Hdr.u8DefIsStmt > 1)
3348 return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
3349 Log2(("DWARF Line number header:\n"
3350 " uVer %d\n"
3351 " offFirstOpcode %#llx\n"
3352 " cbMinInstr %u\n"
3353 " cMaxOpsPerInstr %u\n"
3354 " u8DefIsStmt %u\n"
3355 " s8LineBase %d\n"
3356 " u8LineRange %u\n"
3357 " u8OpcodeBase %u\n",
3358 LnState.Hdr.uVer, LnState.Hdr.offFirstOpcode, LnState.Hdr.cbMinInstr, LnState.Hdr.cMaxOpsPerInstr,
3359 LnState.Hdr.u8DefIsStmt, LnState.Hdr.s8LineBase, LnState.Hdr.u8LineRange, LnState.Hdr.u8OpcodeBase));
3360
3361 LnState.Hdr.pacStdOperands = pCursor->pb;
3362 for (uint8_t iStdOpcode = 1; iStdOpcode < LnState.Hdr.u8OpcodeBase; iStdOpcode++)
3363 rtDwarfCursor_GetUByte(pCursor, 0);
3364
3365 int rc = pCursor->rc;
3366 if (RT_SUCCESS(rc))
3367 rc = rtDwarfLine_ReadIncludePaths(&LnState, pCursor);
3368 if (RT_SUCCESS(rc))
3369 rc = rtDwarfLine_ReadFileNames(&LnState, pCursor);
3370
3371 /*
3372 * Run the program....
3373 */
3374 if (RT_SUCCESS(rc))
3375 rc = rtDwarfCursor_AdvanceToPos(pCursor, pbFirstOpcode);
3376 if (RT_SUCCESS(rc))
3377 rc = rtDwarfLine_RunProgram(&LnState, pCursor);
3378
3379 /*
3380 * Clean up.
3381 */
3382 size_t i = LnState.cFileNames;
3383 while (i-- > 0)
3384 RTStrFree(LnState.papszFileNames[i]);
3385 RTMemFree(LnState.papszFileNames);
3386 RTMemFree(LnState.papszIncPaths);
3387
3388 Assert(rtDwarfCursor_IsAtEndOfUnit(pCursor) || RT_FAILURE(rc));
3389 return rc;
3390}
3391
3392
3393/**
3394 * Explodes the line number table.
3395 *
3396 * The line numbers are insered into the debug info container.
3397 *
3398 * @returns IPRT status code
3399 * @param pThis The DWARF instance.
3400 */
3401static int rtDwarfLine_ExplodeAll(PRTDBGMODDWARF pThis)
3402{
3403 if (!pThis->aSections[krtDbgModDwarfSect_line].fPresent)
3404 return VINF_SUCCESS;
3405
3406 RTDWARFCURSOR Cursor;
3407 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_line);
3408 if (RT_FAILURE(rc))
3409 return rc;
3410
3411 while ( !rtDwarfCursor_IsAtEnd(&Cursor)
3412 && RT_SUCCESS(rc))
3413 rc = rtDwarfLine_ExplodeUnit(pThis, &Cursor);
3414
3415 return rtDwarfCursor_Delete(&Cursor, rc);
3416}
3417
3418
3419/*
3420 *
3421 * DWARF Abbreviations.
3422 * DWARF Abbreviations.
3423 * DWARF Abbreviations.
3424 *
3425 */
3426
3427/**
3428 * Deals with a cache miss in rtDwarfAbbrev_Lookup.
3429 *
3430 * @returns Pointer to abbreviation cache entry (read only). May be rendered
3431 * invalid by subsequent calls to this function.
3432 * @param pThis The DWARF instance.
3433 * @param uCode The abbreviation code to lookup.
3434 */
3435static PCRTDWARFABBREV rtDwarfAbbrev_LookupMiss(PRTDBGMODDWARF pThis, uint32_t uCode)
3436{
3437 /*
3438 * There is no entry with code zero.
3439 */
3440 if (!uCode)
3441 return NULL;
3442
3443 /*
3444 * Resize the cache array if the code is considered cachable.
3445 */
3446 bool fFillCache = true;
3447 if (pThis->cCachedAbbrevsAlloced < uCode)
3448 {
3449 if (uCode >= _64K)
3450 fFillCache = false;
3451 else
3452 {
3453 uint32_t cNew = RT_ALIGN(uCode, 64);
3454 void *pv = RTMemRealloc(pThis->paCachedAbbrevs, sizeof(pThis->paCachedAbbrevs[0]) * cNew);
3455 if (!pv)
3456 fFillCache = false;
3457 else
3458 {
3459 Log(("rtDwarfAbbrev_LookupMiss: Growing from %u to %u...\n", pThis->cCachedAbbrevsAlloced, cNew));
3460 pThis->paCachedAbbrevs = (PRTDWARFABBREV)pv;
3461 for (uint32_t i = pThis->cCachedAbbrevsAlloced; i < cNew; i++)
3462 pThis->paCachedAbbrevs[i].offAbbrev = UINT32_MAX;
3463 pThis->cCachedAbbrevsAlloced = cNew;
3464 }
3465 }
3466 }
3467
3468 /*
3469 * Walk the abbreviations till we find the desired code.
3470 */
3471 RTDWARFCURSOR Cursor;
3472 int rc = rtDwarfCursor_InitWithOffset(&Cursor, pThis, krtDbgModDwarfSect_abbrev, pThis->offCachedAbbrev);
3473 if (RT_FAILURE(rc))
3474 return NULL;
3475
3476 PRTDWARFABBREV pRet = NULL;
3477 if (fFillCache)
3478 {
3479 /*
3480 * Search for the entry and fill the cache while doing so.
3481 * We assume that abbreviation codes for a unit will stop when we see
3482 * zero code or when the code value drops.
3483 */
3484 uint32_t uPrevCode = 0;
3485 for (;;)
3486 {
3487 /* Read the 'header'. Skipping zero code bytes. */
3488 uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
3489 if (pRet && (uCurCode == 0 || uCurCode < uPrevCode))
3490 break; /* probably end of unit. */
3491 if (uCurCode != 0)
3492 {
3493 uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
3494 uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
3495 if (RT_FAILURE(Cursor.rc))
3496 break;
3497 if ( uCurTag > 0xffff
3498 || uChildren > 1)
3499 {
3500 Cursor.rc = VERR_DWARF_BAD_ABBREV;
3501 break;
3502 }
3503
3504 /* Cache it? */
3505 if (uCurCode <= pThis->cCachedAbbrevsAlloced)
3506 {
3507 PRTDWARFABBREV pEntry = &pThis->paCachedAbbrevs[uCurCode - 1];
3508 if (pEntry->offAbbrev != pThis->offCachedAbbrev)
3509 {
3510 pEntry->offAbbrev = pThis->offCachedAbbrev;
3511 pEntry->fChildren = RT_BOOL(uChildren);
3512 pEntry->uTag = uCurTag;
3513 pEntry->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
3514
3515 if (uCurCode == uCode)
3516 {
3517 Assert(!pRet);
3518 pRet = pEntry;
3519 if (uCurCode == pThis->cCachedAbbrevsAlloced)
3520 break;
3521 }
3522 }
3523 else if (pRet)
3524 break; /* Next unit, don't cache more. */
3525 /* else: We're growing the cache and re-reading old data. */
3526 }
3527
3528 /* Skip the specification. */
3529 uint32_t uAttr, uForm;
3530 do
3531 {
3532 uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
3533 uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
3534 } while (uAttr != 0);
3535 }
3536 if (RT_FAILURE(Cursor.rc))
3537 break;
3538
3539 /* Done? (Maximize cache filling.) */
3540 if ( pRet != NULL
3541 && uCurCode >= pThis->cCachedAbbrevsAlloced)
3542 break;
3543 uPrevCode = uCurCode;
3544 }
3545 }
3546 else
3547 {
3548 /*
3549 * Search for the entry with the desired code, no cache filling.
3550 */
3551 for (;;)
3552 {
3553 /* Read the 'header'. */
3554 uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
3555 uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
3556 uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
3557 if (RT_FAILURE(Cursor.rc))
3558 break;
3559 if ( uCurTag > 0xffff
3560 || uChildren > 1)
3561 {
3562 Cursor.rc = VERR_DWARF_BAD_ABBREV;
3563 break;
3564 }
3565
3566 /* Do we have a match? */
3567 if (uCurCode == uCode)
3568 {
3569 pRet = &pThis->LookupAbbrev;
3570 pRet->fChildren = RT_BOOL(uChildren);
3571 pRet->uTag = uCurTag;
3572 pRet->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
3573 pRet->offAbbrev = pThis->offCachedAbbrev;
3574 break;
3575 }
3576
3577 /* Skip the specification. */
3578 uint32_t uAttr, uForm;
3579 do
3580 {
3581 uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
3582 uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
3583 } while (uAttr != 0);
3584 if (RT_FAILURE(Cursor.rc))
3585 break;
3586 }
3587 }
3588
3589 rtDwarfCursor_Delete(&Cursor, VINF_SUCCESS);
3590 return pRet;
3591}
3592
3593
3594/**
3595 * Looks up an abbreviation.
3596 *
3597 * @returns Pointer to abbreviation cache entry (read only). May be rendered
3598 * invalid by subsequent calls to this function.
3599 * @param pThis The DWARF instance.
3600 * @param uCode The abbreviation code to lookup.
3601 */
3602static PCRTDWARFABBREV rtDwarfAbbrev_Lookup(PRTDBGMODDWARF pThis, uint32_t uCode)
3603{
3604 if ( uCode - 1 >= pThis->cCachedAbbrevsAlloced
3605 || pThis->paCachedAbbrevs[uCode - 1].offAbbrev != pThis->offCachedAbbrev)
3606 return rtDwarfAbbrev_LookupMiss(pThis, uCode);
3607 return &pThis->paCachedAbbrevs[uCode - 1];
3608}
3609
3610
3611/**
3612 * Sets the abbreviation offset of the current unit.
3613 *
3614 * @param pThis The DWARF instance.
3615 * @param offAbbrev The offset into the abbreviation section.
3616 */
3617static void rtDwarfAbbrev_SetUnitOffset(PRTDBGMODDWARF pThis, uint32_t offAbbrev)
3618{
3619 pThis->offCachedAbbrev = offAbbrev;
3620}
3621
3622
3623
3624/*
3625 *
3626 * DIE Attribute Parsers.
3627 * DIE Attribute Parsers.
3628 * DIE Attribute Parsers.
3629 *
3630 */
3631
3632/**
3633 * Gets the compilation unit a DIE belongs to.
3634 *
3635 * @returns The compilation unit DIE.
3636 * @param pDie Some DIE in the unit.
3637 */
3638static PRTDWARFDIECOMPILEUNIT rtDwarfDie_GetCompileUnit(PRTDWARFDIE pDie)
3639{
3640 while (pDie->pParent)
3641 pDie = pDie->pParent;
3642 AssertReturn( pDie->uTag == DW_TAG_compile_unit
3643 || pDie->uTag == DW_TAG_partial_unit,
3644 NULL);
3645 return (PRTDWARFDIECOMPILEUNIT)pDie;
3646}
3647
3648
3649/**
3650 * Resolves a string section (debug_str) reference.
3651 *
3652 * @returns Pointer to the string (inside the string section).
3653 * @param pThis The DWARF instance.
3654 * @param pCursor The cursor.
3655 * @param pszErrValue What to return on failure (@a
3656 * pCursor->rc is set).
3657 */
3658static const char *rtDwarfDecodeHlp_GetStrp(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor, const char *pszErrValue)
3659{
3660 uint64_t offDebugStr = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
3661 if (RT_FAILURE(pCursor->rc))
3662 return pszErrValue;
3663
3664 if (offDebugStr >= pThis->aSections[krtDbgModDwarfSect_str].cb)
3665 {
3666 /* Ugly: Exploit the cursor status field for reporting errors. */
3667 pCursor->rc = VERR_DWARF_BAD_INFO;
3668 return pszErrValue;
3669 }
3670
3671 if (!pThis->aSections[krtDbgModDwarfSect_str].pv)
3672 {
3673 int rc = rtDbgModDwarfLoadSection(pThis, krtDbgModDwarfSect_str);
3674 if (RT_FAILURE(rc))
3675 {
3676 /* Ugly: Exploit the cursor status field for reporting errors. */
3677 pCursor->rc = rc;
3678 return pszErrValue;
3679 }
3680 }
3681
3682 return (const char *)pThis->aSections[krtDbgModDwarfSect_str].pv + (size_t)offDebugStr;
3683}
3684
3685
3686/** @callback_method_impl{FNRTDWARFATTRDECODER} */
3687static DECLCALLBACK(int) rtDwarfDecode_Address(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
3688 uint32_t uForm, PRTDWARFCURSOR pCursor)
3689{
3690 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDR), VERR_INTERNAL_ERROR_3);
3691 NOREF(pDie);
3692
3693 uint64_t uAddr;
3694 switch (uForm)
3695 {
3696 case DW_FORM_addr: uAddr = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
3697 case DW_FORM_data1: uAddr = rtDwarfCursor_GetU8(pCursor, 0); break;
3698 case DW_FORM_data2: uAddr = rtDwarfCursor_GetU16(pCursor, 0); break;
3699 case DW_FORM_data4: uAddr = rtDwarfCursor_GetU32(pCursor, 0); break;
3700 case DW_FORM_data8: uAddr = rtDwarfCursor_GetU64(pCursor, 0); break;
3701 case DW_FORM_udata: uAddr = rtDwarfCursor_GetULeb128(pCursor, 0); break;
3702 default:
3703 AssertMsgFailedReturn(("%#x (%s)\n", uForm, rtDwarfLog_FormName(uForm)), VERR_DWARF_UNEXPECTED_FORM);
3704 }
3705 if (RT_FAILURE(pCursor->rc))
3706 return pCursor->rc;
3707
3708 PRTDWARFADDR pAddr = (PRTDWARFADDR)pbMember;
3709 pAddr->uAddress = uAddr;
3710
3711 Log4((" %-20s %#010llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), uAddr, rtDwarfLog_FormName(uForm)));
3712 return VINF_SUCCESS;
3713}
3714
3715
3716/** @callback_method_impl{FNRTDWARFATTRDECODER} */
3717static DECLCALLBACK(int) rtDwarfDecode_Bool(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
3718 uint32_t uForm, PRTDWARFCURSOR pCursor)
3719{
3720 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(bool), VERR_INTERNAL_ERROR_3);
3721 NOREF(pDie);
3722
3723 bool *pfMember = (bool *)pbMember;
3724 switch (uForm)
3725 {
3726 case DW_FORM_flag:
3727 {
3728 uint8_t b = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
3729 if (b > 1)
3730 {
3731 Log(("Unexpected boolean value %#x\n", b));
3732 return RT_FAILURE(pCursor->rc) ? pCursor->rc : pCursor->rc = VERR_DWARF_BAD_INFO;
3733 }
3734 *pfMember = RT_BOOL(b);
3735 break;
3736 }
3737
3738 case DW_FORM_flag_present:
3739 *pfMember = true;
3740 break;
3741
3742 default:
3743 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
3744 }
3745
3746 Log4((" %-20s %RTbool [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), *pfMember, rtDwarfLog_FormName(uForm)));
3747 return VINF_SUCCESS;
3748}
3749
3750
3751/** @callback_method_impl{FNRTDWARFATTRDECODER} */
3752static DECLCALLBACK(int) rtDwarfDecode_LowHighPc(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
3753 uint32_t uForm, PRTDWARFCURSOR pCursor)
3754{
3755 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDRRANGE), VERR_INTERNAL_ERROR_3);
3756 AssertReturn(pDesc->uAttr == DW_AT_low_pc || pDesc->uAttr == DW_AT_high_pc, VERR_INTERNAL_ERROR_3);
3757 NOREF(pDie);
3758
3759 uint64_t uAddr;
3760 switch (uForm)
3761 {
3762 case DW_FORM_addr: uAddr = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
3763 case DW_FORM_data1: uAddr = rtDwarfCursor_GetU8(pCursor, 0); break;
3764 case DW_FORM_data2: uAddr = rtDwarfCursor_GetU16(pCursor, 0); break;
3765 case DW_FORM_data4: uAddr = rtDwarfCursor_GetU32(pCursor, 0); break;
3766 case DW_FORM_data8: uAddr = rtDwarfCursor_GetU64(pCursor, 0); break;
3767 case DW_FORM_udata: uAddr = rtDwarfCursor_GetULeb128(pCursor, 0); break;
3768 default:
3769 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
3770 }
3771 if (RT_FAILURE(pCursor->rc))
3772 return pCursor->rc;
3773
3774 PRTDWARFADDRRANGE pRange = (PRTDWARFADDRRANGE)pbMember;
3775 if (pDesc->uAttr == DW_AT_low_pc)
3776 {
3777 if (pRange->fHaveLowAddress)
3778 {
3779 Log(("rtDwarfDecode_LowHighPc: Duplicate DW_AT_low_pc\n"));
3780 return pCursor->rc = VERR_DWARF_BAD_INFO;
3781 }
3782 pRange->fHaveLowAddress = true;
3783 pRange->uLowAddress = uAddr;
3784 }
3785 else
3786 {
3787 if (pRange->fHaveHighAddress)
3788 {
3789 Log(("rtDwarfDecode_LowHighPc: Duplicate DW_AT_high_pc\n"));
3790 return pCursor->rc = VERR_DWARF_BAD_INFO;
3791 }
3792 pRange->fHaveHighAddress = true;
3793 pRange->fHaveHighIsAddress = uForm == DW_FORM_addr;
3794 if (!pRange->fHaveHighIsAddress && pRange->fHaveLowAddress)
3795 {
3796 pRange->fHaveHighIsAddress = true;
3797 pRange->uHighAddress = uAddr + pRange->uLowAddress;
3798 }
3799 else
3800 pRange->uHighAddress = uAddr;
3801
3802 }
3803 pRange->cAttrs++;
3804
3805 Log4((" %-20s %#010llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), uAddr, rtDwarfLog_FormName(uForm)));
3806 return VINF_SUCCESS;
3807}
3808
3809
3810/** @callback_method_impl{FNRTDWARFATTRDECODER} */
3811static DECLCALLBACK(int) rtDwarfDecode_Ranges(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
3812 uint32_t uForm, PRTDWARFCURSOR pCursor)
3813{
3814 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDRRANGE), VERR_INTERNAL_ERROR_3);
3815 AssertReturn(pDesc->uAttr == DW_AT_ranges, VERR_INTERNAL_ERROR_3);
3816 NOREF(pDie);
3817
3818 /* Decode it. */
3819 uint64_t off;
3820 switch (uForm)
3821 {
3822 case DW_FORM_addr: off = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
3823 case DW_FORM_data4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
3824 case DW_FORM_data8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
3825 case DW_FORM_sec_offset: off = rtDwarfCursor_GetUOff(pCursor, 0); break;
3826 default:
3827 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
3828 }
3829 if (RT_FAILURE(pCursor->rc))
3830 return pCursor->rc;
3831
3832 /* Validate the offset and load the ranges. */
3833 PRTDBGMODDWARF pThis = pCursor->pDwarfMod;
3834 if (off >= pThis->aSections[krtDbgModDwarfSect_ranges].cb)
3835 {
3836 Log(("rtDwarfDecode_Ranges: bad ranges off=%#llx\n", off));
3837 return pCursor->rc = VERR_DWARF_BAD_POS;
3838 }
3839
3840 if (!pThis->aSections[krtDbgModDwarfSect_ranges].pv)
3841 {
3842 int rc = rtDbgModDwarfLoadSection(pThis, krtDbgModDwarfSect_ranges);
3843 if (RT_FAILURE(rc))
3844 return pCursor->rc = rc;
3845 }
3846
3847 /* Store the result. */
3848 PRTDWARFADDRRANGE pRange = (PRTDWARFADDRRANGE)pbMember;
3849 if (pRange->fHaveRanges)
3850 {
3851 Log(("rtDwarfDecode_Ranges: Duplicate DW_AT_ranges\n"));
3852 return pCursor->rc = VERR_DWARF_BAD_INFO;
3853 }
3854 pRange->fHaveRanges = true;
3855 pRange->cAttrs++;
3856 pRange->pbRanges = (uint8_t const *)pThis->aSections[krtDbgModDwarfSect_ranges].pv + (size_t)off;
3857
3858 Log4((" %-20s TODO [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), rtDwarfLog_FormName(uForm)));
3859 return VINF_SUCCESS;
3860}
3861
3862
3863/** @callback_method_impl{FNRTDWARFATTRDECODER} */
3864static DECLCALLBACK(int) rtDwarfDecode_Reference(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
3865 uint32_t uForm, PRTDWARFCURSOR pCursor)
3866{
3867 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFREF), VERR_INTERNAL_ERROR_3);
3868
3869 /* Decode it. */
3870 uint64_t off;
3871 krtDwarfRef enmWrt = krtDwarfRef_SameUnit;
3872 switch (uForm)
3873 {
3874 case DW_FORM_ref1: off = rtDwarfCursor_GetU8(pCursor, 0); break;
3875 case DW_FORM_ref2: off = rtDwarfCursor_GetU16(pCursor, 0); break;
3876 case DW_FORM_ref4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
3877 case DW_FORM_ref8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
3878 case DW_FORM_ref_udata: off = rtDwarfCursor_GetULeb128(pCursor, 0); break;
3879
3880 case DW_FORM_ref_addr:
3881 enmWrt = krtDwarfRef_InfoSection;
3882 off = rtDwarfCursor_GetUOff(pCursor, 0);
3883 break;
3884
3885 case DW_FORM_ref_sig8:
3886 enmWrt = krtDwarfRef_TypeId64;
3887 off = rtDwarfCursor_GetU64(pCursor, 0);
3888 break;
3889
3890 default:
3891 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
3892 }
3893 if (RT_FAILURE(pCursor->rc))
3894 return pCursor->rc;
3895
3896 /* Validate the offset and convert to debug_info relative offsets. */
3897 if (enmWrt == krtDwarfRef_InfoSection)
3898 {
3899 if (off >= pCursor->pDwarfMod->aSections[krtDbgModDwarfSect_info].cb)
3900 {
3901 Log(("rtDwarfDecode_Reference: bad info off=%#llx\n", off));
3902 return pCursor->rc = VERR_DWARF_BAD_POS;
3903 }
3904 }
3905 else if (enmWrt == krtDwarfRef_SameUnit)
3906 {
3907 PRTDWARFDIECOMPILEUNIT pUnit = rtDwarfDie_GetCompileUnit(pDie);
3908 if (off >= pUnit->cbUnit)
3909 {
3910 Log(("rtDwarfDecode_Reference: bad unit off=%#llx\n", off));
3911 return pCursor->rc = VERR_DWARF_BAD_POS;
3912 }
3913 off += pUnit->offUnit;
3914 enmWrt = krtDwarfRef_InfoSection;
3915 }
3916 /* else: not bother verifying/resolving the indirect type reference yet. */
3917
3918 /* Store it */
3919 PRTDWARFREF pRef = (PRTDWARFREF)pbMember;
3920 pRef->enmWrt = enmWrt;
3921 pRef->off = off;
3922
3923 Log4((" %-20s %d:%#010llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), enmWrt, off, rtDwarfLog_FormName(uForm)));
3924 return VINF_SUCCESS;
3925}
3926
3927
3928/** @callback_method_impl{FNRTDWARFATTRDECODER} */
3929static DECLCALLBACK(int) rtDwarfDecode_SectOff(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
3930 uint32_t uForm, PRTDWARFCURSOR pCursor)
3931{
3932 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFREF), VERR_INTERNAL_ERROR_3);
3933 NOREF(pDie);
3934
3935 uint64_t off;
3936 switch (uForm)
3937 {
3938 case DW_FORM_data4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
3939 case DW_FORM_data8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
3940 case DW_FORM_sec_offset: off = rtDwarfCursor_GetUOff(pCursor, 0); break;
3941 default:
3942 AssertMsgFailedReturn(("%#x (%s)\n", uForm, rtDwarfLog_FormName(uForm)), VERR_DWARF_UNEXPECTED_FORM);
3943 }
3944 if (RT_FAILURE(pCursor->rc))
3945 return pCursor->rc;
3946
3947 krtDbgModDwarfSect enmSect;
3948 krtDwarfRef enmWrt;
3949 switch (pDesc->uAttr)
3950 {
3951 case DW_AT_stmt_list: enmSect = krtDbgModDwarfSect_line; enmWrt = krtDwarfRef_LineSection; break;
3952 case DW_AT_macro_info: enmSect = krtDbgModDwarfSect_loc; enmWrt = krtDwarfRef_LocSection; break;
3953 case DW_AT_ranges: enmSect = krtDbgModDwarfSect_ranges; enmWrt = krtDwarfRef_RangesSection; break;
3954 default:
3955 AssertMsgFailedReturn(("%u (%s)\n", pDesc->uAttr, rtDwarfLog_AttrName(pDesc->uAttr)), VERR_INTERNAL_ERROR_4);
3956 }
3957 size_t cbSect = pCursor->pDwarfMod->aSections[enmSect].cb;
3958 if (off >= cbSect)
3959 {
3960 /* Watcom generates offset past the end of the section, increasing the
3961 offset by one for each compile unit. So, just fudge it. */
3962 Log(("rtDwarfDecode_SectOff: bad off=%#llx, attr %#x (%s), enmSect=%d cb=%#llx; Assuming watcom/gcc.\n", off,
3963 pDesc->uAttr, rtDwarfLog_AttrName(pDesc->uAttr), enmSect, cbSect));
3964 off = cbSect;
3965 }
3966
3967 PRTDWARFREF pRef = (PRTDWARFREF)pbMember;
3968 pRef->enmWrt = enmWrt;
3969 pRef->off = off;
3970
3971 Log4((" %-20s %d:%#010llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), enmWrt, off, rtDwarfLog_FormName(uForm)));
3972 return VINF_SUCCESS;
3973}
3974
3975
3976/** @callback_method_impl{FNRTDWARFATTRDECODER} */
3977static DECLCALLBACK(int) rtDwarfDecode_String(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
3978 uint32_t uForm, PRTDWARFCURSOR pCursor)
3979{
3980 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(const char *), VERR_INTERNAL_ERROR_3);
3981 NOREF(pDie);
3982
3983 const char *psz;
3984 switch (uForm)
3985 {
3986 case DW_FORM_string:
3987 psz = rtDwarfCursor_GetSZ(pCursor, NULL);
3988 break;
3989
3990 case DW_FORM_strp:
3991 psz = rtDwarfDecodeHlp_GetStrp(pCursor->pDwarfMod, pCursor, NULL);
3992 break;
3993
3994 default:
3995 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
3996 }
3997
3998 *(const char **)pbMember = psz;
3999 Log4((" %-20s '%s' [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr), psz, rtDwarfLog_FormName(uForm)));
4000 return pCursor->rc;
4001}
4002
4003
4004/** @callback_method_impl{FNRTDWARFATTRDECODER} */
4005static DECLCALLBACK(int) rtDwarfDecode_UnsignedInt(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
4006 uint32_t uForm, PRTDWARFCURSOR pCursor)
4007{
4008 NOREF(pDie);
4009 uint64_t u64Val;
4010 switch (uForm)
4011 {
4012 case DW_FORM_udata: u64Val = rtDwarfCursor_GetULeb128(pCursor, 0); break;
4013 case DW_FORM_data1: u64Val = rtDwarfCursor_GetU8(pCursor, 0); break;
4014 case DW_FORM_data2: u64Val = rtDwarfCursor_GetU16(pCursor, 0); break;
4015 case DW_FORM_data4: u64Val = rtDwarfCursor_GetU32(pCursor, 0); break;
4016 case DW_FORM_data8: u64Val = rtDwarfCursor_GetU64(pCursor, 0); break;
4017 default:
4018 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
4019 }
4020 if (RT_FAILURE(pCursor->rc))
4021 return pCursor->rc;
4022
4023 switch (ATTR_GET_SIZE(pDesc))
4024 {
4025 case 1:
4026 *pbMember = (uint8_t)u64Val;
4027 if (*pbMember != u64Val)
4028 {
4029 AssertFailed();
4030 return VERR_OUT_OF_RANGE;
4031 }
4032 break;
4033
4034 case 2:
4035 *(uint16_t *)pbMember = (uint16_t)u64Val;
4036 if (*(uint16_t *)pbMember != u64Val)
4037 {
4038 AssertFailed();
4039 return VERR_OUT_OF_RANGE;
4040 }
4041 break;
4042
4043 case 4:
4044 *(uint32_t *)pbMember = (uint32_t)u64Val;
4045 if (*(uint32_t *)pbMember != u64Val)
4046 {
4047 AssertFailed();
4048 return VERR_OUT_OF_RANGE;
4049 }
4050 break;
4051
4052 case 8:
4053 *(uint64_t *)pbMember = (uint64_t)u64Val;
4054 if (*(uint64_t *)pbMember != u64Val)
4055 {
4056 AssertFailed();
4057 return VERR_OUT_OF_RANGE;
4058 }
4059 break;
4060
4061 default:
4062 AssertMsgFailedReturn(("%#x\n", ATTR_GET_SIZE(pDesc)), VERR_INTERNAL_ERROR_2);
4063 }
4064 return VINF_SUCCESS;
4065}
4066
4067
4068/**
4069 * Initialize location interpreter state from cursor & form.
4070 *
4071 * @returns IPRT status code.
4072 * @retval VERR_NOT_FOUND if no location information (i.e. there is source but
4073 * it resulted in no byte code).
4074 * @param pLoc The location state structure to initialize.
4075 * @param pCursor The cursor to read from.
4076 * @param uForm The attribute form.
4077 */
4078static int rtDwarfLoc_Init(PRTDWARFLOCST pLoc, PRTDWARFCURSOR pCursor, uint32_t uForm)
4079{
4080 uint32_t cbBlock;
4081 switch (uForm)
4082 {
4083 case DW_FORM_block1:
4084 cbBlock = rtDwarfCursor_GetU8(pCursor, 0);
4085 break;
4086
4087 case DW_FORM_block2:
4088 cbBlock = rtDwarfCursor_GetU16(pCursor, 0);
4089 break;
4090
4091 case DW_FORM_block4:
4092 cbBlock = rtDwarfCursor_GetU32(pCursor, 0);
4093 break;
4094
4095 case DW_FORM_block:
4096 cbBlock = rtDwarfCursor_GetULeb128(pCursor, 0);
4097 break;
4098
4099 default:
4100 AssertMsgFailedReturn(("uForm=%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
4101 }
4102 if (!cbBlock)
4103 return VERR_NOT_FOUND;
4104
4105 int rc = rtDwarfCursor_InitForBlock(&pLoc->Cursor, pCursor, cbBlock);
4106 if (RT_FAILURE(rc))
4107 return rc;
4108 pLoc->iTop = -1;
4109 return VINF_SUCCESS;
4110}
4111
4112
4113/**
4114 * Pushes a value onto the stack.
4115 *
4116 * @returns VINF_SUCCESS or VERR_DWARF_STACK_OVERFLOW.
4117 * @param pLoc The state.
4118 * @param uValue The value to push.
4119 */
4120static int rtDwarfLoc_Push(PRTDWARFLOCST pLoc, uint64_t uValue)
4121{
4122 int iTop = pLoc->iTop + 1;
4123 AssertReturn((unsigned)iTop < RT_ELEMENTS(pLoc->auStack), VERR_DWARF_STACK_OVERFLOW);
4124 pLoc->auStack[iTop] = uValue;
4125 pLoc->iTop = iTop;
4126 return VINF_SUCCESS;
4127}
4128
4129
4130static int rtDwarfLoc_Evaluate(PRTDWARFLOCST pLoc, void *pvLater, void *pvUser)
4131{
4132 RT_NOREF_PV(pvLater); RT_NOREF_PV(pvUser);
4133
4134 while (!rtDwarfCursor_IsAtEndOfUnit(&pLoc->Cursor))
4135 {
4136 /* Read the next opcode.*/
4137 uint8_t const bOpcode = rtDwarfCursor_GetU8(&pLoc->Cursor, 0);
4138
4139 /* Get its operands. */
4140 uint64_t uOperand1 = 0;
4141 uint64_t uOperand2 = 0;
4142 switch (bOpcode)
4143 {
4144 case DW_OP_addr:
4145 uOperand1 = rtDwarfCursor_GetNativeUOff(&pLoc->Cursor, 0);
4146 break;
4147 case DW_OP_pick:
4148 case DW_OP_const1u:
4149 case DW_OP_deref_size:
4150 case DW_OP_xderef_size:
4151 uOperand1 = rtDwarfCursor_GetU8(&pLoc->Cursor, 0);
4152 break;
4153 case DW_OP_const1s:
4154 uOperand1 = (int8_t)rtDwarfCursor_GetU8(&pLoc->Cursor, 0);
4155 break;
4156 case DW_OP_const2u:
4157 uOperand1 = rtDwarfCursor_GetU16(&pLoc->Cursor, 0);
4158 break;
4159 case DW_OP_skip:
4160 case DW_OP_bra:
4161 case DW_OP_const2s:
4162 uOperand1 = (int16_t)rtDwarfCursor_GetU16(&pLoc->Cursor, 0);
4163 break;
4164 case DW_OP_const4u:
4165 uOperand1 = rtDwarfCursor_GetU32(&pLoc->Cursor, 0);
4166 break;
4167 case DW_OP_const4s:
4168 uOperand1 = (int32_t)rtDwarfCursor_GetU32(&pLoc->Cursor, 0);
4169 break;
4170 case DW_OP_const8u:
4171 uOperand1 = rtDwarfCursor_GetU64(&pLoc->Cursor, 0);
4172 break;
4173 case DW_OP_const8s:
4174 uOperand1 = rtDwarfCursor_GetU64(&pLoc->Cursor, 0);
4175 break;
4176 case DW_OP_regx:
4177 case DW_OP_piece:
4178 case DW_OP_plus_uconst:
4179 case DW_OP_constu:
4180 uOperand1 = rtDwarfCursor_GetULeb128(&pLoc->Cursor, 0);
4181 break;
4182 case DW_OP_consts:
4183 case DW_OP_fbreg:
4184 case DW_OP_breg0+0: case DW_OP_breg0+1: case DW_OP_breg0+2: case DW_OP_breg0+3:
4185 case DW_OP_breg0+4: case DW_OP_breg0+5: case DW_OP_breg0+6: case DW_OP_breg0+7:
4186 case DW_OP_breg0+8: case DW_OP_breg0+9: case DW_OP_breg0+10: case DW_OP_breg0+11:
4187 case DW_OP_breg0+12: case DW_OP_breg0+13: case DW_OP_breg0+14: case DW_OP_breg0+15:
4188 case DW_OP_breg0+16: case DW_OP_breg0+17: case DW_OP_breg0+18: case DW_OP_breg0+19:
4189 case DW_OP_breg0+20: case DW_OP_breg0+21: case DW_OP_breg0+22: case DW_OP_breg0+23:
4190 case DW_OP_breg0+24: case DW_OP_breg0+25: case DW_OP_breg0+26: case DW_OP_breg0+27:
4191 case DW_OP_breg0+28: case DW_OP_breg0+29: case DW_OP_breg0+30: case DW_OP_breg0+31:
4192 uOperand1 = rtDwarfCursor_GetSLeb128(&pLoc->Cursor, 0);
4193 break;
4194 case DW_OP_bregx:
4195 uOperand1 = rtDwarfCursor_GetULeb128(&pLoc->Cursor, 0);
4196 uOperand2 = rtDwarfCursor_GetSLeb128(&pLoc->Cursor, 0);
4197 break;
4198 }
4199 if (RT_FAILURE(pLoc->Cursor.rc))
4200 break;
4201
4202 /* Interpret the opcode. */
4203 int rc;
4204 switch (bOpcode)
4205 {
4206 case DW_OP_const1u:
4207 case DW_OP_const1s:
4208 case DW_OP_const2u:
4209 case DW_OP_const2s:
4210 case DW_OP_const4u:
4211 case DW_OP_const4s:
4212 case DW_OP_const8u:
4213 case DW_OP_const8s:
4214 case DW_OP_constu:
4215 case DW_OP_consts:
4216 case DW_OP_addr:
4217 rc = rtDwarfLoc_Push(pLoc, uOperand1);
4218 break;
4219 case DW_OP_lit0 + 0: case DW_OP_lit0 + 1: case DW_OP_lit0 + 2: case DW_OP_lit0 + 3:
4220 case DW_OP_lit0 + 4: case DW_OP_lit0 + 5: case DW_OP_lit0 + 6: case DW_OP_lit0 + 7:
4221 case DW_OP_lit0 + 8: case DW_OP_lit0 + 9: case DW_OP_lit0 + 10: case DW_OP_lit0 + 11:
4222 case DW_OP_lit0 + 12: case DW_OP_lit0 + 13: case DW_OP_lit0 + 14: case DW_OP_lit0 + 15:
4223 case DW_OP_lit0 + 16: case DW_OP_lit0 + 17: case DW_OP_lit0 + 18: case DW_OP_lit0 + 19:
4224 case DW_OP_lit0 + 20: case DW_OP_lit0 + 21: case DW_OP_lit0 + 22: case DW_OP_lit0 + 23:
4225 case DW_OP_lit0 + 24: case DW_OP_lit0 + 25: case DW_OP_lit0 + 26: case DW_OP_lit0 + 27:
4226 case DW_OP_lit0 + 28: case DW_OP_lit0 + 29: case DW_OP_lit0 + 30: case DW_OP_lit0 + 31:
4227 rc = rtDwarfLoc_Push(pLoc, bOpcode - DW_OP_lit0);
4228 break;
4229 case DW_OP_nop:
4230 break;
4231 case DW_OP_dup: /** @todo 0 operands. */
4232 case DW_OP_drop: /** @todo 0 operands. */
4233 case DW_OP_over: /** @todo 0 operands. */
4234 case DW_OP_pick: /** @todo 1 operands, a 1-byte stack index. */
4235 case DW_OP_swap: /** @todo 0 operands. */
4236 case DW_OP_rot: /** @todo 0 operands. */
4237 case DW_OP_abs: /** @todo 0 operands. */
4238 case DW_OP_and: /** @todo 0 operands. */
4239 case DW_OP_div: /** @todo 0 operands. */
4240 case DW_OP_minus: /** @todo 0 operands. */
4241 case DW_OP_mod: /** @todo 0 operands. */
4242 case DW_OP_mul: /** @todo 0 operands. */
4243 case DW_OP_neg: /** @todo 0 operands. */
4244 case DW_OP_not: /** @todo 0 operands. */
4245 case DW_OP_or: /** @todo 0 operands. */
4246 case DW_OP_plus: /** @todo 0 operands. */
4247 case DW_OP_plus_uconst: /** @todo 1 operands, a ULEB128 addend. */
4248 case DW_OP_shl: /** @todo 0 operands. */
4249 case DW_OP_shr: /** @todo 0 operands. */
4250 case DW_OP_shra: /** @todo 0 operands. */
4251 case DW_OP_xor: /** @todo 0 operands. */
4252 case DW_OP_skip: /** @todo 1 signed 2-byte constant. */
4253 case DW_OP_bra: /** @todo 1 signed 2-byte constant. */
4254 case DW_OP_eq: /** @todo 0 operands. */
4255 case DW_OP_ge: /** @todo 0 operands. */
4256 case DW_OP_gt: /** @todo 0 operands. */
4257 case DW_OP_le: /** @todo 0 operands. */
4258 case DW_OP_lt: /** @todo 0 operands. */
4259 case DW_OP_ne: /** @todo 0 operands. */
4260 case DW_OP_reg0 + 0: case DW_OP_reg0 + 1: case DW_OP_reg0 + 2: case DW_OP_reg0 + 3: /** @todo 0 operands - reg 0..31. */
4261 case DW_OP_reg0 + 4: case DW_OP_reg0 + 5: case DW_OP_reg0 + 6: case DW_OP_reg0 + 7:
4262 case DW_OP_reg0 + 8: case DW_OP_reg0 + 9: case DW_OP_reg0 + 10: case DW_OP_reg0 + 11:
4263 case DW_OP_reg0 + 12: case DW_OP_reg0 + 13: case DW_OP_reg0 + 14: case DW_OP_reg0 + 15:
4264 case DW_OP_reg0 + 16: case DW_OP_reg0 + 17: case DW_OP_reg0 + 18: case DW_OP_reg0 + 19:
4265 case DW_OP_reg0 + 20: case DW_OP_reg0 + 21: case DW_OP_reg0 + 22: case DW_OP_reg0 + 23:
4266 case DW_OP_reg0 + 24: case DW_OP_reg0 + 25: case DW_OP_reg0 + 26: case DW_OP_reg0 + 27:
4267 case DW_OP_reg0 + 28: case DW_OP_reg0 + 29: case DW_OP_reg0 + 30: case DW_OP_reg0 + 31:
4268 case DW_OP_breg0+ 0: case DW_OP_breg0+ 1: case DW_OP_breg0+ 2: case DW_OP_breg0+ 3: /** @todo 1 operand, a SLEB128 offset. */
4269 case DW_OP_breg0+ 4: case DW_OP_breg0+ 5: case DW_OP_breg0+ 6: case DW_OP_breg0+ 7:
4270 case DW_OP_breg0+ 8: case DW_OP_breg0+ 9: case DW_OP_breg0+ 10: case DW_OP_breg0+ 11:
4271 case DW_OP_breg0+ 12: case DW_OP_breg0+ 13: case DW_OP_breg0+ 14: case DW_OP_breg0+ 15:
4272 case DW_OP_breg0+ 16: case DW_OP_breg0+ 17: case DW_OP_breg0+ 18: case DW_OP_breg0+ 19:
4273 case DW_OP_breg0+ 20: case DW_OP_breg0+ 21: case DW_OP_breg0+ 22: case DW_OP_breg0+ 23:
4274 case DW_OP_breg0+ 24: case DW_OP_breg0+ 25: case DW_OP_breg0+ 26: case DW_OP_breg0+ 27:
4275 case DW_OP_breg0+ 28: case DW_OP_breg0+ 29: case DW_OP_breg0+ 30: case DW_OP_breg0+ 31:
4276 case DW_OP_piece: /** @todo 1 operand, a ULEB128 size of piece addressed. */
4277 case DW_OP_regx: /** @todo 1 operand, a ULEB128 register. */
4278 case DW_OP_fbreg: /** @todo 1 operand, a SLEB128 offset. */
4279 case DW_OP_bregx: /** @todo 2 operands, a ULEB128 register followed by a SLEB128 offset. */
4280 case DW_OP_deref: /** @todo 0 operands. */
4281 case DW_OP_deref_size: /** @todo 1 operand, a 1-byte size of data retrieved. */
4282 case DW_OP_xderef: /** @todo 0 operands. */
4283 case DW_OP_xderef_size: /** @todo 1 operand, a 1-byte size of data retrieved. */
4284 AssertMsgFailedReturn(("bOpcode=%#x\n", bOpcode), VERR_DWARF_TODO);
4285 default:
4286 AssertMsgFailedReturn(("bOpcode=%#x\n", bOpcode), VERR_DWARF_UNKNOWN_LOC_OPCODE);
4287 }
4288 }
4289
4290 return pLoc->Cursor.rc;
4291}
4292
4293
4294/** @callback_method_impl{FNRTDWARFATTRDECODER} */
4295static DECLCALLBACK(int) rtDwarfDecode_SegmentLoc(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
4296 uint32_t uForm, PRTDWARFCURSOR pCursor)
4297{
4298 NOREF(pDie);
4299 AssertReturn(ATTR_GET_SIZE(pDesc) == 2, VERR_DWARF_IPE);
4300
4301 RTDWARFLOCST LocSt;
4302 int rc = rtDwarfLoc_Init(&LocSt, pCursor, uForm);
4303 if (RT_SUCCESS(rc))
4304 {
4305 rc = rtDwarfLoc_Evaluate(&LocSt, NULL, NULL);
4306 if (RT_SUCCESS(rc))
4307 {
4308 if (LocSt.iTop >= 0)
4309 {
4310 *(uint16_t *)pbMember = LocSt.auStack[LocSt.iTop];
4311 Log4((" %-20s %#06llx [%s]\n", rtDwarfLog_AttrName(pDesc->uAttr),
4312 LocSt.auStack[LocSt.iTop], rtDwarfLog_FormName(uForm)));
4313 return VINF_SUCCESS;
4314 }
4315 rc = VERR_DWARF_STACK_UNDERFLOW;
4316 }
4317 }
4318 return rc;
4319}
4320
4321/*
4322 *
4323 * DWARF debug_info parser
4324 * DWARF debug_info parser
4325 * DWARF debug_info parser
4326 *
4327 */
4328
4329
4330/**
4331 * Special hack to get the name and/or linkage name for a subprogram via a
4332 * specification reference.
4333 *
4334 * Since this is a hack, we ignore failure.
4335 *
4336 * If we want to really make use of DWARF info, we'll have to create some kind
4337 * of lookup tree for handling this. But currently we don't, so a hack will
4338 * suffice.
4339 *
4340 * @param pThis The DWARF instance.
4341 * @param pSubProgram The subprogram which is short on names.
4342 */
4343static void rtDwarfInfo_TryGetSubProgramNameFromSpecRef(PRTDBGMODDWARF pThis, PRTDWARFDIESUBPROGRAM pSubProgram)
4344{
4345 /*
4346 * Must have a spec ref, and it must be in the info section.
4347 */
4348 if (pSubProgram->SpecRef.enmWrt != krtDwarfRef_InfoSection)
4349 return;
4350
4351 /*
4352 * Create a cursor for reading the info and then the abbrivation code
4353 * starting the off the DIE.
4354 */
4355 RTDWARFCURSOR InfoCursor;
4356 int rc = rtDwarfCursor_InitWithOffset(&InfoCursor, pThis, krtDbgModDwarfSect_info, pSubProgram->SpecRef.off);
4357 if (RT_FAILURE(rc))
4358 return;
4359
4360 uint32_t uAbbrCode = rtDwarfCursor_GetULeb128AsU32(&InfoCursor, UINT32_MAX);
4361 if (uAbbrCode)
4362 {
4363 /* Only references to subprogram tags are interesting here. */
4364 PCRTDWARFABBREV pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
4365 if ( pAbbrev
4366 && pAbbrev->uTag == DW_TAG_subprogram)
4367 {
4368 /*
4369 * Use rtDwarfInfo_ParseDie to do the parsing, but with a different
4370 * attribute spec than usual.
4371 */
4372 rtDwarfInfo_ParseDie(pThis, &pSubProgram->Core, &g_SubProgramSpecHackDesc, &InfoCursor,
4373 pAbbrev, false /*fInitDie*/);
4374 }
4375 }
4376
4377 rtDwarfCursor_Delete(&InfoCursor, VINF_SUCCESS);
4378}
4379
4380
4381/**
4382 * Select which name to use.
4383 *
4384 * @returns One of the names.
4385 * @param pszName The DWARF name, may exclude namespace and class.
4386 * Can also be NULL.
4387 * @param pszLinkageName The linkage name. Can be NULL.
4388 */
4389static const char *rtDwarfInfo_SelectName(const char *pszName, const char *pszLinkageName)
4390{
4391 if (!pszName || !pszLinkageName)
4392 return pszName ? pszName : pszLinkageName;
4393
4394 /*
4395 * Some heuristics for selecting the link name if the normal name is missing
4396 * namespace or class prefixes.
4397 */
4398 size_t cchName = strlen(pszName);
4399 size_t cchLinkageName = strlen(pszLinkageName);
4400 if (cchLinkageName <= cchName + 1)
4401 return pszName;
4402
4403 const char *psz = strstr(pszLinkageName, pszName);
4404 if (!psz || psz - pszLinkageName < 4)
4405 return pszName;
4406
4407 return pszLinkageName;
4408}
4409
4410
4411/**
4412 * Parse the attributes of a DIE.
4413 *
4414 * @returns IPRT status code.
4415 * @param pThis The DWARF instance.
4416 * @param pDie The internal DIE structure to fill.
4417 */
4418static int rtDwarfInfo_SnoopSymbols(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie)
4419{
4420 int rc = VINF_SUCCESS;
4421 switch (pDie->uTag)
4422 {
4423 case DW_TAG_subprogram:
4424 {
4425 PRTDWARFDIESUBPROGRAM pSubProgram = (PRTDWARFDIESUBPROGRAM)pDie;
4426
4427 /* Obtain referenced specification there is only partial info. */
4428 if ( pSubProgram->PcRange.cAttrs
4429 && !pSubProgram->pszName)
4430 rtDwarfInfo_TryGetSubProgramNameFromSpecRef(pThis, pSubProgram);
4431
4432 if (pSubProgram->PcRange.cAttrs)
4433 {
4434 if (pSubProgram->PcRange.fHaveRanges)
4435 Log5(("subprogram %s (%s) <implement ranges>\n", pSubProgram->pszName, pSubProgram->pszLinkageName));
4436 else
4437 {
4438 Log5(("subprogram %s (%s) %#llx-%#llx%s\n", pSubProgram->pszName, pSubProgram->pszLinkageName,
4439 pSubProgram->PcRange.uLowAddress, pSubProgram->PcRange.uHighAddress,
4440 pSubProgram->PcRange.cAttrs == 2 ? "" : " !bad!"));
4441 if ( ( pSubProgram->pszName || pSubProgram->pszLinkageName)
4442 && pSubProgram->PcRange.cAttrs == 2)
4443 {
4444 if (pThis->iWatcomPass == 1)
4445 rc = rtDbgModDwarfRecordSegOffset(pThis, pSubProgram->uSegment, pSubProgram->PcRange.uHighAddress);
4446 else
4447 {
4448 RTDBGSEGIDX iSeg;
4449 RTUINTPTR offSeg;
4450 rc = rtDbgModDwarfLinkAddressToSegOffset(pThis, pSubProgram->uSegment,
4451 pSubProgram->PcRange.uLowAddress,
4452 &iSeg, &offSeg);
4453 if (RT_SUCCESS(rc))
4454 {
4455 uint64_t cb;
4456 if (pSubProgram->PcRange.uHighAddress >= pSubProgram->PcRange.uLowAddress)
4457 cb = pSubProgram->PcRange.uHighAddress - pSubProgram->PcRange.uLowAddress;
4458 else
4459 cb = 1;
4460 rc = RTDbgModSymbolAdd(pThis->hCnt,
4461 rtDwarfInfo_SelectName(pSubProgram->pszName, pSubProgram->pszLinkageName),
4462 iSeg, offSeg, cb, 0 /*fFlags*/, NULL /*piOrdinal*/);
4463 if (RT_FAILURE(rc))
4464 {
4465 if ( rc == VERR_DBG_DUPLICATE_SYMBOL
4466 || rc == VERR_DBG_ADDRESS_CONFLICT /** @todo figure why this happens with 10.6.8 mach_kernel, 32-bit. */
4467 )
4468 rc = VINF_SUCCESS;
4469 else
4470 AssertMsgFailed(("%Rrc\n", rc));
4471 }
4472 }
4473 else if ( pSubProgram->PcRange.uLowAddress == 0 /* see with vmlinux */
4474 && pSubProgram->PcRange.uHighAddress == 0)
4475 {
4476 Log5(("rtDbgModDwarfLinkAddressToSegOffset: Ignoring empty range.\n"));
4477 rc = VINF_SUCCESS; /* ignore */
4478 }
4479 else
4480 {
4481 AssertRC(rc);
4482 Log5(("rtDbgModDwarfLinkAddressToSegOffset failed: %Rrc\n", rc));
4483 }
4484 }
4485 }
4486 }
4487 }
4488 else
4489 Log5(("subprogram %s (%s) external\n", pSubProgram->pszName, pSubProgram->pszLinkageName));
4490 break;
4491 }
4492
4493 case DW_TAG_label:
4494 {
4495 PCRTDWARFDIELABEL pLabel = (PCRTDWARFDIELABEL)pDie;
4496 if (pLabel->fExternal)
4497 {
4498 Log5(("label %s %#x:%#llx\n", pLabel->pszName, pLabel->uSegment, pLabel->Address.uAddress));
4499 if (pThis->iWatcomPass == 1)
4500 rc = rtDbgModDwarfRecordSegOffset(pThis, pLabel->uSegment, pLabel->Address.uAddress);
4501 else
4502 {
4503 RTDBGSEGIDX iSeg;
4504 RTUINTPTR offSeg;
4505 rc = rtDbgModDwarfLinkAddressToSegOffset(pThis, pLabel->uSegment, pLabel->Address.uAddress,
4506 &iSeg, &offSeg);
4507 AssertRC(rc);
4508 if (RT_SUCCESS(rc))
4509 {
4510 rc = RTDbgModSymbolAdd(pThis->hCnt, pLabel->pszName, iSeg, offSeg, 0 /*cb*/,
4511 0 /*fFlags*/, NULL /*piOrdinal*/);
4512 AssertRC(rc);
4513 }
4514 else
4515 Log5(("rtDbgModDwarfLinkAddressToSegOffset failed: %Rrc\n", rc));
4516 }
4517
4518 }
4519 break;
4520 }
4521
4522 }
4523 return rc;
4524}
4525
4526
4527/**
4528 * Initializes the non-core fields of an internal DIE structure.
4529 *
4530 * @param pDie The DIE structure.
4531 * @param pDieDesc The DIE descriptor.
4532 */
4533static void rtDwarfInfo_InitDie(PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc)
4534{
4535 size_t i = pDieDesc->cAttributes;
4536 while (i-- > 0)
4537 {
4538 switch (pDieDesc->paAttributes[i].cbInit & ATTR_INIT_MASK)
4539 {
4540 case ATTR_INIT_ZERO:
4541 /* Nothing to do (RTMemAllocZ). */
4542 break;
4543
4544 case ATTR_INIT_FFFS:
4545 switch (pDieDesc->paAttributes[i].cbInit & ATTR_SIZE_MASK)
4546 {
4547 case 1:
4548 *(uint8_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT8_MAX;
4549 break;
4550 case 2:
4551 *(uint16_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT16_MAX;
4552 break;
4553 case 4:
4554 *(uint32_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT32_MAX;
4555 break;
4556 case 8:
4557 *(uint64_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT64_MAX;
4558 break;
4559 default:
4560 AssertFailed();
4561 memset((uint8_t *)pDie + pDieDesc->paAttributes[i].off, 0xff,
4562 pDieDesc->paAttributes[i].cbInit & ATTR_SIZE_MASK);
4563 break;
4564 }
4565 break;
4566
4567 default:
4568 AssertFailed();
4569 }
4570 }
4571}
4572
4573
4574/**
4575 * Creates a new internal DIE structure and links it up.
4576 *
4577 * @returns Pointer to the new DIE structure.
4578 * @param pThis The DWARF instance.
4579 * @param pDieDesc The DIE descriptor (for size and init).
4580 * @param pAbbrev The abbreviation cache entry.
4581 * @param pParent The parent DIE (NULL if unit).
4582 */
4583static PRTDWARFDIE rtDwarfInfo_NewDie(PRTDBGMODDWARF pThis, PCRTDWARFDIEDESC pDieDesc,
4584 PCRTDWARFABBREV pAbbrev, PRTDWARFDIE pParent)
4585{
4586 NOREF(pThis);
4587 Assert(pDieDesc->cbDie >= sizeof(RTDWARFDIE));
4588#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
4589 uint32_t iAllocator = pDieDesc->cbDie > pThis->aDieAllocators[0].cbMax;
4590 Assert(pDieDesc->cbDie <= pThis->aDieAllocators[iAllocator].cbMax);
4591 PRTDWARFDIE pDie = (PRTDWARFDIE)RTMemCacheAlloc(pThis->aDieAllocators[iAllocator].hMemCache);
4592#else
4593 PRTDWARFDIE pDie = (PRTDWARFDIE)RTMemAllocZ(pDieDesc->cbDie);
4594#endif
4595 if (pDie)
4596 {
4597#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
4598 RT_BZERO(pDie, pDieDesc->cbDie);
4599 pDie->iAllocator = iAllocator;
4600#endif
4601 rtDwarfInfo_InitDie(pDie, pDieDesc);
4602
4603 pDie->uTag = pAbbrev->uTag;
4604 pDie->offSpec = pAbbrev->offSpec;
4605 pDie->pParent = pParent;
4606 if (pParent)
4607 RTListAppend(&pParent->ChildList, &pDie->SiblingNode);
4608 else
4609 RTListInit(&pDie->SiblingNode);
4610 RTListInit(&pDie->ChildList);
4611
4612 }
4613 return pDie;
4614}
4615
4616
4617/**
4618 * Free all children of a DIE.
4619 *
4620 * @param pThis The DWARF instance.
4621 * @param pParentDie The parent DIE.
4622 */
4623static void rtDwarfInfo_FreeChildren(PRTDBGMODDWARF pThis, PRTDWARFDIE pParentDie)
4624{
4625 PRTDWARFDIE pChild, pNextChild;
4626 RTListForEachSafe(&pParentDie->ChildList, pChild, pNextChild, RTDWARFDIE, SiblingNode)
4627 {
4628 if (!RTListIsEmpty(&pChild->ChildList))
4629 rtDwarfInfo_FreeChildren(pThis, pChild);
4630 RTListNodeRemove(&pChild->SiblingNode);
4631#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
4632 RTMemCacheFree(pThis->aDieAllocators[pChild->iAllocator].hMemCache, pChild);
4633#else
4634 RTMemFree(pChild);
4635#endif
4636 }
4637}
4638
4639
4640/**
4641 * Free a DIE an all its children.
4642 *
4643 * @param pThis The DWARF instance.
4644 * @param pDie The DIE to free.
4645 */
4646static void rtDwarfInfo_FreeDie(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie)
4647{
4648 rtDwarfInfo_FreeChildren(pThis, pDie);
4649 RTListNodeRemove(&pDie->SiblingNode);
4650#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
4651 RTMemCacheFree(pThis->aDieAllocators[pDie->iAllocator].hMemCache, pDie);
4652#else
4653 RTMemFree(pChild);
4654#endif
4655}
4656
4657
4658/**
4659 * Skips a form.
4660 * @returns IPRT status code
4661 * @param pCursor The cursor.
4662 * @param uForm The form to skip.
4663 */
4664static int rtDwarfInfo_SkipForm(PRTDWARFCURSOR pCursor, uint32_t uForm)
4665{
4666 switch (uForm)
4667 {
4668 case DW_FORM_addr:
4669 return rtDwarfCursor_SkipBytes(pCursor, pCursor->cbNativeAddr);
4670
4671 case DW_FORM_block:
4672 case DW_FORM_exprloc:
4673 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetULeb128(pCursor, 0));
4674
4675 case DW_FORM_block1:
4676 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU8(pCursor, 0));
4677
4678 case DW_FORM_block2:
4679 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU16(pCursor, 0));
4680
4681 case DW_FORM_block4:
4682 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU32(pCursor, 0));
4683
4684 case DW_FORM_data1:
4685 case DW_FORM_ref1:
4686 case DW_FORM_flag:
4687 return rtDwarfCursor_SkipBytes(pCursor, 1);
4688
4689 case DW_FORM_data2:
4690 case DW_FORM_ref2:
4691 return rtDwarfCursor_SkipBytes(pCursor, 2);
4692
4693 case DW_FORM_data4:
4694 case DW_FORM_ref4:
4695 return rtDwarfCursor_SkipBytes(pCursor, 4);
4696
4697 case DW_FORM_data8:
4698 case DW_FORM_ref8:
4699 case DW_FORM_ref_sig8:
4700 return rtDwarfCursor_SkipBytes(pCursor, 8);
4701
4702 case DW_FORM_udata:
4703 case DW_FORM_sdata:
4704 case DW_FORM_ref_udata:
4705 return rtDwarfCursor_SkipLeb128(pCursor);
4706
4707 case DW_FORM_string:
4708 rtDwarfCursor_GetSZ(pCursor, NULL);
4709 return pCursor->rc;
4710
4711 case DW_FORM_indirect:
4712 return rtDwarfInfo_SkipForm(pCursor, rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX));
4713
4714 case DW_FORM_strp:
4715 case DW_FORM_ref_addr:
4716 case DW_FORM_sec_offset:
4717 return rtDwarfCursor_SkipBytes(pCursor, pCursor->f64bitDwarf ? 8 : 4);
4718
4719 case DW_FORM_flag_present:
4720 return pCursor->rc; /* no data */
4721
4722 default:
4723 return VERR_DWARF_UNKNOWN_FORM;
4724 }
4725}
4726
4727
4728
4729#ifdef SOME_UNUSED_FUNCTION
4730/**
4731 * Skips a DIE.
4732 *
4733 * @returns IPRT status code.
4734 * @param pCursor The cursor.
4735 * @param pAbbrevCursor The abbreviation cursor.
4736 */
4737static int rtDwarfInfo_SkipDie(PRTDWARFCURSOR pCursor, PRTDWARFCURSOR pAbbrevCursor)
4738{
4739 for (;;)
4740 {
4741 uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(pAbbrevCursor, 0);
4742 uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(pAbbrevCursor, 0);
4743 if (uAttr == 0 && uForm == 0)
4744 break;
4745
4746 int rc = rtDwarfInfo_SkipForm(pCursor, uForm);
4747 if (RT_FAILURE(rc))
4748 return rc;
4749 }
4750 return RT_FAILURE(pCursor->rc) ? pCursor->rc : pAbbrevCursor->rc;
4751}
4752#endif
4753
4754
4755/**
4756 * Parse the attributes of a DIE.
4757 *
4758 * @returns IPRT status code.
4759 * @param pThis The DWARF instance.
4760 * @param pDie The internal DIE structure to fill.
4761 * @param pDieDesc The DIE descriptor.
4762 * @param pCursor The debug_info cursor.
4763 * @param pAbbrev The abbreviation cache entry.
4764 * @param fInitDie Whether to initialize the DIE first. If not (@c
4765 * false) it's safe to assume we're following a
4766 * DW_AT_specification or DW_AT_abstract_origin,
4767 * and that we shouldn't be snooping any symbols.
4768 */
4769static int rtDwarfInfo_ParseDie(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc,
4770 PRTDWARFCURSOR pCursor, PCRTDWARFABBREV pAbbrev, bool fInitDie)
4771{
4772 RTDWARFCURSOR AbbrevCursor;
4773 int rc = rtDwarfCursor_InitWithOffset(&AbbrevCursor, pThis, krtDbgModDwarfSect_abbrev, pAbbrev->offSpec);
4774 if (RT_FAILURE(rc))
4775 return rc;
4776
4777 if (fInitDie)
4778 rtDwarfInfo_InitDie(pDie, pDieDesc);
4779 for (;;)
4780 {
4781 uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
4782 uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
4783 if (uAttr == 0)
4784 break;
4785 if (uForm == DW_FORM_indirect)
4786 uForm = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
4787
4788 /* Look up the attribute in the descriptor and invoke the decoder. */
4789 PCRTDWARFATTRDESC pAttr = NULL;
4790 size_t i = pDieDesc->cAttributes;
4791 while (i-- > 0)
4792 if (pDieDesc->paAttributes[i].uAttr == uAttr)
4793 {
4794 pAttr = &pDieDesc->paAttributes[i];
4795 rc = pAttr->pfnDecoder(pDie, (uint8_t *)pDie + pAttr->off, pAttr, uForm, pCursor);
4796 break;
4797 }
4798
4799 /* Some house keeping. */
4800 if (pAttr)
4801 pDie->cDecodedAttrs++;
4802 else
4803 {
4804 pDie->cUnhandledAttrs++;
4805 rc = rtDwarfInfo_SkipForm(pCursor, uForm);
4806 Log4((" %-20s [%s]\n", rtDwarfLog_AttrName(uAttr), rtDwarfLog_FormName(uForm)));
4807 }
4808 if (RT_FAILURE(rc))
4809 break;
4810 }
4811
4812 rc = rtDwarfCursor_Delete(&AbbrevCursor, rc);
4813 if (RT_SUCCESS(rc))
4814 rc = pCursor->rc;
4815
4816 /*
4817 * Snoop up symbols on the way out.
4818 */
4819 if (RT_SUCCESS(rc) && fInitDie)
4820 {
4821 rc = rtDwarfInfo_SnoopSymbols(pThis, pDie);
4822 /* Ignore duplicates, get work done instead. */
4823 /** @todo clean up global/static symbol mess. */
4824 if (rc == VERR_DBG_DUPLICATE_SYMBOL)
4825 rc = VINF_SUCCESS;
4826 }
4827
4828 return rc;
4829}
4830
4831
4832/**
4833 * Load the debug information of a unit.
4834 *
4835 * @returns IPRT status code.
4836 * @param pThis The DWARF instance.
4837 * @param pCursor The debug_info cursor.
4838 * @param fKeepDies Whether to keep the DIEs or discard them as soon
4839 * as possible.
4840 */
4841static int rtDwarfInfo_LoadUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor, bool fKeepDies)
4842{
4843 Log(("rtDwarfInfo_LoadUnit: %#x\n", rtDwarfCursor_CalcSectOffsetU32(pCursor)));
4844
4845 /*
4846 * Read the compilation unit header.
4847 */
4848 uint64_t offUnit = rtDwarfCursor_CalcSectOffsetU32(pCursor);
4849 uint64_t cbUnit = rtDwarfCursor_GetInitalLength(pCursor);
4850 cbUnit += rtDwarfCursor_CalcSectOffsetU32(pCursor) - offUnit;
4851 uint16_t const uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
4852 if ( uVer < 2
4853 || uVer > 4)
4854 return rtDwarfCursor_SkipUnit(pCursor);
4855 uint64_t const offAbbrev = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
4856 uint8_t const cbNativeAddr = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
4857 if (RT_FAILURE(pCursor->rc))
4858 return pCursor->rc;
4859 Log((" uVer=%d offAbbrev=%#llx cbNativeAddr=%d\n", uVer, offAbbrev, cbNativeAddr));
4860
4861 /*
4862 * Set up the abbreviation cache and store the native address size in the cursor.
4863 */
4864 if (offAbbrev > UINT32_MAX)
4865 {
4866 Log(("Unexpected abbrviation code offset of %#llx\n", offAbbrev));
4867 return VERR_DWARF_BAD_INFO;
4868 }
4869 rtDwarfAbbrev_SetUnitOffset(pThis, (uint32_t)offAbbrev);
4870 pCursor->cbNativeAddr = cbNativeAddr;
4871
4872 /*
4873 * The first DIE is a compile or partial unit, parse it here.
4874 */
4875 uint32_t uAbbrCode = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
4876 if (!uAbbrCode)
4877 {
4878 Log(("Unexpected abbrviation code of zero\n"));
4879 return VERR_DWARF_BAD_INFO;
4880 }
4881 PCRTDWARFABBREV pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
4882 if (!pAbbrev)
4883 return VERR_DWARF_ABBREV_NOT_FOUND;
4884 if ( pAbbrev->uTag != DW_TAG_compile_unit
4885 && pAbbrev->uTag != DW_TAG_partial_unit)
4886 {
4887 Log(("Unexpected compile/partial unit tag %#x\n", pAbbrev->uTag));
4888 return VERR_DWARF_BAD_INFO;
4889 }
4890
4891 PRTDWARFDIECOMPILEUNIT pUnit;
4892 pUnit = (PRTDWARFDIECOMPILEUNIT)rtDwarfInfo_NewDie(pThis, &g_CompileUnitDesc, pAbbrev, NULL /*pParent*/);
4893 if (!pUnit)
4894 return VERR_NO_MEMORY;
4895 pUnit->offUnit = offUnit;
4896 pUnit->cbUnit = cbUnit;
4897 pUnit->offAbbrev = offAbbrev;
4898 pUnit->cbNativeAddr = cbNativeAddr;
4899 pUnit->uDwarfVer = (uint8_t)uVer;
4900 RTListAppend(&pThis->CompileUnitList, &pUnit->Core.SiblingNode);
4901
4902 int rc = rtDwarfInfo_ParseDie(pThis, &pUnit->Core, &g_CompileUnitDesc, pCursor, pAbbrev, true /*fInitDie*/);
4903 if (RT_FAILURE(rc))
4904 return rc;
4905
4906 /*
4907 * Parse DIEs.
4908 */
4909 uint32_t cDepth = 0;
4910 PRTDWARFDIE pParentDie = &pUnit->Core;
4911 while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
4912 {
4913#ifdef LOG_ENABLED
4914 uint32_t offLog = rtDwarfCursor_CalcSectOffsetU32(pCursor);
4915#endif
4916 uAbbrCode = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
4917 if (!uAbbrCode)
4918 {
4919 /* End of siblings, up one level. (Is this correct?) */
4920 if (pParentDie->pParent)
4921 {
4922 pParentDie = pParentDie->pParent;
4923 cDepth--;
4924 if (!fKeepDies && pParentDie->pParent)
4925 rtDwarfInfo_FreeChildren(pThis, pParentDie);
4926 }
4927 }
4928 else
4929 {
4930 /*
4931 * Look up the abbreviation and match the tag up with a descriptor.
4932 */
4933 pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
4934 if (!pAbbrev)
4935 return VERR_DWARF_ABBREV_NOT_FOUND;
4936
4937 PCRTDWARFDIEDESC pDieDesc;
4938 const char *pszName;
4939 if (pAbbrev->uTag < RT_ELEMENTS(g_aTagDescs))
4940 {
4941 Assert(g_aTagDescs[pAbbrev->uTag].uTag == pAbbrev->uTag || g_aTagDescs[pAbbrev->uTag].uTag == 0);
4942 pszName = g_aTagDescs[pAbbrev->uTag].pszName;
4943 pDieDesc = g_aTagDescs[pAbbrev->uTag].pDesc;
4944 }
4945 else
4946 {
4947 pszName = "<unknown>";
4948 pDieDesc = &g_CoreDieDesc;
4949 }
4950 Log4(("%08x: %*stag=%s (%#x, abbrev %u)%s\n", offLog, cDepth * 2, "", pszName,
4951 pAbbrev->uTag, uAbbrCode, pAbbrev->fChildren ? " has children" : ""));
4952
4953 /*
4954 * Create a new internal DIE structure and parse the
4955 * attributes.
4956 */
4957 PRTDWARFDIE pNewDie = rtDwarfInfo_NewDie(pThis, pDieDesc, pAbbrev, pParentDie);
4958 if (!pNewDie)
4959 return VERR_NO_MEMORY;
4960
4961 if (pAbbrev->fChildren)
4962 {
4963 pParentDie = pNewDie;
4964 cDepth++;
4965 }
4966
4967 rc = rtDwarfInfo_ParseDie(pThis, pNewDie, pDieDesc, pCursor, pAbbrev, true /*fInitDie*/);
4968 if (RT_FAILURE(rc))
4969 return rc;
4970
4971 if (!fKeepDies && !pAbbrev->fChildren)
4972 rtDwarfInfo_FreeDie(pThis, pNewDie);
4973 }
4974 } /* while more DIEs */
4975
4976
4977 /* Unlink and free child DIEs if told to do so. */
4978 if (!fKeepDies)
4979 rtDwarfInfo_FreeChildren(pThis, &pUnit->Core);
4980
4981 return RT_SUCCESS(rc) ? pCursor->rc : rc;
4982}
4983
4984
4985/**
4986 * Extracts the symbols.
4987 *
4988 * The symbols are insered into the debug info container.
4989 *
4990 * @returns IPRT status code
4991 * @param pThis The DWARF instance.
4992 */
4993static int rtDwarfInfo_LoadAll(PRTDBGMODDWARF pThis)
4994{
4995 RTDWARFCURSOR Cursor;
4996 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_info);
4997 if (RT_SUCCESS(rc))
4998 {
4999 while ( !rtDwarfCursor_IsAtEnd(&Cursor)
5000 && RT_SUCCESS(rc))
5001 rc = rtDwarfInfo_LoadUnit(pThis, &Cursor, false /* fKeepDies */);
5002
5003 rc = rtDwarfCursor_Delete(&Cursor, rc);
5004 }
5005 return rc;
5006}
5007
5008
5009
5010/*
5011 *
5012 * Public and image level symbol handling.
5013 * Public and image level symbol handling.
5014 * Public and image level symbol handling.
5015 * Public and image level symbol handling.
5016 *
5017 *
5018 */
5019
5020#define RTDBGDWARF_SYM_ENUM_BASE_ADDRESS UINT32_C(0x200000)
5021
5022/** @callback_method_impl{FNRTLDRENUMSYMS,
5023 * Adds missing symbols from the image symbol table.} */
5024static DECLCALLBACK(int) rtDwarfSyms_EnumSymbolsCallback(RTLDRMOD hLdrMod, const char *pszSymbol, unsigned uSymbol,
5025 RTLDRADDR Value, void *pvUser)
5026{
5027 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
5028 RT_NOREF_PV(hLdrMod); RT_NOREF_PV(uSymbol);
5029 Assert(pThis->iWatcomPass != 1);
5030
5031 RTLDRADDR uRva = Value - RTDBGDWARF_SYM_ENUM_BASE_ADDRESS;
5032 if ( Value >= RTDBGDWARF_SYM_ENUM_BASE_ADDRESS
5033 && uRva < _1G)
5034 {
5035 RTDBGSYMBOL SymInfo;
5036 RTINTPTR offDisp;
5037 int rc = RTDbgModSymbolByAddr(pThis->hCnt, RTDBGSEGIDX_RVA, uRva, RTDBGSYMADDR_FLAGS_LESS_OR_EQUAL, &offDisp, &SymInfo);
5038 if ( RT_FAILURE(rc)
5039 || offDisp != 0)
5040 {
5041 rc = RTDbgModSymbolAdd(pThis->hCnt, pszSymbol, RTDBGSEGIDX_RVA, uRva, 1, 0 /*fFlags*/, NULL /*piOrdinal*/);
5042 Log(("Dwarf: Symbol #%05u %#018RTptr %s [%Rrc]\n", uSymbol, Value, pszSymbol, rc)); NOREF(rc);
5043 }
5044 }
5045 else
5046 Log(("Dwarf: Symbol #%05u %#018RTptr '%s' [SKIPPED - INVALID ADDRESS]\n", uSymbol, Value, pszSymbol));
5047 return VINF_SUCCESS;
5048}
5049
5050
5051
5052/**
5053 * Loads additional symbols from the pubnames section and the executable image.
5054 *
5055 * The symbols are insered into the debug info container.
5056 *
5057 * @returns IPRT status code
5058 * @param pThis The DWARF instance.
5059 */
5060static int rtDwarfSyms_LoadAll(PRTDBGMODDWARF pThis)
5061{
5062 /*
5063 * pubnames.
5064 */
5065 int rc = VINF_SUCCESS;
5066 if (pThis->aSections[krtDbgModDwarfSect_pubnames].fPresent)
5067 {
5068// RTDWARFCURSOR Cursor;
5069// int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_info);
5070// if (RT_SUCCESS(rc))
5071// {
5072// while ( !rtDwarfCursor_IsAtEnd(&Cursor)
5073// && RT_SUCCESS(rc))
5074// rc = rtDwarfInfo_LoadUnit(pThis, &Cursor, false /* fKeepDies */);
5075//
5076// rc = rtDwarfCursor_Delete(&Cursor, rc);
5077// }
5078// return rc;
5079 }
5080
5081 /*
5082 * The executable image.
5083 */
5084 if ( pThis->pImgMod
5085 && pThis->pImgMod->pImgVt->pfnEnumSymbols
5086 && pThis->iWatcomPass != 1
5087 && RT_SUCCESS(rc))
5088 {
5089 rc = pThis->pImgMod->pImgVt->pfnEnumSymbols(pThis->pImgMod,
5090 RTLDR_ENUM_SYMBOL_FLAGS_ALL | RTLDR_ENUM_SYMBOL_FLAGS_NO_FWD,
5091 RTDBGDWARF_SYM_ENUM_BASE_ADDRESS,
5092 rtDwarfSyms_EnumSymbolsCallback,
5093 pThis);
5094 }
5095
5096 return rc;
5097}
5098
5099
5100
5101
5102/*
5103 *
5104 * DWARF Debug module implementation.
5105 * DWARF Debug module implementation.
5106 * DWARF Debug module implementation.
5107 *
5108 */
5109
5110
5111/** @interface_method_impl{RTDBGMODVTDBG,pfnUnwindFrame} */
5112static DECLCALLBACK(int) rtDbgModDwarf_UnwindFrame(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, PRTDBGUNWINDSTATE pState)
5113{
5114 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5115
5116 /*
5117 * Unwinding info is stored in the '.debug_frame' section, or altertively
5118 * in the '.eh_frame' one in the image. In the latter case the dbgmodldr.cpp
5119 * part of the operation will take care of it. Since the sections contain the
5120 * same data, we just create a cursor and call a common function to do the job.
5121 */
5122 if (pThis->aSections[krtDbgModDwarfSect_frame].fPresent)
5123 {
5124 RTDWARFCURSOR Cursor;
5125 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_frame);
5126 if (RT_SUCCESS(rc))
5127 {
5128 /* Figure default pointer encoding from image arch. */
5129 uint8_t bPtrEnc = rtDwarfUnwind_ArchToPtrEnc(pMod->pImgVt->pfnGetArch(pMod));
5130
5131 /* Make sure we've got both seg:off and rva for the input address. */
5132 RTUINTPTR uRva = off;
5133 if (iSeg == RTDBGSEGIDX_RVA)
5134 rtDbgModDwarfRvaToSegOffset(pThis, uRva, &iSeg, &off);
5135 else
5136 rtDbgModDwarfSegOffsetToRva(pThis, iSeg, off, &uRva);
5137
5138 /* Do the work */
5139 rc = rtDwarfUnwind_Slow(&Cursor, iSeg, off, uRva, pState, bPtrEnc, false /*fIsEhFrame*/);
5140
5141 rc = rtDwarfCursor_Delete(&Cursor, rc);
5142 }
5143 return rc;
5144 }
5145 return VERR_DBG_NO_UNWIND_INFO;
5146}
5147
5148
5149/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
5150static DECLCALLBACK(int) rtDbgModDwarf_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
5151 PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
5152{
5153 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5154 return RTDbgModLineByAddr(pThis->hCnt, iSeg, off, poffDisp, pLineInfo);
5155}
5156
5157
5158/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
5159static DECLCALLBACK(int) rtDbgModDwarf_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
5160{
5161 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5162 return RTDbgModLineByOrdinal(pThis->hCnt, iOrdinal, pLineInfo);
5163}
5164
5165
5166/** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
5167static DECLCALLBACK(uint32_t) rtDbgModDwarf_LineCount(PRTDBGMODINT pMod)
5168{
5169 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5170 return RTDbgModLineCount(pThis->hCnt);
5171}
5172
5173
5174/** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
5175static DECLCALLBACK(int) rtDbgModDwarf_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
5176 uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
5177{
5178 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5179 Assert(!pszFile[cchFile]); NOREF(cchFile);
5180 return RTDbgModLineAdd(pThis->hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
5181}
5182
5183
5184/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
5185static DECLCALLBACK(int) rtDbgModDwarf_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off, uint32_t fFlags,
5186 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
5187{
5188 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5189 return RTDbgModSymbolByAddr(pThis->hCnt, iSeg, off, fFlags, poffDisp, pSymInfo);
5190}
5191
5192
5193/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
5194static DECLCALLBACK(int) rtDbgModDwarf_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
5195 PRTDBGSYMBOL pSymInfo)
5196{
5197 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5198 Assert(!pszSymbol[cchSymbol]); RT_NOREF_PV(cchSymbol);
5199 return RTDbgModSymbolByName(pThis->hCnt, pszSymbol/*, cchSymbol*/, pSymInfo);
5200}
5201
5202
5203/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
5204static DECLCALLBACK(int) rtDbgModDwarf_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
5205{
5206 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5207 return RTDbgModSymbolByOrdinal(pThis->hCnt, iOrdinal, pSymInfo);
5208}
5209
5210
5211/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
5212static DECLCALLBACK(uint32_t) rtDbgModDwarf_SymbolCount(PRTDBGMODINT pMod)
5213{
5214 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5215 return RTDbgModSymbolCount(pThis->hCnt);
5216}
5217
5218
5219/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
5220static DECLCALLBACK(int) rtDbgModDwarf_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
5221 RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
5222 uint32_t *piOrdinal)
5223{
5224 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5225 Assert(!pszSymbol[cchSymbol]); NOREF(cchSymbol);
5226 return RTDbgModSymbolAdd(pThis->hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
5227}
5228
5229
5230/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
5231static DECLCALLBACK(int) rtDbgModDwarf_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
5232{
5233 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5234 return RTDbgModSegmentByIndex(pThis->hCnt, iSeg, pSegInfo);
5235}
5236
5237
5238/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
5239static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_SegmentCount(PRTDBGMODINT pMod)
5240{
5241 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5242 return RTDbgModSegmentCount(pThis->hCnt);
5243}
5244
5245
5246/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
5247static DECLCALLBACK(int) rtDbgModDwarf_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
5248 uint32_t fFlags, PRTDBGSEGIDX piSeg)
5249{
5250 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5251 Assert(!pszName[cchName]); NOREF(cchName);
5252 return RTDbgModSegmentAdd(pThis->hCnt, uRva, cb, pszName, fFlags, piSeg);
5253}
5254
5255
5256/** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
5257static DECLCALLBACK(RTUINTPTR) rtDbgModDwarf_ImageSize(PRTDBGMODINT pMod)
5258{
5259 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5260 RTUINTPTR cb1 = RTDbgModImageSize(pThis->hCnt);
5261 RTUINTPTR cb2 = pThis->pImgMod->pImgVt->pfnImageSize(pMod);
5262 return RT_MAX(cb1, cb2);
5263}
5264
5265
5266/** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
5267static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
5268{
5269 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5270 return RTDbgModRvaToSegOff(pThis->hCnt, uRva, poffSeg);
5271}
5272
5273
5274/** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
5275static DECLCALLBACK(int) rtDbgModDwarf_Close(PRTDBGMODINT pMod)
5276{
5277 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
5278
5279 for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
5280 if (pThis->aSections[iSect].pv)
5281 pThis->pDbgInfoMod->pImgVt->pfnUnmapPart(pThis->pDbgInfoMod, pThis->aSections[iSect].cb, &pThis->aSections[iSect].pv);
5282
5283 RTDbgModRelease(pThis->hCnt);
5284 RTMemFree(pThis->paCachedAbbrevs);
5285 if (pThis->pNestedMod)
5286 {
5287 pThis->pNestedMod->pImgVt->pfnClose(pThis->pNestedMod);
5288 RTStrCacheRelease(g_hDbgModStrCache, pThis->pNestedMod->pszName);
5289 RTStrCacheRelease(g_hDbgModStrCache, pThis->pNestedMod->pszDbgFile);
5290 RTMemFree(pThis->pNestedMod);
5291 pThis->pNestedMod = NULL;
5292 }
5293
5294#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
5295 uint32_t i = RT_ELEMENTS(pThis->aDieAllocators);
5296 while (i-- > 0)
5297 {
5298 RTMemCacheDestroy(pThis->aDieAllocators[i].hMemCache);
5299 pThis->aDieAllocators[i].hMemCache = NIL_RTMEMCACHE;
5300 }
5301#endif
5302
5303 RTMemFree(pThis);
5304
5305 return VINF_SUCCESS;
5306}
5307
5308
5309/** @callback_method_impl{FNRTLDRENUMDBG} */
5310static DECLCALLBACK(int) rtDbgModDwarfEnumCallback(RTLDRMOD hLdrMod, PCRTLDRDBGINFO pDbgInfo, void *pvUser)
5311{
5312 RT_NOREF_PV(hLdrMod);
5313
5314 /*
5315 * Skip stuff we can't handle.
5316 */
5317 if (pDbgInfo->enmType != RTLDRDBGINFOTYPE_DWARF)
5318 return VINF_SUCCESS;
5319 const char *pszSection = pDbgInfo->u.Dwarf.pszSection;
5320 if (!pszSection || !*pszSection)
5321 return VINF_SUCCESS;
5322 Assert(!pDbgInfo->pszExtFile);
5323
5324 /*
5325 * Must have a part name starting with debug_ and possibly prefixed by dots
5326 * or underscores.
5327 */
5328 if (!strncmp(pszSection, RT_STR_TUPLE(".debug_"))) /* ELF */
5329 pszSection += sizeof(".debug_") - 1;
5330 else if (!strncmp(pszSection, RT_STR_TUPLE("__debug_"))) /* Mach-O */
5331 pszSection += sizeof("__debug_") - 1;
5332 else if (!strcmp(pszSection, ".WATCOM_references"))
5333 return VINF_SUCCESS; /* Ignore special watcom section for now.*/
5334 else if ( !strcmp(pszSection, "__apple_types")
5335 || !strcmp(pszSection, "__apple_namespac")
5336 || !strcmp(pszSection, "__apple_objc")
5337 || !strcmp(pszSection, "__apple_names"))
5338 return VINF_SUCCESS; /* Ignore special apple sections for now. */
5339 else
5340 AssertMsgFailedReturn(("%s\n", pszSection), VINF_SUCCESS /*ignore*/);
5341
5342 /*
5343 * Figure out which part we're talking about.
5344 */
5345 krtDbgModDwarfSect enmSect;
5346 if (0) { /* dummy */ }
5347#define ELSE_IF_STRCMP_SET(a_Name) else if (!strcmp(pszSection, #a_Name)) enmSect = krtDbgModDwarfSect_ ## a_Name
5348 ELSE_IF_STRCMP_SET(abbrev);
5349 ELSE_IF_STRCMP_SET(aranges);
5350 ELSE_IF_STRCMP_SET(frame);
5351 ELSE_IF_STRCMP_SET(info);
5352 ELSE_IF_STRCMP_SET(inlined);
5353 ELSE_IF_STRCMP_SET(line);
5354 ELSE_IF_STRCMP_SET(loc);
5355 ELSE_IF_STRCMP_SET(macinfo);
5356 ELSE_IF_STRCMP_SET(pubnames);
5357 ELSE_IF_STRCMP_SET(pubtypes);
5358 ELSE_IF_STRCMP_SET(ranges);
5359 ELSE_IF_STRCMP_SET(str);
5360 ELSE_IF_STRCMP_SET(types);
5361#undef ELSE_IF_STRCMP_SET
5362 else
5363 {
5364 AssertMsgFailed(("%s\n", pszSection));
5365 return VINF_SUCCESS;
5366 }
5367
5368 /*
5369 * Record the section.
5370 */
5371 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
5372 AssertMsgReturn(!pThis->aSections[enmSect].fPresent, ("duplicate %s\n", pszSection), VINF_SUCCESS /*ignore*/);
5373
5374 pThis->aSections[enmSect].fPresent = true;
5375 pThis->aSections[enmSect].offFile = pDbgInfo->offFile;
5376 pThis->aSections[enmSect].pv = NULL;
5377 pThis->aSections[enmSect].cb = (size_t)pDbgInfo->cb;
5378 pThis->aSections[enmSect].iDbgInfo = pDbgInfo->iDbgInfo;
5379 if (pThis->aSections[enmSect].cb != pDbgInfo->cb)
5380 pThis->aSections[enmSect].cb = ~(size_t)0;
5381
5382 return VINF_SUCCESS;
5383}
5384
5385
5386static int rtDbgModDwarfTryOpenDbgFile(PRTDBGMODINT pDbgMod, PRTDBGMODDWARF pThis, RTLDRARCH enmArch)
5387{
5388 if ( !pDbgMod->pszDbgFile
5389 || RTPathIsSame(pDbgMod->pszDbgFile, pDbgMod->pszImgFile) == (int)true /* returns VERR too */)
5390 return VERR_DBG_NO_MATCHING_INTERPRETER;
5391
5392 /*
5393 * Only open the image.
5394 */
5395 PRTDBGMODINT pDbgInfoMod = (PRTDBGMODINT)RTMemAllocZ(sizeof(*pDbgInfoMod));
5396 if (!pDbgInfoMod)
5397 return VERR_NO_MEMORY;
5398
5399 int rc;
5400 pDbgInfoMod->u32Magic = RTDBGMOD_MAGIC;
5401 pDbgInfoMod->cRefs = 1;
5402 if (RTStrCacheRetain(pDbgMod->pszDbgFile) != UINT32_MAX)
5403 {
5404 pDbgInfoMod->pszImgFile = pDbgMod->pszDbgFile;
5405 if (RTStrCacheRetain(pDbgMod->pszName) != UINT32_MAX)
5406 {
5407 pDbgInfoMod->pszName = pDbgMod->pszName;
5408 pDbgInfoMod->pImgVt = &g_rtDbgModVtImgLdr;
5409 rc = pDbgInfoMod->pImgVt->pfnTryOpen(pDbgInfoMod, enmArch);
5410 if (RT_SUCCESS(rc))
5411 {
5412 pThis->pDbgInfoMod = pDbgInfoMod;
5413 pThis->pNestedMod = pDbgInfoMod;
5414 return VINF_SUCCESS;
5415 }
5416
5417 RTStrCacheRelease(g_hDbgModStrCache, pDbgInfoMod->pszName);
5418 }
5419 else
5420 rc = VERR_NO_STR_MEMORY;
5421 RTStrCacheRelease(g_hDbgModStrCache, pDbgInfoMod->pszImgFile);
5422 }
5423 else
5424 rc = VERR_NO_STR_MEMORY;
5425 RTMemFree(pDbgInfoMod);
5426 return rc;
5427}
5428
5429
5430/** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
5431static DECLCALLBACK(int) rtDbgModDwarf_TryOpen(PRTDBGMODINT pMod, RTLDRARCH enmArch)
5432{
5433 /*
5434 * DWARF is only supported when part of an image.
5435 */
5436 if (!pMod->pImgVt)
5437 return VERR_DBG_NO_MATCHING_INTERPRETER;
5438
5439 /*
5440 * Create the module instance data.
5441 */
5442 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)RTMemAllocZ(sizeof(*pThis));
5443 if (!pThis)
5444 return VERR_NO_MEMORY;
5445 pThis->pDbgInfoMod = pMod;
5446 pThis->pImgMod = pMod;
5447 RTListInit(&pThis->CompileUnitList);
5448 /** @todo better fUseLinkAddress heuristics! */
5449 if ( (pMod->pszDbgFile && strstr(pMod->pszDbgFile, "mach_kernel"))
5450 || (pMod->pszImgFile && strstr(pMod->pszImgFile, "mach_kernel"))
5451 || (pMod->pszImgFileSpecified && strstr(pMod->pszImgFileSpecified, "mach_kernel")) )
5452 pThis->fUseLinkAddress = true;
5453
5454#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
5455 AssertCompile(RT_ELEMENTS(pThis->aDieAllocators) == 2);
5456 pThis->aDieAllocators[0].cbMax = sizeof(RTDWARFDIE);
5457 pThis->aDieAllocators[1].cbMax = sizeof(RTDWARFDIECOMPILEUNIT);
5458 for (uint32_t i = 0; i < RT_ELEMENTS(g_aTagDescs); i++)
5459 if (g_aTagDescs[i].pDesc && g_aTagDescs[i].pDesc->cbDie > pThis->aDieAllocators[1].cbMax)
5460 pThis->aDieAllocators[1].cbMax = (uint32_t)g_aTagDescs[i].pDesc->cbDie;
5461 pThis->aDieAllocators[1].cbMax = RT_ALIGN_32(pThis->aDieAllocators[1].cbMax, sizeof(uint64_t));
5462
5463 for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aDieAllocators); i++)
5464 {
5465 int rc = RTMemCacheCreate(&pThis->aDieAllocators[i].hMemCache, pThis->aDieAllocators[i].cbMax, sizeof(uint64_t),
5466 UINT32_MAX, NULL /*pfnCtor*/, NULL /*pfnDtor*/, NULL /*pvUser*/, 0 /*fFlags*/);
5467 if (RT_FAILURE(rc))
5468 {
5469 while (i-- > 0)
5470 RTMemCacheDestroy(pThis->aDieAllocators[i].hMemCache);
5471 RTMemFree(pThis);
5472 return rc;
5473 }
5474 }
5475#endif
5476
5477 /*
5478 * If the debug file name is set, let's see if it's an ELF image with DWARF
5479 * inside it. In that case we'll have to deal with two image modules, one
5480 * for segments and address translation and one for the debug information.
5481 */
5482 if (pMod->pszDbgFile != NULL)
5483 rtDbgModDwarfTryOpenDbgFile(pMod, pThis, enmArch);
5484
5485 /*
5486 * Enumerate the debug info in the module, looking for DWARF bits.
5487 */
5488 int rc = pThis->pDbgInfoMod->pImgVt->pfnEnumDbgInfo(pThis->pDbgInfoMod, rtDbgModDwarfEnumCallback, pThis);
5489 if (RT_SUCCESS(rc))
5490 {
5491 if (pThis->aSections[krtDbgModDwarfSect_info].fPresent)
5492 {
5493 /*
5494 * Extract / explode the data we want (symbols and line numbers)
5495 * storing them in a container module.
5496 */
5497 rc = RTDbgModCreate(&pThis->hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
5498 if (RT_SUCCESS(rc))
5499 {
5500 pMod->pvDbgPriv = pThis;
5501
5502 rc = rtDbgModDwarfAddSegmentsFromImage(pThis);
5503 if (RT_SUCCESS(rc))
5504 rc = rtDwarfInfo_LoadAll(pThis);
5505 if (RT_SUCCESS(rc))
5506 rc = rtDwarfSyms_LoadAll(pThis);
5507 if (RT_SUCCESS(rc))
5508 rc = rtDwarfLine_ExplodeAll(pThis);
5509 if (RT_SUCCESS(rc) && pThis->iWatcomPass == 1)
5510 {
5511 rc = rtDbgModDwarfAddSegmentsFromPass1(pThis);
5512 pThis->iWatcomPass = 2;
5513 if (RT_SUCCESS(rc))
5514 rc = rtDwarfInfo_LoadAll(pThis);
5515 if (RT_SUCCESS(rc))
5516 rc = rtDwarfSyms_LoadAll(pThis);
5517 if (RT_SUCCESS(rc))
5518 rc = rtDwarfLine_ExplodeAll(pThis);
5519 }
5520 if (RT_SUCCESS(rc))
5521 {
5522 /*
5523 * Free the cached abbreviations and unload all sections.
5524 */
5525 pThis->cCachedAbbrevsAlloced = 0;
5526 RTMemFree(pThis->paCachedAbbrevs);
5527 pThis->paCachedAbbrevs = NULL;
5528
5529 for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
5530 if (pThis->aSections[iSect].pv)
5531 pThis->pDbgInfoMod->pImgVt->pfnUnmapPart(pThis->pDbgInfoMod, pThis->aSections[iSect].cb,
5532 &pThis->aSections[iSect].pv);
5533
5534 /** @todo Kill pThis->CompileUnitList and the alloc caches. */
5535 return VINF_SUCCESS;
5536 }
5537
5538 /* bail out. */
5539 RTDbgModRelease(pThis->hCnt);
5540 pMod->pvDbgPriv = NULL;
5541 }
5542 }
5543 else
5544 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
5545 }
5546
5547 RTMemFree(pThis->paCachedAbbrevs);
5548
5549#ifdef RTDBGMODDWARF_WITH_MEM_CACHE
5550 uint32_t i = RT_ELEMENTS(pThis->aDieAllocators);
5551 while (i-- > 0)
5552 {
5553 RTMemCacheDestroy(pThis->aDieAllocators[i].hMemCache);
5554 pThis->aDieAllocators[i].hMemCache = NIL_RTMEMCACHE;
5555 }
5556#endif
5557
5558 RTMemFree(pThis);
5559
5560 return rc;
5561}
5562
5563
5564
5565/** Virtual function table for the DWARF debug info reader. */
5566DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDwarf =
5567{
5568 /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
5569 /*.fSupports = */ RT_DBGTYPE_DWARF,
5570 /*.pszName = */ "dwarf",
5571 /*.pfnTryOpen = */ rtDbgModDwarf_TryOpen,
5572 /*.pfnClose = */ rtDbgModDwarf_Close,
5573
5574 /*.pfnRvaToSegOff = */ rtDbgModDwarf_RvaToSegOff,
5575 /*.pfnImageSize = */ rtDbgModDwarf_ImageSize,
5576
5577 /*.pfnSegmentAdd = */ rtDbgModDwarf_SegmentAdd,
5578 /*.pfnSegmentCount = */ rtDbgModDwarf_SegmentCount,
5579 /*.pfnSegmentByIndex = */ rtDbgModDwarf_SegmentByIndex,
5580
5581 /*.pfnSymbolAdd = */ rtDbgModDwarf_SymbolAdd,
5582 /*.pfnSymbolCount = */ rtDbgModDwarf_SymbolCount,
5583 /*.pfnSymbolByOrdinal = */ rtDbgModDwarf_SymbolByOrdinal,
5584 /*.pfnSymbolByName = */ rtDbgModDwarf_SymbolByName,
5585 /*.pfnSymbolByAddr = */ rtDbgModDwarf_SymbolByAddr,
5586
5587 /*.pfnLineAdd = */ rtDbgModDwarf_LineAdd,
5588 /*.pfnLineCount = */ rtDbgModDwarf_LineCount,
5589 /*.pfnLineByOrdinal = */ rtDbgModDwarf_LineByOrdinal,
5590 /*.pfnLineByAddr = */ rtDbgModDwarf_LineByAddr,
5591
5592 /*.pfnUnwindFrame = */ rtDbgModDwarf_UnwindFrame,
5593
5594 /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
5595};
5596
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette