VirtualBox

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

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

symbol hacking.

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