VirtualBox

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

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

One more vmlinux hack.

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

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