VirtualBox

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

Last change on this file since 47824 was 47824, checked in by vboxsync, 11 years ago

RTDbgDwarf: Read gcc 4.8.1 dwarf output.

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