VirtualBox

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

Last change on this file since 39080 was 39070, checked in by vboxsync, 14 years ago

VMM,IPRT: -Wunused-function.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 121.2 KB
Line 
1/* $Id: dbgmoddwarf.cpp 39070 2011-10-21 09:41:18Z 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.215389.xyz. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 *
17 * The contents of this file may alternatively be used under the terms
18 * of the Common Development and Distribution License Version 1.0
19 * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
20 * VirtualBox OSE distribution, in which case the provisions of the
21 * CDDL are applicable instead of those of the GPL.
22 *
23 * You may elect to license modified versions of this file under the
24 * terms and conditions of either the GPL or the CDDL or both.
25 */
26
27
28/*******************************************************************************
29* Header Files *
30*******************************************************************************/
31#define LOG_GROUP RTLOGGROUP_DBG_DWARF
32#include <iprt/dbg.h>
33#include "internal/iprt.h"
34
35#include <iprt/asm.h>
36#include <iprt/ctype.h>
37#include <iprt/err.h>
38#include <iprt/list.h>
39#include <iprt/log.h>
40#include <iprt/mem.h>
41#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#ifdef SOME_UNUSED_FUNCTION
895/**
896 * Unloads a DWARF section previously mapped by rtDbgModDwarfLoadSection.
897 *
898 * @returns IPRT status code.
899 * @param pThis The DWARF instance.
900 * @param enmSect The section to unload.
901 */
902static int rtDbgModDwarfUnloadSection(PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
903{
904 if (!pThis->aSections[enmSect].pv)
905 return VINF_SUCCESS;
906
907 int rc = pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[enmSect].cb, &pThis->aSections[enmSect].pv);
908 AssertRC(rc);
909 return rc;
910}
911#endif
912
913
914/**
915 * Converts to UTF-8 or otherwise makes sure it's valid UTF-8.
916 *
917 * @returns IPRT status code.
918 * @param pThis The DWARF instance.
919 * @param ppsz Pointer to the string pointer. May be
920 * reallocated (RTStr*).
921 */
922static int rtDbgModDwarfStringToUtf8(PRTDBGMODDWARF pThis, char **ppsz)
923{
924 RTStrPurgeEncoding(*ppsz);
925 return VINF_SUCCESS;
926}
927
928
929/**
930 * Convers a link address into a segment+offset or RVA.
931 *
932 * @returns IPRT status code.
933 * @param pThis The DWARF instance.
934 * @param LinkAddress The address to convert..
935 * @param piSeg The segment index.
936 * @param poffSeg Where to return the segment offset.
937 */
938static int rtDbgModDwarfLinkAddressToSegOffset(PRTDBGMODDWARF pThis, uint64_t LinkAddress,
939 PRTDBGSEGIDX piSeg, PRTLDRADDR poffSeg)
940{
941 return pThis->pMod->pImgVt->pfnLinkAddressToSegOffset(pThis->pMod, LinkAddress, piSeg, poffSeg);
942}
943
944
945/*
946 *
947 * DWARF Cursor.
948 * DWARF Cursor.
949 * DWARF Cursor.
950 *
951 */
952
953
954/**
955 * Reads a 8-bit unsigned integer and advances the cursor.
956 *
957 * @returns 8-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
958 * uErrValue is returned.
959 * @param pCursor The cursor.
960 * @param uErrValue What to return on read error.
961 */
962static uint8_t rtDwarfCursor_GetU8(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
963{
964 if (pCursor->cbUnitLeft < 1)
965 {
966 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
967 return uErrValue;
968 }
969
970 uint8_t u8 = pCursor->pb[0];
971 pCursor->pb += 1;
972 pCursor->cbUnitLeft -= 1;
973 pCursor->cbLeft -= 1;
974 return u8;
975}
976
977
978/**
979 * Reads a 16-bit unsigned integer and advances the cursor.
980 *
981 * @returns 16-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
982 * uErrValue is returned.
983 * @param pCursor The cursor.
984 * @param uErrValue What to return on read error.
985 */
986static uint16_t rtDwarfCursor_GetU16(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
987{
988 if (pCursor->cbUnitLeft < 2)
989 {
990 pCursor->pb += pCursor->cbUnitLeft;
991 pCursor->cbLeft -= pCursor->cbUnitLeft;
992 pCursor->cbUnitLeft = 0;
993 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
994 return uErrValue;
995 }
996
997 uint16_t u16 = RT_MAKE_U16(pCursor->pb[0], pCursor->pb[1]);
998 pCursor->pb += 2;
999 pCursor->cbUnitLeft -= 2;
1000 pCursor->cbLeft -= 2;
1001 if (!pCursor->fNativEndian)
1002 u16 = RT_BSWAP_U16(u16);
1003 return u16;
1004}
1005
1006
1007/**
1008 * Reads a 32-bit unsigned integer and advances the cursor.
1009 *
1010 * @returns 32-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
1011 * uErrValue is returned.
1012 * @param pCursor The cursor.
1013 * @param uErrValue What to return on read error.
1014 */
1015static uint32_t rtDwarfCursor_GetU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
1016{
1017 if (pCursor->cbUnitLeft < 4)
1018 {
1019 pCursor->pb += pCursor->cbUnitLeft;
1020 pCursor->cbLeft -= pCursor->cbUnitLeft;
1021 pCursor->cbUnitLeft = 0;
1022 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1023 return uErrValue;
1024 }
1025
1026 uint32_t u32 = RT_MAKE_U32_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3]);
1027 pCursor->pb += 4;
1028 pCursor->cbUnitLeft -= 4;
1029 pCursor->cbLeft -= 4;
1030 if (!pCursor->fNativEndian)
1031 u32 = RT_BSWAP_U32(u32);
1032 return u32;
1033}
1034
1035
1036/**
1037 * Reads a 64-bit unsigned integer and advances the cursor.
1038 *
1039 * @returns 64-bit unsigned integer. On error RTDWARFCURSOR::rc is set and @a
1040 * uErrValue is returned.
1041 * @param pCursor The cursor.
1042 * @param uErrValue What to return on read error.
1043 */
1044static uint64_t rtDwarfCursor_GetU64(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1045{
1046 if (pCursor->cbUnitLeft < 8)
1047 {
1048 pCursor->pb += pCursor->cbUnitLeft;
1049 pCursor->cbLeft -= pCursor->cbUnitLeft;
1050 pCursor->cbUnitLeft = 0;
1051 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1052 return uErrValue;
1053 }
1054
1055 uint64_t u64 = RT_MAKE_U64_FROM_U8(pCursor->pb[0], pCursor->pb[1], pCursor->pb[2], pCursor->pb[3],
1056 pCursor->pb[4], pCursor->pb[5], pCursor->pb[6], pCursor->pb[7]);
1057 pCursor->pb += 8;
1058 pCursor->cbUnitLeft -= 8;
1059 pCursor->cbLeft -= 8;
1060 if (!pCursor->fNativEndian)
1061 u64 = RT_BSWAP_U64(u64);
1062 return u64;
1063}
1064
1065
1066/**
1067 * Reads an unsigned LEB128 encoded number.
1068 *
1069 * @returns unsigned 64-bit number. On error RTDWARFCURSOR::rc is set and @a
1070 * uErrValue is returned.
1071 * @param pCursor The cursor.
1072 * @param uErrValue The value to return on error.
1073 */
1074static uint64_t rtDwarfCursor_GetULeb128(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1075{
1076 if (pCursor->cbUnitLeft < 1)
1077 {
1078 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1079 return uErrValue;
1080 }
1081
1082 /*
1083 * Special case - single byte.
1084 */
1085 uint8_t b = pCursor->pb[0];
1086 if (!(b & 0x80))
1087 {
1088 pCursor->pb += 1;
1089 pCursor->cbUnitLeft -= 1;
1090 pCursor->cbLeft -= 1;
1091 return b;
1092 }
1093
1094 /*
1095 * Generic case.
1096 */
1097 /* Decode. */
1098 uint32_t off = 1;
1099 uint64_t u64Ret = b & 0x7f;
1100 do
1101 {
1102 if (off == pCursor->cbUnitLeft)
1103 {
1104 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1105 u64Ret = uErrValue;
1106 break;
1107 }
1108 b = pCursor->pb[off];
1109 u64Ret |= (b & 0x7f) << off * 7;
1110 off++;
1111 } while (b & 0x80);
1112
1113 /* Update the cursor. */
1114 pCursor->pb += off;
1115 pCursor->cbUnitLeft -= off;
1116 pCursor->cbLeft -= off;
1117
1118 /* Check the range. */
1119 uint32_t cBits = off * 7;
1120 if (cBits > 64)
1121 {
1122 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1123 u64Ret = uErrValue;
1124 }
1125
1126 return u64Ret;
1127}
1128
1129
1130/**
1131 * Reads a signed LEB128 encoded number.
1132 *
1133 * @returns signed 64-bit number. On error RTDWARFCURSOR::rc is set and @a
1134 * uErrValue is returned.
1135 * @param pCursor The cursor.
1136 * @param sErrValue The value to return on error.
1137 */
1138static int64_t rtDwarfCursor_GetSLeb128(PRTDWARFCURSOR pCursor, int64_t sErrValue)
1139{
1140 if (pCursor->cbUnitLeft < 1)
1141 {
1142 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1143 return sErrValue;
1144 }
1145
1146 /*
1147 * Special case - single byte.
1148 */
1149 uint8_t b = pCursor->pb[0];
1150 if (!(b & 0x80))
1151 {
1152 pCursor->pb += 1;
1153 pCursor->cbUnitLeft -= 1;
1154 pCursor->cbLeft -= 1;
1155 if (b & 0x40)
1156 b |= 0x80;
1157 return (int8_t)b;
1158 }
1159
1160 /*
1161 * Generic case.
1162 */
1163 /* Decode it. */
1164 uint32_t off = 1;
1165 uint64_t u64Ret = b & 0x7f;
1166 do
1167 {
1168 if (off == pCursor->cbUnitLeft)
1169 {
1170 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1171 u64Ret = (uint64_t)sErrValue;
1172 break;
1173 }
1174 b = pCursor->pb[off];
1175 u64Ret |= (b & 0x7f) << off * 7;
1176 off++;
1177 } while (b & 0x80);
1178
1179 /* Update cursor. */
1180 pCursor->pb += off;
1181 pCursor->cbUnitLeft -= off;
1182 pCursor->cbLeft -= off;
1183
1184 /* Check the range. */
1185 uint32_t cBits = off * 7;
1186 if (cBits > 64)
1187 {
1188 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1189 u64Ret = (uint64_t)sErrValue;
1190 }
1191 /* Sign extend the value. */
1192 else if (u64Ret & RT_BIT_64(cBits - 1))
1193 u64Ret |= ~(RT_BIT_64(cBits - 1) - 1);
1194
1195 return (int64_t)u64Ret;
1196}
1197
1198
1199/**
1200 * Reads an unsigned LEB128 encoded number, max 32-bit width.
1201 *
1202 * @returns unsigned 32-bit number. On error RTDWARFCURSOR::rc is set and @a
1203 * uErrValue is returned.
1204 * @param pCursor The cursor.
1205 * @param uErrValue The value to return on error.
1206 */
1207static uint32_t rtDwarfCursor_GetULeb128AsU32(PRTDWARFCURSOR pCursor, uint32_t uErrValue)
1208{
1209 uint64_t u64 = rtDwarfCursor_GetULeb128(pCursor, uErrValue);
1210 if (u64 > UINT32_MAX)
1211 {
1212 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1213 return uErrValue;
1214 }
1215 return (uint32_t)u64;
1216}
1217
1218
1219/**
1220 * Reads a signed LEB128 encoded number, max 32-bit width.
1221 *
1222 * @returns signed 32-bit number. On error RTDWARFCURSOR::rc is set and @a
1223 * uErrValue is returned.
1224 * @param pCursor The cursor.
1225 * @param sErrValue The value to return on error.
1226 */
1227static int32_t rtDwarfCursor_GetSLeb128AsS32(PRTDWARFCURSOR pCursor, int32_t sErrValue)
1228{
1229 int64_t s64 = rtDwarfCursor_GetSLeb128(pCursor, sErrValue);
1230 if (s64 > INT32_MAX || s64 < INT32_MIN)
1231 {
1232 pCursor->rc = VERR_DWARF_LEB_OVERFLOW;
1233 return sErrValue;
1234 }
1235 return (int32_t)s64;
1236}
1237
1238
1239/**
1240 * Skips a LEB128 encoded number.
1241 *
1242 * @returns IPRT status code.
1243 * @param pCursor The cursor.
1244 */
1245static int rtDwarfCursor_SkipLeb128(PRTDWARFCURSOR pCursor)
1246{
1247 if (RT_FAILURE(pCursor->rc))
1248 return pCursor->rc;
1249
1250 if (pCursor->cbUnitLeft < 1)
1251 return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1252
1253 uint32_t offSkip = 1;
1254 if (pCursor->pb[0] & 0x80)
1255 do
1256 {
1257 if (offSkip == pCursor->cbUnitLeft)
1258 {
1259 pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1260 break;
1261 }
1262 } while (pCursor->pb[offSkip++] & 0x80);
1263
1264 pCursor->pb += offSkip;
1265 pCursor->cbUnitLeft -= offSkip;
1266 pCursor->cbLeft -= offSkip;
1267 return pCursor->rc;
1268}
1269
1270
1271/**
1272 * Advances the cursor a given number of bytes.
1273 *
1274 * @returns IPRT status code.
1275 * @param pCursor The cursor.
1276 * @param offSkip The number of bytes to advance.
1277 */
1278static int rtDwarfCursor_SkipBytes(PRTDWARFCURSOR pCursor, uint64_t offSkip)
1279{
1280 if (RT_FAILURE(pCursor->rc))
1281 return pCursor->rc;
1282 if (pCursor->cbUnitLeft < offSkip)
1283 return pCursor->rc = VERR_DWARF_UNEXPECTED_END;
1284
1285 size_t const offSkipSizeT = (size_t)offSkip;
1286 pCursor->cbUnitLeft -= offSkipSizeT;
1287 pCursor->cbLeft -= offSkipSizeT;
1288 pCursor->pb += offSkipSizeT;
1289
1290 return VINF_SUCCESS;
1291}
1292
1293
1294/**
1295 * Reads a zero terminated string, advancing the cursor beyond the terminator.
1296 *
1297 * @returns Pointer to the string.
1298 * @param pCursor The cursor.
1299 * @param pszErrValue What to return if the string isn't terminated
1300 * before the end of the unit.
1301 */
1302static const char *rtDwarfCursor_GetSZ(PRTDWARFCURSOR pCursor, const char *pszErrValue)
1303{
1304 const char *pszRet = (const char *)pCursor->pb;
1305 for (;;)
1306 {
1307 if (!pCursor->cbUnitLeft)
1308 {
1309 pCursor->rc = VERR_DWARF_BAD_STRING;
1310 return pszErrValue;
1311 }
1312 pCursor->cbUnitLeft--;
1313 pCursor->cbLeft--;
1314 if (!*pCursor->pb++)
1315 break;
1316 }
1317 return pszRet;
1318}
1319
1320
1321/**
1322 * Reads a 1, 2, 4 or 8 byte unsgined value.
1323 *
1324 * @returns 64-bit unsigned value.
1325 * @param pCursor The cursor.
1326 * @param cbValue The value size.
1327 * @param uErrValue The error value.
1328 */
1329static uint64_t rtDwarfCursor_GetVarSizedU(PRTDWARFCURSOR pCursor, size_t cbValue, uint64_t uErrValue)
1330{
1331 uint64_t u64Ret;
1332 switch (cbValue)
1333 {
1334 case 1: u64Ret = rtDwarfCursor_GetU8( pCursor, UINT8_MAX); break;
1335 case 2: u64Ret = rtDwarfCursor_GetU16(pCursor, UINT16_MAX); break;
1336 case 4: u64Ret = rtDwarfCursor_GetU32(pCursor, UINT32_MAX); break;
1337 case 8: u64Ret = rtDwarfCursor_GetU64(pCursor, UINT64_MAX); break;
1338 default:
1339 pCursor->rc = VERR_DWARF_BAD_INFO;
1340 return uErrValue;
1341 }
1342 if (RT_FAILURE(pCursor->rc))
1343 return uErrValue;
1344 return u64Ret;
1345}
1346
1347
1348/**
1349 * Reads an unsigned DWARF half number.
1350 *
1351 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
1352 * uErrValue is returned.
1353 * @param pCursor The cursor.
1354 * @param uErrValue What to return on error.
1355 */
1356static uint16_t rtDwarfCursor_GetUHalf(PRTDWARFCURSOR pCursor, uint16_t uErrValue)
1357{
1358 return rtDwarfCursor_GetU16(pCursor, uErrValue);
1359}
1360
1361
1362/**
1363 * Reads an unsigned DWARF byte number.
1364 *
1365 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
1366 * uErrValue is returned.
1367 * @param pCursor The cursor.
1368 * @param uErrValue What to return on error.
1369 */
1370static uint8_t rtDwarfCursor_GetUByte(PRTDWARFCURSOR pCursor, uint8_t uErrValue)
1371{
1372 return rtDwarfCursor_GetU8(pCursor, uErrValue);
1373}
1374
1375
1376/**
1377 * Reads a signed DWARF byte number.
1378 *
1379 * @returns The number. On error RTDWARFCURSOR::rc is set and @a
1380 * uErrValue is returned.
1381 * @param pCursor The cursor.
1382 * @param uErrValue What to return on error.
1383 */
1384static int8_t rtDwarfCursor_GetSByte(PRTDWARFCURSOR pCursor, int8_t iErrValue)
1385{
1386 return (int8_t)rtDwarfCursor_GetU8(pCursor, (uint8_t)iErrValue);
1387}
1388
1389
1390/**
1391 * Reads a unsigned DWARF offset value.
1392 *
1393 * @returns The value. On error RTDWARFCURSOR::rc is set and @a
1394 * uErrValue is returned.
1395 * @param pCursor The cursor.
1396 * @param uErrValue What to return on error.
1397 */
1398static uint64_t rtDwarfCursor_GetUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1399{
1400 if (pCursor->f64bitDwarf)
1401 return rtDwarfCursor_GetU64(pCursor, uErrValue);
1402 return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
1403}
1404
1405
1406/**
1407 * Reads a unsigned DWARF native offset value.
1408 *
1409 * @returns The value. On error RTDWARFCURSOR::rc is set and @a
1410 * uErrValue is returned.
1411 * @param pCursor The cursor.
1412 * @param uErrValue What to return on error.
1413 */
1414static uint64_t rtDwarfCursor_GetNativeUOff(PRTDWARFCURSOR pCursor, uint64_t uErrValue)
1415{
1416 switch (pCursor->cbNativeAddr)
1417 {
1418 case 1: return rtDwarfCursor_GetU8(pCursor, (uint8_t )uErrValue);
1419 case 2: return rtDwarfCursor_GetU16(pCursor, (uint16_t)uErrValue);
1420 case 4: return rtDwarfCursor_GetU32(pCursor, (uint32_t)uErrValue);
1421 case 8: return rtDwarfCursor_GetU64(pCursor, uErrValue);
1422 default:
1423 pCursor->rc = VERR_INTERNAL_ERROR_2;
1424 return uErrValue;
1425 }
1426}
1427
1428
1429/**
1430 * Gets the unit length, updating the unit length member and DWARF bitness
1431 * members of the cursor.
1432 *
1433 * @returns The unit length.
1434 * @param pCursor The cursor.
1435 */
1436static uint64_t rtDwarfCursor_GetInitalLength(PRTDWARFCURSOR pCursor)
1437{
1438 /*
1439 * Read the initial length.
1440 */
1441 pCursor->cbUnitLeft = pCursor->cbLeft;
1442 uint64_t cbUnit = rtDwarfCursor_GetU32(pCursor, 0);
1443 if (cbUnit != UINT32_C(0xffffffff))
1444 pCursor->f64bitDwarf = false;
1445 else
1446 {
1447 pCursor->f64bitDwarf = true;
1448 cbUnit = rtDwarfCursor_GetU64(pCursor, 0);
1449 }
1450
1451
1452 /*
1453 * Set the unit length, quitely fixing bad lengths.
1454 */
1455 pCursor->cbUnitLeft = (size_t)cbUnit;
1456 if ( pCursor->cbUnitLeft > pCursor->cbLeft
1457 || pCursor->cbUnitLeft != cbUnit)
1458 pCursor->cbUnitLeft = pCursor->cbLeft;
1459
1460 return cbUnit;
1461}
1462
1463
1464/**
1465 * Calculates the section offset corresponding to the current cursor position.
1466 *
1467 * @returns 32-bit section offset. If out of range, RTDWARFCURSOR::rc will be
1468 * set and UINT32_MAX returned.
1469 * @param pCursor The cursor.
1470 */
1471static uint32_t rtDwarfCursor_CalcSectOffsetU32(PRTDWARFCURSOR pCursor)
1472{
1473 size_t off = pCursor->pb - (uint8_t const *)pCursor->pDwarfMod->aSections[pCursor->enmSect].pv;
1474 uint32_t offRet = (uint32_t)off;
1475 if (offRet != off)
1476 {
1477 pCursor->rc = VERR_OUT_OF_RANGE;
1478 offRet = UINT32_MAX;
1479 }
1480 return offRet;
1481}
1482
1483
1484/**
1485 * Calculates an absolute cursor position from one relative to the current
1486 * cursor position.
1487 *
1488 * @returns The absolute cursor position.
1489 * @param pCursor The cursor.
1490 * @param offRelative The relative position. Must be a positive
1491 * offset.
1492 */
1493static uint8_t const *rtDwarfCursor_CalcPos(PRTDWARFCURSOR pCursor, size_t offRelative)
1494{
1495 if (offRelative > pCursor->cbUnitLeft)
1496 {
1497 Log(("rtDwarfCursor_CalcPos: bad position %#zx, cbUnitLeft=%#zu\n", offRelative, pCursor->cbUnitLeft));
1498 pCursor->rc = VERR_DWARF_BAD_POS;
1499 return NULL;
1500 }
1501 return pCursor->pb + offRelative;
1502}
1503
1504
1505/**
1506 * Advances the cursor to the given position.
1507 *
1508 * @returns IPRT status code.
1509 * @param pCursor The cursor.
1510 * @param pbNewPos The new position - returned by
1511 * rtDwarfCursor_CalcPos().
1512 */
1513static int rtDwarfCursor_AdvanceToPos(PRTDWARFCURSOR pCursor, uint8_t const *pbNewPos)
1514{
1515 if (RT_FAILURE(pCursor->rc))
1516 return pCursor->rc;
1517 AssertPtr(pbNewPos);
1518 if ((uintptr_t)pbNewPos < (uintptr_t)pCursor->pb)
1519 {
1520 Log(("rtDwarfCursor_AdvanceToPos: bad position %p, current %p\n", pbNewPos, pCursor->pb));
1521 return pCursor->rc = VERR_DWARF_BAD_POS;
1522 }
1523
1524 uintptr_t cbAdj = (uintptr_t)pbNewPos - (uintptr_t)pCursor->pb;
1525 if (RT_UNLIKELY(cbAdj > pCursor->cbUnitLeft))
1526 {
1527 AssertFailed();
1528 pCursor->rc = VERR_DWARF_BAD_POS;
1529 cbAdj = pCursor->cbUnitLeft;
1530 }
1531
1532 pCursor->cbUnitLeft -= cbAdj;
1533 pCursor->cbLeft -= cbAdj;
1534 pCursor->pb += cbAdj;
1535 return pCursor->rc;
1536}
1537
1538
1539/**
1540 * Check if the cursor is at the end of the current DWARF unit.
1541 *
1542 * @retval @c true if at the end or a cursor error is pending.
1543 * @retval @c false if not.
1544 * @param pCursor The cursor.
1545 */
1546static bool rtDwarfCursor_IsAtEndOfUnit(PRTDWARFCURSOR pCursor)
1547{
1548 return !pCursor->cbUnitLeft || RT_FAILURE(pCursor->rc);
1549}
1550
1551
1552/**
1553 * Skips to the end of the current unit.
1554 *
1555 * @returns IPRT status code.
1556 * @param pCursor The cursor.
1557 */
1558static int rtDwarfCursor_SkipUnit(PRTDWARFCURSOR pCursor)
1559{
1560 pCursor->pb += pCursor->cbUnitLeft;
1561 pCursor->cbLeft -= pCursor->cbUnitLeft;
1562 pCursor->cbUnitLeft = 0;
1563 return pCursor->rc;
1564}
1565
1566
1567/**
1568 * Check if the cursor is at the end of the section (or whatever the cursor is
1569 * processing).
1570 *
1571 * @retval @c true if at the end or a cursor error is pending.
1572 * @retval @c false if not.
1573 * @param pCursor The cursor.
1574 */
1575static bool rtDwarfCursor_IsAtEnd(PRTDWARFCURSOR pCursor)
1576{
1577 return !pCursor->cbLeft || RT_FAILURE(pCursor->rc);
1578}
1579
1580
1581/**
1582 * Initialize a section reader cursor.
1583 *
1584 * @returns IPRT status code.
1585 * @param pCursor The cursor.
1586 * @param pThis The dwarf module.
1587 * @param enmSect The name of the section to read.
1588 */
1589static int rtDwarfCursor_Init(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis, krtDbgModDwarfSect enmSect)
1590{
1591 int rc = rtDbgModDwarfLoadSection(pThis, enmSect);
1592 if (RT_FAILURE(rc))
1593 return rc;
1594
1595 pCursor->enmSect = enmSect;
1596 pCursor->pbStart = (uint8_t const *)pThis->aSections[enmSect].pv;
1597 pCursor->pb = pCursor->pbStart;
1598 pCursor->cbLeft = pThis->aSections[enmSect].cb;
1599 pCursor->cbUnitLeft = pCursor->cbLeft;
1600 pCursor->pDwarfMod = pThis;
1601 pCursor->f64bitDwarf = false;
1602 /** @todo ask the image about the endian used as well as the address
1603 * width. */
1604 pCursor->fNativEndian = true;
1605 pCursor->cbNativeAddr = 4;
1606 pCursor->rc = VINF_SUCCESS;
1607
1608 return VINF_SUCCESS;
1609}
1610
1611
1612/**
1613 * Initialize a section reader cursor with an offset.
1614 *
1615 * @returns IPRT status code.
1616 * @param pCursor The cursor.
1617 * @param pThis The dwarf module.
1618 * @param enmSect The name of the section to read.
1619 * @param offSect The offset into the section.
1620 */
1621static int rtDwarfCursor_InitWithOffset(PRTDWARFCURSOR pCursor, PRTDBGMODDWARF pThis,
1622 krtDbgModDwarfSect enmSect, uint32_t offSect)
1623{
1624 if (offSect > pThis->aSections[enmSect].cb)
1625 {
1626 Log(("rtDwarfCursor_InitWithOffset: offSect=%#x cb=%#x enmSect=%d\n", offSect, pThis->aSections[enmSect].cb, enmSect));
1627 return VERR_DWARF_BAD_POS;
1628 }
1629
1630 int rc = rtDwarfCursor_Init(pCursor, pThis, enmSect);
1631 if (RT_SUCCESS(rc))
1632 {
1633 pCursor->pbStart += offSect;
1634 pCursor->pb += offSect;
1635 pCursor->cbLeft -= offSect;
1636 pCursor->cbUnitLeft -= offSect;
1637 }
1638
1639 return rc;
1640}
1641
1642
1643/**
1644 * Deletes a section reader initialized by rtDwarfCursor_Init.
1645 *
1646 * @returns @a rcOther or RTDWARCURSOR::rc.
1647 * @param pCursor The section reader.
1648 * @param rcOther Other error code to be returned if it indicates
1649 * error or if the cursor status is OK.
1650 */
1651static int rtDwarfCursor_Delete(PRTDWARFCURSOR pCursor, int rcOther)
1652{
1653 /* ... and a drop of poison. */
1654 pCursor->pb = NULL;
1655 pCursor->cbLeft = ~(size_t)0;
1656 pCursor->cbUnitLeft = ~(size_t)0;
1657 pCursor->pDwarfMod = NULL;
1658 if (RT_FAILURE(pCursor->rc) && RT_SUCCESS(rcOther))
1659 rcOther = pCursor->rc;
1660 pCursor->rc = VERR_INTERNAL_ERROR_4;
1661 return rcOther;
1662}
1663
1664
1665/*
1666 *
1667 * DWARF Line Numbers.
1668 * DWARF Line Numbers.
1669 * DWARF Line Numbers.
1670 *
1671 */
1672
1673
1674/**
1675 * Defines a file name.
1676 *
1677 * @returns IPRT status code.
1678 * @param pLnState The line number program state.
1679 * @param pszFilename The name of the file.
1680 * @param idxInc The include path index.
1681 */
1682static int rtDwarfLine_DefineFileName(PRTDWARFLINESTATE pLnState, const char *pszFilename, uint64_t idxInc)
1683{
1684 /*
1685 * Resize the array if necessary.
1686 */
1687 uint32_t iFileName = pLnState->cFileNames;
1688 if ((iFileName % 2) == 0)
1689 {
1690 void *pv = RTMemRealloc(pLnState->papszFileNames, sizeof(pLnState->papszFileNames[0]) * (iFileName + 2));
1691 if (!pv)
1692 return VERR_NO_MEMORY;
1693 pLnState->papszFileNames = (char **)pv;
1694 }
1695
1696 /*
1697 * Add the file name.
1698 */
1699 if ( pszFilename[0] == '/'
1700 || pszFilename[0] == '\\'
1701 || (RT_C_IS_ALPHA(pszFilename[0]) && pszFilename[1] == ':') )
1702 pLnState->papszFileNames[iFileName] = RTStrDup(pszFilename);
1703 else if (idxInc < pLnState->cIncPaths)
1704 pLnState->papszFileNames[iFileName] = RTPathJoinA(pLnState->papszIncPaths[idxInc], pszFilename);
1705 else
1706 return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
1707 if (!pLnState->papszFileNames[iFileName])
1708 return VERR_NO_STR_MEMORY;
1709 pLnState->cFileNames = iFileName + 1;
1710
1711 /*
1712 * Sanitize the name.
1713 */
1714 int rc = rtDbgModDwarfStringToUtf8(pLnState->pDwarfMod, &pLnState->papszFileNames[iFileName]);
1715 Log((" File #%02u = '%s'\n", iFileName, pLnState->papszFileNames[iFileName]));
1716 return rc;
1717}
1718
1719
1720/**
1721 * Adds a line to the table and resets parts of the state (DW_LNS_copy).
1722 *
1723 * @returns IPRT status code
1724 * @param pLnState The line number program state.
1725 * @param offOpCode The opcode offset (for logging
1726 * purposes).
1727 */
1728static int rtDwarfLine_AddLine(PRTDWARFLINESTATE pLnState, uint32_t offOpCode)
1729{
1730 const char *pszFile = pLnState->Regs.iFile < pLnState->cFileNames
1731 ? pLnState->papszFileNames[pLnState->Regs.iFile]
1732 : "<bad file name index>";
1733 RTDBGSEGIDX iSeg;
1734 RTUINTPTR offSeg;
1735 int rc = rtDbgModDwarfLinkAddressToSegOffset(pLnState->pDwarfMod, pLnState->Regs.uAddress, &iSeg, &offSeg);
1736 if (RT_SUCCESS(rc))
1737 {
1738 Log2(("rtDwarfLine_AddLine: %x:%08llx (%#llx) %s(%d) [offOpCode=%08x]\n", iSeg, offSeg, pLnState->Regs.uAddress, pszFile, pLnState->Regs.uLine, offOpCode));
1739 rc = RTDbgModLineAdd(pLnState->pDwarfMod->hCnt, pszFile, pLnState->Regs.uLine, iSeg, offSeg, NULL);
1740
1741 /* Ignore address conflicts for now. */
1742 if (rc == VERR_DBG_ADDRESS_CONFLICT)
1743 rc = VINF_SUCCESS;
1744 }
1745
1746 pLnState->Regs.fBasicBlock = false;
1747 pLnState->Regs.fPrologueEnd = false;
1748 pLnState->Regs.fEpilogueBegin = false;
1749 pLnState->Regs.uDiscriminator = 0;
1750 return rc;
1751}
1752
1753
1754/**
1755 * Reset the program to the start-of-sequence state.
1756 *
1757 * @param pLnState The line number program state.
1758 */
1759static void rtDwarfLine_ResetState(PRTDWARFLINESTATE pLnState)
1760{
1761 pLnState->Regs.uAddress = 0;
1762 pLnState->Regs.idxOp = 0;
1763 pLnState->Regs.iFile = 1;
1764 pLnState->Regs.uLine = 1;
1765 pLnState->Regs.uColumn = 0;
1766 pLnState->Regs.fIsStatement = RT_BOOL(pLnState->Hdr.u8DefIsStmt);
1767 pLnState->Regs.fBasicBlock = false;
1768 pLnState->Regs.fEndSequence = false;
1769 pLnState->Regs.fPrologueEnd = false;
1770 pLnState->Regs.fEpilogueBegin = false;
1771 pLnState->Regs.uIsa = 0;
1772 pLnState->Regs.uDiscriminator = 0;
1773}
1774
1775
1776/**
1777 * Runs the line number program.
1778 *
1779 * @returns IPRT status code.
1780 * @param pLnState The line number program state.
1781 * @param pCursor The cursor.
1782 */
1783static int rtDwarfLine_RunProgram(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
1784{
1785 LogFlow(("rtDwarfLine_RunProgram: cbUnitLeft=%zu\n", pCursor->cbUnitLeft));
1786
1787 int rc = VINF_SUCCESS;
1788 rtDwarfLine_ResetState(pLnState);
1789
1790 while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
1791 {
1792#ifdef LOG_ENABLED
1793 uint32_t const offOpCode = rtDwarfCursor_CalcSectOffsetU32(pCursor);
1794#else
1795 uint32_t const offOpCode = 0;
1796#endif
1797 uint8_t bOpCode = rtDwarfCursor_GetUByte(pCursor, DW_LNS_extended);
1798 if (bOpCode >= pLnState->Hdr.u8OpcodeBase)
1799 {
1800 /*
1801 * Special opcode.
1802 */
1803 uint8_t const bLogOpCode = bOpCode; NOREF(bLogOpCode);
1804 bOpCode -= pLnState->Hdr.u8OpcodeBase;
1805
1806 int32_t const cLineDelta = bOpCode % pLnState->Hdr.u8LineRange + (int32_t)pLnState->Hdr.s8LineBase;
1807 bOpCode /= pLnState->Hdr.u8LineRange;
1808
1809 uint64_t uTmp = bOpCode + pLnState->Regs.idxOp + bOpCode;
1810 uint64_t const cAddressDelta = uTmp / pLnState->Hdr.cMaxOpsPerInstr * pLnState->Hdr.cbMinInstr;
1811 uint64_t const cOpIndexDelta = uTmp % pLnState->Hdr.cMaxOpsPerInstr;
1812
1813 pLnState->Regs.uLine += cLineDelta;
1814 pLnState->Regs.uAddress += cAddressDelta;
1815 pLnState->Regs.idxOp += cOpIndexDelta;
1816 Log2(("%08x: DW Special Opcode %#04x: uLine + %d => %u; uAddress + %#llx => %#llx; idxOp + %#llx => %#llx\n",
1817 offOpCode, bLogOpCode, cLineDelta, pLnState->Regs.uLine, cAddressDelta, pLnState->Regs.uAddress,
1818 cOpIndexDelta, pLnState->Regs.idxOp));
1819
1820 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
1821 }
1822 else
1823 {
1824 switch (bOpCode)
1825 {
1826 /*
1827 * Standard opcode.
1828 */
1829 case DW_LNS_copy:
1830 Log2(("%08x: DW_LNS_copy\n", offOpCode));
1831 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
1832 break;
1833
1834 case DW_LNS_advance_pc:
1835 {
1836 uint64_t u64Adv = rtDwarfCursor_GetULeb128(pCursor, 0);
1837 pLnState->Regs.uAddress += (pLnState->Regs.idxOp + u64Adv) / pLnState->Hdr.cMaxOpsPerInstr
1838 * pLnState->Hdr.cbMinInstr;
1839 pLnState->Regs.idxOp += (pLnState->Regs.idxOp + u64Adv) % pLnState->Hdr.cMaxOpsPerInstr;
1840 Log2(("%08x: DW_LNS_advance_pc: u64Adv=%#llx (%lld) )\n", offOpCode, u64Adv, u64Adv));
1841 break;
1842 }
1843
1844 case DW_LNS_advance_line:
1845 {
1846 int32_t cLineDelta = rtDwarfCursor_GetSLeb128AsS32(pCursor, 0);
1847 pLnState->Regs.uLine += cLineDelta;
1848 Log2(("%08x: DW_LNS_advance_line: uLine + %d => %u\n", offOpCode, cLineDelta, pLnState->Regs.uLine));
1849 break;
1850 }
1851
1852 case DW_LNS_set_file:
1853 pLnState->Regs.iFile = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1854 Log2(("%08x: DW_LNS_set_file: iFile=%u\n", offOpCode, pLnState->Regs.iFile));
1855 break;
1856
1857 case DW_LNS_set_column:
1858 pLnState->Regs.uColumn = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1859 Log2(("%08x: DW_LNS_set_column\n", offOpCode));
1860 break;
1861
1862 case DW_LNS_negate_stmt:
1863 pLnState->Regs.fIsStatement = !pLnState->Regs.fIsStatement;
1864 Log2(("%08x: DW_LNS_negate_stmt\n", offOpCode));
1865 break;
1866
1867 case DW_LNS_set_basic_block:
1868 pLnState->Regs.fBasicBlock = true;
1869 Log2(("%08x: DW_LNS_set_basic_block\n", offOpCode));
1870 break;
1871
1872 case DW_LNS_const_add_pc:
1873 pLnState->Regs.uAddress += (pLnState->Regs.idxOp + 255) / pLnState->Hdr.cMaxOpsPerInstr
1874 * pLnState->Hdr.cbMinInstr;
1875 pLnState->Regs.idxOp += (pLnState->Regs.idxOp + 255) % pLnState->Hdr.cMaxOpsPerInstr;
1876 Log2(("%08x: DW_LNS_const_add_pc\n", offOpCode));
1877 break;
1878
1879 case DW_LNS_fixed_advance_pc:
1880 pLnState->Regs.uAddress += rtDwarfCursor_GetUHalf(pCursor, 0);
1881 pLnState->Regs.idxOp = 0;
1882 Log2(("%08x: DW_LNS_fixed_advance_pc\n", offOpCode));
1883 break;
1884
1885 case DW_LNS_set_prologue_end:
1886 pLnState->Regs.fPrologueEnd = true;
1887 Log2(("%08x: DW_LNS_set_prologue_end\n", offOpCode));
1888 break;
1889
1890 case DW_LNS_set_epilogue_begin:
1891 pLnState->Regs.fEpilogueBegin = true;
1892 Log2(("%08x: DW_LNS_set_epilogue_begin\n", offOpCode));
1893 break;
1894
1895 case DW_LNS_set_isa:
1896 pLnState->Regs.uIsa = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
1897 Log2(("%08x: DW_LNS_set_isa %#x\n", offOpCode, pLnState->Regs.uIsa));
1898 break;
1899
1900 default:
1901 {
1902 unsigned cOpsToSkip = pLnState->Hdr.pacStdOperands[bOpCode - 1];
1903 Log(("rtDwarfLine_RunProgram: Unknown standard opcode %#x, %#x operands, at %08x.\n", bOpCode, cOpsToSkip, offOpCode));
1904 while (cOpsToSkip-- > 0)
1905 rc = rtDwarfCursor_SkipLeb128(pCursor);
1906 break;
1907 }
1908
1909 /*
1910 * Extended opcode.
1911 */
1912 case DW_LNS_extended:
1913 {
1914 /* The instruction has a length prefix. */
1915 uint64_t cbInstr = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
1916 if (RT_FAILURE(pCursor->rc))
1917 return pCursor->rc;
1918 if (cbInstr > pCursor->cbUnitLeft)
1919 return VERR_DWARF_BAD_LNE;
1920 uint8_t const * const pbEndOfInstr = rtDwarfCursor_CalcPos(pCursor, cbInstr);
1921
1922 /* Get the opcode and deal with it if we know it. */
1923 bOpCode = rtDwarfCursor_GetUByte(pCursor, 0);
1924 switch (bOpCode)
1925 {
1926 case DW_LNE_end_sequence:
1927#if 0 /* No need for this, I think. */
1928 pLnState->Regs.fEndSequence = true;
1929 rc = rtDwarfLine_AddLine(pLnState, offOpCode);
1930#endif
1931 rtDwarfLine_ResetState(pLnState);
1932 Log2(("%08x: DW_LNE_end_sequence\n", offOpCode));
1933 break;
1934
1935 case DW_LNE_set_address:
1936 pLnState->Regs.uAddress = rtDwarfCursor_GetVarSizedU(pCursor, cbInstr - 1, UINT64_MAX);
1937 pLnState->Regs.idxOp = 0;
1938 Log2(("%08x: DW_LNE_set_address: %#llx\n", offOpCode, pLnState->Regs.uAddress));
1939 break;
1940
1941 case DW_LNE_define_file:
1942 {
1943 const char *pszFilename = rtDwarfCursor_GetSZ(pCursor, NULL);
1944 uint32_t idxInc = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
1945 rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
1946 rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
1947 Log2(("%08x: DW_LNE_define_file: {%d}/%s\n", offOpCode, idxInc, pszFilename));
1948
1949 rc = rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
1950 if (RT_SUCCESS(rc))
1951 rc = rtDwarfLine_DefineFileName(pLnState, pszFilename, idxInc);
1952 }
1953
1954 /*
1955 * Note! Was defined in DWARF 4. But... Watcom used it
1956 * for setting the segment in DWARF 2, creating
1957 * an incompatibility with the newer standard.
1958 */
1959 case DW_LNE_set_descriminator:
1960 if (pLnState->Hdr.uVer != 2)
1961 {
1962 Assert(pLnState->Hdr.uVer >= 4);
1963 pLnState->Regs.uDiscriminator = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
1964 Log2(("%08x: DW_LNE_set_descriminator: %u\n", offOpCode, pLnState->Regs.uDiscriminator));
1965 }
1966 else
1967 {
1968 uint64_t uSeg = rtDwarfCursor_GetVarSizedU(pCursor, cbInstr - 1, UINT64_MAX);
1969 Log2(("%08x: DW_LNE_set_segment: %ll#x - Watcom Extension\n", offOpCode, uSeg));
1970 NOREF(uSeg);
1971 /** @todo make use of this? */
1972 }
1973 break;
1974
1975 default:
1976 Log(("rtDwarfLine_RunProgram: Unknown extended opcode %#x, length %#x at %08x\n", bOpCode, cbInstr, offOpCode));
1977 break;
1978 }
1979
1980 /* Advance the cursor to the end of the instruction . */
1981 rtDwarfCursor_AdvanceToPos(pCursor, pbEndOfInstr);
1982 break;
1983 }
1984 }
1985 }
1986
1987 /*
1988 * Check the status before looping.
1989 */
1990 if (RT_FAILURE(rc))
1991 return rc;
1992 if (RT_FAILURE(pCursor->rc))
1993 return pCursor->rc;
1994 }
1995 return rc;
1996}
1997
1998
1999/**
2000 * Reads the include directories for a line number unit.
2001 *
2002 * @returns IPRT status code
2003 * @param pLnState The line number program state.
2004 * @param pCursor The cursor.
2005 */
2006static int rtDwarfLine_ReadFileNames(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
2007{
2008 int rc = rtDwarfLine_DefineFileName(pLnState, "/<bad-zero-file-name-entry>", 0);
2009 if (RT_FAILURE(rc))
2010 return rc;
2011
2012 for (;;)
2013 {
2014 const char *psz = rtDwarfCursor_GetSZ(pCursor, NULL);
2015 if (!*psz)
2016 break;
2017
2018 uint64_t idxInc = rtDwarfCursor_GetULeb128(pCursor, UINT64_MAX);
2019 rtDwarfCursor_SkipLeb128(pCursor); /* st_mtime */
2020 rtDwarfCursor_SkipLeb128(pCursor); /* st_size */
2021
2022 rc = rtDwarfLine_DefineFileName(pLnState, psz, idxInc);
2023 if (RT_FAILURE(rc))
2024 return rc;
2025 }
2026 return pCursor->rc;
2027}
2028
2029
2030/**
2031 * Reads the include directories for a line number unit.
2032 *
2033 * @returns IPRT status code
2034 * @param pLnState The line number program state.
2035 * @param pCursor The cursor.
2036 */
2037static int rtDwarfLine_ReadIncludePaths(PRTDWARFLINESTATE pLnState, PRTDWARFCURSOR pCursor)
2038{
2039 const char *psz = ""; /* The zeroth is the unit dir. */
2040 for (;;)
2041 {
2042 if ((pLnState->cIncPaths % 2) == 0)
2043 {
2044 void *pv = RTMemRealloc(pLnState->papszIncPaths, sizeof(pLnState->papszIncPaths[0]) * (pLnState->cIncPaths + 2));
2045 if (!pv)
2046 return VERR_NO_MEMORY;
2047 pLnState->papszIncPaths = (const char **)pv;
2048 }
2049 Log((" Path #%02u = '%s'\n", pLnState->cIncPaths, psz));
2050 pLnState->papszIncPaths[pLnState->cIncPaths] = psz;
2051 pLnState->cIncPaths++;
2052
2053 psz = rtDwarfCursor_GetSZ(pCursor, NULL);
2054 if (!*psz)
2055 break;
2056 }
2057
2058 return pCursor->rc;
2059}
2060
2061
2062/**
2063 * Explodes the line number table for a compilation unit.
2064 *
2065 * @returns IPRT status code
2066 * @param pThis The DWARF instance.
2067 * @param pCursor The cursor to read the line number information
2068 * via.
2069 */
2070static int rtDwarfLine_ExplodeUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor)
2071{
2072 RTDWARFLINESTATE LnState;
2073 RT_ZERO(LnState);
2074 LnState.pDwarfMod = pThis;
2075
2076 /*
2077 * Parse the header.
2078 */
2079 rtDwarfCursor_GetInitalLength(pCursor);
2080 LnState.Hdr.uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
2081 if ( LnState.Hdr.uVer < 2
2082 || LnState.Hdr.uVer > 4)
2083 return rtDwarfCursor_SkipUnit(pCursor);
2084
2085 LnState.Hdr.offFirstOpcode = rtDwarfCursor_GetUOff(pCursor, 0);
2086 uint8_t const * const pbFirstOpcode = rtDwarfCursor_CalcPos(pCursor, LnState.Hdr.offFirstOpcode);
2087
2088 LnState.Hdr.cbMinInstr = rtDwarfCursor_GetUByte(pCursor, 0);
2089 if (LnState.Hdr.uVer >= 4)
2090 LnState.Hdr.cMaxOpsPerInstr = rtDwarfCursor_GetUByte(pCursor, 0);
2091 else
2092 LnState.Hdr.cMaxOpsPerInstr = 1;
2093 LnState.Hdr.u8DefIsStmt = rtDwarfCursor_GetUByte(pCursor, 0);
2094 LnState.Hdr.s8LineBase = rtDwarfCursor_GetSByte(pCursor, 0);
2095 LnState.Hdr.u8LineRange = rtDwarfCursor_GetUByte(pCursor, 0);
2096 LnState.Hdr.u8OpcodeBase = rtDwarfCursor_GetUByte(pCursor, 0);
2097
2098 if ( !LnState.Hdr.u8OpcodeBase
2099 || !LnState.Hdr.cMaxOpsPerInstr
2100 || !LnState.Hdr.u8LineRange
2101 || LnState.Hdr.u8DefIsStmt > 1)
2102 return VERR_DWARF_BAD_LINE_NUMBER_HEADER;
2103 Log2(("DWARF Line number header:\n"
2104 " uVer %d\n"
2105 " offFirstOpcode %#llx\n"
2106 " cbMinInstr %u\n"
2107 " cMaxOpsPerInstr %u\n"
2108 " u8DefIsStmt %u\n"
2109 " s8LineBase %d\n"
2110 " u8LineRange %u\n"
2111 " u8OpcodeBase %u\n",
2112 LnState.Hdr.uVer, LnState.Hdr.offFirstOpcode, LnState.Hdr.cbMinInstr, LnState.Hdr.cMaxOpsPerInstr,
2113 LnState.Hdr.u8DefIsStmt, LnState.Hdr.s8LineBase, LnState.Hdr.u8LineRange, LnState.Hdr.u8OpcodeBase));
2114
2115 LnState.Hdr.pacStdOperands = pCursor->pb;
2116 for (uint8_t iStdOpcode = 1; iStdOpcode < LnState.Hdr.u8OpcodeBase; iStdOpcode++)
2117 rtDwarfCursor_GetUByte(pCursor, 0);
2118
2119 int rc = pCursor->rc;
2120 if (RT_SUCCESS(rc))
2121 rc = rtDwarfLine_ReadIncludePaths(&LnState, pCursor);
2122 if (RT_SUCCESS(rc))
2123 rc = rtDwarfLine_ReadFileNames(&LnState, pCursor);
2124
2125 /*
2126 * Run the program....
2127 */
2128 if (RT_SUCCESS(rc))
2129 rc = rtDwarfCursor_AdvanceToPos(pCursor, pbFirstOpcode);
2130 if (RT_SUCCESS(rc))
2131 rc = rtDwarfLine_RunProgram(&LnState, pCursor);
2132
2133 /*
2134 * Clean up.
2135 */
2136 size_t i = LnState.cFileNames;
2137 while (i-- > 0)
2138 RTStrFree(LnState.papszFileNames[i]);
2139 RTMemFree(LnState.papszFileNames);
2140 RTMemFree(LnState.papszIncPaths);
2141
2142 Assert(rtDwarfCursor_IsAtEndOfUnit(pCursor) || RT_FAILURE(rc));
2143 return rc;
2144}
2145
2146
2147/**
2148 * Explodes the line number table.
2149 *
2150 * The line numbers are insered into the debug info container.
2151 *
2152 * @returns IPRT status code
2153 * @param pThis The DWARF instance.
2154 */
2155static int rtDwarfLine_ExplodeAll(PRTDBGMODDWARF pThis)
2156{
2157 if (!pThis->aSections[krtDbgModDwarfSect_line].fPresent)
2158 return VINF_SUCCESS;
2159
2160 RTDWARFCURSOR Cursor;
2161 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_line);
2162 if (RT_FAILURE(rc))
2163 return rc;
2164
2165 while ( !rtDwarfCursor_IsAtEnd(&Cursor)
2166 && RT_SUCCESS(rc))
2167 rc = rtDwarfLine_ExplodeUnit(pThis, &Cursor);
2168
2169 return rtDwarfCursor_Delete(&Cursor, rc);
2170}
2171
2172
2173/*
2174 *
2175 * DWARF Abbreviations.
2176 * DWARF Abbreviations.
2177 * DWARF Abbreviations.
2178 *
2179 */
2180
2181/**
2182 * Deals with a cache miss in rtDwarfAbbrev_Lookup.
2183 *
2184 * @returns Pointer to abbreviation cache entry (read only). May be rendered
2185 * invalid by subsequent calls to this function.
2186 * @param pThis The DWARF instance.
2187 * @param uCode The abbreviation code to lookup.
2188 */
2189static PCRTDWARFABBREV rtDwarfAbbrev_LookupMiss(PRTDBGMODDWARF pThis, uint32_t uCode)
2190{
2191 /*
2192 * There is no entry with code zero.
2193 */
2194 if (!uCode)
2195 return NULL;
2196
2197 /*
2198 * Resize the cache array if the code is considered cachable.
2199 */
2200 bool fFillCache = true;
2201 if (pThis->cCachedAbbrevsAlloced < uCode)
2202 {
2203 if (uCode > _64K)
2204 fFillCache = false;
2205 else
2206 {
2207 uint32_t cNew = RT_ALIGN(uCode, 64);
2208 void *pv = RTMemRealloc(pThis->paCachedAbbrevs, sizeof(pThis->paCachedAbbrevs[0]) * cNew);
2209 if (!pv)
2210 fFillCache = false;
2211 else
2212 {
2213 pThis->cCachedAbbrevsAlloced = cNew;
2214 pThis->paCachedAbbrevs = (PRTDWARFABBREV)pv;
2215 }
2216 }
2217 }
2218
2219 /*
2220 * Walk the abbreviations till we find the desired code.
2221 */
2222 RTDWARFCURSOR Cursor;
2223 int rc = rtDwarfCursor_InitWithOffset(&Cursor, pThis, krtDbgModDwarfSect_abbrev, pThis->offCachedAbbrev);
2224 if (RT_FAILURE(rc))
2225 return NULL;
2226
2227 PRTDWARFABBREV pRet = NULL;
2228 if (fFillCache)
2229 {
2230 /*
2231 * Search for the entry and fill the cache while doing so.
2232 */
2233 for (;;)
2234 {
2235 /* Read the 'header'. */
2236 uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2237 uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2238 uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
2239 if (RT_FAILURE(Cursor.rc))
2240 break;
2241 if ( uCurTag > 0xffff
2242 || uChildren > 1)
2243 {
2244 Cursor.rc = VERR_DWARF_BAD_ABBREV;
2245 break;
2246 }
2247
2248 /* Cache it? */
2249 if (uCurCode <= pThis->cCachedAbbrevsAlloced)
2250 {
2251 PRTDWARFABBREV pEntry = &pThis->paCachedAbbrevs[uCurCode - 1];
2252 while (pThis->cCachedAbbrevs < uCurCode)
2253 {
2254 pThis->paCachedAbbrevs[pThis->cCachedAbbrevs].fFilled = false;
2255 pThis->cCachedAbbrevs++;
2256 }
2257
2258 pEntry->fFilled = true;
2259 pEntry->fChildren = RT_BOOL(uChildren);
2260 pEntry->uTag = uCurTag;
2261 pEntry->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
2262
2263 if (uCurCode == uCode)
2264 {
2265 pRet = pEntry;
2266 if (uCurCode == pThis->cCachedAbbrevsAlloced)
2267 break;
2268 }
2269 }
2270
2271 /* Skip the specification. */
2272 uint32_t uAttr, uForm;
2273 do
2274 {
2275 uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2276 uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2277 } while (uAttr != 0);
2278 if (RT_FAILURE(Cursor.rc))
2279 break;
2280
2281 /* Done? (Maximize cache filling.) */
2282 if ( pRet != NULL
2283 && uCurCode >= pThis->cCachedAbbrevsAlloced)
2284 break;
2285 }
2286 }
2287 else
2288 {
2289 /*
2290 * Search for the entry with the desired code, no cache filling.
2291 */
2292 for (;;)
2293 {
2294 /* Read the 'header'. */
2295 uint32_t const uCurCode = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2296 uint32_t const uCurTag = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2297 uint8_t const uChildren = rtDwarfCursor_GetU8(&Cursor, 0);
2298 if (RT_FAILURE(Cursor.rc))
2299 break;
2300 if ( uCurTag > 0xffff
2301 || uChildren > 1)
2302 {
2303 Cursor.rc = VERR_DWARF_BAD_ABBREV;
2304 break;
2305 }
2306
2307 /* Do we have a match? */
2308 if (uCurCode == uCode)
2309 {
2310 pRet = &pThis->LookupAbbrev;
2311 pRet->fFilled = true;
2312 pRet->fChildren = RT_BOOL(uChildren);
2313 pRet->uTag = uCurTag;
2314 pRet->offSpec = rtDwarfCursor_CalcSectOffsetU32(&Cursor);
2315 break;
2316 }
2317
2318 /* Skip the specification. */
2319 uint32_t uAttr, uForm;
2320 do
2321 {
2322 uAttr = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2323 uForm = rtDwarfCursor_GetULeb128AsU32(&Cursor, 0);
2324 } while (uAttr != 0);
2325 if (RT_FAILURE(Cursor.rc))
2326 break;
2327 }
2328 }
2329
2330 rtDwarfCursor_Delete(&Cursor, VINF_SUCCESS);
2331 return pRet;
2332}
2333
2334
2335/**
2336 * Looks up an abbreviation.
2337 *
2338 * @returns Pointer to abbreviation cache entry (read only). May be rendered
2339 * invalid by subsequent calls to this function.
2340 * @param pThis The DWARF instance.
2341 * @param uCode The abbreviation code to lookup.
2342 */
2343static PCRTDWARFABBREV rtDwarfAbbrev_Lookup(PRTDBGMODDWARF pThis, uint32_t uCode)
2344{
2345 if ( uCode - 1 >= pThis->cCachedAbbrevs
2346 || !pThis->paCachedAbbrevs[uCode - 1].fFilled)
2347 return rtDwarfAbbrev_LookupMiss(pThis, uCode);
2348 return &pThis->paCachedAbbrevs[uCode - 1];
2349}
2350
2351
2352/**
2353 * Sets the abbreviation offset of the current unit.
2354 *
2355 * This will flush the cached abbreviation entries if the offset differs from
2356 * the previous unit.
2357 *
2358 * @param pThis The DWARF instance.
2359 * @param offAbbrev The offset into the abbreviation section.
2360 */
2361static void rtDwarfAbbrev_SetUnitOffset(PRTDBGMODDWARF pThis, uint32_t offAbbrev)
2362{
2363 if (pThis->offCachedAbbrev != offAbbrev)
2364 {
2365 pThis->offCachedAbbrev = offAbbrev;
2366 pThis->cCachedAbbrevs = 0;
2367 }
2368}
2369
2370
2371
2372/*
2373 *
2374 * DIE Attribute Parsers.
2375 * DIE Attribute Parsers.
2376 * DIE Attribute Parsers.
2377 *
2378 */
2379
2380/**
2381 * Gets the compilation unit a DIE belongs to.
2382 *
2383 * @returns The compilation unit DIE.
2384 * @param pDie Some DIE in the unit.
2385 */
2386static PRTDWARFDIECOMPILEUNIT rtDwarfDie_GetCompileUnit(PRTDWARFDIE pDie)
2387{
2388 while (pDie->pParent)
2389 pDie = pDie->pParent;
2390 AssertReturn( pDie->uTag == DW_TAG_compile_unit
2391 || pDie->uTag == DW_TAG_partial_unit,
2392 NULL);
2393 return (PRTDWARFDIECOMPILEUNIT)pDie;
2394}
2395
2396
2397/**
2398 * Resolves a string section (debug_str) reference.
2399 *
2400 * @returns Pointer to the string (inside the string section).
2401 * @param pThis The DWARF instance.
2402 * @param pCursor The cursor.
2403 * @param pszErrValue What to return on failure (@a
2404 * pCursor->rc is set).
2405 */
2406static const char *rtDwarfDecode_GetStrp(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor, const char *pszErrValue)
2407{
2408 uint64_t offDebugStr = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
2409 if (RT_FAILURE(pCursor->rc))
2410 return pszErrValue;
2411
2412 if (offDebugStr >= pThis->aSections[krtDbgModDwarfSect_str].cb)
2413 {
2414 /* Ugly: Exploit the cursor status field for reporting errors. */
2415 pCursor->rc = VERR_DWARF_BAD_INFO;
2416 return pszErrValue;
2417 }
2418
2419 if (!pThis->aSections[krtDbgModDwarfSect_str].pv)
2420 {
2421 int rc = rtDbgModDwarfLoadSection(pThis, krtDbgModDwarfSect_str);
2422 if (RT_FAILURE(rc))
2423 {
2424 /* Ugly: Exploit the cursor status field for reporting errors. */
2425 pCursor->rc = rc;
2426 return pszErrValue;
2427 }
2428 }
2429
2430 return (const char *)pThis->aSections[krtDbgModDwarfSect_str].pv + (size_t)offDebugStr;
2431}
2432
2433
2434/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2435static DECLCALLBACK(int) rtDwarfDecode_Address(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2436 uint32_t uForm, PRTDWARFCURSOR pCursor)
2437{
2438 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDR), VERR_INTERNAL_ERROR_3);
2439
2440 uint64_t uAddr;
2441 switch (uForm)
2442 {
2443 case DW_FORM_addr: uAddr = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
2444 case DW_FORM_data1: uAddr = rtDwarfCursor_GetU8(pCursor, 0); break;
2445 case DW_FORM_data2: uAddr = rtDwarfCursor_GetU16(pCursor, 0); break;
2446 case DW_FORM_data4: uAddr = rtDwarfCursor_GetU32(pCursor, 0); break;
2447 case DW_FORM_data8: uAddr = rtDwarfCursor_GetU64(pCursor, 0); break;
2448 case DW_FORM_udata: uAddr = rtDwarfCursor_GetULeb128(pCursor, 0); break;
2449 default:
2450 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2451 }
2452 if (RT_FAILURE(pCursor->rc))
2453 return pCursor->rc;
2454
2455 PRTDWARFADDR pAddr = (PRTDWARFADDR)pbMember;
2456 pAddr->uAddress = uAddr;
2457
2458 return VINF_SUCCESS;
2459}
2460
2461
2462/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2463static DECLCALLBACK(int) rtDwarfDecode_Bool(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2464 uint32_t uForm, PRTDWARFCURSOR pCursor)
2465{
2466 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(bool), VERR_INTERNAL_ERROR_3);
2467
2468 bool *pfMember = (bool *)pbMember;
2469 switch (uForm)
2470 {
2471 case DW_FORM_flag:
2472 {
2473 uint8_t b = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
2474 if (b > 1)
2475 return RT_FAILURE(pCursor->rc) ? pCursor->rc : pCursor->rc = VERR_DWARF_BAD_INFO;
2476 *pfMember = RT_BOOL(b);
2477 break;
2478 }
2479
2480 case DW_FORM_flag_present:
2481 *pfMember = true;
2482 break;
2483
2484 default:
2485 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2486 }
2487
2488 return VINF_SUCCESS;
2489}
2490
2491
2492/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2493static DECLCALLBACK(int) rtDwarfDecode_LowHighPc(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2494 uint32_t uForm, PRTDWARFCURSOR pCursor)
2495{
2496 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDRRANGE), VERR_INTERNAL_ERROR_3);
2497 AssertReturn(pDesc->uAttr == DW_AT_low_pc || pDesc->uAttr == DW_AT_high_pc, VERR_INTERNAL_ERROR_3);
2498
2499 uint64_t uAddr;
2500 switch (uForm)
2501 {
2502 case DW_FORM_addr: uAddr = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
2503 case DW_FORM_data1: uAddr = rtDwarfCursor_GetU8(pCursor, 0); break;
2504 case DW_FORM_data2: uAddr = rtDwarfCursor_GetU16(pCursor, 0); break;
2505 case DW_FORM_data4: uAddr = rtDwarfCursor_GetU32(pCursor, 0); break;
2506 case DW_FORM_data8: uAddr = rtDwarfCursor_GetU64(pCursor, 0); break;
2507 case DW_FORM_udata: uAddr = rtDwarfCursor_GetULeb128(pCursor, 0); break;
2508 default:
2509 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2510 }
2511 if (RT_FAILURE(pCursor->rc))
2512 return pCursor->rc;
2513
2514 PRTDWARFADDRRANGE pRange = (PRTDWARFADDRRANGE)pbMember;
2515 if (pDesc->uAttr == DW_AT_low_pc)
2516 {
2517 if (pRange->fHaveLowAddress)
2518 {
2519 Log(("rtDwarfDecode_LowHighPc: Duplicate DW_AT_low_pc\n"));
2520 return pCursor->rc = VERR_DWARF_BAD_INFO;
2521 }
2522 pRange->fHaveLowAddress = true;
2523 pRange->uLowAddress = uAddr;
2524 }
2525 else
2526 {
2527 if (pRange->fHaveHighAddress)
2528 {
2529 Log(("rtDwarfDecode_LowHighPc: Duplicate DW_AT_high_pc\n"));
2530 return pCursor->rc = VERR_DWARF_BAD_INFO;
2531 }
2532 pRange->fHaveHighAddress = true;
2533 pRange->uHighAddress = uAddr;
2534 }
2535 pRange->cAttrs++;
2536
2537 return VINF_SUCCESS;
2538}
2539
2540
2541/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2542static DECLCALLBACK(int) rtDwarfDecode_Ranges(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2543 uint32_t uForm, PRTDWARFCURSOR pCursor)
2544{
2545 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFADDRRANGE), VERR_INTERNAL_ERROR_3);
2546 AssertReturn(pDesc->uAttr == DW_AT_low_pc || pDesc->uAttr == DW_AT_high_pc, VERR_INTERNAL_ERROR_3);
2547
2548 /* Decode it. */
2549 uint64_t off;
2550 switch (uForm)
2551 {
2552 case DW_FORM_addr: off = rtDwarfCursor_GetNativeUOff(pCursor, 0); break;
2553 case DW_FORM_data4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
2554 case DW_FORM_data8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
2555 default:
2556 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2557 }
2558 if (RT_FAILURE(pCursor->rc))
2559 return pCursor->rc;
2560
2561 /* Validate the offset and load the ranges. */
2562 PRTDBGMODDWARF pThis = pCursor->pDwarfMod;
2563 if (off >= pThis->aSections[krtDbgModDwarfSect_ranges].cb)
2564 {
2565 Log(("rtDwarfDecode_Ranges: bad ranges off=%#llx\n", off));
2566 return pCursor->rc = VERR_DWARF_BAD_POS;
2567 }
2568
2569 if (!pThis->aSections[krtDbgModDwarfSect_ranges].pv)
2570 {
2571 int rc = rtDbgModDwarfLoadSection(pThis, krtDbgModDwarfSect_ranges);
2572 if (RT_FAILURE(rc))
2573 return pCursor->rc = rc;
2574 }
2575
2576 /* Store the result. */
2577 PRTDWARFADDRRANGE pRange = (PRTDWARFADDRRANGE)pbMember;
2578 if (pRange->fHaveRanges)
2579 {
2580 Log(("rtDwarfDecode_Ranges: Duplicate DW_AT_ranges\n"));
2581 return pCursor->rc = VERR_DWARF_BAD_INFO;
2582 }
2583 pRange->fHaveRanges = true;
2584 pRange->cAttrs++;
2585 pRange->pbRanges = (uint8_t const *)pThis->aSections[krtDbgModDwarfSect_ranges].pv + (size_t)off;
2586
2587 return VINF_SUCCESS;
2588}
2589
2590
2591/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2592static DECLCALLBACK(int) rtDwarfDecode_Reference(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2593 uint32_t uForm, PRTDWARFCURSOR pCursor)
2594{
2595 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFREF), VERR_INTERNAL_ERROR_3);
2596
2597 /* Decode it. */
2598 uint64_t off;
2599 krtDwarfRef enmWrt = krtDwarfRef_InfoSection;
2600 switch (uForm)
2601 {
2602 case DW_FORM_ref1: off = rtDwarfCursor_GetU8(pCursor, 0); break;
2603 case DW_FORM_ref2: off = rtDwarfCursor_GetU16(pCursor, 0); break;
2604 case DW_FORM_ref4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
2605 case DW_FORM_ref8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
2606 case DW_FORM_ref_udata: off = rtDwarfCursor_GetULeb128(pCursor, 0); break;
2607
2608 case DW_FORM_ref_addr:
2609 enmWrt = krtDwarfRef_InfoSection;
2610 off = rtDwarfCursor_GetUOff(pCursor, 0);
2611 break;
2612
2613 case DW_FORM_ref_sig8:
2614 enmWrt = krtDwarfRef_TypeId64;
2615 off = rtDwarfCursor_GetU64(pCursor, 0);
2616 break;
2617
2618 default:
2619 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2620 }
2621 if (RT_FAILURE(pCursor->rc))
2622 return pCursor->rc;
2623
2624 /* Validate the offset and convert to debug_info relative offsets. */
2625 if (enmWrt == krtDwarfRef_InfoSection)
2626 {
2627 if (off >= pCursor->pDwarfMod->aSections[krtDbgModDwarfSect_info].cb)
2628 {
2629 Log(("rtDwarfDecode_Reference: bad info off=%#llx\n", off));
2630 return pCursor->rc = VERR_DWARF_BAD_POS;
2631 }
2632 }
2633 else if (enmWrt == krtDwarfRef_SameUnit)
2634 {
2635 PRTDWARFDIECOMPILEUNIT pUnit = rtDwarfDie_GetCompileUnit(pDie);
2636 if (off >= pUnit->cbUnit)
2637 {
2638 Log(("rtDwarfDecode_Reference: bad unit off=%#llx\n", off));
2639 return pCursor->rc = VERR_DWARF_BAD_POS;
2640 }
2641 off += pUnit->offUnit;
2642 enmWrt = krtDwarfRef_InfoSection;
2643 }
2644 /* else: not bother verifying/resolving the indirect type reference yet. */
2645
2646 /* Store it */
2647 PRTDWARFREF pRef = (PRTDWARFREF)pbMember;
2648 pRef->enmWrt = enmWrt;
2649 pRef->off = off;
2650
2651 return VINF_SUCCESS;
2652}
2653
2654
2655/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2656static DECLCALLBACK(int) rtDwarfDecode_SectOff(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2657 uint32_t uForm, PRTDWARFCURSOR pCursor)
2658{
2659 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(RTDWARFREF), VERR_INTERNAL_ERROR_3);
2660
2661 uint64_t off;
2662 switch (uForm)
2663 {
2664 case DW_FORM_data4: off = rtDwarfCursor_GetU32(pCursor, 0); break;
2665 case DW_FORM_data8: off = rtDwarfCursor_GetU64(pCursor, 0); break;
2666 case DW_FORM_sec_offset: off = rtDwarfCursor_GetUOff(pCursor, 0); break;
2667 default:
2668 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2669 }
2670 if (RT_FAILURE(pCursor->rc))
2671 return pCursor->rc;
2672
2673 krtDbgModDwarfSect enmSect;
2674 krtDwarfRef enmWrt;
2675 switch (pDesc->uAttr)
2676 {
2677 case DW_AT_stmt_list: enmSect = krtDbgModDwarfSect_line; enmWrt = krtDwarfRef_LineSection; break;
2678 case DW_AT_macro_info: enmSect = krtDbgModDwarfSect_loc; enmWrt = krtDwarfRef_LocSection; break;
2679 case DW_AT_ranges: enmSect = krtDbgModDwarfSect_ranges; enmWrt = krtDwarfRef_RangesSection; break;
2680 default: AssertMsgFailedReturn(("%u\n", pDesc->uAttr), VERR_INTERNAL_ERROR_4);
2681 }
2682 if (off >= pCursor->pDwarfMod->aSections[enmSect].cb)
2683 {
2684 Log(("rtDwarfDecode_SectOff: bad off=%#llx, attr %#x\n", off, pDesc->uAttr));
2685 return pCursor->rc = VERR_DWARF_BAD_POS;
2686 }
2687
2688 PRTDWARFREF pRef = (PRTDWARFREF)pbMember;
2689 pRef->enmWrt = enmWrt;
2690 pRef->off = off;
2691
2692 return VINF_SUCCESS;
2693}
2694
2695
2696/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2697static DECLCALLBACK(int) rtDwarfDecode_String(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2698 uint32_t uForm, PRTDWARFCURSOR pCursor)
2699{
2700 AssertReturn(ATTR_GET_SIZE(pDesc) == sizeof(const char *), VERR_INTERNAL_ERROR_3);
2701
2702 switch (uForm)
2703 {
2704 case DW_FORM_string:
2705 *(const char **)pbMember = rtDwarfCursor_GetSZ(pCursor, NULL);
2706 break;
2707
2708 case DW_FORM_strp:
2709 *(const char **)pbMember = rtDwarfDecode_GetStrp(pCursor->pDwarfMod, pCursor, NULL);
2710 break;
2711
2712 default:
2713 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2714 }
2715
2716 return pCursor->rc;
2717}
2718
2719
2720/** @callback_method_impl{FNRTDWARFATTRDECODER} */
2721static DECLCALLBACK(int) rtDwarfDecode_UnsignedInt(PRTDWARFDIE pDie, uint8_t *pbMember, PCRTDWARFATTRDESC pDesc,
2722 uint32_t uForm, PRTDWARFCURSOR pCursor)
2723{
2724 uint64_t u64Val;
2725 switch (uForm)
2726 {
2727 case DW_FORM_udata: u64Val = rtDwarfCursor_GetULeb128(pCursor, 0); break;
2728 case DW_FORM_data1: u64Val = rtDwarfCursor_GetU8(pCursor, 0); break;
2729 case DW_FORM_data2: u64Val = rtDwarfCursor_GetU16(pCursor, 0); break;
2730 case DW_FORM_data4: u64Val = rtDwarfCursor_GetU32(pCursor, 0); break;
2731 case DW_FORM_data8: u64Val = rtDwarfCursor_GetU64(pCursor, 0); break;
2732 default:
2733 AssertMsgFailedReturn(("%#x\n", uForm), VERR_DWARF_UNEXPECTED_FORM);
2734 }
2735 if (RT_FAILURE(pCursor->rc))
2736 return pCursor->rc;
2737
2738 switch (ATTR_GET_SIZE(pDesc))
2739 {
2740 case 1:
2741 *pbMember = (uint8_t)u64Val;
2742 if (*pbMember != u64Val)
2743 return VERR_OUT_OF_RANGE;
2744 break;
2745
2746 case 2:
2747 *(uint16_t *)pbMember = (uint16_t)u64Val;
2748 if (*(uint16_t *)pbMember != u64Val)
2749 return VERR_OUT_OF_RANGE;
2750 break;
2751
2752 case 4:
2753 *(uint32_t *)pbMember = (uint32_t)u64Val;
2754 if (*(uint32_t *)pbMember != u64Val)
2755 return VERR_OUT_OF_RANGE;
2756 break;
2757
2758 case 8:
2759 *(uint64_t *)pbMember = (uint64_t)u64Val;
2760 if (*(uint64_t *)pbMember != u64Val)
2761 return VERR_OUT_OF_RANGE;
2762 break;
2763
2764 default:
2765 AssertMsgFailedReturn(("%#x\n", ATTR_GET_SIZE(pDesc)), VERR_INTERNAL_ERROR_2);
2766 }
2767 return VINF_SUCCESS;
2768}
2769
2770
2771
2772/*
2773 *
2774 * DWARF debug_info parser
2775 * DWARF debug_info parser
2776 * DWARF debug_info parser
2777 *
2778 */
2779
2780
2781/**
2782 * Parse the attributes of a DIE.
2783 *
2784 * @returns IPRT status code.
2785 * @param pThis The DWARF instance.
2786 * @param pDie The internal DIE structure to fill.
2787 */
2788static int rtDwarfInfo_SnoopSymbols(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie)
2789{
2790 int rc = VINF_SUCCESS;
2791 switch (pDie->uTag)
2792 {
2793 case DW_TAG_subprogram:
2794 {
2795 PCRTDWARFDIESUBPROGRAM pSubProgram = (PCRTDWARFDIESUBPROGRAM)pDie;
2796 if (pSubProgram->PcRange.cAttrs)
2797 {
2798 if (pSubProgram->PcRange.fHaveRanges)
2799 Log5(("subprogram %s (%s) <implement ranges>\n", pSubProgram->pszName, pSubProgram->pszLinkageName));
2800 else
2801 {
2802 Log5(("subprogram %s (%s) %#llx-%#llx%s\n", pSubProgram->pszName, pSubProgram->pszLinkageName,
2803 pSubProgram->PcRange.uLowAddress, pSubProgram->PcRange.uHighAddress,
2804 pSubProgram->PcRange.cAttrs == 2 ? "" : " !bad!"));
2805 if ( pSubProgram->pszName
2806 && pSubProgram->PcRange.cAttrs == 2)
2807 {
2808 RTDBGSEGIDX iSeg;
2809 RTUINTPTR offSeg;
2810 rc = rtDbgModDwarfLinkAddressToSegOffset(pThis, pSubProgram->PcRange.uLowAddress,
2811 &iSeg, &offSeg);
2812 if (RT_SUCCESS(rc))
2813 rc = RTDbgModSymbolAdd(pThis->hCnt, pSubProgram->pszName, iSeg, offSeg,
2814 pSubProgram->PcRange.uHighAddress - pSubProgram->PcRange.uLowAddress,
2815 0 /*fFlags*/, NULL /*piOrdinal*/);
2816 else
2817 Log5(("rtDbgModDwarfLinkAddressToSegOffset failed: %Rrc\n", rc));
2818 }
2819 }
2820 }
2821 else
2822 Log5(("subprogram %s (%s) external\n", pSubProgram->pszName, pSubProgram->pszLinkageName));
2823 break;
2824 }
2825
2826 }
2827 return rc;
2828}
2829
2830
2831/**
2832 * Initializes the non-core fields of an internal DIE structure.
2833 *
2834 * @param pDie The DIE structure.
2835 * @param pDieDesc The DIE descriptor.
2836 */
2837static void rtDwarfInfo_InitDie(PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc)
2838{
2839 size_t i = pDieDesc->cAttributes;
2840 while (i-- > 0)
2841 {
2842 switch (pDieDesc->paAttributes[i].cbInit & ATTR_INIT_MASK)
2843 {
2844 case ATTR_INIT_ZERO:
2845 /* Nothing to do (RTMemAllocZ). */
2846 break;
2847
2848 case ATTR_INIT_FFFS:
2849 switch (pDieDesc->paAttributes[i].cbInit & ATTR_SIZE_MASK)
2850 {
2851 case 1:
2852 *(uint8_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT8_MAX;
2853 break;
2854 case 2:
2855 *(uint16_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT16_MAX;
2856 break;
2857 case 4:
2858 *(uint32_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT32_MAX;
2859 break;
2860 case 8:
2861 *(uint64_t *)((uintptr_t)pDie + pDieDesc->paAttributes[i].off) = UINT64_MAX;
2862 break;
2863 default:
2864 AssertFailed();
2865 memset((uint8_t *)pDie + pDieDesc->paAttributes[i].off, 0xff,
2866 pDieDesc->paAttributes[i].cbInit & ATTR_SIZE_MASK);
2867 break;
2868 }
2869 break;
2870
2871 default:
2872 AssertFailed();
2873 }
2874 }
2875}
2876
2877
2878/**
2879 * Creates a new internal DIE structure and links it up.
2880 *
2881 * @returns Pointer to the new DIE structure.
2882 * @param pThis The DWARF instance.
2883 * @param pDieDesc The DIE descriptor (for size and init).
2884 * @param pAbbrev The abbreviation cache entry.
2885 * @param pParent The parent DIE (NULL if unit).
2886 */
2887static PRTDWARFDIE rtDwarfInfo_NewDie(PRTDBGMODDWARF pThis, PCRTDWARFDIEDESC pDieDesc,
2888 PCRTDWARFABBREV pAbbrev, PRTDWARFDIE pParent)
2889{
2890 Assert(pDieDesc->cbDie >= sizeof(RTDWARFDIE));
2891 PRTDWARFDIE pDie = (PRTDWARFDIE)RTMemAllocZ(pDieDesc->cbDie);
2892 if (pDie)
2893 {
2894 rtDwarfInfo_InitDie(pDie, pDieDesc);
2895
2896 pDie->uTag = pAbbrev->uTag;
2897 pDie->offSpec = pAbbrev->offSpec;
2898 pDie->pParent = pParent;
2899 if (pParent)
2900 RTListAppend(&pParent->ChildList, &pDie->SiblingNode);
2901 else
2902 RTListInit(&pDie->SiblingNode);
2903 RTListInit(&pDie->ChildList);
2904
2905 }
2906 return pDie;
2907}
2908
2909
2910/**
2911 * Skips a form.
2912 * @returns IPRT status code
2913 * @param pCursor The cursor.
2914 * @param uForm The form to skip.
2915 */
2916static int rtDwarfInfo_SkipForm(PRTDWARFCURSOR pCursor, uint32_t uForm)
2917{
2918 switch (uForm)
2919 {
2920 case DW_FORM_addr:
2921 return rtDwarfCursor_SkipBytes(pCursor, pCursor->cbNativeAddr);
2922
2923 case DW_FORM_block:
2924 case DW_FORM_exprloc:
2925 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetULeb128(pCursor, 0));
2926
2927 case DW_FORM_block1:
2928 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU8(pCursor, 0));
2929
2930 case DW_FORM_block2:
2931 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU16(pCursor, 0));
2932
2933 case DW_FORM_block4:
2934 return rtDwarfCursor_SkipBytes(pCursor, rtDwarfCursor_GetU32(pCursor, 0));
2935
2936 case DW_FORM_data1:
2937 case DW_FORM_ref1:
2938 case DW_FORM_flag:
2939 return rtDwarfCursor_SkipBytes(pCursor, 1);
2940
2941 case DW_FORM_data2:
2942 case DW_FORM_ref2:
2943 return rtDwarfCursor_SkipBytes(pCursor, 2);
2944
2945 case DW_FORM_data4:
2946 case DW_FORM_ref4:
2947 return rtDwarfCursor_SkipBytes(pCursor, 4);
2948
2949 case DW_FORM_data8:
2950 case DW_FORM_ref8:
2951 case DW_FORM_ref_sig8:
2952 return rtDwarfCursor_SkipBytes(pCursor, 8);
2953
2954 case DW_FORM_udata:
2955 case DW_FORM_sdata:
2956 case DW_FORM_ref_udata:
2957 return rtDwarfCursor_SkipLeb128(pCursor);
2958
2959 case DW_FORM_string:
2960 rtDwarfCursor_GetSZ(pCursor, NULL);
2961 return pCursor->rc;
2962
2963 case DW_FORM_indirect:
2964 return rtDwarfInfo_SkipForm(pCursor, rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX));
2965
2966 case DW_FORM_strp:
2967 case DW_FORM_ref_addr:
2968 case DW_FORM_sec_offset:
2969 return rtDwarfCursor_SkipBytes(pCursor, pCursor->f64bitDwarf ? 8 : 4);
2970
2971 case DW_FORM_flag_present:
2972 return pCursor->rc; /* no data */
2973
2974 default:
2975 return VERR_DWARF_UNKNOWN_FORM;
2976 }
2977}
2978
2979
2980
2981#ifdef SOME_UNUSED_FUNCTION
2982/**
2983 * Skips a DIE.
2984 *
2985 * @returns IPRT status code.
2986 * @param pCursor The cursor.
2987 * @param pAbbrevCursor The abbreviation cursor.
2988 */
2989static int rtDwarfInfo_SkipDie(PRTDWARFCURSOR pCursor, PRTDWARFCURSOR pAbbrevCursor)
2990{
2991 for (;;)
2992 {
2993 uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(pAbbrevCursor, 0);
2994 uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(pAbbrevCursor, 0);
2995 if (uAttr == 0 && uForm == 0)
2996 break;
2997
2998 int rc = rtDwarfInfo_SkipForm(pCursor, uForm);
2999 if (RT_FAILURE(rc))
3000 return rc;
3001 }
3002 return RT_FAILURE(pCursor->rc) ? pCursor->rc : pAbbrevCursor->rc;
3003}
3004#endif
3005
3006
3007/**
3008 * Parse the attributes of a DIE.
3009 *
3010 * @returns IPRT status code.
3011 * @param pThis The DWARF instance.
3012 * @param pDie The internal DIE structure to fill.
3013 * @param pDieDesc The DIE descriptor.
3014 * @param pCursor The debug_info cursor.
3015 * @param pAbbrev The abbreviation cache entry.
3016 */
3017static int rtDwarfInfo_ParseDie(PRTDBGMODDWARF pThis, PRTDWARFDIE pDie, PCRTDWARFDIEDESC pDieDesc,
3018 PRTDWARFCURSOR pCursor, PCRTDWARFABBREV pAbbrev)
3019{
3020 RTDWARFCURSOR AbbrevCursor;
3021 int rc = rtDwarfCursor_InitWithOffset(&AbbrevCursor, pThis, krtDbgModDwarfSect_abbrev, pAbbrev->offSpec);
3022 if (RT_FAILURE(rc))
3023 return rc;
3024
3025 rtDwarfInfo_InitDie(pDie, pDieDesc);
3026 for (;;)
3027 {
3028 uint32_t uAttr = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
3029 uint32_t uForm = rtDwarfCursor_GetULeb128AsU32(&AbbrevCursor, 0);
3030 if (uAttr == 0)
3031 break;
3032 if (uForm == DW_FORM_indirect)
3033 uForm = rtDwarfCursor_GetULeb128AsU32(pCursor, 0);
3034
3035 /* Look up the attribute in the descriptor and invoke the decoder. */
3036 PCRTDWARFATTRDESC pAttr = NULL;
3037 size_t i = pDieDesc->cAttributes;
3038 while (i-- > 0)
3039 if (pDieDesc->paAttributes[i].uAttr == uAttr)
3040 {
3041 pAttr = &pDieDesc->paAttributes[i];
3042 rc = pAttr->pfnDecoder(pDie, (uint8_t *)pDie + pAttr->off, pAttr, uForm, pCursor);
3043 break;
3044 }
3045
3046 /* Some house keeping. */
3047 if (pAttr)
3048 pDie->cDecodedAttrs++;
3049 else
3050 {
3051 pDie->cUnhandledAttrs++;
3052 rc = rtDwarfInfo_SkipForm(pCursor, uForm);
3053 }
3054 if (RT_FAILURE(rc))
3055 break;
3056 }
3057
3058 rc = rtDwarfCursor_Delete(&AbbrevCursor, rc);
3059 if (RT_SUCCESS(rc))
3060 rc = pCursor->rc;
3061
3062 /*
3063 * Snoope up symbols on the way out.
3064 */
3065 if (RT_SUCCESS(rc))
3066 rc = rtDwarfInfo_SnoopSymbols(pThis, pDie);
3067
3068 return rc;
3069}
3070
3071
3072/**
3073 * Load the debug information of a unit.
3074 *
3075 * @returns IPRT status code.
3076 * @param pThis The DWARF instance.
3077 * @param pCursor The debug_info cursor.
3078 * @param fKeepDies Whether to keep the DIEs or discard them as soon
3079 * as possible.
3080 */
3081static int rtDwarfInfo_LoadUnit(PRTDBGMODDWARF pThis, PRTDWARFCURSOR pCursor, bool fKeepDies)
3082{
3083 Log(("rtDwarfInfo_LoadUnit: %#x\n", rtDwarfCursor_CalcSectOffsetU32(pCursor)));
3084
3085 /*
3086 * Read the compilation unit header.
3087 */
3088 uint64_t offUnit = rtDwarfCursor_CalcSectOffsetU32(pCursor);
3089 uint64_t cbUnit = rtDwarfCursor_GetInitalLength(pCursor);
3090 cbUnit += rtDwarfCursor_CalcSectOffsetU32(pCursor) - offUnit;
3091 uint16_t const uVer = rtDwarfCursor_GetUHalf(pCursor, 0);
3092 if ( uVer < 2
3093 || uVer > 4)
3094 return rtDwarfCursor_SkipUnit(pCursor);
3095 uint64_t const offAbbrev = rtDwarfCursor_GetUOff(pCursor, UINT64_MAX);
3096 uint8_t const cbNativeAddr = rtDwarfCursor_GetU8(pCursor, UINT8_MAX);
3097 if (RT_FAILURE(pCursor->rc))
3098 return pCursor->rc;
3099 Log((" uVer=%d offAbbrev=%#llx cbNativeAddr=%d\n", uVer, offAbbrev, cbNativeAddr));
3100
3101 /*
3102 * Set up the abbreviation cache and store the native address size in the cursor.
3103 */
3104 if (offAbbrev > UINT32_MAX)
3105 return VERR_DWARF_BAD_INFO;
3106 rtDwarfAbbrev_SetUnitOffset(pThis, (uint32_t)offAbbrev);
3107 pCursor->cbNativeAddr = cbNativeAddr;
3108
3109 /*
3110 * The first DIE is a compile or partial unit, parse it here.
3111 */
3112 uint32_t uAbbrCode = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
3113 if (!uAbbrCode)
3114 return VERR_DWARF_BAD_INFO;
3115 PCRTDWARFABBREV pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
3116 if (!pAbbrev)
3117 return VERR_DWARF_ABBREV_NOT_FOUND;
3118 if ( pAbbrev->uTag != DW_TAG_compile_unit
3119 && pAbbrev->uTag != DW_TAG_partial_unit)
3120 {
3121 Log(("Unexpected compile/partial unit tag %#x\n", pAbbrev->uTag));
3122 return VERR_DWARF_BAD_INFO;
3123 }
3124
3125 PRTDWARFDIECOMPILEUNIT pUnit;
3126 pUnit = (PRTDWARFDIECOMPILEUNIT)rtDwarfInfo_NewDie(pThis, &g_CompileUnitDesc, pAbbrev, NULL /*pParent*/);
3127 if (!pUnit)
3128 return VERR_NO_MEMORY;
3129 pUnit->offUnit = offUnit;
3130 pUnit->cbUnit = cbUnit;
3131 pUnit->offAbbrev = offAbbrev;
3132 pUnit->cbNativeAddr = cbNativeAddr;
3133 pUnit->uDwarfVer = (uint8_t)uVer;
3134 RTListAppend(&pThis->CompileUnitList, &pUnit->Core.SiblingNode);
3135
3136 int rc = rtDwarfInfo_ParseDie(pThis, &pUnit->Core, &g_CompileUnitDesc, pCursor, pAbbrev);
3137 if (RT_FAILURE(rc))
3138 return rc;
3139
3140 /*
3141 * Parse DIEs.
3142 */
3143 uint32_t cDepth = 0;
3144 PRTDWARFDIE pParentDie = &pUnit->Core;
3145 while (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
3146 {
3147 uAbbrCode = rtDwarfCursor_GetULeb128AsU32(pCursor, UINT32_MAX);
3148 if (!uAbbrCode)
3149 {
3150 /* End of siblings, up one level. */
3151 pParentDie = pParentDie->pParent;
3152 if (!pParentDie)
3153 {
3154 if (!rtDwarfCursor_IsAtEndOfUnit(pCursor))
3155 return VERR_DWARF_BAD_INFO;
3156 break;
3157 }
3158 cDepth--;
3159
3160 /* Unlink and free child DIEs if told to do so. */
3161 if (!fKeepDies && pParentDie->pParent)
3162 {
3163 PRTDWARFDIE pChild, pNextChild;
3164 RTListForEachSafe(&pParentDie->ChildList, pChild, pNextChild, RTDWARFDIE, SiblingNode)
3165 {
3166 RTListNodeRemove(&pChild->SiblingNode);
3167 RTMemFree(pChild);
3168 }
3169 }
3170 }
3171 else
3172 {
3173 /*
3174 * Look up the abbreviation and match the tag up with a descriptor.
3175 */
3176 pAbbrev = rtDwarfAbbrev_Lookup(pThis, uAbbrCode);
3177 if (!pAbbrev)
3178 return VERR_DWARF_ABBREV_NOT_FOUND;
3179
3180 PCRTDWARFDIEDESC pDieDesc;
3181 const char *pszName;
3182 if (pAbbrev->uTag < RT_ELEMENTS(g_aTagDescs))
3183 {
3184 Assert(g_aTagDescs[pAbbrev->uTag].uTag == pAbbrev->uTag || g_aTagDescs[pAbbrev->uTag].uTag == 0);
3185 pszName = g_aTagDescs[pAbbrev->uTag].pszName;
3186 pDieDesc = g_aTagDescs[pAbbrev->uTag].pDesc;
3187 }
3188 else
3189 {
3190 pszName = "<unknown>";
3191 pDieDesc = g_aTagDescs[0].pDesc;
3192 }
3193 Log4((" %*stag=%s (%#x)%s\n", cDepth * 2, "", pszName,
3194 pAbbrev->uTag, pAbbrev->fChildren ? " has children" : ""));
3195
3196 /*
3197 * Create a new internal DIE structure and parse the
3198 * attributes.
3199 */
3200 PRTDWARFDIE pNewDie = rtDwarfInfo_NewDie(pThis, pDieDesc, pAbbrev, pParentDie);
3201 if (!pNewDie)
3202 return VERR_NO_MEMORY;
3203
3204 if (pAbbrev->fChildren)
3205 {
3206 pParentDie = pNewDie;
3207 cDepth++;
3208 }
3209
3210 rc = rtDwarfInfo_ParseDie(pThis, pNewDie, pDieDesc, pCursor, pAbbrev);
3211 if (RT_FAILURE(rc))
3212 return rc;
3213 }
3214 } /* while more DIEs */
3215
3216 return RT_SUCCESS(rc) ? pCursor->rc : rc;
3217}
3218
3219
3220/**
3221 * Extracts the symbols.
3222 *
3223 * The symbols are insered into the debug info container.
3224 *
3225 * @returns IPRT status code
3226 * @param pThis The DWARF instance.
3227 */
3228static int rtDwarfInfo_LoadAll(PRTDBGMODDWARF pThis)
3229{
3230 RTDWARFCURSOR Cursor;
3231 int rc = rtDwarfCursor_Init(&Cursor, pThis, krtDbgModDwarfSect_info);
3232 if (RT_FAILURE(rc))
3233 return rc;
3234
3235 while ( !rtDwarfCursor_IsAtEnd(&Cursor)
3236 && RT_SUCCESS(rc))
3237 rc = rtDwarfInfo_LoadUnit(pThis, &Cursor, false /* fKeepDies */);
3238
3239 return rtDwarfCursor_Delete(&Cursor, rc);
3240}
3241
3242
3243
3244
3245/*
3246 *
3247 * DWARF Debug module implementation.
3248 * DWARF Debug module implementation.
3249 * DWARF Debug module implementation.
3250 *
3251 */
3252
3253
3254/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByAddr} */
3255static DECLCALLBACK(int) rtDbgModDwarf_LineByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
3256 PRTINTPTR poffDisp, PRTDBGLINE pLineInfo)
3257{
3258 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3259 return RTDbgModLineByAddr(pThis->hCnt, iSeg, off, poffDisp, pLineInfo);
3260}
3261
3262
3263/** @interface_method_impl{RTDBGMODVTDBG,pfnLineByOrdinal} */
3264static DECLCALLBACK(int) rtDbgModDwarf_LineByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGLINE pLineInfo)
3265{
3266 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3267 return RTDbgModLineByOrdinal(pThis->hCnt, iOrdinal, pLineInfo);
3268}
3269
3270
3271/** @interface_method_impl{RTDBGMODVTDBG,pfnLineCount} */
3272static DECLCALLBACK(uint32_t) rtDbgModDwarf_LineCount(PRTDBGMODINT pMod)
3273{
3274 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3275 return RTDbgModLineCount(pThis->hCnt);
3276}
3277
3278
3279/** @interface_method_impl{RTDBGMODVTDBG,pfnLineAdd} */
3280static DECLCALLBACK(int) rtDbgModDwarf_LineAdd(PRTDBGMODINT pMod, const char *pszFile, size_t cchFile, uint32_t uLineNo,
3281 uint32_t iSeg, RTUINTPTR off, uint32_t *piOrdinal)
3282{
3283 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3284 return RTDbgModLineAdd(pThis->hCnt, pszFile, uLineNo, iSeg, off, piOrdinal);
3285}
3286
3287
3288/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByAddr} */
3289static DECLCALLBACK(int) rtDbgModDwarf_SymbolByAddr(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, RTUINTPTR off,
3290 PRTINTPTR poffDisp, PRTDBGSYMBOL pSymInfo)
3291{
3292 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3293 return RTDbgModSymbolByAddr(pThis->hCnt, iSeg, off, poffDisp, pSymInfo);
3294}
3295
3296
3297/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByName} */
3298static DECLCALLBACK(int) rtDbgModDwarf_SymbolByName(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
3299 PRTDBGSYMBOL pSymInfo)
3300{
3301 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3302 Assert(!pszSymbol[cchSymbol]);
3303 return RTDbgModSymbolByName(pThis->hCnt, pszSymbol/*, cchSymbol*/, pSymInfo);
3304}
3305
3306
3307/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolByOrdinal} */
3308static DECLCALLBACK(int) rtDbgModDwarf_SymbolByOrdinal(PRTDBGMODINT pMod, uint32_t iOrdinal, PRTDBGSYMBOL pSymInfo)
3309{
3310 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3311 return RTDbgModSymbolByOrdinal(pThis->hCnt, iOrdinal, pSymInfo);
3312}
3313
3314
3315/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolCount} */
3316static DECLCALLBACK(uint32_t) rtDbgModDwarf_SymbolCount(PRTDBGMODINT pMod)
3317{
3318 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3319 return RTDbgModSymbolCount(pThis->hCnt);
3320}
3321
3322
3323/** @interface_method_impl{RTDBGMODVTDBG,pfnSymbolAdd} */
3324static DECLCALLBACK(int) rtDbgModDwarf_SymbolAdd(PRTDBGMODINT pMod, const char *pszSymbol, size_t cchSymbol,
3325 RTDBGSEGIDX iSeg, RTUINTPTR off, RTUINTPTR cb, uint32_t fFlags,
3326 uint32_t *piOrdinal)
3327{
3328 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3329 return RTDbgModSymbolAdd(pThis->hCnt, pszSymbol, iSeg, off, cb, fFlags, piOrdinal);
3330}
3331
3332
3333/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentByIndex} */
3334static DECLCALLBACK(int) rtDbgModDwarf_SegmentByIndex(PRTDBGMODINT pMod, RTDBGSEGIDX iSeg, PRTDBGSEGMENT pSegInfo)
3335{
3336 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3337 return RTDbgModSegmentByIndex(pThis->hCnt, iSeg, pSegInfo);
3338}
3339
3340
3341/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentCount} */
3342static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_SegmentCount(PRTDBGMODINT pMod)
3343{
3344 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3345 return RTDbgModSegmentCount(pThis->hCnt);
3346}
3347
3348
3349/** @interface_method_impl{RTDBGMODVTDBG,pfnSegmentAdd} */
3350static DECLCALLBACK(int) rtDbgModDwarf_SegmentAdd(PRTDBGMODINT pMod, RTUINTPTR uRva, RTUINTPTR cb, const char *pszName, size_t cchName,
3351 uint32_t fFlags, PRTDBGSEGIDX piSeg)
3352{
3353 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3354 return RTDbgModSegmentAdd(pThis->hCnt, uRva, cb, pszName, fFlags, piSeg);
3355}
3356
3357
3358/** @interface_method_impl{RTDBGMODVTDBG,pfnImageSize} */
3359static DECLCALLBACK(RTUINTPTR) rtDbgModDwarf_ImageSize(PRTDBGMODINT pMod)
3360{
3361 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3362 RTUINTPTR cb1 = RTDbgModImageSize(pThis->hCnt);
3363 RTUINTPTR cb2 = pMod->pImgVt->pfnImageSize(pMod);
3364 return RT_MAX(cb1, cb2);
3365}
3366
3367
3368/** @interface_method_impl{RTDBGMODVTDBG,pfnRvaToSegOff} */
3369static DECLCALLBACK(RTDBGSEGIDX) rtDbgModDwarf_RvaToSegOff(PRTDBGMODINT pMod, RTUINTPTR uRva, PRTUINTPTR poffSeg)
3370{
3371 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3372 return RTDbgModRvaToSegOff(pThis->hCnt, uRva, poffSeg);
3373}
3374
3375
3376/** @interface_method_impl{RTDBGMODVTDBG,pfnClose} */
3377static DECLCALLBACK(int) rtDbgModDwarf_Close(PRTDBGMODINT pMod)
3378{
3379 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pMod->pvDbgPriv;
3380
3381 for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
3382 if (pThis->aSections[iSect].pv)
3383 pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[iSect].cb, &pThis->aSections[iSect].pv);
3384
3385 RTDbgModRelease(pThis->hCnt);
3386 RTMemFree(pThis->paCachedAbbrevs);
3387 RTMemFree(pThis);
3388
3389 return VINF_SUCCESS;
3390}
3391
3392
3393/** @callback_method_impl{FNRTLDRENUMDBG} */
3394static DECLCALLBACK(int) rtDbgModDwarfEnumCallback(RTLDRMOD hLdrMod, uint32_t iDbgInfo, RTLDRDBGINFOTYPE enmType,
3395 uint16_t iMajorVer, uint16_t iMinorVer, const char *pszPartNm,
3396 RTFOFF offFile, RTLDRADDR LinkAddress, RTLDRADDR cb,
3397 const char *pszExtFile, void *pvUser)
3398{
3399 /*
3400 * Skip stuff we can't handle.
3401 */
3402 if ( enmType != RTLDRDBGINFOTYPE_DWARF
3403 || !pszPartNm
3404 || pszExtFile)
3405 return VINF_SUCCESS;
3406
3407 /*
3408 * Must have a part name starting with debug_ and possibly prefixed by dots
3409 * or underscores.
3410 */
3411 if (!strncmp(pszPartNm, ".debug_", sizeof(".debug_") - 1)) /* ELF */
3412 pszPartNm += sizeof(".debug_") - 1;
3413 else if (!strncmp(pszPartNm, "__debug_", sizeof("__debug_") - 1)) /* Mach-O */
3414 pszPartNm += sizeof("__debug_") - 1;
3415 else
3416 AssertMsgFailedReturn(("%s\n", pszPartNm), VINF_SUCCESS /*ignore*/);
3417
3418 /*
3419 * Figure out which part we're talking about.
3420 */
3421 krtDbgModDwarfSect enmSect;
3422 if (0) { /* dummy */ }
3423#define ELSE_IF_STRCMP_SET(a_Name) else if (!strcmp(pszPartNm, #a_Name)) enmSect = krtDbgModDwarfSect_ ## a_Name
3424 ELSE_IF_STRCMP_SET(abbrev);
3425 ELSE_IF_STRCMP_SET(aranges);
3426 ELSE_IF_STRCMP_SET(frame);
3427 ELSE_IF_STRCMP_SET(info);
3428 ELSE_IF_STRCMP_SET(inlined);
3429 ELSE_IF_STRCMP_SET(line);
3430 ELSE_IF_STRCMP_SET(loc);
3431 ELSE_IF_STRCMP_SET(macinfo);
3432 ELSE_IF_STRCMP_SET(pubnames);
3433 ELSE_IF_STRCMP_SET(pubtypes);
3434 ELSE_IF_STRCMP_SET(ranges);
3435 ELSE_IF_STRCMP_SET(str);
3436 ELSE_IF_STRCMP_SET(types);
3437#undef ELSE_IF_STRCMP_SET
3438 else
3439 {
3440 AssertMsgFailed(("%s\n", pszPartNm));
3441 return VINF_SUCCESS;
3442 }
3443
3444 /*
3445 * Record the section.
3446 */
3447 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)pvUser;
3448 AssertMsgReturn(!pThis->aSections[enmSect].fPresent, ("duplicate %s\n", pszPartNm), VINF_SUCCESS /*ignore*/);
3449
3450 pThis->aSections[enmSect].fPresent = true;
3451 pThis->aSections[enmSect].offFile = offFile;
3452 pThis->aSections[enmSect].pv = NULL;
3453 pThis->aSections[enmSect].cb = (size_t)cb;
3454 if (pThis->aSections[enmSect].cb != cb)
3455 pThis->aSections[enmSect].cb = ~(size_t)0;
3456
3457 return VINF_SUCCESS;
3458}
3459
3460
3461/** @interface_method_impl{RTDBGMODVTDBG,pfnTryOpen} */
3462static DECLCALLBACK(int) rtDbgModDwarf_TryOpen(PRTDBGMODINT pMod)
3463{
3464 /*
3465 * DWARF is only supported when part of an image.
3466 */
3467 if (!pMod->pImgVt)
3468 return VERR_DBG_NO_MATCHING_INTERPRETER;
3469
3470 /*
3471 * Enumerate the debug info in the module, looking for DWARF bits.
3472 */
3473 PRTDBGMODDWARF pThis = (PRTDBGMODDWARF)RTMemAllocZ(sizeof(*pThis));
3474 if (!pThis)
3475 return VERR_NO_MEMORY;
3476 pThis->pMod = pMod;
3477 RTListInit(&pThis->CompileUnitList);
3478
3479 int rc = pMod->pImgVt->pfnEnumDbgInfo(pMod, rtDbgModDwarfEnumCallback, pThis);
3480 if (RT_SUCCESS(rc))
3481 {
3482 if (pThis->aSections[krtDbgModDwarfSect_info].fPresent)
3483 {
3484 /*
3485 * Extract / explode the data we want (symbols and line numbers)
3486 * storing them in a container module.
3487 */
3488 rc = RTDbgModCreate(&pThis->hCnt, pMod->pszName, 0 /*cbSeg*/, 0 /*fFlags*/);
3489 if (RT_SUCCESS(rc))
3490 {
3491 pMod->pvDbgPriv = pThis;
3492
3493 rc = rtDbgModHlpAddSegmentsFromImage(pMod);
3494 if (RT_SUCCESS(rc))
3495 rc = rtDwarfInfo_LoadAll(pThis);
3496 if (RT_SUCCESS(rc))
3497 rc = rtDwarfLine_ExplodeAll(pThis);
3498 if (RT_SUCCESS(rc))
3499 {
3500 /*
3501 * Free the cached abbreviations and unload all sections.
3502 */
3503 pThis->cCachedAbbrevs = pThis->cCachedAbbrevsAlloced = 0;
3504 RTMemFree(pThis->paCachedAbbrevs);
3505
3506 for (unsigned iSect = 0; iSect < RT_ELEMENTS(pThis->aSections); iSect++)
3507 if (pThis->aSections[iSect].pv)
3508 pThis->pMod->pImgVt->pfnUnmapPart(pThis->pMod, pThis->aSections[iSect].cb,
3509 &pThis->aSections[iSect].pv);
3510
3511
3512 return VINF_SUCCESS;
3513 }
3514
3515 /* bail out. */
3516 RTDbgModRelease(pThis->hCnt);
3517 pMod->pvDbgPriv = NULL;
3518 }
3519 }
3520 else
3521 rc = VERR_DBG_NO_MATCHING_INTERPRETER;
3522 }
3523 RTMemFree(pThis->paCachedAbbrevs);
3524 RTMemFree(pThis);
3525
3526 return rc;
3527}
3528
3529
3530
3531/** Virtual function table for the DWARF debug info reader. */
3532DECL_HIDDEN_CONST(RTDBGMODVTDBG) const g_rtDbgModVtDbgDwarf =
3533{
3534 /*.u32Magic = */ RTDBGMODVTDBG_MAGIC,
3535 /*.fSupports = */ RT_DBGTYPE_DWARF,
3536 /*.pszName = */ "dwarf",
3537 /*.pfnTryOpen = */ rtDbgModDwarf_TryOpen,
3538 /*.pfnClose = */ rtDbgModDwarf_Close,
3539
3540 /*.pfnRvaToSegOff = */ rtDbgModDwarf_RvaToSegOff,
3541 /*.pfnImageSize = */ rtDbgModDwarf_ImageSize,
3542
3543 /*.pfnSegmentAdd = */ rtDbgModDwarf_SegmentAdd,
3544 /*.pfnSegmentCount = */ rtDbgModDwarf_SegmentCount,
3545 /*.pfnSegmentByIndex = */ rtDbgModDwarf_SegmentByIndex,
3546
3547 /*.pfnSymbolAdd = */ rtDbgModDwarf_SymbolAdd,
3548 /*.pfnSymbolCount = */ rtDbgModDwarf_SymbolCount,
3549 /*.pfnSymbolByOrdinal = */ rtDbgModDwarf_SymbolByOrdinal,
3550 /*.pfnSymbolByName = */ rtDbgModDwarf_SymbolByName,
3551 /*.pfnSymbolByAddr = */ rtDbgModDwarf_SymbolByAddr,
3552
3553 /*.pfnLineAdd = */ rtDbgModDwarf_LineAdd,
3554 /*.pfnLineCount = */ rtDbgModDwarf_LineCount,
3555 /*.pfnLineByOrdinal = */ rtDbgModDwarf_LineByOrdinal,
3556 /*.pfnLineByAddr = */ rtDbgModDwarf_LineByAddr,
3557
3558 /*.u32EndMagic = */ RTDBGMODVTDBG_MAGIC
3559};
3560
Note: See TracBrowser for help on using the repository browser.

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