VirtualBox

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

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

IPRT: Fixed unused variable warnings.

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

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