VirtualBox

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

Last change on this file since 46916 was 46916, checked in by vboxsync, 12 years ago

dbgmoddwarf.cpp: Ignore abbreviations with code 0, they're just fillers it seems.

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

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