VirtualBox

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

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

IPRT: Added single stack frame unwind function to RTDbgMod and RTLdr, copying over the PoC from DBGFRStack.cpp. bugref:3897

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

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