VirtualBox

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

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

build fix

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