VirtualBox

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

Last change on this file since 38620 was 38620, checked in by vboxsync, 13 years ago

build fixes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 121.1 KB
Line 
1/* $Id: dbgmoddwarf.cpp 38620 2011-09-03 20:01:52Z vboxsync $ */
2/** @file
3 * IPRT - Debug Info Reader For DWARF.
4 */
5
6/*
7 * Copyright (C) 2011 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DBG_DWARF
32#include <iprt/dbg.h>
33#include "internal/iprt.h"
34
35#include <iprt/asm.h>
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/list.h>
39#include <iprt/log.h>
40#include <iprt/mem.h>
41#include <iprt/path.h>
42#include <iprt/string.h>
43#include "internal/dbgmod.h"
44
45
46/*******************************************************************************
47* Defined Constants And Macros *
48*******************************************************************************/
49/** @name Standard DWARF Line Number Opcodes
50 * @{ */
51#define DW_LNS_extended UINT8_C(0x00)
52#define DW_LNS_copy UINT8_C(0x01)
53#define DW_LNS_advance_pc UINT8_C(0x02)
54#define DW_LNS_advance_line UINT8_C(0x03)
55#define DW_LNS_set_file UINT8_C(0x04)
56#define DW_LNS_set_column UINT8_C(0x05)
57#define DW_LNS_negate_stmt UINT8_C(0x06)
58#define DW_LNS_set_basic_block UINT8_C(0x07)
59#define DW_LNS_const_add_pc UINT8_C(0x08)
60#define DW_LNS_fixed_advance_pc UINT8_C(0x09)
61#define DW_LNS_set_prologue_end UINT8_C(0x0a)
62#define DW_LNS_set_epilogue_begin UINT8_C(0x0b)
63#define DW_LNS_set_isa UINT8_C(0x0c)
64#define DW_LNS_what_question_mark UINT8_C(0x0d)
65/** @} */
66
67
68/** @name Extended DWARF Line Number Opcodes
69 * @{ */
70#define DW_LNE_end_sequence UINT8_C(1)
71#define DW_LNE_set_address UINT8_C(2)
72#define DW_LNE_define_file UINT8_C(3)
73#define DW_LNE_set_descriminator UINT8_C(4)
74/** @} */
75
76/** @name DIE Tags.
77 * @{ */
78#define DW_TAG_array_type UINT16_C(0x0001)
79#define DW_TAG_class_type UINT16_C(0x0002)
80#define DW_TAG_entry_point UINT16_C(0x0003)
81#define DW_TAG_enumeration_type UINT16_C(0x0004)
82#define DW_TAG_formal_parameter UINT16_C(0x0005)
83#define DW_TAG_imported_declaration UINT16_C(0x0008)
84#define DW_TAG_label UINT16_C(0x000a)
85#define DW_TAG_lexical_block UINT16_C(0x000b)
86#define DW_TAG_member UINT16_C(0x000d)
87#define DW_TAG_pointer_type UINT16_C(0x000f)
88#define DW_TAG_reference_type UINT16_C(0x0010)
89#define DW_TAG_compile_unit UINT16_C(0x0011)
90#define DW_TAG_string_type UINT16_C(0x0012)
91#define DW_TAG_structure_type UINT16_C(0x0013)
92#define DW_TAG_subroutine_type UINT16_C(0x0015)
93#define DW_TAG_typedef UINT16_C(0x0016)
94#define DW_TAG_union_type UINT16_C(0x0017)
95#define DW_TAG_unspecified_parameters UINT16_C(0x0018)
96#define DW_TAG_variant UINT16_C(0x0019)
97#define DW_TAG_common_block UINT16_C(0x001a)
98#define DW_TAG_common_inclusion UINT16_C(0x001b)
99#define DW_TAG_inheritance UINT16_C(0x001c)
100#define DW_TAG_inlined_subroutine UINT16_C(0x001d)
101#define DW_TAG_module UINT16_C(0x001e)
102#define DW_TAG_ptr_to_member_type UINT16_C(0x001f)
103#define DW_TAG_set_type UINT16_C(0x0020)
104#define DW_TAG_subrange_type UINT16_C(0x0021)
105#define DW_TAG_with_stmt UINT16_C(0x0022)
106#define DW_TAG_access_declaration UINT16_C(0x0023)
107#define DW_TAG_base_type UINT16_C(0x0024)
108#define DW_TAG_catch_block UINT16_C(0x0025)
109#define DW_TAG_const_type UINT16_C(0x0026)
110#define DW_TAG_constant UINT16_C(0x0027)
111#define DW_TAG_enumerator UINT16_C(0x0028)
112#define DW_TAG_file_type UINT16_C(0x0029)
113#define DW_TAG_friend UINT16_C(0x002a)
114#define DW_TAG_namelist UINT16_C(0x002b)
115#define DW_TAG_namelist_item UINT16_C(0x002c)
116#define DW_TAG_packed_type UINT16_C(0x002d)
117#define DW_TAG_subprogram UINT16_C(0x002e)
118#define DW_TAG_template_type_parameter UINT16_C(0x002f)
119#define DW_TAG_template_value_parameter UINT16_C(0x0030)
120#define DW_TAG_thrown_type UINT16_C(0x0031)
121#define DW_TAG_try_block UINT16_C(0x0032)
122#define DW_TAG_variant_part UINT16_C(0x0033)
123#define DW_TAG_variable UINT16_C(0x0034)
124#define DW_TAG_volatile_type UINT16_C(0x0035)
125#define DW_TAG_dwarf_procedure UINT16_C(0x0036)
126#define DW_TAG_restrict_type UINT16_C(0x0037)
127#define DW_TAG_interface_type UINT16_C(0x0038)
128#define DW_TAG_namespace UINT16_C(0x0039)
129#define DW_TAG_imported_module UINT16_C(0x003a)
130#define DW_TAG_unspecified_type UINT16_C(0x003b)
131#define DW_TAG_partial_unit UINT16_C(0x003c)
132#define DW_TAG_imported_unit UINT16_C(0x003d)
133#define DW_TAG_condition UINT16_C(0x003f)
134#define DW_TAG_shared_type UINT16_C(0x0040)
135#define DW_TAG_type_unit UINT16_C(0x0041)
136#define DW_TAG_rvalue_reference_type UINT16_C(0x0042)
137#define DW_TAG_template_alias UINT16_C(0x0043)
138#define DW_TAG_lo_user UINT16_C(0x4080)
139#define DW_TAG_hi_user UINT16_C(0xffff)
140/** @} */
141
142
143/** @name DIE Attributes.
144 * @{ */
145#define DW_AT_sibling UINT16_C(0x0001)
146#define DW_AT_location UINT16_C(0x0002)
147#define DW_AT_name UINT16_C(0x0003)
148#define DW_AT_ordering UINT16_C(0x0009)
149#define DW_AT_byte_size UINT16_C(0x000b)
150#define DW_AT_bit_offset UINT16_C(0x000c)
151#define DW_AT_bit_size UINT16_C(0x000d)
152#define DW_AT_stmt_list UINT16_C(0x0010)
153#define DW_AT_low_pc UINT16_C(0x0011)
154#define DW_AT_high_pc UINT16_C(0x0012)
155#define DW_AT_language UINT16_C(0x0013)
156#define DW_AT_discr UINT16_C(0x0015)
157#define DW_AT_discr_value UINT16_C(0x0016)
158#define DW_AT_visibility UINT16_C(0x0017)
159#define DW_AT_import UINT16_C(0x0018)
160#define DW_AT_string_length UINT16_C(0x0019)
161#define DW_AT_common_reference UINT16_C(0x001a)
162#define DW_AT_comp_dir UINT16_C(0x001b)
163#define DW_AT_const_value UINT16_C(0x001c)
164#define DW_AT_containing_type UINT16_C(0x001d)
165#define DW_AT_default_value UINT16_C(0x001e)
166#define DW_AT_inline UINT16_C(0x0020)
167#define DW_AT_is_optional UINT16_C(0x0021)
168#define DW_AT_lower_bound UINT16_C(0x0022)
169#define DW_AT_producer UINT16_C(0x0025)
170#define DW_AT_prototyped UINT16_C(0x0027)
171#define DW_AT_return_addr UINT16_C(0x002a)
172#define DW_AT_start_scope UINT16_C(0x002c)
173#define DW_AT_bit_stride UINT16_C(0x002e)
174#define DW_AT_upper_bound UINT16_C(0x002f)
175#define DW_AT_abstract_origin UINT16_C(0x0031)
176#define DW_AT_accessibility UINT16_C(0x0032)
177#define DW_AT_address_class UINT16_C(0x0033)
178#define DW_AT_artificial UINT16_C(0x0034)
179#define DW_AT_base_types UINT16_C(0x0035)
180#define DW_AT_calling_convention UINT16_C(0x0036)
181#define DW_AT_count UINT16_C(0x0037)
182#define DW_AT_data_member_location UINT16_C(0x0038)
183#define DW_AT_decl_column UINT16_C(0x0039)
184#define DW_AT_decl_file UINT16_C(0x003a)
185#define DW_AT_decl_line UINT16_C(0x003b)
186#define DW_AT_declaration UINT16_C(0x003c)
187#define DW_AT_discr_list UINT16_C(0x003d)
188#define DW_AT_encoding UINT16_C(0x003e)
189#define DW_AT_external UINT16_C(0x003f)
190#define DW_AT_frame_base UINT16_C(0x0040)
191#define DW_AT_friend UINT16_C(0x0041)
192#define DW_AT_identifier_case UINT16_C(0x0042)
193#define DW_AT_macro_info UINT16_C(0x0043)
194#define DW_AT_namelist_item UINT16_C(0x0044)
195#define DW_AT_priority UINT16_C(0x0045)
196#define DW_AT_segment UINT16_C(0x0046)
197#define DW_AT_specification UINT16_C(0x0047)
198#define DW_AT_static_link UINT16_C(0x0048)
199#define DW_AT_type UINT16_C(0x0049)
200#define DW_AT_use_location UINT16_C(0x004a)
201#define DW_AT_variable_parameter UINT16_C(0x004b)
202#define DW_AT_virtuality UINT16_C(0x004c)
203#define DW_AT_vtable_elem_location UINT16_C(0x004d)
204#define DW_AT_allocated UINT16_C(0x004e)
205#define DW_AT_associated UINT16_C(0x004f)
206#define DW_AT_data_location UINT16_C(0x0050)
207#define DW_AT_byte_stride UINT16_C(0x0051)
208#define DW_AT_entry_pc UINT16_C(0x0052)
209#define DW_AT_use_UTF8 UINT16_C(0x0053)
210#define DW_AT_extension UINT16_C(0x0054)
211#define DW_AT_ranges UINT16_C(0x0055)
212#define DW_AT_trampoline UINT16_C(0x0056)
213#define DW_AT_call_column UINT16_C(0x0057)
214#define DW_AT_call_file UINT16_C(0x0058)
215#define DW_AT_call_line UINT16_C(0x0059)
216#define DW_AT_description UINT16_C(0x005a)
217#define DW_AT_binary_scale UINT16_C(0x005b)
218#define DW_AT_decimal_scale UINT16_C(0x005c)
219#define DW_AT_small UINT16_C(0x005d)
220#define DW_AT_decimal_sign UINT16_C(0x005e)
221#define DW_AT_digit_count UINT16_C(0x005f)
222#define DW_AT_picture_string UINT16_C(0x0060)
223#define DW_AT_mutable UINT16_C(0x0061)
224#define DW_AT_threads_scaled UINT16_C(0x0062)
225#define DW_AT_explicit UINT16_C(0x0063)
226#define DW_AT_object_pointer UINT16_C(0x0064)
227#define DW_AT_endianity UINT16_C(0x0065)
228#define DW_AT_elemental UINT16_C(0x0066)
229#define DW_AT_pure UINT16_C(0x0067)
230#define DW_AT_recursive UINT16_C(0x0068)
231#define DW_AT_signature UINT16_C(0x0069)
232#define DW_AT_main_subprogram UINT16_C(0x006a)
233#define DW_AT_data_bit_offset UINT16_C(0x006b)
234#define DW_AT_const_expr UINT16_C(0x006c)
235#define DW_AT_enum_class UINT16_C(0x006d)
236#define DW_AT_linkage_name UINT16_C(0x006e)
237#define DW_AT_lo_user UINT16_C(0x2000)
238#define DW_AT_hi_user UINT16_C(0x3fff)
239/** @} */
240
241/** @name DIE Forms.
242 * @{ */
243#define DW_FORM_addr UINT16_C(0x01)
244/* What was 0x02? */
245#define DW_FORM_block2 UINT16_C(0x03)
246#define DW_FORM_block4 UINT16_C(0x04)
247#define DW_FORM_data2 UINT16_C(0x05)
248#define DW_FORM_data4 UINT16_C(0x06)
249#define DW_FORM_data8 UINT16_C(0x07)
250#define DW_FORM_string UINT16_C(0x08)
251#define DW_FORM_block UINT16_C(0x09)
252#define DW_FORM_block1 UINT16_C(0x0a)
253#define DW_FORM_data1 UINT16_C(0x0b)
254#define DW_FORM_flag UINT16_C(0x0c)
255#define DW_FORM_sdata UINT16_C(0x0d)
256#define DW_FORM_strp UINT16_C(0x0e)
257#define DW_FORM_udata UINT16_C(0x0f)
258#define DW_FORM_ref_addr UINT16_C(0x10)
259#define DW_FORM_ref1 UINT16_C(0x11)
260#define DW_FORM_ref2 UINT16_C(0x12)
261#define DW_FORM_ref4 UINT16_C(0x13)
262#define DW_FORM_ref8 UINT16_C(0x14)
263#define DW_FORM_ref_udata UINT16_C(0x15)
264#define DW_FORM_indirect UINT16_C(0x16)
265#define DW_FORM_sec_offset UINT16_C(0x17)
266#define DW_FORM_exprloc UINT16_C(0x18)
267#define DW_FORM_flag_present UINT16_C(0x19)
268#define DW_FORM_ref_sig8 UINT16_C(0x20)
269/** @} */
270
271/** @name Address classes.
272 * @{ */
273#define DW_ADDR_none UINT8_C(0)
274#define DW_ADDR_i386_near16 UINT8_C(1)
275#define DW_ADDR_i386_far16 UINT8_C(2)
276#define DW_ADDR_i386_huge16 UINT8_C(3)
277#define DW_ADDR_i386_near32 UINT8_C(4)
278#define DW_ADDR_i386_far32 UINT8_C(5)
279/** @} */
280
281
282/*******************************************************************************
283* Structures and Typedefs *
284*******************************************************************************/
285/** Pointer to a DWARF section reader. */
286typedef struct RTDWARFCURSOR *PRTDWARFCURSOR;
287/** Pointer to an attribute descriptor. */
288typedef struct RTDWARFATTRDESC const *PCRTDWARFATTRDESC;
289/** Pointer to a DIE. */
290typedef struct RTDWARFDIE *PRTDWARFDIE;
291/** Pointer to a const DIE. */
292typedef struct RTDWARFDIE const *PCRTDWARFDIE;
293
294/**
295 * DWARF sections.
296 */
297typedef enum krtDbgModDwarfSect
298{
299 krtDbgModDwarfSect_abbrev = 0,
300 krtDbgModDwarfSect_aranges,
301 krtDbgModDwarfSect_frame,
302 krtDbgModDwarfSect_info,
303 krtDbgModDwarfSect_inlined,
304 krtDbgModDwarfSect_line,
305 krtDbgModDwarfSect_loc,
306 krtDbgModDwarfSect_macinfo,
307 krtDbgModDwarfSect_pubnames,
308 krtDbgModDwarfSect_pubtypes,
309 krtDbgModDwarfSect_ranges,
310 krtDbgModDwarfSect_str,
311 krtDbgModDwarfSect_types,
312 /** End of valid parts (exclusive). */
313 krtDbgModDwarfSect_End
314} krtDbgModDwarfSect;
315
316/**
317 * Abbreviation cache entry.
318 */
319typedef struct RTDWARFABBREV
320{
321 /** Whether this entry is filled in or not. */
322 bool fFilled;
323 /** Whether there are children or not. */
324 bool fChildren;
325 /** The tag. */
326 uint16_t uTag;
327 /** Offset into the abbrev section of the specification pairs. */
328 uint32_t offSpec;
329} RTDWARFABBREV;
330/** Pointer to an abbreviation cache entry. */
331typedef RTDWARFABBREV *PRTDWARFABBREV;
332/** Pointer to a const abbreviation cache entry. */
333typedef RTDWARFABBREV const *PCRTDWARFABBREV;
334
335
336/**
337 * The instance data of the DWARF reader.
338 */
339typedef struct RTDBGMODDWARF
340{
341 /** The debug container containing doing the real work. */
342 RTDBGMOD hCnt;
343 /** Pointer to back to the debug info module (no reference ofc). */
344 PRTDBGMODINT pMod;
345
346 /** DWARF debug info sections. */
347 struct
348 {
349 /** The file offset of the part. */
350 RTFOFF offFile;
351 /** The size of the part. */
352 size_t cb;
353 /** The memory mapping of the part. */
354 void const *pv;
355 /** Set if present. */
356 bool fPresent;
357 } aSections[krtDbgModDwarfSect_End];
358
359 /** The offset into the abbreviation section of the current cache. */
360 uint32_t offCachedAbbrev;
361 /** The number of cached abbreviations we've allocated space for. */
362 uint32_t cCachedAbbrevsAlloced;
363 /** Used for range checking cache lookups. */
364 uint32_t cCachedAbbrevs;
365 /** Array of cached abbreviations, indexed by code. */
366 PRTDWARFABBREV paCachedAbbrevs;
367 /** Used by rtDwarfAbbrev_Lookup when the result is uncachable. */
368 RTDWARFABBREV LookupAbbrev;
369
370 /** The list of compilation units (RTDWARFDIE). */
371 RTLISTNODE CompileUnitList;
372} RTDBGMODDWARF;
373/** Pointer to instance data of the DWARF reader. */
374typedef RTDBGMODDWARF *PRTDBGMODDWARF;
375
376/**
377 * DWARF cursor for reading byte data.
378 */
379typedef struct RTDWARFCURSOR
380{
381 /** The current position. */
382 uint8_t const *pb;
383 /** The number of bytes left to read. */
384 size_t cbLeft;
385 /** The number of bytes left to read in the current unit. */
386 size_t cbUnitLeft;
387 /** The DWARF debug info reader instance. */
388 PRTDBGMODDWARF pDwarfMod;
389 /** Set if this is 64-bit DWARF, clear if 32-bit. */
390 bool f64bitDwarf;
391 /** Set if the format endian is native, clear if endian needs to be
392 * inverted. */
393 bool fNativEndian;
394 /** The size of a native address. */
395 uint8_t cbNativeAddr;
396 /** The cursor status code. This is VINF_SUCCESS until some error
397 * occurs. */
398 int rc;
399 /** The start of the area covered by the cursor.
400 * Used for repositioning the cursor relative to the start of a section. */
401 uint8_t const *pbStart;
402 /** The section. */
403 krtDbgModDwarfSect enmSect;
404} RTDWARFCURSOR;
405
406
407/**
408 * DWARF line number program state.
409 */
410typedef struct RTDWARFLINESTATE
411{
412 /** Virtual Line Number Machine Registers. */
413 struct
414 {
415 uint64_t uAddress;
416 uint64_t idxOp;
417 uint32_t iFile;
418 uint32_t uLine;
419 uint32_t uColumn;
420 bool fIsStatement;
421 bool fBasicBlock;
422 bool fEndSequence;
423 bool fPrologueEnd;
424 bool fEpilogueBegin;
425 uint32_t uIsa;
426 uint32_t uDiscriminator;
427 } Regs;
428 /** @} */
429
430 /** Header. */
431 struct
432 {
433 uint32_t uVer;
434 uint64_t offFirstOpcode;
435 uint8_t cbMinInstr;
436 uint8_t cMaxOpsPerInstr;
437 uint8_t u8DefIsStmt;
438 int8_t s8LineBase;
439 uint8_t u8LineRange;
440 uint8_t u8OpcodeBase;
441 uint8_t const *pacStdOperands;
442 } Hdr;
443
444 /** @name Include Path Table (0-based)
445 * @{ */
446 const char **papszIncPaths;
447 uint32_t cIncPaths;
448 /** @} */
449
450 /** @name File Name Table (0-based, dummy zero entry)
451 * @{ */
452 char **papszFileNames;
453 uint32_t cFileNames;
454 /** @} */
455
456 /** The DWARF debug info reader instance. */
457 PRTDBGMODDWARF pDwarfMod;
458} RTDWARFLINESTATE;
459/** Pointer to a DWARF line number program state. */
460typedef RTDWARFLINESTATE *PRTDWARFLINESTATE;
461
462
463/**
464 * Decodes an attribute and stores it in the specified DIE member field.
465 *
466 * @returns IPRT status code.
467 * @param pDie Pointer to the DIE structure.
468 * @param pbMember Pointer to the first byte in the member.
469 * @param pDesc The attribute descriptor.
470 * @param uForm The data form.
471 * @param pDataCursor The cursor to read data from.
472 */
473typedef DECLCALLBACK(int) FNRTDWARFATTRDECODER(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
474 uint32_t uForm, PRTDWARFCURSOR pCursor);
475/** Pointer to an attribute decoder callback. */
476typedef FNRTDWARFATTRDECODER *PFNRTDWARFATTRDECODER;
477
478/**
479 * Attribute descriptor.
480 */
481typedef struct RTDWARFATTRDESC
482{
483 /** The attribute. */
484 uint8_t uAttr;
485 /** The data member size and initialization method. */
486 uint8_t cbInit;
487 /** The data member offset. */
488 uint16_t off;
489 /** The decoder function. */
490 PFNRTDWARFATTRDECODER pfnDecoder;
491} RTDWARFATTRDESC;
492
493/** Define a attribute entry. */
494#define ATTR_ENTRY(a_uAttr, a_Struct, a_Member, a_Init, a_pfnDecoder) \
495 { \
496 a_uAttr, \
497 a_Init | ((uint8_t)RT_SIZEOFMEMB(a_Struct, a_Member) & ATTR_SIZE_MASK), \
498 (uint16_t)RT_OFFSETOF(a_Struct, a_Member), \
499 a_pfnDecoder\
500 }
501
502/** @name Attribute size and init methods.
503 * @{ */
504#define ATTR_INIT_ZERO UINT8_C(0x00)
505#define ATTR_INIT_FFFS UINT8_C(0x80)
506#define ATTR_INIT_MASK UINT8_C(0x80)
507#define ATTR_SIZE_MASK UINT8_C(0x3f)
508#define ATTR_GET_SIZE(a_pAttrDesc) ((a_pAttrDesc)->cbInit & ATTR_SIZE_MASK)
509/** @} */
510
511
512/**
513 * DIE descriptor.
514 */
515typedef struct RTDWARFDIEDESC
516{
517 /** The size of the DIE. */
518 size_t cbDie;
519 /** The number of attributes. */
520 size_t cAttributes;
521 /** The */
522 PCRTDWARFATTRDESC paAttributes;
523} RTDWARFDIEDESC;
524typedef struct RTDWARFDIEDESC const *PCRTDWARFDIEDESC;
525/** DIE descriptor initializer. */
526#define DIE_DESC_INIT(a_Type, a_aAttrs) { sizeof(a_Type), RT_ELEMENTS(a_aAttrs), &a_aAttrs[0] }
527
528
529/**
530 * DIE core structure, all inherits (starts with) this.
531 */
532typedef struct RTDWARFDIE
533{
534 /** Pointer to the parent node. NULL if root unit. */
535 struct RTDWARFDIE *pParent;
536 /** Our node in the sibling list. */
537 RTLISTNODE SiblingNode;
538 /** List of children. */
539 RTLISTNODE ChildList;
540 /** The number of attributes successfully decoded. */
541 uint8_t cDecodedAttrs;
542 /** The number of unknown or otherwise unhandled attributes. */
543 uint8_t cUnhandledAttrs;
544 /** The date tag, indicating which union structure to use. */
545 uint16_t uTag;
546 /** Offset of the abbreviation specification (within debug_abbrev). */
547 uint32_t offSpec;
548} RTDWARFDIE;
549
550
551/**
552 * DWARF address structure.
553 */
554typedef struct RTDWARFADDR
555{
556 /** The address. */
557 uint64_t uAddress;
558} RTDWARFADDR;
559typedef RTDWARFADDR *PRTDWARFADDR;
560typedef RTDWARFADDR const *PCRTDWARFADDR;
561
562
563/**
564 * DWARF address range.
565 */
566typedef struct RTDWARFADDRRANGE
567{
568 uint64_t uLowAddress;
569 uint64_t uHighAddress;
570 uint8_t const *pbRanges; /* ?? */
571 uint8_t cAttrs : 2;
572 uint8_t fHaveLowAddress : 1;
573 uint8_t fHaveHighAddress : 1;
574 uint8_t fHaveRanges : 1;
575} RTDWARFADDRRANGE;
576typedef RTDWARFADDRRANGE *PRTDWARFADDRRANGE;
577typedef RTDWARFADDRRANGE const *PCRTDWARFADDRRANGE;
578
579/** What a RTDWARFREF is relative to. */
580typedef enum krtDwarfRef
581{
582 krtDwarfRef_NotSet,
583 krtDwarfRef_LineSection,
584 krtDwarfRef_LocSection,
585 krtDwarfRef_RangesSection,
586 krtDwarfRef_InfoSection,
587 krtDwarfRef_SameUnit,
588 krtDwarfRef_TypeId64
589} krtDwarfRef;
590
591/**
592 * DWARF reference.
593 */
594typedef struct RTDWARFREF
595{
596 /** The offset. */
597 uint64_t off;
598 /** What the offset is relative to. */
599 krtDwarfRef enmWrt;
600} RTDWARFREF;
601typedef RTDWARFREF *PRTDWARFREF;
602typedef RTDWARFREF const *PCRTDWARFREF;
603
604
605
606/*******************************************************************************
607* Internal Functions *
608*******************************************************************************/
609static FNRTDWARFATTRDECODER rtDwarfDecode_Address;
610static FNRTDWARFATTRDECODER rtDwarfDecode_Bool;
611static FNRTDWARFATTRDECODER rtDwarfDecode_LowHighPc;
612static FNRTDWARFATTRDECODER rtDwarfDecode_Ranges;
613static FNRTDWARFATTRDECODER rtDwarfDecode_Reference;
614static FNRTDWARFATTRDECODER rtDwarfDecode_SectOff;
615static FNRTDWARFATTRDECODER rtDwarfDecode_String;
616static FNRTDWARFATTRDECODER rtDwarfDecode_UnsignedInt;
617
618
619/*******************************************************************************
620* Global Variables *
621*******************************************************************************/
622/** RTDWARFDIE description. */
623static const RTDWARFDIEDESC g_CoreDieDesc = { sizeof(RTDWARFDIE), 0, NULL };
624
625
626/**
627 * DW_TAG_compile_unit & DW_TAG_partial_unit.
628 */
629typedef struct RTDWARFDIECOMPILEUNIT
630{
631 /** The DIE core structure. */
632 RTDWARFDIE Core;
633 /** The unit name. */
634 const char *pszName;
635 /** The address range of the code belonging to this unit. */
636 RTDWARFADDRRANGE PcRange;
637 /** The language name. */
638 uint8_t uLanguage;
639 /** The identifier case. */
640 uint8_t uIdentifierCase;
641 /** String are UTF-8 encoded. If not set, the encoding is
642 * unknown. */
643 bool fUseUtf8;
644 /** The unit contains main() or equivalent. */
645 bool fMainFunction;
646 /** The line numbers for this unit. */
647 RTDWARFREF StmtListRef;
648 /** The macro information for this unit. */
649 RTDWARFREF MacroInfoRef;
650 /** Reference to the base types. */
651 RTDWARFREF BaseTypesRef;
652 /** Working directory for the unit. */
653 const char *pszCurDir;
654 /** The name of the compiler or whatever that produced this unit. */
655 const char *pszProducer;
656
657 /** @name From the unit header.
658 * @{ */
659 /** The offset into debug_info of this unit (for references). */
660 uint64_t offUnit;
661 /** The length of this unit. */
662 uint64_t cbUnit;
663 /** The offset into debug_abbrev of the abbreviation for this unit. */
664 uint64_t offAbbrev;
665 /** The native address size. */
666 uint8_t cbNativeAddr;
667 /** The DWARF version. */
668 uint8_t uDwarfVer;
669 /** @} */
670} RTDWARFDIECOMPILEUNIT;
671typedef RTDWARFDIECOMPILEUNIT *PRTDWARFDIECOMPILEUNIT;
672
673
674/** RTDWARFDIECOMPILEUNIT attributes. */
675static const RTDWARFATTRDESC g_aCompileUnitAttrs[] =
676{
677 ATTR_ENTRY(DW_AT_name, RTDWARFDIECOMPILEUNIT, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
678 ATTR_ENTRY(DW_AT_low_pc, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
679 ATTR_ENTRY(DW_AT_high_pc, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
680 ATTR_ENTRY(DW_AT_ranges, RTDWARFDIECOMPILEUNIT, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_Ranges),
681 ATTR_ENTRY(DW_AT_language, RTDWARFDIECOMPILEUNIT, uLanguage, ATTR_INIT_ZERO, rtDwarfDecode_UnsignedInt),
682 ATTR_ENTRY(DW_AT_macro_info, RTDWARFDIECOMPILEUNIT, MacroInfoRef, ATTR_INIT_ZERO, rtDwarfDecode_SectOff),
683 ATTR_ENTRY(DW_AT_stmt_list, RTDWARFDIECOMPILEUNIT, StmtListRef, ATTR_INIT_ZERO, rtDwarfDecode_SectOff),
684 ATTR_ENTRY(DW_AT_comp_dir, RTDWARFDIECOMPILEUNIT, pszCurDir, ATTR_INIT_ZERO, rtDwarfDecode_String),
685 ATTR_ENTRY(DW_AT_producer, RTDWARFDIECOMPILEUNIT, pszProducer, ATTR_INIT_ZERO, rtDwarfDecode_String),
686 ATTR_ENTRY(DW_AT_identifier_case, RTDWARFDIECOMPILEUNIT, uIdentifierCase,ATTR_INIT_ZERO, rtDwarfDecode_UnsignedInt),
687 ATTR_ENTRY(DW_AT_base_types, RTDWARFDIECOMPILEUNIT, BaseTypesRef, ATTR_INIT_ZERO, rtDwarfDecode_Reference),
688 ATTR_ENTRY(DW_AT_use_UTF8, RTDWARFDIECOMPILEUNIT, fUseUtf8, ATTR_INIT_ZERO, rtDwarfDecode_Bool),
689 ATTR_ENTRY(DW_AT_main_subprogram, RTDWARFDIECOMPILEUNIT, fMainFunction, ATTR_INIT_ZERO, rtDwarfDecode_Bool)
690};
691
692/** RTDWARFDIECOMPILEUNIT description. */
693static const RTDWARFDIEDESC g_CompileUnitDesc = DIE_DESC_INIT(RTDWARFDIECOMPILEUNIT, g_aCompileUnitAttrs);
694
695
696/**
697 * DW_TAG_subprogram.
698 */
699typedef struct RTDWARFDIESUBPROGRAM
700{
701 /** The DIE core structure. */
702 RTDWARFDIE Core;
703 /** The name. */
704 const char *pszName;
705 /** The linkage name. */
706 const char *pszLinkageName;
707 /** The address range of the code belonging to this unit. */
708 RTDWARFADDRRANGE PcRange;
709 /** The first instruction in the function. */
710 RTDWARFADDR EntryPc;
711} RTDWARFDIESUBPROGRAM;
712/** Pointer to a DW_TAG_subprogram DIE. */
713typedef RTDWARFDIESUBPROGRAM *PRTDWARFDIESUBPROGRAM;
714/** Pointer to a const DW_TAG_subprogram DIE. */
715typedef RTDWARFDIESUBPROGRAM const *PCRTDWARFDIESUBPROGRAM;
716
717
718/** RTDWARFDIESUBPROGRAM attributes. */
719static const RTDWARFATTRDESC g_aSubProgramAttrs[] =
720{
721 ATTR_ENTRY(DW_AT_name, RTDWARFDIESUBPROGRAM, pszName, ATTR_INIT_ZERO, rtDwarfDecode_String),
722 ATTR_ENTRY(DW_AT_linkage_name, RTDWARFDIESUBPROGRAM, pszLinkageName, ATTR_INIT_ZERO, rtDwarfDecode_String),
723 ATTR_ENTRY(DW_AT_low_pc, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
724 ATTR_ENTRY(DW_AT_high_pc, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_LowHighPc),
725 ATTR_ENTRY(DW_AT_ranges, RTDWARFDIESUBPROGRAM, PcRange, ATTR_INIT_ZERO, rtDwarfDecode_Ranges),
726 ATTR_ENTRY(DW_AT_entry_pc, RTDWARFDIESUBPROGRAM, EntryPc, ATTR_INIT_ZERO, rtDwarfDecode_Address),
727};
728
729/** RTDWARFDIESUBPROGRAM description. */
730static const RTDWARFDIEDESC g_SubProgramDesc = DIE_DESC_INIT(RTDWARFDIESUBPROGRAM, g_aSubProgramAttrs);
731
732
733/**
734 * Tag names and descriptors.
735 */
736static const struct RTDWARFTAGDESC
737{
738 /** The tag value. */
739 uint16_t uTag;
740 /** The tag name as string. */
741 const char *pszName;
742 /** The DIE descriptor to use. */
743 PCRTDWARFDIEDESC pDesc;
744} g_aTagDescs[] =
745{
746#define TAGDESC(a_Name, a_pDesc) { DW_ ## a_Name, #a_Name, a_pDesc }
747#define TAGDESC_EMPTY() { 0, NULL, NULL }
748#define TAGDESC_CORE(a_Name) TAGDESC(a_Name, &g_CoreDieDesc)
749 TAGDESC_EMPTY(), /* 0x00 */
750 TAGDESC_CORE(TAG_array_type),
751 TAGDESC_CORE(TAG_class_type),
752 TAGDESC_CORE(TAG_entry_point),
753 TAGDESC_CORE(TAG_enumeration_type), /* 0x04 */
754 TAGDESC_CORE(TAG_formal_parameter),
755 TAGDESC_EMPTY(),
756 TAGDESC_EMPTY(),
757 TAGDESC_CORE(TAG_imported_declaration), /* 0x08 */
758 TAGDESC_EMPTY(),
759 TAGDESC_CORE(TAG_label),
760 TAGDESC_CORE(TAG_lexical_block),
761 TAGDESC_EMPTY(), /* 0x0c */
762 TAGDESC_CORE(TAG_member),
763 TAGDESC_EMPTY(),
764 TAGDESC_CORE(TAG_pointer_type),
765 TAGDESC_CORE(TAG_reference_type), /* 0x10 */
766 TAGDESC_CORE(TAG_compile_unit),
767 TAGDESC_CORE(TAG_string_type),
768 TAGDESC_CORE(TAG_structure_type),
769 TAGDESC_EMPTY(), /* 0x14 */
770 TAGDESC_CORE(TAG_subroutine_type),
771 TAGDESC_CORE(TAG_typedef),
772 TAGDESC_CORE(TAG_union_type),
773 TAGDESC_CORE(TAG_unspecified_parameters), /* 0x18 */
774 TAGDESC_CORE(TAG_variant),
775 TAGDESC_CORE(TAG_common_block),
776 TAGDESC_CORE(TAG_common_inclusion),
777 TAGDESC_CORE(TAG_inheritance), /* 0x1c */
778 TAGDESC_CORE(TAG_inlined_subroutine),
779 TAGDESC_CORE(TAG_module),
780 TAGDESC_CORE(TAG_ptr_to_member_type),
781 TAGDESC_CORE(TAG_set_type), /* 0x20 */
782 TAGDESC_CORE(TAG_subrange_type),
783 TAGDESC_CORE(TAG_with_stmt),
784 TAGDESC_CORE(TAG_access_declaration),
785 TAGDESC_CORE(TAG_base_type), /* 0x24 */
786 TAGDESC_CORE(TAG_catch_block),
787 TAGDESC_CORE(TAG_const_type),
788 TAGDESC_CORE(TAG_constant),
789 TAGDESC_CORE(TAG_enumerator), /* 0x28 */
790 TAGDESC_CORE(TAG_file_type),
791 TAGDESC_CORE(TAG_friend),
792 TAGDESC_CORE(TAG_namelist),
793 TAGDESC_CORE(TAG_namelist_item), /* 0x2c */
794 TAGDESC_CORE(TAG_packed_type),
795 TAGDESC(TAG_subprogram, &g_SubProgramDesc),
796 TAGDESC_CORE(TAG_template_type_parameter),
797 TAGDESC_CORE(TAG_template_value_parameter), /* 0x30 */
798 TAGDESC_CORE(TAG_thrown_type),
799 TAGDESC_CORE(TAG_try_block),
800 TAGDESC_CORE(TAG_variant_part),
801 TAGDESC_CORE(TAG_variable), /* 0x34 */
802 TAGDESC_CORE(TAG_volatile_type),
803 TAGDESC_CORE(TAG_dwarf_procedure),
804 TAGDESC_CORE(TAG_restrict_type),
805 TAGDESC_CORE(TAG_interface_type), /* 0x38 */
806 TAGDESC_CORE(TAG_namespace),
807 TAGDESC_CORE(TAG_imported_module),
808 TAGDESC_CORE(TAG_unspecified_type),
809 TAGDESC_CORE(TAG_partial_unit), /* 0x3c */
810 TAGDESC_CORE(TAG_imported_unit),
811 TAGDESC_EMPTY(),
812 TAGDESC_CORE(TAG_condition),
813 TAGDESC_CORE(TAG_shared_type), /* 0x40 */
814 TAGDESC_CORE(TAG_type_unit),
815 TAGDESC_CORE(TAG_rvalue_reference_type),
816 TAGDESC_CORE(TAG_template_alias)
817#undef TAGDESC
818#undef TAGDESC_EMPTY
819#undef TAGDESC_CORE
820};
821
822
823/** @callback_method_impl{FNRTLDRENUMSEGS} */
824static DECLCALLBACK(int) rtDbgModHlpAddSegmentCallback(RTLDRMOD hLdrMod, PCRTLDRSEG pSeg, void *pvUser)
825{
826 PRTDBGMODINT pMod = (PRTDBGMODINT)pvUser;
827 Log(("Segment %.*s: LinkAddress=%#llx RVA=%#llx cb=%#llx\n",
828 pSeg->cchName, pSeg->pchName, (uint64_t)pSeg->LinkAddress, (uint64_t)pSeg->RVA, pSeg->cb));
829 RTLDRADDR cb = RT_MAX(pSeg->cb, pSeg->cbMapped);
830#if 1
831 return pMod->pDbgVt->pfnSegmentAdd(pMod, pSeg->RVA, cb, pSeg->pchName, pSeg->cchName, 0 /*fFlags*/, NULL);
832#else
833 return pMod->pDbgVt->pfnSegmentAdd(pMod, pSeg->LinkAddress, cb, pSeg->pchName, pSeg->cchName, 0 /*fFlags*/, NULL);
834#endif
835}
836
837
838/**
839 * Calls pfnSegmentAdd for each segment in the executable image.
840 *
841 * @returns IPRT status code.
842 * @param pMod The debug module.
843 */
844DECLHIDDEN(int) rtDbgModHlpAddSegmentsFromImage(PRTDBGMODINT pMod)
845{
846 AssertReturn(pMod->pImgVt, VERR_INTERNAL_ERROR_2);
847 return pMod->pImgVt->pfnEnumSegments(pMod, rtDbgModHlpAddSegmentCallback, pMod);
848}
849
850
851
852
853/**
854 * Loads a DWARF section from the image file.
855 *
856 * @returns IPRT status code.
857 * @param pThis The DWARF instance.
858 * @param enmSect The section to load.
859 */
860static int rtDbgModDwarfLoadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
861{
862 /*
863 * Don't load stuff twice.
864 */
865 if (pThis->aSections[enmSect].pv)
866 return VINF_SUCCESS;
867
868 /*
869 * Sections that are not present cannot be loaded, treat them like they
870 * are empty
871 */
872 if (!pThis->aSections[enmSect].fPresent)
873 {
874 Assert(pThis->aSections[enmSect].cb);
875 return VINF_SUCCESS;
876 }
877 if (!pThis->aSections[enmSect].cb)
878 return VINF_SUCCESS;
879
880 /*
881 * Sections must be readable with the current image interface.
882 */
883 if (pThis->aSections[enmSect].offFile < 0)
884 return VERR_OUT_OF_RANGE;
885
886 /*
887 * Do the job.
888 */
889 return pThis->pMod->pImgVt->pfnMapPart(pThis->pMod, pThis->aSections[enmSect].offFile, pThis->aSections[enmSect].cb,
890 &pThis->aSections[enmSect].pv);
891}
892
893
894/**
895 * Unloads a DWARF section previously mapped by rtDbgModDwarfLoadSection.
896 *
897 * @returns IPRT status code.
898 * @param pThis The DWARF instance.
899 * @param enmSect The section to unload.
900 */
901static int rtDbgModDwarfUnloadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
902{
903 if (!pThis->aSections[enmSect].pv)
904 return VINF_SUCCESS;
905
906 int rc = pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[enmSect].cb, &pThis->aSections[enmSect].pv);
907 AssertRC(rc);
908 return rc;
909}
910
911
912/**
913 * Converts to UTF-8 or otherwise makes sure it's valid UTF-8.
914 *
915 * @returns IPRT status code.
916 * @param pThis The DWARF instance.
917 * @param ppsz Pointer to the string pointer. May be
918 * reallocated (RTStr*).
919 */
920static int rtDbgModDwarfStringToUtf8(PRTDBGMODDWARF pThis, char **ppsz)
921{
922 RTStrPurgeEncoding(*ppsz);
923 return VINF_SUCCESS;
924}
925
926
927/**
928 * Convers a link address into a segment+offset or RVA.
929 *
930 * @returns IPRT status code.
931 * @param pThis The DWARF instance.
932 * @param LinkAddress The address to convert..
933 * @param piSeg The segment index.
934 * @param poffSeg Where to return the segment offset.
935 */
936static int rtDbgModDwarfLinkAddressToSegOffset(PRTDBGMODDWARF pThis, uint64_t LinkAddress,
937 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
938{
939 return pThis->pMod->pImgVt->pfnLinkAddressToSegOffset(pThis->pMod, LinkAddress, piSeg, poffSeg);
940}
941
942
943/*
944 *
945 * DWARF Cursor.
946 * DWARF Cursor.
947 * DWARF Cursor.
948 *
949 */
950
951
952/**
953 * Reads a 8-bit unsigned integer and advances the cursor.
954 *
955 * @returns 8-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
956 * uErrValue is returned.
957 * @param pCursor The cursor.
958 * @param uErrValue What to return on read error.
959 */
960static uint8_t rtDwarfCursor_GetU8(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
961{
962 if (pCursor->cbUnitLeft < 1)
963 {
964 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
965 return uErrValue;
966 }
967
968 uint8_t u8 = pCursor->pb[0];
969 pCursor->pb += 1;
970 pCursor->cbUnitLeft -= 1;
971 pCursor->cbLeft -= 1;
972 return u8;
973}
974
975
976/**
977 * Reads a 16-bit unsigned integer and advances the cursor.
978 *
979 * @returns 16-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
980 * uErrValue is returned.
981 * @param pCursor The cursor.
982 * @param uErrValue What to return on read error.
983 */
984static uint16_t rtDwarfCursor_GetU16(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
985{
986 if (pCursor->cbUnitLeft < 2)
987 {
988 pCursor->pb += pCursor->cbUnitLeft;
989 pCursor->cbLeft -= pCursor->cbUnitLeft;
990 pCursor->cbUnitLeft = 0;
991 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
992 return uErrValue;
993 }
994
995 uint16_t u16 = RT_MAKE_U16(pCursor->pb[0], pCursor->pb[1]);
996 pCursor->pb += 2;
997 pCursor->cbUnitLeft -= 2;
998 pCursor->cbLeft -= 2;
999 if (!pCursor->fNativEndian)
1000 u16 = RT_BSWAP_U16(u16);
1001 return u16;
1002}
1003
1004
1005/**
1006 * Reads a 32-bit unsigned integer and advances the cursor.
1007 *
1008 * @returns 32-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
1009 * uErrValue is returned.
1010 * @param pCursor The cursor.
1011 * @param uErrValue What to return on read error.
1012 */
1013static uint32_t rtDwarfCursor_GetU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
1014{
1015 if (pCursor->cbUnitLeft < 4)
1016 {
1017 pCursor->pb += pCursor->cbUnitLeft;
1018 pCursor->cbLeft -= pCursor->cbUnitLeft;
1019 pCursor->cbUnitLeft = 0;
1020 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1021 return uErrValue;
1022 }
1023
1024 uint32_t u32 = RT_MAKE_U32_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3]);
1025 pCursor->pb += 4;
1026 pCursor->cbUnitLeft -= 4;
1027 pCursor->cbLeft -= 4;
1028 if (!pCursor->fNativEndian)
1029 u32 = RT_BSWAP_U32(u32);
1030 return u32;
1031}
1032
1033
1034/**
1035 * Reads a 64-bit unsigned integer and advances the cursor.
1036 *
1037 * @returns 64-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
1038 * uErrValue is returned.
1039 * @param pCursor The cursor.
1040 * @param uErrValue What to return on read error.
1041 */
1042static uint64_t rtDwarfCursor_GetU64(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1043{
1044 if (pCursor->cbUnitLeft < 8)
1045 {
1046 pCursor->pb += pCursor->cbUnitLeft;
1047 pCursor->cbLeft -= pCursor->cbUnitLeft;
1048 pCursor->cbUnitLeft = 0;
1049 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1050 return uErrValue;
1051 }
1052
1053 uint64_t u64 = RT_MAKE_U64_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3],
1054 pCursor->pb[4], pCursor->pb[5], pCursor->pb[6], pCursor->pb[7]);
1055 pCursor->pb += 8;
1056 pCursor->cbUnitLeft -= 8;
1057 pCursor->cbLeft -= 8;
1058 if (!pCursor->fNativEndian)
1059 u64 = RT_BSWAP_U64(u64);
1060 return u64;
1061}
1062
1063
1064/**
1065 * Reads an unsigned LEB128 encoded number.
1066 *
1067 * @returns unsigned 64-bit number. On error RTDWARFCURSOR::rc is set and @a
1068 * uErrValue is returned.
1069 * @param pCursor The cursor.
1070 * @param uErrValue The value to return on error.
1071 */
1072static uint64_t rtDwarfCursor_GetULeb128(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1073{
1074 if (pCursor->cbUnitLeft < 1)
1075 {
1076 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1077 return uErrValue;
1078 }
1079
1080 /*
1081 * Special case - single byte.
1082 */
1083 uint8_t b = pCursor->pb[0];
1084 if (!(b & 0x80))
1085 {
1086 pCursor->pb += 1;
1087 pCursor->cbUnitLeft -= 1;
1088 pCursor->cbLeft -= 1;
1089 return b;
1090 }
1091
1092 /*
1093 * Generic case.
1094 */
1095 /* Decode. */
1096 uint32_t off = 1;
1097 uint64_t u64Ret = b & 0x7f;
1098 do
1099 {
1100 if (off == pCursor->cbUnitLeft)
1101 {
1102 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1103 u64Ret = uErrValue;
1104 break;
1105 }
1106 b = pCursor->pb[off];
1107 u64Ret |= (b & 0x7f) << off * 7;
1108 off++;
1109 } while (b & 0x80);
1110
1111 /* Update the cursor. */
1112 pCursor->pb += off;
1113 pCursor->cbUnitLeft -= off;
1114 pCursor->cbLeft -= off;
1115
1116 /* Check the range. */
1117 uint32_t cBits = off * 7;
1118 if (cBits > 64)
1119 {
1120 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1121 u64Ret = uErrValue;
1122 }
1123
1124 return u64Ret;
1125}
1126
1127
1128/**
1129 * Reads a signed LEB128 encoded number.
1130 *
1131 * @returns signed 64-bit number. On error RTDWARFCURSOR::rc is set and @a
1132 * uErrValue is returned.
1133 * @param pCursor The cursor.
1134 * @param sErrValue The value to return on error.
1135 */
1136static int64_t rtDwarfCursor_GetSLeb128(PRTDWARFCURSOR pCursor, int64_t sErrValue)
1137{
1138 if (pCursor->cbUnitLeft < 1)
1139 {
1140 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1141 return sErrValue;
1142 }
1143
1144 /*
1145 * Special case - single byte.
1146 */
1147 uint8_t b = pCursor->pb[0];
1148 if (!(b & 0x80))
1149 {
1150 pCursor->pb += 1;
1151 pCursor->cbUnitLeft -= 1;
1152 pCursor->cbLeft -= 1;
1153 if (b & 0x40)
1154 b |= 0x80;
1155 return (int8_t)b;
1156 }
1157
1158 /*
1159 * Generic case.
1160 */
1161 /* Decode it. */
1162 uint32_t off = 1;
1163 uint64_t u64Ret = b & 0x7f;
1164 do
1165 {
1166 if (off == pCursor->cbUnitLeft)
1167 {
1168 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1169 u64Ret = (uint64_t)sErrValue;
1170 break;
1171 }
1172 b = pCursor->pb[off];
1173 u64Ret |= (b & 0x7f) << off * 7;
1174 off++;
1175 } while (b & 0x80);
1176
1177 /* Update cursor. */
1178 pCursor->pb += off;
1179 pCursor->cbUnitLeft -= off;
1180 pCursor->cbLeft -= off;
1181
1182 /* Check the range. */
1183 uint32_t cBits = off * 7;
1184 if (cBits > 64)
1185 {
1186 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1187 u64Ret = (uint64_t)sErrValue;
1188 }
1189 /* Sign extend the value. */
1190 else if (u64Ret & RT_BIT_64(cBits - 1))
1191 u64Ret |= ~(RT_BIT_64(cBits - 1) - 1);
1192
1193 return (int64_t)u64Ret;
1194}
1195
1196
1197/**
1198 * Reads an unsigned LEB128 encoded number, max 32-bit width.
1199 *
1200 * @returns unsigned 32-bit number. On error RTDWARFCURSOR::rc is set and @a
1201 * uErrValue is returned.
1202 * @param pCursor The cursor.
1203 * @param uErrValue The value to return on error.
1204 */
1205static uint32_t rtDwarfCursor_GetULeb128AsU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
1206{
1207 uint64_t u64 = rtDwarfCursor_GetULeb128(pCursor, uErrValue);
1208 if (u64 > UINT32_MAX)
1209 {
1210 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1211 return uErrValue;
1212 }
1213 return (uint32_t)u64;
1214}
1215
1216
1217/**
1218 * Reads a signed LEB128 encoded number, max 32-bit width.
1219 *
1220 * @returns signed 32-bit number. On error RTDWARFCURSOR::rc is set and @a
1221 * uErrValue is returned.
1222 * @param pCursor The cursor.
1223 * @param sErrValue The value to return on error.
1224 */
1225static int32_t rtDwarfCursor_GetSLeb128AsS32(PRTDWARFCURSOR pCursor, int32_t sErrValue)
1226{
1227 int64_t s64 = rtDwarfCursor_GetSLeb128(pCursor, sErrValue);
1228 if (s64 > INT32_MAX || s64 < INT32_MIN)
1229 {
1230 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1231 return sErrValue;
1232 }
1233 return (int32_t)s64;
1234}
1235
1236
1237/**
1238 * Skips a LEB128 encoded number.
1239 *
1240 * @returns IPRT status code.
1241 * @param pCursor The cursor.
1242 */
1243static int rtDwarfCursor_SkipLeb128(PRTDWARFCURSOR pCursor)
1244{
1245 if (RT_FAILURE(pCursor->rc))
1246 return pCursor->rc;
1247
1248 if (pCursor->cbUnitLeft < 1)
1249 return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1250
1251 uint32_t offSkip = 1;
1252 if (pCursor->pb[0] & 0x80)
1253 do
1254 {
1255 if (offSkip == pCursor->cbUnitLeft)
1256 {
1257 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1258 break;
1259 }
1260 } while (pCursor->pb[offSkip++] & 0x80);
1261
1262 pCursor->pb += offSkip;
1263 pCursor->cbUnitLeft -= offSkip;
1264 pCursor->cbLeft -= offSkip;
1265 return pCursor->rc;
1266}
1267
1268
1269/**
1270 * Advances the cursor a given number of bytes.
1271 *
1272 * @returns IPRT status code.
1273 * @param pCursor The cursor.
1274 * @param offSkip The number of bytes to advance.
1275 */
1276static int rtDwarfCursor_SkipBytes(PRTDWARFCURSOR pCursor, uint64_t offSkip)
1277{
1278 if (RT_FAILURE(pCursor->rc))
1279 return pCursor->rc;
1280 if (pCursor->cbUnitLeft < offSkip)
1281 return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1282
1283 size_t const offSkipSizeT = (size_t)offSkip;
1284 pCursor->cbUnitLeft -= offSkipSizeT;
1285 pCursor->cbLeft -= offSkipSizeT;
1286 pCursor->pb += offSkipSizeT;
1287
1288 return VINF_SUCCESS;
1289}
1290
1291
1292/**
1293 * Reads a zero terminated string, advancing the cursor beyond the terminator.
1294 *
1295 * @returns Pointer to the string.
1296 * @param pCursor The cursor.
1297 * @param pszErrValue What to return if the string isn't terminated
1298 * before the end of the unit.
1299 */
1300static const char *rtDwarfCursor_GetSZ(PRTDWARFCURSOR pCursor, const char *pszErrValue)
1301{
1302 const char *pszRet = (const char *)pCursor->pb;
1303 for (;;)
1304 {
1305 if (!pCursor->cbUnitLeft)
1306 {
1307 pCursor->rc = VERR_DWARF_BAD_STRING;
1308 return pszErrValue;
1309 }
1310 pCursor->cbUnitLeft--;
1311 pCursor->cbLeft--;
1312 if (!*pCursor->pb++)
1313 break;
1314 }
1315 return pszRet;
1316}
1317
1318
1319/**
1320 * Reads a 1, 2, 4 or 8 byte unsgined value.
1321 *
1322 * @returns 64-bit unsigned value.
1323 * @param pCursor The cursor.
1324 * @param cbValue The value size.
1325 * @param uErrValue The error value.
1326 */
1327static uint64_t rtDwarfCursor_GetVarSizedU(PRTDWARFCURSOR pCursor, size_t cbValue, uint64_t uErrValue)
1328{
1329 uint64_t u64Ret;
1330 switch (cbValue)
1331 {
1332 case 1: u64Ret = rtDwarfCursor_GetU8( pCursor, UINT8_MAX); break;
1333 case 2: u64Ret = rtDwarfCursor_GetU16(pCursor, UINT16_MAX); break;
1334 case 4: u64Ret = rtDwarfCursor_GetU32(pCursor, UINT32_MAX); break;
1335 case 8: u64Ret = rtDwarfCursor_GetU64(pCursor, UINT64_MAX); break;
1336 default:
1337 pCursor->rc = VERR_DWARF_BAD_INFO;
1338 return uErrValue;
1339 }
1340 if (RT_FAILURE(pCursor->rc))
1341 return uErrValue;
1342 return u64Ret;
1343}
1344
1345
1346/**
1347 * Reads an unsigned DWARF half number.
1348 *
1349 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
1350 * uErrValue is returned.
1351 * @param pCursor The cursor.
1352 * @param uErrValue What to return on error.
1353 */
1354static uint16_t rtDwarfCursor_GetUHalf(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
1355{
1356 return rtDwarfCursor_GetU16(pCursor, uErrValue);
1357}
1358
1359
1360/**
1361 * Reads an unsigned DWARF byte number.
1362 *
1363 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
1364 * uErrValue is returned.
1365 * @param pCursor The cursor.
1366 * @param uErrValue What to return on error.
1367 */
1368static uint8_t rtDwarfCursor_GetUByte(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
1369{
1370 return rtDwarfCursor_GetU8(pCursor, uErrValue);
1371}
1372
1373
1374/**
1375 * Reads a signed DWARF byte number.
1376 *
1377 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
1378 * uErrValue is returned.
1379 * @param pCursor The cursor.
1380 * @param uErrValue What to return on error.
1381 */
1382static int8_t rtDwarfCursor_GetSByte(PRTDWARFCURSOR pCursor, int8_t iErrValue)
1383{
1384 return (int8_t)rtDwarfCursor_GetU8(pCursor, (uint8_t)iErrValue);
1385}
1386
1387
1388/**
1389 * Reads a unsigned DWARF offset value.
1390 *
1391 * @returns The value. On error RTDWARFCURSOR::rc is set and @a
1392 * uErrValue is returned.
1393 * @param pCursor The cursor.
1394 * @param uErrValue What to return on error.
1395 */
1396static uint64_t rtDwarfCursor_GetUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1397{
1398 if (pCursor->f64bitDwarf)
1399 return rtDwarfCursor_GetU64(pCursor, uErrValue);
1400 return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
1401}
1402
1403
1404/**
1405 * Reads a unsigned DWARF native offset value.
1406 *
1407 * @returns The value. On error RTDWARFCURSOR::rc is set and @a
1408 * uErrValue is returned.
1409 * @param pCursor The cursor.
1410 * @param uErrValue What to return on error.
1411 */
1412static uint64_t rtDwarfCursor_GetNativeUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1413{
1414 switch (pCursor->cbNativeAddr)
1415 {
1416 case 1: return rtDwarfCursor_GetU8(pCursor, (uint8_t )uErrValue);
1417 case 2: return rtDwarfCursor_GetU16(pCursor, (uint16_t)uErrValue);
1418 case 4: return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
1419 case 8: return rtDwarfCursor_GetU64(pCursor, uErrValue);
1420 default:
1421 pCursor->rc = VERR_INTERNAL_ERROR_2;
1422 return uErrValue;
1423 }
1424}
1425
1426
1427/**
1428 * Gets the unit length, updating the unit length member and DWARF bitness
1429 * members of the cursor.
1430 *
1431 * @returns The unit length.
1432 * @param pCursor The cursor.
1433 */
1434static uint64_t rtDwarfCursor_GetInitalLength(PRTDWARFCURSOR pCursor)
1435{
1436 /*
1437 * Read the initial length.
1438 */
1439 pCursor->cbUnitLeft = pCursor->cbLeft;
1440 uint64_t cbUnit = rtDwarfCursor_GetU32(pCursor, 0);
1441 if (cbUnit != UINT32_C(0xffffffff))
1442 pCursor->f64bitDwarf = false;
1443 else
1444 {
1445 pCursor->f64bitDwarf = true;
1446 cbUnit = rtDwarfCursor_GetU64(pCursor, 0);
1447 }
1448
1449
1450 /*
1451 * Set the unit length, quitely fixing bad lengths.
1452 */
1453 pCursor->cbUnitLeft = (size_t)cbUnit;
1454 if ( pCursor->cbUnitLeft > pCursor->cbLeft
1455 || pCursor->cbUnitLeft != cbUnit)
1456 pCursor->cbUnitLeft = pCursor->cbLeft;
1457
1458 return cbUnit;
1459}
1460
1461
1462/**
1463 * Calculates the section offset corresponding to the current cursor position.
1464 *
1465 * @returns 32-bit section offset. If out of range, RTDWARFCURSOR::rc will be
1466 * set and UINT32_MAX returned.
1467 * @param pCursor The cursor.
1468 */
1469static uint32_t rtDwarfCursor_CalcSectOffsetU32(PRTDWARFCURSOR pCursor)
1470{
1471 size_t off = pCursor->pb - (uint8_t const *)pCursor->pDwarfMod->aSections[pCursor->enmSect].pv;
1472 uint32_t offRet = (uint32_t)off;
1473 if (offRet != off)
1474 {
1475 pCursor->rc = VERR_OUT_OF_RANGE;
1476 offRet = UINT32_MAX;
1477 }
1478 return offRet;
1479}
1480
1481
1482/**
1483 * Calculates an absolute cursor position from one relative to the current
1484 * cursor position.
1485 *
1486 * @returns The absolute cursor position.
1487 * @param pCursor The cursor.
1488 * @param offRelative The relative position. Must be a positive
1489 * offset.
1490 */
1491static uint8_t const *rtDwarfCursor_CalcPos(PRTDWARFCURSOR pCursor, size_t offRelative)
1492{
1493 if (offRelative > pCursor->cbUnitLeft)
1494 {
1495 Log(("rtDwarfCursor_CalcPos: bad position %#zx, cbUnitLeft=%#zu\n", offRelative, pCursor->cbUnitLeft));
1496 pCursor->rc = VERR_DWARF_BAD_POS;
1497 return NULL;
1498 }
1499 return pCursor->pb + offRelative;
1500}
1501
1502
1503/**
1504 * Advances the cursor to the given position.
1505 *
1506 * @returns IPRT status code.
1507 * @param pCursor The cursor.
1508 * @param pbNewPos The new position - returned by
1509 * rtDwarfCursor_CalcPos().
1510 */
1511static int rtDwarfCursor_AdvanceToPos(PRTDWARFCURSOR pCursor, uint8_t const *pbNewPos)
1512{
1513 if (RT_FAILURE(pCursor->rc))
1514 return pCursor->rc;
1515 AssertPtr(pbNewPos);
1516 if ((uintptr_t)pbNewPos < (uintptr_t)pCursor->pb)
1517 {
1518 Log(("rtDwarfCursor_AdvanceToPos: bad position %p, current %p\n", pbNewPos, pCursor->pb));
1519 return pCursor->rc = VERR_DWARF_BAD_POS;
1520 }
1521
1522 uintptr_t cbAdj = (uintptr_t)pbNewPos - (uintptr_t)pCursor->pb;
1523 if (RT_UNLIKELY(cbAdj > pCursor->cbUnitLeft))
1524 {
1525 AssertFailed();
1526 pCursor->rc = VERR_DWARF_BAD_POS;
1527 cbAdj = pCursor->cbUnitLeft;
1528 }
1529
1530 pCursor->cbUnitLeft -= cbAdj;
1531 pCursor->cbLeft -= cbAdj;
1532 pCursor->pb += cbAdj;
1533 return pCursor->rc;
1534}
1535
1536
1537/**
1538 * Check if the cursor is at the end of the current DWARF unit.
1539 *
1540 * @retval @c true if at the end or a cursor error is pending.
1541 * @retval @c false if not.
1542 * @param pCursor The cursor.
1543 */
1544static bool rtDwarfCursor_IsAtEndOfUnit(PRTDWARFCURSOR pCursor)
1545{
1546 return !pCursor->cbUnitLeft || RT_FAILURE(pCursor->rc);
1547}
1548
1549
1550/**
1551 * Skips to the end of the current unit.
1552 *
1553 * @returns IPRT status code.
1554 * @param pCursor The cursor.
1555 */
1556static int rtDwarfCursor_SkipUnit(PRTDWARFCURSOR pCursor)
1557{
1558 pCursor->pb += pCursor->cbUnitLeft;
1559 pCursor->cbLeft -= pCursor->cbUnitLeft;
1560 pCursor->cbUnitLeft = 0;
1561 return pCursor->rc;
1562}
1563
1564
1565/**
1566 * Check if the cursor is at the end of the section (or whatever the cursor is
1567 * processing).
1568 *
1569 * @retval @c true if at the end or a cursor error is pending.
1570 * @retval @c false if not.
1571 * @param pCursor The cursor.
1572 */
1573static bool rtDwarfCursor_IsAtEnd(PRTDWARFCURSOR pCursor)
1574{
1575 return !pCursor->cbLeft || RT_FAILURE(pCursor->rc);
1576}
1577
1578
1579/**
1580 * Initialize a section reader cursor.
1581 *
1582 * @returns IPRT status code.
1583 * @param pCursor The cursor.
1584 * @param pThis The dwarf module.
1585 * @param enmSect The name of the section to read.
1586 */
1587static int rtDwarfCursor_Init(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
1588{
1589 int rc = rtDbgModDwarfLoadSection(pThis, enmSect);
1590 if (RT_FAILURE(rc))
1591 return rc;
1592
1593 pCursor->enmSect = enmSect;
1594 pCursor->pbStart = (uint8_t const *)pThis->aSections[enmSect].pv;
1595 pCursor->pb = pCursor->pbStart;
1596 pCursor->cbLeft = pThis->aSections[enmSect].cb;
1597 pCursor->cbUnitLeft = pCursor->cbLeft;
1598 pCursor->pDwarfMod = pThis;
1599 pCursor->f64bitDwarf = false;
1600 /** @todo ask the image about the endian used as well as the address
1601 * width. */
1602 pCursor->fNativEndian = true;
1603 pCursor->cbNativeAddr = 4;
1604 pCursor->rc = VINF_SUCCESS;
1605
1606 return VINF_SUCCESS;
1607}
1608
1609
1610/**
1611 * Initialize a section reader cursor with an offset.
1612 *
1613 * @returns IPRT status code.
1614 * @param pCursor The cursor.
1615 * @param pThis The dwarf module.
1616 * @param enmSect The name of the section to read.
1617 * @param offSect The offset into the section.
1618 */
1619static int rtDwarfCursor_InitWithOffset(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis,
1620 krtDbgModDwarfSect enmSect, uint32_t offSect)
1621{
1622 if (offSect > pThis->aSections[enmSect].cb)
1623 {
1624 Log(("rtDwarfCursor_InitWithOffset: offSect=%#x cb=%#x enmSect=%d\n", offSect, pThis->aSections[enmSect].cb, enmSect));
1625 return VERR_DWARF_BAD_POS;
1626 }
1627
1628 int rc = rtDwarfCursor_Init(pCursor, pThis, enmSect);
1629 if (RT_SUCCESS(rc))
1630 {
1631 pCursor->pbStart += offSect;
1632 pCursor->pb += offSect;
1633 pCursor->cbLeft -= offSect;
1634 pCursor->cbUnitLeft -= offSect;
1635 }
1636
1637 return rc;
1638}
1639
1640
1641/**
1642 * Deletes a section reader initialized by rtDwarfCursor_Init.
1643 *
1644 * @returns @a rcOther or RTDWARCURSOR::rc.
1645 * @param pCursor The section reader.
1646 * @param rcOther Other error code to be returned if it indicates
1647 * error or if the cursor status is OK.
1648 */
1649static int rtDwarfCursor_Delete(PRTDWARFCURSOR pCursor, int rcOther)
1650{
1651 /* ... and a drop of poison. */
1652 pCursor->pb = NULL;
1653 pCursor->cbLeft = ~(size_t)0;
1654 pCursor->cbUnitLeft = ~(size_t)0;
1655 pCursor->pDwarfMod = NULL;
1656 if (RT_FAILURE(pCursor->rc) && RT_SUCCESS(rcOther))
1657 rcOther = pCursor->rc;
1658 pCursor->rc = VERR_INTERNAL_ERROR_4;
1659 return rcOther;
1660}
1661
1662
1663/*
1664 *
1665 * DWARF Line Numbers.
1666 * DWARF Line Numbers.
1667 * DWARF Line Numbers.
1668 *
1669 */
1670
1671
1672/**
1673 * Defines a file name.
1674 *
1675 * @returns IPRT status code.
1676 * @param pLnState The line number program state.
1677 * @param pszFilename The name of the file.
1678 * @param idxInc The include path index.
1679 */
1680static int rtDwarfLine_DefineFileName(PRTDWARFLINESTATE pLnState, const char *pszFilename, uint64_t idxInc)
1681{
1682 /*
1683 * Resize the array if necessary.
1684 */
1685 uint32_t iFileName = pLnState->cFileNames;
1686 if ((iFileName % 2) == 0)
1687 {
1688 void *pv = RTMemRealloc(pLnState->papszFileNames, sizeof(pLnState->papszFileNames[0]) * (iFileName + 2));
1689 if (!pv)
1690 return VERR_NO_MEMORY;
1691 pLnState->papszFileNames = (char **)pv;
1692 }
1693
1694 /*
1695 * Add the file name.
1696 */
1697 if ( pszFilename[0] == '/'
1698 || pszFilename[0] == '\\'
1699 || (RT_C_IS_ALPHA(pszFilename[0]) && pszFilename[1] == ':') )
1700 pLnState->papszFileNames[iFileName] = RTStrDup(pszFilename);
1701 else if (idxInc < pLnState->cIncPaths)
1702 pLnState->papszFileNames[iFileName] = RTPathJoinA(pLnState->papszIncPaths[idxInc], pszFilename);
1703 else
1704 return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
1705 if (!pLnState->papszFileNames[iFileName])
1706 return VERR_NO_STR_MEMORY;
1707 pLnState->cFileNames = iFileName + 1;
1708
1709 /*
1710 * Sanitize the name.
1711 */
1712 int rc = rtDbgModDwarfStringToUtf8(pLnState->pDwarfMod, &pLnState->papszFileNames[iFileName]);
1713 Log((" File #%02u = '%s'\n", iFileName, pLnState->papszFileNames[iFileName]));
1714 return rc;
1715}
1716
1717
1718/**
1719 * Adds a line to the table and resets parts of the state (DW_LNS_copy).
1720 *
1721 * @returns IPRT status code
1722 * @param pLnState The line number program state.
1723 * @param offOpCode The opcode offset (for logging
1724 * purposes).
1725 */
1726static int rtDwarfLine_AddLine(PRTDWARFLINESTATE pLnState, uint32_t offOpCode)
1727{
1728 const char *pszFile = pLnState->Regs.iFile < pLnState->cFileNames
1729 ? pLnState->papszFileNames[pLnState->Regs.iFile]
1730 : "<bad file name index>";
1731 RTDBGSEGIDX iSeg;
1732 RTUINTPTR offSeg;
1733 int rc = rtDbgModDwarfLinkAddressToSegOffset(pLnState->pDwarfMod, pLnState->Regs.uAddress, &iSeg, &offSeg);
1734 if (RT_SUCCESS(rc))
1735 {
1736 Log2(("rtDwarfLine_AddLine: %x:%08llx (%#llx) %s(%d) [offOpCode=%08x]\n", iSeg, offSeg, pLnState->Regs.uAddress, pszFile, pLnState->Regs.uLine, offOpCode));
1737 rc = RTDbgModLineAdd(pLnState->pDwarfMod->hCnt, pszFile, pLnState->Regs.uLine, iSeg, offSeg, NULL);
1738
1739 /* Ignore address conflicts for now. */
1740 if (rc == VERR_DBG_ADDRESS_CONFLICT)
1741 rc = VINF_SUCCESS;
1742 }
1743
1744 pLnState->Regs.fBasicBlock = false;
1745 pLnState->Regs.fPrologueEnd = false;
1746 pLnState->Regs.fEpilogueBegin = false;
1747 pLnState->Regs.uDiscriminator = 0;
1748 return rc;
1749}
1750
1751
1752/**
1753 * Reset the program to the start-of-sequence state.
1754 *
1755 * @param pLnState The line number program state.
1756 */
1757static void rtDwarfLine_ResetState(PRTDWARFLINESTATE pLnState)
1758{
1759 pLnState->Regs.uAddress = 0;
1760 pLnState->Regs.idxOp = 0;
1761 pLnState->Regs.iFile = 1;
1762 pLnState->Regs.uLine = 1;
1763 pLnState->Regs.uColumn = 0;
1764 pLnState->Regs.fIsStatement = RT_BOOL(pLnState->Hdr.u8DefIsStmt);
1765 pLnState->Regs.fBasicBlock = false;
1766 pLnState->Regs.fEndSequence = false;
1767 pLnState->Regs.fPrologueEnd = false;
1768 pLnState->Regs.fEpilogueBegin = false;
1769 pLnState->Regs.uIsa = 0;
1770 pLnState->Regs.uDiscriminator = 0;
1771}
1772
1773
1774/**
1775 * Runs the line number program.
1776 *
1777 * @returns IPRT status code.
1778 * @param pLnState The line number program state.
1779 * @param pCursor The cursor.
1780 */
1781static int rtDwarfLine_RunProgram(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
1782{
1783 LogFlow(("rtDwarfLine_RunProgram: cbUnitLeft=%zu\n", pCursor->cbUnitLeft));
1784
1785 int rc = VINF_SUCCESS;
1786 rtDwarfLine_ResetState(pLnState);
1787
1788 while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
1789 {
1790#ifdef LOG_ENABLED
1791 uint32_t const offOpCode = rtDwarfCursor_CalcSectOffsetU32(pCursor);
1792#else
1793 uint32_t const offOpCode = 0;
1794#endif
1795 uint8_t bOpCode = rtDwarfCursor_GetUByte(pCursor, DW_LNS_extended);
1796 if (bOpCode >= pLnState->Hdr.u8OpcodeBase)
1797 {
1798 /*
1799 * Special opcode.
1800 */
1801 uint8_t const bLogOpCode = bOpCode; NOREF(bLogOpCode);
1802 bOpCode -= pLnState->Hdr.u8OpcodeBase;
1803
1804 int32_t const cLineDelta = bOpCode % pLnState->Hdr.u8LineRange + (int32_t)pLnState->Hdr.s8LineBase;
1805 bOpCode /= pLnState->Hdr.u8LineRange;
1806
1807 uint64_t uTmp = bOpCode + pLnState->Regs.idxOp + bOpCode;
1808 uint64_t const cAddressDelta = uTmp / pLnState->Hdr.cMaxOpsPerInstr * pLnState->Hdr.cbMinInstr;
1809 uint64_t const cOpIndexDelta = uTmp % pLnState->Hdr.cMaxOpsPerInstr;
1810
1811 pLnState->Regs.uLine += cLineDelta;
1812 pLnState->Regs.uAddress += cAddressDelta;
1813 pLnState->Regs.idxOp += cOpIndexDelta;
1814 Log2(("%08x: DW Special Opcode %#04x: uLine + %d => %u; uAddress + %#llx => %#llx; idxOp + %#llx => %#llx\n",
1815 offOpCode, bLogOpCode, cLineDelta, pLnState->Regs.uLine, cAddressDelta, pLnState->Regs.uAddress,
1816 cOpIndexDelta, pLnState->Regs.idxOp));
1817
1818 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
1819 }
1820 else
1821 {
1822 switch (bOpCode)
1823 {
1824 /*
1825 * Standard opcode.
1826 */
1827 case DW_LNS_copy:
1828 Log2(("%08x: DW_LNS_copy\n", offOpCode));
1829 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
1830 break;
1831
1832 case DW_LNS_advance_pc:
1833 {
1834 uint64_t u64Adv = rtDwarfCursor_GetULeb128(pCursor, 0);
1835 pLnState->Regs.uAddress += (pLnState->Regs.idxOp + u64Adv) / pLnState->Hdr.cMaxOpsPerInstr
1836 * pLnState->Hdr.cbMinInstr;
1837 pLnState->Regs.idxOp += (pLnState->Regs.idxOp + u64Adv) % pLnState->Hdr.cMaxOpsPerInstr;
1838 Log2(("%08x: DW_LNS_advance_pc: u64Adv=%#llx (%lld) )\n", offOpCode, u64Adv, u64Adv));
1839 break;
1840 }
1841
1842 case DW_LNS_advance_line:
1843 {
1844 int32_t cLineDelta = rtDwarfCursor_GetSLeb128AsS32(pCursor, 0);
1845 pLnState->Regs.uLine += cLineDelta;
1846 Log2(("%08x: DW_LNS_advance_line: uLine + %d => %u\n", offOpCode, cLineDelta, pLnState->Regs.uLine));
1847 break;
1848 }
1849
1850 case DW_LNS_set_file:
1851 pLnState->Regs.iFile = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1852 Log2(("%08x: DW_LNS_set_file: iFile=%u\n", offOpCode, pLnState->Regs.iFile));
1853 break;
1854
1855 case DW_LNS_set_column:
1856 pLnState->Regs.uColumn = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1857 Log2(("%08x: DW_LNS_set_column\n", offOpCode));
1858 break;
1859
1860 case DW_LNS_negate_stmt:
1861 pLnState->Regs.fIsStatement = !pLnState->Regs.fIsStatement;
1862 Log2(("%08x: DW_LNS_negate_stmt\n", offOpCode));
1863 break;
1864
1865 case DW_LNS_set_basic_block:
1866 pLnState->Regs.fBasicBlock = true;
1867 Log2(("%08x: DW_LNS_set_basic_block\n", offOpCode));
1868 break;
1869
1870 case DW_LNS_const_add_pc:
1871 pLnState->Regs.uAddress += (pLnState->Regs.idxOp + 255) / pLnState->Hdr.cMaxOpsPerInstr
1872 * pLnState->Hdr.cbMinInstr;
1873 pLnState->Regs.idxOp += (pLnState->Regs.idxOp + 255) % pLnState->Hdr.cMaxOpsPerInstr;
1874 Log2(("%08x: DW_LNS_const_add_pc\n", offOpCode));
1875 break;
1876
1877 case DW_LNS_fixed_advance_pc:
1878 pLnState->Regs.uAddress += rtDwarfCursor_GetUHalf(pCursor, 0);
1879 pLnState->Regs.idxOp = 0;
1880 Log2(("%08x: DW_LNS_fixed_advance_pc\n", offOpCode));
1881 break;
1882
1883 case DW_LNS_set_prologue_end:
1884 pLnState->Regs.fPrologueEnd = true;
1885 Log2(("%08x: DW_LNS_set_prologue_end\n", offOpCode));
1886 break;
1887
1888 case DW_LNS_set_epilogue_begin:
1889 pLnState->Regs.fEpilogueBegin = true;
1890 Log2(("%08x: DW_LNS_set_epilogue_begin\n", offOpCode));
1891 break;
1892
1893 case DW_LNS_set_isa:
1894 pLnState->Regs.uIsa = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1895 Log2(("%08x: DW_LNS_set_isa %#x\n", offOpCode, pLnState->Regs.uIsa));
1896 break;
1897
1898 default:
1899 {
1900 unsigned cOpsToSkip = pLnState->Hdr.pacStdOperands[bOpCode - 1];
1901 Log(("rtDwarfLine_RunProgram: Unknown standard opcode %#x, %#x operands, at %08x.\n", bOpCode, cOpsToSkip, offOpCode));
1902 while (cOpsToSkip-- > 0)
1903 rc = rtDwarfCursor_SkipLeb128(pCursor);
1904 break;
1905 }
1906
1907 /*
1908 * Extended opcode.
1909 */
1910 case DW_LNS_extended:
1911 {
1912 /* The instruction has a length prefix. */
1913 uint64_t cbInstr = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
1914 if (RT_FAILURE(pCursor->rc))
1915 return pCursor->rc;
1916 if (cbInstr > pCursor->cbUnitLeft)
1917 return VERR_DWARF_BAD_LNE;
1918 uint8_t const * const pbEndOfInstr = rtDwarfCursor_CalcPos(pCursor, cbInstr);
1919
1920 /* Get the opcode and deal with it if we know it. */
1921 bOpCode = rtDwarfCursor_GetUByte(pCursor, 0);
1922 switch (bOpCode)
1923 {
1924 case DW_LNE_end_sequence:
1925#if 0 /* No need for this, I think. */
1926 pLnState->Regs.fEndSequence = true;
1927 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
1928#endif
1929 rtDwarfLine_ResetState(pLnState);
1930 Log2(("%08x: DW_LNE_end_sequence\n", offOpCode));
1931 break;
1932
1933 case DW_LNE_set_address:
1934 pLnState->Regs.uAddress = rtDwarfCursor_GetVarSizedU(pCursor, cbInstr - 1, UINT64_MAX);
1935 pLnState->Regs.idxOp = 0;
1936 Log2(("%08x: DW_LNE_set_address: %#llx\n", offOpCode, pLnState->Regs.uAddress));
1937 break;
1938
1939 case DW_LNE_define_file:
1940 {
1941 const char *pszFilename = rtDwarfCursor_GetSZ(pCursor, NULL);
1942 uint32_t idxInc = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
1943 rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
1944 rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
1945 Log2(("%08x: DW_LNE_define_file: {%d}/%s\n", offOpCode, idxInc, pszFilename));
1946
1947 rc = rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
1948 if (RT_SUCCESS(rc))
1949 rc = rtDwarfLine_DefineFileName(pLnState, pszFilename, idxInc);
1950 }
1951
1952 /*
1953 * Note! Was defined in DWARF 4. But... Watcom used it
1954 * for setting the segment in DWARF 2, creating
1955 * an incompatibility with the newer standard.
1956 */
1957 case DW_LNE_set_descriminator:
1958 if (pLnState->Hdr.uVer != 2)
1959 {
1960 Assert(pLnState->Hdr.uVer >= 4);
1961 pLnState->Regs.uDiscriminator = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
1962 Log2(("%08x: DW_LNE_set_descriminator: %u\n", offOpCode, pLnState->Regs.uDiscriminator));
1963 }
1964 else
1965 {
1966 uint64_t uSeg = rtDwarfCursor_GetVarSizedU(pCursor, cbInstr - 1, UINT64_MAX);
1967 Log2(("%08x: DW_LNE_set_segment: %ll#x - Watcom Extension\n", offOpCode, uSeg));
1968 /** @todo make use of this? */
1969 }
1970 break;
1971
1972 default:
1973 Log(("rtDwarfLine_RunProgram: Unknown extended opcode %#x, length %#x at %08x\n", bOpCode, cbInstr, offOpCode));
1974 break;
1975 }
1976
1977 /* Advance the cursor to the end of the instruction . */
1978 rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
1979 break;
1980 }
1981 }
1982 }
1983
1984 /*
1985 * Check the status before looping.
1986 */
1987 if (RT_FAILURE(rc))
1988 return rc;
1989 if (RT_FAILURE(pCursor->rc))
1990 return pCursor->rc;
1991 }
1992 return rc;
1993}
1994
1995
1996/**
1997 * Reads the include directories for a line number unit.
1998 *
1999 * @returns IPRT status code
2000 * @param pLnState The line number program state.
2001 * @param pCursor The cursor.
2002 */
2003static int rtDwarfLine_ReadFileNames(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
2004{
2005 int rc = rtDwarfLine_DefineFileName(pLnState, "/<bad-zero-file-name-entry>", 0);
2006 if (RT_FAILURE(rc))
2007 return rc;
2008
2009 for (;;)
2010 {
2011 const char *psz = rtDwarfCursor_GetSZ(pCursor, NULL);
2012 if (!*psz)
2013 break;
2014
2015 uint64_t idxInc = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
2016 rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
2017 rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
2018
2019 rc = rtDwarfLine_DefineFileName(pLnState, psz, idxInc);
2020 if (RT_FAILURE(rc))
2021 return rc;
2022 }
2023 return pCursor->rc;
2024}
2025
2026
2027/**
2028 * Reads the include directories for a line number unit.
2029 *
2030 * @returns IPRT status code
2031 * @param pLnState The line number program state.
2032 * @param pCursor The cursor.
2033 */
2034static int rtDwarfLine_ReadIncludePaths(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
2035{
2036 const char *psz = ""; /* The zeroth is the unit dir. */
2037 for (;;)
2038 {
2039 if ((pLnState->cIncPaths % 2) == 0)
2040 {
2041 void *pv = RTMemRealloc(pLnState->papszIncPaths, sizeof(pLnState->papszIncPaths[0]) * (pLnState->cIncPaths + 2));
2042 if (!pv)
2043 return VERR_NO_MEMORY;
2044 pLnState->papszIncPaths = (const char **)pv;
2045 }
2046 Log((" Path #%02u = '%s'\n", pLnState->cIncPaths, psz));
2047 pLnState->papszIncPaths[pLnState->cIncPaths] = psz;
2048 pLnState->cIncPaths++;
2049
2050 psz = rtDwarfCursor_GetSZ(pCursor, NULL);
2051 if (!*psz)
2052 break;
2053 }
2054
2055 return pCursor->rc;
2056}
2057
2058
2059/**
2060 * Explodes the line number table for a compilation unit.
2061 *
2062 * @returns IPRT status code
2063 * @param pThis The DWARF instance.
2064 * @param pCursor The cursor to read the line number information
2065 * via.
2066 */
2067static int rtDwarfLine_ExplodeUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor)
2068{
2069 RTDWARFLINESTATE LnState;
2070 RT_ZERO(LnState);
2071 LnState.pDwarfMod = pThis;
2072
2073 /*
2074 * Parse the header.
2075 */
2076 rtDwarfCursor_GetInitalLength(pCursor);
2077 LnState.Hdr.uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
2078 if ( LnState.Hdr.uVer < 2
2079 || LnState.Hdr.uVer > 4)
2080 return rtDwarfCursor_SkipUnit(pCursor);
2081
2082 LnState.Hdr.offFirstOpcode = rtDwarfCursor_GetUOff(pCursor, 0);
2083 uint8_t const * const pbFirstOpcode = rtDwarfCursor_CalcPos(pCursor, LnState.Hdr.offFirstOpcode);
2084
2085 LnState.Hdr.cbMinInstr = rtDwarfCursor_GetUByte(pCursor, 0);
2086 if (LnState.Hdr.uVer >= 4)
2087 LnState.Hdr.cMaxOpsPerInstr = rtDwarfCursor_GetUByte(pCursor, 0);
2088 else
2089 LnState.Hdr.cMaxOpsPerInstr = 1;
2090 LnState.Hdr.u8DefIsStmt = rtDwarfCursor_GetUByte(pCursor, 0);
2091 LnState.Hdr.s8LineBase = rtDwarfCursor_GetSByte(pCursor, 0);
2092 LnState.Hdr.u8LineRange = rtDwarfCursor_GetUByte(pCursor, 0);
2093 LnState.Hdr.u8OpcodeBase = rtDwarfCursor_GetUByte(pCursor, 0);
2094
2095 if ( !LnState.Hdr.u8OpcodeBase
2096 || !LnState.Hdr.cMaxOpsPerInstr
2097 || !LnState.Hdr.u8LineRange
2098 || LnState.Hdr.u8DefIsStmt > 1)
2099 return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
2100 Log2(("DWARF Line number header:\n"
2101 " uVer %d\n"
2102 " offFirstOpcode %#llx\n"
2103 " cbMinInstr %u\n"
2104 " cMaxOpsPerInstr %u\n"
2105 " u8DefIsStmt %u\n"
2106 " s8LineBase %d\n"
2107 " u8LineRange %u\n"
2108 " u8OpcodeBase %u\n",
2109 LnState.Hdr.uVer, LnState.Hdr.offFirstOpcode, LnState.Hdr.cbMinInstr, LnState.Hdr.cMaxOpsPerInstr,
2110 LnState.Hdr.u8DefIsStmt, LnState.Hdr.s8LineBase, LnState.Hdr.u8LineRange, LnState.Hdr.u8OpcodeBase));
2111
2112 LnState.Hdr.pacStdOperands = pCursor->pb;
2113 for (uint8_t iStdOpcode = 1; iStdOpcode < LnState.Hdr.u8OpcodeBase; iStdOpcode++)
2114 rtDwarfCursor_GetUByte(pCursor, 0);
2115
2116 int rc = pCursor->rc;
2117 if (RT_SUCCESS(rc))
2118 rc = rtDwarfLine_ReadIncludePaths(&LnState, pCursor);
2119 if (RT_SUCCESS(rc))
2120 rc = rtDwarfLine_ReadFileNames(&LnState, pCursor);
2121
2122 /*
2123 * Run the program....
2124 */
2125 if (RT_SUCCESS(rc))
2126 rc = rtDwarfCursor_AdvanceToPos(pCursor, pbFirstOpcode);
2127 if (RT_SUCCESS(rc))
2128 rc = rtDwarfLine_RunProgram(&LnState, pCursor);
2129
2130 /*
2131 * Clean up.
2132 */
2133 size_t i = LnState.cFileNames;
2134 while (i-- > 0)
2135 RTStrFree(LnState.papszFileNames[i]);
2136 RTMemFree(LnState.papszFileNames);
2137 RTMemFree(LnState.papszIncPaths);
2138
2139 Assert(rtDwarfCursor_IsAtEndOfUnit(pCursor) || RT_FAILURE(rc));
2140 return rc;
2141}
2142
2143
2144/**
2145 * Explodes the line number table.
2146 *
2147 * The line numbers are insered into the debug info container.
2148 *
2149 * @returns IPRT status code
2150 * @param pThis The DWARF instance.
2151 */
2152static int rtDwarfLine_ExplodeAll(PRTDBGMODDWARF pThis)
2153{
2154 if (!pThis->aSections[krtDbgModDwarfSect_line].fPresent)
2155 return VINF_SUCCESS;
2156
2157 RTDWARFCURSOR Cursor;
2158 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_line);
2159 if (RT_FAILURE(rc))
2160 return rc;
2161
2162 while ( !rtDwarfCursor_IsAtEnd(&Cursor)
2163 && RT_SUCCESS(rc))
2164 rc = rtDwarfLine_ExplodeUnit(pThis, &Cursor);
2165
2166 return rtDwarfCursor_Delete(&Cursor, rc);
2167}
2168
2169
2170/*
2171 *
2172 * DWARF Abbreviations.
2173 * DWARF Abbreviations.
2174 * DWARF Abbreviations.
2175 *
2176 */
2177
2178/**
2179 * Deals with a cache miss in rtDwarfAbbrev_Lookup.
2180 *
2181 * @returns Pointer to abbreviation cache entry (read only). May be rendered
2182 * invalid by subsequent calls to this function.
2183 * @param pThis The DWARF instance.
2184 * @param uCode The abbreviation code to lookup.
2185 */
2186static PCRTDWARFABBREV rtDwarfAbbrev_LookupMiss(PRTDBGMODDWARF pThis, uint32_t uCode)
2187{
2188 /*
2189 * There is no entry with code zero.
2190 */
2191 if (!uCode)
2192 return NULL;
2193
2194 /*
2195 * Resize the cache array if the code is considered cachable.
2196 */
2197 bool fFillCache = true;
2198 if (pThis->cCachedAbbrevsAlloced < uCode)
2199 {
2200 if (uCode > _64K)
2201 fFillCache = false;
2202 else
2203 {
2204 uint32_t cNew = RT_ALIGN(uCode, 64);
2205 void *pv = RTMemRealloc(pThis->paCachedAbbrevs, sizeof(pThis->paCachedAbbrevs[0]) * cNew);
2206 if (!pv)
2207 fFillCache = false;
2208 else
2209 {
2210 pThis->cCachedAbbrevsAlloced = cNew;
2211 pThis->paCachedAbbrevs = (PRTDWARFABBREV)pv;
2212 }
2213 }
2214 }
2215
2216 /*
2217 * Walk the abbreviations till we find the desired code.
2218 */
2219 RTDWARFCURSOR Cursor;
2220 int rc = rtDwarfCursor_InitWithOffset(&Cursor, pThis, krtDbgModDwarfSect_abbrev, pThis->offCachedAbbrev);
2221 if (RT_FAILURE(rc))
2222 return NULL;
2223
2224 PRTDWARFABBREV pRet = NULL;
2225 if (fFillCache)
2226 {
2227 /*
2228 * Search for the entry and fill the cache while doing so.
2229 */
2230 for (;;)
2231 {
2232 /* Read the 'header'. */
2233 uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2234 uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2235 uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
2236 if (RT_FAILURE(Cursor.rc))
2237 break;
2238 if ( uCurTag > 0xffff
2239 || uChildren > 1)
2240 {
2241 Cursor.rc = VERR_DWARF_BAD_ABBREV;
2242 break;
2243 }
2244
2245 /* Cache it? */
2246 if (uCurCode <= pThis->cCachedAbbrevsAlloced)
2247 {
2248 PRTDWARFABBREV pEntry = &pThis->paCachedAbbrevs[uCurCode - 1];
2249 while (pThis->cCachedAbbrevs < uCurCode)
2250 {
2251 pThis->paCachedAbbrevs[pThis->cCachedAbbrevs].fFilled = false;
2252 pThis->cCachedAbbrevs++;
2253 }
2254
2255 pEntry->fFilled = true;
2256 pEntry->fChildren = RT_BOOL(uChildren);
2257 pEntry->uTag = uCurTag;
2258 pEntry->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
2259
2260 if (uCurCode == uCode)
2261 {
2262 pRet = pEntry;
2263 if (uCurCode == pThis->cCachedAbbrevsAlloced)
2264 break;
2265 }
2266 }
2267
2268 /* Skip the specification. */
2269 uint32_t uAttr, uForm;
2270 do
2271 {
2272 uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2273 uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2274 } while (uAttr != 0);
2275 if (RT_FAILURE(Cursor.rc))
2276 break;
2277
2278 /* Done? (Maximize cache filling.) */
2279 if ( pRet != NULL
2280 && uCurCode >= pThis->cCachedAbbrevsAlloced)
2281 break;
2282 }
2283 }
2284 else
2285 {
2286 /*
2287 * Search for the entry with the desired code, no cache filling.
2288 */
2289 for (;;)
2290 {
2291 /* Read the 'header'. */
2292 uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2293 uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2294 uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
2295 if (RT_FAILURE(Cursor.rc))
2296 break;
2297 if ( uCurTag > 0xffff
2298 || uChildren > 1)
2299 {
2300 Cursor.rc = VERR_DWARF_BAD_ABBREV;
2301 break;
2302 }
2303
2304 /* Do we have a match? */
2305 if (uCurCode == uCode)
2306 {
2307 pRet = &pThis->LookupAbbrev;
2308 pRet->fFilled = true;
2309 pRet->fChildren = RT_BOOL(uChildren);
2310 pRet->uTag = uCurTag;
2311 pRet->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
2312 break;
2313 }
2314
2315 /* Skip the specification. */
2316 uint32_t uAttr, uForm;
2317 do
2318 {
2319 uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2320 uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2321 } while (uAttr != 0);
2322 if (RT_FAILURE(Cursor.rc))
2323 break;
2324 }
2325 }
2326
2327 rtDwarfCursor_Delete(&Cursor, VINF_SUCCESS);
2328 return pRet;
2329}
2330
2331
2332/**
2333 * Looks up an abbreviation.
2334 *
2335 * @returns Pointer to abbreviation cache entry (read only). May be rendered
2336 * invalid by subsequent calls to this function.
2337 * @param pThis The DWARF instance.
2338 * @param uCode The abbreviation code to lookup.
2339 */
2340static PCRTDWARFABBREV rtDwarfAbbrev_Lookup(PRTDBGMODDWARF pThis, uint32_t uCode)
2341{
2342 if ( uCode - 1 >= pThis->cCachedAbbrevs
2343 || !pThis->paCachedAbbrevs[uCode - 1].fFilled)
2344 return rtDwarfAbbrev_LookupMiss(pThis, uCode);
2345 return &pThis->paCachedAbbrevs[uCode - 1];
2346}
2347
2348
2349/**
2350 * Sets the abbreviation offset of the current unit.
2351 *
2352 * This will flush the cached abbreviation entries if the offset differs from
2353 * the previous unit.
2354 *
2355 * @param pThis The DWARF instance.
2356 * @param offAbbrev The offset into the abbreviation section.
2357 */
2358static void rtDwarfAbbrev_SetUnitOffset(PRTDBGMODDWARF pThis, uint32_t offAbbrev)
2359{
2360 if (pThis->offCachedAbbrev != offAbbrev)
2361 {
2362 pThis->offCachedAbbrev = offAbbrev;
2363 pThis->cCachedAbbrevs = 0;
2364 }
2365}
2366
2367
2368
2369/*
2370 *
2371 * DIE Attribute Parsers.
2372 * DIE Attribute Parsers.
2373 * DIE Attribute Parsers.
2374 *
2375 */
2376
2377/**
2378 * Gets the compilation unit a DIE belongs to.
2379 *
2380 * @returns The compilation unit DIE.
2381 * @param pDie Some DIE in the unit.
2382 */
2383static PRTDWARFDIECOMPILEUNIT rtDwarfDie_GetCompileUnit(PRTDWARFDIE pDie)
2384{
2385 while (pDie->pParent)
2386 pDie = pDie->pParent;
2387 AssertReturn( pDie->uTag == DW_TAG_compile_unit
2388 || pDie->uTag == DW_TAG_partial_unit,
2389 NULL);
2390 return (PRTDWARFDIECOMPILEUNIT)pDie;
2391}
2392
2393
2394/**
2395 * Resolves a string section (debug_str) reference.
2396 *
2397 * @returns Pointer to the string (inside the string section).
2398 * @param pThis The DWARF instance.
2399 * @param pCursor The cursor.
2400 * @param pszErrValue What to return on failure (@a
2401 * pCursor->rc is set).
2402 */
2403static const char *rtDwarfDecode_GetStrp(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor, const char *pszErrValue)
2404{
2405 uint64_t offDebugStr = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
2406 if (RT_FAILURE(pCursor->rc))
2407 return pszErrValue;
2408
2409 if (offDebugStr >= pThis->aSections[krtDbgModDwarfSect_str].cb)
2410 {
2411 /* Ugly: Exploit the cursor status field for reporting errors. */
2412 pCursor->rc = VERR_DWARF_BAD_INFO;
2413 return pszErrValue;
2414 }
2415
2416 if (!pThis->aSections[krtDbgModDwarfSect_str].pv)
2417 {
2418 int rc = rtDbgModDwarfLoadSection(pThis, krtDbgModDwarfSect_str);
2419 if (RT_FAILURE(rc))
2420 {
2421 /* Ugly: Exploit the cursor status field for reporting errors. */
2422 pCursor->rc = rc;
2423 return pszErrValue;
2424 }
2425 }
2426
2427 return (const char *)pThis->aSections[krtDbgModDwarfSect_str].pv + (size_t)offDebugStr;
2428}
2429
2430
2431/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2432static DECLCALLBACK(int) rtDwarfDecode_Address(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2433 uint32_t uForm, PRTDWARFCURSOR pCursor)
2434{
2435 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDR), VERR_INTERNAL_ERROR_3);
2436
2437 uint64_t uAddr;
2438 switch (uForm)
2439 {
2440 case DW_FORM_addr: uAddr = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
2441 case DW_FORM_data1: uAddr = rtDwarfCursor_GetU8(pCursor, 0); break;
2442 case DW_FORM_data2: uAddr = rtDwarfCursor_GetU16(pCursor, 0); break;
2443 case DW_FORM_data4: uAddr = rtDwarfCursor_GetU32(pCursor, 0); break;
2444 case DW_FORM_data8: uAddr = rtDwarfCursor_GetU64(pCursor, 0); break;
2445 case DW_FORM_udata: uAddr = rtDwarfCursor_GetULeb128(pCursor, 0); break;
2446 default:
2447 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2448 }
2449 if (RT_FAILURE(pCursor->rc))
2450 return pCursor->rc;
2451
2452 PRTDWARFADDR pAddr = (PRTDWARFADDR)pbMember;
2453 pAddr->uAddress = uAddr;
2454
2455 return VINF_SUCCESS;
2456}
2457
2458
2459/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2460static DECLCALLBACK(int) rtDwarfDecode_Bool(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2461 uint32_t uForm, PRTDWARFCURSOR pCursor)
2462{
2463 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(bool), VERR_INTERNAL_ERROR_3);
2464
2465 bool *pfMember = (bool *)pbMember;
2466 switch (uForm)
2467 {
2468 case DW_FORM_flag:
2469 {
2470 uint8_t b = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
2471 if (b > 1)
2472 return RT_FAILURE(pCursor->rc) ? pCursor->rc : pCursor->rc = VERR_DWARF_BAD_INFO;
2473 *pfMember = RT_BOOL(b);
2474 break;
2475 }
2476
2477 case DW_FORM_flag_present:
2478 *pfMember = true;
2479 break;
2480
2481 default:
2482 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2483 }
2484
2485 return VINF_SUCCESS;
2486}
2487
2488
2489/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2490static DECLCALLBACK(int) rtDwarfDecode_LowHighPc(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2491 uint32_t uForm, PRTDWARFCURSOR pCursor)
2492{
2493 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDRRANGE), VERR_INTERNAL_ERROR_3);
2494 AssertReturn(pDesc->uAttr == DW_AT_low_pc || pDesc->uAttr == DW_AT_high_pc, VERR_INTERNAL_ERROR_3);
2495
2496 uint64_t uAddr;
2497 switch (uForm)
2498 {
2499 case DW_FORM_addr: uAddr = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
2500 case DW_FORM_data1: uAddr = rtDwarfCursor_GetU8(pCursor, 0); break;
2501 case DW_FORM_data2: uAddr = rtDwarfCursor_GetU16(pCursor, 0); break;
2502 case DW_FORM_data4: uAddr = rtDwarfCursor_GetU32(pCursor, 0); break;
2503 case DW_FORM_data8: uAddr = rtDwarfCursor_GetU64(pCursor, 0); break;
2504 case DW_FORM_udata: uAddr = rtDwarfCursor_GetULeb128(pCursor, 0); break;
2505 default:
2506 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2507 }
2508 if (RT_FAILURE(pCursor->rc))
2509 return pCursor->rc;
2510
2511 PRTDWARFADDRRANGE pRange = (PRTDWARFADDRRANGE)pbMember;
2512 if (pDesc->uAttr == DW_AT_low_pc)
2513 {
2514 if (pRange->fHaveLowAddress)
2515 {
2516 Log(("rtDwarfDecode_LowHighPc: Duplicate DW_AT_low_pc\n"));
2517 return pCursor->rc = VERR_DWARF_BAD_INFO;
2518 }
2519 pRange->fHaveLowAddress = true;
2520 pRange->uLowAddress = uAddr;
2521 }
2522 else
2523 {
2524 if (pRange->fHaveHighAddress)
2525 {
2526 Log(("rtDwarfDecode_LowHighPc: Duplicate DW_AT_high_pc\n"));
2527 return pCursor->rc = VERR_DWARF_BAD_INFO;
2528 }
2529 pRange->fHaveHighAddress = true;
2530 pRange->uHighAddress = uAddr;
2531 }
2532 pRange->cAttrs++;
2533
2534 return VINF_SUCCESS;
2535}
2536
2537
2538/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2539static DECLCALLBACK(int) rtDwarfDecode_Ranges(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2540 uint32_t uForm, PRTDWARFCURSOR pCursor)
2541{
2542 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDRRANGE), VERR_INTERNAL_ERROR_3);
2543 AssertReturn(pDesc->uAttr == DW_AT_low_pc || pDesc->uAttr == DW_AT_high_pc, VERR_INTERNAL_ERROR_3);
2544
2545 /* Decode it. */
2546 uint64_t off;
2547 switch (uForm)
2548 {
2549 case DW_FORM_addr: off = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
2550 case DW_FORM_data4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
2551 case DW_FORM_data8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
2552 default:
2553 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2554 }
2555 if (RT_FAILURE(pCursor->rc))
2556 return pCursor->rc;
2557
2558 /* Validate the offset and load the ranges. */
2559 PRTDBGMODDWARF pThis = pCursor->pDwarfMod;
2560 if (off >= pThis->aSections[krtDbgModDwarfSect_ranges].cb)
2561 {
2562 Log(("rtDwarfDecode_Ranges: bad ranges off=%#llx\n", off));
2563 return pCursor->rc = VERR_DWARF_BAD_POS;
2564 }
2565
2566 if (!pThis->aSections[krtDbgModDwarfSect_ranges].pv)
2567 {
2568 int rc = rtDbgModDwarfLoadSection(pThis, krtDbgModDwarfSect_ranges);
2569 if (RT_FAILURE(rc))
2570 return pCursor->rc = rc;
2571 }
2572
2573 /* Store the result. */
2574 PRTDWARFADDRRANGE pRange = (PRTDWARFADDRRANGE)pbMember;
2575 if (pRange->fHaveRanges)
2576 {
2577 Log(("rtDwarfDecode_Ranges: Duplicate DW_AT_ranges\n"));
2578 return pCursor->rc = VERR_DWARF_BAD_INFO;
2579 }
2580 pRange->fHaveRanges = true;
2581 pRange->cAttrs++;
2582 pRange->pbRanges = (uint8_t const *)pThis->aSections[krtDbgModDwarfSect_ranges].pv + (size_t)off;
2583
2584 return VINF_SUCCESS;
2585}
2586
2587
2588/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2589static DECLCALLBACK(int) rtDwarfDecode_Reference(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2590 uint32_t uForm, PRTDWARFCURSOR pCursor)
2591{
2592 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFREF), VERR_INTERNAL_ERROR_3);
2593
2594 /* Decode it. */
2595 uint64_t off;
2596 krtDwarfRef enmWrt = krtDwarfRef_InfoSection;
2597 switch (uForm)
2598 {
2599 case DW_FORM_ref1: off = rtDwarfCursor_GetU8(pCursor, 0); break;
2600 case DW_FORM_ref2: off = rtDwarfCursor_GetU16(pCursor, 0); break;
2601 case DW_FORM_ref4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
2602 case DW_FORM_ref8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
2603 case DW_FORM_ref_udata: off = rtDwarfCursor_GetULeb128(pCursor, 0); break;
2604
2605 case DW_FORM_ref_addr:
2606 enmWrt = krtDwarfRef_InfoSection;
2607 off = rtDwarfCursor_GetUOff(pCursor, 0);
2608 break;
2609
2610 case DW_FORM_ref_sig8:
2611 enmWrt = krtDwarfRef_TypeId64;
2612 off = rtDwarfCursor_GetU64(pCursor, 0);
2613 break;
2614
2615 default:
2616 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2617 }
2618 if (RT_FAILURE(pCursor->rc))
2619 return pCursor->rc;
2620
2621 /* Validate the offset and convert to debug_info relative offsets. */
2622 if (enmWrt == krtDwarfRef_InfoSection)
2623 {
2624 if (off >= pCursor->pDwarfMod->aSections[krtDbgModDwarfSect_info].cb)
2625 {
2626 Log(("rtDwarfDecode_Reference: bad info off=%#llx\n", off));
2627 return pCursor->rc = VERR_DWARF_BAD_POS;
2628 }
2629 }
2630 else if (enmWrt == krtDwarfRef_SameUnit)
2631 {
2632 PRTDWARFDIECOMPILEUNIT pUnit = rtDwarfDie_GetCompileUnit(pDie);
2633 if (off >= pUnit->cbUnit)
2634 {
2635 Log(("rtDwarfDecode_Reference: bad unit off=%#llx\n", off));
2636 return pCursor->rc = VERR_DWARF_BAD_POS;
2637 }
2638 off += pUnit->offUnit;
2639 enmWrt = krtDwarfRef_InfoSection;
2640 }
2641 /* else: not bother verifying/resolving the indirect type reference yet. */
2642
2643 /* Store it */
2644 PRTDWARFREF pRef = (PRTDWARFREF)pbMember;
2645 pRef->enmWrt = enmWrt;
2646 pRef->off = off;
2647
2648 return VINF_SUCCESS;
2649}
2650
2651
2652/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2653static DECLCALLBACK(int) rtDwarfDecode_SectOff(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2654 uint32_t uForm, PRTDWARFCURSOR pCursor)
2655{
2656 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFREF), VERR_INTERNAL_ERROR_3);
2657
2658 uint64_t off;
2659 switch (uForm)
2660 {
2661 case DW_FORM_data4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
2662 case DW_FORM_data8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
2663 case DW_FORM_sec_offset: off = rtDwarfCursor_GetUOff(pCursor, 0); break;
2664 default:
2665 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2666 }
2667 if (RT_FAILURE(pCursor->rc))
2668 return pCursor->rc;
2669
2670 krtDbgModDwarfSect enmSect;
2671 krtDwarfRef enmWrt;
2672 switch (pDesc->uAttr)
2673 {
2674 case DW_AT_stmt_list: enmSect = krtDbgModDwarfSect_line; enmWrt = krtDwarfRef_LineSection; break;
2675 case DW_AT_macro_info: enmSect = krtDbgModDwarfSect_loc; enmWrt = krtDwarfRef_LocSection; break;
2676 case DW_AT_ranges: enmSect = krtDbgModDwarfSect_ranges; enmWrt = krtDwarfRef_RangesSection; break;
2677 default: AssertMsgFailedReturn(("%u\n", pDesc->uAttr), VERR_INTERNAL_ERROR_4);
2678 }
2679 if (off >= pCursor->pDwarfMod->aSections[enmSect].cb)
2680 {
2681 Log(("rtDwarfDecode_SectOff: bad off=%#llx, attr %#x\n", off, pDesc->uAttr));
2682 return pCursor->rc = VERR_DWARF_BAD_POS;
2683 }
2684
2685 PRTDWARFREF pRef = (PRTDWARFREF)pbMember;
2686 pRef->enmWrt = enmWrt;
2687 pRef->off = off;
2688
2689 return VINF_SUCCESS;
2690}
2691
2692
2693/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2694static DECLCALLBACK(int) rtDwarfDecode_String(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2695 uint32_t uForm, PRTDWARFCURSOR pCursor)
2696{
2697 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(const char *), VERR_INTERNAL_ERROR_3);
2698
2699 switch (uForm)
2700 {
2701 case DW_FORM_string:
2702 *(const char **)pbMember = rtDwarfCursor_GetSZ(pCursor, NULL);
2703 break;
2704
2705 case DW_FORM_strp:
2706 *(const char **)pbMember = rtDwarfDecode_GetStrp(pCursor->pDwarfMod, pCursor, NULL);
2707 break;
2708
2709 default:
2710 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2711 }
2712
2713 return pCursor->rc;
2714}
2715
2716
2717/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2718static DECLCALLBACK(int) rtDwarfDecode_UnsignedInt(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2719 uint32_t uForm, PRTDWARFCURSOR pCursor)
2720{
2721 uint64_t u64Val;
2722 switch (uForm)
2723 {
2724 case DW_FORM_udata: u64Val = rtDwarfCursor_GetULeb128(pCursor, 0); break;
2725 case DW_FORM_data1: u64Val = rtDwarfCursor_GetU8(pCursor, 0); break;
2726 case DW_FORM_data2: u64Val = rtDwarfCursor_GetU16(pCursor, 0); break;
2727 case DW_FORM_data4: u64Val = rtDwarfCursor_GetU32(pCursor, 0); break;
2728 case DW_FORM_data8: u64Val = rtDwarfCursor_GetU64(pCursor, 0); break;
2729 default:
2730 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2731 }
2732 if (RT_FAILURE(pCursor->rc))
2733 return pCursor->rc;
2734
2735 switch (ATTR_GET_SIZE(pDesc))
2736 {
2737 case 1:
2738 *pbMember = (uint8_t)u64Val;
2739 if (*pbMember != u64Val)
2740 return VERR_OUT_OF_RANGE;
2741 break;
2742
2743 case 2:
2744 *(uint16_t *)pbMember = (uint16_t)u64Val;
2745 if (*(uint16_t *)pbMember != u64Val)
2746 return VERR_OUT_OF_RANGE;
2747 break;
2748
2749 case 4:
2750 *(uint32_t *)pbMember = (uint32_t)u64Val;
2751 if (*(uint32_t *)pbMember != u64Val)
2752 return VERR_OUT_OF_RANGE;
2753 break;
2754
2755 case 8:
2756 *(uint64_t *)pbMember = (uint64_t)u64Val;
2757 if (*(uint64_t *)pbMember != u64Val)
2758 return VERR_OUT_OF_RANGE;
2759 break;
2760
2761 default:
2762 AssertMsgFailedReturn(("%#x\n", ATTR_GET_SIZE(pDesc)), VERR_INTERNAL_ERROR_2);
2763 }
2764 return VINF_SUCCESS;
2765}
2766
2767
2768
2769/*
2770 *
2771 * DWARF debug_info parser
2772 * DWARF debug_info parser
2773 * DWARF debug_info parser
2774 *
2775 */
2776
2777
2778/**
2779 * Parse the attributes of a DIE.
2780 *
2781 * @returns IPRT status code.
2782 * @param pThis The DWARF instance.
2783 * @param pDie The internal DIE structure to fill.
2784 */
2785static int rtDwarfInfo_SnoopSymbols(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie)
2786{
2787 int rc = VINF_SUCCESS;
2788 switch (pDie->uTag)
2789 {
2790 case DW_TAG_subprogram:
2791 {
2792 PCRTDWARFDIESUBPROGRAM pSubProgram = (PCRTDWARFDIESUBPROGRAM)pDie;
2793 if (pSubProgram->PcRange.cAttrs)
2794 {
2795 if (pSubProgram->PcRange.fHaveRanges)
2796 Log5(("subprogram %s (%s) <implement ranges>\n", pSubProgram->pszName, pSubProgram->pszLinkageName));
2797 else
2798 {
2799 Log5(("subprogram %s (%s) %#llx-%#llx%s\n", pSubProgram->pszName, pSubProgram->pszLinkageName,
2800 pSubProgram->PcRange.uLowAddress, pSubProgram->PcRange.uHighAddress,
2801 pSubProgram->PcRange.cAttrs == 2 ? "" : " !bad!"));
2802 if ( pSubProgram->pszName
2803 && pSubProgram->PcRange.cAttrs == 2)
2804 {
2805 RTDBGSEGIDX iSeg;
2806 RTUINTPTR offSeg;
2807 rc = rtDbgModDwarfLinkAddressToSegOffset(pThis, pSubProgram->PcRange.uLowAddress,
2808 &iSeg, &offSeg);
2809 if (RT_SUCCESS(rc))
2810 rc = RTDbgModSymbolAdd(pThis->hCnt, pSubProgram->pszName, iSeg, offSeg,
2811 pSubProgram->PcRange.uHighAddress - pSubProgram->PcRange.uLowAddress,
2812 0 /*fFlags*/, NULL /*piOrdinal*/);
2813 else
2814 Log5(("rtDbgModDwarfLinkAddressToSegOffset failed: %Rrc\n", rc));
2815 }
2816 }
2817 }
2818 else
2819 Log5(("subprogram %s (%s) external\n", pSubProgram->pszName, pSubProgram->pszLinkageName));
2820 break;
2821 }
2822
2823 }
2824 return rc;
2825}
2826
2827
2828/**
2829 * Initializes the non-core fields of an internal DIE structure.
2830 *
2831 * @param pDie The DIE structure.
2832 * @param pDieDesc The DIE descriptor.
2833 */
2834static void rtDwarfInfo_InitDie(PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc)
2835{
2836 size_t i = pDieDesc->cAttributes;
2837 while (i-- > 0)
2838 {
2839 switch (pDieDesc->paAttributes[i].cbInit & ATTR_INIT_MASK)
2840 {
2841 case ATTR_INIT_ZERO:
2842 /* Nothing to do (RTMemAllocZ). */
2843 break;
2844
2845 case ATTR_INIT_FFFS:
2846 switch (pDieDesc->paAttributes[i].cbInit & ATTR_SIZE_MASK)
2847 {
2848 case 1:
2849 *(uint8_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT8_MAX;
2850 break;
2851 case 2:
2852 *(uint16_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT16_MAX;
2853 break;
2854 case 4:
2855 *(uint32_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT32_MAX;
2856 break;
2857 case 8:
2858 *(uint64_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT64_MAX;
2859 break;
2860 default:
2861 AssertFailed();
2862 memset((uint8_t *)pDie + pDieDesc->paAttributes[i].off, 0xff,
2863 pDieDesc->paAttributes[i].cbInit & ATTR_SIZE_MASK);
2864 break;
2865 }
2866 break;
2867
2868 default:
2869 AssertFailed();
2870 }
2871 }
2872}
2873
2874
2875/**
2876 * Creates a new internal DIE structure and links it up.
2877 *
2878 * @returns Pointer to the new DIE structure.
2879 * @param pThis The DWARF instance.
2880 * @param pDieDesc The DIE descriptor (for size and init).
2881 * @param pAbbrev The abbreviation cache entry.
2882 * @param pParent The parent DIE (NULL if unit).
2883 */
2884static PRTDWARFDIE rtDwarfInfo_NewDie(PRTDBGMODDWARF pThis, PCRTDWARFDIEDESC pDieDesc,
2885 PCRTDWARFABBREV pAbbrev, PRTDWARFDIE pParent)
2886{
2887 Assert(pDieDesc->cbDie >= sizeof(RTDWARFDIE));
2888 PRTDWARFDIE pDie = (PRTDWARFDIE)RTMemAllocZ(pDieDesc->cbDie);
2889 if (pDie)
2890 {
2891 rtDwarfInfo_InitDie(pDie, pDieDesc);
2892
2893 pDie->uTag = pAbbrev->uTag;
2894 pDie->offSpec = pAbbrev->offSpec;
2895 pDie->pParent = pParent;
2896 if (pParent)
2897 RTListAppend(&pParent->ChildList, &pDie->SiblingNode);
2898 else
2899 RTListInit(&pDie->SiblingNode);
2900 RTListInit(&pDie->ChildList);
2901
2902 }
2903 return pDie;
2904}
2905
2906
2907/**
2908 * Skips a form.
2909 * @returns IPRT status code
2910 * @param pCursor The cursor.
2911 * @param uForm The form to skip.
2912 */
2913static int rtDwarfInfo_SkipForm(PRTDWARFCURSOR pCursor, uint32_t uForm)
2914{
2915 switch (uForm)
2916 {
2917 case DW_FORM_addr:
2918 return rtDwarfCursor_SkipBytes(pCursor, pCursor->cbNativeAddr);
2919
2920 case DW_FORM_block:
2921 case DW_FORM_exprloc:
2922 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetULeb128(pCursor, 0));
2923
2924 case DW_FORM_block1:
2925 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU8(pCursor, 0));
2926
2927 case DW_FORM_block2:
2928 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU16(pCursor, 0));
2929
2930 case DW_FORM_block4:
2931 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU32(pCursor, 0));
2932
2933 case DW_FORM_data1:
2934 case DW_FORM_ref1:
2935 case DW_FORM_flag:
2936 return rtDwarfCursor_SkipBytes(pCursor, 1);
2937
2938 case DW_FORM_data2:
2939 case DW_FORM_ref2:
2940 return rtDwarfCursor_SkipBytes(pCursor, 2);
2941
2942 case DW_FORM_data4:
2943 case DW_FORM_ref4:
2944 return rtDwarfCursor_SkipBytes(pCursor, 4);
2945
2946 case DW_FORM_data8:
2947 case DW_FORM_ref8:
2948 case DW_FORM_ref_sig8:
2949 return rtDwarfCursor_SkipBytes(pCursor, 8);
2950
2951 case DW_FORM_udata:
2952 case DW_FORM_sdata:
2953 case DW_FORM_ref_udata:
2954 return rtDwarfCursor_SkipLeb128(pCursor);
2955
2956 case DW_FORM_string:
2957 rtDwarfCursor_GetSZ(pCursor, NULL);
2958 return pCursor->rc;
2959
2960 case DW_FORM_indirect:
2961 return rtDwarfInfo_SkipForm(pCursor, rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX));
2962
2963 case DW_FORM_strp:
2964 case DW_FORM_ref_addr:
2965 case DW_FORM_sec_offset:
2966 return rtDwarfCursor_SkipBytes(pCursor, pCursor->f64bitDwarf ? 8 : 4);
2967
2968 case DW_FORM_flag_present:
2969 return pCursor->rc; /* no data */
2970
2971 default:
2972 return VERR_DWARF_UNKNOWN_FORM;
2973 }
2974}
2975
2976
2977
2978/**
2979 * Skips a DIE.
2980 *
2981 * @returns IPRT status code.
2982 * @param pCursor The cursor.
2983 * @param pAbbrevCursor The abbreviation cursor.
2984 */
2985static int rtDwarfInfo_SkipDie(PRTDWARFCURSOR pCursor, PRTDWARFCURSOR pAbbrevCursor)
2986{
2987 for (;;)
2988 {
2989 uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(pAbbrevCursor, 0);
2990 uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(pAbbrevCursor, 0);
2991 if (uAttr == 0 && uForm == 0)
2992 break;
2993
2994 int rc = rtDwarfInfo_SkipForm(pCursor, uForm);
2995 if (RT_FAILURE(rc))
2996 return rc;
2997 }
2998 return RT_FAILURE(pCursor->rc) ? pCursor->rc : pAbbrevCursor->rc;
2999}
3000
3001
3002/**
3003 * Parse the attributes of a DIE.
3004 *
3005 * @returns IPRT status code.
3006 * @param pThis The DWARF instance.
3007 * @param pDie The internal DIE structure to fill.
3008 * @param pDieDesc The DIE descriptor.
3009 * @param pCursor The debug_info cursor.
3010 * @param pAbbrev The abbreviation cache entry.
3011 */
3012static int rtDwarfInfo_ParseDie(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc,
3013 PRTDWARFCURSOR pCursor, PCRTDWARFABBREV pAbbrev)
3014{
3015 RTDWARFCURSOR AbbrevCursor;
3016 int rc = rtDwarfCursor_InitWithOffset(&AbbrevCursor, pThis, krtDbgModDwarfSect_abbrev, pAbbrev->offSpec);
3017 if (RT_FAILURE(rc))
3018 return rc;
3019
3020 rtDwarfInfo_InitDie(pDie, pDieDesc);
3021 for (;;)
3022 {
3023 uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
3024 uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
3025 if (uAttr == 0)
3026 break;
3027 if (uForm == DW_FORM_indirect)
3028 uForm = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
3029
3030 /* Look up the attribute in the descriptor and invoke the decoder. */
3031 PCRTDWARFATTRDESC pAttr = NULL;
3032 size_t i = pDieDesc->cAttributes;
3033 while (i-- > 0)
3034 if (pDieDesc->paAttributes[i].uAttr == uAttr)
3035 {
3036 pAttr = &pDieDesc->paAttributes[i];
3037 rc = pAttr->pfnDecoder(pDie, (uint8_t *)pDie + pAttr->off, pAttr, uForm, pCursor);
3038 break;
3039 }
3040
3041 /* Some house keeping. */
3042 if (pAttr)
3043 pDie->cDecodedAttrs++;
3044 else
3045 {
3046 pDie->cUnhandledAttrs++;
3047 rc = rtDwarfInfo_SkipForm(pCursor, uForm);
3048 }
3049 if (RT_FAILURE(rc))
3050 break;
3051 }
3052
3053 rc = rtDwarfCursor_Delete(&AbbrevCursor, rc);
3054 if (RT_SUCCESS(rc))
3055 rc = pCursor->rc;
3056
3057 /*
3058 * Snoope up symbols on the way out.
3059 */
3060 if (RT_SUCCESS(rc))
3061 rc = rtDwarfInfo_SnoopSymbols(pThis, pDie);
3062
3063 return rc;
3064}
3065
3066
3067/**
3068 * Load the debug information of a unit.
3069 *
3070 * @returns IPRT status code.
3071 * @param pThis The DWARF instance.
3072 * @param pCursor The debug_info cursor.
3073 * @param fKeepDies Whether to keep the DIEs or discard them as soon
3074 * as possible.
3075 */
3076static int rtDwarfInfo_LoadUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor, bool fKeepDies)
3077{
3078 Log(("rtDwarfInfo_LoadUnit: %#x\n", rtDwarfCursor_CalcSectOffsetU32(pCursor)));
3079
3080 /*
3081 * Read the compilation unit header.
3082 */
3083 uint64_t offUnit = rtDwarfCursor_CalcSectOffsetU32(pCursor);
3084 uint64_t cbUnit = rtDwarfCursor_GetInitalLength(pCursor);
3085 cbUnit += rtDwarfCursor_CalcSectOffsetU32(pCursor) - offUnit;
3086 uint16_t const uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
3087 if ( uVer < 2
3088 || uVer > 4)
3089 return rtDwarfCursor_SkipUnit(pCursor);
3090 uint64_t const offAbbrev = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
3091 uint8_t const cbNativeAddr = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
3092 if (RT_FAILURE(pCursor->rc))
3093 return pCursor->rc;
3094 Log((" uVer=%d offAbbrev=%#llx cbNativeAddr=%d\n", uVer, offAbbrev, cbNativeAddr));
3095
3096 /*
3097 * Set up the abbreviation cache and store the native address size in the cursor.
3098 */
3099 if (offAbbrev > UINT32_MAX)
3100 return VERR_DWARF_BAD_INFO;
3101 rtDwarfAbbrev_SetUnitOffset(pThis, (uint32_t)offAbbrev);
3102 pCursor->cbNativeAddr = cbNativeAddr;
3103
3104 /*
3105 * The first DIE is a compile or partial unit, parse it here.
3106 */
3107 uint32_t uAbbrCode = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
3108 if (!uAbbrCode)
3109 return VERR_DWARF_BAD_INFO;
3110 PCRTDWARFABBREV pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
3111 if (!pAbbrev)
3112 return VERR_DWARF_ABBREV_NOT_FOUND;
3113 if ( pAbbrev->uTag != DW_TAG_compile_unit
3114 && pAbbrev->uTag != DW_TAG_partial_unit)
3115 {
3116 Log(("Unexpected compile/partial unit tag %#x\n", pAbbrev->uTag));
3117 return VERR_DWARF_BAD_INFO;
3118 }
3119
3120 PRTDWARFDIECOMPILEUNIT pUnit;
3121 pUnit = (PRTDWARFDIECOMPILEUNIT)rtDwarfInfo_NewDie(pThis, &g_CompileUnitDesc, pAbbrev, NULL /*pParent*/);
3122 if (!pUnit)
3123 return VERR_NO_MEMORY;
3124 pUnit->offUnit = offUnit;
3125 pUnit->cbUnit = cbUnit;
3126 pUnit->offAbbrev = offAbbrev;
3127 pUnit->cbNativeAddr = cbNativeAddr;
3128 pUnit->uDwarfVer = (uint8_t)uVer;
3129 RTListAppend(&pThis->CompileUnitList, &pUnit->Core.SiblingNode);
3130
3131 int rc = rtDwarfInfo_ParseDie(pThis, &pUnit->Core, &g_CompileUnitDesc, pCursor, pAbbrev);
3132 if (RT_FAILURE(rc))
3133 return rc;
3134
3135 /*
3136 * Parse DIEs.
3137 */
3138 uint32_t cDepth = 0;
3139 PRTDWARFDIE pParentDie = &pUnit->Core;
3140 while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
3141 {
3142 uAbbrCode = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
3143 if (!uAbbrCode)
3144 {
3145 /* End of siblings, up one level. */
3146 pParentDie = pParentDie->pParent;
3147 if (!pParentDie)
3148 {
3149 if (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
3150 return VERR_DWARF_BAD_INFO;
3151 break;
3152 }
3153 cDepth--;
3154
3155 /* Unlink and free child DIEs if told to do so. */
3156 if (!fKeepDies && pParentDie->pParent)
3157 {
3158 PRTDWARFDIE pChild, pNextChild;
3159 RTListForEachSafe(&pParentDie->ChildList, pChild, pNextChild, RTDWARFDIE, SiblingNode)
3160 {
3161 RTListNodeRemove(&pChild->SiblingNode);
3162 RTMemFree(pChild);
3163 }
3164 }
3165 }
3166 else
3167 {
3168 /*
3169 * Look up the abbreviation and match the tag up with a descriptor.
3170 */
3171 pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
3172 if (!pAbbrev)
3173 return VERR_DWARF_ABBREV_NOT_FOUND;
3174
3175 PCRTDWARFDIEDESC pDieDesc;
3176 const char *pszName;
3177 if (pAbbrev->uTag < RT_ELEMENTS(g_aTagDescs))
3178 {
3179 Assert(g_aTagDescs[pAbbrev->uTag].uTag == pAbbrev->uTag || g_aTagDescs[pAbbrev->uTag].uTag == 0);
3180 pszName = g_aTagDescs[pAbbrev->uTag].pszName;
3181 pDieDesc = g_aTagDescs[pAbbrev->uTag].pDesc;
3182 }
3183 else
3184 {
3185 pszName = "<unknown>";
3186 pDieDesc = g_aTagDescs[0].pDesc;
3187 }
3188 Log4((" %*stag=%s (%#x)%s\n", cDepth * 2, "", pszName,
3189 pAbbrev->uTag, pAbbrev->fChildren ? " has children" : ""));
3190
3191 /*
3192 * Create a new internal DIE structure and parse the
3193 * attributes.
3194 */
3195 PRTDWARFDIE pNewDie = rtDwarfInfo_NewDie(pThis, pDieDesc, pAbbrev, pParentDie);
3196 if (!pNewDie)
3197 return VERR_NO_MEMORY;
3198
3199 if (pAbbrev->fChildren)
3200 {
3201 pParentDie = pNewDie;
3202 cDepth++;
3203 }
3204
3205 rc = rtDwarfInfo_ParseDie(pThis, pNewDie, pDieDesc, pCursor, pAbbrev);
3206 if (RT_FAILURE(rc))
3207 return rc;
3208 }
3209 } /* while more DIEs */
3210
3211 return RT_SUCCESS(rc) ? pCursor->rc : rc;
3212}
3213
3214
3215/**
3216 * Extracts the symbols.
3217 *
3218 * The symbols are insered into the debug info container.
3219 *
3220 * @returns IPRT status code
3221 * @param pThis The DWARF instance.
3222 */
3223static int rtDwarfInfo_LoadAll(PRTDBGMODDWARF pThis)
3224{
3225 RTDWARFCURSOR Cursor;
3226 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_info);
3227 if (RT_FAILURE(rc))
3228 return rc;
3229
3230 while ( !rtDwarfCursor_IsAtEnd(&Cursor)
3231 && RT_SUCCESS(rc))
3232 rc = rtDwarfInfo_LoadUnit(pThis, &Cursor, false /* fKeepDies */);
3233
3234 return rtDwarfCursor_Delete(&Cursor, rc);
3235}
3236
3237
3238
3239
3240/*
3241 *
3242 * DWARF Debug module implementation.
3243 * DWARF Debug module implementation.
3244 * DWARF Debug module implementation.
3245 *
3246 */
3247
3248
3249/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
3250static DECLCALLBACK(int) rtDbgModDwarf_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
3251 PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
3252{
3253 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3254 return RTDbgModLineByAddr(pThis->hCnt, iSeg, off, poffDisp, pLineInfo);
3255}
3256
3257
3258/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
3259static DECLCALLBACK(int) rtDbgModDwarf_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
3260{
3261 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3262 return RTDbgModLineByOrdinal(pThis->hCnt, iOrdinal, pLineInfo);
3263}
3264
3265
3266/** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
3267static DECLCALLBACK(uint32_t) rtDbgModDwarf_LineCount(PRTDBGMODINT pMod)
3268{
3269 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3270 return RTDbgModLineCount(pThis->hCnt);
3271}
3272
3273
3274/** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
3275static DECLCALLBACK(int) rtDbgModDwarf_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
3276 uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
3277{
3278 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3279 return RTDbgModLineAdd(pThis->hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
3280}
3281
3282
3283/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
3284static DECLCALLBACK(int) rtDbgModDwarf_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
3285 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
3286{
3287 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3288 return RTDbgModSymbolByAddr(pThis->hCnt, iSeg, off, poffDisp, pSymInfo);
3289}
3290
3291
3292/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
3293static DECLCALLBACK(int) rtDbgModDwarf_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
3294 PRTDBGSYMBOL pSymInfo)
3295{
3296 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3297 Assert(!pszSymbol[cchSymbol]);
3298 return RTDbgModSymbolByName(pThis->hCnt, pszSymbol/*, cchSymbol*/, pSymInfo);
3299}
3300
3301
3302/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
3303static DECLCALLBACK(int) rtDbgModDwarf_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
3304{
3305 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3306 return RTDbgModSymbolByOrdinal(pThis->hCnt, iOrdinal, pSymInfo);
3307}
3308
3309
3310/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
3311static DECLCALLBACK(uint32_t) rtDbgModDwarf_SymbolCount(PRTDBGMODINT pMod)
3312{
3313 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3314 return RTDbgModSymbolCount(pThis->hCnt);
3315}
3316
3317
3318/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
3319static DECLCALLBACK(int) rtDbgModDwarf_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
3320 RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
3321 uint32_t *piOrdinal)
3322{
3323 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3324 return RTDbgModSymbolAdd(pThis->hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
3325}
3326
3327
3328/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
3329static DECLCALLBACK(int) rtDbgModDwarf_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
3330{
3331 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3332 return RTDbgModSegmentByIndex(pThis->hCnt, iSeg, pSegInfo);
3333}
3334
3335
3336/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
3337static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_SegmentCount(PRTDBGMODINT pMod)
3338{
3339 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3340 return RTDbgModSegmentCount(pThis->hCnt);
3341}
3342
3343
3344/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
3345static DECLCALLBACK(int) rtDbgModDwarf_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
3346 uint32_t fFlags, PRTDBGSEGIDX piSeg)
3347{
3348 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3349 return RTDbgModSegmentAdd(pThis->hCnt, uRva, cb, pszName, fFlags, piSeg);
3350}
3351
3352
3353/** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
3354static DECLCALLBACK(RTUINTPTR) rtDbgModDwarf_ImageSize(PRTDBGMODINT pMod)
3355{
3356 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3357 RTUINTPTR cb1 = RTDbgModImageSize(pThis->hCnt);
3358 RTUINTPTR cb2 = pMod->pImgVt->pfnImageSize(pMod);
3359 return RT_MAX(cb1, cb2);
3360}
3361
3362
3363/** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
3364static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
3365{
3366 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3367 return RTDbgModRvaToSegOff(pThis->hCnt, uRva, poffSeg);
3368}
3369
3370
3371/** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
3372static DECLCALLBACK(int) rtDbgModDwarf_Close(PRTDBGMODINT pMod)
3373{
3374 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3375
3376 for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
3377 if (pThis->aSections[iSect].pv)
3378 pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[iSect].cb, &pThis->aSections[iSect].pv);
3379
3380 RTDbgModRelease(pThis->hCnt);
3381 RTMemFree(pThis->paCachedAbbrevs);
3382 RTMemFree(pThis);
3383
3384 return VINF_SUCCESS;
3385}
3386
3387
3388/** @callback_method_impl{FNRTLDRENUMDBG} */
3389static DECLCALLBACK(int) rtDbgModDwarfEnumCallback(RTLDRMOD hLdrMod, uint32_t iDbgInfo, RTLDRDBGINFOTYPE enmType,
3390 uint16_t iMajorVer, uint16_t iMinorVer, const char *pszPartNm,
3391 RTFOFF offFile, RTLDRADDR LinkAddress, RTLDRADDR cb,
3392 const char *pszExtFile, void *pvUser)
3393{
3394 /*
3395 * Skip stuff we can't handle.
3396 */
3397 if ( enmType != RTLDRDBGINFOTYPE_DWARF
3398 || !pszPartNm
3399 || pszExtFile)
3400 return VINF_SUCCESS;
3401
3402 /*
3403 * Must have a part name starting with debug_ and possibly prefixed by dots
3404 * or underscores.
3405 */
3406 if (!strncmp(pszPartNm, ".debug_", sizeof(".debug_") - 1)) /* ELF */
3407 pszPartNm += sizeof(".debug_") - 1;
3408 else if (!strncmp(pszPartNm, "__debug_", sizeof("__debug_") - 1)) /* Mach-O */
3409 pszPartNm += sizeof("__debug_") - 1;
3410 else
3411 AssertMsgFailedReturn(("%s\n", pszPartNm), VINF_SUCCESS /*ignore*/);
3412
3413 /*
3414 * Figure out which part we're talking about.
3415 */
3416 krtDbgModDwarfSect enmSect;
3417 if (0) { /* dummy */ }
3418#define ELSE_IF_STRCMP_SET(a_Name) else if (!strcmp(pszPartNm, #a_Name)) enmSect = krtDbgModDwarfSect_ ## a_Name
3419 ELSE_IF_STRCMP_SET(abbrev);
3420 ELSE_IF_STRCMP_SET(aranges);
3421 ELSE_IF_STRCMP_SET(frame);
3422 ELSE_IF_STRCMP_SET(info);
3423 ELSE_IF_STRCMP_SET(inlined);
3424 ELSE_IF_STRCMP_SET(line);
3425 ELSE_IF_STRCMP_SET(loc);
3426 ELSE_IF_STRCMP_SET(macinfo);
3427 ELSE_IF_STRCMP_SET(pubnames);
3428 ELSE_IF_STRCMP_SET(pubtypes);
3429 ELSE_IF_STRCMP_SET(ranges);
3430 ELSE_IF_STRCMP_SET(str);
3431 ELSE_IF_STRCMP_SET(types);
3432#undef ELSE_IF_STRCMP_SET
3433 else
3434 {
3435 AssertMsgFailed(("%s\n", pszPartNm));
3436 return VINF_SUCCESS;
3437 }
3438
3439 /*
3440 * Record the section.
3441 */
3442 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
3443 AssertMsgReturn(!pThis->aSections[enmSect].fPresent, ("duplicate %s\n", pszPartNm), VINF_SUCCESS /*ignore*/);
3444
3445 pThis->aSections[enmSect].fPresent = true;
3446 pThis->aSections[enmSect].offFile = offFile;
3447 pThis->aSections[enmSect].pv = NULL;
3448 pThis->aSections[enmSect].cb = (size_t)cb;
3449 if (pThis->aSections[enmSect].cb != cb)
3450 pThis->aSections[enmSect].cb = ~(size_t)0;
3451
3452 return VINF_SUCCESS;
3453}
3454
3455
3456/** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
3457static DECLCALLBACK(int) rtDbgModDwarf_TryOpen(PRTDBGMODINT pMod)
3458{
3459 /*
3460 * DWARF is only supported when part of an image.
3461 */
3462 if (!pMod->pImgVt)
3463 return VERR_DBG_NO_MATCHING_INTERPRETER;
3464
3465 /*
3466 * Enumerate the debug info in the module, looking for DWARF bits.
3467 */
3468 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)RTMemAllocZ(sizeof(*pThis));
3469 if (!pThis)
3470 return VERR_NO_MEMORY;
3471 pThis->pMod = pMod;
3472 RTListInit(&pThis->CompileUnitList);
3473
3474 int rc = pMod->pImgVt->pfnEnumDbgInfo(pMod, rtDbgModDwarfEnumCallback, pThis);
3475 if (RT_SUCCESS(rc))
3476 {
3477 if (pThis->aSections[krtDbgModDwarfSect_info].fPresent)
3478 {
3479 /*
3480 * Extract / explode the data we want (symbols and line numbers)
3481 * storing them in a container module.
3482 */
3483 rc = RTDbgModCreate(&pThis->hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
3484 if (RT_SUCCESS(rc))
3485 {
3486 pMod->pvDbgPriv = pThis;
3487
3488 rc = rtDbgModHlpAddSegmentsFromImage(pMod);
3489 if (RT_SUCCESS(rc))
3490 rc = rtDwarfInfo_LoadAll(pThis);
3491 if (RT_SUCCESS(rc))
3492 rc = rtDwarfLine_ExplodeAll(pThis);
3493 if (RT_SUCCESS(rc))
3494 {
3495 /*
3496 * Free the cached abbreviations and unload all sections.
3497 */
3498 pThis->cCachedAbbrevs = pThis->cCachedAbbrevsAlloced = 0;
3499 RTMemFree(pThis->paCachedAbbrevs);
3500
3501 for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
3502 if (pThis->aSections[iSect].pv)
3503 pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[iSect].cb,
3504 &pThis->aSections[iSect].pv);
3505
3506
3507 return VINF_SUCCESS;
3508 }
3509
3510 /* bail out. */
3511 RTDbgModRelease(pThis->hCnt);
3512 pMod->pvDbgPriv = NULL;
3513 }
3514 }
3515 else
3516 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
3517 }
3518 RTMemFree(pThis->paCachedAbbrevs);
3519 RTMemFree(pThis);
3520
3521 return rc;
3522}
3523
3524
3525
3526/** Virtual function table for the DWARF debug info reader. */
3527DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDwarf =
3528{
3529 /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
3530 /*.fSupports = */ RT_DBGTYPE_DWARF,
3531 /*.pszName = */ "dwarf",
3532 /*.pfnTryOpen = */ rtDbgModDwarf_TryOpen,
3533 /*.pfnClose = */ rtDbgModDwarf_Close,
3534
3535 /*.pfnRvaToSegOff = */ rtDbgModDwarf_RvaToSegOff,
3536 /*.pfnImageSize = */ rtDbgModDwarf_ImageSize,
3537
3538 /*.pfnSegmentAdd = */ rtDbgModDwarf_SegmentAdd,
3539 /*.pfnSegmentCount = */ rtDbgModDwarf_SegmentCount,
3540 /*.pfnSegmentByIndex = */ rtDbgModDwarf_SegmentByIndex,
3541
3542 /*.pfnSymbolAdd = */ rtDbgModDwarf_SymbolAdd,
3543 /*.pfnSymbolCount = */ rtDbgModDwarf_SymbolCount,
3544 /*.pfnSymbolByOrdinal = */ rtDbgModDwarf_SymbolByOrdinal,
3545 /*.pfnSymbolByName = */ rtDbgModDwarf_SymbolByName,
3546 /*.pfnSymbolByAddr = */ rtDbgModDwarf_SymbolByAddr,
3547
3548 /*.pfnLineAdd = */ rtDbgModDwarf_LineAdd,
3549 /*.pfnLineCount = */ rtDbgModDwarf_LineCount,
3550 /*.pfnLineByOrdinal = */ rtDbgModDwarf_LineByOrdinal,
3551 /*.pfnLineByAddr = */ rtDbgModDwarf_LineByAddr,
3552
3553 /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
3554};
3555
Note: See TracBrowser for help on using the repository browser.

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