VirtualBox

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

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

More exteran .dSYM and .dwo bundles/files changes.

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